parsac.core module

class parsac.core.Calculator(returns: Iterable[str], fn: Callable[[...], tuple[float]], *args: Any, **kwargs: Any)

Bases: object

reset() None
update(name2value: dict[str, float]) None
class parsac.core.Comparison(name: str, runner: Runner, obs_vals: ndarray, sd: float | ndarray | None = None)

Bases: Metric

class parsac.core.Ensemble(**kwargs)

Bases: Experiment

Ensemble simulation

Parameters:

kwargs – Additional keyword arguments to passed to Experiment.

add_job(runner: Runner) None

Add a job that takes parameters as input.

run(*args, **kwargs)

Run the ensemble.

Parameters:
  • *args – Positional arguments to pass to run_async().

  • **kwargs – Keyword arguments to pass to run_async().

async run_async(values: Sequence[Mapping[str, float] | Sequence[float]], work_dirs: Iterable[PathLike[Any] | str])

Run the ensemble asynchronously.

Parameters:
  • values – A sequence of parameter sets to evaluate. Each set can be a dictionary mapping parameter names to values or a sequence of parameter values.

  • work_dirs – A sequence of work directories for each evaluated parameter set. The length of this sequence must match the length of values.

class parsac.core.Experiment(*, db_file: str | Path | None = None, distributed: bool | None = None, max_workers: int | None = None, seed: int | Sequence[int] | None = None, logger: Logger | None = None)

Bases: object

Initialize the experiment.

Parameters:
  • db_file – The file to store the results in. If None, a file with the same name as the script will be created with the suffix “.results.db”.

  • distributed – Whether to run the experiment in distributed mode using MPI. If None, distributed mode is activated if variable MPI4PY_FUTURES_MAX_WORKERS is present in the environment.

  • max_workers – The maximum number of workers to use. If None, it will be set to the number of available CPUs.

  • seed – The random seed to use for sampling parameters.

  • logger – A custom logger to use for logging messages.

add_parameter(parameter: str | Parameter, minimum: float | rv_continuous, maximum: float | None = None, logscale: bool = False) Parameter

Mark a parameter as target for optimization or sensitivity analysis.

Parameters:
  • parameter – The parameter to select. This can be a name of a new parameter or an existing parameter from a runner.

  • minimum – The minimum value of the parameter.

  • maximum – The maximum value of the parameter.

  • logscale – Whether to vary the parameter value on a logarithmic scale.

Returns:

The parameter object that was added to the experiment.

async async_eval(values: Mapping[str, float] | Sequence[float], **kwargs) Mapping[str, Any]

Evaluate the runners with the given parameter values

Parameters:
  • values – The parameter values to evaluate. This can be a dictionary mapping parameter names to values or a sequence of parameter values.

  • kwargs – extra arguments to pass to RunnerPool.__call__().

Returns:

A dictionary with the combined output of all runners.

async batch_eval(values: Sequence[Mapping[str, float] | Sequence[float]], work_dirs: Iterable[PathLike[Any] | str] | str | None = None, return_exceptions: bool = False, **kwargs) list[Mapping[str, Any] | BaseException]

Evaluate multiple parameter sets in parallel.

Parameters:
  • values – A sequence of parameter sets to evaluate. Each set can be a dictionary mapping parameter names to values or a sequence of parameter values.

  • work_dirs – Optional sequence of work directories for each evaluated parameter set, or a format string with a single placeholder that incorporates the parameter set index i, for instance, work_dirs="{i:03}" to place results in directories 000, 001, … If this argument is not provided, temporary directories will be used to store results while evaluating the parameter sets.

  • return_exceptions – If True, exceptions will be returned as part of the result list instead of raising them.

  • kwargs – Extra arguments to pass to RunnerPool.__call__().

Returns:

A list of results, where each result is either a dictionary with the output of the runners for that parameter set, or an exception if return_exceptions is True and an error occurred during evaluation.

eval(values: Mapping[str, float] | Sequence[float]) Mapping[str, Any]
get_parameter_bounds(transform: bool = False) tuple[ndarray, ndarray]

The minimum and maximum value of the parameters.

Parameters:

transform – Whether to apply the forward transform to the bounds.

sample_parameters(n: int) ndarray[tuple[int, int], dtype[float64]]

Sample n parameter sets from the parameter distributions.

async start(record: bool) Iterable[str]

Start the optimization or sensitivity analysis.

This starts all worker processes, performs a single evaluation with the median parameter values, and initializes the recorder. The evaluation serves as check that the runners (model configurations) are working correctly, and also to determine which outputs are available.

stop() None
unpack_parameters(values: Sequence[float]) dict[str, float]

Unpack the vector with parameter values into a dictionary and add any dynamically calculated parameters.

class parsac.core.InitializedParameter(name: str, initial_value: float)

Bases: Parameter

class parsac.core.Metric(name: str, runner: Runner)

Bases: object

class parsac.core.Parameter(name: str)

Bases: object

infer(fn: Callable[[...], float], *args: Any, **kwargs: Any) None

Specify that this parameter will be calculated dynamically by the given function.

Parameters:
  • fn – The function that will return the parameter value.

  • *args – Arguments to the function. Any parameter objects in the list will be replaced by their values when the function is called. If these parameters are themselves calculated dynamically (for instance, if they are also inferred), they will be updated before the function is called.

  • **kwargs – The named arguments to pass to the function. Any parameter objects will be replaced by their values.

Specify that the specified parameters will be calculated dynamically by the given function.

Parameters:
  • outputs – The parameters to calculate.

  • fn – The function that will return a tuple with values for the specified parameters.

  • *args – Arguments to the function. Any parameter objects in the list will be replaced by their values. If these parameters are themselves calculated dynamically, they will be updated before the function is called.

  • **kwargs – The named parameters to pass to the function. Any parameter objects will be replaced by their values.

reset() None

Register that this parameter has changed.This ensures that any dependent parameters are recalculated.

update(name2value: dict[str, float]) None
class parsac.core.Plotter(sharex: Plotter | None = None, sharey: Plotter | None = None)

Bases: object

plot(ax: matplotlib.axes.Axes, logger: Logger) None
class parsac.core.Prior

Bases: object

logpdf(name2value: Mapping[str, float]) float
class parsac.core.Runner(name: str, *, logger: Logger | None = None)

Bases: object

on_shutdown() None
on_start() None
prepare_work_dir(work_dir: Path | None) Tuple[Path, bool]

Prepare a work directory. It will reuse an existing work directory (without emptying it), unless you provide an explicit directory path that is different from the previous call.

class parsac.core.RunnerPool(runners: Mapping[str, Runner], *, distributed: bool | None = None, max_workers: int | None = None, logger: Logger | None = None)

Bases: object

async __call__(name2value: Mapping[str, float], work_dir: PathLike[Any] | str | None = None, **kwargs) dict[str, Any]

Run all runners with the given parameter values.

Parameters:
  • name2value – The parameter values to use for the runners.

  • work_dir – The directory to use for the runners. If not provided, a temporary directory will be used. Note that the directory will be created only if one or more runners need a work directory.

  • **kwargs – Additional keyword arguments to pass to the runners.

active: dict[str, Runner] = {}
property nworkers: int
shutdown()

Shut down the pool and all runners.

class parsac.core.Transform

Bases: object

class parsac.core.UnivariatePrior(parameter: _TargetedParameter)

Bases: Prior

logpdf(name2value: Mapping[str, float]) float