bl_info = {"name": "Venturial",
           "description": "A GUI to alleviate the effort of constructing OpenFOAM cases.",
           "author": "Rajdeep Adak at FOSSEE, IIT Bombay",
           "contributors": "visit www.github.com/venturial/contributors",
           "version": (0, 1, 0),
           "blender": (3, 2, 1),
           "location": "View3D > Side bar > Venturial",
           "category": "Development"}

import bpy, bmesh, os
import bpy.utils.previews

from bpy.utils import register_class, unregister_class

from bpy.props import (IntProperty,
                       FloatProperty,
                       FloatVectorProperty,
                       BoolProperty,
                       StringProperty,
                       PointerProperty,
                       CollectionProperty,
                       EnumProperty)

from bpy.types import (Operator,
                       Panel,
                       AddonPreferences,
                       PropertyGroup,
                       UIList)

from gpu_extras.batch import batch_for_shader
from bpy_extras.view3d_utils import location_3d_to_region_2d

from venturial.startup.get_tutorials_list import add_tutorials_to_scene
from venturial.startup.get_recents_list import add_recents_to_scene

from venturial.models.header.file_handling_operators import *
from venturial.models.header.developer_menu_operators import *
from venturial.models.header.general_operators import *
from venturial.models.header.help_menu_operators import *

from venturial.models.mainpanel_sublayout_operators import *
from venturial.models.blockmesh.design_operators import *
from venturial.models.visualizer_operators import VNT_OT_vertex_data_control, VNT_OT_edge_data_control
from venturial.models.blockmesh.edge_operators import *
from venturial.models.tutorials_menu_operators import *

# from venturial.models.geometry_designer_operators import *
# from venturial.models.mesh_file_manager_operators import *
# from venturial.models.get_vertices_operators import *
# from venturial.models.boundary_control_operators import *
# from venturial.models.run_panel_operators import *

from venturial.views.schemas.UIList_schemas import *
from venturial.views.user_mode_view import VNT_PT_usermodeview
from venturial.views.header.view import *
from venturial.views.mainpanel.meshing_tools.blockmesh import VNT_PT_cell_location
from venturial.views.mainpanel.view import VNT_OT_active_project_indicator, VNT_OT_list_category
from venturial.views.mainpanel.tutorials import VNT_PT_filter_tutorials
from venturial.views.mainpanel.recents import VNT_PT_filter_recents
from venturial.views.mainpanel.visualizer import VNT_PT_statistics_settings

from venturial.utils.custom_icon_object_generator import register_custom_icon, unregister_custom_icon

from venturial.lib.update_methods import *
from venturial.lib.preferences_properties import VNT_user_preferences_collection
from venturial.lib.global_properties import VNT_global_properties_collection

classes = (VNT_user_preferences_collection,
           VNT_OT_save_preferences,
           VNT_OT_reset_preferences,
           VNT_OT_import_preferences,
           VNT_OT_new_case,
           VNT_OT_select_mesh_filepath,
           VNT_OT_build_mesh,
           VNT_OT_import_mesh,
           VNT_OT_open_case,
           VNT_OT_delete_mesh_file_items,
           VNT_OT_deactivate_mesh_file_item,
           VNT_OT_dev_mode,
           VNT_OT_dev_tools,
           VNT_OT_user_general_settings,
           VNT_OT_select_default_mesh_filepath,
           VNT_OT_select_default_tut_filepath,
           VNT_OT_select_default_user_data_filepath,
           VNT_OT_list_category,
           VNT_OT_venturial_maintools,
           VNT_OT_venturial_homepage,
           VNT_OT_fossee_homepage,
           VNT_OT_close_venturial,
           VNT_OT_user_guide,
           VNT_OT_developer_guide,
           VNT_OT_feature_request,
           VNT_OT_report_bugs,
           VNT_OT_developer_support,
           VNT_OT_user_community,
           VNT_OT_developer_community,
           VNT_OT_release_notes,
           VNT_PT_usermodeview,
           VNT_OT_mainpanel_layout,
           fileitemproperties,
           recent_item_properties,
           tutorialitemproperties,
           VNT_MT_dev_menu,
           VNT_MT_file_menu,
           VNT_PT_uicategory,
           VNT_MT_about_venturial,
           VNT_MT_about_fossee,
           VNT_MT_help_menu,
           VNT_global_properties_collection,
           VNT_UL_mesh_file_manager,
           VNT_UL_mesh_file_coroner,
           VNT_PT_cell_location,
           VNT_PT_statistics_settings,
           VNT_OT_location_spawnner,
           VNT_OT_add_to_viewport,
           VNT_OT_compose,
           VNT_OT_get_blocks,
           VNT_OT_remove_blocks,
           VNT_OT_remove_all_blocks,
           VNT_OT_clearblocks,
           VNT_OT_blocksdatacontrol,
           VNT_OT_showselectedblocks,
           VNT_OT_select_unselect_allblocks,
           VNT_OT_vertex_data_control,
           VNT_OT_edge_data_control,
           VNT_OT_generate_edge,
           VNT_OT_edit_edge,
           VNT_OT_destroy_edge,
           VNT_OT_more_tutorials_viewer,
           VNT_OT_tutorial_viewer,
           VNT_PT_filter_tutorials,
           VNT_PT_filter_recents,
           VNT_OT_active_project_indicator)

def register():

    register_custom_icon("venturial_logo", "/venturial/icons/custom_icons/venturial_logo.png")
    register_custom_icon("fossee_logo", "/venturial/icons/custom_icons/fossee_logo.png")
    register_custom_icon("new_mesh_file_2", "/venturial/icons/custom_icons/new_mesh_file_2.png")
    register_custom_icon("build_mesh_2", "/venturial/icons/custom_icons/build_mesh_2.png")
    register_custom_icon("warning_sign_1", "/venturial/icons/custom_icons/warning_sign_1.png")
    register_custom_icon("file-browser-2", "/venturial/icons/custom_icons/file-browser-2.png")

    for cls in classes:
        register_class(cls)

    bpy.types.Scene.search_tuts = StringProperty(default = "Search Tutorials")
    bpy.types.Scene.search_recents = StringProperty(default = "Search Recents")
    bpy.types.Scene.edit_dict_name = BoolProperty(default=True)

    bpy.types.Scene.current_tool_text = StringProperty(default="BlockMesh")
    bpy.types.Scene.meshing_tool = EnumProperty(items = [("BlockMesh", "BlockMesh", ""),
                                                         ("SnappyHexMesh", "SnappyHexMesh", "")],
                                                default = "BlockMesh",
                                                update = update_current_tool_text_1)

    bpy.types.Scene.solution_tools = EnumProperty(items = [('Solution Modeling', 'Solution Modeling', ''),
                                                          ('Post-Processing', 'Post-Processing', '')],
                                                  update = update_current_tool_text_2)

    bpy.types.Scene.spawn_type = EnumProperty(items = [("Grid", "Grid", ""),
                                                       ("3D Cursor", "3D Cursor", ""),
                                                       ("Center", "Center", "")],
                                              default = "Grid")

    bpy.types.Scene.prompt_meshing_tool = EnumProperty(default = {'BlockMesh'},
                                                       items = [('BlockMesh', 'BlockMesh', ''),
                                                                ('SnappyHexMesh', 'SnappyHexMesh', '')],
                                                       options={"ENUM_FLAG"},
                                                       update=update_mesh_dict_names)

    bpy.types.Scene.mainpanel_categories = EnumProperty(items = [('Explore', 'Explore', ''),
                                                                 ('Geometry', 'Geometry', ''),
                                                                 ('Edges', 'Edges', ''),
                                                                 ('Step Controls', 'Steps Controls', ''),
                                                                 ('Visualize', 'Visualize', ''),
                                                                 ('Run', 'Run', '')],
                                                        default = 'Explore')

    bpy.types.Scene.cellShapes = EnumProperty(items = [('Hexahedron', 'Hexahedron', ''),
                                                       ('Wedge (Experimental)', 'Wedge (Experimental)', ''),
                                                       ('Prism', 'Prism', ''),
                                                       ('Pyramid (Experimental)', 'Pyramid (Experimental)', ''),
                                                       ('Tetrahedron (Experimental)', 'Tetrahedron (Experimental)', ''),
                                                       ('Tetrahedral wedge (Experimental)', 'Tetrahedral wedge (Experimental)', '')],
                                              default = 'Hexahedron',
                                              description = "Cell Shape Types")

    bpy.types.Scene.cellShape_units = IntProperty(min=1,
                                                  max=50,
                                                  default=1)

    bpy.types.Scene.bm_dict_name = StringProperty(default="blockMeshDict")
    bpy.types.Scene.shm_dict_name = StringProperty()

    bpy.types.Scene.pref_pointer = bpy.props.PointerProperty(type=VNT_user_preferences_collection)

    bpy.types.Scene.mfile_item_ptr = bpy.props.PointerProperty(type=fileitemproperties)
    bpy.types.Scene.mfile_item = CollectionProperty(type=fileitemproperties)
    bpy.types.Scene.mfile_item_index = IntProperty(update=update_uicategory_mode)

    bpy.types.Scene.tut_item_ptr = bpy.props.PointerProperty(type=tutorialitemproperties)
    bpy.types.Scene.tut_item = CollectionProperty(type=tutorialitemproperties)
    bpy.types.Scene.tut_item_index = IntProperty()

    bpy.types.Scene.rec_item_ptr = bpy.props.PointerProperty(type=recent_item_properties)
    bpy.types.Scene.rec_item = CollectionProperty(type=recent_item_properties)
    bpy.types.Scene.rec_item_index = IntProperty()

    bpy.types.Scene.mesh_dict_path = StringProperty()
    bpy.types.Scene.row_en = BoolProperty(default=True)

    bpy.types.Scene.cell_x = IntProperty(name = "X: ",
                                         description = "Select Number of cells along X",
                                         min = 1,
                                         max = 1000,
                                         default = 1,
                                         update = update_cellxyz)

    bpy.types.Scene.cell_y = IntProperty(name = "Y: ",
                                         description = "Select Number of cells along Y",
                                         min = 1,
                                         max = 1000,
                                         default = 1,
                                         update = update_cellxyz)

    bpy.types.Scene.cell_z = IntProperty(name = "Z: ",
                                         description = "Select Number of cells along Z",
                                         min = 1,
                                         max = 1000,
                                         default = 1,
                                         update = update_cellxyz)

    bpy.types.Scene.ctm = FloatProperty(name = "Convert To Meters:",
                                        description = "Set converttoMeters parameter of Blockmeshdict",
                                        min = 0.001,
                                        max = 100.0,
                                        default = 0.1)

    bpy.types.Scene.transform = BoolProperty(default=False)

    bpy.types.Scene.transformation_methods = EnumProperty(items = [('Move', 'Move (G)', 'Shortcut: G'),
                                                                   ('Rotate', 'Rotate (R)', 'Shortcut: R'),
                                                                   ('Scale', 'Scale (S)', 'Shortcut: S')],
                                                          default = 'Move')

    bpy.types.Scene.snapping = BoolProperty(default=False, update=update_snapping)

    bpy.types.Scene.snapping_methods = EnumProperty(items = [('VERTEX', 'Vertex', ''),
                                                             ('EDGE', 'Edge', ''),
                                                             ('FACE', 'Face', '')],
                                                    default = 'VERTEX',
                                                    update = update_snapping_method)

    bpy.types.Scene.simblk = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.simblk_index = IntProperty()

    bpy.types.Scene.bcustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.bcustom_index = IntProperty()

    bpy.types.Scene.vcustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.vcustom_index = IntProperty()

    bpy.types.Scene.fcustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.fcustom_index = IntProperty()

    bpy.types.Scene.edge_control_methods = EnumProperty(items = [('IP', 'Interpolation Points', ''),
                                                                 ('AA', 'Axis angle', '')],
                                                        default = 'IP')

    bpy.types.Scene.curve_type = EnumProperty(items = [('ARC', 'Arc', '', "SPHERECURVE", 0),
                                                      ('POLYLINE', 'Polyline', '', "LINCURVE", 1),
                                                      ('SPLINE', 'Spline', '', "SMOOTHCURVE", 2),
                                                      ('BSPLINE', 'Bspline', '', "ROOTCURVE", 3)],
                                             default = 'ARC')

    bpy.types.Scene.cnt = IntProperty()

    bpy.types.Scene.mode = EnumProperty(items = [('OBJECT', 'Object Mode', '', 'OBJECT_DATAMODE', 1),
                                                 ('VERT', 'Vertex Mode', '', 'VERTEXSEL', 2),
                                                 ('FACE', 'Face Mode', '', 'FACESEL', 3),
                                                 ('EDGE', 'Edge Mode', '', 'EDGESEL', 4)],
                                        default = 'OBJECT',
                                        update=update_mode)

    bpy.types.Scene.bdclist = EnumProperty(name= "",
                                           description= "Select Boundary Condition",
                                           items= [('wedge', "wedge", ""),
                                                   ('empty', "empty", ""),
                                                   ('symmetryPlane', "symmetryPlane", ""),
                                                   ('wall', "wall", ""),
                                                   ('patch', "patch", "")])

    bpy.types.Scene.face_name = PointerProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.facedes = PointerProperty(type=VNT_global_properties_collection)

    bpy.types.Scene.acustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.acustom_index = IntProperty()

    bpy.types.Scene.pcustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.pcustom_index = IntProperty()

    bpy.types.Scene.scustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.scustom_index = IntProperty()

    bpy.types.Scene.bscustom = CollectionProperty(type=VNT_global_properties_collection)
    bpy.types.Scene.bscustom_index = IntProperty()

    bpy.types.Scene.ipcnt = IntProperty(name = "IP: ",
                                        description = "Select Number of Interpolation Points",
                                        min = 1,
                                        max = 30,
                                        default = 1)

    bpy.types.Scene.edgelist = EnumProperty(description= "Type of Edge (pre-defined)",
                                            items= [('arc', "arc", "", "SPHERECURVE", 0),
                                                    ('polyLine', "polyLine", "", "LINCURVE", 1),
                                                    ('spline', "spline", "", "SMOOTHCURVE", 2),
                                                    ('BSpline', "BSpline", "", "ROOTCURVE", 3)])

    bpy.types.Scene.face_sel_mode = BoolProperty(default=False, update=update_face_mode)

    bpy.types.Scene.statistics = BoolProperty(default=False)

    bpy.types.Scene.bfc = BoolProperty(default=False,
                                       description="Backface Culling")

    bpy.types.Scene.xray = BoolProperty(default=False,
                                        description="X ray mode")

    bpy.types.Scene.xray_opacity = FloatProperty(name="X-ray opacity",
                                                 description="X-ray opacity",
                                                 min = 0.0,
                                                 max = 1.0,
                                                 default = 0.5)

    bpy.types.Scene.geo_params = EnumProperty(description="Geometry parameters",
                                              items =[('Center', 'Center', ''),
                                                      ('Orientation', 'Orientation', ''),
                                                      ('Outline', 'Outline', '')],
                                              default={"Center", "Orientation", "Outline"},
                                              options={"ENUM_FLAG"})

    bpy.types.Scene.outline_color = FloatVectorProperty(name = "Outline Color",
                                                        subtype = "COLOR",
                                                        size = 4,
                                                        min = 0.0,
                                                        max = 1.0,
                                                        default = (0.0,0.5,0.0,1.0))

    bpy.types.Scene.shading = EnumProperty(description = "Geometry Shading",
                                           items = [('Solid', 'Solid', ''),
                                                    ('Wire', 'Wire', '')])

    bpy.types.Scene.wire_opacity = FloatProperty(name="Wire opacity",
                                                 description="Wire opacity",
                                                 min = 0.0,
                                                 max = 1.0,
                                                 default = 0.5)

    bpy.types.Scene.enable_vert_vis = BoolProperty(name = "")
    bpy.types.Scene.enable_edge_vis = BoolProperty(name = "")

    bpy.types.Scene.vert_order = BoolProperty(name = "")

    bpy.types.Scene.vert_props = EnumProperty(description="Vertex visualization properties",
                                              items =[('Indices', 'Indices', ''),
                                                      ('Coordinates', 'Coordinates', '')],
                                              default={"Indices"},
                                              options={"ENUM_FLAG"})

    bpy.types.Scene.vert_source = EnumProperty(description="Vertex visualization properties",
                                               items =[('Geometry', 'Geometry', ''),
                                                       ('blockmeshdict', 'blockmeshdict', '')],
                                               default="Geometry")

    bpy.types.Scene.vert_text_size = IntProperty(name = "Text Size:",
                                                 description = "Select Size of Vertex Info Text being Displayed",
                                                 min = 6,
                                                 max = 100,
                                                 default = 40)

    bpy.types.Scene.vert_text_color = FloatVectorProperty(name = "Text Color",
                                                          subtype = "COLOR",
                                                          size = 4,
                                                          min = 0.0,
                                                          max = 1.0,
                                                          default = (0.0,0.0,1.0,1.0))

    bpy.types.Scene.active_projects = EnumProperty(description="horizontally placed dynamic list of active projects in the project manager.",
                                                   items=get_active_projects)

    bpy.app.handlers.load_factory_startup_post.append(add_tutorials_to_scene)
    bpy.app.handlers.load_factory_startup_post.append(add_recents_to_scene)

