#!/usr/bin/env python
# coding: utf-8

# In[1]:

import warnings
warnings.simplefilter('ignore')

import numpy as np
import pandas as pd
import os
import random
from datetime import datetime
from sklearn.metrics import mean_squared_error,mean_absolute_error
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import matplotlib.pyplot as plt
import sys

import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Input
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.callbacks import ModelCheckpoint

# In[2]:

file_path = 'outputfile.txt'
sys.stdout = open(file_path, "w")

# In[3]:

df_Gold = pd.read_csv('Gold Futures Historical Data.csv')

# In[4]:

df_Crude = pd.read_csv('Crude Oil WTI Futures Historical Data.csv')

# In[5]:

df = pd.read_csv('Try-3.csv')

# In[6]:

df.shape

# In[7]:

df['DATE'] = pd.to_datetime(df['date'] )
df_Crude['DATE'] = pd.to_datetime(df_Crude['Date'] )
df_Gold['DATE'] = pd.to_datetime(df_Gold['Date'] )

# In[8]:

df.set_index(["DATE"],inplace=True)
df.drop(['date'], axis=1, inplace=True)

df_Gold.set_index(["DATE"],inplace=True)
df_Gold.drop(['Date'], axis=1, inplace=True)

df_Crude.set_index(["DATE"],inplace=True)
df_Crude.drop(['Date'], axis=1, inplace=True)

# In[9]:

df_Gold = df_Gold.iloc[::-1]
df_Crude = df_Crude.iloc[::-1]

# In[10]:

Stock_name = df['symbol'].unique()

# In[11]:

df_SandP = pd.read_csv('S&P 500 Historical Data.csv')
df_Nasdaq = pd.read_csv('Nasdaq Historical Data.csv')
df_DJIA = pd.read_csv('Dow Jones Industrial Average Historical Data.csv')

# In[12]:

df_SandP['DATE'] = pd.to_datetime(df_SandP['Date'])
df_Nasdaq['DATE'] = pd.to_datetime(df_Nasdaq['Date'])
df_DJIA['DATE'] = pd.to_datetime(df_DJIA['Date'])
df_Nasdaq.rename(columns = {'Close':'Price'},inplace = True)

# In[13]:

df_SandP.set_index(["DATE"],inplace=True)
df_Nasdaq.set_index(["DATE"],inplace=True)
df_DJIA.set_index(["DATE"],inplace=True)

# In[14]:

df_SandP.drop(['Date'], axis=1, inplace=True)
df_Nasdaq.drop(['Date'], axis=1, inplace=True)
df_DJIA.drop(['Date'], axis=1, inplace=True)

# In[15]:

df_SandP = df_SandP.iloc[::-1]
df_DJIA = df_DJIA.iloc[::-1]

# In[16]:

df_SandP = df_SandP.astype(float, errors = 'ignore')
df_Nasdaq = df_Nasdaq.astype(float, errors = 'ignore')
df_DJIA = df_DJIA.astype(float, errors = 'ignore')

# In[17]:

Aa = df['OPrice'].loc[df['symbol'] == Stock_name[0]]
Ag = df['OPrice'].loc[df['symbol'] == Stock_name[1]]
In = df['OPrice'].loc[df['symbol'] == Stock_name[2]]
Crude = df_Crude['Price']

df_Gold['Price'] = df_Gold['Price'].apply(lambda x: str(x.replace(',','')))
df_Gold['Price'] = df_Gold['Price'].astype(float, errors = 'ignore')
Gold = df_Gold['Price']

# In[18]:

df_SandP = df_SandP.applymap(lambda x: str(x. replace(',','')))
df_DJIA = df_DJIA.applymap(lambda x: str(x. replace(',','')))

# In[19]:

df_List = [df_SandP,df_Nasdaq,df_DJIA]

# In[20]:

for column in Stock_name:
    df_Target = df['OPrice'].loc[df['symbol'] == column]
    for df_Feat in df_List:
        df_Feat['Price'] = df_Feat['Price'].astype(float, errors = 'ignore')
        df_Feat['Open'] = df_Feat['Open'].astype(float, errors = 'raise')
        df_Feat['Low'] = df_Feat['Low'].astype(float, errors = 'raise')
        df_Feat['High'] = df_Feat['High'].astype(float, errors = 'raise')

# In[21]:

df_SandP = df_SandP.interpolate()
df_Nasdaq = df_Nasdaq.interpolate()
df_DJIA = df_DJIA.interpolate()

# In[22]:

### Indicators ###
def simple_moving_average(df):
    df['SMA_5'] = df['Open'].transform(lambda x:x.rolling(window = 5).mean())
    df['SMA_15'] = df['Open'].transform(lambda x:x.rolling(window = 15).mean())
    df['SMA_30'] = df['Open'].transform(lambda x:x.rolling(window = 30).mean())
    df['SMA_60'] = df['Open'].transform(lambda x:x.rolling(window = 60).mean())

    df['SMA_ratio'] = df['SMA_60'] / df['SMA_5']

    return df

def MACD(df):
    df['ewm_15'] = df['Open'].transform(lambda x:x.ewm(span=15, adjust = False).mean())
    df['ewm_30'] = df['Open'].transform(lambda x:x.ewm(span=30, adjust = False).mean())
    df['MACD'] = df['ewm_30'] - df['ewm_15']
    return df

def stochastic_oscillators(df):
    df['LOWEST15D'] = df['Low'].transform(lambda x:x.rolling(window = 15).min())
    df['HIGHEST15D'] = df['High'].transform(lambda x:x.rolling(window = 15).max())

    df['LOWEST30D'] = df['Low'].transform(lambda x:x.rolling(window = 30).min())
    df['HIGHEST30D'] = df['High'].transform(lambda x:x.rolling(window = 30).max())

    df['Stochastic_15'] = ((df['Open'] - df['LOWEST15D'])/(df['HIGHEST15D'] - df['LOWEST15D']))*100
    df['Stochastic_30'] = ((df['Open'] - df['LOWEST30D'])/(df['HIGHEST30D'] - df['LOWEST30D']))*100

    df['Stochastic_%D_15'] = df['Stochastic_15'].rolling(window = 15).mean()
    df['Stochastic_%D_30'] = df['Stochastic_30'].rolling(window = 30).mean()

    df['Stochastic_Ratio'] = df['Stochastic_%D_15']/df['Stochastic_%D_30']
    return df

def ROC(df):
    df['RC_5'] = df['Open'].transform(lambda x: x.pct_change(periods = 5))
    df['RC_15'] = df['Open'].transform(lambda x: x.pct_change(periods = 15))
    df['RC_30'] = df['Open'].transform(lambda x: x.pct_change(periods = 30))
    df['RC_60'] = df['Open'].transform(lambda x: x.pct_change(periods = 60))
    return df

import talib as ta

def RSI(df):
    df['RSI_5']=ta.RSI(np.array(df['Open']), timeperiod=5)
    df['RSI_15']=ta.RSI(np.array(df['Open']), timeperiod=15)
    df['RSI_30']=ta.RSI(np.array(df['Open']), timeperiod=30)
    df['RSI_60']=ta.RSI(np.array(df['Open']), timeperiod=60)
    return df

def ADX(df):
    df['ADX_5']=ta.ADX(np.array(df['High']),np.array(df['Low']), np.array(df['Open']), timeperiod =5)
    df['ADX_15']=ta.ADX(np.array(df['High']),np.array(df['Low']), np.array(df['Open']), timeperiod =15)
    df['ADX_30']=ta.ADX(np.array(df['High']),np.array(df['Low']), np.array(df['Open']), timeperiod =30)
    df['ADX_60']=ta.ADX(np.array(df['High']),np.array(df['Low']), np.array(df['Open']), timeperiod =60)
    return df

# In[23]:

for df_temp in [df_SandP,df_DJIA,df_Nasdaq]:
    df_temp = simple_moving_average(df_temp)
    df_temp = MACD(df_temp)
    df_temp = stochastic_oscillators(df_temp)
    df_temp = ADX(df_temp)
    df_temp = ROC(df_temp)
    df_temp = RSI(df_temp)

# In[24]:

df_SandP = df_SandP.fillna(0)
df_Nasdaq = df_Nasdaq.fillna(0)
df_DJIA = df_DJIA.fillna(0)

# In[25]:

for df_Feat in [df_SandP,df_DJIA,df_Nasdaq]:
    for i in Crude.index:
        if i in df_Feat.index:
            df_Feat.loc[i,'Crude'] = Crude.loc[i]

for df_Feat in [df_SandP,df_DJIA,df_Nasdaq]:
    for i in Gold.index:
        if i in df_Feat.index:
            df_Feat.loc[i,'Gold'] = Gold.loc[i]

# In[26]:

for column in Stock_name:
    df_Target = df['OPrice'].loc[df['symbol'] == column]
    for df_Feat in [df_SandP,df_DJIA,df_Nasdaq]:
        for i in df_Target.index:
            df_Feat.loc[i,column] = df_Target.loc[i]

# In[27]:

df_SandP = df_SandP.drop(['Change %','Vol.','AAPL'],axis = 1)
df_SandP = df_SandP[df_SandP.index < '2012-01-01']
df_SandP = df_SandP[df_SandP.index > '2001-01-01']

df_DJIA = df_DJIA.drop(['Change %','AAPL'],axis = 1)
df_DJIA = df_DJIA[df_DJIA.index < '2012-01-01']
df_DJIA = df_DJIA[df_DJIA.index > '2001-01-01']

df_Nasdaq = df_Nasdaq.drop(['AAPL'],axis = 1)
df_Nasdaq = df_Nasdaq[df_Nasdaq.index < '2012-01-01']
df_Nasdaq = df_Nasdaq[df_Nasdaq.index > '2001-01-01']

# In[28]:

df_SandP = df_SandP.interpolate()
df_Nasdaq = df_Nasdaq.interpolate()
df_DJIA = df_DJIA.interpolate()

