#! /usr/bin/env python3

def factorialTabulation(n):
  """
  Bottom-up approach to solve factorial
  """
  if n < 0:
    return None
  if n == 1 or n == 0:
    return 1

  tab = [0]* (n+1)
  tab[0]= 1
  tab[1]= 1

  for i in range(1, n+1):
    tab[i] = tab[i-1] * i
  return tab[n]

def factorialRecursive(n):
  if n < 0:
    return None
  if n == 0 or n == 1:
    return 1

  return factorialRecursive(n-1) * n

def factorialMemoization(n):
  """
  Top down approach to solve factorial
  """
  if n < 0:
    return None

  if n == 0:
    return 1

  tab = [0]* (n+1)
  if tab[n] != 0:
    return tab[n]

  tab[n] = factorialMemoization(n-1) * n
  return tab[n]

def main():
  n = int(input("\nFactorial of n: \t"))
  # print(factorialRecursive(n))
  # print(factorialTabulation(n))
  print(factorialMemoization(n))

if __name__=="__main__":
  main()


# 두 개의 정 N면체와 정 M면체의 두 개의 주사위를 던져서
# 나올 수 있는 눈의 합 중 가장 확률이 높은 숫자를 출력하는 프로그램을 작성하세요.
# 정답이 여러 개일 경우 오름차순으로 출력합니다

# 각 주사위의 숫자들을 하나씩 더하면서 모든 경우의 수를 리턴
# 해당 경우의 수 중에 중복 되는 값이 있다면 숫자가 큰 숫자로 출력

n1, n2 = map(int, input().split())
mx = 0
ls = [0]*(n1+n2) # 주사위의 중첩 값을 카운트 해줄 list
result = []

total = []
for i in range(1,n1 + 1) : # 첫번째 주사위가 나올 수 있는 모든 수
    for j in range(1,n2 + 1) : #두번째 주사위가 나올 수 있는 모든 수
        tt = i + j # 각 주사위를 더한 숫자를 tt에 할당
        ls[tt -1] = ls[tt -1] +1 # 각 더한 값들이 들어올때 마다 ls 리스트에 하나씩 카운트 해줌
        # 0, 1, 2, 3, 4, 4, 4, 3, 2, 1 이런 식으로 들어옴
        # ex ) 숫자1 = 0번 중첩, 숫자2 = 1번 중첩, 숫자3 = 2번 중첩, 숫자4 = 3번 중첩, 숫자 5,6,7 = 4번 충첩

# 모든 경우의 수가 담긴 list인 ls를 만들었으니
# 이제부터 해당 ls의 값을 하나씩 빼오면서 가장 높을 경우의 수를 찾을 차례

for i in ls :
    if mx < i : # 이전에 나온 값과 비교해서 현재 값이 크다면
        mx = i  # 현재 값 추가

# 위의 반복문으로 최대 중첩 값을 찾았다면 이제 해당 숫자를 리스트에 저장
for inx, i in enumerate(ls) : # inx = 인덱스 위치 , i = 중첩 횟수
    if i == mx : # 중첩 되는 횟수가 위에서 구한 최대 중첩 값과 같다면 아래 구문 실행
        result.append(inx + 1) # 최종 최대 경우의 수를 담는 list에 해당 숫자를 추가

# 이제 반복문을 통해서 결과를 담은 list를 하나씩 빼오면서 출력
result.sort() # 해당 리스트 정렬

for i in result :
    print(i, end=" ")

2개의 주사위 경우의수 구하기.py

2개의 주사위 경우의수 구하기.py


import os

'''
aabc

'''
def remove_deplicate_num(num_list):
    n = len(num_list)
    if n<=1: return n, num_list

    i=0
    j=1
    while j<n:
        if num_list[i]==num_list[j]:
            j+=1
            continue
        i+=1
        num_list[i] = num_list[j]
        j+=1
    return i+1, num_list[:i+1]

def remove(A):
    length = len(A)
    # trivial
    if length==0 or length==1:
        return length
    # closed pointer, open pointer
    i = 0
    j = 1
    while j<length:
        # find the next non-duplicate:
        if A[i]==A[j]:
            j += 1
            continue  # go to the next iteration
        A[i+1] = A[j]
        i += 1
        j += 1
    return i+1, A[:i+1]

M= [l for l in 'aaabcccddfeggg']
print(remove(M))


# -*- coding: utf-8 -*-
import json

import scrapy
from scrapy import Request, Spider

from zhihuuser.items import UserItem

class ZhihuSpider(Spider):

#定义爬虫名字、种子用户
    name = 'zhihu'
    allowed_domains = ['www.zhihu.com']
    start_urls = ['http://www.zhihu.com/']
    start_user = 'jjmoe'

#请求用户个人信息页，这里user可以用format函数构造字符串来传入不同用户的url_token达到构造不同用户的主页
#user_query为用户主页请求的一些附带信息，加到user_url即可实现请求用户个人信息页（这个用户信息页是知乎的接口，该接口通过ajax请求生成用户信息页，提取该页即可）
    user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
    user_query = 'allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(type=best_answerer)].topics'

#定义用户关注列表url，也是通过传入url_token构造用户，从而获取该用户的关注列表（这个用户列表也是通过接口ajax请求生成用户关注列表）
    follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
    follows_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

#构造初始请求，分别抛出用户信息页和用户关机列表的请求，回调给处理用户信息页和处理用户关注列表函数
    def start_requests(self):
        yield Request(self.user_url.format(user=self.start_user,include=self.user_query),self.parse_user)
        yield Request(self.follows_url.format(user=self.start_user, include=self.follows_query,offset=0,limit=20), self.parse_follows) #这里offset为关注列表的页数导航，limit为每页大小

#处理用户信息页，抛出用户信息item给piplines储存，同时提取用户url_token构造该用户的关注列表，并执行请求，再回调给处理关注列表函数
    def parse_user(self, response):
        result = json.loads(response.text)      #获取用户信息页的信息，由于页面为json信息，所以需要用到json。loads
        item = UserItem()                       #实例化一个item类，这个类有很多fileds字段用于提取用户信息（类似CSS选择器选择对应字段之类）
        for field in item.fields:               #从item获取所有item.fields字段（也就是用户信息的字段）
            if field in result.keys():          #如果字段在用户信息结果里，则将对应用户信息字段的值传给item对应字段（抓取到对应信息）
                item[field] = result.get(field)
        yield item                              #抛出抓取到的item

        yield Request(self.follows_url.format(user=result.get('url_token'),include=self.follows_query,offset=0,limit=20),self.parse_follows)

#处理用户关注列表也，提取列表里用户的url_token，构造成新的用户信息请求，这个请求又抓取新的用户信息和抛出新的关注列表请求，实现递归抓取。并执行当前列表翻页
    def parse_follows(self, response):
        result = json.loads(response.text)

        if 'data' in result.keys():             #判断用户列表有无data字段
            for result in result.get('data'):   #遍历data里面的子字段（也就是用户列表每个用户信息）
                yield  Request(self.user_url.format(user=result.get('url_token'),include=self.user_query),self.parse_user) #抓取用户信息里的url_token，回调给处理用户信息函数，再次爬取用户信息和关注列表

        if 'paging' in result.keys() and result.get('paging').get('is_end') == False:       #通过paging里的is_end字段判断是否为最后一页
            next_page = result.get('paging').get('next')                                    #非最后一页则抓取下一页关注列表，再回调给处理列表函数，实现列表翻页
            yield  Request(next_page,self.parse_follows)

#这个爬虫要区分一个概念，
# 以往的爬虫都是：1请求初始的页面，2提取初始页面的信息，3再去获取初始页面里的链接，4再请求这些链接，提取这些链接的信息和获取新的链接进行层层爬取。这些爬虫是需要进到每个页面才能进一步获取到链接去爬取的
#这次的爬虫是：
#1请求初始用户的信息页（实际是知乎的一个接口，通过ajax请求用户信息），提取初始用户的信息，
#1请求初始用户的关注列表（也是知乎的一个接口，通过ajax请求用户列表），提取关注用户的url_token（普通爬取是直接抓取链接，这里是抓取关键字段重新构造新的请求实现递归）
#实际上是同步进行信息和关注列表的请求的
#2再从关注列表获取新的用户信息页和新的用户的关注列表 这里是直接用第一步的关注列表里直接获取关注用户的url_token去构造新的用户信息页和列表页的url再请求

#测试git
#再次测试


from machine import I2C, Pin
from time import sleep_ms
from pcf8574 import PCF8574
from i2c_lcd import I2cLcd
from keypad import Keypad, Exit

DEFAULT_I2C_ADDR = 60
ADDR = 56

def test_main():
    i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
    lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
    pcf = PCF8574(i2c, ADDR)
    keymap = [
        '123A',
        '456B',
        '789C',
        '*0#D'
    ]
    charmap = {
        '1': '.1',    '2': 'abc2', '3': 'def3',
        '4': 'ghi4',  '5': 'jkl5', '6': 'mno6',
        '7': 'pqrs7', '8': 'tuv8', '9': 'wxyz9',
                      '0': '_0',
    }
    keypad = Keypad(pcf, keymap, charmap)

    lcd.clear()
    lcd.show_cursor()
    try:
        word = keypad.get_word(lcd)
        print("Got {} as input".format(word))
    except Exit:
        print('Received exit')

    lcd.clear()
    try:
        word = keypad.get_word(lcd, True)
        print("Got {} as input".format(word))
    except Exit:
        print('Received exit')

test_main()


import logging
import re

import openpaperwork_core

from . import util

LOGGER = logging.getLogger(__name__)

PAGE_FILENAME_FMT = "paper.{}.jpg"
PAGE_FILENAME_REGEX = re.compile(r"paper\.(\d+)\.jpg")
PAGE_FILE_FORMAT = 'JPEG'
PAGE_QUALITY = 90

class Plugin(openpaperwork_core.PluginBase):
    PRIORITY = 100

    def get_interfaces(self):
        return [
            "doc_type",
            "page_img",
            'pages',
        ]

    def get_deps(self):
        return [
            {
                'interface': 'fs',
                'defaults': ['openpaperwork_gtk.fs.gio'],
            },
            {
                # to provide doc_get_nb_pages_by_url()
                'interface': 'nb_pages',
                'defaults': ['paperwork_backend.model'],
            },
        ]

    def is_doc(self, doc_url):
        page_url = self.core.call_success(
            "fs_join", doc_url, PAGE_FILENAME_FMT.format(1)
        )
        if self.core.call_success("fs_exists", page_url) is None:
            return None
        return True

    def doc_internal_get_mtime_by_url(self, out: list, doc_url):
        mtime = util.get_doc_mtime(self.core, doc_url, PAGE_FILENAME_REGEX)
        if mtime is None:
            return
        out.append(mtime)

    def page_internal_get_mtime_by_url(self, out: list, doc_url, page_idx):
        mtime = util.get_page_mtime(
            self.core, doc_url, page_idx, PAGE_FILENAME_FMT
        )
        if mtime is None:
            return
        out.append(mtime)

    def page_internal_get_hash_by_url(self, out: list, doc_url, page_idx):
        h = util.get_page_hash(self.core, doc_url, page_idx, PAGE_FILENAME_FMT)
        if h is None:
            return
        out.append(h)

    def doc_internal_get_nb_pages_by_url(self, out: list, doc_url):
        nb_pages = util.get_nb_pages(self.core, doc_url, PAGE_FILENAME_REGEX)
        if nb_pages is None:
            return
        out.append(nb_pages)

    def page_get_img_url(self, doc_url, page_idx, write=False):
        if write:
            self.core.call_success("fs_mkdir_p", doc_url)
        page_url = self.core.call_success(
            "fs_join", doc_url, PAGE_FILENAME_FMT.format(page_idx + 1)
        )
        if not write and self.core.call_success("fs_exists", page_url) is None:
            return None
        return page_url

    def page_delete_by_url(self, doc_url, page_idx):
        return util.delete_page_file(
            self.core, PAGE_FILENAME_FMT, doc_url, page_idx
        )

    def page_move_by_url(
                self,
                source_doc_url, source_page_idx,
                dest_doc_url, dest_page_idx
            ):
        return util.move_page_file(
            self.core, PAGE_FILENAME_FMT,
            source_doc_url, source_page_idx,
            dest_doc_url, dest_page_idx
        )


#!/usr/bin/env python3.5
""" Build and train GNN model """

import argparse
import subprocess
import os, sys
import lib.graph_util as graph_util
import lib.impact_module as impact_module
import pandas as pd
import numpy as np

_script_dir = os.path.dirname(os.path.realpath(__file__))

def get_arg_parser():
    """ Build command line parser
    Returns:
        command line parser
    """

    parser = argparse.ArgumentParser(description='Build and train a GNN model using dataset provided.')
    parser.add_argument('--dataset', metavar='dataset.csv', type=str,
                        required=True, dest="dataset",
                        help='Gene expression profiles.')

    parser.add_argument('--trn', metavar='net.tsv', type=str,
                        required=False, dest="net",
                        help='Gene regulatory network.')

    parser.add_argument('--output-model-dir', metavar='./model_dir', type=str,
                        required=False, dest="model_dir", default="./model_dir",
                        help='Directory to save trained GNN model.')

    return parser

def run_net_inference(dataset_filename, model_dir):
    """ Wrapper for GENIE3 gene network inference
    Args:
        dataset_filename: filename containing GE values.
        model_dir: directory name to save generated net_GENIE3.tsv file.
    Returns:
        file path for generated network
    """

    net_filename = "{!s}/net_GENIE3.tsv".format(model_dir)
    GENIE3_script_filename = "{!s}/prep/run_GENIE3_new.R".format(_script_dir)
    command = "{!s} -i {!s} -o {!s}".format(GENIE3_script_filename, dataset_filename, net_filename)
    subprocess.call([command], shell=True)

    return net_filename

