import math
import itertools
import statistics

print("Enter a list of numbers with spaces with an integer average")
print("This program will give you a jugglable permutation")

#input is a_0 a_1... a_{p-1} with spaces
#input should have a whole average

sequence=[int(a_i) for a_i in input().split()]

lengte=int(len(sequence))
legelijst=[0]*lengte #this will be returned as the jugglable permutation
beats=[int(i) for i in range(0,lengte)]

#every time this function is called it changes a number in the legelijst to make it match the number in lijst
#it matches index i, and uses index j to fix the average
#k is the element of the lijst which we want to assign to the ith element of legelijst
#whenever we call upon this function legelijst will be jugglable

def rechtprater(lijst, i, j, k):
    landings=[(beats[l]+legelijst[l])%lengte for l in range(0,lengte)] #set landing times everytime with our new legelijst
    d_i=int(k-legelijst[i])     #d_i is whatever the ith element of legelijst is missing at this point from becoming k
    legelijst[i]=k     #force the ith element to be k

    #We force the jth element to be the smallest multiple of p which is greater than d_i and substract d_i

    legelijst[j]=int(math.ceil(d_i/lengte)*lengte-d_i)+legelijst[j]

    #after changing these two numbers in legelijst we use the function belows to find the jugglable permutation
    permutatiezoekert(legelijst, i, j, landings, lijst)

#swaps two elements in a list on the given positions
def lijstswappert(lijst, pos_1, pos_2):
    lijst[pos_1],lijst[pos_2]=lijst[pos_2],lijst[pos_1]
    return lijst

#this function finds the jugglable permutation of a given list where two elements, ith and jth, were changed
#it uses the landingsites from before changing the ith and jth element to find something jugglable according to the algorithm described in the proof of the lemma

#we check 4 different cases in which we can already permutate it to a juggling sequence
#if we cannot we make a legal swap of throws and try again

def permutatiezoekert(legelijst, i, j, landings, lijst):

    if (beats[i]+legelijst[i])%lengte==landings[i]:
        #it is jugglable as it is

        scanner_output=scanner(lijst, legelijst)     #we scan our current legelijst to our targeted sequence to see what are the first two indices which need fixing

        #we have three cases

        if scanner_output==[]:      #it outputs empty list if everything matches element wise
            #since it is already jugglable we can print and stop the program
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        elif len(scanner_output[0])==1:     #if there is 1 index no right we force fix it and also print and stop the program
            legelijst[scanner_output[0][0]]=scanner_output[1]
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        else:       #in this case there are at least two indices not correct yet so we assign these to i and j
            #we also assign the first element that is not in legelijst to k (the element which we will assign to the ith element of legelijst)
            i=scanner_output[0][0]
            j=scanner_output[0][1]
            k=scanner_output[1]

            #in this case we ask rechtprater to change the indices as needed and assigned
            rechtprater(lijst, i, j, k)

    #same as above
    elif (beats[i]+legelijst[i])%lengte==landings[j]:

        scanner_output=scanner(lijst, legelijst)

        if scanner_output==[]:
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        elif len(scanner_output[0])==1:
            legelijst[scanner_output[0][0]]=scanner_output[1]
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        else:
            i=scanner_output[0][0]
            j=scanner_output[0][1]
            k=scanner_output[1]

            rechtprater(lijst, i, j, k)

    #same except for the need to perform a swap of 2 throws to make the sequence jugglable
    elif (beats[i]+legelijst[j])%lengte==landings[i]:

        lijstswappert(legelijst, i, j)

        scanner_output=scanner(lijst, legelijst)

        if scanner_output==[]:
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        elif len(scanner_output[0])==1:
            legelijst[scanner_output[0][0]]=scanner_output[1]
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        else:
            i=scanner_output[0][0]
            j=scanner_output[0][1]
            k=scanner_output[1]

            rechtprater(lijst, i, j, k)

    #same as above
    elif (beats[i]+legelijst[j])%lengte==landings[j]:

        lijstswappert(legelijst, i, j)

        scanner_output=scanner(lijst, legelijst)

        if scanner_output==[]:
            print("the juggling sequence is")
            print(" ".join(str(x) for x in legelijst))

        elif len(scanner_output[0])==1:
            legelijst[scanner_output[0][0]]=scanner_output[1]
            print("the juggling sequence")
            print(" ".join(str(x) for x in legelijst))

        else:
            i=scanner_output[0][0]
            j=scanner_output[0][1]
            k=scanner_output[1]

            rechtprater(lijst, i, j, k)

    #apprently there is no permutation jugglable yet so we need to swap some throws
    else:
        beat_i=(landings[i]-legelijst[i])%lengte        #we check which beat corresponds to throw and landing spot i (modulo period)

        lijstswappert(legelijst, beat_i, i)     #we swap the throws made at said beats which is legal due to the check above

        lijstswappert(landings, beat_i, i)      #we also swap corresponding landing sites (to have moved the entire column as analogy to the proof)

        lijstswappert(landings, i, j)           #to wrap up we now also swap landing sites of i (which now contains landing site of beat_i) and i+1 to avoid going back here in the next step

        permutatiezoekert(legelijst, i, j, landings, lijst)    #we hope for the best and try again

#this takes two lists, sequence and leglijst, and will look for through legelijst from left to right
#its goal is to spot the first two elements in legelijst which are not in sequence
#of these we want to save the index

def scanner (a, b):
    targetlist=list(a)
    currentlist=list(b)

    lengte_target=int(len(targetlist))
    lengte_current=int(len(currentlist))

    index=[] #we will fill this with indices corresponding to elements which are in legelijst but not in sequence

    if lengte_target!=lengte_current:
        print('WE POOPED UP!!!')
    else:

        #we want to stop when we have found 2 indices or when we have checked every element from currentlist
        while int(len(index))!=2 and int(len(currentlist))!=0:
            i=0
            #we want to stop when we have had every element from the largetlist or when we find an element in targetlist which is not equal to the first element in currentlist
            while i!=int(len(targetlist)) and currentlist[0]!=targetlist[i]:
                    i=i+1

            #if we have had every element from the targetlist we know that currentlist[0] is not in there
            #we save its index in currentlist
            #we take this element away from currentlist so that the next one becomes currentlist[0]
            if i==int(len(targetlist)):
                index.append(lengte_current-int(len(currentlist)))
                currentlist.pop(0)

            #if we find a match then we don't want to save currentlist[0]'s index but we do want to take it away from currentlist
            #we also want to take away the matching element from targetlist as we don't want to use it again to check
            else:
                currentlist.pop(0)
                targetlist.pop(i)

    #we have stopped so apparently we have found 2 indices or the currentlist has length 0

    #if there are elements remaining in the targetlist the currentlist can be empty or non emtpy
    #in either scenario we can be sure that indices were added since, if this would not have happened, they would have all matched and targetlist would in fact be empty
    #we don't know how many indices are in there but it is non empty so we return the list and check later how many we have
    if int(len(targetlist))!=0:
        return [index,targetlist[0]]

    #however if the targetlist is empty we can be sure that the currentlist is empty as well
    #that means both lists are emtpy which only happens when all elements match
    else:
        return []

if statistics.mean(sequence)==int(statistics.mean(sequence)):
   rechtprater(sequence, 0, 1, sequence[0])
else:
    print("That is not an integer average!")

permutatie zoekert (aka converse average theorem) - kopie.py


import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
class Net(nn.Module):
    def __init__(self):
        super(Net, self, ).__init__()

        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=5),
            nn.ReLU(),
            nn.Conv2d(32, 32, kernel_size=5),
            nn.ReLU(),
            nn.BatchNorm2d(32),
            nn.MaxPool2d(kernel_size=2),
            nn.BatchNorm2d(32),
            nn.Dropout(0.28),
        )

        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=5),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=5),
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.MaxPool2d(kernel_size=2),
            nn.BatchNorm2d(64),
            nn.Dropout(0.28),
        )

        self.fc1 = nn.Sequential(
            nn.Linear(64, 4 * 4 * 64),
            nn.ReLU(),
            nn.BatchNorm1d(4 * 4 * 64),
            nn.Linear(4 * 4 * 64, 4 * 4 * 64),
            nn.ReLU(),
            nn.BatchNorm1d(4 * 4 * 64),
            nn.Dropout(0.28),
        )
        self.fc2 = nn.Linear(4 * 4 * 64, 36)

        for layer in self.modules():
            if isinstance(layer, nn.Conv2d) or isinstance(layer, nn.Linear):
                init.xavier_uniform_(layer.weight)

    def forward(self, x):

        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0),-1 )
        out = self.fc1(out)
        out = torch.flatten(out, 1)
        out = self.fc2(out)
        return F.log_softmax(out, dim=1)


import subprocess
from gtts import gTTS
from playsound import playsound

def delete_file(filename: str):
    """delete file by name"""
    subprocess.call(f"rm -r {filename}", shell=True)

def text_to_voice(text: str, lang: str):
    """
    convert text to voice
    """
    filename = "voz.mp3"
    try:
        file = gTTS(text=text, lang=lang)
        file.save(filename)
    except:
        print("Error al procesar voz")
        raise

    try:
        playsound(filename)
    except:
        print("Se produjo un error al reproducir voz.")
        raise

    delete_file(filename)

def main():
    """main method"""
    text = input("Ingrese texto para convertir a voz: ")

    if text:
        text_to_voice(text, "ES")

if __name__ == "__main__":
    main()


import torch, argparse, os
import time
from data import load_dataset
from models import StyleTransformer, Discriminator
from train import train, auto_eval, test_eval

class Config():
    data_path = './data/trump_elon/'
    log_dir = 'runs/exp'
    save_path = './save'
    pretrained_embed_path = './embedding/'
    device = torch.device('cuda' if True and torch.cuda.is_available() else 'cpu')
    discriminator_method = 'Multi' # 'Multi' or 'Cond'
    load_pretrained_embed = False
    min_freq = 3
    max_length = 64
    embed_size = 256
    d_model = 256
    h = 4
    num_styles = 2
    num_classes = num_styles + 1 if discriminator_method == 'Multi' else 2
    num_layers = 4
    batch_size = 32
    lr_F = 0.0001
    lr_D = 0.0001
    L2 = 0
    iter_D = 10
    iter_F = 5
    F_pretrain_iter = 500
    log_steps = 5
    eval_steps = 25
    learned_pos_embed = True
    dropout = 0
    drop_rate_config = [(1, 0)]
    temperature_config = [(1, 0)]

    slf_factor = 0.25
    cyc_factor = 0.5
    adv_factor = 1

    inp_shuffle_len = 0
    inp_unk_drop_fac = 0
    inp_rand_drop_fac = 0
    inp_drop_prob = 0

    run_eval = False
    use_ref = False

def inference(config, fpath, dpath, src_train, src_dev, src_test, tgt_train, tgt_dev, tgt_test, verbose=True):
    if config.data_path == './':
        train_iters, dev_iters, test_iters, vocab = load_dataset(config, train_pos=src_train, train_neg=tgt_train,
                                                                dev_pos=src_dev, dev_neg=tgt_dev,
                                                                test_pos=src_test, test_neg=tgt_test)
    else:
        train_iters, dev_iters, test_iters, vocab = load_dataset(config)

    if verbose:
        print('Vocab size:', len(vocab))
    model_F = StyleTransformer(config, vocab).to(config.device)
    model_D = Discriminator(config, vocab).to(config.device)

    if verbose:
        print(config.discriminator_method)

    if fpath:
        model_F = StyleTransformer(config, vocab).to(config.device)
        model_F.load_state_dict(torch.load(fpath))
        model_F.eval()
    else:
        raise ValueError("Missing path to model_F")
    if dpath:
        model_D = StyleTransformer(config, vocab).to(config.device)
        model_D.load_state_dict(torch.load(dpath))
        model_D.eval()

    config.save_folder = config.save_path + '/' + str(time.strftime('%b%d%H%M%S', time.localtime()))
    os.makedirs(config.save_folder)
    os.makedirs(config.save_folder + '/inference')

    if verbose:
        print('Save Path:', config.save_folder)

    pos_iter = test_iters.pos_iter
    neg_iter = test_iters.neg_iter
    gold_text_neg, raw_output_neg, rev_output_neg = test_eval(vocab, model_F, neg_iter, 0)
    gold_text_pos, raw_output_pos, rev_output_pos = test_eval(vocab, model_F, pos_iter, 1)

    return rev_output_neg, rev_output_pos

def main():
    parser = argparse.ArgumentParser(description='Perform inference on saved model.')
    parser.add_argument('-f', '--fpath', metavar='', help='Path to saved model_F')
    parser.add_argument('-d', '--dpath', metavar='', help='Path to saved model_D')
    args = parser.parse_args()

    config = Config()
    if config.data_path == './data/trump_elon/':
        train_iters, dev_iters, test_iters, vocab = load_dataset(config, train_pos='trump_train.txt', train_neg='elon_train.txt',
                                                                dev_pos='trump_dev.txt', dev_neg='elon_dev.txt',
                                                                test_pos='trump_test.txt', test_neg='elon_test.txt')
    else:
        train_iters, dev_iters, test_iters, vocab = load_dataset(config)
    print('Vocab size:', len(vocab))
    model_F = StyleTransformer(config, vocab).to(config.device)
    model_D = Discriminator(config, vocab).to(config.device)
    print(config.discriminator_method)

    if args.fpath:
        model_F = StyleTransformer(config, vocab).to(config.device)
        model_F.load_state_dict(torch.load(args.fpath))
        model_F.eval()
    else:
        raise ValueError("Missing path to model_F")
    if args.dpath:
        model_D = StyleTransformer(config, vocab).to(config.device)
        model_D.load_state_dict(torch.load(args.dpath))
        model_D.eval()

    config.save_folder = config.save_path + '/' + str(time.strftime('%b%d%H%M%S', time.localtime()))
    os.makedirs(config.save_folder)
    os.makedirs(config.save_folder + '/inference')
    print('Save Path:', config.save_folder)

    pos_iter = test_iters.pos_iter
    neg_iter = test_iters.neg_iter
    gold_text_neg, raw_output_neg, rev_output_neg = test_eval(vocab, model_F, neg_iter, 0)
    gold_text_pos, raw_output_pos, rev_output_pos = test_eval(vocab, model_F, pos_iter, 1)
    with open(config.save_folder + '/inference/' + '/gold_elon_to_trump.txt', 'w') as f:
        for text in gold_text_neg:
            f.write(text + "\n")
    with open(config.save_folder + '/inference/' + '/raw_elon_to_trump.txt', 'w') as f:
        for text in raw_output_neg:
            f.write(text + "\n")
    with open(config.save_folder + '/inference/' + '/rev_elon_to_trump.txt', 'w') as f:
        for text in rev_output_neg:
            f.write(text + "\n")
    with open(config.save_folder + '/inference/' + '/gold_trump_to_elon.txt', 'w') as f:
        for text in gold_text_pos:
            f.write(text + "\n")
    with open(config.save_folder + '/inference/' + '/raw_trump_to_elon.txt', 'w') as f:
        for text in raw_output_pos:
            f.write(text + "\n")
    with open(config.save_folder + '/inference/' + '/rev_trump_to_elon.txt', 'w') as f:
        for text in rev_output_pos:
            f.write(text + "\n")

if __name__ == '__main__':
    main()


import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)
sess_config = tf.ConfigProto()

import sys
import os

COCO_DATA = '/mnt/Disk4/zbfan/coco/'
MASK_RCNN_MODEL_PATH = 'lib/Mask_RCNN/'
ROOT_DIR = os.getcwd()

if MASK_RCNN_MODEL_PATH not in sys.path:
    sys.path.append(MASK_RCNN_MODEL_PATH)

from samples.coco import coco
# from mrcnn import utils
# from mrcnn import model as modellib
# from mrcnn import visualize
from lib.mrcnn import utils
from lib.mrcnn import model as modellib
from lib.mrcnn import visualize

from lib import utils as m_utils
from lib import model as m_model
from lib import config as m_config

import time
import datetime
import random
import numpy as np
import skimage.io
import imgaug
import pickle
import matplotlib.pyplot as plt
from collections import OrderedDict
import keras as K

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

from optparse import OptionParser

class TrainConfig(m_config.Config):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    LEARNING_RATE = 1e-3
    WEIGHT_DECAY = 0.
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
    NAME = 'coco'
    EXPERIMENT = 'matching_relation_lr=e-3'
    CHECKPOINT_DIR = 'checkpoints/'
    # Adapt loss weights
    LOSS_WEIGHTS = {'rpn_class_loss': 2.0,
                    'rpn_bbox_loss': 0.1,
                    'mrcnn_class_loss': 2.0,
                    'mrcnn_embedding_loss': 1.0,
                    'mrcnn_bbox_loss': 0.5,
                    'mrcnn_mask_loss': 1.0}
    DETECTION_MIN_CONFIDENCE = 0.
    DETECTION_NMS_THRESHOLD = 0.5

config = TrainConfig()

train_schedule = OrderedDict()
train_schedule[40] = {"learning_rate": config.LEARNING_RATE, "stage1": "n/a", "stage2": "cls"}
train_schedule[100] = {"learning_rate": config.LEARNING_RATE, "stage1": "n/a", "stage2": "cls"}

parser = OptionParser('')
parser.add_option('--model', dest='model', help='path to weight of the model')
parser.add_option('--dataset', dest='dataset', help='val / train / all is supported')
parser.add_option('--classes', dest='classes', help='which classes to evaluate')
parser.add_option('--ways', dest='ways', help='number of ways to enable')
parser.add_option('--shots', dest='shots', help='number of shots to offer')
parser.add_option('--limit', dest='limit', help='number of images to evaluate in the dataset')
parser.add_option('--verbose', dest='verbose')
parser.add_option('--rois', dest='rois', help='number of random rois to use for stage1')
parser.add_option('--randomroi', dest='randomroi', help='whether or not to use random roi mechanism')
parser.add_option('--nms', dest='nms', help='nms threshold')
parser.add_option('--confidence', dest='confidence', help='minimum detection confidence')
options, args = parser.parse_args()

verbose = 1 if options.verbose is None else int(options.verbose)
limit = 5000 if options.limit is None else int(options.limit)
shots = 1 if options.shots is None else int(options.shots)
ways = 3 if options.ways is None else int(options.ways)
classes_type = options.classes or 'val'
dataset_type = options.classes or 'val'
path = options.model or 'convert_weight/matching_repmet_rpn_lr=e-3_0200.h5'
rois = 1000 if options.rois is None else int(options.rois)
randomroi = True if options.randomroi is None else options.randomroi is True

assert dataset_type in ['train', 'val', 'all']
assert classes_type in ['train', 'val', 'all']

config.USE_RPN_ROIS_INFERENCE = not randomroi
config.POST_NMS_ROIS_INFERENCE = rois
config.WAYS = ways
config.SHOTS = shots
config.SUPPORT_NUMBER = config.WAYS * config.SHOTS
if options.confidence is not None:
    config.DETECTION_MIN_CONFIDENCE = float(options.confidence)
if options.nms is not None:
    config.DETECTION_NMS_THRESHOLD = float(options.nms)

baseline = ['imagenet', 'rpn', 'frcnn'][1]
MODEL_DIR = os.path.join(ROOT_DIR, "logs_" + baseline)
model = m_model.MatchingMaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

model.load_checkpoint(path, training_schedule=train_schedule)

train_classes = []
eval_classes = []
for i in range(1, 81):
    if i % 5 != 0:
        train_classes.append(i)
    else:
        eval_classes.append(i)
train_classes = np.array(train_classes)  #list of class ids
eval_classes = np.array(eval_classes)

datasets = []
if dataset_type == 'train' or dataset_type == 'all':
    coco_train = m_utils.IndexedCocoDataset()
    coco_train_obj = coco_train.load_coco(COCO_DATA, "train", year="2017", return_coco=True)
    coco_train.prepare()
    coco_train.build_indices()
    datasets.append([coco_train, coco_train_obj])
if dataset_type == 'val' or dataset_type == 'all':
    coco_eval = m_utils.IndexedCocoDataset()
    coco_eval_obj = coco_eval.load_coco(COCO_DATA, "val", year="2017", return_coco=True)
    coco_eval.prepare()
    coco_eval.build_indices()
    datasets.append([coco_eval, coco_eval_obj])

classes = []
if classes_type == 'train' or classes_type == 'all':
    classes.append(train_classes)
if classes_type == 'val' or classes_type == 'all':
    classes.append(eval_classes)

random_rois = config.POST_NMS_ROIS_INFERENCE if not config.USE_RPN_ROIS_INFERENCE else 0
for d, d_obj in datasets:
    for c in classes:
        d.ACTIVE_CLASSES = c
        m_utils.evaluate_dataset(model, d, d_obj, limit=limit, random_rois=random_rois, verbose=verbose)


from PIL import Image
import os, sys

path = "path to folder containing images to be resized"
dirs = os.listdir( path )
out = "path to where resized images need to be stored"
def resize():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            imResize = im.resize((224,224), Image.ANTIALIAS)#Here the input image gets resized to 224x224
            imResize.save(out+item, 'JPEG', quality=90)

resize()


import requests
import telebot
import sys
import os
from datetime import datetime
from dotenv import load_dotenv
from telebot.util import split_string

load_dotenv()
API_KEY = os.getenv('API_KEY')
bot = telebot.TeleBot(API_KEY)

base_cowin_url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin"
now = datetime.now()
today_date = now.strftime("%d-%m-%Y")
api_url_telegram = "https://api.telegram.org/bot{}/sendMessage?chat_id=@__groupid__&text=".format(API_KEY)
group_id = "cowin_python"

def fetch_data_from_cowin(pincode):
    query_params = "?pincode={}&date={}".format(pincode[1:], today_date)
    final_url = base_cowin_url+query_params
    hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
    response = requests.get(final_url,headers=hdr)
    return extract_availability_data(response)

def extract_availability_data(response):
    response_json = response.json()
    for center in response_json["centers"]:
        for session in center["sessions"]:
            if session["available_capacity_dose1"] > 0:
                message = " CenterId: {}, \n Name: {}, \n Dose 1 Slots: {}, \n Dose 2 Slots:{}, \n Vaccine name: {}, \n Minimum Age: {} \n ------------------------------------ \n ".format(
                    center["center_id"],center["name"],
                    session["available_capacity_dose1"],
                    session["available_capacity_dose2"],
                    session["vaccine"],
                    session["min_age_limit"]
                )
                send_message_telegram(message)
            else:
                message = "No Slots Available"

def send_message_telegram(message):
    final_telegram_url = api_url_telegram.replace("__groupid__", group_id)
    final_telegram_url = final_telegram_url + message
    response = requests.get(final_telegram_url)

