Skip to content

Commit 3c4d7b2

Browse files
committed
Merge pull request #82 from schwarty/master
concat_images can use filenames or images
2 parents 1046c11 + 426f728 commit 3c4d7b2

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

nibabel/funcs.py

Lines changed: 9 additions & 1 deletion
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
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
@@ -11,6 +12,7 @@
1112

1213
from .orientations import (io_orientation, orientation_affine, flip_axis,
1314
apply_orientation, OrientationError)
15+
from .loadsave import load
1416

1517

1618
def squeeze_image(img):
@@ -92,7 +94,7 @@ def concat_images(images, check_affines=True):
9294
Parameters
9395
----------
9496
images : sequence
95-
sequence of ``SpatialImage``\s
97+
sequence of ``SpatialImage`` or of filenames\s
9698
check_affines : {True, False}, optional
9799
If True, then check that all the affines for `images` are nearly
98100
the same, raising a ``ValueError`` otherwise. Default is True
@@ -105,12 +107,18 @@ def concat_images(images, check_affines=True):
105107
'''
106108
n_imgs = len(images)
107109
img0 = images[0]
110+
is_filename = False
111+
if not hasattr(img0, 'get_data'):
112+
img0 = load(img0)
113+
is_filename = True
108114
i0shape = img0.shape
109115
affine = img0.get_affine()
110116
header = img0.get_header()
111117
out_shape = (n_imgs, ) + i0shape
112118
out_data = np.empty(out_shape)
113119
for i, img in enumerate(images):
120+
if is_filename:
121+
img = load(img)
114122
if check_affines:
115123
if not np.all(img.get_affine() == affine):
116124
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)