import tkinter

class MedalDisplay:
    """
    Creates and manages and canvas to display race results with medals
    """

    def __init__(self, window, results):
        """
        Creates the canvas
        :param window: window to display canvas to
        :param results: unsorted results of race
        """
        self.canvas = tkinter.Canvas(window, width=300, height=100)

        # Loop through the top racers, ensuring we don't display over three
        place = 1
        medals_made = 0
        for time in sorted(results, key=lambda x: (x < 0, x)):
            for racer_name in results[time]:
                if medals_made < 3:
                    self._create_medal(50 + medals_made * 100, racer_name, place)
                    medals_made += 1
            place += len(results[time])

    def _create_medal(self, xpos, name, place):
        """
        Creates and individual medal
        :param xpos: the x position to center all elements of the column on
        :param name: the name of the racer
        :param place: the place of the racer
        """
        self.canvas.create_text(xpos, 25, fill="black", font="Arial 14 bold", text=name)
        self.canvas.create_oval(xpos - 25, 50, xpos + 25, 100, fill=self._place_to_color(place))
        self.canvas.create_text(xpos, 75, fill="black", font="Arial 36 bold", text=str(place))

    @staticmethod
    def _place_to_color(place):
        """
        Returns a color corresponding to a place
        :param place: integer place result
        :return: string representation of a hexadecimal color
        """
        if place == 1:
            return "#FCD535"
        if place == 2:
            return "#AAABB0"
        if place == 3:
            return "#DA8020"

    def get_canvas(self):
        """
        Provides access to the canvas
        :return: the canvas object
        """
        return self.canvas


#!/usr/bin/python3

import socket
import struct
import time

# Communication with the DAC happens over TCP on port 7765
COMMUNICATION_PORT = 7765

# The DAC sends UDP broadcast messages on port 7654
BROADCAST_PORT = 7654

# Commands
COMMAND_PREPARE_STREAM = ord("p")
COMMAND_BEGIN_PLAYBACK = ord("b")
COMMAND_QUEUE_RATE_CHANGE = ord("q")
COMMAND_WRITE_DATA = ord("d")
COMMAND_STOP = ord("s")
COMMAND_EMERGENCY_STOP = 0
COMMAND_CLEAR_ESTOP = ord("c")
COMMAND_PING = ord("?")
COMMAND_UPDATE = ord("u")

# Responses
RESPONSE_ACK = ord("a")
RESPONSE_NAK_FULL = ord("F")
RESPONSE_NAK_INVALID = ord("I")
RESPONSE_NAK_STOP_CONDITION = ord("!")

# Playback states
PLAYBACK_STATE_IDLE = 0
PLAYBACK_STATE_PREPARED = 1
PLAYBACK_STATE_PLAYING = 2

# Light engine states
LE_STATE_READY = 0
LE_STATE_WARMUP = 1
LE_STATE_COOLDOWN = 2
LE_STATE_ESTOP = 3

def pack_point(x, y, r, g, b, i=-1, u1=0, u2=0, flags=0):
    """Pack some color values into a struct dac_point.

    Values must be specified for x, y, r, g, and b. If a value is not
    passed in for the other fields, i will default to max(r, g, b); the
    rest default to zero.
    """

    if i < 0:
        i = max(r, g, b)

    return struct.pack("<HhhHHHHHH", flags, x, y, r, g, b, i, u1, u2)

class ProtocolError(Exception):
    """Exception used when a protocol error is detected."""

    pass

class Status:
    """Represents a status response from the DAC.

    Protocol reference: https://ether-dream.com/protocol.html#status
    """

    def __init__(self, data):
        """Initialize from a chunk of data."""
        (
            self.protocol_version,
            self.light_engine_state,
            self.playback_state,
            self.source,
            self.light_engine_flags,
            self.playback_flags,
            self.source_flags,
            self.fullness,
            self.point_rate,
            self.point_count,
        ) = struct.unpack("<BBBBHHHHII", data)

    def dump(self, prefix=" - "):
        """Dump to a string."""
        lines = [
            "Light engine: state %d, flags 0x%x"
            % (self.light_engine_state, self.light_engine_flags),
            "Playback: state %d, flags 0x%x"
            % (self.playback_state, self.playback_flags),
            "Buffer: %d points" % (self.fullness,),
            "Playback: %d kpps, %d points played" % (self.point_rate, self.point_count),
            "Source: %d, flags 0x%x" % (self.source, self.source_flags),
        ]
        for l in lines:
            print(prefix + l)

class BroadcastPacket:
    """Represents a broadcast packet from the DAC.

    Protocol reference: https://ether-dream.com/protocol.html#broadcast
    """

    def __init__(self, data, ip=None):
        """Initialize from a chunk of data."""
        self.mac = data[:6]
        (
            self.hw_revision,
            self.sw_revision,
            self.buffer_capacity,
            self.max_point_rate,
        ) = struct.unpack("<HHHI", data[6:16])
        self.status = Status(data[16:36])

    def dump(self, prefix=" - "):
        """Dump to a string."""
        lines = [
            "MAC: " + self.macstr(),
            "HW %d, SW %d" % (self.hw_revision, self.sw_revision),
            "Capabilities: max %d points, %d kpps"
            % (self.buffer_capacity, self.max_point_rate),
        ]
        for l in lines:
            print(prefix + l)
        self.status.dump(prefix)

    def macstr(self):
        return "".join("%02x" % c for c in self.mac)

class DAC:
    """A connection to a DAC.

    Protocol reference: https://ether-dream.com/protocol.html
    """

    def __init__(self, host, bp):
        self.macstr = bp.macstr()
        self.firmware_string = "-"
        self.got_broadcast(bp)

        try:
            t1 = time.time()
            self.connect(host)
            t = time.time() - t1
            self.conn_status = "ok (%d ms)" % (t * 500)

            if self.last_broadcast.sw_revision < 2:
                self.firmware_string = "(old)"
            else:
                cmd = struct.pack("<B", ord("v"))
                self.conn.sendall(cmd)
                self.firmware_string = self.read(32).replace(b"\x00", b" ").strip()
        except Exception as e:
            self.conn_status = str(e)
            raise Exception("Could not initialize: " + str(e))

    def got_broadcast(self, bp):
        self.last_broadcast = bp
        self.last_broadcast_time = time.time()

    def read(self, l):
        """Read exactly length bytes from the connection."""
        while l > len(self.buf):
            self.buf += self.conn.recv(4096)

        obuf = self.buf
        self.buf = obuf[l:]
        return obuf[:l]

    def read_response(self, cmd):
        """Read a response from the DAC."""
        data = self.read(22)
        response = data[0]
        cmdR = data[1]
        status = Status(data[2:])

        status.dump()

        if cmdR != cmd:
            raise ProtocolError("expected resp for %r, got %r" % (cmd, cmdR))

        if response != RESPONSE_ACK:
            raise ProtocolError("expected ACK, got %r" % (response,))

        self.last_status = status
        return status

    def connect(self, host, port=COMMUNICATION_PORT):
        """Connect to the DAC over TCP."""
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.settimeout(0.2)
        conn.connect((host, port))
        self.conn = conn
        self.buf = b""

        first_status = self.read_response(COMMAND_PING)
        first_status.dump()

    def begin(self, low_water_mark, point_rate):
        cmd = struct.pack("<BHI", COMMAND_BEGIN_PLAYBACK, low_water_mark, point_rate)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_BEGIN_PLAYBACK)

    def update(self, low_water_mark, point_rate):
        cmd = struct.pack("<BHI", COMMAND_UPDATE, low_water_mark, point_rate)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_UPDATE)

    def encode_point(self, point):
        return pack_point(*point)

    def write(self, points):
        epoints = list(map(self.encode_point, points))
        cmd = struct.pack("<BH", COMMAND_WRITE_DATA, len(epoints))
        self.conn.sendall(cmd + b"".join(epoints))
        return self.read_response(COMMAND_WRITE_DATA)

    def prepare(self):
        cmd = struct.pack("<B", COMMAND_PREPARE_STREAM)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_PREPARE_STREAM)

    def stop(self):
        cmd = struct.pack("<B", COMMAND_STOP)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_STOP)

    def estop(self):
        cmd = struct.pack("<B", COMMAND_EMERGENCY_STOP)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_EMERGENCY_STOP)

    def clear_estop(self):
        cmd = struct.pack("<B", COMMAND_CLEAR_ESTOP)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_CLEAR_ESTOP)

    def ping(self):
        cmd = struct.pack("<B", COMMAND_PING)
        self.conn.sendall(cmd)
        return self.read_response(COMMAND_PING)

    def play_point_stream(self, stream, point_rate=30000, buf_size=1799):
        # First, prepare the stream
        if self.last_status.playback_state == PLAYBACK_STATE_PLAYING:
            raise Exception("Could not play stream. DAC already playing.")
        elif self.last_status.playback_state == PLAYBACK_STATE_IDLE:
            self.prepare()

        started = False

        while True:
            # Determine how many points we can write
            cap = buf_size - self.last_status.fullness
            points = []
            for _ in range(cap):
                try:
                    point = next(stream)
                    points.append(point)
                except StopIteration:
                    break

            if cap < 100:
                time.sleep(0.005)
                cap += 150

            self.write(points)

            if not started:
                self.begin(0, point_rate)
                started = True

def get_dac():
    """Listen for a single broadcast packet from the DAC, and return a new instance of DAC when found."""

    print("Finding DAC...")

    # Create a UDP socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("0.0.0.0", BROADCAST_PORT))

    data, addr = s.recvfrom(1024)
    bp = BroadcastPacket(data)
    print("DAC found. Broadcast packet from %s: " % (addr,))
    bp.dump()

    return DAC(str(addr[0]), bp)


#!/usr/bin/env python3

import collections
import glob
import json
from scipy.stats import spearmanr
import numpy as np
import jezecek.fig_utils
import matplotlib.pyplot as plt
import scipy.stats as st

data = [
    json.loads(x)
    for f in glob.glob("data_matism/collected/*.jsonl")
    for x in open(f, "r").readlines()
]

min_ordering = 1
max_ordering = 0
min_questions = 1
max_questions = 0

def compute_success(line):
    # fix bad data
    if line["task"] == "reading":
        line["task"] = "questions"
    if line["task"] == "questions":
        hits = []
        for answer_k, answer_v in line["answers_extrinsic"].items():
            hits.append(line["task_data"][int(answer_k)]["correct"] == answer_v)
        out = np.average(hits)
    elif line["task"] == "ordering":
        out = perm_distance(line["answers_extrinsic"], line["task_data"]["permutation"])
    return out

def perm_distance(A, B):
    return 1-np.average([abs(a_i-B.index(a))/len(A) for a_i, a in enumerate(A)])

def conf_interval(data):
    return st.t.interval(
        confidence=0.90, df=len(data)-1,
        loc=np.mean(data),
        scale=st.sem(data)
    )

data_aggregate = collections.defaultdict(list)
for line in data:
    line["success"] = compute_success(line)

    # fix bad data
    if line["simplification_type"] == "reading":
        line["simplification_type"] = "questions"

    data_aggregate[line["simplification_type"]].append(line)

    # normalize data
    if line["task"] == "ordering":
        min_ordering = min(min_ordering, line["success"])
        max_ordering = max(max_ordering, line["success"])
    elif line["task"] == "questions":
        min_questions = min(min_questions, line["success"])
        max_questions = max(max_questions, line["success"])

print(min_ordering, max_ordering, min_questions, max_questions)

# aggregation
for line in data:
    if line["task"] == "ordering":
        line["success"] = (line["success"]-min_ordering)/(max_ordering-min_ordering)
    elif line["task"] == "questions":
        line["success"] = (line["success"]-min_questions)/(max_questions-min_questions)

# plotting
plt.figure(figsize=(3.5, 1.5))
succ_questions = []
succ_ordering = []
conf_questions = []
conf_ordering = []

for key, vals in data_aggregate.items():
    conf_questions.append(conf_interval([line["success"] for line in vals if line["task"] == "questions"]))
    val = np.average([line["success"] for line in vals if line["task"] == "questions"])
    succ_questions.append(val)
