-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
274 lines (244 loc) · 9.8 KB
/
main.py
File metadata and controls
274 lines (244 loc) · 9.8 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
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
# for fast debugging run:
# python ./main.py --input=https://users.flatironinstitute.org/~polymathic/data/MultimodalUniverse/v1/sdss/sdss/healpix=583/ --output=./hats --name=mmu_sdss_sdss --tmp-dir=./tmp --max-rows=8192
import argparse
import importlib
import logging
import math
from multiprocessing import cpu_count
from pkgutil import walk_packages
import h5py
import numpy as np
import pyarrow as pa
from dask.distributed import Client
from hats_import import CollectionArguments
from hats_import.catalog.file_readers import InputReader
from hats_import.pipeline import pipeline_with_client
from upath import UPath
import catalog_functions
from catalog_functions.utils import BaseTransformer
LOGGER = logging.getLogger(__name__)
def available_transformers():
transformers = []
for module_info in walk_packages(
catalog_functions.__path__, catalog_functions.__name__ + "."
):
*_, module_name = module_info.name.rsplit(".", maxsplit=1)
if not module_name.endswith("_transformer"):
continue
name = module_name.removesuffix("_transformer")
transformers.append(name)
return transformers
def get_transformer(name):
module = importlib.import_module(
catalog_functions.__name__ + f".{name}_transformer"
)
classes = []
for module_attr in dir(module):
obj = getattr(module, module_attr)
if (
isinstance(obj, type)
and issubclass(obj, BaseTransformer)
and obj != BaseTransformer
):
classes.append(obj)
if len(classes) == 0:
raise ValueError("No transformers found")
if len(classes) >= 2:
raise ValueError(f"More than one transformer found: {classes}")
return classes[0]
def parse_args(argv):
parser = argparse.ArgumentParser("Convert MMU dataset to HATS")
parser.add_argument(
"-c",
"--transformer",
required=True,
type=str,
help="one of the available catalog transformers",
choices=available_transformers(),
)
parser.add_argument("-n", "--name", required=True, help="HATS catalog name")
parser.add_argument(
"-i", "--input", required=True, type=UPath, help="Input MMU dataset URI"
)
parser.add_argument(
"-o",
"--output",
required=True,
type=UPath,
help="Output HATS URI, [NAME] directory will be created inside it",
)
parser.add_argument(
"-r",
"--max-rows",
required=True,
type=int,
help="Max number of rows per HATS partition",
)
parser.add_argument(
"-t",
"--tmp-dir",
default=UPath("./tmp"),
type=UPath,
help="Temporary directory path",
)
parser.add_argument("--ra", default="ra", help="Right ascension column name")
parser.add_argument("--dec", default="dec", help="Declination column name")
parser.add_argument(
"--first-n",
default=None,
type=int,
help="First N files only, useful for debugging",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug mode (single worker, single thread, no separate processes)",
)
parser.add_argument(
"--row-group-size",
default=None,
type=int,
help="Row group size for parquet files. If not specified, uses PyArrow's default (min of table size and 1M rows). For image data, try 50-250.",
)
return parser.parse_args(argv)
def np_to_pyarrow_array(array: np.ndarray) -> pa.Array:
"""Massively copy-pasted from hats_import.catalog.file_reader.fits._np_to_pyarrow_array
https://github.com/astronomy-commons/hats-import/blob/e9c7b647dae309ced9f9ce2916692c2aecde2612/src/hats_import/catalog/file_readers/fits.py#L9
"""
if array.dtype.byteorder == '>':
array = array.byteswap().view(array.dtype.newbyteorder('<'))
values = pa.array(array.reshape(-1))
# "Base" type
if array.ndim == 1:
return values
if array.ndim > 2:
raise ValueError("Only 1D and 2D arrays are supported")
n_lists, length = array.shape
offsets = np.arange(0, (n_lists + 1) * length, length, dtype=np.int32)
return pa.ListArray.from_arrays(values=values, offsets=offsets)
class MMUReader(InputReader):
def __init__(self, chunk_mb: float, transform_klass):
super().__init__()
self.chunk_bytes = chunk_mb * 1024 * 1024
self.transform = transform_klass()
def _get_h5_column(self, h5_file, col_name):
"""Get column from HDF5 file, checking both cases for ra/dec."""
if col_name in h5_file:
return col_name
# Try uppercase for coordinate columns
if col_name.lower() in ("ra", "dec"):
upper_name = col_name.upper()
if upper_name in h5_file:
return upper_name
raise KeyError(f"Column '{col_name}' not found in HDF5 file. "
f"Available columns: {list(h5_file.keys())}")
def _num_chunks(self, upath, h5_file: h5py.File, columns: list[str] | None) -> int:
if columns is None:
size = upath.stat().st_size
else:
size = sum(h5_file[self._get_h5_column(h5_file, col)].nbytes for col in columns)
return max(1, int(math.ceil(size / self.chunk_bytes)))
def read(self, input_file: str, read_columns: list[str] | None = None):
upath = UPath(input_file)
cols_scalar = {}
with upath.open("rb") as fh, h5py.File(fh) as h5_file:
num_chunks = self._num_chunks(upath, h5_file, read_columns)
if read_columns is None:
read_columns = list(h5_file)
first_col_name = self._get_h5_column(h5_file, read_columns[0])
shape = h5_file[first_col_name].shape
if shape == ():
cols_scalar[read_columns[0]] = True
n_rows = 1
else:
cols_scalar[read_columns[0]] = False
n_rows = shape[0]
for col in read_columns[1:]:
col_name = self._get_h5_column(h5_file, col)
shape = h5_file[col_name].shape
if shape == ():
cols_scalar[col] = True
else:
cols_scalar[col] = False
chunk_size = max(1, n_rows // num_chunks)
for i in range(0, n_rows, chunk_size):
if set([col.lower() for col in read_columns]) == set(["ra", "dec"]):
if cols_scalar[first_col_name]:
data = {
col: np_to_pyarrow_array(np.array([h5_file[self._get_h5_column(h5_file, col)][()]]))
for col in read_columns
}
else:
data = {
col: np_to_pyarrow_array(h5_file[self._get_h5_column(h5_file, col)][i : i + chunk_size])
for col in read_columns
}
table = pa.table(data)
yield table
else:
if cols_scalar[first_col_name]:
data = h5_file
else:
data = {}
for col in read_columns:
if cols_scalar[col]:
data[col] = h5_file[col][()]
else:
data[col] = h5_file[col][i : i + chunk_size]
table = self.transform.dataset_to_table(data)
yield table
def input_file_list(path: UPath) -> list[str]:
suffixes = {".h5", ".hdf5"}
path_list = sorted(p for p in path.rglob("*.h*5") if p.suffix in suffixes)
return [upath.path for upath in path_list]
def main(argv=None):
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
cmd_args = parse_args(argv)
# https://github.com/astronomy-commons/hats-import/issues/624
cmd_args.tmp_dir.mkdir(parents=True, exist_ok=True)
transformer_klass = get_transformer(cmd_args.transformer)
input_files = input_file_list(cmd_args.input)
if cmd_args.first_n is not None:
input_files = input_files[: cmd_args.first_n]
row_group_kwargs = {}
if cmd_args.row_group_size is not None:
row_group_kwargs['num_rows'] = cmd_args.row_group_size
import_args = (
CollectionArguments(
output_artifact_name=cmd_args.name,
output_path=cmd_args.output,
tmp_dir=cmd_args.tmp_dir,
)
.catalog(
input_file_list=input_files,
file_reader=MMUReader(chunk_mb=128, transform_klass=transformer_klass),
ra_column=cmd_args.ra,
dec_column=cmd_args.dec,
pixel_threshold=cmd_args.max_rows,
lowest_healpix_order=4,
row_group_kwargs=row_group_kwargs,
)
.add_margin(margin_threshold=10.0, is_default=True)
)
# Choose Client configuration based on debug flag
if cmd_args.debug:
# Debug mode: use 1 worker and 1 thread for easier debugging with breakpoints
client_kwargs = {"n_workers": 1, "threads_per_worker": 1, "processes": False}
LOGGER.info(
"Running in DEBUG mode (single worker, single thread, no separate processes)"
)
else:
# Production mode: use multiple workers
client_kwargs = {"n_workers": min(8, cpu_count()), "threads_per_worker": 1, "memory_limit": "48G"}
LOGGER.info(
f"Running in PRODUCTION mode ({client_kwargs['n_workers']} workers)"
)
with Client(**client_kwargs) as client:
LOGGER.info(f"Dask dashboard: {client.dashboard_link}")
pipeline_with_client(import_args, client)
if __name__ == "__main__":
main()