Repository: astro-informatics/s2fft (language: python)

Issue:
add ssht complex signal generator fixture to pytests
Adds signal generation as a fixture for examples. The fixture returns a function so we can then pass the parameterise arguments for multiple tests.


Discussion / hints:

Fix patch:
diff --git a/s2fft/__init__.py b/s2fft/__init__.py
index c6dd0e17..dce4e4b3 100644
--- a/s2fft/__init__.py
+++ b/s2fft/__init__.py
@@ -1,3 +1,4 @@
 from . import logs
 from . import sampling
 from . import transform
+from . import utils
diff --git a/s2fft/utils.py b/s2fft/utils.py
new file mode 100644
index 00000000..f83bf7e2
--- /dev/null
+++ b/s2fft/utils.py
@@ -0,0 +1,23 @@
+import numpy as np
+import s2fft as s2f
+from s2fft.sampling import elm2ind
+
+
+def generate_flm(L, spin=0, reality=False):
+    ncoeff = s2f.sampling.ncoeff(L)
+    flm = np.zeros(ncoeff, dtype=np.complex128)
+
+    if reality == False:
+        flm = np.random.rand(ncoeff) + 1j * np.random.rand(ncoeff)
+        # For spin signals all flms for el < spin = 0, therfore first spin**2 coefficients = 0
+        flm[: spin**2] = 0.0
+        return flm
+    else:
+        for el in range(spin, L):
+            flm[elm2ind(el, 0)] = np.random.rand()
+            for em in range(1, el + 1):
+                flm[elm2ind(el, em)] = np.random.rand() + 1j * np.random.rand()
+                flm[elm2ind(el, -em)] = -(1 ** (em)) * np.conj(
+                    flm[elm2ind(el, em)]
+                )
+        return flm


Test patch:
diff --git a/tests/test_helper_functions.py b/tests/test_helper_functions.py
new file mode 100644
index 00000000..7b1863ff
--- /dev/null
+++ b/tests/test_helper_functions.py
@@ -0,0 +1,10 @@
+import pytest
+from pytest import fixture
+import numpy as np
+import pyssht as ssht
+import s2fft as s2f
+
+
+@fixture
+def ssht_signal_generator():
+    return s2f.utils.generate_signal_ssht
diff --git a/tests/test_transform.py b/tests/test_transform.py
index d72fe139..d643381d 100644
--- a/tests/test_transform.py
+++ b/tests/test_transform.py
@@ -3,20 +3,19 @@
 import s2fft as s2f
 import pyssht as ssht
 
+from .utils import *
 
 # @pytest.mark.skip(reason="Temporarily skipped for faster development")
 @pytest.mark.parametrize("L", [15, 16])
 @pytest.mark.parametrize("spin", [0, 2])
 @pytest.mark.parametrize("sampling", ["mw", "mwss", "dh"])
-def test_transform_inverse_direct(L: int, spin: int, sampling: str):
-
-    ncoeff = s2f.sampling.ncoeff(L)
-    flm = np.zeros(ncoeff, dtype=np.complex128)
-    flm = np.random.rand(ncoeff) + 1j * np.random.rand(ncoeff)
-
-    f = s2f.transform.inverse_direct(flm, L, spin, sampling)
+def test_transform_inverse_direct(
+    flm_generator, L: int, spin: int, sampling: str
+):
 
+    flm = flm_generator(L=L, spin=spin, reality=False)
     f_check = ssht.inverse(flm, L, Method=sampling.upper(), Spin=spin, Reality=False)
+    f = s2f.transform.inverse_direct(flm, L, spin, sampling)
 
     np.testing.assert_allclose(f, f_check, atol=1e-14)
 
@@ -24,21 +23,14 @@ def test_transform_inverse_direct(L: int, spin: int, sampling: str):
 @pytest.mark.parametrize("L", [15, 16])
 @pytest.mark.parametrize("spin", [0, 2])
 @pytest.mark.parametrize("sampling", ["dh"])
-def test_transform_forward_direct(L: int, spin: int, sampling: str):
+def test_transform_forward_direct(
+    flm_generator, L: int, spin: int, sampling: str
+):
 
     # TODO: move this and potentially do better
     np.random.seed(2)
 
-    # Create bandlimited signal
-    ncoeff = s2f.sampling.ncoeff(L)
-    flm = np.zeros(ncoeff, dtype=np.complex128)
-    flm = np.random.rand(ncoeff) + 1j * np.random.rand(ncoeff)
-
-    # Zero harmonic coefficient with el < abs(spin)
-    el = np.abs(spin) - 1
-    m = el
-    ind = s2f.sampling.elm2ind(el, m)
-    flm[0 : ind + 1] = 0.0
+    flm = flm_generator(L=L, spin=spin, reality=False)
 
     f = s2f.transform.inverse_direct(flm, L, spin, sampling)
 
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 00000000..47d24eea
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,9 @@
+import pytest
+from pytest import fixture
+import numpy as np
+import s2fft as s2f
+
+
+@fixture
+def flm_generator():
+    return s2f.utils.generate_flm