Skip to content

Commit

Permalink
more forgiving about creating from iterable of targetingflags, and ad…
Browse files Browse the repository at this point in the history
…d in_carton_label
  • Loading branch information
andycasey committed Oct 11, 2023
1 parent e30f397 commit 17076aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
11 changes: 6 additions & 5 deletions python/sdss_semaphore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def __init__(self, array: Optional[Union[np.ndarray, Iterable[Iterable[int]], It
self.array[i, :len(item)] = np.frombuffer(item, dtype=self.dtype)
elif len(array) > 0 and isinstance(array[0], BaseFlags):
# need to pad the array to the maximum size
N, F = (0, 0)
N, F, si = (0, 0, 0)
for item in array:
n, f = item.array.shape
F = max(F, f)
N += n
if N != len(array):
raise ValueError("All items must have only one row when building from an iterable of BaseFlags")
self.array = np.zeros((N, F), dtype=self.dtype)
for i, item in enumerate(array):
self.array[i, :item.array.shape[1]] = item.array
for item in array:
n, f = item.array.shape
self.array[si:si + n, :f] = item.array
si += n
else:
self.array = np.atleast_2d(array).astype(self.dtype)
return None
Expand Down Expand Up @@ -74,6 +74,7 @@ def flags_set(self) -> Iterable[Tuple[Dict]]:
yield tuple(self.mapping[bit] for bit in np.where(row)[0])

def _all_attributes(self, key):
"""Helper function to return unique set of attributes for all flags."""
return tuple(set(attrs[key] for attrs in self.mapping.values()))

def count(self, skip_empty: bool = False) -> dict:
Expand Down
22 changes: 15 additions & 7 deletions python/sdss_semaphore/targeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ def all_alt_names(self) -> Tuple[str]:
def all_alt_programs(self) -> Tuple[str]:
"""Return a list of all alternative carton programs."""
return self._all_attributes("alt_program")

def in_carton_label(self, label: str) -> np.array:
"""
Return a N-length boolean array indicating whether the items are assigned to the carton with the given label.
:param label:
The carton label.
"""
return self.is_attribute_set("label", label)

def in_carton_pk(self, carton_pk: int) -> np.array:
"""
Expand All @@ -45,7 +53,7 @@ def in_carton_pk(self, carton_pk: int) -> np.array:
"""
return self.is_attribute_set("carton_pk", carton_pk)

def in_carton_name(self, name) -> np.array:
def in_carton_name(self, name: str) -> np.array:
"""
Return an N-length boolean array indicating whether the items are assigned to a carton with the given name.
Expand All @@ -54,7 +62,7 @@ def in_carton_name(self, name) -> np.array:
"""
return self.is_attribute_set("name", name)

def in_mapper(self, mapper) -> np.array:
def in_mapper(self, mapper: str) -> np.array:
"""
Return an N-length boolean array indicating whether the items are assigned to any cartons with the given mapper.
Expand All @@ -63,7 +71,7 @@ def in_mapper(self, mapper) -> np.array:
"""
return self.is_attribute_set("mapper", mapper)

def in_program(self, program) -> np.array:
def in_program(self, program: str) -> np.array:
"""
Return an N-length boolean array indicating whether the items are assigned to any cartons with the given program.
Expand All @@ -72,7 +80,7 @@ def in_program(self, program) -> np.array:
"""
return self.is_attribute_set("program", program)

def in_alt_name(self, alt_name) -> np.array:
def in_alt_name(self, alt_name: str) -> np.array:
"""
Return an N-length boolean array indicating whether the items are assigned to any cartons with the given alternative name.
Expand All @@ -81,7 +89,7 @@ def in_alt_name(self, alt_name) -> np.array:
"""
return self.is_attribute_set("alt_name", alt_name)

def in_alt_program(self, alt_program) -> np.array:
def in_alt_program(self, alt_program: str) -> np.array:
"""
Return an N-length boolean array indicating whether the items are assigned to any cartons with the given alternative program.
Expand Down Expand Up @@ -115,11 +123,11 @@ def set_bit_by_carton_pk(self, index: int, carton_pk: int):
:param carton_pk:
The carton primary key.
"""
bit = self.get_bit_position_from_carton_pk[carton_pk]
bit = self.bit_position_from_carton_pk[carton_pk]
return self.set_bit(index, bit)

@cached_property
def get_bit_position_from_carton_pk(self):
def bit_position_from_carton_pk(self):
"""
Return a dictionary with carton primary keys as keys, and bit positions as values.
Expand Down

0 comments on commit 17076aa

Please sign in to comment.