Exemple #1
0
    def copy_group(self, filename, new_filename=None):
        """copy XAS group (by filename) to new group"""
        groupname = self.file_groups[filename]
        if not hasattr(self.larch.symtable, groupname):
            return

        ogroup = self.get_group(groupname)

        ngroup = Group(datatype=ogroup.datatype,
                       copied_from=groupname)

        for attr in dir(ogroup):
            do_copy = True
            if attr in ('xdat', 'ydat', 'i0', 'data' 'yerr',
                        'energy', 'mu'):
                val = getattr(ogroup, attr)*1.0
            elif attr in ('norm', 'flat', 'deriv', 'deconv',
                          'post_edge', 'pre_edge', 'norm_mback',
                          'norm_vict', 'norm_poly'):
                do_copy = False
            else:
                try:
                    val = copy.deepcopy(getattr(ogroup, attr))
                except ValueError:
                    do_copy = False
            if do_copy:
                setattr(ngroup, attr, val)

        if new_filename is None:
            new_filename = filename + '_1'
        ngroup.filename = unique_name(new_filename, self.file_groups.keys())
        ngroup.groupname = unique_name(groupname, self.file_groups.values())
        setattr(self.larch.symtable, ngroup.groupname, ngroup)
        return ngroup
Exemple #2
0
    def __init__(self,
                 data2D=None,
                 xpixels=2048,
                 ypixels=2048,
                 data1D=None,
                 nwedge=0,
                 steps=5001,
                 name='xrd',
                 _larch=None,
                 **kws):

        self.name = name
        self.xpix = xpixels
        self.ypix = ypixels
        self.data2D = data2D
        self.nwedge = nwedge
        self.steps = steps
        self.data1D = data1D
        self.data2D = data2D
        self.cake = None

        self.energy = None
        self.wavelength = None
        self.calfile = None

        self.filename = None
        self.title = None
        self.npixels = None

        if HAS_larch:
            Group.__init__(self)
Exemple #3
0
    def __init__(self, filename=None, _larch=None,
                 label=None, s02=None, degen=None, e0=None,
                 ei=None, deltar=None, sigma2=None,
                 third=None, fourth=None,  **kws):

        kwargs = dict(name='FeffPath: %s' % filename)
        kwargs.update(kws)
        Group.__init__(self, **kwargs)
        self._larch = _larch
        self.filename = filename
        def_degen = 1
        if filename is not None:
            self._feffdat = FeffDatFile(filename=filename, _larch=_larch)
            self.geom  = self._feffdat.geom
            def_degen  = self._feffdat.degen
        self.degen = degen if degen is not None else def_degen
        self.label = label if label is not None else filename
        self.s02    = 1 if s02    is None else s02
        self.e0     = 0 if e0     is None else e0
        self.ei     = 0 if ei     is None else ei
        self.deltar = 0 if deltar is None else deltar
        self.sigma2 = 0 if sigma2 is None else sigma2
        self.third  = 0 if third  is None else third
        self.fourth = 0 if fourth is None else fourth
        self.k = None
        self.chi = None
Exemple #4
0
    def copy_group(self, filename, new_filename=None):
        """copy XAS group (by filename) to new group"""
        groupname = self.file_groups[filename]
        if not hasattr(self.larch.symtable, groupname):
            return

        ogroup = self.get_group(groupname)

        ngroup = Group(datatype=ogroup.datatype, copied_from=groupname)

        for attr in dir(ogroup):
            do_copy = True
            if attr in ('xdat', 'ydat', 'i0', 'data' 'yerr', 'energy', 'mu'):
                val = getattr(ogroup, attr) * 1.0
            elif attr in ('norm', 'flat', 'deriv', 'deconv', 'post_edge',
                          'pre_edge'):
                do_copy = False
            else:
                try:
                    val = copy.deepcopy(getattr(ogroup, attr))
                except ValueError:
                    do_copy = False
            if do_copy:
                setattr(ngroup, attr, val)

        if new_filename is None:
            new_filename = filename + '_1'
        ngroup.filename = unique_name(new_filename, self.file_groups.keys())
        ngroup.groupname = unique_name(groupname, self.file_groups.values())
        setattr(self.larch.symtable, ngroup.groupname, ngroup)
        return ngroup
Exemple #5
0
    def __init__(self, kmin=0, kmax=20, kweight=2, dk=4, dk2=None,
                 window='kaiser', nfft=2048, kstep=0.05,
                 rmin = 0, rmax=10, dr=0, dr2=None, rwindow='hanning',
                 fitspace='r', wavelet_mask=None, _larch=None, **kws):
        Group.__init__(self, **kws)
        self.kmin = kmin
        self.kmax = kmax
        self.kweight = kweight
        if 'kw' in kws:
            self.kweight = kws['kw']

        self.dk = dk
        self.dk2 = dk2
        self.window = window
        self.rmin = rmin
        self.rmax = rmax
        self.dr = dr
        self.dr2 = dr2
        if dr2 is None: self.dr2 = self.dr
        self.rwindow = rwindow
        self.__nfft = 0
        self.__kstep = None
        self.nfft  = nfft
        self.kstep = kstep
        self.rstep = pi/(self.kstep*self.nfft)

        self.fitspace = fitspace
        self.wavelet_mask = wavelet_mask
        self._cauchymask = None

        self._larch = _larch

        self.kwin = None
        self.rwin = None
        self.make_karrays()
Exemple #6
0
    def __init__(self, kmin=0, kmax=20, kweight=2, dk=4, dk2=None,
                 window='kaiser', nfft=2048, kstep=0.05,
                 rmin = 0, rmax=10, dr=0, dr2=None, rwindow='hanning',
                 fitspace='r', wavelet_mask=None, _larch=None, **kws):
        Group.__init__(self, **kws)
        self.kmin = kmin
        self.kmax = kmax
        self.kweight = kweight
        if 'kw' in kws:
            self.kweight = kws['kw']

        self.dk = dk
        self.dk2 = dk2
        self.window = window
        self.rmin = rmin
        self.rmax = rmax
        self.dr = dr
        self.dr2 = dr2
        if dr2 is None: self.dr2 = self.dr
        self.rwindow = rwindow
        self.__nfft = 0
        self.__kstep = None
        self.nfft  = nfft
        self.kstep = kstep
        self.rstep = pi/(self.kstep*self.nfft)

        self.fitspace = fitspace
        self.wavelet_mask = wavelet_mask
        self._cauchymask = None

        self._larch = _larch

        self.kwin = None
        self.rwin = None
        self.make_karrays()
Exemple #7
0
 def __init__(self, folder=None, _larch=None, **kws):
     kwargs = dict(name='Feff85exafs unit test: %s' % folder)
     kwargs.update(kws)
     Group.__init__(self,  **kwargs)
     self._larch     = Interpreter()
     self.doplot     = True  
     self.doscf      = False # True = use self-consistency
     self.verbose    = True  # True = print Feff's screen messages and other screenmessages
     self.feffran    = False # True = Feff calculation has been run
     self.count      = 0
     self.feffcount  = 0
     self.datacount  = 0
     self.failed     = list()
     self.folder     = folder
     if self.folder[-1] == '/': self.folder = self.folder[:-1]
     self.testrun    = realpath(join(self.folder, 'testrun'))
     self.testpaths()
     if not isdir(folder):
         print colored(folder + " is not one of the available tests", 'magenta', attrs=['bold'])
         return None
     self.path       = realpath(folder)
     self.repotop    = realpath(join('..','..'))
     # the f85e shell script emulates the behavior of the monolithic Feff application
     self.f85escript = join(self.repotop, 'bin', 'f85e')
     self.epsilon    = 0.00001
     self.epsfit     = 0.001
     self.wrapper_available = wrapper_available
     if wrapper_available:
         self.sp = scatpath()
