import pytest
from framework.base_case import BaseCase
from framework.my_requests import MyRequests
from tests.assertions import Assertions
from tests.data_list_for_test import DataForCommon

id_req = '123-abc-321'
name = 'Jack'
surname = 'Lee'
age = 50
method = 'select'
filter_phone = '1234567890'

class TestCommon(BaseCase):
    def test_request_empty(self):
        response = MyRequests.any_method(data='{}')
        Assertions.check_failure_status_in_response(response=response)

    @pytest.mark.parametrize('data', DataForCommon.data_list_type_method)
    def test_check_type_methods(self, data):
        response = MyRequests.any_method(data=data)
        Assertions.check_failure_status_in_response(response=response)

    @pytest.mark.parametrize('data', DataForCommon.data_list_without_field_method)
    def test_without_field_method(self, data):
        response = MyRequests.any_method(data=data)
        Assertions.check_failure_status_in_response(response=response)

    @pytest.mark.parametrize('data', DataForCommon.data_list_method_is_none)
    def test_field_method_none(self, data):
        response = MyRequests.any_method(data=data)
        Assertions.check_failure_status_in_response(response=response)


from selenium import webdriver

page_type = -1 # set default

driver = webdriver.Firefox(executable_path="./geckodriver")

driver.fullscreen_window()
driver.implicitly_wait(30)

# for signal
import signal

'''
driver.window_handles[0] : Happy face (default)
driver.window_handles[1] : Map
driver.window_handles[2] : Sad face
'''
def signal_SIGUSR1_handler(signum, frame):
    print("Signal switching by signum", signum)
    global driver
    driver.switch_to.window(window_name=driver.window_handles[0])

def signal_SIGUSR2_handler(signum, frame):
    print("Signal switching by signum", signum)
    global driver
    driver.switch_to.window(window_name=driver.window_handles[1])

def signal_SIGUSR3_handler(signum, frame):
    print("Signal switching by signum", signum)
    global driver
    driver.switch_to.window(window_name=driver.window_handles[2])

signal.signal(signal.SIGUSR1, signal_SIGUSR1_handler)  # mac : kill -30 {pid}
# ps | grep chromeOpener | awk 'NR<2{print $1}' | xargs kill -30

signal.signal(signal.SIGUSR2, signal_SIGUSR2_handler)  # mac : kill -31 {pid}
# ps | grep chromeOpener | awk 'NR<2{print $1}' | xargs kill -31

signal.signal(signal.SIGINFO, signal_SIGUSR3_handler)  # mac : kill -29 {pid}
# ps | grep chromeOpener | awk 'NR<2{print $1}' | xargs kill -29

while True:
    # default
    if page_type == -1:

        driver.get("http://localhost:8080/")
        driver.execute_script("window.open('');")
        driver.switch_to.window(window_name=driver.window_handles[1])

        driver.get("http://localhost:8080/sample2")
        driver.execute_script("window.open('');")
        driver.switch_to.window(window_name=driver.window_handles[2])

        driver.get("http://localhost:8080/sample3")

        # set default page
        driver.switch_to.window(window_name=driver.window_handles[0])

    elif page_type == 0:
        print("self switch to page number 0")
        driver.switch_to.window(window_name=driver.window_handles[0])

    elif page_type == 1:
        print("self switch to page number 1")
        driver.switch_to.window(window_name=driver.window_handles[1])

    elif page_type == 2:
        print("self switch to page number 2")
        driver.switch_to.window(window_name=driver.window_handles[2])

    else:
        print("^^ㅗ")
        break
    page_type = int(input())

driver.quit()


# -*- coding: utf-8 -*-
"""Document directory_store here."""
import codecs
import logging
import os
import platform

from six import string_types
from six.moves.urllib import parse as urllib

from oaiharvest.record import Record

class DirectoryRecordStore(object):
    def __init__(self, directory, createSubDirs=False):
        self.directory = directory
        self.createSubDirs = createSubDirs
        self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__)

    def write(self, record: Record, metadataPrefix: str):
        fp = self._get_output_filepath(record.header, metadataPrefix)
        self._ensure_dir_exists(fp)
        self.logger.debug("Writing to file {0}".format(fp))
        with codecs.open(fp, "w", encoding="utf-8") as fh:
            fh.write(record.metadata)

    def delete(self, record: Record, metadataPrefix: str):
        fp = self._get_output_filepath(record.header, metadataPrefix)
        try:
            os.remove(fp)
        except OSError:
            # File probably does't exist in destination directory
            # No further action needed
            self.logger.debug("")
            pass

    def _get_output_filepath(self, header, metadataPrefix):
        filename = "{0}.{1}.xml".format(header.identifier(), metadataPrefix)

        protected = []
        if platform.system() != "Windows":
            protected.append(":")

        if self.createSubDirs:
            if isinstance(self.createSubDirs, string_types):
                # Replace specified character with platform path separator
                filename = filename.replace(self.createSubDirs, os.path.sep)

            # Do not escape path separators, so that sub-directories
            # can be created
            protected.append(os.path.sep)

        filename = urllib.quote(filename, "".join(protected))
        fp = os.path.join(self.directory, filename)
        return fp

    def _ensure_dir_exists(self, fp):
        if not os.path.isdir(os.path.dirname(fp)):
            # Missing base directory or sub-directory
            self.logger.debug("Creating target directory {0}".format(self.directory))
            os.makedirs(os.path.dirname(fp))


import logging

from flask import Flask, request

from picstitch import load_review_stars, load_amazon_prime, load_fonts, \
    PicStitch

from gcloud import storage
import boto
import io
import time
import os

# # ---- Logging prefs -----
log_format = "[%(asctime)s]       [%(process)d] [%(levelname)-1s] %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(level=logging.INFO,
                    format=log_format,
                    datefmt=date_format)

application = Flask(__name__)

def get_s3():
    # aws stuff
    s3_region = 'us-east-1'
    s3_bucket_name = 'if-kip-chat-images'
    conn = boto.s3.connect_to_region(s3_region)
    s3_bucket = conn.get_bucket(s3_bucket_name)
    return s3_bucket

def upload_to_s3(image, s3_bucket=get_s3()):
    tmp_img = io.BytesIO()
    image.created_image.save(tmp_img, 'PNG', quality=90)
    k = s3_bucket.new_key(image.uniq_fn)
    k.set_contents_from_string(tmp_img.getvalue(), headers={
                               "Content-Type": "image/png"})
    s3_base = 'https://s3.amazonaws.com/' + image.bucket_name + '/'
    img_url = s3_base + image.uniq_fn
    return img_url

def get_gcloud():
    # gcloud stuff
    gcloud_config = {
        'proj_name': 'kip_styles',
        'key': 'gcloud-picstitch.json',
        'bucket': 'if-kip-chat-images'
    }

    gcloud_key_file = os.path.join(
        os.path.dirname(__file__),
        'gcloud_key',
        gcloud_config['key']
    )

    gcloud_client = storage.Client(project=gcloud_config['proj_name'])
    gcloud_client = gcloud_client.from_service_account_json(gcloud_key_file)
    gcloud_bucket = gcloud_client.get_bucket(gcloud_config['bucket'])
    # gcloud_bucket.make_public(future=True)
    return gcloud_bucket

def upload_to_gcloud(image, gcloud_bucket=get_gcloud()):
    start = time.time()

    tmp_img = io.BytesIO()
    image.created_image.save(tmp_img, 'PNG', quality=90)
    saved = time.time()

    object_upload = gcloud_bucket.blob(
        os.path.join(image.origin, image.uniq_fn))
    blobbed = time.time()

    object_upload.upload_from_string(
        tmp_img.getvalue(), content_type='image/png')
    uploaded = time.time()

    if time.time() - start > 1:
        logging.info(
            'slow upload. save: %.2fs, blob create: %.2fs, string upload %2fs',
            saved - start, blobbed - saved, uploaded - blobbed)

    # public_url is a property func that appears to just be a string-format
    # call. Probably no value in instrumenting.
    return object_upload.public_url

