import sys
from collections import namedtuple
from dataclasses import dataclass
from typing import Dict, List, Tuple

SNAPSHOT_INTERVAL = 40
SNAPSHOT_CEIL = 220

Waveform = namedtuple("Waveform", "x cycle")

@dataclass
class Command:
    duration: int
    action: callable

    def execute(self, waveform: Waveform, parameters: Dict) -> Waveform:
        x = self.action(waveform.x, parameters)
        cycle = waveform.cycle + self.duration
        return Waveform(x, cycle)

commands = {
    "noop": Command(1, lambda x, _: x),
    "addx": Command(2, lambda x, p: x + int(p[0])),
}

def parse_command_line(line: str) -> Tuple[Command, Dict]:
    args = line.rstrip().split()
    return commands[args[0]], args[1:]

def update_snapshots(snapshots: List[int], snapshot_cycle: int, previous: Waveform, current: Waveform) -> int:
    wave = None
    if current.cycle == snapshot_cycle:
        wave = current
    elif current.cycle > snapshot_cycle > previous.cycle:
        wave = previous

    if wave is None:
        return snapshot_cycle

    snapshots.append(wave.x * snapshot_cycle)
    next_snapshot_cycle = snapshot_cycle + SNAPSHOT_INTERVAL
    if next_snapshot_cycle > SNAPSHOT_CEIL:
        raise OverflowError
    return next_snapshot_cycle

def get_signal_strengths_sum(filepath: str) -> int:
    current_wave = Waveform(1, 1)
    next_snapshot_cycle = 20
    snapshots = []
    with open(filepath) as f:
        for line in f.readlines():
            command, parameters = parse_command_line(line)
            new_wave = command.execute(current_wave, parameters)
            try:
                next_snapshot_cycle = update_snapshots(snapshots, next_snapshot_cycle, current_wave, new_wave)
            except OverflowError:
                break
            current_wave = new_wave
    return sum(snapshots)

if __name__ == "__main__":
    filepath = sys.argv[1]
    total = get_signal_strengths_sum(filepath)
    print(f"The sum of these signal strengths is {total}.")


from django.urls import path
from . import views

urlpatterns = [
    path("sanity_check/", views.SanityCheck.as_view(),name="sanity_check"),
    path("latest-news/", views.LatestNewsListView.as_view() ,name="latest-news"),
    path("headline/", views.HackersNewsHeadlineNewsView.as_view(),name="headline"),
    path("headline/hf", views.HackersFeedHeadlineNewsView.as_view(),name="hackersfeed"),
    path("<int:pk>", views.DetailNewsView.as_view(),name="news-details"),
    path("create/", views.NewsItemCreateView.as_view(),name="post-news"),
    path("<int:news_item_id>/comments/", views.CommentCreateView.as_view(),name="comment"),
    path('search/', views.NewsItemSearchView.as_view(),name="search-news"),
]


import requests
import json
import time

# BASIC FUNCTIONS
# ---------------------------------------------------

# Creates access token necessary for OAuth with givenid and password
def create_access_token(client_id, client_secret, region = 'eu'):
    data = { 'grant_type': 'client_credentials' }
    response = requests.post('https://%s.battle.net/oauth/token' % region, data=data, auth=(client_id, client_secret))
    return response.json()['access_token']

