Skip to content
Merged
32 changes: 32 additions & 0 deletions nigsp/operations/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ def normalise_ts(timeseries):
return z


def spc_ts(timeseries):
"""
Express timeseries in signal percentage change.

It is assumed that time is encoded in the second dimension (axis 1),
e.g. for 90 voxels and 300 timepoints, shape is [90, 300].

Parameters
----------
timeseries : numpy.ndarray
The input timeseries. It is assumed that the second dimension is time.

Returns
-------
numpy.ndarray
The timeseries in SPC if timeseries is not a 1D array.
If timeseries is a 1D array, it is returned as is.
"""
if timeseries.ndim < 2 or (timeseries.ndim == 2 and timeseries.shape[1] == 1):
LGR.warning(
"Given timeseries seems to be a single timepoint. " "Returning it as is."
)
return timeseries

scp = (timeseries - timeseries.mean(axis=1)[:, np.newaxis, ...]) / timeseries.mean(
axis=1
)[:, np.newaxis, ...]
scp[np.isnan(scp)] = timeseries[np.isnan(scp)]

return scp


def graph_fourier_transform(timeseries, eigenvec, energy=False, mean=False):
"""
Projet a graph decomposition onto the timeseries.
Expand Down
6 changes: 3 additions & 3 deletions nigsp/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def test_integration(timeseries, sc_mtx, atlas, mean_fc, sdi, testdir):
assert isfile(join(testdir, "testfile_fc.png"))
assert isfile(join(testdir, "testfile_fc_low.png"))
assert isfile(join(testdir, "testfile_fc_high.png"))
assert isfile(join(testdir, "testfile_grayplot.png"))
assert isfile(join(testdir, "testfile_grayplot_low.png"))
assert isfile(join(testdir, "testfile_grayplot_high.png"))
assert isfile(join(testdir, "testfile_greyplot.png"))
assert isfile(join(testdir, "testfile_greyplot_low.png"))
assert isfile(join(testdir, "testfile_greyplot_high.png"))
assert isfile(join(testdir, "testfile_sdi.png"))
assert isfile(join(testdir, "testfile_mkd_sdi.png"))

Expand Down
66 changes: 59 additions & 7 deletions nigsp/tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def test_plot_connectivity(mtx):


@mark.parametrize("timeseries", [(rand(3, 50)), (rand(3, 50, 3)), (rand(3, 50, 4, 1))])
def test_plot_grayplot(timeseries):
viz.plot_grayplot(timeseries, "troy.png", closeplot=True)
def test_plot_greyplot(timeseries):
viz.plot_greyplot(timeseries, "troy.png", closeplot=True)

assert isfile("troy.png")
matplotlib.pyplot.close()
Expand All @@ -46,31 +46,48 @@ def test_plot_nodes(sdi, atlas):
remove(atlas)


def test_plot_edges(atlas):
mtx = rand(360, 360)
mtx = mtx - mtx.min() + 0.3
viz.plot_edges(mtx, atlas, "britta.png", thr="95%", closeplot=True)

assert isfile("britta.png")
matplotlib.pyplot.close()
remove("britta.png")
remove(atlas)


# ### Break tests
def test_break_plot_connectivity():
sys.modules["matplotlib"] = None
with raises(ImportError) as errorinfo:
viz.plot_connectivity(rand(3, 3), "craig.png")
assert "is required" in str(errorinfo.value)
assert "are required" in str(errorinfo.value)
sys.modules["matplotlib"] = matplotlib

sys.modules["nilearn.plotting"] = None
with raises(ImportError) as errorinfo:
viz.plot_connectivity(rand(3, 3), "craig.png")
assert "are required" in str(errorinfo.value)
sys.modules["nilearn.plotting"] = nilearn.plotting

with raises(ValueError) as errorinfo:
viz.plot_connectivity(rand(3, 3, 3, 4), "craig.png")
assert "plot connectivity" in str(errorinfo.value)

matplotlib.pyplot.close()


def test_break_plot_grayplot():
def test_break_plot_greyplot():
sys.modules["matplotlib"] = None
with raises(ImportError) as errorinfo:
viz.plot_grayplot(rand(3, 3), "dan.png")
viz.plot_greyplot(rand(3, 3), "dan.png")
assert "is required" in str(errorinfo.value)
sys.modules["matplotlib"] = matplotlib

with raises(ValueError) as errorinfo:
viz.plot_grayplot(rand(3, 3, 3, 4), "dan.png")
assert "plot grayplots" in str(errorinfo.value)
viz.plot_greyplot(rand(3, 3, 3, 4), "dan.png")
assert "plot greyplots" in str(errorinfo.value)

matplotlib.pyplot.close()

Expand Down Expand Up @@ -105,3 +122,38 @@ def test_break_plot_nodes(atlas):

matplotlib.pyplot.close()
remove(atlas)


def test_break_plot_edges(atlas):
sys.modules["matplotlib"] = None
with raises(ImportError) as errorinfo:
viz.plot_edges(rand(3), rand(3, 3))
assert "are required" in str(errorinfo.value)
sys.modules["matplotlib"] = matplotlib

sys.modules["nilearn.plotting"] = None
with raises(ImportError) as errorinfo:
viz.plot_edges(rand(3), rand(3, 3))
assert "are required" in str(errorinfo.value)
sys.modules["nilearn.plotting"] = nilearn.plotting

with raises(ValueError) as errorinfo:
viz.plot_edges(rand(3, 3, 4, 5), rand(3, 3))
assert "plot node values" in str(errorinfo.value)

with raises(NotImplementedError) as errorinfo:
viz.plot_edges(rand(3), rand(3, 3, 2))
assert "atlases in nifti" in str(errorinfo.value)
with raises(NotImplementedError) as errorinfo:
viz.plot_edges(rand(3), rand(3, 4))
assert "atlases in nifti" in str(errorinfo.value)

with raises(ValueError) as errorinfo:
viz.plot_edges(rand(3), rand(4, 3))
assert "different length" in str(errorinfo.value)

matplotlib.pyplot.close()
remove(atlas)


# ..and a movie!!!
Loading