-
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
Changes from 7 commits
7b46ac0
ad3687e
e9bdbff
55c2db5
cbc0382
840e9fb
09fdb0c
27b4c88
a564912
68e1176
0482e96
b020705
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1035,6 +1035,9 @@ def initialize( | |
) | ||
|
||
@requires_version(2, 21) | ||
def read(self) -> Optional[Dict[int, Dict[str, float]]]: | ||
"""Initiate read on the Absorbance Reader. Returns a dictionary of wavelengths to dictionary of values ordered by well name.""" | ||
return self._core.read() | ||
def read(self, filename: Optional[str]) -> Dict[int, Dict[str, float]]: | ||
"""Initiate read on the Absorbance Reader. Returns a dictionary of wavelengths to dictionary of values ordered by well name. | ||
|
||
:param filename: Optional, if a filename is provided a CSV file will be saved as a result of the read action containing measurement data. | ||
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. Does this mean that if
My understanding was that we'd always write a file. 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. No file will be written. There is a limit (currently) of 40 files written during a protocol run. The user can do unlimited reads so long as they are not writing a file. They can do 40 reads where they save a file. |
||
""" | ||
return self._core.read(filename=filename) |
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( | ||
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. nice |
||
..., | ||
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, | ||
) | ||
|
||
|
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 would consider naming the
filename
parameter something likesave_read_results_to_filename
.Right now,
module.read(filename="foo")
really makes it look like you're reading from an existing file called"foo"
.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.
would something shorter like
export_filename
orexport_to_filename
work (preference to the first for briefness, but I think clarity is important).