Skip to content

Piecewise to heaviside#1352

Merged
FFroehlich merged 63 commits intodevelopfrom
piecewise_to_heaviside
Dec 10, 2020
Merged

Piecewise to heaviside#1352
FFroehlich merged 63 commits intodevelopfrom
piecewise_to_heaviside

Conversation

@paulstapor
Copy link
Contributor

Piecewise functions were so far parsed into if-else-statements, which made the model files poorly readable. Much more important though, the problem was that there might have been a discontinuity in the right hand side, but the solver didn't notice this and ran over it. This could lead to poor accuracy of the solver.

However, back from Matlab times, AMICI had it's own interface for Heaviside functions. If a piecewise function gets translated into an AMICI-Heaviside function (using the existing C++ code), both problems are solved. Moreover, this implementation should be a first step into the direction of implementing evens on the Python side.

I know this is a comparably large pull-request, sorry. But there's no way to split this into smaller parts, except including substantially less functionality, which is however urgently needed...

This implementation tries to stay as close to the existing event implementation as possible. It is supposed to be working for forward sensitivities and Heaviside functions in full generality, i.e., also supporting parameter and state-dependent arguments of HEaviside functions.

@codecov
Copy link

codecov bot commented Dec 2, 2020

Codecov Report

Merging #1352 (d9d1bfe) into develop (24b9b98) will increase coverage by 8.94%.
The diff coverage is 75.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1352      +/-   ##
===========================================
+ Coverage    78.44%   87.38%   +8.94%     
===========================================
  Files           62        2      -60     
  Lines         9797     1427    -8370     
===========================================
- Hits          7685     1247    -6438     
+ Misses        2112      180    -1932     
Flag Coverage Δ
cpp ?
petab ?
python ?
sbmlsuite 87.38% <75.00%> (-1.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
python/amici/ode_export.py 85.51% <61.72%> (-5.57%) ⬇️
python/amici/sbml_import.py 90.13% <97.87%> (-6.32%) ⬇️
python/amici/parameter_mapping.py
ThirdParty/gsl/gsl/gsl-lite.hpp
src/model_ode.cpp
python/amici/petab_import.py
python/amici/custom_commands.py
include/amici/newton_solver.h
... and 50 more

paulstapor and others added 6 commits December 2, 2020 22:01
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
@paulstapor
Copy link
Contributor Author

Thanks for the code review! I'll implement the suggestions as soon as the test cases are there!

Copy link
Member

@FFroehlich FFroehlich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go from my end

@FFroehlich FFroehlich requested a review from dweindl December 9, 2020 17:06
++++++++++++++++++++++++++++++++++++++

Python-AMICI currently **passes 850 out of the 1780 (~48%) test cases** from
Python-AMICI currently **passes 805 out of the 1780 (~45%) test cases** from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what happened, looking at the tests that are skipped I couldn't really identify those 45 that are not longer passing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is unrelated to this PR. (Here it increases from 804 to 805). This is where it happened: https://github.com/AMICI-dev/AMICI/actions/runs/331448378

Copy link
Member

@dweindl dweindl Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to work but skipped now (Should be all those AMICI does not support SBML models containing variables with SId):

969
970
971
974
1104
1105
1107
1109
1121
1434
1436
1438
1440
1442
1465
1498
1552
1554
1574
1638
1639
1651
1652
1653
1654
1655
1656
1657
1722
1724
1725
1726
1733
1734
1735
1744
1746
1748
1750
1753
1764
1765
1766
1767
1768
1773
1774

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect thats due to the skippings with msgs like

SKIPPED [2] tests/testSBMLSuite.py:87: AMICI does not support SBML models containing variables with SId k0.
3677
SKIPPED [3] tests/testSBMLSuite.py:87: AMICI does not support SBML models containing variables with SId A1_sr.
3678
SKIPPED [2] tests/testSBMLSuite.py:87: AMICI does not support SBML models containing variables with SId A_sr1.

Those are a little over 50 cases, so lets have a more detailed look in a different PR. Also looks like there are still a handful of low hanging fruits like ceil/min/max applied to numerical values, which we should be able to pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, separate PR.

@paulstapor
Copy link
Contributor Author

Just also had a look at the refactored test. Looks fine for me as well!

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 9, 2020

@FFroehlich FFroehlich merged commit 4010909 into develop Dec 10, 2020
@FFroehlich FFroehlich deleted the piecewise_to_heaviside branch December 10, 2020 00:35
@dweindl dweindl linked an issue Dec 10, 2020 that may be closed by this pull request
@FFroehlich FFroehlich mentioned this pull request Dec 12, 2020
FFroehlich added a commit that referenced this pull request Dec 15, 2020
* Fix: Don't require code context for call stack (fixes #1347) (#1348)

* Fix Intel compiler warnings (#1349)

'type qualifier on return type is meaningless'

Closes #1346

* Avoid segfault in CVODES CVAdataStore (#1351)

Does not fix the root cause of occassional segfaults, but turns them into exceptions / integration failues.

* CI: Move windows tests from TravisCI to GitHub Actions (#1354)

Closes #1350

Removes obsolete .travis.yml

* Fix cxxcode import for SymPy 1.6 (#1356)

* Piecewise to heaviside (#1352)

* parse piecewise into heaviside functions

* Add Events as model quantity, parse Heavisides slightly differently

* bugfix

* rename symbols of root to h and switch order of functions to make sure h is known when dxdotdx_explicit is generated, to have the header file of h included

* rename root symbols to HeavisideFunction_ to have things more indicative

* tell AMICI about root, stau, and deltasx functions

* heavisides without sensitivities should be working

* need to change generation of stau to generration via inverse function theorem

* compute stau via inverse function theorem

* added functionality for deltasx, but segfaulting

* bugfixing

* remove debug output

* get indentations right

* Update python/amici/ode_export.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* Update python/amici/ode_export.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* Update python/amici/ode_export.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* Update python/amici/ode_export.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* Update python/amici/ode_export.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* Update python/amici/sbml_import.py

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* add a test for trajectories

* fix sroot and test

* cosmetic

* add analytical expressions for sensitivities

* add test for a complex condition

* add test for many conditions

* fix test

* improve documentation

* rename tests and refactor test for iterated piecewise, to check SBML piecewise logic a bit further

* cleanup for code review

* further cleanup

* quarter way done with parsing heavisides into piecewise

* fix logic parsing

* monkeypatch sympy printer

* enable more tests

* implement denesting of piecewise

* fix SBML nonsense

* fix boolean triggers

* simplify logic checks

* fix code smells

* add events to variable prototypes

* cleanup

* report details for skipped tests durin GHA run

* better skip printing

* resubstitution of non-optimized RHS into stau

* add sensitivity tests

* refactor tests

* remove skipping display code

* update test stats

* Update python/amici/ode_export.py

* Apply suggestions from code review

Co-authored-by: Daniel Weindl <dweindl@users.noreply.github.com>

* document and simplify _collect_heaviside_roots

* address review comments

* reduce complexity

* misplaced docstrings

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: dilpath <dilan.private+github@outlook.com>
Co-authored-by: Daniel Weindl <dweindl@users.noreply.github.com>

* restore support for species references (#1358)

* restore support for species references

* fixup and improve error message

* extend support for sbml functions (#1357)

* add more function mappings

* fixup min/max

* fix rem

* add Mod to unsupported functions

* more trigonometric functions

* more functions, fix xref

* fix xref with init assignment

* extend unsupported functions

* fix plus with empty arguments, update statistics

* update test stats

* fix stats

* Refactor: introduce import_utils.py (#1361)

* Refactor: introduce import_utils.py

To collect format-independent functions for model import.

* add additional file to SBML Testsuite path check

* Include new module in doc

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* CI: Show all compiler warnings and treat as error (#1362)

* CI: Show all compiler errors and treat as error

Closes #1167

* Fix unused arguments

* Fix deprecated-copy

* Fix NVector (non-)const mess

* Fix CMake deprecation warning

* Fix doxygen

* Fix some typos

* Simplify / reduce number of const_casts

* Add support for different noise models for PySB import (#1360)

* Add support for different noise models for PySB import

.. as already the case for SBML import.

Closes #1176

* Respect noise model selection for PySB-PEtab import (#1339)

* doc, import

* Respect observable transformation for PySB-PEtab import (Fixes #1339)

* Set ODEModel._has_quadratic_nllh

Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>

* version bump

* fix some code smells to trigger GHA builds again?

* update docker to 20.04

* pin breathe

Co-authored-by: Daniel Weindl <dweindl@users.noreply.github.com>
Co-authored-by: Lorenzo Contento <lcontento@users.noreply.github.com>
Co-authored-by: Paul Stapor <paul.stapor@helmholtz-muenchen.de>
Co-authored-by: dilpath <dilan.private+github@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incomplete implementation of Piecewise

4 participants