Utility functions (utils)#

Functions:

mask_change_convention(mask[, ...])

Convert an array from one 'mask' convention to another.

masks_change_convention(masks[, ...])

Perform convention change of sequence of masks

labels_to_masks(labels[, features, ...])

Convert labels array to list of masks

masks_to_labels(masks[, features, ...])

Convert list of masks to labels

bfill(arr[, axis, limit])

Inverse of ffill

get_lnz_iter(lnz, x)

Create a lnz_iter object for varying a single lnz

distance_matrix(mask[, convention])

Create matrix of distances from elements of mask to nearest background point

lnpimasked_to_dataset(data[, keys])

Convert a lnPiMasked object into as Dataset.

dataset_to_lnpimasked(ds[, lnpi_name, ...])

Convert a Dataset to a lnPiMasked object.

rootresults_to_rootresultdict(r, residual)

Convert scipy.optimize.RootResults to typed dictionary

array_to_scalar(x)

Convert array to scalar.

Classes:

RootResultDict

Interface to scipy.optimize.RootResults.

lnpy.utils.mask_change_convention(mask, convention_in='image', convention_out='masked')[source]#

Convert an array from one ‘mask’ convention to another.

Parameters:
  • masks (None or ndarray of bool) – Masking array.

  • convention_in, convention_out (string or bool) – Convention for input and output. Convention for mask. Allowable values are:

    • 'image' ``or ``True : True values included, False values excluded. This is the normal convention in scipy.ndimage.

    • 'masked' or False: False values are included, True values are excluded. This is the convention in numpy.ma

Returns:

ndarray – New ‘mask’ array with specified convention.

lnpy.utils.masks_change_convention(masks, convention_in='image', convention_out='masked')[source]#

Perform convention change of sequence of masks

Parameters:
  • masks (sequence of array-like) – masks[i] is the ‘ith’ mask

  • convention_in, convention_out (string or bool or None) – Convention for input and output. Convention for mask. Allowable values are:

    • ‘image’ or True : True values included, False values excluded. This is the normal convention in scipy.ndimage.

    • ‘masked’ or False: False values are included, True values are excluded. This is the convention in numpy.ma

    If None, then pass return input mask

Returns:

new_masks (list of ndarray) – New ‘masks’ array with specified convention.

lnpy.utils.labels_to_masks(labels, features=None, include_boundary=False, convention='image', check_features=True, **kwargs)[source]#

Convert labels array to list of masks

Parameters:
  • labels (ndarray of int) – Each unique value i in labels indicates a mask. That is labels == i.

  • features (sequence of int) – If specified, extract only those locations where labels == feature for all values feature in features. That is, select a subset of unique label values.

  • include_boundary (bool) – if True, include boundary regions in output mask

  • convention (string or bool) – Convention for mask. Allowable values are:

    • ‘image’ or TrueTrue values included, False values excluded.

      This is the normal convention in scipy.ndimage.

    • ‘masked’ or False: False values are included, True values are excluded.

      This is the convention in numpy.ma

  • check_features (bool) – if True, then make sure each feature is in labels

  • **kwargs – arguments to find_boundary if include_boundary is True default to mode=’outer’, connectivity=labels.ndim

Returns:

  • output (list of ndarray of bool) – list of mask arrays, each with same shape as labels, with mask convention convention.

  • features (list) – features

lnpy.utils.masks_to_labels(masks, features=None, convention='image', dtype=<class 'numpy.int64'>)[source]#

Convert list of masks to labels

Parameters:
  • masks (list of array-like of bool) – list of mask arrays.

  • features (sequence of int) – If specified, extract only those locations where labels == feature for all values feature in features. That is, select a subset of unique label values.

  • convention (string or bool) – Convention for mask. Allowable values are:

    • ‘image’ or TrueTrue values included, False values excluded.

      This is the normal convention in scipy.ndimage.

    • ‘masked’ or False: False values are included, True values are excluded.

      This is the convention in numpy.ma

Returns:

ndarray – Label array.

See also

labels_to_masks

lnpy.utils.bfill(arr, axis=-1, limit=None)[source]#

Inverse of ffill

lnpy.utils.get_lnz_iter(lnz, x)[source]#

Create a lnz_iter object for varying a single lnz

Parameters:
  • lnz (list) – list with one element equal to None. This is the component which will be varied For example, lnz=[lnz0,None,lnz2] implies use values of lnz0,lnz2 for components 0 and 2, and vary component 1

  • x (ndarray) – values to insert for variable component

Returns:

ndarray – Shape (len(x),len(lnz)). array with rows [lnz0,lnz1,lnz2]

lnpy.utils.distance_matrix(mask, convention='image')[source]#

Create matrix of distances from elements of mask to nearest background point

Parameters:
  • mask (array-like) – image mask

  • convention (str or bool, default 'image') – mask convention

Returns:

distance (ndarray) – Same shape as mask. Distance from possible feature elements to background

lnpy.utils.lnpimasked_to_dataset(data, keys=('lnpi', 'PE'))[source]#

Convert a lnPiMasked object into as Dataset.

Parameters:

data (lnPiMasked)

Returns:

output (Dataset)

lnpy.utils.dataset_to_lnpimasked(ds, lnpi_name='lnpi', pe_name='PE', extra_kws=None, **kwargs)[source]#

Convert a Dataset to a lnPiMasked object.

Parameters:
  • ds (Dataset)

  • lnpi_name, pe_name (str) – Names of ‘lnPi’ and ‘PE’ parameters

  • extra_kws (mapping, optional) – parameter extra_kws. Note that if pe_name found in ds, then will add it to extra_kws.

Returns:

lnpi (lnPiMasked)

class lnpy.utils.RootResultDict[source]#

Bases: dict

Interface to scipy.optimize.RootResults.

lnpy.utils.rootresults_to_rootresultdict(r, residual)[source]#

Convert scipy.optimize.RootResults to typed dictionary

lnpy.utils.array_to_scalar(x)[source]#

Convert array to scalar.

If x is an ndarray, convert to float of 0-d, or extract first element (of flattened array ) if N-d.