import json
import os
from typing import Dict

from . import logging

DEFAULT_SEVERITY_INFO = "INFO"

RESOURCE_ID_ATTRIBUTE = "azure.resource.id"
SUBSCRIPTION_ATTRIBUTE = "azure.subscription"
RESOURCE_GROUP_ATTRIBUTE = "azure.resource.group"
RESOURCE_TYPE_ATTRIBUTE = "azure.resource.type"
RESOURCE_NAME_ATTRIBUTE = "azure.resource.name"

log_level_to_severity_dict = {
    1: 'Critical',
    2: 'Error',
    3: 'Warning',
    4: 'Informational'
}

severity_to_log_level_dict = {v: k for k, v in log_level_to_severity_dict.items()}

azure_level_properties = ['Level', 'level']
azure_properties_names = ['properties', 'EventProperties']
activity_log_categories = ['alert', 'administrative', 'resourcehealth', 'servicehealth', 'security', 'policy', 'recommendation', 'autoscale']

working_directory = os.path.dirname(os.path.realpath(__file__))
me_type_mapper_file_path = os.path.join(working_directory, "me_type_mapper.json")
dt_me_type_mapper = {}
try:
    with open(me_type_mapper_file_path) as me_type_mapper_file:
        me_type_mapper_json = json.load(me_type_mapper_file)
        for resource_type_to_me_type in me_type_mapper_json:
            resource_type = resource_type_to_me_type["resourceType"].lower()
            category = resource_type_to_me_type.get("category", "").lower()
            key = ",".join(filter(None, [resource_type, category]))
            dt_me_type_mapper.update({key: resource_type_to_me_type["meType"]})
except Exception:
    logging.exception(f"Failed to load file with meType mapping: '{me_type_mapper_file_path}'",
                      "meType-mapping-file-loading-exception")

def extract_resource_id_attributes(parsed_record: Dict, resource_id: str):
    """
    based on https://github.com/Azure/azure-libraries-for-net/blob/Fluent-v1.37.0/src/ResourceManagement/ResourceManager/Core/ResourceId.cs#L29
    Format of id:
    /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/<providerNamespace>(/<parentResourceType>/<parentName>)*/<resourceType>/<name>
    0             1                2              3                   4         5                                                        N-2            N-1
    example: /SUBSCRIPTIONS/69B51384-146C-4685-9DAB-5AE01877D7B8/RESOURCEGROUPS/TESTMS/PROVIDERS/MICROSOFT.APIMANAGEMENT/SERVICE/WEATHERAPP-API-MGMT
    """
    parsed_record[RESOURCE_ID_ATTRIBUTE] = resource_id
    parts = resource_id.lstrip("/").split("/")

    # No logging on invalid resource_id to avoid flooding logs. Invalid resource id will be sent
    # with log line to Dynatrace so we keep the ability to debug in case of any issues
    if len(parts) < 7:
        return
    if parts[0].casefold() != "SUBSCRIPTIONS".casefold():
        return
    if parts[2].casefold() != "RESOURCEGROUPS".casefold():
        return
    if parts[4].casefold() != "PROVIDERS".casefold():
        return

    parsed_record[SUBSCRIPTION_ATTRIBUTE] = parts[1]
    parsed_record[RESOURCE_GROUP_ATTRIBUTE] = parts[3]
    parsed_record[RESOURCE_NAME_ATTRIBUTE] = parts[-1]

    resource_type_parts_with_parent = parts[5:-1]
    # Filter out parent resource name to create hierarchic resource type as cloudbuilder does
    resource_type_parts = [part for index, part in enumerate(resource_type_parts_with_parent) if (index == 0 or index % 2 != 0)]
    parsed_record[RESOURCE_TYPE_ATTRIBUTE] = "/".join(resource_type_parts)

def extract_severity(record: Dict, parsed_record: Dict):
    level_property = next((level for level in azure_level_properties if level in record.keys()), None)
    if level_property:
        map_to_severity(record, parsed_record, level_property)
    else:
        parsed_record["severity"] = DEFAULT_SEVERITY_INFO

def map_to_severity(record: Dict, parsed_record: Dict, level_property: str):
    if isinstance(record[level_property], int):
        parsed_record["severity"] = log_level_to_severity_dict.get(record[level_property], DEFAULT_SEVERITY_INFO)
    else:
        parsed_record["severity"] = record[level_property]


import random
quien_gan = "no"
jug = "X"
jug_2 = "O"
tablero = [
             [" "," "," "],
             [" "," "," "],
             [" "," "," "]]
def tabler():
    print(' ' + tablero[0][0] + '   |  ' + tablero[0][1] + ' |  ' + tablero [0][2])
    print('     |    |')
    print("------------------")
    print(' ' + tablero[1][0] + '   |  ' + tablero[1][1] + ' |  ' + tablero [1][2])
    print('     |    |')
    print("------------------")
    print(' ' + tablero[2][0] + '   |  ' + tablero[2][1] + ' |  ' + tablero [2][2])
    print('     |    |')

def ejemplo_cordenadas():
    print("Abajo esta el tablero con las teclas y sus respectivas posiciones")
    tablero[0][0] = "q"
    tablero[0][1] = "w"
    tablero[0][2] = "e"
    tablero[1][0] = "a"
    tablero[1][1] = "s"
    tablero[1][2] = "d"
    tablero[2][0] = "z"
    tablero[2][1] = "x"
    tablero[2][2] = "c"
    print(' ' + tablero[0][0] + '   |  ' + tablero[0][1] + '  |  ' + tablero [0][2])
    print('     |     |')
    print("------------------")
    print(' ' + tablero[1][0] + '   |  ' + tablero[1][1] + '  |  ' + tablero [1][2])
    print('     |     |')
    print("------------------")
    print(' ' + tablero[2][0] + '   |  ' + tablero[2][1] + '  |  ' + tablero [2][2])
    print('     |     |')
    tablero[0][0] = " "
    tablero[0][1] = " "
    tablero[0][2] = " "
    tablero[1][0] = " "
    tablero[1][1] = " "
    tablero[1][2] = " "
    tablero[2][0] = " "
    tablero[2][1] = " "
    tablero[2][2] = " "

def ganadorcc():
    if tablero[0][0] == "X" and tablero[0][1] == "X" and tablero[0][2] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[1][0] == "X" and tablero[1][1] == "X" and tablero[1][2] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[2][0] == "X" and tablero[2][1] == "X" and tablero[2][2] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[0][0] == "X" and tablero[1][0] == "X" and tablero[2][0] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[0][1] == "X" and tablero[1][1] == "X" and tablero[2][1] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[0][2] == "X" and tablero[1][2] == "X" and tablero[2][2] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[0][0] == "X" and tablero[1][1] == "X" and tablero[2][2] == "X":
        ganador = "si"
        quien_gan = jugador_1
    elif tablero[0][2] == "X" and tablero[1][1] == "X" and tablero[2][0] == "X":
        ganador = "si"
        quien_gan = jugador_1

    elif tablero[0][0] == "O" and tablero[0][1] == "O" and tablero[0][2] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[1][0] == "O" and tablero[1][1] == "O" and tablero[1][2] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[2][0] == "O" and tablero[2][1] == "O" and tablero[2][2] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[0][0] == "O" and tablero[1][0] == "O" and tablero[2][0] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[0][1] == "O" and tablero[1][1] == "O" and tablero[2][1] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[0][2] == "O" and tablero[1][2] == "O" and tablero[2][2] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[0][0] == "O" and tablero[1][1] == "O" and tablero[2][2] == "O":
        ganador = "si"
        quien_gan = jugador_2
    elif tablero[0][2] == "O" and tablero[1][1] == "O" and tablero[2][0] == "O":
        ganador = "si"
        quien_gan = jugador_2
    else:
        ganador = "no"
    if ganador == "si":
        print("Termino el juego felicidades a:",quien_gan)
        exit(0)
    if tablero[0][0] != " " and tablero[0][1] != " " and tablero[0][2] != " " and tablero[1][0] != " " and tablero[1][1] != " " and tablero[1][2] != " " and tablero[2][0] != " " and tablero[2][1] != " " and tablero[2][2] != " ":
        print("Termino el Juego en un Empate")
        exit(0)

def user_choiceP1CC():
    ganadorcc()
    valido = "n"
    while valido != "y":
        print(jugador_1)
        eleccion = input("Ingrese en que posicion quiere poner su pieza: ")
        if eleccion == "q" and tablero[0][0] == " ":
            tablero[0][0] = "X"
            valido = "y"
        elif eleccion == "w" and tablero[0][1] == " ":
            tablero[0][1] = "X"
            valido = "y"
        elif eleccion == "e" and tablero[0][2] == " ":
            tablero[0][2] = "X"
            valido = "y"
        elif eleccion == "a" and tablero[1][0] == " ":
            tablero[1][0] = "X"
            valido = "y"
        elif eleccion == "s" and tablero[1][1] == " ":
            tablero[1][1] = "X"
            valido = "y"
        elif eleccion == "d" and tablero[1][2] == " ":
            tablero[1][2] = "X"
            valido = "y"
        elif eleccion == "z" and tablero[2][0] == " ":
            tablero[2][0] = "X"
            valido = "y"
        elif eleccion == "x" and tablero[2][1] == " ":
            tablero[2][1] = "X"
            valido = "y"
        elif eleccion == "c" and tablero[2][2] == " ":
            tablero[2][2] = "X"
            valido = "y"
        else:
            print("lo siento ya esta tomada la posicion")
        tabler()

def Computer_choice():
    ganadorcc()
    valido = "n"
    while valido != "y":
        print(jugador_2)
        opciones = ["q","w","e","a","s","d","z","x","c"]
        eleccion = random.choice(opciones)
        if eleccion == "q" and tablero[0][0] == " ":
            tablero[0][0] = "O"
            valido = "y"
        elif eleccion == "w" and tablero[0][1] == " ":
            tablero[0][1] = "O"
            valido = "y"
        elif eleccion == "e" and tablero[0][2] == " ":
            tablero[0][2] = "O"
            valido = "y"
        elif eleccion == "a" and tablero[1][0] == " ":
            tablero[1][0] = "O"
            valido = "y"
        elif eleccion == "s" and tablero[1][1] == " ":
            tablero[1][1] = "O"
            valido = "y"
        elif eleccion == "d" and tablero[1][2] == " ":
            tablero[1][2] = "O"
            valido = "y"
        elif eleccion == "z" and tablero[2][0] == " ":
            tablero[2][0] = "O"
            valido = "y"
        elif eleccion == "x" and tablero[2][1] == " ":
            tablero[2][1] = "O"
            valido = "y"
        elif eleccion == "c" and tablero[2][2] == " ":
            tablero[2][2] = "O"
            valido = "y"
        else:
            print("lo siento ya esta tomada la posicion")
    tabler()

jugador_1 = input("Ingrese nombre del primer jugador: ")
posibles_nombres = ["Saad Maan","Sam Sung","Chris P. Bacon","Krystal Ball","Abe Rudder","Al Beback","Amy Stake","Andy Tover","Carrie Oakey"
,"Chris P. Nugget","Clara Nett","Dale E. Bread","Ella Vader","Emma Fraid","Faye Tallity","Anna Conda","Barry Cade","Helen Back","Marty Graw","Paige Turner"
,"Pepe Roni","Polly Ester","Ray Gunn","Tim Burr","Robyn Banks","Penny Wise","Jo King","Terry Bill","Justin Case","Anna Prentice","Anna Sasin","Ana Tomía"
,"Helen Chufe","Elena Nito","Elsa Capunta","Armando Casas","Esteban Dido","Zoyla Vaca","Elsa Polindo","Elsa Pato","Elmer Curio","Rosa Espinoza","Susana Oria"]
nombre_de_comp = random.choice(posibles_nombres)
jugador_2 = nombre_de_comp

