from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _
from django import forms

from administrativo.models import Edificio, Departamento

# Form Edificio
class EdificioForm(ModelForm):
    class Meta:
        model = Edificio
        fields = ['nombre', 'direccion', 'ciudad', 'tipo']
        labels = {
            'nombre': _('Ingrese el nombre del Edificio por favor'),
            'direccion': _('Ingrese la direccion por favor'),
            'ciudad': _('Ingrese la ciudad por favor'),
            'tipo': _('Ingrese el tipo de Edificio por favor'),
        }

# Form Departamento
class DepartamentoForm(ModelForm):
    class Meta:
        model = Departamento
        fields = ['propietario', 'costo', 'numCuartos', 'edificio']

# Form DepartamentoEdificio
class DepartamentoEdificioForm(ModelForm):

    def __init__(self, edificio, *args, **kwargs):
        super(DepartamentoEdificioForm, self).__init__(*args, **kwargs)
        self.initial['edificio'] = edificio
        self.fields["edificio"].widget = forms.widgets.HiddenInput()
        print(edificio)

    class Meta:
        model = Departamento
        fields = ['propietario', 'costo', 'numCuartos', 'edificio']


#!/usr/bin/python3
import io
import grpc
from PIL import Image
import argparse
import logging

from image_pb2 import (
    NLImage,
    NLImageRotateRequest,
)
import image_pb2_grpc

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

class NLImageClient():
    def __init__(self, host: str, port: int):
        # open connection to server
        self.channel = grpc.insecure_channel(f"{host}:{port}")
        logger.info(f"Connected to {host}:{port}")
        # bind the client and the server
        self.stub = image_pb2_grpc.NLImageServiceStub(self.channel)

    def rotate_image(self, image_filepath: str, rotation_string: str):
        rotation_map = {
            "NONE" : NLImageRotateRequest.Rotation.NONE,
            "NINETY_DEG" : NLImageRotateRequest.Rotation.NINETY_DEG,
            "ONE_EIGHTY_DEG" : NLImageRotateRequest.Rotation.ONE_EIGHTY_DEG,
            "TWO_SEVENTY_DEG" : NLImageRotateRequest.Rotation.TWO_SEVENTY_DEG
        }

        image = Image.open(image_filepath)
        # retrieve binary content (bytes) of image
        image_bytes = io.BytesIO()
        image.save(image_bytes, format=image.format)

        request = NLImageRotateRequest(
            image = NLImage(
                color = image.mode == "RGB",
                data = image_bytes.getvalue(),
                width = image.width,
                height = image.height
            ),
            rotation = rotation_map[rotation_string]
        )
        try:
            return self.stub.RotateImage(request)
        except grpc.RpcError as e:
            logger.error(f"{e.code()}: {e.name}={e.value}. {e.details()}")

    def mean_image(self, image_filepath: str):
        image = Image.open(image_filepath)
        image_bytes = io.BytesIO()
        image.save(image_bytes, format=image.format)

        nl_image = NLImage(
            color = image.mode == "RGB",
            data = image_bytes.getvalue(),
            width = image.width,
            height = image.height
        )
        try:
            return self.stub.MeanFilter(nl_image)
        except grpc.RpcError as e:
            logger.error(f"{e.code()}: {e.name}={e.value}. {e.details()}")

    def save_image(self, image: NLImage, output_filepath: str):
        image = Image.open(io.BytesIO(image.data))
        image.save(output_filepath)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Configure host and port for server")
    parser.add_argument("--input",type=str,required=True)
    parser.add_argument("--output",type=str,required=True)
    parser.add_argument("--host",type=str,required=True)
    parser.add_argument("--port",type=int,required=True)

    parser.add_argument("--rotate",type=str)
    parser.add_argument("--mean", action="store_true")

    args = parser.parse_args()
    if args.rotate is None and not args.mean:
        parser.error("Must specify rotate,mean or both")
    else:
        client = NLImageClient(args.host,args.port)
        if args.rotate:
            image = client.rotate_image(args.input,args.rotate)
            client.save_image(image,args.output)
        if args.mean:
            if args.rotate:
                image_filepath = args.output
            else:
                image_filepath = args.input
            image = client.mean_image(image_filepath)
            client.save_image(image,args.output)
        logger.info(f"Saving image to {args.output}")


import ast
import argparse
import pathlib
import sys

from typing import TypedDict, Optional

import find_py_imports.visitor as v
import find_py_imports.formatter as fmt

class _Err(TypedDict):
        filename: Optional[str]
        error: Exception

def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('-n', '--line-number', action='store_true', dest='show_line_number',
        help='show line number')
    parser.add_argument('--hide-syntax-error', action='store_true',dest='hide_syntax_err',
        help='do not print syntax error print to stderr')
    parser.add_argument('--print-file-not-found-error', action='store_true', dest='print_file_not_found_err',
        help='print not founded files to stderr')
    parser.add_argument('file', nargs='*', type=str, help='filename(s)')

    args = parser.parse_args()
    show_line_number: bool = args.show_line_number
    print_syntax_err: bool = not args.hide_syntax_err
    print_file_not_found_err: bool = args.print_file_not_found_err
    files: list[str] = args.file
    filenames_are_provided = True

    formatter = None
    pred = (1 < len(files), show_line_number)  # test [multiple_file?, show line_number?]
    if pred == (False, False):
        formatter = fmt.SimpleFormatter()
    elif pred == (False, True):
        formatter = fmt.LinenoFormatter()
    elif pred == (True, False):
        formatter = fmt.FilenameFormatter()
    elif pred == (True, True):
        formatter = fmt.FilenameLinenoFormatter()
    else:
        raise Exception

    # if no filename is provided then use input chars from stdin as content of a file
    if len(files) < 1:
        filenames_are_provided = False

    result: list[fmt.Record] = []
    def save_result(fn: str, stmt: v.ImportStatement):
        result.append(fmt.Record(fn, stmt))

    errs: list[_Err] = []
    if filenames_are_provided:
        # interpret input as filename
        for fn in files:
            try:
                if not pathlib.Path(fn).exists():
                    raise FileNotFoundError(fn)
                with open(fn) as fp:
                    tree = ast.parse(fp.read())
                    visitor = v.MyNodeVisitor(fn, save_result)
                    visitor.visit(tree)
            except (FileNotFoundError, SyntaxError) as e:
                errs.append({
                    'filename': fn,
                    'error': e,
                })
    else:
        # interpret input as code string
        try:
            tree = ast.parse(sys.stdin.read())
            visitor = v.MyNodeVisitor(None, save_result)
            visitor.visit(tree)
        except (FileNotFoundError, SyntaxError) as e:
            errs.append({
                'filename': None,
                'error': e,
            })

    for r in result:
        print(formatter.format(r))

    for e in errs:
        if print_syntax_err:
            s = ''
            if 1 < len(files):
                s = f' at {e["filename"]}'
            sys.stderr.write(f'syntax error{s}: {e["error"]}\n')
        if print_file_not_found_err:
            sys.stderr.write(f'no such file: {e["error"]}\n')

if __name__ == '__main__':
    main()


import torch
from utils import *
import tqdm
import pytorch_ssim

