# import numpy as np
# import matplotlib.pyplot as plt
# import os
# from skimage.io import imread
# from skimage.draw import polygon
# import pandas as pd
# from sklearn.metrics import confusion_matrix, classification_report
# from skimage.util import img_as_float
#
# disaster_type = 'wind'
# labelsDB = pd.read_pickle('All_Data_Props.pkl')
# labelsDB = labelsDB[labelsDB['Group'] != 'Test']
# labelsDB_post = labelsDB[labelsDB['Pre_Post'] == 'post']
# labelsDB_post_gt0 = labelsDB_post[labelsDB_post['buildings#'] > 0]
# labelsDB_post_gt0_disaster = labelsDB_post_gt0[labelsDB_post_gt0['disaster_type'] == disaster_type].reset_index()
# print(labelsDB_post_gt0_disaster.shape, ' size of database of post disaster images which have buildings.')
# print(labelsDB_post_gt0_disaster)
#
# df = labelsDB_post_gt0_disaster[['destroyed#', 'minor-damage#', 'major-damage#', 'no-damage#']]
# df = pd.concat((labelsDB_post_gt0_disaster, df.prod(axis=1)), axis=1).sort_values(by=0, ascending=False).drop(
#     columns=0).head(20)
#
# classes = ['no-damage', 'minor-damage', 'major-damage', 'destroyed', 'un-classified']
#
# y_true = np.empty((0, 1))
# y_pred = np.empty((0, 1))
#
# for i in range(len(df['img_name'])):
#     name = (df['img_name'].iloc[i].split('/')[-1] + '.npy')
#     classified = np.load('./training_0/classified_images_SVM_2000/' + name)
#
#     post = imread(df['img_name'].iloc[i].replace('labels', 'images') + '.png')
#     pre = imread(df['img_name'].iloc[i].replace('labels', 'images').replace('post', 'pre') + '.png')
#     labels = pd.read_json(open(df['img_name'].iloc[i] + '.json', ))['features']['xy']
#     mask = np.zeros((1024, 1024))
#     classified[classified == 3] = 2
#     for building in labels:
#         Class = classes.index(building['properties']['subtype']) + 1
#         vertices = building['wkt'].partition('POLYGON ((')[2].partition('))')[0].split(', ')
#         rows = []
#         cols = []
#         for vertex in vertices:
#             cols.append(float(vertex.split(' ')[0]))
#             rows.append(float(vertex.split(' ')[1]))
#         rr, cc = polygon(rows, cols, (1024, 1024))
#         pixels = list(zip(rr, cc))
#         for rr, cc in pixels:
#             mask[rr, cc] = Class
#
#
#     plt.figure()
#     plt.subplot(221), plt.imshow(classified), plt.xticks([]), plt.yticks([])
#     plt.subplot(222), plt.imshow(mask, vmin=0, vmax=4), plt.xticks([]), plt.yticks([])
#     mask[mask > 0] = 1
#     mask = np.dstack((mask, mask, mask))
#     plt.subplot(223), plt.imshow(np.multiply(img_as_float(pre), mask)), plt.xticks([]), plt.yticks([])
#     plt.subplot(224), plt.imshow(np.multiply(img_as_float(post), mask)), plt.xticks([]), plt.yticks([])
#     plt.subplots_adjust(left=0, right=0.01, top=0.01, bottom=0, wspace=0)
#     plt.tight_layout(pad=0, w_pad=0, h_pad=0)
#     plt.show()
#
#     y_pred = np.vstack((y_pred, np.atleast_2d(classified[classified > 0]).T))
#     y_true = np.vstack((y_true, np.atleast_2d(mask[mask > 0]).T))
#     # cm = confusion_matrix(y_true, y_pred)
#     # plt.figure()
#     # plt.imshow(cm, cmap='Blues')
#     # plt.xticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
#     # plt.yticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
#     # plt.show()
#
# cm = confusion_matrix(y_true, y_pred)
# print(cm)
# print(classification_report(y_true, y_pred))
# plt.figure()
# plt.imshow(cm[:-1, :-1], cmap='Blues')
# plt.ylabel('True label')
# plt.xlabel('Classified label')
# plt.xticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
# plt.yticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
# plt.show()

# ----------------------------------------------------------------------------------- check with post-processing applied
import numpy as np
import matplotlib.pyplot as plt
from skimage.draw import polygon
import pandas as pd
from sklearn.metrics import confusion_matrix, classification_report
from skimage.io import imread
from matplotlib.colors import ListedColormap
cmap = ListedColormap(["pink", "forestgreen", "gold", "darkorange", "red", "black"])

def create_mask(labels, wclassification=True):
    mask = np.zeros((1024, 1024))
    classes = ['no-damage', 'minor-damage', 'major-damage', 'destroyed', 'un-classified']
    numOfBuildings = 0
    for building in labels:
        vertices = building['wkt'].partition('POLYGON ((')[2].partition('))')[0].split(', ')
        rows = []
        cols = []
        for vertex in vertices:
            cols.append(float(vertex.split(' ')[0]))
            rows.append(float(vertex.split(' ')[1]))
        rr, cc = polygon(rows, cols, (1024, 1024))
        pixels = list(zip(rr, cc))
        Class = classes.index(building['properties']['subtype']) + 1
        for rr, cc in pixels:
            if wclassification:
                mask[rr, cc] = Class
            else:
                mask[rr, cc] = numOfBuildings
        numOfBuildings += 1
    return mask

disaster_type = 'wind'
labelsDB = pd.read_pickle('All_Data_Props.pkl')
labelsDB = labelsDB[labelsDB['Group'] != 'Test']
labelsDB_post = labelsDB[labelsDB['Pre_Post'] == 'post']
labelsDB_post_gt0 = labelsDB_post[labelsDB_post['buildings#'] > 0]
labelsDB_post_gt0_disaster = labelsDB_post_gt0[labelsDB_post_gt0['disaster_type'] == disaster_type].reset_index()
print(labelsDB_post_gt0_disaster.shape, ' size of database of post disaster images which have buildings.')

df = labelsDB_post_gt0_disaster[['destroyed#', 'minor-damage#', 'major-damage#', 'no-damage#']]
df = pd.concat((labelsDB_post_gt0_disaster, df.prod(axis=1)), axis=1).sort_values(by=0, ascending=False).drop(
    columns=0).head(20)  # 60
classes = ['no-damage', 'minor-damage', 'major-damage', 'destroyed', 'un-classified']

areaPercentage = {'no-damage': [], 'minor-damage': [],
                  'major-damage': [], 'destroyed': []}

y_true = []
y_pred = []
for i in range(len(df['img_name'])):
    name = (df['img_name'].iloc[i].split('/')[-1] + '.npy')
    # classified = np.load('./training_0/classified_images_1st_place/' + name)
    classified = np.load('./training_0/classified_images_LGBM_4class_featuresInceptionResNetV2_patch/' + name)
    Labels = pd.read_json(open(df['img_name'].iloc[i] + '.json', ))['features']['xy']

    preimg = imread(df['img_name'].iloc[i].replace('labels', 'images').replace('post', 'pre') + '.png')
    postimg = imread(df['img_name'].iloc[i].replace('labels', 'images') + '.png')

    build_loc = create_mask(Labels, wclassification=False)
    # build_loc[build_loc > 0] = 1
    # build_loc = np.dstack((build_loc, build_loc, build_loc))
    build_cls = create_mask(Labels, wclassification=True)

    # classified_ = np.zeros((1024, 1024))
    for idx in np.unique(build_loc)[1:]:
        #     building_pixels = sum(sum(build_loc == idx))
        #     damaged_pixels = sum(sum(np.multiply((build_loc == idx), classified) == 2))
        #     building_class = np.unique(np.multiply((build_loc == idx), build_cls))[1]
        #     if building_class != 5:
        #         areaPercentage[classes[int(building_class)-1]].append(damaged_pixels / building_pixels)
        #
        #     per = damaged_pixels / building_pixels
        #     if per < 0.3:
        #         classified_[build_loc == idx] = 1
        #     elif (per > 0.3) and (per < 0.48):
        #         classified_[build_loc == idx] = 2
        #     elif (per > 0.48) and (per < 0.66):
        #         classified_[build_loc == idx] = 3
        #     elif (per > 0.66) and (per < 1.00):
        #         classified_[build_loc == idx] = 4
        #
        # y_pred.extend(np.unique(classified[build_loc == idx]))  # Building-wise accuracy assessment
        y_pred.extend(np.array([np.median(classified[build_loc == idx])]))  # Building-wise accuracy assessment
        y_true.extend(np.unique(build_cls[build_loc == idx]))

    # y_pred.extend(classified_[np.multiply(build_cls != 0, build_cls != 5)])
    # y_pred.extend(classified[np.multiply(build_cls != 0, build_cls != 5)])  # Pixel-wise accuracy assessment
    # y_true.extend(build_cls[np.multiply(build_cls != 0, build_cls != 5)])

    # plt.figure()
    # plt.subplot(131), plt.imshow(classified, cmap='jet'), plt.xticks([]), plt.yticks([])
    # plt.subplot(132), plt.imshow(build_cls, cmap=cmap, vmax=5, vmin=0), plt.xticks([]), plt.yticks([])
    # plt.subplot(133), plt.imshow(classified_, cmap=cmap, vmax=5, vmin=0), plt.xticks([]), plt.yticks([])
    # plt.subplots_adjust(left=0, right=0.01, top=0.01, bottom=0, wspace=0)
    # plt.tight_layout(pad=0, w_pad=0, h_pad=0)
    # plt.show()

    # plt.figure()
    # plt.suptitle(df['img_name'].iloc[i])
    # plt.subplot(221), plt.imshow(classified, cmap=cmap, vmax=5, vmin=0), plt.xticks([]), plt.yticks([])
    # plt.subplot(222), plt.imshow(build_cls, cmap=cmap, vmax=5, vmin=0), plt.xticks([]), plt.yticks([])
    # plt.subplot(223), plt.imshow(np.multiply(preimg[:, :, 0], build_loc).astype(int)), plt.xticks([]), plt.yticks([])
    # plt.subplot(224), plt.imshow(np.multiply(postimg[:, :, 0], build_loc).astype(int)), plt.xticks([]), plt.yticks([])
    # plt.subplots_adjust(left=0, right=0.01, top=0.01, bottom=0, wspace=0)
    # plt.tight_layout(pad=0, w_pad=0, h_pad=0)
    # plt.show()

y_pred = np.array(y_pred).astype(int)
y_true = np.array(y_true).astype(int)

y_true = y_true[y_pred != 0]
y_pred = y_pred[y_pred != 0]

y_pred = y_pred[y_true != 5]
y_true = y_true[y_true != 5]

# y_pred[y_pred == 2] = 1
# y_true[y_true == 2] = 1
# y_pred[y_pred == 3] = 4
# y_true[y_true == 3] = 4

cm = confusion_matrix(y_true.astype(int), y_pred.astype(int))
print(cm)
print(classification_report(y_true.astype(int), y_pred.astype(int)))
plt.figure()
plt.imshow(cm, cmap='Blues')
plt.ylabel('True label')
plt.xlabel('Classified label')
plt.xticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
plt.yticks(ticks=[0, 1, 2, 3], labels=['ND', 'MiD', 'MaD', 'D'])
plt.show()

plt.figure()
plt.plot(np.random.uniform(low=0.9, high=1.1, size=(len(areaPercentage['no-damage']),)),
         areaPercentage['no-damage'], mfc='navy', marker='o', alpha=0.08)
plt.plot(np.random.uniform(low=1.9, high=2.1, size=(len(areaPercentage['minor-damage']),)),
         areaPercentage['minor-damage'], mfc='darkcyan', marker='*', alpha=0.08)
