Find all masked and/or non-finite points in a set of arguments, and return the arguments as masked arrays with a common mask. Arguments can be in any of 5 categories: 1) 1-D masked arrays 2) 1-D ndarrays 3) ndarrays with more than one dimension 4) other non-string iter
(*args)
| 1048 | |
| 1049 | |
| 1050 | def _combine_masks(*args): |
| 1051 | """ |
| 1052 | Find all masked and/or non-finite points in a set of arguments, |
| 1053 | and return the arguments as masked arrays with a common mask. |
| 1054 | |
| 1055 | Arguments can be in any of 5 categories: |
| 1056 | |
| 1057 | 1) 1-D masked arrays |
| 1058 | 2) 1-D ndarrays |
| 1059 | 3) ndarrays with more than one dimension |
| 1060 | 4) other non-string iterables |
| 1061 | 5) anything else |
| 1062 | |
| 1063 | The first argument must be in one of the first four categories; |
| 1064 | any argument with a length differing from that of the first |
| 1065 | argument (and hence anything in category 5) then will be |
| 1066 | passed through unchanged. |
| 1067 | |
| 1068 | Masks are obtained from all arguments of the correct length |
| 1069 | in categories 1, 2, and 4; a point is bad if masked in a masked |
| 1070 | array or if it is a nan or inf. No attempt is made to |
| 1071 | extract a mask from categories 2 and 4 if `numpy.isfinite` |
| 1072 | does not yield a Boolean array. Category 3 is included to |
| 1073 | support RGB or RGBA ndarrays, which are assumed to have only |
| 1074 | valid values and which are passed through unchanged. |
| 1075 | |
| 1076 | All input arguments that are not passed unchanged are returned |
| 1077 | as masked arrays if any masked points are found, otherwise as |
| 1078 | ndarrays. |
| 1079 | |
| 1080 | """ |
| 1081 | if not len(args): |
| 1082 | return () |
| 1083 | if is_scalar_or_string(args[0]): |
| 1084 | raise ValueError("First argument must be a sequence") |
| 1085 | nrecs = len(args[0]) |
| 1086 | margs = [] # Output args; some may be modified. |
| 1087 | seqlist = [False] * len(args) # Flags: True if output will be masked. |
| 1088 | masks = [] # List of masks. |
| 1089 | for i, x in enumerate(args): |
| 1090 | if is_scalar_or_string(x) or len(x) != nrecs: |
| 1091 | margs.append(x) # Leave it unmodified. |
| 1092 | else: |
| 1093 | if isinstance(x, np.ma.MaskedArray) and x.ndim > 1: |
| 1094 | raise ValueError("Masked arrays must be 1-D") |
| 1095 | try: |
| 1096 | x = np.asanyarray(x) |
| 1097 | except (VisibleDeprecationWarning, ValueError): |
| 1098 | # NumPy 1.19 raises a warning about ragged arrays, but we want |
| 1099 | # to accept basically anything here. |
| 1100 | x = np.asanyarray(x, dtype=object) |
| 1101 | if x.ndim == 1: |
| 1102 | x = safe_masked_invalid(x) |
| 1103 | seqlist[i] = True |
| 1104 | if np.ma.is_masked(x): |
| 1105 | masks.append(np.ma.getmaskarray(x)) |
| 1106 | margs.append(x) # Possibly modified. |
| 1107 | if len(masks): |
nothing calls this directly
no test coverage detected
searching dependent graphs…