queens.utils package#

Utils.

Modules containing utilities used throughout QUEENS.

Submodules#

queens.utils.ascii_art module#

ASCII art module.

print_banner(message: str = 'QUEENS', output_width: int = 81) None[source]#

Print banner.

Parameters:
  • message – Message in banner

  • output_width – Terminal output width

print_banner_and_description(output_width: int = 81) None[source]#

Print banner and the description.

Parameters:

output_width – Terminal output width

print_bmfia_acceleration(output_width: int = 81) None[source]#

Print BMFIA rocket.

Parameters:

output_width – Terminal output width

print_centered_multiline(string: str, output_width: int = 81) None[source]#

Center every line of a multiline text.

Parameters:
  • string – String to be printed

  • output_width – Terminal output width

print_centered_multiline_block(string: str | FigletString, output_width: int = 81) None[source]#

Print a multiline text in the center as a block.

Parameters:
  • string – String to be printed

  • output_width – Terminal output width

print_classification() None[source]#

Print like a sir as the iterator is classification.

print_crown(output_width: int = 81) None[source]#

Print crown.

Parameters:

output_width – Terminal output width

print_points_iterator(output_width: int = 81) None[source]#

Print points iterator.

Parameters:

output_width – Terminal output width

queens.utils.classifier module#

Classifiers for use in convergence classification.

class ActiveLearningClassifier(n_params: int, classifier_obj: MLPClassifier, batch_size: int, active_sampler_obj: UncertaintySampling | None = None)[source]#

Bases: Classifier

Active learning classifier wrapper.

n_params#

number of parameters of the solver

classifier_obj#

classifier, e.g. sklearn.svm.SVR

active_sampler_obj#

query strategy from skactiveml.pool, e.g. UncertaintySampling

is_active = True#
train(x_train: ndarray, y_train: ndarray) ndarray[source]#

Train the underlying _clf classifier.

Parameters:
  • x_train – array with training samples, size: (n_samples, n_params)

  • y_train – vector with corresponding training labels, size: (n_samples)

Returns:

sample indices in x_train to query next

class Classifier(n_params: int, classifier_obj: SklearnClassifier)[source]#

Bases: object

Classifier wrapper.

n_params#

number of parameters of the solver

classifier_obj#

classifier, e.g. sklearn.svm.SVR

is_active = False#
load(path: str, file_name: str) None[source]#

Load pickled classifier.

Parameters:
  • path – Path to export the classifier

  • file_name – File name without suffix

predict(x_test: ndarray) ndarray[source]#

Perform prediction on given parameter combinations.

Parameters:

x_test – array of parameter combinations (n_samples, n_params)

Returns:

prediction value or vector (n_samples)

train(x_train: ndarray, y_train: ndarray) None[source]#

Train the underlying _clf classifier.

Parameters:
  • x_train – array with training samples, size: (n_samples, n_params)

  • y_train – vector with corresponding training labels, size: (n_samples)

queens.utils.cli module#

Command Line Interface utils collection.

build_html_coverage_report() None[source]#

Build html coverage report.

cli_logging(func: Callable) Callable[source]#

Decorator to create logger for CLI function.

Parameters:

func – Function that is to be decorated

gather_metadata_and_write_to_csv(*args: Any, **kwargs: Any) Any[source]#
get_cli_options(args: Sequence[str]) tuple[Path, Path, bool][source]#

Get input file path, output directory and debug from args.

Parameters:

args – cli arguments

Returns:
  • Path object to input file

  • Path object to the output directory

  • True if debug mode is to be used

inject_template_cli(*args: Any, **kwargs: Any) Any[source]#
print_greeting_message(*args: Any, **kwargs: Any) Any[source]#
print_pickle_data_cli(*args: Any, **kwargs: Any) Any[source]#
remove_html_coverage_report() None[source]#

Remove html coverage report files.

str_to_bool(value: str) bool[source]#

Convert string to boolean for cli commands.

Parameters:

value – String to convert to a bool

Returns:

Bool of the string

queens.utils.collection module#

Utils to collect data during iterative processes.

class CollectionObject(*field_names: str)[source]#

Bases: object

Collection object which stores data.

This object can be indexed by iteration i: collection_object[i] but also using the collected fields collection_object.field1.

add(**field_names_and_values: dict) None[source]#

Add data to the object.

This function can be called with one or multiple fields, i.e.: collection_object.add(field1=value1) or collection_object.add(field1=value1, field2=value2). An error is raised if one tries to add data to a field for a new iteration before all fields are filled for the current iteration.

classmethod create_collection_object_from_dict(data_dict: dict) CollectionObject[source]#

Create collection item from dict.

Parameters:

data_dict – Dictionary with values to be stored in this object

Returns:

Collection object created from dict

items() Iterable[source]#

Items of the current object.

This allows to use the object like a dict.

Returns:

Items of the collection object

keys() Iterable[source]#

Keys, i.e. field names of the current object.

This allows to use the object like a dict.

Returns:

Keys of the collection object

to_dict() dict[source]#

Create a dictionary from the collection object.

Returns:

Dictionary with all data

values() Iterable[source]#

Values of the current object.

This allows to use the object like a dict.

Returns:

Values of the collection object

queens.utils.config_directories module#

Configuration of folder structure of QUEENS experiments.

base_directory() Path[source]#

Holds all queens experiments.

The base directory holds individual folders for each queens experiment on the compute machine. Per default, it is located and structured as follows:

$HOME/queens-experiments
  ├── experiment_name_1
  ├── experiment_name_2

For remote cluster test runs, a separate base directory structure is used:

$HOME/queens-tests
  ├── pytest-0
  │   ├── test_name_1
  │   └── test_name_2
  ├── pytest-1
      ├── test_name_1
      └── test_name_2
create_directory(dir_path: str | Path) None[source]#

Create a directory either local or remote.

Parameters:

dir_path – Directory to create

current_job_directory(experiment_dir: Path, job_id: int) Path[source]#

Directory of the latest submitted job.

Parameters:
  • experiment_dir – Experiment directory

  • job_id – Job ID of the current job

Returns:

Path to the current job directory.

experiment_directory(experiment_name: str, experiment_base_directory: str | Path | None = None) Path[source]#

Directory for data of a specific experiment on the computing machine.

If no experiment_base_directory is provided, base_directory() is used as default.

Parameters:
  • experiment_name – Experiment name

  • experiment_base_directory – Base directory for the experiment directory

Returns:

Experiment directory

job_dirs_in_experiment_dir(experiment_dir: Path | str) list[Path][source]#

Get job directories in experiment_dir.

Parameters:

experiment_dir – Path with the job dirs

Returns:

List with job_dir paths

queens.utils.configure_tensorflow module#

Utils related to tensorflow and friends.

configure_keras(tf_keras: ModuleType) None[source]#

Configure tf keras.

Parameters:

