Exemple #1
0
 def test_create_mat_struct(self):
     targets = [
         opt_struct.TDCStarget(indexes=1, directions=[0, 1, 0], radius=5),
         opt_struct.TDCStarget(indexes=[1, 2],
                               intensity=.3,
                               max_angle=30,
                               tissues=[3, 4])
     ]
     m = opt_struct._save_TDCStarget_mat(targets)
     assert np.all(m[0]['indexes'] == 1)
     assert np.all(m[0]['directions'] == [0, 1, 0])
     assert np.all(m[0]['radius'] == 5)
     assert np.all(m[1]['indexes'] == [1, 2])
     assert np.all(m[1]['intensity'] == .3)
     assert np.all(m[1]['max_angle'] == 30)
     assert np.all(m[1]['tissues'] == [3, 4])
Exemple #2
0
 def test_as_field_none(self, lf_type, sphere_surf):
     t = opt_struct.TDCStarget(indexes=[1, 2],
                               directions=None,
                               mesh=sphere_surf,
                               lf_type=lf_type,
                               intensity=2)
     d = t.as_field()
     assert np.allclose(d[[1, 2]], 2)
     assert np.allclose(d[3:], 0)
Exemple #3
0
 def test_mean_angle(self, sphere_vol):
     t = opt_struct.TDCStarget(indexes=[1, 2],
                               directions=[[1, 0, 0], [0, 1, 0]],
                               mesh=sphere_vol,
                               lf_type='element',
                               radius=0)
     f = mesh_io.ElementData(np.zeros((sphere_vol.elm.nr, 3)))
     f[1] = [1, 1, 0]
     f[2] = [2, 2, 0]
     assert np.isclose(t.mean_angle(f), 45)
Exemple #4
0
 def test_as_field(self, intensity, lf_type, sphere_surf):
     t = opt_struct.TDCStarget(indexes=[1, 2],
                               directions=[[1, 0, 0], [0, 2, 0]],
                               mesh=sphere_surf,
                               lf_type=lf_type,
                               intensity=intensity)
     d = t.as_field()
     assert np.allclose(d[1], [intensity, 0, 0])
     assert np.allclose(d[2], [0, intensity, 0])
     assert np.allclose(d[3:], 0)
Exemple #5
0
 def test_get_indexes_and_directions_2_targets_1_dir(self, sphere_surf):
     idx = [1, 2]
     directions = [[2., 0., 0.]]
     t = opt_struct.TDCStarget(indexes=idx,
                               directions=directions,
                               mesh=sphere_surf,
                               lf_type='node')
     id_, dir_ = t.get_indexes_and_directions()
     assert np.all(id_ == [0, 1])
     assert np.allclose(dir_, [[1., 0., 0.], [1., 0., 0.]])
Exemple #6
0
 def test_get_indexes_and_directions_none(self, sphere_surf):
     idx = [1]
     directions = None
     t = opt_struct.TDCStarget(indexes=idx,
                               directions=directions,
                               mesh=sphere_surf,
                               lf_type='node')
     id_, dir_ = t.get_indexes_and_directions()
     assert np.all(id_ == [0])
     assert dir_ is None
Exemple #7
0
 def test_mat_io(self):
     targets = [
         opt_struct.TDCStarget(indexes=1, directions=[0, 1, 0]),
         opt_struct.TDCStarget(indexes=[1, 2], intensity=.3, max_angle=30)
     ]
     m = opt_struct._save_TDCStarget_mat(targets)
     scipy.io.savemat('tmp.mat', {'targets': m})
     m = scipy.io.loadmat('tmp.mat',
                          struct_as_record=True,
                          squeeze_me=False)
     os.remove('tmp.mat')
     t = opt_struct.TDCStarget.read_mat_struct(m['targets'][0][0])
     assert t.indexes == [1]
     assert np.allclose(t.directions, [0, 1, 0])
     t = opt_struct.TDCStarget.read_mat_struct(m['targets'][0][1])
     assert np.all(t.indexes == [1, 2])
     assert t.directions == 'normal'
     assert t.intensity == .3
     assert t.max_angle == 30
