Commit 7c46f511 authored by David GEISSBUHLER's avatar David GEISSBUHLER
Browse files

renamed slice -> view to be more numpy friendly

parent 497d71c6
......@@ -112,7 +112,7 @@ class Stream:
return self.parent.image_points
# data access
# returns either a frame (if index is int) or a StreamSlice (if index is slice or tuple)
# returns either a frame (if index is int) or a StreamView (if index is slice or tuple)
# provides modest compatibility with python / numpy arrays
def __getitem__(self, index):
# case 1: index is int -> load and return the frame data
......@@ -121,7 +121,7 @@ class Stream:
return data[0]
# case 2: index is slice -> pack as tuple
elif isinstance(index, slice):
slice_indices = (index, )
view_indices = (index, )
# case 3: index is tuple -> validate
elif isinstance(index, tuple):
# we do not check dimensionality here,
......@@ -134,13 +134,12 @@ class Stream:
else:
raise Exception("index can only be int or slice")
# so far so good ...
slice_indices = index
view_indices = index
else:
raise Exception("index can only be int, slice or tuple")
# case 2 & 3: return stream_slice
print('__getitem__', slice_indices)
return self.stream_slice(slice_indices=slice_indices)
# case 2 & 3: return stream_view
print('__getitem__', view_indices)
return self.view(view_indices=view_indices)
def __setitem__(self, index, data):
raise Exception("not yet implemented")
......@@ -307,47 +306,47 @@ class Select(StreamFilter):
################################################################################
### stream_slice ###
@stream_filter("stream_slice")
class StreamSlice(StreamFilter):
def __init__(self, name, parent, slice_indices=None):
### view ###
@stream_filter("view")
class StreamView(StreamFilter):
def __init__(self, name, parent, view_indices=None):
super().__init__(name=name, parent=parent)
self.frame_slice = None
self.bulk_slice = None
if isinstance(slice_indices, tuple):
# separate frame and bulk slices
self.frame_slice = slice_indices[0]
self.frame_view = None
self.bulk_view= None
if isinstance(view_indices, tuple):
# separate frame and bulk views
self.frame_view = view_indices[0]
# TODO should case with int on frame index be allowed?
assert(self.frame_slice is None or isinstance(self.frame_slice, slice))
# bulk slices
if len(slice_indices) > 1:
self.bulk_slice = slice_indices[1:]
for e in self.bulk_slice:
assert(self.frame_view is None or isinstance(self.frame_view, slice))
# bulk views
if len(view_indices) > 1:
self.bulk_view = view_indices[1:]
for e in self.bulk_view:
if isinstance(e, int) or isinstance(e, slice) or e is None:
pass
else:
raise Exception('slice_indices should be a tuple of int / slice object(s) or None')
raise Exception('view_indices should be a tuple of int / slice object(s) or None')
# None is passthru
elif slice_indices is None:
elif view_indices is None:
pass
else:
raise Exception('slice_indices should be a tuple of int / slice object(s) or None')
raise Exception('view_indices should be a tuple of int / slice object(s) or None')
@property
def shape(self):
if self.bulk_slice is not None and self.parent.ndim <= len(self.bulk_slice):
raise Exception('slice dimension exceed parent dimension')
if self.bulk_view is not None and self.parent.ndim <= len(self.bulk_view):
raise Exception('view dimension exceed parent dimension')
# first dimension ...
__shape = [self.parent.get_axis_size(0, self.frame_slice)]
__shape = [self.parent.get_axis_size(0, self.frame_view)]
# ... and others
for d in range(1, self.parent.ndim):
if self.bulk_slice is not None and d-1 < len(self.bulk_slice):
__slice_index = self.bulk_slice[d-1]
# don't add axis to shape if integer slice
if isinstance(__slice_index, int):
if self.bulk_view is not None and d-1 < len(self.bulk_view):
__view_index = self.bulk_view[d-1]
# don't add axis to shape if integer index
if isinstance(__view_index, int):
pass
else:
__shape.append(self.parent.get_axis_size(d, __slice_index))
__shape.append(self.parent.get_axis_size(d, __view_index))
else:
__shape.append(self.parent.get_axis_size(d))
return tuple(__shape)
......@@ -355,39 +354,39 @@ class StreamSlice(StreamFilter):
@property
def ndim(self):
__ndim = self.parent.ndim
if self.frame_slice is not None and isinstance(self.frame_slice, int):
if self.frame_view is not None and isinstance(self.frame_view, int):
__ndim -= 1
if self.bulk_slice is not None:
for a in self.bulk_slice:
if self.bulk_view is not None:
for a in self.bulk_view:
if isinstance(a, int):
__ndim -= 1
return __ndim
# translate frame indices
def get_frame_indices(self, index):
if self.frame_slice is None:
if self.frame_view is None:
return super().get_frame_indices(index)
elif isinstance(self.frame_slice, slice):
elif isinstance(self.frame_view, slice):
# TODO perform translation....
return super().get_frame_indices(index)
def load(self, index=None):
indices = self.get_frame_indices(index)
print('slice load', indices)
print('view load', indices)
return super().load(indices)
def process(self, data, indices):
# generate full bulk slice
__bulk_slice_full = [slice(None, None, None) for d in range(self.parent.ndim - 1)]
if self.bulk_slice is not None:
# generate full bulk view
__bulk_view_full = [slice(None, None, None) for d in range(self.parent.ndim - 1)]
if self.bulk_view is not None:
for d in range(self.ndim - 1):
if d < len(self.bulk_slice):
if self.bulk_slice[d] is not None:
if d < len(self.bulk_view):
if self.bulk_view[d] is not None:
# should be int or slice
__bulk_slice_full[d] = self.bulk_slice[d]
self.__bulk_slice_full = tuple(__bulk_slice_full)
print('self.__bulk_slice_full', self.__bulk_slice_full)
__bulk_view_full[d] = self.bulk_view[d]
self.__bulk_view_full = tuple(__bulk_view_full)
print('self.__bulk_view_full', self.__bulk_view_full)
return super().process(data, indices)
def process_frame(self, data, data_index, stream_index):
return data[self.__bulk_slice_full]
\ No newline at end of file
return data[self.__bulk_view_full]
\ No newline at end of file
Supports Markdown
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