How to use the brainspace.utils.parcellation.map_to_mask function in brainspace

To help you get started, we’ve selected a few brainspace examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MICA-MNI / BrainSpace / brainspace / mesh / mesh_cluster.py View on Github external
axis=1)

        centroid_labs = np.zeros_like(cluster_labs)
        idx_samples = np.arange(points.shape[0])
        for i, lab in enumerate(range(1, n_clusters+1)):
            mask_cl = cluster_labs == lab
            dif = centroids[i] - points[mask_cl]
            idx = np.einsum('ij,ij->i', dif, dif).argmin()
            idx_centroid = idx_samples[mask_cl][idx]
            centroid_labs[idx_centroid] = lab

        if mask is not None:
            centroid_labs = map_to_mask(centroid_labs, mask)

    if mask is not None:
        cluster_labs = map_to_mask(cluster_labs, mask)

    if with_centers:
        return cluster_labs, centroid_labs
    return cluster_labs
github MICA-MNI / BrainSpace / brainspace / mesh / mesh_cluster.py View on Github external
if with_centers:
        points = surf.Points if mask is None else surf.Points[mask]
        centroids = reduce_by_labels(points, cluster_labs, red_op='mean',
                                     axis=1)

        centroid_labs = np.zeros_like(cluster_labs)
        idx_samples = np.arange(points.shape[0])
        for i, lab in enumerate(range(1, n_clusters+1)):
            mask_cl = cluster_labs == lab
            dif = centroids[i] - points[mask_cl]
            idx = np.einsum('ij,ij->i', dif, dif).argmin()
            idx_centroid = idx_samples[mask_cl][idx]
            centroid_labs[idx_centroid] = lab

        if mask is not None:
            centroid_labs = map_to_mask(centroid_labs, mask)

    if mask is not None:
        cluster_labs = map_to_mask(cluster_labs, mask)

    if with_centers:
        return cluster_labs, centroid_labs
    return cluster_labs
github MICA-MNI / BrainSpace / brainspace / mesh / array_operations.py View on Github external
if k == 1:
            feat = d[pids]
        elif red_func[i] == 'mean':
            feat = np.mean(d[pids], axis=1)
        elif red_func[i] == 'weighted_mean':
            feat = np.average(d[pids], weights=w, axis=1)
        elif red_func[i] == 'mode':
            feat = mode(d[pids], axis=1)[0].squeeze()
        elif red_func[i] == 'weighted_mode':
            feat = weighted_mode(d[pids], w, axis=1)[0].squeeze()
            feat = feat.astype(d.dtype)
        else:
            raise ValueError('Unknown red_func: {0}'.format(red_func[i]))

        if target_mask is not None:
            feat = map_to_mask(feat, mask=target_mask, fill=fill)
        resampled[i] = feat

    if append and key is not None:
        for i, feat in enumerate(resampled):
            target.append_array(feat, name=key[i], at='p')

    return resampled if is_list else resampled[0]
github MICA-MNI / BrainSpace / brainspace / mesh / array_operations.py View on Github external
# propagation
    lab_dist_perv = lab_dist
    for i in range(n_iter):
        lab_dist = graph_matrix.dot(lab_dist) + lab_static

        if np.linalg.norm(lab_dist - lab_dist_perv, 'fro') < tol:
            break

        lab_dist_perv = lab_dist

    # lab_dist /= lab_dist.sum(axis=1, keepdims=True)
    new_labeling = labeling.copy()
    new_labeling[~labeled] = ulabs[np.argmax(lab_dist[~labeled], axis=1)]

    if mask is not None:
        new_labeling = map_to_mask(new_labeling, mask)

    return new_labeling
github MICA-MNI / BrainSpace / brainspace / mesh / array_operations.py View on Github external
if mask is not None:
        pts = pts[mask]

    centroids = reduce_by_labels(pts, labeling, axis=1, target_labels=ulab)

    centroid_labs = np.full_like(labeling, non_centroid)
    idx_pts = np.arange(labeling.size)
    for i, c in enumerate(centroids):
        mask_parcel = labeling == ulab[i]
        dif = c - pts[mask_parcel]
        idx = np.einsum('ij,ij->i', dif, dif).argmin()
        idx_centroid = idx_pts[mask_parcel][idx]
        centroid_labs[idx_centroid] = ulab[i]

    if mask is not None:
        centroid_labs = map_to_mask(centroid_labs, mask=mask,
                                    fill=non_centroid)

    return centroid_labs
github MICA-MNI / BrainSpace / brainspace / mesh / array_operations.py View on Github external
# retain = np.ones(pd.shape)
    retain = np.ones((pd.shape[0], 1))
    retain[ws > 0] -= relax

    if np.issubdtype(pd.dtype, np.floating):
        spd = pd.copy()
    else:
        spd = pd.astype(np.float)

    for i in range(n_iter):
        wp = w.dot(spd)
        spd *= retain
        spd += wp

    if mask is not None:
        spd = map_to_mask(spd, mask=mask, axis=1)
        spd[~mask] = point_data[~mask]

    return spd.squeeze() if is_flat else spd