hyperspy.misc.array_tools module

hyperspy.misc.array_tools._linear_bin(dat, scale, crop=True, dtype=None)

Binning of the spectrum image by a non-integer pixel value.

Parameters:
  • originalSpectrum (numpy.array) –

  • scale (a list of floats) – For each dimension specify the new:old pixel ratio, e.g. a ratio of 1 is no binning; a ratio of 2 means that each pixel in the new spectrum is twice the size of the pixels in the old spectrum. The length of the list should match the dimensions of the data.

  • crop (bool, default True) –

    When binning by a non-integer number of pixels it is likely that the final row in each dimension contains less than the full quota to fill one pixel. e.g. 5*5 array binned by 2.1 will produce two rows containing 2.1 pixels and one row containing only 0.8 pixels worth. Selection of crop=’True’ or crop=’False’ determines whether or not this ‘black’ line is cropped from the final binned array or not.

    Please note that if crop=False is used, the final row in each dimension may appear black, if a fractional number of pixels are left over. It can be removed but has been left optional to preserve total counts before and after binning.

Returns:

with new dimensions width/scale for each dimension in the data.

Return type:

np.array

hyperspy.misc.array_tools._numba_histogram(data, bins, ranges)

Numba histogram computation requiring native endian datatype.

hyperspy.misc.array_tools._requires_linear_rebin(arr, scale)

Returns True if linear_rebin is required. :param arr: numpy array to rebin :type arr: array :param scale: rebinning factors :type scale: tuple

hyperspy.misc.array_tools.are_aligned(shape1, shape2)

Check if two numpy arrays are aligned.

Parameters:
  • shape1 (iterable) –

  • shape2 (iterable) –

Returns:

isaligned

Return type:

bool

hyperspy.misc.array_tools.dict2sarray(dictionary, sarray=None, dtype=None)

Populates a struct array from a dictionary

Parameters:
  • dictionary (dict) –

  • sarray (struct array or None) – Either sarray or dtype must be given. If sarray is given, it is populated from the dictionary.

  • dtype (None, numpy dtype or dtype list) – If sarray is None, dtype must be given. If so, a new struct array is created according to the dtype, which is then populated.

Return type:

Structure array

hyperspy.misc.array_tools.get_array_memory_size_in_GiB(shape, dtype)

Given the size and dtype returns the amount of memory that such an array needs to allocate

Parameters:
  • shape (tuple) –

  • dtype (data-type) – The desired data-type for the array.

hyperspy.misc.array_tools.get_signal_chunk_slice(index, chunks)

Convenience function returning the chunk slice in signal space containing the specified index.

Parameters:
  • index (int or tuple of int) – Index determining the wanted chunk.

  • chunks (tuple) – Dask array chunks.

Returns:

Slice containing the index x,y.

Return type:

slice

hyperspy.misc.array_tools.homogenize_ndim(*args)

Given any number of arrays returns the same arrays reshaped by adding facing dimensions of size 1.

hyperspy.misc.array_tools.numba_closest_index_ceil(axis_array, value_array)

For each value in value_array, find the closest larger value in axis_array and return the result as a numpy array of the same shape as value_array.

Parameters:
  • axis_array (numpy array) –

  • value_array (numpy array) –

Return type:

numpy array

hyperspy.misc.array_tools.numba_closest_index_floor(axis_array, value_array)

For each value in value_array, find the closest smaller value in axis_array and return the result as a numpy array of the same shape as value_array.

Parameters:
  • axis_array (numpy array) –

  • value_array (numpy array) –

Return type:

numpy array

hyperspy.misc.array_tools.numba_closest_index_round(axis_array, value_array)

For each value in value_array, find the closest value in axis_array and return the result as a numpy array of the same shape as value_array. Use round half towards zero strategy for rounding float to interger.

Parameters:
  • axis_array (numpy array) –

  • value_array (numpy array) –

Return type:

numpy array

hyperspy.misc.array_tools.numba_histogram(data, bins, ranges)
Parameters:
  • data (numpy array) – Input data. The histogram is computed over the flattened array.

  • bins (int) – Number of bins

  • ranges ((float, float)) – The lower and upper range of the bins.

Returns:

hist – The values of the histogram.

Return type:

array

hyperspy.misc.array_tools.rebin(a, new_shape=None, scale=None, crop=True, dtype=None)

Rebin data into a smaller or larger array based on a linear interpolation. Specify either a new_shape or a scale. Scale of 1 means no binning. Scale less than one results in up-sampling.

Parameters:
  • a (numpy array) – The array to rebin.

  • new_shape (list (of floats or integer) or None) – For each dimension specify the new_shape. This will internally be converted into a scale parameter.

  • scale (list (of floats or integer) or None) – For each dimension, specify the new:old pixel ratio, e.g. a ratio of 1 is no binning and a ratio of 2 means that each pixel in the new spectrum is twice the size of the pixels in the old spectrum. The length of the list should match the dimension of the Signal’s underlying data array. Note : Only one of `scale` or `new_shape` should be specified, otherwise the function will not run

  • crop (bool) – Whether or not to crop the resulting rebinned data (default is True). When binning by a non-integer number of pixels it is likely that the final row in each dimension will contain fewer than the full quota to fill one pixel. For example, a 5*5 array binned by 2.1 will produce two rows containing 2.1 pixels and one row containing only 0.8 pixels. Selection of crop=True or crop=False determines whether or not this “black” line is cropped from the final binned array or not. Please note that if ``crop=False`` is used, the final row in each dimension may appear black if a fractional number of pixels are left over. It can be removed but has been left to preserve total counts before and after binning.

  • dtype ({None, numpy.dtype, "same"}) – Specify the dtype of the output. If None, the dtype will be determined by the behaviour of numpy.sum(), if “same”, the dtype will be kept the same. Default is None.

Return type:

numpy array

Examples

>>> a=rand(6,4); b=rebin(a,scale=(3,2))
>>> a=rand(6); b=rebin(a,scale=(2,))

Notes

Fast re_bin function Adapted from scipy cookbook If rebin function fails with error stating that the function is ‘not binned and therefore cannot be rebinned’, add binned to axes parameters with: >>> s.axes_manager[axis].is_binned = True

hyperspy.misc.array_tools.round_half_away_from_zero(array, decimals=0)

Round input array using “half away from zero” strategy.

Parameters:
  • array (ndarray) – Input array.

  • decimals (int, optional) – Number of decimal places to round to (default: 0).

Returns:

rounded_array – An array of the same type as a, containing the rounded values.

Return type:

ndarray

hyperspy.misc.array_tools.round_half_towards_zero(array, decimals=0)

Round input array using “half towards zero” strategy.

Parameters:
  • array (ndarray) – Input array.

  • decimals (int, optional) – Number of decimal places to round to (default: 0).

Returns:

rounded_array – An array of the same type as a, containing the rounded values.

Return type:

ndarray

hyperspy.misc.array_tools.sarray2dict(sarray, dictionary=None)

Converts a struct array to an ordered dictionary

Parameters:
  • sarray (struct array) –

  • dictionary (None or dict) – If dictionary is not None the content of sarray will be appended to the given dictonary

Return type:

Ordered dictionary