Skip to content

Commit f51ddd7

Browse files
committed
fixed typo
1 parent a8ecda6 commit f51ddd7

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

nigsp/operations/metrics.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _fc(timeseries, mean=False):
248248

249249
@due.dcite(references.SHUMAN_2013)
250250
def smoothness(laplacian, signal):
251-
"""Compute the smoothness of a signal over the graph corresponding to given laplacian.
251+
"""Compute the smoothness of a signal (as defined in [1]) over the graph corresponding to given Laplacian.
252252
253253
Parameters
254254
----------
@@ -270,7 +270,31 @@ def smoothness(laplacian, signal):
270270
Processing Magazine, vol. 30, no. 3, pp. 83-98, May 2013
271271
"""
272272
LGR.info("Compute signal smoothness.")
273-
return np.dot(signal.T, np.dot(laplacian, signal))
273+
274+
# Checking shape of signal
275+
if signal.ndim == 1:
276+
LGR.warning(
277+
"2D array required, but signal is a vector. Adding empty dimension."
278+
)
279+
signal = np.expand_dims(signal, -1)
280+
elif signal.ndim > 2:
281+
raise ValueError("Signal should be a 2D array.")
282+
283+
# Checking if laplacian is square
284+
if laplacian.shape[0] != laplacian.shape[1]:
285+
raise ValueError("Laplacian should be a square matrix.")
286+
287+
# Checking that the dimensions of signal and laplacian are compatible
288+
if signal.shape[0] != laplacian.shape[0]:
289+
if signal.shape[1] == laplacian.shape[0]:
290+
LGR.warning(
291+
"It seems that the signal needs to be transposed to the correct shape."
292+
)
293+
signal = np.swapaxes(signal, 0, 1)
294+
else:
295+
raise ValueError("The dimensions of the signal and Laplacian don't match.")
296+
297+
return np.matmul(signal.T, np.matmul(laplacian, signal))
274298

275299

276300
"""

nigsp/tests/test_metrics.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ def test_gsdi():
7373

7474

7575
def test_smoothness():
76-
signal = rand(10, 1)
76+
signal = rand(10, 2)
7777
laplacian = rand(10, 10)
7878

7979
expected_smoothness = np.dot(signal.T, np.dot(laplacian, signal))
8080
computed_smoothness = metrics.smoothness(laplacian, signal)
8181

82-
assert np.isclose(expected_smoothness, computed_smoothness, rtol=1e-10)
82+
assert (expected_smoothness == computed_smoothness).all()
8383

8484

8585
# ### Break tests
@@ -134,3 +134,29 @@ def test_functional_connectivity():
134134

135135
for k in fcd.keys():
136136
assert (fcd[k] == np.corrcoef(tsd[k])).all()
137+
138+
139+
def test_break_smoothness():
140+
# shape of signal
141+
signal = rand(3, 3, 3)
142+
laplacian = rand(3, 3)
143+
144+
with raises(ValueError) as errorinfo:
145+
metrics.smoothness(laplacian, signal)
146+
assert "should be a 2D" in str(errorinfo.value)
147+
148+
# shape of laplacian
149+
signal = rand(10, 2)
150+
laplacian = rand(10, 9)
151+
152+
with raises(ValueError) as errorinfo:
153+
metrics.smoothness(laplacian, signal)
154+
assert "a square matrix" in str(errorinfo.value)
155+
156+
# shape mismatch
157+
signal = rand(10, 2)
158+
laplacian = rand(9, 9)
159+
160+
with raises(ValueError) as errorinfo:
161+
metrics.smoothness(laplacian, signal)
162+
assert "don't match" in str(errorinfo.value)

0 commit comments

Comments
 (0)