def totito():
    tabler()
    print("Empieza: ",jugador_1)
    ejemplo_cordenadas()
    while quien_gan != "owo":
        tabler()
        user_choiceP1CC()
        Computer_choice()
totito()


#!/usr/bin/env python

import argparse
import re

def main(args: argparse.Namespace) -> None:
    with open(args.predictions, "r") as source:
        regexp_h = re.compile(r"[H]-\d+")
        regexp_t = re.compile(r"[T]-\d+")
        h_data = []
        t_data = []
        errors = 0
        for line in source:
            items = line.split("\t")
            if regexp_h.search(items[0]):
                h_data.append(items)
            if regexp_t.search(items[0]):
                t_data.append(items)
        for x, y in zip(h_data, t_data):
            if x[2] != y[1]:
                errors += 1
        print("WER:", errors)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("predictions", help="path to input .txt file")
    main(parser.parse_args())


"""
B113446

Absorption time experiment
"""

from B113446_exam_class import ExamLattice2D
import sys
import numpy as np
import matplotlib.pyplot as plt
import csv

def main():
    # Remind user of correct command line arguments if needed
    if(len(sys.argv) != 7):
        print ("Usage python B113446_exam_play.py D q p N dx dt")
        sys.exit()
    # Input the various constants of choice
    D = float(sys.argv[1])
    p = float(sys.argv[2])
    q = float(sys.argv[3])
    N = int(sys.argv[4])
    dx = float(sys.argv[5])
    dt = float(sys.argv[6])

    absorption_times = []
    num_experiments = 10
    while len(absorption_times) < num_experiments:
        a_start = np.random.uniform(0., 1./3, (N,N))
        b_start = np.random.uniform(0., 1./3, (N,N))
        c_start = np.random.uniform(0., 1./3, (N,N))
        Simulation = ExamLattice2D(D, q, p, N, dx, dt, a_start, b_start, c_start)
        absorption_time = Simulation.absorption_time()
        if not np.isnan(absorption_time):
            absorption_times.append(absorption_time)

    # Write data to csv file
    csvtitle = ['# Absorption times']
    csvheader = ['Absorption times']
    # Transpose to sort data into columns
    csvdata = np.array([absorption_times]).T
    with open('Absorption_times.csv', 'w', encoding='UTF8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(csvtitle)
        writer.writerow(csvheader)
        writer.writerows(csvdata)

    print('Mean absorption time =', np.mean(np.array(absorption_times)))
    print('Standard error of the mean =', np.std(np.array(absorption_times), ddof=1) / np.sqrt(10))

main()


#Christian Wells
#IMAPS 2013
#
#Name: Move folder from QC to Digitize
#
#Purpose: This tool uses the arcpy and os module to copy directories
#from QC to digitize and populate with a GDB for digitizing. The GDB
#is given the ID Sort of the map.

import arcpy
import os

workpath = arcpy.GetParameterAsText(0)
workpathList = workpath.split(";")
newWorkpath = arcpy.GetParameterAsText(1)
fileGDB = r"M:\gis\Geodatabases\_GIS_Map_Template.gdb"

for path in workpathList:
    try:
        arcpy.AddMessage("Processing: " + path)
        folderName = os.path.basename(path)
        gdbName = os.path.basename(fileGDB)
        arcpy.Copy_management(str(path), os.path.join(newWorkpath, folderName))
        arcpy.AddMessage("  Copy of " + path + " successful")
        arcpy.AddMessage("  Deleting: " + path)
        arcpy.Delete_management(str(path))
        arcpy.AddMessage("  Removal of " + path + " successful")
        arcpy.AddMessage("  Creating file geodatabse for map " + folderName)
        arcpy.Copy_management(fileGDB, os.path.join(newWorkpath, folderName, str(folderName + gdbName)))
        arcpy.AddMessage("  File GDB creation for map " + folderName + " successful")
    except:
        arcpy.AddMessage("Copy of " + folderName + " did not succeed!")

del path


import argparse
import requests

BOT_USERNAME = 'openshift-crt-jira-release-controller'
JIRA_URL = 'https://issues.redhat.com/'

def main():
    parser = argparse.ArgumentParser(description="JIRA Issue Comment Management CLI Tool")
    parser.add_argument('--api-token', required=True, help="JIRA API Token")
    parser.add_argument('--jql', required=True, help="JQL Query")
    parser.add_argument('--target-comment', required=True, help="Target Comment")
    parser.add_argument('--sub-target-comment', required=True, help="Sub-Target Comment")
    args = parser.parse_args()

    API_TOKEN = args.api_token
    JQL_QUERY = args.jql
    TARGET_COMMENT = args.target_comment
    SUB_TARGET_COMMENT = args.sub_target_comment

    api_url = f'{JIRA_URL}/rest/api/2/search'

    headers = {
        'Authorization': f'Bearer {API_TOKEN}'
    }

    start_at = 0
    max_results = 1000
    total_results = max_results  # Set an initial value greater than max_results to enter the loop

    while start_at < total_results:
        payload = {
            'jql': JQL_QUERY,
            'fields': 'key,comment',
            'expand': 'comments',
            'startAt': start_at,
            'maxResults': max_results
        }

        response = requests.get(api_url, params=payload, headers=headers)

        if response.status_code == 200:
            data = response.json()
            total_results = data.get('total', 0)
            issues = data.get('issues', [])

            for issue in issues:
                comments = issue.get('fields', {}).get('comment', {}).get('comments', [])
                comment_count = sum(1 for comment in comments if TARGET_COMMENT in comment.get('body', ''))

                if comment_count > 1:
                    issue_comment_counts[issue['key']] = comment_count

            print("Issues with the target comment appearing more than once:")
            for issue_key, comment_count in issue_comment_counts.items():
                print(f"Issue: {issue_key}, Comment Count: {comment_count}")

            for issue_key, comment_count in issue_comment_counts.items():
                if comment_count > 1:
                    delete_comments(issue_key, headers, SUB_TARGET_COMMENT)

            start_at += max_results

        else:
            print(f"Failed to retrieve data. Status code: {response.status_code}")
            break

def delete_comments(issue_key, headers, SUB_TARGET_COMMENT):
    issue_url = f'{JIRA_URL}/rest/api/2/issue/{issue_key}?fields=comment'
    response = requests.get(issue_url, headers=headers)

    if response.status_code == 200:
        issue_data = response.json()
        issue_comments = issue_data.get('fields', {}).get('comment', {}).get('comments', [])
        matching_401_comments = [
            comment for comment in issue_comments
            if SUB_TARGET_COMMENT in comment.get('body', '') and comment.get('author', {}).get('name') == BOT_USERNAME
        ]

        for matching_comments in matching_401_comments:
            comment_id = matching_comments.get('id')
            comment_delete_url = f'{JIRA_URL}/rest/api/2/issue/{issue_key}/comment/{comment_id}'
            response = requests.delete(comment_delete_url, headers=headers)
            if response.status_code == 204:
                print(f"Deleted comment {comment_id} from issue {issue_key}")
            else:
                print(
                    f"Failed to delete comment {comment_id} from issue {issue_key}. Status code: {response.status_code}")
    else:
        print(f"Failed to fetch issue {issue_key} details. Status code: {response.status_code}")

if __name__ == "__main__":
    issue_comment_counts = {}
    main()


from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name='home'),  # Página inicial
    path('room/<str:room>/', views.room, name='room'),  # Página da sala
    path('checkviews/', views.checkviews, name='checkviews'),  # Verificação de visualizações
    #path('checkview', views.checkview, name='checkview')
]


arr = []
with open("mc5/log.mcnvt.txt") as f:
    for line in f:
        #print(float(x) for x in coloumn.split())
        arr.append(line.split())

probs = []
for i in range(74, 114):
    probs.append(float(arr[i][6]))

prob = 0.0
idx = 0
for k in probs:
	prob+= k
	idx += 1

print(prob/idx)


# Actual learning model using the data from "Output12.npy"
# To make predictions of human randomness.

import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn. model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
from HuRa import randomLister

choice = list(np.linspace(1, 100, num = 20))
dataGen = randomLister(4)
dataGen.takeinputs(choice, 10)
dataGen.save()
_hold = np.logical_or(np.logical_or(dataGen.data == "yes", dataGen.data == "no"), dataGen.data== "nho")
tags = np.reshape(np.extract(_hold, dataGen.data), [-1, 1])
data = np.extract(np.logical_not(_hold), dataGen.data)
data2 = []

for i in data:
    data2.append(np.array(i))

tags[tags == "yes"] = 1
tags[tags == "no"] = 0
tags[tags == "nho"] = 0
tags = tags.astype("int")

model = MLPClassifier(hidden_layer_sizes=(100, 100, 10), max_iter=100)

model.fit(data2, tags.ravel())
for i in range(5):
    new_data = dataGen.createarraynoin(choice, 1)
    for i in model.predict(new_data):
        if i == 1:
            print("Looks humanly random")
        elif i == 0:
            print("Doesn't look humanly random")


import asyncio
import logging
import sys
from typing import Dict, List

from akross.connection.aio.quote_channel import QuoteChannel, Market
from akross.common import enums, util
from akrossworker.common.candle_cache import CandleCache
from akrossworker.common.command import ApiCommand
from akrossworker.common.db import DBEnum, Database
from akrossworker.common.protocol import SymbolInfo
from akross.common import aktime
from akross.rpc.base import RpcBase
from akrossworker.common.args_constants import ApiArgKey as Args
from akrossworker.common.args_constants import TickTimeType
from akrossworker.cybos.candle_cache import CybosCandleCache
from akross.common import env
from datetime import datetime

LOGGER = logging.getLogger(__name__)
MARKET_NAME = 'krx.spot'
FAVORITE_COLLECTION = 'favorite'
GROUP_COLLECTION = 'group'

