Skip to content

Commit 426f728

Browse files
author
Yannick Schwartz
committed
The concat_images function can use both SpatialImages and filenames. Added the associated test.
1 parent 2fbabc9 commit 426f728

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

nibabel/funcs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
23
# vi: set ft=python sts=4 ts=4 sw=4 et:
34
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
@@ -93,7 +94,7 @@ def concat_images(images, check_affines=True):
9394
Parameters
9495
----------
9596
images : sequence
96-
sequence of paths to be loaded as ``SpatialImage``\s
97+
sequence of ``SpatialImage`` or of filenames\s
9798
check_affines : {True, False}, optional
9899
If True, then check that all the affines for `images` are nearly
99100
the same, raising a ``ValueError`` otherwise. Default is True
@@ -105,14 +106,19 @@ def concat_images(images, check_affines=True):
105106
dimension
106107
'''
107108
n_imgs = len(images)
108-
img0 = load(images[0])
109+
img0 = images[0]
110+
is_filename = False
111+
if not hasattr(img0, 'get_data'):
112+
img0 = load(img0)
113+
is_filename = True
109114
i0shape = img0.shape
110115
affine = img0.get_affine()
111116
header = img0.get_header()
112117
out_shape = (n_imgs, ) + i0shape
113118
out_data = np.empty(out_shape)
114119
for i, img in enumerate(images):
115-
img = load(img)
120+
if is_filename:
121+
img = load(img)
116122
if check_affines:
117123
if not np.all(img.get_affine() == affine):
118124
raise ValueError('Affines do not match')

nibabel/tests/test_funcs.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
''' Test for image funcs '''
1010

11+
import os
12+
import tempfile
1113
import numpy as np
1214

1315
from ..funcs import concat_images, as_closest_canonical, OrientationError
1416
from ..nifti1 import Nifti1Image
17+
from ..loadsave import save
1518

1619
from numpy.testing import assert_array_equal
1720
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
1821

19-
2022
def test_concat():
2123
shape = (1,2,5)
2224
data0 = np.arange(10).reshape(shape)
@@ -40,6 +42,35 @@ def test_concat():
4042
assert_array_equal(all_imgs.get_affine(), affine)
4143

4244

45+
def test_concat_with_filenames():
46+
shape = (1,2,5)
47+
data0 = np.arange(10).reshape(shape)
48+
affine = np.eye(4)
49+
img0 = Nifti1Image(data0, affine)
50+
img0_path = tempfile.mkstemp(suffix='.nii')[1]
51+
save(img0, img0_path)
52+
data1 = data0 - 10
53+
img1 = Nifti1Image(data1, affine)
54+
img1_path = tempfile.mkstemp(suffix='.nii')[1]
55+
save(img1, img1_path)
56+
all_imgs = concat_images([img0_path, img1_path])
57+
all_data = np.concatenate(
58+
[data0[:,:,:,np.newaxis],data1[:,:,:,np.newaxis]],3)
59+
assert_array_equal(all_imgs.get_data(), all_data)
60+
assert_array_equal(all_imgs.get_affine(), affine)
61+
# check that not-matching affines raise error
62+
img2 = Nifti1Image(data1, affine+1)
63+
assert_raises(ValueError, concat_images, [img0, img2])
64+
img2 = Nifti1Image(data1.T, affine)
65+
assert_raises(ValueError, concat_images, [img0, img2])
66+
# except if check_affines is False
67+
all_imgs = concat_images([img0, img1])
68+
assert_array_equal(all_imgs.get_data(), all_data)
69+
assert_array_equal(all_imgs.get_affine(), affine)
70+
os.remove(img0_path)
71+
os.remove(img1_path)
72+
73+
4374
def test_closest_canonical():
4475
arr = np.arange(24).reshape((2,3,4,1))
4576
# no funky stuff, returns same thing

0 commit comments

Comments
 (0)