Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions petab/v1/calculate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Functions performing various calculations."""

import numbers
import operator
from functools import reduce

import numpy as np
Expand Down Expand Up @@ -139,19 +140,17 @@ def calculate_residuals_for_table(
# apply scaling
observable = observable_df.loc[row[OBSERVABLE_ID]]
trafo = observable.get(OBSERVABLE_TRANSFORMATION, LIN)
simulation = petab.scale(simulation, trafo)
measurement = petab.scale(measurement, trafo)
scaled_simulation = petab.scale(simulation, trafo)
scaled_measurement = petab.scale(measurement, trafo)

# non-normalized residual is just the difference
residual = simulation - measurement
residual = scaled_measurement - scaled_simulation

noise_value = 1
if normalize:
# look up noise standard deviation
noise_value = evaluate_noise_formula(
# divide by standard deviation
residual /= evaluate_noise_formula(
row, noise_formulas, parameter_df, simulation
)
residual /= noise_value

# fill in value
residual_df.loc[irow, RESIDUAL] = residual
Expand All @@ -169,13 +168,10 @@ def get_symbolic_noise_formulas(observable_df) -> dict[str, sp.Expr]:
"""
noise_formulas = {}
# iterate over observables
for row in observable_df.itertuples():
observable_id = row.Index
if NOISE_FORMULA not in observable_df.columns:
noise_formula = None
else:
noise_formula = sympify_petab(row.noiseFormula)
noise_formulas[observable_id] = noise_formula
for observable_id, row in observable_df.iterrows():
noise_formulas[observable_id] = (
sympify_petab(row.noiseFormula) if NOISE_FORMULA in row else None
)
return noise_formulas


Expand Down Expand Up @@ -364,7 +360,7 @@ def calculate_llh_for_table(
(simulation_df[col] == row[col]) | petab.is_empty(row[col])
for col in compared_cols
]
mask = reduce(lambda x, y: x & y, masks)
mask = reduce(operator.and_, masks)

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

Expand All @@ -375,7 +371,7 @@ def calculate_llh_for_table(

# get noise standard deviation
noise_value = evaluate_noise_formula(
row, noise_formulas, parameter_df, petab.scale(simulation, scale)
row, noise_formulas, parameter_df, simulation
)

# get noise distribution
Expand Down
Loading