-
Notifications
You must be signed in to change notification settings - Fork 186
feat(protocol-designer): introduce byVolume curve builder prototype #19114
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
Draft
ncdiehl11
wants to merge
12
commits into
edge
Choose a base branch
from
pd_curvebuilder
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.
+2,635
−41
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
924df0e
save
ncdiehl11 8471134
remove sycnfusion
ncdiehl11 62c89ba
fix dependencies
ncdiehl11 17706c6
refine organization of byVolume builder, add calculator and add point…
ncdiehl11 c2fe98f
comment
ncdiehl11 4077f8d
fix tests
ncdiehl11 9a32680
Merge branch 'edge' into pd_curvebuilder
ncdiehl11 7d42584
update yarn.lock
ncdiehl11 c21f9c7
refine logic for byVolume axes
ncdiehl11 b69443d
split axis ranges and shape x- and y-radii
ncdiehl11 8949787
Merge branch 'edge' into pd_curvebuilder
ncdiehl11 801cecc
refactor shape size function and scalars
ncdiehl11 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
protocol-designer/src/assets/localization/en/by_volume_builder.json
This file contains hidden or 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,26 @@ | ||
{ | ||
"add_point": "Add point", | ||
"calculator": { | ||
"calculate": "Calculate", | ||
"interpolated_value": "Interpolated {{type}}", | ||
"volume_to_calculate": "Input volume" | ||
}, | ||
"flowRate": { | ||
"axes": { | ||
"x": { | ||
"label": "Volume ({{units}})", | ||
"units": "µl" | ||
}, | ||
"y": { | ||
"label": "Flow Rate ({{units}})", | ||
"units": "µl/s" | ||
} | ||
}, | ||
"title": "Flow Rate Builder" | ||
}, | ||
"instructions": "Drag the points to adjust the curve.", | ||
"reset": "Reset curve", | ||
"type": { | ||
"flowRate": "flow rate" | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
.../pages/Designer/ProtocolSteps/StepForm/StepTools/ByVolumeBuilderModal/ByVolumeBuilder.tsx
This file contains hidden or 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,137 @@ | ||
import { useTranslation } from 'react-i18next' | ||
import Plot from 'react-plotly.js' | ||
|
||
import { | ||
AXIS_OFFSET_PERCENTAGE, | ||
BASE_DATA, | ||
BASE_LAYOUT, | ||
CONFIG, | ||
} from './constants' | ||
import { getAnnotations, getShapes } from './utils' | ||
|
||
import type { LiquidHandlingPropertyByVolume } from '@opentrons/shared-data' | ||
import type { ByVolumeType, DataPoint } from './types' | ||
|
||
export function ByVolumeBuilder(props: { | ||
type: ByVolumeType | ||
dataPoints: DataPoint[] | ||
setDataPoints: (dataPoints: DataPoint[]) => void | ||
byVolume: LiquidHandlingPropertyByVolume | ||
maxX: number | ||
maxY: number | ||
}): JSX.Element { | ||
const { type, dataPoints, setDataPoints, maxX, maxY } = props | ||
|
||
const { t } = useTranslation(['by_volume_builder']) | ||
|
||
const handleRelayout = (eventData: any): void => { | ||
const updatedPoints = [...dataPoints] | ||
let changed = false | ||
|
||
// Handle shape-based editing (when shapes are moved) | ||
for (let i = 0; i < updatedPoints.length; i++) { | ||
const shapeX0Key = `shapes[${i}].x0` | ||
const shapeY0Key = `shapes[${i}].y0` | ||
const shapeX1Key = `shapes[${i}].x1` | ||
const shapeY1Key = `shapes[${i}].y1` | ||
|
||
if ( | ||
eventData[shapeX0Key] !== undefined && | ||
eventData[shapeY0Key] !== undefined && | ||
eventData[shapeX1Key] !== undefined && | ||
eventData[shapeY1Key] !== undefined | ||
) { | ||
// Calculate center point from shape bounds | ||
const newX = (eventData[shapeX0Key] + eventData[shapeX1Key]) / 2 | ||
const newY = (eventData[shapeY0Key] + eventData[shapeY1Key]) / 2 | ||
|
||
if (updatedPoints[i].x !== newX || updatedPoints[i].y !== newY) { | ||
updatedPoints[i] = { | ||
...updatedPoints[i], | ||
x: Math.min(Math.max(newX, 0), maxX), | ||
y: Math.min(Math.max(newY, 0), maxY), | ||
} | ||
changed = true | ||
} | ||
} | ||
} | ||
|
||
// Handle data-based editing (when data points are moved directly) | ||
const xEventData = eventData['data[0].x'] | ||
const yEventData = eventData['data[0].y'] | ||
if (xEventData != null && yEventData != null) { | ||
const newXValues = xEventData as number[] | ||
const newYValues = yEventData as number[] | ||
|
||
for (let i = 0; i < updatedPoints.length; i++) { | ||
const newX = newXValues[i] | ||
const newY = newYValues[i] | ||
|
||
if (updatedPoints[i].x !== newX || updatedPoints[i].y !== newY) { | ||
updatedPoints[i] = { | ||
...updatedPoints[i], | ||
x: Math.max(newX, 0), | ||
y: Math.max(newY, 0), | ||
} | ||
changed = true | ||
} | ||
} | ||
} | ||
|
||
if (changed) { | ||
const sortedPoints = updatedPoints.sort((a, b) => a.x - b.x) | ||
setDataPoints(sortedPoints) | ||
} | ||
} | ||
const axisOffsetX = maxX * AXIS_OFFSET_PERCENTAGE | ||
const axisOffsetY = maxY * AXIS_OFFSET_PERCENTAGE | ||
const axisRangeX = maxX + 2 * axisOffsetX | ||
const axisRangeY = maxY + 2 * axisOffsetY | ||
return ( | ||
<div> | ||
<Plot | ||
data={[ | ||
{ | ||
...BASE_DATA, | ||
// ensure the curve starts at 0 and ends at maxVolume | ||
x: [0, ...dataPoints.map(p => p.x), maxX], | ||
y: [ | ||
dataPoints[0].y, | ||
...dataPoints.map(p => p.y), | ||
dataPoints[dataPoints.length - 1].y, | ||
], | ||
}, | ||
]} | ||
layout={{ | ||
...BASE_LAYOUT, | ||
title: { | ||
text: t(`by_volume_builder:instructions`), | ||
xanchor: 'right', | ||
}, | ||
xaxis: { | ||
title: { | ||
text: t(`by_volume_builder:${type}.axes.x.label`, { | ||
units: t(`by_volume_builder:${type}.axes.x.units`), | ||
}), | ||
editable: false, | ||
}, | ||
range: [-1 * axisOffsetX, maxX + axisOffsetX], | ||
}, | ||
yaxis: { | ||
title: { | ||
text: t(`by_volume_builder:${type}.axes.y.label`, { | ||
units: t(`by_volume_builder:${type}.axes.y.units`), | ||
}), | ||
editable: false, | ||
}, | ||
range: [-1 * axisOffsetY, maxY + axisOffsetY], | ||
}, | ||
shapes: getShapes(dataPoints, axisRangeX, axisRangeY), | ||
annotations: getAnnotations(dataPoints), | ||
}} | ||
config={CONFIG} | ||
onRelayout={handleRelayout} | ||
/> | ||
</div> | ||
) | ||
} |
109 changes: 109 additions & 0 deletions
109
...s/Designer/ProtocolSteps/StepForm/StepTools/ByVolumeBuilderModal/ByVolumeBuilderModal.tsx
This file contains hidden or 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,109 @@ | ||
import { useState } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { | ||
DIRECTION_COLUMN, | ||
Flex, | ||
FLEX_MIN_CONTENT, | ||
JUSTIFY_FLEX_END, | ||
Modal, | ||
PrimaryButton, | ||
SecondaryButton, | ||
SPACING, | ||
} from '@opentrons/components' | ||
import { linearInterpolate } from '@opentrons/shared-data' | ||
|
||
import { getMainPagePortalEl } from '../../../../../../components/organisms' | ||
import { ByVolumeBuilder } from './ByVolumeBuilder' | ||
import { ByVolumeCalculator } from './ByVolumeCalculator' | ||
import { getByVolumeMappedToXY } from './utils' | ||
|
||
import type { Dispatch, SetStateAction } from 'react' | ||
import type { LiquidHandlingPropertyByVolume } from '@opentrons/shared-data' | ||
import type { ByVolumeType } from './types' | ||
|
||
export function ByVolumeBuilderModal(props: { | ||
byVolume: LiquidHandlingPropertyByVolume | ||
setByVolume: Dispatch<SetStateAction<LiquidHandlingPropertyByVolume>> | ||
type: ByVolumeType | ||
onClose: () => void | ||
defaultFlowRates: LiquidHandlingPropertyByVolume | ||
maxX: number | ||
maxY: number | ||
}): JSX.Element { | ||
const { | ||
byVolume = [], | ||
type, | ||
onClose, | ||
setByVolume, | ||
defaultFlowRates, | ||
maxX, | ||
maxY, | ||
} = props | ||
|
||
const { t } = useTranslation(['shared', 'by_volume_builder']) | ||
|
||
const defaultDataPoints = getByVolumeMappedToXY(byVolume) | ||
const [dataPoints, setDataPoints] = useState(defaultDataPoints) | ||
|
||
// adds a new point to the center x value at the interpolated y value | ||
const handleAddPoint = (): void => { | ||
const newXValue = maxX / 2 | ||
const newYValue = linearInterpolate( | ||
newXValue, | ||
dataPoints.map(p => [p.x, p.y]) | ||
) | ||
if (newYValue != null) { | ||
const newPoints = [...dataPoints, { x: newXValue, y: newYValue }] | ||
const newPointsSorted = newPoints.sort((a, b) => a.x - b.x) | ||
setDataPoints(newPointsSorted) | ||
} | ||
} | ||
|
||
return createPortal( | ||
<Modal | ||
title={t(`by_volume_builder:${type}.title`)} | ||
onClose={onClose} | ||
closeOnOutsideClick | ||
width={FLEX_MIN_CONTENT} | ||
> | ||
<Flex flexDirection={DIRECTION_COLUMN} gridGap={SPACING.spacing8}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry for this nit but since this is a new feature, we should be using css modules! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. totally right! thank you |
||
<ByVolumeBuilder | ||
type={type} | ||
dataPoints={dataPoints} | ||
setDataPoints={setDataPoints} | ||
byVolume={byVolume} | ||
maxX={maxX} | ||
maxY={maxY} | ||
/> | ||
<ByVolumeCalculator | ||
type={type} | ||
points={dataPoints.map(p => [p.x, p.y])} | ||
/> | ||
<Flex justifyContent={JUSTIFY_FLEX_END} gridGap={SPACING.spacing4}> | ||
<SecondaryButton | ||
onClick={() => { | ||
setDataPoints(getByVolumeMappedToXY(defaultFlowRates)) | ||
}} | ||
> | ||
{t(`by_volume_builder:reset`)} | ||
</SecondaryButton> | ||
|
||
<PrimaryButton onClick={handleAddPoint}> | ||
{t('by_volume_builder:add_point')} | ||
</PrimaryButton> | ||
<PrimaryButton | ||
onClick={() => { | ||
setByVolume(dataPoints.map(p => [p.x, p.y])) | ||
onClose() | ||
}} | ||
> | ||
{t('shared:save')} | ||
</PrimaryButton> | ||
</Flex> | ||
</Flex> | ||
</Modal>, | ||
getMainPagePortalEl() | ||
) | ||
} |
Oops, something went wrong.
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.