merging

AGNES

author: Richard Bonye (github Boyne272) Last updated on Tue Aug 27 18:23:14 2019

class TSA.merging.AGNES.AGNES(features, join_by='max')

Clusters the given feature vectors by the AGNES algorithm

AGNES(features, join_by=’max’)

Parameters:
  • features (2d numpy array) – The features to cluster by with dimensions (n_samples, n_features). It is advised to input features with normalised distributions as they are not scaled here and this is a distance based clustering algoithm
  • join_by (string (optional)) –

    The method by which to recalculate distance to a new group after merging occures. Allowed options are:

    • ’max’ takes the new clusters distance as the maximum of either of the joined vectors distances
    • ’min’ same as above but with minimum distance
cluster_by_derivative(n_std=3.0, plot=True)

Find the grouping by allowing all mergers up to a cutoff, here here determined by a vairation in the second derivative over the given numbers of standard deviation.

plot bool will plot the grouping projection in 2d.

cluster_by_distance(cutoff_dist=1.0, plot=True)

Find the grouping by allowing all mergers up to a given join distance. Plot will plot the grouping projection in 2d.

cluster_by_index(index, plot=True)

Find the grouping by allowing all mergers up to a given index, There will be N-index clusters where N is the number of samples (-1 from there being N-1 maximum joins is cancled by +1 from indexs starting at 0). Plot will plot the grouping projection in 2d.

cluster_distance_plot(option='all', ax=None, last_few=True)

Plot the option vs iterations on the given axis

Parameters:
  • option (string (optional)) –

    What to plot aginst iteration, choose from: - ‘dists’ plots the join distance - ‘1st’ plots the numerical first derivative - ‘2nd’plots the numerical second derivative - ‘all’ (default) plots plots all of the above

    (ignores ax and last few)
  • ax (Matplotlib axis (optional)) – The axis to plot on, one is created if not given. If ‘all’ chosen then this setting is ignored
  • last_few (bool (optional)) – Plot only the last 25 points (or all if less than 25 in total). If ‘all’ chosen then this setting is ignored
iterate()

Merge closest pairs until all datapoints are connected. This has to be done before any clustering may occure.

Segments

author: Richard Bonye (github Boyne272) Last updated on Tue Aug 27 18:37:15 2019

class TSA.merging.Segments.Mask_utilities(mask, connected=True)

A collection of utilities for minipulating a mask, such as getting its outline or creating an rgba array for plotting

Mask_utilities(mask, connected=True)

Parameters:
  • mask (2d numpy array) – integer array to be used
  • connected (bool, optional) – if true will enforce connectivity of each region in the mask via skimgs morphology package. Defaults to true.
class TSA.merging.Segments.segment(cords, edges, index, group_obj)

A single segment which holds its constituent pixels, edge pixels and figures out its neighbours. This is to be used by the segment_group obj.

segment(cords, edges, index, group_obj)

Parameters:
  • cords (2d numpy array) – The cordinates of this segments pixels in shape (N, 2) where N is the number of pixels and the second axis is the X, Y cordinates
  • edges (2d numpy array) – The cordinates of this segments edge pixels in the same shape as cords
  • index (int) – The id of this segment used in the segment_group.seg_dict
  • group_obj (segment_group obj) – The segment_group which holds all segment objects. This is used to access the locations of other segments in order to find neighbours
edge_mask(arr)

Fill the given 2d arr at this segments edge values with this segments edge confidence at that boarder.

edge_plot(ax)

Plot this pixles edges on the given axis with a different color for each edge and label with a legend.

fill_mask(rgba, color=None, alpha=0.4)

Fill the given 3d rgba array with this segents cordinates using the given color values and given alpha (opaqueness).

class TSA.merging.Segments.segment_group(mask)

Object that handles a feature extraction and merging of a segment mask though the use of segment instances. Upon initalisation these segments locate their neighbours, edges and enforce connectivity of each segment. Note the neighbour identification is a costly process.

segment_group(mask)

Parameters:mask (2d numpy array) – integer array of a mask governing a segmentation
assign_clusters(clusters)

Store the clusters of each segment for use in merge_by_cluster. This is done seperately so that the segments of each cluster can be plotted before the merging is done.

edge_confidence(confidence_func, func_vars)

Loop over every segment with the confidence function to obtain the edge confidence between every neighbour pair. This confidence value is then stored on each segment and used in the merging functions.

Parameters:
  • confidence_func (callable) – callabel which accepts (X_cords, Y_cords, *func_vars) where cordinates are of the segment edge with one neighbour and returns a single value of the confidence of an edges presence. By default >1 is consider to be an edge and <-1 is considered to be no edge, however this can be overwritten in the mergeing functions. Note that X,Y is given in the visible sense (i.e. an array is indexed [y, x])
  • func_vars (tuple of vairables) – vairable to be passed into the fucntion to evaluate the segments edge confidence. This is often some edge detected versions of the original image to be indexed at the segments cordinates
enforce_size(min_size=50)

Merge small sections below the given area with their largest neighbour

feature_extraction(extract_func, func_vars)

Loop over every segment with the extract function to obtain the feature vectors for each segement.

Parameters:
  • extract_func (callable) – callabel which accepts (X_cords, Y_cords, *func_vars) where the cordinates are of a single segment and returns a tuple of features of that segment. Note that X,Y is given in the visible sense (i.e. an array is indexed [y, x])
  • func_vars (tuple of vairables) – vairable to be passed into the fucntion to evaluate the segments feaures. This is often some filtered versions of the original image to be indexed at the segments cordinates
Returns:

feats – array of segment features with shape (n segments, n features)

Return type:

2d array

get_cluster_mask()

Returns a mask of the cluster of each segment

merge_by_cluster(edge_present=1.0)

After a clustering has been assigned this merges segments that are adjasent and of the same group.

If an edge_confidence has been calculated for the segments than the extra condition that the edge value is below the edge_present parameter is requiered.

merge_by_edge(edge_absent=-1.0)

After an edge_confidence hsa been calculated merge any adjasent pairs with edge value less than the edge_absent parameter.

plot(option='default', ax=None, **kwargs)

Plot the specified option on the axis if given. All of these are designed to be plotted over the original image, either by passing axis with this image already on it or by passing the image array as kwarg ‘back_img’.

Valid options are:

  • ‘both’ or ‘default’
    shows the merged segments edges and the original segment edges in a different color so what has been merged is clear.
  • ‘orig_edges’
    shows the original segments edges.
  • ‘merged_edges’
    shows the merged segments edges.
  • ‘cluster’
    plots a mask that is transparent only where segments belong to the cluster specified by the kwarg ‘cluster’.
  • ‘cluster_all’
    plots every cluster as above on a multi axis figure. Must give back_img kwarg so that it can be put on each subplot.
  • ‘segment’
    plots a mask that shades in the segment id given with the kwarg ‘segment’ a slighly translucent red.
  • ‘segment_edge’
    plots the edges of the segment id given with the kwarg ‘segment’. These are colored and labeled different for each different neighbouring segment (if a pixel has more than one neighbour segment adjasent it is random which is indicated)
  • ‘edge_conf’
    plot every segment edge in a color that represents its edge confidence value.