|
| 1 | +import inspect |
| 2 | +from functools import partial |
| 3 | + |
| 4 | +from numpy.testing import assert_array_almost_equal |
| 5 | + |
| 6 | +from aeon.base._base import _clone_estimator |
| 7 | +from aeon.testing.testing_config import ( |
| 8 | + MULTITHREAD_TESTING, |
| 9 | + NON_STATE_CHANGING_METHODS_ARRAYLIKE, |
| 10 | +) |
| 11 | +from aeon.testing.utils.estimator_checks import _get_tag, _run_estimator_method |
| 12 | +from aeon.utils.validation import check_n_jobs |
| 13 | + |
| 14 | + |
| 15 | +def _yield_multithreading_checks(estimator_class, estimator_instances, datatypes): |
| 16 | + """Yield all multithreading checks for an aeon estimator.""" |
| 17 | + can_thread = _get_tag(estimator_class, "capability:multithreading") |
| 18 | + |
| 19 | + # only class required |
| 20 | + if can_thread: |
| 21 | + yield partial(check_multithreading_param, estimator_class=estimator_class) |
| 22 | + else: |
| 23 | + yield partial(check_no_multithreading_param, estimator_class=estimator_class) |
| 24 | + |
| 25 | + if can_thread and MULTITHREAD_TESTING: |
| 26 | + # test class instances |
| 27 | + for i, estimator in enumerate(estimator_instances): |
| 28 | + # test all data types |
| 29 | + for datatype in datatypes[i]: |
| 30 | + yield partial( |
| 31 | + check_estimator_multithreading, |
| 32 | + estimator=estimator, |
| 33 | + datatype=datatype, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def check_multithreading_param(estimator_class): |
| 38 | + """Test that estimators that can multithread have a n_jobs parameter.""" |
| 39 | + default_params = inspect.signature(estimator_class.__init__).parameters |
| 40 | + n_jobs = default_params.get("n_jobs", None) |
| 41 | + |
| 42 | + # check that the estimator has a n_jobs parameter |
| 43 | + if n_jobs is None: |
| 44 | + raise ValueError( |
| 45 | + f"{estimator_class} which sets " |
| 46 | + "capability:multithreading=True must have a n_jobs parameter." |
| 47 | + ) |
| 48 | + |
| 49 | + # check that the default value is to use 1 thread |
| 50 | + if n_jobs.default != 1: |
| 51 | + raise ValueError( |
| 52 | + "n_jobs parameter must have a default value of 1, " |
| 53 | + "disabling multithreading by default." |
| 54 | + ) |
| 55 | + |
| 56 | + # test parameters should not change the default value |
| 57 | + params = estimator_class._get_test_params() |
| 58 | + if not isinstance(params, list): |
| 59 | + params = [params] |
| 60 | + for param_set in params: |
| 61 | + assert "n_jobs" not in param_set |
| 62 | + |
| 63 | + |
| 64 | +def check_no_multithreading_param(estimator_class): |
| 65 | + """Test that estimators that cant multithread have no n_jobs parameter.""" |
| 66 | + default_params = inspect.signature(estimator_class.__init__).parameters |
| 67 | + |
| 68 | + # check that the estimator does not have a n_jobs parameter |
| 69 | + if default_params.get("n_jobs", None) is not None: |
| 70 | + raise ValueError( |
| 71 | + f"{estimator_class} has a n_jobs parameter, but does not set " |
| 72 | + "capability:multithreading=True in its tags." |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def check_estimator_multithreading(estimator, datatype): |
| 77 | + """Test that multithreaded estimators store n_jobs_ and produce same results.""" |
| 78 | + st_estimator = _clone_estimator(estimator, random_state=42) |
| 79 | + mt_estimator = _clone_estimator(estimator, random_state=42) |
| 80 | + n_jobs = max(2, check_n_jobs(-2)) |
| 81 | + mt_estimator.set_params(n_jobs=n_jobs) |
| 82 | + |
| 83 | + # fit and get results for single thread estimator |
| 84 | + _run_estimator_method(st_estimator, "fit", datatype, "train") |
| 85 | + |
| 86 | + results = [] |
| 87 | + for method in NON_STATE_CHANGING_METHODS_ARRAYLIKE: |
| 88 | + if hasattr(st_estimator, method) and callable(getattr(estimator, method)): |
| 89 | + output = _run_estimator_method(st_estimator, method, datatype, "test") |
| 90 | + results.append(output) |
| 91 | + |
| 92 | + # fit multithreaded estimator |
| 93 | + _run_estimator_method(mt_estimator, "fit", datatype, "train") |
| 94 | + |
| 95 | + # check n_jobs_ attribute is set |
| 96 | + assert mt_estimator.n_jobs_ == n_jobs, ( |
| 97 | + f"Multithreaded estimator {mt_estimator} does not store n_jobs_ " |
| 98 | + f"attribute correctly. Expected {n_jobs}, got {mt_estimator.n_jobs_}." |
| 99 | + f"It is recommended to use the check_n_jobs function to set n_jobs_ and use" |
| 100 | + f"this for any multithreading." |
| 101 | + ) |
| 102 | + |
| 103 | + # compare results from single and multithreaded estimators |
| 104 | + i = 0 |
| 105 | + for method in NON_STATE_CHANGING_METHODS_ARRAYLIKE: |
| 106 | + if hasattr(estimator, method) and callable(getattr(estimator, method)): |
| 107 | + output = _run_estimator_method(estimator, method, datatype, "test") |
| 108 | + |
| 109 | + assert_array_almost_equal( |
| 110 | + output, |
| 111 | + results[i], |
| 112 | + err_msg=f"Running {method} after fit twice with test " |
| 113 | + f"parameters gives different results.", |
| 114 | + ) |
| 115 | + i += 1 |
0 commit comments