def __call__(self, img): # choose the pixels that are != 0 and not masked nnz = select(img, self.msk, self.row, self.col, self.val, cut=self.cut) # copy these into a "sparse" format s = sparseframe.sparse_frame(self.row[:nnz], self.col[:nnz], img.shape) s.set_pixels("intensity", self.val[:nnz]) # label them according to the connected objects sparseframe.sparse_connected_pixels(s, threshold=self.cut, data_name='intensity', label_name='cp') # only keep spots with more than 3 pixels ... mom = sparseframe.sparse_moments(s, intensity_name='intensity', labels_name='cp') npx = mom[:, cImageD11.s2D_1] pxcounts = npx[s.pixels['cp'] - 1] msk = pxcounts > self.pixels_in_spot if msk.sum() == 0: return None r = s.mask(msk) sparseframe.sparse_connected_pixels(r, threshold=self.cut, label_name='connectedpixels', data_name='intensity') return r
def getframe(self, i): assert i >= 0 and i < self.nframes ilo = self.ipt[i] ihi = self.ipt[i + 1] f = sparseframe.sparse_frame(self.row[ilo:ihi], self.col[ilo:ihi], shape=self.shape) f.set_pixels("intensity", self.sig[ilo:ihi]) # print("%d"%(i),end=" ") # sys.stdout.flush() return f
def choose_parallel(args): """reads a frame and sends back a sparse frame""" h5name, address, frame_num = args frm = get_dset(h5name, address)[frame_num] if choose_parallel.cache is None: # cache the mallocs on this function. Should be one per process row = np.empty(OPTIONS.MASK.size, np.uint16) col = np.empty(OPTIONS.MASK.size, np.uint16) val = np.empty(OPTIONS.MASK.size, frm.dtype) choose_parallel.cache = row, col, val else: row, col, val = choose_parallel.cache nnz = select(frm, OPTIONS.MASK, row, col, val, OPTIONS.CUT) if nnz == 0: sf = None else: if nnz > OPTIONS.HOWMANY: nnz = top_pixels(nnz, row, col, val, OPTIONS.HOWMANY, OPTIONS.THRESHOLDS) # Now get rid of the single pixel 'peaks' # (for the mallocs, data is copied here) s = sparseframe.sparse_frame(row[:nnz].copy(), col[:nnz].copy(), frm.shape) s.set_pixels("intensity", val[:nnz].copy()) if OPTIONS.PIXELS_IN_SPOT <= 1: sf = s else: # label them according to the connected objects sparseframe.sparse_connected_pixels(s, threshold=OPTIONS.CUT, data_name="intensity", label_name="cp") # only keep spots with more than 3 pixels ... mom = sparseframe.sparse_moments(s, intensity_name="intensity", labels_name="cp") npx = mom[:, cImageD11.s2D_1] pxcounts = npx[s.pixels["cp"] - 1] pxmsk = pxcounts >= OPTIONS.PIXELS_IN_SPOT if pxmsk.sum() == 0: sf = None else: sf = s.mask(pxmsk) return frame_num, sf
def apply_threshold(series, nsigma): """ converts the data to sparse array """ msk = None for img in series: if msk is None: msk = np.empty(img.data.shape, np.uint8) cln = np.empty(img.data.shape, np.uint8) tmp = np.empty(img.data.shape[0], 'i') row = np.empty(img.data.size, np.uint16) col = np.empty(img.data.size, np.uint16) a, s = cImageD11.array_mean_var_cut(img.smoothed, nsigma) nnz = cImageD11.make_clean_mask(img.smoothed, a + nsigma * s, msk, cln) cImageD11.mask_to_coo(cln, row[:nnz], col[:nnz], tmp) header = {"filename": img.filename} img.mask = cln intensity = sparseframe.Container(img.signal[cln > 0], **header) img.sparseframe = sparseframe.sparse_frame( row[:nnz], col[:nnz], img.data.shape, itype=np.uint16, pixels={"intensity": intensity}) yield img
def newpks( hf, scans=None, npixels=1, monitor=None, monval=1e3, ): """ do a peak search in a sparse frame series """ titles = 's_raw f_raw avg_intensity Number_of_pixels sum_intensity omega dty'.split( ) c = {} for name in titles: c[name] = [] # using the python file open overcomes some threading crap with h5py.File(open(hf, "rb"), 'r') as hin: # scan numbers if scans is None: scans = list(hin['/']) for scan in scans: gin = hin[scan] shape = gin.attrs['shape0'], gin.attrs['shape1'] # import pdb; pdb.set_trace() omega = gin['measurement/rot'][:] difty = gin['instrument/positioners/dty'][()] row = gin['row'][()] col = gin['col'][()] sig = gin['intensity'][()] if monitor is not None: mon = monval / gin[monitor][()] ipt = np.cumsum(gin['nnz'][:]) iprev = 0 for k, nnz in enumerate(gin['nnz'][()]): inext = iprev + nnz if nnz == 0: continue f = sparseframe.sparse_frame(row[iprev:inext], col[iprev:inext], shape) f.set_pixels("intensity", sig[iprev:inext]) sparseframe.sparse_connected_pixels(f, threshold=0.1) pks = sparseframe.sparse_moments(f, "intensity", "connectedpixels") # sparseframe.sparse_localmax(f) # pks = sparseframe.sparse_moments(f, "intensity", "localmax") s, f, a, n = moments(pks) m = n > npixels c['s_raw'].append(s[m]) c['f_raw'].append(f[m]) if monitor is not None: c['avg_intensity'].append(a[m] * mon[k]) c['sum_intensity'].append(a[m] * n[m] * mon[k]) else: c['avg_intensity'].append(a[m]) c['sum_intensity'].append(a[m] * n[m]) c['Number_of_pixels'].append(n[m]) npk = m.sum() c['omega'].append(np.full(npk, omega[k])) c['dty'].append(np.full(npk, difty)) iprev = inext for t in titles: c[t] = np.concatenate(c[t]) return c