def test_getNumComponents():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        icfile = op.join(meldir,  'melodic_IC.nii.gz')

        tests.make_random_image(icfile, (10, 10, 10, 17))

        assert mela.getNumComponents(meldir) == 17

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        with pytest.raises(Exception):
            mela.getNumComponents(meldir)
Exemple #2
0
def test_VoxelwiseEVs():
    with tempdir():
        img = tests.make_random_image('image.nii.gz', (10, 10, 10, 10))

        ev1 = featdesign.VoxelwiseEV(0, 0, 'ev1', 'image.nii.gz')
        ev2 = featdesign.VoxelwiseConfoundEV(0, 0, 'ev2', 'image.nii.gz')

        for xyz in tests.random_voxels((10, 10, 10), 10):
            x, y, z = map(int, xyz)
            exp = img.dataobj[x, y, z, :]
            assert np.all(ev1.image[x, y, z, :] == exp)
            assert np.all(ev2.image[x, y, z, :] == exp)
Exemple #3
0
def test_looksLikeTensorImage():
    # dtifit outputs one of two formats:
    #   - the eigenvectors/values of the tensor matrix
    #   - the raw values of the tensor matrix
    #
    # looksLikeTensorImage tests for the latter

    testcases = [((10, 10, 10), False), ((10, 10, 10, 5), False),
                 ((10, 10, 10, 6), True)]

    with tests.testdir() as testdir:

        fname = op.join(testdir, 'tensor_image.nii')

        for dims, expected in testcases:

            tests.make_random_image(fname, dims=dims)
            img = fslimage.Image(fname)

            assert dtifit.looksLikeTensorImage(img) == expected
            img = None
Exemple #4
0
def test_DTIFitTensor():

    with tests.testdir() as testdir:

        with pytest.raises(Exception):
            dtifit.DTIFitTensor(testdir)

        v1file = op.join(testdir, 'dti_V1.nii')
        v2file = op.join(testdir, 'dti_V2.nii')
        v3file = op.join(testdir, 'dti_V3.nii')
        l1file = op.join(testdir, 'dti_L1.nii')
        l2file = op.join(testdir, 'dti_L2.nii')
        l3file = op.join(testdir, 'dti_L3.nii')

        v1 = tests.make_random_image(v1file, (5, 5, 5, 3)).get_data()
        v2 = tests.make_random_image(v2file, (5, 5, 5, 3)).get_data()
        v3 = tests.make_random_image(v3file, (5, 5, 5, 3)).get_data()
        l1 = tests.make_random_image(l1file, (5, 5, 5))   .get_data()
        l2 = tests.make_random_image(l2file, (5, 5, 5))   .get_data()
        l3 = tests.make_random_image(l3file, (5, 5, 5))   .get_data()

        dtiobj = dtifit.DTIFitTensor(testdir)

        assert np.all(np.isclose(dtiobj.V1()[:], v1))
        assert np.all(np.isclose(dtiobj.V2()[:], v2))
        assert np.all(np.isclose(dtiobj.V3()[:], v3))
        assert np.all(np.isclose(dtiobj.L1()[:], l1))
        assert np.all(np.isclose(dtiobj.L2()[:], l2))
        assert np.all(np.isclose(dtiobj.L3()[:], l3))

        v1 = fslimage.Image(v1file)

        assert np.all(np.isclose(dtiobj.voxToWorldMat, v1.voxToWorldMat))
        assert np.all(np.isclose(dtiobj.shape[:3],     v1.shape[:3]))
        assert np.all(np.isclose(dtiobj.pixdim[:3],    v1.pixdim[:3]))
Exemple #5
0
def test_compressed_voxelwise_ev():

    testcases = [((1, 1, 10, 10), (0, 0, 5)), ((1, 10, 1, 10), (0, 5, 0)),
                 ((10, 1, 1, 10), (5, 0, 0))]

    with tempdir():

        for shape, vox in testcases:
            img = tests.make_random_image('vev.nii.gz', shape)
            vev = featdesign.VoxelwiseEV(0, 0, 'ev1', 'vev.nii.gz')
            x, y, z = vox

            assert np.all(vev.getData(5, 5, 5) == img.dataobj[x, y, z, :])
Exemple #6
0
def test_callfsl():

    with tests.testdir() as testdir:

        fname = op.join(testdir, 'myimage.nii.gz')

        img = tests.make_random_image(fname)
        img = img.get_data()

        # Pass a single string
        cmd = 'fslstats {} -m'.format(fname)

        with mock.patch('fsl.utils.callfsl.sp.check_output',
                        mock_check_output):
            result = callfsl.callFSL(cmd)

            assert np.isclose(float(result), img.mean())

            # Or pass a list of args
            result = callfsl.callFSL(*cmd.split())
            assert np.isclose(float(result), img.mean())

        # Bad commands
        badcmds = ['fslblob', 'fslstats notafile']

        for cmd in badcmds:
            with pytest.raises((OSError, sp.CalledProcessError)):
                callfsl.callFSL(cmd)

        # No FSL - should crash
        cmd = 'fslinfo {}'.format(fname)
        with mock.patch('fsl.utils.callfsl.sp.check_output',
                        mock_check_output):
            callfsl.callFSL(cmd)
        fslplatform.fsldir = None
        with pytest.raises(Exception):
            callfsl.callFSL(cmd)