Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
 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 test_FourierCalibrationWorkflow():
    workflow = FourierCalibrationWorkflow()

    data = fabio.open('/home/rp/data/YL1031/AGB_5S_USE_2_2m.edf').data
    ai = AzimuthalIntegrator()
    ai.set_wavelength(124e-12)
    ai.detector = detectors.Pilatus2M()
    c = calibrant.ALL_CALIBRANTS('AgBh')

    print(
        workflow.execute(None,
                         data=data,
                         ai=ai,
                         calibrant=c,
                         callback_slot=print))
Beispiel #6
0
    def setAI(self, ai: AzimuthalIntegrator, device: str):
        self.AIs[device] = ai
        self.multiAI.ais = self.AIs.values()

        # propagate new ai to parameter
        fit2d = ai.getFit2D()
        try:
            self.setSilence(True)
            self.parameter.child(device,
                                 'Detector').setValue(type(ai.detector))
            self.parameter.child(device,
                                 'Binning').setValue(ai.detector.binning[0])
            self.parameter.child(device, 'Detector Tilt').setValue(
                fit2d['tiltPlanRotation'])
            self.parameter.child(device,
                                 'Detector Rotation').setValue(fit2d['tilt'])
            self.parameter.child(device, 'Pixel Size X').setValue(ai.pixel1)
            self.parameter.child(device, 'Pixel Size Y').setValue(ai.pixel2)
            self.parameter.child(device, 'Center X').setValue(fit2d['centerX'])
            self.parameter.child(device, 'Center Y').setValue(fit2d['centerY'])
            self.parameter.child(device, 'Detector Distance').setValue(
                fit2d['directDist'] / 1000)
            self.parameter.child('Wavelength').setValue(ai.wavelength)
        finally:
            self.setSilence(False)
        self.simulateCalibrant()
Beispiel #7
0
    def AI_func():
        from pyFAI.detectors import Pilatus2M
        from pyFAI import AzimuthalIntegrator, units

        return AzimuthalIntegrator(
            0.283, 5.24e-3, 4.085e-3, 0, 0, 0, 1.72e-4, 1.72e-4, detector=Pilatus2M(), wavelength=1.23984e-10
        )
Beispiel #8
0
 def addDevice(self, device):
     try:
         self.setSilence(True)
         devicechild = DeviceParameter(device)
         self.parameter.addChild(devicechild)
         ai = AzimuthalIntegrator(wavelength=self.parameter['Wavelength'])
         self.AIs[device] = ai
         self.multiAI.ais = list(self.AIs.values())
     finally:
         self.setSilence(False)
Beispiel #9
0
def test_SAXSWorkflow():
    # create processes
    thresholdmask = ThresholdMaskPlugin()
    qintegrate = QIntegratePlugin()

    # set values
    AI = AzimuthalIntegrator(.283,
                             5.24e-3,
                             4.085e-3,
                             0,
                             0,
                             0,
                             1.72e-4,
                             1.72e-4,
                             detector=Pilatus2M(),
                             wavelength=1.23984e-10)
    thresholdmask.data.value = fabio.open(
        '/Users/hari/Downloads/AGB_5S_USE_2_2m.edf').data

    def AI_func():
        from pyFAI.detectors import Pilatus2M
        from pyFAI import AzimuthalIntegrator, units
        return AzimuthalIntegrator(.283,
                                   5.24e-3,
                                   4.085e-3,
                                   0,
                                   0,
                                   0,
                                   1.72e-4,
                                   1.72e-4,
                                   detector=Pilatus2M(),
                                   wavelength=1.23984e-10)

    qintegrate.integrator.value = AI_func
    qintegrate.npt.value = 1000
    thresholdmask.minimum.value = 30
    thresholdmask.maximum.value = 1e12

    qintegrate.data.value = fabio.open(
        '/Users/hari/Downloads/AGB_5S_USE_2_2m.edf').data
    thresholdmask.neighborhood.value = 1
    qintegrate.normalization_factor.value = 0.5
    qintegrate.method.value = "numpy"

    # connect processes
    thresholdmask.mask.connect(qintegrate.mask)

    # add processes to workflow
    wf = Workflow('QIntegrate')
    wf.addProcess(thresholdmask)
    wf.addProcess(qintegrate)

    dsk = DaskExecutor()
    result = dsk.execute(wf)
    print(result)
Beispiel #10
0
def test_autoconnect():
    # create processes
    thresholdmask = ThresholdMaskPlugin()
    qintegrate = QIntegratePlugin()

    # set values
    AI = AzimuthalIntegrator(
        0.283, 5.24e-3, 4.085e-3, 0, 0, 0, 1.72e-4, 1.72e-4, detector=Pilatus2M(), wavelength=1.23984e-10
    )
    thresholdmask.data.value = fabio.open("/Users/hari/Downloads/AGB_5S_USE_2_2m.edf").data
    qintegrate.integrator.value = AI
    qintegrate.npt.value = 1000
    thresholdmask.minimum.value = 30
    thresholdmask.maximum.value = 1e12