tf_keras – The module configuration

configure_tensorflow(tensorflow: ModuleType) None[source]#

Configure tensorflow.

Parameters:

tensorflow – The module to configure

queens.utils.exceptions module#

Custom exceptions.

exception CLIError[source]#

Bases: QueensException

QUEENS exception for CLI input.

exception FileTypeError[source]#

Bases: QueensException

Exception for wrong file types.

exception InvalidOptionError[source]#

Bases: QueensException

Custom error class for invalid options during QUEENS runs.

classmethod construct_error_from_options(valid_options: dict | list, desired_option: str, additional_message: str = '') InvalidOptionError[source]#

Construct invalid option error from the valid and desired options.

Parameters:
  • valid_options – List of valid option keys

  • desired_option – Key of desired option

  • additional_message – Additional message to pass (default is None)

Returns:

InvalidOptionError

exception QueensException[source]#

Bases: Exception

QUEENS exception.

exception SubprocessError[source]#

Bases: QueensException

Custom error class for the QUEENS subprocess wrapper.

classmethod construct_error_from_command(command: str, command_output: str, error_message: str, additional_message: str | None = '') SubprocessError[source]#

Construct a Subprocess error from a command and its outputs.

Parameters:
  • command – Command used that raised the error

  • command_output – Command output

  • error_message – Error message of the command

  • additional_message – Additional message to pass

Returns:

SubprocessError

queens.utils.experimental_data_reader module#

Module to read experimental data.

class ExperimentalDataReader(data_processor: DataProcessor | None = None, output_label: str | None = None, coordinate_labels: list[str] | None = None, time_label: str | None = None, file_name_identifier: str | None = None, csv_data_base_dir: str | Path = '')[source]#

Bases: object

Reader for experimental data.

output_label#

Label that marks the output quantity in the csv file

coordinate_labels#

List of column-wise coordinate labels in csv files

time_label#

Name of the time variable in csv file

file_name#

File name of experimental data

Type:

str

base_dir#

Path to base directory containing experimental data

Type:

Path

data_processor#

data processor for experimental data

get_experimental_data() tuple[ndarray, ndarray | None, ndarray | None, dict[str, Any], str | None, list[str] | None, str | None][source]#

Load experimental data.

Returns:
  • Column-vector of model outputs which correspond row-wise to observation coordinates

  • Matrix with observation coordinates. One row corresponds to one coordinate point

  • Unique vector of observation times

  • Dictionary containing the experimental data

  • Name of the time variable in csv file

  • List of column-wise coordinate labels in csv files

  • Label that marks the output quantity in the csv file

queens.utils.fd_jacobian module#

Calculate finite-difference-based approximation of Jacobian.

Note

Implementation is heavily based on the scipy.optimize._numdiff module. We do NOT support complex scheme ‘cs’ and sparsity.

The motivation behind this reimplementation is to enable the parallel computation of all function values required for the finite difference scheme.

In theory, when computing the Jacobian of function at a specific position via a specific finite difference scheme, all positions where the function needs to be evaluated (the perturbed positions) are known immediately/at once, because they do not depend on each other. The evaluation of the function at these perturbed positions may consequently be done “perfectly” (embarrassingly) parallel.

Most implementations of finite-difference-based approximations do not exploit this inherent potential for parallel evaluations because for cheap functions, the communication overhead is too high. For expensive functions, the exploitation ensures significant speed up.

compute_step_with_bounds(x0: ndarray, method: Literal['2-point', '3-point'], rel_step: float | ndarray | None, bounds: tuple | ndarray | None) tuple[ndarray, ndarray][source]#

Compute step sizes of finite difference scheme adjusted to bounds.

Parameters:
  • x0 – Point at which the derivative shall be evaluated

  • method

    Finite difference method to use:

    • 2-point:

      use the first order accuracy forward or backward difference

    • 3-point:

      use central difference in interior points and the second order accuracy forward or backward difference near the boundary

  • rel_step – Relative step size to use. The absolute step size is computed as h = rel_step * sign(x0) * max(1, abs(x0)), possibly adjusted to fit into the bounds. For method=’3-point’ the sign of h is ignored. If None (default) then step is selected automatically, see Notes.

  • bounds – Lower and upper bounds on independent variables. Defaults to no bounds. Each bound must match the size of x0 or be a scalar, in the latter case the bound will be the same for all variables. Use it to limit the range of function evaluation.

Returns:
  • Adjusted step sizes

  • Whether to switch to one-sided scheme due to closeness to bounds. Informative only for – 3-point method

fd_jacobian(f0: ndarray, f_perturbed: ndarray, dx: ndarray, use_one_sided: ndarray, method: Literal['2-point', '3-point']) ndarray[source]#

Calculate finite difference approximation of Jacobian of f at x0.

The necessary function evaluation have been pre-calculated and are supplied via f0 and the f_perturbed vector. Each row in f_perturbed corresponds to a function evaluation. The shape of f_perturbed depends heavily on the chosen finite difference scheme (method) and therefore the pre-calculation of f_perturbed and dx has to be consistent with the requested method.

Supported methods: * 2-point: a one sided scheme by definition * 3-point: more exact but needs twice as many function evaluations

Note: The implementation is supposed to remain very closed to scipy._numdiff.approx_derivative.

Parameters:
  • f0 – Function value at x0, f0=f(x0)

  • f_perturbed – Perturbed function values

  • dx – Deltas of the input variables

  • use_one_sided – Whether to switch to one-sided scheme due to closeness to bounds; informative only for 3-point method

  • method – Which scheme was used to calculate the perturbed function values and deltas

Returns:

Jacobian of the underlying model at x0.

get_positions(x0: ndarray, method: Literal['2-point', '3-point'], rel_step: float | ndarray | None, bounds: tuple | ndarray | None) tuple[ndarray, ndarray, ndarray][source]#

Compute all positions needed for the finite difference approximation.

The Jacobian is defined for a vector-valued function at a given position.

Note: The implementation is supposed to remain very closed to scipy._numdiff.approx_derivative.

Parameters:
  • x0 – Position or sample at which the Jacobian shall be computed.

  • method – Finite difference method that is used to compute the Jacobian.

  • rel_step – Finite difference step size.

  • bounds – Lower and upper bounds on independent variables. Defaults to no bounds. Each bound must match the size of x0 or be a scalar, in the latter case the bound will be the scalar, in the latter case the bound will be the same for all variables. Use it to limit the range of function evaluation.

Returns:
  • List with additional stencil positions that are necessary to calculate the finite difference – approximation to the gradient

  • Delta between positions used to approximate Jacobian

queens.utils.gpflow_transformations module#

Utils for gpflow.

init_scaler(unscaled_data: ndarray) tuple[StandardScaler, ndarray][source]#

Initialize StandardScaler and scale data.

Standardize features by removing the mean and scaling to unit variance: \(scaled\_data = \frac{unscaled\_data - mean}{std}\)

