Skip to content

Commit d21c635

Browse files
authored
Add Multiple X Field Selection to Plot Wizard (#4797)
1 parent b907879 commit d21c635

File tree

8 files changed

+564
-130
lines changed

8 files changed

+564
-130
lines changed

extension/src/fileSystem/index.test.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ describe('addPlotToDvcYamlFile', () => {
625625
addPlotToDvcYamlFile('/', {
626626
template: 'simple',
627627
title: 'Simple Plot',
628-
x: { 'data.json': 'epochs' },
629-
y: { 'data.json': 'accuracy' }
628+
x: { 'data.json': ['epochs'] },
629+
y: { 'data.json': ['accuracy'] }
630630
})
631631

632632
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -654,8 +654,38 @@ describe('addPlotToDvcYamlFile', () => {
654654
addPlotToDvcYamlFile('/', {
655655
template: 'simple',
656656
title: 'simple_plot',
657-
x: { 'data.json': 'epochs' },
658-
y: { 'acc.json': 'accuracy' }
657+
x: { 'data.json': ['epochs'] },
658+
y: { 'acc.json': ['accuracy'] }
659+
})
660+
661+
expect(mockedWriteFileSync).toHaveBeenCalledWith(
662+
'//dvc.yaml',
663+
mockDvcYamlContent + mockPlotYamlContent
664+
)
665+
})
666+
667+
it('should add the new plot with an axis having multiple fields', () => {
668+
const mockDvcYamlContent = mockStagesLines.join('\n')
669+
const mockPlotYamlContent = [
670+
'',
671+
'plots:',
672+
' - simple_plot:',
673+
' template: simple',
674+
' x: epochs',
675+
' y:',
676+
' data.json:',
677+
' - accuracy',
678+
' - epochs',
679+
''
680+
].join('\n')
681+
mockedReadFileSync.mockReturnValueOnce(mockDvcYamlContent)
682+
mockedReadFileSync.mockReturnValueOnce(mockDvcYamlContent)
683+
684+
addPlotToDvcYamlFile('/', {
685+
template: 'simple',
686+
title: 'simple_plot',
687+
x: { 'data.json': ['epochs'] },
688+
y: { 'data.json': ['accuracy', 'epochs'] }
659689
})
660690

661691
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -673,8 +703,8 @@ describe('addPlotToDvcYamlFile', () => {
673703
addPlotToDvcYamlFile('/', {
674704
template: 'simple',
675705
title: 'Simple Plot',
676-
x: { 'data.json': 'epochs' },
677-
y: { 'data.json': 'accuracy' }
706+
x: { 'data.json': ['epochs'] },
707+
y: { 'data.json': ['accuracy'] }
678708
})
679709

680710
mockDvcYamlContent.splice(7, 0, ...mockPlotYamlContent)
@@ -696,8 +726,8 @@ describe('addPlotToDvcYamlFile', () => {
696726
addPlotToDvcYamlFile('/', {
697727
template: 'simple',
698728
title: 'Simple Plot',
699-
x: { 'data.json': 'epochs' },
700-
y: { 'data.json': 'accuracy' }
729+
x: { 'data.json': ['epochs'] },
730+
y: { 'data.json': ['accuracy'] }
701731
})
702732

703733
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -734,8 +764,8 @@ describe('addPlotToDvcYamlFile', () => {
734764
addPlotToDvcYamlFile('/', {
735765
template: 'simple',
736766
title: 'simple_plot',
737-
x: { 'data.json': 'epochs' },
738-
y: { 'data.json': 'accuracy' }
767+
x: { 'data.json': ['epochs'] },
768+
y: { 'data.json': ['accuracy'] }
739769
})
740770

741771
expect(mockedWriteFileSync).toHaveBeenCalledWith(

extension/src/fileSystem/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { processExists } from '../process/execution'
3030
import { getFirstWorkspaceFolder } from '../vscode/workspaceFolders'
3131
import { DOT_DVC } from '../cli/dvc/constants'
3232
import { delay } from '../util/time'
33-
import { PlotConfigData } from '../pipeline/quickPick'
33+
import { PlotConfigData, PlotConfigDataAxis } from '../pipeline/quickPick'
3434

3535
export const exists = (path: string): boolean => existsSync(path)
3636

@@ -214,18 +214,35 @@ const loadYamlAsDoc = (
214214
}
215215
}
216216

217+
const formatPlotYamlObjAxis = (axis: PlotConfigDataAxis) => {
218+
const formattedAxis: { [file: string]: string | string[] } = {}
219+
220+
for (const [file, fields] of Object.entries(axis)) {
221+
if (fields.length === 1) {
222+
formattedAxis[file] = fields[0]
223+
continue
224+
}
225+
226+
formattedAxis[file] = fields
227+
}
228+
229+
return formattedAxis
230+
}
231+
217232
const getPlotYamlObj = (plot: PlotConfigData) => {
218233
const { x, y, template, title } = plot
219234

220235
const yFiles = Object.keys(y)
221-
const [xFile, xKey] = Object.entries(x)[0]
222-
const oneFileUsed = yFiles.length === 1 && yFiles[0] === xFile
236+
const xFiles = Object.keys(x)
237+
const firstXFile = xFiles[0]
238+
const oneFileUsed =
239+
yFiles.length === 1 && xFiles.length === 1 && yFiles[0] === firstXFile
223240

224241
return {
225242
[title]: {
226243
template,
227-
x: oneFileUsed ? xKey : x,
228-
y
244+
x: oneFileUsed ? x[firstXFile][0] : formatPlotYamlObjAxis(x),
245+
y: formatPlotYamlObjAxis(y)
229246
}
230247
}
231248
}

0 commit comments

Comments
 (0)