def get_mr_scores(net_filename):
    df = pd.read_csv(net_filename, sep="\t")
    if (len(df.columns) == 2):
        df['weight'] = float(1.0)
    df.columns = ['src', 'dst', 'weight']
    df_avg_out = pd.DataFrame(df.groupby(['src'], as_index=False).mean())

    return df_avg_out

def gen_dep_file(net_filename, model_dir):
    impact_m = impact_module.OutWImpact(df_input=get_mr_scores(net_filename))
    graph = graph_util.DirGraphReal(net_filename, impact_m, has_header=True)
    mr_list = graph.get_mr()
    nmr_list = graph.get_nmr()
    dep_filename = "{!s}/net.dep".format(model_dir)
    graph.save_as_linear_dep_graph(mr_list, dep_filename)

    return dep_filename, mr_list, nmr_list

def get_ko_vector(ko_str, nmr_list):
    ko_vector = np.ones(len(nmr_list))

    if type(ko_str) is str and len(ko_str)>0 :
        ko_str_parts = ko_str.split("&")

        for gname in ko_str_parts:
            if gname in nmr_list:
                gidx = nmr_list.index(gname)
                ko_vector[gidx] = 0

    return ko_vector

def get_ko_binary_df(ko_df, nmr_list):
    def get_ko(x):
        return get_ko_vector(x, nmr_list)

    ko_data = ko_df.apply(get_ko)
    df = pd.DataFrame.from_records(ko_data, columns=nmr_list)
    return df

def prep_data(dataset_filename, dep_filename, model_dir, mr_list, nmr_list):
    df = pd.read_csv(dataset_filename)
    train_files = {'mr': "{!s}/train_MR.tsv".format(model_dir),
                   'nmr': "{!s}/train_NMR.tsv".format(model_dir),
                   'ko': "{!s}/train_KO.tsv".format(model_dir) }

    df[mr_list].to_csv(train_files['mr'], sep="\t", index=False)
    df[nmr_list].to_csv(train_files['nmr'], sep="\t", index=False)

    ko_bin_df = get_ko_binary_df(df['KO'], nmr_list)
    ko_bin_df.to_csv(train_files['ko'], sep="\t", index=False)

    return train_files

def train_model(model_dir):
    train_script = "th {!s}/train_GNN.lua {!s}".format(_script_dir, model_dir)
    subprocess.call([train_script], shell=True)

command_args = get_arg_parser().parse_args()

if not os.path.exists(command_args.model_dir):
    os.makedirs(command_args.model_dir)

# 1) generate net.tsv if not given (GENIE3)
net_filename = command_args.net
if net_filename == None:
    net_filename = run_net_inference(command_args.dataset, command_args.model_dir)

# 2) generate net.dep and identify MR genes (save into "model_dir/MR_genes.csv")
dep_filename, mr_list, nmr_list = gen_dep_file(net_filename, command_args.model_dir)

# 3) construct training dataset
prep_data(command_args.dataset, dep_filename, command_args.model_dir, mr_list, nmr_list)

# 4) train GNN model and save into "model_dir/gnn.model"
train_model(command_args.model_dir)


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 22 17:14:10 2023