plt.plot(np.random.uniform(low=2.9, high=3.1, size=(len(areaPercentage['major-damage']),)),
         areaPercentage['major-damage'], mfc='deeppink', marker='^', alpha=0.08)
plt.plot(np.random.uniform(low=3.9, high=4.1, size=(len(areaPercentage['destroyed']),)),
         areaPercentage['destroyed'], mfc='orangered', marker='p', alpha=0.08)

plt.boxplot([areaPercentage['no-damage'], areaPercentage['minor-damage'],
             areaPercentage['major-damage'], areaPercentage['destroyed']],
            labels=['No-Damage', 'Minor-Damage', 'Major-Damage', 'Destroyed'],
            showfliers=True, notch=True, widths=0.2, showmeans=True)

plt.hlines(0.00, 0.9, 1.1, colors='gold')
plt.hlines(0.30, 0.9, 1.1, colors='gold')
plt.hlines(0.30, 1.9, 2.1, colors='gold')
plt.hlines(0.48, 1.9, 2.1, colors='gold')
plt.hlines(0.48, 2.9, 3.1, colors='gold')
plt.hlines(0.66, 2.9, 3.1, colors='gold')
plt.hlines(0.66, 3.9, 4.1, colors='gold')
plt.hlines(1.00, 3.9, 4.1, colors='gold')

plt.ylabel('Percent of damaged pixels in each building.')
plt.title('Percentage distribution of damaged pixels per building,\npredicted by classifier per damage class.')
plt.grid(True, linestyle='--', color='grey', alpha=.25)
plt.tight_layout()
plt.show()


import pygame
from player import Player
from bullet import Bullet
from enemy import Enemy
from squadron import Squadron

# Game settings
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
GAME_SIDE_MARGIN = 10
GAME_TOP_MARGIN = 40
GAME_BOTTOM_MARGIN = GAME_TOP_MARGIN
GAME_BORDER_WIDTH = 3

GAME_WALL_TOP = GAME_TOP_MARGIN + GAME_BORDER_WIDTH
GAME_WALL_RIGHT = WINDOW_WIDTH - GAME_SIDE_MARGIN - GAME_BORDER_WIDTH
GAME_WALL_BOTTOM = WINDOW_HEIGHT - GAME_BOTTOM_MARGIN - GAME_BORDER_WIDTH
GAME_WALL_LEFT = GAME_SIDE_MARGIN + GAME_BORDER_WIDTH

# Colors
COLOR_BLACK = (0,0,0)
COLOR_WHITE = (255,255,255)
COLOR_BLUE = (0,0,255)

# Setup pygame elements
pygame.init()
game_display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('SPACE INVADERS')
title_font = pygame.font.SysFont('Arial', 40, True)
score_font = pygame.font.SysFont('Arial', 28, True)
title_text = title_font.render('SPACE INVADERS', False, COLOR_BLUE)
clock = pygame.time.Clock()

# Load Media
player_img = pygame.image.load('media/si-player.gif')
bullet_img = pygame.image.load('media/si-bullet.gif')
enemy_img = pygame.image.load('media/si-enemy.gif')
background_img = pygame.image.load('media/si-background.gif')
laser_sound = pygame.mixer.Sound('media/si-laser.wav')
explosion_sound = pygame.mixer.Sound('media/si-explode.wav')
pygame.mixer.music.load('media/Spirit In The Sky.mp3')
pygame.mixer.music.play(-1)

left_arrow_is_down = False
right_arrow_is_down = False
last_key_pressed = "NEITHER"
def handle_events():
    global left_arrow_is_down, right_arrow_is_down, last_key_pressed
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            player.is_alive = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                left_arrow_is_down = True
                last_key_pressed = "LEFT"
            elif event.key == pygame.K_RIGHT:
                right_arrow_is_down = True
                last_key_pressed = "RIGHT"
            elif event.key == pygame.K_SPACE:
                player.shoot(bullet_img, laser_sound)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                left_arrow_is_down = False
                if right_arrow_is_down:
                    last_key_pressed = "RIGHT"
                else:
                    last_key_pressed = "NEITHER"
            elif event.key == pygame.K_RIGHT:
                right_arrow_is_down = False
                if left_arrow_is_down:
                    last_key_pressed = "LEFT"
                else:
                    last_key_pressed = "NEITHER"

        if last_key_pressed == "NEITHER":
            player.stop_moving()
        elif last_key_pressed == "LEFT":
            player.move_left()
        elif last_key_pressed == "RIGHT":
            player.move_right()

def show_game_scene(score):
    game_display.blit(game_display, (0,0))
    game_display.fill(COLOR_BLACK)

    pygame.draw.rect(game_display, COLOR_WHITE, \
        (GAME_SIDE_MARGIN, GAME_TOP_MARGIN, \
            WINDOW_WIDTH - GAME_SIDE_MARGIN * 2, \
            WINDOW_HEIGHT -GAME_TOP_MARGIN - GAME_BOTTOM_MARGIN))
    game_display.blit(background_img, \
        (GAME_WALL_LEFT, GAME_WALL_TOP), \
        (0, 0, GAME_WALL_RIGHT - GAME_WALL_LEFT, GAME_WALL_BOTTOM - GAME_WALL_TOP))
    game_display.blit(title_text, (WINDOW_WIDTH / 2 - title_text.get_width() / 2, 2))
    score_text = score_font.render("SCORE: " + str(player.score), False, COLOR_BLUE)
    game_display.blit(score_text, (GAME_SIDE_MARGIN, WINDOW_HEIGHT - score_text.get_height()))

player = Player(GAME_WALL_LEFT, GAME_WALL_RIGHT, GAME_WALL_BOTTOM, player_img)
levels = []
levels.append(Squadron(GAME_WALL_LEFT, GAME_WALL_TOP, 3, 5, 2, enemy_img))
levels.append(Squadron(GAME_WALL_LEFT, GAME_WALL_TOP, 3, 5, 4, enemy_img))
levels.append(Squadron(GAME_WALL_LEFT, GAME_WALL_TOP, 5, 6, 2, enemy_img))
levels.append(Squadron(GAME_WALL_LEFT, GAME_WALL_TOP, 5, 4, 4, enemy_img))
levels.append(Squadron(GAME_WALL_LEFT, GAME_WALL_TOP, 8, 8, 4, enemy_img))
level_number = 0

# Main game loop
while player.is_alive:

    squadron = levels[level_number]
    if len(squadron.enemies) == 0:
        level_number += 1

    handle_events()

    squadron.move(GAME_WALL_LEFT, GAME_WALL_RIGHT)
    player.remove_missed_bullets(GAME_WALL_TOP)
    player.kill_enemies_colliding_with_bullets(squadron, game_display, explosion_sound)
    player.kill_player_if_invaded(squadron, GAME_WALL_BOTTOM)

    show_game_scene(player.score)
    player.show(GAME_WALL_LEFT, GAME_WALL_RIGHT, game_display)
    squadron.show(game_display)
    Bullet.show_all_bullets(game_display ,player.bullets_fired)

    pygame.display.update()
    clock.tick(60)

pygame.quit()


import pyttsx3
import speech_recognition as sr
import datetime
import os
import webbrowser
import wikipedia
import random
import smtplib

print(" INITIALIZING JARVIS ")
CREATOR= "JOHN"                                      # YOUR NAME CAN BE ENTERED HERE

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('youremail@gmail.com', 'your-password')
    server.sendmail('youremail@gmail.com', to, content)
    server.close()

def speak(text):
    engine.say(text)
    engine.runAndWait()

def wish () :
    hour = datetime.datetime.now().hour

    if hour>=0 and hour<=12 :
        speak("GOOD MORNING" + CREATOR)

    elif hour>= 12 and hour< 18 :
        speak("GOOD AFTERNOON " + CREATOR)

    else :
        speak("GOOD EVENING " + CREATOR)

    speak("HI I AM JARVIS , HOW MAY I HELP YOU")

# STARTING OF MAIN PROGRAM
said= ""

def inputcommand():
    r= sr.Recognizer()
    with sr.Microphone() as source :
        print("LISTENING....")
        audio = r.listen(source)

    try :
        print("recognizing ....")
        said = r.recognize_google(audio, language = "en-in")
        print(f"you said .. : {said}\n",)

    except Exception as e :
        print(e)
        print("CAN YOU PLEASE SAY THAT AGAIN ...")

    return said

wish()
said = inputcommand()

if "open youtube" in said.lower() :
    url = "youtube.com"

    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    webbrowser.get(chrome_path).open(url)

elif "open google" in said.lower() :
    url = "google.com"

    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'       # FOR OPENING IN GOOGLE CHROME
    webbrowser.get(chrome_path).open(url)

elif 'wikipedia' in said.lower():
    speak('Searching Wikipedia...')
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak("According to Wikipedia")
    print(results)
    speak(results)

elif 'email to friend' in query:
    try:
        speak("What should I say?")
        content = inputcommand()
        to = "friendyourEmail@gmail.com"
        sendEmail(to, content)
        speak("Email has been sent !")
    except Exception as e:
        print(e)
        speak("Sorry my friend harry bhai. I am not able to send this email")

elif "play music" in said.lower() :
    songs_dir = "C:\\Users\\avime\\Desktop\\SONGS"
    songs = os.listdir(songs_dir)
    print(songs)                                                      # INSERT YOUR OWN PATH DIRECTORY
    os.startfile(os.path.join(songs_dir,songs[0]))

elif "the time"  in said.lower() :
    strtime= datetime.datetime.now().strftime("%H:%M:%S")
    speak(f"{CREATOR} the time is {strtime}")

elif  "timer" in said.lower() or "set a timer" in said.lower():
    speak("Starting a timer of 10 seconds ")
    for i in range(10,0,-1) :
        speak(str(i))

    speak("YOUR TIME IS UP")

elif "open coursera" in said.lower() :
    url = "https://www.coursera.org/"

    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    webbrowser.get(chrome_path).open(url)

elif " open movie"  in said.lower() :
    movie_dir ="D:\\MOVIES"                 # enter your own directory path
    movie = os.listdir(movie_dir)
    print(movie)
    os.startfile(os.path.join(movie_dir,movie[0]))

elif "a joke" in said.lower():
    jokes=[" I can't believe I got fired from the calendar factory. All I did was take a day off ", "How did I escape Iraq? Iran.", "I'm reading a book about anti-gravity. It's impossible to put down.","Atheism is a non-prophet organization"," I wasn't originally going to get a brain transplant, but then I changed my mind.","The future, the present and the past walked into a bar. Things got a little tense. "]
    speak("Allrite, hear this ")
    jke= random.sample(jokes,1)
    print(jke)
    speak(jke)

elif " can you perform calculations" in said.lower()  or "calculations" in said.lower() :
    speak("sure")
    speak(" what would you like me to do")
    per = inputcommand()
    if "add" in per.lower() or "perform addition" in per.lower() :
        speak("ok , what are the numbers ")
        op= inputcommand()
        res = [int(i) for i in op.split() if i.isdigit()]
        print(sum(res))
        ans= sum(res)
        speak(ans)

    elif "subtract" in per.lower() or "perform subtraction" in per.lower() :
        speak("ok , what are the numbers ")
        op= inputcommand()
        res = [int(i) for i in op.split() if i.isdigit()]
        print(res[0]-res[1])
        ans= res[0]-res[1]
        speak(ans)

    elif "multiply " in per.lower()  or " perform multiplication "  in per.lower() :
        speak("ok , what are the numbers ")
        op = inputcommand()
        res= [ int(i)  for i in op.split() if i]
        print(res[0] * res[1])
        ans = res[0]* res[1]
        speak(ans)

    elif "divide" in per.lower()  or " perform division" in per.lower() :
        speak("ok , what are the numbers ")
        op = inputcommand()
        res= [ int(i)  for i in op.split() if i]
        print(res[0] / res[1])
        ans = res[0] / res[1]
        speak(ans)

    elif "power" in per.lower() or " perform power " :
        speak("ok , what are the numbers ")
        op = inputcommand()
        res= [ int(i)  for i in op.split() if i]
        print(res[0] ** res[1])
        ans = res[0] ** res[1]
        speak(ans)

