import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('SVA1StyleSheet.mplstyle')

def parse_args():
    import argparse
    parser = argparse.ArgumentParser(description='Correlation of reserved stars')
    parser.add_argument('--tausflask1',
                        default='/home/dfa/sobreira/alsina/catalogs/FLASK/taus/',
                        help='Full Path to the taus measurement with flask version 1')
    parser.add_argument('--tausflask2',
                        default='/home/dfa/sobreira/alsina/catalogs/FLASK/taus_g1g2flip/',
                        help='Full Path to the taus measurement with flask version 2')
    parser.add_argument('--zbin', default=4 , type=int,
                        help='seed used, useful to run parallel')
    parser.add_argument('--plotspath',
                        default='/home/dfa/sobreira/alsina/Y3_shearcat_tests/alpha-beta-eta-test/measured_correlations/plots/',
                        help='location of the plots.')
    args = parser.parse_args()

    return args

def plotflask(axs, zbin, tausflask, plotspath, color, label):
    from src.readfits import read_taus
    import numpy as np

    ax1, ax2, ax3, ax4, ax5, ax6 = axs

    veclist = []
    count = 0

    for seed in range(1, 401 ):
        for ck in range(1, 2):
            name = os.path.join(tausflask, 'taus_src-cat_s%d_z%d_ck%d.fits'%(seed,zbin, ck  ))
            exist =  os.path.isfile(name)
            if exist:
                meanr, taus, covtaus = read_taus(name)
                if (np.count_nonzero(taus) == 0):
                    print("Warning, weird measurement, skipping", name)
                else:
                    veclist.append(np.concatenate(np.c_[taus]))
                    count +=1

    print(count, "FLASK catalogs were read")
    meanvec = np.mean(veclist, axis=0)

    nrows = len(meanr)
    tau0pmean =  meanvec[0:nrows]
    tau0mmean =  meanvec[nrows:2*nrows]
    tau2pmean =  meanvec[2*nrows:3*nrows]
    tau2mmean =  meanvec[3*nrows:4*nrows]
    tau5pmean =  meanvec[4*nrows:5*nrows]
    tau5mmean =  meanvec[5*nrows:6*nrows]

    ranveclist = np.c_[veclist].T
    covmat = np.cov(ranveclist)
    print('matrix covariance shape', covmat.shape)
    sig_tau0p = np.sqrt(np.diag(covmat[0:nrows, 0:nrows]))
    sig_tau0m = np.sqrt(np.diag(covmat[nrows:2*nrows, nrows:2*nrows]))
    sig_tau2p = np.sqrt(np.diag(covmat[2*nrows:3*nrows, 2*nrows:3*nrows]))
    sig_tau2m = np.sqrt(np.diag(covmat[3*nrows:4*nrows, 3*nrows:4*nrows]))
    sig_tau5p = np.sqrt(np.diag(covmat[4*nrows:5*nrows, 4*nrows:5*nrows]))
    sig_tau5m = np.sqrt(np.diag(covmat[5*nrows:6*nrows, 5*nrows:6*nrows]))

    taumeans = [tau0pmean,tau0mmean,tau2pmean,tau2mmean,tau5pmean,tau5mmean ]
    sig_taus = [sig_tau0p,sig_tau0m,sig_tau2p,sig_tau2m,sig_tau5p,sig_tau5m  ]

    ylabels = [r'$\tau_{0+}$', r'$\tau_{0-}$', r'$\tau_{2+}$', r'$\tau_{2-}$', r'$\tau_{5+}$', r'$\tau_{5-}$']
    for i, ax in enumerate(axs):
        ax.errorbar(meanr,taumeans[i],yerr=sig_taus[i],color=color, ls='', marker='.', capsize=2, label=label)
        ax.legend(loc='best', fontsize=10)
        ax.set_ylabel(ylabels[i]); ax1.set_xlabel(r'$\theta$')
        ax.set_xscale('log')
        #ax.set_yscale('log')
        #ax.set_ylim([ -2.e-6,2.e-6 ])

def main():
    import sys; sys.path.append(".")
    from src.readfits import read_taus
    import numpy as np
    args = parse_args()

    plotspath = os.path.expanduser(args.plotspath)
    try:
        if not os.path.exists(plotspath):
            os.makedirs(plotspath)
    except OSError:
        if not os.path.exists(plotspath): raise

    figs = [];  axs = []; filenames = []
    names = ['taus0p', 'taus0m', 'taus2p', 'taus2m' , 'taus5p' , 'taus5m']
    for i in range(6):
        figaux, axaux = plt.subplots()
        figs.append(figaux); axs.append(axaux)
        filenames.append(os.path.join(plotspath,'%s_flask_zbin%d%s'%(names[i], args.zbin, '.png') ))

    plotflask(axs, args.zbin, args.tausflask1, args.plotspath, 'red', 'Taus flask noflip')
    plotflask(axs, args.zbin, args.tausflask2, args.plotspath, 'blue', 'Taus flask g1g2flip')

    for i, fig in enumerate(figs):
        fig.tight_layout()
        fig.savefig(filenames[i], dpi=500)
        plt.close(fig)
        print(filenames[i], 'Printed!')

if __name__ == "__main__":
    main()


def order(a):
    counter_as = 0
    counter_des = 0
    b = 0
    for i in a:
        if i > b:
            counter_as += 1
        else:
            counter_des += 1
        b = i
    if counter_as < 2:
        k = 'descending'
    elif counter_des < 2:
        k = 'ascending'
    else:
        k = 'not sorted'
    return k

order([10, 5, 4])
order([6, 20, 160, 420])
order([6, 20, 160, 420])


from heapq import heapify, heappop, heappush
import collections

'''
Time: O(n) + O(nlogk)
Space: O(n)
'''
class Solution:

    def __init__(self, nums, k):
        self.heap = []
        self.k = k
        self.nums = nums

    def topk(self):
        freq = collections.defaultdict(int)
        for num in self.nums:
            freq[num] += 1
        print(freq)
        for key, val in freq.items():
            heappush(self.heap, (val, key))
            if len(self.heap) > self.k:
                heappop(self.heap)
        return [x[1] for x in self.heap]

if __name__ == '__main__':
    s = Solution([1,1,2,2,2,2,5,5,5,5,5,5,3,3,3], 2)
    print(s.topk())


load(":common/cc/semantics.bzl", "semantics")
load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/cc_common.bzl", "cc_common")

CcToolchainInfo = cc_common.CcToolchainInfo
TemplateVariableInfo = _builtins.toplevel.platform_common.TemplateVariableInfo
ToolchainInfo = _builtins.toplevel.platform_common.ToolchainInfo

def _impl(ctx):
    cc_toolchain = cc_helper.find_cpp_toolchain(ctx, mandatory = ctx.attr.mandatory)
    if not cc_toolchain:
        return []
    make_variables = cc_toolchain.get_additional_make_variables()
    cc_provider_make_variables = cc_helper.get_toolchain_global_make_variables(cc_toolchain)
    template_variable_info = TemplateVariableInfo(make_variables | cc_provider_make_variables)
    toolchain = ToolchainInfo(
        cc = cc_toolchain,
        cc_provider_in_toolchain = True,
    )
    return [
        cc_toolchain,
        toolchain,
        template_variable_info,
        DefaultInfo(
            files = cc_toolchain.get_all_files_including_libc(),
        ),
    ]

cc_toolchain_alias = rule(
    implementation = _impl,
    fragments = ["cpp", "platform"],
    attrs = {
        "mandatory": attr.bool(default = True),
        "_cc_toolchain": attr.label(default = configuration_field(fragment = "cpp", name = "cc_toolchain"), providers = [CcToolchainInfo]),
        "_cc_toolchain_type": attr.label(default = "@" + semantics.get_repo() + "//tools/cpp:toolchain_type"),
    },
    toolchains = cc_helper.use_cpp_toolchain() +
                 semantics.get_runtimes_toolchain(),
)


import epyk as pk
from epyk.mocks import randoms

# Create a basic report object
page = pk.Page()
page.headers.dev()

# Create JavaScript data
js_data = page.data.js.record(js_code="myData", data=randoms.languages)
# Add a filter object
filter1 = js_data.filterGroup("filter1")

# Add a dropdown box to drive the data changes in the charts
select = page.ui.select([
  {"value": 'name', 'name': 'name'},
  {"value": 'type', 'name': 'code'},
], options={"empty_selected": False})

# Create HTML charts
bar = page.ui.charts.chartJs.bar(randoms.languages, y_columns=["rating", 'change'], x_axis='name')
pie = page.ui.charts.chartJs.pie(randoms.languages, y_columns=['change'], x_axis='name')

# Add the charts to a row
row = page.ui.row([bar, pie])

# Add a change event on the dropdown to update the charts
select.change([
  bar.build(filter1.group().sumBy(['rating', 'change'], select.dom.content), options={"x_axis": select.dom.content}),
  pie.build(filter1.group().sumBy(['change'], select.dom.content), options={"x_axis": select.dom.content}),
])


