from lxml import etree
import numpy as np
import re

class BhpDocumentParser:

    isCPTRegistrationRequest = False
    nsMap = {}
    metadata = {}
    cptMatrix = None
    dissipationMatrices = None

    def __init__(self, xmlTree):
        self.xmlTree = xmlTree
        self.generateNsMap()
        self.generateMatrix()
        self.metadata = BhpDocumentParser.lxml_to_dict(self.getRoot())
        self.determineIsCPT()

    def getRoot(self):
        if (hasattr(self.xmlTree, 'getroot')):
            return self.xmlTree.getroot()
        return self.xmlTree

    def determineIsCPT(self):
        if ('registrationRequest' in self.metadata):
            if ('sourceDocument' in self.metadata['registrationRequest']):
                if ('CPT' in self.metadata['registrationRequest']['sourceDocument']):
                    self.isCPTRegistrationRequest = True

    def generateMatrix(self):
        conePenetrationTest = self.getElementByTagName('conePenetrationTest')
        dissipationTests = self.getListOfElementsByTagName('dissipationTest')

        if(conePenetrationTest is not None):
            values = self.getChildElementByTagName(conePenetrationTest, 'values')
            self.cptMatrix = BhpDocumentParser.convertDataToMatrix(values.text, 25)
        if(dissipationTests is not None):
            dissMatrixList = list()
            for dissipationTest in dissipationTests:
                values = self.getChildElementByTagName(dissipationTest, 'values')
                matrix = BhpDocumentParser.convertDataToMatrix(values.text, 5)
                dissMatrixList.append(matrix)
            self.dissipationMatrices = dissMatrixList
        return None

    @staticmethod
    def convertDataToMatrix(data, rows):
        lines = data.split(';')
        lines = [line.split(',') for line in lines]
        lines = [line for line in lines if len(line)==rows]
        return np.matrix(lines, dtype=np.float)

    def getCptMatrix(self):
        return self.cptMatrix

    # list of matrices
    def getDissipation(self):
        return self.dissipationMatrices

    # from lxml_to_dict, slightly adjusted the implementation to remove some tags
    @staticmethod
    def lxml_to_dict(element):
        ret = {}
        if element.getchildren() == []:
            tag = BhpDocumentParser.stripNSFromTagName(element.tag)
            if (tag != 'values'):
                ret[tag] = element.text
        else:
            count = {}
            for elem in element.getchildren():
                subdict = BhpDocumentParser.lxml_to_dict(elem)
                tag = BhpDocumentParser.stripNSFromTagName(element.tag)
                subtag = BhpDocumentParser.stripNSFromTagName(elem.tag)
                # subtag can only be None if the element tag is not a String (could be a comment), in which case we don't add it to the dict
                if(subtag is None):
                    continue
                if ret.get(tag, False) and subtag in ret[tag].keys():
                    count[subtag] = count[subtag]+1 if count.get(subtag, False) else 1
                    elemtag = subtag+str(count[subtag])
                    subdict = {elemtag: subdict[subtag]}
                if ret.get(tag, False):
                    ret[tag].update(subdict)
                else:
                    ret[tag] = subdict
        return ret

    @classmethod
    def fromString(cls, xmlString):
        return cls(etree.fromstring(xmlString))

    @classmethod
    def fromFile(cls, xmlFile):
        return cls(etree.parse(xmlFile))

    def generateNsMap(self):
        for _, elem in etree.iterwalk(self.xmlTree, events = ('start-ns',)):
            ns, url = elem
            self.nsMap[ns] = '{' + url + '}'

    def getCptParametersMap(self):
        registrationRequest = self.metadata['registrationRequest']
        if (registrationRequest is not None):
            sourceDocument = registrationRequest['sourceDocument']
            if (sourceDocument is not None):
                cpt = sourceDocument['CPT']
                if (cpt is not None):
                    conePenetrometerSurvey = cpt['conePenetrometerSurvey']
                    if (conePenetrometerSurvey is not None):
                        parameters = conePenetrometerSurvey['parameters']
                        if (parameters is not None):
                            return parameters
        return None

    def getElementByTag(self, tag):
        for _, elem in etree.iterwalk(self.xmlTree, events = ('end',), tag = self.getTag(tag)):
            return elem
        return None

    def isCPT(self):
        return self.isCPTRegistrationRequest

    def getChildElementByTagName(self, elem, name):
        for _, elem in etree.iterwalk(elem, events = ('end',)):
            if (name == BhpDocumentParser.stripNSFromTagName(elem.tag)):
                return elem
        return None

    # returns first element without ns matching the name param
    def getElementByTagName(self, name):
        for _, elem in etree.iterwalk(self.xmlTree, events = ('end',)):
            if (name == BhpDocumentParser.stripNSFromTagName(elem.tag)):
                return elem
        return None

    @staticmethod
    def stripNSFromTagName(name):
        if (isinstance(name, str)):
            return re.sub('{.*}', '', name)
        return None

    # returns list of elements without ns matching the name param
    def getListOfElementsByTagName(self, name):
        listOfElements = list()
        for _, elem in etree.iterwalk(self.xmlTree, events = ('end',)):
            if (name == BhpDocumentParser.stripNSFromTagName(elem.tag)):
                listOfElements.append(elem)
        if (len(listOfElements) != 0):
            return listOfElements
        return None

    def getTag(self, fullTag):
        (ns, tag) = fullTag.strip().split(":")
        if (ns in self.nsMap):
            return self.nsMap[ns] + tag
        return None

    def toString(self):
        return etree.tostring(self.xmlTree, pretty_print = True).decode("utf-8")

    def stackedPrintOfStructure(self):
        stack = list()
        for event, elem in etree.iterwalk(self.getRoot(), events=('start', 'end')):
            if (not isinstance(elem.tag, str)):
                    continue
            if (event == 'start'):
                stack.append(elem.tag.split('}', 1)[1])
                mapIndex = ""
                # Print this in a way that it's easy to copy paste to get the proper metadata from the dictionary
                for item in stack:
                    mapIndex += "['%s']" % item
                print(mapIndex)
            else:
                stack.pop()
        return stack


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
from random import *
import math

def get_random_coordinates():
    return random(), random()