Beispiel #11
0
 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 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]
Beispiel #13
0
    def apply(self):
        AI = AzimuthalIntegrator(
            wavelength=self.parameter.child('Wavelength').value())
        # if Calibration.isChecked():
        #     AI.setFit2D(self.getvalue('Detector Distance') * 1000.,
        #                 self.getvalue('Center X'),
        #                 self.getvalue('Center Y'),
        #                 self.getvalue('Detector Tilt'),
        #                 360. - self.getvalue('Detector Rotation'),
        #                 self.getvalue('Pixel Size Y') * 1.e6,
        #                 self.getvalue('Pixel Size X') * 1.e6)
        # elif self.wxdiffstyle.isChecked():
        #     AI.setFit2D(self.getvalue('Detector Distance') * 1000.,
        #                 self.getvalue('Center X'),
        #                 self.getvalue('Center Y'),
        #                 self.getvalue('Detector Tilt') / 2. / np.pi * 360.,
        #                 360. - (2 * np.pi - self.getvalue('Detector Rotation')) / 2. / np.pi * 360.,
        #                 self.getvalue('Pixel Size Y') * 1.e6,
        #                 self.getvalue('Pixel Size X') * 1.e6)
        # AI.set_wavelength(self.getvalue('Wavelength'))
        # # print AI

        activeCalibration = AI
    def pre_process(self):
        """
        This method is called after the plugin has been created by the
        pipeline framework as a pre-processing step

        :param parameters: A dictionary of the parameters for this plugin, or
            None if no customisation is required
        :type parameters: dict
        """
        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
        bc = [mData.get_meta_data("beam_center_x")[...],
              mData.get_meta_data("beam_center_y")[...]]
        distance = mData.get_meta_data('distance')[...]
        wl = mData.get_meta_data('incident_wavelength')[...]
        px = mData.get_meta_data('x_pixel_size')[...]
        orien = mData.get_meta_data(
            'detector_orientation')[...].reshape((3, 3))
        #Transform
        yaw = math.degrees(-math.atan2(orien[2, 0], orien[2, 2]))
        roll = math.degrees(-math.atan2(orien[0, 1], orien[1, 1]))
        ai.setFit2D(distance, bc[0], bc[1], -yaw, roll, px, px, None)
        ai.set_wavelength(wl)
        
        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.xrpd(data=pretenddata, npt=self.npts)
        twotheta=ai.__getattribute__(foo.center)(ai.detector.max_shape) # get the detector array of Q # preallocate
        twotheta *= 180.0/np.pi # preallocate
        twotheta_flat = twotheta.ravel() #pre allocate
        self.params = [self.npts, mData, ai, twotheta_flat]
    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]
Beispiel #16
0
import time
from pprint import pprint

import h5py
import numpy as np
from bluesky.utils import new_uid
from pyFAI import AzimuthalIntegrator


def format_keys(*args):
    return '/'.join(args)


key_data_map = {'RayMX': 'image', 'photodiode': 'I0', 'eh1_qbpm1': 'eh1_qbpm1',
                'oh_qbpm2': 'oh_qbpm2'}
ai = AzimuthalIntegrator(wavelength=(12.398 / 9.70803 * 1.0e-10))
# TODO: need to load this info from somewhere!
ai.setFit2D(60.550, 1440.364, (2880 - -26.378), tilt=(-0.024),
            tiltPlanRotation=54.442, pixelX=78, pixelY=78)
calib_config_dict = dict(ai.getPyFAI())


def parse_hdf5(fn):
    """Parse hdf5 file from the PAL-XFEL beamline into an event stream

    Parameters
    ----------
    fn: str
        The path to the hdf5 file

    Yields
Beispiel #17
0
import numpy as np
from bluesky.utils import new_uid
from pyFAI import AzimuthalIntegrator


def format_keys(*args):
    return '/'.join(args)


key_data_map = {
    'RayMX': 'image',
    'photodiode': 'I0',
    'eh1_qbpm1': 'eh1_qbpm1',
    'oh_qbpm2': 'oh_qbpm2'
}
ai = AzimuthalIntegrator(wavelength=(12.398 / 9.70803 * 1.0e-10))
# TODO: need to load this info from somewhere!
ai.setFit2D(60.550,
            1440.364, (2880 - -26.378),
            tilt=(-0.024),
            tiltPlanRotation=54.442,
            pixelX=78,
            pixelY=78)
calib_config_dict = dict(ai.getPyFAI())


def parse_hdf5(fn):
    """Parse hdf5 file from the PAL-XFEL beamline into an event stream

    Parameters
    ----------