util

ilastikrag.util.contingency_table(left_vol, right_vol)[source]

Return a pd.DataFrame with columns ‘left’, ‘right’ and ‘overlap_size’, indicating the count of overlapping pixels for each segment in ‘from’ with segments in ‘to’.

Note: Internally, copies both volumes multiple times.

This function seems to require an extra ~5x RAM relative to the inputs.

ilastikrag.util.dataframe_from_hdf5(h5py_group)[source]

Helper function to deserialize a pandas.DataFrame from an h5py.Group, as written by dataframe_to_hdf5().

Note: This function uses a custom storage format,

not the same format as pandas.read_hdf().

Known to work for the DataFrames used in the Rag datastructure, including the MultiIndex columns in the dense_edge_tables. Not tested with more complicated DataFrame structures.

ilastikrag.util.dataframe_to_hdf5(h5py_group, df)[source]

Helper function to serialize a pandas.DataFrame to an h5py.Group.

Note: This function uses a custom storage format,

not the same format as pandas.DataFrame.to_hdf().

Known to work for the DataFrames used in the Rag datastructure, including the MultiIndex columns in the dense_edge_tables. Not tested with more complicated DataFrame structures.

ilastikrag.util.edge_ids_for_axis(label_img, edge_mask, axis)[source]

Given a ‘left-hand’ edge_mask indicating where edges are located along the given axis, return an array of of edge ids (u,v) corresonding to the voxel ids of every voxel under the mask, in the same order as edge_mask.nonzero().

The edge ids returned in scan-order (i.e. like .nonzero()), but are not sorted such that u < v. Instead, each edge id (u,v) is ordered from ‘left’ to ‘right’.

Parameters:
  • label_img – ndarray

  • edge_mask

    A ‘left-hand’ mask indicating where the image edges are. Should be same shape as label_img, except in the dimension of the given axis, where it is 1 pixel narrower.

    You may also provide edge_mask=None, which implies that all pixel locations contain an edge along the requested axis. (Useful if you’re dealing with flat superpixels.)

  • axis – An int, < label_img.ndim Indicates the axis along which edges will be extracted.

Returns:

  • ndarray of edge_ids, shape=(N,2)

  • To sort each pair, call edge_ids.sort_values(axis=1)

ilastikrag.util.edge_mask_for_axis(label_img, axis)[source]

Find all supervoxel edges along the given axis and return a ‘left-hand’ mask indicating where the edges are located (i.e. a boolean array indicating voxels that are just to the left of an edge). Note that this mask is less wide (by 1 pixel) than label_img along the chosen axis.

ilastikrag.util.extract_edge_values_for_axis(axis, edge_mask, value_img, aspandas=False)[source]

Returns 1D ndarray, in the same order as edge_mask.nonzero(). Result is float32, regardless of value_img.dtype.

ilastikrag.util.generate_random_voronoi(shape, num_sp)[source]

Generate a superpixel image for testing. A set of N seed points (N=``num_sp``) will be chosen randomly, and the superpixels will just be a voronoi diagram for those seeds. Note: The first superpixel ID is 1.

ilastikrag.util.get_edge_ids(label_img)[source]

Convenience function. Returns a DataFrame with columns ['sp1', 'sp2', 'edge_label'], sorted by ('sp1', 'sp2').

ilastikrag.util.label_vol_mapping(vol_from, vol_to)[source]

Determine how remap voxel IDs in vol_from into corresponding IDs in vol_to, according to maxiumum overlap. (Note that this is not a commutative operation.)

Returns:

  • A 1D index array such that mapping[i] = j, where i

  • is a voxel ID in vol_from, and j is the corresponding

  • ID in vol_to.

ilastikrag.util.nonzero_coord_array(a)[source]

Equivalent to np.transpose(a.nonzero()), but much faster for large arrays, thanks to a little trick:

The elements of the tuple returned by a.nonzero() share a common base, so we can avoid the copy that would normally be incurred when calling transpose() on the tuple.

ilastikrag.util.unique_edge_labels(all_edge_ids)[source]

Given a list of edge_id arrays (each of which has shape (N,2)), merge all edge_id arrays into a single pandas.DataFrame with columns ['sp1', 'sp2', and 'edge_label'], where edge_label is a unique ID number for each edge_id pair. (The DataFrame will have no duplicate entries.)