Example #1
0
 def _transform_vertex_pair_valid(self,
                                  graph,
                                  vertex_v,
                                  vertex_u,
                                  radius,
                                  distance,
                                  feature_list,
                                  connection_weight=1):
     cw = connection_weight
     # we need to revert to r/2 and d/2
     radius_dist_key = (radius / 2, distance / 2)
     # reweight using external weight dictionary
     len_v = len(graph.nodes[vertex_v]['neigh_graph_hash'])
     len_u = len(graph.nodes[vertex_u]['neigh_graph_hash'])
     if radius < len_v and radius < len_u:
         # feature as a pair of neighborhoods at a radius,distance
         # canonicalization of pair of neighborhoods
         vertex_v_labels = graph.nodes[vertex_v]['neigh_graph_hash']
         vertex_v_hash = vertex_v_labels[radius]
         vertex_u_labels = graph.nodes[vertex_u]['neigh_graph_hash']
         vertex_u_hash = vertex_u_labels[radius]
         if vertex_v_hash < vertex_u_hash:
             first_hash, second_hash = (vertex_v_hash, vertex_u_hash)
         else:
             first_hash, second_hash = (vertex_u_hash, vertex_v_hash)
         feature = fast_hash_4(first_hash, second_hash, radius, distance,
                               self.bitmask)
         # half features are those that ignore the central vertex v
         # the reason to have those is to help model the context
         # independently from the identity of the vertex itself
         half_feature = fast_hash_3(vertex_u_hash, radius, distance,
                                    self.bitmask)
         if graph.graph.get('weighted', False) is False:
             if self.use_only_context is False:
                 feature_list[radius_dist_key][feature] += cw
             feature_list[radius_dist_key][half_feature] += cw
         else:
             weight_v = graph.nodes[vertex_v]['neigh_graph_weight']
             weight_u = graph.nodes[vertex_u]['neigh_graph_weight']
             weight_vu_radius = weight_v[radius] + weight_u[radius]
             val = cw * weight_vu_radius
             # Note: add a feature only if the value is not 0
             if val != 0:
                 if self.use_only_context is False:
                     feature_list[radius_dist_key][feature] += val
                 half_val = cw * weight_u[radius]
                 feature_list[radius_dist_key][half_feature] += half_val
Example #2
0
 def _transform_vertex_pair_valid(self,
                                  graph,
                                  vertex_v,
                                  vertex_u,
                                  radius,
                                  distance,
                                  feature_list,
                                  connection_weight=1):
     cw = connection_weight
     # we need to revert to r/2 and d/2
     radius_dist_key = (radius / 2, distance / 2)
     # reweight using external weight dictionary
     len_v = len(graph.nodes[vertex_v]['neigh_graph_hash'])
     len_u = len(graph.nodes[vertex_u]['neigh_graph_hash'])
     if radius < len_v and radius < len_u:
         # feature as a pair of neighborhoods at a radius,distance
         # canonicalization of pair of neighborhoods
         vertex_v_labels = graph.nodes[vertex_v]['neigh_graph_hash']
         vertex_v_hash = vertex_v_labels[radius]
         vertex_u_labels = graph.nodes[vertex_u]['neigh_graph_hash']
         vertex_u_hash = vertex_u_labels[radius]
         if vertex_v_hash < vertex_u_hash:
             first_hash, second_hash = (vertex_v_hash, vertex_u_hash)
         else:
             first_hash, second_hash = (vertex_u_hash, vertex_v_hash)
         feature = fast_hash_4(
             first_hash, second_hash, radius, distance, self.bitmask)
         # half features are those that ignore the central vertex v
         # the reason to have those is to help model the context
         # independently from the identity of the vertex itself
         half_feature = fast_hash_3(vertex_u_hash,
                                    radius, distance, self.bitmask)
         if graph.graph.get('weighted', False) is False:
             feature_list[radius_dist_key][feature] += cw
             feature_list[radius_dist_key][half_feature] += cw
         else:
             weight_v = graph.nodes[vertex_v]['neigh_graph_weight']
             weight_u = graph.nodes[vertex_u]['neigh_graph_weight']
             weight_vu_radius = weight_v[radius] + weight_u[radius]
             val = cw * weight_vu_radius
             # Note: add a feature only if the value is not 0
             if val != 0:
                 feature_list[radius_dist_key][feature] += val
                 half_val = cw * weight_u[radius]
                 feature_list[radius_dist_key][half_feature] += half_val
