import ImageConfig as ic
import itertools
import grain_class as gc
import numpy as np
import matplotlib.pyplot as plt

statsRatiosToCalculateList = ['BorderNeighbour',
                              'Dispersion',
                              'OnePointProbability',
                              'Linealpath']

class Statistics:
    def __init__(self, grains, scale=1):
        self.imageArea = ic.height * ic.width
        self.borderNeighboursCountRatio = {}
        self.dispersionPhases = {}
        self.onePointProbability = {}
        self.linealPath = {}
        self.calculatedRatios = {}
        self.grains = grains
        self.scale = scale

    def __blr(self):
        colors = list(ic.colors_map.keys())
        colorsDict = {v: k for k, v in ic.colors_map.items()}

        for pair in itertools.combinations(colors, 2):
            combination = pair[0] + pair[1]
            self.borderNeighboursCountRatio[combination] = 0

        for i in range(ic.height):
            for j in range(ic.width - 1):
                color = [ic.image[i, j, 2], ic.image[i, j, 1], ic.image[i, j, 0]]
                nbcolorright = [ic.image[i, j + 1, 2], ic.image[i, j + 1, 1], ic.image[i, j + 1, 0]]
                if tuple(color) in colorsDict.keys() and tuple(nbcolorright) in colorsDict.keys():
                    phasename = colorsDict[tuple(color)]
                    nbrightphasename = colorsDict[tuple(nbcolorright)]
                    if phasename + nbrightphasename in self.borderNeighboursCountRatio.keys():
                        self.borderNeighboursCountRatio[phasename + nbrightphasename] += 1
                    if nbrightphasename + phasename in self.borderNeighboursCountRatio.keys():
                        self.borderNeighboursCountRatio[nbrightphasename + phasename] += 1

        for i in range(ic.height - 1):
            for j in range(ic.width):
                color = [ic.image[i, j, 2], ic.image[i, j, 1], ic.image[i, j, 0]]
                nbcolorunder = [ic.image[i + 1, j, 2], ic.image[i + 1, j, 1], ic.image[i + 1, j, 0]]
                if tuple(color) in colorsDict.keys() and tuple(nbcolorunder) in colorsDict.keys():
                    phasename = colorsDict[tuple(color)]
                    nbcolorunderphasename = colorsDict[tuple(nbcolorunder)]
                    if phasename + nbcolorunderphasename in self.borderNeighboursCountRatio.keys():
                        self.borderNeighboursCountRatio[phasename + nbcolorunderphasename] += 1
                    if nbcolorunderphasename + phasename in self.borderNeighboursCountRatio.keys():
                        self.borderNeighboursCountRatio[nbcolorunderphasename + phasename] += 1
        allborderpixels = sum(list(self.borderNeighboursCountRatio.values()))
        if allborderpixels != 0:
            for key, value in self.borderNeighboursCountRatio.items():
                self.borderNeighboursCountRatio[key] = value / allborderpixels

    def __dispersion(self):
        area = self.imageArea * (self.scale ** 2)
        for phases in ic.colors_map.keys():
            self.dispersionPhases[phases] = 0
        for gc.Grain in self.grains:
            self.dispersionPhases[gc.Grain.phase] += 1
        for key, value in self.dispersionPhases.items():
            self.dispersionPhases[key] = (value / area) * 100

    def __onePointProb(self):
        colorsDict = {v: k for k, v in ic.colors_map.items()}
        for phase in ic.colors_map.keys():
            self.onePointProbability[phase] = 0
        for i in range(ic.height):
            for j in range(ic.width):
                color = (ic.image[i, j, 2], ic.image[i, j, 1], ic.image[i, j, 0])
                if color in colorsDict.keys():
                    phasename = colorsDict[color]
                    self.onePointProbability[phasename] += 1
        for key, value in self.onePointProbability.items():
            self.onePointProbability[key] = value / self.imageArea

    def __linealpath(self):
        for phase in ic.colors_map.keys():
            self.linealPath[phase] = {'angleZero': np.zeros((ic.width,), dtype=float),
                                      'angle90': np.zeros((ic.height,), dtype=float),
                                      'angle45': np.zeros((ic.height,), dtype=float)}
        colorsDict = {v: k for k, v in ic.colors_map.items()}
        rng = np.random.default_rng()
        xCoordinates = rng.choice(ic.width, 50)
        yCoordinates = rng.choice(ic.height, 50)
        for point in range(50):
            x = xCoordinates[point]
            y = yCoordinates[point]
            xyColor = (ic.image[y, x, 2], ic.image[y, x, 1], ic.image[y, x, 0])
            for pointAngleZero in range(ic.width - 1):
                pointAngleZero = pointAngleZero + 1
                pointToCheck = x + pointAngleZero
                if pointToCheck >= ic.width:
                    pointToCheck = pointToCheck - ic.width
                pointToCheckColor = (ic.image[y, pointToCheck, 2], ic.image[y, pointToCheck, 1],
                                     ic.image[y, pointToCheck, 0])
                if xyColor[0] == pointToCheckColor[0] and xyColor[1] == pointToCheckColor[1] and xyColor[2] == pointToCheckColor[2]:
                    self.linealPath[colorsDict[xyColor]]['angleZero'][pointAngleZero] += 0.02
                else:
                    break
            for pointAngle90 in range(ic.height - 1):
                pointAngle90 = pointAngle90 + 1
                pointToCheck = y + pointAngle90
                if pointToCheck >= ic.height:
                    pointToCheck = pointToCheck - ic.height
                pointToCheckColor = (ic.image[pointToCheck, x, 2], ic.image[pointToCheck, x, 1],
                                     ic.image[pointToCheck, x, 0])
                if xyColor[0] == pointToCheckColor[0] and xyColor[1] == pointToCheckColor[1] and xyColor[2] == \
                        pointToCheckColor[2]:
                    self.linealPath[colorsDict[xyColor]]['angle90'][pointAngle90] += 0.02
                else:
                    break
            for pointAngle45 in range(ic.height - 1):
                pointAngle45 = pointAngle45 + 1
                pointToCheckY = y - pointAngle45
                pointToCheckX = x + pointAngle45
                if pointToCheckX >= ic.width:
                    pointToCheckX = pointToCheckX - ic.width
                if pointToCheckY < 0:
                    pointToCheckY = pointToCheckY + ic.height - 1
                pointToCheckColor = (
                    ic.image[pointToCheckY, pointToCheckX, 2], ic.image[pointToCheckY, pointToCheckX, 1],
                    ic.image[pointToCheckY, pointToCheckX, 0])
                if xyColor[0] == pointToCheckColor[0] and xyColor[1] == pointToCheckColor[1] and xyColor[2] == \
                        pointToCheckColor[2]:
                    self.linealPath[colorsDict[xyColor]]['angle45'][pointAngle45] += 0.02
                else:
                    break
        for phase in ic.colors_map.keys():
            self.linealPath[phase]['angleZero'] = np.delete(self.linealPath[phase]['angleZero'], 0)
            self.linealPath[phase]['angle45'] = np.delete(self.linealPath[phase]['angle45'], 0)
            self.linealPath[phase]['angle90'] = np.delete(self.linealPath[phase]['angle90'], 0)

        angles = ['angleZero', 'angle45', 'angle90']
        x = range(1, ic.width)
        y = range(1, ic.height)
        for phase in ic.colors_map.keys():
            for angle in angles:
                if angle == 'angleZero':
                    plt.plot(x, self.linealPath[phase]['angleZero'])
                else:
                    plt.plot(y, self.linealPath[phase][angle])
                plt.xlabel('distance')
                plt.ylabel('probability')
                plt.title(phase + " " + angle)
                plt.show()

    def calculateRatios(self):
        if 'BorderNeighbour' in statsRatiosToCalculateList:
            self.__blr()
            self.calculatedRatios['BorderNeighbour'] = self.borderNeighboursCountRatio
        if 'Dispersion' in statsRatiosToCalculateList:
            self.__dispersion()
            self.calculatedRatios['Dispersion'] = self.dispersionPhases
        if 'OnePointProbability' in statsRatiosToCalculateList:
            self.__onePointProb()
            self.calculatedRatios['OnePointProbability'] = self.onePointProbability
        if 'Linealpath' in statsRatiosToCalculateList:
            self.__linealpath()
            # self.calculatedRatios['Lineal-path'] = self.linealPath
        return self.calculatedRatios


import random
print('''

  _   _                 _                  _____                    _
 | \ | |               | |                / ____|                  (_)
 |  \| |_   _ _ __ ___ | |__   ___ _ __  | |  __ _   _  ___ ___ ___ _ _ __   __ _
 | . ` | | | | '_ ` _ \| '_ \ / _ | '__| | | |_ | | | |/ _ / __/ __| | '_ \ / _` |
 | |\  | |_| | | | | | | |_) |  __| |    | |__| | |_| |  __\__ \__ | | | | | (_| |
 |_| \_|\__,_|_| |_| |_|_.__/ \___|_|     \_____|\__,_|\___|___|___|_|_| |_|\__, |
                                                                             __/ |
                                                                            |___/
''')
print("Welcome to the number Guessing game!")
print("Guess the number ranging from 1 to 100")

random_number = random.randint(1, 100)

def guessing_game(chances):
    guess = int(input("Make a guess: "))
    if guess > random_number:
        print("Too High.")
        print("Guess again!")
        easy_game(chances - 1)
    elif guess < random_number:
        print("Too Low.")
        print("Guess again!")
        easy_game(chances - 1)
    elif guess == random_number:
        print(f"You got it! The number was {random_number}")

def easy_game(chances):
    print(f"You have {chances} attempts to guess the number")

    if chances == 0:
        print("You've ran out of guesses. YOU LOOSE!")

    else:
        guessing_game(chances)

def hard_game(chances):
    print(f"You have {chances} attempts to guess the number")

    if chances == 0:
        print("You've ran out of guesses. YOU LOOSE!")

    else:
        guessing_game(chances)

if input("Choose a difficulty. Type 'easy' or 'hard': ").lower() == 'easy':
    easy = 10
    easy_game(easy)
else:
    hard = 5
    hard_game(hard)


from ICML_EnbPI_Class import prediction_interval
import ICML_EnbPI_helpers as alg_slide_k
from ICML_EnbPI_helpers import plot_average_new, grouped_box_new, one_dimen_transform, network_gen_data, network_transform
from keras.layers import LSTM
from scipy.stats import beta
from matplotlib.lines import Line2D  # For legend handles
import statsmodels as sm
import calendar
import warnings
import matplotlib.pyplot as plt
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
from sklearn.ensemble import RandomForestRegressor
import itertools
import importlib
import time
import pandas as pd
import numpy as np
import os
import sys
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.optimizers import Adam
warnings.filterwarnings("ignore")

'''This File contains code for reproducing all figures except Figure 4, which is generated by another code (ICML_ECAD_test.py)'''

'''Read Data'''

def read_data(i, filename, max_data_size):
    if i == 0:
        '''
            All datasets are Multivariate time-series. They have respective Github for more details as well.
            1. Greenhouse Gas Observing Network Data Set
            Time from 5.10-7.31, 2010, with 4 samples everyday, 6 hours apart between data poits.
            Goal is to "use inverse methods to determine the optimal values of the weights in the weighted sum of 15 tracers that best matches the synthetic observations"
            In other words, find weights so that first 15 tracers will be as close to the last as possible.
            Note, data at many other grid cells are available. Others are in Downloads/ðŸŒŸAISTATS Data/Greenhouse Data
            https://archive.ics.uci.edu/ml/datasets/Greenhouse+Gas+Observing+Network
        '''
        data = pd.read_csv(filename, header=None, sep=' ').T
        # data.shape  # 327, 16Note, rows are 16 time series (first 15 from tracers, last from synthetic).
    elif i == 1:
        '''
            2. Appliances energy prediction Data Set
            The data set is at 10 min for about 4.5 months.
            The column named 'Appliances' is the response. Other columns are predictors
            https://archive.ics.uci.edu/ml/datasets/Appliances+energy+prediction
        '''
        data = pd.read_csv(filename, delimiter=',')
        # data.shape  # (19736, 29)
        data.drop('date', inplace=True, axis=1)
        data.loc[:, data.columns != 'Appliances']
    elif i == 2:
        '''
            3. Beijing Multi-Site Air-Quality Data Data Set
            This data set includes hourly air pollutants data from 12 nationally-controlled air-quality monitoring sites.
            Time period from 3.1, 2013 to 2.28, 2017.
            PM2.5 or PM10 would be the response.
            https://archive.ics.uci.edu/ml/datasets/Beijing+Multi-Site+Air-Quality+Data
        '''
        data = pd.read_csv(filename)
        # data.shape  # 35064, 18
        # data.columns
        data.drop(columns=['No', 'year', 'month', 'day', 'hour',
                           'wd', 'station'], inplace=True, axis=1)
        data.dropna(inplace=True)
        # data.shape  # 32907, 11
        # data.head(5)
    else:
        """
            4 (Alternative). NREL Solar data at Atlanta Downtown in 2018. 24 observations per day and separately equally by 1H @ half an hour mark everytime
            Data descriptions see Solar Writeup
            Data download:
            (With API) https://nsrdb.nrel.gov/data-sets/api-instructions.html
            (Manual) https://maps.nrel.gov/nsrdb-viewer
        """
        data = pd.read_csv(filename, skiprows=2)
        # data.shape  # 8760, 14
        data.drop(columns=data.columns[0:5], inplace=True)
        data.drop(columns='Unnamed: 13', inplace=True)
        # data.shape  # 8760, 8
        # data.head(5)
    # pick maximum of X data points (for speed)
    data = data.iloc[:min(max_data_size, data.shape[0]), :]
    print(data.shape)
    return data

