forum.alglib.net
http://forum.alglib.net/

[updated] Python interface: using numpy arrays as input
http://forum.alglib.net/viewtopic.php?f=2&t=550
Page 1 of 1

Author:  mhavel [ Wed Mar 14, 2012 6:31 pm ]
Post subject:  [updated] Python interface: using numpy arrays as input

Hi,

I've recently downloaded ALGLIB with the Python interface. I was surprised to see that only list type was accepted, and not numpy arrays for the spline interpolation routines. I'm not sure if I got it right, but after having a look at the Python wrapper, I indeed see that, eg. the function "is_real_vector" explicitly expects a list type, and nothing else. According to the test I've made for Akima spline, having to change my numpy arrays into list for the routine (which in turn will anyway change the numbers into ctypes) is between 8 to 16% slower than providing list directly, so this may have a huge impact for people already dealing with numpy arrays in their code.
Does anyone have a good workaround, apart from modifying by hand the code?

Also I would suggest, if I may, to the author to just check for less specific types, and simply ask for objects that can be iterated over (which then would include numpy arrays). Also I would suggest that the check of any type in Python be done with the built-in function "isinstance(v, list)" rather than "type(v) == list".

Finally I'd like to say that the library looks really promising and good (I'm happy with the tests I've already ran). But as a Python user, I'd like to see numpy involved which would make much sense since ALGLIB is a library for mathematical routines.

Regards.

Author:  Sergey.Bochkanov [ Fri Mar 16, 2012 8:35 am ]
Post subject:  Re: Python interface: numpy arrays as input for akima spline

Hello!

Can you offer a solution which will work with numpy arrays, but won't fail when numpy is not installed? I think that dynamic nature of Python makes it easy to do, but my knowledge of Python is limited and I would like to hear something from experienced users. Small code snippet for 2-dimensional arrays should be enough.

If you help me, it can be included in the ALGLIB 3.6 :) (can't include in 3.5 because it is almost ready to shipping and in feature freeze mode now)

Author:  mhavel [ Mon Mar 26, 2012 5:38 pm ]
Post subject:  Re: Python interface: numpy arrays as input for akima spline

Hi Sergey,

Sorry for the late answer. I'm quite busy these days, but I'd be glad to help you with this task! I'll get back to you as soon as possible with a snippet for 2-D arrays.
Then ALGLIB would integrate better with Python scientific code :)

cheers

Author:  mhavel [ Sat Apr 14, 2012 11:42 pm ]
Post subject:  Re: Python interface: numpy arrays as input for akima spline

Hi Sergey,

As promised (but late), I modified your python wrapper (defining one new function "isiterable(obj)-> bool" and modifying some of your functions to be fully consistent (mainly all the is_XX).
These changes do not introduce any additional dependency on python module, but now the python wrapper will accept any numpy arrays if numpy is available on the system.

I think one could improve a lot the python wrapper (in term of coding, which in turn would most likely have a non-negligible impact on the performance), but I don't have the time to help you with this task, nor the required skills.

Anyway, let me know if the changes I've made are useful to you, and most of all if you plan to include them in a next release. I'd like to rely on ALGLIB for my own code, so it is mandatory for me to be sure I can work with numpy arrays, hence the need for this feature to be supported officially in next releases.

Regards

Attachments:
File comment: Modified python wrapper, now supporting Numpy arrays
xalglib.py.tar.bz2 [38.74 KiB]
Downloaded 427 times

Author:  mhavel [ Sun May 20, 2012 2:52 am ]
Post subject:  Re: [updated] Python interface: using numpy arrays as input

Hi Sergey,

I've seen some bugs that I introduced with the modified python wrapper I've posted. Mostly, it was related to the fact that I was not allowing proper casting of data types (eg. int could not be interpreted as a float). I fixed them.

The consequences are:
1) functions "is_XX{_vector/matrix}" have been all removed (no more needed). I've done that mainly because, even before I changed the wrapper to include numpy support, the code was casting data types several times:
the functions "is_XX{_vector/matrix}" were (often) called more than once/task, and were just returning True or False, thus loosing the casting results.
2) a new function "castto(x, dtype, errmsg)" is added, and used exclusively in "x_from_list" and "x_from_listlist" routines. Hence the interface/API of your code do not change at all.
3) any call to 'x_from_list' or 'x_from_listlist' now takes an (optional) extra argument, which is the error message to display if casting fails. I also moved the dimension check (of data to be cast) into these functions.

I've also replaced some 'if' by 'elif' where it was better to do so.
If you test the changes I've made and see some bugs, tell me I'll fix them.

Regards

Attachments:
xalglib.py_v2.tar.bz2 [36.65 KiB]
Downloaded 418 times

Author:  Sergey.Bochkanov [ Tue May 22, 2012 11:09 am ]
Post subject:  Re: [updated] Python interface: using numpy arrays as input

Thanks for your commit! I've attached it to the bug tracker. Unfortunately, I am not sure that I will start working on it at this week - but in the worst case it will be scheduled to the first half of the next week. I will keep you informed about progress, both by e-mail and posts to this forum. Thanks again!

Author:  mhavel [ Thu Jan 31, 2013 7:44 pm ]
Post subject:  Re: [updated] Python interface: using numpy arrays as input

Hi Sergey,

I've been watching the two last version of ALGLIB (3.6 and 3.7), and I do not see the support of numpy arrays included in it :( I thought that you wanted to include it. Do you have any problem with the modifications I've given to you? I'd just like to understand...

Regards

EDIT: I've created a script that automatically modifies the xalglib.py file to support numpy arrays. Just put the two attached files in the same directory as xalglib.py file, and run add_numpy_support.py script. It should work nicely.

Attachments:
numpy_support_fork_script.zip [4.33 KiB]
Downloaded 331 times

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/