MCPcopy
hub / github.com/dask/dask / posify_index

Function posify_index

dask/array/slicing.py:651–676  ·  view source on GitHub ↗

Flip negative indices around to positive ones >>> posify_index(10, 3) 3 >>> posify_index(10, -3) 7 >>> posify_index(10, [3, -3]) array([3, 7]) >>> posify_index((10, 20), (3, -3)) (3, 17) >>> posify_index((10, 20), (3, [3, 4, -3])) # doctest: +NORMALIZE_WHITESPA

(shape, ind)

Source from the content-addressed store, hash-verified

649
650
651def posify_index(shape, ind):
652 """Flip negative indices around to positive ones
653
654 >>> posify_index(10, 3)
655 3
656 >>> posify_index(10, -3)
657 7
658 >>> posify_index(10, [3, -3])
659 array([3, 7])
660
661 >>> posify_index((10, 20), (3, -3))
662 (3, 17)
663 >>> posify_index((10, 20), (3, [3, 4, -3])) # doctest: +NORMALIZE_WHITESPACE
664 (3, array([ 3, 4, 17]))
665 """
666 if isinstance(ind, tuple):
667 return tuple(map(posify_index, shape, ind))
668 if isinstance(ind, Integral):
669 if ind < 0 and not math.isnan(shape):
670 return ind + shape
671 else:
672 return ind
673 if isinstance(ind, (np.ndarray, list)) and not math.isnan(shape):
674 ind = np.asanyarray(ind)
675 return np.where(ind < 0, ind + shape, ind)
676 return ind
677
678
679@memoize

Callers 2

normalize_indexFunction · 0.90
normalize_indexFunction · 0.85

Calls 1

whereMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…