Create a boolean mask from an array. Return `m` as a boolean mask, creating a copy if necessary or requested. The function can accept any sequence that is convertible to integers, or ``nomask``. Does not require that contents must be 0s and 1s, values of 0 are interpreted as F
(m, copy=False, shrink=True, dtype=MaskType)
| 1605 | |
| 1606 | |
| 1607 | def make_mask(m, copy=False, shrink=True, dtype=MaskType): |
| 1608 | """ |
| 1609 | Create a boolean mask from an array. |
| 1610 | |
| 1611 | Return `m` as a boolean mask, creating a copy if necessary or requested. |
| 1612 | The function can accept any sequence that is convertible to integers, |
| 1613 | or ``nomask``. Does not require that contents must be 0s and 1s, values |
| 1614 | of 0 are interpreted as False, everything else as True. |
| 1615 | |
| 1616 | Parameters |
| 1617 | ---------- |
| 1618 | m : array_like |
| 1619 | Potential mask. |
| 1620 | copy : bool, optional |
| 1621 | Whether to return a copy of `m` (True) or `m` itself (False). |
| 1622 | shrink : bool, optional |
| 1623 | Whether to shrink `m` to ``nomask`` if all its values are False. |
| 1624 | dtype : dtype, optional |
| 1625 | Data-type of the output mask. By default, the output mask has a |
| 1626 | dtype of MaskType (bool). If the dtype is flexible, each field has |
| 1627 | a boolean dtype. This is ignored when `m` is ``nomask``, in which |
| 1628 | case ``nomask`` is always returned. |
| 1629 | |
| 1630 | Returns |
| 1631 | ------- |
| 1632 | result : ndarray |
| 1633 | A boolean mask derived from `m`. |
| 1634 | |
| 1635 | Examples |
| 1636 | -------- |
| 1637 | >>> import numpy as np |
| 1638 | >>> import numpy.ma as ma |
| 1639 | >>> m = [True, False, True, True] |
| 1640 | >>> ma.make_mask(m) |
| 1641 | array([ True, False, True, True]) |
| 1642 | >>> m = [1, 0, 1, 1] |
| 1643 | >>> ma.make_mask(m) |
| 1644 | array([ True, False, True, True]) |
| 1645 | >>> m = [1, 0, 2, -3] |
| 1646 | >>> ma.make_mask(m) |
| 1647 | array([ True, False, True, True]) |
| 1648 | |
| 1649 | Effect of the `shrink` parameter. |
| 1650 | |
| 1651 | >>> m = np.zeros(4) |
| 1652 | >>> m |
| 1653 | array([0., 0., 0., 0.]) |
| 1654 | >>> ma.make_mask(m) |
| 1655 | False |
| 1656 | >>> ma.make_mask(m, shrink=False) |
| 1657 | array([False, False, False, False]) |
| 1658 | |
| 1659 | Using a flexible `dtype`. |
| 1660 | |
| 1661 | >>> m = [1, 0, 1, 1] |
| 1662 | >>> n = [0, 1, 0, 0] |
| 1663 | >>> arr = [] |
| 1664 | >>> for man, mouse in zip(m, n): |
searching dependent graphs…