__author__ = 'luizcartolano'

import json
from pytz import timezone
from time import time
from datetime import datetime
import gmplot

class ManageTweets(object):
    """docstring for ManageTweets."""
    def __init__(self):
        return

    '''
        Read .json file and store his content in a dictionary
    '''
    def read(self, path, fname):
        # starts an empty list for the tweets
        tweets = []
        # opens the .json file in write mode
        with open(path+fname, 'r') as file_json:
            # assigns the content that was in .json to a list
            jsonData = json.load(file_json)
            # traverses each of the data in the json file
            for data in jsonData:
                # Not all tweets use geospatial location, so we use try/except
                try:
                    # initialize the dictionary of each tweet
                    tweet = {}
                    # associates the coordinates of the tweet with the key "coords" in the dict
                    tweet["coords"] = data["coordinates"]["coordinates"]
                    # associates the tweet timestamp with the "time" key in the dict
                    tweet["time"] = data["timestamp_ms"]
                    # add the timezone of each tweet
                    tweet["timezone"] = "America/Sao_Paulo"
                    # add the dictionary to the list
                    tweets.append(tweet)
                except:
                    # if the tweet does not have geolocation then go to the ne
                    pass
        # returns a list of dictionaries
        return tweets

    '''
        Plot heat map a partir de uma lista de coordenadas.
    '''
    def plottingHeatmap(self, path, key, coords, city):
        # sets the filename
        fname = 'heatmap_'+city+'_'+key+'.html'
        # sets the city center coordinate and zoom level.
        if city == 'chicago':
            gmap = gmplot.GoogleMapPlotter(41.878114, -87.629799, 10)
        elif city == 'london':
            gmap = gmplot.GoogleMapPlotter(51.507278, -0.127690, 10)
        elif city == 'ny':
            gmap = gmplot.GoogleMapPlotter(40.68402, -73.95704, 10)
        elif city == 'toronto':
            gmap = gmplot.GoogleMapPlotter(43.653226, -79.383184, 10)
        elif city == 'campinas':
            gmap = gmplot.GoogleMapPlotter(-22.907104, -47.063240, 10)
        else:
            print("PLOT ERROR: invalid city\n")
            return
        # select latitudes
        heat_lats = [coord[1] for coord in coords]
        # select longitudes
        heat_lngs = [coord[0] for coord in coords]
        if len(heat_lats):
            # generate the plot
            gmap.heatmap(heat_lats, heat_lngs,threshold=0)
            # save as .html file
            gmap.draw(path+fname)

        return

    '''
        Find the date of a tweet based on his timestamp
    '''
    def getDate(self, chartime, tz):
        timestamp = float(chartime)
        try:
            # if the timestamp is in seconds, the timestamp conversion to p / date occurs without errors
            date = datetime.fromtimestamp(timestamp)
        except:
            # otherwise, I need to convert from microseconds to sec first
            date = self.convertTimestamp(timestamp)
        date_aware = timezone(tz).localize(date)
        # converts according to tweet timezone
        new_date = date_aware.astimezone(timezone(tz))
        return new_date

    '''
        Convert the timestamp of a tweet from microseconds to seconds
    '''
    def convertTimestamp(self, timestamp):
        if timestamp > 1519231541:  # Date of today - UTC: Wednesday 21st February 2018 04:45:41 PM
            timestamp /= 1000		# convert from microseconds to seconds
        return datetime.fromtimestamp(timestamp)

    '''
        Slice the tweets grouping in hours of a day
    '''
    def slicingDocsHour(self, docs):
        list_docs = {}
        for doc in docs:
            date = self.getDate(doc["time"], doc["timezone"])
            # set the key with the format: daymonthhour
            key = str(date.day)+str(date.month)+str(date.year)+str(date.hour)
            try:
                # if there is already any list of tweets for the key, cocatena the new tweet the list
                list_docs[key] += [doc]
            except:
                # otherwise, start the list of tweets for the key
                list_docs[key] = [doc]
        # now we have a list with several sublists, where each sublist refers to tweets generated on a particular day and hour.
        return list_docs

    '''
        Slice the tweets grouping in periods of a day
    '''
    def slicingDocsRange(self, docs):
        list_docs = {}
        for doc in docs:
            date = self.getDate(doc["time"], doc["timezone"])
            if date.hour >= 8 and date.hour <= 12:
                # the dictionary key is defined by the day, month, year and morning period
                key = str(date.day)+str(date.month)+str(date.year)+str("morning")
            elif date.hour > 12 and date.hour <= 20:
                # the dictionary key is defined by the day, month, year and afternoon period
                key = str(date.day)+str(date.month)+str(date.year)+str("afternoon")
            elif date.hour > 20 and date.hour <= 24:
                # the dictionary key is defined by the day, month, year and night period
                key = str(date.day)+str(date.month)+str(date.year)+str("night")
            else:
                # the dictionary key is defined by the day, month, year and dawm period
                key = str(date.day)+str(date.month)+str(date.year)+str("dawm")
            try:
                # if there is already any list of tweets for the key, cocatena the new tweet the list
                list_docs[key] += [doc]
            except:
                # otherwise, start the list of tweets for the key
                list_docs[key] = [doc]
        # now we have a list with several sublists, where each sublist refers to tweets generated on a particular period of a day.
        return list_docs

    '''
        Slice the tweets grouping in days
    '''
    def slicingDocsDay(self, docs):
        list_docs = {}
        for doc in docs:
            date = self.getDate(doc["time"], doc["timezone"])
            # set the key with the format: daymonthhour
            key = str(date.day)+str(date.month)+str(date.year)
            try:
                # if there is already any list of tweets for the key, cocatena the new tweet the list
                list_docs[key] += [doc]
            except:
                # otherwise, start the list of tweets for the key
                list_docs[key] = [doc]
        # now we have a list with several sublists, where each sublist refers to tweets generated on a particular day.
        return list_docs

    '''
        Eliminates possible noise of the data obtained
    '''
    def deleteBot(self, docs):
        # initializes a list where the twetts without bots will be stored
        list_docs = []
        # for going through the tweets looking for a bot
        for doc in docs:
            # counting the number of occurrences of a specific geolocation
            count = self.countCoords(doc["coords"],docs)
            if count <= 10:
                # if a specific geolocation appears less than 10 times, we add to our list
                list_docs.append(doc)

        # deleting the old list of tweets
        del docs[:]
        # return the new list free of bots
        return list_docs

    '''
        Countig the occurrence of same coordinates in a list of coordinates
    '''
    def countCoords(self, coord, docs):
        # define a counter
        counter = 0
        for tweet in docs:
            # add one to our counter everytime a specific geolocation appears
            if tweet["coords"] == coord:
                counter += 1

        return counter

    '''
        Function to manage the plotting of heat maps
    '''
    def plotMap(self, path, tweets, city):
        # we go through the dictionary of coordinate lists
        for key,value in tweets.iteritems():
            # initializes the list of coordinates to be plotted
            coords = []
            # each value (tweets list separated by keys in our dictionary), we go through the set of coordinates (lat, long)
            for tweet in value:
                # add the coordinates in the list to be plotted
                coords.append(tweet["coords"])
            # plots the heatmap for each dictionary key
            self.plottingHeatmap(path,key,coords,city)


from keeper import keeper
import locale

from .settings_stuff import list_of_settings, country_into_code
from .timezone import get_timezones_from_country
import os

class Handler:
    def __init__(self, builder):
        self.builder = builder

# ADDING CONTROLLER TO HANDLER
# ----------------------------------------------------------------------------------------------------------------------
    def add_controller(self, controller):
        self.controller = controller

# NEXT
# ----------------------------------------------------------------------------------------------------------------------
    def next(self,button):
        self.controller.execute()

# BACK
# ----------------------------------------------------------------------------------------------------------------------
    def back(self,button):
        self.controller.back()

# METHOD TO HANDLE COUNTRY COMBO-BOX
# ----------------------------------------------------------------------------------------------------------------------
    def country_handler(self, item):
        keeper['settingspage']['country'] = item.get_active_id()
        self.insert_languages()
        self.insert_timezones()

# METHOD TO HANDLE LANGUAGE COMBO-BOX
# ----------------------------------------------------------------------------------------------------------------------
    def language_handler(self, item):
        keeper['settingspage']['language'] = item.get_active_text()

# METHOD TO HANDLE TIMEZONE COMBO-BOX
# ----------------------------------------------------------------------------------------------------------------------
    def timezone_handler(self, item):
        keeper['settingspage']['timezone'] = item.get_active_text()

# METHOD TO INSERT LANGUAGES
# ----------------------------------------------------------------------------------------------------------------------
    def insert_languages(self):
        languages_object = self.builder.get_object('combo_box_language')
        languages_object.remove_all()

        country = self.builder.get_object('combo_box_country').get_active_id()

        lang_in_keeper = keeper['settingspage']['language']
        list_of_lang = []
        for item in list_of_settings:
            if country == item[3]:
                list_of_lang.append(item[2])
                languages_object.append(item[2],item[2])

        if lang_in_keeper in list_of_lang:
            languages_object.set_active_id(lang_in_keeper)
        else:
            languages_object.set_active_id(list_of_lang[0])

# METHOD TO INSERT COUNTRIES INTO COUNTRY COMBO-BOX
# ----------------------------------------------------------------------------------------------------------------------
    def insert_countries(self):
        from .settings_stuff import countries
        countries_object = self.builder.get_object('combo_box_country')

        for item in countries:
           countries_object.append(item, item[:22])

        countries_object.set_active_id(keeper['settingspage']['country'])

# METHOD TO INSERT COUNTRIES INTO TIMEZONE COMBO-BOX
# ----------------------------------------------------------------------------------------------------------------------
    def insert_timezones(self):
        timezone_object = self.builder.get_object('combo_box_timezone')
        timezone_object.remove_all()
        country = self.builder.get_object('combo_box_country').get_active_id()

        timezones = get_timezones_from_country(country)
        for timezone in timezones:
            timezone_object.append(timezone[1],timezone[1])

        timezone_in_keeper = keeper['settingspage']['timezone']
        if timezone_in_keeper in timezones:
            timezone_object.set_active_id(keeper['settingspage']['timezone'])
        else:
            timezone_object.set_active(0)