Parameters:

unscaled_data – Unscaled data

Returns:
  • Standard scaler

  • Scaled data

set_transform_function(data: Parameter, transform: Bijector) Parameter[source]#

Set transform function.

Parameters:
  • data – Data to be transformed

  • transform – Transform function

Returns:

Parameter with transform

queens.utils.imports module#

Import utils.

class LazyLoader(module_name: str)[source]#

Bases: object

Lazy loader for modules that take long to load.

Inspired from https://stackoverflow.com/a/78312617

extract_type_checking_imports(file_path: str) dict[source]#

Extract imports inside TYPE_CHECKING blocks from file.

Parameters:

file_path – Path to the file

Returns:

A dict mapping class names to their source modules.

get_module_attribute(path_to_module: str | Path, function_or_class_name: str) Callable[source]#

Load function from python file by path.

Parameters:
  • path_to_module – Path to file

  • function_or_class_name – Name of the function

Returns:

Function or class from the module

get_module_class(module_options: dict, valid_types: dict, module_type_specifier: str = 'type') Any[source]#

Return module class defined in config file.

Parameters:
  • module_options – Module options

  • valid_types – Dict of valid types with corresponding module paths and class names

  • module_type_specifier – Specifier for the module type

Returns:

Class from the module

import_class_from_class_module_map(name: str, class_module_map: dict, package: str | None = None) Any[source]#

Import class from class_module_map.

Parameters:
  • name – Name of the class.

  • class_module_map – Class to module mapping.

  • package (str, opt) – Package name (only necessary if import path is relative)

Returns:

class (obj) – Class object.

queens.utils.injector module#

Injector module.

The module supplies functions to inject parameter values into a template text file.

inject(params: dict, template_path: str | Path, output_file: str | Path, strict: bool = True) None[source]#

Function to insert parameters into file template and write to file.

Parameters:
  • params – Dict with parameters to inject

  • template_path – Path to template

  • output_file – Name of output file with injected parameters

  • strict – Raises exception if mismatch between provided and required parameters

inject_in_template(params: dict, template: str, output_file: str | Path, strict: bool = True) None[source]#

Function to insert parameters into file template and write to file.

Parameters:
  • params – Dict with parameters to inject

  • template – Template (str)

  • output_file – Name of output file with injected parameters

  • strict – Raises exception if mismatch between provided and required parameters

render_template(params: dict, template: str, strict: bool = True) str[source]#

Function to insert parameters into a template.

Parameters:
  • params – Dict with parameters to inject

  • template – Template file as string

  • strict – Raises exception if required parameters from the template are missing

Returns:

injected template

queens.utils.io module#

Utils for input/output handling.

load_input_file(input_file_path: Path) dict[source]#

Load inputs from file by path.

Parameters:

input_file_path – Path to the input file

Returns:

Options in the input file.

load_pickle(file_path: Path) dict[source]#

Load a pickle file directly from path.

Parameters:

file_path – Path to pickle-file

Returns:

Data in the pickle file

load_result(path_to_result_file: Path) Any[source]#

Load QUEENS results.

Parameters:

path_to_result_file – Path to results

Returns:

Results

print_pickled_data(file_path: Path) None[source]#

Print a table of the data within a pickle file.

Only goes one layer deep for dicts. This is similar to python -m pickle file_path but makes it a single command and fancy prints.

Parameters:

file_path – Path to pickle-file

read_file(file_path: Path | str) str[source]#

Function to read in a file.

Parameters:

file_path – Path to file

Returns:

Read-in file

to_dict_with_standard_types(obj: Any) Any[source]#

Convert dictionaries to dictionaries with python standard types only.

Parameters:

obj – Dictionary to convert

Returns:

Dictionary with standard types

write_to_csv(output_file_path: Path, data: ndarray, delimiter: str = ',') None[source]#

Write a simple csv file.

Write data out to a csv-file. Nothing fancy, at the moment, only now header line or index column is supported just pure data.

Parameters:
  • output_file_path – Path to the file the data should be written to

  • data – Data that should be written to the csv file.

  • delimiter – Delimiter to separate individual data. Defaults to comma delimiter.

queens.utils.iterative_averaging module#

Iterative averaging utils.

class ExponentialAveraging(coefficient: float)[source]#

Bases: IterativeAveraging

Exponential averaging.

\(x^{(0)}_{avg}=x^{(0)}\)

\(x^{(j)}_{avg}= \alpha x^{(j-1)}_{avg}+(1-\alpha)x^{(j)}\)

Is also sometimes referred to as exponential smoothing.

coefficient#

Coefficient in (0,1) for the average.

average_computation(new_value: ndarray | floating | int | float) ndarray | floating | int | float[source]#

Compute the exponential average.

Parameters:

new_value – New value to update the average.

Returns:

Returns the current average

class IterativeAveraging[source]#

Bases: object

Base class for iterative averaging schemes.

current_average#

Current average value.

new_value#

New value for the averaging process.

rel_l1_change#

Relative change in L1 norm of the average value.

rel_l2_change#

Relative change in L2 norm of the average value.

abstract average_computation(new_value: ndarray | floating | int | float) ndarray | floating[source]#

Here, the averaging approach is implemented.

update_average(new_value: ndarray | floating | int | float) ndarray | floating[source]#

Compute the actual average.

Parameters:

new_value – New observation for the averaging

Returns:

Current average value

class MovingAveraging(num_iter_for_avg: int)[source]#

Bases: IterativeAveraging

Moving averages.

\(x^{(j)}_{avg}=\frac{1}{k}\sum_{i=0}^{k-1}x^{(j-i)}\)

where \(k-1\) is the number of values from previous iterations that are used

num_iter_for_avg#

Number of samples in the averaging window

data#

Data used to compute the average

average_computation(new_value: ndarray | floating | int | float) ndarray | floating[source]#

Compute the moving average.

Parameters:

new_value – New value to update the average

Returns:

The current average

class PolyakAveraging[source]#

Bases: IterativeAveraging

Polyak averaging.

\(x^{(j)}_{avg}=\frac{1}{j}\sum_{i=0}^{j}x^{(j)}\)

iteration_counter#

Number of samples.

Type:

float

sum_over_iter#

Sum over all samples.

Type:

np.array

average_computation(new_value: ndarray | floating | int | float) ndarray | floating[source]#

Compute the Polyak average.

Parameters:

new_value – New value to update the average

Returns:

Returns the current average

l1_norm(vector: ndarray | floating | int | float, averaged: bool = False) float | floating[source]#

Compute the L1 norm of the vector.

Parameters:
  • vector – Vector

  • averaged – If enabled, the norm is divided by the number of components

Returns:

L1 norm of the vector

l2_norm(vector: ndarray | floating | int | float, averaged: bool = False) float | floating[source]#

Compute the L2 norm of the vector.

