Skip to content

Commit

Permalink
remove the rotation parameter of healpix (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis authored Oct 21, 2024
1 parent 655c619 commit ca19283
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 56 deletions.
16 changes: 2 additions & 14 deletions xdggs/healpix.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import operator
from collections.abc import Mapping
from dataclasses import dataclass, field
from dataclasses import dataclass
from typing import Any, ClassVar, Literal, TypeVar

try:
Expand Down Expand Up @@ -99,8 +99,6 @@ class HealpixInfo(DGGSInfo):

indexing_scheme: Literal["nested", "ring", "unique"] = "nested"

rotation: list[float, float] = field(default_factory=lambda: [0.0, 0.0])

valid_parameters: ClassVar[dict[str, Any]] = {
"resolution": range(0, 29 + 1),
"indexing_scheme": ["nested", "ring", "unique"],
Expand All @@ -115,11 +113,6 @@ def __post_init__(self):
f"indexing scheme must be one of {self.valid_parameters['indexing_scheme']}"
)

if np.any(np.isnan(self.rotation) | np.isinf(self.rotation)):
raise ValueError(
f"rotation must consist of finite values, got {self.rotation}"
)

@property
def nside(self: Self) -> int:
return 2**self.resolution
Expand Down Expand Up @@ -149,10 +142,6 @@ def translate_nside(nside):
"level": ("resolution", identity),
"depth": ("resolution", identity),
"nest": ("indexing_scheme", lambda nest: "nested" if nest else "ring"),
"rot_latlon": (
"rotation",
lambda rot_latlon: (rot_latlon[1], rot_latlon[0]),
),
}

def translate(name, value):
Expand Down Expand Up @@ -191,7 +180,6 @@ def to_dict(self: Self) -> dict[str, Any]:
"grid_name": "healpix",
"resolution": self.resolution,
"indexing_scheme": self.indexing_scheme,
"rotation": self.rotation,
}

def cell_ids2geographic(self, cell_ids):
Expand Down Expand Up @@ -261,4 +249,4 @@ def grid_info(self) -> HealpixInfo:
return self._grid

def _repr_inline_(self, max_width: int):
return f"HealpixIndex(nside={self._grid.resolution}, indexing_scheme={self._grid.indexing_scheme}, rotation={self._grid.rotation!r})"
return f"HealpixIndex(nside={self._grid.resolution}, indexing_scheme={self._grid.indexing_scheme})"
57 changes: 15 additions & 42 deletions xdggs/tests/test_healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class strategies:
lambda x: x not in ["nested", "ring", "unique"]
)

def rotations():
lon_rotation = st.floats(min_value=-180.0, max_value=360.0)
lat_rotation = st.floats(min_value=-90.0, max_value=90.0)
return st.tuples(lon_rotation, lat_rotation)

dims = xrst.names()

@classmethod
Expand All @@ -46,16 +41,13 @@ def grid_mappings(cls):
"order": cls.resolutions,
"indexing_scheme": cls.indexing_schemes,
"nest": st.booleans(),
"rotation": cls.rotations(),
"rot_latlon": cls.rotations().map(lambda x: x[::-1]),
}

names = {
"resolution": st.sampled_from(
["resolution", "nside", "depth", "level", "order"]
),
"indexing_scheme": st.sampled_from(["indexing_scheme", "nest"]),
"rotation": st.sampled_from(["rotation", "rot_latlon"]),
}

return st.builds(lambda **x: list(x.values()), **names).flatmap(
Expand All @@ -81,21 +73,18 @@ def cell_ids(max_value=None, dtypes=None):
def grids(
resolutions=resolutions,
indexing_schemes=indexing_schemes,
rotations=rotations(),
):
return st.builds(
healpix.HealpixInfo,
resolution=resolutions,
indexing_scheme=indexing_schemes,
rotation=rotations,
)

@classmethod
def grid_and_cell_ids(
cls,
resolutions=resolutions,
indexing_schemes=indexing_schemes,
rotations=rotations(),
dtypes=None,
):
cell_resolutions = st.shared(resolutions, key="common-resolutions")
Expand All @@ -108,7 +97,6 @@ def grid_and_cell_ids(
grids_ = cls.grids(
resolutions=grid_resolutions,
indexing_schemes=indexing_schemes,
rotations=rotations,
)

return cell_ids_, grids_
Expand All @@ -124,7 +112,6 @@ def grid_and_cell_ids(
"grid_name": "healpix",
"resolution": 0,
"indexing_scheme": "nested",
"rotation": (0, 0),
},
),
xr.Variable(
Expand All @@ -134,7 +121,6 @@ def grid_and_cell_ids(
"grid_name": "healpix",
"resolution": 0,
"indexing_scheme": "ring",
"rotation": (0, 0),
},
),
xr.Variable(
Expand All @@ -144,7 +130,6 @@ def grid_and_cell_ids(
"grid_name": "healpix",
"resolution": 1,
"indexing_scheme": "nested",
"rotation": (0, 0),
},
),
xr.Variable(
Expand All @@ -154,7 +139,6 @@ def grid_and_cell_ids(
"grid_name": "healpix",
"resolution": 3,
"indexing_scheme": "nested",
"rotation": (0, 0),
},
),
]
Expand All @@ -177,15 +161,14 @@ def test_init_invalid_indexing_scheme(self, indexing_scheme):
indexing_scheme=indexing_scheme,
)