@application.route('/', methods=['GET', 'POST'])
def main():
    '''
    return upload_image_tos_s3(create_image(request.json))
    '''
    t1 = time.time()
    img_req = request.json
    logging.info('received req to make image')
    pic = PicStitch(img_req=img_req,
                    # bucket=s3_bucket,
                    # gcloud_bucket=gcloud_bucket,
                    amazon_prime_image=amazon_images,
                    review_stars_images=review_star_images,
                    font_dict=font_dict)
    t2 = time.time()
    gc_url = upload_to_gcloud(pic, gcloud_bucket)
    t3 = time.time()
    logging.info('request complete. make: %.2fs, upload: %.2fs, total: %.2fs to %s',
                 t2 - t1, t3 - t2, t3 - t1, gc_url)
    return gc_url

@application.route('/health')
def kubernetes_heath_check():
    return 'health'

# load connections to gcloud and aws
gcloud_bucket = get_gcloud()
review_star_images = load_review_stars()
amazon_images = load_amazon_prime()
font_dict = load_fonts()

if __name__ == '__main__':
    port_num = 5000
    # run app
    logging.info('__not_threaded__')
    logging.info('running app on port ' + str(port_num))
    application.run(host='0.0.0.0', port=port_num, debug=True)


from dash import Dash, html, dcc
import plotly.express as px
import pandas as pd
import numpy as np
import statsmodels as sm
from scipy.stats import ttest_1samp
from statsmodels.stats.power import TTestPower
import plotly.express as px
import plotly.offline as pyo
import plotly.io as pio
from jupyter_dash import JupyterDash
from dash import Dash, dcc, html, Input, Output
from dash.dependencies import State
import chart_studio.plotly as py

app = Dash(__name__)

app.layout = html.Div([
    html.H1("P-value Simulation"),
    # html.Div([
    html.H4('# of Sims:', style={'display':'inline-block','margin-left':20,'margin-right':5}),
    dcc.Input(
    id='nSims',
    value='Initial Value',
    type = "number",
        ),
    # ]),
    # html.Div([
    html.H4('Sample Mean:', style={'display':'inline-block','margin-left':20,'margin-right':5}),
    dcc.Input(
    id='sample-mean',
    value='Initial Value',
    type = "number",
    ),
    # ]),
    # html.Div([
    html.H4('Sample Size:', style={'display':'inline-block','margin-left':20,'margin-right':5}),
    dcc.Input(
    id='sample-size',
    value='Initial Value',
    type = "number",
    ),
    # ])
    html.H4('Std. Dev:', style={'display':'inline-block','margin-left':20,'margin-right':5}),
    dcc.Input(
    id='std-dev',
    value='Initial Value',
    type = "number",
    ),
    html.Br(),
    html.Button('Submit', id='submit_val'),
    html.Div(id='container-button-basic',
             children='Enter all parameters and click submit'),
    html.Hr(),
    html.Label('Output'),
    html.Div(id='output-submit')
])

@app.callback(
    Output('output-submit', 'children'),
        [Input('submit_val', 'n_clicks'),
        #  Input('input-1-submit', 'n_blur'),
        #  Input('input-2-submit', 'n_submit'),
        #  Input('input-2-submit', 'n_blur')
         ],
        [State('nSims', 'value'),
        State('sample-mean', 'value'),
        State('sample-size', 'value'),
        State('std-dev', 'value')
        ]
)

# Use the below function to get all the input variables and calculate the p-values

def simulations_output(clicked, nSims, sample_mean, sample_size, std_dev):
    if clicked:
        p_value_list = [] # Initialize a list that will store all the p-values
        np.random.seed(1)
        for i in range(1,nSims):
            x = np.random.normal(loc=sample_mean, scale=std_dev, size=sample_size)
            t_stat, p_value = ttest_1samp(x, popmean=100)
            p_value_list.insert(i,p_value)
        # return p_value_list

        hist_df = pd.DataFrame({"p_values":p_value_list})
        bars = 20
        fig = px.histogram(hist_df, x="p_values")
        fig.update_traces(xbins=dict( # bins used for histogram
                start=0.0,
                end=1.0,
                size=0.05
            ))
        fig.update_layout(yaxis_range=[0,nSims], yaxis_title="Frequency of p-values", margin=dict(l=5, r=5, t=5, b=5))
        fig.add_hline(y=nSims/bars, line_width=3, line_dash="dash", line_color="red")
        return fig.show()

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


#!/usr/bin/env python

import numpy
import scipy.integrate
from pylab import *

datafile="../../../Mathematica/calculated_vals.tsv"

tag,x,e,f = numpy.loadtxt("data.txt",unpack=True)
tags=numpy.unique(tag)
flimit = numpy.zeros(len(tags))

for i in range(0,len(tags)):
    itag=tags[i]
    inds = numpy.where(tag == itag)
    xplot=x[inds]
    yplot=-f[inds]*31e-15
    isort=numpy.argsort(xplot)
    xplot = xplot[isort]
    yplot = yplot[isort]
    plot(xplot,yplot)
    flimit[i] = scipy.integrate.trapz(xplot,-yplot)

yscale('log')
xscale('log')
savefig('integrands.png')

clf()

dist,fpfa,fnaive,fright,ftemp=numpy.loadtxt(datafile,unpack=True)
dist=dist*1e6

plot(tags,flimit)
plot(dist,fpfa)
plot(dist,fright)
plot(dist,ftemp)
xscale('log')
yscale('log')
show()


import numpy as np
import math
import re
import feedparser as fp
def loadDataSet():
    postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                   ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                   ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
                   ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                   ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],
                   ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0,1,0,1,0,1]    #1是侮辱性文字，0是正常言论
    return postingList, classVec
def createVocabList(dataSet):
    vocabSet = set([])
    for document in dataSet:    #添加每篇文章中出现的新词
        vocabSet = vocabSet | set(document)
    return list(vocabSet)
def setOfWords2Vec(vocabList, inputSet):    #朴素贝叶斯的词集模型
    returnVec = [0] * len(vocabList)    #获取输入表的长度，建立一个所有元素都为0的向量
    for word in inputSet:   #遍历需要检查的集合，有匹配的将值变为1
        if word in vocabList:
            returnVec[vocabList.index(word)] = 1
        else:
            print("the word: %s is not in my Vocabulary!"%word)
    return returnVec
def trainNB0(trainMatrix, trainCategory):   #trainMatrix是经过01转换的矩阵
    numTrainDocs = len(trainMatrix) #获取训练矩阵的长度
    numWords = len(trainMatrix[0])  #获取训练矩阵的列数
    pAbusive = sum(trainCategory)/float(numTrainDocs)   #表示侮辱类概率
    p0Num = np.ones(numWords)
    p1Num = np.ones(numWords)
    p0Denom = 2.0
    p1Denom = 2.0
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p1Vect = p1Num / p1Denom
    p1Vect = [math.log(x) for x in p1Vect]
    p0Vect = p0Num / p0Denom
    p0Vect = [math.log(x) for x in p0Vect]
    return p0Vect, p1Vect, pAbusive
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
    p1 = sum(vec2Classify * p1Vec) + math.log(pClass1)
    p0 = sum(vec2Classify * p0Vec) + math.log(1.0 - pClass1)
    if p1 > p0:
        return 1
    else:
        return 0
def testingNB():
    listOPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listOPosts)
    trainMat = []
    for postinDoc in listOPosts:
        trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
    p0V, p1V, pAb = trainNB0(np.array(trainMat), np.array(listClasses))
    testEntry = ['love', 'my', 'dalmation']
    thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
    print(testEntry, 'classified as:', classifyNB(thisDoc, p0V, p1V ,pAb))
    testEntry = ['stupid', 'garbage']
    thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
    print(testEntry, 'classified as:', classifyNB(thisDoc, p0V, p1V, pAb))
def bagOfWords2VecMN(vocabList, inputSet):  #朴素贝叶斯的词袋模型
    returnVec = [0] * len(vocabList)
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] += 1
    return returnVec
def textParse(bigString):
    listOfTokens = re.split(r'\W*', bigString)  #使用split函数根据除了数字和英文字母以外的字符做分割
    return [tok.lower() for tok in listOfTokens if len(tok) > 2]    #转换为小写字母