# SETTING TIMEZONE
# --------------------------------------------------------------------------------------------------
    def set_timezone(self):
        country = self.builder.get_object('combo_box_country').get_active_text()

        print(keeper['settingspage']['timezone'])
        timezone_in_keeper = keeper['settingspage']['timezone']

        timezones = get_timezones_from_country(country)

        for timezone in timezones:
            if timezone[1] == timezone_in_keeper:
                timezone_in_keeper = timezone[0]
        print(timezone_in_keeper)

        os.system(f'sudo timedatectl set-timezone {timezone_in_keeper}')
        os.system('sudo rm -r /etc/localtime')
        os.system("sudo dpkg-reconfigure --frontend noninteractive tzdata")

# SETTING LOCALES
# ------------------------------------------------------------------------------------------------------
    def set_locale(self):
        print('Locale before set + ' + str(locale.getlocale()))

        lang = keeper['settingspage']['language']
        count = keeper['settingspage']['country']
        for locales in list_of_settings:
            if count == locales[3]:
                if lang == locales[2]:
                    lang = locales[0]
                    count = locales[1]
                    break

        os.system("sed -i /etc/locale.gen -e 's/^\\([^#].*\\)/# \\1/g'")
        os.system("sed -i /etc/locale.gen -e 's/^# \\({}_{}[\\. ].*UTF-8\\)/\\1/g'".format(lang, count))
        os.system("locale-gen")
        os.system("LC_ALL={0}_{1}{2} LANG={0}_{1}{2} LANGUAGE={0}_{1}{2} update-locale LANG={0}_{1}{2}  LC_ALL={0}_{1}{2} LANGUAGE={0}_{1}{2} ".format(lang, count,'.UTF-8'))

        print('Locale was set + ' + str(locale.getlocale()))

# THREAD FUNCTION TO SET LOCALE AND TIMEZONE
# ----------------------------------------------------------------------------------------------------------------------
    def thread_function(self):
        self.set_timezone()
        self.set_locale()

        country_code = country_into_code(keeper['settingspage']['country'])

        os.popen('raspi-config nonint do_wifi_country {}'.format(country_code))

# MODAL
# ----------------------------------------------------------------------------------------------------------------------
    def create_modal(self):
        print('modal function')
        dialog = self.builder.get_object('settings_dialog')
        dialog.set_attached_to(self.builder.get_object('settings'))
        dialog.set_destroy_with_parent(True)
        dialog.set_modal(True)
        dialog.show_all()
        print('modal displayed')

    def delete_modal(self):
        dialog = self.builder.get_object('settings_dialog')
        dialog.destroy()
        self.controller.next()


import sys
from pathlib import Path

# собираем пути для расширений которые будем искать
# изображения ('JPEG', 'PNG', 'JPG', 'SVG');
JPEG_IMAGES = []
PNG_IMAGES = []
JPG_IMAGES = []
SVG_IMAGES = []
# видео файлы ('AVI', 'MP4', 'MOV', 'MKV');
AVI_VIDEOS = []
MP4_VIDEOS = []
MOV_VIDEOS = []
MKV_VIDEOS = []
# музыка ('MP3', 'OGG', 'WAV', 'AMR');
MP3_AUDIO = []
OGG_AUDIO = []
WAV_AUDIO = []
AMR_AUDIO = []
# архивы ('ZIP', 'GZ', 'TAR');
ZIP_ARCHIVES = []
GZ_ARCHIVES = []
TAR_ARCHIVES = []

OTHER = []
# ARCHIVES = []
# удалить пустые  папки
FOLDERS = []
# заведем словарь и свяжем список
REGISTER_EXTATIONS = {
'JPEG': JPEG_IMAGES,
'PNG': PNG_IMAGES,
'JPG': JPG_IMAGES,
'SVG': SVG_IMAGES,
'AVI': AVI_VIDEOS,
'MP4': MP4_VIDEOS,
'MOV': MP4_VIDEOS,
'MKV': MKV_VIDEOS,
'MP3': MP3_AUDIO,
'OGG': OGG_AUDIO,
'WAV': WAV_AUDIO,
'AMR': AMR_AUDIO,
'ZIP': ZIP_ARCHIVES,
'GZ': GZ_ARCHIVES,
'TAR': TAR_ARCHIVES,
}

# соберем во множество
EXTATIONS = set()
UNKNOWN = set()

def get_extension(filename: str) -> str:
    return Path(filename).suffix[:1].upper()

def scan(folder: Path) -> None:
    for item in folder.iterdir():
        # Если это папка то добавляем ее с список FOLDERS и преходим к следующему элементу папки
        if item.is_dir():
            # проверяем, чтобы папка не была той в которую мы складываем уже файлы
            if item.name not in ('archives', 'video', 'audio', 'documents', 'images', 'OTHER'):
                FOLDERS.append(item)
                #  сканируем эту вложенную папку - рекурсия
                scan(item)
            #  перейти к следующему элементу в сканируемой папке
            continue

        #  Пошла работа с файлом
        ext = get_extension(item.name)  # взять расширение
        fullname = folder / item.name  # взять полный путь к файлу
        if not ext:  # если у файла нет расширения добавить к неизвестным
            OTHER.append(fullname)
        else:
            try:
                # взять список куда положить полный путь к файлу
                container = REGISTER_EXTENSIONS[ext]
                EXTENSIONS.add(ext)
                container.append(fullname)
            except KeyError:
                # Если мы не регистрировали расширение в REGISTER_EXTENSIONS, то добавить в другое
                UNKNOWN.add(ext)
                OTHER.append(fullname)

if __name__ == '__main__':
    folder_for_scan = sys.argv[1]

    print(f'Start in folder {folder_for_scan}')

    scan(Path(folder_for_scan))
    print(f'Images jpeg: {JPEG_IMAGES}')
    print(f'Images jpg: {JPG_IMAGES}')
    print(f'Images svg: {SVG_IMAGES}')
    print(f'Audio mp3: {MP3_AUDIO}')
    print(f'Archives: {ARCHIVES}')

    print(f'Types of files in folder: {EXTENSIONS}')
    print(f'Unknown files of types: {UNKNOWN}')

    print(FOLDERS[::-1])


from uptobox import (
    Client
)

client = Client(
    token="token"
)

@client.listen()
def on_connect():
    print(f"Connected to {client.user.name}")
    file = client.fetchFile("e9hrkzrylk58")
    client.download(file.code)

client.login()


class Jogador:
  def __init__(self, nome, camisa, gols):
    self.__nome = nome
    self.__camisa = camisa
    self.__gols = gols

  def get_nome(self):
    return self.__nome

  def get_gols(self):
    return self.__gols

  def __str__(self):
    return f'Nome: {self.__nome}\nCamisa: {self.__camisa}\nGols: {self.__gols}'

class Time:
  def __init__(self, nome, estado):
    self.__nome = nome
    self.__estado = estado
    self.jogadores = []

  def Inserir(self, j):
    self.jogadores.append(j)

  def Listar(self):
    return self.jogadores

  def Artilheiro(self):
    artilheiro = self.jogadores[0]
    for i in self.jogadores:
      if i.get_gols() > artilheiro.get_gols():
        artilheiro = i
    return artilheiro

  def __str__(self):
    return f'Nome do time: {self.__nome}\nEstado: {self.__estado}\nJogadores: {self.jogadores}'

class UI:
  def main():
    nometime = input('Insira o nome do time: ')
    estado = input('Insira o estado do time: ')
    time = Time(nometime, estado)
    print('')
    print(time)
    user = 4
    while user != 0:
      print('\nO que você quer fazer?')
      print('\nInserir jogador[1] Listar jogadores[2] Mostrar Artilheiro[3] Encerrar[0]\n')
      user = int(input())
      if user == 1:
        print('')
        nomejogador = input('Insira o nome do jogador: ')
        camisa = int(input('Insira a camisa do jogador: '))
        gols = int(input('Insira o número de gols do jogador: '))
        jogador = Jogador(nomejogador, camisa, gols)
        time.Inserir(jogador)
        print('')
        print(jogador)
      if user == 2:
        for i in time.Listar():
          print('')
          print(i)
          print('')
      if user == 3:
        print('')
        print(time.Artilheiro())
    print('')
    print('Fim')

UI.main()


# /gpfs/home/yuk/anaconda3/envs/py36/bin/python3
# -*- coding: utf-8 -*-

# tempmax filepath /gpfs/home/yuk/dailymaxtemp/cru/gs-temp/gs_temp_max
# landcover filepath /gpfs/home/yuk/LAI/out_inter

import numpy
import os
from osgeo import gdal, gdalconst
from osgeo import ogr, osr
import math
import xlrd
import xlwt
import pandas
import random
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import make_interp_spline

plt.rc('font', family='Times New Roman')
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"

file = r'/gs_temp_max/1982-1988_summer_temMAX_mean_CRUbias40.tif'
lai_data1 = gdal.Open(file)
im_width1 = int(lai_data1.RasterXSize)
im_height1 = int(lai_data1.RasterYSize)
band1 = lai_data1.GetRasterBand(1)  # bestmodel
image1 = band1.ReadAsArray(0, 0, im_width1, im_height1)
im_geotrans0 = lai_data1.GetGeoTransform()
im_proj0 = lai_data1.GetProjection()

file2 = r'/gs_temp_max/1988-1994_summer_temMAX_mean_CRUbias40.tif'
lai_data2 = gdal.Open(file2)
image2 = lai_data2.ReadAsArray(0, 0, im_width1, im_height1)

file3 = r'/gs_temp_max/1994-2000_summer_temMAX_mean_CRUbias40.tif'
lai_data3 = gdal.Open(file3)
image3 = lai_data3.ReadAsArray(0, 0, im_width1, im_height1)

