-
Notifications
You must be signed in to change notification settings - Fork 178
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
feat(api, robot-server): Plate Reader CSV output functionality #16495
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7b46ac0
initial csv file provider infastructure
CaseyBatten ad3687e
csv file writing engine to robot server pipeline and PAPI update for …
CaseyBatten e9bdbff
schema 10 update for plate reader
CaseyBatten 55c2db5
file provider asynchronous fix and directory writing
CaseyBatten cbc0382
test fixture linting fixes and doc strings
CaseyBatten 840e9fb
addition of file output data to run result and tracking of files crea…
CaseyBatten 09fdb0c
ensure file count persists in analysis use of constructors for class …
CaseyBatten 27b4c88
write csv return file id
CaseyBatten a564912
Merge branch 'edge' into plate_reader_csv_engine_file_provider
CaseyBatten 68e1176
test fixtures corrections and file parameter updates
CaseyBatten 0482e96
generic csv build with rules
CaseyBatten b020705
lint
CaseyBatten 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
"""Command models to read absorbance.""" | ||
from __future__ import annotations | ||
from typing import Optional, Dict, TYPE_CHECKING | ||
from datetime import datetime | ||
from typing import Optional, Dict, TYPE_CHECKING, List | ||
from typing_extensions import Literal, Type | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from ..command import AbstractCommandImpl, BaseCommand, BaseCommandCreate, SuccessData | ||
from ...errors import CannotPerformModuleAction | ||
from ...errors import CannotPerformModuleAction, StorageLimitReachedError | ||
from ...errors.error_occurrence import ErrorOccurrence | ||
|
||
from ...resources.file_provider import ( | ||
PlateReaderData, | ||
ReadData, | ||
MAXIMUM_CSV_FILE_LIMIT, | ||
) | ||
from ...resources import FileProvider | ||
|
||
if TYPE_CHECKING: | ||
from opentrons.protocol_engine.state.state import StateView | ||
from opentrons.protocol_engine.execution import EquipmentHandler | ||
|
@@ -21,6 +29,10 @@ class ReadAbsorbanceParams(BaseModel): | |
"""Input parameters for an absorbance reading.""" | ||
|
||
moduleId: str = Field(..., description="Unique ID of the Absorbance Reader.") | ||
fileName: Optional[str] = Field( | ||
None, | ||
description="Optional file name to use when storing the results of a measurement.", | ||
) | ||
|
||
|
||
class ReadAbsorbanceResult(BaseModel): | ||
|
@@ -29,6 +41,10 @@ class ReadAbsorbanceResult(BaseModel): | |
data: Optional[Dict[int, Dict[str, float]]] = Field( | ||
..., description="Absorbance data points per wavelength." | ||
) | ||
fileIds: Optional[List[str]] = Field( | ||
..., | ||
description="List of file IDs for files output as a result of a Read action.", | ||
SyntaxColoring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
class ReadAbsorbanceImpl( | ||
|
@@ -40,18 +56,21 @@ def __init__( | |
self, | ||
state_view: StateView, | ||
equipment: EquipmentHandler, | ||
file_provider: FileProvider, | ||
**unused_dependencies: object, | ||
) -> None: | ||
self._state_view = state_view | ||
self._equipment = equipment | ||
self._file_provider = file_provider | ||
|
||
async def execute( | ||
async def execute( # noqa: C901 | ||
self, params: ReadAbsorbanceParams | ||
) -> SuccessData[ReadAbsorbanceResult, None]: | ||
"""Initiate an absorbance measurement.""" | ||
abs_reader_substate = self._state_view.modules.get_absorbance_reader_substate( | ||
module_id=params.moduleId | ||
) | ||
|
||
# Allow propagation of ModuleNotAttachedError. | ||
abs_reader = self._equipment.get_module_hardware_api( | ||
abs_reader_substate.module_id | ||
|
@@ -62,10 +81,29 @@ async def execute( | |
"Cannot perform Read action on Absorbance Reader without calling `.initialize(...)` first." | ||
) | ||
|
||
# TODO: we need to return a file ID and increase the file count even when a moduel is not attached | ||
if ( | ||
params.fileName is not None | ||
and abs_reader_substate.configured_wavelengths is not None | ||
): | ||
# Validate that the amount of files we are about to generate does not put us higher than the limit | ||
if ( | ||
self._state_view.files.get_filecount() | ||
+ len(abs_reader_substate.configured_wavelengths) | ||
> MAXIMUM_CSV_FILE_LIMIT | ||
): | ||
raise StorageLimitReachedError( | ||
message=f"Attempt to write file {params.fileName} exceeds file creation limit of {MAXIMUM_CSV_FILE_LIMIT} files." | ||
) | ||
|
||
asbsorbance_result: Dict[int, Dict[str, float]] = {} | ||
transform_results = [] | ||
# Handle the measurement and begin building data for return | ||
if abs_reader is not None: | ||
start_time = datetime.now() | ||
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. You probably want to use
|
||
results = await abs_reader.start_measure() | ||
finish_time = datetime.now() | ||
if abs_reader._measurement_config is not None: | ||
asbsorbance_result: Dict[int, Dict[str, float]] = {} | ||
sample_wavelengths = abs_reader._measurement_config.sample_wavelengths | ||
for wavelength, result in zip(sample_wavelengths, results): | ||
converted_values = ( | ||
|
@@ -74,13 +112,67 @@ async def execute( | |
) | ||
) | ||
asbsorbance_result[wavelength] = converted_values | ||
transform_results.append( | ||
ReadData.construct(wavelength=wavelength, data=converted_values) | ||
) | ||
# Handle the virtual module case for data creation (all zeroes) | ||
elif self._state_view.config.use_virtual_modules: | ||
start_time = finish_time = datetime.now() | ||
if abs_reader_substate.configured_wavelengths is not None: | ||
for wavelength in abs_reader_substate.configured_wavelengths: | ||
converted_values = ( | ||
self._state_view.modules.convert_absorbance_reader_data_points( | ||
data=[0] * 96 | ||
) | ||
) | ||
asbsorbance_result[wavelength] = converted_values | ||
transform_results.append( | ||
ReadData.construct(wavelength=wavelength, data=converted_values) | ||
) | ||
else: | ||
raise CannotPerformModuleAction( | ||
"Plate Reader data cannot be requested with a module that has not been initialized." | ||
) | ||
|
||
# TODO (cb, 10-17-2024): FILE PROVIDER - Some day we may want to break the file provider behavior into a seperate API function. | ||
# When this happens, we probably will to have the change the command results handler we utilize to track file IDs in engine. | ||
# Today, the action handler for the FileStore looks for a ReadAbsorbanceResult command action, this will need to be delinked. | ||
|
||
# Begin interfacing with the file provider if the user provided a filename | ||
file_ids = [] | ||
if params.fileName is not None: | ||
# Create the Plate Reader Transform | ||
plate_read_result = PlateReaderData.construct( | ||
read_results=transform_results, | ||
reference_wavelength=abs_reader_substate.reference_wavelength, | ||
start_time=start_time, | ||
finish_time=finish_time, | ||
serial_number=abs_reader.serial_number | ||
if (abs_reader is not None and abs_reader.serial_number is not None) | ||
else "VIRTUAL_SERIAL", | ||
) | ||
|
||
if isinstance(plate_read_result, PlateReaderData): | ||
# Write a CSV file for each of the measurements taken | ||
for measurement in plate_read_result.read_results: | ||
file_id = await self._file_provider.write_csv( | ||
write_data=plate_read_result.build_generic_csv( | ||
filename=params.fileName, | ||
measurement=measurement, | ||
) | ||
) | ||
file_ids.append(file_id) | ||
|
||
# Return success data to api | ||
return SuccessData( | ||
public=ReadAbsorbanceResult(data=asbsorbance_result), | ||
public=ReadAbsorbanceResult( | ||
data=asbsorbance_result, fileIds=file_ids | ||
), | ||
private=None, | ||
) | ||
|
||
return SuccessData( | ||
public=ReadAbsorbanceResult(data=None), | ||
public=ReadAbsorbanceResult(data=asbsorbance_result, fileIds=file_ids), | ||
private=None, | ||
) | ||
|
||
|
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.
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.
nice