Normalize slices to canonical form Parameters ---------- idx: slice or other index dim: dimension length Examples -------- >>> normalize_slice(slice(0, 10, 1), 10) slice(None, None, None)
(idx, dim)
| 762 | |
| 763 | |
| 764 | def normalize_slice(idx, dim): |
| 765 | """Normalize slices to canonical form |
| 766 | |
| 767 | Parameters |
| 768 | ---------- |
| 769 | idx: slice or other index |
| 770 | dim: dimension length |
| 771 | |
| 772 | Examples |
| 773 | -------- |
| 774 | >>> normalize_slice(slice(0, 10, 1), 10) |
| 775 | slice(None, None, None) |
| 776 | """ |
| 777 | |
| 778 | if isinstance(idx, slice): |
| 779 | if math.isnan(dim): |
| 780 | return idx |
| 781 | start, stop, step = idx.indices(dim) |
| 782 | if step > 0: |
| 783 | if start == 0: |
| 784 | start = None |
| 785 | if stop >= dim: |
| 786 | stop = None |
| 787 | if step == 1: |
| 788 | step = None |
| 789 | if stop is not None and start is not None and stop < start: |
| 790 | stop = start |
| 791 | elif step < 0: |
| 792 | if start >= dim - 1: |
| 793 | start = None |
| 794 | if stop < 0: |
| 795 | stop = None |
| 796 | return slice(start, stop, step) |
| 797 | return idx |
| 798 | |
| 799 | |
| 800 | def normalize_index(idx, shape): |
nothing calls this directly
no outgoing calls
no test coverage detected