from __future__ import print_function

import numpy as np
import torch
import os
import glob

import os
import sys
import argparse
import time
import math

import torchvision
import tensorboard_logger as tb_logger
import torch
import torch.backends.cudnn as cudnn
from torchvision import transforms, datasets

from util import TwoCropTransform, AverageMeter
from util import adjust_learning_rate, warmup_learning_rate
from util import set_optimizer, save_model
from util import load_MSTAR, train_test_split, targets_to_labels, polar_transform
from networks.resnet_big import SupConResNet, SupConResNetHead
from losses import SupConLoss

from torch.utils.data import Dataset

try:
    import apex
    from apex import amp, optimizers
except ImportError:
    pass

def parse_option():
    parser = argparse.ArgumentParser('argument for training')

    parser.add_argument('--print_freq', type=int, default=10,
                        help='print frequency')
    parser.add_argument('--save_freq', type=int, default=25,
                        help='save frequency')
    parser.add_argument('--batch_size', type=int, default=512,
                        help='batch_size')
    parser.add_argument('--num_workers', type=int, default=4,
                        help='num of workers to use')
    parser.add_argument('--epochs', type=int, default=500,
                        help='number of training epochs')

    # optimization
    parser.add_argument('--learning_rate', type=float, default=0.05,
                        help='learning rate')
    parser.add_argument('--lr_decay_epochs', type=str, default='700,800,900',
                        help='where to decay lr, can be a list')
    parser.add_argument('--lr_decay_rate', type=float, default=0.1,
                        help='decay rate for learning rate')
    parser.add_argument('--weight_decay', type=float, default=1e-4,
                        help='weight decay')
    parser.add_argument('--momentum', type=float, default=0.9,
                        help='momentum')

    # model dataset
    parser.add_argument('--model', type=str, default='resnet18')  #Changed this to resnet18 from 50
    parser.add_argument('--dataset', type=str, default='mstar',
                        choices=['mstar'], help='dataset') #added mstar as default
    parser.add_argument('--mean', type=str, help='mean of dataset in path in form of str tuple')
    parser.add_argument('--std', type=str, help='std of dataset in path in form of str tuple')
    parser.add_argument('--data_folder', type=str, default=None, help='path to custom dataset')
    parser.add_argument('--size', type=int, default=32, help='parameter for RandomResizedCrop')
    parser.add_argument('--head', type=bool, default=True, help='Add extra MLP to encoder')

    # method
    parser.add_argument('--method', type=str, default='SimCLR',
                        choices=['SupCon', 'SimCLR'], help='choose method')

    # temperature
    parser.add_argument('--temp', type=float, default=0.5, #0.07
                        help='temperature for loss function')

    # other setting
    parser.add_argument('--cosine', action='store_true',
                        help='using cosine annealing')
    parser.add_argument('--syncBN', action='store_true',
                        help='using synchronized batch normalization')
    parser.add_argument('--warm', action='store_true',
                        help='warm-up for large batch training')
    parser.add_argument('--trial', type=str, default='0',
                        help='id for recording multiple runs')

    opt = parser.parse_args()

    # check if dataset is path that passed required arguments
    if opt.dataset == 'path':
        assert opt.data_folder is not None \
            and opt.mean is not None \
            and opt.std is not None

    # set the path according to the environment
    if opt.data_folder is None:
        opt.data_folder = './datasets/'
    opt.model_path = './save/{}/{}_models'.format(opt.method,opt.dataset)
    opt.tb_path = './save/{}/{}_tensorboard'.format(opt.method,opt.dataset)

    iterations = opt.lr_decay_epochs.split(',')
    opt.lr_decay_epochs = list([])
    for it in iterations:
        opt.lr_decay_epochs.append(int(it))

    opt.model_name = '{}_{}_{}_lr_{}_decay_{}_bsz_{}_temp_{}_trial_{}_head_{}'.\
        format(opt.method, opt.dataset, opt.model, opt.learning_rate,
               opt.weight_decay, opt.batch_size, opt.temp, opt.trial,opt.head)

    if opt.cosine:
        opt.model_name = '{}_cosine'.format(opt.model_name)

    # warm-up for large-batch training,
    if opt.batch_size > 256:
        opt.warm = True
    if opt.warm:
        opt.model_name = '{}_warm'.format(opt.model_name)
        opt.warmup_from = 0.01
        opt.warm_epochs = 10
        if opt.cosine:
            eta_min = opt.learning_rate * (opt.lr_decay_rate ** 3)
            opt.warmup_to = eta_min + (opt.learning_rate - eta_min) * (
                    1 + math.cos(math.pi * opt.warm_epochs / opt.epochs)) / 2
        else:
            opt.warmup_to = opt.learning_rate

    opt.tb_folder = os.path.join(opt.tb_path, opt.model_name)
    if not os.path.isdir(opt.tb_folder):
        os.makedirs(opt.tb_folder)

    opt.save_folder = os.path.join(opt.model_path, opt.model_name)
    if not os.path.isdir(opt.save_folder):
        os.makedirs(opt.save_folder)

    return opt

def set_loader(opt):

    from torchvision.utils import _log_api_usage_once
    class RandomCenterCrop(torch.nn.Module):
      #Randomly center crop the image. If fix_ratio is true, randomly center crop of size (a,a) for size_lower<a<size_upper. if fix_ratio is False, then
      #sample randomly center crop of size (a,b) for size_lower < a,b< size_upper.

        def __init__(self, size_lower, size_upper = None, fix_ratio = True):
            super().__init__()
            _log_api_usage_once(self)
            self.size_lower = size_lower

            if size_upper is None:
              self.size_upper = size_upper
            else:
              self.size_upper = size_upper

            self.fix_ratio = fix_ratio

        def forward(self, img):
            if self.fix_ratio:
              size = np.random.randint(low=self.size_lower, high=self.size_upper)
              return torchvision.transforms.functional.center_crop(img,size)
            else:
              size_a = np.random.randint(low=self.size_lower, high=self.size_upper)
              size_b = np.random.randint(low=self.size_lower, high=self.size_upper)
              return torchvision.transforms.functional.center_crop(img, (size_a,size_b))

        def __repr__(self) -> str:
            return f"{self.__class__.__name__}(size={self.size})"

    mstar_transforms = torchvision.transforms.Compose(
        [
            RandomCenterCrop(40,88),
            torchvision.transforms.Resize(size=opt.size), #Moved down from 128 b.c. memory?
            torchvision.transforms.RandomHorizontalFlip(0.5),
            torchvision.transforms.GaussianBlur((7,7)),
        ])

    use_phase = True
    hdr, fields, mag, phase = load_MSTAR()
    labels, target_names = targets_to_labels(hdr)

    if use_phase:
        data = polar_transform(mag,phase)
    else:
        data = np.reshape(mag,(mag.shape[0],1,mag.shape[1],mag.shape[2]))

    class mstar_dataset(Dataset):
        def __init__(self, ims, labels, transform = mstar_transforms):
            self.ims = torch.from_numpy(ims).float()
            self.transform = transform
            self.labels = labels
        def __len__(self):
            return self.ims.shape[0]
        def __getitem__(self,idx):
            if torch.is_tensor(idx):
                idx = idx.tolist()
            return [self.transform(self.ims[idx]), self.transform(self.ims[idx])] , self.labels[idx]

    train_dataset = mstar_dataset(data, labels, transform = mstar_transforms)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=opt.batch_size, shuffle=(train_sampler is None),
        num_workers=opt.num_workers, pin_memory=True, sampler=train_sampler)

    return train_loader

def set_model(opt):
    if opt.head:
        model = SupConResNetHead(name=opt.model)
    else:
        model = SupConResNet(name=opt.model)

    criterion = SupConLoss(temperature=opt.temp)

    # enable synchronized Batch Normalization
    if opt.syncBN:
        model = apex.parallel.convert_syncbn_model(model)

    if torch.cuda.is_available():
        if torch.cuda.device_count() > 1:
            model.encoder = torch.nn.DataParallel(model.encoder)
        model = model.cuda()
        criterion = criterion.cuda()
        cudnn.benchmark = True

    return model, criterion

