general#

Methods for general purpose usage (e.g., cropping arrays).

hsr4hci.general.crop_center(array, size)[source]#

Crop an n-dimensional array to the given size around its center.

Example

>>> crop_center(np.array([1, 2, 3, 4, 5]), (3,))
array([2, 3, 4])
Parameters:
  • array (ndarray) – The numpy array to be cropped.

  • size (Tuple[int, ...]) – A tuple containing the size of the cropped array. To not crop along a specific axis, you can specify the size of that axis as -1.

Returns:

The input array, cropped to the desired size around its center.

Return type:

ndarray

hsr4hci.general.crop_or_pad(array, size, **kwargs)[source]#

Take an array and a target size and either crop or pad the array to match the given size.

Parameters:
  • array (ndarray) – A numpy array.

  • size (Tuple[int, ...]) – A tuple of integers specifying the target size.

  • kwargs (Any) – Additional keyword arguments that are passed to pad_array_to_shape(). For example, use constant_values to specify the padding value (default: 0).

Returns:

The original array, cropped or padded to match the size.

Return type:

ndarray

hsr4hci.general.fast_corrcoef(x, y)[source]#

A fast(er) way to compute the correlation coefficient between the variables x and y, based on numpy.einsum().

Parameters:
  • x (ndarray) – A numpy array with realizations of the random variable X.

  • y (ndarray) – A numpy array with realizations of the random variable Y.

Returns:

The correlation coefficient Corr(X, Y).

Return type:

float

hsr4hci.general.find_closest(sequence, value)[source]#

Given a sorted sequence, find the entry (and its index) in it that is the closest to the given value.

Original source: https://stackoverflow.com/a/12141511/4100721

Parameters:
  • sequence (Sequence) – A sequence (= a list, tuple or numpy array).

  • value (Any) – A numeric value (i.e., usually an int or float).

Returns:

A tuple (index, value) where value is the entry in sequence that is the closest to value, and index is its index: sequence[index] == value.

Return type:

Tuple[int, Any]

hsr4hci.general.flatten_nested_dict(d, parent_key='', sep='_')[source]#

Flatten a nested dictionary into a dictionary with only 1 level.

Original source: https://stackoverflow.com/a/6027615/4100721

Example

>>> flatten_nested_dict({'a': {'b': 5}, 'c': 2})
{'a_b': 5, 'c': 2}.
Parameters:
  • d (dict) – Dictionary to be flattened.

  • parent_key (str) – Key of the parent of d; this is needed because the function calls itself recursively.

  • sep (str) – The separator to use for merging keys.

Returns:

A flattened version of the input dictionary.

Return type:

dict

hsr4hci.general.get_from_nested_dict(nested_dict, location)[source]#

Get a value from a nested dictionary at a given location, described by a sequence of keys.

Example

>>> dictionary = {'a': {'b': 42}}
>>> get_from_nested_dict(dictionary, ['a', 'b'])
42
Parameters:
  • nested_dict (dict) – A nested dictionary.

  • location (Sequence) – The location within the nested dictionary, described by a sequence (i.e., a list or tuple) of keys used to access the target value.

Returns:

The value of the nested_dict at the specified location.

Return type:

Any

hsr4hci.general.pad_array_to_shape(array, shape, **kwargs)[source]#

Pad a numpy array to a given target shape. (In a way, this is the complement to numpy.pad(), which adds a given amount of padding.) By default, zero-padding is used.

Parameters:
  • array (ndarray) – An n-dimensional numpy array.

  • shape (Tuple[int, ...]) – The tuple of integers specifying the target shape to which the array is padded. The length of this tuple must match exactly the number of dimensions of array, i.e., this function will not automatically add new axes (use array.reshape() to add a new dimension first for this). Also, the new shape must be greater or equal to the current array.shape for every axis, i.e., this function cannot be used for negative padding (cropping).

  • kwargs (Any) – Additional keyword arguments that are passed to numpy.pad(); for example constant_values to determine the value with which the array is padded.

Returns:

A copy of the given array that has been padded to the given target shape.

Return type:

ndarray

hsr4hci.general.prestack_array(array, stacking_factor, stacking_function=np.mean, axis=0)[source]#

Perform β€œpre-stacking” (= temporal binning) on a given array: The array is split into blocks (each of size stacking_factor) along the given axis, and the given stacking_function is applied to each block (again along the specified axis). The results for each block are combined and returned, resulting in a numpy array that has the same shape as the input array, except that the specified axis has been reduced by the given stacking_factor.

Parameters:
  • array (ndarray) – A numpy array containing the input array.

  • stacking_factor (int) – An integer defining how many elements of the input go into one block and are combined for the output.

  • stacking_function (Callable) – The function to be used for combining the blocks. For most cases, this will be numpy.mean() or numpy.median().

  • axis (int) – Axis along which the stacking operation is performed. By default, we stack along the time axis, which by convention is the first axis (0).

Returns:

A version of the input stack where blocks of the specified size have been merged using the given stacking_function.

Return type:

ndarray

hsr4hci.general.rotate_position(position, center, angle)[source]#

Take a position and rotate it around the given center for the given angle. Either the position or the angle can also be a numpy array (but not both!).

Parameters:
  • position (Union[Tuple[float, float], ndarray]) – The initial position as a 2-tuple (x, y), or as a numpy array of shape (2, n_positions).

  • center (Tuple[float, float]) – The center of the rotation as a 2-tuple (center_x, center_y).

  • angle (Union[float, ndarray]) – The rotation angle in degrees (not radian); either as a float or as a numpy array of shape (n_angles, ).

Returns:

The rotated position, either as a 2-tuple, or as a numpy array of shape (2, n_positions).

Return type:

Union[Tuple[float, float], ndarray]

hsr4hci.general.set_in_nested_dict(nested_dict, location, value)[source]#

Set a value at a given location (described by a sequence of keys) in a nested dictionary.

Example

>>> dictionary = {'a': {'b': 42}}
>>> set_in_nested_dict(dictionary, ['a', 'b'], 23)
>>> dictionary
{'a': {'b': 23}}
Parameters:
  • nested_dict (dict) – A nested dictionary.

  • location (Sequence) – The target location within the nested dictionary, described by a sequence (i.e., a list or tuple) of keys used to access the target value.

  • value (Any) – The value to be written to the target location.

Return type:

None

hsr4hci.general.shift_image(image, offset, interpolation='bilinear', mode='constant')[source]#

Function to shift a 2D array (i.e., an image) by a given 2D offset.

This function is essentially a simplified port of the PynPoint function of the same name: pynpoint.util.image.shift_image().

Parameters:
  • image (ndarray) – A 2D numpy array containing the image to be shifted.

  • offset (Tuple[float, float]) – A tuple of floats (x_shift, y_shift) containing the amount (in pixels) how much the image should be shifted.

  • interpolation (str) – The interpolation method to be used. Must be one of the following: β€œspline”, β€œbilinear”. Default is β€œbilinear” because it is flux-preserving.

  • mode (str) – The mode parameter determines how the input array is extended beyond its boundaries. See scipy.ndimage.shift() for a full documentation.

Returns:

The image shifted by the amount specified in offset.

Return type:

ndarray