pygmi.clust package

Submodules

pygmi.clust.cluster module

Main Clustering set of routines.

class pygmi.clust.cluster.Cluster(parent=None)

Bases: BasicModule

Cluster Class.

acceptall()

Run the cluster analysis.

Return type:

None.

combo()

Set up combo box.

Return type:

None.

saveproj()

Save project data from class.

Return type:

None.

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

update_vars()

Update the variables.

Return type:

None.

pygmi.clust.crisp_clust module

Main Crisp Clustering set of routines.

class pygmi.clust.crisp_clust.CrispClust(parent=None)

Bases: BasicModule

Crisp Cluster Class.

acceptall()

Process the data.

Return type:

None.

combo()

Set up combo box.

Return type:

None.

crisp_means(data, no_clust, cent, centfix, maxit, term_thresh, cltype, cov_constr)

Script enables the crisp clustering of COMPLETE multi-variate datasets.

Parameters:
  • data (numpy array) – N x P matrix containing the data to be clustered, N is number of samples, P is number of different attributes available for each sample.

  • no_clust (int) – Number of clusters to be used.

  • cent (numpy array) – cluster centre positions, either empty [] –> randomly guessed center positions will be used for initialisation or NO_CLUSTxP matrix

  • centfix (numpy array) – Constrains the position of cluster centers, if CENTFIX is empty, cluster centers can freely vary during cluster analysis, otherwise CENTFIX is of equal size to CENT and gives an absolute deviation from initial center positions that should not be exceeded during clustering. Note, CETNFIX applies only if center values are provided by the user.

  • maxit (int) – number of maximal allowed iterations.

  • term_thresh (float) – Termination threshold, either empty [] –> go for the maximum number of iterations MAXIT or a scalar giving the minimum reduction of the size of the objective function for two consecutive iterations in Percent.

  • cltype (str) – either ‘kmeans’ –> kmeans cluster analysis (spherically shaped cluster), ‘det’ –> uses the determinant criterion of Spath, H., “Cluster-Formation and Analyse, chapter3” (ellipsoidal clusters, all cluster use the same ellipsoid), or ‘vardet’ –> Spath, H., chapter 4 (each cluster uses its individual ellipsoid). Note: the latter is the crisp version of the Gustafson-Kessel algorithm

  • cov_constr (float) – scalar between [0 1], values > 0 trim the covariance matrix to avoid needle-like ellipsoids for the clusters, applies only for cltype=’vardet’, but must always be provided.

Returns:

  • idx (numpy array) – cluster index number for each sample after the last iteration, column vector.

  • cent (numpy array) – matrix with cluster centre positions after last iteration, one cluster centre per row

  • obj_fcn (numpy array) – Vector, size of the objective function after each iteration

  • vrc (numpy array) – Variance Ratio Criterion

saveproj()

Save project data from class.

Return type:

None.

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

update_vars()

Update the variables.

Return type:

None.

pygmi.clust.crisp_clust.gcentroids(data, index, no_clust, mindist)

G Centroids.

Parameters:
  • data (numpy array) – Input data.

  • index (numpy array) – Cluster index number for each sample.

  • no_clust (int) – Number of clusters to be used.

  • mindist (numpy array) – Minimum distances.

Returns:

  • centroids (numpy array) – Centroids

  • index (numpy array) – Index

pygmi.clust.crisp_clust.gdist(data, center, index, no_clust, cltype, cov_constr)

G Dist routine.

Parameters:
  • data (numpy array) – Input data.

  • center (numpy array) – center of each class.

  • index (numpy array) – Cluster index number for each sample.

  • no_clust (int) – Number of clusters to be used.

  • cltype (str) – Clustering type.

  • cov_constr (float) – scalar between [0 1].

Returns:

bigd – Output data.

Return type:

numpy array

pygmi.clust.fuzzy_clust module

Fuzzy clustering.