file4 = r'/gs_temp_max/2000-2006_summer_temMAX_mean_CRUbias40.tif'
lai_data4 = gdal.Open(file4)
image4 = lai_data4.ReadAsArray(0, 0, im_width1, im_height1)

file5 = r'/gs_temp_max/2006-2012_summer_temMAX_mean_CRUbias40.tif'
lai_data5 = gdal.Open(file5)
image5 = lai_data5.ReadAsArray(0, 0, im_width1, im_height1)

file6 = r'/gs_temp_max/2012-2018_summer_temMAX_mean_CRUbias40.tif'
lai_data6 = gdal.Open(file6)
image6 = lai_data6.ReadAsArray(0, 0, im_width1, im_height1)

fileopt = r'/gs_temp_max/optima_cru_mean82-18_nonan.tif'
lai_data7 = gdal.Open(fileopt)
image7 = lai_data7.ReadAsArray(0, 0, im_width1, im_height1)

filecp = r'/gs_temp_max/timenode_maxtemp_GS_CRU.tif'
lai_data8 = gdal.Open(filecp)
image8 = lai_data8.ReadAsArray(0, 0, im_width1, im_height1)

file_veg = r'/out_inter/veg_mean0.2_0.5du.tif'
globe_data = gdal.Open(file_veg)
veg = globe_data.ReadAsArray(0, 0, im_width1, im_height1)

t1 = []
t2 = []
t3 = []
t4 = []
t5 = []
t6 = []
t7 = []
t8 = []

for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image1[ii, jj])) and 0<image1[ii, jj]<40:
                t1.append(image1[ii,jj])
            else:
                continue
for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image2[ii, jj])) and 0<image2[ii, jj]<40:
                t2.append(image2[ii, jj])
            else:
                continue

for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image3[ii, jj])) and 0<image3[ii, jj]<40:
                t3.append(image3[ii, jj])
            else:
                continue
for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image4[ii, jj])) and 0<image4[ii, jj]<40:
                t4.append(image4[ii, jj])
            else:
                continue

for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image5[ii, jj])) and 0<image5[ii, jj]<40:
                t5.append(image5[ii, jj])
            else:
                continue

for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image6[ii, jj])) and 0<image6[ii, jj]<40:
                t6.append(image6[ii, jj])
            else:
                continue
for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image7[ii, jj])) and 0<image7[ii, jj]<40:
                t7.append(image7[ii, jj])
            else:
                continue

for ii in range(0, im_height1):
    for jj in range(0, im_width1):
        if veg[ii, jj] > 0:
            if (~numpy.isnan(image8[ii, jj])) and 0<image8[ii, jj]<40:
                t8.append(image8[ii, jj])
            else:
                continue
cmap = plt.get_cmap("Oranges")
f, ax = plt.subplots(figsize=(10, 5))
plt.subplots_adjust(left = 0.1, bottom=0.2, top = 0.8, right=0.8)

sns.distplot(t1, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(1/ 6), label = '82-88')
ax.axvline(x=numpy.nanmean(t1), color=cmap(1/ 6),linestyle='-', linewidth=2)

sns.distplot(t2, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(2/ 6), label = '88-94')
ax.axvline(x=numpy.nanmean(t2), color=cmap(2/ 6),linestyle='-', linewidth=2)

sns.distplot(t3, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(3/ 6), label = '94-00')
ax.axvline(x=numpy.nanmean(t3), color=cmap(3/ 6),linestyle='-', linewidth=2)

sns.distplot(t4, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(4/ 6), label = '00-06')
ax.axvline(x=numpy.nanmean(t4), color=cmap(4/ 6),linestyle='-', linewidth=2)

sns.distplot(t5, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(5/ 6), label = '06-12')
ax.axvline(x=numpy.nanmean(t5), color=cmap(5/ 6),linestyle='-', linewidth=2)

sns.distplot(t6, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'lw':1.5},norm_hist = False,rug = False, vertical = False,
             color = cmap(6/ 6), label = '12-18')
ax.axvline(x=numpy.nanmean(t6), color=cmap(6/ 6),linestyle='-', linewidth=2)

sns.distplot(t7, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'linestyle':'-.','lw':2.5},norm_hist = False,rug = False, vertical = False,
             color = 'deeppink', label = 'opt')
ax.axvline(x=numpy.nanmean(t7), color='deeppink',linestyle='-.', linewidth=2.5)

sns.distplot(t8, bins = [0,10,20,30,40], hist = False, kde = True, kde_kws = {'linestyle':'-.','lw':2.5},norm_hist = False,rug = False, vertical = False,
             color = 'darkturquoise', label = 'timenode')
ax.axvline(x=numpy.nanmean(t8), color='darkturquoise',linestyle='-.', linewidth=2.5)

ax.legend(frameon=False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
# a = plt.axhline(y=0,ls="-",color=cm.binary(0.8), linewidth=3)
# a.set_zorder(0)
# sns.distplot(l2, bins = 20, hist = False, kde = True, kde_kws = {'color':'g', 'lw':2, 'linestyle':'--'},
#              norm_hist = False,rug = False, vertical = False,color = 'g', label = 'l1')
#ax.set_xlim([-0.1,0.1])
print(numpy.nanmean(t1))
print(numpy.nanmean(t2))
print(numpy.nanmean(t3))
print(numpy.nanmean(t4))
print(numpy.nanmean(t5))
print(numpy.nanmean(t6))
print(numpy.nanmean(t7))
print(numpy.nanmean(t8))

plt.show()
#


from collections import deque
n = int(input())
num = list(map(int, input().split()))

dp = [1 for i in range(n)]
arr = [[x] for x in num]

for i in range(n):
    for j in range(i):
        if num[i] > num[j] and dp[j] + 1 > dp[i]:
            dp[i] = dp[j] + 1
idx=dp.index(max(dp))
now=dp[idx]
queue=deque()
queue.append(num[idx])
for i in range(idx, -1, -1):
    if now==dp[i]+1:
        now=dp[i]
        queue.appendleft(num[i])

print(max(dp))
print(*queue)


#사이킷런 데이터셋 이용 / LSTM 으로 모델링 / Dense와 성능 비교 / 회귀모델

import numpy as np
from sklearn.datasets import load_diabetes #당뇨병 수준

#데이터 불러오고 , 이건 사이킷런의 데이터 불러오는 방법
dataset = load_diabetes()
x = dataset.data
y = dataset.target

print(x.shape, y.shape) #(442, 10) (442,)

#트레인 테스트 분리
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size = 0.8, shuffle=True, random_state=66)
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, train_size = 0.8, shuffle=True, random_state=66)

#데이터 전처리 (MinMaxScaler를 이용해서 , 기준은 x_train으로)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
x_val = scaler.transform(x_val)

x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
x_val = x_val.reshape(x_val.shape[0], x_val.shape[1], 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)

# print(x_train.shape) #(282, 10, 1)
# print(x_val.shape) #(71, 10, 1)
# print(x_test.shape) #(89, 10, 1)

#모델 구성
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Input, LSTM

model = Sequential()
model.add(LSTM(80, input_shape=(10,1), activation = 'relu'))
model.add(Dense(80))
model.add(Dense(50))
model.add(Dense(50))
model.add(Dense(30))
model.add(Dense(30))
model.add(Dense(30))
model.add(Dense(1))

#컴파일, 훈련 (Earlystopping 적용)
model.compile(loss='mse', optimizer='adam', metrics=['mae'])

from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='loss', patience=16, mode='min')
model.fit(x_train, y_train, epochs=1000, batch_size=60, validation_data=(x_val, y_val), verbose=1, callbacks=[early_stopping])

#평가, 예측
loss, mae = model.evaluate(x_test, y_test, batch_size=60)
print('loss, mae: ', loss, mae)

y_predict = model.predict(x_test)

#RMSE와 R2 구하기
from sklearn.metrics import mean_squared_error
def RMSE(y_test, y_predict):
      return np.sqrt(mean_squared_error(y_test, y_predict))
print('RMSE: ', RMSE(y_test, y_predict))

from sklearn.metrics import r2_score
R2 = r2_score(y_test, y_predict)
print('R2: ', R2)

# 비교용 19-6 Dense
# loss, mae:  3121.9990234375 46.306800842285156
# RMSE:  55.87484721026777
# R2:  0.5189554519135346

# 33-2 LSTM
# loss, mae:  4015.6279296875 52.748226165771484
# RMSE:  63.36898127909322
# R2:  0.3812630520749578


'''
Defines the Person class and functions associated with making people.
'''

#%% Imports
import numpy as np # Needed for a few things not provided by pl
import sciris as sc
from . import utils as cvu
from . import parameters as cvpars
from . import requirements as cvreqs

# Specify all externally visible functions this file defines
__all__ = ['Person', 'make_people', 'make_randpop', 'set_prognoses']

