Beispiel #1
0
    def test_loop_undirected(self):
        np.random.seed(12345)
        Abin = er_np(self.n, self.p, loops=True)
        Awt = er_np(self.n, self.p, loops=True, wt=self.wt, wtargs=self.wtargs)
        # check that correct number of edges assigned
        self.assertTrue(
            np.isclose(Abin.sum() / float(np.prod(Abin.shape)), self.p, atol=0.02)
        )
        self.assertTrue(
            np.isclose((Awt != 0).sum() / float(np.prod(Awt.shape)), self.p, atol=0.02)
        )
        # check that the nonzero edges have mean self.mean and var self.var
        self.assertTrue(np.isclose(np.mean(Awt[Awt != 0]), self.mean, atol=0.15))
        self.assertTrue(np.isclose(np.std(Awt[Awt != 0]), self.std, atol=0.15))

        # check loopless and undirected
        self.assertTrue(is_symmetric(Abin))
        self.assertTrue(is_symmetric(Awt))
        self.assertFalse(is_loopless(Abin))
        self.assertFalse(is_loopless(Awt))

        # check dimensions
        self.assertTrue(Abin.shape == (self.n, self.n))
        self.assertTrue(Awt.shape == (self.n, self.n))
        pass
Beispiel #2
0
    def test_loop_directed(self):
        np.random.seed(12345)
        Abin = er_nm(self.n, self.M, directed=True, loops=True)
        Awt = er_nm(
            self.n, self.M, directed=True, loops=True, wt=self.wt, wtargs=self.wtargs
        )
        # check that correct number of edges assigned
        # sum of nonzero entries and correct for the fact that the diagonal
        # is part of the model now
        self.assertTrue(Abin.sum() == self.M)
        self.assertTrue((Awt != 0).sum() == self.M)

        # check that the nonzero edges have mean self.mean and var self.var
        self.assertTrue(np.isclose(np.mean(Awt[Awt != 0]), self.mean, atol=0.2))
        self.assertTrue(np.isclose(np.std(Awt[Awt != 0]), self.std, atol=0.2))

        # check loopless and undirected
        self.assertFalse(is_symmetric(Abin))
        self.assertFalse(is_symmetric(Awt))
        self.assertFalse(is_loopless(Abin))
        self.assertFalse(is_loopless(Awt))

        # check dimensions
        self.assertTrue(Abin.shape == (self.n, self.n))
        self.assertTrue(Awt.shape == (self.n, self.n))
        pass
Beispiel #3
0
    def test_noloop_undirected(self):
        np.random.seed(123)
        Abin = er_np(self.n, self.p)
        Awt = er_np(self.n, self.p, wt=self.wt, wtargs=self.wtargs)
        # check that correct number of edges assigned
        dind = remove_diagonal(Abin)
        dindwt = remove_diagonal(Awt)
        self.assertTrue(np.isclose(dind.sum() / float(len(dind)), self.p, atol=0.02))
        self.assertTrue(
            np.isclose((dindwt != 0).sum() / float(len(dindwt)), self.p, atol=0.02)
        )

        # check that the nonzero edges have mean self.mean and var self.var
        self.assertTrue(np.isclose(np.mean(dindwt[dindwt != 0]), self.mean, atol=0.15))
        self.assertTrue(np.isclose(np.std(dindwt[dindwt != 0]), self.std, atol=0.15))

        # check loopless and undirected
        self.assertTrue(is_symmetric(Abin))
        self.assertTrue(is_symmetric(Awt))
        self.assertTrue(is_loopless(Abin))
        self.assertTrue(is_loopless(Awt))

        # check dimensions
        self.assertTrue(Abin.shape == (self.n, self.n))
        self.assertTrue(Awt.shape == (self.n, self.n))
Beispiel #4
0
    def test_noloop_directed(self):
        np.random.seed(12345)
        Abin = er_nm(self.n, self.M, directed=True)
        Awt = er_nm(self.n,
                    self.M,
                    wt=self.wt,
                    directed=True,
                    wtargs=self.wtargs)
        # check that correct number of edges assigned
        self.assertTrue(Abin.sum() == self.M)
        self.assertTrue((Awt != 0).sum() == self.M)

        dind = remove_diagonal(Awt)
        # check that the nonzero edges have mean self.mean and var self.var
        self.assertTrue(
            np.isclose(np.mean(dind[dind != 0]), self.mean, atol=0.15))
        self.assertTrue(
            np.isclose(np.std(dind[dind != 0]), self.std, atol=0.15))

        # check loopless and undirected
        self.assertFalse(is_symmetric(Abin))
        self.assertFalse(is_symmetric(Awt))
        self.assertTrue(is_loopless(Abin))
        self.assertTrue(is_loopless(Awt))

        # check dimensions
        self.assertTrue(Abin.shape == (self.n, self.n))
        self.assertTrue(Awt.shape == (self.n, self.n))
        pass
