-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.py
373 lines (282 loc) · 11 KB
/
convert.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
import abc
import ast
from argparse import ArgumentTypeError
from functools import partial
import logging
from pathlib import Path
import shutil
from daskms.apps.application import Application
from daskms.fsspec_store import DaskMSStore
log = logging.getLogger(__name__)
class ChunkTransformer(ast.NodeTransformer):
def visit_Module(self, node):
if len(node.body) != 1 or not isinstance(node.body[0], ast.Expr):
raise ValueError("Module must contain a single expression")
expr = node.body[0]
if not isinstance(expr.value, ast.Dict):
raise ValueError("Expression must contain a dictionary")
return self.visit(expr).value
def visit_Dict(self, node):
keys = [self.visit(k) for k in node.keys]
values = [self.visit(v) for v in node.values]
return {k: v for k, v in zip(keys, values)}
def visit_Name(self, node):
return node.id
def visit_Tuple(self, node):
return tuple(self.visit(v) for v in node.elts)
def visit_Num(self, node):
return node.n
class TableFormat(abc.ABC):
@abc.abstractproperty
def version(self):
raise NotImplementedError
@abc.abstractproperty
def subtables(self):
raise NotImplementedError
@abc.abstractclassmethod
def reader(self):
raise NotImplementedError
@abc.abstractclassmethod
def writer(self):
raise NotImplementedError
@staticmethod
def from_path(path):
store = DaskMSStore(path)
typ = store.type()
if typ == "casa":
from daskms.table_proxy import TableProxy
import pyrap.tables as pt
table_proxy = TableProxy(pt.table, str(store.casa_path()),
readonly=True, ack=False)
keywords = table_proxy.getkeywords().result()
try:
version = str(keywords["MS_VERSION"])
except KeyError:
typ = "plain"
version = "<unspecified>"
else:
typ = "measurementset"
subtables = CasaFormat.find_subtables(keywords)
return CasaFormat(version, subtables, typ)
elif typ == "zarr":
subtables = ZarrFormat.find_subtables(store)
return ZarrFormat("0.1", subtables)
elif typ == "parquet":
subtables = ParquetFormat.find_subtables(store)
return ParquetFormat("0.1", subtables)
else:
raise ValueError(f"Unexpected table type {typ}")
@staticmethod
def from_type(typ):
if typ == "casa":
return CasaFormat("<unspecified>", [], "plain")
elif typ == "zarr":
return ZarrFormat("0.1", [])
elif typ == "parquet":
return ParquetFormat("0.1", [])
else:
raise ValueError(f"Unexpected table type {typ}")
class BaseTableFormat(TableFormat):
def __init__(self, version):
self._version = version
@property
def version(self):
return self._version
def check_unused_kwargs(self, fn_name, **kwargs):
if kwargs:
raise NotImplementedError(f"The following kwargs: "
f"{list(kwargs.keys())} "
f"were not consumed by "
f"{self.__class__.__name__}."
f"{fn_name}(**kw)")
class CasaFormat(BaseTableFormat):
TABLE_PREFIX = "Table: "
TABLE_TYPES = set(["plain", "measurementset"])
def __init__(self, version, subtables, type="plain"):
super().__init__(version)
if type not in self.TABLE_TYPES:
raise ValueError(f"{type} is not in {self.TABLE_TYPES}")
self._subtables = subtables
self._type = type
@classmethod
def find_subtables(cls, keywords):
return [k for k, v in keywords.items() if cls.is_subtable(v)]
@classmethod
def is_subtable(cls, keyword):
if not isinstance(keyword, str):
return False
if not keyword.startswith(cls.TABLE_PREFIX):
return False
path = Path(keyword[len(cls.TABLE_PREFIX):])
return (path.exists() and
path.is_dir() and
(path / "table.dat").exists())
def is_measurement_set(self):
return self._type == "measurementset"
def reader(self, **kw):
try:
group_cols = kw.pop("group_columns", None)
if self.is_measurement_set():
from daskms import xds_from_ms
return partial(xds_from_ms, group_cols=group_cols)
else:
from daskms import xds_from_table
return xds_from_table
finally:
self.check_unused_kwargs("reader", **kw)
def writer(self):
from daskms import xds_to_table
if self.is_measurement_set():
return partial(xds_to_table, descriptor="ms")
else:
return xds_to_table
@property
def subtables(self):
return self._subtables
def __str__(self):
return "casa" if not self.is_measurement_set() else self._type
class ZarrFormat(BaseTableFormat):
def __init__(self, version, subtables):
self._subtables = subtables
@classmethod
def find_subtables(cls, store):
paths = (p.relative_to(store.path) for p
in map(Path, store.subdirectories()))
return [str(p) for p in paths if p.stem != "MAIN" and
store.exists(str(p / ".zgroup"))]
@property
def subtables(self):
return self._subtables
def reader(self, **kw):
group_columns = kw.pop("group_columns", False)
if group_columns:
raise ValueError("\"group_column\" is not supported "
"for zarr inputs")
try:
from daskms.experimental.zarr import xds_from_zarr
return xds_from_zarr
finally:
self.check_unused_kwargs("reader", **kw)
def writer(self):
from daskms.experimental.zarr import xds_to_zarr
return xds_to_zarr
def __str__(self):
return "zarr"
class ParquetFormat(BaseTableFormat):
def __init__(self, version, subtables):
super().__init__(version)
self._subtables = subtables
@classmethod
def find_subtables(cls, store):
paths = (p.relative_to(store.path) for p
in map(Path, store.subdirectories()))
return [str(p) for p in paths if p.stem != "MAIN" and
store.exists(str(p / ".zgroup"))]
@property
def subtables(self):
return self._subtables
def reader(self, **kw):
group_columns = kw.pop("group_columns", False)
if group_columns:
raise ValueError("\"group_column\" is not supported "
"for zarr inputs")
try:
from daskms.experimental.arrow.reads import xds_from_parquet
return xds_from_parquet
finally:
self.check_unused_kwargs("reader", **kw)
def writer(self):
from daskms.experimental.arrow.writes import xds_to_parquet
return xds_to_parquet
def __str__(self):
return "parquet"
def convert_table(args):
in_fmt = TableFormat.from_path(args.input)
out_fmt = TableFormat.from_type(args.format)
reader = in_fmt.reader(group_columns=args.group_columns)
writer = out_fmt.writer()
datasets = reader(args.input, chunks=args.chunks)
if isinstance(in_fmt, CasaFormat):
# Drop any ROWID columns
datasets = [ds.drop_vars("ROWID", errors="ignore")
for ds in datasets]
log.info("Input: '%s' %s", in_fmt, str(args.input))
log.info("Output: '%s' %s", out_fmt, str(args.output))
writes = [writer(datasets, str(args.output))]
# Now do the subtables
for table in list(in_fmt.subtables):
if table == "SORTED_TABLE":
log.warning(f"Ignoring {table}")
continue
in_path = args.input.parent / "::".join((args.input.name, table))
in_fmt = TableFormat.from_path(in_path)
out_path = args.output.parent / "::".join((args.output.name, table))
out_fmt = TableFormat.from_type(args.format)
reader = in_fmt.reader()
writer = out_fmt.writer()
if isinstance(in_fmt, CasaFormat):
# Drop any ROWID columns
datasets = [ds.drop_vars("ROWID", errors="ignore")
for ds in reader(in_path, group_cols="__row__")]
else:
datasets = reader(in_path)
writes.append(writer(datasets, str(out_path)))
return writes
def _check_input_path(input: str):
input_path = Path(input)
parts = input_path.name.split("::", 1)
if len(parts) == 1:
check_path = input_path
elif len(parts) == 2:
check_path = input_path.parent / parts[0]
else:
raise RuntimeError("len(parts) not in (1, 2)")
if not check_path.exists():
raise ArgumentTypeError(f"{input} is an invalid path.")
return input_path
def parse_chunks(chunks: str):
return ChunkTransformer().visit(ast.parse(chunks))
class Convert(Application):
TABLE_KEYWORD_PREFIX = "Table: "
def __init__(self, args, log):
self.log = log
self.args = args
@staticmethod
def group_col_converter(group_columns):
if not group_columns:
return None
return [c.strip() for c in group_columns.split(",")]
@classmethod
def setup_parser(cls, parser):
parser.add_argument("input", type=_check_input_path)
parser.add_argument("-o", "--output", type=Path)
parser.add_argument("-g", "--group-columns",
type=Convert.group_col_converter,
default="",
help="Columns to group or partition "
"the input dataset by. "
"This defaults to the default "
"for the underlying storage mechanism")
parser.add_argument("-f", "--format",
choices=["casa", "zarr", "parquet"],
default="zarr",
help="Output format")
parser.add_argument("--force",
action="store_true",
default=False,
help="Force overwrite of output")
parser.add_argument("-c", "--chunks",
default="{row: 10000}",
help=("chunking schema applied to each dataset "
"e.g. {row: 1000, chan: 16, corr: 1}"),
type=parse_chunks)
def execute(self):
import dask
if self.args.output.exists():
if self.args.force:
shutil.rmtree(self.args.output)
else:
raise ValueError(f"{self.args.output} exists. "
f"Use --force to overwrite.")
writes = convert_table(self.args)
dask.compute(writes)