class CybosRestCache(RpcBase):
    def __init__(self):
        super().__init__()
        self.candle = self.on_candle
        self.symbolInfo = self.on_symbol_info
        self.orderbook = self.on_orderbook
        self.search = self.on_search
        self.krxMomentRank = self.on_krx_moment_rank
        self.krxPick = self.on_krx_pick
        self.favorite = self.on_favorite
        self.setFavorite = self.on_set_favorite
        self.group = self.on_group
        self.set_group = self.on_set_group

        self._worker: Market = None
        self._symbols: Dict[str, CandleCache] = {}
        self._symbol_info_cache: Dict[str, SymbolInfo] = {}
        self._conn = QuoteChannel(MARKET_NAME)
        self._db = Database()

    async def preload(self):
        asyncio.create_task(self._check_exit())
        await self._conn.connect()
        await self._conn.market_discovery()
        await self._conn.wait_for_market(MARKET_NAME)
        krx = self._conn.get_markets(MARKET_NAME)
        if krx is None:
            LOGGER.error('cannot find market')
            sys.exit(1)
        self._worker = krx[0]
        await self.regist_symbols()

    async def _check_exit(self):
        prev = datetime.now()
        while True:
            now = datetime.now()
            if prev.hour == 4 and now.hour == 5:
                LOGGER.error('turn off cache')
                sys.exit(0)
            prev = datetime.now()
            await asyncio.sleep(60)

    async def regist_symbols(self) -> None:
        _, symbols = await self._conn.api_call(
            self._worker, ApiCommand.SymbolInfo, cache=False)
        if not isinstance(symbols, list) or len(symbols) == 0:
            LOGGER.error('no symbols')
            sys.exit(1)

        for i, symbol in enumerate(symbols):
            symbol_info = SymbolInfo.CreateSymbolInfo(symbol)
            LOGGER.info('progress(%s) %d/%d', symbol_info.symbol, i+1, len(symbols))
            if symbol_info.symbol not in self._symbols:
                symbol_name = symbol_info.symbol.lower()
                self._symbol_info_cache[symbol_name] = symbol_info
                self._symbols[symbol_name] = \
                    CybosCandleCache(
                        self._db, DBEnum.KRX_QUOTE_DB,
                        self._conn, self._worker, symbol_info)
                await self._symbols[symbol_info.symbol.lower()].run()

    async def on_orderbook(self, **kwargs):
        util.check_required_parameters(kwargs, 'symbol')
        # limit to 20 / 20 for orderbook
        LOGGER.warning('on_orderbook %s', kwargs)
        return []

    async def on_candle(self, **kwargs):
        util.check_required_parameters(kwargs, 'symbol', 'interval')
        interval = kwargs['interval']
        symbol = kwargs['symbol'].lower()
        if symbol in self._symbols:
            return self._symbols[symbol].get_data(interval)
        return []

    def get_market_type(self) -> str:
        now = aktime.get_msec()
        hmsec = aktime.interval_type_to_msec('h')
        mmsec = aktime.interval_type_to_msec('m')
        day_start = aktime.get_start_time(now, 'd', 'KRX')
        if now < day_start + aktime.interval_type_to_msec('h') * 9:
            return TickTimeType.PreBid
        elif now >= day_start + hmsec * 15 + mmsec * 20:
            return TickTimeType.MarketCloseBid
        return TickTimeType.Normal

    async def on_krx_moment_rank(self, **kwargs):
        candidates = []
        for symbol, cache in self._symbols.items():
            candles = cache.get_interval_type_data('m')
            market_type = self.get_market_type()
            if (len(candles) > 0 and symbol in self._symbol_info_cache):
                try:
                    amount = cache.get_amount(market_type)
                    market_cap = int(self._symbol_info_cache[symbol].market_cap)
                except Exception:
                    continue

                if market_cap <= 0:
                    continue

                ratio = amount / market_cap
                candidates.append({'symbol_info': self._symbol_info_cache[symbol],
                                   'ratio': ratio})
        candidates.sort(key=lambda candidate: candidate['ratio'], reverse=True)
        candidates = candidates[:15]
        return [candidate['symbol_info'].to_network() for candidate in candidates]

    async def on_krx_pick(self, **kwargs):
        candidates = []
        for symbol, cache in self._symbols.items():
            if symbol in self._symbol_info_cache:
                score = cache.get_triangle_score()
                if score > 0:
                    candidates.append({
                        'symbol_info': self._symbol_info_cache[symbol],
                        'score': score
                    })
        candidates.sort(key=lambda candidate: candidate['score'], reverse=True)
        candidates = candidates[:15]
        return [candidate['symbol_info'].to_network() for candidate in candidates]

    async def on_symbol_info(self, **kwargs):
        result = []
        for symbol in self._symbols.values():
            result.append(symbol.get_symbol_info().to_network())
        return result

    async def on_favorite(self, **kwargs):
        util.check_required_parameters(kwargs, 'user')
        data = await self._db.find_one(
            DBEnum.FAVORITE_DB,
            FAVORITE_COLLECTION,
            {'user': kwargs['user']})
        symbol_infos: List[SymbolInfo] = []
        if data is not None and 'symbols' in data:
            symbol_infos = data['symbols']
        return symbol_infos

    async def on_group(self, **kwargs):
        return []

    async def on_set_group(self, **kwargs):
        return {}

    async def on_set_favorite(self, **kwargs):
        util.check_required_parameters(kwargs, 'symbols', 'user')
        await self._db.upsert_data(
            DBEnum.FAVORITE_DB,
            FAVORITE_COLLECTION,
            {'user': kwargs['user']},
            {'symbols': kwargs['symbols']}
        )
        return {}

    async def on_search(self, **kwargs):
        LOGGER.info(
            'keyword(%s), sectors(%s)',
            kwargs[Args.KEYWORD] if Args.KEYWORD in kwargs else 'no keyword',
            kwargs[Args.SECTORS] if Args.SECTORS in kwargs else 'no sectors'
        )
        result = []
        for symbol in self._symbols.values():
            if symbol.symbol_matched(**kwargs):
                result.append(symbol.get_symbol_info().to_network())
        return result

async def main() -> None:
    LOGGER.warning('run rest provider')
    conn = QuoteChannel(MARKET_NAME, env.get_rmq_url())

    rest_provider = CybosRestCache()
    await rest_provider.preload()
    await conn.connect()
    await conn.run_with_bus_queue(enums.WorkerType.Cache, rest_provider)
    await asyncio.get_running_loop().create_future()

if __name__ == '__main__':
    import logging
    LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
                  '-35s %(lineno) -5d: %(message)s')
    logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
    asyncio.run(main())


from enum import Enum

class OfferEvent(Enum):
    """Events emitted by the Offer."""
    DELETED = 1
    ACCEPTED = 2

class MarketEvent(Enum):
    """Events emitted by the market classes."""
    OFFER = 1
    OFFER_SPLIT = 4
    OFFER_DELETED = 2
    OFFER_TRADED = 3
    BID = 12
    BID_TRADED = 5
    BID_DELETED = 6
    BID_SPLIT = 7
    BALANCING_OFFER = 8
    BALANCING_OFFER_SPLIT = 9
    BALANCING_OFFER_DELETED = 10
    BALANCING_TRADE = 11

class AreaEvent(Enum):
    """Events emitted by the Area class."""
    TICK = 1
    MARKET_CYCLE = 2
    BALANCING_MARKET_CYCLE = 3
    ACTIVATE = 4


import os
import os.path
import time
import logging
from rasterio.features import Affine
import fiona
import math
import numpy as np
from pathlib import Path
from numpy.core.numeric import ones
from shapely.geometry import polygon
from shapely.geometry.geo import box
from shapely.geometry.point import Point
from shapely.geometry.polygon import Polygon
from tqdm import tqdm
from glob import glob
import pandas as pd
import geopandas as gpd
import rasterio
import rasterio.features
from rasterio import windows
from shapely.geometry import shape
import matplotlib.pyplot as plt
from joblib import Parallel, delayed
from src import utils, center_separation

def get_shape_transform_in_original_resolution(pbounds, im_transform):
    wn = windows.from_bounds(*pbounds, im_transform)
    wnt = windows.transform(wn, im_transform)
    wn_shape = windows.shape(wn)
    wns = (round(wn_shape[0]), round(wn_shape[1]))  # Convert to int
    return (wns, wnt)

def polygon_center_separation(polygon, poly_shape, poly_transform, max_filter_size):
    poly_tf = Affine(*poly_transform[:6])
    pimg = rasterio.features.rasterize([polygon], out_shape=poly_shape, all_touched=False, transform=poly_tf)
    _, _, final_image = center_separation.separate_objects(pimg, max_filter_size, centers_only=False)

    final_image = final_image.astype(np.uint8)

    dp = []
    for feature, _ in rasterio.features.shapes(final_image, mask=final_image, connectivity=4, transform=poly_tf):
        dp.append(shape(feature))

    if len(dp) == 1:
        return [polygon]
    else:
        return dp

def polygon_center_separation_with_area_approximation(
        polygon, polygon_area, poly_shape, poly_transform, max_filter_size):
    poly_tf = Affine(*poly_transform[:6])

    pimg = rasterio.features.rasterize([polygon], out_shape=poly_shape, transform=poly_tf)
    _, final_image, _ = center_separation.separate_objects(pimg, max_filter_size, centers_only=True)
    # Get the centers and the corresponding
    if np.count_nonzero(final_image) == 1:
        return [{
            'geometry': polygon.centroid,
            'approximate_area': polygon_area,
            'approximate_area_proportion': 1,
        }]

    dp = []
    rows, cols = np.nonzero(final_image)
    x, y = rasterio.transform.xy(poly_tf, rows, cols)  # rowcol to xy
    tr = np.sum(np.square(final_image))
    for i in range(len(rows)):  # assuming they are in image coordinates
        pr = np.square((final_image[rows[i], cols[i]])) / tr
        dc = {
            'geometry': Point(x[i], y[i]),
            'approximate_area': polygon_area * pr,
            'approximate_area_proportion': pr
        }
        dp.append(dc)

    return dp

def get_original_raster_transform(corresponding_raster_table, fbounds):
    xmin, ymin, xmax, ymax = fbounds
    overlapping_images = corresponding_raster_table.cx[xmin:xmax, ymin:ymax]
    if len(overlapping_images) == 0:
        raise ValueError(f"No overlapping image found for {fbounds}")
    if len(overlapping_images) > 1:
        logging.error("Multiple images overlap with this area.")
        logging.error(f"Overlapping_images: {overlapping_images[['image_id', 'geometry']]}")
        logging.error(f"This may lead to problems, please double check.\n Proceeding with the first overlap for now.")

    overlapping_images = overlapping_images.iloc[0]
    return overlapping_images['transform'], overlapping_images['img_crs']

# Alternate method for getting the raster transform
# def get_original_raster_transform(corresponding_raster_table, fbounds):
#     bbx = box(*fbounds)
#     a_df = gpd.GeoDataFrame(gpd.GeoSeries(bbx), columns=['geometry'])
#     a_df = a_df.set_crs(corresponding_raster_table.crs)
#     overlapping_images = gpd.overlay(corresponding_raster_table, a_df, how='intersection')
#     if len(overlapping_images) == 0:
#         raise ValueError(f"No overlapping image found for {fbounds}")
#     if len(overlapping_images) > 1:
#         logging.error("Multiple images overlap with this area.")
#         logging.error(f"Overlapping_images: {overlapping_images[['image_id', 'geometry']]}")
#         logging.error(f"This may lead to problems, please double check.\n Proceeding with the first overlap for now.")

#     overlapping_images = overlapping_images.iloc[0]

#     return overlapping_images['transform'], overlapping_images['img_crs']

def get_transform_from_resolution(resolution_per_pixel, fbounds):
    xmin, ymin, _, _ = fbounds

    af = np.zeros((2, 3))  # Double check
    af[0, 1] = resolution_per_pixel[0]
    af[1, 0] = resolution_per_pixel[1]
    af[0, 2] = ymin
    af[1, 2] = xmin
    af = Affine(*af.flatten())
    return af

def polygon_file_processing(
        polygons_file, bbox, rows_slice, corresponding_raster_table, resolution_per_pixel, min_size, max_filter_size,
        only_approximate_area, area_preserving_crs='EPSG:6933', pbar=None):
    """
    Start with a vector file
    Iterate over all polygons (can be parallelized)
    Rasterize individually
    Split the rasterized polygons - either using erosion or center based approach
    Repolygonize
    Write to file
    """
    time1 = time.time()
    polygons = gpd.read_file(polygons_file, bbox=bbox, rows=rows_slice)
    ori_crs = polygons.crs
    time2 = time.time()
    logging.info(f"Read {polygons_file} in {time2 -time1} seconds")

    if len(polygons) == 0:
        logging.error(f"The read files contain no polygons in the given bounds: {bbox}")
        logging.error(f"Returning empty dataframe!")
        fdf = gpd.GeoDataFrame({"geometry": []})
        fdf.set_geometry(col='geometry', inplace=True, crs=ori_crs)
        return fdf, ori_crs

    if corresponding_raster_table is not None:
        ori_raster_tranform, ori_raster_crs = get_original_raster_transform(
            corresponding_raster_table, polygons.total_bounds)
    else:
        ori_raster_crs = polygons.crs
        ori_raster_tranform = get_transform_from_resolution(resolution_per_pixel)

    polygons[['minx', 'miny', 'maxx', 'maxy']] = polygons.bounds
    polygons['shape_and_transform'] = polygons.apply(lambda x: get_shape_transform_in_original_resolution(
        [x['minx'], x['miny'], x['maxx'], x['maxy']], ori_raster_tranform), axis=1)
    polygons['shape'], polygons['transform'] = zip(*polygons['shape_and_transform'])
    polygons['width'], polygons['height'] = zip(*polygons['shape'])

    small_polygons = polygons[(polygons["width"] * polygons["height"] <= min_size)]
    large_polygons = polygons[(polygons["width"] * polygons["height"] > min_size)]

    logging.info(f"""Filtered small polygons in {(time.time() -time2)} seconds.
    \nNow processing {len(large_polygons)} polygons larger than the size threshold.""")

    if only_approximate_area:
        small_polygons = small_polygons.to_crs(area_preserving_crs)
        # It's the actual area but for consistency we call it "approximation"
        small_polygons['approximate_area'] = small_polygons.area
        small_polygons = small_polygons.to_crs(ori_crs)
        small_polygons['geometry'] = small_polygons.centroid
        small_polygons['approximate_area_proportion'] = 1
        final_polygons = small_polygons[['geometry', 'approximate_area',
                                         'approximate_area_proportion']].to_dict('records')
        if pbar is not None:
            pbar.update(len(small_polygons))

        large_polygons = large_polygons.to_crs(area_preserving_crs)
        large_polygons['approximate_area'] = large_polygons.area
        large_polygons = large_polygons.to_crs(ori_crs)

        for _, row in large_polygons.iterrows():  # , total=len(large_polygons)):
            new_polys_with_areas = polygon_center_separation_with_area_approximation(
                row['geometry'], row['approximate_area'], row['shape'], row['transform'],  max_filter_size)
            final_polygons.extend(new_polys_with_areas)
            if pbar is not None:
                pbar.update(1)
        fdf = gpd.GeoDataFrame(final_polygons)
        fdf.set_geometry(col='geometry', inplace=True, crs=ori_crs)
    else:
        final_polygons = list(small_polygons['geometry'])  # Add small polygons directly
        if pbar is not None:
            pbar.update(len(small_polygons))

        for _, row in large_polygons.iterrows():  # , total=len(large_polygons)):  # Split large polygons and add them
            new_polys = polygon_center_separation(row['geometry'], row['shape'], row['transform'], max_filter_size)
            final_polygons.extend(new_polys)
            if pbar is not None:
                pbar.update(1)
        fdf = gpd.GeoDataFrame({"geometry": final_polygons})
        fdf.set_geometry(col='geometry', inplace=True, crs=ori_crs)
        fdf = fdf.to_crs(area_preserving_crs)
        fdf['area'] = fdf.area
        fdf = fdf.to_crs(ori_crs)
    return fdf, ori_crs

def divide_bounds(bounds, cpu, total_polygons):
    left, bottom, right, top = bounds
    dbounds = []
    if cpu <= 1:
        return [bounds]
    else:  # Divide along x and y
        # It's better to divide into a large number so that if a threads get a dense area than others can finish the rest in the meantime
        xf = int(math.log(total_polygons) / 4)
        if xf < 4:
            xf = 4
        cpu = cpu * xf
        logging.info(f"Dividing the bounds into {cpu} parts, xf: {xf} ")
        cpu_i = int(math.sqrt(cpu))
        cpu_j = int(cpu / cpu_i)
        dx = (right - left) / (cpu_i)
        dy = (top - bottom) / (cpu_j)
        logging.info(f"File bounds; left, bottom, right, top: {left, bottom, right, top}")
        logging.info(f"Divisions of the file along x axis {cpu_i} and along y axis{cpu_j}")
        logging.info(f"dx along x and y axis: {dx} and {dy}")
        for i in range(cpu_i):
            for j in range(cpu_j):
                lst = (left + (i*dx))
                # Make sure that we don't miss anything for numeric reasons
                lend = (left + ((1+i)*dx)) if i != cpu_i - 1 else right
                bst = (bottom + (j*dy))
                bend = (bottom + ((1+j)*dy)) if j != cpu_j - 1 else top
                assert lst != lend and bst != bend
                dbounds.append((lst, bst, lend, bend))
    return tuple(dbounds)

def divide_count(cpu, total_polygons):
    if cpu <= 1:
        return ((0, total_polygons))
    else:  # Divide along x and y
        cpu = cpu * 2  # Create more splits for efficiency
        logging.info(f"Dividing the total count into {cpu} (+1)  parts, xf: {2} ")

        # Split instances for parallel processing; 0 - background is ignored
        splits = np.arange(0, total_polygons, int(total_polygons/cpu))
        indx_s = [(splits[i-1], splits[i]) for i in range(1, len(splits))]
        if splits[-1] < total_polygons:  # The likely case
            indx_s.append((splits[-1], total_polygons))
    return tuple(indx_s)

def multi_thread_file_processing(file, file_type, output_dir, corresponding_raster_table, resolution_per_pixel,
                                 min_size, max_filter_size, only_approximate_area, cpu, force_overwrite):
    fpf = fiona.open(file)
    total_polygons = len(fpf)
    print(f'Analysing {file} with {total_polygons} polygons')
    logging.info(f'Analysing {file} with {total_polygons} polygons')

    ofn = 'centers_approx_' if only_approximate_area else ''
    cfn = f"cpu_{cpu}_"
    out_file = f"{output_dir}/separated_{ofn}{cfn}{file.split('/')[-1]}"
    if os.path.isfile(out_file):
        if not force_overwrite:
            logging.info(f'{out_file} already exists. Skipping it.')
            return
        else:
            logging.info(f'Overwriting existing file {out_file}.')
            os.remove(out_file)

    # The following can be used for dividing the image along bounds;
    # Bounds can lead to duplicates due to "intersection" with bounds and also results in the following:
    ## """RuntimeWarning: Sequential read of iterator was interrupted. Resetting iterator. This can negatively impact the performance."""
    # which is the main reason why reading a large file is slow.
    # total_bounds = fpf.bounds
    # dbounds = divide_bounds(total_bounds, cpu, total_polygons)
    # dbbx = [box(*b) for b in dbounds]

    dindexes = divide_count(cpu, total_polygons)
    dslices = [slice(*di) for di in dindexes]
    df_collection = []
    file_crs = None

    with utils.tqdm_joblib(tqdm(desc="Subset progress", total=len(dslices))) as progress_bar:
        processing_result = Parallel(n_jobs=cpu)(
            delayed(polygon_file_processing)
            (file, None, di, corresponding_raster_table, resolution_per_pixel, min_size, max_filter_size,
                only_approximate_area)
            for di in dslices
        )
        # Single thread equivalent; Useful for debugging
        # for db in dbbx:
        #     processing_result = polygon_file_processing(file, db, None, corresponding_raster_table, resolution_per_pixel,
        #                                             min_size, max_filter_size, only_approximate_area)

    for p in processing_result:
        df_collection.append(p[0])
        if file_crs is None:
            file_crs = p[1]

    processed_df = pd.concat(df_collection, ignore_index=True, sort=False)
    processed_df.drop_duplicates(subset=None, keep='first', inplace=True, ignore_index=True)

    print(f'Writing the separated polygons to {out_file}')
    logging.info(f'Writing the separated polygons to {out_file}')
    driver = utils.get_vector_driver(file_type)
    processed_df.to_file(out_file, driver=driver, crs=file_crs, layer="trees")

def single_thread_file_processing(file, file_type, output_dir, corresponding_raster_table, resolution_per_pixel,
                                  min_size, max_filter_size, only_approximate_area, force_overwrite):
    fpf = fiona.open(file)
    total_polygons = len(fpf)
    print(f'Analysing {file} with {total_polygons} polygons')
    logging.info(f'Analysing {file} with {total_polygons} polygons')

    ofn = 'centers_approx_' if only_approximate_area else ''
    out_file = f"{output_dir}/separated_{ofn}{file.split('/')[-1]}"
    if os.path.isfile(out_file):
        if not force_overwrite:
            logging.info(f'{out_file} already exists. Skipping it.')
            return
        else:
            logging.info(f'Overwriting existing file {out_file}.')
            os.remove(out_file)

    with tqdm(total=total_polygons) as pbar:
        processed_df, file_crs = polygon_file_processing(
            file, None, None, corresponding_raster_table, resolution_per_pixel, min_size, max_filter_size,
            only_approximate_area, pbar=pbar)
    processed_df.drop_duplicates(subset=None, keep='first', inplace=True, ignore_index=True)

    print(f'Writing the separated polygons to {out_file}')
    logging.info(f'Writing the separated polygons to {out_file}')
    driver = utils.get_vector_driver(file_type)
    processed_df.to_file(out_file, driver=driver, crs=file_crs, layer="trees")

def separate_instances_in_dir(input_dir, file_prefix, file_type,
                              corresponding_raster_dir, corresponding_raster_type, resolution_per_pixel,
                              output_dir, min_size, max_filter_size, only_approximate_area, cpu, force_overwrite):

    files = glob(f"{input_dir}/{file_prefix}*{file_type}")
    if len(files) == 0:
        raise Exception('No files found in the specified folder!')
    Path(output_dir).mkdir(parents=True, exist_ok=True)
    assert corresponding_raster_dir is not None or resolution_per_pixel is not None

    if corresponding_raster_dir is not None:
        corresponding_raster_table = utils.build_images_table(
            base_path=corresponding_raster_dir, image_file_type=corresponding_raster_type)
    else:
        corresponding_raster_table = None

    for file in files:
        if cpu == 1 or cpu == 0:
            single_thread_file_processing(file, file_type, output_dir, corresponding_raster_table, resolution_per_pixel,
                                          min_size, max_filter_size, only_approximate_area, force_overwrite)
        else:
            multi_thread_file_processing(file, file_type, output_dir, corresponding_raster_table, resolution_per_pixel,
                                         min_size, max_filter_size, only_approximate_area, cpu, force_overwrite)

# Known issue; logging does not work with joblib parallel
# Possible workaround: https://github.com/joblib/joblib/issues/1017
if __name__ == '__main__':
    args = utils.get_args('polygon_separation')
    logs_file = utils.initialize_log_dir(args.log_dir)
    print(f'Writing logs to {logs_file}')
    cpu_count = utils.get_cpu_count(args.cpu)
    separate_instances_in_dir(
        args.input_dir, args.file_prefix, args.file_type, args.corresponding_raster_dir, args.corresponding_raster_type,
        args.resolution_per_pixel, args.output_dir, args.min_size, args.max_filter_size, args.only_approximate_area,
        cpu_count, args.force_overwrite)

# Read the bounds
# Split the bounds depending upon the number of processors
# Read polygons for each bound in parallel
# Split the polygons
# Write to file


from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta

#문제(1) 영화제목 '매트릭스'의 평점을 가져오기
#풀이
movie_name = db.movies.find_one({'title':'매트릭스'},{'_id':False})
# print(movie_name['star'])
#정답
# target_movie = db.movies.find_one({'title':'매트릭스'})
# print (target_movie['star'])
#평점만 불러올거라 아이디값을 안 불러온다는 명령자체가 불필요.

#문제(2) '매트릭스'의 평점과 같은 평점의 영화 제목들을 가져오기
for same_star in list(db.movies.find({'star':movie_name['star']})):
    print(same_star['title'])
db.movies.update_one({'title':'매트릭스'},{'$set':{'star':0}})


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import Grid

my_batch = 20

class Dataset:
    def __init__(self,data,label,batchsize):
        self.dataset=data
        self.labelset=label
        self.tag=0
        self.len=len(data)
        self.batchsize=batchsize
    def nextbatch(self):
        if self.tag+self.batchsize>=self.len:
            self.tag=0
        else:
            self.tag+=self.batchsize
        return self.dataset[self.tag:self.tag+self.batchsize], self.labelset[self.tag:self.tag+self.batchsize]

test, testlabel = Grid.makeBatch("/test_data_path")
dataset2 = Dataset(test, testlabel, my_batch)

'''DEFINE PARAMENTS INITIAL FUNCTION'''
def weight_variable(shape, name):
    initial = tf.truncated_normal(shape, stddev=0.1, name=name)
    return tf.Variable(initial)
def bias_variable(shape, name):
    initial = tf.constant(0.1, shape=shape, name=name)
    return tf.Variable(initial)
def conv2d(x, W):
    return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME')
