Calling Python Code from Igor Pro

It would be very useful to be able to call python code from within Igor Pro, storing the results into variables, waves, or pre-defined structures. Matlab has such functionality, but I’ve only seen this mentioned with regard to Igor Pro in a brief discussion back in 2010.

Is this something others would benefit from? Any chance we might be able to get something like this in Igor Pro 9?

We've considered this, but there hasn't been a lot of demand for it and it would require a lot of work to implement.

"Matlab has such functionality" is true, but I believe that Mathworks also has thousands of employees. You can count the number of WM employees on two hands, even if you are missing some fingers :)

This may seem a bit like using a grenade to kill a fly, but couldn't one compile a python interpreter into an Igor Pro XOP using open source code from the latest python distribution?

Embedding python is what this 2010 topic was about: https://www.wavemetrics.com/forum/general/embedded-python

It would seem that it might be possible to just call a standard python distro via ExecuteScriptText using files for passing data back a forth. You would need to write some code on both the Igor and python side to deal with the data.

I would be helpful to know what kind of processing and data you need via python.

 

Be aware that, according to https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_release_notes?language=objc:

  • Scripting language runtimes such as Python, Ruby, and Perl are included in macOS for compatibility with legacy software. In future versions of macOS, scripting language runtimes won’t be available by default, and may require you to install an additional package. If your software depends on scripting languages, it’s recommended that you bundle the runtime within the app. (49764202)

  • Use of Python 2.7 isn’t recommended. This version is included in macOS for compatibility with legacy software. Future versions of macOS won’t include Python 2.7. Instead, it’s recommended that you run python3 in Terminal. (51097165)

I imagine that those folks on macOS who want Python are not wanting the macOS standard 2.7 Python libraries anyway. So, it is a good call from Apple to just dump this in favor of letting folks pip or homebrew their "third party" installations.

I would say anyone who is like really using python is using a custom installation. The one shipping with the OS/distro is usually outdated and incomplete.

In reply to by thomas_braun

thomas_braun wrote:

I would say anyone who is like really using python is using a custom installation. The one shipping with the OS/distro is usually outdated and incomplete.

 

Isn't that an argument for NOT linking in a standard install of python? 

Also, speaking as a python novice, it seem that python seems to make a special effort to break programs developed under previous versions. 

[My only experience with python was in translating a Neural Net tutorial into Igor. A useful project where I made some changes to Igor syntax and programming in order to match speed of learning the NIST handwriting examples.]

 

In reply to by Larry Hutchinson

Larry Hutchinson wrote:

Isn't that an argument for NOT linking in a standard install of python? 

Also, speaking as a python novice, it seem that python seems to make a special effort to break programs developed under previous versions. 

I would say that folks who currently use python *in a serious way* do not link to a standard installation, at least under macOS. And, perhaps being tongue-in-cheek or factious or cynical ... Python is totally free. You only need to invest your time to fix what was broken during the recent update.

Full Disclosure: I started about a year ago at python3 and I only dabble at the edges of it using JupyterLabs to make interactive demos. I have no dog in the fight as to whether python should or should be linked with Igor Pro.

I asked this before with no response so again: What kind of processing and data transfer do you want to do with Python?

I ask this because I am looking into using the pickle format to exchange data between Igor and Python and I'd like to know if the effort would be worthwhile.

The pickle format is not designed to be used by programs other than Python but a subset could be read and written using Igor code.

I've been able to read an array of images from a sample file (mnist.pkl from http://neuralnetworksanddeeplearning.com/chap1.html) but only after manually inspecting the output of pickletools.dis(). Making this automatic and general is the hard part.
 

@Larry: I'm not very fond of pickled data. What would you do with that in igor? I would say usually it is about array data and that is much easier exported from python via numpy.save, https://www.numpy.org/devdocs/reference/generated/numpy.lib.format.html. So support for reading/writing that is in my eyes more worthwhile than partial pickle support. Of course you could save in hdf5 from python and just load it into igor as well.

What I would like to see in IP is the ability to call python code from within Igor and exchange data with it. https://github.com/pybind/pybind11 is used by a project I know for writing python code on top of C++. Of course that is easier suggested than done.

I was just thinking the pickle format was easy for python users. The one file I have contains multiple numpy arrays.

Any reason you cant just call python using ExecuteScriptText with disk files to pass data?

 

If all you want to read numpy arrays from file then I wouldn't use pickles, the format spec is available: https://www.numpy.org/devdocs/reference/generated/numpy.lib.format.html.

Reading Pickle files would be a fragile way to go.

To call Python from C/C++: https://docs.python.org/3.7/extending/embedding.html

Otherwise it could be possible via ExecuteScriptText, but you have to know the correct path to the Python command.

Otherwise it could be done via sockets, by making a Python webapp.