Skip to content

Commit 41c4266

Browse files
authored
gh-346: enforce NumPy docstrings + use static typing in docs (#347)
Closes #346. Working towards #188. The main changes here have been: - Remove all types in the docstrings in favour of proper static typing (being added in #308) - Switch from `numpydoc` to `sphinx.ext.napolean` due to numpy/numpydoc#196 Have had to change the references to `[1]` rather than `[1]_` due to this bug, sphinx-doc/sphinx#9689. Hopefully this can be fixed in the future. Example: `glass.lensing.from_convergence` <img width="781" alt="image" src="https://github.com/user-attachments/assets/9b6fd087-686a-4a5c-a77a-f8a3ec9fc3e2"> Raised #350, #351.
1 parent 28d5fb8 commit 41c4266

12 files changed

+310
-329
lines changed

docs/conf.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
2424
# ones.
2525
extensions = [
26-
"numpydoc",
27-
"sphinx.ext.intersphinx",
28-
"sphinxcontrib.katex",
2926
"matplotlib.sphinxext.plot_directive",
3027
"nbsphinx",
28+
"sphinx.ext.intersphinx",
29+
"sphinx.ext.napoleon",
3130
"sphinx.ext.viewcode",
31+
"sphinxcontrib.katex",
3232
]
3333

3434
# Add any paths that contain templates here, relative to this directory.
@@ -42,7 +42,7 @@
4242

4343
# -- Options for HTML output -------------------------------------------------
4444

45-
# The theme to use for HTML and HTML Help pages. See the documentation for
45+
# The theme to use for HTML and HTML Help pages. See the documentation for
4646
# a list of builtin themes.
4747
#
4848
html_theme = "furo"
@@ -69,15 +69,12 @@
6969

7070
# -- autodoc -----------------------------------------------------------------
7171

72-
autodoc_typehints = "none"
72+
autodoc_typehints = "description"
7373

7474

75-
# -- numpydoc ----------------------------------------------------------------
75+
# -- napoleon ----------------------------------------------------------------
7676

77-
# Whether to create a Sphinx table of contents for the lists of class methods
78-
# and attributes. If a table of contents is made, Sphinx expects each entry to
79-
# have a separate page.
80-
numpydoc_class_members_toctree = False
77+
napoleon_google_docstring = False
8178

8279

8380
# -- plot_directive ----------------------------------------------------------

glass/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
The :mod:`glass.core` module contains core functionality for developing
88
GLASS modules.
99
10-
""" # noqa: D205, D400, D415
10+
""" # noqa: D205, D400

glass/core/algorithm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def nnls(
2020
"""
2121
Compute a non-negative least squares solution.
2222
23-
Implementation of the algorithm due to [1]_ as described in [2]_.
23+
Implementation of the algorithm due to [1] as described in [2].
2424
2525
References
2626
----------
27-
.. [1] Lawson, C. L. and Hanson, R. J. (1995), Solving Least Squares
27+
* [1] Lawson, C. L. and Hanson, R. J. (1995), Solving Least Squares
2828
Problems. doi: 10.1137/1.9781611971217
29-
.. [2] Bro, R. and De Jong, S. (1997), A fast
29+
* [2] Bro, R. and De Jong, S. (1997), A fast
3030
non-negativity-constrained least squares algorithm. J.
3131
Chemometrics, 11, 393-401.
3232

glass/fields.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.. currentmodule:: glass
66
77
The following functions provide functionality for simulating random
8-
fields on the sphere. This is done in the form of HEALPix maps.
8+
fields on the sphere. This is done in the form of HEALPix maps.
99
1010
Functions
1111
---------
@@ -22,7 +22,7 @@
2222
2323
.. autofunction:: getcl
2424
25-
""" # noqa: D205, D400, D415
25+
""" # noqa: D205, D400
2626

2727
from __future__ import annotations
2828

@@ -163,7 +163,7 @@ def gaussian_gls(
163163
164164
Depending on the given arguments, this truncates the angular power spectra
165165
to ``lmax``, removes all but ``ncorr`` correlations between fields, and
166-
applies the HEALPix pixel window function of the given ``nside``. If no
166+
applies the HEALPix pixel window function of the given ``nside``. If no
167167
arguments are given, no action is performed.
168168
169169
"""
@@ -221,7 +221,7 @@ def generate_gaussian(
221221
``nside``.
222222
223223
The optional argument ``ncorr`` can be used to artificially limit now many
224-
realised fields are correlated. This saves memory, as only `ncorr` previous
224+
realised fields are correlated. This saves memory, as only `ncorr` previous
225225
fields need to be kept.
226226
227227
The ``gls`` array must contain the auto-correlation of each new field
@@ -329,18 +329,15 @@ def getcl(cls, i, j, lmax=None):
329329
330330
Parameters
331331
----------
332-
cls : list of array_like
332+
cls:
333333
List of angular power spectra in *GLASS* ordering.
334-
i, j : int
335-
Combination of indices to return.
336-
lmax : int, optional
334+
i:
335+
Indices to return.
336+
j:
337+
Indices to return.
338+
lmax:
337339
Truncate the returned spectrum at this mode number.
338340
339-
Returns
340-
-------
341-
cl : array_like
342-
The angular power spectrum for indices *i* and *j*.
343-
344341
"""
345342
if j > i:
346343
i, j = j, i
@@ -360,28 +357,25 @@ def effective_cls(
360357
Compute effective angular power spectra from weights.
361358
362359
Computes a linear combination of the angular power spectra *cls*
363-
using the factors provided by *weights1* and *weights2*. Additional
360+
using the factors provided by *weights1* and *weights2*. Additional
364361
axes in *weights1* and *weights2* produce arrays of spectra.
365362
363+
Returns a dictionary of effective angular power spectra, where keys
364+
correspond to the leading axes of *weights1* and *weights2*.
365+
366366
Parameters
367367
----------
368-
cls : (N,) list of array_like
368+
cls:
369369
Angular matter power spectra to combine, in *GLASS* ordering.
370-
weights1 : (N, \\*M1) array_like
371-
Weight factors for spectra. The first axis must be equal to the
372-
number of fields.
373-
weights2 : (N, \\*M2) array_like, optional
374-
Second set of weights. If not given, *weights1* is used.
375-
lmax : int, optional
376-
Truncate the angular power spectra at this mode number. If not
370+
weights1:
371+
Weight factors for spectra. The first axis must be equal to
372+
the number of fields.
373+
weights2:
374+
Second set of weights. If not given, *weights1* is used.
375+
lmax:
376+
Truncate the angular power spectra at this mode number. If not
377377
given, the longest input in *cls* will be used.
378378
379-
Returns
380-
-------
381-
cls : (\\*M1, \\*M2, LMAX+1) array_like
382-
Dictionary of effective angular power spectra, where keys
383-
correspond to the leading axes of *weights1* and *weights2*.
384-
385379
"""
386380
from itertools import combinations_with_replacement, product
387381

glass/galaxies.py

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
.. autofunction:: galaxy_shear
1616
.. autofunction:: gaussian_phz
1717
18-
""" # noqa: D205, D400, D415
18+
""" # noqa: D205, D400
1919

2020
from __future__ import annotations
2121

@@ -45,20 +45,17 @@ def redshifts(
4545
This function samples *n* redshifts from a distribution that follows
4646
the given radial window function *w*.
4747
48+
Returns random redshifts following the radial window function.
49+
4850
Parameters
4951
----------
50-
n : int or array_like
51-
Number of redshifts to sample. If an array is given, the
52+
n:
53+
Number of redshifts to sample. If an array is given, the
5254
results are concatenated.
53-
w : :class:`~glass.RadialWindow`
55+
w:
5456
Radial window function.
55-
rng : :class:`~numpy.random.Generator`, optional
56-
Random number generator. If not given, a default RNG is used.
57-
58-
Returns
59-
-------
60-
z : array_like
61-
Random redshifts following the radial window function.
57+
rng:
58+
Random number generator. If not given, a default RNG is used.
6259
6360
"""
6461
return redshifts_from_nz(n, w.za, w.wa, rng=rng, warn=False)
@@ -77,31 +74,31 @@ def redshifts_from_nz(
7774
7875
The function supports sampling from multiple populations of
7976
redshifts if *count* is an array or if there are additional axes in
80-
the *z* or *nz* arrays. In this case, the shape of *count* and the
77+
the *z* or *nz* arrays. In this case, the shape of *count* and the
8178
leading dimensions of *z* and *nz* are broadcast to a common shape,
8279
and redshifts are sampled independently for each extra dimension.
8380
The results are concatenated into a flat array.
8481
82+
Returns redshifts sampled from the given source distribution. For
83+
inputs with extra dimensions, returns a flattened 1-D array of
84+
samples from all populations.
85+
8586
Parameters
8687
----------
87-
count : int or array_like
88-
Number of redshifts to sample. If an array is given, its shape
88+
count:
89+
Number of redshifts to sample. If an array is given, its shape
8990
is broadcast against the leading axes of *z* and *nz*.
90-
z, nz : array_like
91-
Source distribution. Leading axes are broadcast against the
91+
z:
92+
Source distribution. Leading axes are broadcast against the
9293
shape of *count*.
93-
rng : :class:`~numpy.random.Generator`, optional
94-
Random number generator. If not given, a default RNG is used.
95-
warn : bool
94+
nz:
95+
Source distribution. Leading axes are broadcast against the
96+
shape of *count*.
97+
rng:
98+
Random number generator. If not given, a default RNG is used.
99+
warn:
96100
Throw relevant warnings.
97101
98-
Returns
99-
-------
100-
redshifts : array_like
101-
Redshifts sampled from the given source distribution. For
102-
inputs with extra dimensions, returns a flattened 1-D array of
103-
samples from all populations.
104-
105102
"""
106103
if warn:
107104
warnings.warn(
@@ -158,22 +155,26 @@ def galaxy_shear( # noqa: PLR0913
158155
Takes lensing maps for convergence and shear and produces a lensed
159156
ellipticity (shear) for each intrinsic galaxy ellipticity.
160157
158+
Returns an array of complex-valued observed galaxy shears
159+
(lensed ellipticities).
160+
161161
Parameters
162162
----------
163-
lon, lat : array_like
164-
Arrays for galaxy longitudes and latitudes.
165-
eps : array_like
163+
lon:
164+
Array for galaxy longitudes.
165+
lat:
166+
Array for galaxy latitudes.
167+
eps:
166168
Array of galaxy :term:`ellipticity`.
167-
kappa, gamma1, gamma2 : array_like
168-
HEALPix maps for convergence and two components of shear.
169-
reduced_shear : bool, optional
170-
If ``False``, galaxy shears are not reduced by the convergence.
171-
Default is ``True``.
172-
173-
Returns
174-
-------
175-
she : array_like
176-
Array of complex-valued observed galaxy shears (lensed ellipticities).
169+
kappa:
170+
HEALPix map for convergence.
171+
gamma1:
172+
HEALPix maps for a component of shear.
173+
gamma2:
174+
HEALPix maps for a component of shear.
175+
reduced_shear:
176+
If ``False``, galaxy shears are not reduced
177+
by the convergence. Default is ``True``.
177178
178179
"""
179180
nside = healpix.npix2nside(np.broadcast(kappa, gamma1, gamma2).shape[-1])
@@ -218,40 +219,38 @@ def gaussian_phz(
218219
219220
A simple toy model of photometric redshift errors that assumes a
220221
Gaussian error with redshift-dependent standard deviation
221-
:math:`\sigma(z) = (1 + z) \sigma_0` [1]_.
222+
:math:`\sigma(z) = (1 + z) \sigma_0` [1].
223+
224+
Returns photometric redshifts assuming Gaussian errors, of the same
225+
shape as *z*.
222226
223227
Parameters
224228
----------
225-
z : array_like
229+
z:
226230
True redshifts.
227-
sigma_0 : float or array_like
231+
sigma_0:
228232
Redshift error in the tomographic binning at zero redshift.
229-
lower, upper : float or array_like, optional
233+
lower:
230234
Bounds for the returned photometric redshifts.
231-
rng : :class:`~numpy.random.Generator`, optional
232-
Random number generator. If not given, a default RNG is used.
233-
234-
Returns
235-
-------
236-
phz : array_like
237-
Photometric redshifts assuming Gaussian errors, of the same
238-
shape as *z*.
235+
upper:
236+
Bounds for the returned photometric redshifts.
237+
rng:
238+
Random number generator. If not given, a default RNG is used.
239239
240240
Warnings
241241
--------
242242
The *lower* and *upper* bounds are implemented using plain rejection
243-
sampling from the non-truncated normal distribution. If bounds are
243+
sampling from the non-truncated normal distribution. If bounds are
244244
used, they should always contain significant probability mass.
245245
246246
See Also
247247
--------
248-
glass.tomo_nz_gausserr :
249-
Create tomographic redshift distributions assuming the same
250-
model.
248+
glass.tomo_nz_gausserr:
249+
Create tomographic redshift distributions assuming the same model.
251250
252251
References
253252
----------
254-
.. [1] Amara A., Réfrégier A., 2007, MNRAS, 381, 1018.
253+
* [1] Amara A., Réfrégier A., 2007, MNRAS, 381, 1018.
255254
doi:10.1111/j.1365-2966.2007.12271.x
256255
257256
Examples

0 commit comments

Comments
 (0)