From SW's laptop

- DB에서 데이터 읽은 코드
 - 침하예측 수행 후 DB에 데이터 밀어넣는 코드
master
sanginnwoo 2023-01-16 00:43:18 +09:00
parent a97dd09a68
commit 27453ed2a9
2 changed files with 84 additions and 55 deletions

View File

@ -22,18 +22,18 @@ fill_height: height of surcharge fill
nod: number of date nod: number of date
''' '''
def settlement_prediction(point_name): def settlement_prediction(business_code, cons_code):
# connect the database # connect the database
#connection = pg2.connect("host=localhost dbname=postgres user=postgres password=lab36981 port=5432") # local connection = pg2.connect("host=localhost dbname=postgres user=postgres password=lab36981 port=5432") # local
connection = pg2.connect("host=192.168.0.13 dbname=sgis user=sgis password=sgis port=5432") # ICTWay internal #connection = pg2.connect("host=192.168.0.13 dbname=sgis user=sgis password=sgis port=5432") # ICTWay internal
# set cursor # set cursor
cursor = connection.cursor() cursor = connection.cursor()
# select monitoring data for the monitoring point # select monitoring data for the monitoring point
postgres_select_query = """SELECT * FROM apptb_surset02 WHERE cons_code='""" \ postgres_select_query = """SELECT * FROM apptb_surset02 WHERE business_code='""" + business_code \
+ point_name + """' ORDER BY nod ASC""" + """' and cons_code='""" + cons_code + """' ORDER BY nod ASC"""
cursor.execute(postgres_select_query) cursor.execute(postgres_select_query)
monitoring_record = cursor.fetchall() monitoring_record = cursor.fetchall()
@ -44,9 +44,9 @@ def settlement_prediction(point_name):
# fill lists # fill lists
for row in monitoring_record: for row in monitoring_record:
settlement.append(float(row[5])) settlement.append(float(row[6]))
surcharge.append(float(row[7])) surcharge.append(float(row[8]))
time.append(float(row[1])) time.append(float(row[2]))
# convert lists to np arrays # convert lists to np arrays
settlement = np.array(settlement) settlement = np.array(settlement)
@ -54,7 +54,7 @@ def settlement_prediction(point_name):
time = np.array(time) time = np.array(time)
# run the settlement prediction and get results # run the settlement prediction and get results
results = settle_prediction_steps_main.run_settle_prediction(point_name=point_name, np_time=time, results = settle_prediction_steps_main.run_settle_prediction(point_name=cons_code, np_time=time,
np_surcharge=surcharge, np_settlement=settlement, np_surcharge=surcharge, np_settlement=settlement,
final_step_predict_percent=90, final_step_predict_percent=90,
additional_predict_percent=300, plot_show=False, additional_predict_percent=300, plot_show=False,
@ -64,22 +64,32 @@ def settlement_prediction(point_name):
run_asaoka=True, run_step_prediction=True, run_asaoka=True, run_step_prediction=True,
asaoka_interval=3) asaoka_interval=3)
# if there are prediction data for the given data point, delete it first
postgres_delete_query = """DELETE FROM apptb_pred02 WHERE cons_code='""" + point_name + """'"""
cursor.execute(postgres_delete_query)
connection.commit()
# prediction method code # prediction method code
# 0: original hyperbolic method # 1: original hyperbolic method
# 1: nonlinear hyperbolic method # 2: nonlinear hyperbolic method
# 2: weighted nonlinear hyperbolic method # 3: weighted nonlinear hyperbolic method
# 3: Asaoka method # 4: Asaoka method
# 4: Step loading # 5: Step loading
# 5: temp
'''
time_hyper, sp_hyper_original,
time_hyper, sp_hyper_nonlinear,
time_hyper, sp_hyper_weight_nonlinear,
time_asaoka, sp_asaoka,
time[step_start_index[0]:], -sp_step[step_start_index[0]:],
'''
# insert predicted settlement into database
for i in range(5): for i in range(5):
# if there are prediction data for the given data point, delete it first
postgres_delete_query = """DELETE FROM apptb_pred02_no""" + str(i + 1) \
+ """ WHERE business_code='""" + business_code \
+ """' and cons_code='""" + cons_code + """'"""
cursor.execute(postgres_delete_query)
connection.commit()
# get time and settlement arrays # get time and settlement arrays
time = results[2 * i] time = results[2 * i]
predicted_settlement = results[2 * i + 1] predicted_settlement = results[2 * i + 1]
@ -89,31 +99,34 @@ def settlement_prediction(point_name):
# construct insert query # construct insert query
postgres_insert_query \ postgres_insert_query \
= """INSERT INTO apptb_pred02 """ \ = """INSERT INTO apptb_pred02_no""" + str(i + 1) + """ """ \
+ """(cons_code, prediction_progress_days, predicted_settlement, prediction_method) """ \ + """(business_code, cons_code, prediction_progress_days, predicted_settlement, prediction_method) """ \
+ """VALUES (%s, %s, %s, %s)""" + """VALUES (%s, %s, %s, %s, %s)"""
# set data to insert # set data to insert
record_to_insert = (point_name, time[j], predicted_settlement[j], i) record_to_insert = (business_code, cons_code, time[j], predicted_settlement[j], i + 1)
# execute the insert query # execute the insert query
cursor.execute(postgres_insert_query, record_to_insert) cursor.execute(postgres_insert_query, record_to_insert)
# commit changes # commit changes
connection.commit() connection.commit()
a = 0
def read_database_and_plot(point_name): def read_database_and_plot(business_code, cons_code):
# connect the database # connect the database
connection = pg2.connect("host=localhost dbname=postgres user=postgres password=lab36981 port=5432") connection = pg2.connect("host=localhost dbname=postgres user=postgres password=lab36981 port=5432")
# connection = pg2.connect("host=192.168.0.13 dbname=sgis user=sgis password=sgis port=5432") # ICTWay internal
# set cursor # set cursor
cursor = connection.cursor() cursor = connection.cursor()
# select monitoring data for the monitoring point # select monitoring data for the monitoring point
postgres_select_query = """SELECT * FROM apptb_surset02 WHERE cons_code='""" \ postgres_select_query = """SELECT * FROM apptb_surset02 WHERE business_code='""" + business_code \
+ point_name + """' ORDER BY nod ASC""" + """' and cons_code='""" + cons_code + """' ORDER BY nod ASC"""
cursor.execute(postgres_select_query) cursor.execute(postgres_select_query)
monitoring_record = cursor.fetchall() monitoring_record = cursor.fetchall()
@ -124,9 +137,9 @@ def read_database_and_plot(point_name):
# fill lists # fill lists
for row in monitoring_record: for row in monitoring_record:
time_monitored.append(float(row[2]))
settlement_monitored.append(float(row[6])) settlement_monitored.append(float(row[6]))
surcharge_monitored.append(float(row[8])) surcharge_monitored.append(float(row[8]))
time_monitored.append(float(row[12]))
# convert lists to np arrays # convert lists to np arrays
settlement_monitored = np.array(settlement_monitored) settlement_monitored = np.array(settlement_monitored)
@ -142,13 +155,13 @@ def read_database_and_plot(point_name):
# 5: temp # 5: temp
# temporarily set the prediction method as 0 # temporarily set the prediction method as 0
prediction_method = 0 postgres_select_query = """SELECT prediction_progress_days, predicted_settlement """ \
+ """FROM apptb_pred02_no""" + str(1) \
+ """ WHERE business_code='""" + business_code \
+ """' and cons_code='""" + cons_code \
+ """' ORDER BY prediction_progress_days ASC"""
# select predicted data for the monitoring point # select predicted data for the monitoring point
postgres_select_query = """SELECT prediction_progress_days, predicted_settlement """ \
+ """FROM apptb_pred02 WHERE cons_code= '""" + point_name \
+ """' and prediction_method = """ + str(prediction_method) \
+ """ ORDER BY prediction_progress_days ASC"""
cursor.execute(postgres_select_query) cursor.execute(postgres_select_query)
prediction_record = cursor.fetchall() prediction_record = cursor.fetchall()
@ -166,28 +179,37 @@ def read_database_and_plot(point_name):
time_predicted = np.array(time_predicted) time_predicted = np.array(time_predicted)
# 그래프 크기, 서브 그래프 개수 및 비율 설정 # 그래프 크기, 서브 그래프 개수 및 비율 설정
fig, axes = plt.subplots(2, 1, figsize=(12, 9), gridspec_kw={'height_ratios': [1, 3]}) fig, axes = plt.subplots(2, 1, figsize=(8, 6), gridspec_kw={'height_ratios': [1, 3]})
# 성토고 그래프 표시 # 성토고 그래프 표시
axes[0].plot(time_monitored, surcharge_monitored, color='black', label='surcharge height') axes[0].plot(time_monitored, surcharge_monitored, color='black', label='surcharge height')
# 성토고 그래프 설정 # 성토고 그래프 설정
axes[0].set_ylabel("Surcharge height (m)", fontsize=15) axes[0].set_ylabel("Surcharge height (m)", fontsize=10)
axes[0].set_xlim(left=0) axes[0].set_xlim(left=0)
axes[0].set_xlim(right=np.max(time_predicted))
axes[0].grid(color="gray", alpha=.5, linestyle='--') axes[0].grid(color="gray", alpha=.5, linestyle='--')
axes[0].tick_params(direction='in') axes[0].tick_params(direction='in')
# 계측 및 예측 침하량 표시 # 계측 및 예측 침하량 표시
axes[1].scatter(time_monitored, -settlement_monitored, s=50, axes[1].scatter(time_monitored, -settlement_monitored, s=30,
facecolors='white', edgecolors='black', label='measured data') facecolors='white', edgecolors='black', label='measured data')
axes[1].plot(time_predicted, -settlement_predicted, axes[1].plot(time_predicted, -settlement_predicted,
linestyle='--', color='red', label='Original Hyperbolic') linestyle='--', color='red', label='Original Hyperbolic')
axes[0].set_ylabel("Settlement (cm)", fontsize=10)
axes[1].set_xlim(left=0)
axes[1].set_xlim(right=np.max(time_predicted))
# script to call: python3 controller.py [business_code] [cons_code] # script to call: python3 controller.py [business_code] [cons_code]
# for example: # for example:
if __name__ == '__main__': if __name__ == '__main__':
args = sys.argv[1:] args = sys.argv[1:]
point_name = args[0] business_code = args[0]
settlement_prediction(point_name=point_name) cons_code = args[1]
# read_database_and_plot(point_name=point_name) #DB 입력 결과 확인 시에 활성화 / 평소에는 비활성화 settlement_prediction(business_code=business_code, cons_code=cons_code)
print("The settlement prediction is over.")
read_database_and_plot(business_code=business_code, cons_code=cons_code)
print("Visualization is over.") #DB 입력 결과 확인 시에 활성화 / 평소에는 비활성화

