Check validity of index for a given dimension Examples -------- >>> check_index(0, 3, 5) >>> check_index(0, 5, 5) Traceback (most recent call last): ... IndexError: Index 5 is out of bounds for axis 0 with size 5 >>> check_index(1, 6, 5) Traceback (most recent c
(axis, ind, dimension)
| 867 | |
| 868 | |
| 869 | def check_index(axis, ind, dimension): |
| 870 | """Check validity of index for a given dimension |
| 871 | |
| 872 | Examples |
| 873 | -------- |
| 874 | >>> check_index(0, 3, 5) |
| 875 | >>> check_index(0, 5, 5) |
| 876 | Traceback (most recent call last): |
| 877 | ... |
| 878 | IndexError: Index 5 is out of bounds for axis 0 with size 5 |
| 879 | |
| 880 | >>> check_index(1, 6, 5) |
| 881 | Traceback (most recent call last): |
| 882 | ... |
| 883 | IndexError: Index 6 is out of bounds for axis 1 with size 5 |
| 884 | |
| 885 | >>> check_index(1, -1, 5) |
| 886 | >>> check_index(1, -6, 5) |
| 887 | Traceback (most recent call last): |
| 888 | ... |
| 889 | IndexError: Index -6 is out of bounds for axis 1 with size 5 |
| 890 | |
| 891 | >>> check_index(0, [1, 2], 5) |
| 892 | >>> check_index(0, [6, 3], 5) |
| 893 | Traceback (most recent call last): |
| 894 | ... |
| 895 | IndexError: Index is out of bounds for axis 0 with size 5 |
| 896 | |
| 897 | >>> check_index(1, slice(0, 3), 5) |
| 898 | |
| 899 | >>> check_index(0, [True], 1) |
| 900 | >>> check_index(0, [True, True], 3) |
| 901 | Traceback (most recent call last): |
| 902 | ... |
| 903 | IndexError: Boolean array with size 2 is not long enough for axis 0 with size 3 |
| 904 | >>> check_index(0, [True, True, True], 1) |
| 905 | Traceback (most recent call last): |
| 906 | ... |
| 907 | IndexError: Boolean array with size 3 is not long enough for axis 0 with size 1 |
| 908 | """ |
| 909 | if isinstance(ind, list): |
| 910 | ind = np.asanyarray(ind) |
| 911 | |
| 912 | # unknown dimension, assumed to be in bounds |
| 913 | if np.isnan(dimension): |
| 914 | return |
| 915 | elif is_dask_collection(ind): |
| 916 | return |
| 917 | elif is_arraylike(ind): |
| 918 | if ind.dtype == bool: |
| 919 | if ind.size != dimension: |
| 920 | raise IndexError( |
| 921 | f"Boolean array with size {ind.size} is not long enough " |
| 922 | f"for axis {axis} with size {dimension}" |
| 923 | ) |
| 924 | elif (ind >= dimension).any() or (ind < -dimension).any(): |
| 925 | raise IndexError( |
| 926 | f"Index is out of bounds for axis {axis} with size {dimension}" |
no test coverage detected