From 08d8b4686f4ed94788fa1d72eb3eea6550780720 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Tue, 4 Dec 2018 09:29:15 +0000
Subject: [PATCH] TEST: Test utils.guessType

---
 tests/__init__.py            |  50 ++++++++++++++++
 tests/test_fsl_data_utils.py | 107 +++++++++++++++++++++++++++++++++++
 2 files changed, 157 insertions(+)
 create mode 100644 tests/test_fsl_data_utils.py

diff --git a/tests/__init__.py b/tests/__init__.py
index 06bd6b5b7..2ed387e0b 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -358,6 +358,56 @@ def make_mock_feat_analysis(featdir,
     return featdir
 
 
+def make_mock_melodic_analysis(basedir, shape4D, ntimepoints, xform=None):
+
+    if xform is None:
+        xform = np.eye(4)
+
+    ncomps = shape4D[-1]
+    halftime = int(np.floor(ntimepoints / 2))
+
+    os.makedirs(basedir)
+
+    make_random_image(op.join(basedir, 'melodic_IC.nii.gz'),
+                      dims=shape4D,
+                      xform=xform)
+
+    mix   = np.random.randint(1, 255, (ntimepoints, ncomps))
+    ftmix = np.random.randint(1, 255, (halftime,    ncomps))
+
+    np.savetxt(op.join(basedir, 'melodic_mix'),   mix)
+    np.savetxt(op.join(basedir, 'melodic_FTmix'), ftmix)
+
+
+def make_mock_dtifit_analysis(basedir, shape3D, basename='dti', xform=None, tensor=False):
+
+    if xform is None:
+        xform = np.eye(4)
+
+    os.makedirs(basedir)
+
+    shape4D = tuple(shape3D) + (3,)
+
+    def mk(ident, shp):
+        make_random_image(
+            op.join(basedir, '{}_{}.nii.gz'.format(basename, ident)),
+            shp,
+            xform)
+
+    mk('V1', shape4D)
+    mk('V2', shape4D)
+    mk('V3', shape4D)
+    mk('L1', shape3D)
+    mk('L2', shape3D)
+    mk('L3', shape3D)
+    mk('S0', shape3D)
+    mk('MD', shape3D)
+    mk('MO', shape3D)
+    mk('FA', shape3D)
+
+    if tensor:
+        mk('tensor', tuple(shape3D) + (6,))
+
 
 def make_random_mask(filename, shape, xform, premask=None, minones=1):
     """Make a random binary mask image. """
diff --git a/tests/test_fsl_data_utils.py b/tests/test_fsl_data_utils.py
new file mode 100644
index 000000000..3fd02a8f9
--- /dev/null
+++ b/tests/test_fsl_data_utils.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+#
+# test_fsl_data_utils.py -
+#
+# Author: Paul McCarthy <pauldmccarthy@gmail.com>
+#
+
+import            shutil
+import            os
+import os.path as op
+
+import fsl.utils.tempdir        as tempdir
+import fsl.data.utils           as dutils
+
+import fsl.utils.path           as fslpath
+import fsl.data.image           as fslimage
+import fsl.data.vtk             as fslvtk
+import fsl.data.gifti           as fslgifti
+import fsl.data.freesurfer      as fslfs
+import fsl.data.mghimage        as fslmgh
+import fsl.data.featimage       as featimage
+import fsl.data.melodicimage    as melimage
+import fsl.data.dtifit          as dtifit
+import fsl.data.melodicanalysis as melanalysis
+import fsl.data.featanalysis    as featanalysis
+
+from . import (touch,
+               make_mock_feat_analysis,
+               make_mock_melodic_analysis,
+               make_mock_dtifit_analysis)
+
+
+def test_guessType():
+
+    def asrt(path, cls):
+        restype, respath = dutils.guessType(path)
+
+        assert restype is cls
+
+        if path.startswith('fsleyes://'):
+            path = path[10:]
+
+        # image path might not have an extension
+
+        try:
+            path = fslimage.addExt(path, mustExist=True)
+        except fslimage.PathError:
+            pass
+
+        assert respath == op.abspath(path)
+
+    with tempdir.tempdir() as td:
+
+        touch('foo.nii')
+        asrt('foo',               fslimage.Image)
+        asrt('foo.nii',           fslimage.Image)
+        asrt('fsleyes://foo',     fslimage.Image)
+        asrt('fsleyes://foo.nii', fslimage.Image)
+        os.remove('foo.nii')
+
+
+        touch('foo.vtk')
+        asrt('foo.vtk', fslvtk.VTKMesh)
+        os.remove('foo.vtk')
+
+        touch('foo.surf.gii')
+        asrt('foo.surf.gii', fslgifti.GiftiMesh)
+        os.remove('foo.surf.gii')
+
+        touch('lh.pial')
+        asrt('lh.pial', fslfs.FreesurferMesh)
+        os.remove('lh.pial')
+
+        touch('foo.mgz')
+        asrt('foo.mgz', fslmgh.MGHImage)
+        os.remove('foo.mgz')
+
+        make_mock_melodic_analysis('filtered_func_data.ica',
+                                   (10, 10, 10, 10),
+                                   20)
+        asrt('filtered_func_data.ica/melodic_IC',        melimage.MelodicImage)
+        asrt('filtered_func_data.ica/melodic_IC.nii.gz', melimage.MelodicImage)
+        asrt('filtered_func_data.ica',                   melimage.MelodicImage)
+        asrt('filtered_func_data.ica/',                  melimage.MelodicImage)
+        shutil.rmtree('filtered_func_data.ica')
+
+        featdir = op.join(op.dirname(__file__),
+                          'testdata', 'test_feat', '1stlevel_1.feat')
+        make_mock_feat_analysis(featdir,
+                                td,
+                                (10, 10, 10, 10))
+        asrt('1stlevel_1.feat/filtered_func_data',        featimage.FEATImage)
+        asrt('1stlevel_1.feat/filtered_func_data.nii.gz', featimage.FEATImage)
+        asrt('1stlevel_1.feat',                           featimage.FEATImage)
+
+        make_mock_dtifit_analysis('dti', (10, 10, 10))
+        asrt('dti', dtifit.DTIFitTensor)
+        shutil.rmtree('dti')
+
+        asrt('noexist', None)
+
+        touch('norecognise')
+        asrt('norecognise', None)
+        touch('norecognise.txt')
+        asrt('norecognise.txt', None)
+        os.remove('norecognise')
+        os.remove('norecognise.txt')
-- 
GitLab