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

import cv2
import numpy as np
import torch
import matplotlib.pyplot as plt
from PIL import Image
from diffusers.utils import load_image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel

"""
使用ControlNet增强图像生成的稳定性
先用opencv获取头部轮廓，然后把图片换成明星脸

Canny，边缘检测
Open Pose，捕捉人体的动作
Scribble，以一个简单的简笔画为基础，生成精美的图片
HED Boundary，外一种边缘检测算法
Depth，深度估计，也就是对一张图片的前后深度估计出来的轮廓图
Normal Map，法线贴图，通常在游戏中用得比较多，可以在不增加模型复杂性的情况下，提升细节效果
Semantic Segmentation，语义分割图，可以把图片划分成不同的区域模块
M-LSD，这个能够获取图片中的直线段，很适合用来给建筑物或者房间内的布局描绘轮廓，这个算法也常常被用在自动驾驶里面
"""

def get_canny_image(original_image, low_threshold=100, high_threshold=200):
  image = np.array(original_image)

  image = cv2.Canny(image, low_threshold, high_threshold)
  image = image[:, :, None]
  image = np.concatenate([image, image, image], axis=2)
  canny_image = Image.fromarray(image)
  return canny_image

def display_images(image1, image2):
  # Combine the images horizontally
  combined_image = Image.new('RGB', (image1.width + image2.width, max(image1.height, image2.height)))
  combined_image.paste(image1, (0, 0))
  combined_image.paste(image2, (image1.width, 0))
  # Display the combined image
  plt.imshow(combined_image)
  plt.axis('off')
  plt.show()

def draw_image_grids(images, rows, cols):
  # Create a rows x cols grid for displaying the images
  fig, axes = plt.subplots(2, 2, figsize=(10, 10))

  for row in range(rows):
    for col in range(cols):
      axes[row, col].imshow(images[col + row * cols])
  for ax in axes.flatten():
      ax.axis('off')
  # Display the grid
  plt.show()

if __name__ == '__main__':
    # 使用opencv获取“戴珍珠耳环的少女”中的头像
    image_file = "data/input_image_vermeer.png"
    original_image = load_image(image_file)
    canny_image = get_canny_image(original_image)
    display_images(original_image, canny_image)

    # 加载模型
    controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
    pipe = StableDiffusionControlNetPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
    )

    # 显存不够用时，将模型从显存中移除
    pipe.enable_model_cpu_offload()
    # 通过xformers加速推理
    pipe.enable_xformers_memory_efficient_attention()

    # 同时生成四张图片，根据四位女星
    prompt = ", best quality, extremely detailed"
    prompt = [t + prompt for t in ["Audrey Hepburn", "Elizabeth Taylor", "Scarlett Johansson", "Taylor Swift"]]
    generator = [torch.Generator(device="cpu").manual_seed(42) for i in range(len(prompt))]

    # 计算并展示生成的四张图片
    output = pipe(
        prompt,
        canny_image,
        negative_prompt=["monochrome, lowres, bad anatomy, worst quality, low quality"] * 4,
        num_inference_steps=20,
        generator=generator,
    )
    draw_image_grids(output.images, 2, 2)


x = 250
y = 30
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
import pygame
import time
import random

pygame.init()
##############
crash_sound = pygame.mixer.Sound("crash.wav")
##############
display_w = 800
display_h = 600

background = pygame.image.load('picture4.png')

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
yellow = (255, 255, 0)
green = (0,128,0)
car_width = 73
Display = pygame.display.set_mode((display_w,display_h))
pygame.display.set_caption('Car game')
clock= pygame.time.Clock()
carImg = pygame.image.load("racecar1.png")
pause = False

def quitgame():
    pygame.quit()
    quit()
