def _build_analogy_tensor(self, graph, num_nodes, min_graph_edges, min_feature_edges, max_feature_edges, logging_interval=None): ''' Builds a tensor T whose rows correspond to maximal dense subgraphs of graph and whose columns are relation structures (n-ary relations formed by combining multiple binary relations). graph - The graph from which the tensor is built num_nodes - The size of each vertex set row min_graph_edges - The minimum number of edges in the graphs being indexed min_feature_edges - The minimum number of edges to include in a graph feature max_feature_edges - The minimum number of edges to include in a graph feature Note: the min_graph_edges parameter treats the graph as if it is undirected and untyped, meaning there is at most 1 edge between 2 vertices ''' k_subgraphs = k_edge_subgraphs(graph, min_graph_edges, num_nodes, logging_interval=logging_interval) rel_tensor = SparseLabeledTensor(ndim=2) logging_counter = 0 for size, subgraphs_vertices in k_subgraphs.iteritems(): if size[0] == num_nodes and size[1] >= min_graph_edges: logging.debug( "Adding graphs with %d vertices and %d edges (%d total)...", size[0], size[1], len(subgraphs_vertices)) for subgraph_vertices in subgraphs_vertices: subgraph = graph.subgraph_from_vertices(subgraph_vertices) for subgraph_no_repeats in subgraph.enumerate_without_repeated_edges( ): if (len(subgraph_no_repeats.edges()) >= min_feature_edges and len(subgraph_no_repeats.edges()) <= max_feature_edges): for vertex_order in enumerate_orders( subgraph_vertices): vm = dict([ (y, x) for x, y in enumerate(vertex_order) ]) edges = frozenset([(vm[v1], vm[v2], value) for v1, v2, value in subgraph_no_repeats.edges() ]) rel_tensor[vertex_order, edges] = 1 if logging_interval is not None and logging_counter % logging_interval == 0: logging.debug("%r", subgraph_vertices) logging_counter += 1 return rel_tensor
def normalize_and_copy_mode_one(tensor): newt = SparseLabeledTensor(ndim=2) newt.update(tensor.normalized(mode=1)) return newt
def normalize_and_copy(tensor): newt = SparseLabeledTensor(ndim=2) newt.update(tensor.normalized()) return newt
def insert_test(num_rows, num_cols): t = SparseLabeledTensor(ndim=2) for i in xrange(0, num_rows): for j in xrange(0, num_cols): t[i, j] = 1 return t