else :
    speak("sorry the command is not yet installed , or not found ")


import operator
import re

from ..lib.misc import file_scan, hash_files, random_string
from functools import reduce
from itertools import chain

from pyramid.view import view_config
from pyramid.renderers import render_to_response

import logging
log = logging.getLogger(__name__)

random_revision = random_string()

@view_config(route_name='cache_manifest')
def cache_manifest(request):
    log.debug('cache.manifest')
    request.response.cache_expires(request.registry.settings.get('template.offline.cache_expires'))
    if request.registry.settings['template.offline.enabled']:
        mounts = request.registry.settings['mounts']
        files = sorted(chain(request.root.paths,*map(operator.attrgetter('mounted'), mounts)))
        files.remove('/offline')
        cache = dict(
            revision = "".join(map(operator.attrgetter('hash'), mounts)),
            cache = files,
        )
    else:
        cache = dict(
            revision = random_revision,
            network = '*',
        )
    return render_to_response('_cache.manifest.mako', dict(cache=cache), request=request)


import os,psutil,time,datetime as dttime
from optparse import OptionParser
from paircarstools.access_ms import *
from paircarstools.basic_func import *
from paircarstools.flagger import *
from paircarstasks.split_ms import *
from casatasks import *
from casatools import msmetadata,table,measures,quanta,agentflagger,image,calibrater,ms
import multiprocessing as mp
from multiprocessing import current_process
os.system('rm -rf casa*log')

'''
Code is written by Devojyoti Kansabanik , 20 May, 2021
'''

def split_ms_parallel(paramList,cpu_frac):
	'''
	Function to run multiple split jobs in parallel from same or different measurement sets
	Parameters
	----------
	paramList : list
		Lists of list of parameters for each split, format [[vis1,datacolumn1,spw1,timerange1,chanwidth1,timewidth1],[vis2,datacolumn2,spw2,timerange2,chanwidth2,timewidth2],....]
	Returns
	-------
	list
		List of splited ms
	'''
	print ('Waiting for free cpus....\n')
	try:
		while True:
			available_cpu=int(psutil.cpu_count()*(1-(psutil.cpu_percent()/100.0))*float(cpu_frac))
			if available_cpu>0:
				print ('Number of cpus available for spliting : '+str(available_cpu)+'\n')
				break
		start_time=time.time()
		savedir=paramList[0][6]
		total_count=0
		if os.path.exists(savedir+'/temp'):
			os.system('rm -rf '+savedir+'/temp')
		os.makedirs(savedir+'/temp')
		for i in range(len(paramList)):
			t_param=paramList[i]
			t_param[-3]=savedir+'/temp'
			paramList[i]=t_param
		paramList_copy=copy.deepcopy(paramList)
		output_ms_list=[]
		verbose=paramList_copy[0][-2]
		chunksize=int(len(paramList_copy)/available_cpu)
		if chunksize<1:
			chunksize=1
		pool = mp.Pool(available_cpu)
		ms_list = pool.map(func=split_single_ms, iterable=paramList_copy, chunksize=chunksize)
		pool.close()
		pool.join()
		for ms in ms_list:
			if os.path.exists(ms):
				output_ms_list.append(ms)
			else:
				if verbose:
					print ('Msname '+ms+' does not exist.\n')
		for i in output_ms_list:
			if os.path.exists(savedir+'/'+os.path.basename(i)):
				os.system('rm -rf '+savedir+'/'+os.path.basename(i)+'*')
			os.system('mv '+i+' '+savedir+'/')
		os.system('rm -rf '+savedir+'/temp')
		os.system('rm -rf casa*log')
		final_ms_list=glob.glob(savedir+'/*.ms')
		return 0
	except Exception as e:
		print ('Error occured during spliting : '+str(e)+'\n')
		os.system('rm -rf '+savedir+'/temp')
		os.system('rm -rf casa*log')
		return 1

