Example #1
0
    def testRBallHyper_CenterDefaultColor(self):
        dummy_hypergraph = Hypergraph(example_graphs.gt_dummy_graph)
        rball_in = algorithms.r_ball_hyper(dummy_hypergraph,
                                           "n_10",
                                           2,
                                           -1,
                                           center_default_color=True)
        rball_out = algorithms.r_ball_hyper(dummy_hypergraph,
                                            "n_10",
                                            2,
                                            1,
                                            center_default_color=True)
        rball_all = algorithms.r_ball_hyper(dummy_hypergraph,
                                            "n_10",
                                            2,
                                            0,
                                            center_default_color=True)
        d_rball_all = Hypergraph(example_graphs.gt_dummy_rball_10_r2_all)
        d_rball_out = Hypergraph(example_graphs.gt_dummy_rball_10_r2_out)
        d_rball_in = Hypergraph(example_graphs.gt_dummy_rball_10_r2_in)

        d_rball_all.node["n_10"]["labels"] = ["0"]
        d_rball_out.node["n_10"]["labels"] = ["0"]
        d_rball_in.node["n_10"]["labels"] = ["0"]

        all_isomorphic = algorithms.isomorphic(d_rball_all, rball_all)
        out_isomorphic = algorithms.isomorphic(d_rball_out, rball_out)
        in_isomorphic = algorithms.isomorphic(d_rball_in, rball_in)

        self.assertTrue(all_isomorphic,
                        "Problem extracting r-ball with edge_dir=0.")
        self.assertTrue(out_isomorphic,
                        "Problem extracting r-ball with edge_dir=1.")
        self.assertTrue(in_isomorphic,
                        "Problem extracting r-ball with edge_dir=-1.")
 def testFeatureExtraction(self):
     wl_state_exp = {
         "labels": {
             "g": "wl_0.0",
             "n": "wl_0.1",
             "r": "wl_0.2",
             "b": "wl_0.3",
             "wl_0.0;in(wl_0.2),out(wl_0.2)": "wl_1.0",
             "wl_0.1;in(wl_0.2),out(wl_0.2)": "wl_1.1",
             "wl_0.2;in(wl_0.1)": "wl_1.2",
             "wl_0.2;out(wl_0.0,wl_0.1)": "wl_1.3",
             "wl_0.2;in(wl_0.0)": "wl_1.4",
             "wl_0.1;in(wl_0.3),out(wl_0.2)": "wl_1.5",
             "wl_0.3;out(wl_0.1)": "wl_1.6",
             "wl_0.2;in(wl_0.1,wl_0.1)": "wl_1.7"
         },
         "next_labels": {
             0: 4,
             1: 8
         }
     }
     dummy_hypergraph = Hypergraph(example_graphs.snm_dummy_graph)
     rballs_database = [r_ball_hyper(dummy_hypergraph, "n_2", 1, edge_dir=1),
                        r_ball_hyper(dummy_hypergraph, "n_2", 1, edge_dir=-1)]
     features = []
     wl_state = None
     for rball in rballs_database:
         new_features, wl_state = feature_extraction.extract_features(rball, wl_iterations=1, wl_state=wl_state)
         features += new_features
     self.assertEqual(wl_state_exp, wl_state, "The wrong wl_state was computed by Weisfeiler-Lehman.")
     isomorphic = all([algorithms.isomorphic(features[i], example_graphs.snm_dummy_graph_features[i]) for i in range(len(features))])
     self.assertTrue(isomorphic, "Wrong features extracted.")
Example #3
0
def extract_rballs_of_node(node,
                           hypergraph,
                           r_in=0,
                           r_out=0,
                           r_all=0,
                           center_default_color=False):
    rballs = [
        r_ball_hyper(hypergraph,
                     node,
                     r_in,
                     edge_dir=-1,
                     center_default_color=center_default_color)
        if r_in > 0 else None,
        r_ball_hyper(hypergraph,
                     node,
                     r_out,
                     edge_dir=1,
                     center_default_color=center_default_color)
        if r_out > 0 else None,
        r_ball_hyper(hypergraph,
                     node,
                     r_all,
                     edge_dir=0,
                     center_default_color=center_default_color)
        if r_all > 0 else None
    ]

    return filter(lambda x: x is not None, rballs)
Example #4
0
 def testRBallHyper(self):
     dummy_hypergraph = Hypergraph(example_graphs.gt_dummy_graph)
     rball_in = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, -1)
     rball_out = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 1)
     rball_all = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 0)
     d_rball_all = Hypergraph(example_graphs.gt_dummy_rball_10_r2_all)
     d_rball_out = Hypergraph(example_graphs.gt_dummy_rball_10_r2_out)
     d_rball_in = Hypergraph(example_graphs.gt_dummy_rball_10_r2_in)
     
     all_isomorphic = algorithms.isomorphic(d_rball_all, rball_all)
     out_isomorphic = algorithms.isomorphic(d_rball_out, rball_out)
     in_isomorphic = algorithms.isomorphic(d_rball_in, rball_in)
     
     self.assertTrue(all_isomorphic, "Problem extracting r-ball with edge_dir=0.")
     self.assertTrue(out_isomorphic, "Problem extracting r-ball with edge_dir=1.")
     self.assertTrue(in_isomorphic, "Problem extracting r-ball with edge_dir=-1.")
Example #5
0
    def testRBallHyper(self):
        dummy_hypergraph = Hypergraph(example_graphs.gt_dummy_graph)
        rball_in = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, -1)
        rball_out = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 1)
        rball_all = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 0)
        d_rball_all = Hypergraph(example_graphs.gt_dummy_rball_10_r2_all)
        d_rball_out = Hypergraph(example_graphs.gt_dummy_rball_10_r2_out)
        d_rball_in = Hypergraph(example_graphs.gt_dummy_rball_10_r2_in)

        all_isomorphic = algorithms.isomorphic(d_rball_all, rball_all)
        out_isomorphic = algorithms.isomorphic(d_rball_out, rball_out)
        in_isomorphic = algorithms.isomorphic(d_rball_in, rball_in)

        self.assertTrue(all_isomorphic,
                        "Problem extracting r-ball with edge_dir=0.")
        self.assertTrue(out_isomorphic,
                        "Problem extracting r-ball with edge_dir=1.")
        self.assertTrue(in_isomorphic,
                        "Problem extracting r-ball with edge_dir=-1.")
Example #6
0
 def testRBallHyper_CenterDefaultColor(self):
     dummy_hypergraph = Hypergraph(example_graphs.gt_dummy_graph)
     rball_in = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, -1, center_default_color=True)
     rball_out = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 1, center_default_color=True)
     rball_all = algorithms.r_ball_hyper(dummy_hypergraph, "n_10", 2, 0, center_default_color=True)
     d_rball_all = Hypergraph(example_graphs.gt_dummy_rball_10_r2_all)
     d_rball_out = Hypergraph(example_graphs.gt_dummy_rball_10_r2_out)
     d_rball_in = Hypergraph(example_graphs.gt_dummy_rball_10_r2_in)
     
     d_rball_all.node["n_10"]["labels"] = ["0"]
     d_rball_out.node["n_10"]["labels"] = ["0"]
     d_rball_in.node["n_10"]["labels"] = ["0"]
     
     all_isomorphic = algorithms.isomorphic(d_rball_all, rball_all)
     out_isomorphic = algorithms.isomorphic(d_rball_out, rball_out)
     in_isomorphic = algorithms.isomorphic(d_rball_in, rball_in)
     
     self.assertTrue(all_isomorphic, "Problem extracting r-ball with edge_dir=0.")
     self.assertTrue(out_isomorphic, "Problem extracting r-ball with edge_dir=1.")
     self.assertTrue(in_isomorphic, "Problem extracting r-ball with edge_dir=-1.")
 def testFeatureExtraction(self):
     wl_state_exp = {
         "labels": {
             "g": "wl_0.0",
             "n": "wl_0.1",
             "r": "wl_0.2",
             "b": "wl_0.3",
             "wl_0.0;in(wl_0.2),out(wl_0.2)": "wl_1.0",
             "wl_0.1;in(wl_0.2),out(wl_0.2)": "wl_1.1",
             "wl_0.2;in(wl_0.1)": "wl_1.2",
             "wl_0.2;out(wl_0.0,wl_0.1)": "wl_1.3",
             "wl_0.2;in(wl_0.0)": "wl_1.4",
             "wl_0.1;in(wl_0.3),out(wl_0.2)": "wl_1.5",
             "wl_0.3;out(wl_0.1)": "wl_1.6",
             "wl_0.2;in(wl_0.1,wl_0.1)": "wl_1.7"
         },
         "next_labels": {
             0: 4,
             1: 8
         }
     }
     dummy_hypergraph = Hypergraph(example_graphs.snm_dummy_graph)
     rballs_database = [
         r_ball_hyper(dummy_hypergraph, "n_2", 1, edge_dir=1),
         r_ball_hyper(dummy_hypergraph, "n_2", 1, edge_dir=-1)
     ]
     features = []
     wl_state = None
     for rball in rballs_database:
         new_features, wl_state = feature_extraction.extract_features(
             rball, wl_iterations=1, wl_state=wl_state)
         features += new_features
     self.assertEqual(
         wl_state_exp, wl_state,
         "The wrong wl_state was computed by Weisfeiler-Lehman.")
     isomorphic = all([
         algorithms.isomorphic(features[i],
                               example_graphs.snm_dummy_graph_features[i])
         for i in range(len(features))
     ])
     self.assertTrue(isomorphic, "Wrong features extracted.")
