ZerosMatrix

class ompy.ZerosMatrix(shape=None, Ex=None, Eg=None, std=False, state=None)[source]

Bases: Matrix

Return new Matrix of given shape, filled with zeros.

Parameters:
  • shape (Optional[Tuple[int, int]]) – Shape of the new Matrix as [len(Ex), len(Eg)]. If Ex and Eg are provided, the shape is inferred.

  • Eg (Optional[ndarray]) – The gamma ray energies using midbinning. Defaults to an array with the length inferred from shape, if not provided.

  • Ex (Optional[ndarray]) – The excitation energies using midbinning. Defaults to an array with the length inferred from shape, if not provided.

  • std (bool) – Whether to create an array for the std, too

There is the option to initialize it in an empty state. In that case, all class variables will be None. It can be filled later using the load() method.

For initializing one can give values, [Ex, Eg] arrays, a filename for loading a saved matrix or a shape for initialzing it with zero entries.

Parameters:
  • values – Set the matrix’ values.

  • Eg (Optional[ndarray]) – The gamma ray energies using midbinning.

  • Ex (Optional[ndarray]) – The excitation energies using midbinning.

  • std (bool) – The standard deviations at each bin of values

  • path – Load a Matrix from a given path

  • state (Union[str, MatrixState, None]) – An enum to keep track of what has been done to the matrix. Can also be a str. like in [“raw”, “unfolded”, …]

  • shape (Optional[Tuple[int, int]]) – Depreciated. Use ZerosMatrix instead.

Attributes Summary

counts

range_Eg

Returns all indices of Eg

range_Ex

Returns all indices of Ex

shape

state

Methods Summary

ascii_plot([shape])

Plots a rebinned ascii version of the matrix

calibration()

Calculates the calibration coefficients of the energy axes

copy()

Return a deepcopy of the class

cut(axis[, Emin, Emax, inplace, ...])

Cuts the matrix to the sub-interval limits along given axis.

cut_diagonal([E1, E2, inplace])

Cut away counts to the right of a diagonal line defined by indices

cut_like(other[, inplace])

Cut a matrix like another matrix (according to energy arrays)

diagonal_elements()

Iterates over the last non-zero elements .. note::.

fill(Eg, Ex[, count])

Add counts to the bin containing Eg and Ex.

fill_and_remove_negative([window_size])

Combination of ompy.Matrix.fill_negative() and ompy.Matrix.remove_negative()

fill_negative(window_size)

Wrapper for ompy.fill_negative_gauss()

has_equal_binning(other, **kwargs)

Check whether other has equal binning as self within precision.

index_Eg(E)

Returns the closest index corresponding to the Eg value

index_Ex(E)

Returns the closest index corresponding to the Ex value

indices_Eg(E)

Returns the closest indices corresponding to the Eg value

indices_Ex(E)

Returns the closest indices corresponding to the Ex value

iter()

rtype:

Iterator[Tuple[int, int]]

line_mask(E1, E2)

Create a mask for above (True) and below (False) a line

load(path[, filetype])

Load matrix from specified format

plot(*[, ax, title, scale, vmin, vmax, ...])

Plots the matrix with the energy along the axis

plot_projection(axis[, Emin, Emax, ax, ...])

Plots the projection of the matrix along axis

projection(axis[, Emin, Emax, normalize])

Returns the projection along the specified axis

rebin(axis[, mids, factor, inplace])

Rebins one axis of the matrix

remove_negative()

Entries with negative values are set to 0

save(path[, filetype, which])

Save matrix to file

to_lower_bin()

Transform Eg and Ex from mid bin (=default) to lower bin.

to_mid_bin()

Transform Eg and Ex from lower bin to mid bin (=default).

trapezoid(Ex_min, Ex_max, Eg_min[, Eg_max, ...])

Create a trapezoidal cut or mask delimited by the diagonal of the

verify_equdistant(axis)

Runs checks to verify if energy arrays are equidistant

verify_integrity([check_equidistant])

Runs checks to verify internal structure

Attributes Documentation

counts
range_Eg

Returns all indices of Eg

range_Ex

Returns all indices of Ex

shape
state

Methods Documentation

ascii_plot(shape=(5, 5))

Plots a rebinned ascii version of the matrix

Parameters:

shape (tuple, optional) – Shape of the rebinned matrix

calibration()

Calculates the calibration coefficients of the energy axes

Return type:

Dict[str, ndarray]

Returns:

The calibration coefficients in a dictionary.

copy()

Return a deepcopy of the class

Return type:

AbstractArray

