-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_dataset.py
449 lines (353 loc) · 14.6 KB
/
test_dataset.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import dask
import dask.array as da
from dask.array.core import normalize_chunks
import numpy as np
from numpy.testing import assert_array_equal
import pyrap.tables as pt
import pytest
from daskms.dataset import Dataset, Variable
from daskms.reads import read_datasets
from daskms.writes import write_datasets
from daskms.utils import (select_cols_str, group_cols_str,
index_cols_str, assert_liveness)
@pytest.mark.parametrize("group_cols", [
["FIELD_ID", "SCAN_NUMBER"],
[]],
ids=group_cols_str)
@pytest.mark.parametrize("index_cols", [
["TIME", "ANTENNA1", "ANTENNA2"]],
ids=index_cols_str)
@pytest.mark.parametrize("select_cols", [
["STATE_ID", "TIME", "DATA"]],
ids=select_cols_str)
@pytest.mark.parametrize("shapes", [
{"row": 10, "chan": 16, "corr": 4}],
ids=lambda s: "shapes=%s" % s)
@pytest.mark.parametrize("chunks", [
{"row": 2},
{"row": 3, "chan": 4, "corr": 1},
{"row": 3, "chan": (4, 4, 4, 4), "corr": (2, 2)}],
ids=lambda c: "chunks=%s" % c)
def test_dataset(ms, select_cols, group_cols, index_cols, shapes, chunks):
""" Test dataset creation """
datasets = read_datasets(ms, select_cols, group_cols,
index_cols, chunks=chunks)
# (1) Read-only TableProxy
# (2) Read-only TAQL TableProxy
assert_liveness(2, 1)
chans = shapes['chan']
corrs = shapes['corr']
# Expected output chunks
echunks = {'chan': normalize_chunks(chunks.get('chan', chans),
shape=(chans,))[0],
'corr': normalize_chunks(chunks.get('corr', corrs),
shape=(corrs,))[0]}
for ds in datasets:
compute_dict = {}
for k, v in ds.data_vars.items():
compute_dict[k] = v.data
assert v.dtype == v.data.dtype
res = dask.compute(compute_dict)[0]
assert res['DATA'].shape[1:] == (chans, corrs)
assert 'STATE_ID' in res
assert 'TIME' in res
chunks = ds.chunks
assert chunks["chan"] == echunks['chan']
assert chunks["corr"] == echunks['corr']
dims = ds.dims
dims.pop('row') # row changes
assert dims == {"chan": shapes['chan'],
"corr": shapes['corr']}
del ds, datasets, compute_dict, v
assert_liveness(0, 0)
@pytest.mark.parametrize("group_cols", [
["FIELD_ID", "SCAN_NUMBER"],
[]],
ids=group_cols_str)
@pytest.mark.parametrize("index_cols", [
["TIME", "ANTENNA1", "ANTENNA2"]],
ids=index_cols_str)
@pytest.mark.parametrize("select_cols", [
["STATE_ID", "TIME", "DATA"]],
ids=select_cols_str)
@pytest.mark.parametrize("shapes", [
{"row": 10, "chan": 16, "corr": 4}],
ids=lambda s: "shapes=%s" % s)
@pytest.mark.parametrize("chunks", [
{"row": 2},
{"row": 3, "chan": 4, "corr": 1},
{"row": 3, "chan": (4, 4, 4, 4), "corr": (2, 2)}],
ids=lambda c: "chunks=%s" % c)
def test_dataset_updates(ms, select_cols,
group_cols, index_cols,
shapes, chunks):
""" Test dataset writes """
# Get original STATE_ID and DATA
with pt.table(ms, ack=False, readonly=True, lockoptions='auto') as T:
original_state_id = T.getcol("STATE_ID")
original_data = T.getcol("DATA")
try:
datasets = read_datasets(ms, select_cols, group_cols,
index_cols, chunks=chunks)
assert_liveness(2, 1)
# Test writes
writes = []
states = []
datas = []
# Create write operations and execute them
for i, ds in enumerate(datasets):
state_var = (("row",), ds.STATE_ID.data + 1)
data_var = (("row", "chan", "corr"), ds.DATA.data + 1, {})
states.append(state_var[1])
datas.append(data_var[1])
new_ds = ds.assign(STATE_ID=state_var, DATA=data_var)
writes.append(write_datasets(ms, new_ds, ["STATE_ID", "DATA"]))
_, states, datas = dask.compute(writes, states, datas)
# NOTE(sjperkins)
# Interesting behaviour here. If these objects are not
# cleared up at this point, attempts to re-open the table below
# can fail, reproducing https://github.com/ska-sa/dask-ms/issues/26
# Adding auto-locking to the table opening command seems to fix
# this somehow
del ds, new_ds, datasets, writes, state_var, data_var
assert_liveness(0, 0)
datasets = read_datasets(ms, select_cols, group_cols,
index_cols, chunks=chunks)
for i, (ds, state, data) in enumerate(zip(datasets, states, datas)):
assert_array_equal(ds.STATE_ID.data, state)
assert_array_equal(ds.DATA.data, data)
del ds, datasets
assert_liveness(0, 0)
finally:
# Restore original STATE_ID
with pt.table(ms, ack=False, readonly=False, lockoptions='auto') as T:
state_id = T.getcol("STATE_ID")
data = T.getcol("DATA")
T.putcol("STATE_ID", original_state_id)
T.putcol("DATA", original_data)
# Compare against expected result
assert_array_equal(original_state_id + 1, state_id)
assert_array_equal(original_data + 1, data)
# Even though we ask for two rows, we get single rows out
# due to the "__row__" in group_col
@pytest.mark.parametrize("chunks", [{"row": 2}], ids=lambda c: str(c))
def test_row_grouping(spw_table, spw_chan_freqs, chunks):
""" Test grouping on single rows """
datasets = read_datasets(spw_table, [], ["__row__"], [], chunks=chunks)
assert_liveness(2, 1)
assert len(datasets) == len(spw_chan_freqs)
for i, chan_freq in enumerate(spw_chan_freqs):
assert_array_equal(datasets[i].CHAN_FREQ.data[0], chan_freq)
assert_array_equal(datasets[i].NUM_CHAN.data[0], chan_freq.shape[0])
del datasets
assert_liveness(0, 0)
def test_antenna_table_string_names(ant_table, wsrt_antenna_positions):
ds = read_datasets(ant_table, [], [], None)
assert len(ds) == 1
ds = ds[0]
names = ["ANTENNA-%d" % i for i in range(wsrt_antenna_positions.shape[0])]
assert_array_equal(ds.POSITION.data, wsrt_antenna_positions)
assert_array_equal(ds.NAME.data, names)
names = ds.NAME.data.compute()
# Test that writing back string ndarrays work as
# they must be converted from ndarrays to lists
# of strings internally
write_cols = set(ds.data_vars.keys()) - set(["ROWID"])
writes = write_datasets(ant_table, ds, write_cols)
dask.compute(writes)
def test_dataset_assign(ms):
""" Test dataset assignment """
datasets = read_datasets(ms, [], [], [])
assert len(datasets) == 1
ds = datasets[0]
# Assign on an existing column is easier because we can
# infer the dimension schema from it
nds = ds.assign(TIME=ds.TIME.data + 1)
assert ds.DATA.data is nds.DATA.data
assert ds.TIME.data is not nds.TIME.data
assert_array_equal(nds.TIME.data, ds.TIME.data + 1)
# This doesn't work for new columns
with pytest.raises(ValueError, match="Couldn't find existing dimension"):
ds.assign(ANTENNA3=ds.ANTENNA1.data + 3)
# We have to explicitly supply a dimension schema
nds = ds.assign(ANTENNA3=(("row",), ds.ANTENNA1.data + 3))
assert_array_equal(ds.ANTENNA1.data + 3, nds.ANTENNA3.data)
dims = ds.dims
chunks = ds.chunks
with pytest.raises(ValueError, match="size 9 for dimension 'row'"):
array = da.zeros(dims['row'] - 1, chunks['row'])
nds = ds.assign(ANTENNA4=(("row",), array))
nds.dims
assert chunks['row'] == (10,)
with pytest.raises(ValueError, match=r"chunking \(4, 4, 2\) for dim"):
array = da.zeros(dims['row'], chunks=4)
nds = ds.assign(ANTENNA4=(("row",), array))
nds.chunks
del datasets, ds, nds
assert_liveness(0, 0)
def test_dataset_table_schemas(ms):
""" Test that we can pass table schemas """
data_dims = ("mychan", "mycorr")
table_schema = ["MS", {"DATA": {"dims": data_dims}}]
datasets = read_datasets(ms, [], [], [], table_schema=table_schema)
assert datasets[0].data_vars["DATA"].dims == ("row", ) + data_dims
@pytest.mark.parametrize("dtype", [
np.complex64,
np.complex128,
np.float32,
np.float64,
np.int16,
np.int32,
np.uint32,
np.bool,
pytest.param(np.object,
marks=pytest.mark.xfail(reason="putcol can't handle "
"lists of ints")),
pytest.param(np.uint16,
marks=pytest.mark.xfail(reason="RuntimeError: RecordRep::"
"createDataField: unknown data"
" type 17")),
pytest.param(np.uint8,
marks=pytest.mark.xfail(reason="Creates uint16 column")),
])
def test_dataset_add_column(ms, dtype):
datasets = read_datasets(ms, [], [], [])
assert len(datasets) == 1
ds = datasets[0]
# Create the dask array
bitflag = da.zeros_like(ds.DATA.data, dtype=dtype)
# Assign keyword attribute
col_kw = {"BITFLAG": {'FLAGSETS': 'legacy,cubical',
'FLAGSET_legacy': 1,
'FLAGSET_cubical': 2}}
# Assign variable onto the dataset
nds = ds.assign(BITFLAG=(("row", "chan", "corr"), bitflag))
writes = write_datasets(ms, nds, ["BITFLAG"], descriptor='ratt_ms',
column_keywords=col_kw)
dask.compute(writes)
del datasets, ds, writes, nds
assert_liveness(0, 0)
with pt.table(ms, readonly=False, ack=False, lockoptions='auto') as T:
bf = T.getcol("BITFLAG")
assert T.getcoldesc("BITFLAG")['keywords'] == col_kw['BITFLAG']
assert bf.dtype == dtype
def test_dataset_add_string_column(ms):
datasets = read_datasets(ms, [], [], [])
assert len(datasets) == 1
ds = datasets[0]
dims = ds.dims
name_list = ["BOB"] * dims['row']
names = np.asarray(name_list, dtype=np.object)
names = da.from_array(names, chunks=ds.TIME.chunks)
nds = ds.assign(NAMES=(("row",), names))
writes = write_datasets(ms, nds, ["NAMES"])
dask.compute(writes)
del datasets, ds, writes, nds
assert_liveness(0, 0)
with pt.table(ms, readonly=False, ack=False, lockoptions='auto') as T:
assert name_list == T.getcol("NAMES")
@pytest.mark.parametrize("chunks", [
{"row": (36,)},
{"row": (18, 18)}])
def test_dataset_multidim_string_column(tmp_path, chunks):
row = sum(chunks['row'])
name_list = [["X-%d" % i, "Y-%d" % i, "Z-%d" % i] for i in range(row)]
np_names = np.array(name_list, dtype=np.object)
names = da.from_array(np_names, chunks=(chunks['row'], np_names.shape[1]))
ds = Dataset({"POLARIZATION_TYPE": (("row", "xyz"), names)})
table_name = str(tmp_path / "test.table")
writes = write_datasets(table_name, ds, ["POLARIZATION_TYPE"])
dask.compute(writes)
del writes
assert_liveness(0, 0)
datasets = read_datasets(table_name, [], [], [],
chunks={'row': chunks['row']})
assert len(datasets) == 1
assert_array_equal(datasets[0].POLARIZATION_TYPE.data, np_names)
del datasets
assert_liveness(0, 0)
@pytest.mark.parametrize("dataset_chunks", [
[{'row': (5, 3, 2), 'chan': (4, 4, 4, 4), 'corr': (4,)},
{'row': (4, 3, 3), 'chan': (5, 5, 3, 3), 'corr': (2, 2)}],
])
@pytest.mark.parametrize("dtype", [np.complex128, np.float32])
def test_dataset_create_table(tmp_path, dataset_chunks, dtype):
datasets = []
names = []
datas = []
row_sum = 0
for chunks in dataset_chunks:
shapes = {k: sum(c) for k, c in chunks.items()}
row_sum += shapes['row']
# Make some visibilities
dims = ("row", "chan", "corr")
shape = tuple(shapes[d] for d in dims)
data_chunks = tuple(chunks[d] for d in dims)
data = da.random.random(shape, chunks=data_chunks).astype(dtype)
data_var = Variable(dims, data, {})
# Make some string names
dims = ("row",)
shape = tuple(shapes[d] for d in dims)
str_chunks = tuple(chunks[d] for d in dims)
np_str_array = np.asarray(["BOB"] * shape[0], dtype=np.object)
da_str_array = da.from_array(np_str_array, chunks=str_chunks)
str_array_var = Variable(dims, da_str_array, {})
datasets.append(Dataset({"DATA": data_var, "NAMES": str_array_var}))
datas.append(data)
names.extend(np_str_array.tolist())
# Write the data to a new table
table_name = os.path.join(str(tmp_path), 'test.table')
writes = write_datasets(table_name, datasets, ["DATA", "NAMES"])
dask.compute(writes)
# Check written data
with pt.table(table_name, readonly=True,
lockoptions='auto', ack=False) as T:
assert row_sum == T.nrows()
assert_array_equal(T.getcol("DATA"), np.concatenate(datas))
assert_array_equal(T.getcol("NAMES"), names)
def test_dataset_computes_and_values(ms):
datasets = read_datasets(ms, [], [], [])
assert len(datasets) == 1
ds = datasets[0]
# All dask arrays
for k, v in ds.data_vars.items():
assert isinstance(v.data, da.Array)
nds = ds.compute()
# Now we have numpy arrays that match original data
for k, v in nds.data_vars.items():
assert isinstance(v.data, np.ndarray)
assert_array_equal(v.data, ds.data_vars[k].data)
assert_array_equal(v.values, ds.data_vars[k].data)
def test_dataset_dask(ms):
datasets = read_datasets(ms, [], [], [])
assert len(datasets) == 1
ds = datasets[0]
# All dask arrays
for k, v in ds.data_vars.items():
assert isinstance(v.data, da.Array)
# Test variable compute
v2 = dask.compute(v)[0]
assert isinstance(v2, Variable)
assert isinstance(v2.data, np.ndarray)
# Test variable persists
v3 = dask.persist(v)[0]
assert isinstance(v3, Variable)
# Now have numpy array in the graph
assert len(v3.data.__dask_keys__()) == 1
assert isinstance(v3.data.__dask_graph__().values()[0], np.ndarray)
# Test compute
nds = dask.compute(ds)[0]
for k, v in nds.data_vars.items():
assert isinstance(v.data, np.ndarray)
# Test persist
nds = dask.persist(ds)[0]
for k, v in nds.data_vars.items():
assert isinstance(v.data, da.Array)
# Now have numpy array iin the graph
assert len(v.data.__dask_keys__()) == 1
assert isinstance(v.data.__dask_graph__().values()[0], np.ndarray)