Skip to content

Commit

Permalink
fix: various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Apr 26, 2024
1 parent ece3225 commit f4fb40d
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 33 deletions.
5 changes: 3 additions & 2 deletions docs/notebook/02-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,11 @@
"metadata": {},
"outputs": [],
"source": [
"from eddymotion.data.splitting import lovo_split as logo_split\n",
"from eddymotion.viz import plot_dwi\n",
"\n",
"data_train, data_test = dmri_dataset.logo_split(10)\n",
"plot_dwi(data_test[0], dmri_dataset.affine, gradient=data_test[1]);"
"data_train, data_test = logo_split(dmri_dataset, 10)\n",
"plot_dwi(np.squeeze(data_test[0]), dmri_dataset.affine, gradient=data_test[1]);"
]
},
{
Expand Down
71 changes: 59 additions & 12 deletions docs/notebook/03-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "73bc033f",
"metadata": {},
"source": [
"# Diffusion modeling"
Expand All @@ -10,6 +11,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "86034e8b",
"metadata": {
"tags": [
"hide-cell"
Expand All @@ -18,12 +20,14 @@
"outputs": [],
"source": [
"import warnings\n",
"import numpy as np\n",
"\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"id": "16e127b6",
"metadata": {},
"source": [
"The proposed method requires inferring a motion-less, reference DW map for a given diffusion orientation for which we want to estimate the misalignment.\n",
Expand All @@ -46,16 +50,19 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3225815e",
"metadata": {},
"outputs": [],
"source": [
"from eddymotion.dmri import DWI\n",
"from eddymotion.data.dmri import DWI\n",
"from eddymotion.data.splitting import lovo_split as logo_split\n",
"from eddymotion.viz import plot_dwi\n",
"dmri_dataset = DWI.from_filename(\"../../data/dwi.h5\")"
]
},
{
"cell_type": "markdown",
"id": "1a677d9e",
"metadata": {},
"source": [
"## Implementing a trivial model\n",
Expand All @@ -73,6 +80,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2875c19b",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -106,6 +114,7 @@
},
{
"cell_type": "markdown",
"id": "4ddb8436",
"metadata": {},
"source": [
"The model can easily be initialized as follows (assuming we still have our dataset loaded):"
Expand All @@ -114,6 +123,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "19da1464",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -125,6 +135,7 @@
},
{
"cell_type": "markdown",
"id": "52034adb",
"metadata": {},
"source": [
"Then, at each iteration of our estimation strategy, we will fit this model to the data, after holding one particular direction (`data_test`) out, using the `logo_split` method of the dataset. In every iteration, this finds the b=0 volumes in the data and averages their values in every voxel:"
Expand All @@ -133,15 +144,17 @@
{
"cell_type": "code",
"execution_count": null,
"id": "6a65d321",
"metadata": {},
"outputs": [],
"source": [
"data_train, data_test = dmri_dataset.logo_split(10)\n",
"data_train, data_test = logo_split(dmri_dataset, 10)\n",
"model.fit(data_train[0])"
]
},
{
"cell_type": "markdown",
"id": "dc3f7299",
"metadata": {},
"source": [
"Finally, we can generate our registration reference with the `predict()` method:"
Expand All @@ -150,6 +163,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5229302c",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -159,6 +173,7 @@
},
{
"cell_type": "markdown",
"id": "dce0260f",
"metadata": {},
"source": [
"As expected, the *b=0* doesn't look very much like the particular left-out direction, but it is a start!"
Expand All @@ -167,14 +182,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9b6dd61a",
"metadata": {},
"outputs": [],
"source": [
"plot_dwi(data_test[0], dmri_dataset.affine, gradient=data_test[1]);"
"plot_dwi(np.squeeze(data_test[0]), dmri_dataset.affine, gradient=data_test[1]);"
]
},
{
"cell_type": "markdown",
"id": "231a0729",
"metadata": {},
"source": [
"## Implementing a *regression to the mean* model\n",
Expand All @@ -187,6 +204,11 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95130912",
"metadata": {},
"outputs": [],
"source": [
"class AverageDWModel:\n",
" \"\"\"A trivial model that returns an average map.\"\"\"\n",
Expand All @@ -204,14 +226,11 @@
" def predict(self, gradient, **kwargs):\n",
" \"\"\"Return the average map.\"\"\"\n",
" return self._data"
],
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"id": "893ae6de",
"metadata": {},
"source": [
"**Exercise**\n",
Expand All @@ -224,6 +243,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "77c74c0e",
"metadata": {
"tags": [
"hide-cell"
Expand All @@ -234,6 +254,7 @@
},
{
"cell_type": "markdown",
"id": "63a5bc6b",
"metadata": {},
"source": [
"## Investigating the tensor model\n",
Expand All @@ -245,6 +266,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8dda846f",
"metadata": {
"tags": [
"remove-cell"
Expand All @@ -267,11 +289,12 @@
"\n",
"dmri_dataset = DWI.from_filename(datapath)\n",
"datapath.unlink()\n",
"data_train, data_test = dmri_dataset.logo_split(88, with_b0=True)"
"data_train, data_test = logo_split(dmri_dataset, 88, with_b0=True)"
]
},
{
"cell_type": "markdown",
"id": "f9b5a9b0",
"metadata": {},
"source": [
"### The model factory\n",
Expand All @@ -283,6 +306,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3a957d5e",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -299,6 +323,7 @@
},
{
"cell_type": "markdown",
"id": "3157aa22",
"metadata": {},
"source": [
"### Leveraging the `fit()` / `predict()` API\n",
Expand All @@ -309,6 +334,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "615b8a23",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -318,6 +344,7 @@
},
{
"cell_type": "markdown",
"id": "1428316d",
"metadata": {},
"source": [
"Now, the predicted map for the particular ***b*** gradient looks much closer to the original:"
Expand All @@ -326,6 +353,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2d1c921a",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -334,6 +362,7 @@
},
{
"cell_type": "markdown",
"id": "17ac6e64",
"metadata": {},
"source": [
"Here's the original DW map, for reference:"
Expand All @@ -342,14 +371,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "41078b66",
"metadata": {},
"outputs": [],
"source": [
"plot_dwi(data_test[0], dmri_dataset.affine, gradient=data_test[1]);"
"plot_dwi(np.squeeze(data_test[0]), dmri_dataset.affine, gradient=data_test[1]);"
]
},
{
"cell_type": "markdown",
"id": "d1603ab8",
"metadata": {},
"source": [
"**Exercise**\n",
Expand All @@ -361,6 +392,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "25b9ff4d",
"metadata": {
"tags": [
"hide-cell"
Expand All @@ -371,6 +403,7 @@
},
{
"cell_type": "markdown",
"id": "40c6b7ec",
"metadata": {},
"source": [
"**Exercise**\n",
Expand All @@ -383,12 +416,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "267008fe",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "9b947d74",
"metadata": {},
"source": [
"## Next steps: image registration\n",
Expand All @@ -399,11 +434,23 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading

0 comments on commit f4fb40d

Please sign in to comment.