-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathextras.py
347 lines (280 loc) · 8.1 KB
/
extras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE
"""
This module defines functions that import external libraries used by Uproot, but not
required by an Uproot installation. (Uproot only requires NumPy).
If a library cannot be imported, these functions raise ``ModuleNotFoundError`` with
error messages containing instructions on how to install the library.
"""
from __future__ import annotations
import atexit
import importlib.metadata
import os
from uproot._util import parse_version
def awkward():
"""
Imports and returns ``awkward``.
"""
try:
import awkward
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'awkward' package with:
pip install awkward
Alternatively, you can use ``library="np"`` or globally set ``uproot.default_library``
to output as NumPy arrays, rather than Awkward arrays.
"""
) from err
if parse_version(awkward.__version__) >= parse_version("2.4.6"):
return awkward
else:
raise ModuleNotFoundError(
f"Uproot 5.1+ can only be used with Awkward 2.4.6 or newer; you have Awkward {awkward.__version__}"
)
def pandas():
"""
Imports and returns ``pandas``.
"""
try:
import pandas
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'pandas' package with:
pip install pandas
or
conda install pandas"""
) from err
else:
return pandas
def Minio_client():
"""
Imports and returns ``minio.Minio``.
"""
try:
from minio import Minio
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'minio' package with:
pip install minio
or
conda install minio"""
) from err
else:
return Minio
def XRootD_client():
"""
Imports and returns ``XRootD.client`` (after setting the
```XRD_RUNFORKHANDLER`` environment variable to ``"1"``, to allow
multiprocessing).
"""
os.environ["XRD_RUNFORKHANDLER"] = "1" # set multiprocessing flag
try:
import XRootD
import XRootD.client
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""Install XRootD python bindings with:
conda install -c conda-forge xrootd
(or download from http://xrootd.org/dload.html and manually compile with """
"""cmake; setting PYTHONPATH and LD_LIBRARY_PATH appropriately)."""
) from err
if older_xrootd("5.1.0"):
# This is registered after calling "import XRootD.client" so it is ran
# before XRootD.client.finalize.finalize()
@atexit.register
def cleanup_open_files():
"""Clean up any open xrootd file objects at exit
Required to avoid deadlocks from XRootD, for details see:
* https://github.com/scikit-hep/uproot/issues/504
* https://github.com/xrootd/xrootd/pull/1260
"""
import gc
for obj in gc.get_objects():
try:
isopen = isinstance(obj, XRootD.client.file.File) and obj.is_open()
except ReferenceError:
pass
else:
if isopen:
obj.close()
return XRootD.client
def older_xrootd(min_version):
"""
Check if the installed XRootD bindings are newer than a given version
without importing. Defaults to False if XRootD is not installed. Unrecognized
versions (i.e. self-built XRootD, whose version numbers are strings)
return False: that is, they're assumed to be new, so that no warnings
are raised.
"""
version = xrootd_version()
if version is None:
return False
else:
try:
return parse_version(version) < parse_version(min_version)
except TypeError:
return False
def xrootd_version():
"""
Gets the XRootD version if installed, otherwise returns None.
"""
try:
return importlib.metadata.version("xrootd")
except ModuleNotFoundError:
try:
# Versions before 4.11.1 used pyxrootd as the package name
return importlib.metadata.version("pyxrootd")
except ModuleNotFoundError:
return None
def isal():
"""
Import and return ``isal``.
"""
try:
import isal
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'isal' package with:
pip install isal
or
conda install python-isal"""
) from err
else:
return isal
def deflate():
"""
Import and return ``deflate``.
"""
try:
import deflate
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'deflate' package with:
pip install deflate
or
conda install libdeflate"""
) from err
else:
return deflate
def cramjam():
"""
Import and returns ``cramjam``.
"""
try:
import cramjam
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'cramjam' package with:
pip install cramjam
or
conda install cramjam"""
) from err
else:
return cramjam
def xxhash():
"""
Imports and returns ``xxhash``.
"""
try:
import xxhash
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the `xxhash` packages with:
pip install xxhash
or
conda install python-xxhash"""
) from err
else:
return xxhash
def boost_histogram():
"""
Imports and returns ``boost-histogram``.
"""
try:
import boost_histogram
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'boost-histogram' package with:
pip install boost-histogram
or
conda install -c conda-forge boost-histogram"""
) from err
else:
return boost_histogram
def hist():
"""
Imports and returns ``hist``.
"""
try:
import hist
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'hist' package with:
pip install hist"""
) from err
else:
return hist
def dask():
"""
Imports and returns ``dask``.
"""
try:
import dask
import dask.blockwise
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""for uproot.dask with 'library="np"', install the complete 'dask' package with:
pip install "dask[complete]"
or
conda install dask"""
) from err
else:
return dask
def dask_array():
"""
Imports and returns ``dask.array``.
"""
try:
import dask.array as da
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""for uproot.dask with 'library="np"', install the complete 'dask' package with:
pip install "dask[complete]"
or
conda install dask"""
) from err
else:
return da
def dask_awkward():
"""
Imports and returns ``dask_awkward``.
"""
try:
import dask_awkward
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""for uproot.dask, install 'dask' and the 'dask-awkward' package with:
pip install "dask[complete] dask-awkward"
or
conda install -c conda-forge dask dask-awkward"""
) from err
if parse_version(dask_awkward.__version__) >= parse_version("2023.10.0"):
return dask_awkward
else:
raise ModuleNotFoundError(
f"Uproot 5.1+ can only be used with dask-awkward 2023.10.0 or newer; you have dask-awkward {dask_awkward.__version__}"
)
def awkward_pandas():
"""
Imports and returns ``awkward_pandas``.
"""
try:
import awkward_pandas
except ModuleNotFoundError as err:
raise ModuleNotFoundError(
"""install the 'awkward-pandas' package with:
pip install awkward-pandas
or
conda install -c conda-forge awkward-pandas"""
) from err
else:
return awkward_pandas