示例#1
0
 def __init__(self,
              model_name="",
              model=None,
              model_code=None,
              model_dir="",
              model_code_path="",
              model_data_path="",
              fitmethod="sampling",
              config=None):
     self.fitmethod = fitmethod
     self.model_name = model_name
     self.model = model
     self.config = config or Config()
     if not os.path.isdir(model_dir):
         model_dir = config.out.FOLDER_RES
     if not (os.path.isdir(model_dir)):
         os.mkdir(model_dir)
     self.model_path = os.path.join(model_dir, self.model_name)
     self.model_code = model_code
     if os.path.isfile(model_code_path):
         self.model_code_path = model_code_path
     else:
         self.model_code_path = self.model_path + ".stan"
     if model_data_path == "":
         self.model_data_path = os.path.join(model_dir, "ModelData.h5")
     self.compilation_time = 0.0
def vep_study():
    subjects_top_folder = os.path.join(os.path.expanduser("~"), 'Dropbox',
                                       'Work', 'VBtech', 'VEP', 'results',
                                       "CC")
    subject_base_name = "TVB"
    subj_ids = [3]  #[1, 2, 3, 4, 4]

    e_indices = [[]]  # ([40, 42], [], [1, 26], [], [])
    ep_names = "clinical_hypothesis_preseeg"  # (3 * ["clinical_hypothesis_preseeg"] + ["clinical_hypothesis_preseeg_right"]
    # + ["clinical_hypothesis_preseeg_bilateral"])

    for subj_id in range(4, len(subj_ids)):

        subject_name = subject_base_name + str(subj_ids[subj_id])
        head_path = os.path.join(subjects_top_folder, subject_name, "Head")
        e_inds = e_indices[subj_id]
        folder_results = os.path.join(head_path, ep_names[subj_id], "res")

        config = Config(head_path, Config.generic.MODE_JAVA, folder_results,
                        True)

        main_vep(config,
                 ep_name=ep_names[subj_id],
                 K_unscaled=K_UNSCALED_DEF,
                 ep_indices=e_inds,
                 hyp_norm=0.99,
                 manual_hypos=[],
                 sim_type=["default", "realistic"],
                 pse_flag=True,
                 sa_pse_flag=False,
                 sim_flag=True,
                 n_samples=100)
示例#3
0
def main_sensitivity_analysis(config=Config()):
    # -------------------------------Reading data-----------------------------------
    reader = Reader()
    writer = H5Writer()
    head = reader.read_head(config.input.HEAD)
    # --------------------------Hypothesis definition-----------------------------------
    n_samples = 100
    # Manual definition of hypothesis...:
    x0_indices = [20]
    x0_values = [0.9]
    e_indices = [70]
    e_values = [0.9]
    disease_indices = x0_indices + e_indices
    n_disease = len(disease_indices)
    n_x0 = len(x0_indices)
    n_e = len(e_indices)
    all_regions_indices = np.array(range(head.connectivity.number_of_regions))
    healthy_indices = np.delete(all_regions_indices, disease_indices).tolist()
    n_healthy = len(healthy_indices)
    # This is an example of x0_values mixed Excitability and Epileptogenicity Hypothesis:
    hyp_x0_E = HypothesisBuilder(
        head.connectivity.number_of_regions).set_x0_hypothesis(
            x0_indices,
            x0_values).set_e_hypothesis(e_indices,
                                        e_values).build_hypothesis()
    # Now running the sensitivity analysis:
    logger.info("running sensitivity analysis PSE LSA...")
    for m in METHODS:
        try:
            model_configuration_builder, model_configuration, lsa_service, lsa_hypothesis, sa_results, pse_results = \
                sensitivity_analysis_pse_from_hypothesis(hyp_x0_E,
                                                         head.connectivity.normalized_weights,
                                                         head.connectivity.region_labels,
                                                         n_samples, method=m, param_range=0.1,
                                                         global_coupling=[{"indices": all_regions_indices,
                                                                           "low": 0.0, "high": 2 * K_UNSCALED_DEF}],
                                                         healthy_regions_parameters=[
                                                             {"name": "x0_values", "indices": healthy_indices}],
                                                         config=config, save_services=True)
            Plotter(config).plot_lsa(
                lsa_hypothesis,
                model_configuration,
                lsa_service.weighted_eigenvector_sum,
                lsa_service.eigen_vectors_number,
                region_labels=head.connectivity.region_labels,
                pse_results=pse_results,
                title=m + "_PSE_LSA_overview_" + lsa_hypothesis.name,
                lsa_service=lsa_service)
            # , show_flag=True, save_flag=False
            result_file = os.path.join(
                config.out.FOLDER_RES,
                m + "_PSE_LSA_results_" + lsa_hypothesis.name + ".h5")
            writer.write_dictionary(pse_results, result_file)
            result_file = os.path.join(
                config.out.FOLDER_RES,
                m + "_SA_LSA_results_" + lsa_hypothesis.name + ".h5")
            writer.write_dictionary(sa_results, result_file)
        except:
            logger.warning("Method " + m + " failed!")
示例#4
0
def main_pse(config=Config()):
    # -------------------------------Reading data-----------------------------------
    reader = Reader()
    writer = H5Writer()
    head = reader.read_head(config.input.HEAD)
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)

    # --------------------------Manual Hypothesis definition-----------------------------------
    n_samples = 100
    x0_indices = [20]
    x0_values = [0.9]
    e_indices = [70]
    e_values = [0.9]
    disease_indices = x0_indices + e_indices
    n_disease = len(disease_indices)

    n_x0 = len(x0_indices)
    n_e = len(e_indices)
    all_regions_indices = np.array(range(head.number_of_regions))
    healthy_indices = np.delete(all_regions_indices, disease_indices).tolist()
    n_healthy = len(healthy_indices)
    # This is an example of x0_values mixed Excitability and Epileptogenicity Hypothesis:
    hyp_x0_E = HypothesisBuilder(
        head.connectivity.number_of_regions).set_x0_hypothesis(
            x0_indices,
            x0_values).set_e_hypothesis(e_indices,
                                        e_values).build_hypothesis()

    # Now running the parameter search analysis:
    logger.info("running PSE LSA...")
    model_config, lsa_service, lsa_hypothesis, pse_res = pse_from_hypothesis(
        hyp_x0_E,
        head.connectivity.normalized_weights,
        head.connectivity.region_labels,
        n_samples,
        param_range=0.1,
        global_coupling=[{
            "indices": all_regions_indices
        }],
        healthy_regions_parameters=[{
            "name": "x0_values",
            "indices": healthy_indices
        }],
        save_services=True)[:4]

    logger.info("Plotting LSA...")
    Plotter(config).plot_lsa(lsa_hypothesis,
                             model_config,
                             lsa_service.weighted_eigenvector_sum,
                             lsa_service.eigen_vectors_number,
                             region_labels=head.connectivity.region_labels,
                             pse_results=pse_res,
                             lsa_service=lsa_service)

    logger.info("Saving LSA results ...")
    writer.write_dictionary(
        pse_res,
        os.path.join(config.out.FOLDER_RES,
                     lsa_hypothesis.name + "_PSE_LSA_results.h5"))
示例#5
0
class BaseTest(object):
    config = Config(output_base=os.path.join(os.getcwd(), "vep_test_out"))

    dummy_connectivity = Connectivity("",
                                      numpy.array([[1.0, 2.0, 3.0],
                                                   [2.0, 3.0, 1.0],
                                                   [3.0, 2.0, 1.0]]),
                                      numpy.array([[4, 5, 6], [5, 6, 4],
                                                   [6, 4, 5]]),
                                      labels=["a", "b", "c"],
                                      centres=numpy.array([1.0, 2.0, 3.0]),
                                      normalized_weights=numpy.array(
                                          [[1.0, 2.0, 3.0], [2.0, 3.0, 1.0],
                                           [3.0, 2.0, 1.0]]))
    dummy_surface = Surface(numpy.array([[1, 2, 3], [2, 3, 1], [3, 1, 2]]),
                            numpy.array([[0, 1, 2]]))
    dummy_sensors = Sensors(numpy.array(["sens1", "sens2"]),
                            numpy.array([[0, 0, 0], [0, 1, 0]]),
                            gain_matrix=numpy.array([[1, 2, 3], [2, 3, 4]]))

    def _prepare_dummy_head_from_dummy_attrs(self):
        return Head(self.dummy_connectivity,
                    self.dummy_surface,
                    sensorsSEEG={"SensorsSEEG": self.dummy_sensors})

    def _prepare_dummy_head(self):
        reader = H5Reader()
        connectivity = reader.read_connectivity(
            os.path.join(self.config.input.HEAD, "Connectivity.h5"))
        cort_surface = Surface(numpy.array([]), numpy.array([]))
        seeg_sensors = Sensors(numpy.array(["sens1", "sens2"]),
                               numpy.array([[0, 0, 0], [0, 1, 0]]))
        head = Head(connectivity,
                    cort_surface,
                    sensorsSEEG={"SensorsSEEG": seeg_sensors})

        return head

    @classmethod
    def setup_class(cls):
        for direc in (cls.config.out.FOLDER_LOGS, cls.config.out.FOLDER_RES,
                      cls.config.out.FOLDER_FIGURES,
                      cls.config.out.FOLDER_TEMP):
            if not os.path.exists(direc):
                os.makedirs(direc)

    @classmethod
    def teardown_class(cls):
        for direc in (cls.config.out.FOLDER_LOGS, cls.config.out.FOLDER_RES,
                      cls.config.out.FOLDER_FIGURES,
                      cls.config.out.FOLDER_TEMP):
            for dir_file in os.listdir(direc):
                os.remove(os.path.join(os.path.abspath(direc), dir_file))
            os.removedirs(direc)
