Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZTF matchfiles: imap_unordered + doc cleanup fix #270

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kowalski/ingesters/ingest_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def process_file(argument_list: Sequence):
mongo.insert_many(collection=collection, documents=batch)

elif format == "parquet":
df = pq.read_table(file).to_pandas()
df: pd.DataFrame = pq.read_table(file).to_pandas()
for name in list(df.columns):
if name.startswith("_"):
df.rename(columns={name: name[1:]}, inplace=True)
Expand Down
37 changes: 29 additions & 8 deletions kowalski/ingesters/ingest_ztf_matchfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,30 @@ def process_file(argument_list: Sequence):

def clean_up_document(document):
"""Format passed in dicts for Mongo insertion"""
# we found some edge cases where the documents already had a "coordinates" field
# which was empty. If that happens, remove it
if (
"coordinates" in document
and type(document["coordinates"]) == dict
and len(document["coordinates"]) == 0
):
document.pop("coordinates")
# convert types for pymongo:
for k, v in document.items():
if k != "data":
if k in sources_int_fields:
document[k] = int(document[k])
else:
document[k] = float(document[k])
if k not in ("ra", "dec"):
# this will save a lot of space:
document[k] = round(document[k], 3)
try:
if k != "data":
if k in sources_int_fields:
document[k] = int(document[k])
else:
document[k] = float(document[k])
if k not in ("ra", "dec"):
# this will save a lot of space:
document[k] = round(document[k], 3)
except Exception as e:
log(
f"Failed to convert {k} to int or float: {e}, with value {str(v)}"
)
raise e

# generate unique _id:
document["_id"] = baseid + document["matchid"]
Expand All @@ -176,6 +190,8 @@ def clean_up_document(document):
document["coordinates"] = dict()
_ra = document["ra"]
_dec = document["dec"]
if _ra == 360.0:
_ra = 0.0
_radec_str = [deg2hms(_ra), deg2dms(_dec)]
document["coordinates"]["radec_str"] = _radec_str
# for GeoJSON, must be lon:[-180, 180], lat:[-90, 90] (i.e. in deg)
Expand Down Expand Up @@ -516,6 +532,11 @@ def run(
for _ in tqdm(pool.imap(process_file, input_list), total=len(files)):
pass

with multiprocessing.Pool(processes=num_proc) as pool:
with tqdm(total=len(files)) as pbar:
for _ in pool.imap_unordered(process_file, input_list):
pbar.update(1)


if __name__ == "__main__":
fire.Fire(run)
Loading