Skip to content

Commit

Permalink
make matrix_layout a property
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBodor committed Nov 22, 2023
1 parent 4f78ebb commit 33905ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 9 additions & 5 deletions eitprocessing/roi_selection/gridselection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class GridSelection(ROISelection):
`split_columns` has the same effect on columns as `split_rows` has on rows.
Regions are ordered according to C indexing order. The `matrix_layout()` method provides a map
showing how the regions are ordered.
Regions are ordered according to C indexing order. The `matrix_layout` attribute provides a map
showing how these regions are ordered.
Common grids are pre-defined:
- VentralAndDorsal: vertically divided into ventral and dorsal;
Expand All @@ -66,7 +66,7 @@ class GridSelection(ROISelection):
[16, 17, 18]])
>>> gs = GridSelection(3, 1, split_rows=False)
>>> rois = gs.find_grid(pixel_map)
>>> gs.matrix_layout()
>>> gs.matrix_layout
array([[0],
[1],
[2]])
Expand All @@ -86,7 +86,7 @@ class GridSelection(ROISelection):
[0, 0, 0]])
>>> gs2 = GridSelection(2, 2, split_columns=True)
>>> rois2 = gs.find_grid(pixel_map)
>>> gs2.matrix_layout()
>>> gs2.matrix_layout
array([[0, 1],
[2, 3]])
>>> rois2[2]
Expand Down Expand Up @@ -316,11 +316,15 @@ def _create_grouping_vector_split_pixels( # pylint: disable=too-many-locals

return final

def matrix_layout(self) -> NDArray:
@property
def _matrix_layout(self) -> NDArray:
"""Returns a 2D array showing the layout of the matrices returned by
`find_grid`."""
n_regions = self.v_split * self.h_split
return np.reshape(np.arange(n_regions), (self.v_split, self.h_split))
@_matrix_layout.getter # private attribute with getter avoids users overriding this property
def matrix_layout(self):
return self._matrix_layout


class InvalidDivision(Exception):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_gridselection.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def test_split_pixels_nans(data_string, split_vh, result):
)
def test_matrix_layout(split_vh: tuple[int, int], result: list[list[int]]):
"""
Test `matrix_layout()` method.
Test `matrix_layout` method.
Args:
split_vh (tuple[int, int]): `v_split` and `h_split`.
Expand All @@ -474,6 +474,5 @@ def test_matrix_layout(split_vh: tuple[int, int], result: list[list[int]]):
"""

gs = GridSelection(*split_vh)
layout = gs.matrix_layout()

assert np.array_equal(layout, np.array(result))
assert np.array_equal(gs.matrix_layout, np.array(result))

0 comments on commit 33905ee

Please sign in to comment.