@author: Micha
"""

###############################################################################
#Libraries
###############################################################################
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines

###############################################################################
#Data process
###############################################################################
#Read excel spreadsheet
sheet = ("Gibbs (Water) (SMD)", "Gibbs (Ethanol) (SMD)", "Gibbs (Acetonitrile) (SMD)")
for i in sheet:

    df = pd.read_excel("Vanillin Data.xlsx", sheet_name=i, index_col="Functional")

    dfPBE0 = df.iloc[9:16, 1:15]

    #Equation for element-wise multiplication (coefficient * energy of molecule)
    def Gibbs_step(x, y):
        G = x*y
        return sum(G)

    ###########################################################################
    #Databases
    ###########################################################################
    ######
    #PBE0#
    ######
    #Collect each step as a list of coefficients and the energies as a seperate list
    step_1_PBE0 = pd.to_numeric(dfPBE0.iloc[0, :])
    step_2_PBE0 = pd.to_numeric(dfPBE0.iloc[1, :])
    step_3_PBE0 = pd.to_numeric(dfPBE0.iloc[2, :])
    step_4_PBE0 = pd.to_numeric(dfPBE0.iloc[3, :])
    step_5_PBE0 = pd.to_numeric(dfPBE0.iloc[4, :])
    # PBE0energy = pd.to_numeric(dfPBE0.iloc[5, :])
    PBE0energy = pd.to_numeric(dfPBE0.iloc[6, :])

    #Find relative energy level for each reaction step
    PBE0G = Gibbs_step(step_1_PBE0, PBE0energy)
    PBE0G1 = (Gibbs_step(step_1_PBE0, PBE0energy)) - PBE0G
    PBE0G2 = Gibbs_step(step_2_PBE0, PBE0energy) - PBE0G
    PBE0G3 = Gibbs_step(step_3_PBE0, PBE0energy) - PBE0G
    PBE0G4 = Gibbs_step(step_4_PBE0, PBE0energy) - PBE0G
    PBE0G5 = Gibbs_step(step_5_PBE0, PBE0energy) - PBE0G

    #Find energy difference for each reaction step
    PBE0DG1 = round((PBE0G2 - PBE0G1),2)
    PBE0DG2 = round((PBE0G3 - PBE0G2),2)
    PBE0DG3 = round((PBE0G4 - PBE0G3),2)
    PBE0DG4 = round((PBE0G5 - PBE0G4),2)

    if i == "Gibbs (Water) (SMD)":
        PBE0WaterG = PBE0G
        PBE0WaterG1 = PBE0G1
        PBE0WaterG2 = PBE0G2
        PBE0WaterG3 = PBE0G3
        PBE0WaterG4 = PBE0G4
        PBE0WaterG5 = PBE0G5

        PBE0WaterDG1 = PBE0DG1
        PBE0WaterDG2 = PBE0DG2
        PBE0WaterDG3 = PBE0DG3
        PBE0WaterDG4 = PBE0DG4

    elif i == "Gibbs (Ethanol) (SMD)":
        PBE0EthanolG = PBE0G
        PBE0EthanolG1 = PBE0G1
        PBE0EthanolG2 = PBE0G2
        PBE0EthanolG3 = PBE0G3
        PBE0EthanolG4 = PBE0G4
        PBE0EthanolG5 = PBE0G5

        PBE0EthanolDG1 = PBE0DG1
        PBE0EthanolDG2 = PBE0DG2
        PBE0EthanolDG3 = PBE0DG3
        PBE0EthanolDG4 = PBE0DG4

    elif i == "Gibbs (Acetonitrile) (SMD)":
        PBE0AcetonitrileG = PBE0G
        PBE0AcetonitrileG1 = PBE0G1
        PBE0AcetonitrileG2 = PBE0G2
        PBE0AcetonitrileG3 = PBE0G3
        PBE0AcetonitrileG4 = PBE0G4
        PBE0AcetonitrileG5 = PBE0G5

        PBE0AcetonitrileDG1 = PBE0DG1
        PBE0AcetonitrileDG2 = PBE0DG2
        PBE0AcetonitrileDG3 = PBE0DG3
        PBE0AcetonitrileDG4 = PBE0DG4

sheet = ("Gibbs (Water)", "Gibbs (Ethanol)", "Gibbs (Acetonitrile)")
for i in sheet:

    df = pd.read_excel("Z:/Michael Nicolaou/Projects/Vanillin-veratric acid/Workbench.xlsx", sheet_name=i, index_col="Functional")

    dfPBE0 = df.iloc[9:16, 1:15]

    #Equation for element-wise multiplication (coefficient * energy of molecule)
    def Gibbs_step(x, y):
        G = x*y
        return sum(G)

    ###########################################################################
    #Databases
    ###########################################################################
    ######
    #PBE0#
    ######
    #Collect each step as a list of coefficients and the energies as a seperate list
    step_1_PBE0 = pd.to_numeric(dfPBE0.iloc[0, :])
    step_2_PBE0 = pd.to_numeric(dfPBE0.iloc[1, :])
    step_3_PBE0 = pd.to_numeric(dfPBE0.iloc[2, :])
    step_4_PBE0 = pd.to_numeric(dfPBE0.iloc[3, :])
    step_5_PBE0 = pd.to_numeric(dfPBE0.iloc[4, :])
    # PBE0energy = pd.to_numeric(dfPBE0.iloc[5, :])
    PBE0energy = pd.to_numeric(dfPBE0.iloc[6, :])

    #Find relative energy level for each reaction step
    if i == "Gibbs (Water)":
        PBE0G = Gibbs_step(step_1_PBE0, PBE0energy)
        PBE0G1 = (Gibbs_step(step_1_PBE0, PBE0energy)) - PBE0WaterG
        PBE0G2 = Gibbs_step(step_2_PBE0, PBE0energy) - PBE0WaterG
        PBE0G3 = Gibbs_step(step_3_PBE0, PBE0energy) - PBE0WaterG
        PBE0G4 = Gibbs_step(step_4_PBE0, PBE0energy) - PBE0WaterG
        PBE0G5 = Gibbs_step(step_5_PBE0, PBE0energy) - PBE0WaterG
    elif i == "Gibbs (Ethanol)":
        PBE0G = Gibbs_step(step_1_PBE0, PBE0energy)
        PBE0G1 = (Gibbs_step(step_1_PBE0, PBE0energy)) - PBE0EthanolG
        PBE0G2 = Gibbs_step(step_2_PBE0, PBE0energy) - PBE0EthanolG
        PBE0G3 = Gibbs_step(step_3_PBE0, PBE0energy) - PBE0EthanolG
        PBE0G4 = Gibbs_step(step_4_PBE0, PBE0energy) - PBE0EthanolG
        PBE0G5 = Gibbs_step(step_5_PBE0, PBE0energy) - PBE0EthanolG
    elif i == "Gibbs (Acetonitrile)":
        PBE0G = Gibbs_step(step_1_PBE0, PBE0energy)
        PBE0G1 = (Gibbs_step(step_1_PBE0, PBE0energy)) - PBE0AcetonitrileG
        PBE0G2 = Gibbs_step(step_2_PBE0, PBE0energy) - PBE0AcetonitrileG
        PBE0G3 = Gibbs_step(step_3_PBE0, PBE0energy) - PBE0AcetonitrileG
        PBE0G4 = Gibbs_step(step_4_PBE0, PBE0energy) - PBE0AcetonitrileG
        PBE0G5 = Gibbs_step(step_5_PBE0, PBE0energy) - PBE0AcetonitrileG

    #Find energy difference for each reaction step
    PBE0DG1 = round((PBE0G2 - PBE0G1),2)
    PBE0DG2 = round((PBE0G3 - PBE0G2),2)
    PBE0DG3 = round((PBE0G4 - PBE0G3),2)
    PBE0DG4 = round((PBE0G5 - PBE0G4),2)

    if i == "Gibbs (Water)":
        PBE0WaterPCMG1 = PBE0G1
        PBE0WaterPCMG2 = PBE0G2
        PBE0WaterPCMG3 = PBE0G3
        PBE0WaterPCMG4 = PBE0G4
        PBE0WaterPCMG5 = PBE0G5

        PBE0WaterPCMDG1 = PBE0DG1
        PBE0WaterPCMDG2 = PBE0DG2
        PBE0WaterPCMDG3 = PBE0DG3
        PBE0WaterPCMDG4 = PBE0DG4

    elif i == "Gibbs (Ethanol)":
        PBE0EthanolPCMG1 = PBE0G1
        PBE0EthanolPCMG2 = PBE0G2
        PBE0EthanolPCMG3 = PBE0G3
        PBE0EthanolPCMG4 = PBE0G4
        PBE0EthanolPCMG5 = PBE0G5

        PBE0EthanolPCMDG1 = PBE0DG1
        PBE0EthanolPCMDG2 = PBE0DG2
        PBE0EthanolPCMDG3 = PBE0DG3
        PBE0EthanolPCMDG4 = PBE0DG4

    elif i == "Gibbs (Acetonitrile)":
        PBE0AcetonitrilePCMG1 = PBE0G1
        PBE0AcetonitrilePCMG2 = PBE0G2
        PBE0AcetonitrilePCMG3 = PBE0G3
        PBE0AcetonitrilePCMG4 = PBE0G4
        PBE0AcetonitrilePCMG5 = PBE0G5

        PBE0AcetonitrilePCMDG1 = PBE0DG1
        PBE0AcetonitrilePCMDG2 = PBE0DG2
        PBE0AcetonitrilePCMDG3 = PBE0DG3
        PBE0AcetonitrilePCMDG4 = PBE0DG4

###############################################################################
#Plots
###############################################################################
#Define axes
xaxis = (1, 2, 3, 4, 5)
PBE0Wateryaxis = (PBE0WaterG1, PBE0WaterG2, PBE0WaterG3, PBE0WaterG4, PBE0WaterG5)
PBE0Ethanolyaxis = (PBE0EthanolG1, PBE0EthanolG2, PBE0EthanolG3, PBE0EthanolG4, PBE0EthanolG5)
PBE0Acetonitrileyaxis = (PBE0AcetonitrileG1, PBE0AcetonitrileG2, PBE0AcetonitrileG3, PBE0AcetonitrileG4, PBE0AcetonitrileG5)
PBE0WaterPCMyaxis = (PBE0WaterPCMG1, PBE0WaterPCMG2, PBE0WaterPCMG3, PBE0WaterPCMG4, PBE0WaterPCMG5)
PBE0EthanolPCMyaxis = (PBE0EthanolPCMG1, PBE0EthanolPCMG2, PBE0EthanolPCMG3, PBE0EthanolPCMG4, PBE0EthanolPCMG5)
PBE0AcetonitrilePCMyaxis = (PBE0AcetonitrilePCMG1, PBE0AcetonitrilePCMG2, PBE0AcetonitrilePCMG3, PBE0AcetonitrilePCMG4, PBE0AcetonitrilePCMG5)

#Plot figure
#Add plot
fig, ax = plt.subplots(figsize = (20,10))

#Plot font
plt.rcParams['font.family'] = "serif"
plt.rcParams.update({'font.size': 18})

#Remove main plot default axis ticks
plt.yticks([])
plt.xticks([])

#Remove main plot frame
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

#Adjust subplot positions
# plt.subplots_adjust(bottom = -0.2, left = 0, top = 1., right = 1)
sub1 = fig.add_subplot(3, 3, (1, 3))
sub2 = fig.add_subplot(3, 3, (4, 6))
sub3 = fig.add_subplot(3, 3, (7, 9))

#Plot for each step's energy level
######
#BP86#
######
#Scatterplot
sub1.scatter(xaxis, PBE0Wateryaxis, s = 2000, c = "blue", marker = "_", linewidths = 3)
sub1.scatter(xaxis, PBE0WaterPCMyaxis, s = 2000, c = "cyan", marker = "_", linewidths = 3)

sub2.scatter(xaxis, PBE0Ethanolyaxis, s = 2000, c = "blue", marker = "_", linewidths = 3)
sub2.scatter(xaxis, PBE0EthanolPCMyaxis, s = 2000, c = "cyan", marker = "_", linewidths = 3)

sub3.scatter(xaxis, PBE0Acetonitrileyaxis, s = 2000, c = "blue", marker = "_", linewidths = 3)
sub3.scatter(xaxis, PBE0AcetonitrilePCMyaxis, s = 2000, c = "cyan", marker = "_", linewidths = 3)

#Energy change line
sub1.plot((1.08, 1.92), (PBE0WaterG1, PBE0WaterG2), c = "blue", linestyle = "dashed")
sub1.plot((2.08, 2.92), (PBE0WaterG2, PBE0WaterG3), c = "blue", linestyle = "dashed")
sub1.plot((3.08, 3.92), (PBE0WaterG3, PBE0WaterG4), c = "blue", linestyle = "dashed")
sub1.plot((4.08, 4.92), (PBE0WaterG4, PBE0WaterG5), c = "blue", linestyle = "dashed")

sub1.plot((1.08, 1.92), (PBE0WaterPCMG1, PBE0WaterPCMG2), c = "cyan", linestyle = "dashed")
sub1.plot((2.08, 2.92), (PBE0WaterPCMG2, PBE0WaterPCMG3), c = "cyan", linestyle = "dashed")
sub1.plot((3.08, 3.92), (PBE0WaterPCMG3, PBE0WaterPCMG4), c = "cyan", linestyle = "dashed")
sub1.plot((4.08, 4.92), (PBE0WaterPCMG4, PBE0WaterPCMG5), c = "cyan", linestyle = "dashed")

sub2.plot((1.08, 1.92), (PBE0EthanolG1, PBE0EthanolG2), c = "blue", linestyle = "dashed")
sub2.plot((2.08, 2.92), (PBE0EthanolG2, PBE0EthanolG3), c = "blue", linestyle = "dashed")
sub2.plot((3.08, 3.92), (PBE0EthanolG3, PBE0EthanolG4), c = "blue", linestyle = "dashed")
sub2.plot((4.08, 4.92), (PBE0EthanolG4, PBE0EthanolG5), c = "blue", linestyle = "dashed")

sub2.plot((1.08, 1.92), (PBE0EthanolPCMG1, PBE0EthanolPCMG2), c = "cyan", linestyle = "dashed")
sub2.plot((2.08, 2.92), (PBE0EthanolPCMG2, PBE0EthanolPCMG3), c = "cyan", linestyle = "dashed")
sub2.plot((3.08, 3.92), (PBE0EthanolPCMG3, PBE0EthanolPCMG4), c = "cyan", linestyle = "dashed")
sub2.plot((4.08, 4.92), (PBE0EthanolPCMG4, PBE0EthanolPCMG5), c = "cyan", linestyle = "dashed")

sub3.plot((1.08, 1.92), (PBE0AcetonitrileG1, PBE0AcetonitrileG2), c = "blue", linestyle = "dashed")
sub3.plot((2.08, 2.92), (PBE0AcetonitrileG2, PBE0AcetonitrileG3), c = "blue", linestyle = "dashed")
sub3.plot((3.08, 3.92), (PBE0AcetonitrileG3, PBE0AcetonitrileG4), c = "blue", linestyle = "dashed")
sub3.plot((4.08, 4.92), (PBE0AcetonitrileG4, PBE0AcetonitrileG5), c = "blue", linestyle = "dashed")

sub3.plot((1.08, 1.92), (PBE0AcetonitrilePCMG1, PBE0AcetonitrilePCMG2), c = "cyan", linestyle = "dashed")
sub3.plot((2.08, 2.92), (PBE0AcetonitrilePCMG2, PBE0AcetonitrilePCMG3), c = "cyan", linestyle = "dashed")
sub3.plot((3.08, 3.92), (PBE0AcetonitrilePCMG3, PBE0AcetonitrilePCMG4), c = "cyan", linestyle = "dashed")
sub3.plot((4.08, 4.92), (PBE0AcetonitrilePCMG4, PBE0AcetonitrilePCMG5), c = "cyan", linestyle = "dashed")

#Plot font
plt.rcParams['font.family'] = "serif"
plt.rcParams.update({'font.size': 18})

#Axis labels
sub1.set_ylabel("Gibbs free energy (kcal mol$^{-1}$)", fontsize = 12)
sub2.set_ylabel("Gibbs free energy (kcal mol$^{-1}$)", fontsize = 12)
sub3.set_ylabel("Gibbs free energy (kcal mol$^{-1}$)", fontsize = 12)

#Scatter labels
sub1.text(0.95, (float(PBE0WaterPCMG1)+3), round(float(PBE0WaterPCMG1),1), color = "cyan", fontsize = 12)
sub1.text(1.945, (float(PBE0WaterPCMG2)+3), round(float(PBE0WaterPCMG2),1), color = "cyan", fontsize = 12)
sub1.text(2.965, (float(PBE0WaterPCMG3)+3), round(float(PBE0WaterPCMG3),1), color = "cyan", fontsize = 12)
sub1.text(3.94, (float(PBE0WaterPCMG4)+3), round(float(PBE0WaterPCMG4),1), color = "cyan", fontsize = 12)
sub1.text(4.94, (float(PBE0WaterPCMG5)+3), round(float(PBE0WaterPCMG5),1), color = "cyan", fontsize = 12)

sub1.text(0.96, (float(PBE0WaterG1)-9.5), round(float(PBE0WaterG1),1), color = "blue", fontsize = 12)
sub1.text(1.96, (float(PBE0WaterG2)-9.5), round(float(PBE0WaterG2),1), color = "blue", fontsize = 12)
sub1.text(2.95, (float(PBE0WaterG3)-9.5), round(float(PBE0WaterG3),1), color = "blue", fontsize = 12)
sub1.text(3.94, (float(PBE0WaterG4)-9.5), round(float(PBE0WaterG4),1), color = "blue", fontsize = 12)
sub1.text(4.94, (float(PBE0WaterG5)-9.5), round(float(PBE0WaterG5),1), color = "blue", fontsize = 12)

sub2.text(0.95, (float(PBE0EthanolPCMG1)+3), round(float(PBE0EthanolPCMG1),1), color = "cyan", fontsize = 12)
sub2.text(1.945, (float(PBE0EthanolPCMG2)+3), round(float(PBE0EthanolPCMG2),1), color = "cyan", fontsize = 12)
sub2.text(2.945, (float(PBE0EthanolPCMG3)+3), round(float(PBE0EthanolPCMG3),1), color = "cyan", fontsize = 12)
sub2.text(3.94, (float(PBE0EthanolPCMG4)+3), round(float(PBE0EthanolPCMG4),1), color = "cyan", fontsize = 12)
sub2.text(4.94, (float(PBE0EthanolPCMG5)+3), round(float(PBE0EthanolPCMG5),1), color = "cyan", fontsize = 12)

sub2.text(0.96, (float(PBE0EthanolG1)-9.5), round(float(PBE0EthanolG1),1), color = "blue", fontsize = 12)
sub2.text(1.96, (float(PBE0EthanolG2)-9.5), round(float(PBE0EthanolG2),1), color = "blue", fontsize = 12)
sub2.text(2.95, (float(PBE0EthanolG3)-9.5), round(float(PBE0EthanolG3),1), color = "blue", fontsize = 12)
sub2.text(3.94, (float(PBE0EthanolG4)-9.5), round(float(PBE0EthanolG4),1), color = "blue", fontsize = 12)
sub2.text(4.94, (float(PBE0EthanolG5)-9.5), round(float(PBE0EthanolG5),1), color = "blue", fontsize = 12)

sub3.text(0.95, (float(PBE0AcetonitrilePCMG1)+3), round(float(PBE0AcetonitrilePCMG1),1), color = "cyan", fontsize = 12)
sub3.text(1.945, (float(PBE0AcetonitrilePCMG2)+3), round(float(PBE0AcetonitrilePCMG2),1), color = "cyan", fontsize = 12)
sub3.text(2.967, (float(PBE0AcetonitrilePCMG3)+3), round(float(PBE0AcetonitrilePCMG3),1), color = "cyan", fontsize = 12)
sub3.text(3.94, (float(PBE0AcetonitrilePCMG4)+3), round(float(PBE0AcetonitrilePCMG4),1), color = "cyan", fontsize = 12)
sub3.text(4.94, (float(PBE0AcetonitrilePCMG5)+3), round(float(PBE0AcetonitrilePCMG5),1), color = "cyan", fontsize = 12)

sub3.text(0.96, (float(PBE0AcetonitrileG1)-9.5), round(float(PBE0AcetonitrileG1),1), color = "blue", fontsize = 12)
sub3.text(1.96, (float(PBE0AcetonitrileG2)-9.5), round(float(PBE0AcetonitrileG2),1), color = "blue", fontsize = 12)
sub3.text(2.95, (float(PBE0AcetonitrileG3)-9.5), round(float(PBE0AcetonitrileG3),1), color = "blue", fontsize = 12)
sub3.text(3.94, (float(PBE0AcetonitrileG4)-9.5), round(float(PBE0AcetonitrileG4),1), color = "blue", fontsize = 12)
sub3.text(4.94, (float(PBE0AcetonitrileG5)-9.5), round(float(PBE0AcetonitrileG5),1), color = "blue", fontsize = 12)

#Ticks
sub1.set_xticks([])
sub2.set_xticks([])
sub3.set_xticks([])
#Limits
sub1.set_ylim([-90, 30])
sub2.set_ylim([-90, 30])
sub3.set_ylim([-90, 30])

#Ticks
sub1.set_yticks([20, 10, 0, -10, -20, -30, -40, -50, -60, -70 , -80])
sub2.set_yticks([20, 10, 0, -10, -20, -30, -40, -50, -60, -70 , -80])
sub3.set_yticks([20, 10, 0, -10, -20, -30, -40, -50, -60, -70 , -80])

#Plot titles
sub1.text(0.84, -82.5, "Water", bbox = {'facecolor': "white", "pad": 4}, fontsize = 20)
sub2.text(0.84, -82.5, "Ethanol", bbox = {'facecolor': "white", "pad": 4}, fontsize = 20)
sub3.text(0.84, -82.5, "Acetonitrile", bbox = {'facecolor': "white", "pad": 4}, fontsize = 20)

#Colour parts of plots
sub1.fill_between((1, 2), -100, 30, facecolor = "green", alpha = 0.1)
sub1.fill_between((3, 4), -100, 30, facecolor = "blue", alpha = 0.1)
sub1.fill_between((4, 5), -100, 30, facecolor = "orange", alpha = 0.1)

sub2.fill_between((1, 2), -100, 30, facecolor = "green", alpha = 0.1)
sub2.fill_between((3, 4), -100, 30, facecolor = "blue", alpha = 0.1)
sub2.fill_between((4, 5), -100, 30, facecolor = "orange", alpha = 0.1)

sub3.fill_between((1, 2), -100, 30, facecolor = "green", alpha = 0.1)
sub3.fill_between((3, 4), -100, 30, facecolor = "blue", alpha = 0.1)
sub3.fill_between((4, 5), -100, 30, facecolor = "orange", alpha = 0.1)

#Legend
PBE0Waterleg = mlines.Line2D([], [], color = "blue", linestyle = "dashed", label = "SMD")
PBE0Ethanolleg = mlines.Line2D([], [], color = "blue", linestyle = "dashed", label = "SMD")
PBE0Acetonitrileleg = mlines.Line2D([], [], color = "blue", linestyle = "dashed", label = "SMD")
PBE0WaterPCMleg = mlines.Line2D([], [], color = "cyan", linestyle = "dashed", label = "PCM")
PBE0EthanolPCMleg = mlines.Line2D([], [], color = "cyan", linestyle = "dashed", label = "PCM")
PBE0AcetonitrilePCMleg = mlines.Line2D([], [], color = "cyan", linestyle = "dashed", label = "PCM")
Waterlegends = (PBE0Waterleg, PBE0WaterPCMleg)
Ethanollegends = (PBE0Ethanolleg, PBE0EthanolPCMleg)
Acetonitrilelegends = (PBE0Acetonitrileleg, PBE0AcetonitrilePCMleg)
sub1.legend(handles=Waterlegends, loc="upper right")
sub2.legend(handles=Ethanollegends, loc="upper right")
sub3.legend(handles=Acetonitrilelegends, loc="upper right")

plt.savefig("Solvation models PBE0.png", format = "png", bbox_inches='tight', dpi = 300)
plt.show()

Vanillin analysis/Figure (Energy profile - Solvation model - PBE0).py


import discord

client = discord.Client()

@client.event
async def on_ready():
    print("Ready!")
    print(f'Logged in as {client.user} on {len(client.guilds)} servers')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    channel = discord.utils.get(message.guild.channels, name="no")
    if not message.content == no:
        await message.delete()
        await channel.send(f"no")

client.run('')


import numpy as np
from primeqa.ir.dense.dpr_top.dpr.simple_mmap_dataset import Corpus
from primeqa.ir.dense.dpr_top.util.args_help import fill_from_args
import faiss
from primeqa.ir.dense.dpr_top.util.reporting import Reporting
import os
import time
import math
import logging

logger = logging.getLogger(__name__)

def l2_convert_indexed_vectors(vectors: np.ndarray, max_norm_sqrd: float):
    norms = np.linalg.norm(vectors, axis=1)
    aux_dims_sqrd = max_norm_sqrd - norms * norms
    if np.min(aux_dims_sqrd) < 0:
        print(f'WARNING: max_norm_sqrd = {max_norm_sqrd} but it was less '
              f'({np.min(aux_dims_sqrd)}) than a vectors norm_sqrd')
        aux_dims_sqrd = np.maximum(aux_dims_sqrd, 0)
    aux_dims = np.sqrt(aux_dims_sqrd)
    converted_vectors = np.hstack((vectors, aux_dims.reshape(-1, 1)))
    return converted_vectors

def l2_convert_query_vectors(vectors: np.ndarray):
    aux_dim = np.zeros(vectors.shape[0], dtype=np.float32)
    converted_vectors = np.hstack((vectors, aux_dim.reshape(-1, 1)))
    return converted_vectors

class ANNIndex:
    def __init__(self, index_file):
        self.index = faiss.read_index(index_file)
        self.is_l2 = type(self.index) == faiss.IndexHNSWSQ

    def search(self, query_vectors, k):
        if self.is_l2:
            query_vectors = l2_convert_query_vectors(query_vectors)
        scores, indexes = self.index.search(query_vectors, k)
        if self.is_l2:
            scores = -1 * scores  # make higher scores better always
        return scores, indexes

    def dim(self):
        if self.is_l2:
            return self.index.d - 1
        else:
            return self.index.d

class IndexOptions():
    def __init__(self):
        self.d = 768
        self.m = 128
        self.ef_search = 128
        self.ef_construction = 200
        self.index_batch_size = 100000
        self.scalar_quantizer = -1
        self.product_quantizer_m = -1  # suggested values of 8, 16, 32 - maybe we need much higher though
        self.product_quantizer_sv_bits = 8  # probably don't change this
        self.is_l2 = False
        self.num_vectors = -1
        self.max_norm = -1

    def _post_argparse(self):
        self.is_l2 = self.scalar_quantizer > 0

def build_index(corpus_dir, output_file, opts: IndexOptions):
    logger.info(f'building index, reading data from {corpus_dir}, writing to {output_file}')
    corpus = Corpus(corpus_dir)
    if opts.is_l2:
        print(f'Using L2 distance conversion')

    if (opts.num_vectors <= 0 and opts.product_quantizer_m > 0) or (opts.max_norm <= 0 and opts.is_l2):
        max_norm = 0
        num_vectors = 0
        start_time = time.time()
        for psg in corpus:
            vector = psg['vector']
            max_norm = max(max_norm, np.linalg.norm(vector))
            num_vectors += 1
        print(f'found max norm = {max_norm} over {num_vectors} vectors in {(time.time()-start_time)/60} min.')
        opts.max_norm = max_norm
        opts.num_vectors = num_vectors
    max_norm_sqrd = opts.max_norm * opts.max_norm

    opts.is_trained = False
    if opts.scalar_quantizer > 0:
        if opts.scalar_quantizer == 16:
            sq = faiss.ScalarQuantizer.QT_fp16
        elif opts.scalar_quantizer == 8:
            sq = faiss.ScalarQuantizer.QT_8bit
        elif opts.scalar_quantizer == 4:
            sq = faiss.ScalarQuantizer.QT_4bit
        elif opts.scalar_quantizer == 6:
            sq = faiss.ScalarQuantizer.QT_6bit
        else:
            raise ValueError(f'unknown --scalar_quantizer {opts.scalar_quantizer}')
        # see https://github.com/facebookresearch/faiss/blob/master/benchs/bench_hnsw.py
        index = faiss.IndexHNSWSQ(opts.d+1, sq, opts.m)
        index.hnsw.efSearch = opts.ef_search
        index.hnsw.efConstruction = opts.ef_construction
    elif opts.product_quantizer_m > 0:
        # product quant: https://github.com/matsui528/faiss_tips
        # seems awful - maybe product_quantizer_m should be much higher?
        nlist = int(math.sqrt(opts.num_vectors))
        quantizer = faiss.IndexHNSWFlat(opts.d, opts.m, faiss.METRIC_INNER_PRODUCT)
        index = faiss.IndexIVFPQ(quantizer, opts.d, nlist, opts.product_quantizer_m,
                                 opts.product_quantizer_sv_bits, faiss.METRIC_INNER_PRODUCT)
        index.nprobe = opts.ef_search  # matsui528 recommended 8
        opts.index_batch_size = max(opts.index_batch_size, 50 * nlist)
    else:
        index = faiss.IndexHNSWFlat(opts.d, opts.m, faiss.METRIC_INNER_PRODUCT)
        # defaults are 16 and 40
        index.hnsw.efSearch = opts.ef_search
        index.hnsw.efConstruction = opts.ef_construction
        opts.is_trained = True  # doesn't need training

    vectors = np.zeros((opts.index_batch_size, opts.d), dtype=np.float32)
    vector_ndx = 0

    def add_to_index(vectors):
        if opts.is_l2:
            to_index = l2_convert_indexed_vectors(vectors, max_norm_sqrd)
        else:
            to_index = vectors
        if not opts.is_trained:
            index.train(to_index)
            opts.is_trained = True
        logger.info(f'calling index.add with {len(to_index)} vectors')
        index.add(to_index)

    report = Reporting()

    for pndx, psg in enumerate(corpus):
        if (pndx % 100000) == 0:
            logger.info(f'processed {pndx} passages')
        if report.is_time():
            print(report.progress_str(instance_name='vector'))
        vector = psg['vector']
        vectors[vector_ndx] = vector
        vector_ndx += 1
        if vector_ndx == opts.index_batch_size:
            logger.info(f'processed {pndx} passages')
            add_to_index(vectors)
            vector_ndx = 0
    if vector_ndx > 0:
        add_to_index(vectors[:vector_ndx])
    logger.info(f'processed {len(corpus)} passages')
    logger.info(f'finished building index, writing index file to {output_file}')
    #print(f'finished building index, writing index file to {output_file}')
    faiss.write_index(index, output_file)
    logger.info(f'took {report.elapsed_time_str()}')
    #print(f'took {report.elapsed_time_str()}')

if __name__ == "__main__":
    class CmdOptions(IndexOptions):
        def __init__(self):
            super().__init__()
            self.collection = ''  # can be a directory with passages*.json.gz.records or a single such file
            self.__required_args__ = ['collection']

    opts = CmdOptions()
    fill_from_args(opts)

    if os.path.isdir(opts.collection):
        output_file = os.path.join(opts.collection, 'index.faiss')
    else:
        base_dir, filename = os.path.split(opts.collection)
        assert filename.startswith('passages') and filename.endswith('.json.gz.records')
        index_fname = f'index{filename[len("passages"):-len(".json.gz.records")]}.faiss'
        output_file = os.path.join(base_dir, index_fname)

    build_index(opts.collection, output_file, opts)


#!/usr/bin/python

"""
ZetCode PyQt6 tutorial