def purify_imagenet(x, diffusion,model,  max_iter, mode, config):
    # From noisy initialized image to purified image
    images_list = []
    transform_raw_to_diff = raw_to_diff(config.structure.dataset)
    transform_diff_to_raw = diff_to_raw(config.structure.dataset)
    x_adv = transform_raw_to_diff(x).to(config.device.diff_device)
    x_adv = torch.nn.functional.interpolate(x_adv, size=[256, 256], mode="bilinear")  # transfrom size 224 -> 256

    t_steps = torch.ones(x_adv.shape[0], device=config.device.diff_device).long()
    t_steps = t_steps * (config.purification.purify_step-1)
    shape = list(x_adv.shape)
    model_kwargs = {}

    def cond_fn(x_reverse_t, t):
        """
        Calculate the grad of guided condition.
        """
        print(f'cond_fn{t}')
        with torch.enable_grad():
            x_in = x_reverse_t.detach().requires_grad_(True)

            # x_adv_t = diffusion.q_sample(x_adv, t)
            x_adv_t = x_adv
            # scale = exp(config.purification.guide_exp_a * t / config.purification.purify_step+config.purification.guide_exp_b) + config.purification.guide_scale_base
            if config.purification.guide_mode == 'MSE':
                selected = -1 * F.mse_loss(x_in, x_adv_t)
                scale = diffusion.compute_scale(x_in,t, config.attack.ptb*2/255. / 3. / config.purification.guide_scale)
            elif config.purification.guide_mode == 'SSIM':
                selected = pytorch_ssim.ssim(x_in, x_adv_t)
                scale = diffusion.compute_scale(x_in,t, config.attack.ptb*2/255. / 3. / config.purification.guide_scale)
            elif config.purification.guide_mode == 'CONSTANT':
                selected = pytorch_ssim.ssim(x_in, x_adv_t)
                scale = config.purification.guide_scale
            return torch.autograd.grad(selected.sum(), x_in)[0] * scale

    with torch.no_grad():
        images = []
        xt_reverse = x_adv
        for i in range(max_iter):
            adv_sample = diffusion.q_sample(xt_reverse,t_steps)
            sample_fn = diffusion.p_sample_loop if not config.net.use_ddim else diffusion.ddim_sample_loop
            xt_reverse = sample_fn(
                    model,
                    shape,
                    num_purifysteps = config.purification.purify_step,
                    noise = adv_sample,
                    clip_denoised=config.net.clip_denoised,
                    cond_fn = cond_fn if config.purification.cond else None,
                    model_kwargs=model_kwargs,
                )
            x_pur_t = xt_reverse.clone().detach()
            x_pur = torch.clamp(transform_diff_to_raw(x_pur_t), 0.0, 1.0)
            x_pur = torch.nn.functional.interpolate(x_pur, size=[224, 224], mode="bilinear") # transfrom size 256 -> 224
            images.append(x_pur)

    return images


import season
import dizest
import os
import time
import psutil
import platform
import resource
import json

server_id = "main-" + wiz.session.get("id")
dbname = "workflow"

def health():
    process = psutil.Process(os.getpid())

    data = season.stdClass()
    data.cpu = season.stdClass()
    data.cpu.count = psutil.cpu_count()
    data.cpu.usage = psutil.cpu_percent()

    hdd = psutil.disk_usage('/')
    data.disk = season.stdClass()
    data.disk.total = hdd.total
    data.disk.used = hdd.used

    data.memory = season.stdClass()
    data.memory.usage = psutil.virtual_memory().used
    data.memory.total = psutil.virtual_memory().total

    wiz.response.status(200, data)

def workflow():
    user_id = wiz.session.get("id")
    db = wiz.model("orm").use(dbname)
    rows = db.rows(user_id=user_id, fields="id,title,updated", orderby="updated", order="DESC")

    dizest = wiz.model("dizest").load(server_id)
    server = dizest.server()

    wps = []
    for row in rows:
        wpid = row['id']
        wp = server.workflow_by_id(wpid)
        if wp is None: continue
        status = wp.status()
        if status == 'stop': continue
        row = db.get(id=wpid)
        row['status'] = status
        wps.append(row)

    wiz.response.status(200, wps)

def status():
    workflow_ids = wiz.request.query("id", True)
    workflow_ids = json.loads(workflow_ids)
    res = dict()

    dizest = wiz.model("dizest").load(server_id)
    server = dizest.server()

    for workflow_id in workflow_ids:
        workflow = server.workflow_by_id(workflow_id)
        status = "stop"
        if workflow is not None:
            status = workflow.status()
        res[workflow_id] = status
    wiz.response.status(200, res)

def stop():
    user_id = wiz.session.get("id")
    db = wiz.model("orm").use(dbname)
    rows = db.rows(user_id=user_id, fields="id")

    dizest = wiz.model("dizest").load(server_id)
    server = dizest.server()

    for row in rows:
        wpid = row['id']
        wp = server.workflow_by_id(wpid)
        if wp is None: continue
        status = wp.status()
        if status == 'stop': continue
        wp.kill()

    wiz.response.status(200)


import numpy as np
import open3d as o3d

max_pieces = 1000000
input_path = "./xyz_file/"
output_path = "./models/"

def lod_mesh_export(mesh, lods, extension, path):
    mesh_lods={}
    for i in lods:
        mesh_lod = mesh.simplify_quadric_decimation(i)
        o3d.io.write_triangle_mesh(path+"lod_"+str(i)+extension, mesh_lod)
        mesh_lods[i]=mesh_lod
    print("generation of "+str(i)+" LoD successful")
    return mesh_lods

def generateMesh(day):
    dataname = "output_" + str(day) + ".xyz"
    point_cloud = np.loadtxt(input_path + dataname, skiprows=0)
    print(point_cloud.shape)
    # print(point_cloud)

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(point_cloud[:,:3])
    pcd.colors = o3d.utility.Vector3dVector(point_cloud[:,3:6]/255)
    pcd.normals = o3d.utility.Vector3dVector(point_cloud[:,6:9])
    #
    o3d.visualization.draw_geometries([pcd])

    # distances = pcd.compute_nearest_neighbor_distance()
    # avg_dist = np.mean(distances)
    # print(avg_dist)
    # radius = 2*avg_dist
    #
    # bpa_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(pcd,o3d.utility.DoubleVector([radius, radius * 2]))
    #
    # dec_mesh = bpa_mesh.simplify_quadric_decimation(max_pieces)
    #
    # print("去除重复...")
    # dec_mesh.remove_degenerate_triangles()
    # dec_mesh.remove_duplicated_triangles()
    # dec_mesh.remove_duplicated_vertices()
    # dec_mesh.remove_non_manifold_edges()
    #
    # print("正在写入...")
    # o3d.io.write_triangle_mesh(output_path+"bpa_mesh_"+str(day)+".ply", dec_mesh)

def show(day):
    bpa_mesh = o3d.io.read_triangle_mesh(output_path+"bpa_mesh_"+str(day)+".ply")
    my_lods = lod_mesh_export(bpa_mesh, [max_pieces], ".ply", output_path)
    o3d.visualization.draw_geometries([my_lods[max_pieces]])

if __name__ == '__main__':
    generateMesh(0)
    # show(0)


import torch
import argparse
import numpy as np
import torch.nn as nn
from torch.autograd import Variable

from physicsengine import CDFdata

class Fmodel(nn.Module):
    def __init__(self):
        super(Fmodel,self).__init__()
        self.statefc = nn.Sequential(
            nn.Linear(2,16),
            nn.ReLU(),
            nn.Linear(16,16),
            nn.ReLU(),
            nn.Linear(16,32)
        )
        self.actionfc = nn.Sequential(
            nn.Linear(2, 16),
            nn.ReLU(),
            nn.Linear(16, 16),
            nn.ReLU(),
            nn.Linear(16, 32)
        )
        self.predfc = nn.Sequential(
            nn.Linear(64,32),
            nn.ReLU(),
            nn.Linear(32,16),
            nn.ReLU(),
            nn.Linear(16,2)
        )

    def forward(self, state,action):
        state_feature = self.statefc(state)
        action_feature = self.actionfc(action)
        feature = torch.cat((state_feature,action_feature),1)
        return self.predfc(feature)