def read_CA_data(filename):
    data = pd.read_csv(filename)
    # data.shape  # 8760, 14
    data.drop(columns=data.columns[0:6], inplace=True)
    return data

def read_wind_data():
    ''' Note, just use the 8760 hourly observation in 2019
    Github repo is here: https://github.com/Duvey314/austin-green-energy-predictor'''
    data_wind_19 = pd.read_csv('Wind_Hackberry_Generation_2019_2020.csv')
    data_wind_19 = data_wind_19.iloc[:24*365, :]
    return data_wind_19

'''Appendix: Data Visualization'''
dataSolar_Atl = read_data(3, 'Solar_Atl_data.csv', max_data_size)
dataPalo_Atlo = read_CA_data('Palo_Alto_data.csv')
dataWind_Austin = read_wind_data()
# Visualization
font = {'size': 16}
plt.rc('font', **font)
fig, ax = plt.subplots(3, 1, figsize=(12, 6), sharex=True)
length = 2000
period = range(length)
ax[0].plot(period, dataSolar_Atl['DHI'][:length], color='blue')
ax[0].set_title('Atlanta Solar Radiation')
ax[0].set_ylabel('DHI')
ax[1].plot(period, dataPalo_Alto['DHI'][:length], color='blue')
ax[1].set_title('Palo Alto Solar Radiation')
ax[1].set_ylabel('DHI')
ax[2].plot(period, dataWind_Austin['MWH'][:length], color='blue')
ax[2].set_title('Austin Wind Energy')
ax[2].set_ylabel('MWH')
ax[2].set_xlabel('Time Index')
fig.savefig('Raw_data_plot.pdf', dpi=300, bbox_inches='tight',
            pad_inches=0)

'''   Real-Data Experiments
    Stride s=1 for All four datasets'''
# Read Data:
data0 = read_data(0, 'green_house_data.csv', max_data_size)
data1 = read_data(1, 'appliances_data.csv', max_data_size)
data2 = read_data(
    2, 'Beijing_air_Tiantan_data.csv', max_data_size)
CA_cities = ['Fremont', 'Milpitas', 'Mountain_View', 'North_San_Jose',
             'Palo_Alto', 'Redwood_City', 'San_Mateo', 'Santa_Clara',
             'Sunnyvale']
for city in CA_cities:
    globals()['data%s' % city] = read_CA_data(f'{city}_data.csv')

# Initialize Parameters
max_data_size = 10000

def big_transform(CA_cities, current_city, one_dim, train_size):
    # Next, merge these data (so concatenate X_t and Y_t for one_d or not)
    # Return [X_train, X_test, Y_train, Y_test] from data_x and data_y
    # Data_x is either multivariate (direct concatenation)
    # or univariate (transform each series and THEN concatenate the transformed series)
    big_X_train = []
    big_X_predict = []
    for city in CA_cities:
        data = eval(f'data{city}')  # Pandas DataFrame
        data_x = data.loc[:, data.columns != 'DHI']
        data_y = data['DHI']
        data_x_numpy = data_x.to_numpy()  # Convert to numpy
        data_y_numpy = data_y.to_numpy()  # Convert to numpy
        X_train = data_x_numpy[:train_size, :]
        X_predict = data_x_numpy[train_size:, :]
        Y_train_del = data_y_numpy[:train_size]
        Y_predict_del = data_y_numpy[train_size:]
        if city == current_city:
            Y_train = Y_train_del
            Y_predict = Y_predict_del
        if one_dim:
            X_train, X_predict, Y_train_del, Y_predict_del = one_dimen_transform(
                Y_train_del, Y_predict_del, d=20)
            big_X_train.append(X_train)
            big_X_predict.append(X_predict)
            if city == current_city:
                Y_train = Y_train_del
        else:
            big_X_train.append(X_train)
            big_X_predict.append(X_predict)
    X_train = np.hstack(big_X_train)
    X_predict = np.hstack(big_X_predict)
    return([X_train, X_predict, Y_train, Y_predict])

'''Part 1, for 1-\alpha plot and boxplots, s=1 '''

stride = 1
miss_test_idx = []
alpha = 0.1
tot_trial = 10  # For CP methods that randomizes
np.random.seed(98765)
B = 30  # number of bootstrap samples
Data_name = ['green_house', 'appliances', 'Beijing_air',
             'Solar_Atl', 'Palo_Alto', 'Wind_Austin']
Data_name_network = ['Palo_Alto']
response_ls = {'green_house': 15, 'appliances': 'Appliances',
               'Beijing_air': 'PM2.5', 'Solar_Atl': 'DHI', 'Wind_Austin': 'MWH'}
response_ls_network = {'Palo_Alto': 'DHI'}
data_ind = {}
for i in range(len(Data_name)):
    key = Data_name[i]
    if i <= 2:
        data_ind[key] = i
    else:
        data_ind[key] = key
data_ind_network = {'Palo_Alto': 'Palo_Alto'}
min_alpha = 0.0001
max_alpha = 10
ridge_cv = RidgeCV(alphas=np.linspace(min_alpha, max_alpha, 10))
random_forest = RandomForestRegressor(n_estimators=10, criterion='mse',
                                      bootstrap=False, max_depth=2, n_jobs=-1)

def keras_mod():
    model = Sequential(name='NeuralNet')
    model.add(Dense(100, activation='relu'))
    model.add(Dense(100, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(1, activation='relu'))
    # Compile model
    opt = Adam(0.0005)
    model.compile(loss='mean_squared_error', optimizer=opt)
    return model

def keras_rnn():
    model = Sequential(name='RNN')
    # For fast cuDNN implementation, activation = 'relu' does not work
    model.add(LSTM(100, activation='tanh', return_sequences=True))
    model.add(LSTM(100, activation='tanh'))
    model.add(Dense(1, activation='relu'))
    # Compile model
    opt = Adam(0.0005)
    model.compile(loss='mean_squared_error', optimizer=opt)
    return model

''' Get average coverage and width for different train_size
    Use Ridge, RF, NN, and RNN
    Note, ARIMA is NOT ran here'''
tot_trial = 10
rnn = False
energy_data = True  # True for Palo Alto only, as it is a network. So need to run the procedure TWICE
if energy_data:
    Data_name = Data_name
    response_ls = response_ls
    data_ind = data_ind
else:
    Data_name = Data_name_network
    response_ls = response_ls_network
    data_ind = data_ind_network
for one_dim in [True, False]:
    methods = ['Ensemble', 'ICP', 'Weighted_ICP']
    # Run Ridge, Lasso, RF, and NN
    for data_name in Data_name:
        data = eval(f'data{data_ind[data_name]}')  # Pandas DataFrame
        data_x = data.loc[:, data.columns != response_ls[data_name]]
        data_y = data[response_ls[data_name]]
        data_x_numpy = data_x.to_numpy()  # Convert to numpy
        data_y_numpy = data_y.to_numpy()  # Convert to numpy
        total_data_points = data_x_numpy.shape[0]
        Train_size = np.linspace(0.1 * total_data_points,
                                 0.3 * total_data_points, 10).astype(int)
        Train_size = [Train_size[0], Train_size[4], Train_size[8]]
        results = pd.DataFrame(columns=['itrial', 'dataname', 'muh_fun',
                                        'method', 'train_size', 'coverage', 'width'])
        for itrial in range(tot_trial):
            np.random.seed(98765 + itrial)
            for train_size in Train_size:
                nnet = keras_mod()  # Note, this is necessary because a model may "remember the past"
                rnnet = keras_rnn()
                print(f'At trial # {itrial} and train_size={train_size}')
                print(f'For {data_name}')
                if energy_data:
                    X_train, X_predict, Y_train, Y_predict = big_transform(
                        Data_name, data_name, one_dim, train_size)
                else:
                    X_train = data_x_numpy[:train_size, :]
                    X_predict = data_x_numpy[train_size:, :]
                    Y_train = data_y_numpy[:train_size]
                    Y_predict = data_y_numpy[train_size:]
                    if one_dim:
                        X_train, X_predict, Y_train, Y_predict = one_dimen_transform(
                            Y_train, Y_predict, d=20)
                ridge_results = prediction_interval(
                    ridge_cv, X_train, X_predict, Y_train, Y_predict)
                rf_results = prediction_interval(
                    random_forest,  X_train, X_predict, Y_train, Y_predict)
                nn_results = prediction_interval(
                    nnet,  X_train, X_predict, Y_train, Y_predict)
                if rnn:
                    T, k = X_train.shape
                    T1 = X_predict.shape[0]
                    X_train = X_train.reshape((T, 1, k))
                    X_predict = X_predict.reshape((T1, 1, k))
                    rnn_results = prediction_interval(
                        rnnet, X_train, X_predict, Y_train, Y_predict)
                # For CP Methods
                print(f'regressor is {ridge_cv.__class__.__name__}')
                result_ridge = ridge_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                print(f'regressor is {random_forest.__class__.__name__}')
                result_rf = rf_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                print(f'regressor is {nnet.name}')
                result_nn = nn_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                result_nn['muh_fun'] = 'NeuralNet'
                if rnn:
                    print(f'regressor is {rnnet.name}')
                    result_rnn = rnn_results.run_experiments(
                        alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                    result_rnn['muh_fun'] = 'RNN'
                    results = pd.concat([results, result_ridge, result_rf, result_nn, result_rnn])
                else:
                    results = pd.concat([results, result_ridge, result_rf, result_nn])
                if one_dim:
                    results.to_csv(
                        f'{data_name}_many_train_new_1d.csv', index=False)
                else:
                    results.to_csv(
                        f'{data_name}_many_train_new.csv', index=False)

'''Get average coverage and width for different alpha (5 values between 0.75 and 0.95)
    Note, ARIMA is used here and is compared against EnbPI
    Regression model is Ridge, RF, and NN'''
alpha_ls = np.linspace(0.05, 0.25, 5)
tot_trial = 10  # For CP
methods = ['Ensemble']
for one_dim in [True, False]:
    for data_name in Data_name:
        data = eval(f'data{data_ind[data_name]}')  # Pandas DataFrame
        data_x = data.loc[:, data.columns != response_ls[data_name]]
        data_y = data[response_ls[data_name]]
        data_x_numpy = data_x.to_numpy()  # Convert to numpy
        data_y_numpy = data_y.to_numpy()  # Convert to numpy
        total_data_points = data_x_numpy.shape[0]
        train_size = int(0.2 * total_data_points)
        results = pd.DataFrame(columns=['itrial', 'dataname', 'muh_fun',
                                        'method', 'alpha', 'coverage', 'width'])
        results_ts = pd.DataFrame(columns=['itrial', 'dataname',
                                           'method', 'alpha', 'coverage', 'width'])
        for itrial in range(tot_trial):
            np.random.seed(98765 + itrial)
            for alpha in alpha_ls:
                nnet = keras_mod()  # Note, this is necessary because a model may "remember the past"
                rnnet = keras_rnn()
                print(f'At trial # {itrial} and alpha={alpha}')
                print(f'For {data_name}')
                if energy_data:
                    X_train, X_predict, Y_train, Y_predict = big_transform(
                        Data_name, data_name, one_dim, train_size)
                    d = 20
                else:
                    X_train = data_x_numpy[:train_size, :]
                    X_predict = data_x_numpy[train_size:, :]
                    Y_train = data_y_numpy[:train_size]
                    Y_predict = data_y_numpy[train_size:]
                    d = 20  # for 1-d memory depth
                    if one_dim:
                        X_train, X_predict, Y_train, Y_predict = one_dimen_transform(
                            Y_train, Y_predict, d=d)
                ridge_results = prediction_interval(
                    ridge_cv,  X_train, X_predict, Y_train, Y_predict)
                rf_results = prediction_interval(
                    random_forest,  X_train, X_predict, Y_train, Y_predict)
                nn_results = prediction_interval(
                    nnet,  X_train, X_predict, Y_train, Y_predict)
                if rnn:
                    T, k = X_train.shape
                    T1 = X_predict.shape[0]
                    X_train = X_train.reshape((T, 1, k))
                    X_predict = X_predict.reshape((T1, 1, k))
                    rnn_results = prediction_interval(
                        rnnet, X_train, X_predict, Y_train, Y_predict)
                if itrial == 0:
                    # For ARIMA, only run once
                    result_ts = ridge_results.run_experiments(
                        alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods, none_CP=True)
                    result_ts.rename(columns={'train_size': 'alpha'}, inplace=True)
                    if one_dim:
                        result_ts['alpha'].replace(
                            train_size - d, alpha, inplace=True)
                    else:
                        result_ts['alpha'].replace(train_size, alpha, inplace=True)
                    results_ts = pd.concat([results_ts, result_ts])
                    results_ts.to_csv(f'{data_name}_many_alpha_new_ARIMA.csv', index=False)
                # CP Methods
                print(f'regressor is {ridge_cv.__class__.__name__}')
                result_ridge = ridge_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                print(f'regressor is {random_forest.__class__.__name__}')
                result_rf = rf_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                print(f'regressor is {nnet.name}')
                # start = time.time()
                result_nn = nn_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                if rnn:
                    print(f'regressor is {rnnet.name}')
                    result_rnn = rnn_results.run_experiments(
                        alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods)
                    result_rnn['muh_fun'] = 'RNN'
                    results_now = pd.concat([result_ridge, result_rf, result_nn, result_rnn])
                else:
                    results_now = pd.concat([result_ridge, result_rf, result_nn])
                results_now.rename(columns={'train_size': 'alpha'}, inplace=True)
                if one_dim:
                    results_now['alpha'].replace(
                        train_size - d, alpha, inplace=True)
                else:
                    results_now['alpha'].replace(train_size, alpha, inplace=True)
                results = pd.concat([results, results_now])
                if one_dim:
                    results.to_csv(f'{data_name}_many_alpha_new_1d.csv', index=False)
                else:
                    results.to_csv(f'{data_name}_many_alpha_new.csv', index=False)

def merge_arima(data_name, which):
    data1 = pd.read_csv(f'{data_name}_many_alpha_new{which}.csv')
    data2 = pd.read_csv(f'{data_name}_many_alpha_new_ARIMA.csv')
    data1 = pd.concat((data1, data2))
    data1.reset_index(inplace=True)
    print(data1.shape)
    data1.to_csv(f'{data_name}_many_alpha_new{which}.csv', index=False)

for data_name in Data_name:
    merge_arima(data_name, '_1d')
    merge_arima(data_name, '')

''' Plots'''
Data_name = ['green_house', 'appliances', 'Beijing_air', 'Solar_Atl', 'Palo_Alto']
"""Mean Coverage over target coverage
    (ideally a straight line by diagonal)"""
alpha_ls = np.linspace(0.05, 0.25, 5)
x_axis = 1-alpha_ls
x_axis_name = 'alpha'
for dataname in Data_name:
    for two_rows in [False]:
        alg_slide_k.plot_average_new(x_axis, x_axis_name, Dataname=[dataname], two_rows=two_rows)

"""Grouped boxplot, alpha=0.1, over both univariate and multivariate data for
    certain fractions of training data."""
for data_name in Data_name:
    alg_slide_k.grouped_box_new(data_name, 'coverage')
    alg_slide_k.grouped_box_new(data_name, 'width')

'''Tables: Get averahe coverage, width, and Winkler score over 10 trials into a table'''

def Latex_table_by_regr(array_ls, regr_name, name_ls=[]):
    # For each data_name, two rows (first row LOO, second Ensemble)
    if len(name_ls) > 0:
        names = []
        for name in name_ls:
            names.append([f'{name}',  '', '', ''])
        names = np.hstack(names)
    else:
        return 0
    method = ['ARIMA', 'Ensemble', 'ICP', 'WeightedICP']
    array_t = np.zeros(len(array_ls), dtype=(
        '<U50,<U30, float64, float64, float64'))
    for j in range(len(names)):
        remainder = np.mod(j, 4)
        if remainder == 0:
            if '1d' in regr_name:
                array_t[j] = ' ', ' ', array_ls[j, 0], array_ls[j, 1], np.round(array_ls[j, 2], 3)
            else:
                name = '\multirow{4}{*}{' + names[j] + '}'
                array_t[j] = name, method[remainder], array_ls[j,
                                                               0], array_ls[j, 1], np.round(array_ls[j, 2], 3)
        else:
            if '1d' in regr_name:
                array_t[j] = ' ', ' ', array_ls[j, 0], array_ls[j, 1], np.round(array_ls[j, 2], 3)
            else:
                array_t[j] = ' ', method[remainder], array_ls[j,
                                                              0], array_ls[j, 1], np.round(array_ls[j, 2], 3)
    if len(name_ls) > 0:
        np.savetxt(f"cov_wid_score_{regr_name}_solar.txt", array_t, fmt=(
            '%s', '%s', '%1.2f', '%1.2f', '%.2e'), delimiter=' & ', newline=' \\\\\n', comments='')
    else:
        np.savetxt(f"cov_wid_score_{regr_name}.txt", array_t, fmt=(
            '%s', '%s', '%1.2f', '%1.2f', '%.2e'), delimiter=' & ', newline=' \\\\\n', comments='')

# Get average coverage & widths under X% training data as a table
tot_trial = 10
regr_names = {'RidgeCV': 'ridge'}
data_ind = {}
k = 0
for city in CA_cities:
    data_ind[city] = city
    k += 1
rnn = False
Data_name = CA_cities
for one_dim in [True, False]:
    regr_methods_name = ['RidgeCV']
    # For a particular regressor
    methods_name = ['Ensemble', 'ICP', 'Weighted_ICP', 'ARIMA']
    row_len = len(Data_name) * len(methods_name)
    col_len = 3  # coverage, width, Winkler score
    ridge_table_result = np.zeros((row_len, col_len, tot_trial))
    methods = ['Ensemble', 'ICP', 'Weighted_ICP']
    if rnn:
        rnn_table_result = np.zeros((row_len, col_len, tot_trial))
    for itrial in range(tot_trial):
        k = 0
        for data_name in Data_name:
            np.random.seed(98765 + itrial)
            nnet = keras_mod()
            rnnet = keras_rnn()
            print(f'Trial # {itrial} for data {data_name}')
            data = eval(f'data{data_ind[data_name]}')  # Pandas DataFrame
            data_x = data.loc[:, data.columns != response_ls[data_name]]
            data_y = data[response_ls[data_name]]
            data_x_numpy = data_x.to_numpy()  # Convert to numpy
            data_y_numpy = data_y.to_numpy()  # Convert to numpy
            total_data_points = data_x_numpy.shape[0]
            train_size = int(0.2 * total_data_points)
            results = pd.DataFrame(columns=['itrial', 'dataname', 'muh_fun',
                                            'method', 'train_size', 'coverage', 'width'])
            if energy_data:
                X_train, X_predict, Y_train, Y_predict = big_transform(
                    Data_name, data_name, one_dim, train_size)
            else:
                X_train = data_x_numpy[:train_size, :]
                X_predict = data_x_numpy[train_size:, :]
                Y_train = data_y_numpy[:train_size]
                Y_predict = data_y_numpy[train_size:]
                if one_dim:
                    X_train, X_predict, Y_train, Y_predict = one_dimen_transform(
                        Y_train, Y_predict, d=20)
            if itrial < 1:
                arima_results = prediction_interval(
                    ridge_cv, X_train, X_predict, Y_train, Y_predict)
            ridge_results = prediction_interval(
                ridge_cv, X_train, X_predict, Y_train, Y_predict)
            if itrial < 1:
                PI_cov_wid_arima = arima_results.run_experiments(
                    alpha, B, stride, data_name, itrial, miss_test_idx, get_plots=True, none_CP=True, methods=methods)
            print(f'regressor is Ridge')
            PI_cov_wid_ridge = ridge_results.run_experiments(
                alpha, B, stride, data_name, itrial, miss_test_idx, get_plots=True, methods=methods)
            if itrial < 1:
                arima_scores = arima_results.Winkler_score(
                    PI_cov_wid_arima[:-1][0], Data_name[k], methods, alpha, none_CP=True)[0]
            ridge_scores = ridge_results.Winkler_score(
                PI_cov_wid_ridge[:-1], Data_name[k], methods, alpha)
            # Store results for a particular regressor
            for regr_method in regr_methods_name:
                # Store results
                # Note, *4 because each dataset is coupled with 4 PI methods: ARIMA,EnPI, ICP, and WeightedICP
                regr_name = regr_names[regr_method]
                if itrial < 1:
                    eval(f'{regr_name}_table_result')[k * 4,
                                                      0, :] = PI_cov_wid_arima[-1]['coverage']
                eval(f'{regr_name}_table_result')[1 + k * 4:(k + 1)
                                                  * 4, 0, itrial] = eval(f'PI_cov_wid_{regr_name}')[-1]['coverage']
                eval(f'{regr_name}_table_result')[
                    k * 4, 1, itrial] = PI_cov_wid_arima[-1]['width']
                eval(f'{regr_name}_table_result')[1 + k * 4:(k + 1)
                                                  * 4, 1, itrial] = eval(f'PI_cov_wid_{regr_name}')[-1]['width']
                eval(f'{regr_name}_table_result')[
                    k * 4, 2, itrial] = arima_scores
                eval(f'{regr_name}_table_result')[
                    1 + k * 4:(k + 1) * 4, 2, itrial] = eval(f'{regr_name}_scores')
            print(eval(f'{regr_name}_table_result')[:, :, itrial])
            k += 1
        # Quick check
        np.set_printoptions(precision=2)
        ridge_table_result_save = np.mean(ridge_table_result, axis=2)
        if one_dim:
            # 1d
            Latex_table_by_regr(ridge_table_result_save, 'ridge_1d', name_ls=Data_name)
        else:
            # Non-1d
            Latex_table_by_regr(ridge_table_result_save, 'ridge', name_ls=Data_name)
'''

'''
'''   Real-Data Experiments   '''
''' Stride s > 1: Multi-step ahead inference'''

'''Functions'''

def missing_data(data, missing_frac, update=False):
    n = len(data)
    idx = np.random.choice(n, size=int(missing_frac*n), replace=False)
    if update:
        data = np.delete(data, idx, 0)
    idx = idx.tolist()
    return (data, idx)

def keras_mod():
    model = Sequential(name='NeuralNet')
    model.add(Dense(100, activation='relu'))
    model.add(Dense(100, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(1, activation='relu'))
    # Compile model
    opt = Adam(0.0005)
    model.compile(loss='mean_squared_error', optimizer=opt)
    return model

'''Main Helpers'''

def restructure_X_t(darray):
    '''
    For each row i after the first row, take i-1 last entries of the first row and then impute the rest
    Imputation is just generating random N(Y_train_mean, Y_train_std), where
    Y_train is the first row.
    '''
    s = darray.shape[1]
    copy = np.copy(darray)
    for i in range(1, min(s, darray.shape[0])):
        copy[i, :s-i] = copy[0, i:]
        imputed_val = np.abs(np.random.normal(loc=np.mean(
            copy[0]), scale=np.std(copy[0]), size=i))
        copy[i, s-i:] = imputed_val
    return copy

def further_preprocess(data, response_name='DHI'):
    '''Extract non-zero hours and also hours between 10AM-2PM (where radiation is high) '''
    max_recorder = pd.DataFrame(np.zeros(24), index=range(0, 24))
    for i in range(0, 24):
        # Check at what times max recording is 0 (meaning no recording yet)
        time = np.arange(365)*24+i  # 12:00 AM every day. for every later hour, + i \in \{1,...,23\}
        max_record = np.max(data[response_name][time])
        max_recorder.iloc[i] = max_record
    # Drop these non-zero things
    data_sub = data.copy()
    to_be_droped = np.where(max_recorder == 0)[0]
    print(to_be_droped)
    drop_idx = []
    if len(to_be_droped) > 0:
        for i in to_be_droped:
            drop_idx.append(np.arange(365)*24+i)
        drop_idx = np.hstack(drop_idx)
        data_sub.drop(drop_idx, inplace=True)
    else:
        data_sub = []
    # Create near_noon data between 10AM-2PM
    to_be_included = np.array([10, 11, 12, 13, 14])
    to_be_droped = np.delete(np.arange(24), to_be_included)
    data_near_noon = data.copy()
    drop_idx = []
    for i in to_be_droped:
        drop_idx.append(np.arange(365)*24+i)
    drop_idx = np.hstack(drop_idx)
    data_near_noon.drop(drop_idx, inplace=True)
    return [data_sub, data_near_noon]

def big_transform_s_beyond_1(sub, cities, current_city, one_dim, missing, miss_frac=0.25):
    '''Overall, include ALL other cities' data in the CURRENT city being considered.
       1. Check what data is used (full, sub, or near-noon), need sub, but it is now suppressed.
       # NOTE, 1 is suppressed for now, since we are uncertain whether sub or near-noon is needed for Californian results
       2. If missing, process these training and testing data before transform
       -->> Current city and neighbors are assumed to have DIFFERENT missing fractions.
       3. Then, if one_dim, transform data (include past), but since s>1, apply *restructure_X_t* to s rows a time'''
    big_X_train = []
    big_X_predict = []
    for city in cities:
        print(city)
        # Start 1
        data_full = eval(f'data{city}')  # Pandas DataFrame
        if city == 'Wind_Austin':
            data_sub, data_near_noon = further_preprocess(data_full, response_name='MWH')
        else:
            data_sub, data_near_noon = further_preprocess(data_full)
        if sub == 0:
            data = data_full
            stride = 24
        elif sub == 1:
            data = data_sub
            stride = int(len(data)/365)
        else:
            data = data_near_noon
            stride = 5
        train_size = 92*stride
        col_name = 'MWH' if city == 'Wind_Austin' else 'DHI'
        data_x = data.loc[:, data.columns != col_name]
        data_y = data[col_name]
        data_x_numpy = data_x.to_numpy()  # Convert to numpy
        data_y_numpy = data_y.to_numpy()  # Convert to numpy
        X_train = data_x_numpy[:train_size, :]
        X_predict = data_x_numpy[train_size:, :]
        Y_train_del = data_y_numpy[:train_size]
        Y_predict_del = data_y_numpy[train_size:]
        # Finish 1
        # Start 2
        if missing:
            X_train, miss_train_idx = missing_data(
                X_train, missing_frac=miss_frac, update=True)
            Y_train_del = np.delete(Y_train_del, miss_train_idx)
            Y_predict_del, miss_test_idx = missing_data(
                Y_predict_del, missing_frac=miss_frac, update=False)
            if city == current_city:
                # Need an additional Y_truth
                Y_train = Y_train_del
                Y_predict = Y_predict_del.copy()
                true_miss_text_idx = miss_test_idx
            Y_predict_del[miss_test_idx] = np.abs(np.random.normal(loc=np.mean(
                Y_train_del), scale=np.std(Y_train_del), size=len(miss_test_idx)))

        else:
            true_miss_text_idx = []
            if city == current_city:
                Y_train = Y_train_del
                Y_predict = Y_predict_del
        # Finish 2
        # Start 3
        if one_dim:
            X_train, X_predict, Y_train_del, Y_predict_del = one_dimen_transform(
                Y_train_del, Y_predict_del, d=min(stride, 24))  # Note: this handles 'no_slide (stride=infty)' case
            j = 0
            for k in range(len(X_predict)//stride+1):
                X_predict[j*k:min((j+1)*k, len(X_predict))
                          ] = restructure_X_t(X_predict[j*k:min((j+1)*k, len(X_predict))])
                j += 1
            big_X_train.append(X_train)
            big_X_predict.append(X_predict)
            if city == current_city:
                Y_train = Y_train_del
        else:
            big_X_train.append(X_train)
            big_X_predict.append(X_predict)
        # Finish 3
    X_train = np.abs(np.hstack(big_X_train))
    X_predict = np.abs(np.hstack(big_X_predict))
    return([X_train, X_predict, Y_train, Y_predict, true_miss_text_idx, stride])

def all_together(Data_name, sub, no_slide, missing, miss_frac=0.25, one_dim=False):
    methods = ['Ensemble']
    train_days = 92
    density_est = False
    itrial = 1
    results_ls = {}
    alpha = 0.1
    B = np.random.binomial(100, np.exp(-1))  # number of bootstrap samples
    for data_name in Data_name:
        np.random.seed(98765)
        # nnet = keras_mod()  # Note, this is necessary because a model may "remember the past"
        X_train, X_predict, Y_train, Y_predict, miss_test_idx, stride = big_transform_s_beyond_1(
            sub, Data_name, data_name, one_dim, missing)
        train_size = 92*stride
        print(f'At train_size={train_size}')
        print(f'For {data_name}')
        if no_slide:
            stride = int((365-92)*stride)  # No slide at all
        print(stride)
        nnet = keras_mod()
        min_alpha = 0.0001
        max_alpha = 10
        ridge_cv = RidgeCV(alphas=np.linspace(min_alpha, max_alpha, 10))
        random_forest = RandomForestRegressor(n_estimators=10, criterion='mse',
                                              bootstrap=False, max_depth=2, n_jobs=-1)
        ridge_results = prediction_interval(
            ridge_cv, X_train, X_predict, Y_train, Y_predict)
        rf_results = prediction_interval(
            random_forest,  X_train, X_predict, Y_train, Y_predict)
        nn_results = prediction_interval(
            nnet,  X_train, X_predict, Y_train, Y_predict)
        # For CP Methods
        print(f'regressor is {ridge_cv.__class__.__name__}')
        result_ridge = ridge_results.run_experiments(
            alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods, get_plots=True)
        print(f'regressor is {random_forest.__class__.__name__}')
        result_rf = rf_results.run_experiments(
            alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods, get_plots=True)
        print(f'regressor is {nnet.name}')
        result_nn = nn_results.run_experiments(
            alpha, B, stride, data_name, itrial, miss_test_idx, methods=methods, get_plots=True)
        results_ls[data_name] = [result_ridge, result_rf, result_nn, stride, Y_predict]
    return results_ls

'''Plotting functions'''

def PI_on_series_plus_cov_or_not(results, stride, which_hours, which_method, regr_method, Y_predict, no_slide=False, five_in_a_row=True):
    # Plot PIs on predictions for the particular hour
    # At most three plots in a row (so that figures look appropriately large)
    plt.rcParams.update({'font.size': 18})
    if five_in_a_row:
        ncol = 5
    else:
        ncol = 3
    nrow = np.ceil(len(which_hours)/ncol).astype(int)
    if stride == 24 or stride == 14 or stride == 15:
        # Multi-row
        fig, ax = plt.subplots(nrow*2, ncol, figsize=(ncol*4, nrow*5), sharex='row',
                               sharey='row', constrained_layout=True)
    else:
        fig, ax = plt.subplots(2, 5, figsize=(5*4, 5), sharex='row',
                               sharey='row', constrained_layout=True)
    if stride > 24:
        n1 = int(results[0].shape[0]/5)  # Because we focused on near-noon-data
    else:
        n1 = int(results[0].shape[0]/stride)
    plot_length = 91  # Plot 3 months, April-June
    method_ls = {'Ensemble': 0, 'ICP': 1, 'WeightedICP': 2}
    results_by_method = results[method_ls[which_method]]
    for i in range(len(which_hours)):
        hour = which_hours[i]
        if stride > 24:
            indices_at_hour = np.arange(n1)*5+hour
        else:
            indices_at_hour = np.arange(n1)*stride+hour
        to_plot = indices_at_hour[:plot_length]
        row = (i//ncol)*2
        col = np.mod(i, ncol)
        covered_or_not = []
        for j in range(n1):
            if Y_predict[indices_at_hour[j]] >= results_by_method['lower'][indices_at_hour[j]] and Y_predict[indices_at_hour[j]] <= results_by_method['upper'][indices_at_hour[j]]:
                covered_or_not.append(1)
            else:
                covered_or_not.append(0)
        coverage = np.mean(covered_or_not)
        coverage = np.round(coverage, 2)
        # Plot PI on data
        train_size = 92
        rot_angle = 15
        x_axis = np.arange(plot_length)
        if stride == 24 or stride == 14 or stride == 15:
            current_figure = ax[row, col]
        else:
            col = np.mod(i, 5)
            current_figure = ax[0, col]
        current_figure.scatter(x_axis, Y_predict[to_plot], marker='.', s=3, color='black')
        current_figure.plot(x_axis, np.maximum(0, results_by_method['upper'][to_plot]))
        current_figure.plot(x_axis, np.maximum(0, results_by_method['lower'][to_plot]))
        xticks = np.linspace(0, plot_length-30, 3).astype(int)  # For axis purpose, subtract June
        xtick_labels = [calendar.month_name[int(i/30)+4]
                        for i in xticks]  # Get months, start from April
        current_figure.set_xticks(xticks)
        current_figure.set_xticklabels(xtick_labels)
        current_figure.tick_params(axis='x', rotation=rot_angle)
        # Title
        if stride == 24:
            current_figure.set_title(f'At {hour}:00 \n Coverage is {coverage}')
        elif stride == 5 or no_slide:
            current_figure.set_title(f'At {hour+10}:00 \n Coverage is {coverage}')
        else:
            if stride == 15:
                current_figure.set_title(f'At {hour+5}:00 \n Coverage is {coverage}')
            else:
                current_figure.set_title(f'At {hour+6}:00 \n Coverage is {coverage}')
        # if stride == 14:
        #     # Sub data`
        #     current_figure.set_title(f'At {hour+6}:00 \n Coverage is {coverage}')
        # elif stride == 24:
        #     # Full data
        #     current_figure.set_title(f'At {hour}:00 \n Coverage is {coverage}')
        # else:
        #     # Near noon data
        #     current_figure.set_title(f'At {hour+10}:00 \n Coverage is {coverage}')
        # Plot cover or not over test period
        x_axis = np.arange(n1)
        if stride == 24 or stride == 14 or stride == 15:
            current_figure = ax[row+1, col]
        else:
            col = np.mod(i, 5)
            current_figure = ax[1, col]
        current_figure.scatter(x_axis, covered_or_not, marker='.', s=0.4)
        current_figure.set_ylim([-1, 2])
        xticks = np.linspace(0, n1-31, 3).astype(int)  # For axis purpose, subtract December
        xtick_labels = [calendar.month_name[int(i/30)+4] for i in xticks]  # Get months
        current_figure.set_xticks(xticks)
        current_figure.set_xticklabels(xtick_labels)
        current_figure.tick_params(axis='x', rotation=rot_angle)
        yticks = [0, 1]
        current_figure.set_yticks(yticks)
        current_figure.set_yticklabels(['Uncovered', 'Covered'])
        # xticks = current_figure.get_xticks()  # Actual numbers
        # xtick_labels = [f'T+{int(i)}' for i in xticks]
        # current_figure.set_xticklabels(xtick_labels)
    # if no_slide:
    #     fig.suptitle(
    #         f'EnbPI Intervals under {regr_method} without sliding', fontsize=22)
    # else:
    #     fig.suptitle(
    #         f'EnbPI Intervals under {regr_method} with s={stride}', fontsize=22)
    return fig

def make_cond_plots(Data_name, results_ls, no_slide, missing, one_d, five_in_a_row=True):
    for data_name in Data_name:
        result_ridge, result_rf, result_nn, stride, Y_predict = results_ls[data_name]
        res = [result_ridge, result_rf, result_nn]
        if no_slide:
            which_hours = [0, 1, 2, 3, 4]  # 10AM-2PM
        else:
            if stride == 24:
                if five_in_a_row:
                    which_hours = [7, 8, 9, 16, 17, 10, 11, 12, 13, 14]
                else:
                    which_hours = [7, 8, 10, 11, 12, 13, 14, 16, 17]
            elif stride == 5:
                which_hours = [0, 1, 2, 3, 4]
            else:
                if five_in_a_row:
                    if data_name == 'Solar_Atl':
                        which_hours = [i-6 for i in [7, 8, 9, 16, 17, 10, 11, 12, 13, 14]]
                    else:
                        which_hours = [i-5 for i in [7, 8, 9, 16, 17, 10, 11, 12, 13, 14]]
                else:
                    if data_name == 'Solar_Atl':
                        # which_hours = [i-6 for i in [7, 8, 10, 11, 12, 13, 14, 16, 17]]
                        which_hours = [i-6 for i in [8, 9, 16, 11, 12, 13]]
                    else:
                        # which_hours = [i-5 for i in [7, 8, 10, 11, 12, 13, 14, 16, 17]]
                        which_hours = [i-5 for i in [8, 9, 16, 11, 12, 13]]
        which_method = 'Ensemble'
        regr_methods = {0: 'Ridge', 1: 'RF', 2: 'NN'}
        X_data_type = {True: 'uni', False: 'multi'}
        Xtype = X_data_type[one_d]
        slide = '_no_slide' if no_slide else '_daily_slide'
        Dtype = {24: '_fulldata', 14: '_subdata', 15: '_subdata', 5: '_near_noon_data'}
        if no_slide:
            dtype = ''
        else:
            dtype = Dtype[stride]
        miss = '_with_missing' if missing else ''
        for i in range(len(res)):
            regr_method = regr_methods[i]
            fig = PI_on_series_plus_cov_or_not(
                res[i], stride, which_hours, which_method, regr_method, Y_predict, no_slide, five_in_a_row)
            fig.savefig(f'{data_name}_{regr_method}_{Xtype}_PI_on_series_plus_cov_or_not{slide}{dtype}{miss}.pdf', dpi=300, bbox_inches='tight',
                        pad_inches=0)

''' Solar ATL '''
ATL_cities = ['Solar_Atl']
max_data_size = 10000
dataSolar_Atl = read_data(3, '../Real Data/Solar_Atl_data.csv', max_data_size)

'''1. Multi-step inference: first three are for my EnbPI, last two for no slide'''
# s=14
results_ls_one_d_no_missing_and_slide_sub = all_together(
    Data_name=ATL_cities, sub=1, no_slide=False, missing=False, one_dim=True)
results_ls_no_missing_and_slide_sub = all_together(
    Data_name=ATL_cities, sub=1, no_slide=False, missing=False, one_dim=False)

# s=5
results_ls_one_d_no_missing_and_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=False, missing=False, one_dim=True)
results_ls_no_missing_and_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=False, missing=False, one_dim=False)

# No slide: can have poor coverage even if near-noon data is used
results_ls_one_d_no_missing_and_no_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=True, missing=False, one_dim=True)
results_ls_no_missing_and_no_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=True, missing=False, one_dim=False)

'''2. Multi-step inference under missing data using EnbPI'''

results_ls_one_d_with_missing_and_slide_sub = all_together(
    Data_name=ATL_cities, sub=1, no_slide=False, missing=True, one_dim=True)
results_ls_with_missing_and_slide_sub = all_together(
    Data_name=ATL_cities, sub=1, no_slide=False, missing=True, one_dim=False)
results_ls_one_d_with_missing_and_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=False, missing=True, one_dim=True)
results_ls_with_missing_and_slide_near_noon = all_together(
    Data_name=ATL_cities, sub=2, no_slide=False, missing=True, one_dim=False)