This example shows a QCalendarWidget widget.

Author: Jan Bodnar
Website: zetcode.com
"""

from PyQt6.QtWidgets import (QWidget, QCalendarWidget,
        QLabel, QApplication, QVBoxLayout)
from PyQt6.QtCore import QDate
import sys

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        vbox = QVBoxLayout(self)

        cal = QCalendarWidget(self)
        cal.setGridVisible(True)
        cal.clicked[QDate].connect(self.showDate)

        vbox.addWidget(cal)

        self.lbl = QLabel(self)
        date = cal.selectedDate()
        self.lbl.setText(date.toString())

        vbox.addWidget(self.lbl)

        self.setLayout(vbox)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Calendar')
        self.show()

    def showDate(self, date):
        self.lbl.setText(date.toString())

def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()


################# Functions descriptions #################
##########################################################
## This file contains functions number: 2,4,6 and 7.
## 2. clean_data -> input are: df1, df2, df3, df4 and output are: df1, df2, df3, df4.
##              All df's are cleaned, filtered, column names changed etc.
##              all dfs here are grouped by zipcode and new fields calculated.
## 4. clean_df5 -> input is df5 (after intersection) and output is df5. After
##                  intersecting the accidents we got many rows, they are grouped
##                  and groups changed into columns.
## 6. fill_nas  -> very short function, just fill NA values by zero's.
##                  Input is merged data of all dfs (1,2,3,4,5)
## 7. check_nas -> function without output. If any NA spotted in data, it will
##                 print this column in the console.

###################################### Imports ###############################
import logging

logging.basicConfig(level=logging.INFO)
logging=logging.getLogger()

############################### Function 2 ###################################
def clean_data(df1,df2,df3,df4):

    logging.info("Let's do some cleaning")

    ###################### Cleaning process of df1 #####################
    #### 2014 Housing Market Analysis Data by Zip Code ####
    #Unfortunately, one zip-code is missing. Drop this row.
    df1.count()
    #Zip Code has NA, delete NA only if NA is found in column Zip Code
    df1.dropna(subset=['Zip Code'], inplace = True)
    # Change data type
    df1["Zip Code"] = df1["Zip Code"].astype(int)
    df1.dtypes
    # After droping one Zipcode, some other columns have NA's
    # we don't want to delete them so we input zero in NA by columns:
    df1[df1['Homes affordable to people earning less than $50,000'].isnull()]
    df1['Homes affordable to people earning less than $50,000'].fillna(0, inplace=True)

    df1[df1['Owner units affordable to average retail/service worker'].isnull()]
    df1['Owner units affordable to average retail/service worker'].fillna(0, inplace=True)

    df1[df1['Owner units affordable to average teacher'].isnull()]
    df1['Owner units affordable to average teacher'].fillna(0, inplace=True)

    df1[df1['Owner units affordable to average tech worker'].isnull()]
    df1['Owner units affordable to average tech worker'].fillna(0, inplace=True)

    df1[df1['Owner units affordable to average artist'].isnull()]
    df1['Owner units affordable to average artist'].fillna(0, inplace=True)

    df1.count()
    #No more missing values in df1
    logging.info("CSV 1 is cleaned")

    ###################### Cleaning process of df2 #####################
    #### Austin Water - Residential Water Consumption ####
    df2.head()
    #Check NA values
    df2.count() #No missing values
    df2 = df2[['Postal Code','Customer Class', 'Total Gallons']]
    #group by (Postal Code, Customer Class) -> create new column sum of Total Gallons for each class
    df2_group = df2.groupby(['Postal Code','Customer Class'], as_index=False).sum()
    #When we decided to group data by Customer Class, each of the group (class)
    #has to be presented by column! So,
    #we transpose from rows to columns the total gallons per category
    df2 = df2_group.reset_index().pivot('Postal Code', 'Customer Class', 'Total Gallons')
    #change names of columns
    df2.rename(columns={'Irrigation - Multi-Family':'I-MF-Tot.Gallons',
                        'Irrigation - Residential':'I-R-Tot.Gallons',
                        'Multi-Family':'MF-Tot.Gallons', 'Residential':'R-Tot.Gallons'}, inplace=True)

    #get rid of name='Customer Class'
    df2 = df2.rename_axis(None, axis=1).reset_index()
    #replace all NA values by 0 for all the columns.
    #It might happen that a zip code does not have a particular type of Customer
    #Class, so it does not make sense taking other policies such as median or mean.
    #We just input zero
    df2.loc[:,'I-MF-Tot.Gallons':'R-Tot.Gallons'] = df2.loc[:,'I-MF-Tot.Gallons':'R-Tot.Gallons'].fillna(0)

    logging.info("CSV 2 is cleaned")

    ###################### Cleaning process of df3 #####################
    #### Food Establishment Inspection Scores ####
    df3.head()
    df3.columns
    #Check NAs
    df3.count() #no missing values
    #group by Zip Code, and take the median of Score
    df3 = df3.groupby('Zip Code').median().reset_index()

    logging.info("CSV 3 is cleaned")

    ###################### Cleaning process of df4 #####################
    ####Mixed Beverage Gross Reciepts ####
    #Select only city Austin
    df4 = df4[df4['location_city']=='AUSTIN']
    df4.drop('location_city', axis=1,inplace=True)
    df4 = df4.groupby('location_zip').sum().reset_index()
    df4.count() #no missing values

    logging.info("CSV 4 is cleaned")

    ###### Be advise that cleaning process of df5 is in the other function
    ###### Function called clean_df5 (below)
    return (df1,df2,df3,df4)

############################### Function 4 ###################################
def clean_df5(Incidents): # input is data AFTER intersection with zip-codes
    logging.info("Now, we clean CSV5")

    # Group by (zipcode and type of incident).
    # Take the count of how many incidents for each zipcode are for each type
    Incidents=Incidents.groupby(['zipcode','Issue Reported']).size().reset_index()
    Incidents.rename(columns={0:'Total incidents'}, inplace=True)
    #Since we groupped data, we need to transfor each group into a new column.
    Incidents = Incidents.pivot('zipcode', 'Issue Reported', 'Total incidents').reset_index()

    # Before some zip-codes have never had certain type of accidents. After
    # creating a columns those zip-codes got NA values. Thus, we input NA
    # values into there rows.
    Incidents.loc[:,'AUTO/ PED':'zSTALLED VEHICLE'] = Incidents.loc[:,'AUTO/ PED':'zSTALLED VEHICLE'].fillna(0)

    #get rid of name='Issue Reported'
    Incidents = Incidents.rename_axis(None, axis=1).reset_index()
    #Change a type of Zipcode into int
    Incidents['zipcode'] = Incidents['zipcode'].astype(int)

    logging.info("CSV 5 cleaned!")
    return Incidents

############################### Function 6 ###################################
def fill_nas(data):
    logging.info("Dealing with NA values")
    # After merging we have some NA values, input zero fo them.
    data.loc[:,'beer_receipts':'zSTALLED VEHICLE'] = data.loc[:,'beer_receipts':'zSTALLED VEHICLE'].fillna(0)
    logging.info("NA vales filled by zeros!")

    return data

############################### Function 7 ###################################
    #Just iterate thru data and check if there are any NA values.
    #If so print name of a column and how many NA were spotted.
def check_nas(data):
    logging.info("Let's check if there are any column with NA values")
    missing_data = data.isnull() #localize NULLs
    missing_data.head(5)
    # Go thru missing_data and print if TURE
    print("Missing values were found in columns:\n")
    for column in missing_data.columns.values.tolist():
        if True in missing_data[column].values:
            print(column)
            print(missing_data[column].value_counts())
            print("")


import sys
from collections import deque
sys.stdin = open("baek16509.txt")

"""
왕이 장애물일 수 있다. 주의하자!
"""

def bfs():
    dr = [[-1, -1, -1], [-1, -1, -1], [0, -1, -1], [0, 1, 1], [1, 1, 1], [1, 1, 1], [0, 1, 1], [0, -1, -1]]
    dc = [[0, 1, 1], [0, -1, -1], [1, 1, 1], [1, 1, 1], [0, -1, -1], [0, 1, 1], [-1, -1, -1], [-1, -1, -1]]
    visited = [[False] * COL for _ in range(0, ROW)]

    que = deque()
    visited[R1][C1] = True
    que.append((R1, C1, 0))

    while que:
        row, col, cnt = que.popleft()

        for d in range(0, 8):
            flag = True
            next_row = row
            next_col = col
            for k in range(0, 3):
                tmp_row = next_row + dr[d][k]
                tmp_col = next_col + dc[d][k]
                if tmp_row < 0 or tmp_col < 0 or tmp_row >= ROW or tmp_col >= COL:
                    flag = False
                    break
                # 이 문제에서 유일한 장애물 왕, 왕이 경로에 있으면 안된다.
                if k < 2 and tmp_row == R2 and tmp_col == C2:
                    flag = False
                    break
                next_row = tmp_row
                next_col = tmp_col
            if flag:
                if next_row == R2 and next_col == C2:
                    return cnt+1
                if visited[next_row][next_col]:
                    continue
                visited[next_row][next_col] = True
                que.append((next_row, next_col, cnt+1))
    return -1

# 상의 위치
R1, C1 = map(int, input().split())
# 왕의 위치
R2, C2 = map(int, input().split())
ROW = 10
COL = 9

answer = bfs()
print(answer)


"""
"""
from .base import Operation, OperationOnFile, OperationOnSubdirectories, OpeartionWithShellCall, RoutineOnDirectory
from dxl.fs import Directory, File
from typing import TypeVar, Iterable
from pygate.components.simulation import Simulation
from pygate.scripts.shell import Script
import rx

class KEYS:
    SUBDIRECTORIES = 'subdirectories'
    TARGET = 'target'
    IS_TO_BROADCAST = 'is_to_broadcast'
    CONTENT = 'content'
    TO_BROADCAST_FILES = 'to_broadcast_files'

class TargetFileWithContent:
    def __init__(self, target: TypeVar('FileLike', File, str), is_to_broadcast: bool=True, content: str=None):
        if isinstance(target, File):
            self.target = target
        else:
            self.target = File(target)
        self.is_to_broadcast = is_to_broadcast
        self.content = content

    def to_dict(self):
        return {KEYS.TARGET: self.target.path.s,
                KEYS.IS_TO_BROADCAST: self.is_to_broadcast,
                KEYS.CONTENT: self.content}

    def save(self):
        if self.content is None:
            raise TypeError("None content to save.")
        self.target.save(self.content)

class OpGenerateFile(OperationOnFile):
    def __init__(self, filename: str, is_to_broadcast=True):
        super().__init__(filename)
        self.is_to_broadcast = is_to_broadcast

    def content(self, r: RoutineOnDirectory) -> str:
        raise NotImplementedError

    def target_file_with_content(self, r: RoutineOnDirectory):
        return TargetFileWithContent(self.target(r), self.is_to_broadcast,
                                     self.content(r))

    def apply(self, r: RoutineOnDirectory):
        f = self.target_file_with_content(r)
        f.save()
        return f.to_dict()

    def dryrun(self, r: RoutineOnDirectory):
        return self.target_file_with_content(r).to_dict()

class OpGeneratorMac(OpGenerateFile, OpeartionWithShellCall):
    def __init__(self, script_filename: str, mac_config: str=None, mac_filename: str=None):
        super.__init__(filename)
        self.mac_config = mac_config
        self.mac_filename = mac_filename

    def call_args(self, r: RoutineOnDirectory):
        result = ['python', self.target(r).path.s]
        if self.mac_config is not None:
            result += ['--config', self.mac_config]
        if self.mac_filename is not None:
            result += ['--target', self.mac_filename]
        return result

class OpGenerateMacTemplate(OpGenerateFile):
    def __init__(self, filename):
        super().__init__(filename)
        from ..scripts.shell import ScriptMacTemplate
        self.script = ScriptMacTemplate()

    def content(self, r: RoutineOnDirectory) -> str:
        return self.script.render()

class OpGeneratorPhantom(OperationOnFile):
    def __init__(self, filename: str):
        super.__init__(filename)


from typing import Type, Any, Union, Optional, Callable

from ..base_schema import Schema, SchemaPattern
from ..object_schema import resolve_arg_to_schema
from ..dom import DOMObject, DOMInfo, document

class UserProperty(object):
    """
    A data descriptor for creating attributes in user-defined subclasses
    of :class:`~.wysdom.user_objects.UserObject` which are mapped to keys in the underlying
    data object and to the `properties` key in the object's JSON schema.

    :param property_type:    The data type or schema for this property. Must
                             be one of:

                             * A primitive Python type (:class:`str`, :class:`int`,
                               :class:`bool`, :class:`float`)
                             * A subclass of :class:`~.wysdom.user_objects.UserObject`
                             * An instance of :class:`~wysdom.base_schema.Schema`

    :param optional:         Determines whether this property is optional in the underlying
                             data object. If default or default_function are set, this
                             will default to True, otherwise False.

    :param name:             The name of this property in the underlying
                             data object. If not provided, this defaults to
                             the name of the attribute on the :class:`~.wysdom.user_objects.UserObject`
                             instance that owns the property.

    :param default:          A static value which provides a default value
                             for this property. Cannot be set in conjunction
                             with `default_function`.

    :param default_function: A function which provides a default value
                             for this property. The function must have a
                             single positional argument, `self`, which is
                             passed the :class:`~.wysdom.user_objects.UserObject` instance that
                             owns the property. Cannot be set in conjunction
                             with `default`.

    :param persist_defaults: If this property is set to True and a UserProperty has either the
                             `default` or `default_function` property, when the UserProperty returns
                             a default value that value will also be explicitly stored in the underlying
                             data object. This is often desirable behavior if the UserProperty
                             returns another object and your code expects it to return the same
                             object instance each time it is accessed.

    :param pattern:          A regex pattern to validate the values of this property against.
                             Use only for `str` properties.
    """

    def __init__(
        self,
        property_type: Union[Type, Schema],
        optional: Optional[bool] = None,
        name: Optional[str] = None,
        default: Optional[Any] = None,
        default_function: Optional[Callable] = None,
        persist_defaults: Optional[bool] = None,
        pattern: Optional[str] = None,
    ) -> None:
        if default is not None or default_function is not None:
            if default is not None and default_function is not None:
                raise ValueError("Cannot use both default and default_function.")
            if optional is False:
                raise ValueError(
                    "Cannot set optional to False if default or default_function are specified."
                )
            self.optional = True
        else:
            self.optional = bool(optional)
        if pattern:
            if property_type is not str:
                raise TypeError(
                    "Parameter 'pattern' can only be set if 'property_type' is str."
                )
            self.schema_type = SchemaPattern(pattern)
        else:
            self.schema_type = resolve_arg_to_schema(property_type)
        self.name = name
        self.default = default
        self.default_function = default_function
        self.persist_defaults = persist_defaults

    def __get__(self, instance: DOMObject, owner: Type[DOMObject]) -> Any:
        if instance is None:
            raise AttributeError("UserProperty is not valid as a class data descriptor")
        if self.name not in instance:
            if self.default_function:
                default_value = self.default_function(instance)
            else:
                default_value = self.default
            if self.persist_defaults:
                instance[self.name] = default_value
                return instance[self.name]
            elif default_value is None:
                return default_value
            else:
                return self.schema_type(
                    default_value,
                    DOMInfo(
                        document=document(instance),
                        parent=instance,
                        element_key=self.name,
                    ),
                )
        return instance[self.name]

    def __set__(self, instance: DOMObject, value: Any) -> None:
        instance[self.name] = value


from __future__ import absolute_import

import re
from typing import (
    Dict,
    Text,
)

import yaml

from yaml import FullLoader
from appyratus.enum import Enum

from .file import (
    File,
    FileObject,
)

EXTENSIONS = Enum.of_strings('yml', 'yaml')

class Yaml(File):
    """
    # Yaml
    Yaml file type

    # References
    https://pyyaml.org/wiki/PyYAMLDocumentation
    """

    @classmethod
    def extensions(cls):
        return {v for v in EXTENSIONS}

    @classmethod
    def default_extension(cls):
        return EXTENSIONS.YML

    @classmethod
    def read(cls, path: Text, multi=False, loader_class=FullLoader):
        try:
            with open(path) as yaml_file:
                return cls.load(
                    yaml_file, multi=multi, loader_class=loader_class
                )
        except yaml.composer.ComposerError:
            # this should occur when you attempt to load in a yaml file that
            # that contains multiple documents and you did not specify multiple
            with open(path) as yaml_file:
                return cls.load(
                    yaml_file, multi=True, loader_class=loader_class
                )

    @classmethod
    def write(cls, path: Text, data=None, multi=False, **kwargs):
        file_data = cls.dump(data, multi=multi)
        super().write(path=path, data=file_data, **kwargs)

    @classmethod
    def load(cls, data=None, multi: bool = False, loader_class=FullLoader):
        load_args = {'Loader': loader_class}
        if data is None:
            return
        if multi:
            if not data:
                return []
            docs = yaml.load_all(data, **load_args)
            # a generator comes back
            return [doc for doc in docs]
        else:
            return yaml.load(data, **load_args)

    @classmethod
    def dump(
        cls,
        data,
        multi: bool = False,
        default_flow_style: bool = None,
        explicit_start: bool = None,
        explicit_end: bool = None,
        indent: int = None
    ):
        dump_args = {
            'default_flow_style': default_flow_style if default_flow_style else False,
            'explicit_start': explicit_start if explicit_start is not None else True,
            'explicit_end': explicit_end if explicit_end is not None else True,
            'indent': indent if indent is not None else 2,
        }
        # here as a last defense we should check if this data is yaml construct
        # otherwise a string of yaml erroneously dumped will result in a
        # document with a a list of characters from that dumped string
        if not cls.is_dumped(data):
            if multi:
                data = yaml.dump_all(data, **dump_args)
            else:
                data = yaml.dump(data, **dump_args)
        return data

    @staticmethod
    def is_dumped(data) -> bool:
        """
        # Is Dumped
        Checks for explicit YAML document "---" prefix)
        """
        r_header = r"^---\n"
        dumped = False
        if isinstance(data, str):
            # we check here to see if the string has
            match = re.match(r_header, data)
            if match:
                dumped = True
            # TODO an intensive check would be to load the data and capture any
            # exceptions- if loaded without error then that is an expensive
            # indication that the string is yaml
        return dumped

class YamlFileObject(FileObject):

    @classmethod
    def get_file_type(cls):
        return Yaml


from sklearn import datasets
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer

import joblib
import csv

'''
Gets the full feature list to be used as a training dataset
    Parameters:
            path(string): path of the file to be used
    Returns:
        f: the list of features that would be used for training
        labels: the labels of what the paper was classified as
'''
def train(path):
    features = pd.read_csv(path)
    labels = np.array(features['label'])

    # drops column keyword and label
    features = features.drop('keyword', axis = 1)
    features = features.drop('label', axis = 1)
    # use the title vector as a feature by converting it to a counter
    #title_counter = convert_to_counter(np.array(features['vector_t']))
    features = features.drop('vector_t', axis = 1)
    features = features.drop('vector_s', axis = 1)
    features = features.drop('position_k', axis = 1)
    features = features.drop('position_d', axis = 1)
    features = np.array(features)
    f = []
    # for i in range(len(features)):
    #     f.append(np.concatenate([features[i], title_counter[i]]))
    return features, labels

'''
Gets the full feature list to be used as a training dataset
    Parameters:
        feature(list): The dataset of features
        label(list): List of labels
    Returns:
        The accuarcy of the classifier
'''
def test_scholars(feature, label):
    train_features, test_features, train_labels, test_labels = train_test_split(feature, label, test_size = 0.20, random_state = 15)
    rfc = RandomForestClassifier(n_estimators = 500, random_state = 42)
    rfc.fit(train_features, train_labels)
    joblib.dump(rfc, "./random_forest.joblib")
    predicitons = rfc.predict(test_features)
    correct = 0
    for i in range(len(test_labels)):
        if test_labels[i] == predicitons[i]:
            correct += 1
    print(correct)
    return correct * 100.0 / len(test_labels)

'''
Store the random forrest tree classifier.
    Parameters:
        feature(list): The dataset of features
        label(list): List of labels
'''
def train_scholars(feature, label):
    rfc = RandomForestClassifier(n_estimators = 500, random_state = 42)
    rfc.fit(feature, label)
    joblib.dump(rfc, "./random_forest.joblib")

'''
Fill in the missing data by dummy values that could help them get ignored
    Parameters:
        path(string): path of the file to be modified
'''
def fill_missing_data(path):
    df = pd.read_csv(path)
    citation_num = [i for i in df['citation#'] if i != -999]
    year = [i for i in df['year'] if i != -999]
    k_list = [i for i in df['position_k'] if i != -999]
    d_list = [i for i in df['position_d'] if i != -999]
    avgs = []
    avgs.append(sum(citation_num) / len(citation_num))
    avgs.append(sum(k_list) / len(k_list))
    avgs.append(sum(d_list) / len(d_list))
    avgs.append(sum(year) / len(year))
    print(avgs)
    df.loc[df['year'] == -999, 'year'] = avgs[3]
    df.loc[df['position_d'] == -999, 'position_d'] = avgs[2]
    df.loc[df['position_k'] == -999, 'position_k'] = avgs[1]
    df.loc[df['citation#'] == -999, 'citation#'] = avgs[0]

    df.to_csv(path, index = False)

'''
Convert a list of strings to a vector of strings that could be utilized as features
    Parameters:
        vec(list): Vector that would be converted
'''
def convert_to_counter(vec):
    strs_list = []
    i = 0
    for v in vec:
        v = v.replace('[', '')
        v= v.replace(']', '')
        v = v.replace("',", "")
        v = v.replace("'", "")
        strs_list.append(v)
    vectorizer = CountVectorizer()
    return vectorizer.fit_transform(strs_list).toarray()

# f, l = train('scholars_updated_3 copy.csv')
# train_scholars(f, l)
# print(test_scholars(f, l))
# fill_missing_data('scholars_updated_3 copy.csv')


import matplotlib.pyplot as plt
import pynmea2
import cgi
import math
import numpy as np
from math import radians, cos, sin, asin, sqrt
import pyproj
import re
import pymap3d as pm

EARTH_REDIUS = 6378.137

def dms2dd(degrees, minutes, seconds, direction):
    dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
    if direction == 'S' or direction == 'W':
        dd *= -1
    return dd;

def dd2dms(deg):
    d = int(deg)
    md = abs(deg - d) * 60
    m = int(md)
    sd = (md - m) * 60
    return [d, m, sd]

def parse_dms(dms):
    parts = re.split('[^\d\w]+', dms)
    lat = dms2dd(parts[0], parts[1], parts[2], parts[3])
    lng = dms2dd(parts[4], parts[5], parts[6], parts[7])

    return (lat, lng)

## ==========================================================================================================================
def np_getDistance(A , B ):# 先緯度後經度
    ra = 6378140  # radius of equator: meter
    rb = 6356755  # radius of polar: meter
    flatten = 0.003353 # Partial rate of the earth
    # change angle to radians

    radLatA = np.radians(A[:,0])
    radLonA = np.radians(A[:,1])
    radLatB = np.radians(B[:,0])
    radLonB = np.radians(B[:,1])

    pA = np.arctan(rb / ra * np.tan(radLatA))
    pB = np.arctan(rb / ra * np.tan(radLatB))

    x = np.arccos( np.multiply(np.sin(pA),np.sin(pB)) + np.multiply(np.multiply(np.cos(pA),np.cos(pB)),np.cos(radLonA - radLonB)))
    c1 = np.multiply((np.sin(x) - x) , np.power((np.sin(pA) + np.sin(pB)),2)) / np.power(np.cos(x / 2),2)
    c2 = np.multiply((np.sin(x) + x) , np.power((np.sin(pA) - np.sin(pB)),2)) / np.power(np.sin(x / 2),2)
    dr = flatten / 8 * (c1 - c2)
    distance = 0.001 * ra * (x + dr)
    return distance

def rad(d):
    return d * np.pi / 180.0

def getDistance(lat1, lng1, lat2, lng2):
    radLat1 = rad(lat1)
    radLat2 = rad(lat2)
    a = radLat1 - radLat2
    b = rad(lng1) - rad(lng2)
    s = 2 * math.asin(math.sqrt(math.pow(sin(a/2), 2) + cos(radLat1) * cos(radLat2) * math.pow(sin(b/2), 2)))
    s = s * EARTH_REDIUS
    return s

def deg2num(lat_deg=0.0, lon_deg=0.0, zoom=12):
  lat_rad = math.radians(lat_deg)
  n = 2.0 ** zoom
  xtile = int((lon_deg + 180.0) / 360.0 * n)
  ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
  return (xtile, ytile)

def Write_RTKlibFormat(file_name,data):
    file_name.write(data)

rangeUpdateTime = [0.0]

if __name__=='__main__':
    input = open('serverlog.txt', 'r')
    file_name='tesst.nmea'
    input_nmea = open(file_name, 'r')

    RTKlog_name = open(file_name+'_log', 'w')
    line = '$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76'
    #record = pynmea2.parse(line)
    for line in input_nmea:

        if 'GGA' in line:
            record = pynmea2.parse(line)
            Write_RTKlibFormat(RTKlog_name,line)

            '''
            print('Latitude: {:02d}°{:02d}′{:07.4f}″'.format(int(record.latitude), int(record.latitude_minutes), record.latitude_seconds))
            print('Longitude: {:02d}°{:02d}′{:07.4f}″'.format(int(record.longitude), int(record.longitude_minutes), record.longitude_seconds))
            print('Altitude: {:.3f}'.format(record.altitude))
            '''

        #if 'GPGSA' in line:
            #Write_RTKlibFormat(RTKlog_name,line)
        if 'GPRMC' in line:
            Write_RTKlibFormat(RTKlog_name,line)
    RTKlog_name.close()

    A=np.matrix([[25.060835, 121.544242]])
    B=np.matrix([[25.062247, 121.544236]])

    Distance_2=np_getDistance(A,B)
    Distance_3=getDistance(25.060835, 121.544242,25.062247, 121.544236)
    x = 0
    y = 45
    z = 1000

    ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
    lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
    lon, lat, alt = pyproj.transform(ecef, lla, x, y, z, radians=True)

    print ( Distance_2, Distance_3)
    dd = parse_dms("36°57'9' N 110°4'21' W")

    x,y,z=pm.geodetic2ecef(22.99665875 ,120.222584889,98.211)
    x,y,z=pm.ecef2geodetic(x,y,z)
    print(x,y,z)
    print(dd)
    print(dd2dms(dd[0]))

    for line in input:
        line = line.split()
        if 'update' in line:
            rangeUpdateTime.append(float(line[-1]))

    plt.figure('frame time')
    plt.subplot(211)
    plt.plot(rangeUpdateTime, '.r',)
    plt.grid(True)
    plt.subplot(212)
    plt.plot(rangeUpdateTime)
    plt.grid(True)
    plt.show()


"""
People input format:
    {person name: {day: [[time, type], ...]}}

