Skip to content

neurostatx.clustering.viz

Functions

NameDescription
plot_clustering_resultsPlot a clustering goodness-of-fit curve and write it to a file.
plot_dendrogramPlot a hierarchical clustering dendrogram and write it to a file.
plot_fuzzy_cmeans_solutionsPlot a grid of Fuzzy C-Means solutions for a range of k.
plot_parallel_plotPlot parallel coordinates for clustered features and write a PNG file.
radar_plotPlot a radar chart of feature means by cluster and write a PNG file.
sort_int_labels_legendReorder legend labels numerically and keep matching handles.

plot_clustering_resultsfunction#

def plot_clustering_results(lst, title, metric, output, errorbar=None, annotation=None)

Plot a clustering goodness-of-fit curve and write it to a file.

Parameters

NameTypeDefaultDescription
lstlistrequiredMetric values to plot, one per cluster count starting at k=2.
titlestrrequiredTitle of the plot.
metricstrrequiredY-axis label for the plotted metric.
outputstrrequiredOutput filename.
errorbarlistNoneValues to plot as error bars (CI, SD, etc.). Defaults to None.
annotationstrNoneText annotation drawn on the plot. Defaults to None.

Examples

>>> from neurostatx.clustering.viz import plot_clustering_results
>>> plot_clustering_results([1.2, 0.8, 0.6], "WSS", "WSS", "wss.png")

plot_dendrogramfunction#

def plot_dendrogram(X, output, title='Dendrograms', annotation=None)

Plot a hierarchical clustering dendrogram and write it to a file.

Parameters

NameTypeDefaultDescription
XDataFramerequiredData used to compute Ward linkage.
outputstrrequiredOutput filename and path.
titlestr'Dendrograms'Title for the plot. Defaults to "Dendrograms".
annotationstrNoneText annotation drawn on the plot. Defaults to None.

Examples

>>> import pandas as pd
>>> from neurostatx.clustering.viz import plot_dendrogram
>>> X = pd.DataFrame([[0.0, 0.1], [0.2, 0.0], [1.0, 1.1], [0.9, 1.0]])
>>> plot_dendrogram(X, "dendrogram.png")

plot_fuzzy_cmeans_solutionsfunction#

def plot_fuzzy_cmeans_solutions(results, output, min_clusters=2, max_clusters=None)

Plot a grid of Fuzzy C-Means solutions for a range of k.

Parameters

NameTypeDefaultDescription
resultslist of tuplerequiredPer-k worker results from [process_cluster][neurostatx.clustering.fuzzy.process_cluster]. Each tuple must contain n_cluster, n_iter, fpc, centroids, visualization x/y points, and hard labels.
outputstrrequiredOutput folder. The figure is saved as viz_multiple_cluster_nb.png.
min_clustersint2Smallest k that was fitted. Defaults to 2.
max_clustersintNoneLargest k requested, used only for subplot grid size. Defaults to the number of results plus min_clusters - 1.

Examples

>>> from neurostatx.clustering.viz import plot_fuzzy_cmeans_solutions
>>> result = (2, 10, 0.8, [[0.0, 0.0], [1.0, 1.0]], None, None, None,
... None, None, None, None, [0.0, 1.0], [0.0, 1.0], [0, 1])
>>> plot_fuzzy_cmeans_solutions([result], output=".", min_clusters=2)

plot_parallel_plotfunction#

def plot_parallel_plot(X, labels, output, mean_values=False, cmap='magma', title='Parallel Coordinates plot.')

Plot parallel coordinates for clustered features and write a PNG file.

Parameters

NameTypeDefaultDescription
XDataFramerequiredInput dataset of shape (n_samples, n_features).
labelsnp.arrayrequiredHard cluster labels of shape (n_samples,).
outputstrrequiredOutput PNG filename.
mean_valuesboolFalseIf True, plot the mean of each feature per cluster. Defaults to False.
cmapstr'magma'Colormap name. Defaults to "magma".
titlestr'Parallel Coordinates plot.'Title of the plot. Defaults to "Parallel Coordinates plot.".

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from neurostatx.clustering.viz import plot_parallel_plot
>>> X = pd.DataFrame({"a": [0.0, 0.1, 1.0, 0.9],
... "b": [0.2, 0.0, 1.1, 1.0]})
>>> plot_parallel_plot(X, np.array([0, 0, 1, 1]), "parallel.png",
... mean_values=True)

radar_plotfunction#

def radar_plot(X, labels, output, frame='circle', title='Radar plot', cmap='magma')

Plot a radar chart of feature means by cluster and write a PNG file.

ANOVA p-values across clusters are annotated on each axis. Dense annotations can appear when many clusters are plotted.

Parameters

NameTypeDefaultDescription
XDataFramerequiredInput dataset of shape (n_samples, n_features).
labelsnp.arrayrequiredHard cluster labels of shape (n_samples,).
outputstrrequiredOutput PNG filename.
framestr'circle'Radar frame shape, "circle" or "polygon". Defaults to "circle".
titlestr'Radar plot'Title of the plot. Defaults to "Radar plot".
cmapstr'magma'Colormap name. Defaults to "magma".

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from neurostatx.clustering.viz import radar_plot
>>> X = pd.DataFrame({"a": [0.0, 0.1, 1.0, 0.9], "b": [0.2, 0.0, 1.1, 1.0],
... "c": [0.1, 0.2, 0.9, 1.0]})
>>> radar_plot(X, np.array([0, 0, 1, 1]), "radar.png")

sort_int_labels_legendfunction#

def sort_int_labels_legend(ax, title=None)

Reorder legend labels numerically and keep matching handles.

Parameters

NameTypeDefaultDescription
axMatplotlib AxesrequiredAxes whose legend labels should be sorted.
titlestrNoneTitle of the legend. Defaults to None.

Returns

legendmatplotlib.legend.Legend
Legend with numerically sorted labels.

Examples

>>> import matplotlib.pyplot as plt
>>> from neurostatx.clustering.viz import sort_int_labels_legend
>>> fig, ax = plt.subplots()
>>> ax.plot([0, 1], [0, 1], label="2")
>>> ax.plot([0, 1], [1, 0], label="1")
>>> legend = sort_int_labels_legend(ax, title="Cluster #")
>>> [t.get_text() for t in legend.get_texts()]
['1', '2']