Set the mask.
(self, mask, copy=False)
| 3509 | self._mask = self._mask.reshape(self.shape) |
| 3510 | |
| 3511 | def __setmask__(self, mask, copy=False): |
| 3512 | """ |
| 3513 | Set the mask. |
| 3514 | |
| 3515 | """ |
| 3516 | idtype = self.dtype |
| 3517 | current_mask = self._mask |
| 3518 | if mask is masked: |
| 3519 | mask = True |
| 3520 | |
| 3521 | if current_mask is nomask: |
| 3522 | # Make sure the mask is set |
| 3523 | # Just don't do anything if there's nothing to do. |
| 3524 | if mask is nomask: |
| 3525 | return |
| 3526 | current_mask = self._mask = make_mask_none(self.shape, idtype) |
| 3527 | |
| 3528 | if idtype.names is None: |
| 3529 | # No named fields. |
| 3530 | # Hardmask: don't unmask the data |
| 3531 | if self._hardmask: |
| 3532 | current_mask |= mask |
| 3533 | # Softmask: set everything to False |
| 3534 | # If it's obviously a compatible scalar, use a quick update |
| 3535 | # method. |
| 3536 | elif isinstance(mask, (int, float, np.bool, np.number)): |
| 3537 | current_mask[...] = mask |
| 3538 | # Otherwise fall back to the slower, general purpose way. |
| 3539 | else: |
| 3540 | current_mask.flat = mask |
| 3541 | else: |
| 3542 | # Named fields w/ |
| 3543 | mdtype = current_mask.dtype |
| 3544 | mask = np.asarray(mask) |
| 3545 | # Mask is a singleton |
| 3546 | if not mask.ndim: |
| 3547 | # It's a boolean : make a record |
| 3548 | if mask.dtype.kind == 'b': |
| 3549 | mask = np.array(tuple([mask.item()] * len(mdtype)), |
| 3550 | dtype=mdtype) |
| 3551 | # It's a record: make sure the dtype is correct |
| 3552 | else: |
| 3553 | mask = mask.astype(mdtype) |
| 3554 | # Mask is a sequence |
| 3555 | else: |
| 3556 | # Make sure the new mask is an ndarray with the proper dtype |
| 3557 | try: |
| 3558 | copy = None if not copy else True |
| 3559 | mask = np.array(mask, copy=copy, dtype=mdtype) |
| 3560 | # Or assume it's a sequence of bool/int |
| 3561 | except TypeError: |
| 3562 | mask = np.array([tuple([m] * len(mdtype)) for m in mask], |
| 3563 | dtype=mdtype) |
| 3564 | # Hardmask: don't unmask the data |
| 3565 | if self._hardmask: |
| 3566 | for n in idtype.names: |
| 3567 | current_mask[n] |= mask[n] |
| 3568 | # Softmask: set everything to False |