Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing some warnings #1430

Merged
merged 10 commits into from
Jul 23, 2024
23 changes: 15 additions & 8 deletions pypesto/hierarchical/inner_calculator_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import copy
import warnings
from collections.abc import Sequence
from typing import Union

Expand Down Expand Up @@ -404,14 +405,20 @@ def __call__(

x_dct = copy.deepcopy(x_dct)
x_dct.update(self.necessary_par_dummy_values)
# fill in parameters
fill_in_parameters(
edatas=edatas,
problem_parameters=x_dct,
scaled_parameters=True,
parameter_mapping=parameter_mapping,
amici_model=amici_model,
)
# fill in parameters, we expect here a RunTimeWarning to occur
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The following problem parameters were not used:.*",
category=RuntimeWarning,
)
fill_in_parameters(
edatas=edatas,
problem_parameters=x_dct,
scaled_parameters=True,
parameter_mapping=parameter_mapping,
amici_model=amici_model,
)

# run amici simulation
rdatas = amici.runAmiciSimulations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Any

import libsbml
import petab.v1
import petab.v1 as petab
import roadrunner
from petab.v1.C import (
OBSERVABLE_FORMULA,
Expand Down
29 changes: 17 additions & 12 deletions pypesto/sample/geweke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ def spectrum(x: np.ndarray, nfft: int = None, nw: int = None) -> np.ndarray:
Xx = np.absolute(np.fft.fft(xw, n=nfft, axis=0)) ** 2
spectral_density += Xx

# Normalize
spectral_density = spectral_density * (1 / kmu)

n2 = np.floor(nfft / 2).astype(int)

spectral_density = spectral_density[0:n2]

# Normalize
if kmu != 0:
spectral_density = spectral_density * (1 / kmu)
else:
spectral_density = np.full(spectral_density.shape, np.nan)

return spectral_density


Expand Down Expand Up @@ -149,16 +152,18 @@ def calculate_zscore(
# Mean of Second fraction
mean_b = np.mean(chain[index_b:, :], axis=0)

# Spectral estimates for variance
spectrum_a = spectrum0(chain[0:index_a, :])
spectrum_b = spectrum0(chain[index_b:, :])
# Spectral estimates for variance
spectrum_a = spectrum0(chain[0:index_a, :])
spectrum_b = spectrum0(chain[index_b:, :])

# Calculate z-score
z_score = (mean_a - mean_b) / (
np.sqrt(spectrum_a / index_a + spectrum_b / (nsamples - index_b + 1))
)
# Calculate significance (p value)
p = 2 * (1 - norm.cdf(np.absolute(z_score)))
# Calculate z-score
z_score = (mean_a - mean_b) / (
np.sqrt(
spectrum_a / index_a + spectrum_b / (nsamples - index_b + 1)
)
)
# Calculate significance (p value)
p = 2 * (1 - norm.cdf(np.absolute(z_score)))

return z_score, p

Expand Down
6 changes: 2 additions & 4 deletions pypesto/store/read_from_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ast
import logging
import warnings
from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -121,10 +120,9 @@ def read(self, objective: ObjectiveBase = None) -> Problem:
if objective is None:
objective = Objective()
# raise warning that objective is not loaded.
warnings.warn(
logger.debug(
Comment on lines -124 to +123
Copy link
Member

Choose a reason for hiding this comment

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

Using warnings here was intended to make it easier to disable those, see #1253

"You are loading a problem. This problem is not to be used "
"without a separately created objective.",
stacklevel=2,
"without a separately created objective."
)
problem = Problem(objective, [], [])

Expand Down
7 changes: 2 additions & 5 deletions test/base/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import tempfile

import numpy as np
import pytest
import scipy.optimize as so

import pypesto
Expand Down Expand Up @@ -95,8 +94,7 @@ def test_storage_problem(hdf5_file):
problem_writer = ProblemHDF5Writer(hdf5_file)
problem_writer.write(problem)
problem_reader = ProblemHDF5Reader(hdf5_file)
with pytest.warns(UserWarning, match="loading a problem"):
read_problem = problem_reader.read()
read_problem = problem_reader.read()
problem_attrs = [
value
for name, value in vars(ProblemHDF5Writer).items()
Expand Down Expand Up @@ -371,8 +369,7 @@ def test_storage_all():
filename = "test_file.hdf5"
try:
write_result(result=result, filename=filename)
with pytest.warns(UserWarning, match="loading a problem"):
result_read = read_result(filename=filename)
result_read = read_result(filename=filename)

# test optimize
for i, opt_res in enumerate(result.optimize_result.list):
Expand Down
2 changes: 1 addition & 1 deletion test/base/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ def test_sample():
n_samples + 1,
len(crproblem.p_true),
)

sample.geweke_test(sample_result)
# visualize the results
visualize.sampling_1d_marginals(sample_result)
5 changes: 1 addition & 4 deletions test/optimize/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,7 @@ def test_mpipoolengine():
)

# read results
with pytest.warns(UserWarning, match="You are loading a problem."):
result1 = read_result(
"temp_result.h5", problem=True, optimize=True
)
result1 = read_result("temp_result.h5", problem=True, optimize=True)
# set optimizer
optimizer = optimize.FidesOptimizer(verbose=40)
# initialize problem with x_guesses and objective
Expand Down
25 changes: 15 additions & 10 deletions test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,22 @@ def get_fy(self):

def fy(p):
p0, p1 = p
e = anp.exp(-(p0 + p1) * self.ts)
x = (
1
/ (-p0 - p1)
* anp.array(
[
[-p1 - p0 * e, -p1 + p1 * e],
[-p0 + p0 * e, -p0 - p1 * e],
]
if p0 + p1 == 0:
# Return NaNs with the same shape as `x` would have
x_shape = (2, 2, len(self.ts))
x = anp.full(x_shape, anp.nan)
else:
e = anp.exp(-(p0 + p1) * self.ts)
x = (
1
/ (-p0 - p1)
* anp.array(
[
[-p1 - p0 * e, -p1 + p1 * e],
[-p0 + p0 * e, -p0 - p1 * e],
]
)
)
)
y = anp.einsum("mnr,n->mr", x, self.x0)
return y

Expand Down
Loading