Skip to content

Commit

Permalink
Implement commitChanges on vz-line-chart2 (tensorflow#3571)
Browse files Browse the repository at this point in the history
This is a roll forward of 46b6e61. The commit partiall rolls back
27d0023 which introduces `commitChanges` which was supposed to be
invoked after setting data, visible series, or metadata.

This change addresses the regression caused by 27d0023 which forgot to
call the new API in all places where we use the vz-line-chart2.
  • Loading branch information
stephanwlee authored and caisq committed May 19, 2020
1 parent 4db0e43 commit 6274951
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,8 @@
this.$.chart.setSeriesMetadata(name, metadata);
},

/**
* Not yet implemented.
*/
commitChanges() {
// Temporarily rolled back due to PR curves breakage.
this.$.chart.commitChanges();
},

redraw() {
Expand Down
81 changes: 48 additions & 33 deletions tensorboard/components/vz_line_chart2/line-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ namespace vz_line_chart2 {
private lastPointsDataset: Plottable.Dataset;
private fillArea?: FillArea;
private datasets: Plottable.Dataset[];
private onDatasetChanged: (dataset: Plottable.Dataset) => void;
private nanDataset: Plottable.Dataset;
private smoothingWeight: number;
private smoothingEnabled: boolean;
Expand Down Expand Up @@ -139,10 +138,6 @@ namespace vz_line_chart2 {
// varies based on whether smoothing is enabled.
this.symbolFunction = symbolFunction;

// need to do a single bind, so we can deregister the callback from
// old Plottable.Datasets. (Deregistration is done by identity checks.)
this.onDatasetChanged = this._onDatasetChanged.bind(this);

this._defaultXRange = defaultXRange;
this._defaultYRange = defaultYRange;
this.tooltipColumns = tooltipColumns;
Expand Down Expand Up @@ -322,16 +317,6 @@ namespace vz_line_chart2 {
return new Plottable.Components.Group(groups);
}

/** Updates the chart when a dataset changes. Called every time the data of
* a dataset changes to update the charts.
*/
private _onDatasetChanged(dataset: Plottable.Dataset) {
if (this.smoothingEnabled) {
this.resmoothDataset(dataset);
}
this.updateSpecialDatasets();
}

public ignoreYOutliers(ignoreYOutliers: boolean) {
if (ignoreYOutliers !== this._ignoreYOutliers) {
this._ignoreYOutliers = ignoreYOutliers;
Expand Down Expand Up @@ -833,32 +818,53 @@ namespace vz_line_chart2 {
}

/**
* Update the selected series on the chart.
* Stages update of visible series on the chart.
*
* Please call `commitChanges` for the changes to be reflected on the chart
* after making all the changes.
*/
public setVisibleSeries(names: string[]) {
this.disableChanges();
names = names.sort();
names.reverse(); // draw first series on top
this.seriesNames = names;
}

names.reverse(); // draw first series on top
this.datasets.forEach((d) => d.offUpdate(this.onDatasetChanged));
this.datasets = names.map((r) => this.getDataset(r));
this.datasets.forEach((d) => d.onUpdate(this.onDatasetChanged));
this.linePlot.datasets(this.datasets);
private dirtyDatasets = new Set<string>();

private disableChanges() {
if (!this.dirtyDatasets.size) {
// Prevent plots from reacting to the dataset changes.
this.linePlot.datasets([]);
if (this.smoothLinePlot) {
this.smoothLinePlot.datasets([]);
}

if (this.marginAreaPlot) {
this.marginAreaPlot.datasets([]);
}
}
}

public commitChanges() {
this.datasets = this.seriesNames.map((r) => this.getDataset(r));
[...this.dirtyDatasets].forEach((d) => {
if (this.smoothingEnabled) {
this.resmoothDataset(this.getDataset(d));
}
});
this.updateSpecialDatasets();

this.linePlot.datasets(this.datasets);
if (this.smoothingEnabled) {
this.smoothLinePlot.datasets(this.datasets);
}
if (this.marginAreaPlot) {
this.marginAreaPlot.datasets(this.datasets);
}
this.updateSpecialDatasets();
}

/**
* Not yet implemented.
*/
public commitChanges() {
// Temporarily rolled back due to PR curves breakage.
this.measureBBoxAndMaybeInvalidateLayoutInRaf();
this.dirtyDatasets.clear();
}

/**
Expand All @@ -885,21 +891,30 @@ namespace vz_line_chart2 {
}

/**
* Sets the data of a series on the chart.
* Stages a data change of a series on the chart.
*
* Please call `commitChanges` for the changes to be reflected on the chart
* after making all the changes.
*/
public setSeriesData(name: string, data: vz_chart_helpers.ScalarDatum[]) {
this.disableChanges();
this.getDataset(name).data(data);
this.measureBBoxAndMaybeInvalidateLayoutInRaf();
this.dirtyDatasets.add(name);
}

/**
* Sets the metadata of a series on the chart.
* Sets a metadata change of a series on the chart.
*
* Please call `commitChanges` for the changes to be reflected on the chart
* after making all the changes.
*/
public setSeriesMetadata(name: string, meta: any) {
const newMeta = Object.assign({}, this.getDataset(name).metadata(), {
this.disableChanges();
this.getDataset(name).metadata({
...this.getDataset(name).metadata(),
meta,
});
this.getDataset(name).metadata(newMeta);
this.dirtyDatasets.add(name);
}

public smoothingUpdate(weight: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ benchmark({
context.container.appendChild(context.chart);

context.chart.setVisibleSeries([]);
context.chart.commitChanges();

await polymerFlush();
},
Expand All @@ -82,6 +83,7 @@ benchmark({

context.chart.setSeriesData('sine', DATA_POINTS.sine1k);
context.chart.setVisibleSeries(['sine']);
context.chart.commitChanges();

await polymerFlush();
},
Expand All @@ -103,6 +105,7 @@ benchmark({

context.chart.setSeriesData('sine', DATA_POINTS.sine1k);
context.chart.setVisibleSeries(['sine']);
context.chart.commitChanges();

await polymerFlush();
},
Expand All @@ -124,6 +127,7 @@ benchmark({

context.chart.setSeriesData('cosine', DATA_POINTS.cosine100k);
context.chart.setVisibleSeries(['cosine']);
context.chart.commitChanges();

await polymerFlush();
},
Expand All @@ -146,6 +150,7 @@ benchmark({
context.chart.setSeriesData('sine', DATA_POINTS.sine1k);
context.chart.setSeriesData('cosine', DATA_POINTS.cosine1k);
context.chart.setVisibleSeries(['cosine']);
context.chart.commitChanges();
context.even = true;

await polymerFlush();
Expand Down Expand Up @@ -177,6 +182,7 @@ benchmark({
context.chart.setVisibleSeries(
FIVE_HUNDRED_1K_DATA_POINTS.map(({name}) => name)
);
context.chart.commitChanges();

await polymerFlush();
},
Expand All @@ -201,6 +207,7 @@ benchmark({
context.chart.setSeriesData(name, data);
});
context.chart.setVisibleSeries(datapoints.map(({name}) => name));
context.chart.commitChanges();

await polymerFlush();
context.index = 0;
Expand Down Expand Up @@ -234,6 +241,7 @@ benchmark({
context.container.appendChild(chart);
chart.setSeriesData(name, data);
chart.setVisibleSeries([name]);
chart.commitChanges();
return chart;
}
);
Expand Down Expand Up @@ -264,6 +272,7 @@ benchmark({
chart.style.height = '50px';
context.container.appendChild(chart);
chart.setSeriesData(name, data);
chart.commitChanges();
return chart;
}
);
Expand All @@ -277,12 +286,14 @@ benchmark({
context.charts.forEach((chart: any) => {
chart.setVisibleSeries([]);
});
context.chart.commitChanges();

await context.flushAsync();

context.charts.forEach((chart: any, index: number) => {
chart.setVisibleSeries([context.names[index]]);
});
context.chart.commitChanges();

await context.flushAsync();
},
Expand All @@ -300,6 +311,7 @@ benchmark({

context.chart.setSeriesData('cosine', DATA_POINTS.cosine1k);
context.chart.setVisibleSeries(['cosine']);
context.chart.commitChanges();
context.chart.smoothingEnabled = true;
context.even = true;

Expand Down Expand Up @@ -328,6 +340,7 @@ benchmark({

context.chart.setSeriesData('cosine', DATA_POINTS.cosine100k);
context.chart.setVisibleSeries(['cosine']);
context.chart.commitChanges();
context.chart.smoothingEnabled = true;
context.even = true;

Expand Down Expand Up @@ -358,6 +371,7 @@ benchmark({

context.chart.setSeriesData('cosine', DATA_POINTS.cosine100k);
context.chart.setVisibleSeries(['cosine']);
context.chart.commitChanges();
context.chart.smoothingEnabled = true;
context.even = true;

Expand Down
7 changes: 3 additions & 4 deletions tensorboard/components/vz_line_chart2/vz-line-chart2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,9 @@ namespace vz_line_chart2 {
}
},

/**
* Not yet implemented.
*/
commitChanges() {
// Temporarily rolled back due to PR curves breakage.
if (!this._chart) return;
this._chart.commitChanges();
},

/**
Expand Down Expand Up @@ -433,6 +431,7 @@ namespace vz_line_chart2 {
this._chart.setSeriesMetadata(name, this._seriesMetadataCache[name]);
});
this._chart.setVisibleSeries(this._visibleSeriesCache);
this._chart.commitChanges();
},

_smoothingChanged: function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@
dataSeries.getData()
);
});
this.$.loader.commitChanges();
},
_computeSeriesNames() {
const runLookup = new Set(this.runs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
Object.entries(_nameToDataSeries).forEach(([name, series]) => {
this.$.loader.setSeriesData(name, series.getData());
});
this.$.loader.commitChanges();
},
_computeSelectedRunsSet(runs) {
const mapping = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
seriesData.push({step: xData[i], scalar: yData[i]});
}
chart.setSeriesData(this._defaultSeriesName, seriesData);
chart.commitChanges();
},
});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,15 @@
for (let i = 0; i < seriesData.length; i++) {
seriesData[i] = _.mapValues(fieldsToData, (values) => values[i]);
}
this.$$('tf-line-chart-data-loader').setSeriesData(run, seriesData);
const loader = this.$$('tf-line-chart-data-loader');
loader.setSeriesData(run, seriesData);
loader.commitChanges();
},
_clearSeriesData(run) {
// Clears data for a run in the chart.
this.$$('tf-line-chart-data-loader').setSeriesData(run, []);
const loader = this.$$('tf-line-chart-data-loader');
loader.setSeriesData(run, []);
loader.commitChanges();
},
_updateRunToPrCurveEntry(runToDataOverTime, runToStepCap) {
const runToEntry = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
const name = this._getSeriesNameFromDatum(datum);
scalarChart.setSeriesMetadata(name, datum);
scalarChart.setSeriesData(name, formattedData);
scalarChart.commitChanges();
};
},
readOnly: true,
Expand Down

0 comments on commit 6274951

Please sign in to comment.