def unregister():

    for cls in reversed(classes):
        unregister_class(cls)

    unregister_custom_icon("venturial_logo", "/venturial/icons/custom_icons/venturial_logo.png")
    unregister_custom_icon("fossee_logo", "/venturial/icons/custom_icons/fossee_logo.png")
    unregister_custom_icon("new_mesh_file_2", "/venturial/icons/custom_icons/new_mesh_file_2.png")
    unregister_custom_icon("build_mesh_2", "/venturial/icons/custom_icons/build_mesh_2.png")
    unregister_custom_icon("warning_sign_1", "/venturial/icons/custom_icons/warning_sign_1.png")
    unregister_custom_icon("file-browser-2", "/venturial/icons/custom_icons/file-browser-2.png")

    del bpy.types.Scene.ui_category
    del bpy.types.Scene.tool_type
    del bpy.types.Scene.prompt_meshing_tool
    del bpy.types.Scene.scene_blockmesh_panel_categories
    del bpy.types.Scene.test_enum
    del bpy.types.Scene.cellShapes
    del bpy.types.Scene.cellShape_units
    del bpy.types.Scene.mfile_item_ptr
    del bpy.types.Scene.mfile_item
    del bpy.types.Scene.mfile_item_index
    del bpy.types.Scene.mesh_dict_name
    del bpy.types.Scene.mesh_dict_path
    del bpy.types.Scene.row_en
    del bpy.types.Scene.cell_x
    del bpy.types.Scene.cell_y
    del bpy.types.Scene.cell_z
    del bpy.types.Scene.ctm
    del bpy.types.Scene.transform
    del bpy.types.Scene.transformation_methods
    del bpy.types.Scene.snapping
    del bpy.types.Scene.snapping_methods
    del bpy.types.Scene.simblk
    del bpy.types.Scene.simblk_index
    del bpy.types.Scene.bcustom
    del bpy.types.Scene.bcustom_index
    del bpy.types.Scene.vcustom
    del bpy.types.Scene.vcustom_index
    del bpy.types.Scene.fcustom
    del bpy.types.Scene.fcustom_index
    del bpy.types.Scene.cnt
    del bpy.types.Scene.mode
    del bpy.types.Scene.bdclist
    del bpy.types.Scene.face_name
    del bpy.types.Scene.facedes
    del bpy.types.Scene.acustom
    del bpy.types.Scene.acustom_index
    del bpy.types.Scene.pcustom
    del bpy.types.Scene.pcustom_index
    del bpy.types.Scene.scustom
    del bpy.types.Scene.scustom_index
    del bpy.types.Scene.bscustom
    del bpy.types.Scene.bscustom_index
    del bpy.types.Scene.ipcnt
    del bpy.types.Scene.edgelist
    del bpy.types.Scene.face_sel_mode


from dash import Dash, html, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.express as px
from analise import dados_input_validacao, grafico_potencia, grafico_corrente

dados_df = dados_input_validacao()

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Div(className='row', children="Energia, Corrente, Potência e as fases respectivas",
             style={'textAlign': 'center', 'color': 'blue', 'fontSize': 30}),

    html.Div(className='row', children=[
        dcc.RadioItems(
            options=[
                {'label':'Potência Ativa','value':'Potência Ativa'},
                {'label':'Potência Reativa','value':'Potência Reativa'},
                {'label':'Fator de Potência','value':'Fator de Potência'},
            ],
            value='Potência Ativa',
            inline=True,
            id='control-radio-items')
        ]),

    html.Div(className='row', children=[
        dcc.RadioItems(
            options=['Valor Absoluto','Valor Relativo',],
            value='Valor Absoluto',
            inline=True,
            id='control-radio-items-value')
        ]),

    html.Div(className='row', children=[
        html.Div(className='six columns', children=[
           dash_table.DataTable(data=dados_df.to_dict('records'),
                                 page_size=23,
                                 style_table={'overflowX':'auto'})
        ]),

       html.Div(className='six columns', children=[
           html.Div(className='five row', children=[
               dcc.Graph(figure={}, id='control-graph'),
           ]),
           html.Div(className='two row', children=[
                dcc.Graph(figure=grafico_corrente(dados_df))
           ])
       ])
    ])
])

@callback(
    Output(component_id='control-graph', component_property='figure'),
    [
        Input(component_id='control-radio-items', component_property='value'),
        Input(component_id='control-radio-items-value', component_property='value')
    ]
)

def atualiza_grafico(col_chosen, value_type):
    fig = grafico_potencia(dados_df, col_chosen, value_type)
    return fig

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


#!/usr/bin/python3

from Crypto import Random
from Crypto.Cipher import AES
import os
import datetime
import time

def pad(s):
    return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

def encrypt(message, key, key_size=256):
    message = pad(message)
    iv = os.urandom(AES.block_size)
    #iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return iv + cipher.encrypt(message)

def decrypt(ciphertext, key):
    iv = ciphertext[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext[AES.block_size:])
    return plaintext.rstrip(b"\0")

def encrypt_file(file_name, file_name_out, key):
    with open(file_name, 'rb') as fo:
        plaintext = fo.read()
    enc = encrypt(plaintext, key)
    with open(file_name_out, 'wb') as fo:
        fo.write(enc)

def decrypt_file(file_name, file_name_out, key):
    with open(file_name, 'rb') as fo:
        ciphertext = fo.read()
    dec = decrypt(ciphertext, key)
    with open(file_name_out, 'wb') as fo:
        fo.write(dec)

if __name__ == '__main__':

    #key = b'\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18'
    key = os.urandom(32)
    encrypt_file('original.txt', "out123.txt", key)
    decrypt_file("out123.txt", 'original.txt.enc', key)
    print("urandom: %s" % key)
    a = datetime.datetime.now()
    aa = time.time()
    time.sleep(5.8)
    b = datetime.datetime.now()
    bb = time.time()
    print("b-a: %s" % (b-a).total_seconds())
    print("bb-aa: %s" % (bb-aa))


from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from pynput.keyboard import Key, Listener
from bs4 import BeautifulSoup
import time

typingTestUrl = "https://www.typingtest.com/test.html?textfile=words.txt&minutes=1&mode=words&result_url=result.html"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(typingTestUrl)

def on_release(key):
    if key == Key.f2:
        start()

