Skip to content

Commit

Permalink
fix unit ids not retrieved corectly
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Sep 25, 2024
1 parent a0282d4 commit d1fa04e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
23 changes: 19 additions & 4 deletions pyenzyme/sbml/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Callable, List

import rich
import libsbml
import pandas as pd
from loguru import logger
Expand All @@ -18,6 +19,7 @@
from pyenzyme.sbml.versions import v2
from pyenzyme.tabular import to_pandas
from pyenzyme.units.units import UnitDefinition
from pyenzyme.tools import to_dict_wo_json_ld

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


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

if unit is None:
return None

if unit.id is None:
raise ValueError(f"Unit {unit.name} does not have an ID")
for unit2 in units:
if _same_unit(unit, unit2):
return unit2.id

return unit.id

raise ValueError(f"Unit {unit.name} not found in the list of units")

def _same_unit(unit1: pe.UnitDefinition, unit2: pe.UnitDefinition) -> bool:
"""Check if two units are the same."""

unit1 = to_dict_wo_json_ld(unit1)
unit2 = to_dict_wo_json_ld(unit2)

del unit1["id"]
del unit2["id"]

return unit1 == unit2

def _validate_sbml(sbmldoc: libsbml.SBMLDocument) -> None:
"""Validate the SBML document using the libSBML function."""

sbml_errors = sbmldoc.checkConsistency()
valid = True

Expand Down
4 changes: 2 additions & 2 deletions pyenzyme/sbml/versions/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class MeasurementAnnot(

id: str = attr(name="id")
name: str | None = attr(name="name", default=None)
time_unit: str = attr(name="timeUnit")
time_unit: str | None = attr(name="timeUnit")
conditions: ConditionsAnnot | None = element(tag="conditions", default=None)
species_data: list[SpeciesDataAnnot] = element(
tag="speciesData",
Expand Down Expand Up @@ -291,7 +291,7 @@ class PHAnnot(
value (float): The pH value.
"""

value: float = attr(name="value")
value: float | None = attr(name="value")


class TemperatureAnnot(
Expand Down
16 changes: 13 additions & 3 deletions pyenzyme/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def to_pandas(
enzmldoc: EnzymeMLDocument,
ignore: list[str] | None = None,
) -> pd.DataFrame:
) -> pd.DataFrame | None:
"""This function converts an EnzymeMLDocument object to a pandas DataFrame.
The resulting DataFrame contains the following columns:
Expand All @@ -38,6 +38,9 @@ def to_pandas(
ValueError: If the measurement does not contain species data.
"""

if not enzmldoc.measurements:
return None

if ignore is None:
ignore = []

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

_validate_measurement(measurement)

data = {"time": measurement.species_data[0].time}
data = {"time": _get_time_array(measurement)}
for species in measurement.species_data:
data[species.species_id] = species.data
if len(species.data) > 0:
data[species.species_id] = species.data

return pd.DataFrame(data)

def _get_time_array(measurement: Measurement):
for meas_data in measurement.species_data:
if len(meas_data.time) > 0:
return meas_data.time

def _validate_measurement(meas: Measurement) -> None:
"""Validates a Measurement object"""
Expand All @@ -321,6 +330,7 @@ def _validate_measurement(meas: Measurement) -> None:
{
species.species_id + "_time": species.time
for species in meas.species_data
if len(species.time) > 0
}
)
except ValueError:
Expand Down

0 comments on commit d1fa04e

Please sign in to comment.