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

PDF report for 4D niimage #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Empty file.
45 changes: 45 additions & 0 deletions nilearn_sandbox/plotting/pdf_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from nilearn.plotting import plot_stat_map, plot_prob_atlas
from nilearn._utils import check_niimg_4d
from nilearn.image import index_img


def plot_to_pdf(img, path='output.pdf', vmax=None):
"""Creates a pdf from a 4D nii-image like

Parameters
----------
img: nii-like image,
Image to dump as pdf

path: str,
Path of the output pdf

vmax: float or 'auto' or None,
vmax to use in plot_stat_map. 'auto' will compute it magically
"""
a4_size = (8.27, 11.69)
img = check_niimg_4d(img)
n_components = img.shape[3]

if vmax == 'auto':
vmax = np.max(np.abs(img.get_data()), axis=3)
vmax[vmax >= 0.1] = 0
vmax = np.max(vmax)
elif vmax is not None:
vmax = float(vmax)
with PdfPages(path) as pdf:
for i in range(-1, n_components, 5):
fig, axes = plt.subplots(5, 1, figsize=a4_size, squeeze=False)
axes = axes.reshape(-1)
for j, ax in enumerate(axes):
if i + j < 0:
plot_prob_atlas(img, axes=ax)
elif j + i < n_components:
plot_stat_map(index_img(img, j + i), axes=ax, vmax=vmax)
else:
ax.axis('off')
pdf.savefig(fig)
plt.close()
Empty file.
17 changes: 17 additions & 0 deletions nilearn_sandbox/plotting/tests/test_pdf_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from nose.tools import assert_true
from nilearn.datasets import fetch_atlas_smith_2009
from nilearn_sandbox.plotting.pdf_plotting import plot_to_pdf
from tempfile import mkdtemp
from os.path import join

def test_plot_to_pdf():
# Smoke test pdf plotting
smith = fetch_atlas_smith_2009()
img = smith.rsn10
temp_dir = mkdtemp()
file_path = join(temp_dir, 'output.pdf')
plot_to_pdf(img, path=file_path)
assert_true(os.path.exists(file_path))
os.remove('output.pdf')
os.rmdir(temp_dir)