df_SandP = df_SandP.iloc[1:,:]
df_Nasdaq = df_Nasdaq.iloc[1:,:]

df_DJIA = df_DJIA.iloc[1:,:]

# In[29]:

df_Nasdaq = df_Nasdaq.drop(["Volume","Adj Close"],axis=1)
df_DJIA = df_DJIA.drop(["Vol."],axis=1)

# In[30]:

def datagen(df,seq_len,batch_size,columns):
    X_train = df[columns].iloc[0:round(0.7*(len(df.index)))]
    batch = []
    index = X_train.index
    columns_list = [c for c in columns]
    while True:
        while True:
            t = random.choice(index)
            n = (X_train.index == t).argmax()
            if n-seq_len+1 < 0:
                continue
            frame = df.iloc[n-seq_len+1:n+1]
            batch.append([frame[columns_list].values, df.loc[t,["AMGN","INTC"]]])
            break
        if len(batch) == batch_size:
            X, y = zip(*batch)
            X, y = np.expand_dims(np.array(X),3),np.array(y)
            yield X, y
            batch = []

# In[31]:

def testgen(df,seq_len,columns):
    batch = []
    columns_list = [c for c in columns]
    target_list = ['AMGN','INTC']
    X_temp = df[columns].iloc[round(0.7*(len(df.index))):]
    t = X_temp.index[0]
    n = (df.index == t).argmax()
    for i in range(n+1, len(df)+1):
        frame = df.iloc[i-seq_len:i]
        batch.append([frame[columns_list].values,frame[["AMGN","INTC"]].iloc[0].values])
    X, y = zip(*batch)
    return np.expand_dims(np.array(X),3), np.array(y)

# In[32]:

def cnnpred_3d(seq_len=60, n_features=35, n_filters=(8,8,8), droprate=0.30):
    "3D-CNNpred model according to the paper"
    model = Sequential([
        Input(shape=(seq_len, n_features, 1)),
        Conv2D(n_filters[0], kernel_size=(1,1), activation="relu", data_format="channels_last"),
        Conv2D(n_filters[1], kernel_size=(1,3), activation="relu"),
        MaxPool2D(pool_size=(1,2)),
        Flatten(),
        Dropout(droprate),
        Dense(2, activation="relu")
    ])
    return model

# In[33]:

def MSE(y_true,y_pred):
    MSE_score = mse(y_true,y_pred)
    return MSE_score

# In[34]:

def CNN_Model(df):
    seq_len    = 60
    batch_size = 128
    n_epochs   = 30
    n_features = 35
    n_stocks   = 1
    model = cnnpred_3d(seq_len, n_features)
    model.compile(optimizer="adam", loss="mse", metrics=["mae"])
    model.summary()

    df_train = df.iloc[0:round(0.7*(len(df.index)))]
    columns = df_train.columns
    columns = columns[:-2]

    scaler = StandardScaler().fit(df_train[columns])
    df[columns] = scaler.transform(df[columns])

    model.fit(datagen(df,seq_len,batch_size,columns),epochs=n_epochs, steps_per_epoch=400, validation_steps=None, verbose=1)
    test_data, test_target = testgen(df, seq_len, columns)
    test_out = model.predict(test_data)
    W = mean_absolute_error(test_out, test_target)
    print("Mean Absolute Error: %.2f", W)

    plt.figure(1)
    plt.plot(test_out[:,0],label = "Pred")
    plt.plot(test_target[:,0],label = "Actual")
    plt.legend()
    plt.title("Actual vs. Prediction Plot - AMGEN")
    plt.xlabel("Date")
    plt.ylabel("Stock Price")
    plt.show()

    plt.figure(2)
    plt.plot(test_out[:,1],label = "Pred")
    plt.plot(test_target[:,1],label = "Actual")
    plt.legend()
    plt.title("Actual vs. Prediction Plot - Intel")
    plt.xlabel("Date")
    plt.ylabel("Stock Price")
    plt.show()

    return W

# In[35]:

A = CNN_Model(df_SandP)

# In[36]:

A

# In[37]:

B = CNN_Model(df_Nasdaq)

# In[38]:

B

# In[39]:

C = CNN_Model(df_DJIA)

# In[40]:

C

# In[41]:

from numpy import split
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
#from  WindowNormalizationDailyPredict import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.callbacks import ModelCheckpoint
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.regularizers import L1L2
from sklearn.metrics import r2_score, f1_score, mean_squared_error as mse, mean_absolute_error as mae

# In[52]:

#lstm
def to_supervised(train, n_input, n_out):
    data=train
    # flatten data
    #data = train.reshape((train.shape[0]*train.shape[1], train.shape[2])) #needed if train is weekly data
    X, y = list(), list()
    in_start = 0
    # step over the entire history one time step at a time
    for _ in range(len(data)):
        # define the end of the input sequence
        in_end = in_start + n_input
        out_end = in_end + n_out
        # ensure we have enough data for this instance
        if out_end < len(data):
            X.append(data.loc[data.index[in_start:in_end], :]) #process all columns
            y.append(data.loc[data.index[in_end:out_end], ['AMGN','INTC']]) #leftmost column = Open has the target categories
        # move along one time step
        in_start += 1
    return np.array(X), np.array(y)

# In[53]:

#S&P500

# Data preparation for lstm
train_SandP = df_SandP.iloc[0:round(0.7*(len(df_SandP.index)))]
test_SandP = df_SandP.iloc[round(0.7*(len(df_SandP.index))):]

n_input = 7

X_train_SandP, y_train_SandP = to_supervised(train_SandP, n_input, n_out=1)
X_test_SandP, y_test_SandP = to_supervised(test_SandP, n_input, n_out=1)
alpha = y_train_SandP.reshape(y_train_SandP.shape[0], y_train_SandP.shape[1] * y_train_SandP.shape[2])

# define parameters
import keras
verbose, epochs, batch_size = 1, 150, 16 #1,150,16

n_timesteps, n_features, n_outputs = X_train_SandP.shape[1], X_train_SandP.shape[2], alpha.shape[1]

# define model

model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features), return_sequences = True,
               dropout=0.0, recurrent_dropout=0.0, kernel_regularizer=L1L2(0.0, 0.0)))
model.add(LSTM(200, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0))
model.add(Dense(n_outputs))

model.compile(loss='mse', optimizer='adam')

callbacks_list = [
    ModelCheckpoint(
        filepath="LSTM-weights-best.hdf5",
        monitor = 'val_loss',
        save_best_only=True,
        mode='auto',
        verbose=1
        ),
    ReduceLROnPlateau(
        monitor = 'val_loss',
        factor = 0.1,
        patience=3,
        verbose=1
        ),
    EarlyStopping(
        monitor = 'val_loss',
        patience=2,
        verbose=1
        )
    ]

model.summary()

# fit network
history  = model.fit(X_train_SandP, alpha, epochs=epochs, batch_size=batch_size, verbose=verbose, validation_split=0.2,callbacks=callbacks_list,shuffle=True)

model = keras.models.load_model("LSTM-weights-best.hdf5")

#############################################################################################################

train_loss_values = history.history["loss"] #training loss
val_loss_values = history.history["val_loss"] #validation loss
epochs = range(1,len(train_loss_values)+1)

plt.clf()

df = pd.DataFrame(train_loss_values, columns=["training_loss"], index=epochs)
df["validation_loss"] = val_loss_values
title="training loss vs validation loss"
df[['training_loss','validation_loss']].plot(grid=True,figsize=(8,5))
plt.show()

print("val_loss_value: ", val_loss_values[-1])

#############################################################################################################

predictions = list()

# make a forecast

for i in range(X_test_SandP.shape[0]):
	# predict the week
	input_x = X_test_SandP[i,:,:]
    # reshape into [1, n_input, n]
	input_x = input_x.reshape(1, input_x.shape[0], input_x.shape[1])
	# forecast the next week
	yhat = model.predict(input_x, verbose=0)
	# we only want the vector forecast
	yhat = yhat[0]
	yhat_sequence = yhat
	# store the predictions
	predictions.append(yhat_sequence)
# evaluate predictions days for each week
predictions = np.array(predictions)

actual1 = []
actual2 = []
for i in range(y_test_SandP.shape[0]):
    actual1.append(y_test_SandP[i][0][0])
    actual2.append(y_test_SandP[i][0][1])

pred1 = []
pred2 = []
for i in range(predictions.shape[0]):
    pred1.append(predictions[i][0])
    pred2.append(predictions[i][1])

plt.plot(pred1,label = "prediction")
plt.plot(actual1, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test1 = mae(actual1,pred1)
print(MAE_Score_test1)

plt.plot(pred2,label = "prediction")
plt.plot(actual2, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test2 = mae(actual2,pred2)
print(MAE_Score_test2)

# In[54]:

#DJIA

# Data preparation for lstm
train = df_DJIA.iloc[0:round(0.7*(len(df_DJIA.index)))]
test = df_DJIA.iloc[round(0.7*(len(df_DJIA.index))):]

n_input = 7

X_train, y_train = to_supervised(train, n_input, n_out=1)
X_test, y_test = to_supervised(test, n_input, n_out=1)
alpha = y_train.reshape(y_train.shape[0], y_train.shape[1] * y_train.shape[2])

# define parameters
import keras
verbose, epochs, batch_size = 1, 150, 16 #1,150,16

n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], alpha.shape[1]

# define model

model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features), return_sequences = True,
               dropout=0.0, recurrent_dropout=0.0, kernel_regularizer=L1L2(0.0, 0.0)))
model.add(LSTM(200, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0))
model.add(Dense(n_outputs))

model.compile(loss='mse', optimizer='adam')

callbacks_list = [
    ModelCheckpoint(
        filepath="LSTM-weights-best.hdf5",
        monitor = 'val_loss',
        save_best_only=True,
        mode='auto',
        verbose=1
        ),
    ReduceLROnPlateau(
        monitor = 'val_loss',
        factor = 0.1,
        patience=3,
        verbose=1
        ),
    EarlyStopping(
        monitor = 'val_loss',
        patience=2,
        verbose=1
        )
    ]

