Skip to content

Commit

Permalink
Update test suite
Browse files Browse the repository at this point in the history
fixup! Update test suite
  • Loading branch information
ronald-jaepel committed Mar 22, 2024
1 parent c71e705 commit a4bf77b
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 138 deletions.
39 changes: 33 additions & 6 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,50 @@ on:
jobs:
test-job:
runs-on: ubuntu-latest

defaults:
run:
shell: bash -l {0}

strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4

- name: Get Date
id: get-date
run: echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
shell: bash

- name: Setup Conda Environment
uses: conda-incubator/setup-miniconda@v3
with:
python-version: ${{ matrix.python-version }}
miniforge-variant: Mambaforge
use-mamba: true
activate-environment: cadet-python
channels: conda-forge,

- name: Cache conda
uses: actions/cache@v3
env:
# Increase this value to reset cache if environment.yml has not changed
CACHE_NUMBER: 0
with:
path: ${{ env.CONDA }}/envs
key: python_${{ matrix.python-version }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}

- name: Test Wheel install and import
run: |
mamba install python==${{ matrix.python-version }}
pip install wheel
python setup.py bdist_wheel
cd dist
pip install CADET_Python*.whl
python -c "import cadet; print(cadet.__version__)"
cd ..
- name: Test with pytest
run: |
pip install pytest
pytest tests --rootdir=tests
pip install .[testing]
mamba install cadet -c conda-forge
pytest tests --rootdir=tests -m "not slow"
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Inside of setup.cfg
[metadata]
description-file = README.md
description-file = README.md

[tool:pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
extras_require={
"testing": [
"pytest",
"matplotlib",
"pandas",
],
},
python_requires='>=3.7',
)
Empty file added tests/__init__.py
Empty file.
6 changes: 2 additions & 4 deletions examples/common.py → tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
root.input.model.solver.gs_type = 1
root.input.model.solver.max_krylov = 0
root.input.model.solver.max_restarts = 10
root.input.model.solver.schur_safety = 1e-8
root.input.model.solver.schur_safety = 1e-8

#CADET 3.1 and CADET-dev flags are in here so that it works with both
#CADET-dev removed column from the name on the inputs and outputs since for many
#operations it no longer makes sense
# CADET 3.1 and CADET-dev flags are in here so that it works with both
root.input['return'].write_solution_times = 1
root.input['return'].split_components_data = 1
root.input['return'].unit_000.write_sens_bulk = 0
Expand Down
71 changes: 35 additions & 36 deletions examples/MSSMA_2comp.py → tests/test_MSSMA_2comp.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#!/usr/bin/env python3.6
import os

# Everything in here is based on CADET3.pdf in the same directory
#

# Basic Python CADET file based interface compatible with CADET 3.0 and 3.1
# Some additional fields have been added so that the generated simulations will also
# work in 3.1 and where those differences are I have noted them.
# This whole file follows the CADET pdf documentation. I have broken the system up into many
# functions in order to make it simpler and to make code reuse easier.

# Normally the main function is placed at the bottom of the file but I have placed it at the top so that
# This interface is more like a tutorial and can be read from the top down and any given function
# can be drilled into to learn more about it.

import numpy

#use to render results
import matplotlib.pyplot as plt
import numpy
import pandas
import pytest

from cadet import Cadet
Cadet.cadet_path = "C:/Users/kosh_000/cadet_build/CADET-dev/cadet3.1-win7-x64/bin/cadet-cli.exe"
from tests import common

import common
import pandas

def gen_fraction_times(start, stop, bins):
return numpy.linspace(start, stop, bins+1, endpoint=True)
return numpy.linspace(start, stop, bins + 1, endpoint=True)


def gen_fractions(fraction_times, sim):
nComp = sim.root.input.model.unit_000.ncomp
Expand All @@ -36,24 +22,29 @@ def gen_fractions(fraction_times, sim):

for idx, (start, stop) in enumerate(zip(fraction_times[:-1], fraction_times[1:])):
selected = (times >= start) & (times <= stop)
temp = {'Start':start, 'Stop':stop}
temp = {'Start': start, 'Stop': stop}
for comp in range(nComp):
local_times = times[selected]
local_values = sim.root.output.solution.unit_001["solution_outlet_comp_%03d" % comp][selected]

temp[str(comp)] = numpy.trapz(local_values, local_times) / (stop - start)
df = df.append(temp, ignore_index=True)
return df


def main():
if not os.path.exists("tmp"):
os.makedirs("tmp")
simulation = Cadet(common.common.root)
simulation.filename = "MSSMA_2comp.h5"
simulation.filename = "tmp/MSSMA_2comp.h5"
createSimulation(simulation)
simulation.save()
data = simulation.run()
print(data)
simulation.load()
plotSimulation(simulation)
return simulation


def createSimulation(simulation):
root = simulation.root
Expand Down Expand Up @@ -97,18 +88,18 @@ def createSimulation(simulation):
root.input.model.unit_001.par_surfdiffusion = [0.0, 0.0, 0.0, 0.0, 0.0]
root.input.model.unit_001.unit_type = 'GENERAL_RATE_MODEL'
root.input.model.unit_001.velocity = 0.001