def start():
    try:
        content = driver.find_element_by_class_name("test-text-area test-text-area-font")
        print(content)
    except:
        print(Exception)

with Listener(on_release=on_release) as listener:
    listener.join()


import re
import json
from collections import defaultdict
from difflib import SequenceMatcher
import spacy
from imdb import Cinemagoer
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')

"""
    Global loading phase, nlp spacy model and keywords
"""
nlp = spacy.load('en_core_web_md')
sia = SentimentIntensityAnalyzer()
HOST_WORDS = {'host'}
NOMINATION_WORDS = {'nom'}
PRESENTATION_WORDS = {'present','introduce','announce'}
CONGRATS_WORDS = {'congraluations', 'congrats', 'wins', 'winner', 'won', 'award'}
PEOPLE_WORDS = {'actor', 'actress', 'director', 'score', 'cecil', 'screenplay'}

def type_check(award):
  if tweet_has_words(award, PEOPLE_WORDS):
    return 'person'
  else:
    return 'title'

def extract_award_name(tweet):

  best_match = re.search('best',tweet, re.I)
  best_start = best_match.span()[0]

  tweet = tweet[best_start:]

  # Punctuation Termination Condition
  matches = re.search(r'[.!?#"\'(@:]', tweet)
  if matches:
    best_end = matches.start()
    tweet = tweet[:best_end]

  # Preposition Termination Condition
  # checkin if the last word is in,for, to
  test_chunk = [x for x in tweet.split(' ') if x]
  if test_chunk[-1] in ["in", "for", "to"]:
      test_chunk = test_chunk[:-1]
      tweet = ' '.join(test_chunk)

  result = tweet.lower()
  return result

# TODO:
# This Can be better improved
# We can add rules when iterating through the labels
def extract_potential_names(tweet):
  names = []
  doc = nlp(tweet)
  for ent in doc.ents:
    if ent.label_ == 'PERSON':
      names.append(ent.text)
  names = [re.fullmatch(r"[A-Z][a-z]+,?\s+(?:[A-Z][a-z]*\.?\s*)?[A-Z][a-z]+", x) for x in names]
  names = [x.group(0) for x in names if x]
  return names

def extract_potential_titles(tweet):

  results = []

  # 1. Using #
  if '#' in tweet:

    tweet = tweet+' '
    start = tweet.index('#')
    end = tweet.index(' ', start)
    target_name = tweet[start+1:end]

    if target_name[:6] not in ['Golden', 'golden', "GOLDEN"]:
      results.append(target_name)

  return results

def epn_2(tweet):
  names = []
  doc = nlp(tweet)
  for ent in doc.ents:
    if ent.label_ == 'PERSON':
      names.append(ent.text)

  # Clean posession form ('s)
  names = [name.replace("'s", '') for name in names]

  # Clean each name by dropping non-alphabet char
  regex = re.compile('[^a-zA-Z ]')
  names = [regex.sub("", name) for name in names]

  return names

def token_match_score(tweet, award):

  tweet_token = tweet.lower().split()
  award_token = award.lower().translate(',-').split()

  matches = [x for x in award_token if x in tweet_token]
  count = len(matches)
  award_score = count/float(len(award.split()))

  return award_score

def best_matching_award(tweet, awards):

  # Score Matrix: [(award, score)]
  candidates = [[award, SequenceMatcher(None, tweet.lower(), award.lower()).ratio()] for award in awards]
  candidates = sorted(candidates, key = lambda x: x[1], reverse=True)
  top_5 = [candidate[0] for candidate in candidates][:5]

  # Token Matching
  final_rounds = [[award, token_match_score(tweet, award)] for award in top_5]
  final_rounds = sorted(final_rounds, key = lambda x: x[1], reverse=True)
  best_award = final_rounds[0][0]

  return best_award

def tweet_has_word(tweet, word):
  return re.search(word, tweet, re.I) is not None

def tweet_has_words(tweet, words):
  for word in words:
    if tweet_has_word(tweet, word):
      return True
  return False

def imdb_match_person(name):
  ia = Cinemagoer()
  people = ia.search_person(name)
  if people:
    return people[0]['name']

#TODO: Change it to adaptive threshold
def organize_freq_threshold(frequency, threshold):

  candidates = []
  for name, freq in frequency.items():
    candidates.append([name, freq])

  sorted_candidates = sorted(candidates, key=lambda x: x[1], reverse = True)

  results = [sorted_candidates[0][0]]

  for idx, candidate in enumerate(sorted_candidates):

    if idx == 0:
      continue

    prev_name, prev_vote = sorted_candidates[idx-1]
    curr_name, curr_vote = candidate

    if (prev_vote - curr_vote) > threshold:
      break
    else:
      results.append(curr_name)

  return results

def load_tweets(year):
	f = open('sample_data/gg{}.json'.format(year))
	tweets_json = json.load(f)
	tweets = []
	for tweet in tweets_json:
		if tweet['text'].isascii():
			tweets.append(tweet['text'])
	unique_tweets = list(set(tweets))
	return unique_tweets

# Mining Awards
def mine_awards(tweets):

  awards = []
  for tweet in tweets:
    if tweet_has_words(tweet, CONGRATS_WORDS):
      if tweet_has_words(tweet, ['for Best']):
        award = extract_award_name(tweet)
        awards.append(award)

  # Need to do
  awards = list(set(awards))
  return awards

def mine_winners(tweets, awards):

  # Tweet Looping, faster
  awardNwinner2freq = {}
  for tweet in tweets:

    if tweet_has_words(tweet, CONGRATS_WORDS):
      if tweet_has_words(tweet, ['for Best', 'award']):

        best_award = best_matching_award(tweet, awards)
        award_type = type_check(best_award)

        if award_type == 'person':
          pot_names = extract_potential_names(tweet)
        else:
          pot_names = extract_potential_titles(tweet)

        for name in pot_names:
          # unique_names.append(name)
          pair = (best_award, name)
          if pair not in awardNwinner2freq:
            awardNwinner2freq[pair] = 1
          else:
            awardNwinner2freq[pair] += 1

  award2name2freq = defaultdict(dict)
  for pair,freq in awardNwinner2freq.items():
    award, name = pair
    award2name2freq[award][name] = freq

  award2winners = defaultdict(list)
  for award in awards:
    award2winners[award] = []

  for award, stats in award2name2freq.items():
    top_selection = organize_freq_threshold(stats, 1)
    award2winners[award] = top_selection[0]

  return award2winners

# Return {award -> [presenters]}
# 34 seconds for all awards
# Mid-Low Accuracy
# TODO: find tune accuracy in KEYWORDS as well as AGGREGATION phase
def mine_presenters(tweets, awards):

  # Tweet-Major: somehow faster than award major
  unique_names = []
  awardNpresenter2freq = {}
  for tweet in tweets:
    if tweet_has_words(tweet, PRESENTATION_WORDS):
      if tweet_has_words(tweet, ["Best", "Award"]):

        best_award = best_matching_award(tweet, awards)
        pot_names = epn_2(tweet)

        for name in pot_names:
          unique_names.append(name)
          pair = (best_award, name)
          if pair not in awardNpresenter2freq:
            awardNpresenter2freq[pair] = 1
          else:
            awardNpresenter2freq[pair] += 1

  unique_names = list(set(unique_names))
  unique_names = [imdb_match_person(name) for name in unique_names]
  unique_names = [x for x in unique_names if x]

  award2name2freq = defaultdict(dict)
  for pair,freq in awardNpresenter2freq.items():
    award, name = pair
    best_name = best_matching_award(name, unique_names)
    award2name2freq[award][best_name] = freq

  award2presenters = defaultdict(list)
  for award in awards:
    award2presenters[award] = []

  for award, stats in award2name2freq.items():
    top_selection = organize_freq_threshold(stats, 1)
    award2presenters[award] = top_selection[:2]

  return award2presenters

def mine_nominees(tweets,awards):

  # unique_names = []
  award_nominees_freq={}
  for tweet in tweets:
    if tweet_has_words(tweet, NOMINATION_WORDS):
      if tweet_has_words(tweet, ["Best", "Award"]):
        best_award = best_matching_award(tweet, awards)

        award_type = type_check(best_award)
        if award_type == 'person':
          pot_names = extract_potential_names(tweet)
        else:
          pot_names = extract_potential_titles(tweet)

        for name in pot_names:
            # unique_names.append(name)
            pair = (best_award, name)
            if pair not in award_nominees_freq:
              award_nominees_freq[pair] = 1
            else:
              award_nominees_freq[pair] += 1

  # unique_names = list(set(unique_names))
  # unique_names = [imdb_match_person(name) for name in unique_names]
  # unique_names = [x for x in unique_names if x]

  award2name2freq = defaultdict(dict)
  for pair,freq in award_nominees_freq.items():
    award, name = pair
    # best_name = best_matching_award(name, unique_names)
    award2name2freq[award][name] = freq

  award2nominees = defaultdict(list)
  for award in awards:
    award2nominees[award] = []

  for award, stats in award2name2freq.items():
    top_selection = organize_freq_threshold(stats, 50)
    award2nominees[award] = top_selection[:5]

  return award2nominees

def mine_hosts(tweets):

  host2freq = defaultdict(int)
  for tweet in tweets:
    if tweet_has_words(tweet, HOST_WORDS):
      pot_names = extract_potential_names(tweet)
      for name in pot_names:
        host2freq[name] += 1

  return organize_freq_threshold(host2freq, 100)

'''
Additional Goals
'''
def get_worst_dressed(tweets):

  worst_dressed_freq = defaultdict(int)
  for tweet in tweets:
    if tweet_has_words(tweet, ['worst dress', 'worst clothes', 'worst uniform']):
        potential_name=extract_potential_names(tweet)
        for name in potential_name:
          worst_dressed_freq[name]+=1

  return organize_freq_threshold(worst_dressed_freq, 1)[0]

def get_best_dressed(tweets):

  best_dressed_freq = defaultdict(int)
  for tweet in tweets:
    if tweet_has_words(tweet, ['best dress', 'best clothes', 'best uniform']):
        potential_name=extract_potential_names(tweet)
        for name in potential_name:
          best_dressed_freq[name]+=1

  return organize_freq_threshold(best_dressed_freq, 1)[0]

def get_weird_dressed(tweets):

  weird_dressed_freq = defaultdict(int)
  for tweet in tweets:
    if tweet_has_words(tweet, ['weird dress', 'weird clothes', 'weird uniform']):
        potential_name=extract_potential_names(tweet)
        for name in potential_name:
          weird_dressed_freq[name]+=1

  return organize_freq_threshold(weird_dressed_freq, 1)[0]

def mine_red_carpet(tweets):
  red_carpet = {}
  red_carpet['Best Dressed'] = get_best_dressed(tweets)
  red_carpet['Worst Dressed'] = get_worst_dressed(tweets)
  red_carpet['Most Controversially Dressed'] = get_weird_dressed(tweets)
  return red_carpet

def sentimental_analysis(tweets):

  name2scores = defaultdict(list)
  for tweet in tweets:
    if tweet_has_words(tweet, CONGRATS_WORDS):
      if tweet_has_words(tweet, ['for Best', 'award']):
        for name in extract_potential_names(tweet):
          scores = sia.polarity_scores(tweet)
          name2scores[name].append(scores['compound'])

  name2average = {}
  for name, scores in name2scores.items():
    mean = sum(scores)/len(scores)
    name2average[name] = mean

  most_positive = sorted(name2average, key=name2average.get, reverse=True)[:5]
  most_negative = sorted(name2average, key=name2average.get)[:5]

  senti2tops = {}
  senti2tops['Most Positive'] = most_positive
  senti2tops['Most Negative'] = most_negative
  return senti2tops

def main():
  return

main()