def train(train_loader, model, criterion, optimizer, epoch, opt):
    """one epoch training"""
    model.train()

    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()

    end = time.time()
    for idx, (images, labels) in enumerate(train_loader):
        data_time.update(time.time() - end)

        images = torch.cat([images[0], images[1]], dim=0)
        if torch.cuda.is_available():
            images = images.cuda(non_blocking=True)
            labels = labels.cuda(non_blocking=True)
        bsz = labels.shape[0]

        # warm-up learning rate
        warmup_learning_rate(opt, epoch, idx, len(train_loader), optimizer)

        # compute loss
        features = model(images)
        f1, f2 = torch.split(features, [bsz, bsz], dim=0)
        features = torch.cat([f1.unsqueeze(1), f2.unsqueeze(1)], dim=1)
        #if opt.method == 'SupCon':
    #        loss = criterion(features, labels)
        if opt.method == 'SimCLR':
            loss = criterion(features)
        else:
            raise ValueError('contrastive method not supported: {}'.
                             format(opt.method))

        # update metric
        losses.update(loss.item(), bsz)

        # SGD
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        # print info
        if (idx + 1) % opt.print_freq == 0:
            print('Train: [{0}][{1}/{2}]\t'
                  'BT {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'DT {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'loss {loss.val:.3f} ({loss.avg:.3f})'.format(
                   epoch, idx + 1, len(train_loader), batch_time=batch_time,
                   data_time=data_time, loss=losses))
            sys.stdout.flush()

    return losses.avg

def main():
    opt = parse_option()

    # build data loader
    train_loader = set_loader(opt)

    # build model and criterion
    model, criterion = set_model(opt)

    # build optimizer
    optimizer = set_optimizer(opt, model)

    # tensorboard
    logger = tb_logger.Logger(logdir=opt.tb_folder, flush_secs=2)

    if opt.method == 'SimCLR':
        print('Unsupervised Learning')

    # training routine
    for epoch in range(1, opt.epochs + 1):
        adjust_learning_rate(opt, optimizer, epoch)

        # train for one epoch
        time1 = time.time()
        loss = train(train_loader, model, criterion, optimizer, epoch, opt)
        time2 = time.time()
        print('epoch {}, total time {:.2f}'.format(epoch, time2 - time1))

        # tensorboard logger
        logger.log_value('loss', loss, epoch)
        logger.log_value('learning_rate', optimizer.param_groups[0]['lr'], epoch)

        if epoch % opt.save_freq == 0:
            save_file = os.path.join(
                opt.save_folder, 'ckpt_epoch_{epoch}.pth'.format(epoch=epoch))
            save_model(model, optimizer, opt, epoch, save_file)

    # save the last model
    save_file = os.path.join(
        opt.save_folder, 'last.pth')
    save_model(model, optimizer, opt, opt.epochs, save_file)

if __name__ == '__main__':
    main()


from utils import Point,Line
from logicBlock import Sweeper
from tkinter import *
import sys
import time
import config
from optparse import OptionParser
import random

# 0 if all points are paired
# 1 if there is an un-paired point
points_modulo = 0
px = 0
py = 0
num = 0
epsi = 0.01

INP_FILE = "data_files/input_data.txt"

fh = open(INP_FILE,"w+")

def draw_point(x,y):
   c = 'deep pink'
   size = 3
   x1, y1 = ( x - size ), ( y - size )
   x2, y2 = ( x + size ), ( y + size )
   config.drawing_board.create_oval( x1, y1, x2, y2, fill = c )

def empty_event(event):
   pass

def input_lines(event):
   python_green = "#476042"
   global points_modulo,px,py

   print("Mouse position:",event.x,event.y)
   x1, y1 = ( event.x - 1 ), ( event.y - 1 )
   x2, y2 = ( event.x + 1 ), ( event.y + 1 )
   config.drawing_board.create_oval( x1, y1, x2, y2, fill = python_green )

   if points_modulo == 0:
      px,py = event.x,event.y
      points_modulo = 1
   else:
      points_modulo = 0
      dir = []
      for i in range(4):
         dir.append(random.choice([-1,1]))
      config.drawing_board.create_line(px,py,event.x,event.y)
      if py == event.y: #it's a line parallel to the x axis
         dir[1] = 1
         dir[3] = -1
      fh.write(str(px+(dir[0] * epsi)) + " " + str(py+(dir[1] * epsi)) + " " + str(event.x+(dir[2] * epsi)) + " " + str(event.y+(dir[3] * epsi)) + "\n")
      print("Saving line: " + str(px+(dir[0] * epsi)) + " " + str(py+(dir[1] * epsi)) + " " + str(event.x+(dir[2] * epsi)) + " " + str(event.y+(dir[3] * epsi)) + "\n")
      # write the line correcting for x and y

def draw_sweep_line(p):
   config.drawing_board.create_line(0,p.y,config.canvas_width-1,p.y,fill="green")
   draw_point(p.x,p.y)

def run_eval(delay_param=200):
   print("Evaluation running")
   fh.close()

   # sweep_obj = Sweeper("temp_testing.txt")
   sweep_obj = Sweeper(INP_FILE)
   sweep_obj.run()
   print("Eval complete. Running animation")
   for p in config.sweep_y:
      print(p)

   delay = 0
   delay_amount =delay_param
   num_artifacts = 2#drawing two things and the first one is the line
   n = config.drawing_board.create_oval(0,0,1,1)
   points_drawn = dict()
   for p in config.sweep_y:
      print(p)
      if p not in points_drawn:
         config.drawing_board.after(delay,draw_sweep_line,p)
         n += num_artifacts
         points_drawn[p] = True # since the point isn't hashable
         delay += delay_amount
         config.drawing_board.after(delay,config.drawing_board.delete,n - (num_artifacts - 1))
         delay += delay_amount
   config.sweep_y = []
   print("Animation complete")

def clear_canvas():
   global fh
   fh.close()
   global INP_FILE
   INP_FILE = "data_files/input_data.txt"
   fh = open(INP_FILE,"w")

   global points_modulo
   points_modulo = 0

   config.drawing_board.delete("all")

   config.sweep_y = []

def save_input():
   global num

   print("num = ",num)

   with open("data_files/input_data.txt") as f:
      with open("data_files/examples/tc_"+str(num)+".txt","w") as f1:
         for l in f:
            print(l)
            f1.write(l)

   clear_canvas()
   num += 1

if __name__ == "__main__":

   parser = OptionParser()

   parser.add_option('--inp',dest='inp_file',type=str,help='File from which input is to be loaded')
   parser.add_option('-n',dest='test_case_num',type=int,help='The number to be assigned to the testcase file where the input will be saved')
   parser.add_option('--disp_delay',dest='disp_delay',type=int,help='The delay between two positions of the sweep line on the GUI')

   options, otherjunk = parser.parse_args(sys.argv[1:])
   if len(otherjunk) != 0:
       raise Exception('Command line input not understood: ' + str(otherjunk))
   print(options.inp_file,type(options.inp_file))

   if options.disp_delay is None:
      options.disp_delay = 200

   if options.test_case_num is None:
      num = 0
   else:
      num = options.test_case_num

   # Draw board
   config.master.title("Line Segment Intersection")
   config.drawing_board.bind("<Button-1>",input_lines)
   # config.drawing_board.bind("<Button-1>",temp_testing)

   start_button = Button(config.master,text = "Start Evaluation",command = lambda: run_eval(delay_param=options.disp_delay))
   start_button.pack(side = LEFT)

   clear_all_button = Button(config.master,text = "Clear Screen",command = clear_canvas)
   clear_all_button.pack(side = LEFT)

   print(options.test_case_num)

   save_button = Button(config.master,text = "Save Input",command = save_input)
   save_button.pack(side = LEFT)

   if options.inp_file is None:
      print("loading from gui")
      mainloop()
   else:
      print("loading from file")
      # load_file = "data_files/bad_cases/" + options.inp_file
      load_file = "data_files/examples/" + options.inp_file
      fh_temp = open(load_file ,"r")
      t = fh_temp.readlines()
      for row in t:
         x1,y1,x2,y2 = map(float,row.split(' '))
         config.drawing_board.create_line(x1,y1,x2,y2)
         print("drawing line:",x1,y1,x2,y2)
      fh_temp.close()
      # global INP_FILE
      INP_FILE = load_file
      # run_eval()
      mainloop()


#!/usr/bin/env python
# encoding: utf-8
import wx, os, shutil, weakref, copy
import wx.grid as gridlib
from pyo import sndinfo
from constants import *
import QLiveLib

class SoundFileObject:
    def __init__(self, id, filename, loopmode=0, transpo=1, gain=0,
                 playing=0, directout=True, startpoint=0,
                 endpoint=-1, crossfade=5, channel=0):
        self.id = id
        self.filename = filename
        sndfolder = os.path.join(QLiveLib.getVar("projectFolder"), "sounds")
        info = sndinfo(os.path.join(sndfolder, self.filename))
        if info is None:
            self.valid = False
            self.duration = -1
        else:
            self.valid = True
            self.duration = info[1]
        self.loopmode = loopmode
        self.transpo = transpo
        self.gain = gain
        self.playing = playing
        self.directout = directout
        self.startpoint = startpoint
        self.endpoint = self.duration
        self.crossfade = crossfade
        self.channel = channel

        self.playerRef = None

        self.currentCue = 0
        self.cues = {}
        self.saveCue()

    def setPlayerRef(self, obj):
        if obj is None:
            self.playerRef = None
        else:
            self.playerRef = weakref.ref(obj)

    def getAttributes(self):
        return {
                ID_COL_FILENAME: self.filename,
                ID_COL_LOOPMODE: self.loopmode,
                ID_COL_TRANSPO: self.transpo,
                ID_COL_GAIN: self.gain,
                ID_COL_PLAYING: self.playing,
                ID_COL_DIRECTOUT: self.directout,
                ID_COL_STARTPOINT: self.startpoint,
                ID_COL_ENDPOINT: self.endpoint,
                ID_COL_CROSSFADE: self.crossfade,
                ID_COL_CHANNEL: self.channel
                }

    def setAttributes(self, dict):
        self.filename = dict[ID_COL_FILENAME]
        self.loopmode = dict[ID_COL_LOOPMODE]
        self.transpo = dict[ID_COL_TRANSPO]
        self.gain = dict[ID_COL_GAIN]
        self.playing = dict[ID_COL_PLAYING]
        self.directout = dict[ID_COL_DIRECTOUT]
        self.startpoint = dict[ID_COL_STARTPOINT]
        self.endpoint = dict[ID_COL_ENDPOINT]
        self.crossfade = dict[ID_COL_CROSSFADE]
        self.channel = dict[ID_COL_CHANNEL]

    def copy(self, obj):
        self.setAttributes(copy.deepcopy(obj.getAttributes()))
        self.currentCue = obj.getCurrentCue()
        self.setCues(copy.deepcopy(obj.getCues()))

    def setCurrentCue(self, x):
        self.currentCue = x

    def getCurrentCue(self):
        return self.currentCue

    def loadCue(self, x):
        if x in self.cues:
            self.setAttributes(self.cues[x])
        else:
            c = x
            while (c >= 0):
                if c in self.cues:
                    self.setAttributes(self.cues[c])
                    break
                c -= 1
        self.currentCue = x
        if self.playerRef is not None:
            player = self.playerRef()
            player.setAttributes(self.getAttributes())

    def delCue(self, x):
        del self.cues[x]
        for i in range(x+1, len(self.cues)):
            if i in self.cues:
                self.cues[i-1] = self.cues[i]
                del self.cues[i]

    def saveCue(self):
        self.cues[self.currentCue] = self.getAttributes()

    def addCue(self, x):
        self.currentCue = x
        self.saveCue()

    def getCues(self):
        return self.cues

    def setCues(self, cues):
        self.cues = cues

    def isValid(self):
        return self.valid

    def getId(self):
        return self.id

    def setId(self, x):
        self.id = x

    def setPlayerAttribute(self, id, value):
        if self.playerRef is not None:
            player = self.playerRef()
            player.setAttribute(id, value)

    def getFilename(self):
        return self.filename

    def setLoopMode(self, x):
        self.loopmode = x
        self.setPlayerAttribute(ID_COL_LOOPMODE, x)

    def getTranspo(self):
        return self.transpo

    def setTranspo(self, x):
        self.transpo = x
        self.setPlayerAttribute(ID_COL_TRANSPO, x)

    def getGain(self):
        return self.gain

    def setGain(self, x):
        self.gain = x
        self.setPlayerAttribute(ID_COL_GAIN, x)

    def getPlaying(self):
        return self.playing

    def setPlaying(self, x):
        self.playing = x
        self.setPlayerAttribute(ID_COL_PLAYING, x)

    def getDirectOut(self):
        return self.directout

    def setDirectOut(self, x):
        self.directout = x
        self.setPlayerAttribute(ID_COL_DIRECTOUT, x)

    def getStartPoint(self):
        return self.startpoint

    def setStartPoint(self, x):
        self.startpoint = x
        self.setPlayerAttribute(ID_COL_STARTPOINT, x)

    def getEndPoint(self):
        return self.endpoint

    def setEndPoint(self, x):
        self.endpoint = x
        self.setPlayerAttribute(ID_COL_ENDPOINT, x)

    def getCrossfade(self):
        return self.crossfade

    def setCrossfade(self, x):
        self.crossfade = x
        self.setPlayerAttribute(ID_COL_CROSSFADE, x)

    def getDuration(self):
        return self.duration

    def setChannel(self, x):
        self.channel = x
        self.setPlayerAttribute(ID_COL_CHANNEL, x)

    def getChannel(self):
        return self.channel

class GridFileDropTarget(wx.FileDropTarget):
    def __init__(self, grid):
        wx.FileDropTarget.__init__(self)
        self.grid = grid

    def OnDropFiles(self, x, y, filenames):
        x, y = self.grid.CalcUnscrolledPosition(x, y)
        col = self.grid.XToCol(x)
        row = self.grid.YToRow(y)
        if row > -1 and col == ID_COL_FILENAME:
            filename = filenames[0]
            if sndinfo(filename) is not None:
                self.grid.copyToSoundsFolder(filename)
                self.grid.setSelRow(row)
                self.grid.loadSound(os.path.basename(filename))

class SoundFileGrid(gridlib.Grid):
    def __init__(self, parent):
        super(SoundFileGrid, self).__init__(parent, style=wx.SUNKEN_BORDER)

        self.selRow = self.selCol = -1
        self.objects = []

        self.setInitialValuesToNone()

        self.textColour = self.GetDefaultCellTextColour()
        self.SetColLabelAlignment(wx.CENTER, wx.CENTER)
        self.SetCellHighlightColour("#CCCCCC")
        self.SetCellHighlightPenWidth(0)
        self.SetCellHighlightROPenWidth(0)
        self.SetSelectionBackground("#FFFFFF")
        self.SetSelectionForeground("#000000")
        self.font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.NORMAL,
                            wx.FONTWEIGHT_NORMAL, face="Monospace")
        self.SetRowLabelSize(50)

        dropTarget = GridFileDropTarget(self)
        self.SetDropTarget(dropTarget)

        self.CreateGrid(0, 10)

        for i, label in enumerate(LABELS):
            self.SetColMinimalWidth(i, COLSIZES[i])
            self.SetColSize(i, COLSIZES[i])
            self.SetColLabelValue(i, label)

        self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.OnCellChange)
        self.Bind(gridlib.EVT_GRID_EDITOR_HIDDEN, self.OnCellEditorHidden)
        self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.OnCellEditorShown)
        self.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)
        self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)
        self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick)
        self.GetGridWindow().Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.GetGridWindow().Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMotion)
        self.GetGridWindow().Bind(wx.EVT_SIZE, self.OnSize)

        self.addRow()

    def setSelRow(self, row):
        self.selRow = row

    def copyToSoundsFolder(self, path):
        sndfolder = os.path.join(QLiveLib.getVar("projectFolder"), "sounds")
        shutil.copy(path, sndfolder)

    def OnSize(self, evt):
        w, _ = self.GetGridWindow().GetClientSize()
        sz = w - (sum(COLSIZES) - COLSIZES[0])
        if sz >= COLSIZES[0]:
            self.SetColSize(0, sz)
        evt.Skip()

    def setInitialValuesToNone(self):
        self.clickPosition = self.initialTranspo = self.initialGain = None
        self.initialStart = self.initialEnd = self.initialFade = None
        self.initialChannel = None

    def addRow(self, index=None):
        if index == None:
            self.AppendRows(1, True)
            row = self.GetNumberRows() - 1
        else:
            self.InsertRows(index, 1, True)
            row = index
        # Filename
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_FILENAME, attr)
        renderer = gridlib.GridCellAutoWrapStringRenderer()
        self.SetCellRenderer(row, ID_COL_FILENAME, renderer)
        self.SetCellValue(row, ID_COL_FILENAME, "Click to choose a sound")
        self.SetCellTextColour(row, ID_COL_FILENAME, "#888888")
        # Loop Mode
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_LOOPMODE, attr)
        # Transpo
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_TRANSPO, attr)
        rd = gridlib.GridCellFloatRenderer()
        rd.SetPrecision(4)
        self.SetCellRenderer(row, ID_COL_TRANSPO, rd)
        # Gain
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_GAIN, attr)
        rd = gridlib.GridCellFloatRenderer()
        rd.SetPrecision(2)
        self.SetCellRenderer(row, ID_COL_GAIN, rd)
        # Playing
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_PLAYING, attr)
        # Direct Out
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_DIRECTOUT, attr)
        # Start Point
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_STARTPOINT, attr)
        rd = gridlib.GridCellFloatRenderer()
        rd.SetPrecision(4)
        self.SetCellRenderer(row, ID_COL_STARTPOINT, rd)
        # End Point
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_ENDPOINT, attr)
        rd = gridlib.GridCellFloatRenderer()
        rd.SetPrecision(4)
        self.SetCellRenderer(row, ID_COL_ENDPOINT, rd)
        # Crossfade
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_CROSSFADE, attr)
        rd = gridlib.GridCellFloatRenderer()
        rd.SetPrecision(1)
        self.SetCellRenderer(row, ID_COL_CROSSFADE, rd)
        # Channel
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        self.SetAttr(row, ID_COL_CHANNEL, attr)
        rd = gridlib.GridCellNumberRenderer()
        self.SetCellRenderer(row, ID_COL_CROSSFADE, rd)

        self.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx.ALIGN_CENTER)
        for i in range(1, len(LABELS)):
            self.SetCellAlignment(row, i, wx.ALIGN_CENTER, wx.ALIGN_CENTER)

    def putObjectAttrOnCells(self, obj, row):
        attrs = obj.getAttributes()
        for key in sorted(attrs):
            if key == ID_COL_FILENAME:
                self.SetCellValue(row, key, attrs[key])
            elif key == ID_COL_LOOPMODE:
                self.SetCellValue(row, key, LOOPMODES[attrs[key]])
            elif key == ID_COL_PLAYING:
                self.SetCellValue(row, key, PLAYING[attrs[key]])
            else:
                self.SetCellValue(row, key, str(attrs[key]))
        self.SetCellTextColour(row, ID_COL_FILENAME, self.textColour)

    def OnCellChange(self, evt):
        self.selRow, self.selCol = evt.GetRow(), evt.GetCol()
        val = self.GetCellValue(self.selRow, self.selCol)
        if self.selCol == ID_COL_TRANSPO:
            self.objects[self.selRow].setTranspo(float(val))
        elif self.selCol == ID_COL_GAIN:
            self.objects[self.selRow].setGain(float(val))
        elif self.selCol == ID_COL_STARTPOINT:
            self.objects[self.selRow].setStartPoint(float(val))
        elif self.selCol == ID_COL_ENDPOINT:
            self.objects[self.selRow].setEndPoint(float(val))
        elif self.selCol == ID_COL_CROSSFADE:
            self.objects[self.selRow].setCrossfade(float(val))
        elif self.selCol == ID_COL_CHANNEL:
            self.objects[self.selRow].setChannel(int(val))
        evt.Skip()

    def OnLeftDown(self, evt):
        upos = self.CalcUnscrolledPosition(evt.GetPosition())
        self.selRow, self.selCol = self.XYToCell(*upos)
        if self.selRow < (self.GetNumberRows() - 1) and \
           self.selCol in [ID_COL_TRANSPO, ID_COL_GAIN, ID_COL_STARTPOINT,
                           ID_COL_ENDPOINT, ID_COL_CROSSFADE, ID_COL_CHANNEL]:
            self.clickPosition = evt.GetPosition()[1]
            self.GetGridWindow().CaptureMouse()
            if self.selCol == ID_COL_TRANSPO:
                self.initialTranspo = self.objects[self.selRow].getTranspo()
            elif self.selCol == ID_COL_GAIN:
                self.initialGain = self.objects[self.selRow].getGain()
            elif self.selCol == ID_COL_STARTPOINT:
                self.initialStart = self.objects[self.selRow].getStartPoint()
            elif self.selCol == ID_COL_ENDPOINT:
                self.initialEnd = self.objects[self.selRow].getEndPoint()
            elif self.selCol == ID_COL_CROSSFADE:
                self.initialFade = self.objects[self.selRow].getCrossfade()
            elif self.selCol == ID_COL_CHANNEL:
                self.initialChannel = self.objects[self.selRow].getChannel()
        evt.Skip()

    def OnLeftUp(self, evt):
        if self.GetGridWindow().HasCapture():
            self.setInitialValuesToNone()
            self.GetGridWindow().ReleaseMouse()
        evt.Skip()

    def clip(self, x, mi, ma):
        if x < mi:
            return mi
        elif x > ma:
            return ma
        else:
            return x

    def OnMotion(self, evt):
        if self.clickPosition is not None:
            inc = {ID_COL_TRANSPO: [.0001, .01], ID_COL_GAIN: [.01, .1],
                   ID_COL_STARTPOINT: [.0001, .01], ID_COL_ENDPOINT: [.0001, .01],
                   ID_COL_CROSSFADE: [.1, 1], ID_COL_CHANNEL: [1, 1]}[self.selCol]
            diff = self.clickPosition - evt.GetPosition()[1]
            dur = self.objects[self.selRow].getDuration()
            if evt.ShiftDown():
                fdiff = diff * inc[0]
            else:
                fdiff = diff * inc[1]
            if self.selCol == ID_COL_TRANSPO:
                val = self.clip(self.initialTranspo + fdiff, 0.0, 4.0)
                self.objects[self.selRow].setTranspo(val)
            elif self.selCol == ID_COL_GAIN:
                val = self.clip(self.initialGain + fdiff, -120.0, 24.0)
                self.objects[self.selRow].setGain(val)
            elif self.selCol == ID_COL_STARTPOINT:
                val = self.clip(self.initialStart + fdiff, 0.0, dur)
                self.objects[self.selRow].setStartPoint(val)
            elif self.selCol == ID_COL_ENDPOINT:
                val = self.clip(self.initialEnd + fdiff, 0.0, dur)
                self.objects[self.selRow].setEndPoint(val)
            elif self.selCol == ID_COL_CROSSFADE:
                val = self.clip(self.initialFade + fdiff, 0.0, 50.0)
                self.objects[self.selRow].setCrossfade(val)
            elif self.selCol == ID_COL_CHANNEL:
                val = self.clip(self.initialChannel + fdiff, 0, 32)
                self.objects[self.selRow].setChannel(val)
            self.SetCellValue(self.selRow, self.selCol, str(val))
        evt.Skip()

    def OnCellEditorHidden(self, evt):
        QLiveLib.setVar("CanProcessCueKeys", True)

    def OnCellEditorShown(self, evt):
        QLiveLib.setVar("CanProcessCueKeys", False)

    def OnCellLeftClick(self, evt):
        # TODO: check and warning here...
        sndfolder = os.path.join(QLiveLib.getVar("projectFolder"), "sounds")
        self.snds = sorted([f for f in os.listdir(sndfolder)])
        if self.snds == []:
            self.OnCellRightClick(evt)
        self.selRow, self.selCol = evt.GetRow(), evt.GetCol()
        if self.selCol == ID_COL_FILENAME:
            menu = wx.Menu("Soundfiles")
            self.snds = sorted([f for f in os.listdir(sndfolder)])
            for i, snd in enumerate(self.snds):
                menu.Append(i+1, snd)
            menu.Bind(wx.EVT_MENU, self.selectSound, id=1, id2=i+1)
            self.PopupMenu(menu, evt.GetPosition())
            menu.Destroy()
        if self.selRow != self.GetNumberRows() - 1:
            if self.selCol == ID_COL_LOOPMODE:
                menu = wx.Menu("Loop Modes")
                for i, md in enumerate(LOOPMODES):
                    menu.Append(i+1, md)
                menu.Bind(wx.EVT_MENU, self.selectLoopMode, id=1, id2=i+1)
                self.PopupMenu(menu, evt.GetPosition())
                menu.Destroy()
            elif self.selCol == ID_COL_PLAYING:
                menu = wx.Menu("Playing Modes")
                for i, md in enumerate(PLAYING):
                    menu.Append(i+1, md)
                menu.Bind(wx.EVT_MENU, self.selectPlayingMode, id=1, id2=i+1)
                self.PopupMenu(menu, evt.GetPosition())
                menu.Destroy()
            elif self.selCol == ID_COL_DIRECTOUT:
                val = self.objects[self.selRow].getDirectOut()
                val = not val
                self.objects[self.selRow].setDirectOut(val)
                self.SetCellValue(self.selRow, self.selCol, str(val))
        evt.Skip()

    def OnCellRightClick(self, evt):
        self.selRow, self.selCol = evt.GetRow(), evt.GetCol()
        if self.selCol == ID_COL_FILENAME:
            dlg = wx.FileDialog(self,
                                "Open Soundfile...", os.path.expanduser("~"),
                                "", AUDIO_FILE_WILDCARD, style=wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                self.copyToSoundsFolder(path)
                self.loadSound(os.path.basename(path))
            dlg.Destroy()
        evt.Skip()

    def OnLabelRightClick(self, evt):
        row = evt.GetRow()
        if row != -1:
            self.selRow = row
            menu = wx.Menu()
            for i, act in enumerate(["Duplicate", "Delete"]):
                menu.Append(i+1, act)
            menu.Bind(wx.EVT_MENU, self.doLabelAction, id=1, id2=i+1)
            self.PopupMenu(menu, evt.GetPosition())
            menu.Destroy()
        evt.Skip()

    def doLabelAction(self, evt):
        if evt.GetId() == 1: # Duplicate
            obj = self.objects[self.selRow]
            id = obj.getId()
            filename = obj.getFilename()
            new = SoundFileObject(id, filename)
            new.copy(obj)
            self.selRow += 1
            self.objects.insert(self.selRow, new)
            self.addRow(index=self.selRow)
            self.putObjectAttrOnCells(new, self.selRow)
            for id in [ID_COL_TRANSPO, ID_COL_GAIN, ID_COL_STARTPOINT,
                       ID_COL_ENDPOINT, ID_COL_CROSSFADE, ID_COL_CHANNEL]:
                attr = self.GetOrCreateCellAttr(self.selRow, id)
                attr.SetReadOnly(False)
        elif evt.GetId() == 2: # Delete
            del self.objects[self.selRow]
            self.DeleteRows(self.selRow, 1, True)
        for i, obj in enumerate(self.objects):
            obj.setId(i)

    def selectSound(self, evt):
        sel = self.snds[evt.GetId() - 1]
        if sel is not None:
            self.loadSound(sel)
        evt.Skip()

    def loadSound(self, sel):
        obj = SoundFileObject(self.selRow, sel)
        obj.setCurrentCue(QLiveLib.getVar("CuesPanel").getCurrentCue())
        if obj.isValid():
            if self.selRow < len(self.objects):
                self.objects[self.selRow] = obj
            else:
                self.objects.append(obj)
                self.addRow()
            self.putObjectAttrOnCells(obj, self.selRow)
            for id in [ID_COL_TRANSPO, ID_COL_GAIN, ID_COL_STARTPOINT,
                       ID_COL_ENDPOINT, ID_COL_CROSSFADE, ID_COL_CHANNEL]:
                attr = self.GetOrCreateCellAttr(self.selRow, id)
                attr.SetReadOnly(False)

    def addObject(self, id ,filename):
        self.selRow = id
        self.loadSound(filename)

    def setObjectCues(self, id, cues):
        obj = self.objects[id]
        obj.setCues(cues)
        obj.loadCue(0)

    def selectLoopMode(self, evt):
        sel = LOOPMODES[evt.GetId() - 1]
        if sel is not None:
            self.objects[self.selRow].setLoopMode(evt.GetId() - 1)
            self.SetCellValue(self.selRow, self.selCol, sel)

    def selectPlayingMode(self, evt):
        sel = PLAYING[evt.GetId() - 1]
        if sel is not None:
            self.objects[self.selRow].setPlaying(evt.GetId() - 1)
            self.SetCellValue(self.selRow, self.selCol, sel)

    def getSoundFileObjects(self):
        return self.objects

class SoundFilePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.grid = SoundFileGrid(self)
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.grid, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

    def loadCue(self, x):
        self.saveCue()
        for obj in self.getSoundFileObjects():
            obj.loadCue(x)
            self.grid.putObjectAttrOnCells(obj, obj.getId())

    def saveCue(self):
        for obj in self.getSoundFileObjects():
            obj.saveCue()

    def addCue(self, x):
        self.saveCue()
        for obj in self.getSoundFileObjects():
            obj.addCue(x)

    def delCue(self, x, cur):
        for obj in self.getSoundFileObjects():
            obj.delCue(x)
            self.loadCue(cur)

    def cueEvent(self, evt):
        tp = evt.getType()
        if tp == CUE_TYPE_DELETE:
            self.delCue(evt.getOld(), evt.getCurrent())
        elif tp == CUE_TYPE_SELECT:
            self.loadCue(evt.getCurrent())
        elif tp == CUE_TYPE_NEW:
            self.addCue(evt.getCurrent())
        elif tp == CUE_TYPE_SAVE:
            self.saveCue()

    def setSaveState(self, lst):
        for dict in lst:
            id = dict["id"]
            filename = dict["filename"]
            self.grid.addObject(id, filename)
            self.grid.setObjectCues(id, dict["cues"])

    def getSaveState(self):
        self.saveCue()
        l = []
        for obj in self.getSoundFileObjects():
            dict = {"id": obj.getId(),
                    "filename": obj.getFilename(),
                    "cues": obj.getCues()}
            l.append(dict)
        return l

    def getSoundFileObjects(self):
        return self.grid.getSoundFileObjects()


#!/usr/bin/env pypy
usage="""
compute sblm score on nbest output

verify stdout from
 xrs_info_decoder --nbest-output - --sbmt.sblm.level=verbose --sbmt.sblm.file - --sbmt.ngram.file -

or just compare (or add) score if no component_score logs are kept
"""

import sys
import re
import tree
from graehl import *
from dumpx import *
from ngram import *
from pcfg import *
from tree import *
from nbest import *

sbpre='sb'
pcpre='spc'

def checkunks(xs):
    for n in xs:
        e=needsesc(n)
        if e is not None:
            warn("reserved char %s in feature name %s"%(e,n),max=1)

unks=set([])
def tounk(x):
    return '<unk>' if x.startswith('GLUE') or x in unks else x

def pcfg_score(tree,lm,term=True,num2at=True):
    def score1lvl(t):
        p=t.label
        sent=[sblm_ngram.start]+[x.label for x in t.children]+[sblm_ngram.end,None]
        i=len(sent)-1
        s=0
        while i>1:
            sent[i]=sent[i-1]
            sent[i-1]=p
            sw=lm.score_word_combined(sent,i)
            #warn('score1w','%s = %s'%(' '.join(sent[max(0,i-4):i+1]),sw),max=100)
            s+=sw
            i-=1
        #warn('score1','%s %s %s'%(p,' '.join(sent),s),max=10)
        return s
    def score(t):
        if t.is_terminal() or (not term and t.is_preterminal()):
            return 0
        s=score1lvl(t)
        for c in t.children:
            s+=score(c)
        return s
    #warn('score',str(tree),max=1)
    return -score(tree)

def sblmunk(tree,lm,word=False,cat=False):
    def r(l,cv):
        if len(cv):
            if cat and lm.is_unk(l): return sum(cv)+1
        else:
            #'"'+l+'"'
            if word and lm.is_unk(l): return 1
        return sum(cv)
    return tree.reduce(r)

def check_nbest(l,lm,term=False,pword=True,strip=True,flatten=True,num2at=True,output_nbest=None,maxwords=999999,lineno='?',greedy=True):
    l=l.rstrip()
    tstr=getfield_brace('tree',l)
    t=nbest_tree(tstr) #str_to_tree_warn(tstr)
    if t is None:
        warn("no tree"," from %s line #%s"%(tstr,lineno))
        return False
    if len(t)>maxwords:
        return False
    rt=t.str_impl(lrb=False)
    if tstr!=rt:
        s=' => '.join(smallest_difference([tstr,rt],porch=2))
        warn('mismatch tree roundtrip (should just be -LRB- and -RRB- terminals',' diff = %s \norig = %s\nroundtrip = %s'%(s,tstr,rt),max=10)
    def label(x):
        return tounk(sbmt_lhs_label(x,num2at))
    t=t.mapnode(label)

    def skiplabel(y):
        if strip:
            y=strip_subcat(y)
        return no_bar(y) if flatten else y
    t=t.map_skipping(skiplabel)

    fv=getfields_num(l)
    sb=inds(fv,sbpre)
    pc=inds(fv,pcpre)
    fvd=dict(fv)
    checkunks(fvd.iterkeys())

    sent=fvd['sent']
    sentp=' sent=%s'%sent
    nbest=fvd['nbest']
    nbestp='nbest=%s '%nbest
    check=not greedy or nbest==0

    line=[l,'']
    def replfeat(f,v2,suf1='(nbest)',suf2='(python)'):
        if f in fvd:
            v=fvd[f]
            msg="replfeat %s %s %s"%(f,v,v2)
            if v is None or v2 is None: raise Exception(msg)
            if check: equal_or_warn(v,v2,f,suf1,suf2,pre=nbestp,post=sentp)
            line[0]=stripnumfeat(f,line[0])
        line[1]+=' %s=%s'%(f,v2)

    replfeat('sblm-nts',t.size_nts())
    if lm is not None:
        sblm=pcfg_score(t,lm,True,num2at)
        sblmnoterm=pcfg_score(t,lm,False,num2at)
        pword=sblm-sblmnoterm
        replfeat('sblm',sblm if term else sblmnoterm)
        replfeat('sblm-pword',pword)
        replfeat('sblm-unkword',sblmunk(t,lm,word=True))
        replfeat('sblm-unkcat',sblmunk(t,lm,cat=True))

    sbt=IntDict()
    pct=IntDict()
    def vpc(p,c):
        pct[(p,c)]+=1
    def vlr(left,right):
        sbt[(left,no_none(right,'</s>'))]+=1
    t.visit_pcl(vpc,leaf=False,root=False)
    t.visit_lrl(vlr,leaf=False,left='<s>',right='</s>')
    head='sent=%s nbest=%s tree=%s\ntree-orig=%s'%(sent,nbest,t,tstr)
    if check and len(sb):
        warn_diff(sb,sbt,desc=sbpre,header=nbestp,post=head)
    if check and len(pc):
        warn_diff(pc,pct,desc=pcpre,header=nbestp,post=head)
    if output_nbest is not None:
        s=stripinds('%s|%s'%(sbpre,pcpre),line[0])
        output_nbest.write('%s %s %s%s\n'%(s,strinds(sbpre,sbt),strinds(pcpre,pct),line[1]))
    return True

@optfunc.arghelp('lm','SRI ngram trained by pcfg.py')
@optfunc.arghelp('nbest','sbmt_decoder nbest list (optionally with sblm score debugging logs before each nbest)')
def nbest_sblm_main(lm='/home/nlg-02/pust/v8.1zh/pysblm.sblm/sblm.pcfg.5gram.lwlm',
                    #lm='nbest.pcfg.srilm',
                    nbest='nbest.txt',
                    strip=True,
                    flatten=True,
                    num2at=True,
                    sblm_terminals=0,
                    sblm_pword=1,
                    output_nbest='',
                    maxwords=999999,
                    logp_unk=0.0,
                    closed=True,
                    greedy=True,
                    usage_=usage
#                    rest_=None
                    ):
    lm=None if lm=='' else ngram(lm=lm,closed=closed)
    lm.set_logp_unk(logp_unk)
    output_nbest=None if output_nbest=='' else open(output_nbest,'w')
    n=0
    ng=0
    for l in open(nbest):
        if l.startswith("NBEST sent="):
            n+=1
            if check_nbest(l,lm,sblm_terminals,sblm_pword,strip,flatten,num2at,output_nbest,maxwords,n,greedy):
                ng+=1
    info_summary()
    log("%s good out of %s NBEST lines"%(ng,n))

import optfunc
optfunc.main(nbest_sblm_main)


# This file is a direct copy of the NetKet source code downloaded from https://github.com/netket/netket/blob/master/netket/models/equivariant.py
# It was slightly extended by Matej Mezera (2022) to include visible biases in the GCNN.

from typing import Tuple, Any

import numpy as np

from jax import numpy as jnp
from flax import linen as nn
from jax.nn.initializers import zeros, lecun_normal, normal
from jax.scipy.special import logsumexp

from netket.utils import HashableArray, warn_deprecation
from netket.utils.types import NNInitFunc
from netket.utils.group import PermutationGroup
from netket.graph import Graph, Lattice
from netket.nn.activation import reim_selu
from netket.nn.symmetric_linear import (
    DenseSymmMatrix,
    DenseSymmFFT,
    DenseEquivariantFFT,
    DenseEquivariantIrrep,
)

# Same as netket.nn.symmetric_linear.default_equivariant_initializer
# All GCNN layers have kernels of shape [out_features, in_features, n_symm]
default_gcnn_initializer = lecun_normal(in_axis=1, out_axis=0)

def identity(x):
    return x

""" NetKet implementation of GCNN_Irrep() method modified by Matej Mezera (2022). Implementation is extended include visible biases under the additional argument use_visible_bias = True. """
class pRBM_Irrep(nn.Module):
    r"""Implements a GCNN by projecting onto irreducible
    representations of the group. The projection onto
    the group is implemented with matrix multiplication

    Layers act on a feature maps of shape [batch_size, in_features, n_symm] and
    eeturns a feature map of shape [batch_size, features, n_symm].
    The input and the output are related by

    .. math ::

        y^{(i)}_g = \sum_{h,j} f^{(j)}_h W^{(ij)}_{h^{-1}g}.

    Note that this switches the convention of Cohen et al. to use an actual group
    convolution, but this doesn't affect equivariance.
    The convolution is implemented in terms of a group Fourier transform.
    Therefore, the group structure is represented internally as the set of its
    irrep matrices. After Fourier transforming, the convolution translates to

    .. math ::

        y^{(i)}_\rho = \sum_j f^{(j)}_\rho W^{(ij)}_\rho,

    where all terms are d x d matrices rather than numbers, and the juxtaposition
    stands for matrix multiplication.
    """

    symmetries: HashableArray
    """A group of symmetry operations (or array of permutation indices) over which the network should be equivariant.
    Numpy/Jax arrays must be wrapped into an :class:`netket.utils.HashableArray`.
    """
    irreps: Tuple[HashableArray]
    """List of irreducible represenation matrices"""
    layers: int
    """Number of layers (not including sum layer over output)."""
    features: Tuple
    """Number of features in each layer starting from the input. If a single number is given,
    all layers will have the same number of features."""
    characters: HashableArray
    """Array specifying the characters of the desired symmetry representation"""
    dtype: Any = np.float64
    """The dtype of the weights."""
    activation: Any = reim_selu
    """The nonlinear activation function between hidden layers."""
    output_activation: Any = identity
    """The nonlinear activation before the output."""
    equal_amplitudes: bool = False
    """If true forces all basis states to have the same amplitude by setting `Re[logψ] = 0`"""
    use_bias: bool = True
    """if True uses a bias in all layers."""
    precision: Any = None
    """numerical precision of the computation see `jax.lax.Precision`for details."""
    kernel_init: NNInitFunc = default_gcnn_initializer
    """Initializer for the kernels of all layers."""
    bias_init: NNInitFunc = zeros
    """Initializer for the biases of all layers."""

    visible_bias_init: NNInitFunc = normal(stddev=0.1)
    """Initializer for the visible bias."""
    use_visible_bias: bool = True
    """If True adds a bias to the input not passed through the nonlinear layer."""

    def setup(self):

        self.n_symm = np.asarray(self.symmetries).shape[0]

        self.dense_symm = DenseSymmMatrix(
            symmetries=self.symmetries,
            features=self.features[0],
            dtype=self.dtype,
            use_bias=self.use_bias,
            kernel_init=self.kernel_init,
            bias_init=self.bias_init,
            precision=self.precision,
        )

        self.equivariant_layers = [
            DenseEquivariantIrrep(
                irreps=self.irreps,
                features=self.features[layer + 1],
                use_bias=self.use_bias,
                dtype=self.dtype,
                precision=self.precision,
                kernel_init=self.kernel_init,
                bias_init=self.bias_init,
            )
            for layer in range(self.layers - 1)
        ]

    @nn.compact
    def __call__(self, x):
        if x.ndim < 3:
            x = jnp.expand_dims(x, -2)  # add a feature dimension
        input = x
        x = self.dense_symm(x)

        for layer in range(self.layers - 1):
            x = self.activation(x)
            x = self.equivariant_layers[layer](x)

        x = self.output_activation(x)

        x = jnp.sum(x, axis=(-2,))

        if self.use_visible_bias:
            v_bias = self.param(
                "visible_bias", self.visible_bias_init, (input.shape[-1],), self.dtype
            )
            out_bias = jnp.dot(input, v_bias)
            x = x + out_bias

        x = logsumexp(
            x, axis=(-1,), b=jnp.expand_dims(jnp.asarray(self.characters), (0,))
        )

        if self.equal_amplitudes:
            return 1j * jnp.imag(x)
        else:
            return x

""" NetKet implementation of GCNN modified by Matej Mezera (2022). Implementation is extended include visible biases. """
def pRBM(
    symmetries=None,
    product_table=None,
    irreps=None,
    point_group=None,
    mode="auto",
    shape=None,
    layers=None,
    features=None,
    characters=None,
    parity=None,
    **kwargs,
):
    r"""Implements a Group Convolutional Neural Network (G-CNN) that outputs a wavefunction
    that is invariant over a specified symmetry group.

    The G-CNN is described in ` Cohen et. *al* <http://proceedings.mlr.press/v48/cohenc16.pdf>`_
    and applied to quantum many-body problems in ` Roth et. *al* <https://arxiv.org/pdf/2104.05085.pdf>`_.

    The G-CNN alternates convolution operations with pointwise non-linearities. The first
    layer is symmetrized linear transform given by DenseSymm, while the other layers are
    G-convolutions given by DenseEquivariant. The hidden layers of the G-CNN are related by
    the following equation:

    .. math ::

        {\bf f}^{i+1}_h = \Gamma( \sum_h W_{g^{-1} h} {\bf f}^i_h).

    Args:
        symmetries: A specification of the symmetry group. Can be given by a
            nk.graph.Graph, a nk.utils.PermuationGroup, or an array [n_symm, n_sites]
            specifying the permutations corresponding to symmetry transformations
            of the lattice.
        product_table: Product table describing the algebra of the symmetry group.
            Only needs to be specified if mode='fft' and symmetries is specified as an array.
        irreps: List of 3D tensors that project onto irreducible representations of the symmetry group.
            Only needs to be specified if mode='irreps' and symmetries is specified as an array.
        point_group: The point group, from which the space group is built.
            If symmetries is a graph the default point group is overwritten.
        mode: string "fft, irreps, matrix, auto" specifying whether to use a fast
            fourier transform over the translation group, a fourier transform using
            the irreducible representations or by constructing the full kernel matrix.
        shape: A tuple specifying the dimensions of the translation group.
        layers: Number of layers (not including sum layer over output).
        features: Number of features in each layer starting from the input. If a single
            number is given, all layers will have the same number of features.
        characters: Array specifying the characters of the desired symmetry representation
        parity: Optional argument with value +/-1 that specifies the eigenvalue
            with respect to parity (only use on two level systems).
        dtype: The dtype of the weights.
        activation: The nonlinear activation function between hidden layers. Defaults to
            :ref:`nk.nn.activation.reim_selu`.
        output_activation: The nonlinear activation before the output.
        equal_amplitudes: If True forces all basis states to have equal amplitude
            by setting Re[psi] = 0.
        use_bias: If True uses a bias in all layers.
        precision: Numerical precision of the computation see `jax.lax.Precision`for details.
        kernel_init: Initializer for the kernels of all layers. Defaults to
            `lecun_normal(in_axis=1, out_axis=0)` which guarantees the correct variance of the
            output.
        bias_init: Initializer for the biases of all layers.
    """

    if isinstance(symmetries, Lattice) and (
        point_group is not None or symmetries._point_group is not None
    ):
        # With graph try to find point group, otherwise default to automorphisms
        shape = tuple(symmetries.extent)
        sg = symmetries.space_group(point_group)
        if mode == "auto":
            mode = "fft"
    elif isinstance(symmetries, Graph):
        sg = symmetries.automorphisms()
        if mode == "auto":
            mode = "irreps"
        if mode == "fft":
            raise ValueError(
                "When requesting 'mode=fft' a valid point group must be specified"
                "in order to construct the space group"
            )
    elif isinstance(symmetries, PermutationGroup):
        # If we get a group and default to irrep projection
        if mode == "auto":
            mode = "irreps"
        sg = symmetries
    else:
        if irreps is not None and (mode == "irreps" or mode == "auto"):
            mode = "irreps"
            sg = symmetries
            irreps = tuple(HashableArray(irrep) for irrep in irreps)
        elif product_table is not None and (mode == "fft" or mode == "auto"):
            mode = "fft"
            sg = symmetries
            product_table = HashableArray(product_table)
        else:
            raise ValueError(
                "Specification of symmetries is wrong or incompatible with selected mode"
            )

    if mode == "fft":
        if shape is None:
            raise TypeError(
                "When requesting `mode=fft`, the shape of the translation group must be specified. "
                "Either supply the `shape` keyword argument or pass a `netket.graph.Graph` object to "
                "the symmetries keyword argument."
            )
        else:
            shape = tuple(shape)

    if isinstance(features, int):
        features = (features,) * layers

    if characters is None:
        characters = HashableArray(np.ones(len(np.asarray(sg))))
    else:
        characters = HashableArray(characters)

    if mode == "fft":
        sym = HashableArray(np.asarray(sg))
        if product_table is None:
            product_table = HashableArray(sg.product_table)
        if parity:
            raise NotImplementedError("Wrong type of GCNN evaluation choosen. This is not implemented yet.")
            return GCNN_Parity_FFT(
                symmetries=sym,
                product_table=product_table,
                layers=layers,
                features=features,
                characters=characters,
                shape=shape,
                parity=parity,
                **kwargs,
            )
        else:
            raise NotImplementedError("Wrong type of GCNN evaluation choosen. This is not implemented yet.")
            return GCNN_FFT(
                symmetries=sym,
                product_table=product_table,
                layers=layers,
                features=features,
                characters=characters,
                shape=shape,
                **kwargs,
            )
    elif mode in ["irreps", "auto"]:
        sym = HashableArray(np.asarray(sg))

        if irreps is None:
            irreps = tuple(HashableArray(irrep) for irrep in sg.irrep_matrices())

        if parity:
            raise NotImplementedError("Wrong type of GCNN evaluation choosen. This is not implemented yet.")
            return GCNN_Parity_Irrep(
                symmetries=sym,
                irreps=irreps,
                layers=layers,
                features=features,
                characters=characters,
                parity=parity,
                **kwargs,
            )
        else:
            return pRBM_Irrep(
                symmetries=sym,
                irreps=irreps,
                layers=layers,
                features=features,
                characters=characters,
                **kwargs,
            )
    else:
        raise ValueError(
            f"Unknown mode={mode}. Valid modes are 'fft',irreps' or 'auto'."
        )


import random
import pygame
import time
import math

from pygame.locals import (
    K_UP,
    K_DOWN,
    K_RIGHT,
    K_LEFT,
    K_ESCAPE,
    QUIT,
    KEYDOWN,
)
pygame.init()

myfont = pygame.font.Font("freesansbold.ttf", 60)

class Grid(pygame.sprite.Sprite):
    def __init__(self):
        super(Grid, self).__init__()
        self.surf = pygame.Surface((600,600))
        self.surf.fill((120,120,120))
        self.rect = self.surf.get_rect(center = (((Width/2), (Height/2))))

    def gridUpdate(self):
        screen.blit(grid.surf, grid.rect)
        for x in range(0, 4):
            for y in range(0, 4):
                boxPosn = (10+150*x, 10+150*y)
                boxsize = 130
                noPosn = (110+150*x + (boxsize/2), 10+150*y + (boxsize/2))
                for i in nums:
                    if board[y][x] == i[0]:
                        pygame.draw.rect(self.surf, i[1], (boxPosn, (boxsize, boxsize)))
                        if i[0] != 0:
                            printGame(str(i[0]), noPosn)

def printGame(text, location, color = (0,0,0), font = myfont):
    lossSurface = font.render(text, False, color) #USE FONT TO DISPLAY YOU LOSE, CUS OFC U DID
    lossRect = lossSurface.get_rect()
    lossRect.center = location    #DETERMINING WHERE THE FONT WILL GO
    screen.blit(lossSurface, lossRect)

def spawnFn():
    global board
    global cont
    zerochk = []
    for i in board:
        for j in i:
            zerochk.append(j)
    if 0 in zerochk:
        if cont == 1:
            noX = random.randint(0,3)
            noY = random.randint(0,3)
            if board[noY][noX] == 0:
                board[noY][noX] = 2
            else:
                spawnFn()

def rotateFn():
    global board
    rotbd = [[0,0,0,0],
             [0,0,0,0],
             [0,0,0,0],
             [0,0,0,0]]
    for rad in range(0,2):
        for lth in range(rad, (4-rad)):
            rotbd[lth][rad] = board[rad][(3-lth)]
            rotbd[rad][lth] = board[lth][(3-rad)]
            rotbd[lth][3-rad] = board[3-rad][3-lth]
            rotbd[3-rad][lth] = board[lth][rad]
    board = rotbd.copy()

def gravFn(row):
    j = 0
    while j < 4:
        for i in range(0,3):
            if row[i] == 0 and row[i+1] != 0:
                row[i] = row[i+1]
                row[i+1] = 0
        j += 1

def mergeFn(row):
    for i in range(0,3):
        if row[i] == row[i+1]:
            row[i] = (row[i]+row[i+1])
            row[i+1] = 0

def moveFn(no = 0, dshun = 0):
    global board
    global cont
    global mvd
    global pvBrd
    for row in board:
        gravFn(row)
        mergeFn(row)
        gravFn(row)
    if no == 0:
        if pvBrd != board:
            OverCheck()
            if cont == 1:
                spawnFn()

def printBoard():
    print("")
    for i in board:
        print(i)

def mvLeft(no = 0):
    global board
    global pvBrd
    rotateFn()
    rotateFn()
    rotateFn()
    rotateFn()
    if no == 0:
        pvBrd = list(board)
    moveFn(no, mvLeft)

def mvRight(no = 0):
    global board
    global pvBrd
    rotateFn()
    rotateFn()
    if no == 0:
        pvBrd = list(board)
    moveFn(no, mvRight)
    rotateFn()
    rotateFn()

def mvUp(no = 0):
    global board
    global pvBrd
    rotateFn()
    if no == 0:
        pvBrd = list(board)
    moveFn(no, mvUp)
    rotateFn()
    rotateFn()
    rotateFn()

def mvDown(no = 0):
    global board
    global pvBrd
    rotateFn()
    rotateFn()
    rotateFn()
    if no == 0:
        pvBrd = list(board)
    moveFn(no, mvDown)
    rotateFn()

def OverCheck():
    global board
    global cont
    x = 0
    cpbrd = list(board)
    mvUp(1)
    if board == cpbrd:
        x += 1
    board = list(cpbrd)
    mvDown(1)
    if board == cpbrd:
        x += 1
    board = list(cpbrd)
    mvRight(1)
    if board == cpbrd:
        x += 1
    board = list(cpbrd)
    mvLeft(1)
    if board == cpbrd:
        x += 1
    board = list(cpbrd)
    if x == 4:
        cont = 0

Width = 800
Height = 600
cont = 1
nums = [[0, (0,0,0), 0],[2, (255,215,160), 0],[4, (255,180,140), 0],[8, (255,160,180), 0],[16, (219,112,147), 0],[32, (210,0,210), 0],[64,(186,85,211), 1],[128, (148,0,211), 1], [256, (138,43,226), 1],[512, (75,0,130), 1],[1024, (90, 0, 255), 1],[2048, (0,0,128), 1]]
board = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

TIMEDMOVE = pygame.USEREVENT + 1
pygame.time.set_timer(TIMEDMOVE, 100)

spawnFn()
spawnFn()

screen = pygame.display.set_mode((Width, Height))#, pygame.FULLSCREEN) #MAKE SCREEN IN PYGAME
screen.fill((255,255,255))
grid = Grid()

running = True
while running == True:
    for event in pygame.event.get():
        if event.type == QUIT: #SO THAT THE X BUTTON WORKS TO CLOSE THE GAME OTHERWISE IT WILL RUN FOREVER. THAT IS BAD.
            running = False
        if event.type == KEYDOWN:  #IF A BUTTON IS PRESSED:
            if event.key == K_ESCAPE:
                running = False     #RUNNING FALSE KILLS THE GAME IMMEDIATELY, SO ESC ALSO KILLS THE GAME
            if event.key == K_UP:
                mvUp()
                OverCheck()
            elif event.key == K_DOWN:
                mvDown()
                OverCheck()
            elif event.key == K_RIGHT:
                mvRight()
                OverCheck()
            elif event.key == K_LEFT:
                mvLeft()
                OverCheck()
    #for i in range(0,4):
        #printGame(str(board[i]), ((Width/2), (100 + 100*i)))
    grid.gridUpdate()
    pygame.display.flip()
    if cont == 0:
        screen.fill((255,255,255))
        printGame("Game Over!", (400, 300))
        pygame.display.flip()
        time.sleep(10)
        running = False
    screen.fill((255,255,255))


from torchvision.models import resnet50, ResNet50_Weights,resnet34, ResNet34_Weights
import models
import torchvision.transforms as transforms
import torch

class AddGaussianNoise(object):
    # Custom class to add gaussian noise to input images as data augmentation
    def __init__(self, mean=0., std=1.):
        self.std = std
        self.mean = mean

    def __call__(self, tensor):
        return tensor + torch.randn(tensor.size()) * self.std + self.mean

    def __repr__(self):
        return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)