def main():
	start_script_time=time.time()
	cwd=os.getcwd()
	usage= ' Split measurement set in parallel\n'
	parser = OptionParser(usage=usage)
	parser.add_option('--msname',dest="msname",default=None,help="Name of measurement set to split from",metavar="Measurement Set")
	parser.add_option('--savedir',dest='savedir',default=None,help='Name of directory to save splited measurement sets',metavar="Directory path")
	parser.add_option('--freq_interval',dest='skip_freq',default='MS frequency resolution',help='Spliting frequency interval in kHz',metavar="Float")
	parser.add_option('--time_interval',dest='skip_time',default='MS time resolution',help='Spliting temporal interval in second',metavar="Float")
	parser.add_option('--freq_list',dest='freq_list',default=None,help='Spliting frequency list in MHz (If given frequency interval will not be used)',metavar="Comma separated string")
	parser.add_option('--time_list',dest='time_list',default=None,help='Spliting timestamp list in \'yyyy/mm/dd/hh:mm:ss\' format (If given time interval will not be used)',\
						metavar="Comma separated string")
	parser.add_option('--timerange',dest='timerange',default=None,help='Spliting timerange (Data set in this timerange will be splited at the given time interval)',\
						metavar="Comma separated string")
	parser.add_option('--force',dest='force',default=False,help='Force to determine the frequency time chunks fresh even if the previous metadata exists',\
						metavar="Comma separated string")
	parser.add_option('--freq_width',dest='freq_width',default='MS frequency resolution',help='Bandwidth of each splited ms in kHz start from frequency stamp',metavar="Float")
	parser.add_option('--time_width',dest='time_width',default='MS time resolution',help='Temporal width of each splited ms in second start from time stamp',metavar="Float")
	parser.add_option('--freq_avg',dest='freq_avg',default='MS frequency resolution',help='Frequency averaging in kHz',metavar="Float")
	parser.add_option('--time_avg',dest='time_avg',default='MS time resolution',help='Temporal averaging in second',metavar="Float")
	parser.add_option('--datacolumn',dest='datacolumn',default='data',help='Datacolumn to split',metavar="String")
	parser.add_option('--cpu_frac',dest='cpu_frac',default=0.5,help='Fraction of cpu to use',metavar="Float")
	parser.add_option('--total_block',dest='total_block',default=1,help='Number of total spliting blocks',metavar="Integer")
	parser.add_option('--cur_block',dest='cur_block',default=1,help='Current spliting block',metavar="Integer")
	parser.add_option('--verbose',dest='verbose',default=False,help='Verbose output',metavar="Boolean")
	parser.add_option('--splitms',dest='splitms',default=True,help='Split ms or not',metavar="Boolean")
	parser.add_option('--max_flag_frac',dest='max_flag_frac',default=0.95,help='Maximum allowed flag fraction',metavar="Float")
	(options, args) = parser.parse_args()

	splitms=eval(str(options.splitms))
	count=0
	while True:
		available_cpu=int(psutil.cpu_count()*(1-(psutil.cpu_percent()/100.0))*float(options.cpu_frac))
		if available_cpu>0:
			break
		if count==0:
			print ('Waiting for free hardware resources....\n')
		count+=1

	if str(options.msname)[-1]=='/':
		msname=str(options.msname)[:-1]
	else:
		msname=str(options.msname)
	if os.path.isdir(str(options.msname))==False or options.msname==None:
		print ('Measurement set is not present.\n')
		os.system('touch '+cwd+'/.Finished_spliting_'+os.path.basename(str(msname))+'_noms')
		os._exit(1)
	try:
		if options.savedir==None:
			savedir=os.path.dirname(os.path.abspath(str(options.msname)))+'/splited_ms/'+os.path.basename(options.msname).split('.ms')[0]
		elif os.path.isdir(str(options.savedir)+'/splited_ms/'+os.path.basename(options.msname).split('.ms')[0])==False:
			os.makedirs(str(options.savedir)+'/splited_ms/'+os.path.basename(options.msname).split('.ms')[0])
			savedir=str(options.savedir)+'/splited_ms/'+os.path.basename(options.msname).split('.ms')[0]
		else:
			savedir=str(options.savedir)+'/splited_ms/'+os.path.basename(options.msname).split('.ms')[0]
		if os.path.isdir(savedir)==False:
			os.makedirs(savedir)
		while True:
			available_cpu=int(psutil.cpu_count()*(1-(psutil.cpu_percent()/100.0))*float(options.cpu_frac))
			if available_cpu>0:
				break
		verbose=eval(str(options.verbose))
		print ('Total '+str(available_cpu)+' cpus are available for use.\n')
		param_save_file=str(savedir)+'/'+str(os.path.basename(options.msname)).split('.ms')[0]+'_split_params'
		if eval(str(options.force))==True or os.path.exists(param_save_file+'.npy')==False:
			AM=AccessMS(options.msname)
			if options.timerange==None and options.time_list==None:
				total_time=AM.calc_total_time()
				final_timerange=''
				if total_time>10:
					mjdtimestamps=AM.get_timestamps_in_mjdsecs()[0]
					mid_time=mjdtimestamps[int(len(mjdtimestamps)/2)]
					s_time=mid_time-5
					e_time=mid_time+5
					temp_timerange=mjdsec_to_timestamp(s_time,includedate=True,format=0) +'~'+mjdsec_to_timestamp(e_time,includedate=True,format=0)
					timebin='10s'
				else:
					temp_timerange=''
					timebin=str(total_time)+'s'
			elif options.time_list!=None:
				user_time_list=str(options.time_list).split(',')
				temp_time_list=[]
				for t in user_time_list:
					try:
						s_time=timestamp_to_mjdsec(t.split('~')[0])
					except:
						s_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[0])
					try:
						e_time=timestamp_to_mjdsec(t.split('~')[-1])
					except:
						e_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[-1])
				temp_time_list.append(s_time)
				temp_time_list.append(e_time)
				temp_timerange=mjdsec_to_timestamp(min(temp_time_list),includedate=True,format=0)+'~'+mjdsec_to_timestamp(max(temp_time_list),includedate=True,format=0)
				fnal_timeange=copy.deepcopy(temp_timerange)
				timebin=str(max(temp_time_list)-min(temp_time_list))+'s'
			else:
				temp_timerange=str(options.timerange)
				final_timerange=str(options.timerange)
				start_time=temp_timerange.split('~')[0]
				end_time=temp_timerange.split('~')[-1]
				start_mjd=timestamp_to_mjdsec(start_time,format=0)
				end_mjd=timestamp_to_mjdsec(end_time,format=0)
				if end_time>start_time:
					timebin=str(end_mjd-start_mjd)+'s'
				else:
					timebin=''
			AM=AccessMS(str(options.msname))
			freqres=AM.calc_freqres()
			timeres=round(AM.calc_timeres(),2)
			AM=AccessMS(str(options.msname))
			unflag_freqs=AM.get_freqs()/10**6
			unflag_times_mjd=AM.get_timestamps_in_mjdsecs()[0]

			param_list=[]
			if str(options.freq_avg)=='MS frequency resolution' and str(options.time_avg)=='MS time resolution':
				chanwidth=1
				timebin=str(timeres)+'s'
			elif str(options.freq_avg)=='MS frequency resolution' and str(options.time_avg)!='MS time resolution':
				chanwidth=1
				timebin=str(options.time_avg)+'s'
			elif str(options.freq_avg)!='MS frequency resolution' and str(options.time_avg)=='MS time resolution':
				chanwidth=int(float(options.freq_avg)/freqres)
				timebin=str(timeres)+'s'
			elif str(options.freq_avg)!='MS frequency resolution' and str(options.time_avg)!='MS time resolution':
				chanwidth=int(float(options.freq_avg)/freqres)
				timebin=str(options.time_avg)+'s'
			if str(options.freq_width)=='MS frequency resolution' and str(options.time_width)=='MS time resolution':
				if str(options.skip_freq)!='MS frequency resolution':
					freq_width=float(options.skip_freq)
					skip_freq=float(options.skip_freq)/10**3
				else:
					freq_width=float(freqres/10**3)
					skip_freq=freq_width*10**3
				if str(options.skip_time)!='MS time resolution':
					time_width=float(options.skip_time)
					skip_time=float(options.skip_time)
				else:
					time_width=float(timeres)
					skip_time=float(timeres)
			elif str(options.freq_width)=='MS frequency resolution' and str(options.time_width)!='MS time resolution':
				if str(options.skip_freq)!='MS frequency resolution':
					freq_width=float(options.skip_freq)/10**3
					skip_freq=float(options.skip_freq)*10**3
				else:
					freq_width=float(freqres/10**3)
					skip_freq=freq_width*10**3
				time_width=float(options.time_width)
			elif str(options.freq_width)!='MS frequency resolution' and str(options.time_width)=='MS time resolution':
				freq_width=float(options.freq_width)/10**3
				skip_freq=freq_width*10**3
				if str(options.skip_time)!='MS time resolution':
					time_width=skip_time=float(options.skip_time)
				else:
					time_width=float(timeres)
					skip_time=time_width
			elif str(options.freq_width)!='MS frequency resolution' and str(options.time_width)!='MS time resolution':
				freq_width=float(options.freq_width)/10**3
				time_width=float(options.time_width)
				skip_freq=freq_width*10**3
				skip_time=time_width
			start_date,end_date=AM.get_obs_date()

			if options.freq_list!=None and options.time_list!=None:
				user_freq_list=str(options.freq_list).split(',')
				user_time_list=str(options.time_list).split(',')
				time_list=[]
				freq_list=[]
				for t in user_time_list:
					try:
						s_time=timestamp_to_mjdsec(t.split('~')[0])
					except:
						s_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[0])
					try:
						e_time=timestamp_to_mjdsec(t.split('~')[-1])
					except:
						e_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[-1])
					if s_time<min(unflag_times_mjd):
						s_time=min(unflag_times_mjd)
					if e_time>max(unflag_times_mjd):
						e_time=max(unflag_times_mjd)
					tm_str=mjdsec_to_timestamp(s_time,includedate=True,format=0)+'~'+mjdsec_to_timestamp(e_time,includedate=True,format=0)
					if tm_str not in time_list:
						time_list.append(tm_str)
				for f in user_freq_list:
					s_freq=float(f.split('~')[0])
					e_freq=float(f.split('~')[-1])
					s_e_freq=np.arange(s_freq,e_freq,float(skip_freq)).astype('float')
					for freq in s_e_freq:
						if freq in unflag_freqs:
							freq_list.append(freq)
			elif options.freq_list!=None and options.time_list==None:
				skip_time=int(float(skip_time)/timeres)
				user_freq_list=str(options.freq_list).split(',')
				freq_list=[]
				for f in user_freq_list:
					s_freq=float(f.split('~')[0])
					e_freq=float(f.split('~')[-1])
					s_e_freq=np.arange(s_freq,e_freq,float(skip_freq)).astype('float')
					for freq in s_e_freq:
						if freq in unflag_freqs:
							freq_list.append(freq)
				time_list=[]
				for i in range(0,len(unflag_times_mjd),skip_time):
					time_list.append(unflag_times_mjd[i])
				time_list=[mjdsec_to_timestamp(i,includedate=True,format=0) for i in time_list]
			elif options.freq_list==None and options.time_list!=None:
				skip_chan=int(float(skip_freq)/freqres)
				freq_list=[]
				for i in range(0,len(unflag_freqs),skip_chan):
					freq_list.append(unflag_freqs[i])
				user_time_list=str(options.time_list).split(',')
				time_list=[]
				for t in user_time_list:
					try:
						s_time=timestamp_to_mjdsec(t.split('~')[0])
					except:
						s_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[0])
					try:
						e_time=timestamp_to_mjdsec(t.split('~')[-1])
					except:
						e_time=timestamp_to_mjdsec(start_date+'/'+t.split('~')[-1])
					if s_time<min(unflag_times_mjd):
						s_time=min(unflag_times_mjd)
					if e_time>max(unflag_times_mjd):
						e_time=max(unflag_times_mjd)
					tm_str=mjdsec_to_timestamp(s_time,includedate=True,format=0)+'~'+mjdsec_to_timestamp(e_time,includedate=True,format=0)
					if tm_str not in time_list:
						time_list.append(tm_str)
			elif options.freq_list==None and options.time_list==None:
				skip_chan=int(float(skip_freq)/freqres)
				skip_time=int(float(skip_time)/timeres)
				freq_list=[]
				for i in range(0,len(unflag_freqs),skip_chan):
					freq_list.append(unflag_freqs[i])
				time_list=[]
				for i in range(0,len(unflag_times_mjd),skip_time):
					time_list.append(unflag_times_mjd[i])
				time_list=[mjdsec_to_timestamp(i,includedate=True,format=0) for i in time_list]
			split_spws=['0:'+str(i)+'~'+str(float(i)+float(freq_width)-(float(freqres)/10**3))+'MHz' for i in freq_list]
			if time_width>=2*timeres:
				y_string='+'+str(dttime.timedelta(seconds=time_width))
				split_timestamps=[j+y_string for j in time_list]
			else:
				split_timestamps=time_list
			split_spws=sorted(split_spws)
			split_timestamps=sorted(split_timestamps)

			for i in split_spws:
				for j in split_timestamps:
					param_list.append([str(options.msname),str(options.datacolumn),i,j,chanwidth,timebin,savedir,verbose,\
						float(options.max_flag_frac)])
			if verbose:
				print ('Saving parameter file : '+str(param_save_file+'.npy')+'\n')
			np.save(param_save_file,param_list)
		if splitms==True:
			if verbose:
				print ('Loading spliting parameters from : '+param_save_file+'.npy\n')
			param_list=np.load(param_save_file+'.npy',allow_pickle=True)
			param_list=param_list.tolist()
			total_blocks=int(options.total_block)
			if total_blocks<1:
				total_blocks=1
			cur_block=int(options.cur_block)
			print ('Removing locks.....\n')
			AM=AccessMS(options.msname)
			AM.remove_locks()
			if total_blocks!=1:
				if int((cur_block+1)*len(param_list)/total_blocks)<len(param_list):
					param_list=param_list[int(cur_block*len(param_list)/total_blocks):int((cur_block+1)*len(param_list)/total_blocks)]
					print ('#########################################\n')
					print ('Spliting ms : '+str(options.msname)+' of block : '+str(cur_block)+' into total '+str(len(param_list))+' time frequency chuncks ..............\n')
					msg=split_ms_parallel(param_list,float(options.cpu_frac))
				elif int(cur_block*len(param_list)/total_blocks)<len(param_list):
					param_list=param_list[int(cur_block*len(param_list)/total_blocks):]
					print ('#########################################\n')
					print ('Spliting ms : '+str(options.msname)+' of block : '+str(cur_block)+' into total '+str(len(param_list))+' time frequency chuncks ..............\n')
					msg=split_ms_parallel(param_list,float(options.cpu_frac))
				else:
					print ('#########################################\n')
					print ('Nothing to split.\n')
					msg=1
			else:
				print ('#########################################\n')
				print ('Spliting ms : '+str(options.msname)+' into total '+str(len(param_list))+' time frequency chuncks ..............\n')
				msg=split_ms_parallel(param_list,float(options.cpu_frac))
			print ('Total spliting time : '+str(time.time()-start_script_time)+'s\n')
			os.system('rm -rf casa*log')
			msdir=os.path.basename(options.msname)
			temp_ms_list=glob.glob(msdir+'/*temp.ms')
			if len(temp_ms_list)>0:
				for i in temp_ms_list:
					os.system('rm -rf '+i)
			temp_ms_list=glob.glob(savedir+'/*temp.ms')
			if len(temp_ms_list)>0:
				for i in temp_ms_list:
					os.system('rm -rf '+i)
			if os.path.exists(options.msname.split('.ms')[0]+'_temp.ms'):
				os.system('rm -rf '+options.msname.split('.ms')[0]+'_temp.ms')
			print ('#########################################\n')
			return msg
		else:
			os.system('rm -rf casa*log')
			msdir=os.path.basename(options.msname)
			temp_ms_list=glob.glob(msdir+'/*temp.ms')
			if len(temp_ms_list)>0:
				for i in temp_ms_list:
					os.system('rm -rf '+i)
			temp_ms_list=glob.glob(savedir+'/*temp.ms')
			if len(temp_ms_list)>0:
				for i in temp_ms_list:
					os.system('rm -rf '+i)
			if os.path.exists(options.msname.split('.ms')[0]+'_temp.ms'):
				os.system('rm -rf '+options.msname.split('.ms')[0]+'_temp.ms')
			print ('#########################################\n')
			return 0
	except Exception as e:
		print ('Error occured : '+str(e)+'\n')
		msdir=os.path.basename(options.msname)
		temp_ms_list=glob.glob(msdir+'/*temp.ms')
		if len(temp_ms_list)>0:
			for i in temp_ms_list:
				os.system('rm -rf '+i)
		temp_ms_list=glob.glob(savedir+'/*temp.ms')
		if len(temp_ms_list)>0:
			for i in temp_ms_list:
				os.system('rm -rf '+i)
		os.system('rm -rf casa*log')
		if os.path.exists(options.msname.split('.ms')[0]+'_temp.ms'):
			os.system('rm -rf '+options.msname.split('.ms')[0]+'_temp.ms')
		return 1

if __name__ == "__main__":
	result=main()
	os._exit(result)


from pymongo import MongoClient
import config

client = MongoClient()
db = client[config.db_name]
posts = db.posts

def get_coord(t_id):
    loc_log = {}
    loc_lat = {}
    for id in t_id:
        count2 = 0
        for post in posts.find({'id':id}):
            log = 0.0
            lat = 0.0
            count = 0.0
            # pprint.pprint(post)
            # print(post['id'])
            if post['place'] and (post['id'] not in loc_log.keys()):
                for i in post['place']['bounding_box']['coordinates'][0]:
                    log += i[0]
                    lat += i[1]
                    count += 1
                log = log / count
                lat = lat / count
                loc_log[post['id']] = log
                loc_lat[post['id']] = lat
    return loc_log, loc_lat

def get_time(t_id):
    time = []
    for i in t_id:
        count2 = 0
        for post in posts.find({'id': i}):
            time.append(post['created_at'])

    return time


import os
import glob
import argparse

from PIL import Image
from skimage import io
import numpy as np
from tqdm import tqdm

WORK_DIR = './workspace/U-2-Net'

def parse_args():
    parser = argparse.ArgumentParser(
        description='Postprocess for U-2-Net'
    )
    parser.add_argument('--image_dir', type=str, default='./datasets/ECSSD/images',
                        help='input dataset image dir')
    parser.add_argument('--save_dir', type=str, default='./test_vis_ECSSD')
    parser.add_argument('--out_dir', type=str, default='./result/dumpOutput_device0')
    global_args = parser.parse_args()
    return global_args