class Solution(object):
    def wordPattern(self, pattern, words):
        pattern = list(pattern)
        words = words.split()
        n,m = len(pattern),len(words)
        if n!=m: return False
        dp = {}
        for i in xrange(n):
            k,v = pattern[i],words[i]
            if k in dp:
                if dp[k]!=v: return False
            else: dp[k] =v
        dp = {}
        for i in xrange(n):
            v,k = pattern[i],words[i]
            if k in dp:
                if dp[k]!=v: return False
            else: dp[k] =v
        return True


from tkinter import *
from tkinter import messagebox
import sqlite3

root = Tk()
root.title("User Form")

def save():
    # Create database or connect to an existing one
    myDB = sqlite3.connect('address_book.db')

    # Create a cursor
    dbCursor = myDB.cursor()

    # save the changes
    user_id = select_id.get()
    dbCursor.execute("""UPDATE addresses SET
                first_name = :first,
                last_name = :last,
                address = :address,
                city = :city,
                state = :state,
                zipcode = :zipcode

                WHERE oid = :oid""",
                {'first':fn_updated.get(),
                 'last':ln_updated.get(),
                 'address': address_updated.get(),
                 'city': city_updated.get(),
                 'state': state_updated.get(),
                 'zipcode': zip_updated.get(),
                 'oid': user_id
                 })

    myDB.commit()
    myDB.close()
    messagebox.showinfo("Notification", "Update Sucessfull!!")
    root2.destroy()

    # after we click save we want to close the second window that opens on update

def update():
    global root2
    root2 = Tk()
    root2.title("Update Window")

    # Create database or connect to an existing one
    myDB = sqlite3.connect('address_book.db')

    # Create a cursor
    dbCursor = myDB.cursor()

    user_id = select_id.get()
    dbCursor.execute("SELECT * FROM addresses WHERE oid = " + user_id)

    # Create our form text boxes
    # first name
    # all of these will need to be global so we can call them on the save function
    global fn_updated
    global ln_updated
    global address_updated
    global city_updated
    global state_updated
    global zip_updated

    fn_updated = Entry(root2, width=30)
    fn_updated.grid(row=0, column=1, padx=20, pady=(10, 0))

    # last name
    ln_updated = Entry(root2, width=30)
    ln_updated.grid(row=1, column=1, padx=20)

    # address
    address_updated = Entry(root2, width=30)
    address_updated.grid(row=2, column=1, padx=20)

    # city
    city_updated = Entry(root2, width=30)
    city_updated.grid(row=3, column=1, padx=20)

    # state
    state_updated = Entry(root2, width=30)
    state_updated.grid(row=4, column=1, padx=20)

    # zipcode
    zip_updated = Entry(root2, width=30)
    zip_updated.grid(row=5, column=1, padx=20)

    # we create labels now, so we know what are we adding
    # labels domt affect anything related to what is going inside the text boxes so theres no harm in
    # recycling these
    # first name
    fn_label = Label(root2, text="Insert First Name")
    fn_label.grid(row=0, column=0, pady=(10, 0))

    # last name
    ln_label = Label(root2, text="Insert Last Name")
    ln_label.grid(row=1, column=0)

    # address
    address_label = Label(root2, text="Insert Address")
    address_label.grid(row=2, column=0)

    # city
    city_label = Label(root2, text="Insert City of Residence")
    city_label.grid(row=3, column=0)

    # state
    state_label = Label(root2, text="Insert State of Residence")
    state_label.grid(row=4, column=0)

    # zipcode
    zip_label = Label(root2, text="Insert Zipcode")
    zip_label.grid(row=5, column=0)

    users = dbCursor.fetchall()
    for user in users:
        fn_updated.insert(0, user[0])
        ln_updated.insert(0, user[1])
        address_updated.insert(0, user[2])
        city_updated.insert(0, user[3])
        state_updated.insert(0, user[4])
        zip_updated.insert(0, user[5])

    # we need a save button
    save_button = Button(root2, text="Save Changes", command=save)
    save_button.grid(row=6, column=0, columnspan=2, pady=10, padx=10, ipadx=15)

    myDB.commit()
    myDB.close()

# Create our submit function that will allows us to insert users information in our address book

def submit():
    # Create database or connect to an existing one
    myDB = sqlite3.connect('address_book.db')
    # Create a cursor
    dbCursor = myDB.cursor()

    dbCursor.execute("INSERT INTO addresses VALUES (:fn, :ln, :address, :city, :state, :zip)",
      # same as our text boxes "names"
                        {
                            'fn': fn.get(),
                            'ln': ln.get(),
                            'address': address.get(),
                            'city': city.get(),
                            'state': state.get(),
                            'zip': zip.get()
                        })

    myDB.commit()
    myDB.close()
    # we need to know if we succeeded when we add new information to our address book
    messagebox.showinfo("Notification", "Submission Successful!")

    # Clear the textboxes each time we click submit
    fn.delete(0, END)
    ln.delete(0, END)
    address.delete(0, END)
    city.delete(0, END)
    state.delete(0, END)
    zip.delete(0, END)

def query():
    # Create database or connect to an existing one
    myDB = sqlite3.connect('address_book.db')

    # Create a cursor
    dbCursor = myDB.cursor()

    dbCursor.execute("SELECT *,oid FROM addresses")
    users = dbCursor.fetchall()

    showUsers = ''
    for user in users:
        showUsers += str(user[0]) + " " + str(user[1]) + "\t" + str(user[-1]) + "\n"

    query_label = Label(root,  text = showUsers)
    query_label.grid(row = 12, column = 0, columnspan = 2)

    myDB.commit()
    myDB.close()

# eventualy we need to delete users from our database and thats what the following function will do

def delete():
    # Create database or connect to an existing one
    myDB = sqlite3.connect('address_book.db')

    # Create a cursor
    dbCursor = myDB.cursor()

    dbCursor.execute("DELETE FROM addresses WHERE oid= " + select_id.get())
    messagebox.showinfo("Notification", "User successfully removed!")

    myDB.commit()
    myDB.close()
    return

# Create our form text boxes
# first name
fn = Entry(root, width = 30)
fn.grid(row = 0, column = 1, padx = 20, pady=(10,0))

# last name
ln = Entry(root, width = 30)
ln.grid(row = 1, column = 1, padx = 20)

# address
address = Entry(root, width = 30)
address.grid(row = 2, column = 1, padx = 20)

# city
city = Entry(root, width = 30)
city.grid(row = 3, column = 1, padx = 20 )

# state
state = Entry(root, width = 30)
state.grid(row = 4, column = 1, padx = 20)

# zipcode
zip = Entry(root, width = 30)
zip.grid(row = 5, column = 1, padx = 20)

# select Id box
select_id = Entry(root, width = 30)
select_id.grid(row = 9, column=1, padx = 20)

# we create labels now, so we know what are we adding

# first name
fn_label = Label(root, text = "Insert First Name")
fn_label.grid(row = 0, column =0, pady=(10,0))

# last name
ln_label = Label(root, text = "Insert Last Name")
ln_label.grid(row = 1, column =0)

# address
address_label = Label(root, text = "Insert Address")
address_label.grid(row = 2, column =0)

# city
city_label = Label(root, text = "Insert City of Residence")
city_label.grid(row = 3, column =0)

# state
state_label = Label(root, text = "Insert State of Residence")
state_label.grid(row = 4, column =0)

# zipcode
zip_label = Label(root, text = "Insert Zipcode")
zip_label.grid(row = 5, column =0)

# Insert ID
select_id_label = Label(root, text = "Insert User ID")
select_id_label.grid(row = 9, column = 0)

# we need buttons
# this button adds entries to the database
submit_button = Button(root, text = "Add a user to the database", command = submit)
submit_button.grid(row = 6, column = 0, columnspan = 2, pady = 10, padx = 10, ipadx = 10)

# query button will shows whats in the database

query_button = Button(root, text = "Show users", command = query)
query_button.grid(row = 7, column = 0, columnspan = 2, pady = 10, padx = 10, ipadx = 50)

# delete button
delete_button = Button(root, text = "Delete the selected user", command = delete)
delete_button.grid(row = 10, column = 0, columnspan = 2, pady = 10, padx = 10, ipadx = 20)

# update button
update_button = Button(root, text = "Update users information", command = update)
update_button.grid(row = 11, column = 0, columnspan = 2, pady = 10, padx = 10, ipadx = 15)

root.mainloop()


from sys import argv
import sqlite3 as sql
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import List, Union

from pandas import DataFrame, concat, read_csv

@dataclass
class DatMetaData:
    lat: float = 0
    long: float = 0
    elev_m: float = 0
    local_timezone: str = ""
    serial_num: str = ""
    hardware_id: str = ""

def pair_dat_csv_files(data_root: Path) -> List[Union[Path, Path]]:
    csv_paths = list(data_root.glob("**/*.csv"))
    dat_paths = list(data_root.glob("**/*.dat"))
    dat_dict = {path.name: path for path in dat_paths}
    pairs = []
    for path in csv_paths:
        csv_dat_key = path.name[:-4] + ".dat"
        if csv_dat_key in dat_dict:
            pairs.append((path, dat_dict[csv_dat_key]))
    return pairs

def parse_dat_comments(dat_file: Path) -> DatMetaData:
    print("Processing... ", dat_file)
    keys = {
        "# Local timezone": 0,
        "# SQM serial number": 1,
        "# SQM hardware identity": 2,
        "# Position (lat, lon, elev(m))": 3,
    }
    meta = DatMetaData()
    with open(dat_file) as f:
        lines = f.readlines()
    for line in [line for line in lines if line[0] == "#"]:
        i = line.find(":")
        key = line[:i]
        value = line[i + 1 :].strip()
        if key in keys:
            if keys[key] == 0:
                meta.local_timezone = value
            elif keys[key] == 1:
                meta.serial_num = value
            elif keys[key] == 2:
                meta.hardware_id = value
            elif keys[key] == 3:
                values = [float(s.strip()) for s in value.split(",")]
                meta.lat = values[0]
                meta.long = values[1]
                meta.elev_m = values[2]
    return meta

def clean_csv(csv_file: Path) -> None:
    with open(csv_file, "r+") as f:
        content = f.read()
        f.seek(0)
        content = content.replace(";", ",").replace(",\n", "\n")
        f.write(content)
        f.truncate()

def csv_to_df(csv_file: Path) -> DataFrame:
    print("Processing... ", csv_file)
    clean_csv(csv_file)
    new_data = {"location": csv_file.name[: csv_file.name.find("_")]}
    df = read_csv(csv_file)
    return concat([df, DataFrame(new_data, index=df.index)], axis=1)

def build_df(meta: DatMetaData, df: DataFrame) -> DataFrame:
    new_df = concat([df, DataFrame(asdict(meta), index=df.index)], axis=1)
    new_df.columns = new_df.columns.str.strip()
    new_df.rename(
        columns={
            "UTC Date & Time": "utc_time",
            "Local Date & Time": "local_time",
            "Temperature": "temperature",
            "Voltage": "voltage",
            "MSAS": "msas",
            "Record type": "record_type",
            "MoonPhaseDeg": "moon_phase_deg",
            "MoonElevDeg": "moon_elev_deg",
            "MoonIllum%": "moon_illum_perc",
            "SunElevDeg": "sun_elev_deg",
        },
        inplace=True,
    )
    return new_df

def insert_df_to_db(df: DataFrame, con: sql.Connection) -> None:
    df.to_sql("sensor_data", con, if_exists="replace", index=False)

