From f94ce65f2b47c8d856a4e4d75ff781485111bfb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Bilodeau?= <70441641+zbilodea@users.noreply.github.com> Date: Mon, 25 Mar 2024 13:36:30 +0100 Subject: [PATCH] fix: some tests missing tmp_path argument/implementation (#85) * all tests should use tmp_path, pyroot use should still work. * formatting... --- tests/test_add_histograms.py | 246 ++++++++++++++++++---------------- tests/test_parquet_to_root.py | 18 ++- 2 files changed, 139 insertions(+), 125 deletions(-) diff --git a/tests/test_add_histograms.py b/tests/test_add_histograms.py index 5cf8732..01cdedf 100644 --- a/tests/test_add_histograms.py +++ b/tests/test_add_histograms.py @@ -1,6 +1,6 @@ from __future__ import annotations -from pathlib import Path +import os import numpy as np import pytest @@ -10,80 +10,15 @@ ROOT = pytest.importorskip("ROOT") +# ruff: noqa: PTH118 -def write_root_file(hist, path): - outHistFile = ROOT.TFile.Open(path, "RECREATE") - outHistFile.cd() - hist.Write() - outHistFile.Close() - - -def generate_1D_gaussian(): - gauss_1 = ROOT.TH1I("name", "title", 5, -4, 4) - gauss_1.FillRandom("gaus") - gauss_1.Sumw2() - gauss_1.SetDirectory(0) - outHistFile = ROOT.TFile.Open("/hepconvert/tests/samples/hist1.root", "RECREATE") - outHistFile.cd() - gauss_1.Write() - outHistFile.Close() - gauss_1 = uproot.from_pyroot(gauss_1) - - gauss_2 = ROOT.TH1I("name", "title", 5, -4, 4) - gauss_2.FillRandom("gaus") - gauss_2.Sumw2() - gauss_2.SetDirectory(0) - outHistFile = ROOT.TFile.Open("hepconvert/tests/samples/hist2.root", "RECREATE") - outHistFile.cd() - gauss_2.Write() - outHistFile.Close() - gauss_2 = uproot.from_pyroot(gauss_2) - - gauss_3 = ROOT.TH1I("name", "title", 5, -4, 4) - gauss_3.FillRandom("gaus") - gauss_3.Sumw2() - gauss_3.SetDirectory(0) - outHistFile = ROOT.TFile.Open("hepconvert/tests/samples/hist3.root", "RECREATE") - outHistFile.cd() - gauss_3.Write() - outHistFile.Close() - gauss_3 = uproot.from_pyroot(gauss_3) - - return gauss_1, gauss_2, gauss_3 - - -def generate_1D_simple(): - h1 = ROOT.TH1F("name", "", 10, 0.0, 10.0) - data1 = [11.5, 12.0, 9.0, 8.1, 6.4, 6.32, 5.3, 3.0, 2.0, 1.0] - for i in range(len(data1)): - h1.Fill(i, data1[i]) - - outHistFile = ROOT.TFile.Open("hepconvert/tests/samples/file1dim1.root", "RECREATE") - outHistFile.cd() - h1.Write() - outHistFile.Close() - h1 = uproot.from_pyroot(h1) - h2 = ROOT.TH1F("name", "", 10, 0.0, 10.0) - data2 = [21.5, 10.0, 9.0, 8.2, 6.8, 6.32, 5.3, 3.0, 2.0, 1.0] - - for i in range(len(data2)): - h2.Fill(i, data2[i]) - - outHistFile = ROOT.TFile.Open("tests/file2dim1.root", "RECREATE") - outHistFile.cd() - h2.Write() - outHistFile.Close() - h2 = uproot.from_pyroot(h2) - return h1, h2 - - -def test_simple(tmp_path, file_paths): +def test_simple(tmp_path): gauss_1 = ROOT.TH1I("name", "title", 5, -4, 4) gauss_1.FillRandom("gaus") gauss_1.Sumw2() gauss_1.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[0], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file1.root"), "RECREATE") outHistFile.cd() gauss_1.Write() outHistFile.Close() @@ -93,7 +28,7 @@ def test_simple(tmp_path, file_paths): gauss_2.FillRandom("gaus") gauss_2.Sumw2() gauss_2.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[1], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file2.root"), "RECREATE") outHistFile.cd() gauss_2.Write() outHistFile.Close() @@ -103,15 +38,23 @@ def test_simple(tmp_path, file_paths): gauss_3.FillRandom("gaus") gauss_3.Sumw2() gauss_3.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[2], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file3.root"), "RECREATE") outHistFile.cd() gauss_3.Write() outHistFile.Close() h3 = uproot.from_pyroot(gauss_3) - path = Path(tmp_path) - destination = path / "destination.root" - hepconvert.add_histograms(destination, file_paths, force=True, progress_bar=True) + destination = os.path.join(tmp_path, "destination.root") + hepconvert.add_histograms( + destination, + [ + os.path.join(tmp_path, "file1.root"), + os.path.join(tmp_path, "file2.root"), + os.path.join(tmp_path, "file3.root"), + ], + force=True, + progress_bar=True, + ) with uproot.open(destination) as file: added = uproot.from_pyroot( gauss_1 + gauss_2 + gauss_3 @@ -128,12 +71,12 @@ def test_simple(tmp_path, file_paths): ).all -def mult_1D(tmp_path, file_paths): +def mult_1D(tmp_path): gauss_1 = ROOT.TH1I("name1", "title", 5, -4, 4) gauss_1.FillRandom("gaus") gauss_1.Sumw2() gauss_1.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[0], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file1.root"), "RECREATE") outHistFile.cd() gauss_1.Write() outHistFile.Close() @@ -143,7 +86,7 @@ def mult_1D(tmp_path, file_paths): gauss_2.FillRandom("gaus") gauss_2.Sumw2() gauss_2.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[0], "UPDATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file1.root"), "UPDATE") outHistFile.cd() gauss_2.Write() outHistFile.Close() @@ -153,7 +96,7 @@ def mult_1D(tmp_path, file_paths): gauss_3.FillRandom("gaus") gauss_3.Sumw2() gauss_3.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[1], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file2.root"), "RECREATE") outHistFile.cd() gauss_3.Write() outHistFile.Close() @@ -163,7 +106,7 @@ def mult_1D(tmp_path, file_paths): gauss_4.FillRandom("gaus") gauss_4.Sumw2() gauss_4.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[1], "UPDATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file2.root"), "UPDATE") outHistFile.cd() gauss_4.Write() outHistFile.Close() @@ -173,7 +116,7 @@ def mult_1D(tmp_path, file_paths): gauss_5.FillRandom("gaus") gauss_5.Sumw2() gauss_5.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[2], "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file3.root"), "RECREATE") outHistFile.cd() gauss_5.Write() outHistFile.Close() @@ -183,15 +126,23 @@ def mult_1D(tmp_path, file_paths): gauss_6.FillRandom("gaus") gauss_6.Sumw2() gauss_6.SetDirectory(0) - outHistFile = ROOT.TFile.Open(file_paths[2], "UPDATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file3.root"), "UPDATE") outHistFile.cd() gauss_6.Write() outHistFile.Close() h6 = uproot.from_pyroot(gauss_6) - path = Path(tmp_path) - destination = path / "destination.root" - hepconvert.add_histograms(destination, file_paths, force=True, same_names=False) + destination = os.path.join(tmp_path, "destination.root") + hepconvert.add_histograms( + destination, + [ + os.path.join(tmp_path, "file1.root"), + os.path.join(tmp_path, "file2.root"), + os.path.join(tmp_path, "file3.root"), + ], + force=True, + same_names=False, + ) with uproot.open(destination) as file: added = uproot.from_pyroot( @@ -214,11 +165,41 @@ def mult_1D(tmp_path, file_paths): ) + h6.member("fTsumw") -def test_3_glob(file_paths, tmp_path): - h1, h2, h3 = generate_1D_gaussian(file_paths) +def test_3_glob(tmp_path): + gauss_1 = ROOT.TH1I("name", "title", 5, -4, 4) + gauss_1.FillRandom("gaus") + gauss_1.Sumw2() + gauss_1.SetDirectory(0) + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "hist1.root"), "RECREATE") + outHistFile.cd() + gauss_1.Write() + outHistFile.Close() + h1 = uproot.from_pyroot(gauss_1) + + gauss_2 = ROOT.TH1I("name", "title", 5, -4, 4) + gauss_2.FillRandom("gaus") + gauss_2.Sumw2() + gauss_2.SetDirectory(0) + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "hist2.root"), "RECREATE") + outHistFile.cd() + gauss_2.Write() + outHistFile.Close() + h2 = uproot.from_pyroot(gauss_2) + + gauss_3 = ROOT.TH1I("name", "title", 5, -4, 4) + gauss_3.FillRandom("gaus") + gauss_3.Sumw2() + gauss_3.SetDirectory(0) + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "hist3.root"), "RECREATE") + outHistFile.cd() + gauss_3.Write() + outHistFile.Close() + h3 = uproot.from_pyroot(gauss_3) hepconvert.add_histograms( - Path(tmp_path) / "place.root", Path(tmp_path) / "samples", force=True + os.path.join(tmp_path, "place.root"), + os.path.join(tmp_path, "samples"), + force=True, ) with uproot.open("tests/place.root") as file: @@ -235,14 +216,39 @@ def test_3_glob(file_paths, tmp_path): def simple_1dim_F(tmp_path): - h1, h2 = generate_1D_simple() + h1 = ROOT.TH1F("name", "", 10, 0.0, 10.0) + data1 = [11.5, 12.0, 9.0, 8.1, 6.4, 6.32, 5.3, 3.0, 2.0, 1.0] + for i in range(len(data1)): + h1.Fill(i, data1[i]) + + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file1dim1.root"), "RECREATE") + outHistFile.cd() + h1.Write() + outHistFile.Close() + h1 = uproot.from_pyroot(h1) + + h2 = ROOT.TH1F("name", "", 10, 0.0, 10.0) + data2 = [21.5, 10.0, 9.0, 8.2, 6.8, 6.32, 5.3, 3.0, 2.0, 1.0] + + for i in range(len(data2)): + h2.Fill(i, data2[i]) + + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file2dim1.root"), "RECREATE") + outHistFile.cd() + h2.Write() + outHistFile.Close() + h2 = uproot.from_pyroot(h2) + hepconvert.add_histograms( - Path(tmp_path) / "place2.root", - [Path(tmp_path) / "file1dim1.root", Path(tmp_path) / "file2dim1.root"], + os.path.join(tmp_path, "place2.root"), + [ + os.path.join(tmp_path, "file1dim1.root"), + os.path.join(tmp_path, "file2dim1.root"), + ], force=True, ) - with uproot.open(Path(tmp_path) / "place2.root") as file: + with uproot.open(os.path.join(tmp_path, "place2.root")) as file: assert file["name"].member("fN") == h1.member("fN") assert file["name"].member("fTsumw") == h1.member("fTsumw") + h2.member( "fTsumw" @@ -285,7 +291,7 @@ def mult_2D_hists(tmp_path): for j in range(len(data1[0])): h1.Fill(i, j, data1[i][j]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "file3dim2.root", "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file3dim2.root"), "RECREATE") outHistFile.cd() h1.Write() outHistFile.Close() @@ -307,7 +313,7 @@ def mult_2D_hists(tmp_path): for j in range(len(data2[0])): h2.Fill(i, j, data2[i][j]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "file3dim2.root", "UPDATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file3dim2.root"), "UPDATE") outHistFile.cd() h2.Write() outHistFile.Close() @@ -328,7 +334,7 @@ def mult_2D_hists(tmp_path): for j in range(len(data3[0])): h3.Fill(i, j, data3[i][j]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "file4dim2.root", "RECREATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file4dim2.root"), "RECREATE") outHistFile.cd() h3.Write() outHistFile.Close() @@ -350,19 +356,22 @@ def mult_2D_hists(tmp_path): for j in range(len(data4[0])): h4.Fill(i, j, data4[i][j]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "file4dim2.root", "UPDATE") + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file4dim2.root"), "UPDATE") outHistFile.cd() h4.Write() outHistFile.Close() h4 = uproot.from_pyroot(h4) hepconvert.add_histograms( - Path(tmp_path) / "place2.root", - [Path(tmp_path) / "file3dim2.root", Path(tmp_path) / "file4dim2.root"], + os.path.join(tmp_path, "place2.root"), + [ + os.path.join(tmp_path, "file3dim2.root"), + os.path.join(tmp_path, "file4dim2.root"), + ], force=True, ) - with uproot.open(Path(tmp_path) / "place2.root") as file: + with uproot.open(os.path.join(tmp_path, "place2.root")) as file: assert file["name"].member("fN") == h1.member("fN") assert file["name"].member("fTsumw") == h1.member("fTsumw") + h3.member( "fTsumw" @@ -447,8 +456,11 @@ def simple_2dim_F(tmp_path): h2 = uproot.from_pyroot(h2) hepconvert.add_histograms( - Path(tmp_path) / "tests/place2.root", - [Path(tmp_path) / "/file1dim2.root", Path(tmp_path) / "/file2dim2.root"], + os.path.join(tmp_path, "place2.root"), + [ + os.path.join(tmp_path, "file1dim2.root"), + os.path.join(tmp_path, "file2dim2.root"), + ], force=True, ) @@ -479,7 +491,7 @@ def simple_2dim_F(tmp_path): ).all -def simple_2D(): +def simple_2D(tmp_path): h2 = ROOT.TH2F("name", "", 10, 0.0, 10.0, 8, 0.0, 8.0) data2 = [ [21.5, 10.0, 9.0, 8.2, 6.8, 6.32, 5.3, 3.0, 2.0, 1.0], @@ -495,9 +507,7 @@ def simple_2D(): for i in range(len(data2)): for j in range(len(data2[0])): h2.Fill(i, j, data2[i][j]) - outHistFile = ROOT.TFile.Open( - "/Users/zobil/Documents/hepconvert/tests/samples/file2dim2.root", "RECREATE" - ) + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file2dim2.root"), "RECREATE") outHistFile.cd() h2.Write() outHistFile.Close() @@ -518,26 +528,22 @@ def simple_2D(): for j in range(len(data1[0])): h1.Fill(i, j, data1[i][j]) - outHistFile = ROOT.TFile.Open( - "/Users/zobil/Documents/hepconvert/tests/samples/file1dim2.root", "RECREATE" - ) + outHistFile = ROOT.TFile.Open(os.path.join(tmp_path, "file1dim2.root"), "RECREATE") outHistFile.cd() h1.Write() outHistFile.Close() h1 = uproot.from_pyroot(h1) hepconvert.add_histograms( - "/Users/zobil/Documents/hepconvert/tests/samples/place2.root", + os.path.join(tmp_path, "place2.root"), [ - "/Users/zobil/Documents/hepconvert/tests/samples/file1dim2.root", - "/Users/zobil/Documents/hepconvert/tests/samples/file2dim2.root", + os.path.join(tmp_path, "file1dim2.root"), + os.path.join(tmp_path, "file2dim2.root"), ], force=True, ) - with uproot.open( - "/Users/zobil/Documents/hepconvert/tests/samples/place2.root" - ) as file: + with uproot.open(os.path.join(tmp_path, "place2.root")) as file: assert file["name"].member("fN") == h1.member("fN") assert file["name"].member("fTsumw") == h1.member("fTsumw") + h2.member( "fTsumw" @@ -570,7 +576,9 @@ def break_bins(tmp_path): for i in range(len(data1)): h1.Fill(i, data1[i]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "/file1dim1break.root", "RECREATE") + outHistFile = ROOT.TFile.Open( + os.path.join(tmp_path, "file1dim1break.root"), "RECREATE" + ) outHistFile.cd() h1.Write() outHistFile.Close() @@ -582,17 +590,19 @@ def break_bins(tmp_path): for i in range(len(data2)): h2.Fill(i, data2[i]) - outHistFile = ROOT.TFile.Open(Path(tmp_path) / "/file2dim1break.root", "RECREATE") + outHistFile = ROOT.TFile.Open( + os.path.join(tmp_path, "file2dim1break.root"), "RECREATE" + ) outHistFile.cd() h2.Write() outHistFile.Close() h2 = uproot.from_pyroot(h2) hepconvert.add_histograms( - Path(tmp_path) / "/place2break.root", + os.path.join(tmp_path, "place2break.root"), [ - Path(tmp_path) / "/file1dim1break.root", - Path(tmp_path) / "/file2dim1break.root", + os.path.join(tmp_path, "file1dim1break.root"), + os.path.join(tmp_path, "file2dim1break.root"), ], force=True, ) diff --git a/tests/test_parquet_to_root.py b/tests/test_parquet_to_root.py index 7984e2a..ea29180 100644 --- a/tests/test_parquet_to_root.py +++ b/tests/test_parquet_to_root.py @@ -1,5 +1,7 @@ from __future__ import annotations +from pathlib import Path + import awkward as ak import pytest import uproot @@ -9,16 +11,18 @@ skhep_testdata = pytest.importorskip("skhep_testdata") -def test_hepdata(): +def test_hepdata(tmp_path): arrays = uproot.open(skhep_testdata.data_path("uproot-hepdata-example.root"))[ "ntuple;1" ].arrays() - ak.to_parquet(arrays, "uproot-hepdata-example.parquet") + ak.to_parquet(arrays, Path(tmp_path) / "uproot-hepdata-example.parquet") parquet_to_root( - "uproot-hepdata-example.root", "uproot-hepdata-example.parquet", name="ntuple" + Path(tmp_path) / "uproot-hepdata-example.root", + Path(tmp_path) / "uproot-hepdata-example.parquet", + name="ntuple", ) - test = uproot.open("uproot-hepdata-example.root") + test = uproot.open(Path(tmp_path) / "uproot-hepdata-example.root") original = uproot.open(skhep_testdata.data_path("uproot-hepdata-example.root")) for key in original["ntuple"].keys(): @@ -29,7 +33,7 @@ def test_hepdata(): assert ak.all(test["ntuple"].arrays()[key] == original["ntuple"].arrays()[key]) -def test_hzz(): +def test_hzz(tmp_path): file = uproot.open(skhep_testdata.data_path("uproot-HZZ.root")) tree = file["events"] @@ -81,13 +85,13 @@ def test_hzz(): record = ak.Record(chunks) ak.to_parquet(record, "uproot-HZZ.parquet") parquet_to_root( - "tests/samples/parquet_HZZ.root", + Path(tmp_path) / "parquet_HZZ.root", "uproot-HZZ.parquet", name="events", progress_bar=True, counter_name=lambda counted: "N" + counted, ) - test = uproot.open("tests/samples/parquet_HZZ.root") + test = uproot.open(Path(tmp_path) / "parquet_HZZ.root") original = uproot.open(skhep_testdata.data_path("uproot-HZZ.root")) for key in original["events"].keys():