Created
November 18, 2014 08:51
-
-
Save matthew-brett/667dd9e7516aaa7109cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Test we can correctly import example PARREC files | |
""" | |
from __future__ import print_function, absolute_import | |
from glob import glob | |
from os.path import join as pjoin, basename, splitext, exists, dirname | |
import numpy as np | |
from nibabel import load as top_load, Nifti1Image | |
from nibabel.processing import resample_to_output | |
from nibabel.tests.nibabel_data import get_nibabel_data, needs_nibabel_data | |
def to_real_3d(img): | |
if len(img.shape) == 4: | |
img = Nifti1Image(img.get_data()[..., 0], img.affine, img.header) | |
return resample_to_output(img) | |
def view_pair(pair): | |
img0, img1 = pair | |
viewer0 = img0.plot() | |
viewer1 = img1.plot() | |
viewer0.link_to(viewer1) | |
viewer0.show() | |
NIBABEL_DATA = get_nibabel_data() | |
ROSETTA = pjoin(NIBABEL_DATA, 'rosetta-samples', 'philips') | |
def get_pairs(): | |
pairs = [] | |
for par in glob(pjoin(ROSETTA, '*', '*', 'PARREC', '*.PAR')): | |
par_root, ext = splitext(basename(par)) | |
# Check we can load the image | |
pimg = top_load(par) | |
# Compare against NIFTI if present | |
nifti_glob = pjoin(dirname(par), '..', 'NIFTI', '4D', '*.nii') | |
niftis = glob(nifti_glob) | |
if len(niftis) == 0: | |
raise ValueError("Missing nifti for " + par) | |
if len(niftis) > 1: | |
raise ValueError("Too many niftis for " + par) | |
nifti_fname = niftis.pop() | |
nimg = top_load(nifti_fname) | |
pairs.append((to_real_3d(pimg), to_real_3d(nimg))) | |
return pairs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment