def launch(self, data_file, dataset_name, structure_path='', transpose=False, slice=None, sampling_rate=1000, start_time=0, tstype=None, 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 = self.ts_builder[tstype](self, data, **tstype_parameters) ts.start_time = start_time ts.sample_period = 1.0 / sampling_rate ts.sample_period_unit = 's' ts.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.write_data_slice(data[:, numpy.newaxis, :, numpy.newaxis]) ts.close_file() return ts except ParseException as ex: self.log.exception(ex) raise LaunchException(ex)
def get_mapped_array_volume_view(self, mapped_array, x_plane, y_plane, z_plane, mapped_array_slice=None, **kwargs): x_plane, y_plane, z_plane = preprocess_space_parameters( x_plane, y_plane, z_plane, self.length_1d, self.length_2d, self.length_3d) slice_x, slice_y, slice_z = self.get_volume_slice( x_plane, y_plane, z_plane) if mapped_array_slice: matrix_slice = parse_slice(mapped_array_slice) measure = mapped_array.get_data('array_data', matrix_slice) else: measure = mapped_array.get_data('array_data') if measure.shape != (self.connectivity.number_of_regions, ): raise ValueError('cannot project measure on the space') result_x = measure[slice_x] result_y = measure[slice_y] result_z = measure[slice_z] # Voxels outside the brain are -1. The indexing above is incorrect for those voxels as it # associates the values of the last region measure[-1] to them. # Here we replace those values with an out of scale value. result_x[slice_x == -1] = measure.min() - 1 result_y[slice_y == -1] = measure.min() - 1 result_z[slice_z == -1] = measure.min() - 1 return [[result_x.tolist()], [result_y.tolist()], [result_z.tolist()]]
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 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 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 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 test_parse_slice(self): assert parse_slice("::1, :") == (slice(None, None, 1), slice(None, None, None)) assert parse_slice("2") == 2 assert parse_slice("[2]") == 2 assert parse_slice("[]") == slice(None) try: parse_slice(":::::") except ValueError: pass