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