@bot.message_handler(commands=["Hi","Hello","Hey"])
def Welcome(message):
    bot.send_message(message.chat.id,"Hey There, This is Mr. Proffersor, Please type your area pincode with '/' prefix to know the cowin slot availability in your area.")

@bot.message_handler()
def start(message):
    fetch_data_from_cowin(message.text)

if __name__=="__main__":
    bot.polling()


#!/usr/bin/env python
import ast
import configparser
import csv
import os
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
import sys

import numpy as np
import pandas as pd
from lifelines import CoxPHFitter
from lifelines.utils import concordance_index
from sklearn.preprocessing import StandardScaler

from npsurvival_models import BasicSurvival
from survival_datasets import load_dataset
from util import compute_median_survival_time, compute_IPEC_scores

if not (len(sys.argv) == 2 and os.path.isfile(sys.argv[1])):
    print('Usage: python "%s" [config file]' % sys.argv[0])
    sys.exit()

survival_estimator_name = 'coxph'  # for this file
config = configparser.ConfigParser()
config.read(sys.argv[1])
n_experiment_repeats = int(config['DEFAULT']['n_experiment_repeats'])
IPEC_percentiles = ast.literal_eval(config['DEFAULT']['IPEC_percentiles'])
cindex_method = config['DEFAULT']['cindex_method'].strip()
datasets = ast.literal_eval(config['DEFAULT']['datasets'])
output_dir = config['DEFAULT']['output_dir']
os.makedirs(output_dir, exist_ok=True)

output_table_filename = os.path.join(output_dir,
                                     '%s_experiments%d_%s_table.csv'
                                     % (survival_estimator_name,
                                        n_experiment_repeats,
                                        cindex_method))
if os.path.isfile(output_table_filename):
    print('*** Filename already exists: %s' % output_table_filename)
    print('*** Skipping')
    sys.exit()
output_table_file = open(output_table_filename, 'w')
csv_writer = csv.writer(output_table_file)
csv_writer.writerow(['dataset', 'experiment_idx', 'method', 'cindex'] +
                    ['IPEC (%.2f)' % q for q in IPEC_percentiles])

for experiment_idx in range(n_experiment_repeats):
    for dataset in datasets:
        print('[Dataset: %s, experiment: %d]' % (dataset, experiment_idx))
        print()

        X_train, y_train, X_test, y_test, feature_names = \
            load_dataset(dataset, experiment_idx)

        num_IPEC_horizons = len(IPEC_percentiles)
        sorted_train_times = np.sort(y_train[:, 0])
        num_train_times = len(sorted_train_times)
        IPEC_horizons = [sorted_train_times[int(q * num_train_times)]
                         for q in IPEC_percentiles[:-1]]
        IPEC_horizons.append(sorted_train_times[-1])

        print('Testing...')
        scaler = StandardScaler()
        X_train_standardized = scaler.fit_transform(X_train)
        X_test_standardized = scaler.transform(X_test)

        sort_indices = np.argsort(y_train[:, 0])

        train_data_df = \
            pd.DataFrame(np.hstack((X_train_standardized, y_train)),
                         columns=feature_names + ['time', 'status'])

        surv_model = CoxPHFitter()
        surv_model.fit(train_data_df, duration_col='time', event_col='status',
                       show_progress=False, step_size=.1)

        sorted_y_test = np.sort(np.unique(y_test[:, 0]))
        if sorted_y_test[0] != 0:
            mesh_points = np.concatenate(([0.], sorted_y_test))
        else:
            mesh_points = sorted_y_test
        surv = \
            surv_model.predict_survival_function(X_test_standardized,
                                                 mesh_points)
        surv = surv.values.T

        # ---------------------------------------------------------------------
        # compute c-index
        #
        if cindex_method == 'cum_haz':
            cum_haz = \
                surv_model.predict_cumulative_hazard(X_test_standardized,
                                                     sorted_y_test)
            cum_haz = cum_haz.values.T
            cum_hazard_scores = cum_haz.sum(axis=1)
            test_cindex = concordance_index(y_test[:, 0],
                                            -cum_hazard_scores,
                                            y_test[:, 1])
        elif cindex_method == 'cum_haz_from_surv':
            surv_thresholded = np.maximum(surv,
                                          np.finfo(float).eps)
            cum_haz = -np.log(surv_thresholded)
            cum_hazard_scores = cum_haz.sum(axis=1)
            test_cindex = concordance_index(y_test[:, 0],
                                            -cum_hazard_scores,
                                            y_test[:, 1])
        elif cindex_method == 'median':
            predicted_medians = \
                np.array([compute_median_survival_time(mesh_points,
                                                       surv_row)
                          for surv_row in surv])
            test_cindex = concordance_index(y_test[:, 0],
                                            predicted_medians,
                                            y_test[:, 1])
        elif cindex_method == 'median_from_cum_haz':
            cum_haz = \
                surv_model.predict_cumulative_hazard(X_test_standardized,
                                                     sorted_y_test)
            cum_haz = cum_haz.values.T
            predicted_medians = \
                np.array([compute_median_survival_time(mesh_points,
                                                       surv_row)
                          for surv_row in np.exp(-cum_haz)])
            test_cindex = concordance_index(y_test[:, 0],
                                            predicted_medians,
                                            y_test[:, 1])
        else:
            raise NotImplementedError('Unsupported c-index method: %s'
                                      % cindex_method)

        # ---------------------------------------------------------------------
        # compute IPEC score using a few horizon times
        #
        test_IPEC_scores = compute_IPEC_scores(y_train, y_test,
                                               mesh_points, surv,
                                               IPEC_horizons)
        print('c-index: %5.4f' % test_cindex
              + ', '
              + ', '.join(['IPEC (%.2f): %5.4f'
                          % (q, test_IPEC_scores[horizon] / horizon)
                          for q, horizon in
                          zip(IPEC_percentiles,
                              IPEC_horizons)]))

        csv_writer.writerow([dataset, experiment_idx, 'cox', test_cindex] +
                            [test_IPEC_scores[horizon] / horizon
                             for horizon in IPEC_horizons])

        print()
        print()


import torch
from torch import nn
import zounds
from torch.nn import functional as F
from config.experiment import Experiment
from fft_shift import fft_shift
from modules.fft import fft_convolve
from modules.linear import LinearOutputStack

from modules.pos_encode import pos_encoded
from modules.sparse import soft_dirac
from modules.transfer import ImpulseGenerator
from train.experiment_runner import BaseExperimentRunner
from train.optim import optimizer
from upsample import ConvUpsample
from util import device
from util.readmedocs import readme

exp = Experiment(
    samplerate=zounds.SR22050(),
    n_samples=2**15,
    weight_init=0.1,
    model_dim=128,
    kernel_size=512,
    a_weighting=False)

model_dim = exp.model_dim
n_samples = exp.n_samples

n_atoms = 1024
kernel_size = 1024
n_events = 512

class Generator(nn.Module):
    def __init__(self, channels):
        super().__init__()
        self.channels = channels
        self.kernel_size = kernel_size
        self.atoms = nn.Parameter(torch.zeros(n_atoms, kernel_size).uniform_(-1, 1))
        self.to_atom = LinearOutputStack(channels, 3, out_channels=n_atoms, norm=nn.LayerNorm((channels,)))
        self.to_amps = LinearOutputStack(channels, 3, out_channels=1, norm=nn.LayerNorm((channels,)))

    def forward(self, x):
        batch = x.shape[0]
        atoms = soft_dirac(self.to_atom(x))
        amps = self.to_amps(x)
        atoms = atoms @ self.atoms
        atoms = atoms * amps
        return atoms.view(batch, -1, self.kernel_size)

class Scheduler(nn.Module):
    def __init__(self, start_size, final_size, generator, channels, factor=8):
        super().__init__()
        self.generator = Generator(channels)
        self.channels = channels
        self.final_size = final_size
        self.factor = factor

        self.expand = ConvUpsample(
            channels, channels, 8, end_size=n_events, mode='learned', batch_norm=True, out_channels=channels)

        self.to_location = LinearOutputStack(channels, 3, out_channels=1, norm=nn.LayerNorm((channels,)))

        self.amps = LinearOutputStack(channels, 3, out_channels=1, norm=nn.LayerNorm((channels,)))
        self.sched = ImpulseGenerator(final_size, softmax=soft_dirac)

    def forward(self, x):
        batch = x.shape[0]
        x = self.expand(x).view(batch, self.channels, n_events).permute(0, 2, 1) # batch, events, channels

        sig = self.generator(x)

        sched = self.to_location(x)

        amp = torch.abs(self.amps(x))

        sig = F.pad(sig, (0, exp.n_samples - sig.shape[-1]))
        sig = sig * amp

        if sched.shape[-1] > 1:
            impulses = self.sched.forward(sched.view(-1, self.start_size)).view(batch, -1, exp.n_samples)
            final = fft_convolve(sig, impulses[:, None, :])
        else:
            final = fft_shift(sig, sched)[..., :exp.n_samples]

        return final

class Encoder(nn.Module):
    def __init__(self):
        super().__init__()
        encoder = nn.TransformerEncoderLayer(model_dim, 4, model_dim, batch_first=True)
        self.encoder = nn.TransformerEncoder(encoder, 4, norm=nn.LayerNorm((128, model_dim)))
        self.down = nn.Linear(33 + model_dim, model_dim)
        self.pos = nn.Parameter(torch.zeros(1, 128, 33).uniform_(-1, 1))

    def forward(self, x):
        x = exp.pooled_filter_bank(x)
        pos = self.pos.repeat(x.shape[0], 1, 1)
        x = x.permute(0, 2, 1)
        x = torch.cat([x, pos], dim=-1)
        x = self.down(x)
        x = self.encoder(x)
        x = x[:, -1, :]
        return x

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder = Encoder()
        self.decoder = Scheduler(512, exp.n_samples, Generator(exp.model_dim), exp.model_dim)
        self.apply(lambda x: exp.init_weights(x))

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        decoded = torch.sum(decoded, dim=1, keepdim=True)
        return decoded

model = Model().to(device)
optim = optimizer(model, lr=1e-3)

def train_model(batch, i):
    optim.zero_grad()
    recon = model.forward(batch)
    loss = exp.perceptual_loss(recon, batch)
    loss.backward()
    optim.step()
    return loss, recon

@readme
class NerfContinuation(BaseExperimentRunner):
    def __init__(self, stream, port=None):
        super().__init__(stream, train_model, exp, port=port)


import networkx as nx
import numpy as np
from scipy.spatial import Delaunay
import math
import time
from sklearn.cluster import KMeans

""" 构建三角剖分网 """
def create_delaunay(points, indexs):
    """
    :param points: [[lat,lng]...]
    :param indexs: 点的索引
    :return:networkx.Graph
    """
    print("开始构建三角网")
    tri = Delaunay(np.array(points))
    g = nx.Graph()
    edges = []
    for simplice in tri.simplices:
        for edge in [[0, 1], [0, 2], [1, 2]]:
            source = simplice[edge[0]]
            target = simplice[edge[1]]
            weight = math.pow(points[source][0]-points[target][0], 2)+math.pow(points[source][1]-points[target][1], 2)
            edges.append([indexs[source], indexs[target], weight])
    g.add_weighted_edges_from(edges)
    return g