# Returns a list of connected realms
def connected_realms_list(token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/search/connected-realm?namespace=dynamic-eu&locale=en_GB&orderby=id&access_token='+token)
    results = response.json()['results']
    realms_nr = nr_conn_realms(token)
    data = list()
    i = 0
    while i<realms_nr:
        data.append(results[i]['data'])
        i += 1
    return data

# Returns the number of connected realms
def nr_conn_realms(token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/index?namespace=dynamic-eu&locale=en_GB&access_token='+token)
    return len(response.json()['connected_realms'])

# FUNCTIONS FOR DISPLAYING CHARACTER IMAGE
# -------------------------------------------

# Returns the image of a character
def get_character_img(charName, server, token):
    response = requests.get('https://eu.api.blizzard.com/profile/wow/character/'+server+'/'+charName.lower()+'/character-media?namespace=profile-eu&locale=en_GB&access_token='+token)
    img_link = ""

    try:
        if response.json().get('assets'):
            img_link = response.json()['assets'][2]['value']
        else:
            img_link = response.json()['render_url']
    except KeyError:
        img_link = ""
    return img_link

# Returns a list with all realm names
def get_realm_names(token):
    realm_list = connected_realms_list(token)
    realm_names = list()
    i=0
    while i<len(realm_list):
        realms = realm_list[i]['realms']
        j=0
        while j<len(realms):
            realm_names.append(realm_list[i]['realms'][j]['slug'])
            j += 1
        i+=1
    return realm_names

# FUNCTIONS FOR AUCTION HOUSE DISPLAY
# ---------------------------------------------------

# Returns all the auction listings for a connected realm
def get_auction_listing(realm_id, token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/'+realm_id+'/auctions?namespace=dynamic-eu&locale=en_GB&access_token='+token)
    return response.json()

# Returns the cheapest listing for an item in a connected realm
def get_min_auction_listing(item_id, realm_id, token):
    with open(str(realm_id)) as f:
        json_file = json.load(f)
    auctions = json_file['auctions']
    i=0
    min_price = 10000000
    while i<len(auctions):
        curr_item_id = auctions[i]['item']['id']
        if curr_item_id == item_id:
            item_price = auctions[i]['unit_price']
            if item_price < min_price:
                min_price = item_price
        i+=1
    print(min_price)
    return min_price

# Returns a list of realms names included in the given connected realm ids
def get_realm_names_conn(conn_ids, token):
    realm_names = list()
    i=0
    while i<len(conn_ids):
        response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/'+str(conn_ids[i])+'?namespace=dynamic-eu&access_token='+token)
        results = response.json()['realms']
        j=0
        while j<len(results):
           realm_names.append(results[j]['name']['en_US'])
           j+=1
        i+=1
    return realm_names

# Returns a dictionary containing the cheapest listing for an items along with the realms
def min_listing(item_id, token):
    start = time.time()
    i = 0
    id_list = get_conn_realm_id_list(token)
    min_price = 10000000
    chosen_realms = list()
    while i < len(id_list):
        curr_realm_price = get_min_auction_listing(item_id, id_list[i], token)
        if curr_realm_price < min_price:
            min_price = curr_realm_price
            chosen_realms.clear()
            chosen_realms.append(id_list[i])
        elif curr_realm_price == min_price:
            chosen_realms.append(id_list[i])
        i += 1
    realms = get_realm_names_conn(chosen_realms, token)
    results = {
        "realms": realms,
        "price": min_price
        }
    end = time.time()
    print(end - start)
    return results

# Returns a dictionary with all suggested items and their id based on user input
def get_item_list(item, token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/search/item?namespace=static-eu&locale=en_GB&name.en_GB='+item+'&orderby=id&_page=1&access_token='+token)
    results = response.json()['results']
    i = 0
    item_list = {}
    while i < response.json()['pageSize']:
        if item.lower() in results[i]['data']['name']['en_GB'].lower():
            item_list[results[i]['data']['name']['en_GB']] = results[i]['data']['id']
        i += 1
    return item_list

# Returns a list that contains the ids of all connected realms
def get_conn_realm_id_list(token):
    realm_list = connected_realms_list(token)
    id_list = list()
    i = 0
    while i<len(realm_list):
        id_list.append(realm_list[i]['id'])
        i += 1
    return id_list

# Returns the icon of an item
def get_item_icon(item_id, token):
    #response = requests.get('https://eu.api.blizzard.com/data/wow/media/item/'+str(item_id)+'?namespace=static-9.0.5_37760-eu&access_token='+token)
    print('response='+response)
    results = response.json()['assets'][0]['value']
    print(results)
    return results


from django.urls import path
from .views import myproduct_page, myproduct_detail,product_list,addtocart,showcart, getProducts, search, getdata
urlpatterns = [
    path('info', myproduct_page),
    path('detail/<str:pid>', myproduct_detail),
    path('products', product_list),
    path('addtocart', addtocart),
    path('showcart', showcart),
    path('getall/<str:key>', getProducts),
    path('productsearch', search),
    path('data/',getdata)
]
#here we given <str:id> with detail path which means it expecting a string value when we gave an id there
#example product/detail/1 will result the corresponding details of product of id 1


import unittest
import datetime

from vlib import conf
from vlib import db

class TestError(Exception): pass

class TestDb(unittest.TestCase):
    '''Test Db'''

    def setUp(self):
        self.db = db.getInstance()

    def test__repr__(self):
        A='vlib.db.Db:vlibtests'
        B=str(self.db)
        self.assertEqual(A, B)

    def test_query(self):
        sql = 'select book_name from books where book_id = 1'
        results = self.db.query(sql)
        self.assertEqual(results[0]['book_name'], 'baranoff')

    def test_rowcount(self):
        sql = "update books set subtitle = 'test' where book_id <= 2"
        results = self.db.query(sql)
        rowcount = self.db.rowcount
        self.assertEqual(rowcount, 2)
        sql = "update books set subtitle = null where book_id <= 2"
        results = self.db.query(sql)

    def test_timezone(self):
        sql = 'select @@session.time_zone as time_zone'
        db_timezone = self.db.query(sql)[0]['time_zone']
        self.assertEqual(conf.getInstance().database.timezone, db_timezone)

    def test_timezone_same_as_localhost(self):
        sql = 'SELECT date_format(now(), "%Y-%m-%d %H:%i") as now;'
        db_datetime = self.db.query(sql)[0]['now']
        localhost_datetime = \
            datetime.datetime.now().strftime("%Y-%m-%d %H:%M").lower()
        self.assertEqual(localhost_datetime, db_datetime)

    def test_rollback(self):
        max_book_id = self._getMaxBookId()
        self.db.startTransaction()
        try:
            sql = 'insert into books (book_name) value ("Unit Test Book")'
            self.db.execute(sql)
            raise TestError('Test Error')
        except TestError as e:
            self.db.rollback()
        except Exception as e:
            raise
        max_book_id2 = self._getMaxBookId()
        self.assertEqual(max_book_id, max_book_id2)
        self._resetBooksAutoInc()

    def test_commit(self):
        max_book_id = self._getMaxBookId()
        self.db.startTransaction()
        try:
            sql = 'insert into books (book_name) value ("Unit Test Book")'
            self.db.execute(sql)
            self.db.commit()
        except Exception as e:
            self.db.rollback()
        max_book_id2 = self._getMaxBookId()
        self.assertEqual(max_book_id+1, max_book_id2)
        self._removeBook(max_book_id2)
        self._resetBooksAutoInc()

    def _getMaxBookId(self):
        sql = 'select max(book_id) as max_book_id from books'
        return self.db.query(sql)[0]['max_book_id']

    def _removeBook(self, book_id):
        sql = 'delete from books where book_id = %s' % book_id
        self.db.execute(sql)

    def _resetBooksAutoInc(self):
        sql = 'alter table books auto_increment = 1'
        self.db.execute(sql)


import socket
import sys
import json
import time
import threading
from time import sleep

def alive_check():
    data_alive = '{"id": 8000}'
    send_alive = json.loads(data_alive)

    while True:
        # Send data
        print >>sys.stderr, 'sending "%s"\n' % send_alive["id"]
        sock.sendall(str(send_alive["id"]))
        return_data = sock.recv(16)
        if  return_data == "8100":
            print >>sys.stderr, 'received "%s"\n' % return_data
        time.sleep(15)

def handshake():
    data_handshake = '{"id": 8001}'
    send_handshake = json.loads(data_handshake)

    while True:
        # Send data
        print >>sys.stderr, 'sending "%s"\n' % send_handshake["id"]
        sock.sendall(str(send_handshake["id"]))
        return_data = sock.recv(16)
        if "8101" in return_data:
            print >>sys.stderr, 'received "%s"\n' % return_data
        time.sleep(20)

if __name__ =="__main__":
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = ('localhost', 10000)
    print >>sys.stderr, 'connecting to %s port %s\n' % server_address
    sock.connect(server_address)
    print(server_address)
    # creating thread
    t1 = threading.Thread(target=alive_check)
    t1.daemon = True
    # starting thread 1
    t1.start()

    time.sleep(5)

    t2 = threading.Thread(target=handshake)
    t2.daemon = True
    # starting thread 2
    t2.start()

    # wait until thread 1 is completely executed
    t1.join()
    # wait until thread 2 is completely executed
    t2.join()


# -*- coding: utf-8 -*-
"""Tests for list command."""
from datetime import datetime, timedelta
from unittest.mock import patch

import habito
import habito.commands
from tests.commands import HabitoCommandTestCase

class HabitoListCommandTestCase(HabitoCommandTestCase):
    def test_habito_list_lists_off_track_habits(self):
        habit = self.create_habit()
        self.add_summary(habit)
        self._run_command(habito.commands.checkin, ["Habit", "-q -9.1"])

        result = self._run_command(habito.commands.list, ["-l"])

        # Habit is off track with quanta <= goal. Verify 'x'
        assert habit.name in result.output
        assert u"-9.1" in result.output

    def test_habito_list_lists_on_track_habits(self):
        habit = self.create_habit()
        self.add_summary(habit)
        self._run_command(habito.commands.checkin, ["Habit", "-q 9.1"])

        result = self._run_command(habito.commands.list, ["-l"])

        # Habit is on track with quanta >= goal. Verify 'tick'
        assert habit.name in result.output
        assert u"9.1" in result.output

    def test_habito_list_skips_inactive_habits(self):
        habit = self.create_habit()
        self.add_summary(habit)
        self._run_command(habito.commands.checkin, ["Habit", "-q 9.1"])
        habit.active = False
        habit.save()

        result = self._run_command(habito.commands.list, ["-l"])

        # Habit is on track with quanta >= goal. Verify 'tick'
        assert habit.name not in result.output
        assert u"9.1" not in result.output

    def test_habito_list_should_show_streak(self):
        habit = self.create_habit()
        self.add_summary(habit, streak=10)

        result = self._run_command(habito.commands.list)

        assert "10 days" in result.output

    @patch("shutil.get_terminal_size")
    def test_habito_list_table_adapts_to_terminal_width(self, term_mock):
        for terminal_width in range(0, 101, 5):
            term_mock.return_value = (terminal_width, 80)
            nr_of_dates = terminal_width//10 - 4
            result = self._run_command(habito.commands.list, ["-l"])
            if nr_of_dates < 1:
                assert "terminal window is too small" in result.output
                assert result.exit_code == 1
            else:
                assert result.exit_code == 0
                for i in range(nr_of_dates):
                    date_string = "{dt.month}/{dt.day}".format(dt=(datetime.now() - timedelta(days=i)))
                    assert date_string in result.output


#!/usr/bin/python3

from Crypto.Util.Padding import pad
from Crypto.Cipher import AES
from sys import stdout

import base64
import os

class Unbuffered(object):
	def __init__(self, stream):
		self.stream = stream
	def write(self, data):
		self.stream.write(data)
		self.stream.flush()
	def writelines(self, datas):
		self.stream.writelines(datas)
		self.stream.flush()
	def __getattr__(self, attr):
		return getattr(self.stream, attr)

stdout = Unbuffered(stdout)
flag = os.environ.get('SECRET_FLAG')
iv = bytes(os.environ.get('IV'), 'utf-8')
key = os.urandom(16)

def prompt():
    print("""\n
1. Read file
2. Check flag
    """)

    try:
        choice = int(input('Option: '))

        if choice == 1:
            args = input('Path: ')
            read_file(args)
        elif choice == 2:
            args = input('Flag: ')
            check_flag(args)

    except Exception:
        pass

def encrypt(text):
    aes = AES.new(key, AES.MODE_OFB, iv)
    enc = aes.encrypt(pad(text, 16))

    return base64.b64encode(enc)

def read_file(filename):
    if os.path.isfile(filename):
        with open(filename, 'rb') as handle:
            content = handle.read()

        encrypted = encrypt(content)
        stdout.write(encrypted.decode())

def check_flag(text):
    if text == flag:
        stdout.write('Correct')
    else:
        stdout.write('Incorrect')

if __name__ == '__main__':
    while True:
        prompt()


import torch as torch
import os

import torch.utils.data as data
import numpy as np
from tqdm import tqdm
import argparse

from models.RCA import AE
from data_process import RealDataset

class Solver_RCA:
    def __init__(
        self,
        data_name,
        hidden_dim=128,  # number of hidden neurons in RCA
        z_dim=10,  # bottleneck dimension
        seed=0,  # random seed
        learning_rate=1e-3,  # learning rate
        batch_size=128,  #  batchsize
        training_ratio=0.8,  #  training data percentage
        max_epochs=100,  #  training epochs
        coteaching=1.0,  #  whether selects sample based on loss value
        oe=0.0,  # how much we overestimate the ground-truth anomaly ratio
        missing_ratio=0.0,  # missing ratio in the data
    ):
        # Data loader
        # read data here
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)

        use_cuda = torch.cuda.is_available()
        self.data_name = data_name
        self.device = torch.device("cuda" if use_cuda else "cpu")
        data_path = "./data/" + data_name + ".npy"
        self.missing_ratio = missing_ratio
        self.model_save_path = "./trained_model/{}/{}/RCA/{}/".format(
            data_name, missing_ratio, seed
        )
        if oe == 0.0:
            self.result_path = "./results/{}/{}/RCA/{}/".format(
                data_name, missing_ratio, seed
            )
        else:
            self.result_path = "./results/{}/{}/RCA_{}/{}/".format(
                data_name, missing_ratio, oe, seed
            )

        os.makedirs(self.model_save_path, exist_ok=True)
        self.learning_rate = learning_rate
        self.dataset = RealDataset(
            data_path, missing_ratio=self.missing_ratio
        )
        self.seed = seed
        self.hidden_dim = hidden_dim
        self.z_dim = z_dim
        self.max_epochs = max_epochs
        self.coteaching = coteaching
        self.beta = 0.0  # initially, select all data
        self.alpha = 0.5
        self.data_path = data_path

        self.data_anomaly_ratio = self.dataset.__anomalyratio__() + oe

        self.input_dim = self.dataset.__dim__()
        self.data_normaly_ratio = 1 - self.data_anomaly_ratio

        n_sample = self.dataset.__len__()
        self.n_train = int(n_sample * (training_ratio))
        self.n_test = n_sample - self.n_train
        print(
            "|data dimension: {}|data noise ratio:{}".format(
                self.dataset.__dim__(), self.data_anomaly_ratio
            )
        )

        self.decay_ratio = abs(self.beta - (1 - self.data_anomaly_ratio)) / (
            self.max_epochs / 2
        )
        training_data, testing_data = data.random_split(
            dataset=self.dataset, lengths=[self.n_train, self.n_test]
        )

        self.training_loader = data.DataLoader(
            training_data, batch_size=batch_size, shuffle=True
        )

        self.testing_loader = data.DataLoader(
            testing_data, batch_size=self.n_test, shuffle=False
        )
        self.ae = None
        self.discriminator = None
        self.build_model()
        self.print_network()

    def build_model(self):
        self.ae = AE(
            input_dim=self.input_dim, hidden_dim=self.hidden_dim, z_dim=self.z_dim
        )
        self.ae = self.ae.to(self.device)

    def print_network(self):
        num_params = 0
        for p in self.ae.parameters():
            num_params += p.numel()
        print("The number of parameters: {}".format(num_params))

    def train(self):
        optimizer = torch.optim.Adam(self.ae.parameters(), lr=self.learning_rate)
        self.ae.eval()
        loss_mse = torch.nn.MSELoss(reduction='none')
        if self.data_name == 'optdigits':
            loss_mse = torch.nn.BCELoss(reduction='none')

        for epoch in tqdm(range(self.max_epochs)):  # train 3 time classifier
            for i, (x, y) in enumerate(self.training_loader):
                x = x.to(self.device).float()
                n = x.shape[0]
                n_selected = int(n * (1-self.beta))

                if config.coteaching == 0.0:
                    n_selected = n
                if i == 0:
                    current_ratio = "{}/{}".format(n_selected, n)

                optimizer.zero_grad()

                with torch.no_grad():
                    self.ae.eval()
                    z1, z2, xhat1, xhat2 = self.ae(x.float(), x.float())

                    error1 = loss_mse(xhat1, x)
                    error1 = error1
                    error2 = loss_mse(xhat2, x)
                    error2 = error2

                    error1 = error1.sum(dim=1)
                    error2 = error2.sum(dim=1)
                    _, index1 = torch.sort(error1)
                    _, index2 = torch.sort(error2)

                    index1 = index1[:n_selected]
                    index2 = index2[:n_selected]

                    x1 = x[index2, :]
                    x2 = x[index1, :]

                self.ae.train()
                z1, z2, xhat1, xhat2 = self.ae(x1.float(), x2.float())
                loss = loss_mse(xhat1, x1) + loss_mse(xhat2, x2)
                loss = loss.sum()
                loss.backward()
                optimizer.step()

            if self.beta < self.data_anomaly_ratio:
                self.beta = min(
                    self.data_anomaly_ratio, self.beta + self.decay_ratio
                )

    def test(self):
        print("======================TEST MODE======================")
        self.ae.train()
        mse_loss = torch.nn.MSELoss(reduction='none')
        if self.data_name == 'optdigits':
            mse_loss = torch.nn.BCELoss(reduction='none')

        error_list = []
        for _ in range(1000):  # ensemble score over 100 stochastic feedforward
            with torch.no_grad():
                for _, (x, y) in enumerate(self.testing_loader):  # testing data loader has n_test batchsize, if it is image data, need change this part
                    y = y.data.cpu().numpy()
                    x = x.to(self.device).float()
                    _, _, xhat1, xhat2 = self.ae(x.float(), x.float())
                    error = mse_loss(xhat1, x) + mse_loss(xhat2, x)
                    error = error.mean(dim=1)
                error = error.data.cpu().numpy()
                error_list.append(error)
        error_list = np.array(error_list)
        error = error_list.mean(axis=0)
        from sklearn.metrics import (
            precision_recall_fscore_support as prf,
            accuracy_score,
            roc_auc_score,
        )
        gt = y.astype(int)

        thresh = np.percentile(error, self.dataset.__anomalyratio__() * 100)
        print("Threshold :", thresh)

        pred = (error > thresh).astype(int)
        gt = y.astype(int)
        auc = roc_auc_score(gt, error)
        accuracy = accuracy_score(gt, pred)
        precision, recall, f_score, support = prf(gt, pred, average="binary")

        print(
            "Accuracy : {:0.4f}, Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f}, AUC : {:0.4f}".format(
                accuracy, precision, recall, f_score, auc
            )
        )

        os.makedirs(self.result_path, exist_ok=True)

        np.save(
            self.result_path + "result.npy",
            {
                "accuracy": accuracy,
                "precision": precision,
                "recall": recall,
                "f1": f_score,
                "auc": auc,
            },
        )
        print("result save to {}".format(self.result_path))
        return accuracy, precision, recall, f_score, auc

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="RCA")
    parser.add_argument("--algorithm", type=str, default="RCA", required=False)
    parser.add_argument("--seed", type=int, default=0, required=False)
    parser.add_argument("--data", type=str, default="pima", required=False)
    parser.add_argument("--max_epochs", type=int, default=200, required=False)
    parser.add_argument("--hidden_dim", type=int, default=256, required=False)
    parser.add_argument("--batch_size", type=int, default=128, required=False)
    parser.add_argument("--oe", type=float, default=0.0, required=False)
    parser.add_argument("--training_ratio", type=float, default=0.6, required=False)
    parser.add_argument("--learning_rate", type=float, default=3e-4, required=False)
    parser.add_argument("--z_dim", type=int, default=10, required=False)
    parser.add_argument("--coteaching", type=float, default=1.0, required=False)
    parser.add_argument("--missing_ratio", type=float, default=0.0, required=False)
    config = parser.parse_args()
    """
    read data
    """
    np.random.seed(config.seed)
    torch.manual_seed(config.seed)
    torch.cuda.manual_seed(config.seed)
    torch.backends.cudnn.benchmark = True
    Solver = Solver_RCA(
        data_name=config.data,
        hidden_dim=config.hidden_dim,
        z_dim=config.z_dim,
        seed=config.seed,
        learning_rate=config.learning_rate,
        batch_size=config.batch_size,
        training_ratio=config.training_ratio,
        max_epochs=config.max_epochs,
        missing_ratio=config.missing_ratio,
        oe=config.oe,
    )

    Solver.train()
    Solver.test()
    print("Data {} finished".format(config.data))

{'prf': 'sklearn.metrics.precision_recall_fscore_support', 'accuracy_score': 'sklearn.metrics.accuracy_score', 'roc_auc_score': 'sklearn.metrics.roc_auc_score'}


'''
Given a piece of text, create a histogram of letter pairs (order from high to low). For
instance, for the text, “this is a good thing”,
the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a)
The histogram will be:
th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1

Sample Input/Output:

Enter text: this is a good thing
Histogram: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1
Enter text: coooooool
Histogram: oo: 6, co: 1, ol: 1
'''
import collections

def create_histogram(text):
    hist = {}
    for i in range(len(text)-1):
        pair = text[i:i+2]
        if ' ' in pair:
            continue
        if pair in hist:
            hist[pair] += 1
        else:
            hist[pair] = 1

    hist_sorted = sorted(hist.items(), key=lambda kv: kv[1], reverse=True)
    hist = collections.OrderedDict(hist_sorted)
    return hist

print(create_histogram('this is a good thing'))


#!/usr/bin/env python3
""" Control de la grabación mediante un BOTON PULSADOR
    en RASPBERRY PI >= 2
"""

import  subprocess  as sp
import  gpiozero
from    time        import sleep
import  threading

#################    OPCIONES DE CONFIGURACION    ######################
# GPIO-03_pin-05 <---> GND_pin-06 (tercera pareja de pines)
BOTON = gpiozero.Button(3)  # GPIO-03

# Opción usando uno de los LED integrados en placa (rojo o verde)
#LED   = 'rojo'

# Opción usando un LED dedicado en un pin GPIO
LED   = gpiozero.LED(15)    # GPIO-15 (quinta pareja de pines)
########################################################################

def loop_boton_gpio(tp=3):
    """ BUCLE INFINITO que lee las pulsaciones del botón de control,
        con un tiempo de persistencia tp por defecto 3 segundos.
    """

    def espera_pulsacion():
        """ Comprueba que el botón esté pulsado durante tp segundos
        """
        c = 0
        while True:
            BOTON.wait_for_press()
            c += 1
            sleep(.5)
            if c >= 2 * tp:
                break

    print('PULSAR BOTON PARA INICIAR/PARAR')

    # BUCLE de lectura
    grabar = False
    while True:
        espera_pulsacion()
        grabar = {True:False, False:True}.get(grabar)
        if grabar:
            iniciar_grabacion()
        else:
            detener_grabacion()

def loop_detecta_jack_capture():
    """ BUCLE INFINITO para la detección del proceso 'jack_capture',
        activando entonces el flag JACK_CAPTURE, y gestionando en
        consecuencia las indicaciones de estado (LED y consola)
    """

    global JACK_CAPTURE

    def display_update(estado):
        """ maneja LED y consola
        """
        modo_led = {'PARADO':'off', 'GRABANDO':'normal', 'PREPARANDO':'rapido'}.get(estado, 'off')
        if TIPO_DE_LED == 'gpio':
            led_gpio_blink(modo_led)
        else:
            # el led integrado tiene un loop propio en threading
            pass

        print(f'---> {estado}')

    st = st_prev = 'PARADO'
    while True:

        if GRABACION_INICIADA:

            try:
                sp.check_output('pgrep -f jack_capture'.split())
                JACK_CAPTURE = True
            except:
                JACK_CAPTURE = False

            if JACK_CAPTURE:

                st = 'GRABANDO'
                if st != st_prev:
                    display_update(st)
                st_prev = st

            else:

                st = 'PREPARANDO'
                if st != st_prev:
                    display_update(st)
                st_prev = st

        else:

            st = 'PARADO'
            if st != st_prev:
                display_update(st)
            st_prev = st

        sleep(1)

def loop_led_integrado():
    """ BUCLE INFINITO que gobierna un led integrado en la placa, en función
        del las variables GRABACION_INICIADA y JACK_CAPTURE lo hace parpadar
        o lo mantiene apagado.

        NOTA: los led de placa no están gestionados por la libreria 'gpiozero',
              por tanto necesitamos este LOOP para hacerlo parpadear.
    """

    def blink(frec='normal'):
        t = {'normal': 1.0, 'rapido': 0.1, 'off': 0}.get(frec)
        if t:
            sp.Popen(f"sudo sh -c 'echo {onoff} > /sys/class/leds/{ledX}/brightness'", shell=True)
            sleep(t)
        else:
            sp.Popen(f"sudo sh -c 'echo 0       > /sys/class/leds/{ledX}/brightness'", shell=True)
            sleep(1)

    # Por defecto .get resolverá el LED 'rojo' de la placa 'led1'
    ledX = {'verde': 'led0', 'rojo': 'led1'}.get(LED, 'led1')

    # BUCLE del parpadeo (OjO el sleep está implementado en la subfunción blink)
    onoff = 0
    while True:

        if GRABACION_INICIADA:
            # Parpadeo normal cuando se detecte jack_capture funcionando
            if JACK_CAPTURE:
                blink('normal')
            else:
                blink('rapido')
        else:
            blink('off')

        onoff = {0:1, 1:0}.get(onoff)

def led_gpio_blink(mode='normal'):
    """ Usado para gestionar un LED dedicado en un pin GPIO
        usando la librería gpiozero
    """

    if mode == 'normal':
        ton = 1.5; toff = 1.0
    elif mode == 'rapido':
        ton = 0.15; toff = 0.15
    else:
        LED.off()
        return

    LED.blink(on_time=ton, off_time=toff)

def iniciar_grabacion():

    global GRABACION_INICIADA

    # Ejecuta script de incio de la grabadora
    sp.Popen("~/bin/grabadora_iniciar.sh", shell=True)
    GRABACION_INICIADA = True

def detener_grabacion():

    global GRABACION_INICIADA

    # Ejecuta script de detención de la grabadora
    sp.Popen("~/bin/grabadora_detener.sh", shell=True)
    GRABACION_INICIADA = False

if __name__ == "__main__":

    # Indicativo del tipo de LED en uso
    TIPO_DE_LED = 'integrado' if (type(LED) == str) else 'gpio'

    # Flags indicativos del estado de la grabadora
    GRABACION_INICIADA = False
    JACK_CAPTURE       = False

    # LOOP de lectura del botón de control
    th_boton = threading.Thread(target = loop_boton_gpio)
    th_boton.start()

    # LOOP que detecta si 'jack_capture' está en ejecución y maneja
    # la visualización al usuario en consecuencia.
    th_jack_capture = threading.Thread(target = loop_detecta_jack_capture)
    th_jack_capture.start()

    # LOOP auxiliar para hacer parpadear un LED integrado en placa
    if TIPO_DE_LED == 'integrado':
        th_led = threading.Thread(target = loop_led_integrado)
        th_led.start()


import asyncio
import urllib.request
import os
def download(url,file):
    #f = open(file,'wb')
    urllib.request.urlretrieve(url,file)
    #f.close()

def url_gen(url,start=1):
    url_root = url[:url.find('segment')]
    i = start
    while True:
        yield url_root+f'segment{i}.ts'
        i+=1

def segments_gen(url,path,start=1):
    url_root = url[:url.find('segment')]
    i = start
    while True:
        yield (url_root+f'segment{i}.ts',os.path.join(path,f'{i}.ts'))
        i+=1

def download_segments(url,path,start=1):
    url_generator = url_gen(url,start)
    i=start
    try:

        for each in url_generator:
            download(each, os.path.join(path,f'{i}.ts'))
            i+=1
    except urllib.request.HTTPError as e:
        return [n for n in range(start, i)]

def download_segments_parallel(url,path,workers=4,start=1):
    from concurrent.futures import ThreadPoolExecutor
    executor = ThreadPoolExecutor(max_workers=workers)
    for current_url,name in segments_gen(url,path,start):
        #print(current_url,name)
        try:
            result = executor.submit(download,current_url,name)
            result.result()
        except urllib.request.HTTPError as e:
            return

async def download_segments_parallel2(url,path,start=1):
    import aiohttp
    import aiofiles
    async with aiohttp.ClientSession() as session:
        for current_url, name in segments_gen(url,path,start):
            async with session.get(current_url) as resp:
                if resp.status == 200:
                    f = await aiofiles.open(name, mode='wb')
                    await f.write(await resp.read())
                    await f.close()
                elif resp.status == 404:
                    return

def download_segments_parallel_main(url,path,start):
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(download_segments_parallel2(url,path,start))


import vk_api
import json
import requests
import random
import traceback
import os
import subprocess
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from array import *
from random import randint
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
keyboard = VkKeyboard(one_time=True)

vk_session = vk_api.VkApi(token='token')
longpoll = VkBotLongPoll(vk_session, group_id="202796234")
vk = vk_session
num = int
msg_art = str
avk=vk_session.get_api()
##Далее описывается функционал отправки всякой хуйни и выгрузки фото (для последующей отправки)
def sender(id, text):
    avk.messages.send(user_id = id, message = text, random_id = 0, keyboard = keyboard.get_keyboard())
def send_attach(id, url):
    avk.messages.send(user_id = id, attachment = url, random_id = 0, keyboard = keyboard.get_keyboard())
def send_stick(id, number):
    avk.messages.send(user_id = id, sticker_id = number, random_id = 0)
def sender_chat(chat_id, text):
    avk.messages.send(chat_id = chat_id, message = text, random_id = random.randint(0,100))
def send_attach_chat(chat_id, url):
    avk.messages.send(chat_id = chat_id, attachment = url, random_id = 0)

def upload_photo(chat_id, filename):
    link = vk.method('photos.getMessagesUploadServer', {'peer_id': chat_id})['upload_url']
    print(link)
    cookies = vk.http.cookies
    up = json.loads(requests.post(link, files={'photo': open(filename, 'rb')}).text)
    photos = vk.method('photos.saveMessagesPhoto', up)
    print(photos)
    ownerId = photos[0]['owner_id']
    photoId = photos[0]['id']
    vk.method('messages.send', {'peer_id': chat_id, 'random_id': random.randint(0,2048), 'attachment': "photo{}_{}".format(ownerId, photoId)})
while True: ##цикл для лонгпола, без этой хуеты иногда вылетает ошиб_очка, а так на похуй
    try:
        for event in longpoll.listen():
            if event.type == VkBotEventType.MESSAGE_NEW:
                print(event)
                peer_id = event.message['peer_id']
                if peer_id >= 2000000000: ##Если пир айди - это айди беседы, значит бот работает в режиме беседы с одним функционалом
                    if 'action' in event.message:
                        if (event.message['action']['type'] == 'chat_invite_user') and (event.message['action']['member_id'] == -202796234):
                            chat_id = int(event.chat_id)
                            sender_chat(chat_id, 'Вас приветствует бот-идиот. Разрешите боту просматривать всю переписку чтобы команды срабатывали')
                    else:
                        msg = event.message['text'].lower()
                        chat_id = int(event.chat_id)
                        random_id = random.randint(0, 100)
                        attachments = []
                        id = event.message['from_id']
                        ##if msg in ["/art", "/арт", "[club152451931|@random_anime_official], арт", "[club152451931|@random_anime_official], art", "[club152451931|@random_anime_official], /art", "[club152451931|@random_anime_official], /арт", "[club152451931|@random_anime_official] /art", "[club152451931|@random_anime_official] арт", "[club152451931|@random_anime_official] art", "[club152451931|@random_anime_official] /арт"]:
                           ## files = os.listdir("/home/qph7/Yuri pack")
                            ##upload_photo(peer_id, "/home/qph7/Yuri pack/"+files[random.randint(0, len(files)-1)])
                            ##В этих строчках я пытался описать хуйню для добавления сразу нескольких картиночек в одно сообщение, но мне стало похуй
                        if msg in ["пчел"]:
                            send_attach_chat(chat_id, "photo-202796234_457239018") ##На основе вот этой команды предлагаю сделать команду на блок стикеров. Картинка выгружена в альбом паблика и отправляется просто url этой картинки, что даёт почти моментальный респонс
                        if msg in ["нацист"]:
                            send_attach_chat(chat_id, "video-29544671_171117427")
                        if msg in ["совок"]:
                            send_attach_chat(chat_id, "photo-202796234_457239017")
                        if msg in ["навальный", "/navalny", "овальный"]:
                            files = os.listdir("/home/qph7/овальный")
                            upload_photo(peer_id, "/home/qph7/овальный/"+files[random.randint(0, len(files)-1)])
                        if msg in ["/укр", "/ukr", "укр"]:
                            files = os.listdir("/home/qph7/Укр")
                            upload_photo(peer_id, "/home/qph7/Укр/"+files[random.randint(0, len(files)-1)])
                        if msg in ["/консерва", "консерва"]:
                            files = os.listdir("/home/qph7/Консерва")
                            upload_photo(peer_id, "/home/qph7/Консерва/"+files[random.randint(0, len(files)-1)])
                        if msg in ["/gosl"]:
                            files = os.listdir("/home/qph7/Гуслинг")
                            upload_photo(peer_id, "/home/qph7/Гуслинг/"+files[random.randint(0, len(files)-1)])
                else:     ##Если мы не находимся в беседе - функционал другой
                    msg = event.message['text'].lower()
                    id = event.message['from_id']
                    attachments = []
                    if msg in ["привет", "начать", "здравствуйте", "здравствуй", "ку", "хай", "прив", "дарова", "дороу", "привет.", "здравствуйте.", "здравствуй.", "ку.", "хай.", "прив.", "дарова.", "дороу.", "привет!", "здравствуйте!", "здравствуй!", "ку!", "хай!", "прив!", "дарова!", "дороу!"]:
                        sender(id, "Вас приветствует бот")
                        send_stick(id, 21)
                    elif msg in ["арт", "art", "/art", "/арт", "/юри", "юри"]:
                        files = os.listdir("/home/qph7/Yuri pack")
                        upload_photo(peer_id, "/home/qph7/Yuri pack/"+files[random.randint(0, len(files)-1)])
                    if msg in ["/укр", "/ukr", "укр"]:
                        files = os.listdir("/home/qph7/Укр")
                        upload_photo(peer_id, "/home/qph7/Укр/"+files[random.randint(0, len(files)-1)])
                        ##send_attach(id, 'photo-152451931_'+str(randint(457248038, 457251894))) ##photo-152451931_457248038 - photo-152451931_457251894
                    #elif msg[0] == "/art":
                        #msg_art == msg.split()
                        #print(msg_art)
                        #for word in msg_art:
                            #if word.isnumeric():
                                #num == word
                        #if num == num.empty:
                            #send_attach(id, 'photo-152451931_'+str(randint(457248038, 457251894)))
                        #else:
                            #while num > 0:
                                #send_attach(id, 'photo-152451931_'+str(randint(457248038, 457251894)))
                                #num = num - 1
                    ##О, здесь я тоже пытался описать отправку нескольких картинок, можно поржать
                    elif msg in ["включить пк"] and ((id == 428225339) or (id == 245663694)): ##Wake on LAN удобная штука
                        os.system("wakeonlan B4:2E:99:1B:EE:72")
                        sender(id, "Пк включен")
                    elif msg in ["ip"] and ((id == 428225339) or (id == 245663694)): ##А это мне лень было конфижить на роутере DHCP и поэтому я просто каждый раз обращался к боту чтобы он мне писал айпи локальный
                        result = subprocess.check_output("hostname -I", shell=True)
                        b = result[0:11]
                        print(b)
                        sender(id, b)
                    elif msg in ["пак"]:
                        sender(id, "Пак всех артов за всё время:\nyadi.sk/d/RxmjBQkxMeo1tw?w=1")
                    else:
                        sender(id, 'Я тебя не понимаю')
    except (KeyboardInterrupt, SystemExit):
        break
    except:
        traceback.print_exc()


#written by Zilin Shen and Daniel de Mello
import os
os.environ["CUDA_VISIBLE_DEVICES"]="1"

import numpy as np
from models.lstm import Lstm
from seq2seq.utils import softmax, print_header, get_events
from seq2seq.seq2seq_attention import Seq2seqAttention
import argparse
import copy
import tensorflow as tf
import random
import os

seed = 1
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)

def get_args(jupyter_args = None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--permute_truncated', required=False, action='store_true',
                        help="Bool for activating permutation invariance")
    parser.add_argument('--freeze_seq2seq', required=False, action='store_true',
                        help="Bool for keeping seq2seq weights at each relabel round ")
    parser.add_argument('--use_prob_embedding', required=False, action='store_true',
                        help="Bool for using original probability based embedding proposed in the original paper")
    parser.add_argument('--sequence_length', required=False, type=int, default=10,
                        help="Length of truncated subsequences used in the seq2seq training")
    parser.add_argument('--rv', required=False, type=int, default=1,
                        help="'round value' hyperparameter used for probability embedding, if activated")
    parser.add_argument('--ps_epochs', required=False, type=int, default=50,
                        help="number of training epochs for per-step detectors")
    parser.add_argument('--s2s_epochs', required=False, type=int, default=30,
                        help="number of training epochs for seq2seq detector")
    parser.add_argument('--relabel_rounds', required=False, type=int, default=1,
                        help="Number of relabel rounds")
    parser.add_argument('--patience', required=False, type=int, default=None,
                        help="Patience for early stopping. Any value activates early stopping.")
    parser.add_argument('--data_fraction', required=False, type=float, default=1.0,
                        help="Fraction (between 0 and 1) for how much of the training set to use.")
    #parser.add_argument('--name', required=False, type=str, default=None,
    #                    help="Experiment name.")
    parser.add_argument('--use_wandb', required=False, action='store_true',
                        help="Turn on logs with wandb")

    if jupyter_args is not None:
        args = parser.parse_args(jupyter_args)
    else:
        args = parser.parse_args()
    return args

#jupyter_args = ['--permute_truncated', '--use_prob_embedding']
args = get_args()
args.data_fraction = min(args.data_fraction, 1)
seq2seq_config = {"sequence_length": args.sequence_length,
                  "permute_truncated": args.permute_truncated,
                  "use_prob_embedding": args.use_prob_embedding,
                  "rv": args.rv
                  }

# -----------get the preprocessed training and testing saved as .npy files
test_label_infection = np.load('preprocess/saved/test_label_infection.npy')
train_label_infection = np.load('preprocess/saved/train_label_infection.npy')
test_data_infection = np.load('preprocess/saved/test_data_infection.npy')
train_data_infection = np.load('preprocess/saved/train_data_infection.npy')

test_label_reconnaissance = np.load('preprocess/saved/test_label_reconnaissance.npy')
train_label_reconnaissance = np.load('preprocess/saved/train_label_reconnaissance.npy')
test_data_reconnaissance = np.load('preprocess/saved/test_data_reconnaissance.npy')
train_data_reconnaissance = np.load('preprocess/saved/train_data_reconnaissance.npy')

test_label_attack = np.load('preprocess/saved/test_label_attack.npy')
train_label_attack = np.load('preprocess/saved/train_label_attack.npy')
test_data_attack = np.load('preprocess/saved/test_data_attack.npy')
train_data_attack = np.load('preprocess/saved/train_data_attack.npy')

all_data = {"infection":
                        {
                        'train': [train_data_infection, train_label_infection],
                        'test': [test_data_infection, test_label_infection]
                        },
            "attack":
                        {
                        'train': [train_data_attack, train_label_attack],
                        'test': [test_data_attack, test_label_attack]
                        },
            "reconnaissance":
                        {
                        'train': [train_data_reconnaissance, train_label_reconnaissance],
                        'test': [test_data_reconnaissance, test_label_reconnaissance]
                        }
            }

# ---------------------------wandb-------------------------------
if args.use_wandb:
    import wandb
    os.environ["WANDB_API_KEY"] = "8f5046c50251d53a31a4248e1f1ca0dc4e5c8cf2"
    wandb.login()
    run = wandb.init(
        # Set the project where this run will be logged
        project="ra",
        # Track hyperparameters and run metadata
        config={
            "data_fraction": args.data_fraction,
            "ps_epochs": args.ps_epochs,
            },
        name = f"df-{args.data_fraction}-{args.ps_epochs}"
        )

# ----------------create per-step detectors----------------------
ps_attack = Lstm("ps-detector-attack")
ps_attack.add_dataset(all_data['attack'])

ps_recon = Lstm("ps-detector-recon")
ps_recon.add_dataset(all_data['reconnaissance'])

ps_infec = Lstm("ps-detector-infec")
ps_infec.add_dataset(all_data['infection'])

# ----------------train per-step detectors----------------------
metrics_dict_train = {}
metrics_dict_test = {}
for detector in [ps_attack, ps_recon, ps_infec]:
    #train data
    train_data = detector.dataset['train']
    train_examples = train_data[0]
    train_labels = train_data[1]

    #test data
    test_data = detector.dataset['test']
    test_examples = test_data[0]
    test_labels = test_data[1]

    features_len = train_examples.shape[1]
    print('features len is ', features_len)

    print_header("Training {} detector".format(detector.name))
    detector.learning(features_len, train_examples, train_labels, kind='', epochs=args.ps_epochs, patience=args.patience, fraction=args.data_fraction)

    print_header("Measureing {} detector performance on train data".format(detector.name))
    _, _, metrics_dict_ps_train = detector.detection(train_examples, train_labels, kind='')
    print("Metrics: \n", metrics_dict_ps_train)
    metrics_dict_train[detector.name] = metrics_dict_ps_train

    print_header("Measureing {} detector performance on test data".format(detector.name))
    _, _, metrics_dict_ps_test = detector.detection(test_examples, test_labels, kind='')
    print("Metrics: \n", metrics_dict_ps_test)
    metrics_dict_test[detector.name] = metrics_dict_ps_test

if args.use_wandb:
    keys = ('auprc', 'r_at_99p', 'r_at_95p', 'p_at_99r', 'p_at_95r')
    wandb_dict_train = dict((k, metrics_dict_train[ps_infec.name][k]) for k in keys)
    wandb_dict_test = dict((k, metrics_dict_test[ps_infec.name][k]) for k in keys)
    wandb.log({'train': wandb_dict_train, 'test':  wandb_dict_test})

metrics_dict_per_round = []
for r in range(args.relabel_rounds):

    # -----------------seq2seq stage----------------------
    #get events
    events = get_events(ps_attack, ps_recon, ps_infec, all_data['infection']['train'][0])

    #init seq2seq
    if not(args.freeze_seq2seq):
        seq2seq = Seq2seqAttention('seq2seq')
    elif r == 0:
        seq2seq = Seq2seqAttention('seq2seq')

    #train seq2seq
    seq2seq.learning(events, all_data['infection']['train'][1], seq2seq_config, epochs=args.s2s_epochs)

    #get seq2seq tagged events
    events_preds, tagged_seq2seq = seq2seq.analysis(events, all_data['infection']['train'][1], seq2seq_config)

    #get per-step infection detector tagged windows
    preds_ps_infec = ps_infec.predict(all_data['infection']['train'][0], kind='')
    preds_ps_infec = np.array(preds_ps_infec).squeeze()
    print('preds ps infec shape is', preds_ps_infec.shape)
    tagged_ps_infec = []
    for idx, pred in enumerate(preds_ps_infec):
        if pred>0.5:
            tagged_ps_infec.append(idx)

    # -----------------relabeling---------------------
    #strategy 1
    retrain_pos = []
    retrain_neg = []
    for idx in tagged_ps_infec:
        if idx in tagged_seq2seq:
            retrain_pos.append(idx)
        else:
            retrain_neg.append(idx)

    #overwritte original dataset with new positive and negative labels
    retrain_labels = copy.deepcopy(all_data['infection']['train'][1])
    retrain_data = copy.deepcopy(all_data['infection']['train'][0])
    for idx, l in enumerate(retrain_labels):
        if idx in retrain_pos:
            retrain_labels[idx] = 1
        if idx in retrain_neg:
            retrain_labels[idx] = 0

    # -----------------retrain per-step infection detector with new labels---------------------
    #def retrain_detector(detector, retrain_data, retrain_labels, test_data, test_labels):
    features_len = retrain_data.shape[1]
    print('features len is ', features_len)

    print_header("Retraining {} detector".format('infection'))
    ps_infec.learning(features_len, retrain_data, retrain_labels, kind='', epochs=args.ps_epochs)

    print_header("Measureing {} detector performance on test data".format('infection'))
    _, _, metrics_dict_new = ps_infec.detection(detector.dataset['test'][0], detector.dataset['test'][1], kind='')

    print_header("Per-step infection detector metrics BEFORE relabeling")
    print( metrics_dict_test[ps_infec.name])

    print_header("Per-step infection detector metrics AFTER relabeling")
    metrics_dict_per_round.append(metrics_dict_new)
    for r, mdround in enumerate(metrics_dict_per_round):
        print(f'Round {r}:')
        print(mdround)

    if args.use_wandb:
        keys = ('auprc', 'r_at_99p', 'r_at_95p', 'p_at_99r', 'p_at_95r')
        wandb_dict_relabel = dict((k, metrics_dict_per_round[-1][k]) for k in keys)
        wandb.log({f'test_r{r}':  wandb_dict_relabel})

if args.use_wandb:
    wandb.finish()


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

import datetime
import os, re
import random
import sys, traceback
import posixpath
import user

import web
import board, article
import config
from config import render
from datetime import datetime
import i18n
import bbcode
_ = i18n.custom_gettext

lang_map = { 'ko': u'한국어',
             'en': u'English',
             'ru': u'Русский', }

def session_helper(func):
    def _exec(*args, **kwargs):
        try:
            current_uid = web.ctx.session.uid
        except:
            raise web.seeother(link('/+login'))
            #raise web.unauthorized(render[mobile].error(error_message = _("NOT_LOGGED_IN"), help_context = 'error'))
        if current_uid < 1:
            web.ctx.session.uid = 0
            web.ctx.session.kill()
            raise web.internalerror(default_render.error(error_message = _("INVALID_UID"), help_context = 'error'))
        kwargs.update({'current_uid': current_uid})
        return func(*args, **kwargs)
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def confirmation_helper(func):
    def _exec(*args, **kwargs):
        i = web.input()
        if i.has_key('ok'):
            return func(*args, **kwargs)
        else:
            raise web.seeother(i.referer)
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def theme(func):
    def _exec(*args, **kwargs):
        arglist = [args[0]]
        theme = args[1]

        if theme is not None and theme.endswith('/'):
            theme = theme[:len(theme)-1]

        if theme is None or theme == '':
            web.config.theme = 'default'
        elif theme in config.render:
            web.config.theme = theme
        else:
            raise NameError('Invalid theme: ' + theme)

        for i in range(2, len(args)):
            arglist.append(args[i])

        res = func(*tuple(arglist), **kwargs)
        return res
    return _exec

def error_catcher(func):
    def _exec(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (web.webapi.unauthorized, web.webapi._NotFound, web.webapi._InternalError, web.webapi.SeeOther): # 웹 프로그램의 오류. 대개 해결 가능.
            raise
        except Exception as e:
            current_ctx = web.ctx
            error_text = traceback.format_exc()
            store_error(current_ctx, error_text)
            raise web.internalerror(config.default_render.error(error_message = e,
                error_detail = error_text, help_context = 'error'))
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def store_error(current_ctx, error_text):
    if not config.store_error_report:
        return
    today = datetime.today()
    filename = 'error_%s.txt' % today.strftime('%Y%m%d_%H%M%S')
    try:
        f = open(os.path.join(config.error_report_path, filename), 'w')
        f.write('Traceback:\n')
        f.write(error_text)
        f.write('\n')
        f.write('Context:\n')
        f.write(str(current_ctx))
        f.write('\n')
        f.close()
    except OSError:
        pass

def validate_username(name):
    match = re.search(r'\W+', name)
    if match == None:
        return True
    else:
        return False

def validate_boardname(name):
    match = re.search(r'[^\w/]+', name)
    if match == None:
        return True
    else:
        return False

def format(original):
    b = bbcode.parse
    try:
        return b(process_noah12k_quote(original))
    except:
        text = traceback.format_exc()
        #text = process_noah12k_quote(original)
        return text.replace(' ', ' ').replace('\n', '
\n')

def remove_bracket(original):
    # BBCode 파서 안에 [/]이 들어가면 파서가 헷갈리므로
    # 글 제목에 들어간 [/]을 (/)으로 바꿈.
    return original.replace('[', '(').replace(']', ')')

def process_noah12k_quote(original):
    # noah 1k/2k의 인용구를 BBCode 형태로 바꿔 줌.
    # 반드시 HTML로 변환하기 전에 호출해야 함.
    indent = '> '
    header = [u'"에서: ',u'"에서 : ']
    lines = original.split('\n')
    levels = []
    ret = u''
    for i in range(0, len(lines)):
        last_q = 0
        while lines[i].find(indent) == 0:
            last_q = last_q + 1
            lines[i] = lines[i][len(indent):]
        levels.append(last_q)
    assert(len(levels) == len(lines))

    trimBegin = False
    for i in range(0, len(lines)):
        if i==0 or (levels[i-1] < levels[i]):
            trimBegin = True
        if trimBegin and lines[i].strip() == '':
            lines[i] = ''
        else:
            trimBegin = False

    trimEnd = False
    for i in range(len(lines)-1, -1, -1):
        if i==len(levels)-1 or levels[i] > levels[i+1]:
            trimEnd = True
        if trimEnd and lines[i] == '' and lines[i].strip() == '':
            lines[i] = ''
        else:
            trimEnd = False

    for i in range(0, len(lines)):
        prevLevel = 0
        if i>0:
            prevLevel = levels[i-1]
        while levels[i] != prevLevel:
            if levels[i] > prevLevel:
                if lines[i-1].endswith(header[0]) or lines[i-1].endswith(header[1]):
                    ret += '[quote=%s]\n' % remove_bracket(lines[i-1])
                else:
                    ret += '[quote]\n'
                prevLevel = prevLevel + 1
            if levels[i] < prevLevel:
                ret +='[/quote]\n'
                prevLevel = prevLevel - 1
        if lines[i] != '':
            if not( lines[i].endswith(header[0]) or lines[i].endswith(header[1])):
                ret +=lines[i]
            else:
                continue
        if i < len(lines)-1:
            ret +='\n'
    return ret

def get_login_notice(notice_board = '/noah/welcome'):
    # /noah/welcome에서 공지 표시된 글 중 아무거나 하나를 랜덤으로 돌려 줌.
    # 공지 표시된 글이 없는 경우 '공지가 없습니다.'를 돌려 줌.
    articles = []
    board_id = board._get_board_id_from_path(notice_board)
    if board_id < 0:
        return _('INVALID_NOTICE_BOARD')
    for i in article.get_marked_article(board_id):
        articles.append(i)
    if len(articles) == 0:
        return _('NO_NOTICE')
    return articles[random.randint(0, len(articles)-1)].aContent

def lcode_to_name(lcode):
    return lang_map[lcode]

def get_lang_map():
    return lang_map

def traverse_board_path(path, theme=None):
    if path == '^root':
        path = '/'
    ret = ""
    id = board._get_board_id_from_path(path)
    while id != 1 and id > 0:
        i = board.get_board_info(id)
        if i.bType == 0:
            if theme:
                ret = '/%s' % (theme, i.bName, i.bName.split('/')[-1]) + ret
            else:
                ret = '/%s' % (i.bName, i.bName.split('/')[-1]) + ret
        else:
            if theme:
                ret = '/%s' % (theme, i.bName, i.bName.split('/')[-1]) + ret
            else:
                ret = '/%s' % (i.bName, i.bName.split('/')[-1]) + ret
        new_id = board.get_parent(id)
        if new_id == id:
            break
        id = new_id
#    ret = (u'%s' % _('Board')) + ret
    return ret

def get_type(path):
    if path == '^root':
        path = '/'
    id = board._get_board_id_from_path(path)
    if id < 0:
        return -1
    data = board.get_board_info(id)
    return data.bType

def choose_banner():
    banners = []
    try:
        f = open(config.banner_path, 'r')
        banners = f.readlines()
        f.close()
    except IOError:
        return ['default.png', '#']
    cumulative_list = []
    j = 0
    for b in banners:
        banners[j] = b.strip().split(' ')
        banners[j][2] = int(banners[j][2])
        if len(cumulative_list) == 0:
            cumulative_list.append(banners[j][2])
        else:
            cumulative_list.append(cumulative_list[-1] + banners[j][2])
        j = j + 1
    banner_id = random.randint(1, max(cumulative_list))
    j = 0
    ret = tuple()
    cond = False
    for i in cumulative_list:
        if j == 0:
            cond = (banner_id <= i)
        else:
            cond = (cumulative_list[j-1] < banner_id) and (banner_id <= i)
        if cond:
            ret = tuple((banners[j][0], banners[j][1]))
            break
        else:
            j = j + 1
    return ret

#always starts with traling /
def link(url):
    if web.config.theme is not 'default':
        if url.startswith('/'):
            url = '/' + web.config.theme + url
        else:
            url = web.config.theme + '/' + url
    return url

def https():
    return config.https_domain;

def render():
    try:
        if web.config.theme == 'default':
            return config.render[user.get_user(web.ctx.session.uid)[1].theme]
        else:
            return config.render[web.config.theme]
    except:
        return config.render[web.config.theme]

def strip_path(path):
    elems = path.split('/')
    for i in range(len(elems)-1):
        elems[i] = elems[i][:1]
    return '/'.join(elems)


import numpy as np
import matplotlib.pyplot as plt
import random
from math import pi, cos, sin
from itertools import product

def draw_ann(ax, synapses):
    num_neurons = synapses.shape[0]
    neuron_positions = np.zeros((2,num_neurons))
    angle = 0.0
    angle_update = 2 * pi / num_neurons
    for i in range(num_neurons):
        neuron_positions[0,i] = sin(angle)
        neuron_positions[1,i] = cos(angle)
        angle += angle_update

    for (i,j) in product(range(num_neurons),repeat=2):
        (x1,y1) = neuron_positions[:,i]
        (x2,y2) = neuron_positions[:,j]
        w = synapses[i,j]
        color = [0,0,0] if w > 0 else [0.8,0.8,0.8]
        lw = int(10*abs(w)+1)
        ax.plot([x1,x2],[y1,y2], color=color, linewidth=lw)

    ax.plot(
        neuron_positions[0,:],
        neuron_positions[1,:],
        'ko',
        markerfacecolor=[1,1,1],
        markersize=18)

def simulate_neurons(neurons, synapses):
    time_steps, num_neurons = neurons.shape
    for t in range(1, time_steps):
        old_n_vals = neurons[t-1,:]
        for n in range(num_neurons):
            weights = synapses[n,:]
            temp = np.sum(old_n_vals * weights)
            if temp > 1:
                new_val = 1
            elif temp < 0:
                new_val = 0
            else:
                new_val = temp
            neurons[t,n] = new_val

fig = plt.figure(figsize=(12,36))
for a in range(5):
    neurons = np.zeros((50,10))
    for i in range(neurons.shape[1]):
        neurons[0,i] = random.random()

    synapses = np.vectorize(lambda x: x*2 -1)(np.random.rand(10,10))

    simulate_neurons(neurons, synapses)

    ax1 = fig.add_subplot(5,2,a*2+1)
    draw_ann(ax1, synapses)

    ax2 = fig.add_subplot(5,2,a*2+2)
    ax2.set_ylabel("Time step")
    ax2.set_xlabel("Neuron")
    ax2.imshow(neurons, aspect='auto', cmap=plt.cm.gray, interpolation='nearest')
    fig.savefig("out02.png", bbox_inches='tight')


# importing the module
import math
import os
import pickle
import threading
import time
import cv2
import numpy as np
from shapely.geometry import Point, Polygon
from Detector import Detector
import pygame
from progress.bar import Bar

from BoardScene import BoardScene
from Board import Board
from Constants import POINT_BBOXS, POINT_CENTERS, NUM_POINTS, NUM_POINT_HOMOGRAPHY
from Point import Point
from Disk import Disk, Color
from Utils import pointInPoly
from Dice import Dice
from Class import Class
from BoardPosition import BoardPosition
from Snapshot import Snapshot
from Snapshots import Snapshots

class BackgammonCV:
    def __init__(self):

        self.video = 0
        self.total_frames = 0
        self.detection_every_n_frames = 5
        self.fps = 0
        self.duration = 0
        self.frame_index = 0
        self.template_aligned = False
        self.isPlaying = False
        self.isReplaying = False
        self.replay_frame_index = 0
        self.detect_thread = threading.Thread(target=self.detect, args=(0,))
        self.replay_thread = threading.Thread(target=self.replay)

        self.snapshots = Snapshots()

        self.template = []
        self.frame = []
        self.overlay_text = []
        self.overlay = []
        self.transparent_overlay = []

        self.template_width = 0
        self.template_height = 0
        self.image_width = 0
        self.image_height = 0
        self.alpha_overlay = 0.5

        self.points_template = []
        self.points_homography = []
        self.transformation_matrix = []

        self.point_centers = POINT_CENTERS
        self.point_bboxs = POINT_BBOXS

        self.board = Board()
        self.board_scene = 0

        self.p_min = 0.2
        self.threshold_nms = 0.3
        self.detector = Detector(self.p_min, self.threshold_nms)

        self.bar = Bar("Processing", max=20)
        self.replayBar = Bar("Replay", max=20)
        self.saveBar = Bar("Saving", max=20)

        self.initBoardUI()
        self.loadTemplate()

    def initBoardUI(self):
        pygame.init()
        # UI absolute window position
        os.environ["SDL_VIDEO_WINDOW_POS"] = "%d, %d" % (2, 100)
        self.board_scene = BoardScene()

    def loadTemplate(self):
        self.template = cv2.imread("../data/images/test/template.jpg")
        self.template_height, self.template_width, _ = self.template.shape
        self.points_template = [(0, 0), (self.template_width, 0), (self.template_width, self.template_height), (0, self.template_height)]

    def alignTemplate(self, image):

        if isinstance(image, str):
            self.frame = cv2.imread(image)
        else:
            self.frame = image

        self.board_scene.alligning()
        self.overlay_text = self.frame.copy()
        self.image_height, self.image_width, _ = self.frame.shape

        self.points_homography = []

        # Add points to board
        for i in range(len(self.point_bboxs)):
            point = Point((i + 1), self.point_centers[i], bbox=self.point_bboxs[i])
            self.board.addPoint(point)

        # displaying the image
        cv2.imshow("Source", self.frame)
        cv2.setMouseCallback("Source", self.mouseEvent)

    def mouseEvent(self, event, x, y, flags, params):

        # Test pointInPoly
        if event == cv2.EVENT_MOUSEMOVE:
            if len(self.points_homography) == NUM_POINT_HOMOGRAPHY:
                for point in self.board.points:
                    if pointInPoly((x, y), point.bbox_warped):
                        # print("inside " + str(point.id))
                        overlay_copy = self.overlay.copy()
                        if point.id == 25:
                            cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (0, 0, 255))
                        else:
                            cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (127))
                        self.overlay_text = cv2.addWeighted(overlay_copy, self.alpha_overlay, self.overlay, 1 - self.alpha_overlay, 0)
                        cv2.imshow("Source", self.overlay_text)

        if event == cv2.EVENT_LBUTTONDOWN:

            self.points_homography.append((x, y))

            if len(self.points_homography) > NUM_POINT_HOMOGRAPHY:

                self.board.reset()
                self.board_scene.updateBoard(self.board)
                self.points_homography.clear()
                self.overlay_text = self.frame.copy()

            for cords in self.points_homography:
                x, y = cords
                cv2.putText(self.overlay_text, str(cords[0]) + ", " + str(y), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
                cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)
            cv2.imshow("Source", self.overlay_text)

            # print((x, y))

            # Align template----------------------------------------------------------------------------------

            if len(self.points_homography) == NUM_POINT_HOMOGRAPHY:
                # print(points_img)
                self.overlay_text = self.frame.copy()

                self.board.bbox = self.points_homography.copy()

                # Draw
                for cords in self.points_homography:
                    x, y = cords
                    cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)

                # MATRIX TRANFORM --------------------------------------------------------------

                # Find matrix
                source = np.asarray(self.points_template, dtype=np.float32)
                destination = np.asarray(self.points_homography, dtype=np.float32)
                self.transformation_matrix = cv2.getPerspectiveTransform(source, destination)
                image_out = cv2.warpPerspective(self.template, self.transformation_matrix, (self.image_width, self.image_height))

                # Apply transofmration to all point's bbox
                for point in self.board.points:
                    point.bbox_warped = np.asarray(point.bbox_warped, dtype=np.float32)
                    point.bbox_warped = point.bbox_warped.reshape((-1, 1, 2))

                    point.bbox_warped = cv2.perspectiveTransform(point.bbox_warped, self.transformation_matrix)

                    point.bbox_warped = np.array(point.bbox_warped, dtype=np.int32)

                    # point.center = self.warp_point(point.center, self.transformation_matrix)

                    # Draw
                    overlay_copy = self.overlay_text.copy()
                    cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (255, 255, 255))

                    self.overlay_text = cv2.addWeighted(overlay_copy, self.alpha_overlay, self.overlay_text, 1 - self.alpha_overlay, 0)

                self.overlay = cv2.add(self.overlay_text, image_out)

                # print(self.board)
                self.template_aligned = True
                self.board_scene.update()
                cv2.imshow("Source", self.overlay)
                # self.detect(self.image)

        # checking for right mouse clicks
        if event == cv2.EVENT_RBUTTONDOWN:
            if len(self.points_homography) > 0:
                self.points_homography.pop()
                self.board.reset()
            self.overlay_text = self.frame.copy()
            for cords in self.points_homography:
                x, y = cords
                cv2.putText(self.overlay_text, str(cords[0]) + ", " + str(y), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
                cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)
            cv2.imshow("Source", self.overlay_text)

    def detectThread(self, image):
        if self.detect_thread.is_alive():
            self.detect_thread.join()
        self.detect_thread = threading.Thread(target=self.detect, args=(image,))
        self.detect_thread.start()

    def detect(self, image):

        if not self.template_aligned:
            print("\nFirst you need to align the template. Click clockwise on the 4 extreme points of the board starting from the top left one\n")
            return

        if self.isPlaying:
            self.bar.goto(self.frame_index + 1)
        else:
            self.board_scene.detecting()

        if isinstance(image, str):
            self.frame = cv2.imread(image)
        else:
            self.frame = image

        # cv2.imshow("Source", self.frame)

        self.board.clear()

        self.image_height, self.image_width, _ = self.frame.shape

        self.detector.detect(self.frame)

        self.overlay = self.detector.drawResult()
        self.transparent_overlay = self.detector.drawBboxs()

        self.board.dices = []  # self.board.clear()

        # Generate objects from YOLO detection ---------------------------------------------------------------
        for i in range(len(self.detector.centers)):

            # DISK WHITE
            if self.detector.class_numbers[i] == Class.DISK_WHITE:
                newDisk = Disk(self.detector.centers[i], self.detector.confidences[i], Color.BLACK)
                self.board.addDisk(newDisk)

            # DISK BLACK
            if self.detector.class_numbers[i] == Class.DISK_BLACK:
                newDisk = Disk(self.detector.centers[i], self.detector.confidences[i], Color.WHITE)
                self.board.addDisk(newDisk)

            # DICE
            if self.detector.class_numbers[i] < Class.DISKS:
                newDice = Dice(self.detector.class_numbers[i], self.detector.centers[i], self.detector.confidences[i])

                # Dice position binarization
                if newDice.center[0] >= self.board.getBar().bbox_warped[0][0][0]:
                    newDice.board_position = BoardPosition.RIGHT
                else:
                    newDice.board_position = BoardPosition.LEFT

                self.board.addDice(newDice)

        # print("Dices: " + str(self.board.dices))

        self.board_scene.updateBoard(self.board)

        # Add snapshot
        snapshot = Snapshot(self.frame_index, self.board.copy(), self.frame.copy(), self.transparent_overlay.copy())
        self.snapshots.addSnapshot(snapshot)
        # print(self.snapshots)

        cv2.imshow("Source", self.overlay)

        if self.isPlaying:
            # time.sleep(0.1)
            self.nextFrame()
            self.detect(self.frame)

    # def warp_point(self, center, M):
    #     x, y = center
    #     d = M[2, 0] * x + M[2, 1] * y + M[2, 2]

    #     return (
    #         int((M[0, 0] * x + M[0, 1] * y + M[0, 2]) / d),  # x
    #         int((M[1, 0] * x + M[1, 1] * y + M[1, 2]) / d),  # y
    #     )

    def saveReplay(self):
        # print("\nSaving replay")
        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        out = cv2.VideoWriter("../data/videos/result/replay.mp4", fourcc, 20.0, (1280, 720))
        s = ""
        currentSnapshotIndex = 0
        # overlay = np.zeros((self.image_height, self.image_width, 3), np.uint8)
        # frame_index = self.snapshots.getLastSnapshot().frame_index
        # print(frame_index)
        self.saveBar.max = self.total_frames
        self.saveBar.fill = "\u258C"
        self.saveBar.suffix = "%(index)d/%(max)d (%(percent)d%%) [remaining %(eta_time)s]"
        self.saveBar.goto(1)
        for i in range(self.total_frames - 1):
            self.saveBar.next()
            self.video.set(1, i)
            _, frame = self.video.read()

            # if frame processed and saved in snapshot add overlay
            if i == self.snapshots.getSnapshot(currentSnapshotIndex).frame_index and currentSnapshotIndex < self.snapshots.getLastSnapshot().id:
                currentSnapshotIndex += 1
                # self.board_scene.updateBoard(self.snapshots.getSnapshot(currentSnapshotIndex - 1).board)

            overlay = self.snapshots.getSnapshot(currentSnapshotIndex - 1).overlay
            frame = cv2.add(overlay, frame)
            out.write(frame)

        # for snapshot in self.snapshots.snapshots:
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     # time.sleep(0.2)
        #     out.write(snapshot.overlay)
        #     # self.board_scene.updateBoard(snapshot.board)
        #     s += "Frame " + str(snapshot.frame_index) + "\n"
        #     s += str(snapshot.board) + "\n\n"
        out.release()
        # text_file = open("snapshots.txt", "w")
        # text_file.write(s)
        # text_file.close()
        print("\nReplay saved in replay.mp4 and snapshots.txt ")

    def replayThread(self):
        if self.replay_thread.is_alive():
            self.replay_thread.join()
            # print("Stop replay")
        else:
            self.replay_thread = threading.Thread(target=self.replay)
            self.replay_thread.start()

    def replay(self):
        # print("\nReplay")
        # for snapshot in self.snapshots.snapshots:
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     cv2.imshow("Source", snapshot.overlay)
        #     # time.sleep(0.2)
        #     self.board_scene.updateBoard(snapshot.board)
        #     time.sleep(0.06)
        currentSnapshotIndex = 0
        # overlay = np.zeros((self.image_height, self.image_width, 3), np.uint8)
        max_frame_index = self.snapshots.getLastSnapshot().frame_index
        # print(max_frame_index)
        self.replayBar.max = max_frame_index
        self.replayBar.fill = "\u258C"
        self.replayBar.suffix = "%(index)d/%(max)d (%(percent)d%%) [remaining %(eta_time)s]"
        self.replayBar.goto(0)

        for i in range(max_frame_index):
            self.replayBar.next()
            self.video.set(1, i)
            _, frame = self.video.read()

            # if frame processed and saved in snapshot add overlay
            if i == self.snapshots.getSnapshot(currentSnapshotIndex).frame_index:
                currentSnapshotIndex += 1
                self.board_scene.updateBoard(self.snapshots.getSnapshot(currentSnapshotIndex - 1).board)

            overlay = self.snapshots.getSnapshot(currentSnapshotIndex - 1).overlay
            frame = cv2.add(overlay, frame)

            cv2.imshow("Source", frame)

            time.sleep(0.016)
        # print("\nEnd replay")

    def nextFrame(self):
        self.frame_index += self.detection_every_n_frames
        self.frame_index = min(self.frame_index, self.total_frames - 1)
        if self.frame_index == self.total_frames - 1:
            self.stop()
            self.video.set(1, self.frame_index)
            _, self.frame = self.video.read()
            if not self.isPlaying:
                cv2.imshow("Source", self.frame)
            # time.sleep(3)
            self.saveSnapshots()
            # self.bar.finish()
            # self.replay()
        else:
            self.video.set(1, self.frame_index)
            _, self.frame = self.video.read()
            if not self.isPlaying:
                cv2.imshow("Source", self.frame)
        # self.detect(self.frame)
        # return self.frame_index

    def previousFrame(self):
        self.frame_index -= self.detection_every_n_frames
        self.frame_index = max(0, self.frame_index)
        self.video.set(1, self.frame_index)
        _, self.frame = self.video.read()
        if not self.isPlaying:
            cv2.imshow("Source", self.frame)
        # print(type(self.frame))
        # self.detect(self.frame)
        # return self.frame_index

    def togglePlay(self):
        if self.isPlaying:
            self.isPlaying = False
            self.stop()
        else:
            self.isPlaying = True
            self.play()

    # def toggleReplay(self):
    #     if self.isReplaying:
    #         self.isReplaying = False
    #         self.stopReplay()
    #     else:
    #         self.isReplaying = True
    #         self.replay()

    def play(self):
        # print("Play")
        self.isPlaying = True
        self.detectThread(self.frame)

    def stop(self):
        # print("Stop")
        self.isPlaying = False

    def saveSnapshots(self):
        print("\nSaving snapshots")
        picklefile = open("../data/snapshots/snapshots.bcv", "wb")
        pickle.dump(self.snapshots, picklefile)
        picklefile.close()
        print("Snapshots saved")

    def loadSnapshots(self):
        print("\n\nLoading snapshots")
        picklefile = open("../data/snapshots/snapshots.bcv", "rb")
        # unpickle the dataframe
        snapshots = pickle.load(picklefile)
        picklefile.close()
        # close file
        self.snapshots = snapshots
        # print(self.snapshots)
        print(str(len(self.snapshots.snapshots)) + " snapshots loaded\n")


