From f0325f34ff602fe791ed438283d75a62ffcf2714 Mon Sep 17 00:00:00 2001 From: Tom Donoghue Date: Sun, 8 Sep 2024 23:21:35 -0400 Subject: [PATCH] fix up tutorials for updates --- tutorials/sim/plot_06_SimParams.py | 2 +- tutorials/sim/plot_07_SimMulti.py | 123 ++++++++++++++++++----------- 2 files changed, 76 insertions(+), 49 deletions(-) diff --git a/tutorials/sim/plot_06_SimParams.py b/tutorials/sim/plot_06_SimParams.py index 8044bc6a..d34dfa79 100644 --- a/tutorials/sim/plot_06_SimParams.py +++ b/tutorials/sim/plot_06_SimParams.py @@ -298,7 +298,7 @@ # Register a group of samplers to the object sim_samplers.register_group_samplers([ ['exp_sampler', 'ap', exp_upd_sampler], - ['osc_sampler', 'osc', osc_upd_sampler] + ['osc_sampler', 'osc', osc_upd_sampler], ]) ################################################################################################### diff --git a/tutorials/sim/plot_07_SimMulti.py b/tutorials/sim/plot_07_SimMulti.py index 21df0f7c..59aab4a1 100644 --- a/tutorials/sim/plot_07_SimMulti.py +++ b/tutorials/sim/plot_07_SimMulti.py @@ -7,7 +7,8 @@ from neurodsp.sim.update import SigIter from neurodsp.sim.aperiodic import sim_powerlaw -from neurodsp.sim.multi import sim_multiple, sim_across_values, sim_from_sampler +from neurodsp.sim.multi import (sim_multiple, sim_from_sampler, + sim_across_values, sim_multi_across_values) from neurodsp.sim.update import create_updater, create_sampler, ParamSampler from neurodsp.plts.time_series import plot_time_series, plot_multi_time_series @@ -29,14 +30,14 @@ ################################################################################################### # -# The output the above function is a :class:~.Simulations object that stores multiple simulated -# signals along with relevant metadata. +# The output the above function is a :class:~.Simulations object that stores multiple +# simulated signals along with relevant metadata. # ################################################################################################### # Check the metadata stored in the simulations object -print(sigs.sim_func, ':', sigs.params) +print(sigs.function, ':', sigs.params) ################################################################################################### @@ -66,14 +67,57 @@ for tsig in sig_iter: plot_time_series(None, tsig) +################################################################################################### +# Simulate From Sampler +# --------------------- +# +# We can also use the :func:`~.sim_from_sampler` function to simulate signals, +# sampling parameter values from a sampler definition. +# + +################################################################################################### + +# Define base set of parameters +params = {'n_seconds' : 5, 'fs' : 250, 'exponent' : None} + +# Create an updater and sampler to sample from +exp_sampler = {create_updater('exponent') : create_sampler([-2, -1, 0])} + +# Create a ParamSampler object +sampler = ParamSampler(params, exp_sampler) + +################################################################################################### + +# Simulate a set of signals from the defined sampler +sampled_sims = sim_from_sampler(sim_powerlaw, sampler, 3) + +################################################################################################### +# +# The output of the above is a :class:~.VariableSimulations object that stores simulations +# across variable simulation parameters, storing the simulated time series as well as the +# simulation parameters for each simulated signal. +# + +################################################################################################### + +# Check some of the metadata stored in the VariableSimulations object +print(sampled_sims.function) +for paramdef in sampled_sims.params: + print(paramdef) + +################################################################################################### + +# Plot the set of sampled simulations +plot_multi_time_series(None, sampled_sims) + ################################################################################################### # Simulate Across Values # ---------------------- # -# Sometimes we may want to simulate signals across a set of parameter values. +# Sometimes we may want to simulate signals across a set defined range of parameter values. # -# To do so, we can use the :func:`~.sim_across_values` function. In doing so, we can define -# a set of parameter definitions and a number of simulations to create per definition. +# To do so, we can use the :func:`~.sim_across_values` function, which takes a definition of +# parameter values to simulate across. # ################################################################################################### @@ -86,72 +130,55 @@ ] # Simulate a set of signals -sims_across_params = sim_across_values(sim_powerlaw, multi_params, 3) +sims_across_params = sim_across_values(sim_powerlaw, multi_params) ################################################################################################### # -# The output of the above is a :class:~.MultiSimulations object that stores sets of simulations -# across different parameters, and relevant metadata. Each set of simulations is stored within -# this object as a :class:~.Simulations object. +# The output of the above is a :class:~.VariableSimulations object that stores simulations +# across varying simulation parameters (same as with the sampled simulations). # ################################################################################################### -# The length of the object is the number of parameter sets -print('# of sets of signals:', len(sims_across_params)) +plot_multi_time_series(None, sims_across_params) ################################################################################################### +# Simulate Multiple Instances Across Values +# ----------------------------------------- # -# In the above, we created a set of parameters per definition, which by default are returned -# in a dictionary. +# Finally, we may want to simulate multiple instances across a set of parameter definitions. +# +# To do so, we can use the :func:`~.sim_multi_across_values` function, which takes a set of +# parameter definitions and a number of simulations to create per definition. # ################################################################################################### -# Plot the simulated signals, accessing signals from each simulation definition -plot_multi_time_series(None, sims_across_params[0]) -plot_multi_time_series(None, sims_across_params[1]) -plot_multi_time_series(None, sims_across_params[2]) +# Simulate a set of signals +n_sims = 3 +sims_multi_across_params = sim_multi_across_values(sim_powerlaw, multi_params, n_sims) ################################################################################################### -# Simulate From Sampler -# --------------------- # -# Finally, we can use the :func:`~.sim_from_sampler` function to simulate signals, sampling -# parameter values from a sampler definition. +# The output of the above is a :class:~.MultiSimulations object that stores sets of simulations +# across different parameters, and relevant metadata. Each set of simulations is stored within +# this object as a :class:~.Simulations object. # ################################################################################################### -# Define base set of parameters -params = {'n_seconds' : 5, 'fs' : 250, 'exponent' : None} - -# Create an updater and sampler to sample from -exp_sampler = {create_updater('exponent') : create_sampler([-2, -1, 0])} - -# Create a ParamSampler object -sampler = ParamSampler(params, exp_sampler) - -################################################################################################### - -# Simulate a set of signals from the defined sampler -sampled_sims = sim_from_sampler(sim_powerlaw, sampler, 3) +# The length of the object is the number of parameter sets +print('# of sets of signals:', len(sims_across_params)) ################################################################################################### # -# The output of the above is a :class:~.SampledSimulations object that stores simulations -# created from sampled simulations, as well as the metadata, including the simulation -# parameters for each simulated signal. +# In the above, we created a set of parameters per definition, which by default are returned +# in a dictionary. # ################################################################################################### -# Check some of the metadata stored in the SampledSimulations object -print(sampled_sims.sim_func) -for paramdef in sampled_sims.params: - print(paramdef) - -################################################################################################### - -# Plot the set of sampled simulations -plot_multi_time_series(None, sampled_sims) +# Plot the simulated signals, accessing signals from each simulation definition +plot_multi_time_series(None, sims_across_params[0]) +plot_multi_time_series(None, sims_across_params[1]) +plot_multi_time_series(None, sims_across_params[2])