Special hook for ufuncs. Wraps the numpy array and sets the mask according to context.
(self, obj, context=None, return_scalar=False)
| 3141 | self._fill_value = _check_fill_value(None, self.dtype) |
| 3142 | |
| 3143 | def __array_wrap__(self, obj, context=None, return_scalar=False): |
| 3144 | """ |
| 3145 | Special hook for ufuncs. |
| 3146 | |
| 3147 | Wraps the numpy array and sets the mask according to context. |
| 3148 | |
| 3149 | """ |
| 3150 | if obj is self: # for in-place operations |
| 3151 | result = obj |
| 3152 | else: |
| 3153 | result = obj.view(type(self)) |
| 3154 | result._update_from(self) |
| 3155 | |
| 3156 | if context is not None: |
| 3157 | result._mask = result._mask.copy() |
| 3158 | func, args, out_i = context |
| 3159 | # args sometimes contains outputs (gh-10459), which we don't want |
| 3160 | input_args = args[:func.nin] |
| 3161 | m = functools.reduce(mask_or, [getmaskarray(arg) for arg in input_args]) |
| 3162 | # Get the domain mask |
| 3163 | domain = ufunc_domain.get(func) |
| 3164 | if domain is not None: |
| 3165 | # Take the domain, and make sure it's an ndarray |
| 3166 | with np.errstate(divide='ignore', invalid='ignore'): |
| 3167 | # The result may be masked for two (unary) domains. |
| 3168 | # That can't really be right as some domains drop |
| 3169 | # the mask and some don't behaving differently here. |
| 3170 | d = domain(*input_args).astype(bool, copy=False) |
| 3171 | d = filled(d, True) |
| 3172 | |
| 3173 | if d.any(): |
| 3174 | # Fill the result where the domain is wrong |
| 3175 | try: |
| 3176 | # Binary domain: take the last value |
| 3177 | fill_value = ufunc_fills[func][-1] |
| 3178 | except TypeError: |
| 3179 | # Unary domain: just use this one |
| 3180 | fill_value = ufunc_fills[func] |
| 3181 | except KeyError: |
| 3182 | # Domain not recognized, use fill_value instead |
| 3183 | fill_value = self.fill_value |
| 3184 | |
| 3185 | np.copyto(result, fill_value, where=d) |
| 3186 | |
| 3187 | # Update the mask |
| 3188 | if m is nomask: |
| 3189 | m = d |
| 3190 | else: |
| 3191 | # Don't modify inplace, we risk back-propagation |
| 3192 | m = (m | d) |
| 3193 | |
| 3194 | # Make sure the mask has the proper size |
| 3195 | if result is not self and result.shape == () and m: |
| 3196 | return masked |
| 3197 | else: |
| 3198 | result._mask = m |
| 3199 | result._sharedmask = False |
| 3200 |
no test coverage detected