def spamTest():
    docList=[]
    classList = []
    fullText =[]
    for i in range(1,26):
        wordList = textParse(open('email/spam/%d.txt' %i).read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(1) #在读取所有spam文件夹中的文件后，最后加入标记1
        wordList = textParse(open('email/ham/%d.txt' %i).read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(0) #在读取所有ham文件夹中的文件后，最后加入标记0
    vocabList = createVocabList(docList)
    trainingSet = range(50)
    testSet=[]
    for i in range(10): #随机选择其中的10份邮件
        randIndex = int(np.random.uniform(0, len(trainingSet))) #uniform(x,y)表示在[x,y)之间随机生成一个实数
        testSet.append(trainingSet[randIndex])
        del(trainingSet[randIndex])
    trainMat=[]
    trainClasses = []
    for docIndex in trainingSet:
        trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
        trainClasses.append(classList[docIndex])
    p0V,p1V,pSpam = trainNB0(np.array(trainMat), np.array(trainClasses))
    errorCount = 0
    for docIndex in testSet:
        wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
        if classifyNB(np.array(wordVector),p0V,p1V,pSpam) != classList[docIndex]:
            errorCount += 1
            print("classification error",docList[docIndex])
    print('the error rate is: ',float(errorCount) / len(testSet))
select = int(input("请输入你要选择的操作："))
if select == 1:
    listPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listPosts)
    print(setOfWords2Vec(myVocabList, listPosts[0]))
    print(setOfWords2Vec(myVocabList, listPosts[3]))
elif select == 2:
    listPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listPosts)
    trainMat = []
    for postinDoc in listPosts:
        trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
    p0V, p1V, pAb = trainNB0(trainMat, listClasses)
    print(pAb)
    print(p0V)
    print(p1V)
elif select == 3:
    testingNB()
elif select == 4:
    emailText = open('email/ham/6.txt').read()
    regEx = re.compile('\\W*')
    print(regEx.split(emailText))
elif select == 5:
    spamTest()
elif select == 6:
    ny = fp.parse('http://newyork.craigslist.org/stp/index.rss')
    print(len(ny['entries']))


import numpy as np
import sympy as sp
import math
from matplotlib import pyplot as plt

#BEGIN EXERCISE 1

def left_endpoint_sum(f,a,b,n):
  d = (b-a) / n
  sum = 0
  for i in range(0,n):
    sum += f(a + i*d)
  return d*sum

def right_endpoint_sum(f,a,b,n):
  d = (b-a) / n
  sum = 0
  for i in range(1,n+1):
    sum += f(a + i*d)
  return d*sum

#END EXERCISE 1

#BEGIN EXERCISE 2

x = sp.symbols('x')
f = lambda x : math.e**(-1*x**2)

LRS5 = left_endpoint_sum(f, 0, 0.5, 5) #Left Riemann Sum with 5 intervals
RRS5 = right_endpoint_sum(f, 0, 0.5, 5) #Right Riemann Sum with 5 intervals
LRS10 = left_endpoint_sum(f, 0, 0.5, 10)
RRS10 = right_endpoint_sum(f, 0, 0.5, 10)
LRS100 = left_endpoint_sum(f, 0, 0.5, 100)
RRS100 = right_endpoint_sum(f, 0, 0.5, 100)

xfunc = np.linspace(0,0.5,1001)
x5 = np.linspace(0,0.5,6)
x10 = np.linspace(0,0.5,11)
x100 = np.linspace(0,0.5,101)

yfunc = f(xfunc)
y5 = f(x5)
y10 = f(x10)
y100 = f(x100)

plt.step(x5, y5, 'b', where="post", label="Left Riemann Sum = " + str(LRS5))
plt.step(x5, y5, 'm', where="pre", label="Right Riemann Sum = " + str(RRS5))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 5 divisions")
plt.legend(fontsize=9)
plt.show()

plt.step(x10, y10, 'b', where="post", label="Left Riemann Sum = " + str(LRS10))
plt.step(x10, y10, 'm', where="pre", label="Right Riemann Sum = " + str(RRS10))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 10 divisions")
plt.legend(fontsize=9)
plt.show()

plt.step(x100, y100, 'b', where="post", label="Left Riemann Sum = " + str(LRS100))
plt.step(x100, y100, 'm', where="pre", label="Right Riemann Sum = " + str(RRS100))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 100 divisions")
plt.legend(fontsize=9)
plt.show()

#END EXERCISE 2

#BEGIN EXERCISE 3

def midpoint_sum(f,a,b,n):
  d = (b-a) / n
  sum = 0
  for i in range(0.5, n+0.5, 1):
    sum += f(a + i*d)
  return d*sum

def trapezoid_sum(f,a,b,n):
  d = (b-a) / n
  sum = 0
  for i in range(0, n+1):
    if i == 0:
      sum += f(a)
    elif i == n:
      sum += f(b)
    else:
      sum += 2*f(a + i*d)
  return 0.5*d*sum

#END EXERCISE 3

#BEGIN EXERCISE 4

x = sp.symbols('x')
f = lambda x : math.e**(-1*x**2)

MPS5 = midpoint_sum(f, 0, 0.5, 5) #Midpoint Sum with 5 intervals
TPS5 = trapezoid_sum(f, 0, 0.5, 5) #Trapezoidal Sum with 5 intervals
MPS10 = midpoint_sum(f, 0, 0.5, 10)
TPS10 = trapezoid_sum(f, 0, 0.5, 10)
MPS100 = midpoint_sum(f, 0, 0.5, 100)
TPS100 = trapezoid_sum(f, 0, 0.5, 100)

xfunc = np.linspace(0,0.5,1001)
x5 = np.linspace(0,0.5,6)
x10 = np.linspace(0,0.5,11)
x100 = np.linspace(0,0.5,101)

yfunc = f(xfunc)
y5 = f(x5)
y10 = f(x10)
y100 = f(x100)

plt.step(x5, y5, 'y', where="mid", label="Midpoint Sum = " + str(MPS5))
plt.plot(x5, y5, 'r', label="Trapezoidal Sum = " + str(TPS5))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 5 divisions")
plt.legend(fontsize=9)
plt.show()

plt.step(x10, y10, 'y', where="mid", label="Midpoint Sum = " + str(MPS10))
plt.plot(x10, y10, 'r', label="Trapezoidal Sum = " + str(TPS10))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 10 divisions")
plt.legend(fontsize=9)
plt.show()

plt.step(x100, y100, 'y', where="mid", label="Midpoint Sum = " + str(MPS100))
plt.plot(x100, y100, 'r', label="Trapezoidal Sum = " + str(TPS100))
plt.plot(xfunc,yfunc, 'k')
plt.title("f(x) = e**(-x**2) with 100 divisions")
plt.legend(fontsize=9)
plt.show()

#END EXERCISE 4

#BEGIN EXERCISE 5

def simpsons_rule_sum(f,a,b,n):
  assert n % 2 == 0, "n must be even"
  d = (b-a) / n
  sum = 0
  for i in range(0,n+1):
    if i == 0:
      sum += f(a)
    elif i == n:
      sum += f(b)
    elif i % 2 == 0:
      sum += 2*f(a + i*d)
    else:
      sum += 4*f(a + i*d)
  return (1/3)*d*sum

#END EXERCISE 5

#BEGIN EXERCISE 6

x = sp.symbols('x')
f = lambda x : math.e**(-1*x**2)

SRS10 = simpsons_rule_sum(f, 0, 0.5, 10) #Simpsons Rule Sum with 10 intervals
SRS100 = simpsons_rule_sum(f, 0, 0.5, 100)
print(SRS10)
print(SRS100)

#END EXERCISE 6

Approximations = []
Approximations += [("LRS5",LRS5), ("LRS10",LRS10), ("LRS100",LRS100)]
Approximations += [("RRS5",RRS5), ("RRS10",RRS10), ("RRS100",RRS100)]
Approximations += [("MPS5",MPS5), ("MPS10",MPS10), ("MPS100",MPS100)]
Approximations += [("TPS5",TPS5), ("TPS10",TPS10), ("TPS100",TPS100)]
Approximations += [("SRS10",SRS10), ("SRS100",SRS100)]
Approximations.sort(key = lambda y : y[1])
print(Approximations)

