Skip to content

Commit

Permalink
documentation: agea volumes examples
Browse files Browse the repository at this point in the history
  • Loading branch information
oliche committed Feb 1, 2024
1 parent f04f02c commit 2eb633d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
"""
In this example we train a neural network to predict the cosmos regions from the gene expression
This demonstrates how to align and sample brain regions relative to the gene expression volumes.
This examples requires `sklearn` and `seaborn` to be installed on top of the `iblatlas` requirements.
"""

import numpy as np
import matplotlib.pyplot as plt
from iblatlas.genomics import agea

df_genes, gene_expression_volumes, atlas_agea = agea.load()
import seaborn as sns
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import sklearn.metrics

igenes = (0,)
fig, axs = plt.subplots(3, 2, sharex=True, sharey=True)
atlas_agea.plot_cslice(0, ax=axs[0, 0])
atlas_agea.plot_cslice(0, ax=axs[1, 0], volume='annotation')
atlas_agea.plot_cslice(0, ax=axs[2, 0], volume=gene_expression_volumes[igenes[0]], cmap='viridis')
atlas_agea.plot_sslice(0, ax=axs[0, 1])
atlas_agea.plot_sslice(0, ax=axs[1, 1], volume='annotation')
atlas_agea.plot_sslice(0, ax=axs[2, 1], volume=gene_expression_volumes[igenes[0]], cmap='viridis')
fig.tight_layout()
from iblatlas.genomics import agea

# %% Load the agea atlas
df_genes, gene_expression_volumes, atlas_agea = agea.load()

# %% remap the the agea atlas at the cosmos level parcellation
import numpy as np
ne = gene_expression_volumes.shape[0]
sel = atlas_agea.label.flatten() != 0 # remove void voxels
# reshape in a big array nexp x nvoxels this takes a little while
Expand All @@ -24,10 +28,6 @@
aids_cosmos = atlas_agea.regions.remap(aids, 'Allen-lr', 'Cosmos')

# %% now we learn to predict the cosmos labels from the gene expression
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import sklearn.metrics
X_train, X_test, y_train, y_test = train_test_split(gexps, aids)
scaler = StandardScaler()
scaler.fit(gexps)
Expand All @@ -40,7 +40,6 @@
classes_labels = atlas_agea.regions.id2acronym(clf.classes_)

# %% Plot the confusion matrix
import seaborn as sns
cm = sklearn.metrics.confusion_matrix(y_test, clf.predict(X_test), normalize='pred')
sklearn.metrics.ConfusionMatrixDisplay(cm, display_labels=classes_labels).plot(ax=plt.gca(), cmap='magma')

Expand Down
88 changes: 88 additions & 0 deletions examples/atlas_genomics_load_agea.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Loading the gene expression atlas"
],
"metadata": {
"collapsed": false
},
"id": "48e25dbdfb3b180c"
},
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from iblatlas.genomics import agea\n",
"\n",
"df_genes, gene_expression_volumes, atlas_agea = agea.load()"
]
},
{
"cell_type": "markdown",
"source": [
"Displaying a coronal and a sagittal slice.\n",
"The top row is the diffusion MRI Allen template, the middle row corresponds to the brain regions annotations and the bottom row corresponds to the first gene expression volume."
],
"metadata": {
"collapsed": false
},
"id": "62db803f3b14fc9"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"igenes = (0,)\n",
"fig, axs = plt.subplots(3, 2, sharex=True, sharey=True)\n",
"atlas_agea.plot_cslice(0, ax=axs[0, 0])\n",
"atlas_agea.plot_cslice(0, ax=axs[1, 0], volume='annotation')\n",
"atlas_agea.plot_cslice(0, ax=axs[2, 0], volume=gene_expression_volumes[igenes[0]], cmap='viridis')\n",
"atlas_agea.plot_sslice(0, ax=axs[0, 1])\n",
"atlas_agea.plot_sslice(0, ax=axs[1, 1], volume='annotation')\n",
"atlas_agea.plot_sslice(0, ax=axs[2, 1], volume=gene_expression_volumes[igenes[0]], cmap='viridis')\n",
"fig.tight_layout()"
],
"metadata": {
"collapsed": false
},
"id": "d05447d166c56453"
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false
},
"id": "9a7afbf944dd385c"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 2eb633d

Please sign in to comment.