윤영준 윤영준 2023-08-31
completed functionality, but flask interface needs to be done
@a6f036ff4c32ea16c37cc623c3c56e07c512e448
 
app.py (added)
+++ app.py
@@ -0,0 +1,24 @@
+from flask import Flask
+from flask_restx import Api
+from auth import Auth
+from action import Action
+
+app = Flask(__name__)
+
+print("Api Start")
+api = Api(    app,
+    version='0.1',
+    title="trafficagent",
+    description="API Server",
+    terms_url="/",
+    contact="[email protected]",
+    license="MIT")
+
+api.add_namespace(Auth, '/auth')
+print("Api Add Auth")
+
+api.add_namespace(Action, '/action')
+
+if __name__ == "__main__":
+    app.run(debug=False, host='0.0.0.0', port=8080)
+    print("Flask Start")(파일 끝에 줄바꿈 문자 없음)
data/weather/202007010000_202308310000_f.csv (Renamed from data/weather/202007010000_202308010000.csv)
--- data/weather/202007010000_202308010000.csv
+++ data/weather/202007010000_202308310000_f.csv
This diff is too big to display.
tools/algo/ARIMA.py
--- tools/algo/ARIMA.py
+++ tools/algo/ARIMA.py
@@ -1,2 +1,53 @@
 import numpy as np
-import pandas as pd
(파일 끝에 줄바꿈 문자 없음)
+import pandas as pd
+from statsmodels.tsa.statespace.sarimax import SARIMAX
+import matplotlib.pyplot as plt
+from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
+import plotly.graph_objects as go
+from scipy import signal
+from datetime import datetime
+import plotly.express as px
+from tools.algo.humidity import absolute_humidity
+
+
+if __name__ == "__main__":
+    df = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308310000_f.csv")
+    ah = absolute_humidity(df["상대습도"], df["기온"])
+    df['관측시각'] = df['관측시각'].apply(lambda x: datetime.strptime(f"{x}", '%Y%m%d%H%M'))
+    df["절대습도"] = ah
+    # fig = go.Figure()
+    #
+    # fig.add_trace(
+    #     go.Scatter(x=df["관측시각"], y=df["절대습도"])
+    # )
+    # fig.add_trace(
+    #     go.Scatter(x=df["관측시각"], y=signal.savgol_filter(
+    #         df["절대습도"],72,3)
+    #     ))
+    # fig.show()
+    # log_df = np.log(df["절대습도"])
+    diff_1 = (df["절대습도"].diff(periods=1).iloc[1:])
+    diff_2 = diff_1.diff(periods=1).iloc[1:]
+    # plot_acf(diff_2)
+    # plot_pacf(diff_2)
+    plt.show()
+    model = SARIMAX(df["절대습도"], order=(1,0,2), seasonal_order=(0,1,2,24))
+    model_fit = model.fit()
+    # ARIMA_model = pm.auto_arima(df['절대습도'],
+    #                             start_p=1,
+    #                             start_q=1,
+    #                             test='adf',  # use adftest to find optimal 'd'
+    #                             max_p=3, max_q=3,  # maximum p and q
+    #                             m=24,  # frequency of series (if m==1, seasonal is set to FALSE automatically)
+    #                             d=None,  # let model determine 'd'
+    #                             D=2, #order of the seasonal differencing
+    #                             seasonal=True,  # No Seasonality for standard ARIMA
+    #                             trace=False,  # logs
+    #                             error_action='warn',  # shows errors ('ignore' silences these)
+    #                             suppress_warnings=False,
+    #                             stepwise=True)
+    print(model_fit.summary())
+    df['forecast'] = model_fit.predict(start=-100, end=-1, dynamic=True)
+    # df[['절대습도', 'forecast']].plot(figsize=(12, 8))
+    fig = px.line(df[['절대습도', 'forecast']])
+    fig.show()
(파일 끝에 줄바꿈 문자 없음)
 