def build_db() -> sql.Connection:
    con = sql.connect("Data.db")
    cur = con.cursor()
    cur.execute(
        """
            CREATE TABLE IF NOT EXISTS sensor_data
            (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
            utc_time TIMESTAMP NOT NULL,
            local_time TIMESTAMP NOT NULL,
            location TEXT NOT NULL,
            temperature FLOAT NOT NULL,
            voltage FLOAT NOT NULL,
            msas FLOAT NOT NULL,
            record_type INT NOT NULL,
            moon_phase_deg FLOAT NOT NULL,
            moon_elev_deg FLOAT NOT NULL,
            moon_illum_perc FLOAT NOT NULL,
            sun_elev_deg FLOAT NOT NULL,
            lat FLOAT NOT NULL,
            long FLOAT NOT NULL,
            elev_m FLOAT NOT NULL,
            local_timezone TIMESTAMP NOT NULL,
            serial_num VARCHAR(10) NOT NULL,
            hardware_id VARCHAR(10) NOT NULL)
            """
    )
    return con

def insert_data_to_db(root_dir: Path, con: sql.Connection) -> None:
    paths = pair_dat_csv_files(root_dir)
    data = (
        build_df(parse_dat_comments(dat_path), csv_to_df(csv_path))
        for csv_path, dat_path in paths
    )
    insert_df_to_db(concat(data), con)

if __name__ == "__main__":
    if len(argv) <= 1:
        print("Usage: python main.py <data directory>")
        exit()
    insert_data_to_db(Path(argv[1]), build_db())


import torch
from torch import nn

class transformer(nn.Module):
    def __init__(self,
                 input_size,
                 hidden_size,
                 num_heads,):
        super(transformer, self).__init__()
        self.linear = nn.Linear(input_size, hidden_size)
        self.encoder_layer = nn.TransformerEncoderLayer(d_model=hidden_size, nhead=num_heads, batch_first=True)
        self.predictor = nn.Sequential(
            nn.Linear(hidden_size, 1),
            # nn.Sigmoid()
        )

    def forward(self,
                inputs,):
        x = self.linear(inputs)
        x = self.encoder_layer(x)
        return self.predictor(x[:, -1, :].squeeze()).squeeze()


import tensorflow.keras as keras
from tensorflow.keras import backend as K
from tensorflow.keras.datasets import mnist
import numpy as np
def load_mnist(task):
    """
    Imports the MNIST dataset from the tf datasets
    """

    num_classes = 10
    (x_train, lbl_train), (x_test, lbl_test) = mnist.load_data()
    x_train = np.pad(x_train,((0,0),(2,2),(2,2))) #padding to make images 32x32 and not 28x28
    x_test = np.pad(x_test,((0,0),(2,2),(2,2)))

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    ## normalising to unit variance
    sigma=np.std(x_train)
    x_train /= sigma
    x_test /= sigma

    ## mean subtraction
    mu=np.mean(x_train)
    x_train -= mu
    x_test -= mu
    ## make labels into categorical classes
    y_train = keras.utils.to_categorical(lbl_train, num_classes)
    y_test = keras.utils.to_categorical(lbl_test, num_classes)

    x_train=np.expand_dims(x_train,3)
    x_test=np.expand_dims(x_test,3)

    if task==4:
        pass
    else:
        ### make mnist into 3 channels
        x_train=np.concatenate((x_train,)*3, axis=-1)
        x_test=np.concatenate((x_test,)*3, axis=-1)
    print('mean, variance', mu, sigma)
    print("---------------Load MNIST----------------")
    print('Training set', x_train.shape, y_train.shape)
    print('Test set', x_test.shape, y_test.shape)
    print("\n")
    return x_train, y_train, x_test, y_test


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 13:12:14 2018

@author: ieada
"""

import torch
import numpy as np
from torch.autograd import Variable
import math
import matplotlib.pyplot as plt

xy = np.genfromtxt('data.csv', delimiter=',', dtype='str')
[a,b] = np.shape(xy)
#print(xy[121,:])
#print(a,b,'*')
for i in range(a):
    for j in range(b):
        if xy[i,j] == 'M':
            xy[i,j] = 1
        elif xy[i,j] == 'B':
            xy[i,j] = 0
        elif xy[i,j] == 'nan':
            xy[i,j] = 0

xy = np.float32(xy)
#print(xy[121,:])
x_data = Variable(torch.from_numpy(xy[1:401,2:]))
y_data = Variable(torch.from_numpy(xy[1:401,[1]]))
x_eval = Variable(torch.from_numpy(xy[401:,2:]))
y_gtruth = Variable(torch.from_numpy(xy[401:,[1]]))

#print(np.average(y_gtruth))
#print(x_data.data.shape)
#print(y_data.data)

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.l1 = torch.nn.Linear(30,25)
        self.l2 = torch.nn.Linear(25,10)
        self.l3 = torch.nn.Linear(10,1)
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x):
        out1 = self.sigmoid(self.l1(x))
        out2 = self.sigmoid(self.l2(out1))
        y_pred = self.sigmoid(self.l3(out2))
        return y_pred

model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)

i = 0
n_epoch = 2
y = [0]*n_epoch
x = [0]*n_epoch

for epoch in range(n_epoch):
    y_pred = model(x_data)
    loss = criterion(y_pred,y_data)
    y[i] = loss.data[0]
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    x[i] = i
    i += 1

plt.plot(x, y, linewidth=2.0)
plt.show()
length = y_gtruth.data.shape[0]
y2 = [0]*length
x2 = [0]*length
pred_error = criterion(model.forward(x_eval).data[0],y_gtruth.data[0])
for i in range(length):
    print(y_gtruth.data[i,0]," ",model.forward(x_eval).data[i,0])

    #y2[i] = pred_error
    #x2[i] = i
#plt.plot(x2, y2, linewidth=2.0)
#plt.show()
#print("prediction after training:", xy[401:500,[1]], model.forward(x_prediction).data)

## The result shows that maybe we should do PCA to only choose the most relevant features


# -*- coding: utf-8 -*-
"""
Created on Fri Aug 31 09:57:31 2018

@author: Samiksha Agarwal
"""

#%%
import numpy as np
class combo:

    def ask(self,number):

        mat=[]

        for i in range(number):
                print("Enter the order of matrix"+str(i+1))
                n=int(input("row length"))
                m=int(input("column length"))
                a=self.generate_matrix(n,m)
                print(a)
                mat.append(a)
        return mat

    def generate_matrix(self,n,m):
        matrix=np.asmatrix(np.full((n,m),0))
        for i in range(n):
            for j in range(m):
                try:
                    a=int(input('Enter Number'))
                    matrix[i,j]=a
                except ValueError:
                    print("Please enter a number")
                    a=int(input('Enter Number'))
                    matrix[i,j]=a
        return matrix

    def mat_add(self,a,b):
         c=np.asmatrix(np.full(np.shape(a),0))
         if np.shape(a)==np.shape(b):
             for i in range(np.shape(a)[0]):
                 for j in range(np.shape(a)[1]):
                     c[i,j]=a[i,j]+b[i,j]
         else:
              print("Dimensions of tthe matricies are not equal")
         return c

    def mat_mult(self,a,b):
          if np.shape(a)[1]==np.shape(b)[0]:
            c=np.asmatrix(np.full((np.shape(a)[0],np.shape(b)[1]),0))
            # row of A
            for i in range(np.shape(a)[0]):
                # column of B
                 for j in range(np.shape(b)[1]):
                     # row of B
                     for k in range(np.shape(b)[0]):
                             c[i,j]+=a[i,k]*b[k,j]
            return c
          else:
              print("ValueError: shapes"+ str(np.shape(a)) + "and" + str(np.shape(b))+"not aligned")

#%%

def main():
    test_case=0
    comb=combo()
    while test_case<=20:
        try:
            add_mult=int(input("Press 1 for matrix_multiplication or Press 2 for matrix_addition"))
            if add_mult==1:
                number=int(input("Enter Number of Matrix you want"))
                try:
                    if number>1:
                        matrix=comb.ask(number)
                    else:
                        raise(ValueError)
                except ValueError:
                     print("take atleast two matrix")
                     number=int(input("Enter Number of Matrix you want"))
                     matrix=comb.ask(number)

                A=matrix[0]
                B=matrix[1]
                print("Result",comb.mat_mult(A,B))

            elif add_mult==2:
                number=int(input("Enter Number of Matrix you want"))

                matrix=comb.ask(number)
                A=matrix[0]
                B=matrix[1]
                print("Result",comb.mat_add(A,B))

            else:
                print("-----------------")
                print("Please enter either 1 or 2")
                print("-----------------")

        except ValueError:
            print("-----------------")
            print("Please enter either 1 or 2")
            print("-----------------")

        finally:
            a=input("N to Exit. Press any key to continue...")
            if a=='N':
                break
            #ask_user=input("If you want to continue press y/Y")
            #if ask_user=='y' or ask_user=='Y':
            #    print("once again")
            #else:
            #    break
        test_case+=1
#%%
def trying(number):
    b=combo()
    try:
        if number<=1:
            raise(ValueError)
        else:
            mat=[]
            print("great choice")
            for i in range(number):
                print("Enter the order of matrix"+str(i+1))
                n=int(input("row length"))
                m=int(input("column length"))
                a=b.generate_matrix(n,m)
                print(a)
                mat.append(a)
            return mat
    except ValueError:
        print("please enter atleast 2 matrix")


"""
als 算法
Run with:
  bin/spark-submit --py-files='/Users/t/python/spark-learning/src/utils.zip' \
  /Users/t/python/spark-learning/src/ml/als.py
