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

Fix doxygen generation with Python 3 #3434

Merged
merged 2 commits into from
Apr 9, 2019

Conversation

ellert
Copy link
Contributor

@ellert ellert commented Feb 15, 2019

This PR fixes problems when building the doxygen documentation when the ROOT Python bindings (PyROOT) are built against Python 3.

@ellert ellert requested a review from couet as a code owner February 15, 2019 11:44
@phsft-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@couet
Copy link
Member

couet commented Mar 19, 2019

I see you also changed some a the python tutorials. I will ask our python expert to review.

@couet couet requested a review from etejedor March 19, 2019 11:36
@etejedor
Copy link
Contributor

Thank you @ellert , were the changes of the tutorials intended to be part of this doxygen-related PR? If so, I would perhaps place them in a separate commit. I will comment on the changes.


# A simple helper function to fill a test tree: this makes the example
# stand-alone.
def fill_tree(treeName, fileName):
d = RDataFrame(25000)
d = ROOT.ROOT.RDataFrame(25000)
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason to re-add this duplication? Is this not working properly in Py3? @dpiparo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no problem running the tutorials with python 3 from the command line.

The issue has to do with scoping of variables when the tutorial is run from within the doxygen filter. The global scope is then the makeimage.py script and not the tutorial. So variables declared as "global" in the tutorial are no longer global when run inside the makeimage wrapper, and therefore not accessible in a local scope (like inside a function definition).

In this case the "RDataFrame = ROOT.ROOT.RDataFrame" earlier in the file is not visible inside the scope of fill_tree().


def WithPyROOT():
def WithPyROOT(filename):
from math import sqrt
Copy link
Contributor

Choose a reason for hiding this comment

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

Why moving the import inside the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here. The import if done at the file scope is not visible in the local scope of WithPyROOT() when the tutorial is run inside the makeimage wrapper.

@@ -38,7 +38,12 @@
3.0))

# For each quantile fill with the pdf
xx = [-1.5] + [quant.GetBinContent(i)for i in range(1, 9)] + [1.5]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why eliminating the (nicer) list comprehension syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same, but python 3 specific. The list comprehension in Python 3 defines the code that is run for each element as a function call, and hence in a new local scope. So it can not see the quant variable when run inside the makeimage wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the other proposed changes in this PR applied (so that python 3 is supported in the doxygen filter) except this rewrite of the list comprehension in the tStudent.py tutorial you get the following error when running the doxygen filter on the file:

$ DOXYGEN_SOURCE_DIRECTORY=../.. DOXYGEN_OUTPUT_DIRECTORY=/tmp/x PYTHON_EXECUTABLE=/usr/bin/python3 ./filter ../../tutorials/math/tStudent.py >/dev/null
[NbConvertApp] Converting notebook /tmp/x/notebooks/tStudent.py.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 26589 bytes to /tmp/x/notebooks/tStudent.py.nbconvert.ipynb
Info in <ROOT::Math::MathMoreLibrary>: libMathMore has been loaded.
Traceback (most recent call last):
  File "makeimage.py", line 40, in <module>
    makeimage(argv[1], argv[2], argv[3], bool(argv[4]), bool(argv[5]), bool(argv[6]))
  File "makeimage.py", line 15, in makeimage
    if py: exec(compile(open(MacroName, "rb").read(), MacroName, 'exec'))
  File "../../tutorials/math/tStudent.py", line 41, in <module>
    xx = [-1.5] + [quant.GetBinContent(i)for i in range(1, 9)] + [1.5]
  File "../../tutorials/math/tStudent.py", line 41, in <listcomp>
    xx = [-1.5] + [quant.GetBinContent(i)for i in range(1, 9)] + [1.5]
NameError: name 'quant' is not defined
Segmentation fault (minnesutskrift skapad)

With the change it works as expected:

$ DOXYGEN_SOURCE_DIRECTORY=../.. DOXYGEN_OUTPUT_DIRECTORY=/tmp/x PYTHON_EXECUTABLE=/usr/bin/python3 ROOTSYS= ./filter ../../tutorials/math/tStudent.py >/dev/null
[NbConvertApp] Converting notebook /tmp/x/notebooks/tStudent.py.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 26667 bytes to /tmp/x/notebooks/tStudent.py.nbconvert.ipynb
Info in <ROOT::Math::MathMoreLibrary>: libMathMore has been loaded.
TH1::TH1:0: RuntimeWarning: nbins is <=0 - set to nbins = 1
Info in <TCanvas::Print>: png file /tmp/x/html/pict1_tStudent.py.png has been created

def WithRDataFrameVecOpsJit():
f = RDF(treename, filename)
def WithRDataFrameVecOpsJit(treename, filename):
f = ROOT.ROOT.RDataFrame(treename, filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above here as well. @dpiparo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same answer here.

@ellert
Copy link
Contributor Author

ellert commented Mar 19, 2019

You can see in the following pages that the images are missing:

https://root.cern/doc/v616/df003__profiles_8py.html
https://root.cern/doc/v616/df017__vecOpsHEP_8py.html

So the issue with the scope is not due to the proposed changes to support python 3.

@etejedor
Copy link
Contributor

Hi @ellert , thank you for the explanations! I would go ahead with it, but first I would add an explanation to the commit message about the change of scope when running with the makeimage.py script, which motivates the changes. The changes to the tutorials could even be in a separate commit that included also the explanation about the list comprehension in Python3.

When run inside the makeimage.py wrapper during doxygen generation the
variable scope is different than when the tutorials are run stand-alone.
The change in tutorials/math/tStudent.py is Python 3 specific and is due to
that list comprehensions in Python 3 are run in an implicit function scope.
@ellert
Copy link
Contributor Author

ellert commented Mar 21, 2019

I have split the PR into two commits, and extended the commit message for the commit that changes the tutorials.

@ellert
Copy link
Contributor Author

ellert commented Apr 8, 2019

Ping?

@couet
Copy link
Member

couet commented Apr 8, 2019

Hi Enric, is this PR ok for you on the python side ?

@etejedor
Copy link
Contributor

etejedor commented Apr 9, 2019

Yes, for me it's good to go.

@couet
Copy link
Member

couet commented Apr 9, 2019

Ok I merge

@couet couet merged commit 88262a6 into root-project:master Apr 9, 2019
@ellert ellert deleted the doxygen-with-python-3 branch April 9, 2019 09:44
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.

None yet

4 participants