Array Utils (empanada.array_utils)#

empanada.array_utils.box_area(boxes)[source]#

Computes the area/volume of a set of boxes.

Parameters:

boxes – Array of size (n, 4) or (n, 6) where bounding box is defined as (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

Returns:

Array of (n,) of each box area/volume.

Return type:

areas

empanada.array_utils.box_intersection(boxes1, boxes2=None)[source]#

Computes the pairwise intersection area/volume between two arrays of bounding boxes.

Parameters:
  • boxes1 – Array of size (n, 4) or (n, 6) where bounding box is defined as (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

  • boxes2 – Array of size (m, 4) or (m, 6) where bounding box is defined as (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2). If None, then pairwise intersections are calculated between all pairs of boxes in boxes1. Default, None.

Returns:

Array of (n, m) defining pairwise area/volume intersection between boxes.

Return type:

intersections

empanada.array_utils.box_iou(boxes1, boxes2=None, return_intersection=False)[source]#

Calculates the pairwise intersection-over-union between sets of boxes.

Parameters:
  • boxes1 – Array of size (n, 4) or (n, 6) where bounding box is defined as (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

  • boxes2 – Array of size (m, 4) or (m, 6) where bounding box is defined as (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2). If None, then pairwise IoUs are calculated between all pairs of boxes in boxes1. Default, None.

  • return_intersection – Bool. If True, intersection areas are returned.

Returns:

Sparse CSR matirx of (n, m) defining pairwise IoUs between boxes.

intersections: Returned if return_intersections is True. Sparse

CSR matirx of (n, m) defining intersection areas between boxes.

Return type:

ious

empanada.array_utils.crop_and_binarize(mask, box, label)[source]#

Crop a mask from a bounding box and binarize the cropped mask where it’s equal to the given label value.

Parameters:
  • mask – Array of (h, w) or (d, h, w) defining an image.

  • box – Bounding box tuple of (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

  • label – Label value to binarize within cropped mask.

Returns:

Boolean array of (h’, w’) or (d’, h’, w’).

Return type:

binary_cropped_mask

empanada.array_utils.extend_range(range1, range2, num_votes)#

Merges together two overlapping runs and updates the number of votes at each index within the range.

Parameters:
  • range1 – Tuple or List of (start_i, end_i) as integers.

  • range2 – Tuple or List of (start_j, end_j) as integers.

  • num_votes – List of integers. Stores the number of votes at each index in range1.

Returns:

List of (start_i, end_j) as integers. extended_num_votes: Updated list of num_votes to cover the new range.

Return type:

extended_range

empanada.array_utils.intersection_from_ranges(merged_runs, changes)#

Computes intersection from run ranges.

Parameters:
  • merged_runs – Integer array of (n, 2) where each element is a range of [start, end].

  • changes – Boolean array of (n,). True where the current range is from a different source run length encoding than the next range.

Returns:

Integer, number of pixels/voxels that overlap in merged_runs.

Return type:

intersection

empanada.array_utils.mask_ioa(mask1, mask2)[source]#

Calculates IoA score between two binary masks. The object area is derived from mask2.

Parameters:
  • mask1 – Boolean array of (h, w) or (d, h, w) defining an image.

  • mask2 – Boolean array of (h, w) or (d, h, w) defining an image.

Returns:

Float IoA score.

Return type:

ioa_score

empanada.array_utils.mask_iou(mask1, mask2, return_intersection=False)[source]#

Calculates IoU score between two binary masks.

Parameters:
  • mask1 – Boolean array of (h, w) or (d, h, w) defining an image.

  • mask2 – Boolean array of (h, w) or (d, h, w) defining an image.

  • return_intersection – Bool. If True, the intersection is returned.

Returns:

Float IoU score.

Return type:

iou_score

empanada.array_utils.merge_boxes(box1, box2)[source]#

Merges two bounding boxes into 1 box that encloses both.

Parameters:
  • box1 – Bounding box tuple of (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

  • box2 – Bounding box tuple of (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2).

Returns:

Bounding box tuple of (y1, x1, y2, x2) or (z1, y1, x1, z2, y2, x2). Defines the box that completely encloses box1 and box2.

Return type:

merged_box

empanada.array_utils.merge_rles(starts_a, runs_a, starts_b=None, runs_b=None)[source]#

Joins possible overlapping run length encodings into a single set of non-overlapping rles.

If starts_b and runs_b are none, then it’s assumed that starts_a and runs_a contain overlaps indices.

Parameters:
  • starts_a – Array of (n,) where each element is the starting index of a run.

  • runs_a – Array of (n,) where each element is the run length of a run.

  • starts_b – Default None. Array of (m,) starting indices.

  • runs_b – Default None. Array of (m,) rung lengths.

Returns:

Array of (l,) starting indices merged_runs: Array of (l,) run lengths

Return type:

merged_starts

empanada.array_utils.numpy_fill_instances(volume, instances)[source]#

Helper function to fill numpy volume with run length encoded instances

empanada.array_utils.put(array, indices, value, axis=0)[source]#

Put values at indices, inplace, along an axis.

Parameters:
  • array – np.ndarray

  • indices – List of indices

  • axis – Int. Axis to put along.

empanada.array_utils.rle_decode(starts, runs)[source]#

Decodes run length encoding arrays to an array of indices.

Parameters:
  • starts – Array of (l,) starting indices.

  • runs – Array of (l,) run lengths.

Returns:

An array of (n,) decoded indices.

Return type:

indices

empanada.array_utils.rle_encode(indices)[source]#

Run length encodes an array of 1d indices.

Parameters:

indices – An array of (n,) indices to run length encode.

Returns:

Array of (l,) starting indices. runs: Array of (l,) run lengths.

Return type:

starts

empanada.array_utils.rle_intersection(starts_a, runs_a, starts_b, runs_b)[source]#

Calculates the intersection between two run length encodings.

Parameters:
  • starts_a – Array of (n,) where each element is the starting index of a run.

  • runs_a – Array of (n,) where each element is the run length of a run.

  • starts_b – Array of (m,) where each element is the starting index of a run.

  • runs_b – Array of (m, ) where each element is the run length of a run.

Returns:

The number of overlapping pixels/voxels between rles.

Return type:

intersection

empanada.array_utils.rle_ioa(starts_a, runs_a, starts_b, runs_b, return_intersection=False)[source]#

Calculates the IoA between two run length encodings.

Parameters:
  • starts_a – Array of (n,) where each element is the starting index of a run.

  • runs_a – Array of (n,) where each element is the run length of a run.

  • starts_b – Array of (m,) where each element is the starting index of a run.

  • runs_b – Array of (m, ) where each element is the run length of a run.

  • return_intersection – Bool. If True, the intersection is returned.

Returns:

Float, the intersection-over-area score. intersection: The number of overlapping pixels/voxels between rles.

Return type:

ioa

empanada.array_utils.rle_iou(starts_a, runs_a, starts_b, runs_b, return_intersection=False)[source]#

Calculates the IoU between two run length encodings.

Parameters:
  • starts_a – Array of (n,) where each element is the starting index of a run.

  • runs_a – Array of (n,) where each element is the run length of a run.

  • starts_b – Array of (m,) where each element is the starting index of a run.

  • runs_b – Array of (m, ) where each element is the run length of a run.

  • return_intersection – Bool. If True, the intersection is returned.

Returns:

Float, the intersection-over-union score. intersection: The number of overlapping pixels/voxels between rles.

Return type:

iou

empanada.array_utils.rle_to_string(starts, runs)[source]#

Converts run length encoding to a string.

Parameters:
  • starts – Array of (l,) starting indices.

  • runs – Array of (l,) run lengths.

Returns:

String representation of a run length encoding. Format is “starts[0] runs[0] starts[1] runs[1] … starts[n] runs[n]”

Return type:

rle_string

empanada.array_utils.rle_voting(ranges, vote_thr=2, init_index=None, term_index=None)#

Finds overlapping ranges and tabulates the number of votes at each index within those ranges. Outputs ranges in which all indices had vote_thr or more votes.

Parameters:

ranges – np.ndarray of (n, 2) possibly overlapping ranges. Each range is defined by [start_idx, end_idx].

Returns:

np.ndarray of (m, 2) non-overlapping ranges. All indices in the ranges had more than (or equal to) vote_thr votes in ranges.

Return type:

voted_ranges

empanada.array_utils.split_range_by_votes(running_range, num_votes, vote_thr=2)#

Splits a range into two new ranges based on the votes for each index.

Parameters:
  • running_range – List of 2. First element is the run start and second element is run end.

  • num_votes – List of n. Each element is the number of votes for a particular index within the range(start, end).

  • vote_thr – Minimum number of votes for an index to be kept in the running range.

Returns:

List of new ranges with indices that had too few votes removed.

Return type:

split_voted_ranges

empanada.array_utils.string_to_rle(encoding)[source]#

Converts run length encoding string to start and run arrays.

Parameters:

rle_string – String representation of a run length encoding. Format is “starts[0] runs[0] starts[1] runs[1] … starts[n] runs[n]”

Returns:

Array of (l,) starting indices. runs: Array of (l,) run lengths.

Return type:

starts

empanada.array_utils.take(array, indices, axis=0)[source]#

Take indices from array along an axis

Parameters:
  • array – np.ndarray

  • indices – List of indices

  • axis – Int. Axis to take from.

Returns:

np.ndarray

Return type:

output