class Amodel(nn.Module):
    def __init__(self):
        super(Amodel,self).__init__()
        self.stn = nn.Sequential(
            nn.Linear(2,16),
            nn.ReLU(),
            nn.Linear(16,32),
            nn.ReLU(),
            nn.Linear(32,4)
        )
        self.actionfc = nn.Sequential(
            nn.Linear(2, 16),
            nn.ReLU(),
            nn.Linear(16, 32),
            nn.ReLU(),
            nn.Linear(32, 2)
        )

    def forward(self, action):
        feature = torch.ones_like(action).float().cuda()
        trans = self.stn(feature).view(-1,2,2)
        # iden = Variable(torch.from_numpy(np.array([1, 0, 0, 1]).astype(np.float32))).\
        #                   view(1, 4).repeat(action.shape[0], 1).view(-1,2,2).cuda()
        # trans = trans + iden
        # trans = Variable(torch.from_numpy(np.array([1/0.6, 0, 0, 1/0.6]).astype(np.float32))).\
        #                   view(1, 4).repeat(action.shape[0], 1).view(-1,2,2).cuda()

        action = torch.bmm(trans,action.unsqueeze(-1))
        return action.squeeze(-1)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='toy experiments')
    parser.add_argument('--istrain', type=bool, default=True, help='whether training or test')
    parser.add_argument('--epoch_size', type=int, default=100, help='epoch size')
    parser.add_argument('--batch_size', type=int, default=16, help='batch size')
    parser.add_argument('--exp_id', type=int, default=1, help='experiment id')
    parser.add_argument('--test_id', type=int, default=1, help='dataset test id')
    parser.add_argument('--dynamic',type=bool,default=True,help='whether to use dynamic model')
    parser.add_argument('--clockwise', type=bool, default=False, help='dynamic direction')
    parser.add_argument('--domain',type=str,default='circle',help='the distribution of points')

    parser.add_argument('--loss', type=str, default='l2', help='loss function')
    parser.add_argument('--imgA_id', type=int, default=1, help='datasetA from view1')
    parser.add_argument('--imgB_id',type=int,default=2,help='datasetB from view2')
    parser.add_argument('--data_root', type=str, default='./tmp/data', help='logs root path')

    opt = parser.parse_args()

    model = Fmodel().cuda()
    dataset = CDFdata.get_loader(opt)
    optimizer = torch.optim.Adam(model.parameters())
    loss_fn = nn.L1Loss()

    for epoch in range(100):
        for i,item in enumerate(dataset):
            state,action,result = item[1]
            state = state.float().cuda()
            action = action.float().cuda()
            result = result.float().cuda()

            out = model(state,action)
            loss = loss_fn(out,result)
            # loss = loss_fn(result*0.5,(state*0.5+action*0.05))
            print(epoch,i,loss.item())

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        torch.save(model.state_dict(),'./pred.pth')


# position to mouse.
# shooter game test.
# there are no sounds because gunshot sounds are too loud and damaging to hearing.

# run pygame.
import pgzrun

# initializing.
import pygame
pygame.init()
pygame.display.set_caption("Gunfire Game")
WIDTH = 1200
HEIGHT = 600

# variables declaration.
count_score = 0
message = "—"

# gun image.
# photo attribution:
#  PNGs by Vecteezy
ak47 = Actor("ak47", center = (250, 300))

# adjust angle
ak47.angle -= 45

# target image.
target = Actor("target", center = (1000, 300))

def draw():
    # global screen
    # clear screen, bgcolor = white,
    # draw ak47, draw target, draw console.
    screen.clear()
    screen.fill("white")
    ak47.draw()
    target.draw()
    rect = Rect(0, 525, 1200, 75)
    screen.draw.filled_rect(rect, "white")
    screen.draw.textbox(message, rect, color = "black", center = (600, 562.5),
                        align = "center")

def on_mouse_down(pos):
    # global variables.
    global count_score, message

    # change to gunfire picture, adjust angle.
    ak47.image = "ak47_fire"
    ak47.angle = ak47.angle_to(pos)
    ak47.angle -= 45

    # determine if shot on target.
    if ak47.angle >= -49.7 and ak47.angle <= -38:
        count_score += 1
        message = ("Good shot! Your score is " + str(count_score))
    else:
        count_score -= 1
        message = ("Oof, your score is " + str(count_score))

def on_mouse_up(pos):
    # change to regular image, adjust angle.
    ak47.image = "ak47"
    ak47.angle = ak47.angle_to(pos)
    ak47.angle -= 45

def on_mouse_move(pos):
    # adjust angle.
    ak47.angle = ak47.angle_to(pos)
    ak47.angle -= 45

# update text.
pygame.display.update()

# run pygame.
pgzrun.go()


import math

