bitfield_to_boolean_mask¶
-
ccdproc.
bitfield_to_boolean_mask
(bitfield, ignore_bits=0, flip_bits=None)[source]¶ Convert an integer bit field to a boolean mask.
Parameters: bitfield :
numpy.ndarray
of integer dtypeThe array of bit flags.
ignore_bits : int, None or str, optional
The bits to ignore when converting the bitfield.
- If it’s an integer it’s binary representation is interpreted as the
bits to ignore.
0
means that all bit flags are taken into account while a binary representation of all1
means that all flags would be ignored. - If it’s
None
then all flags are ignored - If it’s a string then it must be a
,
or+
separated string of integers that bits to ignore. If the string starts with an~
the integers are interpreted as the only flags to take into account.
Default is
0
.Returns: mask :
numpy.ndarray
of boolean dtypeThe bitfield converted to a boolean mask that can be used for
numpy.ma.MaskedArray
orCCDData
.Examples
Bitfields (or data quality arrays) are integer arrays where the binary representation of the values indicates whether a specific flag is set or not. The convention is that a value of
0
represents a good value and a value that is!= 0
represents a value that is in some (or multiple) ways considered a bad value. Thebitfield_to_boolean_mask
function can be used by default to create a boolean mask wherever any bit flag is set:>>> import ccdproc >>> import numpy as np >>> ccdproc.bitfield_to_boolean_mask(np.arange(8)) array([False, True, True, True, True, True, True, True], dtype=bool)
To ignore all bit flags
ignore_bits=None
can be used:>>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits=None) array([False, False, False, False, False, False, False, False], dtype=bool)
To ignore only specific bit flags one can use a
list
of bits flags to ignore:>>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits=[1, 4]) array([False, False, True, True, False, False, True, True], dtype=bool)
There are some equivalent ways:
>>> # pass in the sum of the "ignore_bits" directly >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits=5) # 1 + 4 array([False, False, True, True, False, False, True, True], dtype=bool) >>> # use a comma seperated string of integers >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits='1, 4') array([False, False, True, True, False, False, True, True], dtype=bool) >>> # use a + seperated string of integers >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits='1+4') array([False, False, True, True, False, False, True, True], dtype=bool)
Instead of directly specifying the bits flags to ignore one can also pass in the only bits that shouldn’t be ignored by prepending a
~
to the string ofignore_bits
(or if it’s not a string inignore_bits
one can setflip_bits=True
):>>> # ignore all bit flags except the one for 2. >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits='~(2)') array([False, False, True, True, False, False, True, True], dtype=bool) >>> # ignore all bit flags except the one for 1, 8 and 32. >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits='~(1, 8, 32)') array([False, True, False, True, False, True, False, True], dtype=bool) >>> # Equivalent for a list using flip_bits. >>> ccdproc.bitfield_to_boolean_mask(np.arange(8), ignore_bits=[1, 8, 32], flip_bits=True) array([False, True, False, True, False, True, False, True], dtype=bool)
- If it’s an integer it’s binary representation is interpreted as the
bits to ignore.