#Self defined functions must be defined for adjust contrast and sharpness because they are functional pytorch augmentations that cannot be chained via compose normally
def adjust_contrast_compose_transform(cf):
    def _func(img):
        return transforms.functional.adjust_contrast(img,contrast_factor=cf) # 1 for original image, 2 for increasing constrast by factor of 2, will have to be tuned to our data
    return _func

class ModelManager():
    def __init__(self,input_size=512,contrast_factor = 1.2, gaussian_std = 0.1, affine_rotation=30, affine_translate_ratio=0.2):
        """
        This class should manage all the models we want to experiment with,
        either from direct import from pytorch or from the models folder
        """
        self.pretraining_source = ["imagenet",'random']
        # NOTE IF adding noise, MUST add noise AFTER normalize, or else image will become all noise
        # Current hyperparams are determined to be best after empirical tests
        # Standard transformation stack should be contrast -> normalize -> gaussianNoise.
        # Resizing can be done before or after contrast
        self.transform_string_key_base = {'gaussiannoise':AddGaussianNoise(0.,gaussian_std), #MUST set std to a value between 0 - 0.2 or else image becomes TOO much noise since tensor pixels are autoscaled to 0-1
                                        'normalize':transforms.Normalize((0.1875,),(0.198,)),
                                        'resize':transforms.Resize((input_size, input_size)),
                                        'adjust_contrast':adjust_contrast_compose_transform(contrast_factor),
                                        'random_affine':transforms.RandomAffine(degrees=affine_rotation,translate=(affine_translate_ratio,affine_translate_ratio))} #random image rotation by arg degree and translate by arg.decimal% of input_image size

    def get_model(self, model_name="resnet34", pretrain_source="imagenet", **kwargs):
        if model_name == "resnet34":
            return self.pretrained_resnet34(pretrain_source)
        elif model_name == "resnet50":
            return self.pretrained_resnet50(pretrain_source)
        elif model_name == "simpleconv":
            return models.Simpleconv(), None
        elif model_name == "inceptionv3":
            return models.InceptionV3(), None
        elif model_name == "bilbily":
            return models.Bilbily(**kwargs), None
        elif model_name == "vgg16":
            return models.Vgg16(), None
        else:
            raise ValueError("Wrong model name")

    def pretrained_resnet50(self,pretrain_source="imagenet"):

        if pretrain_source not in self.pretraining_source:
            raise NotImplementedError(f"The specified pretrain_source argument {pretrain_source} is not supported, please use one of the following options: {' '.join(self.pretraining_source)}")

        if pretrain_source == "imagenet":

            resnet50_model = resnet50(weights=ResNet50_Weights.DEFAULT)
            # Some pretrained models have packaged preprocessing transforms that should be applied/wrapped to input to ensure performance
            return resnet50_model, ResNet50_Weights.DEFAULT.transforms()

        elif pretrain_source == "random":

            resnet50_model = resnet50(weights=None)
            return resnet50_model, None

    def pretrained_resnet34(self,pretrain_source="imagenet"):

            if pretrain_source not in self.pretraining_source:
                raise NotImplementedError(f"The specified pretrain_source argument {pretrain_source} is not supported, please use one of the following options: {' '.join(self.pretraining_source)}")

            if pretrain_source == "imagenet":

                resnet34_model = resnet34(weights=ResNet34_Weights.DEFAULT)
                # Some pretrained models have packaged preprocessing transforms that should be applied/wrapped to input to ensure performance
                return resnet34_model, ResNet34_Weights.DEFAULT.transforms()

            elif pretrain_source == "random":

                resnet34_model = resnet34(weights=None)
                return resnet34_model, None

    def get_data_transform(self,transform_string_keys):
        compose_list = []
        for t in transform_string_keys:
            if t.lower() not in self.transform_string_key_base.keys():
                raise NotImplementedError(f"The specified data transform  {t} is not supported, please use one of the following options: {' '.join(self.transform_string_key_base.keys())}")
            compose_list.append(self.transform_string_key_base[t])

        composed_transforms = transforms.Compose(compose_list)
        return composed_transforms


