def test_DCER_score(self): p_mat = self.p_mat graph = self.graph estimator = DCEREstimator() _test_score(estimator, p_mat, graph) with pytest.raises(ValueError): estimator.score_samples(graph=graph[1:500, 1:500])
def test_DCER_inputs(self): with pytest.raises(TypeError): DCEREstimator(directed="hey") with pytest.raises(TypeError): DCEREstimator(loops=6) graph = er_np(100, 0.5) dcere = DCEREstimator() with pytest.raises(ValueError): dcere.fit(graph[:, :99]) with pytest.raises(ValueError): dcere.fit(graph[..., np.newaxis])
def test_DCER_nparams(self): n_verts = 1000 graph = self.graph e = DCEREstimator(directed=True) e.fit(graph) assert e._n_parameters() == (n_verts + 1)
def test_DCER_sample(self): np.random.seed(8888) estimator = DCEREstimator(directed=True, loops=False) g = self.graph p_mat = self.p_mat with pytest.raises(NotFittedError): estimator.sample() estimator.fit(g) with pytest.raises(ValueError): estimator.sample(n_samples=-1) with pytest.raises(TypeError): estimator.sample(n_samples="nope") B = 0.5 dc = np.random.uniform(0.25, 0.75, size=100) p_mat = np.outer(dc, dc) * B p_mat -= np.diag(np.diag(p_mat)) g = sample_edges(p_mat, directed=True) estimator.fit(g) estimator.p_mat_ = p_mat _test_sample(estimator, p_mat, n_samples=1000, atol=0.2)
fig.suptitle('Erdos-Reyni (ER) model', fontsize=16) print(f"ER \"p\" parameter: {er.p_}") plt.savefig('figure.png') # --------------------------------------- # Degree-corrected Erdos-Reyni (DCER) model # --------------------------------------- '''A slightly more complicated variant of the ER model is the degree-corrected Erdos-Reyni model (DCER). Here, there is still a global parameter 𝑝 to specify relative connection probability between all edges. However, we add a promiscuity parameter 𝜃𝑖 for each node 𝑖 which specifies its expected degree relative to other nodes: 𝑃𝑖𝑗=𝜃𝑖𝜃𝑗𝑝, so the probility of an edge from 𝑖 to 𝑗 is a function of the two nodes' degree-correction parameters, and the overall probability of an edge in the graph''' from graspy.models import DCEREstimator dcer = DCEREstimator(directed=True, loops=False) dcer.fit(adj) # Fit Degree-corrected Erdos-Reyni model promiscuities = dcer.degree_corrections_ # Show results fig, ax = plt.subplots(1, 2) plotHeatmap(dcer.p_mat_, "DCER probability matrix", params={ 'vmin': 0, 'vmax': 1, 'ax': ax[0] }) plotHeatmap(dcer.sample()[0], "DCER sample", params={'ax': ax[1]}) fig.suptitle('Degree-corrected Erdos-Reyni (ER) model', fontsize=16) print(f"DCER \"p\" parameter: {dcer.p_}")
outer_hier_labels=lcc_hemisphere, hier_label_fontsize=10, sort_nodes=True, ) gridplot( [embed_graph], height=15, inner_hier_labels=lcc_simple_classes, outer_hier_labels=lcc_hemisphere, hier_label_fontsize=10, sizes=(1, 10), sort_nodes=True, ) #%% dcer = DCEREstimator() dcer.fit(embed_graph) gridplot( [binarize(embed_graph)], height=15, inner_hier_labels=lcc_simple_classes, outer_hier_labels=lcc_hemisphere, hier_label_fontsize=10, sizes=(5, 5), sort_nodes=True, ) gridplot( [dcer.sample()[0]], height=15, inner_hier_labels=lcc_simple_classes, outer_hier_labels=lcc_hemisphere,
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", sort_nodes=True); plt.savefig("ERSample", bbox_inches='tight') dcer = DCEREstimator(directed=True,loops=False) dcer.fit(adj) print(f"DCER \"p\" parameter: {dcer.p_}") heatmap(dcer.p_mat_, inner_hier_labels=labels, vmin=0, vmax=1, font_scale=1.5, title="DCER probability matrix", sort_nodes=True) plt.savefig("DCERProbabilityMatrix", bbox_inches='tight') heatmap(dcer.sample()[0], inner_hier_labels=labels, font_scale=1.5,
def test_DCER_score(self): p_mat = self.p_mat graph = self.graph estimator = DCEREstimator() _test_score(estimator, p_mat, graph)