-
Notifications
You must be signed in to change notification settings - Fork 179
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
docs(protocol-designer): proposal for adding Python to step generation #17330
Open
ddcc4
wants to merge
3
commits into
edge
Choose a base branch
from
dc-pd-step-gen-doc
base: edge
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Python Step Generation | ||
|
||
We want to add Python generation to `step-generation` without severely changing the architecture and rewriting all the code. | ||
|
||
## Where does the Python go? | ||
|
||
The command creators produce a `CommandCreatorResult`. We'll augment that to include Python commands: | ||
|
||
```typescript | ||
export type CommandCreatorResult = | ||
| CommandsAndWarnings | ||
| CommandCreatorErrorResponse | ||
|
||
export interface CommandsAndWarnings { | ||
commands: CreateCommand[] | ||
warnings?: CommandCreatorWarning[] | ||
python?: string // <<<ADD | ||
} | ||
``` | ||
|
||
Here and elsewhere, we make `python` an optional field so that we don't have to rewrite all the existing code to specify it when creating objects. | ||
|
||
The new `python` field contains one or more lines of Python commands. It behaves analogously to the JSON `commands`. When reducing `CurriedCommandCreator`s together, we concatenate their JSON `commands`, so now we will also concatenate their `python` commands too: | ||
|
||
```typescript | ||
export const reduceCommandCreators = (...): CommandCreatorResult => { | ||
const result = commandCreators.reduce( | ||
(prev: CCReducerAcc, reducerFn: CurriedCommandCreator): CCReducerAcc => { | ||
const allCommands = [...prev.commands, ...next.commands] | ||
const allPython = [prev.python, next.python].join('\n') // <<<NEW | ||
return { | ||
commands: allCommands, | ||
python: allPython, // <<<NEW | ||
} | ||
}, | ||
... | ||
) | ||
} | ||
``` | ||
|
||
## Data flow | ||
|
||
The JSON commands from the `CommandCreatorResult`s get propagated to the `Timeline`, which is where we ultimately get the commands from to write out to the exported JSON file. By analogy, we'll add the Python commands to the `Timeline` as well: | ||
|
||
```typescript | ||
export interface Timeline { | ||
timeline: CommandsAndRobotState[] | ||
errors?: CommandCreatorError[] | null | ||
} | ||
|
||
export interface CommandsAndRobotState { | ||
commands: CreateCommand[] | ||
robotState: RobotState | ||
warnings?: CommandCreatorWarning[] | ||
python?: string // <<<ADD | ||
} | ||
``` | ||
|
||
## Generating JSON and Python in parallel | ||
|
||
In the easy case, one JSON command corresponds to one Python command, and we can just emit them side-by-side, like: | ||
|
||
```typescript | ||
export const aspirate: CommandCreator<...> = (args, invariantContext, prevRobotState) => { | ||
return { | ||
commands: [ { commandType: 'aspirate', params: {...} } ], | ||
python: `some_pipette.aspirate(...)`, | ||
} | ||
} | ||
``` | ||
|
||
Sometimes, we want to emit a Python command that doesn't correspond to any single JSON command. For example, the command sequence for a Mix step has something like: | ||
|
||
```typescript | ||
[ | ||
curryCommandCreator(aspirate, {...}), | ||
curryCommandCreator(dispense, {...}), | ||
] | ||
``` | ||
|
||
The Python API has a `mix()` that implements both aspirate and dispense. We can generate it by adding a `CommandCreator` that emits the Python `mix()` command with no JSON command: | ||
|
||
```typescript | ||
[ | ||
curryCommandCreator(pythonOnlyMix, {...}), | ||
curryCommandCreator(aspirate, {...}), | ||
curryCommandCreator(dispense, {...}), | ||
] | ||
|
||
const pythonOnlyMix: CommandCreator<...> = (...) => { | ||
return { | ||
commands: [], // emits no JSON | ||
python: `some_pipette.mix(...)`, | ||
} | ||
} | ||
``` | ||
|
||
When the reducer runs, it joins together all the non-empty JSON `commands` to get the final JSON output, and it'll join together all the non-empty `python` commands to get the final Python output. | ||
|
||
We need one more tool to make this work: because the Python `mix()` command replaces both the aspirate and dispense, we need to _suppress_ Python generation from aspirate and dispense. We'll do that by adding a new flag to `curryCommandCreator`, so the final sequence becomes: | ||
|
||
```typescript | ||
[ | ||
curryCommandCreator(pythonOnlyMix, {...}), | ||
curryCommandCreator(aspirate, {...}, suppressPython=true), | ||
curryCommandCreator(dispense, {...}, suppressPython=true), | ||
] | ||
``` | ||
|
||
Now this sequence works for generating both JSON and Python. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also have new command creators for commands that are sometimes used to generate json but not python that emit empty python strings - the converse of
pythonOnlyMix
, so I guessjsonOnlyAspirate
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I'm worried that that could lead to a lot of duplicate code between the existing
aspirate
andjsonOnlyAspirate
.There's another approach I considered, but I don't know if you would consider it too intrusive: We could add a
generatePython
flag to theArgs
that are passed to the CommandCreators. This feels intrusive becausegeneratePython
isn't logically part of the step parameters, but I think it would work. (AddinggeneratePython
to theArgs
is also messier because some of theArgs
classes don't share a common ancestor, so I'd have to add the flag in lots of places.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of these approaches, I sort of prefer
suppressPython
. I agree that ajsonOnlyAspirate
could lead to a lot of duplicate code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if you factored out the common parts of
jsonOnlyAspirate
andaspirate
you wouldn't have that much duplicate code