model.summary()

# fit network
history  = model.fit(X_train, alpha, epochs=epochs, batch_size=batch_size, verbose=verbose, validation_split=0.2,callbacks=callbacks_list,shuffle=True)

model = keras.models.load_model("LSTM-weights-best.hdf5")

#############################################################################################################

train_loss_values = history.history["loss"] #training loss
val_loss_values = history.history["val_loss"] #validation loss
epochs = range(1,len(train_loss_values)+1)

plt.clf()

df = pd.DataFrame(train_loss_values, columns=["training_loss"], index=epochs)
df["validation_loss"] = val_loss_values
title="training loss vs validation loss"
df[['training_loss','validation_loss']].plot(grid=True,figsize=(8,5))
plt.show()

print("val_loss_value: ", val_loss_values[-1])

#############################################################################################################

predictions = list()

# make a forecast

for i in range(X_test.shape[0]):
	# predict the week
	input_x = X_test[i,:,:]
    # reshape into [1, n_input, n]
	input_x = input_x.reshape(1, input_x.shape[0], input_x.shape[1])
	# forecast the next week
	yhat = model.predict(input_x, verbose=0)
	# we only want the vector forecast
	yhat = yhat[0]
	yhat_sequence = yhat
	# store the predictions
	predictions.append(yhat_sequence)
# evaluate predictions days for each week
predictions = np.array(predictions)

actual1 = []
actual2 = []
for i in range(y_test.shape[0]):
    actual1.append(y_test[i][0][0])
    actual2.append(y_test[i][0][1])

pred1 = []
pred2 = []
for i in range(predictions.shape[0]):
    pred1.append(predictions[i][0])
    pred2.append(predictions[i][1])

plt.plot(pred1,label = "prediction")
plt.plot(actual1, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test1 = mae(actual1,pred1)
print(MAE_Score_test1)

plt.plot(pred2,label = "prediction")
plt.plot(actual2, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test2 = mae(actual2,pred2)
print(MAE_Score_test2)

# In[55]:

#NASDAQ

# Data preparation for lstm
train = df_Nasdaq.iloc[0:round(0.7*(len(df_Nasdaq.index)))]
test = df_Nasdaq.iloc[round(0.7*(len(df_Nasdaq.index))):]

n_input = 7

X_train, y_train = to_supervised(train, n_input, n_out=1)
X_test, y_test = to_supervised(test, n_input, n_out=1)
alpha = y_train.reshape(y_train.shape[0], y_train.shape[1] * y_train.shape[2])

# define parameters
import keras
verbose, epochs, batch_size = 1, 150, 16 #1,150,16

n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], alpha.shape[1]

# define model

model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features), return_sequences = True,
               dropout=0.0, recurrent_dropout=0.0, kernel_regularizer=L1L2(0.0, 0.0)))
model.add(LSTM(200, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0))
model.add(Dense(n_outputs))

model.compile(loss='mse', optimizer='adam')

callbacks_list = [
    ModelCheckpoint(
        filepath="LSTM-weights-best.hdf5",
        monitor = 'val_loss',
        save_best_only=True,
        mode='auto',
        verbose=1
        ),
    ReduceLROnPlateau(
        monitor = 'val_loss',
        factor = 0.1,
        patience=3,
        verbose=1
        ),
    EarlyStopping(
        monitor = 'val_loss',
        patience=2,
        verbose=1
        )
    ]

model.summary()

# fit network
history  = model.fit(X_train, alpha, epochs=epochs, batch_size=batch_size, verbose=verbose, validation_split=0.2,callbacks=callbacks_list,shuffle=True)

model = keras.models.load_model("LSTM-weights-best.hdf5")

#############################################################################################################

train_loss_values = history.history["loss"] #training loss
val_loss_values = history.history["val_loss"] #validation loss
epochs = range(1,len(train_loss_values)+1)

plt.clf()

df = pd.DataFrame(train_loss_values, columns=["training_loss"], index=epochs)
df["validation_loss"] = val_loss_values
title="training loss vs validation loss"
df[['training_loss','validation_loss']].plot(grid=True,figsize=(8,5))
plt.show()

print("val_loss_value: ", val_loss_values[-1])

#############################################################################################################

predictions = list()

# make a forecast

for i in range(X_test.shape[0]):
	# predict the week
	input_x = X_test[i,:,:]
    # reshape into [1, n_input, n]
	input_x = input_x.reshape(1, input_x.shape[0], input_x.shape[1])
	# forecast the next week
	yhat = model.predict(input_x, verbose=0)
	# we only want the vector forecast
	yhat = yhat[0]
	yhat_sequence = yhat
	# store the predictions
	predictions.append(yhat_sequence)
# evaluate predictions days for each week
predictions = np.array(predictions)

actual1 = []
actual2 = []
for i in range(y_test.shape[0]):
    actual1.append(y_test[i][0][0])
    actual2.append(y_test[i][0][1])

pred1 = []
pred2 = []
for i in range(predictions.shape[0]):
    pred1.append(predictions[i][0])
    pred2.append(predictions[i][1])

