def launch(self, matfile):
        mat = scipy.io.loadmat(matfile)
        hdr = mat['hdr']
        fs, ns = [hdr[key][0, 0][0, 0] for key in ['Fs', 'nSamples']]

        # the entities to populate
        #ch = Sensors(storage_path=self.storage_path)
        ts = TimeSeries(storage_path=self.storage_path)

        # (nchan x ntime) -> (t, sv, ch, mo)
        dat = mat['dat'].T[:, numpy.newaxis, :, numpy.newaxis]

        # write data
        ts.write_data_slice(dat)

        # fill in header info
        ts.length_1d, ts.length_2d, ts.length_3d, ts.length_4d = dat.shape
        ts.labels_ordering = 'Time 1 Channel 1'.split()
        ts.write_time_slice(numpy.r_[:ns] * 1.0 / fs)
        ts.start_time = 0.0
        ts.sample_period_unit = 's'
        ts.sample_period = 1.0 / float(fs)
        ts.close_file()

        # setup sensors information

        # ch.labels = numpy.array(
        #     [str(l[0]) for l in hdr['label'][0, 0][:, 0]])
        # ch.number_of_sensors = ch.labels.size

        return ts #, ch
Example #2
0
    def launch(self, matfile):
        mat = scipy.io.loadmat(matfile)
        hdr = mat['hdr']
        fs, ns = [hdr[key][0, 0][0, 0] for key in ['Fs', 'nSamples']]

        # the entities to populate
        #ch = Sensors(storage_path=self.storage_path)
        ts = TimeSeries(storage_path=self.storage_path)

        # (nchan x ntime) -> (t, sv, ch, mo)
        dat = mat['dat'].T[:, numpy.newaxis, :, numpy.newaxis]

        # write data
        ts.write_data_slice(dat)

        # fill in header info
        ts.length_1d, ts.length_2d, ts.length_3d, ts.length_4d = dat.shape
        ts.labels_ordering = 'Time 1 Channel 1'.split()
        ts.write_time_slice(numpy.r_[:ns] * 1.0 / fs)
        ts.start_time = 0.0
        ts.sample_period_unit = 's'
        ts.sample_period = 1.0 / float(fs)
        ts.close_file()

        # setup sensors information

        # ch.labels = numpy.array(
        #     [str(l[0]) for l in hdr['label'][0, 0][:, 0]])
        # ch.number_of_sensors = ch.labels.size

        return ts  #, ch