Parameters:
  • vector – Vector

  • averaged – If enabled the norm is divided by the square root of the number of components

Returns:

L2 norm of the vector

relative_change(old_value: ndarray | floating | int | float, new_value: ndarray | floating | int | float, norm: Callable) float | floating[source]#

Compute the relative change of the old and new value for a given norm.

Parameters:
  • old_value – Old values

  • new_value – New values

  • norm – Function to compute a norm

Returns:

Relative change

queens.utils.jax_minimize_wrapper module#

A collection of helper functions for optimization with JAX.

Taken from https://gist.github.com/slinderman/24552af1bdbb6cb033bfea9b2dc4ecfd

minimize(fun: Callable, x0: Any, method: str | None = None, args: tuple = (), bounds: Sequence | Bounds | None = None, constraints: dict | LinearConstraint | NonlinearConstraint | list[dict] | list[LinearConstraint] | list[NonlinearConstraint] = (), tol: float | None = None, callback: Callable | None = None, options: dict | None = None) OptimizeResult[source]#

A simple wrapper for scipy.optimize.minimize using JAX.

Parameters:
  • fun – The objective function to be minimized, written in JAX code so that it is automatically differentiable. It is of type, `fun: x, *args -> float` where x is a PyTree and args is a tuple of the fixed parameters needed to completely specify the function.

  • x0 – Initial guess represented as a JAX PyTree.

  • args – Extra arguments passed to the objective function and its derivative. Must consist of valid JAX types; e.g. the leaves of the PyTree must be floats. _The remainder of the keyword arguments are inherited from scipy.optimize.minimize, and their descriptions are copied here for convenience._

  • method

    Type of solver. Should be one of - ‘Nelder-Mead’ (see here) - ‘Powell’ (see here) - ‘CG’ (see here) - ‘BFGS’ (see here) - ‘Newton-CG’ (see here) - ‘L-BFGS-B’ (see here) - ‘TNC’ (see here) - ‘COBYLA’ (see here) - ‘SLSQP’ (see here) - ‘trust-constr’(see here) - ‘dogleg’ (see here) - ‘trust-ncg’ (see here) - ‘trust-exact’ (see here) - ‘trust-krylov’ (see here) - custom - a callable object (added in version 0.14.0),

    see below for description.

    If not given, chosen to be one of BFGS, L-BFGS-B, SLSQP, depending on if the problem has constraints or bounds.

  • bounds

    Bounds on variables for L-BFGS-B, TNC, SLSQP, Powell, and trust-constr methods. There are two ways to specify the bounds:

    1. Instance of Bounds class.

    2. Sequence of (min, max) pairs for each element in x.

    None is used to specify no bounds. Note that in order to use bounds you will need to manually flatten them in the same order as your inputs x0.

  • constraints

    Constraints definition (only for COBYLA, SLSQP and trust-constr). Constraints for ‘trust-constr’ are defined as a single object or a list of objects specifying constraints to the optimization problem. Constraints for COBYLA, SLSQP are defined as a list of dictionaries. Each dictionary with fields:

    typestr

    Constraint type: ‘eq’ for equality, ‘ineq’ for inequality.

    funcallable

    The function defining the constraint.

    jaccallable, optional

    The Jacobian of fun (only for SLSQP).

    argssequence, optional

    Extra arguments to be passed to the function and Jacobian.

    Equality constraint means that the constraint function result is to be zero whereas inequality means that it is to be non-negative. Note that COBYLA only supports inequality constraints. Note that in order to use constraints you will need to manually flatten them in the same order as your inputs x0.

  • tol – Tolerance for termination. For detailed control, use solver-specific options.

  • options

    A dictionary of solver options. All methods accept the following generic options:
    maxiterint

    Maximum number of iterations to perform. Depending on the method each iteration may use several function evaluations.

    dispbool

    Set to True to print convergence messages.

    For method-specific options, see show_options().

  • callback – Called after each iteration. For ‘trust-constr’ it is a callable with the signature: callback(xk, OptimizeResult state) -> bool where xk is the current parameter vector represented as a PyTree, and state is an OptimizeResult object, with the same fields as the ones from the return. If callback returns True the algorithm execution is terminated. For all the other methods, the signature is: `callback(xk)` where xk is the current parameter vector, represented as a PyTree.

Returns:

The optimization result represented as a ``OptimizeResult`` object.

Important attributes are:

x: the solution array, represented as a JAX PyTree success: a Boolean flag indicating if the optimizer exited successfully message: describes the cause of the termination.

See scipy.optimize.OptimizeResult for a description of other attributes.

queens.utils.logger_settings module#

Logging in QUEENS.

class LogFilter(level: int)[source]#

Bases: Filter

Filters (lets through) all messages with level <= LEVEL.

level#

Logging level

filter(record: LogRecord) bool[source]#

Filter the logging record.

Parameters:

record – Logging record object

Returns:

Filter logging record

class NewLineFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]#

Bases: Formatter

Formatter splitting multiline messages into single line messages.

A logged message that consists of more than one line - contains a new line char - is split into multiple single line messages that all have the same format. Without this the overall format of the logging is broken for multiline messages.

format(record: LogRecord) str[source]#

Override format function.

Parameters:

record – Logging record object

Returns:

Logged message in supplied format split into single lines

log_init_args(method: Callable[[P], None]) Callable[[P], None][source]#

Log arguments of __init__ method.

Parameters:

method – __init__ method

Returns:

Decorated __init__ method

reset_logging() None[source]#

Reset loggers.

This is only needed during testing, as otherwise the loggers are not destroyed resulting in the same output multiple time. This is taken from:

https://stackoverflow.com/a/56810619

setup_basic_logging(log_file_path: ~pathlib.Path, logger: ~logging.Logger = <Logger queens (DEBUG)>, debug: bool = False) None[source]#

Setup basic logging.

Parameters:
  • log_file_path – Path to the log-file

  • logger – Logger instance that should be set up

  • debug – Indicates debug mode and controls level of logging

setup_cli_logging(debug: bool = False) None[source]#

Set up logging for CLI utils.

Parameters:

debug – Indicates debug mode and controls level of logging

setup_cluster_logging() None[source]#

Setup cluster logging.

setup_file_handler(logger: Logger, log_file_path: Path) None[source]#

Set up a file handler.

Parameters:
  • logger – Logger object to add the stream handler to

  • log_file_path – Path of the logging file

setup_logger(logger: ~logging.Logger = <Logger queens (DEBUG)>, debug: bool = False) Logger[source]#

Set up the main QUEENS logger.

Parameters:
  • logger – Logger instance that should be set up

  • debug – Indicates debug mode and controls level of logging

Returns:

QUEENS logger object

setup_stream_handler(logger: Logger) None[source]#

Set up a stream handler.

Parameters:

logger – Logger object to add the stream handler to

queens.utils.mcmc module#