cut(axis, Emin=None, Emax=None, inplace=True, Emin_inclusive=True, Emax_inclusive=True)

Cuts the matrix to the sub-interval limits along given axis.

Parameters:
  • axis (Union[int, str]) – Which axis to apply the cut to. Can be 0, “Eg” or 1, “Ex”.

  • Emin (Optional[float]) – Lowest energy to be included. Defaults to lowest energy. Inclusive.

  • Emax (Optional[float]) – Higest energy to be included. Defaults to highest energy. Inclusive.

  • inplace (bool) – If True make the cut in place. Otherwise return a new matrix. Defaults to True

  • Emin_inclusive (bool) – whether the bin containing the lower bin should be included (True) or excluded (False). Defaults to True.

  • Emax_inclusive (bool) – whether the bin containing the higest bin should be included (True) or excluded (False). Defaults to True.

Return type:

Optional[Matrix]

Returns:

None if inplace == False cut_matrix (Matrix): The cut version of the matrix

cut_diagonal(E1=None, E2=None, inplace=True)

Cut away counts to the right of a diagonal line defined by indices

Parameters:
  • E1 (Optional[Iterable[float]]) – First point of intercept, ordered as (Eg, Ex)

  • E2 (Optional[Iterable[float]]) – Second point of intercept

  • inplace (bool) – Whether the operation should be applied to the current matrix, or to a copy which is then returned.

Return type:

Optional[Matrix]

Returns:

The matrix with counts above diagonal removed (if inplace is False).

cut_like(other, inplace=True)

Cut a matrix like another matrix (according to energy arrays)

Parameters:
  • other (Matrix) – The other matrix

  • inplace (bool, optional) – If True make the cut in place. Otherwise return a new matrix. Defaults to True

Returns:

If inplace is False, returns the cut matrix

Return type:

Optional[Matrix]

diagonal_elements()

Iterates over the last non-zero elements .. note:

Assumes that the matrix is diagonal, i.e. that there are no
entries with `Eg > Ex + dE`.
Parameters:
  • mat – The matrix to iterate over

  • Iterator[Tuple[int – Indicies (i, j) over the last non-zero(=diagonal) elements.

  • int]] – Indicies (i, j) over the last non-zero(=diagonal) elements.

Return type:

Iterator[Tuple[int, int]]

fill(Eg, Ex, count=1)

Add counts to the bin containing Eg and Ex. :type Eg: float :param Eg: Eg energy value (x-axis value) :type Eg: float :type Ex: float :param Ex: Ex energy value (y-axis value) :type Ex: float :type count: Optional[float] :param count: Number to add to the bin. Defaults to 1. :type count: float, otional

Return type:

None

fill_and_remove_negative(window_size=20)

Combination of ompy.Matrix.fill_negative() and ompy.Matrix.remove_negative()

Parameters:

window_size (Tuple[int, ndarray]) – See fill_negative. Defaults to 20 (arbitrary)!.

fill_negative(window_size)

Wrapper for ompy.fill_negative_gauss()

has_equal_binning(other, **kwargs)

Check whether other has equal binning as self within precision. :type other: :param other: Matrix to compare to. :type other: Matrix :type **kwargs: :param **kwargs: Additional kwargs to np.allclose.

Returns:

Returns True if both arrays are equal .

Return type:

bool (bool)

Raises:
  • TypeError – If other is not a Matrix

  • ValueError – If any of the bins in any of the arrays are not equal.

index_Eg(E)

Returns the closest index corresponding to the Eg value

Return type:

int

index_Ex(E)

Returns the closest index corresponding to the Ex value

Return type:

int

indices_Eg(E)

Returns the closest indices corresponding to the Eg value

Return type:

ndarray

indices_Ex(E)

Returns the closest indices corresponding to the Ex value

Return type:

ndarray

iter()
Return type:

Iterator[Tuple[int, int]]

line_mask(E1, E2)

Create a mask for above (True) and below (False) a line

Parameters:
Return type:

ndarray

Returns:

The boolean array with counts below the line set to False

Todo

  • Write as a property with memonized output for unchanged matrix

Note

This method and Jørgen’s original method give 2 pixels difference Probably because of how the interpolated line is drawn

load(path, filetype=None)

Load matrix from specified format

Parameters:
  • path (str or Path) – path to file to load

  • filetype (str, optional) – Filetype to load. Has an auto-recognition.

Raises:

ValueError – If filetype is unknown

Return type:

None

plot(*, ax=None, title=None, scale=None, vmin=None, vmax=None, midbin_ticks=False, add_cbar=True, **kwargs)

