def filter_frames(self, data): print '############ ONE FILTER FRAME ########################' bc = [0, 0] distance, bc[0], bc[1], yaw, roll, px, px = self.ai_params t1 = time.time() data = data[0].astype(np.float) data -= np.min(data) mData = self.params[1] # print self.params[2] # foo = self.params[2] ai = AzimuthalIntegrator() ai.setFit2D(distance, bc[0], bc[1], yaw, roll, px, px, None) ai.set_wavelength(self.wl) foo = units.to_unit(units.TTH) twotheta = ai.__getattribute__(foo.center)( self.sh) # get the detector array of Q # preallocate twotheta *= 180.0 / np.pi # preallocate twotheta_flat = twotheta.ravel() #pre allocate logging.debug(ai) # twotheta_flat = self.params[3] logging.debug("Running azimuthal integration") mask = np.zeros_like(data, np.int) fit = ai.integrate1d(data=data, npt=self.npts, unit='q_A^-1', correctSolidAngle=False) print "here" mData.set_meta_data('Q', fit[0]) newplot = self.calcfrom1d(fit[0], fit[1], twotheta_flat) rat = data / newplot thing = data.copy() thing[np.abs(rat - 1.0) > self.parameters['thresh'] * 1e-2] = 0.0 mask[thing == 0] = 1 # mask[data < minval] = 0 #mask[data > maxval] = 0 # final = ai.integrate1d(data=data, npt=self.npts,unit='q_A^-1', correctSolidAngle=False)# works... del ai ai = AzimuthalIntegrator() ai.setFit2D(distance, bc[0], bc[1], yaw, roll, px, px, None) ai.set_wavelength(self.wl) print "DATA.shape is %s " % str(data.shape) final = ai.integrate1d(data, self.npts, mask=mask, unit='q_A^-1', correctSolidAngle=False, method='numpy') # doesn't work # final = ai.integrate1d(data=data, npt=self.npts,mask=mask,unit='q_A^-1', correctSolidAngle=False)# doesn't work t2 = time.time() del ai # print final[1].shape print "PyFAI iteration with correction took:" + str( (t2 - t1) * 1e3) + "ms" print '##################END##################' return final[1]
class TestSolidAngle(unittest.TestCase): """ Test case for solid angle compared to Fit2D results Masked region have values set to 0 (not negative) and native mask from pilatus desactivated Detector Pilatus6M PixelSize= 1.720e-04, 1.720e-04 m Wavelength= 1.072274e-10m SampleDetDist= 1.994993e-01m PONI= 2.143248e-01, 2.133315e-01m rot1=0.007823 rot2= 0.006716 rot3= -0.000000 rad DirectBeamDist= 199.510mm Center: x=1231.226, y=1253.864 pix Tilt=0.591 deg tiltPlanRotation= 139.352 deg integration in 2theta between 0 and 56 deg in 1770 points """ fit2dFile = "2548/powder_200_2_0001.chi" pilatusFile = "2549/powder_200_2_0001.cbf" ai = None fit2d = None def setUp(self): """Download files""" self.fit2dFile = UtilsTest.getimage(self.__class__.fit2dFile) self.pilatusFile = UtilsTest.getimage(self.__class__.pilatusFile) self.tth_fit2d, self.I_fit2d = numpy.loadtxt(self.fit2dFile, unpack=True) self.ai = AzimuthalIntegrator( dist=1.994993e-01, poni1=2.143248e-01, poni2=2.133315e-01, rot1=0.007823, rot2=0.006716, rot3=0, pixel1=172e-6, pixel2=172e-6, ) self.data = fabio.open(self.pilatusFile).data self.data[self.data < 0] = 0 # discard negative pixels def testSolidAngle(self): """ This dataset goes up to 56deg, very good to test the solid angle correction any error will show off. fit2d makes correction in 1/cos^3(2th) (without tilt). pyFAI used to correct in 1/cos(2th) """ tth, I_nogood = self.ai.integrate1d( self.data, 1770, unit="2th_deg", radial_range=[0, 56], method="splitBBox", correctSolidAngle=False ) delta_tth = abs(tth - self.tth_fit2d).max() delta_I = abs(I_nogood - self.I_fit2d).max() I = abs(I_nogood - self.I_fit2d).mean() self.assert_(delta_tth < 1e-5, "Error on 2th position: %s <1e-5" % delta_tth) self.assert_(delta_I > 100, "Error on (wrong) I are large: %s >100" % delta_I) self.assert_(I > 2, "Error on (wrong) I are large: %s >2" % I) tth, I_good = self.ai.integrate1d( self.data, 1770, unit="2th_deg", radial_range=[0, 56], method="splitBBox", correctSolidAngle=3 ) delta_tth = abs(tth - self.tth_fit2d).max() delta_I = abs(I_good - self.I_fit2d).max() I = abs(I_good - self.I_fit2d).mean() self.assert_(delta_tth < 1e-5, "Error on 2th position: %s <1e-5" % delta_tth) self.assert_(delta_I < 5, "Error on (good) I are small: %s <5" % delta_I) self.assert_(I < 0.05, "Error on (good) I are small: %s <0.05" % I)
class TestSolidAngle(unittest.TestCase): """ Test case for solid angle compared to Fit2D results Masked region have values set to 0 (not negative) and native mask from pilatus desactivated Detector Pilatus6M PixelSize= 1.720e-04, 1.720e-04 m Wavelength= 1.072274e-10m SampleDetDist= 1.994993e-01m PONI= 2.143248e-01, 2.133315e-01m rot1=0.007823 rot2= 0.006716 rot3= -0.000000 rad DirectBeamDist= 199.510mm Center: x=1231.226, y=1253.864 pix Tilt=0.591 deg tiltPlanRotation= 139.352 deg integration in 2theta between 0 and 56 deg in 1770 points """ fit2dFile = '2548/powder_200_2_0001.chi' pilatusFile = '2549/powder_200_2_0001.cbf' ai = None fit2d = None def setUp(self): """Download files""" self.fit2dFile = UtilsTest.getimage(self.__class__.fit2dFile) self.pilatusFile = UtilsTest.getimage(self.__class__.pilatusFile) self.tth_fit2d, self.I_fit2d = numpy.loadtxt(self.fit2dFile, unpack=True) self.ai = AzimuthalIntegrator(dist=1.994993e-01, poni1=2.143248e-01, poni2=2.133315e-01, rot1=0.007823, rot2=0.006716, rot3=0, pixel1=172e-6, pixel2=172e-6) self.data = fabio.open(self.pilatusFile).data self.data[self.data < 0] = 0 #discard negative pixels def testSolidAngle(self): """ This dataset goes up to 56deg, very good to test the solid angle correction any error will show off. fit2d makes correction in 1/cos^3(2th) (without tilt). pyFAI used to correct in 1/cos(2th) """ tth, I_nogood = self.ai.integrate1d(self.data, 1770, unit="2th_deg", radial_range=[0, 56], method="splitBBox", correctSolidAngle=False) delta_tth = abs(tth - self.tth_fit2d).max() delta_I = abs(I_nogood - self.I_fit2d).max() I = abs(I_nogood - self.I_fit2d).mean() self.assert_(delta_tth < 1e-5, 'Error on 2th position: %s <1e-5' % delta_tth) self.assert_(delta_I > 100, 'Error on (wrong) I are large: %s >100' % delta_I) self.assert_(I > 2, 'Error on (wrong) I are large: %s >2' % I) tth, I_good = self.ai.integrate1d(self.data, 1770, unit="2th_deg", radial_range=[0, 56], method="splitBBox", correctSolidAngle=3) delta_tth = abs(tth - self.tth_fit2d).max() delta_I = abs(I_good - self.I_fit2d).max() I = abs(I_good - self.I_fit2d).mean() self.assert_(delta_tth < 1e-5, 'Error on 2th position: %s <1e-5' % delta_tth) self.assert_(delta_I < 5, 'Error on (good) I are small: %s <5' % delta_I) self.assert_(I < 0.05, 'Error on (good) I are small: %s <0.05' % I)
def test_AzimuthalIntegrator_pickle(): import dill import numpy as np from pyFAI import AzimuthalIntegrator det = pyFAI.detectors.detector_factory('pilatus2m') ai = AzimuthalIntegrator(detector=det) ai.set_wavelength(.1) spectra = ai.integrate1d(np.ones(det.shape), 1000) # force lut generation dump = dumps(ai) newai = loads(*dump) assert np.array_equal(newai.integrate1d(np.ones(det.shape), 1000), spectra) assert newai.detector.shape == (1679, 1475)
def pre_process(self): in_dataset, out_datasets = self.get_datasets() mData = self.get_in_meta_data()[0] in_d1 = in_dataset[0] ai = AzimuthalIntegrator() # get me an integrator object # prep the goemtry px_m = mData.get_meta_data('x_pixel_size') bc_m = [ mData.get_meta_data("beam_center_x"), mData.get_meta_data("beam_center_y") ] # in metres bc = bc_m / px_m # convert to pixels px = px_m * 1e6 # convert to microns distance = mData.get_meta_data('distance') * 1e3 # convert to mm wl = mData.get_meta_data('incident_wavelength')[...] # in m self.wl = wl yaw = -mData.get_meta_data("yaw") roll = mData.get_meta_data("roll") self.ai_params = distance, bc[0], bc[1], yaw, roll, px, px ai.setFit2D(distance, bc[0], bc[1], yaw, roll, px, px, None) ai.set_wavelength(wl) logging.debug(ai) self.sh = in_d1.get_shape()[-2:] self.npts = self.get_parameters('num_bins') foo = units.to_unit(units.TTH) # preallocate pretenddata = np.zeros(self.sh) pretendfit = ai.integrate1d(data=pretenddata, npt=self.npts, unit='q_A^-1', correctSolidAngle=False) self.add_axes_to_meta_data(pretendfit[0], mData) twotheta = ai.__getattribute__(foo.center)( self.sh) # get the detector array of Q # preallocate twotheta *= 180.0 / np.pi # preallocate twotheta_flat = twotheta.ravel() #pre allocate self.params = [self.npts, mData, twotheta_flat]