Exemple #8
0
 def test_mean_intensity_none(self, sphere_vol):
     t = opt_struct.TDCStarget(indexes=[1, 2],
                               directions=None,
                               mesh=sphere_vol,
                               lf_type='element',
                               radius=0)
     f = mesh_io.ElementData(np.zeros((sphere_vol.elm.nr, 3)))
     f[1] = [np.sqrt(2), np.sqrt(2), 0]
     f[2] = [np.sqrt(3), np.sqrt(3), np.sqrt(3)]
     vols = sphere_vol.elements_volumes_and_areas()[:]
     m = np.average([2, 3], weights=vols[:2])
     assert np.isclose(t.mean_intensity(f), m)
Exemple #9
0
 def test_as_field_radius(self, sphere_vol):
     bar = sphere_vol.elements_baricenters().value
     t = opt_struct.TDCStarget(positions=bar[0],
                               directions=[[1, 0, 0]],
                               mesh=sphere_vol,
                               lf_type='element',
                               intensity=1.,
                               radius=20,
                               tissues=4)
     d = t.as_field()
     in_r = ((np.linalg.norm(bar - bar[0], axis=1) < 20) *
             (sphere_vol.elm.tag1 == 4))
     assert np.allclose(d[in_r], [1., 0, 0])
     assert np.allclose(d[~in_r], [0, 0, 0])
Exemple #10
0
 def test_calc_target_matrices(self, sphere_surf, leadfield_surf):
     idx = [1]
     directions = [2., 0., 0.]
     t = opt_struct.TDCStarget(indexes=idx,
                               directions=directions,
                               mesh=sphere_surf,
                               lf_type='node')
     t_mat, Q_mat = t.calc_target_matrices(leadfield_surf)
     areas = sphere_surf.nodes_volumes_or_areas().value
     idx_ = [0]
     directions_ = [1., 0., 0.]
     t, Q = methods.target_matrices(leadfield_surf, idx_, directions_,
                                    areas)
     assert np.all(np.isclose(t_mat, t))
     assert np.all(np.isclose(Q_mat, Q))
Exemple #11
0
    def test_get_indexes_and_directions_radius(self, sphere_vol):
        bar = sphere_vol.elements_baricenters().value
        idx_ = np.where((np.linalg.norm(bar - bar[0], axis=1) < 20) *
                        (sphere_vol.elm.tag1 == 4))[0]
        directions = [[2., 0., 0.]]

        t = opt_struct.TDCStarget(positions=bar[0],
                                  directions=directions,
                                  mesh=sphere_vol,
                                  lf_type='element',
                                  radius=20,
                                  tissues=4)

        id_, dir_ = t.get_indexes_and_directions()

        directions_ = [(1., 0., 0.)] * len(idx_)

        assert np.all(id_ == idx_)
        assert np.allclose(dir_, directions_)
Exemple #12
0
    def test_calc_target_matrices_radius(self, sphere_vol, leadfield_vol):
        vols = sphere_vol.elements_volumes_and_areas().value
        bar = sphere_vol.elements_baricenters().value
        idx_ = np.where((np.linalg.norm(bar - bar[0], axis=1) < 20) *
                        (sphere_vol.elm.tag1 == 4))[0]
        directions = [[2., 0., 0.]]

        t = opt_struct.TDCStarget(positions=bar[0],
                                  directions=directions,
                                  mesh=sphere_vol,
                                  lf_type='element',
                                  radius=20,
                                  tissues=4)
        t_mat, Q_mat = t.calc_target_matrices(leadfield_vol)

        directions_ = [(1., 0., 0.)] * len(idx_)
        t, Q = methods.target_matrices(leadfield_vol, idx_, directions_, vols)
        assert np.all(np.isclose(t_mat, t))
        assert np.all(np.isclose(Q_mat, Q))