Exemplo n.º 1
0
def test_calling_shapes():
    cs2d = CS("ij")
    cs1d = CS("i")
    cm2d = CoordinateMap(cs2d, cs2d, lambda x: x + 1)
    cm1d2d = CoordinateMap(cs1d, cs2d, lambda x: np.concatenate((x, x), axis=-1))
    at2d = AffineTransform(cs2d, cs2d, np.array([[1, 0, 1], [0, 1, 1], [0, 0, 1]]))
    at1d2d = AffineTransform(cs1d, cs2d, np.array([[1, 0], [0, 1], [0, 1]]))
    # test coordinate maps and affine transforms
    for xfm2d, xfm1d2d in ((cm2d, cm1d2d), (at2d, at1d2d)):
        arr = np.array([0, 1])
        yield assert_array_equal(xfm2d(arr), [1, 2])
        # test lists work too
        res = xfm2d([0, 1])
        yield assert_array_equal(res, [1, 2])
        # and return arrays (by checking shape attribute)
        yield assert_equal(res.shape, (2,))
        # maintaining input shape
        arr_long = arr[None, None, :]
        yield assert_array_equal(xfm2d(arr_long), arr_long + 1)
        # wrong shape array raises error
        yield assert_raises(CoordinateSystemError, xfm2d, np.zeros((3,)))
        yield assert_raises(CoordinateSystemError, xfm2d, np.zeros((3, 3)))
        # 1d to 2d
        arr = np.array(1)
        yield assert_array_equal(xfm1d2d(arr), [1, 1])
        arr_long = arr[None, None, None]
        yield assert_array_equal(xfm1d2d(arr_long), np.ones((1, 1, 2)))
        # wrong shape array raises error.  Note 1d input requires size 1
        # as final axis
        yield assert_raises(CoordinateSystemError, xfm1d2d, np.zeros((3,)))
        yield assert_raises(CoordinateSystemError, xfm1d2d, np.zeros((3, 2)))
Exemplo n.º 2
0
def test_as_image():
    # test image creation / pass through function
    img = as_image(funcfile)  # string filename
    img1 = as_image(six.text_type(funcfile))  # unicode
    img2 = as_image(img)
    assert_equal(img.affine, img1.affine)
    assert_array_equal(img.get_data(), img1.get_data())
    assert_true(img is img2)
Exemplo n.º 3
0
def test_as_image():
    # test image creation / pass through function
    img = as_image(funcfile)  # string filename
    img1 = as_image(six.text_type(funcfile))  # unicode
    img2 = as_image(img)
    assert_equal(img.affine, img1.affine)
    assert_array_equal(img.get_data(), img1.get_data())
    assert_true(img is img2)
Exemplo n.º 4
0
def test_drop_io_dim():
    aff = np.diag([1,2,3,1])
    in_dims = list('ijk')
    out_dims = list('xyz')
    cm = Affine.from_params(in_dims, out_dims, aff)
    yield assert_raises(ValueError, drop_io_dim, cm, 'q')
    for i, d in enumerate(in_dims):
        cm2 = drop_io_dim(cm, d)
        yield assert_equal(cm2.ndim, (2,2))
        ods = out_dims[:]
        ids = in_dims[:]
        ods.pop(i)
        ids.pop(i)
        yield assert_equal(ods, cm2.output_coords.coord_names)
        yield assert_equal(ids, cm2.input_coords.coord_names)
        a2 = cm2.affine
        yield assert_equal(a2.shape, (3,3))
        ind_a = np.ones((4,4), dtype=np.bool)
        ind_a[:,i] = False
        ind_a[i,:] = False
        aff2 = cm.affine[ind_a].reshape((3,3))
        yield assert_array_equal(aff2, a2)
        # Check non-orth case
        waff = np.diag([1,2,3,1])
        wrong_i = (i+1) % 3
        waff[wrong_i,i] = 2 # not OK if in same column as that being removed
        wcm = Affine.from_params(in_dims, out_dims, waff)
        yield assert_raises(ValueError, drop_io_dim, wcm, d)
        raff = np.diag([1,2,3,1])
        raff[i,wrong_i] = 2 # is OK if in same row
        rcm = Affine.from_params(in_dims, out_dims, raff)
        cm2 = drop_io_dim(rcm, d)
    # dims out of range give error
    yield assert_raises(ValueError, drop_io_dim, cm, 'p')
    # only works for affine case
    cm = CoordinateMap(lambda x:x, cm.input_coords, cm.output_coords)
    yield assert_raises(AttributeError, drop_io_dim, cm, 'i')
    # Check non-square case
    aff = np.array([[1.,0,0,0],
                    [0,2,0,0],
                    [0,0,3,0],
                    [0,0,0,1],
                    [0,0,0,1]])
    cm = Affine.from_params(in_dims, out_dims + ['t'], aff)
    cm2 = drop_io_dim(cm, 'i')
    yield assert_array_equal(cm2.affine,
                             [[2,0,0],
                              [0,3,0],
                              [0,0,1],
                              [0,0,1]])
    yield assert_raises(ValueError, drop_io_dim, cm, 't')