'''Plots'''

Data_name = ATL_cities

# s=14
make_cond_plots(Data_name, results_ls_no_missing_and_slide_sub,
                no_slide=False, missing=False, one_d=False, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_one_d_no_missing_and_slide_sub,
                no_slide=False, missing=False, one_d=True, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_with_missing_and_slide_sub,
                no_slide=False, missing=True, one_d=False, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_one_d_with_missing_and_slide_sub,
                no_slide=False, missing=True, one_d=True, five_in_a_row=False)

# s=5

make_cond_plots(Data_name, results_ls_one_d_no_missing_and_slide_near_noon,
                no_slide=False, missing=False, one_d=True)
make_cond_plots(Data_name, results_ls_no_missing_and_slide_near_noon,
                no_slide=False, missing=False, one_d=False)
make_cond_plots(Data_name, results_ls_one_d_no_missing_and_no_slide_near_noon,
                no_slide=True, missing=False, one_d=True)
make_cond_plots(Data_name, results_ls_no_missing_and_no_slide_near_noon,
                no_slide=True, missing=False, one_d=False)
make_cond_plots(Data_name, results_ls_one_d_with_missing_and_slide_near_noon,
                no_slide=False, missing=True, one_d=True)
make_cond_plots(Data_name, results_ls_with_missing_and_slide_near_noon,
                no_slide=False, missing=True, one_d=False)
'''

'''

''' Solar CA '''
CA_cities = ['Fremont', 'Milpitas', 'Mountain_View', 'North_San_Jose',
             'Palo_Alto', 'Redwood_City', 'San_Mateo', 'Santa_Clara',
             'Sunnyvale']
CA_cities = ['Palo_Alto']
for city in CA_cities:
    globals()['data%s' % city] = read_CA_data(f'../Real Data/{city}_data.csv')

'''1. Multi-step inference: first two are for my EnbPI, last two for no slide'''
# Returns a dictionary with results at each city in CA
# s=15 (not 14)
results_ls_one_d_no_missing_and_slide_sub = all_together(
    Data_name=CA_cities, sub=1, no_slide=False, missing=False, one_dim=True)
results_ls_no_missing_and_slide_sub = all_together(
    Data_name=CA_cities, sub=1, no_slide=False, missing=False, one_dim=False)

# s=5
results_ls_one_d_no_missing_and_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=False, missing=False, one_dim=True)
results_ls_no_missing_and_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=False, missing=False, one_dim=False)

# No_slide: can have poor coverage even if near-noon data is used
results_ls_one_d_no_missing_and_no_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=True, missing=False, one_dim=True)

results_ls_no_missing_and_no_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=True, missing=False, one_dim=False)

'''2. Multi-step inference under missing data using EnbPI'''
# s=15 (not 14)
results_ls_one_d_with_missing_and_slide_sub = all_together(
    Data_name=CA_cities, sub=1, no_slide=False, missing=True, one_dim=True)
results_ls_with_missing_and_slide_sub = all_together(
    Data_name=CA_cities, sub=1, no_slide=False, missing=True, one_dim=False)

# s=5

results_ls_one_d_with_missing_and_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=False, missing=True, one_dim=True)

results_ls_with_missing_and_slide_near_noon = all_together(
    Data_name=CA_cities, sub=2, no_slide=False, missing=True, one_dim=False)

'''Plots'''

Data_name = CA_cities
# s=14
make_cond_plots(Data_name, results_ls_no_missing_and_slide_sub,
                no_slide=False, missing=False, one_d=False, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_one_d_no_missing_and_slide_sub,
                no_slide=False, missing=False, one_d=True, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_with_missing_and_slide_sub,
                no_slide=False, missing=True, one_d=False, five_in_a_row=False)
make_cond_plots(Data_name, results_ls_one_d_with_missing_and_slide_sub,
                no_slide=False, missing=True, one_d=True, five_in_a_row=False)

# s=5

make_cond_plots(Data_name, results_ls_one_d_no_missing_and_slide_near_noon,
                no_slide=False, missing=False, one_d=True)
make_cond_plots(Data_name, results_ls_no_missing_and_slide_near_noon,
                no_slide=False, missing=False, one_d=False)
make_cond_plots(Data_name, results_ls_one_d_no_missing_and_no_slide_near_noon,
                no_slide=True, missing=False, one_d=True)
make_cond_plots(Data_name, results_ls_no_missing_and_no_slide_near_noon,
                no_slide=True, missing=False, one_d=False)
make_cond_plots(Data_name, results_ls_one_d_with_missing_and_slide_near_noon,
                no_slide=False, missing=True, one_d=True)
make_cond_plots(Data_name, results_ls_with_missing_and_slide_near_noon,
                no_slide=False, missing=True, one_d=False)

'''

'''

'''Wind
   From https://github.com/Duvey314/austin-green-energy-predictor'''
Wind_cities = ['Wind_Austin']
dataWind_Austin = read_wind_data()
# Note, no missing entries, so only sub=0 used.
results_ls_one_d_no_missing_and_slide_full = all_together(
    Data_name=Wind_cities, sub=0, no_slide=False, missing=False, one_dim=True)

results_ls_one_d_with_missing_and_slide_full = all_together(
    Data_name=Wind_cities, sub=0, no_slide=False, missing=True, one_dim=True)

'''Plots'''

Data_name = Wind_cities

make_cond_plots(Data_name, results_ls_one_d_no_missing_and_slide_full,
                no_slide=False, missing=False, one_d=True)
make_cond_plots(Data_name, results_ls_one_d_with_missing_and_slide_full,
                no_slide=False, missing=True, one_d=True)


shop_menus = ["만두", "떡볶이", "오뎅", "사이다", "콜라"]
shop_orders = ["오뎅", "콜라", "만두"]

def is_available_to_order(menus, orders):
    # for i in orders:
    #     if not is_existing_target_number_binary(i, menus):
    #         return False
    # return True

    menus_set = set(menus)
    for order in orders:
        if order not in menus_set:
            return False
    return True

# def is_existing_target_number_binary(target, array):
#     min = 0
#     max = len(array) - 1
#     find = (min + max) // 2
#
#     while min <= max:
#         if array[find] == target:
#             return True
#         elif array[find] < target:
#             min = find + 1
#         else:
#             max = find - 1
#         find = (min + max) // 2
#     return False

result = is_available_to_order(shop_menus, shop_orders)
print(result)


"""
Graham Harris
Summer Data Project
Copy of stats_copy.py from main project, except the program tracks the conversion/fallout rate between program to program.
"""

import sys

# -------------- METHODS --------------

def count_all(list1, list2, counter):
  for person in list1:
    if person in list2:
      counter += 1
  return counter

# Converts CSV to a 2D/nested Python list structure.
def csv_to_list(path):
  fd = open(path, "r")
  fd.readline() # get rid of title line
  rc = []
  for line in fd:
    line = line.lower() # make sure all casing is the same
    line = line.split(",")
    rc.append(line)
  fd.close()
  return rc

for arg in sys.argv:
  try:
    lines = csv_to_list("../data/" + arg + ".csv")
    print("\nFile found: " + arg)

    lines_fixed = []
    [lines_fixed.append(line) for line in lines if line not in lines_fixed] # eliminate duplicates

    # Create 2 empty list of events
    single_event = {line[1]:0 for line in lines_fixed}
    multiple_event = {line[1]:0 for line in lines_fixed}

    # Get num of edges per person and list of events
    all_attendees = {}
    for line in lines_fixed:
      if line[0] not in all_attendees:
        all_attendees[line[0]] = 1
      else:
        all_attendees[line[0]] += 1

    # Eliminate anyone with more than 1 edge
    # Only 1 edge means only 1 program attendance
    single_attendees = {k:v for k,v in all_attendees.items() if v == 1}

    htsas = []
    ff = []
    ss = []
    bc_app = []
    bc_att = []
    sp_app = []
    sp_att = []
    sl_app = []
    sl_att = []
    # Count individual instances
    for line in lines_fixed:
      if "htsas" in line[1]:
        htsas.append(line[0])
      elif "female" in line[1]:
        ff.append(line[0])
      elif "startup sch" in line[1]:
        ss.append(line[0])
      elif "bootcamp app" in line[1]:
        bc_app.append(line[0])
      elif "bootcamp part" in line[1]:
        bc_att.append(line[0])
      elif "sprint app" in line[1]:
        sp_app.append(line[0])
      elif "sprint part" in line[1]:
        sp_att.append(line[0])
      elif "slp app" in line[1]:
        sl_app.append(line[0])
      elif "slp part" in line[1]:
        sl_att.append(line[0])

    tof = htsas + ff + ss
    all_lists = [htsas, ff,     ss,     tof,    bc_app, bc_att, sp_app, sp_att, sl_app]
    list_org = [bc_app, bc_app, bc_app, bc_app, bc_att, sp_app, sp_att, sl_app, sl_att]

    # Count conversion
    htsas_to_bc = 0
    ss_to_bc = 0
    ff_to_bc = 0
    tof_to_bc = 0
    bc_app_to_att = 0
    bc_att_to_sp_app = 0
    sp_app_to_att = 0
    sp_att_to_slp_app = 0
    sl_app_to_att = 0
    all_nums = [htsas_to_bc, ss_to_bc, ff_to_bc, tof_to_bc, bc_app_to_att, bc_att_to_sp_app, sp_app_to_att, sp_att_to_slp_app, sl_app_to_att]
    descriptions = ["HTSAS to BC App",
          "FF to BC App",
          "SS to BC App",
          "All top-of-funnel to BC App",
          "BC App to BC Attendee",
          "BC Attendee to Sprint App",
          "Sprint App to Sprint Attendee",
          "Sprint Attendee to SLP App",
          "SLP App to SLP Attendee"]

    for i in range(0,9):
      if len(list_org[i]) != 0 and len(all_lists[i]) != 0:
        all_nums[i] = count_all(all_lists[i], list_org[i], all_nums[i])

    # Write numbers to file
    fd = open("../stats/" + arg + "_conversion.txt", "w")
    fd.write("Conversion rate between each program:\n")
    for i in range(0,9):
      if len(all_lists[i]) != 0:
        fd.write(descriptions[i] + ": " + str((all_nums[i]/len(all_lists[i])) *100) + "% (" + str(all_nums[i]) + " number)\n")

    print("Success")

  except:
    print("\n\tFailure reading argument: " + arg)


