-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (41 loc) · 1.58 KB
/
main.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
import logging
# external modules
import click
import pandas as pd
from google.cloud import bigquery
# local modules
from train.train import getModel
from predict.predict import getPrediction
# configuration file
from config import AIRLINES
@click.command()
@click.argument('bq_dataset')
@click.argument('table_name')
@click.argument('country')
@click.option('-l', '--debug', default='INFO', type=click.Choice(['CRITICAL','ERROR','WARNING','INFO','DEBUG']), help='Sets the logging level.')
def main(bq_dataset, table_name, country, debug):
# logging setup
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.getLevelName(debug), datefmt='%Y-%m-%d %H:%M:%S')
logging.info('Program initiated')
# training
xgbmodel, xtrain_columns = getModel(country)
df = pd.DataFrame()
# inference
for competitor in AIRLINES[country]:
logging.info("Predicting load factors for {}".format(competitor))
try:
result = getPrediction(xgbmodel, xtrain_columns, competitor, country)
df = pd.concat([df, result])
except Exception as err:
logging.warning(err)
if not df.empty:
client = bigquery.Client()
dataset_ref = client.dataset(bq_dataset)
table_ref = dataset_ref.table(table_name)
df.reset_index(drop=True, inplace=True)
logging.info("Uploading predictions to BigQuery")
client.load_table_from_dataframe(df, table_ref).result()
else:
logging.warning("Prediction dataframe is empty")
if __name__ == '__main__':
main()