View File

@ -63,7 +63,7 @@ def run_settle_prediction_from_file(input_file, output_dir,
print_values, print_values,
run_original_hyperbolic='True', run_original_hyperbolic='True',
run_nonlinear_hyperbolic='True', run_nonlinear_hyperbolic='True',
run_weighted_nonlinear_hyperbolic='False', run_weighted_nonlinear_hyperbolic='True',
run_asaoka='True', run_asaoka='True',
run_step_prediction='True', run_step_prediction='True',
asaoka_interval=3): asaoka_interval=3):
@ -82,9 +82,12 @@ def run_settle_prediction_from_file(input_file, output_dir,
run_settle_prediction(point_name=input_file, np_time=time, np_surcharge=surcharge, np_settlement=settle, run_settle_prediction(point_name=input_file, np_time=time, np_surcharge=surcharge, np_settlement=settle,
final_step_predict_percent=final_step_predict_percent, final_step_predict_percent=final_step_predict_percent,
additional_predict_percent=additional_predict_percent, plot_show=plot_show, additional_predict_percent=additional_predict_percent, plot_show=plot_show,
print_values=print_values, run_original_hyperbolic=run_original_hyperbolic, print_values=print_values,
run_nonlinear_hyperbolic=run_nonlinear_hyperbolic, run_weighted_nonlinear_hyperbolic='False', run_original_hyperbolic=run_original_hyperbolic,
run_asaoka=run_asaoka, run_step_prediction=run_step_prediction, run_nonlinear_hyperbolic=run_nonlinear_hyperbolic,
run_weighted_nonlinear_hyperbolic=run_weighted_nonlinear_hyperbolic,
run_asaoka=run_asaoka,
run_step_prediction=run_step_prediction,
asaoka_interval=asaoka_interval) asaoka_interval=asaoka_interval)
def run_settle_prediction(point_name, def run_settle_prediction(point_name,
@ -369,7 +372,7 @@ def run_settle_prediction(point_name,
''' '''
# ========================================================= # =========================================================
# Settlement prediction (nonliner and original hyperbolic) # Settlement prediction (nonliner, weighted nonlinear and original hyperbolic)
# ========================================================= # =========================================================
# 성토 마지막 데이터 추출 # 성토 마지막 데이터 추출
@ -563,6 +566,8 @@ def run_settle_prediction(point_name,
print("Error in Final Settlement (Asaoka): %0.3f" % final_error_asaoka) print("Error in Final Settlement (Asaoka): %0.3f" % final_error_asaoka)
# ========================================== # ==========================================
# Post-Processing #2 : 그래프 작성 # Post-Processing #2 : 그래프 작성
# ========================================== # ==========================================
@ -744,17 +749,19 @@ def run_settle_prediction(point_name,
time_hyper, sp_hyper_nonlinear, time_hyper, sp_hyper_nonlinear,
time_hyper, sp_hyper_weight_nonlinear, time_hyper, sp_hyper_weight_nonlinear,
time_asaoka, sp_asaoka, time_asaoka, sp_asaoka,
time[step_start_index[0]:], -sp_step[step_start_index[0]:], time[step_start_index[0]:], sp_step[step_start_index[0]:],
rmse_hyper_original, rmse_hyper_nonlinear, rmse_hyper_weight_nonlinear, rmse_hyper_original,
rmse_asaoka, rmse_step, rmse_hyper_nonlinear,
final_error_hyper_original, final_error_hyper_nonlinear, final_error_hyper_weight_nonlinear, rmse_hyper_weight_nonlinear,
final_error_asaoka, final_error_step] rmse_asaoka,
rmse_step,
final_error_hyper_original,
final_error_hyper_nonlinear,
final_error_hyper_weight_nonlinear,
final_error_asaoka,
final_error_step]
#def run_postprocessing_error(point_name, np_time, np_surcharge, np_settlement):
# a = a + 1
#def run_postprocessing_graph(point_name, np_time, np_surcharge, np_)
#
''' '''
run_settle_prediction(input_file='data/2-5_No.39.csv', run_settle_prediction(input_file='data/2-5_No.39.csv',
output_dir='output', output_dir='output',