|
| 1 | +# Copyright (c) 2025, IRIS-HEP |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright notice, this |
| 8 | +# list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the name of the copyright holder nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +import pytest |
| 29 | +import uproot |
| 30 | +import awkward as ak |
| 31 | +import dask_awkward as dak |
| 32 | +import logging |
| 33 | +import os |
| 34 | +import sys |
| 35 | +import numpy as np |
| 36 | + |
| 37 | +#Setting rpath |
| 38 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 39 | +from servicex_analysis_utils.materialization import to_awk |
| 40 | + |
| 41 | +def build_test_samples(): |
| 42 | + # example data for two branches |
| 43 | + tree_data1 = { |
| 44 | + "branch1": np.ones(100), |
| 45 | + "branch2": np.zeros(100) |
| 46 | + } |
| 47 | + # example data for one branch |
| 48 | + tree_data2 = {"branch1": np.ones(10)} |
| 49 | + |
| 50 | + # Create tmp .root files |
| 51 | + with uproot.create(test_path1) as file: |
| 52 | + file["Tree"] = tree_data1 |
| 53 | + |
| 54 | + with uproot.create(test_path2) as file: |
| 55 | + file["Tree"] = tree_data2 |
| 56 | + |
| 57 | +#Initial test configuration |
| 58 | +@pytest.fixture(scope="function", autouse=True) |
| 59 | +def init(tmp_path): |
| 60 | + #Setting global variables to be used in the tests and helper function |
| 61 | + global test_path1, test_path2, \ |
| 62 | + result, result_da, result_filtered |
| 63 | + |
| 64 | + test_path1 = tmp_path / "test_file1.root" |
| 65 | + test_path2 = tmp_path / "test_file2.root" |
| 66 | + |
| 67 | + #Building dumy test files |
| 68 | + if not os.path.exists(test_path1) or not os.path.exists(test_path2): |
| 69 | + build_test_samples() |
| 70 | + |
| 71 | + #Dict like servicex.deliver() output |
| 72 | + sx_dict = {"Test-Sample1": test_path1, "Test-Sample2": test_path2} |
| 73 | + |
| 74 | + #Executing to_awk() and saving results for tests |
| 75 | + result = to_awk(sx_dict) |
| 76 | + result_da = to_awk(sx_dict, dask=True, step_size=10) #uproot.dask step_size kwarg |
| 77 | + result_filtered = to_awk(sx_dict, expressions="branch1") #uproot.iterate expressions kwarg |
| 78 | + |
| 79 | +#Test functions |
| 80 | +def test_to_awk_instances(): |
| 81 | + arr1=result["Test-Sample1"] |
| 82 | + da_arr1=result_da["Test-Sample1"] |
| 83 | + |
| 84 | + #Testing returned types |
| 85 | + assert isinstance(arr1, ak.Array), "to_awk() does not produce an awkward.Array instance" |
| 86 | + assert isinstance(da_arr1, dak.Array), "to_awk(dask=True) does not produce a dask_awkward.Array instance" |
| 87 | + |
| 88 | +def test_to_awk_collection(): |
| 89 | + arr1=result["Test-Sample1"] |
| 90 | + arr2=result["Test-Sample2"] |
| 91 | + |
| 92 | + #Collecting all samples |
| 93 | + assert list(result.keys())==["Test-Sample1", "Test-Sample2"] |
| 94 | + |
| 95 | + #Collecting all branches |
| 96 | + assert ak.fields(arr1) == ['branch1', 'branch2'] |
| 97 | + assert ak.fields(arr2) == ['branch1'] |
| 98 | + |
| 99 | + #Collecting all elements per branch |
| 100 | + assert ak.all(arr1['branch2'] == ak.from_numpy(np.zeros(100))) |
| 101 | + assert ak.all(arr2['branch1'] == ak.from_numpy(np.ones(10))) |
| 102 | + |
| 103 | +def test_to_awk_dask(): |
| 104 | + arr1=result_da["Test-Sample1"] |
| 105 | + arr2=result_da["Test-Sample2"] |
| 106 | + |
| 107 | + #Testing if dask.compute() leads to same results |
| 108 | + assert ak.almost_equal(arr1.compute(), result["Test-Sample1"]) |
| 109 | + assert ak.almost_equal(arr2.compute(), result["Test-Sample2"]) |
| 110 | + |
| 111 | + #Testing partitionning kwarg |
| 112 | + assert arr1.npartitions == 10 |
| 113 | + assert arr2.npartitions == 1 |
| 114 | + |
| 115 | +def test_to_awk_filter(): |
| 116 | + arr1=result_filtered["Test-Sample1"] |
| 117 | + arr2=result_filtered["Test-Sample2"] |
| 118 | + |
| 119 | + #Testing if filtering kwargs are passed to uproot.iterate() |
| 120 | + assert ak.fields(arr1) == ['branch1'] #branch2 should be filtered out |
| 121 | + assert ak.fields(arr2) == ['branch1'] |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments