jpivarski / pyminuit

Automatically exported from code.google.com/p/pyminuit
GNU General Public License v2.0
14 stars 3 forks source link

use of npoints under contour fails #25

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. define some objective function to minimize: call it chi2
2. give the usual commands:
        m = minuit.Minuit(chi2, a=a_fid, b=b_fid)
        m.migrad()
        cont = array(m.contour("a", "b", 1., 50))

What is the expected output? What do you see instead?

I expected to have as output an array of 50 contour points, at the 1 sigma 
level, instead of the default 20 points... In fact the following error is 
issued:

TypeError                                 Traceback (most recent call last)

/home/orca/Dropbox/supernovae/SN_sim/scripts/<ipython console> in <module>()

/home/orca/Dropbox/supernovae/SN_sim/scripts/trotta_sim_func.py in 
trotta_sim(a_fid, b_fid, x_std, y_std, nobs, xmin, xmax)                        

     48         m.printMode = 1
     49         m.migrad()
---> 50         cont = array(m.contour("a", "b", 1.,50))
     51         cont = append(cont,[[cont[0,0],cont[0,1]]],axis=0)
     52         subplot(1,2,2)

TypeError: Arguments are: param1, param2, number of sigmas, and optionally 
approximate number of points (20).

What version of the product are you using? On what operating system?

This corresponds to the latest pyminuit in its homepage, run under a Debian 
unstable (sid) system.

Please provide any additional information below.

Original issue reported on code.google.com by mocal...@gmail.com on 18 Jul 2011 at 8:22

GoogleCodeExporter commented 9 years ago
Is this issues section, or the project in general for that matter, still being 
developed and/or updated???

Original comment by mocal...@gmail.com on 20 Jul 2011 at 9:23

GoogleCodeExporter commented 9 years ago
I tried to test this, but did not reproduce the error:

% python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import minuit
>>> m = minuit.Minuit(lambda x, y: x**2 + y**2)
>>> m.migrad()
>>> len(m.contour("x", "y", 1.))
20
>>> len(m.contour("x", "y", 1., 50))
50
>>> len(m.contour("x", "y", 1., 100))
100

But you can get different results from different fit functions (the x**2 + y**2 
in this example is naieve).  The npoints parameter is a suggestion sent to 
Minuit--- Minuit decides how many points to use.  That's why the documentation 
says "approximate".

Try this simple fit function to see if your problem is specific to your 
compilation or to your fit function.

The Minuit packaged with PyMinuit is no longer maintained (in fact, it was 
obsolete when PyMinuit began because Minuit2 was too tightly bound to ROOT).  
PyMinuit won't be ported to Python 3 because the biggest Python 2 -> Python 3 
changes are in the C API--- everything would need to be rewritten from scratch.

Original comment by jpivar...@gmail.com on 20 Jul 2011 at 3:53

GoogleCodeExporter commented 9 years ago
Hi,

I have tried the suggested code:

import minuit
m = minuit.Minuit(lambda x, y: x**2 + y**2)
m.migrad()
len(m.contour("x", "y", 1.))

 len(m.contour("x", "y", 1., 50))

both under the Debian lenny box (python 2.5) and the Debian sid box
(python 2.6) and the results are still the same:

* under lenny:

In [4]: len(m.contour("x","y",1.))
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
Out[4]: 20

In [5]: len(m.contour("x","y",1.,50))
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
Out[5]: 50

which, apart from the warnings, seems to be fine...

* under sid:
In [4]: len(m.contour("x","y",1.))
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
VariableMetricBuilder: warning: no improvement in line search
Out[4]: 20

In [5]: len(m.contour("x","y",1.,50))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/orca/Dropbox/supernovae/SN_sim/scripts/<ipython console> in <module>()

TypeError: Arguments are: param1, param2, number of sigmas, and
optionally approximate number of points (20).