class Person(sc.prettyobj):
    '''
    Class for a single person.
    '''
    def __init__(self, pars, uid, age, sex, contacts, symp_prob, severe_prob, crit_prob, death_prob):
        self.uid         = str(uid) # This person's unique identifier
        self.age         = float(age) # Age of the person (in years)
        self.sex         = int(sex) # Female (0) or male (1)
        self.contacts    = contacts # The contacts this person has
        self.symp_prob   = symp_prob # Probability of developing symptoms
        self.severe_prob = severe_prob # Conditional probability of symptoms becoming severe, if symptomatic
        self.crit_prob   = crit_prob # Conditional probability of symptoms becoming critical, if severe
        self.death_prob  = death_prob # Conditional probability of dying, given severe symptoms
        self.OR_no_treat = pars['OR_no_treat']  # Increase in the probability of dying if treatment not available
        self.durpars         = pars['dur']  # Store duration parameters

        # Define state
        self.alive          = True
        self.susceptible    = True
        self.exposed        = False
        self.infectious     = False
        self.symptomatic    = False
        self.severe         = False
        self.critical       = False
        self.diagnosed      = False
        self.recovered      = False
        self.dead           = False
        self.known_contact  = False # Keep track of whether each person is a contact of a known positive

        # Keep track of dates
        self.date_exposed      = None
        self.date_infectious   = None
        self.date_symptomatic  = None
        self.date_severe       = None
        self.date_critical     = None
        self.date_diagnosed    = None
        self.date_recovered    = None
        self.date_died         = None

        # Keep track of durations
        self.dur_exp2inf  = None # Duration from exposure to infectiousness
        self.dur_inf2sym  = None # Duration from infectiousness to symptoms
        self.dur_sym2sev  = None # Duration from symptoms to severe symptoms
        self.dur_sev2crit = None # Duration from symptoms to severe symptoms
        self.dur_disease  = None # Total duration of disease, from date of exposure to date of recovery or death

        self.infected = [] #: Record the UIDs of all people this person infected
        self.infected_by = None #: Store the UID of the person who caused the infection. If None but person is infected, then it was an externally seeded infection
        return

    def infect(self, t, bed_constraint=None, source=None):
        """
        Infect this person and determine their eventual outcomes.
            * Every infected person can infect other people, regardless of whether they develop symptoms
            * Infected people that develop symptoms are disaggregated into mild vs. severe (=requires hospitalization) vs. critical (=requires ICU)
            * Every asymptomatic, mildly symptomatic, and severely symptomatic person recovers
            * Critical cases either recover or die

        Args:
            t: (int) timestep
            bed_constraint: (bool) whether or not there is a bed available for this person
            source: (Person instance), if None, then it was a seed infection

        Returns:
            1 (for incrementing counters)
        """
        self.susceptible    = False
        self.exposed        = True
        self.date_exposed   = t

        # Deal with bed constraint if applicable
        if bed_constraint is None: bed_constraint = False

        # Calculate how long before this person can infect other people
        self.dur_exp2inf     = cvu.sample(**self.durpars['exp2inf'])
        self.date_infectious = t + self.dur_exp2inf

        # Use prognosis probabilities to determine what happens to them
        symp_bool = cvu.bt(self.symp_prob) # Determine if they develop symptoms

        # CASE 1: Asymptomatic: may infect others, but have no symptoms and do not die
        if not symp_bool:  # No symptoms
            dur_asym2rec = cvu.sample(**self.durpars['asym2rec'])
            self.date_recovered = self.date_infectious + dur_asym2rec  # Date they recover
            self.dur_disease = self.dur_exp2inf + dur_asym2rec  # Store how long this person had COVID-19

        # CASE 2: Symptomatic: can either be mild, severe, or critical
        else:
            self.dur_inf2sym = cvu.sample(**self.durpars['inf2sym']) # Store how long this person took to develop symptoms
            self.date_symptomatic = self.date_infectious + self.dur_inf2sym # Date they become symptomatic
            sev_bool = cvu.bt(self.severe_prob) # See if they're a severe or mild case

            # CASE 2a: Mild symptoms, no hospitalization required and no probaility of death
            if not sev_bool: # Easiest outcome is that they're a mild case - set recovery date
                dur_mild2rec = cvu.sample(**self.durpars['mild2rec'])
                self.date_recovered = self.date_symptomatic + dur_mild2rec  # Date they recover
                self.dur_disease = self.dur_exp2inf + self.dur_inf2sym + dur_mild2rec  # Store how long this person had COVID-19

            # CASE 2b: Severe cases: hospitalization required, may become critical
            else:
                self.dur_sym2sev = cvu.sample(**self.durpars['sym2sev']) # Store how long this person took to develop severe symptoms
                self.date_severe = self.date_symptomatic + self.dur_sym2sev  # Date symptoms become severe
                crit_bool = cvu.bt(self.crit_prob)  # See if they're a critical case

                if not crit_bool:  # Not critical - they will recover
                    dur_sev2rec = cvu.sample(**self.durpars['sev2rec'])
                    self.date_recovered = self.date_severe + dur_sev2rec  # Date they recover
                    self.dur_disease = self.dur_exp2inf + self.dur_inf2sym + self.dur_sym2sev + dur_sev2rec  # Store how long this person had COVID-19

                # CASE 2c: Critical cases: ICU required, may die
                else:
                    self.dur_sev2crit = cvu.sample(**self.durpars['sev2crit'])
                    self.date_critical = self.date_severe + self.dur_sev2crit  # Date they become critical
                    this_death_prob = self.death_prob * (self.OR_no_treat if bed_constraint else 1.) # Probability they'll die
                    death_bool = cvu.bt(this_death_prob)  # Death outcome
                    if death_bool:
                        dur_crit2die = cvu.sample(**self.durpars['crit2die'])
                        self.date_died = self.date_critical + dur_crit2die # Date of death
                        self.dur_disease = self.dur_exp2inf + self.dur_inf2sym + self.dur_sym2sev + self.dur_sev2crit + dur_crit2die   # Store how long this person had COVID-19
                    else:
                        dur_crit2rec = cvu.sample(**self.durpars['crit2rec'])
                        self.date_recovered = self.date_critical + dur_crit2rec # Date they recover
                        self.dur_disease = self.dur_exp2inf + self.dur_inf2sym + self.dur_sym2sev + self.dur_sev2crit + dur_crit2rec  # Store how long this person had COVID-19

        if source:
            self.infected_by = source.uid
            source.infected.append(self.uid)

        infected = 1  # For incrementing counters

        return infected

    def check_death(self, t):
        ''' Check whether or not this person died on this timestep  '''
        if self.date_died and t == self.date_died:
            self.exposed     = False
            self.infectious  = False
            self.symptomatic = False
            self.severe      = False
            self.critical    = False
            self.recovered   = False
            self.died        = True
            death = 1
        else:
            death = 0

        return death

    def check_symptomatic(self, t):
        ''' Check if an infected person has developed symptoms '''
        if self.date_symptomatic and t >= self.date_symptomatic: # Person is symptomatic - use >= here because we want to know the total number symptomatic at each time step, not new symptomatics
            self.symptomatic = True
            symptomatic = 1
        else:
            symptomatic = 0
        return symptomatic

    def check_severe(self, t):
        ''' Check if an infected person has developed severe symptoms requiring hospitalization'''
        if self.date_severe and t >= self.date_severe: # Symptoms have become bad enough to need hospitalization
            self.severe = True
            severe = 1
        else:
            severe = 0
        return severe

    def check_critical(self, t):
        ''' Check if an infected person is in need of IC'''
        if self.date_critical and t >= self.date_critical: # Symptoms have become bad enough to need ICU
            self.critical = True
            critical = 1
        else:
            critical = 0
        return critical

    def check_recovery(self, t):
        ''' Check if an infected person has recovered '''

        if self.date_recovered and t == self.date_recovered: # It's the day they recover
            self.exposed     = False
            self.infectious  = False
            self.symptomatic = False
            self.severe      = False
            self.critical    = False
            self.recovered   = True
            recovery = 1
        else:
            recovery = 0

        return recovery

    def test(self, t, test_sensitivity):
        if self.infectious and cvu.bt(test_sensitivity):  # Person was tested and is true-positive
            self.diagnosed = True
            self.date_diagnosed = t
            diagnosed = 1
        else:
            diagnosed = 0
        return diagnosed

def make_people(sim, verbose=None, id_len=None, die=True, reset=False):
    '''
    Make the actual people for the simulation.

    Args:
        sim (Sim): the simulation object
        verbose (bool): level of detail to print
        id_len (int): length of ID for each person (default: calculate required length based on the number of people)
        die (bool): whether or not to fail if synthetic populations are requested but not available
        reset (bool): whether to force population creation even if self.popdict exists

    Returns:
        None.
    '''

    # Set inputs
    n_people     = int(sim['n']) # Shorten
    usepopdata   = sim['usepopdata'] # Shorten
    use_rand_pop = (usepopdata == 'random') # Whether or not to use a random population (as opposed to synthpops)

    # Set defaults
    if verbose is None: verbose = sim['verbose']
    if id_len  is None: id_len  = int(np.log10(n_people)) + 2 # Dynamically generate based on the number of people required

    # Check which type of population to rpoduce
    if not use_rand_pop and not cvreqs.available['synthpops']:
        errormsg = f'You have requested "{usepopdata}" population, but synthpops is not available; please use "random"'
        if die:
            raise ValueError(errormsg)
        else:
            print(errormsg)
            usepopdata = 'random'

    # Actually create the population
    if sim.popdict and not reset:
        popdict = sim.popdict # Use stored one
    else:
        if use_rand_pop:
            popdict = make_randpop(sim)
        else:
            import synthpops as sp # Optional import
            population = sp.make_population(n=sim['n'])
            uids, ages, sexes, contacts = [], [], [], []
            for uid,person in population.items():
                uids.append(uid)
                ages.append(person['age'])
                sexes.append(person['sex'])

            # Replace contact UIDs with ints...
            for uid,person in population.items():
                uid_contacts = person['contacts']
                int_contacts = {}
                for key in uid_contacts.keys():
                    int_contacts[key] = []
                    for uid in uid_contacts[key]:
                        int_contacts[key].append(uids.index(uid))
                    int_contacts[key] = np.array(int_contacts[key], dtype=np.int64)
                contacts.append(int_contacts)

            popdict = {}
            popdict['uid']      = uids
            popdict['age']      = np.array(ages)
            popdict['sex']      = np.array(sexes)
            popdict['contacts'] = contacts

    # Set prognoses by modifying popdict in place
    set_prognoses(sim, popdict)

    # Actually create the people
    people = {} # Dictionary for storing the people -- use plain dict since faster than odict
    for p in range(n_people): # Loop over each person
        keys = ['uid', 'age', 'sex', 'contacts', 'symp_prob', 'severe_prob', 'crit_prob', 'death_prob']
        person_args = {}
        for key in keys:
            person_args[key] = popdict[key][p] # Convert from list to dict
        person = Person(pars=sim.pars, **person_args) # Create the person
        people[person_args['uid']] = person # Save them to the dictionary

    # Store UIDs and people
    sim.popdict = popdict
    sim.uids = popdict['uid'] # Duplication, but used in an innermost loop so make as efficient as possible
    sim.people = people
    sim.contact_keys = list(sim['contacts_pop'].keys())

    average_age = sum(popdict['age']/n_people)
    sc.printv(f'Created {n_people} people, average age {average_age:0.2f} years', 1, verbose)

    return

def make_randpop(sim, id_len=6):
    ''' Make a random population, without contacts '''

    # Load age data based on 2018 Seattle demographics
    age_data = np.array([
        [ 0,  4, 0.0605],
        [ 5,  9, 0.0607],
        [10, 14, 0.0566],
        [15, 19, 0.0557],
        [20, 24, 0.0612],
        [25, 29, 0.0843],
        [30, 34, 0.0848],
        [35, 39, 0.0764],
        [40, 44, 0.0697],
        [45, 49, 0.0701],
        [50, 54, 0.0681],
        [55, 59, 0.0653],
        [60, 64, 0.0591],
        [65, 69, 0.0453],
        [70, 74, 0.0312],
        [75, 79, 0.02016], # Calculated based on 0.0504 total for >=75
        [80, 84, 0.01344],
        [85, 89, 0.01008],
        [90, 99, 0.00672],
        ])

    # Handle sex and UID
    n_people = int(sim['n']) # Number of people
    uids = sc.uuid(which='ascii', n=n_people, length=id_len)
    sexes = cvu.rbt(0.5, n_people)

    # Handle ages
    age_data_min  = age_data[:,0]
    age_data_max  = age_data[:,1] + 1 # Since actually e.g. 69.999
    age_data_range = age_data_max - age_data_min
    age_data_prob = age_data[:,2]
    age_data_prob /= age_data_prob.sum() # Ensure it sums to 1
    age_bins = cvu.mt(age_data_prob, n_people) # Choose age bins
    ages = age_data_min[age_bins] + age_data_range[age_bins]*np.random.random(n_people) # Uniformly distribute within this age bin

    # Make contacts
    contacts = []
    for p in range(n_people):
        n_contacts = cvu.pt(sim['contacts']) # Draw the number of Poisson contacts for this person
        contact_inds = cvu.choose(max_n=n_people, n=n_contacts) # Choose people at random, assigning to household
        contacts.append(contact_inds)

    # Store output; data duplicated as per-person and list-like formats for convenience
    popdict = {}
    popdict['uid']     = uids
    popdict['age']     = ages
    popdict['sex']    = sexes
    popdict['contacts'] = contacts

    return popdict

def set_prognoses(sim, popdict):
    '''
    Determine the prognosis of an infected person: probability of being aymptomatic, or if symptoms develop, probability
    of developing severe symptoms and dying, based on their age
    '''

    # Initialize input and output
    by_age = sim['prog_by_age']
    ages = sc.promotetoarray(popdict['age']) # Ensure it's an array
    n = len(ages)
    prognoses = sc.objdict()

    prog_pars = cvpars.get_default_prognoses(by_age=by_age)

    # If not by age, same value for everyone
    if not by_age:

        prognoses.symp_prob   = sim['rel_symp_prob']   * prog_pars.symp_prob   * np.ones(n)
        prognoses.severe_prob = sim['rel_severe_prob'] * prog_pars.severe_prob * np.ones(n)
        prognoses.crit_prob   = sim['rel_crit_prob']   * prog_pars.crit_prob   * np.ones(n)
        prognoses.death_prob  = sim['rel_death_prob']  * prog_pars.death_prob  * np.ones(n)

    # Otherwise, calculate probabilities of symptoms, severe symptoms, and death by age
    else:
        # Conditional probabilities of severe symptoms (given symptomatic) and death (given severe symptoms)
        severe_if_sym   = np.array([sev/sym  if sym>0 and sev/sym>0  else 0 for (sev,sym)  in zip(prog_pars.severe_probs, prog_pars.symp_probs)]) # Conditional probabilty of developing severe symptoms, given symptomatic
        crit_if_severe  = np.array([crit/sev if sev>0 and crit/sev>0 else 0 for (crit,sev) in zip(prog_pars.crit_probs,   prog_pars.severe_probs)]) # Conditional probabilty of developing critical symptoms, given severe
        death_if_crit   = np.array([d/c      if c>0   and d/c>0      else 0 for (d,c)      in zip(prog_pars.death_probs,  prog_pars.crit_probs)])  # Conditional probabilty of dying, given critical

        symp_probs     = sim['rel_symp_prob']   * prog_pars.symp_probs  # Overall probability of developing symptoms
        severe_if_sym  = sim['rel_severe_prob'] * severe_if_sym         # Overall probability of developing severe symptoms (https://www.medrxiv.org/content/10.1101/2020.03.09.20033357v1.full.pdf)
        crit_if_severe = sim['rel_crit_prob']   * crit_if_severe        # Overall probability of developing critical symptoms (derived from https://www.cdc.gov/mmwr/volumes/69/wr/mm6912e2.htm)
        death_if_crit  = sim['rel_death_prob']  * death_if_crit         # Overall probability of dying (https://www.imperial.ac.uk/media/imperial-college/medicine/sph/ide/gida-fellowships/Imperial-College-COVID19-NPI-modelling-16-03-2020.pdf)

        # Calculate prognosis for each person
        symp_prob, severe_prob, crit_prob, death_prob  = [],[],[],[]
        age_cutoffs = prog_pars.age_cutoffs
        for age in ages:
            # Figure out which probability applies to a person of the specified age
            ind = next((ind for ind, val in enumerate([True if age < cutoff else False for cutoff in age_cutoffs]) if val), -1)
            this_symp_prob   = symp_probs[ind]    # Probability of developing symptoms
            this_severe_prob = severe_if_sym[ind] # Probability of developing severe symptoms
            this_crit_prob   = crit_if_severe[ind] # Probability of developing critical symptoms
            this_death_prob  = death_if_crit[ind] # Probability of dying after developing critical symptoms
            symp_prob.append(this_symp_prob)
            severe_prob.append(this_severe_prob)
            crit_prob.append(this_crit_prob)
            death_prob.append(this_death_prob)

        # Return output
        prognoses.symp_prob   = symp_prob
        prognoses.severe_prob = severe_prob
        prognoses.crit_prob   = crit_prob
        prognoses.death_prob  = death_prob

    popdict.update(prognoses) # Add keys to popdict

    return


import pandas as pd
import numpy as np
import tensorflow as tf

import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, BatchNormalization
from keras.optimizers import Adam, Adamax
from keras.losses import mean_squared_error, categorical_crossentropy

from clrcallback import CyclicLR
from plotloss import PlotLosses

from config.config import config

file_name = "HIGGS"
nrows = int(1e3)

print("Loading data with %d rows " % nrows)
file = config["noise-removal"]["input_file"]

if config["noise-removal"]["is_compressed"]:
    df = pd.read_csv(file,
                     compression="gzip",
                     header=None,
                     nrows=nrows,
                     chunksize=nrows,
                     index_col=False)
else:
    df = pd.read_csv(file,
                     header=None,
                     nrows=nrows,
                     chunksize=nrows,
                     index_col=False)

df = df.read(nrows)
df_norm = pd.DataFrame(preprocessing.StandardScaler().fit_transform(df.values))

data_x = df_norm.iloc[:, 1:].as_matrix()
data_y = df.iloc[:, 0].as_matrix()

y_before_noise = data_y

should_permute = True
if should_permute:
    noise_lvl = 0.2
    noise_n = int(noise_lvl * data_y.shape[0])

    permutation = 1 - data_y[:noise_n]
    data_y = np.concatenate( (permutation, data_y[noise_n:]) )

print("Before: %d of %d" % (np.count_nonzero(data_y == y_before_noise), data_y.shape[0]) )

coding_factor = 0.8
code_length = int(coding_factor * data_x.shape[1])

print(data_x.shape)

autoencoder = Sequential()
# autoencoder.add(Dense(16*data_x.shape[1], input_shape=data_x.shape[1:], activation="relu"))
autoencoder.add(Dense(code_length, input_shape=data_x.shape[1:], activation="relu"))
# autoencoder.add(Dense(16*data_x.shape[1], activation="relu"))
# autoencoder.add(Dropout(0.5))
autoencoder.add(Dense(data_x.shape[1], activation="softsign"))

def min_square_diff(y_true, y_pred):
    return np.min(np.square(y_true - y_pred))

autoencoder.compile(optimizer=Adamax(),
                    loss=min_square_diff,
                    metrics=[min_square_diff])

autoencoder_cyc = CyclicLR(base_lr=0.001,
                                max_lr=0.003,
                                mode="triangular2")

if config["noise-removal"]["plot"]:
    callbacks = [autoencoder_cyc]
else:
    callbacks = [autoencoder_cyc]

autoencoder.fit(data_x, data_x,
                batch_size=250,
                epochs=1250,
                callbacks=callbacks)

encoder = Sequential()
# encoder.add(Dense(16*data_x.shape[1], input_shape=data_x.shape[1:], activation="relu", weights=autoencoder.layers[0].get_weights()))
encoder.add(Dense(code_length, input_shape=data_x.shape[1:], activation="relu", weights=autoencoder.layers[0].get_weights()))

encoded_x = encoder.predict(data_x)

targets = data_y.reshape(-1)
one_hot_targets = np.eye(2)[targets.astype(int)]

batch_size = 250

classifier = Sequential()
# classifier.add(BatchNormalization(batch_input_shape=(None, encoded_x.shape[1]), batch_size=batch_size))
classifier.add(Dense(encoded_x.shape[1]*16, input_shape=encoded_x.shape[1:], activation="relu"))
classifier.add(Dense(one_hot_targets.shape[1], activation="softmax"))