""" 初始化最小生成树 """
def init_min_spaning_tree(graph):
    """
    :param graph: networkx.graph  三角网
    :return: [[source,target],...]
    """
    print("初始化最小生成树")
    edges = list(nx.minimum_spanning_edges(graph,data=False))
    g = nx.Graph()
    g.add_edges_from(edges)
    return edges, g.nodes()

""" 切割树生成子树直到树的数量满足聚类个数 """
def cut_tree(trees, values, n_cluster):
    """
    :param trees: [{edges:初始的最小生成树的边,std:float,node:[]},...] 初始树没有std 和 node
    :param values: 点对应的属性值 np.array
    :param n_cluster: 最终的聚类个数
    :return:
    """
    current_count=len(trees)
    print(f'正在分类 已有{current_count}类')
    if len(trees) < n_cluster:
        #选择属性标准差最大的树切割
        select_tree_edges = trees.pop(0)['edges']
        g = nx.Graph()
        g.add_edges_from(select_tree_edges)
        cut_edges = []
        index=0
        for edge in select_tree_edges:
            index+=1
            print(f'第{current_count}类，第{index}个边')
            degree_source = nx.degree(g, edge[0])
            #选择包含度不为1的点的边切割
            if degree_source > 1:
                degree_target = nx.degree(g, edge[1])
                if degree_target > 1:
                    #切割
                    g.remove_edge(edge[0], edge[1])
                    sub_graphs = nx.connected_component_subgraphs(g)
                    std = max([np.std(values[sub.nodes()]) for sub in sub_graphs])
                    cut_edges.append({'edge': edge, 'std': std})
                    g.add_edge(edge[0], edge[1])
                else:
                    continue
            else:
                continue
        #对该树所有砍边的结果排序，并选择标准差最小的为切割结果
        cut_edges.sort(key=lambda x: x['std'])
        select_cut_edge = cut_edges[0]['edge']
        g.remove_edge(select_cut_edge[0],select_cut_edge[1])
        sub_graphs = nx.connected_component_subgraphs(g)

        for sub in sub_graphs:
            edges = sub.edges()
            nodes = sub.nodes()
            std = np.std(values[nodes])
            if len(nodes) <= 4:
                trees.append({'std': float('-inf'), 'nodes': nodes, "edges": edges})
            else:
                if len(trees) == 0:
                    trees.append({'std': std, 'nodes': nodes, "edges": edges})
                else:
                    for i in range(len(trees)):
                        if std > trees[i]['std']:
                            trees.insert(i, {'std': std, 'nodes': nodes, "edges": edges})
                            break
                    else:
                        trees.append({'std': std, 'nodes': nodes, "edges": edges})
        return cut_tree(trees, values, n_cluster)
    else:
        return trees

""" 根据类别重新构造数据 """
def add_label(data,trees):
    """
    :param data: 原始数据 [{}...]
    :param trees: 最终的分类树 [{'nodes':[...],'std':float,'edges':[[]...]}]
    :return: 添加了聚类标签的原始数据
    """
    for i in range(len(trees)):
        for node in trees[i]['nodes']:
            data[node]['vLabel'] = i
    return data

""" K-Means 预处理 """
def pre_cluster(points, k=40):
    estimator = KMeans(n_clusters=k).fit(points)
    labels = estimator.labels_
    clusters = []
    indexs = []
    for _ in range(k):
        clusters.append([])
        indexs.append([])
    for i in range(len(labels)):
        clusters[labels[i]].append(points[i])
        indexs[labels[i]].append(i)
    return clusters, indexs

def group_min_spaning_tree(data, n_cluster, init_cluster=20):
    points = []
    values = []
    for i in range(len(data)):
        points.append([data[i]['lat'],data[i]['lng']])
        values.append(data[i]['value'])
    values = np.array(values)

    clusters, indexs = pre_cluster(points, init_cluster)
    trees = []
    for i in range(init_cluster):
        if len(clusters[i]) >=4:
            g = create_delaunay(clusters[i], indexs[i])
            init_tree_edges, init_tree_nodes = init_min_spaning_tree(g)
            std = np.std(values[indexs[i]])
            trees.append({'edges': init_tree_edges, 'nodes':init_tree_nodes, 'std': std})
    trees.sort(key=lambda x: x['std'], reverse=True)
    return add_label(data, cut_tree(trees, values, n_cluster))

if __name__ == "__main__":
    import json

    start = time.time()
    with open('../data/Occupation.json', 'r') as fr:
        data = json.load(fr)
    data = group_min_spaning_tree(data, 800, init_cluster=100)
    with open('../data/Occupation_selectNodes.json', 'w') as fw:
        json.dump(data, fw)
    end = time.time()
    print(str(end - start))


class Solution:
    from typing import List
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        #v for i in range(nums)
        n = len(nums)

        res = list()

        def dfs(pos):
            if pos > n:
                return
            elif pos == n:
                res.append(nums)
            else:

                dfs(pos + 1)
            dfs(pos + 1)
if __name__ == "__main__":
    Solu = Solution()
    ans = Solu.nextPermutation([1,2,3])
    print(ans)


import numpy as np
import matplotlib.pyplot as plt

from HelperClass2.NeuralNet_2_2 import *
from HelperClass2.Visualizer_1_1 import *

train_data_name = "ch11.train.npz"
test_data_name = "ch11.test.npz"

def train(n_hidden):

    n_input = dataReader.num_feature
    n_output = dataReader.num_category
    eta, batch_size, max_epoch = 0.1, 10, 10000
    eps = 0.01

    hp = HyperParameters_2_0(n_input, n_hidden, n_output, eta, max_epoch, batch_size, eps, NetType.MultipleClassifier, InitialMethod.Xavier)
    net = NeuralNet_2_2(hp, "Bank_2N3")
    net.train(dataReader, 100, True)
    net.ShowTrainingHistory()
    loss = net.GetLatestAverageLoss()

    fig = plt.figure(figsize=(6,6))
    DrawThreeCategoryPoints(dataReader.XTrain[:,0], dataReader.XTrain[:,1], dataReader.YTrain, hp.toString())
    ShowClassificationResult25D(net, 50, str.format("{0}, loss={1:.3f}", hp.toString(), loss))
    plt.show()

if __name__ == '__main__':
    dataReader = DataReader_2_0(train_data_name, test_data_name)
    dataReader.ReadData()
    dataReader.NormalizeY(NetType.MultipleClassifier, base=1)
    dataReader.NormalizeX()
    dataReader.Shuffle()
    dataReader.GenerateValidationSet()

    train(2)
    train(4)
    train(8)
    train(16)
    train(32)
    train(64)


#Exercise
#On the diabetes dataset, find the optimal regularization parameter alpha.
#Bonus: How much can you trust the selection of alpha?

from sklearn import cross_validation, datasets, linear_model
import copy
import numpy as np

diabetes = datasets.load_diabetes()
X = diabetes.data[:150]
y = diabetes.target[:150]

X_test = diabetes.data[150:]
y_test = diabetes.target[150:]

lasso = linear_model.Lasso()
alphas = np.logspace(-4, -.5, 30)

scores = []

for alpha in alphas :
    print(alpha)
    lasso_alpha = copy.deepcopy(lasso)
    lasso_alpha.alpha = alpha

    scores.append(lasso_alpha.fit(X,y).score(X_test, y_test))

import matplotlib.pyplot as plt

plt.plot(alphas, scores)
plt.xlabel('alpha  ')
plt.ylabel('score  ')
plt.grid(True)

plt.show()


import os
import sys
from datetime import datetime
from os.path import isdir

from PySide2.QtCore import QAbstractItemModel, Qt
from PySide2.QtWidgets import QTableView

class ExportHandler:
    def __init__( self ):
        pass

    # def exportToCsv2( self, tv:QTableView ):
    #     self.exportToCsv( tv.model() )

    def exportToCsv( self, model:QAbstractItemModel, tablename:str="TableExport"  ):
        folder = "./csv"
        now = str( datetime.now() )
        if not isdir( folder ):
            os.makedirs( folder )
        csv = folder + "/" + tablename + "_" + now + ".csv"
        csv = csv.replace( " ", "-" )
        csv = csv.replace( ":", "-" )
        f = open( csv, "w" )
        rows = model.rowCount()
        cols = model.columnCount()
        #  Export headers
        for c in range( 0, cols ):
            header = model.headerData( c, Qt.Horizontal, Qt.DisplayRole )
            f.write( header )
            if not c == cols - 1:
                f.write( "\t" )

        for r in range( 0, rows ):
            f.write( "\n" )
            for c in range( 0, cols ):
                idx = model.index( r, c )
                val = model.data( idx, Qt.DisplayRole )
                if val is None: val = ""
                # print( "val=", val, " - r/c=", r, "/", c )
                if isinstance( val, int ) or isinstance( val, float ):
                    val = str( val )
                    val = val.replace( ".", "," )
                else:
                    if self._isIntOrFloatFormat( val ):
                        val = val.replace( ".", "," )
                    else:
                        val = val.replace( "\t", "   " )
                        val = val.replace( "\n", " " )

                f.write( val )
                if not c == cols - 1:
                    f.write( "\t" )
        f.close()

        if sys.platform.startswith( "linux" ):
            os.system( "xdg-open " + csv )

    @staticmethod
    def _isIntOrFloatFormat( val: str ) -> bool:
        points = 0
        minus = 0
        for c in val:
            if not c.isdigit():
                if c == ".":
                    points = points + 1
                    if points > 1: return False
                elif c == "-":
                    minus = minus + 1
                    if minus > 1: return False
                else:
                    return False
        return True


from utils.draw_digraph import draw_graph
import networkx as nx

def test_draw_graph():
    # This is just a "smoke" test that drawing does not fail with exceptions
    G = nx.DiGraph()
    G.add_nodes_from(list(range(5)))
    G.add_edge(0, 2)
    G.add_edge(1, 2)
    G.add_edge(2, 3)
    G.add_edge(2, 4)
    draw_graph(G)


import PySimpleGUI as sg
from random import randint
from PIL import Image, ImageTk
import YahtzeeScore as YS
import os

window, previous_total, total = None, -1 , 0
dice = [6]*5
rerolls = [0]*5
dice_img = []
ScoreLogged = False
turn = 0
CurrentScore = 0

cwd = os.getcwd()

ScoreCard_Options = ["ones", "twos", "threes", "fours", "fives", "sixes", "Three of a Kind", \
                        "Four of a Kind", "Full House", "Small Straight", "Large Straight", "Yahtzee", "Chance"]

for i in range(len(dice)):
    dice_img += [sg.Image(filename=f'./images/{dice[i]}.png', size=(200,200), key= f'_DieImage{i}_', tooltip = f'Die {i+1}')]

def LaunchScoreWindow():
    layout = [[sg.Text('The second window')],
              [sg.Listbox(ScoreCard_Options, enable_events=True, size=(30,15), k='ScoreOptions')],
              [sg.Button('Accept', k='LogScore')]]
    return sg.Window('Second Window', layout, finalize=True)

def load_image(path, window, frame):
    try:
        image = Image.open(path)
        image.thumbnail((250, 250))
        photo_img = ImageTk.PhotoImage(image)
        window[frame].update(data=photo_img)
    except Exception as e:
        print(f"Unable to open {path}!")
        print(f"error: {e}")

sg.theme('dark grey 9')
layout = [
[sg.Text('Controls:')],
[sg.Button(f'Hold Die {col+1}') for col in range(5)],
[sg.Button('Roll Dice', bind_return_key=True)],
[sg.Text('  3 ',size=(3,1), key='_TOTAL_', font='ANY 20', text_color = 'red')],
[sg.Text(f'Turn {turn}', key = "_Turn_")],
[sg.Column([dice_img])],
]

window = sg.Window('Dice Roller', layout).Finalize()

while True:         #Main Dice Rolling Loop

    if previous_total != total:
        window.Element('_TOTAL_').Update(CurrentScore)
        window.Element('_Turn_').Update(f"Turn: {turn}")

        for i in range(len(dice)):
            load_image(f'./images/{dice[i]}.png',window, f'_DieImage{i}_')

    if turn >= 3:
        print("round over")
        ScoreWindow = LaunchScoreWindow()
        while ScoreLogged != True:      # Loop to log score
            event, values = ScoreWindow.Read()
            print(event)
            print(values)

            if event == sg.WIN_CLOSED or event == "LogScore" and values['ScoreOptions'] == []:
                print("----Error----")
                print("Please select ScoreOption")
                #TODO:insert popup?

            if event == sg.WIN_CLOSED or event == "LogScore" and values['ScoreOptions'] != []:
                print ("Closed Score")
                print (f"final dice: {dice}")

                CurrentScore = YS.Check(values, dice, CurrentScore)

                turn = 0
                total = 0
                dice = [6]*5
                rerolls = [0]*5
                ScoreLogged = True

                for i in range(len(dice)):
                    dice_img += [sg.Image(filename=f'{cwd}/images/{dice[i]}.png', \
                                size=(200,200), \
                                key= f'_DieImage{i}_', \
                                tooltip = f'Die {i+1}')]

                window.Element('_TOTAL_').Update(CurrentScore)
                window.Element('_Turn_').Update(f"Turn: {turn}")

                for i in range(len(dice)):
                    load_image(f'./images/{dice[i]}.png',window, f'_DieImage{i}_')

                print (f"Current Score: {CurrentScore}")

                ScoreCard_Options.remove(values['ScoreOptions'][0])

                ScoreWindow.close()

    else:
        ScoreLogged = False

    while True:      # Loop to read # dice to roll, create list of images
        event, values = window.Read()

        if event is None:
            exit()
        else:
            print(event)

        total = 0
        old_dice = dice

        if event == "Roll Dice":
            for i in range(len(dice)):           # for each die rolled, get the random # and build list of images
                if rerolls[i] == 0:
                    val = randint(1,6)
                    dice[i] = val
                else:
                    dice[i] = old_dice[i]

                total += dice[i]
                dice_img += [sg.Image(filename=f'./images/{dice[i]}.png', size=(250,250), key= f'_DieImage{i}_')]
            turn += 1

            print(turn)
            print(dice)

            break

        if "Hold Die" in event and turn > 0:
            hold_val = int(event[-1])
            if rerolls[hold_val-1] == 1:
                rerolls[hold_val-1] = 0
            elif rerolls[hold_val-1] == 0:
                rerolls[hold_val-1] = 1
            print(rerolls)


import process
import threading
import customtkinter as ctk
from tkinter import filedialog

