Ejemplo n.º 1
0
 def test_RDPG_nparams(self):
     n_verts = 1000
     g = self.graph
     e = RDPGEstimator(n_components=2)
     e.fit(g)
     assert e._n_parameters() == n_verts * 2
     g[100:, 50:] = 1
     e = RDPGEstimator(n_components=2)
     e.fit(g)
     assert e._n_parameters() == n_verts * 4
Ejemplo n.º 2
0
 def test_RDPG_sample(self):
     np.random.seed(8888)
     g = self.graph
     p_mat = self.p_mat
     estimator = RDPGEstimator(n_components=2)
     estimator.fit(g)
     _test_sample(estimator, p_mat, atol=0.2, n_samples=200)
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
def estimate_rdpg(graph, n_components=None):
    estimator = RDPGEstimator(loops=False, n_components=n_components)
    estimator.fit(graph)
    if n_components is None:
        n_components = estimator.latent_.shape[0]
    # n_params = graph.shape[0] * n_components
    n_params = estimator._n_parameters()
    return estimator, n_params
Ejemplo n.º 5
0
    def test_RDPG_score(self):
        p_mat = self.p_mat
        graph = self.graph
        estimator = RDPGEstimator()
        _test_score(estimator, p_mat, graph)

        with pytest.raises(ValueError):
            estimator.score_samples(graph=graph[1:100, 1:100])
Ejemplo n.º 6
0
    def test_RDPG_intputs(self):
        rdpge = RDPGEstimator()

        with pytest.raises(TypeError):
            RDPGEstimator(loops=6)

        with pytest.raises(ValueError):
            rdpge.fit(self.graph[:, :99])

        with pytest.raises(ValueError):
            rdpge.fit(self.graph[..., np.newaxis])

        with pytest.raises(TypeError):
            RDPGEstimator(ase_kws=5)

        with pytest.raises(TypeError):
            RDPGEstimator(diag_aug_weight="f")

        with pytest.raises(ValueError):
            RDPGEstimator(diag_aug_weight=-1)

        with pytest.raises(TypeError):
            RDPGEstimator(plus_c_weight="F")

        with pytest.raises(ValueError):
            RDPGEstimator(plus_c_weight=-1)
Ejemplo n.º 7
0
    def test_RDPG_fit(self):
        np.random.seed(8888)
        n_points = 2000
        dists = np.random.uniform(0, 1, n_points)
        points = hardy_weinberg(dists)

        p_mat = points @ points.T
        p_mat -= np.diag(np.diag(p_mat))
        g = sample_edges(p_mat)

        estimator = RDPGEstimator(loops=False, n_components=3)
        estimator.fit(g)

        assert_allclose(estimator.p_mat_, p_mat, atol=0.2)
Ejemplo n.º 8
0
def select_rdpg(graph, param_grid, directed=True, n_jobs=1):
    if is_symmetric(graph) and directed:
        msg = ("select_RDPG was given an undirected graph but you wanted" +
               " a directed model")
        raise ValueError(msg)

    rdpg = RDPGEstimator(loops=False)

    # # define scoring functions to evaluate models
    scorers = gen_scorers(rdpg, graph)

    # run the grid search
    grid_search = GridSearchUS(rdpg,
                               param_grid,
                               scoring=scorers,
                               n_jobs=n_jobs,
                               verbose=0)
    grid_search.fit(graph)

    return grid_search.cv_results_
Ejemplo n.º 9
0
from graspy.models import DCSBMEstimator, RDPGEstimator, SBMEstimator
from graspy.plot import heatmap
from src.data import load_right

# Load data
right_adj, right_labels = load_right()

# Fit the models
sbm = SBMEstimator(directed=True, loops=False)
sbm.fit(right_adj, y=right_labels)

dcsbm = DCSBMEstimator(degree_directed=False, directed=True, loops=False)
dcsbm.fit(right_adj, y=right_labels)

rdpg = RDPGEstimator(loops=False, n_components=3)
rdpg.fit(right_adj)

# Plotting
np.random.seed(8888)

cmap = mpl.cm.get_cmap("RdBu_r")
center = 0
vmin = 0
vmax = 1
norm = mpl.colors.Normalize(0, 1)
cc = np.linspace(0.5, 1, 256)
cmap = mpl.colors.ListedColormap(cmap(cc))

heatmap_kws = dict(
    cbar=False,
Ejemplo n.º 10
0
        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",
        sort_nodes=True
       )

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

heatmap(rdpge.sample()[0],
        inner_hier_labels=labels,
        font_scale=1.5,
Ejemplo n.º 11
0
 def test_RDPG_score(self):
     p_mat = self.p_mat
     graph = self.graph
     estimator = RDPGEstimator()
     _test_score(estimator, p_mat, graph)