def apply_preprocess(aggregated, devices, method, threshold=np.float32(0.0)):
    """
    Apply the given preprocessing method to the devices.
    """
    if method == 'raw':
        return

    indicators = [d.indicators(threshold) for d in devices]

    if method == 'constant':
        (energies, _) = solve_constant_energy(aggregated, indicators)

        for (e, d) in zip(energies, devices):
            log.info('Setting constant energy %s for device %s.' % (e, d.name))
            d.powers = e * d.indicators(np.float32(10))

    elif method == 'interval':
        energy_dict = confidence_estimator(aggregated, devices, sort_data,
                                           threshold)

        for d in devices:
            log.info('Setting constant energy %s for device %s.' %
                     (energy_dict[d.name], d.name))
            d.powers = energy_dict[d.name] * d.indicators(np.float32(10))

    elif method == 'edge':
        energy_dict = confidence_estimator(aggregated, devices,
                                           get_changed_data, threshold)

        for d in devices:
            log.info('Setting constant energy %s for device %s.' %
                     (energy_dict[d.name], d.name))
            d.powers = energy_dict[d.name] * d.indicators(np.float32(10))

    elif method == 'markov':
        agg = dict((time, power) for (time, power) in enumerate(aggregated))
        device = [i for i in xrange(len(devices))]
        active = {}
        for i, d in enumerate(devices):
            active[(i,0)] = 0
            for t, ind in enumerate(d.indicators(threshold)):
                active[(i,t+1)] = ind * 1

        for d in device:
            print devices[d].name
            try:
                p = fit_data(agg, d, active, 3, device)
                for (t, power) in p.iteritems():
                    devices[d].powers[t-1] = power
            except KeyError:
                devices[d].powers = np.zeros(len(devices[d].powers))
 def test_get_changed_data(self):
     """
     Test the estimator by looking for immediate power changes from a single
     device changing state.
     """
     estimate = confidence_estimator(self.aggregate.powers, self.devices,
                                     get_changed_data)
     estimate_test = {'a': np.float(0.0), 'b': np.float(2.5)}
     self.assertEqual(estimate, estimate_test)
 def test_sort_data(self):
     """Test the estimator by finding intervals of a single active device."""
     estimate = confidence_estimator(self.aggregate.powers, self.devices,
                                     sort_data)
     estimate_test = {'a': np.float32(4.5), 'b': np.float32(-2.5)}
     self.assertEqual(estimate, estimate_test)