import kivy
from kivy.config import Config
Config.set('graphics', 'width', '900')
Config.set('graphics', 'height', '400')
from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy import platform
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import Clock
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Line
from kivy.graphics.vertex_instructions import Quad, Triangle, Rectangle
from kivy.graphics.vertex_instructions import Ellipse
import random
from kivy.properties import (
    NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint

class FinalGame(Widget):
    perspective_point_x = NumericProperty(0)
    perspective_point_y = NumericProperty(0)
    vertical_lines = []
    v_lines_num = 6
    v_lines_spacing = 0.5
    horizontal_lines = []
    h_lines_num = 10
    h_lines_spacing = 0.15
    current_offset_y = 0
    speed_y = 0.5
    current_offset_x = 0
    current_y_loop = 0
    speed_x = 3
    current_speed_x = 0
    num_tiles = 300
    tiles = []
    tiles_coordinates = []

    score = NumericProperty(0)

    def __init__(self,**kwargs):
        super(FinalGame,self).__init__(**kwargs)
        self.init_vertical_line()
        self.init_horizontal_line()
        self.init_tiles()

        self.generate_tiles_coordinates()

        if self.is_desktop():
            self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
            self._keyboard.bind(on_key_down=self._on_keyboard_down)
            self._keyboard.bind(on_key_up=self._on_keyboard_up)
        Clock.schedule_interval(self.update, 1.0/60.0)
    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard.unbind(on_key_up=self._on_keyboard_up)

        self._keyboard = None

    def is_desktop(self):
        if platform in ("linux", "win", "macosx"):
            return True
        else:
            return False
    def init_tiles(self):
        with self.canvas:
            Color(1,1,1)
            for i in range(0, self.num_tiles):
                self.tiles.append(Quad())
    def pre_fill_tiles_coordinates(self):
        for i in range(0, 10):
            self.tiles_coordinates.append((0, i))
    def generate_tiles_coordinates(self):
        last_y = 0
        for i in range(len(self.tiles_coordinates)-1, -1, -1):
            if self.tiles_coordinates[i][1] < self.current_y_loop:
                del self.tiles_coordinates[i]
        if len(self.tiles_coordinates) > 0:
            last_coordinates = self.tiles_coordinates[-1]
            last_y = last_coordinates[1] + 1
        for i in range(len(self.tiles_coordinates), self.num_tiles):
            r = random.randint(-(int(self.v_lines_num/2)-1),(int(self.v_lines_num/2)-1))
            self.tiles_coordinates.append((r, last_y))
            last_y += 1
            #self.v_lines_num += 2
    def init_vertical_line(self):
        with self.canvas:
            Color(1,1,1)
            for i in range(0,self.v_lines_num):
                self.vertical_lines.append(Line())
    def get_line_x_from_index(self, index):
        start_index = -int(self.v_lines_num/2)
        central_line_x = self.perspective_point_x
        spacing = self.v_lines_spacing * self.width
        offset = index - 0.5
        line_x = int(central_line_x + offset * spacing + self.current_offset_x)
        return line_x
    def get_line_y_from_index(self, index):
        spacing_y = self.h_lines_spacing * self.height
        line_y = index * spacing_y - self.current_offset_y
        return line_y
    def get_tile_coordinates(self, ti_x, ti_y):
        ti_y = ti_y - self.current_y_loop
        x = self.get_line_x_from_index(ti_x)
        y = self.get_line_y_from_index(ti_y)
        return x, y
    def update_tiles(self):
        for i in range(0, self.num_tiles):
            tile = self.tiles[i]
            tile_coordinates = self.tiles_coordinates[i]
            xmin, ymin = self.get_tile_coordinates(tile_coordinates[0], tile_coordinates[1])
            xmax, ymax = self.get_tile_coordinates(tile_coordinates[0]+1, tile_coordinates[1]+1)
            x1, y1 = self.transform(xmin, ymin)
            x2, y2 = self.transform(xmin, ymax)
            x3, y3 = self.transform(xmax, ymax)
            x4, y4 = self.transform(xmax, ymin)
            tile.points = [x1,y1,x2,y2,x3,y3,x4,y4]

    def update_vertical_lines(self):
        start_index = -int(self.v_lines_num/2)+1
        for i in range(start_index,start_index+self.v_lines_num):
            line_x = self.get_line_x_from_index(i)
            x1,y1 = self.transform(line_x,0)
            x2,y2 = self.transform(line_x,self.height)
            self.vertical_lines[i].points=[x1,y1,x2,y2 ]
    def init_horizontal_line(self):
        with self.canvas:
            Color(1,1,1)
            for i in range(0,self.h_lines_num):
                self.horizontal_lines.append(Line())
    def update_horizontal_lines(self):
        start_index = -int(self.v_lines_num / 2) + 1
        end_index = start_index + self.v_lines_num - 1
        xmin = self.get_line_x_from_index(start_index)
        xmax = self.get_line_x_from_index(end_index)
        for i in range(0,self.h_lines_num):
            line_y = self.get_line_y_from_index(i)
            x1,y1 = self.transform(xmin, line_y)
            x2,y2 = self.transform(xmax, line_y)
            self.horizontal_lines[i].points=[x1,y1,x2,y2 ]
    def transform(self,x,y):
        #return self.transform_2D(x,y)
        return self.transform_perspective(x,y)
    def transform_2D(self,x,y):
        return int(x),int(y)
    def transform_perspective(self,x,y):
        lin_y = y * self.perspective_point_y / self.height
        if lin_y > self.perspective_point_y:
            lin_y = self.perspective_point_y
        diff_x = x - self.perspective_point_x
        diff_y = self.perspective_point_y - lin_y
        factor_y = diff_y/self.perspective_point_y
        factor_y = pow(factor_y, 2)
        tr_x = self.perspective_point_x + diff_x*factor_y
        tr_y = self.perspective_point_y - factor_y * self.perspective_point_y
        return int(tr_x), int(tr_y)
    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        if keycode[1] == 'left':
            self.current_speed_x = self.speed_x
        elif keycode[1] == 'right':
            self.current_speed_x = -self.speed_x
        return True
    def _on_keyboard_up(self, keyboard, keycode):
        self.current_speed_x = 0
        return True
    def on_touch_down(self, touch):
        if touch.x < self.width/2:
            self.current_speed_x = self.speed_x
        else:
            self.current_speed_x = -self.speed_x
    def on_touch_up(self, touch):
        self.current_speed_x = 0

    def update(self, dt):
        self.update_vertical_lines()
        self.update_horizontal_lines()
        self.update_tiles()
        time_factor = dt*60
        speed = self.speed_y*self.height/100
        self.current_offset_y += speed*time_factor
        spacing_y = self.h_lines_spacing*self.height
        if self.current_offset_y >= spacing_y:
            self.current_offset_y -= spacing_y
            self.current_y_loop += 1
            self.generate_tiles_coordinates()
        speed2 = self.current_speed_x*self.width/100
        self.current_offset_x += speed2*time_factor

class FinalApp(App):
    def build(self):
        game = FinalGame()

        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game

if __name__ == '__main__':
    FinalApp().run()

# References: FreeCodeCamp


from aiogram import Router, types
from aiogram.filters import Command
from aiogram.types import FSInputFile

from loguru import logger

router = Router()

# Обработчик команды /price
@router.message(Command(commands="price"))
async def prices_handler(message: types.Message) -> None:
    user_id: str = str(message.from_user)
    price_photo = FSInputFile("images/price.png")
    try:
        await message.answer_photo(photo=price_photo)
    except Exception as error:
        logger.debug(f"{error}: Trouble id: {user_id}")
        return

# Обработчик команды /worktime
@router.message(Command(commands="worktime"))
async def worktime_handler(message: types.Message) -> None:
    user_id: str = str(message.from_user)
    worktime_photo = FSInputFile("images/worktime.png")
    try:
        await message.answer_photo(photo=worktime_photo)
    except Exception as error:
        logger.debug(f"{error}: Trouble id: {user_id}")
        return

# Обработчик команды /location
@router.message(Command(commands="location"))
async def how_to_get_handler(message: types.Message) -> None:
    user_id: str = str(message.from_user)
    location_photo = FSInputFile("images/location.jpg")
    try:
        await message.answer_photo(photo=location_photo)
    except Exception as error:
        logger.debug(f"{error}: Trouble id: {user_id}")
        return


import gzip
from optparse import OptionParser

#from matplotlib import rc
import matplotlib.pyplot as plt

class Grapher(object):
    def __init__(self, inputfile, outputfile, title, xlabel, ylabel):
        self.inputfile = inputfile
        self.outputfile = outputfile

        self.title = title
        self.xlabel = xlabel
        self.ylabel = ylabel

    def read_values(self, inputfile):
        xs = []
        ys = []

        with gzip.open(inputfile, 'r') as inputfile:
            for line in inputfile:
                x, y = map(int, line.strip().split('\t'))
                xs.append(x)
                ys.append(y)

        return xs, ys

    def run(self):
        xs, ys = self.read_values(self.inputfile)

        fig = plt.figure(1, figsize=(8, 8))
        ax = fig.add_subplot(111)
        # rc('font', **{'family': 'serif', 'serif': ['Palatino']})
        # rc('text', usetex=True)

        ax.set_xscale('log')
        ax.set_yscale('log')
        ax.plot(xs, ys, 'rx')

        if self.ylabel:
            ax.set_ylabel(self.ylabel)
        if self.xlabel:
            ax.set_xlabel(self.xlabel)
        if self.title:
            ax.set_title(title)

        fig.savefig(self.outputfile, bbox_inches='tight')
        plt.close()

if __name__ == "__main__":
    op = OptionParser()
    op.add_option("-i", "--input", dest="input", metavar="FILE",
                  help="Input file")
    op.add_option("--ylabel", dest="ylabel", default=None,
                  help="Label on the y-axis")
    op.add_option("--xlabel", dest="xlabel", default=None,
                  help="Label on the x-axis")
    op.add_option("--title", dest="title", default=None,
                  help="Title for the graph")
    op.add_option("-o", "--output", dest="output", metavar="FILE",
                  help="Output file")

    (opts, args) = op.parse_args()

    Grapher(
        opts.input,
        opts.output,
        opts.title,
        opts.xlabel,
        opts.ylabel
    ).run()


# 나이트의 이동 https://www.acmicpc.net/problem/7562
"""
나이트가 한 번에 이동할 수 있는 칸
dx = [-1, 1, -2, 2, -2, -1, 1, 2]
dy = [2, 2, 1, 1, -1, -2, -2, -1]
나이트가 이동하려고 하는 칸이 주어진다.
나이트는 몇 번 움직이면 이 칸으로 이동할 수 있을까?
"""
import sys
from collections import deque

dx = [-1, 1, -2, 2, -2, -1, 1, 2]
dy = [2, 2, 1, 1, -1, -2, -2, -1]

for _ in range(int(sys.stdin.readline())):
    length = int(sys.stdin.readline()) # 체스판 한 변의 길이
    curr = tuple(map(int, sys.stdin.readline().split())) # 현재 위치
    goal = tuple(map(int, sys.stdin.readline().split())) # 목표 위치

    count = 0 # 최소 몇 번만에 이동할 수 있는지
    visited = [[-1 for _ in range(length)] for _ in range(length)]
    queue = deque([curr])
    visited[curr[0]][curr[1]] = 0 # 시작 위치는 0으로 둠

    while queue:
        x, y = queue.popleft()

        if x == goal[0] and y == goal[1]:
            count = visited[x][y]
            break

        for i in range(8):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < length and 0 <= ny < length:
                if visited[nx][ny] == -1:
                    queue.append((nx, ny))
                    visited[nx][ny] = visited[x][y] + 1

    print(count)


import numpy as np
import pandas as pd

import skill_metrics as sm

from scipy.stats import t
from scipy.stats import ttest_ind

def concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,Var,series):

    """
    concat_tables_and_loc
    =====================

    Esta função concatena um dicionário de tabelas do SCANTEC em um único dataframe e
    retorna uma lista com as séries das variáveis e experimentos escolhidos.

    Parâmetros de entrada
    ---------------------
        dTable      : objeto dicionário com uma ou mais tabelas do SCANTEC;
        dataInicial : objeto datetime com a data inicial do experimento;
        dataFinal   : objeto datetime com a data final do experimento;
        Exps        : lista com os nomes das estatísticas a serem processadas;
        Var         : nome da variável na tabela de correlação de anomalia do SCANTEC;
        series      : valor Booleano para combinar as curvas dos experimentos em um só gráfico.

    Resultado
    ---------
        Lista com as séries das variáveis e experimentos escolhidos.

    Uso
    ---
        import scanplot

        data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")

        dataInicial = data_conf["Starting Time"]
        dataFinal = data_conf["Ending Time"]
        Vars = list(map(data_vars.get,[*data_vars.keys()]))
        Var = Vars[0][0].lower()
        Stats = ["ACOR", "RMSE", "VIES"]
        Exps = list(data_conf["Experiments"].keys())
        outDir = data_conf["Output directory"]

        dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)

        varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,Var,series=False)
    """

    datai_fmt = dataInicial.strftime("%Y%m%d%H")
    dataf_fmt = dataFinal.strftime("%Y%m%d%H")

    cTable = pd.concat(dTable, axis=0, join='outer', ignore_index=False, keys=None, sort=True)

    varlev_exps = []

    if series:

        for exp in Exps:

            fname_exp_datai = 'ACOR' + str(exp) + '_' + datai_fmt + datai_fmt + 'T.scan'
            fname_exp_dataf = 'ACOR' + str(exp) + '_' + dataf_fmt + dataf_fmt + 'T.scan'

            varlev_dia_exp = cTable.sort_index(0).loc[fname_exp_datai:fname_exp_dataf, str(Var)]

            varlev_exps.append(varlev_dia_exp)

    else:

        for exp in Exps:

            fname_exp = 'ACOR' + str(exp) + '_' + datai_fmt + dataf_fmt + 'T.scan'

            varlev_exp = cTable.loc[fname_exp, str(Var)]

            varlev_exps.append(varlev_exp)

    return varlev_exps