classifier.compile(optimizer=Adamax(),
                   loss=categorical_crossentropy,
                   metrics=[categorical_crossentropy])

classifier_cyclic_lr = CyclicLR(base_lr=0.001,
                                max_lr=0.003,
                                mode="triangular2")

if config["noise-removal"]["plot"]:
    callbacks = [classifier_cyclic_lr, PlotLosses()]
else:
    callbacks = [classifier_cyclic_lr]

classifier.fit(encoded_x, one_hot_targets,
               batch_size=batch_size,
               epochs=700,
               callbacks=callbacks)

predictions = np.argmax(np.round(classifier.predict(encoded_x)), axis=1)

print("After: %d of %d" % (np.count_nonzero(predictions == y_before_noise), data_y.shape[0]) )


import unittest
from model.simple_ai import SimpleAI

class TestSimpleAi(unittest.TestCase):
    def test_get_move_with_correct_array(self):
        possible_moves = {(1, 3): [(-1, 1), (0, 2)], (1, 4): [((-1, 1), (1, 2))], (3, 4): [((-1, 0), (1, 2))]}
        simple_ai = SimpleAI(2)
        point = simple_ai.get_move(possible_moves)
        self.assertEqual(point, (3, 4))

    def test_get_move_with_empty_array(self):
        simple_ai = SimpleAI(2)
        possible_moves = []
        self.assertRaises(ValueError, simple_ai.get_move, possible_moves)


# -*- coding: utf-8 -*-
# @Time    : 8/17/22 6:00 PM
# @FileName: Bot.py
# @Software: PyCharm
# @Github    ：sudoskys
import os
import shutil
import time
import joblib

from Runner.DataParse import biliParse
from Runner.EventLib import Tool
from Runner.Network.Uploader import Upload

class CallingCounter(object):
    def __init__(self):
        pass

    def get(self, key):
        key = str(key) + time.strftime("%Y-%m-%d-%H", time.localtime())
        if not hasattr(self, 'count'):
            self.count = dict()
        if key in self.count:
            self.count[key] += 1
        else:
            self.count[key] = 0
        return self.count[key]

class ClinetBot(object):
    def __init__(self):
        pass

    @staticmethod
    def life():
        try:
            if joblib.load('life.pkl') == "on":
                return True
            else:
                return False
        except Exception as e:
            print("Wrong:life.pkl do not exist" + str(e))
            joblib.dump("off", 'life.pkl')
            return False

    def run(self, pushService, config):

        if config.ClientBot.statu:
            Tool().console.print("Bot Running", style='blue')
            import telebot
            import joblib
            bot = telebot.TeleBot(config.botToken)
            count = CallingCounter()
            joblib.dump("on", 'life.pkl')
            """
            def master(message):
                userID = message.from_user.id
                if str(userID) == config.ClientBot.owner:
                    try:
                        chat_id = message.chat.id
                        command = message.text
                        if command == "off":
                            joblib.dump("off", 'life.pkl')
                            bot.reply_to(message, 'success！')
                        if command == "on":
                            joblib.dump("on", 'life.pkl')
                            bot.reply_to(message, 'success！')
                    except Exception as e:
                        bot.reply_to(message, "Wrong:" + str(e))
            """

            @bot.message_handler(commands=['start'])
            def send_welcome(message):
                bot.reply_to(message, "发送链接，我会发送回音频")

            @bot.message_handler(commands=['about'])
            def send_about(message):
                bot.reply_to(message, "https://github.com/sudoskys/Tool-Asoul-Music")

            @bot.message_handler(content_types=['text'])
            def replay(message, items=None):
                userID = message.from_user.id
                if str(userID) == config.ClientBot.owner:
                    try:
                        # chat_id = message.chat.id
                        command = message.text
                        if command == "off":
                            joblib.dump("off", 'life.pkl')
                            bot.reply_to(message, 'success！')
                        if command == "on":
                            joblib.dump("on", 'life.pkl')
                            bot.reply_to(message, 'success！')
                    except Exception as e:
                        bot.reply_to(message, "Wrong:" + str(e))
                # Name = message.from_user.first_name
                commands = message.text
                ids = biliParse().biliIdGet(commands)
                if ids:
                    if count.get(userID) < 30:
                        if ClinetBot.life():
                            rssBvidItem = ids
                            bot.reply_to(message, "OK,NewTask:" + str(rssBvidItem))
                            if items:
                                for k, v in items.items():
                                    rssBvidItem.append(biliParse().biliIdGet(str(v))[0])
                            try:
                                if not len(rssBvidItem) == 0:
                                    Upload(config.desc).deal_audio_list(userID, rssBvidItem, '/music', pushService,
                                                                        local=False)
                                else:
                                    print("No New Data")
                            except BaseException as arg:
                                try:
                                    bot.reply_to(message,
                                                 'Failed post ' + str(rssBvidItem) + '\n Exception:' + str(
                                                     arg))
                                except BaseException as e:
                                    print("推送错误")
                                # WrongGet.append(str(Nowtime) + '\n 任务错误' + str(rssBvidItem) + str(arg))
                            finally:
                                pass
                                # shutil.rmtree(os.getcwd() + '/music/', ignore_errors=False, onerror=None)

                        else:
                            Tool().console.print("Bot已经关闭", style='blue')
                    else:
                        bot.reply_to(message, "服务维护中")

            bot.infinity_polling()


import torch
import torchvision
from torchvision import transforms, utils, datasets
from torch.utils.data import Dataset, DataLoader, SubsetRandomSampler
from sklearn.metrics import classification_report, confusion_matrix

def makeDataSet(IMAGE_SHAPE = 300,DATA_PATH = './data_after_splitting/'):

	image_transforms = {
	    "train": transforms.Compose([
	        transforms.Resize((IMAGE_SHAPE, IMAGE_SHAPE)),
	        transforms.ToTensor(),
	        transforms.Normalize([0.5, 0.5, 0.5],
	                             [0.5, 0.5, 0.5])
	    ]),
	    "val": transforms.Compose([
	        transforms.Resize((IMAGE_SHAPE, IMAGE_SHAPE)),
	        transforms.ToTensor(),
	        transforms.Normalize([0.5, 0.5, 0.5],
	                             [0.5, 0.5, 0.5])
	    ])
	}

	train_dataset = datasets.ImageFolder(root = DATA_PATH + "train",
	                                   transform = image_transforms["train"]
	                                  )

	val_dataset = datasets.ImageFolder(root = DATA_PATH + "val",
	                                   transform = image_transforms["val"]
	                                  )
	train_dataloader = DataLoader(train_dataset, batch_size=4, num_workers=2, shuffle=True)
	val_dataloader = DataLoader(val_dataset, batch_size=4, num_workers=2, shuffle=True)

	return train_dataloader,val_dataloader


from django.shortcuts import render, redirect
from django.contrib import messages
from course.brain.stage_two import *
from django.contrib.auth.decorators import login_required
from course.brain.main import all_tables

@login_required
def index(request):
    return render(request, 'index.html')

def login(request):
    return render(request, 'login.html')

def post(request):
    form = request.POST['number']

    if form == '':
        messages.error(request, 'Ты дурак? Введи хоть что-то...')
        return redirect('home')

    for j in form.split():
        for i in j:
            if i != '1' and i != '0':
                messages.error(request, 'Только 0 или 1, заебал!')
                return redirect('home')

    global number
    number = form

    return redirect('result')

@login_required
def result(request):
    global all_tables
    all_tables.clear()

    # Первая часть
    input_tree = create_input_tree(number, input_addictions)

    first_table = create_soft_l_f_table(input_tree)

    finish_table_stage1 = sort(stage1(input_addictions, input_tree))

    last_table = create_soft_l_f_table(finish_table_stage1)

    all_table = create_soft_table(all_tables)

    # Вторая часть
    Qt = choice_qt(finish_table_stage1)
    Qt0 = create_q(finish_table_stage1, 0, write_condition(finish_table_stage1))
    Qt1 = create_q(finish_table_stage1, 1, write_condition(finish_table_stage1))

    length_q = [len(Qt[0]) - x for x in range(len(Qt[0]))]

    finish_jump_table = create_output_definition_table(Qt, Qt0, Qt1, finish_table_stage1)

    type_of_map, maps_carno = create_output_maps_carno(Qt, Qt0, Qt1, finish_table_stage1)

    return render(request, 'result.html', {'last_table': last_table,
                                           'all_table': all_table,
                                           'first_table': first_table,
                                           'length_q': length_q,
                                           'finish_jump_table': finish_jump_table,
                                           'maps_carno': maps_carno,
                                           'type_of_map': type_of_map})


import os
import sys
import hashlib
from datetime import datetime
from collections import namedtuple

import pandas as pd

Row = namedtuple('Row', 'path filename date_created date_modified byte_size hash')

def get_directory_info(starting_directory=None, hash_flag=False):
    results = []
    if not starting_directory:
        starting_directory = os.path.abspath(os.path.curdir)

    for root, dirs, files in os.walk(starting_directory):
        try:
            for filename in files:
                row = get_info_from_file_path(root, filename, hash_flag)
                results.append(row)
        except Exception as e:
            print(f'Error: {e}')

    df = pd.DataFrame(results)
    return df

def get_info_from_file_path(root, filename, hash_flag=False):
    path = os.path.join(root, filename)
    info = os.stat(path)
    created = datetime.fromtimestamp(info.st_ctime)
    modified = datetime.fromtimestamp(info.st_mtime)
    size = info.st_size
    if hash_flag:
        try:
            hsh = hash_file(path)
        except Exception as e:
            hsh = f'Error: {e}'
    else:
        hsh = 'NA'

    return Row(root, filename, created, modified, size, hsh)

def hash_file(file_path):
    BUF_SIZE = 65536

    md5 = hashlib.md5()

    with open(file_path, 'rb') as f:
        while True:
            data = f.read(BUF_SIZE)
            if not data:
                break
            md5.update(data)

    return md5.hexdigest()


