Esempio n. 1
0
def main(config=None, database=None):
    taskname = Path(__file__).name.replace(".py", "")

    if database is None or config is None:
        raise RuntimeError(
            "Please specify a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package."
        )

    with TaskContext(taskname, config, database) as context:
        context.logger.info("Querying solar prediction jobs from database")
        prediction_jobs = context.database.get_prediction_jobs_solar()
        num_prediction_jobs = len(prediction_jobs)

        # only make customer = Provincie once an hour
        utc_now_minute = datetime.utcnow().minute
        if utc_now_minute >= 15:
            prediction_jobs = [
                pj for pj in prediction_jobs if str(pj["name"]).startswith("Provincie")
            ]
            num_removed_jobs = num_prediction_jobs - len(prediction_jobs)
            num_prediction_jobs = len(prediction_jobs)
            context.logger.info(
                "Remove 'Provincie' solar predictions",
                num_removed_jobs=num_removed_jobs,
                num_prediction_jobs=num_prediction_jobs,
            )

        PredictionJobLoop(context, prediction_jobs=prediction_jobs).map(
            make_solar_prediction_pj, context
        )
    def test_task_context_with_prediction_loop(self):
        dbmock = MagicMock()
        config_mock = MagicMock()
        func_fail = Mock()
        func_fail.side_effect = ValueError("Forced error")

        with TaskContext("unit_test_supposed_to_fail", dbmock, config_mock,
                         True, False) as context:
            PredictionJobLoop(context,
                              prediction_jobs=PREDICTION_JOBS).map(func_fail)
Esempio n. 3
0
def main(config=None, database=None):
    taskname = Path(__file__).name.replace(".py", "")

    if database is None or config is None:
        raise RuntimeError(
            "Please specify a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package.")

    with TaskContext(taskname, config, database) as context:
        model_type = [ml.value for ml in MLModelType]

        PredictionJobLoop(context, model_type=model_type).map(
            optimize_hyperparameters_task, context)
    def test_prediction_job_loop_with_dependencies(self):
        # We check that the prediction jobs are called in the
        # correct order when dependencies are present.
        def make_prediction_job(pj_id, depends_on=None):
            return PredictionJobDataClass(
                id=pj_id,
                depends_on=depends_on,
                model="",
                forecast_type="",
                train_components=False,
                name="",
                lat=0,
                lon=0,
                resolution_minutes=0,
                horizon_minutes=0,
            )

        pjs = [
            make_prediction_job(1),
            make_prediction_job(2),
            make_prediction_job(3),
            make_prediction_job(4, depends_on=[1, 2]),
            make_prediction_job(5, depends_on=[1, 3]),
            make_prediction_job(6, depends_on=[4]),
            make_prediction_job(7),
        ]

        context_mock = MagicMock()
        context_mock.database.get_prediction_jobs.return_value = pjs

        class MockFunction:
            def __init__(self):
                self.pjs = []

            def __call__(self, pj, *args, **kwargs):
                self.pjs.append(pj)

        function_mock = MockFunction()

        PredictionJobLoop(context_mock, ).map(function_mock)

        found_pjs = function_mock.pjs
        group1 = set(pj.id for pj in found_pjs[:4])
        group2 = set(pj.id for pj in found_pjs[4:6])
        group3 = set(pj.id for pj in found_pjs[6:])

        assert group1 == {1, 2, 3, 7}
        assert group2 == {4, 5}
        assert group3 == {6}
Esempio n. 5
0
def main(config=None, database=None):
    taskname = Path(__file__).name.replace(".py", "")

    if database is None or config is None:
        raise RuntimeError(
            "Please specifiy a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package."
        )

    with TaskContext(taskname, config, database) as context:
        model_type = ["xgb", "xgb_quantile", "lgb"]

        PredictionJobLoop(context, model_type=model_type).map(
            create_basecase_forecast_task, context
        )
def main(config=None, database=None):
    taskname = Path(__file__).name.replace(".py", "")

    if database is None or config is None:
        raise RuntimeError(
            "Please specifiy a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package.")

    with TaskContext(taskname, config, database) as context:
        context.logger.info("Querying wind prediction jobs from database")
        prediction_jobs = context.database.get_prediction_jobs_wind()
        prediction_jobs = [
            x for x in prediction_jobs if x["model"] == "latest"
        ]

        PredictionJobLoop(context, prediction_jobs=prediction_jobs).map(
            make_wind_forecast_pj, context)
