Sampler#
- class Sampler(sampling_plan, **kwargs)[source]#
Bases:
object
Generate samples based on a sampling plan. Initiate the class by passing a
do_mpc.sampling.SamplingPlanner
(sampling_plan
) object. The class can be configured to create samples based on the defined cases in thesampling_plan
.The class can be created with optional keyword arguments which are passed to
set_param()
.Configuration and sampling:
(Optional) use
set_param()
to configure the class. Usedata_dir
to choose the save location for the samples.Set the sample generating function with
set_sample_function()
. This function is executed for each of the samples in thesampling_plan
.Use
sample_data()
to generate all samples defined in thesampling_plan
. A new file is written for each sample.Or: Create an individual sample result with
sample_idx()
, where an index (int
) referring to thesampling_plan
determines the sampled case.
Note
By default, the
Sampler
will only create samples that do not already exist in the chosendata_dir
.Example:
sp = do_mpc.sampling.SamplingPlanner() # Plan with two variables alpha and beta: sp.set_sampling_var('alpha', np.random.randn) sp.set_sampling_var('beta', lambda: np.random.randint(0,5)) plan = sp.gen_sampling_plan(n_samples=10) sampler = do_mpc.sampling.Sampler(plan) # Sampler computes the product of two variables alpha and beta # that were created in the SamplingPlanner: def sample_function(alpha, beta): return alpha*beta sampler.set_sample_function(sample_function) sampler.sample_data()
- Parameters
sampling_plan (
list
) –
Methods#
sample_data#
- sample_data(self)#
Sample data after having configured the
Sampler
. No user input is required and the method will iterate through all the items defined in thesampling_plan
(obtained withdo_mpc.sampling.SamplingPlanner
). :rtype:None
Note
Depending on your
sample_function
(set withset_sample_function()
) and the total number of samples, executing this method may take some time.Note
If
sampler.set_param(overwrite = False)
(default) data will only be sampled for instances that do not yet exist.
sample_idx#
- sample_idx(self, idx)#
Sample case based on the index of the sample.
- Parameters
idx (
int
) – Index of thesampling_plan
for which the sample should be created.- Raises
assertion – Index must be between 0 and
n_samples
.assertion – sample_function must be set prior to sampling data.
- Return type
None
set_param#
- set_param(self, **kwargs)#
Configure the
do_mpc.sampling.Sampler
class.Parameters must be passed as pairs of valid keywords and respective argument. For example:
sampler.set_param(overwrite = True)
- Parameters
overwrite (bool) – Should previously created results be overwritten. Default is
False
sample_name (str) – Naming scheme for samples.
save_format (str) – Choose either
pickle
ormat
.print_progress (bool) – Print progress-bar to terminal. Default is
True
.
- Return type
None
set_sample_function#
- set_sample_function(self, sample_function)#
Set sample generating function. The sampling function produces a sample result for each sample definition in the
sampling_plan
and is called in the methodsample_data()
.It is important that the sample function only uses keyword arguments with the same name as previously defined in the
sampling_plan
.Example:
sp = do_mpc.sampling.SamplingPlanner() sp.set_sampling_var('alpha', np.random.randn) sp.set_sampling_var('beta', lambda: np.random.randint(0,5)) sampler = do_mpc.sampling.Sampler(plan) def sample_function(alpha, beta): return alpha*beta sampler.set_sample_function(sample_function)
- Parameters
sample_function (
Callable
[[Union
[FunctionType
,BuiltinMethodType
],Union
[FunctionType
,BuiltinMethodType
]],Union
[FunctionType
,BuiltinMethodType
]]) – Function to create each sample of the sampling plan.- Return type
None
Attributes#
data_dir#
- Sampler.data_dir#
Set the save directory for the results. If the directory does not exist yet, it is created. If the directory is nested all (non-existing) parent folders are also created.
Example:
sampler = do_mpc.sampling.Sampler() sampler.data_dir = './samples/experiment_1/'
This will set the directory to the indicated path. If the path does not exist, all folders are created.