def get_subgraph_nedges(self, rows, cols): """ Pulls out an arbitrary i.e. non-contiguous submatrix out of a sparse.coo_matrix. Returns ------ tuples of org_row_id, org_col_id, value """ matr = self.sm.tocoo() gr = -1 * st.ones(matr.shape[0], dtype=int) gc = -1 * st.ones(matr.shape[1], dtype=int) lr = len(rows) lc = len(cols) ar = st.arange(0, lr, 1) ac = st.arange(0, lc, 1) gr[rows[ar]] = ar gc[cols[ac]] = ac mrow = matr.row mcol = matr.col newelem = (gr[mrow] > -1) & (gc[mcol] > -1) subvalues = matr.data[newelem] if self.weighted: nedges = len(subvalues) else: nedges = subvalues.sum() return nedges
def test_arange_shape(self): # Arange with no parameters. Assert.raises_exception(ValueError, spartan.arange) # Arange with shape and stop # Assert.raises_exception(ValueError, spartan.arange, (0, ), stop=0) # Arange with shape Assert.all_eq(spartan.arange((10,)).glom(), np.arange(10)) Assert.all_eq(spartan.arange((3, 5)).glom(), np.arange(15).reshape((3, 5))) # Arange with shape, start Assert.all_eq(spartan.arange((10,), -1).glom(), np.arange(-1, 9)) Assert.all_eq(spartan.arange((10,), 1).glom(), np.arange(1, 11)) Assert.all_eq(spartan.arange((3, 5), -1).glom(), np.arange(-1, 14).reshape((3, 5))) # Arange with shape, step Assert.all_eq(spartan.arange((10,), step=2).glom(), np.arange(0, 20, 2)) Assert.all_eq(spartan.arange((3, 5), step=2).glom(), np.arange(0, 30, 2).reshape((3, 5))) # Arange with shape, start, step Assert.all_eq(spartan.arange((10,), -1, step=2).glom(), np.arange(-1, 19, 2)) Assert.all_eq(spartan.arange((10,), 1, step=2).glom(), np.arange(1, 21, 2)) Assert.all_eq(spartan.arange((3, 5), 1, step=2).glom(), np.arange(1, 31, 2).reshape((3, 5)))
def get_sub_graph(self, rows, cols): cootensor = self.graph_tensor gr = -1 * st.ones(cootensor.shape[0], dtype=int) gc = -1 * st.ones(cootensor.shape[1], dtype=int) lr = len(rows) lc = len(cols) ar = st.arange(0, lr, 1) ac = st.arange(0, lc, 1) gr[rows[ar]] = ar gc[cols[ac]] = ac mrow = cootensor.coords[0] mcol = cootensor.coords[1] newelem = (gr[mrow] > -1) & (gc[mcol] > -1) newrows = mrow[newelem] newcols = mcol[newelem] subcoords = st.stack( (gr[newrows], gc[newcols], *cootensor.coords[2:, newelem]), axis=0) subvalues = cootensor.data[newelem] subtensor = st.STensor((subcoords, subvalues), shape=(lr, lc, *cootensor.shape[2:])) return st.Graph(subtensor, self.weighted, self.bipartite, self.modet)
def test_arange_stop(self): # Arange with stop. Assert.all_eq(spartan.arange(stop=10).glom(), np.arange(10)) # Arange with start, stop Assert.all_eq(spartan.arange(None, -1, 10).glom(), np.arange(-1, 10)) Assert.all_eq(spartan.arange(None, 1, 10).glom(), np.arange(1, 10)) # Arange with start, stop, step Assert.all_eq(spartan.arange(None, -1, 19, 2).glom(), np.arange(-1, 19, 2)) Assert.all_eq(spartan.arange(None, 1, 21, 2).glom(), np.arange(1, 21, 2))
def test_arange_stop(self): # Arange with stop. Assert.all_eq(spartan.arange(stop=10).glom(), np.arange(10)) # Arange with start, stop Assert.all_eq(spartan.arange(-1, 10).glom(), np.arange(-1, 10)) Assert.all_eq(spartan.arange(1, 10).glom(), np.arange(1, 10)) # Arange with start, stop, step Assert.all_eq(spartan.arange(-1, 19, 2).glom(), np.arange(-1, 19, 2)) Assert.all_eq(spartan.arange(1, 21, 2).glom(), np.arange(1, 21, 2))
def test_distarray(self): A = spartan.arange(40000, dtype=np.int32).reshape(100, 400).evaluate() nA = np.arange(40000).reshape(100, 400) B = A.transpose().evaluate() nB = nA.transpose() C = B.T.evaluate() nC = nB.T D = (C / 100).evaluate() nD = nC / 100 E = D.all() nE = nD.all() Assert.all_eq(E.glom(), nE)
def test_logic(self): # Arange with no parameters. A = spartan.arange(40000, dtype=np.int32).reshape(100, 400) nA = np.arange(40000).reshape(100, 400) B = A.T nB = nA.T C = B / 1000 nC = nB / 1000 D = spartan.all(C) nD = np.all(nC) E = spartan.any(C) nE = np.any(nC) Assert.all_eq(D.glom(), nD) Assert.all_eq(E.glom(), nE)
def test_arange_shape(self): # Arange with no parameters. Assert.raises_exception(ValueError, spartan.arange) # Arange with shape and stop Assert.raises_exception(ValueError, spartan.arange, (0, ), stop=0) # Arange with shape Assert.all_eq(spartan.arange((10, )).glom(), np.arange(10)) Assert.all_eq( spartan.arange((3, 5)).glom(), np.arange(15).reshape((3, 5))) # Arange with shape, start Assert.all_eq(spartan.arange((10, ), -1).glom(), np.arange(-1, 9)) Assert.all_eq(spartan.arange((10, ), 1).glom(), np.arange(1, 11)) Assert.all_eq( spartan.arange((3, 5), -1).glom(), np.arange(-1, 14).reshape((3, 5))) # Arange with shape, step Assert.all_eq( spartan.arange((10, ), step=2).glom(), np.arange(0, 20, 2)) Assert.all_eq( spartan.arange((3, 5), step=2).glom(), np.arange(0, 30, 2).reshape((3, 5))) # Arange with shape, start, step Assert.all_eq( spartan.arange((10, ), -1, step=2).glom(), np.arange(-1, 19, 2)) Assert.all_eq( spartan.arange((10, ), 1, step=2).glom(), np.arange(1, 21, 2)) Assert.all_eq( spartan.arange((3, 5), 1, step=2).glom(), np.arange(1, 31, 2).reshape((3, 5)))
def test_ravel(self): x = spartan.arange((TEST_SIZE, TEST_SIZE)) n = np.arange(TEST_SIZE * TEST_SIZE).reshape((TEST_SIZE, TEST_SIZE)) Assert.all_eq(n.ravel(), x.ravel().glom())
def test_mathematics(self): A = spartan.arange(40000, dtype=np.int32).reshape(100, 400) nA = np.arange(40000).reshape(100, 400) B = A.prod() nB = nA.prod() Assert.all_eq(B.glom(), nB)