Example #3
0
 def _transform_vertex_pair(self,
                            graph,
                            vertex_v,
                            vertex_u,
                            distance,
                            feature_list,
                            connection_weight=1):
     # for all radii
     for radius in range(self.min_r, self.r + 2, 2):
         for label_index in range(graph.graph['label_size']):
             if radius < len(graph.node[vertex_v]
                             ['neigh_graph_hash'][label_index]) and \
                     radius < len(graph.node[vertex_u]['neigh_graph_hash'][label_index]):
                 # feature as a pair of neighborhoods at a radius,distance
                 # canonicalization of pair of neighborhoods
                 vertex_v_hash = graph.node[vertex_v]['neigh_graph_hash'][
                     label_index][radius]
                 vertex_u_hash = graph.node[vertex_u]['neigh_graph_hash'][
                     label_index][radius]
                 if vertex_v_hash < vertex_u_hash:
                     first_hash, second_hash = (vertex_v_hash,
                                                vertex_u_hash)
                 else:
                     first_hash, second_hash = (vertex_u_hash,
                                                vertex_v_hash)
                 feature = fast_hash_4(first_hash, second_hash, radius,
                                       distance, self.bitmask)
                 # half features are those that ignore the central vertex v
                 # the reason to have those is to help model the context
                 # independently from the identity of the vertex itself
                 half_feature = fast_hash_3(vertex_u_hash, radius, distance,
                                            self.bitmask)
                 key = fast_hash_2(radius, distance)
                 if graph.graph.get('weighted', False) is False:
                     feature_list[key][feature] += 1
                     feature_list[key][half_feature] += 1
                 else:
                     val = connection_weight * \
                         (graph.node[vertex_v]['neigh_graph_weight'][radius] +
                          graph.node[vertex_u]['neigh_graph_weight'][radius])
                     feature_list[key][feature] += val
                     half_val = \
                         connection_weight * \
                         graph.node[vertex_u]['neigh_graph_weight'][radius]
                     feature_list[key][half_feature] += half_val
Example #4
0
 def _transform_vertex_pair(self,
                            graph,
                            vertex_v,
                            vertex_u,
                            distance,
                            feature_list,
                            connection_weight=1):
     # for all radii
     for radius in range(self.min_r, self.r + 2, 2):
         for label_index in range(graph.graph['label_size']):
             if radius < len(graph.node[vertex_v]
                             ['neigh_graph_hash'][label_index]) and \
                     radius < len(graph.node[vertex_u]['neigh_graph_hash'][label_index]):
                 # feature as a pair of neighborhoods at a radius,distance
                 # canonicalization of pair of neighborhoods
                 vertex_v_hash = graph.node[vertex_v]['neigh_graph_hash'][label_index][radius]
                 vertex_u_hash = graph.node[vertex_u]['neigh_graph_hash'][label_index][radius]
                 if vertex_v_hash < vertex_u_hash:
                     first_hash, second_hash = (vertex_v_hash,
                                                vertex_u_hash)
                 else:
                     first_hash, second_hash = (vertex_u_hash,
                                                vertex_v_hash)
                 feature = fast_hash_4(first_hash, second_hash,
                                       radius, distance, self.bitmask)
                 # half features are those that ignore the central vertex v
                 # the reason to have those is to help model the context
                 # independently from the identity of the vertex itself
                 half_feature = fast_hash_3(vertex_u_hash,
                                            radius, distance, self.bitmask)
                 key = fast_hash_2(radius, distance)
                 if graph.graph.get('weighted', False) is False:
                     feature_list[key][feature] += 1
                     feature_list[key][half_feature] += 1
                 else:
                     val = connection_weight * \
                         (graph.node[vertex_v]['neigh_graph_weight'][radius] +
                          graph.node[vertex_u]['neigh_graph_weight'][radius])
                     feature_list[key][feature] += val
                     half_val = \
                         connection_weight * \
                         graph.node[vertex_u]['neigh_graph_weight'][radius]
                     feature_list[key][half_feature] += half_val