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:
- Returns:
The input array, cropped to the desired size around its center.
- Return type:
- hsr4hci.general.crop_or_pad(array, size, **kwargs)[source]#
Take an
arrayand a targetsizeand either crop or pad thearrayto 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, useconstant_valuesto specify the padding value (default: 0).
- Returns:
The original
array, cropped or padded to match thesize.- Return type:
- hsr4hci.general.fast_corrcoef(x, y)[source]#
A fast(er) way to compute the correlation coefficient between the variables
xandy, based onnumpy.einsum().
- 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 givenvalue.Original source: https://stackoverflow.com/a/12141511/4100721
- Parameters:
- 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:
- 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}.
- 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
- 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
arrayis 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 (usearray.reshape()to add a new dimension first for this). Also, the new shape must be greater or equal to the currentarray.shapefor 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 exampleconstant_valuesto determine the value with which the array is padded.
- Returns:
A copy of the given
arraythat has been padded to the given targetshape.- Return type:
- 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 sizestacking_factor) along the given axis, and the givenstacking_functionis 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 givenstacking_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()ornumpy.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
stackwhere blocks of the specified size have been merged using the givenstacking_function.- Return type:
- hsr4hci.general.rotate_position(position, center, angle)[source]#
Take a
positionand rotate it around the givencenterfor the givenangle. Either thepositionor theanglecan 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
floator 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:
- 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:
- 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 2Doffset.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
imageshifted by the amount specified inoffset.- Return type: