Skip to content

Commit d1fa04e

Browse files
committed
fix unit ids not retrieved corectly
1 parent a0282d4 commit d1fa04e

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

pyenzyme/sbml/serializer.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from typing import Callable, List
66

7+
import rich
78
import libsbml
89
import pandas as pd
910
from loguru import logger
@@ -18,6 +19,7 @@
1819
from pyenzyme.sbml.versions import v2
1920
from pyenzyme.tabular import to_pandas
2021
from pyenzyme.units.units import UnitDefinition
22+
from pyenzyme.tools import to_dict_wo_json_ld
2123

2224
MAPPINGS = tools.read_static_file("pyenzyme.sbml", "mappings.toml")
2325
NSMAP = {"enzymeml": "https://www.enzymeml.org/v2"}
@@ -438,20 +440,33 @@ def _get_sbml_kind(unit_type: pe.UnitType):
438440
raise ValueError(f"Unit type {unit_type} not found in libsbml")
439441

440442

441-
def _get_unit_id(unit: pe.UnitDefinition) -> str | None:
443+
def _get_unit_id(unit: pe.UnitDefinition | None) -> str | None:
442444
"""Helper function to get the unit from the list of units."""
443445

444446
if unit is None:
445447
return None
446448

447-
if unit.id is None:
448-
raise ValueError(f"Unit {unit.name} does not have an ID")
449+
for unit2 in units:
450+
if _same_unit(unit, unit2):
451+
return unit2.id
449452

450-
return unit.id
451453

454+
raise ValueError(f"Unit {unit.name} not found in the list of units")
455+
456+
def _same_unit(unit1: pe.UnitDefinition, unit2: pe.UnitDefinition) -> bool:
457+
"""Check if two units are the same."""
458+
459+
unit1 = to_dict_wo_json_ld(unit1)
460+
unit2 = to_dict_wo_json_ld(unit2)
461+
462+
del unit1["id"]
463+
del unit2["id"]
464+
465+
return unit1 == unit2
452466

453467
def _validate_sbml(sbmldoc: libsbml.SBMLDocument) -> None:
454468
"""Validate the SBML document using the libSBML function."""
469+
455470
sbml_errors = sbmldoc.checkConsistency()
456471
valid = True
457472

pyenzyme/sbml/versions/v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class MeasurementAnnot(
179179

180180
id: str = attr(name="id")
181181
name: str | None = attr(name="name", default=None)
182-
time_unit: str = attr(name="timeUnit")
182+
time_unit: str | None = attr(name="timeUnit")
183183
conditions: ConditionsAnnot | None = element(tag="conditions", default=None)
184184
species_data: list[SpeciesDataAnnot] = element(
185185
tag="speciesData",
@@ -291,7 +291,7 @@ class PHAnnot(
291291
value (float): The pH value.
292292
"""
293293

294-
value: float = attr(name="value")
294+
value: float | None = attr(name="value")
295295

296296

297297
class TemperatureAnnot(

pyenzyme/tabular.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def to_pandas(
1717
enzmldoc: EnzymeMLDocument,
1818
ignore: list[str] | None = None,
19-
) -> pd.DataFrame:
19+
) -> pd.DataFrame | None:
2020
"""This function converts an EnzymeMLDocument object to a pandas DataFrame.
2121
2222
The resulting DataFrame contains the following columns:
@@ -38,6 +38,9 @@ def to_pandas(
3838
ValueError: If the measurement does not contain species data.
3939
"""
4040

41+
if not enzmldoc.measurements:
42+
return None
43+
4144
if ignore is None:
4245
ignore = []
4346

@@ -305,11 +308,17 @@ def _measurement_to_pandas(measurement: Measurement) -> pd.DataFrame:
305308

306309
_validate_measurement(measurement)
307310

308-
data = {"time": measurement.species_data[0].time}
311+
data = {"time": _get_time_array(measurement)}
309312
for species in measurement.species_data:
310-
data[species.species_id] = species.data
313+
if len(species.data) > 0:
314+
data[species.species_id] = species.data
315+
311316
return pd.DataFrame(data)
312317

318+
def _get_time_array(measurement: Measurement):
319+
for meas_data in measurement.species_data:
320+
if len(meas_data.time) > 0:
321+
return meas_data.time
313322

314323
def _validate_measurement(meas: Measurement) -> None:
315324
"""Validates a Measurement object"""
@@ -321,6 +330,7 @@ def _validate_measurement(meas: Measurement) -> None:
321330
{
322331
species.species_id + "_time": species.time
323332
for species in meas.species_data
333+
if len(species.time) > 0
324334
}
325335
)
326336
except ValueError:

0 commit comments

Comments
 (0)