Skip to content

Commit

Permalink
add helper properties to just get the bits/flags that are set
Browse files Browse the repository at this point in the history
  • Loading branch information
andycasey committed Oct 11, 2023
1 parent 8c67ff5 commit 256748c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion python/sdss_semaphore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import warnings
from typing import Union, Tuple, Iterable, List, Optional, Tuple
from typing import Dict, Union, Tuple, Iterable, List, Optional, Tuple


class BaseFlags:
Expand Down Expand Up @@ -50,6 +50,29 @@ def n_bits(self):
def mapping(self):
raise NotImplementedError(f"`mapping` must be defined in subclass")

@property
def bits_set(self) -> Iterable[Tuple[int]]:
"""
A generator that yields a tuple of bits set for each item.
If you want a more efficient lookup, you can use:
items, bits = np.where(flags.as_boolean_array())
"""
for row in self.as_boolean_array():
yield tuple(np.where(row)[0])

@property
def flags_set(self) -> Iterable[Tuple[Dict]]:
"""
A generator that yields all set flags.
This can be a hugely expensive query if you have large number of items (e.g., stars).
"""
for row in self.as_boolean_array():
yield tuple(self.mapping[bit] for bit in np.where(row)[0])

def _all_attributes(self, key):
return tuple(set(attrs[key] for attrs in self.mapping.values()))

Expand Down

0 comments on commit 256748c

Please sign in to comment.