| 6739 | |
| 6740 | |
| 6741 | class MaskedConstant(MaskedArray): |
| 6742 | # the lone np.ma.masked instance |
| 6743 | __singleton = None |
| 6744 | |
| 6745 | @classmethod |
| 6746 | def __has_singleton(cls): |
| 6747 | # second case ensures `cls.__singleton` is not just a view on the |
| 6748 | # superclass singleton |
| 6749 | return cls.__singleton is not None and type(cls.__singleton) is cls |
| 6750 | |
| 6751 | def __new__(cls): |
| 6752 | if not cls.__has_singleton(): |
| 6753 | # We define the masked singleton as a float for higher precedence. |
| 6754 | # Note that it can be tricky sometimes w/ type comparison |
| 6755 | data = np.array(0.) |
| 6756 | mask = np.array(True) |
| 6757 | |
| 6758 | # prevent any modifications |
| 6759 | data.flags.writeable = False |
| 6760 | mask.flags.writeable = False |
| 6761 | |
| 6762 | # don't fall back on MaskedArray.__new__(MaskedConstant), since |
| 6763 | # that might confuse it - this way, the construction is entirely |
| 6764 | # within our control |
| 6765 | cls.__singleton = MaskedArray(data, mask=mask).view(cls) |
| 6766 | |
| 6767 | return cls.__singleton |
| 6768 | |
| 6769 | def __array_finalize__(self, obj): |
| 6770 | if not self.__has_singleton(): |
| 6771 | # this handles the `.view` in __new__, which we want to copy across |
| 6772 | # properties normally |
| 6773 | return super().__array_finalize__(obj) |
| 6774 | elif self is self.__singleton: |
| 6775 | # not clear how this can happen, play it safe |
| 6776 | pass |
| 6777 | else: |
| 6778 | # everywhere else, we want to downcast to MaskedArray, to prevent a |
| 6779 | # duplicate maskedconstant. |
| 6780 | self.__class__ = MaskedArray |
| 6781 | MaskedArray.__array_finalize__(self, obj) |
| 6782 | |
| 6783 | def __array_wrap__(self, obj, context=None, return_scalar=False): |
| 6784 | return self.view(MaskedArray).__array_wrap__(obj, context) |
| 6785 | |
| 6786 | def __str__(self): |
| 6787 | return str(masked_print_option._display) |
| 6788 | |
| 6789 | def __repr__(self): |
| 6790 | if self is MaskedConstant.__singleton: |
| 6791 | return 'masked' |
| 6792 | else: |
| 6793 | # it's a subclass, or something is wrong, make it obvious |
| 6794 | return object.__repr__(self) |
| 6795 | |
| 6796 | def __format__(self, format_spec): |
| 6797 | # Replace ndarray.__format__ with the default, which supports no |
| 6798 | # format characters. |
no outgoing calls
no test coverage detected
searching dependent graphs…