What is the python you have run your example in: 2.5, 2.6, 2.7? Could
it be somehow that from 2.5 to 2.6 or 2.7 the Python to C API has gone
"wrong" with parsing the parameters from a Python function to a C
one?? It looks as if under the Debian sid box, which runs Python 2.6,
pyminuit is not recognizing the optional parameter :-(

Thanks anyhow!

PS: the rewriting from scratch, in order to make it work under Python
3.0, would mean rewriting the interface bewteen Minuit and Python
(sorry if it is a silly question; just would like to have some notion
of the difficulty)??

-- 
#######################################
Prof. Mauricio Ortiz Calvao
Federal University of Rio de Janeiro
Institute of Physics, P O Box 68528
CEP 21941-972 Rio de Janeiro, RJ
Brazil

Email: orca@if.ufrj.br
Phone: (55)(21)25627483
Homepage: http://www.if.ufrj.br/~orca
#######################################

Original comment by mocal...@gmail.com on 20 Jul 2011 at 5:33

GoogleCodeExporter commented 9 years ago
> TypeError                                 Traceback (most recent call last)
> 
> /home/orca/Dropbox/supernovae/SN_sim/scripts/<ipython console> in <module>()
> 
> TypeError: Arguments are: param1, param2, number of sigmas, and
> optionally approximate number of points (20).

I know that this has been tested in Python 2.6 (and a few earlier versions) 
because I've been using that.  I just recently upgraded to Ubuntu 11.04, so I'm 
using Python 2.7 now.

I'd be very surprised if the Python-C API changed so much as to break this 
functionality between Pythons ~2.4 and 2.7.  One thing that's more likely is 
that your copy needs to be recompiled.  If some things were compiled on one 
system and others on another, then the link tables in your shared objects could 
become offset.  Recompiling would be a good idea.  Unfortunately, this is the 
only possibility that I can think of, not the only possibility.

> PS: the rewriting from scratch, in order to make it work under Python
> 3.0, would mean rewriting the interface bewteen Minuit and Python
> (sorry if it is a silly question; just would like to have some notion
> of the difficulty)??

It would be a matter of rewriting the whole PyMinuit project.  Minuit itself 
would not need to be changed, but PyMinuit is a thick wrapper: it extends the 
functionality of Minuit in a few ways.

In Python 3, the whole Python-C API has been changed (for the better, I hear).  
When the day comes to switch to Python 3, I don't think I'll rewrite PyMinuit.  
There are some function-fitting algorithms in SciPy, and SciPy is being updated 
to Python 3.  I'd rather have direct access to the function minimizer, rather 
than just the fitting application, but maybe there's a way to do that.

Original comment by jpivar...@gmail.com on 20 Jul 2011 at 9:04

GoogleCodeExporter commented 9 years ago
Ok Jim

Thanks for the replies; I will try to recompile on my Debian sid
machine and will ask my colleague with Ubuntu to do the same; one last
question, are you sure the version of pyminuit available in its site
is the one you effectively used in your present Ubuntu machine?

Thanks again!

PS: the thick wrapper you mentioned are "just" the files minuit.cpp
and minuit.h under the pyminuit directory, after unzipping, right??

-- 
#######################################
Prof. Mauricio Ortiz Calvao
Federal University of Rio de Janeiro
Institute of Physics, P O Box 68528
CEP 21941-972 Rio de Janeiro, RJ
Brazil

Email: orca@if.ufrj.br
Phone: (55)(21)25627483
Homepage: http://www.if.ufrj.br/~orca
#######################################

Original comment by mocal...@gmail.com on 20 Jul 2011 at 9:32

GoogleCodeExporter commented 9 years ago
> Thanks for the replies; I will try to recompile on my Debian sid
> machine and will ask my colleague with Ubuntu to do the same; one last
> question, are you sure the version of pyminuit available in its site
> is the one you effectively used in your present Ubuntu machine?

Yes, I had to download it and install it fresh to do this test (new computer).

> PS: the thick wrapper you mentioned are "just" the files minuit.cpp
> and minuit.h under the pyminuit directory, after unzipping, right??

Yes.

Original comment by jpivar...@gmail.com on 20 Jul 2011 at 9:46

GoogleCodeExporter commented 9 years ago
Just FYI: the trunk of PyMinuit starting with revision r93 compiles and works 
with Python3. The C API changes from Python 2 to 3 are relatively minor and can 
be abstracted away in some (ugly) pre-processor macros.

Original comment by franke....@gmail.com on 30 Aug 2011 at 1:58

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
> Just FYI: the trunk of PyMinuit starting with revision r93 compiles and works 
with Python3. The C API changes from Python 2 to 3 are relatively minor and can 
be abstracted away in some (ugly) pre-processor macros.

Thanks!  I made revision r93 into pyminuit-1.2.1, and I'm using it now under 
Python 2.7.

-- JIm

Original comment by jpivar...@gmail.com on 30 Aug 2011 at 4:29