MCPcopy Create free account
hub / github.com/numpy/numpy / __array_wrap__

Method __array_wrap__

numpy/ma/core.py:3071–3125  ·  view source on GitHub ↗

Special hook for ufuncs. Wraps the numpy array and sets the mask according to context.

(self, obj, context=None)

Source from the content-addressed store, hash-verified

3069 self._fill_value = _check_fill_value(None, self.dtype)
3070
3071 def __array_wrap__(self, obj, context=None):
3072 """
3073 Special hook for ufuncs.
3074
3075 Wraps the numpy array and sets the mask according to context.
3076
3077 """
3078 if obj is self: # for in-place operations
3079 result = obj
3080 else:
3081 result = obj.view(type(self))
3082 result._update_from(self)
3083
3084 if context is not None:
3085 result._mask = result._mask.copy()
3086 func, args, out_i = context
3087 # args sometimes contains outputs (gh-10459), which we don't want
3088 input_args = args[:func.nin]
3089 m = reduce(mask_or, [getmaskarray(arg) for arg in input_args])
3090 # Get the domain mask
3091 domain = ufunc_domain.get(func, None)
3092 if domain is not None:
3093 # Take the domain, and make sure it's a ndarray
3094 with np.errstate(divide='ignore', invalid='ignore'):
3095 d = filled(domain(*input_args), True)
3096
3097 if d.any():
3098 # Fill the result where the domain is wrong
3099 try:
3100 # Binary domain: take the last value
3101 fill_value = ufunc_fills[func][-1]
3102 except TypeError:
3103 # Unary domain: just use this one
3104 fill_value = ufunc_fills[func]
3105 except KeyError:
3106 # Domain not recognized, use fill_value instead
3107 fill_value = self.fill_value
3108
3109 np.copyto(result, fill_value, where=d)
3110
3111 # Update the mask
3112 if m is nomask:
3113 m = d
3114 else:
3115 # Don't modify inplace, we risk back-propagation
3116 m = (m | d)
3117
3118 # Make sure the mask has the proper size
3119 if result is not self and result.shape == () and m:
3120 return masked
3121 else:
3122 result._mask = m
3123 result._sharedmask = False
3124
3125 return result
3126
3127 def view(self, dtype=None, type=None, fill_value=None):
3128 """

Callers 1

__array_wrap__Method · 0.45

Calls 7

getmaskarrayFunction · 0.85
filledFunction · 0.85
_update_fromMethod · 0.80
viewMethod · 0.45
copyMethod · 0.45
getMethod · 0.45
anyMethod · 0.45

Tested by

no test coverage detected