preprocessing

preprocessing pipeline

ardent.preprocessing.__init__.preprocess(data: (<class 'numpy.ndarray'>, <class 'list'>), processes: list)[source]

Perform each preprocessing function in processes, in the order listed, on data if it is an array, or on each element in data if it is a list of arrays.

Parameters:
  • data (np.ndarray, list) -- The array or list of arrays to be preprocessed.
  • processes (list) -- The list of strings, each corresponding to the name of a preprocessing function.
Raises:
  • TypeError -- Raised if data is a list whose elements are not all of type np.ndarray.
  • TypeError -- Raised if data is neither a np.ndarray or a list of np.ndarrays.
  • ValueError -- Raised if processes cannot be cast to a np.ndarray with dtype str.
  • ValueError -- Raised if any element of processes is not a recognized preprocessing function.
Returns:

A copy of data after having each function in processes applied.

Return type:

np.ndarray, list

resampling

ardent.preprocessing.resampling.downsample_image(image, scale_factors, truncate=False)[source]

Downsample an image by averaging.

Parameters:
  • image (np.ndarray) -- The image to be downsampled.
  • scale_factors (int, sequence) -- The per-axis factor by which to reduce the image size.
  • truncate (bool, optional) -- If True, evenly truncates the image down to the nearest multiple of the scale_factor for each axis. Defaults to False.
Raises:

ValueError -- Raised if any of scale_factors is less than 1.

Returns:

A downsampled copy of image.

Return type:

np.ndarray

ardent.preprocessing.resampling.change_resolution_to(image, xyz_resolution, desired_xyz_resolution, pad_to_match_res=True, err_to_higher_res=True, average_on_downsample=True, truncate=False, return_true_resolution=False, **resample_kwargs)[source]

Resamples <image> to get its resolution as close as possible to <desired_xyz_resolution>.

Parameters:
  • image (np.ndarray) -- The image to be resampled, allowing arbitrary dimensions.
  • xyz_resolution (float, sequence) -- The per-axis resolution of <image>.
  • desired_xyz_resolution (float, sequence) -- The desired per-axis resolution of <image> after resampling.
  • pad_to_match_res (bool, optional) -- If True, pads a copy of <image> to guarantee that <desired_xyz_resolution> is achieved. Defaults to True.
  • err_to_higher_res (bool, optional) -- If True and <pad_to_match_res> is False, rounds the shape of the new image up rather than down. Defaults to True.
  • average_on_downsample (bool, optional) -- If True, performs downsample_image on a copy of <image> before resampling to prevent aliasing. It scales the image by the largest integer possible along each axis without reducing the resolution past the final resolution. Defaults to True.
  • truncate (bool, optional) -- A kwarg passed to downsample_image. If true, evenly truncates the image down to the nearest multiple of the scale_factor for each axis. Defaults to False.
  • return_true_resolution (bool, optional) -- If True, rather than just returning the resampled image, returns a tuple containing the resampled image and its actual resolution. Defaults to False.
Returns:

A resampled copy of <image>.

If <return_true_resolution> was provided as True, then the return value is a tuple containing the resampled copy of <image> and its actual resolution.

Return type:

np.ndarray, tuple

ardent.preprocessing.resampling.change_resolution_by(image, xyz_scales, xyz_resolution=1, pad_to_match_res=True, err_to_higher_res=True, average_on_downsample=True, truncate=False, return_true_resolution=False, **resample_kwargs)[source]

Resample image such that its resolution is scaled by 1 / <xyz_scales>[dim] or abs(xyz_scales[dim]) if xyz_scales[dim] is negative, in each dimension dim.

Parameters:
  • image (np.ndarray) -- The image to be resampled, allowing arbitrary dimensions.
  • xyz_scales (float, sequence) --

    The per-axis factors by which to adjust the resolution of <image>. Negative values are treated as the reciprocal of their positive counterparts.

    xyz_scales[dim] > 1 implies upsampling - increasing resolution and image size. xyz_scales[dim] = 1 implies unity - no change in resolution for this dimension. xyz_scales[dim] < 1 implies downsampling - decreasing resolution and image size. xyz_scales[dim] < 0 implies downsampling by this factor - cast to -1 / xyz_scales[dim].

    Examples: xyz_scales[dim] = 2 --> upsample by 2 xyz_scales[dim] = 1 --> do nothing xyz_scales[dim] = 1/2 --> downsample by 2 xyz_scales[dim] = -3 --> downsample by 3 xyz_scales[dim] = -1/5 --> upsample by 5

  • xyz_resolution (float, sequence) -- The per-axis resolution of <image>. Defaults to 1.
  • pad_to_match_res (bool) -- If True, pads a copy of <image> to guarantee that <desired_xyz_resolution> is achieved. Defaults to True.
  • err_to_higher_res (bool) -- If True and <pad_to_match_res> is False, rounds the shape of the new image up rather than down. Defaults to True.
  • average_on_downsample (bool) -- If True, performs downsample_image on a copy of <image> before resampling to prevent aliasing. It scales the image by the largest integer possible along each axis without reducing the resolution past the final resolution. Defaults to True.
  • truncate (bool) -- A kwarg passed to downsample_image. If true, evenly truncates the image down to the nearest multiple of the scale_factor for each axis. Defaults to False.
  • return_true_resolution (bool) -- If True, rather than just returning the resampled image, returns a tuple containing the resampled image and its actual resolution. Defaults to False.
Returns:

A resampled copy of <image>.

If <return_true_resolution> was provided as True, then the return value is a tuple containing the resampled copy of <image> and its actual resolution.

Return type:

np.ndarray, tuple

normalization

ardent.preprocessing.normalization.cast_to_typed_array(data, dtype=<class 'float'>)[source]

Returns a copy of data cast as a np.ndarray of type dtype.

Parameters:
  • data (np.ndarray) -- The array to be cast.
  • dtype (type, optional) -- The dtype to cast data to. Defaults to float. Defaults to float.
Returns:

A copy of data cast to type dtype.

Return type:

np.ndarray

ardent.preprocessing.normalization.normalize_by_MAD(data)[source]

Returns a copy of data divided by its mean absolute deviation.

Parameters:data (np.ndarray) -- The array to be normalized.
Returns:A copy of data divided by its mean absolute deviation.
Return type:np.ndarray
ardent.preprocessing.normalization.center_to_mean(data)[source]

Returns a copy of data subtracted by its mean.

Parameters:data (np.ndarray) -- The array to be subtracted from.
Returns:A copy of data subtracted by its mean.
Return type:np.ndarray
ardent.preprocessing.normalization.pad(data, pad_width=5, mode='constant', constant_values=0)[source]

Returns a padded copy of data.

Parameters:
  • data (np.ndarray) -- The array to be padded.
  • pad_width (int, optional) -- The amount by which to pad. Defaults to 5.
  • mode (str, optional) -- The padding mode used in np.pad. Defaults to 'constant'.
  • constant_values (float, optional) -- The values to use in padding if mode='constant'. Defaults to 0.
Returns:

The padded copy of data.

Return type:

np.ndarray