Exemplo n.º 5
0
def test_as_image():
    # test image creation / pass through function
    img = as_image(funcfile) # string filename
    img1 = as_image(unicode(funcfile))
    img2 = as_image(img)
    yield assert_equal(img.affine, img1.affine)
    yield assert_array_equal(np.asarray(img), np.asarray(img1))
    yield assert_true(img is img2)
Exemplo n.º 6
0
def test_mask_files():
    with InTemporaryDirectory():
        # Make a 4D file from the anatomical example
        img = nib.load(anatfile)
        arr = img.get_data()
        a2 = np.zeros(arr.shape + (2, ))
        a2[:, :, :, 0] = arr
        a2[:, :, :, 1] = arr
        img = nib.Nifti1Image(a2, np.eye(4))
        a_fname = 'fourd_anat.nii'
        nib.save(img, a_fname)
        # check 4D mask
        msk1, mean1 = nnm.compute_mask_files(a_fname, return_mean=True)
        # and mask from identical list of 3D files
        msk2, mean2 = nnm.compute_mask_files([anatfile, anatfile],
                                             return_mean=True)
        assert_array_equal(msk1, msk2)
        assert_array_equal(mean1, mean2)
Exemplo n.º 7
0
def test_mask_files():
    with InTemporaryDirectory():
        # Make a 4D file from the anatomical example
        img = nib.load(anatfile)
        arr = img.get_data()
        a2 = np.zeros(arr.shape + (2, ))
        a2[:, :, :, 0] = arr
        a2[:, :, :, 1] = arr
        img = nib.Nifti1Image(a2, np.eye(4))
        a_fname = 'fourd_anat.nii'
        nib.save(img, a_fname)
        # check 4D mask
        msk1, mean1 = nnm.compute_mask_files(a_fname, return_mean=True)
        # and mask from identical list of 3D files
        msk2, mean2 = nnm.compute_mask_files([anatfile, anatfile],
                                             return_mean=True)
        assert_array_equal(msk1, msk2)
        assert_array_equal(mean1, mean2)
Exemplo n.º 8
0
def test_pproba():
    test = 5 * np.random.rand(10)
    order = np.argsort(-test)
    learn = np.random.rand(100)
    learn[:20] += 3
    #
    pval = _stat_to_proba(test)
    # check that pvals are between 0 and 1, and that its is monotonous
    assert_true((pval >= 0).all())
    assert_true((pval <= 1).all())
    assert_array_equal(pval[order], np.sort(pval))
    #
    pval = _stat_to_proba(test, learn)
    assert_true((pval >= 0).all())
    assert_true((pval <= 1).all())
    assert_array_equal(pval[order], np.sort(pval))
    #
    for method in ['gauss_mixture', 'emp_null', 'gam_gauss']:
        pval = _stat_to_proba(test, learn, method=method)
        assert_true((pval >= 0).all())
        assert_true((pval <= 1).all())