import pytest

import nengo
from nengo.base import NengoObjectParam

def test_nengoobjectparam():
    """NengoObjectParam must be a Nengo object and is readonly by default."""
    class Test(object):
        nop = NengoObjectParam()

    inst = Test()
    assert inst.nop is None
    # Must be a Nengo object
    with pytest.raises(ValueError):
        inst.nop = 'a'
    a = nengo.Ensemble(10, dimensions=2, add_to_container=False)
    inst.nop = a.neurons
    assert inst.nop is a.neurons
    # Can't set it twice
    with pytest.raises(ValueError):
        inst.nop = a

def test_nengoobjectparam_nonzero():
    """Can check that objects have nonzero size in/out."""
    class Test(object):
        nin = NengoObjectParam(nonzero_size_in=True)
        nout = NengoObjectParam(nonzero_size_out=True)

    inst = Test()
    with nengo.Network():
        nin = nengo.Node(output=lambda t: t)
        nout = nengo.Node(output=lambda t, x: None, size_in=1)
        probe = nengo.Probe(nin)

        with pytest.raises(ValueError):
            inst.nin = nin
        with pytest.raises(ValueError):
            inst.nout = nout
        with pytest.raises(ValueError):
            inst.nout = probe

        inst.nin = nout
        inst.nout = nin