def save_output(image_name, predict_np, d_dir):
    im = Image.fromarray(predict_np * 255).convert('RGB')
    img_name = image_name.split(os.sep)[-1]
    image = io.imread(image_name)
    imo = im.resize((image.shape[1], image.shape[0]), resample=Image.BILINEAR)

    aaa = img_name.split(".")
    bbb = aaa[0:-1]
    imidx = bbb[0]
    for i in range(1, len(bbb)):
        imidx = imidx + "." + bbb[i]

    save_output_result = os.path.join(d_dir, imidx + '.png')
    imo.save(save_output_result)

def normPRED(d):
    ma = np.max(d)
    mi = np.min(d)
    dn = (d - mi) / (ma - mi)
    return dn

def postprocess(ori_image_list, bin_dir, num, save_dir):
    for idx in tqdm(range(num)):
        bin_path = os.path.join(bin_dir, '{}_0.bin'.format(idx))
        bin_data = np.fromfile(bin_path, dtype=np.float32).reshape([320, 320])
        bin_data = normPRED(bin_data)
        image_path = ori_image_list[idx]
        save_output(image_path, bin_data, save_dir)

if __name__ == '__main__':
    args = parse_args()
    out_dir = args.out_dir
    save_dir_path = args.save_dir
    image_dir = args.image_dir
    bin_files = os.listdir(out_dir)
    os.makedirs(save_dir_path, exist_ok=True)
    img_name_list = glob.glob(image_dir + os.sep + '*')
    img_name_list.sort()
    postprocess(img_name_list, out_dir, len(bin_files), save_dir_path)


import glob

import numpy as np
import cv2
import yaml

boardsize = [6, 7]
numEdgeX = 7
numEdgeY = 6
img_shape = (640,480)
criteria = (cv2.TermCriteria_EPS +
                    cv2.TermCriteria_MAX_ITER, 30, 0.001)
criteria_cal = (cv2.TermCriteria_EPS +
                    cv2.TermCriteria_MAX_ITER, 30, 1e-5)
objp = np.zeros((numEdgeX*numEdgeY, 3), np.float32)
objp[:, :2] = np.mgrid[0:numEdgeX, 0:numEdgeY].T.reshape(-1, 2)

objpoints = []     # 3d points in real world space
imgpoints_l = []   # 2d points in image plane for calibration
imgpoints_r = []
images_right = []
images_left = []

def calibrator(add,imgpoints,b):

    images = glob.glob(add)
    objpoint = []
    for fname in images:
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #
        ret, corners = cv2.findChessboardCorners(gray, (boardsize[1], boardsize[0]), None)
        if ret == True:
            if(b):
                images_left.append(img)
            else:
                images_right.append(img)
            objpoint.append(objp)
            corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
            imgpoints.append(corners2)
            img = cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
            cv2.imshow('img', img)
            cv2.waitKey(500)

    print(np.shape(objpoint))
    print(np.shape( imgpoints))
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoint, imgpoints, gray.shape[::-1], None, None)
    for i in objpoint:
        objpoints.append(i)
    return ret, mtx, dist, rvecs, tvecs,objpoint

rt, M1, d1, r1, t1 , objpoints_l = calibrator('data_stereo/left*.jpg',imgpoints_l,1)
# calibrate right camera
rt, M2, d2, r2, t2 , objpoints_r = calibrator('data_stereo/right*.jpg',imgpoints_r,0)

flags = (cv2.CALIB_FIX_K5 + cv2.CALIB_FIX_K6)

stereocalib_criteria = (cv2.TERM_CRITERIA_MAX_ITER +
                    cv2.TERM_CRITERIA_EPS, 100, 1e-5)

#flags = 0
#flags = cv2.CALIB_USE_INTRINSIC_GUESS
#flags = cv2.CALIB_FIX_PRINCIPAL_POINT
#flags = cv2.CALIB_FIX_ASPECT_RATIO
#flags = cv2.CALIB_ZERO_TANGENT_DIST
#flags = cv2.CALIB_FIX_INTRINSIC
#flags = cv2.CALIB_FIX_FOCAL_LENGTH
#flags = cv2.CALIB_FIX_K1...6
#flags = cv2.CALIB_RATIONAL_MODEL
#flags = cv2.CALIB_THIN_PRISM_MODEL
#flags = cv2.CALIB_SAME_FOCAL_LENGTH
#flags = cv2.CALIB_FIX_S1_S2_S3_S4

flags = (cv2.CALIB_FIX_PRINCIPAL_POINT | cv2.CALIB_FIX_ASPECT_RATIO | cv2.CALIB_FIX_FOCAL_LENGTH |
         cv2.CALIB_FIX_INTRINSIC | cv2.CALIB_FIX_K3 | cv2.CALIB_FIX_K4 | cv2.CALIB_FIX_K5 |
         cv2.CALIB_FIX_K6)

T = np.zeros((3, 1), dtype=np.float64)
R = np.eye(3, dtype=np.float64)

ret, M1, d1, M2, d2, R, T, E, F = cv2.stereoCalibrate(objpoints_l, imgpoints_l,imgpoints_r,
        M1, d1, M2,d2, img_shape, None,None,None,None,
        criteria = stereocalib_criteria,
        flags=flags)
data = {'camera_matrix1': np.asarray(M1).tolist(), 'dist_coeff1': np.asarray(d1).tolist()
    ,'camera_matrix2': np.asarray(M2).tolist(), 'dist_coeff2': np.asarray(d2).tolist()}
with open("stereo_calibration_chess.yaml", "w") as f:
    yaml.dump(data, f)

newCamMtx1, roi1 = cv2.getOptimalNewCameraMatrix(M1, d1,img_shape , 0, img_shape )
newCamMtx2, roi2 = cv2.getOptimalNewCameraMatrix(M2, d2, img_shape, 0, img_shape)

# rectification and undistortion maps which can be used directly to correct the stereo pair
(rectification_l, rectification_r, projection_l,
    projection_r, disparityToDepthMap, ROI_l, ROI_r) = cv2.stereoRectify(
        M1, d1, M2, d2, img_shape, R, T,
        None, None, None, None, None,
        #cv2.CALIB_ZERO_DISPARITY,                  # principal points of each camera have the same pixel coordinates in rect views
        alpha=0)                                   # alpha=1 no pixels lost, alpha=0 pixels lost

leftMapX, leftMapY = cv2.initUndistortRectifyMap(
    M1, d1, rectification_l, projection_l,
    img_shape, cv2.CV_32FC1)
rightMapX, rightMapY = cv2.initUndistortRectifyMap(
    M2, d2, rectification_r, projection_r,
    img_shape, cv2.CV_32FC1)

# compute rectification uncalibrated
### REMAPPING ###
# load images and convert to cv2 format
img_l = images_left[0]
img_l = cv2.cvtColor(img_l, cv2.COLOR_BGR2GRAY)
#img_l_undis = cv2.undistort(img_l, M1, d1, None, newCamMtx1)
img_r = images_right[0]
img_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY)
#img_r_undis = cv2.undistort(img_r, M2, d2, None, newCamMtx2)

# remap
imglCalRect = cv2.remap(img_l, leftMapX, leftMapY, cv2.INTER_LINEAR)
imgrCalRect = cv2.remap(img_r, rightMapX, rightMapY, cv2.INTER_LINEAR)

cv2.imshow("calibRectl",imglCalRect)
cv2.imshow("calibRectr",imgrCalRect)
cv2.waitKey()


import logging
import pandas as pd
from dotenv import load_dotenv
import os
from pymongo import MongoClient
import certifi
import requests
from pyquery import PyQuery

logger = logging.getLogger('process_doc')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())

#init Mongo Client as global
client = None
urlArchives = "https://www.sec.gov/Archives/"
MOZILLA_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
docType = '10-K'
docDateStart = 20231001
docDateEnd = 20231019

def connectMongo():
    # open Mongo
    logger.info("Connect to Mongo")
    load_dotenv()
    user = os.environ.get('MONGODB_USER')
    pswd = os.environ.get('MONGODB_PASSWORD')
    # mongosh -u nick -p <pswd> mongodb+srv://nick:<pswd>@cluster0.5346qcs.mongodb.net/SEC
    port = 27017 #default
    CONNECTION_STRING = 'mongodb+srv://' + user + ':' + pswd + '@cluster0.5346qcs.mongodb.net/SEC'
    client = MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
    return client

def downloadIdx():
    for docDate in range(docDateStart, docDateEnd):
        idx = "company." + str(docDate) + ".idx"
        idx_local = "./data/" + idx
        if not os.path.isfile(idx_local):
            url = urlArchives + "edgar/daily-index/2023/QTR4/" + idx
            logger.info("Downloading " + url + " to " + idx_local)
            headers = {'User-Agent': MOZILLA_AGENT}
            response = requests.get(url, headers=headers)

            # send a HTTP request to the server and save
            with open(idx_local, 'wb') as f:
                f.write(response.content)

def get_docs(idx):
    logger.info("Processing Document Index File " + idx)
    doc_list = []
    with open(idx) as f:
        lines = f.readlines()  # list containing lines of file
        columns = []  # To store column names

        header = True
        for line in lines:
            line = line.strip()  # remove leading/trailing white spaces
            if line:
                if header:
                    if '---' in line:
                        # for now hard code, headers might change
                        columns = ['company_name', 'form_type', 'cik', 'date_filed', 'file_name' ]
                        header = False
                        logger.info("Document Header Found: " + str(columns))
                else:
                    doc = {}  # dictionary to store file data (each line)
                    data = [item.strip() for item in line.split('  ')]
                    index_col = -1
                    for index, elem in enumerate(data):
                        if len(elem) > 0:
                            index_col += 1
                            if index_col < len(columns):
                                doc[columns[index_col]] = data[index]
                    doc_list.append(doc)  # append dictionary to list
                    logger.debug(doc)
    docCount = len(doc_list)
    logger.info("Read " + str(docCount) + " Documents")
    return doc_list

def test_header():
    response = requests.get('http://httpbin.org/headers')
    print(response.status_code)
    print(response.text)

def getIDFromDoc(doc):
    filename = doc['file_name']
    idx_full = filename.split('/')[-1]
    id = idx_full.split('.')[0]
    return id

def saveData(doc, text):
    # get ID
    id = getIDFromDoc(doc)
    logger.info("Save Data: " + str(doc))

    # Getting the database instance
    db = client['SEC']

    # Creating a collection
    colDoc = db['company-doc']
    logger.debug('db: ' + str(db))
    logger.debug('coll: ' + str(colDoc))
    logger.debug("Update Doc")
    filter = {'_id': id}
    docWithText = doc
    docWithText['text'] = text
    vals = {"$set": docWithText}

    # this is always returning None, why?
    result = colDoc.update_one(filter, vals, upsert=True)
    logger.debug("Upserted ID:", str(result.upserted_id))

def request_docs(doc_list):
    # https://stackoverflow.com/questions/11709079/parsing-html-using-python
    logger.info("Requesting " + docType + " Documents")
    for doc in doc_list:
        formType = doc['form_type']
        if docType in formType:
            filename = doc['file_name']
            companyName = doc['company_name']
            id = getIDFromDoc(doc)
            logger.info("Processing " + str(companyName) + ": Document " + str(id))
            #id = "0000320193-22-000108"
            #url = "https://www.sec.gov/Archives/edgar/data/320193/" + id + ".txt"
            url = urlArchives + filename
            headers = {'User-Agent': MOZILLA_AGENT }
            response = requests.get(url, headers=headers)
            logger.debug('Status Code: ' + str(response.status_code))
            html = response.content

            # grab text within spans only
            pq = PyQuery(html)
            tag = pq('span')
            fullText = tag.text()
            if len(fullText) > 10:
                firstLast = 150
                sampleTextHeader = fullText[0:firstLast]
                sampleTextFooter = fullText[-firstLast:]
                sampleText = sampleTextHeader + sampleTextFooter
                logger.debug("Sample Text: " + str(sampleText))
                saveData(doc, str(fullText))