tools/algo/correlation.py (added)
+++ tools/algo/correlation.py
@@ -0,0 +1,39 @@
+import numpy as np
+import pandas as pd
+from scipy.stats import pearsonr, spearmanr, kendalltau, pointbiserialr
+import plotly.express as px
+import random
+
+from tools.algo.interpolation import interpolate_value
+from tools.algo.humidity import absolute_humidity
+
+if __name__ == "__main__":
+    df_weather = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308310000_f.csv")
+    df_failure = pd.read_excel("/home/juni/PycharmProjects/failure_analysis/data/failure/대옹 작업내용.xlsx")
+    times = df_failure["작업 시작 시간"]
+    temp = []
+    humidity = []
+    i=0
+    for time in times:
+        temp.append(interpolate_value(df_weather, timestamp=time, col_key="기온", k=6))
+        humidity.append(interpolate_value(df_weather, timestamp=time, col_key="상대습도", k=6))
+
+    absolute_humidity = absolute_humidity(humidity, temp)
+
+    df_failure["기온"] = temp
+    df_failure["상대습도"] = humidity
+    df_failure["절대습도"] = absolute_humidity
+    df_failure['불량 여부'] = np.where(df_failure['불량 여부'] == 'F', 0, df_failure['불량 여부'])
+    df_failure['불량 여부'] = np.where(df_failure['불량 여부'] == 'T', 1, df_failure['불량 여부'])
+
+
+    df = df_failure.copy()
+    correlation_manufacturing_abhumidity = pointbiserialr(df["절대습도"], df["불량 여부"])
+    correlation_manufacturing_rehumidity = pointbiserialr(df["상대습도"], df["불량 여부"])
+    correlation_manufacturing_temp = pointbiserialr(df["기온"], df["불량 여부"])
+    print(correlation_manufacturing_abhumidity)
+    print(correlation_manufacturing_rehumidity)
+    print(correlation_manufacturing_temp)
+
+    df_failure.to_csv("merged_data.csv")
+    pass(파일 끝에 줄바꿈 문자 없음)
tools/algo/humidity.py
--- tools/algo/humidity.py
+++ tools/algo/humidity.py
@@ -7,11 +7,14 @@
     return saturation_vapor_pressure * 1000 # KPa -> Pa
 
 def absolute_humidity(relative_humidity, temperature):
+    relative_humidity = np.array(relative_humidity)
+    temperature = np.array(temperature)
     saturation_vapor_pressure = buck_equation(temperature)
     # 461.5/Kg Kelvin is specific gas constatnt
     return saturation_vapor_pressure * relative_humidity * 0.01 /(461.5 * (temperature + 273.15) ) # g/m^3
 
-df = pd.read_csv('data/202007010000_202308010000.csv')
+if __name__ == "__main__":
+    df = pd.read_csv('data/202007010000_202308010000.csv')
 
 # 절대 습도 구하기
 # interpolation을 통해 시간 사이의 온습도 구하기
tools/algo/interpolation.py
--- tools/algo/interpolation.py
+++ tools/algo/interpolation.py
@@ -14,7 +14,10 @@
     :return: Interpolated value for the given timestamp.
     """
     # Convert timestamp to datetime object
-    target_time = datetime.strptime(timestamp, '%Y%m%d%H%M')
+    if type(timestamp) is str:
+        target_time = datetime.strptime(timestamp, '%Y%m%d%H%M')
+    else:
+        target_time = timestamp
 
     # Get nearest left 'o clock' time
     left_o_clock = target_time.replace(minute=0)
@@ -30,7 +33,7 @@
 
     # Convert datetime to numerical format: -k to k
     x = [i for i in range(-k, k+1)]
-    y = relevant_df[col_key].values
+    y = relevant_df[col_key]
 
     # Find the x value for the target timestamp
     x_target = (target_time - left_o_clock).total_seconds() / 3600
@@ -55,7 +58,7 @@
 
 
 if __name__ == '__main__':
-    df = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308010000.csv",
+    df = pd.read_csv("/home/juni/PycharmProjects/failure_analysis/data/weather/202007010000_202308310000_f.csv",
                      encoding='utf-8')
     timestamp = '202307271015'
     print(interpolate_value(df, timestamp, '기온', show_graph=True))
(파일 끝에 줄바꿈 문자 없음)
Add a comment
List