Skip to content

Commit 927fa1b

Browse files
authored
Check whether values are constant before smooth (#1698)
Due to IEEE 754 floating-point precision, multiplying floating smoothing factor into a value can cause discrepancy in otherwise mathematically constant value. Please see #786 for examples of weird spikes/messed up y-scale. Partially addresses #786.
1 parent 485deb2 commit 927fa1b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

tensorboard/components/vz_line_chart/vz-line-chart.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,13 @@ class LineChart {
10151015
// frequency components of the time-series.
10161016
let last = data.length > 0 ? 0 : NaN;
10171017
let numAccum = 0;
1018+
1019+
const yValues = data.map((d, i) => this.yValueAccessor(d, i, dataset));
1020+
// See #786.
1021+
const isConstant = yValues.every((v) => v == yValues[0]);
10181022
data.forEach((d, i) => {
1019-
let nextVal = this.yValueAccessor(d, i, dataset);
1020-
if (!Number.isFinite(nextVal)) {
1023+
const nextVal = yValues[i];
1024+
if (isConstant || !Number.isFinite(nextVal)) {
10211025
d.smoothed = nextVal;
10221026
} else {
10231027
last = last * smoothingWeight + (1 - smoothingWeight) * nextVal;

tensorboard/components/vz_line_chart2/line-chart.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,13 @@ export class LineChart {
707707
// frequency components of the time-series.
708708
let last = data.length > 0 ? 0 : NaN;
709709
let numAccum = 0;
710+
711+
const yValues = data.map((d, i) => this.yValueAccessor(d, i, dataset));
712+
// See #786.
713+
const isConstant = yValues.every((v) => v == yValues[0]);
710714
data.forEach((d, i) => {
711-
let nextVal = this.yValueAccessor(d, i, dataset);
712-
if (!Number.isFinite(nextVal)) {
715+
const nextVal = yValues[i];
716+
if (isConstant || !Number.isFinite(nextVal)) {
713717
d.smoothed = nextVal;
714718
} else {
715719
last = last * smoothingWeight + (1 - smoothingWeight) * nextVal;

0 commit comments

Comments
 (0)