Skip to content

Commit 4c6058f

Browse files
committed
Add deepcopy for lists to prevent hidden mutability of matrices
A bug that caused matrices saved in lists, i.e. using the multi-block method, to be altered even though the copy parameter was set to true is fixed. Now those lists are deepcopied.
1 parent 4317d29 commit 4c6058f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mbpls/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222

2323
__all__ = ["mbpls", "data"]
2424

25-
__version__ = "1.0.3"
25+
__version__ = "1.0.4"

mbpls/mbpls.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# License: 3-clause BSD
88

99

10+
from copy import deepcopy
1011
from sklearn.base import BaseEstimator, RegressorMixin, TransformerMixin, MultiOutputMixin
1112
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted, check_consistent_length
1213
from sklearn import metrics
@@ -298,6 +299,8 @@ def fit(self, X, Y):
298299
if self.standardize:
299300
self.x_scalers_ = []
300301
if isinstance(X, list) and not isinstance(X[0], list):
302+
if self.copy:
303+
X = deepcopy(X)
301304
if self.sparse_data is True:
302305
self.sparse_X_info_ = {}
303306
for block in range(len(X)):
@@ -323,6 +326,8 @@ def fit(self, X, Y):
323326
Y = self.y_scaler_.fit_transform(Y)
324327
else:
325328
if isinstance(X, list) and not isinstance(X[0], list):
329+
if self.copy:
330+
X = deepcopy(X)
326331
if self.sparse_data is True:
327332
self.sparse_X_info_ = {}
328333
for block in range(len(X)):
@@ -1082,6 +1087,8 @@ def transform(self, X, Y=None, return_block_scores=False, copy=True):
10821087

10831088
if self.standardize:
10841089
if isinstance(X, list) and not isinstance(X[0], list):
1090+
if copy:
1091+
X = deepcopy(X)
10851092
for block in range(len(X)):
10861093
# Check dimensions
10871094
X[block] = check_array(X[block], dtype=np.float64, force_all_finite=not self.sparse_data, copy=copy)
@@ -1214,6 +1221,8 @@ def transform(self, X, Y=None, return_block_scores=False, copy=True):
12141221

12151222
else:
12161223
if isinstance(X, list) and not isinstance(X[0], list):
1224+
if copy:
1225+
X = deepcopy(X)
12171226
for block in range(len(X)):
12181227
# Check dimensions
12191228
X[block] = check_array(X[block], dtype=np.float64, force_all_finite=not self.sparse_data, copy=copy)
@@ -1352,6 +1361,8 @@ def predict(self, X, copy=True):
13521361

13531362
if self.standardize:
13541363
if isinstance(X, list) and not isinstance(X[0], list):
1364+
if copy:
1365+
X = deepcopy(X)
13551366
for block in range(len(X)):
13561367
# Check dimensions
13571368
X[block] = check_array(X[block], dtype=np.float64, force_all_finite=not self.sparse_data, copy=copy)
@@ -1375,6 +1386,8 @@ def predict(self, X, copy=True):
13751386
y_hat = self.y_scaler_.inverse_transform(X.dot(self.beta_))
13761387
else:
13771388
if isinstance(X, list) and not isinstance(X[0], list):
1389+
if copy:
1390+
X = deepcopy(X)
13781391
for block in range(len(X)):
13791392
# Check dimensions
13801393
X[block] = check_array(X[block], dtype=np.float64, force_all_finite=not self.sparse_data, copy=copy)

0 commit comments

Comments
 (0)