Defines masked version of unary operations, where invalid values are pre-masked. Parameters ---------- mufunc : callable The function for which to define a masked version. Made available as ``_MaskedUnaryOperation.f``. fill : scalar, optional Filling
| 901 | |
| 902 | |
| 903 | class _MaskedUnaryOperation(_MaskedUFunc): |
| 904 | """ |
| 905 | Defines masked version of unary operations, where invalid values are |
| 906 | pre-masked. |
| 907 | |
| 908 | Parameters |
| 909 | ---------- |
| 910 | mufunc : callable |
| 911 | The function for which to define a masked version. Made available |
| 912 | as ``_MaskedUnaryOperation.f``. |
| 913 | fill : scalar, optional |
| 914 | Filling value, default is 0. |
| 915 | domain : class instance |
| 916 | Domain for the function. Should be one of the ``_Domain*`` |
| 917 | classes. Default is None. |
| 918 | |
| 919 | """ |
| 920 | |
| 921 | def __init__(self, mufunc, fill=0, domain=None): |
| 922 | super().__init__(mufunc) |
| 923 | self.fill = fill |
| 924 | self.domain = domain |
| 925 | ufunc_domain[mufunc] = domain |
| 926 | ufunc_fills[mufunc] = fill |
| 927 | |
| 928 | def __call__(self, a, *args, **kwargs): |
| 929 | """ |
| 930 | Execute the call behavior. |
| 931 | |
| 932 | """ |
| 933 | d = getdata(a) |
| 934 | # Deal with domain |
| 935 | if self.domain is not None: |
| 936 | # Case 1.1. : Domained function |
| 937 | # nans at masked positions cause RuntimeWarnings, even though |
| 938 | # they are masked. To avoid this we suppress warnings. |
| 939 | with np.errstate(divide='ignore', invalid='ignore'): |
| 940 | result = self.f(d, *args, **kwargs) |
| 941 | # Make a mask |
| 942 | m = ~umath.isfinite(result) |
| 943 | m |= self.domain(d) |
| 944 | m |= getmask(a) |
| 945 | else: |
| 946 | # Case 1.2. : Function without a domain |
| 947 | # Get the result and the mask |
| 948 | with np.errstate(divide='ignore', invalid='ignore'): |
| 949 | result = self.f(d, *args, **kwargs) |
| 950 | m = getmask(a) |
| 951 | |
| 952 | if not result.ndim: |
| 953 | # Case 2.1. : The result is scalarscalar |
| 954 | if m: |
| 955 | return masked |
| 956 | return result |
| 957 | |
| 958 | if m is not nomask: |
| 959 | # Case 2.2. The result is an array |
| 960 | # We need to fill the invalid data back w/ the input Now, |