Skip to content

Commit

Permalink
refactor: ThermoFisher Qubit Flex - refactor parser to use schema map…
Browse files Browse the repository at this point in the history
…per design pattern (#699)
  • Loading branch information
nathan-stender authored Oct 7, 2024
1 parent efb557b commit 08f1fbd
Show file tree
Hide file tree
Showing 9 changed files with 1,077 additions and 1,362 deletions.
28 changes: 21 additions & 7 deletions src/allotropy/allotrope/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,32 @@ def add_custom_information_document(
if not custom_info_doc:
return model

if isinstance(custom_info_doc, dict):
custom_info_doc = structure_custom_information_document(
custom_info_doc, "custom information document"
)
if not is_dataclass(custom_info_doc):
# Convert to a dictionary first, so we can clean up values.
if is_dataclass(custom_info_doc):
custom_info_dict = asdict(custom_info_doc)
elif isinstance(custom_info_doc, dict):
custom_info_dict = custom_info_doc
else:
msg = f"Invalid custom_info_doc: {custom_info_doc}"
raise ValueError(msg)

# Do not add custom info doc if all values are None
if all(value is None for value in asdict(custom_info_doc).values()):
# Remove None and {"value": None, "unit"...} values
cleaned_dict = {}
for key, value in custom_info_dict.items():
if value is None:
continue
if isinstance(value, dict) and "value" in value and value["value"] is None:
continue
cleaned_dict[key] = value

# If dict is empty after cleaning, do not attach.
if not cleaned_dict:
return model

custom_info_doc = structure_custom_information_document(
cleaned_dict, "custom information document"
)

model.custom_information_document = custom_info_doc # type: ignore
return model

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,16 @@ def _get_sample_document(self, measurement: Measurement) -> SampleDocument:
measurement.original_sample_concentration,
)
}
# TODO: this is a temporary work around until we implement the new REC custom info doc logic, at which
# time we will detect and convert timestamps generically.
if (
measurement.sample_custom_info
and "last read standards" in measurement.sample_custom_info
):
measurement.sample_custom_info["last read standards"] = self.get_date_time(
measurement.sample_custom_info["last read standards"]
)

return add_custom_information_document(
SampleDocument(
sample_identifier=measurement.sample_identifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Reader file for ThermoFisher Qubit 4 Adapter"""

import numpy as np
import pandas as pd

from allotropy.constants import DEFAULT_ENCODING
Expand Down Expand Up @@ -35,18 +36,17 @@ def read(cls, named_file_contents: NamedFileContents) -> pd.DataFrame:
AllotropeConversionError: If the file format is not supported.
"""
if named_file_contents.extension == "xlsx":
dataframe = read_excel(named_file_contents.contents)
df = read_excel(named_file_contents.contents)
else:
dataframe = read_csv(
named_file_contents.contents,
index_col=False,
encoding=DEFAULT_ENCODING,
df = read_csv(
named_file_contents.contents, index_col=False, encoding=DEFAULT_ENCODING
)

columns = dataframe.columns.tolist()
columns = df.columns.tolist()
new_columns = [
f"Units_{columns[i - 1]}" if "Units" in col else col
for i, col in enumerate(columns)
]
set_columns(dataframe, new_columns)
return dataframe
set_columns(df, new_columns)
df = df.replace(np.nan, None)
return df
Loading

0 comments on commit 08f1fbd

Please sign in to comment.