hyperspy.misc.array_tools module

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

Check if two numpy arrays are aligned.

Parameters

shape2 (shape1,) –

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.

Returns

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.homogenize_ndim(*args)

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

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

Rebin array.

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

Parameters
  • a (numpy array) –

  • new_shape (a list of floats or integer, default None) – For each dimension specify the new_shape of the np.array. This will then be converted into a scale.

  • scale (a list of floats or integer, default 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 numpy array. *Note : Only one of scale or new_shape should be specified otherwise the function will not run*

  • 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 to preserve total counts before and after binning.

Returns

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 metadata with: >>> s.metadata.Signal.binned = True

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

Returns

Return type

Ordered dictionary