for key, vals in data_aggregate.items():
    conf_ordering.append(conf_interval([line["success"] for line in vals if line["task"] == "ordering"]))
    val = np.average([line["success"] for line in vals if line["task"] == "ordering"])
    succ_ordering.append(val)

BAR_STYLE = {
    "width": 0.4,
    "linewidth": 2,
    "edgecolor": "black",
}

plt.bar(
    [x-0.2 for x in range(len(succ_questions))],
    succ_questions,
    label="Questions task",
    **BAR_STYLE
)
plt.scatter(
    2*[x-0.2 for x in range(len(succ_questions))],
    [x[0] for x in conf_questions]+[x[1] for x in conf_questions],
    color="black",
    marker="_"
)

plt.bar(
    [x+0.2 for x in range(len(succ_ordering))],
    succ_ordering,
    label="Ordering task",
    **BAR_STYLE
)
plt.scatter(
    2*[x+0.2 for x in range(len(succ_questions))],
    [x[0] for x in conf_ordering]+[x[1] for x in conf_ordering],
    color="black",
    marker="_"
)

plt.ylim(0, 1)

plt.xticks(
    range(len(data_aggregate.keys())),
    list(data_aggregate.keys())
)
plt.xlabel("Text simplification model")
plt.ylabel("Task success")

plt.legend(
    loc=(0, 0),
    edgecolor="black", facecolor="white", fancybox=False,
    framealpha=0.9,
    labelspacing=0.2,
    borderpad=0.2,
)

plt.tight_layout(pad=0)
# plt.savefig("computed/figures/model_aggregate.png", dpi=200)
plt.savefig("computed/figures/model_aggregate.pdf")
plt.show()


#!/usr/bin/env python3

# Cryptopals Set 2 - Challenge 10
# Implement AES CBC
# take ECB function, make it encrypt instead of decrypt, and
# use XOR function to combine

import base64
from fixed_xor import fixed_xor
import aes_ecb
import pkcs7

def encrypt(pt, key, iv):
    assert len(pt) % 16 == 0
    assert len(key) == 16
    assert len(iv) == 16

    ct = bytearray(len(pt) + 16)
    ct[0:16] = iv
    for i in range(0, len(pt), 16):
        ct[i+16:i+32] = aes_ecb.encrypt(fixed_xor(pt[i:i+16], ct[i:i+16]), key)
    return bytes(ct[16:])

def decrypt(ct, key, iv):
    assert len(ct) % 16 == 0
    assert len(key) % 16 == 0
    assert len(iv) == 16
    ivs = iv + ct
    pt_padded = bytearray(len(ct))
    for i in range(0, len(ct), 16):
        pt_padded[i:i+16] = fixed_xor(aes_ecb.decrypt(ct[i:i+16], key), ivs[i:i+16])
    return bytes(pt_padded)

if __name__ == '__main__':
    key = b"YELLOW SUBMARINE"
    iv = bytes(16)

    assert len(encrypt(pkcs7.pad_for_aes(b''), key, iv)) == 16
    assert len(encrypt(pkcs7.pad_for_aes(b'a'*15), key, iv)) == 16
    assert len(encrypt(pkcs7.pad_for_aes(b'a'*16), key, iv)) == 32
    assert pkcs7.unpad(decrypt(encrypt(pkcs7.pad_for_aes(b''), key, iv), key, iv)) == b''
    assert pkcs7.unpad(decrypt(encrypt(pkcs7.pad_for_aes(b'a'*15), key, iv), key, iv)) == b'a'*15
    assert pkcs7.unpad(decrypt(encrypt(pkcs7.pad_for_aes(b'a'*16), key, iv), key, iv)) == b'a'*16

    f = open("challenge-data/10.txt")
    ct_b64 = f.read()
    f.close()
    ct = base64.b64decode(ct_b64)

    res = pkcs7.unpad(decrypt(ct, key, iv))
    # print(res)


import torch
import segmentation_models_pytorch as smp
import numpy as np
import cv2
from torch.utils.data import DataLoader
from torch.utils.data import Dataset as BaseDataset
from time import time
from torch.optim.lr_scheduler import StepLR
from torch.utils.tensorboard import SummaryWriter
from augmentations import get_preprocessing
from augmentations import get_training_augmentation
from augmentations import get_validation_augmentation
from copy import deepcopy
from params import IMG_HEIGHT, IMG_WIDTH, DATASET_TYPE

class SegModel:
    """Segmentation Models wrapper
    """
    def __init__(self,
                 name='Unet',
                 encoder='mobilenet_v2',
                 encoder_weights='imagenet',
                 classes=['road', 'car']):
        # model params
        self.name = name
        self.arch = self.arch_selection_function(name)
        self.encoder = encoder
        self.encoder_weights = encoder_weights
        self.classes = classes
        self.n_classes = 1 if len(self.classes) == 1 else (len(self.classes) + 1)
        self.activation = 'sigmoid' if len(self.classes) == 1 else 'softmax2d'
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        self.model = None
        self.max_train_iou_score = 0
        self.max_val_iou_score = 0
        self.preprocessing_fn = smp.encoders.get_preprocessing_fn(self.encoder, self.encoder_weights)
        # training params
        self.learning_rate = 1e-4
        self.weight_decay = 0.5
        self.batch_size = 8
        self.epochs = 1
        self.time0 = time()
        # self.tb = SummaryWriter(log_dir=f'tb_runs/train_{name}_{encoder}_{time()}') # tensorboard

    @staticmethod
    def arch_selection_function(name):
        if name =='Unet':
            arch = smp.Unet
        elif name =='Linknet':
            arch = smp.Linknet
        elif name =='FPN':
            arch = smp.FPN
        elif name =='PSPNet':
            arch = smp.PSPNet
        elif name =='PAN':
            arch = smp.PAN
        elif name =='DeepLabV3':
            arch = smp.DeepLabV3
        else:
            print('Supported sample selection functions: Unet, Linknet, FPN, PSPNet, PAN, DeepLabV3')
            return None
        return arch

    def create_epoch_runners(self,
                             model,
                             loss,
                             metrics,
                             optimizer,
                             verbose=False):
        # it is a simple loop of iterating over dataloader`s samples
        train_epoch = smp.utils.train.TrainEpoch(
            model=model,
            loss=loss,
            metrics=metrics,
            optimizer=optimizer,
            device=self.device,
            verbose=verbose,
        )
        valid_epoch = smp.utils.train.ValidEpoch(
            model=model,
            loss=loss,
            metrics=metrics,
            device=self.device,
            verbose=verbose,
        )
        return train_epoch, valid_epoch

    def create_datasets(self,
                        train_images_paths,
                        train_masks_paths,
                        valid_images_paths,
                        valid_masks_paths,
                        Dataset=DATASET_TYPE):
        train_dataset = Dataset(
            train_images_paths,
            train_masks_paths,
            augmentation=get_training_augmentation(),
            preprocessing=get_preprocessing(self.preprocessing_fn),
            classes=self.classes,
        )
        valid_dataset = Dataset(
            valid_images_paths,
            valid_masks_paths,
            augmentation=get_validation_augmentation(),
            preprocessing=get_preprocessing(self.preprocessing_fn),
            classes=self.classes,
        )
        return train_dataset, valid_dataset

    def create_model(self):
        self.model = self.arch(encoder_name=self.encoder,
                               encoder_weights=self.encoder_weights,
                               classes=self.n_classes,
                               activation=self.activation)
        return self.model

    def train(self,
              train_images_paths,
              train_masks_paths,
              valid_images_paths,
              valid_masks_paths,
              Dataset=DATASET_TYPE,
              verbose=False):

        if self.model is None:
            print(f'Creating new model: {self.name}_{self.encoder}')
            self.create_model()
        self.model.train()
        # Dice/F1 score - https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
        # IoU/Jaccard score - https://en.wikipedia.org/wiki/Jaccard_index
        loss = smp.utils.losses.DiceLoss()
        metrics = [
            smp.utils.metrics.IoU(threshold=0.5),
        ]
        optimizer = torch.optim.Adam([
            dict(params=self.model.parameters(), lr=self.learning_rate),
        ])
        train_epoch, valid_epoch = self.create_epoch_runners(self.model, loss, metrics, optimizer, verbose=verbose)
        train_dataset, valid_dataset = self.create_datasets(train_images_paths,
                                                            train_masks_paths,
                                                            valid_images_paths,
                                                            valid_masks_paths,
                                                            Dataset=Dataset)
        train_loader = DataLoader(train_dataset, batch_size=self.batch_size, shuffle=True, num_workers=12)
        valid_loader = DataLoader(valid_dataset, batch_size=1, shuffle=False, num_workers=4)
        # LR scheduler
        lr_scheduler = StepLR(optimizer, step_size=1, gamma=self.weight_decay)
        # train loop
        train_max_score = 0; val_max_score = 0
        for i in range(0, self.epochs):
            if verbose:
                print('\nEpoch:', i, 'LR:', lr_scheduler.get_last_lr())
            train_logs = train_epoch.run(train_loader)
            valid_logs = valid_epoch.run(valid_loader)
            # do something (save model, change lr, etc.)
            lr_scheduler.step() # decay lr
            if val_max_score < valid_logs['iou_score']:
                val_max_score = valid_logs['iou_score']
                torch.save(self.model, f'./{self.name}_{self.encoder}_best_model.pth')
                if verbose: print('Model saved!')
            if train_max_score < train_logs['iou_score']:
                train_max_score = train_logs['iou_score']
            # tensorboard logging
            # self.tb.add_scalar('Valid_Dice_Loss vs Time', valid_logs['dice_loss'], time()-self.time0)
            # self.tb.add_scalar('Train_Dice_Loss vs Time', train_logs['dice_loss'], time()-self.time0)
            # self.tb.add_scalar('Valid_IoU vs Time', valid_logs['iou_score'], time()-self.time0)
            # self.tb.add_scalar('Train_IoU vs Time', train_logs['iou_score'], time()-self.time0)
        # update model with the best saved
        self.max_val_iou_score = val_max_score
        self.max_train_iou_score = train_max_score
        self.model = torch.load(f'./{self.name}_{self.encoder}_best_model.pth').train()

    def predict(self, image_paths):
        images = []
        for image_path in image_paths:
            # input preprocessing
            image_raw = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
            image = np.copy(image_raw)
            image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH))
            preprocessing = get_preprocessing(self.preprocessing_fn)
            image = preprocessing(image=image)['image']
            images.append(image)
        # convert to torch tensor and do inference
        x_tensor = torch.tensor(images).to(self.device)
        predictions = self.model.predict(x_tensor)
        return predictions


import pandas as pd
import os
import sys
import numpy as np
from tqdm import tqdm
import argparse
from loguru import logger
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import svm
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import TruncatedSVD
from sklearn.linear_model import SGDClassifier
import scipy.sparse as sp
from gensim.test.utils import common_texts
from gensim.models import KeyedVectors
import seaborn as sns
import matplotlib.pyplot as plt
import re

parser = argparse.ArgumentParser()
parser.add_argument('--path_to_dataset', type=str, default="./dataset")
parser.add_argument('--model', type=str, choices=['svm', 'gb', 'rf', 'sgd'], default='svm')
parser.add_argument('--random', type=int, default=0)
parser.add_argument('--concat_loc', action='store_true')
parser.add_argument('--concat_keyword', action='store_true')
parser.add_argument('--word2vec', action='store_true')
parser.add_argument('--num_classes', type=int, default=3)
parser.add_argument('--val_size', type=float, default=0.2)
args = parser.parse_args()

