diff --git a/pyproject.toml b/pyproject.toml index fc3e6928..176bb19d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,7 @@ dependencies = ["numpy>=1.19.2,<2.0", "scipy>=1.7.0", "matplotlib>=3.3.2"] dynamic = ["version"] [project.optional-dependencies] + dev = [ "pytest >=7.0.0,<8.0.0", "pytest-cov >=2.12.0,<4.0.0", @@ -87,7 +88,7 @@ doc = [ # avoid jinja import error using 3.0.3 version "jinja2==3.0.3", ] -all = ["torch >=1.7.1"] +all = ["torch >=1.7.1, <2.4.0"] [project.urls] homepage = "http://sysidentpy.org" diff --git a/sysidentpy/general_estimators/narx.py b/sysidentpy/general_estimators/narx.py index 576721ad..bd5442ac 100644 --- a/sysidentpy/general_estimators/narx.py +++ b/sysidentpy/general_estimators/narx.py @@ -160,7 +160,12 @@ def fit(self, *, X=None, y=None): self.max_lag = self._get_max_lag() lagged_data = self.build_matrix(X, y) reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) if X is not None: @@ -213,7 +218,7 @@ def predict( The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) @@ -263,11 +268,7 @@ def _one_step_ahead_prediction(self, X, y): """ lagged_data = self.build_matrix(X, y) X_base = self.basis_function.transform( - lagged_data, - self.max_lag, - self.ylag, - self.xlag, - self.model_type + lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type ) yhat = self.base_estimator.predict(X_base) @@ -485,11 +486,7 @@ def _basis_function_predict(self, X, y_initial, forecast_horizon=None): yhat[i : i + analyzed_elements_number].reshape(-1, 1), ) X_tmp = self.basis_function.transform( - lagged_data, - self.max_lag, - self.ylag, - self.xlag, - self.model_type + lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type ) a = self.base_estimator.predict(X_tmp) diff --git a/sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py b/sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py index 77ec0c94..21fa9af1 100644 --- a/sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py +++ b/sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py @@ -308,7 +308,12 @@ def fit(self, *, X: Optional[np.ndarray] = None, y: Optional[np.ndarray] = None) self.max_lag = self._get_max_lag() lagged_data = self.build_matrix(X, y) reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) if X is not None: @@ -371,7 +376,7 @@ def predict( The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) diff --git a/sysidentpy/model_structure_selection/entropic_regression.py b/sysidentpy/model_structure_selection/entropic_regression.py index 4c6db987..668125d0 100644 --- a/sysidentpy/model_structure_selection/entropic_regression.py +++ b/sysidentpy/model_structure_selection/entropic_regression.py @@ -584,7 +584,12 @@ def fit(self, *, X=None, y=None): lagged_data = self.build_matrix(X, y) reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) if X is not None: @@ -687,7 +692,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None): The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) diff --git a/sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py b/sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py index 5ee4eb84..5c067715 100644 --- a/sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py +++ b/sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py @@ -602,7 +602,12 @@ def fit(self, *, X: Optional[np.ndarray] = None, y: np.ndarray): lagged_data = self.build_matrix(X, y) reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) if X is not None: @@ -689,7 +694,7 @@ def predict( The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) diff --git a/sysidentpy/model_structure_selection/meta_model_structure_selection.py b/sysidentpy/model_structure_selection/meta_model_structure_selection.py index ec5b4174..85ed5cef 100644 --- a/sysidentpy/model_structure_selection/meta_model_structure_selection.py +++ b/sysidentpy/model_structure_selection/meta_model_structure_selection.py @@ -385,7 +385,12 @@ def evaluate_objective_function( lagged_data = self.build_matrix(X_train, y_train) psi = self.basis_function.fit( - lagged_data, self.max_lag, self.xlag, self.ylag, self.model_type, predefined_regressors=self.pivv + lagged_data, + self.max_lag, + self.xlag, + self.ylag, + self.model_type, + predefined_regressors=self.pivv, ) pos_insignificant_terms, _, _ = self.perform_t_test( @@ -607,7 +612,7 @@ def predict( The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) diff --git a/sysidentpy/neural_network/narx_nn.py b/sysidentpy/neural_network/narx_nn.py index ee0dc3eb..0be6b392 100644 --- a/sysidentpy/neural_network/narx_nn.py +++ b/sysidentpy/neural_network/narx_nn.py @@ -271,15 +271,24 @@ def split_data(self, X, y): self.max_lag = self._get_max_lag() lagged_data = self.build_matrix(X, y) - basis_name = self.basis_function.__class__.__name__ - if basis_name == "Polynomial": + if isinstance(self.basis_function, Polynomial): reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) reg_matrix = reg_matrix[:, 1:] else: reg_matrix = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=None, ) if X is not None: @@ -476,7 +485,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None): The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: return self._model_prediction(X, y, forecast_horizon=forecast_horizon) if steps_ahead == 1: @@ -513,23 +522,14 @@ def _one_step_ahead_prediction(self, X, y): """ lagged_data = self.build_matrix(X, y) - basis_name = self.basis_function.__class__.__name__ - if basis_name == "Polynomial": + if isinstance(self.basis_function, Polynomial): X_base = self.basis_function.transform( - lagged_data, - self.max_lag, - self.ylag, - self.xlag, - self.model_type + lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type ) X_base = X_base[:, 1:] else: X_base = self.basis_function.transform( - lagged_data, - self.max_lag, - self.ylag, - self.xlag, - self.model_type + lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type ) yhat = np.zeros(X.shape[0], dtype=float) @@ -716,11 +716,7 @@ def _basis_function_predict(self, X, y_initial, forecast_horizon=None): ) X_tmp = self.basis_function.transform( - lagged_data, - self.max_lag, - self.ylag, - self.xlag, - self.model_type + lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type ) X_tmp = np.atleast_1d(X_tmp).astype(np.float32) yhat = yhat.astype(np.float32) diff --git a/sysidentpy/simulation/_simulation.py b/sysidentpy/simulation/_simulation.py index f8ce542b..81729b68 100644 --- a/sysidentpy/simulation/_simulation.py +++ b/sysidentpy/simulation/_simulation.py @@ -179,7 +179,7 @@ def _validate_simulate_params(self): ) def _check_simulate_params(self, y_train, y_test, model_code, steps_ahead, theta): - if self.basis_function.__class__.__name__ != "Polynomial": + if not isinstance(self.basis_function, Polynomial): raise NotImplementedError( "Currently, SimulateNARMAX only works for polynomial models." ) @@ -277,7 +277,12 @@ def simulate( self.max_lag = self._get_max_lag() lagged_data = self.build_matrix(X_train, y_train) psi = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=self.pivv + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=self.pivv, ) self.theta = self.estimator.optimize( @@ -303,7 +308,12 @@ def simulate( self.max_lag = self._get_max_lag() lagged_data = self.build_matrix(X_train, y_train) psi = self.basis_function.fit( - lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=self.pivv + lagged_data, + self.max_lag, + self.ylag, + self.xlag, + self.model_type, + predefined_regressors=self.pivv, ) _, self.err, _, _ = self.error_reduction_ratio( @@ -434,7 +444,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None): The predicted values of the model. """ - if self.basis_function.__class__.__name__ == "Polynomial": + if isinstance(self.basis_function, Polynomial): if steps_ahead is None: yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon) yhat = np.concatenate([y[: self.max_lag], yhat], axis=0) diff --git a/sysidentpy/utils/narmax_tools.py b/sysidentpy/utils/narmax_tools.py index 46e9a307..3f3076fd 100644 --- a/sysidentpy/utils/narmax_tools.py +++ b/sysidentpy/utils/narmax_tools.py @@ -4,6 +4,7 @@ import numpy as np from ..narmax_base import RegressorDictionary +from ..basis_function import Polynomial from ._check_arrays import _num_features @@ -47,24 +48,28 @@ def regressor_code( xlag=xlag, ylag=ylag, model_type=model_type, basis_function=basis_function ).regressor_space(n_inputs) - basis_name = basis_function.__class__.__name__ - if basis_name != "Polynomial" and basis_function.ensemble: + if not isinstance(basis_function, Polynomial) and basis_function.ensemble: repetition = basis_function.n * 2 basis_code = np.sort( np.tile(encoding[1:, :], (repetition, 1)), axis=0, ) encoding = np.concatenate([encoding[1:], basis_code]) - elif basis_name != "Polynomial" and basis_function.ensemble is False: + elif ( + not isinstance(basis_function, Polynomial) and basis_function.ensemble is False + ): repetition = basis_function.n * 2 encoding = np.sort( np.tile(encoding[1:, :], (repetition, 1)), axis=0, ) - if basis_name == "Polynomial" and model_representation == "neural_network": + if ( + isinstance(basis_function, Polynomial) + and model_representation == "neural_network" + ): return encoding[1:] - if basis_name == "Polynomial" and model_representation is None: + if isinstance(basis_function, Polynomial) and model_representation is None: return encoding return encoding