def create_covariance(self, time_series): """ :returns: a stored DataType Covariance. """ operation, _, storage_path = self.__create_operation() covariance = Covariance(storage_path=storage_path, source=time_series) covariance.write_data_slice(numpy.random.random((10, 10, 10))) adapter_instance = StoreAdapter([covariance]) OperationService().initiate_prelaunch(operation, adapter_instance, {}) return covariance
def build(): ts_index = time_series_factory() ts_h5 = h5_file_for_index(ts_index) ts = TimeSeries() ts_h5.load_into(ts) ts_h5.close() data_shape = ts.data.shape result_shape = (data_shape[2], data_shape[2], data_shape[1], data_shape[3]) result = numpy.zeros(result_shape) for mode in range(data_shape[3]): for var in range(data_shape[1]): data = ts_h5.data[:, var, :, mode] data = data - data.mean(axis=0)[numpy.newaxis, 0] result[:, :, var, mode] = numpy.cov(data.T) covariance = Covariance(source=ts, array_data=result) op = operation_factory() covariance_db = CovarianceIndex() covariance_db.fk_from_operation = op.id covariance_db.fill_from_has_traits(covariance) covariance_h5_path = h5.path_for_stored_index(covariance_db) with TimeSeriesH5(covariance_h5_path) as f: f.store(ts) session.add(covariance_db) session.commit() return covariance_db
def _compute_node_covariance(self, small_ts, input_ts_h5): """ Compute the temporal covariance between nodes in a TimeSeries dataType. A nodes x nodes matrix is returned for each (state-variable, mode). """ data_shape = small_ts.data.shape # (nodes, nodes, state-variables, modes) result_shape = (data_shape[2], data_shape[2], data_shape[1], data_shape[3]) self.log.info("result shape will be: %s" % str(result_shape)) result = numpy.zeros(result_shape) # One inter-node temporal covariance matrix for each state-var & mode. for mode in range(data_shape[3]): for var in range(data_shape[1]): data = input_ts_h5.data[:, var, :, mode] data = data - data.mean(axis=0)[numpy.newaxis, 0] result[:, :, var, mode] = numpy.cov(data.T) self.log.debug("result") self.log.debug(narray_describe(result)) covariance = Covariance(source=small_ts, array_data=result) return covariance
def launch(self, time_series): """ Launch algorithm and build results. :returns: the `Covariance` built with the given timeseries as source """ #Create a FourierSpectrum dataType object. covariance = Covariance(source=time_series, storage_path=self.storage_path) #NOTE: Assumes 4D, Simulator timeSeries. node_slice = [ slice(self.input_shape[0]), None, slice(self.input_shape[2]), None ] for mode in range(self.input_shape[3]): for var in range(self.input_shape[1]): small_ts = TimeSeries(use_storage=False) node_slice[1] = slice(var, var + 1) node_slice[3] = slice(mode, mode + 1) small_ts.data = time_series.read_data_slice(tuple(node_slice)) self.algorithm.time_series = small_ts partial_cov = self.algorithm.evaluate() covariance.write_data_slice(partial_cov.array_data) covariance.close_file() return covariance
def launch(self, time_series): """ Launch algorithm and build results. :returns: the `Covariance` built with the given timeseries as source """ #Create a FourierSpectrum dataType object. covariance = Covariance(source=time_series, storage_path=self.storage_path) #NOTE: Assumes 4D, Simulator timeSeries. node_slice = [slice(self.input_shape[0]), None, slice(self.input_shape[2]), None] for mode in range(self.input_shape[3]): for var in range(self.input_shape[1]): small_ts = TimeSeries(use_storage=False) node_slice[1] = slice(var, var + 1) node_slice[3] = slice(mode, mode + 1) small_ts.data = time_series.read_data_slice(tuple(node_slice)) self.algorithm.time_series = small_ts partial_cov = self.algorithm.evaluate() covariance.write_data_slice(partial_cov.array_data) covariance.close_file() return covariance