class pygmi.clust.fuzzy_clust.FuzzyClust(parent=None)

Bases: BasicModule

Fuzzy Clustering class.

acceptall()

Run.

Return type:

None.

combo()

Set up combo box.

Return type:

None.

fuzzy_means(data, no_clust, init, centfix, maxit, term_thresh, expo, cltype, cov_constr)

Fuzzy clustering.

Finds NO_CLUST clusters in the data set DATA.. Supported algorithms are fuzzy c-means, Gustafson-Kessel, advanced fuzzy c-means.

Parameters:
  • data (numpy array) – DATA is size M-by-N, where M is the number of samples and N is the number of coordinates (attributes) for each sample.

  • no_clust (int) – Number of clusters.

  • init (numpy array) – INIT may be set to [], in this case the FCM generates random initial center locations to start the algorithm. Alternatively, INIT can be of matrix type, either containing a user-given membership matrix [NO_CLUST M] or a cluster center matrix [NO_CLUST, N].

  • centfix (numpy array) – Constrains the position of cluster centers.

  • maxit (int) – MAXIT give the maximum number of iterations..

  • term_thresh (float) – Gives the required minimum improvement in per cent per iteration. (termination threshold)

  • expo (float) – Fuzzification exponent.

  • cltype (str) – either ‘FCM’ for fuzzy c-means (spherically shaped clusters), ‘DET’ for advanced fuzzy c-means (ellipsoidal clusters, all clusters use the same ellipsoid), or ‘GK’ for Gustafson-Kessel clustering (ellipsoidal clusters, each cluster uses its own ellipsoid).

  • cov_constr (float) – COV_CONSTR applies only to the GK algorithm. constrains the cluster shape towards spherical clusters to avoid needle-like clusters. COV_CONSTR = 1 make the GK algorithm equal to the FCM algorithm, COV_CONSTR = 0 results in no constraining of the covariance matrices of the clusters.

Returns:

  • uuu (numpy array) – This membership function matrix contains the grade of membership of each data sample to each cluster.

  • cent (numpy array) – The coordinates for each cluster center are returned in the rows of the matrix CENT.

  • obj_fcn (numpy array) – At each iteration, an objective function is minimized to find the best location for the clusters and its values are returned in OBJ_FCN.

  • vrc (numpy array) – Variance ration criterion.

  • nce – Normalised class entropy.

  • xbi (numpy array) – Xie beni index.

saveproj()

Save project data from class.

Return type:

None.

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

update_vars()

Update the variables.

Return type:

None.

pygmi.clust.fuzzy_clust.fuzzy_dist(cent, data, uuu, expo, cltype, cov_constr)

Fuzzy distance.

Parameters:
  • cent (numpy array) – Class centers.

  • data (numpy array) – Input data.

  • uuu (numpy array) – Membership function matrix.

  • expo (float) – Fuzzification exponent.

  • cltype (str) – Clustering type.

  • cov_constr (float) – Applies only to the GK algorithm. constrains the cluster shape towards spherical clusters.

Returns:

ddd – Output data.

Return type:

numpy array

pygmi.clust.fuzzy_clust.xie_beni(data, expo, uuu, center, edist)

Xie Beni.

Calculates the Xie-Beni index accepts missing values when given as nan elements in the data base) min xbi is optimal

Parameters:
  • data (numpy array) – input dataset

  • expo (float) –

  • uuu (numpy array) – membership matrix (FCM) or cluster index values (k-means)

  • center (numpy array) – cluster centers

  • edist (numpy array) –

Returns:

xbi – xie beni index

Return type:

numpy array

pygmi.clust.graphs module

Plot Cluster Data.

class pygmi.clust.graphs.GraphWindow(parent=None)

Bases: ContextModule

Graph Window.

change_band()

Combo to change band.

Return type:

None.

class pygmi.clust.graphs.MyMplCanvas(parent=None)

Bases: FigureCanvasQTAgg

