From 33905eeaa08ac9d7d261381f062494241a0fb1c0 Mon Sep 17 00:00:00 2001 From: Dani Bodor Date: Wed, 8 Nov 2023 16:44:24 +0100 Subject: [PATCH] make `matrix_layout` a property --- eitprocessing/roi_selection/gridselection.py | 14 +++++++++----- tests/test_gridselection.py | 5 ++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/eitprocessing/roi_selection/gridselection.py b/eitprocessing/roi_selection/gridselection.py index 1a2ccc9c4..f77028c49 100644 --- a/eitprocessing/roi_selection/gridselection.py +++ b/eitprocessing/roi_selection/gridselection.py @@ -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; @@ -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]]) @@ -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] @@ -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): diff --git a/tests/test_gridselection.py b/tests/test_gridselection.py index 24f98bdb4..21b5a866b 100644 --- a/tests/test_gridselection.py +++ b/tests/test_gridselection.py @@ -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`. @@ -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))