def max_pool_2x2x2(x):
    return tf.nn.max_pool3d(x, ksize=[1, 2, 2, 2, 1],
                            strides=[1, 2, 2, 2, 1], padding='SAME')

'''POOL LAYER1'''
x_image = tf.placeholder(tf.float32, [my_batch, 48, 48, 48, 32])
x_resh = tf.reshape(x_image, [my_batch, 48, 48, 48, 32])  # N_a_a_a_C
unit1_pool = max_pool_2x2x2(x_resh)  # 24*24*24*32
'''CONV3D LAYER2'''
unit2_conv_W = weight_variable([5, 5, 5, 32, 64], name="unit2_conv_W")
unit2_conv_B = bias_variable([64], name="unit2_conv_B")
unit2_conv_H = tf.nn.relu(conv2d(unit1_pool, unit2_conv_W) + unit2_conv_B)
'''POOL LAYER3'''
unit3_pool = max_pool_2x2x2(unit2_conv_H)  # 12*12*12*64
'''CONV3D LAYER4'''
unit4_conv_W = weight_variable([5, 5, 5, 64, 128], name="unit4_conv_W")
unit4_conv_B = bias_variable([128], name="unit4_conv_B")
unit4_conv_H = tf.nn.relu(conv2d(unit3_pool, unit4_conv_W) + unit4_conv_B)
'''POOL LAYER5'''
unit5_pool = max_pool_2x2x2(unit4_conv_H)  # 6*6*6*128
'''CONV3D LAYER6'''
unit6_conv_W = weight_variable([5, 5, 5, 128, 32], name="unit6_conv_W")
unit6_conv_B = bias_variable([32], name="unit6_conv_B")
unit6_conv_H = tf.nn.relu(conv2d(unit5_pool, unit6_conv_W) + unit6_conv_B)
'''FULL LAYER7'''
unit7_full_flat = tf.reshape(unit6_conv_H, shape=[-1, 6 * 6 * 6 * 32])
unit7_full_W = weight_variable([6 * 6 * 6 * 32, 2], name="unit7_full_W")
unit7_full_B = bias_variable([2], name="unit7_full_B")
y_conv = tf.nn.softmax(tf.matmul(unit7_full_flat, unit7_full_W) + unit7_full_B)
y_ = tf.placeholder(tf.float32, [my_batch, 2])

'''MODEL TRAINING OPS OBJECTS'''
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4, ).minimize(cross_entropy)
correct_prediction = tf.equal(tf.arg_max(y_conv, 1), tf.arg_max(y_, 1))

'''ADD ops to save and restore all the variables.'''
saver = tf.train.Saver()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
    saver.restore(sess, "tmpCNN3D/model.ckpt")
    print("Model restored.")
    data_test, label_test = dataset2.nextbatch()
    score = y_conv.eval(feed_dict={x_image: data_test, y_: label_test})
    print("predict score of proteins:")
    print(score)


from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0009_startup_email'),
    ]

    operations = [
        migrations.AddField(
            model_name='startup',
            name='bio',
            field=models.TextField(blank=True, null=True),
        ),
    ]


from django.urls import path
from.import views

urlpatterns=[
    #signup url---->>>>
    path('signup/', views.SignUpAPI.as_view(),name='Signup'),

    path('POST/api/posts/',views.PostCreateView.as_view()),
    path('GET/api/posts/', views.PostListView.as_view()),
    path('GET/api/posts/<int:pk>/', views.PostRetrieveView.as_view()),
    path('GET/api/posts/all/',views.PostListAllView.as_view()),
    path('PUT/api/posts/<int:pk>/', views.PostUpdateView.as_view()),
    path('DELETE/api/posts/<int:pk>/', views.PostDeleteView.as_view()),
    path('GET/api/posts/search/', views.PostSearchList.as_view(), name='post-search'),

]

#filter------->>
#http://127.0.0.1:8000/project1/GET/api/posts/search/?body=mmm

#Order by ID------>>Reverse
#http://127.0.0.1:8000/project1/GET /api/posts/all/?ordering=-id


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('home',views.home, name='home'),
    path('submit_appraisal_application',views.submit_appraisal_application,name='submit_appraisal_application'),
    path('add_leave_record',views.add_leave_record,name='add_leave_record'),
    path('add_appraisal',views.add_appraisal,name='add_appraisal'),
    path('mark_attendance',views.mark_attendance,name='mark_attendance'),
    path('get_approved_application',views.get_approved_application,name='get_approved_application'),
    path('card_upload',views.card_upload,name='card_upload'),
    path('signup/', views.SignUp.as_view(), name='signup'),
    # path('signup/', views.SignUp.as_view(), name='signup'),
]


def TSLoader(x,y, valid_size =0.3,stratify = False , shuffle = False,random_state =23, train_batch =128,val_batch =128,num_worker =0):
    from tsai.all import to3d, get_splits,plot_splits
    from tsai.all import TSDatasets,TSDataLoaders,Categorize,TSStandardize
    import sklearn.metrics as skm
    import numpy as np
    import logging
    import matplotlib.pyplot as plt

    x = x
    y = y

    X_3d = to3d(x)
    y = y.squeeze()

    # To logging splits , change get_splits -> plot_splits -> add plt.savefig
    splits = get_splits(y.squeeze(), valid_size=valid_size, stratify=stratify, random_state=random_state, shuffle=shuffle )
    print('DataLoading-----------------------------------------------')
    plt.savefig('/home/yunkwan/project/AI_UWB/log/split.png')
    plt.close()

    train_idx , valid_idx = splits
    valid_idx

    tfms  = [None, [Categorize()]]
    dsets = TSDatasets(X_3d, y, tfms= tfms, splits=[train_idx,valid_idx], inplace=True)
    dsets

    dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=[train_batch, val_batch], batch_tfms=[TSStandardize()], num_workers=num_worker)

    return dls


import os
os.system('color 3f')
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier

import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")

data = pd.read_csv('diabetes.csv')

x = data.iloc[:,[1,2,3,4,5,6,7]]
y = data.iloc[:,[8]]

model = KNeighborsClassifier()

model.fit(x,y)

print('WelCome to Diabetes Prediction Software') #greeting
speak.Speak('WelCome to Diabetes Prediction Software')

print("Enter Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age 'with comma'")
speak.Speak("Enter Glucose, BloodPressure, SkinThickness, Insulin, BMI, DiabetesPedigreeFunction, Age 'with comma'")
val = list(input().split(","))

pred = model.predict([val])
print(pred)

if pred == [1]:
    print('You Have Diabetes')
    speak.Speak('You Have Diabetes')
    print('You need to take this medicines')
    speak.Speak('You need to take this medicines')
    print('Alpha-glucosidase inhibitors, Biguanides, Dopamine agonist, DPP-4 inhibitors, Meglitinides')
    speak.Speak('Alpha-glucosidase inhibitors, Biguanides, Dopamine agonist, DPP-4 inhibitors, Meglitinides')
    print('And immediate contact to your doctor')
    speak.Speak('and immediate contact to your doctor')

else:
    print('You have not Diabetes')
    speak.Speak('You have not Diabetes')


'''
The "finally-block" is executed no matter how the Exception handler try-except block exits,
Thus having a finally block is usefull for cleaning up.
'''
import os
import sys

def make_dir_at(path, dir_name):
    '''
    Crates a directory at path, if mkdir fails to create dir_name, the program exits how it starts.
    aka. there are no unforeseen consequences, if an error is experinced in this try block.
    :param
        path: assign a path.
    :param
        dir_name: Specify a directory name, intended for creation.
    :return
        None
    '''
    original_path = os.getcwd()
    try:
        os.chdir(path)
        os.mkdir(dir_name)
    except OSError as e:
        print("Ups! Something went wrong")
        print(e, file=sys.stderr)
#        raise
    finally:
        os.chdir(original_path)

make_dir_at( "c:/" ,"woot2")

print("Program was executed fully")


from copy import deepcopy
from typing import Callable

def get_adjacent_occupants(grid: list[str], row: int, seat: int) -> int:
    count = 0
    directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for direction in directions:
        pos = (row + direction[0], seat + direction[1])
        if 0 <= pos[0] < len(grid) and 0 <= pos[1] < len(grid[0]):
                count += 1 if grid[pos[0]][pos[1]] == "#" else 0
    return count

def get_visible_occupants(grid: list[str], row: int, seat: int) -> int:
    directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    count = 0
    for direction in directions:
        multiplier = 1
        found = False
        while not found:
            y, x = row + (multiplier * direction[0]), seat + (multiplier * direction[1])
            if 0 <= y < len(grid) and 0 <= x < len(grid[0]):
                if grid[y][x] == '.':
                    multiplier += 1
                else:
                    found = True
                    count += 1 if grid[y][x] == '#' else 0
            else:
                found = True
    return count

def get_next_grid(grid: list[str], selection: Callable[[list[str], int, int], int], threshold: int) \
        -> tuple[bool, list[str]]:
    next_grid = []
    changed = False
    for row in range(len(grid)):
        new_row = ''
        for seat in range(len(grid[row])):
            if grid[row][seat] == 'L':
                if selection(grid, row, seat) == 0:
                    new_row += '#'
                    changed = True
                else:
                    new_row += 'L'
            elif grid[row][seat] == '#':
                if selection(grid, row, seat) < threshold:
                    new_row += '#'
                else:
                    new_row += 'L'
                    changed = True
            else:
                new_row += '.'
        next_grid.append(new_row)
    return changed, next_grid

def part1(grid: list[str]) -> tuple[bool, list[str]]:
    return get_next_grid(grid, get_adjacent_occupants, 4)

def part2(grid: list[str]) -> tuple[bool, list[str]]:
    return get_next_grid(grid, get_visible_occupants, 5)

def main(filename: str) -> None:
    original_grid = open(filename).read().split('\n')
    next_stage = (True, deepcopy(original_grid))
    while next_stage[0]:
        next_stage = part1(next_stage[1])
    print(sum([row.count('#') for row in next_stage[1]]))

    next_stage = (True, deepcopy(original_grid))
    while next_stage[0]:
        next_stage = part2(next_stage[1])

    print(sum([row.count('#') for row in next_stage[1]]))

if __name__ == "__main__":
    main("input.txt")


from django.conf import settings
from django.contrib import admin
from django.shortcuts import render
from django.conf.urls import url, include
from django.conf.urls.static import static

from rest_framework import routers
from rest_framework.authtoken import views as authtoken

from . import views

class CustomRouter(routers.SimpleRouter):
    def get_lookup_regex(self, viewset, lookup_prefix=''):
        lookup_field = getattr(viewset, 'lookup_field', 'pk')
        lookup_url_kwarg = getattr(viewset, 'lookup_url_kwarg', None) or lookup_field

        if not isinstance(lookup_url_kwarg, (tuple, list)):
            return super(CustomRouter, self).get_lookup_regex(viewset, lookup_prefix='')

        lookup_value_regex = getattr(viewset, 'lookup_value_regex', '[^/.]+')
        base_regex = '(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})'

        regex = []
        for lookup_kwarg, lookup_regex in zip(lookup_url_kwarg, lookup_value_regex):
            regex.append(base_regex.format(
                lookup_prefix=lookup_prefix,
                lookup_url_kwarg=lookup_kwarg,
                lookup_value=lookup_regex
            ))
        return "/".join(regex)

router = CustomRouter()
router.register(r'user', views.User)
router.register(r'definition', views.Definition)
router.register(r'test-execution', views.TestExecution)
router.register(r'test-job', views.TestJob)
router.register(r'test-result', views.TestResult)
router.register(r'issue', views.Issue)
router.register(r'issue-kind', views.IssueKind, base_name="issue-kind")
router.register(r'test-manual', views.TestManual, base_name="testmanual")

