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

feat: implement TGraph-writing #1256

Merged

Conversation

Pepesob
Copy link
Contributor

@Pepesob Pepesob commented Jul 24, 2024

Implemented function as_TGraph() that gives to user possibilty to save DataFrame (or any dict like object) as TGraph.
Example of usage:

import uproot
f = uproot.recreate("example.root")
x = np.array([-50,0,1, 2, 3, 4, 5, 17, 100,110], dtype='float64')
y = np.array([-50,0,1, 2, 3, 4, 5, 10, 100,120], dtype='float64')
d = {'x': x, 'y': y}
df = pd.DataFrame(d)
f["mytgraph"] = uproot.as_TGraph(df)
f.close()

List of arguments:


- df (DataFrame or and dict like object): DataFrame object with column names as follows:
     x (float): x values of TGraph.
     y (float): y values of TGraph.
     x_errors (float or left unspecified): Symethrical error values for corresponding x value
     y_errors (float or left unspecified): Symethrical error values for corresponding y value
     x_errors_low (float or left unspecified): Asymmetrical lower error values for corresponding x value
     x_errors_high (float or left unspecified): Asymmetrical upper error values for corresponding x value
     y_errors_low (float or left unspecified): Asymmetrical lower error values for corresponding y value
     y_errors_high (float or left unspecified): Asymmetrical upper error values for corresponding y value
     (other column names will be ignored!)
- title (str): Title of the histogram.
- xAxisLabel (str): Label of the X axis.
- yAxisLabel (str): Label of the Y axis.
- minY (None or float): Minimum value on the Y axis to be shown, if set to None then minY=min(y)
- maxY (None or float): Maximum value on the Y axis to be shown, if set to None then maxY=max(y)
- lineColor (int): Line color. (https://root.cern.ch/doc/master/classTAttLine.html)
- lineStyle (int): Line style.
- lineWidth (int): Line width.
- markerColor (int): Marker color. (https://root.cern.ch/doc/master/classTAttMarker.html)
- markerStyle (int): Marker style.
- markerSize (float): Marker size.

How does it work:

  Converts arguments into TGraph, TGraphErrors or TGraphAsymmErros based on the given arguments.
  When all errors are unspecified, detected object is TGraph.
  When x_errors, y_errors are specified, detected object is TGraphErrors.
  When x_errors_low, x_errors_high, y_errors_low, y_errors_high are specified, detected object is TGraphAsymmErrors.
  Note that both x_errors, y_errors need to be specified or set to None.
  The same rule applies to x_errors_low, x_errors_high, y_errors_low, y_errors_high.
  Also can't specify x_errors, y_errors and x_errors_low, x_errors_high, y_errors_low, y_errors_high at the same time.
  All rules are designed to remove any ambiguity.

Copy link
Member

@jpivarski jpivarski left a comment

Choose a reason for hiding this comment

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

I didn't carefully check all the cases as described in lines 246 through 252 of interpret.py (the docstring of to_TGraph). But it's good that there's an effective flow-chart/decision tree for these cases because the user will need to internalize it.

I only have a few code clean-up things below, and a likely solution to the test failures.

tests/test_1128_TGraph_writing.py Outdated Show resolved Hide resolved
tests/test_1128_TGraph_writing.py Outdated Show resolved Hide resolved
tests/test_1128_TGraph_writing.py Outdated Show resolved Hide resolved
tests/test_1128_TGraph_writing.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
@grzanka
Copy link

grzanka commented Jul 25, 2024

This is a nice design ! Can it be extended (maybe in future PR) to handle TMultiGraph as well ? I would like to save plots with different data series. Each serie would be separate collection of x,y pairs. Each serie may have different number of points.

@Pepesob Pepesob requested a review from jpivarski July 25, 2024 16:50
Copy link
Member

@jpivarski jpivarski left a comment

Choose a reason for hiding this comment

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

Very nice! I'll make the suggested changes (below) myself, and then merge this PR if the tests pass. Thank you very much!

@grzanka said,

This is a nice design ! Can it be extended (maybe in future PR) to handle TMultiGraph as well ? I would like to save plots with different data series. Each serie would be separate collection of x,y pairs. Each serie may have different number of points.

That sounds like a good future PR. Would that be similar enough to include in the as_TGraph function (which already covers three different classes), or would the interface be different enough to warrant a new function?

src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
src/uproot/writing/interpret.py Outdated Show resolved Hide resolved
@jpivarski jpivarski enabled auto-merge (squash) July 27, 2024 17:52
@jpivarski
Copy link
Member

@all-contributors please add @Pepesob for code

Copy link
Contributor

@jpivarski

I've put up a pull request to add @Pepesob! 🎉

@jpivarski jpivarski merged commit 90039ea into scikit-hep:main Jul 27, 2024
24 checks passed
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.

3 participants