Calling stats functions in cython

120 views
Skip to first unread message

Joon Ro

unread,
Apr 11, 2012, 1:16:24 PM4/11/12
to cython-users
Hi,

I need to call cdf and ppf of beta distribution (with general parameters)
inside cython module, and
I suspect importing scipy.stats.beta inside cython and use the function
will be slow.
The routine is loop intensive and I have call them a lot of times.
I was wondering what would be the best way to do this?

Best,
Joon
--

Thomas Wiecki

unread,
Apr 11, 2012, 2:29:47 PM4/11/12
to cython...@googlegroups.com
I had great success calling directly into the GSL. You can find a
cython interface to the GSL (maintained by me) here:

https://github.com/twiecki/CythonGSL

Thomas

Joon Ro

unread,
Apr 11, 2012, 4:49:00 PM4/11/12
to cython...@googlegroups.com
Thanks a lot. I will definitely check this out.

Joon


--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Joon Ro

unread,
Apr 13, 2012, 4:48:02 PM4/13/12
to cython...@googlegroups.com, Thomas Wiecki
Hi Thomas,

I'm trying out cythongsl at the moment, but when I try to compile the
module I get the following error:

/home/joon/programs/epd-7.2-2-rh5-x86_64/lib/python2.7/distutils/extension.py:133:
UserWarning: Unknown Extension options: 'cython_include_dirs'
warnings.warn(msg)
running build_ext
cythoning integrate.pyx to integrate.c

Error compiling Cython file:
------------------------------------------------------------
...
from cython_gsl cimport *
^
------------------------------------------------------------

integrate.pyx:1:0: 'cython_gsl.pxd' not found


I just git cloned your git repo and tried to compile the example with

python setup_integrate.py build_ext --inplace

Could you please help me out on this?

Best regards,
Joon


On Wed, 11 Apr 2012 13:29:47 -0500, Thomas Wiecki
<thomas...@googlemail.com> wrote:

Thomas Wiecki

unread,
Apr 13, 2012, 6:50:14 PM4/13/12
to Joon Ro, cython...@googlegroups.com
Great! I'll add cython 0.16 as a requirement once it comes out.

Please share any success stories!

HTH,
Thomas


On Fri, Apr 13, 2012 at 6:29 PM, Joon Ro <joon...@gmail.com> wrote:
> Apparently it was because of the cython version. I just installed
> Cython-0.16rc1 and it worked well.
>
> -Joon
>
>
>
> On Fri, 13 Apr 2012 17:00:27 -0500, Joon Ro <joon...@gmail.com> wrote:
>
>> Thanks for the reply!
>>
>> I did not change anything. Actually what I found was my
>> Cython.Distutils.Extension does not have
>> cython_include_dirs argument:
>>
>> In [1]: import Cython.Distutils as distutils
>>
>> In [2]: distutils.Extension?
>> Type:       classobj
>> String Form:Cython.Distutils.extension.Extension
>> Namespace:  Interactive
>> File:
>> /home/joon/programs/epd-7.2-2-rh5-x86_64/lib/python2.7/site-packages/Cython/Distutils/extension.py
>> Docstring:  <no docstring>
>> Constructor information:
>>  Definition:distutils.Extension(self, name, sources, include_dirs=None,
>> define_macros=None, undef_macros=None, library_dirs=None, libraries=None,
>> runtime_library_dirs=None, extra_objects=None, extra_compile_args=None,
>> extra_link_args=None, export_symbols=None, depends=None, language=None,
>> pyrex_include_dirs=None, pyrex_directives=None, pyrex_create_listing=0,
>> pyrex_line_directives=0, pyrex_cplus=0, pyrex_c_in_temp=0, pyrex_gen_pxi=0,
>> pyrex_gdb=False, no_c_in_traceback=False, **kw)
>>
>> So I changed cython_include_dirs to pyrex_include_dirs, and now I could
>> compile it.
>>
>> But after compiling it, when I try to run the function I get the
>> following:
>>
>> In [1]: import integrate
>> In [2]: integrate.cdf_numerical(0.5, 1, 1)
>> gsl: qag.c:261: ERROR: could not integrate function
>> Default GSL error handler invoked.
>> [1]    6243 abort      ipython
>>
>> It would be great if you could help me out. Is this caused by my cython
>> version? I've got Cython-0.15.1.
>>
>> Best regards,
>> Joon
>>
>>
>>
>> On Fri, 13 Apr 2012 16:12:54 -0500, Thomas Wiecki
>> <thomas...@googlemail.com> wrote:


