Example #1
0
 def set_ponifile(self, ponifile=None):
     if ponifile is None:
         ponifile = self.poni.text()
         print ponifile
     try:
         self.ai = pyFAI.load(ponifile)
     except Exception as error:
         logger.error("file %s does not look like a poni-file, error %s" %
                      (ponifile, error))
         return
     self.pixel1.setText(str_(self.ai.pixel1))
     self.pixel2.setText(str_(self.ai.pixel2))
     self.dist.setText(str_(self.ai.dist))
     self.poni1.setText(str_(self.ai.poni1))
     self.poni2.setText(str_(self.ai.poni2))
     self.rot1.setText(str_(self.ai.rot1))
     self.rot2.setText(str_(self.ai.rot2))
     self.rot3.setText(str_(self.ai.rot3))
     self.splineFile.setText(str_(self.ai.detector.splineFile))
     self.wavelength.setText(str_(self.ai._wavelength))
     name = self.ai.detector.name.lower()
     if name in self.all_detectors:
         self.detector.setCurrentIndex(self.all_detectors.index(name))
     else:
         self.detector.setCurrentIndex(self.all_detectors.index("detector"))
Example #2
0
def calc_cake(xrd2d,
              calfile,
              unit='q',
              mask=None,
              dark=None,
              xsteps=2048,
              ysteps=2048,
              verbose=False):

    if HAS_pyFAI:
        try:
            ai = pyFAI.load(calfile)
        except:
            print('Provided calibration file could not be loaded.')
            return
    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')

    attrs = {}
    if unit.startswith('2th'):
        attrs.update({'unit': '2th_deg'})
    else:
        attrs.update({'unit': 'q_A^-1'})
    if mask:
        if np.shape(mask) == np.shape(xrd2d): attrs.update({'mask': mask})
    if dark:
        if np.shape(dark) == np.shape(xrd2d): attrs.update({'dark': dark})

    return calcXRDcake(xrd2d, ai, xsteps, ysteps, attrs)
Example #3
0
def loadgeometry(ponifilepath=None):
    """Loads the detector geometry information from a poni file or from hard coded information
    Parameters
    ----------
    ponifilepath: str
        File path to the .poni file
    Returns
    -------
    a: an object which contains the geometry information for the detector
    """
    if ponifilepath is None:
        distance = 0.204666698799
        poni1 = 0.205911555168
        poni2 = 0.207264947729
        rot1 = 0.0105322341791
        rot2 = 0.0104587423844
        rot3 = -3.27539683464e-08
        spline_file = None
        wavelength = 1.839e-11
        detector = "Perkin"
        # This is the PyFAI geometry stuff
        a = geo.Geometry(dist=distance, poni1=poni1, poni2=poni2, rot1=rot1,
                         rot2=rot2, rot3=rot3, pixel1=200e-6, pixel2=200e-6,
                         splineFile=None, detector=detector,
                         wavelength=wavelength)
    else:
        a = pyFAI.load(ponifilepath)
    return a
Example #4
0
    def __init__(self,
                 poni,
                 npchi,
                 npfilt,
                 nptth,
                 maskfile=None,
                 dims=(2048, 2048)):
        self.npchi = npchi
        self.npfilt = npfilt
        self.nptth = nptth

        self.ai = pyFAI.load(poni)
        self.ai.setChiDiscAtZero()  # Does not help, wanted pi/2

        if maskfile is not None:
            self.immask = 1 - fabio.open(maskfile).data
        else:
            self.immask = np.ones(dims, np.float32)
        tmps, x, y = self.ai.integrate2d(self.immask,
                                         self.nptth,
                                         self.npchi,
                                         unit='2th_rad',
                                         azimuth_range=(0, 360),
                                         correctSolidAngle=False,
                                         polarization_factor=None)
        self.mask = (tmps > 0.8).T

        tthstep = (x[-1] - x[0]) / len(x)
        self.tthbin = np.round(
            (self.ai.twoThetaArray() - x[0]) / tthstep).astype(int)
        chid = np.degrees(self.ai.chiArray())
        chistep = 360.0 / (len(y) - 1)
        self.chibin = np.round((chid) / chistep).astype(int)
Example #5
0
    def openPONI(self,event):
             
        wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*'
        dlg = wx.FileDialog(self, message='Choose pyFAI calibration file',
                           defaultDir=os.getcwd(),
                           wildcard=wildcards, style=wx.FD_OPEN)

        path, read = None, False
        if dlg.ShowModal() == wx.ID_OK:
            read = True
            path = dlg.GetPath().replace('\\', '/')
        dlg.Destroy()
        
        if read:

            try:
                print
                self.ai = pyFAI.load(path)
                print('Loading calibration file: %s' % path)
            except:
                print('Not recognized as a pyFAI calibration file: %s' % path)
                return

            ## Sets viewer to values in .poni file
            self.entr_dist.SetValue('%0.4f' % self.ai._dist)
            self.entr_pix.SetValue('%0.1f' % float(self.ai.detector.pixel1*1000000.))
            self.ch_DorP.SetSelection(1)
            self.entr_EorL.SetValue('%0.4f' % float(self.ai._wavelength*1.e10))
            self.ch_EorL.SetSelection(1)
            self.onDorPSel(None)
            
            cenx = float(self.ai._poni1)/float(self.ai.detector.pixel1)
            ceny = float(self.ai._poni2)/float(self.ai.detector.pixel2)
            self.entr_cntrx.SetValue('%0.3f' % cenx)
            self.entr_cntry.SetValue('%0.3f' % ceny)
Example #6
0
    def set_ai(self):
        poni = str(self.poni.text()).strip()
        if poni and os.path.isfile(poni):
            self.ai = pyFAI.load(poni)
        detector = str(self.detector.currentText()).lower().strip() or "detector"
        self.ai.detector = pyFAI.detectors.detector_factory(detector)

        wavelength = str(self.wavelength.text()).strip()
        if wavelength:
            try:
                fwavelength = float(wavelength)
            except ValueError:
                logger.error("Unable to convert wavelength to float: %s" % wavelength)
            else:
                if fwavelength <= 0 or fwavelength > 1e-6:
                    logger.warning("Wavelength is in meter ... unlikely value %s" % fwavelength)
                self.ai.wavelength = fwavelength

        splineFile = str(self.splineFile.text()).strip()
        if splineFile and os.path.isfile(splineFile):
            self.ai.detector.splineFile = splineFile

        self.ai.pixel1 = self._float("pixel1", 1)
        self.ai.pixel2 = self._float("pixel2", 1)
        self.ai.dist = self._float("dist", 1)
        self.ai.poni1 = self._float("poni1", 0)
        self.ai.poni2 = self._float("poni2", 0)
        self.ai.rot1 = self._float("rot1", 0)
        self.ai.rot2 = self._float("rot2", 0)
        self.ai.rot3 = self._float("rot3", 0)

        if self.chi_discontinuity_at_0.isChecked():
            self.ai.setChiDiscAtZero()

        mask_file = str(self.mask_file.text()).strip()
        if mask_file and os.path.exists(mask_file) and bool(self.do_mask.isChecked()):
            try:
                mask = fabio.open(mask_file).data
            except Exception as error:
                logger.error("Unable to load mask file %s, error %s" % (mask_file, error))
            else:
                self.ai.mask = mask
        dark_files = [i.strip() for i in str(self.dark_current.text()).split(",")
                      if os.path.isfile(i.strip())]
        if dark_files and bool(self.do_dark.isChecked()):
            d0 = fabio.open(dark_files[0]).data
            darks = numpy.zeros(d0.shape[0], d0.shape[1], len(dark_files), dtype=numpy.float32)
            for i, f in enumerate(dark_files):
                darks[:, :, i] = fabio.open(f).data
            self.ai.darkcurrent = darks.mean(axis= -1)

        flat_files = [i.strip() for i in str(self.flat_field.text()).split(",")
                      if os.path.isfile(i.strip())]
        if flat_files and bool(self.do_flat.isChecked()):
            d0 = fabio.open(flat_files[0]).data
            flats = numpy.zeros(d0.shape[0], d0.shape[1], len(flat_files), dtype=numpy.float32)
            for i, f in enumerate(flat_files):
                flats[:, :, i] = fabio.open(f).data
            self.ai.darkcurrent = flats.mean(axis= -1)
        print self.ai
