def compute_2d_view(self, dtm_index, slice_s): # type: (DataTypeMatrix, str) -> (numpy.array, str, bool) """ Create a 2d view of the matrix using the suggested slice If the given slice is invalid or fails to produce a 2d array the default is used which selects the first 2 dimensions. If the matrix is complex the real part is shown :param dtm_index: main input. It can have more then 2D :param slice_s: a string representation of a slice :return: (a 2d array, the slice used to make it, is_default_returned) """ default = (slice(None), slice(None)) + tuple( 0 for _ in range(dtm_index.ndim - 2)) # [:,:,0,0,0,0 etc] slice_used = default try: if slice_s is not None and slice_s != "": slice_used = parse_slice(slice_s) except ValueError: # if the slice could not be parsed self.log.warning("failed to parse the slice") try: with h5.h5_file_for_index(dtm_index) as h5_file: result_2d = h5_file.array_data[slice_used] result_2d = result_2d.astype(float) if result_2d.ndim > 2: # the slice did not produce a 2d array, treat as error raise ValueError(str(dtm_index.shape)) except (ValueError, IndexError, TypeError): # if the slice failed to produce a 2d array self.log.warning("failed to produce a 2d array") return self.compute_2d_view(dtm_index, "") return result_2d, slice_str(slice_used), slice_used == default
def compute_2d_view(matrix, slice_s): """ Create a 2d view of the matrix using the suggested slice If the given slice is invalid or fails to produce a 2d array the default is used which selects the first 2 dimensions. If the matrix is complex the real part is shown :param matrix: main input. It can have more then 2D :param slice_s: a string representation of a slice :return: (a 2d array, the slice used to make it, is_default_returned) """ default = (slice(None), slice(None)) + tuple( 0 for _ in range(matrix.ndim - 2)) # [:,:,0,0,0,0 etc] try: if slice_s is not None: matrix_slice = parse_slice(slice_s) else: matrix_slice = slice(None) m = matrix[matrix_slice] if m.ndim > 2: # the slice did not produce a 2d array, treat as error raise ValueError(str(matrix.shape)) except ( IndexError, ValueError ): # if the slice could not be parsed or it failed to produce a 2d array matrix_slice = default slice_used = slice_str(matrix_slice) return matrix[matrix_slice].astype( float), slice_used, matrix_slice == default
def launch(self, data_file, dataset_name, structure_path='', transpose=False, slice=None, sampling_rate=1000, start_time=0, tstype_parameters=None): try: data = read_nested_mat_file(data_file, dataset_name, structure_path) if transpose: data = data.T if slice: data = data[parse_slice(slice)] ts, ts_idx, ts_h5 = self.ts_builder[self.tstype](self, data.shape, tstype_parameters) ts.start_time = start_time ts.sample_period_unit = 's' ts_h5.write_time_slice(numpy.r_[:data.shape[0]] * ts.sample_period) # we expect empirical data shape to be time, channel. # But tvb expects time, state, channel, mode. Introduce those dimensions ts_h5.write_data_slice(data[:, numpy.newaxis, :, numpy.newaxis]) data_shape = ts_h5.read_data_shape() ts_h5.nr_dimensions.store(len(data_shape)) ts_h5.gid.store(uuid.UUID(ts_idx.gid)) ts_h5.sample_period.store(ts.sample_period) ts_h5.sample_period_unit.store(ts.sample_period_unit) ts_h5.sample_rate.store(ts.sample_rate) ts_h5.start_time.store(ts.start_time) ts_h5.labels_ordering.store(ts.labels_ordering) ts_h5.labels_dimensions.store(ts.labels_dimensions) ts_h5.title.store(ts.title) ts_h5.close() ts_idx.title = ts.title ts_idx.time_series_type = type(ts).__name__ ts_idx.sample_period_unit = ts.sample_period_unit ts_idx.sample_period = ts.sample_period ts_idx.sample_rate = ts.sample_rate ts_idx.labels_dimensions = json.dumps(ts.labels_dimensions) ts_idx.labels_ordering = json.dumps(ts.labels_ordering) ts_idx.data_ndim = len(data_shape) ts_idx.data_length_1d, ts_idx.data_length_2d, ts_idx.data_length_3d, ts_idx.data_length_4d = prepare_array_shape_meta( data_shape) return ts_idx except ParseException as ex: self.log.exception(ex) raise LaunchException(ex)
def launch(self, view_model): # type: (RegionMatTimeSeriesImporterModel) -> [TimeSeriesRegionIndex, TimeSeriesEEGIndex] try: data = read_nested_mat_file(view_model.data_file, view_model.dataset_name, view_model.structure_path) if view_model.transpose: data = data.T if view_model.slice: data = data[parse_slice(view_model.slice)] datatype_index = self.load_entity_by_gid(view_model.datatype) ts, ts_idx, ts_h5 = self.ts_builder[self.tstype](self, data.shape, datatype_index) ts.start_time = view_model.start_time ts.sample_period_unit = 's' ts_h5.write_time_slice(numpy.r_[:data.shape[0]] * ts.sample_period) # we expect empirical data shape to be time, channel. # But tvb expects time, state, channel, mode. Introduce those dimensions ts_h5.write_data_slice(data[:, numpy.newaxis, :, numpy.newaxis]) data_shape = ts_h5.read_data_shape() ts_h5.nr_dimensions.store(len(data_shape)) ts_h5.gid.store(uuid.UUID(ts_idx.gid)) ts_h5.sample_period.store(ts.sample_period) ts_h5.sample_period_unit.store(ts.sample_period_unit) ts_h5.sample_rate.store(ts.sample_rate) ts_h5.start_time.store(ts.start_time) ts_h5.labels_ordering.store(ts.labels_ordering) ts_h5.labels_dimensions.store(ts.labels_dimensions) ts_h5.title.store(ts.title) ts_h5.close() ts_idx.title = ts.title ts_idx.time_series_type = type(ts).__name__ ts_idx.sample_period_unit = ts.sample_period_unit ts_idx.sample_period = ts.sample_period ts_idx.sample_rate = ts.sample_rate ts_idx.labels_dimensions = json.dumps(ts.labels_dimensions) ts_idx.labels_ordering = json.dumps(ts.labels_ordering) ts_idx.data_ndim = len(data_shape) ts_idx.data_length_1d, ts_idx.data_length_2d, ts_idx.data_length_3d, ts_idx.data_length_4d = prepare_array_shape_meta( data_shape) return ts_idx except ParseException as ex: self.log.exception(ex) raise LaunchException(ex)