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_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!")
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"))
def set_empirical_data(empirical_file, ts_file, head, sensors_lbls, sensor_id=0, seizure_length=SEIZURE_LENGTH, times_on_off=[], label_strip_fun=None, plotter=None, title_prefix="", **kwargs): try: return H5Reader().read_timeseries(ts_file) except: # ... or preprocess empirical data for the first time: if len(sensors_lbls) == 0: sensors_lbls = head.get_sensors_id(sensor_ids=sensor_id).labels signals = prepare_seeg_observable_from_mne_file( empirical_file, head.get_sensors_id(sensor_ids=sensor_id), sensors_lbls, seizure_length, times_on_off, label_strip_fun=label_strip_fun, plotter=plotter, title_prefix=title_prefix, **kwargs) H5Writer().write_timeseries(signals, ts_file) return signals
def _prepare_dummy_head(self): reader = H5Reader() connectivity = reader.read_connectivity( os.path.join(self.config.input.HEAD, "Connectivity.h5")) cort_surface = Surface([], []) seeg_sensors = Sensors(numpy.array(["sens1", "sens2"]), numpy.array([[0, 0, 0], [0, 1, 0]])) head = Head(connectivity, cort_surface, sensorsSEEG=seeg_sensors) return head
def main_h5_model(config=Config()): # -------------------------------Reading data----------------------------------- reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader() writer = H5Writer() logger.info("Reading from: %s", config.input.HEAD) head = reader.read_head(config.input.HEAD) empty_hypothesis = HypothesisBuilder().build_hypothesis() x0_indices = [20] x0_values = [0.9] e_indices = [70] e_values = [0.9] 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() obj = { "hyp_x0_E": hyp_x0_E, "test_dict": { "list0": [ "l00", 1, { "d020": "a", "d021": [True, False, np.inf, np.nan, None, [], (), {}, ""] } ] } } logger.info("Original object: %s", obj) logger.info("Writing object to h5 file...") writer.write_generic( obj, os.path.join(config.out.FOLDER_RES, "test_h5_model.h5")) h5_model1 = read_h5_model(config.out.FOLDER_RES + "/test_h5_model.h5") obj1 = h5_model1.convert_from_h5_model(deepcopy(obj)) if assert_equal_objects(obj, obj1): logger.info("Read identical object: %s", obj1) else: logger.error("Comparison failed!: %s", obj1) h5_model2 = read_h5_model(config.out.FOLDER_RES + "/test_h5_model.h5") obj2 = h5_model2.convert_from_h5_model( obj={"DiseaseHypothesis": empty_hypothesis}) if assert_equal_objects(obj, obj2): logger.info("Read object as dictionary: %s", obj2) else: logger.error("Comparison failed!: %s", obj2)
def from_head_to_hypotheses(ep_name, config, plot_head=False): # -------------------------------Reading model_data----------------------------------- reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader() logger.info("Reading from: " + config.input.HEAD) head = reader.read_head(config.input.HEAD) if plot_head: plotter = Plotter(config) plotter.plot_head(head) # --------------------------Hypothesis definition----------------------------------- # # Manual definition of hypothesis...: # x0_indices = [20] # x0_values = [0.9] # e_indices = [70] # e_values = [0.9] # disease_values = x0_values + e_values # disease_indices = x0_indices + e_indices # ...or reading a custom file: # FOLDER_RES = os.path.join(data_folder, ep_name) hypo_builder = HypothesisBuilder(head.connectivity.number_of_regions, config=config).set_normalize(0.95) # This is an example of Excitability Hypothesis: hyp_x0 = hypo_builder.build_hypothesis_from_file(ep_name) # This is an example of Epileptogenicity Hypothesis: hyp_E = hypo_builder.build_hypothesis_from_file( ep_name, e_indices=hyp_x0.x0_indices) # This is an example of Mixed Hypothesis: x0_indices = [hyp_x0.x0_indices[-1]] x0_values = [hyp_x0.x0_values[-1]] e_indices = hyp_x0.x0_indices[0:-1].tolist() e_values = hyp_x0.x0_values[0:-1].tolist() hyp_x0_E = hypo_builder.set_x0_hypothesis(x0_indices, x0_values). \ set_e_hypothesis(e_indices, e_values).build_hypothesis() hypos = (hyp_x0, hyp_E, hyp_x0_E) return head, hypos
def load_model_data_from_file(self, reset_path=False, **kwargs): model_data_path = kwargs.get("model_data_path", self.model_data_path) if reset_path: self.model_data_path = model_data_path extension = model_data_path.split(".", -1)[-1] if isequal_string(extension, "R"): model_data = rload(model_data_path) elif isequal_string(extension, "npy"): model_data = np.load(model_data_path).item() elif isequal_string(extension, "mat"): model_data = loadmat(model_data_path) elif isequal_string(extension, "pkl"): with open(model_data_path, 'wb') as f: model_data = pickle.load(f) elif isequal_string(extension, "h5"): model_data = H5Reader().read_dictionary(model_data_path) else: raise_not_implemented_error("model_data file (" + model_data_path + ") that are not one of (.R, .npy, .mat, .pkl) cannot be read!") for key in model_data.keys(): if key[:3] == "EPI": del model_data[key] return model_data
def correlate_sensors( empirical_file="/Users/lia.domide/Downloads/TRECempirical/110223B-EEX_0004.EEG.mat", existing_ep_file="/WORK/episense/episense-root/trunk/demo-data/SensorsSEEG_125.h5" ): data = loadmat(empirical_file) desired_labels = [str(i).strip().lower() for i in data["channel_names"]] reader = H5Reader() labels, locations = reader.read_sensors_of_type(existing_ep_file, "SEEG") new_labels = [] new_locations = [] ignored_indices = [] for i, label in enumerate(desired_labels): if label not in labels: print "Ignored channel", label ignored_indices.append(i) continue idx = numpy.where(labels == label) new_labels.append(label) new_locations.append(locations[idx]) H5Writer().write_sensors( Sensors(new_labels, new_locations), os.path.join(os.path.dirname(PATIENT_VIRTUAL_HEAD), "SensorsSEEG_" + str(len(labels)) + ".h5")) return ignored_indices
class SimulatorJava(ABCSimulator): """ From a VEP Hypothesis, write a custom JSON simulation configuration. To run a simulation, we can also open a GUI and import the resulted JSON file. """ logger = initialize_logger(__name__) reader = H5Reader() json_custom_config_file = "SimulationConfiguration.json" def __init__(self, connectivity, model_configuration, simulation_settings): self.model = None self.simulation_settings = simulation_settings self.model_configuration = model_configuration self.connectivity = connectivity self.head_path = os.path.dirname(self.connectivity.file_path) self.json_config_path = os.path.join(self.head_path, self.json_custom_config_file) self.configure_model() def get_vois(self): return self.model.vois @staticmethod def _save_serialized(ep_full_config, result_path): json_text = json.dumps(obj_to_dict(ep_full_config), indent=2) result_file = open(result_path, 'w') result_file.write(json_text) result_file.close() def config_simulation(self): ep_settings = Settings( integration_step=self.simulation_settings.integration_step, noise_seed=self.simulation_settings.noise_seed, simulated_period=self.simulation_settings.simulated_period, downsampling_period=self.simulation_settings. monitor_sampling_period) if isinstance(self.simulation_settings.noise_intensity, (float, int)): self.logger.info("Using uniform noise %s" % self.simulation_settings.noise_intensity) ep_settings.noise_intensity = self.simulation_settings.noise_intensity elif len(self.simulation_settings.noise_intensity ) == JavaEpileptor._nvar: self.logger.info("Using noise/voi %s" % self.simulation_settings.noise_intensity) ep_settings.set_voi_noise_dispersions( self.simulation_settings.noise_intensity, self.connectivity.number_of_regions) elif len( self.simulation_settings.noise_intensity ) == JavaEpileptor._nvar * self.connectivity.number_of_regions: self.logger.info("Using node noise %s" % self.simulation_settings.noise_intensity) ep_settings.set_node_noise_dispersions( self.simulation_settings.noise_intensity) else: self.logger.warning("Could not set noise %s" % self.simulation_settings.noise_intensity) json_model = self.prepare_epileptor_model_for_json( self.connectivity.number_of_regions) # TODO: history length has to be computed given the time delays (i.e., the tract lengths...) # TODO: when dfun is implemented for JavaEpileptor, we can use commented lines with initial_conditions # initial_conditions = self.prepare_initial_conditions(history_length=1) # custom_config = FullConfiguration(connectivity_path=os.path.abspath(self.connectivity.file_path), # epileptor_params=json_model, settings=ep_settings, # initial_states=initial_conditions.flatten(), # initial_states_shape=numpy.array(initial_conditions.shape)) custom_config = FullConfiguration(connectivity_path=os.path.abspath( self.connectivity.file_path), epileptor_params=json_model, settings=ep_settings, initial_states=None, initial_states_shape=None) self._save_serialized(custom_config, self.json_config_path) def launch_simulation(self): opts = "java -Dncsa.hdf.hdf5lib.H5.hdf5lib=" + os.path.join(GenericConfig.LIB_PATH, GenericConfig.HDF5_LIB) + \ " " + "-Djava.library.path=" + GenericConfig.LIB_PATH + " " + "-cp" + " " + GenericConfig.JAR_PATH + \ " " + GenericConfig.JAVA_MAIN_SIM + " " + os.path.abspath(self.json_config_path) + " " + \ os.path.abspath(self.head_path) try: status = subprocess.call(opts, shell=True) print(status) except: status = False self.logger.warning("Something went wrong with this simulation...") time, data = self.reader.read_ts( os.path.join(self.head_path, "full-configuration", "ts.h5")) return time, data, status def prepare_epileptor_model_for_json(self, no_regions=88): epileptor_params_list = [] self.logger.warning("No of regions is " + str(no_regions)) for idx in xrange(no_regions): epileptor_params_list.append( EpileptorParams(self.model.a[idx], self.model.b[idx], self.model.c[idx], self.model.d[idx], self.model.aa[idx], self.model.r[idx], self.model.Kvf[idx], self.model.Kf[idx], self.model.Ks[idx], self.model.tau[idx], self.model.Iext[idx], self.model.Iext2[idx], self.model.slope[idx], self.model.x0[idx], self.model.tt[idx])) return epileptor_params_list def configure_model(self): x0 = calc_x0_val_to_model_x0( self.model_configuration.x0_values, self.model_configuration.yc, self.model_configuration.Iext1, self.model_configuration.a, self.model_configuration.b - self.model_configuration.d) self.model = JavaEpileptor(a=self.model_configuration.a, b=self.model_configuration.b, d=self.model_configuration.d, x0=x0, iext=self.model_configuration.Iext1, ks=self.model_configuration.K, c=self.model_configuration.yc, tt=self.model_configuration.tau1, r=1.0 / self.model_configuration.tau0) def configure_initial_conditions(self, initial_conditions=None): if isinstance(initial_conditions, numpy.ndarray): self.initial_conditions = initial_conditions else: # TODO: have a function to calculate the correct history length when we have time delays self.initial_conditions = self.prepare_initial_conditions( history_length=1)
def main_vep(config=Config(), sim_type="default", test_write_read=False, pse_flag=PSE_FLAG, sa_pse_flag=SA_PSE_FLAG, sim_flag=SIM_FLAG): logger = initialize_logger(__name__, config.out.FOLDER_LOGS) # -------------------------------Reading data----------------------------------- reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader() writer = H5Writer() logger.info("Reading from: " + config.input.HEAD) head = reader.read_head(config.input.HEAD) plotter = Plotter(config) plotter.plot_head(head) if test_write_read: writer.write_head(head, os.path.join(config.out.FOLDER_RES, "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_values = x0_values + e_values # disease_indices = x0_indices + e_indices # ...or reading a custom file: hypo_builder = HypothesisBuilder(head.connectivity.number_of_regions, config=config).set_normalize(0.95) # This is an example of Epileptogenicity Hypothesis: you give as ep all indices for values > 0 hyp_E = hypo_builder.build_hypothesis_from_file(EP_NAME, e_indices=[1, 3, 16, 25]) # print(hyp_E.string_regions_disease(head.connectivity.region_labels)) # This is an example of Excitability Hypothesis: hyp_x0 = hypo_builder.build_hypothesis_from_file(EP_NAME) # # This is an example of Mixed Hypothesis set manually by the user: # x0_indices = [hyp_x0.x0_indices[-1]] # x0_values = [hyp_x0.x0_values[-1]] # e_indices = hyp_x0.x0_indices[0:-1].tolist() # e_values = hyp_x0.x0_values[0:-1].tolist() # hyp_x0_E = hypo_builder.set_x0_hypothesis(x0_indices, x0_values). \ # set_e_hypothesis(e_indices, e_values).build_hypothesis() # This is an example of x0_values mixed Excitability and Epileptogenicity Hypothesis set from file: all_regions_indices = np.array(range(head.number_of_regions)) healthy_indices = np.delete(all_regions_indices, hyp_E.x0_indices + hyp_E.e_indices).tolist() hyp_x0_E = hypo_builder.build_hypothesis_from_file(EP_NAME, e_indices=[16, 25]) hypotheses = (hyp_x0_E, hyp_x0, hyp_E) # --------------------------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, # -multiplicative 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_builder = SimulatorBuilder(config.simulator.MODE) if isequal_string(sim_type, "realistic"): sim_settings = sim_builder.set_model_name("EpileptorDPrealistic").set_simulated_period(50000).build_sim_settings() sim_settings.noise_type = COLORED_NOISE sim_settings.noise_ntau = 10 elif isequal_string(sim_type, "fitting"): sim_settings = sim_builder.set_model_name("EpileptorDP2D").build_sim_settings() sim_settings.noise_intensity = 1e-3 elif isequal_string(sim_type, "paper"): sim_builder.set_model_name("Epileptor") sim_settings = sim_builder.build_sim_settings() else: sim_settings = sim_builder.build_sim_settings() # --------------------------Hypothesis and LSA----------------------------------- for hyp in hypotheses: logger.info("\n\nRunning hypothesis: " + hyp.name) logger.info("\n\nCreating model configuration...") builder = ModelConfigurationBuilder(hyp.number_of_regions) mcs_file = os.path.join(config.out.FOLDER_RES, hyp.name + "_model_config_service.h5") writer.write_model_configuration_builder(builder, mcs_file) if test_write_read: logger.info("Written and read model configuration services are identical?: " + str(assert_equal_objects(builder, reader.read_model_configuration_builder(mcs_file), logger=logger))) if hyp.type == "Epileptogenicity": model_configuration = builder.build_model_from_E_hypothesis(hyp, head.connectivity.normalized_weights) else: model_configuration = builder.build_model_from_hypothesis(hyp, head.connectivity.normalized_weights) 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, "6d", head.connectivity.region_labels, special_idx=hyp_x0.x0_indices + hyp_E.e_indices, zmode="lin", figure_name=hyp.name + "_StateSpace") logger.info("\n\nRunning LSA...") lsa_service = LSAService(eigen_vectors_number=None, weighted_eigenvector_sum=True) 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) if pse_flag: # --------------Parameter Search Exploration (PSE)------------------------------- logger.info("\n\nRunning PSE LSA...") pse_results = pse_from_lsa_hypothesis(lsa_hypothesis, 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}], model_configuration_builder=builder, lsa_service=lsa_service, 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 sensitivity analysis 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(lsa_hypothesis, head.connectivity.normalized_weights, head.connectivity.region_labels, n_samples, method="sobol", param_range=0.1, global_coupling=[{"indices": all_regions_indices, "bounds": [0.0, 2 * builder.K_unscaled[ 0]]}], healthy_regions_parameters=[ {"name": "x0_values", "indices": healthy_indices}], model_configuration_builder=builder, lsa_service=lsa_service, 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-------------------------------------- logger.info("\n\nConfiguring simulation from model_configuration...") model = sim_builder.generate_model(model_configuration) if isequal_string(sim_type, "realistic"): model.tau0 = 30000.0 model.tau1 = 0.2 model.slope = 0.25 elif isequal_string(sim_type, "fitting"): model.tau0 = 10.0 model.tau1 = 0.5 sim, sim_settings, model = sim_builder.build_simulator_TVB_from_model_sim_settings(model_configuration, head.connectivity, model, sim_settings) # Integrator and initial conditions initialization. # By default initial condition is set right on the equilibrium point. writer.write_generic(sim.model, config.out.FOLDER_RES, lsa_hypothesis.name + "_sim_model.h5") logger.info("\n\nSimulating...") ttavg, tavg_data, status = sim.launch_simulation(report_every_n_monitor_steps=100) sim_path = os.path.join(config.out.FOLDER_RES, lsa_hypothesis.name + "_sim_settings.h5") writer.write_simulation_settings(sim.simulation_settings, sim_path) 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.simulation_settings, reader.read_simulation_settings(sim_path), logger=logger))) if not status: logger.warning("\nSimulation failed!") else: time = np.array(ttavg, dtype='float32') output_sampling_time = np.mean(np.diff(time)) tavg_data = tavg_data[:, :, :, 0] logger.info("\n\nSimulated signal return shape: %s", tavg_data.shape) logger.info("Time: %s - %s", time[0], time[-1]) logger.info("Values: %s - %s", tavg_data.min(), tavg_data.max()) # Variables of interest in a dictionary: res_ts = prepare_vois_ts_dict(sim_settings.monitor_expressions, tavg_data) res_ts['time'] = time res_ts['time_units'] = 'msec' res_ts = compute_seeg_and_write_ts_h5_file(config.out.FOLDER_RES, lsa_hypothesis.name + "_ts.h5", sim.model, res_ts, output_sampling_time, sim_settings.simulated_period, hpf_flag=True, hpf_low=10.0, hpf_high=512.0, sensors_list=head.sensorsSEEG) # Plot results if model._ui_name is "EpileptorDP2D": spectral_raster_plot = False trajectories_plot = True else: spectral_raster_plot = "lfp" trajectories_plot = False #TODO: plotting fails when spectral_raster_plot="lfp". Denis will fix this plotter.plot_sim_results(sim.model, lsa_hypothesis.lsa_propagation_indices, res_ts, head.sensorsSEEG, hpf_flag=True, trajectories_plot=trajectories_plot, spectral_raster_plot=False, log_scale=True)
def build_hypothesis_from_file(self, hyp_file, e_indices=None): self.set_diseased_regions_values(H5Reader().read_epileptogenicity(self.config.input.HEAD, name=hyp_file)) if e_indices: self.set_e_indices(e_indices) return self.build_hypothesis()
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") try: if head is not None: del head head = H5Reader().read_head(head_folder) except: head = None logger.warning("Failed reading head of patient TVB " + str(ii) + "!") if not os.path.isdir(head_folder): logger.warning("Head folder of patient TVB " + str(ii) + " is not an existing directory!") continue if head.number_of_regions == NUMBER_OF_REGIONS: logger.info("Number of regions: " + str(head.number_of_regions)) else: logger.warning("Excluding patient TVB " + str(ii) + " because of a head of " + str(head.number_of_regions) + " regions!")
for vector in data: result.append(tuple(vector)) if len(data.shape) == 3: for vector in data: subresult = [] for subvector in vector: subresult.append(tuple(subvector)) result.append(tuple(subresult)) return result if __name__ == "__main__": data, total_time, nr_of_steps, start_time = read_ts( "/WORK/Episense/trunk/demo-data/Head_TREC/epHH/ts.h5") conn = H5Reader().read_connectivity( "/WORK/Episense/trunk/demo-data/Head_TREC/Connectivity.h5") signal = Timeseries( data, OrderedDict({ TimeseriesDimensions.SPACE.value: list(conn.region_labels), TimeseriesDimensions.VARIABLES.value: [PossibleVariables.X1.value, PossibleVariables.X2.value, "c"] }), start_time, total_time / float(nr_of_steps)) timeline = signal.time_line sv = signal.get_state_variable("x1") subspace = signal.get_subspace_by_labels(list(conn.region_labels)[:3]) timewindow = signal.get_time_window(10, 100)
class TestCustomH5Reader(BaseTest): reader = H5Reader() writer = H5Writer() in_head = InputConfig().HEAD not_existent_file = "NotExistent.h5" def test_read_connectivity(self): connectivity = self.reader.read_connectivity( os.path.join(self.in_head, "Connectivity.h5")) assert connectivity is not None assert connectivity.number_of_regions == 76 def test_read_surface(self): surface = self.reader.read_surface( os.path.join(self.in_head, "CorticalSurface.h5")) assert surface is not None assert surface.vertices.shape[0] == 16 def test_read_surface_not_existent_file(self): surface = self.reader.read_surface( os.path.join(self.in_head, self.not_existent_file)) assert surface is None def test_read_region_mapping(self): region_mapping = self.reader.read_region_mapping( os.path.join(self.in_head, "RegionMapping.h5")) assert isinstance(region_mapping, numpy.ndarray) assert region_mapping.size == 16 def test_read_region_mapping_not_existent_file(self): region_mapping = self.reader.read_region_mapping( os.path.join(self.in_head, self.not_existent_file)) assert isinstance(region_mapping, numpy.ndarray) assert region_mapping.size == 0 def test_read_volume_mapping(self): volume_mapping = self.reader.read_volume_mapping( os.path.join(self.in_head, "VolumeMapping.h5")) assert isinstance(volume_mapping, numpy.ndarray) assert volume_mapping.shape == (6, 5, 4) def test_read_volume_mapping_not_existent_file(self): volume_mapping = self.reader.read_volume_mapping( os.path.join(self.in_head, self.not_existent_file)) assert isinstance(volume_mapping, numpy.ndarray) assert volume_mapping.shape == (0, ) def test_sensors_of_type(self): sensors_file = os.path.join(self.in_head, "SensorsSEEG_20.h5") sensors = self.reader.read_sensors_of_type(sensors_file, Sensors.TYPE_SEEG) assert sensors is not None assert sensors.number_of_sensors == 20 def test_sensors_of_type_not_existent_file(self): sensors_file = os.path.join(self.in_head, self.not_existent_file) sensors = self.reader.read_sensors_of_type(sensors_file, Sensors.TYPE_SEEG) assert sensors is None def test_read_sensors(self): sensors_seeg, sensors_eeg, sensors_meg = self.reader.read_sensors( self.in_head) assert len(sensors_seeg) > 0 assert len(sensors_eeg) == 0 assert len(sensors_meg) == 0 assert sensors_seeg[0] is not None assert sensors_seeg[0].number_of_sensors == 20 def test_read_hypothesis(self): test_file = os.path.join(self.config.out.FOLDER_TEMP, "TestHypothesis.h5") hypothesis_builder = HypothesisBuilder(3, self.config) dummy_hypothesis = hypothesis_builder.set_e_hypothesis( [0], [0.6]).build_hypothesis() self.writer.write_hypothesis(dummy_hypothesis, test_file) hypothesis = self.reader.read_hypothesis(test_file) assert dummy_hypothesis.number_of_regions == hypothesis.number_of_regions assert numpy.array_equal(dummy_hypothesis.x0_values, hypothesis.x0_values) assert dummy_hypothesis.x0_indices == hypothesis.x0_indices assert numpy.array_equal(dummy_hypothesis.e_values, hypothesis.e_values) assert dummy_hypothesis.e_indices == hypothesis.e_indices assert numpy.array_equal(dummy_hypothesis.w_values, hypothesis.w_values) assert dummy_hypothesis.w_indices == hypothesis.w_indices assert numpy.array_equal(dummy_hypothesis.lsa_propagation_indices, hypothesis.lsa_propagation_indices) if len(dummy_hypothesis.lsa_propagation_indices) == 0: assert numpy.array_equal([0, 0, 0], hypothesis.lsa_propagation_strengths) else: assert numpy.array_equal( dummy_hypothesis.lsa_propagation_strengths, hypothesis.lsa_propagation_strengths) def test_read_model_configuration(self): test_file = os.path.join(self.config.out.FOLDER_TEMP, "TestModelConfiguration.h5") dummy_mc = ModelConfiguration(x1eq=numpy.array([2.0, 3.0, 1.0]), zmode=None, zeq=numpy.array([3.0, 2.0, 1.0]), model_connectivity=numpy.array( [[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [2.0, 2.0, 2.0]]), Ceq=numpy.array([1.0, 2.0, 3.0])) self.writer.write_model_configuration(dummy_mc, test_file) mc = self.reader.read_model_configuration(test_file) assert numpy.array_equal(dummy_mc.x1eq, mc.x1eq) assert numpy.array_equal(dummy_mc.zeq, mc.zeq) assert numpy.array_equal(dummy_mc.Ceq, mc.Ceq) assert numpy.array_equal(dummy_mc.model_connectivity, mc.model_connectivity) def test_read_lsa_service(self): test_file = os.path.join(self.config.out.FOLDER_TEMP, "TestLSAService.h5") dummy_lsa_service = LSAService() self.writer.write_lsa_service(dummy_lsa_service, test_file) lsa_service = self.reader.read_lsa_service(test_file) assert dummy_lsa_service.eigen_vectors_number_selection == lsa_service.eigen_vectors_number_selection assert dummy_lsa_service.eigen_vectors_number == lsa_service.eigen_vectors_number assert dummy_lsa_service.eigen_values == lsa_service.eigen_values assert dummy_lsa_service.eigen_vectors == lsa_service.eigen_vectors assert dummy_lsa_service.weighted_eigenvector_sum == lsa_service.weighted_eigenvector_sum assert dummy_lsa_service.normalize_propagation_strength == lsa_service.normalize_propagation_strength def test_read_model_configuration_builder(self): test_file = os.path.join(self.config.out.FOLDER_TEMP, "TestModelConfigService.h5") dummy_mc_service = ModelConfigurationBuilder(3) self.writer.write_model_configuration_builder(dummy_mc_service, test_file) mc_service = self.reader.read_model_configuration_builder(test_file) assert dummy_mc_service.number_of_regions == mc_service.number_of_regions assert numpy.array_equal(dummy_mc_service.x0_values, mc_service.x0_values) assert numpy.array_equal(dummy_mc_service.K_unscaled, mc_service.K_unscaled) assert numpy.array_equal(dummy_mc_service.e_values, mc_service.e_values) assert dummy_mc_service.yc == mc_service.yc assert dummy_mc_service.Iext1 == mc_service.Iext1 assert dummy_mc_service.Iext2 == mc_service.Iext2 assert dummy_mc_service.a == mc_service.a assert dummy_mc_service.b == mc_service.b assert dummy_mc_service.d == mc_service.d assert dummy_mc_service.slope == mc_service.slope assert dummy_mc_service.s == mc_service.s assert dummy_mc_service.gamma == mc_service.gamma assert dummy_mc_service.tau1 == mc_service.tau1 assert dummy_mc_service.tau0 == mc_service.tau0 assert dummy_mc_service.zmode == mc_service.zmode assert dummy_mc_service.x1eq_mode == mc_service.x1eq_mode assert dummy_mc_service.K.all() == mc_service.K.all() assert dummy_mc_service.x0cr == mc_service.x0cr assert dummy_mc_service.rx0 == mc_service.rx0 def test_read_simulation_settigs(self): test_file = os.path.join(self.config.out.FOLDER_TEMP, "TestSimSettings.h5") dummy_sim_settings = SimulationSettings() self.writer.write_simulation_settings(dummy_sim_settings, test_file) sim_settings = self.reader.read_simulation_settings(test_file) assert dummy_sim_settings.integration_step == sim_settings.integration_step assert dummy_sim_settings.simulated_period == sim_settings.simulated_period assert dummy_sim_settings.integrator_type == sim_settings.integrator_type assert dummy_sim_settings.noise_type == sim_settings.noise_type assert dummy_sim_settings.noise_ntau == sim_settings.noise_ntau assert dummy_sim_settings.noise_intensity == sim_settings.noise_intensity assert dummy_sim_settings.noise_seed == sim_settings.noise_seed assert dummy_sim_settings.monitor_type == sim_settings.monitor_type assert dummy_sim_settings.monitor_sampling_period == sim_settings.monitor_sampling_period assert dummy_sim_settings.monitor_expressions == sim_settings.monitor_expressions assert numpy.array_equal(dummy_sim_settings.initial_conditions, sim_settings.initial_conditions)
import os from tvb_epilepsy.base.constants.config import Config from tvb_epilepsy.base.utils.log_error_utils import initialize_logger from tvb_epilepsy.io.tvb_data_reader import TVBReader from tvb_epilepsy.io.h5_reader import H5Reader from tvb_epilepsy.io.h5_writer import H5Writer from tvb_epilepsy.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!")
def main_vep(config=Config(), ep_name=EP_NAME, K_unscaled=K_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=1000, 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() logger.info("Reading from: " + config.input.HEAD) head = reader.read_head(config.input.HEAD) plotter = Plotter(config) 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(hyp.number_of_regions, K=K_unscaled, tau1=TAU1_DEF, 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 services 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, head.connectivity.normalized_weights) # Fix healthy regions to default x0s: model_configuration = \ model_config_builder.build_model_from_hypothesis(hyp, head.connectivity.normalized_weights) 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, "6d", head.connectivity.region_labels, special_idx=hyp.regions_disease_indices, zmode="lin", 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, head.connectivity.normalized_weights, 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 sensitivity analysis 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, head.connectivity.normalized_weights, 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) integrator = "HeunStochastic" for sim_type in sim_types: # ------------------------------Simulation-------------------------------------- logger.info( "\n\nConfiguring simulation from model_configuration...") sim_builder = SimulatorBuilder(config.simulator.MODE) if isequal_string(sim_type, "realistic"): model.tau0 = 60000.0 model.tau1 = 0.2 model.slope = 0.25 model.Iext2 = 0.45 model.pmode = np.array( "z") # np.array("None") to opt out for feedback sim_settings = \ sim_builder.set_fs(2048.0).set_fs_monitor(1024.0).set_simulated_period(60000).build_sim_settings() sim_settings.noise_type = COLORED_NOISE sim_settings.noise_ntau = 20 integrator = "Dop853Stochastic" elif isequal_string(sim_type, "fitting"): sim_settings = sim_builder.set_model_name("EpileptorDP2D").set_fs(2048.0).set_fs_monitor(2048.0).\ set_simulated_period(2000).build_sim_settings() sim_settings.noise_intensity = 1e-5 model = sim_builder.generate_model_tvb(model_configuration) model.tau0 = 300.0 model.tau1 = 0.5 elif isequal_string(sim_type, "reduced"): sim_settings = sim_builder.set_model_name("EpileptorDP2D").set_fs(4096.0). \ set_simulated_period(1000).build_sim_settings() model = sim_builder.generate_model_tvb(model_configuration) elif isequal_string(sim_type, "paper"): sim_builder.set_model_name("Epileptor") sim_settings = sim_builder.build_sim_settings() model = sim_builder.generate_model_tvb(model_configuration) else: sim_settings = sim_builder.build_sim_settings() model = sim_builder.generate_model_tvb(model_configuration) sim, sim_settings, model = \ sim_builder.build_simulator_TVB_from_model_sim_settings(model_configuration,head.connectivity, model, sim_settings, integrator=integrator) # Integrator and initial conditions initialization. # By default initial condition is set right on the equilibrium point. writer.write_simulator_model( sim.model, sim.connectivity.number_of_regions, os.path.join(config.out.FOLDER_RES, lsa_hypothesis.name + "_sim_model.h5")) logger.info("\n\nSimulating...") sim_output, status = sim.launch_simulation( report_every_n_monitor_steps=100) sim_path = os.path.join( config.out.FOLDER_RES, lsa_hypothesis.name + "_sim_settings.h5") writer.write_simulation_settings(sim.simulation_settings, sim_path) 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.simulation_settings, reader.read_simulation_settings(sim_path), logger=logger))) if not status: logger.warning("\nSimulation failed!") else: time = np.array(sim_output.time_line).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, model._ui_name + "_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_list=seeg, spectral_raster_plot=False, title_prefix=hyp.name, spectral_options={"log_scale": True})
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) model_configuration = \ ModelConfigurationBuilder(head.connectivity.number_of_regions).\ build_model_from_E_hypothesis(hypothesis, head.connectivity.normalized_weights) statistical_model = SDEProbabilisticModelBuilder(model_config=model_configuration).generate_model() H5Writer().write_probabilistic_model(statistical_model, config.out.FOLDER_RES, "TestStatsModelorig.h5") statistical_model2 = reader.read_probabilistic_model(os.path.join(config.out.FOLDER_RES, "TestStatsModelorig.h5"))
def main_cc_vep(config, head_folder, ep_name="clinical_hypothesis", x0_indices=[], pse_flag=False, sim_flag=True): if not (os.path.isdir(config.out.FOLDER_RES)): os.mkdir(config.out.FOLDER_RES) logger = initialize_logger(__name__, config.out.FOLDER_LOGS) # -------------------------------Reading data----------------------------------- reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader() writer = H5Writer() logger.info("Reading from: %s", head_folder) head = reader.read_head(head_folder) plotter = Plotter(config) plotter.plot_head(head) # --------------------------Hypothesis definition----------------------------------- hypo_builder = HypothesisBuilder(head.connectivity.number_of_regions) all_regions_indices = np.array(range(head.number_of_regions)) # This is an example of Epileptogenicity Hypothesis: hyp_E = hypo_builder.build_hypothesis_from_file(ep_name, x0_indices) # This is an example of Excitability Hypothesis: hyp_x0 = hypo_builder.build_hypothesis_from_file(ep_name) disease_indices = hyp_E.e_indices + hyp_x0.x0_indices healthy_indices = np.delete(all_regions_indices, disease_indices).tolist() if len(x0_indices) > 0: # This is an example of x0_values mixed Excitability and Epileptogenicity Hypothesis: disease_values = reader.read_epileptogenicity(head_folder, name=ep_name) disease_values = disease_values.tolist() x0_values = [] for ix0 in x0_indices: ind = disease_indices.index(ix0) del disease_indices[ind] x0_values.append(disease_values.pop(ind)) e_indices = disease_indices e_values = np.array(disease_values) x0_values = np.array(x0_values) hyp_x0_E = hypo_builder.set_x0_hypothesis( x0_indices, x0_values).set_e_hypothesis(e_indices, e_values).build_hypothesis() hypotheses = (hyp_E, hyp_x0, hyp_x0_E) else: hypotheses = ( hyp_E, hyp_x0, ) # --------------------------Hypothesis and LSA----------------------------------- for hyp in hypotheses: logger.info("Running hypothesis: %s", hyp.name) logger.info("Creating model configuration...") builder = ModelConfigurationBuilder(hyp.number_of_regions) writer.write_model_configuration_builder( builder, os.path.join(config.out.FOLDER_RES, "model_config_service.h5")) if hyp.type == "Epileptogenicity": model_configuration = builder.build_model_from_E_hypothesis( hyp, head.connectivity.normalized_weights) else: model_configuration = builder.build_model_from_hypothesis( hyp, head.connectivity.normalized_weights) writer.write_model_configuration( model_configuration, os.path.join(config.out.FOLDER_RES, "ModelConfiguration.h5")) # Plot nullclines and equilibria of model configuration plotter.plot_state_space(model_configuration, region_labels=head.connectivity.region_labels, special_idx=disease_indices, model="2d", zmode="lin", figure_name=hyp.name + "_StateSpace") logger.info("Running LSA...") lsa_service = LSAService(eigen_vectors_number=None, weighted_eigenvector_sum=True) lsa_hypothesis = lsa_service.run_lsa(hyp, model_configuration) writer.write_hypothesis( lsa_hypothesis, os.path.join(config.out.FOLDER_RES, lsa_hypothesis.name + ".h5")) writer.write_lsa_service( lsa_service, os.path.join(config.out.FOLDER_RES, "lsa_config_service.h5")) plotter.plot_lsa(lsa_hypothesis, model_configuration, lsa_service.weighted_eigenvector_sum, lsa_service.eigen_vectors_number, head.connectivity.region_labels, None) if pse_flag: n_samples = 100 # --------------Parameter Search Exploration (PSE)------------------------------- logger.info("Running PSE LSA...") pse_results = pse_from_lsa_hypothesis( lsa_hypothesis, 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 }], model_configuration_builder=builder, lsa_service=lsa_service, save_flag=True, folder_res=config.out.FOLDER_RES, filename="PSE_LSA", logger=logger)[0] plotter.plot_lsa(lsa_hypothesis, model_configuration, lsa_service.weighted_eigenvector_sum, lsa_service.eigen_vectors_number, head.connectivity.region_labels, pse_results, title="Hypothesis PSE LSA Overview") if sim_flag: config.out.subfolder = "simulations" for folder in (config.out.FOLDER_RES, config.out.FOLDER_FIGURES): if not (os.path.isdir(folder)): os.mkdir(folder) dynamical_models = ["EpileptorDP2D", "EpileptorDPrealistic"] for dynamical_model, sim_type in zip(dynamical_models, ["fitting", "realistic"]): ts_file = None # os.path.join(sim_folder_res, dynamical_model + "_ts.h5") vois_ts_dict = \ from_model_configuration_to_simulation(model_configuration, head, lsa_hypothesis, sim_type=sim_type, dynamical_model=dynamical_model, ts_file=ts_file, plot_flag=True, config=config)
def from_model_configuration_to_simulation(model_configuration, head, lsa_hypothesis, 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): # 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: # ------------------------------Simulation-------------------------------------- hypname = lsa_hypothesis.name.replace("_LSA", "") logger.info("\n\nConfiguring simulation...") if isequal_string(sim_type, "realistic"): sim, sim_settings, dynamical_model = build_simulator_TVB_realistic( model_configuration, head.connectivity) elif isequal_string(sim_type, "fitting"): sim, sim_settings, dynamical_model = build_simulator_TVB_fitting( model_configuration, head.connectivity) elif isequal_string(sim_type, "paper"): sim, sim_settings, dynamical_model = build_simulator_TVB_paper( model_configuration, head.connectivity) else: sim, sim_settings, dynamical_model = build_simulator_TVB_default( model_configuration, head.connectivity) writer = H5Writer() writer.write_simulator_model( sim.model, sim.connectivity.number_of_regions, os.path.join(config.out.FOLDER_RES, hypname + dynamical_model._ui_name + "_model.h5")) sim_output = [] 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...") sim_output, status = sim.launch_simulation( report_every_n_monitor_steps=100) if not status: logger.warning("\nSimulation failed!") else: time = np.array(sim_output.time_line).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.lsa_propagation_indices, seeg_list=seeg, spectral_raster_plot=False, title_prefix=hypname, spectral_options={"log_scale": True}) return {"source": sim_output, "seeg": seeg}, sim
def from_model_configuration_to_simulation(model_configuration, head, lsa_hypothesis, sim_type="realistic", dynamical_model="EpileptorDP2D", ts_file=None, plot_flag=True, config=Config()): # 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 dynamical_model is "EpileptorDP2D": spectral_raster_plot = False trajectories_plot = True else: spectral_raster_plot = False # "lfp" trajectories_plot = False # ------------------------------Simulation-------------------------------------- logger.info("\n\nConfiguring simulation...") if isequal_string(sim_type, "realistic"): sim, sim_settings, dynamical_model = build_simulator_TVB_realistic( model_configuration, head.connectivity) elif isequal_string(sim_type, "fitting"): sim, sim_settings, dynamical_model = build_simulator_TVB_fitting( model_configuration, head.connectivity) elif isequal_string(sim_type, "paper"): sim, sim_settings, dynamical_model = build_simulator_TVB_paper( model_configuration, head.connectivity) else: sim, sim_settings, dynamical_model = build_simulator_TVB_default( model_configuration, head.connectivity) writer = H5Writer() writer.write_generic(sim.model, config.out.FOLDER_RES, dynamical_model._ui_name + "_model.h5") vois_ts_dict = {} if ts_file is not None and os.path.isfile(ts_file): logger.info("\n\nLoading previously simulated time series...") vois_ts_dict = H5Reader().read_dictionary(ts_file) else: logger.info("\n\nSimulating...") ttavg, tavg_data, status = sim.launch_simulation( report_every_n_monitor_steps=100) if not status: logger.warning("\nSimulation failed!") else: time = np.array(ttavg, dtype='float32').flatten() output_sampling_time = np.mean(np.diff(time)) tavg_data = tavg_data[:, :, :, 0] logger.info("\n\nSimulated signal return shape: %s", tavg_data.shape) logger.info("Time: %s - %s", time[0], time[-1]) logger.info("Values: %s - %s", tavg_data.min(), tavg_data.max()) # Variables of interest in a dictionary: vois_ts_dict = prepare_vois_ts_dict( dynamical_model.variables_of_interest, tavg_data) vois_ts_dict['time'] = time vois_ts_dict['time_units'] = 'msec' vois_ts_dict = compute_seeg_and_write_ts_h5_file( config.out.FOLDER_RES, dynamical_model._ui_name + "_ts.h5", sim.model, vois_ts_dict, output_sampling_time, sim_settings.simulated_period, hpf_flag=True, hpf_low=10.0, hpf_high=512.0, sensors_list=head.sensorsSEEG, save_flag=True) if isinstance(ts_file, basestring): writer.write_dictionary( vois_ts_dict, os.path.join(os.path.dirname(ts_file), os.path.basename(ts_file))) if plot_flag and len(vois_ts_dict) > 0: # Plot results Plotter(config).plot_sim_results( sim.model, lsa_hypothesis.lsa_propagation_indices, vois_ts_dict, sensorsSEEG=head.sensorsSEEG, hpf_flag=False, trajectories_plot=trajectories_plot, spectral_raster_plot=spectral_raster_plot, log_scale=True, region_labels=head.connectivity.region_labels) return vois_ts_dict