Skip to content

Commit

Permalink
add experimental queue experiments from csv command
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Dec 3, 2021
1 parent 91fa1a4 commit 832c39c
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 4 deletions.
14 changes: 14 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@
"category": "DVC",
"icon": "$(cloud-upload)"
},
{
"title": "%command.queueExperimentsFromCsv%",
"command": "dvc.queueExperimentsFromCsv",
"category": "DVC",
"icon": {
"dark": "resources/dark/queue-experiment.svg",
"light": "resources/light/queue-experiment.svg"
}
},
{
"title": "%command.queueExperiment%",
"command": "dvc.queueExperiment",
Expand Down Expand Up @@ -462,6 +471,10 @@
"command": "dvc.pushTarget",
"when": "false"
},
{
"command": "dvc.queueExperimentsFromCsv",
"when": "dvc.commands.available && dvc.project.available"
},
{
"command": "dvc.queueExperiment",
"when": "dvc.commands.available && dvc.project.available"
Expand Down Expand Up @@ -1042,6 +1055,7 @@
"dependencies": {
"@hediet/std": "^0.6.0",
"chokidar": "^3.5.2",
"csv-parse": "^5.0.3",
"execa": "^5.1.1",
"fs-extra": "^10.0.0",
"lodash.get": "^4.4.2",
Expand Down
1 change: 1 addition & 0 deletions extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"command.pullTarget": "Pull",
"command.push": "Push",
"command.pushTarget": "Push",
"command.queueExperimentsFromCsv": "Queue Experiments From CSV",
"command.queueExperiment": "Queue Experiment",
"command.removeExperiment": "Remove Experiment",
"command.removeExperimentsTableFilters": "Remove Filter(s) From Experiments Table",
Expand Down
5 changes: 3 additions & 2 deletions extension/src/cli/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ export class CliExecutor extends Cli {
)
}

public experimentRunQueue(cwd: string) {
public experimentRunQueue(cwd: string, ...args: Args) {
return this.executeExperimentProcess(
cwd,
ExperimentSubCommand.RUN,
ExperimentFlag.QUEUE
ExperimentFlag.QUEUE,
...args
)
}

Expand Down
1 change: 1 addition & 0 deletions extension/src/commands/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export enum RegisteredCommands {
EXPERIMENT_SORTS_REMOVE = 'dvc.removeExperimentsTableSorts',
EXPERIMENT_SORTS_REMOVE_ALL = 'dvc.views.experimentsSortByTree.removeAllSorts',
EXPERIMENT_TOGGLE = 'dvc.views.experimentsTree.toggleStatus',
QUEUE_EXPERIMENTS_FROM_CSV = 'dvc.queueExperimentsFromCsv',
STOP_EXPERIMENT = 'dvc.stopRunningExperiment',

PLOTS_SHOW = 'dvc.showPlots',
Expand Down
8 changes: 7 additions & 1 deletion extension/src/experiments/commands/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import {
const registerExperimentCwdCommands = (
experiments: WorkspaceExperiments,
internalCommands: InternalCommands
): void =>
): void => {
internalCommands.registerExternalCliCommand(
RegisteredCliCommands.QUEUE_EXPERIMENT,
() => experiments.getCwdThenReport(AvailableCommands.EXPERIMENT_QUEUE)
)

internalCommands.registerExternalCommand(
RegisteredCommands.QUEUE_EXPERIMENTS_FROM_CSV,
() => experiments.queueExperimentsFromCsv()
)
}

const registerExperimentNameCommands = (
experiments: WorkspaceExperiments,
internalCommands: InternalCommands
Expand Down
42 changes: 42 additions & 0 deletions extension/src/experiments/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { exists, readCsv } from '../fileSystem'
import { join } from '../test/util/path'
import { definedAndNonEmpty } from '../util/array'
import { delay } from '../util/time'

const reduceRow = (row: Record<string, unknown>) =>
Object.entries(row).reduce((acc, [k, v]) => {
const key = k.trim()
const value = (v as string).trim()

if (key !== '' && value !== '') {
acc.push('-S')
const str = [key, value].join('=')
acc.push(str)
}

return acc
}, [] as string[])

export const readToQueueFromCsv = (path: string): Promise<string[][]> =>
new Promise(resolve => {
const toQueue: string[][] = []
readCsv(path)
.on('data', row => {
const arr = reduceRow(row)
if (definedAndNonEmpty(arr)) {
toQueue.push(arr)
}
})
.on('end', () => {
resolve(toQueue)
})
})

export const waitForLock = async (cwd: string): Promise<void> => {
const lock = join(cwd, '.dvc', 'rwlock')

if (exists(lock)) {
await delay(2000)
return waitForLock(cwd)
}
}
25 changes: 25 additions & 0 deletions extension/src/experiments/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter, Memento } from 'vscode'
import { Experiments } from '.'
import { readToQueueFromCsv, waitForLock } from './queue'
import { pickExperimentName } from './quickPick'
import { TableData } from './webview/contract'
import {
Expand All @@ -12,6 +13,7 @@ import { reportOutput } from '../vscode/reporting'
import { getInput } from '../vscode/inputBox'
import { BaseWorkspaceWebviews } from '../webview/workspace'
import { WorkspacePlots } from '../plots/workspace'
import { pickCsv } from '../vscode/resourcePicker'

export class WorkspaceExperiments extends BaseWorkspaceWebviews<
Experiments,
Expand Down Expand Up @@ -100,6 +102,29 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
return this.getRepository(dvcRoot).autoApplyFilters(enable)
}

public async queueExperimentsFromCsv() {
const cwd = await this.getFocusedOrOnlyOrPickProject()
if (!cwd) {
return
}

const csv = await pickCsv('Select a CSV to queue experiments from')
if (!csv) {
return
}

const toQueue = await readToQueueFromCsv(csv)

for (const arr of toQueue) {
await waitForLock(cwd)
await this.internalCommands.executeCommand(
AvailableCommands.EXPERIMENT_QUEUE,
cwd,
...arr
)
}
}

public async getCwdThenRun(commandId: CommandId) {
const cwd = await this.getFocusedOrOnlyOrPickProject()
if (!cwd) {
Expand Down
6 changes: 5 additions & 1 deletion extension/src/fileSystem/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { basename, extname, join, relative, resolve } from 'path'
import { existsSync, lstatSync, readdir } from 'fs-extra'
import { createReadStream, existsSync, lstatSync, readdir } from 'fs-extra'
import { Uri } from 'vscode'
import { parse, Parser } from 'csv-parse'
import { definedAndNonEmpty } from '../util/array'

export const exists = (path: string): boolean => existsSync(path)
Expand Down Expand Up @@ -64,3 +65,6 @@ export const isAnyDvcYaml = (path?: string): boolean =>

export const relativeWithUri = (dvcRoot: string, uri: Uri) =>
relative(dvcRoot, uri.fsPath)

export const readCsv = (path: string): Parser =>
createReadStream(path).pipe(parse({ columns: true, delimiter: ',' }))
1 change: 1 addition & 0 deletions extension/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface IEventNamePropertyMapping {
[EventName.EXPERIMENT_SORTS_REMOVE_ALL]: undefined
[EventName.EXPERIMENT_TOGGLE]: undefined
[EventName.QUEUE_EXPERIMENT]: undefined
[EventName.QUEUE_EXPERIMENTS_FROM_CSV]: undefined
[EventName.STOP_EXPERIMENT]: { stopped: boolean; wasRunning: boolean }

[EventName.PLOTS_SHOW]: undefined
Expand Down
15 changes: 15 additions & 0 deletions extension/src/vscode/resourcePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ export const pickFile = async (title: string): Promise<string | undefined> => {
}
}

export const pickCsv = async (title: string): Promise<string | undefined> => {
const uris = await window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
filters: { CSV: ['csv'] },
title
})

if (uris) {
const [{ fsPath }] = uris
return fsPath
}
}

export const pickResources = (title: string): Thenable<Uri[] | undefined> => {
return window.showOpenDialog({
canSelectFiles: true,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6087,6 +6087,11 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.9.tgz#6410af31b26bd0520933d02cbc64fce9ce3fbf0b"
integrity sha512-rpw6JPxK6Rfg1zLOYCSwle2GFOOsnjmDYDaBwEcwoOg4qlsIVCN789VkBZDJAGi4T07gI4YSutR43t9Zz4Lzuw==

csv-parse@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-5.0.3.tgz#eeb0b1ac8bf2970f703176c8f54d313325b6d2e8"
integrity sha512-86R0WU4aEEF/1fPZKxP3NmDAYC4Ce1t9iFgKPZogG5Lvk4m9WZQkCEsDANktG29jppejwclTtEOzubN2ieCJqw==

cyclist@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
Expand Down

0 comments on commit 832c39c

Please sign in to comment.