Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue experiment from existing #1159

Merged
merged 6 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@
"light": "resources/light/queue-experiment.svg"
}
},
{
"title": "%command.queueExperimentFromExisting%",
"command": "dvc.queueExperimentFromExisting",
"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 @@ -481,6 +490,10 @@
"command": "dvc.queueExperimentsFromCsv",
"when": "dvc.commands.available && dvc.project.available"
},
{
"command": "dvc.queueExperimentFromExisting",
"when": "dvc.commands.available && dvc.project.available"
},
{
"command": "dvc.queueExperiment",
"when": "dvc.commands.available && dvc.project.available"
Expand Down
1 change: 1 addition & 0 deletions extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"command.push": "Push",
"command.pushTarget": "Push",
"command.queueExperimentsFromCsv": "Queue Experiments From CSV",
"command.queueExperimentFromExisting": "Queue Experiment From Existing",
"command.queueExperiment": "Queue Experiment",
"command.removeExperiment": "Remove Experiment",
"command.removeExperimentsTableFilters": "Remove Filter(s) From Experiments Table",
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 @@ -38,6 +38,7 @@ export enum RegisteredCommands {
EXPERIMENT_SORTS_REMOVE_ALL = 'dvc.views.experimentsSortByTree.removeAllSorts',
EXPERIMENT_TOGGLE = 'dvc.views.experimentsTree.toggleStatus',
QUEUE_EXPERIMENTS_FROM_CSV = 'dvc.queueExperimentsFromCsv',
QUEUE_EXPERIMENT_FROM_EXISTING = 'dvc.queueExperimentFromExisting',
STOP_EXPERIMENT = 'dvc.stopRunningExperiment',

PLOTS_SHOW = 'dvc.showPlots',
Expand Down
8 changes: 8 additions & 0 deletions extension/src/experiments/commands/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const registerExperimentCwdCommands = (
experiments.queueExperimentsFromCsv()
)
)

internalCommands.registerExternalCommand(
RegisteredCommands.QUEUE_EXPERIMENT_FROM_EXISTING,
() =>
experiments.pauseUpdatesThenRun(() =>
experiments.queueExperimentFromExisting()
)
)
}

const registerExperimentNameCommands = (
Expand Down
18 changes: 18 additions & 0 deletions extension/src/experiments/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Event, EventEmitter, Memento } from 'vscode'
import { ExperimentsModel } from './model'
import { pickExperiments } from './model/quickPicks'
import { pickParamsToQueue } from './model/queue/quickPick'
import { pickExperimentName } from './quickPick'
import {
pickFilterToAdd,
pickFiltersToRemove
Expand Down Expand Up @@ -198,6 +200,22 @@ export class Experiments extends BaseRepository<TableData> {
}
}

public async pickParamsToQueue() {
const base = await pickExperimentName(this.experiments.getExperimentNames())

if (!base) {
return
}

const params = this.experiments.getExperimentParams(base)

if (!params) {
return
}

return pickParamsToQueue(params)
}

public getExperiments() {
return this.experiments.getExperiments()
}
Expand Down
19 changes: 19 additions & 0 deletions extension/src/experiments/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { collectExperiments } from './collect'
import { copyOriginalColors } from './colors'
import { collectColors, Colors } from './colors/collect'
import { collectFlatExperimentParams } from './queue/collect'
import { Experiment, RowData } from '../webview/contract'
import { definedAndNonEmpty, flatten } from '../../util/array'
import { ExperimentsOutput } from '../../cli/reader'
Expand Down Expand Up @@ -167,6 +168,24 @@ export class ExperimentsModel {
}))
}

public getExperimentNames() {
return [
'workspace',
...this.flattenExperiments().map(experiment => experiment.displayName)
].filter(Boolean) as string[]
}
Comment on lines +171 to +176
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consolidate this and pickExperimentName from extension/src/experiments/workspace.ts? As far as I can tell, they do the same thing but this is more efficient since it uses our internal model over running exp list.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This includes queued experiments. The other does not. I will take a look though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogermparent we use AvailableCommands.EXPERIMENT_LIST_CURRENT for applying and branching experiments. We can switch to using code that pulls the names out of the model. I will follow up.


