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)
| 649 | |
| 650 | |
| 651 | def 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 |
no test coverage detected
searching dependent graphs…