Skip to content

Commit d72b257

Browse files
committed
chore: add an example
1 parent 100e77d commit d72b257

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

CONTRIBUTING.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,28 @@ Following the issue [#2230](https://github.com/pytorch/ignite/issues/2230), PyTo
330330
[`.. testcode::`]: https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html#directive-testcode
331331
[`.. testoutput::`]: https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html#directive-testoutput
332332

333-
PyTorch-Ignite uses the second option, **Sphinx directives**. Every code that needs to be tested should be under `.. testcode::` and expected output should be under `.. testoutput::`.
333+
PyTorch-Ignite uses the second option, **Sphinx directives**. Every code that needs to be tested should be under `.. testcode::` and expected output should be under `.. testoutput::`. For example:
334334

335-
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 `...`. Refer [an example here](https://github.com/pytorch/ignite/pull/2241/files#diff-fade24f3d7d60aa791f85f0552747eae4deef34314e9d509daf791524dc78d3fR54).
335+
```py
336+
.. testcode::
337+
338+
def process_function(engine, batch):
339+
y_pred, y = batch
340+
return y_pred, y
341+
engine = Engine(process_function)
342+
metric = SSIM(data_range=1.0)
343+
metric.attach(engine, 'ssim')
344+
preds = torch.rand([4, 3, 16, 16])
345+
target = preds * 0.75
346+
state = engine.run([[preds, target]])
347+
print(state.metrics['ssim'])
348+
349+
.. testoutput::
350+
351+
0.9218971...
352+
```
353+
354+
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 `...`.
336355

337356
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.
338357

0 commit comments

Comments
 (0)