urlpatterns = [
    url(r'^$', lambda x: render(x, 'index.html')),
    url(r'^api/', include(router.urls)),
    url(r'^api/login/', authtoken.obtain_auth_token),
    url(r'^admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


from __future__ import print_function
import torch
import numpy as np
from PIL import Image
import os
from torch import is_tensor
from torch.autograd import Variable

# Converts a Tensor into a float
def tensor2float(input_error):
    if is_tensor(input_error):
        if input_error.dim() < 1:
            error = float(input_error)
        else:
            error = input_error[0]
    elif isinstance(input_error, Variable):
        error = input_error.data[0]
    else:
        error = input_error
    return error

# Converts a Tensor into an image array (numpy)
# |imtype|: the desired type of the converted numpy array
def tensor2im(input_image, imtype=np.uint8):
    if is_tensor(input_image):
        image_tensor = input_image
    elif isinstance(input_image, Variable):
        image_tensor = input_image.data
    else:
        return input_image

    # image_numpy = image_tensor.cpu().float().numpy()
    image_numpy = image_tensor.detach().cpu().float().numpy()
    shape = image_numpy.shape
    image_numpy = image_numpy.reshape(shape[0], shape[2], shape[3])
    if image_numpy.shape[0] > 1:
        image_numpy = save_images(image_numpy)
        image_numpy = image_numpy[np.newaxis, ]

    if image_numpy.shape[0] == 1:
        image_numpy = np.tile(image_numpy, (3, 1, 1))
    image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0
    return image_numpy.astype(imtype)

def save_images(X):
    # [0, 1] -> [0,255]
    # if isinstance(X.flatten()[0], np.floating):
    #     X = (255.99*X).astype('uint8')

    n_samples = X.shape[0]
    rows = int(np.sqrt(n_samples))
    while n_samples % rows != 0:
        rows -= 1

    nh, nw = rows, n_samples//rows

    if X.ndim == 2:
        X = np.reshape(X, (X.shape[0], int(np.sqrt(X.shape[1])), int(np.sqrt(X.shape[1]))))

    if X.ndim == 4:
        # BCHW -> BHWC
        X = X.transpose(0,2,3,1)
        h, w = X[0].shape[:2]
        img = np.zeros((h*nh, w*nw, 3))
    elif X.ndim == 3:
        h, w = X[0].shape[:2]
        img = np.zeros((h*nh, w*nw))

    for n, x in enumerate(X):
        j = n//nw
        i = n%nw
        img[j*h:j*h+h, i*w:i*w+w] = x

    return img

def diagnose_network(net, name='network'):
    mean = 0.0
    count = 0
    for param in net.parameters():
        if param.grad is not None:
            mean += torch.mean(torch.abs(param.grad.data))
            count += 1
    if count > 0:
        mean = mean / count
    print(name)
    print(mean)

def save_image(image_numpy, image_path):
    image_pil = Image.fromarray(image_numpy)
    image_pil.save(image_path)

def print_numpy(x, val=True, shp=False):
    x = x.astype(np.float64)
    if shp:
        print('shape,', x.shape)
    if val:
        x = x.flatten()
        print('mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f' % (
            np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x)))

def mkdirs(paths):
    if isinstance(paths, list) and not isinstance(paths, str):
        for path in paths:
            mkdir(path)
    else:
        mkdir(paths)

def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)


import pandas as pd
import yfinance as yf
# import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

import os.path
from dateutil.relativedelta import relativedelta

from sp500 import get_sp500_tickers
from bot import BotTemplate

class Simulator:

    def __init__(self, bot_array: list[BotTemplate], stock_ticker, start_date, end_date, interval):
        """
        Creates a simulator object using specified parameters
        :param bot_array: array with bot objects used in simulation
        :param stock_ticker: integer amount of stocks to simulate on (from S&P500)
        :param start_date: date to start simulation from (historical data)
        :param end_date: date to stop simulation (e.g. today)
        :param interval: interval between simulation data points
        """
        self.bot_array = bot_array
        self.stock_ticker = stock_ticker
        self.start_date = start_date
        self.end_date = end_date
        self.interval = interval

        # Amount of dataframe entries given to bots in first cycle
        self.history = 15

        self.stock_data = self.get_stock_data()
        print(self.stock_data)

    def simulate(self):
        """Runs the whole simulation by adding extra time steps and then calling sim_cycle"""
        # Initiate all bots by giving them the stocks where they are going to get the data from
        for i in range(len(self.bot_array)):
            self.bot_array[i].initiate(self.stock_data.columns.tolist())

        # Run all sim cycles by adding a time datapoint in each cycle
        for i in range(self.history, len(self.stock_data.index)):
            self.sim_cycle(self.stock_data.iloc[:i])

        # Plot the graphs using plotly
        self.plot_value_graphs()
        self.plot_rsi_graphs()
        self.plot_mov_avg_graphs()

    def sim_cycle(self, current_stock_data):
        """Everything that happens during one cycle of the simulation"""
        # Looping over all the bots and giving them the current stock data, so they can trade
        for i in range(len(self.bot_array)):
            self.bot_array[i].trade(current_stock_data)

    def get_stock_data(self):
        """Gets stock data from yahoo finance and puts it in a dataframe"""
        # If stock_ticker is number: get first x amount of stocks from S&P500
        if isinstance(self.stock_ticker, int):
            # Get the first x amount of tickers from the S&P500 index
            tickers = get_sp500_tickers(self.stock_ticker)

        # Else it is a string, so use the specified stock_ticker and add ^GSPC
        else:
            tickers = [self.stock_ticker, "^GSPC"]

        # Generate a filename based on the amount of stocks, the interval and the start and end date
        filename = str(str(self.stock_ticker) + "_" + self.interval + "_" +
                       self.start_date.strftime("%m-%d-%Y") + "_" +
                       self.end_date.strftime("%m-%d-%Y") + ".csv")

        # Check if that csv file already exists, which means that the data has been requested before
        if not self.load_df(filename).empty:
            # Load the data from this existing csv file into a dataframe and return it
            return self.load_df(filename)
        else:
            # If it doesn't exist yet: request the data and save it into a csv file for later use
            stock_data = self.download_stock_data_loop(tickers, self.start_date, self.end_date, self.interval)
            self.save_df(stock_data, filename)
            return stock_data

    def download_stock_data_loop(self, tickers, start_date, end_date, interval):
        """Downloads stock data from yahoo finance using loops if more than max data_amount per request is needed"""
        # Create a list where the dataframes will be stored that have to be concatenated
        df_list = list()

        # Calculate the time between the start and end date in days
        timespan = end_date - start_date
        days = timespan.days

        # The maximum amount of data that can be requested depends on the interval
        if interval == "1m":
            # Max for interval of 1 minute is 7 days of data, so if less just return data from one request
            if days <= 7:
                return self.read_price_data(tickers, start_date, end_date, interval)
            else:
                # Divide the total time up into segments of 7 days (max request time)
                current_start_date = start_date
                current_end_date = current_start_date + relativedelta(days=7)

                # While not yet all the timespan has been covered: download all data for 7 days, then go to next 7 days
                while current_end_date < end_date:
                    df = self.read_price_data(tickers, current_start_date, current_end_date, interval)
                    df_list.append(df)
                    current_start_date = current_end_date
                    current_end_date = current_start_date + relativedelta(days=7)

                # If total timespan is not a multiple of 7 days the rest of the time still has to be downloaded
                if current_start_date != end_date:
                    df = self.read_price_data(tickers, current_start_date, end_date, interval)
                    df_list.append(df)

        elif interval == "2m" or interval == "5m" or interval == "15m" or interval == "30m" or interval == "60m" \
                or interval == "90m" or interval == "1h":
            # Max for interval lower than 1 day is 60 days of data, so if less just return data from one request
            if days <= 60:
                return self.read_price_data(tickers, start_date, end_date, interval)
            else:
                # Divide the total time up into segments of 60 days (max request time)
                current_start_date = start_date
                current_end_date = current_start_date + relativedelta(days=60)

                # While not all the timespan has been covered: download all data for 60 days, then go to next 60 days
                while current_end_date < end_date:
                    df = self.read_price_data(tickers, current_start_date, current_end_date, interval)
                    df_list.append(df)
                    current_start_date = current_end_date
                    current_end_date = current_start_date + relativedelta(days=60)

                # If there is still a bit of not downloaded time left: request the data for the rest of the time
                if current_start_date != end_date:
                    df = self.read_price_data(tickers, current_start_date, end_date, interval)
                    df_list.append(df)

        # All intervals of 1 day or more can just download all data at once, so return the data from that request
        else:
            return self.read_price_data(tickers, start_date, end_date, interval)

        # Concatenate all the individual dataframes from the list into one dataframe and return it
        return pd.concat(df_list)

    def plot_value_graphs(self):
        """Plots the value of all bots over time together with the value if stock was bought and held the whole time"""
        # Looping over all the bots and plot the value of the bots in a graph
        df_bot_values = pd.DataFrame()
        fig = go.Figure()
        for bot in self.bot_array:
            df_bot_values['date'] = list(bot.hist_trade.index.values)
            fig.add_trace(
                go.Scatter(x=df_bot_values['date'], y=bot.hist_trade['value'],
                           name=str(bot.__class__.__name__ + " " + str(bot.alfa)))
            )

        # Getting stock data to plot value if stocks bought at beginning and not sold & normalize to start_cash
        df_bot_values[self.stock_data.columns[0]] = self.stock_data[self.stock_data.columns[0]]\
            .tail(len(df_bot_values)).values
        df_bot_values[self.stock_data.columns[0]] = \
            df_bot_values[self.stock_data.columns[0]] * (100000 / self.stock_data.iat[self.history - 1, 0])

        # Add the stock price as a line in the plot
        fig.add_scatter(x=df_bot_values['date'], y=df_bot_values[self.stock_data.columns[0]],
                        name=str("Stock Price of " + str(self.stock_data.columns[0])))

        # Set the title and axis labels of the plot
        fig.update_layout(title="Value over Time")
        fig.update_yaxes(title_text="Value")
        fig.update_xaxes(title_text="Date")

        fig.show()

    def plot_mov_avg_graphs(self):
        """Plots the moving averages of the BotMovingAverage bots together with the stock price"""
        # Looping over all the bots and plot the moving averages saved in hist_trade['var']
        df_bot_values = pd.DataFrame()
        fig = go.Figure()
        for bot in self.bot_array:
            df_bot_values['date'] = list(bot.hist_trade.index.values)
            # Check if class is of type BotMovingAverage and otherwise don't plot the moving average
            if bot.__class__.__name__ == "BotMovingAverage":
                fig.add_trace(
                    go.Scatter(x=df_bot_values['date'], y=bot.hist_trade['var'],
                               name=str('Moving Average of bot ' + str(bot.alfa)))
                )

        # Make the lines dotted, so the moving averages are distinguishable from the stock price line
        fig.update_traces(patch={"line": {"width": 2, "dash": 'dot'}})

        # Set title and axis titles of the graph
        fig.update_layout(title="Moving average & Stock Price over Time")
        fig.update_yaxes(title_text="Stock Price")
        fig.update_xaxes(title_text="Date")

        # Getting stock price data
        df_bot_values[self.stock_data.columns[0]] = self.stock_data[self.stock_data.columns[0]] \
            .tail(len(df_bot_values)).values

        # Plotting the stock price over time
        fig.add_scatter(x=df_bot_values['date'], y=df_bot_values[self.stock_data.columns[0]],
                        name=str("Stock Price of " + str(self.stock_data.columns[0])))

        fig.show()

    def plot_rsi_graphs(self):
        """Plots the RSI calculation of a bot over time together with the stock price over time"""
        # Create a dataframe with the stock price over time in it and the dates, handy for plotting
        df_stock_price = pd.DataFrame(self.stock_data[self.stock_data.columns[0]])
        df_stock_price['Date'] = self.stock_data.index.values

        # Create figure with secondary y-axis
        fig = make_subplots(rows=2, cols=1)

        # Looping over all the bots and add var_data to the plot
        for bot in self.bot_array:
            # Check if class is of type BotRSI and otherwise don't plot the RSI
            if bot.__class__.__name__ == "BotRSI":
                fig.add_trace(
                    go.Scatter(x=df_stock_price['Date'], y=bot.hist_trade['var'],
                               name=str('RSI of bot ' + str(bot.alfa))), row=2, col=1
                )

        # Add stock price graph to be able to view it together with the RSI
        fig.add_trace(
            go.Scatter(x=df_stock_price['Date'], y=df_stock_price[self.stock_data.columns[0]],
                       name=str("Stock Price of " + str(self.stock_data.columns[0]))), row=1, col=1
        )

        # Set the RSI axes to range from 0 to 100
        fig.update_yaxes(range=[0, 100], row=2, col=1)

        # Add horizontal RSI lines
        fig.add_hline(y=70, line_width=2, line_dash="dash", line_color="red", row=2, col=1)
        fig.add_hline(y=30, line_width=2, line_dash="dash", line_color="green", row=2, col=1)

        # Add figure title
        fig.update_layout(title_text="RSI & Stock Price over Time")

        # Set y-axes & x-axes titles
        fig.update_yaxes(title_text="RSI", row=2, col=1)
        fig.update_yaxes(title_text="Stock Price", row=1, col=1)
        fig.update_xaxes(title_text="Date")

        fig.show()

    def read_price_data(self, stock_symbol, start_date, end_date, interval):
        """Imports price data from Yahoo Finance"""
        try:
            stock_data = yf.download(stock_symbol, start_date, end_date, interval=interval)
        except:
            return None

        prices = stock_data.loc[:, "Adj Close"]
        prices = prices.ffill()

        return prices

    def save_df(self, df: pd.DataFrame, filename):
        """Saves a dataframe as a cvs file and returns it"""
        df.to_csv(filename)

    def load_df(self, filename):
        """Loads a dataframe from a csv file and returns it"""
        if not os.path.isfile(filename):
            return pd.DataFrame

        return pd.read_csv(filename, index_col=0)


