def test_matrix_assign(): m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[2] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 2, 2, 2], [0, 1, 0, 1, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[2, :] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 2, 2, 2], [0, 1, 0, 1, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[:, 2] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 0, 1, 2], [0, 1, 2, 2, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_type(int, 3, 3) assert m.nvals == 0 n = Matrix.from_lists([0, 1, 2], [0, 1, 2], [0, 1, 2]) m[:, :] = n assert m == n
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 test_vector_gb_type(): v = Vector.from_type(bool, 10) assert v.gb_type == lib.GrB_BOOL v = Vector.from_type(int, 10) assert v.gb_type == lib.GrB_INT64 v = Vector.from_type(float, 10) assert v.gb_type == lib.GrB_FP64
def test_mxv(): m = Matrix.from_lists([0, 1, 2], [1, 2, 0], [1, 2, 3]) v = Vector.from_lists([0, 1, 2], [2, 3, 4]) o = m.mxv(v) assert o == Vector.from_lists([0, 1, 2], [3, 8, 6]) assert m @ v == o
def test_apply(): v = Vector.from_lists( [0, 1, 2], [2, 3, 4]) w = v.apply(unaryop.ainv_int64) assert w == Vector.from_lists( [0, 1, 2], [-2, -3, -4])
def test_vector_ewise_add(): v = Vector.from_list(list(range(10))) w = Vector.from_list(list(range(10))) x = v.ewise_add(w) assert x == Vector.from_lists(list(range(10)), list(range(0, 20, 2))) z = v + w assert x == z v += w assert v == z
def test_to_dense(): v = Vector.from_lists(list(range(0, 6, 2)), list(range(3))) assert v.size == 5 assert v.nvals == 3 w = v.to_dense() assert w.nvals == 5 assert w == Vector.from_lists( [0, 1, 2, 3, 4], [0, 0, 1, 0, 2])
def test_vector_ewise_mult(): v = Vector.from_list(list(range(10))) w = Vector.from_list(list(range(10))) x = v.ewise_mult(w) assert x == Vector.from_lists(list(range(10)), list(map(lambda x: x * x, list(range(10))))) z = v * w assert x == z v *= w assert v == z
def test_vxm(): m = Matrix.from_lists([0, 1, 2], [1, 2, 0], [1, 2, 3]) v = Vector.from_lists([0, 1, 2], [2, 3, 4]) o = v.vxm(m) assert o == Vector.from_lists([0, 1, 2], [12, 2, 6]) w = Vector.dup(v) assert v @ m == o v @= m assert v == o
def sssp2(matrix, start): v = Vector.from_type(matrix.gb_type, matrix.nrows) v[start] = 0 with min_plus_int64, Accum(min_int64): for _ in range(matrix.nrows): w = Vector.dup(v) v @= matrix if w == v: break return v
def maxflow_direct(matrix, start): v = Vector.from_type(matrix.gb_type, matrix.nrows) v[start] = 0 with max_times_fp64, Accum(max_fp64): for _ in range(matrix.nrows): w = Vector.dup(v) v @= matrix if w == v: break return v
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 test_matrix_reduce_vector(): m = Matrix.from_lists( list(range(10)), list(range(10)), list(range(10))) v = m.reduce_vector() v == Vector.from_list(list(range(10)))
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 load_categories(neurons, nlayers, dest): cats = Path('{}/neuron{}-l{}-categories.tsv'.format(dest, neurons, nlayers)) result = Vector.from_type(bool, NFEATURES) with cats.open() as i: for line in i.readlines(): result[int(line.strip())-1] = True return result
def test_vector_set_element(): m = Vector.from_type(int, 10) m[3] = 3 assert m.size == 10 assert m.nvals == 1 assert m[3] == 3 m = Vector.from_type(bool, 10) m[3] = True assert m.size == 10 assert m.nvals == 1 assert m[3] == True m = Vector.from_type(float, 10) m[3] = 3.3 assert m.size == 10 assert m.nvals == 1 assert m[3] == 3.3
def test_vector_slice(): v = Vector.from_list(list(range(10))) w = v[:9] assert w.size == 10 assert w.nvals == 10 assert w.to_lists() == [ list(range(10)), list(range(10))] w = v[1:8] assert w.size == 8 assert w.nvals == 8 assert w.to_lists() == [ [0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8]] w = v[1:] assert w.size == 9 assert w.nvals == 9 assert w.to_lists() == [ [0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8, 9]] w = v[1:9:2] assert w.size == 5 assert w.nvals == 5 assert w.to_lists() == [ [0, 1, 2, 3, 4], [1, 3, 5, 7, 9]] w = v[7:1:-2] assert w.size == 4 assert w.nvals == 4 assert w.to_lists() == [ [0, 1, 2, 3], [7, 5, 3, 1]]
def test_vector_reduce_float(): v = Vector.from_type(float, 10) r = v.reduce_float() assert type(r) is float assert r == 0.0 v[3] = 3.3 v[4] = 4.4 assert v.reduce_float() == 7.7
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): result = dnn(layers, bias, images) r = result.reduce_vector() cats = r.apply(lib.GxB_ONE_BOOL, out=Vector.from_type(bool, r.size)) truecats = load_categories(neurons, nlayers, dest) assert cats == truecats
def test_vector_reduce_int(): v = Vector.from_type(int, 10) r = v.reduce_int() assert type(r) is int assert r == 0 v[3] = 3 v[4] = 4 assert v.reduce_int() == 7
def test_matrix_slicing(): I, J = tuple(map(list, zip(*product(range(3), repeat=2)))) V = list(range(9)) m = Matrix.from_lists(I, J, V, 3, 3) v = m[2] assert v == Vector.from_lists([0, 1, 2], [6, 7, 8]) # slice out row vector v = m[2, :] assert v == Vector.from_lists([0, 1, 2], [6, 7, 8]) # slice out column vector v = m[:, 2] assert v == Vector.from_lists([0, 1, 2], [2, 5, 8]) # slice copy n = m[:] assert n == m # also slice copy n = m[:, :] assert n == m # submatrix slice out rows sm = m[0:1] assert sm == Matrix.from_lists([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5], 2, 3) # submatrix slice out columns n = m[:, 1:] assert n == Matrix.from_lists([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], [1, 2, 4, 5, 7, 8], 3, 2) # submatrix slice out column range sm = m[:, 1:2] assert sm == Matrix.from_lists([0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1], [1, 2, 4, 5, 7, 8], 3, 2) # submatrix slice out rows n = m[1:, :] assert n == Matrix.from_lists([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [3, 4, 5, 6, 7, 8], 2, 3) # submatrix slice out row range sm = m[1:2, :] assert sm == Matrix.from_lists([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [3, 4, 5, 6, 7, 8], 2, 3)
def test_Graph_perform_bellman_ford(): graph = WeightedGraph.from_mm_file( os.path.join(TEST_DATA_PATH, 'weighted_graph.mtx')) actual = perform_bellman_ford(graph, 0) expected = Vector.from_list([0.0, 0.3, 1.0, 0.8, 0.4, 0.5, 1.0]) assert len(actual) == len(expected) assert actual.iseq(expected)
def sssp(m, start): v = Vector.from_type( # create a vector m.gb_type, # same type as m m.nrows # same size as rows of m ) v[start] = 0 # set the starting vertext distance for _ in range(m.nrows): # for every row in m: w = Vector.dup(v) # dup the vector v.vxm( # multiply vector by matrix m, out=v, semiring=min_plus_int64, # with min_plus, accum=min_int64 # acccumulate the minimum ) if w == v: # if the result hasn't changed, break # exit early return v
def test_Graph_perform_level_bfs_on_weighted_graph(): graph = WeightedGraph.from_mm_file( os.path.join(TEST_DATA_PATH, 'weighted_graph.mtx')) actual = perform_level_bfs(graph, 0) expected = Vector.from_list([1, 2, 3, 2, 3, 4, 3]) assert len(actual) == len(expected) assert actual.iseq(expected)
def test_vector_assign(): v = Vector.from_type(int, 10) assert v.nvals == 0 w = Vector.from_lists(list(range(10)), list(range(10))) v[:] = w assert v == w v[1:] = w[9:1:-1] assert v == Vector.from_lists([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]) w[9:1:-1] = v[9:1:-1] assert w == v v[:] = 3 assert v == Vector.from_lists(list(range(10)), list(repeat(3, 10))) v[1:] = 0 assert v == Vector.from_lists(list(range(10)), [3] + list(repeat(0, 9)))
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 test_matrix_slice_vector(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) assert v[5] == Vector.from_lists([5], [5], 10)
def test_vector_reduce_bool(): v = Vector.from_type(bool, 10) assert not v.reduce_bool() v[3] = True assert v.reduce_bool()
def test_vector_eq(): v = Vector.from_list(list(range(10))) w = Vector.from_list(list(range(10))) x = Vector.from_list(list(range(1,11))) assert v == w assert v != x