diff --git a/api/__pycache__/app.cpython-310.pyc b/api/__pycache__/app.cpython-310.pyc deleted file mode 100644 index a694bd589ce1602823bdfe06ffd3bb86cfc44283..0000000000000000000000000000000000000000 Binary files a/api/__pycache__/app.cpython-310.pyc and /dev/null differ diff --git a/api/run.py b/api/run.py index 78667ab11fd833ef291c9c160027b3878b816171..8a8319d4bec2f2b16d925633e644837019ee0453 100644 --- a/api/run.py +++ b/api/run.py @@ -1,72 +1,84 @@ -#==================================================== +# ==================================================== # Written By Don Gunasinha = # Master in Data Science = -#==================================================== +# ==================================================== -# libarary declare +# libarary declare from app import app import pandas as pd -import datetime as dt +import datetime as dt import numpy as np import json import time +import math +import sklearn.metrics from sqlalchemy import create_engine -from sklearn.preprocessing import MinMaxScaler +from sklearn.preprocessing import MinMaxScaler from tensorflow import keras -from keras.models import Sequential +from keras.models import Sequential from keras.layers import Dense, LSTM, Dropout +from statsmodels.tsa.seasonal import seasonal_decompose + from flask import Flask, jsonify, request, make_response from flask import flash, request from datetime import datetime, timedelta -import matplotlib.pyplot as plt import matplotlib.dates as mdates import seaborn as sns +from matplotlib import pyplot +import matplotlib.pyplot as plt +from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, + AutoMinorLocator) +import matplotlib.dates as mdates sns.set_theme(style="darkgrid") -#================================================= +# ================================================= -# Globally Declare data set +# Globally Declare data set -# Data Frames -raw_data_set=pd.DataFrame() -data_set =pd.DataFrame() -index_data_set =pd.DataFrame() -tranning_set=pd.DataFrame() -result=pd.DataFrame() -predictions_set=pd.DataFrame() +# Data Frames +raw_data_set = pd.DataFrame() +data_set = pd.DataFrame() +index_data_set = pd.DataFrame() +tranning_set = pd.DataFrame() +result = pd.DataFrame() +predictions_set = pd.DataFrame() +decompose_set = pd.DataFrame() +model_settings = pd.DataFrame() -# numpy Arrays -data_arr_x=np.array([]) -data_arr_y=np.array([]) +# numpy Arrays +data_arr_x = np.array([]) +data_arr_y = np.array([]) x_train = np.array([]) y_train = np.array([]) x_test = np.array([]) y_test = np.array([]) -predictions = np.array([]) +predictions = np.array([]) # Initialising the LSTM model with training model = Sequential() -feature_length = 100 -testing_records=50 +feature_length =0 +testing_records = 50 batch_size = 5 epochs = 10 -accuracy=0 -execute_time=0 -input_units=100 -hidden_layer_1=50 -hidden_layer_2=25 -output_units=1 +accuracy = 0 +execute_time = 0 +input_units = 100 +hidden_layer_1 = 67 +hidden_layer_2 = 34 +output_units = 1 -# Scaler -scaler = MinMaxScaler() +seetings_id=0 + +# Scaler +scaler = MinMaxScaler() # MySQL database connection @@ -74,412 +86,496 @@ db_connection_str = 'mysql+pymysql://root:@localhost/csct' db_connection = create_engine(db_connection_str) -#============================================ +# mesure perforemence +rmse=0 + + +# ============================================ + +# Support Functions definitions -# Support Functions definitions +def read_model_settings(): + global model_settings + global feature_length + global batch_size + global epochs + global input_units + global hidden_layer_1 + global hidden_layer_2 + global output_units + global seetings_id + model_settings = pd.read_sql('select * from model_settings ORDER BY id DESC LIMIT 1', con=db_connection) + feature_length=model_settings['feature_length'].values[0] + batch_size=model_settings['batch_size'].values[0] + epochs=model_settings['epochs'].values[0] + input_units=model_settings['input_units'].values[0] + hidden_layer_1=model_settings['hidden_layer_1'].values[0] + hidden_layer_2=model_settings['hidden_layer_2'].values[0] + output_units=model_settings['output_units'].values[0] + seetings_id=model_settings['id'].values[0] -# Read data from data base and re arrange data + return model_settings -#--------------------------------------------- + + +# Read data from data base and re arrange data + +# --------------------------------------------- def read_data_set(): - # Declare global variable - global data_set - global raw_data_set - global index_data_set + # Declare global variable + global data_set + global raw_data_set + global index_data_set + global decompose_set - - + # Read data in to Data Frame + raw_data_set = pd.read_sql( + 'SELECT * FROM aw_product_demand', con=db_connection) - # Read data in to Data Frame - raw_data_set = pd.read_sql('SELECT * FROM aw_product_demand', con=db_connection) + # Validate Date + raw_data_set['Date'] = pd.to_datetime(raw_data_set['Date']).dt.date + raw_data_set['Order_Demand'] = raw_data_set['Order_Demand'].astype('int64') - # Validate Date - raw_data_set['Date']= pd.to_datetime(raw_data_set['Date']).dt.date - raw_data_set['Order_Demand'] = raw_data_set['Order_Demand'].astype('int64') + # combine to single date + data_set = raw_data_set.groupby('Date')['Order_Demand'].sum().reset_index() - #combine to single date - data_set = raw_data_set.groupby('Date')['Order_Demand'].sum().reset_index() - - data_set.sort_values('Date', inplace=True) - data_set['Date']=data_set['Date'].astype(str) + data_set.sort_values('Date', inplace=True) + data_set['Date'] = data_set['Date'].astype(str) - # Create index data frame - index_data_set=data_set + # Create index data frame + index_data_set = data_set + index_data_set = index_data_set.set_index(index_data_set['Date']) + index_data_set = index_data_set.drop('Date', axis=1) - index_data_set=index_data_set.set_index(index_data_set['Date']) + #genarate decompose data set using seasonal_decompose - return data_set + result = seasonal_decompose(index_data_set, model='multiplicative', extrapolate_trend='freq', period=100) + + #push data in to pandas dataframe + + decompose_set['Date'] = data_set['Date'] + decompose_set['trend'] = result.trend.values + decompose_set['sesonal'] = result.seasonal.values + decompose_set['resid'] = result.resid.values + decompose_set['observed'] = result.observed.values + + return data_set + + +# ------------------------------------------------------------------------------- +def save_tranning_history(epochs, time_steps, batch_size, execute_time, rmse,mae,mape,accurecy): + global seetings_id + + print(seetings_id) + + id = db_connection.execute("INSERT INTO `trannings` ( `date_time`, `epochs`, `time_steps`, `batch_size`, `execute_time`, `rmse`,`mae`,`mape`,`accurecy`) VALUES ( CURRENT_TIMESTAMP, '" + + str(epochs)+"', '"+str(time_steps)+"', '"+str(batch_size)+"', '"+str(execute_time)+"', '"+str(rmse)+"', '"+str(mae)+"', '"+str(mape)+"','"+str(accurecy)+"')") + + db_connection.execute("INSERT INTO `model_perforemence` ( `date_time`, `trannings_id`, `model_settings_id`) VALUES ( CURRENT_TIMESTAMP, '"+str(seetings_id)+"', '"+str(id.lastrowid)+"')") + print("Row Added = ", id.rowcount) -#------------------------------------------------------------------------------- -def save_tranning_history(epochs,time_steps,batch_size,execute_time,accuracy): - id=db_connection.execute("INSERT INTO `trannings` ( `date_time`, `epochs`, `time_steps`, `batch_size`, `execute_time`, `accurecy`) VALUES ( CURRENT_TIMESTAMP, '"+str(epochs)+"', '"+str(time_steps)+"', '"+str(batch_size)+"', '"+str(execute_time)+"', '"+str(accuracy)+"')") - print("Row Added = ",id.rowcount) #------------------------------------------------------------------------------- # Function to create x and y data -def input_and_targert(data,feature_length): - - # Genarate Samples - x_samples = list() - y_samples = list() - NumerOfRows=len(data) - - for i in range(feature_length , NumerOfRows , 1): - x_sample = data[i-feature_length:i] - y_sample = data[i] - x_samples.append(x_sample) - y_samples.append(y_sample) - - # Reshape the input as a 3D (Number of Samples,time steps,features) - X = np.array(x_samples) - X=X.reshape(X.shape[0],X.shape[1],1) - print("\n____Input Data Shape :____") - print(X.shape) - # Reshape Target - Y=np.array(y_samples) - Y=Y.reshape(Y.shape[0],1) - print("\n ______Target Data Shape :______") - print(Y.shape) - - return X,Y -#---------------------------------------------------------------------------- -# function to calculate prediction + + +def input_and_targert(data, feature_length): + + # Genarate Samples + x_samples = list() + y_samples = list() + NumerOfRows = len(data) + + for i in range(feature_length, NumerOfRows, 1): + x_sample = data[i-feature_length:i] + y_sample = data[i] + x_samples.append(x_sample) + y_samples.append(y_sample) + + # Reshape the input as a 3D (Number of Samples,time steps,features) + X = np.array(x_samples) + X = X.reshape(X.shape[0], X.shape[1], 1) + print("\n____Input Data Shape :____") + print(X.shape) + # Reshape Target + Y = np.array(y_samples) + Y = Y.reshape(Y.shape[0], 1) + print("\n ______Target Data Shape :______") + print(Y.shape) + + return X, Y +# ---------------------------------------------------------------------------- +# function to calculate prediction + def predict_given_date(data, date, feature_length): - if date not in data.index: - last_date=data.tail(1) - data.loc[date]=0 - idx = data.index.get_loc(date) - # close_col = data.iloc[:,1:2] - # close_col = close_col.iloc[idx - feature_length : idx,:].values - # close_col = np.expand_dims(scaler.transform(close_col) , axis = 0) - # Prediction = model.predict(close_col) - # Prediction=np.array(Prediction).reshape(-1, 1) - # Prediction = scaler.inverse_transform(Prediction) - return str(last_date) + if date not in data.index: + last_date = data.tail(1) + data.loc[date] = 0 + idx = data.index.get_loc(date) + # close_col = data.iloc[:,1:2] + # close_col = close_col.iloc[idx - feature_length : idx,:].values + # close_col = np.expand_dims(scaler.transform(close_col) , axis = 0) + # Prediction = model.predict(close_col) + # Prediction=np.array(Prediction).reshape(-1, 1) + # Prediction = scaler.inverse_transform(Prediction) + return str(last_date) -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ def one_step_ahade(): - global data_set - global predictions_set - global model - - last_values=data_set.tail(feature_length)['Order_Demand'].values - last_values=np.array(last_values) - print(last_values) - last_values=scaler.transform(last_values.reshape(-1,1)) + global data_set + global predictions_set + global model - NumSamples=1 - TimeSteps=feature_length - NumFeatures=1 - last_values=last_values.reshape(NumSamples,TimeSteps,NumFeatures) - predicted_order_demand = model.predict(last_values) - predicted_order_demand = scaler.inverse_transform(predicted_order_demand) - predicted_order_demand=round(predicted_order_demand[0][0]) + last_values = data_set.tail(feature_length)['Order_Demand'].values + last_values = np.array(last_values) + print(last_values) + last_values = scaler.transform(last_values.reshape(-1, 1)) - last_date=str(data_set.tail(1)["Date"].values[0]) - last_date = datetime.strptime(last_date, "%Y-%m-%d") - last_date+= timedelta(days=1) - new_date=datetime.strftime(last_date,"%Y-%m-%d") + NumSamples = 1 + TimeSteps = feature_length + NumFeatures = 1 + last_values = last_values.reshape(NumSamples, TimeSteps, NumFeatures) + predicted_order_demand = model.predict(last_values) + predicted_order_demand = scaler.inverse_transform(predicted_order_demand) + predicted_order_demand = round(predicted_order_demand[0][0]) - new_row={'Date':new_date,'Order_Demand':predicted_order_demand} - data_set = data_set.append(new_row, ignore_index=True) - predictions_set= predictions_set.append(new_row, ignore_index=True) - print(predictions_set) + last_date = str(data_set.tail(1)["Date"].values[0]) + last_date = datetime.strptime(last_date, "%Y-%m-%d") + last_date += timedelta(days=1) + new_date = datetime.strftime(last_date, "%Y-%m-%d") + new_row = {'Date': new_date, 'Order_Demand': predicted_order_demand} + data_set = data_set.append(new_row, ignore_index=True) + predictions_set = predictions_set.append(new_row, ignore_index=True) + print(predictions_set) +# ----------------------------------------------------------------------------- -#------------------------------------------------------------------------------ # =========================== End of support Functions ================= -#============================= Main funtion =================== +# ============================= Main funtion =================== def setup(): - # Declare Global variables ================= + # Declare Global variables ================= - global data_set - global predictions - global result - global scaler - global accuracy - global execute_time + global data_set + global predictions + global result + global scaler + global accuracy + global execute_time + global rmse - # Read Data from DataSet - read_data_set() + # Read Data from DataSet + read_data_set() + print(data_set.describe()) - # Filter order Deamands array - data_set["Order_Demand"]=pd.to_numeric(data_set.Order_Demand,errors='coerce') - data = data_set.iloc[:,1:2] - data = data.values + # Filter order Deamands array + data_set["Order_Demand"] = pd.to_numeric( + data_set.Order_Demand, errors='coerce') + data = data_set.iloc[:, 1:2] + print(data) - ds = scaler.fit(data) - scal_data=ds.transform(data) + print("--------------------------------------") + data = data.values + ds = scaler.fit(data) + scal_data = ds.transform(data) - scal_data = scaler.fit_transform(data) + scal_data = scaler.fit_transform(data) - x,y= input_and_targert(scal_data,feature_length) - - # Split the data into training and test - x_train=x[:-testing_records] - x_test=x[-testing_records:] - y_train=y[:-testing_records] - y_test=y[-testing_records:] + x, y = input_and_targert(scal_data, feature_length) + # Split the data into training and test + x_train = x[:-testing_records] + x_test = x[-testing_records:] + y_train = y[:-testing_records] + y_test = y[-testing_records:] - print("\n___Tranning Data Shape___ ") - print(x_train.shape) - print(y_train.shape) - print("\n___Testing Data Shape___ ") - print(x_test.shape) - print(y_test.shape) + print("\n___Tranning Data Shape___ ") + print(x_train.shape) + print(y_train.shape) + print("\n___Testing Data Shape___ ") + print(x_test.shape) + print(y_test.shape) - for inp, out in zip(x_train[0:2], y_train[0:2]): - print(inp,'--', out) + for inp, out in zip(x_train[0:2], y_train[0:2]): + print(inp, '--', out) - - # Defining Input shapes for LSTM - time_steps=x_train.shape[1] - features=x_train.shape[2] - print("Number of TimeSteps:", time_steps) - print("Number of Features:", features) + # Defining Input shapes for LSTM + time_steps = x_train.shape[1] + features = x_train.shape[2] + print("Number of TimeSteps:", time_steps) + print("Number of Features:", features) + # Add First LSTM Layer + model.add(LSTM(units=input_units, activation='relu', input_shape=( + time_steps, features), return_sequences=True)) + # Adding the Second hidden layer and the LSTM layer + model.add(LSTM(units=hidden_layer_1, activation='relu', + input_shape=(time_steps, features), return_sequences=True)) - #Add First LSTM Layer - model.add(LSTM(units = input_units, activation = 'relu', input_shape = (time_steps, features), return_sequences=True)) + # Adding the Third hidden layer and the LSTM layer + model.add(LSTM(units=hidden_layer_2, + activation='relu', return_sequences=False)) - # Adding the Second hidden layer and the LSTM layer - model.add(LSTM(units = hidden_layer_1, activation = 'relu', input_shape = (time_steps, features), return_sequences=True)) + # Adding the output layer + model.add(Dense(units=output_units)) + # Compiling model + model.compile(optimizer='adam', loss='mean_squared_error') - # Adding the Third hidden layer and the LSTM layer - model.add(LSTM(units = hidden_layer_2, activation = 'relu', return_sequences=False )) + print(model.input) + print(model.output) - # Adding the output layer - model.add(Dense(units = output_units)) - # Compiling model - model.compile(optimizer = 'adam', loss = 'mean_squared_error') + print(model.summary()) - print(model.input) - print(model.output) + # Measuring the time taken by the model to train + start_time = time.time() - print(model.summary()) + # Fitting the RNN to the Training set + model_history = model.fit(x_train, y_train, batch_size, epochs) + end_time = time.time() + execute_time = round((end_time-start_time)/60, 2) + print("__Total Time Taken: ", execute_time, ' Minutes___') - # Measuring the time taken by the model to train - start_time=time.time() + # Making predictions on test data + predicted = model.predict(x_test) + predicted = scaler.inverse_transform(predicted) - # Fitting the RNN to the Training set - model_history=model.fit(x_train, y_train, batch_size , epochs) + # Getting the original price values for testing data + orig = y_test + orig = scaler.inverse_transform(y_test) - end_time=time.time() - execute_time=round((end_time-start_time)/60,2) - print("__Total Time Taken: ", execute_time, ' Minutes___') + # Accuracy of the predictions + accuracy = 100 - (100*(abs(orig-predicted)/orig)).mean() + mse = sklearn.metrics.mean_squared_error(orig, predicted) + rmse=math.sqrt(mse) + mae=sklearn.metrics.mean_absolute_error(orig, predicted) + mape=sklearn.metrics.mean_absolute_percentage_error(orig, predicted) + mape=mape + mape=round(mape,2) + + accuracy = round(accuracy, 2) + print('Accuracy:', accuracy) - # Making predictions on test data - predicted_Price = model.predict(x_test) - predicted_Price = scaler.inverse_transform(predicted_Price) + save_tranning_history(epochs, time_steps, batch_size, + execute_time, rmse,mae,mape,accuracy) - # Getting the original price values for testing data - orig=y_test - orig=scaler.inverse_transform(y_test) + # Generating predictions on full data + TrainPredictions = scaler.inverse_transform(model.predict(x_train)) + TestPredictions = scaler.inverse_transform(model.predict(x_test)) + FullDataPredictions = np.append(TrainPredictions, TestPredictions) - # Accuracy of the predictions - accuracy=100 - (100*(abs(orig-predicted_Price)/orig)).mean() - accuracy=round(accuracy,2) - print('Accuracy:',accuracy) + # Save data to result data set + + result["Date"] = data_set.iloc[time_steps:]["Date"] + result["Predictions"] = FullDataPredictions + result["OrderDemand"] = data[time_steps:] + +# ==================================================================== +# main function call - save_tranning_history(epochs,time_steps,batch_size,execute_time,accuracy) +read_model_settings() - # Generating predictions on full data - TrainPredictions=scaler.inverse_transform(model.predict(x_train)) - TestPredictions=scaler.inverse_transform(model.predict(x_test)) - FullDataPredictions=np.append(TrainPredictions, TestPredictions) +setup() - # Save data to result data set - result["Date"]=data_set.iloc[time_steps:]["Date"] - result["Predictions"]=FullDataPredictions - result["OrderDemand"]=data[time_steps:] - # ==================================================================== -# main function call -setup() +# end points declaration + +@app.route("/plot_result", methods=['GET']) +def plot_result(): + + # plotting the full data + plt.plot(result["Predictions"], color='blue', + label='Predicted Order Demand') + plt.plot(result["OrderDemand"], color='lightblue', label='Original Price') + plt.title('Order Demand Predictions') + plt.xlabel(' Date') + plt.ylabel('Order Demand') + plt.legend() + fig = plt.gcf() + fig.set_figwidth(20) + fig.set_figheight(8) + plt.show() + + return "plot complete" -#==================================================================== +@app.route('/decompose', methods=['GET']) +def decompose(): + return decompose_set.to_json(orient='records') -# end points declaration -@app.route("/predict_one_step",methods=['GET']) +@app.route('/decompose_plot_resid', methods=['GET']) +def decompose_plot(): + result = seasonal_decompose( + index_data_set, model='multiplicative', extrapolate_trend='freq', period=100) + + ax = sns.lineplot(data=result.resid) + + ax.xaxis.set_major_locator(mdates.DayLocator(interval=30)) + plt.gcf().autofmt_xdate() + + plt.show() + return decompose_set.to_json(orient='records') + + +@app.route("/predict_one_step", methods=['GET']) def predict_one_step(): - one_step_ahade() - steps=str(len(predictions_set)) - return steps + one_step_ahade() + steps = str(len(predictions_set)) + return steps + -@app.route("/predict_result",methods=['GET']) +@app.route("/predict_result", methods=['GET']) def predicpredict_result(): global predictions_set return predictions_set.to_json(orient='records') - - - -@app.route("/plot_order-demands",methods=['GET']) +@app.route("/plot_order-demands", methods=['GET']) def plot_order_demands_total(): - global raw_data_set - sns.lineplot(x='Date', y='Order_Demand',hue='Warehouse', data=raw_data_set) - plt.figure(figsize=(20, 7)) - plt.xticks(rotation=15) - plt.gcf().autofmt_xdate() + global raw_data_set + sns.lineplot(x='Date', y='Order_Demand', + hue='Warehouse', data=raw_data_set) + plt.figure(figsize=(20, 7)) + plt.xticks(rotation=15) + plt.gcf().autofmt_xdate() - plt.legend() - plt.show() + plt.legend() + plt.show() - return "Order demand" + return "Order demand" -@app.route("/plot_order_demand_total",methods=['GET']) +@app.route("/plot_order_demand_total", methods=['GET']) def plot_order_demands(): - global data_set - import matplotlib.pyplot as plt - from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, - AutoMinorLocator) - import matplotlib.dates as mdates - - dtFmt = mdates.DateFormatter('%Y-%b') # define the formatting + global data_set - sns.lineplot(x='Date', y='Order_Demand', data=data_set) - plt.figure(figsize=(20, 7)) - plt.gca().xaxis.set_major_formatter(dtFmt) - # show every 12th tick on x axes - plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=1)) - plt.xticks(rotation=90, fontweight='light', fontsize='x-small',) - plt.legend() - plt.show() + dtFmt = mdates.DateFormatter('%Y-%b') # define the formatting - return "Order demand" + sns.lineplot(x='Date', y='Order_Demand', data=data_set) + plt.figure(figsize=(20, 7)) + plt.gca().xaxis.set_major_formatter(dtFmt) + # show every 12th tick on x axes + plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=1)) + plt.xticks(rotation=90, fontweight='light', fontsize='x-small',) + plt.legend() + plt.show() + return "Order demand" -@app.route("/products",methods=['GET']) +@app.route("/products", methods=['GET']) def products(): - global raw_data_set - result_array =raw_data_set['Product_code'].unique() - return result_array.tolist() + global raw_data_set + result_array = raw_data_set['Product_code'].unique() + return result_array.tolist() -@app.route("/date_range",methods=['GET']) +@app.route("/date_range", methods=['GET']) def date_range(): - result_array =data_set['Date'].unique() - return result_array.tolist() + result_array = data_set['Date'].unique() + return result_array.tolist() -@app.route("/warehouse",methods=['GET']) - +@app.route("/warehouse", methods=['GET']) def warehouse(): - global raw_data_set - warehouse_array =raw_data_set['Warehouse'].unique() - return warehouse_array.tolist() + global raw_data_set + warehouse_array = raw_data_set['Warehouse'].unique() + return warehouse_array.tolist() -@app.route("/row_data",methods=['GET']) +@app.route("/row_data", methods=['GET']) def row_data(): - global raw_data_set - return raw_data_set.to_json(orient='records') - + global raw_data_set + return raw_data_set.to_json(orient='records') - -@app.route("/validation",methods=['GET']) +@app.route("/forecast_data", methods=['GET']) def validation(): - global result - result=result.set_index(result['Date']) - return result.to_json(orient='records') + global result + result = result.set_index(result['Date']) + return result.to_json(orient='records') + -@app.route("/forecast_to_date",methods=["POST"]) +@app.route("/forecast_to_date", methods=["POST"]) def forecast_to_date(): - global data_set - global scaler - global index_data_set - global model - - # read incomming json data - data=request.get_json() - date=data['date'] - new_date=datetime.strptime(date, "%Y-%m-%d").date() - new_date=new_date - timedelta(days=(feature_length-1)) - new_date=new_date.strftime("%Y-%m-%d") - print(new_date) - - - result=predict_given_date(index_data_set,new_date, feature_length) - # df=pd.DataFrame() - # # df=pd.DataFrame(data=result,columns=["Prediction"]) - # df['Date']=pd.date_range(start=new_date,periods=feature_length) - # df=df.loc[::-1] - # df['Prediction']=result - - - # df['Date']= pd.to_datetime(df['Date']).dt.date - # df.sort_values('Date', inplace=True) - # df['Date']=df['Date'].astype(str) - # df=df.set_index(df['Date']) - # df=df.tail(1) - - return result - -@app.route('/get_accuracy',method=["POST"]) -def get_accuracy(): - # model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) - # history = model.fit(X, Y, epochs=100) - # print(history.history['loss']) - # print(history.history['accuracy']) - return "accuracy" - -@app.route("/forecast_to_range",methods=["POST"]) + global data_set + global scaler + global index_data_set + global model + + # read incomming json data + data = request.get_json() + date = data['date'] + new_date = datetime.strptime(date, "%Y-%m-%d").date() + new_date = new_date - timedelta(days=(feature_length-1)) + new_date = new_date.strftime("%Y-%m-%d") + print(new_date) + + result = predict_given_date(index_data_set, new_date, feature_length) + # df=pd.DataFrame() + # # df=pd.DataFrame(data=result,columns=["Prediction"]) + # df['Date']=pd.date_range(start=new_date,periods=feature_length) + # df=df.loc[::-1] + # df['Prediction']=result + + # df['Date']= pd.to_datetime(df['Date']).dt.date + # df.sort_values('Date', inplace=True) + # df['Date']=df['Date'].astype(str) + # df=df.set_index(df['Date']) + # df=df.tail(1) + + return result + + +@app.route("/forecast_to_range", methods=["POST"]) def forecast_to_range(): - global data_set - global scaler - global index_data_set - global model - - index_data_set=data_set - - index_data_set=index_data_set.set_index(index_data_set['Date']) - - # read incomming json data - data=request.get_json() - date=data['date'] - new_date=datetime.strptime(date, "%Y-%m-%d").date() - new_date=new_date - timedelta(days=(feature_length-1)) - new_date=new_date.strftime("%Y-%m-%d") - - result=predict_given_date(index_data_set,new_date, feature_length) - df=pd.DataFrame() - # df=pd.DataFrame(data=result,columns=["Prediction"]) - df['Date']=pd.date_range(start=new_date,periods=feature_length) - df=df.loc[::-1] - df['Prediction']=result - df['Date']= pd.to_datetime(df['Date']).dt.date - df.sort_values('Date', inplace=True) - df['Date']=df['Date'].astype(str) - df=df.set_index(df['Date']) - return df.to_json(orient='records') + global data_set + global scaler + global index_data_set + global model + + index_data_set = data_set + + index_data_set = index_data_set.set_index(index_data_set['Date']) + + # read incomming json data + data = request.get_json() + date = data['date'] + new_date = datetime.strptime(date, "%Y-%m-%d").date() + new_date = new_date - timedelta(days=(feature_length-1)) + new_date = new_date.strftime("%Y-%m-%d") + + result = predict_given_date(index_data_set, new_date, feature_length) + df = pd.DataFrame() + # df=pd.DataFrame(data=result,columns=["Prediction"]) + df['Date'] = pd.date_range(start=new_date, periods=feature_length) + df = df.loc[::-1] + df['Prediction'] = result + df['Date'] = pd.to_datetime(df['Date']).dt.date + df.sort_values('Date', inplace=True) + df['Date'] = df['Date'].astype(str) + df = df.set_index(df['Date']) + return df.to_json(orient='records') if __name__ == "__main__": - app.run() \ No newline at end of file + app.run() diff --git a/dash_board.php b/dash_board.php index 3942082e97af264b700324edad8658af16eba617..f8c22ed1fefa202af4d6e352f557d7b8ebcfa44d 100644 --- a/dash_board.php +++ b/dash_board.php @@ -97,8 +97,70 @@ option && myChart.setOption(option); </script> - - <script src="dist/js/chart2.js" type="text/javascript"> </script> + + <script type="text/javascript"> + var dom = document.getElementById('chart2'); + var myChart = echarts.init(dom, null, { + renderer: 'canvas', + useDirtyRect: false + }); + var app = {}; + + var option2; + + option2 = { + tooltip: { + trigger: 'item' + }, + legend: { + bottom: '5%', + left: 'center' + }, + series: [ + { + name: 'Access From', + type: 'pie', + radius: ['40%', '70%'], + avoidLabelOverlap: false, + itemStyle: { + borderRadius: 10, + borderColor: '#fff', + borderWidth: 2 + }, + label: { + show: false, + position: 'center' + }, + emphasis: { + label: { + show: true, + fontSize: 40, + fontWeight: 'bold' + } + }, + labelLine: { + show: false + }, + data: [ + { value: <?=$total_order_demands?>, name: 'Order demands' }, + { value: <?=$total_predection?>, name: 'Predictions' }, + { value: <?=$total_error?>, name: 'Error' }, + ] + } + ] + }; + + + if (option2 && typeof option2 === 'object') { + myChart.setOption(option2); + } + + window.addEventListener('resize', myChart.resize); + + </script> + + + <script src="dist/js/chart3.js" type="text/javascript"> </script> diff --git a/decompose.php b/decompose.php new file mode 100644 index 0000000000000000000000000000000000000000..1d8b1c12e192bea6f7192dc4dc7ecbf282efc4b9 --- /dev/null +++ b/decompose.php @@ -0,0 +1,178 @@ +<div class="content-wrapper"> + + <?php + $title = "Analysis Result"; + $t1 = "Home"; + $t2 = "Decomposition"; + + + include_once './page_header.php';?> + <!-- Main content --> + <div class="content"> + <div class="container-fluid"> + + <?php include_once './data/decompose_data.php';?> + + + + <div class="row" style="padding: 10px;"> + + <div class="col-md-12"> + + <div style="width:full; height:400px;" id="chart1"></div> + + </div> + + <div class="col-md-12"> + + <div style="width:full; height:400px;" id="chart2"></div> + + </div> + + <div class="col-md-12"> + + <div style="width:full; height:400px;" id="chart3"></div> + + </div> + + + </div> + + <script> + var chartDom = document.getElementById('chart1'); + var myChart = echarts.init(chartDom); + var option; + + option = { + title: { + text: 'Trend' + }, + tooltip: { + trigger: 'axis' + }, + legend: { + data: ['Trend'] + }, + grid: { + left: '3%', + right: '4%', + bottom: '3%', + containLabel: true + }, + toolbox: { + feature: { + saveAsImage: {} + } + }, + xAxis: { + type: 'category', + data: [<?=$dates?>] + }, + yAxis: { + type: 'value' + }, + series: [{ + data: [<?=$trend?>], + type: 'line', + smooth: true + }] + }; + + option && myChart.setOption(option); + </script> + + <script> + var chartDom = document.getElementById('chart2'); + var myChart2 = echarts.init(chartDom); + var option2; + + option2 = { + title: { + text: 'Sesonal' + }, + tooltip: { + trigger: 'axis' + }, + legend: { + data: ['Sesonal'] + }, + grid: { + left: '3%', + right: '4%', + bottom: '3%', + containLabel: true + }, + toolbox: { + feature: { + saveAsImage: {} + } + }, + xAxis: { + type: 'category', + data: [<?=$dates?>] + }, + yAxis: { + type: 'value' + }, + series: [{ + data: [<?=$sesonal?>], + type: 'line', + smooth: true + }] + }; + + option2 && myChart2.setOption(option2); + </script> + + +<script> + var chartDom = document.getElementById('chart3'); + var myChart3 = echarts.init(chartDom); + var option3; + + option3 = { + title: { + text: 'Resid' + }, + tooltip: { + trigger: 'axis' + }, + legend: { + data: ['Resid'] + }, + grid: { + left: '3%', + right: '4%', + bottom: '3%', + containLabel: true + }, + toolbox: { + feature: { + saveAsImage: {} + } + }, + xAxis: { + type: 'category', + data: [<?=$dates?>] + }, + yAxis: { + type: 'value' + }, + series: [{ + data: [<?=$resid?>], + type: 'line', + smooth: true + }] + }; + + option3 && myChart3.setOption(option3); + </script> + + + <!-- /.row --> + </div> + <!-- /.container-fluid --> + </div> + <!-- /.content --> +</div> +<!-- /.content-wrapper --> \ No newline at end of file diff --git a/forecast_list.php b/forecast_list.php new file mode 100644 index 0000000000000000000000000000000000000000..a0a7cd383b3a064210437af7a194936cb5fe8046 --- /dev/null +++ b/forecast_list.php @@ -0,0 +1,120 @@ +<?php +include_once './data/forecast_data.php'; +include_once('header.php'); + +?> + + +<body class="hold-transition sidebar-mini"> + <div class="wrapper"> + <!-- Navbar --> + + <?php include_once('./nav_bar.php'); ?> + <!-- Main Sidebar Container --> + <?php include_once('./side_bar.php'); ?> + + + <!-- Content Wrapper. Contains page content --> + <div class="content-wrapper"> + <!-- Content Header (Page header) --> + + <?php + $title = "Model Settings List "; + $t1="Model"; + $t2= " Settings List"; + + + + ?> + + <?php include_once('page_header.php')?> + + <!-- Main content --> + <section class="content"> + <div class="row"> + <div class="col-12"> + + <div class="card"> + + <div class="card-header"> + <h3 class="card-title"> + <button type="button" class="btn btn-block btn-outline-secondary" + onclick="location.href = 'settings.php';">Add New Record</button> + + </h3> + </div> + + <!-- /.card-header --> + <div class="card-body"> + <table id="example23" + class="display nowrap table table-hover table-striped table-bordered" + cellspacing="0" width="100%"> + <thead> + <tr> + <th>#</th> + <th>Date</th> + <th>Forecast Order Demand</th> + <th>Epochs</th> + <th>Input Units</th> + <th>Hidden Layer 1</th> + <th>Hiddden Layer 2</th> + <th>Output Layer</th> + + + + </tr> + </thead> + <tfoot> + <tr> + <th>#</th> + <th>Feture Length</th> + <th>batch size</th> + <th>Epochs</th> + <th>Input Units</th> + <th>Hidden Layer 1</th> + <th>Hiddden Layer 2</th> + <th>Output Layer</th> + </tr> + </tfoot> + <tbody> + <?php + $i = 1; + while ($row = mysqli_fetch_assoc($result)) { + + + ?> + <tr> + <td><?php echo $i++; ?></td> + + <td><?= $row['feature_length'] ?></td> + <td><?= $row['batch_size'] ?></td> + <td><?= $row['epochs'] ?></td> + <td><?= $row['input_units'] ?></td> + <td><?= $row['hidden_layer_1'] ?></td> + <td><?= $row['hidden_layer_2'] ?></td> + <td><?= $row['output_units'] ?></td> + </tr> + <?php } ?> + </tbody> + </table> + </div> + <!-- /.card-body --> + </div> + <!-- /.card --> + </div> + <!-- /.col --> + </div> + <!-- /.row --> + </section> + <!-- /.content --> + </div> + + <?php include_once('control_side_bar.php');?> + <!-- /.content-wrapper --> + <?php include_once('footer.php');?> + + </div> + <?php include_once('footer_script.php');?> +</body> + +</html> \ No newline at end of file diff --git a/inc/function.php b/inc/function.php index e5b8515f7cd49b8b1f38c18e31a781479c436574..e1c54d9b7226cf4a199736b4fb50b2d0d69f360c 100644 --- a/inc/function.php +++ b/inc/function.php @@ -13,6 +13,11 @@ function get_accurecy($conn){ return $res['accurecy']; +} + +function get_layer_1_nodes(){ + + } function get_epochs($conn){ diff --git a/index.php b/index.php index 457eb671722121219bf7e794da0d9812127d2dea..25cb3b3314b6242e772935dc35b739278991d638 100644 --- a/index.php +++ b/index.php @@ -3,12 +3,6 @@ include_once('inc/conn.php'); include_once('header.php'); - - - - - - ?> <body class="hold-transition sidebar-mini"> diff --git a/output.png b/output.png new file mode 100644 index 0000000000000000000000000000000000000000..9f7e2eb4aa8555cc8df98bcee38001670dc8f135 Binary files /dev/null and b/output.png differ diff --git a/page_decompose.php b/page_decompose.php new file mode 100644 index 0000000000000000000000000000000000000000..c8aa7bf8813e1f6fa2a5cf0c6af61deefdd861c5 --- /dev/null +++ b/page_decompose.php @@ -0,0 +1,26 @@ +<?php +include_once('inc/conn.php'); +include_once('header.php'); + +?> + +<body class="hold-transition sidebar-mini"> + <div class="wrapper"> + + + + <?php include_once('./nav_bar.php'); ?> + <!-- Main Sidebar Container --> + <?php include_once('./side_bar.php'); ?> + + <?php include_once('decompose.php');?> + + <?php include_once('control_side_bar.php');?> + + <?php include_once('footer.php');?> + </div> + + <?php include_once('footer_script.php');?> +</body> + +</html> \ No newline at end of file diff --git a/perforemence_list.php b/perforemence_list.php new file mode 100644 index 0000000000000000000000000000000000000000..e3ac5bf47426dd6fb1e2d6af133ef7dc015180b6 --- /dev/null +++ b/perforemence_list.php @@ -0,0 +1,132 @@ +<?php +include_once './data/data_mp.php'; +include_once('header.php'); + + +?> + + + + +<body class="hold-transition sidebar-mini"> + <div class="wrapper"> + <!-- Navbar --> + + <?php include_once('./nav_bar.php'); ?> + <!-- Main Sidebar Container --> + <?php include_once('./side_bar.php'); ?> + + + <!-- Content Wrapper. Contains page content --> + <div class="content-wrapper"> + <!-- Content Header (Page header) --> + + <?php + $title = "Model Settings List "; + $t1="Model"; + $t2= " Settings List"; + + + + ?> + + <?php include_once('page_header.php')?> + + <!-- Main content --> + <section class="content"> + <div class="row"> + <div class="col-12"> + + <div class="card"> + + <div class="card-header"> + <h3 class="card-title"> + <button type="button" class="btn btn-block btn-outline-secondary" + onclick="location.href = 'settings.php';">Add New Record</button> + + </h3> + </div> + + <!-- /.card-header --> + <div class="card-body"> + <table id="example23" + class="display nowrap table table-hover table-striped table-bordered" + cellspacing="0" width="100%"> + <thead> + <tr> + <th>#</th> + <th>Input Nodes</th> + <th>Hidden layer 1 nodes</th> + <th>Hidden layer 2 nodes</th> + <th>output nodes</th> + <th>Epochs</th> + <th>RMSE</th> + <th>MAE</th> + <th>MAPE</th> + <th>Accurecy %</th> + + + + + </tr> + </thead> + <tfoot> + <tr> + <th>#</th> + <th>Input Nodes</th> + <th>Hidden layer 1 nodes</th> + <th>Hidden layer 2 nodes</th> + <th>output nodes</th> + <th>Epochs</th> + <th>RMSE</th> + <th>MAE</th> + <th>MAPE</th> + <th>Accurecy %</th> + + </tr> + </tfoot> + <tbody> + <?php + $i = 1; + while ($row = mysqli_fetch_assoc($result)) { + + + ?> + <tr> + <td><?php echo $i++; ?></td> + + <td><?= $row['input_units'] ?></td> + <td><?= $row['hidden_layer_1'] ?></td> + <td><?= $row['hidden_layer_2'] ?></td> + <td><?= $row['output_units'] ?></td> + <td><?= $row['epochs'] ?></td> + <td><?= round($row['rmse'],3) ?></td> + <td><?= round($row['mae'],3) ?></td> + <td><?= $row['mape'] ?></td> + <td><?= $row['accurecy'] ?></td> + + </tr> + <?php } ?> + </tbody> + </table> + </div> + <!-- /.card-body --> + </div> + <!-- /.card --> + </div> + <!-- /.col --> + </div> + <!-- /.row --> + </section> + <!-- /.content --> + </div> + + <?php include_once('control_side_bar.php');?> + <!-- /.content-wrapper --> + <?php include_once('footer.php');?> + + </div> + <?php include_once('footer_script.php');?> +</body> + +</html> \ No newline at end of file diff --git a/settings.php b/settings.php index b82369805d4821ac5a9a9350b441228179cfe9d9..4dbd154ff5d6138791f4960812129b0b52d70084 100644 --- a/settings.php +++ b/settings.php @@ -47,22 +47,75 @@ include_once('header.php'); <div class="form-row"> <div class="col-lg-6 col-md-6 form-group"> - <lable>Number of Nodes for Layer 1:</lable> + <lable>Feature Length:</lable> - <input type="text" class="form-control" name="layer1_nodes" + <input type="text" class="form-control" name="feture_length" placeholder="0"> </div> <div class="col-lg-6 col-md-6 form-group"> - <lable>Number of Nodes for Layer 2:</lable> - - <input type="text" class="form-control" name="layer2_nodes" + <lable>Batch Size:</lable> + + <input type="text" class="form-control" name="batch_size" + placeholder="0"> + + </div> + + <div class="col-lg-6 col-md-6 form-group"> + <lable>Epochs:</lable> + + <input type="text" class="form-control" name="epochs" + placeholder="0"> + + </div> + + <div class="col-lg-6 col-md-6 form-group"> + <lable>Input Layer:</lable> + + <input type="text" class="form-control" name="input_units" placeholder="0"> </div> + <div class="col-lg-6 col-md-6 form-group"> + <lable>Hidden Layer 1:</lable> + + <input type="text" class="form-control" name="hidden_layer_1" + placeholder="0"> + </div> + <div class="col-lg-6 col-md-6 form-group"> + <lable>Hidden Layer 2:</lable> + + <input type="text" class="form-control" name="hidden_layer_2" + placeholder="0"> + </div> + + <div class="col-lg-6 col-md-6 form-group"> + <lable>Output Layer:</lable> + + <input type="text" class="form-control" name="output_layer" + placeholder="0"> + + </div> + + + + + + </div> + + <div class="form-row"> + + <div class="col-md-6"> + + </div> + + <div class="col-md-6 "> + <input type="submit" class="btn btn-outline-success float-right" value="Add New"> + </div> + </div> diff --git a/settings_list.php b/settings_list.php new file mode 100644 index 0000000000000000000000000000000000000000..4692285eb81b0ea6fafe8a304a501782b93bcbdb --- /dev/null +++ b/settings_list.php @@ -0,0 +1,120 @@ +<?php +include_once './data/model_settings_list.php'; +include_once('header.php'); + +?> + + +<body class="hold-transition sidebar-mini"> + <div class="wrapper"> + <!-- Navbar --> + + <?php include_once('./nav_bar.php'); ?> + <!-- Main Sidebar Container --> + <?php include_once('./side_bar.php'); ?> + + + <!-- Content Wrapper. Contains page content --> + <div class="content-wrapper"> + <!-- Content Header (Page header) --> + + <?php + $title = "Model Settings List "; + $t1="Model"; + $t2= " Settings List"; + + + + ?> + + <?php include_once('page_header.php')?> + + <!-- Main content --> + <section class="content"> + <div class="row"> + <div class="col-12"> + + <div class="card"> + + <div class="card-header"> + <h3 class="card-title"> + <button type="button" class="btn btn-block btn-outline-secondary" + onclick="location.href = 'settings.php';">Add New Record</button> + + </h3> + </div> + + <!-- /.card-header --> + <div class="card-body"> + <table id="example23" + class="display nowrap table table-hover table-striped table-bordered" + cellspacing="0" width="100%"> + <thead> + <tr> + <th>#</th> + <th>Feture Length</th> + <th>batch size</th> + <th>Epochs</th> + <th>Input Units</th> + <th>Hidden Layer 1</th> + <th>Hiddden Layer 2</th> + <th>Output Layer</th> + + + + </tr> + </thead> + <tfoot> + <tr> + <th>#</th> + <th>Feture Length</th> + <th>batch size</th> + <th>Epochs</th> + <th>Input Units</th> + <th>Hidden Layer 1</th> + <th>Hiddden Layer 2</th> + <th>Output Layer</th> + </tr> + </tfoot> + <tbody> + <?php + $i = 1; + while ($row = mysqli_fetch_assoc($result)) { + + + ?> + <tr> + <td><?php echo $i++; ?></td> + + <td><?= $row['feature_length'] ?></td> + <td><?= $row['batch_size'] ?></td> + <td><?= $row['epochs'] ?></td> + <td><?= $row['input_units'] ?></td> + <td><?= $row['hidden_layer_1'] ?></td> + <td><?= $row['hidden_layer_2'] ?></td> + <td><?= $row['output_units'] ?></td> + </tr> + <?php } ?> + </tbody> + </table> + </div> + <!-- /.card-body --> + </div> + <!-- /.card --> + </div> + <!-- /.col --> + </div> + <!-- /.row --> + </section> + <!-- /.content --> + </div> + + <?php include_once('control_side_bar.php');?> + <!-- /.content-wrapper --> + <?php include_once('footer.php');?> + + </div> + <?php include_once('footer_script.php');?> +</body> + +</html> \ No newline at end of file diff --git a/side_bar.php b/side_bar.php index 99520a4b849505582b4af939f648616b56f34423..041f873b176cc83090f1a9a556172f727144345f 100644 --- a/side_bar.php +++ b/side_bar.php @@ -46,11 +46,11 @@ <li class="nav-item"> <a href="run.php" class="nav-link"> <i class="fa fa-play nav-icon"></i> - <p>Run Model</p> + <p>Model History</p> </a> </li> <li class="nav-item"> - <a href="settings.php" class="nav-link"> + <a href="settings_list.php" class="nav-link"> <i class="fa fa-cog nav-icon"></i> <p>Model Settings</p> </a> @@ -73,37 +73,44 @@ <p>Get the forecast</p> </a> </li> + + <li class="nav-item"> + <a href="page_forecast.php" class="nav-link"> + <i class="fas fa-list nav-icon"></i> + <p>List Forecast</p> + </a> + </li> </ul> </li> <li class="nav-header">Data Analytics</li> <li class="nav-item"> - <a href="products.php" class="nav-link"> - <i class="nav-icon fas fa-calendar-alt"></i> + <a href="page_decompose.php" class="nav-link"> + <i class="nav-icon far fa-chart-bar"></i> <p> - Products + Time Series </p> </a> </li> <li class="nav-item"> - <a href="pages/gallery.html" class="nav-link"> - <i class="nav-icon far fa-image"></i> + <a href="perforemence_list.php" class="nav-link"> + <i class="nav-icon fa-solid fa-dumbbell"></i> <p> - Warehouse + Model Perforemence </p> </a> </li> - <li class="nav-item"> + <!-- <li class="nav-item"> <a href="category.php" class="nav-link"> <i class="nav-icon far fa-image"></i> <p> Category </p> </a> - </li> + </li> --> </ul>