def test_store_load_region_mapping(session, connectivity_factory, surface_factory, region_mapping_factory, sensors_factory): connectivity = connectivity_factory(2) conn_idx = ConnectivityIndex() conn_idx.fill_from_has_traits(connectivity) session.add(conn_idx) surface = surface_factory(5) surf_idx = SurfaceIndex() surf_idx.fill_from_has_traits(surface) session.add(surf_idx) region_mapping = region_mapping_factory(surface, connectivity) rm_idx = RegionMappingIndex() rm_idx.fill_from_has_traits(region_mapping) rm_idx.connectivity = conn_idx rm_idx.surface = surf_idx session.add(rm_idx) sensors = sensors_factory("SEEG", 3) sensors_seeg_idx = SensorsIndex() sensors_seeg_idx.fill_from_has_traits(sensors) session.add(sensors_seeg_idx) sensors_eeg = sensors_factory("EEG", 3) sensors_eeg_idx = SensorsIndex() sensors_eeg_idx.fill_from_has_traits(sensors_eeg) session.add(sensors_eeg_idx) time_series = TimeSeries(data=numpy.arange(5)) fcd = Fcd( array_data=numpy.arange(5), source=time_series, ) ts_index = TimeSeriesIndex() ts_index.fill_from_has_traits(time_series) session.add(ts_index) fcd_index = FcdIndex() fcd_index.fill_from_has_traits(fcd) fcd_index.source = ts_index session.add(fcd_index) session.commit() res = session.query(ConnectivityIndex) assert res.count() == 1 assert res[0].number_of_regions == 2 assert res[0].number_of_connections == 4 assert res[0].undirected is True assert res[0].weights_min == 0 res = session.query(SurfaceIndex) assert res.count() == 1 res = session.query(RegionMappingIndex) assert res.count() == 1
def launch(self, view_model): # type: (FCDAdapterModel) -> [FcdIndex, ConnectivityMeasureIndex] """ Launch algorithm and build results. :param view_model: the ViewModel keeping the algorithm inputs :return: the fcd index for the computed fcd matrix on the given time-series, with that sw and that sp """ with h5.h5_file_for_index(self.input_time_series_index) as ts_h5: [fcd, fcd_segmented, eigvect_dict, eigval_dict] = self._compute_fcd_matrix(ts_h5) connectivity_gid = ts_h5.connectivity.load() connectivity = self.load_traited_by_gid(connectivity_gid) result = [ ] # list to store: fcd index, fcd_segmented index (eventually), and connectivity measure indexes # Create an index for the computed fcd. fcd_index = FcdIndex() fcd_h5_path = h5.path_for(self.storage_path, FcdH5, fcd_index.gid) with FcdH5(fcd_h5_path) as fcd_h5: self._populate_fcd_h5(fcd_h5, fcd, fcd_index.gid, self.input_time_series_index.gid, view_model.sw, view_model.sp) self._populate_fcd_index(fcd_index, self.input_time_series_index.gid, fcd_h5) result.append(fcd_index) if np.amax(fcd_segmented) == 1.1: result_fcd_segmented_index = FcdIndex() result_fcd_segmented_h5_path = h5.path_for( self.storage_path, FcdH5, result_fcd_segmented_index.gid) with FcdH5( result_fcd_segmented_h5_path) as result_fcd_segmented_h5: self._populate_fcd_h5(result_fcd_segmented_h5, fcd_segmented, result_fcd_segmented_index.gid, self.input_time_series_index.gid, view_model.sw, view_model.sp) self._populate_fcd_index(result_fcd_segmented_index, self.input_time_series_index.gid, result_fcd_segmented_h5) result.append(result_fcd_segmented_index) for mode in eigvect_dict.keys(): for var in eigvect_dict[mode].keys(): for ep in eigvect_dict[mode][var].keys(): for eig in range(3): cm_data = eigvect_dict[mode][var][ep][eig] measure = ConnectivityMeasure() measure.connectivity = connectivity measure.array_data = cm_data measure.title = "Epoch # %d, eigenvalue = %s, variable = %s, " \ "mode = %s." % (ep, eigval_dict[mode][var][ep][eig], var, mode) cm_index = h5.store_complete(measure, self.storage_path) result.append(cm_index) return result
def launch(self, view_model): # type: (FCDAdapterModel) -> [FcdIndex] """ Launch algorithm and build results. :param time_series: the input time-series index for which fcd matrix should be computed :param sw: length of the sliding window :param sp: spanning time: distance between two consecutive sliding window :returns: the fcd index for the computed fcd matrix on the given time-series, with that sw and that sp :rtype: `FcdIndex`,`ConnectivityMeasureIndex` """ with h5.h5_file_for_index(self.input_time_series_index) as ts_h5: [fcd, fcd_segmented, eigvect_dict, eigval_dict] = self._compute_fcd_matrix(ts_h5) connectivity_gid = ts_h5.connectivity.load() result = [ ] # list to store: fcd index, fcd_segmented index (eventually), and connectivity measure indexes # Create an index for the computed fcd. fcd_index = FcdIndex() fcd_h5_path = h5.path_for(self.storage_path, FcdH5, fcd_index.gid) with FcdH5(fcd_h5_path) as fcd_h5: fcd_array_metadata = self._populate_fcd_h5( fcd_h5, fcd, fcd_index.gid, self.input_time_series_index.gid, view_model.sw, view_model.sp) self._populate_fcd_index(fcd_index, self.input_time_series_index.gid, fcd, fcd_array_metadata) result.append(fcd_index) if np.amax(fcd_segmented) == 1.1: result_fcd_segmented_index = FcdIndex() result_fcd_segmented_h5_path = h5.path_for( self.storage_path, FcdH5, result_fcd_segmented_index.gid) with FcdH5( result_fcd_segmented_h5_path) as result_fcd_segmented_h5: fcd_segmented_metadata = self._populate_fcd_h5( result_fcd_segmented_h5, fcd_segmented, result_fcd_segmented_index.gid, self.input_time_series_index.gid, view_model.sw, view_model.sp) self._populate_fcd_index(result_fcd_segmented_index, self.input_time_series_index.id, fcd_segmented, fcd_segmented_metadata) result.append(result_fcd_segmented_index) for mode in eigvect_dict.keys(): for var in eigvect_dict[mode].keys(): for ep in eigvect_dict[mode][var].keys(): for eig in range(3): cm_data = eigvect_dict[mode][var][ep][eig] cm_index = ConnectivityMeasureIndex() cm_index.type = ConnectivityMeasure.__name__ cm_index.fk_connectivity_gid = connectivity_gid.hex cm_index.title = "Epoch # %d, \n eigenvalue = %s,\n variable = %s,\n " \ "mode = %s." % (ep, eigval_dict[mode][var][ep][eig], var, mode) storage_path = h5.path_for(self.storage_path, ConnectivityMeasureH5, cm_index.gid) with ConnectivityMeasureH5(storage_path) as f: f.array_data.store(cm_data) f.connectivity.store(connectivity_gid) f.title.store(cm_index.title) cm_array_metadata = f.array_data.get_cached_metadata( ) cm_index.array_data_min = cm_array_metadata.min cm_index.array_data_max = cm_array_metadata.max cm_index.array_data_mean = cm_array_metadata.mean result.append(cm_index) return result