def _test_output_dim_directed(self, method): n_components = 4 embed = method(n_components=n_components, concat=True) n = 10 M = 20 A = er_nm(n, M, directed=True) + 5 self.assertEqual(embed.fit_transform(A).shape, (n, 8)) self.assertEqual(embed.latent_left_.shape, (n, 4)) self.assertEqual(embed.latent_right_.shape, (n, 4))
def test_baseembed_er(self): n_components = 4 embed = BaseSpectralEmbed(n_components=n_components) n = 10 M = 20 A = er_nm(n, M) + 5 embed._reduce_dim(A) self.assertEqual(embed.latent_left_.shape, (n, n_components)) self.assertTrue(embed.latent_right_ is None)
def _test_output_dim(self, method, *args, **kwargs): n_components = 4 embed = method(n_components=n_components) n = 10 M = 20 A = er_nm(n, M) + 5 embed._reduce_dim(A) self.assertEqual(embed.latent_left_.shape, (n, 4)) self.assertTrue(embed.latent_right_ is None)
def test_omni_unconnected(): np.random.seed(4) n = 100 m = 50 graphs = [er_nm(n, m) for _ in range(2)] omni = OmnibusEmbed() with pytest.warns(UserWarning): omni.fit(graphs)
def test_baseembed_er_directed_concat(self): n_components = 4 embed = BaseSpectralEmbed(n_components=n_components, concat=True) n = 10 M = 20 A = er_nm(n, M, directed=True) embed._reduce_dim(A) out = embed.fit_transform(A) self.assertEqual(out.shape, (n, 2 * n_components)) self.assertTrue(embed.latent_right_ is not None)
def _gen_mat_data(n: int = 20, m: int = 20, p: int = 0.50, mat_type: str = 'sb', binary: bool = False, asfile: bool = True, n_graphs: int = 1): if binary is True: wt = 1 else: wt = np.random.uniform mat_list = [] mat_file_list = [] for nm in range(n_graphs): if mat_type == 'er': mat = largest_connected_component( symmetrize( remove_loops( er_nm(n, m, wt=np.random.uniform, wtargs=dict(low=0, high=1))))) elif mat_type == 'sb': if p is None: raise ValueError( f"for mat_type {mat_type}, p cannot be None") mat = largest_connected_component( symmetrize( remove_loops( sbm(np.array([n]), np.array([[p]]), wt=wt, wtargs=dict(low=0, high=1))))) else: raise ValueError(f"mat_type {mat_type} not recognized!") mat_list.append(mat) if asfile is True: mat_path_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.npy', delete=False) mat_path = str(mat_path_tmp.name) np.save(mat_path, mat) mat_file_list.append(mat_path) mat_path_tmp.close() return {'mat_list': mat_list, 'mat_file_list': mat_file_list}
def test_unconnected_warning(self): A = csr_matrix(er_nm(100, 10)) with self.assertWarns(UserWarning): ase = AdjacencySpectralEmbed() ase.fit(A)
def _gen_mat_data(n: int=20, m: int=20, p: int=0.50, mat_type: str='sb', binary: bool=False, asfile: bool=True, n_graphs: int=1, lcc: bool=False, modality: str='func'): if binary is True: wt = 1 else: wt = np.random.uniform mat_list = [] mat_file_list = [] if n_graphs > 0: for nm in range(n_graphs): if mat_type == 'er': mat = symmetrize( remove_loops(er_nm(n, m, wt=np.random.uniform, wtargs=dict(low=0, high=1)))) elif mat_type == 'sb': if p is None: raise ValueError( f"for mat_type {mat_type}, p cannot be None") mat = symmetrize( remove_loops(sbm(np.array([n]), np.array([[p]]), wt=wt, wtargs=dict(low=0, high=1)))) else: raise ValueError(f"mat_type {mat_type} not recognized!") if lcc is True: mat = largest_connected_component(mat) mat_list.append(autofix(mat)) if asfile is True: path_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.npy', delete=False) mat_path_tmp = str(path_tmp.name) out_folder = f"{str(Path.home())}/test_mats" os.makedirs(out_folder, exist_ok=True) if modality == 'func': mat_path = f"{out_folder}/graph_sub-999_modality-func_" \ f"model-corr_template-" \ f"MNI152_2mm_" \ f"parc_tol-6fwhm_hpass-" \ f"0Hz_" \ f"signal-mean_thrtype-prop_thr-" \ f"{round(random.uniform(0, 1),2)}.npy" elif modality == 'dwi': mat_path = f"{out_folder}/graph_sub-999_modality-func_" \ f"model-csa_template-" \ f"MNI152_2mm_tracktype-local_" \ f"traversal-det_minlength-30_" \ f"tol-5_thrtype-prop_thr-" \ f"{round(random.uniform(0, 1),2)}.npy" shutil.copyfile(mat_path_tmp, mat_path) np.save(mat_path, mat) mat_file_list.append(mat_path) path_tmp.close() return {'mat_list': mat_list, 'mat_file_list': mat_file_list}
def test_baseembed(self): embed = BaseSpectralEmbed(n_components=None) n = 10 M = 20 A = er_nm(n, M) + 5 embed._reduce_dim(A)
def test_unconnected_warning(self): A = er_nm(100, 10) with pytest.warns(UserWarning): ase = AdjacencySpectralEmbed() ase.fit(A)