Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
from allotropy.parsers.utils.pandas import map_rows, SeriesData
from allotropy.parsers.utils.uuids import random_uuid_str

# Maps source units to (target_unit, conversion_multiplier)
UNIT_CONVERSIONS: dict[str, tuple[str, float]] = {
Comment thread
joshua-benchling marked this conversation as resolved.
"mg/L": ("g/L", 0.001),
"mM": ("mmol/L", 1.0),
}


@dataclass(frozen=True)
class Title:
Expand Down Expand Up @@ -111,15 +117,18 @@ def create(data: SeriesData) -> RawMeasurement:

@staticmethod
def _get_value_and_unit(value: JsonFloat, unit: str) -> tuple[JsonFloat, str]:
multiplier = 1.0
if unit == "mg/L":
unit = "g/L"
multiplier = 1 / 1000

if isinstance(value, int | float):
value = value * multiplier

return value, unit
normalized_unit = unit.strip()

# Check if unit needs conversion
if normalized_unit in UNIT_CONVERSIONS:
target_unit, multiplier = UNIT_CONVERSIONS[normalized_unit]
if isinstance(value, int | float):
converted_value: JsonFloat = value * multiplier
else:
converted_value = value
return converted_value, target_unit

return value, normalized_unit


def create_measurements(data: pd.DataFrame) -> dict[str, dict[str, RawMeasurement]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
40 2023-09-15 16:55:51 SMPL1 SAM GLN2B mmol/L 2.45 0.17138 R
40 2023-09-15 16:55:53 SMPL1 SAM ODB OD 6.32 1.05394 R
40 2023-09-15 16:56:18 SMPL1 SAM LDH2B U/L 88.09 0.00728 R
40 2023-09-15 16:56:26 SMPL1 SAM NH3B mmol/L 1.846 0.05333 R
40 2023-09-15 16:56:26 SMPL1 SAM NH3B mM 1.846 0.05333 R
40 2023-09-15 16:56:37 SMPL1 SAM LAC2B g/L 0.02 0.01567 R
40 2023-09-15 16:56:48 SMPL1 SAM TP2LB g/L 4.6 0.14883 R
40 2023-09-15 16:56:58 SMPL2 SAM GLN2B mmol/L 2.40 0.16787 R
Expand All @@ -20,7 +20,7 @@
40 2023-09-15 16:59:16 SMPL3 SAM LAC2B g/L < TEST RNG < 0.00 0.00329 R
40 2023-09-15 16:59:38 SMPL3 SAM TP2D g/L < TEST RNG < 40.0 0.02702 R
40 2023-09-15 17:00:52 SMPL3 SAM TP2LB g/L 4.8 0.15436 R
40 2023-09-16 10:12:10 SMPL4 SAM GLN2B mmol/L 2.07 0.14503 R
40 2023-09-16 10:12:10 SMPL4 SAM GLN2B mM 2.07 0.14503 R
40 2023-09-16 10:12:12 SMPL4 SAM ODB OD 4.09 0.68160 R
40 2023-09-16 10:13:29 SMPL4 SAM LDH2B U/L 334.84 0.02665 R
40 2023-09-16 10:13:37 SMPL4 SAM NH3B mmol/L 3.788 0.11415 R
Expand Down
Loading