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

Improve reading/writing of fits file. #361

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 6 additions & 2 deletions src/hats/catalog/dataset/table_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@
if isinstance(str_value, str):
# Split on a few kinds of delimiters (just to be safe), and remove duplicates
return list(filter(None, re.split(";| |,|\n", str_value)))
return str_value
if str_value:
return str_value
## Convert empty strings and empty lists to None
return None

Check warning on line 136 in src/hats/catalog/dataset/table_properties.py

View check run for this annotation

Codecov / codecov/patch

src/hats/catalog/dataset/table_properties.py#L136

Added line #L136 was not covered by tests

@field_serializer("default_columns", "extra_columns")
def serialize_as_space_delimited_list(self, str_list: Iterable[str]) -> str:
"""Convert a python list of strings into a space-delimited string."""
if str_list is None:
if str_list is None or len(str_list) == 0:
return None
return " ".join(str_list)

Expand Down Expand Up @@ -169,6 +172,7 @@
return self

def copy_and_update(self, **kwargs):
"""Create a validated copy of these table properties, updating the fields provided in kwargs."""
new_properties = self.model_copy(update=kwargs)
TableProperties.model_validate(new_properties)
return new_properties
Expand Down
15 changes: 2 additions & 13 deletions src/hats/io/file_io/file_io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import tempfile
import warnings
from collections.abc import Generator
from pathlib import Path
from typing import Any, Tuple
Expand Down Expand Up @@ -228,17 +227,7 @@
with map_file_pointer.open("rb") as _map_file:
map_data = _map_file.read()
_tmp_file.write(map_data)
map_fits_image = hp.read_map(_tmp_file.name, nest=True, h=True)
header_dict = dict(map_fits_image[1])
if header_dict["ORDERING"] != "NESTED":
warnings.warn(
"point_map.fits file written in RING ordering, due to "
"https://github.com/astronomy-commons/hats/issues/271. "
"Converting to NESTED."
)
map_fits_image = hp.read_map(_tmp_file.name)
return map_fits_image
return map_fits_image[0]
return hp.read_map(_tmp_file.name, nest=True)


def write_fits_image(histogram: np.ndarray, map_file_pointer: str | Path | UPath):
Expand All @@ -252,7 +241,7 @@
map_file_pointer = get_upath(map_file_pointer)
with tempfile.NamedTemporaryFile() as _tmp_file:
with map_file_pointer.open("wb") as _map_file:
hp.write_map(_tmp_file.name, histogram, overwrite=True, dtype=np.int64, nest=True)
hp.write_map(_tmp_file.name, histogram, overwrite=True, dtype=np.int32, nest=True, coord="CEL")

Check warning on line 244 in src/hats/io/file_io/file_io.py

View check run for this annotation

Codecov / codecov/patch

src/hats/io/file_io/file_io.py#L244

Added line #L244 was not covered by tests
_map_file.write(_tmp_file.read())