Collection of utils for Markov Chain Monte Carlo algorithms.

mh_select(log_acceptance_probability: ndarray, current_sample: ndarray, proposed_sample: ndarray) tuple[ndarray, ndarray][source]#

Perform Metropolis-Hastings selection.

The Metropolis-Hastings algorithm is used in Markov Chain Monte Carlo (MCMC) methods to accept or reject a proposed sample based on the log of the acceptance probability. This function compares the acceptance probability with a random number between 0 and 1 to decide if each proposed sample should replace the current sample. If the random number is smaller than the acceptance probability, the proposed sample is accepted. The function further checks whether the log_acceptance_probability is finite. If it is infinite or NaN, the function will not accept the respective proposed sample.

Parameters:
  • log_acceptance_probability – Logarithm of the acceptance probability for each sample. This represents the log of the ratio of the probability densities of the proposed sample to the current sample.

  • current_sample – The current sample values from the MCMC chain.

  • proposed_sample – The proposed sample values to be considered for acceptance.

Returns:
  • The sample values selected after the Metropolis-Hastings step. – If the proposed sample is accepted, it will be returned; otherwise, the current sample is returned.

  • A bool array indicating whether each proposed sample was accepted.

tune_scale_covariance(scale_covariance: ndarray | float, accept_rate: ndarray | float) ndarray[source]#

Adjust the covariance scaling factor based on the acceptance rate.

This function tunes the covariance scaling factor used in Metropolis-Hastings or similar MCMC algorithms based on the observed acceptance rate of proposed samples. The goal is to maintain an acceptance rate within the range of 20% to 50%, which is considered optimal for many MCMC algorithms. The covariance scaling factor is adjusted according to the following rules:

Acceptance Rate

Variance adaptation factor

<0.001

x 0.1

<0.05

x 0.5

<0.2

x 0.9

>0.5

x 1.1

>0.75

x 2

>0.95

x 10

Reference: [1]: pymc-devs/pymc

Parameters:
  • scale_covariance – The current covariance scaling factor for the proposal distribution.

  • accept_rate – The observed acceptance rate of the proposed samples. This value should be between 0 and 1.

Returns:

The updated covariance scaling factor adjusted according to the acceptance rate.

queens.utils.metadata module#

Metadata objects.

class SimulationMetadata(job_id: int, inputs: dict, job_dir: Path)[source]#

Bases: object

Simulation metadata object.

This objects holds metadata, times code sections and exports them to yaml.

job_id#

Id of the job

inputs#

Parameters for this job

file_path#

Path to export the metadata

Type:

pathlib.Path

timestamp#

Timestamp of the object creation

Type:

str

outputs#

Results obtain by the simulation

Type:

tuple

times#

Wall times of code sections

Type:

dict

export() None[source]#

Export the object to human readable format.

time_code(code_section_name: str) Iterator[None][source]#

Timer some code section.

This method allows us to time not only the runtime of the simulation, but also subparts.

Parameters:

code_section_name – Name for this code section

to_dict() dict[str, Any][source]#

Create dictionary from object.

Returns:

Dictionary of the metadata object

get_metadata_from_experiment_dir(experiment_dir: Path | str) Iterator[Any][source]#

Get metadata from experiment_dir.

To keep memory usage limited, this is implemented as a generator.

Parameters:

experiment_dir – Path with the job dirs

Yields:

Metadata of a job

write_metadata_to_csv(experiment_dir: Path | str, csv_path: Path | None = None) None[source]#

Gather and write job metadata to csv.

Parameters:
  • experiment_dir – Path with the job dirs

  • csv_path – Path to export the csv file

queens.utils.numpy_array module#

Numpy array utils.

at_least_2d(arr: ndarray) ndarray[source]#

View input array as array with at least two dimensions.

Parameters:

arr – Input array

Returns:

View of input array with at least two dimensions

at_least_3d(arr: ndarray) ndarray[source]#

View input array as array with at least three dimensions.

Parameters:

arr – Input array

Returns:

View of input array with at least three dimensions

extract_block_diag(array: ndarray, block_size: int) ndarray[source]#

Extract block diagonals of square 2D Array.

Parameters:
  • array – Square 2D array

  • block_size – Block size

Returns:

3D Array containing block diagonals

queens.utils.numpy_linalg module#

Numpy linear algebra utils.

add_nugget_to_diagonal(matrix: ndarray, nugget_value: generic | float) ndarray[source]#

Add a small value to diagonal of matrix.

The nugget value is only added to diagonal entries that are smaller than the nugget value.

Parameters:
  • matrix – Matrix

  • nugget_value – Small nugget value to be added

Returns:

Manipulated matrix

safe_cholesky(matrix: ndarray, jitter_start_value: generic | float = 1e-10) ndarray[source]#

Numerically stable Cholesky decomposition.

Compute the Cholesky decomposition of a matrix. Numeric stability is increased by sequentially adding a small term to the diagonal of the matrix.

Parameters:
  • matrix – Matrix to be decomposed

  • jitter_start_value – Starting value to be added to the diagonal

Returns:

Lower-triangular Cholesky factor of matrix

queens.utils.path module#

Path utilities for QUEENS.

check_if_path_exists(path: Path, error_message: str = '') bool[source]#

Check if a path exists.

Parameters:
  • path – Path to be checked

  • error_message – If an additional message is desired

Returns:

`True` if the path exists, `False` otherwise.

Raises:

FileNotFoundError – If the path does not exist.

create_folder_if_not_existent(path: Path | str) Path[source]#

Create folder if not existent.

Parameters:

path – Path to be created

Returns:

Path object

is_empty(paths: str | Path | Sequence) bool[source]#

Check whether paths is empty.

Parameters:

paths – (List of) path-like objects

relative_path_from_queens_source(relative_path: str) Path[source]#

Create relative path from src/queens/.

For example, to create src/queens/folder/file.A, call relative_path_from_queens_source(“folder/file.A”) .

Parameters:

relative_path – Path starting from src/queens/

Returns:

Absolute path to the file

relative_path_from_root(relative_path: str) Path[source]#

Create relative path from root directory.

As an example to create: src/queens/folder/file.A .

Call relative_path_from_root(“src/queens/folder/file.A”) .

Parameters:

relative_path – Path starting from the root directory

Returns:

Absolute path to the file

queens.utils.pdf_estimation module#

Kernel density estimation (KDE).

Estimation of the probability density function based on samples from the distribution.

estimate_bandwidth_for_kde(samples: ndarray, min_samples: float, max_samples: float, kernel: str = 'gaussian') generic[source]#

Estimate optimal bandwidth for kde of pdf.

Parameters:
  • samples – Samples for which to estimate pdf

  • min_samples – Smallest value

  • max_samples – Largest value

  • kernel – Kernel type

Returns:

Estimate for optimal kernel bandwidth