Example #7
0
 def __init__(self, poni, img):
     self.ponifile = poni
     self.ai = pyFAI.load(poni)
     self.img = fabio.open(img)
     self.r = None
     self.I = None
     self.resynth = None
     self.delta = None
Example #8
0
 def __init__(self, poni, img):
     self.ponifile = poni
     self.ai = pyFAI.load(poni)
     self.img = fabio.open(img)
     self.r = None
     self.I = None
     self.resynth = None
     self.delta = None
Example #9
0
def loadgeometry(ponifilepath=None):
    """Loads the detector geometry information from a poni file or from hard coded information
    Parameters
    ----------
    ponifilepath: str
        File path to the .poni file
    Returns
    -------
    Azimuthal integrator object:
        an object which contains the geometry information for the detector
    """
    if ponifilepath is None:
        filepath = tkFileDialog.askopenfilename()
        a = pyFAI.load(ponifilepath)
    else:
        a = pyFAI.load(ponifilepath)
    return a
Example #10
0
def q_from_xy(x, y, ai=None, calfile=None):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        return ai.qFunction(np.array([y,]),np.array([x,]))[0]
    except:
        return 0
Example #11
0
def integrate_xrd(xrd_map,
                  ai=None,
                  AI=None,
                  calfile=None,
                  unit='q',
                  steps=10000,
                  save=False,
                  file='~/test.xy',
                  mask=None,
                  dark=None):
    '''
    Uses pyFAI (poni) calibration file and 2D XRD image to produce 1D XRD data

    Must provide one of following: ai, AI, or calfile!
    options :
    ai -
    AI - 
    calfile - 
    unit - 
    steps - 
    save -
    file - 
    mask - 
    dark - 
    '''

    if HAS_pyFAI:
        if ai is None:
            if AI is None:
                try:
                    ai = pyFAI.load(calfile)
                except IOError:
                    print('No calibration parameters specified.')
                    return
            else:
                ai = calculate_ai(AI)

        attrs = {}
        if unit == '2th':
            attrs.update({'unit': '2th_deg'})
        else:
            attrs.update({'unit': 'q_A^-1'})
        if mask:
            attrs.update({'mask': mask})
        if dark:
            attrs.update({'dark': dark})
        if save:
            print('Saving %s data to file: %s\n' % (unit, file))
            attrs.update({'filename': file})

        qI = ai.integrate1d(xrd_map, steps, **attrs)

    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')
        return

    return qI
Example #12
0
def integrate_xrd_row(rowxrd2d,
                      calfile,
                      unit='q',
                      steps=10001,
                      wedge_limits=None,
                      mask=None,
                      dark=None,
                      flip=True):
    '''
    Uses pyFAI (poni) calibration file to produce 1D XRD data from a row of 2D XRD images 

    Must provide pyFAI calibration file
    
    rowxrd2d     : 2D diffraction images for integration
    calfile      : poni calibration file
    unit         : unit for integration data ('2th'/'q'); default is 'q'
    steps        : number of steps in integration data; default is 10000
    wedge_limits : azimuthal slice limits
    mask         : mask array for image
    dark         : dark image array
    flip         : vertically flips image to correspond with Dioptas poni file calibration
    '''

    if HAS_pyFAI:
        try:
            ai = pyFAI.load(calfile)
        except:
            print('Provided calibration file could not be loaded.')
            return

        if type(dark) is str:
            try:
                dark = np.array(tifffile.imread(xrd2dbkgd))
            except:
                dark = None

        dir = -1 if flip else 1
        attrs = {'mask': mask, 'dark': dark}
        if unit.startswith('2th'):
            attrs.update({'unit': '2th_deg'})
        else:
            attrs.update({'unit': 'q_A^-1'})

        if wedge_limits is not None:
            attrs.update({'azimuth_range': wedge_limits})

        q, xrd1d = [], []

        for i, xrd2d in enumerate(rowxrd2d):
            row_q, row_xrd1d = calcXRD1d(xrd2d[::dir, :], ai, steps, attrs)
            q += [row_q]
            xrd1d += [row_xrd1d]

        return np.array(q), np.array(xrd1d)
    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')
Example #13
0
def load_integrator(poni_file='images/lab6_1_S002_00000.poni'):
    # ai = pyFAI.load('lab6_s3.poni')
    # ai = pyFAI.load('coin_cell/S7_lab6.poni')
    # ai = pyFAI.load('images/lab6_10-15-2019a.poni')
    ai = pyFAI.load(str(poni_file))
    # # Fix poni calibration
    # poni = 0.0845 # 0.0852
    # ai.set_poni1(0.0858)
    # ai.set_poni2(0.0843)
    return ai
Example #14
0
    def setup(self, kwargs=None):
        """Perform the setup of the job.
        mainly parsing of the kwargs. 
         
        :param kwargs: dict with parmaters.
        :return: None
        """
        logger.debug("IntegrateManyFrames.setup")
        Plugin.setup(self, kwargs)


        self.input_files = self.input.get("input_files")
        if not self.input_files:
            self.log_error("InputError: input_files not in input.")

        if "output_file" not in self.input:
            self.log_error("InputWarning: output_file not in input, save in input directory",
                           do_raise=False)
            self.output_file = os.path.join(os.path.dirname(self.input_files[0]), "output.h5")
        else:
            self.output_file = os.path.abspath(self.input["output_file"])
        if not self.output_file.endswith(".h5"):
            self.output_file = self.output_file + ".h5"

        poni_file = self.input.get("poni_file")
        if not poni_file:
            self.log_error("InputError: poni_file not in input.")
        ai = pyFAI.load(poni_file)
        stored = self._ais.get(poni_file, ai)
        if stored is ai:
            self.ai = stored
        else:
            self.ai = copy.deepcopy(stored)

        self.npt = int(self.input.get("npt", self.npt))
        self.unit = self.input.get("unit", self.unit)
        self.wavelength = self.input.get("wavelength", self.wavelength)
        if os.path.exists(self.input.get("mask", "")):
            self.mask = fabio.open(self.input["mask"]).data
        self.dummy = self.input.get("dummy", self.dummy)
        self.delta_dummy = self.input.get("delta_dummy", self.delta_dummy)
        if self.input.get("do_polarziation"):
            self.polarization_factor = self.input.get("polarization_factor", self.polarization_factor)
        self.do_SA = self.input.get("do_SA", self.do_SA)
        self.norm = self.input.get("norm", self.norm)
        self.method = self.input.get("method", self.method)
        self.save_raw = self.input.get("save_raw", self.save_raw)
        self.raw_compression = self.input.get("raw_compression", self.raw_compression)
        if self.save_raw:
            dataset = self.prepare_raw_hdf5(self.raw_compression)
            self.queue_saver = Queue()
            self.raw_saver = RawSaver(self.queue_saver, self.quit_event, dataset)
            self.raw_saver.start()
        # create the pool of workers
        self.pool = Reader.build_pool((self.queue_in, self.queue_out, self.quit_event), self.pool_size)
Example #15
0
def integrate_xrd(xrd2d,
                  calfile,
                  unit='q',
                  steps=2048,
                  file='',
                  wedge_limits=None,
                  mask=None,
                  dark=None,
                  is_eiger=True,
                  save=False,
                  verbose=False):
    '''
    Uses pyFAI (poni) calibration file and 2D XRD image to produce 1D XRD data

    Must provide pyFAI calibration file

    xrd2d        : 2D diffraction images for integration
    calfile      : poni calibration file
    unit         : unit for integration data ('2th'/'q'); default is 'q'
    steps        : number of steps in integration data; default is 10000
    wedge_limits : azimuthal slice limits
    file         : filename for saving data; if '' (default) will not save
    mask         : mask array for image
    dark         : dark image array
    '''

    if HAS_pyFAI:
        try:
            ai = pyFAI.load(calfile)
        except:
            print('Provided calibration file could not be loaded.')
            return
    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')
        return

    attrs = {}
    if unit.startswith('2th'):
        attrs.update({'unit': '2th_deg'})
    else:
        attrs.update({'unit': 'q_A^-1'})

    if wedge_limits is not None:
        attrs.update({'azimuth_range': wedge_limits})

    if mask:
        if np.shape(mask) == np.shape(xrd2d): attrs.update({'mask': mask})
    if dark:
        if np.shape(dark) == np.shape(xrd2d): attrs.update({'dark': dark})

    if file != '':
        if verbose:
            print('\nSaving %s data to file: %s' % (unit, file))
        attrs.update({'filename': file})
    return calcXRD1d(xrd2d, ai, steps, attrs)
