Hi Matt,
On Nov 1, 2016, at 2:00 PM, Matt Newville
wrote: Hi Sean,
On Tue, Nov 1, 2016 at 1:39 PM, Sean Fackler
mailto:swfackler@lbl.gov> wrote: Dear Matt, I’ve been working to implement Larch functions as a Python library. During the implementation I found a number of things I needed to do including:
You can use Larch functions from Python, of course. Normally, you'd do something like
import larch from larch_plugins.xafs import pre_edge, mback
1. Identifiying a functions’ python callable name, i.e. _group as a python callable function instead of group as the larch function.
I'm not sure I understand. You shouldn't need "a function's python callable name", you should just need the function. Maybe I misunderstand.
If you need a larch Group, you should be able to do import larch agroup = larch.Group(name='mygroupname', xdata = range(100), label='hello')
Thanks for showing me the precise command.
2. The need to pass '_larch = mylarch’ in the said function or I get the 'AttributeError: 'NoneType' object has no attribute ‘symtable’ ‘
Yes, many of the larch plugin functions either require or are much happier with an instance of a larch session passed in as the '_larch' argument. It's kind of a feature ;).
3. The need to run the script through the Python console as oppose to running as a script.
I'm not sure I understand what this means. You should be able to run a python program from any environment.
You can see my sample script with data attached. Keep in mind I am a Python novice. So for point 3 in particular I don’t understand the difference between running some selected code in the Python console and running the whole script in the shell (I am using PyCharm CE). I checked my Python interpreter with sys.executable (path) and sys.version_info (version) which are the same between the console and interpreter which were initial issues I found online. Is the interpreter somehow not seeing the xraylarch library?
Hmm, that doesn't make a lot of sense to me either. What errors do you get when you run your script?
I don't use PyCharm, so maybe that's not seeing all of the modules from anaconda? Is PyCharm seeing your anaconda distribution?
When I run the script I get the following error from the same script “import_fit.py": "Traceback (most recent call last): File "/Users/SeanFackler/PycharmProjects/msp_sean/import_fit.py", line 10, in <module> from larch_plugins.lineshapes import gaussian ImportError: No module named larch_plugins.lineshapes Process finished with exit code 1” Somehow it can’t load the module lineshapes. But selecting all the code and running it in the Python console works fine. I checked as before and it appears to be seeing the same anaconda Python interpreter. Pycharm also appears to see the anaconda distribution.
A few comments on your script: a) there should be no need to do a os.chdir() to any folder, especially not to source code folders. b) doing from xxx import * is bound to lead to confusion. Import what you need, and don't import what you don't need.
Thanks for those helpful comments. The asterisks was an attempt to get plotter working on my system :)
Finally: 1. Is there an easier way to use the larch functions as a python library?
Is the above suggestion better?
2. Is a larch group basically a python dictionary?
Actually, it's basically an empty class:
class Group: def __init__(self, **kws): ....
mygroup = Group() mygroup.x = 1 mygroup.y = 'a string'
even in more detail, it's an empty class. In most cases it would probably be OK to substitute almost any class instance as a Group.
In Larch, I tend to make as many things as possible "group-like" so that accessing elements is easy.
Indeed I had no problem replacing larch groups as dictionaries so the group-like accessing of elements is more general than a dictionary I guess. That’s useful!
If so, this could be made clearer in the documentation. I am interested to contribute to the documentation if that’s of interest/help.
Sure! The docs are definitely in need of some serious work.
Let me know how best I could contribute. For starters I can help with typos and the like.
I am running MacOS Sierra 10.12.1 on a mid-2015 15" MacBook Pro with Python 2.7.
Ron and Alpha: Maybe you have some ideas to the above points?
Thanks everyone for any help.
Sean
The attached modified version of your script works for me from a Terminal (The fit's not very good, yet, but it did run). On Mac OSX with anaconda, you probably have to run this as "pythonw import_fit.py" (note: pythonw, not python).
Hope that helps,
That works perfectly! I just ran it in the Interpreter normally (no pythonw). I guess it’s just the way I was importing things. Thank you, Sean
--Matt
_______________________________________________ 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