From d2da018dc9891af0acfa577ea7e30aef7f60c1f1 Mon Sep 17 00:00:00 2001 From: Darren Boss Date: Tue, 31 Oct 2023 16:08:08 -0700 Subject: [PATCH] Fix off by one day error Whitespace --- api/app/db/crud/observations.py | 4 ++-- api/app/weather_models/machine_learning.py | 19 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/api/app/db/crud/observations.py b/api/app/db/crud/observations.py index 00b79b352..f3ee6eb0e 100644 --- a/api/app/db/crud/observations.py +++ b/api/app/db/crud/observations.py @@ -90,7 +90,7 @@ def get_accumulated_precip_by_24h_interval(session: Session, station_code: int, 2023-11-01 20:00:00 2023-11-02 20:00:00 2023-11-03 20:00:00 - + We then join the HourlyActuals table so that we can sum hourly precip in a 24 hour period. The join is based on the weather_date field in the HourlyActuals table being in a 24 hour range using this odd looking syntax: weather_date <@ tstzrange(day, day + '24 hours', '(]') @@ -113,7 +113,7 @@ def get_accumulated_precip_by_24h_interval(session: Session, station_code: int, LEFT JOIN hourly_actuals ON - weather_date <@ tstzrange(day, day - '24 hours', '(]') + weather_date <@ tstzrange(day - INTERVAL '24 hours', day, '(]') WHERE station_code = {} GROUP BY diff --git a/api/app/weather_models/machine_learning.py b/api/app/weather_models/machine_learning.py index d96277fb9..3cb079503 100644 --- a/api/app/weather_models/machine_learning.py +++ b/api/app/weather_models/machine_learning.py @@ -89,7 +89,6 @@ def __init__(self, # NOTE: This could be an environment variable. self.max_days_to_learn = MAX_DAYS_TO_LEARN - def _add_sample_to_collection(self, prediction: ModelRunPrediction, actual: HourlyActual, @@ -142,15 +141,8 @@ def _collect_data(self, start_date: datetime): prev_prediction = prediction prev_actual = actual return sample_collection - - def learn(self): - # Calculate the date to start learning from. - start_date = self.max_learn_date - \ - timedelta(days=self.max_days_to_learn) - self.learn_models(start_date) - self.learn_precip_model(start_date) - def learn_models(self, start_date: datetime): + def _learn_models(self, start_date: datetime): """ Collect data and perform linear regression. """ # collect data @@ -175,7 +167,7 @@ def learn_models(self, start_date: datetime): self.regression_models_v2.collect_data(query) self.regression_models_v2.train() - def learn_precip_model(self, start_date): + def _learn_precip_model(self, start_date): """ Collect precip data and perform linear regression. """ # Precip is based on 24 hour periods at 20:00 hours UTC. Use the start_date @@ -193,6 +185,13 @@ def learn_precip_model(self, start_date): self.regression_models_v2.add_precip_samples(actual_daily_precip, predicted_daily_precip) self.regression_models_v2.train_precip() + def learn(self): + # Calculate the date to start learning from. + start_date = self.max_learn_date - \ + timedelta(days=self.max_days_to_learn) + self._learn_models(start_date) + self._learn_precip_model(start_date) + def predict_temperature(self, model_temperature: float, timestamp: datetime): """ Predict the bias adjusted temperature for a given point in time, given a corresponding model temperature.