Create a new masked array from scratch. Notes ----- A masked array can also be created by taking a .view(MaskedArray).
(cls, data=None, mask=nomask, dtype=None, copy=False,
subok=True, ndmin=0, fill_value=None, keep_mask=True,
hard_mask=None, shrink=True, order=None)
| 2880 | _print_width_1d = 1500 |
| 2881 | |
| 2882 | def __new__(cls, data=None, mask=nomask, dtype=None, copy=False, |
| 2883 | subok=True, ndmin=0, fill_value=None, keep_mask=True, |
| 2884 | hard_mask=None, shrink=True, order=None): |
| 2885 | """ |
| 2886 | Create a new masked array from scratch. |
| 2887 | |
| 2888 | Notes |
| 2889 | ----- |
| 2890 | A masked array can also be created by taking a .view(MaskedArray). |
| 2891 | |
| 2892 | """ |
| 2893 | # Process data. |
| 2894 | copy = None if not copy else True |
| 2895 | _data = np.array(data, dtype=dtype, copy=copy, |
| 2896 | order=order, subok=True, ndmin=ndmin) |
| 2897 | _baseclass = getattr(data, '_baseclass', type(_data)) |
| 2898 | # Check that we're not erasing the mask. |
| 2899 | if isinstance(data, MaskedArray) and (data.shape != _data.shape): |
| 2900 | copy = True |
| 2901 | |
| 2902 | # Here, we copy the _view_, so that we can attach new properties to it |
| 2903 | # we must never do .view(MaskedConstant), as that would create a new |
| 2904 | # instance of np.ma.masked, which make identity comparison fail |
| 2905 | if isinstance(data, cls) and subok and not isinstance(data, MaskedConstant): |
| 2906 | _data = ndarray.view(_data, type(data)) |
| 2907 | else: |
| 2908 | _data = ndarray.view(_data, cls) |
| 2909 | |
| 2910 | # Handle the case where data is not a subclass of ndarray, but |
| 2911 | # still has the _mask attribute like MaskedArrays |
| 2912 | if hasattr(data, '_mask') and not isinstance(data, ndarray): |
| 2913 | _data._mask = data._mask |
| 2914 | # FIXME: should we set `_data._sharedmask = True`? |
| 2915 | # Process mask. |
| 2916 | # Type of the mask |
| 2917 | mdtype = make_mask_descr(_data.dtype) |
| 2918 | if mask is nomask: |
| 2919 | # Case 1. : no mask in input. |
| 2920 | # Erase the current mask ? |
| 2921 | if not keep_mask: |
| 2922 | # With a reduced version |
| 2923 | if shrink: |
| 2924 | _data._mask = nomask |
| 2925 | # With full version |
| 2926 | else: |
| 2927 | _data._mask = np.zeros(_data.shape, dtype=mdtype) |
| 2928 | # Check whether we missed something |
| 2929 | elif isinstance(data, (tuple, list)): |
| 2930 | try: |
| 2931 | # If data is a sequence of masked array |
| 2932 | mask = np.array( |
| 2933 | [getmaskarray(np.asanyarray(m, dtype=_data.dtype)) |
| 2934 | for m in data], dtype=mdtype) |
| 2935 | except (ValueError, TypeError): |
| 2936 | # If data is nested |
| 2937 | mask = nomask |
| 2938 | # Force shrinking of the mask if needed (and possible) |
| 2939 | if (mdtype == MaskType) and mask.any(): |
nothing calls this directly
no test coverage detected