Example #1
0
def test_stochastic_blockmodel_graph():
    torch.manual_seed(12345)

    block_sizes = [2, 2, 4]
    edge_probs = [
        [0.25, 0.05, 0.02],
        [0.05, 0.35, 0.07],
        [0.02, 0.07, 0.40],
    ]

    edge_index = stochastic_blockmodel_graph(block_sizes,
                                             edge_probs,
                                             directed=False)
    assert edge_index.tolist() == [
        [2, 3, 4, 4, 5, 5, 6, 7, 7, 7],
        [3, 2, 5, 7, 4, 7, 7, 4, 5, 6],
    ]

    edge_index = stochastic_blockmodel_graph(block_sizes,
                                             edge_probs,
                                             directed=True)
    assert edge_index.tolist() == [
        [0, 1, 3, 5, 6, 6, 7, 7],
        [3, 3, 2, 4, 4, 7, 5, 6],
    ]
Example #2
0
    def process(self):
        from sklearn.datasets import make_classification

        edge_index = stochastic_blockmodel_graph(
            self.block_sizes, self.edge_probs, directed=not self.is_undirected)

        num_samples = int(self.block_sizes.sum())
        num_classes = self.block_sizes.size(0)

        x = None
        if self.num_channels is not None:
            x, _ = make_classification(
                n_samples=num_samples,
                n_features=self.num_channels,
                n_classes=num_classes,
                weights=self.block_sizes / num_samples,
                **self.kwargs,
            )
            x = torch.from_numpy(x).to(torch.float)

        y = torch.arange(num_classes).repeat_interleave(self.block_sizes)

        data = Data(x=x, edge_index=edge_index, y=y)

        if self.pre_transform is not None:
            data = self.pre_transform(data)

        torch.save(self.collate([data]), self.processed_paths[0])
Example #3
0
        for y in range(len(block_sizes)):
            cur_prob = 0.0
            if x == y:
                cur_prob = INTER_PROB[x]
            elif x >= NUM_BLOCKS or y >= NUM_BLOCKS:
                cur_prob = NOISE_PROB
            else:
                if USE_NOISE_NODES:
                    cur_prob = INTRA_PROB
                else:
                    cur_prob = INTRA_PROB + sbm_noise
            cur_probs.append(float(cur_prob))
        edge_probs.append(cur_probs)

    sbm_ei = stochastic_blockmodel_graph(block_sizes,
                                         edge_probs,
                                         directed=False)
    sbm_x = []
    for x in range((COMMUNITY_SIZE * NUM_BLOCKS) + len(block_sizes) -
                   NUM_BLOCKS):
        sbm_x.append([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
    sbm_x = torch.tensor(sbm_x)
    sbm_y = []
    for ind, x in enumerate(block_sizes):
        for y in range(x):
            sbm_y.append(int(ind))
    sbm_y = torch.tensor(sbm_y)
    print(sbm_ei)
    print(sbm_x)
    print(sbm_y)
    real_data.append(