def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x  and y + h > mouse[1] > y:
        pygame.draw.rect(Display, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(Display, ic, (x, y, w, h))
    smalltext = pygame.font.SysFont("comicsansms",20)
    TextSurf, TextRect = text_objects(msg, smalltext)
    TextRect.center = ((x + (w / 2)), (y + (h / 2)))
    Display.blit(TextSurf, TextRect)

def unpause():
    pygame.mixer.music.unpause()
    global pause
    pause = False
def paused():

    pygame.mixer.music.pause()
    largetext = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Paused", largetext)
    TextRect.center = ((display_w/2), (display_h/2))
    Display.blit(TextSurf, TextRect)
    while pause:
        print(pause)
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("continue", 150,450,100,50, green, blue, unpause)
        button("exit", 550,450,100,50, red, blue, quitgame)

        pygame.display.update()
        clock.tick(15)

def game_intro(text):
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        Display.blit(background,(0,0))
        large_text = pygame.font.Font('freesansbold.ttf', 115)
        TextSurf, TextRect = text_objects(text, large_text)
        TextRect.center = ((display_w / 2), (display_h / 2))
        Display.blit(TextSurf, TextRect)

        pygame.draw.rect(Display, green,(150,450,100,50))
        smalltext = pygame.font.Font('freesansbold.ttf', 20)
        TextSurf, TextRect = text_objects('Go!', smalltext)
        TextRect.center = ((150+ (100/ 2)), (450+(50/ 2)))
        Display.blit(TextSurf, TextRect)

        pygame.draw.rect(Display, red, (550, 450, 100, 50))

        smalltext = pygame.font.Font('freesansbold.ttf', 20)
        TextSurf, TextRect = text_objects('Exit!', smalltext)
        TextRect.center = ((550+ (100/ 2)), (450+(50/ 2)))
        Display.blit(TextSurf, TextRect)

        button("Go!", 150, 450, 100, 50, green, blue, game_loop)
        button("Exit!", 550, 450, 100, 50, red, blue, quitgame)

        pygame.display.update()
        clock.tick(100)
        #time.sleep(3)
        #intro = False

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("dodged: "+ str(count), True, white)
    Display.blit(text,(0,0))

def thing(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(Display, color, [thingx, thingy, thingw, thingh])

def car(x,y):
    Display.blit(carImg, (x,y))
def text_objects(text, font):
    text_surface = font.render(text,True,white)
    return text_surface, text_surface.get_rect()
def message_display(text):
    large_text = pygame.font.Font('freesansbold.ttf', 115)
    TextSurf, TextRect =text_objects(text,large_text)
    TextRect.center=((display_w/2),(display_h/2))
    Display.blit(TextSurf,TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()
def crash():

    pygame.mixer.Sound.play(crash_sound)
    pygame.mixer.music.stop()

    #message_display('You crashed')
    large_text = pygame.font.SysFont('comicsansms', 115)
    TextSurf, TextRect = text_objects('You crashed!', large_text)
    TextRect.center = ((display_w / 2), (display_h / 2))
    Display.blit(TextSurf, TextRect)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        button("Play Again", 150, 450, 100, 50, green, blue, game_loop)
        button("Exit!", 550, 450, 100, 50, red, blue, quitgame)

        pygame.display.update()
        clock.tick(15)

def game_loop():
    global pause

    pygame.mixer.music.load('background.wav')
    pygame.mixer.music.play(-1)

    x = display_w*0.45
    y = display_h*0.8
    x_change = 0
    car_speed = 0
    GameExit = False

    thing_startx = random.randrange(0, display_w)
    thing_starty = -600
    thing_speed = 7
    thing_width =  100
    thing_height = 100

    dodged = 0
    #crashed=False
    while not GameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type== pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5
                if event.key == pygame.K_ESCAPE:
                    pause = True
                    print("paused")
                    paused()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                    x_change = 0
        x += x_change

        Display.blit(background,(0,0))
        ###################
        thing(thing_startx, thing_starty, thing_width, thing_height, red)
        thing_starty += thing_speed
        car(x,y)
        things_dodged(dodged)
        if x > display_w - car_width or x < 0:
            crash()

        if thing_starty > display_h:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_w)
            dodged += 1
            if dodged >= 2 and dodged < 12:
                thing_speed += 1
            if dodged >= 10:
                thing_width = 100
            thing_width += (dodged*1.2)

        if y < thing_starty + thing_height:
            print('ycrossover')

            if x > thing_startx and x < thing_startx + thing_width or x + \
                car_width > thing_startx and x + car_width < thing_startx + thing_width:
                print('xcrossover')
                crash()
        pygame.display.update()
        clock.tick(60)
game_intro("Racey Game")
game_loop()
pygame.quit()
quit()


import torch
from graph_based_sampling import topological_sort, deterministic_eval
import distributions
from tqdm import tqdm

optim_dist = {
    'normal': distributions.Normal,
    'bernoulli': distributions.Bernoulli,
    'discrete': distributions.Categorical,
    'gamma': distributions.Gamma,
    'dirichlet': distributions.Dirichlet,
    'uniform-continuous': lambda low, high: distributions.Gamma((low+high)/2, torch.tensor(1.0))
}

class BBVI:
    def __init__(self, graph, lr=1e-3):
        self.graph = graph[1]
        self.expr = graph[2]
        self.X = [v for v in self.graph['V'] if v not in self.graph['Y']]
        self.graph['Y'] = {k: torch.tensor(float(v)) for k, v in self.graph['Y'].items()}
        self.topo_order = topological_sort(self.graph['V'], self.graph['A'])
        self.optimizer = torch.optim.Adam([torch.tensor(0.0)], lr=lr)
        self.init_Q()   # initialize proposal distributions
        self.optimizer.zero_grad()

    def init_Q(self):
        self.Q = {}
        vars = {**{x: torch.tensor(0.0) for x in self.X}, **self.graph['Y']}
        for v in self.topo_order:
            if v in self.X:
                dist_type = self.graph['P'][v][1][0]
                dist_param = [deterministic_eval(e, vars) for e in self.graph['P'][v][1][1:]]
                self.Q[v] = optim_dist[dist_type](*dist_param).make_copy_with_grads()
                self.optimizer.param_groups[0]['params'] += self.Q[v].Parameters()
                vars[v] = deterministic_eval(self.graph['P'][v], vars)

    def bbvi_eval(self, sigma, vars):
        for v in self.topo_order:
            d = deterministic_eval(self.graph['P'][v][1], vars)
            if v in self.X:
                # sample evaluation
                with torch.no_grad():
                    c = self.Q[v].sample()
                log_prob_q = self.Q[v].log_prob(c)
                log_prob_q.backward()
                sigma['grads'][v] = [p.grad.clone().detach() for p in self.Q[v].Parameters()]
                sigma['logW'] += (d.log_prob(c.detach()) - log_prob_q.clone().detach())
                vars[v] = c.clone().detach()
            else:
                # observe evaluation
                vars[v] = self.graph['Y'][v]
                sigma['logW'] += d.log_prob(vars[v]).clone().detach()

        return deterministic_eval(self.expr, vars), sigma

    def optimizer_step(self, elbo_grads):
        for v in self.X:
            for d, param in enumerate(self.Q[v].Parameters()):
                param.grad = -elbo_grads[v][d]

        self.optimizer.step()
        self.optimizer.zero_grad()

    def elbo_gradients(self, grads, log_weights):
        elbo_grads = {v: [] for v in self.X}
        L = len(grads)
        for v in self.X:
            D = len(grads[0][v])
            param_to_G = {d: [] for d in range(D)}  # map from param d to L gradients w.r.t param d
            param_to_F = {d: [] for d in range(D)}
            for l in range(L):
                for d, param in enumerate(grads[l][v]):
                    param_to_G[d].append(param)
                    param_to_F[d].append(param*log_weights[l])

            for d in range(D):
                # convert to tensor of size (L, K) where K is the length of param d
                if param_to_G[d][0].dim() == 0:
                    param_to_G[d] = torch.tensor(param_to_G[d]).unsqueeze(1)
                    param_to_F[d] = torch.tensor(param_to_F[d]).unsqueeze(1)
                else:
                    param_to_G[d] = torch.stack(param_to_G[d], dim=0)
                    param_to_F[d] = torch.stack(param_to_F[d], dim=0)

                K = param_to_G[d].size()[1]     # length of parameter d
                # computing baseline
                cov_sum, var_sum = torch.tensor(0.0), torch.tensor(0.0)
                for k in range(K):
                    FG_mat = torch.stack((param_to_F[d][:, k], param_to_G[d][:, k]), dim=0)
                    cov_mat = torch.cov(FG_mat, correction=0)
                    cov_sum += cov_mat[0, 1]
                    var_sum += cov_mat[1, 1]

                g = torch.sum(param_to_F[d] - cov_sum/var_sum * param_to_G[d], dim=0) / L
                elbo_grads[v].append(g.squeeze(0) if K == 1 else g)

        return elbo_grads

    def bbvi(self, T, L):
        rets = []
        log_weights = []
        grads = []
        elbos = []
        pbar = tqdm(range(T))
        for t in pbar:
            for l in range(L):
                sigma = {'logW': torch.tensor(0.0), 'grads': {}}
                vars = {}
                r, sigma = self.bbvi_eval(sigma, vars)
                self.optimizer.zero_grad()
                grads.append(sigma['grads'])
                log_weights.append(sigma['logW'])
                rets.append(r)

            elbos.append(sum(log_weights[-L:])/L)
            elbo_grads = self.elbo_gradients(grads[-L:], log_weights[-L:])
            self.optimizer_step(elbo_grads)
            pbar.set_postfix({'ELBO': elbos[-1]})

        return rets, log_weights, elbos

    def sample(self, T, L):
        return self.bbvi(T, L)


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

from odoo import models, fields, api, _
from odoo.tools import UserError

from datetime import datetime, timedelta

class HrEmployeeSch(models.Model):
    _inherit = 'hr.employee'

    schedule_ids = fields.One2many('specialist.schedule', 'specialist_id', 'Specialist Schedule Planning')

class Schedules(models.Model):
    _name = 'specialist.schedule'
    _description = 'Specialist Schedule'

    specialist_id = fields.Many2one('hr.employee', string="Especialista", required=True)
    start_date = fields.Datetime('Fecha y Hora de Inicio')
    date_delay = fields.Float('Duración')

    @api.constrains('date_delay')
    def range_duration(self):
        for rec in self:
            if rec.date_delay < 4 or rec.date_delay > 8:
                raise UserError(_("La duración del horario no puede ser menor de 4 o mayor de 8 horas."))

    @api.model_create_multi
    def create(self, vals):
        records = super(Schedules, self).create(vals)
        for r in records:
            fmt = "%Y-%m-%d %H:%M:%S"
            date_end = (datetime.strptime(str(r.start_date), fmt) + timedelta(hours=r.date_delay))
            vdate = datetime.strptime(str(r.start_date), fmt).date()
            vals_list = {
                'appoint_employ_id': r.specialist_id.id,
                'date_start': r.start_date,
                'duration': r.date_delay,
                'date_end': date_end,
                'appoint_date': vdate,
                'sch_id': r.id,
            }
            self.env['cits.admin'].create(vals_list)
        return records

    @api.multi
    def write(self, vals):
        record = super(Schedules, self).write(vals)
        fmt = "%Y-%m-%d %H:%M:%S"
        date_end = (datetime.strptime(str(self.start_date), fmt) + timedelta(hours=self.date_delay))
        vdate = datetime.strptime(str(self.start_date), fmt).date()
        vals_list = {
            'date_start': self.start_date,
            'duration': self.date_delay,
            'date_end': date_end,
            'appoint_date': vdate,
        }
        self.env['cits.admin'].sudo().search([('sch_id', '=', self.id)]).write(vals_list)
        return record

    @api.multi
    def unlink(self):
        res = super(Schedules, self).unlink()
        self.env['cits.admin'].sudo().search([('sch_id', '=', self.id)]).unlink()
        return res


import os
import importlib
import sys
import logging
import configparser

from .config import get_config
from ..utils import get_checkpoints
from ..trainer.tf_trainer_config import get_config as get_train_config

logger = logging.getLogger(__name__)

def get_base_project(path):
    if not os.path.exists(path):
        return get_base_project('.')

    dirname = os.path.abspath(os.path.dirname(path))
    while dirname != '/':
        try:
            subdirs = os.listdir(dirname)
        except (IOError, OSError):
            break
        if '.project' in subdirs:
            return dirname
        dirname = os.path.dirname(dirname)
    return None

def get_class(name, subtype, project, config):
    # Get class name
    split = name.split(':')
    if len(split) == 1:
        name = split[0]
        klass_name = subtype
    else:
        name = split[0]
        klass_name = split[1]

    # Check if name is path to file
    abspath = os.path.abspath(name)
    if os.path.exists(abspath):
        name, ext = os.path.splitext(abspath)
        if ext != '.py':
            msg = '{} is being loaded from a non-python file: {}'
            msg = msg.format(subtype.title(), abspath)
            raise IOError(msg)

        klass = extract_class(name, klass_name)
    else:
        if project is None:
            msg = 'No path to {subtype} file :{file}: was given'
            msg += ' and training is not being done inside'
            msg += ' a project.'
            msg = msg.format(subtype=subtype, file=name)
            raise IOError(msg)

        if subtype == 'architecture':
            subdir = config['structure']['architectures_dir']
        elif subtype == 'loss':
            subdir = config['structure']['losses_dir']
        elif subtype == 'dataset':
            subdir = config['structure']['datasets_dir']
        elif subtype == 'metric':
            subdir = config['structure']['metrics_dir']

        path = os.path.join(project, subdir, name)
        klass = extract_class(path, klass_name)

    return klass

def extract_class(path, name):
    basename = os.path.basename(path)
    abspath = os.path.dirname(os.path.abspath(path))
    sys.path.insert(0, abspath)
    module = importlib.import_module(basename)
    try:
        klass = getattr(module, name)
    except AttributeError:
        msg = 'Python module {} does not have a class named {}'
        msg = msg.format(os.path.basename(path), name)
        raise AttributeError(msg)
    return klass

def get_all_models():
    project = get_base_project('.')

    # Get configuration
    config_path = None
    if project is not None:
        config_path = os.path.join(
            project, '.project', 'axon_config.ini')
    else:
        return []
    config = get_config(path=config_path)
    models_dir = config['structure']['models_dir']
    return os.listdir(models_dir)

def get_model_path(name, project, config):
    models_dir = config['structure']['models_dir']
    return os.path.join(project, models_dir, name)

def load_model(name=None, path=None):
    if (name is None) and (path is None):
        raise ValueError('Name or path must be supplied')

    if path is None:
        project = get_base_project('.')

    if name is None:
        name = os.path.basename(path)

    config_path = os.path.join(
        project, '.project', 'axon_config.ini')
    config = get_config(path=config_path)

    if path is None:
        path = os.path.join(
            project, config['structure']['models_dir'], name)

    model_file = config['configurations']['model_specs']
    model_config = configparser.ConfigParser()
    model_config.read([
        os.path.join(project, '.project', model_file),
        os.path.join(path, model_file)])

    architecture_name = model_config['model']['architecture']

    # Read classes
    model = get_class(
        architecture_name,
        'architecture',
        project,
        config)()

    project_train_config_path = os.path.join(
        project,
        '.project',
        config['configurations']['train_configs'])
    train_config_path = os.path.join(
        path,
        config['configurations']['train_configs'])
    train_config = get_train_config(
        paths=[project_train_config_path, train_config_path]).config
    tf_subdir = train_config['checkpoints']['tensorflow_checkpoints_dir']
    npy_subdir = train_config['checkpoints']['numpy_checkpoints_dir']
    ckpt = get_checkpoints(
        path,
        tf_subdir=tf_subdir,
        npy_subdir=npy_subdir)

    if ckpt is not None:
        ckpt_type, ckpt_path, ckpt = ckpt
        model.ckpt_type = ckpt_type
        model.ckpt_path = ckpt_path

    return model


import logging
import sqlite3
from datetime import datetime
from pathlib import Path
from sqlite3 import Error, Connection
from typing import List, Optional, Any, Tuple, Dict

from app.model.image_data import ImageData
from app.model.workspace import Workspace

_log = logging.getLogger(__name__)

class Repository:
    __connection: Connection = None

    def __init__(self, db_file: Path):
        try:
            self.__connection = sqlite3.connect(db_file)
            _log.debug(f"Using sqlite version {sqlite3.version}")
            self._ensure_schema()
        except Error as e:
            _log.error("Couldn't connect to the database", exc_info=e)
            raise e

    def __del__(self):
        if self.__connection is not None:
            self.__connection.close()
            del self.__connection

    def _ensure_schema(self):
        _log.debug("Ensuring schema is initialized")
        cur = self.__connection.cursor()
        cur.execute("PRAGMA foreign_keys = ON;")
        try:
            cur.executescript(SQL_CREATE_WORKSPACE_TABLE)
            cur.executescript(SQL_CREATE_IMAGE_TABLE)
            cur.connection.commit()
        finally:
            cur.close()

    @staticmethod
    def _dataclass_to_upsert_query(table_name: str, obj: Any) -> Tuple[str, Tuple[Any]]:
        # Convert the dataclass object to a dictionary
        workspace_dict = vars(obj)

        column_names = ', '.join(workspace_dict.keys())
        placeholders = ', '.join('?' * len(workspace_dict))
        query = f"INSERT OR REPLACE INTO {table_name} ({column_names}) VALUES ({placeholders})"
        return query, tuple(workspace_dict.values())

    # WORKSPACE #

    def get_all_workspaces(self) -> List[Workspace]:
        cur = self.__connection.cursor()
        cur.row_factory = Workspace.row_factory
        try:
            cur.execute("SELECT * FROM workspace ORDER BY last_used_at DESC, name ASC")
            return cur.fetchall()
        finally:
            cur.close()

    def get_workspace(self, id_pk: int) -> Optional[Workspace]:
        cur = self.__connection.cursor()
        cur.row_factory = Workspace.row_factory
        try:
            cur.execute("SELECT * FROM workspace WHERE id=?", (id_pk,))
            return cur.fetchone()
        finally:
            cur.close()

    def persist_workspace(self, obj: Workspace) -> Workspace:
        cur = self.__connection.cursor()
        cur.row_factory = Workspace.row_factory
        try:
            query, values = self._dataclass_to_upsert_query("workspace", obj)
            cur.execute(query, values)
            cur.connection.commit()
            # Retrieve the just inserted record
            cur.execute("SELECT * FROM workspace WHERE rowid=?", (cur.lastrowid,))
            return cur.fetchone()
        finally:
            cur.close()

    def rm_workspace(self, id_pk: int):
        cur = self.__connection.cursor()
        try:
            cur.execute("DELETE FROM workspace WHERE id=?", (id_pk,))
            cur.connection.commit()
        finally:
            cur.close()

    # IMAGE DATA #

    def get_all_images_for_workspace(self, workspace_id: int) -> List[ImageData]:
        cur = self.__connection.cursor()
        cur.row_factory = ImageData.row_factory
        try:
            # * performance optimization opportunity: don't load thumbs when not needed
            query = "SELECT * FROM image_data WHERE workspace_id=? ORDER BY workspace_id ASC, path ASC"
            cur.execute(query, (workspace_id,))
            return cur.fetchall()
        finally:
            cur.close()

    def get_image(self, workspace_id: int, path: str) -> Optional[ImageData]:
        cur = self.__connection.cursor()
        cur.row_factory = ImageData.row_factory
        try:
            cur.execute("SELECT * FROM image_data WHERE workspace_id=? AND path=?", (workspace_id, path))
            return cur.fetchone()
        finally:
            cur.close()

    def is_image_outdated(self, workspace_id: int, path: str, last_updated_at: datetime) -> bool:
        cur = self.__connection.cursor()
        cur.row_factory = ImageData.row_factory
        try:
            query = "SELECT * FROM image_data WHERE workspace_id=? AND path=? AND last_updated_at >= ?"
            cur.execute(query, (workspace_id, path, last_updated_at))
            return cur.fetchone() is None
        finally:
            cur.close()

    def persist_image(self, obj: ImageData) -> ImageData:
        cur = self.__connection.cursor()
        cur.row_factory = ImageData.row_factory
        try:
            query, values = self._dataclass_to_upsert_query("image_data", obj)
            cur.execute(query, values)
            cur.connection.commit()
            # Retrieve the just inserted record
            cur.execute("SELECT * FROM image_data WHERE rowid=?", (cur.lastrowid,))
            return cur.fetchone()
        finally:
            cur.close()

    def rm_image(self, workspace_id: int, path: str):
        cur = self.__connection.cursor()
        try:
            cur.execute("DELETE FROM image_data WHERE workspace_id=? AND path=?", (workspace_id, path))
            cur.connection.commit()
        finally:
            cur.close()

    def get_image_rank_histogram(self, workspace_id: int) -> Dict[int, int]:
        """
        Returns counts of images in every rank in the workspace (key is rank, value is count).
        """
        cur = self.__connection.cursor()
        try:
            cur.execute(
                "SELECT rank, COUNT(*) as count FROM image_data WHERE workspace_id=? GROUP BY rank ORDER BY rank ASC",
                (workspace_id,),
            )
            return dict(cur.fetchall())
        finally:
            cur.close()

SQL_CREATE_WORKSPACE_TABLE = """
CREATE TABLE IF NOT EXISTS workspace (
    id              integer PRIMARY KEY AUTOINCREMENT,
    name            text    NOT NULL,
    path            text    NOT NULL,
    last_used_at    text
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_workspace_name
ON workspace(name);
CREATE INDEX IF NOT EXISTS        idx_workspace_last_used_at
ON workspace(last_used_at);
"""

SQL_CREATE_IMAGE_TABLE = """
CREATE TABLE IF NOT EXISTS image_data (
    workspace_id    integer NOT NULL,
    path            text    NOT NULL,

    size            integer NOT NULL,
    last_updated_at text    NOT NULL,
    width           integer NOT NULL,
    height          integer NOT NULL,
    thumbnail       blob    NULL,
    rank            integer DEFAULT 0 NOT NULL,

    PRIMARY KEY     (workspace_id, path),
    CONSTRAINT      fk_workspace
        FOREIGN KEY (workspace_id)
        REFERENCES  workspace(id)
        ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS        idx_image_data_path
ON image_data(path);
CREATE INDEX IF NOT EXISTS        idx_image_data_last_updated_at
ON image_data(last_updated_at);
CREATE INDEX IF NOT EXISTS        idx_image_data_rank
ON image_data(rank);
"""


import logging
import os

from argparse import ArgumentParser
#from progressbar import progressbar

import torch

import torch.nn as nn

from src.detr.polyline import build
import json
from src.utils.configs import get_default_configuration, load_config

from src.utils.confusion import BinaryConfusionMatrix
from src.data import data_factory

import src.utils.visualise_polyline as vis_tools

from tqdm import tqdm
import numpy as np
from PIL import Image
import time
import glob

def train(dataloader,dataset, model, criterion, optimiser, postprocessors, confusion, config,args, iteration):

    model.train()
    criterion.train()

    for m in model.modules():
        if isinstance(m, nn.modules.batchnorm._BatchNorm):
            if hasattr(m, 'weight'):
                m.weight.requires_grad_(False)
            if hasattr(m, 'bias'):
                m.bias.requires_grad_(False)
            m.eval()

    loss_list=[]
    iou_list=[]

    data_loading_times=[]
    optimization_times=[]

    running_loss_dict={}

    time3 = time.time()
    for i, batch in enumerate(dataloader):

        if iteration % config.reset_confusion_interval == 0:
            confusion.reset()

        if batch[-1]:
            continue

        seq_images, targets, _ = batch

        cuda_targets = []
        for b in targets:
            temp_dict={}
            temp_dict['calib'] = b['calib'].cuda()
            temp_dict['center_img'] = b['center_img'].cuda()
            temp_dict['labels'] = b['labels'].cuda()
            temp_dict['roads'] = b['roads'].cuda()
            temp_dict['control_points'] = b['control_points'].cuda()
            temp_dict['con_matrix'] = b['con_matrix'].cuda()
            temp_dict['endpoints'] = b['endpoints'].cuda()

            temp_dict['mask'] = b['mask'].cuda()
            temp_dict['bev_mask'] = b['bev_mask'].cuda()

            temp_dict['obj_corners'] = b['obj_corners'].cuda()
            temp_dict['obj_converted'] = b['obj_converted'].cuda()
            temp_dict['obj_exists'] = b['obj_exists'].cuda()

            temp_dict['init_point_matrix'] = b['init_point_matrix'].cuda()
            temp_dict['sorted_control_points'] = b['sorted_control_points'].cuda()
            temp_dict['grid_sorted_control_points'] = b['grid_sorted_control_points'].cuda()
            temp_dict['sort_index'] = b['sort_index'].cuda()

            temp_dict['left_traffic'] = b['left_traffic'].cuda()
            temp_dict['outgoings'] = b['outgoings']
            temp_dict['incomings'] = b['incomings']

            cuda_targets.append(temp_dict)

        seq_images=seq_images.cuda()

        time2 = time.time()
        data_loading_times.append(time2-time3)

        outputs = model(seq_images,cuda_targets[0]['calib'], cuda_targets[0]['grid_sorted_control_points'], targets[0]['left_traffic'],training=True, iteration=iteration)

        loss_dict = criterion(outputs, cuda_targets)
        weight_dict = criterion.weight_dict

        loss = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)

        optimiser.zero_grad()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip_max_norm)
        optimiser.step()

        time3 = time.time()

        optimization_times.append(time3-time2)

        loss_list.append(loss.data.cpu().numpy())

        for k in loss_dict.keys():

            if k in running_loss_dict.keys():
                running_loss_dict[k].append(loss_dict[k].data.cpu().numpy())
            else:
                running_loss_dict[k] = [loss_dict[k].data.cpu().numpy()]

#            Visualise
        if iteration % config.stats_interval == 0:

            out = vis_tools.get_selected_polylines(outputs , thresh = 0.5)
            hausdorff_static_dist, hausdorff_static_idx, hausdorff_gt, out = vis_tools.hausdorff_match(out, targets[0])

            confusion.update(out[0], hausdorff_gt, hausdorff_static_idx,   targets[0],   static=True, polyline=True)

#        if iteration % config.vis_interval == 0:

#            vis_tools.save_results_train(seq_images.cpu().numpy(), outputs,targets,  config)

        if iteration % config.log_interval == 0:

            logging.error('ITERATION ' + str(iteration))

            for k in running_loss_dict.keys():
                 logging.error('LOSS ' + str(k) +' : ' + str(np.mean(running_loss_dict[k]) ))

            static_res_dict, object_res_dict = confusion.get_res_dict

            logging.error('MEAN F : ' + str(static_res_dict['mean_f_score']))
            logging.error('ASSOC F : ' + str(static_res_dict['assoc_f']))
            logging.error('DETECTION : ' + str(static_res_dict['detection_ratio']))

            logging.error('Opt time : '+ str(np.mean(optimization_times)) +', data time : ' + str(np.mean(data_loading_times)))
            data_loading_times=[]
            optimization_times=[]

            loss_list=[]
            running_loss_dict = {}
            confusion.reset

        iteration += 1
    return iteration, confusion

def evaluate(dataloader, model, criterion, postprocessors, confusion,  config,args, thresh):

     model.eval()

     criterion.eval()

     logging.error('VALIDATION')
     # Iterate over dataset
     for i, batch in enumerate(tqdm(dataloader)):

        seq_images, targets, _ = batch
        if seq_images == None:
            continue
        seq_images = seq_images.cuda()
        cuda_targets = []

        cuda_targets = []
        for b in targets:
            temp_dict={}
            temp_dict['calib'] = b['calib'].cuda()
            temp_dict['center_img'] = b['center_img'].cuda()
            temp_dict['labels'] = b['labels'].cuda()
            temp_dict['roads'] = b['roads'].cuda()
            temp_dict['control_points'] = b['control_points'].cuda()
            temp_dict['con_matrix'] = b['con_matrix'].cuda()
            temp_dict['endpoints'] = b['endpoints'].cuda()

            temp_dict['mask'] = b['mask'].cuda()
            temp_dict['bev_mask'] = b['bev_mask'].cuda()

            temp_dict['obj_corners'] = b['obj_corners'].cuda()
            temp_dict['obj_converted'] = b['obj_converted'].cuda()
            temp_dict['obj_exists'] = b['obj_exists'].cuda()

            temp_dict['init_point_matrix'] = b['init_point_matrix'].cuda()
            temp_dict['sorted_control_points'] = b['sorted_control_points'].cuda()
            temp_dict['grid_sorted_control_points'] = b['grid_sorted_control_points'].cuda()
            temp_dict['sort_index'] = b['sort_index'].cuda()

            temp_dict['left_traffic'] = b['left_traffic'].cuda()
            temp_dict['outgoings'] = b['outgoings']
            temp_dict['incomings'] = b['incomings']

            cuda_targets.append(temp_dict)

        seq_images=seq_images.cuda()

        outputs = model(seq_images,cuda_targets[0]['calib'], cuda_targets[0]['grid_sorted_control_points'], targets[0]['left_traffic'], thresh=thresh,training=False)

        static_thresh = thresh

        out = vis_tools.get_selected_polylines(outputs , thresh = static_thresh)

        hausdorff_static_dist, hausdorff_static_idx, hausdorff_gt, out = vis_tools.hausdorff_match(out, targets[0])

        try:
             confusion.update(out, hausdorff_gt, hausdorff_static_idx,  targets[0],   static=True,polyline=True)

        except Exception as e:
             logging.error('EXCEPTION IN CONFUSION ')
             logging.error(str(e))
             continue

#        vis_tools.save_results_eval(seq_images.cpu().numpy(),out,targets,inter_dict,target_ids,config)

     return confusion

def save_checkpoint(path, model, optimizer, scheduler, epoch, iteration,best_iou):

    if isinstance(model, nn.DataParallel):
        model = model.module

    ckpt = {
        'model' : model.state_dict(),
        'optimizer' : optimizer.state_dict(),
        'scheduler' : scheduler.state_dict(),
        'epoch' : epoch,
        'iteration': iteration,
        'best_iou' : best_iou
    }

    torch.save(ckpt, path)

def load_checkpoint(path, model, optimizer, scheduler, load_orig_ckpt=False):

    ckpt = torch.load(path)
    logging.error('CKPT PATH ' + str(path))

    if load_orig_ckpt:
        if isinstance(model, nn.DataParallel):
            model = model.module

        model.load_state_dict(ckpt['model'],strict=False)
        # model.load_state_dict(ckpt,strict=False)
        return 1, 0,0
    else:
        if isinstance(model, nn.DataParallel):
            model = model.module

        model.load_state_dict(ckpt['model'],strict=False)

    # Load optimiser state
        optimizer.load_state_dict(ckpt['optimizer'])

        # Load scheduler state
        scheduler.load_state_dict(ckpt['scheduler'])

        if 'iteration' not in ckpt.keys():
            to_return_iter = 0
        else:
            to_return_iter = ckpt['iteration']
        # to_return_iter = 0
        logging.error('LOADED MY')
        return ckpt['epoch'], ckpt['best_iou'],to_return_iter
    # return 0,0,0
def load_pretrained_backbone(path, model):

    ckpt = torch.load(path)

    model.load_state_dict(ckpt,strict=False)

# Load the configuration for this experiment
def get_configuration(args):

    # Load config defaults
    config = get_default_configuration()

    return config

def create_experiment(config,  resume=None):

    # Restore an existing experiment if a directory is specified
    if resume is not None:
        print("\n==> Restoring experiment from directory:\n" + resume)
        logdir = resume

    else:

        name = name = config.train_dataset + '_PRNN_'
        logdir = os.path.join(os.path.expandvars(config.logdir), name)
        print("\n==> Creating new experiment in directory:\n" + logdir)
        os.makedirs(logdir,exist_ok=True)
        os.makedirs(os.path.join(config.logdir,'val_images'),exist_ok=True)
        os.makedirs(os.path.join(config.logdir,'train_images'),exist_ok=True)

        # Display the config options on-screen
        print(config.dump())

        # Save the current config
        with open(os.path.join(logdir, 'config.yml'), 'w') as f:
            f.write(config.dump())

    return logdir

def freeze_backbone_layers(model):
    logging.error('MODEL FREEZE')
    for n, p in model.named_parameters():
#        logging.error('STR ' + str(n))
        if "backbone" in n and p.requires_grad:

#            if  (('block14' in n) |('block15' in n) |('block16' in n) |('block17' in n) |('block18' in n)
#                 |('block19' in n) | ('block20' in n) | ('block21' in n) | ('spp' in n)):
            if  ( ('block18' in n) |('block19' in n) | ('block20' in n) | ('block21' in n) | ('spp' in n)):
                p.requires_grad_(True)
            else:
                p.requires_grad_(False)
            # logging.error(str(n) + ', '+str(p.requires_grad))

#                logging.error(str(n) + ', '+str(p.requires_grad))

apply_poly_loss = True
def main():

    parser = ArgumentParser()

    parser.add_argument('--resume', default= None,
                        help='path to an experiment to resume')

    parser.add_argument('--exp', default='/cluster/home/cany/lanefinder_github/baseline/Experiments/mle.json',
                        help='path to an experiment to resume')

    parser.add_argument('--split_pe', type=bool, default=False,
                    help='whether it is on dgx')
    parser.add_argument('--apply_poly_loss', type=bool, default=apply_poly_loss,
                    help='whether it is on dgx')

    parser.add_argument('--objects', type=bool, default=False,
                help='whether estimate objects')

    parser.add_argument('--num_object_queries', default=100, type=int,
                    help="Number of query slots")

    parser.add_argument('--num_object_classes', default=8, type=int,
                help="Num object classes")

    parser.add_argument('--num_spline_points', default=3, type=int,
                help="Num object classes")

    parser.add_argument('--lr', default=1e-5, type=float)
    parser.add_argument('--lr_backbone', default=1e-5, type=float)
    parser.add_argument('--batch_size', default=2, type=int)
    parser.add_argument('--weight_decay', default=1e-4, type=float)
    parser.add_argument('--epochs', default=300, type=int)
    parser.add_argument('--lr_drop', default=50, type=int)
    parser.add_argument('--clip_max_norm', default=0.1, type=float,
                        help='gradient clipping max norm')

    # Model parameters
    parser.add_argument('--frozen_weights', type=str, default=None,
                        help="Path to the pretrained model. If set, only the mask head will be trained")
    # * Backbone
    parser.add_argument('--backbone', default='resnet50', type=str,
                        help="Name of the convolutional backbone to use")
    parser.add_argument('--dilation', default=True,
                        help="If true, we replace stride with dilation in the last convolutional block (DC5)")
    parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
                        help="Type of positional embedding to use on top of the image features")

    # * Transformer

    parser.add_argument('--dim_feedforward', default=256, type=int,
                        help="Intermediate size of the feedforward layers in the transformer blocks")

    parser.add_argument('--hidden_dim', default=256, type=int,
                        help="Size of the embeddings (dimension of the transformer)")

    parser.add_argument('--dropout', default=0.1, type=float,
                        help="Dropout applied in the transformer")

    parser.add_argument('--nheads', default=4, type=int,
                        help="Number of attention heads inside the transformer's attentions")
    parser.add_argument('--num_queries', default=100, type=int,
                        help="Number of query slots")
    parser.add_argument('--pre_norm', action='store_true')

    # * Segmentation
    parser.add_argument('--masks',default=False,
                        help="Train segmentation head if the flag is provided")

    # Loss
    parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_false',
                        help="Disables auxiliary decoding losses (loss at each layer)")
    # * Matcher
    parser.add_argument('--set_obj_cost_class', default=2, type=float,
                        help="Class coefficient in the matching cost")
    parser.add_argument('--set_obj_cost_center', default=3, type=float,
                        help="Class coefficient in the matching cost")

    parser.add_argument('--set_obj_cost_len', default=0.5, type=float,
                        help="Class coefficient in the matching cost")

    parser.add_argument('--set_obj_cost_orient', default=1, type=float,
                        help="Class coefficient in the matching cost")

    parser.add_argument('--set_cost_class', default=2, type=float,
                        help="Class coefficient in the matching cost")
    parser.add_argument('--set_cost_bbox', default=4, type=float,
                        help="L1 box coefficient in the matching cost")
    parser.add_argument('--set_cost_end', default=1, type=float,
                        help="L1 endpoint coefficient in the matching cost")

    parser.add_argument('--set_cost_giou', default=1, type=float,
                        help="giou box coefficient in the matching cost")
    # * Loss coefficients

    parser.add_argument('--object_detection_loss_coef', default=4, type=float)
    parser.add_argument('--object_center_loss_coef', default=3, type=float)
    parser.add_argument('--object_len_loss_coef', default=0.5, type=float)
    parser.add_argument('--object_orient_loss_coef', default=0.5, type=float)

    parser.add_argument('--polyline_loss_coef', default=2, type=float)
    parser.add_argument('--mask_loss_coef', default=1, type=float)
    parser.add_argument('--dice_loss_coef', default=1, type=float)
    parser.add_argument('--assoc_loss_coef', default=1, type=float)
    parser.add_argument('--detection_loss_coef', default=1, type=float)
    parser.add_argument('--endpoints_loss_coef', default=2, type=float)
    parser.add_argument('--bbox_loss_coef', default=1, type=float)
    parser.add_argument('--focal_loss_coef', default=0.1, type=float)
    parser.add_argument('--init_points_loss_coef', default=10, type=float)

    parser.add_argument('--loss_end_match_coef', default=1, type=float)

    parser.add_argument('--giou_loss_coef', default=2, type=float)
    parser.add_argument('--visible_loss_coef', default=1, type=float)

    parser.add_argument('--eos_coef', default=0.01, type=float,
                        help="Relative classification weight of the no-object class")

    parser.add_argument('--object_eos_coef', default=0.1, type=float,
                        help="Relative classification weight of the no-object class")

    # dataset parameters
    parser.add_argument('--dataset_file', default='coco')
    parser.add_argument('--coco_path', type=str)
    parser.add_argument('--coco_panoptic_path', type=str)
    parser.add_argument('--remove_difficult', action='store_true')

    parser.add_argument('--output_dir', default='',
                        help='path where to save, empty for no saving')
    parser.add_argument('--device', default='cuda',
                        help='device to use for training / testing')
    parser.add_argument('--seed', default=42, type=int)

    parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
                        help='start epoch')
    parser.add_argument('--eval',default=False, action='store_true')
    parser.add_argument('--num_workers', default=2, type=int)

    # distributed training parameters
    parser.add_argument('--world_size', default=1, type=int,
                        help='number of distributed processes')
    parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')

    args = parser.parse_args()

    print('GOT ARGS ')
    logging.error(str(args))

    # Load configuration
    config = get_configuration(args)

    opts = json.load(open(args.exp, 'r'))

    # Create a directory for the experiment
    logdir = create_experiment(config, args.resume)

    config.save_logdir = logdir
    config.n_control_points = args.num_spline_points
    config.freeze()

    device = torch.device(args.device)
    # Setup experiment
    model, criterion, postprocessors = build(args, config,opts)

    model.to(device)

    if config.train_dataset == 'nuscenes':

        train_loader,train_dataset, val_loader, val_dataset = data_factory.build_nuscenes_dataloader(config,args, val=True)

    else:
        train_loader,train_dataset, val_loader, val_dataset = data_factory.build_argoverse_dataloader(config,args, val=True)

    freeze_backbone_layers(model)
    param_dicts = [
        {"params": [p for n, p in model.named_parameters() if "backbone" not in n and p.requires_grad]},
        {
            "params": [p for n, p in model.named_parameters() if "backbone" in n and p.requires_grad],
            "lr": args.lr_backbone,
        },
    ]
    optimizer = torch.optim.AdamW(param_dicts, lr=args.lr,
                                  weight_decay=args.weight_decay)
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_drop)

    # Load checkpoint

    # epoch, best_iou, iteration = load_checkpoint(os.path.join('/cluster/work/cvl/cany/simplice/ckpts/poly-base-False', 'polyrnn-base.pth'),
    #                               model, optimizer, lr_scheduler)

    if config.load_pretrained_backbone:
        load_pretrained_backbone(config.backbone_ckpt_path, model.backbone.backbone_net)
        epoch, best_iou, iteration = 1,0,0

    else:

        epoch, best_iou, iteration = load_checkpoint(os.path.join(logdir, 'latest.pth'),
                                      model, optimizer, lr_scheduler)

        logging.error('LOADED MY CHECKPOINT')

    freeze_backbone_layers(model)

    confusion = BinaryConfusionMatrix(1,args.num_object_classes)

    # Main training loop
    while epoch <= config.num_epochs:

        print('\n\n=== Beginning epoch {} of {} ==='.format(epoch,
                                                            config.num_epochs))

        iteration, confusion=train(train_loader, train_dataset,model, criterion, optimizer,postprocessors , confusion, config,args, iteration)
        logging.error('COCO FINISHED')

        lr_scheduler.step()

        save_checkpoint(os.path.join(logdir, 'latest.pth'), model, optimizer,
                        lr_scheduler, epoch, iteration,best_iou)
        # Evaluate on the validation set
        if epoch % 5 == 0:
            thresh=0.3
            val_con = evaluate(val_loader, model, criterion, postprocessors,BinaryConfusionMatrix(1,args.num_object_classes), config, args, thresh)

            logging.error('EPOCH ' + str(epoch))

            static_res_dict, object_res_dict = val_con.get_res_dict
            file1 = open(os.path.join(logdir,'val_res_thresh_'+str(thresh)+'.txt'),"a")

            file1.write('EPOCH : ' + str(epoch) + ' \n')

            for k in static_res_dict.keys():
                logging.error(str(k) + ' : ' + str(static_res_dict[k]))
                file1.write(str(k) + ' : ' + str(static_res_dict[k]) + ' \n')

            for k in object_res_dict.keys():
                logging.error(str(k) + ' : ' + str(object_res_dict[k]))
                file1.write(str(k) + ' : ' + str(object_res_dict[k]) + ' \n')

            file1.close()

        epoch += 1

    print("\nTraining complete!")

if __name__ == '__main__':
    main()


import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *
from overview import *
from PyQt5 import uic
from PyQt5.QtCore import *
from pyupbit import Upbit
#from volatility import *

class worker(QThread):
    def __init__(self, b):
        super().__init__()
        self.b = b

    def end(self):
        self.alive = False

    def run(self):
        hold_flag = False
        self.alive = True
        #target_price = get_target_price()

        while self.alive:
            now = datetime.datetime.now()
            mid = datetime.datetime(now.year, now.month, now.day)
            delta = datetime.timedelta(seconds=10)

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi("main.ui", self)

        with open("upbit.txt", "r") as f:
            key0 = f.readline().strip()
            key1 = f.readline().strip()

        self.key0.setText(key0)
        self.key1.setText(key1)

        self.btn.setText("login")
        self.btn.clicked.connect(self.login)

    #이부분 수정 필요함
    def login(self):
        if self.btn.text() == "login":
            b = Upbit(self.key0.text(), self.key1.text())
            balance = b.get_balance("KRW")
            self.log.append(f"[로그인] 성공 / 현금 : {balance}")
            self.btn.setText("logout")

            #self.w = Worker(b)
            #self.w.start()

        else:
            # 자동매매 로직 종료
            #self.w.end()
            self.btn.setText("login")
            self.log.append(f"[로그아웃] 성공 / 자동매매종료")

    def closeEvent(self, event):
        self.overviewWidget.closeEvent(event)
        self.chartWidget.closeEvent(event)
        self.orderbookWidget.closeEvent(event)

if __name__ == "__main__":
    app = QApplication([])
    m  = MyWindow()
    m.show()
    app.exec_()


import os
import argparse

def extract_static_table():
    path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        'static_table.txt'
    )

    with open(path, 'r') as f:
        print('(')

        lines = list(f)
        for line in lines[3:len(lines)-1]:
            sp = line.split('|')
            name = sp[2].strip()
            value = sp[3].strip()
            print("    ('{0}', '{1}'),".format(name, value))

        print(')\n')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Extract data (as tuples) from *.txt tables of this repo.')
    parser.add_argument('-s', '--static', action='store_true', help='print static tuple')
    parser.add_argument('-H', '--huffman', action='store_true', help='print huffman tuple')

    args = parser.parse_args()
    if args.static:
        extract_static_table()
    if args.huffman:
        pass


from pynput.mouse import Button,Controller
from pynput.keyboard import Key
from time import sleep
sleep(10)
mouse=Controller()
mouse.position=(450,850)
mouse.press(Button.left)
mouse.release(Button.left)
sleep(4)
a=' '
from pynput.keyboard import Controller
Keyboard=Controller()
def tyype(a):
    keyboard=Controller()
    a=list(a)
    for i in range(0,len(a)):
        if(a[i]=='$'):
            Keyboard.press(Key.ctrl)
            continue
        Keyboard.press(a[i])
        Keyboard.release(a[i])
    Keyboard.release(Key.ctrl)
    Keyboard.press(Key.enter)
    Keyboard.release(Key.enter)

a="$t"
a='facebook.com'
tyype(a)
sleep(4)
mouse.position=(450,200)
sleep(4)
mouse.press(Button.left)
mouse.release(Button.left)
a="Nithin is THE topper!!"
tyype(a)
from pynput.mouse import Button,Controller
mouse=Controller()
mouse.position=(450,450)
sleep(5)
mouse.press(Button.left)
mouse.release(Button.left)


# coding=utf8

import torch
import torch.nn as nn

class MyNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(3, 10, 2, stride=2)
        self.relu = nn.ReLU()
        self.flatten = lambda x: x.view(-1)
        self.fc1 = nn.Linear(160, 5)

    def forward(self, x):
        x = self.relu(self.conv(x))
        x.register_hook(lambda grad: torch.clamp(grad,  min=0))
        x.register_hook(lambda grad: print("Gradients less than zero: ", bool((grad <0).any())))
        return self.fc1(self.flatten(x))

net = MyNet()

grad_res_dict = {}

def store_gradient_info(grad_res_dict, model):
    named_paras = model.named_parameters()
    for n, p in named_paras:
        if p.requires_grad:
            if n not in grad_res_dict:
                grad_res_dict[n] = []
            if p.grad is None:
                print(f"{n}'s gradient is None")
            else:
                grad_res_dict[n].append(p.grad.abs().mean().cpu().item())

def calc_wei_dist(model, pre_wei_dict, wei_dis_dict):
    named_paras = model.named_parameters()
    for n, p in named_paras:
        if p.requires_grad:
            if n in pre_wei_dict:
                dis_ = (p.data - pre_wei_dict[n]).norm().cpu().item()
                if n not in wei_dis_dict:
                    wei_dis_dict[n] = []
                wei_dis_dict[n].append(dis_)
            pre_wei_dict[n] = p.data.clone()

def save_to_wb(info_dict, wb, suffix='_suffix'):
    for key, val_arr  in info_dict.items():
        for val in val_arr:
            wb.log({f'{key}_grad': val})

inp = torch.randn(1, 3, 8, 8)
out = net(inp)

(1-out.mean()).backward()
store_gradient_info(grad_res_dict, net)
print("The biases are", net.fc1.bias.grad)
print(grad_res_dict)


from flask import Flask
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servo = GPIO.PWM(11,50)

app = Flask(__name__)

@app.route('/')

def index():
    return 'This is it!'

if(__name__) == '__main__':
    app.run(debug=True, host='0.0.0.0')

@app.route('/move-servo')

def move_servo():
    print('moving servo!')
    servo.start(0)
    time.sleep(1)
    print("rotating")
    duty = 2
    while duty <= 17:
        servo.ChangeDutyCycle(duty)
        time.sleep(1)
        duty = duty + 1
    servo.stop()
    GPIO.cleanup()
    print("all cleared")

    return 'servo moved'


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

# Learn more: https://github.com/kennethreitz/setup.py

from setuptools import setup, find_packages

with open('README.rst') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='spark_ts',
    version='0.1.0',
    description='Pyspark package supporting climate computing',
    long_description=readme,
    author='Jesse Lima',
    author_email='jesseamerico@gmail.com',
    url='https://github.com/jaglima/climaspark',
    license=license,
    packages=find_packages(exclude=('tests', 'docs'))
)


from datetime import time

from pydantic import BaseModel
from fastapi import APIRouter

from Presenter import pre_request_post

router = APIRouter()

class AddDocter(BaseModel):
    docter_name: str
    hospital_name: str
    department : str
    nonpaid : str
    day_start_time : time
    day_end_time : time
    day_start_rest : time
    day_end_rest : time
    sat_start_time : time
    sat_end_time : time
    sun_start_time : time
    sun_end_time : time
    holiday : str
    workday : str

class AddPatient(BaseModel):
    patient_name : str

class AddRequest(BaseModel):
    patient_id : int
    docter_id : int
    date: str
    time : time

# 의사 추가
@router.post('/docter', tags=["Add"])
async def add_post_docter(add : AddDocter):
    result = pre_request_post.pre_post_doc(add)

    return result

# 환자 추가
@router.post('/patient', tags=["Add"])
async def add_post_patient(add : AddPatient):
    result = pre_request_post.pre_post_patient(add)

    return result

#진료 요청
@router.post('/request', tags=["Add"])
async def add_post_request(add : AddRequest):
    result = pre_request_post.pre_post_request(add)

    return result


from setuptools import setup

package_name = 'api_test'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='omose',
    maintainer_email='hiro.tx@kmd.keio.ac.jp',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'autonomous_api = py_pubsub.publisher_member_function:main',
        ],
    },
)


import logging
from skmultiflow.lazy import KNNADWINClassifier
from skmultiflow.data import FileStream
from skmultiflow.transform import OneHotToCategorical
from sklearn.neighbors.classification import KNeighborsClassifier
from timeit import default_timer as timer

def demo():
    """ _test_knn_adwin

    This demo tests the KNNADWINClassifier on a file stream, which gives
    instances coming from a SEA generator.

    The test computes the performance of the KNNADWINClassifier as well as
    the time to create the structure and classify max_samples (10000 by
    default) instances.

    """
    start = timer()
    logging.basicConfig(format='%(message)s', level=logging.INFO)
    # warnings.filterwarnings("ignore", ".*Passing 1d.*")
    stream = FileStream("https://raw.githubusercontent.com/scikit-multiflow/streaming-datasets/"
                        "master/sea_big.csv", -1, 1)
    # stream = RandomRBFGeneratorDrift(change_speed=41.00, n_centroids=50, model_random_state=32523423,
    #                                  sample_seed=5435, n_classes=2, num_att=10, num_drift_centroids=50)

    t = OneHotToCategorical([[10, 11, 12, 13],
                            [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
                            36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]])
    t2 = OneHotToCategorical([[10, 11, 12, 13],
                            [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
                            36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]])

    knn = KNNADWINClassifier(n_neighbors=8, leaf_size=40, max_window_size=2000)
    # pipe = Pipeline([('one_hot_to_categorical', t), ('KNNClassifier', knn)])

    compare = KNeighborsClassifier(n_neighbors=8, algorithm='kd_tree', leaf_size=40, metric='euclidean')
    # pipe2 = Pipeline([('one_hot_to_categorical', t2), ('KNNClassifier', compare)])
    first = True
    train = 200
    if train > 0:
        X, y = stream.next_sample(train)
        # pipe.partial_fit(X, y, classes=stream.target_values)
        # pipe.partial_fit(X, y, classes=stream.target_values)
        # pipe2.fit(X, y)

        knn.partial_fit(X, y, classes=stream.target_values)
        compare.fit(X, y)
        first = False
    n_samples = 0
    max_samples = 10000
    my_corrects = 0
    compare_corrects = 0

    while n_samples < max_samples:
        if n_samples % (max_samples/20) == 0:
            logging.info('%s%%', str((n_samples//(max_samples/20)*5)))
        X, y = stream.next_sample()
        # my_pred = pipe.predict(X)
        my_pred = knn.predict(X)
        # my_pred = [1]
        if first:
            # pipe.partial_fit(X, y, classes=stream.target_values)
            # pipe.partial_fit(X, y, classes=stream.target_values)
            knn.partial_fit(X, y, classes=stream.target_values)
            first = False
        else:
            # pipe.partial_fit(X, y)
            knn.partial_fit(X, y)
        # compare_pred = pipe2.predict(X)
        compare_pred = compare.predict(X)
        if y[0] == my_pred[0]:
            my_corrects += 1
        if y[0] == compare_pred[0]:
            compare_corrects += 1
        n_samples += 1

    end = timer()

    print('Evaluation time: ' + str(end - start))
    print(str(n_samples) + ' samples analyzed.')
    print('My performance: ' + str(my_corrects / n_samples))
    print('Compare performance: ' + str(compare_corrects / n_samples))

if __name__ == '__main__':
    demo()


from requests import session
import requests
import re
import execjs
from pprint import pprint
import time

class GetIndex(object):

    def __init__(self):
        self.url = 'http://bj.gsxt.gov.cn/index.html'
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Host': 'bj.gsxt.gov.cn',
            'Pragma': 'no-cache',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',
            'Referer': 'http://bj.gsxt.gov.cn/index.html',
        }

        self.cookie_headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Host': 'bj.gsxt.gov.cn',
            'Pragma': 'no-cache',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',

        }

    def get_cookies(self, res):
        js_str = re.findall("document.cookie=(.*?)\+\('m'\)\+\('a'\)\+\('x'\)", res.text, re.S)[0]
        js = """function jl_clear(){ return %s }""" % js_str
        __jsluid_h = "__jsluid_h=" + res.cookies['__jsluid_h']
        __jsl_clearance = execjs.compile(js).call('jl_clear').replace(";", '')

        cookie = '{}; {}'.format(__jsluid_h, __jsl_clearance)
        self.headers['Cookie'] = cookie

        res2 = requests.get(self.url, headers=self.headers)
        set_timeout = re.findall(r"(setTimeout.*?\},.*?\);)", res2.text, re.S)[0]
        js_cookie = "return {}".format(re.findall("setTimeout.*?=(.*?);", res2.text, re.S)[0])
        js = res.content.decode("utf-8").replace(set_timeout, js_cookie).replace("go({",
                                                                                "function get_jsl(){return go({").replace(
            "<script>", '').replace("</script>", "}").replace("\\x20", ' ').replace("\\x22", '"')

        js = self.get_js(js)

        jsl = execjs.compile(js, cwd=r'E:\node\node_modules\npm\node_modules').call("get_jsl")

        cookie = "{} {};".format(__jsluid_h, jsl.split(";")[0])
        self.headers['Cookie'] = cookie

        res = requests.get(self.url, headers=self.headers)
        jsid = re.findall("(JSESSIONID=.*?;)", res.headers['Set-Cookie'])[0]
        token = re.findall("(SECTOKEN=.*?;)", res.headers['Set-Cookie'])[0]
        tlb_cookie = re.findall("(tlb_cookie=.*?;)", res.headers['Set-Cookie'])[0]
        self.headers['Cookie'] = '{} {} {} {}'.format(self.headers['Cookie'], jsid, token, tlb_cookie)

        res = requests.get(self.url, headers=self.headers)
        print(res.text)

    def get_js(self, js_):
        js = r"""
            const jsdom = require("jsdom");
        const { JSDOM } = jsdom;
        const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
        window = global;
        var document = dom.window.document;
        var params = {
            location:{
                hash: "",

                pathname: "/",
                port: "",
                protocol: "https:",
                search: "",
            },
            navigator:{
                appCodeName: "Mozilla",
                appName: "Netscape",
                appVersion: "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
                cookieEnabled: true,
                deviceMemory: 8,
                doNotTrack: null,
                hardwareConcurrency: 4,
                language: "zh-CN",
                languages: ["zh-CN", "zh"],
                maxTouchPoints: 0,
                onLine: true,
                platform: "Win32",
                product: "Gecko",
                productSub: "20030107",
                userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
                vendor: "Google Inc.",
                vendorSub: "",
            },
            "screen":{
                availHeight: 1040,
                availLeft: 0,
                availTop: 0,
                availWidth: 1920,
                colorDepth: 24,
                height: 1080,
                pixelDepth: 24,
                width: 1920,
            }
        };
        Object.assign(window,params);
        window.document = document;

            %s
            """ % js_
        return js

    def run(self):
        res = requests.get(self.url, headers=self.headers)
        self.get_cookies(res)
        pprint(self.headers)

if __name__ == '__main__':
    get_index = GetIndex()
    get_index.run()


import requests
from bs4 import BeautifulSoup
import pandas as pd
from streamlit import write,title,text_input,markdown

title('Weather App')
country = text_input('Enter any country:')
city = text_input("Enter any city from " + str(country) +" :")
url = f'https://www.timeanddate.com/weather/{country}/{city}/ext'

page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find(id ='wt-ext')

page_bg_img = '''
<style>
body, html {
  height: 100%;
}

.bg {
  /* The image used */
  background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRNd9WjgkHO3Oh00fbvHKVCz42aA6OsztNvVg&usqp=CAU");

  /* Full height */
  height: 100%;

  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
'''

markdown(page_bg_img, unsafe_allow_html=True)

hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>

"""
markdown(hide_streamlit_style, unsafe_allow_html=True)

if table:
	column = table.find_all('tr')[2:-1]
	days = [i.find('th').get_text()[:3] for i in column]
	date = [i.find('th').get_text()[3:] for i in column]
	descp = [i.find(class_ ='small').get_text().strip('.') for i in column]
	temp = [i.find(class_ = 'wt-ic').findNext('td').get_text().replace('\xa0','') for i in column]
	weather = pd.DataFrame({
		'Day': days,
		'Date': date,
		'Description':descp,
		'Temparature(Fareinheit)':temp,
})
	write(weather)
	weather.to_csv('weather.csv')
else:
	if city and country:
		write("Sorry weather report for " + str(city) + " is not available !")
	else:
		"Please enter a city & a country name."


import math
from tqdm import tqdm
import pickle

class RiskModel(object):
    def __init__(self, args):
        self.args = args

    def forward(self, batch):
        x_transformed = {
            key: func(batch[key]) for key, func in self.input_transformers.items()
        }
        x_scaled = self.scale_inputs(x_transformed)
        risk = self.model(x_scaled)
        return risk

    def test(self, data):
        results = []
        for sample in tqdm(data.dataset):
            sample["golds"] = sample["y"]
            sample["probs"] = self.forward(sample)

        if self.args.save_predictions:
            self.save_predictions(data.dataset)

    def save_predictions(self, data):
        predictions_dict = [
            {k: v for k, v in d.items() if k in self.save_keys} for d in data
        ]
        predictions_filename = "{}.{}.predictions".format(
            self.args.results_path, self.save_prefix
        )
        pickle.dump(predictions_dict, open(predictions_filename, "wb"))

    @property
    def input_coef(self):
        pass

    @property
    def input_transformers(self):
        pass

class PLCOm2012(RiskModel):
    def __init__(self, args):
        super(PLCOm2012, self).__init__(args)

    def model(self, x):
        return 1 / (1 + math.exp(-x))

    def scale_inputs(self, x):
        running_sum = -4.532506
        for key, beta in self.input_coef.items():
            if key == "race":
                running_sum += beta[x["race"]]
            else:
                running_sum += x[key] * beta
        return running_sum

    @property
    def input_coef(self):
        coefs = {
            "age": 0.0778868,
            "race": {
                "white": 0,
                "black": 0.3944778,
                "hispanic": -0.7434744,
                "asian": -0.466585,
                "native_hawaiian_pacific": 0,
                "american_indian_alaskan": 1.027152,
            },
            "education": -0.0812744,
            "bmi": -0.0274194,
            "cancer_hx": 0.4589971,
            "family_lc_hx": 0.587185,
            "copd": 0.3553063,
            "is_smoker": 0.2597431,
            "smoking_intensity": -1.822606,
            "smoking_duration": 0.0317321,
            "years_since_quit_smoking": -0.0308572,
        }
        return coefs

    @property
    def input_transformers(self):
        funcs = {
            "age": lambda x: x - 62,
            "race": lambda x: x,
            "education": lambda x: x - 4,
            "bmi": lambda x: x - 27,
            "cancer_hx": lambda x: x,
            "family_lc_hx": lambda x: x,
            "copd": lambda x: x,
            "is_smoker": lambda x: x,
            "smoking_intensity": lambda x: 10 / x - 0.4021541613,
            "smoking_duration": lambda x: x - 27,
            "years_since_quit_smoking": lambda x: x - 10,
        }
        return funcs

    @property
    def save_keys(self):
        return [
            "pid",
            "age",
            "race",
            "education",
            "bmi",
            "cancer_hx",
            "family_lc_hx",
            "copd",
            "is_smoker",
            "smoking_intensity",
            "smoking_duration",
            "years_since_quit_smoking",
            "exam",
            "golds",
            "probs",
            "time_at_event",
            "y_seq",
            "y_mask",
            "screen_timepoint",
        ]


from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.db import models

import docker as docker_env
from channels.layers import get_channel_layer

from authenticate.models import User
from .utils import *
from .generate.proto import ProtoFile
from .generate.server import ServerFile
from .managers import UserManager

import importlib
import shutil
import traceback
import json
import shutil
import sys
import os

# import __Rmyw9LxbPBhhegjOy3IjvADs9U1KlT.protobuf_pb2 as objects

class Image:
    def __init__(self, id, image):
        self.id = id
        self.name = image
        self.label = image.split(':')[0]

class Docker(models.Model):
    lenguaje_choices = (
        ('python', 'Python'),
    )

    state_choices = (
        ('stopped', 'Stopped'),
        ('deleted', 'Deleted'),
        ('building', 'Building'),
        ('active', 'Active'),
    )

    id = models.CharField(max_length=32, primary_key=True)
    image_name = models.CharField(max_length=32, unique=True)

    state = models.CharField(
        max_length=10, choices=state_choices, default='building')
    ip = models.CharField(max_length=30, null=True)
    proto = models.CharField(max_length=500, null=True)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='owner')
    subscribers = models.ManyToManyField(User, related_name='subscriptions')
    language = models.CharField(
        max_length=100, choices=lenguaje_choices, default='python')

    protocol = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    name = models.CharField(max_length=250, unique=True)
    background = models.CharField(max_length=500)
    description = models.TextField(null=True)
    image = models.CharField(max_length=100, null=True)
    workdir = models.CharField(max_length=500, null=True)
    file = models.CharField(max_length=100, null=True)
    classname = models.CharField(max_length=100, null=True)
    view = models.CharField(max_length=1, blank=False)
    extensions = models.CharField(max_length=250, null=True, blank=True)

    def check_if_exist(self, sub):
        return sub in self.subscribers.all()

    def get_container(self):
        try:
            client = docker_env.from_env()
            return client.containers.get(self.image_name)
        except docker_env.errors.NotFound as error:
            return None

    def check_active_state(self):
        if self.state == 'active':
            docker = self.get_container()
            return True if docker else False

    def get_container_or_run(self):
        container = self.get_container()
        if container:
            return container
        else:
            return self.run_container()

    def get_public_path(self):
        return f'{settings.MEDIA_URL}{self.id}'

    def get_path(self):
        return f'{settings.MEDIA_ROOT}/{self.id}'

    def create_folders(self):
        os.makedirs('{0}/experiments'.format(self.get_path()), 0o777)

    def dockerfile(self):
        """
            proto:
                {0}: ubicación absoluta del archivo (media_root)
                {1}: archivo proto (proto)
                {2}: ubicación del contenedor (path)
            image:
                {0}: nombre de la imagen creada (image)
            compile:
                {0}: ubicación del contenedor (path)
                {1}: carpeta contenedora de los archivos de comunicación (id)
                {2}: ruta relativa del archivo proto (proto)
        """
        commands = {
            'image': 'FROM {0}'.format(self.image),
            'server': 'COPY ./server.py {0}'.format(self.workdir),
            'proto': 'COPY ./protobuf.proto {0}'.format(self.workdir),

            'mkdir': 'RUN mkdir {0}/{1}'.format(self.workdir, self.id),
            'requirements': 'RUN pip install grpcio grpcio-tools',
            'compile': 'RUN python -m grpc_tools.protoc --proto_path={0} --python_out={0}/{1} --grpc_python_out={0}/{1} protobuf.proto'.format(self.workdir, self.id),
            'init': 'RUN touch {0}/{1}/__init__.py'.format(self.workdir, self.id)
        }

        dockerfile = '{0}/Dockerfile'.format(self.get_path())

        with open(dockerfile, '+w') as file:
            for command in commands.values():
                file.write('{0}\n'.format(command))

    def protobuf(self):
        protobuf = '{0}/{1}'.format(settings.MEDIA_ROOT, self.proto)

        with open(protobuf, '+w') as file_:
            proto = ProtoFile(items=[{
                'kind': element.kind,
                'len': element.len
            } for element in self.elements_type.all()])

            file_.write(proto.create_protobuf(self.image_name))

    def server(self):
        server = '{0}/{1}/server.py'.format(settings.MEDIA_ROOT, self.id)

        with open(server, '+w') as file_:
            aux = ServerFile(items=[{
                'kind': element.kind,
                'len': element.len
            } for element in self.elements_type.all()])

            file_.write(aux.create_server(self.id, ''.join(self.file.split(
                '.')[:-1]), self.workdir, self.classname))

    def create_docker(self):

        try:
            # Creando el WORKDIR
            self.create_folders()

            # Generando los archivos de comunicación
            self.protobuf()
            self.dockerfile()
            self.server()

            # Compilando el archivo de comunicación
            terminal_out(
                "python -m grpc_tools.protoc --proto_path=%(media)s --python_out=%(media)s/%(id)s --grpc_python_out=%(media)s/%(id)s %(id)s/%(proto)s" % {
                    'media': settings.MEDIA_ROOT,
                    'id': self.id,
                    'proto':  'protobuf.proto'.format(self.id)
                })

            shutil.move('%(media)s/%(id)s/%(id)s' % {
                'media': settings.MEDIA_ROOT, 'id': self.id
            },
                settings.ENV_ROOT
            )
        except:
            exc_type, exc_obj, tb = sys.exc_info()
            print(exc_type)
            print(exc_obj)
            traceback.print_exc()
            # self.delete()
        self.save()
        return True

    def build_docker(self):
        try:
            client = docker_env.APIClient(
                base_url='unix://var/run/docker.sock')
            steps = [
                [
                    "Step 1/7:",
                    "Loading image {0} ...".format(self.image),
                    "Loaded image {0}".format(self.image)
                ], [
                    "Step 2/7:",
                    "Creating service files ...",
                    "Created service files"
                ], [
                    "Step 3/7:",
                    "Creating comunication files ...",
                    "Created comunication files"
                ], [
                    "Step 4/7:",
                    "Moving comunication and service files to workdir('{0}') ...".format(
                        self.workdir),
                    "Moved comunication and service files to workdir('{0}')".format(
                        self.workdir)
                ], [
                    "Step 5/7:",
                    "Instaling dependencies of comunication ...",
                    "Instaled dependencies of comunication"
                ], [
                    "Step 6/7:",
                    "Compiling comunication files ...",
                    "Compiled comunication files"
                ], [
                    "Step 7/7:",
                    "Cleaning records ...",
                    "Cleaned records"
                ]
            ]

            return (
                client.build(path=self.get_path(), rm=True,
                             tag='{0}:latest'.format(self.image_name)),
                steps
            )
        except expression as identifier:
            self.delete()
            return [{'stream': "error"}, {'stream': "error"}], [["Error:", "An error occurred during model shrinkage. Check the data and try again"]]

    def run_container(self, builded=False):
        try:
            client = docker_env.from_env()
            client.containers.run(
                image=self.image_name,
                command='python server.py',
                detach=True,
                name=self.image_name,
                # ports={50051: 50051},
                remove=True,
                working_dir=self.workdir,
                volumes={
                    '{}/experiments'.format(self.get_path()): {
                        'bind': '{}/media'.format(self.workdir), 'mode': 'rw'
                    }
                }
            )
            container = self.get_container()
            self.ip = '%s:50051' % container.attrs['NetworkSettings']['IPAddress']
            self.state = 'builded' if builded else 'active'
            self.save()
            return container
        except docker_env.errors.ContainerError as error:
            print(error)
            return error
        except docker_env.errors.APIError as error:
            print(error)
            return error

    def stop_container(self):
        try:
            container = self.get_container()
            if container:
                self.get_container().stop()
                self.state = 'stopped'
                self.ip = ''
                self.save()
                return True
            return False
        except ValueError as error:
            print(error)
            return False
        except docker_env.errors.APIError as error:
            return False

    def delete_image(self):
        try:
            client = docker_env.from_env()
            client.images.remove(image=self.image_name, force=True)
        except docker_env.errors.ImageNotFound as error:
            print(error)

    def delete_module(self):
        self.stop_container()
        self.delete_image()
        self.state = 'deleted'
        self.save()

    def delete(self, delete_img=False):
        super().delete()
        shutil.rmtree(self.get_path(), ignore_errors=True)
        shutil.rmtree('{0}{1}'.format(
            settings.ENV_ROOT, self.id), ignore_errors=True)

class ElementType(models.Model):
    kind = models.CharField(max_length=30)
    docker = models.ForeignKey(Docker, null=False, blank=False,
                               on_delete=models.CASCADE, related_name='elements_type')
    element = models.ForeignKey(
        'Element', null=False, blank=False, on_delete=models.CASCADE, related_name='types')
    len = models.CharField(max_length=1, default=0)
    value = models.TextField()

    def __str__(self):
        return self.kind

class Element(models.Model):
    name = models.CharField(max_length=100, unique=True)

class Experiment(models.Model):
    state = models.CharField(max_length=10, default='created')
    user = models.ForeignKey(User, null=True, blank=False,
                             on_delete=models.CASCADE, related_name='experiments')
    docker = models.ForeignKey(Docker, null=True, blank=False,
                               on_delete=models.CASCADE, related_name='experiments')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def get_logs(self):
        return f"{self.get_workdir()}/logs.txt"

    def get_public_logs(self):
        return f"{self.get_public_path()}/logs.txt"

    def create_workdir(self, outputs=False):
        os.makedirs(self.inputs(), 0o777)
        open(self.get_logs(), "x")
        if outputs:
            os.makedirs(self.outputs(), 0o777)

    def inputs(self):
        return f"{self.get_workdir()}/inputs"

    def outputs(self):
        return f"{self.get_workdir()}/outputs"

    def get_public_path(self):
        return f"{self.docker.get_public_path()}/experiments/user_{self.user.id}/exp_{self.id}"

    def get_workdir(self):
        return '{0}/experiments/user_{1}/exp_{2}'.format(
            self.docker.get_path(), self.user.id, self.id)

    def delete(self):
        shutil.rmtree(self.get_workdir())
        super().delete()

    def run(self):
        try:
            objects = importlib.import_module(
                '{}.protobuf_pb2'.format(self.docker.id))

            input = self.docker.elements_type.filter(kind='input').get()

            if int(input.len) > 0:
                inputs_data = ['{0}/media/user_{1}/exp_{2}/inputs/{3}'.format(
                    self.docker.workdir, self.user.id, self.id, data.value) for data in self.elements.filter(kind='input')]
            else:
                inputs_data = ['{0}/media/user_{1}/exp_{2}/inputs/{3}'.format(
                    self.docker.workdir, self.user.id, self.id, data.value) for data in self.elements.filter(kind='input')][-1]

            outputs = self.docker.elements_type.filter(kind='output')
            have_output = outputs.count() == 1

            if have_output:
                outputs_data = '{0}/media/user_{1}/exp_{2}/outputs'.format(
                    self.docker.workdir, self.user.id, self.id)

            def get_inputs_obj(values, len):
                if len > 0:
                    return objects.Inputs(inputs=(objects.Input(value=value) for value in values))
                else:
                    return objects.Inputs(inputs=objects.Input(value=values))

            def createIn(inputs, outputs, len, have_output=False):
                # print(inputs, outputs, len, have_output)
                if have_output:
                    return {'inputs': get_inputs_obj(inputs, len), 'output': objects.Output(value=outputs)}
                else:
                    return {'inputs': get_inputs_obj(inputs, len)}

            print(createIn(inputs_data, outputs_data, int(input.len), have_output))

            return objects.In(
                **createIn(inputs_data, outputs_data, int(input.len), have_output)
            )

        except ValueError as erro:
            print("############ ERROR", erro)
            return ({"progress": 0, "state": 'Error', "description": "Something unexpected happened, try again"})

class ElementData(models.Model):
    experiment = models.ForeignKey(
        Experiment, on_delete=models.CASCADE, related_name='elements')
    kind = models.CharField(max_length=30)
    element = models.ForeignKey(Element, on_delete=models.CASCADE)
    value = models.TextField(null=True)
    name = models.TextField(null=True)
    example = models.BooleanField(default=False)

    def rename_output(self):
        if self.kind == "output":
            ext = self.name.split('.')[-1]
            new_name = f"output_{self.id}"
            self.value = f"{new_name}.{ext}"
            os.rename(f"{self.experiment.outputs()}/{self.name}",
                      f"{self.experiment.outputs()}/{self.value}")
            self.save()
        else:
            raise Exception("This is not a kind output")

    def copy_input(self, to):
        print(to)
        if self.kind == "input":
            shutil.copyfile(self.get_root_path(), to)
            return to.split('/')[-1]
        else:
            raise Exception("This is not a kind input")

    def get_root_path(self):
        if self.kind == "input":
            return '{}/{}'.format(self.experiment.inputs(), self.value)
        if self.kind == "output":
            return'{}/{}'.format(self.experiment.outputs(), self.value)

    def get_public_path(self):
        if self.kind == "input":
            return f"{self.experiment.get_public_path()}/inputs/{self.value}"
        if self.kind == "output":
            return f"{self.experiment.get_public_path()}/outputs/{self.value}"
        return None

    def delete(self):
        if os.path.exists(self.get_root_path()):
            os.remove(self.get_root_path())
        super().delete()

class Records(models.Model):
    experiment = models.ForeignKey(
        Experiment, on_delete=models.CASCADE, related_name='records')
    description = models.CharField(max_length=200)
    progress = models.CharField(max_length=30)
    state = models.CharField(max_length=20, default='execute')

    def write(self):
        with open(self.experiment.get_logs(), "a") as file:
            file.write(f"{self.description}\n")


import itertools as it

s, num = input().split()

num = int(num)

s = sorted(s)

for i in range(1, num + 1):
    li = list(it.combinations(s, i))

    for j in li:
        print("".join(j))


#!/usr/bin/python
from mapnik import Image, Map, Projection, Box2d, Coord, load_map, render
import sys
import logging

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

def center_to_bbox(lat, lon, zoom, imgx, imgy):
    base = 0.000005364418  # longitude range of 1 pixel at zoom 18
    lon_half = imgx * base * (2**(18 - zoom - 1))
    lat_half = imgy * base * (2**(18 - zoom - 1))
    west = lon - lon_half
    east = lon + lon_half
    north = lat + base
    south = lat - base
    logging.info("range: bottomleft(%s, %s), topright(%s, %s)" % (west, lat - lat_half, east, lat + lat_half))
    return west, south, east, north

if __name__ == "__main__":
    mapfile = "my_styles/mapnik2normal.xml"

    map_uri = "map.png"

    lat = 49.25
    lon = 7.0
    if len(sys.argv) == 2:
        zoom = int(sys.argv[1])
    else:
        zoom = 13
    imgx = 500
    imgy = 400

    ll = center_to_bbox(lat, lon, zoom, imgx, imgy)
    m = Map(imgx, imgy)
    load_map(m, mapfile)
    prj = Projection("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m"
                     " +nadgrids=@null +no_defs +over")
    c0 = prj.forward(Coord(ll[0], ll[1]))
    c1 = prj.forward(Coord(ll[2], ll[3]))
    bbox = Box2d(c0.x, c0.y, c1.x, c1.y)
    m.zoom_to_box(bbox)
    im = Image(imgx, imgy)
    render(m, im)
    view = im.view(0, 0, imgx, imgy)  # x,y,width,height
    view.save(map_uri, 'png')


#!/usr/bin/env python3

import argparse
import datetime
import glob
import json
import os
import shutil
import sys
import subprocess
import time
import uuid

def run_command(message):
    """
    Initiate pipeline run
    input:
      message: {"base_command": "nextflow", "subcommand": "run", "command_invocation_directory": "/home/user/analysis", ...}
    output: {message}
    """
    now = datetime.datetime.now()
    today_iso8601_str = now.strftime('%Y-%m-%d')
    message['message_type'] = "command_invocation"
    message['command_invocation_id'] = str(uuid.uuid4())

    command = [
        message['base_command']
    ]

    if 'subcommand' in message:
        command.append(message['subcommand'])

    if 'positional_arguments_before_flagged_arguments' in message:
        for argument in message['positional_arguments_before_flagged_arguments']:
            command.append(argument)

    if 'flags' in message:
        for flag in message['flags']:
            command.append(flag)

    if 'flagged_arguments' in message:
        for flag, value in message['flagged_arguments'].items():
            if '$' in value:
                value = os.path.expandvars(value)
                message['flagged_arguments'][flag] = value
            if value is not None:
                command.append(flag)
                command.append(value)

    if 'positional_arguments_after_flagged_arguments' in message:
        for argument in message['positional_arguments_before_flagged_arguments']:
            command.append(argument)

    if 'positional_arguments' in message:
        for argument in message['positional_arguments']:
            command.append(argument)

    exit_code = None
    try:
        message['timestamp_command_invoked'] = datetime.datetime.now().isoformat()
        exit_code = subprocess.check_call(command, cwd=message['command_invocation_directory'])
    except subprocess.CalledProcessError as e:
        sys.stderr.write("Error running command: " + " ".join(command))

    message['timestamp_command_invocation_completed'] = datetime.datetime.now().isoformat()
    message['command_exit_code'] = exit_code

    return message

def remove_nextflow_work_dir(message):
    if '-work-dir' in message['flagged_arguments']:
        work_dir_path = message['flagged_arguments']['-work-dir']
    else:
        work_dir_path = os.path.join(message['command_invocation_directory'], 'work')
    try:
        time.sleep(10) # wait for filesystem latency to settle
        shutil.rmtree(work_dir_path)
    except Exception as e:
        raise(e)

def remove_nextflow_logs(message):
    nextflow_logs = glob.glob(os.path.join(message['command_invocation_directory'], ".nextflow*"))
    for log in nextflow_logs:
        try:
            if os.path.isdir(log):
                shutil.rmtree(log)
            elif os.path.isfile(log):
                os.remove(log)
            else:
                pass
        except Exception as e:
            raise(e)

def nextflow_cleanup(message):
    cleanup_options = message['cleanup_options']
    if 'remove_command_tmp_directories' in cleanup_options and cleanup_options['remove_command_tmp_directories']:
        remove_nextflow_work_dir(message)

    if 'remove_command_logs' in cleanup_options and cleanup_options['remove_command_logs']:
        remove_nextflow_logs(message)

def cleanup(message):
    if 'base_command' in message and message['base_command'] == 'nextflow':
        nextflow_cleanup(message)

def main():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    args = parser.parse_args()

    for line in sys.stdin:
        try:
            message = json.loads(line.rstrip())
            if message['message_type'] == 'command_creation':
                message = run_command(message)
                cleanup(message)
            elif message['message_type'] == 'sentinel':
                pass
            print(json.dumps(message))
        except Exception as e:
            cleanup(message)

if __name__ == '__main__':
    main()


from hivclinic import db
from hivclinic.models import BaseModel
from sqlalchemy.dialects.postgresql import ARRAY, UUID

class VisitModel(BaseModel):
    __tablename__ = "visit"
    relationship_keys = {"patientID"}
    # protected_keys = {"medications"}

    date = db.Column(db.Date(), nullable=False)

    bodyWeight = db.Column(db.Float())

    historyOfContactWithTB = db.Column(db.Boolean())

    ARTAdherenceScale = db.Column(db.Float())
    ARTAvgDelayedDosing = db.Column(db.Float())
    ARTAdherenceProblem = db.Column(db.Unicode())

    impression = db.Column(ARRAY(db.Unicode()))

    arvMedications = db.Column(ARRAY(db.Unicode()))
    whySwitchingARV = db.Column(db.Unicode())

    tbMedications = db.Column(ARRAY(db.Unicode()))
    oiMedications = db.Column(ARRAY(db.Unicode()))

    medications = db.Column(ARRAY(db.Unicode()))

    # parent id
    patientID = db.Column(UUID(as_uuid=True), db.ForeignKey("patient.id"))


from dataclasses import dataclass
from typing import Any, Dict

from valsys.modeling.client.socket_handler import SocketHandler

from .exceptions import ModelingServiceGetException

@dataclass
class ModelingServiceSocketClient:
    auth_token: str = ""
    error: Any = None

    def run(self, url: str, data: Dict[str, str] = None, after_token=None):
        handler = SocketHandler(url=url,
                                config=data,
                                auth_token=self.auth_token,
                                trace=False,
                                after_token=after_token)

        handler.run()

        while True:
            if not handler.complete:
                continue
            if handler.error is not None:
                self.error = handler.error
                raise Exception(f"{handler.error}")
            elif handler.resp is not None:
                return handler.resp
            break

    def get(self, url: str, data: Dict[str, str] = None, after_token=None):
        return self.run(url, data, after_token=after_token)

    def post(self, url: str, data: Dict[str, str] = None, after_token=None):
        return self.run(url, data, after_token=after_token)


from app import app
from model.auth_model import auth_model
from model.users_roles_model import users_roles
from flask import make_response, request
import traceback

import json

obj = users_roles()
auth = auth_model()

@app.route("/users_roles", methods=["GET"])
# @auth.token_auth()
def get_all_users_roles():
    try:
        return obj.get_all_users_roles()
    except Exception as e:
        traceback.print_exc()
        return make_response(f"error in getAll users roles controller: {e}", 204)

@app.route("/users_roles/<int:user_id>", methods=["GET"])
# @auth.token_auth()
def get_user_roles(user_id):
    try:
        return obj.get_user_roles(user_id)
    except Exception as e:
        traceback.print_exc()
        return make_response(f"error in get user roles controller: {e}", 204)

@app.route("/user_roles", methods=["POST"])
# @auth.token_auth()
def add_new_user_roles():
    try:
        return obj.add_new_user_roles(request.data)
    except Exception as e:
        traceback.print_exc()
        return make_response(f"Error in add new user roles controller : {e}", 204)

@app.route("/user_role", methods=["POST"])
# @auth.token_auth()
def add_new_user_role():
    try:
        return obj.add_new_user_role(request.data)
    except Exception as e:
        traceback.print_exc()
        return make_response(f"Error in add new user role controller : {e}", 204)

@app.route("/users_roles/<int:user_id>", methods=["DELETE"])
# @auth.token_auth()
def delete_user_roles(user_id):
    try:
        return obj.delete_user_roles(user_id)
    except Exception as e:
        return make_response(f"Error in deleting users_roles controller : {e}", 204)

@app.route("/users_roles/<int:user_id>", methods=["PUT"])
# @auth.token_auth()
def update_role_user_relationship(user_id):
    try:
        data = request.get_json()  # Get the data from the request body
        data_str = json.dumps(data)  # Convert the data to a JSON string
        return obj.update_role_user_relationship(user_id, data_str)  # Pass the JSON string to the method
    except Exception as e:
        return make_response(f"Error in updating user roles: {e}", 500)