plt.plot(pred1,label = "prediction")
plt.plot(actual1, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test1 = mae(actual1,pred1)
print(MAE_Score_test1)

plt.plot(pred2,label = "prediction")
plt.plot(actual2, label = "actual")
plt.legend()
plt.plot()

MAE_Score_test2 = mae(actual2,pred2)
print(MAE_Score_test2)

# In[56]:

mse(actual2,pred2)

# In[57]:

mse(actual1,pred1)

# In[ ]:


import nltk
from nltk.chat.util import Chat, reflections

reflections ={
    "hello" : "hey !",
    "my" : "your",
    "i am" : "you are",
    "you" : "me",
    "i will" : "you will",
    "your" : "my",
    "me" : "you"
    ""
}

pairs = [
[
    r"hi|hello|hey",
    ["Hello , Whats goin on !",]
],
[
    r"how are you",
    ["I am fine , how are you",]
],
[
    r"i am fine as well",
    ["Good to Hear That",]
],
[
    r"what is your name",
    ["My Name is Gintoki Sakata",]
],
[
    r"what is your age",
    ["its been a whole day.",]
],
[
    r"who created you ?",
    ["i was created by Sourav Singh",]
],
[
    r"how do you think so fast",
    ["i am fast as i am not real just a program",]
],
[
    r"who is moron",
    ["Siddhant is moron",]
],
[
    r"what is your fav game",
    ["my favourite game is Samurai Vengence",]
],
[
    r"what is your fav anime",
    ["GTO - Great Teacher Onizuka",]
],
[
    r"how is life going on",
    ["good",]
],
]

def chat():
    print("Hi ! I am a chat bot")
    chat = Chat(pairs, reflections)
    chat.converse()

if __name__ == "__main__":
    chat()


import numpy as np
import utm

from Data_Processing import denormalize_lat, denormalize_long
from matplotlib import pyplot as plt
import gif
from pyproj import Transformer

def get_lat_long_values(predicted_values, actual_values):
    latitude_predict = []
    longitude_predict = []

    for lat in range(len(predicted_values)):
        denormalize_lat_value = denormalize_lat(predicted_values[lat, 0])
        latitude_predict.append(denormalize_lat_value)

    for long in range(len(predicted_values)):
        denormalize_long_value = denormalize_long(predicted_values[long, 1])
        longitude_predict.append(denormalize_long_value)

    actual_values["LATITUDE"] = actual_values["LATITUDE"].apply(denormalize_lat)
    actual_values["LONGITUDE"] = actual_values["LONGITUDE"].apply(denormalize_long)

    latitude_actual = actual_values["LATITUDE"].values
    longitude_actual = actual_values["LONGITUDE"].values

    return latitude_predict, longitude_predict, latitude_actual, longitude_actual

@gif.frame
def plot(i, lon_pre, lat_pre, lon_act, lat_act):
    x_actual = lon_act[i]
    y_actual = lat_act[i]
    actual = plt.scatter(x_actual, y_actual)

    x_predict = lon_pre[i]
    y_predict = lat_pre[i]
    predict = plt.scatter(x_predict, y_predict)

    plt.xlim((-2.92, -2.89))
    plt.ylim((43.92, 43.96))

    plt.xlabel("Longitude")
    plt.ylabel("Latitude")

    plt.legend(handles=[predict, actual], labels=['Predict Position', 'Actual Position'], loc='upper right')

def draw_on_earth(predicted_values, actual_values):
    latitude_predict, longitude_predict, latitude_actual, longitude_actual = get_lat_long_values(predicted_values, actual_values)

    transformer = Transformer.from_crs("epsg:32730", "epsg:4326")
    print(latitude_predict)
    print(longitude_predict)

    predict_lat = []
    predict_long = []

    actual_lat = []
    actual_long = []

    for num in range(len(latitude_predict)):
        x = latitude_predict[num]
        y = longitude_predict[num]
        lat, long = utm.to_latlon(500000 - y, x, 30, 'S')
        predict_lat.append(lat)
        predict_long.append(long)

    for num in range(len(latitude_actual)):
        x = latitude_actual[num]
        y = longitude_actual[num]

        lat, long = utm.to_latlon(500000 - y, x, 30, 'S')
        actual_lat.append(lat)
        actual_long.append(long)

    print(predict_long)
    print(predict_lat)

    gif.options.matplotlib["dpi"] = 300

    frames = []

    for i in range(len(latitude_predict)):
        frame = plot(i, predict_long, predict_lat, actual_long, actual_lat)
        frames.append(frame)

    gif.save(frames, './result.gif', duration=120)


# python3
# Anna Kūliņa 14.gr.

def build_heap(data):
    swaps = []
    gar = len(data)
    for i in range(gar//2 - 1, -1, -1):
        a = i
        while 2*a+1 < gar:
            kr = 2*a+1
            if kr+1 < gar and data[kr+1] < data[kr]:
                kr = kr+1
            if data[a] >= data[kr]:
                swaps.append((a, kr))
                data[a], data[kr] = data[kr], data[a]
                a = kr
            else:
                break

    return swaps

def main():

    text = input("Ievadiet F testiem vai I manuālai ievadei: ")
    if 'F' in text:
        fails = input("Ievadiet faila nosaukumu: ")
        if fails:
            try:
                with open("./tests/" + fails) as fails1:
                    f = fails1.readlines()
                    n = int(f[0])
                    data = list(map(int, f[1].split()))
                    assert len(data) == n
                    swaps = build_heap(data)
            except FileNotFoundError:
                print("Fails nav atrasts")
                return

    elif 'I' in text:
        n = int(input())
        data = list(map(int, input().split()))
        assert len(data) == n
        swaps = build_heap(data)

    else:
        print("Nepareiza komanda")
        return

    print(len(swaps))
    for i, j in swaps:
        print(i, j)

if __name__ == "__main__":
    main()


from sourpea.primitives import *
from sourpea.util import *
import pandas as pd
import os
_dir=os.path.dirname(__file__)
### DERIVED FACTORS
vyl = Factor("vyl", ["nfji","ewcnwl","zhrtv","gevbzq","mutl","omuphx"])
lytpda = Factor("lytpda", ["hmeuy","uqjxcp","bgh","kiwcz","rzqhjz","ewmcgk","hxdqz","ygzcc"])
def jjond(vyl, lytpda):
     return "omuphx" == vyl[-1] and lytpda[0] != "uqjxcp"
def kwefzx(vyl, lytpda):
     return "omuphx" != vyl[-1] and lytpda[0] == "uqjxcp"
def emotn(vyl, lytpda):
     return (vyl[-1] != "omuphx") and (lytpda[0] != "uqjxcp")
nrc = DerivedLevel("zigcr", Transition(jjond, [vyl, lytpda]))
ffkul = DerivedLevel("ltkzxi", Transition(kwefzx, [vyl, lytpda]))
vombk = DerivedLevel("xinlkr", Transition(emotn, [vyl, lytpda]))
togld = Factor("jljwku", [nrc, ffkul, vombk])

### EXPERIMENT
design=[vyl,lytpda,togld]
crossing=[togld]
### APPENDIX
block=Block(design,crossing,[])
sequence_code_1=trials_from_csv(os.path.join(_dir,"out_code_1/3_2_2_0.csv"))
nr_input_factors=2
nr_output_levels=3
test_code_1=block.test(sequence_code_1)
try:
	sequence_code_2=trials_from_csv(os.path.join(_dir,"out_code_2/3_2_2_0.csv"))
	test_code_2=block.test(sequence_code_2)
except:
	test_code_2={"pValue":0}
df=pd.read_csv(os.path.join(_dir,"result_sour.csv"))
df.loc[len(df.index)] = [test_code_1["pValue"], test_code_2["pValue"], test_code_1["levels"], test_code_2["levels"], nr_input_factors, nr_output_levels]
df.to_csv(os.path.join(_dir,"result_sour.csv"), index=False)


"""
data structure yang digunakan
data = {
    'A': ('B', 10),
    'B': ('C', 100)
}
"""

data = {}

# Menerima input dari user dan berhenti jika menemui input selesai

def get_input():
    while True:
        user_input = input()
        if user_input == "SELESAI":
            break
        nama, kenalan, jarak = user_input.split()
        data[nama] = (kenalan, float(jarak))  # Menambahkan data ke dict

# melakuan search dan menghitung jarak dari awal dan akhir

def search(start, finish):
    # base case, jika tidak dapat dilakukan search lebih lanjut
    # dan finish masih belum ditemukan maka raise ValueError
    if start not in data:
        raise ValueError()
    # ambil data next node yang akan menggantikan start dan juga jarak
    next_node, path_len = data[start]
    # jika next_node yang akan dicari merupakan finish
    # maka langsung return jarak
    if next_node == finish:
        return path_len
    # jika bukan maka tambahkan jarak lalu lanjut kan rekursi dengan next_node
    return path_len + search(next_node, finish)

# main function

def main():
    print("Masukkan data hubungan:")
    get_input()
    print()
    start = input("Masukkan nama awal: ")
    end = input("Masukkan nama tujuan: ")
    try:  # Melakukan printing output sesuai dengan nilai jarak total
        jarak_total = search(start, end) * 10
        print(f"Jarak total: {int(jarak_total)}")
        if jarak_total > 1000:
            print(f"{start} dan {end} tidak saling kenal.")
        elif 100 < jarak_total <= 1000:
            print(f"{start} dan {end} mungkin saling kenal.")
        elif 0 < jarak_total <= 100:
            print(f"{start} dan {end} kenal dekat.")
    except ValueError:  # jika function search mendapatkan value error berarti user tidak ditemukan
        print(f"Tidak ada hubungan antara {start} dan {end}.")

if __name__ == '__main__':
    main()


## import
import maya.cmds as cmds
import maya.mel as mel
## import libs
import common.transforms as transforms
import namingAPI.naming as naming
import riggingAPI.joints as joints
import riggingAPI.constraints as constraints
import riggingAPI.controls as controls
import riggingAPI.rigLimbs.baseLimbs.ikRPsolverRig as ikRPsolverRig
import modelingAPI.curves as curves
## function
class armIkRig(ikRPsolverRig.ikRPsolverRig ):
	"""docstring for armIkRig"""
	def __init__(self, *args, **kwargs):
		super(armIkRig, self).__init__(*args, **kwargs)

		self._sPart = 'arm'
		self._bOrient = True
		self._sName = 'Ik'

	@property
	def lJntsOutput(self):
		return self._lJntsOutput

	def create(self):
		self._lBpJnts = [
							naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'upperArm').sName,
							naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'elbow').sName,
							naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'wrist').sName,
							naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'wristEnd').sName,
						]

		self._sBpCtrl = naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'handIk').sName
		self._sBpPv = naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'elbowPvIk').sName
		self._sBpRoot = naming.oName(sType = 'bpJnt', sSide = self._sSide, sPart = 'upperArmIk').sName

		super(armIkRig, self).create()

		sString_lJntsOutput = self._convertListToString(self._lJnts[:-1])
		lRigInfo = ['armIkRig', sString_lJntsOutput]
		lAttrs = ['sModuleType', 'lJntsOutput']
		self._writeRigInfo(self._sModuleNode, lRigInfo, lAttrs)

		self._getRigInfo(self._sModuleNode)

	def _getRigInfo(self, sModuleNode):
		super(armIkRig, self)._getRigInfo(sModuleNode)
		if cmds.attributeQuery('lJntsOutput', node = sModuleNode, ex = True):
			sJntsOutputString = cmds.getAttr('%s.lJntsOutput' %sModuleNode)
			self._lJntsOutput = sJntsOutputString.split(',')


#!/usr/bin/python3
import os
import unittest

from actions import (MakeDirectoryBuildAction, RunShellCommandBuildAction,
                     SpyBuildAction)

class BuildActionTest(unittest.TestCase):
    def test_to_str(self) -> None:
        action = SpyBuildAction()
        self.assertEqual(
            str(action),
            "SpyBuildAction",
        )

class MakeDirectoryBuildActionTest(unittest.TestCase):
    def test_creates_directory(self) -> None:
        path = "foobar"
        action = MakeDirectoryBuildAction(path)
        action.execute()

        self.assertTrue(os.path.exists(path))

        os.rmdir(path)

class RunShellCommandBuildActionTest(unittest.TestCase):
    def test_shell_command_runs(self) -> None:
        path = "foobar"
        action = RunShellCommandBuildAction(["touch", path])
        action.execute()

        self.assertTrue(os.path.exists(path))

        os.remove(path)

class SpyBuildActionTest(unittest.TestCase):
    def test_spy_build_action_not_called_by_default(self) -> None:
        action = SpyBuildAction()
        self.assertRaises(AssertionError, action.assert_called)

    def test_spy_build_action_called_once(self) -> None:
        action = SpyBuildAction()
        action.execute()
        action.assert_called()

    def test_spy_build_can_be_called_at_most_once(self) -> None:
        action = SpyBuildAction()
        action.execute()
        self.assertRaises(AssertionError, action.execute)

if __name__ == "__main__":
    unittest.main()


from tensorflow import keras

from dlgo.data.datasets import KGSDataSet
from dlgo.data.encoders import OnePlaneEncoder
from dlgo.data.generators import repeat
from dlgo.neural.networks import small

board_size = 19
num_classes = board_size ** 2
num_games = 100

encoder = OnePlaneEncoder(board_size)
dataset = KGSDataSet(encoder)
generator = dataset.load_data('train', num_games, use_generator=True)
test_generator = dataset.load_data('test', num_games, use_generator=True)

input_shape = encoder.board_shape
model = small(input_shape, num_classes)
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

epochs = 5
batch_size = 128

model.fit_generator(generator=repeat(generator.generate(batch_size)), epochs=epochs,
                    steps_per_epoch=generator.length(batch_size),
                    validation_data=repeat(test_generator.generate(batch_size)),
                    validation_steps=test_generator.length(batch_size),
                    callbacks=[keras.callbacks.ModelCheckpoint('../../data/checkpoints/small_model_epoch_{epoch}.h5')])

model.evaluate_generator(generator=test_generator.generate(batch_size, num_classes),
                         steps=test_generator.length(batch_size))


import argparse
import logging

import foremastutils

from ..args import add_debug
from ..consts import APP_FORMATS, LOGGING_FORMAT
from .outputs import write_variables
from .prepare_configs import process_git_configs, process_runway_configs

LOG = logging.getLogger(__name__)