class Covid19Dataset:
  def __init__(self, data_type="train"):
    if data_type == "train":
      self.dataset, _ = self.__load_dataset("train.csv",
                                            args.val_size)
    elif data_type == "val":
      _, self.dataset = self.__load_dataset("train.csv",
                                            args.val_size)
    else:
      self.dataset, _ = self.__load_dataset("test.csv",
                                            1e-3)

    self.text = self.dataset['text'].tolist()
    self.location = self.dataset['location'].tolist()
    self.keyword = self.dataset['keyword'].tolist()
    self.labels = self.dataset['target'].tolist()

  def __load_dataset(self, path, val_size):
    path_to_file = os.path.join(args.path_to_dataset, path)
    dataset = pd.read_csv(path_to_file)

    dataset = self.__clean(dataset)
    train_dataset, val_dataset = train_test_split(dataset,
                                                  test_size=val_size,
                                                  random_state=args.random)
    return train_dataset, val_dataset

  def __clean(self, df):

    if 'target' in df.columns:
      df.dropna(subset=['target', 'text'], inplace=True)

    df.drop_duplicates(keep="first")
    df['text'] = df['text'].apply(lambda x : str(x).lower())
    return df

class Model:
  def __init__(self):
    if args.model == "gb":
      self.classifier = GradientBoostingClassifier(
          n_estimators=500, max_depth=10, learning_rate=0.1, min_samples_split=10)
    elif args.model == "svm":
      self.classifier = svm.SVC(kernel='rbf')
    elif args.model == "rf":
      self.classifier = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
    elif args.model == "sgd":
      self.classifier = SGDClassifier()
    else:
        raise "Model not supported"
    if args.word2vec:
      self.word_vectors = KeyedVectors.load_word2vec_format('../covid19_classification/GoogleNews-vectors-negative300.bin', binary=True)
    else:
      self.vectorizer = TfidfVectorizer()
      self.vectorizer_concat = TfidfVectorizer()
  def train(self, train_dataset):
    if args.word2vec:
      out = self.__get_vectors(train_dataset.text,
                               train_dataset.location,
                               train_dataset.keyword)
    else:
      out = self.vectorizer.fit_transform(train_dataset.text)

    self.classifier.fit(out, train_dataset.labels)

  def predict(self, test_dataset):
    if args.word2vec:
      out = self.__get_vectors(test_dataset.text,
                               test_dataset.location,
                               test_dataset.keyword)
    else:
      out = self.vectorizer.transform(test_dataset.text)

    return self.classifier.predict(out)

  def __get_vectors__(self, text, location, keyword):
    vectors = []
    for sentence in text:
      vector = np.zeros(300)

      for word in sentence.split():
        if word in self.word_vectors:
          vector += self.word_vectors[word]

      vectors.append(vector)

    vectors = np.stack(vectors, 0)
    if args.concat_loc:
      loc_vectors = []
      for loc in location:
        vector = np.zeros(300)
        for word in sentence.split():
          word = re.sub('[^a-zA-Z]', '', word)
          if word in self.word_vectors:
            vector += self.word_vectors[word]
        loc_vectors.append(vector)
      loc_vectors = np.stack(loc_vectors, 0)

      vectors = np.concatenate([vectors, loc_vectors], 1)
    if args.concat_keyword:
      key_vectors = []
      for loc in keyword:
        vector = np.zeros(300)
        for word in sentence.split():
          word = re.sub('[^a-zA-Z]', '', word)
          if word in self.word_vectors:
            vector += self.word_vectors[word]
        key_vectors.append(vector)
      key_vectors = np.stack(key_vectors, 0)
      #vectors += key_vectors
      vectors = np.concatenate([vectors, key_vectors], 1)

    return vectors

def compute_accuracy(y_val, y_pred, str1):
  accuracy = sum([1 for i in range(len(y_val)) if y_val[i] == y_pred[i]]) / len(y_val)
  print(f"{str1} accuracy: {accuracy}")

def infer(model, val_loader, str1="val"):
  y_val = val_loader.labels
  y_pred = model.predict(val_loader)
  compute_accuracy(y_val, y_pred, str1)

@logger.catch
def main():
  train_dataset = Covid19Dataset(data_type="train")
  val_dataset = Covid19Dataset(data_type="val")
  #test_dataset = Covid19Dataset(data_type="test")

  #model = Model()
  model = bert()
  #model.trainBatch(train_dataset)
  model.train(train_dataset)
  infer(model, train_dataset, "Train")
  infer(model, val_dataset, "Val")
  #infer(model, test_dataset, "Test")

if __name__ == "__main__":
  main()


import yagmail
import os
state = True
i = 0
while (state):
    here = os.path.dirname(os.path.abspath(__file__))
    filename = os.path.join(here, "message"+str(i)+".txt")
    try:
        f = open(filename, "r")
        alias = f.readline().strip() #get alias
        print(alias)
        message = f.readlines() #get message
        message.pop(0) #remove alias from message
        yag = yagmail.SMTP({'username@email.com':alias}, 'password')
        yag.send('receiver@email.com', 'Test bot', message)
        f.close()
        i += 1
    except FileNotFoundError:
        print('File does not exist')
        state = False


from preprocessing import *
from tqdm import tqdm
import argparse
import matplotlib.pyplot as plt
import bar_chart_race as bcr

def from_millis_to_minutes(millis: float) -> float:
    return round(millis / (1000*60), 2)

def from_minutes_to_millis(minutes: float) -> float:
    return (minutes * (1000*60))

def gather_durations_data(df, args):
    data = {}
    if args.decades:
        decades = df.groupby(pd.Grouper(key='date', freq='10YS'))
        for dt, decade in tqdm(decades):
            data[dt.year] = {}
            for duration in durations_millis:
                data[dt.year][f'{from_millis_to_minutes(duration)}'] = (decade.duration > duration).sum()
    else:
        for duration in durations_millis:
            data[from_millis_to_minutes(duration)] = (df.duration > duration).sum()
    return data

def gather_genres_data(df, args):
    df = df[df.genre.notna()]
    df['genre'] = df['genre'].apply(get_genre)
    df = df[df.genre != 'wtf']
    df.genre = df.genre.map(lambda i: genres_acc[i])
    data = {}
    selected_genres = set(args.genres)
    if args.decades:
        decades = df.groupby(pd.Grouper(key='date', freq='10YS'))
        for dt, decade in tqdm(decades):
            data[dt.year] = {}
            if len(args.genres) == 1 and "top" in args.genres[0]:
                top_k = int(args.genres[0].split(' ')[1])
                for genre_name, count in decade.groupby('genre').genre.count().nlargest(top_k).iteritems():
                    genre_name = 'prog. rock' if genre_name == 'progressive rock' else genre_name
                    data[dt.year][genre_name.capitalize()] = count
            else:
                for genre_name, genre in decade.groupby('genre'):
                    if genre_name in selected_genres or 'all' in selected_genres:
                        genre_name = 'prog. rock' if genre_name == 'progressive rock' else genre_name
                        data[dt.year][genre_name.capitalize()] = genre.genre.count()
    else:
        if len(args.genres) == 1 and "top" in args.genres[0]:
            top_k = int(args.genres[0].split(' ')[1])
            for genre_name, count in df.groupby('genre').genre.count().nlargest(top_k).iteritems():
                genre_name = 'prog. rock' if genre_name == 'progressive rock' else genre_name
                data[genre_name.capitalize()] = count
        else:
            for genre_name, genre in df.groupby('genre'):
                if genre_name in selected_genres or 'all' in selected_genres:
                    genre_name = 'prog. rock' if genre_name == 'progressive rock' else genre_name
                    data[genre_name.capitalize()] = genre.genre.count()
    return data

def chart_race_and_exit(args):
    df = pd.read_csv('data/results_genre.csv')
    df = df[df['artist'] != 'Glee Cast']
    df = df[df.genre.notna()]
    df['genre'] = df['genre'].apply(get_genre)
    df = df[df.genre != 'wtf']
    genres_acc_cap = list(map(lambda g: g.capitalize(), genres_acc))
    df.genre = df.genre.map(lambda i: genres_acc_cap[i])
    df.date = pd.to_datetime(df.date, format='%Y-%m-%d')
    df = df[df.date < datetime(year=2015, month=1, day=1)]
    groups = df.groupby(pd.Grouper(key='date', freq='1YS'))
    data = {}
    for date, group in groups:
        counts = group.genre.value_counts()
        counts = counts.to_dict()
        for g in (set(genres_acc_cap) - set(counts.keys())):
            counts[g] = 0.

        data[date] = counts
    data = pd.DataFrame(data).T
    print("Creating chart race and quitting")
    bcr.bar_chart_race(
        df=data,
        n_bars=8,
        filename='./chart_race_genres.gif',
        sort='desc',
        fixed_order=False,
        steps_per_period=20,
        period_length=800,
        period_label={'x': .98, 'y': .3, 'ha': 'right', 'va': 'center'},
        perpendicular_bar_func=None,
        period_fmt="%Y",
        cmap='dark12',
        bar_size=.70,
        scale='log',
        title=args.title
    )
    exit(0)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Plot random stuff duration-related')
    parser.add_argument('-d', '--durations', dest='durations', nargs='+', help='compute number of songs greater than these durations. Format: minutes.seconds')
    parser.add_argument('-g', '--genres', dest='genres', nargs='+', help='plot frequency of specified genres. Use "all" for all genres, use "top k" to select top k genres only')
    parser.add_argument('--chart_race', '-r', dest='chart_race', action='store_true', help='specify to output a bar chart race (only valid when -g is specified)')
    parser.add_argument('--decades', dest='decades', action='store_true', help='specify to group by decades')
    parser.add_argument('--normalize', dest='normalize', action='store_true', help='specify to normalize data')
    parser.add_argument('--decades_on_x', dest='decades_on_x', action='store_true', help='plots decades on x (plots durations on x otherwise)')
    parser.add_argument('--title', dest='title', help='title of the plot figure', default='')
    parser.add_argument('--x_label', dest='x_label', help='x label name', default='')
    parser.add_argument('--y_label', dest='y_label', help='y label name', default='')
    parser.add_argument('--figsize', dest='figsize', help='size of plot figure, eg. 12,10', default='12,10')
    parser.add_argument('--output_path', dest='output_path', help='output path to save plot, eg. decades.png')
    args = parser.parse_args()
    if args.chart_race and not args.genres:
        raise UserWarning("--chart_race specified without --genres. This will be ignored")
    elif args.chart_race and args.genres:
        chart_race_and_exit(args)
    durations_millis = list(map(lambda d: from_minutes_to_millis(float(d)), args.durations))
    df = pd.read_csv('data/results_genre.csv')
    df = df[df['artist'] != 'Glee Cast']
    df = aggregate_sixties_and_2020s(df)
    _, ax = plt.subplots(figsize=tuple(map(int, args.figsize.split(','))))

    if args.genres:
        data = gather_genres_data(df, args)
    else:
        data = gather_durations_data(df, args)

    data = pd.DataFrame(data) if args.decades else pd.DataFrame(data, index=['Frequency'])
    if args.normalize:
        for ind in data.index:
            data.loc[ind] = data.loc[ind] / data.loc[ind].max()
    if args.decades_on_x:
        data.T.plot.bar(title=args.title, xlabel=args.x_label, ylabel=args.y_label, ax=ax, legend=args.decades)
    else:
        data.plot.bar(title=args.title, xlabel=args.x_label, ylabel=args.y_label, ax=ax, legend=args.decades)

    if args.output_path:
        plt.savefig(args.output_path)
    else:
        plt.show()


from .test_hr_payslip import TestPayslipBase