Canvas for the actual plot.

axes
Type:

matplotlib subplot

parent

reference to the parent routine

Type:

parent

update_classes(data1)

Update the plot.

Parameters:

data1 (PyGMI Data.) – Input raster dataset.

Return type:

None.

update_membership(data1, mem)

Update membership plot.

Parameters:
  • data1 (PyGMI Data.) – Raster dataset.

  • mem (int) – Membership.

Return type:

None.

update_scatter(x, y)

Update the scatter plot.

Parameters:
  • x (numpy array) – X coordinates (Number of classes).

  • y (numpy array) – Y Coordinates.

Return type:

None.

update_wireframe(x, y, z)

Update wireframe plot.

Parameters:
  • x (numpy array) – Iteration number.

  • y (numpy array) – Number of classes.

  • z (numpy array) – z coordinate.

Return type:

None.

class pygmi.clust.graphs.PlotMembership(parent=None)

Bases: GraphWindow

Plot Fuzzy Membership data.

change_band()

Combo to change band.

Return type:

None.

change_band_two()

Combo box to choose band.

run()

Run.

Return type:

None.

class pygmi.clust.graphs.PlotRaster(parent=None)

Bases: GraphWindow

Plot Raster Class.

change_band()

Combo to change band.

Return type:

None.

run()

Run.

Return type:

None.

class pygmi.clust.graphs.PlotVRCetc(parent=None)

Bases: GraphWindow

Plot VRC, NCE, OBJ and XBI.

change_band()

Combo to change band.

Return type:

None.

run()

Run.

Return type:

None.

pygmi.clust.graphtool module

Multi-function graphing tool for use with cluster analysis.

class pygmi.clust.graphtool.GraphHist(parent=None)

Bases: FigureCanvasQTAgg

Graph Hist.

get_clust_scat(bins, dattmp, ctmp)

Routine to get the scattergram with cluster overlay.

Parameters:
  • bins (int) – Number of bins.

  • dattmp (list of PyGMI Data) – Data.

  • ctmp (list) – Cluster indices.

Returns:

xymahist – Output data.

Return type:

numpy array

get_hist(bins)

Routine to get the scattergram with histogram overlay.

Parameters:

bins (int) – Number of bins.

Returns:

xymahist – Output data.

Return type:

numpy array

init_graph()

Initialize the Graph.

Return type:

None.

polyint()

Polygon Interactor routine.

Return type:

None.

setup_coords()

Routine to setup the coordinates for the scattergram.

Return type:

None.

setup_hist()

Routine to setup the 1D histograms.

Return type:

None.

update_graph(clearaxis=False)

Draw Routine.

Parameters:

clearaxis (bool, optional) – True to clear the axis. The default is False.

Return type:

None.

class pygmi.clust.graphtool.GraphMap(parent=None)

Bases: FigureCanvasQTAgg

Graph Map.

parent

reference to the parent routine

Type:

parent

init_graph()

Initialize the Graph.

Return type:

None.

polyint()

Polygon Integrator.

Return type:

None.

update_graph()

Draw routine.

Return type:

None.

class pygmi.clust.graphtool.PolygonInteractor(axtmp, pntxy)

Bases: QObject

Polygon Interactor for the graph tool.

Parameters:
  • showverts (bool) –

  • epsilon (int) –

  • polyi_changed (signal) –

button_press_callback(event)

Button press callback.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Return type:

None.

button_release_callback(event)

Button release callback.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Return type:

None.

draw_callback()

Draw callback.

Return type:

None.

epsilon = 5
get_ind_under_point(event)

Get the index of vertex under point if within epsilon tolerance.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Returns:

ind – Index of vertex under point.

Return type:

int or None

motion_notify_callback(event)

Mouse notify callback.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Return type:

None.

new_poly(npoly)

Create new Polygon.

Parameters:

npoly (list) – New polygon coordinates.

Return type:

None.

