Skip to content

Commit

Permalink
Fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Mar 10, 2022
1 parent 74f5bee commit 22da0bd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pyenzyme/enzymeml/core/enzymemldocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import ast
import json
from tkinter import TRUE
import yaml
import logging
import pandas as pd
Expand Down Expand Up @@ -551,7 +552,7 @@ def exportMeasurementData(
self,
measurement_ids: Union[str, List[str]] = "all",
species_ids: Union[str, List[str]] = "all",
proteins: bool = False,
proteins: bool = True,
reactants: bool = True,
) -> Dict[str, Dict[str, Union[Tuple, pd.DataFrame]]]:
"""Exports either all replicates present in any measurement or the ones specified via 'species_ids' or 'measurement_ids'
Expand All @@ -564,6 +565,11 @@ def exportMeasurementData(
Dict[str, Dict[str, Union[tuple, pd.DataFrame]]]: The data corresponding to the specified options. The dictionary will still distinguish between meassuremnts.
"""

if proteins is False and reactants is False:
raise ValueError(
"Export of data needs at least one of 'protein' and 'reactants' specified. Otherwise no data can be exported."
)

if isinstance(measurement_ids, str):
measurement_ids = [measurement_ids]
if isinstance(species_ids, str):
Expand All @@ -577,15 +583,20 @@ def exportMeasurementData(
data = measurement.exportData(species_ids=species_ids)

# Initialize the data dict that will be returned
measurement_data = {}
df = {}
init_conc = {}

if reactants:
measurement_data.update(data["reactants"])
df.update(data["reactants"]["data"].to_dict())
init_conc.update(data["reactants"]["initConc"])
if proteins:
measurement_data.update(data["proteins"])
df.update(data["proteins"]["data"].to_dict())
init_conc.update(data["proteins"]["initConc"])

if measurement_data["data"] is not None:
replicate_data[measurement_id] = measurement_data
replicate_data[measurement_id] = {
"data": pd.DataFrame(df),
"initConc": init_conc,
}

return replicate_data

Expand Down
70 changes: 70 additions & 0 deletions tests/enzymeml/core/test_enzymemldocument.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import pytest

from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument
from pyenzyme.enzymeml.core.abstract_classes import AbstractSpecies
Expand Down Expand Up @@ -152,3 +153,72 @@ def test_add_species(self):

assert "p0" in enzmldoc.protein_dict
assert species == enzmldoc.protein_dict["p0"]

def test_unit_change(self, enzmldoc):
"""Tests whether units remain consistent when changed at run-time and are reloaded"""

# Change an arbitrary unit
species = enzmldoc.getReactant("s0")
species.unit = "umole / l"

# Write to file
enzmldoc.toFile("./tests/tmp/", name="Test_Unit_Change")

# Read file and check if the unit change is consistent
nu_enzmldoc = EnzymeMLDocument.fromFile("./tests/tmp/Test_Unit_Change.omex")
unit = enzmldoc.getReactant("s0").unit

assert unit == "umole / l"

def test_full_data_export(self, enzmldoc):
"""Tests whether data is exported correctly"""

# Test all export of reactant/protein
data = enzmldoc.exportMeasurementData()

expected_conc = {"s0": (10.0, "mmole / l"), "p0": (10.0, "mmole / l")}
expected_data = {
"s0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
"p0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
}

assert "m0" in data
assert data["m0"]["data"].to_dict() == expected_data
assert data["m0"]["initConc"] == expected_conc

# Test case of no specification
with pytest.raises(ValueError) as exc_info:
enzmldoc.exportMeasurementData(proteins=False, reactants=False)

def test_protein_data_export(self, enzmldoc):
"""Tests whether data is exported correctly"""

# Test all export of reactant/protein
data = enzmldoc.exportMeasurementData(reactants=False)

expected_conc = {"p0": (10.0, "mmole / l")}
expected_data = {
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
"p0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
}

assert "m0" in data
assert data["m0"]["data"].to_dict() == expected_data
assert data["m0"]["initConc"] == expected_conc

def test_reactant_data_export(self, enzmldoc):
"""Tests whether data is exported correctly"""

# Test all export of reactant/protein
data = enzmldoc.exportMeasurementData(proteins=False)

expected_conc = {"s0": (10.0, "mmole / l")}
expected_data = {
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
"s0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
}

assert "m0" in data
assert data["m0"]["data"].to_dict() == expected_data
assert data["m0"]["initConc"] == expected_conc

0 comments on commit 22da0bd

Please sign in to comment.