def grab2d( im, dark, nsigma=3., blocksize=256 ): """ returns the fabio image adding .corr = corrected data .sparseframe = segmented spots """ data = np.empty( im.size, np.float32 ) # corrected data msk = np.empty( im.shape, 'b' ) # mask csk = np.empty( im.shape, 'b' ) # cleaned mask # subtract the background shape0 = im.shape data.shape = data.size dark.shape = dark.size im.shape = im.size cImageD11.uint16_to_float_darksub( data, dark, im ) # compute the mean and std of the data trimming at nsigma avg, std = cImageD11.array_mean_var_cut( data, nsigma ) threshold = avg + nsigma * std # remove the readout drift data.shape = im.size // blocksize, blocksize cImageD11.frelon_lines( data, threshold ) data.shape = shape0 dark.shape = shape0 im.shape = shape0 # threshold and clean nnz = cImageD11.make_clean_mask( data, threshold, msk, csk ) row = np.empty( nnz, np.uint16 ) col = np.empty( nnz, np.uint16 ) tmp = np.empty( shape0[0], 'i' ) cImageD11.mask_to_coo( csk, row, col, tmp ) header = { 'threshold' : threshold, } spar = sparseframe.from_data_mask( csk, data, header ) return spar, data
def threshold_image(im, nsigma=2, fix_lines=False, mskmem=None, block=256, verbose=0): """ Process one single image (numpy array) """ if fix_lines: im = fix_frelon_lines(im, block=block, nsigma=nsigma) if verbose: print("fixed lines") cut, val = sigma_cut(im, nsigma) if verbose: print("cut,val: ", cut, val) mask = im > cut if verbose: print("mask raw:", mask.sum()) npx, clean_mask = binary_clean(mask, out=mskmem) i = np.empty(npx, np.uint16) j = np.empty(npx, np.uint16) tmp = np.empty(im.shape[0], np.int32) ok = cImageD11.mask_to_coo(clean_mask.astype(np.int8), i, j, tmp) if verbose: print("mask clean", clean_mask.sum()) assert ok == 0 print(i.shape, j.shape, clean_mask.sum()) frm = sparse_frame(i, j, im.shape, itype=np.uint16, pixels={'data': im[clean_mask.astype(np.bool)]}) return frm
def from_data_mask(mask, data, header): """ Create a sparse from a dense array """ assert mask.shape == data.shape # using uint16 here - perhaps make this general in the future # ... but not for now assert data.shape[0] < pow(2, 16) - 1 assert data.shape[1] < pow(2, 16) - 1 nnz = (mask > 0).sum() tmp = np.empty(data.shape[0], 'i') # tmp hold px per row cumsums row = np.empty(nnz, np.uint16) col = np.empty(nnz, np.uint16) cImageD11.mask_to_coo(mask, row, col, tmp) intensity = data[mask > 0] # intensity.attrs = dict(header) # FIXME USE xarray ? spf = sparse_frame(row, col, data.shape, itype=np.uint16) spf.set_pixels("intensity", intensity, dict(header)) return spf
def test_coo2(self): npx = self.src.sum() i = np.empty(npx, np.uint16) j = np.empty(npx, np.uint16) tmp = np.empty(self.target.shape[0], np.int32) ok = cImageD11.mask_to_coo(self.src.astype(np.int8), i, j, tmp) self.assertTrue(ok == 0) pi, pj = py_coo(self.src) self.assertTrue((pi == i).all()) self.assertTrue((pj == j).all())
def from_data_mask(mask, data, header): """ Create a sparse from a dense array """ assert mask.shape == data.shape # using uint16 here - perhaps make this general in the future # ... but not for now assert data.shape[0] < pow(2, 16) - 1 assert data.shape[1] < pow(2, 16) - 1 nnz = (mask > 0).sum() tmp = np.empty(data.shape[0], 'i') # tmp hold px per row cumsums row = np.empty(nnz, np.uint16) col = np.empty(nnz, np.uint16) cImageD11.mask_to_coo(mask, row, col, tmp) intensity = data[mask > 0] return sparse_frame(row, col, data.shape, itype=np.uint16, pixels={"intensity": Container(intensity, **header)})
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