From 54579e4c960c42a9a63d5848d4f88ed08cefcf53 Mon Sep 17 00:00:00 2001 From: Arthur Mensch Date: Wed, 9 Sep 2015 11:08:33 +0200 Subject: [PATCH] PDF report for 4D niimage Useful to create a report for 4D map image. Uses plot_prob_atlas and plot_stat_map --- nilearn_sandbox/plotting/__init__.py | 0 nilearn_sandbox/plotting/pdf_plotting.py | 45 +++++++++++++++++++ nilearn_sandbox/plotting/tests/__init__.py | 0 .../plotting/tests/test_pdf_plotting.py | 17 +++++++ 4 files changed, 62 insertions(+) create mode 100644 nilearn_sandbox/plotting/__init__.py create mode 100644 nilearn_sandbox/plotting/pdf_plotting.py create mode 100644 nilearn_sandbox/plotting/tests/__init__.py create mode 100644 nilearn_sandbox/plotting/tests/test_pdf_plotting.py diff --git a/nilearn_sandbox/plotting/__init__.py b/nilearn_sandbox/plotting/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nilearn_sandbox/plotting/pdf_plotting.py b/nilearn_sandbox/plotting/pdf_plotting.py new file mode 100644 index 0000000..d3904d1 --- /dev/null +++ b/nilearn_sandbox/plotting/pdf_plotting.py @@ -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() diff --git a/nilearn_sandbox/plotting/tests/__init__.py b/nilearn_sandbox/plotting/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nilearn_sandbox/plotting/tests/test_pdf_plotting.py b/nilearn_sandbox/plotting/tests/test_pdf_plotting.py new file mode 100644 index 0000000..9744c60 --- /dev/null +++ b/nilearn_sandbox/plotting/tests/test_pdf_plotting.py @@ -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)