Skip to content
Snippets Groups Projects
Commit a817ea2c authored by Frederik Hennig's avatar Frederik Hennig Committed by Markus Holzer
Browse files

Fixes to shift_slice

- `shift_slice` did not work for single slices as it always tried to
iterate its argument
- `shift_slice` returned lists of slices, but for accessing numpy
arrays, tuples of slices are required
parent 43bdbcb8
Branches
Tags
No related merge requests found
......@@ -89,9 +89,12 @@ def shift_slice(slices, offset):
raise ValueError()
if hasattr(offset, '__len__'):
return [shift_slice_component(k, off) for k, off in zip(slices, offset)]
return tuple(shift_slice_component(k, off) for k, off in zip(slices, offset))
else:
return [shift_slice_component(k, offset) for k in slices]
if isinstance(slices, slice) or isinstance(slices, int) or isinstance(slices, float):
return shift_slice_component(slices, offset)
else:
return tuple(shift_slice_component(k, offset) for k in slices)
def slice_from_direction(direction_name, dim, normal_offset=0, tangential_offset=0):
......
import numpy as np
from numpy.testing import assert_array_equal
from pystencils import create_data_handling
from pystencils.slicing import SlicedGetter, make_slice, SlicedGetterDataHandling, shift_slice, slice_intersection
......@@ -59,6 +60,28 @@ def test_shift_slice():
assert sh[1] == 1.5
def test_shifted_array_access():
arr = np.array(range(10))
sh = make_slice[2:5]
assert_array_equal(arr[sh], [2,3,4])
sh = shift_slice(sh, 3)
assert_array_equal(arr[sh], [5,6,7])
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
sh = make_slice[0:2, 0:2]
assert_array_equal(arr[sh], [[1, 2], [4, 5]])
sh = shift_slice(sh, (1,1))
assert_array_equal(arr[sh], [[5, 6], [8, 9]])
def test_slice_intersection():
sl1 = make_slice[1:10, 1:10]
sl2 = make_slice[5:15, 5:15]
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment