Set the mask.
(self, mask, copy=False)
| 3443 | self._mask.shape = self.shape |
| 3444 | |
| 3445 | def __setmask__(self, mask, copy=False): |
| 3446 | """ |
| 3447 | Set the mask. |
| 3448 | |
| 3449 | """ |
| 3450 | idtype = self.dtype |
| 3451 | current_mask = self._mask |
| 3452 | if mask is masked: |
| 3453 | mask = True |
| 3454 | |
| 3455 | if current_mask is nomask: |
| 3456 | # Make sure the mask is set |
| 3457 | # Just don't do anything if there's nothing to do. |
| 3458 | if mask is nomask: |
| 3459 | return |
| 3460 | current_mask = self._mask = make_mask_none(self.shape, idtype) |
| 3461 | |
| 3462 | if idtype.names is None: |
| 3463 | # No named fields. |
| 3464 | # Hardmask: don't unmask the data |
| 3465 | if self._hardmask: |
| 3466 | current_mask |= mask |
| 3467 | # Softmask: set everything to False |
| 3468 | # If it's obviously a compatible scalar, use a quick update |
| 3469 | # method. |
| 3470 | elif isinstance(mask, (int, float, np.bool_, np.number)): |
| 3471 | current_mask[...] = mask |
| 3472 | # Otherwise fall back to the slower, general purpose way. |
| 3473 | else: |
| 3474 | current_mask.flat = mask |
| 3475 | else: |
| 3476 | # Named fields w/ |
| 3477 | mdtype = current_mask.dtype |
| 3478 | mask = np.array(mask, copy=False) |
| 3479 | # Mask is a singleton |
| 3480 | if not mask.ndim: |
| 3481 | # It's a boolean : make a record |
| 3482 | if mask.dtype.kind == 'b': |
| 3483 | mask = np.array(tuple([mask.item()] * len(mdtype)), |
| 3484 | dtype=mdtype) |
| 3485 | # It's a record: make sure the dtype is correct |
| 3486 | else: |
| 3487 | mask = mask.astype(mdtype) |
| 3488 | # Mask is a sequence |
| 3489 | else: |
| 3490 | # Make sure the new mask is a ndarray with the proper dtype |
| 3491 | try: |
| 3492 | mask = np.array(mask, copy=copy, dtype=mdtype) |
| 3493 | # Or assume it's a sequence of bool/int |
| 3494 | except TypeError: |
| 3495 | mask = np.array([tuple([m] * len(mdtype)) for m in mask], |
| 3496 | dtype=mdtype) |
| 3497 | # Hardmask: don't unmask the data |
| 3498 | if self._hardmask: |
| 3499 | for n in idtype.names: |
| 3500 | current_mask[n] |= mask[n] |
| 3501 | # Softmask: set everything to False |
| 3502 | # If it's obviously a compatible scalar, use a quick update |