Skip to content

Commit 608ac26

Browse files
committed
revert backends experiment
1 parent 1e7cc81 commit 608ac26

File tree

3 files changed

+33
-144
lines changed

3 files changed

+33
-144
lines changed

README.md

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
2-
transformcl
3-
===========
1+
# transformcl
42

53
**Transform angular power spectra and correlation functions.**
64

75
This is a minimal Python package for transformations between angular power
8-
spectra and correlation functions. It is currently limited to the spin zero
6+
spectra and correlation functions. It is currently limited to the spin zero
97
case.
108

119
The package can be installed using pip:
@@ -22,30 +20,7 @@ ct = transformcl.corr(cl)
2220

2321
For more information, please see the [documentation].
2422

25-
Current functionality covers the absolutely minimal use case. Please open an
23+
Current functionality covers the absolutely minimal use case. Please open an
2624
issue on GitHub if you would like to see anything added.
2725

2826
[documentation]: https://glass.readthedocs.io/projects/transformcl/latest
29-
30-
31-
Backends
32-
--------
33-
The `transformcl` package supports multiple transform backends. The
34-
current backend can be changed globally by assigning to
35-
`transformcl.backend`:
36-
37-
```py
38-
transformcl.backend = "<choice>"
39-
...
40-
```
41-
42-
or using the `transformcl.use()` context manager:
43-
44-
```py
45-
with transformcl.use("<choice>"):
46-
...
47-
```
48-
49-
The list of available backends can be found in the [documentation][backends].
50-
51-
[backends]: https://glass.readthedocs.io/projects/transformcl/latest#backends

docs/index.rst

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,10 @@ Current functionality covers the absolutely minimal use case. Please open an
1919
issue on GitHub if you would like to see anything added.
2020

2121

22-
.. _backends:
23-
24-
Backends
25-
--------
26-
The :mod:`transformcl` module supports multiple transform backends. The
27-
current backend can be changed globally by assigning to
28-
:data:`transformcl.backend`::
29-
30-
transformcl.backend = "<choice>"
31-
...
32-
33-
or using the :func:`transformcl.use` context manager::
34-
35-
with transformcl.use("<choice>"):
36-
...
37-
38-
The following backends are available:
39-
40-
.. data:: transformcl.backend
41-
:value: "flt-ii"
42-
:no-index:
43-
44-
The "open" Discrete Legendre Transform (DLT) as implemented by the
45-
:doc:`flt:index` package, with correlation function values over the open
46-
interval :math:`(0, \pi)`.
47-
48-
.. note:: This is the default backend. It supports multiple array types.
49-
50-
.. data:: transformcl.backend
51-
:value: "flt-i"
52-
:no-index:
53-
54-
The "closed" Discrete Legendre Transform (DLT) as implemented by the
55-
:doc:`flt:index` package, with correlation function values over the closed
56-
interval :math:`[0, \pi]`.
57-
58-
.. caution:: This transform should be used with care. The correlation
59-
function values are computed all the way to :math:`\theta = 0` from a
60-
finite bandlimited angular power spectrum.
61-
62-
6322
Reference
6423
---------
6524

66-
.. autodata:: transformcl.backend
67-
.. autofunction:: transformcl.use
68-
.. autofunction:: transformcl.theta
6925
.. autofunction:: transformcl.corr
7026
.. autofunction:: transformcl.cl
7127
.. autofunction:: transformcl.var
28+
.. autofunction:: transformcl.theta

transformcl.py

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,37 @@
11
"""Transform angular power spectra."""
22

33
__all__ = [
4-
"backend",
54
"cl",
65
"corr",
76
"theta",
8-
"use",
97
"var",
108
]
119

12-
from contextlib import contextmanager
13-
from typing import Literal
14-
1510
from array_api_compat import array_namespace
1611

17-
BackendStr = Literal[
18-
"flt-ii",
19-
"flt-i",
20-
]
21-
22-
23-
backend: BackendStr = "flt-ii"
24-
"""Backend for transforms. See :ref:`backends`."""
25-
26-
27-
@contextmanager
28-
def use(choice: BackendStr) -> None:
29-
"""Context manager to change backend. See :ref:`backends`."""
30-
global backend
31-
restore = backend
32-
backend = choice
33-
try:
34-
yield
35-
finally:
36-
backend = restore
12+
import flt
3713

3814

39-
def corr(cl):
15+
def corr(cl, closed=False):
4016
r"""
4117
Transform angular power spectrum to angular correlation function.
4218
4319
Takes an angular power spectrum with :math:`\mathtt{n} =
4420
\mathtt{lmax}+1` coefficients and returns the corresponding angular
4521
correlation function in :math:`\mathtt{n}` points.
4622
47-
The correlation function is computed at the angles returned by
48-
:func:`transformcl.theta`.
23+
The correlation function values can be computed either over the
24+
closed interval :math:`[0, \pi]`, in which case :math:`\theta_0 = 0`
25+
and :math:`\theta_{n-1} = \pi`, or over the open interval :math:`(0,
26+
\pi)`.
4927
5028
Parameters
5129
----------
5230
cl : (n,) array_like
5331
Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
32+
closed : bool
33+
Compute correlation function over open (``closed=False``) or closed
34+
(``closed=True``) interval.
5435
5536
Returns
5637
-------
@@ -66,30 +47,20 @@ def corr(cl):
6647
6748
"""
6849

50+
xp = array_namespace(cl)
51+
6952
# length n of the transform
7053
if cl.ndim != 1:
7154
raise TypeError("cl must be 1d array")
7255
n = cl.shape[-1]
7356

74-
if backend in ["flt-ii", "flt-i"]:
75-
xp = array_namespace(cl)
76-
# DLT coefficients = (2l+1)/(4pi) * Cl
77-
a = (2 * xp.arange(n) + 1) / (4 * xp.pi) * cl
78-
79-
if backend == "flt-ii":
80-
import flt
81-
82-
return flt.idlt(a)
83-
84-
if backend == "flt-i":
85-
import flt
57+
# DLT coefficients = (2l+1)/(4pi) * Cl
58+
a = (2 * xp.arange(n) + 1) / (4 * xp.pi) * cl
8659

87-
return flt.idlt(a, True)
60+
return flt.idlt(a, closed)
8861

89-
raise NotImplementedError(f"unknown backend {backend!r}")
9062

91-
92-
def cl(corr):
63+
def cl(corr, closed=False):
9364
r"""
9465
Transform angular correlation function to angular power spectrum.
9566
@@ -98,7 +69,10 @@ def cl(corr):
9869
:math:`\mathtt{lmax} = \mathtt{n}-1`.
9970
10071
The correlation function must be given at the angles returned by
101-
:func:`transformcl.theta`.
72+
:func:`transformcl.theta`. These can be distributed either over the
73+
closed interval :math:`[0, \pi]`, in which case :math:`\theta_0 = 0`
74+
and :math:`\theta_{n-1} = \pi`, or over the open interval :math:`(0,
75+
\pi)`.
10276
10377
Parameters
10478
----------
@@ -109,6 +83,9 @@ def cl(corr):
10983
-------
11084
cl : (n,) array_like
11185
Angular power spectrum from :math:`0` to :math:`\mathtt{lmax}`.
86+
closed : bool
87+
Compute correlation function over open (``closed=False``) or
88+
closed (``closed=True``) interval.
11289
11390
See Also
11491
--------
@@ -119,27 +96,17 @@ def cl(corr):
11996
12097
"""
12198

99+
xp = array_namespace(corr)
100+
122101
# length n of the transform
123102
if corr.ndim != 1:
124103
raise TypeError("corr must be 1d array")
125104
n = corr.shape[-1]
126105

127-
if backend in ["flt-ii", "flt-i"]:
128-
xp = array_namespace(corr)
129-
# DLT coefficients = (2l+1)/(4pi) * Cl
130-
fl = (2 * xp.arange(n) + 1) / (4 * xp.pi)
131-
132-
if backend == "flt-ii":
133-
import flt
134-
135-
return flt.dlt(corr) / fl
136-
137-
if backend == "flt-i":
138-
import flt
106+
# DLT coefficients = (2l+1)/(4pi) * Cl
107+
fl = (2 * xp.arange(n) + 1) / (4 * xp.pi)
139108

140-
return flt.dlt(corr, True) / fl
141-
142-
raise NotImplementedError(f"unknown backend {backend!r}")
109+
return flt.dlt(corr, closed) / fl
143110

144111

145112
def var(cl):
@@ -175,22 +142,12 @@ def var(cl):
175142
return xp.sum((2 * ell + 1) / (4 * xp.pi) * cl, axis=-1)
176143

177144

178-
def theta(n):
145+
def theta(n, closed=False):
179146
r"""
180147
Return the angles :math:`\theta_1, \ldots, \theta_n` of the
181148
correlation function with *n* points.
182149
"""
183-
184-
if backend == "flt-ii":
185-
import flt
186-
187-
return flt.theta(n)
188-
if backend == "flt-i":
189-
import flt
190-
191-
return flt.theta(n, True)
192-
193-
raise NotImplementedError(f"unknown backend {backend!r}")
150+
return flt.theta(n, closed)
194151

195152

196153
cltocorr = corr

0 commit comments

Comments
 (0)