示例#6
0
def from_hypothesis_to_model_config_lsa(hyp,
                                        head,
                                        eigen_vectors_number=None,
                                        weighted_eigenvector_sum=True,
                                        config=Config(),
                                        save_flag=None,
                                        plot_flag=None,
                                        **kwargs):
    logger.info("\n\nRunning hypothesis: " + hyp.name)
    logger.info("\n\nCreating model configuration...")
    if save_flag is None:
        save_flag = config.figures.SAVE_FLAG
    if plot_flag is None:
        plot_flag = config.figures.SHOW_FLAG
    builder = ModelConfigurationBuilder("EpileptorDP2D",
                                        head.connectivity.normalized_weights,
                                        **kwargs)
    if hyp.type == "Epileptogenicity":
        model_configuration = builder.build_model_from_E_hypothesis(hyp)
    else:
        model_configuration = builder.build_model_from_hypothesis(hyp)
    logger.info("\n\nRunning LSA...")
    lsa_service = LSAService(eigen_vectors_number=eigen_vectors_number,
                             weighted_eigenvector_sum=weighted_eigenvector_sum)
    lsa_hypothesis = lsa_service.run_lsa(hyp, model_configuration)
    if save_flag:
        writer = H5Writer()
        path_mc = os.path.join(config.out.FOLDER_RES,
                               hyp.name + "_ModelConfig.h5")
        writer.write_model_configuration(model_configuration, path_mc)
        writer.write_hypothesis(
            lsa_hypothesis,
            os.path.join(config.out.FOLDER_RES, lsa_hypothesis.name + ".h5"))
    if plot_flag:
        # Plot nullclines and equilibria of model configuration
        plotter = Plotter(config)
        plotter.plot_state_space(model_configuration,
                                 head.connectivity.region_labels,
                                 special_idx=hyp.regions_disease_indices,
                                 figure_name=hyp.name + "_StateSpace")
        plotter.plot_lsa(lsa_hypothesis,
                         model_configuration,
                         lsa_service.weighted_eigenvector_sum,
                         lsa_service.eigen_vectors_number,
                         head.connectivity.region_labels,
                         None,
                         lsa_service=lsa_service)
    return model_configuration, lsa_hypothesis, builder, lsa_service
示例#7
0
def start_lsa_run(hypothesis,
                  model_connectivity,
                  config=Config(),
                  model_config_args={}):
    logger.info("creating model configuration...")
    model_configuration_builder = ModelConfigurationBuilder(
        "EpileptorDP2D", model_connectivity, **model_config_args)
    model_configuration = model_configuration_builder.build_model_from_hypothesis(
        hypothesis)

    logger.info("running LSA...")
    lsa_service = LSAService(
        eigen_vectors_number_selection=config.calcul.
        EIGENVECTORS_NUMBER_SELECTION,
        eigen_vectors_number=None,
        weighted_eigenvector_sum=config.calcul.WEIGHTED_EIGENVECTOR_SUM,
        normalize_propagation_strength=False)
    lsa_hypothesis = lsa_service.run_lsa(hypothesis, model_configuration)

    return model_configuration_builder, model_configuration, lsa_service, lsa_hypothesis
def pse_from_hypothesis(n_samples,
                        hypothesis,
                        model_connectivity,
                        region_labels,
                        param_range=0.1,
                        global_coupling=[],
                        healthy_regions_parameters=[],
                        save_flag=False,
                        folder_res="",
                        filename=None,
                        config=Config(),
                        model_config_kwargs={},
                        **kwargs):
    if not os.path.isdir(folder_res):
        folder_res = config.out.FOLDER_RES
    logger = initialize_logger(__name__)
    logger.info("Running hypothesis: " + hypothesis.name)

    # Compute lsa for this hypothesis before the parameter search:
    model_configuration_builder, model_configuration, lsa_service, lsa_hypothesis = \
        start_lsa_run(hypothesis, model_connectivity, config, **model_config_kwargs)
    pse_results, pse_params_list = pse_from_lsa_hypothesis(
        lsa_hypothesis,
        model_connectivity,
        model_configuration_builder,
        lsa_service,
        region_labels,
        n_samples,
        param_range,
        global_coupling,
        healthy_regions_parameters,
        save_flag,
        folder_res=folder_res,
        filename=filename,
        logger=logger,
        config=config,
        **kwargs)
    return model_configuration, lsa_service, lsa_hypothesis, pse_results, pse_params_list
def sensitivity_analysis_pse_from_hypothesis(n_samples,
                                             hypothesis,
                                             connectivity_matrix,
                                             region_labels,
                                             method="sobol",
                                             half_range=0.1,
                                             global_coupling=[],
                                             healthy_regions_parameters=[],
                                             save_services=False,
                                             config=Config(),
                                             model_config_kwargs={},
                                             **kwargs):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
    # Compute lsa for this hypothesis before sensitivity analysis:
    logger.info("Running hypothesis: " + hypothesis.name)
    model_configuration_builder, model_configuration, lsa_service, lsa_hypothesis = \
        start_lsa_run(hypothesis, connectivity_matrix, config, **model_config_kwargs)
    results, pse_results = sensitivity_analysis_pse_from_lsa_hypothesis(
        n_samples, lsa_hypothesis, connectivity_matrix,
        model_configuration_builder, lsa_service, region_labels, method,
        half_range, global_coupling, healthy_regions_parameters, save_services,
        config, **kwargs)
    return model_configuration_builder, model_configuration, lsa_service, lsa_hypothesis, results, pse_results
def get_target_timeseries(probabilistic_model,
                          head,
                          hypothesis,
                          times_on,
                          time_length,
                          sensors_lbls,
                          sensor_id,
                          observation_model,
                          sim_target_file,
                          empirical_target_file,
                          sim_source_type="paper",
                          downsampling=1,
                          empirical_files=[],
                          config=Config(),
                          plotter=None):

    # Some scripts for settting and preprocessing target signals:
    simulator = None
    log_flag = observation_model == OBSERVATION_MODELS.SEEG_LOGPOWER.value
    empirical_files = ensure_list(empirical_files)
    times_on = ensure_list(times_on)
    seizure_length = int(
        np.ceil(
            compute_seizure_length(probabilistic_model.tau0) / downsampling))
    if len(empirical_files) > 0:
        if log_flag:
            preprocessing = ["spectrogram", "log"]  #
        else:
            preprocessing = [
                "hpf", "mean_center", "abs-envelope", "convolve", "decimate",
                "baseline"
            ]
        # -------------------------- Get empirical data (preprocess edf if necessary) --------------------------
        signals, probabilistic_model.number_of_seizures = \
            set_multiple_empirical_data(empirical_files, empirical_target_file, head, sensors_lbls, sensor_id,
                                        seizure_length, times_on, time_length,
                                        label_strip_fun=lambda s: s.split("POL ")[-1], preprocessing=preprocessing,
                                        plotter=plotter, title_prefix="")
    else:
        probabilistic_model.number_of_seizures = 1
        # --------------------- Get fitting target simulated data (simulate if necessary) ----------------------
        probabilistic_model.target_data_type = Target_Data_Type.SYNTHETIC.value
        if sim_source_type == "paper":
            if log_flag:
                preprocessing = [
                    "spectrogram", "log"
                ]  #, "convolve" # ["hpf", "mean_center", "abs_envelope", "log"]
            else:
                preprocessing = ["convolve", "decimate", "baseline"]
        else:
            if log_flag:
                preprocessing = ["log"]
            else:
                preprocessing = ["decimate", "baseline"]
        rescale_x1eq = -1.225
        if np.max(probabilistic_model.model_config.x1eq) > rescale_x1eq:
            rescale_x1eq = False
        signals, simulator = \
            set_simulated_target_data(sim_target_file, head, hypothesis, probabilistic_model, sensor_id,
                                      rescale_x1eq=rescale_x1eq, sim_type=sim_source_type,
                                      times_on_off=[times_on[0], times_on[0] + time_length],
                                      seizure_length=seizure_length,
                                      # Maybe change some of those for Epileptor 6D simulations:
                                      bipolar=False, preprocessing=preprocessing,
                                      plotter=plotter, config=config, title_prefix="")
    return signals, probabilistic_model, simulator
def set_simulated_target_data(ts_file,
                              head,
                              lsa_hypothesis,
                              probabilistic_model,
                              sensor_id=0,
                              rescale_x1eq=None,
                              sim_type="paper",
                              times_on_off=[],
                              seizure_length=SEIZURE_LENGTH,
                              preprocessing=TARGET_DATA_PREPROCESSING,
                              low_hpf=LOW_HPF,
                              high_hpf=HIGH_HPF,
                              low_lpf=LOW_LPF,
                              high_lpf=HIGH_LPF,
                              bipolar=BIPOLAR,
                              win_len_ratio=WIN_LEN_RATIO,
                              plotter=None,
                              config=Config(),
                              title_prefix=""):
    signals, simulator = from_model_configuration_to_simulation(
        probabilistic_model.model_config,
        head,
        lsa_hypothesis,
        rescale_x1eq=rescale_x1eq,
        sim_type=sim_type,
        ts_file=ts_file,
        config=config,
        plotter=plotter)
    try:
        probabilistic_model.ground_truth.update({
            "tau1":
            np.mean(simulator.model.tau1),
            "tau0":
            np.mean(simulator.model.tau0),
            "sigma":
            np.mean(simulator.settings.noise_intensity)
        })
    except:
        probabilistic_model.ground_truth.update({
            "tau1":
            np.mean(simulator.model.tt),
            "tau0":
            1.0 / np.mean(simulator.model.r),
            "sigma":
            np.mean(simulator.settings.noise_intensity)
        })
    x1z = signals["source"].get_time_window_by_units(times_on_off[0],
                                                     times_on_off[1])
    x1 = x1z.x1.squeezed
    z = x1z.z.squeezed
    del x1z
    probabilistic_model.ground_truth.update({
        "x1_init": x1[0].squeeze(),
        "z_init": z[0].squeeze(),
    })
    del x1, z
    signals = signals["source"].get_source()
    signals.data = -signals.data  # change sign to fit x1

    if probabilistic_model.observation_model in OBSERVATION_MODELS.SEEG.value:
        log_flag = probabilistic_model.observation_model == OBSERVATION_MODELS.SEEG_LOGPOWER.value
        title_prefix = title_prefix + "SimSEEG"
        signals = prepare_simulated_seeg_observable(
            signals, head.get_sensors_by_index(sensor_ids=sensor_id),
            seizure_length, log_flag, times_on_off, [], preprocessing, low_hpf,
            high_hpf, low_lpf, high_lpf, bipolar, win_len_ratio, plotter,
            title_prefix)

    else:
        title_prefix = title_prefix + "SimSource"
        signals = prepare_signal_observable(signals,
                                            probabilistic_model.time_length,
                                            times_on_off, [], preprocessing,
                                            low_hpf, high_hpf, low_lpf,
                                            high_lpf, win_len_ratio, plotter,
                                            title_prefix)
    move_overwrite_files_to_folder_with_wildcard(
        os.path.join(plotter.config.out.FOLDER_FIGURES,
                     "fitData_" + title_prefix),
        os.path.join(plotter.config.out.FOLDER_FIGURES,
                     title_prefix.replace(" ", "_")) + "*")
    move_overwrite_files_to_folder_with_wildcard(
        os.path.join(plotter.config.out.FOLDER_FIGURES, "Simulation"),
        os.path.join(plotter.config.out.FOLDER_FIGURES, "*Simulated*"))
    return signals, simulator
示例#12
0
def main_vep(config=Config(),
             ep_name=EP_NAME,
             K_unscaled=K_UNSCALED_DEF,
             ep_indices=[],
             hyp_norm=0.99,
             manual_hypos=[],
             sim_type="paper",
             pse_flag=PSE_FLAG,
             sa_pse_flag=SA_PSE_FLAG,
             sim_flag=SIM_FLAG,
             n_samples=100,
             test_write_read=False):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
    # -------------------------------Reading data-----------------------------------
    reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader()
    writer = H5Writer()
    plotter = Plotter(config)
    logger.info("Reading from: " + config.input.HEAD)
    head = reader.read_head(config.input.HEAD)
    plotter.plot_head(head)
    if test_write_read:
        writer.write_head(head, os.path.join(config.out.FOLDER_RES, "Head"))
    # --------------------------Hypothesis definition-----------------------------------

    hypotheses = []
    # Reading a h5 file:

    if len(ep_name) > 0:
        # For an Excitability Hypothesis you leave e_indices empty
        # For a Mixed Hypothesis: you give as e_indices some indices for values > 0
        # For an Epileptogenicity Hypothesis: you give as e_indices all indices for values > 0
        hyp_file = HypothesisBuilder(head.connectivity.number_of_regions, config=config).set_normalize(hyp_norm). \
            build_hypothesis_from_file(ep_name, e_indices=ep_indices)
        hyp_file.name += ep_name
        # print(hyp_file.string_regions_disease(head.connectivity.region_labels))
        hypotheses.append(hyp_file)

    hypotheses += manual_hypos

    # --------------------------Hypothesis and LSA-----------------------------------
    for hyp in hypotheses:
        logger.info("\n\nRunning hypothesis: " + hyp.name)

        all_regions_indices = np.array(range(head.number_of_regions))
        healthy_indices = np.delete(all_regions_indices,
                                    hyp.regions_disease_indices).tolist()

        logger.info("\n\nCreating model configuration...")
        model_config_builder = ModelConfigurationBuilder("EpileptorDP2D", head.connectivity, K_unscaled=K_unscaled). \
                                    set_parameter("tau1", TAU1_DEF).set_parameter("tau0", TAU0_DEF)
        mcs_file = os.path.join(config.out.FOLDER_RES,
                                hyp.name + "_model_config_builder.h5")
        writer.write_model_configuration_builder(model_config_builder,
                                                 mcs_file)
        if test_write_read:
            logger.info(
                "Written and read model configuration builders are identical?: "
                + str(
                    assert_equal_objects(
                        model_config_builder,
                        reader.read_model_configuration_builder(mcs_file),
                        logger=logger)))
        # Fix healthy regions to default equilibria:
        # model_configuration = \
        #        model_config_builder.build_model_from_E_hypothesis(hyp)
        # Fix healthy regions to default x0s:
        model_configuration = model_config_builder.build_model_from_hypothesis(
            hyp)
        mc_path = os.path.join(config.out.FOLDER_RES,
                               hyp.name + "_ModelConfig.h5")
        writer.write_model_configuration(model_configuration, mc_path)
        if test_write_read:
            logger.info(
                "Written and read model configuration are identical?: " + str(
                    assert_equal_objects(model_configuration,
                                         reader.read_model_configuration(
                                             mc_path),
                                         logger=logger)))
        # Plot nullclines and equilibria of model configuration
        plotter.plot_state_space(model_configuration,
                                 head.connectivity.region_labels,
                                 special_idx=hyp.regions_disease_indices,
                                 figure_name=hyp.name + "_StateSpace")

        logger.info("\n\nRunning LSA...")
        lsa_service = LSAService(eigen_vectors_number=1)
        lsa_hypothesis = lsa_service.run_lsa(hyp, model_configuration)

        lsa_path = os.path.join(config.out.FOLDER_RES,
                                lsa_hypothesis.name + "_LSA.h5")
        lsa_config_path = os.path.join(config.out.FOLDER_RES,
                                       lsa_hypothesis.name + "_LSAConfig.h5")
        writer.write_hypothesis(lsa_hypothesis, lsa_path)
        writer.write_lsa_service(lsa_service, lsa_config_path)
        if test_write_read:
            logger.info("Written and read LSA services are identical?: " + str(
                assert_equal_objects(lsa_service,
                                     reader.read_lsa_service(lsa_config_path),
                                     logger=logger)))
            logger.info(
                "Written and read LSA hypotheses are identical (no input check)?: "
                + str(
                    assert_equal_objects(lsa_hypothesis,
                                         reader.read_hypothesis(lsa_path),
                                         logger=logger)))
        plotter.plot_lsa(lsa_hypothesis,
                         model_configuration,
                         lsa_service.weighted_eigenvector_sum,
                         lsa_service.eigen_vectors_number,
                         head.connectivity.region_labels,
                         None,
                         lsa_service=lsa_service)

        if pse_flag:
            # --------------Parameter Search Exploration (PSE)-------------------------------
            logger.info("\n\nRunning PSE LSA...")
            pse_results = pse_from_lsa_hypothesis(
                n_samples,
                lsa_hypothesis,
                model_configuration.connectivity,
                model_config_builder,
                lsa_service,
                head.connectivity.region_labels,
                param_range=0.1,
                global_coupling=[{
                    "indices": all_regions_indices
                }],
                healthy_regions_parameters=[{
                    "name": "x0_values",
                    "indices": healthy_indices
                }],
                logger=logger,
                save_flag=True)[0]
            plotter.plot_lsa(lsa_hypothesis, model_configuration,
                             lsa_service.weighted_eigenvector_sum,
                             lsa_service.eigen_vectors_number,
                             head.connectivity.region_labels, pse_results)

            pse_lsa_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_PSE_LSA_results.h5")
            writer.write_dictionary(pse_results, pse_lsa_path)
            if test_write_read:
                logger.info(
                    "Written and read parameter search results are identical?: "
                    + str(
                        assert_equal_objects(pse_results,
                                             reader.read_dictionary(
                                                 pse_lsa_path),
                                             logger=logger)))

        if sa_pse_flag:
            # --------------Sensitivity Analysis Parameter Search Exploration (PSE)-------------------------------
            logger.info("\n\nrunning sensitivity analysis PSE LSA...")
            sa_results, pse_sa_results = \
                sensitivity_analysis_pse_from_lsa_hypothesis(n_samples, lsa_hypothesis,
                                                             model_configuration.connectivity,
                                                             model_config_builder, lsa_service,
                                                             head.connectivity.region_labels,
                                                             method="sobol", param_range=0.1,
                                                             global_coupling=[{"indices": all_regions_indices,
                                                                               "bounds": [0.0, 2 *
                                                                                          model_config_builder.K_unscaled[
                                                                                              0]]}],
                                                             healthy_regions_parameters=[
                                                                 {"name": "x0_values", "indices": healthy_indices}],
                                                             config=config)
            plotter.plot_lsa(lsa_hypothesis,
                             model_configuration,
                             lsa_service.weighted_eigenvector_sum,
                             lsa_service.eigen_vectors_number,
                             head.connectivity.region_labels,
                             pse_sa_results,
                             title="SA PSE Hypothesis Overview")

            sa_pse_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_SA_PSE_LSA_results.h5")
            sa_lsa_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_SA_LSA_results.h5")
            writer.write_dictionary(pse_sa_results, sa_pse_path)
            writer.write_dictionary(sa_results, sa_lsa_path)
            if test_write_read:
                logger.info(
                    "Written and read sensitivity analysis results are identical?: "
                    + str(
                        assert_equal_objects(sa_results,
                                             reader.read_dictionary(
                                                 sa_lsa_path),
                                             logger=logger)))
                logger.info(
                    "Written and read sensitivity analysis parameter search results are identical?: "
                    + str(
                        assert_equal_objects(pse_sa_results,
                                             reader.read_dictionary(
                                                 sa_pse_path),
                                             logger=logger)))

        if sim_flag:
            # --------------------------Simulation preparations-----------------------------------
            # If you choose model...
            # Available models beyond the TVB Epileptor (they all encompass optional variations from the different papers):
            # EpileptorDP: similar to the TVB Epileptor + optional variations,
            # EpileptorDP2D: reduced 2D model, following Proix et all 2014 +optional variations,
            # EpleptorDPrealistic: starting from the TVB Epileptor + optional variations, but:
            #      -x0, Iext1, Iext2, slope and K become noisy state variables,
            #      -Iext2 and slope are coupled to z, g, or z*g in order for spikes to appear before seizure,
            #      -correlated noise is also used
            # We don't want any time delays for the moment
            head.connectivity.tract_lengths *= config.simulator.USE_TIME_DELAYS_FLAG
            sim_types = ensure_list(sim_type)
            for sim_type in sim_types:
                # ------------------------------Simulation--------------------------------------
                logger.info(
                    "\n\nConfiguring %s simulation from model_configuration..."
                    % sim_type)
                if isequal_string(sim_type, "realistic"):
                    sim_builder = SimulatorBuilder(model_configuration).set_model("EpileptorDPrealistic"). \
                        set_fs(2048.0).set_simulation_length(60000.0)
                    sim_builder.model_config.tau0 = 60000.0
                    sim_builder.model_config.tau1 = 0.2
                    sim_builder.model_config.slope = 0.25
                    sim_builder.model_config.pmode = np.array([PMODE_DEF])
                    sim_settings = sim_builder.build_sim_settings()
                    sim_settings.noise_type = COLORED_NOISE
                    sim_settings.noise_ntau = 20
                    # Necessary a more stable integrator:
                    sim_settings.integrator_type = "Dop853Stochastic"
                elif isequal_string(sim_type, "fitting"):
                    sim_builder = SimulatorBuilder(model_configuration).set_model("EpileptorDP2D"). \
                        set_fs(2048.0).set_fs_monitor(2048.0).set_simulation_length(300.0)
                    sim_builder.model_config.tau0 = 30.0
                    sim_builder.model_config.tau1 = 0.5
                    sim_settings = sim_builder.build_sim_settings()
                    sim_settings.noise_intensity = np.array([0.0, 1e-5])
                elif isequal_string(sim_type, "reduced"):
                    sim_builder = \
                        SimulatorBuilder(model_configuration).set_model("EpileptorDP2D").set_fs(
                            4096.0).set_simulation_length(1000.0)
                    sim_settings = sim_builder.build_sim_settings()
                elif isequal_string(sim_type, "paper"):
                    sim_builder = SimulatorBuilder(
                        model_configuration).set_model("Epileptor")
                    sim_settings = sim_builder.build_sim_settings()
                else:
                    sim_builder = SimulatorBuilder(
                        model_configuration).set_model("EpileptorDP")
                    sim_settings = sim_builder.build_sim_settings()

                # Integrator and initial conditions initialization.
                # By default initial condition is set right on the equilibrium point.
                sim, sim_settings = \
                    sim_builder.build_simulator_TVB_from_model_sim_settings(head.connectivity, sim_settings)
                sim_path = os.path.join(
                    config.out.FOLDER_RES,
                    lsa_hypothesis.name + "_" + sim_type + "_sim_settings.h5")
                model_path = os.path.join(
                    config.out.FOLDER_RES,
                    lsa_hypothesis.name + sim_type + "_model.h5")
                writer.write_simulation_settings(sim.settings, sim_path)
                writer.write_simulator_model(
                    sim.model, model_path, sim.connectivity.number_of_regions)
                if test_write_read:
                    # TODO: find out why it cannot set monitor expressions
                    logger.info(
                        "Written and read simulation settings are identical?: "
                        + str(
                            assert_equal_objects(
                                sim.settings,
                                reader.read_simulation_settings(sim_path),
                                logger=logger)))
                    # logger.info("Written and read simulation model are identical?: " +
                    #             str(assert_equal_objects(sim.model,
                    #                                      reader.read_epileptor_model(model_path), logger=logger)))

                logger.info("\n\nSimulating %s..." % sim_type)
                sim_output, status = sim.launch_simulation(
                    report_every_n_monitor_steps=100, timeseries=Timeseries)
                if not status:
                    logger.warning("\nSimulation failed!")
                else:
                    time = np.array(sim_output.time).astype("f")
                    logger.info("\n\nSimulated signal return shape: %s",
                                sim_output.shape)
                    logger.info("Time: %s - %s", time[0], time[-1])
                    logger.info("Values: %s - %s", sim_output.data.min(),
                                sim_output.data.max())
                    if not status:
                        logger.warning("\nSimulation failed!")
                    else:
                        sim_output, seeg = compute_seeg_and_write_ts_to_h5(
                            sim_output,
                            sim.model,
                            head.sensorsSEEG,
                            os.path.join(config.out.FOLDER_RES,
                                         sim_type + "_ts.h5"),
                            seeg_gain_mode="lin",
                            hpf_flag=True,
                            hpf_low=10.0,
                            hpf_high=512.0)

                        # Plot results
                        plotter.plot_simulated_timeseries(
                            sim_output,
                            sim.model,
                            lsa_hypothesis.lsa_propagation_indices,
                            seeg_dict=seeg,
                            spectral_raster_plot=False,
                            title_prefix=hyp.name,
                            spectral_options={"log_scale": True})
