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

Consistently use the test sets as reference for gap_before and gap_after #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion tscv/_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class GapCrossValidator(metaclass=ABCMeta):
Implementations must define one of the following 4 methods:
`_iter_train_indices`, `_iter_train_masks`,
`_iter_test_indices`, `_iter_test_masks`.

Parameters
----------
gap_before : int, default=0
Gap before the test sets.

gap_after : int, default=0
Gap after the test sets.
"""

def __init__(self, gap_before=0, gap_after=0):
Expand Down Expand Up @@ -106,6 +114,8 @@ def split(self, X, y=None, groups=None):

# Since subclasses implement any of the following 4 methods,
# none can be abstract.
# _iter_train_indices <- _iter_test_indices <-
# _iter_test_masks <- _iter_train_masks <- _iter_train_indices
def _iter_train_indices(self, X=None, y=None, groups=None):
"""Generates integer indices corresponding to training sets.

Expand Down Expand Up @@ -151,7 +161,8 @@ def __indices_to_masks(indices, n_samples):
yield mask

def __complement_masks(self, masks):
before, after = self.gap_before, self.gap_after
# switch gap_before and gap_after because of different viewpoints
before, after = self.gap_after, self.gap_before
for mask in masks:
complement = np.ones(len(mask), dtype=np.bool_)
for i, masked in enumerate(mask):
Expand Down
12 changes: 6 additions & 6 deletions tscv/tests/test_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def _iter_train_indices(self, X=None, y=None, groups=None):
masks = cv._GapCrossValidator__complement_masks(
[[False, True, True, True, False, False],
[False, False, False, False, False, True]])
assert_array_equal(next(masks), [True, False, False, False, False, False])
assert_array_equal(next(masks), [True, True, True, True, True, False])
assert_array_equal(next(masks), [False, False, False, False, True, True])
assert_array_equal(next(masks), [True, True, True, False, False, False])

indices = cv._GapCrossValidator__complement_indices([[1, 2, 3], [5]], 7)
assert_array_equal(next(indices), [0, 6])
Expand All @@ -73,12 +73,12 @@ def _iter_train_indices(self, X=None, y=None, groups=None):
assert_array_equal(next(masks), [False, False, True, False, True])

masks = cv._iter_test_masks("abcde")
assert_array_equal(next(masks), [True, False, False, False, False])
assert_array_equal(next(masks), [True, True, False, False, False])
assert_array_equal(next(masks), [False, False, False, False, True])
assert_array_equal(next(masks), [False, False, False, False, False])

indices = cv._iter_test_indices("abcde")
assert_array_equal(next(indices), [0])
assert_array_equal(next(indices), [0, 1])
assert_array_equal(next(indices), [4])
assert_array_equal(next(indices), [])

# Another dummy subclass
class test2CV(GapCrossValidator):
Expand Down