mckit_meshes.utils package¶
Submodules¶
mckit_meshes.utils.cartesian_product module¶
Apply function to cartesian product of arrays.
- mckit_meshes.utils.cartesian_product.cartesian_product(*arrays, aggregator=<function <lambda>>, **kw)[source]¶
Computes transformations of cartesian product of all the elements in arrays.
- Parameters:
arrays (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – The arrays to product.aggregator (
Callable) – Callable to handle an item from product iterator. The first parameter of the callable is tuple of current product item. May return scalar or numpy ndarray.kw (
Any) – keyword arguments to pass to aggregator
- Return type:
NDArray
Examples
>>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> cartesian_product(a, b, aggregator=lambda x: x[0] * x[1]) array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])
>>> cartesian_product(a, b) array([[[1, 4], [1, 5], [1, 6]], [[2, 4], [2, 5], [2, 6]], [[3, 4], [3, 5], [3, 6]]])
- Returns:
- Numpy array with dimension of arrays and
additional dimensions for their cartesian product.
- Return type:
ret
- Parameters:
arrays (ArrayLike)
aggregator (Callable)
kw (Any)
mckit_meshes.utils.io module¶
mckit_meshes.utils.no_daemon_process module¶
mckit_meshes.utils.rebin module¶
Functions for rebinning histogram-like distributions.
- mckit_meshes.utils.rebin.interpolate(x_new, x, y, axis=None)[source]¶
- Return type:
ArrayFloat
- Parameters:
x_new (ArrayFloat)
x (ArrayFloat)
y (ArrayFloat)
axis (int | None)
- mckit_meshes.utils.rebin.rebin_1d(a, bins, new_bins, axis=0, *, grouped=False, assume_sorted=False)[source]¶
Transforms 1-D histogram defined as data on the limiting points.
define like bins to equivalent (see the terms below) histogram defined on other limiting points defined as new_bins.
Notes
The algorithm maintains the equality of integral on intervals defined on new_bins for the original and rebinned distributions.
- Parameters:
a (
ndarray) – The array to rebinbins (
ndarray) – Defines 1-D array representing a binning along the given `axisnew_bins (
ndarray) – The new binning required.axis (
int) – int, optional An axis along which to rebin array agrouped (
bool) –bool, optional Defines the approach for rebinning.
- If True, then the values in a represent the data already
integrated over bins, like in energy group distributions. On rebinning maintain equivalence of integral over same energy range in old and new bins.
If False (default), as for spatial binning - maintain the same average value in the same volume in old and new bins.
assume_sorted (
bool) – bool, optional If True, then skip assertion of bins sorting order, by default False - asserts the input_file data
- Return type:
- Returns:
rebinned data
- mckit_meshes.utils.rebin.rebin_nd(a, rebin_spec, *, assume_sorted=False, external_process_threshold=1000000)[source]¶
Rebin an array a over multidimensional grid.
- Parameters:
a (
ndarray[tuple[Any,...],dtype[float]]) – An array to rebin.rebin_spec (
Iterable[tuple[ndarray[tuple[Any,...],dtype[float]],ndarray[tuple[Any,...],dtype[float]],int,bool]]) – Iterator An iterator listing tuples specifying bins, new_bins, axis and grouped parameters for rebinning. Seerebin_1d()for details on the parameters.assume_sorted (
bool) – bool, optional If True skip assertion of bins sorting order, by default False - asserts the input_file dataexternal_process_threshold (
int) – int If size of a is greater than that, then the computation is executed in external process, to achieve immediate memory cleanup.
- Return type:
- Returns:
Rebinned data.
- mckit_meshes.utils.rebin.rebin_spec_composer(bins_seq, new_bins_seq, axes=None, grouped_flags=None)[source]¶
Compose rebin_spec parameter.
See also
mckit_meshes.utils.rebin.rebin_nd()with reasonable defaults for axes and grouped iterators.- Parameters:
bins_seq – sequence of ndarrays Iterates over the list of original bins
new_bins_seq – sequence of ndarrays Iterates over the list of new bins.
axes – sequence of ints, optional Iterates over the list of corresponding axes. If not provided (default), then iterates over sequence 0 … len(bins).
grouped_flags – sequence of booleans, optional Iterates over a sequence of grouped flags. If not provided (default), then all the axes considered as not grouped. If constant boolean value is proved, then for all the axes this value is applied.
- Return type:
Iterable[tuple[ndarray[tuple[Any,...],dtype[float]],ndarray[tuple[Any,...],dtype[float]],ndarray[tuple[Any,...],dtype[float]],ndarray[tuple[Any,...],dtype[float]]]]- Returns:
Iterator over the sequence of tuples (bins, new_bins, axis, grouped)
- mckit_meshes.utils.rebin.shrink_1d(a, bins, low=None, high=None, axis=None, *, assume_sorted=False)[source]¶
Select sub-arrays of a a and corresponding bins for minimal span.
of bins, which completely covers the range [low…`high`] both sides included.
- Parameters:
a (
ndarray) – ndarray An array to shrink.bins (
ndarray) – ndarray Bins corresponding to the grid a over the given axis.low (
float|None) – float, optional Left edge of the range to shrink to. When omitted, the bins left edge is used.high (
float|None) – float Right edge of the range to shrink to. When omitted, the bins right edge is used.axis (
int|None) – int, optional An axis of a over which to shrink. Default axis = 0.assume_sorted (
bool) – bool, optional If True skip assertion of bins sorting order, by default False - asserts the input_file data
- Returns:
- ndarray
The shrank bins
- new_data: ndarray
The shrank grid
- Return type:
new_bins
- mckit_meshes.utils.rebin.shrink_nd(a, trim_spec, *, assume_sorted=False)[source]¶
Perform multidimensional shrink.
- Parameters:
- Return type:
- Returns:
A sequence with new bins, if any, the shrunk or initial grid.
- mckit_meshes.utils.rebin.trim_spec_composer(bins_seq, lefts=None, rights=None, axes=None)[source]¶
Helps to compose trim_spec parameter in.
mckit_meshes.utils.rebin.trim_nd()with reasonable defaults for lefts, rights and axes iterators.- Parameters:
bins_seq – sequence of ndarrays Iterates over the list of bins associated with a grid to be trimmed.
lefts – sequence of floats Iterates over the list of left edges for trimming.
rights – sequence of floats Iterates over the list of right edges for trimming.
axes – sequence of ints, optional Iterates over the list of corresponding axes. If not provided (default), then iterates over sequence 0..len(bins).
- Return type:
Iterable[tuple[ndarray[tuple[Any,...],dtype[float]],float,float,int]]- Returns:
Iterator over the sequence of tuples (bins, lefts, rights, axis)
mckit_meshes.utils.resource module¶
mckit_meshes.utils.testing module¶
Utilities fof testing.
- mckit_meshes.utils.testing.a(*args, dtype=<class 'float'>)[source]¶
Shorten typing in parametrized tests.
Equivalent to np.ndarray([*args], dtype=’dtype’)`.
- Parameters:
args – sequence of numbers (any type) will be converted to the specified type.
dtype – A type for the output array
- Returns:
The ndarray with the given numbers and type.
- Return type:
np.ndarray
Examples: >>> a(1, 2, 3) array([1., 2., 3.]) >>> a(1, 2, 3, dtype=np.int32) array([1, 2, 3], dtype=int32) >>> a(0, 1, 2, 3).reshape(2, 2) array([[0., 1.],
[2., 3.]])
mckit_meshes.utils.utils module¶
Module contents¶
The package common utilities.
- mckit_meshes.utils.cartesian_product(*arrays, aggregator=<function <lambda>>, **kw)[source]¶
Computes transformations of cartesian product of all the elements in arrays.
- Parameters:
arrays (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str]]) – The arrays to product.aggregator (
Callable) – Callable to handle an item from product iterator. The first parameter of the callable is tuple of current product item. May return scalar or numpy ndarray.kw (
Any) – keyword arguments to pass to aggregator
- Return type:
Examples
>>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> cartesian_product(a, b, aggregator=lambda x: x[0] * x[1]) array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])
>>> cartesian_product(a, b) array([[[1, 4], [1, 5], [1, 6]], [[2, 4], [2, 5], [2, 6]], [[3, 4], [3, 5], [3, 6]]])
- Returns:
- Numpy array with dimension of arrays and
additional dimensions for their cartesian product.
- Return type:
ret
- Parameters:
- mckit_meshes.utils.check_if_path_exists(*, override)[source]¶
Select strategy to handle existing files, depending on option override.
- mckit_meshes.utils.print_cols(seq, fid=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, max_columns=6, fmt='{}')[source]¶
Print sequence in columns.
- Parameters:
seq (Iterable[Any]) – sequence to print
fid (SupportsWrite[str]) – output
max_columns (int) – max columns in a line
fmt (str) – format string
- Returns:
the number of the last column printed on the last row
- Return type:
- mckit_meshes.utils.print_n(words, io=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, indent='', max_columns=5)[source]¶
Print sequence in columns with indentation starting from the second row.
If anything was printed, add a newline.
- Parameters:
words (Iterable) – sequence ot items to print
io (SupportsWrite[str]) – where to print
indent (str) – indent to apply starting the second row
max_columns (int) – max number of columns in row
- Return type:
None
- mckit_meshes.utils.raise_error_when_file_exists_strategy(path)[source]¶
Strategy to use when file exists.
- Parameters:
path (
str|Path) – path to check- Raises:
FileExistsError – if path exits.
- Return type: