Skip to content

Commit

Permalink
V 021 (#455)
Browse files Browse the repository at this point in the history
* Parameters plot, #418 (#425)

* parameters plot, plotting a subset of parameters (#418)
* "Variable used before assignment" fixed for parameters plot (#364)

* Feature autocorrelation - close #251 (#426)

* autocorrelation calculation by sokal

* docs

* notebook using ess

* added ess and ac in result

* created ess calculation

* rename variable

* docs

* fix

* remove print from test

* parameter index in 1d marginal plot

* added type

* added type

* docu

* flake8

* Update pypesto/sample/diagnostics.py

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* docs

* docs

* grammar thingy

* handle in get_data_to_plot in visu

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* fix display for least squares solver (#430)

fix #429

* basic sanity checking for parameter fixing (#420)

* basic sanity checking for parameter fixing

* fixup type checks

* fixup

* fix typehints

* fixup & refactor

* update doc

* fixup

* Fix amici error output, fixes #427 (#428)

* initial fix

* add filter function

* unify error and base initialization of return values

* Profile options (#419)

* change profile indexing to parameter indices instead of boolean indexing

* adapt also approximation and test to new profile api

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Prior Example, closes # 434 (#438)

* Add notebook prior definition

* fix sphinx

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/example/prior_definition.ipynb

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Improve notebook

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Fix offset_y and scale_y in waterfal plot (#441)

* Correct offset_y for waterfall plots of result lists

* enforce y_limits which show whole data when plotting result lists, if no explicit user input was given

* fix nans and infs, deal with possibly different number of optiimzation runs in different result objects

* fix 2 typos (#444)

Co-authored-by: Thomas S. Ligon <tom@thomassligon.info>

* Update priors.py (#448)

* Fix optimization with priors (#452)

* Update priors.py

* Update priors.py

* Fix PEtab import (Priors) (#454), closes #447

* bugfix + compliance to current PEtab version in prior import

* resolve review + flake8

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* prepare version 0.2.1

* Update doc/releasenotes.rst

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/releasenotes.rst

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

* Update doc/releasenotes.rst

Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>

Co-authored-by: Polina Lakrisenko <p.lakrisenko@gmail.com>
Co-authored-by: Elba Raimúndez Alvarez <elba.raimundez@helmholtz-muenchen.de>
Co-authored-by: Yannik Schälte <31767307+yannikschaelte@users.noreply.github.com>
Co-authored-by: Fabian Fröhlich <fabian@schaluck.com>
Co-authored-by: Paul Stapor <paul.stapor@helmholtz-muenchen.de>
Co-authored-by: Thomas S. Ligon <tom@thomassligon.info>
  • Loading branch information
7 people committed Sep 7, 2020
1 parent 8951c39 commit cbec7ee
Show file tree
Hide file tree
Showing 29 changed files with 1,195 additions and 336 deletions.
2 changes: 2 additions & 0 deletions doc/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following examples cover typical use cases and should help get a better idea
example/store.ipynb
example/sampler_study.ipynb
example/sampling_diagnostics.ipynb
example/prior_definition.ipynb

Download the examples as notebooks
----------------------------------
Expand All @@ -26,6 +27,7 @@ Download the examples as notebooks
* :download:`Storage <example/store.ipynb>`
* :download:`Sampler study <example/sampler_study.ipynb>`
* :download:`Sampling diagnostics <example/sampling_diagnostics.ipynb>`
* :download:`Prior definition <example/prior_definition.ipynb>`

.. Note::
Some of the notebooks have extra dependencies.
202 changes: 101 additions & 101 deletions doc/example/fixed_parameters.ipynb

Large diffs are not rendered by default.

243 changes: 243 additions & 0 deletions doc/example/prior_definition.ipynb

Large diffs are not rendered by default.

104 changes: 83 additions & 21 deletions doc/example/sampling_diagnostics.ipynb

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions doc/releasenotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ Release notes
0.2 series
..........

0.2.1 (2020-09-07)
------------------

* Example Notebook for prior functionality (#438)
* Changed parameter indexing in profiling routines (#419)
* Basic sanity checking for parameter fixing (#420)
* Bug fixes in:
* Displaying of multi start optimization (#430)
* AMICI error output (#428)
* Axes scaling/limits in waterfall plots (#441)
* Priors (PEtab impor, error handling) (#448, #452, #454)
* Improved sampling diagnostics (e.g. effective samples size) (#426)
* Improvements and bug fixes in parameter plots (#425)


0.2.0 (2020-06-17)
------------------
Expand Down
2 changes: 1 addition & 1 deletion pypesto/objective/amici.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def __init__(self,
# need to do preequilibration
if self.guess_steadystate:
if self.amici_model.ncl() > 0:
raise ValueError('Steadystate prediciton is not supported for'
raise ValueError('Steadystate prediction is not supported for '
'models with conservation laws!')

if self.amici_model.getSteadyStateSensitivityMode() == \
Expand Down
33 changes: 9 additions & 24 deletions pypesto/objective/amici_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
)
from .amici_util import (
add_sim_grad_to_opt_grad, add_sim_hess_to_opt_hess,
sim_sres_to_opt_sres, log_simulation, get_error_output)
sim_sres_to_opt_sres, log_simulation, get_error_output, filter_return_dict,
init_return_values
)

try:
import amici
Expand Down Expand Up @@ -128,24 +130,11 @@ def calculate_function_values(rdatas,

# check if the simulation failed
if any(rdata['status'] < 0.0 for rdata in rdatas):
return get_error_output(amici_model, edatas, rdatas, dim)

# prepare outputs
nllh = 0.0
snllh = None
s2nllh = None
if mode == MODE_FUN and sensi_order > 0:
snllh = np.zeros(dim)
s2nllh = np.zeros([dim, dim])

chi2 = None
res = None
sres = None
if mode == MODE_RES:
chi2 = 0.0
res = np.zeros([0])
if sensi_order > 0:
sres = np.zeros([0, dim])
return get_error_output(amici_model, edatas, rdatas,
sensi_order, mode, dim)

nllh, snllh, s2nllh, chi2, res, sres = init_return_values(sensi_order,
mode, dim)

par_sim_ids = list(amici_model.getParameterIds())
sensi_method = amici_solver.getSensitivityMethod()
Expand Down Expand Up @@ -212,8 +201,4 @@ def calculate_function_values(rdatas,
SRES: sres,
RDATAS: rdatas
}
return {
key: val
for key, val in ret.items()
if val is not None
}
return filter_return_dict(ret)
63 changes: 56 additions & 7 deletions pypesto/objective/amici_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from typing import Dict, Sequence, Union
import logging

from .constants import FVAL, GRAD, HESS, RES, SRES, RDATAS
from .constants import (
FVAL, CHI2, GRAD, HESS, RES, SRES, RDATAS, MODE_FUN, MODE_RES
)

try:
import amici
Expand Down Expand Up @@ -224,6 +226,8 @@ def get_error_output(
amici_model: AmiciModel,
edatas: Sequence['amici.ExpData'],
rdatas: Sequence['amici.ReturnData'],
sensi_order: int,
mode: str,
dim: int):
"""Default output upon error.
Expand All @@ -237,11 +241,56 @@ def get_error_output(
for data in edatas)
n_res = nt * amici_model.nytrue

return {
FVAL: np.inf,
GRAD: np.nan * np.ones(dim),
HESS: np.nan * np.ones([dim, dim]),
RES: np.nan * np.ones(n_res),
SRES: np.nan * np.ones([n_res, dim]),
nllh, snllh, s2nllh, chi2, res, sres = init_return_values(sensi_order,
mode, dim, True)
if res is not None:
res = np.nan * np.ones(n_res)
if sres is not None:
sres = np.nan * np.ones([n_res, dim])

ret = {
FVAL: nllh,
CHI2: chi2,
GRAD: snllh,
HESS: s2nllh,
RES: res,
SRES: sres,
RDATAS: rdatas
}
return filter_return_dict(ret)


def init_return_values(sensi_order, mode, dim, error=False):
if error:
fval = np.inf
sval = np.nan
else:
fval = sval = 0.0

nllh = fval
snllh = None
s2nllh = None
if mode == MODE_FUN and sensi_order > 0:
snllh = sval * np.ones(dim)
if sensi_order > 1:
s2nllh = sval * np.ones([dim, dim])

chi2 = None
res = None
sres = None
if mode == MODE_RES:
chi2 = fval
res = np.zeros([0])
if sensi_order > 0:
sres = np.zeros([0, dim])

return nllh, snllh, s2nllh, chi2, res, sres


def filter_return_dict(ret):
"""Filters return dict for non-None values"""
return {
key: val
for key, val in ret.items()
if val is not None
}
14 changes: 8 additions & 6 deletions pypesto/objective/priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ def call_unprocessed(

res = {}

res[FVAL] = self.neg_log_density(x)

for order in sensi_orders:
if order == 0:
res[FVAL] = self.neg_log_density(x)
continue
elif order == 1:
res[GRAD] = self.gradient_neg_log_density(x)
elif order == 2:
res[HESS] = self.hessian_neg_log_density(x)
else:
ValueError(f'Invalid sensi order {order}.')
raise ValueError(f'Invalid sensi order {order}.')

return res

Expand All @@ -104,8 +106,8 @@ def check_mode(self, mode) -> bool:
elif mode == MODE_RES:
return False
else:
ValueError(f'Invalid input: Expected mode {MODE_FUN} or'
f' {MODE_RES}, received {mode} instead.')
raise ValueError(f'Invalid input: Expected mode {MODE_FUN} or'
f' {MODE_RES}, received {mode} instead.')

def neg_log_density(self, x):
"""
Expand Down Expand Up @@ -347,8 +349,8 @@ def dd_log_f_ddx(x):
# when implementing: add to tests
raise NotImplementedError
else:
ValueError(f'NegLogPriors of type {prior_type} are currently '
'not supported')
raise ValueError(f'NegLogPriors of type {prior_type} are currently '
'not supported')


def _get_linear_function(slope: float,
Expand Down
11 changes: 7 additions & 4 deletions pypesto/optimize/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@ def minimize(
# TODO: pass jac computing methods in options

if self.options is not None:
self.options['verbose'] = 2 if 'disp' in self.options.keys() \
and self.options['disp'] else 0
self.options.pop('disp', None)
ls_options = self.options.copy()
ls_options['verbose'] = 2 if 'disp' in ls_options.keys() \
and ls_options['disp'] else 0
ls_options.pop('disp', None)
else:
ls_options = {}

# optimize
res = scipy.optimize.least_squares(
Expand All @@ -320,7 +323,7 @@ def minimize(
bounds=bounds,
tr_solver='exact',
loss='linear',
**self.options
**ls_options
)
# extract fval/grad from result, note that fval is not available
# from least squares solvers
Expand Down
7 changes: 2 additions & 5 deletions pypesto/petab/importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pandas as pd
import numpy as np
import os
import sys
import importlib
Expand Down Expand Up @@ -283,10 +282,8 @@ def create_prior(self) -> NegLogParameterPriors:
prior_type_entry = self.petab_problem.\
parameter_df.loc[x_id, petab.OBJECTIVE_PRIOR_TYPE]

# TODO: Change the hardcoded "uninformative" to
# petab.UNINFORMATIVE, if corresponding PEtab PR is merged
if not (np.isnan(prior_type_entry)
or prior_type_entry == 'uninformative'):
if (isinstance(prior_type_entry, str)
and prior_type_entry != petab.PARAMETER_SCALE_UNIFORM):

prior_params = [float(param) for param in
self.petab_problem.parameter_df.
Expand Down
Loading

0 comments on commit cbec7ee

Please sign in to comment.