Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ jobs:
# healpy to be installed to avoid issues when trying to build these packages)
runs-on: ${{ matrix.os }}
needs: tests-non-cached
env:
# --xla_cpu_multi_thread_eigen=false: XLA:CPU passes its intra-op Eigen thread pool
# to the DUCC FFT of each FFT thunk. In the N320 case of
# test_wigner_transform_reduced_gaussian, concurrent ring FFTs can hold every thread
# of the pool in ducc0's latch::wait, and no thread is left to run their subtasks.
# On a 2-core Colab CPU the test did not finish within 900 s without the flag and
# passes in 22 to 27 s with it. tests/test_spherical_custom_grads.py takes 330 s
# with the flag and 335 s without.
XLA_FLAGS: --xla_cpu_multi_thread_eigen=false
strategy:
matrix:
python-version: ["3.11", "3.12", "3.13"]
Expand Down
217 changes: 206 additions & 11 deletions src/s2fft/sampling/s2_samples.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
import numpy as np


def ntheta(L: int = None, sampling: str = "mw", nside: int = None) -> int:
# Number of phi samples on each ring of the classic reduced Gaussian grid N320, from the
# north pole to the equator; the southern rings mirror them. From ECMWF's Atlas library,
# src/atlas/grid/detail/pl/classic_gaussian/N320.cc at commit 695fd13a, (C) Copyright
# 2013 ECMWF, licensed under the Apache Licence Version 2.0.
# fmt: off
N320_NPHI_NORTH = np.array([
18, 25, 36, 40, 45, 50, 60, 64, 72, 72, 75, 81, 90, 96, 100, 108, 120, 120, 125,
135, 144, 144, 150, 160, 180, 180, 180, 192, 192, 200, 216, 216, 216, 225, 240, 240,
240, 250, 256, 270, 270, 288, 288, 288, 300, 300, 320, 320, 320, 324, 360, 360, 360,
360, 360, 360, 375, 375, 384, 384, 400, 400, 405, 432, 432, 432, 432, 450, 450, 450,
480, 480, 480, 480, 480, 486, 500, 500, 500, 512, 512, 540, 540, 540, 540, 540, 576,
576, 576, 576, 576, 576, 600, 600, 600, 600, 640, 640, 640, 640, 640, 640, 640, 648,
648, 675, 675, 675, 675, 720, 720, 720, 720, 720, 720, 720, 720, 720, 729, 750, 750,
750, 750, 768, 768, 768, 768, 800, 800, 800, 800, 800, 800, 810, 810, 864, 864, 864,
864, 864, 864, 864, 864, 864, 864, 864, 900, 900, 900, 900, 900, 900, 900, 900, 960,
960, 960, 960, 960, 960, 960, 960, 960, 960, 960, 960, 960, 960, 972, 972, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1024, 1024, 1024, 1024, 1024, 1024, 1080,
1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1125,
1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1152,
1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1200, 1200, 1200, 1200, 1200, 1200,
1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1215, 1215,
1215, 1215, 1215, 1215, 1215, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280, 1280,
])
# fmt: on


def ntheta(
L: int = None, sampling: str = "mw", nside: int = None, n_gauss: int = None
) -> int:
r"""
Number of :math:`\theta` samples for sampling scheme at specified resolution.

Expand All @@ -10,11 +42,16 @@ def ntheta(L: int = None, sampling: str = "mw", nside: int = None) -> int:
Defaults to None.

sampling (str, optional): Sampling scheme. Supported sampling schemes include
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2"}. Defaults to "mw".
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2", "octahedral",
"reduced_gaussian"}. Defaults to "mw".

nside (int, optional): HEALPix Nside resolution parameter. Only required
if sampling="healpix". Defaults to None.

n_gauss (int, optional): Gaussian grid number :math:`N`, the number of
:math:`\theta` rings between a pole and the equator. Only required if
sampling="octahedral" or "reduced_gaussian". Defaults to None.

Raises:
ValueError: L not specified when sampling not healpix.

Expand Down Expand Up @@ -64,6 +101,9 @@ def ntheta(L: int = None, sampling: str = "mw", nside: int = None) -> int:

return 4 * nside - 1

elif is_reduced_gaussian(sampling):
return nphi_reduced_gaussian(sampling, n_gauss).size

else:
raise ValueError(f"Sampling scheme sampling={sampling} not supported")

Expand Down Expand Up @@ -147,12 +187,15 @@ def m_offset(L: int, sampling: str) -> int:
L (int): Harmonic band-limit.

sampling (str): Sampling scheme. Supported sampling schemes include
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2"}.
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2", "octahedral",
"reduced_gaussian"}.

Returns:
Integer binary (0/1) offset.

"""
if is_reduced_gaussian(sampling):
return 0
return (
1 if sampling.lower() == "healpix" or nphi_equiang(L, sampling) % 2 == 0 else 0
)
Expand Down Expand Up @@ -237,6 +280,22 @@ def is_equiangular_in_theta(sampling: str) -> bool:
return sampling.lower() in ("mw", "mwss", "dh", "cc", "f2")


