Esempio n. 1
0
    def test_DCSBM_sample(self):
        np.random.seed(8888)
        estimator = DCSBMEstimator(directed=True, loops=False)
        B = np.array([[0.9, 0.1], [0.1, 0.9]])
        dc = np.random.uniform(0.25, 0.75, size=100)
        labels = _n_to_labels([50, 50])

        p_mat = _block_to_full(B, labels, (100, 100))
        p_mat = p_mat * np.outer(dc, dc)
        p_mat -= np.diag(np.diag(p_mat))
        g = sample_edges(p_mat, directed=True)

        with pytest.raises(NotFittedError):
            estimator.sample()

        estimator.fit(g, y=labels)
        with pytest.raises(ValueError):
            estimator.sample(n_samples=-1)

        with pytest.raises(TypeError):
            estimator.sample(n_samples="nope")
        estimator.p_mat_ = p_mat
        _test_sample(estimator, p_mat, n_samples=1000, atol=0.1)
Esempio n. 2
0
    adj,
    meta=meta,
    sort_class=["hemisphere", "lvl0_labels"],
    colors="merge_class",
    palette=CLASS_COLOR_DICT,
    class_order=["signal_flow"],
    item_order=["te"],
    plot_type="scattermap",
    sizes=(0.5, 0.5),
    ax=ax,
    ticks=False,
)

estimator = DCSBMEstimator(degree_directed=True, directed=True, loops=False)
estimator.fit(adj, meta["lvl0_labels"].values)
sample = np.squeeze(estimator.sample())
ax = axs[0]
adjplot(
    sample,
    meta=meta,
    sort_class=["hemisphere", "lvl0_labels"],
    colors="merge_class",
    palette=CLASS_COLOR_DICT,
    class_order=["signal_flow"],
    item_order=["te"],
    plot_type="scattermap",
    sizes=(0.5, 0.5),
    ax=ax,
    ticks=False,
)
Esempio n. 3
0
pairplot(embed, labels=pred_labels, palette=cc.glasbey_light)

# %% [markdown]
# ##

sbm = DCSBMEstimator(directed=True,
                     degree_directed=True,
                     loops=False,
                     max_comm=30)
sbm.fit(binarize(adj))
pred_labels = sbm.vertex_assignments_
print(len(np.unique(pred_labels)))

meta["pred_labels"] = pred_labels

graph = np.squeeze(sbm.sample())

meta["adj_sf"] = -signal_flow(binarize(adj))

block_sf = -signal_flow(sbm.block_p_)
block_map = pd.Series(data=block_sf)
meta["block_sf"] = meta["pred_labels"].map(block_map)

#%%
graph_type = "G"
fig, axs = plt.subplots(1, 2, figsize=(20, 10))
ax = axs[0]
ax, _, tax, _ = matrixplot(
    binarize(adj),
    ax=ax,
    plot_type="scattermap",
Esempio n. 4
0
)
side_label_kws = dict(labelpad=45, fontsize=24)

fig, ax = plt.subplots(3, 2, figsize=(10, 17))

# SBM
heatmap(sbm.p_mat_, ax=ax[0, 0], title="Probability matrix", **heatmap_kws)
heatmap(np.squeeze(sbm.sample()),
        ax=ax[0, 1],
        title="Random sample",
        **heatmap_kws)
ax[0, 0].set_ylabel("SBM", **side_label_kws)

# DCSBM
heatmap(dcsbm.p_mat_, ax=ax[1, 0], **heatmap_kws)
heatmap(np.squeeze(dcsbm.sample()), ax=ax[1, 1], **heatmap_kws)
ax[1, 0].set_ylabel("DCSBM", **side_label_kws)

# RDPG
heatmap(rdpg.p_mat_, ax=ax[2, 0], **heatmap_kws)
heatmap(np.squeeze(rdpg.sample()), ax=ax[2, 1], **heatmap_kws)
ax[2, 0].set_ylabel("RDPG", **side_label_kws)

plt.tight_layout()

# Add colorbar
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array(rdpg.p_mat_)
cbar = fig.colorbar(sm,
                    ax=ax,
                    orientation="horizontal",
Esempio n. 5
0
dcsbme = DCSBMEstimator(directed=True,loops=False)
dcsbme.fit(adj, y=labels)
print("DCSBM \"B\" matrix:")
print(dcsbme.block_p_)
heatmap(dcsbme.p_mat_,
        inner_hier_labels=labels,
        font_scale=1.5,
        title="DCSBM probability matrix",
        vmin=0,
        vmax=1,
        sort_nodes=True)

plt.savefig("DCSBMProbabilityMatrix", bbox_inches='tight')

heatmap(dcsbme.sample()[0],
        inner_hier_labels=labels,
        title="DCSBM sample",
        font_scale=1.5,
        sort_nodes=True)

plt.savefig("DCSBMSample", bbox_inches='tight')

rdpge = RDPGEstimator(loops=False)
rdpge.fit(adj, y=labels)
heatmap(rdpge.p_mat_,
        inner_hier_labels=labels,
        vmin=0,
        vmax=1,
        font_scale=1.5,
        title="RDPG probability matrix",