from datetime import datetime, timezone, timedelta
from alpaca_trade_api.rest import REST
import os

api = REST(os.environ.get("APCA_API_KEY_ID"),
           os.environ.get("APCA_API_SECRET_KEY"),
           api_version='v2')

def get_historical_data(tickers):
    # Maximum number of days per bar
    limit = 365*2

    # RFC3339 date format
    end_date = datetime.now(timezone.utc).astimezone()
    delta = timedelta(days=365*2)
    start_date = end_date-delta
    end_date.isoformat()
    start_date.isoformat()

    historical_data = api.get_barset(tickers,
                                     "day",
                                     limit,
                                     start_date,
                                     end_date)

    return historical_data

def remap_keys(mapping):
    new_mapping = dict()

    for parent_key, parent_value in mapping.items():
        ticker = parent_key[0]
        metric = parent_key[1]

        sorting_list = [{"date": k.date(), metric: v} for k, v in parent_value.items()]
        sorted_data = sorted(sorting_list, key=lambda l: l["date"])

        if ticker not in new_mapping:
            new_mapping[ticker] = [data_point for data_point in sorted_data]
        else:
            for index, element in enumerate(new_mapping[ticker]):
                element[metric] = sorted_data[index][metric]

    return new_mapping


import collections
from collections.abc import Iterable  # pylint: disable=g-importing-member
import itertools
import warnings

