Numpy/Scipy for IronPython


#1

I know compas.utilities/xfunc provides some numpy access for “fd” function, but is there a general way to use numpy/scipy functions in Rhino Ipython?


#2

sure, you can use XFunc for anything. just make sure to set the serializer to 'pickle' when using numerical data.

import compas

from compas.utilities import XFunc

solve = XFunc('scipy.linalg.solve', serializer='pickle')

a = [[3, 2, 0], [1, -1, 0], [0, 5, 1]]
b = [2, 4, -1]

x = solve(a, b)

print(x)

should return [ 2. -2. 9.]


#3

I get the following error at line x=solve(a,b), while running the code in Ipython for Rhino:

Message: unexpected EOF while unpickling

Also tried it for other functions with numpy:

import compas
from compas.utilities import XFunc

lis=[1,2,3]
solve = XFunc(‘numpy.array’, serializer=‘pickle’)
x=array(lis)

print(x)

gives error at line x=array(lis): Message: name ‘array’ is not defined


#4

just tried it in Rhino.
i was wrong about the serializer.
it should be set to 'json', which is default.

import compas

from compas.utilities import XFunc

array = XFunc('numpy.array', serializer='json')
solve = XFunc('scipy.linalg.solve', serializer='json')

a = [[3, 2, 0], [1, -1, 0], [0, 5, 1]]
b = [2, 4, -1]

x = solve(a, b)
a = array(a)

print(x)
print(a)

see the attached image for the result…


#5

by the way, it makes sense that your example doesn’t work, regardless of the type of serializer.
for it to work, do something like

import compas
from compas.utilities import XFunc

lis = [1, 2, 3]
array = XFunc('numpy.array')
x = array(lis)

print(x)

however, not sure what the point of that would be, because the array will be converted back to a list when the result is returned such that it can be used in Rhino…


#6

“numpy.array” was just an example, but I still get the same error for numpy functions as before: name ‘array’ is not defined or it could be any other function numpy.zeros: name ‘zeros’ is not defined

for the scipy one I get this error now:

Message: No JSON object could be decoded

line 384, in raw_decode, “C:\Program Files (x86)\IronPython 2.7\Lib\json\decoder.py”


#7

the “name ‘xxx’ is not defined” is because you have to change the name of your XFunc as i wrote in my previous answer. otherwise the name indeed does not exist.

array = Xfunc('...')
result = array(...)

and not

solve = Xfunc('...')
result = array(...)

as for the other error messages, it is a bit difficult to assess what is going wrong without more information. does XFunc work with fd_numpy of dr_numpy?


#8

running the following code from https://compas-dev.github.io/main/tutorial/xfuncs.html?highlight=numpy gives the same No JSON object could be decoded error.


#9

do you have a CPython version on your system with Numpy/Scipy installed?


#10

in other words, can you open a terminal running python and then import compas, numpy, and scipy?

$ python
>>> import compas
>>> import numpy
>>> import scipy

#11

Well, for my normal coding with python I use numpy and scipy everyday. For example, the same code runs in eclipse (see the picture), however for a long time I wanted to use some of the geometrical properties of rhinoscriptsyntax with some algebraic formulations in the same environment.


#12

but does it work in the terminal (command prompt)?

eclipse uses its own environment variables, so the fact that it works in eclipse is no guarantee that it would work outside of eclipse…


#13

It does not recognize compas:


#14

that means compas is not installed on your system.

since you used an anaconda prompt in your screenshot and it still didn’t work, i am going to assume you still have an older version of COMPAS which you installed by downloading the source folder and adding it to you PYTHONPATH in eclipse and to the Python Search Path in Rhino.

however, in that case your system still does not know about it. the PYTHONPATH in eclipse is limited to eclipse and has no effect outside of it.

to make COMPAS available system-wide, just add PYTHONPATH as an environment variable and make sure to point it to the COMPAS source folder.

Control Panel > System > Advanced System Settings > Environment Variables


#15

I added python path from Anaconda/pkgs to Environments and compas src folder was already added in Environment Variables, still could not import compas in Anaconda prompt.

I had to install compas with conda install COMPAS, in order to be able to import it in anaconda prompt. Now, I also added the compas folder from Anaconda/pkgs to the Environment Variables and to the Rhino Ipython path, but still get the json error in Rhino Ipython. As you can see in the previously posted picture from Ipython, it looks like that there is an error with the IronPython in Program Files (x86).


#16

bit difficult to offer advice at this point. seems like a configuration issue. perhaps you should stop by the office at some point. will save us a lot of time…


#17

Sure, can I come over today?


#18

yes, i am around until 4.30…


#19

I was able to solve the problem: there exist also Environment Variables for the current user (not admin). I cleaned that up as you did for the other one (removed extra paths and old compas paths, …). Now xFunc works with Ipython. :slight_smile:


#20

ha, happy to hear that it works!