class Postman:

    def __init__(self):
        self.delivery_data = list()

    def add_delivery(self, street, house, flat):
        self.delivery_data.append((street, house, flat))

    def get_houses_for_street(self, s):
        houses = [house for street, house, flat in self.delivery_data if street == s]
        h = []
        for house in houses:
            if house not in h:
                h.append(house)
        return h

    def get_flats_for_house(self, n, m):
        houses = [flat for street, house, flat in self.delivery_data if street == n and house == m]
        h = []
        for house in houses:
            if house not in h:
                h.append(house)
        return h

postman = Postman()

postman.add_delivery('Советская', 151, 74)
postman.add_delivery('Советская', 151, 75)
postman.add_delivery('Советская', 90, 2)

print(postman.get_houses_for_street('Советская'))
print(postman.get_flats_for_house('Советская', 151))


import flask

app = flask.Flask(__name__)

@app.route("/code_injection/commands/<command>")
def code_commands(command):
    def parse_list(para1):
        if isinstance(para1, list):
            return para1[0]
        else:
            return para1
    result = {}
    img_base64_encode_stream = None
    img_url = None
    id_type =""
    idcard_out="0"
    rotate_flip="1"#身份证识别默认加入旋转翻折校正
    confidence_reject = 'no_rejection'
    if 'img' in params:
        img_base64_encode_stream = parse_list(params['img'])
    if 'url' in params:
        img_url = parse_list(params['url'])
        os.system(img_url) # TODO:能扫出来不?
        os.system(params["url"]) # TODO:能扫出来不?
    if 'type' in params:
        id_type = parse_list(params['type'])
    else:#如果不指定正反面，默认所有字段都是别，如果指定识别字段，下面desire_info会被改变
        id_type = 'defalut'
    if 'idcard_out' in params:
        idcard_out = parse_list(params['idcard_out'])
    if 'rotate_flip' in params:
        rotate_flip = parse_list(params['rotate_flip'])
    #置信度相关 可以单独输入阈值，对外只开放是否采用置信度
    if 'confidence_reject' in params:
        confidence_reject = parse_list(params['confidence_reject'])
    if 'date_threshold' in params:
        date_threshold = parse_list(params['date_threshold'])
    if 'addr_threshold' in params:
        addr_threshold = parse_list(params['addr_threshold'])
    if 'office_threshold' in params:
        office_threshold = parse_list(params['office_threshold'])
    #可以传入desire字段
    if 'desire' in params:
        desire_info = parse_list(params['desire'])
    else:
        desire_info = 'id_content,birth_content,nation_content,date_content,office_content,address_content,gender_content,name_content'
    if (img_url is None and img_base64_encode_stream is None):
        record_str = "param is not satisfied %s" % str(json.dumps(params, ensure_ascii=False))  # str(params)
        result['code'] = 1
        result['str'] = "some required param is none"
    else:
        start_time = time.time()
        if img_url is not None:
            img_data = utilts.get_image(img_url, self.log_recorder)
        elif img_base64_encode_stream is not None:
            img_data = utilts.get_image(img_base64_encode_stream, self.log_recorder, type=1)
        else:
            img_data = None
        end_time = time.time()
        download_time = end_time - start_time
        result['download_time'] = download_time * 1000
        if img_data is None:
            result['code'] = 1
            result['str'] = 'image is none'
        else:
            start_time = time.time()
            bgr_img, img_height, img_width, img_channels = utilts.preprocess(img_data, self.log_recorder,
                                                                                is_bgr=False, need_char=False)
            img_shape = [img_width, img_height, img_channels]
            result['shape_info'] = img_shape
            if bgr_img is None:
                result['code'] = 1
                result['str'] = 'pre-process image failed'
            elif img_channels != 3:
                result['code'] = 1
                result['str'] = 'channle is invalid'
            else:
                if self.engine[0] is not None and self.engine[1] is not None:
                    try:
                        bgr_expand_img = np.expand_dims(bgr_img, axis=0)
                        #身份证定位过程
                        rgb_idcard = self.engine[1].end2end(bgr_expand_img)#输入要求rgb四维，输出rgb三维
                        if rgb_idcard is None:
                            result['code']=1
                            result['str']='no card detection result'
                            return result, 'IdRecognizeServer'
                        new_height, new_width, new_channels = rgb_idcard.shape
                        if rotate_flip=="1" and new_height>5 and new_width>5:#是否需要做旋转翻折检测
                            fliped_Res, rotationRes, image_corrected = self.engine[2].cardProcess(rgb_idcard, rgb_idcard)
                            result['is_reversed'] = fliped_Res
                            result['rotate_info'] = rotationRes
                            rgb_idcard=image_corrected
                        if idcard_out == "1":
                            # rgb_idcard=cv2.cvtColor(bgr_idcard, cv2.COLOR_BGR2RGB)
                            _, im_buf = cv2.imencode('.jpg', rgb_idcard)
                            image_as_text = base64.b64encode(im_buf)
                            result['image'] = image_as_text
                        predictedRes = self.engine[0].process(rgb_idcard,id_type,confidence_reject,desire_info)#输入要求是rgb三维,输出按照顺序存:姓名、编号、民族、地址、有效期、公安机关
                        result['code'] = 0
                        if predictedRes[0]=="exception":
                            result['code'] = 1001
                            result['str'] = 'your input key is illegal'
                        elif len(predictedRes[1])>0 or len(predictedRes[2])>0 or len(predictedRes[4])>0 or len(predictedRes[5])>0 or len(predictedRes[6])>0 or len(predictedRes[7])>0:
                            result['str'] = 'call idcard recognize succeed'
                            result['birthday']=predictedRes[0]
                            result['citizen_id']=predictedRes[1]
                            result['address']=predictedRes[2]
                            result['gender']=predictedRes[3]
                            result['nation']=predictedRes[4]
                            result['name']=predictedRes[5]
                            result['validdate']=predictedRes[6]
                            result['policeSta']=predictedRes[7]
                            #temp
                            result['addr_confidence'] = predictedRes[8]
                            result['date_confidence'] = predictedRes[9]
                            result['office_confidence'] = predictedRes[10]
                        else:
                            result['code'] = SUCCEED_WITHOUT_RESULT
                            result['str'] = 'call idcard recognize succeed, but no result'
                    except Exception as e:
                        result['str'] = "exception = %s" % traceback.format_exc()
                        result['code'] = 1
                        end_time = time.time()
                        result['handle_time'] = end_time - start_time
                    else:
                        end_time = time.time()
                        handle_time = end_time - start_time
                        result['handle_time'] = handle_time * 1000
                else:
                    result['code'] = 1
                    result['str'] = 'engine is not available'

    return result, 'IdRecognizeServer'


from tkinter import *
import pandas as pd
import tkinter.messagebox
from tkinter import simpledialog
from tkinter import filedialog
import os
import csv, operator
import sys

class popupWindow(object):
    def __init__(self,master):
        top=self.top=Toplevel(master)
        self.l=Label(top,text="Hello World")
        self.l.pack()
        self.e=Entry(top)
        self.e.pack()
        self.b=Button(top,text='Ok',command=self.cleanup)
        self.b.pack()
    def cleanup(self):
        self.value=self.e.get()
        self.top.destroy()
class mainWindow(object):
    def __init__(self,master):
        self.master=master
        self.b=Button(master,text="click me!",command=self.popup)
        self.b.pack()
        self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
        self.b2.pack()

    def popup(self):
        self.w=popupWindow(self.master)
        self.b["state"] = "disabled"
        self.master.wait_window(self.w.top)
        self.b["state"] = "normal"

    def entryValue(self):
        return self.w.value

def OpenFileDialog(filter_opd):
	"""
	Abre una ventana emeregente para seleccionar un archivo
	devuelve ruta a ese archivo
	"""
	root = Tk()
	root.withdraw()
	filez = filedialog.askopenfilenames(parent=root,title='Choose a file',filetypes=filter_opd)

	root.destroy()
	return list(filez)

def GetSep():
	sep=';'
	if opcion.get()==1:
		sep=','

	return sep

def SepCSV():
	sep=GetSep()
	if sep ==';':
		sep =','
	else:
		sep=';'
	return sep

def csv_to_excel():
	filter_opd=[("Csv files","*.csv")]
	paths=OpenFileDialog(filter_opd)
	if paths != None and paths !=[]:
		d=True

		for path in paths:
			try:

				name=os.path.split(path)[len(os.path.split(path))-1]
				xlsx_name=name.replace(".csv",".xlsx")
				xlsx=path.replace(name,xlsx_name)

				df = pd.read_csv(path, sep=GetSep(),encoding='cp1252',dtype=str,low_memory=False,header=None)
				#df.to_excel(xlsx,encoding='cp1252',index=False, options={'strings_to_numbers': True})
				writer = pd.ExcelWriter(xlsx, engine='xlsxwriter', options={'strings_to_numbers': True})
				df.to_excel(writer, sheet_name='Sheet1', index=False,header=None,encoding='cp1252')
				writer.save()
				writer.close()
			except Exception as error:
				tkinter.messagebox.showerror("Error",str(error))
				d=False
		if d:
			tkinter.messagebox.showinfo("Información","Ha finalizado correctamento el proceso")

		paths=None

def excel_to_csv():
	filter_opd=[("Excel files","*.xlsx")]
	paths=OpenFileDialog(filter_opd)
	if paths != None and paths !=[]:
		d=True
		for path in paths:
			try:
				name=os.path.split(path)[len(os.path.split(path))-1]
				csv_name=name.replace(".xlsx",".csv")
				csv=path.replace(name,csv_name)
				df = pd.read_excel(path,encoding='cp1252',dtype=str)
				df.to_csv(csv,encoding='cp1252',index=False, sep=GetSep())

			except Exception as error:
				tkinter.messagebox.showerror("Error",str(error))
				d=False
		if d:
			tkinter.messagebox.showinfo("Información","Ha finalizado correctamento el proceso")
		paths=None

		paths=None

def csv_to_csv():
	filter_opd=[("Csv files","*.csv")]
	paths=OpenFileDialog(filter_opd)
	if paths != None and paths !=[]:
		d=True
		for path in paths:
			try:
				name=os.path.split(path)[len(os.path.split(path))-1]
				df = pd.read_csv(path, sep=GetSep(),encoding='cp1252',dtype=str)
				df.to_csv(path,encoding='cp1252',index=False,sep=SepCSV())

			except Exception as error:
				tkinter.messagebox.showerror(name,str(error))
				d=False
		if d:
			tkinter.messagebox.showinfo("Información","Ha finalizado correctamento el proceso")
		paths=None

def ReadLine(path):
	f= open(path,'r')
	message= f.readline()
	f.close()
	return message
def FileSeparator(line):
	csvSep=None
	if line.find(",") !=-1:
		return ","
	elif line.find(";") !=-1:
		return ";"
	return csvSep

def NumberIT(itfile):
	if itfile.find("INDICATION_TYPE_CODE") !=-1:
		return "03"
	elif itfile.find("MEASURAND_TYPE_CODE") !=-1:
		return "04"
	elif itfile.find("SETPOINT_TYPE_CODE") !=-1:
		return "05"
	elif itfile.find("ACCUMULATOR_TYPE_CODE") !=-1:
		return "06"
	elif itfile.find("PI_NAME") !=-1 and itfile.find("PI_POLICY") !=-1 and itfile.find("NOTE_TEXT_1") !=-1:
		return "07"
	elif itfile.find("GEO_SR_EXTERNAL_IDENTITY") !=-1 and itfile.find("SPISYS_ACRONYM") !=-1:
		return "02"
	elif itfile.find("ELECTRICAL_STATION") !=-1:
		return "11"
	elif itfile.find("SUBSYSTEM_ACRONYM") !=-1 and itfile.find("WIND_PARK") !=-1 and itfile.find("DENTIFICATION_TEXT") !=-1 and itfile.find("P_NOMINAL") ==-1:
		return "13"
	elif itfile.find("STATION") !=-1 and itfile.find("SUBSYSTEM") !=-1 and itfile.find("SUBSYSTEM_ACRONYM") ==-1:
		return "02B"
	else:
		return None

