diff --git a/src/allotropy/parsers/roche_cedex_bioht/roche_cedex_bioht_structure.py b/src/allotropy/parsers/roche_cedex_bioht/roche_cedex_bioht_structure.py index 55384d96e..d1a9a6cbc 100644 --- a/src/allotropy/parsers/roche_cedex_bioht/roche_cedex_bioht_structure.py +++ b/src/allotropy/parsers/roche_cedex_bioht/roche_cedex_bioht_structure.py @@ -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]] = { + "mg/L": ("g/L", 0.001), + "mM": ("mmol/L", 1.0), +} + @dataclass(frozen=True) class Title: @@ -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]]: diff --git a/tests/parsers/roche_cedex_bioht/testdata/roche_cedex_bioht_example01.txt b/tests/parsers/roche_cedex_bioht/testdata/roche_cedex_bioht_example01.txt index 431a33ef8..aa5d09fd5 100644 --- a/tests/parsers/roche_cedex_bioht/testdata/roche_cedex_bioht_example01.txt +++ b/tests/parsers/roche_cedex_bioht/testdata/roche_cedex_bioht_example01.txt @@ -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 @@ -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