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

Simulator: rename measurement column to simulation #199

Merged
merged 5 commits into from
May 7, 2023
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
10 changes: 10 additions & 0 deletions petab/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def simulate(
self,
noise: bool = False,
noise_scaling_factor: float = 1,
as_measurement: bool = False,
**kwargs
) -> pd.DataFrame:
"""Simulate a PEtab problem, optionally with noise.
Expand All @@ -123,6 +124,9 @@ def simulate(
noise: If True, noise is added to simulated data.
noise_scaling_factor:
A multiplier of the scale of the noise distribution.
as_measurement:
Whether the data column is named :const:`petab.C.MEASUREMENT`
(`True`) or :const:`petab.C.SIMULATION` (`False`).
**kwargs:
Additional keyword arguments are passed to
:meth:`petab.simulate.Simulator.simulate_without_noise`.
Expand All @@ -133,6 +137,12 @@ def simulate(
simulation_df = self.simulate_without_noise(**kwargs)
if noise:
simulation_df = self.add_noise(simulation_df, noise_scaling_factor)

columns = {petab.C.MEASUREMENT: petab.C.SIMULATION}
if as_measurement:
columns = {petab.C.SIMULATION: petab.C.MEASUREMENT}
simulation_df = simulation_df.rename(columns=columns)

return simulation_df

def add_noise(
Expand Down
24 changes: 12 additions & 12 deletions tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ def test_zero_bounded(petab_problem):
(positive if index in pos_indices else np.nan)
for index in range(n_measurements)
]
synthetic_data_df = simulator.simulate().assign(**{
synthetic_data_df = simulator.simulate(as_measurement=True).assign(**{
petab.C.MEASUREMENT: measurements
})
# All measurements are non-zero
assert (synthetic_data_df['measurement'] != 0).all()
assert (synthetic_data_df[MEASUREMENT] != 0).all()
# No measurements are NaN
assert not (np.isnan(synthetic_data_df['measurement'])).any()
assert not (np.isnan(synthetic_data_df[MEASUREMENT])).any()

synthetic_data_df_with_noise = simulator.add_noise(
synthetic_data_df,
)
# Both negative and positive values are returned by default.
assert all([
(synthetic_data_df_with_noise['measurement'] <= 0).any(),
(synthetic_data_df_with_noise['measurement'] >= 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT] <= 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT] >= 0).any(),
])

synthetic_data_df_with_noise = simulator.add_noise(
Expand All @@ -108,12 +108,12 @@ def test_zero_bounded(petab_problem):
# Values with noise that are different in sign to values without noise are
# zeroed.
assert all([
(synthetic_data_df_with_noise['measurement'][neg_indices] <= 0).all(),
(synthetic_data_df_with_noise['measurement'][pos_indices] >= 0).all(),
(synthetic_data_df_with_noise['measurement'][neg_indices] == 0).any(),
(synthetic_data_df_with_noise['measurement'][pos_indices] == 0).any(),
(synthetic_data_df_with_noise['measurement'][neg_indices] < 0).any(),
(synthetic_data_df_with_noise['measurement'][pos_indices] > 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] <= 0).all(),
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] >= 0).all(),
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] == 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] == 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT][neg_indices] < 0).any(),
(synthetic_data_df_with_noise[MEASUREMENT][pos_indices] > 0).any(),
])


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

# Generate samples of noisy data
samples = []
Expand Down