Example #3
0
    def launch(self, view_model):
        # type: (NodeComplexCoherenceModel) -> [ComplexCoherenceSpectrumIndex]
        """
        Launch algorithm and build results.

        :returns: the `ComplexCoherenceSpectrum` built with the given time-series
        """
        # ------- Prepare a ComplexCoherenceSpectrum object for result -------##
        complex_coherence_spectrum_index = ComplexCoherenceSpectrumIndex()
        time_series_h5 = h5.h5_file_for_index(self.input_time_series_index)

        dest_path = h5.path_for(self.storage_path, ComplexCoherenceSpectrumH5,
                                complex_coherence_spectrum_index.gid)
        spectra_h5 = ComplexCoherenceSpectrumH5(dest_path)
        spectra_h5.gid.store(uuid.UUID(complex_coherence_spectrum_index.gid))
        spectra_h5.source.store(time_series_h5.gid.load())

        # ------------------- NOTE: Assumes 4D TimeSeries. -------------------##
        input_shape = time_series_h5.data.shape
        node_slice = [
            slice(input_shape[0]),
            slice(input_shape[1]),
            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()
        small_ts.data = time_series_h5.read_data_slice(tuple(node_slice))
        self.algorithm.time_series = small_ts

        partial_result = self.algorithm.evaluate()
        self.log.debug("got partial_result")
        self.log.debug("partial segment_length is %s" %
                       (str(partial_result.segment_length)))
        self.log.debug("partial epoch_length is %s" %
                       (str(partial_result.epoch_length)))
        self.log.debug("partial windowing_function is %s" %
                       (str(partial_result.windowing_function)))
        # LOG.debug("partial frequency vector is %s" % (str(partial_result.frequency)))

        spectra_h5.write_data_slice(partial_result)
        spectra_h5.segment_length.store(partial_result.segment_length)
        spectra_h5.epoch_length.store(partial_result.epoch_length)
        spectra_h5.windowing_function.store(partial_result.windowing_function)
        # spectra.frequency = partial_result.frequency
        spectra_h5.close()
        time_series_h5.close()

        complex_coherence_spectrum_index.fk_source_gid = self.input_time_series_index.gid
        complex_coherence_spectrum_index.epoch_length = partial_result.epoch_length
        complex_coherence_spectrum_index.segment_length = partial_result.segment_length
        complex_coherence_spectrum_index.windowing_function = partial_result.windowing_function
        complex_coherence_spectrum_index.frequency_step = partial_result.freq_step
        complex_coherence_spectrum_index.max_frequency = partial_result.max_freq

        return complex_coherence_spectrum_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(view_model.time_series)
        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()
        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))
            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)
        array_metadata = coherence_h5.array_data.get_cached_metadata()
        freq_metadata = coherence_h5.frequency.get_cached_metadata()
        coherence_h5.close()
        time_series_h5.close()

        coherence_spectrum_index.array_data_min = array_metadata.min
        coherence_spectrum_index.array_data_max = array_metadata.max
        coherence_spectrum_index.array_data_mean = array_metadata.mean
        coherence_spectrum_index.array_has_complex = array_metadata.has_complex
        coherence_spectrum_index.array_is_finite = array_metadata.is_finite
        coherence_spectrum_index.shape = json.dumps(
            coherence_h5.array_data.shape)
        coherence_spectrum_index.ndim = len(coherence_h5.array_data.shape)
        coherence_spectrum_index.fk_source_gid = self.input_time_series_index.gid
        coherence_spectrum_index.nfft = partial_coh.nfft
        coherence_spectrum_index.frequencies_min = freq_metadata.min
        coherence_spectrum_index.frequencies_max = freq_metadata.max
        coherence_spectrum_index.subtype = CoherenceSpectrum.__name__

        return coherence_spectrum_index
    def launch(self, view_model):
        """ 
        Launch algorithm and build results. 
        """
        # --------- Prepare a WaveletCoefficients object for result ----------##
        frequencies_array = numpy.array([])
        if self.algorithm.frequencies is not None:
            frequencies_array = self.algorithm.frequencies.to_array()

        time_series_h5 = h5.h5_file_for_index(self.input_time_series_index)
        assert isinstance(time_series_h5, TimeSeriesH5)

        wavelet_index = WaveletCoefficientsIndex()
        dest_path = h5.path_for(self.storage_path, WaveletCoefficientsH5,
                                wavelet_index.gid)

        wavelet_h5 = WaveletCoefficientsH5(path=dest_path)
        wavelet_h5.gid.store(uuid.UUID(wavelet_index.gid))
        wavelet_h5.source.store(time_series_h5.gid.load())
        wavelet_h5.mother.store(self.algorithm.mother)
        wavelet_h5.q_ratio.store(self.algorithm.q_ratio)
        wavelet_h5.sample_period.store(self.algorithm.sample_period)
        wavelet_h5.frequencies.store(frequencies_array)
        wavelet_h5.normalisation.store(self.algorithm.normalisation)

        # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------##
        node_slice = [
            slice(self.input_shape[0]),
            slice(self.input_shape[1]), None,
            slice(self.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()
        for node in range(self.input_shape[2]):
            node_slice[2] = slice(node, node + 1)
            small_ts.data = time_series_h5.read_data_slice(tuple(node_slice))
            self.algorithm.time_series = small_ts
            partial_wavelet = self.algorithm.evaluate()
            wavelet_h5.write_data_slice(partial_wavelet)

        wavelet_h5.close()
        time_series_h5.close()

        wavelet_index.fk_source_gid = self.input_time_series_index.gid
        wavelet_index.mother = self.algorithm.mother
        wavelet_index.normalisation = self.algorithm.normalisation
        wavelet_index.q_ratio = self.algorithm.q_ratio
        wavelet_index.sample_period = self.algorithm.sample_period
        wavelet_index.number_of_scales = frequencies_array.shape[0]
        wavelet_index.frequencies_min, wavelet_index.frequencies_max, _ = from_ndarray(
            frequencies_array)

        return wavelet_index
Example #6
0
    def launch(self, view_model):
        # type: (CrossCorrelateAdapterModel) -> [CrossCorrelationIndex]
        """ 
        Launch algorithm and build results.
        Compute the node-pairwise cross-correlation of the source 4D TimeSeries represented by the index given as input.

        Return a CrossCorrelationIndex. Create a CrossCorrelationH5 that contains the cross-correlation
        sequences for all possible combinations of the nodes.

        See: http://www.scipy.org/doc/api_docs/SciPy.signal.signaltools.html#correlate

        :param time_series: the input time series index for which the correlation should be computed
        :returns: the cross correlation index for the given time series
        :rtype: `CrossCorrelationIndex`
        """
        # --------- Prepare CrossCorrelationIndex and CrossCorrelationH5 objects for result ------------##
        cross_corr_index = CrossCorrelationIndex()
        cross_corr_h5_path = h5.path_for(self.storage_path, CrossCorrelationH5, cross_corr_index.gid)
        cross_corr_h5 = CrossCorrelationH5(cross_corr_h5_path)

        node_slice = [slice(self.input_shape[0]), None, slice(self.input_shape[2]), slice(self.input_shape[3])]
        # ---------- Iterate over slices and compose final result ------------##
        small_ts = TimeSeries()

        with h5.h5_file_for_index(self.input_time_series_index) as ts_h5:
            small_ts.sample_period = ts_h5.sample_period.load()
            small_ts.sample_period_unit = ts_h5.sample_period_unit.load()
            partial_cross_corr = None
            labels_ordering = ts_h5.labels_ordering.load()
            for var in range(self.input_shape[1]):
                node_slice[1] = slice(var, var + 1)
                small_ts.data = ts_h5.read_data_slice(tuple(node_slice))
                partial_cross_corr = self._compute_cross_correlation(small_ts, ts_h5)
                cross_corr_h5.write_data_slice(partial_cross_corr)
            ts_array_metadata = cross_corr_h5.array_data.get_cached_metadata()

        cross_corr_h5.time.store(partial_cross_corr.time)
        cross_corr_labels_ordering = list(partial_cross_corr.labels_ordering)
        cross_corr_labels_ordering[1] = labels_ordering[2]
        cross_corr_labels_ordering[2] = labels_ordering[2]
        cross_corr_h5.labels_ordering.store(json.dumps(tuple(cross_corr_labels_ordering)))
        cross_corr_h5.source.store(uuid.UUID(self.input_time_series_index.gid))
        cross_corr_h5.gid.store(uuid.UUID(cross_corr_index.gid))

        cross_corr_index.fk_source_gid = self.input_time_series_index.gid
        cross_corr_index.labels_ordering = cross_corr_h5.labels_ordering.load()
        cross_corr_index.type = type(cross_corr_index).__name__
        cross_corr_index.array_data_min = ts_array_metadata.min
        cross_corr_index.array_data_max = ts_array_metadata.max
        cross_corr_index.array_data_mean = ts_array_metadata.mean

        cross_corr_h5.close()
        return cross_corr_index
Example #7
0
    def launch(self, view_model):
        # type: (WaveletAdapterModel) -> (WaveletCoefficientsIndex)
        """ 
        Launch algorithm and build results.
        :param view_model: the ViewModel keeping the algorithm inputs
        :return: the wavelet coefficients for the specified time series
        """
        frequencies_array = numpy.array([])
        if view_model.frequencies is not None:
            frequencies_array = view_model.frequencies.to_array()

        time_series_h5 = h5.h5_file_for_index(self.input_time_series_index)
        assert isinstance(time_series_h5, TimeSeriesH5)

        # --------------------- Prepare result entities ----------------------##
        wavelet_index = WaveletCoefficientsIndex()
        dest_path = self.path_for(WaveletCoefficientsH5, wavelet_index.gid)
        wavelet_h5 = WaveletCoefficientsH5(path=dest_path)

        # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------##
        node_slice = [
            slice(self.input_shape[0]),
            slice(self.input_shape[1]), None,
            slice(self.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()
        for node in range(self.input_shape[2]):
            node_slice[2] = slice(node, node + 1)
            small_ts.data = time_series_h5.read_data_slice(tuple(node_slice))
            partial_wavelet = compute_continuous_wavelet_transform(
                small_ts, view_model.frequencies, view_model.sample_period,
                view_model.q_ratio, view_model.normalisation,
                view_model.mother)
            wavelet_h5.write_data_slice(partial_wavelet)

        time_series_h5.close()

        partial_wavelet.source.gid = view_model.time_series
        partial_wavelet.gid = uuid.UUID(wavelet_index.gid)

        wavelet_index.fill_from_has_traits(partial_wavelet)
        self.fill_index_from_h5(wavelet_index, wavelet_h5)

        wavelet_h5.store(partial_wavelet, scalars_only=True)
        wavelet_h5.frequencies.store(frequencies_array)
        wavelet_h5.close()

        return wavelet_index
    def launch(self, view_model):
        # type: (BalloonModelAdapterModel) -> [TimeSeriesRegionIndex]
        """
        Launch algorithm and build results.

        :param time_series: the input time-series used as neural activation in the Balloon Model
        :returns: the simulated BOLD signal
        :rtype: `TimeSeries`
        """
        input_time_series_h5 = h5.h5_file_for_index(
            self.input_time_series_index)
        time_line = input_time_series_h5.read_time_page(0, self.input_shape[0])

        bold_signal_index = TimeSeriesRegionIndex()
        bold_signal_h5_path = h5.path_for(self.storage_path,
                                          TimeSeriesRegionH5,
                                          bold_signal_index.gid)
        bold_signal_h5 = TimeSeriesRegionH5(bold_signal_h5_path)
        bold_signal_h5.gid.store(uuid.UUID(bold_signal_index.gid))
        self._fill_result_h5(bold_signal_h5, input_time_series_h5)

        # ---------- Iterate over slices and compose final result ------------##

        node_slice = [
            slice(self.input_shape[0]),
            slice(self.input_shape[1]), None,
            slice(self.input_shape[3])
        ]
        small_ts = TimeSeries()
        small_ts.sample_period = self.input_time_series_index.sample_period
        small_ts.sample_period_unit = self.input_time_series_index.sample_period_unit
        small_ts.time = time_line

        for node in range(self.input_shape[2]):
            node_slice[2] = slice(node, node + 1)
            small_ts.data = input_time_series_h5.read_data_slice(
                tuple(node_slice))
            self.algorithm.time_series = small_ts
            partial_bold = self.algorithm.evaluate()
            bold_signal_h5.write_data_slice_on_grow_dimension(
                partial_bold.data, grow_dimension=2)

        bold_signal_h5.write_time_slice(time_line)
        bold_signal_shape = bold_signal_h5.data.shape
        bold_signal_h5.nr_dimensions.store(len(bold_signal_shape))
        bold_signal_h5.close()
        input_time_series_h5.close()

        self._fill_result_index(bold_signal_index, bold_signal_shape)
        return bold_signal_index
Example #9
0
    def _create_timeseries(self):
        """Launch adapter to persist a TimeSeries entity"""
        storage_path = FilesHelper().get_project_folder(self.test_project, str(self.operation.id))

        time_series = TimeSeries()
        time_series.sample_period = 10.0
        time_series.start_time = 0.0
        time_series.storage_path = storage_path
        time_series.write_data_slice(numpy.array([1.0, 2.0, 3.0]))
        time_series.close_file()
        time_series.sample_period_unit = 'ms'

        self._store_entity(time_series, "TimeSeries", "tvb.datatypes.time_series")
        count_ts = self.count_all_entities(TimeSeries)
        self.assertEqual(1, count_ts, "Should be only one TimeSeries")
Example #10
0
    def _create_timeseries(self):
        """Launch adapter to persist a TimeSeries entity"""
        storage_path = FilesHelper().get_project_folder(self.test_project, str(self.operation.id))

        time_series = TimeSeries()
        time_series.sample_period = 10.0
        time_series.start_time = 0.0
        time_series.storage_path = storage_path
        time_series.write_data_slice(numpy.array([1.0, 2.0, 3.0]))
        time_series.close_file()
        time_series.sample_period_unit = 'ms'

        self._store_entity(time_series, "TimeSeries", "tvb.datatypes.time_series")
        count_ts = self.count_all_entities(TimeSeries)
        self.assertEqual(1, count_ts, "Should be only one TimeSeries")
Example #11
0
    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 launch(self, view_model):
        # type: (CrossCorrelateAdapterModel) -> [CrossCorrelationIndex]
        """ 
        Launch algorithm and build results.
        Compute the node-pairwise cross-correlation of the source 4D TimeSeries represented by the index given as input.

        Return a CrossCorrelationIndex. Create a CrossCorrelationH5 that contains the cross-correlation
        sequences for all possible combinations of the nodes.

        See: http://www.scipy.org/doc/api_docs/SciPy.signal.signaltools.html#correlate

        :param view_model: the ViewModel keeping the algorithm inputs
        :return: the cross correlation index for the given time series
        :rtype: `CrossCorrelationIndex`
        """
        # --------- Prepare CrossCorrelationIndex and CrossCorrelationH5 objects for result ------------##
        cross_corr_index = CrossCorrelationIndex()
        cross_corr_h5_path = h5.path_for(self.storage_path, CrossCorrelationH5, cross_corr_index.gid)
        cross_corr_h5 = CrossCorrelationH5(cross_corr_h5_path)

        node_slice = [slice(self.input_shape[0]), None, slice(self.input_shape[2]), slice(self.input_shape[3])]
        # ---------- Iterate over slices and compose final result ------------##
        small_ts = TimeSeries()

        with h5.h5_file_for_index(self.input_time_series_index) as ts_h5:
            small_ts.sample_period = ts_h5.sample_period.load()
            small_ts.sample_period_unit = ts_h5.sample_period_unit.load()
            partial_cross_corr = None
            for var in range(self.input_shape[1]):
                node_slice[1] = slice(var, var + 1)
                small_ts.data = ts_h5.read_data_slice(tuple(node_slice))
                partial_cross_corr = self._compute_cross_correlation(small_ts, ts_h5)
                cross_corr_h5.write_data_slice(partial_cross_corr)

        partial_cross_corr.source.gid = view_model.time_series
        partial_cross_corr.gid = uuid.UUID(cross_corr_index.gid)

        cross_corr_index.fill_from_has_traits(partial_cross_corr)
        self.fill_index_from_h5(cross_corr_index, cross_corr_h5)

        cross_corr_h5.store(partial_cross_corr, scalars_only=True)
        cross_corr_h5.close()

        return cross_corr_index
Example #13
0
    def launch(self, view_model):
        # type: (FFTAdapterModel) -> [FourierSpectrumIndex]
        """
        Launch algorithm and build results.
        :param view_model: the ViewModel keeping the algorithm inputs
        :return: the fourier spectrum for the specified time series
        """
        block_size = int(math.floor(self.input_shape[2] / self.memory_factor))
        blocks = int(math.ceil(self.input_shape[2] / block_size))

        input_time_series_h5 = h5.h5_file_for_index(
            self.input_time_series_index)

        # --------------------- Prepare result entities ----------------------
        fft_index = FourierSpectrumIndex()
        dest_path = self.path_for(FourierSpectrumH5, fft_index.gid)
        spectra_file = FourierSpectrumH5(dest_path)

        # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------
        node_slice = [
            slice(self.input_shape[0]),
            slice(self.input_shape[1]), None,
            slice(self.input_shape[3])
        ]

        # ---------- Iterate over slices and compose final result ------------
        small_ts = TimeSeries()
        small_ts.sample_period = input_time_series_h5.sample_period.load()
        small_ts.sample_period_unit = input_time_series_h5.sample_period_unit.load(
        )

        for block in range(blocks):
            node_slice[2] = slice(
                block * block_size,
                min([(block + 1) * block_size, self.input_shape[2]]), 1)
            small_ts.data = input_time_series_h5.read_data_slice(
                tuple(node_slice))

            partial_result = compute_fast_fourier_transform(
                small_ts, view_model.segment_length,
                view_model.window_function, view_model.detrend)

            if blocks <= 1 and len(partial_result.array_data) == 0:
                self.add_operation_additional_info(
                    "Fourier produced empty result (most probably due to a very short input TimeSeries)."
                )
                return None
            spectra_file.write_data_slice(partial_result)

        input_time_series_h5.close()

        # ---------------------------- Fill results ----------------------------
        partial_result.source.gid = view_model.time_series
        partial_result.gid = uuid.UUID(fft_index.gid)

        fft_index.fill_from_has_traits(partial_result)
        self.fill_index_from_h5(fft_index, spectra_file)

        spectra_file.store(partial_result, scalars_only=True)
        spectra_file.windowing_function.store(view_model.window_function)
        spectra_file.close()

        self.log.debug("partial segment_length is %s" %
                       (str(partial_result.segment_length)))
        return fft_index
Example #14
0
    def launch(self, view_model):
        # type: (FFTAdapterModel) -> [FourierSpectrumIndex]
        """
        Launch algorithm and build results.

        :param time_series: the input time series to which the fft is to be applied
        :param segment_length: the block size which determines the frequency resolution \
                               of the resulting power spectra
        :param window_function: windowing functions can be applied before the FFT is performed
        :type  window_function: None; ‘hamming’; ‘bartlett’; ‘blackman’; ‘hanning’
        :returns: the fourier spectrum for the specified time series
        :rtype: `FourierSpectrumIndex`

        """
        fft_index = FourierSpectrumIndex()
        fft_index.fk_source_gid = view_model.time_series.hex

        block_size = int(math.floor(self.input_shape[2] / self.memory_factor))
        blocks = int(math.ceil(self.input_shape[2] / block_size))

        input_time_series_h5 = h5.h5_file_for_index(
            self.input_time_series_index)

        dest_path = h5.path_for(self.storage_path, FourierSpectrumH5,
                                fft_index.gid)
        spectra_file = FourierSpectrumH5(dest_path)
        spectra_file.gid.store(uuid.UUID(fft_index.gid))
        spectra_file.source.store(uuid.UUID(self.input_time_series_index.gid))

        # ------------- NOTE: Assumes 4D, Simulator timeSeries. --------------
        node_slice = [
            slice(self.input_shape[0]),
            slice(self.input_shape[1]), None,
            slice(self.input_shape[3])
        ]

        # ---------- Iterate over slices and compose final result ------------
        small_ts = TimeSeries()
        small_ts.sample_period = input_time_series_h5.sample_period.load()
        small_ts.sample_period_unit = input_time_series_h5.sample_period_unit.load(
        )

        for block in range(blocks):
            node_slice[2] = slice(
                block * block_size,
                min([(block + 1) * block_size, self.input_shape[2]]), 1)
            small_ts.data = input_time_series_h5.read_data_slice(
                tuple(node_slice))
            self.algorithm.time_series = small_ts
            partial_result = self.algorithm.evaluate()

            if blocks <= 1 and len(partial_result.array_data) == 0:
                self.add_operation_additional_info(
                    "Fourier produced empty result (most probably due to a very short input TimeSeries)."
                )
                return None
            spectra_file.write_data_slice(partial_result)
        fft_index.ndim = len(spectra_file.array_data.shape)
        input_time_series_h5.close()

        fft_index.windowing_function = self.algorithm.window_function
        fft_index.segment_length = self.algorithm.segment_length
        fft_index.detrend = self.algorithm.detrend
        fft_index.frequency_step = partial_result.freq_step
        fft_index.max_frequency = partial_result.max_freq

        spectra_file.segment_length.store(self.algorithm.segment_length)
        spectra_file.windowing_function.store(
            str(self.algorithm.window_function))
        spectra_file.close()

        self.log.debug("partial segment_length is %s" %
                       (str(partial_result.segment_length)))
        return fft_index