Beispiel #1
0
def test_lil_laplace(dtype, lap_type):
    N = 8
    g = rand_udg(N, dtype=dtype)
    coo_m = g.to_scipy("coo")
    L = laplace(coo_m, lap_type)
    assert L.shape == (N, N)
    assert isinstance(L, coo_matrix)
Beispiel #2
0
def test_single():
    ray.init(num_cpus=3, ignore_reinit_error=True, log_to_driver=False)
    N = 32 * 3
    strategy = "admm"
    graph = rand_udg(N)
    f1 = NumQmf(graph, in_channels=3, zero_dc=True, strategy=strategy)
    print(f1)
Beispiel #3
0
 def test_amfs1level(self, dt):
     N = 100
     delta = 0.1
     W = rand_udg(N, dtype=dt).to_scipy("csr").tolil().astype("d")
     Sigma = compute_sigma(W, delta)
     s1, s2 = amfs1level(W, Sigma, delta)
     print("\n|L|: ", s1)
     print("|H|: ", s2)
Beispiel #4
0
    def test_init(self, dtype, device, strategy, N):
        ray.init(num_cpus=RAY_NUM_CPUS,
                 log_to_driver=False,
                 ignore_reinit_error=True)

        graph = rand_udg(N, dtype=dtype, device=device)
        f1 = NumQmf(graph, in_channels=3, zero_dc=True, strategy=strategy)
        print(f1)
Beispiel #5
0
def test_dsatur(device):
    N = 10
    G = rand_udg(N, 0.2, device=device)
    pos = torch.rand(N, 2)
    vtx_color = dsatur(G)
    assert check_coloring(G, vtx_color)
    assert not check_coloring(G, [0] * N)
    draw_cn(G, pos=pos, node_color=vtx_color)
    plot()
Beispiel #6
0
def test_dsatur_py():
    for _ in range(20):
        N = 13
        G = rand_udg(N, 0.3)
        pos = torch.rand(N, 2)
        vtx_color = dsatur_py(G)
        assert check_coloring(G, vtx_color)
        assert not check_coloring(G, [0] * N)
        draw_cn(G, pos=pos, node_color=vtx_color)
    plot()
Beispiel #7
0
def test_admm_bga(density, M, dtype):
    N = 32
    G = rand_udg(N, density, dtype)
    bptGs = admm_bga(G.to_dense(), M=M)
    for i in range(M):
        # use is_bipartite_fix() is recommended to ensure the bipartiteness
        assert is_bipartite_fix(bptGs[i], fix_flag=True)[0]

    for i in range(M):  # Check the bipartiteness
        assert is_bipartite_fix(bptGs[i], fix_flag=False)[0]
Beispiel #8
0
    def test_bipartite_fix_th(self):
        N1, N2 = 4, 6
        N = N1 + N2
        B = rand_udg(N, 0.6)
        flag, vtx_color, Bb = is_bipartite_fix(B.to_dense(), fix_flag=False)
        assert not flag  # if fix_flag, always bipartite

        B = rand_bipartite(N1, N2, 0.6)
        flag, vtx_color, _ = is_bipartite_fix(B.to_dense(), fix_flag=False)
        assert flag
Beispiel #9
0
    def test_multi_level(self, dtype, device):
        N = 16
        G = rand_udg(N, 0.2, dtype, device)
        bptG, beta, _, _, _ = harary(G)

        basis = QmfOperator(bptG, beta, order=4, device=device)
        x = torch.ones(N, 1, dtype=dtype, device=device)
        y = basis(x)
        z = basis.inverse_transform(y)
        print("\nsnr: ",
              snr(z.permute(-1, -2), x.permute(-1, -2)).item(), "dB.")
        print("dis: ", (z - x).abs().sum())
Beispiel #10
0
def test_harary():
    for _ in range(30):
        N = 20
        G = rand_udg(N, 0.3)
        print("\nA: ", G.sum())
        try:
            bptG, beta, beta_dist, colors, _ = harary(G)
            for i in range(len(bptG)):
                assert is_bipartite_fix(bptG[i])
                print("bptG[{}]: {} ".format(i, bptG[i].sum()))
        except RuntimeError as err:
            raise err
Beispiel #11
0
 def test_bipartite(self):
     n_sample = 7
     N1, N2 = 4, 6
     N = N1 + N2
     for i in range(n_sample):
         B = rand_bipartite(N1, N2)
         flag, vtx_color, _ = is_bipartite_fix(B.to_scipy("csr"),
                                               fix_flag=False)
         assert flag
         NB = rand_udg(N, 0.8)  # complete graph must be non-bipartite
         flag, vtx_color, _ = is_bipartite_fix(NB.to_scipy("csr"),
                                               fix_flag=False)
         assert not flag
Beispiel #12
0
    def test_multi_level(self, dtype, device):
        N = 120
        G = rand_udg(N, 0.4, dtype, device)
        bptG, beta, _, _, _ = harary(G)

        basis = BiorthOperator(bptG, beta, k=2, device=device)
        x = torch.ones(N, 1, dtype=dtype, device=device)
        y = basis.transform(x)
        z = basis.inverse_transform(y)
        print("\nsnr: ",
              snr(z.permute(-1, -2), x.permute(-1, -2)).item(), "dB.")
        print("dis: ", (z - x).abs().sum())
        self.display_density(basis.operator, basis.inv_operator)
