2.5 and 2.6 Fine tune, discover, and visualize

2.5 and 2.6 Fine tune, discover, and visualize#

mode = "svg"  # output format for figs

import matplotlib

font = {'family' : 'Dejavu Sans',
        'weight' : 'normal',
        'size'   : 20}

matplotlib.rc('font', **font)

import matplotlib
from matplotlib import pyplot as plt
import os
import urllib
import boto3
from botocore import UNSIGNED
from botocore.client import Config
from graspologic.utils import import_edgelist
import numpy as np
import glob
from tqdm import tqdm

# the AWS bucket the data is stored in
BUCKET_ROOT = "open-neurodata"
parcellation = "Schaefer400"
FMRI_PREFIX = "m2g/Functional/BNU1-11-12-20-m2g-func/Connectomes/" + parcellation + "_space-MNI152NLin6_res-2x2x2.nii.gz/"
FMRI_PATH = os.path.join("datasets", "fmri")  # the output folder
DS_KEY = "abs_edgelist"  # correlation matrices for the networks to exclude

def fetch_fmri_data(bucket=BUCKET_ROOT, fmri_prefix=FMRI_PREFIX,
                    output=FMRI_PATH, name=DS_KEY):
    """
    A function to fetch fMRI connectomes from AWS S3.
    """
    # check that output directory exists
    if not os.path.isdir(FMRI_PATH):
        os.makedirs(FMRI_PATH)
    # start boto3 session anonymously
    s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
    # obtain the filenames
    bucket_conts = s3.list_objects(Bucket=bucket, 
                    Prefix=fmri_prefix)["Contents"]
    for s3_key in tqdm(bucket_conts):
        # get the filename
        s3_object = s3_key['Key']
        # verify that we are grabbing the right file
        if name not in s3_object:
            op_fname = os.path.join(FMRI_PATH, str(s3_object.split('/')[-1]))
            if not os.path.exists(op_fname):
                s3.download_file(bucket, s3_object, op_fname)

def read_fmri_data(path=FMRI_PATH):
    """
    A function which loads the connectomes as adjacency matrices.
    """
    fnames = glob.glob(os.path.join(path, "*.csv"))
    fnames.sort()
    # import edgelists with graspologic
    # edgelists will be all of the files that end in a csv
    networks = [import_edgelist(fname) for fname in tqdm(fnames)]
    return np.stack(networks, axis=0)
fetch_fmri_data()
As = read_fmri_data()
  0%|          | 0/212 [00:00<?, ?it/s]