if __name__ == '__main__':
    logger.info('Start MarcusLion Data Process')
    pd.set_option('display.max_rows', 500)
    pd.set_option('display.max_columns', 500)
    pd.set_option('display.width', 1000)
    downloadIdx()
    client = connectMongo()

    # process past week
    for docDate in range(docDateStart, docDateEnd):
        idx = ".\data\company." + str(docDate) + ".idx"
        doc_list = get_docs(idx)
        result = request_docs(doc_list)


import pandas as pd
from pymongo import MongoClient,TEXT
from json import loads
from datetime import datetime

client = MongoClient('mongodb://localhost:27017/')
db = client["datavis"]

senate = db["senate"]
chamber = db["chamber"]

def to_mongo(dest, data, idxs=[]):
    dest.drop()

    for chunk in data:
        dest.insert_one(loads(chunk.to_json(orient='records')))

    for idx in idxs:
    	dest.create_index(idx)

indexes = [("congressperson_name",TEXT)]

deputies_dtype= {"cnpj_cpf":"np.uint64","document_number":"np.uint64","issue_date":datetime}
df_deputies = pd.read_csv('data/Chamber/reimbursements.xz',compression='xz',chunksize=10000,thousands=",")
to_mongo(chamber,df_deputies,indexes)

df_senate = pd.read_csv("data/Senate/federal-senate-reimbursements.xz",compression='xz',chunksize=10000,thousands=",")
to_mongo(senate,df_senate,indexes)


import pandas as pd
import re #清楚数字标点的标准库
from textblob import TextBlob
import  emotion
from nltk.corpus import stopwords  # 下载之后 载入字典
from nltk.stem.porter import PorterStemmer  # stem：词根 PorterStemmer： 词根函数库

positive, negative = emotion.get_emotion()
positive = tuple(positive)
negative = tuple(negative)

table = "microwave"

# dataset = pd.read_csv('data/hair_dryer.tsv', delimiter = '\t', quoting = 3, encoding='utf-8')
# dataset = pd.read_csv('data/pacifier.tsv', delimiter = '\t', quoting = 3, encoding='utf-8')

dataset = pd.read_csv('data/microwave.tsv', delimiter = '\t', quoting = 3, encoding='utf-8')

def get_scores():

    corpus = []  # 空list
    lens = len(dataset)

    listc = []
    mp = dict()
    k = 0
    sum = 0
    scorce = []
    sum_count = 0
    for i in range(0, lens):
        try:
            # if dataset['star_rating'][i] != 5:
            #     continue
            review = re.sub('[^a-zA-Z]', ' ', dataset['review_headline'][i] + dataset['review_body'][i])  # 去除标点，数字，去除之后用空格代替，只留下大小写字母
            review = review.lower()  # 全部转换成小写
            review = review.split()  # 将句子字符串，转换成含有不同单词的list
            ps = PorterStemmer()  # 取词根化的方程
            review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]  # 用词根化后的结果进行循环
            # set让速度变得更快。 最后查看是否在英文的虚词字典里 ‘english’
            # 代码运行到这里 举栗子： 原来第一行review便转化成了 ['wow','love','place']

            for item in review:
                if item not in positive and item not in negative:
                    continue
                if item not in mp:
                    mp[item] = 1
                else:
                    mp[item] += 1
            review = ' '.join(review)  # 在每两个单词之间加上空格，并重现转化成字符串

            # 情感分析
            blob = TextBlob(review)
            # ans = -1
            temp_sum = 0
            temp_c = 0
            lens2 = len(blob.sentences)
            if(len != 0):
                for item in blob.sentences:
                    # print(item.sentiment)
                    # print(item.sentiment.polarity)
                    temp_sum += item.sentiment.polarity
                    temp_c += item.sentiment.subjectivity
            if temp_sum != 0:
                temp_list = []
                temp_list.append(temp_sum / lens2)
                temp_list.append(temp_c / lens2)
                scorce.append(temp_list)
            else:
                scorce.append([-2, 0.75])
            # print(temp_sum)
            if lens2 != 0:
                sum += temp_sum / lens2

            corpus.append(review)  # 每处理一行，变加入list
            listc.append(dataset.iloc[i, 7])
        except Exception as e:
            k += 1
            scorce.append([-2, 0.75])
            print(e)
            continue
        else:
            sum_count += 1
            if (i % 500 == 0):
                print(i)

    ave = sum / lens
    print(ave, len(scorce), lens)

    with open("data/评论得分%s.csv" % table, "w+") as f:
        for item in scorce:
            # print(item)
            if item[0] != -2:
                f.write(str(item[0]) + ',' + str(item[1]) + '\n')
            else:
                f.write(str(ave) + ',0.75\n')
                item[0] = ave
                item[1] = 0.75

    with open("data/词频统计%s.csv" % table, "w+") as f:
    # 输出频率最高的情感单词
        mp = sorted(mp.items(), key=lambda d: d[1], reverse=True)
        for item in mp:
            print(item, end='\n')
            # print(mp[item])
            f.write(str(item)+',' +'\n')
    return scorce, dataset, table


from collections import OrderedDict
import glob
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('apj')
import h5py

from scipy.interpolate import interp1d
import pandas as pd

dirs_p1 = ["erf_AE_cut/erf_step_3D_Re4e2_P1e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild/",
           "erf_AE_cut/erf_step_3D_Re4e2_P1e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart/"]

dirs_p1_down = ['noslip_erf_P_cut/erf_step_3D_Re4e2_P1e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0/']
dirs_p1_up   = ['erf_AE_cut/erf_step_3D_Re4e2_P1e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0.3']

dirs_p2 = ["erf_AE_cut/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild/",
           "erf_AE_cut/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart/",
           "erf_AE_cut/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart2/",
           "erf_AE_cut/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart3/"]

dirs_p2_down = ['noslip_erf_P_cut/not_in_use/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0.1/']
dirs_p2_up   = ['erf_AE_cut/erf_step_3D_Re4e2_P2e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0.45']

dirs_p4 = ["erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart2/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart3/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart4/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart5/",
        "erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer0_64x64x256_schwarzschild_restart6/"]

dirs_p4_down = ['noslip_erf_P_cut/not_in_use/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0.4/']
dirs_p4_up   = ['erf_AE_cut/erf_step_3D_Re4e2_P4e0_zeta1e-3_S1e3_Lz2_Lcz1_Pr0.5_a2_Titer100_64x64x256_predictive0.7']

name_dirs_p1      = 'erf_re4e2_p1e0_s1e3_longevolution'
name_dirs_p1_down = 'erf_re4e2_p1e0_s1e3_pertDown'
name_dirs_p1_up   = 'erf_re4e2_p1e0_s1e3_pertUp'
name_dirs_p2      = 'erf_re4e2_p2e0_s1e3_longevolution'
name_dirs_p2_down = 'erf_re4e2_p2e0_s1e3_pertDown'
name_dirs_p2_up   = 'erf_re4e2_p2e0_s1e3_pertUp'
name_dirs_p4      = 'erf_re4e2_p4e0_s1e3_longevolution'
name_dirs_p4_down = 'erf_re4e2_p4e0_s1e3_pertDown'
name_dirs_p4_up   = 'erf_re4e2_p4e0_s1e3_pertUp'

dir_lists = [   dirs_p1,
                dirs_p1_down,
                dirs_p1_up,
                dirs_p2,
                dirs_p2_down,
                dirs_p2_up,
                dirs_p4,
                dirs_p4_down,
                dirs_p4_up ]

name_lists = [  name_dirs_p1,
                name_dirs_p1_down,
                name_dirs_p1_up,
                name_dirs_p2,
                name_dirs_p2_down,
                name_dirs_p2_up,
                name_dirs_p4,
                name_dirs_p4_down,
                name_dirs_p4_up ]

for dirs, this_trace_name in zip(dir_lists, name_lists):
    #Parse input parmeters
    data = OrderedDict()
    S = float(dirs[0].split('_S')[-1].split('_')[0])
    P = float(dirs[0].split('_Pr')[0].split('_P')[-1].split('_')[0])
    Re = float(dirs[0].split('_Re')[-1].split('_')[0])
    Lz = float(dirs[0].split('_Lz')[-1].split('_')[0])
    threeD = True
    erf = True

    k_rz = 0.2 / (S * P)
    t_diff = 1/k_rz

    #Read in scalar and profile data
    times = []
    L_d01s = []
    L_d05s = []
    L_d09s = []
    theory_f = []
    theory_xi = []
    grad = []
    prof_times = []
    for i, d in enumerate(dirs):
        if i == 0 and 'longevolution' in d:
            skip_points = 100
        else:
            skip_points = 0
        with h5py.File('{:s}/data_top_cz.h5'.format(d), 'r') as f:
            dictionary = dict()
            for k in ['times', 'L_d01s', 'L_d05s', 'L_d09s']:
                dictionary[k] = f[k][()]
                if k != 'times':
                    dictionary[k] = dictionary[k][np.unique(dictionary['times'], return_index=True)[1]]
            try:
                for k in ['modern_f', 'modern_xi']:
                    dictionary[k] = f[k][()]
                    dictionary[k] = dictionary[k][np.unique(dictionary['times'], return_index=True)[1]]

            except:
                #some older files don't have modern_f/modern_xi
                Ls = f['Ls'][()]
                L_d09s_tmp = f['L_d09s'][()]
                enstrophy_Ls = f['enstrophy_Ls'][()]
                enstrophy_cz = f['enstrophy_cz'][()]
                fconv_Ls = f['fconv_Ls'][()]

                L_pz = L_d09s_tmp - Ls
                dissipation_cz   = enstrophy_cz/Re
                dissipation_Ls   = enstrophy_Ls/Re
                dissipation_pz   = (dissipation_cz * L_d09s_tmp - dissipation_Ls * Ls) / L_pz

                modern_f         = dissipation_Ls/fconv_Ls
                modern_xi        = ( (dissipation_pz*L_pz) / (fconv_Ls*Ls) ) / (modern_f * (L_pz/Ls))
                dictionary['modern_f'] = modern_f
                dictionary['modern_xi'] = modern_xi
                for k in ['modern_f', 'modern_xi']:
                    dictionary[k] = dictionary[k][np.unique(dictionary['times'], return_index=True)[1]]

            dictionary['times'] = np.unique(dictionary['times'])
            if i > 0:
                #clean out some time artifacts from restarting simulations
                skip_points = int(np.sum(dictionary['times'] <= times[-1][-1]))

            times.append(dictionary['times'][skip_points:])
            L_d01s.append(dictionary['L_d01s'][skip_points:])
            L_d05s.append(dictionary['L_d05s'][skip_points:])
            L_d09s.append(dictionary['L_d09s'][skip_points:])
            theory_f.append(dictionary['modern_f'][skip_points:])
            theory_xi.append(dictionary['modern_xi'][skip_points:])
            Ls = f['Ls'][()]
        with h5py.File('{:s}/avg_profs/averaged_avg_profs.h5'.format(d), 'r') as f:
            z = f['z'][()]
            grad_ad = -f['T_ad_z'][()][0,:].squeeze()
            grad_rad = -f['T_rad_z'][()][0,:].squeeze()

            grad.append(-f['T_z'][()])
            prof_times.append(f['T_z_times'][()])

    times          = np.array(np.concatenate(times))
    L_d01s         = np.array(np.concatenate(L_d01s))
    L_d05s         = np.array(np.concatenate(L_d05s))
    L_d09s         = np.array(np.concatenate(L_d09s))
    theory_f       = np.array(np.concatenate(theory_f))
    theory_xi      = np.array(np.concatenate(theory_xi))

    grad           = np.array(np.concatenate(grad, axis=0))
    prof_times     = np.array(np.concatenate(prof_times))

    with h5py.File('temporal_data/{}.h5'.format(this_trace_name), 'w') as f:
        f['scalars/Ls']  = Ls
        f['scalars/d01'] = L_d01s - Ls
        f['scalars/d05'] = L_d05s - Ls
        f['scalars/d09'] = L_d09s - Ls
        f['scalars/times'] = times
        f['scalars/f']     = theory_f
        f['scalars/xi']    = theory_xi
        f['profiles/z']     = z
        f['profiles/grad']  = grad
        f['profiles/times'] = prof_times
        f['profiles/grad_ad'] = grad_ad
        f['profiles/grad_rad'] = grad_rad


