imfun.cluster

class imfun.cluster.Cluster(points, center_fn=<Mock name='mock.mean' id='140508897394640'>, dist_fn=<function euclidean at 0x7fcac6e1ac80>)

Cluster representatin class

addpoint(point)

add a point to the cluster

centroid()

return coordinates of the centroid

diam()

maximal distance between two points in the cluster

distortion()

sum of distances of all points from the centroid

distortion_mean()

mean of distances of all points from the centroid

farthest_linkage(point)

maximal distance from a given point to points in the cluster

mass()

return number of points in the cluster

set_points(points)

set points that belong to the cluster

update(points)

replace old points with new ones and return the change in centroid position

imfun.cluster.apearson(v1, v2)

Absolute Pearson distance measure

imfun.cluster.arreq(arr1, arr2)

elemen-wise equality of two arrays

imfun.cluster.cityblock(p1, p2)

Cityblock (Manhattan) distance between 2 points

imfun.cluster.cluster2array(c)

helpful for scatter plots

imfun.cluster.converge_square(m, square, step=1, efunc=<Mock name='mock.sum' id='140508896832272'>, min_size=1)

Converge one starting square to a small square

Parameters:
  • m: input ND matrix
  • square: a starting structuring element, a list of slices
  • step : deflating coefficient
  • efunc: a measure function to apply to elements within a square
  • min_size: smallest size of the square when the algorithm is stopped
Returns:
  • final (smallest) square which maximizes the efunc over the elements
imfun.cluster.csq_find_rois(m, threshold=None, stride=5, reduct_step=1, efunc=<Mock name='mock.mean' id='140508897394640'>, min_size=1)

Find regions of interest in an image with converging squares algorithm

Parameters:
  • m: an N-dimensional matrix

  • threshold: a threshold whether a local starting square should be

    taken into account

  • stride: size of a starting square

  • reduct_step: square is reduced by this step at each iteration

  • efunc: a measure function to apply to elements within a square

    [np.sum]

  • min_size: smallest size of the square when the algorithm is stopped

Returns:
  • a list of found ROIs as minimal squares
imfun.cluster.csqroi2point(roi)

Helper function, converts a ROI to a point to plot

imfun.cluster.dbscan(points, eps, min_pts, distances=None, dist_fn='euclidean', verbose=True)

Implementation of DBSCAN density-based clustering algorithm

Parameters:
  • points: input collection of points. points must be hashable

    (i.e. a point is a tuple of coordinates)

  • eps: (number) – neighborhood radius

  • min_pts: (number) – minimal number of neighborhood points

  • dist_fn: (function) – distance measure, [euclidean]

  • verbose: (bool) – if True, be verbose

Returns:
a list of clusters (each cluster is a Cluster class instance)
imfun.cluster.dbscan_old(points, eps, min_pts, dist_fn=<function euclidean at 0x7fcac6e1ac80>, verbose=False)

Implementation of DBSCAN density-based clustering algorithm

Parameters:
  • points: input collection of points. points must be hashable

    (i.e. a point is a tuple of coordinates)

  • eps: (number) – neighborhood radius

  • min_pts: (number) – minimal number of neighborhood points

  • dist_fn: (function) – distance measure, [euclidean]

  • verbose: (bool) – if True, be verbose

Returns:
a list of clusters (each cluster is a Cluster class instance)
imfun.cluster.euclidean(p1, p2)

Euclidean distanc between 2 points

imfun.cluster.kmeans1(points, k, tol=0.001, center_fn=<Mock name='mock.mean' id='140508897394640'>, distance=<function euclidean at 0x7fcac6e1ac80>, max_iter=10000000.0, output='labels', verbose=True)

Simple K-means implementation

Parameters:
  • points: (collection) – a collection of points, each point is a

    list/tuple/array of coordinates

  • k: (number) – number of clusters

  • tol: (number) – minimal shift of cluster centroid, before stop of algorithm

  • center_fn: (function) – a function to find centroid; by default, numpy.mean

  • distance: (function) – a distance measure; by default, euclidean]

  • max_iter: (number) – maximal number of iterations

  • output: (string) – can be “labels”, “clusters” and “full”, modifies

    return value

  • verbose: (bool) – if True, be verbose

Returns: modified by output argument
  • if output is ‘labels’, return a list of integer labels, specifying point affiliation to a cluster
  • if output is ‘clusters’ return clusters
  • if ouput is ‘full’ return a tuple of clusters and affiliations
imfun.cluster.locations(shape)

all locations for a shape; substitutes nested cycles

imfun.cluster.make_grid(shape, stride)

Make a generator over sets of slices which go through the provided shape by a stride

imfun.cluster.mask2points(mask)

mask to a list of points, as row,col

imfun.cluster.mask2pointsr(mask)

mask to a list of points, with X,Y coordinates reversed

imfun.cluster.minkowski(p1, p2, k)

Minkowski distance between points p1 and p2 if k equals 1, it is a Manhattan (cityblock) distance if k equals 2, it is a Euclidean distance

imfun.cluster.pearson(v1, v2)

Pearson distance measure

imfun.cluster.plot_csq_rois(m, rois)

A helper function to plot the ROIs determined by the csq_find_rois implementation of the converging squares algorithm

Parameters:
  • m : a 2D matrix
  • rois: a list of ROIs
imfun.cluster.spearman(v1, v2)

Spearman distance measure

imfun.cluster.xcorrdist(v1, v2)

Correlation distance measure