mlmc.quantity

Subpackage provides methods to represent and handle a quantity of interest.

Submodules

mlmc.quantity.quantity module

class mlmc.quantity.quantity.Quantity(quantity_type, operation, input_quantities=[])[source]

Bases: object

static QArray(quantities)[source]
static QDict(key_quantity)[source]
static QField(key_quantity)[source]
static QTimeSeries(time_quantity)[source]
static add_op(x, y)[source]
static create_quantity(quantities, operation)[source]

Create new quantity (Quantity or QuantityConst) based on given quantities and operation. There are two scenarios: 1. At least one of quantities is Quantity instance then all quantities are considered to be input_quantities

of new Quantity

2. All of quantities are QuantityConst instances then new QuantityConst is created :param quantities: List[Quantity] :param operation: function which is run with given quantities :return: Quantity

get_cache_key(chunk_spec)[source]

Create cache key

get_quantity_storage()[source]

Get QuantityStorage instance :return: None, QuantityStorage

static mod_op(x, y)[source]
static mult_op(x, y)[source]
static pick_samples(chunk, subsample_params)[source]

Pick samples some samples from chunk in order to have ‘k’ samples from ‘n’ after all chunks are processed Inspired by https://dl.acm.org/doi/10.1145/23002.23003 method S

Parameters:
  • chunk – np.ndarray, shape M, N, 2, where N denotes number of samples in chunk
  • subsample_params – instance of SubsampleParams class, it has two parameters: k: number of samples which we want to get from all chunks n: number of all samples among all chunks
Returns:

np.ndarray

samples(chunk_spec)[source]

Return list of sample chunks for individual levels. Possibly calls underlying quantities. :param chunk_spec: object containing chunk identifier level identifier and chunk_slice - slice() object :return: np.ndarray or None

select(*args)[source]

Performs sample selection based on conditions :param args: Quantity :return: Quantity

selection_id()[source]

Get storage ids of all input quantities :return: List[int]

set_selection_id()[source]

Set selection id selection id is None by default,

but if we create new quantity from quantities that are result of selection we need to pass selection id
size() → int[source]

Quantity size from qtype :return: int

static sub_op(x, y)[source]
subsample(sample_vec)[source]

Subsampling :param sample_vec: list of number of samples at each level :return: Quantity

static truediv_op(x, y)[source]
static wrap(value)[source]

Convert flat, bool or array (list) to Quantity :param value: flat, bool, array (list) or Quantity :return: Quantity

class mlmc.quantity.quantity.QuantityConst(quantity_type, value)[source]

Bases: mlmc.quantity.quantity.Quantity

samples(chunk_spec)[source]

Get constant values with an enlarged number of axes :param chunk_spec: object containing chunk identifier level identifier and chunk_slice - slice() object :return: np.ndarray

selection_id()[source]

Get storage ids of all input quantities :return: List[int]

class mlmc.quantity.quantity.QuantityMean(quantity_type, l_means, l_vars, n_samples, n_rm_samples)[source]

Bases: object

l_means
l_vars
mean
n_rm_samples
n_samples
var
class mlmc.quantity.quantity.QuantityStorage(storage, qtype)[source]

Bases: mlmc.quantity.quantity.Quantity

chunks(level_id=None)[source]
get_quantity_storage()[source]

Get QuantityStorage instance :return: None, QuantityStorage

level_ids()[source]

Number of levels :return: List[int]

n_collected()[source]
samples(chunk_spec)[source]

Get results for given level id and chunk id :param chunk_spec: object containing chunk identifier level identifier and chunk_slice - slice() object :return: Array[M, chunk size, 2]

selection_id()[source]

Identity of QuantityStorage instance :return: int

mlmc.quantity.quantity.make_root_quantity(storage: mlmc.sample_storage.SampleStorage, q_specs: List[mlmc.quantity.quantity_spec.QuantitySpec])[source]

Create a root quantity that has QuantityStorage as the input quantity, QuantityStorage is the only class that directly accesses the stored data. Quantity type is created based on the q_spec parameter :param storage: SampleStorage :param q_specs: same as result format in simulation class :return: QuantityStorage

mlmc.quantity.quantity_estimate module

mlmc.quantity.quantity_estimate.cache_clear()[source]
mlmc.quantity.quantity_estimate.covariance(quantity, moments_fn, cov_at_bottom=True)[source]

Create quantity with operation that evaluates covariance matrix :param quantity: Quantity :param moments_fn: mlmc.moments.Moments child :param cov_at_bottom: bool, if True cov matrices are underneath,

