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

get help from docstring #4344

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `fsspec` support for profilers ([#4162](https://github.com/PyTorchLightning/pytorch-lightning/pull/4162))


- Added autogenerated helptext to `Trainer.add_argparse_args`. ([#4344](https://github.com/PyTorchLightning/pytorch-lightning/pull/4344))


### Changed


Expand Down
29 changes: 26 additions & 3 deletions pytorch_lightning/utilities/argparse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import inspect
import os
from argparse import ArgumentParser, Namespace
from typing import Union, List, Tuple, Any
from typing import Dict, Union, List, Tuple, Any
from pytorch_lightning.utilities import parsing


Expand Down Expand Up @@ -160,7 +160,7 @@ def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser:

allowed_types = (str, int, float, bool)

# TODO: get "help" from docstring :)
args_help = parse_args_from_docstring(cls.__init__.__doc__ or cls.__doc__)
for arg, arg_types, arg_default in (
at for at in get_init_arguments_and_types(cls) if at[0] not in depr_arg_names
):
Expand Down Expand Up @@ -200,13 +200,36 @@ def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser:
dest=arg,
default=arg_default,
type=use_type,
help='autogenerated by pl.Trainer',
help=args_help.get(arg),
**arg_kwargs,
)

return parser


def parse_args_from_docstring(docstring: str) -> Dict[str, str]:
arg_block_indent = None
current_arg = None
parsed = {}
for line in docstring.split("\n"):
stripped = line.lstrip()
if not stripped:
continue
line_indent = len(line) - len(stripped)
if stripped.startswith(('Args:', 'Arguments:', 'Parameters:')):
arg_block_indent = line_indent + 4
elif arg_block_indent is None:
continue
elif line_indent < arg_block_indent:
break
elif line_indent == arg_block_indent:
current_arg, arg_description = stripped.split(':', maxsplit=1)
parsed[current_arg] = arg_description.lstrip()
elif line_indent > arg_block_indent:
parsed[current_arg] += f' {stripped}'
return parsed


def _gpus_allowed_type(x) -> Union[int, str]:
if ',' in x:
return str(x)
Expand Down
50 changes: 50 additions & 0 deletions tests/utilities/test_argparse_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pytorch_lightning.utilities.argparse_utils import parse_args_from_docstring


def test_parse_args_from_docstring_normal():
args_help = parse_args_from_docstring(
"""Constrain image dataset

Args:
root: Root directory of dataset where ``MNIST/processed/training.pt``
and ``MNIST/processed/test.pt`` exist.
train: If ``True``, creates dataset from ``training.pt``,
otherwise from ``test.pt``.
normalize: mean and std deviation of the MNIST dataset.
download: If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
num_samples: number of examples per selected class/digit
digits: list selected MNIST digits/classes

Examples:
>>> dataset = TrialMNIST(download=True)
>>> len(dataset)
300
>>> sorted(set([d.item() for d in dataset.targets]))
[0, 1, 2]
>>> torch.bincount(dataset.targets)
tensor([100, 100, 100])
"""
)

expected_args = ['root', 'train', 'normalize', 'download', 'num_samples', 'digits']
assert len(args_help.keys()) == len(expected_args)
assert all([x == y for x, y in zip(args_help.keys(), expected_args)])
assert args_help['root'] == 'Root directory of dataset where ``MNIST/processed/training.pt``' \
' and ``MNIST/processed/test.pt`` exist.'
assert args_help['normalize'] == 'mean and std deviation of the MNIST dataset.'


def test_parse_args_from_docstring_empty():
args_help = parse_args_from_docstring(
"""Constrain image dataset

Args:

Returns:

Examples:
"""
)
assert len(args_help.keys()) == 0