Session input format:
    {session name: [day, start, end]}

Output format:
    [{person name: [session name, ...]}, ...]
"""

from backend.algo import Allocator
from backend.structs import Person, Session
from pprint import pprint
import sys
import random

RUN_COUNT = 10

TIMES = ["8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
         "18:00", "19:00", "20:00"]

def time_lt(a, b):
    return TIMES.index(a) < TIMES.index(b)

def time_leq(a, b):
    return TIMES.index(a) <= TIMES.index(b)

def time_gt(a, b):
    return TIMES.index(a) > TIMES.index(b)

def time_geq(a, b):
    return TIMES.index(a) >= TIMES.index(b)

def time_in(day_a, start_a, end_a, day_b, start_b, end_b):
    """Returns if a is within b's hours"""
    b = (time_geq(start_a, end_a) and time_lt(start_a, end_b)) or \
        (time_leq(end_a, end_b) and time_gt(end_a, start_b))
    return b and day_a == day_b

def clash(day_a, start_a, end_a, day_b, start_b, end_b):
    """Returns if a and b clash"""
    return time_in(day_a, start_a, end_a, day_b, start_b, end_b) or \
           time_in(day_b, start_b, end_b, day_a, start_a, end_a)

def plus_1(time):
    return TIMES[TIMES.index(time) + 1]