estimate_pdf(samples: ndarray, kernel_bandwidth: float, support_points: ndarray | None = None, kernel: str = 'gaussian') tuple[ndarray, ndarray][source]#

Estimate pdf using kernel density estimation.

Parameters:
  • samples – Samples for which to estimate pdf

  • kernel_bandwidth – Kernel width to use in kde

  • support_points – Points where to evaluate pdf

  • kernel – Kernel type

Returns:

PDF estimate at support points

queens.utils.plot_outputs module#

Collection of plotting capabilities for probability distributions.

plot_cdf(cdf_estimate: dict, support_points: ndarray, bayes: bool = False) None[source]#

Create cdf plot based on passed data.

Parameters:
  • cdf_estimate – Estimate of cdf at supporting points

  • support_points – Supporting points

  • bayes – Do we want to plot confidence intervals

plot_icdf(icdf_estimate: dict, bayes: bool = False) None[source]#

Create icdf plot based on passed data.

Parameters:
  • icdf_estimate – Estimate of icdf at supporting points

  • bayes – Do we want to plot confidence intervals

plot_pdf(pdf_estimate: dict, support_points: ndarray, bayes: bool = False) None[source]#

Create pdf plot based on passed data.

Parameters:
  • pdf_estimate – Estimate of pdf at supporting points

  • support_points – Supporting points

  • bayes – Do we want to plot confidence intervals

queens.utils.pool module#

Pool utils.

create_pool(number_of_workers: int) ProcessPool | None[source]#

Create pathos Pool from number of workers.

Parameters:

number_of_workers – Number of parallel evaluations

Returns:

Pathos multiprocessing pool

queens.utils.printing module#

Print utils.

get_str_table(name: str, print_dict: dict, use_repr: bool = False) str[source]#

Function to get table to be used in __str__ methods.

Parameters:
  • name – Object name

  • print_dict – Dict containing labels and values to print

  • use_repr – If true, use repr() function to obtain string representations of objects

Returns:

Table to print

queens.utils.process_outputs module#

Collection of utility functions for post-processing.

do_processing(output_data: dict, output_description: dict) dict[source]#

Do actual processing of output.

Parameters:
  • output_data – Dictionary containing model output

  • output_description – Dictionary describing desired output quantities

Returns:

Dictionary with processed results

estimate_bandwidth_for_kde(samples: ndarray, min_samples: float, max_samples: float) float[source]#

Estimate optimal bandwidth for kde of pdf.

Parameters:
  • samples – Samples for which to estimate pdf

  • min_samples – Smallest value

  • max_samples – Largest value

Returns:

Estimate for optimal kernel bandwidth

estimate_cdf(output_data: dict, support_points: ndarray, bayesian: bool) dict[source]#

Compute estimate of CDF based on provided sampling data.

Parameters:
  • output_data – Dictionary with output data

  • support_points – Points where to evaluate cdf

  • bayesian – Compute confidence intervals etc.

Returns:

Dictionary with cdf estimates

estimate_cov(output_data: dict) ndarray[source]#

Estimate covariance based on standard unbiased estimator.

Parameters:

output_data – Dictionary with output data

Returns:

Unbiased covariance estimate

estimate_icdf(output_data: dict, bayesian: bool) dict[source]#

Compute estimate of inverse CDF based on provided sampling data.

Parameters:
  • output_data – Dictionary with output data

  • bayesian – Compute confidence intervals etc.

Returns:

Dictionary with icdf estimates

estimate_mean(output_data: dict) ndarray[source]#

Estimate mean based on standard unbiased estimator.

Parameters:

output_data – Dictionary with output data

Returns:

Unbiased mean estimate

estimate_pdf(output_data: dict, support_points: ndarray, bayesian: bool) dict[source]#

Compute estimate of PDF based on provided sampling data.

Parameters:
  • output_data – Dictionary with output data

  • support_points – Points where to evaluate pdf

  • bayesian – Compute confidence intervals etc.

Returns:

Dictionary with pdf estimates

estimate_result_interval(output_data: dict) list[source]#

Estimate interval of output data.

Estimate interval of output data and add small margins.

Parameters:

output_data – Dictionary with output data

Returns:

Output interval

estimate_var(output_data: dict) ndarray[source]#

Estimate variance based on standard unbiased estimator.

Parameters:

output_data – Dictionary with output data

Returns:

Unbiased variance estimate

perform_kde(samples: ndarray, kernel_bandwidth: float, support_points: ndarray) ndarray[source]#

Estimate pdf using kernel density estimation.

Parameters:
  • samples – Samples for which to estimate pdf

  • kernel_bandwidth – Kernel width to use in kde

  • support_points – Points where to evaluate pdf

Returns:

PDF estimate at support points

process_outputs(output_data: dict, output_description: dict, input_data: ndarray | None = None) dict[source]#

Process output from QUEENS models.

Parameters:
  • output_data – Dictionary containing model output

  • output_description – Dictionary describing desired output quantities

  • input_data – Array containing model input

Returns:

Dictionary with processed results

write_results(processed_results: Any, file_path: Path) None[source]#

Write results to pickle file.

Parameters:
  • processed_results – Dictionary with results

  • file_path – Path to pickle file to write results to

queens.utils.remote_build module#

Utils to build queens on remote resource.

queens.utils.remote_operations module#

Module supplies functions to conduct operation on remote resource.

class RemoteConnection(host: str, remote_python: str | Path, remote_queens_repository: str | Path, user: str | None = None, gateway: dict | Connection | None = None)[source]#

Bases: Connection

This is class wrapper around the Connection class of fabric.

remote_python#

Path to Python with installed (editable) QUEENS (see remote_queens_repository)

remote_queens_repository#

Path to the QUEENS source code on the remote host

build_remote_environment(package_manager: str = 'mamba') None[source]#

Build remote QUEENS environment.

Parameters:

package_manager – Package manager used for the creation of the environment (“mamba” or “conda”)

copy_to_remote(source: str | Path | Sequence, destination: Path | str, verbose: bool = True, exclude: str | Sequence | None = None, filters: str | None = None) None[source]#

Copy files or folders to remote.

Parameters:
  • source – Paths to copy

  • destination – Destination relative to host

  • verbose – True for verbose

  • exclude – Options to exclude

  • filters – Filters for rsync

create_remote_directory(remote_directory: str | Path) None[source]#

Make a directory (including parents) on the remote host.

Parameters:

remote_directory – Path of the directory that will be created

get_free_local_port() int[source]#

Get a free port on localhost.

get_free_remote_port() Any[source]#

Get a free port on remote host.

open() None[source]#

Initiate the SSH connection.

open_port_forwarding(local_port: int | None = None, remote_port: int | None = None) tuple[int, Any][source]#

Open port forwarding.

Parameters:
  • local_port – Free local port

  • remote_port – Free remote port

Returns:
  • Used local port

  • Used remote port

