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

[RF] Add tutorial for simulation based inference #16044

Merged
merged 2 commits into from
Aug 16, 2024

Conversation

robsyr
Copy link

@robsyr robsyr commented Jul 17, 2024

  • created new tutorial for simulation based inference (SBI)

Copy link

github-actions bot commented Jul 18, 2024

Test Results

    13 files      13 suites   3d 2h 50m 52s ⏱️
 3 028 tests  3 026 ✅ 0 💤 2 ❌
33 838 runs  33 836 ✅ 0 💤 2 ❌

For more details on these failures, see this check.

Results for commit d9e398f.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@guitargeek guitargeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this contribution. I have made a few change requests, but overall this is going in a very good direction!

Thanks to @bellenot, scikit-learn is now also installed on Windows 64 bit, but on Windows 32 bit it's not supported. Could you please add the tutorial to the list of tutorials that are vetoed on Windows 32 bit?

https://github.com/root-project/root/blob/master/tutorials/CMakeLists.txt#L131

Thanks a lot!

@@ -138,7 +138,7 @@ else()
# The following tutorials are failing with this error:
# IncrementalExecutor::executeFunction: symbol '__std_find_trivial_4@12' unresolved while linking [cling interface function]!
# on Windows 32 bit and Visual Studio v17.8
list(APPEND roofit_veto roofit/rf509_wsinteractive.C roofit/rf614_binned_fit_problems.C)
list(APPEND roofit_veto roofit/rf509_wsinteractive.C roofit/rf614_binned_fit_problems.C roofit/rf615_simulation_based_inference.py)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
list(APPEND roofit_veto roofit/rf509_wsinteractive.C roofit/rf614_binned_fit_problems.C roofit/rf615_simulation_based_inference.py)
list(APPEND roofit_veto roofit/rf509_wsinteractive.C roofit/rf614_binned_fit_problems.C)
# The next tutorial uses scikit-learn, which is not available on Windows 32 bit
list(APPEND roofit_veto roofit/rf509_wsinteractive.C roofit/rf615_simulation_based_inference.py)

Now it looks like the comment above with the The following tutorials are failing with this... is also referring to you new tutorial, which is not the case. I would organize this differently.

m_varlist("!varlist", this, right.m_varlist) {
}

RooPyBindFunction::~RooPyBindFunction() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RooPyBindFunction::~RooPyBindFunction() {}

You already deleted the destructor declaration, but you also need to remove the corresponding implementation.


#include "RooPyBindFunction.h"

// ClassImp(RooPyBindFunction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ClassImp(RooPyBindFunction);

We should not push commented-out code if there is not strong reason for it

Comment on lines +15 to +17
## \macro_code
## \macro_output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## \macro_code
## \macro_output
## \macro_image
## \macro_code
## \macro_output

We also want to see the output plots