import jax
from jax import core
from jax import lax
from jax.extend import linear_util as lu
from jax.interpreters import partial_eval as pe
import jax.numpy as jnp
import numpy as np

def _pmap_device_order():
  return jax.local_devices()

def replicate(tree, devices=None):
  """Replicates arrays to multiple devices.

  Args:
    tree: a pytree containing the arrays that should be replicated.
    devices: the devices the data is replicated to
      (default: same order as expected by `jax.pmap()`).
  Returns:
    A new pytree containing the replicated arrays.
  """
  devices = devices or _pmap_device_order()
  return jax.device_put_replicated(tree, devices)

def unreplicate(tree):
  """Returns a single instance of a replicated array."""
  return jax.tree_util.tree_map(lambda x: x[0], tree)

def pmean(xs, axis_name):
  warnings.warn("use jax.lax.pmean instead", DeprecationWarning)
  return lax.pmean(xs, axis_name)

def partial_eval_by_shape(fn, input_spec, *args, **kwargs):
  """Lazily evaluate a function by using the shapes of the inputs.

  This function is similar to `jax.eval_shape` with the key difference that
  function outputs that can be computed without a concrete value of the
  inputs are returned as is instead of only the shape. See for example
  `module.init_by_shape` where this functionality is used to initialize a
  model without using input data lr computation.

  Args:
    fn: the function to be lazily evaluated.
    input_spec: an iterable of shapes or (shape, dtype) tuples specifying the
      shape and type of the inputs. If unspecified the dtype is float32.
    *args: other arguments passed to the module's apply function
    **kwargs: keyword arguments passed to the module's apply function
  Returns:
    A pair consisting of the model output and an instance of Model
  """
  # output cannot be returned in lazy_create because jax.eval_shape will only
  # return the shape and dtype.
  # TODO(mattjj,jheek): use a public JAX API
  f = lambda *inputs: fn(*inputs, *args, **kwargs)
  input_structs = [_parse_spec(spec) for spec in input_spec]
  inputs_flat, in_tree = jax.tree_util.tree_flatten(input_structs)
  f_flat, out_tree = jax.api_util.flatten_fun_nokwargs(lu.wrap_init(f), in_tree)
  in_pvals = [
      pe.PartialVal.unknown(core.ShapedArray(x.shape, x.dtype))
      for x in inputs_flat
  ]
  _, out_pvals, _ = pe.trace_to_jaxpr_nounits(f_flat, in_pvals)
  out_flat = [
      const if pv is None else jax.ShapeDtypeStruct(pv.shape, pv.dtype)
      for pv, const in out_pvals
  ]
  return jax.tree_util.tree_unflatten(out_tree(), out_flat)