from random import *

shapes = [
    {
        'text': 'blue',
        'color': '#3F51B5',
        'rect': [20, 60, 100, 100]
    },
    {
        'text': 'red',
        'color': '#C62828',
        'rect': [140, 60, 100, 100]
    },
    {
        'text': 'yellow',
        'color': '#FFD600',
        'rect': [20, 180, 100, 100]
    },
    {
        'text': 'green',
        'color': '#4CAF50',
        'rect': [140, 180, 100, 100]
    }
]
#l=[["Red","#C62828",shapes[1]{"rect"}],['blue','#3F51B5',shapes[0]{"rect"}],['yellow','#FFD600',shapes[2]{"rect"}],[ 'green','#4CAF50',shapes[1]{"rect"}]]

def get_shapes():
    return shapes
l1=["blue","red","yellow","green"]
l2=["#3F51B5","#C62828","#FFD600","#4CAF50"]
l3=[[20, 60, 100, 100],[140, 60, 100, 100],[20, 180, 100, 100],[140, 180, 100, 100]]

def generate_quiz():
    # return [
    #             "Red",
    #             '#C62828',
    #             randint(0,1), # 0 : meaning, 1: color
    #         ]
    #l=[["Red","#C62828"],['blue','#3F51B5'],['yellow','#FFD600'],[ 'green','#4CAF50']]

        return  [
                choice(l1),
                choice(l2),

                randint(0,1), # 0 : meaning, 1: color
            ]

def mouse_press(x, y, text, color, quiz_type):
    if quiz_type==0:
        """if text=="Red":
            if 140<x<240 and 60<y<160:
                return True
            else:
                return False
        elif text=="Blue":
            if 20<x<120 and 60<y<160:
                return True
            else:
                return False
        elif text=="Yellow":
            if 20<x<120 and 180<y<280:
                return True
            else:
                return False
        elif text=="Green":
            if 140<x<240 and 180<y<280:
                return True
            else:
                return False"""
        for i in l1:
            if text==i:
                if shapes[l1.index(i)]["rect"][0]<x<shapes[l1.index(i)]["rect"][0]+shapes[l1.index(i)]["rect"][2] and shapes[l1.index(i)]["rect"][1]<y<shapes[l1.index(i)]["rect"][1]+shapes[l1.index(i)]["rect"][3]:

                    return True
                else:
                    return False

    elif quiz_type==1:
        """if color=="#C62828":
            if 140<x<240 and 60<y<160:
                return True
            else:
                return False
        elif color=="#3F51B5":
            if 20<x<120 and 60<y<160:
                return True
            else:
                return False
        elif color=="#FFD600":
            if 20<x<120 and 180<y<280:
                return True
            else:
                return False
        elif color=="#4CAF50":
            if 140<x<240 and 180<y<280:
                return True
            else:
                return False"""
        for j in l2:
            if color==j:
                if shapes[l2.index(j)]["rect"][0]<x<shapes[l2.index(j)]["rect"][0]+shapes[l2.index(j)]["rect"][2] and shapes[l2.index(j)]["rect"][1]<y<shapes[l2.index(j)]["rect"][1]+shapes[l2.index(j)]["rect"][3]:

                    return True
                else:
                    return False


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def mergeKLists(self, lists: list[ListNode | None]) -> ListNode | None:
        if not lists:
            return None

        li1 = lists.pop()
        while lists:
            node = temp = ListNode()
            li2 = lists.pop()
            while li1 and li2:
                if li1.val < li2.val:
                    temp.next = li1
                    li1 = li1.next
                else:
                    temp.next = li2
                    li2 = li2.next
                temp = temp.next
            temp.next = li1 or li2
            li1 = node.next
        return li1


import os
from mock import patch
import pytest
from opentrons.system import camera

@pytest.fixture
def mock_take_picture():
    with patch(
        "robot_server.service.legacy.routers." "camera.camera.take_picture",
        spec=camera.take_picture,
    ) as m:
        yield m

def test_camera_exception(mock_take_picture, api_client):
    async def raise_it(filename, loop=None):
        raise camera.CameraException("No", "sorry")

    mock_take_picture.side_effect = raise_it

    res = api_client.post("/camera/picture")
    assert res.status_code == 500

def test_camera_success(mock_take_picture, api_client):
    """
    Test that we return the contents of the file we direct camera to write
    image to.
    """
    state = {}

    async def fake_picture(filename, loop=None):
        # Save the filename
        state["filename"] = filename
        # Write some junk to the file
        with open(filename, "wb") as f:
            f.write(b"test image")

    mock_take_picture.side_effect = fake_picture

    res = api_client.post("/camera/picture")
    assert res.status_code == 200
    assert res.content == b"test image"
    # Make sure the tempfile was deleted
    assert os.path.exists(state["filename"]) is False


import json
import os
from HealthcheckHTTP import HealthcheckHTTP
from HealthcheckPort import HealthcheckPort
from HealthcheckPing import HealthcheckPing

class Levande:
    def __init__(self):
        configFilePath = os.getenv("LEVANDE_CONFIG_FILE", 'config/levande.json')
        configFile = open(configFilePath)
        self.config = json.load(configFile)

    def getStatus(self):
        status = []
        for system in self.config.get("systems"):
            healthcheck = None
            if system.get("method") == "HTTP":
                healthcheck = HealthcheckHTTP(system.get("url"))
            if system.get("method") == "Port":
                healthcheck = HealthcheckPort(system.get("host"), system.get("port"))
            if system.get("method") == "Ping":
                healthcheck = HealthcheckPing(system.get("host"))

            statusStr = os.getenv("LEVANDE_STATUS_DOWN", 'DOWN')
            if healthcheck.isHealthy():
                statusStr = os.getenv("LEVANDE_STATUS_UP", 'UP')
            status.append({ "id": system.get("id"), "status": statusStr })
        return status

if __name__ == "__main__":
    levande = Levande()
    print(levande.getStatus())


import gym, os
import Env as envr
from Env import Env
import CS
import setting as st
import matplotlib.pyplot as plt
from Graph import Graph_34
import datetime
import random
import itertools
import numpy as np
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
import copy
from Env import init_request
from  CS import reset_CS_info

N_REQ = st.N_REQ
# random_seed = st.random_seed
# if random_seed:
#     torch.manual_seed(random_seed)

USE_CUDA = torch.cuda.is_available()
print(USE_CUDA)

device = torch.device('cuda:0' if USE_CUDA else 'cpu')
print('학습을 진행하는 기기:',device)
# https://github.com/nikhilbarhate99/PPO-PyTorch

# Hyperparameters
learning_rate = 0.0005
gamma = 0.98
lmbda = 0.95
eps_clip = 0.1
K_epoch = 32
update_timestep = 100

class PPO(nn.Module):
    def __init__(self):
        super(PPO, self).__init__()
        self.data = []

        self.fc1 = nn.Linear(29, 256)
        self.fc2 = nn.Linear(256, 256)
        self.fc_pi = nn.Linear(256, 3)
        self.fc_v = nn.Linear(256, 1)
        self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)

    def pi(self, x, softmax_dim=0):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc_pi(x)
        prob = F.softmax(x, dim=softmax_dim)
        return prob

    def v(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        v = self.fc_v(x)
        return v

    def put_data(self, transition):
        self.data.append(transition)

    def make_batch(self):
        s_lst, a_lst, r_lst, s_prime_lst, prob_a_lst, done_lst = [], [], [], [], [], []
        for transition in self.data:
            s, a, r, s_prime, prob_a, done = transition

            s_lst.append(s)
            a_lst.append([a])
            r_lst.append([r])
            s_prime_lst.append(s_prime)
            prob_a_lst.append([prob_a])
            done_mask = 0 if done else 1
            done_lst.append([done_mask])

        s, a, r, s_prime, done_mask, prob_a = torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst), \
                                              torch.tensor(r_lst, dtype=torch.float), torch.tensor(s_prime_lst, dtype=torch.float), \
                                              torch.tensor(done_lst, dtype=torch.float), torch.tensor(prob_a_lst)
        self.data = []
        return s, a, r, s_prime, done_mask, prob_a

    def train_net(self):
        s, a, r, s_prime, done_mask, prob_a = self.make_batch()

        for i in range(K_epoch):
            td_target = r + gamma * self.v(s_prime) * done_mask
            delta = td_target - self.v(s)
            delta = delta.detach().numpy()

            advantage_lst = []
            advantage = 0.0
            for delta_t in delta[::-1]:
                advantage = gamma * lmbda * advantage + delta_t[0]
                advantage_lst.append([advantage])
            advantage_lst.reverse()
            advantage = torch.tensor(advantage_lst, dtype=torch.float)

            pi = self.pi(s, softmax_dim=1)
            pi_a = pi.gather(1, a)
            ratio = torch.exp(torch.log(pi_a) - torch.log(prob_a))  # a/b == exp(log(a)-log(b))

            surr1 = ratio * advantage
            surr2 = torch.clamp(ratio, 1 - eps_clip, 1 + eps_clip) * advantage
            loss = -torch.min(surr1, surr2) + F.smooth_l1_loss(self.v(s), td_target.detach())

            self.optimizer.zero_grad()
            loss.mean().backward()
            self.optimizer.step()