def is_reduced_gaussian(sampling: str) -> bool:
"""
Whether the sampling scheme is a reduced Gaussian grid: Gauss-Legendre rings in θ
whose number of φ samples decreases towards the poles. Both the octahedral grids
and the classic reduced Gaussian grids of ECMWF are reduced Gaussian grids.

Args:
sampling (str): Sampling scheme.

Returns:
Boolean value.

"""
return sampling.lower() in ("octahedral", "reduced_gaussian")


def ftm_shape(L: int, sampling: str = "mw", nside: int = None) -> tuple[int, int]:
r"""
Shape of intermediate array, before/after latitudinal step.
Expand Down Expand Up @@ -314,7 +373,100 @@ def nphi_ring(t: int, nside: int = None) -> int:
raise ValueError(f"Ring t={t} not contained by nside={nside}")


def thetas(L: int = None, sampling: str = "mw", nside: int = None) -> np.ndarray:
def nphi_reduced_gaussian(sampling: str, n_gauss: int) -> np.ndarray:
r"""
Number of :math:`\phi` samples on each :math:`\theta` ring of a reduced Gaussian
grid, from north to south.

The grid has :math:`2N` Gauss-Legendre rings, for :math:`N` = `n_gauss` rings
between a pole and the equator. On the octahedral grid O\ :math:`N`, ring
:math:`i = 1, \dots, N` counted from the nearest pole has :math:`4i + 16` samples,
as in ECMWF's ectrans (``parse_grid`` in ``src/programs/ectrans-benchmark.F90``).
The classic reduced Gaussian grid is supported for N320 only, with the ring sizes
of :data:`N320_NPHI_NORTH`. The southern rings mirror the northern ones.

Args:
sampling (str): Sampling scheme, "octahedral" or "reduced_gaussian".

n_gauss (int): Gaussian grid number :math:`N`, the number of :math:`\theta`
rings between a pole and the equator.

Raises:
ValueError: Sampling scheme and n_gauss not supported.

Returns:
np.ndarray: Number of :math:`\phi` samples on each :math:`\theta` ring.

"""
if sampling.lower() == "octahedral" and n_gauss is not None and n_gauss >= 1:
north = 4 * np.arange(1, n_gauss + 1) + 16
elif sampling.lower() == "reduced_gaussian" and n_gauss == 320:
north = N320_NPHI_NORTH
else:
raise ValueError(
f"Sampling scheme sampling={sampling} with n_gauss={n_gauss} not supported"
)
return np.concatenate((north, north[::-1]))


def m_max_reduced_gaussian(L: int, sampling: str, n_gauss: int) -> np.ndarray:
r"""
Highest order :math:`|m|` kept on each :math:`\theta` ring of a reduced Gaussian
grid, from north to south.

On ring :math:`t` the transforms keep the orders :math:`|m| \leq` `m_max[t]` and
drop the others, in both directions, as ECMWF's ectrans does with its cut-off
wavenumbers NMEN. The rule is that of ectrans' SETUP_GEOM
(``src/trans/common/internal/setup_geom_mod.F90``, lines 44-78 at release 1.6.0),
for the truncation NSMAX = L - 1 on the 2N rings: linear, quadratic or cubic
according to NSMAX. It gives `m_max[t]` :math:`\leq (n_{\phi, t} - 1) / 2`, so no
kept order aliases onto another on the ring.

Args:
L (int): Harmonic band-limit.

sampling (str): Sampling scheme, "octahedral" or "reduced_gaussian".

n_gauss (int): Gaussian grid number :math:`N`, the number of :math:`\theta`
rings between a pole and the equator.