def draw_circle():
    center = (0, 0)
    radius = 1

    circle = plt.Circle(center, radius, fill=False, ec='b')

    a = plt.axes(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
    a.add_patch(circle)
    a.set_aspect('equal')

    plt.grid(True)

def main():
    data = []
    gen_cnt = 100000
    inside_cnt = 0

    for i in range(gen_cnt):
        x, y = get_random_coordinates()
        data.append((x, y))

        xy_pow_sum = math.pow(x, 2) + math.pow(y, 2)
        if xy_pow_sum <= 1:
            inside_cnt += 1

    xs, ys = zip(*data)

    draw_circle()
    plt.scatter(xs, ys)

    plt.grid(True)
    plt.show()

    print("Div: {} (Expected: {})".format(str(inside_cnt/gen_cnt), math.pi/4))

if __name__ == '__main__':
    main()


from WoG.GuessGame import play as guess_play
from WoG.MemoryGame import play as memory_play
from WoG.Utils import ERROR_MESSAGE as error_message

def welcome(name):
    return 'Hello ' + name + ' and welcome to the World of Games (WoG).\nHere you can find many cool games to play.'

def load_game():
    print("Please choose a game to play:\n1. Memory Game - a sequence of numbers will appear for 1 second and you have to guess it back\n2. Guess Game - guess a number and see if you chose like the computer")
    while True:
        try:
            game = int(input("Please choose a game to play - 1/2"))
        except ValueError:
            continue
        if game > 2 or game < 1:
         error_message()
        if game in (1, 2):
            break
    difficulty = int(input("Please choose game difficulty from 1 to 5:"))
    if game == 1:
        memory_play(difficulty)
    if game == 2:
        guess_play(difficulty)
    return difficulty


def getDivisors(number):
    if(number == 1) : return 1

    divisors = set()

    count = 0
    for i in range(1, int(number ** 0.5)+1):
        if(number % i == 0):
            divisors.add(i)

    tmp_divisors = divisors.copy()

    for i in tmp_divisors:
        divisors.add(number//i)

    return len(divisors)

def getCost(power, limit, limitPower):
    if(power > limit):
        return limitPower

    else:
        return power

def solution(number, limit, power):
    answer = 0

    divisorCounts = []

    for num in range(1, number+1):
        divisorCounts.append(getDivisors(num))

    for count in divisorCounts:
        answer += getCost(count,limit,power)

    return answer

프로그래머스/unrated/136798. 기사단원의 무기/기사단원의 무기.py


# -*- coding:utf-8 -*-

"""
Pour éviter de surcharger de commandes le drone.
"""

from vector import Vector

class Queue (object):
    """
    Permet de moduler le flux de commandes à donner au drone, en moyennant les vecteurs déplacement successifs sur plusieurs frames.
    """

    def __init__(self, drone, cmd_max):
        self.queue = []
        self.processed = []
        self.drone = drone

        self.opposed = {self.drone.forward : self.drone.backward,
                self.drone.backward : self.drone.forward,
                self.drone.left : self.drone.right,
                self.drone.right : self.drone.left,
                self.drone.down : self.drone.up,
                self.drone.up : self.drone.down,
                self.drone.rLeft : self.drone.rRight,
                self.drone.rRight : self.drone.rLeft}
        self.count = 0
        self.cmd_max = cmd_max

    def flush(self):
        """
        Envoie les commandes intelligemment : somme les vecteurs déplacement pour améliorer la précision des déplacements.
        """
        #Vecteur représentant le pointage moyen sur plusieurs frames
        sum_vector = Vector(0,0,0)
        for vector in self.queue:
            sum_vector += vector

        sum_vector /= self.count
        self.drone.processVector(sum_vector)
        self.empty()

    def empty(self):
        self.queue = []
        self.count = 0

    def add(self, offset):
        self.queue.append(Vector(offset[0], offset[1], offset[2]))
        self.count+=1

    def check(self):
        if self.count >= self.cmd_max:
            self.flush()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('carga', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='carga',
            name='id_carga',
            field=models.CharField(max_length=6),
        ),
    ]


import os
import papermill as pm

def train_ct(dataset_name, hampel_window_len, num_sigmas):
    input = "constant-threshold.ipynb"
    output = "out/constant-threshold-" + dataset_name + ".ipynb"
    pm.execute_notebook(input, output, parameters = dict(
        dataset_name = dataset_name,
        hampel_window_len = hampel_window_len,
        num_sigmas = num_sigmas
    ))

def train_all(num_datasets, hampel_window_len, num_sigmas):
    for i in range(0, num_datasets):
        name = "wn-" + ("00" + str(i))[-3:]
        train_ct(name, hampel_window_len, num_sigmas)
        i = i + 1

dir = "./out"
if not os.path.exists(dir):
    os.mkdir(dir)

# Different training params
#train_all(10, 5, 2)
train_all(10, 5, 3)
#train_all(10, 5, 4)


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.db.models import CharField, Value, Q
from itertools import chain

from authentication.models import User
from website.models import Review, UserFollows, Ticket
from website import forms

@login_required
def flux(request):
    """
    Gérer le flux pour afficher les tickets et les avis de l'utilisateur et de ses abonnés
    """

    # Récupérer les identifiants des utilisateurs suivis par l'utilisateur connecté
    users_followed_ids = UserFollows.objects.filter(user=request.user).values_list('followed_user', flat=True)

    # Récupérer les avis associés aux tickets pour l'utilisateur connecté et les utilisateurs suivis
    reviews_followed = Review.objects.filter(
        Q(user__in=users_followed_ids) | Q(ticket__user__in=users_followed_ids)
    ).select_related('user', 'ticket').annotate(content_type=Value('REVIEW', CharField()))

    # Récupérer les tickets de l'utilisateur connecté et des utilisateurs suivis
    tickets_user_and_followed = Ticket.objects.filter(
        Q(user=request.user) | Q(user__in=users_followed_ids)
    ).annotate(content_type=Value('TICKET', CharField()))

    # Combiner tous les objets (avis et tickets) et les trier par date de création
    posts = sorted(chain(reviews_followed, tickets_user_and_followed),
                   key=lambda post: post.time_created, reverse=True)

    # Récupérer les tickets déjà répondu par des avis
    tickets_already_answer = tickets_user_and_followed.filter(review__in=reviews_followed)

    return render(request, 'website/flux.html', context={'posts': posts, 'tickets_already_answer': tickets_already_answer})

@login_required
def create_ticket(request):
    """
    Permit to create a new ticket
    """
    ticket_form = forms.TicketForm()
    if request.method == "POST":
        ticket_form = forms.TicketForm(request.POST, request.FILES)
        if ticket_form.is_valid():
            ticket = ticket_form.save(commit=False)
            ticket.user = request.user
            ticket.save()
            return redirect('flux')
    context = {"ticket_form": ticket_form}
    return render(request, 'website/create_ticket.html', context=context)

@login_required
def create_review(request):
    """
    Permet de créer une nouvelle review avec un ticket associé
    """
    if request.method == "POST":
        ticket_form = forms.TicketForm(request.POST, request.FILES)
        review_form = forms.ReviewForm(request.POST)
        if ticket_form.is_valid() and review_form.is_valid():
            ticket = ticket_form.save(commit=False)
            ticket.user = request.user
            ticket.save()

            review = review_form.save(commit=False)
            review.user = request.user
            review.ticket = ticket
            review.save()

            return redirect('flux')
    else:
        ticket_form = forms.TicketForm()
        review_form = forms.ReviewForm()

    context = {"review_form": review_form, "ticket_form": ticket_form}
    return render(request, 'website/create_review.html', context=context)

@login_required
def create_review_from_ticket(request, ticket_id):
    """
    Permet de créer une review en réponse à un ticket
    """
    ticket = None
    review_form = forms.ReviewForm()

    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if request.method == "POST":
            review_form = forms.ReviewForm(request.POST)
            if review_form.is_valid():
                review = review_form.save(commit=False)
                review.user = request.user
                review.ticket = ticket
                review.save()
                return redirect('flux')
    except UnboundLocalError:
        return redirect('flux')
    except Ticket.DoesNotExist:
        return redirect('flux')

    return render(request, 'website/create_review_from_ticket.html', context={"ticket": ticket, "review_form": review_form})

@login_required
def display_posts(request):
    """
    Display all ticket et review from user connected
    """
    tickets = Ticket.objects.filter(user=request.user)
    tickets = tickets.annotate(content_type=Value('TICKET', CharField()))
    reviews = Review.objects.filter(user=request.user)
    reviews = reviews.annotate(content_type=Value('REVIEW', CharField()))
    posts = sorted(chain(reviews, tickets),
                   key=lambda post: post.time_created, reverse=True)
    return render(request, 'website/posts.html', context={"posts": posts})

@login_required
def follow_users(request):
    """
    Manage the subscription system
    """
    users_follow_you = [user_follow.user.username for user_follow in
                        UserFollows.objects.filter(followed_user=request.user.id)]

    users_followed = UserFollows.objects.filter(user_id=request.user)
    users_to_exclude = [user_followed.followed_user.username for user_followed in users_followed]
    users_to_exclude.append(request.user.username)
    users_to_follow = User.objects.exclude(username__in=users_to_exclude)
    if request.method == "POST":
        to_follow = User.objects.get(pk=request.POST["to_follow"])
        if to_follow in users_to_follow:
            UserFollows(user=request.user, followed_user=to_follow).save()
    users_followed = UserFollows.objects.filter(user_id=request.user)
    return render(request, 'website/follow_users.html', context={"users_followed": users_followed,
                                                                 "users_to_follow": users_to_follow,
                                                                 "users_follow_you": users_follow_you})

@login_required
def delete_follow_user(request, user_id):
    """
        Delete a follow user
    """
    try:
        user_follow = UserFollows.objects.get(user=request.user, followed_user=user_id)
    except UserFollows.DoesNotExist:
        return redirect('follow_users')
    if request.method == "POST":
        user_follow.delete()
        return redirect('follow_users')

@login_required
def delete_review(request, review_id):
    """
        Delete a review
    """
    try:
        review = Review.objects.get(pk=review_id)
        if review.user != request.user:
            return redirect('posts')
    except UserFollows.DoesNotExist:
        return redirect('posts')
    if request.method == "POST":
        review.delete()
        return redirect('posts')

@login_required
def update_review(request, review_id):
    """
        Modifie a review
    """
    try:
        review = Review.objects.get(pk=review_id)
        if review.user != request.user:
            return redirect('posts')
    except Review.DoesNotExist:
        return redirect('posts')

    review_form = forms.ReviewForm(instance=review)
    if request.method == "POST":
        update_form = forms.ReviewForm(request.POST)
        if update_form.is_valid():
            review_updated = update_form.save()
            return redirect('posts')
    context = {"review_form": review_form}
    return render(request, 'website/update_review.html', context=context)

@login_required
def delete_ticket(request, ticket_id):
    """
        Delete a review
    """
    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if ticket.user != request.user:
            return redirect('posts')
    except UserFollows.DoesNotExist:
        pass
    if request.method == "POST":
        ticket.delete()
        return redirect('posts')

@login_required
def update_ticket(request, ticket_id):
    """
        Modifie a ticket
    """
    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if ticket.user != request.user:
            return redirect('posts')
    except Ticket.DoesNotExist:
        return redirect('posts')
    ticket_form = forms.TicketForm(instance=ticket)
    if request.method == "POST":
        update_form = forms.TicketForm(request.POST, request.FILES)
        if update_form.is_valid():
            ticket_updated = update_form.save()
            return redirect('posts')
    context = {"ticket_form": ticket_form}
    return render(request, 'website/update_ticket.html', context=context)


import os
import unittest
import dateutil.parser
from dropmunch import munch_data, munch_spec

iso8601_timestamp1 = '2007-10-01T13:47:12.345Z'
valid_datafile_name = 'DATAspecvalid_{0}.txt'.format(iso8601_timestamp1)

class DataFileProcessing(unittest.TestCase):

    def setUp(self):
        self.working_directory = os.getcwd() + '/fixtures/'
        self.munch_data = munch_data.MunchData(self.working_directory)
        self.munch_spec = munch_spec.MunchSpec(self.working_directory)

    def tearDown(self):
        self.munch_spec.delete_all_specs()

    """Test validation and processing of data files"""
    def test_datafile_found_db_spec(self):
        spec_name = 'DATAspecvalid'
        spec_columns = [munch_spec.SpecColumn('color', 7, 'TEXT')]
        spec = munch_spec.Spec(spec_name, spec_columns)

        self.munch_spec.persist_spec(spec)
        datafile_spec = self.munch_data.get_datafile_spec(valid_datafile_name)

        self.assertIsNotNone(datafile_spec,
                             'providing a filename corresponding to a spec available in the db '
                             'produces a valid DatafileSpec object')

    def test_valid_datafile(self):
        spec_name = 'DATAspecvalid'
        spec_columns = [munch_spec.SpecColumn('color', 7, 'TEXT'),
                        munch_spec.SpecColumn('sohot_rightnow', 1, 'BOOLEAN')]
        spec = munch_spec.Spec(spec_name, spec_columns)
        datafile_spec = munch_data.DataFileSpec(spec, dateutil.parser.parse('2007-10-01T13:47:12.345Z'))
        self.munch_spec.persist_spec(spec)
        import_log_row = self.munch_data.create_import_log(datafile_spec)

        datafile_spec = self.munch_data.get_datafile_spec(valid_datafile_name)

        self.assertEquals(self.munch_data.process_datafile(valid_datafile_name, datafile_spec, import_log_row['id']),
                          3,
                          '3 rows are processed from valid datafile')

    def test_padded_boolean_column(self):
        self.munch_spec.process_spec_from_file('DATApaddedbool.csv')
        spec = self.munch_spec.load_spec_from_db('DATApaddedbool')
        datafile_spec = munch_data.DataFileSpec(spec, dateutil.parser.parse(iso8601_timestamp1))
        import_log_row = self.munch_data.create_import_log(datafile_spec)

        self.assertEquals(self.munch_data.process_datafile('DATApaddedbool_{0}.txt'.format(iso8601_timestamp1),
                                                           datafile_spec,
                                                           import_log_row['id']),
                          3,
                          '3 rows are processed from valid datafile')

    def test_invalid_datafile_row(self):
        self.munch_spec.process_spec_from_file('DATAspecvalid.csv')
        spec = self.munch_spec.load_spec_from_db('DATAspecvalid')
        datafile_spec = munch_data.DataFileSpec(spec, dateutil.parser.parse(iso8601_timestamp1))
        import_log_row = self.munch_data.create_import_log(datafile_spec)

        filename = 'DATAspecvalid_{0}_badrow.txt'.format(iso8601_timestamp1)
        processed_row_count = self.munch_data.process_datafile(filename,
                                                               datafile_spec,
                                                               import_log_row['id'])

        self.assertEquals(processed_row_count,
                          2,
                          '2 rows are processed from valid datafile {0}'.format(filename))
        self.assertEquals(self.munch_data.row_failure_count,
                          1,
                          '1 row failed to be processed from datafile {0}'.format(filename))

### TODO - implement unit tests for invalid data conditions :
    # def test_data_file_missing_db_spec_found_file_spec(self):
    #     """when data file's spec is found in filesystem, but
    #     not in the database a warning is logged and the data file is skipped"""
    #     self.assertTrue(False)
    #
    # def test_data_file_missing_spec(self):
    #     """when data file's spec isn't found in database or in the filesystem, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_file_empty(self):
    #     """when a data file is processed containing no rows, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_file_io_error(self):
    #     """when an io error occurs, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_column_invalid_datatype(self):
    #     """when a column can't be parsed using on the spec width and datatype, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_row_missing_characters(self):
    #     """when a row contains fewer characters than spec, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_row_extra_characters(self):
    #     """when a row contains more characters than spec, an error is raised"""
    #     self.assertTrue(False)
    #
    # def test_data_error_saving_to_db(self):
    #     """when an error occurs while saving data to the database, an error is raised"""
    #     self.assertTrue(False)


from fastapi import APIRouter, Body
from models.model import User, UserLogin
from auth.jwt_handler import signJWT

router = APIRouter(
    tags = ["User Routes"]
)

users = []

@router.get("/")
def get():
    return {"Hello": "Wob"}

@router.post("/user/signup")
def user_signup(user: User = Body(default=None)):
    users.append(user)
    return signJWT(user.email)

def check_user(data: UserLogin):
    for user in users:
        if user.email == data.email and user.password == data.password:
            return True
        return False

@router.post("/user/login")
def user_login(user: UserLogin = Body(default=None)):
    if check_user(user):
        return signJWT(user.email)
    else:
        return{
            "error": "Invalid login details"
        }


import pytest
import yaml
import os
import json

def load_configuration(filename):
    prod_config_file = filename
    with open(prod_config_file) as input_file:
        config_parameters = yaml.load(input_file, Loader=yaml.FullLoader)
    return config_parameters

def load_model_performance(filename):
    with open(filename, "r") as input_file:
        model_performance = json.load(input_file)
    return model_performance

def test_ATTC_1a():
    conf_parameters = load_configuration("tests/configuration.yml")
    candidate_model_performance = load_model_performance("tests/candidate_models_performance.json")

    model_accuracy = candidate_model_performance["general_metrics"]["accuracy"]
    minimal_accuracy = conf_parameters["target_metrics"]["accuracy"]

    assert model_accuracy >= minimal_accuracy


from fastapi import APIRouter, Depends, HTTPException
from pyairtable.api.table import Table
from pyairtable.formulas import match
from sentry_sdk import capture_message

from app.auth.auth_bearer import JWTBearer
from app.routers.attendees import update_attendee
from ..dependencies import get_mobile_table, get_registration_table
from ..auth.auth_handler import verify_jwt, decode_jwt
from ..models.attendee import UpdatedAttendee, recordToAttendee

router = APIRouter(prefix="/verify", tags=["verify"])

@router.get("/discord", dependencies=[Depends(JWTBearer())])
async def verify_discord(
    email: str, disc_username: str, table: Table = Depends(get_mobile_table)
):
    res = []
    for i in table.all(formula=match({"Email": email})):
        res.append(recordToAttendee(i))

    if len(res) == 0:
        e = HTTPException(
            status_code=400,
            detail="No user with specified email found.",
        )
        capture_message("No user with specified email found.")
        raise e
    else:
        user = res[0]
        if user.discord_id:
            e = HTTPException(
                status_code=400,
                detail="Discord username already set.",
            )
            capture_message("Discord username already set.")
            raise e

        try:
            table.update(user.airtable_id, {"Discord ID": disc_username})
        except:
            raise HTTPException(status_code=500, detail="Updating attendee failed")

    return user.first_name, user.last_name

@router.get("/{token}")
async def verify_email(token: str, table: Table = Depends(get_registration_table)):
    # token is invalid or expired
    if not verify_jwt(token):
        e = HTTPException(
            status_code=403,
            detail="Expired or invalid verification link. Please try registering again!",
        )
        capture_message("Invalid or expired verification link.")
        raise e

    payload = decode_jwt(token)
    attendee_id = payload["id"]
    email_type = payload["type"]

    if not attendee_id or not email_type:
        # token does not contain payload id
        e = HTTPException(status_code=403, detail="Invalid verification link.")
        capture_message("Invalid verification link.")
        raise e

    field_name = "Parent Email Verified" if email_type == "parent" else "Email Verified"

    try:
        attendee = table.update(attendee_id, {field_name: True})

        parent_email_verified = (
            attendee["fields"]["Parent Email Verified"]
            if "Parent Email Verified" in attendee["fields"]
            else False
        )
        email_verified = (
            attendee["fields"]["Email Verified"]
            if "Email Verified" in attendee["fields"]
            else False
        )

        return {
            "parent_email_verified": parent_email_verified,
            "email_verified": email_verified,
        }
    except:
        # attendee does not exist
        e = HTTPException(status_code=403, detail="Invalid attendee ID.")
        capture_message("Invalid attendee ID.")
        raise e


# set FLASK_APP=app
# set FLASK_ENV=development
# flask run

from flask import Flask, render_template, request
import semsim_funcs
import semtag_funcs
import semnull_funcs
import semcluster_funcs
import textCoder_funcs

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/semsim')
def semsim():
    return render_template('semsim.html')

@app.route('/semsim_results', methods=['GET', 'POST'])
def semsim_results():
    target_words = ''.join(request.form['target_words'].lower().split()).split(',')
    contrast_word = request.form['contrast_word']
    pos_words = ''.join(request.form['pos_words'].lower().split()).split(',')
    neg_words = ''.join(request.form['neg_words'].lower().split()).split(',')
    relative_sims, target_words, pos_words, neg_words, error_message = semsim_funcs.get_sims(target_words, pos_words, neg_words, contrast_word=contrast_word)
    print(relative_sims)
    targets_with_sims = []
    for z in zip(target_words, relative_sims):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        targets_with_sims.append(this_str)
    return render_template('semsim_results.html', error_message=error_message, targets_with_sims=targets_with_sims, target_words=target_words, pos_words=pos_words, neg_words=neg_words, relative_sims=relative_sims, contrast_word=contrast_word)

@app.route('/semtag')
def semtag():
    return render_template('semtag.html')

@app.route('/semtag_results', methods=['GET', 'POST'])
def semtag_results():
    text = request.form['text']
    tags = request.form['tags']
    tags_in_vocab, output_all, output_sim_all, output_per_paragraph, output_sim_per_paragraph, output_paragraphs = semtag_funcs.get_semtags(text, tags)
    tags_in_vocab_str = ','.join(tags_in_vocab)
    output_per_paragraph_str = ','.join(output_per_paragraph)
    output_sim_per_paragraph_str = ','.join([str(round(a, 3)) for a in output_sim_per_paragraph])
    text = text.split('\n')
    output_tag_with_sim_per_paragraph = []
    for z in zip(output_per_paragraph, output_sim_per_paragraph):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        output_tag_with_sim_per_paragraph.append(this_str)
    output_tag_with_sim_all = []
    for z in zip(output_all, output_sim_all):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        output_tag_with_sim_all.append(this_str)
    nested_info_per_paragraph = []
    for z in zip(output_per_paragraph, output_sim_per_paragraph, output_paragraphs):
        this_row = [z[0], round(z[1], 3), z[2]]
        nested_info_per_paragraph.append(this_row)
    return render_template('semtag_results.html', nested_info_per_paragraph=nested_info_per_paragraph,output_tag_with_sim_per_paragraph=output_tag_with_sim_per_paragraph, output_tag_with_sim_all=output_tag_with_sim_all,tags_in_vocab_str=tags_in_vocab_str, text=text, output_all=output_all, output_sim_all=output_sim_all, output_per_paragraph_str=output_per_paragraph_str, output_sim_per_paragraph_str=output_sim_per_paragraph_str)

@app.route('/semnull')
def semnull():
    return render_template('semnull.html')

@app.route('/semnull_results', methods=['GET', 'POST'])
def semnull_results():
    target_words = ''.join(request.form['target_words'].lower().split()).split(',')
    contrast_word = request.form['contrast_word']
    pos_words = ''.join(request.form['pos_words'].lower().split()).split(',')
    neg_words = ''.join(request.form['neg_words'].lower().split()).split(',')
    scores_to_test = request.form['scores_to_test']
    template_sentence = request.form['template_sentence']
    template_pos = request.form['template_pos']
    print(target_words)
    print(contrast_word)
    null_distr_scores = semnull_funcs.get_semnull(pos_words, neg_words, template_sentence, template_pos)
    N_random_words_found = len(null_distr_scores)
    print(N_random_words_found)
    p_values, target_words_scores, target_words_output, scores_to_test_output, error_message = semnull_funcs.get_p(target_words, pos_words, neg_words, scores_to_test, null_distr_scores, contrast_word=contrast_word)
    p_values_nested = []
    target_words_output.extend(scores_to_test_output)
    print(p_values)
    for z in zip(target_words_output, target_words_scores, p_values):
        p_values_nested.append([z[0], round(z[1], 3), z[2]])
    print(p_values_nested)
    return render_template('semnull_results.html', error_message=error_message,null_distr_scores=null_distr_scores, N_random_words_found=N_random_words_found, p_values_nested=p_values_nested)

@app.route('/textCoder')
def textCoder():
    return render_template('textCoder.html')

@app.route('/textCoder_results', methods=['GET', 'POST'])
def textCoder_results():
    text = request.form['text']
    print(text)
    knowledge_list = textCoder_funcs.get_textCoder(text)
    topics_str = ','.join([k[0] for k in knowledge_list])
    attributes_str = ', '.join([a[0] for k in knowledge_list for a in k[1]])
    text_in_paragraphs = text.split("\n")
    nested_info_per_paragraph = []
    nested_info_per_paragraph_count = []
    nested_info_per_paragraph_parindex = []
    topics_str_list = [k[0] for k in knowledge_list]
    attr_str_list = [', '.join([a[0] for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_list):
        nested_info_per_paragraph.append([z[0], z[1]])
    attr_str_count_list = [', '.join([a[0] + " (" + str(a[1]) + ")" for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_count_list):
        nested_info_per_paragraph_count.append([z[0], z[1]])
    attr_str_parindex_list = [', '.join([a[0] + " (" + str(a[2]) + ")" for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_parindex_list):
        nested_info_per_paragraph_parindex.append([z[0], z[1]])
    return render_template('textCoder_results.html', topics_str=topics_str, attributes_str=attributes_str, nested_info_per_paragraph=nested_info_per_paragraph, text_in_paragraphs=text_in_paragraphs,nested_info_per_paragraph_count=nested_info_per_paragraph_count,nested_info_per_paragraph_parindex=nested_info_per_paragraph_parindex)

@app.route('/semcluster')
def semcluster():
    return render_template('semcluster.html')

@app.route('/semcluster_results', methods=['GET', 'POST'])
def semcluster_results():
    words = request.form['words']
    cluster_labels, cluster_items = semcluster_funcs.get_clusters(words)
    cluster_info = []
    for z in zip(cluster_labels, cluster_items):
        cluster_info.append(['Cluster label: ' + z[0], 'Items: ' + ', '.join(z[1])])
    return render_template('semcluster_results.html', words=words, cluster_info=cluster_info)


from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

from accounts.models import User

class Category(MPTTModel):
    name = models.CharField(max_length=100, verbose_name='Имя категории')
    slug = models.SlugField(max_length=100, verbose_name='Ссылка')
    parent = TreeForeignKey('self',
                            verbose_name='Родитель',
                            related_name='children',
                            on_delete=models.SET_NULL,
                            blank=True,
                            null=True
                            )

    class MPTTMeta:
        level_attr = 'mptt_level'
        order_insertion_by = ['name']

    class Meta:
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'

    def __str__(self):
        return self.name

class Ingredients(models.Model):
    name = models.CharField(max_length=100, verbose_name='Ингредиент')
    slug = models.SlugField(max_length=100, verbose_name='Ссылка')

    class Meta:
        verbose_name = 'Ингредиент'
        verbose_name_plural = 'Ингредиенты'

    def __str__(self) -> str:
        return self.name

class Dish(models.Model):
    name = models.CharField(max_length=50, verbose_name='Название блюда')
    slug = models.SlugField(max_length=50)
    ingredients = models.ForeignKey(Ingredients,
                                    related_name='ingredients',
                                    verbose_name='Ингредиенты',
                                    on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Блюда'
        verbose_name_plural = 'Блюда'

    def __str__(self) -> str:
        return self.name

class DeyWeek(models.Model):
    dey_name = models.CharField(max_length=50, verbose_name='День недели')

    class Meta:
        verbose_name = 'День'
        verbose_name_plural = 'Дни'

    def __str__(self) -> str:
        return self.dey_name

class DishWeek(models.Model):
    dish = models.ForeignKey(Dish,
                             related_name='dishweek',
                             verbose_name='Название блюда',
                             on_delete=models.CASCADE,
                             )
    slug = models.SlugField(max_length=50)
    ingredients = models.ForeignKey(Ingredients,
                                    related_name='ingredient',
                                    verbose_name='Ингредиенты',
                                    on_delete=models.CASCADE)
    dey_week = models.ForeignKey(DeyWeek,
                                    related_name='deyweek',
                                    verbose_name='Дни',
                                    on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Блюдо на неделю'
        verbose_name_plural = 'Блюда на неделю '

    def __str__(self) -> str:
        return f"Блюдо {self.dish}"

class Menu(models.Model):
    author = models.ForeignKey(
        User,
        verbose_name='Автор',
        related_name='Menu',
        on_delete=models.CASCADE)
    title = models.CharField(max_length=200, verbose_name='Заглавление')
    category = models.ForeignKey(Category,
                                 verbose_name='Категория',
                                 related_name='Menu',
                                 on_delete=models.SET_NULL,
                                 null=True)
    created_at = models.DateTimeField(
        auto_now_add=True, verbose_name='Дата создания')
    Ingredients = models.ForeignKey(
        Ingredients,
        verbose_name='Ингредиенты',
        related_name='Ингредиент',
        on_delete=models.CASCADE)
    breakfast = models.ForeignKey(
        Dish,
        verbose_name='Завтрак',
        related_name='Завтрак',
        on_delete=models.CASCADE)
    lunch = models.ForeignKey(
        Dish,
        verbose_name='Обед',
        related_name='Обед',
        on_delete=models.CASCADE)
    dinner = models.ForeignKey(
        Dish,
        verbose_name='Ужин',
        related_name='Ужин',
        on_delete=models.CASCADE)
    create_at = models.DateTimeField(
        null=True, blank=True, verbose_name='Дата создания')

    class Meta:
        verbose_name = 'Меню'
        verbose_name_plural = 'Меню'

    def __str__(self):
        return f"Владелец:{self.author} Завтрак: {self.breakfast} Обед: {self.lunch} Ужин:{self.dinner} Дата Создания записи {self.create_at}"

class Recipe(models.Model):
    name = models.CharField(max_length=100, verbose_name='Название')
    serves = models.CharField(max_length=50, verbose_name='Служит')
    prep_time = models.DurationField(
        default=0, verbose_name='Время подготовки')
    cook_time = models.DurationField(
        default=0, verbose_name='Время приготовления')
    ingredients = models.ForeignKey(
        Ingredients,
        verbose_name='Ингредиенты',
        related_name='Ингредиенты',
        on_delete=models.CASCADE)
    directions = models.TextField(verbose_name='Направление')
    menu = models.ForeignKey(Menu,
                             verbose_name='Меню',
                             related_name='recipe',
                             on_delete=models.SET_NULL,
                             null=True,
                             blank=True
                             )

    class Meta:
        verbose_name = 'Рецепт'
        verbose_name_plural = 'Рецепты'

    def __str__(self) -> str:
        return self.name

class Comment(models.Model):
    title = models.CharField(max_length=50, verbose_name='Заглавление')
    email = models.CharField(max_length=100, verbose_name='Email')
    message = models.TextField(max_length=500, verbose_name='Сообщение')
    menu = models.ForeignKey(Menu,
                             verbose_name='Меню',
                             related_name='comment',
                             on_delete=models.CASCADE,
                             )
    author = models.ForeignKey(User,
                               verbose_name='Пользователь',
                               on_delete=models.SET_NULL,
                               null=True)

    class Meta:
        verbose_name = 'Коментарий'
        verbose_name_plural = 'Коментарии'

    def __str__(self) -> str:
        return self.title


import pandas as pd

xls = pd.ExcelFile('/Users/scott/Downloads/AAPL5yr.xlsx')
df1 = pd.read_excel(xls)

#200/50 are used for Golden Cross/Death Cross
df1['50dayMA'] = df1['Close/Last'].rolling(50).mean()
df1['200dayMA'] = df1['Close/Last'].rolling(200).mean()

#Bollinger Bands use 20 day moving average of the 'typical price'
#and 20 day standard deviations to create their margins
df1['Typical'] = (df1['Close/Last'] + df1['High'] + df1['Low'])/3
df1['20dayMA'] = df1['Typical'].rolling(20).mean()
df1['20dayStddev'] = df1['Typical'].rolling(20).std()
df1['20dayUpperBand'] = df1['20dayMA'] + 2*df1['20dayStddev']
df1['20dayLowerBand'] = df1['20dayMA'] - 2*df1['20dayStddev']

#MACD uses the difference of the 26 and 12 day exponential moving average
#and the 9 day exponential moving average of the the difference
df1['26dayEWM'] = df1['Close/Last'].ewm(span=26, adjust=False).mean()
df1['12dayEWM'] = df1['Close/Last'].ewm(span=12, adjust=False).mean()
df1['MACD'] = df1['26dayEWM']-df1['12dayEWM']
df1['MACDEWM'] = (df1['MACD']).ewm(span=9, adjust=False).mean()


from abc import ABC, abstractmethod
import logging

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session
from sqlalchemy.pool import NullPool
from os import getenv
from dotenv import load_dotenv
from models import BaseTable

class Collector(ABC):

    def __init__(self) -> None:
        self.session_factory = SessionHandler()

        # set up logging
        self.logger = logging.getLogger(__name__)
        logging.basicConfig(
            level=logging.INFO,
            format='%(levelname)s-%(threadName)s: %(message)s'
        )
    @abstractmethod
    def _collect_surfaces(self, bag_building_id: str):
        raise NotImplementedError('Implement this')

    @abstractmethod
    def _collect_images(self, model: BaseTable):
        raise NotImplementedError('Implement this')

class SessionHandler:
    engine = None

    def __init__(self, echo=False):
        load_dotenv()

        user = getenv('user')
        host = getenv('host')
        name = getenv('dbname')
        self.engine = create_engine(
            f'postgresql://{user}:@{host}/{name}',
            poolclass=NullPool,
            echo=echo
        )

    def build(self) -> Session:
        return sessionmaker(bind=self.engine)()


x = input()
y = input()

dp = [[None for i in range(len(y)+1)] for j in range(len(x)+1)]

def lcs(x, y, lx, ly):
  if(lx==0 or ly ==0):
    return 0

  if(dp[lx][ly]!=None):
    return dp[lx][ly]

  if(x[lx-1]==y[ly-1]):

    ans = 1+lcs(x, y, lx-1, ly-1)
    dp[lx][ly] = ans
    return ans

  else:

    ans1 = lcs(x, y, lx-1, ly)
    ans2 = lcs(x, y, lx, ly-1)
    ans = max(ans1, ans2)
    dp[lx][ly] = ans
    return ans

print(lcs(x, y, len(x),len(y)))


# Мне надоело постоянно менять номер версии в разных файлах, поэтому появился данный скрипт :)

import io

newver = input("Введите номер новой версии: ")

f = open("installer.iss", "rt")
buf = f.readlines()
f.close()
f = open("installer.iss", "wt")
for item in buf:
	if item.startswith("#define MyAppVersion "):
		print(item.strip(), "---> ", end="")
		item = "#define MyAppVersion \"" + newver + "\"\n"
		print(item)
	f.write(item)
f.close()

f = io.open("main.py", mode="rt", encoding="utf-8")
buf = f.readlines()
f.close()
f = io.open("main.py", mode="wt", encoding="utf-8")
for item in buf:
	if item.startswith("VER = "):
		print(item.strip(), "---> ", end="")
		item = "VER = \"" + newver + "\"\n"
		print(item)
	f.write(item)
f.close()

f = io.open("README.md", mode="rt", encoding="utf-8")
buf = f.readlines()
f.close()
f = io.open("README.md", mode="wt", encoding="utf-8")
for item in buf:
	if item.startswith("Текущая версия: "):
		print(item.strip(), "---> ", end="")
		item = "Текущая версия: **v" + newver + "**    \n"
		print(item)
	f.write(item)
f.close()


from gameAI import findBestMove, findRandomMove

# function that takes a gamestate and excecute the move on the board of the gamestate, given a difficulty.
# Inputs: gs (game state object), difficulty ('easy' or 'hard'), turn (integer turn counter)
# Output: updated turn counter if move is made.

def computerMove(gs, difficulty, turn):
    possibleMoves = gs.getAllPossibleMoves()
    if difficulty == 'easy':
        AImove = findRandomMove(possibleMoves)
    elif difficulty == 'hard':
        AImove = findBestMove(gs, possibleMoves)
    else:
        print('{} is not a valid difficulty'.format(difficulty))
        return turn
    gs.makeMove(AImove)
    return turn + 1


import itertools
import math
from collections import defaultdict

import brownie
import pytest

from scripts.utils import pack_values

ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

@pytest.fixture(scope="module", autouse=True)
def registry(
    Registry,
    provider,
    gauge_controller,
    alice,
    lending_swap,
    lp_token,
    n_coins,
    is_v1,
    rate_method_id,
    underlying_decimals,
    wrapped_decimals,
    chain,
):
    registry = Registry.deploy(provider, gauge_controller, {"from": alice})
    registry.add_pool(
        lending_swap,
        n_coins,
        lp_token,
        rate_method_id,
        pack_values(wrapped_decimals),
        pack_values(underlying_decimals),
        hasattr(lending_swap, "initial_A"),
        is_v1,
        "",
        {"from": alice},
    )
    chain.sleep(10)
    registry.remove_pool(lending_swap, {"from": alice})
    yield registry

@pytest.mark.itercoins("send", "recv")
def test_find_pool(registry, wrapped_coins, underlying_coins, send, recv):
    assert registry.find_pool_for_coins(wrapped_coins[send], wrapped_coins[recv]) == ZERO_ADDRESS
    assert (
        registry.find_pool_for_coins(underlying_coins[send], underlying_coins[recv]) == ZERO_ADDRESS
    )

def test_get_n_coins(registry, lending_swap):
    assert registry.get_n_coins(lending_swap) == [0, 0]

def test_get_coins(registry, lending_swap):
    assert registry.get_coins(lending_swap) == [ZERO_ADDRESS] * 8
    assert registry.get_underlying_coins(lending_swap) == [ZERO_ADDRESS] * 8

def test_get_decimals(registry, lending_swap):
    assert registry.get_decimals(lending_swap) == [0] * 8
    assert registry.get_underlying_decimals(lending_swap) == [0] * 8

def test_get_rates(registry, lending_swap):
    assert registry.get_rates(lending_swap) == [0] * 8

@pytest.mark.itercoins("send", "recv")
def test_get_coin_indices(registry, lending_swap, underlying_coins, wrapped_coins, send, recv):
    with brownie.reverts("No available market"):
        registry.get_coin_indices(lending_swap, wrapped_coins[send], wrapped_coins[recv])
    with brownie.reverts("No available market"):
        registry.get_coin_indices(lending_swap, underlying_coins[send], underlying_coins[recv])

@pytest.mark.once
def test_get_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_balances(lending_swap)

@pytest.mark.once
def test_get_underlying_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_underlying_balances(lending_swap)

@pytest.mark.once
def test_get_admin_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_admin_balances(lending_swap)

@pytest.mark.once
def test_get_virtual_price_from_lp_token(alice, registry, lp_token):
    with brownie.reverts():
        registry.get_virtual_price_from_lp_token(lp_token)

@pytest.mark.once
def test_get_pool_from_lp_token(registry, lp_token):
    assert registry.get_pool_from_lp_token(lp_token) == ZERO_ADDRESS

@pytest.mark.once
def test_get_lp_token(registry, lending_swap):
    assert registry.get_lp_token(lending_swap) == ZERO_ADDRESS

def test_coin_count_is_correct(registry):

    assert registry.coin_count() == 0

def test_get_all_swappable_coins(registry, wrapped_coins, underlying_coins):
    coin_set = set(map(str, itertools.chain(wrapped_coins, underlying_coins)))
    coin_count = len(coin_set)

    coins = set(registry.get_coin(i) for i in range(coin_count))

    assert coins == {ZERO_ADDRESS}

@pytest.mark.once
def test_last_updated_getter(registry, history):
    registry_txs = history.filter(receiver=registry.address)
    assert math.isclose(registry_txs[-1].timestamp, registry.last_updated())

def test_coin_swap_count(registry, wrapped_coins, underlying_coins):
    coins = set(map(str, itertools.chain(wrapped_coins, underlying_coins)))

    for coin in coins:
        assert registry.get_coin_swap_count(coin) == 0

def test_swap_coin_for(registry, wrapped_coins, underlying_coins):
    wrapped_coins = list(map(str, wrapped_coins))
    underlying_coins = list(map(str, underlying_coins))
    pairings = defaultdict(set)

    wrapped_pairs = itertools.combinations(wrapped_coins, 2)
    underlying_pairs = itertools.combinations(underlying_coins, 2)

    for coin_a, coin_b in itertools.chain(wrapped_pairs, underlying_pairs):
        pairings[coin_a].add(coin_b)
        pairings[coin_b].add(coin_a)

    for coin in pairings.keys():
        coin_swap_count = len(pairings[coin])
        available_swaps = {
            registry.get_coin_swap_complement(coin, i) for i in range(coin_swap_count)
        }

        assert available_swaps == {ZERO_ADDRESS}


#MORFOLOJİK OPERASYONLAR
import cv2
import matplotlib.pyplot as plt
import numpy as np

#
img = cv2.imread("img.png",0)

plt.figure(),plt.imshow(img , cmap = "gray"),plt.axis("off")

#EROZYON -ön plandaki nesnenin sınırlarını aşındırır
kernel = np.ones((3,3),dtype = np.uint8)
result = cv2.erode(img, kernel, iterations = 1)
plt.figure(),plt.imshow(result , cmap = "gray"),plt.axis("off")

#GENİŞLEME - ön plandaki nesnenin sınırlarını genişletir - erozyonun tam tersidir
result2 = cv2.dilate(img , kernel , iterations = 1)
plt.figure(),plt.imshow(result2 , cmap = "gray"),plt.axis("off")

#AÇMA - erozyon + genişleme - gürültünün giderilmesinde faydalıdır
whiteNoise = np.random.randint(0,2,size = img.shape[:2])
whiteNoise = whiteNoise * 255
plt.figure(),plt.imshow(whiteNoise , cmap = "gray"),plt.axis("off")

noise_img = whiteNoise + img
plt.figure(),plt.imshow(noise_img , cmap = "gray"),plt.axis("off")

#açılma
opening = cv2.morphologyEx(noise_img.astype(np.float32),cv2.MORPH_OPEN,kernel)
plt.figure(),plt.imshow(opening , cmap = "gray"),plt.axis("off"),plt.title("Açilma")

#KAPATMA - genişleme+erozyon - ön plandaki nesnelerin içindeki küçük delikleri veya nesne üzerindeki küçük siyah noktaları kapatmak için kullanışlıdır
blackNoise = np.random.randint(0,2,size = img.shape[:2])
blackNoise = whiteNoise  * -255
black_noise_img = img+blackNoise
black_noise_img[black_noise_img<=-245] =0
plt.figure(),plt.imshow(black_noise_img , cmap = "gray"),plt.axis("off"),plt.title("black noise")

closing = cv2.morphologyEx(black_noise_img.astype(np.float32),cv2.MORPH_CLOSE,kernel)
plt.figure(),plt.imshow(closing , cmap = "gray"),plt.axis("off"),plt.title("Kapanma")

#MORFOLOJİK GRADYAN - GENİŞLEME - EROZYON - GRADIENT
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
plt.figure(),plt.imshow(gradient , cmap = "gray"),plt.axis("off"),plt.title("GRADIENT")
plt.show()


import click
from datetime import timedelta
from pathlib import Path
from typing import Iterable

# this library does not come with type stubs, so we tell mypy to ignore it
import pytimeparse  # type: ignore

from urbasys import log_utils
from urbasys.urbackup import retention

BACKUP_ROOT_DIR = "/urbackup/mirror"

@click.group()
@click.option("-v", "--verbose", count=True)
@click.option("-q", "--quiet", count=True)
def main(verbose, quiet):
    log_utils.setup_logging(verbose, quiet)

@main.command(name="retain-monthlies", help="Keeps one backup snapshot per month.")
@click.argument(
    "backups-root-dirs",
    nargs=-1,
    type=click.Path(path_type=Path, dir_okay=True, exists=True),
)
@click.option("-n", "--dry-run", is_flag=True)
@click.option(
    "--keep-latest",
    default=30,
    type=int,
    help="The number of latest snapshots to keep. "
    "After that only the oldest snapshot of the month will be kept.",
)
def retain_monthlies(
    backups_root_dirs: Iterable[Path], dry_run: bool, keep_latest: int
) -> None:
    for backup_root_dir in backups_root_dirs:
        retention.retain_monthlies(backup_root_dir, dry_run, keep_latest)

@main.command(
    name="delete-old",
    help="Deletes backup folders older than a certain amount of time.",
)
@click.argument(
    "backups-root-dirs",
    nargs=-1,
    type=click.Path(path_type=Path, dir_okay=True, exists=True),
)
@click.option("-n", "--dry-run", is_flag=True)
@click.option(
    "--max-age",
    required=True,
    type=lambda arg: timedelta(seconds=pytimeparse.parse(arg)),
    help="Snapshots older than this will be deleted.",
)
@click.option(
    "--min-keep",
    default=5,
    type=int,
    help="The minimum number of snapshots to keep (even if they are older than `--max-age`).",
)
def delete_old(
    backups_root_dirs: Iterable[Path], dry_run: bool, max_age: timedelta, min_keep: int
) -> None:
    for backup_root_dir in backups_root_dirs:
        retention.delete_old(backup_root_dir, dry_run, max_age, min_keep)


import os
import numpy as np
import matplotlib.pyplot as plt

from config import *
from model import get_model
from data import get_single_test

def evaluate(test_num):
    model = get_model(train=TRAIN_MODEL)
    print("got model")
    test = get_single_test(test_num)
    print("got test")

    num_clips = test.shape[0] - CLIP_LEN
    clips = np.zeros((num_clips, *DIM, N_CHANNELS))

    # apply sliding window technique to get the clips
    for i in range(num_clips):
        clip = np.zeros((*DIM, N_CHANNELS))
        for j in range(CLIP_LEN):
            clip[j] = test[i+j, :, :, :]
        clips[i] = clip

    # get reconstruction cost of all the clips
    reconstructed_clips = model.predict(clips, batch_size=BATCH_SIZE)
    cost = np.array([
        np.linalg.norm( clips[i] - reconstructed_clips[i] )
        for i in range(num_clips)
    ])
    # arregularity score
    sa = (cost - np.min(cost)) / np.max(cost)
    # regularity score
    sr = 1.0 - sa

    # plot scores
    plt.plot(sr)
    plt.ylabel('regularity score Sr(t)')
    plt.xlabel('frame t')
    if not os.path.exists(FIG_PATH):
        os.makedirs(FIG_PATH)
    plt.savefig(f'{FIG_PATH}/Figure_{(test_num or TEST_NUM):03d}.png')
    # plt.show()
    plt.clf()

if __name__=="__main__":
    for i in range(4, 16):
        evaluate(i)


import time
start_time = time.time()

from global_var import *
from demandSimulate import *
from oneStepOptimization_poly import *
from sklearn import preprocessing
from sklearn.linear_model import LinearRegression

from scipy.stats import qmc

def microgrid_poly2d():

    # stock
    alpha = 0.5; K0 = 0; sigma = 2; processType = "Regular";

    # backward simulation

    sampler = qmc.Sobol(d=2, scramble=False)
    # 512 samples
    W = sampler.random_base2(m=9)
    X0 = -10 + 20*W[:,0]; # X0 in [-10,10]
    ICord = 10*W[:,1];    # Ic in[0,Imax = 10]
    ICord_rep = np.repeat(ICord,batch);
    X0_rep = np.repeat(X0,batch);

    demandMatrix = demandSimulate(alpha, K0, sigma, 1, nsim, dt, X0_rep);
    # I0 is 5
    finalCost = -200 * ((ICord_rep-5))*(ICord_rep<5)
    # 20 rows, 512 columns,create 20x512 matrix, filled by column wise with order = "F" (default fill row),
    # take row sum mean  to get final cost for each starting site
    finalCost = np.mean(finalCost.reshape((batch,sites),order = "F"),axis = 0)

    costNext = np.zeros((sites,no_regime));

    for r in range(no_regime):
        costNext[:,r] = finalCost;

    modelIndx = 0; Model = [None] * (nstep)
    polyMdl = [None]*no_regime
    for iStep in range(nstep,0,-1):
        for r in range(no_regime):
            polyMdl[r] = LinearRegression()

            X_train = np.column_stack((X0, ICord))

            poly = preprocessing.PolynomialFeatures(degree=3)
            X_poly = poly.fit_transform(X_train)

            y_train = costNext[:, r]
            polyMdl[r].fit(X_poly, y_train)
        print(iStep)
        print(costNext[:10,0])
        print("\n")
        # generate X_t
        demandMatrix = demandSimulate(alpha, K0, sigma, 1, nsim, dt, X0_rep);

        # optimize basedd on X_t-1,I_t
        cost, _, control, nextInventory, nextRegime, imbalance, batteryOutput =oneStepOptimization_microgrid(
            demandMatrix[:, 1], ICord_rep, polyMdl)

        for r in range(no_regime):
            costNext[:, r] = np.mean(cost[:, r].reshape((batch, sites),order = "F"), axis=0)

        Model[modelIndx] = polyMdl.copy();
        modelIndx+=1;

    # forward simulations
    np.random.seed(10)
    simOutSample = 2000;

    X0 = np.zeros(simOutSample);
    I0 = 5;
    demandMatrix = demandSimulate(alpha, K0, sigma, nstep, simOutSample, maturity, X0);

    inventoryForward = np.zeros((simOutSample,nstep+1));
    inventoryForward[:, 0] = I0

    regimeForward = np.zeros((simOutSample, nstep + 1))
    regimeForward[:, 0] = 0

    Bt = np.zeros((simOutSample, nstep))
    St = np.zeros((simOutSample, nstep))
    dieselPower = np.zeros((simOutSample, nstep))
    trueCost = np.zeros((simOutSample, nstep))
    artificialCost = np.zeros((simOutSample, nstep))

    costForward = np.zeros((simOutSample, nstep + 1))

    for iStep in range(nstep):
        _, _, control, nextInventory, nextRegime, imbalance, batteryOutput = \
        oneStepOptimization_microgrid(demandMatrix[:, iStep], inventoryForward[:, iStep], Model[nstep - iStep-1])
        row_idx = tuple(np.arange(0,simOutSample))
        col_idx = tuple(np.int64(regimeForward[:,iStep]))
        inventoryForward[:, iStep + 1] = nextInventory[row_idx,col_idx]
        regimeForward[:, iStep + 1] = nextRegime[row_idx,col_idx]
        Bt[:, iStep] = batteryOutput[row_idx,col_idx]
        St[:, iStep] = imbalance[row_idx,col_idx]
        dieselPower[:, iStep] = control[row_idx,col_idx]

        trueCost[:, iStep] = k[0] * (dieselPower[:, iStep] ** 0.9) * dt + switchCost * (regimeForward[:, iStep + 1] > regimeForward[:, iStep])
        artificialCost[:, iStep] = k[1] * St[:, iStep] * (St[:, iStep] > 0) * dt - k[2] * St[:, iStep] * (St[:, iStep] < 0) * dt

    penalty = -200 * (inventoryForward[:, -1] - I0) * (inventoryForward[:, -1] < I0)

    costForward = trueCost + artificialCost
    pathWiseCost = np.sum(costForward, axis=1) + penalty
    totalCost = np.mean(pathWiseCost)
    stdDeviation = np.std(pathWiseCost) / np.sqrt(simOutSample)
    totalTrueCost = np.mean(np.sum(trueCost, axis=1))
    totalArtificialCost = np.mean(np.sum(artificialCost, axis=1))

    print(f'nsim: {sites} x {batch}, regime=1, totalCost= {totalCost}, totalTrueCost= {totalTrueCost}, totalArtificialCost= {totalArtificialCost}')

if __name__ == "__main__":
    microgrid_poly2d()
    print(f'Running time: {time.time()- start_time} s')


import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from torch.nn.utils.rnn import pad_sequence
from torch.nn.utils.rnn import pack_padded_sequence
from torch.nn.utils.rnn import pad_packed_sequence
from gensim.models.word2vec import Word2Vec

import torch_geometric.nn as pyg_nn
import torch_geometric.utils as pyg_utils

import time
from datetime import datetime

import networkx as nx

# from torch_geometric.datasets import TUDataset
# from torch_geometric.datasets import Planetoid
from torch_geometric.data import DataLoader
from torch_geometric.data import Data
from torch_geometric.data import InMemoryDataset, Dataset, download_url

# import torch_geometric.transforms as T

from tensorboardX import SummaryWriter
# from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import pandas as pd
import os

import logging
from pathlib import Path
import json
import argparse
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, confusion_matrix
try:
   import cPickle as pickle
except:
   import pickle

from model import GNNStack, GNNStack2, GNNStack3
from data_loader import MyLargeDataset

# import nni
# from nni.utils import merge_parameter

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

logger = None

# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

#Additional Info when using cuda
if device.type == 'cuda':
    print('#', torch.cuda.get_device_name(0))
    # print('# Memory Usage:')
    # print('# Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
    # print('# Cached:   ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')

def plot_dataset(dataset):
    edges_raw = dataset.data.edge_index.numpy()
    edges = [(x, y) for x, y in zip(edges_raw[0, :], edges_raw[1, :])]
    labels = dataset.data.y.numpy()

    G = nx.Graph()
    G.add_nodes_from(list(range(np.max(edges_raw))))
    G.add_edges_from(edges)
    plt.subplot(111)
    options = {
                'node_size': 30,
                'width': 0.2,
    }
    nx.draw(G, with_labels=False, node_color=labels.tolist(), cmap=plt.cm.tab10, font_weight='bold', **options)
    plt.show()

def myprint(s, is_log=0):
    print(s)
    if is_log>0:
        logger.info(s)

def test(loader, model, is_validation=False):
    model.eval()

    correct = 0
    y_true = []
    y_pred = []
    for data in loader:
        with torch.no_grad():
            pred = model(data)
            # pred = pred.argmax(dim=1)
            label = data.y

        _, pred = torch.max(pred.data, 1)

        y_true.append(label.cpu())
        y_pred.append(pred.cpu())
        correct += pred.eq(label).sum().item()

    total = len(loader.dataset)

    # return correct / total
    y_true = torch.cat(y_true)
    y_pred = torch.cat(y_pred)
    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
    res = {
        "accuracy": accuracy_score(y_true, y_pred),
        "recall": recall_score(y_true, y_pred),
        "f1": f1_score(y_true, y_pred),
        "precision": precision_score(y_true, y_pred),
        "tn": tn,
        "fp": fp,
        "fn": fn,
        "tp": tp
    }
    # if res['f1'] < 0.01:
    #     logger.info("y_true: {}".format(y_true))
    #     logger.info("y_pred: {}".format(y_pred))
    return res

def print_result(phase, score, epoch = -1):
    if phase in ['train', 'vali']:
        myprint("Epoch {}, {}:\tAcc\tR\tP\tF1\tTN\tFP\tFN\tTP".format(epoch, phase), 1)
        myprint("Epoch {}, {}:\t{:.4f}\t{:.4f}\t{:.4f}\t{:.4f}\t{}\t{}\t{}\t{}".format(epoch, phase,
            score['accuracy'], score['recall'], score['precision'], score['f1'],
            score['tn'], score['fp'], score['fn'], score['tp']), 1)
    else:
        myprint("{}:\tAcc\tR\tP\tF1\tTN\tFP\tFN\tTP".format(phase), 1)
        myprint("{}:\t{:.4f}\t{:.4f}\t{:.4f}\t{:.4f}\t{}\t{}\t{}\t{}".format(phase,
            score['accuracy'], score['recall'], score['precision'], score['f1'],
            score['tn'], score['fp'], score['fn'], score['tp']), 1)

def train(params, dataset, writer, plot=False, print_grad=False):

    data_size = len(dataset)
    print("data_size:", data_size)
    logger.info("data_size: {}".format(data_size) )

    loader = DataLoader(dataset[:int(data_size * 0.8)], batch_size=params['batch_size'], shuffle=True)
    # loader = DataLoader(dataset[:800], batch_size=params['batch_size'], shuffle=True)

    vali_loader = DataLoader(dataset[int(data_size * 0.8): int(data_size * 0.9)], batch_size=params['batch_size'], shuffle=True)
    test_loader = DataLoader(dataset[int(data_size * 0.9):], batch_size=params['batch_size'], shuffle=True)

    # build model
    if params['model_type'] == 'GNNStack2':
        logger.info("loading model: GNNStack2")
        model = GNNStack2(params)
    elif params['model_type'] == 'GNNStack3':
        logger.info("loading model: GNNStack3")
        model = GNNStack3(params)
    else:
        logger.info("loading model: GNNStack")
        model = GNNStack(params)

    # gpu_tracker = MemTracker()  # define a GPU tracker
    # gpu_tracker.track()
    # print(model)
    # exit()
    model.to(device)

    # gpu_tracker.track()

    # print("len(model.convs):", len(model.convs))

    opt = optim.Adam(model.parameters(), lr=params['learning_rate'], weight_decay=5e-4)
    # opt = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9)
    min_valid_loss = np.inf
    best_f1_score = -1
    best_model_path = ""

    train_accuracies, vali_accuracies = list(), list()

    # train
    for epoch in range(params['epoch']):
        logger.info("=== now epoch: %d" % epoch)
        total_loss = 0
        model.train()
        for batch in loader:
            # print(batch.train_mask, '----')
            opt.zero_grad() # 清空梯度
            pred = model(batch)

            # pred = pred.argmax(dim=1, keepdim=True)
            # logger.info("pred: {}".format(pred))
            label = batch.y.to(device)

            # print("pred.shape : ",pred.shape)
            # print("label.shape: ",label.shape)

            loss = model.loss(pred, label)

            # gpu_tracker.track()

            loss.backward() # 反向计算梯度，累加到之前梯度上
            opt.step() # 更新参数
            total_loss += loss.item() * batch.num_graphs

            # gpu_tracker.track()

            # delete caches
            del pred, loss
            # torch.cuda.empty_cache()

            # gpu_tracker.track()

        total_loss /= len(loader.dataset)
        writer.add_scalar("loss", total_loss, epoch)

        # validate
        train_score = test(loader, model)
        vali_score = test(vali_loader, model)

        train_accuracies.append(train_score['accuracy'])
        vali_accuracies.append(vali_score['accuracy'])

        if print_grad:
            for i, para in enumerate(model.parameters()):
                print(f'{i + 1}th parameter tensor:', para.shape)
                print(para)
                print("grad:")
                print(para.grad)

        logger.info("Epoch: {}, loss: {:.6f}".format(epoch, total_loss))
        if total_loss < min_valid_loss:
            # Saving State Dict
            # torch.save(model.state_dict(), MODEL_SAVE_PATH + "/gcn_model_epoch%d.pth" % epoch)
            # best_model_path = MODEL_SAVE_PATH + "/gcn_model_epoch%d.pth" % epoch

            print("Training Loss Decreased: {:.6f} --> {:.6f}.".format(min_valid_loss, total_loss))
            logger.info("Training Loss Decreased: {:.6f} --> {:.6f}.".format(min_valid_loss, total_loss))
            min_valid_loss = total_loss

        if vali_score['f1'] > best_f1_score:
            # Saving State Dict
            torch.save(model.state_dict(), params['model_save_path'] + "/gcn_model_epoch%d.pth" % epoch)
            best_model_path = params['model_save_path'] + "/gcn_model_epoch%d.pth" % epoch

            print("New best F1: {:.4f} --> {:.4f}. Saved model: {}".format(best_f1_score, vali_score['f1'], best_model_path))
            logger.info("New best F1: {:.4f} --> {:.4f}. Saved model: {}".format(best_f1_score, vali_score['f1'], best_model_path))
            best_f1_score = vali_score['f1']

        print_result("train", train_score, epoch)
        print_result("vali", vali_score, epoch)
        # exit()

        # report intermediate result
        # nni.report_intermediate_result(vali_score['accuracy'])

        # logger.info("w_pdt: {}, w_ref: {}, w_def: {}, w_lp: {}, w_ns: {}".format(model.w_pdt, model.w_ref, model.w_def, model.w_lp, model.w_ns))

        writer.add_scalar("test_accuracy", vali_score['accuracy'], epoch)

    # report final result
    # nni.report_final_result(vali_score['accuracy'])

    # Load
    # model = Net()
    if os.path.exists(best_model_path):
        myprint("loading the best model: %s" % best_model_path, 1)
        model.load_state_dict(torch.load(best_model_path))
        test_score = test(test_loader, model)
        print_result("test", test_score)

    if plot:
        plt.plot(train_accuracies, label="Train accuracy")
        plt.plot(vali_accuracies, label="Validation accuracy")
        plt.xlabel("# Epoch")
        plt.ylabel("Accuracy")
        plt.legend(loc='upper right')
        # plt.show()
        plt.savefig(params['model_save_path'] + "/gcn_model_accuracy.pdf")

    return model

def find_edges(func_key, relations_call, relations_callby, deleted, lv=0, call_type="all", nodes=[], edge_index=[], edge_weight=[]):
    if lv == 0:
        nodes = [func_key]
    func_index = nodes.index(func_key)

    if (lv < 2 and lv > -2) and func_key in relations_call.keys() and call_type in ['all', 'call']:
        weight = {}
        for sub_func in relations_call[func_key]:
            if sub_func in deleted:
                continue
            if not sub_func in weight.keys():
                weight[sub_func] = 1
            else:
                if weight[sub_func] < 1.5:
                    weight[sub_func] += 0.1
        for sub_func in weight.keys():
            if sub_func not in nodes:
                nodes.append(sub_func)
            sub_func_index = nodes.index(sub_func)
            edge_index.append( [func_index, sub_func_index] )
            edge_weight.append(weight[sub_func])
            nodes, edge_index, edge_weight = find_edges( sub_func, relations_call, relations_callby, deleted, lv+1, "call", nodes, edge_index, edge_weight)

    if (lv < 2 and lv > -2) and func_key in relations_callby.keys() and call_type in ['all', 'callby']:
        weight = {}
        for sub_func in relations_callby[func_key]:
            if sub_func in deleted:
                continue
            if not sub_func in weight.keys():
                weight[sub_func] = 1
            else:
                if weight[sub_func] < 1.5:
                    weight[sub_func] += 0.1
        for sub_func in weight.keys():
            if sub_func not in nodes:
                nodes.append(sub_func)
            sub_func_index = nodes.index(sub_func)
            edge_index.append( [func_index, sub_func_index] )
            edge_weight.append(weight[sub_func])
            nodes, edge_index, edge_weight = find_edges( sub_func, relations_call, relations_callby, deleted, lv-1, "callby", nodes, edge_index, edge_weight)
    return nodes, edge_index, edge_weight

def read_relation_file(relation_file, deleted):
    res = {}

    # read relations
    relations_call = {}
    relations_callby = {}

    json_str = ""
    if not os.path.exists(relation_file):
        return res

    with open(relation_file) as f:
        json_str = f.read().strip()
    if json_str == "":
        return []
    obj = json.loads(json_str)

    lv0_functions = []
    for k, arr in obj.items():
        for v in arr:
            if v['type'] == 'call':
                if k in deleted or v['value'] in deleted:
                    continue
                if k in relations_call.keys():
                    relations_call[k].append(v['value'])
                else:
                    relations_call[k] = [v['value']]
            elif v['type'] == 'callby':
                if k in deleted or v['value'] in deleted:
                    continue
                if k in relations_callby.keys():
                    relations_callby[k].append(v['value'])
                else:
                    relations_callby[k] = [v['value']]
            elif v['type'] == 'define':
                if v['value'] not in lv0_functions and v['value'] not in deleted:
                    lv0_functions.append(v['value'])
    print("len(lv0_functions):", len(lv0_functions))

    for func in lv0_functions:
        nodes = []
        edge_index = []
        edge_weight = []

        nodes, edge_index, edge_weight = find_edges(func, relations_call, relations_callby, deleted, 0, "all", [], [], [])
        res[func] = {
            "nodes": nodes,
            "edge_index": edge_index,
            "edge_weight": edge_weight
        }
    return res

def read_jh_relation_file(entities_file, relation_file, deleted):
    res = {}

    # read relations
    relations_call = {}
    relations_callby = {}

    json_str = ""
    if not os.path.exists(relation_file):
        return res

    lv0_functions = []
    with open(entities_file) as f:
        for line in f.readlines():
            l = line.strip()
            if l=="":
                continue
            obj = json.loads(l)
            if obj['level'] == 0 and obj['vul'] == 1:
                lv0_functions.append(obj['func_key'])

    with open(relation_file) as f:
        for line in f.readlines():
            l = line.strip()
            if l == "":
                continue
            obj = json.loads(l)
            for k, arr in obj.items():
                for v in arr:
                    if v['type'] == 'call':
                        if k in relations_call.keys():
                            relations_call[k].append(v['value'])
                        else:
                            relations_call[k] = [v['value']]
                    elif v['type'] == 'callby':
                        if k in relations_callby.keys():
                            relations_callby[k].append(v['value'])
                        else:
                            relations_callby[k] = [v['value']]

    print("len(lv0_functions):", len(lv0_functions))
    logger.info("len(lv0_functions): {}".format(len(lv0_functions)))

    for func in lv0_functions:
        nodes = []
        edge_index = []
        edge_weight = []

        nodes, edge_index, edge_weight = find_edges(func, relations_call, relations_callby, deleted, 0, "all", [], [], [])
        res[func] = {
            "nodes": nodes,
            "edge_index": edge_index,
            "edge_weight": edge_weight
        }
    return res

def get_params():
    # args
    parser = argparse.ArgumentParser(description='Test for argparse')
    parser.add_argument('--tasks_file', help='tasks_file', type=str, default='/data/function2vec3/tasks.json')
    parser.add_argument('--functions_path', help='functions_path', type=str,
                        default=BASE_DIR + "/data/function2vec3/functions_jy")
    parser.add_argument('--embedding_path', help='embedding_path', type=str, default='/data/function2vec4')
    parser.add_argument('--model_save_path', help='model_save_path', type=str, default='/data/automl_models')
    parser.add_argument('--best_model_path', help='best_model_path', type=str, default="")

    parser.add_argument('--model_type', help='model_type', type=str, default="GNNStack")
    parser.add_argument('--input_dim', help='input_dim', type=int, default=128)
    parser.add_argument('--output_dim', help='output_dim', type=int, default=2)
    parser.add_argument('--hidden_dim', help='hidden_dim', type=int, default=64)
    parser.add_argument('--batch_size', help='hidden_dim', type=int, default=16)
    parser.add_argument('--learning_rate', help='learning_rate', type=float, default=0.001)
    parser.add_argument('--epoch', help='epoch', type=int, default=100)

    parser.add_argument('--lp_path_num', help='lp_path_num', type=int, default=20)
    parser.add_argument('--lp_length', help='lp_length', type=int, default=60)
    parser.add_argument('--lp_dim', help='lp_dim', type=int, default=128)
    parser.add_argument('--lp_w2v_path', help='lp_w2v_path', type=str,
                        default=BASE_DIR + "/data/function2vec2/models/w2v_lp_combine.bin")

    parser.add_argument('--ns_length', help='ns_length', type=int, default=2000)
    parser.add_argument('--ns_dim', help='ns_dim', type=int, default=128)
    parser.add_argument('--ns_w2v_path', help='ns_w2v_path', type=str,
                        default=BASE_DIR + "/data/function2vec2/models/w2v_ns.bin")

    parser.add_argument('--log_msg', help='log_msg', type=str, default="Soft weights + CE-loss")

    args, _ = parser.parse_known_args()
    # print(args)
    return args

if __name__ == '__main__':
    try:

        params = get_params()

        # get parameters form tuner
        # tuner_params = nni.get_next_parameter()
        # logger.debug(tuner_params)
        #params = vars(merge_parameter(params, tuner_params))

        params = vars(params)
        # logger.info("params: {}".format(params))

        # train
        # /xye_data_nobackup/wenbo/dlvp/data/function2vec4/automl_dataset_1v1/

        # log file
        now_time = time.strftime("%Y-%m-%d_%H-%M", time.localtime())
        log_file = "{}/../logs/{}_{}.log".format(BASE_DIR, now_time, params['model_type'])

        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s %(levelname)s %(filename)s line: %(lineno)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S',
                            filename=log_file)
        logger = logging.getLogger(__name__)

        # Device
        print('# Using device:', device)
        logger.info("=== using device: %s" % str(device))

        dataset_savepath = params['embedding_path'] + "/automl_dataset_1v1"
        Path(dataset_savepath).mkdir(parents=True, exist_ok=True)
        Path(params['model_save_path']).mkdir(parents=True, exist_ok=True)

        dataset = MyLargeDataset(dataset_savepath, params)

        logger.info("=== Train ===")
        writer = SummaryWriter("./log/" + datetime.now().strftime("%Y%m%d-%H%M%S"))
        model = train(params, dataset, writer, plot=False, print_grad=False)

    except Exception as exception:
        logger.exception(exception)
        raise

    logger.info("training GCN done.")


from batea import NmapReportParser, CSVFileParser
from os.path import join, dirname

nmap_full_filename = join(dirname(__file__), "samples/single_full.xml")
nmap_base_filename = join(dirname(__file__), "samples/single_base.xml")
nmap_malformed_filename = join(dirname(__file__), "samples/single_with_nulls.xml")

csv_short_filename = join(dirname(__file__), 'samples/batea_simple_csv')
csv_long_filename = join(dirname(__file__), 'samples/batea_long_csv')
csv_null_filename = join(dirname(__file__), 'samples/batea_null_csv')

def test_nmap_parser_generates_list_of_hosts():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert len(hosts) == 2

def test_nmap_parser_extract_hosts_ip_add():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert hosts[0].ipv4.exploded == "192.168.1.1"
    assert hosts[1].ipv4.exploded == "192.168.1.2"

def test_nmap_parser_extract_hostname():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert hosts[0].hostname == "test1.organization.org"

def test_nmap_parser_extract_os_information():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert hosts[0].os_info['vendor'] == "Hackerbox"
    assert hosts[0].os_info['family'] == "Hackerbox Linux"
    assert hosts[0].os_info['type'] == "general purpose"
    assert hosts[0].os_info['name'] == 'Linux 3.16 - 4.6'
    assert hosts[0].os_info['accuracy'] == 100

def test_nmap_parser_extract_port_lists():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))
    ports = list(hosts[0].ports)

    assert ports[0].port == 80
    assert ports[0].protocol == "tcp"
    assert ports[0].state == "open"
    assert ports[0].service == 'http'

def test_nmap_parser_extract_cpe_when_available():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))
    ports = list(hosts[1].ports)

    assert ports[0].cpe == "cpe:/a:openbsd:openssh:7.3"
    assert ports[1].cpe is None

def test_nmap_parser_extract_software_when_available():
    parser = NmapReportParser()
    with open(nmap_full_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))
    ports = list(hosts[1].ports)

    assert ports[0].software == "OpenSSH"
    assert ports[0].version == "7.3"

def test_nmap_parser_extract_info_from_simple_report():
    parser = NmapReportParser()
    with open(nmap_base_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    host = hosts[0]
    ports = list(host.ports)

    assert len(ports) == 11
    assert ports[0].port == 22
    assert ports[0].software is None
    assert not host.os_info

def test_nmap_parser_doesnt_crash_if_port_number_is_null_or_zero():
    parser = NmapReportParser()
    with open(nmap_malformed_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert len(hosts) == 2
    assert len(hosts[0].ports) == 1
    assert len(hosts[1].ports) == 0

def test_csv_parser_generates_list_of_hosts():
    parser = CSVFileParser()
    with open(csv_short_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert len(hosts) == 2
    assert hosts[0].ipv4.exploded == '10.251.53.100'

def test_csv_parser_generates_list_of_ports_for_each_hosts():
    parser = CSVFileParser()
    with open(csv_long_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert len(hosts) == 3
    assert len(hosts[-1].ports) == 12
    assert hosts[-1].ports[0].port == 853
    assert hosts[-1].ports[0].service == 'unknown'

def test_csv_parser_doesnt_generates_list_of_ports_if_port_number_is_null_or_zero():
    parser = CSVFileParser()
    with open(csv_null_filename, 'r') as f:
        hosts = list(parser.load_hosts(f))

    assert len(hosts) == 2
    assert len(hosts[0].ports) == 1
    assert len(hosts[1].ports) == 0


# -*- utf-8 -*-
########################################
# PSF license aggrement for logmsg.py
# Developed by Ivan Rybko
# Logmsg
########################################

from singleton import Singleton
import logging

@Singleton
class Logmsg:
    def __init__(self, name):
        if isinstance(name, str):
            self.lname = name
            self.lpath = self.lname + ".log"

    def logmsg(self, message):
        # create file handler which logs even debug messages
        logger = logging.getLogger(self.lname)
        logger.setLevel(logging.DEBUG)
        # create file handler which logs even debug info messages
        loghandler = logging.FileHandler(self.lpath)
        loghandler.setLevel(logging.DEBUG)
        # add format
        frmt = logging.Formatter('{asctime} {name} {levelname:8s} {message}',style='{')
        loghandler.setFormatter(frmt)
        logger.addHandler(loghandler)
        # add message
        logger.debug(message)
        logging.info(message)

    def __call__(self, message):
        self.logmsg(message)

def uselog(*args):
    step1 = args[0]
    step2 = args[1]
    log = Logmsg(step1)
    log(step2)

logs = {
    "static":     Logmsg("staticserver"),
    "dynamic":    Logmsg("dynamicserver"),
    "httpserver": Logmsg("httpserver"),
    "smtpserver": Logmsg("smtpserver"),
    "wsgiserver": Logmsg("wsgiserver"),
    "wsgiapp":    Logmsg("wsgiapp"),
    "imapclient": Logmsg("imapclient"),
    "smtpclient": Logmsg("smtpclient"),
    "httpclient": Logmsg("httpclient"),
    "dbrequest":  Logmsg("dbrequest")
    }


# pykarta/geometry/projection.py
# Last modified: 3 February 2015

import math

# Convert latitude and longitude to tile coordinates. The whole part of the
# x and y coordinates indicates the tile number. The fractional part
# indicates the position within the tile.
def project_to_tilespace(lat, lon, zoom):
	lat_rad = math.radians(lat)
	n = 2.0 ** zoom
	xtile = (lon + 180.0) / 360.0 * n
	ytile = (1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n
	return (xtile, ytile)

# Converts tile coordinates back to latitude and longitude
def unproject_from_tilespace(xtile, ytile, zoom):
	n = 2.0 ** zoom
	lon = xtile / n * 360.0 - 180.0
	lat = math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))))
	return (lat, lon)

# Convert latitude and longitude to a pixel position within the
# coordinate space of a particular tile. Note that the pixel
# position itself could well be outside the tile.
def project_to_tilespace_pixel(lat, lon, zoom, xtile, ytile):
	xtile2, ytile2 = project_to_tilespace(lat, lon, zoom)
	return ((xtile2 - xtile) * 256.0, (ytile2 - ytile) * 256.0)

#=============================================================================
# Mean radius of earth in meters used in the Spherical Mercartor projection
# Spherical Mercartor in meters rather than in tiles
# http://wiki.openstreetmap.org/wiki/Mercator
#=============================================================================

radius_of_earth = 6378137.0

def project_point_mercartor(lat, lon):
	return (
		math.radians(lon) * radius_of_earth,
		math.log(math.tan(math.pi/4.0 + math.radians(lat) / 2.0)) * radius_of_earth
		)

def unproject_point_mercartor(x, y):
	return (
		math.degrees(2.0 * math.atan(math.exp(y / radius_of_earth)) - math.pi / 2.0),
		math.degrees(x / radius_of_earth)
		)

#=============================================================================
# Project to meters using sinusoidal projection.
# Use for area computations.
# See: http://stackoverflow.com/questions/4681737/how-to-calculate-the-area-of-a-polygon-on-the-earths-surface-using-python
#=============================================================================

def project_points_sinusoidal(points):
	lat_dist = math.pi * radius_of_earth / 180.0	# size of degree in meters
	return [(
		lon * lat_dist * math.cos(math.radians(lat)),
		lat * lat_dist
		) for lat, lon in points]


from typing import List, Union

from openai.types.chat import ChatCompletionMessageParam, ChatCompletionContentPartParam

from imported_code_prompts import (
    IMPORTED_CODE_BOOTSTRAP_SYSTEM_PROMPT,
    IMPORTED_CODE_IONIC_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_REACT_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_SVG_SYSTEM_PROMPT,
)
from screenshot_system_prompts import (
    BOOTSTRAP_SYSTEM_PROMPT,
    IONIC_TAILWIND_SYSTEM_PROMPT,
    REACT_TAILWIND_SYSTEM_PROMPT,
    TAILWIND_SYSTEM_PROMPT,
    SVG_SYSTEM_PROMPT,
)

USER_PROMPT = """
Generate code for a web page that looks exactly like this.
"""

SVG_USER_PROMPT = """
Generate code for a SVG that looks exactly like this.
"""

def assemble_imported_code_prompt(
    code: str, stack: str, result_image_data_url: Union[str, None] = None
) -> List[ChatCompletionMessageParam]:
    system_content = IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT
    if stack == "html_tailwind":
        system_content = IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT
    elif stack == "react_tailwind":
        system_content = IMPORTED_CODE_REACT_TAILWIND_SYSTEM_PROMPT
    elif stack == "bootstrap":
        system_content = IMPORTED_CODE_BOOTSTRAP_SYSTEM_PROMPT
    elif stack == "ionic_tailwind":
        system_content = IMPORTED_CODE_IONIC_TAILWIND_SYSTEM_PROMPT
    elif stack == "svg":
        system_content = IMPORTED_CODE_SVG_SYSTEM_PROMPT
    else:
        raise Exception("Code config is not one of available options")

    user_content = (
        "Here is the code of the app: " + code
        if stack != "svg"
        else "Here is the code of the SVG: " + code
    )
    return [
        {
            "role": "system",
            "content": system_content,
        },
        {
            "role": "user",
            "content": user_content,
        },
    ]
    # TODO: Use result_image_data_url

def assemble_prompt(
    image_data_url: str,
    generated_code_config: str,
    result_image_data_url: Union[str, None] = None,
) -> List[ChatCompletionMessageParam]:
    # Set the system prompt based on the output settings
    system_content = TAILWIND_SYSTEM_PROMPT
    if generated_code_config == "html_tailwind":
        system_content = TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "react_tailwind":
        system_content = REACT_TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "bootstrap":
        system_content = BOOTSTRAP_SYSTEM_PROMPT
    elif generated_code_config == "ionic_tailwind":
        system_content = IONIC_TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "svg":
        system_content = SVG_SYSTEM_PROMPT
    else:
        raise Exception("Code config is not one of available options")

    user_prompt = USER_PROMPT if generated_code_config != "svg" else SVG_USER_PROMPT

    user_content: List[ChatCompletionContentPartParam] = [
        {
            "type": "image_url",
            "image_url": {"url": image_data_url, "detail": "high"},
        },
        {
            "type": "text",
            "text": user_prompt,
        },
    ]

    # Include the result image if it exists
    if result_image_data_url:
        user_content.insert(
            1,
            {
                "type": "image_url",
                "image_url": {"url": result_image_data_url, "detail": "high"},
            },
        )
    return [
        {
            "role": "system",
            "content": system_content,
        },
        {
            "role": "user",
            "content": user_content,
        },
    ]


#!/usr/bin/env python

'''
./converter.py \
	--model model.h5 \
	--desc "Predicts either a phone is in the hands or in a pocket" \
	--input_desc "Sensor samples (acc, gyro, mag, 50Hz)" \
	--output_desc "1 - phone in the hands, 0 - phone in a pocket" \
	--author "Danylo Kostyshyn" \
	--license="MIT"
'''

from __future__ import absolute_import
import os
import argparse
import coremltools
import numpy as np
from keras.models import load_model

parser = argparse.ArgumentParser(description='Converts Keras .h5 model to CoreML .mlmodel')
parser.add_argument('--model', dest='model_name', help='Input model name')
parser.add_argument('--desc', dest='desc', help='Short description')
parser.add_argument('--input_desc', dest='input_desc', help='Input description')
parser.add_argument('--output_desc', dest='output_desc', help='Oouput description')
parser.add_argument('--author', dest='author', help='Author')
parser.add_argument('--license', dest='license', help='License')

args = parser.parse_args()

def main():
	model_name = args.model_name
	keras_model = load_model(model_name)

	coreml_model = coremltools.converters.keras.convert(keras_model, \
		input_names='input', output_names='output')

	coreml_model.input_description['input'] = args.input_desc
	coreml_model.output_description['output'] = args.output_desc

	coreml_model.short_description = args.desc
	coreml_model.author = args.author
	coreml_model.license = args.license

	f_name, f_ext = os.path.splitext(model_name)
	coreml_model.save(os.path.join(f_name + '.mlmodel'))

	print("Success!")

if __name__ == '__main__':
	main()


import os,sys,re
import slide_window
import vcf_operating
import read_group
import read_dict
from multiprocessing import Pool
#https://github.com/mfumagalli/ngsPopGen/blob/9ee3a6d5e733c1e248e81bfc21514b0527da967b/scripts/getDxy.pl
def help():
    print("python3 xx.py vcf two_group_list fasta_fai outfile window step")
    print("two_group_list: classA\tHH1\nclassB\tHH2\nclassB\tHH3")
    sys.exit()
def maf_1_0(list_):
    count_0=sum([x.count('0') for x in list_])
    count_1=sum([x.count('1') for x in list_])
    all_count=count_0+count_1
    if all_count==0:
        return [0,0]
    maf_1=count_1/all_count
    maf_0=count_0/all_count
    return [maf_0,maf_1]

def each_maf(line):
    types1=[line[x][:3] for x in loci1]
    types2=[line[x][:3] for x in loci2]
    all_maf=maf_1_0(types1)+maf_1_0(types2)
    return all_maf

def each_dxy(line_):
    line=line_.split()
    chr_,pos=line[0],int(line[1])
    all_maf=each_maf(line)
    dxy=all_maf[0]*all_maf[3]+all_maf[1]*all_maf[2]
    return [chr_,pos,dxy]

def every_dxy(vcfs):
    head=next(vcfs).split()
    global loci1,loci2
    loci1=[head.index(x) for x in group_dict[group1]]
    loci2=[head.index(x) for x in group_dict[group2]]
    po1=Pool(theads)
    all_dxy=po1.map(each_dxy,vcfs)
    po1.close()
    dxy_dict={}
    for list_ in all_dxy:
        if list_[2]==0:
            continue
        dxy_dict.setdefault(list_[0],{}).setdefault(list_[1],list_[2])
    return dxy_dict

def window_dxy(cal_window):
    chr_=cal_window[0]
    if chr_ not in dxy_snp_pos_dict:
        return
    chr_snp_pos=dxy_snp_pos_dict[chr_]
    N_SNP,ALL_DXY=0,0
    for pos in chr_snp_pos:
        if pos<cal_window[1]:
            continue
        elif cal_window[1]<=pos<=cal_window[2]:
            N_SNP+=1
            ALL_DXY+=dxy_dict[chr_][pos]
        elif pos >cal_window[2]:
            break
    if ALL_DXY==0 or N_SNP==0:
        return
    ALL_DXY=ALL_DXY/(cal_window[2]-cal_window[1]+1)
    return [cal_window[0],str(cal_window[1]),str(cal_window[2]),str(N_SNP),str(ALL_DXY)]

def write_result(all_window_dxy,out_file):
    all_window_dxy.sort(key=lambda x:(x[0],int(x[1])))
    with open(out_file,'w') as fp:
        print('CHROM\tSTART\tEND\tN_SNP\tDXY',file=fp)
        for win in all_window_dxy:
            print('\t'.join(win),file=fp)

def main(vcf_file,group_file,fasta_fai,out_file,window,step):
    global group_dict,group1,group2
    group_dict=read_group.Read_group1(group_file)
    [group1,group2]=list(group_dict.keys())
    vcfs=For_vcf.vcf_read2(vcf_file)
    global dxy_dict,dxy_snp_pos_dict
    dxy_dict=every_dxy(vcfs)
    dxy_snp_pos_dict={}
    for chr_ in dxy_dict:
        dxy_snp_pos_dict[chr_]=sorted(dxy_dict[chr_].keys())
    chr_len_dict=read_dict.read_fai(fasta_fai)
    all_windows=[]
    #[[chr,start,end]]
    for chr_ in chr_len_dict:
        all_windows+=slide_window.slide_window2(chr_,window,step,1,chr_len_dict[chr_])
    po2=Pool(theads)
    all_window_dxy=po2.map(window_dxy,all_windows)
    po2.close()
    #all_window_dxy=list(filter(None,all_window_dxy))
    all_window_dxy=[x for x in all_window_dxy if x !=None]
    write_result(all_window_dxy,out_file)

if __name__=="__main__":
    try:
        [vcf_file,group_file,fasta_fai,out_file]=sys.argv[1:5]
        window=int(sys.argv[5])
        step=int(sys.argv[6])
    except:
        help()
    theads=30
    For_vcf=vcf_operating.for_vcf()
    main(vcf_file,group_file,fasta_fai,out_file,window,step)


# 백준 1987 알파벳

def sol():
    global res

    q = [(0, 0)]
    v = [[0]*C for _ in range(R)]
    used = [board[0][0]]
    v[0][0] = 1
    cnt = 1
    while q:
        r, c = q.pop(0)
        for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            nr, nc = r + dr, c + dc
            if 0 <= nr < R and 0 <= nc < C and not v[nr][nc]:
                if board[nr][nc] not in used:
                    used.append(board[nr][nc])
                    cnt += 1
                    q.append((nr, nc))
                    v[nr][nc] = 1

    if res < cnt:
        res = cnt

R, C = map(int, input().split())
board = [list(input()) for _ in range(R)]

res = 0
sol()
print(res)

'''
문제 접근 방식 : bfs

어려웠던점 : 케이스가 많아질 경우 답이 틀려짐

설명이 필요한점 : fail
'''


class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()

    # initialize the result list
        result = []

        # iterate through the array
        for i in range(len(nums)):
            # skip duplicates
            if i > 0 and nums[i] == nums[i-1]:
                continue

            # set the target sum
            target = -nums[i]

            left = i + 1
            right = len(nums) - 1

            # use binary search to find the other two elements
            while left < right:
                # if the sum is equal to the target, add the triplet to the result list
                x = nums[left] + nums[right]
                if x == target:
                    result.append([nums[i], nums[left], nums[right]])
                    # skip duplicates
                    while left < right and nums[left] == nums[left+1]:
                        left += 1
                    while left < right and nums[right] == nums[right-1]:
                        right -= 1
                    # move the pointers
                    left += 1
                    right -= 1
                # if the sum is less than the target, increment the left pointer
                elif x < target:
                    left += 1
                # if the sum is greater than the target, decrement the right pointer
                else:
                    right -= 1

        return result