def main(df, sims_list): with st.beta_expander('{} Dataframe for Simulated and Observed Stream Discharge'.format(wnam)): st.dataframe(df, height=500) st.markdown(utils.filedownload(df), unsafe_allow_html=True) if obd_file: stats_df = utils.get_stats_df(df, sims_list) with col2: st.markdown( """ ### Objective Functions """) st.dataframe(stats_df.T) st.markdown("## Hydrographs for stream discharge") st.plotly_chart(utils.get_plot(df, sims_list), use_container_width=True) tcol1, tcol2 = st.beta_columns([0.55, 0.45]) with tcol1: st.markdown("## Flow Duration Curve") pcol1, pcol2= st.beta_columns([0.1, 0.9]) with pcol1: yscale = st.radio("Select Y-axis scale", ["Linear", "Logarithmic"]) with pcol2: st.plotly_chart(utils.get_fdcplot(df, sims_list, yscale), use_container_width=True) elif obd_file is None: st.markdown("## Hydrographs for stream discharge") st.plotly_chart(utils.get_sim_plot(df, sims_list), use_container_width=True) tcol1, tcol2 = st.beta_columns([0.55, 0.45]) with tcol1: st.markdown("## Flow Duration Curve") pcol1, pcol2= st.beta_columns([0.1, 0.9]) with pcol1: yscale = st.radio("Select Y-axis scale", ["Linear", "Logarithmic"]) with pcol2: st.plotly_chart(utils.get_sim_fdcplot(df, sims_list, yscale), use_container_width=True)
def get(self): path = request.args.get('path') try: plt = get_plot(path) except TypeError as e: return jsonify("", 204) return json.dumps(plt, cls=NumpyEncoder)
def post_plots(): """ Create a plot of the spike request = { year: [2016, 2017, 2018, 2019], form: [hashtags, unigrams, wordpairs], value: "#fash" } """ data = json.loads(request.data) # if there is an error in the request if "year" not in data or "form" not in data or "value" not in data: return jsonify({}), 400 year = data["year"] form = data["form"] value = data["value"] if form == "wordpairs": form = "wordpair" path = "./files/{}/{}_{}.pkl".format(year, form, year) title = value if form == "hashtags": title = title[1:] fig_path = utils.get_plot(path, value, title) fig_path = fig_path.split("/")[-1] if form == "hashtags": title = "#" + title res = {"path": fig_path, "title": title} print(res) return jsonify(res), 201
def handle_client(conn,addr): utils.write_output_formatted(MODE,f"[NEW CONNECTION] {addr} connected.",SERVER_OUTPUT_DIR_LOG) connected=True while connected: pre_msg_header=conn.recv(HEADER) msg_header=pre_msg_header.decode(FORMAT) conn.send(CONFIRMATION_MSG.encode(FORMAT)) if msg_header.strip(): header_elems=msg_header.split('-') msg_cat=header_elems[0].strip() msg_type=header_elems[1].strip() if msg_cat=="SEND": msg_size=int(header_elems[2].strip()) if msg_type=="TEXT": msg=conn.recv(msg_size).decode(FORMAT) conn.send(CONFIRMATION_MSG.encode(FORMAT)) utils.write_output_formatted(MODE,"Received text message: {}".format(msg),SERVER_OUTPUT_DIR_LOG) if msg==DISCONNECT_MSG: connected=False elif msg_type=="FILE": data=utils.receive_chunks(conn,msg_size) conn.send(CONFIRMATION_MSG.encode(FORMAT)) filename=msg_header.split('-')[3].strip() with utils.safe_open(f"./{filename}",'wb') as f: f.write(data) utils.write_output_formatted(MODE,"Received file {}".format(filename),SERVER_OUTPUT_DIR_LOG) elif msg_cat=="REQUEST": if msg_type=="LOG": logpath=utils.get_latest_log() if logpath: msg="True" msg_send=b' '*(HEADER-len(msg))+msg.encode(FORMAT) conn.send(msg_send) with open(logpath,'rb') as f: logdata=f.read() logdata_size=str(len(logdata)).encode(FORMAT) logdata_size += b' ' * (HEADER-len(logdata_size)) conn.send(logdata_size) utils.send_chunks(conn,logdata) utils.write_output_formatted(MODE,"Sent log file {}".format(logpath),SERVER_OUTPUT_DIR_LOG) else: msg="False" msg_send=b' '*(HEADER-len(msg))+msg.encode(FORMAT) conn.send(msg_send) elif msg_type=="PLOT": ticker=header_elems[2].strip() plot=utils.get_plot(ticker) if plot: msg="True" msg_send=b' '*(HEADER-len(msg))+msg.encode(FORMAT) conn.send(msg_send) with open(plot,'rb') as f: plotdata=f.read() plotdata_size=str(len(plotdata)).encode(FORMAT) plotdata_size += b' ' * (HEADER-len(plotdata_size)) conn.send(plotdata_size) utils.send_chunks(conn,plotdata) utils.write_output_formatted(MODE,"Sent plot {}".format(utils.get_plot(ticker)),SERVER_OUTPUT_DIR_LOG) else: msg="False" msg_send=b' '*(HEADER-len(msg))+msg.encode(FORMAT) conn.send(msg_send) utils.write_output_formatted(MODE,f"Closing connection with {addr}.",SERVER_OUTPUT_DIR_LOG) conn.close()
def get_results(): try: result_form = ResultForm() reset_form = ResetButtons() plot_url = None code = request.args.get('code', '') if request.method == 'POST': if reset_form.submit.data: return redirect(url_for('home')) elif reset_form.reset_all.data: shutil.rmtree(results_dir) shutil.rmtree(model_path_dir) shutil.rmtree(test_path_dir) shutil.rmtree(train_path_dir) os.makedirs(results_dir) os.makedirs(model_path_dir) os.makedirs(test_path_dir) os.makedirs(train_path_dir) return redirect(url_for('home')) elif result_form.download_plot.data: if GlobalFlags.is_recently_tested: return send_file(results_plot_test_path, mimetype='image/png', as_attachment=True) else: return send_file(results_plot_train_path, mimetype='image/png', as_attachment=True) elif result_form.download_pred.data: return send_file(results_pred_path, mimetype='text/csv', as_attachment=True) elif result_form.download_info.data: return send_file(results_info_path, mimetype='application/json', as_attachment=True) elif result_form.download_test.data: return send_file(results_test_path, mimetype='text/csv', as_attachment=True) elif result_form.download_train.data: return send_file(results_train_path, mimetype='text/csv', as_attachment=True) if ((code == '0' or code == '1') and GlobalFlags.is_recently_trained) or \ (code == '2' and GlobalFlags.is_recently_tested): train_loss, test_loss, full_info = get_losses_info(results_train_path, results_test_path, results_info_path) with open(model_path, 'r', encoding='utf-8') as model_file: model_json = json.load(model_file) full_info.update(model_json) if GlobalFlags.is_recently_trained: best_iter, best_train_loss = get_best_by_train(train_loss) result_form.train_loss.data = str(best_train_loss) full_info['best_train_by_train'] = best_train_loss full_info['best_iter_by_train'] = int(best_iter + 1) full_info.pop('best_test_loss_by_test', None) full_info.pop('best_train_loss_by_test', None) full_info.pop('best_iter_by_test', None) plot_url = get_plot(train_loss, [], results_plot_train_path) if GlobalFlags.is_recently_tested: best_iter, best_test_loss, best_train_loss = get_best_by_test(train_loss, test_loss) result_form.train_loss.data = str(best_train_loss) result_form.test_loss.data = str(best_test_loss) full_info['best_test_loss_by_test'] = best_test_loss full_info['best_train_loss_by_test'] = best_train_loss full_info['best_iter_by_test'] = int(best_iter + 1) plot_url = get_plot(train_loss, test_loss, results_plot_test_path) with open(results_info_path, 'w') as info_file: json.dump(full_info, info_file) elif not GlobalFlags.no_f5: if code == '1': GlobalFlags.no_f5 = True thread = Thread() thread.train(data_paths['train'], model_path, model_dumped_path, results_dir, results_train_path, results_info_path, GlobalFlags) elif code == '2': GlobalFlags.no_f5 = True thread = Thread() thread.test(data_paths['test'], model_dumped_path, results_dir, results_test_path, results_pred_path, results_info_path, GlobalFlags) return render_template('results.html', result_form=result_form, reset_form=reset_form, global_flags=GlobalFlags, plot_url=plot_url) except Exception as exc: app.logger.info('Exception: {0}'.format(exc))
import constants from random import randint from time import sleep from create_db import CreateDatabase from digitalize import read_plot_image_and_save_to_db from datetime import timedelta, date, time, datetime from utils import get_plot start_day = datetime(2016, 8, 3, 23, 59) week = timedelta(days=7) end_day = start_day + week db = CreateDatabase() session = db.connect() for end_date in [datetime(2017, 8, 3, 23, 59)]: while start_day < end_date: get_plot(constants.PRESSURE_URL % start_day.strftime('%d%m%y'), "PRESSURE_URL.png") get_plot(constants.TEMPERATURE_URL % start_day.strftime('%d%m%y'), "TEMPERATURE_URL.png") get_plot(constants.WIND_SPEED_URL % start_day.strftime('%d%m%y'), "WIND_SPEED_URL.png") get_plot(constants.SOLAR_RADIATION_URL % start_day.strftime('%d%m%y'), "SOLAR_RADIATION_URL.png") get_plot(constants.HUMIDITY_URL % start_day.strftime('%d%m%y'), "HUMIDITY_URL.png") get_plot(constants.RAIN_URL % start_day.strftime('%d%m%y'), "RAIN_URL.png") img = cv2.imread("PRESSURE_URL.png") read_plot_image_and_save_to_db(start_day, end_day, img, session, constants.PRESSURE_TYPE)