100%|██████████| 212/212 [00:00<00:00, 221247.19it/s]

  0%|          | 0/106 [00:00<?, ?it/s]
  1%|          | 1/106 [00:00<00:31,  3.33it/s]
  2%|▏         | 2/106 [00:00<00:30,  3.40it/s]
  3%|▎         | 3/106 [00:00<00:30,  3.42it/s]
  4%|▍         | 4/106 [00:01<00:30,  3.36it/s]
  5%|▍         | 5/106 [00:01<00:29,  3.39it/s]
  6%|▌         | 6/106 [00:01<00:29,  3.39it/s]
  7%|▋         | 7/106 [00:02<00:29,  3.39it/s]
  8%|▊         | 8/106 [00:02<00:28,  3.40it/s]
  8%|▊         | 9/106 [00:02<00:28,  3.41it/s]
  9%|▉         | 10/106 [00:02<00:28,  3.42it/s]
 10%|█         | 11/106 [00:03<00:27,  3.41it/s]
 11%|█▏        | 12/106 [00:03<00:27,  3.42it/s]
 12%|█▏        | 13/106 [00:03<00:27,  3.42it/s]
 13%|█▎        | 14/106 [00:04<00:26,  3.42it/s]
 14%|█▍        | 15/106 [00:04<00:26,  3.40it/s]
 15%|█▌        | 16/106 [00:04<00:26,  3.42it/s]
 16%|█▌        | 17/106 [00:04<00:25,  3.43it/s]
 17%|█▋        | 18/106 [00:05<00:25,  3.42it/s]
 18%|█▊        | 19/106 [00:05<00:25,  3.43it/s]
 19%|█▉        | 20/106 [00:05<00:24,  3.44it/s]
 20%|█▉        | 21/106 [00:06<00:24,  3.43it/s]
 21%|██        | 22/106 [00:06<00:24,  3.40it/s]
 22%|██▏       | 23/106 [00:06<00:24,  3.40it/s]
 23%|██▎       | 24/106 [00:07<00:24,  3.41it/s]
 24%|██▎       | 25/106 [00:07<00:23,  3.41it/s]
 25%|██▍       | 26/106 [00:07<00:23,  3.42it/s]
 25%|██▌       | 27/106 [00:07<00:23,  3.43it/s]
 26%|██▋       | 28/106 [00:08<00:22,  3.44it/s]
 27%|██▋       | 29/106 [00:08<00:22,  3.42it/s]
 28%|██▊       | 30/106 [00:08<00:22,  3.43it/s]
 29%|██▉       | 31/106 [00:09<00:21,  3.44it/s]
 30%|███       | 32/106 [00:09<00:21,  3.40it/s]
 31%|███       | 33/106 [00:09<00:21,  3.42it/s]
 32%|███▏      | 34/106 [00:09<00:21,  3.41it/s]
 33%|███▎      | 35/106 [00:10<00:20,  3.41it/s]
 34%|███▍      | 36/106 [00:10<00:20,  3.39it/s]
 35%|███▍      | 37/106 [00:10<00:20,  3.40it/s]
 36%|███▌      | 38/106 [00:11<00:19,  3.41it/s]
 37%|███▋      | 39/106 [00:11<00:19,  3.39it/s]
 38%|███▊      | 40/106 [00:11<00:19,  3.39it/s]
 39%|███▊      | 41/106 [00:12<00:19,  3.40it/s]
 40%|███▉      | 42/106 [00:12<00:18,  3.41it/s]
 41%|████      | 43/106 [00:12<00:18,  3.36it/s]
 42%|████▏     | 44/106 [00:12<00:18,  3.38it/s]
 42%|████▏     | 45/106 [00:13<00:17,  3.39it/s]
 43%|████▎     | 46/106 [00:13<00:17,  3.37it/s]
 44%|████▍     | 47/106 [00:13<00:17,  3.38it/s]
 45%|████▌     | 48/106 [00:14<00:17,  3.41it/s]
 46%|████▌     | 49/106 [00:14<00:16,  3.42it/s]
 47%|████▋     | 50/106 [00:14<00:16,  3.42it/s]
 48%|████▊     | 51/106 [00:14<00:16,  3.43it/s]
 49%|████▉     | 52/106 [00:15<00:15,  3.43it/s]
 50%|█████     | 53/106 [00:15<00:15,  3.39it/s]
 51%|█████     | 54/106 [00:15<00:15,  3.39it/s]
 52%|█████▏    | 55/106 [00:16<00:14,  3.41it/s]
 53%|█████▎    | 56/106 [00:16<00:14,  3.42it/s]
 54%|█████▍    | 57/106 [00:16<00:14,  3.41it/s]
 55%|█████▍    | 58/106 [00:17<00:14,  3.42it/s]
 56%|█████▌    | 59/106 [00:17<00:13,  3.43it/s]
 57%|█████▋    | 60/106 [00:18<00:30,  1.50it/s]
 58%|█████▊    | 61/106 [00:19<00:24,  1.81it/s]
 58%|█████▊    | 62/106 [00:19<00:20,  2.13it/s]
 59%|█████▉    | 63/106 [00:19<00:17,  2.40it/s]
 60%|██████    | 64/106 [00:19<00:15,  2.66it/s]
 61%|██████▏   | 65/106 [00:20<00:14,  2.87it/s]
 62%|██████▏   | 66/106 [00:20<00:13,  3.03it/s]
 63%|██████▎   | 67/106 [00:20<00:12,  3.13it/s]
 64%|██████▍   | 68/106 [00:21<00:11,  3.23it/s]
 65%|██████▌   | 69/106 [00:21<00:11,  3.32it/s]
 66%|██████▌   | 70/106 [00:21<00:10,  3.36it/s]
 67%|██████▋   | 71/106 [00:21<00:10,  3.42it/s]
 68%|██████▊   | 72/106 [00:22<00:09,  3.44it/s]
 69%|██████▉   | 73/106 [00:22<00:09,  3.47it/s]
 70%|██████▉   | 74/106 [00:22<00:09,  3.44it/s]
 71%|███████   | 75/106 [00:23<00:08,  3.46it/s]
 72%|███████▏  | 76/106 [00:23<00:08,  3.45it/s]
 73%|███████▎  | 77/106 [00:23<00:08,  3.46it/s]
 74%|███████▎  | 78/106 [00:24<00:08,  3.45it/s]
 75%|███████▍  | 79/106 [00:24<00:07,  3.47it/s]
 75%|███████▌  | 80/106 [00:24<00:07,  3.49it/s]
 76%|███████▋  | 81/106 [00:24<00:07,  3.43it/s]
 77%|███████▋  | 82/106 [00:25<00:06,  3.45it/s]
 78%|███████▊  | 83/106 [00:25<00:06,  3.44it/s]
 79%|███████▉  | 84/106 [00:25<00:06,  3.38it/s]
 80%|████████  | 85/106 [00:26<00:06,  3.39it/s]
 81%|████████  | 86/106 [00:26<00:05,  3.41it/s]
 82%|████████▏ | 87/106 [00:26<00:05,  3.43it/s]
 83%|████████▎ | 88/106 [00:26<00:05,  3.40it/s]
 84%|████████▍ | 89/106 [00:27<00:04,  3.41it/s]
 85%|████████▍ | 90/106 [00:27<00:04,  3.43it/s]
 86%|████████▌ | 91/106 [00:27<00:04,  3.45it/s]
 87%|████████▋ | 92/106 [00:28<00:04,  3.47it/s]
 88%|████████▊ | 93/106 [00:28<00:03,  3.48it/s]
 89%|████████▊ | 94/106 [00:28<00:03,  3.48it/s]
 90%|████████▉ | 95/106 [00:28<00:03,  3.43it/s]
 91%|█████████ | 96/106 [00:29<00:02,  3.46it/s]
 92%|█████████▏| 97/106 [00:29<00:02,  3.48it/s]
 92%|█████████▏| 98/106 [00:29<00:02,  3.48it/s]
 93%|█████████▎| 99/106 [00:30<00:02,  3.48it/s]
 94%|█████████▍| 100/106 [00:30<00:01,  3.50it/s]
 95%|█████████▌| 101/106 [00:30<00:01,  3.51it/s]
 96%|█████████▌| 102/106 [00:30<00:01,  3.43it/s]
 97%|█████████▋| 103/106 [00:31<00:00,  3.46it/s]
 98%|█████████▊| 104/106 [00:31<00:00,  3.47it/s]
 99%|█████████▉| 105/106 [00:31<00:00,  3.48it/s]