if __name__ == "__main__":
    nengo.log(debug=True)
    pytest.main([__file__, '-v'])


from flask import Flask, request, jsonify, render_template, json
from flask_cors import CORS
from movie_recommendation import *
import pandas as pd

app = Flask(__name__)
CORS(app)

@app.route("/")
def getModel():
    return render_template('base.html')

@app.route('/', methods=['GET','POST'])
def recommend_movies():
    if request.method == 'POST':
        movie_name = request.form['movie']
        result = results(movie_name)
        # recommendations = jsonify(result)
        recommendations = get_name(result)
    return render_template('show.html', movies = recommendations)

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


class ContainsItsAverage:

	def missing(self,arr):

		summ=sum(arr)

		n=len(arr)+1

		ans=[]

		for i in arr:

			 temp=(n*i)-summ

			 ans.append(temp)

		return sorted(set(ans))

obj=ContainsItsAverage()
print(obj.missing([1,10,1000,100]))


from django.core.management.base import BaseCommand, CommandError
from articles.queries import *
from articles.models import *

from articles.getdata import *

class Command(BaseCommand):
    """
    """
    def handle(self, *args, **options):
        experiment_id = 'E-GEOD-10588'

        exp = Experiment.objects.get(data__contains={'accession':experiment_id})
        health_range = list(range(225470,225481)) + list(range(225483,225498))
        pre_eclampsia_range = list(range(225481,225482))+list(range(225498,225514))
        health_samples = ['GSM'+str(item) for item in health_range]
        pre_eclampsia_samples = ['GSM'+str(item) for item in pre_eclampsia_range]

        diagnosis = StandardName.objects.get(name='Diagnosis')
        pre_eclampsia = StandardValue.objects.get(value='severe preeclampsia')
        health = StandardValue.objects.get(value='Health')

        samples_in_experiment= Sample.objects.filter(experiment=exp)

        for sample in samples_in_experiment:
            sample_name = SampleAttribute.objects.get(
                            sample=sample,
                            old_name='name').old_value
            sample_name = str(sample_name)[:-2] # delete " 1" in the end
            print(sample_name)
            if sample_name in health_samples:
                SampleAttribute.objects.create(sample=sample,
                                               unificated_name=diagnosis,
                                               unificated_value=health)

            elif sample_name in pre_eclampsia_samples:
                SampleAttribute.objects.create(sample=sample,
                                               unificated_name=diagnosis,
                                               unificated_value=pre_eclampsia)
                # for item in d:
                #     item.delete()

            else:
                print("something went wrong")


