Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

stats

stats

Provide standalone statistical functions for Adjacency matrices.

Each function takes an Adjacency instance as its first argument (adj).

Methods:

NameDescription
cluster_summaryThis function provides summaries of clusters within Adjacency matrices.
plot_label_distanceCreate a violin plot of within- and between-label distances.
plot_silhouetteCreate a silhouette plot.
r_to_zApply Fisher’s r to z transformation to each element of the data object.
similarityCalculate similarity between two Adjacency matrices.
stats_label_distanceCalculate permutation tests on within and between label distance.
thresholdThreshold an Adjacency instance.
ttestCalculate ttest across samples.
z_to_rConvert z score back into r value for each element of data object.

Methods

cluster_summary

cluster_summary(adj, *, clusters = None, summary = 'mean', scope = 'within')

This function provides summaries of clusters within Adjacency matrices.

It can compute mean/median of within and between cluster values. Requires a list of cluster ids indicating the row/column of each cluster.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instancerequired
clusters(list) list of cluster labelsNone
summary(str) central tendency, ‘mean’ or ‘median’. If None then return all r values‘mean’
scope(str) summarize ‘within’ cluster or ‘between’ clusters‘within’

Returns:

NameTypeDescription
dict(dict) per-cluster summaries

plot_label_distance

plot_label_distance(adj, labels = None, ax = None)

Create a violin plot of within- and between-label distances.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance (must be a single matrix)required
labelsarraynumpy array of labels to plotNone

Returns:

TypeDescription
None

plot_silhouette

plot_silhouette(adj, *, labels = None, ax = None, permutation_test = True, n_permute = 5000, colors = None, figsize = (6, 4))

Create a silhouette plot.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance (must be a single matrix).required
labelsarrayNumpy array of cluster/group labels.None
axMatplotlib axis handle.None
permutation_testboolWhether to run a permutation test. Default True.True
n_permuteintNumber of permutations for the test. Default 5000.5000
colorsOptional list of RGB triplets, one per cluster (default: seaborn ‘hls’ palette).None
figsizeFigure size tuple. Default (6, 4).(6, 4)

Returns:

NameTypeDescription
dictSilhouette plot results including scores and optional permutation p-value.

r_to_z

r_to_z(adj)

Apply Fisher’s r to z transformation to each element of the data object.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance.required

Returns:

NameTypeDescription
AdjacencyNew Adjacency with z-transformed values.

similarity

similarity(adj, data, plot = False, method = '2d', n_permute = 5000, metric = 'spearman', include_diag = False, nan_policy = 'omit', tail = 2, return_null = False, n_jobs = -1, random_state = None, *, project: bool = False, progress_bar: bool = False)

Calculate similarity between two Adjacency matrices.

The default uses Spearman correlation and a permutation test.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance.required
dataAdjacency or arrayAdjacency data, or 1-d array same size as adj.data.required
plotboolIf True, plot stacked adjacency matrices. Default False.False
methodstrpermutation scheme ‘1d’, ‘2d’, or None.‘2d’
n_permuteintNumber of permutations. Default 5000.5000
metricstr‘spearman’, ‘pearson’, or ‘kendall’.‘spearman’
include_diagboolOnly applies to ‘directed’ Adjacency types using method=None or method=‘1d’. Default False (self-similarity is uninformative). Symmetric matrices never store the diagonal, so this flag is a no-op for them.False
nan_policystrHow to handle NaN values. Options: - ‘omit’: Remove NaN values pairwise before computing correlation (default) - ‘propagate’: Allow NaN to propagate through calculations - ‘raise’: Raise an error if NaN values are present‘omit’
tailint | str2‘two’ (two-tailed, default) or 1
return_nullboolIf True, also return the null distribution. Default False.False
n_jobsintNumber of parallel jobs. -1 means all cores. Default -1.-1
random_stateintRandom seed for reproducibility.None
projectboolIf True and adj has a spatial_scale, project the per-matrix correlations back into brain space. Default False.False
progress_barboolIf True, show a progress bar. Default False.False

Returns:

TypeDescription
dictlist

stats_label_distance

stats_label_distance(adj, *, labels = None, n_permute = 5000, n_jobs = -1, progress_bar = False)

Calculate permutation tests on within and between label distance.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance (must be a single matrix)required
labelsarraynumpy array of labels to plotNone
n_permuteintnumber of permutations to run (default=5000)5000

Returns:

NameTypeDescription
dictdictionary of within and between group differences and p-values

threshold

threshold(adj, *, upper = None, lower = None, binarize = False)

Threshold an Adjacency instance.

Provide upper and lower values or percentages to perform two-sided thresholding. Binarize will return a mask image respecting thresholds if provided, otherwise respecting every non-zero value.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instancerequired
upper(float or str) Upper cutoff for thresholding. If string will interpret as percentile; can be None for one-sided thresholding.None
lower(float or str) Lower cutoff for thresholding. If string will interpret as percentile; can be None for one-sided thresholding.None
binarizeboolreturn binarized image respecting thresholds if provided, otherwise binarize on every non-zero value; default FalseFalse

Returns:

NameTypeDescription
Adjacencythresholded Adjacency instance

ttest

ttest(adj, *, permutation = False, n_permute = 5000, tail = 2, return_null = False, n_jobs = -1, random_state = None, progress_bar = False)

Calculate ttest across samples.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance (must contain multiple matrices)required
permutation(bool) Run ttest as permutation. Note this can be very slow.False
n_permuteNumber of permutations (used only when permutation=True). Default 5000.5000
tail2‘two’ (two-tailed, default) or 1
return_nullIf True, also return the null distribution. Default False.False
n_jobsNumber of parallel jobs. Default -1 (all cores).-1
random_stateRandom seed for reproducibility.None
progress_barIf True, show a progress bar. Default False.False

Returns:

NameTypeDescription
out(dict) contains Adjacency instances of t values (or mean if running permutation) and Adjacency instance of p values.

z_to_r

z_to_r(adj)

Convert z score back into r value for each element of data object.

Parameters:

NameTypeDescriptionDefault
adjAdjacencyAdjacency instance.required

Returns:

NameTypeDescription
AdjacencyNew Adjacency with r values.