def test_little_with_from_parameters(): graph = Graph.from_file("src/data/test_little_graph.txt") automaton = Graph.from_regexp("src/data/test_simple_regexp.txt") result = graph.intersect_with(automaton) args = {"type": 2, "from": [1]} paths_matrix = result.get_reachability(args) for i in range(len(paths_matrix)): if i in args["from"]: assert paths_matrix[0] != Vector.sparse(BOOL, result.size).full(0) else: assert paths_matrix[0] == Vector.sparse(BOOL, result.size).full(0)
def perform_bellman_ford(graph, src_vertex): """ From a given start vertex, finds the shortest paths to every other (reachable) vertex in the graph. graph: weighted graph src_vertex: source vertex return: Vector of computed distances """ if not graph.matrix.type == FP64: raise Exception("Graph is not weighted") res_vect = Vector.sparse(FP64, graph.matrix.nrows) res_vect[src_vertex] = 0 with semiring.MIN_PLUS, Accum(binaryop.MIN): for _ in range(graph.matrix.nrows): found_vect = res_vect.dup() res_vect @= graph.matrix if found_vect.iseq(res_vect): break return res_vect
def perform_level_bfs(graph, src_vertex): """ Computes traversal level for each vertex from source vertex. graph: any graph src_vertex: source vertex return: Vector of visited vertices """ res_vect = Vector.dense( INT64, graph.matrix.nrows, fill=0) # filling allows to work with disconnected graphs found_nodes_vect = Vector.sparse(BOOL, graph.matrix.nrows) found_nodes_vect[src_vertex] = True not_empty = True level = 1 while not_empty and level <= graph.matrix.nrows: res_vect.assign_scalar(level, mask=found_nodes_vect) with semiring.LOR_LAND_BOOL: found_nodes_vect = res_vect.vxm(graph.matrix, mask=res_vect, desc=descriptor.RC) not_empty = found_nodes_vect.reduce_bool() level += 1 return res_vect
def load_categories(neurons, nlayers, dest): fname = "{}/neuron{}-l{}-categories.tsv" cats = Path(fname.format(dest, neurons, nlayers)) result = Vector.sparse(BOOL, NFEATURES) with cats.open() as i: for line in i.readlines(): result[int(line.strip()) - 1] = True return result
def run(neurons, images, layers, bias, dest, movie="/dnn_demo/frames/f"): # hyperlayers = hypergraph(layers) # hyperbias = hypergraph(bias) # images.resize(hyperlayers.nrows, hyperlayers.ncols) # result = hyperdnn(len(layers), hyperlayers, hyperbias, images) result = dnn(layers, bias, images, movie=movie) r = result.reduce_vector() cats = r.apply(BOOL.ONE, out=Vector.sparse(BOOL, r.size)) truecats = load_categories(neurons, len(layers), dest) assert cats == truecats
def run(neurons, images, layers, bias, dest): result = dnn(layers, bias, images) r = result.reduce_vector() cats = r.apply(BOOL.ONE, out=Vector.sparse(BOOL, r.size)) truecats = load_categories(neurons, len(layers), dest) assert cats == truecats
def maximal_vector(T): return Vector.sparse(T, GxB_INDEX_MAX)