Skip to content

Commit

Permalink
fix: Allow Binseg to hit minsize bounds for segments (#249)
Browse files Browse the repository at this point in the history
* storing work to fix precommit and docs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: compare to the explicit results rather than lenght

* style: typos in comment

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Charles T <charles@doffy.net>
  • Loading branch information
3 people authored Apr 28, 2022
1 parent 5c6929a commit 939e7e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ruptures/detection/binseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def single_bkp(self, start, end):
return None, 0
gain_list = list()
for bkp in range(start, end, self.jump):
if bkp - start > self.min_size and end - bkp > self.min_size:
if bkp - start >= self.min_size and end - bkp >= self.min_size:
gain = (
segment_cost
- self.cost.error(start, bkp)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,17 @@ def test_model_small_signal(signal_bkps_5D_n10, algo, model):
)


@pytest.mark.parametrize(
"model",
["l1", "l2", "ar", "normal", "rbf", "rank", "mahalanobis"],
)
def test_binseg_min_size(signal_bkps_5D_n10, model):
signal, _ = signal_bkps_5D_n10

c_bkps = Binseg(model=model, min_size=5, jump=1).fit_predict(signal, n_bkps=1)
assert all([a == b for a, b in zip(c_bkps, [5, 10])])


@pytest.mark.parametrize(
"model", ["l1", "l2", "ar", "normal", "rbf", "rank", "mahalanobis"]
)
Expand All @@ -368,6 +379,9 @@ def test_model_small_signal_dynp(signal_bkps_5D_n10, model):
Dynp(model=model, min_size=9, jump=2).fit_predict(signal, 2)
with pytest.raises(BadSegmentationParameters):
Dynp(model=model, min_size=11, jump=2).fit_predict(signal, 2)
# Test if it can find the single eligible break point compatible with min_size
c_bkps = Dynp(model=model, min_size=5, jump=1).fit_predict(signal, 1)
assert all([a == b for a, b in zip(c_bkps, [5, 10])])


@pytest.mark.parametrize(
Expand Down

0 comments on commit 939e7e2

Please # to comment.