root.input.model.unit_001.adsorption.is_kinetic = 1
root.input.model.unit_001.adsorption.mssma_ka = [0.0, 1E11, 8E6, 1E11, 8E6]
root.input.model.unit_001.adsorption.mssma_kd = [0.0, 6E11, 2E16, 6E11, 2E16]
root.input.model.unit_001.adsorption.mssma_lambda = 225
root.input.model.unit_001.adsorption.mssma_nu = [0.0, 10, 25, 20, 50]
root.input.model.unit_001.adsorption.mssma_sigma = [0.0, 48, 66, 48*2, 66*2]
root.input.model.unit_001.adsorption.mssma_sigma = [0.0, 48, 66, 48 * 2, 66 * 2]
root.input.model.unit_001.adsorption.mssma_refq = 225
root.input.model.unit_001.adsorption.mssma_refc0 = 520.0
root.input.model.unit_001.adsorption.mssma_rates = [0.0, 0.0, 1e20, 10, 0.0, 0.0, 1e20, 10, 0.0]

root.input.model.unit_001.discretization.nbound = [1, 2,2]
root.input.model.unit_001.discretization.nbound = [1, 2, 2]
root.input.model.unit_001.discretization.ncol = 50
root.input.model.unit_001.discretization.npar = 5

Expand All @@ -122,12 +113,14 @@ def createSimulation(simulation):

root.input.solver.consistent_init_mode = 3


def plotSimulation(simulation):
f, (ax1, ax2) = plt.subplots(1, 2, figsize=[16, 8])
plotInlet(ax1, simulation)
plotOutlet(ax2, simulation)
f.tight_layout()
plt.show()
plt.savefig("tmp/MSSMA.png")


def plotInlet(axis, simulation):
solution_times = simulation.root.output.solution.solution_times
Expand All @@ -139,7 +132,7 @@ def plotInlet(axis, simulation):
axis.set_title("Inlet")
axis.plot(solution_times, inlet_salt, 'b-', label="Salt")
axis.set_xlabel('time (s)')

# Make the y-axis label, ticks and tick labels match the line color.
axis.set_ylabel('mMol Salt', color='b')
axis.tick_params('y', colors='b')
Expand All @@ -150,7 +143,6 @@ def plotInlet(axis, simulation):
axis2.set_ylabel('mMol Protein', color='r')
axis2.tick_params('y', colors='r')


lines, labels = axis.get_legend_handles_labels()
lines2, labels2 = axis2.get_legend_handles_labels()
axis2.legend(lines + lines2, labels + labels2, loc=0)
Expand All @@ -164,22 +156,22 @@ def plotOutlet(axis, simulation):
outlet_p2 = simulation.root.output.solution.unit_002.solution_outlet_comp_002

data = numpy.vstack([solution_times, outlet_p1]).transpose()
numpy.savetxt('comp2_1.csv', data, delimiter=',')
numpy.savetxt('tmp/comp2_1.csv', data, delimiter=',')

data = numpy.vstack([solution_times, outlet_p2]).transpose()
numpy.savetxt('comp2_2.csv', data, delimiter=',')
numpy.savetxt('tmp/comp2_2.csv', data, delimiter=',')

data = numpy.vstack([solution_times, outlet_p1 + outlet_p2]).transpose()
numpy.savetxt('comp2_comb.csv', data, delimiter=',')
numpy.savetxt('tmp/comp2_comb.csv', data, delimiter=',')

fraction_times = gen_fraction_times(6000, 14000, 10)
df = gen_fractions(fraction_times, simulation)
df.to_csv("comp2_fraction.csv", columns=('Start', 'Stop', '1'), index=False)
df.to_csv("tmp/comp2_fraction.csv", columns=('Start', 'Stop', '1'), index=False)

axis.set_title("Output")
axis.plot(solution_times, outlet_salt, 'b-', label="Salt")
axis.set_xlabel('time (s)')

# Make the y-axis label, ticks and tick labels match the line color.
axis.set_ylabel('mMol Salt', color='b')
axis.tick_params('y', colors='b')
Expand All @@ -190,13 +182,20 @@ def plotOutlet(axis, simulation):
axis2.set_ylabel('mMol Protein', color='r')
axis2.tick_params('y', colors='r')


lines, labels = axis.get_legend_handles_labels()
lines2, labels2 = axis2.get_legend_handles_labels()
axis2.legend(lines + lines2, labels + labels2, loc=0)


@pytest.mark.slow
def test_MSSMA_2comp():
sim = main()
assert isinstance(sim.root.output.solution.unit_002.solution_outlet_comp_001, numpy.ndarray)
assert isinstance(sim.root.output.solution.unit_002.solution_outlet_comp_000, numpy.ndarray)


if __name__ == "__main__":
import sys

print(sys.version)
main()
Loading

0 comments on commit a4bf77b

Please sign in to comment.