-
Notifications
You must be signed in to change notification settings - Fork 733
[ENH] test suite for pytorch-forecasting forecasters
#1780
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
Merged
Merged
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
b3644a6
test suite
fkiraly a1d64c6
Merge branch 'main' into test-suite
fkiraly 4b2486e
skeleton
fkiraly 02b0ce6
skeleton
fkiraly 41cbf66
Update test_all_estimators.py
fkiraly cef62d3
Update _base_object.py
fkiraly bc2e93b
Update _lookup.py
fkiraly eee1c86
Update _lookup.py
fkiraly 164fe0d
base metadatda
fkiraly 20e88d0
registry
fkiraly 318c1fb
fix private name
fkiraly 012ab3d
Update _base_object.py
fkiraly 86365a0
test failure
fkiraly f6dee46
Update test_all_estimators.py
fkiraly 9b0e4ec
Update test_all_estimators.py
fkiraly 7de5285
Update test_all_estimators.py
fkiraly 57dfe3a
test folders
fkiraly c9f12db
Update test.yml
fkiraly fa8144e
test integration
fkiraly 232a510
fixes
fkiraly 1c8d4b5
Update _conftest.py
fkiraly f632e32
try scenarios
fkiraly ef37f55
Merge branch 'main' into test-suite
fkiraly a669134
Update _lookup.py
fkiraly d78bf5d
Update _lookup.py
fkiraly 616aba7
Merge branch 'main' into test-suite
fkiraly 94e8ce8
move cell_type and n_plotting samples to kwargs
fkiraly 5d2d001
doctest runner fixed
fkiraly 487c981
Update test_all_estimators.py
fkiraly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """PyTorch Forecasting registry.""" | ||
|
|
||
| from pytorch_forecasting._registry._lookup import all_objects | ||
|
|
||
| __all__ = ["all_objects"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| """Registry lookup methods. | ||
|
|
||
| This module exports the following methods for registry lookup: | ||
|
|
||
| all_objects(object_types, filter_tags) | ||
| lookup and filtering of objects | ||
| """ | ||
|
|
||
| # based on the sktime module of same name | ||
|
|
||
| __author__ = ["fkiraly"] | ||
| # all_objects is based on the sklearn utility all_estimators | ||
|
|
||
| from inspect import isclass | ||
| from pathlib import Path | ||
|
|
||
| from skbase.lookup import all_objects as _all_objects | ||
|
|
||
| from pytorch_forecasting.models.base import _BaseObject | ||
|
|
||
|
|
||
| def all_objects( | ||
| object_types=None, | ||
| filter_tags=None, | ||
| exclude_objects=None, | ||
| return_names=True, | ||
| as_dataframe=False, | ||
| return_tags=None, | ||
| suppress_import_stdout=True, | ||
| ): | ||
| """Get a list of all objects from pytorch_forecasting. | ||
|
|
||
| This function crawls the module and gets all classes that inherit | ||
| from skbase compatible base classes. | ||
|
|
||
| Not included are: the base classes themselves, classes defined in test | ||
| modules. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| object_types: str, list of str, optional (default=None) | ||
| Which kind of objects should be returned. | ||
|
|
||
| * if None, no filter is applied and all objects are returned. | ||
| * if str or list of str, strings define scitypes specified in search | ||
| only objects that are of (at least) one of the scitypes are returned | ||
|
|
||
| return_names: bool, optional (default=True) | ||
|
|
||
| * if True, estimator class name is included in the ``all_objects`` | ||
| return in the order: name, estimator class, optional tags, either as | ||
| a tuple or as pandas.DataFrame columns | ||
| * if False, estimator class name is removed from the ``all_objects`` return. | ||
|
|
||
| filter_tags: dict of (str or list of str or re.Pattern), optional (default=None) | ||
| For a list of valid tag strings, use the registry.all_tags utility. | ||
|
|
||
| ``filter_tags`` subsets the returned objects as follows: | ||
|
|
||
| * each key/value pair is statement in "and"/conjunction | ||
| * key is tag name to sub-set on | ||
| * value str or list of string are tag values | ||
| * condition is "key must be equal to value, or in set(value)" | ||
|
|
||
| In detail, he return will be filtered to keep exactly the classes | ||
| where tags satisfy all the filter conditions specified by ``filter_tags``. | ||
| Filter conditions are as follows, for ``tag_name: search_value`` pairs in | ||
| the ``filter_tags`` dict, applied to a class ``klass``: | ||
|
|
||
| - If ``klass`` does not have a tag with name ``tag_name``, it is excluded. | ||
| Otherwise, let ``tag_value`` be the value of the tag with name ``tag_name``. | ||
| - If ``search_value`` is a string, and ``tag_value`` is a string, | ||
| the filter condition is that ``search_value`` must match the tag value. | ||
| - If ``search_value`` is a string, and ``tag_value`` is a list, | ||
| the filter condition is that ``search_value`` is contained in ``tag_value``. | ||
| - If ``search_value`` is a ``re.Pattern``, and ``tag_value`` is a string, | ||
| the filter condition is that ``search_value.fullmatch(tag_value)`` | ||
| is true, i.e., the regex matches the tag value. | ||
| - If ``search_value`` is a ``re.Pattern``, and ``tag_value`` is a list, | ||
| the filter condition is that at least one element of ``tag_value`` | ||
| matches the regex. | ||
| - If ``search_value`` is iterable, then the filter condition is that | ||
| at least one element of ``search_value`` satisfies the above conditions, | ||
| applied to ``tag_value``. | ||
|
|
||
| Note: ``re.Pattern`` is supported only from ``scikit-base`` version 0.8.0. | ||
|
|
||
| exclude_objects: str, list of str, optional (default=None) | ||
| Names of objects to exclude. | ||
|
|
||
| as_dataframe: bool, optional (default=False) | ||
|
|
||
| * True: ``all_objects`` will return a ``pandas.DataFrame`` with named | ||
| columns for all of the attributes being returned. | ||
| * False: ``all_objects`` will return a list (either a list of | ||
| objects or a list of tuples, see Returns) | ||
|
|
||
| return_tags: str or list of str, optional (default=None) | ||
| Names of tags to fetch and return each estimator's value of. | ||
| For a list of valid tag strings, use the ``registry.all_tags`` utility. | ||
| if str or list of str, | ||
| the tag values named in return_tags will be fetched for each | ||
| estimator and will be appended as either columns or tuple entries. | ||
|
|
||
| suppress_import_stdout : bool, optional. Default=True | ||
| whether to suppress stdout printout upon import. | ||
|
|
||
| Returns | ||
| ------- | ||
| all_objects will return one of the following: | ||
|
|
||
| 1. list of objects, if ``return_names=False``, and ``return_tags`` is None | ||
|
|
||
| 2. list of tuples (optional estimator name, class, optional estimator | ||
| tags), if ``return_names=True`` or ``return_tags`` is not ``None``. | ||
|
|
||
| 3. ``pandas.DataFrame`` if ``as_dataframe = True`` | ||
|
|
||
| if list of objects: | ||
| entries are objects matching the query, | ||
| in alphabetical order of estimator name | ||
|
|
||
| if list of tuples: | ||
| list of (optional estimator name, estimator, optional estimator | ||
| tags) matching the query, in alphabetical order of estimator name, | ||
| where | ||
| ``name`` is the estimator name as string, and is an | ||
| optional return | ||
| ``estimator`` is the actual estimator | ||
| ``tags`` are the estimator's values for each tag in return_tags | ||
| and is an optional return. | ||
|
|
||
| if ``DataFrame``: | ||
| column names represent the attributes contained in each column. | ||
| "objects" will be the name of the column of objects, "names" | ||
| will be the name of the column of estimator class names and the string(s) | ||
| passed in return_tags will serve as column names for all columns of | ||
| tags that were optionally requested. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from pytorch_forecasting._registry import all_objects | ||
| >>> # return a complete list of objects as pd.Dataframe | ||
| >>> all_objects(as_dataframe=True) # doctest: +SKIP | ||
|
|
||
| References | ||
| ---------- | ||
| Adapted version of sktime's ``all_estimators``, | ||
| which is an evolution of scikit-learn's ``all_estimators`` | ||
| """ | ||
| MODULES_TO_IGNORE = ( | ||
| "tests", | ||
| "setup", | ||
| "contrib", | ||
| "utils", | ||
| "all", | ||
| ) | ||
|
|
||
| result = [] | ||
| ROOT = str(Path(__file__).parent.parent) # package root directory | ||
|
|
||
| def _coerce_to_str(obj): | ||
| if isinstance(obj, (list, tuple)): | ||
| return [_coerce_to_str(o) for o in obj] | ||
| if isclass(obj): | ||
| obj = obj.get_tag("object_type") | ||
| return obj | ||
|
|
||
| def _coerce_to_list_of_str(obj): | ||
| obj = _coerce_to_str(obj) | ||
| if isinstance(obj, str): | ||
| return [obj] | ||
| return obj | ||
|
|
||
| if object_types is not None: | ||
| object_types = _coerce_to_list_of_str(object_types) | ||
| object_types = list(set(object_types)) | ||
|
|
||
| if object_types is not None: | ||
| if filter_tags is None: | ||
| filter_tags = {} | ||
| elif isinstance(filter_tags, str): | ||
| filter_tags = {filter_tags: True} | ||
| else: | ||
| filter_tags = filter_tags.copy() | ||
|
|
||
| if "object_type" in filter_tags: | ||
| obj_field = filter_tags["object_type"] | ||
| obj_field = _coerce_to_list_of_str(obj_field) | ||
| obj_field = obj_field + object_types | ||
| else: | ||
| obj_field = object_types | ||
|
|
||
| filter_tags["object_type"] = obj_field | ||
|
|
||
| result = _all_objects( | ||
| object_types=[_BaseObject], | ||
| filter_tags=filter_tags, | ||
| exclude_objects=exclude_objects, | ||
| return_names=return_names, | ||
| as_dataframe=as_dataframe, | ||
| return_tags=return_tags, | ||
| suppress_import_stdout=suppress_import_stdout, | ||
| package_name="pytorch_forecasting", | ||
| path=ROOT, | ||
| modules_to_ignore=MODULES_TO_IGNORE, | ||
| ) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def _check_list_of_str_or_error(arg_to_check, arg_name): | ||
| """Check that certain arguments are str or list of str. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| arg_to_check: argument we are testing the type of | ||
| arg_name: str, | ||
| name of the argument we are testing, will be added to the error if | ||
| ``arg_to_check`` is not a str or a list of str | ||
|
|
||
| Returns | ||
| ------- | ||
| arg_to_check: list of str, | ||
| if arg_to_check was originally a str it converts it into a list of str | ||
| so that it can be iterated over. | ||
|
|
||
| Raises | ||
| ------ | ||
| TypeError if arg_to_check is not a str or list of str | ||
| """ | ||
| # check that return_tags has the right type: | ||
| if isinstance(arg_to_check, str): | ||
| arg_to_check = [arg_to_check] | ||
| if not isinstance(arg_to_check, list) or not all( | ||
| isinstance(value, str) for value in arg_to_check | ||
| ): | ||
| raise TypeError( | ||
| f"Error in all_objects! Argument {arg_name} must be either\ | ||
| a str or list of str" | ||
| ) | ||
| return arg_to_check | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add these "coerce" functions to
utils._coercejust to make it usable all over the module?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, I thought about it and would say "no", because it assumes the presence of
get_tagmeans we have aBaseObjectdescendant instance.The
utils._coercemakes no specific assumptions on inheritance.I wonder how we could resolve this, maybe we move the
get_taglogic out?