Exemple #8
0
 def __init__(self, filename=None, _larch=None, **kws):
     self._larch = _larch
     kwargs = dict(name='feff.dat: %s' % filename)
     kwargs.update(kws)
     Group.__init__(self,  **kwargs)
     if filename is not None:
         self.__read(filename)
Exemple #9
0
    def setUp(self):
        self.param1 = Parameter(name='a', value=2.0, vary=True, min=0)
        self.param2 = Parameter(name='b', expr='sqrt(x/2)')
        self.group1 = Group(a=1.030405,
                            label='a string',
                            opts={
                                'a': 1,
                                'b': 2
                            },
                            x=np.linspace(0, 10, 21),
                            y=np.sqrt(np.arange(3) / 5.))

        self.group2 = Group(par1=Parameter(name='p1',
                                           value=3.0,
                                           min=0.0,
                                           vary=False),
                            par2=Parameter(name='p2', value=1.0, vary=True),
                            sub=Group(label='a label',
                                      x=np.linspace(0, 10, 21)))

        self.group3 = Group(par1=Parameter(name='p1', value=3.0, min=0.0),
                            dx={
                                'a': 1.3 + 0.2j,
                                'blist': [1, 2, 3.0],
                                'tup': (0, None)
                            })
Exemple #10
0
    def __init__(self,
                 filename=None,
                 _larch=None,
                 label=None,
                 s02=None,
                 degen=None,
                 e0=None,
                 ei=None,
                 deltar=None,
                 sigma2=None,
                 third=None,
                 fourth=None,
                 **kws):

        kwargs = dict(name='FeffPath: %s' % filename)
        kwargs.update(kws)
        Group.__init__(self, **kwargs)
        self._larch = _larch
        self.filename = filename
        def_degen = 1
        if filename is not None:
            self._feffdat = FeffDatFile(filename=filename, _larch=_larch)
            self.geom = self._feffdat.geom
            def_degen = self._feffdat.degen
        self.degen = degen if degen is not None else def_degen
        self.label = label if label is not None else filename
        self.s02 = 1 if s02 is None else s02
        self.e0 = 0 if e0 is None else e0
        self.ei = 0 if ei is None else ei
        self.deltar = 0 if deltar is None else deltar
        self.sigma2 = 0 if sigma2 is None else sigma2
        self.third = 0 if third is None else third
        self.fourth = 0 if fourth is None else fourth
        self.k = None
        self.chi = None
Exemple #11
0
 def __init__(self, filename=None, _larch=None, **kws):
     self._larch = _larch
     kwargs = dict(name='feff.dat: %s' % filename)
     kwargs.update(kws)
     Group.__init__(self, **kwargs)
     if filename is not None:
         self.__read(filename)
Exemple #12
0
    def as_group(self):
        """convert AthenaProect to Larch group"""
        out = Group()
        out.__doc__ = """XAFS Data from Athena Project File %s""" % (self.filename)
        out._athena_journal = self.journal
        out._athena_header = self.header

        for name, group in self.groups.items():
            setattr(out, name, group)
        return out
Exemple #13
0
    def as_group(self):
        """convert AthenaProect to Larch group"""
        out = Group()
        out.__doc__ = """XAFS Data from Athena Project File %s""" % (
            self.filename)
        out._athena_journal = self.journal
        out._athena_header = self.header

        for name, group in self.groups.items():
            setattr(out, name, group)
        return out
 def __init__(self, folder=None, _larch=None, **kws):
     kwargs = dict(name='FeffPath wrapper')
     kwargs.update(kws)
     Group.__init__(self,  **kwargs)
     self._larch     = Interpreter()
     self.wrapper    = feffpathwrapper.FEFFPATH()
     feffpathwrapper.create_path(self.wrapper)
     self.wrapper.phpad = ''
     self.ipot = []
     self.rat  = []
     self.geom = []
Exemple #15
0
def read_stepscan(fname, _larch=None, **kws):
    """read Epics Step Scan file to larch Group"""

    scan = EpicsScanData(fname)
    group = Group()
    group.__name__ ='Epics Step Sscan Data file %s' % fname
    for key, val in scan.__dict__.items():
        if not key.startswith('_'):
            setattr(group, key, val)

    group.get_data = scan.get_data
    return group
Exemple #16
0
 def __init__(self, name=None, shape='gaussian',
              amplitude=1, center=0, sigma=1,
              sigma_params=None, **kws):
     kwargs = {'name': name}
     kwargs.update(kws)
     self.amplitude = amplitude
     self.center   = center
     self.sigma = sigma
     Group.__init__(self)
     self.name = name
     if name is not None:
         self._define(name, shape=shape,
                      sigma_params=sigma_params)
Exemple #17
0
def parse_jsonathena(text, filename):
    """parse a JSON-style athena file"""
    jsdict = json.loads(text)

    out = Group()
    out.__doc__ = """XAFS Data from Athena Project File %s""" % (filename)

    header = []
    athena_names = []
    for key, val in jsdict.items():
        if key.startswith('_____head'):
            header.append(val)
        elif key.startswith('_____journ'):
            journal = val
        elif key.startswith('_____order'):
            athena_names = val

    out.journal = journal
    out.header = '\n'.join(header)
    out.group_names  = []
    for name in athena_names:
        label = name
        dat = jsdict[name]
        x = np.array(dat['x'], dtype='float64')
        y = np.array(dat['y'], dtype='float64')
        this = Group(athena_id=name, energy=x, mu=y,
                     bkg_params=Group(), fft_params=Group(),
                     athena_params=Group())
        if 'i0' in dat:
            this.i0 = np.array(dat['i0'], dtype='float64')
        if 'signal' in dat:
            this.signal = np.array(dat['signal'], dtype='float64')
        if 'stddev' in dat:
            this.stddev = np.array(dat['stddev'], dtype='float64')
        if 'args' in dat:
            for key, val in dat['args'].items():
                if key.startswith('bkg_'):
                    setattr(this.bkg_params, key[4:], asfloat(val))
                elif key.startswith('fft_'):
                    setattr(this.fft_params, key[4:], asfloat(val))
                elif key == 'label':
                    label = this.label = val
                else:
                    setattr(this.athena_params, key, asfloat(val))
        this.__doc__ = """Athena Group Name %s (key='%s')""" % (label, name)
        name = fix_varname(label)
        if name.startswith('_'):
            name = 'd' + name
        setattr(out, name, this)
        out.group_names.append(name)
    return out
Exemple #18
0
 def __init__(self, filename='spykscan.001', configfile=None,
              auto_increment=True, _larch=None, **kwargs):
     Group.__init__(self, **kwargs)
     self.motors  = {}
     self.detectors = []
     self.bare_counters = []
     self._scan = LarchStepScan(filename=filename,
                                auto_increment=auto_increment,
                                _larch=_larch)
     self.datafilename = filename
     if configfile is not None:
         self.configfile = configfile
     self.read_config(filename=configfile)
     self.lup = self.dscan
Exemple #19
0
    def __init__(self, energy=None, mu=None, z=None, edge='K', mback_kws=None, _larch=None, **kws):
        kwargs = dict(name='diffKK')
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self.energy     = energy
        self.mu         = mu
        self.z          = z
        self.edge       = edge
        self.mback_kws  = mback_kws

        if _larch == None:
            self._larch   = Interpreter()
        else:
            self._larch = _larch
Exemple #20
0
    def __init__(self, filename=None, bad=None, **kws):

        kwargs = {'name': 'GSE MCA File: %s' % filename}
        kwargs.update(kws)
        Group.__init__(self, **kwargs)
        self.mcas = []
        self.__mca0 = None
        self.bad = bad
        if bad is None:
            self.bad = []

        self.filename = filename
        if filename:
            self.read(filename=filename)
