Exemple #1
0
 def test_prepare_set_mesh_vol(self, fn_vol, sphere_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_vol)
     p.mesh = sphere_surf
     assert np.all(
         np.isclose(p.mesh.nodes.node_coord, sphere_surf.nodes.node_coord))
     assert np.all(
         p.mesh.elm.node_number_list == sphere_surf.elm.node_number_list)
Exemple #2
0
    def test_optimize(self, intensity, max_el_c, max_tot_c, max_ac, max_angle,
                      n_targets, sphere_surf, fn_surf, leadfield_surf):

        p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf,
                                    max_individual_current=max_el_c,
                                    max_total_current=max_tot_c,
                                    max_active_electrodes=max_ac)

        for i in range(n_targets):
            t = p.add_target()
            t.indexes = i + 1
            t.directions = [1., 0., 0.]
            t.intensity = intensity

        if max_el_c is None and max_tot_c is None:
            pass
        elif n_targets > 1 and max_angle is not None:
            pass
        else:
            currents = p.optimize()
            assert np.isclose(np.sum(currents), 0, atol=1e-6)
            if max_el_c is not None:
                assert np.max(np.abs(currents)) < max_el_c * 1.05
            if max_tot_c is not None:
                assert np.linalg.norm(currents, 1) < 2 * max_tot_c * 1.05
            if max_ac is not None:
                assert np.linalg.norm(currents, 0) <= max_ac
            for i in range(n_targets):
                field = currents[1:].dot(leadfield_surf[:, i, :])
                assert np.sign(field[0]) == np.sign(intensity)
Exemple #3
0
    def test_optimize_norm(self, max_el_c, max_tot_c, max_ac, n_targets,
                           sphere_surf, fn_surf, leadfield_surf):

        intensity = 3e-5
        p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf,
                                    max_individual_current=max_el_c,
                                    max_total_current=max_tot_c,
                                    max_active_electrodes=max_ac)

        for i in range(n_targets):
            t = p.add_target()
            t.indexes = i + 1
            t.directions = None
            t.intensity = intensity

        if max_el_c is None and max_tot_c is None:
            pass
        else:
            currents = p.optimize()
            assert np.isclose(np.sum(currents), 0, atol=1e-6)
            if max_el_c is not None:
                assert np.max(np.abs(currents)) < max_el_c * 1.05
            if max_tot_c is not None:
                assert np.linalg.norm(currents, 1) < 2 * max_tot_c * 1.05
            if max_ac is not None:
                assert np.linalg.norm(currents, 0) <= max_ac
Exemple #4
0
 def test_get_avoid_field_vol(self, fn_vol, sphere_vol):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_vol)
     t = p.add_avoid()
     t.tissues = 4
     t.weight = 1e5
     avoid_field = p._get_avoid_field()
     assert np.allclose(avoid_field[sphere_vol.elm.tag1 == 4], 1e5)
     assert np.allclose(avoid_field[sphere_vol.elm.tag1 != 4], 1)
Exemple #5
0
 def test_calc_energy_matrix_surf(self, fn_surf, sphere_surf,
                                  leadfield_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf)
     p.mesh = sphere_surf
     energy_mat = p.calc_energy_matrix()
     areas = sphere_surf.nodes_volumes_or_areas().value
     e_matrix = methods.energy_matrix(leadfield_surf, areas)
     assert np.all(np.isclose(energy_mat, e_matrix))
Exemple #6
0
 def test_currents_csv(self, names, fn_elec):
     csv_fn = 'test.csv'
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_elec)
     currents = [-0.2, 0.2]
     p.write_currents_csv(currents, csv_fn, electrode_names=names)
     with open(csv_fn) as f:
         reader = csv.reader(f)
         csv_rows = [row for row in reader]
     os.remove(csv_fn)
     assert csv_rows[0][0] == 'A'
     assert np.isclose(float(csv_rows[0][1]), currents[0])
     assert csv_rows[1][0] == 'B'
     assert np.isclose(float(csv_rows[1][1]), currents[1])
Exemple #7
0
 def test_create_mat_struct(self):
     p = opt_struct.TDCSoptimize(leadfield_hdf='a.hdf5',
                                 max_total_current=2.,
                                 max_individual_current=.1,
                                 max_active_electrodes=3,
                                 name='a',
                                 open_in_gmsh=False)
     m = p.to_mat()
     assert m['leadfield_hdf'] == 'a.hdf5'
     assert m['max_total_current'] == 2.
     assert m['max_individual_current'] == .1
     assert m['max_active_electrodes'] == 3
     assert m['name'] == 'a'
     assert m['open_in_gmsh'] == False
Exemple #8
0
 def test_mat_io(self, max_active_electrodes):
     p = opt_struct.TDCSoptimize(
         leadfield_hdf='a.hdf5',
         max_total_current=2.,
         max_individual_current=.1,
         max_active_electrodes=max_active_electrodes,
         name='aaa')
     m = p.to_mat()
     scipy.io.savemat('tmp.mat', m)
     m = scipy.io.loadmat('tmp.mat',
                          struct_as_record=True,
                          squeeze_me=False)
     os.remove('tmp.mat')
     p = opt_struct.TDCSoptimize.read_mat_struct(m)
     assert p.leadfield_hdf == 'a.hdf5'
     assert p.max_total_current == 2.
     assert p.max_individual_current == .1
     assert p.max_active_electrodes == max_active_electrodes
     assert p.name == 'aaa'
Exemple #9
0
 def test_add_target(self):
     p = opt_struct.TDCSoptimize()
     t = p.add_target()
     assert t is p.target[-1]
Exemple #10
0
 def test_field_elm(self, leadfield_vol, fn_vol):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_vol)
     c = [1., -1., 0, 0., 0.]
     E = p.field(c)
     assert isinstance(E, mesh_io.ElementData)
     assert np.allclose(E.value, -leadfield_vol[0])
Exemple #11
0
 def test_field_node(self, leadfield_surf, fn_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf)
     c = [1., -1., 0, 0., 0.]
     E = p.field(c)
     assert isinstance(E, mesh_io.NodeData)
     assert np.allclose(E.value, -leadfield_surf[0])
Exemple #12
0
 def test_read_lf(self, fn_surf, leadfield_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf)
     assert np.all(np.isclose(p.leadfield, leadfield_surf))
     p.leadfield
Exemple #13
0
 def test_set_lf(self, fn_surf, leadfield_vol):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf)
     p.leadfield = leadfield_vol
     assert np.all(np.isclose(p.leadfield, leadfield_vol))
Exemple #14
0
 def test_add_target_arg(self):
     t = 'taget'
     p = opt_struct.TDCSoptimize()
     p.add_target(t)
     assert t is p.target[-1]
Exemple #15
0
 def test_read_lftype(self, fn_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_surf)
     assert p.lf_type == 'node'
Exemple #16
0
 def test_read_lftype_elm(self, fn_vol):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_vol)
     assert p.lf_type == 'element'
Exemple #17
0
 def test_read_lftype_wrong(self, fn_vol, sphere_surf):
     p = opt_struct.TDCSoptimize(leadfield_hdf=fn_vol)
     p.mesh = sphere_surf
     with pytest.raises(ValueError):
         p.lf_type