Skip to content

Commit 70ef57b

Browse files
authored
Add v2.calculate / fix v1.calculate (#395)
* Mostly a copy of `v1.calculate` with a few adaptations to handle the new noise distributions and placeholder format. * Fix sign of residuals * Fix incorrect scaling of observables used in noise expressions for log-type-distributions
1 parent b0f4c4f commit 70ef57b

File tree

6 files changed

+984
-41
lines changed

6 files changed

+984
-41
lines changed

petab/v1/calculate.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Functions performing various calculations."""
22

33
import numbers
4+
import operator
45
from functools import reduce
56

67
import numpy as np
@@ -139,19 +140,17 @@ def calculate_residuals_for_table(
139140
# apply scaling
140141
observable = observable_df.loc[row[OBSERVABLE_ID]]
141142
trafo = observable.get(OBSERVABLE_TRANSFORMATION, LIN)
142-
simulation = petab.scale(simulation, trafo)
143-
measurement = petab.scale(measurement, trafo)
143+
scaled_simulation = petab.scale(simulation, trafo)
144+
scaled_measurement = petab.scale(measurement, trafo)
144145

145146
# non-normalized residual is just the difference
146-
residual = simulation - measurement
147+
residual = scaled_measurement - scaled_simulation
147148

148-
noise_value = 1
149149
if normalize:
150-
# look up noise standard deviation
151-
noise_value = evaluate_noise_formula(
150+
# divide by standard deviation
151+
residual /= evaluate_noise_formula(
152152
row, noise_formulas, parameter_df, simulation
153153
)
154-
residual /= noise_value
155154

156155
# fill in value
157156
residual_df.loc[irow, RESIDUAL] = residual
@@ -169,13 +168,10 @@ def get_symbolic_noise_formulas(observable_df) -> dict[str, sp.Expr]:
169168
"""
170169
noise_formulas = {}
171170
# iterate over observables
172-
for row in observable_df.itertuples():
173-
observable_id = row.Index
174-
if NOISE_FORMULA not in observable_df.columns:
175-
noise_formula = None
176-
else:
177-
noise_formula = sympify_petab(row.noiseFormula)
178-
noise_formulas[observable_id] = noise_formula
171+
for observable_id, row in observable_df.iterrows():
172+
noise_formulas[observable_id] = (
173+
sympify_petab(row.noiseFormula) if NOISE_FORMULA in row else None
174+
)
179175
return noise_formulas
180176

181177

@@ -364,7 +360,7 @@ def calculate_llh_for_table(
364360
(simulation_df[col] == row[col]) | petab.is_empty(row[col])
365361
for col in compared_cols
366362
]
367-
mask = reduce(lambda x, y: x & y, masks)
363+
mask = reduce(operator.and_, masks)
368364

369365
simulation = simulation_df.loc[mask][SIMULATION].iloc[0]
370366

@@ -375,7 +371,7 @@ def calculate_llh_for_table(
375371

376372
# get noise standard deviation
377373
noise_value = evaluate_noise_formula(
378-
row, noise_formulas, parameter_df, petab.scale(simulation, scale)
374+
row, noise_formulas, parameter_df, simulation
379375
)
380376

381377
# get noise distribution

0 commit comments

Comments
 (0)