def _parse_spec(spec):
  """Parse an input spec of the form (shape, dtype) or shape into a jax.ShapeDtypeStruct."""
  spec = tuple(spec)
  if len(spec) == 2 and isinstance(spec[0], Iterable):
    return jax.ShapeDtypeStruct(tuple(spec[0]), spec[1])
  else:
    return jax.ShapeDtypeStruct(spec, jnp.float32)

def prefetch_to_device(iterator, size, devices=None):
  """Shard and prefetch batches on device.

  This utility takes an iterator and returns a new iterator which fills an on
  device prefetch buffer. Eager prefetching can improve the performance of
  training loops significantly by overlapping compute and data transfer.

  This utility is mostly useful for GPUs, for TPUs and CPUs it should not be
  necessary -- the TPU & CPU memory allocators (normally) don't pick a memory
  location that isn't free yet so they don't block. Instead those allocators OOM.

  Args:
    iterator: an iterator that yields a pytree of ndarrays where the first
      dimension is sharded across devices.

    size: the size of the prefetch buffer.

      If you're training on GPUs, 2 is generally the best choice because this
      guarantees that you can overlap a training step on GPU with a data
      prefetch step on CPU.

    devices: the list of devices to which the arrays should be prefetched.

      Defaults to the order of devices expected by `jax.pmap`.

  Yields:
    The original items from the iterator where each ndarray is now sharded to
    the specified devices.
  """
  queue = collections.deque()
  devices = _pmap_device_order() if devices is None else devices

  def _prefetch(xs):
    return jax.device_put_sharded(list(xs), devices)

  def enqueue(n):  # Enqueues *up to* `n` elements from the iterator.
    for data in itertools.islice(iterator, n):
      queue.append(jax.tree_util.tree_map(_prefetch, data))

  enqueue(size)  # Fill up the buffer.
  while queue:
    yield queue.popleft()
    enqueue(1)

def _scan_nd(body_fn, init, xs, n=1, unroll=(1,)):
  """Utility for performing an n-dimensional `lax.scan`.

  The n-d scan is simply recursive call of 1-d scan.
  Args:
    body_fn: the body of the loop of type (c, x) -> (c, y).
    init: initial value for the carry.
    xs: a pytree of tensors to scan over.
    n: number of dimensions to scan over (default: 1)
  Returns:
    A tuple of the final carry and the values returned by the body.
  """
  if n == 1:
    return lax.scan(body_fn, init, xs, unroll=unroll[0])
  else:

    def scan_body(c, x):
      return _scan_nd(body_fn, c, x, n=n - 1, unroll=unroll[1:])

    return lax.scan(scan_body, init, xs, unroll=unroll[0])

def _invert_perm(perm):
  perm_inv = [0] * len(perm)
  for i, j in enumerate(perm):
    perm_inv[j] = i
  return tuple(perm_inv)

def scan_in_dim(body_fn, init, xs, axis=(0,), unroll=(1,), keepdims=False):
  """utility for doing a scan along arbitrary dimensions.

  See `lax.scan` for details on how the scan operation works.

  Note on `unroll`: This argument gets left padded with ones to match the size
  of `axis`. Doing so allows unrolls to performed from the innermost loop first.
  For example, `scan_in_dim(..., axis=(1, 2, 3), unroll=5)` is equivalent to
  `scan_in_dim(..., axis=(1, 2, 3), unroll=(1, 1, 5))`.

  Args:
    body_fn: the body of the loop of type (c, x) -> (c, y).
    init: initial value for the carry.
    xs: a pytree of tensors to scan over.
    axis: the axis to scan over.
    keepdims: keep the dimensions that are scanned over.
    unroll: an optional positive integer, or tuple of positive integers
      showing how many iterations of the loop to be unrolled into a single
      iteration for each axis.
  Returns:
    A tuple of the final carry and the values returned by the body.
  """
  if not isinstance(axis, Iterable):
    axis = (axis,)

  if not isinstance(unroll, Iterable):
    unroll = (unroll,)

  # Pad unroll with ones so we start unrolling from the innermost loop
  len_diff = len(axis) - len(unroll)
  unroll = (1,) * len_diff + unroll

  def transpose_in(x):
    perm = axis + tuple(np.delete(np.arange(x.ndim), axis))
    return x.transpose(perm)

  def transpose_out(x):
    perm = axis + tuple(np.delete(np.arange(x.ndim), axis))
    return x.transpose(_invert_perm(perm))

  def body_wrapper(c, xs):
    if keepdims:
      xs = jax.tree_util.tree_map(
          lambda x: x.reshape((1,) * len(axis) + x.shape), xs
      )
      xs = jax.tree_util.tree_map(transpose_out, xs)
    c, ys = body_fn(c, xs)
    if keepdims:
      ys = jax.tree_util.tree_map(transpose_in, ys)
      ys = jax.tree_util.tree_map(lambda x: x.reshape(x.shape[len(axis) :]), ys)
    return c, ys

  xs = jax.tree_util.tree_map(transpose_in, xs)
  c, ys = _scan_nd(body_wrapper, init, xs, n=len(axis), unroll=unroll)
  ys = jax.tree_util.tree_map(transpose_out, ys)
  return c, ys