polyi_changed

polygon changed signal.

showverts = True
update_plots()

Update plots.

Return type:

None.

class pygmi.clust.graphtool.ScatterPlot(parent=None)

Bases: BasicModule

Main Graph Tool Routine.

on_cp_combo()

On cp combo.

Return type:

None.

on_cp_combo2()

On cp combo 2.

Return type:

None.

on_cp_combo3()

On cp combo 3.

Return type:

None.

on_cp_dpoly()

On cp delete polygon.

Return type:

None.

on_map_combo()

On map combo.

Return type:

None.

on_map_combo2()

On map combo 2.

Return type:

None.

on_map_dpoly()

On map delete polygon.

Return type:

None.

saveproj()

Save project data from class.

Return type:

None.

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

update_hist()

Update histogram.

Return type:

None.

update_map()

Update map.

Return type:

None.

pygmi.clust.graphtool.dist_point_to_segment(p, s0, s1)

Dist point to segment.

Reimplementation of Matplotlib’s dist_point_to_segment, after it was depreciated. Follows http://geomalgorithms.com/a02-_lines.html

Parameters:
  • p (numpy array) – Point.

  • s0 (numpy array) – Start of segment.

  • s1 (numpy array) – End of segment.

Returns:

Distance of point to segment.

Return type:

numpy array

pygmi.clust.menu module

Clustering Menu Routines.

class pygmi.clust.menu.MenuWidget(parent=None)

Bases: object

Widget class to call the main interface.

This widget class creates the clustering menus to be found on the main interface. Normal as well as context menus are defined here.

parent

Reference to MainWidget class found in main.py

Type:

pygmi.main.MainWidget

cluster()

Clustering of data.

cluster_stats()

Calculate Statistics.

crisp_cluster()

Crisp Clustering of data.

export_data()

Export raster data.

fuzzy_cluster()

Fuzzy Clustering of data.

scatter_plot()

Scatter Plot Tool.

segmentation()

Image Segmentation.

show_membership_data()

Show membership data.

show_raster_data()

Show raster data.

show_vrc_etc()

Show vrc, xbi, obj, nce graphs.

super_class()

Supervised Classification.

pygmi.clust.segmentation module

Image segmentation routines.

class pygmi.clust.segmentation.ImageSeg(parent=None)

Bases: BasicModule

Image Segmentation.

saveproj()

Save project data from class.

Return type:

None.

segment1(data, scale=500, wcolor=0.5, wcompact=0.5, doshape=True)

Segment Part 1.

Parameters:
  • data (numpy array) – Input data.

  • scale (int, optional) – Scale. The default is 500.

  • wcolor (float, optional) – Colour weight. The default is 0.5.

  • wcompact (float, optional) – Compactness weight. The default is 0.5.

  • doshape (bool, optional) – Perform shape segmentation. The default is True.

Returns:

omap – Output data.

Return type:

numpy array

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

pygmi.clust.segmentation.get_l(data)

Get bounding box length.

Parameters:

data (numpy array) – Input data.

Returns:

ltmp – Bounding box length.

Return type:

int

pygmi.clust.super_class module

Supervised Classification tool.

class pygmi.clust.super_class.GraphMap(parent=None)

Bases: FigureCanvasQTAgg

Graph Map.

parent

reference to the parent routine

Type:

parent

compute_initial_figure(dat)

Compute initial figure.

Parameters:

dat (PyGMI Data) – PyGMI dataset.

Return type:

None.

polyint(dat)

Polygon integrator.

Return type:

None.

update_plot(dat)

Update plot.

Parameters:

dat (Dictionary) – PyGMI dataset/s in a dictionary.

Return type:

None.

class pygmi.clust.super_class.PolygonInteractor(axtmp, pntxy)

Bases: QObject

Polygon Interactor for the supervised classification tool.

Parameters:
  • showverts (bool) –

  • epsilon (int) –

  • polyi_changed (signal) –

button_press_callback(event)

Button press callback.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Return type:

None.

button_release_callback(event)

Button release callback.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse Event.

Return type:

None.

draw_callback(event=None)

Draw callback.

Parameters:

event (matplotlib.backend_bases.DrawEvent, optional) – Draw event object. The default is None.

Return type:

None.

epsilon = 5
get_ind_under_point(event)

Get the index of vertex under point if within epsilon tolerance.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Returns:

ind – Index of vertex under point.

Return type:

int or None

motion_notify_callback(event)

Motion notify on mouse movement.

Parameters:

event (matplotlib.backend_bases.MouseEvent) – Mouse event.

Return type:

None.

new_poly(npoly=None)

Create new polygon.

Parameters:

npoly (list or None, optional) – New polygon coordinates.

Return type:

None.

polyi_changed

polygon changed signal.

showverts = True
update_plots()

Update plots.

Return type:

None.

class pygmi.clust.super_class.SuperClass(parent=None)

Bases: BasicModule

Main Supervised Classification Tool Routine.

calc_metrics()

Calculate metrics.

Return type:

None.

class_change()

Routine called when current classification choice changes.

Return type:

None.

init_classifier()

Initialise classifier.

Returns:

  • classifier (object) – Scikit learn classification object.

  • lbls (numpy array) – Class labels.

  • datall (numpy array) – Dataset.

  • X_test (numpy array) – X test dataset.

  • y_test (numpy array) – Y test dataset.

  • tlbls (numpy array) – Class labels.

load_shape()

Load shapefile.

Returns:

True if successful, False otherwise.

Return type:

bool

on_apoly()

On add polygon.

Return type:

None.

on_combo()

On combo.

Return type:

None.

on_dpoly()

On delete polygon.

Return type:

None.

oncellchange(row, col)

Routine activated whenever a cell is changed.

Parameters:
  • row (int) – Current row.

  • col (int) – Current column.

Return type:

None.

onrowchange(current, previous)

Routine activated whenever a row is changed.

Parameters:
  • current (QTableWidgetItem) – current item.

  • previous (QTableWidgetItem) – previous item.

Return type:

None.

save_shape()

Save shapefile.

Returns:

True if successful, False otherwise.

Return type:

bool

saveproj()

Save project data from class.

Return type:

None.

settings(nodialog=False)

Entry point into item.

Parameters:

nodialog (bool, optional) – Run settings without a dialog. The default is False.

Returns:

True if successful, False otherwise.

Return type:

bool

setupui()

Set up UI.

Return type:

None.

update_class_polys()

Update class poly summaries.

updatepoly(xycoords=None)

Update polygon.

Parameters:

xycoords (numpy array, optional) – x, y coordinates. The default is None.

Return type:

None.

pygmi.clust.super_class.dist_point_to_segment(p, s0, s1)

Dist point to segment.

Reimplementation of Matplotlib’s dist_point_to_segment, after it was depreciated. Follows http://geomalgorithms.com/a02-_lines.html

Parameters:
  • p (numpy array) – Point.

  • s0 (numpy array) – Start of segment.

  • s1 (numpy array) – End of segment.

Returns:

Distance of point to segment.

Return type:

numpy array

pygmi.clust.var_ratio module

Variance Ratio.

pygmi.clust.var_ratio.var_ratio(data, uuu, center, dist_orig)

Variance Ratio.

Calculates the Variance ratio criterion after Calinski and Harabasz, 1974. Does not accept missing data. Max VRC is optimal. U can either be membership matrix (FCM) or cluster index values (k-means)

Parameters:
  • data (numpy array) – input dataset

  • uuu (numpy array) – membership matrix (FCM) or cluster index values (k-means)

  • center (numpy array) – cluster centers

  • dist_orig (numpy array) –

Returns:

vrc – variance ration criterion

Return type:

numpy array

Module contents

Cluster analysis routines