def df_fill_nan(varlev_exps,varlev_dia_exps):

    """
    df_fill_nan
    ===========

    Esta função completa os dataframes até um tamanho específico.

    Parâmetros de entrada
    ---------------------
        varlev_exps     : lista de dataframes com as variáveis avaliadas para um período;
        varlev_dia_exps : lista de dataframes com as variáveis avaliadas para todos os dias de um período.

    Resultado
    ---------
        Lista de dataframes completados com NaN até o tamanho do maior dataframe.

    Uso
    ---
        import scanplot

        data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")

        dataInicial = data_conf["Starting Time"]
        dataFinal = data_conf["Ending Time"]
        Vars = list(map(data_vars.get,[*data_vars.keys()]))
        Stats = ["ACOR", "RMSE", "VIES"]
        Exps = list(data_conf["Experiments"].keys())
        outDir = data_conf["Output directory"]

        dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)

        varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,series=False)

        lst_varlev_dia_exps_rsp = scanplot.df_fill_nan(varlev_exps,varlev_dia_exps)
    """

    lst_shapes = []

    for varlev_exp, varlev_dia_exp in zip(varlev_exps, varlev_dia_exps):

        shape_exp = int(varlev_dia_exp.shape[0] / varlev_exp.shape[0]), int(varlev_exp.shape[0])
        lst_shapes.append(shape_exp)

    shape_exp_max = sorted(lst_shapes, key=lambda x: x[1], reverse=True)[0]
    shape_exp_min = sorted(lst_shapes, key=lambda x: x[1], reverse=False)[0]

    varlev_dia_exps_rsp = []

    for varlev_dia_exp, shape_exp in zip(varlev_dia_exps, lst_shapes):

        varlev_dia_exp_rsp = pd.DataFrame(varlev_dia_exp.values.reshape(shape_exp))

        if shape_exp < shape_exp_max:
            df_nan = pd.DataFrame(np.nan, index=np.arange(shape_exp_max[0]), columns=np.arange(shape_exp[1],shape_exp_max[1]))
            varlev_dia_exp_rsp = varlev_dia_exp_rsp.join(df_nan)

        varlev_dia_exps_rsp.append(varlev_dia_exp_rsp)

    return varlev_dia_exps_rsp

def calc_tStudent(lst_varlev_dia_exps_rsp):

    """
    calc_tStudent
    ===========

    Esta função calcula o teste de significância t-Student com intervalo de confiânça de 95%.

    Parâmetros de entrada
    ---------------------
        lst_varlev_dia_exps_rsp : lista de dataframes com as variáveis avaliadas para um período.

    Resultado
    ---------
        Resultado do teste de significância e valores críticos para serem utilizados pela função
        plot_lines_tStudent.

    Uso
    ---
        import scanplot

        data_vars, data_conf = scanplot.read_namelists("~/SCANTEC")

        dataInicial = data_conf["Starting Time"]
        dataFinal = data_conf["Ending Time"]
        Vars = list(map(data_vars.get,[*data_vars.keys()]))
        Stats = ["ACOR", "RMSE", "VIES"]
        Exps = list(data_conf["Experiments"].keys())
        outDir = data_conf["Output directory"]

        dTable = scanplot.get_dataframe(dataInicial,dataFinal,Stats,Exps,outDir)

        varlev_exps = scanplot.concat_tables_and_loc(dTable,dataInicial,dataFinal,Exps,series=False)

        lst_varlev_dia_exps_rsp = scanplot.df_fill_nan(varlev_exps,varlev_dia_exps)

        ldrom_exp, ldrosup_exp, ldroinf_exp = scanplot.calc_tStudent(lst_varlev_dia_exps_rsp)
    """

    lst_drom_exp = []
    lst_drosup_exp = []
    lst_droinf_exp = []

    for varlev_dia_exp_rsp in lst_varlev_dia_exps_rsp[1:]:

        dzm_exp = ((0.5 * np.log((1.0 + 0.5 * (lst_varlev_dia_exps_rsp[0] - varlev_dia_exp_rsp)) /
                                 (1.0 - 0.5 * (lst_varlev_dia_exps_rsp[0] - varlev_dia_exp_rsp)))))

        med_exp = dzm_exp.mean()
        var_exp = dzm_exp.var()

        stat, pval = ttest_ind(lst_varlev_dia_exps_rsp[0], varlev_dia_exp_rsp, equal_var=False)

        dof_exp = lst_varlev_dia_exps_rsp[0].shape[0] + varlev_dia_exp_rsp.shape[0] - 2.0

        texp = t.ppf(pval, dof_exp)

        dzc_exp = texp * (np.sqrt(var_exp / dof_exp))

        drom_exp   = 2.0 * (np.exp(2.0 * med_exp) - 1.0) / (np.exp(2.0 * med_exp) + 1.0)
        lst_drom_exp.append(drom_exp)

        drosup_exp = 2.0 * (np.exp(2.0 * dzc_exp) - 1.0) / (np.exp(2.0 * dzc_exp) + 1.0)
        lst_drosup_exp.append(drosup_exp)

        droinf_exp = 2.0 * (np.exp(-2.0 * dzc_exp )- 1.0) / (np.exp(-2.0 * dzc_exp) + 1.0)
        lst_droinf_exp.append(droinf_exp)

    return lst_drom_exp, lst_drosup_exp, lst_droinf_exp


from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import tensorflow as tf
import matplotlib.pyplot as plt

learning_rate = 0.1
num_steps = 500
batch_size = 128
display_step = 100

n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph input
X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}

def neural_net(x):
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

logits = neural_net(X)
prediction = tf.nn.softmax(logits)

loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)

correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    y_loss = []
    y_acc = []
    for step in range(num_steps):
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
        loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x, Y: batch_y})
        y_loss.append(loss)
        y_acc.append(acc)
    print("Testing Accuracy:", sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))

x = range(num_steps)
plt.subplot(211)
plt.plot(x, y_loss, 'r-')
plt.subplot(212)
plt.plot(x, y_acc, 'b-')
plt.show()


from flask.ext.principal import Principal, Identity, AnonymousIdentity, identity_changed, RoleNeed, UserNeed, Permission
from collections import namedtuple
from functools import partial

LoadNeed = namedtuple('load', ['method', 'value'])
EditLoadNeed = partial(LoadNeed, 'edit')
DeleteLoadNeed = partial(LoadNeed, 'delete')
ViewLoadNeed = partial(LoadNeed, 'view')
AssignLoadNeed = partial(LoadNeed, 'assign')
InvoiceLoadNeed = partial(LoadNeed, 'invoice')
CompleteLoadNeed = partial(LoadNeed, 'complete')