Example #16
0
    def set_ai(self):
        poni = str(self.poni.text()).strip()
        if poni and op.isfile(poni):
            self.ai = pyFAI.load(poni)
        detector = str(self.detector.currentText()).lower().strip() or "detector"
        self.ai.detector = pyFAI.detectors.detector_factory(detector)

        wavelength = str(self.wavelength.text()).strip()
        if wavelength:
            try:
                fwavelength = float(wavelength)
            except ValueError:
                logger.error("Unable to convert wavelength to float: %s" % wavelength)
            else:
                if fwavelength <= 0 or fwavelength > 1e-6:
                    logger.warning("Wavelength is in meter ... unlikely value %s" % fwavelength)
                self.ai.wavelength = fwavelength

        splineFile = str(self.splineFile.text()).strip()
        if splineFile and op.isfile(splineFile):
            self.ai.detector.splineFile = splineFile

        self.ai.pixel1 = self._float("pixel1", 1)
        self.ai.pixel2 = self._float("pixel2", 1)
        self.ai.dist = self._float("dist", 1)
        self.ai.poni1 = self._float("poni1", 0)
        self.ai.poni2 = self._float("poni2", 0)
        self.ai.rot1 = self._float("rot1", 0)
        self.ai.rot2 = self._float("rot2", 0)
        self.ai.rot3 = self._float("rot3", 0)

        if self.chi_discontinuity_at_0.isChecked():
            self.ai.setChiDiscAtZero()

        mask_file = str(self.mask_file.text()).strip()
        if mask_file  and bool(self.do_mask.isChecked()):
            if op.exists(mask_file):
                try:
                    mask = fabio.open(mask_file).data
                except Exception as error:
                    logger.error("Unable to load mask file %s, error %s" % (mask_file, error))
                else:
                    self.ai.mask = mask
#            elif mask_file==FROM_PYMCA:
#                self.ai.mask = mask
        dark_files = [i.strip() for i in str(self.dark_current.text()).split(",")
                      if op.isfile(i.strip())]
        if dark_files and bool(self.do_dark.isChecked()):
            self.ai.set_darkfiles(dark_files)

        flat_files = [i.strip() for i in str(self.flat_field.text()).split(",")
                      if op.isfile(i.strip())]
        if flat_files and bool(self.do_flat.isChecked()):
            self.ai.set_flatfiles(flat_files)
        print self.ai
Example #17
0
def get_poni_dict(poni_file):
    """ Read Poni File and convert to Dictionary"""
    ai = pyFAI.load(poni_file)
    poni_keys = [
        '_dist', '_rot1', '_rot2', '_rot3', '_poni1', '_poni2', 'detector',
        '_wavelength'
    ]

    poni_dict = {k: ai.__getattribute__(k) for k in poni_keys}

    return poni_dict
Example #18
0
def integrate_xrd_row(rowxrd2d, calfile, unit='q', steps=2048,
                      wedge_limits=None, mask=None, dark=None,
                      flip=True):
    '''
    Uses pyFAI (poni) calibration file to produce 1D XRD data from a row of 2D XRD images

    Must provide pyFAI calibration file

    rowxrd2d     : 2D diffraction images for integration
    calfile      : poni calibration file
    unit         : unit for integration data ('2th'/'q'); default is 'q'
    steps        : number of steps in integration data; default is 10000
    wedge_limits : azimuthal slice limits
    mask         : mask array for image
    dark         : dark image array
    flip         : vertically flips image to correspond with Dioptas poni file calibration
    '''

    if not HAS_pyFAI:
        print('pyFAI not imported. Cannot calculate 1D integration.')
        return

    try:
        ai = pyFAI.load(calfile)
    except:
        print('calibration file "%s" could not be loaded.' % calfile)
        return

    if type(dark) is str:
        try:
            dark = np.array(tifffile.imread(xrd2dbkgd))
        except:
            dark = None

    dir = -1 if flip else 1
    attrs = dict(mask=mask, dark=dark, method='csr',
             polarization_factor=0.999, correctSolidAngle=True)

    if unit.startswith('2th'):
        attrs.update({'unit':'2th_deg'})
    else:
        attrs.update({'unit':'q_A^-1'})

    if wedge_limits is not None:
        attrs.update({'azimuth_range':wedge_limits})

    # print("Calc XRD 1D for row", ai, steps, attrs)
    q, xrd1d = [], []
    for i, xrd2d in enumerate(rowxrd2d):
        row_q,row_xrd1d = calcXRD1d(xrd2d[::dir,:], ai, steps, attrs)
        q     += [row_q]
        xrd1d += [row_xrd1d]

    return np.array(q), np.array(xrd1d)
Example #19
0
File: id31.py Project: kif/UPBL09a
def integrate_simple(poni_file, image_file, curve_file, nbins=1000):
    """Simple azimuthal integration for a single frame (very inefficient)
    
    :param poni_file: configuration of the geometry
    :param image_file: 
    :param curve_file: output file
    :param nbins: number of output bins
    """
    ai = pyFAI.load(poni_file)
    img = fabio.open(image_file).data
    ai.integrate1d(img, nbins, filename=curve_file, unit="2th_deg", method="splitpixel")
    return {"out_file": curve_file}
Example #20
0
def q_from_xy(x, y, ai=None, calfile=None):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        return ai.qFunction(np.array([
            y,
        ]), np.array([
            x,
        ]))[0]
    except:
        return 0
Example #21
0
def twth_from_xy(x, y, ai=None, calfile=None, ang_units='degrees'):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        twth = ai.tth(np.array([y,]),np.array([x,]))
    except:
        return 0

    if ang_units.startswith('rad'):
        return twth[0]
    else:
        return np.degrees(twth[0])
Example #22
0
def eta_from_xy(x, y, ai=None, calfile=None, ang_units='degrees'):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        eta = ai.chi(np.array([y,]),np.array([x,]))
    except:
        return 0

    if ang_units.startswith('rad'):
        return eta[0]
    else:
        return np.degrees(eta[0])
def createMg (aifiles = None):
    if (aifiles == None):
        root = Tk()
        aifiles = filedialog.askopenfilenames(parent=root, title='Choose multiple files')
        aifiles = root.tk.splitlist(aifiles)
        print (aifiles)
        root.destroy()
    ais = []
    for file in aifiles:
        ais.append(pyFAI.load(file))

    mg = MultiGeometry(ais, unit = "2th_deg", radial_range= (25,75)   )
    print (mg)
    return (mg)
Example #24
0
def save1D(filename,
           xaxis,
           I,
           error=None,
           xaxis_unit=None,
           calfile=None,
           has_dark=False,
           has_flat=False,
           polarization_factor=None,
           normalization_factor=None):
    '''
    copied and modified from pyFAI/io.py
    '''
    if xaxis_unit is None or xaxis_unit == 'q':
        xaxis_unit = pyFAI.units.Q_A
    elif xaxis_unit.startswith('2th'):
        xaxis_unit = pyFAI.units.TTH_DEG

    if calfile is None:
        ai = None
    else:
        ai = pyFAI.load(calfile)

    xaxis_unit = pyFAI.units.to_unit(xaxis_unit)
    with open(filename, 'w') as f:
        f.write(
            make_headers(has_dark=has_dark,
                         has_flat=has_flat,
                         ai=ai,
                         polarization_factor=polarization_factor,
                         normalization_factor=normalization_factor))
        try:
            f.write('\n# --> %s\n' % (filename))
        except UnicodeError:
            f.write('\n# --> %s\n' % (filename.encode('utf8')))
        if error is None:
            try:
                f.write('#%14s %14s\n' % (xaxis_unit.REPR, 'I '))
            except:
                f.write('#%14s %14s\n' % (xaxis_unit.name, 'I '))
            f.write('\n'.join(
                ['%14.6e  %14.6e' % (t, i) for t, i in zip(xaxis, I)]))
        else:
            f.write('#%14s  %14s  %14s\n' % (xaxis_unit.REPR, 'I ', 'sigma '))
            f.write('\n'.join([
                '%14.6e  %14.6e %14.6e' % (t, i, s)
                for t, i, s in zip(xaxis, I, error)
            ]))
        f.write('\n')
