Skip to content

Commit

Permalink
Small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabaldini committed Apr 16, 2024
1 parent cd4a77c commit e406e34
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
4 changes: 1 addition & 3 deletions hexsample/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ def draw(self, offset: Tuple[float, float] = (0., 0.), pixel_labels: bool = Fals
"""Draw the full grid display.
"""
# pylint: disable = invalid-name, too-many-locals
col = np.tile(np.arange(self._grid.num_cols), self._grid.num_rows)
row = np.repeat(np.arange(self._grid.num_rows), self._grid.num_cols)
x, y = self._grid.pixel_to_world(col, row)
col, row, x, y = self._grid.pixel_physical_coordinates()
dx, dy = offset
collection = HexagonCollection(x + dx, y + dy, 0.5 * self._grid.pitch,
self._grid.hexagon_orientation(), **kwargs)
Expand Down
23 changes: 23 additions & 0 deletions hexsample/hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ def world_to_pixel(self, x: np.array, y: np.array) -> Tuple[np.array, np.array]:
col, row = self._axial_to_offset(q, r)
return col, row

def pixel_logical_coordinates(self) -> Tuple[np.array, np.array]:
"""Return a 2-element tuple (cols, rows) of numpy arrays containing all the
column and row indices that allow to loop over the full matrix. The specific
order in which the pixels are looped upon is completely arbitrary and, for
the sake of this function, we assume that, e.g., for a 2 x 2 grid we return
>>> cols = [0, 1, 0, 1]
>>> rows = [0, 0, 1, 1]
i.e., we loop with the following order
>>> (0, 0), (1, 0), (0, 1), (1, 1)
"""
cols = np.tile(np.arange(self.num_cols), self.num_rows)
rows = np.repeat(np.arange(self.num_rows), self.num_cols)
return cols, rows

def pixel_physical_coordinates(self) -> Tuple[np.array, np.array, np.array, np.array]:
"""Return a 4-element tuple (cols, rows, x, y) containing the logical
coordinates returned by pixel_logical_coordinates(), along with the
corresponding physical coordinates.
"""
cols, rows = self.pixel_logical_coordinates()
x, y = self.pixel_to_world(cols, rows)
return cols, rows, x, y

def __str__(self):
"""String formatting.
"""
Expand Down
33 changes: 29 additions & 4 deletions tests/test_hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from hexsample.plot import plt


def test_parity(nside : int = 10, pitch : float = 0.1):
def test_parity(nside: int = 10, pitch: float = 0.1):
"""Test the HexagonalGrid._parity_offset() method.
"""
for layout in (HexagonalLayout.EVEN_R, HexagonalLayout.EVEN_Q):
Expand All @@ -35,7 +35,16 @@ def test_parity(nside : int = 10, pitch : float = 0.1):
assert grid._parity_offset(0) == 0
assert grid._parity_offset(1) == -1

def test_coordinate_transform(nside : int = 10, pitch : float = 0.1):
def test_coordinates(nside: int = 2, pitch: float = 0.1):
"""Test the full list of logical and physical coordinates that we use for
looping over the matrix.
"""
for layout in HexagonalLayout:
grid = HexagonalGrid(layout, nside, nside, pitch)
print(grid.pixel_logical_coordinates())
print(grid.pixel_physical_coordinates())

def test_coordinate_transform(nside: int = 10, pitch: float = 0.1):
"""Simple test of the coordinate transformations: we pick the four corner
pixels and verify that pixel_to_world() and word_to_pixel() roundtrip.
(This not really an exahustive test, and all the points are at the center
Expand All @@ -48,7 +57,7 @@ def test_coordinate_transform(nside : int = 10, pitch : float = 0.1):
x, y = grid.pixel_to_world(col, row)
assert grid.world_to_pixel(x, y) == (col, row)

def test_display(nside : int = 10, pitch : float = 0.1):
def test_display(nside: int = 10, pitch: float = 0.1):
"""Display all the four possible layout in a small arrangement.
"""
target_col = 5
Expand All @@ -69,7 +78,7 @@ def test_display(nside : int = 10, pitch : float = 0.1):
plt.scatter(x[mask], y[mask], color='b', s=4.)
display.setup_gca()

def test_neighbors(nside : int = 10, pitch : float = 0.1) -> None:
def test_neighbors(nside: int = 10, pitch: float = 0.1) -> None:
"""
"""
target_pixels = (2, 3), (7, 4)
Expand All @@ -87,9 +96,25 @@ def test_neighbors(nside : int = 10, pitch : float = 0.1) -> None:
plt.text(x, y, f'{i + 1}', **fmt)
display.setup_gca()

def test_routing_7(nside: int = 10, pitch: float = 0.1) -> None:
"""Test the routing from the pixel matrix to the ADCs on the periphery in the
7-pixel readout strategy.
"""
fmt = dict(size='xx-small', ha='center', va='center')
for layout in HexagonalLayout:
plt.figure(f'Hexagonal routing 7 {layout}')
grid = HexagonalGrid(layout, nside, nside, pitch)
display = HexagonalGridDisplay(grid)
display.draw(pixel_labels=False)
col, row, x, y = grid.pixel_physical_coordinates()
for (_col, _row, _x, _y) in zip(col, row, x, y):
plt.text(_x, _y, f'{_col + _row}', **fmt)
display.setup_gca()



if __name__ == '__main__':
test_display()
test_neighbors()
test_routing_7()
plt.show()

0 comments on commit e406e34

Please sign in to comment.