From 6457f78ed9edfca6dfc9f2c62dc916d979bc855a Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 12 Nov 2026 13:25:26 +0530 Subject: [PATCH] fix the last windows of difference, ratio, first and last These reducers stopped their loops early: with strict the last full window was skipped, and without strict the last k - 1 - s truncated windows were skipped, so a windowed line stopped short of the right edge. Loop to I.length - k + 1 (strict) and I.length - s (not strict), like the other reducers. Fixes #2472 --- src/transforms/window.js | 16 ++-- test/output/metroUnemploymentSlope.svg | 120 +++++++++++++++---------- test/transforms/window-test.js | 21 ++++- 3 files changed, 100 insertions(+), 57 deletions(-) diff --git a/src/transforms/window.js b/src/transforms/window.js index a9a918927a..95e4a1ca5f 100644 --- a/src/transforms/window.js +++ b/src/transforms/window.js @@ -244,7 +244,7 @@ function reduceDifference(k, s, strict) { return strict ? { mapIndex(I, S, T) { - for (let i = 0, n = I.length - k; i < n; ++i) { + for (let i = 0, n = I.length - k + 1; i < n; ++i) { const a = S[I[i]]; const b = S[I[i + k - 1]]; T[I[i + s]] = a === null || b === null ? NaN : b - a; @@ -253,7 +253,7 @@ function reduceDifference(k, s, strict) { } : { mapIndex(I, S, T) { - for (let i = -s, n = I.length - k + s + 1; i < n; ++i) { + for (let i = -s, n = I.length - s; i < n; ++i) { T[I[i + s]] = lastNumber(S, I, i, k) - firstNumber(S, I, i, k); } } @@ -264,7 +264,7 @@ function reduceRatio(k, s, strict) { return strict ? { mapIndex(I, S, T) { - for (let i = 0, n = I.length - k; i < n; ++i) { + for (let i = 0, n = I.length - k + 1; i < n; ++i) { const a = S[I[i]]; const b = S[I[i + k - 1]]; T[I[i + s]] = a === null || b === null ? NaN : b / a; @@ -273,7 +273,7 @@ function reduceRatio(k, s, strict) { } : { mapIndex(I, S, T) { - for (let i = -s, n = I.length - k + s + 1; i < n; ++i) { + for (let i = -s, n = I.length - s; i < n; ++i) { T[I[i + s]] = lastNumber(S, I, i, k) / firstNumber(S, I, i, k); } } @@ -284,14 +284,14 @@ function reduceFirst(k, s, strict) { return strict ? { mapIndex(I, S, T) { - for (let i = 0, n = I.length - k; i < n; ++i) { + for (let i = 0, n = I.length - k + 1; i < n; ++i) { T[I[i + s]] = S[I[i]]; } } } : { mapIndex(I, S, T) { - for (let i = -s, n = I.length - k + s + 1; i < n; ++i) { + for (let i = -s, n = I.length - s; i < n; ++i) { T[I[i + s]] = firstDefined(S, I, i, k); } } @@ -302,14 +302,14 @@ function reduceLast(k, s, strict) { return strict ? { mapIndex(I, S, T) { - for (let i = 0, n = I.length - k; i < n; ++i) { + for (let i = 0, n = I.length - k + 1; i < n; ++i) { T[I[i + s]] = S[I[i + k - 1]]; } } } : { mapIndex(I, S, T) { - for (let i = -s, n = I.length - k + s + 1; i < n; ++i) { + for (let i = -s, n = I.length - s; i < n; ++i) { T[I[i + s]] = lastDefined(S, I, i, k); } } diff --git a/test/output/metroUnemploymentSlope.svg b/test/output/metroUnemploymentSlope.svg index def52b46de..66b32ef7ab 100644 --- a/test/output/metroUnemploymentSlope.svg +++ b/test/output/metroUnemploymentSlope.svg @@ -150,7 +150,7 @@ - + @@ -236,7 +236,8 @@ - + + @@ -332,7 +333,8 @@ - + + @@ -437,7 +439,8 @@ - + + @@ -542,7 +545,8 @@ - + + @@ -634,7 +638,8 @@ - + + @@ -721,7 +726,8 @@ - + + @@ -815,7 +821,7 @@ - + @@ -915,7 +921,7 @@ - + @@ -1014,7 +1020,8 @@ - + + @@ -1107,7 +1114,8 @@ - + + @@ -1198,7 +1206,7 @@ - + @@ -1286,7 +1294,8 @@ - + + @@ -1370,7 +1379,7 @@ - + @@ -1454,7 +1463,8 @@ - + + @@ -1557,7 +1567,8 @@ - + + @@ -1640,7 +1651,8 @@ - + + @@ -1735,7 +1747,7 @@ - + @@ -1828,7 +1840,7 @@ - + @@ -1934,7 +1946,8 @@ - + + @@ -2040,7 +2053,8 @@ - + + @@ -2125,7 +2139,8 @@ - + + @@ -2218,7 +2233,7 @@ - + @@ -2315,7 +2330,8 @@ - + + @@ -2414,7 +2430,8 @@ - + + @@ -2502,7 +2519,7 @@ - + @@ -2606,7 +2623,8 @@ - + + @@ -2695,7 +2713,8 @@ - + + @@ -2785,7 +2804,7 @@ - + @@ -2872,7 +2891,7 @@ - + @@ -2972,7 +2991,7 @@ - + @@ -3057,7 +3076,8 @@ - + + @@ -3149,7 +3169,8 @@ - + + @@ -3238,7 +3259,7 @@ - + @@ -3329,7 +3350,7 @@ - + @@ -3417,7 +3438,7 @@ - + @@ -3507,7 +3528,8 @@ - + + @@ -3603,7 +3625,8 @@ - + + @@ -3707,7 +3730,8 @@ - + + @@ -3809,7 +3833,8 @@ - + + @@ -3905,7 +3930,8 @@ - + + @@ -3998,7 +4024,8 @@ - + + @@ -4087,7 +4114,8 @@ - + + @@ -4174,7 +4202,8 @@ - + + @@ -4258,7 +4287,8 @@ - + + diff --git a/test/transforms/window-test.js b/test/transforms/window-test.js index 280ba478c8..896c88fa19 100644 --- a/test/transforms/window-test.js +++ b/test/transforms/window-test.js @@ -208,7 +208,7 @@ it(`windowX({reduce: "difference", k, strict: true}) produces invalid values if NaN, NaN, NaN, - , + NaN, , ]); }); @@ -250,7 +250,7 @@ it(`windowX({reduce: "ratio", k, strict: true}) produces invalid values if the c NaN, NaN, NaN, - , + NaN, , ]); }); @@ -279,7 +279,7 @@ it(`windowX({reduce: "ratio", k}) returns the expected values`, () => { it(`windowX({reduce: "first", k, strict: true}) produces NaN if the current window starts with NaN`, () => { const data = [1, 2, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, NaN, NaN, 256]; const m3 = applyTransform(Plot.windowX({reduce: "first", k: 3, strict: true, x: (d) => d}), data); - assert.deepStrictEqual(m3.x.transform(), [, 1, 2, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, , ,]); + assert.deepStrictEqual(m3.x.transform(), [, 1, 2, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, NaN, ,]); }); it(`windowX({reduce: "first", k}) returns the expected values`, () => { @@ -297,7 +297,7 @@ it(`windowX({reduce: "first", k}) does not coerce to numbers`, () => { it(`windowX({reduce: "last", k, strict: true}) produces NaN if the current window ends with NaN`, () => { const data = [1, 2, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, NaN, NaN, 256]; const m3 = applyTransform(Plot.windowX({reduce: "last", k: 3, strict: true, x: (d) => d}), data); - assert.deepStrictEqual(m3.x.transform(), [, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, NaN, NaN, , ,]); + assert.deepStrictEqual(m3.x.transform(), [, 4, NaN, 8, 16, 32, 64, 128, NaN, NaN, NaN, NaN, 256, ,]); }); it(`windowX({reduce: "last", k}) returns the expected values`, () => { @@ -311,3 +311,16 @@ it(`windowX({reduce: "last", k}) does not coerce to numbers`, () => { const m3 = applyTransform(Plot.windowX({reduce: "last", k: 3, x: (d) => d}), data); assert.deepStrictEqual(m3.x.transform(), ["B", "A", "A", "C", "C", "A", "B", "B", "B", "B"]); }); + +it(`windowX({reduce: "difference" | "ratio" | "first" | "last", k}) fills the last windows`, () => { + const data = [2, 4, 8, 16]; + const t = (options) => applyTransform(Plot.windowX({...options, x: (d) => d}), data).x.transform(); + assert.deepStrictEqual(t({reduce: "difference", k: 2, strict: true, anchor: "end"}), [, 2, 4, 8]); + assert.deepStrictEqual(t({reduce: "ratio", k: 2, strict: true, anchor: "end"}), [, 2, 2, 2]); + assert.deepStrictEqual(t({reduce: "first", k: 1, strict: true}), [2, 4, 8, 16]); + assert.deepStrictEqual(t({reduce: "last", k: 1, strict: true}), [2, 4, 8, 16]); + assert.deepStrictEqual(t({reduce: "first", k: 3, anchor: "start"}), [2, 4, 8, 16]); + assert.deepStrictEqual(t({reduce: "last", k: 3, anchor: "start"}), [8, 16, 16, 16]); + assert.deepStrictEqual(t({reduce: "difference", k: 3, anchor: "start"}), [6, 12, 8, 0]); + assert.deepStrictEqual(t({reduce: "ratio", k: 3, anchor: "start"}), [4, 4, 2, 1]); +});