Ejemplo n.º 1
0
def create_preprocessing_pipeline(power_scaler):
    pipeline = Pipeline(path="../results/preprocessing")

    # Deal with missing values through linear interpolation
    imputer_power_statistics = LinearInterpolater(method="nearest", dim="time",
                                                  name="imputer_power")(x=pipeline["scaler_power"])
    # Scale the data using a standard SKLearn scaler
    scale_power_statistics = power_scaler(x=imputer_power_statistics)

    # Create lagged time series to later be used in the regression
    ClockShift(lag=1)(x=scale_power_statistics)
    ClockShift(lag=2)(x=scale_power_statistics)
    return pipeline
Ejemplo n.º 2
0
class TestClockShift(unittest.TestCase):
    def setUp(self):
        self.clock_shift = ClockShift(lag=2)

    def tearDown(self):
        self.clock_shift = None

    def test_get_params(self):
        params = self.clock_shift.get_params()

        self.assertEqual({"lag": 2, "indexes": None}, params)

    def test_set_params(self):
        params = self.clock_shift.get_params()

        self.assertEqual({"lag": 2, "indexes": None}, params)

        self.clock_shift.set_params(indexes=["time"], lag=24)
        params = self.clock_shift.get_params()

        self.assertEqual({"lag": 24, "indexes": ["time"]}, params)

    def test_transform(self):
        time = pd.date_range('2000-01-01', freq='24H', periods=7)

        ds = xr.Dataset({'foo': ('time', [2, 3, 4, 5, 6, 7, 8]), 'time': time})

        result = self.clock_shift.transform(ds)

        time = pd.date_range('2000-01-01', freq='24H', periods=7)
        expected_result = xr.Dataset({
            'foo': ('time', [np.nan, np.nan, 2., 3., 4.5, 5., 6.]),
            'time':
            time
        })

        self.assertEqual(result.values(), expected_result)

    def test_transform_exception(self):
        time = pd.date_range('2000-01-01', freq='24H', periods=7)
        self.clock_shift.set_params(indexes=["FOO"])

        ds = xr.Dataset({'foo': ('time', [2, 3, 4, 5, 6, 7, 8]), 'time': time})
        with self.assertRaises(WrongParameterException) as context:
            self.clock_shift.transform(ds)
        self.assertEqual(
            context.exception.message,
            "Not all indexes (['FOO']) are in the indexes of x (['time']). "
            "Perhaps you set the wrong indexes with set_params or during the initialization of the ClockShift."
        )
Ejemplo n.º 3
0
    def pipe(params):
        keras_model = get_keras_model(params)

        pipeline = Pipeline(path="../results")

        imputer_power_statistics = LinearInterpolater(
            method='nearest', dim='time',
            name='imputer_power')(x=pipeline['load_power_statistics'])

        power_scaler = SKLearnWrapper(module=StandardScaler(),
                                      name='scaler_power')
        scale_power_statistics = power_scaler(x=imputer_power_statistics)

        shift_power_statistics = ClockShift(
            lag=1, name='ClockShift_Lag1')(x=scale_power_statistics)
        shift_power_statistics2 = ClockShift(
            lag=2, name='ClockShift_Lag2')(x=scale_power_statistics)

        keras_wrapper = KerasWrapper(keras_model,
                                     fit_kwargs={'batch_size': 32, 'epochs': 100, 'verbose': 0},
                                     compile_kwargs={'loss': 'mse', 'optimizer': 'Adam', 'metrics': ['mse']}) \
            (ClockShift_Lag1=shift_power_statistics,
             ClockShift_Lag2=shift_power_statistics2,
             target=scale_power_statistics)

        inverse_power_scale_dl = power_scaler(
            x=keras_wrapper,
            computation_mode=ComputationMode.Transform,
            use_inverse_transform=True,
            callbacks=[LinePlotCallback('prediction')])

        rmse_dl = RmseCalculator()(keras_model=inverse_power_scale_dl,
                                   y=pipeline['load_power_statistics'],
                                   callbacks=[CSVCallback('RMSE')])

        pipeline.train(train)
        result = pipeline.test(test)

        return {
            "loss": float(result['RmseCalculator'].values),
            "status": STATUS_OK,
            "eval_time": time.time() - start
        }
Ejemplo n.º 4
0
                                      CalendarFeature.month,
                                      CalendarFeature.weekday,
                                      CalendarFeature.weekend
                                  ])(x=pipeline["load_power_statistics"])

    # Deal with missing values through linear interpolation
    imputer_power_statistics = LinearInterpolater(
        method="nearest", dim="time",
        name="imputer_power")(x=pipeline["load_power_statistics"])

    # Scale the data using a standard SKLearn scaler
    power_scaler = SKLearnWrapper(module=StandardScaler(), name="scaler_power")
    scale_power_statistics = power_scaler(x=imputer_power_statistics)

    # Create lagged time series to later be used in the regression
    shift_power_statistics = ClockShift(
        lag=1, name="ClockShift_Lag1")(x=scale_power_statistics)
    shift_power_statistics2 = ClockShift(
        lag=2, name="ClockShift_Lag2")(x=scale_power_statistics)

    # Create a linear regression that uses the lagged values to predict the current value
    # NOTE: SKLearnWrapper has to collect all **kwargs itself and fit it against target.
    #       It is also possible to implement a join/collect class
    regressor_power_statistics = SKLearnWrapper(module=LinearRegression(
        fit_intercept=True))(
            power_lag1=shift_power_statistics,
            power_lag2=shift_power_statistics2,
            calendar=calendar,
            target=scale_power_statistics,
            callbacks=[LinePlotCallback('linear_regression')],
        )
Ejemplo n.º 5
0
 def setUp(self):
     self.clock_shift = ClockShift(lag=2)