from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import render, redirect
from tablib import Dataset

from .models import Internal, Exportal,UpdateInfo
from django.views.generic import ListView
from .forms import UploadForm
from .resource import InternalResource,ExportalResource

# Create your views here.
class InternalListView(ListView):
    model = Internal
    template_name = "product/internal_list.html"

class UpdateInfoListView(ListView):
    model = UpdateInfo
    template_name = "product/update_info.html"

class LineListView(ListView):
    queryset = Internal.objects.filter(line=True)
    template_name = "product/line_list.html"

class ExportalListView(ListView):
    model = Exportal
    template_name = "product/exportal_list.html"

def internal_upload(request):
    if request.method == 'POST':
        new_internal = InternalResource()
        dataset = Dataset()
        new_internal = request.FILES['file']
        imported_data = dataset.load(new_internal.read(), format='xlsx')
        for data in imported_data:
            print(data[1])
            value = Internal(
                data[0],
                data[1],
                data[2],
                data[3],
                data[4],
                data[5],
                data[6],
                data[7],
                data[8],
                data[9],
                data[10],
                data[11],
                data[12],
                data[13],
            )
            value.save()
            UpdateInfo.objects.create(user=request.user,file_name="file")
    return render(request, 'product/add_internal.html')

def exportal_upload(request):
    if request.method == 'POST':
        exportal_rescource = ExportalResource()
        dataset = Dataset()
        new_persons = request.FILES['file']
        imported_data = dataset.load(new_persons.read(), format='xlsx')
        for data in imported_data:
            print(data[1])
            value = Exportal(
                data[0],
                data[1],
                data[2],
                data[3],
                data[4],
                data[5],
                data[6],
                data[7],
                data[8],
                data[9],
                data[10],
                data[11],
                data[12],
                data[13],
                data[14],
                data[15],
                data[16],
                data[17],
                data[18],
                data[19],
                data[20],

            )
            value.save()
            UpdateInfo.objects.create(user=request.user, file_name="file")
            # result = person_resource.import_data(dataset, dry_run=True)  # Test the data import

        # if not result.has_errors():
        #    person_resource.import_data(dataset, dry_run=False)  # Actually import now

    return render(request, 'product/add_exportal.html')