Exemple #21
0
    def __init__(self, filename=None, bad=None, **kws):

        kwargs = {'name': 'GSE MCA File: %s' % filename}
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self.mcas   = []
        self.__mca0 = None
        self.bad    = bad
        if bad is None:
            self.bad = []

        self.filename = filename
        if filename:
            self.read(filename=filename)
Exemple #22
0
    def __init__(self, energy=None, mu=None, z=None, edge='K', mback_kws=None, _larch=None, **kws):
        kwargs = dict(name='diffKK')
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self.energy     = energy
        self.mu         = mu
        self.z          = z
        self.edge       = edge
        self.mback_kws  = mback_kws

        if _larch == None:
            self._larch   = Interpreter()
        else:
            self._larch = _larch
def fit_peak(x, y, model, dy=None, background=None, step=None,
             negative=False, use_gamma=False, _larch=None):
    """fit peak to one a selection of simple 1d models

    out = fit_peak(x, y, model, dy=None,
                   background='linear', step='linear')

    arguments:
    ---------
    x           array of values at which to calculate model
    y           array of values for model to try to match
    dy          array of values for uncertainty in y data to be matched.
    model       name of model to use.  One of (case insensitive)
                     'linear', 'quadratic', 'step', 'rectangle',
                      'gaussian', 'lorentzian', 'voigt', 'exponential'
    background  name of background model to use. One of (case insensitive)
                     None, 'constant', 'linear', or 'quadratic'
                this is ignored when model is 'linear' or 'quadratic'
    step        name of step model to use for 'step' and 'rectangle' models.
                One of (case insensitive):
                    'linear', 'erf', or 'atan'
    negative    True/False for whether peak or steps are expected to go down.
    use_gamma   True/False for whether to use separate gamma parameter for
                voigt model.
    output:
    -------
    Group with fit parameters, and more...
    """
    out = Group(x=x*1.0, y=y*1.0, dy=1.0, model=model,
                background=background, step=step)
    if dy is not None:
        out.dy = 1.0*dy
    if model.lower() not in MODELS:
        _larch.writer.write('Unknown fit model: %s ' % model)
        return None

    kwargs = dict(negative=negative, background=background,
                  step=step, _larch=_larch)

    fitclass = MODELS[model.lower()]
    if fitclass == VoigtModel:
        kwargs['use_gamma'] = use_gamma

    mod = fitclass(**kwargs)
    mod.guess_starting_values(out.y, out.x)

    out.fit_init = mod.model(x=out.x)
    if background is not None:
        out.bkg_init = mod.calc_background(out.x)
        out.fit_init += out.bkg_init

    mod.fit(out.y, x=out.x, dy=out.dy, _larch=_larch)

    out.fit = mod.model(x=out.x)
    if background is not None:
        out.bkg = mod.calc_background(out.x)
        out.fit += out.bkg
    out.params = mod.params
    return out
Exemple #24
0
def gsescan_group(fname, _larch=None, bad=None, **kws):
    """simple mapping of EscanData file to larch groups"""
    escan = EscanData(fname, bad=bad)
    if escan.status is not None:
        raise ValueError('Not a valid Escan Data file')

    group = Group()
    group.__name__ ='GSE Escan Data file %s' % fname
    for key, val in escan.__dict__.items():
        if not key.startswith('_'):
            setattr(group, key, val)

    group.array_labels = group.pos_desc + group.sums_names
    group.get_data = escan.get_data
    return group
Exemple #25
0
    def __init__(self, data=None, pathlist=None, transform=None,
                 _larch=None, **kws):
        self._larch = _larch
        Group.__init__(self, **kws)

        self.pathlist = pathlist

        self.data = data
        if transform is None:
            transform = TransformGroup()
        self.transform = transform
        self.model = Group()
        self.model.k = None
        self.__chi = None
        self.__prepared = False
Exemple #26
0
def set_xafsGroup(group, _larch=None):
    """set _sys.xafsGroup to the s<upplied group (if not None)

    return _sys.xafsGroup.

    if needed, a new, empty _sys.xafsGroup may be created.
    """
    if group is None:
        if _larch is None:
            group = Group()
        else:
            group = getattr(_larch.symtable._sys, 'xafsGroup', Group())
    if _larch is not None:
        _larch.symtable._sys.xafsGroup = group
    return group
Exemple #27
0
 def __init__(self, feffinp=None, verbose=True, repo=None, _larch=None, **kws):
     kwargs = dict(name='Feff runner')
     kwargs.update(kws)
     Group.__init__(self,  **kwargs)
     if _larch == None:
         self._larch   = Interpreter()
     else:
         self._larch = _larch
     self.feffinp  = feffinp
     self.verbose  = verbose
     self.mpse     = False
     self.repo     = repo
     self.resolved = None
     self.threshold = []
     self.chargetransfer = []
Exemple #28
0
def gsescan_group(fname, _larch=None, bad=None, **kws):
    """simple mapping of EscanData file to larch groups"""
    escan = EscanData(fname, bad=bad)
    if escan.status is not None:
        raise ValueError('Not a valid Escan Data file')

    group = Group()
    group.__name__ = 'GSE Escan Data file %s' % fname
    for key, val in escan.__dict__.items():
        if not key.startswith('_'):
            setattr(group, key, val)

    group.array_labels = group.pos_desc + group.sums_names
    group.get_data = escan.get_data
    return group
Exemple #29
0
    def __init__(self, feffinp='feff.inp', folder=None, verbose=True,
                 repo=None, _larch=None, **kws):
        kwargs = dict(name='Feff runner')
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self._larch = _larch

        self.folder   = folder or '.'
        self.feffinp  = feffinp
        self.verbose  = verbose
        self.mpse     = False
        self.repo     = repo
        self.resolved = None
        self.threshold = []
        self.chargetransfer = []
Exemple #30
0
 def __init__(self,
              energy=None,
              mu=None,
              z=None,
              edge='K',
              mback_kws=None,
              **kws):
     kwargs = dict(name='diffKK')
     kwargs.update(kws)
     Group.__init__(self, **kwargs)
     self.energy = energy
     self.mu = mu
     self.z = z
     self.edge = edge
     self.mback_kws = mback_kws
Exemple #31
0
    def __init__(self,
                 counts=None,
                 nchans=2048,
                 start_time='',
                 offset=0,
                 slope=0,
                 quad=0,
                 name='mca',
                 dt_factor=1,
                 real_time=0,
                 live_time=0,
                 input_counts=0,
                 tau=0,
                 **kws):

        self.name = name
        self.nchans = nchans
        self.environ = []
        self.rois = []
        self.counts = counts

        # Calibration parameters
        self.offset = offset  # Offset
        self.slope = slope  # Slope
        self.quad = quad  # Quadratic

        # Counting parameters
        self.start_time = start_time
        self.real_time = real_time  # Elapsed real time in seconds (requested counting time)
        self.live_time = live_time  # Elapsed live time in seconds (time detector is live)
        self.input_counts = input_counts  # Actual total input counts (eg given by detector software)
        # Note total_counts and input counts ARE NOT time normalized
        #
        self.total_counts = 0.  # Total counts between the preset start and stop channels
        self.tau = tau  # Factor for deadtime/detector saturation calculations, ie
        #     ocr = icr * exp(-icr*tau)
        # Calculated correction values
        self.icr_calc = -1.0  # Calculated input count rate from above expression
        # corrected_counts = counts * dt_factor
        self.bgr = None
        self.dt_factor = float(dt_factor)
        if counts is not None:
            self.nchans = len(counts)
            self.total_counts = counts.sum()
        self.incident_energy = None
        self.get_energy()
        self._calc_correction()
        Group.__init__(self)