import sys
from collections import deque

def bfs(v):
    q = deque([v]) #큐 구현을 위해 deque 사용
    while q:
        v = q.popleft()
        if v == k:
            return visited[v]#BFS연산이므로 최소값을 따로 정의하지 않아도 된다.
        for i in (v-1, v+1, 2*v):
            if 0 <= i <= 100000 and not visited[i]:
                visited[i] = visited[v] + 1
                q.append(i)

n, k = map(int, sys.stdin.readline().split())
visited = [0 for i in range(100001)]
print(bfs(n))

#BFS 함수 안에서 if문 사용시 and로 복수 조건 걸면 순차적으로 확인.


from unityagents import UnityEnvironment
import numpy as np
from ddpg_agent import Agent
import torch
from collections import deque
import matplotlib.pyplot as plt

env = UnityEnvironment(file_name='Tennis_Windows_x86_64/Tennis.exe',no_graphics=True)

# get the default brain
brain_name = env.brain_names[0]
brain = env.brains[brain_name]

# reset the environment
env_info = env.reset(train_mode=True)[brain_name]

# number of agents
num_agents = len(env_info.agents)

# size of each action
action_size = brain.vector_action_space_size

# size state space
states = env_info.vector_observations
state_size = states.shape[1]

# init agent
agent = Agent(state_size, action_size, random_seed=2)

def ddpg(n_episodes=3000, target_score=0.5, print_every=10, window_len=100, updateAfterEvery=1, numUpdateCycles=1):
    scoreOverEpisodes = []
    averageScore = []
    scoreDeque = deque(maxlen=window_len)
    actions = np.zeros([num_agents, action_size])

    for i_episode in range(1, n_episodes+1):
        env_info = env.reset(train_mode=True)[brain_name]      # reset the environment
        states = env_info.vector_observations                  # get the current state (for each agent)
        agent.reset()                                          # reset noise
        scores = np.zeros(num_agents)                          # initialize the score (for each agent)
        t = 0

        while True:
            t += 1
            actions = agent.act(states)                        # select an action (for each agent)
            env_info = env.step(actions)[brain_name]           # send all actions to tne environment
            next_states = env_info.vector_observations         # get next state (for each agent)
            rewards = env_info.rewards                         # get reward (for each agent)
            dones = env_info.local_done                        # see if episode finished

            for state, action, reward, next_state, done in zip(states, actions, rewards, next_states, dones):
                agent.store(state, action, reward, next_state, done)

            if t % updateAfterEvery == 0:
                for i in range(numUpdateCycles):
                    agent.learnFromBufferSamples()

            scores += rewards                                  # update the score (for each agent)
            states = next_states                               # roll over states to next time step
            if np.any(dones):                                  # exit loop if episode finished
                break

        if i_episode % print_every == 0:
            print('Score of episode {:04d}: {:1.4f} \t Window mean: {:1.4f}'.format(i_episode, np.max(scores), np.mean(scoreDeque)))

        scoreOverEpisodes.append(np.max(scores))
        scoreDeque.append(np.max(scores))
        averageScore.append(np.mean(scoreDeque))

        if np.mean(scoreDeque) >= target_score:
            # solved
            break

    torch.save(agent.actor_local.state_dict(), 'checkpoint_actor.pth')      # save final actor
    torch.save(agent.critic_local.state_dict(), 'checkpoint_critic.pth')    # save final critic

    print('Windowed mean score at episode {:04d}: \t Window mean: {:1.4f}'.format(i_episode, np.max(scores), np.mean(scoreDeque)))

    return scoreOverEpisodes, averageScore

scores, average = ddpg()
env.close()

# plot results
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(np.arange(1, len(scores)+1), scores, label='instant')
plt.plot(np.arange(1, len(average)+1), average, label='average')
plt.ylabel('Score')
plt.xlabel('Episode #')
ax.legend(fontsize='large', loc='upper left')
plt.show()
fig.savefig('scores.png')


import asyncio
import random
import time, logging

format = '[%(asctime)s] %(levelname)s: %(message)s'
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format=format)

async def heavy_work(n: int):
    i = 2 # random.randint(0, 2)
    logger.info(f"sleep for {i}")
    await asyncio.sleep(i)
    return f"finished {n}"

async def main(*args):
    results = await asyncio.gather(*(heavy_work(n) for n in args))
    logger.info(results)

if __name__ == "__main__":
    args = [1, 2, 3]

    start = time.perf_counter()
    asyncio.run(main(*args))

    end = time.perf_counter() - start
    logger.debug(f"Program finished in {end:0.2f} seconds.")


import xenon_worker as wkr
import utils
import asyncio
import pymongo
from datetime import datetime, timedelta
import random

from backups import BackupSaver, BackupLoader

MAX_BACKUPS = 15

class BackupListMenu(wkr.ListMenu):
    embed_kwargs = {"title": "Your Backups"}

    async def get_items(self):
        args = {
            "limit": 10,
            "skip": self.page * 10,
            "sort": [("timestamp", pymongo.DESCENDING)],
            "filter": {
                "creator": self.ctx.author.id,
            }
        }
        backups = self.ctx.bot.db.backups.find(**args)
        items = []
        async for backup in backups:
            items.append((
                backup["_id"],
                f"{backup['data']['name']} (`{utils.datetime_to_string(backup['timestamp'])} UTC`)"
            ))

        return items