示例#13
0
                            hpf_flag=True,
                            hpf_low=10.0,
                            hpf_high=512.0)

                        # Plot results
                        plotter.plot_simulated_timeseries(
                            sim_output,
                            sim.model,
                            lsa_hypothesis.lsa_propagation_indices,
                            seeg_dict=seeg,
                            spectral_raster_plot=False,
                            title_prefix=hyp.name,
                            spectral_options={"log_scale": True})


if __name__ == "__main__":
    head_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work',
                               'VBtech', 'VEP', "results", "CC", "TVB3",
                               "Head")
    output = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work', 'VBtech',
                          'VEP', "results", "tests")
    config = Config(head_folder=head_folder,
                    output_base=output,
                    separate_by_run=False)
    main_vep(config,
             "clinical_hypothesis_postseeg",
             ep_indices=[1, 26],
             sim_type=["paper", "default", "fitting", "reduced"],
             test_write_read=True)  #",, "realistic"
    # main_vep()
def main_sampling_service(config=Config()):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)

    n_samples = 100
    logger.info("\nDeterministic numpy.linspace sampling:")
    sampler = DeterministicSampler(n_samples=n_samples, grid_mode=True)
    samples, stats = sampler.generate_samples(low=1.0,
                                              high=2.0,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nStochastic uniform sampling with numpy:")
    sampler = ProbabilisticSampler(n_samples=n_samples,
                                   sampling_module="numpy")
    #                                      a (low), b (high)
    samples, stats = sampler.generate_samples(
        parameter=(1.0, 2.0),
        probability_distribution=ProbabilityDistributionTypes.UNIFORM,
        shape=(2, ),
        stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))

    logger.info(sampler.__repr__())

    logger.info("\nStochastic truncated normal sampling with scipy:")
    sampler = ProbabilisticSampler(n_samples=n_samples)
    #                                   loc (mean), scale (sigma)
    samples, stats = sampler.generate_samples(parameter=(1.5, 1.0),
                                              probability_distribution="norm",
                                              low=1,
                                              high=2,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nSensitivity analysis sampling:")
    sampler = SalibSamplerInterface(n_samples=n_samples, sampler="latin")
    samples, stats = sampler.generate_samples(low=1,
                                              high=2,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nTesting distribution class and conversions...")
    sampler = ProbabilisticSampler(n_samples=n_samples)
    for distrib_name in ProbabilityDistributionTypes.available_distributions:
        logger.info("\n" + distrib_name)
        logger.info("\nmode/mean, std to distribution " + distrib_name + ":")
        if np.in1d(distrib_name, [
                ProbabilityDistributionTypes.EXPONENTIAL,
                ProbabilityDistributionTypes.CHISQUARE
        ]):
            target_stats = {"mean": 1.0}
            stats_m = "mean"
        elif np.in1d(distrib_name, [
                ProbabilityDistributionTypes.BERNOULLI,
                ProbabilityDistributionTypes.POISSON
        ]):
            target_stats = {"mean": np.ones((2, ))}
            stats_m = "mean"
        elif isequal_string(distrib_name,
                            ProbabilityDistributionTypes.BINOMIAL):
            target_stats = {"mean": 1.0, "std": 2.0}
            stats_m = "mean"
        else:
            if isequal_string(distrib_name,
                              ProbabilityDistributionTypes.UNIFORM):
                target_stats = {"mean": 1.0, "std": 2.0}
                stats_m = "mean"
            else:
                target_stats = {"mean": 1.0, "std": 2.0}
                stats_m = "mean"
        parameter1 = generate_probabilistic_parameter(
            name="test1_" + distrib_name,
            low=0.0,
            high=2.0,
            p_shape=(2, 2),
            probability_distribution=distrib_name,
            optimize_pdf=True,
            use="manual",
            **target_stats)
        name2 = "test2_" + distrib_name
        defaults = set_parameter_defaults(name2,
                                          _pdf=distrib_name,
                                          _shape=(2, 2),
                                          _lo=0.0,
                                          _hi=2.0,
                                          **(deepcopy(target_stats)))
        parameter2 = set_parameter(name=name2, use="manual", **defaults)
        for parameter in (parameter1, parameter2):
            logger.info(str(parameter))
            samples = sampler.generate_samples(parameter=parameter, stats=True)
            for key, value in stats.items():
                logger.info("\n" + key + ": " + str(value))
            diff = target_stats[stats_m] - stats[stats_m]
            if np.any(np.abs(diff.flatten()) > 0.001):
                logger.warning(
                    "Large difference between target and resulting samples' " +
                    stats_m + "!: " + str(diff))
            del parameter
 def __init__(self, number_of_regions=0, config=Config()):
     self.config = config
     self.logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
     self.number_of_regions = number_of_regions
     self.diseased_regions_values = numpy.zeros((self.number_of_regions, ))

if __name__ == "__main__":

    user_home = os.path.expanduser("~")
    SUBJECT = "TVB3"
    head_folder = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP',
                               "results", "CC", SUBJECT, "HeadDK")
    SEEG_data = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work',
                             'VBtech', 'VEP', "data/CC", "seeg", SUBJECT)

    if user_home == "/home/denis":
        output = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP',
                              "results", "INScluster/fit")
        config = Config(head_folder=head_folder,
                        raw_data_folder=SEEG_data,
                        output_base=output,
                        separate_by_run=False)
        config.generic.C_COMPILER = "g++"
        config.generic.CMDSTAN_PATH = "/soft/stan/cmdstan-2.17.0"
    # elif user_home == "/Users/lia.domide":
    #     config = Config(head_folder="/WORK/episense/tvb-epilepsy/data/TVB3/Head",
    #                     raw_data_folder="/WORK/episense/tvb-epilepsy/data/TVB3/ts_seizure")
    #     config.generic.CMDSTAN_PATH = "/WORK/episense/cmdstan-2.17.1"

    else:
        output = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP',
                              "results", "fit/tests/empirical_DK")
        config = Config(head_folder=head_folder,
                        raw_data_folder=SEEG_data,
                        output_base=output,
                        separate_by_run=False)