a scalar is substituted with a matrix of moments of that scalar
Returns:Quantity
mlmc.quantity.quantity_estimate.estimate_mean(quantity)[source]

MLMC mean estimator. The MLMC method is used to compute the mean estimate to the Quantity dependent on the collected samples. The squared error of the estimate (the estimator variance) is estimated using the central limit theorem. Data is processed by chunks, so that it also supports big data processing :param quantity: Quantity :return: QuantityMean which holds both mean and variance

mlmc.quantity.quantity_estimate.mask_nan_samples(chunk)[source]

Mask out samples that contain NaN in either fine or coarse part of the result :param chunk: np.ndarray [M, chunk_size, 2] :return: chunk: np.ndarray, number of masked samples: int

mlmc.quantity.quantity_estimate.moment(quantity, moments_fn, i=0)[source]

Create quantity with operation that evaluates particular moment :param quantity: Quantity instance :param moments_fn: mlmc.moments.Moments child :param i: index of moment :return: Quantity

mlmc.quantity.quantity_estimate.moments(quantity, moments_fn, mom_at_bottom=True)[source]

Create quantity with operation that evaluates moments_fn :param quantity: Quantity :param moments_fn: mlmc.moments.Moments child :param mom_at_bottom: bool, if True moments are underneath,

a scalar is substituted with an array of moments of that scalar
Returns:Quantity

mlmc.quantity.quantity_spec module

class mlmc.quantity.quantity_spec.ChunkSpec(chunk_id: int = None, chunk_slice: slice = None, level_id: int = None)[source]

Bases: object

class mlmc.quantity.quantity_spec.QuantitySpec(name: str, unit: str, shape: Tuple[int, int], times: List[float], locations: Union[List[str], List[Tuple[float, float, float]]])[source]

Bases: object

mlmc.quantity.quantity_types module

class mlmc.quantity.quantity_types.ArrayType(shape, qtype: mlmc.quantity.quantity_types.QType)[source]

Bases: mlmc.quantity.quantity_types.QType

get_key(key)[source]

ArrayType indexing :param key: int, tuple of ints or slice objects :return: QuantityType - ArrayType or self._qtype

reshape(data)[source]
size() → int[source]

Size of type :return: int

class mlmc.quantity.quantity_types.BoolType(qtype=<class 'float'>)[source]

Bases: mlmc.quantity.quantity_types.ScalarType

class mlmc.quantity.quantity_types.DictType(args: List[Tuple[str, mlmc.quantity.quantity_types.QType]])[source]

Bases: mlmc.quantity.quantity_types.QType

base_qtype()[source]
get_key(key)[source]
get_qtypes()[source]
replace_scalar(substitute_qtype)[source]

Find ScalarType and replace it with substitute_qtype :param substitute_qtype: QType, replaces ScalarType :return: DictType

size() → int[source]

Size of type :return: int

class mlmc.quantity.quantity_types.FieldType(args: List[Tuple[str, mlmc.quantity.quantity_types.QType]])[source]

Bases: mlmc.quantity.quantity_types.QType

get_key(key)[source]
size() → int[source]

Size of type :return: int

class mlmc.quantity.quantity_types.QType(qtype)[source]

Bases: object

base_qtype()[source]
static keep_dims(chunk)[source]

Always keep chunk shape to be [M, chunk size, 2]! For scalar quantities, the input block can have the shape (chunk size, 2) Sometimes we need to ‘flatten’ first few shape to have desired chunk shape :param chunk: list :return: list

replace_scalar(substitute_qtype)[source]

Find ScalarType and replace it with substitute_qtype :param substitute_qtype: QType, replaces ScalarType :return: QType

reshape(data)[source]
size() → int[source]

Size of type :return: int

class mlmc.quantity.quantity_types.ScalarType(qtype=<class 'float'>)[source]

Bases: mlmc.quantity.quantity_types.QType

base_qtype()[source]
replace_scalar(substitute_qtype)[source]

Find ScalarType and replace it with substitute_qtype :param substitute_qtype: QType, replaces ScalarType :return: QType

size() → int[source]

Size of type :return: int

class mlmc.quantity.quantity_types.TimeSeriesType(times, qtype)[source]

Bases: mlmc.quantity.quantity_types.QType

get_key(key)[source]
size() → int[source]

Size of type :return: int

static time_interpolation(quantity, value)[source]

Interpolation in time :param quantity: Quantity instance :param value: point where to interpolate :return: Quantity

Module contents

Subpackage provides methods to represent and handle a quantity of interest.