def read_sensors_of_type(self, sensors_file, type): """ :param sensors_file: Path towards a custom Sensors H5 file type: Senors type :return: Sensors object """ if not os.path.exists(sensors_file): self.logger.warning("Senors file %s does not exist!" % sensors_file) return None self.logger.info("Starting to read sensors of type %s from: %s" % (type, sensors_file)) h5_file = h5py.File(sensors_file, 'r', libver='latest') labels = h5_file['/' + SensorsH5Field.LABELS][()] locations = h5_file['/' + SensorsH5Field.LOCATIONS][()] if '/orientations' in h5_file: orientations = h5_file['/orientations'][()] else: orientations = None if '/' + SensorsH5Field.GAIN_MATRIX in h5_file: gain_matrix = h5_file['/' + SensorsH5Field.GAIN_MATRIX][()] else: gain_matrix = None h5_file.close() sensors = Sensors(labels, locations, orientations=orientations, gain_matrix=gain_matrix, s_type=type) self.logger.info("Successfully read sensors from: %s" % sensors_file) return sensors
def select_signals_seeg(self, signals, rois, auto_selection, **kwargs): sensors = Sensors(self.sensors_labels, self.sensors_locations, gain_matrix=self.gain_matrix) inds = range(signals.shape[1]) head_service = HeadService() if auto_selection.find("rois") >= 0: if sensors.gain_matrix is not None: current_selection = head_service.select_sensors_rois(sensors, kwargs.get("rois", rois), self.signals_inds, kwargs.get("gain_matrix_th", None)) inds = np.where([s in current_selection for s in self.signals_inds])[0] self.signals_inds = np.array(self.signals_inds)[inds].tolist() signals = signals[:, inds] if auto_selection.find("correlation-power") >= 0: power = kwargs.get("power", np.sum((signals - np.mean(signals, axis=0)) ** 2, axis=0) / signals.shape[0]) correlation = kwargs.get("correlation", np.corrcoef(signals.T)) current_selection = head_service.select_sensors_corr(sensors, correlation, self.signals_inds, power=power, n_electrodes=kwargs.get("n_electrodes"), sensors_per_electrode=kwargs.get( "sensors_per_electrode", 1), group_electrodes=kwargs.get("group_electrodes", True)) inds = np.where([s in current_selection for s in self.signals_inds])[0] self.signals_inds = np.array(self.signals_inds)[inds].tolist() elif auto_selection.find("power"): power = kwargs.get("power", np.sum(signals ** 2, axis=0) / signals.shape[0]) inds = select_greater_values_array_inds(power, kwargs.get("power_th", None)) self.signals_inds = (np.array(self.signals_inds)[inds]).tolist() self.n_signals = len(self.signals_inds) return signals[:, inds]
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
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=[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([], []) 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 @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)
def read_sensors(self, filename, root_folder, s_type): filename = ensure_list(filename) path = os.path.join(root_folder, filename[0]) if os.path.isfile(path): if s_type == Sensors.TYPE_EEG: tvb_sensors = sensors.SensorsEEG.from_file(path) elif s_type == Sensors.TYPE_MEG: tvb_sensors = sensors.SensorsMEG.from_file(path) else: tvb_sensors = sensors.SensorsInternal.from_file(path) if len(filename) > 1: gain_matrix = self.read_gain_matrix( os.path.join(root_folder, filename[1]), s_type) else: gain_matrix = np.array([]) return Sensors(tvb_sensors.labels, tvb_sensors.locations, orientations=tvb_sensors.orientations, gain_matrix=gain_matrix, s_type=s_type) else: self.logger.warning("\nNo Sensor file found at path " + path + "!") return None
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