Exemplo n.º 9
0
def test_pproba():
    test = 5 * np.random.rand(10)
    order = np.argsort(-test)
    learn = np.random.rand(100)
    learn[:20] += 3
    # 
    pval = _stat_to_proba(test)
    # check that pvals are between 0 and 1, and that its is monotonous
    assert_true((pval >= 0).all())
    assert_true((pval <= 1).all())
    assert_array_equal(pval[order], np.sort(pval))
    #
    pval = _stat_to_proba(test, learn)
    assert_true((pval >= 0).all())
    assert_true((pval <= 1).all())
    assert_array_equal(pval[order], np.sort(pval))
    #
    for method in ['gauss_mixture', 'emp_null', 'gam_gauss']:
        pval = _stat_to_proba(test, learn, method=method)
        assert_true((pval >= 0).all())
        assert_true((pval <= 1).all())
Exemplo n.º 10
0
def test_drop_io_dim():
    # test ordinary case of 4d to 3d
    cm4d = AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1]))
    cm3d = drop_io_dim(cm4d, 't')
    yield assert_array_equal(cm3d.affine, np.diag([1, 2, 3, 1]))
    # 3d to 2d
    cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1,2,3,1]))
    cm2d = drop_io_dim(cm3d, 'z')
    yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
    # test zero scaling for dropped dimension
    cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1, 2, 0, 1]))
    cm2d = drop_io_dim(cm3d, 'z')
    yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
    # test not diagonal but orthogonal
    aff = np.array([[1, 0, 0, 0],
                    [0, 0, 2, 0],
                    [0, 3, 0, 0],
                    [0, 0, 0, 1]])
    cm3d = AffineTransform.from_params('ijk', 'xyz', aff)
    cm2d = drop_io_dim(cm3d, 'z')
    yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
    cm2d = drop_io_dim(cm3d, 'k')
    yield assert_array_equal(cm2d.affine, np.diag([1, 3, 1]))
    # and with zeros scaling for orthogonal dropped dimension
    aff[2] = 0
    cm3d = AffineTransform.from_params('ijk', 'xyz', aff)
    cm2d = drop_io_dim(cm3d, 'z')
    yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
Exemplo n.º 11
0
def test_compute_mask_sessions():
    """Test that the mask computes well on multiple sessions
    """
    with InTemporaryDirectory():
        # Make a 4D file from the anatomical example
        img = nib.load(anatfile)
        arr = img.get_data()
        a2 = np.zeros(arr.shape + (2, ))
        a2[:, :, :, 0] = arr
        a2[:, :, :, 1] = arr
        img = nib.Nifti1Image(a2, np.eye(4))
        a_fname = 'fourd_anat.nii'
        nib.save(img, a_fname)
        a3 = a2.copy()
        a3[:10, :10, :10] = 0
        img2 = nib.Nifti1Image(a3, np.eye(4))
        # check 4D mask
        msk1 = nnm.compute_mask_sessions([img2, img2])
        msk2 = nnm.compute_mask_sessions([img2, a_fname])
        assert_array_equal(msk1, msk2)
        msk3 = nnm.compute_mask_sessions([img2, a_fname], threshold=.9)
        msk4 = nnm.compute_mask_sessions([img2, a_fname], threshold=0)
        msk5 = nnm.compute_mask_sessions([a_fname, a_fname])
        assert_array_equal(msk1, msk3)
        assert_array_equal(msk4, msk5)
Exemplo n.º 12
0
def test_compute_mask_sessions():
    """Test that the mask computes well on multiple sessions
    """
    with InTemporaryDirectory():
        # Make a 4D file from the anatomical example
        img = nib.load(anatfile)
        arr = img.get_data()
        a2 = np.zeros(arr.shape + (2, ))
        a2[:, :, :, 0] = arr
        a2[:, :, :, 1] = arr
        img = nib.Nifti1Image(a2, np.eye(4))
        a_fname = 'fourd_anat.nii'
        nib.save(img, a_fname)
        a3 = a2.copy()
        a3[:10, :10, :10] = 0
        img2 = nib.Nifti1Image(a3, np.eye(4))
        # check 4D mask
        msk1 = nnm.compute_mask_sessions([img2, img2])
        msk2 = nnm.compute_mask_sessions([img2, a_fname])
        assert_array_equal(msk1, msk2)
        msk3 = nnm.compute_mask_sessions([img2, a_fname], threshold=.9)
        msk4 = nnm.compute_mask_sessions([img2, a_fname], threshold=0)
        msk5 = nnm.compute_mask_sessions([a_fname, a_fname])
        assert_array_equal(msk1, msk3)
        assert_array_equal(msk4, msk5)