from respect_validation.Rules.AbstractRule import AbstractRule

class Fibonacci(AbstractRule):

    def validate(self, input_val):

        if isinstance(input_val, str) and input_val.isdigit():
            input_val = int(input_val)

        if not isinstance(input_val, int) and not isinstance(input_val, float):
            return False

        sequence = [0, 1]
        position = 1

        while input_val > sequence[position]:
            position += 1
            sequence.append(sequence[position - 1] + sequence[position - 2])

        return sequence[position] == input_val


import random
import time
import string
from math import trunc
from paho.mqtt import client as mqtt_client

l_left = 0
l_right = 0
dist = 0
f1 = 0
f2 = 0
motor_stl = 0
motor_str = 0
u_akku = 1600

# Instructions for using the RobotEsp32 class :
#                                               - import RobotEsp32 as classexample
#                                               - examplebot = classexample.RobotEsp32( your broker IP-Adress, Port (optional if different from port 1883), subscriber-topic, publicher-topic)
#                                                   - example1 examplebot = classexample.RobotEsp32("123.456.789.01")  now Port = 1883, sub_topic="channel_1" and pub_topic="channel_2"
#                                                   - example2 examplebot = classexample.RobotEsp32("123.456.789.01", 1883, "mytpoic1", "mytopic2")
#                                               -Starting the MQTT- Client :    client = examplebot.connect_mqtt()
#                                                                               client.loop_start()
#                                               -Calling Function, for example: examplebot.drive(motor_left, motor_right,client)
class RobotEsp32:

    def __init__(self, ip, port=1883, sub_topic='channel_1', pub_topic='channel_2'):
        # self.__broker = 'test.mosquitto.org'
        # self.__broker = '192.168.178.51'
        self.__broker = ip
        # self.__port = 1883
        self.__port = port
        # self.__sub_topic = "isdtfdw/channel_1"
        self.__sub_topic = sub_topic
        # self.__pub_topic = "isdtfdw/channel_2"
        self.__pub_topic = pub_topic

    def connect_mqtt(self):  # -> mqtt_client:
        def on_connect(client, userdata, flags, rc):
            if rc == 0:
                print("Connected to MQTT Broker!")
            else:
                print("Failed to connect, return code %d\n", rc)

        # client.username_pw_set(username, password)
        # generate client ID with pub prefix randomly
        client_id = f'python-mqtt-{random.randint(0, 1000)}'
        client = mqtt_client.Client(client_id)
        client.on_connect = on_connect
        client.connect(self.__broker, self.__port)
        return client

    # function drive(motor left, motor right, mode, calibrate, Kp, Ki, Kd)
    # motor right and left: 0-300 in [rpm]
    # mode: 1-3 (mode = 1 Motor left is stronger; mode = 2 Motor right is stronger; mode = 3 motors have the same torque)
    # calibrate: 1 (the PID-Parameter Kp, Ki,Kd are passed by this function), 0 (the PID-Parameter of the arduino-code are used)
    def drive(self, motor_left, motor_right, client, mode=3, calib=0, p_calib=0, i_calib=0, d_calib=0) -> object:
        msg_count = 0
        if calib > 1:
            calib = 1
        if motor_left > 450:
            motor_left = 450
        elif motor_left < -450:
            motor_left = -450
        if motor_right > 450:
            motor_right = 450
        elif motor_right < -450:
            motor_right = -450
        motor_left_cor = motor_left + 450
        motor_right_cor = motor_right + 450
        ml = list(map(int, str(motor_left_cor)))
        mr = list(map(int, str(motor_right_cor)))
        p = list(map(int, str(p_calib)))
        i = list(map(int, str(i_calib)))
        d = list(map(int, str(d_calib)))
        m_s = list(map(int, str(mode)))
        c_s = list(map(int, str(calib)))
        if motor_left_cor < 10:
            ml.insert(0, 0)
            ml.insert(0, 0)
        elif motor_left_cor < 100:
            ml.insert(0, 0)
        if motor_right_cor < 10:
            mr.insert(0, 0)
            mr.insert(0, 0)
        elif motor_right_cor < 100:
            mr.insert(0, 0)
        if i_calib < 10:
            i.insert(0, 0)
        if p_calib < 10:
            p.insert(0, 0)
        if d_calib < 10:
            d.insert(0, 0)
            d.insert(0, 0)
        if d_calib < 100:
            d.insert(0, 0)
        msg = f"{ml[0]}{ml[1]}{ml[2]}{mr[0]}{mr[1]}{mr[2]}{m_s[0]}{c_s[0]}{p[0]}{p[1]}{i[0]}{i[1]}{d[0]}{d[1]}{d[2]}"
        print(msg)
        result = client.publish(self.__pub_topic, msg)
        status = result[0]

    def get_data(self, client):

        def on_message(client, userdata, msg):
            global f1
            global f2
            global motor_str
            global motor_stl
            # print(f"{msg.payload.decode()}")
            data = msg.payload.decode()
            length = len(data)
            f1 = (int(data[0]) * 1000 + int(data[1]) * 100 + int(data[2]) * 10 + int(data[3])) - 2500
            f2 = (int(data[4]) * 1000 + int(data[5]) * 100 + int(data[6]) * 10 + int(data[7])) - 2500
            motor_stl = (int(data[8]) * 1000 + int(data[9]) * 100 + int(data[10]) * 10 + int(data[11])) - 3000
            motor_str = (int(data[12]) * 1000 + int(data[13]) * 100 + int(data[14]) * 10 + int(data[15])) - 3000
            u_akku = int(data[16]) * 1000 + int(data[17]) * 100 + int(data[18]) * 10 + int(data[19])
            dist = int(data[20]) * 1000 + int(data[21]) * 100 + int(data[22]) * 10 + int(data[23])
            l_left = int(data[24]) * 1000 + int(data[25]) * 100 + int(data[26]) * 10 + int(data[27])
            l_right = int(data[28]) * 1000 + int(data[29]) * 100 + int(data[30]) * 10 + int(data[31])

            if u_akku < 1500:
                print("______battery is empty!!!______")
            # print(f"left: {line_l} right: {line_r} dist: {dist}")
            print(
                f'right: {f1} left: {f2} mo_r: {motor_stl} mo_l: {motor_str} battery:{u_akku} distance:{dist} line_left:{l_left} line_right: {l_right} time: {time.time()}')
            # client.disconnect()

        client.subscribe(self.__sub_topic)
        client.on_message = on_message
        global f1
        global f2
        global motor_str
        global motor_stl
        global l_left
        global l_right
        global dist
        return f1, f2, motor_stl, motor_str, l_left, l_right, dist


#LABORATORIO_1
#NOMBRES: Yamil Azaharvich y Yamil Azaharvich.
#DESAFIO 1
print("---------------------------------------------")
print("Desafio 1\n")

import requests

headers = {
    'Content-Type': 'text/plain',
}

response = requests.get('http://finis.malba.cl/GetMsg', headers=headers)

#ROT
def custom(i):
    if i>=0:
        return i%26
    return 26+i

def rot(r,n):
    if r>="a" and r<="z":
        return chr(custom(ord(r)+n-97)+97)
    if r>="A" and r<="Z":
        return chr(custom(ord(r)+n-65)+65)
    return str(r)

def Rot(cadenita,n):
    nuevo = ""
    for r in cadenita:
        if r.isalpha():

            nuevo = nuevo+rot(r,n)
        else:
            nuevo = nuevo + r
    return nuevo

#VIGENERE

abc = ("abcdefghijklmnopqrstuvwxyz")

def cifrar(cadena,llave):
        texto = ""
        aux=0
        for letra in cadena:
                orden = abc.find(letra)+abc.find(llave[aux%len(llave)]) #En caso de que la llave sea mayor al mensaje se vuelve a comenzar
                modo = int(orden)%len(abc)
                texto = texto + str(abc[modo])
                aux = aux+1
        return texto

def descifrar(cadena,llave):
        texto = ""
        aux=0
        for letra in cadena:
                orden = abc.find(letra)-abc.find(llave[aux%len(llave)])
                modo = int(orden)%len(abc)
                texto = texto + str(abc[modo])
                aux = aux+1
        return texto

#CIFRADO

print("Cifrado en Rot(15)")
primer_paso = Rot("HolaMundo",15)
print(Rot("HolaMundo",15))
print("")
print ("Cifrado en Vigenere")
linea = str(primer_paso).lower()
clave = "cvqnoteshrwnszhhksorbqcoas"
segundo_paso = cifrar(linea,clave)
print (cifrar(linea,clave))
print("")
print("Cifrado en Rot(7)")
print(Rot(segundo_paso,7))

import requests

headers = {
    'Content-Type': 'text/plain',
}

data = '{"msg":"ffxjwjnrr"}'

response = requests.post('http://finis.malba.cl/SendMsg', headers=headers, data=data)
print(response.text)

#DESAFIO 2

print("---------------------------------------------")
print("Desafio 2\n")
import requests

headers = {
    'Content-Type': 'text/plain',
}

response = requests.get('http://finis.malba.cl/GetMsg', headers=headers)
print(response.text)
#DESCIFRADO

print("Descifrado en Rot -7")
primero = Rot("oemkd lyzqgvqxgptuuinqy nrkkfmnv",-7)
print(Rot("oemkd lyzqgvqxgptuuinqy nrkkfmnv",-7))
print("")
print ("Descifrado en Vigenere")
line = str(primero).lower()
llave = "aobkqolrzsrigpknkufezioer"
segundo = descifrar(line,llave)
print (descifrar(line,llave))
print("")
print("Descifrado en rot -15")
print(Rot(segundo,-15))
print("---------------------------------------------")

Lab 1 seguridad informática/YamilAzaharvich_ManuelNavarrete (1).py


from typing import Optional

from pydantic import BaseModel, root_validator

__author__ = "Bruno Lange"
__email__ = "blangeram@gmail.com"
__license__ = "MIT"

class Model(BaseModel):
    name: str

class Node(BaseModel):
    label: str
    value: int

class NodePatch(BaseModel):
    label: Optional[str]
    value: Optional[int]

    @root_validator
    def at_least_one(cls, values):
        if not values["value"] and not values["label"]:
            raise ValueError("must provide either label or value (or both)")
        return values


from flask import Flask, render_template
from data import get_data

app = Flask(__name__)

Tutors = get_data()

@app.route('/')
def index():
    return render_template('index.html', tutors = Tutors)

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

