# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="cj5erWsexM2h" outputId="654de4b2-49ff-464f-ba43-1997e2342c96"
# !pip install HydroErr
import HydroErr as HEEM
import numpy 
import pandas as pd
from pandas import read_csv
import math
import keras
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense, Activation
from sklearn.metrics import mean_squared_error
from keras.layers import BatchNormalization, Dropout
from google.colab import drive 
drive.mount('/content/gdrive')

# + id="i0eJldqrxM29"
AI_Method = "/content/gdrive/My Drive/Colab Notebooks/Nal7/12188MT-1-LSTM-SSA-Gini-1b-Sil"
Var_LRs=[1e-2]# 1e-4,1e-5,1e-6,1e-7,1e-8,1e-9
Var_Decays=[1e-4] # 1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9
Var_epochs=[100] # 100,500,1000,1500,2000
 
Streamflow=pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/12188MT-1SSA-1.csv', delimiter=',')

# + id="PKeLrvjKxM3N" colab={"base_uri": "https://localhost:8080/"} outputId="cbc1fd72-ab81-440d-8aad-cbb685766f43"
import numpy as np
x= Streamflow.drop('Q',axis=1)
Y= Streamflow['Q']
x= x.drop('Wind',axis=1)
 
x.head()
x.info()

# + id="HP41l7mOxM3b" colab={"base_uri": "https://localhost:8080/"} outputId="13b7705f-545b-410e-ce2e-35bd60ddd680"
X=np.array(x)
y=np.array(Y)
print (X.shape[1])

# + id="NYQVG2IOxM3p"
test_size = int(len(X) * 0.15)
valid_size = int(len(X) * 0.15)
train_size= len(X) - (valid_size+test_size)
y_train, y_valid, y_test = y[0:train_size], y[train_size:train_size+valid_size], y[-test_size:]
X_train, X_valid, X_test = X[0:train_size], X[train_size:train_size+valid_size], X[train_size+valid_size:]
X_train = numpy.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_valid = numpy.reshape(X_valid, (X_valid.shape[0], 1, X_valid.shape[1]))
X_test = numpy.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
input=X_train[1:]
input_shape=X_train.shape[1:]

# + id="pDp56jR4iL5B" colab={"base_uri": "https://localhost:8080/"} outputId="cfa677a6-5914-4af1-92f8-d4b3fc2104ba"
print (y_train.shape, y_valid.shape, y_test.shape)

# + id="NZmw4JnIxM33"
from datetime import datetime
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from math import sqrt
import numpy as geek

# + id="pisDJM4b2rzU"
#def create_NN():
model = Sequential()
model.add(LSTM(200, input_shape=X_train.shape[1:], activation='relu',return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(200, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.10))
model.add(Dense(1, activation='relu'))

# + id="PZ96ms4xxM4F" colab={"base_uri": "https://localhost:8080/"} outputId="db30c4d2-91e3-4212-baff-e34687cb3151"
startTime = datetime.now()
fileOfSummary = open(AI_Method+'_Summary_'+str(datetime.now()).replace(":", ".")+'.csv', "w")
fileOfSummary.write("Method,LR,DE,Epoch,RunTime,CCTr,CCVd,CCTt,NSTr,NSVd,NSTt,WITr,WIVd,WITt,RMSETr,RMSEVd,RMSETt,MAETr,MAEVd,MAETt"+str(X.shape[1])+"\n")

# + id="5UFy_CAexM4O" colab={"base_uri": "https://localhost:8080/"} outputId="1098b3b7-9f91-431c-db06-497a4d0385ee"

            
            optimizer = keras.optimizers.Adam(lr=e_LR, decay=e_decay)
            model.compile(optimizer=optimizer,loss='mean_squared_error')
        
            print(" ")
            print(" ")
            print("LRs:",e_LR)
            print("Decays:",e_decay)
            print("epochs:",e_epoch)
            history = model.fit(X_train, y_train, epochs=e_epoch, batch_size=256, verbose=0, validation_data=(X_valid, y_valid), shuffle=True)

            hist_df = pd.DataFrame(history.history) 
            Time_elasped= datetime.now() - startTime
            print('\nTime elapsed: ', Time_elasped)
 
            Train = model.predict(X_train)
            Valid = model.predict(X_valid)
            Test = model.predict(X_test)
 
            FileName=AI_Method+'-LR'+str(e_LR)+'-DE'+str(e_decay)+'-'+str(e_epoch)
            np.savetxt(FileName+'_Train.csv', Train)
            np.savetxt(FileName+'_Valid.csv', Valid)
            np.savetxt(FileName+'_Test.csv', Test)
            

 
            Train = Train.reshape(Train.shape[0])
            Valid = Valid.reshape(Valid.shape[0])
            Test = Test.reshape(Test.shape[0])
 
            print(" ")
            print("LRs:",e_LR)
            print("Decays:",e_decay)
            print("epochs:",e_epoch)
            
            print("Train ==>")
            CC_Train=np.corrcoef(y_train,Train)
            print("CC_Train = %.3f" %CC_Train[0,1])
 
            NSTr=HEEM.nse(Train, y_train) 
            print("NSTr = %.2f" %NSTr)

            WITr=HEEM.d(Train, y_train)   
            print("WITr = %.2f" %WITr)

            rootMeanSquaredErrorTr = sqrt(mean_squared_error(y_train, Train))
            print("RMSE = %.2f" % rootMeanSquaredErrorTr)
            MAETr=mean_absolute_error(y_train, Train)
            print("MAE = %.2f" %  MAETr)
 
            print("Validation ===============>")
            CC_Valid=np.corrcoef(y_valid,Valid)
            print("CC_Test = %.3f" %CC_Valid[0,1])
 
            NSVd=HEEM.nse(Valid, y_valid)
            print("NSTt = %.2f" %NSVd)
 
            WIVd=HEEM.d(Valid, y_valid)
            print("WITr = %.2f" %WITr)

            rootMeanSquaredErrorVd = sqrt(mean_squared_error(y_valid, Valid))
            print("RMSE = %.2f" % rootMeanSquaredErrorVd)
            MAEVd=mean_absolute_error(y_valid, Valid)
            print("MAE = %.2f" %  MAEVd)

            print("Test ======================>")
            CC_Test=np.corrcoef(y_test,Test)
            print("CC_Test = %.3f" %CC_Test[0,1])
 
            NSTt=HEEM.nse(Test, y_test)
            print("NSTt = %.2f" %NSTt)
 
            WITt=HEEM.d(Test, y_test) 
            print("WITr = %.2f" %WITr)

            rootMeanSquaredErrorTt = sqrt(mean_squared_error(y_test, Test))
            print("RMSE = %.2f" % rootMeanSquaredErrorTt)
            MAETt=mean_absolute_error(y_test, Test)
            print("MAE = %.2f" %  MAETt)
 
            fileOfSummary.write(AI_Method+','+str(e_LR)+','+str(e_decay)+','+str(e_epoch)+','+str(Time_elasped)+','
              +str(CC_Train[0,1])+','+str(CC_Valid[0,1])+','+str(CC_Test[0,1])+','
              +str(NSTr)+','+str(NSVd)+','+str(NSTt)+','
              +str(WITr)+','+str(WIVd)+','+str(WITt)+','
              +str(rootMeanSquaredErrorTr)+','+str(rootMeanSquaredErrorVd)+','+str(rootMeanSquaredErrorTt)+','
              +str(MAETr)+','+str(MAEVd)+','+str(MAETt)+'\n')

# + id="Uds-9YfxxM4e" colab={"base_uri": "https://localhost:8080/"} outputId="c740e2f9-f5d1-43a3-8eff-1be6b47e7bf1"
import io
buf = io.StringIO()
x.info(buf=buf)
s = buf.getvalue()

fileOfSummary.write(s)
fileOfSummary.close()

print("Finished")

# + id="JBpRfeyZ2L2S"
import matplotlib.pyplot as plt
plt.figure(figsize=(8,4))
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epochs')
plt.legend(loc='upper right')
plt.show();

StarcoderdataJupyterScriptsDedupFiltered