-
Notifications
You must be signed in to change notification settings - Fork 240
/
record_util.py
243 lines (192 loc) · 6.41 KB
/
record_util.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
"""
Utility functions related to recording or execution metadata.
"""
from fnmatch import fnmatchcase
import os
import re
import json
import numpy as np
def create_local_meta(name):
"""
Create the metadata dictionary for this level of execution.
Parameters
----------
name : str
String to describe the current level of execution.
Returns
-------
dict
Dictionary containing the metadata.
"""
local_meta = {
'name': name,
'timestamp': None,
'success': 1,
'msg': '',
}
return local_meta
def format_iteration_coordinate(coord, prefix=None):
"""
Format the iteration coordinate to a human-readable string.
Parameters
----------
coord : list
List containing the iteration coordinate.
prefix : str or None
Prefix to prepend to iteration coordinates.
Returns
-------
str
Iteration coordinate converted to a string.
"""
separator = '|'
iteration_number_separator = '-'
iteration_coordinate = []
for name, local_coord in zip(coord[1::2], coord[2::2]):
iteration_coordinate.append(name)
iter_str = map(str, local_coord)
coord_str = iteration_number_separator.join(iter_str)
iteration_coordinate.append(coord_str)
if prefix:
prefix = "%s_rank%d" % (prefix, coord[0])
else:
prefix = "rank%d" % (coord[0])
return ':'.join([prefix, separator.join(iteration_coordinate)])
# regular expression used to determine if a node in an iteration coordinate represents a system
_coord_system_re = re.compile('(_solve_nonlinear|_apply_nonlinear)$')
# Regular expression used for splitting iteration coordinates, removes separator and iter counts
_coord_split_re = re.compile('\\|\\d+\\|*')
def get_source_system(iteration_coordinate):
"""
Get pathname of system that is the source of the iteration.
Parameters
----------
iteration_coordinate : str
The full unique identifier for this iteration.
Returns
-------
str
The pathname of the system that is the source of the iteration.
"""
path = []
parts = _coord_split_re.split(iteration_coordinate)
for part in parts:
if (_coord_system_re.search(part) is not None):
if ':' in part:
# get rid of 'rank#:'
part = part.split(':')[1]
path.append(part.split('.')[0])
# return pathname of the system
return '.'.join(path)
def check_valid_sqlite3_db(filename):
"""
Raise an IOError if the given filename does not reference a valid SQLite3 database file.
Parameters
----------
filename : str
The path to the file to be tested.
Raises
------
IOError
If the given filename does not reference a valid SQLite3 database file.
"""
# check that the file exists
if not os.path.isfile(filename):
raise IOError('File does not exist({0})'.format(filename))
# check that the file is large enough (SQLite database file header is 100 bytes)
if os.path.getsize(filename) < 100:
raise IOError('File does not contain a valid sqlite database ({0})'.format(filename))
# check that the first 100 bytes actually contains a valid SQLite database header
with open(filename, 'rb') as fd:
header = fd.read(100)
if header[:16] != b'SQLite format 3\x00':
raise IOError('File does not contain a valid sqlite database ({0})'.format(filename))
def check_path(path, includes, excludes, include_all_path=False):
"""
Calculate whether `path` should be recorded.
Parameters
----------
path : str
Path proposed to be recorded.
includes : list
List of things to be included in recording list.
excludes : list
List of things to be excluded from recording list.
include_all_path : bool
If set to True, will return True unless it is in excludes.
Returns
-------
bool
True if path should be recorded, False if it's been excluded.
"""
for ex_pattern in excludes:
if fnmatchcase(path, ex_pattern):
return False
if not include_all_path:
for pattern in includes:
if fnmatchcase(path, pattern):
return True
return include_all_path
def deserialize(json_data, abs2meta, prom2abs, conns):
"""
Deserialize recorded data from a JSON formatted string.
If all data values are arrays then a numpy structured array will be returned,
otherwise a dictionary mapping variable names to values will be returned.
Parameters
----------
json_data : str
JSON encoded data.
abs2meta : dict
Dictionary mapping absolute variable names to variable metadata.
prom2abs : dict
Dictionary mapping promoted input names to absolute. Needed to resolve auto_ivc outputs
that are recorded with their promoted input name.
conns : dict
Dictionary of all model connections.
Returns
-------
array or dict
Variable names and values parsed from the JSON string.
"""
values = json.loads(json_data)
if values is None:
return None
all_array = True
for name, value in values.items():
try:
has_shape = 'shape' in abs2meta[name]
except KeyError:
abs_name = prom2abs['input'][name]
src_name = conns[abs_name[0]]
has_shape = 'shape' in abs2meta[src_name]
if isinstance(value, list) and has_shape:
values[name] = np.asarray(value) # array will be proper shape based on list structure
else:
all_array = False
if all_array:
return dict_to_structured_array(values)
else:
return values
def dict_to_structured_array(values):
"""
Convert a dict of variable names and values into a numpy structured array.
Parameters
----------
values : dict
Dict of variable names and values.
Returns
-------
array
Numpy structured array containing the same names and values as the input values dict.
"""
if values:
dtype_tuples = []
for name, value in values.items():
tple = (str(name), '{}f8'.format(value.shape))
dtype_tuples.append(tple)
array = np.zeros((1,), dtype=dtype_tuples)
for name, value in values.items():
array[name] = value
return array
else:
return None