run_function(func: Callable, *func_args: Any, wait: bool = True, **func_kwargs: Any) Any[source]#

Run a python function remotely using an ssh connection.

Parameters:
  • func – Function that is executed

  • func_args – Additional arguments for the functools.partial function

  • wait – Flag to decide whether to wait for result of function

  • func_kwargs – Additional keyword arguments for the functools.partial function

Returns:

Return value of function

start_cluster(workload_manager: str, dask_cluster_kwargs: dict, dask_cluster_adapt_kwargs: dict, experiment_dir: str) tuple[Any, Any][source]#

Start a Dask Cluster remotely using an ssh connection.

Parameters:
  • workload_manager – Workload manager (“pbs” or “slurm”) on cluster

  • dask_cluster_kwargs – Collection of keyword arguments to be forwarded to DASK Cluster

  • dask_cluster_adapt_kwargs – Collection of keyword arguments to be forwarded to DASK Cluster adapt method

  • experiment_dir – Directory holding all data of QUEENS experiment on remote

Returns:

Return value of function

sync_remote_repository() None[source]#

Synchronize local and remote QUEENS source files.

get_port() int[source]#

Get free port.

Returns:

Free port

queens.utils.rsync module#

Rsync utils.

assemble_rsync_command(source: str | Path | Sequence, destination: Path | str, archive: bool = False, exclude: str | Sequence | None = None, filters: str | None = None, verbose: bool = True, rsh: str | None = None, host: str | None = None, rsync_options: Sequence | None = None) str[source]#

Assemble rsync command.

Parameters:
  • source – Paths to copy

  • destination – Destination relative to host

  • archive – Use the archive option

  • exclude – Options to exclude

  • filters – Filters for rsync

  • verbose – True for verbose

  • rsh – Remote ssh command

  • host – Host to which to copy the files

  • rsync_options – Additional rsync options

Returns:

Command to run rsync

rsync(source: str | Path | Sequence, destination: str | Path, archive: bool = True, exclude: str | Sequence | None = None, filters: str | None = None, verbose: bool = True, rsh: str | None = None, host: str | None = None, rsync_options: Sequence | None = None) None[source]#

Run rsync command.

Parameters:
  • source – Paths to copy

  • destination – Destination relative to host

  • archive – Use the archive option

  • exclude – Options to exclude

  • filters – Filters for rsync

  • verbose – True for verbose

  • rsh – Remote ssh command

  • host – Host where to copy the files to

  • rsync_options – Additional rsync options

queens.utils.run_subprocess module#

Wrapped functions of subprocess stdlib module.

run_subprocess(command: str, raise_error_on_subprocess_failure: bool = True, additional_error_message: str | None = None, allowed_errors: list[str] | None = None) tuple[int, int, str, str][source]#

Run a system command outside of the Python script.

Parameters:
  • command – Command that will be run in subprocess

  • raise_error_on_subprocess_failure – Raise or warn error defaults to True

  • additional_error_message – Additional error message to be displayed

  • allowed_errors – List of strings to be removed from the error message

Returns:
  • Code for success of subprocess

  • Unique process ID that was assigned to the subprocess on computing machine

  • Standard output content

  • Standard error content

start_subprocess(command: str) Popen[source]#

Start subprocess.

Parameters:

command – Command that will be run in subprocess

Returns:

Subprocess object

queens.utils.scaling module#

Utils for data scaling.

class IdentityScaler[source]#

Bases: Scaler

The identity scaler.

fit(x_mat: ndarray) None[source]#

Fit/calculate the scaling based on the input samples.

Parameters:

x_mat – Data matrix that should be standardized

inverse_transform_grad_mean(grad_mean: ndarray, *_args: Any) ndarray[source]#

Conduct the inverse scaling of the mean gradient.

Parameters:

grad_mean – Gradient of the transformed mean function

Returns:

Inversely transformed gradient of the mean function

inverse_transform_grad_var(grad_var: ndarray, *_args: Any) ndarray[source]#

Conduct the inverse scaling of the variance gradient.

Parameters:

grad_var – Gradient of the transformed variance function

Returns:

Inversely transformed gradient of the variance function

inverse_transform_mean(x_mat: ndarray) ndarray[source]#

Conduct the inverse scaling transformation on the data matrix.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

inverse_transform_std(x_mat: ndarray) ndarray[source]#

Conduct the inverse scaling.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

transform(x_mat: ndarray) ndarray[source]#

Conduct the scaling transformation on the data matrix.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

class Scaler[source]#

Bases: object

Base class for general scaling classes.

The purpose of these classes is the scaling of data.

abstract fit(x_mat: ndarray) None[source]#

Fit/calculate the scaling based on the input samples.

Parameters:

x_mat – Data matrix that should be standardized

abstract inverse_transform_mean(x_mat: ndarray) ndarray[source]#

Conduct the inverse transformation for the mean.

Parameters:

x_mat – Data matrix that should be standardized

abstract inverse_transform_std(x_mat: ndarray) ndarray[source]#

Conduct the inverse transformation.

Parameters:

x_mat – Data matrix that should be standardized

abstract transform(x_mat: ndarray) ndarray[source]#

Conduct the scaling transformation on the input samples.

Parameters:

x_mat – Data matrix that should be standardized

class StandardScaler[source]#

Bases: Scaler

Scaler for standardization of data.

In case a stochastic process is trained on the scaled data, inverse rescaling is implemented to recover the correct mean and standard deviation prediction for the posterior process.

mean#

Mean-values of the data-matrix (column-wise).

standard_deviation#

Standard deviation of the data-matrix (per column).

fit(x_mat: ndarray) None[source]#

Fit/calculate the scaling based on the input samples.

Parameters:

x_mat – Data matrix that should be standardized

inverse_transform_grad_mean(grad_mean: ndarray, standard_deviation_input: float) ndarray[source]#

Conduct the inverse scaling of the mean gradient.

Parameters:
  • grad_mean – Gradient of the transformed mean function

  • standard_deviation_input – Standard deviation of the input data

Returns:

Inversely transformed gradient of the mean function

inverse_transform_grad_var(grad_var: ndarray, var: ndarray, trans_var: ndarray, input_standard_deviation: float) ndarray[source]#

Conduct the inverse scaling of the variance gradient.

Parameters:
  • grad_var – Gradient of the transformed variance

  • var – Variance of the untransformed data

  • trans_var – Variance of the transformed data

  • input_standard_deviation – Standard deviation of the input data

Returns:

Inversely transformed gradient of the variance function

inverse_transform_mean(x_mat: ndarray) ndarray[source]#

Conduct the inverse scaling transformation on the data matrix.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

inverse_transform_std(x_mat: ndarray) ndarray[source]#

Conduct the inverse scaling transformation.

The data is transformed based on the standard deviation.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

transform(x_mat: ndarray) ndarray[source]#

Conduct the scaling transformation on the data matrix.

