diff --git a/fsl/tests/test_wrappers/__init__.py b/fsl/tests/test_wrappers/__init__.py index e146dbf6aaf91c0091e2eb95206fd45a14093421..4dca534cd4278553f118b3ab4d0c7876735e9976 100644 --- a/fsl/tests/test_wrappers/__init__.py +++ b/fsl/tests/test_wrappers/__init__.py @@ -46,3 +46,4 @@ def testenv(*fslexes): fslexes = [op.join(fsldir, 'bin', e) for e in fslexes] if len(fslexes) == 1: yield fslexes[0] else: yield fslexes +testenv.__test__ = False diff --git a/fsl/tests/test_wrappers/test_cluster.py b/fsl/tests/test_wrappers/test_cluster.py new file mode 100644 index 0000000000000000000000000000000000000000..9475a026e82b5e192d147cc2d4ba9a4c1defa8af --- /dev/null +++ b/fsl/tests/test_wrappers/test_cluster.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# +# test_cluster.py - +# +# Author: Paul McCarthy <pauldmccarthy@gmail.com> +# + +import contextlib +import os +import os.path as op +import sys + +import numpy as np + +from fsl.wrappers.cluster import cluster, _cluster + +from . import testenv +from .. import mockFSLDIR + + +@contextlib.contextmanager +def reseed(seed): + state = np.random.get_state() + np.random.seed(seed) + try: + yield + finally: + np.random.set_state(state) + + +def test_cluster_wrapper(): + with testenv('fsl-cluster') as cluster_exe: + result = _cluster('input', 10, mm=True) + expected = [cluster_exe, '-i', 'input', '-t', '10', '--mm'] + expected = ' '.join(expected) + assert result.stdout[0] == expected + + +mock_titles = 'ABCDEFGHIJ' +mock_cluster = f""" +#!{sys.executable} + +import numpy as np + +np.random.seed(12345) +data = np.random.randint(1, 10, (10, 10)) + +print('\t'.join('{mock_titles}')) +for row in data: + print('\t'.join([str(val) for val in row])) +""".strip() + + +def test_cluster(): + with mockFSLDIR() as fsldir: + cluster_exe = op.join(fsldir, 'bin', 'fsl-cluster') + with open(cluster_exe, 'wt') as f: + f.write(mock_cluster) + os.chmod(cluster_exe, 0o755) + + data, titles, result1 = cluster('input', 3.5) + result2 = cluster('input', 3.5, load=False) + + with reseed(12345): + expected = np.random.randint(1, 10, (10, 10)) + + assert np.all(np.isclose(data, expected)) + assert ''.join(titles) == mock_titles + assert result1.stdout == result2.stdout