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

Method _comparison

numpy/ma/core.py:4097–4169  ·  view source on GitHub ↗

Compare self with other using operator.eq or operator.ne. When either of the elements is masked, the result is masked as well, but the underlying boolean data are still set, with self and other considered equal if both are masked, and unequal otherwise. For structur

(self, other, compare)

Source from the content-addressed store, hash-verified

4095 return array_ufunc is None
4096
4097 def _comparison(self, other, compare):
4098 """Compare self with other using operator.eq or operator.ne.
4099
4100 When either of the elements is masked, the result is masked as well,
4101 but the underlying boolean data are still set, with self and other
4102 considered equal if both are masked, and unequal otherwise.
4103
4104 For structured arrays, all fields are combined, with masked values
4105 ignored. The result is masked if all fields were masked, with self
4106 and other considered equal only if both were fully masked.
4107 """
4108 omask = getmask(other)
4109 smask = self.mask
4110 mask = mask_or(smask, omask, copy=True)
4111
4112 odata = getdata(other)
4113 if mask.dtype.names is not None:
4114 # only == and != are reasonably defined for structured dtypes,
4115 # so give up early for all other comparisons:
4116 if compare not in (operator.eq, operator.ne):
4117 return NotImplemented
4118 # For possibly masked structured arrays we need to be careful,
4119 # since the standard structured array comparison will use all
4120 # fields, masked or not. To avoid masked fields influencing the
4121 # outcome, we set all masked fields in self to other, so they'll
4122 # count as equal. To prepare, we ensure we have the right shape.
4123 broadcast_shape = np.broadcast(self, odata).shape
4124 sbroadcast = np.broadcast_to(self, broadcast_shape, subok=True)
4125 sbroadcast._mask = mask
4126 sdata = sbroadcast.filled(odata)
4127 # Now take care of the mask; the merged mask should have an item
4128 # masked if all fields were masked (in one and/or other).
4129 mask = (mask == np.ones((), mask.dtype))
4130 # Ensure we can compare masks below if other was not masked.
4131 if omask is np.False_:
4132 omask = np.zeros((), smask.dtype)
4133
4134 else:
4135 # For regular arrays, just use the data as they come.
4136 sdata = self.data
4137
4138 check = compare(sdata, odata)
4139
4140 if isinstance(check, (np.bool_, bool)):
4141 return masked if mask else check
4142
4143 if mask is not nomask:
4144 if compare in (operator.eq, operator.ne):
4145 # Adjust elements that were masked, which should be treated
4146 # as equal if masked in both, unequal if masked in one.
4147 # Note that this works automatically for structured arrays too.
4148 # Ignore this for operations other than `==` and `!=`
4149 check = np.where(mask, compare(smask, omask), check)
4150
4151 if mask.shape != check.shape:
4152 # Guarantee consistency of the shape, making a copy since the
4153 # the mask may need to get written to later.
4154 mask = np.broadcast_to(mask, check.shape).copy()

Callers 6

__eq__Method · 0.95
__ne__Method · 0.95
__le__Method · 0.95
__lt__Method · 0.95
__ge__Method · 0.95
__gt__Method · 0.95

Calls 9

getmaskFunction · 0.85
mask_orFunction · 0.85
getdataFunction · 0.85
_check_fill_valueFunction · 0.85
_update_fromMethod · 0.80
compareFunction · 0.70
filledMethod · 0.45
copyMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected