Esempio n. 1
0
    def test_dataset_hcp(self):
        ds = Dataset("hcp")
        ds = Dataset("hcp", normalizeCmats="waytotal")
        ds = Dataset("hcp", normalizeCmats="nvoxel")

        self.assertTupleEqual(ds.Cmat.shape, (80, 80))
        self.assertTupleEqual(ds.Dmat.shape, (80, 80))
        self.assertTupleEqual(ds.FCs[0].shape, (80, 80))
        self.assertTrue(len(ds.FCs) == len(ds.BOLDs))
Esempio n. 2
0
    def setUpClass(cls):
        ds = Dataset("gw")
        aln = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)

        # Resting state fits
        aln.params["mue_ext_mean"] = 1.57
        aln.params["mui_ext_mean"] = 1.6
        aln.params["sigma_ou"] = 0.09
        aln.params["b"] = 5.0
        aln.params["duration"] = 0.2 * 60 * 1000
        aln.run(bold=True, chunkwise=True)

        cls.model = aln
        cls.ds = Dataset("gw")
Esempio n. 3
0
    def test_functional_connectivity(self):
        # assert log warning was issued
        root_logger = logging.getLogger()
        with self.assertLogs(root_logger, level="ERROR") as cm:
            fcs = self.signal.functional_connectivity()
            self.assertEqual(
                cm.output,
                [
                    "ERROR:root:Cannot compute functional connectivity from one timeseries."
                ],
            )
        # should be None when computing on one timeseries
        self.assertEqual(None, fcs)

        # now proper example with network - 3D case
        ds = Dataset("gw")
        aln = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        # in ms, simulates for 2 minutes
        aln.params["duration"] = 2 * 1000
        aln.run()
        network_sig = RatesSignal.from_model_output(aln)
        fcs = network_sig.functional_connectivity()
        self.assertTrue(isinstance(fcs, xr.DataArray))
        correct_shape = (network_sig.shape[0], network_sig.shape[1],
                         network_sig.shape[1])
        self.assertTupleEqual(fcs.shape, correct_shape)

        # 2D case
        fc = network_sig["rates_exc"].functional_connectivity()
        self.assertTrue(isinstance(fc, xr.DataArray))
        correct_shape = (network_sig.shape[1], network_sig.shape[1])
        self.assertTupleEqual(fc.shape, correct_shape)
Esempio n. 4
0
    def setUpClass(cls):
        ds = Dataset("hcp")
        model = FHNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        model.params.duration = 10 * 1000  # ms
        model.params.dt = 0.05
        model.params.bold = True
        parameters = ParameterSpace(
            {
                "x_ext": [
                    np.ones((model.params["N"], )) * a
                    for a in np.linspace(0, 2, 2)
                ],
                "K_gl":
                np.linspace(0, 2, 2),
                "coupling": ["additive", "diffusive"],
            },
            kind="grid",
        )
        search = BoxSearch(
            model=model,
            parameterSpace=parameters,
            filename=f"test_exploration_utils_{randomString(20)}.hdf")

        search.run(chunkwise=True, bold=True)

        search.loadResults()
        # flatten x_ext parameter
        search.dfResults.x_ext = [a[0] for a in list(search.dfResults.x_ext)]

        cls.model = model
        cls.search = search
        cls.ds = ds
Esempio n. 5
0
    def test_fhn_brain_network_exploration(self):
        ds = Dataset("hcp")
        model = FHNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        model.params.duration = 10 * 1000  # ms
        model.params.dt = 0.2
        model.params.bold = True
        parameters = ParameterSpace(
            {
                "x_ext": [
                    np.ones((model.params["N"], )) * a
                    for a in np.linspace(0, 2, 2)
                ],
                "K_gl":
                np.linspace(0, 2, 2),
                "coupling": ["additive", "diffusive"],
            },
            kind="grid",
        )
        search = BoxSearch(model=model,
                           parameterSpace=parameters,
                           filename="test_fhn_brain_network_exploration.hdf")

        search.run(chunkwise=True, bold=True)

        pu.getTrajectorynamesInFile(
            os.path.join(paths.HDF_DIR,
                         "test_fhn_brain_network_exploration.hdf"))
        search.loadDfResults()
        search.getRun(0, pypetShortNames=True)
        search.getRun(0, pypetShortNames=False)
        search.loadResults()
        search.info()
    def setUpClass(cls):
        ds = Dataset("hcp")
        model = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        model.params.duration = 11 * 1000  # ms
        model.params.dt = 0.2
        parameters = ParameterSpace(
            {
                "mue_ext_mean": np.linspace(0, 3, 2),
                "mui_ext_mean": np.linspace(0, 3, 2),
                "b": [0.0, 10.0],
            },
            kind="grid",
        )
        search = BoxSearch(
            model=model,
            parameterSpace=parameters,
            filename=f"test_exploration_utils_{randomString(20)}.hdf")

        search.run(chunkwise=True, bold=True)

        search.loadResults()

        cls.model = model
        cls.search = search
        cls.ds = ds