#BEGIN EXERCISE 7

x = sp.symbols('x')
f = lambda x : math.e**(-1*x**2)
fppmag = lambda x : abs(4*x**2 * math.e**(-1*x**2) - 2*math.e**(-1*x**2))
xtest = np.linspace(0,0.5,101)
ytest = fppmag(xtest)
plt.plot(xtest,ytest)
plt.show()
K = fppmag(0)

def midpoint_errorf(k,a,b,n):
  return (k*(b-a)**3) / (24*n**2)
def trapezoid_errorf(k,a,b,n):
  return (k*(b-a)**3) / (12*n**2)
def simpsons_errorf(k,a,b,n):
  return (k*(b-a)**5) / (180*n**4)

T10 = trapezoid_errorf(K,0,0.5,10)
M10 = midpoint_errorf(K,0,0.5,10)
S10 = simpsons_errorf(K,0,0.5,10)

Trange = (TPS10 - T10, TPS10 + T10)
Mrange = (MPS10 - M10, MPS10 + M10)
Srange = (SRS10 - S10, SRS10 + S10)

print(Trange) #T10
print(Mrange) #M10
print(Srange) #S10

#END EXERCISE 7

#BEGIN EXERCISE 8

x = sp.symbols('x')
f = lambda x : 1/x
L10 = left_endpoint_sum(f,1,5,10)
R10 = left_endpoint_sum(f,1,5,10)
T10 = trapezoid_sum(f,1,5,10)
M10 = midpoint_sum(f,1,5,10)
S10 = simpsons_rule_sum(f,1,5,10)

fppmag = lambda x : abs(2/(x**3))
xtest = np.linspace(1,5,101)
yest = fppmag(xtest)
plt.plot(xtest,ytest)
plt.show()

K = fppmag(1)
LE = L10-R10
RE = L10-R10
TE = trapezoid_errorf(K,1,5,10)
ME = midpoint_errorf(K,1,5,10)
SE = simpsons_errorf(K,1,5,10)

Lrange = (L10-LE,L10+LE)
Rrange = (R10-RE,R10+RE)
Trange = (T10-TE,T10+TE)
Mrange = (M10-ME,M10+ME)
Srange = (S10-SE,S10+SE)

print(Lrange)
print(Rrange)
print(Trange)
print(Mrange)
print(Srange)

LET = L10 - math.log(5)
RET = R10 - math.log(5)
TET = T10 - math.log(5)
MET = M10 - math.log(5)
SET = S10 - math.log(5)

print(LET) #True lefthand error
print(RET) #True righthand error
print(TET) #True trapezoidal error
print(MET) #True midpoint error
print(SET) #True simpsons error

#END EXERCISE 8


import yaml
import torch
import torch.nn as nn
from . import layers

class Model(nn.Module):

    def __init__(self, yaml_file):
        super(Model, self).__init__()

        with open(yaml_file, 'r') as file:
            try:
                model_cfg = yaml.load(file.read(), Loader=yaml.FullLoader)
            except:
                # for old verion or yaml
                model_cfg = yaml.load(file.read())

        self.layers  = []
        self.sources = []
        self.nums    = []
        self.input_indexs = set()
        layers_config = model_cfg['layers']
        for n, line in enumerate(layers_config):
            sources, layer_name, args, kwargs, num = line

            if isinstance(sources, int):
                sources = [sources]

            if not isinstance(num, int) or num <= 0:
                assert False, "layer's num must be int and > 0"

            self.layers.append(
                eval(f"layers.{layer_name}")(*args, **kwargs)
            )

            indexs = []
            for source in sources:
                if source < 0:
                    index = len(self.sources) + source
                    assert index >= 0, "找不到输入层"
                    indexs.append(index)
                else:
                    self.input_indexs.add(n)
                    indexs.append(-(source + 1))
            self.sources.append(indexs)
            self.nums.append(num)

        # get output layers index
        all_indexs = set()
        index_been_used = set()
        for i, indexs in enumerate(self.sources):
            all_indexs.add(i)
            for index in indexs:
                index_been_used.add(index)

        self.output_indexs = all_indexs - index_been_used
        self.layers = nn.Sequential(*self.layers)

    def get_layer_output(self, index, forward_dict):
        if index in forward_dict.keys():
            return forward_dict[index]
        else:
            source_outputs = []
            for source_index in self.sources[index]:
                source_outputs.append(self.get_layer_output(source_index, forward_dict))

            output = self.layers[index](*source_outputs)
            for i in range(self.nums[index] - 1):
                if not isinstance(output, list):
                    output = [output]
                output = self.layers[index](*output)

            forward_dict[index] = output
            return output

    def forward(self, *inputs, **kwargs):
        assert len(inputs) == len(self.input_indexs), ""
        forward_dict = {}
        for i, input in enumerate(inputs):
            forward_dict[-(i + 1)] = input

        outputs = [self.get_layer_output(output_index, forward_dict) for output_index in self.output_indexs]

        if len(outputs) == 1:
            return outputs[0]

        return outputs


"""
This module provides functions for justifying Unicode text in a monospaced
display such as a terminal.

We used to have our own implementation here, but now we mostly rely on
the 'wcwidth' library.
"""
from unicodedata import normalize

from wcwidth import wcswidth, wcwidth
from ftfy.fixes import remove_terminal_escapes

def character_width(char: str) -> int:
    r"""
    Determine the width that a character is likely to be displayed as in
    a monospaced terminal. The width for a printable character will
    always be 0, 1, or 2.

    Nonprintable or control characters will return -1, a convention that comes
    from wcwidth.

    >>> character_width('車')
    2
    >>> character_width('A')
    1
    >>> character_width('\N{ZERO WIDTH JOINER}')
    0
    >>> character_width('\n')
    -1
    """
    return wcwidth(char)

def monospaced_width(text: str) -> int:
    r"""
    Return the number of character cells that this string is likely to occupy
    when displayed in a monospaced, modern, Unicode-aware terminal emulator.
    We refer to this as the "display width" of the string.

    This can be useful for formatting text that may contain non-spacing
    characters, or CJK characters that take up two character cells.

    Returns -1 if the string contains a non-printable or control character.

    >>> monospaced_width('ちゃぶ台返し')
    12
    >>> len('ちゃぶ台返し')
    6
    >>> monospaced_width('owl\N{SOFT HYPHEN}flavored')
    12
    >>> monospaced_width('example\x80')
    -1

    A more complex example: The Korean word 'ibnida' can be written with 3
    pre-composed characters or 7 jamo. Either way, it *looks* the same and
    takes up 6 character cells.

    >>> monospaced_width('입니다')
    6
    >>> monospaced_width('\u110b\u1175\u11b8\u1102\u1175\u1103\u1161')
    6

    The word "blue" with terminal escapes to make it blue still takes up only
    4 characters, when shown as intended.
    >>> monospaced_width('\x1b[34mblue\x1b[m')
    4
    """
    # NFC-normalize the text first, so that we don't need special cases for
    # Hangul jamo.
    #
    # Remove terminal escapes before calculating width, because if they are
    # displayed as intended, they will have zero width.
    return wcswidth(remove_terminal_escapes(normalize("NFC", text)))

def display_ljust(text, width, fillchar=" "):
    """
    Return `text` left-justified in a Unicode string whose display width,
    in a monospaced terminal, should be at least `width` character cells.
    The rest of the string will be padded with `fillchar`, which must be
    a width-1 character.

    "Left" here means toward the beginning of the string, which may actually
    appear on the right in an RTL context. This is similar to the use of the
    word "left" in "left parenthesis".

    >>> lines = ['Table flip', '(╯°□°)╯︵ ┻━┻', 'ちゃぶ台返し']
    >>> for line in lines:
    ...     print(display_ljust(line, 20, '▒'))
    Table flip▒▒▒▒▒▒▒▒▒▒
    (╯°□°)╯︵ ┻━┻▒▒▒▒▒▒▒
    ちゃぶ台返し▒▒▒▒▒▒▒▒

    This example, and the similar ones that follow, should come out justified
    correctly when viewed in a monospaced terminal. It will probably not look
    correct if you're viewing this code or documentation in a Web browser.
    """
    if character_width(fillchar) != 1:
        raise ValueError("The padding character must have display width 1")

    text_width = monospaced_width(text)
    if text_width == -1:
        # There's a control character here, so just don't add padding
        return text

    padding = max(0, width - text_width)
    return text + fillchar * padding

def display_rjust(text, width, fillchar=" "):
    """
    Return `text` right-justified in a Unicode string whose display width,
    in a monospaced terminal, should be at least `width` character cells.
    The rest of the string will be padded with `fillchar`, which must be
    a width-1 character.

    "Right" here means toward the end of the string, which may actually be on
    the left in an RTL context. This is similar to the use of the word "right"
    in "right parenthesis".

    >>> lines = ['Table flip', '(╯°□°)╯︵ ┻━┻', 'ちゃぶ台返し']
    >>> for line in lines:
    ...     print(display_rjust(line, 20, '▒'))
    ▒▒▒▒▒▒▒▒▒▒Table flip
    ▒▒▒▒▒▒▒(╯°□°)╯︵ ┻━┻
    ▒▒▒▒▒▒▒▒ちゃぶ台返し
    """
    if character_width(fillchar) != 1:
        raise ValueError("The padding character must have display width 1")

    text_width = monospaced_width(text)
    if text_width == -1:
        return text

    padding = max(0, width - text_width)
    return fillchar * padding + text

def display_center(text, width, fillchar=" "):
    """
    Return `text` centered in a Unicode string whose display width, in a
    monospaced terminal, should be at least `width` character cells. The rest
    of the string will be padded with `fillchar`, which must be a width-1
    character.

    >>> lines = ['Table flip', '(╯°□°)╯︵ ┻━┻', 'ちゃぶ台返し']
    >>> for line in lines:
    ...     print(display_center(line, 20, '▒'))
    ▒▒▒▒▒Table flip▒▒▒▒▒
    ▒▒▒(╯°□°)╯︵ ┻━┻▒▒▒▒
    ▒▒▒▒ちゃぶ台返し▒▒▒▒
    """
    if character_width(fillchar) != 1:
        raise ValueError("The padding character must have display width 1")

    text_width = monospaced_width(text)
    if text_width == -1:
        return text

    padding = max(0, width - text_width)
    left_padding = padding // 2
    right_padding = padding - left_padding
    return fillchar * left_padding + text + fillchar * right_padding


import sys
from PyQt5 import QtCore, QtWidgets, uic
import mysql.connector as mc
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtWidgets import QMessageBox
from FrmMatakuliah import WindowMatakuliah

qtcreator_file  = "dashboard_admin.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtcreator_file)

class WindowDashboardAdmin(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        # Event Setup
        self.actionExit.triggered.connect(self.app_exit)
        self.actionMatakuliah_2.triggered.connect(self.app_matakuliah)

    def app_exit(self):
        sys.exit()

    def app_matakuliah(self):
        winmatakuliah.setWindowModality(QtCore.Qt.ApplicationModal)
        winmatakuliah.show()

    def messagebox(self, title, message):
        mess = QMessageBox()
        mess.setWindowTitle(title)
        mess.setText(message)
        mess.setStandardButtons(QMessageBox.Ok)
        mess.exec_()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = WindowDashboardAdmin()
    winmatakuliah = WindowMatakuliah()
    window.showFullScreen()
    sys.exit(app.exec_())
else:
    app = QtWidgets.QApplication(sys.argv)
    window = WindowDashboardAdmin()
    winmatakuliah = WindowMatakuliah()


from django import template

from all_products.queryutil import ShirtQuery

register = template.Library()

@register.filter
def shirt_price(shirt):
    shirt_query = ShirtQuery(shirt)
    for size in shirt_query.sizes:
    	stock = shirt_query.get_stock(size)
    	if stock > 0:
    		return shirt_query.get_price(size)


# -*- coding: utf-8 -*-
"""
Created on Mon Dec 24 13:13:32 2018

@author: Administrator
"""

import wget, time
import os

# 网络地址

DATA_URL = 'http://164.52.0.183:8000/file/findTrace/2018-12-24.txt'
# DATA_URL = '/home/xxx/book/data.tar.gz'
out_fname = '2018-12-24.txt'

def download(DATA_URL):
    out_fname = '2018-12-24.txt'
    date = time.ctime()
    path = str(date.split(' ')[1] +'-' + date.split(' ')[2])
    wget.download(DATA_URL, out=out_fname)

    if not os.path.exists('./' + out_fname):
        wget.download(DATA_URL, out=out_fname)
    else:
        #os.remove('./' + out_fname)
        print("today's data has been download")
    mkdir(path)

    return path

def mkdir(path):
    # 去除首位空格

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists=os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        os.makedirs(path)

        print(path +' 创建成功')
        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print(path +' 目录已存在')
        return False
# 提取压缩包
#tar = tarfile.open(out_fname)
#tar.extractall()
#tar.close()
# 删除下载文件
#os.remove(out_fname)

# 调用函数
path = download(DATA_URL)

file = open("./" + out_fname)
lines = file.readlines()
output = {}
temp = ""
cnt = 0
for line in lines:
    line=line.strip('\n')
    if line.startswith("FPS"):
        fps_split = line.split("=")
        #print(fps_split)
        fps_temp = fps_split[1]
        for i in range(1,cnt+1):
            output[temp][-i] += " "+fps_temp
        cnt = 0
    elif line.startswith("ID:dokidoki/mlinkm/"):
        Channel_ID_1200 = line[19:]
        if Channel_ID_1200 in output:
            temp = Channel_ID_1200 + "_high"
        else:
            output[Channel_ID_1200 + "_high"] = []
            temp = Channel_ID_1200 + "_high"
        cnt = 0
    elif line.startswith("ID:EXT-ENC-0/dokidoki/mlinkm/"):
        Channel_ID_500 = line[29:]
        if Channel_ID_1200 in output:
            temp = Channel_ID_500 + "_low"
        else:
            output[Channel_ID_500 + "_low"] = []
            temp = Channel_ID_500 + "_low"
        cnt = 0
    else:
        output[temp].append(line)
        cnt += 1
for key,value in output.items():
    f_file = open("./" + path + "/" + str(key) + ".csv","w")
    for idx in range(len(value)):
        data = value[idx].replace(" ",",")
        data += "\n"
        f_file.write(data)
#print(output)
        #print(Channel_ID_500)


from pymongo import MongoClient

client = MongoClient('localhost', 27017)

database = client.mflix
pipline = [
    {'$unwind':'$cast'},
    {'$group':
        {
        '_id':'$cast',
        'count':{'$sum':1}
        }},
        {
        '$sort':{'count':-1}
        }]
actors = database.movies.aggregate(pipline)
for actor in actors:
    print(actor)


import copy
import numpy as np
import cv2
import time
import random
import argparse

def draw_obstacles(canvas,clr=5,unknown=False, map_flag=1):
    """
    @brief: This function goes through each node in the canvas image and checks for the
    obstacle space using the half plane equations.
    If the node is in obstacle space, the color is changed to blue.
    :param canvas: Canvas Image
    :param unknown: Unknown Map flag
    """
    # Uncomment to use the cv2 functions to create the obstacle space
    # cv2.circle(canvas, (300,65),45,(255,0,0),-1)
    # cv2.fillPoly(canvas, pts = [np.array([[115,40],[36,65],[105,150],[80,70]])], color=(255,0,0)) #Arrow
    # cv2.fillPoly(canvas, pts = [np.array([[200,110],[235,130],[235,170],[200,190],[165,170],[165,130]])], color=(255,0,0)) #Hexagon

    height,width,_ = canvas.shape

    for i in range(width):
        for j in range(height):
            if map_flag == 1:
                # Map 01
                if(i<=clr) or (i>=(400-clr)) or (j<=clr) or (j>=(250-clr)):
                    canvas[j][i] = [255,0,0]

                if ((i-300)**2+(j-65)**2-((40+clr)**2))<=0:
                    canvas[j][i] = [255,0,0]

                if (j+(0.57*i)-213.53)>=clr and (j-(0.57*i)+5.04+clr)>=0 and (i-235-clr)<=0 and (j+(0.57*i)-305.04-clr)<=0 and (j-(0.57*i)-76.465-clr)<=0 and (i-155-clr)>=0:
                    canvas[j][i] = [255,0,0]

                if ((j+(0.316*i)-66.1483-clr)>=0 and (j+(0.857*i)-140.156-clr)<=0 and (j-(0.114*i)-55.909-clr)<=0) or ((j-(1.23*i)-23.576-clr)<=0 and (j-(3.2*i)+197.763+clr)>=0 and (j-(0.114*i)-55.909-clr)>=0):
                    canvas[j][i] = [255,0,0]

            elif map_flag == 2:
                # Map 02
                if(i<=clr) or (i>=(400-clr)) or (j<=clr) or (j>=(250-clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=clr) and (j<=63)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=103-clr) and (j<=147+clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=187-clr) and (j<=(250-clr))):
                    canvas[j][i] = [255,0,0]

                if ((i>=251-clr) and (i<=281+clr) and (j>=42-clr) and (j<=105+clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=251-clr) and (i<=281+clr) and (j>=145-clr) and (j<=208+clr)):
                    canvas[j][i] = [255,0,0]

    if(unknown):
        for i in range(width):
            for j in range(height):

                if map_flag == 1:
                    if ((i-110)**2+(j-210)**2-((35)**2))<=0:
                        canvas[j][i] = [0,255,0]
                elif map_flag == 2:
                    if ((i-70)**2+(j-190)**2-((35)**2))<=0:
                        canvas[j][i] = [0,255,0]

                    if ((i-200)**2+(j-140)**2-((20)**2))<=0:
                        canvas[j][i] = [0,255,0]

    return canvas

