Skip to content

Commit 430a6ee

Browse files
committed
[sampledDim.index_of] use round instead of floor
1 parent 526f530 commit 430a6ee

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

nixio/dimensions.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,14 @@ def index_of(self, position, mode=IndexMode.LessOrEqual):
386386
if scaled_position < 0:
387387
if mode == IndexMode.GreaterOrEqual:
388388
return 0
389-
# position is OOB (left side) but can't round up
389+
# position is OOB (left side) but can't round up because LessOrEqual or Less
390390
raise IndexError("Position {} is out of bounds for SampledDimension with offset {} and mode {}".format(
391391
position, offset, mode.name
392392
))
393393

394394
if np.isclose(position, 0) and mode == IndexMode.Less:
395395
raise IndexError("Position {} is out of bounds for SampledDimension with mode {}".format(position, mode.name))
396-
397-
index = int(np.floor(scaled_position))
396+
index = int(np.round(scaled_position))
398397
if np.isclose(scaled_position, index):
399398
# exact position
400399
if mode in (IndexMode.GreaterOrEqual, IndexMode.LessOrEqual):
@@ -404,13 +403,20 @@ def index_of(self, position, mode=IndexMode.LessOrEqual):
404403
# exact position and Less mode
405404
return index - 1
406405
raise ValueError("Unknown IndexMode: {}".format(mode))
407-
408-
if mode == IndexMode.GreaterOrEqual: # and inexact position
409-
return index + 1
410-
if mode in (IndexMode.LessOrEqual, IndexMode.Less): # and inexact position
411-
return index
412-
413-
raise ValueError("Unknown IndexMode: {}".format(mode))
406+
if index < scaled_position:
407+
if mode in (IndexMode.LessOrEqual, IndexMode.Less):
408+
return index
409+
elif mode == IndexMode.GreaterOrEqual:
410+
return index + 1
411+
else:
412+
raise ValueError("Unknown IndexMode: {}".format(mode))
413+
else:
414+
if mode in (IndexMode.LessOrEqual, IndexMode.Less):
415+
return index - 1
416+
elif mode == IndexMode.GreaterOrEqual:
417+
return index
418+
else:
419+
raise ValueError("Unknown IndexMode: {}".format(mode))
414420

415421
def range_indices(self, start_position, end_position, mode=SliceMode.Exclusive):
416422
"""

0 commit comments

Comments
 (0)