Skip to content
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

Merged
merged 1 commit into from
Feb 20, 2023

Conversation

lmoneta
Copy link
Member

@lmoneta lmoneta commented Feb 9, 2023

Convert NumericalMinimization.C tutorial to Python

@lmoneta
Copy link
Member Author

lmoneta commented Feb 9, 2023

@phsft-bot build just on ROOT-ubuntu2004/default with flags -DCTEST_TEST_EXCLUDE_NONE=On

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-ubuntu2004/default with flags -DCTEST_TEST_EXCLUDE_NONE=On
How to customize builds

@root-project root-project deleted a comment from phsft-bot Feb 9, 2023
@root-project root-project deleted a comment from phsft-bot Feb 9, 2023
@root-project root-project deleted a comment from phsft-bot Feb 9, 2023
Copy link
Member

@couet couet left a 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
Copy link
Member

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?

Copy link
Member Author

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.

@lmoneta
Copy link
Member Author

lmoneta commented Feb 10, 2023

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

Copy link
Member

@vepadulano vepadulano left a 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 ):
Copy link
Member

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()
Copy link
Member

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

Comment on lines 23 to 32
# 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
Copy link
Member

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
Copy link
Member

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?")
Copy link
Member

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))

Comment on lines 40 to 41
# set tolerance and other minimizer parameters
# one can also use default values
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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())
Copy link
Member

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!");
Copy link
Member

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.

@lmoneta
Copy link
Member Author

lmoneta commented Feb 10, 2023

Thank you very much @vepadulano for all the comments. I have included them in the new version.

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac12/noimt, mac11/cxx14, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu2004/python3.
See console output.

import ROOT
import numpy as np

def RosenBrock( vecx ):
Copy link
Member

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

Comment on lines +23 to +32
# 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
Copy link
Member

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())
Copy link
Member

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

Copy link
Member Author

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!

Comment on lines 77 to 78
if ( ret and minimizer.MinValue() < 1.E-4 ) :
print("Minimizer {} - {} converged to the right minimum!".format(minimizerName,algoName))
Copy link
Member

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?

Copy link
Member Author

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).

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac12/noimt, mac11/cxx14, windows10/cxx14
How to customize builds

@lmoneta
Copy link
Member Author

lmoneta commented Feb 16, 2023

@phsft-bot build just on ROOT-performance-centos8-multicore/cxx17 -DCTEST_TEST_EXCLUDE_NONE=On

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-performance-centos8-multicore/cxx17
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-performance-centos8-multicore/cxx17.
Running on olbdw-01.cern.ch:/data/sftnight/workspace/root-pullrequests-build
See console output.

Failing tests:

@lmoneta lmoneta merged commit 652d7fd into root-project:master Feb 20, 2023
@lmoneta lmoneta deleted the add_minim_tutorial branch February 20, 2023 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants