Skip to content

Commit f035144

Browse files
authored
Improve Plot Wizard Error Handling (#4770)
1 parent fa9b67b commit f035144

File tree

4 files changed

+246
-141
lines changed

4 files changed

+246
-141
lines changed

extension/src/fileSystem/index.test.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe('loadDataFiles', () => {
182182
for (const file of dataFiles) {
183183
const resultWithErr = await loadDataFiles([file])
184184

185-
expect(resultWithErr).toStrictEqual(undefined)
185+
expect(resultWithErr).toStrictEqual([{ data: undefined, file }])
186186
}
187187
})
188188

@@ -201,7 +201,23 @@ describe('loadDataFiles', () => {
201201
.mockReturnValueOnce(mockJsonContent)
202202

203203
const resultWithErr = await loadDataFiles(dataFiles)
204-
expect(resultWithErr).toStrictEqual(undefined)
204+
expect(resultWithErr).toStrictEqual([
205+
{
206+
data: [
207+
{ acc: 0.69, epoch: 10 },
208+
{ acc: 0.345, epoch: 11 }
209+
],
210+
file: 'values.csv'
211+
},
212+
{ data: undefined, file: 'file.tsv' },
213+
{
214+
data: [
215+
{ acc: 0.69, epoch: 10 },
216+
{ acc: 0.345, epoch: 11 }
217+
],
218+
file: 'file.json'
219+
}
220+
])
205221
})
206222
})
207223

@@ -608,8 +624,8 @@ describe('addPlotToDvcYamlFile', () => {
608624

609625
addPlotToDvcYamlFile('/', {
610626
template: 'simple',
611-
x: { file: '/data.json', key: 'epochs' },
612-
y: { file: '/data.json', key: 'accuracy' }
627+
x: { file: 'data.json', key: 'epochs' },
628+
y: { file: 'data.json', key: 'accuracy' }
613629
})
614630

615631
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -636,8 +652,8 @@ describe('addPlotToDvcYamlFile', () => {
636652

637653
addPlotToDvcYamlFile('/', {
638654
template: 'simple',
639-
x: { file: '/data.json', key: 'epochs' },
640-
y: { file: '/acc.json', key: 'accuracy' }
655+
x: { file: 'data.json', key: 'epochs' },
656+
y: { file: 'acc.json', key: 'accuracy' }
641657
})
642658

643659
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -654,8 +670,8 @@ describe('addPlotToDvcYamlFile', () => {
654670

655671
addPlotToDvcYamlFile('/', {
656672
template: 'simple',
657-
x: { file: '/data.json', key: 'epochs' },
658-
y: { file: '/data.json', key: 'accuracy' }
673+
x: { file: 'data.json', key: 'epochs' },
674+
y: { file: 'data.json', key: 'accuracy' }
659675
})
660676

661677
mockDvcYamlContent.splice(7, 0, ...mockPlotYamlContent)
@@ -676,8 +692,8 @@ describe('addPlotToDvcYamlFile', () => {
676692

677693
addPlotToDvcYamlFile('/', {
678694
template: 'simple',
679-
x: { file: '/data.json', key: 'epochs' },
680-
y: { file: '/data.json', key: 'accuracy' }
695+
x: { file: 'data.json', key: 'epochs' },
696+
y: { file: 'data.json', key: 'accuracy' }
681697
})
682698

683699
expect(mockedWriteFileSync).toHaveBeenCalledWith(
@@ -713,8 +729,8 @@ describe('addPlotToDvcYamlFile', () => {
713729

714730
addPlotToDvcYamlFile('/', {
715731
template: 'simple',
716-
x: { file: '/data.json', key: 'epochs' },
717-
y: { file: '/data.json', key: 'accuracy' }
732+
x: { file: 'data.json', key: 'epochs' },
733+
y: { file: 'data.json', key: 'accuracy' }
718734
})
719735

720736
expect(mockedWriteFileSync).toHaveBeenCalledWith(

extension/src/fileSystem/index.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -214,31 +214,27 @@ const loadYamlAsDoc = (
214214
}
215215
}
216216

217-
const getPlotYamlObj = (cwd: string, plot: PlotConfigData) => {
217+
const getPlotYamlObj = (plot: PlotConfigData) => {
218218
const { x, y, template } = plot
219219
const plotName = `${template}_plot`
220220
return {
221221
[plotName]: {
222222
template,
223-
x: x.file === y.file ? x.key : { [relative(cwd, x.file)]: x.key },
224-
y: { [relative(cwd, y.file)]: y.key }
223+
x: x.file === y.file ? x.key : { [x.file]: x.key },
224+
y: { [y.file]: y.key }
225225
}
226226
}
227227
}
228228

229-
const getPlotsYaml = (
230-
cwd: string,
231-
plotObj: PlotConfigData,
232-
indentSearchLines: string[]
233-
) => {
229+
const getPlotsYaml = (plotObj: PlotConfigData, indentSearchLines: string[]) => {
234230
const indentReg = /^( +)[^ ]/
235231
const indentLine = indentSearchLines.find(line => indentReg.test(line)) || ''
236232
const spacesMatches = indentLine.match(indentReg)
237233
const spaces = spacesMatches?.[1]
238234

239235
return yaml
240236
.stringify(
241-
{ plots: [getPlotYamlObj(cwd, plotObj)] },
237+
{ plots: [getPlotYamlObj(plotObj)] },
242238
{ indent: spaces ? spaces.length : 2 }
243239
)
244240
.split('\n')
@@ -258,7 +254,7 @@ export const addPlotToDvcYamlFile = (cwd: string, plotObj: PlotConfigData) => {
258254
const plots = doc.get('plots', true) as yaml.YAMLSeq | undefined
259255

260256
if (!plots?.range) {
261-
const plotYaml = getPlotsYaml(cwd, plotObj, dvcYamlLines)
257+
const plotYaml = getPlotsYaml(plotObj, dvcYamlLines)
262258
dvcYamlLines.push(...plotYaml)
263259
writeFileSync(dvcYamlFile, dvcYamlLines.join('\n'))
264260
return
@@ -272,7 +268,6 @@ export const addPlotToDvcYamlFile = (cwd: string, plotObj: PlotConfigData) => {
272268

273269
const plotsStartPos = lineCounter.linePos(plots.range[0]).line - 1
274270
const plotYaml = getPlotsYaml(
275-
cwd,
276271
plotObj,
277272
dvcYamlLines.slice(plotsStartPos, insertLineNum)
278273
)
@@ -342,15 +337,10 @@ const loadDataFile = (file: string): unknown => {
342337

343338
export const loadDataFiles = async (
344339
files: string[]
345-
): Promise<{ file: string; data: unknown }[] | undefined> => {
340+
): Promise<{ file: string; data: unknown }[]> => {
346341
const filesData: { file: string; data: unknown }[] = []
347342
for (const file of files) {
348343
const data = await loadDataFile(file)
349-
350-
if (!data) {
351-
return undefined
352-
}
353-
354344
filesData.push({ data, file })
355345
}
356346
return filesData

0 commit comments

Comments
 (0)