@app.route('/category/<string:category>/')
def category(category):
    mastered_tutors = []
    cap_cat = category.capitalize()
    if (category == "languages"):
        print("Yay")
        cap_cat = "Foreign Language"
    if (category == "standardized"):
        cap_cat = "Standardized Testing"
    if (category == "cs"):
        cap_cat = "Computer Science"
    for tutor in Tutors:
        if (tutor.subject.find(cap_cat) >= 0):
            mastered_tutors.append(tutor)
    return render_template('category.html', tutors = mastered_tutors, category = cap_cat)

@app.route('/tutors/<string:id>/')
def tutor(id):
    index = int(id) - 1
    return render_template('tutor.html', id = id, tutor = Tutors[index])

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


import requests
import hashlib
import base58
import codecs
import ecdsa
private_key = "18e14a7b6aaaaaaaaaa4f8114701e7c8e774e7f9a47e2c2035db29a206321725"
print(private_key)

#randomizing the startpoint at every program launch, so you dont scroll the same wallets again and again
a =int(input("Enter a big random number(1-9999999999999999999999): "))
if a % 2 == 0:
	private_key_decimal = int(private_key, base=16)
	private_key_decimal += a
	private_key=str(hex(private_key_decimal))
	#print(private_key,"\n",type(private_key))
	private_key = private_key[2:]
else:
	private_key_decimal = int(private_key, base=16)
	private_key_decimal -= a
	private_key=str(hex(private_key_decimal))
	#print(private_key,"\n",type(private_key))
	private_key = private_key[2:]

def gen_public_key(private_key):

	private_key_bytes = codecs.decode(private_key, 'hex')
	public_key_raw = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1).verifying_key
	public_key_bytes = public_key_raw.to_string()
	public_key_hex = codecs.encode(public_key_bytes, 'hex')
	public_key = (b'04' + public_key_hex).decode("utf-8")

	if (ord(bytearray.fromhex(public_key[-2:])) % 2 == 0):
	    public_key_compressed = '02'
	else:
	    public_key_compressed = '03'

	public_key_compressed += public_key[2:66]

	hex_str = bytearray.fromhex(public_key_compressed)
	sha = hashlib.sha256()
	sha.update(hex_str)

	rip = hashlib.new('ripemd160')
	rip.update(sha.digest())
	key_hash = rip.hexdigest()

	modified_key_hash = "00" + key_hash

	sha = hashlib.sha256()
	hex_str = bytearray.fromhex(modified_key_hash)
	sha.update(hex_str)

	sha_2 = hashlib.sha256()
	sha_2.update(sha.digest())

	checksum = sha_2.hexdigest()[:8]

	byte_25_address = modified_key_hash + checksum

	address = base58.b58encode(bytes(bytearray.fromhex(byte_25_address))).decode('utf-8')
	print(f"Privatekey: {private_key}")
	print(f"Publickey: {address}")
	print("-----------------------------------------------------------------------------------------------")
	return address

while True:
	private_key_decimal = int(private_key, base=16)
	private_key_decimal += 1
	private_key=str(hex(private_key_decimal))
	private_key = private_key[2:]
	pub_key = gen_public_key(private_key)
	data = requests.get(f"https://blockstream.info/api/address/{pub_key}").json()
	eingehend = data["chain_stats"]["funded_txo_count"]
	print(f'Balance: {data["chain_stats"]["funded_txo_sum"]-data["chain_stats"]["spent_txo_sum"]}')
	if eingehend >= 1:
		with open("yay.txt","a") as f:
			f.write(f"{private_key}\n")


from strip import Strip
from time import sleep
from animation import Animation

class RedAlarmAnimation(Animation):
  def init(self):
    self.strip.leds.fill(0xff0000)

  def run(self):
    leds = self.strip.leds

    brightness = 0
    blink_velocity = 0.1
    is_increasing_brighness = True

    while self.is_running:
      if brightness >= 1:
        is_increasing_brighness = False
        sleep(0.3)
      elif brightness <= 0:
        is_increasing_brighness = True
        sleep(0.1)

      brightness = brightness + blink_velocity if is_increasing_brighness else brightness - blink_velocity

      leds.brightness = brightness
      leds.show()
      sleep(0.01)

EXPORT = {
  'label': 'Red Alarm',
  'class': RedAlarmAnimation,
}


import torch
import pytorch_lightning as pl
from accent.accent_dataset import AccentDataset
from accent.model import Model

datamodule = AccentDataset()
datamodule.prepare_data()
datamodule.setup()
vocab = datamodule.vocab

unvocab = { y: x for x,y in vocab.items() }

model = Model.load_from_checkpoint(
    'model-accent.ckpt',
    vocab_size=datamodule.vocab_size,
    max_seq_len=32,
)

model.eval()
hits = 0
miss = 0

for batch in datamodule.val_dataloader():
    in_ = batch['in']
    out = batch['out']
    lens = batch['len']
    logits = model.forward(in_, lens)

    for i in range(logits.shape[0]):
        nlogits = logits[i, :lens[i]].detach().numpy()
        prediction = nlogits.argmax(axis=1)
        word = ''.join(unvocab[i] for i in in_[i].tolist()[:lens[i].item()])
        outx = out[i, :lens[i]].detach().numpy()
        if all(x==y for x, y in zip(prediction, outx)):
            hits += 1
        else:
            print(word, prediction, outx)
            miss += 1
    # break
    print(hits, miss, miss / (hits + miss))
print(hits, miss, miss / (hits + miss))


import math

def solution(n):
    if str(math.sqrt(n)).split(".")[1] == '0':
        return 1
    else:
        return 2

print(solution(144))
print(solution(976))

# 다른 좋은 코드
# def solution(n):
#     return 1 if (n ** 0.5).is_integer() else 2


import coffee
import codecs
order_list=[]
items = []
price = []
num = []

infile = codecs.open("goocoffee.txt", "r", "utf-8")

for line in infile.readlines():
    line = line.strip()
    parts = line.split(",")
    order_list.append(0)
    items.append(parts[0])
    price.append(parts[1])
    num.append(10)
infile.close()
# for i in range (len(items)):
#     num.append(5)

while(True):
    coffee.menu()

    number = int(input("메뉴를 선택하시오 : "))
    if (number == 1):
        while True:
            coffee.order_menu(items, price)
            if (coffee.order(items, price, order_list, num)==False):
                coffee.result(items, order_list)
                break
    elif (number == 2):
        coffee.plus_menu(items, price, num)
    elif (number ==3):
        coffee.remove_menu(items, order_list, price, num)
    elif (number== 4):
        coffee.check(items, num)
    elif (number== 5):
        coffee.checkplus(items, num)
    elif (number ==0):
        outfile = codecs.open("goocoffee.txt", "w", "utf-8")
        for i in range(len(items)):
            outfile.write(items[i]+",")
            outfile.write(str(price[i]) + "\n")
        outfile.close()
        print("종료합니다.")
        break


"""
从old_root 中随机移动 n张到new_root下
"""
import shutil
import os
import random

test_path = './data/train'  # old_root
test_fold = os.listdir(test_path)  #[001, 002, 003 ]
for sub_folder in test_fold:
    file_root = os.path.join(test_path, sub_folder)
    file_name = os.listdir(file_root)
    choice_file = random.sample(file_name, 1000)  # 将1000张移动到新文件夹下
    for i in choice_file:
        root = os.path.join(test_path, sub_folder, i)
        new_root = './data/test'  # new_root
        new_folder = os.path.join(new_root, sub_folder)
        if not os.path.exists(new_folder):
            os.mkdir(new_folder)
        root_2 = os.path.join(new_folder, i)
        shutil.move(root, root_2)
        print('comprss:{}'.format(sub_folder))


class MyCircularDeque:

    def __init__(self, k: int):
        self.lst = [None]*k
        self.currentSize = 0
        self.maxSize = k
        self.front = 0
        self.back = 0
    def insertFront(self, value: int) -> bool:
        if self.isEmpty():
            self.lst[self.front] = value
            self.currentSize += 1
            return True
        elif not self.isFull():
            self.front = (self.front - 1)%self.maxSize
            self.lst[self.front] = value
            self.currentSize += 1
            return True
        else:
            return False

    def insertLast(self, value: int) -> bool:
        if self.isEmpty():
            self.lst[self.back] = value
            self.currentSize += 1
            return True
        elif not self.isFull():
            self.back = (self.back + 1)%self.maxSize
            self.lst[self.back] = value
            self.currentSize += 1
            return True
        else:
            return False

    def deleteFront(self) -> bool:
        if not self.isEmpty():
            self.lst[self.front] = None
            if self.currentSize != 1:
                self.front = (self.front + 1 ) % self.maxSize
            self.currentSize -= 1
            return True
        else:
            return False
    def deleteLast(self) -> bool:
        if not self.isEmpty():
            self.lst[self.back] = None
            if self.currentSize != 1:
                self.back = (self.back-1)%self.maxSize
            self.currentSize -= 1
            return True
        else:
            return False

    def getFront(self) -> int:
        if not self.isEmpty():
            return self.lst[self.front]
        return -1

    def getRear(self) -> int:
        if not self.isEmpty():
            return self.lst[self.back]
        return -1

    def isEmpty(self) -> bool:
        return self.currentSize == 0

    def isFull(self) -> bool:
        return self.currentSize == self.maxSize

# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()


import enum

from matiushin_medvedev.backend.db.db_config import db

class ExaminationForm(enum.Enum):
    EXAM = 'EXAM'
    TEST = 'TEST'

    def __str__(self):
        return self.value

class EducationalPlan(db.Model):
    __tablename__ = 'educational_plan'

    id = db.Column(db.Integer, primary_key=True)
    spec_name = db.Column(db.String(255))
    discipline = db.Column(db.String(255))
    hours = db.Column(db.Integer())
    examination_form = db.Column(db.Enum(ExaminationForm))
    gradebooks = db.relationship("Gradebook", backref="educational_plan")

    def __init__(self, spec_name, discipline, hours, examination_form):
        self.spec_name = spec_name
        self.discipline = discipline
        self.hours = hours
        self.examination_form = examination_form

    def __repr__(self):
        return f"Educational plan({self.id})"


from typing import Mapping, Sequence

from acme import specs
from acme.tf import networks
from acme.tf import utils as tf2_utils

import numpy as np
import sonnet as snt

def make_default_networks(
    environment_spec: specs.EnvironmentSpec,
    *,
    policy_layer_sizes: Sequence[int] = (256, 256, 256),
    critic_layer_sizes: Sequence[int] = (512, 512, 256),
    policy_init_scale: float = 0.7,
    critic_init_scale: float = 1e-3,
    critic_num_components: int = 5,
) -> Mapping[str, snt.Module]:
  """Creates networks used by the agent."""

  # Unpack the environment spec to get appropriate shapes, dtypes, etc.
  act_spec = environment_spec.actions
  obs_spec = environment_spec.observations
  num_dimensions = np.prod(act_spec.shape, dtype=int)

  # Create the observation network and make sure it's a Sonnet module.
  observation_network = tf2_utils.batch_concat
  observation_network = tf2_utils.to_sonnet_module(observation_network)

  # Create the policy network.
  policy_network = snt.Sequential([
      networks.LayerNormMLP(policy_layer_sizes, activate_final=True),
      networks.MultivariateNormalDiagHead(
          num_dimensions,
          init_scale=policy_init_scale,
          use_tfd_independent=True)
  ])

  # The multiplexer concatenates the (maybe transformed) observations/actions.
  critic_network = snt.Sequential([
      networks.CriticMultiplexer(action_network=networks.ClipToSpec(act_spec)),
      networks.LayerNormMLP(critic_layer_sizes, activate_final=True),
      networks.GaussianMixtureHead(
          num_dimensions=1,
          num_components=critic_num_components,
          init_scale=critic_init_scale)
  ])

  # Create network variables.
  # Get embedding spec by creating observation network variables.
  emb_spec = tf2_utils.create_variables(observation_network, [obs_spec])
  tf2_utils.create_variables(policy_network, [emb_spec])
  tf2_utils.create_variables(critic_network, [emb_spec, act_spec])

  return {
      'policy': policy_network,
      'critic': critic_network,
      'observation': observation_network,
  }