def main_fit_sim_hyplsa(stan_model_name,
                        empirical_files,
                        times_on,
                        time_length,
                        sim_times_on_off,
                        sensors_lbls,
                        normal_flag=False,
                        sim_source_type="fitting",
                        observation_model=OBSERVATION_MODELS.SEEG_POWER.value,
                        downsampling=2,
                        normalization="baseline-maxstd",
                        fitmethod="sample",
                        pse_flag=True,
                        fit_flag=True,
                        test_flag=False,
                        config=Config(),
                        **kwargs):

    # Prepare necessary services:
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
    reader = H5Reader()
    writer = H5Writer()
    plotter = Plotter(config)

    # Read head
    logger.info("Reading from: " + config.input.HEAD)
    head = reader.read_head(config.input.HEAD)
    sensors = ensure_list(head.get_sensors_by_name("distance"))[0]
    if isinstance(sensors, dict):
        sensors = sensors.values()[0]
        sensor_id = head.sensors_name_to_id(sensors.name)
    elif sensors is None:
        sensors = head.get_sensors_by_index()
        sensor_id = 0
    plotter.plot_head(head)

    # Set hypothesis:
    hyp = set_hypotheses(head, config)[0]

    config.out._out_base = os.path.join(config.out._out_base, hyp.name)
    plotter = Plotter(config)

    # Set model configuration and compute LSA
    model_configuration, lsa_hypothesis, pse_results = \
        set_model_config_LSA(head, hyp, reader, config, K_unscaled=3 * K_UNSCALED_DEF, tau1=TAU1_DEF, tau0=TAU0_DEF,
                             pse_flag=pse_flag, plotter=plotter, writer=writer)

    base_path = os.path.join(config.out.FOLDER_RES)

    # -----------Get prototypical simulated data from the fitting version of Epileptor(simulate if necessary) ----------
    source2D_file = path("Source2Dts", base_path)
    source2D_ts = get_2D_simulation(model_configuration,
                                    head,
                                    lsa_hypothesis,
                                    source2D_file,
                                    sim_times_on_off,
                                    config=config,
                                    reader=reader,
                                    writer=writer,
                                    plotter=plotter)

    # -------------------------- Get model_data and observation signals: -------------------------------------------

    target_data_file = path("FitTargetData", base_path)
    sim_target_file = path("ts_fit", base_path)
    empirical_target_file = path("ts_empirical", base_path)
    problstc_model_file = path("ProblstcModel", base_path)
    model_data_file = path("ModelData", base_path)

    if os.path.isfile(problstc_model_file) and os.path.isfile(
            model_data_file) and os.path.isfile(target_data_file):
        # Read existing probabilistic model and model data...
        probabilistic_model = reader.read_probabilistic_model(
            problstc_model_file)
        model_data = reader.read_dictionary(model_data_file)
        target_data = reader.read_timeseries(target_data_file)
    else:
        # Create model inversion service (stateless)
        model_inversion = SDEModelInversionService()
        model_inversion.normalization = normalization
        # Exclude ctx-l/rh-unknown regions from fitting
        model_inversion.active_regions_exlude = find_labels_inds(
            head.connectivity.region_labels, ["unknown"])

        # Generate probabilistic model and model data
        probabilistic_model = \
            SDEProbabilisticModelBuilder(model_name=stan_model_name, model_config=model_configuration,
                                         xmode=XModes.X1EQMODE.value, priors_mode=PriorsModes.NONINFORMATIVE.value,
                                         sde_mode=SDE_MODES.NONCENTERED.value, observation_model=observation_model,
                                         normal_flag=normal_flag,  K=np.mean(model_configuration.K),
                                         sigma=SIGMA_DEF).generate_model(generate_parameters=False)

        # Get by simulation and/or loading prototypical source 2D timeseries and the target (simulater or empirical)
        # time series for fitting
        signals, probabilistic_model, simulator = \
           get_target_timeseries(probabilistic_model, head, lsa_hypothesis, times_on, time_length,
                                 sensors_lbls, sensor_id, observation_model, sim_target_file,
                                 empirical_target_file, sim_source_type, downsampling, empirical_files, config, plotter)

        # Update active model's active region nodes
        e_values = pse_results.get("e_values_mean",
                                   model_configuration.e_values)
        lsa_propagation_strength = pse_results.get(
            "lsa_propagation_strengths_mean",
            lsa_hypothesis.lsa_propagation_strengths)
        model_inversion.active_e_th = 0.05
        probabilistic_model = \
            model_inversion.update_active_regions(probabilistic_model, sensors=sensors, e_values=e_values,
                                                  lsa_propagation_strengths=lsa_propagation_strength, reset=True)

        # Select and set the target time series
        target_data, probabilistic_model, model_inversion = \
            set_target_timeseries(probabilistic_model, model_inversion, signals, sensors, head,
                                  target_data_file, writer, plotter)

        #---------------------------------Finally set priors for the parameters-------------------------------------
        probabilistic_model = \
                set_prior_parameters(probabilistic_model, target_data, source2D_ts, None, problstc_model_file,
                                     [XModes.X0MODE.value, "x1_init", "z_init", "tau1",  # "tau0", "K", "x1",
                                      "sigma", "dWt", "epsilon", "scale", "offset"], normal_flag,
                                     writer=writer, plotter=plotter)

        # Construct the stan model data dict:
        model_data = build_stan_model_data_dict(
            probabilistic_model,
            target_data.squeezed,
            model_configuration.connectivity,
            time=target_data.time)

        # # ...or interface with INS stan models
        # from tvb_fit.service.model_inversion.vep_stan_dict_builder import \
        #   build_stan_model_data_dict_to_interface_ins
        # model_data = build_stan_model_data_dict_to_interface_ins(probabilistic_model, target_data.squeezed,
        #                                                          model_configuration.connectivity, gain_matrix,
        #                                                          time=target_data.time)
        writer.write_dictionary(model_data, model_data_file)

    # -------------------------- Fit or load results from previous fitting: -------------------------------------------

    estimates, samples, summary, info_crit = \
        run_fitting(probabilistic_model, stan_model_name, model_data, target_data, config, head,
                    hyp.all_disease_indices, ["tau1", "sigma", "epsilon", "scale", "offset"], ["x0", "PZ", "x1eq", "zeq"],
                    fit_flag, test_flag, base_path, fitmethod, n_chains_or_runs=2,
                    output_samples=200, num_warmup=100, min_samples_per_chain=200, max_depth=15, delta=0.95,
                    iter=500000, tol_rel_obj=1e-6, debug=1, simulate=0, writer=writer, plotter=plotter, **kwargs)

    # -------------------------- Reconfigure model after fitting:---------------------------------------------------
    model_configuration_fit = reconfigure_model_with_fit_estimates(
        head, model_configuration, probabilistic_model, estimates, base_path,
        writer, plotter)

    logger.info("Done!")
def from_model_configuration_to_simulation(model_configuration, head, lsa_hypothesis, rescale_x1eq=None,
                                           sim_type="realistic", ts_file=None, seeg_gain_mode="lin", hpf_flag=False,
                                           hpf_low=10.0, hpf_high=512.0, config=Config(), plotter=False,
                                           title_prefix=""):
    # Choose model
    # Available models beyond the TVB Epileptor (they all encompass optional variations from the different papers):
    # EpileptorDP: similar to the TVB Epileptor + optional variations,
    # EpileptorDP2D: reduced 2D model, following Proix et all 2014 +optional variations,
    # EpleptorDPrealistic: starting from the TVB Epileptor + optional variations, but:
    #      -x0, Iext1, Iext2, slope and K become noisy state variables,
    #      -Iext2 and slope are coupled to z, g, or z*g in order for spikes to appear before seizure,
    #      -multiplicative correlated noise is also used
    # Optional variations:

    if rescale_x1eq is not None:
        model_config = deepcopy(model_configuration)
        x1eq_min = np.min(model_config.x1eq)
        model_config.x1eq = interval_scaling(model_config.x1eq, min_targ=x1eq_min, max_targ=rescale_x1eq,
                                              min_orig=x1eq_min, max_orig=np.max(model_config.x1eq))
        zeq = calc_eq_z(model_config.x1eq, model_config.yc, model_config.Iext1, "2d",
                        np.zeros(model_config.zeq.shape), model_config.slope, model_config.a,
                                 model_config.b, model_config.d)
        model_config.x0 = calc_x0(model_config.x1eq, zeq, model_config.K, model_config.connectivity,
                                  model_config.zmode, z_pos=True, shape=zeq.shape, calc_mode="non_symbol")
    else:
        model_config = model_configuration

    # ------------------------------Simulation--------------------------------------
    hypname = lsa_hypothesis.name.replace("_LSA", "")
    logger.info("\n\nConfiguring simulation...")
    if isequal_string(sim_type, "realistic"):
        sim, sim_settings= build_simulator_TVB_realistic(model_config, head.connectivity)
    elif isequal_string(sim_type, "fitting"):
        sim, sim_settings = build_simulator_TVB_fitting(model_config, head.connectivity)
    elif isequal_string(sim_type, "paper"):
        sim, sim_settings = build_simulator_TVB_paper(model_config, head.connectivity)
    else:
        sim, sim_settings = build_simulator_TVB_default(model_config, head.connectivity)
    dynamical_model = sim.model
    writer = H5Writer()
    if config.out.FOLDER_RES.find(hypname) >= 0:
        model_path = os.path.join(config.out.FOLDER_RES, dynamical_model._ui_name + "_model.h5")
        title_prefix += ""
    else:
        model_path = os.path.join(config.out.FOLDER_RES, hypname + dynamical_model._ui_name + "_model.h5")
        title_prefix += hypname
    writer.write_simulator_model(sim.model, model_path, sim.connectivity.number_of_regions)

    seeg=[]
    if ts_file is not None and os.path.isfile(ts_file):
        logger.info("\n\nLoading previously simulated time series from file: " + ts_file)
        sim_output = H5Reader().read_timeseries(ts_file)
        seeg = TimeseriesService().compute_seeg(sim_output.get_source(), head.sensorsSEEG, sum_mode=seeg_gain_mode)
    else:
        logger.info("\n\nSimulating %s..." % sim_type)
        sim_output, status = sim.launch_simulation(report_every_n_monitor_steps=100, timeseries=Timeseries)
        if not status:
            logger.warning("\nSimulation failed!")
        else:
            time = np.array(sim_output.time).astype("f")
            logger.info("\n\nSimulated signal return shape: %s", sim_output.shape)
            logger.info("Time: %s - %s", time[0], time[-1])
            sim_output, seeg = compute_seeg_and_write_ts_to_h5(sim_output, sim.model, head.sensorsSEEG,
                                                               ts_file, seeg_gain_mode=seeg_gain_mode,
                                                               hpf_flag=hpf_flag, hpf_low=hpf_low, hpf_high=hpf_high)

    if plotter:
        if not isinstance(plotter, Plotter):
            plotter = Plotter(config)
        # Plot results
        plotter.plot_simulated_timeseries(sim_output, sim.model, lsa_hypothesis.all_disease_indices, seeg_dict=seeg,
                                          spectral_raster_plot=False, title_prefix=title_prefix,
                                          spectral_options={"log_scale": True})

    return {"source": sim_output, "seeg": seeg}, sim
