main.py 2.14 KB
Newer Older
1
2
3
4
5
6
7
"""
    # main.py

    - Load된 데이터들에 대해 Linear Regression을 진행합니다.
    - 진행된 후의 Weights를 파일로 저장합니다.
"""

8
import datetime
9
import os
10
import psycopg2
11
12
from psycopg2.extras import Json
import sys
13

14
from preprocessing import preprocess
15
16
from model import modeling

17

18
# DB 환경 변수
19
20
21
22
23
dbconfig = {"host": sys.argv[1], "port": sys.argv[2], "user": sys.argv[3],
            "password": sys.argv[4], "database": sys.argv[5]}

data_dir = os.getcwd() + "/src/data_processing/temp.csv"
model_dir = os.getcwd() + "/src/data_processing/model.h5"
24

25
26
27
28
29
30
31
32
33

def makeDateForm():
    today = datetime.datetime.today()
    year = str(today.year)
    month = str(today.month) if today.month >= 10 else '0'+str(today.month)
    day = str(today.day) if today.day >= 10 else '0'+str(today.day)
    collected_at = year + "-" + month + "-" + day

    return collected_at
34
35


36
37
# DB 연결
connection = psycopg2.connect(
38
    dbname=dbconfig["database"], user=dbconfig["user"], password=dbconfig["password"], host=dbconfig["host"], port=dbconfig["port"])
39

40
41
# DB에 대한 동작을 위한 cursor 생성
cursor = connection.cursor()
42

43
cursor.execute("SELECT email, loc_code, using_aircon FROM \"Users\"")
44
45
46
47
48
users = cursor.fetchall()

for user in users:

    host = {"email": user[0], "loc_code": user[1], "using_aircon": user[2]}
49

50
    # 데이터 전처리
51
    standard_df, mean_df, std_df = preprocess(cursor, host)
52

53
    # 데이터 분석
박예은's avatar
박예은 committed
54
    modeling(standard_df)
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

    collected_at = makeDateForm()

    model_file = open(model_dir, 'rb')
    model_file_data = model_file.read()

    params = {"mean": mean_df.to_json(), "std": std_df.to_json()}

    cursor.execute("INSERT INTO \"Data_Processings\" (host,collected_at,model_file,params) VALUES (%s,%s,%s,%s)",
                   (host["email"],
                    collected_at,
                    model_file_data,
                    Json(params),))

    connection.commit()
    model_file.close()

    if os.path.isfile(data_dir):
        os.remove(data_dir)

    if os.path.isfile(model_dir):
        os.remove(model_dir)

80

81
82
# Cursor와 Connection 종료
cursor.close()
83
connection.close()