def main():
    """Append Application Configurations to a given file in multiple formats."""
    logging.basicConfig(format=LOGGING_FORMAT)

    parser = argparse.ArgumentParser(description=main.__doc__)
    add_debug(parser)
    parser.add_argument('-o', '--output', required=True, help='Name of environment file to append to')
    parser.add_argument(
        '-g', '--git-short', metavar='GROUP/PROJECT', required=True, help='Short name for Git, e.g. forrest/core')
    parser.add_argument('-r', '--runway-dir', help='Runway directory with app.json files, requires --git-short')
    args = parser.parse_args()

    LOG.setLevel(args.debug)
    logging.getLogger(__package__.split('.')[0]).setLevel(args.debug)

    generated = foremastutils.Generator(*foremastutils.Parser(args.git_short).parse_url(), formats=APP_FORMATS)
    git_short = generated.gitlab()['main']

    if args.runway_dir:
        configs = process_runway_configs(runway_dir=args.runway_dir)
    else:
        configs = process_git_configs(git_short=git_short)

    write_variables(app_configs=configs, out_file=args.output, git_short=git_short)

if __name__ == '__main__':
    main()


import dash  # version 1.13.1
from dash import dcc, html
from dash.dependencies import Input, Output, ALL, State, MATCH, ALLSMALLER
import dash_bootstrap_components as dbc
import plotly
import plotly.express as px
import pandas as pd
import numpy as np

app = dash.Dash(__name__, use_pages=True, external_stylesheets=[dbc.themes.SPACELAB]) #use_pages=True,

'''
sidebar = dbc.Nav(
            [
                dbc.NavLink(
                    [
                        html.Div(page["name"], className="ms-2"),
                    ],
                    href=page["path"],
                    active="exact",
                )
                for page in dash.page_registry.values()
            ],
            vertical=True,
            pills=True,
            className="bg-light",
)
'''
dash.register_page(__name__,
                   path='/',  # '/' is home page and it represents the url
                   name='Home',  # name of page, commonly used as name of link
                   title='Index',  # title that appears on browser's tab
                   image='pg1.png',  # image in the assets folder
                #    description='Histograms are the new bar charts.'
)

df = pd.read_csv("caspar_envcan.csv", low_memory=False)
df.rename(columns={'predictionTime': 'date', 'source': 'source', 'temp': 'temp',
                    'windDir': 'wind direction', 'windSpeed': 'wind speed',
                    'pressure': 'pressure', 'relHumidity': 'relative humidity',
                    'humidity': 'humidity'}, inplace=True)
#update pd
#df['predictionTime'] = pd.to_datetime(df['predictionTime'])

#all values >50 in temp column, are set = 50
# for x in df.index:
#     if df.loc[x, "temp"] > 50:
#         df.loc[x, "temp"] = 50

#removes all rows with cells empty or NaN
# df.dropna(inplace = True)

# print(df.to_string())

#Header_component = html.H1("Environment Dashboard") #, style={'color':'darkred'}

layout = dbc.Container([
    dbc.Row(
        dbc.Col(html.H1("Environment Dashboard",
                        className='text-danger text-center, mb-4'),
                width=10),
        #dbc.Col()
    ),

    dbc.Row([

        dbc.Col(html.Div([html.Button('Add Chart',
            id='add-chart', n_clicks=0)],))
        ]),

    dbc.Row([
        html.Div(id='container', children=[]),
    ]),

        #To-do
        html.Div('Weather Dash To-Do list'),
        dcc.Input(id="new-item"),
        html.Button("Add", id="add"),
        html.Button("Clear Done", id="clear-done"),
        html.Div(id="list-container"),
        html.Div(id="totals")
], fluid=False)

@app.callback(
    Output('container', 'children'),
    [Input('add-chart', 'n_clicks')],
    [State('container', 'children')]
)
def display_graphs(n_clicks, div_children):
    new_child = html.Div(
        style={'width': '85%', 'display': 'inline-block', 'outline': 'thin lightgrey solid', 'padding': 10},
        children=[
            dcc.Graph(
                id={
                    'type': 'dynamic-graph',
                    'index': n_clicks
                },
                figure={}
            ),
            dcc.RadioItems(
                id={
                    'type': 'dynamic-choice',
                    'index': n_clicks
                },
                options=[{'label': 'Line Chart', 'value': 'line'},
                         {'label': 'Histogram', 'value': 'histogram'},
                         {'label': 'Pie Chart', 'value': 'pie'}],
                value='line',
            ),
            dcc.Dropdown(
                id={
                    'type': 'dynamic-dpn-s',
                    'index': n_clicks
                },
                options=[{'label': s, 'value': s} for s in np.sort(df['source'].unique())],
                multi=True,
                value=["caspar", "envcan"],
            ),
            dcc.Dropdown(
                id={
                    'type': 'dynamic-dpn-temp',
                    'index': n_clicks
                },
                options=[{'label': c, 'value': c} for c in ['relative humidity', 'humidity', 'wind direction', 'wind speed', 'pressure', 'source', 'temp']],
                value='source',
                clearable=True
            ),

            dcc.Dropdown(
                id={
                    'type': 'dynamic-dpn-win',
                    'index': n_clicks
                },
                options=[{'label': n, 'value': n} for n in ['relative humidity', 'humidity', 'wind direction', 'wind speed', 'pressure', 'source', 'temp']],
                value='temp',
                clearable=True
            )
        ]
    )
    div_children.append(new_child)
    return div_children

@app.callback(
    Output({'type': 'dynamic-graph', 'index': MATCH}, 'figure'),
    [Input(component_id={'type': 'dynamic-dpn-s', 'index': MATCH}, component_property='value'),
     Input(component_id={'type': 'dynamic-dpn-temp', 'index': MATCH}, component_property='value'),
     Input(component_id={'type': 'dynamic-dpn-win', 'index': MATCH}, component_property='value'),
     Input({'type': 'dynamic-choice', 'index': MATCH}, 'value')]
)
def update_graph(s_value, ctg_value, num_value, chart_choice):
    print(s_value)
    dff = df[df['source'].isin(s_value)]

    if chart_choice == 'line':
        dff = dff.groupby([ctg_value, 'date'], as_index=False)[['relative humidity', 'humidity', 'wind direction', 'wind speed', 'temp', 'pressure']].mean()
        fig = px.line(dff, x='date', y=num_value, color=ctg_value)
        return fig
    elif chart_choice == 'bar':
        if len(s_value) == 0:
            return {}

        else:
            dff = dff.groupby([ctg_value], as_index=False)[['relative humidity', 'humidity', 'wind direction', 'wind speed', 'pressure', 'source', 'temp']].mean()
            fig = px.histogram(dff, x='date', y=num_value, histfunc='avg')
            return fig

    elif chart_choice == 'pie':
        fig = px.pie(dff, names=ctg_value, values=num_value)
        return fig

#To-do open---------------------------------------------------->>>>>>>>>
#To-do
style_todo = {"display": "inline", "margin": "10px"}
style_done = {"textDecoration": "line-through", "color": "#888"}
style_done.update(style_todo)

@app.callback(
    [
        Output("list-container", "children"),
        Output("new-item", "value")
    ],
    [
        Input("add", "n_clicks"),
        Input("new-item", "n_submit"),
        Input("clear-done", "n_clicks")
    ],
    [
        State("new-item", "value"),
        State({"index": ALL}, "children"),
        State({"index": ALL, "type": "done"}, "value")
    ]
)
def edit_list(add, add2, clear, new_item, items, items_done):
    triggered = [t["prop_id"] for t in dash.callback_context.triggered]
    adding = len([1 for i in triggered if i in ("add.n_clicks", "new-item.n_submit")])
    clearing = len([1 for i in triggered if i == "clear-done.n_clicks"])
    new_spec = [
        (text, done) for text, done in zip(items, items_done)
        if not (clearing and done)
    ]
    if adding:
        new_spec.append((new_item, []))
    new_list = [
        html.Div([
            dcc.Checklist(
                id={"index": i, "type": "done"},
                options=[{"label": "", "value": "done"}],
                value=done,
                style={"display": "inline"},
                labelStyle={"display": "inline"}
            ),
            html.Div(text, id={"index": i}, style=style_done if done else style_todo)
        ], style={"clear": "both"})
        for i, (text, done) in enumerate(new_spec)
    ]
    return [new_list, "" if adding else new_item]

@app.callback(
    Output({"index": MATCH}, "style"),
    Input({"index": MATCH, "type": "done"}, "value")
)
def mark_done(done):
    return style_done if done else style_todo

@app.callback(
    Output("totals", "children"),
    Input({"index": ALL, "type": "done"}, "value")
)
def show_totals(done):
    count_all = len(done)
    count_done = len([d for d in done if d])
    result = "{} of {} items completed".format(count_done, count_all)
    if count_all:
        result += " - {}%".format(int(100 * count_done / count_all))
    return result
## To-do close <<<<<<<<<<<<--------------------------------------

    # if chart_choice == 'bar':
    #     dff = dff.groupby([ctg_value], as_index=False)[['detenues', 'under trial', 'convicts', 'others']].sum()
    #     fig = px.bar(dff, x=ctg_value, y=num_value)
    #     return fig
    # elif chart_choice == 'line':
    #     if len(s_value) == 0:
    #         return {}
    #     else:
    #         dff = dff.groupby([ctg_value, 'year'], as_index=False)[['detenues', 'under trial', 'convicts', 'others']].sum()
    #         fig = px.line(dff, x='year', y=num_value, color=ctg_value)
    #         return fig
    # elif chart_choice == 'pie':
    #     fig = px.pie(dff, names=ctg_value, values=num_value)
    #     return fig

# if __name__ == '__main__':
#     app.run_server(debug=True)

# https://youtu.be/4gDwKYaA6ww


import itertools

def print_matrix(w, h, x=0, y=0):
    #确定初始坐标
    op = [(1, 0), (-1, 0), (0, -1), (0, 1)]
    #四个旋转方向
    direction = itertools.cycle(op)
    result = {(xx, yy): None for xx in range(w) for yy in range(h)}
    result[(x, y)] = 1
    _x, _y = next(direction)
    #2.3用法有差别
    i = 1
    flag = 0
    #四次结束一个大循环

    while True:
        new_x = x + _x
        new_y = y + _y
        if (new_x, new_y) in result and result[(new_x, new_y)] is None:
            #判断是否有值，是否需要
            i += 1
            result[(new_x, new_y)] = i
            x = new_x
            y = new_y
            flag = 0

        else:
            _x, _y = next(direction)
            flag += 1
            if flag > 4:
                break

    for y in range(h):
        for x in range(w):
            print("%3d"% (result[(x, y)]),end='')
        print()

