Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test(kernelcpd): exhaustive test for kernelcpd #108

Merged
merged 9 commits into from
Feb 1, 2021
Merged

Conversation

oboulant
Copy link
Collaborator

No description provided.

@oboulant oboulant marked this pull request as draft January 12, 2021 14:49
@github-actions github-actions bot added the Type: Testing Adding missing tests or correcting existing tests label Jan 12, 2021
@codecov
Copy link

codecov bot commented Jan 12, 2021

Codecov Report

Merging #108 (4890b03) into master (a9d1827) will increase coverage by 2.34%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #108      +/-   ##
==========================================
+ Coverage   92.77%   95.12%   +2.34%     
==========================================
  Files          41       41              
  Lines         955      964       +9     
==========================================
+ Hits          886      917      +31     
+ Misses         69       47      -22     
Impacted Files Coverage Δ
src/ruptures/detection/kernelcpd.py 100.00% <ø> (+33.92%) ⬆️
src/ruptures/detection/binseg.py 100.00% <100.00%> (ø)
src/ruptures/detection/window.py 100.00% <100.00%> (ø)
src/ruptures/utils/bnode.py 96.15% <100.00%> (+0.50%) ⬆️
src/ruptures/__init__.py 100.00% <0.00%> (ø)
src/ruptures/detection/bottomup.py 100.00% <0.00%> (+2.77%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9d1827...4890b03. Read the comment docs.

@oboulant
Copy link
Collaborator Author

I modified the tests for the detection part in order to take into account the model argument in the pytest.mark.parametrize.
It creates some fails. I fixed some. Some remain.
2 major things to adress :

  • For 1D constant signal test, the window detection algo returns a break points array of length 1 where it should be 2 (1 + the size of the signal). To be noted that when the signal is not constant, then the length of the returned array is fine
  • There are some warnings at runtime with numpy : RuntimeWarning: invalid value encountered in double_scalars. It is always with the normal cost and constant signal (for Binseg, BottomUp-normal and Window-normal. Pelt and Dynp seem to be fine in this setup with normal cost). Possibility to modify numpy behavior on warnings with old_settings = np.seterr(all='raise'). See here

@oboulant
Copy link
Collaborator Author

As for the runtime warnings and the Binseg case, it comes from the fact that we take the log of the det (see here). Since the cost is 0, then log(0)=-inf, which is fine as long as we are not trying to add something to it (actually we do -inf - (-inf) - (-inf) here.
I will proceed with some modifications in Binseg._single_bkp to add an early stop in case segment_cost = self.cost.error(start, end) is -inf end return the appropriate values (as of now, the returned values are wrong)

@oboulant
Copy link
Collaborator Author

To be checked but all the problems raised above might be solved.

@oboulant oboulant marked this pull request as ready for review January 18, 2021 14:59
@oboulant oboulant requested a review from deepcharles January 19, 2021 12:58
@deepcharles
Copy link
Owner

For 1D constant signal test, the window detection algo returns a break points array of length 1 where it should be 2 (1 + the size of the signal). To be noted that when the signal is not constant, then the length of the returned array is fine

To me, this is the correct behaviour: on constant signals, the returned list should be [n_samples]

@deepcharles
Copy link
Owner

deepcharles commented Jan 28, 2021

There are some warnings at runtime with numpy : RuntimeWarning: invalid value encountered in double_scalars. It is always with the normal cost and constant signal (for Binseg, BottomUp-normal and Window-normal. Pelt and Dynp seem to be fine in this setup with normal cost). Possibility to modify numpy behavior on warnings with old_settings = np.seterr(all='raise')

This is because it computes the inverse of the variance (so 1/0 because the signal is constant). To me, this is a normal behaviour that we should let as is.

EDIT: oh ok, I should have read your last comment.

@deepcharles
Copy link
Owner

I do not think that modifying the search method is the correct answer to this issue, because this problems really is about CostNormal. This imposes some restrictions (quite unlikely to be triggered I admit) on all future cost functions.
I would rather add an if loop in the cost function method .error(start, end). Something like

_, val = slogdet(cov)
if np.isinf(val) and val<0:
    return 0

@deepcharles
Copy link
Owner

I will chek what is the behaviour of search methods (other than windows) on constant signals.

@oboulant
Copy link
Collaborator Author

I do not think that modifying the search method is the correct answer to this issue, because this problems really is about CostNormal. This imposes some restrictions (quite unlikely to be triggered I admit) on all future cost functions.
I would rather add an if loop in the cost function method .error(start, end). Something like

_, val = slogdet(cov)
if np.isinf(val) and val<0:
    return 0

I do not think it would work, methodologically speaking. Here is an example where the result of such a implementation would be false :

X = np.ones((100,1))
X[52] = 1.5

my_normal_cost = CostNormal().fit(X)

error_constant = my_normal_cost.error(0, 50)
error_non_constant = my_normal_cost.error(50, 100)

We would end up with error_constant > error_non_constant (0 > -5.318520073865557), which sounds contrary to what a Cost() should implement in terms of the underlying properties.

WDYT ?

@deepcharles
Copy link
Owner

you are right, good catch. Then your solution is clearly the best one.

@deepcharles deepcharles merged commit 037b6c0 into master Feb 1, 2021
Copy link
Owner

@deepcharles deepcharles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@deepcharles deepcharles deleted the test-kernelcpd branch February 1, 2021 09:59
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Type: Testing Adding missing tests or correcting existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants