def test01(ntest, prefix='fig-v01') : """Test for radial 1-d binning of entire image. """ from time import time import algos.graph.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) t0_sec = time() iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() print 'Time to retrieve geometry %.3f sec' % (time()-t0_sec) t0_sec = time() hp = HPolar(X, Y, mask, nradbins=500, nphibins=1) # v1 print 'HPolar initialization time %.3f sec' % (time()-t0_sec) t0_sec = time() nda, title = arr, None if ntest == 1 : nda, title = arr, 'averaged data' elif ntest == 2 : nda, title = hp.pixel_rad(), 'pixel radius value' elif ntest == 3 : nda, title = hp.pixel_phi(), 'pixel phi value' elif ntest == 4 : nda, title = hp.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 5 : nda, title = hp.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 6 : nda, title = hp.pixel_iseq() + 2, 'pixel sequential (rad and phi) bin index' elif ntest == 7 : nda, title = mask, 'mask' elif ntest == 8 : nda, title = hp.pixel_avrg(nda), 'averaged radial intensity' elif ntest == 9 : nda, title = hp.pixel_avrg_interpol(arr) * mask , 'interpolated radial intensity' else : print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time()-t0_sec) img = img_from_pixel_arrays(iX, iY, nda) if not ntest in (21,) else nda[100:300,:] da, ds = None, None colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' if ntest in (2,3,4,5,6,7) : da = ds = (nda.min()-1., nda.max()+1.) else : ave, rms = nda.mean(), nda.std() da = ds = (ave-2*rms, ave+3*rms) gg.plotImageLarge(img, amp_range=da, figsize=(14,12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title
def on_but_roi_convert(self): self.setStatus(1, 'Convert image to ndarray') mcbits = self.sensor_mask_cbits.value() gfname = self.fname_geometry.value() ifname = self.fname_roi_mask_img.value() ofname = self.fname_roi_mask_nda.value() tfname = self.fname_roi_mask_nda_tst.value() msg = '\n Convert ROI mask image: %s\n to ndarray: %s\n using geometry: %s' % \ ( ifname, ofname, gfname ) logger.info(msg, __name__) geometry = GeometryAccess(gfname, 0) iX, iY = geometry.get_pixel_coord_indexes() msg = 'Pixel index array iX, iY shapes: %s, %s' % (str( iX.shape), str(iY.shape)) logger.info(msg, __name__) ifext = os.path.splitext(ifname)[1] ofext = os.path.splitext(ofname)[1] mask_roi = np.load(ifname) if ifext == '.npy' else np.loadtxt( ifname, dtype=np.uint16) mask_nda = np.array([mask_roi[r, c] for r, c in zip(iX, iY)]) # 155 msec if mcbits: mask_nda *= geometry.get_pixel_mask(mbits=mcbits) img_mask_test = img_from_pixel_arrays(iX, iY, W=mask_nda) if ofext == '.npy': np.save(ofname, mask_nda) else: mask_nda.shape = [iX.size / iX.shape[-1], iX.shape[-1]] logger.info( 'Mask ndarray is re-shape for saving in txt to 2-d: %s' % str(mask_nda.shape), __name__) np.savetxt(ofname, mask_nda, fmt='%d', delimiter=' ') logger.info('Mask ndarray is saved in the file %s' % ofname, __name__) self.setStatus(1, 'Test: reconstruct image from mask ndarray...') tfext = os.path.splitext(tfname)[1] if tfext == '.npy': np.save(tfname, img_mask_test) else: np.savetxt(tfname, img_mask_test, fmt='%d', delimiter=' ') logger.info( 'Test-image generated from mask ndarray is saved in file %s' % tfname, __name__) self.setStatus(0)
def on_but_roi_convert (self): self.setStatus(1, 'Convert image to ndarray') mcbits = self.sensor_mask_cbits.value() gfname = self.fname_geometry.value() ifname = self.fname_roi_mask_img.value() ofname = self.fname_roi_mask_nda.value() tfname = self.fname_roi_mask_nda_tst.value() msg = '\n Convert ROI mask image: %s\n to ndarray: %s\n using geometry: %s' % \ ( ifname, ofname, gfname ) logger.info(msg, __name__) geometry = GeometryAccess(gfname, 0) iX, iY = geometry.get_pixel_coord_indexes() msg = 'Pixel index array iX, iY shapes: %s, %s' % (str(iX.shape), str(iY.shape)) logger.info(msg, __name__) ifext = os.path.splitext(ifname)[1] ofext = os.path.splitext(ofname)[1] mask_roi = np.load(ifname) if ifext == '.npy' else np.loadtxt(ifname, dtype=np.uint16) mask_nda = np.array( [mask_roi[r,c] for r,c in zip(iX, iY)] ) # 155 msec if mcbits : mask_nda *= geometry.get_pixel_mask(mbits=mcbits) img_mask_test = img_from_pixel_arrays(iX, iY, W=mask_nda) if ofext == '.npy' : np.save(ofname, mask_nda) else : mask_nda.shape = [iX.size/iX.shape[-1],iX.shape[-1]] logger.info('Mask ndarray is re-shape for saving in txt to 2-d: %s' % str(mask_nda.shape), __name__) np.savetxt(ofname, mask_nda, fmt='%d', delimiter=' ') logger.info('Mask ndarray is saved in the file %s' % ofname, __name__) self.setStatus(1, 'Test: reconstruct image from mask ndarray...') tfext = os.path.splitext(tfname)[1] if tfext == '.npy' : np.save(tfname, img_mask_test) else : np.savetxt(tfname, img_mask_test, fmt='%d', delimiter=' ') logger.info('Test-image generated from mask ndarray is saved in file %s' % tfname, __name__) self.setStatus(0)
def reco_image_from_ndarray(self, gfname, afname): #mcbits = self.sensor_mask_cbits.value() msg = 'Reconstruct image from\n geometry: %s\n and ndarray: %s' % \ ( gfname, afname ) logger.info(msg, __name__) geometry = GeometryAccess(gfname, 0) iX, iY = geometry.get_pixel_coord_indexes() afext = '' if afname is None else os.path.splitext(afname)[1] nda = np.ones(iX.shape, dtype=np.uint16) if afname is None else \ np.load(afname) if afext == '.npy' else \ np.loadtxt(afname) #, dtype=np.uint16) nda.shape = iX.shape #if mcbits : nda *= geometry.get_pixel_mask(mbits=mcbits) return img_from_pixel_arrays(iX, iY, W=nda)
def reco_image_from_ndarray (self, gfname, afname): #mcbits = self.sensor_mask_cbits.value() msg = 'Reconstruct image from\n geometry: %s\n and ndarray: %s' % \ ( gfname, afname ) logger.info(msg, __name__) geometry = GeometryAccess(gfname, 0) iX, iY = geometry.get_pixel_coord_indexes() afext = '' if afname is None else os.path.splitext(afname)[1] nda = np.ones(iX.shape, dtype=np.uint16) if afname is None else \ np.load(afname) if afext == '.npy' else \ np.loadtxt(afname) #, dtype=np.uint16) nda.shape = iX.shape #if mcbits : nda *= geometry.get_pixel_mask(mbits=mcbits) return img_from_pixel_arrays(iX, iY, W=nda)
def test03(ntest, prefix='fig-v01') : """Test for 2-d binning of the restricted rad-phi range of entire image """ from time import time import algos.histo.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() t0_sec = time() rb = RadialBkgd(X, Y, mask, nradbins=200, nphibins=32, phiedges=(-20, 240), radedges=(10000,80000)) if ntest in (51,52)\ else RadialBkgd(X, Y, mask, nradbins= 5, nphibins= 8, phiedges=(-20, 240), radedges=(10000,80000)) #rb = RadialBkgd(X, Y, mask, nradbins=3, nphibins=8, phiedges=(240, -20), radedges=(80000,10000)) # v3 print 'RadialBkgd initialization time %.3f sec' % (time()-t0_sec) #print 'npixels_per_bin:', rb.npixels_per_bin() #print 'intensity_per_bin:', rb.intensity_per_bin(arr) #print 'average_per_bin:', rb.average_per_bin(arr) t0_sec = time() nda, title = arr, None if ntest == 41 : nda, title = arr, 'averaged data' elif ntest == 42 : nda, title = rb.pixel_rad(), 'pixel radius value' elif ntest == 43 : nda, title = rb.pixel_phi(), 'pixel phi value' elif ntest == 44 : nda, title = rb.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 45 : nda, title = rb.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 46 : nda, title = rb.pixel_iseq() + 2, 'pixel sequential (rad and phi) bin index' elif ntest == 47 : nda, title = mask, 'mask' elif ntest == 48 : nda, title = rb.pixel_avrg(nda), 'averaged radial background' elif ntest == 49 : nda, title = rb.subtract_bkgd(nda) * mask, 'background-subtracted data' elif ntest == 50 : nda, title = rb.bin_avrg_rad_phi(nda),'r-phi' elif ntest == 51 : nda, title = rb.pixel_avrg_interpol(nda), 'averaged radial interpolated background' elif ntest == 52 : nda, title = rb.subtract_bkgd_interpol(nda) * mask, 'interpol-background-subtracted data' else : print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time()-t0_sec) img = img_from_pixel_arrays(iX, iY, nda) if not ntest in (50,) else nda # [100:300,:] colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' 'gray' da = (nda.min()-1, nda.max()+1) ds = da if ntest in (41,48,49,50,51) : ave, rms = nda.mean(), nda.std() da = ds = (ave-2*rms, ave+3*rms) elif ntest in (52,) : colmap = 'gray' ds = da = (-20, 20) gg.plotImageLarge(img, amp_range=da, figsize=(14,12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title
def test03(ntest, prefix='fig-v01'): """Test for 2-d binning of the restricted rad-phi range of entire image """ from time import time import algos.histo.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() t0_sec = time() rb = RadialBkgd(X, Y, mask, nradbins=200, nphibins=32, phiedges=(-20, 240), radedges=(10000,80000)) if ntest in (51,52)\ else RadialBkgd(X, Y, mask, nradbins= 5, nphibins= 8, phiedges=(-20, 240), radedges=(10000,80000)) #rb = RadialBkgd(X, Y, mask, nradbins=3, nphibins=8, phiedges=(240, -20), radedges=(80000,10000)) # v3 print 'RadialBkgd initialization time %.3f sec' % (time() - t0_sec) #print 'npixels_per_bin:', rb.npixels_per_bin() #print 'intensity_per_bin:', rb.intensity_per_bin(arr) #print 'average_per_bin:', rb.average_per_bin(arr) t0_sec = time() nda, title = arr, None if ntest == 41: nda, title = arr, 'averaged data' elif ntest == 42: nda, title = rb.pixel_rad(), 'pixel radius value' elif ntest == 43: nda, title = rb.pixel_phi(), 'pixel phi value' elif ntest == 44: nda, title = rb.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 45: nda, title = rb.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 46: nda, title = rb.pixel_iseq( ) + 2, 'pixel sequential (rad and phi) bin index' elif ntest == 47: nda, title = mask, 'mask' elif ntest == 48: nda, title = rb.pixel_avrg(nda), 'averaged radial background' elif ntest == 49: nda, title = rb.subtract_bkgd(nda) * mask, 'background-subtracted data' elif ntest == 50: nda, title = rb.bin_avrg_rad_phi(nda), 'r-phi' elif ntest == 51: nda, title = rb.pixel_avrg_interpol( nda), 'averaged radial interpolated background' elif ntest == 52: nda, title = rb.subtract_bkgd_interpol( nda) * mask, 'interpol-background-subtracted data' else: print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time() - t0_sec) img = img_from_pixel_arrays( iX, iY, nda) if not ntest in (50, ) else nda # [100:300,:] colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' 'gray' da = (nda.min() - 1, nda.max() + 1) ds = da if ntest in (41, 48, 49, 50, 51): ave, rms = nda.mean(), nda.std() da = ds = (ave - 2 * rms, ave + 3 * rms) elif ntest in (52, ): colmap = 'gray' ds = da = (-20, 20) gg.plotImageLarge(img, amp_range=da, figsize=(14, 12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title
def test01(ntest, prefix='fig-v01'): """Test for radial 1-d binning of entire image. """ from time import time import algos.histo.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) t0_sec = time() iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() print 'Time to retrieve geometry %.3f sec' % (time() - t0_sec) t0_sec = time() rb = RadialBkgd(X, Y, mask, nradbins=500, nphibins=1) # v1 print 'RadialBkgd initialization time %.3f sec' % (time() - t0_sec) t0_sec = time() nda, title = arr, None if ntest == 1: nda, title = arr, 'averaged data' elif ntest == 2: nda, title = rb.pixel_rad(), 'pixel radius value' elif ntest == 3: nda, title = rb.pixel_phi(), 'pixel phi value' elif ntest == 4: nda, title = rb.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 5: nda, title = rb.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 6: nda, title = rb.pixel_iseq( ) + 2, 'pixel sequential (rad and phi) bin index' elif ntest == 7: nda, title = mask, 'mask' elif ntest == 8: nda, title = rb.pixel_avrg(nda), 'averaged radial background' elif ntest == 9: nda, title = rb.subtract_bkgd(nda) * mask, 'background-subtracted data' else: t1_sec = time() pf = polarization_factor(rb.pixel_rad(), rb.pixel_phi(), 94e3) # Z=94mm print 'Time to evaluate polarization correction factor %.3f sec' % ( time() - t1_sec) if ntest == 10: nda, title = pf, 'polarization factor' elif ntest == 11: nda, title = arr * pf, 'polarization-corrected averaged data' elif ntest == 12: nda, title = rb.subtract_bkgd( arr * pf) * mask, 'polarization-corrected background subtracted data' elif ntest == 13: nda, title = rb.pixel_avrg( arr * pf), 'polarization-corrected averaged radial background' elif ntest == 14: nda, title = rb.pixel_avrg_interpol( arr * pf ) * mask, 'polarization-corrected interpolated radial background' elif ntest == 15: nda, title = rb.subtract_bkgd_interpol( arr * pf ) * mask, 'polarization-corrected interpolated radial background-subtracted data' else: print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time() - t0_sec) img = img_from_pixel_arrays( iX, iY, nda) if not ntest in (21, ) else nda[100:300, :] da, ds = None, None colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' if ntest in (2, 3, 4, 5, 6, 7): da = ds = (nda.min() - 1., nda.max() + 1.) if ntest in (12, 15): ds = da = (-20, 20) colmap = 'gray' else: ave, rms = nda.mean(), nda.std() da = ds = (ave - 2 * rms, ave + 3 * rms) gg.plotImageLarge(img, amp_range=da, figsize=(14, 12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title
def test02(ntest, prefix='fig-v01') : """Test for 2-d (default) binning of the rad-phi range of entire image """ #from Detector.GlobalUtils import print_ndarr from time import time import algos.graph.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() t0_sec = time() #hp = HPolar(X, Y, mask) # v0 hp = HPolar(X, Y, mask, nradbins=500) # , nphibins=8, phiedges=(-20, 240), radedges=(10000,80000)) print 'HPolar initialization time %.3f sec' % (time()-t0_sec) #print 'bin_number_of_pixels:', hp.bin_number_of_pixels() #print 'bin_intensity:', hp.bin_intensity(arr) #print 'bin_avrg:', hp.bin_avrg(arr) t0_sec = time() nda, title = arr, None if ntest == 21 : nda, title = arr, 'averaged data' elif ntest == 24 : nda, title = hp.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 25 : nda, title = hp.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 26 : nda, title = hp.pixel_iseq() + 2, 'pixel sequential (rad and phi) bin index' #elif ntest == 27 : nda, title = mask, 'mask' elif ntest == 28 : nda, title = hp.pixel_avrg(nda), 'averaged radial intensity' elif ntest == 29 : nda, title = hp.pixel_avrg_interpol(nda), 'averaged radial interpolated intensity' elif ntest == 30 : nda, title = hp.bin_avrg_rad_phi(nda),'r-phi' else : print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time()-t0_sec) img = img_from_pixel_arrays(iX, iY, nda) if not ntest in (30,) else nda # [100:300,:] colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' 'gray' da = (nda.min()-1, nda.max()+1) ds = da if ntest in (21,28,29,30) : ave, rms = nda.mean(), nda.std() da = ds = (ave-2*rms, ave+3*rms) gg.plotImageLarge(img, amp_range=da, figsize=(14,12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title
def test02(ntest, prefix='fig-v01'): """Test for 2-d (default) binning of the rad-phi range of entire image """ #from Detector.GlobalUtils import print_ndarr from time import time import algos.graph.GlobalGraphics as gg from PSCalib.GeometryAccess import img_from_pixel_arrays arr, geo = data_geo(ntest) iX, iY = geo.get_pixel_coord_indexes() X, Y, Z = geo.get_pixel_coords() mask = geo.get_pixel_mask(mbits=0377).flatten() t0_sec = time() #hp = HPolar(X, Y, mask) # v0 hp = HPolar(X, Y, mask, nradbins=500 ) # , nphibins=8, phiedges=(-20, 240), radedges=(10000,80000)) print 'HPolar initialization time %.3f sec' % (time() - t0_sec) #print 'bin_number_of_pixels:', hp.bin_number_of_pixels() #print 'bin_intensity:', hp.bin_intensity(arr) #print 'bin_avrg:', hp.bin_avrg(arr) t0_sec = time() nda, title = arr, None if ntest == 21: nda, title = arr, 'averaged data' elif ntest == 24: nda, title = hp.pixel_irad() + 2, 'pixel radial bin index' elif ntest == 25: nda, title = hp.pixel_iphi() + 2, 'pixel phi bin index' elif ntest == 26: nda, title = hp.pixel_iseq( ) + 2, 'pixel sequential (rad and phi) bin index' #elif ntest == 27 : nda, title = mask, 'mask' elif ntest == 28: nda, title = hp.pixel_avrg(nda), 'averaged radial intensity' elif ntest == 29: nda, title = hp.pixel_avrg_interpol( nda), 'averaged radial interpolated intensity' elif ntest == 30: nda, title = hp.bin_avrg_rad_phi(nda), 'r-phi' else: print 'Test %d is not implemented' % ntest return print 'Get %s n-d array time %.3f sec' % (title, time() - t0_sec) img = img_from_pixel_arrays( iX, iY, nda) if not ntest in (30, ) else nda # [100:300,:] colmap = 'jet' # 'cubehelix' 'cool' 'summer' 'jet' 'winter' 'gray' da = (nda.min() - 1, nda.max() + 1) ds = da if ntest in (21, 28, 29, 30): ave, rms = nda.mean(), nda.std() da = ds = (ave - 2 * rms, ave + 3 * rms) gg.plotImageLarge(img, amp_range=da, figsize=(14, 12), title=title, cmap=colmap) gg.save('%s-%02d-img.png' % (prefix, ntest)) gg.hist1d(nda, bins=None, amp_range=ds, weights=None, color=None, show_stat=True, log=False, \ figsize=(6,5), axwin=(0.18, 0.12, 0.78, 0.80), \ title=None, xlabel='Pixel value', ylabel='Number of pixels', titwin=title) gg.save('%s-%02d-his.png' % (prefix, ntest)) gg.show() print 'End of test for %s' % title