Skip to content

always serialize the function using cloudpickle #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion adaptive/learner/average_learner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from math import sqrt

import cloudpickle
import numpy as np

from adaptive.learner.base_learner import BaseLearner
Expand Down Expand Up @@ -151,7 +152,7 @@ def _set_data(self, data):

def __getstate__(self):
return (
self.function,
cloudpickle.dumps(self.function),
self.atol,
self.rtol,
self.min_npoints,
Expand All @@ -160,5 +161,6 @@ def __getstate__(self):

def __setstate__(self, state):
function, atol, rtol, min_npoints, data = state
function = cloudpickle.loads(function)
self.__init__(function, atol, rtol, min_npoints)
self._set_data(data)
4 changes: 3 additions & 1 deletion adaptive/learner/integrator_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from math import sqrt
from operator import attrgetter

import cloudpickle
import numpy as np
from scipy.linalg import norm
from sortedcontainers import SortedSet
Expand Down Expand Up @@ -594,13 +595,14 @@ def _set_data(self, data):

def __getstate__(self):
return (
self.function,
cloudpickle.dumps(self.function),
self.bounds,
self.tol,
self._get_data(),
)

def __setstate__(self, state):
function, bounds, tol, data = state
function = cloudpickle.loads(function)
self.__init__(function, bounds, tol)
self._set_data(data)
4 changes: 3 additions & 1 deletion adaptive/learner/learner1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Iterable
from copy import deepcopy

import cloudpickle
import numpy as np
import sortedcollections
import sortedcontainers
Expand Down Expand Up @@ -634,7 +635,7 @@ def _set_data(self, data):

def __getstate__(self):
return (
self.function,
cloudpickle.dumps(self.function),
self.bounds,
self.loss_per_interval,
dict(self.losses), # SortedDict cannot be pickled
Expand All @@ -644,6 +645,7 @@ def __getstate__(self):

def __setstate__(self, state):
function, bounds, loss_per_interval, losses, losses_combined, data = state
function = cloudpickle.loads(function)
self.__init__(function, bounds, loss_per_interval)
self._set_data(data)
self.losses.update(losses)
Expand Down
4 changes: 3 additions & 1 deletion adaptive/learner/learner2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from copy import copy
from math import sqrt

import cloudpickle
import numpy as np
from scipy import interpolate

Expand Down Expand Up @@ -709,7 +710,7 @@ def _set_data(self, data):

def __getstate__(self):
return (
self.function,
cloudpickle.dumps(self.function),
self.bounds,
self.loss_per_triangle,
self._stack,
Expand All @@ -718,6 +719,7 @@ def __getstate__(self):

def __setstate__(self, state):
function, bounds, loss_per_triangle, _stack, data = state
function = cloudpickle.loads(function)
self.__init__(function, bounds, loss_per_triangle)
self._set_data(data)
self._stack = _stack
4 changes: 3 additions & 1 deletion adaptive/learner/sequence_learner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import copy

import cloudpickle
from sortedcontainers import SortedDict, SortedSet

from adaptive.learner.base_learner import BaseLearner
Expand Down Expand Up @@ -131,12 +132,13 @@ def _set_data(self, data):

def __getstate__(self):
return (
self._original_function,
cloudpickle.dumps(self._original_function),
self.sequence,
self._get_data(),
)

def __setstate__(self, state):
function, sequence, data = state
function = cloudpickle.loads(function)
self.__init__(function, sequence)
self._set_data(data)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_version_and_cmdclass(package_name):
"sortedcollections >= 1.1",
"sortedcontainers >= 2.0",
"atomicwrites",
"cloudpickle",
]

extras_require = {
Expand All @@ -51,7 +52,6 @@ def get_version_and_cmdclass(package_name):
"pre_commit",
],
"other": [
"cloudpickle",
"dill",
"distributed",
"ipyparallel>=6.2.5", # because of https://github.com/ipython/ipyparallel/issues/404
Expand Down