From 2eb633d6a99b5fa6ad8f5ccfe5fc5e5b0b4decb9 Mon Sep 17 00:00:00 2001 From: olivier Date: Thu, 1 Feb 2024 09:34:19 +0000 Subject: [PATCH] documentation: agea volumes examples --- ... atlas_genomics_agea_regions_predictor.py} | 33 ++++--- examples/atlas_genomics_load_agea.ipynb | 88 +++++++++++++++++++ 2 files changed, 104 insertions(+), 17 deletions(-) rename examples/{atlas_genomics_load_allen_gene_expression_atlas.py => atlas_genomics_agea_regions_predictor.py} (78%) create mode 100644 examples/atlas_genomics_load_agea.ipynb diff --git a/examples/atlas_genomics_load_allen_gene_expression_atlas.py b/examples/atlas_genomics_agea_regions_predictor.py similarity index 78% rename from examples/atlas_genomics_load_allen_gene_expression_atlas.py rename to examples/atlas_genomics_agea_regions_predictor.py index 6761682..30a2a51 100644 --- a/examples/atlas_genomics_load_allen_gene_expression_atlas.py +++ b/examples/atlas_genomics_agea_regions_predictor.py @@ -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 @@ -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) @@ -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') diff --git a/examples/atlas_genomics_load_agea.ipynb b/examples/atlas_genomics_load_agea.ipynb new file mode 100644 index 0000000..0519fa1 --- /dev/null +++ b/examples/atlas_genomics_load_agea.ipynb @@ -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 +}