This clsss provides the functionality to wrap arbitrary python functions in
RooFit.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace RooFit {
namespace Detail {

The implementation needs to be in the same namespace (also think about the closing brackets at the end of the file).

@@ -0,0 +1,231 @@
## \file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you go over the tutorial one with the code formatter?

black --line-length=120 tutorials/roofit/rf615_simulation_based_inference.py

@@ -46,6 +46,7 @@
LineStyle,
FillStyle,
MarkerStyle,
bindFunction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please process this file with the code formatter.

@@ -319,3 +319,37 @@ def DataError(etype):
raise exception

return RooFit._DataError(etype)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please process this file with the code formatter.

Comment on lines 66 to 69
# frame1= x_var.frame()
# c0= ROOT.TCanvas()
# frame1.Draw()
# input("")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# frame1= x_var.frame()
# c0= ROOT.TCanvas()
# frame1.Draw()
# input("")
# frame1 = x_var.frame()
# c0 = ROOT.TCanvas()
# frame1.Draw()
# input() # wait for user input to proceed

Also the commented-out code should be formatted :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually the part where you need to draw on the frame needs to be in the commented-out block too:

    # Uncomment to see input plots
    # frame1 = x_var.frame()
    # for i in parampoints:
    #    workspace[f"histpdf{i}"].plotOn(frame1)
    # c0 = ROOT.TCanvas()
    # frame1.Draw()
    # input() # wait for user input to proceed

workspace.Import(morph)


# class used in this case to demonstate the use of SBI in Root
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# class used in this case to demonstate the use of SBI in Root
# class used in this case to demonstrate the use of SBI in Root

self.classifier.fit(self.X_train, self.y_train)


# Setting the training and toy data samples; the factor 5 to enable a fair comparisson to morphing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Setting the training and toy data samples; the factor 5 to enable a fair comparisson to morphing
# Setting the training and toy data samples; the factor 5 to enable a fair comparison to morphing

## \file
## \ingroup tutorial_roofit
## \notebook
## Use Simulation Based Inference (SBI) in RooFit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Use Simulation Based Inference (SBI) in RooFit
## Use Simulation Based Inference (SBI) in RooFit.

The short description is doxygen needs to end with a period

Comment on lines 35 to 36
bin_mu_x = ROOT.RooBinning(4, 0., 4.)
grid = ROOT.RooMomentMorphFuncND.Grid2(bin_mu_x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bin_mu_x = ROOT.RooBinning(4, 0., 4.)
grid = ROOT.RooMomentMorphFuncND.Grid2(bin_mu_x)
grid = ROOT.RooMomentMorphFuncND.Grid(ROOT.RooBinning(4, 0., 4.))

Can we do this in one line? And maybe use Grid instead of Grid2. I think Grid2 only exists for backwards compatibility, but it is identical.

n_samples_train = n_samples * 5


# define the "observed" data in a workspade
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# define the "observed" data in a workspade
# define the "observed" data in a workspace

# compute the "learned" negative log likelihood ratio
nllr_learned = ROOT.RooFit.bindFunction("MyBinFunc", compute_log_likelihood_sum, mu_var)

# compute the morphed nll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# compute the morphed nll
# Create the NLL based on the template morphing pdf

Sorry to be picky here, but to be consistent with the other RooFit tutorials, all the comments need to start with upper case. Also, we should try to be as precise as possible in the comments.

Comment on lines 39 to 42
# number of 'sampled' gaussians, if you change it, adjust the binning properly
parampoints = np.arange(5)

for i in parampoints:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# number of 'sampled' gaussians, if you change it, adjust the binning properly
parampoints = np.arange(5)
for i in parampoints:
# number of 'sampled' gaussians, if you change it, adjust the binning properly
n_grid = 5
for i in range(n_grid):

No need to use numpy arrays here, especially not of type double

# pdf = workspace[f'histpdf{i}']

# add the pdf to the grid
grid.addPdf(workspace[f'histpdf{i}'], int(i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grid.addPdf(workspace[f'histpdf{i}'], int(i))
grid.addPdf(workspace[f'histpdf{i}'], i)

If you use a simple integer index instead of the NumPy array element (which is a double), I think you also won't need the conversion.

Copy link
Contributor

@guitargeek guitargeek 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 the updates, I think we're very close! Can you please also format the .h and .cxx files with the clang-format style of ROOT? Thanks!
https://github.com/root-project/root/blob/master/.clang-format

Robin Syring added 2 commits August 16, 2024 13:56
- added new tutorial for simulation based inference with roofit
- added RooPyBindFunction to bind python function with arbitrary number
  of parameters
- added Realease notes about previous changes
Copy link
Contributor

@guitargeek guitargeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this new tutorial and following up on all the change requests.

Congrats to your first contribution!

@guitargeek guitargeek merged commit 02981db into root-project:master Aug 16, 2024
16 of 17 checks passed
@guitargeek guitargeek changed the title [RF] Add tutorial SBI [RF] Add tutorial for simulation based inference Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants