Skip to content

Commit f21bdea

Browse files
chkoarglemaitre
andauthored
FIX make pipeline memory compatible with older joblib version (#687)
Co-authored-by: Guillaume Lemaitre <[email protected]>
1 parent 703cee1 commit f21bdea

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

doc/whats_new/v0.7.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
.. _changes_0_7:
22

3-
Version 0.7.0 (under development)
4-
=================================
3+
Version 0.7.0
4+
=============
55

66
Changelog
77
---------
88

9+
Maintenance
10+
...........
11+
12+
- Ensure that :class:`imblearn.pipeline.Pipeline` is working when `memory`
13+
is activated and `joblib==0.11`.
14+
:pr:`687` by :user:`Christos Aridas <chkoar>`.
15+
916
Changed models
1017
..............
1118

imblearn/pipeline.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,15 @@ def _fit(self, X, y=None, **fit_params):
213213
with _print_elapsed_time('Pipeline',
214214
self._log_message(step_idx)):
215215
continue
216-
if hasattr(memory, "location"):
216+
217+
try:
217218
# joblib >= 0.12
218-
if memory.location is None:
219-
# we do not clone when caching is disabled to
220-
# preserve backward compatibility
221-
cloned_transformer = transformer
222-
else:
223-
cloned_transformer = clone(transformer)
224-
elif hasattr(memory, "cachedir"):
225-
# joblib <= 0.11
226-
if memory.cachedir is None:
227-
# we do not clone when caching is disabled to
228-
# preserve backward compatibility
229-
cloned_transformer = transformer
230-
else:
231-
cloned_transformer = clone(transformer)
219+
mem = memory.location
220+
except AttributeError:
221+
mem = memory.cachedir
222+
finally:
223+
cloned_transformer = clone(transformer) if mem else transformer
224+
232225
# Fit or load from cache the current transfomer
233226
if hasattr(cloned_transformer, "transform") or hasattr(
234227
cloned_transformer, "fit_transform"

imblearn/tests/test_pipeline.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,22 @@ def test_pipeline_score_samples_pca_lof():
13421342
# Check the values
13431343
lof.fit(pca.fit_transform(X))
13441344
assert_allclose(pipe.score_samples(X), lof.score_samples(pca.transform(X)))
1345+
1346+
1347+
def test_pipeline_old_joblib_memory(monkeypatch):
1348+
"""Test that Pipeline works with old versions of joblib"""
1349+
1350+
monkeypatch.setattr(Memory, "cachedir", "foo", raising=False)
1351+
monkeypatch.setattr(Memory, "cache", lambda self, x: x, raising=False)
1352+
memory = Memory()
1353+
1354+
del memory.location # Older versions do not have the location parameter
1355+
1356+
iris = load_iris()
1357+
X = iris.data
1358+
y = iris.target
1359+
1360+
cached_pipe = Pipeline(
1361+
[("transf", DummyTransf()), ("svc", SVC(gamma="scale"))], memory=memory
1362+
)
1363+
cached_pipe.fit(X, y)

0 commit comments

Comments
 (0)