ximage package#
Subpackages#
Module contents#
ximage software.
- ximage.label(xr_obj, *, variable=None, core_dims=None, min_value_threshold=-inf, max_value_threshold=inf, min_area_threshold=1, max_area_threshold=inf, footprint=None, sort_by='area', sort_decreasing=True, labeled_comprehension_kwargs=None, label_name='label')[source][source]#
Compute labels and and add as a coordinates to an xarray object.
- Parameters:
xr_obj (xarray.DataArray or xarray.Dataset) – xarray object.
variable (str, optional) – Dataset variable to exploit to derive the labels array. Must be specified only if the input object is an xarray.Dataset.
core_dims (tuple of str, optional) – Names of the two dimensions along which the labeling is applied. If the xarray DataArray is two-dimensional and
core_dimsis not provided, the core dimensions are inferred automatically from DataArray.dims. If the xarray DataArray has more than two dimensions,core_dimsmust be specified explicitly. In this case, labeling is applied independently over all remaining (non-core) dimensions. Example: for a 3D DataArray with dimensions(x, y, time), usecore_dims=("x", "y")to apply labeling to each timestep.min_value_threshold (float, optional) – The minimum value to define the interior of a label. The default is
-np.inf.max_value_threshold (float, optional) – The maximum value to define the interior of a label. The default is
np.inf.min_area_threshold (float, optional) – The minimum number of connected pixels to be defined as a label. The default is 1.
max_area_threshold (float, optional) – The maximum number of connected pixels to be defined as a label. The default is
np.inf.footprint (int, numpy.ndarray or None, optional) – This argument enables to dilate the mask derived after applying min_value_threshold and max_value_threshold. If
footprint = 0orNone, no dilation occur. Iffootprintis a positive integer, it create adisk(footprint)Iffootprintis a 2D array, it must represent the neighborhood expressed as a 2-D array of 1’s and 0’s. The default isNone(no dilation).sort_by (callable or str, optional) – A function or statistics to define the order of the labels. Valid string statistics are
"area","maximum","minimum","mean","median","sum","standard_deviation","variance". The default is"area".sort_decreasing (bool, optional) – If
True, sort labels by decreasingsort_byvalue. The default isTrue.labeled_comprehension_kwargs (dict, optional) – Additional arguments to be passed to dask_image.ndmeasure.labeled_comprehension. if
sort_byis a callable.
- Returns:
xr_obj – xarray object with the new label coordinate. In the label coordinate, non-labels values are set to np.nan.
- Return type:
- ximage.label_patches(xr_obj, label_name, patch_size, variable=None, n_patches=None, n_labels=None, labels_id=None, highlight_label_id=True, centered_on='max', padding=0, n_patches_per_label=None, n_patches_per_partition=1, partitioning_method=None, n_partitions_per_label=None, kernel_size=None, buffer=0, stride=None, include_last=True, ensure_slice_size=True, debug=False, verbose=False)[source]#
Routines to extract patches around labels.
Create a generator extracting (from a prelabeled xarray.Dataset) a patch around:
a label point
a label bounding box
If
centered_onis specified, output patches are guaranteed to have equal shape ! Ifcentered_onis not specified, output patches are guaranteed to have only have a minimum shape !If you want to extract the patch around the label bounding box,
centered_onmust not be specified.If you want to extract the patch around a label point, the
centered_onmethod must be specified. If the identified point is close to an array boundary, the patch is expanded toward the valid directions.Tiling or sliding enables to split/slide over each label and extract multiple patch for each tile.
tiling=True-centered_on = "centroid"(tiling around labels bbox) -centered_on = "center_of_mass"(better coverage around label)sliding=True-centered_on = "center_of_mass"(better coverage around label) (further data coverage)Only one parameter between
n_patchesandlabels_idcan be specified.- Parameters:
xr_obj (xarray.Dataset) – xarray.Dataset with a label array named
label_name.label_name (str) – Name of the variable/coordinate representing the label array.
patch_size (int or tuple) – The dimensions of the n-dimensional patch to extract. Only positive values (>1) are allowed. The value -1 can be used to specify the full array dimension shape. If the
centered_onmethod is not'label_bbox', all output patches are ensured to have the same shape. Otherwise, ifcentered_on='label_bbox', thepatch_sizeargument defines defined the minimum n-dimensional shape of the output patches. Ifint, the value is applied to all label array dimensions. Iflistortuple, the length must match the number of dimensions of the array. If adict, the dictionary must have has keys the label array dimensions.n_patches (int, optional) – Maximum number of patches to extract. The default (
None) enable to extract all available patches allowed by the specified patch extraction criteria.labels_id (list, optional) – List of labels for which to extract the patch. If
None, it extracts the patches by label order(1, 2, 3, ...)The default isNone.n_labels (int, optional) – The number of labels for which extract patches. If
None(the default), it extract patches for all labels. This argument can be specified only iflabels_idis unspecified !highlight_label_id (bool, optional) – If
True, thelabel_namearray of each patch is modified to contain only thelabel_idused to select the patch.variable (str, optional) – Dataset variable to use to identify the patch center when centered_on is defined. This is required only for
centered_on='max',centered_on='min'or the custom function.centered_on (str or callable, optional) –
The centered_on method characterize the point around which the patch is extracted. Valid pre-implemented centered_on methods are
'label_bbox','max','min','centroid','center_of_mass','random'. The default method is'max'.If
label_bboxit extract the patches around the (padded) bounding box of the label. Iflabel_bbox, the output patch sizes are only ensured to have a minimumpatch_size, and will likely be of different size. Otherwise, the other methods guarantee that the output patches have a common shape.If
centered_onis'max','min'or a custom function, thevariableargument must be specified. Ifcentered_onis a custom function, it must: - returnNoneif all array values are non-finite (i.enp.nan) - return a tuple with same length as the array shape.padding (int, tuple or dict, optional) – The padding to apply in each direction around a label prior to partitioning (tiling/sliding) or direct patch extraction. The default, 0, applies 0 padding in every dimension. Negative padding values are allowed ! If
int, the value is applied to all label array dimensions. Iflistortuple, the length must match the number of dimensions of the array. If adict, the dictionary must have has keys the label array dimensions.n_patches_per_label (int, optional) – The maximum number of patches to extract for each label. The default (
None) enables to extract all the available patches per label. If specified,n_patches_per_labelmust be larger thann_patches_per_partition!n_patches_per_partition – The maximum number of patches to extract from each label partition. The default values is 1. This method can be specified only if
centered_on='random'or a callable.int – The maximum number of patches to extract from each label partition. The default values is 1. This method can be specified only if
centered_on='random'or a callable.optional – The maximum number of patches to extract from each label partition. The default values is 1. This method can be specified only if
centered_on='random'or a callable.partitioning_method (str) – Whether to retrieve
'tiling'or'sliding'slices. If'tiling', partition start slices are separated bystride+kernel_size. If'sliding', partition start slices are separated by stride.n_partitions_per_label (int, optional) – The maximum number of partitions to extract for each label. The default (
None) enables to extract all the available partitions per label.stride (int, tuple or dict, optional) – If
partitioning_method = 'sliding', defaultstrideis set to 1. Ifpartitioning_method = 'tiling', defaultstrideis set to 0. Step size between slices. Whenpartitioning_method='tiling', a positive stride make partition slices to not overlap and not touch, while a negative stride make partition slices to overlap bystrideamount. Ifstride=0, the partition slices are contiguous (no spacing between partitions). Whenpartitioning_method='sliding', only a positive stride (>= 1) is allowed. Ifint, the value is applied to all label array dimensions. Iflistortuple, the length must match the number of dimensions of the array. If adict, the dictionary must have has keys the label array dimensions.kernel_size (int, tuple or dict, optional) – The shape of the desired partitions. Only positive values (>1) are allowed. The value
-1can be used to specify the full array dimension shape. Ifint, the value is applied to all label array dimensions. Iflistortuple, the length must match the number of dimensions of the array. If adict, the dictionary must have has keys the label array dimensions.buffer (int, tuple or dict, optional) – The default is
0. Value by which to enlarge a partition on each side. The final partition size should bekernel_size+buffer. Ifpartitioning_method='tiling'andstride=0, a positive buffer value corresponds to the amount of overlap between each partition. Depending onmin_startandmax_stopvalues, buffering might cause border partitions to not have same sizes. Ifint, the value is applied to all label array dimensions. Iflistortuple, the length must match the number of dimensions of the array. If adict, the dictionary must have has keys the label array dimensions.include_last (bool, optional) – Whether to include the last partition if it does not match the
kernel_size. The default isTrue.ensure_slice_size (bool, optional) – Used only if include_last is
True. IfFalse, the last partition will not have the specifiedkernel_size. IfTrue, the last partition is enlarged to the specifiedkernel_sizeby tentatively expanding it on both sides (accounting formin_startandmax_stop).
- Yields:
(xarray.Dataset or xarray.DataArray) – A xarray object patch.