Skip to content
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

fix picklability of sliced function array #834

Merged
merged 1 commit into from
Nov 29, 2023
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
9 changes: 2 additions & 7 deletions nutils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,12 +1589,6 @@ def get(__array: IntoArray, __axis: int, __index: IntoArray) -> Array:
return numpy.take(Array.cast(__array), Array.cast(__index, dtype=int, ndim=0), __axis)


def _range(__length: int, __offset: int) -> Array:
length = Array.cast(__length, dtype=int, ndim=0)
offset = Array.cast(__offset, dtype=int, ndim=0)
return _Wrapper(lambda l, o: evaluable.Range(l) + o, _WithoutPoints(length), _WithoutPoints(offset), shape=(__length,), dtype=int)


def _takeslice(__array: IntoArray, __s: slice, __axis: int) -> Array:
array = Array.cast(__array)
s = __s
Expand All @@ -1605,7 +1599,8 @@ def _takeslice(__array: IntoArray, __s: slice, __axis: int) -> Array:
stop = n if s.stop is None else s.stop if s.stop >= 0 else s.stop + n
if start == 0 and stop == n:
return array
index = _range(stop-start, start)
length = stop - start
index = _Wrapper(evaluable.Range, _WithoutPoints(_Constant(length)), shape=(length,), dtype=int) + start
elif isinstance(n, numbers.Integral):
index = Array.cast(numpy.arange(*s.indices(int(n))))
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings as _builtin_warnings
import functools
import fractions
import pickle


class Array(TestCase):
Expand Down Expand Up @@ -167,6 +168,12 @@ def test_lower_eval(self):
desired = self.n_op(*self.args)
self.assertArrayAlmostEqual(actual, desired, decimal=15)

def test_pickle(self):
f = self.op(*self.args)
s = pickle.dumps(f)
f_ = pickle.loads(s)
self.assertEqual(f.as_evaluable_array, f_.as_evaluable_array)


def generate(*shape, real, imag, zero, negative):
'generate array values that cover certain numerical classes'
Expand Down
Loading