Expand Down
4 changes: 2 additions & 2 deletions tests/data/small_sky/partition_info.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Norder,Npix,Dir
0,11,0
Norder,Npix
0,11
Binary file modified tests/data/small_sky/point_map.fits
Binary file not shown.
21 changes: 14 additions & 7 deletions tests/data/small_sky/properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
obs_collection = small_sky
dataproduct_type = object
hats_col_j2000_ra = ra
hats_col_j2000_dec = dec
hats_max_rows = 1000
hats_nrows = 131
hats_order = 0
#HATS catalog
obs_collection=small_sky
dataproduct_type=object
hats_nrows=131
hats_col_j2000_ra=ra
hats_col_j2000_dec=dec
hats_max_rows=1000000
hats_order=0
moc_sky_fraction=0.08333
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:32UTC
hats_estsize=98328
hats_release_date=2024-09-18
hats_version=v0.1
10 changes: 5 additions & 5 deletions tests/data/small_sky_order1/partition_info.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Norder,Npix,Dir
1,44,0
1,45,0
1,46,0
1,47,0
Norder,Npix
1,44
1,45
1,46
1,47
Binary file modified tests/data/small_sky_order1/point_map.fits
Binary file not shown.
21 changes: 14 additions & 7 deletions tests/data/small_sky_order1/properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
obs_collection = small_sky_order1
dataproduct_type = object
hats_col_j2000_ra = ra
hats_col_j2000_dec = dec
hats_max_rows = 1000
hats_nrows = 131
hats_order = 0
#HATS catalog
obs_collection=small_sky_order1
dataproduct_type=object
hats_nrows=131
hats_col_j2000_ra=ra
hats_col_j2000_dec=dec
hats_max_rows=1000000
hats_order=1
moc_sky_fraction=0.08333
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:32UTC
hats_estsize=48
hats_release_date=2024-09-18
hats_version=v0.1
17 changes: 11 additions & 6 deletions tests/data/small_sky_order1_id_index/properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
obs_collection = small_sky_order1_id_index
dataproduct_type = index
hats_max_rows = 1000
hats_nrows = 131
hats_order = 0
#HATS catalog
obs_collection=small_sky_order1_id_index
dataproduct_type=index
hats_nrows=131
hats_primary_table_url=small_sky
hats_index_column=id
hats_primary_table_url=small_sky
hats_index_extra_column=
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:35UTC
hats_estsize=8
hats_release_date=2024-09-18
hats_version=v0.1
12 changes: 6 additions & 6 deletions tests/data/small_sky_order1_margin/partition_info.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Norder,Npix,Dir
0,4,0
1,44,0
1,45,0
1,46,0
1,47,0
Norder,Npix
0,4
1,44
1,45
1,46
1,47
22 changes: 14 additions & 8 deletions tests/data/small_sky_order1_margin/properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
obs_collection = small_sky_order1_margin
dataproduct_type = margin
hats_col_j2000_ra = ra
hats_col_j2000_dec = dec
hats_max_rows = 1000
hats_nrows = 28
hats_order = 0
#HATS catalog
obs_collection=small_sky_order1_margin
dataproduct_type=margin
hats_nrows=28
hats_col_j2000_ra=ra
hats_col_j2000_dec=dec
hats_primary_table_url=small_sky_order1
hats_margin_threshold=7200
hats_margin_threshold=7200.0
hats_order=1
moc_sky_fraction=0.16667
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:32UTC
hats_estsize=57
hats_release_date=2024-09-18
hats_version=v0.1
30 changes: 15 additions & 15 deletions tests/data/small_sky_source/partition_info.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Norder,Npix,Dir
0,4,0
1,47,0
2,176,0
2,177,0
2,178,0
2,179,0
2,180,0
2,181,0
2,182,0
2,183,0
2,184,0
2,185,0
2,186,0
2,187,0
Norder,Npix
0,4
1,47
2,176
2,177
2,178
2,179
2,180
2,181
2,182
2,183
2,184
2,185
2,186
2,187
Binary file modified tests/data/small_sky_source/point_map.fits
Binary file not shown.
21 changes: 14 additions & 7 deletions tests/data/small_sky_source/properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
obs_collection = small_sky_source
dataproduct_type = source
hats_col_j2000_ra = source_ra
hats_col_j2000_dec = source_dec
hats_max_rows = 1000
hats_nrows = 17161
hats_order = 0
#HATS catalog
obs_collection=small_sky_source
dataproduct_type=source
hats_nrows=17161
hats_col_j2000_ra=source_ra
hats_col_j2000_dec=source_dec
hats_max_rows=3000
hats_order=2
moc_sky_fraction=0.16667
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:33UTC
hats_estsize=99328
hats_release_date=2024-09-18
hats_version=v0.1
16 changes: 10 additions & 6 deletions tests/data/small_sky_source_object_index/properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
obs_collection = small_sky_source_object_index
dataproduct_type = index
hats_max_rows = 1000
hats_nrows = 148
hats_order = 0
#HATS catalog
obs_collection=small_sky_source_object_index
dataproduct_type=index
hats_nrows=148
hats_primary_table_url=small_sky_source
hats_index_column=object_id
hats_primary_table_url=small_sky_source
hats_builder=hats-import v0.3.6.dev22+g6346e4d
hats_creation_date=2024-09-30T13\:44UTC
hats_estsize=10
hats_release_date=2024-09-18
hats_version=v0.1
Binary file modified tests/data/small_sky_to_small_sky_order1/_metadata
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/data/small_sky_to_small_sky_order1/partition_info.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Norder,Npix,Dir
0,11,0
Norder,Npix
0,11