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

Write point map with cdshealpix skymap #409

Merged
merged 4 commits into from
Nov 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
22 changes: 8 additions & 14 deletions src/hats/io/file_io/file_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import tempfile
from collections.abc import Generator
from pathlib import Path
from typing import Any, Tuple
Expand All @@ -10,10 +9,10 @@
import pyarrow.dataset as pds
import pyarrow.parquet as pq
import yaml
from cdshealpix.skymap.skymap import Skymap
from pyarrow.dataset import Dataset
from upath import UPath

import hats.pixel_math.healpix_shim as hp
from hats.io.file_io.file_pointer import get_upath


Expand Down Expand Up @@ -213,21 +212,18 @@ def write_parquet_metadata(
)


def read_fits_image(map_file_pointer: str | Path | UPath):
def read_fits_image(map_file_pointer: str | Path | UPath) -> np.ndarray:
"""Read the object spatial distribution information from a healpix FITS file.

Args:
file_pointer: location of file to be written
map_file_pointer (path-like): location of file to be written
camposandro marked this conversation as resolved.
Show resolved Hide resolved

Returns:
one-dimensional numpy array of integers where the
value at each index corresponds to the number of objects found at the healpix pixel.
"""
map_file_pointer = get_upath(map_file_pointer)
with tempfile.NamedTemporaryFile() as _tmp_file:
with map_file_pointer.open("rb") as _map_file:
map_data = _map_file.read()
_tmp_file.write(map_data)
return hp.read_map(_tmp_file.name, nest=True)
return Skymap.from_fits(map_file_pointer).values
camposandro marked this conversation as resolved.
Show resolved Hide resolved


def write_fits_image(histogram: np.ndarray, map_file_pointer: str | Path | UPath):
Expand All @@ -236,13 +232,11 @@ def write_fits_image(histogram: np.ndarray, map_file_pointer: str | Path | UPath
Args:
histogram (:obj:`np.ndarray`): one-dimensional numpy array of long integers where the
value at each index corresponds to the number of objects found at the healpix pixel.
file_pointer: location of file to be written
map_file_pointer (path-like): location of file to be written
"""
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.int32, nest=True, coord="CEL")
_map_file.write(_tmp_file.read())
skymap = Skymap.from_array(histogram)
skymap.to_fits(map_file_pointer)


def read_yaml(file_handle: str | Path | UPath):
Expand Down
12 changes: 12 additions & 0 deletions tests/hats/io/file_io/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import pandas as pd
import pytest

from hats.io import paths
from hats.io.file_io import (
delete_file,
load_csv_to_pandas,
load_csv_to_pandas_generator,
make_directory,
read_fits_image,
read_parquet_dataset,
read_parquet_file_to_pandas,
remove_directory,
write_dataframe_to_csv,
write_fits_image,
write_string_to_file,
)
from hats.io.file_io.file_pointer import does_file_or_directory_exist
Expand Down Expand Up @@ -123,3 +126,12 @@ def test_read_parquet_dataset(small_sky_dir, small_sky_order1_dir):
)

assert ds.count_rows() == 131


def test_write_point_map_roundtrip(small_sky_order1_dir, tmp_path):
"""Test the reading/writing of a catalog point map"""
expected_counts_skymap = read_fits_image(paths.get_point_map_file_pointer(small_sky_order1_dir))
output_map_pointer = paths.get_point_map_file_pointer(tmp_path)
write_fits_image(expected_counts_skymap, output_map_pointer)
counts_skymap = read_fits_image(output_map_pointer)
np.testing.assert_array_equal(counts_skymap, expected_counts_skymap)