Exemplo n.º 13
0
def test_append_io_dim():
    aff = np.diag([1, 2, 3, 1])
    in_dims = list("ijk")
    out_dims = list("xyz")
    cm = AffineTransform.from_params(in_dims, out_dims, aff)
    cm2 = append_io_dim(cm, "l", "t")
    yield assert_array_equal(cm2.affine, np.diag([1, 2, 3, 1, 1]))
    yield assert_equal(cm2.function_range.coord_names, out_dims + ["t"])
    yield assert_equal(cm2.function_domain.coord_names, in_dims + ["l"])
    cm2 = append_io_dim(cm, "l", "t", 9, 5)
    a2 = np.diag([1, 2, 3, 5, 1])
    a2[3, 4] = 9
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.function_range.coord_names, out_dims + ["t"])
    yield assert_equal(cm2.function_domain.coord_names, in_dims + ["l"])
    # non square case
    aff = np.array([[2, 0, 0], [0, 3, 0], [0, 0, 1], [0, 0, 1]])
    cm = AffineTransform.from_params("ij", "xyz", aff)
    cm2 = append_io_dim(cm, "q", "t", 9, 5)
    a2 = np.array([[2, 0, 0, 0], [0, 3, 0, 0], [0, 0, 0, 1], [0, 0, 5, 9], [0, 0, 0, 1]])
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.function_range.coord_names, list("xyzt"))
    yield assert_equal(cm2.function_domain.coord_names, list("ijq"))
Exemplo n.º 14
0
def test_append_io_dim():
    aff = np.diag([1,2,3,1])
    in_dims = list('ijk')
    out_dims = list('xyz')
    cm = AffineTransform.from_params(in_dims, out_dims, aff)
    cm2 = append_io_dim(cm, 'l', 't')
    yield assert_array_equal(cm2.affine, np.diag([1,2,3,1,1]))
    yield assert_equal(cm2.function_range.coord_names,
                       out_dims + ['t'])
    yield assert_equal(cm2.function_domain.coord_names,
                       in_dims + ['l'])
    cm2 = append_io_dim(cm, 'l', 't', 9, 5)
    a2 = np.diag([1,2,3,5,1])
    a2[3,4] = 9
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.function_range.coord_names,
                       out_dims + ['t'])
    yield assert_equal(cm2.function_domain.coord_names,
                       in_dims + ['l'])
    # non square case
    aff = np.array([[2,0,0],
                    [0,3,0],
                    [0,0,1],
                    [0,0,1]])
    cm = AffineTransform.from_params('ij', 'xyz', aff)
    cm2 = append_io_dim(cm, 'q', 't', 9, 5)
    a2 = np.array([[2,0,0,0],
                   [0,3,0,0],
                   [0,0,0,1],
                   [0,0,5,9],
                   [0,0,0,1]])
    yield assert_array_equal(cm2.affine, a2)
    yield assert_equal(cm2.function_range.coord_names,
                       list('xyzt'))
    yield assert_equal(cm2.function_domain.coord_names,
                       list('ijq'))
Exemplo n.º 15
0
def test_input_effects():
    ntotal = data['nimages'] - 1
    # return full rank - mean PCA over last axis
    p = pca(data['fmridata'], -1)
    yield assert_equal(
        p['basis_vectors'].shape,
        (data['nimages'], ntotal))
    yield assert_equal(
        p['basis_projections'].shape,
        data['mask'].shape + (ntotal,))
    yield assert_equal(p['pcnt_var'].shape, (ntotal,))
    # Reconstructed data lacks only mean
    rarr = reconstruct(p['basis_vectors'], p['basis_projections'], -1)
    rarr = rarr + data['fmridata'].mean(-1)[...,None]
    # same effect if over axis 0, which is the default
    arr = data['fmridata']
    arr = np.rollaxis(arr, -1)
    pr = pca(arr)
    out_arr = np.rollaxis(pr['basis_projections'], 0, 4)
    yield assert_array_equal(out_arr, p['basis_projections'])
    yield assert_array_equal(p['basis_vectors'], pr['basis_vectors'])
    yield assert_array_equal(p['pcnt_var'], pr['pcnt_var'])
    # Check axis None raises error
    yield assert_raises(ValueError, pca, data['fmridata'], None)
