Skip to content

Commit 0c1439d

Browse files
committed
refactor to avoid using a dict in creating a variable to have type validation
1 parent 554f045 commit 0c1439d

2 files changed

Lines changed: 52 additions & 49 deletions

File tree

src/allotropy/parsers/unchained_labs_lunatic_stunner/unchained_labs_lunatic_stunner_structure.py

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,47 @@ def _create_measurement(
181181
error_documents.extend(calculated_data_errors)
182182
error_documents.extend(absorbance_errors)
183183

184-
measurement_dict = {
185-
"type_": MeasurementType.ULTRAVIOLET_ABSORBANCE_CUBE_SPECTRUM
184+
# Initialize optional variables to None; set if applicable
185+
spectrum_data_cube: DataCube | None = None
186+
absorbance_value: float | None = None
187+
detector_wavelength_setting: float | None = None
188+
wavelength_identifier: str | None = None
189+
190+
if len(wavelength_columns) > 1:
191+
spectrum_data_cube = _get_spectrum_data_cube(
192+
well_plate_data, wavelength_columns
193+
)
194+
elif len(wavelength_columns) == 1:
195+
wavelength_match = WAVELENGTH_COLUMNS_RE.match(wavelength_columns[0])
196+
wavelength, _ = wavelength_match.groups() if wavelength_match else ("", "")
197+
absorbance_tmp = well_plate_data.get(float, wavelength_columns[0])
198+
absorbance_value = (
199+
absorbance_tmp if absorbance_tmp is not None else NEGATIVE_ZERO
200+
)
201+
detector_wavelength_setting = float(wavelength) if wavelength else None
202+
wavelength_identifier = wavelength_columns[0]
203+
204+
return Measurement(
205+
type_=MeasurementType.ULTRAVIOLET_ABSORBANCE_CUBE_SPECTRUM
186206
if len(wavelength_columns) > 1
187207
else MeasurementType.ULTRAVIOLET_ABSORBANCE,
188-
"device_type": DEVICE_TYPE,
189-
"detection_type": detection_type,
190-
"identifier": measurement_identifier,
191-
"analytical_method_identifier": analytical_method_identifier,
192-
"experimental_data_identifier": experimental_data_identifier,
193-
"electronic_absorbance_reference_wavelength_setting": background_wavelength,
194-
"sample_identifier": well_plate_data[str, "sample name"],
195-
"location_identifier": well_plate_data[str, "plate position"],
196-
"well_plate_identifier": well_plate_data.get(str, "plate id"),
197-
"batch_identifier": well_plate_data.get(str, "sample group"),
198-
"firmware_version": header.get(str, "client version"),
199-
"number_of_averages": well_plate_data.get(float, "number of acquisitions"),
200-
"integration_time": well_plate_data.get(float, "acquisition time (s)"),
201-
"compartment_temperature": well_plate_data.get(float, "temperature (°c)"),
202-
"sample_custom_info": {
208+
device_type=DEVICE_TYPE,
209+
detection_type=detection_type,
210+
identifier=measurement_identifier,
211+
analytical_method_identifier=analytical_method_identifier,
212+
experimental_data_identifier=experimental_data_identifier,
213+
detector_wavelength_setting=detector_wavelength_setting,
214+
electronic_absorbance_reference_wavelength_setting=background_wavelength,
215+
absorbance=absorbance_value,
216+
sample_identifier=well_plate_data[str, "sample name"],
217+
location_identifier=well_plate_data[str, "plate position"],
218+
well_plate_identifier=well_plate_data.get(str, "plate id"),
219+
batch_identifier=well_plate_data.get(str, "sample group"),
220+
firmware_version=header.get(str, "client version"),
221+
number_of_averages=well_plate_data.get(float, "number of acquisitions"),
222+
integration_time=well_plate_data.get(float, "acquisition time (s)"),
223+
compartment_temperature=well_plate_data.get(float, "temperature (°c)"),
224+
sample_custom_info={
203225
"plate type": header.get(str, "plate type")
204226
or well_plate_data.get(str, "plate type"),
205227
"nr of plates": header.get(str, "nr of plates"),
@@ -212,7 +234,7 @@ def _create_measurement(
212234
float, "molecular weight (kda)"
213235
),
214236
},
215-
"device_control_custom_info": {
237+
device_control_custom_info={
216238
"path length mode": well_plate_data.get(str, "path length mode"),
217239
"pump": well_plate_data.get(str, "pump"),
218240
"column": header.get(str, "column") or well_plate_data.get(str, "column"),
@@ -221,22 +243,22 @@ def _create_measurement(
221243
),
222244
"Acquisition filtering": well_plate_data.get(str, "acquisition filtering"),
223245
},
224-
"error_document": error_documents,
225-
"processed_data_document": ProcessedDataDocument(
246+
error_document=error_documents,
247+
processed_data_document=ProcessedDataDocument(
226248
identifier=random_uuid_str(),
227249
concentration_factor=concentration_factor,
228250
peak_list_custom_info=peak_data,
229251
)
230252
if concentration_factor is not None or peak_data
231253
else None,
232-
"calc_docs_custom_info": {
254+
calc_docs_custom_info={
233255
**calculated_data_values,
234256
**{
235257
"b22 linear fit": well_plate_data.get(str, "b22 linear fit"),
236258
"kd linear fit": well_plate_data.get(str, "kd linear fit"),
237259
},
238260
},
239-
"measurement_custom_info": {
261+
measurement_custom_info={
240262
"electronic_absorbance_reference_absorbance": background_absorbance,
241263
**_filter_empty_string_values(
242264
well_plate_data.get_unread(
@@ -264,28 +286,9 @@ def _create_measurement(
264286
)
265287
),
266288
},
267-
}
268-
269-
extra_variables: dict[str, Any] = {}
270-
if len(wavelength_columns) > 1:
271-
spectrum_data_cube = _get_spectrum_data_cube(
272-
well_plate_data, wavelength_columns
273-
)
274-
extra_variables["spectrum_data_cube"] = spectrum_data_cube
275-
elif len(wavelength_columns) == 1:
276-
wavelength_match = WAVELENGTH_COLUMNS_RE.match(wavelength_columns[0])
277-
278-
wavelength, _ = wavelength_match.groups() if wavelength_match else ("", "")
279-
absorbance = well_plate_data.get(float, wavelength_columns[0])
280-
extra_variables["absorbance"] = (
281-
absorbance if absorbance is not None else NEGATIVE_ZERO
282-
)
283-
extra_variables["detector_wavelength_setting"] = float(wavelength)
284-
extra_variables["wavelength_identifier"] = wavelength_columns[0]
285-
286-
measurement_dict.update(extra_variables)
287-
288-
return Measurement(**measurement_dict) # type: ignore
289+
spectrum_data_cube=spectrum_data_cube,
290+
wavelength_identifier=wavelength_identifier,
291+
)
289292

290293

291294
def _get_spectrum_data_cube(
@@ -304,7 +307,7 @@ def _get_spectrum_data_cube(
304307
wavelengths.append(float(wavelength_str))
305308

306309
return DataCube(
307-
label="absorvance-spectrum",
310+
label="absorbance-spectrum",
308311
structure_dimensions=[
309312
DataCubeComponent(
310313
concept="wavelength",

tests/parsers/unchained_labs_lunatic_stunner/testdata/spectrum_measurement.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
"analytical method identifier": "dsDNA (Single point)",
3333
"absorption spectrum data cube": {
34-
"label": "absorvance-spectrum",
34+
"label": "absorbance-spectrum",
3535
"cube-structure": {
3636
"dimensions": [
3737
{
@@ -101,7 +101,7 @@
101101
},
102102
"analytical method identifier": "dsDNA (Single point)",
103103
"absorption spectrum data cube": {
104-
"label": "absorvance-spectrum",
104+
"label": "absorbance-spectrum",
105105
"cube-structure": {
106106
"dimensions": [
107107
{
@@ -178,7 +178,7 @@
178178
]
179179
},
180180
"absorption spectrum data cube": {
181-
"label": "absorvance-spectrum",
181+
"label": "absorbance-spectrum",
182182
"cube-structure": {
183183
"dimensions": [
184184
{
@@ -369,7 +369,7 @@
369369
"UNC path": "tests/parsers/unchained_labs_lunatic_stunner/testdata/spectrum_measurement.csv",
370370
"file name": "spectrum_measurement.csv",
371371
"ASM converter name": "allotropy_unchained_labs_lunatic_&_stunner",
372-
"ASM converter version": "0.1.103",
372+
"ASM converter version": "0.1.105",
373373
"software name": "Lunatic and Stunner Analysis"
374374
},
375375
"device system document": {

0 commit comments

Comments
 (0)