-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add point cloud support to_spherical
- Loading branch information
Showing
4 changed files
with
139 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""Unit-tests for :func:`geovista.common.to_spherical`.""" | ||
import numpy as np | ||
import numpy.typing as npt | ||
import pytest | ||
|
||
from geovista.common import DISTANCE_DECIMALS, RADIUS, ZLEVEL_SCALE, to_spherical | ||
|
||
|
||
def _distance( | ||
pts: npt.ArrayLike, stacked: bool = True, decimals: int = DISTANCE_DECIMALS | ||
) -> float: | ||
"""Calculate the mean distance to the xyz-cartesian `pts` from the origin.""" | ||
if np.ma.isMaskedArray(pts): | ||
pts = pts.data | ||
if not stacked: | ||
pts = np.transpose(pts) | ||
nrow, ncol = pts.shape | ||
result = np.round( | ||
np.sqrt(np.sum(pts.T @ pts * np.identity(ncol)) / nrow), decimals=decimals | ||
) | ||
return result | ||
|
||
|
||
def test_shape_fail(): | ||
"""Test trap of longitude and latitude shape mismatch.""" | ||
lons, lats = np.arange(10), np.arange(10).reshape(5, 2) | ||
emsg = "Require longitudes and latitudes with same shape" | ||
with pytest.raises(ValueError, match=emsg): | ||
_ = to_spherical(lons, lats) | ||
|
||
|
||
def test_zlevel_broadcast_fail(): | ||
"""Test trap of zlevel shape can't broadcast with longitude/latitude.""" | ||
lons, lats = np.arange(10), np.arange(10) | ||
zlevel = np.arange(2) | ||
emsg = "Cannot broadcast zlevel" | ||
with pytest.raises(ValueError, match=emsg): | ||
_ = to_spherical(lons, lats, zlevel=zlevel) | ||
|
||
|
||
@pytest.mark.parametrize("stacked", [True, False]) | ||
def test_defaults(lam_uk_sample, stacked): | ||
"""Test expected defaults are honoured.""" | ||
result = to_spherical(*lam_uk_sample, stacked=stacked) | ||
assert _distance(result, stacked=stacked) == RADIUS | ||
|
||
|
||
@pytest.mark.parametrize("zlevel", range(-5, 6)) | ||
def test_zlevel__scalar(lam_uk_sample, zlevel): | ||
"""Test spherical z-control with zlevel.""" | ||
result = to_spherical(*lam_uk_sample, zlevel=zlevel) | ||
actual = _distance(result) | ||
expected = RADIUS + RADIUS * zlevel * ZLEVEL_SCALE | ||
assert np.isclose(actual, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"xy_reshape, z_reshape", [((-1,), (-1, 1)), ((1, 5, 5), (-1, 1, 1))] | ||
) | ||
@pytest.mark.parametrize("n_levels", range(3, 11)) | ||
def test_zlevel__broadcast(lam_uk_sample, xy_reshape, z_reshape, n_levels): | ||
"""Test spherical z-control with zlevel broadcast.""" | ||
lons, lats = lam_uk_sample | ||
(npts,) = lons.shape | ||
vlons = np.vstack([lons[:].reshape(*xy_reshape)] * n_levels) | ||
vlats = np.vstack([lats[:].reshape(*xy_reshape)] * n_levels) | ||
mid = n_levels // 2 | ||
zlevel = np.arange(-mid, n_levels - mid) | ||
result = to_spherical(vlons, vlats, zlevel=zlevel.reshape(*z_reshape)) | ||
assert result.ndim == 2 | ||
assert result.shape[0] == n_levels * npts | ||
for i in range(n_levels): | ||
actual = _distance(result[i * npts : (i + 1) * npts]) | ||
expected = RADIUS + RADIUS * zlevel[i] * ZLEVEL_SCALE | ||
assert np.isclose(actual, expected) | ||
|
||
|
||
@pytest.mark.parametrize("zlevel", [0, 1]) | ||
@pytest.mark.parametrize("zscale", np.linspace(-1, 1, num=5)) | ||
def test_zscale(lam_uk_sample, zlevel, zscale): | ||
"""Test spherical z-control with zscale.""" | ||
lons, lats = lam_uk_sample | ||
(npts,) = lons.shape | ||
result = to_spherical(lons, lats, zlevel=zlevel, zscale=zscale) | ||
assert result.ndim == 2 | ||
assert result.shape[0] == npts | ||
actual = _distance(result) | ||
expected = RADIUS + RADIUS * zlevel * zscale | ||
assert np.isclose(actual, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters