ntamas / plfit

Fitting power-law distributions to empirical data, according to the method of Clauset, Shalizi and Newman
GNU General Public License v2.0
47 stars 17 forks source link

howto use "plfit_discrete_options_t" in python #8

Closed enavarro222 closed 11 years ago

enavarro222 commented 11 years ago

Hi, First of all : thanks for this great package !

I wounder how to run "plfit_discrete" with some options, from python. I test different kind call :

opt = plfit.plfit_discrete_options_t() AttributeError: No constructor defined res = plfit.plfit_discrete([1,1,1,1,1,1,1,3,3,3,3,56,56], plfit.plfit_discrete_options_t) TypeError: in method 'plfit_discrete', argument 3 of type 'plfit_discrete_options_t const *'

The mapping between to the C code is not clear for me. (I don't understand swig so much...) So, thanks if you can give me some small advices...

+

ntamas commented 11 years ago

The Python interface is pretty experimental now and I have recently added a few things to plfit_discrete_options_t on the C side without checking the Python interface, so it is theoretically possible that plfit_discrete_options_t is not fully functional now. I have checked it now and the following seems to work for me using the very latest revision:

>>> import plfit
>>> opts = plfit.plfit_discrete_options_t()
>>> opts
<plfit.plfit_discrete_options_t; proxy of <Swig Object of type 'plfit_discrete_options_t *' at 0xdeadbeef> >
>>> print opts
finite_size_correction = 0, alpha_method = 0, alpha.min = 1.01, alpha.step = 0.01, alpha.max = 5, p_value_method = 2, p_value_precision = 0.01
enavarro222 commented 11 years ago

thanks for your answer !

I just compile the last version "by hand" : $ git clone https://github.com/ntamas/plfit.git $ cd plfit $ mkdir build; cd build $ swig -Wall -O -python -outcurrentdir ../src/plfit.i ../src/plfit.i:110: Warning 504: Function _plfit_continuous_options_t must have a return type. ../src/plfit.i:141: Warning 504: Function _plfit_discrete_options_t must have a return type. $ gcc -shared -fPIC -o _plfit.so -I../src -I/usr/include/python2.7 -lm plfit_wrap.c ../src/plfit.c ../src/zeta.c ../src/sampling.c ../src/plgen.c ../src/lbfgs.c ../src/kolmogorov.c ../src/mt.c ../src/error.c ../src/options.c ../src/stats.c ../src/rbinom.c ../src/gss.c ../src/getopt.c $ cp plfit.py _plfit.so ../../

So swig have some warnings... maybe here is the problem (?) (by the way: I have to specify all the needed .c for gcc, elsewhere "main.c" is included and there is conflict with "plgen.c")

And then still the same "AttributeError:"

In [1]: import plfit

In [2]: opts = plfit.plfit_discrete_options_t()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/plfit/build/<ipython-input-2-3195538f9b9d> in <module>()
----> 1 opts = plfit.plfit_discrete_options_t()

/tmp/plfit/build/plfit.pyc in __init__(self, *args, **kwargs)
    128 class plfit_discrete_options_t(object):
    129     thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
--> 130     def __init__(self, *args, **kwargs): raise AttributeError("No constructor defined")
    131     __repr__ = _swig_repr
    132     finite_size_correction = _swig_property(_plfit.plfit_discrete_options_t_finite_size_correction_get, _plfit.plfit_discrete_options_t_finite_size_correction_set)

AttributeError: No constructor defined
ntamas commented 11 years ago

Could be a version difference between my SWIG and yours, and yes, the warnings are probably related. I'm using SWIG 2.0.7. Which SWIG version are you using?

enavarro222 commented 11 years ago

indeed : $ swig -version SWIG Version 2.0.4 Compiled with g++ [x86_64-unknown-linux-gnu] Configured options: +pcre

enavarro222 commented 11 years ago

ok, it was trivial : I just add the good return type to close the two warning and it works ! Thanks again !

ntamas commented 11 years ago

Thanks - fixed in 8eae4eb.