Exemple #32
0
def _path2chi(path, paramgroup=None, _larch=None, **kws):
    """calculate chi(k) for a Feff Path,
    optionally setting path parameter values
    output chi array will be written to path group

    Parameters:
    ------------
      path:        a FeffPath Group
      paramgroup:  a Parameter Group for calculating Path Parameters [None]
      kmax:        maximum k value for chi calculation [20].
      kstep:       step in k value for chi calculation [0.05].
      k:           explicit array of k values to calculate chi.

    Returns:
    ---------
      None - outputs are written to path group

    """
    if not isNamedClass(path, FeffPathGroup):
        msg('%s is not a valid Feff Path' % path)
        return
    if _larch is not None:
        if (paramgroup is not None and _larch.symtable.isgroup(paramgroup)):
            _larch.symtable._sys.paramGroup = paramgroup
        elif not hasattr(_larch.symtable._sys, 'paramGroup'):
            _larch.symtable._sys.paramGroup = Group()
    path._calc_chi(**kws)
Exemple #33
0
def decode4js(obj):
    """
    return decoded Python object from encoded object.
    """
    out = obj
    if isinstance(obj, dict):
        classname = obj.pop('__class__', None)
        if classname is None:
            return obj
        elif classname == 'Complex':
            out = obj['value'][0] + 1j*obj['value'][1]
        elif classname in ('List', 'Tuple'):
            out = []
            for item in obj['value']:
                out.append(decode4js(item))
            if classname == 'Tuple':
                out = tuple(out)
        elif classname == 'Array':
            if obj['__dtype__'].startswith('complex'):
                re = np.fromiter(obj['value'][0], dtype='double')
                im = np.fromiter(obj['value'][1], dtype='double')
                out = re + 1j*im
            else:
                out = np.fromiter(obj['value'], dtype=obj['__dtype__'])
            out.shape = obj['__shape__']
        elif classname in ('Dict', 'Parameter', 'Group'):
            out = {}
            for key, val in obj.items():
                out[key] = decode4js(val)
            if classname == 'Parameter':
                out = Parameter(**out)
            elif classname == 'Group':
                out = Group(**out)
    return out
Exemple #34
0
    def onCopyGroup(self, event=None):
        fname = self.current_filename
        if fname is None:
            fname = self.current_filename = self.controller.filelist.GetStringSelection()

        groupname = self.controller.file_groups[fname]
        if not hasattr(self.larch.symtable, groupname):
            return
        ogroup = self.controller.get_group(groupname)

        ngroup = Group(datatype=ogroup.datatype,
                       energy=1.0*ogroup.energy,
                       mu=1.0*ogroup.mu,
                       xdat=1.0*ogroup.energy,
                       ydat=1.0*ogroup.mu)

        for attr in dir(ogroup):
            if attr in ('i0', 'data' 'yerr'):
                val = getattr(ogroup, attr)*1.0
            if attr in ('norm', 'flat', 'deriv', 'deconv', 'post_edge', 'pre_edge'):
                pass
            else:
                try:
                    val = copy.deepcopy(getattr(ogroup, attr))
                except ValueError:
                    val = None
            setattr(ngroup, attr, val)

        new_fname = unique_name(fname,     self.controller.file_groups.keys())
        new_gname = unique_name(groupname, self.controller.file_groups.values())
        setattr(self.larch.symtable, new_gname, ngroup)
        self.install_group(new_gname, new_fname, overwrite=False)
        self.nb_panels[0].process(ngroup)
        self.ShowFile(groupname=new_gname)
Exemple #35
0
def json_decode(value, _larch=None):
    """
    return json decoded object from larch symbol table
    for Parameter decoding, a non-None larch instance
    must be passed in
    """
    out = None
    if isinstance(value, basestring):
        try:
            value = json.loads(value)
        except ValueError:
            return value

    if isinstance(value, dict):
        classname = value.get('__class__', None)
        if classname == 'Array':
            out = np.fromiter(value['value'], dtype=value['__dtype__'])
        elif classname == 'Group':
            out = Group()
            value.pop('__class__')
            for key, val in value.items():
                print('Key ', key, val)
                setattr(out, key, json_decode(val))
        elif classname == 'Parameter':
            args = {'_larch': _larch}
            for attr in ('value', 'name', 'vary', 'min', 'max', 'expr'):
                val = value.get(attr, None)
                if val is not None:
                    args[attr] = val

            out = Parameter(**args)
    else:
        out = value
    return out
Exemple #36
0
    def __init__(self, parent=None, _larch=None, **kws):
        wx.Frame.__init__(self, parent, -1, size=XASVIEW_SIZE, style=FRAMESTYLE)

        self.last_array_sel = {}
        self.paths2read = []

        title = "Larch XAS GUI: XAS Visualization and Analysis"

        self.larch_buffer = parent
        if not isinstance(parent, LarchFrame):
            self.larch_buffer = LarchFrame(_larch=_larch, is_standalone=False)

        self.larch_buffer.Show()
        self.larch_buffer.Raise()
        self.larch = self.larch_buffer.larchshell
        self.larch.symtable._sys.xas_viewer = Group()

        self.controller = XASController(wxparent=self, _larch=self.larch)
        self.current_filename = None
        self.subframes = {}
        self.plotframe = None
        self.SetTitle(title)
        self.SetSize(XASVIEW_SIZE)

        self.SetFont(Font(FONTSIZE))
        self.larch_buffer.Hide()
        self.createMainPanel()
        self.createMenus()
        self.statusbar = self.CreateStatusBar(2, style=wx.STB_DEFAULT_STYLE)
        self.statusbar.SetStatusWidths([-3, -1])
        statusbar_fields = [" ", "initializing...."]
        for i in range(len(statusbar_fields)):
            self.statusbar.SetStatusText(statusbar_fields[i], i)
Exemple #37
0
    def __init__(self, data2D=None, xpixels=2048, ypixels=2048,
                 data1D=None, nwedge=2, nchan=5001, 
                 name='xrd',**kws):

        self.name    = name
        self.xpix    = xpixels
        self.ypix    = ypixels
        self.data2D  = data2D
        self.nwedge  = nwedge
        self.nchan   = nchan
        self.data1D  = data1D
        
        ## Also include calibration data file?
        ## mkak 2016.08.20

        Group.__init__(self)
Exemple #38
0
def read_csv(filename):
    """read CSV file, return group with data as columns"""
    csvfile = open(filename, 'r')
    dialect = csv.Sniffer().sniff(csvfile.read(),  [',',';', '\t'])
    csvfile.seek(0)

    data = None
    isfloat = None
    for row in csv.reader(csvfile, dialect):
        if data is None:
            ncols = len(row)
            data = [[] for i in range(ncols)]
            isfloat =[None]*ncols
        for i, word in enumerate(row):
            data[i].append(str2float(word))
            if isfloat[i] is None:
                try:
                    _ = float(word)
                    isfloat[i] = True
                except ValueError:
                    isfloat[i] = False

    out = Group(filename=filename, data=data)
    for icol in range(ncols):
        cname = 'col_%2.2d' % (icol+1)
        val = data[icol]
        if isfloat[icol]:
            val = np.array(val)
        setattr(out, cname, val)

    return out
Exemple #39
0
def _larch_init(_larch):
    """initialize xrf"""
    from ..symboltable import Group
    _larch.symtable._sys.display = Group(use_color=True,
                                         colors=dict(text={'color': 'black'},
                                                     text2={'color': 'blue'},
                                                     error={'color': 'red'}))
