[docs]classDelayedMask(delayedarray.DelayedOp):""" Delayed mask to replace the missing value placeholder with a NumPy masked array. """def__init__(self,seed,placeholder,dtype:Optional[numpy.dtype]=None):""" Args: seed: Any object that satisfies the seed contract, see :py:class:`~delayedarray.DelayedArray.DelayedArray` for details. placeholder: Placeholder value for defining masked values, of the same type as ``seed.dtype`` (or coercible into that type). All values equal to the placeholder are considered to be missing. dtype: Desired type of the masked output, defaults to ``seed.dtype``. """self._seed=seedifnumpy.issubdtype(seed.dtype,numpy.str_)andisinstance(placeholder,bytes):self._placeholder=numpy.str_(placeholder.decode("UTF8"))else:self._placeholder=seed.dtype.type(placeholder)ifdtypeisNone:dtype=seed.dtypeself._dtype=dtype@propertydefshape(self)->Tuple[int,...]:""" Returns: Tuple of integers specifying the extent of each dimension of this object. This is the same as the ``seed`` object. """returnself._seed.shape@propertydefdtype(self)->numpy.dtype:""" Returns: NumPy type for the contents after masking. """returnself._dtype@propertydefseed(self):""" Returns: The seed object. """returnself._seed@propertydefplaceholder(self):""" Returns: The placeholder value. """returnself._placeholder
[docs]@delayedarray.extract_dense_array.registerdefextract_dense_array_DelayedMask(x:DelayedMask,subset:Tuple[Sequence[int],...]):"""See :py:meth:`~delayedarray.extract_dense_array.extract_dense_array`."""out=delayedarray.extract_dense_array(x._seed,subset)mask=_create_mask(out,x._placeholder)# do this before type coercion, as the placeholder is assumed to be of the same underlying seed type.out=out.astype(x._dtype,copy=False)returnnumpy.ma.MaskedArray(out,mask=mask)
def_mask_SparseNdarray(contents,placeholder,dtype):ifnotisinstance(contents,list):indices,values=contentsmask=_create_mask(values,placeholder)# do this before type coercion, again.values=values.astype(dtype,copy=False)returnindices,numpy.ma.MaskedArray(values,mask=mask)else:output=[]forvalincontents:ifvalisNone:output.append(val)else:output.append(_mask_SparseNdarray(val,placeholder,dtype))returnoutput