Exemplo n.º 1
0
 def cplabel(self, threshold=0):
     """ Label pixels using the connectedpixels assigment code
     Fills in:
        self.nlabels = number of peaks per frame
        self.labels  = peak labels (should be unique)
        self.total_labels = total number of peaks
     """
     self.nlabels = np.zeros(len(self.nnz), np.int32)
     self.labels = np.zeros(len(self.row), "i")
     nl = 0
     # TODO: run this in parallel with threads?
     for i, npx in enumerate(self.nnz):
         s = self.ipt[i]
         e = self.ipt[i + 1]
         if npx > 0:
             self.nlabels[i] = cImageD11.sparse_connectedpixels(
                 self.intensity[s:e], self.row[s:e], self.col[s:e],
                 threshold, self.labels[s:e])
             # zero label is the background!
             self.labels[s:e] = np.where(self.labels[s:e] > 0,
                                         self.labels[s:e] + nl, 0)
         else:
             self.nlabels[i] = 0
         nl += self.nlabels[i]
     self.total_labels = nl
Exemplo n.º 2
0
    def testOK(self):
        #import pdb; pdb.set_trace()
        i = self.s.row.astype(np.uint16)
        j = self.s.col.astype(np.uint16)
        v = self.s.data
        self.assertTrue((self.s.todense() == self.a).all())
        dl = np.zeros(self.a.shape, 'i')
        nd = cImageD11.connectedpixels(self.a, dl, self.threshold)
        dstart = timer()
        nd = cImageD11.connectedpixels(self.a, dl, self.threshold)
        dend = timer()
        sl = np.zeros(v.shape, 'i')
        ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
        sbegin = timer()
        ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
        send = timer()
        sld = scipy.sparse.coo_matrix((sl, (i, j)), shape=self.a.shape)
        testcase = (sld.todense() == dl).all()

        densetime = dend - dstart
        sparsetime = send - sbegin
        print(
            "sp_cp: %.3f ms vs %.3f ms, ratio %.1f nnz %d" %
            (1e3 * sparsetime, 1e3 * densetime, densetime / sparsetime,
             sld.nnz), " t=%.0f" % (self.threshold))
        if ~testcase:
            import pylab as pl
            pl.ioff()
            pl.figure()
            pl.subplot(311)
            pl.title("t=%f" % (self.threshold))
            pl.imshow(dl, interpolation='nearest')
            pl.colorbar()
            pl.subplot(312)
            pl.imshow(sld.todense(), interpolation='nearest')
            pl.colorbar()
            pl.subplot(313)
            pl.imshow(sld.todense() - dl, interpolation='nearest')
            pl.colorbar()
            pl.show()
        self.assertTrue(testcase)
Exemplo n.º 3
0
 def testsplat(self):
     i = self.s.row.astype(np.uint16)
     j = self.s.col.astype(np.uint16)
     v = self.s.data
     self.assertTrue((self.s.todense() == self.a).all())
     ni, nj = self.s.shape
     z = np.empty((ni + 2) * (nj + 2), np.int32)
     sl = np.zeros(v.shape, 'i')
     sls = np.zeros(v.shape, 'i')
     ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
     s0 = timer()
     ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
     s1 = timer()
     ns = cImageD11.sparse_connectedpixels_splat(v, i, j, self.threshold,
                                                 sls, z, ni, nj)
     s2 = timer()
     ns = cImageD11.sparse_connectedpixels_splat(v, i, j, self.threshold,
                                                 sls, z, ni, nj)
     s3 = timer()
     sld = scipy.sparse.coo_matrix((sl, (i, j)), shape=self.a.shape)
     testcase = (sls == sl).all()
     print("\nsparse_connectedpixels       %.3f ms" % (1e3 * (s1 - s0)),
           "\nsparse_connectedpixels_splat %.3f ms" % (1e3 * (s3 - s2)))
     if ~testcase:
         import pylab as pl
         pl.ioff()
         pl.figure()
         pl.subplot(311)
         pl.title("t=%f" % (self.threshold))
         pl.imshow(dl, interpolation='nearest')
         pl.colorbar()
         pl.subplot(312)
         pl.imshow(sld.todense(), interpolation='nearest')
         pl.colorbar()
         pl.subplot(313)
         pl.imshow(sld.todense() - dl, interpolation='nearest')
         pl.colorbar()
         pl.show()
     self.assertTrue(testcase)
Exemplo n.º 4
0
 def __init__(self, frame, threshold, name='data'):
     """
     guesses threshold as min val-1 or given threshold
     adds an array named [cplabels_tx.xxx]
     self.nclabel number of connected objects
     """
     sparse_labelling.__init__(self, frame, name=name)
     if threshold is None:
         self.threshold = self.frame.pixels[name].min() - 1
     else:
         self.threshold = threshold
     self.nlabel = cImageD11.sparse_connectedpixels(
         self.frame.pixels[self.name], self.frame.row, self.frame.col,
         self.threshold, self.labels)
Exemplo n.º 5
0
 def testsplat(self):
     i = self.s.row.astype(np.uint16)
     j = self.s.col.astype(np.uint16)
     v = self.s.data
     self.assertTrue((self.s.todense() == self.a).all())
     ni, nj = self.s.shape
     z = np.empty((ni + 2) * (nj + 2), np.int32)
     sl = np.zeros(v.shape, 'i')
     sls = np.zeros(v.shape, 'i')
     ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
     s0 = timer()
     ns = cImageD11.sparse_connectedpixels(v, i, j, self.threshold, sl)
     s1 = timer()
     ns = cImageD11.sparse_connectedpixels_splat(v, i, j, self.threshold,
                                                 sls, z, ni, nj)
     s2 = timer()
     ns = cImageD11.sparse_connectedpixels_splat(v, i, j, self.threshold,
                                                 sls, z, ni, nj)
     s3 = timer()
     sld = scipy.sparse.coo_matrix((sl, (i, j)), shape=self.a.shape)
     testcase = (sls == sl).all()
     print("\nsparse_connectedpixels       %.3f ms" % (1e3 * (s1 - s0)),
           "\nsparse_connectedpixels_splat %.3f ms" % (1e3 * (s3 - s2)))
     self.assertTrue(testcase)
Exemplo n.º 6
0
def sparse_connected_pixels( frame,
                             label_name="connectedpixels",
                             data_name="intensity",
                             threshold=None ):
    """
    frame = a sparse frame
    label_name = the array to save labels to in that frame
    data_name = an array in that frame
    threshold = float value or take data.threshold
    """
    labels = np.zeros( frame.nnz, "i" )
    if threshold is None:
        threshold = frame.meta[data_name]["threshold"]
    nlabel = cImageD11.sparse_connectedpixels(
        frame.pixels[data_name], frame.row, frame.col,
        threshold,  labels )
    frame.set_pixels( label_name, labels, { 'nlabel' : nlabel } )
Exemplo n.º 7
0
def sparse_connected_pixels(frame,
                            label_name="connectedpixels",
                            data_name="intensity",
                            threshold=None):
    """
    frame = a sparse frame
    label_name = the array to save labels to in that frame
    data_name = an array in that frame
    threshold = float value or take data.threshold
    """
    labels = np.zeros(frame.nnz, "i")
    if threshold is None:
        threshold = frame.pixels[data_name].header["threshold"]
    nlabel = cImageD11.sparse_connectedpixels(frame.pixels[data_name].data,
                                              frame.row, frame.col, threshold,
                                              labels)
    px = Container(
        labels, **{
            "data_name": data_name,
            "threshold": threshold,
            "nlabel": nlabel
        })
    frame.set_pixels(label_name, px)