prediction.py 3.11 KB
Newer Older
1
2
3
4
5
6
7
from config import DB
import datetime
from dateutil.relativedelta import relativedelta
import json
import os
import psycopg2
import sys
박예은's avatar
박예은 committed
8
9
10
11
import pandas as pd
import tensorflow as tf
import numpy as np

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

if __name__ == "__main__":

    dbconfig = {"host": DB["host"], "port": DB["port"], "user": DB["user"],
                "password": DB["password"], "database": DB["database"]}
    user_email = sys.argv[1]

    data_dir = os.getcwd() + "/src/data_processing/temp.csv"

    # DB Connect and Make Cursor
    connection = psycopg2.connect(
        dbname=dbconfig["database"], user=dbconfig["user"], password=dbconfig["password"], host=dbconfig["host"], port=dbconfig["port"])
    cursor = connection.cursor()

    # Get Model and Params, then Make File and Variable
    cursor.execute(
28
        "SELECT model_file_path, params FROM \"Data_Processings\" WHERE host=%s", (user_email,))
29
30
    model_params = cursor.fetchone()

31
    model_file_path = model_params[0]
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

    params = model_params[1]
    mean = json.loads(params["mean"])
    std = json.loads(params["std"])

    # Get User Info
    cursor.execute(
        "SELECT using_aircon, loc_code FROM \"Users\" WHERE email=%s", (user_email,))
    user_info = cursor.fetchone()

    # Get Weather Data and Make File
    today = datetime.date.today()
    yesterday = today - relativedelta(days=1)
    f_yesterday = "{0}-{1}-{2}".format(yesterday.year,
                                       yesterday.month, yesterday.day)

    cursor.execute(
        "SELECT collected_at as \"date\", temp as temp_out, humi as humi_out, press, wind_speed "
        + "From \"Weather_Outs\" "
        + "WHERE loc_code = %s AND collected_at >= %s", (user_info[1], f_yesterday,)
    )
    results = cursor.fetchall()

    data_file = open(data_dir, 'w')

    # header
    data_file.write("date,temp_out,humi_out,press,wind_speed\n")

    # contents
    for result in results:
        data_file.write("{0},{1},{2},{3},{4}\n".format(
            result[0], result[1], result[2], result[3], result[4]))

    data_file.close()
    cursor.close()

박예은's avatar
박예은 committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    data_list = pd.read_csv(data_dir)
    new_data = data_list[-6:]

    date_time = pd.to_datetime(new_data['date'], format='%Y-%m-%d %H:%M')
    timestamp_s = date_time.map(datetime.datetime.timestamp)

    new_data = new_data[['temp_out', 'humi_out', 'press', 'wind_speed']]
    day = 24*60*60
    year = (365.2425)*day

    new_data['Day sin'] = np.sin(timestamp_s * (2 * np.pi / day))
    new_data['Day cos'] = np.cos(timestamp_s * (2 * np.pi / day))
    new_data['Year sin'] = np.sin(timestamp_s * (2 * np.pi / year))
    new_data['Year cos'] = np.cos(timestamp_s * (2 * np.pi / year))

    feature_cols = ['temp_out', 'humi_out', 'press',
                    'wind_speed', 'Day sin', 'Day cos', 'Year sin', 'Year cos']
    for col in feature_cols:
86
        new_data[col] = (new_data[col] - mean[col]) / std[col]
박예은's avatar
박예은 committed
87

88
    model_pro = tf.keras.models.load_model(os.getcwd() + model_file_path)
박예은's avatar
박예은 committed
89
90
91
    prediction = model_pro.predict(new_data)
    prediction = prediction * std['temp_out'] + mean['temp_out']

92
93
94
95
96
    # 사용한 파일 삭제
    if os.path.isfile(data_dir):
        os.remove(data_dir)

    print(prediction)