Example #25
0
def get_integrator(keycache):
    "retrieve or build an azimuthal integrator based on the keycache provided "
    #key = KeyCache(self.npt, self.unit, self.poni, self.mask, self.energy)
    if keycache in shared_cache:
        ai = shared_cache[keycache]
    else:
        ai = pyFAI.load(keycache.poni)
        ai.wavelength = 1e-10 * pyFAI.units.hc / keycache.energy
        if keycache.mask:
            mask = numpy.logical_or(
                fabio.open(keycache.mask).data,
                ai.detector.mask).astype("int8")
            ai.detector.mask = mask
        shared_cache[keycache] = ai
    return ai
Example #26
0
def load_calibration_md(poni_file: str) -> dict:
    """Load the calibration metadata in a dictionary from a .poni file.

    Parameters
    ----------
    poni_file :
        The path to the .poni file.

    Returns
    -------
    calibration_md :
        The metadata in a dictionary.
    """
    ai = pyFAI.load(poni_file)
    return dict(ai.getPyFAI())
Example #27
0
def integrate_simple(poni_file, image_file, curve_file, nbins=1000):
    """Simple azimuthal integration for a single frame (very inefficient)
    
    :param poni_file: configuration of the geometry
    :param image_file: 
    :param curve_file: output file
    :param nbins: number of output bins
    """
    ai = pyFAI.load(poni_file)
    img = fabio.open(image_file).data
    ai.integrate1d(img,
                   nbins,
                   filename=curve_file,
                   unit="2th_deg",
                   method="splitpixel")
    return {"out_file": curve_file}
Example #28
0
def twth_from_xy(x, y, ai=None, calfile=None, ang_units='degrees'):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        twth = ai.tth(np.array([
            y,
        ]), np.array([
            x,
        ]))
    except:
        return 0

    if ang_units.startswith('rad'):
        return twth[0]
    else:
        return np.degrees(twth[0])
Example #29
0
def eta_from_xy(x, y, ai=None, calfile=None, ang_units='degrees'):

    if ai is None: ai = pyFAI.load(calfile)

    try:
        eta = ai.chi(np.array([
            y,
        ]), np.array([
            x,
        ]))
    except:
        return 0

    if ang_units.startswith('rad'):
        return eta[0]
    else:
        return np.degrees(eta[0])
Example #30
0
def integrate_xrd(xrd_map, AI=None, calfile=None, unit='q', steps=10000, 
                  save=True, aname = 'default', prefix = 'XRD', path = '~/',
                  mask=None, dark=None, verbose=False):

    if HAS_pyFAI:
        if AI is None:
            try:
                ai = pyFAI.load(calfile)
            except IOError:
                print('No calibration parameters specified.')
                return
        else:
            ai = calculate_ai(AI)
        
        if unit == 'q':
            iunit = 'q_A^-1'
        elif unit == '2th':
            iunit='2th_deg'
        else:
            print('Unknown unit: %s. Using q.' % unit)
            unit = 'q'
            iunit = 'q_A^-1'

        t0 = time.time()
    
        if save:
            counter = 1
            while os.path.exists('%s/%s-%s-%03d.xy' % (path,prefix,aname,counter)):
                counter += 1
            fname = '%s/%s-%s-%03d.xy' % (path,prefix,aname,counter)
            print('\nSaving %s data in file: %s\n' % (unit,fname))
            qI = ai.integrate1d(xrd_map,steps,unit=iunit,mask=mask,dark=dark,filename=fname)
        else:
            qI = ai.integrate1d(xrd_map,steps,unit=iunit,mask=mask,dark=dark)
        t1 = time.time()
        if verbose:
            print('\ttime to integrate data = %0.3f s' % ((t1-t0)))

        if verbose:
            print('Parameters for 1D integration:')
            print(ai)
    else:
        print('pyFAI not imported. Cannot calculate 1D integration without it.')
        return

    return qI
Example #31
0
def integrate_xrd(xrd2d, calfile, unit='q', steps=2048, file='',  wedge_limits=None,
                  mask=None, dark=None, is_eiger=True, save=False, verbose=False):
    '''
    Uses pyFAI (poni) calibration file and 2D XRD image to produce 1D XRD data

    Must provide pyFAI calibration file

    xrd2d        : 2D diffraction images for integration
    calfile      : poni calibration file
    unit         : unit for integration data ('2th'/'q'); default is 'q'
    steps        : number of steps in integration data; default is 10000
    wedge_limits : azimuthal slice limits
    file         : filename for saving data; if '' (default) will not save
    mask         : mask array for image
    dark         : dark image array
    '''

    if HAS_pyFAI:
        try:
            ai = pyFAI.load(calfile)
        except:
            print('Provided calibration file could not be loaded.')
            return
    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')
        return

    attrs = {}
    if unit.startswith('2th'):
        attrs.update({'unit':'2th_deg'})
    else:
        attrs.update({'unit':'q_A^-1'})

    if wedge_limits is not None:
        attrs.update({'azimuth_range':wedge_limits})

    if mask:
        if np.shape(mask) == np.shape(xrd2d): attrs.update({'mask':mask})
    if dark:
        if np.shape(dark) == np.shape(xrd2d): attrs.update({'dark':dark})

    if file is not '':
        if verbose:
            print('\nSaving %s data to file: %s' % (unit,file))
        attrs.update({'filename':file})
    return calcXRD1d(xrd2d, ai, steps, attrs)
Example #32
0
    def config_by_poni(calib_cpt: CalibrationData, poni_file: str):
        """Configure the CalibrationData using the info in a poni file.

        Parameters
        ----------
        calib_cpt : CalibrationData
            The CalibrationData component to be configured.
        poni_file : str
            The path to the poni file.

        Yields
        ------
        Msg : Msg
            The plan message.
        """
        ai = pyFAI.load(poni_file)
        return (yield from config_by_ai(calib_cpt, ai))
Example #33
0
    def run(self):

        m = fabio.open(self.mask).data
        ai = pyFAI.load(self.poniname)
        alpha = float(75)  #In degrees

        x0 = ai.poni1 / ai.pixel1
        y0 = ai.poni2 / ai.pixel2
        dy = 1479 - x0
        dx = 1675 - y0

        tang = tan((90.0 - alpha) / 2.0 * pi / 180.0)

        y = 1479 - dy - x0 * tang
        y = dy + x0 * tang
        x = 1675 - dx - y0 * tang

        # so the points on the edge are
        # (x0,0), (0,y0)
        # which is shorter?

        d1x = (x0 - x)
        d1y = (y0)
        d2y = (y0 - y)
        d2x = (x0)

        l1 = sqrt(d1x * d1x + d1y * d1y)
        l2 = sqrt(d2x * d2x + d2y * d2y)

        # print x0,y0,x,y,tang

        # take off 5 pixels near the edge
        self.radial_mask(m, min(l1, l2) - 5, (x0, y0))
        self.angle_mask(m, tang, (x0, y0))
        self.block_mask(m, tang, (x0, y0))

        mask = logical_or.reduce((self.rmask, self.amask, self.bmask, m))
        #mask=np.logical_or.reduce((rmask,amask,m))
        outfile = 'PDFmask_%.1fdeg.cbf' % (alpha)
        outbuffer = fabio.cbfimage.cbfimage(data=mask)
        outbuffer.write(outfile)
        print('Wrote mask to', outfile)

        perm = 'chmod 777 %s' % self.outfile
        system(perm)
