Exemple #1
0
def build_within_group_matrix(graph,
                              group_node_list,
                              group_tag,
                              precompute=False,
                              labels_as_ids=False):
    result_mat = []
    adj_mat = nx.adjacency_matrix(graph).todense()

    global_node_list = list(graph.nodes())
    if labels_as_ids:
        global_node_list = [graph.node[n]['label'] for n in global_node_list]

    indices = [global_node_list.index(n) for n in group_node_list]

    result_mat = adj_mat[indices, :]
    result_mat = result_mat[:, indices]

    if precompute:
        entity = EntityNet.from_raw_matrix(scipy.sparse.csr_matrix(result_mat),
                                           group_tag, group_node_list)
    else:
        entity = EntityNet(scipy.sparse.csr_matrix(result_mat), group_tag,
                           group_node_list)

    return entity
Exemple #2
0
    def test_entity_net_is_sparse_returns_false_for_numpy_matrix(self):
        e = EntityNet(self.net_a,
                      'Net',
                      self.node_names,
                      precomputed=self.net_a_precomp)

        self.assertFalse(e.is_sparse())
Exemple #3
0
    def test_entity_net_is_sparse_returns_true_for_csr_matrix(self):
        sp = sparse.csr_matrix(self.net_a)
        e = EntityNet(sp,
                      'Net',
                      self.node_names,
                      precomputed=self.net_a_precomp)

        self.assertTrue(e.is_sparse())
Exemple #4
0
    def _create_good_graphdataset(self):
        ent_a = EntityNet(self.net_a, "net_a", self.node_names,
                          self.net_a_precomp)
        ent_b = EntityNet(self.net_b, "net_b", self.node_names_b,
                          self.net_b_precomp)
        rel = RelationNet.from_raw_matrix(self.rel_ab, "rel_ab")
        connections = np.matrix([[-1, 0], [-1, -1]])

        dataset = GraphDataSet([ent_a, ent_b], [rel], connections)
        return dataset
Exemple #5
0
    def test_subset_nodes_precompute_calls_precompute(self, mock_entity_init,
                                                      mock_precompute_matrix):
        a = EntityNet(self.net_a,
                      self.name,
                      self.node_names,
                      precomputed=self.net_a_precomp)

        subset = a.subset([1, 2], precompute=True)
        mock_precompute_matrix.assert_called()
        mock_entity_init.assert_called()
Exemple #6
0
    def test_subset_nodes(self):
        a = EntityNet(self.net_a,
                      self.name,
                      self.node_names,
                      precomputed=self.net_a_precomp)

        subset = a.subset([1, 2], precompute=False)

        self.assertEqual(subset.matrix.shape, (2, 2))
        self.assertEqual(subset.node_names, ['a1', 'a2'])
        self.assertEqual(subset.matrix[0, 0], 0.00)
        self.assertEqual(subset.matrix[0, 1], 0.00)
        self.assertEqual(subset.matrix[1, 0], 0.00)
        self.assertEqual(subset.matrix[1, 1], 0.00)
Exemple #7
0
    def test_graphdataset_densify_generates_dense_matrices(self):
        ent_a = EntityNet(self.net_a, "net_a", self.node_names,
                          self.net_a_precomp)
        ent_b = EntityNet(self.net_b, "net_b", self.node_names_b,
                          self.net_b_precomp)
        rel = RelationNet.from_raw_matrix(self.rel_ab, "rel_ab")
        connections = np.matrix([[-1, 0], [-1, -1]])

        dataset = GraphDataSet([ent_a, ent_b], [rel],
                               connections,
                               densify=True)

        self.assertFalse(sparse.issparse(dataset.networks[0].matrix))
        self.assertFalse(sparse.issparse(dataset.networks[1].matrix))
        self.assertFalse(sparse.issparse(dataset.relations[0].matrix))
Exemple #8
0
 def test_inconsistency_length_names_matrix_dims_raises_exception(self):
     new_names = ['a0']
     with self.assertRaises(ValueError):
         a_from_raw = EntityNet(self.net_a,
                                self.name,
                                new_names,
                                precomputed=self.net_a_precomp)
Exemple #9
0
 def test_densify_keep_dense_matrix(self):
     a_from_raw = EntityNet.from_raw_matrix(self.net_a, self.name,
                                            self.node_names)
     type_before = type(a_from_raw.matrix)
     a_from_raw.densify()
     type_after = type(a_from_raw.matrix)
     self.assertEqual(type_before, type_after)
Exemple #10
0
    def test_densify_changes_sparse_matrix_to_dense(self):
        a_from_raw = EntityNet.from_raw_matrix(sparse.csr_matrix(self.net_a),
                                               self.name, self.node_names)

        type_before = type(a_from_raw.matrix)
        a_from_raw.densify()
        type_after = type(a_from_raw.matrix)
        self.assertNotEqual(type_before, type_after)
Exemple #11
0
    def test_read_write_consistency(self):
        matfile = 'testmat.mat'
        ent_a = EntityNet(self.net_a, "net_a", self.node_names,
                          self.net_a_precomp)
        ent_b = EntityNet(self.net_b, "net_b", self.node_names_b,
                          self.net_b_precomp)
        rel = RelationNet.from_raw_matrix(self.rel_ab, "rel_ab")
        connections = np.matrix([[-1, 0], [-1, -1]])
        dataset = GraphDataSet([ent_a, ent_b], [rel], connections)

        dataset.write(self.test_dir, matfile)
        new_dataset = GraphDataSet.read(self.test_dir, matfile)

        self.assertEqual(len(new_dataset.networks), len(dataset.networks))
        self.assertEqual(len(new_dataset.relations), len(dataset.relations))
        self.assertEqual(new_dataset.connections.shape,
                         dataset.connections.shape)
Exemple #12
0
 def test_entity_net_from_raw_non_squared_raises_exception(self):
     with self.assertRaises(ValueError):
         e = EntityNet.from_raw_matrix(self.rel_ab, 'Net', self.node_names)
Exemple #13
0
 def test_entity_net_from_raw_no_errors(self):
     e = EntityNet.from_raw_matrix(self.net_a, 'Net', self.node_names)
Exemple #14
0
 def test_dims_difference_precomputed_matrix_raise_exception(self):
     with self.assertRaises(ValueError):
         a_from_raw = EntityNet(self.net_a,
                                self.name,
                                self.node_names,
                                precomputed=self.net_b_precomp)
Exemple #15
0
 def test_non_squared_matrix_shape_raises_exception(self):
     with self.assertRaises(ValueError):
         entity = EntityNet(self.rel_ab,
                            self.name,
                            self.node_names,
                            precomputed=self.net_b_precomp)
Exemple #16
0
 def test_non_matching_names_length_matrix_shape_raises_exception(self):
     with self.assertRaises(ValueError):
         entity = EntityNet(self.net_b,
                            self.name,
                            self.node_names,
                            precomputed=self.net_b_precomp)