-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
executable file
·169 lines (133 loc) · 5.57 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
import configparser
import getpass
import logging
import os
import sys
from dataclasses import InitVar, dataclass
from datetime import date, datetime, timedelta
from pathlib import Path
import pandas as pd
import pyodbc
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from pytz import timezone
from app import keeper_of_the_state, now_adjusted, predictor
from app.constants import DAY_OF_WEEK, UNENFORCED_DAYS
from app.model import ParkingAvailabilityModel
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
if sys.stdout.isatty():
LOGGER.addHandler(logging.StreamHandler(sys.stdout))
DIRNAME = Path(__file__).parent.absolute()
@dataclass
class SqlServerConfig:
server: str
database: str
uid: InitVar[str] = None
pwd: InitVar[str] = None
def __post_init__(self, uid, pwd):
if not (uid is None or pwd is None):
self.driver = 'ODBC Driver 17 for SQL Server'
self.uid = uid
self.pwd = pwd
else:
self.driver = 'SQL Server Native Client 11.0'
self.trusted_connection = self.mars_connection = 'yes'
def main():
occupancy_dataframe = _get_occupancy_data_from_database(_get_database_config())
model = ParkingAvailabilityModel()
model.train(
(occupancy_dataframe
.astype({'semihour': 'datetime64[ns]'})
.rename(columns={'zone_name': 'zone_id'})
.pipe(_remove_unoccupied_timeslots)
.pipe(_remove_times_outside_hours_of_operation))
)
keeper_of_the_state.archive_model(model)
# _validate_variance()
def _get_database_config():
config = configparser.RawConfigParser()
config.read(DIRNAME / 'app/train.config')
smrt_environment = os.getenv('SCOS_ENV', default='dev')
sql_password = os.getenv('SQL_SERVER_PASSWORD') or getpass.getpass()
return SqlServerConfig(
server=os.getenv('SQL_SERVER_URL', config.get(smrt_environment, 'mssql_url')),
database=os.getenv('SQL_SERVER_DATABASE', config.get(smrt_environment, 'mssql_db_name')),
uid=os.getenv('SQL_SERVER_USERNAME', config.get(smrt_environment, 'mssql_db_user')),
pwd=sql_password
)
def _get_occupancy_data_from_database(database_config):
sql_query = '''
SELECT
[zone_name], [semihour], [occu_min], [occu_mtr_cnt],
[no_trxn_one_day_flg], [no_trxn_one_week_flg],
[total_cnt],
[occu_min_rate], [occu_cnt_rate],
[city_holiday], [shortnorth_event],
[no_data]
FROM [dbo].[parking_zone_occupancy_aggr]
WHERE CONVERT(date, semihour) >= CONVERT(date, DATEADD(month, -6, GETUTCDATE()))
ORDER BY zone_name, semihour
'''
try:
occupancy_dataframe = _sql_read(database_config, sql_query)
except Exception as e:
LOGGER.error(f'Unexpected error: {e}')
raise e
if not occupancy_dataframe.empty:
LOGGER.info('Read data from DB into dataframe successfully.')
LOGGER.info(f'Total (row, col) counts for dataframe: {occupancy_dataframe.shape}')
LOGGER.info(f'Zones in dataframe: {len(occupancy_dataframe["zone_name"].unique())}')
else:
LOGGER.error(f'No data read from DB: {occupancy_dataframe}')
raise Exception('No data read from DB')
return occupancy_dataframe
def _sql_read(database_config, sql_query):
LOGGER.info(f'Reading data from DB {database_config.server}')
LOGGER.debug('Performing DB read with spec of %s', database_config.__dict__)
with pyodbc.connect(**database_config.__dict__) as conn:
return pd.concat(list(pd.read_sql_query(sql_query, conn, chunksize=10 ** 6)), ignore_index=True)
def _remove_unoccupied_timeslots(occupancy_dataframe: pd.DataFrame) -> pd.DataFrame:
return occupancy_dataframe.loc[(
(occupancy_dataframe.no_data != 1)
& (occupancy_dataframe.no_trxn_one_week_flg != 1)
)]
def _remove_times_outside_hours_of_operation(occupancy_dataframe: pd.DataFrame) -> pd.DataFrame:
enforcement_days = [day.value for day in DAY_OF_WEEK
if day not in UNENFORCED_DAYS]
return (
occupancy_dataframe
.set_index('semihour')
.between_time('08:00', '22:00', include_end=False)
.reset_index()
.loc[lambda df: df.semihour.dt.dayofweek.isin(enforcement_days), :]
)
def _validate_variance():
yesterday_model = keeper_of_the_state.historical_model_name(date.today() - timedelta(1))
today_model = keeper_of_the_state.historical_model_name(date.today())
models = [yesterday_model, today_model]
keeper_of_the_state.warm_caches_synchronously(models)
now = now_adjusted.adjust(datetime.now(timezone('US/Eastern')))
today_at_ten = now.replace(hour=10)
predictions = predictor.predict_with(models, today_at_ten)
registry = CollectorRegistry()
gauge = Gauge(
'parking_model_variance',
'Variance in prediction after new model is trained',
registry=registry,
labelnames=['zone']
)
for prediction in predictions:
prediction_yesterday = prediction[f'{yesterday_model}Prediction']
prediction_today = prediction[f'{today_model}Prediction']
variance = abs(round(prediction_today - prediction_yesterday, 10))
zone = prediction['zoneId']
gauge.labels(zone=zone).set(variance)
environment = os.getenv('SCOS_ENV', default='dev')
push_to_gateway(
f'https://pushgateway.{environment}.internal.smartcolumbusos.com',
job='variance',
registry=registry
)
if __name__ == '__main__':
main()