class TestPayrollStructureBase(TestPayslipBase):
    def get_payslip_lines(self, payslip_id):
        """
        Get a dict of payslip lines
        """
        payslip = self.payslip_model.browse(
            self.cr, self.uid, payslip_id, context=self.context)

        return {
            line.code: line.amount
            for line in payslip.details_by_salary_rule_category
        }

    def get_deduction_id(self, ref, module='payroll_canada'):
        """
        Gets a deduction category id
        """
        return self.data_model.get_object_reference(
            self.cr, self.uid, module, ref)[1]

    def get_benefit_id(self, ref, module='payroll_canada'):
        """
        Gets an employee benefit category id
        """
        return self.data_model.get_object_reference(
            self.cr, self.uid, module, ref)[1]

    def setUp(self):
        super(TestPayrollStructureBase, self).setUp()
        self.data_model = self.registry('ir.model.data')
        self.structure_model = self.registry("hr.payroll.structure")
        self.deduction_model = self.registry("hr.deduction.category")
        self.benefit_model = self.registry("hr.employee.benefit.category")
        self.input_model = self.registry("hr.payslip.input")
        self.benefit_rate_model = self.registry("hr.employee.benefit.rate")
        self.benefit_rate_line_model = self.registry(
            "hr.employee.benefit.rate.line")

        self.context = self.user_model.context_get(self.cr, self.uid)
        cr, uid, context = self.cr, self.uid, self.context

        # Create an employee and all his deductions
        self.employee_model.write(cr, uid, [self.employee_id], {
            'deduction_ids': [
                (0, 0, {
                    'category_id': self.get_deduction_id(ded[0]),
                    'date_start': '2014-01-01',
                    'date_end': '2014-12-31',
                    'amount': ded[1],
                }) for ded in [
                    ('rrsp', 15),
                    ('fitf2', 16),
                    ('fitu1', 17),
                    ('fithd', 936),
                    ('fitf1', 988),
                    ('fitk1', 1040),
                    ('fit_k3_char', 1092),
                ]
            ]
        }, context=context)

        # Get the canadian payroll structure
        self.structure_id = self.structure_model.search(
            cr, uid, [('code', '=', 'CA')], context=context)[0]

        self.benefit_rate_ids = [
            self.benefit_rate_model.create(cr, uid, {
                'name': 'Test',
                'category_id': self.get_benefit_id(rate[0]),
                'amount_type': 'percent_gross',
            }, context=context)
            for rate in [
                ('rpp', ),
                ('prpp', ),
                ('vrsp', ),
            ]
        ]

        self.benefit_line_ids = [
            self.benefit_rate_line_model.create(cr, uid, {
                'parent_id': line[0],
                'employee_amount': line[1],
                'employer_amount': line[2],
                'date_start': line[3],
                'date_end': line[4],
            }, context=context)
            for line in [
                (self.benefit_rate_ids[0], 0.1, 0.2,
                    '2014-01-01', '2014-12-31'),
                (self.benefit_rate_ids[1], 0.4, 0.8,
                    '2014-01-01', '2014-12-31'),
                (self.benefit_rate_ids[2], 0.9, 1.8,
                    '2014-01-01', '2014-12-31'),
            ]
        ]

        # Create a contract
        self.contract_model.write(cr, uid, [self.contract_id], {
            'struct_id': self.structure_id,
            'benefit_line_ids': [
                (0, 0, {
                    'category_id': self.get_benefit_id(ben[0]),
                    'date_start': '2014-01-01',
                    'date_end': '2014-12-31',
                    'rate_id': ben[1],
                }) for ben in [
                    ('rpp', self.benefit_rate_ids[0]),
                    ('prpp', self.benefit_rate_ids[1]),
                    ('vrsp', self.benefit_rate_ids[2]),
                ]
            ]
        }, context=context)

        self.payslip_vals = {
            'company_id': self.company_id,
            'employee_id': self.employee_id,
            'contract_id': self.contract_id,
            'date_from': '2014-01-01',
            'date_to': '2014-01-07',
            'date_payment': '2014-01-07',
            'struct_id': self.structure_id,
        }

    def create_worked_days(self, wd_lines):
        cr, uid, context = self.cr, self.uid, self.context

        for wd in wd_lines:
            self.worked_days_model.create(
                cr, uid, {
                    'payslip_id': self.payslip_1_id,
                    'date': wd[0],
                    'activity_id': wd[1],
                    'number_of_hours': wd[2],
                    'hourly_rate': wd[3],
                }, context=context)

    def create_inputs(self, input_lines):
        cr, uid, context = self.cr, self.uid, self.context

        for input_line in input_lines:
            self.input_model.create(
                cr, uid, {
                    'name': 'Test',
                    'payslip_id': self.payslip_1_id,
                    'category_id': input_line[0],
                    'amount': input_line[1],
                }, context=context)

    def compute_payslip(self):
        """ Compute the payslip and return a dict containing the
        payslip lines """
        cr, uid, context = self.cr, self.uid, self.context

        self.payslip_model.compute_sheet(
            cr, uid, [self.payslip_1_id], context=context)

        return self.get_payslip_lines(self.payslip_1_id)

class TestCanadaPayrollStructure(TestPayrollStructureBase):
    """ Test the Canada payroll structure """

    def test_create_payslip_low_wage(self):
        self.payslip_1_id = self.create_payslip(self.payslip_vals)
        self.create_worked_days([
            # (date,  activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.job_activity_id, 40, 20),
        ])
        self.create_inputs([
            (self.ref('payslip_input_bonus'), 30),
            (self.ref('payslip_input_retro_pay'), 70)
        ])

        payslip = self.compute_payslip()

        p = 52
        self.assertEqual(payslip['GROSSP'], 40 * 20)

        self.assertEqual(
            payslip['FIT_I'],
            round(payslip['GROSSP'] * (1 + 0.008 + 0.018), 2))

        self.assertEqual(
            payslip['FIT_I_OTHER_WAGE'],
            payslip['FIT_I'] + payslip['OTHER_WAGE'])

        pension_rate = 0.001 + 0.004 + 0.008 + 0.009 + 0.018

        self.assertEqual(
            payslip['FIT_F'],
            15 + pension_rate * payslip['GROSSP'])
        self.assertEqual(payslip['FIT_F2'], 16)
        self.assertEqual(payslip['FIT_U1'], 17)
        self.assertEqual(payslip['FIT_HD'], 936)
        self.assertEqual(payslip['FIT_F1'], 988)

        self.assertEqual(
            round(payslip['FIT_A']), round(payslip['FIT_I'] * p - (
                payslip['FIT_F'] + 16 + 17) * p - 936 - 988))

        # the k2 tax credit is based on ei and cpp contributions
        self.assertEqual(
            round(payslip['FIT_K2']),
            round((payslip['EI_EE_C'] + payslip['CPP_EE_C']) * p))

        self.assertEqual(
            round(payslip['FIT_T3']),
            round((
                payslip['FIT_A'] - ((20 + 21) * p + payslip['FIT_K2'] + 1127)
            ) * 0.15)
        )

        self.assertEqual(
            round(payslip['FIT_T']),
            round((payslip['FIT_T3'] / p + 100 * 0.15)))

    def test_create_payslip_higher_wage(self):
        self.payslip_1_id = self.create_payslip(self.payslip_vals)
        self.create_worked_days([
            # (date, activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.job_activity_id, 40, 40),
        ])
        self.create_inputs([
            (self.ref('payslip_input_bonus'), 300),
            (self.ref('payslip_input_retro_pay'), 700)
        ])

        payslip = self.compute_payslip()

        p = 52
        self.assertEqual(payslip['GROSSP'], 40 * 40)

        self.assertEqual(
            payslip['FIT_I'],
            round(payslip['GROSSP'] * (1 + 0.008 + 0.018), 2))

        self.assertEqual(
            payslip['FIT_I_OTHER_WAGE'],
            payslip['FIT_I'] + payslip['OTHER_WAGE'])

        pension_rate = 0.001 + 0.004 + 0.008 + 0.009 + 0.018

        self.assertEqual(
            round(payslip['FIT_A']), round(payslip['FIT_I'] * p - (
                pension_rate * payslip['GROSSP'] + 15 + 16 + 17 + 18 + 19) * p)
        )

        # the k2 tax credit is based on ei and cpp contributions
        self.assertEqual(round(payslip['FIT_K2']), round(913.68 + 2425.50))

        self.assertEqual(
            round(payslip['FIT_T3']),
            round((
                payslip['FIT_A'] * 0.22 - 3077 -
                ((20 + 21) * p + payslip['FIT_K2'] + 1127) * 0.15)))

        self.assertEqual(
            round(payslip['FIT_T']),
            round((payslip['FIT_T3'] / p + 1000 * 0.22)))

    def test_create_payslip_end_of_year(self):
        """
        Test the salary rules, when the payslip is at the end
        of the year.
        Need to compute 4 payslips (1 payslip each 3 months)
        and check the results for the last one
        """
        cr, uid, context = self.cr, self.uid, self.context

        # Create 4 payslips
        payslips = {
            ps[0]: self.create_payslip({
                'company_id': self.company_id,
                'employee_id': self.employee_id,
                'contract_id': self.contract_id,
                'date_from': ps[1],
                'date_to': ps[2],
                'date_payment': ps[2],
                'struct_id': self.structure_id,
            }) for ps in [
                (1, '2014-01-01', '2014-03-31'),
                (2, '2014-04-01', '2014-06-30'),
                (3, '2014-07-01', '2014-09-30'),
                (4, '2014-10-01', '2014-12-31'),
            ]
        }

        self.contract_model.write(
            cr, uid, [self.contract_id], {
                'wage': 83200,
                'schedule_pay': 'quarterly',
                'worked_hours_per_pay_period': 520,
            }, context=context)

        # Create the worked_days records
        for wd in [
            # (date_from, date_to, activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.job_activity_id, 520, 40, 1),
            ('2014-04-01', self.job_activity_id, 520, 40, 2),
            ('2014-07-01', self.job_activity_id, 520, 40, 3),
            ('2014-10-01', self.job_activity_id, 520, 40, 4),
        ]:
            self.worked_days_model.create(
                cr, uid, {
                    'date': wd[0],
                    'activity_id': wd[1],
                    'number_of_hours': wd[2],
                    'hourly_rate': wd[3],
                    'payslip_id': payslips[wd[4]],
                }, context=context)

        # Add a bonus and a retroactive pay increase payment to payslip 2
        for input_line in [
            (self.ref('payslip_input_bonus'), 30),
            (self.ref('payslip_input_retro_pay'), 70)
        ]:
            self.input_model.create(
                cr, uid, {
                    'name': 'Test',
                    'payslip_id': payslips[2],
                    'category_id': input_line[0],
                    'amount': input_line[1],
                })

        for payslip in [payslips[1], payslips[2], payslips[3], payslips[4]]:
            self.payslip_model.compute_sheet(
                cr, uid, [payslip],
                context=context)
            self.payslip_model.write(
                cr, uid, [payslip], {
                    'state': 'done',
                }, context=context)

        payslip2 = self.get_payslip_lines(payslips[2])
        payslip4 = self.get_payslip_lines(payslips[4])

        self.assertEqual(payslip4['GROSSP'], 520 * 40)

        taxable_income = payslip4['GROSSP'] * (1 + 0.008 + 0.018)

        # Canada pension plan
        self.assertEqual(payslip4['CPP_EE_C'], 0)
        self.assertEqual(
            round(payslip2['CPP_EE_C']),
            round((taxable_income + 30 + 70 - 3500 / 4) * 0.0495))

        self.assertEqual(payslip4['CPP_ER_C'], 0)
        self.assertEqual(payslip2['CPP_ER_C'], payslip2['CPP_EE_C'])

        # Employment insurance
        self.assertEqual(round(payslip4['EI_EE_C']), 0)

        self.assertEqual(
            round(payslip2['EI_EE_C']),
            round((taxable_income + 30 + 70) * 0.0188))

        self.assertEqual(payslip4['EI_ER_C'], 0)
        self.assertEqual(
            round(payslip2['EI_ER_C']), round(payslip2['EI_EE_C'] * 1.4))

        # RRP
        self.assertEqual(
            payslip4['RPP_EE_C'], payslip4['GROSSP'] * 0.001)

        # the k2 tax credit is based on ei and cpp contributions
        self.assertEqual(payslip4['FIT_K2'], 913.68 + 2425.50)

        self.payslip_model.write(
            cr, uid, payslips.values(),
            {'state': 'draft'},
            context=context)

    def test_create_payslip_wage(self):
        """ Test the salary rules, when the employee is paid
        by wage instead of hourly rates and unpaid worked
        days were computed """
        cr, uid, context = self.cr, self.uid, self.context

        self.contract_model.write(cr, uid, [self.contract_id], {
            'salary_computation_method': 'yearly'
        }, context=context)

        self.payslip_1_id = self.create_payslip(self.payslip_vals)

        # Create the worked_days records including an unpaid day
        self.create_worked_days([
            # (date, activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.unpaid_activity_id, 8, 0),
            ('2014-01-02', self.job_activity_id, 7, 0),
        ])

        payslip = self.compute_payslip()

        self.assertEqual(payslip['GROSSP'], 52000 / 52 * (40 - 8) / 40)

        # Add 28 worked hours to the contract and recompute.
        # The paid worked hours now equal 35 (7 + 28)
        self.create_worked_days([
            # (date, activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.job_activity_id, 28, 0),
        ])

        self.payslip_model.compute_sheet(
            cr, uid, [self.payslip_1_id], context=context)

        payslip = self.get_payslip_lines(self.payslip_1_id)

        self.assertEqual(payslip['GROSSP'], 52000 / 52 * 35 / 40)

        # Add 10 worked hours to the contract and recompute.
        # The paid worked hours now equal 45 (7 + 28 + 10)
        # The employee is paid at full wage
        self.create_worked_days([
            # (date, activity_id, nb_hours, hourly_rate)
            ('2014-01-01', self.job_activity_id, 10, 0),
        ])

        self.payslip_model.compute_sheet(
            cr, uid, [self.payslip_1_id], context=context)

        payslip = self.get_payslip_lines(self.payslip_1_id)

        self.assertEqual(payslip['GROSSP'], 52000 / 52)