def is_obstacle(next_width,next_height,canvas,unknown=False):
    """
    @brief: This function checks if the node is present in the obstacle.
    If the node is in obstacle space, the function returns true.
    :param canvas: Canvas Image
    :param unknown: Unknown Map flag
    """
    if(unknown):
        if canvas[int(round(next_height))][int(round(next_width))][1]==255 or canvas[int(round(next_height))][int(round(next_width))][0]==255:
            return True
        else:
            return False
    else:
        if canvas[int(round(next_height))][int(round(next_width))][0]==255:
            # print("In obstacle")
            return True
        else:
            return False

def cost_to_goal(node,final):
    """
    @brief: This function computes the euclidean distance between the current node and the final goal node.
    :param node: present node
    :param final: final node (Goal Node)
    """
    return np.sqrt(np.power(node[0]-final[0],2)+np.power(node[1]-final[1],2))

def compute_distance(node1,node2):
    """
    @brief: This function computes the euclidean distance between the two given nodes. Mainly used to compute
    the edge length.
    :param node1: First Node
    :param node2: Second Node
    """
    return np.sqrt(np.power(node1[0]-node2[0],2)+np.power(node1[1]-node2[1],2))

def nearest(sample, TV):
    """
    @brief: This function returns the nearest node from the previously generated vertices.
    :param sample: sampled node
    :param TV: Tree Vertices
    """
    dist = []
    for vertex in TV:
        dist.append(compute_distance(sample,vertex))

    nearest_vertex = TV[dist.index(min(dist))]
    return nearest_vertex

def neighbor_nodes(sample, TV, d=10):
    """
    @brief: This function computes the nearby neighbouring nodes at a particular distance threshold from
    the sample and the tree vertices.
    :param sample: Node from which the neighbouring nodes are to be returned
    :param TV: Tree vertices
    """
    neighbors = []
    for vertex in TV:
        if compute_distance(sample,vertex) < d:
            neighbors.append(vertex)

    return neighbors

