Utility functions
qp.utils.array: Array utility functions
Utility functions for array handling in the the qp package
- qp.utils.array.edge_to_center(edges: ArrayLike) ndarray[source]
Return the centers of a set of bins given the edges
- qp.utils.array.bin_widths(edges: ArrayLike) ndarray[source]
Return the widths of a set of bins given the edges
- qp.utils.array.get_bin_indices(bins: ArrayLike, x: ArrayLike) ndarray[int][source]
Return the bin indexes for a set of values
If the bins are equal width this will use arithmetic, If the bins are not equal width this will use a binary search
- qp.utils.array.get_eval_case(x: ArrayLike, row: ArrayLike) tuple[int, ndarray, ndarray][source]
Figure out which of the various input formats scipy.stats has passed us
- Parameters:
- Returns:
- case
int The case code
- xx
np.ndarray The x-values properly shaped
- rr
np.ndarrray The y-values, properly shaped
- case
Notes
The cases are:
CASE_FLAT : x, row have shapes (n), (n) and do not factor CASE_FACTOR : x, row have shapes (n), (n) but can be factored to shapes (1, nx) and (npdf, 1)
(i.e., they were flattened by scipy)
CASE_PRODUCT : x, row have shapes (1, nx) and (npdf, 1) CASE_2D : x, row have shapes (npdf, nx) and (npdf, nx)
- qp.utils.array.profile(x_data: ArrayLike, y_data: ArrayLike, x_bins: ArrayLike, std: bool = True) tuple[ndarray[float], ndarray[float]][source]
Make a ‘profile’ plot
- Parameters:
- Returns:
- vals
np.ndarray[float], lengthnbins The means
- errs
np.ndarray[float], lengthnbins The standard deviations or errors on the means
- vals
- qp.utils.array.reshape_to_pdf_size(vals: ndarray, split_dim: int) ndarray[source]
Reshape an array to match the number of PDFs in a distribution
- Parameters:
- vals
np.ndarray The input array
- split_dim
int The dimension at which to split between pdf indices and per_pdf indices
- vals
- Returns:
- out
np.ndarray The reshaped array
- out
- qp.utils.array.reshape_to_pdf_shape(vals: np.ndarray, pdf_shape: int, per_pdf: int | ArrayLike) np.ndarray[source]
Reshape an array to match the shape of PDFs in a distribution
- Parameters:
- vals
np.ndarray The input array
- pdf_shape
int The shape for the pdfs
- per_pdf
int|ArrayLike The shape per pdf
- vals
- Returns:
- out
np.ndarray The reshaped array
- out
- qp.utils.array.encode_strings(data: Mapping[str, ndarray]) Mapping[str, ndarray][source]
Encodes any dictionary values that are Unicode strings (or just strings if not numpy arrays). Other data types are not affected.
- Parameters:
- data
Mapping[str,np.ndarray] Dictionary of data to encode.
- data
- Returns:
Mapping[str,np.ndarray]Dictionary of data with strings encoded.
- qp.utils.array.decode_strings(data: Mapping[str, ndarray]) Mapping[str, ndarray][source]
Decodes dictionary values that have been encoded (dtype = bytes). Other data types are not affected.
- Parameters:
- data
Mapping[str,np.ndarray] The dictionary of data to be decoded.
- data
- Returns:
Mapping[str,np.ndarray]The dictionary of data with any strings decoded.
- qp.utils.array.reduce_dimensions(arr: ndarray, x: ArrayLike) float | ndarray[source]
If the given array has dimensionality greater than x, reduces its dimensionality to match x, if this will not result in a loss of data.
- Parameters:
- arr
np.ndarray Array to reduce dimensionality
- x
ArrayLike Object to match dimensionality to
- arr
- Returns:
Union[float,np.ndarray]The array with dimension reduced (if possible)
qp.utils.conversion: Conversion utility functions
This module implements functions to convert distributions between various representations
These functions should then be registered with the qp.ConversionDict using qp_add_mapping.
That will allow the automated conversion mechanisms to work.
- qp.utils.conversion.extract_xy_vals(in_dist: Ensemble, xvals: np.ndarray) dict[str, np.ndarray][source]
Convert using a set of x and y values.
- Parameters:
- in_dist
Ensemble Input distributions
- xvals
np.ndarray Locations at which the pdf is evaluated
- in_dist
- Returns:
- data
dict[str,np.ndarray] The extracted data
- data
- qp.utils.conversion.extract_fit(in_dist, **kwargs)[source]
Convert to a functional distribution by fitting it to a set of x and y values
- qp.utils.conversion.extract_voigt_mixmod(in_dist, **kwargs)[source]
Convert to a voigt mixture model starting with a gaussian mixture model, trivially by setting gammas to 0
qp.utils.dictionary: Multi-level dictionary manipulation
This module implements tools to handle dictionaries
- qp.utils.dictionary.get_val_or_default(in_dict: dict, key: str) Any | None[source]
Helper functions to return either an item in a dictionary or the default value of the dictionary
- Parameters:
- Returns:
Notes
- This will first try to return:
in_dict[key] : i.e., the requested item.
- If that fails it will try
in_dict[None] : i.e., the default for that dictionary.
- If that fails it will return
None
- qp.utils.dictionary.set_val_or_default(in_dict: dict, key: str, val: Any)[source]
Helper functions to either get and item from or add an item to a dictionary and return that item
- Parameters:
- Returns:
- out
Any If the key already existed, return the current value. Otherwise, return
val.
- out
Notes
- This will first try to return:
in_dict[key] : i.e., the requested item.
- If that fails it will return
val
- qp.utils.dictionary.pretty_print(in_dict: dict, prefixes: list, idx=0, stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>) None[source]
Print a level of the converstion dictionary in a human-readable format
- qp.utils.dictionary.print_dict_shape(in_dict: dict) None[source]
Print the shape of arrays in a dictionary. This is useful for debugging table creation.
- Parameters:
- in_dict
dict The dictionary to print
- in_dict
- qp.utils.dictionary.slice_dict(in_dict: dict, subslice: int | slice) dict[source]
Create a new dict by taking a slice of of every array in a dict
- qp.utils.dictionary.check_keys(in_dicts: list[dict]) None[source]
Check that the keys in all the in_dicts match
Raises KeyError if one does not match.
- qp.utils.dictionary.concatenate_dicts(in_dicts: list[dict], add_axis: int = 0) dict[source]
Create a new dict by concatenate each array in
in_dicts
- qp.utils.dictionary.check_array_shapes(in_dict: dict, npdf: int) None[source]
Check that all the arrays in in_dict match the number of pdfs
Raises ValueError if one does not match.
- qp.utils.dictionary.compare_two_dicts(d1: dict, d2: dict) bool[source]
Check that all the items in d1 and d2 match
- Returns:
- matchbool
True if they all match, False otherwise
- qp.utils.dictionary.compare_dicts(in_dicts: list[dict]) bool[source]
Check that all the dicts in in_dicts match
- Returns:
- matchbool
True if they all match, False otherwise
- qp.utils.dictionary.reduce_arrays_to_1d(in_dict: Mapping) Mapping[source]
Checks if any arrays in the dictionary have ndim greater than 1, and if the first dimension is equal to 1 it reshapes the array to remove that dimension.
- qp.utils.dictionary.make_len_equal(in_dict: Mapping[str, ndarray], l_arr: int = 1) Mapping[source]
Ensures that all arrays in the dictionary have
shape[0]of at least l_arr.This essentially assures that a dictionary of numpy arrays is a
numpyDictTable-likeobject according totables_ioto allow for writing.
- qp.utils.dictionary.expand_dimensions(in_dict: Mapping[str, ndarray], npdf: int, nvals: int) Mapping[source]
- qp.utils.dictionary.ensure_2d_array(arr: ndarray, add_axis: int = 0) ndarray[source]
Makes sure that the input array is at least 2 dimensions, by adding a new axis to 1D arrays. By default, the new axis is added as axis=0, so the new array will have shape (1, len(arr)).
- Parameters:
- arr
np.ndarray The input array
- add_axis
int Where to add the new axis.
- arr
- Returns:
np.ndarrayReturns the input array if it’s 2D, or an array with an extra dimension if given a 1D array.
qp.utils.interpolation: PDF evaluation and construction utility functions
- qp.utils.interpolation.interpolate_multi_x_y(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_y_product(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_y_2d(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_y_flat(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_x_multi_y_product(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_x_multi_y(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray The interpolated values
- vals
- qp.utils.interpolation.interpolate_x_multi_y_2d(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_x_multi_y_flat(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_multi_y_flat(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_multi_y_product(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_multi_y_2d(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray, shape (npdf,n) The interpolated values
- vals
- qp.utils.interpolation.interpolate_multi_x_multi_y(x: ArrayLike, row: ArrayLike, xvals: ArrayLike, yvals: ArrayLike, **kwargs) ndarray[source]
Interpolate a set of values
- Parameters:
- Returns:
- vals
np.ndarray The interpolated values
- vals