def overlaps(frame1, labels1, frame2, labels2): """ figures out which label of self matches which label of other Assumes the zero label does not exist (background) Returns sparse array of: label in self (row) label in other (col) number of shared pixels (data) """ ki = np.empty(frame1.nnz, 'i') kj = np.empty(frame2.nnz, 'i') npx = cImageD11.sparse_overlaps(frame1.row, frame1.col, ki, frame2.row, frame2.col, kj) # self.data and other.data filled during init row = frame1.pixels[labels1][ki[:npx]] # my labels col = frame2.pixels[labels2][kj[:npx]] # your labels ect = np.empty(npx, 'i') # ect = counts of overlaps tj = np.empty(npx, 'i') # tj = temporary for sorting n1 = frame1.meta[labels1]["nlabel"] n2 = frame2.meta[labels2]["nlabel"] tmp = np.empty(max(n1, n2) + 1, 'i') # for histogram nedge = cImageD11.compress_duplicates(row, col, ect, tj, tmp) # overwrites row/col in place : ignore the zero label (hope it is not there) crow = row[:nedge] - 1 ccol = col[:nedge] - 1 cdata = ect[:nedge] cedges = scipy.sparse.coo_matrix((cdata, (crow, ccol)), shape=(n1, n2)) # really? return cedges
def overlaps(frame1, labels1, frame2, labels2): """ figures out which label of self matches which label of other Returns sparse array of: label in self (row) label in other (col) number of shared pixels (data) """ ki = np.empty(frame1.nnz, 'i') kj = np.empty(frame2.nnz, 'i') npx = cImageD11.sparse_overlaps(frame1.row, frame1.col, ki, frame2.row, frame2.col, kj) # self.data and other.data filled during init row = frame1.pixels[labels1].data[ki[:npx]] # my labels col = frame2.pixels[labels2].data[kj[:npx]] # your labels ect = np.empty(npx, 'i') # ect = counts of overlaps tj = np.empty(npx, 'i') # tj = temporary for sorting n1 = frame1.pixels[labels1].header["nlabel"] n2 = frame2.pixels[labels2].header["nlabel"] tmp = np.empty(max(n1, n2) + 1, 'i') # for histogram nedge = cImageD11.compress_duplicates(row, col, ect, tj, tmp) # overwrites row/col in place crow = row[:nedge] ccol = col[:nedge] cdata = ect[:nedge] cedges = scipy.sparse.coo_matrix((cdata, (crow, ccol))) return cedges
def overlaps(self, other): """ figures out which label of self matches which label of other Returns sparse array of: label in self (row) label in other (col) number of shared pixels (data) """ ki = np.empty(self.frame.nnz, 'i') kj = np.empty(other.frame.nnz, 'i') npx = cImageD11.sparse_overlaps(self.frame.row, self.frame.col, ki, other.frame.row, other.frame.col, kj) # self.data and other.data filled during init row = self.labels[ki[:npx]] # my labels col = other.labels[kj[:npx]] # your labels ect = np.empty(npx, 'i') # ect = counts of overlaps tj = np.empty(npx, 'i') # tj = temporary for sorting tmp = np.empty(max(self.nlabel, other.nlabel) + 1, 'i') # for histogram nedge = cImageD11.compress_duplicates(row, col, ect, tj, tmp) # overwrites row/col in place crow = row[:nedge] ccol = col[:nedge] cdata = ect[:nedge] cedges = scipy.sparse.coo_matrix((cdata, (crow, ccol))) return cedges
def test1(self): np.random.seed(42) N = self.N a = np.random.random(N * 2) i, j = (a * 10).astype('i').reshape(2, N) oj = np.empty(N, 'i') oi = np.empty(N, 'i') tmp = np.empty(20, 'i') d = np.ones(N, int) r, c, d = _sum_duplicates(i, j, d) n = cImageD11.compress_duplicates(i, j, oi, oj, tmp) self.assertTrue(n == len(r)) self.assertTrue((r == i[:n]).all()) self.assertTrue((c == j[:n]).all()) self.assertTrue((d == oi[:n]).all())
def test2(self): np.random.seed(42) N = self.N a = np.random.random(N * 2) i, j = (a * 10).astype('i').reshape(2, N) oj = np.empty(N, 'i') oi = np.empty(N, 'i') tmp = np.empty(20, 'i') d = np.ones(N, int) coo = scipy.sparse.coo_matrix((d, (i.copy(), j.copy()))) csr = coo.tocsr() coo = csr.tocoo() #print("",i,"\n",j) n = cImageD11.compress_duplicates(i, j, oi, oj, tmp) self.assertEqual( cImageD11.sparse_is_sorted(i[:n].astype(np.uint16), j[:n].astype(np.uint16)), 0) r = coo.row c = coo.col d = coo.data self.assertTrue(n == len(r)) self.assertTrue((r == i[:n]).all()) self.assertTrue((c == j[:n]).all()) self.assertTrue((d == oi[:n]).all())