Geo encoders¶
Geophysical inputs usually arrive as longitude/latitude in degrees, while downstream neural-field and GP features typically want periodic encodings or unit-sphere coordinates. The geo encoders in pyrox_nn make those preprocessing steps first-class and composable.
The canonical spherical-harmonic pipeline is:
import equinox as eqx
from pyrox_nn import (
Cartesian3DEncoder,
Deg2Rad,
SphericalHarmonicEncoder,
)
encoder = eqx.nn.Sequential(
[
Deg2Rad(),
Cartesian3DEncoder(input_unit="radians"),
SphericalHarmonicEncoder(l_max=8, input_mode="cartesian"),
]
)
features = encoder(lonlat_deg) # (N, 81)
Cartesian3DEncoder uses the same axis convention expected by pyrox_gp.SphericalHarmonicInducingFeatures, so the NN and GP spherical paths line up. For temporal complements, see fourier_features and seasonal_features.
Stateful encoder layers¶
Deg2Rad
¶
Bases: Module
Element-wise conversion from degrees to radians.
The map is y = π x / 180. It is a stateless wrapper around
jax.numpy.deg2rad with no learnable parameters.
Examples:
A vector of three angles keeps its shape:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import Deg2Rad
>>> Deg2Rad()(jnp.array([0.0, 90.0, 180.0])).shape
(3,)
A scalar converts to a scalar:
Batch with vmap over a matrix of angles:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
LonLatScale
¶
Bases: Module
Affine-rescale a single longitude and latitude pair into [-1, 1].
The map is y = 2 (x − lo) / (hi − lo) − 1 per component, with lo and
hi taken from the two ranges. Values outside the range are not clipped.
The default ranges assume degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lon_range
|
|
required | |
lat_range
|
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a range is not increasing. |
Examples:
A typical pair in degrees:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import LonLatScale
>>> LonLatScale()(jnp.array([180.0, 90.0])).shape
(2,)
A regional domain, with the midpoint mapped to zero:
>>> regional = LonLatScale(lon_range=(-10.0, 10.0), lat_range=(40.0, 60.0))
>>> bool(jnp.allclose(regional(jnp.array([0.0, 50.0])), 0.0))
True
Batch with vmap over a matrix of pairs:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
Cartesian3DEncoder
¶
Bases: Module
Lift a single longitude and latitude onto the unit sphere S².
The map is p = (cos ϕ cos λ, cos ϕ sin λ, sin ϕ). It is a stateless
wrapper around functax.geometry.lonlat_to_unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_unit
|
Whether the input is in |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
The point (0, 0) maps to the unit vector along x:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import Cartesian3DEncoder
>>> Cartesian3DEncoder()(jnp.array([0.0, 0.0])).shape
(3,)
Degrees and radians agree for the same point:
>>> deg = Cartesian3DEncoder(input_unit="degrees")(jnp.array([90.0, 0.0]))
>>> rad = Cartesian3DEncoder()(jnp.array([jnp.pi / 2, 0.0]))
>>> bool(jnp.allclose(deg, rad, atol=1e-6))
True
Batch with vmap over a matrix of pairs:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
CyclicEncoder
¶
Bases: Module
Encode a single periodic input as concatenated cosine and sine features.
The map is y = [cos θ, sin θ] for a scalar θ, and y = [cos θ, sin θ]
concatenated over the D components for a vector. It is a stateless
wrapper around geonnax.encoders.cyclic_encode.
Examples:
Batch with vmap over two scalar angles:
>>> import jax.numpy as jnp
>>> import jax
>>> from geonnax.encoders import CyclicEncoder
>>> jax.vmap(CyclicEncoder())(jnp.array([0.0, jnp.pi])).shape
(2, 2)
A single scalar gives two features:
A vector of three angles gives six features:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
SphericalHarmonicEncoder
¶
Bases: Module
Real spherical-harmonic features on the unit 2-sphere for a single point.
The features are y = Y(p) with M = (l_max + 1)² components, where p
is the point on the sphere. With input_mode="cartesian" the point is
given directly as p ∈ ℝ³. With input_mode="lonlat" it is given as
(λ, ϕ) in radians and lifted first. The map wraps
functax.basis.sphere.real_spherical_harmonics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l_max
|
Maximum harmonic degree |
required | |
input_mode
|
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
A Cartesian point at l_max = 3 gives (l_max + 1)² = 16 features:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import SphericalHarmonicEncoder
>>> SphericalHarmonicEncoder(l_max=3)(jnp.array([1.0, 0.0, 0.0])).shape
(16,)
A longitude and latitude pair in radians:
>>> enc = SphericalHarmonicEncoder(l_max=2, input_mode="lonlat")
>>> enc(jnp.array([0.0, 0.0])).shape
(9,)
Batch with vmap over a matrix of Cartesian points:
>>> import jax
>>> batched = jax.vmap(SphericalHarmonicEncoder(l_max=1))(jnp.eye(3))
>>> batched.shape
(3, 4)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
num_features: int
property
¶
Slepian encoders¶
Region-localized spherical encoders built on the Slepian concentration
problem. The deterministic SlepianEncoder and
HybridSphericalSlepianEncoder are re-exported from geonnax;
BayesianSlepianEncoder adds NumPyro sites over the cap radius and
centre.
SlepianEncoder
¶
Bases: Module
Deterministic Slepian-cap positional encoder on S².
Slepian functions are the band-limited modes maximally concentrated inside
a spherical cap. Each is a weighted sum of spherical harmonics up to
l_max, with concentration eigenvalue λ_k ∈ [0, 1]. With
weight_by_eigenvalue the encoder returns
φ_k(x) = √λ_k ψ_k(x),
where ψ_k is the k-th unweighted Slepian mode, so well-concentrated modes
dominate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basis
|
The retained Slepian cap basis, with its modes |
required | |
input_mode
|
|
required | |
weight_by_eigenvalue
|
Whether to scale mode |
required |
Examples:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import SlepianEncoder
>>> enc = SlepianEncoder.from_cap(
... l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=4,
... )
>>> enc(jnp.array([0.0, 0.0])).shape == (enc.num_features,)
True
Cartesian input on the unit sphere:
>>> enc_xyz = SlepianEncoder.from_cap(
... l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=4,
... input_mode="cartesian",
... )
>>> enc_xyz(jnp.array([1.0, 0.0, 0.0])).shape
(4,)
Composition with vmap over a batch of points:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_slepian.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
num_features: int
property
¶
from_cap(*, l_max: int, cap_radius_deg: float, cap_centre_lonlat_deg: tuple[float, float], eig_threshold: float = 0.05, n_modes: int | None = None, input_mode: Literal['cartesian', 'lonlat'] = 'lonlat', weight_by_eigenvalue: bool = True) -> SlepianEncoder
classmethod
¶
Construct an encoder from cap geometry specified in degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l_max
|
int
|
Maximum spherical-harmonic degree of the Slepian modes. |
required |
cap_radius_deg
|
float
|
Cap radius in degrees. |
required |
cap_centre_lonlat_deg
|
tuple[float, float]
|
Cap centre |
required |
eig_threshold
|
float
|
Keep only modes with |
0.05
|
n_modes
|
int | None
|
Maximum number of modes to keep, or |
None
|
input_mode
|
Literal['cartesian', 'lonlat']
|
|
'lonlat'
|
weight_by_eigenvalue
|
bool
|
Whether to scale mode |
True
|
Returns:
| Type | Description |
|---|---|
SlepianEncoder
|
A new |
Examples:
>>> from geonnax.encoders import SlepianEncoder
>>> enc = SlepianEncoder.from_cap(
... l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=4,
... )
>>> enc.num_features
4
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_slepian.py
HybridSphericalSlepianEncoder
¶
Bases: Module
Concatenate low-bandwidth global SHs with local Slepian cap modes.
The output is φ(x) = [Y_ℓᵐ(x) | ψ_k(x)], global structure from spherical
harmonics followed by localized detail from the cap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sh_encoder
|
Spherical-harmonic encoder giving the global block |
required | |
slepian_encoder
|
Slepian cap encoder giving the local block |
required |
Examples:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import HybridSphericalSlepianEncoder
>>> enc = HybridSphericalSlepianEncoder.from_cap(
... sh_l_max=1,
... slepian_l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=2,
... )
>>> enc(jnp.array([0.0, 0.0])).shape == (enc.num_features,)
True
Degree zero gives a single spherical harmonic:
>>> enc_l0 = HybridSphericalSlepianEncoder.from_cap(
... sh_l_max=0,
... slepian_l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=2,
... )
>>> enc_l0(jnp.array([0.0, 0.0])).shape
(3,)
Composition with vmap over a batch of points:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_slepian.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
num_features: int
property
¶
Number of concatenated output features (SH modes + Slepian modes).
Examples:
from_cap(*, sh_l_max: int, slepian_l_max: int, cap_radius_deg: float, cap_centre_lonlat_deg: tuple[float, float], eig_threshold: float = 0.05, n_modes: int | None = None, input_mode: Literal['cartesian', 'lonlat'] = 'lonlat') -> HybridSphericalSlepianEncoder
classmethod
¶
Construct the hybrid encoder used by Slepian positional encodings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sh_l_max
|
int
|
Maximum degree |
required |
slepian_l_max
|
int
|
Maximum spherical-harmonic degree of the Slepian modes. |
required |
cap_radius_deg
|
float
|
Cap radius in degrees. |
required |
cap_centre_lonlat_deg
|
tuple[float, float]
|
Cap centre |
required |
eig_threshold
|
float
|
Keep only Slepian modes with |
0.05
|
n_modes
|
int | None
|
Maximum number of Slepian modes, or |
None
|
input_mode
|
Literal['cartesian', 'lonlat']
|
|
'lonlat'
|
Returns:
| Type | Description |
|---|---|
HybridSphericalSlepianEncoder
|
A new |
Examples:
>>> from geonnax.encoders import HybridSphericalSlepianEncoder
>>> enc = HybridSphericalSlepianEncoder.from_cap(
... sh_l_max=1,
... slepian_l_max=3,
... cap_radius_deg=20.0,
... cap_centre_lonlat_deg=(0.0, 0.0),
... eig_threshold=0.0,
... n_modes=2,
... )
>>> # sh_l_max=1 gives (1 + 1)^2 = 4 SH features, plus 2 Slepian modes.
>>> enc.num_features
6
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_slepian.py
BayesianSlepianEncoder
¶
Bases: Parameterized
Slepian encoder with NumPyro sites for cap radius and centre.
Truncation is controlled by n_modes only. eig_threshold is not
exposed because filtering by concentration ratio under tracing would
require a data-dependent output size (jnp.nonzero needs a static
size), which breaks JIT and downstream shape contracts.
Source code in packages/pyrox-nn/src/pyrox_nn/_slepian.py
Pure-JAX helper functions¶
lonlat_scale(lonlat: Num[Array, 'N 2'], *, lon_range: tuple[float, float] = (-180.0, 180.0), lat_range: tuple[float, float] = (-90.0, 90.0)) -> Float[Array, 'N 2']
¶
Affine-rescale longitude and latitude columns into [-1, 1].
The affine map is y = 2 (x − lo) / (hi − lo) − 1 per column, so the
range [lo, hi] maps onto [-1, 1]. Values outside the range are not
clipped and map linearly outside [-1, 1]. The default ranges assume
degrees; pass matching lon_range and lat_range for another unit.
Integer inputs are promoted to float32 first, so the division is not
done in integer arithmetic (which would round the output to -1, 0
or 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lonlat
|
Num[Array, 'N 2']
|
Longitude and latitude matrix |
required |
lon_range
|
tuple[float, float]
|
|
(-180.0, 180.0)
|
lat_range
|
tuple[float, float]
|
|
(-90.0, 90.0)
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'N 2']
|
The rescaled matrix |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
Typical case, a batch of three points:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import lonlat_scale
>>> lonlat = jnp.array([[-180.0, -90.0], [0.0, 0.0], [180.0, 90.0]])
>>> lonlat_scale(lonlat).shape
(3, 2)
The midpoint of each range maps exactly to zero:
A regional grid in degrees, with its own domain:
>>> lonlat_scale(
... jnp.array([[0.0, 50.0]]),
... lon_range=(-10.0, 10.0),
... lat_range=(40.0, 60.0),
... ).shape
(1, 2)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
lonlat_to_cartesian3d(lonlat: Float[Array, 'N 2'], *, input_unit: Literal['degrees', 'radians'] = 'radians') -> Float[Array, 'N 3']
¶
Lift lon/lat coordinates onto the unit sphere.
functax.geometry.lonlat_to_unit with pyrox-nn's default unit: geonnax
served this name until it moved to functax (jejjohnson/geonnax#158).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lonlat
|
Float[Array, 'N 2']
|
Longitude and latitude, shape |
required |
input_unit
|
Literal['degrees', 'radians']
|
Whether |
'radians'
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'N 3']
|
Unit vectors |
Examples:
>>> import jax.numpy as jnp
>>> from pyrox_nn import lonlat_to_cartesian3d
>>> lonlat = jnp.array([[0.0, 90.0]]) # the north pole, in degrees
>>> lonlat_to_cartesian3d(lonlat, input_unit="degrees").shape
(1, 3)
Source code in packages/pyrox-nn/src/pyrox_nn/_geonnax.py
cyclic_encode(angles: Float[Array, ' N'] | Float[Array, 'N D']) -> Float[Array, 'N F']
¶
Encode periodic inputs as concatenated cosine and sine features.
Each angle θ is embedded on the unit circle as [cos θ, sin θ]. A
vector of N angles gives two features per point. A matrix of N rows
and D columns gives 2 D features, with the cosines first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angles
|
Float[Array, ' N'] | Float[Array, 'N D']
|
Angles in radians, |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'N F']
|
The features |
Float[Array, 'N F']
|
for a matrix input, laid out as `[cos θ₀, …, cos θ_{D−1}, sin θ₀, …, |
Float[Array, 'N F']
|
sin θ_{D−1}]`. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
A vector of two angles gives two [cos, sin] pairs:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import cyclic_encode
>>> cyclic_encode(jnp.array([0.0, jnp.pi])).shape
(2, 2)
A matrix input encodes each column independently:
Composition with jax.jit:
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/encoders/_coordinates.py
spherical_harmonic_encode(lonlat: Float[Array, 'N 2'], l_max: int, *, input_unit: Literal['degrees', 'radians'] = 'radians') -> Float[Array, 'N M']
¶
Lift longitude and latitude to the unit sphere S² and evaluate Y_l^m.
The point is mapped to p = (cos ϕ cos λ, cos ϕ sin λ, sin ϕ), and the
real spherical harmonics Y_l^m(p) are evaluated for every degree
l ≤ l_max.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lonlat
|
Float[Array, 'N 2']
|
Longitude |
required |
l_max
|
int
|
Maximum harmonic degree |
required |
input_unit
|
Literal['degrees', 'radians']
|
Whether |
'radians'
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'N M']
|
The features |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
Typical case, two points at l_max = 3:
>>> import jax.numpy as jnp
>>> from geonnax.encoders import spherical_harmonic_encode
>>> lonlat = jnp.array([[0.0, 0.0], [1.5707964, 0.0]])
>>> spherical_harmonic_encode(lonlat, l_max=3).shape
(2, 16)
Degree zero keeps only the constant mode Y_0^0:
Composition with jax.jit, with l_max held static:
>>> import jax
>>> encode = jax.jit(spherical_harmonic_encode, static_argnames="l_max")
>>> encode(lonlat, l_max=3).shape
(2, 16)