-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhdf.py
More file actions
112 lines (91 loc) · 3.5 KB
/
Copy pathhdf.py
File metadata and controls
112 lines (91 loc) · 3.5 KB
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
import os
from typing import Any, Optional
import cloudpickle
import h5py
import numpy as np
from executorlib.standalone.cache import group_dict
def dump(file_name: Optional[str], data_dict: dict) -> None:
"""
Dump data dictionary into HDF5 file
Args:
file_name (str): file name of the HDF5 file as absolute path
data_dict (dict): dictionary containing the python function to be executed {"fn": ..., "args": (), "kwargs": {}}
"""
if file_name is not None:
file_name_abs = os.path.abspath(file_name)
os.makedirs(os.path.dirname(file_name_abs), exist_ok=True)
with h5py.File(file_name_abs, "a") as fname:
for data_key, data_value in data_dict.items():
if data_key in group_dict:
fname.create_dataset(
name="/" + group_dict[data_key],
data=np.void(cloudpickle.dumps(data_value)),
)
def load(file_name: str) -> dict:
"""
Load data dictionary from HDF5 file
Args:
file_name (str): file name of the HDF5 file as absolute path
Returns:
dict: dictionary containing the python function to be executed {"fn": ..., "args": (), "kwargs": {}}
"""
with h5py.File(file_name, "r") as hdf:
data_dict = {}
if "function" in hdf:
data_dict["fn"] = cloudpickle.loads(np.void(hdf["/function"]))
else:
raise TypeError("Function not found in HDF5 file.")
if "input_args" in hdf:
data_dict["args"] = cloudpickle.loads(np.void(hdf["/input_args"]))
else:
data_dict["args"] = ()
if "input_kwargs" in hdf:
data_dict["kwargs"] = cloudpickle.loads(np.void(hdf["/input_kwargs"]))
else:
data_dict["kwargs"] = {}
if "error_log_file" in hdf:
data_dict["error_log_file"] = cloudpickle.loads(
np.void(hdf["/error_log_file"])
)
return data_dict
def get_output(file_name: str) -> tuple[bool, bool, Any]:
"""
Check if output is available in the HDF5 file
Args:
file_name (str): file name of the HDF5 file as absolute path
Returns:
Tuple[bool, bool, object]: boolean flag indicating if output is available and the output object itself
"""
with h5py.File(file_name, "r") as hdf:
if "output" in hdf:
return True, True, cloudpickle.loads(np.void(hdf["/output"]))
elif "error" in hdf:
return True, False, cloudpickle.loads(np.void(hdf["/error"]))
else:
return False, False, None
def get_runtime(file_name: str) -> float:
"""
Get run time from HDF5 file
Args:
file_name (str): file name of the HDF5 file as absolute path
Returns:
float: run time from the execution of the python function
"""
with h5py.File(file_name, "r") as hdf:
if "runtime" in hdf:
return cloudpickle.loads(np.void(hdf["/runtime"]))
else:
return 0.0
def get_queue_id(file_name: Optional[str]) -> Optional[int]:
"""
Get queuing system id from HDF5 file
Args:
file_name (str): file name of the HDF5 file as absolute path
Returns:
int: queuing system id from the execution of the python function
"""
if file_name is not None and os.path.exists(file_name):
with h5py.File(file_name, "r") as hdf:
if "queue_id" in hdf:
return cloudpickle.loads(np.void(hdf["/queue_id"]))
return None