Exemple #1
0
def calculate_cross_coherence(time_series, nfft):
    """
    # type: (TimeSeries, int)  -> CoherenceSpectrum
    # Adapter for cross-coherence algorithm(s)
    # Evaluate coherence on time series.

    Parameters
    __________
    time_series : TimeSeries
    The TimeSeries to which the Cross Coherence is to be applied.

    nfft : int
    Data-points per block (should be a power of 2).
    """

    srate = time_series.sample_rate
    coh, freq = _coherence(time_series.data, srate, nfft=nfft)
    log.debug("coherence")
    log.debug(narray_describe(coh))
    log.debug("freq")
    log.debug(narray_describe(freq))

    spec = spectral.CoherenceSpectrum(source=time_series,
                                      nfft=nfft,
                                      array_data=coh.astype(numpy.float),
                                      frequency=freq)
    return spec
Exemple #2
0
 def evaluate(self):
     "Evaluate coherence on time series."
     cls_attr_name = self.__class__.__name__ + ".time_series"
     self.time_series.trait["data"].log_debug(owner=cls_attr_name)
     srate = self.time_series.sample_rate
     coh, freq = coherence(self.time_series.data, srate, nfft=self.nfft)
     util.log_debug_array(LOG, coh, "coherence")
     util.log_debug_array(LOG, freq, "freq")
     spec = spectral.CoherenceSpectrum(source=self.time_series,
                                       nfft=self.nfft,
                                       array_data=coh,
                                       frequency=freq,
                                       use_storage=False)
     return spec
Exemple #3
0
 def test_coherencespectrum(self):
     data = numpy.random.random((10, 10))
     ts = time_series.TimeSeries(data=data)
     dt = spectral.CoherenceSpectrum(source=ts,
                                     nfft = 4,
                                     array_data = numpy.random.random((10, 10)),
                                     frequency = numpy.random.random((10,)))
     summary_info = dt.summary_info 
     self.assertEqual(summary_info['Number of frequencies'], 10)
     self.assertEqual(summary_info['Spectral type'], 'CoherenceSpectrum')
     self.assertEqual(summary_info['FFT length (time-points)'], 4)
     self.assertEqual(summary_info['Source'], '')
     self.assertEqual(dt.nfft, 4)
     self.assertEqual(dt.shape, (10, 10))
     self.assertTrue(dt.source is not None)
Exemple #4
0
 def test_coherencespectrum(self):
     data = numpy.random.random((10, 10))
     ts = time_series.TimeSeries(data=data, title='meh')
     dt = spectral.CoherenceSpectrum(source=ts,
                                     nfft=4,
                                     array_data=numpy.random.random((10, 10)),
                                     frequency=numpy.random.random((10,)))
     summary_info = dt.summary_info()
     assert summary_info['Number of frequencies'] == 10
     assert summary_info['Spectral type'] == 'CoherenceSpectrum'
     assert summary_info['FFT length (time-points)'] == 4
     assert summary_info['Source'] == 'meh'
     assert dt.nfft == 4
     assert dt.array_data.shape == (10, 10)
     assert dt.source is not None
Exemple #5
0
    def evaluate(self):
        "Evaluate coherence on time series."
        cls_attr_name = self.__class__.__name__ + ".time_series"
        # self.time_series.trait["data"].log_debug(owner=cls_attr_name)
        srate = self.time_series.sample_rate
        coh, freq = coherence(self.time_series.data, srate, nfft=self.nfft)
        self.log.debug("coherence")
        self.log.debug(narray_describe(coh))
        self.log.debug("freq")
        self.log.debug(narray_describe(freq))

        spec = spectral.CoherenceSpectrum(source=self.time_series,
                                          nfft=self.nfft,
                                          array_data=coh.astype(numpy.float),
                                          frequency=freq)
        return spec
    def evaluate(self):
        """ 
        Coherence function.  Matplotlib.mlab implementation.
        """
        cls_attr_name = self.__class__.__name__ + ".time_series"
        self.time_series.trait["data"].log_debug(owner=cls_attr_name)

        data_shape = self.time_series.data.shape

        #(frequency, nodes, nodes, state-variables, modes)
        result_shape = (self.nfft / 2 + 1, data_shape[2], data_shape[2],
                        data_shape[1], data_shape[3])
        LOG.info("result shape will be: %s" % str(result_shape))

        result = numpy.zeros(result_shape)

        #TODO: For region level, 4s, 2000Hz, this takes ~2min... (which is stupidly slow)
        #One inter-node coherence, across frequencies for each state-var & mode.
        for mode in range(data_shape[3]):
            for var in range(data_shape[1]):
                data = self.time_series.data[:, var, :, mode]
                data = data - data.mean(axis=0)[numpy.newaxis, :]
                #TODO: Work out a way around the 4 level loop,
                #TODO: coherence isn't directional, so, get rid of redundancy...
                for n1 in range(data_shape[2]):
                    for n2 in range(data_shape[2]):
                        cxy, freq = mlab.cohere(
                            data[:, n1],
                            data[:, n2],
                            NFFT=self.nfft,
                            Fs=self.time_series.sample_rate,
                            detrend=detrend_linear,
                            window=mlab.window_none)
                        result[:, n1, n2, var, mode] = cxy

        util.log_debug_array(LOG, result, "result")
        util.log_debug_array(LOG, freq, "freq")

        coherence = spectral.CoherenceSpectrum(source=self.time_series,
                                               nfft=self.nfft,
                                               array_data=result,
                                               frequency=freq,
                                               use_storage=False)

        return coherence
Exemple #7
0
    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