|
| 1 | +from __future__ import print_function |
| 2 | +from __future__ import division |
| 3 | + |
| 4 | +import nibabel as nib |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +def compute_variance_of_cluster(clusters, cluster_index, coords): |
| 9 | + filtered = coords[clusters == cluster_index] |
| 10 | + return ((filtered - filtered.mean(axis=0)) ** 2).sum(axis=1).mean(axis=0) |
| 11 | + |
| 12 | + |
| 13 | +def plot_least_varying(plt, clusters, coords, left, right): |
| 14 | + n_clusters = np.max(clusters) + 1 |
| 15 | + variances = [compute_variance_of_cluster(clusters, k, coords) for k in range(n_clusters)] |
| 16 | + order = np.argsort(variances) |
| 17 | + fig = plt.figure(figsize=(6, 6)) |
| 18 | + ax = fig.gca(projection='3d') |
| 19 | + for k in range(left, right): |
| 20 | + print(variances[order[k]]) |
| 21 | + index = (clusters == order[k]) |
| 22 | + filtered = coords[index] |
| 23 | + ax.scatter(filtered[:, 0], filtered[:, 1], filtered[:, 2], s=5, alpha=0.1) |
| 24 | + |
| 25 | + |
| 26 | +def plot_most_important(plt, clusters, importance, coords, left, right, mode='absolute'): |
| 27 | + a = np.array(importance).copy() |
| 28 | + if mode == 'relative': |
| 29 | + a = np.array(importance).copy() |
| 30 | + n_clusters = np.max(clusters) |
| 31 | + for j in range(n_clusters): |
| 32 | + cnt = np.sum(clusters == j) |
| 33 | + a[j] /= (cnt + 1e-4) |
| 34 | + |
| 35 | + order = np.argsort(-a) |
| 36 | + fig = plt.figure(figsize=(6, 6)) |
| 37 | + ax = fig.gca(projection='3d') |
| 38 | + for k in range(left, right): |
| 39 | + print(a[order[k]]) |
| 40 | + index = (clusters == order[k]) |
| 41 | + filtered = coords[index] |
| 42 | + ax.scatter(filtered[:, 0], filtered[:, 1], filtered[:, 2], s=5, alpha=0.1) |
| 43 | + |
| 44 | + |
| 45 | +def plot_biggest(plt, clusters, coords, left, right): |
| 46 | + n_clusters = np.max(clusters) + 1 |
| 47 | + cnt = [0] * n_clusters |
| 48 | + for j in range(n_clusters): |
| 49 | + cnt[j] = np.sum(clusters == j) |
| 50 | + order = np.argsort(-np.array(cnt)) |
| 51 | + fig = plt.figure(figsize=(6, 6)) |
| 52 | + ax = fig.gca(projection='3d') |
| 53 | + for k in range(left, right): |
| 54 | + print(cnt[order[k]]) |
| 55 | + index = (clusters == order[k]) |
| 56 | + filtered = coords[index] |
| 57 | + ax.scatter(filtered[:, 0], filtered[:, 1], filtered[:, 2], s=5, alpha=0.1) |
| 58 | + |
| 59 | + |
| 60 | +def plot_clusters_probabilistic(plotting, prob_clusters, coords, source_img): |
| 61 | + """ Plot probabilistic atlas. |
| 62 | + :param plotting: nilearn.plotting |
| 63 | + :param prob_clusters: (n_clusters, n_voxels) |
| 64 | + :param coords: (n_voxels, 3) |
| 65 | + :return: |
| 66 | + """ |
| 67 | + X, Y, Z, T = source_img.shape |
| 68 | + a = np.zeros((X, Y, Z)) |
| 69 | + for j in range(prob_clusters.shape[0]): |
| 70 | + for i in range(prob_clusters.shape[1]): |
| 71 | + x = int(coords[i, 0]) |
| 72 | + y = int(coords[i, 1]) |
| 73 | + z = int(coords[i, 2]) |
| 74 | + a[x, y, z, j] = prob_clusters[j, i] |
| 75 | + atlas = nib.Nifti1Image(a, affine=source_img.affine) |
| 76 | + plotting.plot_prob_atlas(atlas, bg_img=False) |
| 77 | + |
| 78 | + |
| 79 | +def plot_clusters(plotting, clusters, coords, source_img, output_file=None, figure=None): |
| 80 | + """ Plot probabilistic atlas. |
| 81 | + :param plotting: nilearn.plotting |
| 82 | + :param clusters: (n_voxels,) |
| 83 | + :param coords: (n_voxels, 3) |
| 84 | + :param output_file: if given the plot is saved here |
| 85 | + :param figure: figure param to be passed to plotting.plot_roi function |
| 86 | + :return: |
| 87 | + """ |
| 88 | + X, Y, Z, T = source_img.shape |
| 89 | + a = np.zeros((X, Y, Z)) |
| 90 | + for i in range(clusters.shape[0]): |
| 91 | + x = int(coords[i, 0]) |
| 92 | + y = int(coords[i, 1]) |
| 93 | + z = int(coords[i, 2]) |
| 94 | + a[x, y, z] = clusters[i] |
| 95 | + img = nib.Nifti1Image(a, affine=source_img.affine) |
| 96 | + return plotting.plot_roi(img, output_file=output_file, figure=figure) |
0 commit comments