From 2620c37b5f41478b3505ec637582802f3f6269ba Mon Sep 17 00:00:00 2001 From: mtorabi59 Date: Wed, 17 Jul 2024 12:10:29 -0400 Subject: [PATCH] add test_dfc --- pydfc/dfc.py | 12 ++- tests/test_dfc.py | 215 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/test_dfc.py diff --git a/pydfc/dfc.py b/pydfc/dfc.py index ee49cf0..e02e662 100644 --- a/pydfc/dfc.py +++ b/pydfc/dfc.py @@ -170,7 +170,7 @@ def dFC2dict(self, TRs=None): dFC_mat = self.get_dFC_mat(TRs=TRs) dFC_dict = {} for k, TR in enumerate(TRs): - dFC_dict["TR" + str(TR)] = dFC_mat[k, :, :] + dFC_dict[f"TR{TR}"] = dFC_mat[k, :, :] return dFC_dict # test this @@ -199,7 +199,7 @@ def get_dFC_mat(self, TRs=None, num_samples=None): dFC_mat = list() for TR in TRs: - dFC_mat.append(self.FCSs[self.FCS_idx["TR" + str(TR)]]) + dFC_mat.append(self.FCSs[self.FCS_idx[f"TR{TR}"]]) dFC_mat = np.array(dFC_mat) @@ -231,6 +231,10 @@ def SWed_dFC_mat(self, W=None, n_overlap=None, tapered_window=False): return dFC_mat_new def set_dFC(self, FCSs, FCS_idx=None, TS_info=None, TR_array=None): + """ + FCSs: a 3D numpy array of FC matrices with shape (n_time, n_regions, n_regions) + FCS_idx: a list of indices that correspond to each FC matrix in FCSs over time + """ if len(FCSs.shape) == 2: FCSs = np.expand_dims(FCSs, axis=0) @@ -267,11 +271,11 @@ def set_dFC(self, FCSs, FCS_idx=None, TS_info=None, TR_array=None): # the input FCS_idx is ranged from 0 to len(FCS)-1 but we shift it to 1 to len(FCS) self.FCSs_ = {} for i, FCS in enumerate(FCSs): - self.FCSs_["FCS" + str(i + 1)] = FCS + self.FCSs_[f"FCS{i + 1}"] = FCS self.FCS_idx_ = {} for i, idx in enumerate(FCS_idx): - self.FCS_idx_["TR" + str(TR_array[i])] = "FCS" + str(idx + 1) + self.FCS_idx_[f"TR{TR_array[i]}"] = f"FCS{idx + 1}" # "FCS" + str(idx + 1) self.TS_info_ = TS_info self.n_regions_ = FCSs.shape[1] diff --git a/tests/test_dfc.py b/tests/test_dfc.py new file mode 100644 index 0000000..b3a7761 --- /dev/null +++ b/tests/test_dfc.py @@ -0,0 +1,215 @@ +import numpy as np + +from pydfc import DFC + + +def test_create_dFC_state_based(): + ## STATE BASED ## + # create 5 FCSs with 3 regions representing 5 states + FCSs = np.array( + [ + [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]], + [[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]], + [[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]], + [[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]], + [[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]], + ] + ) + # create a random FCS_idx with 10 time points and 5 states + FCS_idx = np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4]) + + dfc = DFC() + dfc.set_dFC(FCSs=FCSs, FCS_idx=FCS_idx) + + assert dfc.FCSs.keys() == {"FCS1", "FCS2", "FCS3", "FCS4", "FCS5"} + assert np.all( + dfc.FCSs["FCS1"] == np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) + ) + assert np.all( + dfc.FCSs["FCS2"] + == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dfc.FCSs["FCS3"] + == np.array([[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]]) + ) + assert np.all( + dfc.FCSs["FCS4"] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dfc.FCSs["FCS5"] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + + assert dfc.FCS_idx.keys() == { + "TR0", + "TR1", + "TR2", + "TR3", + "TR4", + "TR5", + "TR6", + "TR7", + "TR8", + "TR9", + } + assert dfc.FCS_idx["TR0"] == "FCS1" + assert dfc.FCS_idx["TR1"] == "FCS2" + assert dfc.FCS_idx["TR2"] == "FCS3" + assert dfc.FCS_idx["TR3"] == "FCS4" + assert dfc.FCS_idx["TR4"] == "FCS5" + assert dfc.FCS_idx["TR5"] == "FCS1" + assert dfc.FCS_idx["TR6"] == "FCS2" + assert dfc.FCS_idx["TR7"] == "FCS3" + assert dfc.FCS_idx["TR8"] == "FCS4" + assert dfc.FCS_idx["TR9"] == "FCS5" + + assert dfc.n_regions == 3 + assert dfc.n_time == 10 + assert np.all(dfc.state_TC() == np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])) + + dFC_mat = dfc.get_dFC_mat() + assert dFC_mat.shape == (10, 3, 3) + assert np.all( + dFC_mat[0] == np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) + ) + assert np.all( + dFC_mat[1] == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_mat[2] == np.array([[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]]) + ) + assert np.all( + dFC_mat[3] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_mat[4] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + assert np.all( + dFC_mat[5] == np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) + ) + assert np.all( + dFC_mat[6] == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_mat[7] == np.array([[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]]) + ) + assert np.all( + dFC_mat[8] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_mat[9] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + + dFC_mat = dfc.get_dFC_mat(TRs=[1, 3, 8]) + assert dFC_mat.shape == (3, 3, 3) + assert np.all( + dFC_mat[0] == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_mat[1] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_mat[2] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + + dFC_dict = dfc.dFC2dict(TRs=[1, 3, 8]) + assert dFC_dict.keys() == {"TR1", "TR3", "TR8"} + assert np.all( + dFC_dict["TR1"] + == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_dict["TR3"] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_dict["TR8"] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + + +def test_create_dFC_state_free(): + ## STATE FREE ## + # create 5 FCSs with 3 regions corresponding to 5 time points + FCSs = np.array( + [ + [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]], + [[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]], + [[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]], + [[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]], + [[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]], + ] + ) + + dfc = DFC() + dfc.set_dFC(FCSs=FCSs) + + assert dfc.FCSs.keys() == {"FCS1", "FCS2", "FCS3", "FCS4", "FCS5"} + assert np.all( + dfc.FCSs["FCS1"] == np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) + ) + assert np.all( + dfc.FCSs["FCS2"] + == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dfc.FCSs["FCS3"] + == np.array([[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]]) + ) + assert np.all( + dfc.FCSs["FCS4"] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dfc.FCSs["FCS5"] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + + assert dfc.FCS_idx.keys() == {"TR0", "TR1", "TR2", "TR3", "TR4"} + assert dfc.FCS_idx["TR0"] == "FCS1" + assert dfc.FCS_idx["TR1"] == "FCS2" + assert dfc.FCS_idx["TR2"] == "FCS3" + assert dfc.FCS_idx["TR3"] == "FCS4" + assert dfc.FCS_idx["TR4"] == "FCS5" + + assert dfc.n_regions == 3 + assert dfc.n_time == 5 + + dFC_mat = dfc.get_dFC_mat() + assert dFC_mat.shape == (5, 3, 3) + assert np.all( + dFC_mat[0] == np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]) + ) + assert np.all( + dFC_mat[1] == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_mat[2] == np.array([[0.3, 0.4, 0.5], [0.6, 0.7, 0.8], [-0.9, 0.1, -0.2]]) + ) + assert np.all( + dFC_mat[3] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_mat[4] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + + dFC_mat = dfc.get_dFC_mat(TRs=[1, 3, 4]) + assert dFC_mat.shape == (3, 3, 3) + assert np.all( + dFC_mat[0] == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_mat[1] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_mat[2] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + ) + + dFC_dict = dfc.dFC2dict(TRs=[1, 3, 4]) + assert dFC_dict.keys() == {"TR1", "TR3", "TR4"} + assert np.all( + dFC_dict["TR1"] + == np.array([[0.2, 0.3, 0.4], [-0.5, -0.6, -0.7], [0.8, 0.9, 0.1]]) + ) + assert np.all( + dFC_dict["TR3"] == np.array([[0.4, 0.5, 0.6], [-0.7, 0.8, 0.9], [0.1, 0.2, 0.3]]) + ) + assert np.all( + dFC_dict["TR4"] == np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 0.1], [0.2, 0.3, 0.4]]) + )