Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ready for review) Draft docstring template #690

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Each pull request should include:
- The implementation of the new metric or score in xarray, ideally with support for pandas and dask.
- 100% unit test coverage.
- A tutorial notebook showcasing the use of that metric or score, ideally based on the standard sample data.
- API documentation (docstrings) using [Napoleon (google)](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style, making sure to clearly explain the use of the metric.
- API documentation (docstrings) using [Napoleon (google)](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style, making sure to clearly explain the use of the metric. Please see our [docstring template](docstring_template.md).
- A reference should be added to the API documentation. The preferred referencing style is [APA (7th edition)](https://apastyle.apa.org/style-grammar-guidelines/references/examples).
- If there is an authoritative reference (e.g. an academic article that defines the metric, or the specific implementation, being used), please cite that reference.
- When there is an authoritative reference, please cite it even if it cannot be accessed without payment. In these instances, if there is another suitable reference that is open and available without payment, cite that as well.
Expand Down
72 changes: 72 additions & 0 deletions docs/docstring_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Docstring Template
--------------------

Below is a docstring example. It contains a suggested docstring layout, example text and example MathJax.

The layout below will render properly in Read the Docs. Please adjust the example text and MathJax as required.

Note: backticks render differently in docstrings to elsewhere. Should you wish to include text in backticks, please use double backticks - e.g. ``scores.continuous.mean_error``. (If you only use one set of backticks, the text will instead be rendered as italics in Read the Docs).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the scores.continuous.mean_error example, when writing the name of a function it is better to use ":py:func:`"

Consider updating this to suggest writing function names with ":py:func:" and non-functions that you want to render as a block (or whatever it's called) with double backtracks (e.g., reduce_dims="all"). I hope that makes sense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you use ":py:func:" further down in the example. Consider just updating this example to something like reduce_dims="all"


```
def function_name(*args, *, **kwargs) -> type hint:
'''
A short one or two line description of what the function does.

Additional information (e.g. a paragraph or two) may be included here following the short description.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer lines aren't wrapping on 228. I suggest limiting the line length so that it renders correctly


MathJax giving the mathematical formula for the metric. Below is some MathJax to use as a
starting point. Note that double-backslash formatting is required to ensure proper rendering in docstrings.

.. math::
\\text{mean error} =\\frac{1}{N}\\sum_{i=1}^{N}(x_i - y_i)
\\text{where } x = \\text{the forecast, and } y = \\text{the observation}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be split into two sections, otherwise the second line becomes part of the first equation. I suggest just using :math:` around the x and y


Where applicable, say what the range of the score is. For example:
Range: 0 to infinity, 0 is perfect.

Mathjax can also be included inline, like this :math:`\\text{obs_cdf}(x) = 0`

Args:
fcst: Forecast data.
obs: Observation data.
reduce_dims: Dimensions to reduce. Can be "all" to reduce all dimensions.
preserve_dims: Dimensions to preserve. Can be "all" to preserve all dimensions.
weights: Weights for calculating a weighted mean of individual scores (e.g. by area, by latitude,
by population, custom). Note that these weights are different to threshold weighting which is done
by decision threshold.

Returns:
A semantic description of what is returned. Note - type information is automatically included based on type hinting.

Raises:
A type and description of any special error checking which may result in an exception, e.g.
ValueError: if there are values in `fcst` and `obs` which are invalid

Notes:
- This section is optional.
- Any additional comments or notes should go here.

References:
- If possible, a citation to the first (original) paper introducing the score/metric.
- In addition, if that paper is not open access, please also add an open access reference.
- The preferred referencing style for journal articles is [APA (7th edition).](https://apastyle.apa.org/style-grammar-guidelines/references/examples/journal-article-references)
- Example reference below:
- Sorooshian, S., Duan, Q., & Gupta, V. K. (1993). Calibration of rainfall-runoff models:
Application of global optimization to the Sacramento Soil Moisture Accounting Model.
Water Resources Research, 29(4), 1185-1194. https://doi.org/10.1029/92WR02617
- Optionally, if there is a website(s) that has particularly good information about the metric, please feel free to list it as well.

See also:
- This section is optional.
- If there are other functions which are directly relevant, you can add e.g. :py:func:`scores.continuous.rmse` with a description.

Examples:

>>> import numpy as np # NOTE - the previous line should be empty for rendering to work properly.
>>> import xarray as xr
>>> from scores.probability import interval_tw_crps_for_ensemble
>>> fcst = xr.DataArray(np.random.uniform(-40, 10, size=(10, 10)), dims=['time', 'ensemble'])
>>> obs = xr.DataArray(np.random.uniform(-40, 10, size=10), dims=['time'])

'''
```