100%|██████████| 106/106 [00:32<00:00,  3.46it/s]
100%|██████████| 106/106 [00:32<00:00,  3.30it/s]

from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.pipeline import Pipeline
from graspologic.utils import pass_to_ranks

def remove_isolates(A):
    """
    A function which removes isolated nodes from the 
    adjacency matrix A.
    """
    degree = A.sum(axis=0)  # sum along the rows to obtain the node degree
    out_degree = A.sum(axis=1)
    A_purged = A[~(degree == 0),:]
    A_purged = A_purged[:,~(degree == 0)]
    return A_purged

class CleanData(BaseEstimator, TransformerMixin):

    def fit(self, X):
        return self

    def transform(self, X):
        Acleaned = remove_isolates(X)
        A_abs_cl = np.abs(Acleaned)
        self.A_ = A_abs_cl
        return self.A_

class FeatureScaler(BaseEstimator, TransformerMixin):
    
    def fit(self, X):
        return self
    
    def transform(self, X):
        A_scaled = pass_to_ranks(X)
        return (A_scaled)

num_pipeline = Pipeline([
    ('cleaner', CleanData()),
    ('scaler', FeatureScaler()),
])

As_xfm = [num_pipeline.fit_transform(A) for A in As]
from graspologic.embed import MultipleASE
from graspologic.plot import pairplot
from matplotlib import pyplot as plt

embedding = MultipleASE(n_components=5, svd_seed=0).fit_transform(As_xfm)
fig = pairplot(embedding, title="(A) Multiple spectral embedding of all connectomes")

fig.tight_layout()
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1513: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=vector, **plot_kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1513: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=vector, **plot_kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1513: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=vector, **plot_kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1513: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=vector, **plot_kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1513: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=vector, **plot_kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/seaborn/axisgrid.py:1615: UserWarning: Ignoring `palette` because no `hue` variable has been assigned.
  func(x=x, y=y, **kwargs)
<seaborn.axisgrid.PairGrid at 0x7f2654ccd040>
../../_images/87865906ac05edb3f1282b9575cfeffe37b14741b11ccdbba298c152afc60b05.png
from graspologic.cluster import AutoGMMCluster

labels = AutoGMMCluster(max_components=10, random_state=0).fit_predict(embedding)
fig = pairplot(embedding, labels=labels,
                title="Multiple spectral embedding of all connectomes", 
                legend_name="Predicted Clusters")