Beispiel #5
0
 def test_kwarg_passing(self):
     np.random.seed(8888)
     X = 0.5 * np.ones((300, 2))
     g = rdpg(X, rescale=True, loops=True, directed=True)
     self.assertFalse(is_symmetric(g))
     self.assertFalse(is_loopless(g))
     g = rdpg(X, rescale=True, loops=False, directed=True)
     self.assertFalse(is_symmetric(g))
     self.assertTrue(is_loopless(g))
     g = rdpg(X, rescale=True, loops=False, directed=False)
     self.assertTrue(is_symmetric(g))
     self.assertTrue(is_loopless(g))
Beispiel #6
0
 def test_sbm_dc_dc_kws_directed_loopy(self):
     np.random.seed(self.seed)
     funcs = [np.random.power, np.random.uniform]
     dc_kwss = [{"a": 3}, {"low": 5, "high": 10}]
     for i in range(len(funcs)):
         A = sbm(
             self.n,
             self.Psy,
             directed=True,
             loops=True,
             dc=funcs[i],
             dc_kws=dc_kwss[i],
         )
         for i in range(0, len(self.n)):
             for j in range(0, len(self.n)):
                 block = A[
                     (self.vcount[i] - self.n[i]) : self.vcount[i],
                     (self.vcount[j] - self.n[j]) : self.vcount[j],
                 ]
                 if i == j:
                     block = remove_diagonal(block)
                 self.assertTrue(
                     np.isclose(np.mean(block), self.Psy[i, j], atol=0.02)
                 )
         self.assertFalse(is_symmetric(A))
         self.assertFalse(is_loopless(A))
         # check dimensions
         self.assertTrue(A.shape == (np.sum(self.n), np.sum(self.n)))
     pass
Beispiel #7
0
    def test_sbm_multiwt_undirected_loopy(self):
        np.random.seed(12345)
        Wt = np.vstack(
            (
                [np.random.normal, np.random.poisson],
                [np.random.poisson, np.random.uniform],
            )
        )
        Wtargs = np.vstack(
            ([{"loc": 2, "scale": 2}, {"lam": 5}], [{"lam": 5}, {"low": 5, "high": 10}])
        )
        check = np.vstack(
            ([self.exp_normal, self.exp_poisson], [self.exp_poisson, self.exp_unif])
        )
        A = sbm(self.n, self.Psy, wt=Wt, directed=False, loops=True, wtargs=Wtargs)
        for i in range(0, len(self.n)):
            for j in range(0, len(self.n)):
                irange = np.arange(self.vcount[i] - self.n[i], self.vcount[i])
                jrange = np.arange(self.vcount[j] - self.n[j], self.vcount[j])

                block = A[
                    (self.vcount[i] - self.n[i]) : self.vcount[i],
                    (self.vcount[j] - self.n[j]) : self.vcount[j],
                ]
                self.assertTrue(
                    np.isclose(np.mean(block != 0), self.Psy[i, j], atol=0.02)
                )
                fit = check[i, j](block[block != 0])
                for k, v in fit.items():
                    self.assertTrue(np.isclose(v, Wtargs[i, j][k], atol=0.2))
        self.assertTrue(is_symmetric(A))
        self.assertFalse(is_loopless(A))
        # check dimensions
        self.assertTrue(A.shape == (np.sum(self.n), np.sum(self.n)))
        pass
Beispiel #8
0
    def test_sbm_singlewt_undirected_loopless(self):
        np.random.seed(12345)
        wt = np.random.normal
        params = {"loc": 2, "scale": 2}
        A = sbm(self.n, self.Psy, wt=wt, wtargs=params)
        for i in range(0, len(self.n)):
            for j in range(0, len(self.n)):
                irange = np.arange(self.vcount[i] - self.n[i], self.vcount[i])
                jrange = np.arange(self.vcount[j] - self.n[j], self.vcount[j])

                block = A[
                    (self.vcount[i] - self.n[i]) : self.vcount[i],
                    (self.vcount[j] - self.n[j]) : self.vcount[j],
                ]
                if i == j:
                    block = remove_diagonal(block)
                self.assertTrue(
                    np.isclose(np.mean(block != 0), self.Psy[i, j], atol=0.02)
                )
                self.assertTrue(
                    np.isclose(np.mean(block[block != 0]), params["loc"], atol=0.2)
                )
                self.assertTrue(
                    np.isclose(np.std(block[block != 0]), params["scale"], atol=0.2)
                )
        self.assertTrue(is_symmetric(A))
        self.assertTrue(is_loopless(A))
        # check dimensions
        self.assertTrue(A.shape == (np.sum(self.n), np.sum(self.n)))