Beispiel #13
0
    def test_transform(self, device, dtype):
        M, N = 2, 7
        Bs = [rand_udg(N, dtype=dtype, device=device) for _ in range(M)]
        beta = torch.rand(N, M, device=device) > 0.5

        bio = BiorthCore(Bs, beta)
        x = torch.rand(N, dtype=bio.dtype, device=bio.device)
        y = bio.analyze(x)
        assert y.shape == (2**M, N, 1)

        z = bio.synthesize(y)
        assert z.shape == (2**M, N, 1)
        # since beta and Bs are all randomly generated, the transform won't be
        # numerically valid
        assert (z.sum(0).squeeze() - x).abs().sum() != 0
Beispiel #14
0
 def test_transform(self, dtype, device, strategy, M, N):
     ray.init(num_cpus=2, log_to_driver=False, ignore_reinit_error=True)
     graph = rand_udg(N, device=device, dtype=dtype)
     qmf = NumQmf(graph, strategy=strategy, level=M)
     f = torch.rand(N, device=device, dtype=dtype)
     y = qmf.analyze(f)
     z = qmf.synthesize(y)
     z.squeeze_()
     f_hat = z.sum(0)
     dis = (f_hat - f).abs()
     pf = snr(f_hat, f).item()
     print(f"\n|----- Strategy: {strategy:8s}, M={M}, "
           f"Device: {str(device):5s}, Dtype: {str(dtype):6s} -----")
     ppprint(dis, pf)
     assert pf > 20
Beispiel #15
0
    def test_amfs(self, dt):
        from thgsp.bga._utils import is_bipartite_fix

        N = 100
        M = 2
        W = rand_udg(N, dtype=dt)
        bptG, beta = amfs(W, level=M)
        weights = []
        for i in range(len(bptG)):
            assert is_bipartite_fix(bptG[i])[0]
            weights.append(bptG[i].sum())
        assert beta.shape == (N, M)
        assert len(bptG) == M
        print("bptG  weights: ", weights)
        print("total weights: ", sum(weights))
        assert (sum(weights) - W.sum()).sum() < 1e-6
Beispiel #16
0
 def test_transform(self, dtype, device, strategy, Ci):
     N = 60
     graph = rand_udg(N, device=device, dtype=dtype)
     qmf = ColorQmf(graph, strategy=strategy, in_channels=1)
     M = qmf.num_bgraph
     f = torch.rand(N, Ci, device=device, dtype=dtype)
     y = qmf.analyze(f)
     z = qmf.synthesize(y)
     z.squeeze_()
     f_hat = z.sum(0)
     dis = (f_hat - f).abs()
     pf = snr(f_hat, f.squeeze_()).item()
     print(f"\n|-- Strategy: {strategy:8s}, M={M}, "
           f"Device: {str(device):5s}, Dtype: {str(dtype):6s} ---")
     ppprint(dis, pf)
     assert pf > 20
Beispiel #17
0
    def test_init(self, dtype, device):
        M, N = 3, 7
        K = 12
        k = 3
        Bs = [rand_udg(N, dtype=dtype, device=device) for _ in range(M)]
        beta_np = np.random.rand(N, M) > 0.3

        bio = BiorthCore(Bs, beta_np, k=k, order=K)
        assert bio.coefficient_a.shape == (M, 2**M, 1, K + 1)
        assert bio.coefficient_a.shape == bio.coefficient_s.shape

        bio = BiorthCore(Bs,
                         torch.as_tensor(beta_np),
                         in_channels=3,
                         zero_dc=True)
        # 16 is the default order of approximation
        assert bio.coefficient_a.shape == (M, 2**M, 3, 16 + 1)
Beispiel #18
0
    def test_transform(self, dtype, device, strategy, zero_dc):
        N = 100
        graph = rand_udg(N, device=device, dtype=dtype)
        bio = ColorBiorth(graph, strategy=strategy)
        M = bio.num_bgraph

        f = torch.rand(N, device=bio.device, dtype=bio.dtype)
        y = bio.analyze(f)
        z = bio.synthesize(y)
        z.squeeze_()
        f_hat = z.sum(0)
        dis = (f_hat - f).abs()
        pf = snr(f_hat, f).item()
        print(f"\n|----- Strategy: {strategy:8s}, M:{M:4d}  "
              f"Device: {str(device):5s}, Dtype: {str(dtype):6s}")
        ppprint(dis, pf)
        assert pf > 20
Beispiel #19
0
 def test_admm_lbga_ray(self, density, style, M, dtype, device, part):
     ray.init(num_cpus=RAY_NUM_CPUS,
              ignore_reinit_error=True,
              log_to_driver=False)
     N = 32 * 3 + 10
     G = rand_udg(N, density, dtype=dtype, device=device)
     bptGs, beta, partptr, perm = admm_lbga_ray(G,
                                                M,
                                                block_size=32,
                                                style=style,
                                                part=part)
     print(
         f"\n---num_node: {N}, density: {density}, strategy: {style}, M: {M},"
         f" dtype:{dtype}, device:{device}, part:{part}")
     print("total weights: {}".format(G.sum().item()))
     for i, bptG in enumerate(bptGs):
         assert is_bipartite_fix(bptG)[0]
         print("{}-th subgraph, weights: {}".format(i, bptG.sum()))
Beispiel #20
0
    def test_bipartite_fix(self):
        N = 12
        B = rand_udg(N, 0.3)
        flag, vtx_color, Bb = is_bipartite_fix(B.to_scipy("csr"),
                                               fix_flag=True)
        assert flag  # if fix_flag, always bipartite
        assert is_bipartite_fix(Bb)[0]

        print("the last one\n", B)
        # visual
        import matplotlib.pyplot as plt

        from thgsp.visual import draw, draw_cn

        pos = torch.rand(N, 2)
        _, axes = plt.subplots(1, 2)
        draw(B, pos, ax=axes[0])
        draw_cn(Graph(Bb), pos, vtx_color, ax=axes[1])
        plot()
Beispiel #21
0
    def test_init(self, device, dtype):
        M, N = 2, 7
        K = 10
        Bs = [rand_udg(N, dtype=dtype, device=device) for _ in range(M)]
        beta_np = np.random.rand(N, M) > 0.5

        qmf = QmfCore(bipartite_graphs=Bs, beta=beta_np, order=K)
        assert qmf.coefficient_a.shape == (M, 2**M, 1, K + 1)
        assert qmf.coefficient_a.shape == qmf.coefficient_s.shape

        QmfCore(
            Bs,
            torch.as_tensor(beta_np),
            analyze_kernels=(meyer_kernel, meyer_mirror_kernel),
        )

        qmf = QmfCore(Bs, beta_np, in_channels=3)
        # 20 is the default order of approximation
        assert qmf.coefficient_a.shape == (M, 2**M, 3, qmf.order + 1)
Beispiel #22
0
    def test_transform(self, dtype, device, strategy, M, part):
        ray.init(num_cpus=2, log_to_driver=False, ignore_reinit_error=True)
        N = 32 * 3
        print(f"\n|----- Strategy: {strategy:8s}, M={M}, "
              f"Device: {str(device):5s}, Dtype: {str(dtype):6s} ----")
        kwargs = dict()
        if strategy == "admm":
            print("|admm-lbga part strategy: {}".format(part))
            kwargs["part"] = part

        graph = rand_udg(N, device=device, dtype=dtype)
        bio = NumBiorth(graph, strategy=strategy, level=M, **kwargs)
        f = torch.randn(N, device=device, dtype=dtype)
        y = bio.analyze(f)
        z = bio.synthesize(y)
        z.squeeze_()
        f_hat = z.sum(0)
        dis = (f_hat - f).abs()
        pf = snr(f_hat, f).item()
        ppprint(dis, pf)
        assert pf > 20
Beispiel #23
0
def test_osglm():
    N = 40
    G = rand_udg(N, 0.3)

    bptG, beta, append_nodes, vtx_color = osglm(G)
    assert is_bipartite_fix(bptG[0])

    try:
        import torch as th
        from torgsp.sampling import osglm as dense_osglm
        from torgsp.types import ColorG

        dense_bptG, dense_beta, dense_append_nodes, _ = dense_osglm(
            G.to_dense(), colorg=ColorG(vtx_color, max(vtx_color) + 1)
        )

        print("      beta: ", beta[:, -1])
        print("dense beta: ", dense_beta.numpy()[:, -1])

        assert (th.as_tensor(bptG[0].toarray()) - dense_bptG[0]).abs().sum() == 0

    except ImportError as err:
        print(err)
Beispiel #24
0
 def test_rand_udg(self, device, dtype, density):
     N = 12
     G = rand_udg(N, density, dtype, device)
     assert G.dtype() == dtype
     assert G.density() - density < 2 / N * (N - 1)
Beispiel #25
0
def test_is_bipartite1(device):
    ts_spm = rand_udg(50, device=device)
    assert not is_bipartite(ts_spm)[0]
Beispiel #26
0
 def test_init(self, dtype, device, strategy):
     N = 12
     graph = rand_udg(N, dtype=dtype, device=device)
     ColorBiorth(graph)
     ColorBiorth(graph)
     ColorBiorth(graph, in_channels=3, strategy=strategy, zero_dc=True)
Beispiel #27
0
 def test_compute_sigma(self, p, dt):
     N = 100
     delta = 0.1
     Acoo = rand_udg(N, p, dt).to_scipy("coo")
     Sigma = compute_sigma(Acoo, delta)
     assert Sigma.shape == (N, N)
Beispiel #28
0
 def test_init(self, dtype, device, strategy, N):
     ray.init(num_cpus=2, log_to_driver=False, ignore_reinit_error=True)
     graph = rand_udg(N, dtype=dtype, device=device)
     NumBiorth(graph)
     NumBiorth(graph, in_channels=3, zero_dc=True, strategy=strategy)