From de446b61a05012ae8494b8c11efb6081cc4cc824 Mon Sep 17 00:00:00 2001 From: Jan Range Date: Tue, 24 Sep 2024 12:22:28 +0200 Subject: [PATCH] fix units not recognized --- pyenzyme/sbml/serializer.py | 15 ++++++++++++++- pyenzyme/tabular.py | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pyenzyme/sbml/serializer.py b/pyenzyme/sbml/serializer.py index de3e9b2..5e440eb 100644 --- a/pyenzyme/sbml/serializer.py +++ b/pyenzyme/sbml/serializer.py @@ -67,7 +67,10 @@ def to_sbml( sbmldoc = libsbml.SBMLDocument() model = sbmldoc.createModel() model.setName(doc.name) - units = _assign_ids_to_units(tools.find_unique(doc, UnitDefinition)) + units = _assign_ids_to_units(tools.find_unique(doc, pe.UnitDefinition)) + + # Add units that have been defined by the custom UnitDefinition + convert_unit_classes(doc, units) print_warnings = verbose @@ -109,6 +112,16 @@ def to_sbml( logger.info(f"OMEX archive written to {out}") +def convert_unit_classes(doc: pe.EnzymeMLDocument, custom_units: list[UnitDefinition]): + custom_units.extend( + [ + pe.UnitDefinition(**unit.model_dump()) + for unit in _assign_ids_to_units(tools.find_unique(doc, pe.UnitDefinition)) + if unit.id not in [unit.id for unit in custom_units] + ] + ) + + def _add_unit_definitions(unit: UnitDefinition): """Add unit definitions to the SBML model.""" diff --git a/pyenzyme/tabular.py b/pyenzyme/tabular.py index 1185dd3..32c1e4e 100644 --- a/pyenzyme/tabular.py +++ b/pyenzyme/tabular.py @@ -59,7 +59,10 @@ def to_pandas( df["id"] = [meas.id] * len(df) dfs.append(df) - return pd.concat(dfs, ignore_index=True).reset_index(drop=True) + if dfs: + return pd.concat(dfs, ignore_index=True).reset_index(drop=True) + else: + return pd.DataFrame() def read_excel(