Newer
Older
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
"""Write EXDF files based on EXtra-data SourceData objects.
Building on the EXDF DataFile API, SourceDataWriter builds
EXDf-structured HDF5 files based on a collection of EXtra-data
SourceData objects. It bypasses the DataCollection interface used in
extra_data.write to allow for more flexibility with data selection and
stricter validation when combining data from different locations.
"""
from functools import reduce
from itertools import accumulate
from logging import getLogger
from operator import or_
import numpy as np
from extra_data import FileAccess
from . import DataFile
log = getLogger('exdf.write.SourceDataWriter')
class SourceDataWriter:
def get_data_format_version(self):
"""Determine the data format version."""
return '1.3'
def with_origin(self):
"""Determine whether to write INDEX/origin data."""
return True
def with_attrs(self):
"""Determine whether to write key attributes."""
return True
def chunk_instrument_data(self, source, key, orig_chunks):
"""Determine chunk size for INSTRUMENT key.
Args:
source (str): Source name.
key (str): Key name.
orig_chunks (tuple of int): Chunk size as found in input.
Returns:
(tuple of int): Chunk size to use for output.
"""
return orig_chunks
def copy_instrument_data(self, source, key, dest, data):
"""Copy INSTRUMENT data from input to output.
Args:
source (str): Source name.
key (str): Key name.
dset (h5py.Dataset): Destination dataset.
data (ndarray): Source data.
Returns:
None
"""
dest[:] = data
def write_sequence(self, output_path, sources, sequence=0):
"""Write iterable of SourceData to file.
Any train or key selection of each SourceData file is taken into
account.
Args:
output_path (Path, str): Path to write to.
sources (Iterable of extra_data.SourceData): Data sources.
sequence (int, optional): Sequence number, 0 by default.
Returns:
None
"""
with DataFile(output_path, 'w', driver='core') as f:
self.write_base(f, sources, sequence)
self.write_control(
f, [sd for sd in sources if sd.is_control])
self.write_instrument(
f, [sd for sd in sources if sd.is_instrument])
def write_base(self, f, sources, sequence):
"""Write METADATA, INDEX and source groups.
Args:
f (exdf.DataFile): Output file.
sources (Iterable of extra_data.SourceData): Data sources.
sequence (int, optional): Sequence number, 0 by default.
Returns:
None
"""
train_ids, *index_dsets = get_index_root_data(sources)
control_indices, instrument_indices = build_sources_index(sources)
f.create_metadata(
like=sources[0],
sequence=sequence,
data_format_version=self.get_data_format_version(),
control_sources=control_indices.keys(),
instrument_channels=[
f'{source}/{channel}'
for source, channels in instrument_indices.items()
for channel in channels.keys()])
f.create_dataset('METADATA/dataWriter', data=b'exdf-tools', shape=(1,))
if not self.with_origin():
index_dsets = (*index_dsets[:-1], None)
f.create_index(train_ids, *index_dsets)
for source, counts in control_indices.items():
control_src = f.create_control_source(source)
control_src.create_index(len(train_ids), per_train=True)
for source, channel_counts in instrument_indices.items():
instrument_src = f.create_instrument_source(source)
instrument_src.create_index(**channel_counts)
def write_control(self, f, sources):
"""Write CONTROL and RUN data.
This method assumes the source datasets already exist.
Args:
f (exdf.DataFile): Output file.
sources (Iterable of extra_data.SourceData): Data sources,
should not contain instrument sources.
Returns:
None
"""
for sd in sources:
source = f.source[sd.source]
attrs = get_key_attributes(sd) if self.with_attrs() else {}
run_data_leafs = {}
for path, value in sd.run_values().items():
key = path[:path.rfind('.')]
leaf = path[path.rfind('.')+1:]
run_data_leafs.setdefault(key, {})[leaf] = value
# Write CONTROL keys and their RUN keys.
for key in sd.keys(False):
run_entry = run_data_leafs.pop(key, False)
if run_entry:
run_entry = (run_entry['value'], run_entry['timestamp'])
ctrl_values = sd[f'{key}.value'].ndarray()
ctrl_timestamps = sd[f'{key}.timestamp'].ndarray()
source.create_key(
key, values=ctrl_values, timestamps=ctrl_timestamps,
run_entry=run_entry, attrs=attrs.pop(key, None))
# Write remaining RUN-only keys.
for key, leafs in run_data_leafs.items():
source.create_run_key(key, **leafs, attrs=attrs.pop(key, None))
# Fill in the missing attributes for nodes.
for path, attrs in attrs.items():
source.run_key[path].attrs.update(attrs)
source.key[path].attrs.update(attrs)
def write_instrument(self, f, sources):
"""Write INSTRUMENT data.
This method assumes the source datasets already exist.
Args:
f (exdf.DataFile): Output file.
sources (Iterable of extra_data.SourceData): Data sources,
should not contain control sources.
Returns:
None
"""
for sd in sources:
source = f.source[sd.source]
attrs = get_key_attributes(sd) if self.with_attrs() else {}
for key in sd.keys():
kd = sd[key]
shape = (kd.data_counts(labelled=False).sum(), *kd.entry_shape)
chunks = self.chunk_instrument_data(
sd.source, key,
kd.files[0].file[kd.hdf5_data_path].chunks)
source.create_key(
key, shape=shape, maxshape=(None,) + shape[1:],
chunks=chunks, dtype=kd.dtype, attrs=attrs.pop(key, None))
for path, attrs in attrs.items():
source.key[path].attrs.update(attrs)
# Update tableSize for each index group to the correct
# number of trains.
for index_group in sd.index_groups:
source[index_group].attrs['tableSize'] = sd.data_counts(
labelled=False, index_group=index_group).sum()
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
# Copy INSTRUMENT data.
for sd in sources:
source = f.source[sd.source]
for key in sd.keys():
# TODO: Copy by chunk / file if too large
self.copy_instrument_data(sd.source, key, source.key[key],
sd[key].ndarray())
def get_index_root_data(sources):
"""Get train ID, timestamp, flag and origin data."""
# Collect train IDs for this sequence.
train_ids = np.zeros(0, dtype=np.uint64)
for sd in sources:
train_ids = np.union1d(train_ids, sd.train_ids)
# Collect input files by index keys (source / index_group).
files_by_index_keys = {}
for sd in sources:
for key in sd.keys():
kd = sd[key]
index_key = f'{sd.source}/{kd.index_group}'
if index_key not in files_by_index_keys:
files_by_index_keys[index_key] = set(kd.source_file_paths)
inp_files = reduce(or_, files_by_index_keys.values(), set())
# {trainId: ({item: timestamp}, {item: flag}, {item: origin})
root_data = {}
for inp_file in inp_files:
fa = FileAccess(inp_file)
sel_trains, sel_rows, _ = np.intersect1d(fa.train_ids, train_ids,
return_indices=True)
if 'INDEX/timestamp' in fa.file:
sel_timestamps = np.array(fa.file['INDEX/timestamp'][sel_rows])
else:
sel_timestamps = np.zeros_like(sel_trains, dtype=np.uint64)
# Missing INDEX/flag dataset handled on the EXtra-data side.
sel_flag = fa.validity_flag[sel_rows]
if 'INDEX/origin' in fa.file:
sel_origin = np.array(fa.file['INDEX/origin'][sel_rows])
else:
sel_origin = np.ones_like(sel_trains, dtype=np.int32)
sel_origin[sel_flag] = 0
item = (fa.data_category, fa.aggregator)
# Collect timestamp, flag and origin as a function of train ID
# and item while ensuring there is no mismatch for the same
# train ID and item.
for train_id, timestamp, flag, origin in zip(
sel_trains, sel_timestamps, sel_flag, sel_origin
):
new_entry = (timestamp, flag, origin)
old_entry = root_data \
.setdefault(train_id, dict()) \
.setdefault(item, new_entry)
if old_entry != new_entry:
raise ValueError(f'INDEX root data mismatch for item {item}: '
f'{new_entry} (from train {train_id}) vs '
f'{old_entry}')
if not np.array_equal(sorted(root_data.keys()), train_ids):
# This is not supposed to happen.
raise ValueError('input files missing index data for selected trains')
timestamps = []
flags = []
origins = []
# Decide which value to choose for each train.
for train_id, entries in root_data.items():
values, counts = np.unique(
np.fromiter(entries.values(), dtype=object, count=len(entries)),
return_counts=True)
if len(values) > 1:
values_str = ', '.join([f'{c}x {v}' for v, c
in zip(values, counts)])
log.warn(f'INDEX root data mismatch across items on train '
f'{train_id}: {values_str}')
entry = values[counts.argmax()]
timestamps.append(entry[0])
flags.append(entry[1])
origins.append(entry[2])
return train_ids, np.array(timestamps), np.array(flags), np.array(origins)
def build_sources_index(sources):
"""Build data count and offsets."""
# Build sources and indices.
control_indices = {}
instrument_indices = {}
for sd in sources:
if sd.is_control:
control_indices[sd.source] = sd.data_counts(labelled=False)
else:
instrument_indices[sd.source] = {
grp: sd.data_counts(labelled=False, index_group=grp)
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
return control_indices, instrument_indices
def get_key_attributes(sd):
source_attrs = dict()
def build_path(a, b):
return f'{a}.{b}'
for key in sd.keys(inc_timestamps=False):
paths = accumulate(key.split('.'), func=build_path)
for path in paths:
if path in source_attrs:
# Skip this path, already parent of some other key.
continue
for source_file in sd[key].source_file_paths:
fa = FileAccess(source_file)
hdf_path = f'{sd.section}/{sd.source}/{path.replace(".", "/")}'
path_attrs = dict(fa.file[hdf_path].attrs)
existing_attrs = source_attrs.setdefault(path, path_attrs)
if existing_attrs is not path_attrs:
same_keys = existing_attrs.keys() == path_attrs.keys()
same_values = all([
(
np.array_equal(v1, v2)
if isinstance(v1, np.ndarray)
else v1 == v2
)
Philipp Schmidt
committed
for (k, v1), v2
in zip(existing_attrs.items(), path_attrs.values())
if k != 'tableSize'])
if not same_keys or not same_values:
log.debug(f'Attributes for {sd.source}.{path} in '
f'{source_file} are {path_attrs}, but got '
f'{source_attrs[path]} from previous file')
raise ValueError(f'attribute mismatch on '
f'{sd.source}.{path}')
return source_attrs