def setup(people_input, session_input):

    people = []
    sessions = []
    session_times = {}

    for s in session_input:
        day, start, end = session_input[s]
        new = Session(s)
        session_times[(day, start, end)] = new
        sessions.append(new)
    compute_clashes(session_times)

    for p in people_input:
        new = Person(p)
        for t in session_times:
            i = session_times[t]
            a = is_avail(t, people_input[p])
            if not a:
                new.set_pref(i, sys.maxsize)
            elif a == 1:
                new.set_pref(i, 1)
            elif a == 2:
                new.set_pref(i, 5)
        people.append(new)

    return people, sessions

def run_multi(people, sessions):
    allocations = []
    for i in range(RUN_COUNT):
        try:
            a = Allocator(people, sessions)
            a.run()
            this = {}
            for p in people:
                this[p] = p.get_allocs()
            if this not in allocations:
                allocations.append(this)
        except:
            continue
    return allocations

def run(people_input, session_input):
    all_allocs = run_multi(*setup(people_input, session_input))
    alloc = random.choice(all_allocs)
    output = []
    for person in alloc:
        output.append({"email": str(person), "allocation": ", ".join([str(i) for i in alloc[person]])})
    return output  # [{email: email, allocations: allocations}, ...]

def is_avail(session_time, person_details):
    day = session_time[0]
    start = session_time[1]
    end = session_time[2]
    need_be = False
    while time_lt(start, end):
        if [start, 2] in person_details[day]:
            need_be = True
        elif [start, 1] not in person_details[day]:
            return 0
        start = plus_1(start)
    if need_be:
        return 2
    return 1

def compute_clashes(session_times):
    for t in session_times:
        i = session_times[t]
        for u in session_times:
            j = session_times[u]
            if i != j and clash(t[0], t[1], t[2], u[0], u[1], u[2]):
                i.add_clash(j)
                j.add_clash(i)
                print(i, j)

if __name__ == "__main__":
    sessions = {
        "T01": [0, "8:00", "9:00"],
        "T02": [0, "9:00", "10:00"],
        "T03": [1, "9:00", "10:00"],
        "T04": [1, "10:00", "11:00"]
    }

    people = {
        "Alice": {0: [["8:00", 1], ["9:00", 1], ["10:00", 1]], 1: [["8:00", 0], ["9:00", 0], ["10:00", 0]]},
        "Bob": {0: [["8:00", 0], ["9:00", 0], ["10:00", 0]], 1: [["8:00", 1], ["9:00", 1], ["10:00", 1]]},
        "Charlie": {0: [["8:00", 1], ["9:00", 0], ["10:00", 1]], 1: [["8:00", 0], ["9:00", 1], ["10:00", 0]]},
        "Emily": {0: [["8:00", 1], ["9:00", 0], ["10:00", 0]], 1: [["8:00", 1], ["9:00", 1], ["10:00", 0]]}
    }

    pprint(run(people, sessions))


from setuptools import setup

from core.config import APP_DESCRIPTION, APP_VERSION, APP_NAME

APP = ['dmclient.py']
DATA_FILES = []
OPTIONS = {}

setup(name=APP_NAME, version=APP_VERSION, description=APP_DESCRIPTION,
      app=APP,
      data_files=DATA_FILES,
      options={'py2app': OPTIONS},
      setup_requires=['py2app'],
      )


from pathlib import Path

from bug_changing.types.placeholder import Placeholder
from bug_changing.utils.file_util import FileUtil
from bug_changing.utils.path_util import PathUtil
import pandas as pd
import numpy as np

from config import OUTPUT_DIR, MOZILLA_PROJ, ECLIPSE_PROJ

def convert_summary_pairs_into_dataframe(summary_pairs, folder_name='mozilla'):
    columns = ['bug id', 'bug link', 'summary id', 'summary',
               Placeholder.SUGGESTED_SOLUTION, Placeholder.ACTUAL_BEHAVIOR, Placeholder.TRIGGER_ACTION,
               Placeholder.IS_ACTUAL_BEHAVIOR_VAGUE, Placeholder.IS_TRIGGER_ACTION_VAGUE,
               Placeholder.PLATFORM, Placeholder.COMPONENT,
               Placeholder.SPELLING_ERROR, Placeholder.SYNTAX_ERROR,
               Placeholder.DECLARATIVE_STATEMENT, Placeholder.PRESENT_TENSE,
               Placeholder.PERSONAL_PRONOUN, Placeholder.EMOTIONAL_REACTION,
               Placeholder.OFFENCE, Placeholder.WISHY_WASHY_WORD]

    # for index in range(5):
    #     columns.extend(['change from', 'change what', 'change for'])
    rows = []
    for summary_pair in summary_pairs:
        if folder_name == MOZILLA_PROJ:
            bug_link = f"https://bugzilla.mozilla.org/show_bug.cgi?id={summary_pair.bug.id}"
        else:
            bug_link = f"https://bugs.eclipse.org/bugs/show_bug.cgi?id={summary_pair.bug.id}"

        summaries = [summary_pair.rm_summary, summary_pair.add_summary]
        for summary in summaries:
            row = [summary_pair.bug.id, bug_link, summary.id, summary.text, ]
            for add_column in range(15):
                row.extend([''])
            # row.append('')
            rows.append(row)
    df = pd.DataFrame(rows, columns=columns)
    return df