def collision_free(X_nearest,X_rand,canvas,unknown=False): #Replace X_rand with X_new for steer function
    """
    @brief: This function samples the edge and checks for the validity of the edge by checking the obstacle space
    :param X_nearest: Nearest Node
    :param X_rand: Random node
    :param canvas: Map
    :unknown: Flag for unknown map
    """
    if X_rand[0] != X_nearest[0]:
        x1 = min(X_nearest[0],X_rand[0])
        if(X_nearest[0] == x1):
            for w in range(X_nearest[0],X_rand[0]+1):
                h = ((X_rand[1] - X_nearest[1])/(X_rand[0] - X_nearest[0]))*(w - X_nearest[0]) + X_nearest[1]
                if(is_obstacle(int(w),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False
        else:
            for w in range(X_rand[0],X_nearest[0]+1):
                h = ((X_nearest[1] - X_rand[1])/(X_nearest[0] - X_rand[0]))*(w - X_rand[0]) + X_rand[1]
                if(is_obstacle(int(w),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False

    else:
        y1 = min(X_nearest[1],X_rand[1])
        if(y1 == X_nearest[1]):
            for h in range(X_nearest[1],X_rand[1]+1):
                if(is_obstacle(int(X_nearest[0]),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False
        else:
            for h in range(X_rand[1],X_nearest[1]+1):
                if(is_obstacle(int(X_rand[0]),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False

    return True

def rewire(node1,node2,node_dict,canvas,final_state):
    """
    @brief: This function rewires the edge between the nodes by checking the edge length and the cost to goal.
    :param node1: Node 1
    :param node2: Node 2
    :param node_dict: Dictionary containing the Parent nodes and costs
    :param canvas: Map
    :param final_state: Goal Node
    """
    # print("In rewire")
    parent = []
    if collision_free(node1,node2, canvas) is True:
        if (compute_distance(node1, node2) + cost_to_goal(node1, final_state)) < cost_to_goal(node2, final_state):
            node_dict[tuple(node2)] = [node1, compute_distance(node1, node2) + cost_to_goal(node1, final_state)]
            parent = node1.copy()
    if len(parent) != 0:
        return parent
    else:
        return node_dict[tuple(node2)][0]

def mod_rrt_star(initial_state,final_state,canvas):
    """
    @brief: This function generates the random tree for the given obstacle map.
    :param initial_state: Start Node
    :param final_state: final node (Goal Node)
    :param canvas: Map
    """
    TV = []
    TE = {}
    TV.append(final_state)
    node_dict = {}
    node_dict[tuple(final_state)] = final_state
    while True:
        width_rand = random.randint(0,canvas.shape[1]-1)
        height_rand = random.randint(0,canvas.shape[0]-1)

        X_rand = [width_rand,height_rand]
        # print("Random sample: ", X_rand)
        X_nearest = nearest(X_rand,TV)

        #Steer function to be implemented later for non-holonomic constraints.
        #X_new <- Steer(X_rand, X_nearest)
        #Here X_rand is X_new
        if(collision_free(X_nearest, X_rand, canvas) is False):
            continue

        X_parent = X_nearest.copy()
        node_dict[tuple(X_rand)] = [X_parent, cost_to_goal(X_nearest,final_state) + compute_distance(X_nearest,X_rand)]

        X_neighbors = neighbor_nodes(X_rand, TV, 10)

        for n in X_neighbors:
            X_parent = rewire(n, X_rand, node_dict, canvas, final_state)

        TV.append(X_rand)
        TE[tuple(X_rand)] = X_parent.copy()
        # print("X_parent", X_parent)

        for n in X_neighbors:
            X_parent_temp = rewire(X_rand, n, node_dict, canvas, final_state)

            if X_parent_temp == X_rand:
                # print("Before Pop", n)
                TE.pop(tuple(n))
                TE[tuple(n)] = X_rand.copy()

        if compute_distance(X_rand,initial_state) < 5:
            print("RRT* Converged!")
            return TE, TV, X_rand

def backtrack(initial_state, final_state, edges, canvas):
    """
    @brief: This function backtracks the path from the goal node to the start node.
    :param initial_state: start node
    :param final_state: final node (Goal Node)\
    :param edges: Edges between the nodes
    """
    state = initial_state.copy()
    path = []
    while True:
        node = edges[tuple(state)]
        path.append(state)
        # cv2.line(canvas, tuple(state), tuple(node), (0, 255, 230), 3)
        if(tuple(node) == tuple(final_state)):
            path.append(final_state)
            print("Back Tracking Done!")
            break
        state = node.copy()
    return path

def path_sampling(path):
    """
    @brief: This function samples the generated path
    :param path: path from start node to the goal node
    """
    sampled_path = []
    for i in range(0,len(path)-1):
        X_rand = path[i]
        X_nearest = path[i+1]
        if X_rand[0] != X_nearest[0]:
            x1 = min(X_nearest[0],X_rand[0])
            if(X_nearest[0] == x1):
                for w in range(X_nearest[0],X_rand[0]+1):
                    h = ((X_rand[1] - X_nearest[1])/(X_rand[0] - X_nearest[0]))*(w - X_nearest[0]) + X_nearest[1]
                    sampled_path.append([int(w),int(h)])
            else:
                for w in range(X_rand[0],X_nearest[0]+1):
                    h = ((X_nearest[1] - X_rand[1])/(X_nearest[0] - X_rand[0]))*(w - X_rand[0]) + X_rand[1]
                    sampled_path.append([int(w),int(h)])

        else:
            print("vertical line", X_nearest[1], X_rand[1])
            y1 = min(X_nearest[1],X_rand[1])
            print("y1 ", y1)
            if(y1 == X_nearest[1]):
                for h in range(X_nearest[1],X_rand[1]+1):
                    sampled_path.append([int(X_nearest[0]),int(h)])
            else:
                for h in range(X_rand[1],X_nearest[1]+1):
                    sampled_path.append([int(X_rand[0]),int(h)])
    return sampled_path

def path_smoothening(sampled_path, final_state, canvas, unknown = False):
    """
    @brief: This function smoothenes the path by connecting the start nodes with the most feasible node starting
    from the goal node
    :param sampled path: Sampled Path
    :param final_state: final node (Goal Node)
    :param canvas: Map
    :param unknown: Flag for dynamic map ( unknown obstacles )
    """
    shortest_path = []
    # if len(sampled_path) > 0:
    shortest_path.append(sampled_path[0])
    print("Length of Sampled Path: ",len(sampled_path))
    while (tuple(shortest_path[-1]) != tuple(sampled_path[-1])):
        # print(sampled_path.index(shortest_path[-1]))
        for i in range(sampled_path.index(shortest_path[-1]),len(sampled_path)):
            if collision_free(shortest_path[-1],sampled_path[len(sampled_path)-1-i+sampled_path.index(shortest_path[-1])], canvas, unknown):
                shortest_path.append(sampled_path[len(sampled_path)-1-i+sampled_path.index(shortest_path[-1])])
                break
    # print(shortest_path)
    return shortest_path

def path_replanning(path, dynamic_map, edges, vertices, initial, final):
    """
    @brief: This function replans the path based on the dynamic obstacles present in the map.
    :param path: actual path to be followed
    :param dynamic_map: Dynamic map
    :param edges: Edges
    :param vertices: Vertices of the tree
    :param initial: starting node
    :param final: Goal Node
    """
    replanned_path = path.copy()
    print("in path replanning")
    for i in range(1,len(path)):
        node = replanned_path[i]
        X_next = node.copy()
        if is_obstacle(node[0],node[1],dynamic_map,True) or (not collision_free(replanned_path[i-1],X_next,dynamic_map, unknown=True)):
            X_curr = replanned_path[i-1].copy()
            X_candi = []
            X_near = neighbor_nodes(X_curr, vertices, d=50)
            for near in X_near:
                if collision_free(X_curr,near,dynamic_map, unknown=True):
                    if near not in replanned_path:
                        X_candi.append(near)

            X_next = pareto(X_candi,X_curr,final)
            # X_next = X_candi[0]
        if(X_next is not None):
            # print("Nearby node found!")
            # X_curr = X_next.copy()
            # print("Previous: ",replanned_path[i])
            replanned_path[i] = X_next.copy()
            # print("Updated: ",replanned_path[i])

        else:
            print("Not Enough Samples found nearby and hence the path goes through the obstacle")

    # print("Replanned Path: ", replanned_path)
    return replanned_path

def pareto(X_candi,initial,final):
    """
    @brief: This function returns the most dominant node by using the pareto dominance theory
    :param X_candi: Candidate Nodes
    :param initial: Initial Node
    :paran final: Final Node (Goal Node)
    """
    paretos = []
    for candidates in X_candi:
        paretos.append([compute_distance(candidates,initial), cost_to_goal(candidates,final)])

    if(len(paretos) != 0):
        pareto_dict = {}
        for i in range(0,len(paretos)):
            dominant_node = paretos[i].copy()
            ID = 0
            OD = 0
            for j in range(0,len(paretos)):
                if(tuple(paretos[i]) == tuple(paretos[j])):
                    continue
                elif ((paretos[j][0]<=dominant_node[0] and paretos[j][1]<=dominant_node[1]) and (paretos[j][0]<dominant_node[0] or paretos[j][1]<dominant_node[1])):
                    ID += 1
                elif (((paretos[j][0]>=dominant_node[0] and paretos[j][1]>=dominant_node[1]) and (paretos[j][0]>dominant_node[0] or paretos[j][1]>dominant_node[1]))):
                    OD += 1
            pareto_dict[tuple(dominant_node)] = [ID, OD]

        pareto_keys = list(pareto_dict.keys())
        pareto_IDs = []
        pareto_ODs = []
        for p_key in pareto_keys:
            pareto_IDs.append(pareto_dict[tuple(p_key)][0])
            pareto_ODs.append(pareto_dict[tuple(p_key)][1])
        zero_ID_index = list(np.where(np.array(pareto_IDs)==0))[0]
        # print("Zero ID Index Type: ",type(zero_ID_index), zero_ID_index)
        if(len(zero_ID_index)>1):
            zero_ID_keys = []
            for i in zero_ID_index:
                zero_ID_keys.append(pareto_keys[i])

            zero_ID_max_OD = []
            for key in zero_ID_keys:
                zero_ID_max_OD.append(pareto_dict[tuple(key)][1])
            max_OD = np.max(zero_ID_max_OD)
            max_OD_key = zero_ID_keys[zero_ID_max_OD.index(max_OD)]

            # print(max_OD_key)
            return X_candi[paretos.index(list(max_OD_key))]
        elif(len(zero_ID_index)==1):
            return X_candi[paretos.index(list(pareto_keys[zero_ID_index[0]]))]
        else:
            print("NO PARETO!")

    else:
        print("No Candidate Nodes")

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--map1', action='store_true',
                        help="Loads Map 01")
    parser.add_argument('--map2', action='store_true',
                        help="Loads Map 02")
    args = parser.parse_args()

    #Gives the time at which the program has started
    canvas = np.ones((250,400,3),dtype="uint8") #Creating a blank canvas

    if args.map1:
        flag = 1
    elif args.map2:
        flag = 2

    canvas = draw_obstacles(canvas,clr=5,unknown=False,map_flag=flag) #Draw the obstacles in the canvas, default point robot with 5 units of clearance

    initial_state,final_state = [10,10], [350,150] #Take the start and goal node from the user

    #Changing the cartesian coordinates to image coordinates:
    initial_state[1] = canvas.shape[0]-1 - initial_state[1]
    final_state[1] = canvas.shape[0]-1 - final_state[1]

    #Write a condition to check if the initial state and final state are in the obstacle space and exit from program and ask to rerun with valid start and goal positions
    if(canvas[initial_state[1]][initial_state[0]][0]==255 or canvas[final_state[1]][final_state[0]][0]==255):
        print("Given Start or Goal Node is in the Obstacle Region. Please re-run with Valid Coordinates")
        exit()

    start_time = time.time()
    #Start Node and Goal Node
    cv2.circle(canvas,(int(initial_state[0]),int(initial_state[1])),5,(0,255,0),-1)
    cv2.circle(canvas,(int(final_state[0]),int(final_state[1])),5,(0,0,255),-1)

    #Generate the graph from the Modified RRT* Algorithm
    edges, vertices, s_node = mod_rrt_star(initial_state,final_state,canvas) #Compute the path using A Star Algorithm

    tree_canvas = canvas.copy()
    #Draw the edges
    for key in edges:
        # cv2.line(canvas, tuple(key), tuple(edges[key]), (255, 128, 223), 2)
        cv2.line(tree_canvas, tuple(key), tuple(edges[key]), (0, 0, 255), 3)
        cv2.circle(tree_canvas,(key[0], key[1]), 1, (0,255,255), -1)
        cv2.circle(tree_canvas,(edges[key][0],edges[key][1]), 1, (0,255,0), -1)
    # cv2.imshow("Modified RRT* Tree Expansion", canvas)

    #Generate a dynamic map
    dynamic_map = np.ones((250,400,3),dtype="uint8") #Creating a blank canvas
    dynamic_map = draw_obstacles(dynamic_map,clr=5,unknown=True, map_flag=flag) #Draw the obstacles in the canvas, default point robot with 5 units of clearance
    cv2.imshow("Known Map with Unknown Obstacles", dynamic_map)

    #Backtack the path to reach from start node to goal node
    path = backtrack(s_node, final_state, edges, canvas)
    rrt_path_canvas = tree_canvas.copy()
    for i in range(1,len(path)):
        cv2.line(rrt_path_canvas, tuple(path[i-1]), tuple(path[i]), (0, 255, 0), 3)
    # cv2.imshow("Modified RRT* Path", rrt_path_canvas)

    #Sample and Smoothen the path from the list returned from the backtracking function.
    sampled_path = path_sampling(path)

    smoothened_path = path_smoothening(sampled_path.copy(),final_state,canvas,unknown = False)

    smooth_rrt_path_canvas = rrt_path_canvas.copy()
    for i in range(1,len(smoothened_path)):
        cv2.line(smooth_rrt_path_canvas, tuple(smoothened_path[i-1]), tuple(smoothened_path[i]), (255, 255, 255), 3)
    # cv2.imshow("Smoothened Modified RRT* Path", smooth_rrt_path_canvas)

    #Resample the smoothened path
    sampled_path = path_sampling(smoothened_path)

    #Replan the path from the dynamic obstacles
    replanned_path = path_replanning(sampled_path, dynamic_map, edges, vertices,s_node,final_state)

    replanned_path_canvas = dynamic_map.copy()
    for i in range(0,len(replanned_path)):
        cv2.circle(replanned_path_canvas,tuple(replanned_path[i]), 2, (0,145,145), -1)

    n_path  = []
    prev_path = []
    for i in range(0,len(replanned_path)):
        if(tuple(sampled_path[i]) == tuple(replanned_path[i])):

            prev_path.append(sampled_path[i])
            continue
        else:
            # print("N Path Append")
            n_path.append(sampled_path[i-2])
            n_path.append(sampled_path[i-1])
            for j in range(i,len(replanned_path)):
                n_path.append(replanned_path[j])
            break

    # cv2.imshow("Replanned Modified RRT* Path", replanned_path_canvas)

    # replanned_sampled = path_sampling(replanned_path)
    # print("New path ",n_path)
    new_replanned_path = path_smoothening(n_path.copy(), final_state, dynamic_map, unknown=True)

    smooth_replanned_path_canvas = replanned_path_canvas.copy()
    for i in range(1,len(sampled_path)):
        cv2.line(dynamic_map, tuple(sampled_path[i-1]), tuple(sampled_path[i]), (0, 137, 255), 3)

    # print(replanned_path)
    for i in range(1,len(prev_path)):
        cv2.line(smooth_replanned_path_canvas, tuple(prev_path[i-1]), tuple(prev_path[i]), (255, 128, 223), 2)
    for i in range(1,len(new_replanned_path)):
        cv2.line(smooth_replanned_path_canvas, tuple(new_replanned_path[i-1]), tuple(new_replanned_path[i]), (255, 128, 223), 2)
        # cv2.circle(dynamic_map,tuple(replanned_path[i]), 3, (255,255,255), -1)
    # cv2.imshow("Smoothened Replanned Modified RRT* Path",dynamic_map)

    end_time = time.time() #Time taken to run the whole algorithm to find the optimal path

    cv2.imshow("Known Map with Initial & Final Nodes", canvas)
    cv2.imshow("Modified RRT* Tree Expansion", tree_canvas)
    # cv2.imshow("Known Map with Unknown Obstacles", dynamic_map)
    cv2.imshow("Modified RRT* Path", rrt_path_canvas)
    cv2.imshow("Smoothened Modified RRT* Path", smooth_rrt_path_canvas)
    # cv2.imshow("Replanned Pareto Dominant Nodes", replanned_path_canvas)
    cv2.imshow("Smoothened Replanned Modified RRT* Path", smooth_replanned_path_canvas)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # print("Code Execution Time: ",end_time-start_time) #Prints the total execution time


from pathlib import Path
from typing import IO

def sentencepiece_load(file):
    """Load a SentencePiece model"""
    from sentencepiece import SentencePieceProcessor
    spm = SentencePieceProcessor()
    spm.Load(str(file))
    return spm

# source: https://github.com/allenai/allennlp/blob/master/allennlp/common/file_utils.py#L147  # NOQA
def http_get_temp(url: str, temp_file: IO) -> None:
    import requests
    import warnings
    from urllib3.exceptions import InsecureRequestWarning

    # temporary fix for dealing with this SSL certificate issue:
    # https://github.com/bheinzerling/bpemb/issues/63
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", InsecureRequestWarning)
        req = requests.get(url, stream=True, verify=False)

    req.raise_for_status()
    content_length = req.headers.get('Content-Length')
    total = int(content_length) if content_length is not None else None
    try:
        from tqdm import tqdm
        progress = tqdm(unit="B", total=total)
    except ImportError:
        progress = None
    for chunk in req.iter_content(chunk_size=1024):
        if chunk:  # filter out keep-alive new chunks
            if progress is not None:
                progress.update(len(chunk))
            temp_file.write(chunk)
    if progress is not None:
        progress.close()
    return req.headers

# source: https://github.com/allenai/allennlp/blob/master/allennlp/common/file_utils.py#L147  # NOQA
def http_get(url: str, outfile: Path, ignore_tardir=False) -> None:
    import tempfile
    import shutil
    with tempfile.NamedTemporaryFile() as temp_file:
        headers = http_get_temp(url, temp_file)
        # we are copying the file before closing it, flush to avoid truncation
        temp_file.flush()
        # shutil.copyfileobj() starts at current position, so go to the start
        temp_file.seek(0)
        outfile.parent.mkdir(exist_ok=True, parents=True)
        if headers.get("Content-Type") == "application/x-gzip":
            import tarfile
            tf = tarfile.open(fileobj=temp_file)
            members = tf.getmembers()
            if len(members) != 1:
                raise NotImplementedError("TODO: extract multiple files")
            member = members[0]
            if ignore_tardir:
                member.name = Path(member.name).name
            tf.extract(member, str(outfile.parent))
            extracted_file = outfile.parent / member.name
            assert extracted_file == outfile, "{} != {}".format(
                extracted_file, outfile)
        else:
            with open(str(outfile), 'wb') as out:
                shutil.copyfileobj(temp_file, out)
    return outfile

def load_word2vec_file(word2vec_file, add_pad=False, pad="<pad>"):
    """Load a word2vec file in either text or bin format."""
    from gensim.models import KeyedVectors
    word2vec_file = str(word2vec_file)
    binary = word2vec_file.endswith(".bin")
    vecs = KeyedVectors.load_word2vec_format(word2vec_file, binary=binary)
    if add_pad:
        if pad not in vecs:
            add_embeddings(vecs, pad)
        else:
            raise ValueError("Attempted to add <pad>, but already present")
    return vecs

def add_embeddings(keyed_vectors, *words, init=None):
    import numpy as np
    if init is None:
        init = np.zeros
    vectors_to_add = init((len(words), keyed_vectors.vectors.shape[1]))
    keyed_vectors.add_vectors(words, vectors_to_add)
    return keyed_vectors.vectors.shape[0]