From 1cdaba81e7a23c27f8a297e892d2bafff67dacbf Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Thu, 30 Jul 2020 12:42:19 +0100
Subject: [PATCH] ENH,RF: Moved avwutils unit tests into subdir, added
 fslchpixdim test

---
 unit_tests/avwutils/fslchpixdim/feedsRun      | 58 +++++++++++++++++++
 .../{ => avwutils}/fslcreatehd/feedsRun       |  0
 2 files changed, 58 insertions(+)
 create mode 100755 unit_tests/avwutils/fslchpixdim/feedsRun
 rename unit_tests/{ => avwutils}/fslcreatehd/feedsRun (100%)

diff --git a/unit_tests/avwutils/fslchpixdim/feedsRun b/unit_tests/avwutils/fslchpixdim/feedsRun
new file mode 100755
index 0000000..56bcee4
--- /dev/null
+++ b/unit_tests/avwutils/fslchpixdim/feedsRun
@@ -0,0 +1,58 @@
+#!/usr/bin/env fslpython
+
+import os
+import sys
+import subprocess as sp
+
+import numpy   as np
+import nibabel as nib
+
+
+def create_image(shape, pixdim):
+    pixdim = list(pixdim)
+    data   = np.random.random(shape).astype(np.float32)
+    hdr    = nib.Nifti1Header()
+
+    hdr.set_data_dtype(np.float32)
+    hdr.set_data_shape(shape)
+    hdr.set_zooms(pixdim[:len(shape)])
+
+    return nib.Nifti1Image(data, np.eye(4), hdr)
+
+
+def check_image(origimg, changedimg, exppixdim):
+
+    gotshape  = changedimg.shape
+    gotpixdim = list(changedimg.header.get_zooms())
+    gotdata   = changedimg.get_fdata()
+
+    assert origimg.shape == gotshape
+    assert gotpixdim     == exppixdim
+    assert np.all(origimg.get_fdata() == gotdata)
+
+
+def run_tests():
+    # (image shape, command, exppixdims)
+    tests = [
+        ((5, 5, 5),    '2 2 2',   (2, 2, 2)),
+        ((5, 5, 5),    '2 2 2 2', (2, 2, 2)),
+        ((5, 5, 5, 5), '2 2 2',   (2, 2, 2, 1)),
+        ((5, 5, 5, 5), '2 2 2 2', (2, 2, 2, 2)),
+    ]
+
+    for shape, cmd, exppixdim in tests:
+
+        imgfile = 'image.nii.gz'
+        img     = create_image(shape, [1] * len(shape))
+        img.to_filename(imgfile)
+
+        try:
+            sp.run(['fslchpixdim', imgfile] + list(cmd.split()))
+            check_image(img, nib.load(imgfile), list(exppixdim))
+
+        finally:
+            os.remove(imgfile)
+
+
+if __name__ == '__main__':
+    sys.exit(run_tests())
diff --git a/unit_tests/fslcreatehd/feedsRun b/unit_tests/avwutils/fslcreatehd/feedsRun
similarity index 100%
rename from unit_tests/fslcreatehd/feedsRun
rename to unit_tests/avwutils/fslcreatehd/feedsRun
-- 
GitLab