Example #34
0
    def setup(self, kwargs):
        logger.debug("Integrate.setup")
        Plugin.setup(self, kwargs)

        if "output_dir" not in self.input:
            self.log_error("output_dir not in input")
        self.dest_dir = os.path.abspath(self.input["output_dir"])

        ponifile = self.input.get("poni_file", "")
        if not os.path.exists(ponifile):
            self.log_error("Ponifile: %s does not exist" % ponifile,
                           do_raise=True)
        ai = pyFAI.load(ponifile)
        stored = self._ais.get(str(ai), ai)
        if stored is ai:
            self.ai = stored
        else:
            self.ai = stored.__deepcopy__()

        self.npt = int(self.input.get("npt", self.npt))
        self.unit = self.input.get("unit", self.unit)
Example #35
0
    def openPONI(self,event=None):
             
        wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*'
        dlg = wx.FileDialog(self, message='Choose pyFAI calibration file',
                           defaultDir=os.getcwd(),
                           wildcard=wildcards, style=wx.FD_OPEN)

        path, read = None, False
        if dlg.ShowModal() == wx.ID_OK:
            read = True
            path = dlg.GetPath().replace('\\', '/')
        dlg.Destroy()
        
        if read:
            try:
                self.ai = pyFAI.load(path)
                print('Loading calibration file: %s' % path)
                #self.showPONI()
                self.btn_integ.Enable()
            except:
                print('Not recognized as a pyFAI calibration file: %s' % path)
Example #36
0
    def openPONI(self, event=None):

        wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*'
        dlg = wx.FileDialog(self,
                            message='Choose pyFAI calibration file',
                            defaultDir=os.getcwd(),
                            wildcard=wildcards,
                            style=wx.FD_OPEN)

        path, read = None, False
        if dlg.ShowModal() == wx.ID_OK:
            read = True
            path = dlg.GetPath().replace('\\', '/')
        dlg.Destroy()

        if read:

            try:
                print
                self.ai = pyFAI.load(path)
                print('Loading calibration file: %s' % path)
            except:
                print('Not recognized as a pyFAI calibration file: %s' % path)
                return

            ## Sets viewer to values in .poni file
            self.entr_dist.SetValue('%0.4f' % self.ai._dist)
            self.entr_pix.SetValue('%0.1f' %
                                   float(self.ai.detector.pixel1 * 1000000.))
            self.ch_DorP.SetSelection(1)
            self.entr_EorL.SetValue('%0.4f' %
                                    float(self.ai._wavelength * 1.e10))
            self.ch_EorL.SetSelection(1)
            self.onDorPSel()

            cenx = float(self.ai._poni1) / float(self.ai.detector.pixel1)
            ceny = float(self.ai._poni2) / float(self.ai.detector.pixel2)
            self.entr_cntrx.SetValue('%0.3f' % cenx)
            self.entr_cntry.SetValue('%0.3f' % ceny)
Example #37
0
def calc_cake(xrd2d, calfile, unit='q', mask=None, dark=None,
              xsteps=2048, ysteps=2048, verbose=False):

    if HAS_pyFAI:
        try:
            ai = pyFAI.load(calfile)
        except:
            print('Provided calibration file could not be loaded.')
            return
    else:
        print('pyFAI not imported. Cannot calculate 1D integration.')
    attrs = {}
    if unit.startswith('2th'):
        attrs.update({'unit':'2th_deg'})
    else:
        attrs.update({'unit':'q_A^-1'})
    if mask:
        if np.shape(mask) == np.shape(xrd2d): attrs.update({'mask':mask})
    if dark:
        if np.shape(dark) == np.shape(xrd2d): attrs.update({'dark':dark})

    return calcXRDcake(xrd2d, ai, xsteps, ysteps, attrs)
Example #38
0
def integrate_xrd(xrd_map, ai=None,AI=None, calfile=None, unit='q', steps=10000, 
                  save=False, file='~/test.xy', mask=None, dark=None, verbose=False):

    if HAS_pyFAI:
        if ai is None:
            if AI is None:
                try:
                    ai = pyFAI.load(calfile)
                except IOError:
                    print('No calibration parameters specified.')
                    return
            else:
                ai = calculate_ai(AI)
        
        attrs = {}
        if unit == '2th':
            attrs.update({'unit':'2th_deg'})
        else:
            attrs.update({'unit':'q_A^-1'})
        if mask:
            attrs.update({'mask':mask})
        if dark:
            attrs.update({'dark':dark})        
        if save:
            print('Saving %s data to file: %s\n' % (unit,file))
            attrs.update({'filename':file})

        if verbose:
            t0 = time.time()
        qI = ai.integrate1d(xrd_map,steps,**attrs)
        if verbose:
            t1 = time.time()
            print('\tTime to integrate data = %0.3f s' % ((t1-t0)))

    else:
        print('pyFAI not imported. Cannot calculate 1D integration without it.')
        return

    return qI
Example #39
0
    def openPONI(self,event):
             
        wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*'
        dlg = wx.FileDialog(self, message='Choose pyFAI calibration file',
                           defaultDir=os.getcwd(),
                           wildcard=wildcards, style=wx.FD_OPEN)

        path, read = None, False
        if dlg.ShowModal() == wx.ID_OK:
            read = True
            path = dlg.GetPath().replace('\\', '/')
        dlg.Destroy()
        
        if read:

            try:
                print('Loading calibration file: %s' % path)
                ai = pyFAI.load(path)
            except:
                print('Not recognized as a pyFAI calibration file.')
                return

            self.addLAMBDA(ai._wavelength,units='m')
Example #40
0
    def openPONI(self, event=None):

        wildcards = 'pyFAI calibration file (*.poni)|*.poni|All files (*.*)|*.*'
        dlg = wx.FileDialog(self,
                            message='Choose pyFAI calibration file',
                            defaultDir=os.getcwd(),
                            wildcard=wildcards,
                            style=wx.FD_OPEN)

        path, read = None, False
        if dlg.ShowModal() == wx.ID_OK:
            read = True
            path = dlg.GetPath().replace('\\', '/')
        dlg.Destroy()

        if read:
            try:
                self.ai = pyFAI.load(path)
                print('Loading calibration file: %s' % path)
                #self.showPONI()
                self.btn_integ.Enable()
            except:
                print('Not recognized as a pyFAI calibration file: %s' % path)
Example #41
0
 def set_ponifile(self, ponifile=None):
     if ponifile is None:
         ponifile = self.poni.text()
         print ponifile
     try:
         self.ai = pyFAI.load(ponifile)
     except Exception as error:
         logger.error("file %s does not look like a poni-file, error %s" % (ponifile, error))
         return
     self.pixel1.setText(str(self.ai.pixel1))
     self.pixel2.setText(str(self.ai.pixel2))
     self.dist.setText(str(self.ai.dist))
     self.poni1.setText(str(self.ai.poni1))
     self.poni2.setText(str(self.ai.poni2))
     self.rot1.setText(str(self.ai.rot1))
     self.rot2.setText(str(self.ai.rot2))
     self.rot3.setText(str(self.ai.rot3))
     self.splineFile.setText(self.ai.detector.splineFile or "")
     name = self.ai.detector.name.lower()
     if name in self.all_detectors:
         self.detector.setCurrentIndex(self.all_detectors.index(name))
     else:
         self.detector.setCurrentIndex(self.all_detectors.index("detector"))
