Skip to content

Commit 446f031

Browse files
committed
[BaseTag] delegate figuring out the range indices to the methods on
the respective dimension
1 parent dd14fe1 commit 446f031

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

nixio/tag.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,31 @@ def _calc_data_slices(self, data, position, extent, stop_rule):
122122
units = self.units
123123

124124
for idx, dim in enumerate(data.dimensions):
125+
scaling = 1.0
125126
if idx < len(position):
126-
pos = position[idx]
127-
start = self._pos_to_idx(pos, units[idx], dim, IndexMode.GreaterOrEqual)
128-
else:
129-
# Tag doesn't specify (pos, ext) for all dimensions: will return entire remaining dimensions (0:len)
130-
start = 0
131-
if extent is None or len(extent) == 0:
132-
# no extents: return one element
133-
stop = start + 1
134-
elif idx < len(extent):
135-
ext = extent[idx]
136-
stop = self._pos_to_idx(pos + ext, units[idx], dim, stop_rule.to_index_mode()) + 1
137-
else:
138-
# Tag doesn't specify (pos, ext) for all dimensions: will return entire remaining dimensions (0:len)
139-
stop = data.shape[idx]
140-
141-
if stop <= start:
142-
# always return at least one element per dimension
143-
stop = start + 1
144-
refslice.append(slice(start, stop))
127+
start_pos = position[idx]
128+
start_pos, scaling = self._scale_position(start_pos, units[idx], dim)
129+
if extent is not None and idx < len(extent):
130+
stop_pos = extent[idx]
131+
stop_pos *= scaling
132+
stop_pos += start_pos
133+
slice_mode = stop_rule if extent[idx] > 0.0 else SliceMode.Inclusive
134+
else:
135+
stop_pos = start_pos
136+
slice_mode = SliceMode.Inclusive
137+
range_indices = dim.range_indices(start_pos, stop_pos, slice_mode)
138+
refslice.append(range_indices if range_indices is None else slice(range_indices[0], range_indices[1] + 1))
139+
else: # no position, we take the whole slice for this dimension
140+
start_index = 0
141+
stop_index = data.shape[idx]
142+
refslice.append(slice(start_index, stop_index))
143+
145144
return tuple(refslice)
146145

147146
@staticmethod
148147
def _slices_in_data(data, slices):
148+
if slices is None or not all(slices):
149+
return False
149150
dasize = data.data_extent
150151
stops = tuple(sl.stop for sl in slices)
151152
return np.all(np.less_equal(stops, dasize))

0 commit comments

Comments
 (0)