-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add smoothness metric #72
base: master
Are you sure you want to change the base?
Changes from all commits
3d4761b
af2c427
1ce603d
c1ed943
a8ecda6
f51ddd7
1b4d091
854c047
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
import numpy as np | ||
from numpy.random import rand | ||
from pytest import raises | ||
from pytest import raises, warns | ||
|
||
from nigsp.operations import metrics | ||
from nigsp.utils import prepare_ndim_iteration | ||
|
@@ -72,6 +72,25 @@ def test_gsdi(): | |
assert (gsdi_out["beta_over_alpha"] == gsdi_in).all() | ||
|
||
|
||
def test_smoothness(): | ||
s1 = rand(10) | ||
s2 = rand(10, 2) | ||
s3 = rand(2, 10) | ||
laplacian = rand(10, 10) | ||
|
||
expected_smoothness1 = np.dot(s1.T, np.dot(laplacian, s1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? If so you don't need a warning and a special treatment, since it's the same as the main case! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for s3 I need to transpose it by hand to compute the expected_smoothness otherwise the dot product won't work! See the difference between lines 81 and 84 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm talking about s1-s2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes that's correct sorry, I'm trying to make it work with a 3rd dimension for different subjects so I'll see how this evolves as I will probably move from np.matmul to np.tensordot There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure it'd fit in in memory, keep an eye on mem usage! You might have to run over subjects dim with a loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tensordot might indeed not work out - Maybe you can constrain axes in the multiplication, or indeed loop through subjects. I have some code in another function (the graph fourier transform, if I remember correctly) that split cases based on dimensions (and runs a loop). |
||
expected_smoothness2 = np.dot(s2.T, np.dot(laplacian, s2)) | ||
expected_smoothness3 = np.dot(s3, np.dot(laplacian, s3.T)) | ||
|
||
computed_smoothness1 = metrics.smoothness(laplacian, s1) | ||
computed_smoothness2 = metrics.smoothness(laplacian, s2) | ||
computed_smoothness3 = metrics.smoothness(laplacian, s3) | ||
|
||
assert (expected_smoothness1 == computed_smoothness1).all() | ||
assert (expected_smoothness2 == computed_smoothness2).all() | ||
assert (expected_smoothness3 == computed_smoothness3).all() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can add an assert here with Or use chatGPT 🤣 |
||
|
||
# ### Break tests | ||
def test_break_sdi(): | ||
ts1 = np.arange(1, 3)[..., np.newaxis] | ||
|
@@ -124,3 +143,29 @@ def test_functional_connectivity(): | |
|
||
for k in fcd.keys(): | ||
assert (fcd[k] == np.corrcoef(tsd[k])).all() | ||
|
||
|
||
def test_break_smoothness(): | ||
# shape of signal | ||
signal = rand(3, 3, 3) | ||
laplacian = rand(3, 3) | ||
|
||
with raises(ValueError) as errorinfo: | ||
metrics.smoothness(laplacian, signal) | ||
assert "should be a 2D" in str(errorinfo.value) | ||
|
||
# shape of laplacian | ||
signal = rand(10, 2) | ||
laplacian = rand(10, 9) | ||
|
||
with raises(ValueError) as errorinfo: | ||
metrics.smoothness(laplacian, signal) | ||
assert "a square matrix" in str(errorinfo.value) | ||
|
||
# shape mismatch | ||
signal = rand(10, 2) | ||
laplacian = rand(9, 9) | ||
|
||
with raises(ValueError) as errorinfo: | ||
metrics.smoothness(laplacian, signal) | ||
assert "don't match" in str(errorinfo.value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeaaaah... Do you remember when your function was one line? Now you understand the pain of the Sdev...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes definitely, thanks for the guidance and regular feedback!