Exemplo n.º 16
0
def test_mask():
    mean_image = np.ones((9, 9))
    mean_image[3:-3, 3:-3] = 10
    mean_image[5, 5] = 100
    mask1 = nnm.compute_mask(mean_image)
    mask2 = nnm.compute_mask(mean_image, exclude_zeros=True)
    # With an array with no zeros, exclude_zeros should not make
    # any difference
    assert_array_equal(mask1, mask2)
    # Check that padding with zeros does not change the extracted mask
    mean_image2 = np.zeros((30, 30))
    mean_image2[:9, :9] = mean_image
    mask3 = nnm.compute_mask(mean_image2, exclude_zeros=True)
    assert_array_equal(mask1, mask3[:9, :9])
    # However, without exclude_zeros, it does
    mask3 = nnm.compute_mask(mean_image2)
    assert_false(np.allclose(mask1, mask3[:9, :9]))
    # check that  opening is 2 by default
    mask4 = nnm.compute_mask(mean_image, exclude_zeros=True, opening=2)
    assert_array_equal(mask1, mask4)
    # check that opening has an effect
    mask5 = nnm.compute_mask(mean_image, exclude_zeros=True, opening=0)
    assert_true(mask5.sum() > mask4.sum())
Exemplo n.º 17
0
def test_mask():
    mean_image = np.ones((9, 9))
    mean_image[3:-3, 3:-3] = 10
    mean_image[5, 5] = 100
    mask1 = nnm.compute_mask(mean_image)
    mask2 = nnm.compute_mask(mean_image, exclude_zeros=True)
    # With an array with no zeros, exclude_zeros should not make
    # any difference
    assert_array_equal(mask1, mask2)
    # Check that padding with zeros does not change the extracted mask
    mean_image2 = np.zeros((30, 30))
    mean_image2[:9, :9] = mean_image
    mask3 = nnm.compute_mask(mean_image2, exclude_zeros=True)
    assert_array_equal(mask1, mask3[:9, :9])
    # However, without exclude_zeros, it does
    mask3 = nnm.compute_mask(mean_image2)
    assert_false(np.allclose(mask1, mask3[:9, :9]))
    # check that  opening is 2 by default
    mask4 = nnm.compute_mask(mean_image, exclude_zeros=True, opening=2)
    assert_array_equal(mask1, mask4)
    # check that opening has an effect
    mask5 = nnm.compute_mask(mean_image, exclude_zeros=True, opening=0)
    assert_true(mask5.sum() > mask4.sum())
Exemplo n.º 18
0
    img = nibabel.load(infile)
    aff = img.get_affine()
    dat = img.get_data()
    newdat = dat
    
if __name__ == '__main__':

    rootdir = '/home/jagust/cindeem/CODE/manja/testdata'
    infile = os.path.join(rootdir,'resting/B08-247_resting.nii.gz')

    d = get_slicetime_vars(infile)
    assert_almost_equal( d['TA'] , 1.8112499999999998)
    assert_almost_equal( d['TR'] , 1.8899999999999999)
    assert_equal(d['nslices'], 24)
    assert_array_equal(d['sliceorder'],
                       np.concatenate((np.arange(2,25,2),
                                     np.arange(1,25,2))))
    
    # test splitting movement_params
    infile = os.path.join(rootdir, 'movement/rp_B05-2010000.txt')
    outdir, exists = make_dir(rootdir + '/movement', dirname='nuisance')
    assert_equal(exists, True)
    newfiles = split_movement_params(infile, outdir)
    tmp = np.loadtxt(newfiles[0])
    expected_data = np.array([ 0.,  0.00878314,  0.00027707, -0.00278364,
                               -0.00659256, -0.02603804, -0.04524383,
                               -0.02203125, -0.0388689 , -0.02098652])
    assert_almost_equal(tmp[:10], expected_data)