Skip to content

Commit d9ce63e

Browse files
committed
testing for pathlib
1 parent e1d78c8 commit d9ce63e

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

nibabel/tests/test_image_api.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import warnings
2828
from functools import partial
2929
from itertools import product
30+
import pathlib
3031

3132
import numpy as np
3233

@@ -144,19 +145,21 @@ def validate_filenames(self, imaker, params):
144145
assert_almost_equal(img.get_data(), rt_rt_img.get_data())
145146
# get_ / set_ filename
146147
fname = 'an_image' + self.standard_extension
147-
img.set_filename(fname)
148-
assert_equal(img.get_filename(), fname)
149-
assert_equal(img.file_map['image'].filename, fname)
148+
for path in (fname, pathlib.Path(fname)):
149+
img.set_filename(path)
150+
assert_equal(img.get_filename(), path)
151+
assert_equal(img.file_map['image'].filename, path)
150152
# to_ / from_ filename
151153
fname = 'another_image' + self.standard_extension
152-
with InTemporaryDirectory():
153-
img.to_filename(fname)
154-
rt_img = img.__class__.from_filename(fname)
155-
assert_array_equal(img.shape, rt_img.shape)
156-
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
157-
# get_data will be deprecated
158-
assert_almost_equal(img.get_data(), rt_img.get_data())
159-
del rt_img # to allow windows to delete the directory
154+
for path in (fname, pathlib.Path(fname)):
155+
with InTemporaryDirectory():
156+
img.to_filename(path)
157+
rt_img = img.__class__.from_filename(path)
158+
assert_array_equal(img.shape, rt_img.shape)
159+
assert_almost_equal(img.get_fdata(), rt_img.get_fdata())
160+
# get_data will be deprecated
161+
assert_almost_equal(img.get_data(), rt_img.get_data())
162+
del rt_img # to allow windows to delete the directory
160163

161164
def validate_no_slicing(self, imaker, params):
162165
img = imaker()

nibabel/tests/test_image_load_save.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import shutil
1313
from os.path import dirname, join as pjoin
1414
from tempfile import mkdtemp
15+
import pathlib
1516

1617
import numpy as np
1718

@@ -253,13 +254,14 @@ def test_filename_save():
253254
try:
254255
pth = mkdtemp()
255256
fname = pjoin(pth, 'image' + out_ext)
256-
nils.save(img, fname)
257-
rt_img = nils.load(fname)
258-
assert_array_almost_equal(rt_img.get_data(), data)
259-
assert_true(type(rt_img) is loadklass)
260-
# delete image to allow file close. Otherwise windows
261-
# raises an error when trying to delete the directory
262-
del rt_img
257+
for path in (fname, pathlib.Path(fname)):
258+
nils.save(img, path)
259+
rt_img = nils.load(path)
260+
assert_array_almost_equal(rt_img.get_data(), data)
261+
assert_true(type(rt_img) is loadklass)
262+
# delete image to allow file close. Otherwise windows
263+
# raises an error when trying to delete the directory
264+
del rt_img
263265
finally:
264266
shutil.rmtree(pth)
265267

nibabel/tests/test_loadsave.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from os.path import dirname, join as pjoin
55
import shutil
6+
import pathlib
67

78
import numpy as np
89

@@ -26,13 +27,16 @@
2627

2728

2829
def test_read_img_data():
29-
for fname in ('example4d.nii.gz',
30-
'example_nifti2.nii.gz',
31-
'minc1_1_scale.mnc',
32-
'minc1_4d.mnc',
33-
'test.mgz',
34-
'tiny.mnc'
35-
):
30+
fnames_test = [
31+
'example4d.nii.gz',
32+
'example_nifti2.nii.gz',
33+
'minc1_1_scale.mnc',
34+
'minc1_4d.mnc',
35+
'test.mgz',
36+
'tiny.mnc'
37+
]
38+
fnames_test += [pathlib.Path(p) for p in fnames_test]
39+
for fname in fnames_test:
3640
fpath = pjoin(data_path, fname)
3741
img = load(fpath)
3842
data = img.get_data()

0 commit comments

Comments
 (0)