# -*- coding: utf-8 -*-
__author__ = 'eso'
import sys
sys.path.append('../../')
from tools.petscan import PetScan
from tools.template_handler import TemplateHandler
import re
import requests
import pywikibot

def substitute_sperrsatz(template):
    handler = TemplateHandler(template.group(0))
    handler.set_title('SperrSchrift')
    parameters = handler.get_parameterlist()
    parameters.append({'key': 'satz', 'value': '1'})
    handler.update_parameters(parameters)
    return handler.get_str(str_complex=False)

searcher_catscan = PetScan()
searcher_catscan.add_namespace(0)
searcher_catscan.add_namespace('Seite')
searcher_catscan.add_namespace('Index')
searcher_catscan.add_yes_template('Sperrsatz')
sites = searcher_catscan.run()
site = pywikibot.Site()

for lemma in sites:
    if lemma['a']['nstext'] == '(Article)':
        page = pywikibot.Page(site, lemma['a']['title'])
    else:
        page = pywikibot.Page(site, lemma['a']['nstext'] + ':' + lemma['a']['title'])
    test_for_fit = re.search('Sperrsatz', page.text)
    if test_for_fit:
        try:
            page.text = re.sub('\{\{Sperrsatz(?:\{\{.*?\}\}|.)*?\}\}', substitute_sperrsatz, page.text)
            page.save(summary='bot edit: Vereinheitlichung der Vorlage Sperrsatz zu SperrSchrift', botflag=True, )
        except:
            print(lemma)


"""
Export epochs data from mne format to csv.
"""
#%%
import argparse
from nemo.epochs import get_epochs, get_epochs_dfs
from nemo.utils import get_cwd

#%%
parser = argparse.ArgumentParser()
parser.add_argument(
    "-e",
    "--include_events",
    nargs="+",
    default=["empe", "afim"],
    help="Event(s) to include (default: E, R)",
)
parser.add_argument(
    "-b",
    "--include_baseline",
    action="store_true",
    help="Include baseline (default: False)",
)
args = parser.parse_args()

for include_events in args.include_events:
    epochs_df, epochs_metadata = get_epochs_dfs(
        get_epochs(include_events=include_events)
    )

    if not args.include_baseline:
        epochs_df = epochs_df[epochs_df["time"] > 0]

    csv_data_dir = get_cwd() / "data" / f"{include_events}_csv"
    csv_data_dir.mkdir(parents=True, exist_ok=True)
    epochs_df.to_csv(
        csv_data_dir / "epochs.csv", sep=";", index=False
    )
    epochs_metadata.to_csv(
        csv_data_dir / "epochs_metadata.csv", sep=";", index=False
    )


from ch.systemsx.cisd.openbis.dss.etl.dto.api.impl import MaximumIntensityProjectionGenerationAlgorithm

class CustomExperimentMaximumIntensityProjectionGenerationAlgorithm(MaximumIntensityProjectionGenerationAlgorithm):
    """
    Custom MaximumIntensityProjectionGenerationAlgorithm that makes sure that the first
    timepoint in a series is registered for creation of the representative thumbnail.
    """

    def __init__(self, datasetTypeCode, width, height, filename):
        """
        Constructor
        """

        # Call the parent base constructor
        MaximumIntensityProjectionGenerationAlgorithm.__init__(self,
            datasetTypeCode, width, height, filename)

    def findMinTimepoint(self, information):
        mintimepoint = None
        for image in information.getImageDataSetStructure().getImages():
            timepoint = image.tryGetTimepoint()
            if timepoint is not None:
                if mintimepoint is None:
                    mintimepoint = timepoint
                else:
                    mintimepoint = min(mintimepoint, timepoint)
        return mintimepoint

    def generateImages(self, information, thumbnailDatasets, imageProvider):
        self.mintimepoint = self.findMinTimepoint(information)
        return super(CustomExperimentMaximumIntensityProjectionGenerationAlgorithm, self).generateImages(information, thumbnailDatasets, imageProvider)

    def imageToBeIgnored(self, image):
        """
        Overrides the parent imageToBeIgnored method. The selection of which
        series should be used to create the representative thumbnail is done
        elsewhere. Here we prevent the base
        MaximumIntensityProjectionGenerationAlgorithm.imageToBeIgnored() method
        to make a decision based on the timepoint (==0).
        """

        return image.tryGetTimepoint() != self.mintimepoint


from transformers import *
import torch
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm
import numpy as np
import argparse

from text_dataset import TextDataset, load_text_dataset
from models import BERTGRUSentiment

parser = argparse.ArgumentParser(description='Process hyperparameters...')
parser.add_argument('--dataset','-d', type=str, choices=["SetFit/20_newsgroups","ag_news","sst2","imdb"], default="SetFit/20_newsgroups")
parser.add_argument('--batch_size','-b', type=int, default=16)
parser.add_argument("--gpu", '-g', type=int, choices=[0,1,2,3], default=0)
parser.add_argument("--num_classes", '-n', type=int)
parser.add_argument("--emsize", '-s', type=int, default=256)
parser.add_argument("--epoch", '-e', type=int, default=2)
parser.add_argument("--lr", type=float, default=2e-5)

args = parser.parse_args()
DATASET = args.dataset
BATCH_SIZE = args.batch_size
GPU = args.gpu
NUM_CLASSES = args.num_classes
EMSIZE = args.emsize
EPOCHS = args.epoch # epoch
LR = args.lr  # learning rate

# Set hyperparameter
device = torch.device(f'cuda:{GPU}')

# load dataset
train_set, test_set = load_text_dataset(DATASET)

train_dataset = TextDataset(train_set[0], train_set[1])
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=BATCH_SIZE)
train_eval_loader = DataLoader(train_dataset, shuffle=False, batch_size=BATCH_SIZE)

test_dataset = TextDataset(test_set[0], test_set[1])
test_eval_loader = DataLoader(test_dataset, shuffle=False, batch_size=BATCH_SIZE)

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

N_LAYERS = 2
BIDIRECTIONAL = True
DROPOUT = 0.25

bert = BertModel.from_pretrained('bert-base-uncased')
model = BERTGRUSentiment(bert,
                         EMSIZE,
                         NUM_CLASSES,
                         N_LAYERS,
                         BIDIRECTIONAL,
                         DROPOUT)

model = model.to(device)

for name, param in model.named_parameters():
    print(name)
    if name.startswith('bert'):
        param.requires_grad = False

optimizer = torch.optim.AdamW(model.parameters(), lr=LR)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5)
criterion = torch.nn.CrossEntropyLoss()

def binary_accuracy(preds, y):
    """
    Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8
    """
    prediction = preds.argmax(axis=1)
    correct = (prediction==y).float()
    acc = correct.sum()/len(correct)
    return acc

def train():

    epoch_loss = 0
    epoch_acc = 0
    model.train()

    for text, label in tqdm(train_loader):
        inputs = tokenizer(text, return_tensors='pt',padding=True)

        for w in inputs:
            inputs[w] = inputs[w].to(device)

        label = label.to(dtype=torch.long, device=device)

        optimizer.zero_grad()

        predictions = model(**inputs)

        loss = criterion(predictions, label)

        acc = binary_accuracy(predictions, label)

        loss.backward()

        optimizer.step()

        epoch_loss += loss.item()
        epoch_acc += acc.item()

    return epoch_loss / len(train_loader), epoch_acc / len(train_loader)

def test(loader, data_set_name):
    with torch.no_grad():
        model.eval()
        epoch_acc = 0
        for text, label in tqdm(loader):
            inputs = tokenizer(text, return_tensors='pt', padding=True)

            for w in inputs:
                    inputs[w] = inputs[w].to(device)

            label = label.to(dtype=torch.long, device=device)

            optimizer.zero_grad()

            predictions = model(**inputs)

            acc = binary_accuracy(predictions, label)

            epoch_acc += acc.item()

        print(data_set_name, epoch_acc / len(loader))

def split(loader, data_set_name):
    with torch.no_grad():
        model.eval()
        correct = 0
        total = 0
        for x, y in tqdm(loader):
            inputs = tokenizer(x, return_tensors='pt', padding=True)
            labels = y
            for w in inputs:
                inputs[w] = inputs[w].to(device)
            labels = labels.to(device)
            out = model(**inputs)
            outputs = model.bert(**inputs)
            pooled_output = outputs[1]
            logits = model.classifier(pooled_output)

            o = model.feature()(**inputs)
            p = o[1]
            l = model.prediction()(p)

            pred = torch.argmax(out[0], dim=-1, keepdim=False)
            pred = pred.cpu().numpy()
            labels = labels.cpu().numpy()
            correct += np.sum(pred == labels)
            total += pred.shape[0]
        print(data_set_name, correct / total)

for epoch in range(EPOCHS):
    train()
    test(train_eval_loader, "train")
    test(test_eval_loader, 'test')
    scheduler.step()


from heapq import heappush, heappop

def main():
    N, M = map(int, input().split())

    # 日数別に仕事を管理
    works = [[] for _ in range(M)]

    for i in range(N):
        A, B = map(int, input().split())

        if A > M:
            continue

        deadline = M - A
        # heapqは最小値を返す仕組みしかないので、最大値を返すため-する
        works[deadline].append(-B)

    ans = 0
    heapque = []

    # i日目に始めることができる仕事で報酬の高いものを選ぶ
    for i in range(M - 1, -1, -1):
        for work in works[i]:
            # heapqに候補を追加
            heappush(heapque, work)

        if heapque:
            ans += heappop(heapque)

    print(-ans)

main()


import random
from typing import Union, Tuple

import torch
import torchvision.transforms.functional as F
import cv2
import torch.nn.functional as FNN
import numpy as np
# from skimage import transform

ImageOrTensor = Union[np.ndarray, torch.Tensor]
Transformed = Tuple[Tuple[ImageOrTensor, ImageOrTensor],
                    Union[np.ndarray, torch.Tensor]]

