@@ -386,15 +386,14 @@ def index_of(self, position, mode=IndexMode.LessOrEqual):
386
386
if scaled_position < 0 :
387
387
if mode == IndexMode .GreaterOrEqual :
388
388
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
390
390
raise IndexError ("Position {} is out of bounds for SampledDimension with offset {} and mode {}" .format (
391
391
position , offset , mode .name
392
392
))
393
393
394
394
if np .isclose (position , 0 ) and mode == IndexMode .Less :
395
395
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 ))
398
397
if np .isclose (scaled_position , index ):
399
398
# exact position
400
399
if mode in (IndexMode .GreaterOrEqual , IndexMode .LessOrEqual ):
@@ -404,13 +403,20 @@ def index_of(self, position, mode=IndexMode.LessOrEqual):
404
403
# exact position and Less mode
405
404
return index - 1
406
405
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 ))
414
420
415
421
def range_indices (self , start_position , end_position , mode = SliceMode .Exclusive ):
416
422
"""
0 commit comments