Skip to content

Commit 7b7fd01

Browse files
authored
Merge pull request scipy#21320 from TiborVoelcker/main
BUG: odr: fix pickling
2 parents 58f465c + d684776 commit 7b7fd01

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

scipy/odr/_odrpack.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def set_meta(self, **kwds):
286286
def __getattr__(self, attr):
287287
""" Dispatch attribute access to the metadata dictionary.
288288
"""
289-
if attr in self.meta:
289+
if attr != "meta" and attr in self.meta:
290290
return self.meta[attr]
291291
else:
292292
raise AttributeError(f"'{attr}' not in metadata")
@@ -408,17 +408,18 @@ def _cov2wt(self, cov):
408408
return weights
409409

410410
def __getattr__(self, attr):
411-
lookup_tbl = {('wd', 'sx'): (self._sd2wt, self.sx),
412-
('wd', 'covx'): (self._cov2wt, self.covx),
413-
('we', 'sy'): (self._sd2wt, self.sy),
414-
('we', 'covy'): (self._cov2wt, self.covy)}
415-
411+
416412
if attr not in ('wd', 'we'):
417-
if attr in self.meta:
413+
if attr != "meta" and attr in self.meta:
418414
return self.meta[attr]
419415
else:
420416
raise AttributeError(f"'{attr}' not in metadata")
421417
else:
418+
lookup_tbl = {('wd', 'sx'): (self._sd2wt, self.sx),
419+
('wd', 'covx'): (self._cov2wt, self.covx),
420+
('we', 'sy'): (self._sd2wt, self.sy),
421+
('we', 'covy'): (self._cov2wt, self.covy)}
422+
422423
func, arg = lookup_tbl[(attr, self._ga_flags[attr])]
423424

424425
if arg is not None:
@@ -533,7 +534,7 @@ def __getattr__(self, attr):
533534
""" Dispatch attribute access to the metadata.
534535
"""
535536

536-
if attr in self.meta:
537+
if attr != "meta" and attr in self.meta:
537538
return self.meta[attr]
538539
else:
539540
raise AttributeError(f"'{attr}' not in metadata")

scipy/odr/tests/test_odr.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pickle
12
import tempfile
23
import shutil
34
import os
@@ -563,3 +564,43 @@ def func(b, x):
563564
odr_obj.set_job(fit_type=0, del_init=1)
564565
# Just make sure that it runs without raising an exception.
565566
odr_obj.run()
567+
568+
def test_pickling_data(self):
569+
x = np.linspace(0.0, 5.0)
570+
y = 1.0 * x + 2.0
571+
data = Data(x, y)
572+
573+
obj_pickle = pickle.dumps(data)
574+
del data
575+
pickle.loads(obj_pickle)
576+
577+
def test_pickling_real_data(self):
578+
x = np.linspace(0.0, 5.0)
579+
y = 1.0 * x + 2.0
580+
data = RealData(x, y)
581+
582+
obj_pickle = pickle.dumps(data)
583+
del data
584+
pickle.loads(obj_pickle)
585+
586+
def test_pickling_model(self):
587+
obj_pickle = pickle.dumps(unilinear)
588+
pickle.loads(obj_pickle)
589+
590+
def test_pickling_odr(self):
591+
x = np.linspace(0.0, 5.0)
592+
y = 1.0 * x + 2.0
593+
odr_obj = ODR(Data(x, y), unilinear)
594+
595+
obj_pickle = pickle.dumps(odr_obj)
596+
del odr_obj
597+
pickle.loads(obj_pickle)
598+
599+
def test_pickling_output(self):
600+
x = np.linspace(0.0, 5.0)
601+
y = 1.0 * x + 2.0
602+
output = ODR(Data(x, y), unilinear).run
603+
604+
obj_pickle = pickle.dumps(output)
605+
del output
606+
pickle.loads(obj_pickle)

0 commit comments

Comments
 (0)