Skip to content

Commit

Permalink
Merge pull request #108 from juriarte62/main
Browse files Browse the repository at this point in the history
add jupyter notebook for denoising

fix ci issues, add pages deployment ci [WIP]
  • Loading branch information
bilgelm authored Sep 12, 2024
2 parents 81d123d + 1265998 commit 7a6fc62
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 74 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Deploy Sphinx documentation to Pages

on:
push:
branches: [master] # branch to trigger deployment

jobs:
pages:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
permissions:
pages: write
id-token: write
steps:
- id: deployment
uses: sphinx-notes/pages@v3
9 changes: 4 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- { python: "3.12", os: "ubuntu-latest", session: "tests" }
- { python: "3.12", os: "ubuntu-latest", session: "typeguard" }
- { python: "3.12", os: "ubuntu-latest", session: "xdoctest" }
- { python: "3.12", os: "ubuntu-latest", session: "docs-build" }
- { python: "3.11", os: "ubuntu-latest", session: "mypy" }
- { python: "3.11", os: "ubuntu-latest", session: "tests" }
- { python: "3.11", os: "ubuntu-latest", session: "xdoctest" }
Expand Down Expand Up @@ -46,8 +45,8 @@ jobs:
- name: Check out the repository
uses: actions/checkout@v4

- name: Set up R if not on Windows
if: matrix.os != 'windows-latest'
- name: Set up R if on MacOS
if: matrix.os == 'macos-latest'
uses: r-lib/actions/setup-r@v2

- name: Install remotes R package if on MacOS
Expand Down Expand Up @@ -128,14 +127,14 @@ jobs:
include-hidden-files: true

- name: Upload documentation
if: matrix.session == 'docs-build'
if: matrix.session == 'docs-build' && matrix.os == 'macos-latest'
uses: actions/upload-artifact@v3
with:
name: docs
path: docs/_build

coverage:
runs-on: ubuntu-latest
runs-on: macos-latest
needs: tests
steps:
- name: Check out the repository
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ caption: Notebooks:
---
notebooks/basics
notebooks/denoise
```
81 changes: 15 additions & 66 deletions docs/notebooks/basics.ipynb

Large diffs are not rendered by default.

245 changes: 245 additions & 0 deletions docs/notebooks/denoise.ipynb

Large diffs are not rendered by default.

145 changes: 145 additions & 0 deletions docs/notebooks/denoise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.4
# kernelspec:
# display_name: dynamicpet-9lkmTgRy-py3.12
# language: python
# name: python3
# ---

# %% [markdown]
# # Denoising

# %% [markdown]
# This notebook illustrates 4-D image denoising with [_Dynamic PET_].
#
# First, we download a 4-D PET image with its [PET-BIDS] json sidecar from
# [OpenNeuro](https://openneuro.org/):
#
# [_Dynamic PET_]: https://github.com/bilgelm/dynamicpet
# [PET-BIDS]: https://bids-specification.readthedocs.io/en/stable/modality-specific-files/positron-emission-tomography.html

# %%
from pathlib import Path

import requests


outdir = Path.cwd() / "nb_data"
outdir.mkdir(exist_ok=True)

petjson_fname = outdir / "pet_to_denoise.json"
pet_fname = outdir / "pet_to_denoise.nii.gz"

# we will download the PET for the baseline session for subject 01 from
# https://openneuro.org/datasets/ds001420/versions/1.2.0
baseurl = "https://s3.amazonaws.com/openneuro.org/ds001420/sub-01/ses-baseline/"

peturl = (
baseurl
+ "pet/sub-01_ses-baseline_pet.nii.gz"
+ "?versionId=8Qon4IjB8ejnZq7JgCUlFJhLUYSG1zJB"
)

if not petjson_fname.exists():
r = requests.get(
baseurl
+ "pet/sub-01_ses-baseline_pet.json"
+ "?versionId=rLpwPCPOzgW1MduO53VKxsKGuD5K0j5R",
timeout=10,
)
r.raise_for_status()
with open(petjson_fname, "wb") as f:
f.write(r.content)

if not pet_fname.exists():
with requests.get(peturl, timeout=10, stream=True) as r:
r.raise_for_status()
with open(pet_fname, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)

# %% [markdown]
# ## Denoise
#
# We read in a PET image and apply HYPR-LR denoising.

# %%
from dynamicpet.denoise.hypr import hypr_lr
from dynamicpet.petbids.petbidsimage import load


pet = load(pet_fname)
pet_hyprlr = hypr_lr(pet, fwhm=5)

# %% [markdown]
# ## Visualize result
#
# We inspect the middle time frame without and with HYPR-LR denoising.

# %%
from nilearn.image import index_img
from nilearn.plotting import plot_anat


# get mid slice index (slice 16)
slice_index = pet.num_frames // 2

# pick common colorbar limits
vmin = 0
vmax = 1.2e5

plot_anat(
index_img(pet.img, slice_index),
title="4-D PET image",
colorbar=True,
draw_cross=False,
vmin=vmin,
vmax=vmax,
)

# plot HYPR-LR denoised image
plot_anat(
index_img(pet_hyprlr.img, slice_index),
title="HYPR-LR denoised PET image",
colorbar=True,
draw_cross=False,
vmin=vmin,
vmax=vmax,
);

# %% [markdown]
# We can also look at the time activity curve for a single voxel.

# %%
import matplotlib.pyplot as plt


voxel_index = (100, 100, 100) # an arbitrarily selected voxel

time = pet.frame_mid
pet_tac = pet.dataobj[*voxel_index, ...]
pet_hyprlr_tac = pet_hyprlr.dataobj[*voxel_index, ...]

plt.figure()
plt.plot(time, pet_tac, label="Without denoising")
plt.plot(time, pet_hyprlr_tac, label="HYPR-LR")
plt.xlabel("Time (minutes)")
plt.ylabel(f'Radioactivity ({pet.json_dict["Units"]})')
plt.title("Time activity curve (TAC) for a single voxel")
plt.legend();

# %% [markdown]
# ## Command line interface
#
# Instead of using the Python API, we can also perform denoising via the
# command line:

# %%
# !denoise --method HYPRLR --fwhm 5 --outputdir nb_data nb_data/pet_to_denoise.nii.gz
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a6fc62

Please sign in to comment.