# Copied from https://github.com/google-research/big_vision
def pad_shard_unpad(
    wrapped, static_argnums=(0,), static_argnames=(), static_return=False
):
  """Wraps a function with code that pads, shards, then un-shards, un-pads.

  Args:
    wrapped: the function to be wrapped. Signature is `params, *args, *kwargs`.
    static_argnums: indices of arguments to `wrapped` that should _not_ be
      padded and sharded, but instead be forwarded as-is. The default is (0,)
      because by far the most common use-case is to pass `params` first.
    static_argnames: names of kwargs to `wrapped` that should _not_ be padded
      and sharded, but instead be forwarded as-is.
    static_return: whether not to un-shard, and un-pad the return value; static
      return values are typically used with eval steps that compute metrics

  Returns:
    A new function that pads and shards its arguments before passing them to
    the wrapped function, and un-shards and un-pads the returned pytree.

    This is useful for calling a pmap'ed function with inputs that aren't
    divisible by the number of devices. A typical use is:
      @pad_shard_unpad
      @jax.pmap
      def forward(params, x): ...

  Notes:
    The padding is done in host-memory before being passed to the function, and
    the values returned by the function are transferred back to host memory.

    The returned function is augmented with a new keyword-only argument
    `min_device_batch` that, if specified, forces padding inputs to at least
    this size per device. This can be useful to avoid recompiles for the last
    batch and reduce memory fragmentation.

    For more information refer to https://flax.readthedocs.io/en/latest/guides/full_eval.html
  """

  def pad_shard_unpad_wrapper(*args, min_device_batch=None, **kw):
    d = jax.local_device_count()  # d = devices, b = batch
    batch_sizes = set()
    for i, a in enumerate(args):
      if i not in static_argnums:
        batch_sizes |= {t.shape[0] for t in jax.tree_util.tree_leaves(a)}
    for k, v in kw.items():
      if k not in static_argnames:
        batch_sizes |= {t.shape[0] for t in jax.tree_util.tree_leaves(v)}
    assert len(batch_sizes) == 1, f"Inconsistent batch-sizes: {batch_sizes}"
    b = batch_sizes.pop()

    def pad(x):
      _, *shape = x.shape
      db, rest = divmod(b, d)
      if rest:
        x = np.concatenate([x, np.zeros((d - rest, *shape), x.dtype)], axis=0)
        db += 1
      if min_device_batch and db < min_device_batch:
        x = np.concatenate(
            [x, np.zeros((d * (min_device_batch - db), *shape), x.dtype)]
        )
        db = min_device_batch
      return x.reshape(d, db, *shape)

    def maybe_pad(tree, actually_pad=True):
      if not actually_pad:
        return tree  # For call-site convenience below.
      return jax.tree_util.tree_map(pad, tree)

    args = [maybe_pad(a, i not in static_argnums) for i, a in enumerate(args)]
    kw = {k: maybe_pad(v, k not in static_argnames) for k, v in kw.items()}
    out = wrapped(*args, **kw)

    def unpad(x):
      # Transfer back before cutting, to reduce on-device shape diversity.
      return jax.device_get(x).reshape([np.prod(x.shape[:2]), *x.shape[2:]])[:b]

    return out if static_return else jax.tree_util.tree_map(unpad, out)

  return pad_shard_unpad_wrapper


