Skip to content

Commit 1013d0a

Browse files
CopilotCopilotConnorStoneAstropre-commit-ci[bot]
authored
Fix AstroPhot docstrings to explicit RST fields for Sphinx compatibility (#294)
Updates docstrings to RST for Sphinx/Jupyter Book compatibility, merges docstrings across the MRO, and rebuilds the API docs structure with autodoc pages and MathJax support. ## Changes Made - Reworked `combine_docstrings` to merge parameters/options from the full MRO into a single RST parameter list and add `[model param]` tags to distinguish model parameters from options. - Converted model and mixin docstrings from markdown to RST (including `.. math::` blocks). - Converted docstrings in the **image**, **fitting**, and **plotting** sections to explicit RST field-list syntax using `:param:`, `:type:`, `:returns:`, and `:rtype:` (replacing `Args:` / `Returns:` style sections). - Updated docstring terminology from `Tensor`/`tensor` to `Array` where applicable, to reflect support for both PyTorch and JAX backends. - Added `description` entries to every `_parameter_specs` parameter. - Applied `combine_docstrings` to auto-generated classes in `radial.py` and `radial_psf.py`. - Removed `make_docs.py` and replaced the docstring notebook tree with Sphinx autodoc RST pages. ## Documentation - Added `docs/source/astrophotdocs/*.rst` autodoc pages and updated `astrophotdocs/index.rst`. - Enabled MathJax in the Jupyter Book/Sphinx configuration. ## Testing - Targeted pytest runs for utility and parameter tests. - Targeted pytest runs for updated sections: `tests/test_image.py`, `tests/test_fit.py`, and `tests/test_plots.py`. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Model docstrings need a rewrite</issue_title> > <issue_description>As part of my latest update I tried to make the model docstrings use markdown formatting, but this ultimately doesn't seem to have worked. I would like to revert to a format that sphinx autodoc can work with (I think this is RST). My main challenge is to get it to work in a way that also compiles into the jupyter-book website system with latex equations. > > Also, I think the `combine_docstrings` decorator should be smarter about merging docstrings. It should find all of the "Parameters" and the "Options" from each collected docstring and merge them into single lists so that autodoc compiles them correctly. > > The combine_docstrings decorator also needs to be applied to all the auto generated types in the `radial.py` and `radial_psf.py` files. Further, they should be modified so that sphinx autodoc picks them up when generating the documentation. > > My attempted giant jupyter notebook of docstrings didn't work so that should be removed and instead the tree of autodoc results should be made more human navigable. > > While doing this, it would be good to also check that all the docstrings are up to date to match the actual coded implementations. And the "Parameters" descriptions in the docstrings should be added as a "description" key in all the `_parameter_specs` dictionaries associated with each model.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes #293 <!-- START COPILOT CODING AGENT TIPS --> --- ⌨️ Start Copilot coding agent tasks without leaving your editor — available in [VS Code](https://gh.io/cca-vs-code-docs), [Visual Studio](https://gh.io/cca-visual-studio-docs), [JetBrains IDEs](https://gh.io/cca-jetbrains-docs) and [Eclipse](https://gh.io/cca-eclipse-docs). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: ConnorStoneAstro <78555321+ConnorStoneAstro@users.noreply.github.com> Co-authored-by: Connor Stone <connorstone628@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent abbbf01 commit 1013d0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1707
-853
lines changed

.readthedocs.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ build:
2626
- graphviz
2727
jobs:
2828
pre_build:
29-
# Build docstring jupyter notebooks
30-
- "python make_docs.py"
3129
# Generate the Sphinx configuration for this Jupyter Book so it builds.
3230
- "jupyter-book config sphinx docs/source/"
3331
# Create font cache ahead of jupyter book

astrophot/fit/base.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ class BaseOptimizer:
1313
"""
1414
Base optimizer object that other optimizers inherit from. Ensures consistent signature for the classes.
1515
16-
**Args:**
17-
- `model`: an AstroPhot_Model object that will have its (unlocked) parameters optimized [AstroPhot_Model]
18-
- `initial_state`: optional initialization for the parameters as a 1D tensor [tensor]
19-
- `relative_tolerance`: tolerance for counting success steps as: $0 < (\\chi_2^2 - \\chi_1^2)/\\chi_1^2 < \\text{tol}$ [float]
20-
- `verbose`: verbosity level for the optimizer [int]
21-
- `max_iter`: maximum allowed number of iterations [int]
22-
- `save_steps`: optional string for path to save the model at each step (fitter dependent), e.g. "model_step_{step}.hdf5" [str]
23-
- `fit_valid`: whether to fit while forcing parameters into valid range, or allow any value for each parameter. Default True [bool]
16+
:param model: an AstroPhot_Model object that will have its (unlocked) parameters optimized [AstroPhot_Model]
17+
:param initial_state: optional initialization for the parameters as a 1D Array [Array]
18+
:param relative_tolerance: tolerance for counting success steps as: $0 < (\\chi_2^2 - \\chi_1^2)/\\chi_1^2 < \\text{tol}$ [float]
19+
:param verbose: verbosity level for the optimizer [int]
20+
:param max_iter: maximum allowed number of iterations [int]
21+
:param save_steps: optional string for path to save the model at each step (fitter dependent), e.g. "model_step_{step}.hdf5" [str]
22+
:param fit_valid: whether to fit while forcing parameters into valid range, or allow any value for each parameter. Default True [bool]
2423
2524
"""
2625

astrophot/fit/gradient.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ class Grad(BaseOptimizer):
2525
The optimizer is instantiated with a set of initial parameters and optimization options provided by the user.
2626
The `fit` method performs the optimization, taking a series of gradient steps until a stopping criteria is met.
2727
28-
**Args:**
29-
- `likelihood` (str, optional): The likelihood function to use for the optimization. Defaults to "gaussian".
30-
- `method` (str, optional): the optimization method to use for the update step. Defaults to "NAdam".
31-
- `optim_kwargs` (dict, optional): a dictionary of keyword arguments to pass to the pytorch optimizer.
32-
- `patience` (int, optional): number of steps with no improvement before stopping the optimization. Defaults to 10.
33-
- `report_freq` (int, optional): frequency of reporting the optimization progress. Defaults to 10 steps.
28+
:param likelihood: The likelihood function to use for the optimization. Defaults to "gaussian".
29+
:type likelihood: str, optional
30+
:param method: the optimization method to use for the update step. Defaults to "NAdam".
31+
:type method: str, optional
32+
:param optim_kwargs: a dictionary of keyword arguments to pass to the pytorch optimizer.
33+
:type optim_kwargs: dict, optional
34+
:param patience: number of steps with no improvement before stopping the optimization. Defaults to 10.
35+
:type patience: int, optional
36+
:param report_freq: frequency of reporting the optimization progress. Defaults to 10 steps.
37+
:type report_freq: int, optional
3438
"""
3539

3640
def __init__(
@@ -164,13 +168,18 @@ class Slalom(BaseOptimizer):
164168
gradient descent algorithms, Slalom slows down considerably when trying to
165169
achieve very high precision.
166170
167-
**Args:**
168-
- `S` (float, optional): The initial step size for the Slalom optimizer. Defaults to 1e-4.
169-
- `likelihood` (str, optional): The likelihood function to use for the optimization. Defaults to "gaussian".
170-
- `report_freq` (int, optional): Frequency of reporting the optimization progress. Defaults to 10 steps.
171-
- `relative_tolerance` (float, optional): The relative tolerance for convergence. Defaults to 1e-4.
172-
- `momentum` (float, optional): The momentum factor for the Slalom optimizer. Defaults to 0.5.
173-
- `max_iter` (int, optional): The maximum number of iterations for the optimizer. Defaults to 1000.
171+
:param S: The initial step size for the Slalom optimizer. Defaults to 1e-4.
172+
:type S: float, optional
173+
:param likelihood: The likelihood function to use for the optimization. Defaults to "gaussian".
174+
:type likelihood: str, optional
175+
:param report_freq: Frequency of reporting the optimization progress. Defaults to 10 steps.
176+
:type report_freq: int, optional
177+
:param relative_tolerance: The relative tolerance for convergence. Defaults to 1e-4.
178+
:type relative_tolerance: float, optional
179+
:param momentum: The momentum factor for the Slalom optimizer. Defaults to 0.5.
180+
:type momentum: float, optional
181+
:param max_iter: The maximum number of iterations for the optimizer. Defaults to 1000.
182+
:type max_iter: int, optional
174183
"""
175184

176185
def __init__(

astrophot/fit/hmc.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ def new_configure(self, mass_matrix_shape, adapt_mass_matrix=True, options={}):
3131
"""
3232
Sets up an initial mass matrix.
3333
34-
**Args:**
35-
- `mass_matrix_shape`: a dict that maps tuples of site names to the shape of
34+
:param mass_matrix_shape: a dict that maps tuples of site names to the shape of
3635
the corresponding mass matrix. Each tuple of site names corresponds to a block.
37-
- `adapt_mass_matrix`: a flag to decide whether an adaptation scheme will be used.
38-
- `options`: tensor options to construct the initial mass matrix.
36+
:param adapt_mass_matrix: a flag to decide whether an adaptation scheme will be used.
37+
:param options: Array options to construct the initial mass matrix.
3938
"""
4039
inverse_mass_matrix = {}
4140
for site_names, shape in mass_matrix_shape.items():
@@ -70,16 +69,24 @@ class HMC(BaseOptimizer):
7069
https://arxiv.org/abs/1701.02434, and
7170
http://www.mcmchandbook.net/HandbookChapter5.pdf
7271
73-
**Args:**
74-
- `max_iter` (int, optional): The number of sampling steps to perform. Defaults to 1000.
75-
- `epsilon` (float, optional): The length of the integration step to perform for each leapfrog iteration. The momentum update will be of order epsilon * score. Defaults to 1e-5.
76-
- `leapfrog_steps` (int, optional): Number of steps to perform with leapfrog integrator per sample of the HMC. Defaults to 10.
77-
- `inv_mass` (float or array, optional): Inverse Mass matrix (covariance matrix) which can tune the behavior in each dimension to ensure better mixing when sampling. Defaults to the identity.
78-
- `progress_bar` (bool, optional): Whether to display a progress bar during sampling. Defaults to True.
79-
- `prior` (distribution, optional): Prior distribution for the parameters. Defaults to None.
80-
- `warmup` (int, optional): Number of warmup steps before actual sampling begins. Defaults to 100.
81-
- `hmc_kwargs` (dict, optional): Additional keyword arguments for the HMC sampler. Defaults to {}.
82-
- `mcmc_kwargs` (dict, optional): Additional keyword arguments for the MCMC process. Defaults to {}.
72+
:param max_iter: The number of sampling steps to perform. Defaults to 1000.
73+
:type max_iter: int, optional
74+
:param epsilon: The length of the integration step to perform for each leapfrog iteration. The momentum update will be of order epsilon * score. Defaults to 1e-5.
75+
:type epsilon: float, optional
76+
:param leapfrog_steps: Number of steps to perform with leapfrog integrator per sample of the HMC. Defaults to 10.
77+
:type leapfrog_steps: int, optional
78+
:param inv_mass: Inverse Mass matrix (covariance matrix) which can tune the behavior in each dimension to ensure better mixing when sampling. Defaults to the identity.
79+
:type inv_mass: float or array, optional
80+
:param progress_bar: Whether to display a progress bar during sampling. Defaults to True.
81+
:type progress_bar: bool, optional
82+
:param prior: Prior distribution for the parameters. Defaults to None.
83+
:type prior: distribution, optional
84+
:param warmup: Number of warmup steps before actual sampling begins. Defaults to 100.
85+
:type warmup: int, optional
86+
:param hmc_kwargs: Additional keyword arguments for the HMC sampler. Defaults to {}.
87+
:type hmc_kwargs: dict, optional
88+
:param mcmc_kwargs: Additional keyword arguments for the MCMC process. Defaults to {}.
89+
:type mcmc_kwargs: dict, optional
8390
8491
"""
8592

@@ -122,8 +129,8 @@ def fit(
122129
123130
Records the chain for later examination.
124131
125-
**Args:**
126-
state (torch.Tensor, optional): Model parameters as a 1D tensor.
132+
:param state: Model parameters as a 1D Array.
133+
:type state: Array, optional
127134
128135
"""
129136

astrophot/fit/iterative.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ class Iter(BaseOptimizer):
3636
is not worthwhile for a single model to spend lots of time optimizing when
3737
its neighbors havent converged.
3838
39-
**Args:**
40-
- `max_iter`: Maximum number of iterations, defaults to 100.
41-
- `lm_kwargs`: Keyword arguments to pass to `LM` optimizer.
39+
:param max_iter: Maximum number of iterations, defaults to 100.
40+
:param lm_kwargs: Keyword arguments to pass to `LM` optimizer.
4241
"""
4342

4443
def __init__(
@@ -165,14 +164,15 @@ class IterParam(BaseOptimizer):
165164
likely better to optimize the full problem with LM as, when it works, LM is
166165
faster than the IterParam method.
167166
168-
Args:
169-
chunks (Union[int, tuple]): Specify how to break down the model
170-
parameters. If an integer, at each iteration the algorithm will break the
171-
parameters into groups of that size. If a tuple, should be a tuple of
172-
arrays of length num_dimensions which act as selectors for the parameters
173-
to fit (1 to include, 0 to exclude). Default: 50
174-
chunk_order (str): How to iterate through the chunks. Should be one of: random,
175-
sequential. Default: sequential
167+
:param chunks: Specify how to break down the model parameters. If an integer,
168+
at each iteration the algorithm will break the parameters into groups of
169+
that size. If a tuple, it should be a tuple of arrays of length
170+
num_dimensions which act as selectors for the parameters to fit (1 to
171+
include, 0 to exclude). Default: 50.
172+
:type chunks: Union[int, tuple]
173+
:param chunk_order: How to iterate through the chunks. Should be one of:
174+
``"random"``, ``"sequential"``. Default: ``"sequential"``.
175+
:type chunk_order: str
176176
"""
177177

178178
def __init__(

astrophot/fit/lm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,28 @@ class LM(BaseOptimizer):
8282
by Henri Gavin on which much of the AstroPhot LM implementation is
8383
based::
8484
85-
```{latex}
85+
.. code-block:: text
8686
@article{Gavin2019,
8787
title={The Levenberg-Marquardt algorithm for nonlinear least squares curve-fitting problems},
8888
author={Gavin, Henri P},
8989
journal={Department of Civil and Environmental Engineering, Duke University},
9090
volume={19},
9191
year={2019}
9292
}
93-
```
93+
9494
9595
as well as the paper on LM geodesic acceleration by Mark
9696
Transtrum::
9797
98-
```{latex}
98+
.. code-block:: text
9999
@article{Tanstrum2012,
100100
author = {{Transtrum}, Mark K. and {Sethna}, James P.},
101101
title = "{Improvements to the Levenberg-Marquardt algorithm for nonlinear least-squares minimization}",
102102
year = 2012,
103103
doi = {10.48550/arXiv.1201.5885},
104104
adsurl = {https://ui.adsabs.harvard.edu/abs/2012arXiv1201.5885T},
105105
}
106-
```
106+
107107
108108
The damping factor $\\lambda$ is adjusted at each iteration:
109109
it is effectively increased when we are far from the solution, and

astrophot/fit/mala.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ class MALA(BaseOptimizer):
2323
Which can be found fairly easily with the LM optimizer (see the fitting
2424
methods tutorial).
2525
26-
**Args:**
27-
- `chains`: The number of MCMC chains to run in parallel. Default is 4.
28-
- `epsilon`: The step size for the MALA sampler. Default is 1e-2.
29-
- `mass_matrix`: The mass matrix for the MALA sampler. If None, the identity matrix is used.
30-
- `progress_bar`: Whether to show a progress bar during sampling. Default is True.
31-
- `likelihood`: The likelihood function to use for the MCMC sampling. Can be "gaussian" or "poisson". Default is "gaussian".
26+
:param chains: The number of MCMC chains to run in parallel. Default is 4.
27+
:param epsilon: The step size for the MALA sampler. Default is 1e-2.
28+
:param mass_matrix: The mass matrix for the MALA sampler. If None, the identity matrix is used.
29+
:param progress_bar: Whether to show a progress bar during sampling. Default is True.
30+
:param likelihood: The likelihood function to use for the MCMC sampling. Can be "gaussian" or "poisson". Default is "gaussian".
3231
"""
3332

3433
def __init__(

astrophot/fit/mhmcmc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class MHMCMC(BaseOptimizer):
2626
number of parameters by default, but can be made higher (not lower) if desired.
2727
This is done by passing a 2D array of shape (nwalkers, ndim) to the `fit` method.
2828
29-
**Args:**
30-
- `likelihood`: The likelihood function to use for the MCMC sampling. Can be "gaussian" or "poisson". Default is "gaussian".
29+
:param likelihood: The likelihood function to use for the MCMC sampling. Can be "gaussian" or "poisson". Default is "gaussian".
3130
"""
3231

3332
def __init__(

astrophot/fit/scipy_fit.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ class ScipyFit(BaseOptimizer):
1919
tasks. It supports a variety of methods, however only a subset allow users to
2020
define boundaries for the parameters. This wrapper is only for those methods.
2121
22-
**Args:**
23-
- `model`: The model to fit, which should be an instance of `Model`.
24-
- `initial_state`: Initial guess for the model parameters as a 1D tensor.
25-
- `method`: The optimization method to use. Default is "Nelder-Mead", but can be set to any of: "Nelder-Mead", "L-BFGS-B", "TNC", "SLSQP", "Powell", or "trust-constr".
26-
- `ndf`: Optional number of degrees of freedom for the fit. If not provided, it is calculated as the number of data points minus the number of parameters.
22+
:param model: The model to fit, which should be an instance of `Model`.
23+
:param initial_state: Initial guess for the model parameters as a 1D Array.
24+
:param method: The optimization method to use. Default is "Nelder-Mead", but can be set to any of: "Nelder-Mead", "L-BFGS-B", "TNC", "SLSQP", "Powell", or "trust-constr".
25+
:param ndf: Optional number of degrees of freedom for the fit. If not provided, it is calculated as the number of data points minus the number of parameters.
2726
"""
2827

2928
def __init__(

astrophot/image/cmos_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from .mixins import CMOSMixin
66
from .model_image import ModelImage
77
from ..backend_obj import backend
8+
from ..utils.decorators import combine_docstrings
89

10+
__all__ = ("CMOSModelImage", "CMOSTargetImage")
911

12+
13+
@combine_docstrings
1014
class CMOSModelImage(CMOSMixin, ModelImage):
1115
"""A ModelImage with CMOS-specific functionality."""
1216

1317

18+
@combine_docstrings
1419
class CMOSTargetImage(CMOSMixin, TargetImage):
1520
"""
1621
A TargetImage with CMOS-specific functionality.

0 commit comments

Comments
 (0)