Exemple #40
0
def _ff2chi(pathlist,
            group=None,
            paramgroup=None,
            _larch=None,
            k=None,
            kmax=None,
            kstep=0.05,
            **kws):
    """sum chi(k) for a list of FeffPath Groups.

    Parameters:
    ------------
      pathlist:    a list of FeffPath Groups
      paramgroup:  a Parameter Group for calculating Path Parameters [None]
      kmax:        maximum k value for chi calculation [20].
      kstep:       step in k value for chi calculation [0.05].
      k:           explicit array of k values to calculate chi.
    Returns:
    ---------
       group contain arrays for k and chi

    This essentially calls path2chi() for each of the paths in the
    pathlist and writes the resulting arrays to group.k and group.chi.

    """
    msg = _larch.writer.write
    if (paramgroup is not None and _larch is not None
            and _larch.symtable.isgroup(paramgroup)):
        _larch.symtable._sys.paramGroup = paramgroup
    for path in pathlist:
        if not isNamedClass(path, FeffPathGroup):
            msg('%s is not a valid Feff Path' % path)
            return
        path._calc_chi(k=k, kstep=kstep, kmax=kmax)
    k = pathlist[0].k[:]
    out = np.zeros_like(k)
    for path in pathlist:
        out += path.chi

    if group is None:
        group = Group()
    else:
        group = set_xafsGroup(group, _larch=_larch)
    group.k = k
    group.chi = out
    return group
Exemple #41
0
 def __init__(self,
              name=None,
              shape='gaussian',
              amplitude=1,
              center=0,
              sigma=1,
              sigma_params=None,
              **kws):
     kwargs = {'name': name}
     kwargs.update(kws)
     self.amplitude = amplitude
     self.center = center
     self.sigma = sigma
     Group.__init__(self)
     self.name = name
     if name is not None:
         self._define(name, shape=shape, sigma_params=sigma_params)
Exemple #42
0
def merge_groups(grouplist,
                 master=None,
                 xarray='energy',
                 yarray='mu',
                 kind='cubic',
                 trim=True,
                 calc_yerr=True,
                 _larch=None):
    """merge arrays from a list of groups.

    Arguments
    ---------
     grouplist   list of groups to merge
     master      group to use for common x arrary [None -> 1st group]
     xarray      name of x-array for merge ['energy']
     yarray      name of y-array for merge ['mu']
     kind        interpolation kind ['cubic']
     trim        whether to trim to the shortest energy range [True]
     calc_yerr   whether to use the variance in the input as yerr [True]

    Returns
    --------
     group with x-array and y-array containing merged data.

    """
    if master is None:
        master = grouplist[0]

    xout = getattr(master, xarray)
    xmins = [min(xout)]
    xmaxs = [max(xout)]
    yvals = []

    for g in grouplist:
        x = getattr(g, xarray)
        y = getattr(g, yarray)
        yvals.append(interp(x, y, xout, kind=kind))
        xmins.append(min(x))
        xmaxs.append(max(x))

    yvals = np.array(yvals)
    yave = yvals.mean(axis=0)
    ystd = yvals.std(axis=0)

    if trim:
        xmin = min(xmins)
        xmax = min(xmaxs)
        ixmin = index_of(xout, xmin)
        ixmax = index_of(xout, xmax)
        xout = xout[ixmin:ixmax]
        yave = yave[ixmin:ixmax]
        ystd = ystd[ixmin:ixmax]

    grp = Group()
    setattr(grp, xarray, xout)
    setattr(grp, yarray, yave)
    setattr(grp, yarray + '_std', ystd)
    return grp
Exemple #43
0
 def __init__(self,
              filename='spykscan.001',
              configfile=None,
              auto_increment=True,
              _larch=None,
              **kwargs):
     Group.__init__(self, **kwargs)
     self.motors = {}
     self.detectors = []
     self.bare_counters = []
     self._scan = LarchStepScan(filename=filename,
                                auto_increment=auto_increment,
                                _larch=_larch)
     self.datafilename = filename
     if configfile is not None:
         self.configfile = configfile
     self.read_config(filename=configfile)
     self.lup = self.dscan
Exemple #44
0
def copy_group(group, _larch=None):
    from larch import Group

    out = Group(datatype=getattr(group, 'datatype', 'unknown'),
                copied_from=getattr(group, 'groupname', repr(group)))

    for attr in dir(group):
        setattr(out, attr, copy.deepcopy(getattr(group, attr)))
    return out
 def fetch(self, uid, name=None, mode='transmission'):
     self.uid = uid
     if name is not None:
         self.name = name
     else:
         self.name = uid[-6:]
     self.group = Group(__name__=self.name)
     self.make_xmu(uid, mode=mode)
     self.prep()
Exemple #46
0
    def __init__(self, data=None, pathlist=None, transform=None,
                 epsilon_k=None, _larch=None, **kws):
        self._larch = _larch
        Group.__init__(self, **kws)

        self.pathlist = pathlist

        self.data = data
        if transform is None:
            transform = TransformGroup()
        self.transform = transform
        if epsilon_k is not None:
            self.data.epsilon_k = epsilon_k

        self.model = Group()
        self.model.k = None
        self.__chi = None
        self.__prepared = False
Exemple #47
0
 def __init__(self, left=0, right=0, name='', bgr_width=3, counts=None,
              address=''):
     """
     Parameters:
     -----------
     * left      Left limit in index/channels numbers
     * right     Right limit in index/channels numbers
     * name     Name of the ROI
     * bgr_width Number of channels to use for background subtraction
     """
     self.name     = name
     self.address  = address
     self.bgr_width = int(bgr_width)
     self.total  = 0
     self.net    = 0
     self.set_bounds(left, right)
     if counts is not None:
         self.get_counts(counts)
     Group.__init__(self)
Exemple #48
0
def read_xdi(filename, labels=None, _larch=None):
    """simple mapping of XDI file to larch groups"""
    xdif = XDIFile(filename, labels=labels)
    group = Group()
    for key, val in xdif.__dict__.items():
        if not key.startswith('_'):
            if six.PY3 and key in string_attrs:
                val = tostr(val)
            setattr(group, key, val)
    group.__name__ ='XDI file %s' % filename
    doc = ['%i arrays, %i npts' % (xdif.narrays, xdif.npts)]
    arr_labels = getattr(xdif, 'array_labels', None)
    if arr_labels is not None:
        doc.append("Array Labels: %s" % repr(arr_labels))
    group.__doc__ = '\n'.join(doc)

    group.path = filename
    path, fname = os.path.split(filename)
    group.filename = fname
    return group
Exemple #49
0
def _ff2chi(pathlist, group=None, paramgroup=None, _larch=None,
            k=None, kmax=None, kstep=0.05, **kws):
    """sum chi(k) for a list of FeffPath Groups.

    Parameters:
    ------------
      pathlist:    a list of FeffPath Groups
      paramgroup:  a Parameter Group for calculating Path Parameters [None]
      kmax:        maximum k value for chi calculation [20].
      kstep:       step in k value for chi calculation [0.05].
      k:           explicit array of k values to calculate chi.
    Returns:
    ---------
       group contain arrays for k and chi

    This essentially calls path2chi() for each of the paths in the
    pathlist and writes the resulting arrays to group.k and group.chi.

    """
    msg = _larch.writer.write
    if (paramgroup is not None and _larch is not None and
         _larch.symtable.isgroup(paramgroup)):
        _larch.symtable._sys.paramGroup = paramgroup
    for path in pathlist:
        if not isNamedClass(path, FeffPathGroup):
            msg('%s is not a valid Feff Path' % path)
            return
        path._calc_chi(k=k, kstep=kstep, kmax=kmax)
    k = pathlist[0].k[:]
    out = np.zeros_like(k)
    for path in pathlist:
        out += path.chi

    if group is None:
        group = Group()
    else:
        group = set_xafsGroup(group, _larch=_larch)
    group.k = k
    group.chi = out
    return group
Exemple #50
0
    def __init__(self, folder=None, _larch=None, **kws):
        kwargs = dict(name='Feff85exafs unit test: %s' % folder)
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self._larch     = Interpreter()
        self.doplot     = True
        self.doscf      = False # True = use self-consistency
        self.verbose    = True  # True = print Feff's screen messages and other screen messages
        self.feffran    = False # True = Feff calculation has been run
        self.count      = 0
        self.feffcount  = 0
        self.datacount  = 0
        self.failed     = list()
        if folder[-1] == '/': folder = folder[:-1] # strip trailing /
        self.folder     = folder

        if not isdir(folder):
            folder = join('tests', folder)
        if not isdir(folder):
            print_error(folder + " isn't one of the available tests")
            return None

        self.path       = realpath(folder)
        self.testrun    = realpath(join(self.path, 'testrun'))
        self.fefflog    = realpath(join(self.path, 'testrun', 'feff8l.log'))
        self.__testpaths()
        self.repotop    = getcwd()
        if not self.repotop.endswith('feff85exafs'):  self.repotop = realpath(join('..'))
        # the f85e shell script emulates the behavior of the monolithic Feff application
        self.eps5       = 0.00001
        self.eps4       = 0.0001
        self.eps3       = 0.001
        self.epsilon    = self.eps4
        self.epsfit     = self.eps3
        self.epserr     = 5.0 * self.epsfit
        self.firstshell = False
        self.fittest    = None
        if WRAPPER_AVAILABLE:
            self.sp = Feff8L_XAFSPath(_larch=self._larch)
Exemple #51
0
    def __init__(self, counts=None, nchans=2048, start_time='',
                 offset=0, slope=0, quad=0, name='mca', dt_factor=1,
                 real_time=0, live_time = 0, input_counts=0, tau=0, **kws):

        self.name    = name
        self.nchans  = nchans
        self.environ = []
        self.rois    = []
        self.counts  = counts

        # Calibration parameters
        self.offset       = offset # Offset
        self.slope        = slope  # Slope
        self.quad         = quad   # Quadratic

        # Counting parameters
        self.start_time   = start_time
        self.real_time    = real_time  # Elapsed real time in seconds (requested counting time)
        self.live_time    = live_time  # Elapsed live time in seconds (time detector is live)
        self.input_counts = input_counts # Actual total input counts (eg given by detector software)
                                  # Note total_counts and input counts ARE NOT time normalized
                                  #
        self.total_counts = 0.    # Total counts between the preset start and stop channels
        self.tau          = tau   # Factor for deadtime/detector saturation calculations, ie
                                  #     ocr = icr * exp(-icr*tau)
        # Calculated correction values
        self.icr_calc    = -1.0  # Calculated input count rate from above expression
        # corrected_counts = counts * dt_factor
        self.bgr = None
        self.dt_factor   = float(dt_factor)
        if counts is not None:
            self.nchans      = len(counts)
            self.total_counts =  counts.sum()
        self.get_energy()
        self._calc_correction()
        Group.__init__(self)
Exemple #52
0
    def __init__(self, _larch=None, **kws):
        kwargs = dict(name='Feff test framework')
        kwargs.update(kws)
        Group.__init__(self,  **kwargs)
        self._larch       = Interpreter()
        self.materials    = ("Copper", "NiO", "FeS2", "UO2", "BaZrO3", "bromoadamantane", "uranyl")
        self.tests        = ('scf', 'iorder', 'mpse')
        self.__material   = None
        self.__test       = None
        self.testmodule   = None

        self.json         = None
        self.mustache     = None
        self.dryrun       = False
        self.dopathfinder = False
        self.tableformat  = 'pipe' # 'plain', 'simple', 'grid', 'fancy_grid', 'pipe', 'orgtbl'
                                   # 'rst', 'mediawiki', 'html', 'latex', 'latex_booktabs'

        ## some things to make the cohabitation with f85ut happy
        self.doplot       = False
        self.verbose      = False
        self.firstshell   = False
        self.folder       = None
        self.path         = None