Returns:
np.ndarray: Highest order :math:`|m|` kept on each :math:`\theta` ring.

"""
nphi = nphi_reduced_gaussian(sampling, n_gauss)
nsmax = L - 1
ndgl = nphi.size
ndgnh = (ndgl + 1) // 2
nsmaxlin = ndgl - 1
# Sine of the latitude of the rings, north to south: the Gauss-Legendre nodes,
# which numpy makes symmetric about the equator, as ectrans does (sugaw_mod.F90)
mu = np.flip(np.polynomial.legendre.leggauss(ndgl)[0])
# Squared cosine of the latitude, computed as ectrans does (suleg_mod.F90:386-389)
r1mu2 = np.cos(np.arcsin(mu)) ** 2
if nsmax >= nsmaxlin or np.all(nphi == nphi.max()):
# Linear or full grid
return np.minimum(nsmax, (nphi - 1) // 2)
if nsmax >= ndgl * 2 // 3 - 1:
# Quadratic grid, the integer division coming before the product with r1mu2
cut = ((nphi - 1) / (2.0 + 3 * (nsmaxlin - nsmax) // ndgl * r1mu2)).astype(int)
else:
# Cubic grid
cut = ((nphi - 1) / (2.0 + r1mu2)).astype(int) - 1
# Orders kept never decrease from either pole to the equator
m_max = np.empty(ndgl, dtype=int)
m_max[0] = min(nsmax, cut[0])
for t in range(1, ndgnh):
m_max[t] = min(nsmax, max(m_max[t - 1], cut[t]))
m_max[-1] = min(nsmax, cut[-1])
for t in range(ndgl - 2, ndgnh - 1, -1):
m_max[t] = min(nsmax, max(m_max[t + 1], cut[t]))
return m_max


def thetas(
L: int = None, sampling: str = "mw", nside: int = None, n_gauss: int = None
) -> np.ndarray:
r"""
Compute :math:`\theta` samples for given sampling scheme.

Expand All @@ -323,17 +475,27 @@ def thetas(L: int = None, sampling: str = "mw", nside: int = None) -> np.ndarray
Defaults to None.

sampling (str, optional): Sampling scheme. Supported sampling schemes include
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2"}. Defaults to "mw".
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2", "octahedral",
"reduced_gaussian"}. Defaults to "mw".

nside (int, optional): HEALPix Nside resolution parameter. Only required
if sampling="healpix". Defaults to None.

n_gauss (int, optional): Gaussian grid number :math:`N`, the number of
:math:`\theta` rings between a pole and the equator. Only required if
sampling="octahedral" or "reduced_gaussian". Defaults to None.

Returns:
np.ndarray: Array of :math:`\theta` samples for given sampling scheme.

"""
if sampling.lower() == "gl":
return np.flip(np.arccos(np.polynomial.legendre.leggauss(L)[0]))
# The rings of a reduced Gaussian grid are those of GL sampling with 2N rings
if sampling.lower() == "gl" or is_reduced_gaussian(sampling):
return np.flip(
np.arccos(
np.polynomial.legendre.leggauss(ntheta(L, sampling, nside, n_gauss))[0]
)
)

t = np.arange(0, ntheta(L=L, sampling=sampling, nside=nside)).astype(np.float64)

Expand Down Expand Up @@ -470,6 +632,27 @@ def p2phi_ring(t: int, p: int, nside: int) -> np.ndarray:
return factor * (p + shift)


def phis_ring_reduced_gaussian(t: int, sampling: str, n_gauss: int) -> np.ndarray:
r"""
Compute :math:`\phi` samples for given :math:`\theta` ring of a reduced Gaussian
grid. The first sample of every ring is at :math:`\phi = 0`.

Args:
t (int): :math:`\theta` index of ring, from north to south.

sampling (str): Sampling scheme, "octahedral" or "reduced_gaussian".

n_gauss (int): Gaussian grid number :math:`N`, the number of :math:`\theta`
rings between a pole and the equator.

Returns:
np.ndarray: :math:`\phi` angles.

