def launch(self, view_model): # type: (NodeCoherenceModel) -> [CoherenceSpectrumIndex] """ Launch algorithm and build results. :param view_model: the ViewModel keeping the algorithm inputs :return: the node coherence for the specified time series """ # -------------------- Prepare result entities -----------------------## coherence_spectrum_index = CoherenceSpectrumIndex() dest_path = h5.path_for(self.storage_path, CoherenceSpectrumH5, coherence_spectrum_index.gid) coherence_h5 = CoherenceSpectrumH5(dest_path) # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------## time_series_h5 = h5.h5_file_for_index(self.input_time_series_index) input_shape = time_series_h5.data.shape node_slice = [ slice(input_shape[0]), None, slice(input_shape[2]), slice(input_shape[3]) ] # ---------- Iterate over slices and compose final result ------------## small_ts = TimeSeries() small_ts.sample_period = time_series_h5.sample_period.load() small_ts.sample_period_unit = time_series_h5.sample_period_unit.load() partial_coh = None for var in range(input_shape[1]): node_slice[1] = slice(var, var + 1) small_ts.data = time_series_h5.read_data_slice(tuple(node_slice)) partial_coh = calculate_cross_coherence(small_ts, view_model.nfft) coherence_h5.write_data_slice(partial_coh) time_series_h5.close() partial_coh.source.gid = view_model.time_series partial_coh.gid = uuid.UUID(coherence_spectrum_index.gid) coherence_spectrum_index.fill_from_has_traits(partial_coh) self.fill_index_from_h5(coherence_spectrum_index, coherence_h5) coherence_h5.store(partial_coh, scalars_only=True) coherence_h5.frequency.store(partial_coh.frequency) coherence_h5.close() return coherence_spectrum_index
def build(): time_series_index = time_series_index_factory() time_series = h5.load_from_index(time_series_index) cross_coherence = spectral.CoherenceSpectrum(source=time_series, nfft=4, array_data=numpy.random.random((10, 10)), frequency=numpy.random.random((10,))) op = operation_factory() cross_coherence_index = CoherenceSpectrumIndex() cross_coherence_index.fk_from_operation = op.id cross_coherence_index.fill_from_has_traits(cross_coherence) cross_coherence_h5_path = h5.path_for_stored_index(cross_coherence_index) with CoherenceSpectrumH5(cross_coherence_h5_path) as f: f.store(cross_coherence) cross_coherence_index = dao.store_entity(cross_coherence_index) return cross_coherence_index
def launch(self, view_model): # type: (NodeCoherenceModel) -> [CoherenceSpectrumIndex] """ Launch algorithm and build results. """ # --------- Prepare a CoherenceSpectrum object for result ------------## coherence_spectrum_index = CoherenceSpectrumIndex() time_series_h5 = h5.h5_file_for_index(self.input_time_series_index) dest_path = h5.path_for(self.storage_path, CoherenceSpectrumH5, coherence_spectrum_index.gid) coherence_h5 = CoherenceSpectrumH5(dest_path) coherence_h5.gid.store(uuid.UUID(coherence_spectrum_index.gid)) coherence_h5.source.store(time_series_h5.gid.load()) coherence_h5.nfft.store(self.algorithm.nfft) # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------## input_shape = time_series_h5.data.shape node_slice = [ slice(input_shape[0]), None, slice(input_shape[2]), slice(input_shape[3]) ] # ---------- Iterate over slices and compose final result ------------## small_ts = TimeSeries() small_ts.sample_period = time_series_h5.sample_period.load() partial_coh = None for var in range(input_shape[1]): node_slice[1] = slice(var, var + 1) small_ts.data = time_series_h5.read_data_slice(tuple(node_slice)) self.algorithm.time_series = small_ts partial_coh = self.algorithm.evaluate() coherence_h5.write_data_slice(partial_coh) coherence_h5.frequency.store(partial_coh.frequency) coherence_h5.close() coherence_spectrum_index.ndim = len(coherence_h5.array_data.shape) time_series_h5.close() coherence_spectrum_index.fk_source_gid = self.input_time_series_index.gid coherence_spectrum_index.nfft = partial_coh.nfft coherence_spectrum_index.frequencies = partial_coh.frequency return coherence_spectrum_index