>>
>>> On Fri, Apr 13, 2012 at 4:48 PM, Joon Ro <joon...@gmail.com> wrote:
>>>>
>>>> Hi Thomas,
>>>>
>>>> I'm trying out cythongsl at the moment, but when I try to compile the
>>>> module
>>>> I get the following error:
>>>>
>>>>
>>>> /home/joon/programs/epd-7.2-2-rh5-x86_64/lib/python2.7/distutils/extension.py:133:
>>>> UserWarning: Unknown Extension options: 'cython_include_dirs'
>>>>  warnings.warn(msg)
>>>> running build_ext
>>>> cythoning integrate.pyx to integrate.c
>>>>
>>>> Error compiling Cython file:
>>>> ------------------------------------------------------------
>>>> ...
>>>> from cython_gsl cimport *
>>>> ^
>>>> ------------------------------------------------------------
>>>>
>>>> integrate.pyx:1:0: 'cython_gsl.pxd' not found
>>>>
>>>>
>>>> I just git cloned your git repo and tried to compile the example with
>>>>
>>>> python setup_integrate.py build_ext --inplace
>>>>
>>>> Could you please help me out on this?
>>>>
>>>> Best regards,
>>>>
>>>> Joon
>>>
>>>

>>> I assume you installed CythonGSL via setup.py install?
>>>
>>> Also, did you alter the setup_integrate.py?
>>> It's weird that you get this:


>>>
>>>>
>>>> /home/joon/programs/epd-7.2-2-rh5-x86_64/lib/python2.7/distutils/extension.py:133:
>>>> UserWarning: Unknown Extension options: 'cython_include_dirs'
>>>
>>>

>>> as it should use Extension from Cython.Distutils, not distutils, which
>>> has a cython_include_dirs. To make sure, the second line of
>>> setup_integrate.py should read:
>>>
>>> from Cython.Distutils import Extension
>>>
>>> HTH,
>>> Thomas

Joon Ro

unread,
Apr 14, 2012, 1:50:40 PM4/14/12
to cython...@googlegroups.com, Thomas Wiecki
Will do. I'm currently playing with it and it seems to be perfect for my
application. Thank you very much.
Oh by the way, shouldn't

return gsl_ran_gaussian_pdf(x, sigma) + mu

be

return gsl_ran_gaussian_pdf(x - mu, sigma)

in the CythonGSL / examples / integrate.pyx?

Hopefully I will be able to submit some more examples in the future.

Best,
Joon

On Fri, 13 Apr 2012 17:50:14 -0500, Thomas Wiecki

Thomas Wiecki

unread,
Apr 14, 2012, 5:08:38 PM4/14/12
to Joon Ro, cython...@googlegroups.com
On Sat, Apr 14, 2012 at 1:50 PM, Joon Ro <joon...@gmail.com> wrote:
> Will do. I'm currently playing with it and it seems to be perfect for my
> application. Thank you very much.

Great, happy to help.

> Oh by the way, shouldn't
>
>  return gsl_ran_gaussian_pdf(x, sigma) + mu
>
> be
>
>  return gsl_ran_gaussian_pdf(x - mu, sigma)
>
> in the CythonGSL / examples / integrate.pyx?

Absolutely -- thanks for reporting. Pushed a fix.

> Hopefully I will be able to submit some more examples in the future.

OK, will be glad to include them.

Thomas

Reply all
Reply to author
Forward
0 new messages