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][PyROOT] Pythonize RooFit::DataError to avoid Python 3 syntax errors #9721

Merged
merged 3 commits into from
Feb 2, 2022

Conversation

guitargeek
Copy link
Contributor

In Python 3, you can't do this because None is special:

DataError=ROOT.RooAbsArg.None # syntax error!

The tutorials used some workarounds that were either obfuscating None
by using the enum value directly, or used the more verbose getattr
approach:

DataError=ROOT.RooAbsData.ErrorType(2)
DataError=getattr(ROOT.RooAbsData, "None")

This commit pythonizes DataError with the usual string-to-enum
pythonization pattern already used for LineColor or LineStyle to
avoid this uglieness. One can do now:

DataError="None"
DataError=None

This PR also generalized the string-to-enum pythonization for strings (see the first commit).

@guitargeek guitargeek self-assigned this Jan 26, 2022
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, ROOT-ubuntu2004/soversion, mac1015/python3, mac11/cxx17, windows10/cxx14
How to customize builds

@guitargeek guitargeek changed the title [RF][PyROOT] Pythonize RooFit::DataError to avoid Py 3 syntax errors [RF][PyROOT] Pythonize RooFit::DataError to avoid Python 3 syntax errors Jan 26, 2022
# also less convenient. With `eval`, the string that is passed
# by the user can also contain postfix operations on the enum
# value, which is often used for columns, e.g. `kGreen+1`.
return eval("ROOT." + value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a test of this new feature e.g. pdf.plotOn(frame, LineColor="kOrange+1") ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's tested in the tutorials

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but ideally all pythonizations should have their own unit tests here, in _pythonization/test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok! I have added a new unit test file with this (see the first commit in the PR, which got amended)

XErrorSize=0,
)
morph_datahist_0p01.plotOn(frame1, Name="morph_dh_cHq3=0.01", LineColor="kGreen", **plot_args)
morph_datahist_0p25.plotOn(frame1, Name="morph_dh_cHq3=0.25", LineColor="kGreen+1", **plot_args)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see the test for the kGreen+1 is in this other commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also same comment as for the None pythonization: do we want this in Doxygen too to give it even more visibility (and to have it as reference for users).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok! I added the docs.



@cpp_signature("DataError(Int_t) ;")
def 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.

Looks good! It would be good to have this new addition in the Doxygen docs of this file, right? Besides modifying the tutorials.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok! I added the docs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Where is a DataError="SumW2"-like example shown in the docs, sorry?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a few lines below the line you made this comment for

Copy link
Contributor

Choose a reason for hiding this comment

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

Saw it now, thanks!

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, ROOT-ubuntu2004/soversion, mac1015/python3, mac11/cxx17, windows10/cxx14
How to customize builds

@guitargeek
Copy link
Contributor Author

Hi @etejedor, thanks for the review! I have improved the documentation situation now with and additional commit.

After this PR, if you go to the documentation of RooAbsData::plotOn(), you can click on DataError to end up at the documentation for DataError that includes the PyROOT box with the python docstring. I have compiled the documentation locally to see that this works.

Before, `getattr(ROOT, value)` was used to the the color enum value
corresponding to a string, but this commit suggests to use:

```Python
eval("ROOT." + value)
```

The `getattr(ROOT, value)` call had a bit less overhead, but it's
also less convenient. With `eval`, the string that is passed
by the user can also contain postfix operations on the enum
value, which is often used for columns, e.g. `kGreen+1`.
In Python 3, you can't do this because `None` is special:
```Python
DataError=ROOT.RooAbsArg.None # syntax error!
```

The tutorials used some workarounds that were either obfuscating `None`
by using the enum value directly, or used the more verbose `getattr`
approach:
```Python
DataError=ROOT.RooAbsData.ErrorType(2)
DataError=getattr(ROOT.RooAbsData, "None")
```

This commit pythonizes DataError with the usual string-to-enum
pythonization pattern already used for `LineColor` or `LineStyle` to
avoid this uglieness. One can do now:
```Python
DataError="None"
DataError=None
```
The pythonizaiton of some RooFit global functions merited their own
documentations, which are added in this commit.

To make these documentations accessible from doxygen, the following
changes were made in addition:

  * the `print_roofit_pyz_doctrings.py` script now also considers free
    functions

  * the global functions are correctly linked to in the documentation of
    `RooAbsData::plotOn`
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, ROOT-ubuntu2004/soversion, mac1015/python3, mac11/cxx17, windows10/cxx14
How to customize builds

@guitargeek guitargeek merged commit 483d15c into root-project:master Feb 2, 2022
@guitargeek guitargeek deleted the dataerror_pyz_1 branch February 2, 2022 11:21
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.

3 participants