Example #8
0
def extract_rballs_of_node(node, hypergraph, r_in=0, r_out=0, r_all=0, center_default_color=False):
    rballs = [r_ball_hyper(hypergraph, node, r_in, edge_dir=-1, center_default_color=center_default_color) if r_in > 0 else None,
              r_ball_hyper(hypergraph, node, r_out, edge_dir=1, center_default_color=center_default_color) if r_out > 0 else None,
              r_ball_hyper(hypergraph, node, r_all, edge_dir=0, center_default_color=center_default_color) if r_all > 0 else None]
    
    return filter(lambda x: x is not None, rballs)
Example #9
0
    def chem_database_generator(full_graph, uri_node_map, type_color_map, compounds_and_targets):
        literal_colors = set()
        for rdf_type in type_color_map:
            # TODO: this condition is unsafe because it may remove not only literal colors
            if rdf_type.startswith(u"http://www.w3.org/2001/XMLSchema#"):
                literal_colors.add(type_color_map[rdf_type])
        
        bool_colors = filter(lambda x: x.startswith(u"http://www.w3.org/2001/XMLSchema#boolean"), type_color_map)
        bool_colors = set(map(lambda x: type_color_map[x], bool_colors))
        literal_colors -= bool_colors
         
        for node in full_graph.nodes():
            node_labels_set = set(full_graph.node[node]["labels"])
            # remove all literals (except booleans)
            if literal_colors & node_labels_set:
                full_graph.remove_node(node)
        
        # remove the color of named individual type from all nodes where it occurs
        named_indiv_uri = u"http://www.w3.org/2002/07/owl#NamedIndividual"
        if named_indiv_uri in type_color_map:
            named_indiv_color = type_color_map[named_indiv_uri]
            for node in full_graph.nodes_iter():
                if named_indiv_color in full_graph.node[node]["labels"]:
                    full_graph.node[node]["labels"].remove(named_indiv_color)
    
        full_hypergraph = Hypergraph(full_graph)
        
#         ################
#         # INFO: use this to remove the isMutagenic property when predicting mutagenicity
#         is_mutag_color = type_color_map[u"http://dl-learner.org/carcinogenesis#isMutagenic"]
#         edges_to_remove = []
#         for edge in full_hypergraph.edges_iter():
#             if is_mutag_color in full_hypergraph.edge(edge)['labels']:
#                 edges_to_remove.append(edge)
#         for edge in edges_to_remove:
#             full_hypergraph.safe_remove_edge(edge)
#         ################
        
        if not compounds_and_targets:
            compounds_and_targets = read_compounds_and_targets()
        
        def remove_other_neighbors_of_bool_literals(hypergraph, center_node):
            center_neighbors = hypergraph.neighbors(center_node)
            bool_literals = filter(lambda n: set(hypergraph.node[n]['labels']) & bool_colors, center_neighbors)
            for bool_literal in bool_literals:
                bool_literal_neigbors = set(hypergraph.neighbors(bool_literal))
                # exclude the center node from the removable nodes
                bool_literal_neigbors.remove(center_node)
                for neigh in bool_literal_neigbors:
                    hypergraph.safe_remove_node(neigh)
        
        for comp_id, target_label in compounds_and_targets:
            node_id = u"n_{0}".format(uri_node_map[uri_prefix + comp_id])
            comp_neighborhood_hypergraph = algorithms.r_ball_hyper(full_hypergraph, node_id, 2, 0)
            remove_other_neighbors_of_bool_literals(comp_neighborhood_hypergraph, node_id)
            ch_db_record = (comp_id, [comp_neighborhood_hypergraph], target_label)
            if process_compound_function:
                process_compound_function(ch_db_record)
#             ############
#             def get_key(value, dictionary):
#                 for key in dictionary:
#                     if dictionary[key] == value:
#                         return key
#                 return None
#             g = ch_db_record[1][0].copy()
#             for n in g.node:
#                 n_new_labels = []
#                 for n_color in g.node[n]['labels']:
#                     n_rdf_type = get_key(n_color, type_color_map)
#                     n_rdf_type = n_rdf_type[n_rdf_type.find(u"#") + 1:]
#                     n_new_labels.append(n_rdf_type)
#                 g.node[n]['labels'] = n_new_labels
#             g.visualize()
#             ############
            yield ch_db_record