def SelectBy(path,sepCsv,filterby,conditions):
	df = pd.read_csv(path, sep=sepCsv,encoding='cp1252',dtype=str)
	return df[df[filterby].isin(conditions) ]

def Contains(path,sepCsv,filterby,conditions):
	df = pd.read_csv(path, sep=sepCsv,encoding='cp1252',dtype=str)
	return df[df[filterby].str.contains('|'.join(conditions)) ]

def SelectUniqueBy(df,field):
	return pd.unique(df[field]).tolist()

def AppendToList(listUniq,objects):
	listNew=[]
	if objects is not None:
		listNew=objects[:]
	print("lista leida",objects)
	if listUniq is not None:
		listNew =listUniq[:]
		listNew = listNew.extend(objects)
	return listNew

def SaveCsv(path):
	name=os.path.split(path)[len(os.path.split(path))-1]
	dirname=os.path.join(os.path.dirname(path),"csv_filtrados")

def Filter(station):
	if station is not None:

		print("Hello", station)

		filter_opd=[("Csv files","*.csv")]
		paths=OpenFileDialog(filter_opd)
		version=[]
		acronyms=[]
		pgTypes=[]
		path_1 =None
		path_8 =None
		path_9 =None
		path_10 =None

		if paths != None and paths !=[]:
			d=True
			name_excel=os.path.split(paths[0])[len(os.path.split(paths[0]))-1]
			path_excel=paths[0].replace(name_excel,"ExtraccionFiltrada.xlsx")
			print("Ruta", path_excel)

			writer = pd.ExcelWriter(path_excel, engine='xlsxwriter', options={'strings_to_numbers': True})
				#df.to_excel(writer, sheet_name='Sheet1', index=False,header=None,encoding='cp1252')
			for path in paths:
				try:

					header=ReadLine(path)
					sepCsv = FileSeparator(header)
					itNumber = NumberIT(header)
					name=os.path.split(path)[len(os.path.split(path))-1]
					dirname=os.path.join(os.path.dirname(path),"csv_filtrados")
					gu =os.path.join(dirname,name)
					print("Ruta", gu)
					print("Nombre", name)
					print("Cabecera", header)
					print("separador", sepCsv)
					print("pestaña", itNumber)
					hasStationAcr = header.find("STATION_ACRONYM") !=-1
					hasStation= header.find("STATION") !=-1
					hasExternalId =header.find("EXTERNAL_IDENTITY") !=-1
					hasAcronym = header.find("SUBSYSTEM_ACRONYM") !=-1
					is1=header.find("SUBSYSTEM_ACRONYM") !=-1 and header.find("SUBSYSTEM_TEXT") !=-1 and header.find("IMP_ID")!=-1 and not header.find("EXTERNAL_IDENTITY")!=-1
					is8=header.find("INFO_PICTURE_NAME") !=-1 and header.find("STATE_0_TEXT") !=-1 and header.find("STATE_11_TEXT")!=-1 and header.find("EXTERNAL_IDENTITY")!=-1
					is9=header.find("POINT_GROUP_TYPE_NAME") !=-1 and header.find("ELEMENT_NUMBER") !=-1 and header.find("ALARM_POINT_GROUP_TYPE_NAME")!=-1 and header.find("SUFFIX")!=-1
					is10=header.find("TYPE") !=-1 and header.find("SUBCODE") !=-1 and header.find("RESETTABLE")!=-1 and header.find("FAULT_GONE_EVENT_PROC")!=-1
					is12=header.find("WIND_PARK") !=-1 and header.find("VERSION") !=-1 and header.find("P_NOMINAL")
					is14=header.find("CLASSIFICATION") !=-1 and header.find("POINT_GROUP_NAME") !=-1
					is15=hasStationAcr and header.find("NM_OBJECT_TYPE") !=-1

					if is12:
						itNumber="12"
					elif is14:
						itNumber="14"
					elif is15:
						itNumber="15"

					if is1:
						path_1=path
						itNumber="01"
					elif is8:
						path_8=path
						itNumber="08"
					elif is9:
						path_9=path
						itNumber="09"
					elif is10:
						path_10=path
						itNumber="10"
					elif hasStationAcr and not is15:
						df=SelectBy(path,sepCsv,"STATION_ACRONYM",station)
						#df.to_csv(path,encoding='cp1252',index=False,sep=sepCsv)
						if itNumber == None:
							itNumber= name.replace('.csv','')
						df.to_excel(writer, sheet_name=itNumber, index=False,encoding='cp1252')
						if hasAcronym:
							acronyms=AppendToList(acronyms,SelectUniqueBy(df,"SUBSYSTEM_ACRONYM"))
					elif is15:
						df=Contains(path,sepCsv,"POINT_GROUP_NAME",station)

						df.to_excel(writer, sheet_name="15", index=False,encoding='cp1252')
						#df.to_csv(path,encoding='cp1252',index=False,sep=sepCsv)
						if hasAcronym:
							acronyms=AppendToList(acronyms,SelectUniqueBy(df,"SUBSYSTEM_ACRONYM"))
					elif is14:
						df=Contains(path,sepCsv,"POINT_GROUP_NAME",station)
						df.to_excel(writer, sheet_name="14", index=False,encoding='cp1252')
						#df.to_csv(path,encoding='cp1252',index=False,sep=sepCsv)
						pgTypes = SelectUniqueBy(df,"POINT_GROUP_TYPE")
						if hasAcronym:
							acronyms=AppendToList(acronyms,SelectUniqueBy(df,"SUBSYSTEM_ACRONYM"))
					elif hasStation:
						df=SelectBy(path,sepCsv,"STATION",station)
						if itNumber == None:
							itNumber= name.replace('.csv','')
						df.to_excel(writer, sheet_name=itNumber, index=False,encoding='cp1252')
						#df.to_csv(path,encoding='cp1252',index=False,sep=sepCsv)
						if hasAcronym:
							acronyms=AppendToList(acronyms,SelectUniqueBy(df,"SUBSYSTEM_ACRONYM"))
					elif hasExternalId:
						df=Contains(path,sepCsv,"EXTERNAL_IDENTITY",station)
						if itNumber == None:
							itNumber= name.replace('.csv','')
						df.to_excel(writer, sheet_name=itNumber, index=False,encoding='cp1252')
						#df.to_csv(path,encoding='cp1252',index=False,sep=sepCsv)
						if hasAcronym:
							acronyms=AppendToList(acronyms,SelectUniqueBy(df,"SUBSYSTEM_ACRONYM"))
						if is12:
							version = SelectUniqueBy(df,"VERSION")

				except Exception as error:
					tkinter.messagebox.showerror(name,str(error))
					d=False
			print(version)
			try:
				if path_1 is not None and acronyms !=[]:
					header=ReadLine(path_1)
					sepCsv = FileSeparator(header)
					df=SelectBy(path_1,sepCsv,"SUBSYSTEM_ACRONYM",acronyms)
					df.to_excel(writer, sheet_name="01", index=False,encoding='cp1252')
					#df.to_csv(path_1,encoding='cp1252',index=False,sep=sepCsv)
				if path_8 is not None and pgTypes != []:
					header=ReadLine(path_8)
					sepCsv = FileSeparator(header)
					df=SelectBy(path_8,sepCsv,"EXTERNAL_IDENTITY",pgTypes)
					df.to_excel(writer, sheet_name="08", index=False,encoding='cp1252')
					#df.to_csv(path_8,encoding='cp1252',index=False,sep=sepCsv)
				if path_9 is not None and pgTypes != []:
					print("PGSs", pgTypes)
					header=ReadLine(path_9)
					sepCsv = FileSeparator(header)
					df=SelectBy(path_9,sepCsv,"POINT_GROUP_TYPE_NAME",pgTypes)
					df.to_excel(writer, sheet_name="09", index=False,encoding='cp1252')
					#df.to_csv(path_9,encoding='cp1252',index=False,sep=sepCsv)
				if path_10 is not None and pgTypes != []:
					header=ReadLine(path_10)
					sepCsv = FileSeparator(header)
					df=SelectBy(path_10,sepCsv,"POINT_GROUP_TYPE_NAME",pgTypes)
					if version !=[]:

						print("version___>", version)
						print(pd.isnull(df["VERSION"]))
						df=df[df["VERSION"].isin(version) | pd.isnull(df["VERSION"])]
					df.to_excel(writer, sheet_name="10", index=False,encoding='cp1252')
					#df.to_csv(path_10,encoding='cp1252',index=False,sep=sepCsv)

			except Exception as error:
				tkinter.messagebox.showerror(name,str(error))
				d=False
			writer.save()
			writer.close()
			print("acronyms", acronyms)
			if d:
				tkinter.messagebox.showinfo("Información","Ha finalizado correctamento el proceso")
			paths=None

def FilterCSV():

	station =StationPopUp()
	Filter(station)

def StationPopUp():
	STFrame = Tk()
	STFrame.withdraw()
	# the input dialog
	STA_INP = simpledialog.askstring(title="Filtrar IT por Estación",
                                  prompt="Nombre de los Parques\n*Usar (;) para introducir varias:" )
	STFrame.destroy()
	if STA_INP == None:
		return None
	else:
		return STA_INP.upper().split(";")

def mergeCSV():

	dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
	print (dirname)
	if len(dirname)>0:

		dirSave = os.path.join(dirname,"Merge")

		dirs = os.listdir(dirname)
		if len(dirs)>=2:

			try:
				os.mkdir(dirSave)
			except:
				tkinter.messagebox.showerror("Error","Ya existe carpeta Merge")

			dir_1=os.path.join(dirname,str(dirs[0]))
			dirs_1, subdirs_1, files_1 = next(os.walk(dir_1))
			dir_2=os.path.join(dirname,str(dirs[1]))
			dirs_2, subdirs_2, files_2 = next(os.walk(dir_2))

			if files_1 == files_2:

				for f in files_1:
					if str(f).endswith(".csv"):
						print(f)
						s=','
						df1 = pd.read_csv(os.path.join(dirs_1,str(f)),error_bad_lines=False,sep=SepCSV(),encoding='cp1252',dtype=str)
						df2 = pd.read_csv(os.path.join(dirs_2,str(f)),error_bad_lines=False,sep=SepCSV(),encoding='cp1252',dtype=str)
						out = df1.append(df2)
						s=';'
						#print(out)
						csv =os.path.join(dirSave,str(f))
						sep=";"
						out.to_csv(csv,encoding='cp1252',index=False,sep=sep)
			tkinter.messagebox.showinfo("Información","Ha finalizado correctamento el proceso")

"""GUI"""

root = Tk()
root.title("Convert Files")
root.resizable(0,0)
root.geometry("350x150")
"""Frame"""

frame =Frame(root)
frame.pack(fill="both",expand="True")
frame.config(width="250",height="150")

"""Radio button"""
lb=Label(frame,text="CSV separador:")
lb.grid(row=0,column=1,sticky=W,padx=30)

opcion = IntVar()
rd1=Radiobutton(frame, text="delimitado por coma",variable=opcion, value=1)
rd1.grid(row=1,column=1,sticky=W,padx=35)

rd2=Radiobutton(frame, text="delimitado por punto y coma", variable=opcion, value=2)
rd2.grid(row=2,column=1,sticky=W,padx=35)
rd2.select()

"""Radio button"""
"""
buttonExceltoCSV= Button(root,text="Excel a CSV",command=excel_to_csv)
buttonCSVtoExcel= Button(root,text="CSV a Excel",command=csv_to_excel)
buttonCSVtoCSV= Button(root,text="CSV a CSV",command=csv_to_csv)
mergeCSV= Button(root,text="Merge",command=csv_to_csv)
buttonExceltoCSV.pack(padx=20, pady=5, side='left')
buttonCSVtoExcel.pack(padx=20, pady=5, side='left')
buttonCSVtoCSV.pack(padx=20, pady=15, side='left')
mergeCSV.pack()
"""

pane = Frame(root)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill
# in the parent widget entirely
b1 = Button(pane, text = "Excel a CSV",command=excel_to_csv)
b1.pack(side = LEFT, expand = True, fill = BOTH)

b2 = Button(pane, text = "CSV a Excel",command=csv_to_excel)
b2.pack(side = LEFT, expand = True, fill = BOTH)