"""
nphi = nphi_reduced_gaussian(sampling, n_gauss)[t]
return 2 * np.pi * np.arange(nphi) / nphi


def phis_equiang(L: int, sampling: str = "mw") -> np.ndarray:
r"""
Compute :math:`\phi` samples for equiangular sampling scheme.
Expand Down Expand Up @@ -550,22 +733,31 @@ def ring_phase_shift_hp(
return np.exp(sign * 1j * np.arange(m_start_ind, L) * phi_offset)


def f_shape(L: int = None, sampling: str = "mw", nside: int = None) -> tuple[int]:
def f_shape(
L: int = None, sampling: str = "mw", nside: int = None, n_gauss: int = None
) -> tuple[int]:
r"""
Shape of spherical signal.

Args:
L (int, optional): Harmonic band-limit.

sampling (str, optional): Sampling scheme. Supported sampling schemes include
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2"}. Defaults to "mw".
{"mw", "mwss", "dh", "gl", "healpix", "cc", "f2", "octahedral",
"reduced_gaussian"}. Defaults to "mw".

nside (int, optional): HEALPix Nside resolution parameter. Only required
if sampling="healpix". Defaults to None.

n_gauss (int, optional): Gaussian grid number :math:`N`, the number of
:math:`\theta` rings between a pole and the equator. Only required if
sampling="octahedral" or "reduced_gaussian". Defaults to None.

Returns:
Tuple[int]: Pixel-space array dimensions with shape :math:`[n_{\theta}, n_{\phi}]`.
Note that "healpix" is instead indexed by a 1D array, with standard conventions.
Note that "healpix" is instead indexed by a 1D array, with standard conventions,
and "octahedral" and "reduced_gaussian" by a 1D array of the rings from north
to south, each from :math:`\phi = 0`.

"""
if sampling.lower() != "healpix" and L is None:
Expand All @@ -581,6 +773,9 @@ def f_shape(L: int = None, sampling: str = "mw", nside: int = None) -> tuple[int
if sampling.lower() == "healpix":
return (12 * nside**2,)

elif is_reduced_gaussian(sampling):
return (int(nphi_reduced_gaussian(sampling, n_gauss).sum()),)

else:
return ntheta(L, sampling), nphi_equiang(L, sampling)

Expand Down
12 changes: 10 additions & 2 deletions src/s2fft/sampling/so3_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def f_shape(
L: int, N: int, sampling: str = "mw", nside: int = None
L: int, N: int, sampling: str = "mw", nside: int = None, n_gauss: int = None
) -> tuple[int, int, int]:
r"""
Computes the pixel-space sampling shape for signal on the rotation group
Expand All @@ -24,11 +24,16 @@ def f_shape(
N (int): Directional band-limit.

sampling (str, optional): Sampling scheme. Supported sampling schemes include
{"mw", "mwss", "dh", "gl"}. Defaults to "mw".
{"mw", "mwss", "dh", "gl", "healpix", "octahedral", "reduced_gaussian"}.
Defaults to "mw".

nside (int, optional): HEALPix Nside resolution parameter. Only required
if sampling="healpix". Defaults to None.

n_gauss (int, optional): Gaussian grid number, the number of :math:`\beta`
rings between a pole and the equator. Only required if
sampling="octahedral" or "reduced_gaussian". Defaults to None.

Raises:
ValueError: Unknown sampling scheme.

Expand All @@ -43,6 +48,9 @@ def f_shape(
elif sampling.lower() == "healpix":
return _ngamma(N), 12 * nside**2

elif samples.is_reduced_gaussian(sampling):
return _ngamma(N), *samples.f_shape(L, sampling, nside, n_gauss)

else:
raise ValueError(f"Sampling scheme sampling={sampling} not supported")

Expand Down
6 changes: 4 additions & 2 deletions src/s2fft/transforms/_ftm_flm_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def flm_to_ftm(
*_tuplify_precomps(precomps),
L=L,
nside=nside,
sampling=sampling,
# A reduced Gaussian grid takes the GL latitudinal step at its own rings
sampling="gl" if samples.is_reduced_gaussian(sampling) else sampling,
reality=reality,
spmd=spmd,
L_lower=L_lower,
Expand Down Expand Up @@ -335,7 +336,8 @@ def ftm_to_flm(
*_tuplify_precomps(precomps),
L=L,
nside=nside,
sampling=sampling,
# A reduced Gaussian grid takes the GL latitudinal step at its own rings
sampling="gl" if samples.is_reduced_gaussian(sampling) else sampling,
reality=reality,
spmd=spmd,
L_lower=L_lower,
Expand Down
Loading
Loading