Skip to content

Commit 6613522

Browse files
committed
removed duplication, made things more generic
1 parent 2f89242 commit 6613522

File tree

2 files changed

+40
-65
lines changed

2 files changed

+40
-65
lines changed

nibabel/cmdline/diff.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import nibabel as nib
2323
import nibabel.cmdline.utils
2424
import hashlib
25+
import os
2526

2627

2728
def get_opt_parser():
@@ -151,16 +152,7 @@ def main():
151152
print("{:<15}".format('Field'), end="")
152153

153154
for f in files:
154-
output = ""
155-
i = 0
156-
while i < len(f):
157-
if f[i] == "/" or f[i] == "\\":
158-
output = ""
159-
else:
160-
output += f[i]
161-
i += 1
162-
163-
print("{:<55}".format(output), end="")
155+
print("{:<55}".format(os.path.basename(f)), end="")
164156

165157
print()
166158

nibabel/cmdline/tests/test_utils.py

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import numpy as np
1212
from nibabel.cmdline.utils import *
1313
from nibabel.cmdline.diff import get_headers_diff
14-
from os.path import (dirname, join as pjoin, abspath)
14+
from os.path import (join as pjoin)
15+
from nibabel.testing import data_path
16+
from collections import OrderedDict
1517

1618

17-
DATA_PATH = abspath(pjoin(dirname(__file__), '../../tests/data'))
18-
1919

2020
def test_table2string():
2121
assert_equal(table2string([["A", "B", "C", "D"], ["E", "F", "G", "H"]]), "A B C D\nE F G H\n")
@@ -48,57 +48,40 @@ def get_test(self):
4848

4949

