Parameter types and pre-defined classes¶
melissa.server.parameters.ParameterSamplerType¶
melissa.server.parameters.RandomUniform¶
Bases: RandomUniformSamplerMixIn, SobolBaseExperiment
Random uniform sampling experiment that integrates Sobol and uniform sampling methods.
melissa.server.parameters.HaltonGenerator¶
Bases: HaltonSamplerMixIn, SobolBaseExperiment
Deterministic sample generator based on scipy Halton sequence https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.qmc.Halton.html
melissa.server.parameters.LHSGenerator¶
Bases: LatinHypercubeSamplerMixIn, SobolBaseExperiment
Non-deterministic sample generator based on scipy LHS sampling https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.qmc.LatinHypercube.html
Creating a parameter sampler¶
melissa.server.parameters.make_parameter_sampler(sampler_t, **kwargs)
¶
Creates and returns an instance of a parameter sampler based on the specified sampler type.
This function supports both predefined sampler types from ParameterSamplerType and custom
sampler classes passed directly. It instantiates the appropriate sampler class with the
provided keyword arguments.
Parameters:¶
- sampler_t (
Union[ParameterSamplerType, Type[ParameterSamplerClass]]):ParameterSamplerType: An enum value fromParameterSamplerType. (RANDOM_UNIFORM,HALTON,LHS)Type[ParameterSamplerClass]: A predefined or a custom class type to be instantiated (Not an object).
- kwargs: Additional keyword arguments passed to the sampler class constructor.
Returns:¶
BaseExperiment: An instance of the chosen parameter sampler.
Raises:¶
ValueError: Ifsampler_tis not one of the supported enum values or a valid subclass ofBaseExperiement.
Base classes¶
These parent classes can be used for creating a custom parameter sampler.
The inherited server must set the sampler via set_parameter_sampler method.
melissa.server.parameters.BaseExperiment¶
Bases: ABC
A base class for parameter generation strategies for simulations.
- This class serves as the common parent for different methods of sampling simulation
parameters.
The
draw()method, which is implemented by subclasses, is called from the server when creating client scripts to generate parameter values. - The
sample()method samples parameters in their raw form that must be post-processed.
Attributes¶
- nb_params (
int): The number of parameters to be generated. - nb_sims (
int): The number of simulations to be run. - l_bounds (
List[Union[float, int]]): The lower bounds for the parameter values. - u_bounds (
List[Union[float, int]]): The upper bounds for the parameter values. - seed (
int): The seed value for random number generation to ensure reproducibility (Default isNone). - dtype (
DTypeLike): The data type for the generated parameters. (Default isnp.float64) - rng
Generator: A NumPy random number generator initialized with either the given seed or a random seed. - parameters (
MemmapWrapper): The pre-generated parameter values stored as a NumPy array or memmap. - _current_idx (
int): The current index for selecting the next set of parameters.
checkpoint_state()
¶
Saves the current state for checkpointing.
restart_from_checkpoint()
¶
Restores the state from a checkpoint.
__initialize_memmap()
¶
Initializes a numpy.memmap to store the parameters to be sampled in a file such that
we share a common set of attributes across a parallel server.
The sampling rank performs the sampling and writes to a file. Usually, the sampling rank is the last MPI rank but the decision is made at runtime for multi-node studies.
set_seeds()
¶
Sets the random seeds for ensuring reproducibility across experiments.
This method initializes the random seed for,
-Python's random module
- NumPy's random number generator
- Default random number generator (RNG) using NumPy's default_rng.
Behavior¶
- If
self.seedis specified, it sets the same seed acrossrandom, NumPy's random module, andnp.random.default_rngfor consistent results. - If
self.seedis not specified (Noneor0), it initializesself.rngwith a random seed provided by the system.
process_drawn(parameters)
¶
Processes the next set of parameters before generating client scripts.
Parameters:¶
parameters(NDArray): A row of parameters obtained from a pre-generated parameters matrix of shape(nb_sims, nb_params).
Returns¶
Any: Processed parameter compatible with the client scripts.
For example, if your client script requires the inputs in a certain way
Then, process_drawn can be overriden such that it returns
draw(sim_id)
¶
set_parameters(parameters)
¶
Sets the static parameters.
Parameters¶
- parameters (
Union[List, NDArray]): A list or array of parameters to set.
flush_to_disk()
¶
Writes current parameter map to memmap_file on the disk.
base_sample(nb_samples)
abstractmethod
¶
sample(nb_samples)
¶
melissa.server.parameters.SobolBaseExperiment¶
Bases: BaseExperiment
SobolBaseExperiment is an extension of the BaseExperiment class
designed to support Sobol sensitivity analysis. It introduces the concept
of the pick-freeze method for generating samples and supports both first-order
and second-order Sobol indices.
Attributes¶
apply_pick_freeze (bool): Indicates whether to use the pick-freeze method
for sample generation.
second_order (bool): Specifies whether to include second-order Sobol indices
in the pick-freeze matrix generation.
MixIn Classes¶
melissa.server.parameters.RandomUniformSamplerMixIn¶
melissa.server.parameters.QMCSamplerMixIn¶
Mixin class for samplers based on scipy.stats.qmc methods,
such as Halton and Latin Hypercube.
This class defines the sample() method specifically for generating samples
using quasi-Monte Carlo methods, including Halton and Latin Hypercube samplers