-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[math] Add new Python tutorial for minimization #12274
Conversation
0ecf563
to
9efb10b
Compare
@phsft-bot build just on ROOT-ubuntu2004/default with flags -DCTEST_TEST_EXCLUDE_NONE=On |
Starting build on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, There is no plot to show?
# Show usage with all the possible minimizers. | ||
# Minimize the Rosenbrock function (a 2D -function) | ||
# This example is described also in | ||
# http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really want to refer to the old drupal wb site?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! This link is broken, we need to fix it also in the C++ version of the tutorial.
No there is no plot to show, this just performing a minimization of a function and it is a translation of the same existing tutorial in C++. We could maybe make a nice plot showing the steps to converge to the minimum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the tutorial! See some comments before merging
|
||
import ROOT | ||
|
||
def RosenBrock( vecx ): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to parse the file through a formatter such as autopep8
or black
|
||
return 0; | ||
|
||
NumericalMinimization() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline at the end of the file
# create minimizer giving a name and a name (optionally) for the specific algorithm | ||
# possible choices are: | ||
# minimizerName algoName | ||
# Minuit Migrad, Simplex,Combined,Scan (default is Migrad) | ||
# Minuit2 Migrad, BFGS, Simplex,Combined,Scan (default is Migrad) | ||
# GSLMultiMin ConjugateFR, ConjugatePR, BFGS, BFGS2, SteepestDescent | ||
# GSLSimAn | ||
# Genetic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting here is very weird, it would be nicer to have the choices listed in some sort of bullet list or similar. Also, I believe this is the documentation of the following function. In Python this should be written as a docstring, after the function name
# | ||
# \author Lorenzo Moneta | ||
|
||
import ROOT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a comment valid for the whole file. It would be better to stick to PEP8 as much as possible when writing Python code. For example, naming of variables and functions should follow snake_case and not CamelCase (apart from the calls to ROOT API which uses CamelCase).
|
||
minimizer = ROOT.Math.Factory.CreateMinimizer(minimizerName, algoName); | ||
if (not minimizer) : | ||
print("Error: cannot create minimizer \"",minName,"\". Maybe the required library was not built?") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few problems here. The first is that the minName
variable is not defined. I guess you mean minimizerName
. Second, I don't think this way of using print is what we want:
Python 2 will print it as a tuple
>>> a = "here"
>>> print("This is it: ", a, " --> hello")
('This is it: ', 'here', ' --> hello')
Python 3 will add unnecessary whitespace to the string.
>>> a = "here"
>>> print("This is it: ", a, " --> hello")
This is it: here --> hello
The more correct way would be using format
, which is Python2 and 3 compatible:
print("Error: cannot create minimizer \"{}\". Maybe the required library was not built?".format(minimizerName))
# set tolerance and other minimizer parameters | ||
# one can also use default values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All comments should start with capital letter
|
||
f = ROOT.Math.Functor(RosenBrock,2); | ||
|
||
#evaluate function at a point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment should start with capital letter. Also, there should be a whitespace after the #
sign
f = ROOT.Math.Functor(RosenBrock,2); | ||
|
||
#evaluate function at a point | ||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import
statements should go at the top of the module
ret = minimizer.Minimize(); | ||
|
||
xs = minimizer.X(); | ||
print("Minimum: f(", xs[0], ",", xs[1],"): ",minimizer.MinValue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In all print statements please use format
, see comment above
"converged to the right minimum!") | ||
else : | ||
print("Minimizer " ,minimizerName, " - ",algoName, "failed to converge !!!"); | ||
ROOT.Error("NumericalMinimization","fail to converge!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use ROOT.Error
here? In the error case above (the one if (not minimizer) :
) you were just returning an error code. The tutorial should be more consistent regarding error handling.
If you decide to keep the error here, it should be a raise RuntimeError
or other standard Python exceptions.
Thank you very much @vepadulano for all the comments. I have included them in the new version. |
9efb10b
to
1d358cf
Compare
Starting build on |
Build failed on ROOT-ubuntu2004/python3. |
import ROOT | ||
import numpy as np | ||
|
||
def RosenBrock( vecx ): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file is still missing a pass with a formatter such as autopep8
# create minimizer giving a name and a name (optionally) for the specific algorithm | ||
# possible choices are: | ||
# minimizerName algoName | ||
# | ||
# Minuit Migrad, Simplex,Combined,Scan (default is Migrad) | ||
# Minuit2 Migrad, BFGS, Simplex,Combined,Scan (default is Migrad) | ||
# GSLMultiMin ConjugateFR, ConjugatePR, BFGS, BFGS2, SteepestDescent | ||
# GSLSimAn | ||
# Genetic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments should become a docstring, right?
ret = minimizer.Minimize(); | ||
|
||
xs = minimizer.X(); | ||
print("Minimum: f(", xs[0], ",", xs[1],"): ",minimizer.MinValue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This print statement is missing format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is also fixed now!
if ( ret and minimizer.MinValue() < 1.E-4 ) : | ||
print("Minimizer {} - {} converged to the right minimum!".format(minimizerName,algoName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this is the "correct" output from the function right? Is there no correct return value? Does the tutorial only show this print statement? What is the user supposed to see at the end of the tutorial?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user is supposed to see the minimum XMIN and the minimum value of the function, f(XMIN) and if it fails ret
will be false or minimizer.MinValue() will be larger than zero. (the minimum of the function is at zero).
1d358cf
to
8b17151
Compare
Starting build on |
@phsft-bot build just on ROOT-performance-centos8-multicore/cxx17 -DCTEST_TEST_EXCLUDE_NONE=On |
Starting build on |
Build failed on ROOT-performance-centos8-multicore/cxx17. Failing tests: |
Convert
NumericalMinimization.C
tutorial to Python