Utilities

Utility functions for array processing and analysis.

objscale.coarsen_array(array, factor)[source]

Coarsen a given array by a specified factor by averaging along both dimensions.

This function takes an input array and reduces it by a given factor along both the x and y dimensions. The coarsening is achieved by summing ‘superpixel’ regions of the original array and dividing by the number of pixels in each region. The resulting coarsened array has reduced resolution.

Parameters:
  • array (np.ndarray) – The input array to be coarsened.

  • factor (int) – The coarsening factor for reducing the array resolution. Must be a positive integer.

Returns:

The coarsened array with reduced resolution.

Return type:

np.ndarray

Raises:

ValueError – If an even coarsening factor is provided while attempting to create a binary array, as this may lead to rounding issues.

Examples

>>> original_array = np.array([[1, 2], [3, 4]])
>>> coarsened = coarsen_array(original_array, factor=2)
>>> coarsened
array([2.5])
objscale.linear_regression(x, y)[source]

Perform linear regression and return coefficients with 95% confidence errors.

Parameters:
  • x (np.ndarray) – Independent variable values.

  • y (np.ndarray) – Dependent variable values.

Returns:

  • coefficients (tuple of float) – (slope, y-intercept) from linear regression.

  • errors (tuple of float) – (error_slope, error_y_intercept) for 95% confidence.

Raises:

TypeError – If x or y are not numpy arrays.

objscale.encase_in_value(array, value=nan, dtype=<class 'numpy.float32'>, n_deep=1)[source]

Add a border of specified value around a 2-D array.

Parameters:
  • array (np.ndarray) – 2-D input array.

  • value (scalar, default=np.nan) – Value to append on the edge.

  • dtype (np.dtype, default=np.float32) – Data type of the resulting array.

  • n_deep (int, default=1) – Number of layers deep to add around the edge.

Returns:

Same as input but with a layer ‘n_deep’ of ‘value’ all around the edge.

Return type:

np.ndarray