## New IronPython version and numpy/scipy

Discussions on extending SharpCap using the built in Python scripting functionality
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### New IronPython version and numpy/scipy

Hello Robin,

Do you see that a new version of IronPython is available ?
It is based on Python 3.4. OK, it in not 3.9, but maybe better than 2.7.

One point ... it is still not possible to use the numpy and scipy with the new IronPython.

I found that some people "convert" the numpy from C in C# and that it should be compatible with .NET.

Do you see this ? And Do you try it ? ... If yes, can you say me how to use it with a script in SharpCap ?

Best regards,
Jean-Francois
Posts: 6974
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: New IronPython version and numpy/scipy

Hi,

no, I hadn't spotted that, but I will investigate the new version. I will have to think carefully as updating to Python3 may breaks some scripts (it's a shame I didn't spot this before the relase of SC4.0).

I have never seen or tried any C# numpy/scipy libraries. If they are standard .NET then you should be able to use clr.AddReference() and then import then... Hopefully!

Robin
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### Re: New IronPython version and numpy/scipy

Hello Robin,

OK thanks, I will try with clr.AddReference() ... but with my experience of Python use ... I guess <10% chance of success, but I repeat several time so the chance of success will be improved

Concerning the IronPython 3.4 version ... one possibility is to have both. A setting in SharpCap allow the user to select the 2.x or 3.x version.
The PythonLib.zip file is small (5.5 MB) and if a PythonLib3.zip is produced ... maybe some MB more.

Regards,
Jean-Francois
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### Re: New IronPython version and numpy/scipy

Hello Robin,

I download from https://github.com/SciSharp/Numpy.NET the files and I generate with Visual Studio the Numpy.dll from the Numpy.NET.sln file.
I copy the Numpy.dll in the SharpCap directory and I test the following in the IronPython Console window:

import clr
or
import clr

both works without error ... so I continue with ...
import clr
import Numpy as np

... works without error ... Ah nice ... so the next step ...

import clr
import Numpy as np
A = np.array([[1, 2], [3, 4]])

... error ! AttributeError: 'Numpy' object has no attribute 'array'

Does somebody know what is missing ?

Regards,
Jean-Francois
Posts: 6974
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: New IronPython version and numpy/scipy

Hi,

could it be that the code has no idea what to do with the Python style array, which resolves to an IronPython.Runtime.List

Code: Select all

>>> [[1,2],[3,4]].GetType()
<System.RuntimeType object at 0x000000000000006C [IronPython.Runtime.List]>

For a 1 dimensional array you can do

Code: Select all

>>> from System import Array
>>> Array[int]([1,2])
Array[int]((1, 2))

- that will give you a .NET Array, which might make Numpy happier. Worth a try to see if it is the right idea.

Unfortunately multi-dimensional arrays look harder - see https://stackoverflow.com/questions/743 ... rp-library

cheers,

Robin
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### Re: New IronPython version and numpy/scipy

Hello Robin,

I wish to use some code from Python in IronPython-SharpCap. The code use a lot of numpy calculation ... I have no motivation to rewrite the full code in other code. I found another mathematics library Meta.Numerics http://www.meta-numerics.net/ ... it works with SharpCap/IronPython.
But the calculation/function/... are different ... and the same problem with rewriting all the code.
If it is a simple 1 to 1 change, then it could be done for small code. But sometimes, numpy automatic list/loop are not direct available in other libraries. In this case, it is not only to change the language, it is necessary to modify the algorithm.

In my example, the "array" was not working. I test several other functions of numpy. Nothing is working (yet).

I need to continue to search. Maybe that I will contact the Numpy.NET development team and describe my problems with IronPython.

Regards,
Jean-Francois
Posts: 6974
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: New IronPython version and numpy/scipy

Hi Jean-Francois,

I think that is going to be the biggest problem - finding something that doesn't just work, but works without the need to perform major edits on the code.

How about just running a real Python interpreter (as a separate process) from IronPython - That would put all your numpy/scipy in a proper python environment and you could either communicate just by running a python process with command line arguments and reading an output file or use some sort of more complicated remoting API (have a look at Pyro).

cheers,

Robin
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### Re: New IronPython version and numpy/scipy

Hello Robin,

To use a separate Python process from IronPython sounds good. I will search how to do this.
The actual script in Python open and process a SER file from the disk. I guess that in this way it will be simpler to use the Python code.

The "problem" is ... in the past I do too much test with different Python (IronLab, local IronPython, Python and with Visual Studio).
I need to be sure that I "use" the correct Python. Ideally the same as the installed in Visual Studio ... where all the numpy/scipy libraries are installed.

Regards,
Jean-Francois
Posts: 6974
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

### Re: New IronPython version and numpy/scipy

Hi Jean-Francois,

I guess the easiest way to ensure you get the right python is to use 'os.system' to run the exact python install that you want from inside SharpCap?

cheers,

Robin
Jean-Francois
Posts: 126
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

### Re: New IronPython version and numpy/scipy

Hello Robin,

Thank you for the simple solution

I modify the initial Python script (it has a window with several field, so now a new version starts the calculation with standard values).
And now, I can start from SharpCap (IronPython) the script that use numpy from the local Python.

I test "os.system()" and "subprocess.Popen()". Both work. I have no idea of the difference, if any, but I'm happy as long as one works.

In the next days, I will program in SharpCap different functions ... start/end of a film, control with the mount so that the mount scan and the film will be synchronized.

Regards,
Jean-Francois