Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tutorials #199

Merged
merged 7 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/colab.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. highlight:: shell

============================
Use DIRECT with Google Colab
============================



1. First mount your Google drive in Colab and create a directory named, e.g. `DIRECT`, and `cd` there:

.. code-block:: ipython3

%cd /content/drive/MyDrive/DIRECT/

This `notebook <https://colab.research.google.com/notebooks/io.ipynb>`_ can help with mounting your Google drive.


2. Clone the repo:

.. code-block:: ipython3

!git clone https://github.com/NKI-AI/direct.git

3. Copy paste and run the following

.. code-block:: ipython3

!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python -m ipykernel install --name "py38" --user

The above block is needed to install python 3.8 in Colab as it runs using Python 3.7.

4. Run the following to install the latest `PyTorch` version:

.. code-block:: ipython3

!pip3 uninstall torch
!pip3 uninstall torchvision
!pip3 uninstall torchaudio
!pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

5. Navigate to the repo:

.. code-block:: ipython3

%cd direct/

6. Install package.

.. code-block:: ipython3

!python3 setup.py install

OR

.. code-block:: ipython3

!python3 -m pip install -e ".[dev]"

7. Run experiments using the configuration files in the `projects <https://github.com/NKI-AI/direct/tree/main/projects>`_ folder,
or you can set up your own configuration files following our `template <https://docs.aiforoncology.nl/direct/config.html>`_.
1 change: 1 addition & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. include:: ./examples/calgary_campinas_dataset.rst
99 changes: 99 additions & 0 deletions docs/examples/calgary_campinas_dataset.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
=======================================
Setting up the Calgary-Campinas Dataset
=======================================

Imports
-------

.. code:: ipython3

from functools import partial

import numpy as np
import matplotlib.pyplot as plt


from direct.data.datasets import CalgaryCampinasDataset
from direct.data.mri_transforms import build_mri_transforms
from direct.data.transforms import fft2, ifft2, modulus, root_sum_of_squares
from direct.common.subsample import CalgaryCampinasMaskFunc

Define forward and backward transforms
--------------------------------------


.. code:: ipython3

forward_operator = partial(fft2, dim=(1,2), centered=False)
backward_operator = partial(ifft2, dim=(1,2), centered=False)

Define sub-sampling function
----------------------------

.. code:: ipython3

mask_func = CalgaryCampinasMaskFunc(accelerations=[5, 10])

Define MRI transforms
---------------------

.. code:: ipython3

transforms = build_mri_transforms(
forward_operator=forward_operator,
backward_operator=backward_operator,
mask_func=mask_func,
scaling_key="masked_kspace",
)

Create the Dataset
------------------

.. code:: ipython3

# root pointing to the Calgary-Campinas data directory
root = "<...>"
dataset = CalgaryCampinasDataset(
root = root,
transform=transforms,
crop_outer_slices=True,
)

Investigate a sample
--------------------

.. code:: ipython3

sample = dataset[20]

print("Sample keys: ", sample.keys())

Visualizations
~~~~~~~~~~~~~~

.. code:: ipython3

plt.imshow(sample["target"], cmap='gray')
plt.axis("off")
plt.title("Target")
plt.show()

plt.imshow(sample["sampling_mask"].squeeze(), cmap='gray')
plt.axis("off")
plt.title("Sub-Sampling Mask")
plt.show()

.. code:: ipython3

masked_kspace = sample["masked_kspace"]
print("K-space shape: (Coils, Height, Width, Complex) = ", tuple(masked_kspace.shape))

backprojected_kspace = backward_operator(masked_kspace)

fig, axs = plt.subplots(1, masked_kspace.shape[0], figsize=(20,50))

for i in range(masked_kspace.shape[0]):
axs[i].imshow(modulus(backprojected_kspace[i]))
axs[i].axis("off")
axs[i].set_title(f"Coil {i+1}")
plt.show()
7 changes: 7 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ images such as MRIs from partially observed or noisy input data.
datasets
samplers

.. toctree::
:maxdepth: 1
:caption: Examples

examples.rst
colab.rst

.. toctree::
:maxdepth: 1
:caption: Model Zoo
Expand Down