Skip to content

Commit bd0176f

Browse files
authored
Simulator: rename measurement column to simulation, with flag (#199)
1 parent 335a8d4 commit bd0176f

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

petab/simulate.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def simulate(
115115
self,
116116
noise: bool = False,
117117
noise_scaling_factor: float = 1,
118+
as_measurement: bool = False,
118119
**kwargs
119120
) -> pd.DataFrame:
120121
"""Simulate a PEtab problem, optionally with noise.
@@ -123,6 +124,9 @@ def simulate(
123124
noise: If True, noise is added to simulated data.
124125
noise_scaling_factor:
125126
A multiplier of the scale of the noise distribution.
127+
as_measurement:
128+
Whether the data column is named :const:`petab.C.MEASUREMENT`
129+
(`True`) or :const:`petab.C.SIMULATION` (`False`).
126130
**kwargs:
127131
Additional keyword arguments are passed to
128132
:meth:`petab.simulate.Simulator.simulate_without_noise`.
@@ -133,6 +137,12 @@ def simulate(
133137
simulation_df = self.simulate_without_noise(**kwargs)
134138
if noise:
135139
simulation_df = self.add_noise(simulation_df, noise_scaling_factor)
140+
141+
columns = {petab.C.MEASUREMENT: petab.C.SIMULATION}
142+
if as_measurement:
143+
columns = {petab.C.SIMULATION: petab.C.MEASUREMENT}
144+
simulation_df = simulation_df.rename(columns=columns)
145+
136146
return simulation_df
137147

138148
def add_noise(

tests/test_simulate.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ def test_zero_bounded(petab_problem):
8484
(positive if index in pos_indices else np.nan)
8585
for index in range(n_measurements)
8686
]
87-
synthetic_data_df = simulator.simulate().assign(**{
87+
synthetic_data_df = simulator.simulate(as_measurement=True).assign(**{
8888
petab.C.MEASUREMENT: measurements
8989
})
9090
# All measurements are non-zero
91-
assert (synthetic_data_df['measurement'] != 0).all()
91+
assert (synthetic_data_df[MEASUREMENT] != 0).all()
9292
# No measurements are NaN
93-
assert not (np.isnan(synthetic_data_df['measurement'])).any()
93+
assert not (np.isnan(synthetic_data_df[MEASUREMENT])).any()
9494

9595
synthetic_data_df_with_noise = simulator.add_noise(
9696
synthetic_data_df,
9797
)
9898
# Both negative and positive values are returned by default.
9999
assert all([
100-
(synthetic_data_df_with_noise['measurement'] <= 0).any(),
101-
(synthetic_data_df_with_noise['measurement'] >= 0).any(),
100+
(synthetic_data_df_with_noise[MEASUREMENT] <= 0).any(),
101+
(synthetic_data_df_with_noise[MEASUREMENT] >= 0).any(),
102102
])
103103

104104
synthetic_data_df_with_noise = simulator.add_noise(
@@ -108,12 +108,12 @@ def test_zero_bounded(petab_problem):
108108
# Values with noise that are different in sign to values without noise are
109109
# zeroed.
110110
assert all([
111-
(synthetic_data_df_with_noise['measurement'][neg_indices] <= 0).all(),
112-
(synthetic_data_df_with_noise['measurement'][pos_indices] >= 0).all(),
113-
(synthetic_data_df_with_noise['measurement'][neg_indices] == 0).any(),
114-
(synthetic_data_df_with_noise['measurement'][pos_indices] == 0).any(),
115-
(synthetic_data_df_with_noise['measurement'][neg_indices] < 0).any(),
116-
(synthetic_data_df_with_noise['measurement'][pos_indices] > 0).any(),
111+
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] <= 0).all(),
112+
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] >= 0).all(),
113+
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] == 0).any(),
114+
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] == 0).any(),
115+
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] < 0).any(),
116+
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] > 0).any(),
117117
])
118118

119119

@@ -148,7 +148,7 @@ def _test_add_noise(petab_problem) -> None:
148148
simulator = TestSimulator(petab_problem)
149149
# Set the random seed to ensure predictable tests.
150150
simulator.rng = np.random.default_rng(seed=0)
151-
synthetic_data_df = simulator.simulate()
151+
synthetic_data_df = simulator.simulate(as_measurement=True)
152152

153153
# Generate samples of noisy data
154154
samples = []

0 commit comments

Comments
 (0)