Skip to content

Commit ef39103

Browse files
committed
helper for loading str from delivered file - error log on empty path
1 parent 27adbf6 commit ef39103

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

servicex_analysis_utils/file_peeking.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import numpy as np
3232
import awkward as ak
3333
import json
34+
import logging
3435

3536

3637
def run_query(input_filenames):
@@ -123,6 +124,30 @@ def build_deliver_spec(datasets):
123124
return spec_python
124125

125126

127+
def open_delivered_file(sample, path):
128+
"""
129+
Opens the first file delivered by ServiceX for a given sample and returns the
130+
structure encoded in the "servicex/branch" branch.
131+
If no files are found, logs a warning and returns None.
132+
Parameters:
133+
sample (str): The sample name for which to open the file.
134+
path (list): List of file paths delivered by ServiceX for the sample.
135+
"""
136+
137+
if not path:
138+
logging.warning(
139+
f"Warning: No files found for sample '{sample}' in delivered results. Skipping."
140+
)
141+
return None
142+
143+
try:
144+
with uproot.open(path[0]) as f:
145+
return f["servicex"]["branch"].array()[0]
146+
except Exception as e:
147+
logging.error(f"Error opening file for sample '{sample}': {e}")
148+
return None
149+
150+
126151
def print_structure_from_str(
127152
deliver_dict, filter_branch="", save_to_txt=False, do_print=False
128153
):
@@ -147,16 +172,18 @@ def print_structure_from_str(
147172
)
148173

149174
for sample_name, path in deliver_dict.items():
175+
structure_str = open_delivered_file(sample_name, path)
176+
if structure_str is None:
177+
continue
178+
# Parse the JSON string into a dictionary
179+
structure_dict = json.loads(structure_str)
180+
150181
output_lines.append(
151182
f"\n---------------------------\n"
152183
f"\U0001f4c1 Sample: {sample_name}\n"
153184
f"---------------------------"
154185
)
155186

156-
with uproot.open(path[0]) as f:
157-
json_str = f["servicex"]["branch"].array()[0]
158-
structure_dict = json.loads(json_str)
159-
160187
for tree_name, branches in structure_dict.items():
161188
output_lines.append(f"\n\U0001f333 Tree: {tree_name}")
162189
output_lines.append(" ├── Branches:")
@@ -272,8 +299,9 @@ def get_structure(datasets, array_out=False, **kwargs):
272299
if array_out == True:
273300
all_arrays = {}
274301
for sample, path in output.items():
275-
with uproot.open(path[0]) as f:
276-
structure_str = f["servicex"]["branch"].array()[0]
302+
structure_str = open_delivered_file(sample, path)
303+
if structure_str is None:
304+
continue
277305
sample_array = str_to_array(structure_str)
278306
all_arrays[sample] = sample_array
279307
return all_arrays

0 commit comments

Comments
 (0)