5050
def test_get_headers_diff():
51-
fnames = [pjoin(DATA_PATH, f)
51+
fnames = [pjoin(data_path, f)
5252
for f in ('standard.nii.gz', 'example4d.nii.gz')]
5353
actual_difference = get_headers_diff([nib.load(f).header for f in fnames])
54-
expected_difference = {
55-
"regular": ["".encode("utf-8"), "r".encode("utf-8")],
56-
"dim_info": [0, 57],
57-
"dim": np.array([[3, 4, 5, 7, 1, 1, 1, 1], [ 4, 128, 96, 24, 2, 1, 1, 1]], "int16"),
58-
"datatype": [2, 4],
59-
"bitpix": [8, 16],
60-
"pixdim": np.array([[ 1., 1., 3., 2., 1., 1., 1., 1.], [ -1.00000000e+00, 2.00000000e+00,
61-
2.00000000e+00, 2.19999909e+00,
62-
2.00000000e+03, 1.00000000e+00,
63-
1.00000000e+00, 1.00000000e+00]], "float32"),
64-
"slice_end": [0, 23],
65-
"xyzt_units": [0, 10],
66-
"cal_max": [0.0, 1162.0],
67-
"descrip": ["".encode("utf-8"), "FSL3.3\x00 v2.25 NIfTI-1 Single file format".encode("utf-8")],
68-
"qform_code": [0, 1],
69-
"sform_code": [2, 1],
70-
"quatern_b": [0.0, -1.9451068140294884e-26],
71-
"quatern_c": [0.0, -0.9967085123062134],
72-
"quatern_d": [0.0, -0.0810687392950058],
73-
"qoffset_x": [0.0, 117.8551025390625],
74-
"qoffset_y": [0.0, -35.72294235229492],
75-
"qoffset_z": [0.0, -7.248798370361328],
76-
"srow_x": np.array([[ 1., 0., 0., 0.], [ -2.00000000e+00, 6.71471565e-19, 9.08102451e-18,
77-
1.17855103e+02]], "float32"),
78-
"srow_y": np.array([[ 0., 3., 0., 0.], [ -6.71471565e-19, 1.97371149e+00, -3.55528235e-01,
79-
-3.57229424e+01]], "float32"),
80-
"srow_z": np.array([[ 0., 0., 2., 0.], [ 8.25548089e-18, 3.23207617e-01, 2.17108178e+00,
81-
-7.24879837e+00]], "float32")
82-
}
83-
84-
assert_equal(actual_difference["regular"], expected_difference["regular"])
85-
assert_equal(actual_difference["dim_info"], expected_difference["dim_info"])
86-
np.testing.assert_array_equal(actual_difference["dim"], expected_difference["dim"])
87-
assert_equal(actual_difference["datatype"], expected_difference["datatype"])
88-
assert_equal(actual_difference["bitpix"], expected_difference["bitpix"])
89-
np.testing.assert_array_equal(actual_difference["pixdim"], expected_difference["pixdim"])
90-
assert_equal(actual_difference["slice_end"], expected_difference["slice_end"])
91-
assert_equal(actual_difference["xyzt_units"], expected_difference["xyzt_units"])
92-
assert_equal(actual_difference["cal_max"], expected_difference["cal_max"])
93-
assert_equal(actual_difference["descrip"], expected_difference["descrip"])
94-
assert_equal(actual_difference["qform_code"], expected_difference["qform_code"])
95-
assert_equal(actual_difference["sform_code"], expected_difference["sform_code"])
96-
assert_equal(actual_difference["quatern_b"], expected_difference["quatern_b"])
97-
assert_equal(actual_difference["quatern_c"], expected_difference["quatern_c"])
98-
assert_equal(actual_difference["quatern_d"], expected_difference["quatern_d"])
99-
assert_equal(actual_difference["qoffset_x"], expected_difference["qoffset_x"])
100-
assert_equal(actual_difference["qoffset_y"], expected_difference["qoffset_y"])
101-
assert_equal(actual_difference["qoffset_z"], expected_difference["qoffset_z"])
102-
np.testing.assert_array_equal(actual_difference["srow_x"], expected_difference["srow_x"])
103-
np.testing.assert_array_equal(actual_difference["srow_y"], expected_difference["srow_y"])
104-
np.testing.assert_array_equal(actual_difference["srow_z"], expected_difference["srow_z"])
54+
expected_difference = OrderedDict([
55+
("regular", [np.asarray("".encode("utf-8")), np.asarray("r".encode("utf-8"))]),
56+
("dim_info", [np.asarray(0).astype(dtype="uint8"), np.asarray(57).astype(dtype="uint8")]),
57+
("dim", [np.array([3, 4, 5, 7, 1, 1, 1, 1]).astype(dtype="int16"),
58+
np.array([ 4, 128, 96, 24, 2, 1, 1, 1]).astype(dtype="int16")]),
59+
("datatype", [np.array(2).astype(dtype="uint8"), np.array(4).astype(dtype="uint8")]),
60+
("bitpix", [np.array(8).astype(dtype="uint8"), np.array(16).astype(dtype="uint8")]),
61+
("pixdim", [np.array([ 1., 1., 3., 2., 1., 1., 1., 1.]).astype(dtype="float32"), np.array(
62+
[ -1.00000000e+00, 2.00000000e+00, 2.00000000e+00, 2.19999909e+00, 2.00000000e+03, 1.00000000e+00,
63+
1.00000000e+00, 1.00000000e+00]).astype(dtype="float32")]),
64+
("slice_end", [np.array(0).astype(dtype="uint8"), np.array(23).astype(dtype="uint8")]),
65+
("xyzt_units", [np.array(0).astype(dtype="uint8"), np.array(10).astype(dtype="uint8")]),
66+
("cal_max", [np.array(0.0).astype(dtype="float32"), np.asarray(1162.0).astype(dtype="float32")]),
67+
("descrip", [np.array("".encode("utf-8")).astype(dtype="S80"),
68+
np.array("FSL3.3\x00 v2.25 NIfTI-1 Single file format".encode("utf-8")).astype(dtype="S80")]),
69+
("qform_code", [np.array(0).astype(dtype="int16"), np.array(1).astype(dtype="int16")]),
70+
("sform_code", [np.array(2).astype(dtype="int16"), np.array(1).astype(dtype="int16")]),
71+
("quatern_b", [np.array(0.0).astype(dtype="float32"),
72+
np.array(-1.9451068140294884e-26).astype(dtype="float32")]),
73+
("quatern_c", [np.array(0.0).astype(dtype="float32"), np.array(-0.9967085123062134).astype(dtype="float32")]),
74+
("quatern_d", [np.array(0.0).astype(dtype="float32"), np.array(-0.0810687392950058).astype(dtype="float32")]),
75+
("qoffset_x", [np.array(0.0).astype(dtype="float32"), np.array(117.8551025390625).astype(dtype="float32")]),
76+
("qoffset_y", [np.array(0.0).astype(dtype="float32"), np.array(-35.72294235229492).astype(dtype="float32")]),
77+
("qoffset_z", [np.array(0.0).astype(dtype="float32"), np.array(-7.248798370361328).astype(dtype="float32")]),
78+
("srow_x", [np.array([ 1., 0., 0., 0.]).astype(dtype="float32"),
79+
np.array([ -2.00000000e+00, 6.71471565e-19, 9.08102451e-18,
80+
1.17855103e+02]).astype(dtype="float32")]),
81+
("srow_y", [np.array([ 0., 3., 0., 0.]).astype(dtype="float32"),
82+
np.array([ -6.71471565e-19, 1.97371149e+00, -3.55528235e-01, -3.57229424e+01]).astype(dtype="float32")]),
83+
("srow_z", [np.array([ 0., 0., 2., 0.]).astype(dtype="float32"),
84+
np.array([ 8.25548089e-18, 3.23207617e-01, 2.17108178e+00,
85+
-7.24879837e+00]).astype(dtype="float32")])])
86+
87+
np.testing.assert_equal(actual_difference, expected_difference)

0 commit comments

Comments
 (0)