def test_ER_score(self): p_mat = self.p_mat graph = self.graph estimator = EREstimator(directed=False) _test_score(estimator, p_mat, graph) with pytest.raises(ValueError): estimator.score_samples(graph=er_np(500, 0.5))
def test_ER_sample(self): with pytest.raises(ValueError): self.estimator.sample(n_samples=-1) with pytest.raises(TypeError): self.estimator.sample(n_samples="nope") g = er_np(100, 0.5) estimator = EREstimator(directed=True, loops=False) estimator.fit(g) p_mat = np.full((100, 100), 0.5) p_mat -= np.diag(np.diag(p_mat)) _test_sample(estimator, p_mat)
def evaluate_models(graph, labels=None, title=None, plot_graphs=False, min_comp=0, max_comp=1, n_comp=5): if plot_graphs: heatmap(graph, inner_hier_labels=cell_labels) ## Set up models to test non_rdpg_models = [ EREstimator(fit_degrees=False), SBEstimator(fit_degrees=False), SBEstimator(fit_degrees=True), ] d = [6] rdpg_models = [RDPGEstimator(n_components=i) for i in d] models = non_rdpg_models + rdpg_models names_nonRDPG = ["ER", "SBM", "DCSBM"] names_RDPG = ["RDPGrank{}".format(i) for i in d] names = names_nonRDPG + names_RDPG bics = [] log_likelihoods = [] ## Test models for model, name in zip(models, names): m = model.fit(graph, y=labels) if plot_graphs: heatmap(m.p_mat_, inner_hier_labels=labels, title=(name + "P matrix")) heatmap(m.sample(), inner_hier_labels=labels, title=(name + "sample")) bic = m.bic(graph) log_likelihoods.append(m.score(graph)) bics.append(bic) plt.show() ase = AdjacencySpectralEmbed(n_components=2) latent = ase.fit_transform(m.p_mat_) # if type(latent) is tuple: # pairplot(np.concatenate((latent[0], latent[1]), axis=1)) # plt.show() # else: print("here") # plt.figure(figsize=(20, 20)) ax = scatterplot(latent, labels=cell_labels, height=4, alpha=0.6, font_scale=1.25) # plt.suptitle(name, y=0.94, x=0.1, fontsize=30, horizontalalignment="left") plt.savefig(name + "latent.png", format="png", dpi=1000) plt.close()
def setup_class(cls): np.random.seed(8888) cls.graph = er_np(1000, 0.5) cls.p = 0.5 cls.p_mat = np.full((1000, 1000), 0.5) cls.estimator = EREstimator(directed=True, loops=False) cls.estimator.fit(cls.graph) cls.p_hat = cls.estimator.p_
def test_ER_inputs(self): ere = EREstimator() with pytest.raises(TypeError): EREstimator(directed="hey") with pytest.raises(TypeError): EREstimator(loops=6) with pytest.raises(ValueError): ere.fit(self.graph[:, :99]) with pytest.raises(ValueError): ere.fit(self.graph[..., np.newaxis])
**params) plotHeatmap(adj, "Drosophila right MB") # --------------------------------------- # Erdos-Reyni (ER) model # --------------------------------------- '''The Erdos-Reyni (ER) model is the simplest random graph model one could write down. We are interested in modeling the probability of an edge existing between any two nodes, π and π. We denote this probability πππ. For the ER model: πππ = π, for any combination of π and π. This means that the one parameter π is the overall probability of connection for any two nodes''' from graspy.models import EREstimator er = EREstimator(directed=True, loops=False) # Create Erdos-Reyni estimator er.fit(adj) # Fit Erdos-Reyni model # Show results fig, ax = plt.subplots(1, 2) plotHeatmap(er.p_mat_, "ER probability matrix", params={ 'vmin': 0, 'vmax': 1, 'ax': ax[0] }) plotHeatmap(er.sample()[0], "ER sample", params={'ax': ax[1]}) fig.suptitle('Erdos-Reyni (ER) model', fontsize=16) print(f"ER \"p\" parameter: {er.p_}") plt.savefig('figure.png')
# With only that, you can design all the graphs you want, except RDPG because it uses some latent positions (see notes), # and that can only be generated by ASE. adj, labels = load_drosophila_right(return_labels=True) # makes the adjacency matrix contain only 0 and 1 as float values adj = binarize(adj) info = heatmap(adj, inner_hier_labels=labels, title='Drosophila right MB', font_scale=1.5, sort_nodes=True); plt.savefig("DrosophilaRightMB", bbox_inches='tight') er = EREstimator(directed=True,loops=False) er.fit(adj) print(f"ER \"p\" parameter: {er.p_}") heatmap(er.p_mat_, inner_hier_labels=labels, font_scale=1.5, title="ER probability matrix", vmin=0, vmax=1, sort_nodes=True) plt.savefig("ERProbabilityMatrix", bbox_inches='tight') heatmap(er.sample()[0], inner_hier_labels=labels, font_scale=1.5, title="ER sample",
def test_ER_score(self): p_mat = self.p_mat graph = self.graph estimator = EREstimator(directed=False) _test_score(estimator, p_mat, graph)