Beispiel #1
0
def compute_pca(time_series):
    """
    # type: (TimeSeries)  -> PrincipalComponents
    Compute the temporal covariance between nodes in the time_series.

    Parameters
    __________
    time_series : TimeSeries
    The timeseries to which the PCA is to be applied.
    """

    ts_shape = time_series.data.shape

    # Need more measurements than variables
    if ts_shape[0] < ts_shape[2]:
        msg = "PCA requires a longer timeseries (tpts > number of nodes)."
        log.error(msg)
        raise Exception(msg)

    # (nodes, nodes, state-variables, modes)
    weights_shape = (ts_shape[2], ts_shape[2], ts_shape[1], ts_shape[3])
    log.info("weights shape will be: %s" % str(weights_shape))

    fractions_shape = (ts_shape[2], ts_shape[1], ts_shape[3])
    log.info("fractions shape will be: %s" % str(fractions_shape))

    weights = numpy.zeros(weights_shape)
    fractions = numpy.zeros(fractions_shape)

    # One inter-node temporal covariance matrix for each state-var & mode.
    for mode in range(ts_shape[3]):
        for var in range(ts_shape[1]):
            data = time_series.data[:, var, :, mode]

            fracts, w = _compute_weights_and_fractions(data)
            fractions[:, var, mode] = fracts
            weights[:, :, var, mode] = w

    log.debug("fractions")
    log.debug(narray_describe(fractions))
    log.debug("weights")
    log.debug(narray_describe(weights))

    pca_result = mode_decompositions.PrincipalComponents(
        source=time_series,
        fractions=fractions,
        weights=weights,
        norm_source=numpy.array([]),
        component_time_series=numpy.array([]),
        normalised_component_time_series=numpy.array([]))

    return pca_result
Beispiel #2
0
    def evaluate(self):
        """
        Compute the temporal covariance between nodes in the time_series. 
        """
        cls_attr_name = self.__class__.__name__ + ".time_series"
        # self.time_series.trait["data"].log_debug(owner = cls_attr_name)

        ts_shape = self.time_series.data.shape

        # Need more measurements than variables
        if ts_shape[0] < ts_shape[2]:
            msg = "PCA requires a longer timeseries (tpts > number of nodes)."
            self.log.error(msg)
            raise Exception(msg)

        # (nodes, nodes, state-variables, modes)
        weights_shape = (ts_shape[2], ts_shape[2], ts_shape[1], ts_shape[3])
        self.log.info("weights shape will be: %s" % str(weights_shape))

        fractions_shape = (ts_shape[2], ts_shape[1], ts_shape[3])
        self.log.info("fractions shape will be: %s" % str(fractions_shape))

        weights = numpy.zeros(weights_shape)
        fractions = numpy.zeros(fractions_shape)

        # One inter-node temporal covariance matrix for each state-var & mode.
        for mode in range(ts_shape[3]):
            for var in range(ts_shape[1]):
                data = self.time_series.data[:, var, :, mode]

                data_pca = PCA_mlab(data)
                fractions[:, var, mode] = data_pca.fracs

                weights[:, :, var, mode] = data_pca.Wt

        self.log.debug("fractions")
        self.log.debug(narray_describe(fractions))
        self.log.debug("weights")
        self.log.debug(narray_describe(weights))

        pca_result = mode_decompositions.PrincipalComponents(
            source=self.time_series,
            fractions=fractions,
            weights=weights,
            norm_source=numpy.array([]),
            component_time_series=numpy.array([]),
            normalised_component_time_series=numpy.array([]))

        return pca_result
Beispiel #3
0
 def test_principalcomponents(self):
     data = numpy.random.random((10, 10, 10, 10))
     ts = time_series.TimeSeries(data=data)
     dt = mode_decompositions.PrincipalComponents(source = ts,
                                                 fractions = numpy.random.random((10, 10, 10)),
                                                 weights = data)
     dt.configure()
     dt.compute_norm_source()
     dt.compute_component_time_series()
     dt.compute_normalised_component_time_series()
     summary = dt.summary_info
     assert summary['Mode decomposition type'] == 'PrincipalComponents'
     assert dt.source is not None
     assert dt.weights.shape == (10, 10, 10, 10)
     assert dt.fractions.shape == (10, 10, 10)
     assert dt.norm_source.shape == (10, 10, 10, 10)
     assert dt.component_time_series.shape == (10, 10, 10, 10)
     assert dt.normalised_component_time_series.shape == (10, 10, 10, 10)
Beispiel #4
0
 def evaluate(self):
     """
     Compute the temporal covariance between nodes in the time_series. 
     """
     cls_attr_name = self.__class__.__name__+".time_series"
     self.time_series.trait["data"].log_debug(owner = cls_attr_name)
     
     ts_shape = self.time_series.data.shape
     
     #Need more measurements than variables
     if ts_shape[0] < ts_shape[2]:
         msg = "PCA requires a longer timeseries (tpts > number of nodes)."
         LOG.error(msg)
         raise Exception, msg
     
     #(nodes, nodes, state-variables, modes)
     weights_shape = (ts_shape[2], ts_shape[2], ts_shape[1], ts_shape[3])
     LOG.info("weights shape will be: %s" % str(weights_shape))
     
     fractions_shape = (ts_shape[2], ts_shape[1], ts_shape[3])
     LOG.info("fractions shape will be: %s" % str(fractions_shape))
     
     weights = numpy.zeros(weights_shape)
     fractions = numpy.zeros(fractions_shape)
     
     #One inter-node temporal covariance matrix for each state-var & mode.
     for mode in range(ts_shape[3]):
         for var in range(ts_shape[1]):
             data = self.time_series.data[:, var, :, mode]
             data_pca = mlab.PCA(data)
             fractions[:, var, mode ] = data_pca.fracs
             weights[:, :, var, mode] = data_pca.Wt
     
     util.log_debug_array(LOG, fractions, "fractions")
     util.log_debug_array(LOG, weights, "weights")
     
     pca_result = mode_decompositions.PrincipalComponents(
         source = self.time_series,
         fractions = fractions,
         weights = weights,
         use_storage = False)
     
     return pca_result