fig.tight_layout()

fname = "mase"
if mode != "png":
    os.makedirs(f"Figures/{mode:s}", exist_ok=True)
    fig.savefig(f"Figures/{mode:s}/{fname:s}.{mode:s}")

os.makedirs("Figures/png", exist_ok=True)
fig.savefig(f"Figures/png/{fname:s}.png")
../../_images/3a6f203dda9cfd49919af707ef44330072ad5ccefed70aa5381bed7fe6b27199.png
from urllib import request
import json
import pandas as pd
from pathlib import Path

coord_dest = os.path.join(FMRI_PATH, "coordinates.json")
request.urlretrieve("https://github.com/neurodata/neuroparc/" + "raw/master/atlases/label/Human/Metadata-json/" + parcellation + "_space-MNI152NLin6_res-2x2x2.json", coord_dest)

with open(coord_dest) as coord_f:
    coords = []
    for roiname, contents in json.load(coord_f)["rois"].items():
        try:
            if roiname != "0":
                coord_roi = {"x" : contents["center"][0], "y" : contents["center"][1], "z" : contents["center"][2]}
                coords.append(coord_roi)
        except:
            continue
Path("./Images/").mkdir(parents=True, exist_ok=True)
request.urlretrieve("https://github.com/neurodata/graph-stats-book/" +
                    "blob/master/network_machine_learning_in_python/" +
                    "foundations/ch2/Images/lobes.png?raw=true", "./Images/lobes.png")
            
coords_df = pd.DataFrame(coords)
import matplotlib.image as mpimg
import seaborn as sns

coords_df["Community"] = labels
coords_df['Community'] = coords_df['Community'].astype('category')
fig, axs = plt.subplots(1, 2, figsize=(18, 6))
axs[0].imshow(mpimg.imread('./Images/lobes.png'))
axs[0].set_axis_off()
sns.scatterplot(x="y", y="z", data=coords_df, hue="Community", ax=axs[1])
axs[1].set_ylabel("Bottom to Top (mm)")
axs[1].set_xlabel("Back to Front (mm)")
axs[1].legend(title="Predicted Community", bbox_to_anchor=(1, 1))

fig.tight_layout()

fname = "brain_preds"
if mode != "png":
    fig.savefig(f"Figures/{mode:s}/{fname:s}.{mode:s}")

fig.savefig(f"Figures/png/{fname:s}.png")
../../_images/d8931ecc6d991467745648a0d01f631a1e0b3a5fc62a05ab1360cfbcb214ba55.png
import contextlib
import datasets.dice as dice
from sklearn.metrics import confusion_matrix
from graphbook_code import cmaps

group_dest = os.path.join("./datasets/", "Yeo-7_space-MNI152NLin6_res-2x2x2.nii.gz")
request.urlretrieve("https://github.com/neurodata/neuroparc/" + "blob/master/atlases/label/Human/" +
                    "Yeo-7_space-MNI152NLin6_res-2x2x2.nii.gz?raw=true", group_dest)
roi_dest = os.path.join("./datasets/", parcellation + "_space-MNI152NLin6_res-2x2x2.nii.gz")
request.urlretrieve("https://github.com/neurodata/neuroparc/" + "blob/master/atlases/label/Human/" + 
                    parcellation + "_space-MNI152NLin6_res-2x2x2.nii.gz?raw=true", roi_dest)

dicemap, _, _ = dice.dice_roi("./datasets/", "./datasets", 
                              "Yeo-7_space-MNI152NLin6_res-2x2x2.nii.gz", 
                              parcellation + "_space-MNI152NLin6_res-2x2x2.nii.gz",
                              verbose=False)
actual_cluster = np.argmax(dicemap, axis=0)[1:] - 1

# make confusion matrix
cf_matrix = confusion_matrix(actual_cluster, labels)
# and plot it
fig, ax = plt.subplots(1,1, figsize=(5, 4))
sns.heatmap(cf_matrix, cmap=cmaps["sequential"], ax=ax)
ax.set_title("Confusion matrix")
ax.set_ylabel("True Parcel")
ax.set_xlabel("Predicted Community")
ax.set_xlim((0, 4))
fig.tight_layout()

fname = "cf_mtx"
if mode != "png":
    fig.savefig(f"Figures/{mode:s}/{fname:s}.{mode:s}")

fig.savefig(f"Figures/png/{fname:s}.png")
../../_images/e4121f92c854e67cc2c2dad4a42dc15cb63a8d2388717022baf5de7b51b197dc.png