class Compose(object):

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

    def __call__(self, *args):
        o = args
        for trf in self.transforms:
            o = trf(*o)
        return o

class Resize(object):

    def __init__(self, height: int, width: int) -> None:
        self.height = height
        self.width = width

    def __call__(self,
                 frames: Tuple[np.ndarray, np.ndarray],
                 optical_flow: np.ndarray) -> Transformed:

        frame1 = F.resize(frames[0], (self.height, self.width) , antialias=True)
        frame2 = F.resize(frames[1], (self.height, self.width), antialias=True)
        # print(self.width, optical_flow.size()[2]) # -> (60,960) for level 0
        optical_flow = F.resize(optical_flow, (self.height, self.width),
                                antialias=True) / optical_flow.size()[2] * self.width
        # print(self.width, optical_flow.size()[2])
        # optical_flow = FNN.interpolate(optical_flow, size=(self.height, self.width),
        #                              align_corners=True, mode='bilinear') * optical_flow.size()[2] / self.width

        return (frame1, frame2), optical_flow

class RandomRotate(object):

    def __init__(self, minmax: Union[Tuple[int, int], int]) -> None:
        self.minmax = minmax
        if isinstance(minmax, int):
            self.minmax = (-minmax, minmax)

    def __call__(self,
                 frames: Tuple[np.ndarray, np.ndarray],
                 optical_flow: np.ndarray) -> Transformed:
        angle = random.randint(*self.minmax)
        frame1 = F.rotate(frames[0], angle)
        frame2 = F.rotate(frames[1], angle)
        optical_flow = F.rotate(optical_flow, angle) # this is wrong

        return (frame1, frame2), optical_flow

class Normalize(object):

    def __init__(self,
                 mean: Tuple[float, float, float],
                 std: Tuple[float, float, float]) -> None:
        self.mean = mean
        self.std = std

    def __call__(self,
                 frames: Tuple[torch.Tensor, torch.Tensor],
                 optical_flow: torch.Tensor) -> Transformed:

        frame1 = F.normalize(frames[0], self.mean, self.std)
        frame2 = F.normalize(frames[1], self.mean, self.std)

        return (frame1, frame2), optical_flow

class ToTensor(object):

    def __call__(self,
                 frames: Tuple[np.ndarray, np.ndarray],
                 optical_flow: np.ndarray) -> Transformed:

        return ((F.to_tensor(frames[0]), F.to_tensor(frames[1])),
                 torch.from_numpy(optical_flow).permute(2, 0, 1).float())


class Misc(object):
    """regroupement de fonction ne correspondant a aucune autre classe"""

    @staticmethod
    def AskValidInput(msg,check=lambda val: True):
        """
        demande une saisie tant qu'une saisie valide n'a pas été rentrée
        """
        print(msg)
        while(True):
            try:
                val = input(">")
                if(check(val)):
                    print("saisie acceptée: ",val)
                    return val
            except:
                print("saisie incorrecte")

    @staticmethod
    def CheckIfInt(val,mymin,mymax):
        val = int(val)
        if(isinstance(val,int)):
            if(val >= mymin and val <= mymax):
                return True
            else:
                print("valeur non comprise entre ",mymin," et ",mymax)
        else:
            print("saisie incorrecte, un entier est attendu")
        return False


#!/usr/bin/python
"""Script to parse Syscache.hve"""

from yarp import Registry
from datetime import datetime as dt

import argparse
import csv
import os
import struct
import sys

# Code Reference for FILETIME to Python Date:
# https://gist.github.com/Mostafa-Hamdy-Elgiar/9714475f1b3bc224ea063af81566d873
EPOCH_AS_FILETIME = 116444736000000000   # January 1, 1970 as MS FILETIME
HUNDREDS_OF_NANOSECONDS = 10000000

def _validate_input(options):
    if options.hive is None:
        print("No registry hive provided!")
        return False
    elif not os.path.isfile(options.hive):
        print("Path of registry hive is invalid!")
        return False

    if not os.path.isdir(options.output):
        print("Path of output directory is invalid!")

    return True

def decode_fileid(file_id):
    mft_rec1, mft_rec2, mft_seq = struct.unpack('IHH', bytearray(file_id))
    return mft_rec1 | (mft_rec2 << 16), mft_seq

def parse_values(yhive, output_path):
    object_id = object_lru = file_id = usn = usn_id = 0
    file_ref = [0, 0]
    usn_id_timestamp = sha1 = aeprogram_id = ''

    with open(output_path, mode='w', newline='') as outfile:
        entry_writer = csv.writer(outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        entry_writer.writerow(['_ObjectId_', '_ObjectLru_', '_FileId_', 'MFT Rec No.', 'MFT Seq No.', '_Usn_',
                               '_UsnJournalId_', 'UsnJournalId_Timestamp', 'Last Write Timestamp', 'AeFileID',
                               'AeProgramID'])
        key = yhive.find_key('DefaultObjectStore\\ObjectTable')
        for sk in key.subkeys():
            for v in sk.values():
                if v.name() == '_ObjectId_':
                    object_id = v.data()
                if v.name() == '_ObjectLru_':
                    object_lru = v.data()
                if v.name() == '_FileId_':
                    file_id = v.data()
                    file_ref = decode_fileid(v.data_raw())
                if v.name() == '_Usn_':
                    usn = v.data()
                if v.name() == '_UsnJournalId_':
                    usn_id = v.data()
                    usn_id_timestamp = dt.utcfromtimestamp((usn_id - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS)
                if v.name() == 'AeFileID':
                    sha1 = v.data().decode('ascii').replace('\0', '')[4:]
                if v.name() == 'AeProgramID':
                    aeprogram_id = v.data().decode('ascii').replace('\0', '')
            entry_writer.writerow([object_id, object_lru, file_id, file_ref[0], file_ref[1], usn, usn_id,
                                   usn_id_timestamp, sk.last_written_timestamp(), sha1, aeprogram_id])

def main():
    start_time = dt.now()
    timestamp = dt.now().strftime("%Y-%m-%d@%H%M%S")

    argument_parser = argparse.ArgumentParser(description=(
        "syscache parses values of subkeys under Syscache\DefaultObjectStore\\ObjectTable"
    ))

    argument_parser.add_argument('-f', '--hive', default=None, help="path of the registry hive to be parsed")
    argument_parser.add_argument('-o', '--output', default=None, help=(
        "path to the directory to store the output, default to directory of the registry hive"))

    options = argument_parser.parse_args()
    if options.hive is not None:
        options.hive = os.path.abspath(options.hive)
    if options.output is None:
        options.output = os.path.dirname(options.hive)  # default to directory of the registry hive
    else:
        options.output = os.path.abspath(options.output)

    if not _validate_input(options):
        sys.exit(1)

    hive_name = os.path.basename(options.hive)
    output_path = os.path.join(options.output, hive_name + '_' + timestamp + '.csv')
    with open(options.hive, 'rb') as hive:
        yarp_hive = Registry.RegistryHive(hive)
        parse_values(yarp_hive, output_path)

    print("\rTime Taken: {}".format(dt.now() - start_time))

if __name__ == '__main__':
    if not main():
        sys.exit(1)
    else:
        sys.exit(0)


from .misc_util import orthogonal_init
import torch
import numpy as np
import torch.nn as nn

def mlp_block(in_features, out_features, use_batchnorm):
    layers = []
    layer = orthogonal_init(nn.Linear(in_features, out_features),
                            gain = nn.init.calculate_gain('relu'))
    layers.append(layer)
    if use_batchnorm:
        layers.append(nn.BatchNorm1d(out_features))
    layers.append(nn.ReLU())

    return nn.Sequential(*layers)

class MlpModel(nn.Module):
    def __init__(self,
                 input_dim   = 4,
                 hidden_dims = [64, 64],
                 use_batchnorm = False,
                 **kwargs):
        """
        input_dim:     (int)  number of the input dimensions
        hidden_dims:   (list) list of the dimensions for the hidden layers
        use_batchnorm: (bool) whether to use batchnorm
        """
        super(MlpModel, self).__init__()

        # Hidden layers
        hidden_dims = [input_dim] + hidden_dims
        layers = []
        for i in range(len(hidden_dims)-1):
            in_features  = hidden_dims[i]
            out_features = hidden_dims[i+1]
            layers.append(mlp_block(in_features, out_features, use_batchnorm))
        self.layers = nn.Sequential(*layers)
        self.output_dim = hidden_dims[-1]

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

def conv_block(in_channels, out_channels, kernel_size, stride, use_batchnorm):
    layers = []
    layer = orthogonal_init(nn.Conv2d(in_channels, out_channels, kernel_size = kernel_size, stride = stride),
                            gain = nn.init.calculate_gain('relu'))
    layers.append(layer)
    if use_batchnorm:
        layers.append(nn.BatchNorm2d(out_channels))
    layers.append(nn.ReLU())

    return nn.Sequential(*layers)

def conv2d_size_out(size, kernel_size, stride):
    return (size - (kernel_size - 1) - 1) // stride  + 1

class ConvModel(nn.Module):
    def __init__(self,
                 input_shape = (3, 84, 84),
                 filters = [(32, 8, 4), (64, 4, 2), (64, 3, 1)],
                 use_batchnorm = False,
                 **kwargs):
        """
        input_shape:  (tuple) tuple of the input dimension shape (channel, height, width)
        filters:       (list) list of the tuples consists of (number of channels, kernel size, and strides)
        use_batchnorm: (bool) whether to use batchnorm
        """
        super(ConvModel, self).__init__()

        # Conv layers
        input_channel, input_height, input_width = input_shape
        filters = [(input_channel, 0, 0)] + filters
        output_height, output_width = input_height, input_width
        layers = []
        for i in range(len(filters)-1):
            in_channels  = filters[i][0]
            out_channels = filters[i+1][0]
            kernel_size  = filters[i+1][1]
            stride       = filters[i+1][2]
            layers.append(conv_block(in_channels, out_channels, kernel_size, stride, use_batchnorm))

            output_height = conv2d_size_out(output_height, kernel_size, stride)
            output_width = conv2d_size_out(output_width, kernel_size, stride)

        self.layers = nn.Sequential(*layers)
        self.output_shape = (out_channels, output_height, output_width)
        self.output_dim = out_channels * output_height * output_width

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

class ConvToMlpModel(nn.Module):
    """
    Model object
    """

    def __init__(self,
                 input_shape = (3, 84, 84),
                 filters = [(32, 8, 4), (64, 4, 2), (64, 3, 1)],
                 hidden_dims = [512],
                 use_batchnorm = False,
                 **kwargs):
        """
        input_shape:  (tuple) tuple of the input dimension shape (channel, height, width)
        filters:       (list) list of the tuples consists of (number of channels, kernel size, and strides)
        hidden_dims:   (list) list of the dimensions for the hidden layers
        use_batchnorm: (bool) whether to use batchnorm
        """
        super(ConvToMlpModel, self).__init__()
        self.conv = ConvModel(input_shape, filters, use_batchnorm)
        self.mlp  = MlpModel(self.conv.output_dim, hidden_dims, use_batchnorm)
        self.output_dim = hidden_dims[-1]

    def forward(self, x):
        x = self.conv(x)
        x = x.view(x.size(0), -1)
        x = self.mlp(x)
        return x


from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from .serializers import WishlistSerializer
from wishlists.models import Wishlist

@api_view(['GET'])
def getRoutes(request):

    routes = [
        {'GET':'/api/wishists'},
        {'GET':'/api/wishists/id'},

        {'POST':'/api/account/token'},
        {'POST':'/api/account/token/refresh'},
    ]

    return Response(routes)

@api_view(['GET'])
def getWishlists(request):
    wishlists = Wishlist.objects.all()
    serializer = WishlistSerializer(wishlists, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def getWishlist(request, pk):
    wishlist = Wishlist.objects.get(id=pk)
    serializer = WishlistSerializer(wishlist, many=False)
    return Response(serializer.data)