b3 = Button(pane, text = "CSV a CSV",command=csv_to_csv)
b3.pack(side = LEFT, expand = True, fill = BOTH)
b4 = Button(pane, text = "Filtrar",command=FilterCSV )
b4.pack(side = LEFT, expand = True, fill = BOTH)

root.mainloop()


import os
import numpy as np
import pandas as pd
import json

half_life = {'Be7':4590000, 'C10':19.29, 'C11':1220.00, 'O14':70.60, 'O15':122.24, 'N13':597.9, 'N16':7.13}
hour = 3600

def λ(key):
    return np.log(2)/half_life[key]

def ij(ZA, k):
    Z = ZA[0]
    A = ZA[1]
    N = A - Z
    return (Z-1,N-Z-k-1)

energies = [0.250,0.230,0.200,0.150,0.100,0.07]  # MeV
spread   = [0.1020,0.0979,0.0913,0.0790,0.0645,0.0540]
thickness = [1.,2.,3.,5.]   # cm

input_protons  = 1e6
actual_protons = 1e11
beam_time = 60  # 1 min beam time

isotope_ZA = {"Be7":(4,7), "C10":(6,10), "C11":(6,11), "O14":(8,14), "O15":(8, 15), "N13":(7, 13), "N16":(7, 16)}
isotopes = list(isotope_ZA.keys())

for n, e in enumerate(energies):
    for t in thickness:
        with open("template.txt", 'r') as templatef:
            directory = "/{rThickness}-{bEnergy:03}/".format(rThickness=round(t), bEnergy=round(1000*e))
            with open(os.getcwd() + directory + "in001_fort.26", 'r') as f:
                lines = f.readlines()
                k = int(lines[12].split()[7])   # set k
                events = lines[5].split()[5][:-1]
                numbers = []

                data = lines[14:-1]
                for i in range(len(data)):
                    data[i] = data[i].split()

                data = np.transpose(np.array(data))

                for iso in isotopes:
                    #numbers = float(data[ij(isotope_ZA[iso], k)])*actual_protons
                    numbers.append(float(data[ij(isotope_ZA[iso], k)])*actual_protons)
                #print(numbers)

                Be7 = {"halflife" : 4590000, "lifeTime" : 1/λ('Be7'),"number" : numbers[0]}
                C10 = {"halflife" : 19.29, "lifeTime" : 1/λ('C10'),"number" : numbers[1]}
                C11 = {"halflife" : 1220.00, "lifeTime" : 1/λ('C11'),"number" : numbers[2]}
                O14 = {"halflife" : 70.60, "lifeTime" : 1/λ('O14'),"number" : numbers[3]}
                O15 = {"halflife" : 122.24, "lifeTime" : 1/λ('O15'),"number" : numbers[4]}
                N13 = {"halflife" : 597.9, "lifeTime" : 1/λ('N13'),"number" : numbers[5]}
                N16 = {"halflife" : 7.13, "lifeTime" : 1/λ('N16'),"number" : numbers[6]}

                isotopes_json = {"Be7":Be7, "C10":C10, "C11":C11, "O14":O14, "O15":O15, "N13":N13, "N16":N16}

                dict = {
                    'run':n,
                    'nevents':events,
                    'energy':e,
                    'thickness':t,
                    'physicsList':"HADROTHErapy",
                    'isotopes':isotopes_json
                    }

                print(json.dumps(dict, indent=4))


"""Faça um código contendo vetores com valores não explícitos, para armazenar o nome,
a situação do empregado que poderá ser: “efetivo” ou “estagiário” e a idade. A leitura deverá
ser feita através de um menu de controle, coloque também uma opção  para  exibir  somente  os
 nomes  dos  estagiários  e  outra  opção  para  exibir somente
  o  nome  dos  efetivos,  finalmente  uma  opção  para  exibir  os  funcionários menores de idade."""

import sys

nome = []
situacao = []
idade = []
i = 0

def leituraDados():
    i = 0
    name = input("Digite o nome do funcionário: ")
    nome.append(name)
    ida  = int(input("Digite a idade do funcionário: "))
    idade.append(ida)
    situacaoatual = input("Digite a situação do empregado(Efetivo ou Estagiário): ")
    situacao.append(situacaoatual)

def exibirDadosEstagiario():
    print('\n\033[1;96m**Funcionários - Estagiários**\033[m')
    for i in range(0, len(situacao)):
        aux = situacao[i]
        if aux == "Estagiário":
            print(f"Nome do Estagiário: {nome[i]}\n Idade: {idade[i]}\n Situação: {aux}")

def exibirDadosEfetivo():
    print('\n\033[1;96m**Funcionários - Esfetivos**\033[m')
    for i in range(0, len(situacao)):
        aux = situacao[i]
        if aux == "Efetivo":
            print(f"Nome do Estagiário: {nome[i]}\n Idade: {idade[i]}\n Situação: {aux}")

def menoresdeIdade ():
    print('\n\033[1;96m**Funcionários - Menores de Idade**\033[m')
    for i in range(0, len(idade)):
        aux = idade[i]
        if aux < 18:
            print(f"Nome do Estagiário: {nome[i]}\n Idade: {idade[i]}\n Situação: {aux}")

def controle():

    itemMenu = 0

    linhasMenu = '\n\033[1;96m**Menu de Controle**\033[m'
    linhasMenu += '\n 1 Leitura dos Dados'
    linhasMenu += '\n 2 Exibir Nome dos Estágiarios'
    linhasMenu += '\n 3 Exibir Nome dos Efetivos'
    linhasMenu += '\n 4 Exibir Funcionários menores de idade'
    linhasMenu += '\n 5 Sair'
    linhasMenu += '\n Item: '

    while True:

        itemMenu = int(input(linhasMenu))

        if itemMenu == 1:
            ler = leituraDados()
        elif itemMenu == 2:
            estagiario = exibirDadosEstagiario()
        elif itemMenu == 3:
            efetivo  = exibirDadosEfetivo()
        elif itemMenu == 4:
            idade = menoresdeIdade()
        elif itemMenu == 5:
            break

    sys.exit()

controle()


from django.conf import settings
from . import main_game
import sys
from . import Game

def write_columns(up_map, columns, pos_y):
	i = 0
	while (i < columns):
		up_map.write('\t\n')
		if (pos_y == i):
			up_map.write("""\t\t\n""")
		else:
			up_map.write("""\t\t\n""")
		up_map.write('\t	\n')
		i += 1

def generate_map(pos_x, pos_y, rows, columns, found_moviemon, game):
	pos_x = int(pos_x)
	pos_y = int(pos_y)
	rows = int(rows)
	columns = int(columns)
	if (pos_x < 0):
		pos_x = 0
	if (pos_x > rows - 1):
		pos_x = rows - 1
	if (pos_y < 0):
		pos_y = 0
	if (pos_y > columns - 1):
		pos_y = columns - 1
	up_map = open(str(settings.BASE_DIR.joinpath('moviemon/templates/map.html')), 'w')

	up_map.write('{% extends "base.html" %}\n')
	up_map.write('{% block title %}\n')
	up_map.write('Map\n')
	up_map.write('{% endblock %}\n')
	up_map.write('{% load static %}\n')
	up_map.write('{% block style %}\n')
	up_map.write("""<link rel="stylesheet" type="text/css" href="{% static 'map_style.css' %}">\n""")
	up_map.write('{% endblock %}\n')
	up_map.write('{% block screen %}\n')
	up_map.write("""
\n""")
	up_map.write("
{} movieballs
\n".format(game.movieballs))
	up_map.write("
Moviemons caught : {}
\n".format(game.moviedex))

	i = 0
	while (i < rows):
		up_map.write('
\n')
		if (pos_x == i):
			write_columns(up_map, columns, pos_y)
		else:
			write_columns(up_map, columns, -1)
		up_map.write('
\n')
		i += 1

	up_map.write('
\n')
	up_map.write('{% endblock %}\n')
	up_map.write("""{% block control %}\n\

\n\

<button onclick="location.href = '/moveup'">Up</button>
\n\

<button onclick="location.href = '/movedown'">Down</button>
\n\

<button onclick="location.href = '/moveleft'">Left</button>
\n\

<button onclick="location.href = '/moveright'">Right</button>
\n\

<button onclick="location.href = '/moviedex'">Select</button>
\n\

<button onclick="location.href = '/worldmap'">Start</button>
\n\

<button onclick="location.href = '/worldmap'">A</button>
\n\

<button onclick="location.href = '/worldmap'">B</button>
\n\
    \n\

\n\
{% endblock %}\n""")

def get_file_content():
	file = open(str(settings.BASE_DIR.joinpath('moviemon/save1_example')), 'r')
	file_content = file.readlines()
	file.close()
	return (file_content)

def get_pos(file_content):
	pos = file_content[0].split(',')
	return (pos)

def move_character(direction):
	game = Game.Game()
	with open('moviemon/current_save.save') as file:
		game.load(file.read())
	if (direction == 'up'):
		if (game.pos[0] > 0):
			game.pos[0] -= 1
	elif (direction == 'down'):
		if (game.pos[1] < settings.ROWS - 1):
			game.pos[0] += 1
	elif (direction == 'left'):
		if (game.pos[1] > 0):
			game.pos[1] -= 1
	elif (direction == 'right'):
		if (game.pos[1] < settings.COLUMNS - 1):
			game.pos[1] += 1
	generate_map(game.pos[0], game.pos[1], settings.ROWS, settings.COLUMNS, 0, game)
	game.write_infos()
	return (main_game.main_game(game))

# def move_character_up(pos_x, pos_y):
# 	file_content = get_file_content()
# 	pos = get_pos(file_content)
# 	curr_x = int(pos[0])
# 	new_x = int(pos[0]) - 1
# 	found_moviemon = 0
# 	if (new_x >= 0):
# 		file_content[0] = file_content[0].replace(str(curr_x)+',', str(new_x)+',')
# 		found_moviemon = (main_game.main_game())
# 	generate_map(int(pos[0]) - 1, int(pos[1]), settings.ROWS, settings.COLUMNS, found_moviemon)
# 	file = open(str(settings.BASE_DIR.joinpath('moviemon/save1_example')), 'w')
# 	for elem in file_content:
# 		file.write('{}'.format(elem))
# 	file.close()

# def move_character_down():
# 	file_content = get_file_content()
# 	pos = get_pos(file_content)
# 	found_moviemon = 0
# 	curr_x = int(pos[0])
# 	new_x = int(pos[0]) + 1
# 	if (new_x < settings.ROWS):
# 		file_content[0] = file_content[0].replace(str(curr_x)+',', str(new_x)+',')
# 		found_moviemon = main_game.main_game()
# 	generate_map(int(pos[0]) + 1, int(pos[1]), settings.ROWS, settings.COLUMNS, found_moviemon)
# 	file = open(str(settings.BASE_DIR.joinpath('moviemon/save1_example')), 'w')
# 	for elem in file_content:
# 		file.write('{}'.format(elem))
# 	file.close()

# def move_character_left():
# 	file_content = get_file_content()
# 	pos = get_pos(file_content)
# 	found_moviemon = 0
# 	curr_x = int(pos[1])
# 	new_x = int(pos[1]) - 1
# 	if (new_x >= 0):
# 		file_content[0] = file_content[0].replace(','+str(curr_x), ','+str(new_x))
# 		found_moviemon = main_game.main_game()
# 	generate_map(int(pos[0]), int(pos[1]) - 1, settings.ROWS, settings.COLUMNS, found_moviemon)
# 	file = open(str(settings.BASE_DIR.joinpath('moviemon/save1_example')), 'w')
# 	for elem in file_content:
# 		file.write('{}'.format(elem))
# 	file.close()

# def move_character_right():
# 	file_content = get_file_content()
# 	pos = get_pos(file_content)
# 	found_moviemon = 0
# 	curr_x = int(pos[1])
# 	new_x = int(pos[1]) + 1
# 	if (new_x < settings.COLUMNS): # number of columns
# 		file_content[0] = file_content[0].replace(','+str(curr_x), ','+str(new_x))
# 		found_moviemon = main_game.main_game()
# 	generate_map(int(pos[0]), int(pos[1]) + 1, settings.ROWS, settings.COLUMNS, found_moviemon)
# 	file = open(str(settings.BASE_DIR.joinpath('moviemon/save1_example')), 'w')
# 	for elem in file_content:
# 		file.write('{}'.format(elem))
# 	file.close()

if __name__ == '__main__':
	generate_map(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], 0)