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
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
37c5052
feat: implement TGraph-writing
jpivarski Feb 23, 2024
403232a
style: pre-commit fixes
pre-commit-ci[bot] Feb 23, 2024
3fe27cd
feat: implemented as_TGraph funciton
Pepesob Jul 23, 2024
2fb7e27
to_TGraph() checkpoint
Pepesob Jul 24, 2024
017a284
to_TGraph() checkpoint
Pepesob Jul 24, 2024
549cece
to_TGraph() checkpoint
Pepesob Jul 24, 2024
9744dee
Functionality implemented, basic tests done
Pepesob Jul 24, 2024
288d98f
changed to_TGraph function placement
Pepesob Jul 24, 2024
e889b16
By default line of the graph won't touch the edge of the chart
Pepesob Jul 24, 2024
7037230
Removed my script from tracking
Pepesob Jul 24, 2024
181b9eb
Handle 0 length arrays
Pepesob Jul 24, 2024
013e889
Test saving TGraph to .root file and test reading with ROOT
Pepesob Jul 24, 2024
fb87ecc
style: pre-commit fixes
pre-commit-ci[bot] Jul 24, 2024
4af97ae
fix: test failing because of imports and added license
Pepesob Jul 25, 2024
0b7b3c7
change: using pytest.approx() to compare floats
Pepesob Jul 25, 2024
581950a
change: remove unused epsilon value
Pepesob Jul 25, 2024
c0c0a74
style: pre-commit fixes
pre-commit-ci[bot] Jul 25, 2024
eaf2487
Update tests/test_1128_TGraph_writing.py
Pepesob Jul 25, 2024
5d760ab
Change: changed error message
Pepesob Jul 25, 2024
514c50d
Change: change error message
Pepesob Jul 25, 2024
4eec10d
Change: change error message
Pepesob Jul 25, 2024
b9ae449
Change: change error message
Pepesob Jul 25, 2024
597e532
change: using np.min/np.max instead of builtin min/max
Pepesob Jul 25, 2024
88c9425
style: pre-commit fixes
pre-commit-ci[bot] Jul 25, 2024
2534c92
change: check if minY, maxY is instance of numbers.Real
Pepesob Jul 25, 2024
621e60a
Merge branch 'pepesob/implement-TGraph-interpretation' of https://git…
Pepesob Jul 25, 2024
2c95999
style: pre-commit fixes
pre-commit-ci[bot] Jul 25, 2024
094e7b3
change: changed function name from to_TGraph() to as_TGraph (better r…
Pepesob Jul 25, 2024
81443a3
Merge
Pepesob Jul 25, 2024
d3d47f9
Change: improve error messages
Pepesob Jul 25, 2024
7eeb162
Merge branch 'main' into pepesob/implement-TGraph-interpretation
Pepesob Jul 25, 2024
a5c9212
Fixed error messages
Pepesob Jul 26, 2024
b5bba5a
edit error messages for consistency and clarity
jpivarski Jul 27, 2024
28aeb53
style: pre-commit fixes
pre-commit-ci[bot] Jul 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uproot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
from uproot.writing import to_writable
from uproot.writing import dask_write

from uproot.writing.interpret import to_TGraph
from uproot.writing.interpret import as_TGraph

import uproot.models.TObject
import uproot.models.TString
Expand Down
48 changes: 25 additions & 23 deletions src/uproot/writing/interpret.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import numpy
import numbers

import numpy as np

import uproot


def _to_TGraph(
def _as_TGraph(
x,
y,
x_errors=None,
Expand Down Expand Up @@ -75,21 +77,21 @@ def _to_TGraph(
# Detecting which type of TGraph to chose
if any(sym_errors_bool):
if not all(sym_errors_bool):
raise ValueError("Have to specify both x_errors and y_errors")
raise ValueError("uproot.as_TGraph requires both x_errors and y_errors")
if any(asym_errors_bool):
raise ValueError(
"Can's specify both symetrical errors and asymetrical errors"
"uproot.as_TGraph can accept symmetrical errors OR asymmetrical errors, but not both"
)
tgraph_type = "TGraphErrors"

elif any(asym_errors_bool):
if not all(asym_errors_bool):
raise ValueError(
"Have to specify all: x_errors_low, x_errors_high, y_errors_low, y_errors_high"
"uproot.as_TGraph requires all of the following: x_errors_low, x_errors_high, y_errors_low, y_errors_high"
)
if any(sym_errors_bool):
raise ValueError(
"Can's specify both symetrical errors and asymetrical errors"
"uproot.as_TGraph can accept symmetrical errors OR asymmetrical errors, but not both"
)
tgraph_type = "TGraphAsymmErrors"

Expand Down Expand Up @@ -130,23 +132,23 @@ def _to_TGraph(
if len(x) != len(y):
raise ValueError("Arrays x and y must have the same length!")
if len(x) == 0:
raise ValueError("Arguments and values arrays can't be emty")
raise ValueError("x and Y arrays can't be emty")
jpivarski marked this conversation as resolved.
Show resolved Hide resolved
if len(x.shape) != 1:
raise ValueError(f"x has to be 1D, but is {len(x.shape)}D!")
if len(y.shape) != 1:
raise ValueError(f"y has to be 1D, but is {len(y.shape)}D!")

if minY is None:
new_minY = min(x, default=0)
elif not isinstance(minY, (int, float)):
raise ValueError(f"fMinium has to be None or a number! But is {type(minY)}")
new_minY = np.min(x)
elif not isinstance(minY, numbers.Real):
raise ValueError(f"minY has to be None or a number! But is {type(minY)}")
jpivarski marked this conversation as resolved.
Show resolved Hide resolved
else:
new_minY = minY

if maxY is None:
new_maxY = max(x, default=0)
elif not isinstance(maxY, (int, float)):
raise ValueError(f"fMinium has to be None or a number! But is {type(maxY)}")
new_maxY = np.max(x)
elif not isinstance(maxY, numbers.Real):
raise ValueError(f"minY has to be None or a number! But is {type(maxY)}")
jpivarski marked this conversation as resolved.
Show resolved Hide resolved
else:
new_maxY = maxY

Expand Down Expand Up @@ -203,7 +205,7 @@ def _to_TGraph(
return returned_TGraph


def to_TGraph(
def as_TGraph(
df,
title="",
xAxisLabel="",
Expand Down Expand Up @@ -252,36 +254,36 @@ def to_TGraph(
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.
"""

x = numpy.array(df["x"]) if df.get("x", None) is not None else None
y = numpy.array(df["y"]) if df.get("y", None) is not None else None
x = np.array(df["x"]) if df.get("x", None) is not None else None
y = np.array(df["y"]) if df.get("y", None) is not None else None
x_errors = (
numpy.array(df["x_errors"]) if df.get("x_errors", None) is not None else None
np.array(df["x_errors"]) if df.get("x_errors", None) is not None else None
)
y_errors = (
numpy.array(df["y_errors"]) if df.get("y_errors", None) is not None else None
np.array(df["y_errors"]) if df.get("y_errors", None) is not None else None
)
x_errors_low = (
numpy.array(df["x_errors_low"])
np.array(df["x_errors_low"])
if df.get("x_errors_low", None) is not None
else None
)
x_errors_high = (
numpy.array(df["x_errors_high"])
np.array(df["x_errors_high"])
if df.get("x_errors_high", None) is not None
else None
)
y_errors_low = (
numpy.array(df["y_errors_low"])
np.array(df["y_errors_low"])
if df.get("y_errors_low", None) is not None
else None
)
y_errors_high = (
numpy.array(df["y_errors_high"])
np.array(df["y_errors_high"])
if df.get("y_errors_high", None) is not None
else None
)

return _to_TGraph(
return _as_TGraph(
x,
y,
x_errors,
Expand Down
23 changes: 13 additions & 10 deletions tests/test_1128_TGraph_writing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import uproot
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

import os
import pandas as pd

import pytest
import ROOT
import numpy as np

EPS = 1e-6
import uproot

pd = pytest.importorskip("pandas")
ROOT = pytest.importorskip("ROOT")


def test_saving_TGraph_to_file(tmp_path):
Expand All @@ -15,7 +18,7 @@ def test_saving_TGraph_to_file(tmp_path):
df = pd.DataFrame({"x": x, "y": y})

with uproot.recreate(newfile) as f:
f["myTGraph"] = uproot.to_TGraph(df)
f["myTGraph"] = uproot.as_TGraph(df)

with uproot.open(newfile) as f:
tgraph = f["myTGraph"]
Expand All @@ -24,8 +27,8 @@ def test_saving_TGraph_to_file(tmp_path):
y_new = tgraph.values("y")

for i in range(len(x)):
assert abs(x_new[i] - x[i]) < EPS
assert abs(y_new[i] - y[i]) < EPS
assert x_new[i] == pytest.approx(x[i])
assert y_new[i] == pytest.approx(y[i])


def test_opening_TGraph_with_root(tmp_path):
Expand All @@ -39,7 +42,7 @@ def test_opening_TGraph_with_root(tmp_path):
yLabel = "yLabel"

with uproot.recreate(newfile) as f:
f[tGraphName] = uproot.to_TGraph(
f[tGraphName] = uproot.as_TGraph(
df, title=title, xAxisLabel=xLabel, yAxisLabel=yLabel
)

Expand All @@ -53,5 +56,5 @@ def test_opening_TGraph_with_root(tmp_path):
for i in range(len(x)):
xAxis = tgraph.GetX()
yAxis = tgraph.GetY()
assert abs(xAxis[i] - x[i]) < EPS
assert abs(yAxis[i] - y[i]) < EPS
assert xAxis[i] == pytest.approx(x[i])
assert yAxis[i] == pytest.approx(y[i])
Loading