print_matrix(6, 6, 0, 0)


import sys, re

numbers = sys.stdin.readline()
numbers = re.findall('-?\d*\.?\d+', numbers)

#numbers = list(map(int, input))
numbers = [float(each) for each in numbers]

mean = sum(numbers)/len(numbers)

'''
import statistics
mean = statistics.mean(numbers)
'''

print("%.2f" %mean) #precission 2


import threading
import argparse
import json
import time
import requests
from pathlib import Path

links = ['https://en.wikipedia.org/wiki/Wikipedia',
         'https://en.wikipedia.org/wiki/Wikipedia',
         'https://en.wikipedia.org/wiki/Main_Page',
         'https://en.wikipedia.org/wiki/COVID-19_pandemic',
         'https://ru.wikipedia.org/wiki/Пандемия_COVID-19',
         'https://yi.wikipedia.org/wiki/קאוויד-19_פאנדעמיק',
         'https://tr.wikipedia.org/wiki/COVID-19_pandemisi',
         'https://uk.wikipedia.org/wiki/Пандемія_коронавірусної_хвороби_2019',
         'https://bg.wikipedia.org/wiki/Пандемия_от_коронавирус_(2019_–_2020)',
         'https://be.wikipedia.org/wiki/Пандэмія_COVID-19',
         'https://ro.wikipedia.org/wiki/Pandemia_de_coronaviroză_(COVID-19)',
         ]

'''No Threading'''
def get_data(links):
    data = ''
    for i in links:
        r = requests.get(i)
        data +=  str(r.content)
    return data

def count_sort(data):
    letter_count = {}
    for i in data:
        if i not in letter_count:
            letter_count[i] = 1
        else:
            letter_count[i] += 1
    sort_dict = sorted(letter_count.items(), key=lambda x: x[1], reverse=True)
    return sort_dict

def wr_file(sort_dict, file_name):
    p = Path(file_name)
    with p.open("w") as write_file:
        json.dump(sort_dict, write_file, ensure_ascii=False, indent=4)

'''...'''

'''With Threading'''
data = ''
lock = threading.Lock()
def t_load(urls):
    global data
    for url in urls:
        with lock:
            data += get_data(url)

def threading_load(urls,threads_count):
    for i in range(threads_count):
        start = i * len(urls) // threads_count
        stop = (i+1) * len(urls) // threads_count
        thread = threading.Thread(target=t_load, args=(urls[start:stop],))
        thread.start()
        print(thread.name)
        thread.join()
    print('Total letters quantity: {}'.format(len(data)))
    return data
'''...'''

if __name__ == '__main__':
    start_time = time.time()

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--output_file_name',
        type=str,
        help='Path to the outputfile in JSON format',
    )
    parser.add_argument(
        '--script_mode',
        type=str,
        choices=('single', 'threaded'),
        help='Script run mode',
    )
    parser.add_argument(
        '--threads_count',
        type=int,
        help='Count of threads',
    )

    args = parser.parse_args()
    if args.script_mode == 'threaded':
        wr_file(count_sort(
            threading_load(links, int(args.threads_count))),
            file_name=args.output_file_name)
    else:
        data = get_data(links)
        dict = count_sort(data)
        wr_file(dict, 'json')
    print('Total time: {}'.format(time.time() - start_time))


import numpy as np
import codecs
import pandas

from functions import *

exo_paragraph = open('exo_paragraph', 'w', encoding='utf-8')
exo_question = open('exo_question', 'w', encoding='utf-8')
exo_label = open('exo_label', 'w', encoding='utf-8')
exo_answers = open('exo_answers', 'w', encoding='utf-8')

paragraph_file = open('wiki_corpus', 'r', encoding='utf-8')
rule_file = open('wiki_info', 'r', encoding='utf-8')
# 정보가 들어있는 텍스트파일

exo_Questions = []
exo_Titles = []
exo_answer_info = []
exo_answer = []

exobrain_data1 = pandas.read_excel('exo1.xlsx')
temp = exobrain_data1['질문']
temp2 = exobrain_data1['위키피디아 제목']
temp3 = exobrain_data1['정답 근거1(문장)']
temp4 = exobrain_data1['정답']

for i in range(len(temp)):
    exo_Questions.append(str(temp[i]).replace('?', ''))
    exo_Titles.append(str(temp2[i]))

    info_str = str(temp3[i])

    exo_answer_info.append(preprocess(info_str))
    exo_answer.append(preprocess(str(temp4[i])))

exobrain_data1 = pandas.read_excel('exo3.xlsx')
temp = exobrain_data1['질문']
temp2 = exobrain_data1['위키피디아 제목']
temp3 = exobrain_data1['정답 근거1(문장)']
temp4 = exobrain_data1['정답']

for i in range(len(temp)):
    exo_Questions.append(str(temp[i]).replace('?', ''))
    exo_Titles.append(str(temp2[i]))

    info_str = str(temp3[i]).replace('.', '\n')

    exo_answer_info.append(preprocess(info_str))
    exo_answer.append(preprocess(str(temp4[i])))

print(len(exo_answer))
print(len(exo_answer_info))
print(len(exo_Questions))
print(len(exo_Titles))

exo_data_dictionary = np.array(exo_Titles, dtype='<U20')
exo_dictionary_index = exo_data_dictionary.argsort()
exo_data_dictionary.sort()

paragraphs = paragraph_file.read().split('\a')

count = 0

for i in range(len(exo_answer)):
    exo_answers.write(exo_answer[i])
    exo_answers.write('\n')
exo_answers.close()

for i, paragraph in enumerate(exo_answer_info):
    temp_TK = str(exo_answer_info[i]).split()

    TK = str(exo_answer[i]).split()

    if len(TK) > 0:
        start_word = TK[0]
        stop_word = TK[len(TK) - 1]

        TK = str(exo_answer_info[i]).split()
        start_index = -1
        stop_index = -1

        for j in range(len(TK)):
            a = TK[j].find(start_word)
            b = TK[j].find(stop_word)

            if a != -1 and start_index == -1:
                start_index = j
            if b != -1 and stop_index == -1:
                stop_index = j

        if start_index != -1 and stop_word != -1:
            if start_index <= stop_index:
                para = str(exo_answer_info[i]).replace(str(exo_answer[i]), '#' + str(i) + '@')
                TK = para.split('\n')
                for k in range(len(TK)):
                    exo_paragraph.write(TK[k].strip())
                    exo_paragraph.write('\n')
                exo_paragraph.write('@#!\n')

                exo_question.write(exo_Questions[i])
                exo_question.write('\a')

                exo_label.write(exo_answer[i])
                exo_label.write('\a')

                count += 1
            else:
                print('check!!!!!!!!!!')
                print(exo_answer_info[i])
                print(exo_answer[i])
                print(start_index, stop_index)
                print('---------------')

print(count)

exo_paragraph.close()
exo_question.close()
exo_label.close()


from multiprocessing import context
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from user.models import Profile ,studyLicenceNameAndPrice , Instructor
from customer.models import CustomerDetails, ServiceApplication
from .form import addInstructors,CustomerResistrationform
from customer.form import Resistrationform
from django.contrib.auth.forms import UserCreationForm
from user.models import Branch
from customer.models import *
from django.contrib.auth.forms import UserCreationForm

# Create your views here.
@login_required
def index(request):
    return render(request,'owner/index.html')

@login_required
def staffDetails(request):
    staff1 = Profile.objects.filter(is_staff= True)
    context = {
        'staff1' : staff1,
    }
    return render (request,'owner/staffDetails.html',context)

@login_required
def studentDetails(request):
    student1 = CustomerDetails.objects.all()
    context = {
        'student1':student1,

    }
    return render (request,'owner/studentDetails.html',context)

@login_required
def appliedServices(request):
    services1 = ServiceApplication.objects.all()
    context = {
        'services1':services1
    }
    return render (request,'owner/appliedServices.html',context)

@login_required
def instructor(request):
    instructor1 = Instructor.objects.all()
    context = {
        'instructor1':instructor1,
    }
    return render (request,'owner/instructor.html',context)

@login_required
def addInstructor(request):
    if request.method == 'POST':
        form = addInstructors(request.POST)
        if form.is_valid():
            form.save()
            return redirect (instructor)
    else:
        form = addInstructors()
    context = {
        "form" : form
    }
    return render(request,'owner/addInstructor.html',context)

@login_required
def addStaff(request):
    branchName = Branch.objects.all()
    if request.method == 'POST':
        branch1 = request.POST.get('stfBranch')
        branchid1 = Branch.objects.get(BranchName=branch1).id
        print("fg",branchid1)
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()

            user1 =User.objects.get(username=form.data['username']).id
            profilupdate = Profile.objects.get(user=user1)
            profilupdate.is_customer = False
            profilupdate.is_staff=True
            profilupdate.staffBranch_id=branchid1
            profilupdate.save()
            # return redirect (instructor)
    else:
        form = UserCreationForm()
    context = {
        "form" : form,
        "branchName":branchName,
    }
    return render(request,'owner/addStaff.html',context)