Example #42
0
def run_pyfai_calib2(run: Header, data_key: str, output_dir: str = "xpdacq_calib",
                     file_prefix: str = r"{start[uid]}_", **kwargs) -> pyFAI.AzimuthalIntegrator:
    """Run the pyFAi calibration2 for a data entry in databroker."""
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    args = ["pyFAI-calib2"]
    # add wavelength
    if "w" not in kwargs and "wavelength" not in kwargs and "bt_wavelength" in run.start:
        kwargs["wavelength"] = run.start["bt_wavelength"]
    # add poni file
    poni_path = output_dir.joinpath(
        file_prefix.format(start=run.start, stop=run.stop) + "{}.poni".format(data_key))
    kwargs["poni"] = str(poni_path)
    # add kwargs to args
    for k, v in kwargs.items():
        if len(k) == 1:
            args.append("-{}".format(k))
        else:
            args.append("--{}".format(k))
        args.append(str(v))
    # write out the file
    tiff_path = output_dir.joinpath(
        file_prefix.format(start=run.start, stop=run.stop) + "{}.tiff".format(data_key))
    avg_img: np.ndarray = _mean(run.data(data_key))
    tw = TiffWriter(str(tiff_path))
    tw.write(avg_img)
    args.append(str(tiff_path))
    # run the pyFAI-calib2
    cp = subprocess.run(args, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    if cp.returncode != 0:
        raise RuntimeError("Error in pyFAI-calib2:\n{}".format(cp.stderr.decode()))
    if not poni_path.is_file():
        raise FileNotFoundError(
            "No poni file {}. Did you change the file name when you save the poni file?".format(
                str(poni_path)))
    ai = pyFAI.load(str(poni_path))
    return ai
Example #43
0
def save1D(filename, xaxis, I, error=None, xaxis_unit=None, calfile=None,
           has_dark=False, has_flat=False, polarization_factor=None,
           normalization_factor=None):
    '''
    copied and modified from pyFAI/io.py
    '''
    if xaxis_unit is None or xaxis_unit == 'q':
        xaxis_unit = pyFAI.units.Q_A
    elif xaxis_unit.startswith('2th'):
        xaxis_unit = pyFAI.units.TTH_DEG

    if calfile is None:
        ai = None
    else:
        ai = pyFAI.load(calfile)

    xaxis_unit = pyFAI.units.to_unit(xaxis_unit)
    with open(filename, 'w') as f:
        f.write(make_headers(has_dark=has_dark, has_flat=has_flat, ai=ai,
                                 polarization_factor=polarization_factor,
                                 normalization_factor=normalization_factor))
        try:
            f.write('\n# --> %s\n' % (filename))
        except UnicodeError:
            f.write('\n# --> %s\n' % (filename.encode('utf8')))
        if error is None:
            try:
                f.write('#%14s %14s\n' % (xaxis_unit.REPR, 'I '))
            except:
                f.write('#%14s %14s\n' % (xaxis_unit.name, 'I '))
            f.write('\n'.join(['%14.6e  %14.6e' % (t, i) for t, i in zip(xaxis, I)]))
        else:
            f.write('#%14s  %14s  %14s\n' %
                    (xaxis_unit.REPR, 'I ', 'sigma '))
            f.write('\n'.join(['%14.6e  %14.6e %14.6e' % (t, i, s) for t, i, s in zip(xaxis, I, error)]))
        f.write('\n')
Example #44
0
import numpy as np
import pyFAI
import pytest
import tifffile

from xpdsim import pyfai_poni, image_file
from xpdtools.pipelines.raw_pipeline import (
    pipeline_order,
    namespace as g_namespace,
)
from streamz_ext.link import link
from xpdtools.pipelines.extra import z_score_gen
from streamz_ext import destroy_pipeline

img = tifffile.imread(image_file)
geo = pyFAI.load(pyfai_poni)


@pytest.mark.parametrize("mask_s", ["first", "none", "auto"])
def test_raw_pipeline(mask_s):
    # link the pipeline up
    namespace = link(*pipeline_order, **g_namespace)

    is_calibration_img = namespace["is_calibration_img"]
    geo_input = namespace["geo_input"]
    img_counter = namespace["img_counter"]
    namespace["mask_setting"]["setting"] = mask_s

    pdf = namespace["pdf"]
    raw_background_dark = namespace["raw_background_dark"]
    raw_background = namespace["raw_background"]
Example #45
0
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Fast Azimuthal integration
#             https://github.com/pyFAI/pyFAI
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer ([email protected])
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import pyFAI, numpy
ai = pyFAI.load("mock.poni")
shape = (600, 600)
ai.xrpd_OpenCL(numpy.ones(shape), 500, devicetype="cpu", useFp64=False)
Example #46
0
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import cProfile, pstats, StringIO
import unittest
import pyFAI, fabio
aimg = fabio.open("testimages/halfccd.edf").data
ai = pyFAI.load("testimages/halfccd.poni")
a, b = ai.separate(aimg)
pr = cProfile.Profile()
pr.enable()
a, b = ai.separate(aimg)
pr.disable()
pr.dump_stats(__file__ + ".numpy.log")
a, b = ai.separate(aimg, method="ocl_csr")
pr = cProfile.Profile()
pr.enable()
a, b = ai.separate(aimg, method="ocl_csr")
pr.disable()
pr.dump_stats(__file__ + ".opencl.log")
# s = StringIO.StringIO()
# sortby = 'cumulative'
# ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
Example #47
0
def read_lambda(calfile):

    ai = pyFAI.load(calfile)
    return ai._wavelength * 1e10  ## units A
Example #48
0
    def setJsonConfig(self, jsonconfig):
        print("start config ...")
        if os.path.isfile(jsonconfig):
            config = json.load(open(jsonconfig, "r"))
        else:
            config = json.loads(jsonconfig)
        if "poni" in config:
            poni = config["poni"]
            if poni and os.path.isfile(poni):
                self.ai = pyFAI.load(poni)

        detector = config.get("detector", "detector")
        self.ai.detector = pyFAI.detectors.detector_factory(detector)

        if "wavelength" in config:
            wavelength = config["wavelength"]
            try:
                fwavelength = float(wavelength)
            except ValueError:
                logger.error("Unable to convert wavelength to float: %s" %
                             wavelength)
            else:
                if fwavelength <= 0 or fwavelength > 1e-6:
                    logger.warning(
                        "Wavelength is in meter ... unlikely value %s" %
                        fwavelength)
                self.ai.wavelength = fwavelength

        splineFile = config.get("splineFile")
        if splineFile and os.path.isfile(splineFile):
            self.ai.detector.splineFile = splineFile
        self.ai.pixel1 = float(config.get("pixel1", 1))
        self.ai.pixel2 = float(config.get("pixel2", 1))
        self.ai.dist = config.get("dist", 1)
        self.ai.poni1 = config.get("poni1", 0)
        self.ai.poni2 = config.get("poni2", 0)
        self.ai.rot1 = config.get("rot1", 0)
        self.ai.rot2 = config.get("rot2", 0)
        self.ai.rot3 = config.get("rot3", 0)

        if config.get("chi_discontinuity_at_0"):
            self.ai.setChiDiscAtZero()

        mask_file = config.get("mask_file")
        do_mask = config.get("do_mask")
        if mask_file and os.path.exists(mask_file) and do_mask:
            try:
                mask = fabio.open(mask_file).data
            except Exception as error:
                logger.error("Unable to load mask file %s, error %s" %
                             (mask_file, error))
            else:
                self.ai.mask = mask
                self.mask_image = os.path.abspath(mask_file)

        self.ai.set_darkfiles([
            i.strip() for i in config.get("dark_current", "").split(",")
            if os.path.isfile(i.strip())
        ])
        self.ai.set_flatfiles([
            i.strip() for i in config.get("flat_field", "").split(",")
            if os.path.isfile(i.strip())
        ])
        self.dark_current_image = self.ai.darkfiles
        self.flat_field_image = self.ai.flatfiles
        if config.get("do_2D"):
            self.nbpt_azim = int(config.get("azim_pt"))
        else:
            self.nbpt_azim = 1
        if config.get("rad_pt"):
            self.nbpt_rad = int(config.get("rad_pt"))
        self.unit = pyFAI.units.to_unit(config.get("unit",
                                                   pyFAI.units.TTH_DEG))
        self.do_poisson = config.get("do_poisson")
        if config.get("do_polarization"):
            self.polarization = config.get("polarization")
        else:
            self.polarization = None
        logger.info(self.ai.__repr__())
        self.reset()
Example #49
0
    def setJsonConfig(self, jsonconfig):
        print("start config ...")
        if os.path.isfile(jsonconfig):
            config = json.load(open(jsonconfig, "r"))
        else:
            config = json.loads(jsonconfig)
        if "poni" in config:
            poni = config["poni"]
            if poni and os.path.isfile(poni):
                self.ai = pyFAI.load(poni)

        detector = config.get("detector", "detector")
        self.ai.detector = pyFAI.detectors.detector_factory(detector)

        if "wavelength" in config:
            wavelength = config["wavelength"]
            try:
                fwavelength = float(wavelength)
            except ValueError:
                logger.error("Unable to convert wavelength to float: %s" % wavelength)
            else:
                if fwavelength <= 0 or fwavelength > 1e-6:
                    logger.warning("Wavelength is in meter ... unlikely value %s" % fwavelength)
                self.ai.wavelength = fwavelength

        splineFile = config.get("splineFile")
        if splineFile and os.path.isfile(splineFile):
            self.ai.detector.splineFile = splineFile
        self.ai.pixel1 = float(config.get("pixel1", 1))
        self.ai.pixel2 = float(config.get("pixel2", 1))
        self.ai.dist = config.get("dist", 1)
        self.ai.poni1 = config.get("poni1", 0)
        self.ai.poni2 = config.get("poni2", 0)
        self.ai.rot1 = config.get("rot1", 0)
        self.ai.rot2 = config.get("rot2", 0)
        self.ai.rot3 = config.get("rot3", 0)


        if config.get("chi_discontinuity_at_0"):
            self.ai.setChiDiscAtZero()

        mask_file = config.get("mask_file")
        do_mask = config.get("do_mask")
        if mask_file and os.path.exists(mask_file) and do_mask:
            try:
                mask = fabio.open(mask_file).data
            except Exception as error:
                logger.error("Unable to load mask file %s, error %s" % (mask_file, error))
            else:
                self.ai.mask = mask
                self.mask_image = os.path.abspath(mask_file)

        self.ai.set_darkfiles([i.strip() for i in config.get("dark_current", "").split(",")
                               if os.path.isfile(i.strip())])
        self.ai.set_flatfiles([i.strip() for i in config.get("flat_field", "").split(",")
                               if os.path.isfile(i.strip())])
        self.dark_current_image = self.ai.darkfiles
        self.flat_field_image = self.ai.flatfiles
        if config.get("do_2D"):
            self.nbpt_azim = int(config.get("azim_pt"))
        else:
            self.nbpt_azim = 1
        if config.get("rad_pt"):
            self.nbpt_rad = int(config.get("rad_pt"))
        self.unit = pyFAI.units.to_unit(config.get("unit", pyFAI.units.TTH_DEG))
        self.do_poisson = config.get("do_poisson")
        if config.get("do_polarization"):
            self.polarization = config.get("polarization")
        else:
            self.polarization = None
        logger.info(self.ai.__repr__())
        self.reset()
Example #50
0
def calibration(json: str, params: Calibration) -> None:
    """Do a calibration with a bunch of images"""

    # Definition of the geometry refinement: the parameter order is
    # the same as the param_names
    calibrant = get_calibrant(params.calibrant,
                              params.wavelength)
    detector = get_detector(params.detector)

    parameters = {p.name: p.value for p in params.initial_parameters}
    bounds = {p.name: p.bounds for p in params.initial_parameters}
    param_names = [p.name for p in params.initial_parameters]

    # Let's refine poni1 and poni2 also as function of the distance:

    trans_function = GeometryTransformation(param_names=param_names,
                                            pos_names=["delta"],
                                            dist_expr="dist",
                                            poni1_expr="poni1",  # noqa
                                            poni2_expr="poni2",  # noqa
                                            rot1_expr="rot1",
                                            rot2_expr="rot2_scale * delta + rot2_offset",  # noqa
                                            rot3_expr="rot3")

    def pos_function(frame: CalibrationFrame) -> Tuple[float]:
        """Definition of the function reading the detector position from the
        header of the image."""
        return (frame.delta,)

    gonioref = GoniometerRefinement(parameters,  # initial guess
                                    bounds=bounds,
                                    pos_function=pos_function,
                                    trans_function=trans_function,
                                    detector=detector,
                                    wavelength=params.wavelength)

    print("Empty refinement object:")
    print(gonioref)

    # Let's populate the goniometer refinement object with the know poni

    with File(params.filename, mode='r') as h5file:
        for frame in gen_metadata_idx(h5file, params):
            base = os.path.splitext(os.path.basename(params.filename))[0]

            label = base + "_%d" % (frame.idx,)
            control_points = params.filename + "_{:02d}.npt".format(frame.idx)
            ai = pyFAI.load(params.filename + "_{:02d}.poni".format(frame.idx))
            print(ai)

            gonioref.new_geometry(label, frame.image, frame,
                                  control_points, calibrant, ai)

        print("Filled refinement object:")
        print(gonioref)
        print(os.linesep + "\tlabel \t tx")
        for k, v in gonioref.single_geometries.items():
            print(k, v.get_position())

        for g in gonioref.single_geometries.values():
            ai = gonioref.get_ai(g.get_position())
            print(ai)

        for sg in gonioref.single_geometries.values():
            jupyter.display(sg=sg)

        gonioref.refine2()

    for multi in [params]:
        with File(multi.filename, mode='r') as h5file:
            optimize_with_new_images(h5file, multi, gonioref, calibrant)

    for idx, sg in enumerate(gonioref.single_geometries.values()):
        sg.geometry_refinement.set_param(gonioref.get_ai(sg.get_position()).param)  # noqa
        jupyter.display(sg=sg)

    gonioref.save(json)
Example #51
0
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Fast Azimuthal integration
#             https://github.com/pyFAI/pyFAI
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer ([email protected])
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import pyFAI, numpy
ai = pyFAI.load("moke.poni")
shape = (600, 600)
ai.xrpd_OpenCL(numpy.ones(shape), 500, devicetype="cpu", useFp64=False)
Example #52
0
poni = os.path.join(root, "LaB6.poni")
bins = 2048
res = []
list_size = [2 ** i for i in range(10)]
with open(poni, "r") as f:
    for l in f:
        if l.startswith("SplineFile"):
            res.append("SplineFile: %s%s" % (spline, os.linesep))
        else:
            res.append(l)
with open(poni, "w") as f:
    f.writelines(res)
edf = os.path.join(root, "LaB6_0020.edf")

img = fabio.open(edf)
ai = pyFAI.load(poni)
ai.xrpd(img.data, bins)
tth = ai._ttha.ravel().astype(numpy.float32)
dtth = ai._dttha.ravel().astype(numpy.float32)
data = img.data.ravel().astype(numpy.float32)

import splitBBox
t0 = time.time()
ra, rb, rc, rd = splitBBox.histoBBox1d(data, tth, dtth, bins=bins)
t1 = time.time()
ref_time = 1000 * (t1 - t0)
print("ref time: %.2fms" % ref_time)


try:
    from pyFAI import ocl_azim
Example #53
0
    def setJsonConfig(self, jsonconfig):
        print("start config ...")
        if os.path.isfile(jsonconfig):
            config = json.load(open(jsonconfig, "r"))
        else:
            config = json.loads(jsonconfig)
        if "poni" in config:
            poni = config["poni"]
            if poni and os.path.isfile(poni):
                self.ai = pyFAI.load(poni)

        detector = config.get("detector", "detector")
        self.ai.detector = pyFAI.detectors.detector_factory(detector)

        if "wavelength" in config:
            wavelength = config["wavelength"]
            try:
                fwavelength = float(wavelength)
            except ValueError:
                logger.error("Unable to convert wavelength to float: %s" % wavelength)
            else:
                if fwavelength <= 0 or fwavelength > 1e-6:
                    logger.warning("Wavelength is in meter ... unlikely value %s" % fwavelength)
                self.ai.wavelength = fwavelength

        splineFile = config.get("splineFile")
        if splineFile and os.path.isfile(splineFile):
            self.ai.detector.splineFile = splineFile
        self.ai.pixel1 = float(config.get("pixel1", 1))
        self.ai.pixel2 = float(config.get("pixel2", 1))
        self.ai.dist = config.get("dist", 1)
        self.ai.poni1 = config.get("poni1", 0)
        self.ai.poni2 = config.get("poni2", 0)
        self.ai.rot1 = config.get("rot1", 0)
        self.ai.rot2 = config.get("rot2", 0)
        self.ai.rot3 = config.get("rot3", 0)


        if config.get("chi_discontinuity_at_0"):
            self.ai.setChiDiscAtZero()

        mask_file = config.get("mask_file")
        do_mask = config.get("do_mask")
        if mask_file and os.path.exists(mask_file) and do_mask:
            try:
                mask = getDataFromFile(mask_file).buffer
            except Exception as error:
                logger.error("Unable to load mask file %s, error %s" % (mask_file, error))
            else:
                self.ai.mask = mask
        dark_files = [i.strip() for i in config.get("dark_current", "").split(",")
                      if os.path.isfile(i.strip())]
        if dark_files and config.get("do_dark"):
            d0 = getDataFromFile(dark_files[0]).buffer
            darks = numpy.zeros(d0.shape[0], d0.shape[1], len(dark_files), dtype=numpy.float32)
            for i, f in enumerate(dark_files):
                darks[:, :, i] = getDataFromFile(f).buffer
            self.ai.darkcurrent = darks.mean(axis= -1)

        flat_files = [i.strip() for i in config.get("flat_field", "").split(",")
                      if os.path.isfile(i.strip())]
        if flat_files and config.get("do_flat"):
            d0 = getDataFromFile(flat_files[0]).buffer
            flats = numpy.zeros(d0.shape[0], d0.shape[1], len(flat_files), dtype=numpy.float32)
            for i, f in enumerate(flat_files):
                flats[:, :, i] = getDataFromFile(f).buffer
            self.ai.darkcurrent = flats.mean(axis= -1)

        if config.get("do_2D"):
            self.nbpt_azim = int(config.get("azim_pt"))
        else:
            self.nbpt_azim = 1
        if config.get("rad_pt"):
            self.nbpt_rad = int(config.get("rad_pt"))
        self.unit = pyFAI.units.to_unit(config.get("unit", pyFAI.units.TTH_DEG))

        logger.info(self.ai.__repr__())
        self.reset()
Example #54
0
def read_lambda(calfile):

    ai = pyFAI.load(calfile)
    return ai._wavelength*1e10 ## units A
Example #55
0
NI_FGR_FILE = resource_filename('tests', 'test_data/Ni_fgr_file.fgr')
NI_IMG_FILE = resource_filename('tests', 'test_data/Ni_img_file.tiff')
MASK_FILE = resource_filename("tests", "test_data/mask_file.npy")
KAPTON_IMG_FILE = resource_filename('tests', 'test_data/Kapton_img_file.tiff')
BLACK_IMG_FILE = resource_filename('tests', 'test_data/black_img.tiff')
WHITE_IMG_FILE = resource_filename('tests', 'test_data/white_img.tiff')
NI_IMG = load_img(NI_IMG_FILE)
NI_FRAMES = numpy.expand_dims(NI_IMG, 0)
KAPTON_IMG = load_img(KAPTON_IMG_FILE)
NI_GR = load_array(NI_GR_FILE)
NI_CHI = load_array(NI_CHI_FILE)
NI_FGR = load_array(NI_FGR_FILE)
NI_CONFIG = PDFConfig()
NI_CONFIG.readConfig(NI_GR_FILE)
NI_PDFGETTER = PDFGetter(NI_CONFIG)
AI = pyFAI.load(NI_PONI_FILE)
MASK = numpy.load(MASK_FILE)
BLACK_IMG = load_img(BLACK_IMG_FILE)
WHITE_IMG = load_img(WHITE_IMG_FILE)
START_DOC_FILE = resource_filename('tests', 'test_data/start.json')
with Path(START_DOC_FILE).open("r") as f:
    START_DOC = json.load(f)

DB = {
    'Ni_img_file': NI_IMG_FILE,
    'Ni_img': NI_IMG,
    'Kapton_img_file': KAPTON_IMG_FILE,
    'Kapton_img': KAPTON_IMG,
    'Ni_poni_file': NI_PONI_FILE,
    'Ni_gr_file': NI_GR_FILE,
    'Ni_chi_file': NI_CHI_FILE,
Example #56
0
NI_PDFGETTER = PDFGetter(NI_CONFIG)
ZRP_CIF = resource_filename('tests', 'test_data/ZrP.cif')
NI_CRYSTAL = loadCrystal(NI_CIF)
ZRP_CRYSTAL = loadCrystal(ZRP_CIF)
NI_DIFFPY = loadStructure(NI_CIF)

DB = {
    'Ni_img_file': NI_IMG,
    'Ni_img': load_img(NI_IMG),
    'Kapton_img_file': KAPTON_IMG,
    'Kapton_img': load_img(KAPTON_IMG),
    'Ni_poni_file': NI_PONI,
    'Ni_gr_file': NI_GR,
    'Ni_chi_file': NI_CHI,
    'Ni_fgr_file': NI_FGR,
    'ai': pyFAI.load(NI_PONI),
    'Ni_gr': load_data(NI_GR).T,
    'Ni_chi': load_data(NI_CHI).T,
    'Ni_fgr': load_data(NI_FGR).T,
    'black_img_file': BLACK_IMG,
    'white_img_file': WHITE_IMG,
    'black_img': numpy.zeros((128, 128)),
    'white_img': numpy.ones((128, 128)),
    'Ni_config': NI_CONFIG,
    'Ni_pdfgetter': NI_PDFGETTER,
    'Ni_stru_file': NI_CIF,
    'Ni_stru': NI_CRYSTAL,
    'Ni_stru_molecule': Molecule(NI_CRYSTAL),
    'Ni_stru_diffpy': NI_DIFFPY,
    'ZrP_stru': ZRP_CRYSTAL
}
Example #57
0
from xpdtools.pipelines.raw_pipeline import pipeline_order, namespace

from profilehooks import profile
from rapidz.link import link

namespace["mask_setting"].update(setting="first")

namespace = link(*pipeline_order, **namespace)

# dark_corrected_background.sink(print)
# pol_corrected_img_zip.sink(print)
# mask.sink(print)
# binner.sink(print)
# mean.sink(print)

geo = pyFAI.load("test.poni")
img = imread("test.tiff")

namespace["geometry"].emit(geo)
namespace["composition"].emit("Au")
for n in [
        namespace["raw_background_dark"],
        namespace["raw_background"],
        namespace["raw_foreground_dark"],
]:
    n.emit(np.zeros(img.shape))


@profile(skip=1000, sort="tottime", entries=20)
def f(i):
    namespace["img_counter"].emit(i)
Example #58
0
def return_ai(calfile):

    if calfile is not None and os.path.exists(calfile):
        return pyFAI.load(calfile)
Example #59
0
    return master_list


if __name__ == '__main__':
    import fabio
    import pyFAI
    import matplotlib.pyplot as plt
    from matplotlib.colors import LogNorm
    from pims.tiff_stack import TiffStack_tifffile as TiffStack


    # load experiment information
    geo = pyFAI.load(
        '/mnt/bulk-data/research_data/USC_beamtime/08-05-2015/2015-08-05/Ni_STD/Ni_PDF_60s-00000.poni'
        # '/mnt/bulk-data/research_data/Low T-MST-summed/Ni_STD_XRD300_60S-00000.poni'
        # '/mnt/bulk-data/Dropbox/BNL_Project/misc/CGO_summed/CGO_summed/Ni_stnd/Ni_STD_60s-00004.poni'
    )

    # start_mask = fabio.open(
        # '/mnt/bulk-data/research_data/USC_beamtime/08-05-2015/2015-08-05/xpd_pdf_beamstop.msk'
        # '/mnt/bulk-data/research_data/Low T-MST-summed/low_T_mst.msk'
    # ).data
    # start_mask = np.zeros((2048, 2048)).astype(bool)
    # start_mask = np.flipud(start_mask)
    start_mask = np.load(
        '/mnt/bulk-data/research_data/USC_beamtime/08-05-2015/2015-08-05/Au2nm_Cold/xpd_pdf_mask_Au2nm_60s_120K_sum.npy'
        )

    # load images
    # folder, name = ('/mnt/bulk-data/research_data/USC_beamtime/08-05-2015/2015-08-05/Au2nm_Temp/Au2nm_300-400', 'Au2nm_300-400')