def get_labels_from_row(row):
    labels = row.values[5:20].tolist()  # [5, 20)
    return labels

if __name__ == "__main__":
    folder_name = MOZILLA_PROJ
    # folder_name = ECLIPSE_PROJ
    summary_pairs_filepath = PathUtil.get_instance_summary_pairs_filepath(folder_name)
    summary_pairs = FileUtil.load_pickle(summary_pairs_filepath)
    # print(summary_pairs)
    summary_pairs.remove_tags()

    df = pd.read_excel(Path(OUTPUT_DIR, folder_name, f'instance_summary_pairs_labels.xlsx'),
                       sheet_name=f'{folder_name}')
    df = df.replace(np.nan, None)

    for index, two_rows in df.groupby(np.arange(len(df)) // 2):
        # print(index)
        first_row = two_rows.iloc[0]
        second_row = two_rows.iloc[1]
        bug_id = int(first_row.values[1])
        rm_summary_id = int(first_row.values[3])
        add_summary_id = int(second_row.values[3])
        summary_pair = summary_pairs.get_summary_pair_by_bug_id_summary_ids(bug_id, rm_summary_id, add_summary_id)
        labels_1 = get_labels_from_row(first_row)
        labels_2 = get_labels_from_row(second_row)
        print(labels_1)
        print(labels_2)
        summary_pair.rm_summary.initiate_from_labels(labels_1)
        summary_pair.add_summary.initiate_from_labels(labels_2)
        print(summary_pair)
        # print(labels_1)
        # print(labels_2)
        # print(type(first_row.values[0:5].tolist()))
        # print(first_row.values[1])
        # print(second_row)
        # print(type(g))
        # input()
    if folder_name == MOZILLA_PROJ:
        bug_id_summary_ids_tuple_list = [(551591, 3, 4),
                                         (1388414, 0, 1), (697762, 0, 1), (1393563, 0, 1), (831452, 0, 1),
                                         (1611864, 0, 1), (641322, 1, 2), (1302671, 0, 1), (1698580, 1, 2)]
    else:
        bug_id_summary_ids_tuple_list = [(478512, 0, 1), (560060, 0, 1), (357540, 0, 1)]
    print(len(summary_pairs))
    summary_pairs = summary_pairs.filter_for_goal_model_instances(bug_id_summary_ids_tuple_list)
    summary_pairs.complete_vague_labels()
    print(len(summary_pairs))
    # print(summary_pairs.get_summary_pair_by_bug_id_summary_ids(1403460, 0, 1))
    FileUtil.dump_pickle(summary_pairs_filepath, summary_pairs)


import tvm
import numpy as np
from tvm.tensor_graph.core import compute, GraphTensor, GraphOp, ForwardGraph, \
                              BackwardGraph, GraphMutator, PyTIRGraph
import tvm.tensor_graph.nn.functional as F
from tvm.tensor_graph.nn.modules.loss import CELoss
from tvm.tensor_graph.nn.modules.optimize import SGD

batch_size = 4
num_class = 1000
channel = 3
height = width = 224
image_shape = (channel, height, width)
data_shape = (batch_size, ) + image_shape
out_shape = (batch_size, num_class)

def resnet():
    data = GraphTensor([4, 3, 224, 224], name="data") #(4, 3, 224, 224)

    alpha0 = GraphTensor([3], name="alpha0")
    beta0 = GraphTensor([3], name="beta0")
    bn0 = F.batch_norm2d(data, alpha0, beta0)

    conv2_weight = GraphTensor([64, 3, 7, 7])
    conv2 = F.conv2d_nchw(bn0, conv2_weight, bias=None, \
        stride=2, padding=3)
    #(4, 64, 112, 112), %2

    alpha3 = GraphTensor([64], name="alpha3")
    beta3 = GraphTensor([64], name="beta3")
    bn3 = F.batch_norm2d(conv2, alpha3, beta3)

    relu5 = F.ReLU(bn3)
    # %5

    maxpool6 = F.avgpool2d(relu5, kernel_h=3, kernel_w=3, \
        stride_h=2, stride_w=2, padding=1)
    #(4, 64, 56, 56)

    alpha7 = GraphTensor([64], name="alpha7")
    beta7 = GraphTensor([64], name="beta7")
    bn7 = F.batch_norm2d(maxpool6, alpha7, beta7)

    relu9 = F.ReLU(bn7)
    # %9

    conv10_weight = GraphTensor([64, 64, 3, 3])
    conv10 = F.conv2d_nchw(relu9, conv10_weight, bias=None,\
        stride=1, padding=1)
    #(4, 64, 56, 56)

    alpha11 = GraphTensor([64], name="alpha11")
    beta11 = GraphTensor([64], name="beta11")
    bn11 = F.batch_norm2d(conv10, alpha11, beta11)

    relu13 = F.ReLU(bn11)
    # %13

    conv14_weight = GraphTensor([64, 64, 3, 3])
    conv14 = F.conv2d_nchw(relu13, conv14_weight, bias=None,\
        stride=1, padding=1)
    #(4, 64, 56, 56), %14

    conv15_weight = GraphTensor([64, 64, 1, 1])
    conv15 = F.conv2d_nchw(relu9, conv15_weight, bias=None,\
        stride=1, padding=0)
    #relu11 is far above!
    #(4, 64, 56, 56), %15

    add16 = F.add_4d(conv14, conv15) #Skip Connection
    # %16

    alpha17 = GraphTensor([64], name="alpha17")
    beta17 = GraphTensor([64], name="beta17")
    bn17 = F.batch_norm2d(add16, alpha17, beta17)

    relu19 = F.ReLU(bn17)
    # %19

    conv20_weight = GraphTensor([64, 64, 3, 3])
    conv20 = F.conv2d_nchw(relu19, conv20_weight, bias=None,\
        stride=1, padding=1)

    alpha21 = GraphTensor([64], name="alpha21")
    beta21 = GraphTensor([64], name="beta21")
    bn21 = F.batch_norm2d(conv20, alpha21, beta21)

    relu23 = F.ReLU(bn21)
    # %23

    conv24_weight = GraphTensor([64, 64, 3, 3])
    conv24 = F.conv2d_nchw(relu23, conv24_weight, bias=None,\
        stride=1, padding=1)

    add25 = F.add_4d(conv24, add16)

    alpha26 = GraphTensor([64], name="alpha26")
    beta26 = GraphTensor([64], name="beta26")
    bn26 = F.batch_norm2d(add25, alpha26, beta26)

    relu28 = F.ReLU(bn26)
    # (4, 64, 56, 56)

    conv29_weight = GraphTensor([128, 64, 3, 3])
    conv29 = F.conv2d_nchw(relu28, conv29_weight, bias=None,\
        stride=2, padding=1)
    # (4, 128, 28, 28)

    alpha30 = GraphTensor([128], name="alpha30")
    beta30 = GraphTensor([128], name="beta30")
    bn30 = F.batch_norm2d(conv29, alpha30, beta30)

    relu32 = F.ReLU(bn30)

    conv33_weight = GraphTensor([128, 128, 3, 3])
    conv33 = F.conv2d_nchw(relu32, conv33_weight, bias=None,\
        stride=1, padding=1)

    conv34_weight = GraphTensor([128, 64, 1, 1])
    conv34 = F.conv2d_nchw(relu28, conv34_weight, bias=None,\
        stride=2, padding=0)

    add35 = F.add_4d(conv33, conv34)

    alpha36 = GraphTensor([128], name="alpha36")
    beta36 = GraphTensor([128], name="beta36")
    bn36 = F.batch_norm2d(add35, alpha36, beta36)

    relu38 = F.ReLU(bn36)

    conv39_weight = GraphTensor([128, 128, 3, 3])
    conv39 = F.conv2d_nchw(relu38, conv39_weight, bias=None,\
        stride=1, padding=1)

    alpha40 = GraphTensor([128], name="alpha40")
    beta40 = GraphTensor([128], name="beta40")
    bn40 = F.batch_norm2d(conv39, alpha40, beta40)

    relu42 = F.ReLU(bn40)

    conv43_weight = GraphTensor([128, 128, 3, 3])
    conv43 = F.conv2d_nchw(relu42, conv43_weight, bias=None,\
        stride=1, padding=1)

    add44 = F.add_4d(conv43, add35)

    alpha45 = GraphTensor([128], name="alpha45")
    beta45 = GraphTensor([128], name="beta45")
    bn45 = F.batch_norm2d(add44, alpha45, beta45)

    relu47 = F.ReLU(bn45)
    #(4, 128, 28, 28)

    conv48_weight = GraphTensor([256, 128, 3, 3])
    conv48 = F.conv2d_nchw(relu47, conv48_weight, bias=None,\
        stride=2, padding=1)
    #(4, 256, 14, 14)

    alpha49 = GraphTensor([256], name="alpha49")
    beta49 = GraphTensor([256], name="beta49")
    bn49 = F.batch_norm2d(conv48, alpha49, beta49)

    relu51 = F.ReLU(bn49)

    conv52_weight = GraphTensor([256, 256, 3, 3])
    conv52 = F.conv2d_nchw(relu51, conv52_weight, bias=None,\
        stride=1, padding=1)

    conv53_weight = GraphTensor([256, 128, 1, 1])
    conv53 = F.conv2d_nchw(relu47, conv53_weight, bias=None,\
        stride=2, padding=0)
    #(4, 256, 14, 14)

    add54 = F.add_4d(conv52, conv53)

    alpha55 = GraphTensor([256], name="alpha55")
    beta55 = GraphTensor([256], name="beta55")
    bn55 = F.batch_norm2d(add54, alpha55, beta55)

    relu57 = F.ReLU(bn55)

    conv58_weight = GraphTensor([256, 256, 3, 3])
    conv58 = F.conv2d_nchw(relu57, conv58_weight, bias=None,\
        stride=1, padding=1)

    alpha59 = GraphTensor([256], name="alpha59")
    beta59 = GraphTensor([256], name="beta59")
    bn59 = F.batch_norm2d(conv58, alpha59, beta59)

    relu61 = F.ReLU(bn59)

    conv62_weight = GraphTensor([256, 256, 3, 3])
    conv62 = F.conv2d_nchw(relu61, conv62_weight, bias=None,\
        stride=1, padding=1)

    add63 = F.add_4d(conv62, add54)

    alpha64 = GraphTensor([256], name="alpha64")
    beta64 = GraphTensor([256], name="beta64")
    bn64 = F.batch_norm2d(add63, alpha64, beta64)

    relu66 = F.ReLU(bn64)
    #(4, 256, 14, 14)

    conv67_weight = GraphTensor([512, 256, 3, 3])
    conv67 = F.conv2d_nchw(relu66, conv67_weight, bias=None,\
        stride=2, padding=1)
    #(4, 512, 7, 7)

    alpha68 = GraphTensor([512], name="alpha68")
    beta68 = GraphTensor([512], name="beta68")
    bn68 = F.batch_norm2d(conv67, alpha68, beta68)

    relu70 = F.ReLU(bn68)

    conv71_weight = GraphTensor([512, 512, 3, 3])
    conv71 = F.conv2d_nchw(relu70, conv71_weight, bias=None,\
        stride=1, padding=1)

    conv72_weight = GraphTensor([512, 256, 1, 1])
    conv72 = F.conv2d_nchw(relu66, conv72_weight, bias=None,\
        stride=2, padding=0)

    add73 = F.add_4d(conv71, conv72)

    alpha74 = GraphTensor([512], name="alpha74")
    beta74 = GraphTensor([512], name="beta74")
    bn74 = F.batch_norm2d(add73, alpha74, beta74)

    relu76 = F.ReLU(bn74)

    conv77_weight = GraphTensor([512, 512, 3, 3])
    conv77 = F.conv2d_nchw(relu76, conv77_weight, bias=None,\
        stride=1, padding=1)

    alpha78 = GraphTensor([512], name="alpha78")
    beta78 = GraphTensor([512], name="beta78")
    bn78 = F.batch_norm2d(conv77, alpha78, beta78)

    relu80 = F.ReLU(bn78)

    conv81_weight = GraphTensor([512, 512, 3, 3])
    conv81 = F.conv2d_nchw(relu80, conv81_weight, bias=None,\
        stride=1, padding=1)

    add82 = F.add_4d(conv81, add73)

    alpha83 = GraphTensor([512], name="alph83")
    beta83 = GraphTensor([512], name="beta83")
    bn83 = F.batch_norm2d(add82, alpha83, beta83)

    relu85 = F.ReLU(bn83)

    pool86 = F.global_avg_pool2d(relu85)

    flatten87 = F.batch_flatten(pool86)
    #(4, 512)

    dense_weight = GraphTensor([1000, 512])
    dense_bias = GraphTensor([1000])
    dense89 = F.dense(flatten87, dense_weight, dense_bias)
    #(4, 1000)

    label = GraphTensor([4, 1000])

    ce_loss = CELoss(label)
    sgd = SGD(0.002)

    weights = [alpha0, beta0, conv2_weight, alpha3, beta3, alpha7, beta7,
        conv10_weight, alpha11, beta11, conv14_weight, conv15_weight, alpha17,
        beta17, conv20_weight, alpha21, beta21, conv24_weight, alpha26, beta26,
        conv29_weight, alpha30, beta30, conv33_weight, conv34_weight, alpha36,
        beta36, conv39_weight, alpha40, beta40, conv43_weight, alpha45, beta45,
        conv48_weight, alpha49, beta49, conv52_weight, conv53_weight, alpha55,
        beta55, conv58_weight, alpha59, beta59, conv62_weight, alpha64, beta64,
        conv67_weight, alpha68, beta68, conv71_weight, conv72_weight, alpha74,
        beta74, conv77_weight, alpha78, beta78, conv81_weight, alpha83, beta83,
        dense_weight, dense_bias]
    fgraph = ForwardGraph([data], [dense89], weights)

    bgraph = fgraph.make_backward(ce_loss, sgd)
    sch, bufs = bgraph.create_schedule()
    #print(tvm.lower(sch, bufs, simple_mode=True))
    target = "llvm"
    dev = 0
    #naive_func = bgraph.build(sch, bufs, target)

    tgraph = PyTIRGraph(
        [x.tvm_tensor for x in bgraph.inputs],
        [x.tvm_tensor for x in bgraph.labels],
        [x.tvm_tensor for x in bgraph.outputs],
        [x.tvm_tensor for x in bgraph.weights],
        bgraph.loss.tvm_tensor,
        [x.tvm_tensor for x in bgraph.gradients],
        [x.tvm_tensor for x in bgraph.updates])

    # apply config
    # 1. modify op stat list -> head, tail
    # 2. make subgraphs
    tgraph.partition_graph()

    # 3. create schedule
    tgraph.create_schedule()
    # 4. modify schedule
    tgraph.build(target)
    # allocate buffer
    # only the first call has effect
    A_np = np.random.uniform(-1, 1, [4, 3, 224, 224]).astype("float32")
    label_np = np.random.uniform(-1, 1, [4, 1000]).astype("float32")
    tgraph.set_inputs({bgraph.inputs[0].tvm_tensor: A_np})
    tgraph.set_labels({bgraph.labels[0].tvm_tensor: label_np})
    tgraph.allocate_buffer(target, dev)

    # # get golden result
    # ctx = tvm.context(target, dev)
    # # copy the data (do not use reference)
    # A_tvm = tvm.nd.array(tgraph.get_tvm_array(bgraph.inputs[0].tvm_tensor).asnumpy(), ctx)
    # label_tvm = tvm.nd.array(tgraph.get_tvm_array(bgraph.labels[0].tvm_tensor).asnumpy(), ctx)
    # B_tvm = tvm.nd.array(tgraph.get_tvm_array(bgraph.weights[0].tvm_tensor).asnumpy(), ctx)
    # bias_tvm = tvm.nd.array(tgraph.get_tvm_array(bgraph.weights[1].tvm_tensor).asnumpy(), ctx)
    # E_tvm = tvm.nd.array(tgraph.get_tvm_array(bgraph.weights[2].tvm_tensor).asnumpy(), ctx)
    # updates_tvm = [tvm.nd.array(x.asnumpy(), ctx) for x in tgraph.get_updates()]
    # naive_func(A_tvm, label_tvm, B_tvm, bias_tvm, E_tvm, *updates_tvm)

    # compute target value
    for mark in tgraph.call_order:
        func = tgraph.functions[mark]
        bufs = tgraph.bufs[mark]
        real_bufs = [tgraph.tvm_array_dict[tgraph.subgraphs[mark].index[x]] for x in bufs]
        func(*real_bufs)
    print("Checking Gradients!")
    for item in tgraph.get_updates():
        print(item.asnumpy())

    # # test correctness
    # for (gold, value) in zip(updates_tvm, tgraph.get_updates()):
    #     tvm.testing.assert_allclose(gold.asnumpy(), value.asnumpy(), atol=1e-5, rtol=1e-30)

    # print("Success!")

if __name__ == "__main__":
    resnet()


from collections import defaultdict
Q = int(input())
record = defaultdict(int)

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        record[q[1]] =int(q[2])
    else:
        print(record[q[1]])


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional, SpatialDropout1D
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils.np_utils import to_categorical
from keras import optimizers
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
import h5py
import pickle
import os

CLUSTER_PATH = '/users/vsoni3/machinelearning/'
PATH = 'Applied ML/Project2/'
DATAFILE_NAME = 'updated_data_binary.csv'
IS_EMBEDDING = True
IS_GLOVE = False
IS_GLOVE_AVAIL = True

#setting model paraemters
epochs = 10
emb_dim = 100
batch_size = 32

#get data for news articles
#data = pd.read_csv('users/vsoni3/machinelearning/'+DATAFILE_NAME)
#data.head()

#import preprocessed text data
with open(CLUSTER_PATH+'posts.pickle','rb') as f:
    posts = pickle.load(f)

with open(CLUSTER_PATH+'output.pickle','rb') as f:
    output = pickle.load(f)
################################################################
# Preprocessing text
################################################################
max_len = 8000
embedding_dim = 100
#filter text and tokenizing
tokenizer_obj = Tokenizer()
tokenizer_obj.fit_on_texts(posts)
sequences = tokenizer_obj.texts_to_sequences(posts)

#pad sequences
word_index = tokenizer_obj.word_index
print('Found %s unique tokens. '%len(word_index))

print('Shape of output tensor:',np.array(output).shape)
vocab_size = len(word_size) + 1

###############################################################
# Glove word embedding 100 dimensions
# https://nlp.stanford.edu/projects/glove/
###############################################################
GLOVE_DIR = '/glove_6b/glove.6B/'
EMBED_MATRIX_DIR = ''

#checks if it has loaded embedding then import it
if IS_GLOVE_AVAIL:
    with open(CLUSTER_PATH+'embedding_matrix.pickle','rb') as s:
        embedding_matrix_glove = pickle.load(s)
else:
    #dictionary to store word embeddings
    embeddings_index = {}

    with open(os.path.join(PATH+GLOVE_DIR, 'glove.6B.100d.txt'), encoding='utf8') as f:
        for line in f:
            values = line.split()
            word = values[0]
            embedding = np.asarray(values[1:], dtype='float32') # Load embedding
            embeddings_index[word] = embedding # Add embedding to our embedding dictionary

    print('Found {:,} word vectors in GloVe.'.format(len(embeddings_index)))

    word_index = tokenizer.word_index
    nb_words = min(vocab_size, len(word_index)) # How many words are there actually

    embedding_matrix_glove = np.zeros((nb_words, embedding_dim))

    # Loop over all words in the word index
    for word, i in word_index.items():
        # If we are above the amount of words we want to use we do nothing
        if i >= vocab_size:
            continue
        # Get the embedding vector for the word
        embedding_vector = embeddings_index.get(word)
        if embedding_vector is not None:
            embedding_matrix_glove[i] = embedding_vector

    with open('embedding_matrix.pickle','wb') as s:
        pickle.dump(embedding_matrix, s)
################################################################
# Trained word2vec on data
################################################################
with open(CLUSTER_PATH+'w2vmatrix.pickle','rb') as f:
    embedding_matrix = pickle.load(f)

#One-hot encode the labels
data.loc[data['subreddit'] == 'conservative', 'LABEL'] = 0
data.loc[data['subreddit'] == 'liberal', 'LABEL'] = 1
labels = to_categorical(data['LABEL'], num_classes=2)

################################################################
# Network architecture and training
################################################################

learningrate = [0.1, 0.01]
inputlength = [1000,1200]
lr_dict = {}

'''
RNN-LSTM network which takes learningrate and inputlength as parameters to learn weights.
Embedding layer takes either pre-trained embedding or learns the weight vectors during the training.
NOTE - Do not call this function for larger sequences on local machine unless machine have huge memory.
'''
def lstm_network(lr, length):
    text_pad = pad_sequences(sequences, maxlen=length)
    print('Shape of post tensor:',text_pad.shape)
    model_name = str(lr)+str(length)+'_model'
    #split the data into train and test
    X_train, X_test, y_train, y_test = train_test_split(text_pad , np.array(output), test_size=0.2, random_state=42)

    #defining LSTM model
    model = Sequential()
    #if embedding is being used then add weight matrix to the embedding layer
    if IS_EMBEDDING:
        model.add(Embedding(vocab_size, emb_dim, embeddings_initializer=Constant(embedding_matrix), input_length=max_len, trainable=False))
    else:
        model.add(Embedding(vocab_size, emb_dim, input_length=length))
    model.add(SpatialDropout1D(0.5))
    model.add(LSTM(32, dropout=0.5, recurrent_dropout=0.5))
    model.add(Dense(2, activation='softmax'))

    rms = optimizers.RMSprop(lr=lr, rho=0.9, epsilon=None, decay=0.0)
    model.compile(optimizer=rms, loss='binary_crossentropy', metrics=['acc'])
    print(model.summary())
    history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,validation_split=0.25)

    #store history to plot loss and accuracy
    with open(CLUSTER_PATH+model_name+'_hist.pickle') as f:
        pickle.dump(history.history, f)
    #save model
    model.save_weights(CLUSTER_PATH+model_name+'.h5')

