pyminuit icon indicating copy to clipboard operation
pyminuit copied to clipboard

Impossible to use PyMinuit from inside a class

Open GoogleCodeExporter opened this issue 10 years ago • 3 comments

What steps will reproduce the problem?

#!/usr/bin/python
import minuit

class program:
    def __init__(self):
        m = minuit.Minuit(self.f)
        print m.parameters
        m.migrad()

    def f(self, x, y):
        return (x+3)**2 + (y-4)**2


p = program()


What is the expected output? What do you see instead?
('self', 'x', 'y')
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    p = program()
  File "test.py", line 7, in __init__
    m.migrad()
TypeError: f() takes exactly 3 arguments (4 given)

The output of the print statement (first line in the paragraph above) shows
the problem: 'self' is recognized as an argument of the function 'f'. Then,
apparantly, pyminuit calls f as f(self, x, y) which, by the class magic, is
translated to f(self, self, x, y).

Please provide any additional information below.

You can work around this by placing the function 'f' outside the class.
Ugly, but this works. However, if you import a module and that module is
using minuit, it is impossible to let that module reference the global
function 'f'. At least, I didn't find a way.

I have a simulation/analysis program which is neatly factorized into
modules. However, I can't use PyMinuit and have to resort to the scipy
optimize functions, which fall short of my needs.

Possible fix:
Recognize the 'self' argument and remove that from the parameter list. The
only caveat is then that the function f should be defined as f(self, x, y)
and not as f(s, x, y) or something like that, which is allowed by the
language. Maybe it is possible to recognize you're inside a module? I can
live with the caveat. Since I don't grok C++/Python bindings I can't create
a patch...

PyMinuit seems great (I know Minuit is) so please...?? :-)

Thanks!

Original issue reported on code.google.com by [email protected] on 12 Apr 2008 at 8:22

GoogleCodeExporter avatar Mar 15 '15 22:03 GoogleCodeExporter