def test_optimize(self, intensity, max_el_c, max_tot_c, max_ac, sphere_surf, fn_surf, leadfield_surf): target_img = np.random.rand(100, 100, 100) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf, max_individual_current=max_el_c, max_total_current=max_tot_c, max_active_electrodes=max_ac, target_image=(target_img, affine), intensity=intensity, min_img_value=0, mni_space=False) 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
def test_prepare_set_mesh_vol(self, fn_vol, sphere_surf): p = opt_struct.TDCSDistributedOptimize(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)
def test_target_distribution_subject(self, sphere_surf, fn_surf): target_image = np.meshgrid(np.arange(-100, 100, 2), np.arange(-100, 100, 2), np.arange(-100, 100, 2), indexing='ij')[0].astype(np.float) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 min_img_value = 10 p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf, target_image=(target_image, affine), mni_space=False, min_img_value=min_img_value, intensity=2) field, W = p._target_distribution() assert np.allclose(field[np.abs(field) > min_img_value], 2 * sphere_surf.nodes[np.abs(field) > min_img_value, 0], atol=1e-2) assert np.allclose( field[np.abs(sphere_surf.nodes[:, 0]) < min_img_value], 0) assert np.allclose( W[np.abs(sphere_surf.nodes[:, 0]) > min_img_value], np.abs( sphere_surf.nodes[np.abs(sphere_surf.nodes[:, 0]) > min_img_value, 0]), atol=1e-2) assert np.allclose(W[np.abs(sphere_surf.nodes[:, 0]) < min_img_value], min_img_value)
def test_field_msh_node(self, leadfield_surf, fn_surf, sphere_surf): target_distribution = np.meshgrid(np.arange(-100, 100, 2), np.arange(-100, 100, 2), np.arange(-100, 100, 2), indexing='ij')[0].astype(np.float) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 p = opt_struct.TDCSDistributedOptimize( leadfield_hdf=fn_surf, target_image=(target_distribution, affine), mni_space=False, min_img_value=0., intensity=1, ) c = [1., -1., 0, 0., 0.] m = p.field_mesh(c) normals = sphere_surf.nodes_normals()[:] assert isinstance(m.field['Field'], mesh_io.NodeData) assert np.allclose(m.field['Field'][:], -leadfield_surf[0]) assert np.allclose(m.field['normField'][:], np.linalg.norm(leadfield_surf[0], axis=1)) assert np.allclose(m.field['normalField'][:], np.sum(leadfield_surf[0] * normals, axis=1)) assert np.allclose(m.field['target_map'][:], sphere_surf.nodes[:, 0], atol=1e-3)
def test_terget_field_mni(self, s2mni_coords_mock, sphere_surf, fn_surf): target_image = np.meshgrid(np.arange(-100, 100, 2), np.arange(-100, 100, 2), np.arange(-100, 100, 2), indexing='ij')[0].astype(np.float) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 s2mni_coords_mock.return_value = -sphere_surf.nodes[:] p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf, target_image=(target_image, affine), subpath='', min_img_value=0, intensity=1) field, _ = p._target_distribution() assert np.allclose(field, -sphere_surf.nodes[:, 0], atol=1e-2)
def test_target_distribution_subject_elm(self, sphere_vol, fn_vol): target_image = np.meshgrid(np.arange(-100, 100, 2), np.arange(-100, 100, 2), np.arange(-100, 100, 2), indexing='ij')[0].astype(np.float) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_vol, target_image=(target_image, affine), mni_space=False, min_img_value=0, intensity=1) field, _ = p._target_distribution() assert np.allclose(field, sphere_vol.elements_baricenters()[:, 0], atol=1e-1)
def test_target_distribution_subject_file(self, sphere_surf, fn_surf): target_image = np.meshgrid(np.arange(-100, 100, 2), np.arange(-100, 100, 2), np.arange(-100, 100, 2), indexing='ij')[0].astype(np.float) affine = np.eye(4) affine[:3, 3] = -100 affine[:3, :3] *= 2 fn_nii = 'tmp.nii.gz' nibabel.save(nibabel.Nifti1Image(target_image, affine), fn_nii) p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf, target_image=fn_nii, mni_space=False, min_img_value=0, intensity=1) field, W = p._target_distribution() os.remove(fn_nii) assert np.allclose(field, sphere_surf.nodes[:, 0], atol=1e-3) assert np.allclose(W, np.abs(sphere_surf.nodes[:, 0]), atol=1e-3)
def test_set_lf(self, fn_surf, leadfield_vol): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf) p.leadfield = leadfield_vol assert np.all(np.isclose(p.leadfield, leadfield_vol))
def test_read_lf(self, fn_surf, leadfield_surf): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf) assert np.all(np.isclose(p.leadfield, leadfield_surf)) p.leadfield
def test_field_node(self, leadfield_surf, fn_surf): p = opt_struct.TDCSDistributedOptimize(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])
def test_normals_vol(self, fn_vol, sphere_vol): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_vol) with pytest.raises(ValueError): p.normal_directions()
def test_normals_surf(self, fn_surf, sphere_surf): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf) n = p.normal_directions() assert np.allclose(n, -sphere_surf.nodes_normals()[:])
def test_read_lftype_wrong(self, fn_vol, sphere_surf): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_vol) p.mesh = sphere_surf with pytest.raises(ValueError): p.lf_type
def test_read_lftype_elm(self, fn_vol): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_vol) assert p.lf_type == 'element'
def test_read_lftype(self, fn_surf): p = opt_struct.TDCSDistributedOptimize(leadfield_hdf=fn_surf) assert p.lf_type == 'node'