@given(strategies.resolutions, strategies.indexing_schemes, strategies.rotations())
def test_init(self, resolution, indexing_scheme, rotation):
@given(strategies.resolutions, strategies.indexing_schemes)
def test_init(self, resolution, indexing_scheme):
grid = healpix.HealpixInfo(
resolution=resolution, indexing_scheme=indexing_scheme, rotation=rotation
resolution=resolution, indexing_scheme=indexing_scheme
)

assert grid.resolution == resolution
assert grid.indexing_scheme == indexing_scheme
assert grid.rotation == rotation

@given(strategies.resolutions)
def test_nside(self, resolution):
Expand All @@ -211,26 +194,24 @@ def test_nest(self, indexing_scheme):
def test_from_dict(self, mapping) -> None:
healpix.HealpixInfo.from_dict(mapping)

@given(strategies.resolutions, strategies.indexing_schemes, strategies.rotations())
def test_to_dict(self, resolution, indexing_scheme, rotation) -> None:
@given(strategies.resolutions, strategies.indexing_schemes)
def test_to_dict(self, resolution, indexing_scheme) -> None:
grid = healpix.HealpixInfo(
resolution=resolution, indexing_scheme=indexing_scheme, rotation=rotation
resolution=resolution, indexing_scheme=indexing_scheme
)
actual = grid.to_dict()

assert set(actual) == {"grid_name", "resolution", "indexing_scheme", "rotation"}
assert set(actual) == {"grid_name", "resolution", "indexing_scheme"}
assert actual["grid_name"] == "healpix"
assert actual["resolution"] == resolution
assert actual["indexing_scheme"] == indexing_scheme
assert actual["rotation"] == rotation

@given(strategies.resolutions, strategies.indexing_schemes, strategies.rotations())
def test_roundtrip(self, resolution, indexing_scheme, rotation):
@given(strategies.resolutions, strategies.indexing_schemes)
def test_roundtrip(self, resolution, indexing_scheme):
mapping = {
"grid_name": "healpix",
"resolution": resolution,
"indexing_scheme": indexing_scheme,
"rotation": rotation,
}

grid = healpix.HealpixInfo.from_dict(mapping)
Expand Down Expand Up @@ -404,31 +385,29 @@ def test_geographic2cell_ids(
["mapping", "expected"],
(
pytest.param(
{"resolution": 10, "indexing_scheme": "nested", "rotation": (0.0, 0.0)},
{"resolution": 10, "indexing_scheme": "nested", "rotation": (0.0, 0.0)},
{"resolution": 10, "indexing_scheme": "nested"},
{"resolution": 10, "indexing_scheme": "nested"},
id="no_translation",
),
pytest.param(
{
"resolution": 10,
"indexing_scheme": "nested",
"rotation": (0.0, 0.0),
"grid_name": "healpix",
},
{"resolution": 10, "indexing_scheme": "nested", "rotation": (0.0, 0.0)},
{"resolution": 10, "indexing_scheme": "nested"},
id="no_translation-grid_name",
),
pytest.param(
{"nside": 1024, "indexing_scheme": "nested", "rotation": (0.0, 0.0)},
{"resolution": 10, "indexing_scheme": "nested", "rotation": (0.0, 0.0)},
{"nside": 1024, "indexing_scheme": "nested"},
{"resolution": 10, "indexing_scheme": "nested"},
id="nside-alone",
),
pytest.param(
{
"nside": 1024,
"resolution": 10,
"indexing_scheme": "nested",
"rotation": (0.0, 0.0),
},
ExceptionGroup(
"received multiple values for parameters",
Expand All @@ -445,7 +424,6 @@ def test_geographic2cell_ids(
"resolution": 10,
"indexing_scheme": "nested",
"nest": True,
"rotation": (0.0, 0.0),
},
ExceptionGroup(
"received multiple values for parameters",
Expand All @@ -463,7 +441,6 @@ def test_geographic2cell_ids(
"resolution": 10,
"indexing_scheme": "nested",
"nest": True,
"rotation": (0.0, 0.0),
},
ExceptionGroup(
"received multiple values for parameters",
Expand Down Expand Up @@ -514,15 +491,13 @@ def test_grid(self, grid):
def test_from_variables(variable_name, variable, options) -> None:
expected_resolution = variable.attrs["resolution"]
expected_scheme = variable.attrs["indexing_scheme"]
expected_rot = variable.attrs["rotation"]

variables = {variable_name: variable}

index = healpix.HealpixIndex.from_variables(variables, options=options)

assert index._grid.resolution == expected_resolution
assert index._grid.indexing_scheme == expected_scheme
assert index._grid.rotation == expected_rot

assert (index._dim,) == variable.dims
np.testing.assert_equal(index._pd_index.index.values, variable.data)
Expand Down Expand Up @@ -552,9 +527,7 @@ def test_replace(old_variable, new_variable) -> None:
@pytest.mark.parametrize("max_width", [20, 50, 80, 120])
@pytest.mark.parametrize("resolution", [0, 1, 3])
def test_repr_inline(resolution, max_width) -> None:
grid_info = healpix.HealpixInfo(
resolution=resolution, indexing_scheme="nested", rotation=(0, 0)
)
grid_info = healpix.HealpixInfo(resolution=resolution, indexing_scheme="nested")
index = healpix.HealpixIndex(cell_ids=[0], dim="cells", grid_info=grid_info)

actual = index._repr_inline_(max_width)
Expand Down

0 comments on commit ca19283

Please sign in to comment.