"""
from __future__ import print_function
from pyspark.sql import Row
from pyspark.sql import SparkSession
from pyspark.ml.evaluation import RegressionEvaluator
from pyspark.ml.recommendation import ALS
from utils import constants

def load_data(spark, path=None):
    """加载数据"""
    lines = spark.read.text(path).rdd
    parts = lines.map(lambda row: row.value.split("::"))
    ratings_rdd = parts.map(lambda p: Row(userId=int(p[0]), movieId=int(p[1]),
                                         rating=float(p[2]), timestamp=int(p[3])))
    ratings = spark.createDataFrame(ratings_rdd)

    return ratings

if __name__ == "__main__":

    spark = SparkSession.builder.appName("als").getOrCreate()
    path = constants.DATA_PATH + '16.RecommenderSystems/ml-1m/ratings.dat'

    # 加载数据
    ratings = load_data(spark, path)
    train, test = ratings.randomSplit([0.8, 0.2])

    # Build the recommendation model using ALS on the train data
    # Note we set cold start strategy to 'drop' to ensure we don't get NaN evaluation metrics
    als = ALS(maxIter=10, regParam=0.01, userCol="userId", itemCol="movieId", ratingCol="rating",
              coldStartStrategy="drop")

    model = als.fit(train)

    # Evaluate the model by computing the RMSE on the test data
    predictions = model.transform(test)
    evaluator = RegressionEvaluator(metricName="rmse", labelCol="rating",
                                    predictionCol="prediction")
    rmse = evaluator.evaluate(predictions)
    print("Root-mean-square error = " + str(rmse))

    # Generate top 10 movie recommendations for each user
    user_recs = model.recommendForAllUsers(10)
    recs = user_recs.select("userId", "recommendations").collect()
    for ur in recs:
        recommendations = list(row['movieId'] for row in ur['recommendations'])
        print("userId: {}, recommendations: {}".format(ur['userId'], recommendations))
    # # Generate top 10 user recommendations for each movie
    # movieRecs = model.recommendForAllItems(10)
    #
    # # Generate top 10 movie recommendations for a specified set of users
    # users = ratings.select(als.getUserCol()).distinct().limit(3)
    # userSubsetRecs = model.recommendForUserSubset(users, 10)
    # # Generate top 10 user recommendations for a specified set of movies
    # movies = ratings.select(als.getItemCol()).distinct().limit(3)
    # movieSubSetRecs = model.recommendForItemSubset(movies, 10)
    # # $example off$
    # userRecs.show()
    # movieRecs.show()
    # userSubsetRecs.show()
    # movieSubSetRecs.show()

    spark.stop()


import os
import re

def _patch_trust_env(file):
    """
    People use proxies, but they never realize that proxy software leaves a
    global proxy pointing to itself even when the software is not running.
    In most situations we set `session.trust_env = False` in requests, but this
    does not effect the `pip` command.

    To handle untrusted user environment for good. We patch the code file in
    requests directly. Of course, the patch only effect the python env inside
    Alas.

    Returns:
        bool: If patched.
    """
    if os.path.exists(file):
        with open(file, 'r', encoding='utf-8') as f:
            content = f.read()
        if re.search('self.trust_env = True', content):
            content = re.sub('self.trust_env = True', 'self.trust_env = False', content)
            with open(file, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f'{file} trust_env patched')
        elif re.search('self.trust_env = False', content):
            print(f'{file} trust_env already patched')
        else:
            print(f'{file} trust_env not found')
    else:
        print(f'{file} trust_env no need to patch')

def patch_trust_env():
    _patch_trust_env('./toolkit/Lib/site-packages/requests/sessions.py')
    _patch_trust_env('./toolkit/Lib/site-packages/pip/_vendor/requests/sessions.py')


def launchAgent(env_name: int, model_name: str, test_mode=False, filepath=None):
    """
    :param test_mode: 에이전트를 테스트 모드로 불러와 주행시킬지를 확인하는 모드입니다. 이럴 시에 학습은 이루어지지 않으며, 주행만 이루어집니다.
    :param env_name: 불러올 환경의 이름입니다.
        1 : 미니맵 이미지를 사용하지 않은, 점 사이의 거리 계산을 한 환경입니다.
        2 : 미니맵 이미지를 사용하고, 보상을 업데이트한 모델입니다.
        다른 값(기본) : 현재 쓰는 모델입니다. 미니맵 이미지를 사용하고, 보상을 다시 업데이트한 모델입니다.
    :param model_name: 설정할 모델의 이름입니다.
        DQN : DQN 모델을 불러옵니다.
        HER : HER 모델을 불러옵니다.
        다른 값(기본) : PPO2 모델을 불러옵니다.
    :return: 마지막으로 episode를 수행한 모델을 return합니다.
    """

    from stable_baselines import DQN, HER, PPO2

    if env_name == 1:
        from Reinforcement_AI.env.a_env import KartEnv
        kart_env = KartEnv()
        policy = "MlpPolicy"
    elif env_name == 2:
        from Reinforcement_AI.env.d_image_env import DetailedMiniMapEnv as DetailedMiniMapEnv1
        kart_env = DetailedMiniMapEnv1()
        policy = "CnnPolicy"
    elif env_name == 3:
        from Reinforcement_AI.env.a_env2 import KartEnv
        kart_env = KartEnv()
        policy = "MlpPolicy"
    elif env_name == 4:
        from Reinforcement_AI.env.a_env3 import KartEnv
        kart_env = KartEnv()
        policy = "MlpPolicy"
    else: #env_name == "detailed_minimap_enhanced" or env_name == "4":
        from Reinforcement_AI.env.e_enhanced_image_env import DetailedMiniMapEnv as DetailedMiniMapEnv2
        kart_env = DetailedMiniMapEnv2()
        policy = "CnnPolicy"

    if model_name == "DQN":
        model = DQN(policy=policy, env=kart_env, double_q=True, prioritized_replay=True, verbose=1)
    elif model_name == "HER":
        model = HER(policy=policy, env=kart_env, model_class=DQN, verbose=1)
    else: # model_name == "PPO2"
        model = PPO2(policy=policy, learning_rate=0.0001, env=kart_env, verbose=1)

    if test_mode:       # 테스트 모드일때 에이전트 불러와서 작동하게함
        model.load(filepath)
        kart_env.set_continuos(True)

        while True:
            observation = kart_env.reset()
            while True:
                action, _states = model.predict(observation)
                observation, rewards, dones, info = kart_env.step(action)
                if dones:
                    break

    else:
        for i in range(1000):
            model.learn(total_timesteps=12500)
            model.save(str(env_name) + "_" + model_name + "_" + str(i+1))

if __name__ == "__main__":

    import sys
    import time
    import os

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

    if len(sys.argv) != 2:
        print("프로그램에 심각한 에러 발생! 종료합니다.")
        time.sleep(2)

    if sys.argv[1] == "1":
        filepath = PATH + "/Best_Agent/PPO2_best"
    elif sys.argv[1] == "2":
        filepath = PATH + "/Best_Agent/PPO2_best2"
    else:
        print("프로그램에 심각한 에러 발생! 종료합니다.")
        time.sleep(2)

    # 에이전트 및 환경 설정
    agent = "PPO2"
    env = 3
    """
    env: 불러올 환경의 이름입니다. (int)
        1 : 미니맵 이미지를 사용하지 않은, 점 사이의 거리 계산을 한 환경입니다.
        2 : 미니맵 이미지를 사용하고, 보상을 업데이트한 모델입니다.
        3 : 1의 버전에서 개선된 모델입니다.
        4 : 3의 버전에서 부스터를 사용하도록 개선된 모델입니다.
        다른 값(기본) : 현재 쓰는 모델입니다. 미니맵 이미지를 사용하고, 보상을 다시 업데이트한 모델입니다.
    agent: 설정할 모델의 이름입니다.
        DQN : DQN 모델을 불러옵니다.
        HER : HER 모델을 불러옵니다.
        다른 값(기본) : PPO2 모델을 불러옵니다.
    """

    if env == 2:
        launchAgent(env, agent)
    else:
        from Image_Processing.image_processing import runIP
        from multiprocessing import Manager

        manager = Manager()
        runIP(manager)
        launchAgent(env, agent, test_mode=True, filepath=filepath)


import asyncio
from discord import opus
from .internal import perms
from .internal import commands

class main:
  def __init__(self, client):
    self.client = client
    if not opus.is_loaded():
      try:
        opus.load_opus("opus") #windows provides opus
      except OSError: #not on windows
        try:
          opus.load_opus("libopus") #use the provided opus for OSX, linux provides under this name
        except OSError:
          print("Could not load OPUS, therefore voice will work.")
    self.channels = {}
    self.servers = {}

  @commands.command()
  async def play(self, m, args):
    """Search youtube for and play a song
USAGE:
  play query

ARGUMENTS:
  query:  The youtube search query to use."""
    channel = m.server.get_member(m.author.id).voice.voice_channel
    if channel is not None:
      if self.channels.get(channel.id, None) is None:
        if m.server.id in self.servers.keys():
          await self.client.send_message(m.channel, "You must be in the same voice channel as me to control me.")
          return
        vc = voiceChannel()
        await vc.init(self.client, channel, m, self)
        self.channels[channel.id] = vc
        self.servers[m.server.id] = vc
      else:
        vc = self.channels[channel.id]
      await self.client.send_message(m.channel, "Added your request to the queue.")
      try:
        await vc.add(args, m)
      except Exception as e:
        if type(e).__name__.endswith('IndexError'):
          await self.client.send_message(m.channel, "Something went wrong adding your song. Please try again.")
        elif type(e).__name__.endswith('DownloadError'):
          await self.client.send_message(m.channel, "There were no youtube results for that search term. Please try another search.")
        else:
          raise e
    else:
      await self.client.send_message(m.channel, "Err: Please join a voice channel first.")

  @commands.command()
  async def skip(self, m, _):
    """Skip the current song.
USAGE:
  skip"""
    channel = m.server.get_member(m.author.id).voice.voice_channel
    if channel is not None:
      if self.channels.get(channel.id, None) is not None:
        vc = self.channels[channel.id]
        if m.author.id == vc.m.author.id:
          await self.client.send_message(m.channel, "The requester of this song has requested to skip it. Skipping...")
          await vc.next()
        elif await perms.check(m.author.id, "owner"):
          await self.client.send_message(m.channel, "The bot owner has requested to skip this song. Skipping...")
          await vc.next()
        else:
          if m.author.id in vc.skipVotes:
            await self.client.send_message(m.channel, "You have already voted to skip this song.")
            return
          vc.skipVotes.append(m.author.id)
          if len(vc.skipVotes) >= len(channel.voice_members) // 2:
            await self.client.send_message(m.channel, "Half of the people listening have voted to skip this song. Skipping...")
            await vc.next()
          else:
            await self.client.send_message(m.channel, str(len(vc.skipVotes)) + " people have voted to skip the current song, out of a needed " + str(len(channel.voice_members) // 2) + ".")
      elif m.server.id in self.servers.keys():
        await self.client.send_message(m.channel, "You must be in the same voice channel as me to control me.")
      else:
        await self.client.send_message(m.channel, "I am not playing voice in this server right now.")
    else:
      await self.client.send_message(m.channel, "Err: Please join a voice channel first.")

  @commands.adminCommand()
  async def stop(self, m, _):
    """Stop playing voice for the current server
USAGE:
  stop"""
    if m.server.id in self.servers.keys():
      await self.servers[m.server.id].disconnect()
      await self.client.send_message(m.channel, "Stopped playing audio.")

  @commands.command(optional=True)
  async def volume(self, m, args):
    """Set the volume of the current song.
USAGE:
  volume [percentage|up|down]

ARGUMENTS:
  percentage:  10-200, The percentage volume.

  up:  Increase the volume by 10%.

  down Decrease the volume by 10%."""
    channel = m.server.get_member(m.author.id).voice.voice_channel
    if channel is not None:
      if self.channels.get(channel.id, None) is not None:
        vc = self.channels[channel.id]
        if vc.playing is not None:
          if args is None:
            await self.client.send_message(m.channel, "The current volume is " + str(int(vc.playing.volume * 100)) + "%.")
            return
          elif args == "up":
            args = str(int((vc.playing.volume * 100) + 10))
          elif args == "down":
            args = str(int((vc.playing.volume * 100) - 10))
          try:
            num = int(args)
          except ValueError:
            await self.client.send_message(m.channel, "You must input a number between 10 and 200, or 'up' or 'down'.")
            return
          if num < 10 or num > 200:
            await self.client.send_message(m.channel, "Err: Input must be between 10 and 200.")
          else:
            vc.playing.volume = num / 100
            await self.client.send_message(m.channel, "Volume set to " + str(int(vc.playing.volume * 100)) + "%.")
        else:
          await self.client.send_message(m.channel, "Nothing is playing right now.")
      elif m.server.id in self.servers.keys():
        await self.client.send_message(m.channel, "You must be in the same voice channel as me to control me.")
      else:
        await self.client.send_message(m.channel, "I am not playing voice in this server right now.")
    else:
      await self.client.send_message(m.channel, "Err: Please join a voice channel first.")

  @commands.command()
  async def pause(self, m, _):
    """Pauses the current song.
USAGE:
  pause"""
    channel = m.server.get_member(m.author.id).voice.voice_channel
    if channel is not None:
      if self.channels.get(channel.id, None) is not None:
        vc = self.channels[channel.id]
        if vc.playing is not None:
          vc.playing.pause()
          await self.client.send_message(m.channel, "Paused audio.")
        else:
          await self.client.send_message(m.channel, "Nothing is playing right now.")
      elif m.server.id in self.servers.keys():
        await self.client.send_message(m.channel, "You must be in the same voice channel as me to control me.")
      else:
        await self.client.send_message(m.channel, "I am not playing voice in this server right now.")
    else:
      await self.client.send_message(m.channel, "Err: Please join a voice channel first.")

  @commands.command()
  async def resume(self, m, _):
    """Resumes the current song.