@login_required
def addStudent(request):
    form = CustomerResistrationform(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
    else:
        form = CustomerResistrationform()
    context = {
        'form':form ,
    }
    return render(request,'owner/addStudent.html',context)

@login_required
def totalRecipt(request):
    o1=request.user
    recipts1 = Payment.objects.all()
    totalRecipt = 0
    for each in recipts1:
        totalRecipt=totalRecipt+each.amount

    context = {
       'recipts1':recipts1,
       'totalRecipt':totalRecipt,
    }
    return render(request,'owner/TotalRecipts.html',context)

def userRegister(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid:
            form.save()
            return redirect('owner-userCreated') #this is temparary redirect change it later
    else:
        form = UserCreationForm()
    context ={
        'form': form
    }
    return render(request,'owner/createUser.html',context)

@login_required
def userCreated(request):
    return render(request,'owner/userAddMessage.html')


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minTime function below.
def minTime(machines, goal):
    max_days = math.ceil(goal/len(machines))*max(machines)
    min_days = math.ceil(goal/len(machines))*min(machines)
    up=max_days
    low=min_days
    tot=0
    for days in machines:
        tot+=min_days//days
    if tot >= goal:
        return min_days
    while up>low:
        mid_days= (up+low)//2
        tot=0
        for days in machines:
            tot+=mid_days//days
        if tot >=goal:
            up=mid_days
        elif tot < goal :
            low = mid_days+1
    return low

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nGoal = input().split()

    n = int(nGoal[0])

    goal = int(nGoal[1])

    machines = list(map(int, input().rstrip().split()))

    ans = minTime(machines, goal)

    fptr.write(str(ans) + '\n')

    fptr.close()


# -*- coding: utf-8 -*-
"""
Created on Sun Jul 19 12:03:56 2020

@author: 李运辰
"""
import requests
import time
from lxml import etree
import os
import json
#from flask_cors import CORS
from flask import Flask,render_template,request,Response,redirect,url_for
#内网ip
app = Flask(__name__)
###此处改为自己的ip地址，在index.html中两次也记得更改
ip="127.0.0.1"

headers = {
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
        }

###获取信息
def getlist(name_i):
    url_name = "https://baike.baidu.com/search/word?word="+str(name_i)
    s = requests.Session()
    response = s.get(url_name, headers=headers)
    text = response.text
    t_split = text.split('id="J-vars" data-lemmaid="')[1].split('" data-lemmatitle="')[0]
    #print(t_split)

    url="https://baike.baidu.com/item/"+str(name_i)+"/"+str(t_split)+"?fr=aladdin"
    res = requests.get(url,headers=headers)
    res.encoding = 'utf-8'
    text = res.text

    selector = etree.HTML(text)
    #属性
    key = []
    #值
    value = []

    dt =[]
    dd =[]
    basicInfo_left = selector.xpath('//*[@class="basicInfo-block basicInfo-left"]')[0]
    dt.append(basicInfo_left.xpath('.//dt'))
    dd.append(basicInfo_left.xpath('.//dd'))

    basicInfo_right = selector.xpath('//*[@class="basicInfo-block basicInfo-right"]')[0]
    dt.append(basicInfo_right.xpath('.//dt'))
    dd.append(basicInfo_right.xpath('.//dd'))

    for j in dt:
        for i in j:
            text = i.xpath('.//text()')
            if len(text)==1:
                text = text[0].replace(" ","").replace("\n","").replace("\xa0","")
            else:
                text = "-".join(text)
                text = text.replace(" ", "").replace("\n", "").replace("\xa0", "")
            key.append(text)
    for j in dd:
        for i in j:
            text = i.xpath('.//text()')
            if len(text) == 1:
                text = text[0].replace(" ", "").replace("\n", "").replace("\xa0", "")
            else:
                text = "-".join(text)
                text = text.replace(" ", "").replace("\n", "").replace("\xa0", "").replace("-", " ")
            value.append(text)

    links=[]

    for k in range(0,len(key)):
        if key[k]=="代表作品"  or key[k]=="主要成就":
            v = value[k].split(" ")
            dict = {'source': str(name_i), 'target': str(v[0]+v[1]), 'rela': str(key[k]), 'type': 'resolved'}
            links.append(dict)
        else:
            dict= {'source': str(name_i), 'target': str(value[k]), 'rela': str(key[k]), 'type': 'resolved'}
            links.append(dict)

    return links

############################flask路由
#进入首页
@app.route('/')
def index():
    return render_template('index.html')
#获取数据
@app.route('/getdata')
def getdata():
    name_i = request.args.get('name')
    # 采集数据
    links = getlist(name_i)

    #return Response(json.dumps(links), mimetype='application/json')
    return render_template('index.html', linkss=json.dumps(links))

if __name__ == "__main__":
    """初始化"""

    app.run(host=''+ip, port=5000,threaded=True)


import tensorflow as tf

width = 10
height = 10

ti = tf.range(width)
tj = tf.range(height)
mi = tf.meshgrid(ti, tj)
mj = tf.meshgrid(ti, tj)
mii=tf.tensordot(ti,tj)

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
tti, ttj, ttmi, ttmj,tmii = sess.run([ti, tj, mi, mj,mii])
print(tti)
print(ttj)
print(ttmi)
print(ttmj)
print(tmii)
print(tmii)


import pandas as pd
import numpy as np
from decisiontree import *
import copy
# from decisiontreetwo import DecisionTreePhi

NUMBER_OF_TREES = 25

def get_samples(index):
    return np.random.choice(index, size=index.size)

def get_features(data):
    # d = data.deepcopy()
    d = copy.deepcopy(data)
    features = d.columns
    size = int(np.floor(np.sqrt(features.size)))
    return np.random.choice(features, size=size, replace=False)

def forest_classifier(sample, trees):
    votes = [False for i in range(len(trees))]
    for i in range(len(trees)):
        votes[i] = trees[i].classify(sample)
    return (votes.count(True) > votes.count(False), votes.count(True), votes.count(False))

def evaluate(confusion_matrix):
    # translate our confusion matrix from booleans to tp/fp/tn/fn
    for i in confusion_matrix:
        if i.startswith('NC'):
            confusion_matrix[i] = 'tn' if confusion_matrix[i] == False else 'fp'
        else:
            confusion_matrix[i] = 'tp' if confusion_matrix[i] == True else 'fn'

    # after classifying everything, we need to determine:
    # accuracy, sensitivity, specificity, precision, miss rate,
    # false discovery rate, and false omission rate
    # we also need to plot a confusion matrix

    classified_tp_sum = 0
    classified_tn_sum = 0
    classified_fp_sum = 0
    classified_fn_sum = 0
    for i in confusion_matrix:
        if confusion_matrix[i] == 'tp':
            classified_tp_sum += 1
        elif confusion_matrix[i] == 'tn':
            classified_tn_sum += 1
        elif confusion_matrix[i] == 'fp':
            classified_fp_sum += 1
        else:
            classified_fn_sum += 1

    accuracy = (classified_tp_sum + classified_tn_sum) / len(confusion_matrix)
    denom_sens = 1 if (classified_tp_sum + classified_fn_sum == 0) else (classified_tp_sum + classified_fn_sum)
    sensitivity = classified_tp_sum / denom_sens
    denom_spec = 1 if (classified_tn_sum + classified_fp_sum == 0) else (classified_tn_sum + classified_fp_sum)
    specificity = classified_tn_sum / denom_spec
    denom_prec = 1 if (classified_tp_sum + classified_fp_sum == 0) else (classified_tp_sum + classified_fp_sum)
    precision = classified_tp_sum / denom_prec
    miss_rate = 1 - sensitivity
    fdr = 1 - precision
    denom = 1 if (classified_fn_sum + classified_tn_sum == 0) else (classified_fn_sum + classified_tn_sum)
    false_omission_rate = classified_fn_sum / denom
    return accuracy, sensitivity, specificity, precision, miss_rate, fdr, false_omission_rate, \
        classified_tp_sum, classified_tn_sum, classified_fp_sum, classified_fn_sum

def main():
    data = pd.read_csv('./mutations.csv', index_col='Unnamed: 0')
    trees = []
    out_of_bags = []

    for i in range(NUMBER_OF_TREES):
        # grab sqrt(n) columns, 230 columns with replacement
        samples = get_samples(data.index)
        mutations = get_features(data)
        data_set = data.loc[samples, mutations]
        out_of_bag = data.drop(index=samples, columns=mutations)
        decision_tree = DecisionTreePhi(data_set, 2)
        trees.append(decision_tree)
        out_of_bags.append(out_of_bag)

        print('iteration: ', i+1, '====================')
        print('Out of Bag size: ', len(out_of_bag))
        print('Out of Bag: ', out_of_bag)
        print('Tree: ')
        print(decision_tree)

    print('================================')
    print('Avergage Out of Bag size: ', np.average(len(out_of_bags)))

    print('================================')
    print('Classifying Samples')

    samples_list = ['C1', 'C10', 'C50', 'NC5', 'NC15']
    for i in samples_list:
        print()
        sample_data = data.loc[i,:]
        classification, votes_positive, votes_negative = forest_classifier(sample_data, trees)
        print('Classification Results for', i)
        print('Classification:', classification, '\tVotes Positive:', votes_positive, \
              '\tVotes Negative:', votes_negative)

    print()
    print('================================')
    print('Average size of the Out of Bag set')
    out_of_bag_sum = 0
    for i in out_of_bags:
        out_of_bag_sum += len(i)

    out_of_bag_sum /= len(out_of_bags)
    print(out_of_bag_sum)

    print()
    print('================================')
    print('Classifying Out of Bag Set-0')
    print()
    confusion_matrix = {}

    for i in out_of_bags[0].index:
        sample_data = data.loc[i,:]
        classification, votes_positive, votes_negative = forest_classifier(sample_data, trees)
        # if classification:
        #     print('TRUE!!!!!!!!!!!!!!!!!!!')
        confusion_matrix[i] = classification

    accuracy, sensitivity, specificity, precision, miss_rate, fdr, false_omission_rate, tp, tn, fp, fn\
        = evaluate(confusion_matrix)
    print('Accuracy:', accuracy)
    print('Sensitivity:', sensitivity)
    print('Specificity:', specificity)
    print('Precision:', precision)
    print('Miss Rate:', miss_rate)
    print('FDR:', fdr)
    print('FOR:', false_omission_rate)

    print('tp:', tp)
    print('tn:', tn)
    print('fp:', fp)
    print('fn:', fn)

if __name__ == '__main__':
    main()


import os, sys, operator
import tensorflow as tf
import numpy as np

sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from reader import batch_patcher as patcher
from network.DenseNet.DenseNet import *
from network.VGG.VGG19 import *

class Trainer(object):
    def __init__(self, model):
        self.model = model
        self.train_loss_op = None
        self.train_accuracy_op = None
        self.train_op = None
        self.train_xentropy_op = None
        self.train_prob_op = None
        self.test_loss_op = None
        self.test_accuracy_op = None
        self.test_xentropy_op = None
        self.test_prob_op = None

# Recommend T_k = 15 in coteaching paper
def coteaching(gpu_id, input_reader, model_type, total_epochs, batch_size, lr_boundaries, lr_values, optimizer_type, noise_rate, noise_type, T_k=15, log_dir="log"):

    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    # log list
    text_log = []
    text_log.append("epcoh, learning rate, training loss (network1), training error (network1), training loss (network2), training error (network2), "
                        "test loss (network1), test error (network1), test loss (network2), test error (network2)\n")

    num_train_images = input_reader.num_train_images
    num_test_images = input_reader.num_val_images
    num_label = input_reader.num_classes
    image_shape = [input_reader.height, input_reader.width, input_reader.depth]

    train_batch_patcher = patcher.BatchPatcher(num_train_images, batch_size, num_label)
    test_batch_patcher = patcher.BatchPatcher(num_test_images, batch_size, num_label)

    config = tf.ConfigProto(allow_soft_placement = True)
    config.gpu_options.visible_device_list = str(gpu_id)
    config.gpu_options.allow_growth = True
    graph = tf.Graph()

    with graph.as_default():
        with tf.device('/gpu:' + str(gpu_id)):
            with tf.Session(config = config) as sess:
                train_ids, train_images, train_labels = input_reader.data_read(batch_size, train = True)
                test_ids, test_images, test_labels = input_reader.data_read(batch_size, train = False)

                if model_type == "DenseNet-25-12":
                    model1 = DenseNet(25, 12, image_shape, num_label, scope='network1')
                    model2 = DenseNet(25, 12, image_shape, num_label, scope='network2')
                elif model_type == "DenseNet-40-12":
                    model1 = DenseNet(40, 12, image_shape, num_label, scope='network1')
                    model2 = DenseNet(40, 12, image_shape, num_label, scope='network2')
                elif model_type == "DenseNet-10-12":
                    model1 = DenseNet(10, 12, image_shape, num_label, scope='network1')
                    model2 = DenseNet(10, 12, image_shape, num_label, scope='network2')
                elif model_type == "VGG-19":
                    model1 = VGG19(image_shape, num_label, scope='network1')
                    model2 = VGG19(image_shape, num_label, scope='network2')

                # register training operations on Trainer class
                trainer1 = Trainer(model1)
                trainer1.train_loss_op, trainer1.train_accuracy_op, trainer1.train_op, trainer1.train_xentropy_op, trainer1.train_prob_op = model1.build_train_op(lr_boundaries, lr_values, optimizer_type)
                trainer1.test_loss_op, trainer1.test_accuracy_op, trainer1.test_xentropy_op, trainer1.test_prob_op = model1.build_test_op()

                trainer2 = Trainer(model2)
                trainer2.train_loss_op, trainer2.train_accuracy_op, trainer2.train_op, trainer2.train_xentropy_op, trainer2.train_prob_op = model2.build_train_op(lr_boundaries, lr_values, optimizer_type)
                trainer2.test_loss_op, trainer2.test_accuracy_op, trainer2.test_xentropy_op, trainer2.test_prob_op = model2.build_test_op()

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord = coord)

                # load data set in main memory
                train_batch_patcher.bulk_load_in_memory(sess, train_ids, train_images, train_labels)
                test_batch_patcher.bulk_load_in_memory(sess, test_ids, test_images, test_labels)

                # give noise on data set
                train_batch_patcher.set_noise(noise_rate, noise_type)

                ######################## main methodology for training #######################
                sess.run(tf.global_variables_initializer())

                # for model 1
                for epoch in range(total_epochs):
                    ratio = 1.0 - noise_rate * np.fmin(1.0, float(float(epoch) + 1.0) / float(T_k))
                    # print("Clean sample ratio: ", ratio)
                    for i in range(train_batch_patcher.num_iters_per_epoch):
                        ids, images, labels = train_batch_patcher.get_next_random_mini_batch(batch_size)

                        # losses of model 1
                        xentropy_array1 = sess.run(trainer1.train_xentropy_op, feed_dict={trainer1.model.train_image_placeholder: images, trainer1.model.train_label_placeholder: labels})
                        # losses of model 2
                        xentropy_array2 = sess.run(trainer2.train_xentropy_op, feed_dict={trainer2.model.train_image_placeholder: images, trainer2.model.train_label_placeholder: labels})

                        # choose R(T)% small-loss instances
                        ids_for_2, images_for_2, labels_for_2 = get_lower_loss_instances(batch_size, ids, images, labels, xentropy_array1, ratio)
                        ids_for_1, images_for_1, labels_for_1 = get_lower_loss_instances(batch_size, ids, images, labels, xentropy_array2, ratio)

                        # model update
                        _, _ = sess.run([trainer1.train_op, trainer2.train_op], feed_dict={trainer1.model.train_image_placeholder: images_for_1, trainer1.model.train_label_placeholder: labels_for_1, trainer2.model.train_image_placeholder: images_for_2, trainer2.model.train_label_placeholder: labels_for_2})

                    # inference and test error for logs
                    # test
                    avg_val_loss_1 = 0.0
                    avg_val_acc_1 = 0.0
                    avg_val_loss_2 = 0.0
                    avg_val_acc_2 = 0.0
                    for i in range(test_batch_patcher.num_iters_per_epoch):
                        ids, images, labels = test_batch_patcher.get_init_mini_batch(i)
                        val_loss_1, val_acc_1 = sess.run([trainer1.test_loss_op, trainer1.test_accuracy_op], feed_dict={trainer1.model.test_image_placeholder: images, trainer1.model.test_label_placeholder: labels})
                        val_loss_2, val_acc_2 = sess.run([trainer2.test_loss_op, trainer2.test_accuracy_op], feed_dict={trainer2.model.test_image_placeholder: images, trainer2.model.test_label_placeholder: labels})

                        avg_val_loss_1 += val_loss_1
                        avg_val_acc_1 += val_acc_1
                        avg_val_loss_2 += val_loss_2
                        avg_val_acc_2 += val_acc_2

                    avg_val_loss_1 /= test_batch_patcher.num_iters_per_epoch
                    avg_val_acc_1 /= test_batch_patcher.num_iters_per_epoch
                    avg_val_loss_2 /= test_batch_patcher.num_iters_per_epoch
                    avg_val_acc_2 /= test_batch_patcher.num_iters_per_epoch

                    # Inference
                    avg_train_loss_1 = 0.0
                    avg_train_acc_1 = 0.0
                    avg_train_loss_2 = 0.0
                    avg_train_acc_2 = 0.0

                    for i in range(train_batch_patcher.num_iters_per_epoch):
                        ids, images, labels = train_batch_patcher.get_init_mini_batch(i)
                        imp_loss_1, imp_acc_1 = sess.run([trainer1.train_loss_op, trainer1.train_accuracy_op], feed_dict={trainer1.model.train_image_placeholder: images, trainer1.model.train_label_placeholder: labels})
                        imp_loss_2, imp_acc_2 = sess.run([trainer2.train_loss_op, trainer2.train_accuracy_op], feed_dict={trainer2.model.train_image_placeholder: images, trainer2.model.train_label_placeholder: labels})

                        avg_train_loss_1 += imp_loss_1
                        avg_train_acc_1 += imp_acc_1
                        avg_train_loss_2 += imp_loss_2
                        avg_train_acc_2 += imp_acc_2

                    avg_train_loss_1 /= train_batch_patcher.num_iters_per_epoch
                    avg_train_acc_1 /= train_batch_patcher.num_iters_per_epoch
                    avg_train_loss_2 /= train_batch_patcher.num_iters_per_epoch
                    avg_train_acc_2 /= train_batch_patcher.num_iters_per_epoch

                    cur_lr = sess.run(trainer1.model.learning_rate)
                    print((epoch + 1), ", ", cur_lr, ", ", avg_train_loss_1, ", ", avg_train_acc_1,
                          ", ", avg_train_loss_2, ", ", avg_train_acc_2,
                          ", ", avg_val_loss_1, ", ", avg_val_acc_1,
                          ", ", avg_val_loss_2, ", ", avg_val_acc_2)
                    text_log.append(str(epoch + 1) + ", " + str(cur_lr) + ", " + str(avg_train_loss_1) + ", " + str(1.0 - avg_train_acc_1)
                                    + ", " + str(avg_train_loss_2) + ", " + str(1.0 - avg_train_acc_2)
                                    + ", " + str(avg_val_loss_1) + ", " + str(1.0 - avg_val_acc_1)
                                    + ", " + str(avg_val_loss_2) + ", " + str(1.0 - avg_val_acc_2))
                ##############################################################################

                coord.request_stop()
                coord.join(threads)
                sess.close()

    f = open(log_dir + "/log.csv", "w")
    for text in text_log:
        f.write(text + "\n")
    f.close()

def get_lower_loss_instances(batch_size, ids, images, labels, xentropy_array, ratio):

    num_instance = int(np.ceil(float(batch_size) * ratio))

    loss_map = {}
    image_map = {}
    label_map = {}

    for i in range(len(ids)):
        loss_map[ids[i]] = xentropy_array[i]
        image_map[ids[i]] = images[i]
        label_map[ids[i]] = labels[i]

    loss_map = dict(sorted(loss_map.items(), key=operator.itemgetter(1), reverse=False))

    new_ids = []
    new_images = []
    new_labels = []

    index = 0
    for key in loss_map.keys():
        if index >= num_instance:
            break

        new_ids.append(key)
        new_images.append(image_map[key])
        new_labels.append(label_map[key])
        index += 1

    return new_ids, new_images, new_labels