示例#1
0
    def test_read_from_hdf5(self):
        l = sim_struct.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

        with tempfile.NamedTemporaryFile(suffix='.hdf5') as f:
            fn_hdf5 = f.name
        l._write_conductivity_to_hdf5(fn_hdf5)
        l2 = sim_struct.SimuList()
        l2._get_conductivity_from_hdf5(fn_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(fn_hdf5)
示例#2
0
    def test_list_cond_mat_struct(self, mat_session):
        l = sim_struct.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 = sim_struct.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])
示例#3
0
    def test_list_cond_array_aniso(self, sphere3_msh):
        l = sim_struct.SimuList()
        l.anisotropy_type = 'dir'
        l.cond[3].value = 7
        l.cond[4].value = 9
        with tempfile.NamedTemporaryFile(suffix='.nii.gz') as f:
            l.fn_tensor_nifti = f.name
        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(l.fn_tensor_nifti)
示例#4
0
    def test_write_to_hdf5(self):
        l = sim_struct.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
        with tempfile.NamedTemporaryFile(suffix='.hdf5') as f:
            fn_hdf5 = f.name
        l._write_conductivity_to_hdf5(fn_hdf5)

        with h5py.File(fn_hdf5) as f:
            assert np.isclose(f['cond/values'][3], 2)
            assert np.isnan(f['cond/values'][46])
            assert f['cond/names'][0] == b'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] == b'beta'

        os.remove(fn_hdf5)
示例#5
0
 def test_list_cond_array(self, sphere3_msh):
     l = sim_struct.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)
示例#6
0
文件: test_gpc.py 项目: wqhot/simnibs
def sampler_args(sphere3):
    poslist = sim_struct.SimuList()

    poslist.cond[2].name = 'inner'
    poslist.cond[2].distribution_type = 'beta'
    poslist.cond[2].distribution_parameters = [2, 3, 0.3, 0.4]
    poslist.cond[3].name = 'middle'
    poslist.cond[3].value = 1
    poslist.cond[4].name = 'outer'
    poslist.cond[4].value = 10
    with tempfile.NamedTemporaryFile(suffix='.hdf5') as f:
        fn_hdf5 = f.name
    if os.path.isfile(fn_hdf5):
        os.remove(fn_hdf5)

    return sphere3, poslist, fn_hdf5, [3]
示例#7
0
    def test_list_cond_array_aniso_vn(self, sphere3_msh):
        l = sim_struct.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])
示例#8
0
 def test_list_read_mat_cond(self, mat_session):
     l = sim_struct.SimuList()
     l.read_cond_mat_struct(mat_session['poslist'][0][0][0][0])
     assert l.cond[1].name == 'GM'
示例#9
0
 def test_list_postprocess(self):
     l = sim_struct.SimuList()
     with pytest.raises(ValueError):
         l.postprocess = 'Y'
示例#10
0
 def test_list_anisotropy(self):
     l = sim_struct.SimuList()
     with pytest.raises(ValueError):
         l.anisotropy_type = None
示例#11
0
 def test_set_conductivity(self):
     l = sim_struct.SimuList()
     l.conductivity[1].value = 5
     assert l.cond[0].value == 5
示例#12
0
文件: test_gpc.py 项目: wqhot/simnibs
    def test_postprocessing(self, gpc_regression_instance, sphere3):
        fn = 'gpc_testing.hdf5'
        if os.path.isfile(fn):
            os.remove(fn)
        msh = sphere3.crop_mesh(elm_type=4)
        msh.write_hdf5(fn, 'mesh_roi/')
        # "normalized" coordinates
        coords_norm = np.array([[-.5], [0], [.7]])
        # Define regression object.
        # The random variable is the conductivity of the layer 3
        # Uniform distribution, between 0 and 10
        gpc_reg = \
            simnibs_gpc.gPC_regression([3],
                                       ['beta'], [[1], [1]],
                                       [[0], [10]], [[0], [1]],
                                       coords_norm, 'TCS',
                                       data_file=fn)
        gpc_reg.regularization_factors = [0]
        # Create potentials
        pot = np.tile(msh.nodes.node_coord[None, :, 0],
                      (coords_norm.shape[0], 1))
        lst = sim_struct.SimuList()
        lst.cond[3].value = 1   # Layer 4 conductivity
        lst.cond[4].value = 10  # Layer 5 conductivity
        lst._write_conductivity_to_hdf5(fn)
        for i, c in enumerate(gpc_reg.grid.coords):
            pot[i] *= c  # Linear relationship between potential and random variable
        #  Record potentials
        with h5py.File(fn, 'a') as f:
            f.create_group('mesh_roi/data_matrices')
            f['mesh_roi/data_matrices'].create_dataset('v_samples', data=pot)
        #  Postprocess
        gpc_reg.postprocessing('eEjJ')
        with h5py.File(fn, 'r') as f:
            # Linear relationship between random variable and potential, uniform
            # distribution
            mean_E = f['mesh_roi/elmdata/E_mean'][()]
            mean_E_expected = -np.array([5, 0, 0]) * 1e3
            assert np.allclose(mean_E, mean_E_expected)
            mean_e = f['mesh_roi/elmdata/normE_mean'][()]
            assert np.allclose(mean_e, 5e3)
            # J is a bit more complicated
            mean_J = f['mesh_roi/elmdata/J_mean'][()]
            assert np.allclose(mean_J[msh.elm.tag1==4],
                               mean_E_expected)
            assert np.allclose(mean_J[msh.elm.tag1==5],
                               10 * mean_E_expected)
            coeffs = gpc_reg.expand(gpc_reg.grid.coords ** 2, return_error=False)
            mean = gpc_reg.mean(coeffs)
            assert np.allclose(mean_J[msh.elm.tag1==3],
                               mean * -np.array([1, 0, 0]) * 1e3)
            dsets = f['mesh_roi/elmdata/'].keys()

            std_E = f['mesh_roi/elmdata/E_std'][()]
            assert np.allclose(std_E, np.array([np.sqrt(1e8 / 12), 0., 0.]))
            assert 'normE_std' in dsets
            assert 'J_std' in dsets

            assert 'E_sobol_3' in dsets
            assert 'normE_sobol_3' in dsets
            assert 'J_sobol_3' in dsets

            assert 'E_sensitivity_3' in dsets
            assert 'normE_sensitivity_3' in dsets
            assert 'J_sensitivity_3' in dsets

        os.remove(fn)