def test_read_from_hdf5(self):
        l = nnav.SimuList()
        l.anisotropy_type = 'vn'
        l.cond[3].value = 2
        l.anisotropic_tissues = [3]
        l.cond[0].distribution_type = 'beta'
        v = np.zeros((255, 255, 255, 6))
        for i in range(6):
            v[:, :, :, i] = i
        affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127],
                           [0, 0, 0, 1]])
        l.anisotropy_vol = v
        l.anisotropy_affine = affine
        try:
            os.remove('/tmp/cond.hdf5')
        except:
            pass

        l._write_conductivity_to_hdf5('/tmp/cond.hdf5')
        l2 = nnav.SimuList()
        l2._get_conductivity_from_hdf5('/tmp/cond.hdf5')

        assert np.isclose(l2.cond[3].value, 2)
        assert l2.cond[46].value is None
        assert l2.cond[0].name == 'WM'
        assert l2.cond[46].name is None
        assert np.all(l2.anisotropic_tissues == [3])
        assert l2.anisotropy_type == 'vn'
        assert np.allclose(l2.anisotropy_affine, affine)
        assert np.allclose(l2.anisotropy_vol, v)
        assert l2.cond[0].distribution_type == 'beta'
        assert l2.cond[1].distribution_type is None

        os.remove('/tmp/cond.hdf5')
    def test_list_cond_mat_struct(self, mat_session):
        l = nnav.SimuList()
        l.cond[0].distribution_type = 'uniform'
        l.cond[0].distribution_parameters = [1.5, 3.0]
        mat = l.cond_mat_struct()
        scipy.io.savemat('tmp.mat', mat)

        mat = scipy.io.loadmat('tmp.mat',
                               struct_as_record=True,
                               squeeze_me=False)
        l2 = nnav.SimuList()
        l2.read_cond_mat_struct(mat)
        os.remove('tmp.mat')
        assert l2.cond[0].distribution_type == 'uniform'
        assert np.all(l2.cond[0].distribution_parameters == [1.5, 3.0])
    def test_write_to_hdf5(self):
        l = nnav.SimuList()
        l.anisotropy_type = 'vn'
        l.cond[3].value = 2
        l.cond[0].distribution_type = 'beta'
        l.anisotropic_tissues = [3]
        v = np.zeros((255, 255, 255, 6))
        for i in range(6):
            v[:, :, :, i] = i
        affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127],
                           [0, 0, 0, 1]])
        l.anisotropy_vol = v
        l.anisotropy_affine = affine
        try:
            os.remove('/tmp/cond.hdf5')
        except:
            pass

        l._write_conductivity_to_hdf5('/tmp/cond.hdf5')

        with h5py.File('/tmp/cond.hdf5') as f:
            assert np.isclose(f['cond/values'][3], 2)
            assert np.isnan(f['cond/values'][46])
            assert f['cond/names'][0] == 'WM'
            assert f['cond/names'][0] == 'WM'
            assert np.all(f['cond'].attrs['anisotropic_tissues'] == [3])
            assert f['cond'].attrs['anisotropy_type'] == 'vn'
            assert np.allclose(f['cond/anisotropy_affine'], affine)
            assert np.allclose(f['cond/anisotropy_vol'], v)
            assert f['cond/distribution_types'][0] == 'beta'

        os.remove('/tmp/cond.hdf5')
    def test_list_cond_array_aniso(self, sphere3_msh):
        l = nnav.SimuList()
        l.anisotropy_type = 'dir'
        l.cond[3].value = 7
        l.cond[4].value = 9
        l.fn_tensor_nifti = '/tmp/test_aniso.nii.gz'
        v = np.zeros((255, 255, 255, 6))
        for i in range(6):
            v[:, :, :, i] = i
        affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127],
                           [0, 0, 0, 1]])

        img = nibabel.Nifti1Image(v, affine)
        nibabel.save(img, l.fn_tensor_nifti)
        msh = copy.deepcopy(sphere3_msh)
        msh.elm.tag1[msh.elm.tag1 == 3] = 1

        l.mesh = msh
        elmcond = l.cond2elmdata()
        assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 1],
                           [0, -1, -2, -1, 3, 4, -2, 4, 5])
        assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 4],
                           [7, 0, 0, 0, 7, 0, 0, 0, 7])
        assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 5],
                           [9, 0, 0, 0, 9, 0, 0, 0, 9])
        os.remove('/tmp/test_aniso.nii.gz')
 def test_list_cond_array(self, sphere3_msh):
     l = nnav.SimuList()
     l.cond[0].value = None
     l.cond[1].value = None
     l.cond[2].value = 21
     l.cond[3].value = 31
     l.cond[4].value = 41
     l.mesh = sphere3_msh
     elmcond = l.cond2elmdata()
     assert np.all(elmcond.value[sphere3_msh.elm.tag1 == 3] == 21)
     assert np.all(elmcond.value[sphere3_msh.elm.tag1 == 4] == 31)
     assert np.all(elmcond.value[sphere3_msh.elm.tag1 == 5] == 41)
    def test_list_cond_array_aniso_vn(self, sphere3_msh):
        l = nnav.SimuList()
        l.anisotropy_type = 'vn'
        l.conductivity[3].value = 2
        l.conductivity[4].value = 7
        l.conductivity[5].value = 9
        l.anisotropic_tissues = [3]
        l.fn_tensor_nifti = '/tmp/test_aniso.nii.gz'
        l.mesh = sphere3_msh
        v = np.zeros((255, 255, 255, 6))
        # set-up tensor
        v1 = np.random.rand(3)
        v1 /= np.linalg.norm(v1)
        v2 = np.random.rand(3)
        v2 = v2 - v1.dot(v2) * v1 / np.linalg.norm(v1)
        v2 /= np.linalg.norm(v2)
        v3 = np.random.rand(3)
        v3 = v3 - v1.dot(v3) * v1 / np.linalg.norm(v1)
        v3 = v3 - v2.dot(v3) * v2 / np.linalg.norm(v2)
        v3 /= np.linalg.norm(v3)
        t = np.outer(v1, v1) + 2 * np.outer(v2, v2) + 3 * np.outer(v3, v3)
        v[:, :, :] = t.reshape(-1)[[0, 1, 2, 4, 5, 8]]
        affine = np.array([[-1, 0, 0, 128], [0, 1, 0, -128], [0, 0, 1, -127],
                           [0, 0, 0, 1]])

        l.anisotropy_vol = v
        l.anisotropy_affine = affine

        elmcond = l.cond2elmdata()
        tensor = elmcond.value[sphere3_msh.elm.tag1 == 3].reshape(-1, 3, 3)
        t = affine[:3, :3].dot(t).dot(affine[:3, :3])
        assert np.allclose(np.linalg.det(tensor), 2**3)
        assert np.allclose(
            np.linalg.eig(tensor)[1][:, 0],
            np.linalg.eig(t)[1][0])
        assert np.allclose(
            np.linalg.eig(tensor)[1][:, 1],
            np.linalg.eig(t)[1][1])
        assert np.allclose(
            np.linalg.eig(tensor)[1][:, 2],
            np.linalg.eig(t)[1][2])
        assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 4],
                           [7, 0, 0, 0, 7, 0, 0, 0, 7])
        assert np.allclose(elmcond.value[sphere3_msh.elm.tag1 == 5],
                           [9, 0, 0, 0, 9, 0, 0, 0, 9])
 def test_list_read_mat_cond(self, mat_session):
     l = nnav.SimuList()
     l.read_cond_mat_struct(mat_session['poslist'][0][0][0][0])
     assert l.cond[1].name == 'GM'
 def test_list_postprocess(self):
     l = nnav.SimuList()
     with pytest.raises(ValueError):
         l.postprocess = 'Y'
 def test_list_anisotropy(self):
     l = nnav.SimuList()
     with pytest.raises(ValueError):
         l.anisotropy_type = None
示例#10
0
 def test_set_conductivity(self):
     l = nnav.SimuList()
     l.conductivity[1].value = 5
     assert l.cond[0].value == 5