Skip to content

Commit

Permalink
update figures script
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioAPeraza committed Jul 12, 2023
1 parent 317e6ca commit 8de4bc7
Show file tree
Hide file tree
Showing 24 changed files with 4,262 additions and 7,047 deletions.
116 changes: 116 additions & 0 deletions figures/01_connectivity-matrix.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os.path as op\n",
"\n",
"import numpy as np\n",
"import nibabel as nib\n",
"from nilearn import plotting\n",
"\n",
"sys.path.append(op.abspath(\"../\"))\n",
"import utils"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_dir = op.abspath(op.join(\"..\", \"data\"))\n",
"template_dir = op.join(data_dir, \"templates\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"subcortical_fn = op.join(template_dir, \"rois-subcortical_mni152_mask.nii.gz\")\n",
"subcort_img = nib.load(subcortical_fn)\n",
"subcort_dat = subcort_img.get_fdata()\n",
"subcort_mask = subcort_dat != 0\n",
"n_subcort_vox = np.where(subcort_mask)[0].shape[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dcon_img = nib.load(\n",
" op.join(\n",
" data_dir, \"hcp\", \"HCP_S1200_1003_rfMRI_MSMAll_groupPCA_d4500ROW_zcorr.dconn.nii\"\n",
" )\n",
" )\n",
"dcon_mtx = np.tanh(dcon_img.get_fdata()) # 91,282 X 91,282 grayordinates\n",
"\n",
"del dcon_img"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cort_mtx = dcon_mtx[: dcon_mtx.shape[0] - n_subcort_vox, : dcon_mtx.shape[0] - n_subcort_vox] # 59,412 X 59,412 vertex\n",
"del dcon_mtx"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plotting.plot_matrix(cort_mtx, colorbar=True, vmax=1, vmin=-1, cmap=\"spectral\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Calculate affinity matrix\n",
"affin_mtx = utils.affinity(dcon_mtx, 90)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 ('gradec_env')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "32eae8b81dca7564140c4bb02978cc1f5cb3ca50d75c604cb4f67ffb4db99fb5"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
188 changes: 188 additions & 0 deletions figures/02_gradient.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Example for plotting gradient data\"\"\"\n",
"import os.path as op\n",
"from glob import glob\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from utils import plot_gradient, plot_subcortical_gradient"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualize Gradients"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Margulies 2016 (HCP S900)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"from neuromaps.datasets import fetch_annotation\n",
"\n",
"data_dir = \"../data\"\n",
"neuromaps_dir = op.join(data_dir, \"neuromaps-data\")\n",
"\n",
"marg_grad_out_path = \"../figures/Fig/gradient\"\n",
"grad_marg_lh, grad_marg_rh = fetch_annotation(\n",
" source=\"margulies2016\",\n",
" desc=\"fcgradient01\",\n",
" space=\"fsLR\",\n",
" den=\"32k\",\n",
" data_dir=neuromaps_dir,\n",
")\n",
"\n",
"plot_gradient(data_dir, [(grad_marg_lh, grad_marg_rh)], cbar=True, out_dir=marg_grad_out_path)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Peraza 2022 (HCP S1200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jperaza_grad_path = op.abspath(\"../results/gradient\")\n",
"jperaza_grad_out_path = \"../figures/Fig/gradient\"\n",
"jperaza_grad_lh_fnames = sorted(glob(op.join(jperaza_grad_path, \"*hemi-L_feature.func.gii\")))\n",
"jperaza_grad_rh_fnames = sorted(glob(op.join(jperaza_grad_path, \"*hemi-R_feature.func.gii\")))\n",
"jperaza_subcort_grad_fnames = sorted(glob(op.join(jperaza_grad_path, \"*_feature.nii.gz\")))\n",
"jperaza_grad_fnames = zip(jperaza_grad_lh_fnames, jperaza_grad_rh_fnames)\n",
"\n",
"plot_gradient(\"../data\", jperaza_grad_fnames, cbar=True, out_dir=jperaza_grad_out_path)\n",
"# plot_subcortical_gradient(jperaza_subcort_grad_fnames, threshold_=0.01)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lambdas = np.load(\"../results/gradient/lambdas.npy\")\n",
"lambdas = lambdas[:9]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"sns.set_style(\"ticks\",{'axes.grid' : True})\n",
"# sns.set(style=\"white\")\n",
"\n",
"_, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(3.5, 6))\n",
"#ax1.set_xlabel(\"Component Nb\")\n",
"\n",
"\n",
"ax1.set_ylabel(\"Explained Variance Ratio\")\n",
"ax2.set_ylabel(\"Difference in Explained\\nVariance Ratio\")\n",
"x1 = range(1, lambdas.size + 1, 1)\n",
"x2 = range(1, lambdas.size, 1)\n",
"y1 = (100 * lambdas) / lambdas.sum()\n",
"y2 = 100 * (lambdas[:-1] - lambdas[1:]) / lambdas.sum()\n",
"\n",
"sns.lineplot(\n",
" x=x1,\n",
" y=y1,\n",
" marker=\"o\",\n",
" color=\"#1D5B79\",\n",
" ax=ax1,\n",
")\n",
"sns.lineplot(\n",
" x=x2,\n",
" y=y2,\n",
" marker=\"o\",\n",
" color=\"#1D5B79\",\n",
" ax=ax2,\n",
")\n",
"ax1.set_xticks(x1)\n",
"ax1.set_xticklabels(x1)\n",
"ax1.set_xlabel(\"Component\")\n",
"\n",
"x2_lbs = [f\"{x}-{x+1}\" for x in x2]\n",
"ax2.set_xticks(x2)\n",
"ax2.set_xticklabels(x2_lbs) \n",
"ax2.set_xlabel(\"Component Difference ($C_{i} - C_{i+1}$)\")\n",
"\n",
"plt.tight_layout()\n",
"plt.savefig(op.join(\"./\", \"Fig\", \"Fig-S2.png\"), bbox_inches=\"tight\", dpi=500)\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 ('gradec_env')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "32eae8b81dca7564140c4bb02978cc1f5cb3ca50d75c604cb4f67ffb4db99fb5"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 8de4bc7

Please sign in to comment.