class EditLoadPermission(Permission):
	def __init__(self, load_id):
		need = EditLoadNeed(unicode(load_id))
		super(EditLoadPermission, self).__init__(need)

class DeleteLoadPermission(Permission):
	def __init__(self, load_id):
		need = DeleteLoadNeed(unicode(load_id))
		super(DeleteLoadPermission, self).__init__(need)

class ViewLoadPermission(Permission):
	def __init__(self, load_id):
		need = ViewLoadNeed(unicode(load_id))
		super(ViewLoadPermission, self).__init__(need)

class AssignLoadPermission(Permission):
	def __init__(self, load_id):
		need = AssignLoadNeed(unicode(load_id))
		super(AssignLoadPermission, self).__init__(need)

class InvoiceLoadPermission(Permission):
	def __init__(self, load_id):
		need = InvoiceLoadNeed(unicode(load_id))
		super(InvoiceLoadPermission, self).__init__(need)

class CompleteLoadPermission(Permission):
	def __init__(self, load_id):
		need = CompleteLoadNeed(unicode(load_id))
		super(CompleteLoadPermission, self).__init__(need)

TruckNeed = namedtuple('truck', ['method', 'value'])
EditTruckNeed = partial(TruckNeed, 'edit')
DeleteTruckNeed = partial(TruckNeed, 'delete')
ViewTruckNeed = partial(TruckNeed, 'view')
RouteTruckNeed = partial(TruckNeed, 'route')

class EditTruckPermission(Permission):
	def __init__(self, truck_id):
		need = EditTruckNeed(unicode(truck_id))
		super(EditTruckPermission, self).__init__(need)

class DeleteTruckPermission(Permission):
	def __init__(self, truck_id):
		need = DeleteTruckNeed(unicode(truck_id))
		super(DeleteTruckPermission, self).__init__(need)

class ViewTruckPermission(Permission):
	def __init__(self, truck_id):
		need = ViewTruckNeed(unicode(truck_id))
		super(ViewTruckPermission, self).__init__(need)

class RouteTruckPermission(Permission):
	def __init__(self, truck_id):
		need = RouteTruckNeed(unicode(truck_id))
		super(RouteTruckPermission, self).__init__(need)

DriverNeed = namedtuple('driver', ['method', 'value'])
EditDriverNeed = partial(DriverNeed, 'edit')
DeleteDriverNeed = partial(DriverNeed, 'delete')
ViewDriverNeed = partial(DriverNeed, 'view')

class EditDriverPermission(Permission):
	def __init__(self, truck_id):
		need = EditDriverNeed(unicode(truck_id))
		super(EditDriverPermission, self).__init__(need)

class DeleteDriverPermission(Permission):
	def __init__(self, truck_id):
		need = DeleteDriverNeed(unicode(truck_id))
		super(DeleteDriverPermission, self).__init__(need)

class ViewDriverPermission(Permission):
	def __init__(self, truck_id):
		need = ViewDriverNeed(unicode(truck_id))
		super(ViewDriverPermission, self).__init__(need)

CompanyNeed = namedtuple('company', ['method', 'value'])
EditCompanyNeed = partial(CompanyNeed, 'edit')
ViewCompanyNeed = partial(CompanyNeed, 'view')

class EditCompanyPermission(Permission):
	def __init__(self, company_id):
		need = EditCompanyNeed(unicode(company_id))
		super(EditCompanyPermission, self).__init__(need)

class ViewCompanyPermission(Permission):
	def __init__(self, company_id):
		need = ViewCompanyNeed(unicode(company_id))
		super(ViewCompanyPermission, self).__init__(need)


#!/usr/bin/env python3
# pylint: disable=missing-function-docstring, missing-class-docstring, missing-module-docstring

import itertools
import sys
from collections import defaultdict
from dataclasses import dataclass
from typing import Iterator
from typing import Tuple

def unidirectional_range(a: int, b: int) -> range:
    # pylint: disable=invalid-name
    step = 1 if (a <= b) else -1
    return range(a, b + step, step)

@dataclass
class Position:
    x: int  # pylint: disable=invalid-name
    y: int  # pylint: disable=invalid-name

    def __str__(self) -> str:
        return f"({self.x},{self.y})"

    def __hash__(self) -> Tuple[int, int]:
        return hash((self.x, self.y))

    @classmethod
    def from_string(cls, string: str) -> "Position":
        return cls(*(int(coordinate) for coordinate in string.split(",")))

class Path:
    def __init__(self, start: Position, end: Position) -> None:
        self.start = start
        self.end = end

    def __iter__(self) -> Iterator[Position]:
        """Return Iterator over the positions in this Path.

        INCLUDE diagonal lines."""

        distance_x = self.end.x - self.start.x
        distance_y = self.end.y - self.start.y

        if distance_x:
            x_range = unidirectional_range(self.start.x, self.end.x)
        else:
            x_range = itertools.repeat(self.start.x)

        if distance_y:
            y_range = unidirectional_range(self.start.y, self.end.y)
        else:
            y_range = itertools.repeat(self.start.y)

        return (Position(x, y) for x, y in zip(x_range, y_range))

    def __str__(self) -> str:
        return f"{self.start} -> {self.end}"

    @classmethod
    def from_string(cls, string: str) -> "Path":
        return cls(*map(Position.from_string, string.strip().split(" -> ")))

def main() -> None:

    vent_paths = [Path.from_string(line) for line in sys.stdin]
    vent_map = defaultdict(int)

    for vent_path in vent_paths:
        print(f"### {vent_path}")
        for position in vent_path:
            if len(vent_paths) <= 10:
                print(f"### --- {position}")
            vent_map[position] += 1

    print(sum(1 for count in vent_map.values() if count >= 2))

if __name__ == "__main__":
    main()