Esempio n. 7
0
    def test_network(self):
        logging.info("\t > WC: Testing brain network (chunkwise integration and BOLD simulation) ...")
        start = time.time()
        ds = Dataset("gw")
        wc = WCModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        wc.params["signalV"] = 4.0
        wc.params["duration"] = 10 * 1000
        wc.params["sigma_ou"] = 0.1
        wc.params["K_gl"] = 0.6
        wc.params["x_ext_mean"] = 0.72

        wc.run(chunkwise=True, bold=True, append_outputs=True)
        end = time.time()
        logging.info("\t > Done in {:.2f} s".format(end - start))
Esempio n. 8
0
    def test_network(self):
        logging.info("\t > Hopf: Testing brain network (chunkwise integration and BOLD" " simulation) ...")
        start = time.time()
        ds = Dataset("gw")
        hopf = HopfModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        hopf.params["w"] = 1.0
        hopf.params["signalV"] = 0
        hopf.params["duration"] = 10 * 1000
        hopf.params["sigma_ou"] = 0.14
        hopf.params["K_gl"] = 0.6

        hopf.run(chunkwise=True, bold=True, append_outputs=True)

        end = time.time()
        logging.info("\t > Done in {:.2f} s".format(end - start))
Esempio n. 9
0
    def test_check_chunkwise(self):
        """Full test of chunkwise integration over all models
        """
        ds = Dataset("hcp")
        Models = [ALNModel, FHNModel, HopfModel, WCModel]
        durations = [0.1, 0.5, 10.5, 22.3]
        chunksizes = [1, 5, 7, 33, 55, 123]
        modes = ["single", "network"]
        signalVs = [0, 1, 10, 10000]

        plot = False
        for mode in modes:
            for Model in Models:
                for duration in durations:
                    for chunksize in chunksizes:
                        for signalV in signalVs:
                            if mode == "network":
                                m1 = Model(Cmat=ds.Cmat, Dmat=ds.Dmat)
                            else:
                                m1 = Model()
                            m1.params.signalV = signalV
                            m1.params["duration"] = duration
                            pars_bak = copy.deepcopy(m1.params)

                            m1.run()

                            if mode == "network":
                                m2 = Model(Cmat=ds.Cmat, Dmat=ds.Dmat)
                            else:
                                m2 = Model()
                            m2.params = pars_bak.copy()
                            m2.run(chunkwise=True,
                                   chunksize=chunksize,
                                   append=True)

                            assert (
                                m1.output.shape == m2.output.shape
                            ), "Shape of chunkwise output does not match normal output!"
                            difference = np.sum(abs(m1.output - m2.output))
                            assert (
                                difference == 0
                            ), f"difference: {difference} > Model: {Model.name}, Mode: {mode}, signalV: {signalV}, Chunksize: {chunksize}, Duration: {duration}"
                            if difference > 0:
                                logging.info(
                                    f"difference: {difference} > Model: {Model.name}, Mode: {mode}, signalV: {signalV}, Chunksize: {chunksize}, Duration: {duration}"
                                )
Esempio n. 10
0
    def test_network(self):
        logging.info("\t > ALN: Testing brain network (chunkwise integration and BOLD" " simulation) ...")
        start = time.time()

        ds = Dataset("gw")

        aln = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)

        aln.params["duration"] = 10 * 1000

        aln.run(chunkwise=True, bold=True, append_outputs=True)

        # access outputs
        aln.xr()
        aln.xr("BOLD")

        end = time.time()
        logging.info("\t > Done in {:.2f} s".format(end - start))
Esempio n. 11
0
 def network_test(self, model):
     ds = Dataset("hcp")
     for duration in self.durations:
         for chunksize in self.chunksizes:
             for signalV in self.signalVs:
                 # full run
                 m1 = model(Cmat=ds.Cmat, Dmat=ds.Dmat)
                 m1.params.signalV = signalV
                 m1.params["duration"] = duration
                 pars_bak = copy.deepcopy(m1.params)
                 m1.run()
                 # chunkwise run
                 m2 = model(Cmat=ds.Cmat, Dmat=ds.Dmat)
                 m2.params = pars_bak.copy()
                 m2.run(chunkwise=True, chunksize=chunksize, append=True)
                 # check
                 self.assertTupleEqual(m1.output.shape, m2.output.shape)
                 difference = np.sum(np.abs(m1.output - m2.output))
                 self.assertAlmostEqual(difference, 0.0)