from tvb_fit.io.h5_writer import H5Writer
from tvb_fit.io.h5_reader import H5Reader
from tvb_fit.tvb_epilepsy.base.constants.config import Config
from tvb_fit.tvb_epilepsy.service.hypothesis_builder import HypothesisBuilder
from tvb_fit.tvb_epilepsy.service.model_configuration_builder import ModelConfigurationBuilder
from tvb_fit.tvb_epilepsy.service.probabilistic_models_builders import SDEProbabilisticModelBuilder


if __name__ == "__main__":

    user_home = os.path.expanduser("~")
    head_folder = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "CC", "TVB3", "Head")

    if user_home == "/home/denis":
        output = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "INScluster")
        config = Config(head_folder=head_folder, output_base=output, separate_by_run=False)
    elif user_home == "/Users/lia.domide":
        config = Config(head_folder="/WORK/episense/tvb-epilepsy/data/TVB3/Head",
                        raw_data_folder="/WORK/episense/tvb-epilepsy/data/TVB3/ts_seizure")
    else:
        output = os.path.join(user_home, 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "fit")
        config = Config(head_folder=head_folder, output_base=output, separate_by_run=False)

    # Read head
    reader = H5Reader()

    head = reader.read_head(config.input.HEAD)

    hyp_builder = HypothesisBuilder(head.connectivity.number_of_regions, config).set_normalize(0.99)
    e_indices = [1, 26]  # [1, 2, 25, 26]
    hypothesis = hyp_builder.build_hypothesis_from_file("clinical_hypothesis_postseeg", e_indices)
def pse_from_lsa_hypothesis(n_samples,
                            lsa_hypothesis,
                            model_connectivity,
                            model_configuration_builder,
                            lsa_service,
                            region_labels,
                            param_range=0.1,
                            global_coupling=[],
                            healthy_regions_parameters=[],
                            save_flag=False,
                            folder_res="",
                            filename=None,
                            logger=None,
                            config=Config(),
                            **kwargs):
    if not os.path.isdir(folder_res):
        folder_res = config.out.FOLDER_RES
    if logger is None:
        logger = initialize_logger(__name__)
    all_regions_indices = range(lsa_hypothesis.number_of_regions)
    disease_indices = lsa_hypothesis.regions_disease_indices
    healthy_indices = np.delete(all_regions_indices, disease_indices).tolist()
    pse_params = {"path": [], "indices": [], "name": [], "samples": []}
    sampler = ProbabilisticSampler(n_samples=n_samples,
                                   random_seed=kwargs.get("random_seed", None))
    # First build from the hypothesis the input parameters of the parameter search exploration.
    # These can be either originating from excitability, epileptogenicity or connectivity hypotheses,
    # or they can relate to the global coupling scaling (parameter K of the model configuration)
    for ii in range(len(lsa_hypothesis.x0_values)):
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.x0_values")
        pse_params["name"].append(
            str(region_labels[lsa_hypothesis.x0_indices[ii]]) +
            " Excitability")

        # Now generate samples using a truncated uniform distribution
        pse_params["samples"].append(
            sampler.generate_samples(
                parameter=(
                    lsa_hypothesis.x0_values[ii],  # loc
                    param_range / 3.0),  # scale
                probability_distribution="norm",
                high=MAX_DISEASE_VALUE,
                shape=(1, )))
        # pse_params["samples"].append(
        #     sampler.generate_samples(parameter=(lsa_hypothesis.x0_values[ii] - param_range,  # loc
        #                                         2 * param_range),                            # scale
        #                              probability_distribution="uniform",
        #                              high=MAX_DISEASE_VALUE, shape=(1,)))
    for ii in range(len(lsa_hypothesis.e_values)):
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.e_values")
        pse_params["name"].append(
            str(region_labels[lsa_hypothesis.e_indices[ii]]) +
            " Epileptogenicity")

        # Now generate samples using a truncated uniform distribution
        pse_params["samples"].append(
            sampler.generate_samples(
                parameter=(
                    lsa_hypothesis.e_values[ii],  # loc
                    param_range / 3.0),  # scale
                probability_distribution="norm",
                high=MAX_DISEASE_VALUE,
                shape=(1, )))
        # pse_params["samples"].append(
        #     sampler.generate_samples(parameter=(lsa_hypothesis.e_values[ii] - param_range,  # loc
        #                                         2 * param_range),  # scale
        #                              probability_distribution="uniform",
        #                              high=MAX_DISEASE_VALUE, shape=(1,)))
    for ii in range(len(lsa_hypothesis.w_values)):
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.w_values")
        inds = linear_index_to_coordinate_tuples(lsa_hypothesis.w_indices[ii],
                                                 model_connectivity.shape)
        if len(inds) == 1:
            pse_params["name"].append(
                str(region_labels[inds[0][0]]) + "-" +
                str(region_labels[inds[0][0]]) + " Connectivity")
        else:
            pse_params["name"].append("Connectivity[" + str(inds), + "]")
        # Now generate samples using a truncated normal distribution
        pse_params["samples"].append(
            sampler.generate_samples(
                parameter=(
                    lsa_hypothesis.w_values[ii],  # loc
                    param_range * lsa_hypothesis.w_values[ii]),  # scale
                probability_distribution="norm",
                low=0.0,
                shape=(1, )))
    kloc = model_configuration_builder.K_unscaled[0]
    for val in global_coupling:
        pse_params["path"].append("model_configuration_builder.K_unscaled")
        inds = val.get("indices", all_regions_indices)
        if np.all(inds == all_regions_indices):
            pse_params["name"].append("Global coupling")
        else:
            pse_params["name"].append("Afferent coupling[" + str(inds) + "]")
        pse_params["indices"].append(inds)

        # Now generate samples using a truncated normal distribution
        pse_params["samples"].append(
            sampler.generate_samples(
                parameter=(
                    0.1 * kloc,  # loc
                    2 * kloc),  # scale
                probability_distribution="uniform",
                low=1.0,
                shape=(1, )))
        # pse_params["samples"].append(
        #     sampler.generate_samples(parameter=(kloc,  # loc
        #                                         30 * param_range),  # scale
        #                              probability_distribution="norm", low=0.0, shape=(1,)))
    pse_params_list = dicts_of_lists_to_lists_of_dicts(pse_params)
    # Add a random jitter to the healthy regions if required...:
    for val in healthy_regions_parameters:
        inds = val.get("indices", healthy_indices)
        name = val.get("name", "x0_values")
        n_params = len(inds)
        samples = sampler.generate_samples(
            parameter=(
                0.0,  # loc
                param_range / 10),  # scale
            probability_distribution="norm",
            shape=(n_params, ))
        for ii in range(n_params):
            pse_params_list.append({
                "path": "model_configuration_builder." + name,
                "samples": samples[ii],
                "indices": [inds[ii]],
                "name": name
            })

    # Now run pse service to generate output samples:
    pse = LSAPSEService(hypothesis=lsa_hypothesis, params_pse=pse_params_list)
    pse_results, execution_status = pse.run_pse(model_connectivity, False,
                                                model_configuration_builder,
                                                lsa_service)
    logger.info(pse.__repr__())
    pse_results = list_of_dicts_to_dicts_of_ndarrays(pse_results)
    for key in pse_results.keys():
        pse_results[key + "_mean"] = np.mean(pse_results[key], axis=0)
        pse_results[key + "_std"] = np.std(pse_results[key], axis=0)
    if save_flag:
        if not (isinstance(filename, basestring)):
            filename = "LSA_PSA"
        writer = H5Writer()
        writer.write_pse_service(
            pse, os.path.join(folder_res, filename + "_pse_service.h5"))
        writer.write_dictionary(pse_results,
                                os.path.join(folder_res, filename + ".h5"))

    return pse_results, pse_params_list
def sensitivity_analysis_pse_from_lsa_hypothesis(n_samples,
                                                 lsa_hypothesis,
                                                 connectivity_matrix,
                                                 model_configuration_builder,
                                                 lsa_service,
                                                 region_labels,
                                                 method="sobol",
                                                 half_range=0.1,
                                                 global_coupling=[],
                                                 healthy_regions_parameters=[],
                                                 save_services=False,
                                                 config=Config(),
                                                 **kwargs):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
    method = method.lower()
    if np.in1d(method, METHODS):
        if np.in1d(method, ["delta", "dgsm"]):
            sampler = "latin"
        elif method == "sobol":
            sampler = "saltelli"
        elif method == "fast":
            sampler = "fast_sampler"
        else:
            sampler = method
    else:
        raise_value_error("Method " + str(method) +
                          " is not one of the available methods " +
                          str(METHODS) + " !")
    all_regions_indices = range(lsa_hypothesis.number_of_regions)
    disease_indices = lsa_hypothesis.regions_disease_indices
    healthy_indices = np.delete(all_regions_indices, disease_indices).tolist()
    pse_params = {"path": [], "indices": [], "name": [], "low": [], "high": []}
    n_inputs = 0
    # First build from the hypothesis the input parameters of the sensitivity analysis.
    # These can be either originating from excitability, epileptogenicity or connectivity hypotheses,
    # or they can relate to the global coupling scaling (parameter K of the model configuration)
    for ii in range(len(lsa_hypothesis.x0_values)):
        n_inputs += 1
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.x0_values")
        pse_params["name"].append(
            str(region_labels[lsa_hypothesis.x0_indices[ii]]) +
            " Excitability")
        pse_params["low"].append(lsa_hypothesis.x0_values[ii] - half_range)
        pse_params["high"].append(
            np.min(
                [MAX_DISEASE_VALUE,
                 lsa_hypothesis.x0_values[ii] + half_range]))
    for ii in range(len(lsa_hypothesis.e_values)):
        n_inputs += 1
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.e_values")
        pse_params["name"].append(
            str(region_labels[lsa_hypothesis.e_indices[ii]]) +
            " Epileptogenicity")
        pse_params["low"].append(lsa_hypothesis.e_values[ii] - half_range)
        pse_params["high"].append(
            np.min(
                [MAX_DISEASE_VALUE, lsa_hypothesis.e_values[ii] + half_range]))
    for ii in range(len(lsa_hypothesis.w_values)):
        n_inputs += 1
        pse_params["indices"].append([ii])
        pse_params["path"].append("hypothesis.w_values")
        inds = linear_index_to_coordinate_tuples(lsa_hypothesis.w_indices[ii],
                                                 connectivity_matrix.shape)
        if len(inds) == 1:
            pse_params["name"].append(
                str(region_labels[inds[0][0]]) + "-" +
                str(region_labels[inds[0][0]]) + " Connectivity")
        else:
            pse_params["name"].append("Connectivity[" + str(inds), + "]")
            pse_params["low"].append(
                np.max([lsa_hypothesis.w_values[ii] - half_range, 0.0]))
            pse_params["high"].append(lsa_hypothesis.w_values[ii] + half_range)
    for val in global_coupling:
        n_inputs += 1
        pse_params["path"].append("model.configuration.service.K_unscaled")
        inds = val.get("indices", all_regions_indices)
        if np.all(inds == all_regions_indices):
            pse_params["name"].append("Global coupling")
        else:
            pse_params["name"].append("Afferent coupling[" + str(inds) + "]")
        pse_params["indices"].append(inds)
        pse_params["low"].append(val.get("low", 0.0))
        pse_params["high"].append(val.get("high", 2.0))
    # Now generate samples suitable for sensitivity analysis
    sampler = SalibSamplerInterface(n_samples=n_samples,
                                    sampler=sampler,
                                    random_seed=kwargs.get(
                                        "random_seed", None))
    input_samples = sampler.generate_samples(low=pse_params["low"],
                                             high=pse_params["high"],
                                             **kwargs)
    n_samples = input_samples.shape[1]
    pse_params.update(
        {"samples": [np.array(value) for value in input_samples.tolist()]})
    pse_params_list = dicts_of_lists_to_lists_of_dicts(pse_params)
    # Add a random jitter to the healthy regions if required...:
    sampler = ProbabilisticSampler(n_samples=n_samples,
                                   random_seed=kwargs.get("random_seed", None))
    for val in healthy_regions_parameters:
        inds = val.get("indices", healthy_indices)
        name = val.get("name", "x0_values")
        n_params = len(inds)
        samples = sampler.generate_samples(
            parameter=(
                kwargs.get("loc", 0.0),  # loc
                kwargs.get("scale", 2 * half_range)),  # scale
            probability_distribution="uniform",
            low=0.0,
            shape=(n_params, ))
        for ii in range(n_params):
            pse_params_list.append({
                "path": "model_configuration_builder." + name,
                "samples": samples[ii],
                "indices": [inds[ii]],
                "name": name
            })
    # Now run pse service to generate output samples:
    pse = LSAPSEService(hypothesis=lsa_hypothesis, params_pse=pse_params_list)
    pse_results, execution_status = pse.run_pse(connectivity_matrix, False,
                                                model_configuration_builder,
                                                lsa_service)
    pse_results = list_of_dicts_to_dicts_of_ndarrays(pse_results)
    # Now prepare inputs and outputs and run the sensitivity analysis:
    # NOTE!: Without the jittered healthy regions which we don' want to include into the sensitivity analysis!
    inputs = dicts_of_lists_to_lists_of_dicts(pse_params)
    outputs = [{
        "names": ["LSA Propagation Strength"],
        "values": pse_results["lsa_propagation_strengths"]
    }]
    sensitivity_analysis_service = SensitivityAnalysisService(
        inputs,
        outputs,
        method=method,
        calc_second_order=kwargs.get("calc_second_order", True),
        conf_level=kwargs.get("conf_level", 0.95))
    results = sensitivity_analysis_service.run(**kwargs)
    if save_services:
        logger.info(pse.__repr__())
        writer = H5Writer()
        writer.write_pse_service(
            pse,
            os.path.join(config.out.FOLDER_RES,
                         method + "_test_pse_service.h5"))
        logger.info(sensitivity_analysis_service.__repr__())
        writer.write_sensitivity_analysis_service(
            sensitivity_analysis_service,
            os.path.join(config.out.FOLDER_RES,
                         method + "_test_sa_service.h5"))
    return results, pse_results
示例#22
0
import os
from tvb_fit.tvb_epilepsy.base.constants.config import Config
from tvb_fit.base.utils.log_error_utils import initialize_logger
from tvb_fit.io.tvb_data_reader import TVBReader
from tvb_fit.io.h5_reader import H5Reader
from tvb_fit.io.h5_writer import H5Writer
from tvb_fit.plot.plotter import Plotter
# input_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "CC", "TVB3", "tvb")
# head_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "CC", "TVB3", "Head")
input_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work',
                            'VBtech', 'VEP', "results", "INS", "JUNCH", "tvb")
head_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work',
                           'VBtech', 'VEP', "results", "INS", "JUNCH", "Head")
output_folder = os.path.join(os.path.expanduser("~"), 'Dropbox', 'Work',
                             'VBtech', 'VEP', "results", "tests")
config = Config(head_folder=input_folder,
                output_base=output_folder,
                data_mode="tvb")  #, data_mode="java"
config.hypothesis.head_folder = head_folder
config.figures.MATPLOTLIB_BACKEND = "inline"
config.figures.SHOW_FLAG = True
logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader()
writer = H5Writer()
plotter = Plotter(config)

logger.info("Reading from: " + config.input.HEAD)
head = reader.read_head(config.input.HEAD,
                        seeg_sensors_files=[("seeg_xyz.txt", )])
print("OK!")
示例#23
0
from tvb_fit.tvb_epilepsy.base.constants.config import Config
from tvb_fit.base.utils.log_error_utils import initialize_logger
from tvb_fit.io.h5_reader import H5Reader
from tvb_fit.io.h5_writer import H5Writer


NUMBER_OF_REGIONS = 87

if __name__ == "__main__":

    User = os.path.expanduser("~")

    base_folder = os.path.join(User, 'Dropbox', 'Work', 'VBtech', 'VEP', "results", "CC")
    output_base = os.path.join(base_folder, "testing_heads")

    config = Config(output_base=output_base, separate_by_run=True)

    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)

    patients_included = []

    attributes = ["areas", "normalized_weights", "weights", "tract_lengths"]
    stats = dict((attribute, []) for attribute in attributes)

    head = None
    for ii in range(1, 31):

        patient = "TVB" + str(ii)
        logger.info("Patient TVB " + str(ii) + ":")

        head_folder = os.path.join(base_folder, patient, "Head")