class InterfaceApp:
    def __init__(self, root: ctk.CTk):
        ctk.set_appearance_mode("Dark")
        ctk.set_default_color_theme("blue")

        self.root = root
        self.root.title("Обезличивание данных")

        self.input_file = ""
        self.button_ds_anon = None
        self.button_k_anon_calc = None
        self.input_file_button = None
        self.input_file_label = None
        self.checkboxes = None
        self.checkbox_labels = None
        self.status_label = None
        self.enabled_instances = None

        self.create_interface()

    def create_interface(self) -> None:
        """ Create a label to show the selected input file """
        self.input_file_label = ctk.CTkLabel(self.root, text="Входной файл: Не выбран")
        self.input_file_label.grid(row=0, column=0, padx=10, pady=10, sticky="w")

        """ Create a button to select the input file """
        self.input_file_button = ctk.CTkButton(self.root, text="Выбрать", command=self.select_input_file)
        self.input_file_button.grid(row=0, column=1, padx=10, pady=10, sticky="w")

        """ Create a text about instances """
        self.status_label = ctk.CTkLabel(self.root, text="Квази-идентификаторы в К-анонимити:")
        self.status_label.grid(row=1, column=0, columnspan=2, padx=10)

        """ Create CTkCheckBox instances with default state set to True """
        self.checkbox_labels = ["ФИО", "Номер телефона", "Адрес работы", "Должность", "З/П, в рублях"]
        self.checkboxes = [ctk.CTkCheckBox(self.root, text=label) for label in self.checkbox_labels]
        for i, checkbox in enumerate(self.checkboxes):
            checkbox.grid(row=i // 2 + 2, column=i % 2, padx=10, pady=10, sticky="w")

        """ Create a button to start dataset depersonalization """
        self.button_ds_anon = ctk.CTkButton(self.root, text="Обезличивание данных", command=self.threading_run_ds_anon)
        self.button_ds_anon.grid(row=5, column=0, padx=10, pady=10, sticky="w")

        """ Create a button to start calculating k-anonymity """
        self.button_k_anon_calc = ctk.CTkButton(self.root, text="Вычисление k-anonymity", command=self.threading_run_k_anon_calc)
        self.button_k_anon_calc.grid(row=5, column=1, padx=10, pady=10, sticky="e")

        """ Create a status label """
        self.status_label = ctk.CTkLabel(self.root, text="")
        self.status_label.grid(row=6, column=0, columnspan=2, pady=10, padx=10)

    def select_input_file(self) -> None:
        self.input_file = filedialog.askopenfilename()
        if self.input_file:
            self.input_file_label.configure(text="Входной файл: Выбран")

    def threading_run_ds_anon(self) -> None:
        t = threading.Thread(target=self.start_depersonalization)
        t.start()

    def threading_run_k_anon_calc(self) -> None:
        t = threading.Thread(target=self.start_k_anon_calc)
        t.start()

    def start_depersonalization(self) -> None:
        if not self.input_file:
            self.status_label.configure(text="Выберете исходный файл!", text_color="red")
            return
        self.status_label.configure(text="В процессе...", text_color="green")
        dataset = process.DatasetDepersonalization(self.input_file, self.status_label)
        dataset.depersonalization()

    def start_k_anon_calc(self) -> None:
        if not self.input_file:
            self.status_label.configure(text="Выберете исходный файл!", text_color="red")
            return
        self.status_label.configure(text="В процессе...", text_color="green")
        self.enabled_instances = [label for label, checkbox in zip(self.checkbox_labels, self.checkboxes) if checkbox.get() == 1]
        dataset = process.DatasetDepersonalization(self.input_file, self.status_label, self.enabled_instances)
        dataset.calc_k_anonymity()

if __name__ == "__main__":
    window = ctk.CTk()
    InterfaceApp(window)
    window.mainloop()


"""
"""
import numpy as np
from time import time
from find_matching_central import matching_central_indices
from astropy.utils.misc import NumpyRNGContext

def test_find_matching_central1():
    central_histories = np.array(((0, 1, 2), (3, 4, 5), (7, 8, 9)))
    satellite_time_indices = np.array((0, 1, 0))
    vals_to_match = np.array((0, 4, 7))
    matching_idx = matching_central_indices(
        central_histories, satellite_time_indices, vals_to_match)
    assert np.all(matching_idx == (0, 1, 2))

def test_find_matching_central2():
    num_centrals = int(1e4)
    num_satellites = int(5e3)
    num_time_steps = 178

    with NumpyRNGContext(43):
        central_histories = np.random.random((num_centrals, num_time_steps))
        satellite_time_indices = np.random.randint(0, num_time_steps, num_satellites)
        vals_to_match = np.random.random(num_satellites)

    start = time()
    matching_idx = matching_central_indices(
        central_histories, satellite_time_indices, vals_to_match)
    end = time()
    runtime = end-start
    assert runtime < 5

    assert np.all(matching_idx < num_centrals)
    assert np.all(matching_idx >= 0)
    matching_cen_vals = central_histories[matching_idx, satellite_time_indices]
    assert np.all(np.abs(matching_cen_vals - vals_to_match) < 0.05)


import pygame

W = 900
H = 600
fps = 30

pygame.init()
screen = pygame.display.set_mode((W,H))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

class IMG(pygame.sprite.Sprite):
    def __init__(self, x, y, file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(file).convert_alpha()
        self.rect = self.image.get_rect(center = (x,y))
    def update(self):
        self.image = pygame.transform.rotate(self.image, 90)

class BT(pygame.sprite.Sprite):
    def __init__(self, x, y, file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(file).convert_alpha()
        self.rect = self.image.get_rect(center = (x,y))
    def is_clicked(self):
        return pygame.mouse.get_pressed()[0] and self.rect.collidepoint(pygame.mouse.get_pos())

stephen = IMG(W//2, H//2.5, '178185_320.jpg')
button = BT(W//2,H//1.2, 'photo_2022-12-30_02-11-48.jpg')

f1 = pygame.font.Font(None, 36)
text1 = f1.render('Покрути Вольфрама!', True,(180, 0, 0))

running = True
screen.fill((200,200,200))
pygame.display.flip()
while running:
    clock.tick(fps)
    screen.fill((200,200,200))
    screen.blit(stephen.image, stephen.rect)
    screen.blit(button.image, button.rect)
    screen.blit(text1, (320, 40))
    if pygame.mouse.get_pressed()[0] and button.rect.collidepoint(pygame.mouse.get_pos()):
        stephen.update()
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = false
pygame.quit()


import atheris
import sys
with atheris.instrument_imports():
    from ansible.errors import (
        AnsibleError,
        AnsibleParserError,
        AnsibleUndefinedVariable
    )
    from ansible.playbook.task import Task

@atheris.instrument_func
def TestInput(input_bytes):
    if len(input_bytes) < 5:
        return

    fdp = atheris.FuzzedDataProvider(input_bytes)

    try:
        task = Task.load(
            {
                'name': fdp.ConsumeString(10),
                'shell': fdp.ConsumeString(10),
                'action': fdp.ConsumeString(10),
                'debug': fdp.ConsumeString(10)
            }
        )

        task.get_vars()
        task.get_include_params()
        task.copy()
        data = task.serialize()
        task.deserialize(data)
    except (
        AnsibleError,
        AnsibleParserError,
        AnsibleUndefinedVariable
    ) as e:
        pass

def main():
    atheris.Setup(sys.argv, TestInput, enable_python_coverage=True)
    atheris.Fuzz()

if __name__ == "__main__":
    main()


#blog-urls
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:blog_id>/', views.blog, name='blog'),
    #path('<int:blog_id>/like/', views.ike, name='like'),
    path('<int:post_id>/edit/', view=views.edit, name='edit'),
    path('<int:blog_id>/comment/', view=views.comment, name='comment'),
    path('new/', view=views.new, name='new'),
]


import plotly.graph_objects as go
import xml.etree.ElementTree as ET
import numpy as np
from PIL import Image
import rasterio
from rasterio.plot import show
from rasterio.merge import merge
import laspy
import open3d
import os
import pandas as pd

import rasterio
import pandas as pd
import math

from pylab import *
import datetime

import time
import rioxarray as rxr
import numpy as np
import xarray as xr
from rasterio.crs import CRS

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.animation as animation
import matplotlib.patches as mpatches
from matplotlib import dates

from pycocotools.coco import COCO
from matplotlib import image
from pathlib import Path

import cv2
import json
from skimage.transform import resize

def main():
    las = laspy.read("./tnris/stratmap18-50cm_2995222a1.laz")
    point_data_1 = np.stack([las.x, las.y, las.z], axis=0).transpose((1, 0))

    las = laspy.read("./tnris/stratmap18-50cm_2995222a3.laz")
    point_data_2 = np.stack([las.x, las.y, las.z], axis=0).transpose((1, 0))

    point_data = np.vstack((point_data_1, point_data_2))

    header = laspy.header.Header()
    outfile = laspy.file.File("output.las", mode="w", header=header)
    outfile.close()

    # geom = open3d.geometry.PointCloud()
    # geom.points = open3d.utility.Vector3dVector(point_data)
    # open3d.visualization.draw_geometries([geom])

def read_img():
    dir = 'tnris'
    filename_list = ['stratmap18-1m_2995222a1.img', 'stratmap18-1m_2995222a3.img']

    rester_list = []
    for f in filename_list:
        rester_list.append(rasterio.open(os.path.join(dir, f)))
        print(rasterio.open(os.path.join(dir, f)).profile)
        show(rasterio.open(os.path.join(dir, f)))

    mosaic, output = merge(rester_list, bounds=polygon.bounds)
    show(mosaic)

def find_file():
    dir = 'tnris'
    coord_list = ['westbc', 'eastbc', 'northbc', 'southbc']
    target_list = [(-95.243, 29.722), (-95.244, 29.715)]

    for target in target_list:

        for f in os.listdir(dir):
            if f.endswith('.xml') and not f.endswith('.img.xml') and not f.endswith('.aux.xml'):
                root = ET.parse(os.path.join(dir, f)).getroot()

                coord = [ float(root.find(f'idinfo/spdom/bounding/{c}').text) for c in coord_list ]
                if (coord[0] - target[0]) * (coord[1] - target[0]) < 0 and (coord[2] - target[1]) * (coord[3] - target[1]) < 0:
                    print(target, f)

def read_tif():
    def remove_outliers(data, sigma=8):
        std = np.std(data)
        mean = np.mean(data)
        data = np.array([ d if abs(d - mean) < sigma * std else mean for d in data ])
        return data

    file_path = 'a5_las_lasda_23.tif'

    dsm = rxr.open_rasterio(file_path, masked=True).squeeze()
    dsm_array = np.array(dsm)

    # check distribution of height information
    fig = plt.figure()
    plt.plot(dsm_array.flatten())
    plt.savefig('outlier.png')

    # remove outliers
    dsm_array = remove_outliers(dsm_array.ravel()).reshape(dsm_array.shape)

    # resize to the image size of the RGB image
    json_file = 'instances_default.json'
    img = COCO(json_file).imgs[1]
    dsm_array = resize(dsm_array, (img['height'], img['width']))
    np.save('dem.npy', dsm_array.astype(float))

    # export the plot
    fig, ax = plt.subplots(dpi=500)
    im = ax.imshow(dsm_array, cmap='viridis', vmin=dsm_array.min(), vmax=dsm_array.max())
    v = np.linspace(dsm_array.min(), dsm_array.max(), 15, endpoint=True)
    fig.colorbar(im, ticks=v)
    plt.savefig('dem.png')

def append_to_las():
    las_list = ['stratmap18-50cm_2995222a3output.las']
    las_out = "stratmap18-50cm_2995222a1output.las"

    for las in las_list:
        with laspy.open(las_out, mode='a') as outlas:
            with laspy.open(las) as inlas:
                for points in inlas.chunk_iterator(2_000_000):
                    outlas.append_points(points)

def print_npy():
    filename = 'label.npy'

    label = np.load(filename)
    df = pd.DataFrame(label)
    df.to_csv('data.csv', index=False)

    print(label)

def mask_filter():
    # json_file = 'combine.json'
    json_file = 'instances_default.json'

    coco = COCO(json_file)

    img = coco.imgs[1]
    blank_mask = np.zeros((img['height'], img['width']))

    # background: 0, asphalt: 1, building: 2, grass: 4, pedestrian walk: 6, tree, 8
    for cat_id in [4, 2, 8, 6]:
        anns_ids = coco.getAnnIds(imgIds=img['id'], catIds=cat_id, iscrowd=None)
        anns = coco.loadAnns(anns_ids)

        if len(anns) != 0:
            mask = coco.annToMask(anns[0])
            for i in range(1, len(anns)):
                mask += coco.annToMask(anns[i])

            mask_mask = np.invert(np.logical_and(mask, blank_mask))
            blank_mask = (mask >= 1) * cat_id + np.multiply(blank_mask, mask_mask)

            plt.imsave(f'output/{cat_id}.png', blank_mask)
            plt.imsave(f'output/{cat_id}_mask.png', mask)

    np.save('label.npy', blank_mask.astype(int))
    lookup = np.array([1, 1, 2, 0, 5, 6, 100, 7, 101])
    blank_mask = lookup[blank_mask.astype(int)]

    # asphalt: 1, building: 2, grass: 5, pedestrian walk: 100, tree, 101
    print_npy()

def shift():
    code = 'C6'

    json_file = f'data/{code}.json'
    data = json.load(open(json_file, "r"))

    # coco = COCO(json_file)
    # img = coco.imgs[1]
    # anns_ids = coco.getAnnIds(imgIds=img['id'])
    # anns = coco.loadAnns(anns_ids)
    shift_list = [6, 5]
    width, height = data['images'][0]['width'] - shift_list[0] * 2, data['images'][0]['height'] - shift_list[1] * 2

    # shift
    for a in data['annotations']:
        a['segmentation'][0] = [coord - shift_list[i % 2]  if coord - shift_list[i % 2] >= 0 else 0 for i, coord in enumerate(a['segmentation'][0])]
        a['segmentation'][0] = [width if coord > width and not i % 2 else coord for i, coord in enumerate(a['segmentation'][0])]
        a['segmentation'][0] = [height if coord > height and i % 2 else coord for i, coord in enumerate(a['segmentation'][0])]

    # copy for RGB image
    anntation_length = len(data['annotations'])
    for a in data['annotations']:
        if anntation_length >= a['id']:
            new_a = a.copy()
            new_a['id'] = anntation_length + new_a['id']
            new_a['image_id'] = 2
            data['annotations'].append(new_a)

    with open("test.json", "w") as f:
        json.dump(data, f)

def resize_annotation():
    code = 'C6'

    json_file = f'data/{code}.json'
    # json_file = 'test.json'
    data = json.load(open(json_file, "r"))
    w1, h1 = data['images'][0]['width'], data['images'][0]['height']

    png_file = f'data/{code}.png'
    image = Image.open(png_file)
    w2, h2 = image.size
    w2, h2 = 2388, 1727

    ratio_w = w2 / w1
    ratio_h = h2 / h1
    ratio = [ratio_w, ratio_h]

    # shift
    for a in data['annotations']:
        a['segmentation'][0] = [coord * ratio[i % 2] for i, coord in enumerate(a['segmentation'][0])]
        a['segmentation'][0] = [w2 if coord > w2 and not i % 2 else coord for i, coord in enumerate(a['segmentation'][0])]
        a['segmentation'][0] = [h2 if coord > h2 and i % 2 else coord for i, coord in enumerate(a['segmentation'][0])]

    with open("test.json", "w") as f:
        json.dump(data, f)

if __name__ == '__main__':
    # main()
    # read_img()
    # find_file()
    # read_tif()

    # append_to_las()
    # print_npy()
    # mask_filter()

    # shift()
    resize_annotation()


from flask import Flask, render_template, url_for, request
from pyrebase import pyrebase

app = Flask(__name__)

Config = {
    "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com",
  "serviceAccount": "path/to/serviceAccountCredentials.json"
  }

firebase = pyrebase.initialize_app(Config)
db = firebase.database()

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        if request.form['submit'] == 'add':
            todo_name = request.form['name']
            db.child("todo").push(todo_name)
            todo = db.child("todo").get()
            todo_value = todo.val()
            return render_template('list.html', todo_value=todo_value.values())
        elif request.form['submit'] == 'delete':
            todo_name = request.form['name']
            db.child("todo").push(todo_name)
            todo = db.child("todo").remove()
            todo_done = "Congratulation! You are Done with all your tasks "
            return render_template('list.html', todo_done=todo_done)
    return render_template('list.html')

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


""" This module contains various helpers for the pepenc project.
"""
import logging
logger = logging.getLogger(__name__)

import torch

def calculate_matched_minkowski_distances(x_1:torch.Tensor, x_2:torch.Tensor, p:float=2.0) -> torch.Tensor:
    """ Calculate the distance between row-wise matched vectors

    This function is only valid for two-dimensional tensors.

    Parameters
    ----------
    x_{1,2} : torch.Tensor
        The two tensors. **N.B.** The tensors must have the same shape (and be located on the same device).

    p : float
        $p$ value for the $p$-norm distance to calculate between the vectors, $p > 0$

    Returns
    -------
    distances : torch.Tensor
        The distance between each pair of row-wise matched vectors. For example, `distances[0] = dist(x_1[0], x_2[0])`.
        Thus, the size of the tensor will be `x_1.shape[0]` (which is the same as `x_2.shape[0]`).
    """
    assert len(x_1.shape) == 2
    assert x_1.shape == x_2.shape

    d = torch.pow(x_1 - x_2, p) # square the element-wise difference
    d = torch.sum(d, 1) # sum for each row
    d = torch.pow(d, 1.0/p) # and take the square root
    return d


from PIL import Image, ImageDraw

def pixelate_image(input_loc, image_name, image_type, output_loc, scale):
    # open image and define dimensions
    original = Image.open(input_loc + image_name + image_type, 'r')
    w, h = original.size
    w = int(w * scale)
    h = int(h * scale)
    size = 1 / scale
    pix = original.load()

    # create output image
    new = Image.new(mode="RGB", size=(int(w * size), int(h * size)))
    draw_new = ImageDraw.Draw(new)

    # draw each pixel of the image
    for j in range(h):
        for i in range(w):
            # store the RGB averages
            color = [0, 0, 0]

            # how large each pixel is
            box_size = (int((j + 1) * size) - int(j * size)) * (int((i + 1) * size) - int(i * size))

            # get the average color in the pixel
            for y in range(int(j * size), int((j + 1) * size)):
                for x in range(int(i * size), int((i + 1) * size)):
                    for code in range(3):
                        color[code] += pix[x, y][code]
            for code in range(3):
                color[code] = int(color[code] / box_size)

            # draw the pixel
            draw_new.rectangle(((int(i * size), int(j * size)), (int((i + 1) * size), int((j + 1) * size))),
                               fill=(color[0], color[1], color[2]))

    # save the image
    new.save(output_loc + image_name + '.png')


"""
DiaBLa: English-French Bilingual dialogue dataset for Machine Translation
https://link.springer.com/article/10.1007/s10579-020-09514-4

Rachel Bawden, Eric Bilinski, Thomas Lavergne and Sophie Rosset
(2021). DiaBLa: A Corpus of Bilingual Spontaneous Written Dialogues
for Machine Translation. Language Resources and Evaluation(55). Pages
635–660. Springer Verlag. 10.1007/s10579-020-09514-4.

DiaBLa is an English-French dataset for the evaluation of Machine
Translation (MT) for informal, written bilingual dialogue.  It
contains 144 spontaneous dialogues (5,700+ sentences) between native
English and French speakers, mediated by one of two neural MT systems
in a range of role-play settings. The dialogues are accompanied by
fine-grained sentence-level judgments of MT quality, produced by the
dialogue participants themselves, as well as by manually normalised
versions and reference translations produced a posteriori

Homepage: http://almanach.inria.fr/software_and_resources/custom/DiaBLa-en.html
"""
from lm_eval.api.task import PromptSourceTask
from typing import List, Tuple, Optional
import datasets
import copy
import numpy as np

_CITATION = """@article{bawden_DiaBLa:-A-Corpus-of_2021,
  author = {Bawden, Rachel and Bilinski, Eric and Lavergne, Thomas and Rosset, Sophie},
  doi = {10.1007/s10579-020-09514-4},
  title = {DiaBLa: A Corpus of Bilingual Spontaneous Written Dialogues for Machine Translation},
  year = {2021},
  journal = {Language Resources and Evaluation},
  publisher = {Springer Verlag},
  volume = {55},
  pages = {635--660},
  url = {https://hal.inria.fr/hal-03021633},
  pdf = {https://hal.inria.fr/hal-03021633/file/diabla-lre-personal-formatting.pdf},
}
"""

class DiaBLa(PromptSourceTask):

    DATASET_PATH = "rbawden/DiaBLa"
    DATASET_NAME = None

    def has_training_docs(self):
        return False

    def has_validation_docs(self):
        return False

    def has_test_docs(self):
        return True

    def training_docs(self):
        if self.has_training_docs():
            return self.dataset["train"]

    def validation_docs(self):
        if self.has_validation_docs():
            return self.dataset["validation"]

    def test_docs(self):
        if self.has_test_docs():
            return self.dataset["test"]

    def max_generation_length(self):
        return 512

    def invalid_doc_for_prompt(self, doc) -> bool:
        if len(self.doc_to_target(doc)) == 0 or self.doc_to_target(doc)[0] == "":
            return True
        return False

class DiaBLa_1_shot_context_same(PromptSourceTask):
    """
    This task is identical to the DiaBLa task, but in the 1-shot setting takes the
    1-shot example from the previous sentence in the dialogue if this is available
    (source sentence and MT output, in the same language direction as the direction
    of the current example). N.B. this task is not currently designed for more than
    1-shot.
    """

    DATASET_PATH = "rbawden/DiaBLa"
    DATASET_NAME = None

    def has_training_docs(self):
        return False

    def has_validation_docs(self):
        return False

    def has_test_docs(self):
        return True

    def training_docs(self):
        if self.has_training_docs():
            return self.dataset["train"]

    def validation_docs(self):
        if self.has_validation_docs():
            return self.dataset["validation"]

    def test_docs(self):
        if self.has_test_docs():
            return self.dataset["test"]

    def max_generation_length(self):
        return 512

    def invalid_doc_for_prompt(self, doc) -> bool:
        if len(self.doc_to_target(doc)) == 0 or self.doc_to_target(doc)[0] == "":
            return True
        return False

    def doc_to_shot_text(self, doc: dict) -> str:
        text, _ = self.shot_prompt_template.apply(doc)
        return text

    def doc_to_shot_target(self, doc: dict) -> List[str]:
        _, target = self.shot_prompt_template.apply(doc)
        return target

    def fewshot_docs(self) -> datasets.Dataset:
        """
        Returns the `dataset` split that the few-shot examples should be sample
        from. This prioritizes the `train_docs` split as the few-shot example
        source, then `validation_docs`, and lastly `test_docs`.
        """
        return self.test_docs()

    # heuristically hack the current template to replace the attributes 'orig' and 'ref' by
    # the original and reference sentences of the previous sentence (if available)
    def get_fewshot_template(self):
        self.shot_prompt_template = copy.deepcopy(self.prompt_template)
        old_jinja = self.shot_prompt_template.jinja
        preamble = '{% set src_sent = ""%}'
        preamble += '{% set trg_sent = "" %}'
        preamble += "{% if dialogue_history|length > 0 %}{% if utterance_meta.lang == dialogue_history[-1].utterance_meta.lang %}{% set src_sent = dialogue_history[-1].orig %}{% set trg_sent = dialogue_history[-1].ref %}{% else %}{% set src_sent = dialogue_history[-1].ref %}{% set trg_sent = dialogue_history[-1].orig %}{% endif %}{% endif %}"
        self.shot_prompt_template.jinja = preamble + old_jinja.replace(
            "{{ orig }}", "{{ src_sent }}"
        ).replace("{{ ref }}", "{{ trg_sent }}")
        return self.shot_prompt_template

    def fewshot_examples(
        self,
        docs: datasets.Dataset,
        k: int,
        rng: np.random.Generator,
        prompt: dict = None,
    ) -> Tuple[List[dict], List[int]]:
        """Returns `k` random examples from the set of documents in `docs`.

        :param docs: datasets.Dataset
            The dataset of documents to sample few-shot examples from.
        :param k: int
            The number of few-shot examples.
        :param rng: np.random.Generator
            The pseudo-random number generator used to randomly sample examples.
        :param prompt: Optional[dict]
            The prompt document. Specify this to ensure the prompt is not in
            the set of few-shot examples.
        """
        # hack the hack to the be used so that it uses other attributes
        self.get_fewshot_template()
        return [prompt], 0

    def fewshot_context(
        self, doc: dict, num_fewshot: int, rng: Optional[np.random.Generator]
    ) -> Tuple[str, dict]:
        """Returns a few-shot context string made up of `num_fewshot` number of
        labeled examples, and an appended prompt example without labeling.

        :param doc: dict
            The document as returned from training_docs, validation_docs, or test_docs.
        :param num_fewshot: int
            The number of fewshot examples to provide in the returned context string.
        :param rng: numpy.random.Generator
            The pseudo-random number generator used to randomly sample few-shot examples.
        :returns: Tuple[str, dict]
            ctx: str
                The fewshot context.
            logging_info: dict
                A `dict` of logging info that can be used to identify few-shot sources.
        """
        assert (
            rng is not None
        ), "A `numpy.random.Generator` argument must be provided to `rng`"

        if num_fewshot == 0:
            labeled_examples = ""
            fewshot_idx, fewshot_target_idx, fewshot_src = ([], [], None)
        else:
            # Construct few-shot labeled examples.
            fewshot_docs = self.fewshot_docs()
            fewshot_src = str(fewshot_docs.split)
            fewshot_examples, fewshot_idx = self.fewshot_examples(
                fewshot_docs, k=num_fewshot, rng=rng, prompt=doc
            )
            labeled_examples_list = []
            fewshot_target_idx = []
            for fewshot_example in fewshot_examples:
                # format the example, but use the previous context of the example
                text = self.doc_to_shot_text(fewshot_example)
                targets = self.doc_to_shot_target(fewshot_example)
                # Choose 1 random target from multi-reference targets.
                target_idx = int(rng.integers(0, len(targets)))
                target = targets[target_idx].strip()
                labeled_examples_list.append(
                    self.format_example(text, target, self.text_target_separator)
                )
                fewshot_target_idx.append(target_idx)
            labeled_examples = self.example_separator.join(labeled_examples_list)
            # Leave an extra `example_separator` right before the prompt.
            labeled_examples += self.example_separator

        prompt = self.doc_to_text(doc)
        ctx = labeled_examples + prompt
        logging_info = {
            "fewshot_idx": fewshot_idx,
            "fewshot_target_idx": fewshot_target_idx,
            "fewshot_source": fewshot_src,
            "fewshot_num": num_fewshot,
            "ctx": ctx,
        }
        return ctx, logging_info

class DiaBLa_1_shot_context_opposite(DiaBLa_1_shot_context_same):
    """
    This task is identical to the DiaBLa task, but in the 1-shot setting takes the
    1-shot example from the previous sentence in the dialogue if this is available
    (source sentence and MT output, in the same language direction as the direction
    of the current example). N.B. this task is not currently designed for more than
    1-shot.
    """

    DATASET_PATH = "rbawden/DiaBLa"
    DATASET_NAME = None

    # heuristically hack the current template to replace the attributes 'orig' and 'ref' by
    # the original and reference sentences of the previous sentence (if available)
    def get_fewshot_template(self):
        self.shot_prompt_template = copy.deepcopy(self.prompt_template)
        old_jinja = self.shot_prompt_template.jinja
        preamble = '{% set src_sent = ""%}'
        preamble += '{% set trg_sent = "" %}'
        preamble += "{% if dialogue_history|length > 0 %}{% if utterance_meta.lang != dialogue_history[-1].utterance_meta.lang %}{% set src_sent = dialogue_history[-1].orig %}{% set trg_sent = dialogue_history[-1].ref %}{% else %}{% set src_sent = dialogue_history[-1].ref %}{% set trg_sent = dialogue_history[-1].orig %}{% endif %}{% endif %}"
        self.shot_prompt_template.jinja = preamble + old_jinja.replace(
            "{{ orig }}", "{{ src_sent }}"
        ).replace("{{ ref }}", "{{ trg_sent }}").replace(
            '{% if utterance_meta.lang == "french" %}',
            '{% if utterance_meta.lang != "french" %}',
        )
        return self.shot_prompt_template

class DiaBLa_1_shot_context_orig(DiaBLa_1_shot_context_same):
    """
    This task is identical to the DiaBLa task, but in the 1-shot setting takes the
    1-shot example from the previous sentence in the dialogue if this is available
    (source sentence and MT output, in the same language direction as the direction
    of the current example). N.B. this task is not currently designed for more than
    1-shot.
    """

    DATASET_PATH = "rbawden/DiaBLa"
    DATASET_NAME = None

    # heuristically hack the current template to replace the attributes 'orig' and 'ref' by
    # the original and reference sentences of the previous sentence (if available)
    def get_fewshot_template(self):
        self.shot_prompt_template = copy.deepcopy(self.prompt_template)
        old_jinja = self.shot_prompt_template.jinja
        preamble = '{% set src_sent = ""%}{% set trg_sent = "" %}'
        preamble += "{% if dialogue_history|length > 0 %}{% set src_sent = dialogue_history[-1].orig %}{% set trg_sent = dialogue_history[-1].ref %}{% endif %}"
        self.shot_prompt_template.jinja = preamble + old_jinja.replace(
            "{{ orig }}", "{{ src_sent }}"
        ).replace("{{ ref }}", "{{ trg_sent }}").replace(
            "utterance_meta.lang", "dialogue_history[-1].utterance_meta.lang"
        )
        return self.shot_prompt_template


import readJSON as rs
import cleanTokenize as ct
import processWords as pw

import time
import gensim

print ("the script was started at ", time.ctime(time.time()))
# first we need to read all the json files
path_to_json = 'document_parses/pdf_json/'
files = rs.readJsonFiles(path_to_json, 10000)

# second we clean the files

cleaned_files = ct.clean_tokenize_multicore(files)

########### start building multi word units ############

#multi_worded_file = pw.multi_word_units_bi(cleaned_files)

#multi_worded_file = pw.multi_word_units(cleaned_files)

#lemmatized_file = pw.tag_lemmatize(multi_worded_file)

# Create Dictionary
beginId2wordTime = time.time()
id2word = gensim.corpora.Dictionary(cleaned_files)
endId2wordTime = time.time()
id2word.save('id2word.dict');
print("id2word took " + str(endId2wordTime - beginId2wordTime))
# Create Corpus: Term Document Frequency
beginCreateCorpus = time.time()
corpus = [id2word.doc2bow(doc) for doc in cleaned_files]
gensim.corpora.MmCorpus.serialize('myBigCorpus.mm', corpus)
endCreateCorpus = time.time()
print(" Creating a corpus took  " + str(endCreateCorpus - beginCreateCorpus))


#!/usr/bin/python3
import getpass
import readline, sys, atexit, pathlib, os, urllib.request, tempfile, subprocess, tarfile, shutil
from typing import List

release_version = "0.6"

def choices(question, choices):
    readline.set_completer(lambda text, state: (choices+[None])[state])
    print("\n")
    while True:
        answer = input(question+" ").lower()
        if answer in choices: return answer
        print("Please enter one of " + ", ".join(choices))

def read_path(question, check):
    def path_completer(f, i):
        dir = os.path.dirname(f)
        #print("DIR",f,dir)
        files = os.listdir("." if dir=="" else dir)
        #print("FILES",files)
        paths = [os.path.join(dir, file) for file in files]
        #print(paths)
        paths = [p for p in paths if p.startswith(f)] + [None]
        selected = paths[i]
        if selected and os.path.isdir(selected): selected = selected + "/"
        #print("SELECTED",selected)
        return selected

    readline.set_completer(path_completer)

    print("\n")
    while True:
        answer = input(question+" ")
        if answer == "": continue
        path = pathlib.Path(answer)
        if check(path): return path

def init_readline():
    readline.parse_and_bind("tab: complete")
    readline.set_completer_delims('')

    try: readline.read_history_file()
    except: pass

    atexit.register(readline.write_history_file)

def dialogue():
    global version, zip_url, isabelle_version, install_isabelle, isabelle_dir, install_afp, afp_dir, qrhl_dir

    ### qrhl-tool installation
    version = choices(f"Which version to install? R = release (v{release_version}); D = development snapshot.", ['d', 'r'])
    if version == 'd':
        isabelle_version = '2021-1'
    elif version == 'r':
        isabelle_version = '2019'
    else:
        sys.exit("Internal error")

    def check(f):
        if f.exists(): print("Directory already exist.")
        if not f.parent.exists(): print("Parent directory does not exist.")
        else: return True
    qrhl_dir = read_path(f"Where should qrhl-tool be installed? (Non-existing directory, e.g., /opt/qrhl-tool.)", check)

    ### Isabelle installation
    install_isabelle = choices(f"qrhl-tool needs Isabelle{isabelle_version}. Should I install it? Y = install, N = use existing installation.", ['y','n'])

    if install_isabelle == 'y':
        def check(f):
            if f.exists(): print("Directory already exists.")
            else: return True
        isabelle_dir = read_path(f"Where should Isabelle be installed? (Non-existing directory, e.g. /opt/Isabelle{isabelle_version})", check)

    else:
        def check(f):
            if not f.exists(): print("Directory does not exist.")
            elif not f.is_dir(): print("Not a directory.")
            elif not f.joinpath(f"Isabelle{isabelle_version}").is_file(): print(f"Not a valid Isabelle installation. Should contain the executable Isabelle{isabelle_version}")
            else: return True
        isabelle_dir = read_path(f"Where is Isabelle{isabelle_version} installed? (Existing directory, e.g. /opt/Isabelle{isabelle_version})", check)

    ### AFP installation
    install_afp = choices(f"qrhl-tool needs the Archive of Formal Proofs (AFP), a version compatible with Isabelle{isabelle_version}. Should I install it? Y = install, N = use existing installation.", ['y', 'n'])

    if install_afp == 'y':
        def check(f):
            if f.exists(): print("Directory already exists.")
            else: return True
        afp_dir = read_path(f"Where should the AFP be installed? (Non-existing directory, e.g. /opt/afp-{isabelle_version})", check)
    else:
        def check(f):
            if not f.exists(): print("Directory does not exist.")
            elif not f.is_dir(): print("Not a directory.")
            elif not f.joinpath(f"thys").is_dir(): print(f"Not a valid AFP installation. Should contain the directory thys.")
            else: return True
        afp_dir = read_path(f"Where is the AFP installed? (Existing directory, e.g. /opt/afp-{isabelle_version})", check)

def summary():
    print(f"""Configuration summary:

* Install {"development snapshot" if version=="d" else f"release {release_version}"}
* qrhl-tool directory: {qrhl_dir}
* Install Isabelle: {install_isabelle}
* Isabelle directory: {isabelle_dir}
* Install AFP: {install_afp}
* AFP directory: {afp_dir}

Press enter to start installation. (Ctrl-C to abort.)
""")
    input()

def do_install_isabelle():
    isabelle_urls = {'2021-1': 'https://isabelle.in.tum.de/dist/Isabelle2021-1_linux.tar.gz',
                     '2019': 'https://isabelle.in.tum.de/website-Isabelle2019/dist/Isabelle2019_linux.tar.gz'}
    url = isabelle_urls[isabelle_version]
    tgz_file = tempdir.joinpath("isabelle.tgz")

    print(f"Downloading Isabelle from {url}...")
    with urllib.request.urlopen(url) as response:
        with open(tgz_file, 'xb') as f:
            f.write(response.read())

    print(f"Extracting Isabelle to {isabelle_dir}...")
    os.mkdir(isabelle_dir)
    subprocess.check_call(["tar","-x","-f",tgz_file.absolute().as_posix(),
                           "-z","--strip-components","1"],
                          cwd = isabelle_dir)

    if isabelle_dir.joinpath(f"Isabelle{isabelle_version}").is_file():
        print("Isabelle successfully installed.")
    else:
        sys.exit(f"Isabelle installation failed. (Does not contain executable Isabelle{isabelle_version}.)")

def do_install_afp():
    afp_urls = {'2021-1': 'https://www.isa-afp.org/release/afp-current.tar.gz',
                '2019': 'https://downloads.sourceforge.net/project/afp/afp-Isabelle2019/afp-2019-08-19.tar.gz?download'}
    url = afp_urls[isabelle_version]
    tgz_file = tempdir.joinpath("afp.tgz")

    print(f"Downloading AFP from {url}...")
    with urllib.request.urlopen(url) as response:
        with open(tgz_file, 'xb') as f:
            f.write(response.read())

    print(f"Extracting AFP to {afp_dir}...")
    os.mkdir(afp_dir)
    subprocess.check_call(["tar","-x","-f",tgz_file.absolute().as_posix(),
                           "-z","--strip-components","1"],
                          cwd = afp_dir)

    if afp_dir.joinpath("thys").is_dir():
        print("AFP successfully installed.")
    else:
        sys.exit(f"AFP installation failed. (Does not contain directory thys.)")

def do_install_qrhl_tool():
    if version == 'd':
        url = 'https://nightly.link/dominique-unruh/qrhl-tool/workflows/test/master/qrhl.zip'
    elif version == 'r':
        url = f'https://github.com/dominique-unruh/qrhl-tool/releases/download/v{release_version}/qrhl.zip'
    else:
        sys.exit("Internal error")

    zip_file = tempdir.joinpath("qrhl.zip")

    print(f"Downloading qrhl-tool from {url}...")
    with urllib.request.urlopen(url) as response:
        with open(zip_file, 'xb') as f:
            f.write(response.read())

    print(f"Extracting qrhl-tool to {qrhl_dir}...")
    os.mkdir(qrhl_dir)
    subprocess.check_call(["unzip","-q","-d", qrhl_dir.as_posix(), zip_file.as_posix()])

    # Extracting nested ZIP if necessary
    content : List[pathlib.Path] = list(qrhl_dir.iterdir())
    if len(content)==1 and content[0].name.lower().endswith(".zip"):
        subprocess.check_call(["unzip","-q","-d", qrhl_dir.as_posix(), content[0].as_posix()])
        os.remove(content[0])

    # Moving stuff from the single subdir in the ZIP one level up
    content = list(qrhl_dir.iterdir())
    if len(content) == 0:
        sys.exit("qrhl-tool extraction failed. (Contains no files).")
    elif len(content) > 1:
        sys.exit("qrhl-tool extraction failed. (Contains multiple directories).")

    subdir = content[0]
    for f in subdir.iterdir():
        shutil.move(str(f), qrhl_dir)
    os.rmdir(subdir)

    if qrhl_dir.joinpath("bin").joinpath("qrhl").is_file():
        print("qrhl-tool successfully installed.")
    else:
        sys.exit(f"qrhl-tool installation failed. (Does not contain file bin/qrhl.)")

def config_qrhl_tool():
    conf_file = qrhl_dir.joinpath("qrhl-tool.conf")
    print(f"Configuring qrhl-tool (creating {conf_file.as_posix()})...")
    with open(conf_file, "wt") as f:
        f.write(f"""# Generated by the qrhl-tool installer. Change the paths of Isabelle and AFP if needed.

isabelle-home = {isabelle_dir}
afp-root = {afp_dir}
""")

def build_qrhl_tool():
    print("""

*************************************************
Installation successful!

Building the Isabelle libraries of the qrhl-tool.
This can take a very long time.

It is safe to interrupt this step. (Ctrl-C)

But in that case, the long process will start the
first time you run the qrhl-tool.
*************************************************
""")
    bin_qrhl = qrhl_dir.joinpath("bin").joinpath("qrhl")

    # In case this script is executed as root (most likely via sudo), try to find the original invoking user and sudo back to them.
    if getpass.getuser()=="root": sudo = ["sudo", "-u", os.getlogin()]
    else: sudo = []

    subprocess.run(sudo + [bin_qrhl.as_posix()], input = b"isabelle.\n", stdout=subprocess.DEVNULL)
    print("Build completed successfully.")

def install():
    global tempdir
    tempdir_object = tempfile.TemporaryDirectory(prefix="qrhl-tool-installer-")
    tempdir = pathlib.Path(tempdir_object.name)

    if install_isabelle == 'y': do_install_isabelle()
    if install_afp == 'y': do_install_afp()
    do_install_qrhl_tool()
    config_qrhl_tool()
    build_qrhl_tool()

    tempdir_object.cleanup()

# For testing purposes. Replace the call to dialogue() below by fake_dialogue() to skip the questions
def fake_dialogue():
    global version, zip_url, isabelle_version, install_isabelle, isabelle_dir, install_afp, afp_dir, qrhl_dir

    install_isabelle = 'n'
    install_afp = 'n'
    isabelle_version = '2021-1'
    version = 'd'
    isabelle_dir = pathlib.Path("/tmp/Isabelle2021-1")
    afp_dir = pathlib.Path("/opt/afp-2021-1")
    qrhl_dir = pathlib.Path("/tmp/qrhl")

init_readline()
dialogue()
summary()
install()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render,redirect
from .models import Articles
from django.contrib.auth.decorators import login_required
from . import forms
# Create your views here.
def articlelist(request):
    #return HttpResponse("about")
    article=Articles.objects.all().order_by('date');
    #return HttpResponse("hello")
    return render(request,'articles/article.html',{'articles':article})
def articledetail(request,slug):
    #return HttpResponse(slug)
    article=Articles.objects.get(slug=slug)
    return render(request,'articles/articledetail.html',{'article':article})
@login_required (login_url='/accounts/login/')
def create(request):
    if request.method=='POST':
        form=forms.CreateArticle(request.POST,request.FILES)
            # validates wheter all fields came etc files is for images
        if form.is_valid():
            # validates whether all fields are corect are not
            #save db
            a=form.save(commit=False)
            a.author=request.user
            a.save()
            return redirect('articles:list')
    else:
        form=forms.CreateArticle()
        return render(request,'articles/create.html',{'form':form })