forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmask.py
238 lines (211 loc) · 7.69 KB
/
bitmask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import yaml
import numpy as np
class _MaskBit(int):
"""
Taken from desiutil/bitmask.py
A single mask bit.
Subclasses :class:`int` to act like an :class:`int`, but allows the
ability to extend with blat.name, blat.comment, blat.mask, blat.bitnum.
Attributes
----------
name : :class:`str`
The name of the bit.
bitnum : :class:`int`
The number of the bit. The value of the bit is ``2**bitnum``.
mask : :class:`int`
The value of the bit, ``2**bitnum``.
comment : :class:`str`
A comment explaining the meaning of the bit.
"""
def __new__(cls, name, bitnum, comment, extra=dict()):
self = super(_MaskBit, cls).__new__(cls, 2**bitnum)
self.name = name
self.bitnum = bitnum
self.mask = 2**bitnum
self.comment = comment
self._extra = extra
for key, value in extra.items():
if hasattr(self, key):
raise AttributeError(
"Bit {0} extra key '{1}' is already in use by int objects.".format(name, key))
self.__dict__[key] = value
return self
def __str__(self):
return ('{0.name:16s} bit {0.bitnum} mask 0x{0.mask:X} - ' +
'{0.comment}').format(self)
# def __repr__(self):
# return "_MaskBit(name='{0.name}', bitnum={0.bitnum:d}, comment='{0.comment}')".format(self)
# Class to provide mask bit utility functions
class BitMask(object):
"""BitMask object to represent bit names, masks, and comments.
Typical users are not expected to create BitMask objects directly;
other packages like desispec and desitarget will have used this
to pre-create the bitmasks for them using definition files in those
packages.
Parameters
----------
name : :class:`str`
Name of this mask, must be key in `bitdefs`.
bitdefs : :class:`dict`
Dictionary of different mask bit definitions;
each value is a list of ``[bitname, bitnum, comment]``.
A 4th entry is optional, which must be a dictionary.
"""
def __init__(self, name, bitdefs):
"""Init.
"""
self._bits = dict()
self._name = name
for x in bitdefs[name]:
bitname, bitnum, comment = x[0:3]
if len(x) == 4:
extra = x[3]
if not isinstance(extra, dict):
raise ValueError(
'{} extra values should be a dict'.format(bitname))
else:
extra = dict()
self._bits[bitname] = _MaskBit(bitname, bitnum, comment, extra)
self._bits[bitnum] = self._bits[bitname]
def __getitem__(self, bitname):
"""Return mask for individual bitname.
"""
return self._bits[bitname]
def bitnum(self, bitname):
"""Return bit number (int) for this `bitname` (string).
Parameters
----------
bitname : :class:`str`
The bit name.
Returns
-------
:class:`int`
The bit value.
"""
return self._bits[bitname].bitnum
def bitname(self, bitnum):
"""Return bit name (string) for this `bitnum` (integer).
Parameters
----------
bitnum : :class:`int`
The number of the bit.
Returns
-------
:class:`str`
The name of the bit.
"""
return self._bits[bitnum].name
def comment(self, bitname_or_num):
"""Return comment for this bit name or bit number.
Parameters
----------
bitname_or_num : :class:`int` or :class:`str`
Name of number of the mask.
Returns
-------
:class:`str`
The comment string.
"""
return self._bits[bitname_or_num].comment
def mask(self, name_or_num):
"""Return mask value.
Parameters
----------
name_or_num : :class:`int` or :class:`str`
Name of number of the mask.
Returns
-------
:class:`int`
The value of the mask.
Examples
--------
>>> bitmask.mask(3) # 2**3
8
>>> bitmask.mask('BLAT')
>>> bitmask.mask('BLAT|FOO')
"""
if isinstance(name_or_num, int):
return self._bits[name_or_num].mask
else:
mask = 0
for name in name_or_num.split('|'):
mask |= self._bits[name].mask
return mask
def names(self, mask=None):
"""Return list of names of masked bits.
Parameters
----------
mask : :class:`int`, optional
The mask integer to convert to names. If not supplied,
return names of all known bits.
Returns
-------
:class:`list`
The list of names contained in the mask.
"""
names = list()
if mask is None:
# return names in sorted order of bitnum
bitnums = [x for x in self._bits.keys() if isinstance(x, int)]
for bitnum in sorted(bitnums):
names.append(self._bits[bitnum].name)
else:
mask = int(mask) # workaround numpy issue #2955 for uint64
bitnum = 0
while 2**bitnum <= mask:
if (2**bitnum & mask):
if bitnum in self._bits.keys():
names.append(self._bits[bitnum].name)
else:
names.append('UNKNOWN' + str(bitnum))
bitnum += 1
return names
def __getattr__(self, name):
"""Enable ``mask.BITNAME`` equivalent to ``mask['BITNAME']``.
"""
if name in self._bits:
return self._bits[name]
else:
raise AttributeError('Unknown mask bit name ' + name)
def __repr__(self):
'''Return yaml representation defining the bits of this bitmask.
'''
result = list()
result.append(self._name + ':')
# return names in sorted order of bitnum
bitnums = [x for x in self._bits.keys() if isinstance(x, int)]
for bitnum in sorted(bitnums):
bit = self._bits[bitnum]
# format the line for single bit, with or without extra keys
line = ' - [{:16s} {:2d}, "{}"'.format(
bit.name+',', bit.bitnum, bit.comment)
if len(bit._extra) > 0:
line = line + ', '+str(bit._extra)+']'
else:
line = line + ']'
result.append(line)
return "\n".join(result)
def not_set(mask, attribute, column):
return (column.data & getattr(mask, attribute)) == 0
def update_bit(column, mask, attribute, updates):
print('{} has fraction {} to be updated'.format(attribute, np.mean(updates)))
ns = not_set(mask, attribute, column)
print('{} has fraction {} not set.'.format(attribute, np.mean(ns)))
column.data[updates & ns] += getattr(mask, attribute)
print('{} has fraction {} now set.'.format(attribute, np.mean(updates & ns)))
_bitdefs = yaml.safe_load('''
lumfn_mask:
- [DDP1ZLIM, 0, "Galaxy not in DDP limits"]
- [FILLFACTOR, 1, "Fillfactor < fillfactor_threshold"]
- [INBGSBRIGHT, 2, "Galaxy not in BGS Bright"]
- [CONSERVATIVE, 3, "Galaxy not conserved; see CONSERVATIVE mask"]
- [DESI_HICOMP, 4, "High completeness region of DESI, e.g. 0.5 - 1.5 deg. of rosette."]
- [ZMAX_FAIL, 5, "Failed the zmax calculation."]
''')
_cbitdefs = yaml.safe_load('''
consv_mask:
- [DDP1ZLIM, 0, "Galaxy not in conservative DDP limits"]
- [BOUNDDIST, 1, "Boundary distance < 8"]
''')
lumfn_mask = BitMask('lumfn_mask', _bitdefs)
consv_mask = BitMask('consv_mask', _cbitdefs)