Skip to content

Commit

Permalink
Merge pull request #31 from MetaSys-LISBP/dev
Browse files Browse the repository at this point in the history
Added Continuous integration
  • Loading branch information
llegregam committed May 16, 2023
2 parents 860acad + 589ae0e commit 6536b31
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 2,893 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Launch tests

on:
pull_request:
types: [opened, synchronize]

jobs:
test:
runs-on: ${{ matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: [ '3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{matrix.python-version}}
uses: actions/setup-python@v4
with:
python-version: ${{matrix.python-version}}
architecture: x64
- name: Install Dependencies
run:
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
5 changes: 3 additions & 2 deletions physiofit/base/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ def initialize_fitter(self, data, **kwargs):
"""
Initialize a PhysioFitter object
:param data: input data
:param kwargs: Keyword arguments for fitter initialization
:return: None
"""

wrong_keys = []
#
# wrong_keys = []

# Initialize fitter
fitter = PhysioFitter(
Expand Down
34 changes: 29 additions & 5 deletions physiofit/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,47 @@
the fixture function's name.
"""

import os

import pandas as pd
import pytest
from numpy import nan
from physiofit.base.io import IoHandler
from physiofit.models.base_model import StandardDevs

@pytest.fixture
def data():
"""Test data to use in tests"""
"""Test data to use in tests (taken from Bergès et al., 2021 --> KEIO_ROBOT1_1)"""

return IoHandler.read_data(
r"C:\Users\legregam\PycharmProjects\PhysioFit\physiofit\data\data_example.tsv"
return pd.DataFrame.from_dict(
{
"time": [0, 1.18, 2.27, 3.13, 3.77, 4.42, 4.82, 0.67, 1.72, 2.8, 3.63, 4.27, 4.88],
"X": [0.03, 0.05, 0.08, 0.13, 0.18, 0.24, 0.34, nan, nan, nan, nan, nan, nan],
"Glucose": [nan, nan, nan, nan, nan, nan, nan, 14, 15.30, 13.68, 12.81, 12.15, 10.93],
"Acetate": [nan, nan, nan, nan, nan, nan, nan, 0.01, 0.33, 0.72, 1.17, 1.63, 2.14]
}
)

# time X Glc Ace
# 0 0.031752 NA NA
# 1.18888888888889 0.05292 NA NA
# 2.27694444444444 0.084672 NA NA
# 3.12833333333333 0.134568 NA NA
# 3.77138888888889 0.175392 NA NA
# 4.41555555555556 0.244944 NA NA
# 4.82277777777778 0.337176 NA NA
# 0.0666666666666667 NA 14.0042771787617 0.0130592124021829
# 1.71666666666667 NA 15.3032563161238 0.325073443768164
# 2.8 NA 13.6751055268759 0.722570137805818
# 3.63333333333333 NA 12.8110643342243 1.16940283499877
# 4.26666666666667 NA 12.1520761714542 1.62777664140305
# 4.88333333333333 NA 10.9297935900362 2.13639255382759

@pytest.fixture
def sds():
return StandardDevs(
X = 0.2,
Glc = 0.5,
Ace = 0.5
Glucose = 0.5,
Acetate = 0.5
)

110 changes: 110 additions & 0 deletions physiofit/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""
Test the creation and use of the PhysioFitter
"""
import numpy as np
import pandas as pd
import pytest
import physiofit


def test_physiofitter(data, sds):
"""
Test that the model and PhysioFitter can be safely instanciated from IoHandler
"""

io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", data)
assert isinstance(model, physiofit.models.base_model.Model)
model.get_params()
fitter = io.initialize_fitter(
model.data,
model=model,
sd=sds,
debug_mode=True
)
assert isinstance(fitter, physiofit.base.fitter.PhysioFitter)

def test_simulation(data, sds):

io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", data)
model.get_params()
sim_mat = model.simulate(
[param for param in model.parameters_to_estimate.values()],
model.experimental_matrix,
model.time_vector,
model.fixed_parameters
)
assert isinstance(sim_mat, np.ndarray)

def test_wrong_entry_for_model_data(data, sds):

with pytest.raises(AttributeError):
io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", None)
with pytest.raises(AttributeError):
model = io.select_model(
"Steady-state batch model",
np.array(
[[0, 1, 2],[0, 1, 2]]
)
)
with pytest.raises(AttributeError):
model = io.select_model(
"Steady-state batch model",
"Hello world this is an error"
)

def test_optimization_process(data, sds):

io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", data)
model.get_params()
fitter = io.initialize_fitter(
model.data,
model=model,
sd=sds,
debug_mode=True
)
fitter.optimize()
assert isinstance(fitter.simulated_data, pd.DataFrame)
for col in ["X", "Glucose", "Acetate"]:
assert col in fitter.simulated_data.columns

def test_monte_carlo(data, sds):
io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", data)
model.get_params()
fitter = io.initialize_fitter(
model.data,
model=model,
sd=sds,
debug_mode=True
)
fitter.optimize()
fitter.monte_carlo_analysis()
assert hasattr(fitter, "matrices_ci")
assert isinstance(fitter.matrices_ci["lower_ci"], np.ndarray)
assert isinstance(fitter.matrices_ci["higher_ci"], np.ndarray)
assert np.subtract(fitter.matrices_ci["higher_ci"], fitter.matrices_ci["lower_ci"]).all() > 0
assert hasattr(fitter, "parameter_stats")
assert isinstance(fitter.parameter_stats, dict)

def test_that_simulated_and_experimental_matrices_are_close(data, sds):
io = physiofit.base.io.IoHandler()
model = io.select_model("Steady-state batch model", data)
model.get_params()
fitter = io.initialize_fitter(
model.data,
model=model,
sd=sds,
debug_mode=True
)
fitter.optimize()
assert np.allclose(
a=fitter.experimental_matrix,
b=fitter.simulated_data,
rtol=1,
equal_nan=True
)

28 changes: 0 additions & 28 deletions physiofit/tests/test_fitter.py

This file was deleted.

Loading

0 comments on commit 6536b31

Please sign in to comment.