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

[GSoC] Hist and TGraph Pythonizations. #11357

Closed
wants to merge 2 commits into from
Closed

[GSoC] Hist and TGraph Pythonizations. #11357

wants to merge 2 commits into from

Conversation

harshal0815
Copy link
Contributor

This Pull request:

Changes or fixes:

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

This PR fixes #

@phsft-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@harshal0815
Copy link
Contributor Author

harshal0815 commented Sep 22, 2022

@etejedor Sir,
Can you kindly review this PR. Also, there seems to be an issue with the TGraph constructor pythoniztion and the GetAsNumpy methods. Please post any changes or additions I can make. I will update the PR soon accordingly based on your inputs and suggestions.
Thank you.

Here's a link to the github gist for your reference.

@etejedor
Copy link
Contributor

Hello @Harshalzzzzzzz

Also, there seems to be an issue with the TGraph constructor pythoniztion and the GetAsNumpy methods

What is exactly the problem?

@harshal0815
Copy link
Contributor Author

harshal0815 commented Sep 22, 2022

TGraph Constructor returns a null value
@etejedor

@etejedor
Copy link
Contributor

TGraph Constructor returns a null value

Please see my comments inline in the code about this.

@harshal0815
Copy link
Contributor Author

@etejedor Can you also please review the GetAsNumpy method, so that I can resolve the errors

@etejedor
Copy link
Contributor

@etejedor Can you also please review the GetAsNumpy method, so that I can resolve the errors

What kind of errors do you get?

@harshal0815
Copy link
Contributor Author

harshal0815 commented Sep 27, 2022

What kind of errors do you get?

@etejedor Thank you sir, both errors are resolved, Kindly review the PR if possible.
Regards.

@harshal0815
Copy link
Contributor Author

@lmoneta @sanjibansg @omazapa
PR updated. Kindly review, I will update the fixes/changes, if any, asap.

Thanks.

Comment on lines 43 to 60
def _GetBin(self, *args):
a = _numpy_content(self._Original_GetBin(*args), *args)
return a


def _GetBinCenter(self, *args):
a = _numpy_content(self._Original_GetBinCenter(*args), *args)
return a


def _GetBinContent(self, *args):
a = _numpy_content(self._Original_GetBinContent(*args), *args)
return a


def _GetBinLowEdge(self, *args):
a = _numpy_content(self._Original_GetBinLowEdge(*args), *args)
return a
Copy link
Contributor

@guitargeek guitargeek Oct 3, 2022

Choose a reason for hiding this comment

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

I don't see the point of these Pythonizations of GetBin, GetBinCenter, GetBinContent, and GetBinLowEdge. All the original functions return a scalar, and in this case the _numpy_context function just returns the scalar you pass to it.

Can you please elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the TH2 and TH3 functions

Copy link
Contributor

Choose a reason for hiding this comment

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

The bin contents are stored in the fArray data member of the TH1 (here the header file for reference).

To implement GetBinContent in an efficient way for the full array, you would have access this fArray member and create a numpy array from it with frombuffer. However, I think this member is not public. Maybe we can add a public accessor to TH1, or what would you do here @lmoneta?

Copy link
Contributor

Choose a reason for hiding this comment

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

For the bin centers and edges, it's easier. You can access the array of bin edges with public getters from the TAxis instances in the histogram, which you can for example get like hist.GetXaxis(). Then you got to study the TAxis class a bit to understand how to retrieve bin boundaries best in NumPy.

I guess if you have non, uniform binning, you can get the full bin edges array with hist.GetXaxis().GetXbins().GetArray(). For uniform bins, you can just create a np.linspace based on the limits and the number of bins

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@guitargeek Sir when I try to use something like hist.GetSumw2() to convert this I get segmentation error, when I try to return the bin sum of weight square while implementing GetContent()

Link to updated gist.

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.

I have a few technical suggestions, and a question about what these GetBinContent et al. pythonizations actually do (please clarify).

Also, I'm not not convinced by the approach to the FromNumpy function. If you want to support filling of histograms from NumPy arrays, why not Pythonize Fill() and FillN(), which is probably the first thing people will try out intuitively? And once that exists, I'm nut sure if a FromNumpy function will add much more value to the pythonizations, if most arguments are just forwarded to the constructor anyway, and the additional NumPy arguments could be passed to fill.

Something to discuss also with @lmoneta.

@lmoneta
Copy link
Member

lmoneta commented Oct 3, 2022

@guitargeek We have already FillN to fill a numpy array in an histogram.
I think is good to have a function creating and filling histogram directly from Numpy passing the array and the other c'tor arguments (number of bins, min and max), or just number of bins and compute min and max automatically.

@guitargeek
Copy link
Contributor

Okay! Having FromNumpy then makes sense, it just needs some proper docstring

@lgtm-com
Copy link

lgtm-com bot commented Oct 9, 2022

This pull request introduces 4 alerts when merging cb774a4 into 2441d13 - view on LGTM.com

new alerts:

  • 2 for Unused import
  • 2 for Module is imported with 'import' and 'import from'

@lgtm-com
Copy link

lgtm-com bot commented Oct 14, 2022

This pull request introduces 4 alerts when merging 3285d60 into b7a3cb5 - view on LGTM.com

new alerts:

  • 3 for Unused import
  • 1 for Wrong number of arguments in a call

@lgtm-com
Copy link

lgtm-com bot commented Oct 17, 2022

This pull request introduces 4 alerts when merging 538d1cf into 97802b9 - view on LGTM.com

new alerts:

  • 3 for Unused import
  • 1 for Wrong number of arguments in a call

@lgtm-com
Copy link

lgtm-com bot commented Oct 20, 2022

This pull request introduces 1 alert when merging b5f4c78 into e7c7170 - view on LGTM.com

new alerts:

  • 1 for Wrong number of arguments in a call

@harshal0815 harshal0815 closed this by deleting the head repository Nov 24, 2022
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.

5 participants