Skip to content

Commit dd14fe1

Browse files
committed
[BaseTag] adds new method that only does a position scaling ...
and returns the new position as well as the scaling factor, this is done to separate scaling and getting the index for ranges/slices where we need more control
1 parent 1f0a70c commit dd14fe1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

nixio/tag.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ def _slices_in_data(data, slices):
150150
stops = tuple(sl.stop for sl in slices)
151151
return np.all(np.less_equal(stops, dasize))
152152

153+
@staticmethod
154+
def _scale_position(pos, unit, dim):
155+
dimtype = dim.dimension_type
156+
if dimtype == DimensionType.Set:
157+
dimunit = None
158+
else:
159+
dimunit = dim.unit
160+
scaling = 1.0
161+
if dimtype == DimensionType.Set:
162+
if unit and unit != "none":
163+
raise IncompatibleDimensions(
164+
"Cannot apply a position with unit to a SetDimension",
165+
"Tag._pos_to_idx"
166+
)
167+
else:
168+
if dimunit is None and unit is not None:
169+
raise IncompatibleDimensions(
170+
"Units of position and SampledDimension "
171+
"must both be given!",
172+
"Tag._pos_to_idx"
173+
)
174+
elif dimunit is not None and unit is not None:
175+
try:
176+
scaling = util.units.scaling(unit, dimunit)
177+
except InvalidUnit:
178+
raise IncompatibleDimensions(
179+
"Cannot scale Tag unit {} to match dimension unit {}".format(unit, dimunit),
180+
"Tag._pos_to_idx"
181+
)
182+
return pos * scaling, scaling
183+
153184
@staticmethod
154185
def _pos_to_idx(pos, unit, dim, mode):
155186
dimtype = dim.dimension_type

0 commit comments

Comments
 (0)