import os
from PIL import Image
import click

@click.command()
@click.option('--dir', 'dir', help='Directory of files', required=True)
@click.option('--exten', 'extenction', help='Extenction of files', required=False, default="png")

def zmiana_nazwy(dir:str, extenction:str):
    a=0
    for file_path in os.listdir(dir):
        a+=1
        im = Image.open(f"{dir}/{file_path}")
        rgb_im = im.convert("RGB")
        os.remove(f"{dir}/{file_path}")
        file = file_path.split(".")
        txt =""
        if len(file_path) == 1:
            rgb_im.save(f"{dir}/{file[0]}{extenction}")
            print(f"[{a}/{len(os.listdir(dir))}] Converted {file_path} to {file[0]}{extenction}")
        else:
            for f in range(0,len(file)-1):
                txt +=f"{file[f]}."
            rgb_im.save(f"{dir}/{txt}{extenction}")
            print(f"[{a}/{len(os.listdir(dir))}] Converted {file_path} to {txt}{extenction}")

zmiana_nazwy()


from .pytorch_tools.gradient_reversal import RevGrad
import torch
import os
from processing.models.deep_tl_predictor import DeepTlPredictor
import numpy as np
import torch.nn as nn
from .pytorch_tools.training import fit, predict
from captum.attr import IntegratedGradients