from django.urls import path
from . import views

app_name = "shop"
urlpatterns = [
    path("", views.index, name="index"),
    path("datails/<int:p_id>/", views.details, name="detail"),
    path("cart/", views.cart, name="cart"),
    path("show_cart/", views.show_cart, name="show_cart"),
    path("get_date/", views.get_date, name="get_date"),
]


from django.db import models
from django.utils.translation import gettext_lazy as _
from django.db.models.signals import post_save
from back.signals.mailing_signals import send_mail_on_creation

from django.contrib.auth import get_user_model

FoxUser = get_user_model()

class ClientManager(FoxUser):
    class Position(models.TextChoices):
        safety_manager = "SafeMan", _("Safety Manager")
        security_manager = "SecMan", _("Security Manager")
        safety_officer = "SecOff", _("Security Officer")
        site_owner = "SiteOwn", _("Site Owner")
        work_owner = "WorkOwn", _("Owner of Work")
        security_guards = "SecGrd", _("Security Guards")

    # name = models.CharField(max_length=128, null=True)
    department = models.CharField(max_length=128, null=True)
    position = models.CharField(
        max_length=10,
        choices=Position.choices,
        default=Position.site_owner,
    )

    class Meta:
        verbose_name = "Client Manager"

    @property
    def info(self):
        return {"client_manager_name": self.name}

post_save.connect(send_mail_on_creation, sender=ClientManager)


import csv
import numpy as np
import pandas as pd
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf

#yf.pdr_override()
df_full = pdr.get_data_yahoo("RELIANCE.NS", start="2018-01-01").reset_index()
df_full.to_csv('RELIANCE.NS.csv',index=False)
df_full.head()

dates = []
prices = []

def get_data(filename):
	with open(filename, 'r') as csvfile:
		csvFileReader = csv.reader(csvfile)
		next(csvFileReader)
		next(csvFileReader)
		for row in csvFileReader:
			dates.append(int(row[0].split('-')[2]))
			prices.append(float(row[4]))
	return

def predict_price(dates, prices, x):
	dates = np.reshape(dates,(len(dates), 1))

	clf = RandomForestRegressor()
	gp = GaussianProcessRegressor()
	tr = DecisionTreeRegressor()
	svr_rbf = SVR(kernel= 'rbf', C= 1e3, gamma= 0.1)
	svr_rbf.fit(dates, prices)
	clf.fit(dates, prices)
	gp.fit(dates, prices)
	tr.fit(dates, prices)

	print('Plotting started:')

	#plotting using Support Vector Regressor
	plt.figure()
	plt.scatter(dates, prices, color= 'black', label= 'Data')
	plt.plot(dates, svr_rbf.predict(dates), color= 'red', label= 'RBF model')
	plt.xlabel('Date')
	plt.ylabel('Price')
	plt.legend()

	#plotting using Gaussin process
	plt.figure()
	plt.scatter(dates, prices, color= 'black', label= 'Data')
	plt.plot(dates,gp.predict(dates), color= 'green', label= 'Gaussian Process')
	plt.xlabel('Date')
	plt.ylabel('Price')
	plt.legend()

	#plotting using Decision tree
	plt.figure()
	plt.scatter(dates, prices, color= 'black', label= 'Data')
	plt.plot(dates,tr.predict(dates), color= 'blue', label= 'Decision Tree')
	plt.xlabel('Date')
	plt.ylabel('Price')
	plt.legend()

	#plotting using Random Forest model
	plt.figure()
	plt.scatter(dates, prices, color= 'black', label= 'Data')
	plt.plot(dates,clf.predict(dates), color= 'yellow', label= 'Random Forest Regression')
	plt.xlabel('Date')
	plt.ylabel('Price')
	plt.legend()

	plt.show()

	for i in range(int(x),int(x)+11):
		i=np.array(i)
		i=i.reshape(-1,1)
		print('')
		print('Prediction of date -',int(i),':')
		print('1.')
		print(svr_rbf.predict(i)[0],)
		print('2.',)
		print(tr.predict(i)[0],)
		print('3.',)
		print(gp.predict(i)[0],)
		print('4.',)
		print(clf.predict(i)[0],)

a = np.array(7)
a = a.reshape(-1,1)
get_data('RELIANCE.NS.csv')
print('Imported Data')
predict_price(dates, prices, a)
print('\nOver')


from project import app
from project.model import Eval
from flask import render_template
from flask import request

@app.route('/')
def start(): # Function to render the initial page
    return render_template('main.html')

@app.route('/main',methods=['GET','POST'])
def evaluate(): # Catch the information insert in the webpage and direct to model (rule)
    if request.method == 'POST':
        colection = Eval()
        info = dict()
        info['nome'] = request.form['nome']
        info['email'] = request.form['email']
        info['htmlskill'] = request.form['html_skill']
        info['cssskill'] = request.form['css_skill']
        info['javascriptskill'] = request.form['javascript_skill']
        info['pythonskill'] = request.form['python_skill']
        info['djangoskill'] = request.form['django_skill']
        info['iosskill'] = request.form['ios_skill']
        info['androidskill'] = request.form['android_skill']
        colection.show_string(info)
        return render_template('result.html')
    return render_template('main.html')