Skip to content

Commit 7babb2d

Browse files
authored
Stabilize plot paths (#2811)
* do not drop previously collected plot paths * do not display plots without datapoints
1 parent 54922dd commit 7babb2d

File tree

4 files changed

+93
-34
lines changed

4 files changed

+93
-34
lines changed

extension/src/plots/model/collect.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,44 @@ const fillTemplate = (
594594
) as TopLevelSpec
595595
}
596596

597+
const collectTemplatePlot = (
598+
acc: TemplatePlotEntry[],
599+
selectedRevisions: string[],
600+
path: string,
601+
template: string,
602+
revisionData: RevisionData,
603+
size: number,
604+
revisionColors: ColorScale | undefined,
605+
multiSourceEncoding: MultiSourceEncoding
606+
) => {
607+
const isMultiView = isMultiViewPlot(JSON.parse(template))
608+
const multiSourceEncodingUpdate = multiSourceEncoding[path] || {}
609+
const { datapoints, revisions } = transformRevisionData(
610+
path,
611+
selectedRevisions,
612+
revisionData,
613+
isMultiView,
614+
multiSourceEncodingUpdate
615+
)
616+
617+
if (datapoints.length === 0) {
618+
return
619+
}
620+
621+
const content = extendVegaSpec(fillTemplate(template, datapoints), size, {
622+
...multiSourceEncodingUpdate,
623+
color: revisionColors
624+
})
625+
626+
acc.push({
627+
content,
628+
id: path,
629+
multiView: isMultiViewPlot(content),
630+
revisions,
631+
type: PlotsType.VEGA
632+
})
633+
}
634+
597635
const collectTemplateGroup = (
598636
paths: string[],
599637
selectedRevisions: string[],
@@ -607,30 +645,20 @@ const collectTemplateGroup = (
607645
for (const path of paths) {
608646
const template = templates[path]
609647

610-
if (template) {
611-
const isMultiView = isMultiViewPlot(JSON.parse(template))
612-
const multiSourceEncodingUpdate = multiSourceEncoding[path] || {}
613-
const { datapoints, revisions } = transformRevisionData(
614-
path,
615-
selectedRevisions,
616-
revisionData,
617-
isMultiView,
618-
multiSourceEncodingUpdate
619-
)
620-
621-
const content = extendVegaSpec(fillTemplate(template, datapoints), size, {
622-
...multiSourceEncodingUpdate,
623-
color: revisionColors
624-
})
625-
626-
acc.push({
627-
content,
628-
id: path,
629-
multiView: isMultiViewPlot(content),
630-
revisions,
631-
type: PlotsType.VEGA
632-
})
648+
if (!template) {
649+
continue
633650
}
651+
652+
collectTemplatePlot(
653+
acc,
654+
selectedRevisions,
655+
path,
656+
template,
657+
revisionData,
658+
size,
659+
revisionColors,
660+
multiSourceEncoding
661+
)
634662
}
635663
return acc
636664
}

extension/src/plots/paths/collect.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { join } from 'path'
22
import { VisualizationSpec } from 'react-vega'
3+
import isEqual from 'lodash.isequal'
34
import {
45
collectEncodingElements,
56
collectPaths,
67
collectTemplateOrder,
7-
EncodingType
8+
EncodingType,
9+
PathType
810
} from './collect'
911
import { TemplatePlotGroup, PlotsType } from '../webview/contract'
1012
import plotsDiffFixture from '../../test/fixtures/plotsDiff/output'
1113
import { Shape, StrokeDash } from '../multiSource/constants'
1214

1315
describe('collectPath', () => {
1416
it('should return the expected data from the test fixture', () => {
15-
expect(collectPaths(plotsDiffFixture)).toStrictEqual([
17+
expect(collectPaths([], plotsDiffFixture)).toStrictEqual([
1618
{
1719
hasChildren: false,
1820
label: 'acc.png',
@@ -65,6 +67,21 @@ describe('collectPath', () => {
6567
])
6668
})
6769

70+
it('should not drop already collected paths', () => {
71+
const mockPath = 'completely:madeup:path'
72+
const mockPlotPath = {
73+
hasChildren: false,
74+
label: mockPath,
75+
parentPath: undefined,
76+
path: mockPath,
77+
type: new Set([PathType.TEMPLATE_SINGLE])
78+
}
79+
80+
const paths = collectPaths([mockPlotPath], plotsDiffFixture)
81+
82+
expect(paths.some(plotPath => isEqual(plotPath, mockPlotPath))).toBeTruthy()
83+
})
84+
6885
it('should handle more complex paths', () => {
6986
const mockPlotsDiff = {
7087
[join('logs', 'scalars', 'acc.tsv')]: [
@@ -99,7 +116,7 @@ describe('collectPath', () => {
99116
]
100117
}
101118

102-
expect(collectPaths(mockPlotsDiff)).toStrictEqual([
119+
expect(collectPaths([], mockPlotsDiff)).toStrictEqual([
103120
{
104121
hasChildren: false,
105122
label: 'acc.tsv',

extension/src/plots/paths/collect.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ export type PlotPath = {
2626
hasChildren: boolean
2727
}
2828

29+
type PathAccumulator = {
30+
plotPaths: PlotPath[]
31+
included: Set<string>
32+
}
33+
34+
const createPathAccumulator = (existingPaths: PlotPath[]): PathAccumulator => {
35+
const acc: PathAccumulator = { included: new Set<string>(), plotPaths: [] }
36+
37+
for (const existing of existingPaths) {
38+
acc.included.add(existing.path)
39+
acc.plotPaths.push(existing)
40+
}
41+
42+
return acc
43+
}
44+
2945
const collectType = (plots: Plot[]) => {
3046
const type = new Set<PathType>()
3147

@@ -60,11 +76,6 @@ const getType = (
6076
return collectType(plots)
6177
}
6278

63-
type PathAccumulator = {
64-
plotPaths: PlotPath[]
65-
included: Set<string>
66-
}
67-
6879
const collectPath = (
6980
acc: PathAccumulator,
7081
data: PlotsOutput,
@@ -95,8 +106,11 @@ const collectPath = (
95106
acc.included.add(path)
96107
}
97108

98-
export const collectPaths = (data: PlotsOutput): PlotPath[] => {
99-
const acc: PathAccumulator = { included: new Set<string>(), plotPaths: [] }
109+
export const collectPaths = (
110+
existingPaths: PlotPath[],
111+
data: PlotsOutput
112+
): PlotPath[] => {
113+
const acc = createPathAccumulator(existingPaths)
100114

101115
const paths = Object.keys(data)
102116

extension/src/plots/paths/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class PathsModel extends PathSelectionModel<PlotPath> {
2727
}
2828

2929
public transformAndSet(data: PlotsOutput) {
30-
const paths = collectPaths(data)
30+
const paths = collectPaths(this.data, data)
3131

3232
this.setNewStatuses(paths)
3333

0 commit comments

Comments
 (0)