class LSTM(DeepTlPredictor):
    def __init__(self, subject, ph, params, train, valid, test):
        super().__init__(subject, ph, params, train, valid, test)

        self.model = self.LstmModule(self.input_shape, self.params["hidden"], self.params["dropout_weights"],
                                     self.params["dropout_layer"], self.params["domain_adversarial"], self.n_domains)
        self.model.cuda()
        self.loss_func = self._compute_loss_func()
        self.opt = torch.optim.Adam(self.model.parameters(), lr=self.params["lr"], weight_decay=self.params["l2"])

    def fit(self):
        x_train, y_train, t_train = self._str2dataset("train")
        x_valid, y_valid, t_valid = self._str2dataset("valid")
        train_ds = self._to_tensor_ds(x_train, y_train)
        valid_ds = self._to_tensor_ds(x_valid, y_valid)

        fit(self.params["epochs"], self.params["batch_size"], self.model, self.loss_func, self.opt, train_ds, valid_ds,
            self.params["patience"], self.checkpoint_file)

    def predict(self, dataset, clear=False):
        # get the data for which we make the predictions
        x, y, t = self._str2dataset(dataset)
        ds = self._to_tensor_ds(x, y)

        # create the model
        self.model.load_state_dict(torch.load(self.checkpoint_file))

        if self.params["domain_adversarial"]:
            [y_trues_glucose, y_trues_subject], [y_preds_glucose, y_preds_subject] = predict(self.model, ds)
            results = self._format_results_source(y_trues_glucose, y_trues_subject, y_preds_glucose, y_preds_subject, t)
        else:
            y_true, y_pred = predict(self.model, ds)
            results = self._format_results(y_true, y_pred, t)

        if clear:
            self._clear_checkpoint()

        return results

    def save(self, save_file, clear=True):
        no_da_lstm = self.LstmModule(self.input_shape, self.params["hidden"], self.params["dropout_weights"],
                                     self.params["dropout_layer"], False, 0)
        self.model.load_state_dict(torch.load(self.checkpoint_file))
        no_da_lstm.encoder.load_state_dict(self.model.encoder.state_dict())
        no_da_lstm.regressor.load_state_dict(self.model.regressor.state_dict())
        if not os.path.exists(os.path.dirname(save_file)):
            os.makedirs(os.path.dirname(save_file))
        torch.save(no_da_lstm.state_dict(), save_file)
        if clear:
            self._clear_checkpoint()

    def _compute_input_shape(self):
        x_train, _, _ = self._str2dataset("train")
        return x_train.shape[2]

    def extract_features(self, dataset, file):
        x, y, _ = self._str2dataset(dataset)
        self.model.load_state_dict(torch.load(file))
        self.model.eval()
        features = self.model.encoder(torch.Tensor(x).cuda())[0].detach().cpu().numpy()
        features = features.reshape(features.shape[0], -1)

        return [features, y]

    def integrated_gradients(self, dataset, file):
        self.model.load_state_dict(torch.load(file))
        x, y, _ = self._str2dataset(dataset)
        baselines = np.zeros(x.shape)
        names = self.input_names
        for i in range(len(names)):
            if names[i] == "glucose":
                mean = np.mean(y)
                baselines[:, :, i] = mean * np.ones(x.shape[0:2])
            # if names[i] == "mets":
            #     baselines[:, :, i] = mean * np.ones(x.shape[0:2])

        x = torch.Tensor(x).cuda()
        x.requires_grad_()
        baselines = torch.Tensor(baselines).cuda()
        self.model.train()
        ig = IntegratedGradients(self.model)
        attr = []
        for i in range(len(x) // 10):
            attr_i = ig.attribute(x[i * 10:(i + 1) * 10], baselines=baselines[i * 10:(i + 1) * 10], n_steps=50)
            attr.append(attr_i.cpu().detach().numpy())
        attr_i = ig.attribute(x[(i + 1) * 10:len(x)], baselines=baselines[(i + 1) * 10:len(x)], n_steps=50)
        attr.append(attr_i.cpu().detach().numpy())
        attr = np.concatenate(attr, axis=0)
        return attr

    class LstmModule(nn.Module):

        def __init__(self, n_in, neurons, dropout_weights, dropout_layer, domain_adversarial=False, n_domains=1):
            super().__init__()

            self.encoder = nn.LSTM(n_in, neurons[0], len(neurons), dropout=dropout_layer, batch_first=True)
            self.regressor = nn.Linear(neurons[-1], 1)
            self.domain_classifier = nn.Sequential(
                RevGrad(),
                nn.Linear(neurons[-1], n_domains),
                nn.LogSoftmax(dim=1),
            ) if domain_adversarial else None

        def forward(self, xb):
            features, _ = self.encoder(xb)
            prediction = self.regressor(features[:, -1])
            if self.domain_classifier is not None:
                # domain = self.domain_classifier(self.GradReverse.apply(features[:,-1]))
                domain = self.domain_classifier(features[:, -1])
                return prediction.reshape((-1)), domain
            else:
                return prediction.reshape((-1))


import math as M
childs_meal = float(input("What is the price of a child's meal? $"))
adults_meal = float(input("What is the price of an adult's meal? $"))
drinks = float(input("What is the price of the drinks? $"))
appetizers = float(input("What is the price od the appetizers? $"))
number_children = int(input("How many children are there? "))
number_adults = int(input("How many adults are there? "))
sales_tax_rate = float(input("What is the sales tax rate? "))
tip = int(input("How much would you like to leave for a tip? %"))
print('')
print('')
sub_totatl_children = number_children * childs_meal
sub_total_adult = number_adults * adults_meal
sub_total = sub_totatl_children + sub_total_adult + appetizers + ((number_children + number_adults) * drinks)
sales_tax_total = sub_total * sales_tax_rate /100
total_meal1 = sub_total + sales_tax_total
tip_total = tip / 100 * sub_total
total_meal = total_meal1 + tip_total
randon1 = number_children + number_adults * drinks
print(f'Subtotal: ${sub_total}')
print(f"Tip: {tip_total: ,.2f}")
print(f'Sales Tax: ${sales_tax_total}')
print(f'Total: ${total_meal: ,.2f}')
print('')
print('')
payment_amount = int(input('What is the payment amount? $'))
change = payment_amount - total_meal
print(f'Change: ${change: ,.2f}')
print('Have a wonderful day :)')


from sklearn.preprocessing import Binarizer

X = [
    [ 1., -1.,  2.],
    [ 2.,  0.,  0.],
    [ 0.,  1., -1.]
]

bn = Binarizer(threshold=0.0) #threshold = 임계치 / Binarizer는 0보다 크면 1 작으면 0 --> 임계치 조정해주는 함수
fit = bn.fit(X)  # 0.0보다 큰지 작은지 비교
print(fit)
trans = fit.transform(X)
fit_trans = bn.fit_transform(X)
print(fit_trans)


from __future__ import print_function, division, absolute_import
from fontTools import ttLib
from collections import OrderedDict
import argparse
import re

__version__ = '0.1'

uniname_RE = re.compile(r"""((uni)|(u))              # match "uni" or "u"
                            ((?(2)([0-9A-F]{4}))     # if "uni" match 4 digits
                             (?(3)([0-9A-F]{4,6})))  # if "u" match 4-6 digits
                            (?!.)""", re.X)          # don't match anythg else

def codepoint_from_uniname(name):
    m = re.match(uniname_RE, name)
    if m:
        codepoint_hex = m.group(4)
        return int(codepoint_hex, 16)
    else:
        raise ValueError("invalid uniname: '%s'" % name)

def parse_options(args):
    parser = argparse.ArgumentParser(
        description="Use GlyphOrderAndAliasDB to rename glyphs in TTF/OTF")
    parser.add_argument('goadb', metavar='GOADB.txt',
                        type=argparse.FileType('r'), help='input GOADB file.')
    parser.add_argument('infile', metavar='INPUT',
                        help='input font file.')
    parser.add_argument('outfile', metavar='OUTPUT', nargs='?',
                        help='output font file (default: overwrite input '
                        'file).')
    parser.add_argument('--version', action='version', version=__version__)
    options = parser.parse_args(args)
    if not options.outfile:
        # careful: this is going to overwrite the input file!
        options.outfile = options.infile
    return options

class GoadbManager(object):

    def __init__(self, goadbpath, fontpath):
        self.names, self.mapping = self.parse_goadb(goadbpath)
        self.font = ttLib.TTFont(fontpath, recalcBBoxes=False,
                                 recalcTimestamp=False)

    @staticmethod
    def parse_goadb(goadbfile):
        try:
            goadb = goadbfile.read()
        except AttributeError:
            with open(goadbfile, 'r') as f:
                goadb = f.read()
        names = OrderedDict()
        mapping = {}
        for i, line in enumerate(goadb.splitlines()):
            line = re.sub(r"#.*", "", line).strip()
            if not line:
                continue
            items = line.split()
            n = len(items)
            assert len(items) in (2, 3)
            if n == 2:
                final, friendly = items
            elif n == 3:
                final, friendly, uniname = items
                code = codepoint_from_uniname(uniname)
                if code not in mapping:
                    mapping[code] = final
                else:
                    raise AssertionError('uniname redefined: %s' % uniname)
            else:
                raise AssertionError(
                    "GOADB must have either 2 or 3 items per entry: "
                    "line %d: %s" % line)
            names[friendly] = final
        return names, mapping

    def rename_glyphs(self):
        # Do the renaming in newnames
        newnames = []
        for gname in self.font.getGlyphOrder():
            if gname in self.names:
                newnames.append(self.names[gname])
            else:
                newnames.append(gname)
        # must reload TTFont again
        font = ttLib.TTFont(self.font.reader.file.name)
        # set glyph order as newnames then load post table
        font.setGlyphOrder(newnames)
        font['post']
        self.font = font

    def save(self, outpath):
        self.font.save(outpath, reorderTables=False)

    def close(self):
        self.font.close()

def main(args=None):
    options = parse_options(args)
    manager = GoadbManager(options.goadb, options.infile)
    manager.rename_glyphs()
    manager.save(options.outfile)
    manager.close()

if __name__ == '__main__':
    main()


import math
import jittor as jt
from jittor import Module

class dualAttentionWrapper(Module):
    def __init__(self, hidden_size, input_size, field_size):
        self.hidden_size = hidden_size
        self.input_size = input_size
        self.field_size = field_size

        k = math.sqrt(1 / hidden_size)
        self.Wh = jt.init.uniform((input_size, hidden_size), 'float32', -k, k)
        self.bh = jt.init.uniform((hidden_size), 'float32', -k, k)
        self.Ws = jt.init.uniform((input_size, hidden_size), 'float32', -k, k)
        self.bs = jt.init.uniform((hidden_size), 'float32', -k, k)
        self.Wo = jt.init.uniform((2*input_size, hidden_size), 'float32', -k, k)
        self.bo = jt.init.uniform((hidden_size), 'float32', -k, k)
        self.Wf = jt.init.uniform((field_size, hidden_size), 'float32', -k, k)
        self.bf = jt.init.uniform((hidden_size), 'float32', -k, k)
        self.Wr = jt.init.uniform((input_size, hidden_size), 'float32', -k, k)
        self.br = jt.init.uniform((hidden_size), 'float32', -k, k)

    def execute(self, x, hs, fds, finished=0):
        hs = jt.transpose(hs, [1, 0, 2])
        hs2d = jt.reshape(hs, [-1, self.input_size])
        phi_hs2d = jt.tanh(jt.nn.matmul(hs2d, self.Wh) + self.bh)
        phi_hs = jt.reshape(phi_hs2d, hs.shape)

        fds = jt.transpose(fds, [1, 0, 2])
        fds2d = jt.reshape(fds, [-1, self.field_size])
        phi_fds2d = jt.tanh(jt.nn.matmul(fds2d, self.Wf) + self.bf)
        phi_fds = jt.reshape(phi_fds2d, hs.shape)

        gamma_h = jt.tanh(jt.nn.matmul(x, self.Ws) + self.bs)
        weights = jt.reduce_add(phi_hs * gamma_h, 2, True)
        weights = jt.exp(weights - jt.max(weights, 0, True))
        weights = jt.divide(weights, 1e-6 + jt.reduce_add(weights, 0, True))

        alpha_h = jt.tanh(jt.nn.matmul(x, self.Wr) + self.br)
        fd_weights = jt.reduce_add(phi_fds * alpha_h, 2, True)
        fd_weights = jt.exp(fd_weights - jt.max(fd_weights, 0, True))
        fd_weights = jt.divide(fd_weights, 1e-6 + jt.reduce_add(fd_weights, 0, True))

        weights = jt.divide(weights * fd_weights, 1e-6 + jt.reduce_add(weights * fd_weights, 0, True))

        context = jt.reduce_add(hs * weights, 0)  # batch * input_size
        out = jt.tanh(jt.nn.matmul(jt.concat([context, x], -1), self.Wo) + self.bo)
        out[jt.where(finished)[0]] = 0

        return out, weights


#!/usr/bin/env python

from time import sleep
import urwid as uw
from urwid_timed_progress import TimedProgressBar

# Demo of bar adjusting to reasonable units
# The demo simulates progress at different rates to show
# the progress bar changing units as needed.
if __name__ == '__main__':

    palette = [
        ('normal',   'white', 'black', 'standout'),
        ('complete', 'white', 'dark magenta'),
    ]

    # Using SI units: https://en.wikipedia.org/wiki/Kilobyte
    units = [
        ('bytes', 1),
        ('kB', 1000),
        ('MB', 1000000)
    ]

    bar = TimedProgressBar('normal', 'complete', done=5e7, units=units)
    status = uw.Text('simulation that demonstrates auto selecting best units')
    footer = uw.Text('q to exit, r to run simulation')
    progress = uw.Frame(uw.ListBox([bar, uw.Divider(bottom=5), status]),
                        footer=footer)

    def run():
        bar.reset()

        # start with slow rate of progress
        status.set_text('starting out, running slowly')
        for i in range(5):
            bar.add_progress(1)
            loop.draw_screen()
            sleep(.5)
        for i in range(50):
            bar.add_progress(200)
            loop.draw_screen()
            sleep(.1)

        # run fast until done
        status.set_text('running fast until done')
        while bar.current < bar.done:
            bar.add_progress(100000)
            loop.draw_screen()
            sleep(.01)

        status.set_text('done')
        loop.draw_screen()

    def keypress(key):
        if key == 'q':
            raise uw.ExitMainLoop()
        elif key == 'r':
            run()

    loop = uw.MainLoop(progress, palette, unhandled_input=keypress)
    loop.run()


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

from bs4 import BeautifulSoup

from core.crawler import CrawlerBase
from utils.common import format_list
from utils.tomongo import insert
from utils import req
from utils.common import set_data_md5

class SpiderBase(CrawlerBase):
    desc = "spider_guanjia"
    auth = "fairy"
    thread = 1

    def get_url_list(self):
        url_list = []
        page = 1
        flag = 0
        while flag == 0:
            url = "https://www.freebuf.com/author/腾讯电脑管家?page={}".format(
                str(page))
            res = req.get(url)
            if not res:
                continue

            resp_text = res.replace('<br />', '\n')
            if '该用户还没发表文章!' in resp_text:
                break
            soup = BeautifulSoup(resp_text, 'html5lib')
            articles = soup.select('dd')
            for article in articles:
                if article.find_all('a'):
                    title = article.find_all('a')[0].getText()
                    href = article.find_all('a')[0].get('href')
                    time = article.find_all('div', 'time')[0].getText()
                    if href:
                        url_list.append((title, href, time))
                    else:
                        flag = 1
                        break
            page += 1

        return url_list

    def get_info(self, target):
        url = target[1]
        result = []
        resp_text = req.get(url)
        if not resp_text:
            return

        soup = BeautifulSoup(resp_text, 'html5lib')
        content = soup.select_one(
            "#getWidth > div.article-wrap.panel.panel-default").getText()

        if "IOCs" in content and "参考" in content:
            for ioc in format_list(content[content.find('IOCs'):content.rfind('参考')].split('\n')):
                ioc['reference'] = url
                ioc['name'] = target[0]
                ioc['disclosuretime'] = target[2]
                result.append(ioc)
        else:
            for ioc in format_list(content.split('\n')):
                ioc['reference'] = url
                ioc['name'] = target[0]
                ioc['disclosuretime'] = target[2]
                result.append(ioc)

        ioc_info = set_data_md5(result)
        insert(ioc_info)


import math
def isprime(startnumber):
    startnumber2=1.0 * startnumber
    prime = True
    for divisor in range(2, startnumber):
        if startnumber2/divisor==int(startnumber2/divisor):
            prime=False
    return prime
if __name__ == "__main__":
	pstr = input("p:")
	qstr = input("q:")
	dstr = input("d:")
	p = int(pstr)
	q = int(qstr)
	d = int(dstr)
	n = p * q
	z = (p-1) * (q-1)
	e = 1
	for i in range(2, 1000):
		if isprime(i):
			if ((d * i) % z) == 1:
				e = i
				break
	print(e)
	#print((d*e) % z)


import numpy as np
import cv2
from cv2 import CV_32F
import warnings

def FPE(out, ):
    warnings.filterwarnings(action='ignore')
    # out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
    [v, h, d] = np.shape(out)
    # print('--- Mask generation stage ---')
    mask = (v + h)*np.ones((v, h))
    mask[0,0] = 2*v*h
    mask[0,1:] = (v*h + v)
    mask[1:,0] = (v*h + h)
    mask1 = mask[0,0]/mask
    # print('--- FFT stage ---')
    fft_out = np.ones([v,h,d],dtype = 'complex_')
    fft_out2 = np.ones([v,h,d])
    fft_out[:,:,0]=np.fft.fft2(out[:,:,0])*mask1
    fft_out[:,:,1]=np.fft.fft2(out[:,:,1])*mask1
    fft_out[:,:,2]=np.fft.fft2(out[:,:,2])*mask1

    # print('--- Restoration stage ---')
    fft_out2[:,:,0]=np.fft.ifft2(fft_out[:,:,0])
    fft_out2[:,:,1]=np.fft.ifft2(fft_out[:,:,1])
    fft_out2[:,:,2]=np.fft.ifft2(fft_out[:,:,2])
    if np.min(fft_out2) < 0:
        fft_out2 = fft_out2 - np.min(fft_out2)

    fft_out2 = (fft_out2/np.max(fft_out2))
    return fft_out2

if __name__ == "__main__":
    img = cv2.imread("F:/_project_/_matlab/_source/fog1.jpg")
    cv2.imshow("original", img)
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    fft_out2 = FPE(img)
    cv2.imshow("fpimg", fft_out2)
    cv2.waitKey()
    cv2.destroyAllWindows()


from app import app
from flask import request, render_template, jsonify, make_response

@app.route("/")
def index():
    return 'HELLO'

@app.route('/upload-video', methods=['GET', 'POST'])
def upload_video():

    print(request.method)

    if request.method == 'POST':

        filesize = request.cookies.get('filesize')
        file = request.files["file"]

        print(f"Filesize: {filesize}")
        print(file)

        res = make_response(jsonify({"message": f"{file.filename} uploaded"}), 200)

        return res

    return render_template('public/upload_video.html')


'''
Created on Aug 10, 2011

@author: mkiyer
'''
import logging
import argparse
import sys
import collections
import subprocess

# local imports
import pysam
from bx.intervals.intersection import Interval, IntervalTree

# project imports
from oncoseq.rnaseq.lib.base import DNA_reverse_complement, split_seq

def parse_gtf_exons(f):
    for line in f:
        fields = line.strip().split('\t')
        feature_type = fields[2]
        if feature_type != "exon":
            continue
        chrom = fields[0]
        start = int(fields[3])
        end = int(fields[4])
        strand = fields[6]
        yield chrom, start, end, strand

def build_exon_trees(gtf_file):
    # find all unique exons first
    chrom_exon_dict = collections.defaultdict(lambda: set())
    for chrom,start,end,strand in parse_gtf_exons(open(gtf_file)):
        chrom_exon_dict[chrom].add((start, end, strand))
    # now add exons to interval tree
    trees = collections.defaultdict(lambda: IntervalTree())
    for chrom, exons in chrom_exon_dict.iteritems():
        for start,end,strand in exons:
            trees[chrom].insert_interval(Interval(start, end, strand=strand))
    return trees

def find_unambiguous_exon_intervals(gtf_file):
    """
    returns (chrom, start, end, strand) tuples for exon
    intervals that are unique and have no overlapping
    transcripts or exons.
    """
    trees = build_exon_trees(gtf_file)
    for chrom,start,end,strand in parse_gtf_exons(open(gtf_file)):
        hits = set((hit.start, hit.end, hit.strand) for hit in trees[chrom].find(start, end))
        hits.add((start, end, strand))
        if len(hits) == 1:
            yield chrom, start, end, strand

def create_fragment_size_index(gtf_file, genome_fasta_file, index_name,
                               min_interval_size,
                               bowtie_build_bin):
    """
    make an alignment index containing sequences that can be used to
    assess the fragment size distribution.  these sequences must be
    larger than the 'max_insert_size' in order to be viable for use
    in characterizing the fragment size distribution.
    """
    # find all exons that are larger than the maximum estimated fragment size
    exons = set(coord for coord in find_unambiguous_exon_intervals(gtf_file)
                if (coord[2] - coord[1]) >= min_interval_size)
    logging.info("Found %d exons larger than %d" % (len(exons), min_interval_size))
    # extract the nucleotide sequence of the exons
    logging.info("Extracting sequences to use for estimating the fragment "
                 "size distribution")
    ref_fa = pysam.Fastafile(genome_fasta_file)
    frag_size_fasta_file = index_name + ".fa"
    f = open(frag_size_fasta_file, 'w')
    for chrom, start, end, strand in exons:
        seq = ref_fa.fetch(chrom, start, end)
        if not seq:
            logging.warning("exon %s:%d-%d not found in reference" % (chrom, start, end))
            continue
        if len(seq) < (end - start):
            logging.warning("could not fetch full length seq for %s:%d-%d" % (chrom, start, end))
            continue
        # make fasta record
        if strand == '-':
            seq = DNA_reverse_complement(seq)
        # break seq onto multiple lines
        seqlines = split_seq(seq)
        record = (">%s:%d-%d_%s\n%s" %
                  (chrom, start, end, strand, seqlines))
        print >>f, record
    f.close()
    ref_fa.close()
    # build bowtie alignment index from the fragment size exons
    logging.info("Building bowtie index")
    args = [bowtie_build_bin, frag_size_fasta_file, index_name]
    return subprocess.call(args)

def main():
    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    parser = argparse.ArgumentParser()
    parser.add_argument("--bowtie-build-bin", dest="bowtie_build_bin",
                        default="bowtie-build")
    parser.add_argument("--min-interval-size", dest="min_interval_size",
                        type=int, default=1000)
    parser.add_argument("gtf_file")
    parser.add_argument("genome_fasta_file")
    parser.add_argument("index_name")
    args = parser.parse_args()
    return create_fragment_size_index(args.gtf_file, args.genome_fasta_file,
                                      args.index_name,
                                      min_interval_size=args.min_interval_size,
                                      bowtie_build_bin=args.bowtie_build_bin)

if __name__ == '__main__':
    sys.exit(main())


# Importáljuk a math csomagot
import math

# Bekérjük a sugár értékét
r = int(input("Add meg a kör sugarát: "))

# Kiszámojuk az értékeket és 2 tízedesjegyre kerekítjük az értékeket
k = round((2 * r * math.pi), 2)
t = round((r ** 2 * math.pi), 2)

# Kiíratjuk a konzolra
print(f"A kör kerülete {k}, a területe {t}")


n = int(input("Entre com um número inteiro: "))

ultimo = 0
j=1
for i in range(1,n+1):
	count = 0
	for j in range(1,i+1):
		if i%j == 0:
			count += 1

	if count == 2:
		ultimo = i
print(ultimo)


from django.db import models
from core.categorias.models import Categoria

class Produto(models.Model):
    nome = models.CharField(
        'Nome',
        max_length=128
    )
    valor = models.DecimalField(
        'Valor',
        max_digits=10,
        decimal_places=2,
        blank=True,
        null=True
    )
    categoria = models.ForeignKey(Categoria, models.SET_NULL, null=True)

    def __str__(self):
        return '%s' % self.nome


from django.views import generic
from holiday_manager import models, forms
from holiday_manager.utils import redirect_to_referer
from django.forms.models import inlineformset_factory, modelformset_factory
from invites import forms as invite_forms
from invites.models import User
from holiday_manager.views import LoginRequiredViewMixin
from holiday_manager.views.base import ProjectViewMixin
from django.core.urlresolvers import reverse
from django.db import transaction
from django.shortcuts import redirect
from django.contrib import messages
from paypal.standard.forms import PayPalPaymentsForm
from holiday_manager.google_calendar import GoogleCalendarApi, calendar_choices
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth import login, get_backends
from social_auth.db.django_models import UserSocialAuth
import itertools
from django.http import HttpResponse

# Debug views
class LoginAs(ProjectViewMixin, generic.View):

    def get(self, request, **kwargs):
        user = User.objects.get(pk=kwargs['pk'])
        backend = get_backends()[0]
        user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
        login(request, user)
        return redirect(reverse('app:dashboard', kwargs={'project': self.curr_project.slug}))

# User management

class UserList(ProjectViewMixin, generic.ListView):
    #model = models.ApprovalGroup
    model = User
    template_name = 'holiday_manager/user_list.html'
    main_section = 'users'

    def get_context_data(self, **kwargs):
        context = super(UserList, self).get_context_data(**kwargs)

        groups = []
        context['object_list'] = sorted(list(context['object_list']), key=lambda x: x.approval_group.pk if x.approval_group else None)
        all_groups = models.ApprovalGroup.objects.filter(project=self.curr_project)
        populated_groups = []

        for group, users in itertools.groupby(context['object_list'], lambda x: x.approval_group):
            if group:
                populated_groups.append(group)
                groups.append((group, sorted(users, key=lambda x: x.last_name)))

        empty_groups = [group for group in all_groups if group not in populated_groups]

        context['object_list'] = groups
        context.update({
            'filterform': self.filterform,
            'empty_groups': empty_groups
        })
        return context

    def get_queryset(self):
        queryset = super(UserList, self).get_queryset()
        #queryset = queryset.prefetch_related('user_set__approval_group')
        queryset = queryset.select_related('approval_group')
        self.filterform = forms.UsersFilterForm(self.request.GET)
        if self.filterform.is_valid():
            queryset = self.filterform.filter(queryset)

        return queryset

class EditUser(ProjectViewMixin, generic.UpdateView):
    model = User
    form_class = invite_forms.EditUserForm
    template_name = 'edit_user.html'
    main_section = 'users'

    def get_success_url(self):
        return reverse('app:user_edit', kwargs={'pk': self.object.pk, 'project': self.curr_project.slug})

class ViewUser(ProjectViewMixin, generic.DetailView):
    model = User
    template_name = 'holiday_manager/view_user.html'
    main_section = 'users'

    def get_context_data(self, **kwargs):
        context = super(ViewUser, self).get_context_data(**kwargs)
        context.update({
            'last_requests': self.object.holidayrequest_set.all()[:5]
        })
        return context

class DeleteUser(ProjectViewMixin, generic.DeleteView):
    model = User

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        messages.warning(request, 'User "%s" deleted.' % self.object)
        return redirect_to_referer(self.request)

# Holiday Requests

class EditHolidayRequest(ProjectViewMixin, generic.UpdateView):
    model = models.HolidayRequest

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.save()
        return redirect_to_referer(self.request)

# Approval groups

class CreateApprovalGroup(ProjectViewMixin, generic.CreateView):
    model = models.ApprovalGroup
    object = None

    def get_context_data(self, **kwargs):
        context = super(CreateApprovalGroup, self).get_context_data(**kwargs)
        formset = kwargs.get('formset') or self.get_formset()

        # TODO: refactor this creating a ProjectFormsetMixin maybe, which sets curr project even for empty form
        empty_form = formset.empty_form
        empty_form.set_project(self.curr_project)
        context.update({
            'create': True,
            'formset': formset,
            'empty_form': empty_form # Small hack: each time this is called in the template, the form is recreated, so we can't set the project!
        })
        return context

    def get(self, request, *args, **kwargs):
        form_class = forms.ApprovalGroupForm
        form = self.get_form(form_class)
        return self.render_to_response(self.get_context_data(form=form))

    def formset_kwargs(self):
        kwargs = {
            'formset': forms.ApprovalRulesFormset,
            'form': forms.ApprovalRuleForm,
            'extra': 2
        }
        return kwargs

    def get_formset(self):
        formset_args = self.formset_kwargs()
        RuleFormSet = inlineformset_factory(models.ApprovalGroup, models.ApprovalRule, **formset_args)
        form_data = self.request.POST if self.request.method == 'POST' else None
        formset = RuleFormSet(data=form_data, instance=self.object)
        for subform in formset.forms:
            subform.set_project(self.curr_project)
        return formset

    @transaction.commit_on_success
    def post(self, request, *args, **kwargs):
        form_class = forms.ApprovalGroupForm
        form = self.get_form(form_class)
        formset = self.get_formset()
        if form.is_valid() and formset.is_valid():
            form.instance.project = self.curr_project
            self.object = form.save()
            formset.instance = self.object
            formset.save()
            messages.success(self.request, "Changes saved successfully")
            return redirect(reverse('app:group_edit', kwargs={'project': self.curr_project.slug, 'pk': self.object.pk}))
        else:
            return self.form_invalid(form=form, formset=formset)

    def form_invalid(self, **kwargs):
        return self.render_to_response(self.get_context_data(**kwargs))

class UpdateApprovalGroup(CreateApprovalGroup):

    def get_context_data(self, **kwargs):
        context = super(UpdateApprovalGroup, self).get_context_data(**kwargs)
        context.update({
            'create': False,
        })
        return context

    def formset_kwargs(self):
        kwargs = super(UpdateApprovalGroup, self).formset_kwargs();
        kwargs.update({
            'extra': 0
        })
        return kwargs

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(UpdateApprovalGroup, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(UpdateApprovalGroup, self).post(request, *args, **kwargs)

class ListApprovalGroup(ProjectViewMixin, generic.ListView):
    model = models.ApprovalGroup

class DeleteApprovalGroup(ProjectViewMixin, generic.DeleteView):
    model = models.ApprovalGroup

    def get_success_url(self):
        return reverse('group_list')

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.remove()
        messages.warning(request, 'The group "%s" has been deleted. All its member are now inside the default group.' % self.object)
        # TODO: no need to render here
        return HttpResponse('Deleted')
        #return redirect(self.get_success_url())

# Project settings

class EditProjectSettings(ProjectViewMixin, generic.UpdateView):
    model = models.Project
    form_class = forms.EditProjectSettingsForm
    template_name = 'holiday_manager/project_settings.html'
    main_section = 'settings'
    active_section = 'general'

    def get_form(self, form_class):
        form = super(EditProjectSettings, self).get_form(form_class)
        try:
            social_user = self.request.user.social_auth.get(provider='google-oauth2')
            access_token = social_user.extra_data['access_token']
            gapi = GoogleCalendarApi(api_key=settings.GOOGLE_API_KEY, access_token=access_token, auth_user=social_user)
            calendars = gapi.list_calendars()
            form.fields['google_calendar_id'].widget.choices = [('', "Don't use Google Calendar")] + calendar_choices(calendars)
        except UserSocialAuth.DoesNotExist:
            pass
        return form

    def get_success_url(self):
        return reverse('app:project_settings', kwargs={'project': self.curr_project.slug})

    def get_object(self, queryset=None):
        return models.Project.objects.get(pk=self.curr_project.pk)

class EditProjectClosures(ProjectViewMixin, generic.UpdateView):
    model = models.Project
    form_class = forms.EditProjectClosuresForm
    template_name = 'holiday_manager/project_closures.html'
    main_section = 'settings'
    active_section = 'closures'
    object = None

    @transaction.commit_on_success
    def post(self, request, *args, **kwargs):
        formset = self.get_formset()
        form = self.get_form(self.form_class)
        if form.is_valid() and formset.is_valid():
            self.object = form.save()
            formset.instance = self.object
            formset.save()
            messages.success(self.request, "Changes saved successfully")
            return redirect(reverse('app:project_closures', kwargs={'project': self.curr_project.slug}))
        else:
            return self.form_invalid(formset=formset, form=form)

    def form_invalid(self, **kwargs):
        return self.render_to_response(self.get_context_data(**kwargs))

    def get_context_data(self, **kwargs):
        context = super(EditProjectClosures, self).get_context_data(**kwargs)
        formset = kwargs.get('formset') or self.get_formset()
        # TODO: refactor this creating a ProjectFormsetMixin maybe, which sets curr project even for empty form
        empty_form = formset.empty_form
        empty_form.set_project(self.curr_project)
        context.update({
            'formset': formset,
            'empty_form': empty_form # Small hack: each time this is called in the template, the form is recreated, so we can't set the project!
        })
        return context

    def formset_kwargs(self):
        kwargs = {
            #'formset': forms.ApprovalRulesFormset,
            'form': forms.ClosurePeriodForm,
            'extra': 1
        }
        return kwargs

    def get_formset(self):
        self.object = self.curr_project
        form_data = self.request.POST if self.request.method == 'POST' else None
        formset_args = self.formset_kwargs()
        formset_class = inlineformset_factory(models.Project, models.ClosurePeriod, **formset_args)
        formset = formset_class(data=form_data, instance=self.object)
        for subform in formset.forms:
            subform.set_project(self.curr_project)
        return formset

    def get_success_url(self):
        return reverse('app:project_closures', kwargs={'project': self.curr_project.slug})

    def get_object(self, queryset=None):
        return self.curr_project

class UpgradePlan(ProjectViewMixin, generic.TemplateView):
    template_name = 'holiday_manager/upgrade_plan.html'

    def get_context_data(self, **kwargs):
        paypal_dict = {
            "cmd": "_xclick-subscriptions",
            "business": "h1_1352217439_biz@gmail.com",
            "a3": self.curr_project.calculate_price(),                      # monthly price
            "p3": 1,                           # duration of each unit (depends on unit)
            "t3": "M",                         # duration unit ("M for Month")
            "src": "1",                        # make payments recur
            "sra": "1",                        # reattempt payment on payment error
            "no_note": "1",                    # remove extra notes (optional)
            "item_name": "Holiday manager subscription",
            "notify_url": self.request.build_absolute_uri(reverse('paypal:paypal-ipn')),
            "return_url": self.request.build_absolute_uri(reverse('home')),
            "cancel_return": self.request.build_absolute_uri(reverse('home')),
            'custom': 'some custom data'
        }
        # Create the instance.
        form = PayPalPaymentsForm(initial=paypal_dict, button_type="subscribe")
        return {
            'paypal_form': form
        }


# GRAFY ZE SPRAWOZDANIA ZAPISANE W POSTACI LISTY KRAWEDZI
graf1 = [['A', 'B'],
         ['A', 'C'],
         ['B', 'D'],
         ['B', 'E'],
         ['C', 'B'],
         ['D', 'E'],
         ['E', 'F'],
         ['F', 'G'],
         ['G', 'H']
         ]

graf2 = [['A', 'B'],
         ['A', 'C'],
         ['A', 'I'],
         ['B', 'D'],
         ['B', 'E'],
         ['B', 'I'],
         ['C', 'B'],
         ['C', 'I'],
         ['D', 'E'],
         ['D', 'I'],
         ['E', 'F'],
         ['E', 'I'],
         ['F', 'G'],
         ['F', 'I'],
         ['G', 'H'],
         ['G', 'I'],
         ['H', 'I']
         ]

graf3 = [['A', 'B'],
         ['A', 'C'],
         ['A', 'I'],
         ['A', 'J'],
         ['B', 'D'],
         ['B', 'E'],
         ['B', 'I'],
         ['B', 'J'],
         ['C', 'B'],
         ['C', 'I'],
         ['C', 'J'],
         ['D', 'E'],
         ['D', 'I'],
         ['D', 'J'],
         ['E', 'F'],
         ['E', 'I'],
         ['E', 'J'],
         ['F', 'G'],
         ['F', 'I'],
         ['F', 'J'],
         ['G', 'H'],
         ['G', 'I'],
         ['G', 'J'],
         ['H', 'I'],
         ['H', 'J'],
         ['I', 'J'],
         ['J', 'I']
         ]

graf4 = [['A', 'B'],
         ['A', 'C'],
         ['A', 'I'],
         ['B', 'D'],
         ['B', 'E'],
         ['B', 'I'],
         ['C', 'B'],
         ['C', 'I'],
         ['D', 'E'],
         ['D', 'I'],
         ['E', 'F'],
         ['E', 'I'],
         ['F', 'G'],
         ['F', 'I'],
         ['G', 'H'],
         ['G', 'I'],
         ['H', 'I'],
         ['J', 'J'],
         # ['H', 'J']
         ]

graf5 = [['A', 'B'],
         ['A', 'C'],
         ['B', 'D'],
         ['B', 'E'],
         ['B', 'A'],
         ['C', 'B'],
         ['D', 'E'],
         ['E', 'F'],
         ['F', 'G'],
         ['G', 'H'],
         ['A', 'A'],
         ['H', 'H'],
         ]

# FUNKCJE DO KONKRETNYCH ZADAŃ

def zadanie1(graph: list):
    """Funkcja wypisująca wszystkich sąsiadów dla każdego wierzchołka"""

    neighbors = []
    # Dla krawędzi w liście krawędzi
    for e in graph:
        help_list = []  # Pomocnicza lista w której będe zapisywać sąsiadów wierzchołka
        v1 = e[0]       # Biorę wierzchołek z 'lewej' strony krawędzi
        for e1 in graph:
            # Jeśli wierzchołek zawiera się w krawędziach po lewej stronie dodaj go.
            if v1 == e1[0]:
                help_list.append(e1[1])

        # Dodaj tekst do listy wszystkich sąsiadów jeśli się nie powtarza
        text = [f"Wierzchołek {v1} ma sąsiadów:", help_list]
        if text not in neighbors:
            neighbors.append([f"Wierzchołek {v1} ma sąsiadów:", help_list])

    # Wypisanie sąsiadów
    for neighbor in neighbors:
        print(neighbor)

    return neighbors

def count_vertices(graph: list):
    """Funkcja liczy ilość wierzchołków w liście krawędzi"""
    all_v = []
    for e in graph:
        v1 = e[0]
        v2 = e[1]
        if v1 not in all_v:
            all_v.append(v1)
        if v2 not in all_v:
            all_v.append(v2)

    return len(all_v)

def zadanie2(graph: list):
    """Funkcja wypisująca wszyskie wierzchołki, które są sąsiadami każdego wierzchołka"""

    e_count = len(graph)             # Ilość krawędzi listy
    v_count = count_vertices(graph) - 1  # Ilość wierzchołków aby spełnic warunek, że jest sąsiadem każdego wierzchołka
    list = []

    # Dla krawędzi w grafie
    for e in graph:
        temp_list = []    # Tworze pomocniczą listę
        v1 = e[1]         # Biore wierzchołek z prawej strony listy krawędzi
        # Dla kolejnej krawędzi
        for e1 in graph:
            # Jeśli krawędź e1 i jej prawa strona (wierzchołek) jest równy aktualnemu v1
            if e1[1] == v1:
                # Jeśli wierzchołek z lewej strony listy krawedzi nie zawiera sie w pomocniczej liscie
                if e1[0] not in temp_list:
                    # Dodaje go
                    temp_list.append(e1[0])

        # Jeśli liczba wierzchołków jest równa wymaganej liczbie wierzchołków
        if len(temp_list) == v_count:
            text = [f"Wierzchołek {v1} ma sąsiadów", temp_list]
            # I jeśli nie występuje już w ogólnej liście to ją dodaj
            if text not in list:
                list.append(text)

    for i in list:
        print(i)

def zadanie3(graph):
    """Funkcja pokazujaca wszystkie stopnie wychodzące wszystkich wierzchołków"""

    return_list = []

    for e in graph:
        v1 = e[0]
        help_list = []
        for e1 in graph:
            # Jeśli wierzchołek 'v1' zawiera się w elemencie listy krawędzi (0 miejsec) to je dodaj
            if v1 in e1[0]:
                help_list.append([e1[0], e1[1]])

        # Pomiń duplikaty
        text = [f"Stopnie wychodzące dla {v1}:", help_list]
        if text not in return_list:
            return_list.append([f"Stopnie wychodzące dla {v1}:", help_list])

    for i in return_list:
        print(i)

def zadanie4(graph):
    """Funkcja wypisujaca wszystkie stopnie wchodzace do wierzcholków"""
    return_list = []

    for e in graph:
        # Bierzemy drugi wierzcholek z listy krawedzi
        v1 = e[1]
        help_list = []
        for e1 in graph:
            # Jesli w elemencie listy krawedzi na drugim miejscu zawiera sie ten sam wierzchołek to go dodaj.
            if v1 in e1[1]:
                help_list.append([e1[0], e1[1]])
        # Pomiń duplikaty
        text = [f"Stopnie wchodzące do wierzchołka {v1}:", help_list]
        if text not in return_list:
            return_list.append([f"Stopnie wchodzące do wierzchołka {v1}:", help_list])

    for i in return_list:
        print(i)

def zadanie5(graph):
    """Funkcja wypisująca wierzchołki izolowane.
        (Czyli w sumie takie które tworzą pętle z samym sobą,
        oraz nie są sąsiadami i nie mają sąsiadów)"""

    return_list = []

    for e in graph:
        help_list = []
        v1 = e[0]
        v2 = e[1]
        # Sprawdzamy czy dwa wierzchołki są takie same w tym elemencie listy
        if v1 == v2:
            # Wchodzimy w petle ktora sprawdzi czy nie ma innych krawedzi,
            # które zawierają wierzchołek
            for e1 in graph:
                if e1[0] == v1 or e1[1] == v1 is False:
                    help_list.append(e)
                else:
                    help_list = []
        # Jeśli lista nie jest pusta to ją dodaj do listy zwracanej.
        if help_list:
            return_list.append(help_list)

    for i in return_list:
        print(f"Wierzchołki izolowane to:", i[0][0])

def zadanie6(graph):
    """
    Funkcja wypisująca wszystkie pętle
    """

    # PRÓBA WYPISANIA CYKLU W GRAFIE.
    # edges_len = len(graph)
    #
    # for e in graph:
    #     print("Dla krawędzi", e)
    #     # DLa np ['A', 'B'] bierzemy wartość 'B' i
    #     # musimy wziąc wszystkie krawędzie które kierują sie do 'B' i będziemy szukać czy dązą do 'A'
    #     v = e[1]
    #     temp_list = []
    #     for e1 in graph:
    #         if e1[1] == v:
    #             # Tworze pomocniczą listę krawedzi wchodzacych do 'B'
    #             temp_list.append(e1)
    #
    #     for e1 in temp_list:
    #         v1 = e1[0]
    #         v2 = e1[1]
    #         if e1 == e:
    #             temp_list.remove(e1)
    #         else:
    #             boolean = True
    #             while boolean:
    #                 for e2 in graph:
    #                     if v in e2[1]:
    #                         return_list.append([e2, e1, e])
    #                     if v1 in e2[1]:
    #
    #
        #
        # for e1 in temp_list:
        #     if
        # print(temp_list)

    # # https://networkx.org/documentation/stable/_modules/networkx/algorithms/cycles.html
    # import networkx as nx
    #
    # G = nx.DiGraph(graph)
    #
    # for cycle in nx.simple_cycles(G):
    #     print(f"Pętla {cycle}")

    # PĘTLA TO CO INNEGO NIŻ CYKL - ODNOTOWANO :D
    return_list = []

    for e in graph:
        if e[0] == e[1]:
            return_list.append(e)

    for i in return_list:
        print(f"Pętle", i)

def zadanie7(graph):
    """Funkcja pokazujaca wszystkie krawedzie dwukierunkowe"""

    all_edges = []

    for e in graph:
        er = [e[1], e[0]]  # 'er' - 'edge reversed' - Krawedź odwrotna
        # Jeśli odwrócona krawedź znajduje się w liscie krawedzi to ją dodaj
        if er in graph:
            if not er[0] == er[1]:
                all_edges.append(f"Krawędź {e} i {er}")

    for i in all_edges:
        print(i)

if __name__ == '__main__':
    print("Zadanie 1")
    zadanie1(graf1)
    print("Zadanie 2")
    zadanie2(graf2)
    print("Zadanie 3")
    zadanie3(graf2)
    print("Zadanie 4")
    zadanie4(graf2)
    print("Zadanie 5")
    zadanie5(graf4)
    print("Zadanie 6")
    zadanie6(graf5)
    print("Zadanie 7")
    zadanie7(graf5)