Exemple #53
0
def parse_perlathena(text, filename):
    """
    parse old athena file format to Group of Groups
    """
    lines = text.split('\n')
    athenagroups = []
    raw = {'name':''}
    vline = lines.pop(0)
    if  "Athena project file -- Demeter version" not in vline:
        raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename))

    major, minor, fix = '0', '0', '0'
    try:
        vs = vline.split("Athena project file -- Demeter version")[1]
        major, minor, fix = vs.split('.')
    except:
        raise ValueError("%s '%s': cannot read version" % (ERR_MSG, filename))
    if int(minor) < 9 or int(fix[:2]) < 21:
        raise ValueError("%s '%s': file is too old to read" % (ERR_MSG, filename))

    header = [vline]
    journal = {}
    is_header = True
    for t in lines:
        if t.startswith('#') or len(t) < 2 or 'undef' in t:
            if is_header:
                header.append(t)
            continue
        is_header = False
        key = t.split(' ')[0].strip()
        key = key.replace('$', '').replace('@', '')
        if key == 'old_group':
            raw['name'] = plarray2json(t)
        elif key == '[record]':
            athenagroups.append(raw)
            raw = {'name':''}
        elif key == 'journal':
            journal = plarray2json(t)
        elif key == 'args':
            raw['args'] = plarray2json(t)
        elif key == 'xdi':
            raw['xdi'] = t
        elif key in ('x', 'y', 'i0', 'signal', 'stddev'):
            raw[key] = np.array([float(x) for x in plarray2json(t)])
        elif key == '1;': # end of list
            pass
        else:
            print(" do not know what to do with key ", key, raw['name'])

    out = Group()
    out.__doc__ = """XAFS Data from Athena Project File %s""" % (filename)
    out.journal = journal
    out.group_names = []
    out.header = '\n'.join(header)
    for dat in athenagroups:
        label = dat.get('name', 'unknown')
        this = Group(athena_id=label, energy=dat['x'], mu=dat['y'],
                     bkg_params=Group(), fft_params=Group(),
                     athena_params=Group())
        if 'i0' in dat:
            this.i0 = dat['i0']
        if 'signal' in dat:
            this.signal = dat['signal']
        if 'stddev' in dat:
            this.stddev = dat['stddev']
        if 'args' in dat:
            for i in range(len(dat['args'])//2):
                key = dat['args'][2*i]
                val = dat['args'][2*i+1]
                if key.startswith('bkg_'):
                    setattr(this.bkg_params, key[4:], asfloat(val))
                elif key.startswith('fft_'):
                    setattr(this.fft_params, key[4:], asfloat(val))
                elif key == 'label':
                    label = this.label = val
                elif key in ('valence', 'lasso_yvalue',
                             'epsk', 'epsr', 'importance'):
                    setattr(this, key, asfloat(val))
                elif key in ('atsym', 'edge', 'provenance'):
                    setattr(this, key, val)
                else:
                    setattr(this.athena_params, key, asfloat(val))
        this.__doc__ = """Athena Group Name %s (key='%s')""" % (label, dat['name'])
        name = fix_varname(label)
        if name.startswith('_'):
            name = 'd' + name
        setattr(out, name, this)
        out.group_names.append(name)

    return out
Exemple #54
0
def initializeLarchPlugin(_larch=None):
    """initialize _scan"""
    if not _larch.symtable.has_group(MODNAME):
        g = Group()
        g.__doc__ = MODDOC
        _larch.symtable.set_symbol(MODNAME, g)
Exemple #55
0
def do_fit(self, which, firstshell=False, fittest='baseline'):

    if which == 'testrun':
        folder = self.testrun
    elif which == 'baseline':
        folder = self.baseline
    else:
        folder = realpath(join(self.folder, fittest, which))
    #endif

    data = read_xdi(join(self.path, 'NiO.chik'), _larch=self._larch)
    if hasattr(data, 'wavenumber'):
        data.k = data.wavenumber

    gds = Group(amp    = Parameter(1,      vary=True, _larch=self._larch),
                enot   = Parameter(1e-7,   vary=True, _larch=self._larch),
                sso    = Parameter(0.003,  vary=True, _larch=self._larch), _larch=self._larch  )

    if firstshell:
        gds.delr   = Parameter(1e-7,   vary=True, _larch=self._larch)
        dr1param   = 'delr'
    else:
        gds.alpha  = Parameter(1e-7,   vary=True, _larch=self._larch)
        gds.ssni   = Parameter(0.003,  vary=True, _larch=self._larch)
        gds.sso2   = Parameter(0.003,  vary=True, _larch=self._larch)
        #gds.sso3   = Parameter(0.003,  vary=True, _larch=self._larch)
        gds.ssni2  = Parameter(0.003,  vary=True, _larch=self._larch)
        #gds.ssni3  = Parameter(0.003,  vary=True, _larch=self._larch)
        #gds.ssni4  = Parameter(0.003,  vary=True, _larch=self._larch)
        dr1param   = 'alpha*reff'

    paths = list() 
    paths.append(feffpath(realpath(join(folder, "feff0001.dat")), # 1st shell O SS
                          s02    = 'amp',
                          e0     = 'enot',
                          sigma2 = 'sso',
                          deltar = dr1param, _larch=self._larch))
    if not firstshell:
        paths.append(feffpath(realpath(join(folder, "feff0002.dat")), # 2nd shell Ni SS
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'ssni',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0003.dat")), # O-O triangle
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = '1.5*sso',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0004.dat")), # O-Ni triangle
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'sso+ssni/2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0005.dat")), # 3rd shell O SS
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'sso2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0006.dat")), # 4th shell Ni SS
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'ssni2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0007.dat")), # O-O non-forward linear
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'sso*2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0008.dat")), # O-Ni forward scattering
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'ssni2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0009.dat")), # O-O forward through absorber
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'sso*2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0011.dat")), # O-Ni-O double forward
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'ssni2',
                              deltar = 'alpha*reff', _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0010.dat")), # O-O rattle (the order of 10 and 11 is different in Demeter's pathfinder!)
                              s02    = 'amp',
                              e0     = 'enot',
                              sigma2 = 'sso*4',
                              deltar = 'alpha*reff', _larch=self._larch))

    rx  = 4.2
    if firstshell: rx  = 1.95

    trans = feffit_transform(kmin=3, kmax=15.938, kw=(2,1,3), dk=1, window='hanning', rmin=1.0, rmax=rx, _larch=self._larch)
    dset  = feffit_dataset(data=data, pathlist=paths, transform=trans, _larch=self._larch)
    fit   = feffit(gds, dset, _larch=self._larch)

    if self.doplot:
        offset = 0.6*max(dset.data.chir_mag)
        _newplot(dset.data.r,  dset.data.chir_mag+offset, xmax=8,
              xlabel=r'$R \rm\,(\AA)$', label='data',
              ylabel=r'$|\chi(R)| \rm\,(\AA^{-3})$',
              title='Fit to '+self.folder, show_legend=True, _larch=self._larch)
        _plot(dset.model.r, dset.model.chir_mag+offset, label='fit', _larch=self._larch)
        _plot(dset.data.r,  dset.data.chir_re, label='data', _larch=self._larch)
        _plot(dset.model.r, dset.model.chir_re, label='fit', _larch=self._larch)
    #end if
    
    if self.verbose:
        print feffit_report(fit, _larch=self._larch)
    #end if

    shells = ''
    if firstshell: shells='_1st'

    write_ascii(join(self.folder, fittest, "fit_"+which+shells+".k"), dset.data.k, dset.data.chi, dset.model.chi,
                labels="r data_mag fit_mag data_re fit_re", _larch=self._larch)
    write_ascii(join(self.folder, fittest, "fit_"+which+shells+".r"), dset.data.r, dset.data.chir_mag, dset.model.chir_mag,
                dset.data.chir_re, dset.model.chir_re, labels="r data_mag fit_mag data_re fit_re", _larch=self._larch)

    renderer = pystache.Renderer()
    with open(join(self.folder, fittest,'fit_'+which+shells+'.gp'), 'w') as inp:
        inp.write(renderer.render_path( 'plot.mustache', # gnuplot mustache file
                                        {'material': 'NiO',
                                         'model': which,
                                         'fittest': fittest,
                                         'shells': shells,
                                         'kmin': 3,
                                         'kmax': 15.938,
                                         'rmin': 1.0,
                                         'rmax': rx,
                                         'offset': 1,
                                     } ))

    return fit
Exemple #56
0
def do_fit(self, which, firstshell=False, fittest='baseline'):

    if which == 'testrun':
        folder = self.testrun
    elif which == 'baseline':
        folder = self.baseline
    else:
        folder = realpath(join(self.folder, fittest, which))
    #endif

    data = read_xdi(join(self.path, 'BaZrO3.chik'), _larch=self._larch)

    gds = Group(amp    = Parameter(0.95,    vary=True,  _larch=self._larch),
                enot   = Parameter(1e-7,    vary=True,  _larch=self._larch),
                sso    = Parameter(0.003,   vary=True,  _larch=self._larch),
                czr    = Parameter(0.,      vary=False, _larch=self._larch), _larch=self._larch  )

    if firstshell:
        gds.delr   = Parameter(1e-7,    vary=True,  _larch=self._larch)
        dr1param   = 'delr'
    else:
        gds.alpha  = Parameter(0.00001, vary=True,  _larch=self._larch)
        gds.ssba   = Parameter(0.003,   vary=True,  _larch=self._larch)
        gds.sszr   = Parameter(0.003,   vary=True,  _larch=self._larch)
        gds.eba    = Parameter(1e-7,    vary=True,  _larch=self._larch)
        gds.ezr    = Parameter(1e-7,    vary=True,  _larch=self._larch)
        #gds.eba    = Parameter(expr='enot',          _larch=self._larch)
        #gdsezr    = Parameter(expr='enot',         _larch=self._larch)
        gds.sso2   = Parameter(0.003,   vary=True,  _larch=self._larch)
        dr1param   = 'alpha*reff'

    paths = list()
    paths.append(feffpath(realpath(join(folder, "feff0001.dat")),
                          s02    = 'amp',
                          deltar = dr1param,
                          e0     = 'enot',
                          sigma2 = 'sso',
                          _larch=self._larch))
    if not firstshell:
        paths.append(feffpath(realpath(join(folder, "feff0002.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso*1.5',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0003.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'eba',
                              sigma2 = 'ssba',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0004.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'ezr',
                              sigma2 = 'sszr',
                              third  = 'czr',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0005.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso*2',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0006.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = '(enot+ezr)/2',
                              sigma2 = 'sszr',
                              third  = 'czr',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0007.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso*2',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0009.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = '(2*enot+ezr)/3',
                              sigma2 = 'sszr',
                              third  = 'czr',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0008.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso*4',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0011.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = '(enot+eba)/2',
                              sigma2 = 'ssba+sso',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0012.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso2',
                              _larch=self._larch))
        paths.append(feffpath(realpath(join(folder, "feff0013.dat")),
                              s02    = 'amp',
                              deltar = 'alpha*reff',
                              e0     = 'enot',
                              sigma2 = 'sso+sso2',
                              _larch=self._larch))

    rx  = 4.5
    if firstshell: rx  = 1.95

    trans = feffit_transform(kmin=3, kmax=14.5, kw=(2,1,3), dk=1, window='hanning', rmin=1.2, rmax=rx, _larch=self._larch)
    dset  = feffit_dataset(data=data, pathlist=paths, transform=trans, _larch=self._larch)
    fit   = feffit(gds, dset, _larch=self._larch)

    if self.doplot:
        offset = max(dset.data.chir_mag)
        _newplot(dset.data.r,  dset.data.chir_mag+offset, xmax=8,
              xlabel=r'$R \rm\,(\AA)$', label='data',
              ylabel=r'$|\chi(R)| \rm\,(\AA^{-3})$',
              title='Fit to '+self.folder, show_legend=True, _larch=self._larch)
        _plot(dset.model.r, dset.model.chir_mag+offset, label='fit', _larch=self._larch)
        _plot(dset.data.r,  dset.data.chir_re, label='data', _larch=self._larch)
        _plot(dset.model.r, dset.model.chir_re, label='fit', _larch=self._larch)
    #end if
    
    if self.verbose:
        print feffit_report(fit, _larch=self._larch)
    #end if

    shells = ''
    if firstshell: shells='_1st'

    write_ascii(join(self.folder, fittest, "fit_"+which+shells+".k"), dset.data.k, dset.data.chi, dset.model.chi,
                labels="r data_mag fit_mag data_re fit_re", _larch=self._larch)
    write_ascii(join(self.folder, fittest, "fit_"+which+shells+".r"), dset.data.r, dset.data.chir_mag, dset.model.chir_mag,
                dset.data.chir_re, dset.model.chir_re, labels="r data_mag fit_mag data_re fit_re", _larch=self._larch)

    renderer = pystache.Renderer()
    with open(join(self.folder, fittest, 'fit_'+which+shells+'.gp'), 'w') as inp:
        inp.write(renderer.render_path( 'plot.mustache', # gnuplot mustache file
                                        {'material': 'BaZrO3',
                                         'model': which,
                                         'fittest': fittest,
                                         'shells': shells,
                                         'kmin': 3,
                                         'kmax': 14.5,
                                         'rmin': 1.2,
                                         'rmax': rx,
                                         'offset': 1,
                                     } ))

    return fit
Exemple #57
0
def read_athena(filename, match=None, do_preedge=True, do_bkg=True, do_fft=True, use_hashkey=False, _larch=None):
    """read athena project file
    returns a Group of Groups, one for each Athena Group in the project file

    Arguments:
        filename (string): name of Athena Project file
        match (sring): pattern to use to limit imported groups (see Note 1)
        do_preedge (bool): whether to do pre-edge subtraction [True]
        do_bkg (bool): whether to do XAFS background subtraction [True]
        do_fft (bool): whether to do XAFS Fast Fourier transform [True]
        use_hashkey (bool): whether to use Athena's hash key as the
                       group name instead of the Athena label [False]

    Returns:
        group of groups each named according the label used by Athena.

    Notes:
        1. To limit the imported groups, use the pattern in `match`,
           using '*' to match 'all' '?' to match any single character,
           or [sequence] to match any of a sequence of letters.  The match
           will always be insensitive to case.
        3. do_preedge,  do_bkg, and do_fft will attempt to reproduce the
           pre-edge, background subtraction, and FFT from Athena by using
           the parameters saved in the project file.
        2. use_hashkey=True will name groups from the internal 5 character
           string used by Athena, instead of the group label.

    Example:
        1. read in all groups from a project file:
           cr_data = read_athena('My Cr Project.prj')

        2. read in only the "merged" data from a Project, and don't do FFT:
           zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_fft=False)

    """

    from larch_plugins.xafs import pre_edge, autobk, xftf

    if not os.path.exists(filename):
        raise IOError("%s '%s': cannot find file" % (ERR_MSG, filename))

    try:
        fh = GzipFile(filename)
        lines = [bytes2str(t) for t in fh.readlines()]
        fh.close()
    except:
        raise ValueError("%s '%s': invalid gzip file" % (ERR_MSG, filename))

    athenagroups = []
    dat = {"name": ""}
    Athena_version = None
    vline = lines.pop(0)
    if "Athena project file -- Demeter version" not in vline:
        raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename))

    major, minor, fix = "0", "0", "0"
    try:
        vs = vline.split("Athena project file -- Demeter version")[1]
        major, minor, fix = vs.split(".")
    except:
        raise ValueError("%s '%s': cannot read version" % (ERR_MSG, filename))
    if int(minor) < 9 or int(fix[:2]) < 21:
        raise ValueError("%s '%s': file is too old to read" % (ERR_MSG, filename))

    for t in lines:
        if t.startswith("#") or len(t) < 2:
            continue
        key = t.split(" ")[0].strip()
        key = key.replace("$", "").replace("@", "")
        if key == "old_group":
            dat["name"] = perl2json(t)
        elif key == "[record]":
            athenagroups.append(dat)
            dat = {"name": ""}
        elif key == "args":
            dat["args"] = perl2json(t)
        elif key in ("x", "y", "i0"):
            dat[key] = np.array([float(x) for x in perl2json(t)])

    if match is not None:
        match = match.lower()

    out = Group()
    out.__doc__ = """XAFS Data from Athena Project File %s""" % (filename)
    for dat in athenagroups:
        label = dat["name"]
        this = Group(
            athena_id=label, energy=dat["x"], mu=dat["y"], bkg_params=Group(), fft_params=Group(), athena_params=Group()
        )
        if "i0" in dat:
            this.i0 = dat["i0"]
        if "args" in dat:
            for i in range(len(dat["args"]) // 2):
                key = dat["args"][2 * i]
                val = dat["args"][2 * i + 1]
                if key.startswith("bkg_"):
                    setattr(this.bkg_params, key[4:], val)
                elif key.startswith("fft_"):
                    setattr(this.fft_params, key[4:], val)
                elif key == "label":
                    this.label = val
                    if not use_hashkey:
                        label = this.label
                else:
                    setattr(this.athena_params, key, val)
        this.__doc__ = """Athena Group Name %s (key='%s')""" % (label, dat["name"])
        olabel = fix_varname(label)
        if match is not None:
            if not fnmatch(olabel.lower(), match):
                continue

        if do_preedge or do_bkg:
            pars = this.bkg_params
            pre_edge(
                this,
                _larch=_larch,
                e0=float(pars.e0),
                pre1=float(pars.pre1),
                pre2=float(pars.pre2),
                norm1=float(pars.nor1),
                norm2=float(pars.nor2),
                nnorm=float(pars.nnorm) - 1,
                make_flat=bool(pars.flatten),
            )

            if do_bkg and hasattr(pars, "rbkg"):
                autobk(
                    this,
                    _larch=_larch,
                    e0=float(pars.e0),
                    rbkg=float(pars.rbkg),
                    kmin=float(pars.spl1),
                    kmax=float(pars.spl2),
                    kweight=float(pars.kw),
                    dk=float(pars.dk),
                    clamp_lo=float(pars.clamp1),
                    clamp_hi=float(pars.clamp2),
                )

        if do_fft:
            pars = this.fft_params
            kweight = 2
            if hasattr(pars, "kw"):
                kweight = float(pars.kw)
            xftf(
                this,
                _larch=_larch,
                kmin=float(pars.kmin),
                kmax=float(pars.kmax),
                kweight=kweight,
                window=pars.kwindow,
                dk=float(pars.dk),
            )

        setattr(out, olabel, this)
    return out