Skip to content

Commit 4287069

Browse files
authored
chore(contributing): update for make doctest [skip ci] (#2278)
* chore(contributing): update for `make doctest` * chore: update about `...` [skip ci] * chore: add an example * chore: directly state sphinx directive + example
1 parent 6174931 commit 4287069

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

CONTRIBUTING.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,39 @@ python -m http.server <port>
319319
```
320320

321321
Then open the browser at `localhost:<port>` (e.g. `localhost:1234`) and click to `html` folder.
322+
323+
#### Examples testing (doctests)
324+
325+
PyTorch-Ignite uses **Sphinx directives**. Every code that needs to be tested
326+
should be under `.. testcode::` and expected output should be under
327+
`.. testoutput::`. For example:
328+
329+
```py
330+
.. testcode::
331+
332+
def process_function(engine, batch):
333+
y_pred, y = batch
334+
return y_pred, y
335+
engine = Engine(process_function)
336+
metric = SSIM(data_range=1.0)
337+
metric.attach(engine, 'ssim')
338+
preds = torch.rand([4, 3, 16, 16])
339+
target = preds * 0.75
340+
state = engine.run([[preds, target]])
341+
print(state.metrics['ssim'])
342+
343+
.. testoutput::
344+
345+
0.9218971...
346+
```
347+
348+
If the floating point results are needed for assertion and the results can vary per operating systems and PyTorch versions, we could assert the results up to 4 or 6 decimal places and match the rest of the results with `...`. Learn more about `sphinx.ext.doctest` in [the official documentation](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html).
349+
350+
To make writing doctests easy, there are some configuratons defined in `conf.py`. Search `doctest_global_setup` in [conf.py](docs/source/conf.py) to see which variables and functions are available.
351+
352+
To run doctests locally:
353+
354+
```sh
355+
cd docs
356+
make html && make doctest
357+
```

0 commit comments

Comments
 (0)