Works like a charm! Thanks, Matt.

Thanks also for pointing out the larch feff6l function.. works very well too. I have some related question which I’ll raise separately.

Best wishes,
Imad


From: Matt Newville <newville@cars.uchicago.edu>
Reply: XAFS Analysis using Ifeffit <ifeffit@millenia.cars.aps.anl.gov>
Date: 17 April 2018 at 03:49:30
To: Imad Ahmed <imad.ahmed@earth.ox.ac.uk>
Cc: XAFS Analysis using Ifeffit <ifeffit@millenia.cars.aps.anl.gov>
Subject:  Re: [Ifeffit] Larch python instructions

Hi Imad,


On Mon, Apr 16, 2018 at 7:53 PM, Imad Ahmed <imad.ahmed@earth.ox.ac.uk> wrote:
Dear Matt,

Thank you so much for your feedback. I’ve tested both the python and Larch scripts and they work absolutely fine. Although, I am able to work with the python route, I do like the Larch way! 


Great! 

 
By the way, for a Larch script, how would you create plots in different windows? display = get_display(win=i); i = 1..n, following plot(), does not seem to work?


To plot in a new or different window, just add "win=N" to any of the plot_*() functions, like

     plot(group.energy, group.mu, win=1)

     plot(group.r, group.chir_mag, win=2)

The windows are numbered starting with 1.   For common XAFS plots, see  http://xraypy.github.io/xraylarch/xafs/utilities.html#plotting-macros-for-xafs   The usage in your earlier example could become

    plot_chik(path1, kweight=2, label = r'$\sigma^2 = 0$',  title=r'$\chi(k)$ from %s' % fname)

The `get_display(win=1)` function returns the PlotDisplay or wxPython Frame object.  The most useful attribute of that is `panel` and its `axes` which allows you to drill down to the matplotlib components.  I've tried to put *most* of the functionality you'd need from matplotlib available from the plot frame (especially from "configure plot") or from larch / wxmplot functionality, but matplotlib is pretty vast, and if you're writing python and know matplotlib you can get at it.   See, for example
    https://github.com/xraypy/xraylarch/blob/master/examples/plotting/nobox_histogram.lar

which uses `get_display().panel.axes` to be able to plot a histogram with matplotlib's `hist()` method.  It's meant for "advanced usage by python programmers", so you might be interested....

 

Out of interest how would you create the different paths from a cif file? e.g., similar to *.dat files in  ../xraylarch/examples/Feff_Cu. This does not seem possible using Atoms?


Atoms can turn CIF files into feff.inp files.  And then you can run feff6l or feff85l to turn that into the feffNNNN.dat files. If some part of this didn't work for you, please ask as a separate question.

Larch does use the very nice PyCifRW library but does not have full capability to convert CIF into XYZ coordinates for feff.inp.  I am not aware of any python package that does this, but there may very well be.  That functionality would be nice to add to Larch, but Atoms exists and does a fine job.

--Matt



Best wishes,
Imad

 


From: Matt Newville <newville@cars.uchicago.edu>
Reply: XAFS Analysis using Ifeffit <ifeffit@millenia.cars.aps.anl.gov>
Date: 16 April 2018 at 03:57:27
To: Imad Ahmed <imad.ahmed@earth.ox.ac.uk>, XAFS Analysis using Ifeffit <ifeffit@millenia.cars.aps.anl.gov>
Subject:  Re: [Ifeffit] Larch python instructions

Hi Imad,


On Sun, Apr 15, 2018 at 7:15 PM, Imad Ahmed <imad.ahmed@earth.ox.ac.uk> wrote:
Dear Matt,

I am looking for some proper instructions on how to use Larch from python. 
I have larch installed on Anaconda2 and trying to run the /examples/feffit/doc_feffdat1.lar script from python. 
I tried the python code below but still having issues to run it, especially with Feffpath and some larch plugins. Could you please help?

Best wishes, Imad


import sys, os
import numpy as np
from scipy.optimize import leastsq as scipy_leastsq
import larch
from larch import Group, Parameter
from larch_plugins.io import read_ascii
from larch_plugins.xafs import FeffPathGroup
from larch_plugins.xafs.feffit import (TransformGroup, FeffitDataSet,feffit, feffit_report)

fname = ~/xraylarch/examples/feffit/feff0001.dat'
path1 = FeffPathGroup(fname)
path2chi(path1)
show(path1)
newplot(path1.k, path1.chi*path1.k**2, xlabel=r' $ k \rm\, (\AA^{-1})$',
ylabel=r'$ k^2\chi(k)$', label = r'$\sigma^2 = 0$', show_legend=True,
title=r'$\chi(k)$ from %s' % fname)
path1.sigma2 = 0.002
path2chi(path1)
plot(path1.k, path1.chi*path1.k**2, label = r'$\sigma^2 = 0.002$')
 


Well, there are some differences between Larch and Python.

With some small modifications, your scaript can become a valid Larch script like this:
      # larch
from os.path import join, expanduser
fname
= join(expanduser(‘~'), '/xraylarch/examples/feffit/feff0001.dat')
path1 = feffpath(fname)

 path2chi(path1)
show(path1)
newplot(path1.k, path1.chi*path1.k**2, xlabel=r' $ k \rm\, (\AA^{-1})$',
ylabel=r'$ k^2\chi(k)$', label = r'$\sigma^2 = 0$', show_legend=True,
title=r'$\chi(k)$ from %s' % fname)
path1.sigma2 = 0.002
path2chi(path1)
plot(path1.k, path1.chi*path1.k**2, label = r'$\sigma^2 = 0.002$')
 

To do that from Python is a bit more involved.

Each of the functions "feffpath", "path2chi", "show", "newplot", and "plot"  need to be translated.  As you probably say, "feffpath" is effectively `FeffPathGroup'.   The others are less obvious, and many of the larch functions really want to be passed a "larch session" so that they can find other larch function or data sources.  For example, FeffPathGroup needs a larch session in order to be able to look up atomic information.

So in pure python that would be approximately:

    import os
    import os.path
    import matplotlib.pyplot as plt   # using pyplot instead of build in plot commmands

    from larch import Interpreter
    from larch_plugins.xafs.feffdat import FeffPathGroup, _path2chi

   # need to create a Larch interpreter for each "session"
   _larch = Interpreter()

    fname = os.path.join(os.path.expanduser("~"), "Codes/xraylarch/examples/feffit/feff0001.dat")
  
    # note that many of the Larch functions will want a `_larch` instance passed in.
    path1 = FeffPathGroup(fname, _larch=_larch)

   _path2chi(path1, _larch=_larch)

    # show, approximately
    for attr in dir(path1):
        print(attr, getattr(path1, attr))

    # plot, approximately
    plt.plot(path1.k, path1.chi*path1.k**2, label= r'$\sigma^2 = 0$')

    path1.sigma2 = 0.002
   _path2chi(path1, _larch=_larch)

    plt.plot(path1.k, path1.chi*path1.k**2, label = r'$\sigma^2 = 0.002$')

    plt.xlabel(r' $ k \rm\, (\AA^{-1})$')
    plt.ylabel(r'$ k^2\chi(k)$')
    plt.title(r'$\chi(k)$ from %s' % fname)

    plt.legend()
    plt.show()


But also, since you have to build a Larch Interpreter, you could simply use it to execute the previous Larch script:


    from larch import Interpreter

   text = '''
      # larch
from os.path import join, expanduser
fname
= join(expanduser(‘~'), '/xraylarch/examples/feffit/feff0001.dat')
path1 = feffpath(fname)

 path2chi(path1)
show(path1)
newplot(path1.k, path1.chi*path1.k**2, xlabel=r' $ k \rm\, (\AA^{-1})$',
ylabel=r'$ k^2\chi(k)$', label = r'$\sigma^2 = 0$', show_legend=True,
title=r'$\chi(k)$ from %s' % fname)
path1.sigma2 = 0.002
path2chi(path1)
plot(path1.k, path1.chi*path1.k**2, label = r'$\sigma^2 = 0.002$')
'''
    _larch = Interpreter()
    _larch.run(text)

The latter approach is highly recommended.

--Matt Newville
_______________________________________________
Ifeffit mailing list
Ifeffit@millenia.cars.aps.anl.gov
http://millenia.cars.aps.anl.gov/mailman/listinfo/ifeffit
Unsubscribe: http://millenia.cars.aps.anl.gov/mailman/options/ifeffit


_______________________________________________
Ifeffit mailing list
Ifeffit@millenia.cars.aps.anl.gov
http://millenia.cars.aps.anl.gov/mailman/listinfo/ifeffit
Unsubscribe: http://millenia.cars.aps.anl.gov/mailman/options/ifeffit