Plots the matrix with the energy along the axis

Parameters:
  • ax (Optional[Any]) – A matplotlib axis to plot onto

  • title (Optional[str]) – Defaults to the current matrix state

  • scale (Optional[str]) – Scale along the z-axis. Can be either “log” or “linear”. Defaults to logarithmic if number of counts > 1000

  • vmin (Optional[float]) – Minimum value for coloring in scaling

  • scaling (vmax Maximum value for coloring in) –

  • add_cbar (bool) – Whether to add a colorbar. Defaults to True.

  • **kwargs – Additional kwargs to plot command.

Return type:

Any

Returns:

The ax used for plotting

Raises:

ValueError – If scale is unsupported

plot_projection(axis, Emin=None, Emax=None, *, ax=None, normalize=False, scale='linear', **kwargs)

Plots the projection of the matrix along axis

Parameters:
  • axis (Union[int, str]) – The axis to project onto. Can be either of (0, ‘Eg’, ‘x’), (1, ‘Ex’, ‘y’)

  • Emin (Optional[float]) – The minimum energy to be summed over.

  • Emax (Optional[float]) – The maximum energy to be summed over.

  • ax (Optional[Any]) – The axes object to plot onto.

  • normalize (bool) – If True, normalize the counts to 1. Defaults to False.

  • scale (optional, str) – y-scale, i.e log or linear. Defaults to “linear”.

  • **kwargs – Additional kwargs to plot command.

Raises:

ValueError – If axis is not in [0, 1]

Return type:

Any

Returns:

The ax used for plotting

projection(axis, Emin=None, Emax=None, normalize=False)

Returns the projection along the specified axis

Parameters:
  • axis (Union[int, str]) – The axis to project onto. Can be 0 or 1.

  • Emin (optional) – The minimum energy to be summed over.

  • Emax (optional) – The maximum energy to be summed over.

  • normalize (bool) – If True, normalize the counts to 1. Defaults to False.

Raises:

ValueError – If axis is not in [0, 1]

Return type:

Tuple[ndarray, ndarray]

Returns:

The projection and the energies summed onto

rebin(axis, mids=None, factor=None, inplace=True)

Rebins one axis of the matrix

Parameters:
  • axis (Union[int, str]) – the axis to rebin.

  • mids (Optional[Sequence[float]]) – The new mids along the axis. Can not be given alongside ‘factor’.

  • factor (Optional[float]) – The factor by which the step size shall be changed. Can not be given alongside ‘mids’.

  • inplace (bool) – Whether to change the axis and values inplace or return the rebinned matrix.

Return type:

Optional[Matrix]

Returns:

The rebinned Matrix if inplace is ‘False’.

Raises:

ValueError if the axis is not a valid axis.

remove_negative()

Entries with negative values are set to 0

save(path, filetype=None, which='values', **kwargs)

Save matrix to file

Parameters:
  • path (str or Path) – path to file to save

  • filetype (str, optional) – Filetype to save. Has an auto-recognition. Options: [“numpy”, “tar”, “mama”, “txt”]

  • which (str, optional) – Which attribute to save. Default is ‘values’. Options: [“values”, “std”]

  • **kwargs – additional keyword arguments

Raises:
to_lower_bin()

Transform Eg and Ex from mid bin (=default) to lower bin.

to_mid_bin()

Transform Eg and Ex from lower bin to mid bin (=default).

trapezoid(Ex_min, Ex_max, Eg_min, Eg_max=None, inplace=True)
Create a trapezoidal cut or mask delimited by the diagonal of the

matrix

Parameters:
  • Ex_min (float) – The bottom edge of the trapezoid

  • Ex_max (float) – The top edge of the trapezoid

  • Eg_min (float) – The left edge of the trapezoid

  • Eg_max (Optional[float]) – The right edge of the trapezoid used for defining the diagonal. If not set, the diagonal will be found by using the last nonzeros of each row.

Return type:

Optional[Matrix]

Returns:

Cut matrix if ‘inplace’ is True

Todo

-possibility to have inclusive or exclusive cut

verify_equdistant(axis)

Runs checks to verify if energy arrays are equidistant

axis: The axis to project onto.

Can be either of (0, ‘Eg’, ‘x’), (1, ‘Ex’, ‘y’)

Raises:

ValueError – If any check fails

verify_integrity(check_equidistant=False)

Runs checks to verify internal structure

Parameters:

check_equidistant (bool, optional) – Check whether energy array are equidistant spaced. Defaults to False.

Raises:

ValueError – If any check fails