Skip to content

Commit f9544c7

Browse files
authored
Merge pull request #141 from wilsonrljr/check_polynomial
change Polynomial check from class name to isinstance method.
2 parents f878e83 + aae0c80 commit f9544c7

File tree

9 files changed

+81
-52
lines changed

9 files changed

+81
-52
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dependencies = ["numpy>=1.19.2,<2.0", "scipy>=1.7.0", "matplotlib>=3.3.2"]
5757
dynamic = ["version"]
5858

5959
[project.optional-dependencies]
60+
6061
dev = [
6162
"pytest >=7.0.0,<8.0.0",
6263
"pytest-cov >=2.12.0,<4.0.0",
@@ -87,7 +88,7 @@ doc = [
8788
# avoid jinja import error using 3.0.3 version
8889
"jinja2==3.0.3",
8990
]
90-
all = ["torch >=1.7.1"]
91+
all = ["torch >=1.7.1, <2.4.0"]
9192

9293
[project.urls]
9394
homepage = "http://sysidentpy.org"

sysidentpy/general_estimators/narx.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ def fit(self, *, X=None, y=None):
160160
self.max_lag = self._get_max_lag()
161161
lagged_data = self.build_matrix(X, y)
162162
reg_matrix = self.basis_function.fit(
163-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
163+
lagged_data,
164+
self.max_lag,
165+
self.ylag,
166+
self.xlag,
167+
self.model_type,
168+
predefined_regressors=None,
164169
)
165170

166171
if X is not None:
@@ -213,7 +218,7 @@ def predict(
213218
The predicted values of the model.
214219
215220
"""
216-
if self.basis_function.__class__.__name__ == "Polynomial":
221+
if isinstance(self.basis_function, Polynomial):
217222
if steps_ahead is None:
218223
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
219224
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)
@@ -263,11 +268,7 @@ def _one_step_ahead_prediction(self, X, y):
263268
"""
264269
lagged_data = self.build_matrix(X, y)
265270
X_base = self.basis_function.transform(
266-
lagged_data,
267-
self.max_lag,
268-
self.ylag,
269-
self.xlag,
270-
self.model_type
271+
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type
271272
)
272273

273274
yhat = self.base_estimator.predict(X_base)
@@ -485,11 +486,7 @@ def _basis_function_predict(self, X, y_initial, forecast_horizon=None):
485486
yhat[i : i + analyzed_elements_number].reshape(-1, 1),
486487
)
487488
X_tmp = self.basis_function.transform(
488-
lagged_data,
489-
self.max_lag,
490-
self.ylag,
491-
self.xlag,
492-
self.model_type
489+
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type
493490
)
494491

495492
a = self.base_estimator.predict(X_tmp)

sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,12 @@ def fit(self, *, X: Optional[np.ndarray] = None, y: Optional[np.ndarray] = None)
308308
self.max_lag = self._get_max_lag()
309309
lagged_data = self.build_matrix(X, y)
310310
reg_matrix = self.basis_function.fit(
311-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
311+
lagged_data,
312+
self.max_lag,
313+
self.ylag,
314+
self.xlag,
315+
self.model_type,
316+
predefined_regressors=None,
312317
)
313318

314319
if X is not None:
@@ -371,7 +376,7 @@ def predict(
371376
The predicted values of the model.
372377
373378
"""
374-
if self.basis_function.__class__.__name__ == "Polynomial":
379+
if isinstance(self.basis_function, Polynomial):
375380
if steps_ahead is None:
376381
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
377382
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)

sysidentpy/model_structure_selection/entropic_regression.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,12 @@ def fit(self, *, X=None, y=None):
584584
lagged_data = self.build_matrix(X, y)
585585

586586
reg_matrix = self.basis_function.fit(
587-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
587+
lagged_data,
588+
self.max_lag,
589+
self.ylag,
590+
self.xlag,
591+
self.model_type,
592+
predefined_regressors=None,
588593
)
589594

590595
if X is not None:
@@ -687,7 +692,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
687692
The predicted values of the model.
688693
689694
"""
690-
if self.basis_function.__class__.__name__ == "Polynomial":
695+
if isinstance(self.basis_function, Polynomial):
691696
if steps_ahead is None:
692697
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
693698
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)

sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,12 @@ def fit(self, *, X: Optional[np.ndarray] = None, y: np.ndarray):
602602
lagged_data = self.build_matrix(X, y)
603603

604604
reg_matrix = self.basis_function.fit(
605-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
605+
lagged_data,
606+
self.max_lag,
607+
self.ylag,
608+
self.xlag,
609+
self.model_type,
610+
predefined_regressors=None,
606611
)
607612

608613
if X is not None:
@@ -689,7 +694,7 @@ def predict(
689694
The predicted values of the model.
690695
691696
"""
692-
if self.basis_function.__class__.__name__ == "Polynomial":
697+
if isinstance(self.basis_function, Polynomial):
693698
if steps_ahead is None:
694699
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
695700
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)

sysidentpy/model_structure_selection/meta_model_structure_selection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,12 @@ def evaluate_objective_function(
385385
lagged_data = self.build_matrix(X_train, y_train)
386386

387387
psi = self.basis_function.fit(
388-
lagged_data, self.max_lag, self.xlag, self.ylag, self.model_type, predefined_regressors=self.pivv
388+
lagged_data,
389+
self.max_lag,
390+
self.xlag,
391+
self.ylag,
392+
self.model_type,
393+
predefined_regressors=self.pivv,
389394
)
390395

391396
pos_insignificant_terms, _, _ = self.perform_t_test(
@@ -607,7 +612,7 @@ def predict(
607612
The predicted values of the model.
608613
609614
"""
610-
if self.basis_function.__class__.__name__ == "Polynomial":
615+
if isinstance(self.basis_function, Polynomial):
611616
if steps_ahead is None:
612617
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
613618
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)

sysidentpy/neural_network/narx_nn.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,24 @@ def split_data(self, X, y):
271271
self.max_lag = self._get_max_lag()
272272
lagged_data = self.build_matrix(X, y)
273273

274-
basis_name = self.basis_function.__class__.__name__
275-
if basis_name == "Polynomial":
274+
if isinstance(self.basis_function, Polynomial):
276275
reg_matrix = self.basis_function.fit(
277-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
276+
lagged_data,
277+
self.max_lag,
278+
self.ylag,
279+
self.xlag,
280+
self.model_type,
281+
predefined_regressors=None,
278282
)
279283
reg_matrix = reg_matrix[:, 1:]
280284
else:
281285
reg_matrix = self.basis_function.fit(
282-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=None
286+
lagged_data,
287+
self.max_lag,
288+
self.ylag,
289+
self.xlag,
290+
self.model_type,
291+
predefined_regressors=None,
283292
)
284293

285294
if X is not None:
@@ -476,7 +485,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
476485
The predicted values of the model.
477486
478487
"""
479-
if self.basis_function.__class__.__name__ == "Polynomial":
488+
if isinstance(self.basis_function, Polynomial):
480489
if steps_ahead is None:
481490
return self._model_prediction(X, y, forecast_horizon=forecast_horizon)
482491
if steps_ahead == 1:
@@ -513,23 +522,14 @@ def _one_step_ahead_prediction(self, X, y):
513522
"""
514523
lagged_data = self.build_matrix(X, y)
515524

516-
basis_name = self.basis_function.__class__.__name__
517-
if basis_name == "Polynomial":
525+
if isinstance(self.basis_function, Polynomial):
518526
X_base = self.basis_function.transform(
519-
lagged_data,
520-
self.max_lag,
521-
self.ylag,
522-
self.xlag,
523-
self.model_type
527+
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type
524528
)
525529
X_base = X_base[:, 1:]
526530
else:
527531
X_base = self.basis_function.transform(
528-
lagged_data,
529-
self.max_lag,
530-
self.ylag,
531-
self.xlag,
532-
self.model_type
532+
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type
533533
)
534534

535535
yhat = np.zeros(X.shape[0], dtype=float)
@@ -716,11 +716,7 @@ def _basis_function_predict(self, X, y_initial, forecast_horizon=None):
716716
)
717717

718718
X_tmp = self.basis_function.transform(
719-
lagged_data,
720-
self.max_lag,
721-
self.ylag,
722-
self.xlag,
723-
self.model_type
719+
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type
724720
)
725721
X_tmp = np.atleast_1d(X_tmp).astype(np.float32)
726722
yhat = yhat.astype(np.float32)

sysidentpy/simulation/_simulation.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _validate_simulate_params(self):
179179
)
180180

181181
def _check_simulate_params(self, y_train, y_test, model_code, steps_ahead, theta):
182-
if self.basis_function.__class__.__name__ != "Polynomial":
182+
if not isinstance(self.basis_function, Polynomial):
183183
raise NotImplementedError(
184184
"Currently, SimulateNARMAX only works for polynomial models."
185185
)
@@ -277,7 +277,12 @@ def simulate(
277277
self.max_lag = self._get_max_lag()
278278
lagged_data = self.build_matrix(X_train, y_train)
279279
psi = self.basis_function.fit(
280-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=self.pivv
280+
lagged_data,
281+
self.max_lag,
282+
self.ylag,
283+
self.xlag,
284+
self.model_type,
285+
predefined_regressors=self.pivv,
281286
)
282287

283288
self.theta = self.estimator.optimize(
@@ -303,7 +308,12 @@ def simulate(
303308
self.max_lag = self._get_max_lag()
304309
lagged_data = self.build_matrix(X_train, y_train)
305310
psi = self.basis_function.fit(
306-
lagged_data, self.max_lag, self.ylag, self.xlag, self.model_type, predefined_regressors=self.pivv
311+
lagged_data,
312+
self.max_lag,
313+
self.ylag,
314+
self.xlag,
315+
self.model_type,
316+
predefined_regressors=self.pivv,
307317
)
308318

309319
_, self.err, _, _ = self.error_reduction_ratio(
@@ -434,7 +444,7 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
434444
The predicted values of the model.
435445
436446
"""
437-
if self.basis_function.__class__.__name__ == "Polynomial":
447+
if isinstance(self.basis_function, Polynomial):
438448
if steps_ahead is None:
439449
yhat = self._model_prediction(X, y, forecast_horizon=forecast_horizon)
440450
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)

sysidentpy/utils/narmax_tools.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55

66
from ..narmax_base import RegressorDictionary
7+
from ..basis_function import Polynomial
78
from ._check_arrays import _num_features
89

910

@@ -47,24 +48,28 @@ def regressor_code(
4748
xlag=xlag, ylag=ylag, model_type=model_type, basis_function=basis_function
4849
).regressor_space(n_inputs)
4950

50-
basis_name = basis_function.__class__.__name__
51-
if basis_name != "Polynomial" and basis_function.ensemble:
51+
if not isinstance(basis_function, Polynomial) and basis_function.ensemble:
5252
repetition = basis_function.n * 2
5353
basis_code = np.sort(
5454
np.tile(encoding[1:, :], (repetition, 1)),
5555
axis=0,
5656
)
5757
encoding = np.concatenate([encoding[1:], basis_code])
58-
elif basis_name != "Polynomial" and basis_function.ensemble is False:
58+
elif (
59+
not isinstance(basis_function, Polynomial) and basis_function.ensemble is False
60+
):
5961
repetition = basis_function.n * 2
6062
encoding = np.sort(
6163
np.tile(encoding[1:, :], (repetition, 1)),
6264
axis=0,
6365
)
6466

65-
if basis_name == "Polynomial" and model_representation == "neural_network":
67+
if (
68+
isinstance(basis_function, Polynomial)
69+
and model_representation == "neural_network"
70+
):
6671
return encoding[1:]
67-
if basis_name == "Polynomial" and model_representation is None:
72+
if isinstance(basis_function, Polynomial) and model_representation is None:
6873
return encoding
6974

7075
return encoding

0 commit comments

Comments
 (0)