"""
-*- coding: utf-8 -*-
File    : basepage.py
Version : 0.1
Author  : usrpi
Date    :2021/1/4
"""
import logging
import datetime
import os

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 封装基本函数 -- 执行日志、处理异常、失败截图
# 所有的页面公共部分，不涉及业务
class BasePage:

    def __init__(self, driver):
        self.driver = driver

    # 等待元素可见
    def wait_eleVisible(self, locator, times=30 ,poll_frequency=0.5, doc=""):
        """
        :param locator: 元素定位。 元素形式（元素定位类型、元素定位方式）
        :param times:
        :param poll_frequency:
        :param doc:
        :return:
        """
        logging.info("等待元素 s% 可见", locator)
        try:
            # 开始等待时间
            start = datetime.datetime.now()
            return WebDriverWait(self.driver, times=30 ,poll_frequency=0.5).until(EC.visibility_of_element_located(locator))
            # 结束等待时间
            end = datetime.datetime.now()
            # 求一个插值，写在日志中，就是等待了多久
            logging.info("等待结束，等待时长为：s%", (end-start))
        except:
            logging.exception("等待元素可见失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise

    # 等待元素可见
    def wait_eleExist(self, locator):
        pass
    # 查找元素
    def get_ele(self, locator, doc = ""):
        logging.info("查找元素: s%", locator)
        try:
            return self.driver.find_element(*locator)
        except:
            logging.exception("查找元素失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise

    # 点击操作
    def click_ele(self, locator, doc = ""):
        # 找元素
        ele = self.get_ele(locator, doc)
        # 元素操作
        logging.info("点击元素：s%", locator)
        try:
           ele.click()
        except:
            logging.exception("元素点击操作失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise
    # 输入操作
    def input_text(self, locator, text , doc = ""):
        # 找元素
        ele = self.get_ele(locator,doc)
        # 输入操作
        logging.info("元素输入：s%",locator)
        try:
           ele.send_keys(text)
        except:
            logging.exception("数据输入失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise

    # 获取元素的文本内容
    def get_text(self, locator, doc = ""):
        # 找元素
        ele = self.get_ele(locator, doc)
        # 获取文本
        try:
            return ele.text
        except:
            logging.exception("获取文本失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise

    # 获取元素的属性
    def get_attr(self, locator,attr, doc = ""):
        # 找元素
        ele = self.get_ele(locator, doc)
        # 获取文本
        try:
            return ele.get_attribute(attr)
        except:
            logging.exception("获取元素属性失败！！！")
            # 截图
            self.save_screenshot(doc)
            raise

    # alter弹窗处理

    # iframe切换

    # 上传操作

    # 滚动条处理
    # 窗口切换

    # 失败截图
    def save_screenshot(self, name):
        # 图片名称：模块名_页面名称_操作名称_时间.png
        path = os.path.dirname(os.path.dirname(__file__)) + '\\Output\screenshots'
        t = datetime.datetime.now()
        file_name = path + t + ".png"
        self.driver.save_screenshot(file_name)
        logging.info("接取网页成功，文件路径为：s%" , file_name)


import logging
from datetime import timedelta

import requests
from django.db import models
from django.utils.six import string_types
from django.utils import timezone
from requests_oauthlib import OAuth2Session

from killboard import app_settings
from killboard.errors import TokenError, IncompleteResponseError

logger = logging.getLogger(__name__)

def _process_scopes(scopes):
    if scopes is None:
        # support filtering by no scopes with None passed
        scopes = []
    if not isinstance(scopes, models.QuerySet) and len(scopes) == 1:
        # support a single space-delimited string inside a list because :users:
        scopes = scopes[0]
    # support space-delimited string scopes or lists
    if isinstance(scopes, string_types):
        scopes = set(scopes.split())
    return set(str(s) for s in scopes)

class TokenQueryset(models.QuerySet):
    def get_expired(self):
        """
        Get all tokens which have expired.
        :return: All expired tokens.
        :rtype: :class:`esi.managers.TokenQueryset`
        """
        max_age = timezone.now() - timedelta(seconds=app_settings.ESI_TOKEN_VALID_DURATION)
        return self.filter(created__lte=max_age)

    def bulk_refresh(self):
        """
        Refreshes all refreshable tokens in the queryset.
        Deletes any tokens which fail to refresh.
        Deletes any tokens which are expired and cannot refresh.
        Excludes tokens for which the refresh was incomplete for other reasons.
        """
        session = OAuth2Session(app_settings.ESI_SSO_CLIENT_ID)
        auth = requests.auth.HTTPBasicAuth(app_settings.ESI_SSO_CLIENT_ID, app_settings.ESI_SSO_CLIENT_SECRET)
        incomplete = []
        for model in self.filter(refresh_token__isnull=False):
            try:
                model.refresh(session=session, auth=auth)
                logging.debug("Successfully refreshed {0}".format(repr(model)))
            except TokenError:
                logger.info("Refresh failed for {0}. Deleting.".format(repr(model)))
                model.delete()
            except IncompleteResponseError:
                incomplete.append(model.pk)
        self.filter(refresh_token__isnull=True).get_expired().delete()
        return self.exclude(pk__in=incomplete)

    def require_valid(self):
        """
        Ensures all tokens are still valid. If expired, attempts to refresh.
        Deletes those which fail to refresh or cannot be refreshed.
        :return: All tokens which are still valid.
        :rtype: :class:`esi.managers.TokenQueryset`
        """
        expired = self.get_expired()
        valid = self.exclude(pk__in=expired)
        valid_expired = expired.bulk_refresh()
        return valid_expired | valid

    def require_scopes(self, scope_string):
        """
        :param scope_string: The required scopes.
        :type scope_string: Union[str, list]
        :return: The tokens with all requested scopes.
        :rtype: :class:`esi.managers.TokenQueryset`
        """
        scopes = _process_scopes(scope_string)
        if not scopes:
            # asking for tokens with no scopes
            return self.filter(scopes__isnull=True)
        from .models import Scope
        scope_pks = Scope.objects.filter(name__in=scopes).values_list('pk', flat=True)
        if not len(scopes) == len(scope_pks):
            # there's a scope we don't recognize, so we can't have any tokens for it
            return self.none()
        tokens = self.all()
        for pk in scope_pks:
            tokens = tokens.filter(scopes__pk=pk)
        return tokens

    def require_scopes_exact(self, scope_string):
        """
        :param scope_string: The required scopes.
        :type scope_string: Union[str, list]
        :return: The tokens with only the requested scopes.
        :rtype: :class:`esi.managers.TokenQueryset`
        """
        num_scopes = len(_process_scopes(scope_string))
        pks = [v['pk'] for v in self.annotate(models.Count('scopes')).require_scopes(scope_string).filter(
            scopes__count=num_scopes).values('pk', 'scopes__id')]
        return self.filter(pk__in=pks)

    def equivalent_to(self, token):
        """
        Gets all tokens which match the character and scopes of a reference token
        :param token: :class:`esi.models.Token`
        :return: :class:`esi.managers.TokenQueryset`
        """
        return self.filter(character_id=token.character_id).require_scopes_exact(token.scopes.all()).filter(
            models.Q(user=token.user) | models.Q(user__isnull=True)).exclude(pk=token.pk)

class TokenManager(models.Manager):
    def get_queryset(self):
        """
        Replace base queryset model with custom TokenQueryset
        :rtype: :class:`esi.managers.TokenQueryset`
        """
        return TokenQueryset(self.model, using=self._db)

class EVEClassManager(models.Manager):
    def get_or_create_from_code(self, i_id, json_data, api):
        try:
            return self.get(id=i_id), False
        except self.model.DoesNotExist:
            item = self.model(id=i_id)
            item.process(json_data, api)
            item.save()
            return item, True


from app.views import v_menu
from app.controllers import c_input
from app.models import m_search
from app.models import m_players
from app.models import m_list
from app.models import m_tournaments

class Controller:

    def main_menu(self):
        v_menu.View().main_menu()
        self.menu_number = c_input.Input().select_menu_number(3)

        # Call player menu
        if self.menu_number == 1:
            Controller().players_menu()

        # Call tournaments menu
        elif self.menu_number == 2:
            Controller().tournaments_menu()

        # Close app
        elif self.menu_number == 3:
            pass

    def players_menu(self):
        v_menu.View().players_menu()
        self.menu_number = c_input.Input().select_menu_number(5)

        #  Call for create player
        if self.menu_number == 1:
            player = m_players.Player()
            Controller().players_menu()

        # Call search player and call modify player
        elif self.menu_number == 2:
            self.find_player = m_search.Search().player(
                c_input.Input().check_input('text', 'Nom ou Prénom: ')
            )
            if self.find_player == 'None':
                Controller().players_menu()
            else:
                player = m_players.Player(self.find_player)
                player.modify(c_input.Input().check_input(
                    'number', 'Nouveau classement: ')
                )
            Controller().players_menu()

        # Call list player
        elif self.menu_number == 3:
            v_menu.View().list_menu()
            m_list.List().players(c_input.Input().select_menu_number(2))
            Controller().players_menu()

        # Back to main menu
        elif self.menu_number == 4:
            Controller().main_menu()

        # Close app
        elif self.menu_number == 5:
            pass

    def tournaments_menu(self):
        v_menu.View().tournaments_menu()
        self.menu_number = c_input.Input().select_menu_number(5)

        # Call for create tounament
        if self.menu_number == 1:
            m_tournaments.Tournament()
            Controller().tournaments_menu()

        # Search and load tounament
        elif self.menu_number == 2:
            self.find_tournament = m_search.Search().tournament(
                c_input.Input().check_input('text', 'Nom ou Lieu: ')
            )
            if self.find_tournament == 'None':
                Controller().tournaments_menu()
            else:
                Controller().current_tournament_menu(self.find_tournament)

        # Call list tounaments
        elif self.menu_number == 3:
            m_list.List().tournaments()
            Controller().tournaments_menu()

        # Back to main menu
        elif self.menu_number == 4:
            Controller().main_menu()

        # Close app
        elif self.menu_number == 5:
            pass

    def current_tournament_menu(self, id):
        self.tournament = m_tournaments.Tournament(id)

        self.show_menu = v_menu.View().current_tournament_menu(
            self.tournament.name, self.tournament.stat,
            self.tournament.start, self.tournament.end,
            self.tournament.rounds_nbr
        )
        self.menu_number = c_input.Input().select_menu_number(6)

        # Start round
        if self.menu_number == 1:
            if self.tournament.stat > self.tournament.rounds_nbr:
                self.current_tournament_menu(self.tournament.id)
            else:
                self.tournament.start_rounds()
                self.tournament.stat += 1
                self.current_tournament_menu(self.tournament.id)

        # List players
        elif self.menu_number == 2:
            v_menu.View().list_menu_t()
            self.tournament.list_players(
                c_input.Input().select_menu_number(3)
            )
            self.current_tournament_menu(self.tournament.id)

        # List matchs et rounds
        elif self.menu_number == 3:
            self.tournament.list_rounds()
            self.current_tournament_menu(self.tournament.id)

        # Show tournament infos
        elif self.menu_number == 4:
            v_menu.View().list_tournaments(
                self.tournament.name, self.tournament.place,
                self.tournament.start, self.tournament.end,
                self.tournament.rounds_nbr, self.tournament.timing,
                self.tournament.description
            )
            input('\nEntrer pour revenir au menu précédent')
            self.current_tournament_menu(self.tournament.id)

        # Back to tournament menu
        elif self.menu_number == 5:
            Controller().tournaments_menu()

        # Close app
        elif self.menu_number == 6:
            pass


import numpy as np
import cv2 as cv

def bitfield(n):
    return [int(digit) for digit in bin(n)[2:]]

def gerar_mensagem(mensagem):
    lista = []
    for m in mensagem:
        val = ord(m)
        bits = bitfield(val)

        if len(bits) < 8:
            for a in range(8-len(bits)):
                bits.insert(0, 0)
        lista.append(bits)
    arr = np.array(lista)
    arr = arr.flatten()
    return arr

def converter_mensagem(saida):
    bits = np.array(saida)
    mensagem_out = ''
    bits = bits.reshape((int(len(saida)/8), 8))
    for b in bits:
        sum = 0
        for i in range(8):
            sum += b[i]*(2**(7-i))
        mensagem_out += chr(sum)
    return mensagem_out

texto = "Batatinha 123"
terminadorMensagem = "##"
texto += terminadorMensagem
arrayBits = gerar_mensagem(texto)

terminadorEmBits = gerar_mensagem(terminadorMensagem)
terminadorEmBitsString = ""
for val in terminadorEmBits:
    terminadorEmBitsString += str(val)

image = cv.imread('teste.png')
listaUltimosDigitosVermelhos = []

# gerei lista de ultimos digitos vermelhos
for row in image:
    for pixel in row:
        if(pixel[2] % 2 == 0):
            listaUltimosDigitosVermelhos.append(0)
        else:
            listaUltimosDigitosVermelhos.append(1)

# inseri a mensagem na lista de ultimos digitos vermelhos
for i in range(len(arrayBits)):
    listaUltimosDigitosVermelhos[i] = arrayBits[i]

# inseri a mensagem oculta na imagem
i = 0
countLimite = len(arrayBits)
print('comecou')

for row in image:
    for pixel in row:
        if(i < countLimite):
            # se for par, e quero guardar 1, soma 1, transforma ele em impar
            if(pixel[2] % 2 == 0 and listaUltimosDigitosVermelhos[i] == 1):
                pixel[2] = pixel[2] + 1

            # se for impar, quero guardar um 0, subtrai 1, transforma ele em par
            if(pixel[2] % 2 > 0 and (listaUltimosDigitosVermelhos[i] == 0)):
                pixel[2] = pixel[2] - 1
            i += 1
        else:
            break

listaUltimosDigitosVermelhos = []

# gerei lista de ultimos digitos vermelhos
for row in image:
    for pixel in row:
        # listaUltimosDigitosVermelhos.append(pixel[2])
        if(pixel[2] % 2 == 0):
            listaUltimosDigitosVermelhos.append(0)
        else:
            listaUltimosDigitosVermelhos.append(1)

i = 0
mensagem = ""

# peguei a mensagem oculta dentro da lista de ultimos digitos vermelhos
for val in listaUltimosDigitosVermelhos:
    i += 1

    if(mensagem.find(terminadorEmBitsString) >= 0):
        print('achou')
        break

    mensagem += str(val)

mensagemBits = []
for letra in mensagem:
    mensagemBits.append(int(letra))

# print('mensagem criptografada', arrayBits)
# print('mensagem criptografada', len(arrayBits))
# print('mensagem encontrada', mensagemBits)
# print('mensagem encontrada', len(mensagemBits))
mensagemConvertida = converter_mensagem(mensagemBits)
mensagemConvertida = mensagemConvertida.replace(terminadorMensagem, '')

print('mensagem convertida: ', mensagemConvertida)

# cv.imshow('top', image)
# cv.waitKey(0)


import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots
import os
import plotly.io as pio
pio.renderers.default = "browser"

num_of_interviews = [0, 1, 2, 3]

y_list = 3
x_list = 6

current = False
trend = False

# example: communication = 0
specific_aspect = 1

# categories set for the radar plots
categories = ["Value for the customer",
              "Value for the producer",
              "Risks for the customer",
              "Risks for the producer",
              "Cost of development",
              "Cost of implementation",
              "Return of investment",
              "Market establishment"
              ]

# aspect names
aspects = ["Communication",
           "Data handling",
           "Data driven problem solving",
           "Situational context awareness",
           "Adaption to different contexts",
           "Remote Operation",
           "Recommendation and decision support",
           "Self-organization and control",
           "Predictive acting",
           "Continuous improvement",
           "Task automation"]

def add_figure_to_plot(values, _categories, name):
    """
    This function adds another trace to a plotly figure.
    You need to provide values, categories and a plot name.
    """
    fig.add_trace(go.Scatterpolar(
        r=values,
        theta=_categories,
        fill=None,
        name=name
    ))

if __name__ == "__main__":
    # df gets loaded from csv
    df = pd.read_csv("interview0/interview0_current.csv")

    # 0 = "Value for the customer"
    # 1 = "Value for the producer"
    # 2 = "Risks for the customer"
    # 3 = "Risks for the producer"
    # 4 = "Cost of development"
    # 5 = "Cost of implementation"
    # 6 = "Return of investment"
    # 7 = "Market establishment"
    # first criterion row gets selected.
    lst0 = list(df.iloc[y_list])
    lst0.pop(0)
    # print(lst0)

    # second criterion row gets selected.
    lst1 = list(df.iloc[x_list])
    lst1.pop(0)
    # print(lst1)

    # initial lists for the heatmap
    # hl / hm / hh
    # ml / mm / mh
    # ll / lm / lh
    ll = []
    lm = []
    lh = []
    ml = []
    mm = []
    mh = []
    hl = []
    hm = []
    hh = []

    # first and second list get sorted into the initial lists for the heatmap
    for i in range(len(lst0)):
        value = "input"
        if lst0[i] == "l" and lst1[i] == "l":
            ll.append(value)
        elif lst0[i] == "l" and lst1[i] == "m":
            lm.append(value)
        elif lst0[i] == "l" and lst1[i] == "h":
            lh.append(value)
        elif lst0[i] == "m" and lst1[i] == "l":
            ml.append(value)
        elif lst0[i] == "m" and lst1[i] == "m":
            mm.append(value)
        elif lst0[i] == "m" and lst1[i] == "h":
            mh.append(value)
        elif lst0[i] == "h" and lst1[i] == "l":
            hl.append(value)
        elif lst0[i] == "h" and lst1[i] == "m":
            hm.append(value)
        elif lst0[i] == "h" and lst1[i] == "h":
            hh.append(value)
        else:
            print(f"Cannot sort value {lst0[i], lst1[i]}, wrong input")

    # data list gets created
    data = [[len(hl), len(hm), len(hh)],
            [len(ml), len(mm), len(mh)],
            [len(ll), len(lm), len(lh)]]

    # figure gets created
    fig = px.imshow(data,
                    labels=dict(x=categories[x_list], y=categories[y_list], color="Occurrence"),
                    x=['low', 'medium', 'high'],
                    y=['high', 'medium', 'low']
                    )
    fig.update_xaxes(side="bottom")
    # fig.show()
    fig.write_html(f"test_heatmap.html")

    # # new figure class gets created
    # fig = make_subplots()
    #
    # interview_count = 0
    #
    # if current:
    #     for i in current_data:
    #         add_figure_to_plot(i[specific_aspect], categories, f"Interview{interview_count}: Current")
    #         interview_count = interview_count + 1
    #
    # interview_count = 0
    # if trend:
    #     for i in trend_data:
    #         add_figure_to_plot(i[specific_aspect], categories, f"Interview{interview_count}: Trend")
    #         interview_count = interview_count + 1
    #
    # fig.update_traces(opacity=0.9)
    #
    # # Plot styling happens here
    # fig.update_layout(font_family="Arial", legend=dict(
    #     title=f"Aspect: {aspects[specific_aspect]}",
    #     font=dict(size=20),
    #     orientation="h",
    #     y=1.1,
    #     yanchor="bottom",
    #     x=0.5,
    #     xanchor="center"
    #     ),
    #     template='plotly',
    #     showlegend=True)
    #
    # fig.update_layout(
    #     polar=dict(
    #         radialaxis=dict(
    #             visible=True,
    #             range=[0, 3]
    #         )),
    # )
    #
    # fig.add_annotation(text='Criterion rating information:
high = value 3
medium = value 2
low = value 1',
    #                    x=0.9,
    #                    y=0.8,
    #                    bordercolor='black',
    #                    borderwidth=1,
    #                    showarrow=False,
    #                    font=dict(size=15,
    #                              family="Arial"))
    #
    # # savings html in specific folders
    # if current:
    #     naming = "current.html"
    # if trend:
    #     naming = "trend.html"
    # if trend and current:
    #     naming = "both.html"
    # interview_interpretation = "multigraph_interpretation"
    # path = os.path.join(os.getcwd(), interview_interpretation)
    # if not os.path.isdir(path):
    #     os.mkdir(path)
    # try:
    #     filename = f"{aspects[specific_aspect] + '_' + str(num_of_interviews) + '_' + naming}"
    #     # fig.write_image(f"{path}/{filename}.png")
    #     fig.write_html(f"{path}/{filename}")
    # except:
    #     print(f"{naming} not defined")


import sys
import os
from django.conf import settings
from django.core.management import execute_from_command_line
from django.conf.urls import url
from django.http import HttpResponse
from django.core.wsgi import get_wsgi_application

DEBUG = os.environ.get('DEBUG', 'on') == 'on'

print(DEBUG)

SECRET_KEY = os.environ.get('SECRET_KEY', os.urandom(32))

print(SECRET_KEY)

settings.configure(
    DEBUG = DEBUG,
    SECRET_KEY = SECRET_KEY,
    ROOT_URLCONF  = __name__,
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
)

def index(request):
    return HttpResponse('Hello World')

urlpatterns = (
    url(r'^$', placeholder, name='homepage'),
)

application = get_wsgi_application()

if __name__ == '__main__':

    execute_from_command_line(sys.argv)


import os

# os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

import tensorflow as tf
import keras
# from keras import layers
from sempler import Dataset, DatasetSide, DatasetSoft
from wave_u_net import wave_u_net
from loss import combined_loss, ScatterLoss, RegulatedLoss
from call_back import CustomCallback
from keras.callbacks import ModelCheckpoint
# import random
# from matplotlib import pyplot as plt
import json
# import numpy as np
import math

s_size = 16384 * (24 // 2)
steps_per_epoch = 100
steps = 20 # 40
noise_ratio = 0.21
batch_size=3
side = False
cycles = 10

# model = wave_u_net(num_initial_filters = 12, num_layers = 6, kernel_size = 10, input_size = s_size, output_type = "single")

# model = wave_u_net(num_initial_filters = 24, num_layers = 12, kernel_size = 15, input_size = s_size, output_type = "single")
# model = wave_u_net(num_initial_filters = 32, num_layers = 16, kernel_size = 30, input_size = s_size, output_type = "single")
# model = wave_u_net(num_initial_filters = 32, num_layers = 16, kernel_size = 50, input_size = s_size, output_type = "single")

# model = wave_u_net(num_initial_filters = 24, num_layers = 12, kernel_size = 15, input_size = s_size, output_type = "single", attention = "Gate", attention_res = False, dropout = "False", dropout_rate = 0.2, sub=True, side_chanel=True, side_chanel_cycles=10)
# model = wave_u_net(num_initial_filters = 32, num_layers = 16, kernel_size = 30, input_size = s_size, output_type = "single", attention = "Gate", attention_res = False, dropout = "False", dropout_rate = 0.2, sub=True, side_chanel=side, side_chanel_cycles=cycles)
model = wave_u_net(num_initial_filters = 32, num_layers = 16, kernel_size = 50, input_size = s_size, output_type = "single", attention = "Gate", attention_res = False, dropout = "Last", dropout_rate = 0.2, sub=True, side_chanel=side, side_chanel_cycles=cycles)

if os.path.exists('model.h5'): model.load_weights('model.h5')

initial_epoch = 0

if os.path.exists('epoch.txt'):
    with open("epoch.txt", "r") as f:
        initial_epoch = int(f.read())

opt = keras.optimizers.Adam(learning_rate=0.000_01) #0.000_1 - 0.000_01

loss = RegulatedLoss(s_size, steps, noise_ratio)
# loss = ScatterLoss(s_size, steps, noise_ratio)
# loss = "MSE"

model.compile(loss=loss, optimizer=opt)

model.summary()

c1 = CustomCallback(chackpoint=True)
c2 = ModelCheckpoint(filepath='model.h5', save_best_only=False, save_weights_only=True, save_freq='epoch')

# 90
if side:
    dataset = DatasetSide(list(range(90)), s_size=s_size, steps=steps, batch_size=batch_size, noise_ratio=noise_ratio, orig=True, info=True, side_cysles=cycles)
else:
    # dataset = Dataset(list(range(900, 1044)), s_size=s_size, steps=steps, batch_size=batch_size, noise_ratio=noise_ratio, orig=True, info=True)
    dataset = DatasetSoft(list(range(0, 250)), s_size=s_size, steps=steps, batch_size=batch_size, noise_ratio=noise_ratio, orig=True, info=True)

epochs = len(dataset) // steps_per_epoch
print(f"data: {(len(dataset) * batch_size):_}")

model.fit(dataset, epochs=epochs, steps_per_epoch=steps_per_epoch, initial_epoch=initial_epoch, shuffle=False, callbacks=[c1, c2])


#!/usr/bin/python
# -*- coding: utf8 -*-
import sys
import cherrypy
import platform
import os
import time

cur_dir = os.path.dirname(os.path.abspath(__file__))

#python 2.4为simplejson，python 2.6以上为json
try:
    import json
except ImportError:
    import simplejson as json

#假装做一个index出来
class Index(object):
    #下面这句表示修饰index方法，这个index方法是暴露给http server的
    @cherrypy.expose
    def index(self):
        return "hello cherrypy"

class Node(object):
    '''
    url /node/dist/
    '''
    #获取目标机器的发行分支，版本号，架构类型，主机名称等等，返回json
    @cherrypy.expose
    def dist(self):
        dist_json = ''
        sysinstaller = ''
        installer = ''
        ostype = platform.dist()
        if(ostype[0] in ['Ubuntu','debian','ubuntu','Debian']):
            sysinstaller = 'apt-get'
            installer = 'dpkg'
        elif(ostype[0] in ['SuSE']):
            sysinstaller = 'zypper'
            installer = 'rpm'
        elif(ostype[0] in ['CentOS', 'centos', 'redhat','RedHat']):
            sysinstaller = 'yum'
            installer = 'rpm'
            machine = platform.machine()
        hostname = platform.node()

        dist_json = {'os.system':ostype[0], 'os.version':ostype[1], 'os.release':ostype[2], 'os.sysinstall':sysinstaller, 'os.installer':installer, 'os.arch':machine, 'os.hostname':hostname}
        return json.dumps(dist_json, sort_keys=False, indent=4, separators=(',', ': '))
    '''
    url /node/GetCpuInfo/
    '''
    #获取CPU型号等，返回json
    @cherrypy.expose
    def GetCpuInfo(self):
        cpu = []
        cpuinfo = {}
        f = open("/proc/cpuinfo")
        lines = f.readlines()
        f.close()
        for line in lines:
            if line == '\n':
                cpu.append(cpuinfo)
                cpuinfo = {}
            if len(line) < 2: continue
            name = line.split(':')[0].strip().replace(' ','_')
            var = line.split(':')[1].strip()
            cpuinfo[name] = var
        return json.dumps(cpu, sort_keys=False, indent=4, separators=(',', ': '))
    '''
    url /node/GetMemInfo/
    '''
    #获取内存使用的详细信息
    @cherrypy.expose
    def GetMemInfo(self):
        mem = {}
        f = open("/proc/meminfo")
        lines = f.readlines()
        f.close()
        for line in lines:
            if len(line) < 2:
                   continue
            name = line.split(':')[0]
            var = line.split(':')[1].split()[0]
            mem[name] = long(var) * 1024.0
        mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']
        return json.dumps(mem, sort_keys=False, indent=4, separators=(',', ': '))
    '''
    url /node/GetLoadAvg//
    '''
    #获取系统负载的详细信息
    @cherrypy.expose
    def GetLoadAvg(self):
        loadavg = {}
        f = open("/proc/loadavg")
        con = f.read().split()
        f.close()
        loadavg['lavg_1']=con[0]
        loadavg['lavg_5']=con[1]
        loadavg['lavg_15']=con[2]
        loadavg['nr']=con[3]
        loadavg['last_pid']=con[4]
        return json.dumps(loadavg, sort_keys=False, indent=4, separators=(',', ': '))
    '''
    url /node/GetIfInfo/eth(x)
    '''
    #获取指定网卡的流量信息，这里面有点复杂
    @cherrypy.expose
    def GetIfInfo(self, interface):
        dist_json = self.dist()
        f = open("/proc/net/dev")
        lines = f.readlines()
        f.close()
        intf = {}
        for line in lines[2:]:
            con = line.split()
            #if部分是给centos使用的，centos在流量大的情况下，网卡信息里面字符串会连上，所以需要单独拆分处理，else部分则是ubuntu或者其他系统格式化很好的使用
            offset = con[0].split(':')
            if str(offset[0]) == interface:
                intf['interface'] = str(offset[0])
                intf['ReceiveBytes'] = str(offset[1])
                intf['ReceivePackets'] = str(con[1])
                intf['ReceiveErrs'] = str(con[2])
                intf['ReceiveDrop'] = str(con[3])
                intf['ReceiveFifo'] = str(con[4])
                intf['ReceiveFrames'] = str(con[5])
                intf['ReceiveCompressed'] = str(con[6])
                intf['ReceiveMulticast'] = str(con[7])
                intf['TransmitBytes'] = str(con[8])
                intf['TransmitPackets'] = str(con[9])
                intf['TransmitErrs'] = str(con[10])
                intf['TransmitDrop'] = str(con[11])
                intf['TransmitFifo'] = str(con[12])
                intf['TransmitFrames'] = str(con[13])
                intf['TransmitCompressed'] = str(con[14])
                intf['TransmitMulticast'] = str(con[15])
                return json.dumps(intf, sort_keys=False)
    #获取全部网卡的接口和流量信息
    @cherrypy.expose
    def GetIfTraffic(self):
        ifs = []
        nettraffic = {}
        f = open("/proc/net/dev")
        lines = f.readlines()
        f.close()
        for line in lines[2:]:
            con = line.split()
            ifname = con[0].split(':')
            if(ifname[0].strip() != 'lo'):
                ifs.append(ifname[0].strip())
            else:
                continue
        for interface in ifs:
            nettraffic[interface] = self.GetIfInfo(interface)

        return json.dumps(nettraffic)
    #获取硬盘的分区信息和使用量
    @cherrypy.expose
    def GetHddInfo(self):
        hdds = []
        mount = {}
        file_system = []
        type = []
        size = []
        used = []
        avail = []
        used_percent = []
        mounted_on = []
        hdds = os.popen('df -lhT  | grep -v tmpfs | grep -v boot | grep -v usr | grep -v tmp | sed \'1d;/ /!N;s/\\n//;s/[ ]*[ ]/\\t/g;\'').readlines()
        for line in hdds:
            dict = {}
            file_system = line.replace('\\n','').replace('\\t',' ').split()[0]
            dict['type'] = line.replace('\\n','').replace('\\t',' ').split()[1]
            dict['size'] = line.replace('\\n','').replace('\\t',' ').split()[2]
            dict['used'] = line.replace('\\n','').replace('\\t',' ').split()[3]
            dict['avail'] = line.replace('\\n','').replace('\\t',' ').split()[4]
            dict['used_percent'] = line.replace('\\n','').replace('\\t',' ').split()[5]
            dict['mounted_on'] = line.replace('\\n','').replace('\\t',' ').split()[6]
            dict['file_system'] = file_system
            mount[file_system] = dict

        dist_json = json.dumps(mount)
        return dist_json

    #获取CPU的使用量信息，需要系统安装sysstat支持
    @cherrypy.expose
    def GetCpuDetail(self):
        dist_json = self.dist()
        dist = json.loads(dist_json)
        if(dist['os.system'] in ['CentOS', 'centos', 'redhat', 'RedHat']):
            if(int(dist['os.version'].split('.')[0])  < 6):  #For CentOS only
                cmd = 'mpstat 1 1 | sed \'1d;2d;3d;4d\' | awk \'{print "{\\\"user\\\":\\\"\"$3\"\\\",\\\"nice\\\":\\\"\"$4\"\\\",\\\"sys\\\":\\\"\"$5\"\\\",\\\"iowait\\\":\\\"\"$6\"\\\",\\\"irq\\\":\\\"\"$7\"\\\",\\\"soft\\\":\\\"\"$8\"\\\",\\\"steal\\\":\\\"\"$9\"\\\",\\\"idle\\\":\\\"\"$10\"\\\"}"}\''
            else:
                cmd = 'mpstat 1 1 | sed \'1d;2d;3d;4d\' | awk \'{print "{\\\"user\\\":\\\"\"$3\"\\\",\\\"nice\\\":\\\"\"$4\"\\\",\\\"sys\\\":\\\"\"$5\"\\\",\\\"iowait\\\":\\\"\"$6\"\\\",\\\"irq\\\":\\\"\"$7\"\\\",\\\"soft\\\":\\\"\"$8\"\\\",\\\"steal\\\":\\\"\"$9\"\\\",\\\"idle\\\":\\\"\"$11\"\\\"}"}\''
        else:
            cmd = 'mpstat 1 1 | sed \'1d;2d;3d;4d\' | awk \'{print "{\\\"user\\\":\\\"\"$3\"\\\",\\\"nice\\\":\\\"\"$4\"\\\",\\\"sys\\\":\\\"\"$5\"\\\",\\\"iowait\\\":\\\"\"$6\"\\\",\\\"irq\\\":\\\"\"$7\"\\\",\\\"soft\\\":\\\"\"$8\"\\\",\\\"steal\\\":\\\"\"$9\"\\\",\\\"idle\\\":\\\"\"$11\"\\\"}"}\''
        cpu = os.popen(cmd).readline().strip()
        return cpu

    @cherrypy.expose
    def GetLvsExtStatsSumm(self):
        stats = {}
        f = open("/proc/net/ip_vs_ext_stats")
        lines = f.readlines()
        f.close()
        for line in lines[1:]:
            con = line.split(':')
            con2 = con[1].split()
            stats[str(con[0].strip())] = con2
        return json.dumps(stats, sort_keys=False, indent=4, separators=(',', ': '))

    @cherrypy.expose
    def GetLvsStatsSumm(self):
        stats = {}
        conns = []
        in_pks = []
        out_pks = []
        in_bytes = []
        out_bytes = []
        f = open("/proc/net/ip_vs_stats")
        lines = f.readlines()
        f.close()
        for line in lines[2:]:
            con = line.split(':')[1].split()
            conns.append(con[0])
            in_pks.append(con[1])
            out_pks.append(con[2])
            in_bytes.append(con[3])
            out_bytes.append(con[4])
        stats = {"conns":conns,"in_pks":in_pks,"out_pks":out_pks,"in_bytes":in_bytes,"out_bytes":out_bytes}
        return json.dumps(stats, sort_keys=False, indent=4, separators=(',', ': '))

    @cherrypy.expose
    def GetLvsConn(self):
        Conn = []
        node_list = []
        dict = {}
        num = 0
        cmd = "ipvsadm -ln"
        lines = os.popen(cmd).readlines()
        for line in lines[3:]:
            num += 1
            con = line.split()
            if con[0] == "TCP" or con[0] == "UDP":
                if num == 1:
                        pass
                else:
                        Conn.append(dict)
                dict = {}
                dict['lb_algo'] =  str(con[2])
                dict['vip'] = str(con[1])
                dict['node'] = []
                continue
            node_dict = {"rs":con[1],"lb_kind":con[2],"weight":con[3],"activeconn":con[4],"inactconn":con[5]}
            dict['node'].append(node_dict)
            if num == len(lines[3:]):
                Conn.append(dict)
        return json.dumps(Conn, sort_keys=False, indent=4, separators=(',', ': '))

    @cherrypy.expose
    def GetLvsStatus(self):
        Conn = []
        node_list = []
        dict = {}
        num = 0
        cmd = "ipvsadm -ln"
        lines = os.popen(cmd).readlines()
        for line in lines[3:]:
            num += 1
            con = line.split()
            if con[0] == "TCP" or con[0] == "UDP":
                if num == 1:
                        pass
                else:
                        Conn.append(dict)
                dict = {}
                dict['lb_algo'] =  str(con[2])
                dict['vip'] = str(con[1])
                dict['node'] = []
                continue
            node_dict = {"rs":con[1],"lb_kind":con[2],"weight":con[3]}
            dict['node'].append(node_dict)
            if num == len(lines[3:]):
                Conn.append(dict)
        return json.dumps(Conn, sort_keys=False, indent=4, separators=(',', ': '))

    @cherrypy.expose
    def GetLvsTraffic(self):
        result = json.loads(open(os.path.join(cur_dir,'data/','lvstraffic')).read())
        return json.dumps(result,sort_keys=False, indent=4, separators=(',', ': '))

if "__main__" == __name__:
    #服务器配置
    settings = {
                'global': {
                    #绑定端口
                    'server.socket_port' : 60090,
                    #ip地址设置，觉得够安全就用0.0.0.0，否则就单独写那台服务器的ip
                    'server.socket_host': '0.0.0.0',
                    'server.socket_file': '',
                    'server.socket_queue_size': 100,
                    'server.protocol_version': 'HTTP/1.1',
                    'server.log_to_screen': True,
                    'server.log_file': '',
                    'server.reverse_dns': False,
                    'server.thread_pool': 200,
                    'server.environment': 'production',
                    'engine.timeout_monitor.on': False
                }
        }
    #使用配置和映射路由并启动webserver
    cherrypy.config.update(settings)
    cherrypy.tree.mount(Index(), '/')
    cherrypy.tree.mount(Node(), '/node')
    cherrypy.engine.start()


from numpy import mean
from numpy import std
from matplotlib import pyplot as plt
from sklearn.model_selection import KFold
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
from tensorflow.keras.optimizers import SGD
from scipy.ndimage.interpolation import shift
from tensorflow.keras.models import load_model
import numpy as np

def getDataset():
 	(Xtrain, Ytrain), (Xtest, Ytest) = mnist.load_data()
 	Xtrain = Xtrain.reshape((Xtrain.shape[0], 28, 28, 1))
 	Xtest = Xtest.reshape((Xtest.shape[0], 28, 28, 1))
 	Ytest = to_categorical(Ytest)
 	Ytrain = to_categorical(Ytrain)
 	return Xtrain, Ytrain, Xtest, Ytest

def processData(train, test):
 	train = train.astype('float32')
 	test = test.astype('float32')
 	train = train / 255.0
 	test = test / 255.0
 	return train, test

def constructModel():
 	model = Sequential()
 	model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
 	model.add(MaxPooling2D((2, 2)))
 	model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform'))
 	model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform'))
 	model.add(MaxPooling2D((2, 2)))
 	model.add(Flatten())
 	model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
 	model.add(Dense(10, activation='softmax'))
 	opt = SGD(learning_rate=0.01, momentum=0.9)
 	model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
 	return model

def modelEvaluation(dataX, dataY, n_folds=5):
 	scores, histories = list(), list()
 	kfold = KFold(n_folds, shuffle=True, random_state=1)
 	for train_ix, test_ix in kfold.split(dataX):
          model = constructModel()
          trainX, trainY, testX, testY = dataX[train_ix], dataY[train_ix], dataX[test_ix], dataY[test_ix]
          history = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX, testY), verbose=0)
          _, acc = model.evaluate(testX, testY, verbose=0)
          print('> %.3f' % (acc * 100.0))
          scores.append(acc)
          histories.append(history)
 	return scores, histories

def getDiagnostics(histories):
 	for i in range(len(histories)):
         plt.subplot(2, 1, 1)
         plt.title('Cross Entropy Loss')
         plt.plot(histories[i].history['loss'], color='blue', label='train')
         plt.plot(histories[i].history['val_loss'], color='orange', label='test')
         plt.subplot(2, 1, 2)
         plt.title('Classification Accuracy')
         plt.plot(histories[i].history['acc'], color='blue', label='train')
         plt.plot(histories[i].history['val_acc'], color='orange', label='test')
 	plt.show()

def getPerformance(scores):
 	print('Accuracy: mean=%.3f std=%.3f, n=%d' % (mean(scores)*100, std(scores)*100, len(scores)))
 	plt.boxplot(scores)
 	plt.show()

def startTraining():
    trainX, trainY, testX, testY = getDataset()
    trainX, testX = processData(trainX, testX)
    scores, histories = modelEvaluation(trainX, trainY)
    getDiagnostics(histories)
    getPerformance(scores)
    model = constructModel()
    model.fit(trainX, trainY, epochs=10, batch_size=32, verbose=0)
    model.save('DigitClassifier.h5')

startTraining()

def startTesting():
 	trainX, trainY, testX, testY = getDataset()
 	trainX, testX = processData(trainX, testX)
 	model = load_model('DigitClassifier.h5')
 	_, acc = model.evaluate(testX, testY, verbose=0)
 	print('> %.3f' % (acc * 100.0))

startTesting()


'''
Write  your code

Push

MinMaxStack

st = [(1,1,1), (1,2,3)]

self.st[-1][2]

0   1   2
(9  1  10)
1
8
7

(7   7     10)
10  None None

'''

class Stack:
  def __init__(self):
    self.st = []

  def pop(self):
    if(self.isEmpty()):
      raise Exception("stack is empty!")
    else:
      element =  self.st[-1]
      del self.st[-1]
      return element

  def push(self, key):
    if(len(self.st) < 0):
      raise Exception("stack is full!")
    else:
      min = self.getMin()
      max = self.getMax()
      # Handling Min
      if(min is None or key < min):
        min = key
      if(max is None or key > max):
        max = key

      self.st.append((key, min, max))

  def getMin(self):
    if(self.st):
      return self.st[-1][1]
    else:
      return None

  def getMax(self):
    if(self.st):
      return self.st[-1][2]
    else:
      return None

  def peek(self):
    return self.st[-1]

  def isEmpty(self):
    if(self.st is None):
      return True

  def printStack(self):
    for v in self.st:
      print(v)

if __name__ == "__main__":
  st = Stack()
  st.push(3)
  st.push(4)

  st.push(1)
  st.push(101)
  st.push(56)

  st.printStack()


#Tutorial 4

number = int(input('Enter the number: '))
cycle = int(input('Enter number of cycles you want: '))
count = 1
while count <= cycle:
    output = count*number
    count += 1
    print(output)

num = 0
sum = 0
while num != -1:
    num = int(input('Enter the number: '))
    sum = sum + num
print('Total is '+str(sum))

hidden_num = 21
score = 100

while score != 0:
    guessed_num = int(input('Enter your guess here: '))
    if guessed_num == hidden_num:
        print('You won!')
        break
    score -= 5
if score == 0:
    print('No more attempts')

import random

hidden_num = random.randint(1,100)
score = 100

while score != 0:
    guessed_num = int(input('Enter your guess here: '))
    if guessed_num == hidden_num:
        print('You won!')
        break
    score -= 5
if score == 0:
    print('No more attempts')

row = 1
while row <= 6:
    col = 1
    while col <= row:
        print(col, end="")
        col +=1
    row += 1
    print('')

row = ''
num = 1
while num <= 6:
    row += str(num)
    print(row)
    num += 1

counter = 1
while counter != 100:
    print(counter)
    counter += 1

import random as rand
var=10
gen_num=rand.randint(1,1515151515)
while gen_num != 2:
      print('Random num is not 2')
      gen_num = rand.randint(1, 1515151515)
else:
     print('Finally it is 2')

count = 1
while count <= 3:
    p = int(input('Enter 1st number: '))
    n = int(input('Enter 2nd number: '))
    r = int(input('Enter 3rd number: '))
    si = p*n*r / 100
    print(si)
    count += 1
else:
    exit()

for i in range(9,-1,-1):
    if i==5:
        break
    print('Current value:', str(i))

for i in range(9, -1, -1):
    if i==5:
        continue
    print('Current value:', str(i))
else:
    print('bye!')


import os
import cv2
import time
import random
import numpy as np
import pandas as pd
import keras.api._v2.keras as keras
import tensorflow as tf
tf.__version__, np.__version__
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras import backend, layers, metrics

from tensorflow.keras.optimizers.experimental import SGD
from tensorflow.keras.applications import Xception
from tensorflow.keras.models import Model, Sequential

from tensorflow.keras.utils import plot_model
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

import seaborn as sns
import matplotlib.pyplot as plt
from Classes import *
# a function that reads images and put them in a dictionary for each person
def read_images(split_type = 'Train'):
    images_dict={}
    image_count = 0
    for person in ['personA','personB','personC','personD','personE']:
        csv_path = f"{person + '/' + split_type+'/'}{person}_SigVerification{split_type}Labels.csv"
        df = pd.read_csv(csv_path)
        images_dict[person]={'forged':[],'real':[]}
        for index, row in df.iterrows():
            folder= person + '/' + split_type
            image = row['image_name']
            if os.path.exists(f'{folder}'+f'/{image}'):
                if row['label'] == 'forged':
                    images_dict[person]['forged'].append([folder,image])
                else:
                    images_dict[person]['real'].append([folder,image])
                image_count +=1
    return images_dict , image_count

# a function that creates triplets to use for training
def create_triplets(images_dict):
    triplets=[]
    for person in images_dict:
        for i in range(len(images_dict[person]['real'])):
            for j in range(i+1,len(images_dict[person]['real'])):
                anchor = (images_dict[person]['real'][i][0] , images_dict[person]['real'][i][1])
                positive = (images_dict[person]['real'][j][0] , images_dict[person]['real'][j][1])
                k = random.randint(0, len(images_dict[person]['forged'])-1)
                negative = (images_dict[person]['forged'][k][0],images_dict[person]['forged'][k][1])
                triplets.append((anchor,positive,negative))
    random.shuffle(triplets)
    return triplets

# a Function that samples the data accordingly
def get_batch(triplet_list, batch_size=256, preprocess=True):
    batch_steps = len(triplet_list) // batch_size

    for i in range(batch_steps + 1):
        anchor = []
        positive = []
        negative = []

        j = i * batch_size
        while j < (i + 1) * batch_size and j < len(triplet_list):
            a, p, n = triplet_list[j]
            a = cv2.imread(f"{a[0]}/{a[1]}")
            p = cv2.imread(f"{p[0]}/{p[1]}")
            n = cv2.imread(f"{n[0]}/{n[1]}")
            a = cv2.resize(a, (128, 128))
            p = cv2.resize(p, (128, 128))
            n = cv2.resize(n, (128, 128))
            anchor.append(a)
            positive.append(p)
            negative.append(n)
            j += 1
        anchor = np.array(anchor)
        positive = np.array(positive)
        negative = np.array(negative)

        if preprocess:
            anchor = preprocess_input(anchor)
            positive = preprocess_input(positive)
            negative = preprocess_input(negative)

        yield ([anchor, positive, negative])

# a Function that returns a pretrained Xception encoder
def get_encoder(input_shape):
    """ Returns the image encoding model """

    pretrained_model = Xception(
        input_shape=input_shape,
        weights='imagenet',
        include_top=False,
        pooling='avg',
    )

    for i in range(len(pretrained_model.layers) - 27):
        pretrained_model.layers[i].trainable = False

    encode_model = Sequential([
        pretrained_model,
        layers.Flatten(),
        layers.Dense(512, activation='relu'),
        layers.BatchNormalization(),
        layers.Dense(256, activation="relu"),
        layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))
    ], name="Encode_Model")
    return encode_model

# a Function that encodes the inputs and computes the distances using distancelayer()
def get_siamese_network(mode = 'train',input_shape=(128, 128, 3)):
    encoder = get_encoder(input_shape)

    if mode != 'train':
        encoder.load_weights("encoder")

    # Input Layers for the images
    anchor_input = layers.Input(input_shape, name="Anchor_Input")
    positive_input = layers.Input(input_shape, name="Positive_Input")
    negative_input = layers.Input(input_shape, name="Negative_Input")

    ## Generate the encodings (feature vectors) for the images
    encoded_a = encoder(anchor_input)
    encoded_p = encoder(positive_input)
    encoded_n = encoder(negative_input)

    # A layer to compute ‖f(A) - f(P)‖² and ‖f(A) - f(N)‖²
    distances = DistanceLayer()(
        encoder(anchor_input),
        encoder(positive_input),
        encoder(negative_input)
    )

    # Creating the Model
    siamese_network = Model(
        inputs=[anchor_input, positive_input, negative_input],
        outputs=distances,
        name="Siamese_Network"
    )
    return siamese_network

# a Function to test the model
def test_on_triplets(test_triplets,siamese_model,batch_size=256):
    pos_scores, neg_scores = [], []

    for data in get_batch(test_triplets, batch_size=batch_size):
        prediction = siamese_model.predict(data)
        pos_scores += list(prediction[0])
        neg_scores += list(prediction[1])

    accuracy = np.sum(np.array(pos_scores) < np.array(neg_scores)) / len(pos_scores)
    ap_mean = np.mean(pos_scores)
    an_mean = np.mean(neg_scores)
    ap_stds = np.std(pos_scores)
    an_stds = np.std(neg_scores)

    print(f"Accuracy on test = {accuracy:.5f}")
    return (accuracy, ap_mean, an_mean, ap_stds, an_stds)

# a Function that saves the encoder weights
def extract_encoder(model):
    encoder = get_encoder((128, 128, 3))
    i=0
    for e_layer in model.layers[0].layers[3].layers:
        layer_weight = e_layer.get_weights()
        encoder.layers[i].set_weights(layer_weight)
        i+=1
    return encoder

# a Function that takes two lists of images and classifies them
def classify_images(encoder,sig_list1, sig_list2, threshold=1.3, mode = 'train'):
    # Getting the encodings for the passed faces
    tensor1 = encoder.predict(sig_list1)
    tensor2 = encoder.predict(sig_list2)

    distance = np.sum(np.square(tensor1 - tensor2), axis=-1)
    prediction = np.where(distance <= threshold, 0, 1)

    if mode != 'trian':
        prediction = np.where(distance <= threshold, 'Real', "Forged")

    return prediction

# a Function that computes the confusion matrix
def ModelMetrics(pos_list, neg_list):
    true = np.array([0] * len(pos_list) + [1] * len(neg_list))
    pred = np.append(pos_list, neg_list)

    # Compute and print the accuracy
    print(f"\nAccuracy of model: {accuracy_score(true, pred)}\n")

    # Compute and plot the Confusion matrix
    cf_matrix = confusion_matrix(true, pred)

    categories = ['Similar', 'Different']
    names = ['True Similar', 'False Similar', 'False Different', 'True Different']
    percentages = ['{0:.2%}'.format(value) for value in cf_matrix.flatten() / np.sum(cf_matrix)]

    labels = [f'{v1}\n{v2}' for v1, v2 in zip(names, percentages)]
    labels = np.asarray(labels).reshape(2, 2)

    sns.heatmap(cf_matrix, annot=labels, cmap='Blues', fmt='',
                xticklabels=categories, yticklabels=categories)

    plt.xlabel("Predicted", fontdict={'size': 14}, labelpad=10)
    plt.ylabel("Actual", fontdict={'size': 14}, labelpad=10)
    plt.title("Confusion Matrix", fontdict={'size': 18}, pad=20)

def Testimages(img1,img2):
    img1 = cv2.imread(img1)
    img2 = cv2.imread(img2)
    img1 = cv2.resize(img1, (128, 128))
    img2 = cv2.resize(img2, (128, 128))
    img1 = np.array(img1)
    img2 = np.array(img2)
    img1 = preprocess_input(img1)
    img2 = preprocess_input(img2)
    yield ([img1, img2])

def predict(cluster, scalar, model, img):

    labels = {0:"Person A", 1:"Person B", 2:"Person C", 3:"Person D", 4:"Person E"}
    number_of_clusters = 10
    sift = cv2.SIFT_create()
    keypoints, descriptors = sift.detectAndCompute(img, None)  # 1- extract features
    cluster_result = cluster.predict(descriptors)  # 2- predict cluster

    # 3- build vocabulary
    vocabulary = np.array([[0 for i in range(number_of_clusters)]], 'float32')
    for each in cluster_result:
        vocabulary[0][each] += 1

    # vocabulary = reweight_tf_idf(vocabulary) ### tf_idf
    vocabulary = scalar.transform(vocabulary)  # 4- normalization
    prediction = model.predict(vocabulary)  # 5 - classification
    return labels[prediction[0]]


N, M = map(int, input().split())
a = list(map(int, input().split()))
g = {i + 1: [] for i in range(N)}

for ai in a:
    g[ai].append(ai + 1)
    g[ai + 1].append(ai)

flag = [False] * (N + 1)

def dfs(i):
    route.append(i)
    flag[i] = True
    for gi in g[i]:
        if flag[gi]:
            continue
        return dfs(gi)
    return route

ans = []
for i in range(1, N + 1):
    route = []
    if flag[i]:
        continue
    res = dfs(i)
    ans += list(reversed(res))

print(*ans)


def lab_blackjack():
  deck = ['A', '2', '3', '4', '5', '6',
          '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', '3', '4', '5', '6',
          '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', '3', '4', '5', '6',
          '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', '3', '4', '5', '6',
          '7', '8', '9', '10', 'J', 'Q', 'K', ]
  options = ['hit', 'stay']
  yes = ['yes', 'y', 'sure']
  no = ['no', 'n', 'nope']
  validyn = []
  for y in yes:
    validyn.append(y)
  for n in no:
    validyn.append(n)

  def getcard():
    from random import choice
    card = choice(deck)
    index = deck.index(card)
    deck.pop(index)
    print(f'cards remaining: {len(deck)}')
    return card

  def gethand():
    hand = []
    card = getcard()
    if card != 'invalid':
      hand.append(card)
    card = getcard()
    if card != 'invalid':
      hand.append(card)
    return hand

  def getvalue(hand):
    value = 0
    aces = []
    for card in hand:
      while True:
        try:
          int(card)
        except:
          if card == 'A':
            value += 1
            ace = card
            aces.append(ace)
            break
          else:
            value += 10
            break
        else:
          card = int(card)
          value += card
          break
    for ace in aces:
      value += 10
      if value > 21:
        value -= 10
    return value

  def getadvise(hand):
    if len(hand) >= 2:
      value = getvalue(hand)
      print(f'hand value: {value}')
      if value < 17:
        advise = 'hit.'
      elif value < 21:
        advise = 'stay.'
      elif value == 21:
        advise = 'stay. You have blackjack!'
      else:
        advise = 'busted'
    else:
      advise = 'invalid hand'
    return advise

  def start():
    hand = gethand()
    print(f'your hand: {hand}')
    advise = getadvise(hand)
    print(f'you should probably: {advise}')
    print(options)
    choice = input('what would you like to do?\n')
    while choice not in options:
      print(options)
      choice = input('what would you like to do?\n')
    return choice, hand

  def hit(hand):
    card = getcard()
    hand.append(card)
    return hand
  opponent = gethand()
  result = start()
  choice = result[0]
  hand = result[1]
  while choice == 'hit':
    hand = hit(hand)
    print(f'your hand: {hand}')
    advise = getadvise(hand)
    if advise == 'busted':
      print('busted'.upper())
      result = 'busted'
      break
    print(f'you should probably: {advise}')
    print(options)
    choice = input('what would you like to do?\n')
  if choice == 'stay':
    value = getvalue(opponent)
    cards = hand
    hand = getvalue(hand)
    if hand > value:
      print(f'Your {cards} beat {opponent}.')
      print(f'You win!')
      result = 'win'
    elif hand == value:
      print(f'Your {cards} match {opponent}.')
      print('tie')
      result = 'tie'
    else:
      print(f'{opponent} beat your {cards}.')
      print('You lost.')
      result = 'lost'
  return result


import logging

import pytest

import nengo
from nengo.builder import Model
from nengo.builder.ensemble import BuiltEnsemble

def test_seeding(Simulator, allclose):
    """Test that setting the model seed fixes everything"""

    #  TODO: this really just checks random parameters in ensembles.
    #   Are there other objects with random parameters that should be
    #   tested? (Perhaps initial weights of learned connections)

    m = nengo.Network(label="test_seeding")
    with m:
        input = nengo.Node(output=1, label="input")
        A = nengo.Ensemble(40, 1, label="A")
        B = nengo.Ensemble(20, 1, label="B")
        nengo.Connection(input, A)
        C = nengo.Connection(A, B, function=lambda x: x ** 2)

    m.seed = 872
    with Simulator(m) as sim:
        m1 = sim.data
    with Simulator(m) as sim:
        m2 = sim.data
    m.seed = 873
    with Simulator(m) as sim:
        m3 = sim.data

    def compare_objs(obj1, obj2, attrs, equal=True):
        for attr in attrs:
            check = allclose(getattr(obj1, attr), getattr(obj2, attr)) == equal
            if not check:
                logging.info("%s: %s", attr, getattr(obj1, attr))
                logging.info("%s: %s", attr, getattr(obj2, attr))
            assert check

    ens_attrs = BuiltEnsemble._fields
    As = [mi[A] for mi in [m1, m2, m3]]
    Bs = [mi[B] for mi in [m1, m2, m3]]
    compare_objs(As[0], As[1], ens_attrs)
    compare_objs(Bs[0], Bs[1], ens_attrs)
    compare_objs(As[0], As[2], ens_attrs, equal=False)
    compare_objs(Bs[0], Bs[2], ens_attrs, equal=False)

    conn_attrs = ("eval_points", "weights")
    Cs = [mi[C] for mi in [m1, m2, m3]]
    compare_objs(Cs[0], Cs[1], conn_attrs)
    compare_objs(Cs[0], Cs[2], conn_attrs, equal=False)

def test_hierarchical_seeding():
    """Changes to subnetworks shouldn't affect seeds in top-level network"""

    def create(make_extra, seed):
        objs = []
        with nengo.Network(seed=seed, label="n1") as model:
            objs.append(nengo.Ensemble(10, 1, label="e1"))
            with nengo.Network(label="n2"):
                objs.append(nengo.Ensemble(10, 1, label="e2"))
                if make_extra:
                    # This shouldn't affect any seeds
                    objs.append(nengo.Ensemble(10, 1, label="e3"))
            objs.append(nengo.Ensemble(10, 1, label="e4"))
        return model, objs

    same1, same1objs = create(False, 9)
    same2, same2objs = create(True, 9)
    diff, diffobjs = create(True, 10)

    m1 = Model()
    m1.build(same1)
    same1seeds = m1.seeds

    m2 = Model()
    m2.build(same2)
    same2seeds = m2.seeds

    m3 = Model()
    m3.build(diff)
    diffseeds = m3.seeds

    for diffobj, same2obj in zip(diffobjs, same2objs):
        # These seeds should all be different
        assert diffseeds[diffobj] != same2seeds[same2obj]

    # Skip the extra ensemble
    same2objs = same2objs[:2] + same2objs[3:]

    for same1obj, same2obj in zip(same1objs, same2objs):
        # These seeds should all be the same
        assert same1seeds[same1obj] == same2seeds[same2obj]

def test_seed_override(seed, allclose):
    """Test that seeds are not overwritten by the seeding function"""
    with nengo.Network(seed=seed - 1) as net:
        a = nengo.Ensemble(10, 1, seed=seed - 2)
        b = nengo.Ensemble(10, 1, seed=seed + 2)

    model = nengo.builder.Model()
    model.seeds[net] = seed + 1
    model.seeds[a] = seed + 2

    # note: intentionally setting this to the 'wrong' value, to check that
    # it isn't being overridden (things with seeds set should have seeded=True)
    model.seeded[net] = False
    model.seeded[a] = False

    model.build(net)

    assert model.seeds[net] == seed + 1
    assert model.seeds[a] == seed + 2
    assert not model.seeded[net]
    assert not model.seeded[a]
    assert allclose(model.params[a].gain, model.params[b].gain)

def test_build_twice():
    model = nengo.builder.Model()
    ens = nengo.Ensemble(10, 1, add_to_container=False)
    model.seeds[ens] = 0
    model.build(ens)
    built_ens = model.params[ens]

    with pytest.warns(UserWarning, match="has already been built"):
        assert model.build(ens) is None
    assert model.params[ens] is built_ens


# coding: utf-8

from flask import jsonify
from app import app

class Error():
    '''
    HTTP Response Error
    '''

    def __init__(self):
        self.status = None
        self.code = None
        self.message = None
        self.errors = None

    def _ready(self, log_level='info'):
        if log_level == 'critical':
            app.logger.critical(str(self.status) + ' ' + self.message)
        else:
            app.logger.info(str(self.status) + ' ' + self.message)

        error = {
            'status': self.status,
            'code': self.code,
            'message': self.message
        }

        if self.errors:
            error['errors'] = self.errors

        return jsonify(error), self.status

    def bad_request(self, message):
        self.status = 400
        if 'errors' in message:
            self.code = message['code']
            self.message = message['message']
            self.errors = message['errors']
        else:
            self.code = message['code']
            self.message = message['message']

        return self._ready()

    def unauthorized(self, message):
        self.status = 403
        self.code = 'unauthorized'
        self.message = message

        return self._ready()

    def forbidden(self, message):
        self.status = 403
        self.code = 'forbidden'
        self.message = message

        return self._ready()

    def not_found(self, message):
        self.status = 404
        self.code = 'not_found'
        self.message = str(message)

        return self._ready()

    def method_not_allowed(self, message):
        self.status = 405
        self.code = 'method_not_allowed'
        self.message = message

        return self._ready()

    def request_timeout(self, message):
        self.status = 408
        self.code = 'request_timeout'
        self.message = message

        return self._ready()

    def conflict(self, message):
        self.status = 409
        self.code = 'conflict'
        self.message = message

        return self._ready()

    def internal_server_error(self, message):
        self.status = 500
        self.code = 'internal_server_error'
        self.message = str(message)

        return self._ready('critical')


from datetime import datetime
import csv

AIRPORTS_DB_LINK = "https://raw.githubusercontent.com/cohaolain/ryanair-py/develop/ryanair/airports.csv"
AIRPORTS_DB_FILE = "data/airports.csv"
AIRPORTS_TIMESTAMP_FILE = "data/airports_timestamp.txt"

airports = None

def get_distance(lat1, lat2, lon1, lon2):
    from math import radians, cos, sin, asin, sqrt
    # The math module contains a function named
    # radians which converts from degrees to radians.
    lon1 = radians(lon1)
    lon2 = radians(lon2)
    lat1 = radians(lat1)
    lat2 = radians(lat2)

    # Haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2

    c = 2 * asin(sqrt(a))

    # Radius of earth in kilometers. Use 3956 for miles
    r = 6371

    # calculate the result
    return(c * r)

# download the airports database from github page of ryanair py
def download_airports():
	from requests import get

	r = get(AIRPORTS_DB_LINK)
	with open(AIRPORTS_DB_FILE, "wb") as f:
		f.write(r.content)
	with open(AIRPORTS_TIMESTAMP_FILE, "w") as f:
		f.write("{}".format(datetime.now()))
	return True

def get_airports():
	import os
	if not os.path.exists(AIRPORTS_DB_FILE) or not os.path.exists(AIRPORTS_TIMESTAMP_FILE):
		print("Downloading airports database...")
		result = download_airports()
		if result:
			print("Airports database downloaded.")

		else:
			print("Error downloading airports database.")
			return None
	elif os.path.exists(AIRPORTS_TIMESTAMP_FILE):
		with open(AIRPORTS_TIMESTAMP_FILE, "r") as f:
			timestamp = f.read()
		if timestamp:
			timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f")
			# check if the timestamp is older than 5 months
			if (datetime.now() - timestamp).total_seconds() > 60*60*24*30*5:
				print("Downloading airports database...")
				result = download_airports()
				if result:
					print("Airports database downloaded.")
				else:
					print("Error downloading airports database.")
					return None
	else:
		print("Error downloading airports database.")
		return None

	airports = {}
	with open(AIRPORTS_DB_FILE, "r") as csv_file:
		csv_reader = csv.DictReader(csv_file, delimiter=",")
		line_count = 0
		airports = []
		for row in csv_reader:
			if line_count == 0:
				line_count += 1
				continue
			airport = {
				"code": row["iata_code"].upper(),
				"name": row["name"].upper(),
				"city": row["municipality"].upper(),
				"country": row["iso_country"].upper(),
				"latitude": float(row["latitude_deg"]),
				"longitude": float(row["longitude_deg"]),
				"continent": row["continent"].upper(),
				"keywords": row["keywords"].upper().split(",")[0]
			}
			if len(airport["code"]) == 3 and airport["continent"] == "EU":
				airports.append(airport)
			line_count += 1
		return airports

def get_airports_by_city(cityname, country, distance=150):
	from utils.cities import get_city_by_name

	city = get_city_by_name(cityname, country)
	if not city:
		return None

	city_airports = []
	for airport in get_airports():
		if airport["country"] == city["country"]:
			if get_distance(airport["latitude"], city["latitude"], airport["longitude"], city["longitude"]) <=distance:
				city_airports.append(airport)

	return city_airports

def get_airport_by_code(code):
	for airport in get_airports():
		if airport["code"] == code or airport["keywords"] == code:
			return airport
	return None

airports = get_airports()


import math

def get_last_L2_error(lines,name,LastLines=35) :
   """Get L_2 eror value from a set of lines for the last timestep.
   The set of lines correspond to the output-lines of a flexi-run"""
   for l in lines[-LastLines:] : # read the last XX lines (default is 35)
       # search for name, e.g., "L2_Part" or "L_2"
       if name in l :
           tmp = l.split(":")[1]
   return [float(x) for x in tmp.split()]

def get_last_number_of_timesteps(lines,name,LastLines=35) :
   """Get the number of total timesteps used for the simulation.
   The set of lines correspond to the output-lines of a flexi-run"""
   for l in lines[-LastLines:] : # read the last XX lines (default is 35)
       # search for name, e.g., "#Timesteps"
       if name in l :
           tmp = l.split(":")[1]
   return [float(x) for x in tmp.split()]

def get_initial_timesteps(lines,name) :
   """Get the initial timestep used for the simulation.
   The set of lines correspond to the output-lines of a flexi-run"""
   for l in lines: # read all
       # search for name, e.g., "#Timesteps"
       if name in l :
           tmp = l.split(":")[1]
   return [float(x) for x in tmp.split()]

def get_last_Linf_error(lines,LastLines=35) :
   """Get L_inf eror value from a set of lines for the last timestep
   The set of lines correspond to the output-lines of a flexi-run"""
   for l in lines[-LastLines:] : # read the last XX lines (default is 35)
       if "L_inf" in l :
           tmp = l.split(":")[1]
           return [float(x) for x in tmp.split()]

def get_last_number(lines) :
   for line in reversed(lines) :
       tmp = line.split(' ')
       for t in reversed(tmp) :
           try :
               return float(t)
           except :
               pass

def get_cpu_per_dof(lines) :
   """Get the PID value from a set of lines
   The set of lines correspond to the output-lines of a flexi-run"""
   for line in reversed(lines) :
        if "CALCULATION TIME PER TSTEP/DOF: [" in line :
           return float(line.split("[")[1].split("sec")[0])

def calcOrder_h(h,E,invert_h=False) :
    """Determine the order of convergence for a list of grid spacings h and errors E"""
    if invert_h :
        h = [1./float(elem) for elem in h]
    else :
        h = [float(elem) for elem in h]
    E = [float(elem) for elem in E]
    if len(h) != len(E) :
        return -1

    order = []
    for i in range(1,len(h)) :
        dh=1.0/(h[i]/h[i-1])
        # Check if any error value is exactly zero
        if E[i-1] == 0.0 or E[i] == 0.0:
            order.append(0.0)
        else :
            dE=E[i]/E[i-1]
            order.append(math.log(dE)/math.log(dh))

    return order

def calcOrder_p(p,E) :
    """Determine the order of convergence for a list of polynomial degrees p and errors E"""
    p = [float(elem) for elem in p]
    E = [float(elem) for elem in E]
    if len(p) != len(E) :
        return -1

    order = []
    for i in range(1,len(p)) :
        dp=1.0/((p[i]+1.0)/(p[i-1]+1.0))
        if E[i-1] == 0.0 :
            order.append(0.0)
        else :
            dE=E[i]/E[i-1]
            order.append(math.log(dE)/math.log(dp))

    return order


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

# # Ici on va importer les packages de Python

# In[14]:

import gudhi as gd
import scipy.io as sio
import math
import matplotlib.pyplot as plt
import numpy as np

# # On donne les coordonnées de chaque atome

# In[16]:

coords = {'Ti':[[5,5,5]], 'O':[[5, 5, 10], [5, 10, 5], [10, 5, 5], [5,0,5], [0, 5, 5], [5, 5, 0]]}
coords['Ca'] = [[10, 10, 0], [10, 0, 0], [10, 0, 10], [10, 10, 10], [0, 10, 0], [0, 0, 0], [0, 0, 10], [0, 10, 10]]
data = []
for key, val in coords.items():
    for j in val:
        data.append(j)

mat = np.zeros((len(data), len(data)))
for i in range(len(data)):
    for j in range(len(data)):
        dist = np.linalg.norm(np.array(data[i])-np.array(data[j]))
        mat[i][j] = dist

rips = gd.AlphaComplex(data)
st = rips.create_simplex_tree()
dgmsalpha = st.persistence()

betti0, betti1, betti2 = [], [], []
for r in dgmsalpha:
    if r[0] == 0:
        betti0.append([r[1][0], r[1][1]])
    elif r[0] == 1:
        betti1.append([r[1][0], r[1][1]])
    elif r[0] == 2:
        betti2.append([r[1][0], r[1][1]])

# Using circumradius, we take sqrt of F and multiply by 2
betti0 = np.array(np.sqrt(betti0)*2)
betti1 = np.array(np.sqrt(betti1)*2)
betti2 = np.array(np.sqrt(betti2)*2)
betti = [betti0, betti1, betti2]

betti0 = sorted(betti[0], key=lambda x: x[0])
betti0 = np.flip(betti0, axis=0)
betti1 = sorted(betti[1], key=lambda x: x[0])
betti1 = np.flip(betti1, axis=0)
betti2 = sorted(betti[2], key=lambda x: x[0])
betti2 = np.flip(betti2, axis=0)

sio.savemat("ABX3_gdalpha.mat", {"betti0": betti0, "betti1": betti1, "betti2": betti2})
print("c'est fait !")


from flask import Blueprint, jsonify, render_template,request,flash,redirect,url_for, session
import json
import sqlite3
from numpy import empty
from .excel_data import Device_Excel_Table, get_arr, get_by_ID_from_table
from .location import get_all_location
from .data_processing.index import database_initialization
from .downloadFiles.index import main as download_file
from selenium import webdriver
# from website import excel_data
# from downloadFiles
views = Blueprint('views',__name__)

@views.route('/',methods=['GET','POST'])
def Homepage():
    print("Homepage",request.method)
    if request.method =="GET":
        loc_list = get_all_location()
        return render_template("home.html", loc_list = loc_list)
    if request.method =="POST":
        if request.form.get("selenium"):
            print("in selenium")
            # flash("Downloading the requested files", category="error")
            path = r"C:/Users/Kei Ka Shun/Desktop/project-env/FYP-main/website/downloadFiles/chromedriver.exe"
            driver = webdriver.Chrome(executable_path=path)
            download_file(driver)
            return redirect(url_for("views.Homepage"))
        elif request.form.get("change_folder"):
            print("change_folder")
            timestamp = database_initialization()
            timestamp_json = json.dumps({"timestamp" : str(timestamp)})
            session['timestamp'] = timestamp_json
            return redirect(url_for("views.updated_data")) # timestamp_json=timestamp_json
        elif request.form.get("select_location"):
            print("selection")
            loc = request.form.get("select_location")

            return redirect(url_for("views.location_list", loc=loc))

        elif request.form.get("input_location"):
            print("in input")
            loc = request.form.get("input_location")
            if request.form.get("input_system"):
                print("can get sys")
                sys = request.form.get("input_system")
                if request.form.get("input_device"):
                    print("can get dev")
                    dev = request.form.get("input_device")
                    if request.form.get("input_equip"):
                        print("if loop")
                        equip_no = request.form.get("input_equip")
                        return redirect(url_for("views.get_by_ID", loc= loc, sys= sys, device = dev,eqipID =equip_no))
                    else:
                        print(f"else loop {loc}, {sys} {dev}")
                        return redirect(url_for("views.table_list", loc= loc, sys= sys, device = dev))
                else:
                    print("cannot get dev")
                    flash("Please enter Decive Short Form eg. CTR", category="error")
                    return redirect("/")

            else:
                print("cannot get sys")
                flash("Please enter System Short Form eg. AUS", category="error")
                return redirect(url_for("views.Homepage"))
        else:
            print("cannot get loc")
            flash("Please enter Location Short Form eg. HKBCF_001", category="error")
            return redirect(url_for("views.Homepage"))

@views.route("/change", methods= ['GET','POST'])
def updated_data():
    print("updated data")
    # ts = request.args['timestamp_json']   # counterpart for url_for()
    ts = session['timestamp']
    file = "./change_log.json"

    with open(file, 'r') as f:
        data = json.load(f)

    return render_template("change.html",data=data, timestamp=ts)

@views.route('/<loc>',methods=['GET','POST'])
def location_list(loc):
    if loc == "Location":
        return (redirect(url_for("views.Homepage")))
    if ".db" not in loc:
        loc = loc + ".db"
    loc_no_filetype = loc.split('.')[0]
    sys_arr = get_arr(loc)
    print(f"Method: {request.method} in location list")

    if request.method == "GET":
        print("in location get")
        return render_template("sys.html", sys_arr = sys_arr, Location = loc, location_link = loc_no_filetype)
    if request.method == "POST":
        print("in location post")

        sys = request.form.get("system")
        print(f"sys: {sys}")
        return redirect(url_for("views.system_list", loc= loc_no_filetype, sys= sys)) #, loc = loc_no_filetype, sys = sys, device_arr = device_arr

@views.route('/<loc>/<sys>',methods=['GET','POST'])
def system_list(loc, sys): #loc,sys,device_arr
    print(f"Method: {request.method} {loc} {sys} in system_list")
    if ".db" not in loc:
        loc = loc + ".db"
    loc_no_filetype = loc.split('.')[0]

    device_arr = get_arr(loc,sys)
    if request.method == "GET":
        print(f"in sys_list GET")
        return render_template("device.html", System = sys, device_arr = device_arr)
    if request.method == "POST":
        print("in sys post")
        dev = request.form.get("device")
        return redirect(url_for("views.table_list", loc= loc_no_filetype, sys= sys, device = dev))

@views.route('/<loc>/<sys>/<device>',methods=['GET','POST'])
def table_list(loc, sys, device):
    print(f"Method: {request.method} {loc} {sys} {device} in table_list")
    if request.method =="GET":
        [data, attr_list] = Device_Excel_Table(loc,sys,device)
        return render_template("table.html",
                                data =data ,
                                attr_list = attr_list,
                                loc = loc,
                                sys = sys,
                                device = device
                                )

@views.route('/<loc>/<sys>/<device>/<eqipID>',methods=['GET','POST', 'PUT'])
def get_by_ID(loc, sys, device, eqipID):
    print(f"Method: {request.method} {loc} {sys} {device}  {eqipID}in table_list")
    if request.method =="GET":
        [data, attr_list] = get_by_ID_from_table(loc,sys,device,eqipID)
        return render_template("table.html",
                                data =data ,
                                attr_list = attr_list,
                                loc = loc,
                                sys = sys,
                                device = device
                                )
    if request.method =="POST":
        pass


import sys
from pyspark import SparkConf, SparkContext

import re

from bwt import reverseBwt
from radix import radixSort
from segment import segSort
from default import defaultSort
from partition import partitionSort

# config spark context, set master, name and memory size
def getSC(master, name):
    conf = (SparkConf()
             .setMaster(master)
             .setAppName(name)
             #.set("spark.executor.memory", "1g")
             .set("spark.akka.frameSize", "512")
             )
    sc = SparkContext(conf = conf)

    sc.addPyFile('default.py')
    sc.addPyFile('segment.py')
    sc.addPyFile('radix.py')
    sc.addPyFile('partition.py')
    sc.addPyFile('bwt.py')

    return sc

# select a sort method
def sort(sort_name, reads, threads_number, output_path, prefixes):
    if (sort_name=='radix'):
        bwt = radixSort(reads)
    elif (sort_name=='segment'):
        bwt = segSort(reads)
    elif (sort_name=='partition'):
        bwt = partitionSort(reads, threads_number, output_path, prefixes)
    else:
        bwt = defaultSort(reads, threads_number, output_path, prefixes)
    return bwt

# RDD does not support communications among lines,
# because each line is independent during processing.
# Thus we first collect RDD (RDD->List), then parallelize List (List->RDD)
def collectReads(lines, file_type):
    if file_type == 'fasta' :
        reads = []
        read = ''
        lines = lines.collect()
        #concatinate lines begin with '>'
        for line in lines :
            if '>' not in line:
                read += line
            else :
                if len(read)>0: reads.append(read)
                read = ''
        if len(read)>0: reads.append(read)
    elif file_type == 'fastq' :
        #choose the second line of every four lines
        reads = lines.collect()[1::4]
    else :
        reads = lines.collect()
    return reads

def filterReads(lines, file_type):
    if file_type == 'fasta' :
        reads = lines.filter(lambda line: '>' not in line)
    elif file_type == 'fastq' :
        reads = lines.filter(lambda line: re.match('^[ACGTN]*$', line))
    else :
        reads = lines
    return reads

def getReads(lines, file_type, collect, reads_output_path):
    if collect:
        # collect RDD (RDD->List)
        reads = collectReads(lines,file_type)
        # parallelize List (List->RDD)
        reads = sc.parallelize(reads,int(threads_number))
    else :
        reads = filterReads(lines,file_type)

    # output reads
    # reads.saveAsTextFile(reads_output_path)
    return reads

if __name__ == "__main__":
    if len(sys.argv) < 7:
        print >> sys.stderr, "Usage: <sort> <master> <threads_num> <file_type> <input> <bwt_output_path>"
        exit(-1)

    sort_method = sys.argv[1]
    master_address = sys.argv[2]
    threads_number = sys.argv[3]
    file_type = sys.argv[4]
    input_path = sys.argv[5]
    reads_output_path = ""
    bwt_output_path = sys.argv[6]

    sc = getSC(master_address, sort_method+threads_number+input_path)

    lines = sc.textFile(input_path,int(threads_number))

    reads = getReads(lines,file_type, False, reads_output_path).cache()

    prefixes='$ACGNT'
    #prefixes = ['$','AA','CA','GA','NA','TA','AC','CC','GC','NC','TC','AG','CG','GG','NG','TG','AN','CN','GN','NN','TN','AT','CT','GT','NT','TT']
    # sort suffixes
    bwt = sort(sort_method,reads, int(threads_number), bwt_output_path, prefixes)


class PartyAnimal:
    x = 0
    name = ''

    def __init__(self, name):
        self.name = name
        print('Name:', self.name)

    def party(self):
        self.x = self.x + 1
        print(self.name, 'Party count', self.x)

person1 = PartyAnimal('Amjed')
person1.party()
print('\n')

person2 = PartyAnimal('Danwand')
person2.party()


# Write a Python program to count the occurrences of each word in a given sentence.

string=str(input("Enter a string :"))
words=string.split()                                #store splitted string into variable
count=dict()                                        #initiate a dictionary

for word in words:                                  #initiate loop
    if(word in count):                              #if word is already in dictionary
        count[word]=count[word]+1                   #increment the count
    else:                                           #else keep it 1
        count[word]=1

print(count)


#client berada di sisi remote, client hanya mmebutuhkan
# dependency kepada library Pyro5

import Pyro5.api

if __name__=='__main__':
    # untuk mengecek service apa yang ada di ns, gunakan pyro5-nsc  -p 9900 list
    #dalam kasus ini namanya adalah phonebook.server
    phonebook = Pyro5.api.Proxy('PYRONAME:phonebook.server')
    # untuk melihat daftar dari phonebook
    print(phonebook.list())
    #create record di phonebook
    phonebook.create(dict(nama='Roberto Carlos',alamat='Jambangan',notelp='67829'))
    # untuk melihat daftar dari phonebook, cek kembali
    print(phonebook.list())


from abc import ABC, abstractmethod
from nltk.translate.bleu_score import sentence_bleu
from bert_score import score as bert_score
from BARTScore import bart_score

import argparse

class SimilarityClass(ABC):
    def __init__(self):
        pass

    @abstractmethod
    def get_similarity(self):
        pass

class BLEUSimilarityScore(SimilarityClass):
    def __init__(self):
        super().__init__()

    def get_similarity(self, sentence_1: str, sentence_2: str, gram=None) -> str:
        format_sent_1 = sentence_1.split()
        format_sent_2 = sentence_2.split()
        if gram == None:
            print('BLEU score -> {}'.format(sentence_bleu(format_sent_1, format_sent_2)))
        elif gram == 1:
            print('Individual 1-gram: %f' % sentence_bleu(format_sent_1, format_sent_2, weights=(1, 0, 0, 0)))
        elif gram == 2:
            print('Individual 2-gram: %f' % sentence_bleu(format_sent_1, format_sent_2, weights=(0, 1, 0, 0)))
        elif gram == 3:
            print('Individual 3-gram: %f' % sentence_bleu(format_sent_1, format_sent_2, weights=(0, 0, 1, 0)))
        elif gram == 4:
            print('Individual 4-gram: %f' % sentence_bleu(format_sent_1, format_sent_2, weights=(0, 0, 0, 1)))

class BERTSimilarityScore(SimilarityClass):
    def __init__(self):
        super().__init__()

    def get_similarity(self, sentence_1: str, sentence_2: str) -> str:
        format_sent_1 = [sentence_1]
        format_sent_2 = [sentence_2]
        P, R, F1 = bert_score(format_sent_1, format_sent_2, lang='en', verbose=True)
        print(f"BERT Score: P={P.mean().item():.6f} R={R.mean().item():.6f} F={F1.mean().item():.6f}")

class BART_similarity_score(similarity_class):
    def __init__(self):
        super().__init__()

    def get_similarity(self, sentence_1: str, sentence_2: str, type='ParaBank') -> str:
        format_sent_1 = [sentence_1]
        format_sent_2 = [sentence_2]
        if type == 'ParaBank':
            bart_scorer = bart_score(device='cuda:0', checkpoint='bart.pth')
            bart_scorer.load(path='bart.pth')
            bart_scorer.score(format_sent_1, format_sent_2, batch_size=1)
        elif type == 'CNNDM':
            bart_scorer = bart_score(device='cuda:0', checkpoint='facebook/bart-large-cnn')
            bart_scorer.score(format_sent_1, format_sent_2, batch_size=1) # generation scores from the first list of texts to the second list of texts.

def main(args):
    if args.model == 'BLEU':

        bleu = BLEU_similarity_score()
        bleu.get_similarity("I am good", "You are good")

    if args.model == 'BERT':

        bert = BERT_similarity_score()
        bert.get_similarity("I am good", "You are good")

    if args.model == 'BART':

        bart = BART_similarity_score()
        bart.get_similarity("I am good", "You are good")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str, dest='model', required=True)
    parser.add_argument("--model-specifics", type=str, dest='specifics', required=False)
    args = parser.parse_args()

    main(args)


import os
import pandas as pd
pd.set_option('display.max_columns', None)  # or 1000
pd.set_option('display.max_rows', None)  # or 1000
pd.set_option('display.max_colwidth', None)  # or 199
import numpy as np
import pickle
from tabulate import tabulate

def create_empty_table_lr_micro_macro():
    classifiers = ['logistic_regression']
    cols = pd.MultiIndex.from_product([classifiers, ['accuracy', 'precision_micro', 'recall_micro', 'f1_micro', 'precision_macro', 'recall_macro', 'f1_macro', 'auc']])
    return pd.DataFrame([], columns=cols)

def main_micro_macro():
    classifiers = ['logistic_regression']
    cols = pd.MultiIndex.from_product([classifiers, ['accuracy', 'precision_micro', 'recall_micro', 'f1_micro', 'precision_macro', 'recall_macro', 'f1_macro', 'auc']])
    table = create_empty_table_lr_micro_macro()
    models_to_config_dict = {'20210227-235159_testing-False-ws-200-bs-256-transformations-01-lr-00001-agg-mean': 'Mean aggregate Noise Scaled Transformation',
                            '20210227-235154_testing-False-ws-200-bs-256-transformations-01-lr-00001-agg-std': 'Std aggregate Noise Scaled Transformation',
                            '20210227-235154_testing-False-ws-200-bs-256-transformations-23-lr-00001-agg-mean': 'Mean aggregated Negated Time-Flipped Transformation'}

    # models = next(os.walk(os.getcwd()))[1]
    models = ['20210227-235159_testing-False-ws-200-bs-256-transformations-01-lr-00001-agg-mean',
            '20210227-235154_testing-False-ws-200-bs-256-transformations-01-lr-00001-agg-std',
            '20210227-235154_testing-False-ws-200-bs-256-transformations-23-lr-00001-agg-mean']
    for model in models:
        print(model)
        path_to_chapman_predictions = os.path.join(os.getcwd(), model, 'lr_results.pickle')
        with open(path_to_chapman_predictions, 'rb') as f:
            results = pickle.load(f)

        classifier_metrics_data = [results[0], results[1], results[2], results[3], results[4], results[5], results[6], results[7]]
        np_classifier_metrics_data = np.array(classifier_metrics_data).reshape(1, len(classifier_metrics_data))
        classifier_metrics_dataframe = pd.DataFrame(np_classifier_metrics_data, columns=cols)
        table = table.append(classifier_metrics_dataframe)

    path_to_excel = os.path.join(os.getcwd(), f'heart_rhythm_classifier_results_lr_micro_macro.xlsx')
    model_to_config_index = [models_to_config_dict[model] for model in models]
    table.index = model_to_config_index
    table.to_excel(path_to_excel)

if __name__ == '__main__':
    main_micro_macro()