from train_tool import *
import torchvision.transforms as transforms
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data as data
from util.net import WideResNet
from util.cifar10 import get_cifar10
from util.svhn import get_svhn
import os
from set_args import create_parser
from util.data_augment import get_data_augment
def main(dataset):
    print('start train %s '%dataset)
    def create_model(ema=False):
        print("=> creating {ema}model ".format(
            ema='EMA ' if ema else ''))
        model = WideResNet(num_classes=10)
        model = nn.DataParallel(model).cuda()
        if ema:
            for param in model.parameters():
                param.detach_()
        return model

    transform_aug, transform_normal, transform_val = get_data_augment(dataset)
    if args.autoaugment:
        transform_1, transform_2 = transform_aug, transform_normal
    else:
        transform_1, transform_2 = transform_normal, transform_normal
    if dataset == 'cifar10':
        train_labeled_set, train_unlabeled_set, train_unlabeled_set2,val_set, test_set = get_cifar10('./data', args.n_labeled, args.val_size,
                                                                                transform_1=transform_1,
                                                                                transform_2=transform_2,
                                                                                transform_val=transform_val)
    if dataset == 'svhn':
        train_labeled_set, train_unlabeled_set, train_unlabeled_set2, val_set, test_set = get_svhn('./data', args.n_labeled, transform_1=transform_1, transform_2=transform_2, transform_val=transform_val)
    train_labeled_loader = data.DataLoader(train_labeled_set, batch_size=args.batch_size, shuffle=True, num_workers=0,
                                          drop_last=True)
    train_unlabeled_loader = data.DataLoader(train_unlabeled_set, batch_size=args.batch_size*args.unsup_ratio, shuffle=True,
                                            num_workers=0, drop_last=True)
    train_unlabeled_loader2 = data.DataLoader(train_unlabeled_set2, batch_size=args.batch_size*args.unsup_ratio, shuffle=False,
                                            num_workers=0)

    if args.val_size>0:
        val_loader = data.DataLoader(val_set, batch_size=args.batch_size*args.unsup_ratio, shuffle=False, num_workers=0)
    test_loader = data.DataLoader(test_set, batch_size=args.batch_size*args.unsup_ratio, shuffle=False, num_workers=0)
    model = create_model()
    ema_model = create_model(ema=True)
    tmp_model= create_model(ema=True)

    criterion = nn.CrossEntropyLoss().cuda()
    if args.optimizer == 'Adam':
        optimizer = optim.Adam(model.parameters(), lr=args.lr)
    else:
        optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=args.weight_decay, nesterov=True)
    ema_optimizer = WeightEMA(model, ema_model, tmp_model, alpha=args.ema_decay)
    cudnn.benchmark = True
    if args.warmup_step>0:
        totals = args.epochs*args.epoch_iteration
        warmup_step = args.warmup_step*args.epoch_iteration
        scheduler =  WarmupCosineSchedule(optimizer,warmup_step,totals)
    else:
        scheduler = None
    all_labels = np.zeros([len(train_unlabeled_set), 10])
    # optionally resume from a checkpoint
    title = dataset
    if args.resume:
        assert os.path.isfile(args.resume), "=> no checkpoint found at '{}'".format(args.resume)
        print("=> loading checkpoint '{}'".format(args.resume))
        checkpoint = torch.load(args.resume)
        args.start_epoch = checkpoint['epoch']
        model.load_state_dict(checkpoint['state_dict'])
        ema_model.load_state_dict(checkpoint['ema_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        print("Evaluating the  model:")
        if args.val_size>0:
            val_loss, val_acc = validate(val_loader, model, criterion,args.start_epoch)
        else:
            val_loss, val_acc = 0, 0
        test_loss, test_acc = validate(test_loader, model, criterion, args.start_epoch)
        print("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']))

        logger = Logger(os.path.join(args.out_path, '%s_log_%d.txt'%(dataset,args.n_labeled)), title=title, resume=True)
        logger.append([args.start_epoch, 0, 0, val_loss, val_acc,test_loss, test_acc])
    else:
        logger = Logger(os.path.join(args.out_path, '%s_log_%d.txt'%(dataset,args.n_labeled)), title=title)
        logger.set_names(['epoch', 'Train_class_loss',  'Train_consistency_loss', 'Val_Loss', 'Val_Acc.', 'Test_Loss', 'Test_Acc.'])

    for epoch in range(args.start_epoch, args.epochs):
        start_time = time.time()
        # train for one epoch
        class_loss, cons_loss,all_labels = train_semi(train_labeled_loader, train_unlabeled_loader, model, ema_model,optimizer, ema_optimizer,all_labels, epoch, scheduler)
        if epoch >= args.ema_stage:
            all_labels = get_u_label(ema_model, train_unlabeled_loader2,all_labels)

        print("--- training epoch in %s seconds ---" % (time.time() - start_time))

        if args.evaluation_epochs and (epoch + 1) % args.evaluation_epochs == 0:
            start_time = time.time()
            print("Evaluating the  model:")
            if args.val_size>0:
                val_loss, val_acc = validate(val_loader, model, criterion,epoch)
            else:
                val_loss, val_acc = 0, 0
            test_loss, test_acc = validate(test_loader, model, criterion, epoch)
            print("--- validation in %s seconds ---" % (time.time() - start_time))
            logger.append([epoch, class_loss, cons_loss, val_loss, val_acc,test_loss, test_acc])

            print("Evaluating the EMA model:")
            if args.val_size > 0:
                ema_val_loss, ema_val_acc = validate(val_loader, ema_model, criterion,epoch)
            else:
                ema_val_loss, ema_val_acc = 0, 0
            ema_test_loss, ema_test_acc = validate(test_loader, ema_model, criterion, epoch)
            print("--- validation in %s seconds ---" % (time.time() - start_time))
            logger.append([epoch, class_loss, cons_loss, ema_val_loss, ema_val_acc,ema_test_loss, ema_test_acc])

        if args.checkpoint_epochs and (epoch + 1) % args.checkpoint_epochs == 0:
            save_checkpoint(
                '%s_%d'%(dataset, args.n_labeled),
                {
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'ema_state_dict': ema_model.state_dict(),
                'optimizer' : optimizer.state_dict(),
            }, 'checkpoint_path', epoch + 1)

if __name__ == '__main__':
    dirs = [ 'result','data','checkpoint_path']
    for path in dirs:
        if os.path.exists(path) is False:
            os.makedirs(path)
    args = create_parser()
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    if args.seed is None:
        np.random.seed(args.seed)
        torch.manual_seed(args.seed)
        torch.cuda.manual_seed_all(args.seed)
    set_args(args)
    main(args.dataset)


import cv2
import numpy as np
import matplotlib.pyplot as plt
import json
import multiprocessing as mp

from src.process.dataloader import *

from pytesseract import Output
import pytesseract

def string_to_bag_of_chars(string):

    base = [0 for _ in range(256)]
    for l in string: base[ord(l)] += 1

    return base

def ad_contours_mp(boxes_bbx, contours, hierarchy, thread, num_threads):

    for n in range(thread, len(contours), num_threads):
        c = contours[n]
        if hierarchy[0][n][-2] == -1: continue
        rect = cv2.boundingRect(c)
        if rect[2] < 100 or rect[3] < 100: continue
        x,y,w,h = rect
        # cv2.rectangle(copied,(x,y),(x+w,y+h),(255, 0, 0),2)
        boxes_bbx[n] = (x, y, w, h)

    return None

def ad_detector(img):

    # copied = cv2.cvtColor(img.copy(), cv2.COLOR_GRAY2BGR)

    # Processing operations
    ret, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) # Letters (1) Background (0)
    dst = cv2.cornerHarris(thresh,10,3,0.04)
    dst = (dst - dst.min()) / (dst.max() - dst.min()) * 255
    ret, thresh = cv2.threshold(dst.astype(np.uint8), 0, 255, cv2.THRESH_OTSU)

    horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 1))
    blops = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))

    horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, blops)
    horizontal = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, horizontalStructure.T)

    contours, hierarchy = cv2.findContours(horizontal, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    boxes_bbx = [0 for i in range(len(contours))]
    threads = 8

    L = mp.Manager().list(boxes_bbx)
    processes = [mp.Process(target = ad_contours_mp, args=(L, contours, hierarchy, i, threads)) for i in range(threads)]
    [p.start() for p in processes]
    [p.join() for p in processes]

    return list(L)

def crop_words(bbx):

    d = pytesseract.image_to_data(bbx, lang = 'spa', nice = 5, output_type=Output.DICT)
    n_boxes = len(d['level'])
    text = []

    for i in range(n_boxes):

        detection = d['text'][i]
        if len(detection)!=0: text.append(detection)

    return text

def ad_to_json(bbx):

    words = crop_words(bbx)

    return words

def file_to_json(doc):

    bbxs = ad_detector(doc)

    res = {}
    for n, bbx in enumerate(bbxs):
        if not bbx: continue
        x, y, h, w = bbx
        res[n] = {"ocr": ad_to_json(doc[y:y+h, x:x+w]), 'bbx': bbx}

    return res

def all_files_json(dataloader):

    json_collection = {"files": {}}

    for num_document in range(len(dataloader)):

        print(f"Processant {num_document}\t",)

        document = dataloader[num_document][0]
        name = dataloader.files[num_document].strip()

        json_collection['files'][num_document] = {'filename': name, 'ads': file_to_json(document)}

    return json_collection

def compute(filenames):
    files = DataAnuncis(filenames)
    json_all = all_files_json(files)
    with open("./src/jsons/results.json", "wb") as outfile:
        string = json.dumps(json_all)
        outfile.write(string.encode('utf-8'))


import sympy as sym
import mpmath
import numpy
import pylab
import time

def lagrange_series(N):
  psi = []
#  h = Rational(1, N)
  h = 1.0/N
  points = [i*h for i in range(N+1)]
  for i in range(len(points)):
    p = 1
    for k in range(len(points)):
      if k != i:
        p *= (x - points[k])/(points[i] - points[k])
    psi.append(p)
  psi = psi[1:-1]
  return psi

def bernstein_series(N):
  # FIXME: check if a normalization constant is common in the definition
  # advantage is that the basis is always positive
  psi = []
#  for k in range(0,N+1):
  for k in range(1,N):  # bc elsewhere
    psi_k = x**k*(1-x)**(N-k)
    psi.append(psi_k)
  return psi

def sin_series(N):
  # FIXME: do not satisfy bc
  psi = []
  for k in range(1,N):
    psi_k = sin(pi*k*x)
    psi.append(psi_k)
  return psi

def taylor_series(N):
  # FIXME: do not satisfy bc
  print("Cannot with current BC implementation")
  return
  psi = []
  for k in range(1,N):
    psi_k = x**k
    psi.append(psi_k)
  return psi

def series(series_type, N):
  if series_type=="Taylor" : return taylor_series(N) # cannot do with current implementation of bc
  elif series_type=="sin"  : return sin_series(N)
  elif series_type=="Bernstein"  : return bernstein_series(N)
  elif series_type=="Lagrange"  : return lagrange_series(N)
  else: print("series type unknown ") # sys.exit(0)

x = sym.Symbol("x")
integrand_type = "stiffness"

bstime = []
lstime = []

bqtime = []
lqtime = []

Ns = [2, 4, 8, 16, 32]

for N in Ns:
  t0 = time.time()
  bpsi = series("Bernstein", N)
  A = sym.zeros((N-1), (N-1))
  for i in range(0, N-1):
    for j in range(0, N-1):
      integrand = 0
      if integrand_type == "mass": integrand = bpsi[i]*bpsi[j]
      if integrand_type == "stiffness": integrand = sym.diff(bpsi[i],x)*sym.diff(bpsi[j],x)
      integrand = sym.lambdify([x], integrand)
      A[i,j] = mpmath.quad(integrand, [0, 1])
  t1 = time.time()
  bqtime.append(t1-t0)

for N in Ns:
  t0 = time.time()
  lpsi = series("Lagrange", N)
  A = sym.zeros((N-1), (N-1))
  for i in range(0, N-1):
    for j in range(0, N-1):
      integrand = 0
      if integrand_type == "mass": integrand = lpsi[i]*lpsi[j]
      if integrand_type == "stiffness" : integrand = sym.diff(lpsi[i],x)*sym.diff(lpsi[j],x)
      integrand = sym.lambdify([x], integrand)
      A[i,j] = mpmath.quad(integrand, [0,1])
  t1 = time.time()
  lqtime.append(t1-t0)

for N in Ns:
  t0 = time.time()
  bpsi = series("Bernstein", N)
  A = sym.zeros((N-1), (N-1))
  for i in range(0, N-1):
    for j in range(0, N-1):
      integrand = 0
      if integrand_type == "mass": integrand = bpsi[i]*bpsi[j]
      if integrand_type == "stiffness": integrand = sym.diff(bpsi[i],x)*sym.diff(bpsi[j],x)
      A[i,j] = sym.integrate(integrand, (x, [0, 1]))
  t1 = time.time()
  bstime.append(t1-t0)

for N in Ns:
  t0 = time.time()
  lpsi = series("Lagrange", N)
  A = sym.zeros((N-1), (N-1))
  for i in range(0, N-1):
    for j in range(0, N-1):
      integrand = 0
      if integrand_type == "mass": integrand = lpsi[i]*lpsi[j]
      if integrand_type == "stiffness" : integrand = sym.diff(lpsi[i],x)*sym.diff(lpsi[j],x)
      A[i,j] = sym.integrate(integrand, (x, [0, 1]))
  t1 = time.time()
  lstime.append(t1-t0)

print("Berstein quadrature ", bqtime)
print("Lagrange quadrature ", lqtime)
print("Bernstein symbolic ", bstime)
print("Lagrange symbolic ", lstime)

import pylab
pylab.loglog(Ns, bqtime)
pylab.loglog(Ns, lqtime)
pylab.loglog(Ns, bstime)
pylab.loglog(Ns, lstime)
pylab.loglog(Ns, [4*10**-4*N**2 for N in Ns])
pylab.loglog(Ns, [10**-4*N**4 for N in Ns])
pylab.legend(["Bernstein quad", "Lagrange quad", "Berstein symb", "Lagrange symb", "N**2", "N**4"], loc="upper left")
pylab.show()


"""adding_long_read_models

Revision ID: 1929562f5d92
Revises: 0e3a27ee14c8, 140ad44cd00f
Create Date: 2023-03-29 17:05:29.795410

"""
from alembic import op
import sqlalchemy as sa
import rdr_service.model.utils

from rdr_service.participant_enums import PhysicalMeasurementsStatus, QuestionnaireStatus, OrderStatus
from rdr_service.participant_enums import WithdrawalStatus, WithdrawalReason, SuspensionStatus, QuestionnaireDefinitionStatus
from rdr_service.participant_enums import EnrollmentStatus, Race, SampleStatus, OrganizationType, BiobankOrderStatus
from rdr_service.participant_enums import OrderShipmentTrackingStatus, OrderShipmentStatus
from rdr_service.participant_enums import MetricSetType, MetricsKey, GenderIdentity
from rdr_service.model.base import add_table_history_table, drop_table_history_table
from rdr_service.model.code import CodeType
from rdr_service.model.site_enums import SiteStatus, EnrollingStatus, DigitalSchedulingStatus, ObsoleteStatus

# revision identifiers, used by Alembic.
revision = '1929562f5d92'
down_revision = ('0e3a27ee14c8', '140ad44cd00f')
branch_labels = None
depends_on = None

def upgrade(engine_name):
    globals()["upgrade_%s" % engine_name]()

def downgrade(engine_name):
    globals()["downgrade_%s" % engine_name]()

def upgrade_rdr():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('genomic_lr_raw',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('modified', sa.DateTime(), nullable=True),
    sa.Column('file_path', sa.String(length=255), nullable=True),
    sa.Column('ignore_flag', sa.SmallInteger(), nullable=False),
    sa.Column('biobank_id', sa.String(length=255), nullable=True),
    sa.Column('genome_type', sa.String(length=255), nullable=True),
    sa.Column('parent_tube_id', sa.String(length=255), nullable=True),
    sa.Column('lr_site_id', sa.String(length=255), nullable=True),
    sa.Column('long_read_platform', sa.String(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_genomic_lr_raw_file_path'), 'genomic_lr_raw', ['file_path'], unique=False)
    op.create_table('genomic_long_read',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('modified', sa.DateTime(), nullable=True),
    sa.Column('genomic_set_member_id', sa.Integer(), nullable=False),
    sa.Column('biobank_id', sa.String(length=128), nullable=False),
    sa.Column('sample_id', sa.String(length=80), nullable=True),
    sa.Column('genome_type', sa.String(length=80), nullable=False),
    sa.Column('lr_site_id', sa.String(length=11), nullable=False),
    sa.Column('long_read_platform', sa.String(length=80), nullable=False),
    sa.Column('ignore_flag', sa.SmallInteger(), nullable=False),
    sa.ForeignKeyConstraint(['genomic_set_member_id'], ['genomic_set_member.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_genomic_long_read_biobank_id'), 'genomic_long_read', ['biobank_id'], unique=False)
    op.create_index(op.f('ix_genomic_long_read_genomic_set_member_id'), 'genomic_long_read', ['genomic_set_member_id'], unique=False)
    op.create_index(op.f('ix_genomic_long_read_long_read_platform'), 'genomic_long_read', ['long_read_platform'], unique=False)
    op.create_index(op.f('ix_genomic_long_read_sample_id'), 'genomic_long_read', ['sample_id'], unique=False)
    # ### end Alembic commands ###

def downgrade_rdr():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_genomic_long_read_sample_id'), table_name='genomic_long_read')
    op.drop_index(op.f('ix_genomic_long_read_long_read_platform'), table_name='genomic_long_read')
    op.drop_index(op.f('ix_genomic_long_read_genomic_set_member_id'), table_name='genomic_long_read')
    op.drop_index(op.f('ix_genomic_long_read_biobank_id'), table_name='genomic_long_read')
    op.drop_table('genomic_long_read')
    op.drop_index(op.f('ix_genomic_lr_raw_file_path'), table_name='genomic_lr_raw')
    op.drop_table('genomic_lr_raw')
    # ### end Alembic commands ###

def upgrade_metrics():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###

def downgrade_metrics():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###