From heroxbd at gmail.com Wed Dec 4 16:05:00 2013 From: heroxbd at gmail.com (heroxbd at gmail.com) Date: Thu, 05 Dec 2013 00:05:00 +0900 Subject: [C++-sig] numpy integration with boost.python Message-ID: <86iov4y7vn.fsf@moguhome00.in.awa.tohoku.ac.jp> Dear all, This is a cross post of stackoverflow[0]. I am interfacing a C++ data intense library with Python by py++[1]/boost.python[2]. After profiling my program, I find 70% of run time is spent on code like this: ni = range(v2o.getHits()) tau = np.array([v2o.TofCorrectedTime[i] for i in ni]) q = np.array[v2o.getCharge()[i] for i in ni] v2o.TofCorrectedTime is typed __array_1_float_2368[3] from py++. v2o.getCharge() is typed _impl_details_range_iterator_[4] from py++, too. Size being about 2000, the convertion from these py++ array wrappers to numpy is slow: In [42]: timeit np.array(v2o.TofCorrectedTime) 100 loops, best of 3: 2.52 ms per loop In [43]: timeit np.array(v2o.getCharge()) 100 loops, best of 3: 4.94 ms per loop In [44]: timeit np.array([0]*2368) 1000 loops, best of 3: 310 ?s per loop In [45]: timeit np.array(np.zeros(2368)) 100000 loops, best of 3: 4.41 ?s per loop I searched the web for a solution. The candidates are: 1. Cython[5] with memoryview[6] 2. pyublas[7] 3. Boost.NumPy[8] Questions: - Is cython/memoryview easy to be integrated with boost.python and py++? I want to keep the rest of the library wrapper. - Which one best suites my problem in terms of convertion overhead? Thanks, Benda [0]: http://stackoverflow.com/questions/20376021/exchange-numpy-array-with-boost-python-pyublas-or-boost-numpy [1]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev [2]: http://www.boost.org/libs/python [3]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev/pyplusplus/code_repository/array_1.py [4]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev/pyplusplus/code_repository/return_range.py [5]: http://cython.org [6]: http://docs.cython.org/src/userguide/memoryviews.html [7]: http://mathema.tician.de/software/pyublas/ [8]: https://github.com/ndarray/Boost.NumPy From ndbecker2 at gmail.com Thu Dec 5 13:00:26 2013 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 05 Dec 2013 07:00:26 -0500 Subject: [C++-sig] numpy integration with boost.python References: <86iov4y7vn.fsf@moguhome00.in.awa.tohoku.ac.jp> Message-ID: heroxbd at gmail.com wrote: > Dear all, > > This is a cross post of stackoverflow[0]. > ... > I searched the web for a solution. The candidates are: > > 1. Cython[5] with memoryview[6] > 2. pyublas[7] > 3. Boost.NumPy[8] > ... I've been using ndarray lately: https://github.com/ndarray/ndarray From heroxbd at gmail.com Fri Dec 6 14:55:09 2013 From: heroxbd at gmail.com (heroxbd at gmail.com) Date: Fri, 06 Dec 2013 22:55:09 +0900 Subject: [C++-sig] numpy integration with boost.python In-Reply-To: <86iov4y7vn.fsf@moguhome00.in.awa.tohoku.ac.jp> (heroxbd@gmail.com's message of "Thu, 05 Dec 2013 00:05:00 +0900") References: <86iov4y7vn.fsf@moguhome00.in.awa.tohoku.ac.jp> Message-ID: <86mwkeuls2.fsf@moguhome00.in.awa.tohoku.ac.jp> Dear all, heroxbd at gmail.com writes: > This is a cross post of stackoverflow[0]. > > [0]: http://stackoverflow.com/questions/20376021/exchange-numpy-array-with-boost-python-pyublas-or-boost-numpy Feedback as a happy user. Jim has answered on stackoverflow. I decided to go with Boost.NumPy. And thanks Neal for sharing with your experience. The array data needs not to be copied anymore, giving a 2000-sized array exposing to Python 1000x faster than iteration- over-elements scheme. I had tens of such arrays to maintain. Too tedious to do it manually. So I made a patch against Py++ SVN trunk[1]. I think Roman is in this list. Could you please review it? Jim, thanks again! I hope boost.python v3 is going on well. Cheers, Benda 1. https://sourceforge.net/p/pygccxml/patches/2/ From catchall at schlipf.it Mon Dec 16 16:06:19 2013 From: catchall at schlipf.it (Mario Schlipf) Date: Mon, 16 Dec 2013 16:06:19 +0100 Subject: [C++-sig] Boost.Python: How to expose std::unique_ptr Message-ID: <008b01cefa70$6107b7e0$231727a0$@schlipf.it> Hi all, I am farily new to boost.python and trying to expose the return value of a function to python. The function signature looks like this: std::unique_ptr someFunc(const std::string &str) const; When calling the function in python, I get the following error: TypeError: No to_python (by-value) converter found for C++ type: std::unique_ptr > My function call in python looks like this: a = mymodule.MyClass() a.someFunc("some string here") # error here I tried to expose the smart pointer with the following statements: class_>, bost::noncopyable ("Message", init<>()) ; This example compiles, but I still get the error mentioned above. Also, I tried to expose the class `Message` itself class_("Message", init()) .def(init()) .def("f", &Message::f) ; Does someone know how to properly expose the pointer class? Thanks!