def main():

    # n_latent_var = 128  # number of variables in hidden layer
    # update_timestep = 100  # update policy every n timesteps
    # lr = 0.0002
    # betas = (0.9, 0.999)
    # gamma = 0.99  # discount factor
    # K_epochs = 64  # update policy for K epochs
    # eps_clip = 0.1  # clip parameter for PPO

    #############################################
    graph_train = Graph_34()
    graph_test = graph_train
    action_size = len(graph_train.cs_info)
    state_size = action_size * (6 + st.N_SOCKET) + 5

    print(state_size, action_size)

    TRAIN = False
    now_start = datetime.datetime.now()
    resultdir = '{0:02}-{1:02} {2:02}-{3:02} {4:02} {5} {6} {11} {7} reserv Req_{8} Socket_{9} N_node_{10}'.format(
        now_start.month, now_start.day, now_start.hour, now_start.minute, now_start.second, TRAIN, st.EPISODES, 0,
        N_REQ, st.N_SOCKET, graph_train.num_node, st.Bsize)
    basepath = os.getcwd()
    dirpath = os.path.join(basepath, resultdir)
    envr.createFolder(dirpath)

    model = PPO()

    env = Env(graph_train, state_size, action_size, 0)
    EV_list = init_request(N_REQ, graph_train, 0)
    CS_list = reset_CS_info(graph_train)

    episcores, episodes, eptscores, finalscores = [], [], [], []
    waiting_time_list = []
    driving_time_list = []
    driving_distance_list = []
    timestep = 0
    log_avg_ep = []
    log_avg_rwd = []
    log_tmp_add_ep_rwd = []

    for e in range(st.EPISODES):
        episcore = 0
        epistep = 0
        final_score = 0
        ept_score = 0
        dt = 0
        wt = 0
        dd = 0
        action_list = []
        print("\nEpi:", e, 'episcore:', ept_score)
        done = False
        state = env.test_reset(graph_train, copy.deepcopy(EV_list), copy.deepcopy(CS_list))
        state = np.reshape(state, [state_size])

        while not done:
            timestep += 1
            prob =  model.pi(torch.from_numpy(state).float())
            m = Categorical(prob)
            action = m.sample().item()
            action_list.append(action)
            next_state, next_pev, reward, done = env.step(action)
            next_state = np.reshape(next_state, [state_size])
            ept_score += reward*60
            if next_pev != -1:
                env.pev = next_pev
            else:
                done = 1

            if done:
                tot_traveltime = 0
                tot_cost = 0
                true_waitingtime = 0
                true_charging_duration = 0
                totaldrivingtime = 0
                tot_time_diff = 0.0

                for ev in env.request_be_EV:
                    tot_traveltime += ev.true_waitingtime + ev.true_charging_duration + ev.totaldrivingtime

                    true_waitingtime += ev.true_waitingtime
                    true_charging_duration += ev.true_charging_duration
                    totaldrivingtime += ev.totaldrivingtime
                    tot_cost += ev.totalcost
                    dd += ev.totaldrivingdistance
                    tot_time_diff += ev.totaldrivingtime - ev.ept_totaldrivingtime
                    tot_time_diff += ev.true_waitingtime - ev.ept_waitingtime
                    tot_time_diff += ev.true_charging_duration - ev.ept_charging_duration

                final_score = tot_traveltime
                dt = totaldrivingtime
                wt = true_waitingtime
                # reward = -tot_time_diff / 60
                # ept_score += reward
                print('ept_score: ', ept_score)

            model.put_data((state, action, reward*60, next_state, prob[action].item(), done))
            state = next_state

            if timestep % update_timestep == 0:
                model.train_net()
        # model.train_net()

        print(action_list)
        episodes.append(e)
        eptscores.append(ept_score)
        finalscores.append(final_score)
        waiting_time_list.append(wt)
        driving_time_list.append(dt)
        driving_distance_list.append(dd)

        log_tmp_add_ep_rwd.append(ept_score)

        if e % 500 == 499:
            log_avg_ep.append(e)
            avg_rwd = sum(log_tmp_add_ep_rwd)/len(log_tmp_add_ep_rwd)
            log_avg_rwd.append(avg_rwd)
            log_tmp_add_ep_rwd.clear()

        # if e % 100 == 99:
        #     agent.save_model("{}/ppo_model_{}.pt".format(resultdir, e))

        if e % 500 == 499:
            now = datetime.datetime.now()
            training_time = now - now_start

            plt.title('Training avg eptscores: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('score')
            plt.plot(log_avg_ep, log_avg_rwd, 'b')
            fig = plt.gcf()
            fig.savefig('{}/train avg eptscores.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()

            plt.title('Training eptscores: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('score')
            plt.plot(episodes, eptscores, 'b')
            fig = plt.gcf()
            fig.savefig('{}/train eptscores.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()

            plt.title('Training finalscores: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('step')
            plt.plot(episodes, finalscores, 'r')
            fig = plt.gcf()
            fig.savefig('{}/train finalscores.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()
            ##############################################################################################################
            plt.title('Training waiting_time: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('score')
            plt.plot(episodes, waiting_time_list, 'b')
            fig = plt.gcf()
            fig.savefig('{}/train waiting_time.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()

            plt.title('Training driving_time: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('step')
            plt.plot(episodes, driving_time_list, 'r')
            fig = plt.gcf()
            fig.savefig('{}/train driving_time.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()

            plt.title('Training distance: {}'.format(training_time))
            plt.xlabel('Epoch')
            plt.ylabel('step')
            plt.plot(episodes, driving_distance_list, 'r')
            fig = plt.gcf()
            fig.savefig('{}/train driving_distance.png'.format(resultdir), facecolor='white', dpi=600)
            plt.clf()

    now = datetime.datetime.now()
    training_time = now - now_start

    fw = open('{}/epi_score.txt'.format(resultdir), 'w', encoding='UTF8')
    for sc in eptscores:
        fw.write(str(sc) + '\t')
    fw.write('\n')
    for fsc in finalscores:
        fw.write(str(fsc) + '\t')

    fw.close()

if __name__ == '__main__':

    main()


import random
from functools import cmp_to_key

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.cluster.hierarchy as hie

import settings as st

def calculateSimilarity(a, b):
    ref = -1
    for i in range(0, st.AP_COUNT):
        if a[i] != 1 and b[i] != 1:
            ref = i
            break
    if ref == -1:
        return 0
    a[a != 1] -= a[ref]
    b[b != 1] -= b[ref]

    tot = 0
    count = 0
    for i in range(0, st.AP_COUNT):
        if a[i] != 1 or b[i] != 1:
            tot += abs(a[i] - b[i])
            count += 1
    avg = tot / count
    if avg > 1:
        avg = 1
    return avg

def doCluster(data):
    sim = hie.distance.pdist(data, metric=calculateSimilarity)
    Z = hie.linkage(sim, method='average')
    hie.dendrogram(Z, color_threshold=st.LOC_CLU_THRESHOLD)
    res = hie.fcluster(Z, st.LOC_CLU_THRESHOLD,criterion='distance')
    print(res)
    return res

def doSelect(seq, data):
    if len(seq) == 1:
        return seq[0]
    lens = []
    for i in seq:
        lens.append([i, len(data[i][data[i] != 1])])
    sorted(lens, key=cmp_to_key(lambda a, b: b[1] - a[1]))
    eq = []
    ml = lens[0][1]
    for ln in lens:
        if ln[1] == ml:
            eq.append(ln[0])
        else:
            break
    if len(eq) == 1:
        return eq[0]
    maxsm = 0
    selectIdx = 0
    for i in eq:
        tot = 0
        for oth in seq:
            tot += calculateSimilarity(data[i], data[oth])
        if tot > maxsm:
            maxsm = tot
            selectIdx = i
    return selectIdx

def selectLocs(srcData):
    data = srcData.values[:, 0: st.AP_COUNT]
    data = abs(data)
    data[data > st.RSSI_THRESHOLD] = st.RSSI_THRESHOLD
    data /= st.RSSI_THRESHOLD

    cluArr = doCluster(data)
    cluDir = {}
    for i, val in enumerate(cluArr):
        if val in cluDir:
            cluDir[val].append(i)
        else:
            cluDir[val] = [i]

    res = []
    for clu in cluDir.items():
        res.append(doSelect(clu[1], data))

    return res

# srcData = pd.read_csv(st.TRAIDATA_PATH)
# srcData = srcData[(srcData.BUILDINGID == st.BUILDINGID)
#                & (srcData.FLOOR == st.FLOORID)]
# data = srcData.values[:, 0: st.AP_COUNT]
# data = abs(data)
# data[data > st.RSSI_THRESHOLD] = st.RSSI_THRESHOLD
# data /= st.RSSI_THRESHOLD

# sim = hie.distance.pdist(data, metric=calculateSimilarity)
# Z = hie.linkage(sim, method='average')
# res = hie.fcluster(Z, st.LOC_CLU_THRESHOLD)

# hie.dendrogram(Z, color_threshold=0.1)
# plt.show()
# res = hie.fcluster(Z, 0.1)

# res = selectLocs(data)
# import dataload as ld
# data, apMap = ld.loadData()
# ls = selectLocs(data)
# print(len(ls))
# print(data.shape)
# plt.show()


import numpy as np

with open('day8.txt') as f:
    forest = np.array([[int(x) for x in list(line.strip())] for line in f])

def look_along(x):
    return x > np.hstack((-1, np.maximum.accumulate(x)[:-1]))

is_visible = np.apply_along_axis(look_along, 0, forest)
is_visible |= np.apply_along_axis(look_along, 1, forest)
is_visible |= np.apply_along_axis(look_along, 0, forest[::-1, :])[::-1, :]
is_visible |= np.apply_along_axis(look_along, 1, forest[:, ::-1])[:, ::-1]
print('Day 8, part 1:', is_visible.sum())

def compute_scenic_score(candidate_tree):
    height = forest[candidate_tree]
    row, col = candidate_tree
    if row == 0 or col == 0 or row == forest.shape[0] - 1 or col == forest.shape[1] - 1:
        return 0

    score = (np.maximum.accumulate(forest[row, col + 1:-1]) < height).sum() + 1
    score *= (np.maximum.accumulate(forest[row +
              1:-1, col]) < height).sum() + 1
    score *= (np.maximum.accumulate(forest[row,
              col - 1:0:-1]) < height).sum() + 1
    score *= (np.maximum.accumulate(forest[row -
              1:0:-1, col]) < height).sum() + 1
    return score

scenic_scores = [compute_scenic_score(tree)
                 for tree in zip(*np.nonzero(is_visible))]
print('Day 8, part 2:', np.max(scenic_scores))