def save(
            self, station_availability_algorithm: StationAvailabilityAlgorithm
    ) -> None:
        station_id = station_availability_algorithm.station_id()
        self._storage_manager.create_storage_location(self._model_storage_path)

        with open(self._model_filename(station_id), "wb") as file:
            self._serializer.dump(station_availability_algorithm.model(), file)

        station_availability_algorithm.training_data_set().to_pickle(
            self._data_training_set_filename(station_id))
Esempio n. 2
0
    def test_it_can_override_an_existing_one(self) -> None:
        station_id = uuid.uuid4()

        first = StationAvailabilityAlgorithm(
            station_id, DataFrame(['data_1', 'frame_1', 'test_1']), Booster())
        self._repository.save(first)

        override = StationAvailabilityAlgorithm(
            station_id, DataFrame(['data_2', 'frame_2', 'test_2']), Booster())
        self._repository.save(override)

        self.assertEqual(self._repository.find_by_station_id(station_id),
                         override)
Esempio n. 3
0
    def test_it_can_not_find_one_by_station_id_if_it_does_not_exist(
            self) -> None:
        station_availability_algorithm = StationAvailabilityAlgorithm(
            uuid.uuid4(), DataFrame(['data', 'frame', 'test']), Booster())

        self._repository.save(station_availability_algorithm)

        self.assertIsNone(self._repository.find_by_station_id(uuid.uuid4()))
Esempio n. 4
0
    def test_it_can_find_one_by_station_id(self) -> None:
        station_id = uuid.uuid4()
        station_availability_algorithm = StationAvailabilityAlgorithm(
            station_id, DataFrame(['data', 'frame', 'test']), Booster())

        self._repository.save(station_availability_algorithm)

        self.assertEqual(self._repository.find_by_station_id(station_id),
                         station_availability_algorithm)
    def find_by_station_id(
            self, station_id: uuid) -> Optional[StationAvailabilityAlgorithm]:
        try:
            with open(self._model_filename(station_id), "rb") as file:
                model = self._serializer.load(file)

            data_frame_train = pandas.read_pickle(
                self._data_training_set_filename(station_id))

            return StationAvailabilityAlgorithm(station_id, data_frame_train,
                                                model)
        except FileNotFoundError:
            return None
    def handle(self, command: CreateStationAvailabilityAlgorithmCommand) -> None:
        station_id = command.station_id()
        records = self._query.query(station_id)

        if not records:
            raise EmptyValueForDataTrainingError.with_station_id(station_id)

        station_data = StationDataTraining(records, self._query.FIELDS, self._query.FEATURE_FIELD,
                                           self._query.INDEX_FIELD, command.frequency(), command.start_date())

        training_data_set = station_data.data_frame()

        model = self._data_trainer.train(training_data_set)

        self._repository.save(StationAvailabilityAlgorithm(command.station_id(), training_data_set, model))