|
| 1 | +# copyright: skpro developers, BSD-3-Clause License (see LICENSE file) |
| 2 | +"""Registry and dispatcher for test classes. |
| 3 | +
|
| 4 | +Module does not contain tests, only test utilities. |
| 5 | +""" |
| 6 | + |
| 7 | +__author__ = ["fkiraly"] |
| 8 | + |
| 9 | +from inspect import isclass |
| 10 | + |
| 11 | + |
| 12 | +def get_test_class_registry(): |
| 13 | + """Return test class registry. |
| 14 | +
|
| 15 | + Wrapped in a function to avoid circular imports. |
| 16 | +
|
| 17 | + Returns |
| 18 | + ------- |
| 19 | + testclass_dict : dict |
| 20 | + test class registry |
| 21 | + keys are scitypes, values are test classes TestAll[Scitype] |
| 22 | + """ |
| 23 | + from hyperactive.tests.test_all_objects import ( |
| 24 | + TestAllExperiments, |
| 25 | + TestAllObjects, |
| 26 | + TestAllOptimizers, |
| 27 | + ) |
| 28 | + |
| 29 | + testclass_dict = dict() |
| 30 | + # every object in sktime inherits from BaseObject |
| 31 | + # "object" tests are run for all objects |
| 32 | + testclass_dict["object"] = TestAllObjects |
| 33 | + # more specific base classes |
| 34 | + # these inherit either from BaseEstimator or BaseObject, |
| 35 | + # so also imply estimator and object tests, or only object tests |
| 36 | + testclass_dict["experiment"] = TestAllExperiments |
| 37 | + testclass_dict["optimizer"] = TestAllOptimizers |
| 38 | + |
| 39 | + return testclass_dict |
| 40 | + |
| 41 | + |
| 42 | +def get_test_classes_for_obj(obj): |
| 43 | + """Get all test classes relevant for an object or estimator. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + obj : object or estimator, descendant of sktime BaseObject or BaseEstimator |
| 48 | + object or estimator for which to get test classes |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + test_classes : list of test classes |
| 53 | + list of test classes relevant for obj |
| 54 | + these are references to the actual classes, not strings |
| 55 | + if obj was not a descendant of BaseObject or BaseEstimator, returns empty list |
| 56 | + """ |
| 57 | + from skbase.base import BaseObject |
| 58 | + |
| 59 | + def is_object(obj): |
| 60 | + """Return whether obj is an estimator class or estimator object.""" |
| 61 | + if isclass(obj): |
| 62 | + return issubclass(obj, BaseObject) |
| 63 | + else: |
| 64 | + return isinstance(obj, BaseObject) |
| 65 | + |
| 66 | + # warning: BaseEstimator does not inherit from BaseObject, |
| 67 | + # therefore we need to check both |
| 68 | + if not is_object(obj): |
| 69 | + return [] |
| 70 | + |
| 71 | + testclass_dict = get_test_class_registry() |
| 72 | + |
| 73 | + # we always need to run "object" tests |
| 74 | + test_clss = [testclass_dict["object"]] |
| 75 | + |
| 76 | + try: |
| 77 | + if isclass(obj): |
| 78 | + obj_scitypes = obj.get_class_tag("object_type") |
| 79 | + elif hasattr(obj, "get_tag"): |
| 80 | + obj_scitypes = obj.get_tag("object_type") |
| 81 | + else: |
| 82 | + obj_scitypes = [] |
| 83 | + except Exception: |
| 84 | + obj_scitypes = [] |
| 85 | + |
| 86 | + if isinstance(obj_scitypes, str): |
| 87 | + # if obj_scitypes is a string, convert to list |
| 88 | + obj_scitypes = [obj_scitypes] |
| 89 | + |
| 90 | + for obj_scitype in obj_scitypes: |
| 91 | + if obj_scitype in testclass_dict: |
| 92 | + test_clss += [testclass_dict[obj_scitype]] |
| 93 | + |
| 94 | + return test_clss |
0 commit comments