USAGE:
  resume"""
    channel = m.server.get_member(m.author.id).voice.voice_channel
    if channel is not None:
      if self.channels.get(channel.id, None) is not None:
        vc = self.channels[channel.id]
        if vc.playing is not None:
          vc.playing.resume()
          await self.client.send_message(m.channel, "Resumed audio.")
        else:
          await self.client.send_message(m.channel, "Nothing is playing right now.")
      elif m.server.id in self.servers.keys():
        await self.client.send_message(m.channel, "You must be in the same voice channel as me to control me.")
      else:
        await self.client.send_message(m.channel, "I am not playing voice in this server right now.")
    else:
      await self.client.send_message(m.channel, "Err: Please join a voice channel first.")

class voiceChannel:
  async def init(self, client, channel, m, parent):
    self.client = client
    self.channel = channel
    self.m = m
    self.parent = parent
    self.queue = []
    self.voice = await self.client.join_voice_channel(self.channel)
    self.playing = None
    self.event = asyncio.Event()
    self.skipVotes = []
    self.task = None
    self.timeoutTask = None

  async def add(self, song, m):
    self.timeoutTask = asyncio.get_event_loop().create_task(self.timeout()) #leave if the search fails
    player = await self.voice.create_ytdl_player(song, ytdl_options={'default_search': 'ytsearch','quiet': True,}, after=self.setEvent)
    self.queue.append((player, m))
    if self.playing is None:
      await self.next()
    elif self.timeoutTask is not None:
      self.timeoutTask.cancel()

  def setEvent(self):
    self.event.set()

  async def nextEvent(self):
    try:
      await self.event.wait()
    except asyncio.CancelledError:
      self.task = None
      self.event.clear()
      return
    self.event.clear()
    self.task = None
    await self.next()

  async def next(self):
    if self.task is not None:
      self.task.cancel()
    if self.timeoutTask is not None:
      self.timeoutTask.cancel()
    if self.playing is not None:
      await self.stop()
    if len(self.queue) > 0:
      self.skipVotes = []
      self.playing = self.queue[0][0]
      self.m = self.queue[0][1]
      dur = str(self.playing.duration // 60) + "m" + str(self.playing.duration % 60) + "s"
      await self.client.send_message(self.m.channel, "Playing *" + self.playing.title + "*, requested by " + self.queue[0][1].author.display_name + ", duration " + dur + ".")
      self.task = asyncio.get_event_loop().create_task(self.nextEvent())
      self.playing.start()
    else:
      self.timeoutTask = asyncio.get_event_loop().create_task(self.timeout())

  async def stop(self):
    if self.playing is not None and self.playing.is_playing():
      self.playing.stop()
    await self.client.send_message(self.m.channel, "Done playing *" + self.playing.title + "*.")
    self.playing = None
    self.queue = self.queue[1:]

  async def timeout(self):
    try:
      await asyncio.sleep(30)
    except asyncio.CancelledError:
      self.timeoutTask = None
      return
    await self.disconnect()

  async def disconnect(self):
    await self.voice.disconnect()
    del self.parent.channels[self.parent.servers[self.m.server.id].channel.id]
    del self.parent.servers[self.m.server.id]


# Made By Barry
import mona

# Spécifie le système d'exploitation cible
target = mona.process("notepad.exe")

# Analyse le code binaire de la cible
mona.find_all_calls(target)

# Recherche des vulnérabilités connues
mona.find_bugs(target)

# Affiche les résultats de l'analyse
mona.show()


#!/usr/bin/env python
"""connectivity.py: demonstrate NetworkX edge_connectivity, etc.
"""
import networkx as nx

def main():
    """The main function.

    Returns:
        None.
    """

    for i, G in enumerate(generate_graphs()):
        print(f"G{i} is "
              f"{nx.node_connectivity(G)}-connected and "
              f"{nx.edge_connectivity(G)}-edge connected.")

def generate_graphs():
    """Generate graphs to demonstrate connectivities.

    Graphs are borrowed from:
    http://www.personal.kent.edu/~rmuhamma/GraphTheory/MyGraphTheory/connectivity.htm

    Yields:
      Graph instances.
    """

    # G0
    yield nx.MultiGraph((
        (0, 2), (2, 0),
        (1, 2),
        (3, 1)))
    # G1
    yield nx.Graph((
        (0, 1), (1, 2), (2, 0),
        (2, 3),
        (3, 4), (4, 6), (6, 3)))
    # G2
    yield nx.Graph((
        (0, 1), (1, 2), (2, 0),
        (2, 3), (3, 4), (4, 2)))
    # G3
    G = nx.cycle_graph(4)
    G.add_edge(3, 3)
    yield G

if __name__ == '__main__':
    main()


import sys

def validate():
    creator = '{{ cookiecutter.creator }}'

    if not creator.strip():
        print("You must specify a creator to use this template.")
        sys.exit(1)

    favorite_color = '{{ cookiecutter.favorite_color }}'
    if favorite_color == 'black':
        print("Currently black is actually not implemented!")
        sys.exit(7)

if __name__ == '__main__':
    validate()


# Loading the required packages
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# Function to normalize the input data(array form)
def Normalize(x):
    mean = np.mean(x)
    standard_deviation = np.std(x)
    X = (x - mean)/standard_deviation
    return X

no_of_epochs = 100
loss_per_epoch = []

###########################################################################################
# Data Preprocessing
###########################################################################################

# Now we load the boston house price dataset using tensorflow contrib Datasets
boston_dataset = tf.contrib.learn.datasets.load_dataset('boston')

#Print the data shape
print(boston_dataset.data.shape)

# Separate the data into X_train and Y_train
# Here we are considering all the features
X_train , Y_train = boston_dataset.data , boston_dataset.target
print(X_train.shape)
print(Y_train.shape)

# Normalize the training data
X_train = Normalize(X_train)

# First index gives the number of samples
n_samples = X_train.shape[0]

# Second index gives the number of features
n_features = X_train.shape[1]

###########################################################################################
# Creating Computational Graph
###########################################################################################

# Placeholder for storing the training data
X = tf.placeholder(tf.float32 , name = 'X', shape = [n_samples , n_features])
Y = tf.placeholder(tf.float32 , name = 'Y')

# Assigning weights and biases randomly
w = tf.Variable(tf.random_normal([n_features , 1] , seed = 12) , name = 'weight')
b = tf.Variable(tf.random_normal([n_samples , 1] ,  seed = 12) , name = 'biases')

# Linear Regression Model for prediction
y_pred = tf.add(tf.matmul(X,w) , b)

# Loss function(MSE)
loss = tf.reduce_mean(tf.square( Y - y_pred, name = 'loss')) + 0.01*tf.nn.l2_loss(w)

# Gradient descent optimiter for minimizing loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(loss)

# Initializing variables
init = tf.global_variables_initializer()

############################################################################################
# Training the model
############################################################################################

with tf.Session() as sess:

    # initializing the variables
    sess.run(init)

    # for visualization in tensorboard
    writer = tf.summary.FileWriter('graphs' , sess.graph)
    for epoch in range(no_of_epochs):

        # Feeding the data and finding the loss in the current epoch
        # Evaluating the train and loss nodes
        _ , current_loss = sess.run([optimizer , loss] , feed_dict = {X : X_train , Y : Y_train})

        # Append the current loss
        loss_per_epoch.append(current_loss)

        #Printing average loss in each epoch
        print('Epoch {0} : Loss {1}'.format(epoch , current_loss))

    writer.close()

    # Getting the value of weight and bias
    w_value , b_value = sess.run([w , b])
    print('Weights {0}: Bias {1}'.format(w_value, b_value))

# Find the prediction vector
Y_pred = np.matmul(X_train,w_value) + b_value

#################################################################################################
# Plotting the loss curve
#################################################################################################

plt.plot(loss_per_epoch)
plt.show()


import os.path

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
	def get(self):
		self.render('index.html')

class PoemPageHandler(tornado.web.RequestHandler):
	def post(self):
		noun1 = self.get_argument('noun1')
		noun2 = self.get_argument('noun2')
		verb = self.get_argument('verb')
		noun3 = self.get_argument('noun3')
		self.render('poem.html', roads=noun1, wood=noun2, made=verb,
				difference=noun3)

if __name__ == '__main__':
	tornado.options.parse_command_line()
	app = tornado.web.Application(
		handlers=[(r'/', IndexHandler), (r'/poem', PoemPageHandler)],
		template_path=os.path.join(os.path.dirname(__file__), "templates")
	)
	http_server = tornado.httpserver.HTTPServer(app)
	http_server.listen(options.port)
	tornado.ioloop.IOLoop.instance().start()


#This script is for creating the class labels
import csv
import itertools
#We just need the iowa data for 31 years for creating the class labels
fileName = "raw_data/iowa_31years.csv"
sumList = []
for time in range(0,11309):
    with open(fileName, 'r') as csvFileIn:
        csvReader = csv.reader(csvFileIn)
        sum = 0
        #take 15 days sum and put into sum
        for row in itertools.islice(csvReader, time, time+15):
            sum = sum + float(row[0])
    #append each sum to sumList
    sumList.append(sum)

import numpy as np
#convert sumList to numpy array, so as to do percentile
sumArray = np.array(sumList)
#p is the 95th percentile
p = np.percentile(sumArray, 95)

classLabel = []
for row in sumList:
    #if sum is > than p, classLabel is 1. Else its 0
    if row > p:
        classLabel.append(1)
    else:
        classLabel.append(0)

#we need only class labels from 1-15-1980, so remove first 14 rows
classLabel = classLabel[14:]
sumList = sumList[14:]

#Write into class_label.csv
with open("classLabels.csv", 'wb') as csvFileOut:
    csvWriter = csv.writer(csvFileOut)
    for label in classLabel:
        csvWriter.writerow([label])


from models.resnet101 import ResNet101, ResNet50, ResNet152
from config import opt
from preprocess_and_load.dataset import ChestXrayDataSet
from torch.utils.data import DataLoader
import torch
import time
from tqdm import tqdm
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from torch.optim.lr_scheduler import ReduceLROnPlateau
import torch.nn as nn

class MyLoss(nn.Module):
    def __init__(self):
        super(MyLoss, self).__init__()

    def forward(self, input, target):
        input = input.reshape(-1)
        target = target.reshape(-1)
        wp, wn = self.calculate_weight(target)
        loss1 = 0
        loss0 = 0

        for i in range(len(target)):
            if target[i] == 1:
                loss1 += torch.log(input[i])
            elif target[i] == 0:
                loss0 += torch.log(1-input[i])
        loss = (-1 * wp*loss1-wn*loss0)/len(target)
        return loss

    def calculate_weight(self, target):
        length = len(target)+1
        wp = length/(torch.sum(target)+1)
        wn = length/(length-torch.sum(target))
        return wp, wn

def compute_AUCs(gt, pred):
    AUROCs = []
    gt_np = gt.cpu().numpy()  # (117,6)
    pred_np = pred.cpu().numpy()  # (117,6)
    for i in range(len(opt.classes)):
        tmp = np.sum(gt_np[:, i], axis=0)
        if tmp == 0:
            AUROCs.append(0)
            continue
        AUROCs.append(roc_auc_score(gt_np[:, i], pred_np[:, i]))
    return AUROCs

def compute_three_score(y_true, y_pred):
    y_pred = y_pred.cpu().numpy()
    for i in range(y_pred.shape[0]):
        for j in range(y_pred.shape[1]):
            if y_pred[i][j] > 0.5:
                y_pred[i][j] = 1
            else:
                y_pred[i][j] = 0
    y_true = y_true.cpu().numpy()

    precision_micro = precision_score(y_true, y_pred, average='micro')
    precision_macro = precision_score(y_true, y_pred, average='macro')

    recall_micro = recall_score(
        y_true, y_pred, average='micro', zero_division=1)
    recall_macro = recall_score(
        y_true, y_pred, average='macro', zero_division=1)

    f1_score_micro = f1_score(
        y_true, y_pred, average='micro', zero_division=1)
    f1_score_macro = f1_score(
        y_true, y_pred, average='macro', zero_division=1)

    print('******************** mirco *************************')
    print('mirco_precision = ', precision_micro)
    print('mirco_recall    = ', recall_micro)
    print('mirco_F1        = ', f1_score_micro)
    print('******************** marco *************************')
    print('marco_Precision = ', precision_macro)
    print('marco_Recall    = ', recall_macro)
    print('marco_F1        = ', f1_score_macro)
    print('\n')

def test(device='cuda'):
    model = ResNet101(num_class=14).to(device)
    checkpoint = torch.load('your_checkpoint_name')
    model.load_state_dict(checkpoint['state_dict'])
    print('Done')

    # data
    test_data = ChestXrayDataSet(opt.data_root, opt.test_data_list)
    test_dataloader = DataLoader(test_data, batch_size=opt.batch_size, shuffle=False, num_workers=opt.num_workers)
    total_batch = int(len(test_data) / opt.batch_size)
    gt = torch.FloatTensor().to(device)
    pred = torch.FloatTensor().to(device)

    # test
    print('\n---------------------------------')
    print(' ٩( ᐛ )و - Start testing ......')
    print('---------------------------------\n')

    model.eval()
    with torch.no_grad():
        bar = tqdm(enumerate(test_dataloader), total=total_batch)
        for i, (data, label) in bar:
            inp = data.clone().detach().to(device)
            target = label.clone().detach().to(device)

            output = model(inp)
            gt = torch.cat((gt, target), 0)
            pred = torch.cat((pred, output.data), 0)

    AUROCs = compute_AUCs(gt, pred)
    AUROC_avg = np.array(AUROCs).mean()
    print('*' * 30)
    print('The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
    print('*' * 30)
    for i in range(len(opt.classes)):
        print('The AUROC of {} is {}'.format(opt.classes[i], AUROCs[i]))
    print('*' * 30)

    compute_three_score(gt, pred)
    return

def train(device='cuda'):
    model = ResNet101(num_class=14).to(device)

    # step2: data
    train_data = ChestXrayDataSet(opt.data_root, opt.train_data_list)
    val_data = ChestXrayDataSet(opt.data_root, opt.valid_data_list)
    train_dataloader = DataLoader(train_data, opt.batch_size, shuffle=True, num_workers=opt.num_workers)
    val_dataloader = DataLoader(val_data, opt.batch_size, shuffle=False, num_workers=opt.num_workers)

    # step3: criterion and optimizer
    criterion_bce = torch.nn.BCELoss(reduction='mean')
    criterion = MyLoss().to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr, betas=opt.betas,
                                 eps=opt.eps, weight_decay=opt.weight_decay)
    scheduler = ReduceLROnPlateau(optimizer, factor=0.1, patience=5, mode='min')

    # step4: meters
    loss_mean_min = 1e100

    # train
    print('\n---------------------------------')
    print('...... Start training ......')
    print('---------------------------------\n')
    for epoch in range(5):
        print('Epoch', epoch + 1)
        model.train()
        total_batch = int(len(train_data) / opt.batch_size)

        bar = tqdm(enumerate(train_dataloader), total=total_batch)
        for i, (data, label) in bar:
            model.train()
            torch.set_grad_enabled(True)
            inp = data.clone().detach().requires_grad_(True).to(device)
            target = label.clone().detach().to(device)

            optimizer.zero_grad()
            output = model(inp)
            loss0 = criterion_bce(output, target)
            loss = loss0  # + loss1
            loss.backward()
            optimizer.step()
            bar.set_postfix_str('loss: %.5s' % loss.item())

        loss_mean = _val(model, val_dataloader, criterion_bce, total_batch)
        time_end = time.strftime('%m%d_%H%M%S')
        scheduler.step(loss_mean)
        if loss_mean_min > loss_mean:
            loss_mean_min = loss_mean
            torch.save({'epoch': epoch + 1,
                        'state_dict': model.state_dict(),
                        'optimizer': optimizer.state_dict()},
                       'your_path' + time_end + '.pth.tar')
            print('Epoch [' + str(epoch + 1) + '] [save] [m_' + time_end + '] loss= ' + str(loss_mean))
        else:
            print('Epoch [' + str(epoch + 1) + '] [----] [m_' + time_end + '] loss= ' + str(loss_mean))
        print('--------------------------------------------------------------------------\n')
    return

def _val(model, dataloader, criterion_bce, total_batch, device='cuda'):
    model.eval()
    counter = 0
    loss_sum = 0

    with torch.no_grad():
        bar = tqdm(enumerate(dataloader), total=total_batch)
        for i, (data, label) in bar:
            inp = data.clone().detach().to(device)
            target = label.clone().detach().to(device)

            output = model(inp)
            loss0 = criterion_bce(output, target)
            loss = loss0  # + loss1
            loss_sum += loss.item()
            counter += 1
            bar.set_postfix_str('loss: %.5s' % loss.item())

    loss_mean = loss_sum / counter
    return loss_mean

if __name__ == '__main__':
    print('--- CheXpert Baseline Classifier ---')
    mission = 2

    if mission == 1:
        train()
    elif mission == 2:
        test()


import torch
import torch.nn as nn
import numpy as np

from torch.utils.data import DataLoader, Dataset, ConcatDataset
import yaml
from tqdm import tqdm
import pickle

from scripts.train_naturl_representations import NATURLDataModule
from scripts.train_rca_cost import RCATerrainDataset
from scripts.models import VisualEncoderModel, CostNet

if __name__ == '__main__':

    # data_config_path = '/robodata/haresh92/spot-vrl/spot_data/data_config.yaml'
    # dm = NATURLDataModule(data_config_path, psd=False, data_statistics=None)
    # dataset = dm.val_dataset
    data_config  = yaml.load(open('/robodata/haresh92/spot-vrl/spot_data/data_config.yaml', 'r'), Loader=yaml.FullLoader)
    dataset = ConcatDataset([RCATerrainDataset(pickle_files_root, img_augment=False) for pickle_files_root in data_config['train']])

    # load the data_statistics.pkl file
    data_statistics = pickle.load(open('/robodata/haresh92/spot-vrl/spot_data/data_statistics.pkl', 'rb'))
    imin, imax = data_statistics['min']['imu'], data_statistics['max']['imu']

    data = {}

    for patch1, patch2, imu, leg, feet, label, idx in tqdm(dataset):
        # print(' imu shape :', imu.shape)
        # convert to torch tensor and add batch dimension
        patch1 = torch.tensor(patch1).unsqueeze(0)
        patch2 = torch.tensor(patch2).unsqueeze(0)
        imu = torch.tensor(imu).unsqueeze(0)
        leg = torch.tensor(leg).unsqueeze(0)
        feet = torch.tensor(feet).unsqueeze(0)

        # move to device
        patch1 = patch1.cuda()
        patch2 = patch2.cuda()
        imu = imu.cuda()
        leg = leg.cuda()
        feet = feet.cuda()

        # forward pass
        # cost = model(patch1.float())
        cost = torch.mean(imu[:, 0]) + torch.mean(imu[:, 1]) + torch.mean(imu[:, 5])
        cost = cost.detach().cpu().numpy().flatten()[0]

        if label not in data:
            data[label] = []
        data[label].append(cost)

        # if idx > 500:
        #     break

    # plot the labels in x axis and the mean cost with std in y axis
    import matplotlib.pyplot as plt
    labels = []
    mean_costs = []
    std_costs = []
    for label in data:
        labels.append(label)
        mean_costs.append(np.mean(data[label]))
        std_costs.append(np.std(data[label]))

    # find mean across all labels
    for label in data:
        data[label] = np.mean(data[label])

    # minmax normalize the costs
    minlabel, maxlabel = min(data.values()), max(data.values())
    for label in data:
        data[label] = (data[label] - minlabel) / (maxlabel - minlabel)

    # save the data to a pickle file
    pickle.dump(data, open('rca_costs.pkl', 'wb'))

    # plot
    plt.figure()
    plt.bar(data.keys(), data.values())
    plt.xticks(list(data.keys()), rotation=45)
    plt.ylabel('Cost')
    plt.title('Costs for different labels')
    # prevent the labels from being cut off
    plt.tight_layout()
    plt.savefig('rca_costs_minmax.png')


import numpy as np

from sinter._decoding_decoder_class import Decoder, CompiledDecoder

class VacuousDecoder(Decoder):
    """An example decoder that always predicts the observables aren't flipped.
    """

    def compile_decoder_for_dem(self, *, dem: 'stim.DetectorErrorModel') -> CompiledDecoder:
        return VacuousCompiledDecoder(shape=(dem.num_observables + 7) // 8)

    def decode_via_files(self,
                         *,
                         num_shots: int,
                         num_dets: int,
                         num_obs: int,
                         dem_path: 'pathlib.Path',
                         dets_b8_in_path: 'pathlib.Path',
                         obs_predictions_b8_out_path: 'pathlib.Path',
                         tmp_dir: 'pathlib.Path',
                       ) -> None:
        with open(obs_predictions_b8_out_path, 'wb') as f:
            f.write(b'\0' * (num_obs * num_shots))

class VacuousCompiledDecoder(CompiledDecoder):
    """An example decoder that always predicts the observables aren't flipped.
    """
    def __init__(self, shape: int):
        self.shape = shape

    def decode_shots_bit_packed(
            self,
            *,
            bit_packed_detection_event_data: 'np.ndarray',
    ) -> 'np.ndarray':
        return np.zeros(shape=(bit_packed_detection_event_data.shape[0], self.shape), dtype=np.uint8)


#!/usr/bin/python3
"""The rectangle subclass of the BaseGeometry class"""
BaseGeometry = __import__('7-base_geometry').BaseGeometry

class Rectangle(BaseGeometry):
    """class for rectangles"""

    def __init__(self, width, height):
        """initializes a new rectangle"""
        self.integer_validator("width", width)
        self.__width = width
        self.integer_validator("height", height)
        self.__height = height


import torch
import torch_geometric

from torch.nn import Linear
from torch.nn.functional import dropout, elu
from torch_geometric.nn.conv import SplineConv
from torch_geometric.nn.norm import BatchNorm
from torch_geometric.transforms import Cartesian

from aegnn.models.layer import MaxPooling, MaxPoolingX

class GraphWen(torch.nn.Module):

    def __init__(self, dataset: str, input_shape: torch.Tensor, num_outputs: int, **kwargs):
        super(GraphWen, self).__init__()
        assert len(input_shape) == 3, "invalid input shape, should be (img_width, img_height, dim)"

        self.block1 = ConvBlock(1, 64, voxel_size=4)
        self.block2 = ConvBlock(64, 128, voxel_size=8)
        self.block3 = ConvBlock(128, 256, voxel_size=16)
        self.block4 = ConvBlock(256, 512, voxel_size=32)

        self.pool_final = MaxPoolingX(input_shape[:2] // 2, size=4)
        self.linear1 = Linear(512 * 4, out_features=512,)
        self.linear2 = Linear(512, out_features=num_outputs)

    def forward(self, data: torch_geometric.data.Batch) -> torch.Tensor:
        data = self.block1(data)
        data = self.block2(data)
        data = self.block3(data)
        data = self.block4(data)

        x = self.pool_final(data.x, pos=data.pos[:, :2], batch=data.batch)
        x = x.view(-1, self.linear1.in_features)
        x = self.linear1(x)
        x = dropout(x, p=0.3, training=self.training)
        return self.linear2(x)

class ConvBlock(torch.nn.Module):

    def __init__(self, in_channels: int, out_channels: int, voxel_size: int, transform=Cartesian(norm=True, cat=False)):
        super(ConvBlock, self).__init__()
        self.conv = SplineConv(in_channels, out_channels, dim=3, kernel_size=4)
        self.norm = BatchNorm(out_channels)
        self.pool = MaxPooling([voxel_size] * 3, transform=transform)

    def forward(self, data: torch_geometric.data.Batch):
        data.x = elu(self.conv(data.x, data.edge_index, data.edge_attr))
        data.x = self.norm(data.x)
        data = self.pool(data.x, pos=data.pos, batch=data.batch, edge_index=data.edge_index, return_data_obj=True)
        return data