Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use int minval instead -1 for invalid telescope ids in SubarrayDescription.tel_index_array, improve docstrings #2376

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions ctapipe/instrument/subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class SubarrayDescription:
----------
name: str
name of subarray
tel_coords: astropy.coordinates.SkyCoord
tel_coords: ctapipe.coordinates.GroundFrame
Tobychev marked this conversation as resolved.
Show resolved Hide resolved
coordinates of all telescopes
tels:
dict of TelescopeDescription for each telescope in the subarray
"""

#: Current version number of the format written by `SubarrayDescription.to_hdf`
CURRENT_TAB_VERSION = "2.0"
#: Version numbers supported by `SubarrayDescription.from_hdf`
COMPATIBLE_VERSIONS = {"2.0"}

def __init__(
Expand Down Expand Up @@ -148,7 +150,7 @@ def info(self, printer=print):

@lazyproperty
def tel_coords(self):
"""returns telescope positions as astropy.coordinates.SkyCoord"""
"""Telescope positions in `~ctapipe.coordinates.GroundFrame`"""

pos_x = [p[0].to_value(u.m) for p in self.positions.values()]
pos_y = [p[1].to_value(u.m) for p in self.positions.values()]
Expand All @@ -160,25 +162,28 @@ def tel_coords(self):

@lazyproperty
def tel_ids(self):
"""telescope IDs as an array"""
"""Array of telescope ids in order of telescope indices"""
return np.array(list(self.tel.keys()))

@lazyproperty
def tel_indices(self):
"""returns dict mapping tel_id to tel_index, useful for unpacking
lists based on tel_ids into fixed-length arrays"""
"""dictionary mapping telescope ids to telescope index"""
return {tel_id: ii for ii, tel_id in enumerate(self.tels.keys())}

@lazyproperty
def tel_index_array(self):
"""
returns an expanded array that maps tel_id to tel_index. I.e. for a given
telescope, this array maps the tel_id to a flat index starting at 0 for
the first telescope. ``tel_index = tel_id_to_index_array[tel_id]``
If the tel_ids are not contiguous, gaps will be filled in by -1.
Array of telescope indices that can be indexed by telescope id

I.e. for a given telescope, this array maps the tel_id to a flat index
starting at 0 for the first telescope.
``tel_index = subarray.tel_index_array[tel_id]``

If the tel_ids are not contiguous, gaps will be filled in by int minval.
For a more compact representation use the `tel_indices`
"""
idx = np.zeros(np.max(self.tel_ids) + 1, dtype=int) - 1 # start with -1
invalid = np.iinfo(int).min
idx = np.full(np.max(self.tel_ids) + 1, invalid, dtype=int)
for key, val in self.tel_indices.items():
idx[key] = val
return idx
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/2376.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change the fill value for invalid telescope ids in ``SubarrayDescription.tel_index_array``
from ``-1`` to ``np.iinfo(int).minval`` to prevent ``-1`` being used as an index resulting in the last element being used for invalid telescope ids.