def main(model_type=None, config=None, database=None):

    if database is None or config is None:
        raise RuntimeError(
            "Please specifiy a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package.")

    if model_type is None:
        model_type = [ml.value for ml in MLModelType]

    taskname = Path(__file__).name.replace(".py", "")
    datetime_now = datetime.utcnow()
    with TaskContext(taskname, config, database) as context:
        PredictionJobLoop(context,
                          model_type=model_type).map(train_model_task,
                                                     context,
                                                     datetime_end=datetime_now)
    def test_task_context_teams_message(self, postteamsmock):
        """Test to check that:
        if multiple exceptions are raised,
        pids they are nicely grouped per exception type."""

        dbmock = MagicMock()
        config_mock = MagicMock()

        # Specify which types of exceptions are raised
        func_fail = Mock()
        # the int/pid is arbitrary, unused currently.
        func_fail.side_effect = [
            None,
            NoPredictedLoadError(60),
            NoRealisedLoadError(307),
            None,
        ]

        # Specify test prediction jobs.
        # Required are the 'id' key and a second 'random' key.
        # The presence of a second key ensures that no additional pj_data is collected
        test_prediction_jobs = [
            TestData.get_prediction_job(pid=307),
            TestData.get_prediction_job(pid=60),
            TestData.get_prediction_job(pid=307),
            TestData.get_prediction_job(pid=60),
        ]

        with self.assertRaises(PredictionJobException):
            with TaskContext("test_with_teams_message", dbmock, config_mock,
                             False, True) as context:
                PredictionJobLoop(context,
                                  prediction_jobs=test_prediction_jobs,
                                  random_order=False).map(func_fail)

        # Assert that specification of exception: [pids] is 'posted' to the postteamsmock
        self.assertListEqual(
            postteamsmock.call_args_list[0].args[0]["sections"][2]["facts"],
            [(
                "Exceptions: pid(s)",
                "No predicted load found:[60]\n\nNo realised load found:[307]\n",
            )],
        )
Esempio n. 9
0
def main(model_type: MLModelType = None, config=None, database=None) -> None:
    taskname = Path(__file__).name.replace(".py", "")

    if database is None or config is None:
        raise RuntimeError(
            "Please specifiy a configmanager and/or database connection object. These"
            " can be found in the openstef-dbc package.")

    if model_type is None:
        model_type = [ml.value for ml in MLModelType]

    with TaskContext(taskname, config, database) as context:
        # Set start and end time
        start_time = datetime.utcnow() - timedelta(days=1)
        end_time = datetime.utcnow()

        PredictionJobLoop(context, model_type=model_type).map(
            check_kpi_task,
            context,
            start_time=start_time,
            end_time=end_time,
        )
    def test_prediction_job_loop_fail_once(self):
        # Build mocks
        context_mock = MagicMock()
        context_mock.database.get_prediction_jobs.return_value = PREDICTION_JOBS
        on_exception_callback = Mock()
        on_successful_callback = Mock()
        on_end_callback = Mock()
        function_mock = Mock(side_effect=Exception("Test"))

        # Create loop that fails and stops
        with self.assertRaises(PredictionJobException):
            PredictionJobLoop(
                context_mock,
                True,
                True,
                on_exception_callback,
                on_successful_callback,
                on_end_callback,
            ).map(function_mock)

        self.assertEqual(function_mock.call_count, 1)
        self.assertEqual(on_exception_callback.call_count, 1)
        self.assertEqual(on_successful_callback.call_count, 0)
        self.assertEqual(on_end_callback.call_count, 1)
    def test_prediction_job_loop_success(self):
        # Build mocks
        context_mock = MagicMock()
        context_mock.database.get_prediction_jobs.return_value = PREDICTION_JOBS
        on_exception_callback = Mock()
        on_successful_callback = Mock()
        on_end_callback = Mock()
        function_mock = Mock()

        # Create loop that succeeds
        PredictionJobLoop(
            context_mock,
            False,
            True,
            on_exception_callback,
            on_successful_callback,
            on_end_callback,
        ).map(function_mock)

        self.assertEqual(function_mock.call_count, NUM_PREDICTION_JOBS)
        self.assertEqual(on_exception_callback.call_count, 0)
        self.assertEqual(on_successful_callback.call_count,
                         NUM_PREDICTION_JOBS)
        self.assertEqual(on_end_callback.call_count, NUM_PREDICTION_JOBS)
 def test_prediction_job_loop_debug_pid(self):
     """Test if a list of prediction_jobs with len 1 is returned if debug_pid is given"""
     context_mock = MagicMock()
     pjl = PredictionJobLoop(context_mock, debug_pid=1)
     self.assertEqual(len(pjl.prediction_jobs), 1)