class Backups(wkr.Module):
    @wkr.Module.listener()
    async def on_load(self, *_, **__):
        await self.bot.db.backups.create_index([("creator", pymongo.ASCENDING)])
        await self.bot.db.backups.create_index([("timestamp", pymongo.ASCENDING)])
        await self.bot.db.backups.create_index([("data.id", pymongo.ASCENDING)])

    @wkr.Module.command(aliases=("backups", "bu"))
    async def backup(self, ctx):
        """
        Create & load private backups of your servers
        """
        await ctx.invoke("help backup")

    @backup.command(aliases=("c",))
    @wkr.guild_only
    @wkr.has_permissions(administrator=True)
    @wkr.bot_has_permissions(administrator=True)
    @wkr.cooldown(1, 10, bucket=wkr.CooldownType.GUILD)
    async def create(self, ctx):
        """
        Create a backup

        __Examples__

        ```{b.prefix}backup create```
        """
        backup_count = await ctx.bot.db.backups.count_documents({"creator": ctx.author.id})
        if backup_count >= MAX_BACKUPS:
            raise ctx.f.ERROR(
                f"You have **exceeded the maximum count** of backups. (`{backup_count}/{MAX_BACKUPS}`)\n"
                f"You need to **delete old backups** with `{ctx.bot.prefix}backup delete <id>` or **buy "
                f"[Xenon Premium](https://www.patreon.com/merlinfuchs)** to create new backups.."
            )

        status_msg = await ctx.f_send("**Creating Backup** ...", f=ctx.f.WORKING)
        guild = await ctx.get_full_guild()
        backup = BackupSaver(ctx.client, guild)
        await backup.save()

        backup_id = utils.unique_id()
        await ctx.bot.db.backups.insert_one({
            "_id": backup_id,
            "creator": ctx.author.id,
            "timestamp": datetime.utcnow(),
            "data": backup.data
        })

        embed = ctx.f.format(f"Successfully **created backup** with the id `{backup_id}`.", f=ctx.f.SUCCESS)["embed"]
        embed.setdefault("fields", []).append({
            "name": "Usage",
            "value": f"```{ctx.bot.prefix}backup load {backup_id}```\n"
                     f"```{ctx.bot.prefix}backup info {backup_id}```"
        })
        await ctx.client.edit_message(status_msg, embed=embed)

    @backup.command(aliases=("l",))
    @wkr.guild_only
    @wkr.has_permissions(administrator=True)
    @wkr.bot_has_permissions(administrator=True)
    @wkr.cooldown(1, 60, bucket=wkr.CooldownType.GUILD)
    async def load(self, ctx, backup_id, *options):
        """
        Load a backup

        __Arguments__

        **backup_id**: The id of the backup or the guild id of the latest automated backup
        **options**: A list of options (See examples)

        __Examples__

        Default options: ```{b.prefix}backup load oj1xky11871fzrbu```
        Only roles: ```{b.prefix}backup load oj1xky11871fzrbu !* roles```
        Everything but bans: ```{b.prefix}backup load oj1xky11871fzrbu !bans```
        """
        backup_d = await ctx.client.db.backups.find_one({"_id": backup_id, "creator": ctx.author.id})
        if backup_d is None:
            raise ctx.f.ERROR(f"You have **no backup** with the id `{backup_id}`.")

        warning_msg = await ctx.f_send("Are you sure that you want to load this backup?\n"
                                       "__**All channels and roles will get replaced!**__", f=ctx.f.WARNING)
        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and
                                d["user_id"] == ctx.author.id and
                                d["emoji"]["name"] in reactions,
                timeout=60
            )
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        guild = await ctx.get_full_guild()
        backup = BackupLoader(ctx.client, guild, backup_d["data"], reason="Backup loaded by " + str(ctx.author))
        await backup.load(**utils.backup_options(options))

    @backup.command(aliases=("del", "remove", "rm"))
    @wkr.cooldown(5, 30)
    async def delete(self, ctx, backup_id):
        """
        Delete one of your backups
        __**This cannot be undone**__

        __Examples__

        ```{b.prefix}backup delete 3zpssue46g```
        """
        result = await ctx.client.db.backups.delete_one({"_id": backup_id, "creator": ctx.author.id})
        if result.deleted_count > 0:
            raise ctx.f.SUCCESS("Successfully **deleted backup**.")

        else:
            raise ctx.f.ERROR(f"You have **no backup** with the id `{backup_id}`.")

    @backup.command(aliases=("clear",))
    @wkr.cooldown(1, 60, bucket=wkr.CooldownType.GUILD)
    async def purge(self, ctx):
        """
        Delete all your backups
        __**This cannot be undone**__

        __Examples__

        ```{b.prefix}backup purge```
        """
        warning_msg = await ctx.f_send("Are you sure that you want to delete all your backups?\n"
                                       "__**This cannot be undone!**__", f=ctx.f.WARNING)
        reactions = ("✅", "❌")
        for reaction in reactions:
            await ctx.client.add_reaction(warning_msg, reaction)

        try:
            data, = await ctx.client.wait_for(
                "message_reaction_add",
                ctx.shard_id,
                check=lambda d: d["message_id"] == warning_msg.id and
                                d["user_id"] == ctx.author.id and
                                d["emoji"]["name"] in reactions,
                timeout=60
            )
        except asyncio.TimeoutError:
            await ctx.client.delete_message(warning_msg)
            return

        await ctx.client.delete_message(warning_msg)
        if data["emoji"]["name"] != "✅":
            return

        await ctx.client.db.backups.delete_many({"creator": ctx.author.id})
        raise ctx.f.SUCCESS("Deleted all your backups.")

    @backup.command(aliases=("ls",))
    @wkr.cooldown(1, 10)
    async def list(self, ctx):
        """
        Get a list of your backups

        __Examples__

        ```{b.prefix}backup list```
        """
        menu = BackupListMenu(ctx)
        await menu.start()

    @backup.command(aliases=("i",))
    @wkr.cooldown(5, 30)
    async def info(self, ctx, backup_id):
        """
        Get information about a backup

        __Arguments__

        **backup_id**: The id of the backup or the guild id to for latest automated backup

        __Examples__

        ```{b.prefix}backup info oj1xky11871fzrbu```
        """
        backup = await ctx.client.db.backups.find_one({"_id": backup_id, "creator": ctx.author.id})
        if backup is None:
            raise ctx.f.ERROR(f"You have **no backup** with the id `{backup_id}`.")

        backup["data"].pop("members", None)
        guild = wkr.Guild(backup["data"])

        channels = utils.channel_tree(guild.channels)
        if len(channels) > 1024:
            channels = channels[:1000] + "\n...\n```"

        roles = "```{}```".format("\n".join([
            r.name for r in sorted(guild.roles, key=lambda r: r.position, reverse=True)
        ]))
        if len(roles) > 1024:
            roles = roles[:1000] + "\n...\n```"

        raise ctx.f.DEFAULT(embed={
            "title": guild.name,
            "fields": [
                {
                    "name": "Created At",
                    "value": utils.datetime_to_string(backup["timestamp"]) + " UTC",
                    "inline": False
                },
                {
                    "name": "Channels",
                    "value": channels,
                    "inline": True
                },
                {
                    "name": "Roles",
                    "value": roles,
                    "inline": True
                }
            ]
        })

    @backup.command(aliases=("iv",))
    @wkr.guild_only
    @wkr.has_permissions(administrator=True)
    @wkr.bot_has_permissions(administrator=True)
    @wkr.cooldown(1, 10, bucket=wkr.CooldownType.GUILD)
    async def interval(self, ctx, *interval):
        """
        Manage automated backups

        __Arguments__

        **interval**: The time between every backup or "off". (min 24h)
                    Supported units: hours(h), days(d), weeks(w)
                    Example: 1d 12h

        __Examples__

        ```{b.prefix}backup interval 24h```
        """
        if len(interval) > 0:
            await ctx.invoke("backup interval on " + " ".join(interval))
            return

        interval = await ctx.bot.db.intervals.find_one({"_id": ctx.guild_id})
        if interval is None:
            raise ctx.f.INFO("The **backup interval is** currently turned **off**.\n"
                             f"Turn it on with `{ctx.bot.prefix}backup interval on 24h`.")

        else:
            raise ctx.f.INFO(embed={
                "author": {
                    "name": "Backup Interval"
                },
                "fields": [
                    {
                        "name": "Interval",
                        "value": utils.timedelta_to_string(timedelta(hours=interval["interval"])),
                        "inline": True
                    },
                    {
                        "name": "Next Backup",
                        "value": utils.datetime_to_string(interval["next"]),
                        "inline": True
                    }
                ]
            })

    @interval.command(aliases=["enable"])
    @wkr.cooldown(1, 10, bucket=wkr.CooldownType.GUILD)
    async def on(self, ctx, *interval):
        """
        Turn on automated backups

        __Arguments__

        **interval**: The time between every backup. (min 24h)
                    Supported units: hours(h), days(d), weeks(w)
                    Example: 1d 12h

        __Examples__

        ```{b.prefix}backup interval on 24h```
        """
        units = {
            "h": 1,
            "d": 24,
            "w": 24 * 7
        }

        hours = 0
        for arg in interval:
            count, unit = int(arg[:-1]), arg[-1]
            multiplier = units.get(unit.lower(), 1)
            hours += count * multiplier

        hours = max(hours, 24)

        now = datetime.utcnow()
        td = timedelta(hours=hours)
        await ctx.bot.db.intervals.update_one({"_id": ctx.guild_id}, {"$set": {
            "_id": ctx.guild_id,
            "last": now,
            "next": now + td,
            "interval": hours
        }}, upsert=True)

        raise ctx.f.SUCCESS("Successful **enabled the backup interval**.\nThe first backup will be created in "
                            f"`{utils.timedelta_to_string(td)}` "
                            f"at `{utils.datetime_to_string(datetime.utcnow() + td)} UTC`.")

    @interval.command(aliases=["disable"])
    @wkr.cooldown(1, 10, bucket=wkr.CooldownType.GUILD)
    async def off(self, ctx):
        """
        Turn off automated backups

        __Examples__

        ```{b.prefix}backup interval off```
        """
        result = await ctx.bot.db.intervals.delete_one({"_id": ctx.guild_id})
        if result.deleted_count > 0:
            raise ctx.f.SUCCESS("Successfully **disabled the backup interval**.")

        else:
            raise ctx.f.ERROR(f"The backup interval is not enabled.")

    async def run_interval_backups(self, guild_id):
        guild = await self.bot.get_full_guild(guild_id)
        if guild is None:
            return

        backup = BackupSaver(self.bot, guild)
        await backup.save()

        await self.bot.db.backups.replace_one({"_id": guild_id}, {
            "_id": guild_id,
            "creator": guild.owner_id,
            "timestamp": datetime.utcnow(),
            "interval": True,
            "data": backup.data
        }, upsert=True)

    @wkr.Module.task(minutes=random.randint(5, 15))
    async def interval_task(self):
        to_backup = self.bot.db.intervals.find({"next": {"$lt": datetime.utcnow()}})
        async for interval in to_backup:
            guild_id = interval["_id"]
            self.bot.schedule(self.run_interval_backups(guild_id))
            await self.bot.db.intervals.update_one({"_id": guild_id}, {"$set": {
                "next": interval["next"] + timedelta(hours=interval["interval"])
            }})


import json
from enum import Enum
from typing import List, Optional

from pydantic import BaseModel, Field, validator  # pylint: disable=no-name-in-module

class ContentSignature(BaseModel):
    """
    Defines the data used to generate the signature, as well as the signer's identifier.
    """
    event_content_hash: str = Field(
        description="Hash of the event content. It's also the request id")
    signer: str = Field(description="Identifier of the signer")
    timestamp: int = Field(description="Timestamp of the event")

class Signature(BaseModel):
    """
    The format, in addition to the signature, includes additional information,
    namely the signer's identifier, the signature timestamp and the hash of the signed contents.
    """
    content: ContentSignature = Field(description="The signed content")
    signature: str = Field(description="The signature value")

class Metadata(BaseModel):
    """
    Metadata of a TAPLE Event
    """
    governance_id: str = Field(description="Identifier of the governance")
    governance_version: int = Field(
        description="Version of the governance")
    namespace: str = Field(description="Namespace")
    owner: str = Field(description="Owner identifier")
    schema_id: str = Field(description="Identifier of the schema")

class ApprovalType(str, Enum):
    """
    Approval type
    """
    Accept = "Accept"
    Reject = "Reject"

class ContentApproval(BaseModel):
    """
    Defines the data used to generate the signature,
    as well as the signer's identifier.
    """
    approval_type: ApprovalType = Field(description="Type of approval")
    event_request_hash: str = Field(
        description="Hash of the event request")
    expected_sn: int = Field(description="Expected sequence number")
    signer: str = Field(description="Identifier of the signer")
    timestamp: int = Field(description="Timestamp of the approval")

class Approval(BaseModel):
    """
    Approval of a TAPLE Event
    """
    content: ContentApproval = Field(
        description="Content approval")
    signature: str = Field(description="The signature value")

class Payload(BaseModel):
    """
    Payload of a TAPLE Event type json
    """
    json_: Optional[dict] = Field(
        description="Payload", alias="Json", default=None)
    json_patch: Optional[dict] = Field(
        description="Payload", alias="JsonPatch", default=None)

    @validator("json_", pre=True)
    @classmethod
    def coerce_dict(cls, value):
        """
        Coerce dict
        """
        if isinstance(value, dict):
            return value
        return json.loads(value)

    @validator("json_patch", pre=True)
    @classmethod
    def coerce_dict_patch(cls, value):
        """
        Coerce dict
        """
        if isinstance(value, dict):
            return value
        return json.loads(value)

    class Config:  # pylint: disable=missing-class-docstring
        allow_population_by_field_name = True

class Create(BaseModel):
    """
    Create event
    """
    governance_id: str = Field(description="Governance identifier")
    namespace: str = Field(description="Namespace")
    payload: Payload = Field(description="Payload object")
    schema_id: str = Field(
        description="Identifier of the schema used by the object")

class State(BaseModel):
    """
    Change State event
    """
    payload: Payload = Field(description="Payload object")
    subject_id: str = Field(description="Subject identifier")

class RequestType(str, Enum):
    """
    Approval type
    """
    CREATE = "Create"
    STATE = "State"

class Request(BaseModel):
    """
    Request of an event
    """
    create: Optional[Create] = Field(
        description="Create event", alias="Create", default=None)
    state: Optional[State] = Field(
        description="State event", alias="State", default=None)

    @property
    def get_type(self) -> RequestType:
        """
        Get type of event
        """
        return RequestType.CREATE if self.create else RequestType.STATE

    @property
    def payload(self) -> Payload:
        """
        Get payload of event
        """
        return self.create.payload if self.create else self.state.payload

    @property
    def subject_id(self) -> str:
        """
        Get subject id of event
        """
        return self.state.subject_id if self.state else None

    @property
    def schema_id(self) -> str:
        """
        Get schema id of event
        """
        return self.create.schema_id if self.create else None

    class Config:  # pylint: disable=missing-class-docstring
        allow_population_by_field_name = True

class EventRequest(BaseModel):
    """
    Request that originated the event. It contains basically the proposed change and the votes obtained related to it.
    """
    approvals: List[Approval] = Field(description="List of approvals")
    request: Request = Field(description="Request object")
    signature: Signature = Field(description="Signature object")
    timestamp: int = Field(description="Timestamp")

    @property
    def request_id(self) -> str:
        """
        Get request id
        """
        return self.signature.content.event_content_hash

class EventContent(BaseModel):
    """
    Content of a TAPLE Event
    """
    approved: bool = Field(
        description="Indicates if the event is approved")
    event_request: EventRequest = Field(
        description="Request that originated the event")
    metadata: Metadata = Field(description="Metadata of the event")
    previous_hash: str = Field(description="Hash of the previous event")
    sn: int = Field(description="Sequence number of the event")
    state_hash: str = Field(description="Hash of the state")
    subject_id: str = Field(description="Subject identifier")

class Event(BaseModel):
    """
    TAPLE Event
    """
    event_content: EventContent = Field(
        description="Content of the event")
    signature: Signature = Field(description="Signature of the event")


from typing import List

from raspberry_home.view.view import *

class ZStack(View):

    def __init__(self, children: List[View]):
        self.children = children

    def content_size(self, container_size: Size) -> Size:
        width, height = 0, 0
        for child in self.children:
            content_size = child.content_size(container_size)
            width = max(width, content_size.width)
            height = max(height, content_size.height)
        return Size(width, height)

    def render(self, context: RenderContext):
        for child in self.children:
            child.render(context)


from django.conf import settings
from django.core.mail import EmailMultiAlternatives, send_mail
from django.template import loader, RequestContext, TemplateDoesNotExist

email_templates = [
    'order_confirmation',
    'payment_confirmation'
]

def templates():
    """Loads all the required templates"""
    base = {}
    for template in email_templates:
        base.update({template: loader.render_to_string(template)})
    return base

class Notifications:
    def __init__(self, request, template_name, order_or_reference, **kwargs):
        # self.templates = templates()
        self.subject = None
        self.body = None

        template = loader.render_to_string(template_name, RequestContext(request))