11"""Functions performing various calculations."""
22
3+ import numbers
4+ from functools import reduce
5+ from typing import Dict , List , Union
6+
37import numpy as np
48import pandas as pd
5- from functools import reduce
6- from typing import List , Union
9+ import petab
710import sympy
8- import numbers
911
1012from .C import *
11- import petab
1213
1314__all__ = ['calculate_residuals' , 'calculate_residuals_for_table' ,
1415 'get_symbolic_noise_formulas' , 'evaluate_noise_formula' ,
@@ -121,7 +122,7 @@ def calculate_residuals_for_table(
121122 return residual_df
122123
123124
124- def get_symbolic_noise_formulas (observable_df ) -> dict :
125+ def get_symbolic_noise_formulas (observable_df ) -> Dict [ str , sympy . Expr ] :
125126 """Sympify noise formulas.
126127
127128 Arguments:
@@ -144,9 +145,10 @@ def get_symbolic_noise_formulas(observable_df) -> dict:
144145
145146def evaluate_noise_formula (
146147 measurement : pd .Series ,
147- noise_formulas : dict ,
148+ noise_formulas : Dict [ str , sympy . Expr ] ,
148149 parameter_df : pd .DataFrame ,
149- simulation : numbers .Number ) -> float :
150+ simulation : numbers .Number ,
151+ ) -> float :
150152 """Fill in parameters for `measurement` and evaluate noise_formula.
151153
152154 Arguments:
@@ -165,10 +167,11 @@ def evaluate_noise_formula(
165167 # extract measurement specific overrides
166168 observable_parameter_overrides = petab .split_parameter_replacement_list (
167169 measurement .get (NOISE_PARAMETERS , None ))
168- overrides = {}
169170 # fill in measurement specific parameters
170- for i_obs_par , obs_par in enumerate (observable_parameter_overrides ):
171- overrides [f"noiseParameter{ i_obs_par + 1 } _{ observable_id } " ] = obs_par
171+ overrides = {
172+ f"noiseParameter{ i_obs_par + 1 } _{ observable_id } " : obs_par
173+ for i_obs_par , obs_par in enumerate (observable_parameter_overrides )
174+ }
172175
173176 # fill in observables
174177 overrides [observable_id ] = simulation
@@ -190,10 +193,12 @@ def evaluate_noise_formula(
190193 # conversion is possible if all parameters are replaced
191194 try :
192195 noise_value = float (noise_value )
193- except TypeError :
194- raise TypeError (
196+ except TypeError as e :
197+ raise ValueError (
195198 f"Cannot replace all parameters in noise formula { noise_value } "
196- f"for observable { observable_id } ." )
199+ f"for observable { observable_id } . "
200+ f"Missing { noise_formula .free_symbols } . Note that model states "
201+ "are currently not supported." ) from e
197202 return noise_value
198203
199204
@@ -230,12 +235,12 @@ def calculate_chi2(
230235 normalize , scale )
231236 chi2s = [calculate_chi2_for_table_from_residuals (df )
232237 for df in residual_dfs ]
233- chi2 = sum (chi2s )
234- return chi2
238+ return sum (chi2s )
235239
236240
237241def calculate_chi2_for_table_from_residuals (
238- residual_df : pd .DataFrame ) -> float :
242+ residual_df : pd .DataFrame ,
243+ ) -> float :
239244 """Compute chi2 value for a single residual table."""
240245 return (np .array (residual_df [RESIDUAL ])** 2 ).sum ()
241246
@@ -278,8 +283,7 @@ def calculate_llh(
278283 _llh = calculate_llh_for_table (
279284 measurement_df , simulation_df , observable_df , parameter_df )
280285 llhs .append (_llh )
281- llh = sum (llhs )
282- return llh
286+ return sum (llhs )
283287
284288
285289def calculate_llh_for_table (
@@ -326,8 +330,7 @@ def calculate_llh_for_table(
326330 llh = calculate_single_llh (
327331 measurement , simulation , scale , noise_distribution , noise_value )
328332 llhs .append (llh )
329- llh = sum (llhs )
330- return llh
333+ return sum (llhs )
331334
332335
333336def calculate_single_llh (
@@ -371,5 +374,4 @@ def calculate_single_llh(
371374 raise NotImplementedError (
372375 "Unsupported combination of noise_distribution and scale "
373376 f"specified: { noise_distribution } , { scale } ." )
374- llh = - nllh
375- return llh
377+ return - nllh
0 commit comments