Parameters:

x_mat – Data matrix that should be standardized

Returns:

Transformed data-array

queens.utils.sequential_monte_carlo module#

Collection of utility functions and classes for SMC algorithms.

class StaticStateSpaceModel(likelihood_model: Callable, data: None = None, prior: StructDist | None = None)[source]#

Bases: StaticModel

Model needed for the particles library implementation of SMC.

likelihood_model#

Log-likelihood function.

loglik(theta: ndarray, t: None = None) ndarray[source]#

Log-likelihood function for particles SMC implementation.

Parameters:
  • theta – Samples at which to evaluate the likelihood

  • t – Time (if set to None, the full log-likelihood is returned)

Returns:

The log likelihood

logpyt(theta: Any, t: Any) None[source]#

Log-likelihood of Y_t, given parameter and previous datapoints.

Parameters:
  • theta – theta[‘par’] is a ndarray containing the N values for parameter par

  • t – Time

numpy_to_particles_array(samples: ndarray) ndarray[source]#

Convert numpy arrays to particles objects.

The particles library uses np.ndarrays with homemade variable dtypes. This method converts it back to the particles library type.

Parameters:

samples – Samples

Returns:

*Particle* variables object

particles_array_to_numpy(theta: ndarray) ndarray[source]#

Convert particles objects to numpy arrays.

The particles library uses np.ndarrays with homemade variable dtypes. We need to convert this into numpy arrays to work with queens.

Parameters:

thetaParticle variables object

Returns:

Numpy array of the particles

calc_ess(weights: ndarray) generic[source]#

Calculate the Effective Sample Size (ESS) from the given weights.

The Effective Sample Size (ESS) is a measure used to assess the quality of a set of weights by indicating how many independent samples would be required to achieve the same level of information as the current weighted samples. This is computed using the exp-log trick to improve numerical stability.

Parameters:

weights – An array of weights, typically representing the importance weights of samples in a weighted sampling scheme.

Returns:

The Effective Sample Size (ESS)

temper_factory(temper_type: Literal['bayes', 'generic']) Callable[source]#

Return the appropriate tempering function based on the specified type.

The tempering function can be used for transitioning between different log-probability density functions in various probabilistic models.

Parameters:

temper_type

Type of the tempering function to return. Valid options are:

  • bayes: Returns the Bayes tempering function.

  • generic: Returns the generic tempering function.

Returns:

The corresponding tempering function based on `temper_type`.

Raises:

ValueError – If temper_type is not one of the valid options (“bayes”, “generic”).

temper_logpdf_bayes(log_prior: ndarray, log_like: ndarray, tempering_parameter: float = 1.0) ndarray[source]#

Bayesian tempering function.

It phases from the prior to the posterior = like * prior. Special cases are:

  • tempering parameter = 0.0:

    We interpret this as “disregard contribution of the likelihood”. Therefore, return just log_prior.

  • log_prior or log_like = +inf:

    Prohibit this case. The reasoning is that (+inf + -inf) is ambiguous. We know that -inf is likely to occur, e.g. in uniform priors. On the other hand, +inf is rather unlikely to be a reasonable value. Therefore, we chose to exclude it here.

Parameters:
  • log_prior – Array containing the values of the log-prior distribution at sample points

  • log_like – Array containing the values of the log-likelihood at sample points

  • tempering_parameter – Tempering parameter for resampling

temper_logpdf_generic(logpdf0: ndarray, logpdf1: ndarray, tempering_parameter: float = 1.0) ndarray[source]#

Perform generic tempering between two log-probability density functions.

This function performs a linear interpolation between two log-probability density functions based on a tempering parameter. The tempering parameter determines the weight given to each log-probability density function in the transition from the initial distribution (logpdf0) to the goal distribution (logpdf1).

The function handles the following scenarios:

  • tempering parameter = 0.0:

    We interpret this as “disregard contribution of the goal pdf”. Therefore, return logpdf0.

  • tempering parameter = 1.0:

    We interpret this as “we are fully transitioned.” Therefore, ignore the contribution of the initial distribution. Therefore, return logpdf1.

  • logpdf0 or logpdf1 = +inf:

    Prohibit this case. The reasoning is that (+inf + -inf) is ambiguous. We know that -inf is likely to occur, e.g., in uniform distributions. On the other hand, +inf is rather unlikely to be a reasonable value. Therefore, we chose to exclude it here.

Parameters:
  • logpdf0 – Logarithm of the probability density function of the initial distribution.

  • logpdf1 – Logarithm of the probability density function of the goal distribution.

  • tempering_parameter – Parameter between 0 and 1 that controls the interpolation between logpdf0 and logpdf1. A value of 0.0 corresponds to logpdf0, while a value of 1.0 corresponds to logpdf1.

Returns:

The tempered log-probability density function based on the `tempering_parameter`.

Raises:

ValueError – If either logpdf0 or logpdf1 is positive infinity (+inf).

queens.utils.sobol_sequence module#

Collection of utility functions and classes for Sobol sequences.

sample_sobol_sequence(dimension: int, number_of_samples: int, parameters: Parameters, randomize: bool = False, seed: int | Generator | None = None) ndarray[source]#

Generate samples from Sobol sequence.

Parameters:
  • dimension – Dimensionality of the sequence. Max dimensionality is 21201.

  • number_of_samples – Number of samples to generate in the parameter space

  • parameters – Parameters object defining the true distribution of the samples

  • randomize – If True, use LMS+shift scrambling, i.e. randomize the sequence. Otherwise, no scrambling is done.

  • seed – If seed is an int or None, a new numpy.random.Generator is created using np.random.default_rng(seed). If seed is already a Generator instance, then the provided instance is used.

Returns:

Sobol sequence quasi Monte Carlo samples for the parameter distribution

queens.utils.start_dask_cluster module#

Main module to start a dask jobqueue cluster.

parse_arguments(unparsed_args: Sequence[str]) Namespace[source]#

Parse arguments passed via command line call.

queens.utils.valid_options module#

Helper functions for valid options and switch analogy.

check_if_valid_options(valid_options: list | dict, desired_options: str | dict[str, int] | list[str], error_message: str = '') None[source]#

Check if the desired option(s) is/are in valid_options.

Parameters:
  • valid_options – List of valid option keys or dict with valid options as keys

  • desired_options – Key(s) of desired options

  • error_message – Error message in case the desired option can not be found

Raises:

InvalidOptionError – If any of the desired options is in invalid options

get_option(options_dict: dict[str, Any], desired_option: str, error_message: str = '') Any[source]#

Get option desired_option from options_dict.

The options_dict consists of the keys and their values. Note that the value can also be functions. In case the option is not found an error is raised.

Parameters:
  • options_dict – Dictionary with valid options and their value

  • desired_option – Desired method key

  • error_message – Custom error message to be used if the desired_option is not found.

Returns:

Value of the desired option