for lr in learningrate:
    for length in inputlength:
        lstm_network(lr, length)

accr = model.evaluate(X_test,y_test)
print('Test set\n  Loss: {:0.3f}\n  Accuracy: {:0.3f}'.format(accr[0],accr[1]))


from easysnmp import Session
import MySQLdb
import time,os,sys,json
from snmp import *
from db import *
from line import *
from mongo import *

hostip = ''
user = ''
passw = ''
dbase = ''

time_sleep = 300 # Unit = Second
i=0
j=0
try:
    db = connectdb(hostip,user,passw,dbase)
    host = getIp(db)
    nameAll = checkdevice(db)
    for item in range (len(host)):
        try:
            chk = 0
            snmp = connSession(host[item]['ip'],"cmumrtg")

#################################################################################
            oid = getOID(db,host[item]['model'])
            name = nameDevice(snmp,oid)
            ip = host[item]['id']
            uptime = int(upTimeDevice(snmp,oid))/100

            if checkdevice1(db,name) == 1:
                updatedevice(db,name,ip,uptime)
                print('updateDevice : '+name)
            else:
                insertdevice(db,name,ip,uptime)
                print('insertdevice : '+ name)

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

            device_id = checkid(db,name)
            typeport = ifType(snmp,db,nameAll,oid)
            ifoper = ifOper(snmp,db,nameAll,oid)
            chkcore = checkcore(db,name)

            for i in range (len(typeport)):
                try:
                    chk = 0
                    print(typeport[i])
                    if checkport(db,device_id,typeport[i])==device_id:
                        chk = 1
                    if chk == 1:
                        if(int(line_checkstat(db,name,typeport[i]))!=int(ifoper[i])):
                            dataline = line_senddata(db,int(device_id),typeport[i])
                            notify_line(ifoper[i],dataline)
                            updateport(db,device_id,typeport[i],ifoper[i])
                        if(chkcore==1):
                            updateconnect(db,device_id,typeport[i],ifoper[i])
                        if(chkcore==0 and checkconnect2(db,str(device_id),str(typeport[i]))==1):
                            updateconnect(db,device_id,typeport[i],ifoper[i])
                    else:
                        insertport(db,device_id,typeport[i],ifoper[i])
                        if(chkcore==1):
                            neiname = cdpName(snmp,db,nameAll,oid)
                            neiport = cdpIf(snmp,db,nameAll,oid)
                            if(checkconnect(db,str(device_id),str(typeport[i]),str(checkid(db,neiname[i])),str(neiport[i]))!=1):
                                insertconnect(db,device_id,typeport[i],ifoper[i],checkid(db,neiname[i]),neiport[i])
                        if(chkcore==0 and checkconnect1(db,str(device_id),str(typeport[i]))==0):
                            neiname = cdpName(snmp,db,nameAll,oid)
                            neiport = cdpIf(snmp,db,nameAll,oid)
                            if(checkcore(db,neiname[i]) == 1):
                                continue
                            else:
                                if(checkconnect(db,str(device_id),str(typeport[i]),str(checkid(db,neiname[i])),str(neiport[i]))!=1):
                                    insertconnect(db,device_id,typeport[i],ifoper[i],checkid(db,neiname[i]),neiport[i])

#####################################################################################
                except Exception as e:
                    print('Interface Fail : ' + str(e))
                    iscore = faildevice_if(db,host[item]['ip'])
                    if (iscore == 1) :
                        faildevice_connect(db,host[item]['ip'])

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

        except Exception as e:
            try:
                print('device Fail : ' + str(e))
                fail_idname = checkidbyip(db,host[item]['ip'])
                failport = findportbyid(db,fail_idname[0])
                for i in range(len(failport)):
                    if(int(line_checkstat(db,fail_idname[1],failport[i]['typeport'])) != 0):
                        dataline = line_senddata(db,int(fail_idname[0]),failport[i]['typeport'])
                        notify_line('0',dataline)
                faildevice_uptime(db,host[item]['ip'])
                iscore = faildevice_if(db,host[item]['ip'])
                faildevice_connect(db,host[item]['ip'])
            except Exception as error:
                print(str(error))
                print('No Device in Database or Cannot Connect to Device')
except Exception as e:
    print('Internet Problem')
    print(str(e))
closedb(db)


a = open("./input.txt")
s = a.read()

ans = s.count('(') - s.count(')')
print(ans)

c = 0
for idx, i in enumerate(s):
    if i == '(':
        c = c + 1
    else:
        c = c - 1
    if c == -1:
        print(idx + 1)
        break