Esempio n. 12
0
    def setUpClass(cls):
        # def test_brain_network_postprocessing(self):
        ds = Dataset("hcp")
        model = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat)
        # Resting state fits
        model.params["mue_ext_mean"] = 1.57
        model.params["mui_ext_mean"] = 1.6
        model.params["sigma_ou"] = 0.09
        model.params["b"] = 5.0
        model.params["signalV"] = 2
        model.params["dt"] = 0.2
        model.params["duration"] = 0.2 * 60 * 1000

        # multi stage evaluation function
        def evaluateSimulation(traj):
            model = search.getModelFromTraj(traj)
            model.randomICs()
            model.params["dt"] = 0.2
            model.params["duration"] = 4 * 1000.0
            model.run(bold=True)

            result_dict = {"outputs": model.outputs}

            search.saveToPypet(result_dict, traj)

        # define and run exploration
        parameters = ParameterSpace({
            "mue_ext_mean": np.linspace(0, 3, 2),
            "mui_ext_mean": np.linspace(0, 3, 2)
        })
        search = BoxSearch(
            evalFunction=evaluateSimulation,
            model=model,
            parameterSpace=parameters,
            filename=f"test_brain_postprocessing_{randomString(20)}.hdf",
        )
        search.run()
        cls.model = model
        cls.search = search
        cls.ds = ds
Esempio n. 13
0
    def test_brain_network(self):
        from neurolib.utils.loadData import Dataset

        ds = Dataset("hcp")
        aln = ALNModel(Cmat=ds.Cmat, Dmat=ds.Dmat, bold=True)
        # Resting state fits
        aln.params["mue_ext_mean"] = 1.57
        aln.params["mui_ext_mean"] = 1.6
        aln.params["sigma_ou"] = 0.09
        aln.params["b"] = 5.0
        aln.params["signalV"] = 2
        aln.params["dt"] = 0.2
        aln.params["duration"] = 0.2 * 60 * 1000

        # multi stage evaluation function
        def evaluateSimulation(traj):
            model = search.getModelFromTraj(traj)
            defaultDuration = model.params["duration"]
            invalid_result = {"fc": [0] * len(ds.BOLDs)}

            # -------- stage wise simulation --------

            # Stage 1 : simulate for a few seconds to see if there is any activity
            # ---------------------------------------
            model.params["dt"] = 0.1
            model.params["duration"] = 3 * 1000.0
            model.run()

            # check if stage 1 was successful
            if np.max(model.rates_exc[:, model.t > 500]) > 300 or np.max(
                    model.rates_exc[:, model.t > 500]) < 10:
                search.saveOutputsToPypet(invalid_result, traj)
                return invalid_result, {}

            # Stage 2: simulate BOLD for a few seconds to see if it moves
            # ---------------------------------------
            model.params["dt"] = 0.2
            model.params["duration"] = 20 * 1000.0
            model.run(bold=True)

            if np.std(model.BOLD.BOLD[:, 5:10]) < 0.001:
                search.saveOutputsToPypet(invalid_result, traj)
                return invalid_result, {}

            # Stage 3: full and final simulation
            # ---------------------------------------
            model.params["dt"] = 0.2
            model.params["duration"] = defaultDuration
            model.run()

            # -------- evaluation here --------

            scores = []
            for i, fc in enumerate(ds.FCs):  # range(len(ds.FCs)):
                fc_score = func.matrix_correlation(
                    func.fc(model.BOLD.BOLD[:, 5:]), fc)
                scores.append(fc_score)

            meanScore = np.mean(scores)
            result_dict = {"fc": meanScore}

            search.saveOutputsToPypet(result_dict, traj)

        # define and run exploration
        parameters = ParameterSpace({
            "mue_ext_mean": np.linspace(0, 3, 2),
            "mui_ext_mean": np.linspace(0, 3, 2)
        })
        search = BoxSearch(evalFunction=evaluateSimulation,
                           model=aln,
                           parameterSpace=parameters)
        search.run()
Esempio n. 14
0
    mats = ['Cmat', 'Dmat', 'backbone']
    # graph_properties = ['omega', 'sigma']

    n_subjects = df.shape[0]
    df.loc[:, 'subject'] = df.index
    for m in m_dist:
        value = np.array([df.loc[i, m] for i in range(n_subjects)])
        for s in range(n_subjects):
            corr = np.corrcoef(value[0, :], value[s, :])[0, 1]
            df.loc[s, f'{m}_corr'] = corr
    for m in mats:
        for s in range(n_subjects):
            corr = np.corrcoef(df.loc[0, m].flatten(),
                               df.loc[s, m].flatten())[0, 1]
            df.loc[s, f'{m}_corr'] = corr
    # for m in graph_properties:
    #    value = np.array([df.loc[i, m] for i in range(n_subjects)])
    #    for s in range(n_subjects):
    #        corr = np.corrcoef(value[0], value[s])[0, 1]
    #        df.loc[s, f'{m}_corr'] = corr

    # + graph_properties]
    all_values = ['subject'] + [f'{m}_corr' for m in mats + m_dist]
    return df.loc[:, all_values]


if __name__ == '__main__':
    from neurolib.utils.loadData import Dataset
    ds = Dataset("gw")
    G = make_graph(ds.Cmats[1])
Esempio n. 15
0
 def test_dataset_gw(self):
     ds = Dataset("gw", fcd=True)
     self.assertTupleEqual(ds.Cmat.shape, (80, 80))
     self.assertTupleEqual(ds.Dmat.shape, (80, 80))
     self.assertTupleEqual(ds.FCs[0].shape, (80, 80))
     self.assertTrue(len(ds.FCs) == len(ds.BOLDs))