Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Array of cluster labels. If `mask` is provided, points out of the mask
are assigned label 0.
center_labels : 1D ndarray, shape (n_points,)
Array with centers labeled with their corresponding cluster label.
The rest of points is assigned label 0. Returned only if
``with_centers=True``.
Notes
-----
Valid cluster labels start from 1. If the mask is provided, zeros are
assigned to the points outside the mask.
"""
# Get immediate geodesic distance
a = me.get_ring_distance(surf, n_ring=1, metric='geodesic')
if mask is not None:
a = a[mask, :][:, mask]
a.data = np.exp(-a.data/np.median(a.data))
a.tolil().setdiag(1)
# Embedding
evs, _ = diffusion_mapping(a, n_components=30, alpha=0, diffusion_time=1,
random_state=random_state)
evs = normalize(evs) # To find spherical clusters
if is_size:
n_clusters = evs.shape[0] // n_clusters
# Find clusters
if approach == 'kmeans':
_, cluster_labs, _ = k_means(evs, n_clusters=n_clusters,
# Keep only the temporal lobe.
embedding_tl = embedding[:n_pts_lh][mask_tl]
t1wt2w_tl = t1wt2w_lh[mask_tl]
curv_tl = load_marker('curvature')[0][mask_tl]
###############################################################################
# We will now compute the Moran eigenvectors. This can be done either by
# providing a weight matrix of spatial proximity between each vertex, or by
# providing a cortical surface. Here we’ll use a cortical surface.
from brainspace.null_models import MoranRandomization
# compute spatial weight matrix
w = me.get_ring_distance(surf_lh, n_ring=1, mask=mask_tl)
w.data **= -1
n_rand = 1000
msr = MoranRandomization(n_rep=n_rand, procedure='singleton', tol=1e-6,
random_state=0)
msr.fit(w)
###############################################################################
# Using the Moran eigenvectors we can now compute the randomized data.
curv_rand = msr.randomize(curv_tl)
t1wt2w_rand = msr.randomize(t1wt2w_tl)
:class:`.MoranRandomization`
References
----------
* Wagner H.H. and Dray S. (2015). Generating spatially constrained
null models for irregularly spaced data using Moran spectral
randomization methods. Methods in Ecology and Evolution, 6(10):1169-78.
"""
if spectrum not in ['all', 'nonzero']:
raise ValueError("Unknown autocor '{0}'.".format(spectrum))
# If surface is provided instead of affinity
if not (isinstance(w, np.ndarray) or ssp.issparse(w)):
w = me.get_ring_distance(w, n_ring=n_ring, metric='geodesic')
w.data **= -1 # inverse of distance
# w /= np.nansum(w, axis=1, keepdims=True) # normalize rows
if not is_symmetric(w):
w = make_symmetric(w, check=False, sparse_format='coo')
# Doubly centering weight matrix
if ssp.issparse(w):
m = w.mean(axis=0).A
wc = w.mean() - m - m.T
if not ssp.isspmatrix_coo(w):
w_format = w.format
w = w.tocoo(copy=False)
row, col = w.row, w.col
w = getattr(w, 'to' + w_format)(copy=False)
if no_label is np.nan:
labeled = ~np.isnan(labeling) != 0
else:
labeled = labeling != no_label
ulabs, idx_lab = np.unique(labeling[labeled], return_inverse=True)
n_labs = ulabs.size
n_pts = labeling.size
# Graph matrix
if mode == 'connectivity':
adj = me.get_ring_adjacency(surf, n_ring=n_ring, include_self=False,
dtype=np.float)
else:
adj = me.get_ring_distance(surf, n_ring=n_ring, dtype=np.float)
adj.data[:] = np.exp(-adj.data/n_ring**2)
if mask is not None:
adj = adj[mask][:, mask]
graph_matrix = -alpha * laplacian(adj, normed=True)
diag_mask = (graph_matrix.row == graph_matrix.col)
graph_matrix.data[diag_mask] = 0.0
# Label distributions and label static
lab_dist = np.zeros((n_pts, n_labs))
lab_dist[np.argwhere(labeled)[:, 0], idx_lab] = 1
lab_static = lab_dist.copy()
lab_static *= 1 - alpha