From 4436e2f33884c5488d7c4938b49e0a3af216fd9c Mon Sep 17 00:00:00 2001 From: BalzaniEdoardo Date: Fri, 20 Dec 2024 16:54:36 -0500 Subject: [PATCH 1/5] bugfix getitem with integer --- pynapple/core/time_series.py | 2 +- tests/test_time_series.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pynapple/core/time_series.py b/pynapple/core/time_series.py index 72d33d5a..7aa17e2e 100644 --- a/pynapple/core/time_series.py +++ b/pynapple/core/time_series.py @@ -1172,7 +1172,7 @@ def __getitem__(self, key, *args, **kwargs): if ( (len(index) == 1) and (output.ndim == 1) - and ((len(output) > 1) or isinstance(key[1], (list, np.ndarray))) + and ((len(output) > 1) or isinstance(key, int) or isinstance(key[1], (list, np.ndarray))) ): # reshape output of single index to preserve column axis if there are more than one columns being indexed # or if column key is a list or array diff --git a/tests/test_time_series.py b/tests/test_time_series.py index 0c5e9289..1580a73e 100755 --- a/tests/test_time_series.py +++ b/tests/test_time_series.py @@ -954,6 +954,7 @@ def test_interpolate_with_ep(self, tsd): @pytest.mark.parametrize( "tsdframe", [ + nap.TsdFrame(t=np.arange(100), d=np.random.rand(100, 1), time_units="s"), nap.TsdFrame(t=np.arange(100), d=np.random.rand(100, 3), time_units="s"), nap.TsdFrame( t=np.arange(100), From 84745e747d57cf2cd42bbd67ecb3c2038addf925 Mon Sep 17 00:00:00 2001 From: BalzaniEdoardo Date: Fri, 20 Dec 2024 16:56:24 -0500 Subject: [PATCH 2/5] linted --- pynapple/core/time_series.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pynapple/core/time_series.py b/pynapple/core/time_series.py index 7aa17e2e..9d640668 100644 --- a/pynapple/core/time_series.py +++ b/pynapple/core/time_series.py @@ -1172,7 +1172,11 @@ def __getitem__(self, key, *args, **kwargs): if ( (len(index) == 1) and (output.ndim == 1) - and ((len(output) > 1) or isinstance(key, int) or isinstance(key[1], (list, np.ndarray))) + and ( + (len(output) > 1) + or isinstance(key, int) + or isinstance(key[1], (list, np.ndarray)) + ) ): # reshape output of single index to preserve column axis if there are more than one columns being indexed # or if column key is a list or array From 2249d72e8676483da47ff1a238352ce773695404 Mon Sep 17 00:00:00 2001 From: BalzaniEdoardo Date: Fri, 20 Dec 2024 17:11:04 -0500 Subject: [PATCH 3/5] ignore nature that generates 406 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8c4f5482..5b0e8983 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -85,7 +85,7 @@ jobs: with: directory: "./doc/_build/html" # The directory to scan - arguments: --checks Links,Scripts --ignore-urls "https://fonts.gstatic.com,https://mkdocs-gallery.github.io,./doc/_build/html/_static/" --assume-extension --check-external-hash --ignore-status-codes 403 --ignore-files "/.+\/html\/_static\/.+/" + arguments: --checks Links,Scripts --ignore-urls "https://fonts.gstatic.com,https://mkdocs-gallery.github.io,./doc/_build/html/_static/,https://www.nature.com/articles/s41593-022-01020-w" --assume-extension --check-external-hash --ignore-status-codes 403 --ignore-files "/.+\/html\/_static\/.+/" # The arguments to pass to HTMLProofer From e866f221c78efc2009ef5ccbe359b09126257989 Mon Sep 17 00:00:00 2001 From: BalzaniEdoardo Date: Fri, 20 Dec 2024 18:53:13 -0500 Subject: [PATCH 4/5] fixed expectation --- pynapple/core/time_series.py | 2 +- tests/test_time_series.py | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pynapple/core/time_series.py b/pynapple/core/time_series.py index 9d640668..41a84888 100644 --- a/pynapple/core/time_series.py +++ b/pynapple/core/time_series.py @@ -1174,7 +1174,7 @@ def __getitem__(self, key, *args, **kwargs): and (output.ndim == 1) and ( (len(output) > 1) - or isinstance(key, int) + or isinstance(key, (int, slice)) or isinstance(key[1], (list, np.ndarray)) ) ): diff --git a/tests/test_time_series.py b/tests/test_time_series.py index 1580a73e..04948d88 100755 --- a/tests/test_time_series.py +++ b/tests/test_time_series.py @@ -998,6 +998,7 @@ def test_copy(self, tsdframe): ], ) def test_horizontal_slicing(self, tsdframe, index, nap_type): + index = index if isinstance(index, int) else index[: tsdframe.shape[1]] assert isinstance(tsdframe[:, index], nap_type) np.testing.assert_array_almost_equal( tsdframe[:, index].values, tsdframe.values[:, index] @@ -1050,10 +1051,10 @@ def test_vertical_slicing(self, tsdframe, index): "row", [ 0, - [0, 2], - slice(20, 30), - np.hstack([np.zeros(10, bool), True, True, True, np.zeros(87, bool)]), - np.hstack([np.zeros(10, bool), True, np.zeros(89, bool)]), + # [0, 2], + # slice(20, 30), + # np.hstack([np.zeros(10, bool), True, True, True, np.zeros(87, bool)]), + # np.hstack([np.zeros(10, bool), True, np.zeros(89, bool)]), ], ) @pytest.mark.parametrize( @@ -1068,6 +1069,15 @@ def test_vertical_slicing(self, tsdframe, index): ], ) def test_vert_and_horz_slicing(self, tsdframe, row, col, expected): + if tsdframe.shape[1] == 1: + if isinstance(col, list) and isinstance(col[0], int): + col = [0] + elif isinstance(col, slice): + col = slice(0, 1) + expected = nap.Tsd + elif isinstance(col, list) and isinstance(col[0], bool): + col = [col[0]] + # get details about row index row_array = isinstance(row, (list, np.ndarray)) if row_array and isinstance(row[0], (bool, np.bool_)): From c8d21f3e849ff2e378815ff4ec707066e1200415 Mon Sep 17 00:00:00 2001 From: BalzaniEdoardo Date: Sat, 21 Dec 2024 00:15:23 -0500 Subject: [PATCH 5/5] fix tests --- pynapple/core/time_series.py | 35 ++++++++++++++++------------------- tests/test_time_series.py | 11 ++++------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/pynapple/core/time_series.py b/pynapple/core/time_series.py index 41a84888..fbb3d9ee 100644 --- a/pynapple/core/time_series.py +++ b/pynapple/core/time_series.py @@ -1169,27 +1169,24 @@ def __getitem__(self, key, *args, **kwargs): index = np.array([index]) if all(is_array_like(a) for a in [index, output]): - if ( - (len(index) == 1) - and (output.ndim == 1) - and ( - (len(output) > 1) - or isinstance(key, (int, slice)) - or isinstance(key[1], (list, np.ndarray)) - ) - ): - # reshape output of single index to preserve column axis if there are more than one columns being indexed - # or if column key is a list or array + if isinstance(key, tuple): + if ( + len(index) == 1 + and output.ndim == 1 + and not isinstance(key[1], int) + ): + output = output[None, :] + elif ( + (output.ndim == 1) + and isinstance(key[1], (list, np.ndarray)) + and (len(columns) == 1) + ): + # reshape output of single column if column key is a list or array + output = output[:, None] + # if getting a row (1 dim implied) + elif isinstance(key, Number): output = output[None, :] - elif ( - (output.ndim == 1) - and isinstance(key[1], (list, np.ndarray)) - and (len(columns) == 1) - ): - # reshape output of single column if column key is a list or array - output = output[:, None] - kwargs["columns"] = columns kwargs["metadata"] = self._metadata.loc[columns] return _initialize_tsd_output( diff --git a/tests/test_time_series.py b/tests/test_time_series.py index 04948d88..354104ed 100755 --- a/tests/test_time_series.py +++ b/tests/test_time_series.py @@ -1051,10 +1051,10 @@ def test_vertical_slicing(self, tsdframe, index): "row", [ 0, - # [0, 2], - # slice(20, 30), - # np.hstack([np.zeros(10, bool), True, True, True, np.zeros(87, bool)]), - # np.hstack([np.zeros(10, bool), True, np.zeros(89, bool)]), + [0, 2], + slice(20, 30), + np.hstack([np.zeros(10, bool), True, True, True, np.zeros(87, bool)]), + np.hstack([np.zeros(10, bool), True, np.zeros(89, bool)]), ], ) @pytest.mark.parametrize( @@ -1072,9 +1072,6 @@ def test_vert_and_horz_slicing(self, tsdframe, row, col, expected): if tsdframe.shape[1] == 1: if isinstance(col, list) and isinstance(col[0], int): col = [0] - elif isinstance(col, slice): - col = slice(0, 1) - expected = nap.Tsd elif isinstance(col, list) and isinstance(col[0], bool): col = [col[0]]