Beispiel #9
0
 def test_is_almost_symmetric(self):
     np.random.seed(8888)
     vec1 = np.random.normal(0, 1, (100, 100))
     vec2 = np.random.normal(0, 1, (100, 100))
     corr = np.corrcoef(vec1, vec2)
     self.assertTrue(gus.is_almost_symmetric(corr, atol=1e-15))
     self.assertFalse(gus.is_symmetric(corr))
Beispiel #10
0
def test_omni_matrix_symmetric():
    np.random.seed(3)
    n = 15
    p = 0.4

    n_graphs = [2, 5, 10]
    for n in n_graphs:
        graphs = [er_np(n, p) for _ in range(n)]
        output = _get_omni_matrix(graphs)
        assert is_symmetric(output)
Beispiel #11
0
    def test_loop_directed(self):
        rng = np.random.default_rng(self.seed)
        A = mmsbm(self.n, self.p, self.alpha, rng=rng, directed=True, loops=True)

        # check loops and directed
        self.assertFalse(is_symmetric(A))
        self.assertFalse(is_loopless(A))

        # check dimensions
        self.assertTrue(A.shape == (self.n, self.n))
        pass
Beispiel #12
0
    def test_noloop_undirected(self):
        rng = np.random.default_rng(self.seed)
        # Test that when loops = False and directed = False the output is undirected
        # and with no loops
        A = mmsbm(self.n, self.p, self.alpha, rng=rng, directed=False, loops=False)

        # check loopless and undirected
        self.assertTrue(is_symmetric(A))
        self.assertTrue(is_loopless(A))

        # check dimensions
        self.assertTrue(A.shape == (self.n, self.n))
        pass
Beispiel #13
0
 def test_sbm_multi_dc_empty_dc_kws(self):
     np.random.seed(self.seed)
     dc = [np.random.rayleigh, np.random.uniform]
     A = sbm(self.n, self.Psy, directed=True, loops=True, dc=dc)
     for i in range(0, len(self.n)):
         for j in range(0, len(self.n)):
             block = A[(self.vcount[i] - self.n[i]):self.vcount[i],
                       (self.vcount[j] - self.n[j]):self.vcount[j], ]
             if i == j:
                 block = remove_diagonal(block)
             self.assertTrue(
                 np.isclose(np.mean(block), self.Psy[i, j], atol=0.02))
     self.assertFalse(is_symmetric(A))
     self.assertFalse(is_loopless(A))
     # check dimensions
     self.assertTrue(A.shape == (np.sum(self.n), np.sum(self.n)))
     pass
Beispiel #14
0
    def test_sbm(self):
        n = [50, 60, 70]
        vcount = np.cumsum(n)
        # define symmetric probability as evenly weighted
        Psy = np.vstack(([0.6, 0.2, 0.3], [0.3, 0.4, 0.2], [0.2, 0.8, 0.1]))
        Psy = symmetrize(Psy)
        np.random.seed(12345)
        A = sbm(n, Psy)
        for i in range(0, len(n)):
            for j in range(0, len(n)):
                irange = np.arange(vcount[i] - n[i], vcount[i])
                jrange = np.arange(vcount[j] - n[j], vcount[j])

                block = A[
                    (vcount[i] - n[i]) : vcount[i], (vcount[j] - n[j]) : vcount[j]
                ]
                if i == j:
                    block = remove_diagonal(block)
                self.assertTrue(np.isclose(np.mean(block), Psy[i, j], atol=0.02))
        self.assertTrue(is_symmetric(A))
        self.assertTrue(is_loopless(A))
        # check dimensions
        self.assertTrue(A.shape == (np.sum(n), np.sum(n)))
        pass
Beispiel #15
0
    def test_to_laplace_symmetric(self):
        L_normed = gus.to_laplace(self.A, form="DAD")

        self.assertTrue(gus.is_symmetric(L_normed))