public getExperimentParams(displayName: string) {
const params =
displayName === 'workspace'
? this.workspace.params
: this.flattenExperiments().find(
experiment => experiment.displayName === displayName
)?.params

return collectFlatExperimentParams(params)
}

public getSelectable() {
return this.getExperiments().filter(
exp => exp.displayName !== 'workspace' && !exp.queued
Expand Down
41 changes: 41 additions & 0 deletions extension/src/experiments/model/queue/collect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { collectFlatExperimentParams } from './collect'
import rowsFixture from '../../../test/fixtures/expShow/rows'
import { joinParamOrMetricFilePath } from '../../paramsAndMetrics/paths'
import { join } from '../../../test/util/path'

describe('collectFlatExperimentParams', () => {
it('should flatten the params into an array', () => {
const params = collectFlatExperimentParams(rowsFixture[0].params)
expect(params).toEqual([
{ path: joinParamOrMetricFilePath('params.yaml', 'epochs'), value: 2 },
{
path: joinParamOrMetricFilePath('params.yaml', 'learning_rate'),
value: 2.2e-7
},
{
path: joinParamOrMetricFilePath('params.yaml', 'dvc_logs_dir'),
value: 'dvc_logs'
},
{
path: joinParamOrMetricFilePath('params.yaml', 'log_file'),
value: 'logs.csv'
},
{
path: joinParamOrMetricFilePath('params.yaml', 'dropout'),
value: 0.122
},
{
path: joinParamOrMetricFilePath('params.yaml', 'process.threshold'),
value: 0.86
},
{
path: joinParamOrMetricFilePath('params.yaml', 'process.test_arg'),
value: 'string'
},
{
path: joinParamOrMetricFilePath(join('nested', 'params.yaml'), 'test'),
value: true
}
])
})
})
36 changes: 36 additions & 0 deletions extension/src/experiments/model/queue/collect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Value, ValueTree } from '../../../cli/reader'
import { joinParamOrMetricFilePath } from '../../paramsAndMetrics/paths'
import { ParamsOrMetrics } from '../../webview/contract'

export type Param = {
path: string
value: number | string | boolean
}

const collectFromParamsFile = (
acc: { path: string; value: string | number | boolean }[],
key: string | undefined,
value: Value | ValueTree,
ancestors: string[] = []
) => {
const pathArray = [...ancestors, key].filter(Boolean) as string[]

if (typeof value === 'object') {
Object.entries(value as ValueTree).forEach(([childKey, childValue]) => {
return collectFromParamsFile(acc, childKey, childValue, pathArray)
})
return
}

const path = joinParamOrMetricFilePath(...pathArray)

acc.push({ path, value })
}

export const collectFlatExperimentParams = (params: ParamsOrMetrics = {}) => {
const acc: { path: string; value: string | number | boolean }[] = []
Object.keys(params).forEach(file =>
collectFromParamsFile(acc, undefined, params[file], [file])
)
return acc
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flag } from '../cli/args'
import { readCsv } from '../fileSystem'
import { definedAndNonEmpty } from '../util/array'
import { Flag } from '../../../cli/args'
import { readCsv } from '../../../fileSystem'
import { definedAndNonEmpty } from '../../../util/array'

const collectParamsToVary = (csvRow: Record<string, unknown>): string[] =>
Object.entries(csvRow).reduce((acc, [k, v]) => {
Expand Down
75 changes: 75 additions & 0 deletions extension/src/experiments/model/queue/quickPick.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { mocked } from 'ts-jest/utils'
import { pickParamsToQueue } from './quickPick'
import { getInput } from '../../../vscode/inputBox'
import { quickPickManyValues } from '../../../vscode/quickPick'

jest.mock('../../../vscode/inputBox')
jest.mock('../../../vscode/quickPick')

const mockedGetInput = mocked(getInput)
const mockedQuickPickManyValues = mocked(quickPickManyValues)

beforeEach(() => {
jest.resetAllMocks()
})

describe('pickParamsToQueue', () => {
it('should return early if no params are selected', async () => {
mockedQuickPickManyValues.mockResolvedValueOnce(undefined)

const paramsToQueue = await pickParamsToQueue([
{ path: 'params.yaml:learning_rate', value: 2e-12 }
])

expect(paramsToQueue).toBeUndefined()
expect(mockedGetInput).not.toBeCalled()
})

it('should return early if the user exits from the input box', async () => {
const unchanged = { path: 'params.yaml:learning_rate', value: 2e-12 }
const initialUserResponse = [
{ path: 'params.yaml:dropout', value: 0.15 },
{ path: 'params.yaml:process.threshold', value: 0.86 }
]
mockedQuickPickManyValues.mockResolvedValueOnce(initialUserResponse)
const firstInput = '0.16'
mockedGetInput.mockResolvedValueOnce(firstInput)
mockedGetInput.mockResolvedValueOnce(undefined)

const paramsToQueue = await pickParamsToQueue([
unchanged,
...initialUserResponse
])

expect(paramsToQueue).toBeUndefined()
expect(mockedGetInput).toBeCalledTimes(2)
})

it('should convert any selected params into the required format', async () => {
const unchanged = { path: 'params.yaml:learning_rate', value: 2e-12 }
const initialUserResponse = [
{ path: 'params.yaml:dropout', value: 0.15 },
{ path: 'params.yaml:process.threshold', value: 0.86 }
]
mockedQuickPickManyValues.mockResolvedValueOnce(initialUserResponse)
const firstInput = '0.16'
const secondInput = '0.87'
mockedGetInput.mockResolvedValueOnce(firstInput)
mockedGetInput.mockResolvedValueOnce(secondInput)

const paramsToQueue = await pickParamsToQueue([
unchanged,
...initialUserResponse
])

expect(paramsToQueue).toEqual([
'-S',
`params.yaml:dropout=${firstInput}`,
'-S',
`params.yaml:process.threshold=${secondInput}`,
'-S',
[unchanged.path, unchanged.value].join('=')
])
expect(mockedGetInput).toBeCalledTimes(2)
})
})
63 changes: 63 additions & 0 deletions extension/src/experiments/model/queue/quickPick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Param } from './collect'
import { quickPickManyValues } from '../../../vscode/quickPick'
import { getInput } from '../../../vscode/inputBox'
import { Flag } from '../../../cli/args'
import { definedAndNonEmpty } from '../../../util/array'

const pickParamsToVary = (params: Param[]): Thenable<Param[] | undefined> =>
quickPickManyValues<Param>(
params.map(param => ({
description: `${param.value}`,
label: param.path,
picked: true,
value: param
})),
{ title: 'Select a param to vary' }
)

const pickNewParamValues = async (
paramsToVary: Param[]
): Promise<string[] | undefined> => {
const args: string[] = []
for (const { path, value } of paramsToVary) {
const input = await getInput(`Enter a value for ${path}`, `${value}`)
if (input === undefined) {
return
}
args.push(Flag.SET_PARAM)
args.push([path, input.trim()].join('='))
}
return args
}

const addUnchanged = (args: string[], unchanged: Param[]) => {
unchanged.forEach(({ path, value }) => {
args.push(Flag.SET_PARAM)
args.push([path, value].join('='))
})

return args
}

export const pickParamsToQueue = async (
params: Param[]
): Promise<string[] | undefined> => {
const paramsToVary = await pickParamsToVary(params)

if (!definedAndNonEmpty(paramsToVary)) {
return
}

const args = await pickNewParamValues(paramsToVary)

if (!args) {
return
}

const paramPathsToVary = paramsToVary.map(param => param.path)
const unchanged = params.filter(
param => !paramPathsToVary.includes(param.path)
)

return addUnchanged(args, unchanged)
}
2 changes: 1 addition & 1 deletion extension/src/experiments/quickPick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { quickPickManyValues, quickPickOne } from '../vscode/quickPick'
import { reportError } from '../vscode/reporting'

export const pickExperimentName = async (
experimentNamesPromise: Promise<string[]>
experimentNamesPromise: Promise<string[]> | string[]
): Promise<string | undefined> => {
const experimentNames = await experimentNamesPromise
if (experimentNames.length === 0) {
Expand Down
Loading