import torch
from torch.utils.data import Dataset
from my_imfolder import ImageFolder
from torchvision.transforms import ToTensor
from torch.autograd import Variable
import torch.autograd as autograd
import numpy as np
import argparse
import cv2
from tqdm import tqdm
from model import GeneratorNet, DiscriminatorNet

def set_requires_grad(net, switch):
    for param in net.parameters():
        param.requires_grad = switch
    return

def to_var(x, requires_grad=True):
    if torch.cuda.is_available():
        x = x.cuda()
    return Variable(x, requires_grad= requires_grad)

def dcgan_train(data_loader, generator, discriminator, optimizer_G, optimizer_D, criterion, epoch):
    Tensor = torch.FloatTensor
    for e in range(epoch):
        desc = 'Epoch {}'.format(e)
        d_losses = []
        g_losses = []
        t = tqdm(data_loader)
        for i, im in enumerate(t):
            optimizer_G.zero_grad()
            optimizer_D.zero_grad()
            batch_size = im[0].size(0)
            #valid_label = to_var(Tensor(batch_size, 1).fill_(1.0), requires_grad=False)
            valid_label = to_var(Tensor(np.random.uniform(0.7, 1.2, batch_size)), requires_grad=False)
            fake_valid_label = to_var(Tensor(round(batch_size), 1).fill_(1.0), requires_grad=False)
            #fake_label = to_var(Tensor(round(batch_size), 1).fill_(0.0), requires_grad=False)
            fake_label = to_var(Tensor(np.random.uniform(0, 0.3, batch_size)), requires_grad=False)

            # Create Fake Image
            set_requires_grad(discriminator, False)
            noise = to_var(Tensor(np.random.normal(0, 1, (round(batch_size), opts.latent_size))))
            im_fake = generator(noise)
            g_loss = criterion(discriminator(im_fake), fake_valid_label)
            g_losses.append(g_loss.data[0])
            #print('g_loss = {}'.format(g_loss.data))
            if (i+1) % 5 == 0 or e > 30:
                g_loss.backward()
                optimizer_G.step()

            # Discriminate Valid Image
            set_requires_grad(discriminator, True)
            im_valid = to_var(im[0])
            valid_loss = criterion(discriminator(im_valid), valid_label)
            #print('valid_loss = {}'.format(valid_loss))
            fake_loss = criterion(discriminator(im_fake.detach()), fake_label)
            #print('fake_loss = {}'.format(fake_loss))
            d_loss = (valid_loss+fake_loss)/2
            d_losses.append(d_loss.data[0])
            d_loss.backward()
            optimizer_D.step()
            t.set_description('Epoch{},g_loss={:.4f}'.format(e+1, g_loss.data[0]))
        im_fake_output = np.clip(im_fake.cpu().data.numpy(), 0, 1)
        im_fake_output0 = np.round(im_fake_output[0]*255)
        im_fake_output1 = np.round(im_fake_output[1]*255)
        im_fake_output2 = np.round(im_fake_output[2]*255)
        im_fake_output = np.transpose(im_fake_output0 , (1, 2, 0))
        im_fake_output1 = np.transpose(im_fake_output1 , (1, 2, 0))
        im_fake_output2 = np.transpose(im_fake_output2 , (1, 2, 0))
        print(im_fake_output.shape)

        torch.save(generator.state_dict(), 'model/generator_epoch{}.pt'.format(e+1))
        torch.save(discriminator.state_dict(), 'model/discriminator_epoch{}.pt'.format(e+1))
        cv2.imwrite('epoch{}-{}.jpg'.format(e+1, 0), im_fake_output)
        cv2.imwrite('epoch{}-{}.jpg'.format(e+1, 1), im_fake_output1)
        cv2.imwrite('epoch{}-{}.jpg'.format(e+1, 2), im_fake_output2)
        print('d_loss = {}'.format(np.average(d_losses)))
        print('g_loss = {}'.format(np.average(g_losses)))

def compute_gradient_penalty(D, real_samples, fake_samples):
    # Random weight term for interpolation between real and fake samples
    Tensor = torch.FloatTensor
    if torch.cuda.is_available():
        Tensor = torch.cuda.FloatTensor
    alpha = Tensor(np.random.random((real_samples.size(0), 1, 1, 1)))
    # Get random interpolation between real and fake samples
    interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples)).requires_grad_(True)
    d_interpolates = D(interpolates)
    fake = Variable(Tensor(real_samples.shape[0], 1).fill_(1.0), requires_grad=False)
    # Get gradient w.r.t. interpolates
    gradients = autograd.grad(outputs=d_interpolates, inputs=interpolates,
                              grad_outputs=fake, create_graph=True, retain_graph=True,
                              only_inputs=True)[0]
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
    return gradient_penalty

def wgan_train(data_loader, generator, discriminator, optimizer_G, optimizer_D, epoch, clip_val=0.01, LAMBDA=0.1, use_GP=False):
    Tensor = torch.FloatTensor
    for e in range(epoch):
        desc = 'Epoch {}'.format(e)
        for i, im in enumerate(tqdm(data_loader, desc= desc)):
            optimizer_G.zero_grad()
            optimizer_D.zero_grad()
            batch_size = im[0].size(0)
            im_valid = to_var(im[0])

            noise = to_var(Tensor(np.random.normal(0, 1, (batch_size, opts.latent_size))))
            set_requires_grad(discriminator, False)
            im_fake = generator(noise)
            g_loss = -torch.mean(discriminator(im_fake))
            if (i+1) % 7 == 0 or e > 30:
                g_loss.backward()
                optimizer_G.step()

            set_requires_grad(discriminator, True)
            if use_GP:
                gradient_penalty = compute_gradient_penalty(discriminator, im_valid.data, im_fake.data)
                d_loss = -torch.mean(discriminator(im_valid)) + torch.mean(discriminator(im_fake.detach())) + LAMBDA * gradient_penalty
            else:
                d_loss = -torch.mean(discriminator(im_valid)) + torch.mean(discriminator(im_fake.detach()))
            d_loss.backward()
            optimizer_D.step()
            #Weight Clipping
            if not use_GP:
                for param in discriminator.parameters():
                    param.data.clamp_(-clip_val, clip_val)
        im_fake_output = np.clip(im_fake.cpu().data.numpy(), 0, 1)
        im_fake_output = np.round(im_fake_output[0]*255)
        im_fake_output = np.transpose(im_fake_output , (1, 2, 0))
        cv2.imwrite('wgan_epoch{}.jpg'.format(e+1), im_fake_output)
        torch.save(generator.state_dict(), 'model/wgan/generator_epoch{}.pt'.format(e+1))

def main(opts):
    CHANNEL = 3
    mode = 'RGB'
    if opts.use_gray:
        CHANNEL = 1
        mode = 'L'
    im_folder = ImageFolder(root=opts.input, transform= ToTensor(), mode= mode)

    data_loader = torch.utils.data.DataLoader(im_folder,
                                              batch_size=opts.batch_size,
                                              shuffle=True)
    USE_WGAN = opts.use_wgan

    discriminator = DiscriminatorNet(use_WGAN=USE_WGAN, channel=CHANNEL)
    generator = GeneratorNet(input_size=100, channel= CHANNEL)
    criterion_dcgan = torch.nn.BCELoss()

    if torch.cuda.is_available():
        print('Using CUDA')
        discriminator.cuda()
        generator.cuda()
    optimizer_G = torch.optim.Adam(generator.parameters(), lr= 0.0002)
    #optimizer_G = torch.optim.RMSprop(generator.parameters(), lr= 0.0002)
    optimizer_D = torch.optim.Adam(discriminator.parameters(), lr= 0.0002)
    #optimizer_D = torch.optim.RMSprop(discriminator.parameters(), lr= 0.0002)

    if opts.use_wgan or opts.use_wgan_gp:
        print('WGAN Training')
        wgan_train(data_loader,generator,discriminator,optimizer_G,optimizer_D, opts.epoch, use_GP=opts.use_wgan_gp)
    else:
        print('DCGAN Training')
        dcgan_train(data_loader,generator,discriminator,optimizer_G,optimizer_D, criterion_dcgan, opts.epoch)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', type=str, default= 'resized_data', dest='input', help='Animate Face Input File Path')
    parser.add_argument('-b', type=int, default= 128, dest='batch_size', help='Batch Number')
    parser.add_argument('-l', type=int, default= 100, dest='latent_size', help='Latent Number')
    parser.add_argument('-e', type=int, default= 1, dest='epoch', help='Epoch Number')
    parser.add_argument('-WGAN', action= 'store_true', default= False, dest= 'use_wgan', help='use WGAN Weight Clipping')
    parser.add_argument('-WGAN-GP', action= 'store_true', default= False, dest= 'use_wgan_gp', help='use WGAN Weight Clipping')
    parser.add_argument('-GRAY', action= 'store_true', default= False, dest='use_gray', help='use Gray Scale Data Set')
    opts = parser.parse_args()
    main(opts)


'''
Created on Nov 16, 2012

@author: bxs003
'''
class MetalInstance:
    def __init__(self,dimensions,properties):
        """
                This is a part of Substrate Instance
                Keyword arguments:
                dimensions -- tuple (width, length, thickness) of substrate (units in mm)
        """
        self.dimensions = dimensions
        self.properties=properties # information about material name, material resistivity

class DielectricInstance:
    def __init__(self,dimensions,properties):
        """
                This is a part of Substrate Instance
                Keyword arguments:
                dimensions -- tuple (width, length, thickness) of substrate (units in mm)
        """
        self.dimensions = dimensions
        self.properties=properties # information about material name, material resistivity

class SubstrateInstance:
    def __init__(self, dimensions, ledge_width, substrate_tech):
        """
        Keyword arguments:
        dimensions -- tuple (width, length) of substrate (units in mm)
        ledge_width -- (float) width of metal removed around border of substrate (units in mm)
        substrate_tech -- reference of Substrate Tech. Lib. Obj.

        ===substrate_width=====>
        -------|
         metal |<=ledge_width=>
        -----------------------|
          iso                  |
        -----------------------|
         metal |
        -------|

        """
        self.dimensions = dimensions
        self.ledge_width = ledge_width
        self.substrate_tech = substrate_tech

class SubstrateAttachInstance:
    def __init__(self, thickness, attach_tech):
        """
        Keyword arguments:
        thickness -- (float) thickness of substrate attach layer (units in mm)
        attach_tech -- reference of SubstrateAttach Tech. Lib. Obj.
        """
        self.thickness = thickness
        self.attach_tech = attach_tech

class BaseplateInstance:
    def __init__(self, dimensions, eff_conv_coeff, baseplate_tech):
        """
        Keyword arguments:
        dimensions -- tuple (width, length, thickness) (units in mm)
        eff_conv_coeff -- (float) -- (W/m^2) -- "Effective Convection Coefficient"
        baseplate_tech -- reference of Baseplate Tech. Lib. Obj.
        """
        self.dimensions = dimensions
        self.eff_conv_coeff = eff_conv_coeff
        self.baseplate_tech = baseplate_tech

class DeviceInstance:
    def __init__(self, attach_thickness, heat_flow, device_tech, attach_tech):
        """
        Keyword arguments:
        attach_thickness -- device attach thickness (units in mm)
        heat_flow -- estimated amount of power device will dissipate (units in Watts)
        device_tech -- reference of Device Tech. Lib. Obj.
        attach_tech -- reference of DeviceAttach Tech. Lib. Obj.
        """

        self.attach_thickness = attach_thickness
        self.heat_flow = heat_flow
        self.device_tech = device_tech
        self.attach_tech = attach_tech

class ProcessDesignRules:
    def __init__(self, min_trace_trace_width, min_trace_width, min_die_die_dist,
                min_die_trace_dist, power_wire_trace_dist, signal_wire_trace_dist,
                power_wire_component_dist, signal_wire_component_dist):
        """Describes the process design rules for the device.

        Keyword arguments:
        min_trace_trace_width -- minimum trace to trace width
        min_trace_width -- minimum trace width
        min_die_die_dist -- minimum die to die distance
        min_die_trace_dist -- minimum die to trace distance
        power_wire_trace_dist -- minimum power bondwire landing to trace distance
        signal_wire_trace_dist -- minimum signal bondwire landing to trace distance
        power_wire_component_dist -- minimum power bondwire landing to component distance
        signal_wire_component_dist -- minimum signal bondwire landing to component distance

        """
        self.min_trace_trace_width = min_trace_trace_width
        self.min_trace_width = min_trace_width
        self.min_die_die_dist = min_die_die_dist
        self.min_die_trace_dist = min_die_trace_dist
        self.power_wire_trace_dist = power_wire_trace_dist
        self.signal_wire_trace_dist = signal_wire_trace_dist
        self.power_wire_component_dist = power_wire_component_dist
        self.signal_wire_component_dist = signal_wire_component_dist


from __future__ import unicode_literals

from ..permissions import (
    permission_document_version_revert, permission_document_version_view,
)

from .base import GenericDocumentViewTestCase
from .literals import TEST_VERSION_COMMENT
from .mixins import DocumentVersionTestMixin, DocumentVersionViewTestMixin

class DocumentVersionViewTestCase(
    DocumentVersionTestMixin, DocumentVersionViewTestMixin,
    GenericDocumentViewTestCase
):
    def test_document_version_list_no_permission(self):
        self._upload_new_version()

        response = self._request_document_version_list_view()
        self.assertEqual(response.status_code, 404)

    def test_document_version_list_with_access(self):
        self._upload_new_version()
        self.grant_access(
            obj=self.test_document, permission=permission_document_version_view
        )

        response = self._request_document_version_list_view()
        self.assertContains(
            response=response, text=TEST_VERSION_COMMENT, status_code=200
        )

    def test_document_version_revert_no_permission(self):
        first_version = self.test_document.latest_version
        self._upload_new_version()

        response = self._request_document_version_revert_view(
            document_version=first_version
        )
        self.assertEqual(response.status_code, 404)

        self.assertEqual(self.test_document.versions.count(), 2)

    def test_document_version_revert_with_access(self):
        first_version = self.test_document.latest_version
        self._upload_new_version()

        self.grant_access(
            obj=self.test_document, permission=permission_document_version_revert
        )

        response = self._request_document_version_revert_view(
            document_version=first_version
        )
        self.assertEqual(response.status_code, 302)

        self.assertEqual(self.test_document.versions.count(), 1)


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

"""Synchronous program as example that can be turned into a async program."""

# Standard Library Imports
from datetime import datetime
from random import random
import time

def main():
    """Run program."""
    t0 = datetime.now()
    print("Starting the app. Start time: {0}".format(t0))

    data = []
    generate_data(20, data)
    processing_data(20, data)

    dt = datetime.now() - t0
    print("Finished run. Run time: {0:.2f} s".format(dt.total_seconds()))

def generate_data(num: int, data: list):
    """Generate data in the passed in (mutable) list."""
    for index in range(1, num + 1):
        value = index * index
        data.append((value, datetime.now()))

        print("+++ Generated Data: {0}".format(value), flush=True)
        time.sleep(random() + 0.5)  # noqa: S311

def processing_data(num: int, data: list):
    """Process data in the (mutable) list."""
    processed = 0
    while processed < num:
        item = data.pop(0)
        value = item[0]
        creation_time = item[1]
        tunaround_time = datetime.now() - creation_time
        processed += 1

        print("--- Processing Data: {0} --- Turnaround time: {1}".format(
            value,
            tunaround_time,
        ))

        time.sleep(0.5)

if __name__ == "__main__":
    main()


################################################################################
# Geocode data using Google and GeoNames
# Benjamin P. Stewart, May, 2013
# Purpose: Geocoding is something we need to do frequently, so this script will do that.
#   It leverages GeoNames (http://www.geonames.org/) primarily, and then tries to use
#   Google if GeoNames does not use. The reason for this is the request limit for Google
####  Completed by Hamza Haloui, September 2018
################################################################################

import urllib, xlrd, unicodedata, time, json

###USER Variables
inputExcelDocument = r"C:\Users\WB512163\Documents\test.xls"
outputDoc = open(r"C:\Users\WB512163\Documents\test_geocoded.tsv", 'w')
#Define the columns in the inputExcelDocument that describe the place to geocode.
#   The values will be separated by a comma to create a single string
locColumns = [0,0]
sheetIdx = 0 #Zer0-Based!!! If the data is not on the first sheet, you can change it here

#This bit makes the function emulate a firefox request...I really don't know if it is necessary
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)"
headers = { 'User-Agent' : userAgent }
values = { 's' : 'nothing' }
data = urllib.parse.urlencode(values).encode('utf8')

#Open the excel input dictionary, get the first sheet
dataDict = xlrd.open_workbook(inputExcelDocument)
projects = dataDict.sheet_by_index(sheetIdx)
outputDoc.write("OBJECTID\tPostal\tLat\tLng\n")
print("There are %s projects to geocode" % projects.nrows)

#Wrapper for the geo location tools
def geoCode(self, data, headers):
    geogLoc = getGeogLocation(self.rawLocation, data, headers)
    return(geogLoc)

def getJSONresponse(url, data, headers):
    urlReq = urllib.request.Request(url, data, headers)
    req = urllib.request.urlopen(urlReq)
    return(json.loads(req.read()))

# This is where the geolocation action actually happens
def getGeogLocation (location, data, headers):
    location2 = urllib.parse.quote(location) # get the internet version of the location string
    #Perform the geonames.org geolocation
    geoNamesUrl = "http://api.geonames.org/searchJSON?q=" + location2 + "&maxRows=3&username=petie_stewart&featureClass=P"
    jsonResponse = getJSONresponse(geoNamesUrl, data, headers)
    ret = dict(lat=-999, lng=-999)
    # If the geonames result has a length
    geoCodeSource = "nothing"
    if (jsonResponse['totalResultsCount'] > 0):
        res = jsonResponse['geonames'][0]
        ret = dict(lat=res['lat'], lng=res['lng'])
        geoCodeSource = "geonames"
    else:
        googleUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" + location2 + "&sensor=false"
        jsonResponse = getJSONresponse(googleUrl, data, headers)
        if (jsonResponse['status'] == "OK"):
            res = jsonResponse['results'][0]['geometry']['location']
            ret = dict(lat=res['lat'], lng=res['lng'])
            geoCodeSource = "google"
        else:
            print(location + " was not found")
    return({'location':ret, 'source':geoCodeSource})

class projectLocation:
    def __init__(self, curProject, columns):
        try:
            locString = ",".join([str(curProject[k]) for k in columns])
            self.rawLocation = locString #unicodedata.normalize('NFKD', locString)
        except:
            self.source = "StringError"
            self.geogLoc = dict(lat=-999, lng=-999)
            self.rawLocation = ''
        geogCodeResult = geoCode(self, data, headers)
        self.geogLoc = geogCodeResult['location']
        self.source = geogCodeResult['source']

googleCnt = 0
geoNames = 0
missed = 0

for rownum in range(1, projects.nrows):
    curProject = projects.row_values(rownum)
    if rownum < 5:
        print(curProject)
    projLocation = projectLocation(curProject, locColumns)
    if projLocation.source == "StringError":
        print("Error with row %s" % rownum)
        print(curProject)
    geoNames = (geoNames + 1 if projLocation.source == "geonames" else geoNames)
    googleCnt = (googleCnt + 1 if projLocation.source == "google" else googleCnt)
    missed = (missed + 1 if projLocation.source == "nothing" else missed)
    outputDoc.write("%s\t%s\t%s\t%s\t%s\n" % (rownum, projLocation.rawLocation, projLocation.geogLoc['lat'], projLocation.geogLoc['lng'], projLocation.source))
    time.sleep(1)

outputDoc.close()
print("Of %s projects, %s were found in GeoNames, %s were found in Google, %s were not found" %(projects.nrows, geoNames, googleCnt, missed))


# anagram words check

s1 = input("Enter the 1st word: ")
s2 = input("Enter the 2nd word: ")
w1 = sorted(s1)
w2 = sorted(s2)
print("Enter the sorted words w1 = {} & w2 = {}".format(w1,w2))

if w1 == w2:
    print("Words are anagrams")
else:
    print("Words are not anagrams.")


import requests
import json
import datetime
from fam_pay_main.models import VideoInformation
from django.conf import settings
from fam_pay_test.celery import app

@app.task()
def fetch_youtube_data():
	current_start_date = VideoInformation.objects.all().first()

	API_KEY_LIST = settings.API_KEY

	if current_start_date:
		current_start_date = current_start_date.published_at
	else:
		current_start_date = datetime.datetime.now() - datetime.timedelta(days=10)

	end_date = datetime.datetime.now()

	query = "football"

	kwargs = {'part': 'snippet', 'maxResults': 50, 'q': query, 'order': 'date',
	          'publishedAfter': current_start_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
	          'publishedBefore': end_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
	          'type': 'video'}

	index = 0

	while True:
		if index == len(API_KEY_LIST):
			break

		url = "https://www.googleapis.com/youtube/v3/search?key={}".format(API_KEY_LIST[index])
		payload={}
		headers = {}
		response = requests.request("GET", url, headers=headers, params=kwargs)
		if response.status_code == 200:
			results = json.loads(response.text)
			count = 1

			while True:
				count = count + 1
				data = results['items']
				for item in data:
					video_data = {}
					video_data['video_id'] = item['id']['videoId']
					video_data['channel_id'] = item['snippet']['channelId']
					video_data['title'] = item['snippet']['title']
					video_data['description'] = item['snippet']['description']
					video_data['published_at'] = item['snippet']['publishedAt']
					video_data['thumbnail_url'] = item['snippet']['thumbnails']['default']['url']
					VideoInformation.objects.create(**video_data)

				if results.get('nextPageToken') is None:
					break

				kwargs['pageToken'] = results['nextPageToken']
				response = requests.request("GET", url, headers=headers, params=kwargs)
				results = json.loads(response.text)
			break

		elif response.status_code == 403:
			index = index + 1


# SPDX-License-Identifier: Apache-2.0
# based on:
# https://arxiv.org/pdf/1907.12461.pdf
# https://huggingface.co/docs/transformers/model_doc/bertgeneration
# https://medium.com/huggingface/encoder-decoders-in-transformers-a-hybrid-pre-trained-architecture-for-seq2seq-af4d7bf14bb8

from transformers import BertTokenizer, BertTokenizerFast, EncoderDecoderModel, BertGenerationEncoder, BertGenerationDecoder

# add the EOS token as PAD token to avoid warnings
#model = EncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-uncased", "bert-base-uncased")

# leverage checkpoints for Bert2Bert model...
# use BERT's cls token as BOS token and sep token as EOS token
encoder = BertGenerationEncoder.from_pretrained("bert-large-uncased", bos_token_id=101, eos_token_id=102)
# add cross attention layers and use BERT's cls token as BOS token and sep token as EOS token
decoder = BertGenerationDecoder.from_pretrained("bert-large-uncased", add_cross_attention=True, is_decoder=True, bos_token_id=101, eos_token_id=102)
bert2bert = EncoderDecoderModel(encoder=encoder, decoder=decoder)

# create tokenizer...
tokenizer = BertTokenizer.from_pretrained("bert-large-uncased")

# Inputs.
#input_ids = tokenizer('This is a long article to summarize', add_special_tokens=False, return_tensors="pt").input_ids
#labels = tokenizer('This is a short summary', return_tensors="pt").input_ids

# train...
#loss = bert2bert(input_ids=input_ids, decoder_input_ids=labels, labels=labels).loss
#loss.backward()

# encode context the generation is conditioned on
input_ids = tokenizer.encode('I enjoy walking with my cute dog', return_tensors='pt')

# generate text until the output length (which includes the context length) reaches 50
greedy_output = bert2bert.generate(input_ids, max_length=50)
print(f"Output ({greedy_output.shape}): {greedy_output}")
print(f"Detokenized: `{tokenizer.decode(greedy_output[0], skip_special_tokens=False)}`")


from django.contrib import admin
from .models import EventLang, Event
from lib.admin import AdminBase, AdminInlineBase

class EventLangInline(AdminInlineBase):
    model = EventLang

@admin.register(Event)
class EventAdmin(AdminBase):
    list_display = ('id', 'title', 'date', 'langs_available', 'commit', 'issue')
    search_fields = ('id', 'title', 'date')
    inlines = [
        EventLangInline
    ]


import csv

# https://ryanmadden.net/posts/Adapting-Elo

# Expected probability for p1 to win vs. p2
def expected(p1, p2):
    probability = 1 / (1 + 10 ** ( (p2 - p1) / 400) )
    return probability

# New rating of p after facing a team of o1 - o5
def newRating(p, acs, o1, o2, o3, o4, o5, outcome):
    # Get all expected matchups (0.00 to 1.00)
    match1 = expected(p, o1)
    match2 = expected(p, o2)
    match3 = expected(p, o3)
    match4 = expected(p, o4)
    match5 = expected(p, o5)

    # Calculate rating change depending on W/L
    K = acs / 10
    if (outcome == "win"):
        elodiff = K * (5 - match1 - match2 - match3 - match4 - match5)
    if (outcome == "loss"):
        elodiff = (K - 40) * (match1 + match2 + match3 + match4 + match5)

    return p + round(elodiff)


from pathlib import Path
import shutil
import numpy as np
import scipy
import torch
import torch.sparse
from transformers import AutoTokenizer, DataCollatorWithPadding
from utils import DataCollatorForLanguageModeling
from datasets import load_dataset, load_from_disk
from torch.utils.data import DataLoader, WeightedRandomSampler
from scipy.sparse import csr_matrix, coo_matrix
from lightning import LightningDataModule
from typing import List
import pickle
import numpy as np

class DynamicBatchModule(LightningDataModule):
    def __init__(
        self,
        batch_size: List[int] | int,
        effective_batch_size: int | None,
    ):
        super().__init__()
        if isinstance(batch_size, int):
            batch_size = [batch_size]
        self.batch_sizes = batch_size
        self.effective_batch_size = effective_batch_size
        self.batch_size = batch_size[0]

    def adjust_grad_accum(self):
        if self.effective_batch_size is None:
            return

        devices = self.trainer.num_devices * self.trainer.num_nodes
        new_accumulated_batches = self.effective_batch_size // (
            self.batch_size * devices
        )
        if new_accumulated_batches == 0:
            new_accumulated_batches = 1

        if (
            new_accumulated_batches * (self.batch_size * devices)
            != self.effective_batch_size
        ):
            raise ValueError("Inconsistent effective batch size")

        self.trainer.accumulate_grad_batches = new_accumulated_batches
        print(f"Next accumulated batches: {new_accumulated_batches}")

    def adjust_batch_size(self):
        if self.trainer is None:
            return

        new_batch_size = (
            self.batch_sizes[self.trainer.current_epoch]
            if self.trainer.current_epoch < len(self.batch_sizes)
            else self.batch_sizes[-1]
        )
        if self.batch_size != new_batch_size:
            self.batch_size = new_batch_size

        # If we won't have new batch size, we don't need to reload the dataloader
        if self.trainer.current_epoch + 1 < len(self.batch_sizes):
            self.trainer.reload_dataloaders_every_n_epochs = 1
        else:
            print("Won't reload dataloaders")
            self.trainer.reload_dataloaders_every_n_epochs = 0

        print(f"Next batch size: {self.batch_size}")

    def _train_dataloader(self) -> DataLoader:
        raise NotImplementedError()

    def _val_dataloader(self) -> DataLoader:
        raise NotImplementedError()

    def _test_dataloader(self) -> DataLoader:
        raise NotImplementedError()

    def _predict_dataloader(self) -> DataLoader:
        raise NotImplementedError()

    def train_dataloader(self):
        self.adjust_batch_size()
        self.adjust_grad_accum()
        return self._train_dataloader()

    def val_dataloader(self):
        self.adjust_batch_size()
        self.adjust_grad_accum()
        return self._val_dataloader()

    def test_dataloader(self):
        self.adjust_batch_size()
        self.adjust_grad_accum()
        return self._test_dataloader()

    def predict_dataloader(self):
        self.adjust_batch_size()
        self.adjust_grad_accum()
        return self._predict_dataloader()

class NewsDataModule(DynamicBatchModule):
    def __init__(
        self,
        column,
        num_classes,
        tokenizer,
        cache_dir,
        effective_batch_size: int | None,
        max_length=512,
        batch_size: List | int = 12,
        pin_memory=False,
        limit=None,
        num_proc: int = 4,
        pad_mode=True,
        trunc_type="start",
        train_split="train",
        val_split="validation",
        test_split="test",
        predict_split="test",
        time_weighting=False,
        time_weighting_factor=1.0,
    ):
        super().__init__(batch_size, effective_batch_size)
        self.num_proc = num_proc
        self.tokenizer = AutoTokenizer.from_pretrained(tokenizer, use_fast=True)
        self.tokenizer_name = tokenizer
        self.max_length = max_length
        self.column = column
        self.trunc_type = trunc_type
        self.pin_memory = pin_memory

        self.data_collator = DataCollatorWithPadding(
            tokenizer=self.tokenizer, padding=pad_mode, max_length=max_length
        )
        self.cache_dir = Path(cache_dir)
        self.limit = limit
        self.pad_mode = pad_mode
        self.num_classes = num_classes
        self.train_split = train_split
        self.val_split = val_split
        self.test_split = test_split
        self.predict_split = predict_split
        self.time_weighting = time_weighting
        self.time_weighting_factor = time_weighting_factor
        self.weights = None

    def path(self, split):
        return (
            self.cache_dir
            / self.column
            / self.tokenizer_name
            / str(self.max_length)
            / self.trunc_type
            / split
        )

    def __truncate_tokenized(self, tokenized):
        if self.trunc_type == "start":
            truncated = tokenized[: self.max_length]
        elif self.trunc_type == "end":
            truncated = tokenized[-self.max_length :]
        # CLS and SEP tokens
        truncated[0] = tokenized[0]
        truncated[-1] = tokenized[-1]
        return truncated

    def tokenize(self, examples):
        content = examples["content"]
        tokenized = self.tokenizer(content, truncation=False, padding=False)
        for key in ["input_ids", "attention_mask"]:
            tokenized[key] = [self.__truncate_tokenized(x) for x in tokenized[key]]
        return tokenized

    def prepare_dst(self, split):
        if self.path(split).exists():
            # No cache invalidation but don't wanna bother
            return

        dataset = load_dataset(str("hynky/czech_news_dataset"), split=split)
        dataset = dataset.rename_column(self.column, "labels")
        dataset = dataset.filter(
            lambda batch: [x != 0 for x in batch["labels"]],
            batched=True,
            keep_in_memory=True,
        )

        if self.limit:
            dataset = dataset.select(range(self.limit))
        dataset = dataset.remove_columns(
            set(dataset.column_names) - {"content", "labels"}
        )
        dataset = dataset.map(
            self.tokenize,
            batched=True,
        )
        # Remove "Nones"
        # Map to 0-indexed labels
        dataset = dataset.map(
            lambda batch: {"labels": [x - 1 for x in batch["labels"]]},
            batched=True,
            keep_in_memory=True,
        )
        dataset.set_format("pt", columns=["input_ids", "attention_mask", "labels"])

        print("Saving")
        dataset.save_to_disk(self.path(split), num_proc=self.num_proc)

    def get_weights_name(self, split):
        return self.path(split) / f"weights-{self.time_weighting_factor}.pkl"

    def prepare_weights(self, split):
        if (
            not (self.time_weighting and split == "train")
            or (self.get_weights_name(split).exists())
        ):
            return

        dataset = load_dataset(str("hynky/czech_news_dataset"), split=split)
        dataset = dataset.rename_column(self.column, "labels")
        dataset = dataset.filter(
            lambda batch: [x != 0 for x in batch["labels"]],
            batched=True,
            keep_in_memory=True,
        )
        dates = dataset["date"]
        min_date = min(dates)
        dates = np.array([(x - min_date).days for x in dates])
        normalized_dates = dates / np.max(dates) * self.time_weighting_factor
        self.weights = np.exp(normalized_dates) / np.sum(np.exp(normalized_dates))
        # save weights to file
        with open(self.get_weights_name(split), "wb") as f:
            pickle.dump(self.weights, f)

    def prepare_split(self, split):
        self.prepare_dst(split)
        self.prepare_weights(split)

    def load_split(self, split):
        dataset = load_from_disk(
            self.path(split),
        )
        if split == "train" and self.time_weighting:
            with open(self.get_weights_name(split), "rb") as f:
                self.weights = pickle.load(f)

        return dataset

    def prepare_data(self):
        self.prepare_split(self.train_split)
        self.prepare_split(self.val_split)
        self.prepare_split(self.test_split)

    def setup(self, stage=None):
        if stage == "fit" or stage is None:
            self.train_dataset = self.load_split(self.train_split)
            self.val_dataset = self.load_split(self.val_split)

        if stage == "validate" or stage is None:
            self.val_dataset = self.load_split(self.val_split)

        if stage == "test" or stage is None:
            self.test_dataset = self.load_split(self.test_split)

        if stage == "predict" or stage is None:
            self.predict_dataset = self.load_split(self.predict_split)

    def create_dataloader(self, dataset, shuffle=False, weights=None):
        sampler = None
        if weights is not None:
            sampler = WeightedRandomSampler(self.weights, len(self.weights))
            shuffle = False

        return DataLoader(
            dataset,
            batch_size=self.batch_size,
            num_workers=self.num_proc,
            collate_fn=self.data_collator,
            pin_memory=self.pin_memory,
            shuffle=shuffle,
            sampler=sampler,
        )

    def _train_dataloader(self):
        return self.create_dataloader(
            self.train_dataset, shuffle=True, weights=self.weights
        )

    def _val_dataloader(self):
        return self.create_dataloader(self.val_dataset, shuffle=True)

    def _test_dataloader(self):
        return self.create_dataloader(self.test_dataset)

    def _predict_dataloader(self):
        return self.create_dataloader(self.predict_dataset)

class SparseDataset:
    """
    Custom Dataset class for scipy sparse matrix
    """

    def __init__(
        self,
        data: np.ndarray | coo_matrix | csr_matrix,
        targets: np.ndarray | coo_matrix | csr_matrix,
    ):

        # Transform data coo_matrix to csr_matrix for indexing
        if type(data) == coo_matrix:
            self.data = data.tocsr()
        else:
            self.data = data

        # Transform targets coo_matrix to csr_matrix for indexing
        if type(targets) == coo_matrix:
            self.targets = targets.tocsr()
        else:
            self.targets = targets

    def __getitem__(self, index):
        return self.data[index], self.targets[index]

    def __len__(self):
        return self.data.shape[0]

def sparse_coo_to_tensor(coo: scipy.sparse.coo_matrix):
    """
    Transform scipy coo matrix to pytorch sparse tensor
    """
    values = coo.data
    indices = np.vstack((coo.row, coo.col))
    shape = coo.shape

    i = torch.LongTensor(indices)
    v = torch.FloatTensor(values)
    s = torch.Size(shape)

    return torch.sparse_coo_tensor(i, v, s)

def sparse_batch_collate(batch: list):
    """
    Collate function which to transform scipy coo matrix to pytorch sparse tensor
    """
    data_batch, targets_batch = zip(*batch)
    if type(data_batch[0]) == scipy.sparse.csr_matrix:
        data_batch = scipy.sparse.vstack(data_batch).tocoo()
        data_batch = sparse_coo_to_tensor(data_batch)
    else:
        data_batch = torch.FloatTensor(data_batch)

    if type(targets_batch[0]) == scipy.sparse.csr_matrix:
        targets_batch = scipy.sparse.vstack(targets_batch).tocoo()
        targets_batch = sparse_coo_to_tensor(targets_batch)
    else:
        targets_batch = torch.LongTensor(targets_batch)
    return {"labels": targets_batch, "input_ids": data_batch}

class NewsTfidfDataModule(LightningDataModule):
    def __init__(
        self,
        column,
        num_classes,
        num_features,
        data_path: str,
        batch_size=12,
        num_proc: int = 4,
    ):
        super().__init__()
        self.batch_size = batch_size
        self.num_proc = num_proc
        self.column = column
        self.data_path = Path(data_path)
        self.dst_path = "hynky/czech_news_dataset"
        self.num_classes = num_classes
        self.num_features = num_features

    def load_split(self, split):
        labels = np.array(load_dataset(self.dst_path, split=split)[self.column]) - 1
        sparse_input = scipy.sparse.load_npz(
            self.data_path / f"{split}_tfidf_with_metadata.npz"
        )
        if sparse_input.shape[0] != len(labels):
            limit = min(sparse_input.shape[0], len(labels))
            sparse_input = sparse_input[:, :limit]
            labels = labels[:limit]
        keep_idxs = np.where(labels != -1)[0]
        labels = labels[keep_idxs]
        sparse_input = sparse_input[keep_idxs, :]
        return SparseDataset(sparse_input, labels)

    def setup(self, stage=None):
        if stage == "fit" or stage is None:
            self.train_dataset = self.load_split("validation")
            self.val_dataset = self.load_split("validation")
        if stage == "test" or stage is None:
            self.test_dataset = self.load_split("validation")

    def create_dataloader(self, dataset, shuffle=False):
        return DataLoader(
            dataset,
            batch_size=self.batch_size,
            num_workers=self.num_proc,
            collate_fn=sparse_batch_collate,
            shuffle=shuffle,
        )

    def train_dataloader(self):
        return self.create_dataloader(self.train_dataset, shuffle=True)

    def val_dataloader(self):
        return self.create_dataloader(self.val_dataset)

    def test_dataloader(self):
        return self.create_dataloader(self.test_dataset)

class NewsDataModuleForLM(DynamicBatchModule):
    def __init__(
        self,
        tokenizer,
        cache_dir,
        batch_size: List[int] | int,
        effective_batch_size: int | None,
        max_length=512,
        mlm=0.15,
        pin_memory=True,
        limit=None,
        num_proc: int = 4,
    ):
        super().__init__(batch_size, effective_batch_size)
        self.num_proc = num_proc
        self.tokenizer = AutoTokenizer.from_pretrained(tokenizer, use_fast=True)
        self.max_length = max_length
        self.cache_dir = Path(cache_dir)
        self.max_tokenizer_id = max(self.tokenizer.get_vocab().values())
        self.pin_memory = pin_memory
        self.mlm = mlm
        self.limit = limit

    def tokenize_function(self, examples):
        result = self.tokenizer(examples["content"])
        if self.tokenizer.is_fast:
            result["word_ids"] = [
                result.word_ids(i) for i in range(len(result["input_ids"]))
            ]
        return result

    def chunkify(self, examples):
        concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()}
        total_length = len(concatenated_examples[list(examples.keys())[0]])
        total_length = (total_length // self.max_length) * self.max_length
        result = {
            k: [
                t[i : i + self.max_length]
                for i in range(0, total_length, self.max_length)
            ]
            for k, t in concatenated_examples.items()
        }

        result["labels"] = result["input_ids"].copy()
        return result

    def prepare_split(self, split):
        name = "LM"
        split_cache_dir = self.cache_dir / name / str(self.max_length) / split
        if split_cache_dir.exists():
            # No cache invalidation but don't wanna bother
            return

        dataset = load_dataset(str("hynky/czech_news_dataset"), split=split)
        if self.limit:
            dataset = dataset.select(range(self.limit))
        dataset = dataset.remove_columns(set(dataset.column_names) - {"content"})

        dataset = dataset.map(
            self.tokenize_function,
            batched=True,
            keep_in_memory=True,
            remove_columns=["content"],
        )
        dataset = dataset.map(self.chunkify, batched=True, keep_in_memory=True)
        dataset.set_format("pt", columns=["input_ids", "attention_mask", "labels"])
        dataset.save_to_disk(str(split_cache_dir), num_proc=self.num_proc)

    def load_split(self, split):
        dataset = load_from_disk(self.cache_dir / "LM" / str(self.max_length) / split)
        return dataset

    def prepare_data(self):
        self.prepare_split("train")
        self.prepare_split("validation")
        self.prepare_split("test")

    def setup(self, stage=None):
        if stage == "fit" or stage is None:
            self.train_dataset = self.load_split("train")
            self.val_dataset = self.load_split("validation")
        if stage == "validate" or stage is None:
            self.val_dataset = self.load_split("validation")
        if stage == "test" or stage is None:
            self.test_dataset = self.load_split("test")

    def create_dataloader(self, dataset):
        collator = DataCollatorForLanguageModeling(
            self.tokenizer, max_input_id=self.max_tokenizer_id, mlm_probability=self.mlm
        )
        return DataLoader(
            dataset,
            batch_size=self.batch_size,
            pin_memory=self.pin_memory,
            num_workers=self.num_proc,
            collate_fn=collator,
            shuffle=False,
        )

    def _train_dataloader(self):
        return self.create_dataloader(self.train_dataset)

    def _val_dataloader(self):
        return self.create_dataloader(self.val_dataset)

    def _test_dataloader(self):
        return self.create_dataloader(self.test_dataset)


# -*- coding: utf-8 -*-
"""
Created on Wed May  5 12:34:46 2021

@author: Fatemeh
"""
import numpy as np
import pandas as pd
import os, re, sys, glob
from matplotlib.dates import date2num

def poisson_dist_sampling_mean(df, sample_num):
    """for each entry (value) in df, the mean value of random values drown from
    poisson distribution is replaced"""
    df_to_array = df.to_numpy()
    # print(df_to_array.shape)
    row = []
    for i in range(df_to_array.shape[0]):
        column = []
        for j in range(df_to_array.shape[1]):
            column.append(np.mean(np.random.poisson(df_to_array[i][j], sample_num)))
        row.append(column)
    row = pd.DataFrame(row, index = df.index)
    # row.to_csv("test/poisson_distr_"+str(sample_num)+".csv")
    return row

def extract_strain_name(labels):
    ids = []
    for label in labels:
        try:
            ids.append(label[label.find("/") + 1:].split('|')[0])
        except:
            ids.append(label)
    ids = np.asarray(ids)
    return ids

def extract_EPI_id(labels):
    ids = []
    for label in labels:
        try:
            ids.append(label[label.find("EPI"):].split('|')[0])
        except:
            ids.append(label)
    ids = np.asarray(ids)
    return ids

def remove_nans(list_1, list_2):
    """remove every corresponding element in list_2 that has nan value in list_1"""
    nans = np.isnan(list_1)
    nan_indecis = np.where(nans)[0]
    return np.delete(list_2, nan_indecis).tolist()

def read_meta(metadata, id_name):
    print("reading metadata...")
    tsv_read = pd.read_csv(metadata, sep='\t')
    metadata_info = tsv_read.set_index(id_name)
    metadata_info = metadata_info[~metadata_info.index.duplicated(keep='first')]
    return metadata_info[['Accession ID', 'Collection date', 'Virus name']]

def remove_non_val_dates(dates):
    """removes no valid dates from the list of string dates with format 0000-00-00"""
    valid_dates = []
    count = 0
    for date in dates:
        if len(date) == 10:
            try:
                valid = pd.to_datetime(date)
                valid_dates.append(date)
            except:
                count = count + 1
                continue
    # print(count, " non valid dates")
    return valid_dates

def generate_datapoints(dates, mode, time_period, start_delay):
    """from the start date (+ start_delay), it calculates all the dates 'time_period' number of days apart from
    each other, and converts it to a list of numbers if it is datetime"""
    dates = np.sort(dates)
    start = dates[0]
    end = dates[len(dates)-1]
    timepoints = []
    if (mode == 'date'):
        start = pd.to_datetime(start)
        date = start + np.timedelta64(start_delay,'D')
    else:
        date = start + start_delay
    timepoints.append(start)
    while True:
        if (mode == 'date'):
            date =  date + np.timedelta64(time_period,'D')
        else:
            date = date + time_period
        timepoints.append(date)
        if(date >= end):
            break
    dates = timepoints
    if (mode == 'date'):
        timepoints = date2num(timepoints)
    return timepoints, dates


def calc_gpa():
    units=[4,3,2,1]
    print('please, enter the number of courses you are offering below')
    number_of_courses=int(input('>> '))
    count=0
    total_unit=0
    total_quality_point = 0
    while count < number_of_courses:
        print('enter your SCORES and UNITS for each course below')
        score=int(input('your score:'))
        unit=int(input('How many UNIT is the course you entered above: '))
        if score >= 70 and score <= 100 and unit in units:
            point = 5
            total_quality_point += point * unit
            total_unit += unit
        elif score >= 60 and score <= 69 and unit in units:
            point = 4
            total_quality_point += point * unit
            total_unit += unit
        elif score >= 50 and score <= 59 and unit in units:
            point = 3
            total_quality_point += point * unit
            total_unit += unit
        elif score >= 49 and score <= 45 and unit in units:
            point = 2
            total_quality_point += point * unit
            total_unit += unit
        elif score <= 44 and unit in units:
            point = 1
            total_quality_point += point * unit
            total_unit += unit
        count += 1
    gpa= total_quality_point/ total_unit
    rounded_gpa = round(gpa,2)
    return (f' your current GPA is {rounded_gpa}/5.0')

stud_gpa=calc_gpa()
print(stud_gpa)


import nrrd
import numpy as np
import pandas as pd
import json
from treelib import Tree

def read_nrrd(path):
    anno, _ = nrrd.read(path)
    print(anno.shape)
    return anno

def find_region(coords, annotataion, resolution):
    return annotataion[int(coords[0]/resolution), int(coords[1]/resolution), int(coords[2]/resolution)]

def find_unique_regions(annotation):
    region_list = list(np.unique(annotation))
    # if 0 in region_list:
    #     region_list.remove(0)
    return region_list

def _recover_tree(json_file, tree=Tree()):
    for node in json_file:
        if isinstance(node, dict):
            if node['parent_structure_id']==None:
                tree.create_node(identifier=node['id'], data={'acronym':node['acronym'], 'name':node['name']})
            else:
                tree.create_node(identifier=node['id'], data={'acronym':node['acronym'], 'name':node['name']},
                                 parent=node['parent_structure_id'])
            for item in node.keys():
                if isinstance(node[item], list) and len(node[item])>0:
                    _recover_tree(node[item], tree=tree)
    return tree

def allen_brain_tree(json_path):
    info = json.load(open(json_path, 'rb'))['msg']
    return _recover_tree(info, tree=Tree())

def acronym_dict(json_path):
    stl_acro_dict = {}
    tree = allen_brain_tree(json_path)
    for node in tree.nodes.keys():
        stl_acro_dict[node] = tree.nodes[node].data['acronym']
    return stl_acro_dict

def find_name_by_id(node_id, json_path):
    tree = allen_brain_tree(json_path)
    return tree.nodes[node_id].data['name']

def find_acronym_by_id(node_id, json_path):
    tree = allen_brain_tree(json_path)
    return tree.nodes[node_id].data['acronym']

def find_id_by_acronym(acro, json_path):
    tree = allen_brain_tree(json_path)
    for node in tree.nodes.keys():
        if tree.nodes[node].data['acronym'] == acro:
            return node
    return ValueError('Invalid acronym.')

def find_parent(key, layer, json_path):
    tree = allen_brain_tree(json_path)
    if isinstance(key, int):
        id = key
    elif isinstance(key, str):
        id = find_id_by_acronym(key, json_path)
    while tree.depth(id)>=layer:
        id = tree.parent(id).identifier
    if isinstance(key, int):
        return id
    elif isinstance(key, str):
        return find_acronym_by_id(id, json_path)


from django.http import HttpResponse
import time
# Create your views here.

def changeTime(request):
    d = {'0': 'Zero', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four', '5': 'Five', '6': 'Six',
    '7': 'Seven', '8': 'Eight', '9': 'Nine', }

    timeCur = None

    timeCur = (time.strftime("%I:%M") + 'PM')
    t1 = list(timeCur)
    out = ''
    count = 0

    for le in t1:
        for num, word in d.iteritems():
            if num == le:
                out = out + word
                count = count + 1
                if count == 2:
                    out = out + ' ' + ':'
        continue
    out = out + ' PM'
    return HttpResponse(out)


import requests
import wikipediaapi
from difflib import SequenceMatcher

session = requests.Session()

def getRandomArticle():
    URL = "https://en.wikipedia.org/w/api.php"

    PARAMS = {
        "action": "query",
        "format": "json",
        "list": "random",
        "rnlimit": "1",
        "rnnamespace": 0
    }

    R = session.get(url=URL, params=PARAMS)
    DATA = R.json()

    RANDOMS = DATA["query"]["random"]
    return RANDOMS[0]["title"]

def printPageInfo(page):
    print()
    print(page.title)
    print('=' * len(page.title))
    print(page.summary.split('\n')[0][:300], '...')
    print()
    # print('\n'.join(page.links))

print("+----------------------+")
print("| WELCOME TO WIKICHASE |")
print("+----------------------+")
print()
print("May the best wiki-er win.")
print("No cheating!!! (looking at you abe)")
print()
print("(c) Alexander Miller, 2020, all rights reserved, trademarks")

wikipedia = wikipediaapi.Wikipedia('en')
current = wikipedia.page(getRandomArticle())
printPageInfo(current)
# print(current.links)

bad_words = ['category:', 'template:', 'help:', 'module:', 'wikipedia:', 'template talk:', 'file:', 'portal:', 'talk:', 'book:']

counter = 0
while True:
    guess = ''

    loweredToLink = { key.lower(): value for (key, value) in current.links.items() }
    lowered = loweredToLink

    for bad_word in bad_words:
        lowered = list(filter(lambda x : not bad_word in x, lowered))

    while not guess in lowered:
        guess = input("> ").lower()
        if guess == 'help':
            print('\n'.join(lowered))
        else:
            candidates = list(map(lambda x : (x, SequenceMatcher(None, x, guess).ratio()), lowered))
            candidates = list(filter(lambda x : x[1] > 0.8, candidates))
            candidates.sort(reverse=True, key=lambda x: x[1])
            if len(candidates) == 1:
                guess = candidates[0][0]
                print('-->', guess)
            else:
                print('\n'.join([c[0] for c in candidates]))

    current = loweredToLink[guess]
    counter += 1
    print('page', counter)
    printPageInfo(current)


from ENPM808Y_HW3_Main import *
import re
import ast
np.set_printoptions(suppress=True)

def prepData(data):
    f = open(data, "r")

    count = 0
    dataOut = []
    for row in f:

        if count == 0:
            count = count + 1

        else:
            row = re.sub('\s+', ',', row)

            row = list(ast.literal_eval(row))

            count = count + 1

            dataOut.append(row)

    dataOut=np.asarray(dataOut)

    return dataOut

def prb2main():
    wineDesired = prepData('Wine Data/Wine Desired.asc')

    wineInput = prepData('Wine Data/Wine Input.asc')

    PercentTrain=.8

    indexLin = np.arange(0, len(wineInput), 1)
    indexshuf = np.random.shuffle(indexLin)

    trainNdx = indexLin[:int(len(indexLin) * PercentTrain)]
    trainI = wineInput[trainNdx]
    trainO = wineDesired[trainNdx]

    testNdx = indexLin[int(len(indexLin) * PercentTrain):]
    testI = wineInput[testNdx]
    testO = wineDesired[testNdx]

    #   IRIS data for comparaison
    data = np.genfromtxt('iris.csv', delimiter=',', usecols=(0, 1, 2, 3))
    # data normalization
    data = np.apply_along_axis(lambda x: x / np.linalg.norm(x), 1, data)

    data=data
    # fig, ax = plt.subplots(7, 2,sharey=True,sharex=True,squeeze=False)

    # for i in range(trainI.shape[0]):
    # cluster,net = som2Main(input=trainI, epoch=10000, eta=.5,pivot=4,sigma=5)
    # eta = .5, pivot = 2, sigma = 5

    cluster,net=som2MainB(input=trainI, epoch=10000, lr=.7, pivot=5, sigma=7)
    # input = data, epoch = 10000, lr = .7, pivot = 2, sigma = 5 IRIS
    # input = trainI, epoch = 10000, lr = .7, pivot = 5, sigma = 20
    norm = np.linalg.norm(cluster)
    cluster = cluster / norm

    # # print(clusterNorm)
    plt.figure(figsize=(7, 7))
    plt.imshow(cluster, cmap='plasma',interpolation='nearest')
    plt.show()

    dmap=neuronDiff(net)

    plt.figure(figsize=(7, 7))
    plt.pcolor(dmap.T, cmap='bone_r')

    # target = np.genfromtxt('iris.csv', delimiter=',', usecols=(4), dtype=str)
    # t = np.zeros(len(target), dtype=int)
    # t[target == 'Setosa'] = 0
    # t[target == 'Versicolor'] = 1
    # t[target == 'Virginica'] = 2

    # use different colors and markers for each label
    # markers = ['d', 's', 'D']
    # colors = ['C0', 'C1', 'C2']
    # for cnt, data in enumerate(data.T):
    #     y,x,_ = activate(data,net)  # getting the winner
    #     # place a marker on the winning position for the sample xx
    #
    #     plt.plot(x + .5, y + .5, markers[t[cnt]], markerfacecolor='None',
    #                           markeredgecolor=colors[t[cnt]], markersize=12, markeredgewidth=2)
    # plt.axis([0, 8, 0, 8])

    markers = ['d', 's', 'D']
    colors = ['C0', 'C1', 'C2']
    for cnt, data in enumerate(trainI):
        y,x,_ = activate(data,net)  # getting the winner
        # place a marker on the winning position for the sample xx
        if trainO[cnt][2]==1:
            DESG=0
        if trainO[cnt][1]==1:
            DESG=1
        if trainO[cnt][0]==1:
            DESG=2
        plt.plot(x + .5, y + .5, markers[DESG], markerfacecolor='None',
                              markeredgecolor=colors[DESG], markersize=12, markeredgewidth=2)
    plt.axis([0, net.shape[0], 0, net.shape[1]])
    plt.show()

    # plt.figure(figsize=(7, 7))
    # plt.pcolor(dmap.T, cmap='bone_r')
    # markers = ['d', 's', 'D']
    # colors = ['C0', 'C1', 'C2']
    # for cnt, data in enumerate(testI):
    #     y, x, _ = activate(data, net)  # getting the winner
    #     # place a marker on the winning position for the sample xx
    #     if testI[cnt][2] == 1:
    #         DESG = 0
    #     if trainO[cnt][1] == 1:
    #         DESG = 1
    #     if trainO[cnt][0] == 1:
    #         DESG = 2
    #     plt.plot(x + .5, y + .5, markers[DESG], markerfacecolor='None',
    #              markeredgecolor=colors[DESG], markersize=12, markeredgewidth=2)
    # plt.axis([0, net.shape[0], 0, net.shape[1]])
    # plt.show()


from django.db import models
from django.urls import reverse
from django.template.defaultfilters import slugify

STATUS = (
        ('ONGOING', "Ongoing"),
        ('COMPLETED', "Completed"),
    )

FEATURED = (
    (0, 'No'),
    (1, 'Everywhere'),
    (2, 'Front Page')
)
# Create your models here.
class Novel(models.Model):

    name = models.CharField(max_length=200, null=False)
    author = models.CharField(max_length=200)
    picture = models.ImageField(upload_to='static/images')
    description = models.TextField(null=True)
    genre = models.ManyToManyField('Genre', blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length= 40, choices=STATUS)
    featured = models.IntegerField(choices= FEATURED, default=0)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        slug = slugify(f"{self.name}")
        return reverse("novel", kwargs={"id": self.id, "slug": slug})

class Chapter(models.Model):
    id = models.AutoField(primary_key=True, null=False)
    novel = models.ForeignKey(Novel, on_delete=models.CASCADE, null=False)
    name = models.CharField(max_length=1000)
    chapter = models.IntegerField(null=False)
    chapter_content = models.TextField(null=True)

    slug = models.SlugField(unique=True, null=False)

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.slug = slugify(f"{self.novel.name}")
        super(Chapter, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse("chapter", kwargs={"slug": self.slug, "chapter":self.chapter})

class Genre(models.Model):
    name = models.CharField(max_length=500, null=False)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name


# Uses python3
import sys

# def incorrect_optimal_sequence(n):
#     sequence = []
#     while n >= 1:
#         sequence.append(n)
#         if n % 3 == 0:
#             n = n // 3
#         elif n % 2 == 0:
#             n = n // 2
#         else:
#             n = n - 1
#     return reversed(sequence)

# def optimal_sequence2(n):
#     sequence = []
#     while n >= 1:
#         sequence.append(n)
#         if n % 3 == 0:
#             n = n // 3
#         elif (n - 1) % 3 == 0:
#             n -= 1
#         elif n % 2 == 0:
#             n = n // 2
#         else:
#             n = n - 1
#     return reversed(sequence)

def optimal_sequence(n):
    MinOperations = [0] * (n + 1)
    for i in range(1, len(MinOperations)):
        MinOperations[i] = MinOperations[i-1] + 1
        if i % 2 == 0:
            MinOperations[i] = min(MinOperations[i], MinOperations[i // 2] + 1)
        if i % 3 == 0:
            MinOperations[i] = min(MinOperations[i], MinOperations[i // 3] + 1)

    result = [1] * MinOperations[-1]
    for i in range(1, MinOperations[-1]):
        result[-i] = n
        if MinOperations[n-1] == MinOperations[n] - 1:
            n -= 1
        elif n % 2 == 0 and (MinOperations[n // 2] == MinOperations[n] - 1):
            n //= 2
        else:
            n //= 3
    return result

input = sys.stdin.read()
n = int(input)
# print(optimal_sequence(n))
# for i in range(1,n+1):
#     sequence = list(optimal_sequence(i))
#     sequence2 = list(optimal_sequence2(i))
#     print(f"{i}: {len(sequence) - 1}\t{len(sequence2) - 1}")
sequence = list(optimal_sequence(n))
# sequence = list(optimal_sequence2(n))
print(len(sequence) - 1)
for x in sequence:
    print(x, end=' ')

1. Algorithmic Toolbox/coursera resources/week5_dynamic_programming1/2_primitive_calculator/primitive_calculator.py


import typer
from .oracle import Oracle
from .extractive import Extractive, ExtractivePreAbstractive
from .abstractive import Abstractive
from datasets import load_dataset, load_metric
from transformers import AutoTokenizer
from nltk.tokenize import sent_tokenize, word_tokenize
import numpy as np

app = typer.Typer()

@app.command()
def extractive_oracle(data_file: str, output_file: str):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")
    oracle = Oracle()
    new_dataset = oracle.add_oracle_sources_text_to_dataset(dataset)
    new_dataset.to_json(output_file)
    typer.secho("Done", fg=typer.colors.GREEN, bold=True)

@app.command()
def extractive_bert(
    data_file: str,
    output_file: str,
    model_checkpoint: str = "distilbert-base-multilingual-cased",
    pre_abstractive: bool = False,
    abstractive_model_checkpoint: str = "google/mt5-small",
):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")
    ext = (
        Extractive(model_checkpoint=model_checkpoint)
        if not pre_abstractive
        else ExtractivePreAbstractive(
            extractive_model_checkpoint=model_checkpoint,
            abstractive_model_checkpoint=abstractive_model_checkpoint,
        )
    )
    typer.echo(f"Type of Extractive: {type(ext)}")
    new_dataset = ext.add_extractive_summary_to_dataset(dataset)
    new_dataset.to_json(output_file)
    typer.secho("Done", fg=typer.colors.GREEN, bold=True)

@app.command()
def abstractive_mt5(
    data_file: str,
    output_file: str,
    model_checkpoint: str,
    device: int = -1,
    sources_text: str = "oracle_sources_text",
    min_length: int = 200,
    max_length: int = 512,
    num_beams: int = 5,
    no_repeat_ngram_size: int = 3,
):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")
    abs = Abstractive(
        model_checkpoint=model_checkpoint,
        device=device,
        sources_text=sources_text,
        min_length=min_length,
        max_length=max_length,
        num_beams=num_beams,
        no_repeat_ngram_size=no_repeat_ngram_size,
    )
    new_dataset = abs.add_abstractive_summary_to_dataset(dataset)
    new_dataset.to_json(output_file)
    typer.secho("Done", fg=typer.colors.GREEN, bold=True)

@app.command()
def tokenize(
    data_file: str,
    output_file: str,
    max_input_length: int = 512,
    max_target_length: int = 512,
    model_checkpoint: str = "google/mt5-small",
    source: str = "oracle_sources_text",
    target: str = "news",
):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")

    tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)

    def preprocess_function(examples):
        model_inputs = tokenizer(
            examples[source], max_length=max_input_length, truncation=True
        )
        # Set up the tokenizer for targets
        with tokenizer.as_target_tokenizer():
            labels = tokenizer(
                examples[target], max_length=max_target_length, truncation=True
            )

        model_inputs["labels"] = labels["input_ids"]
        return model_inputs

    tokenized_dataset = dataset.map(preprocess_function, batched=True)
    tokenized_dataset.to_json(output_file)
    typer.secho("Done", fg=typer.colors.GREEN, bold=True)

@app.command()
def stats(
    data_file: str,
    output_file: str,
    model_checkpoint: str = "google/mt5-small",
    sources: str = "sources",
    sources_text: str = "sources_text",
    target: str = "news",
):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")

    tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)

    rouge_score = load_metric("rouge")

    def stats_function(example):
        example_sources = example[sources]
        example_sources_text = example[sources_text]
        example_target = example[target]
        nb_of_sources = len(example_sources)
        nb_of_words_sources = len(word_tokenize(example_sources_text))
        sources_text_sent_tokenized = sent_tokenize(example_sources_text)
        nb_of_sentences_sources = len(sources_text_sent_tokenized)
        nb_of_tokens_sources = len(tokenizer(example_sources_text)["input_ids"])
        nb_of_words_target = len(word_tokenize(example_target))
        target_sent_tokenized = sent_tokenize(example_target)
        nb_of_sentences_target = len(target_sent_tokenized)
        nb_of_tokens_target = len(tokenizer(example_target)["input_ids"])

        rouge_score_sources = rouge_score.compute(
            references=["\n".join(target_sent_tokenized)],
            predictions=["\n".join(sources_text_sent_tokenized)],
        )

        if "stats" not in example.keys():
            example["stats"] = {}
        example["stats"][f"{sources_text}"] = {
            "nb": nb_of_sources,
            "nb_sentences": nb_of_sentences_sources,
            "nb_words": nb_of_words_sources,
            "nb_tokens": nb_of_tokens_sources,
            "rouge_score": rouge_score_sources,
        }
        example["stats"][f"{target}"] = {
            "nb_sentences": nb_of_sentences_target,
            "nb_words": nb_of_words_target,
            "nb_tokens": nb_of_tokens_target,
        }
        return example

    new_dataset = dataset.map(stats_function)
    new_dataset.to_json(output_file)
    typer.secho("Done", fg=typer.colors.GREEN, bold=True)

@app.command()
def print_stats(data_file: str, language: str = None):
    dataset = load_dataset("json", data_files={"split": data_file}, split="split")

    if language:

        def filter_by_language(example):
            return example["language"] == language

        dataset = dataset.filter(filter_by_language)

    stats = dataset["stats"]
    results = {}
    for stat in stats:
        for key, value in stat.items():
            if key not in results.keys():
                results[key] = {}
                for k, v in value.items():
                    if k == "rouge_score":
                        results[key]["R1_P"] = [round(v["rouge1"][1][0] * 100, 2)]
                        results[key]["R1_R"] = [round(v["rouge1"][1][1] * 100, 2)]
                        results[key]["R1_F"] = [round(v["rouge1"][1][2] * 100, 2)]
                        results[key]["R2_P"] = [round(v["rouge2"][1][0] * 100, 2)]
                        results[key]["R2_R"] = [round(v["rouge2"][1][1] * 100, 2)]
                        results[key]["R2_F"] = [round(v["rouge2"][1][2] * 100, 2)]
                        results[key]["RL_P"] = [round(v["rougeL"][1][0] * 100, 2)]
                        results[key]["RL_R"] = [round(v["rougeL"][1][1] * 100, 2)]
                        results[key]["RL_F"] = [round(v["rougeL"][1][2] * 100, 2)]
                        results[key]["RLsum_P"] = [round(v["rougeLsum"][1][0] * 100, 2)]
                        results[key]["RLsum_R"] = [round(v["rougeLsum"][1][1] * 100, 2)]
                        results[key]["RLsum_F"] = [round(v["rougeLsum"][1][2] * 100, 2)]
                    else:
                        results[key][k] = [v]
            else:
                for k, v in value.items():
                    if k == "rouge_score":
                        results[key]["R1_P"].append(round(v["rouge1"][1][0] * 100, 2))
                        results[key]["R1_R"].append(round(v["rouge1"][1][1] * 100, 2))
                        results[key]["R1_F"].append(round(v["rouge1"][1][2] * 100, 2))
                        results[key]["R2_P"].append(round(v["rouge2"][1][0] * 100, 2))
                        results[key]["R2_R"].append(round(v["rouge2"][1][1] * 100, 2))
                        results[key]["R2_F"].append(round(v["rouge2"][1][2] * 100, 2))
                        results[key]["RL_P"].append(round(v["rougeL"][1][0] * 100, 2))
                        results[key]["RL_R"].append(round(v["rougeL"][1][1] * 100, 2))
                        results[key]["RL_F"].append(round(v["rougeL"][1][2] * 100, 2))
                        results[key]["RLsum_P"].append(
                            round(v["rougeLsum"][1][0] * 100, 2)
                        )
                        results[key]["RLsum_R"].append(
                            round(v["rougeLsum"][1][1] * 100, 2)
                        )
                        results[key]["RLsum_F"].append(
                            round(v["rougeLsum"][1][2] * 100, 2)
                        )
                    else:
                        results[key][k].append(v)
    for key, value in results.items():
        print(f"===== {key.upper()} =====\n")
        for k, v in value.items():
            print(f"+++ {k} +++")
            print(f"Mean:             {round(np.mean(v), 2)}")
            print(f"99th percentile:  {round(np.percentile(v, 99), 2)}")
            print(f"95th percentile:  {round(np.percentile(v, 95), 2)}")
            print(f"75th percentile:  {round(np.percentile(v, 75), 2)}")
            print(f"50th percentile:  {round(np.percentile(v, 50), 2)}")
            print(f"25th percentile:  {round(np.percentile(v, 25), 2)}")
            print(f"5th percentile:   {round(np.percentile(v, 5), 2)}")
            print(f"1th percentile:   {round(np.percentile(v, 1), 2)}")
            print()
        print("\n\n")

if __name__ == "__main__":
    app()


from formspawner import FormSpawner
from batchspawner import MoabSpawner
from wtforms import BooleanField, StringField, validators, IntegerField, SelectField, SelectMultipleField, Form
from wtforms.fields.html5 import EmailField

class MoabFormSpawner(FormSpawner, MoabSpawner):
    @property
    def options_form(self):
        self.form.project.choices = [('ipm-500-aa', 'ipm-500-aa')]
        return super().options_form

class MoabForm(Form):
    project   = SelectField("Project")
    runtime   = IntegerField('Runtime (hours)', [validators.NumberRange(min=1, max=10)])
    email_addr = EmailField('Email address', [validators.Email()])

c.JupyterHub.spawner_class = MoabFormSpawner
c.MoabFormSpawner.form_class = MoabForm
c.MoabFormSpawner.req_nprocs = '8'
c.MoabFormSpawner.batch_submit_cmd = 'cat'
c.MoabFormSpawner.batch_script = """#!/bin/sh
#PBS -l walltime={user_options[runtime]}:00:00
#PBS -l nodes=1:ppn={nprocs}
#PBS -N jupyterhub-singleuser
#PBS -v {keepvars}
{cmd}
"""


"""
Constraints for 1940 schema. Largely inherited from PL94
"""
import programs.constraints.Constraints_DHCP_HHGQ as Constraints_DHCP_HHGQ
import programs.schema.schemas.schemamaker as schemamaker
from constants import CC
import numpy as np

HHGQ = CC.ATTR_HHGQ_1940

class ConstraintsCreator(Constraints_DHCP_HHGQ.ConstraintsCreator):
    """
    This class is what is used to create constraint objects from what is listed in the config file
    Inputs:
        hist_shape: the shape of the underlying histogram
        invarinats: the list of invaraints (already created)
        constraint_names: the names of the constraints to be created (from the list below)
    """
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.constraint_funcs_dict = {
            "total"                     : self.total,
            "hhgq_total_lb"             : self.hhgq_total_lb,
            "hhgq_total_ub"             : self.hhgq_total_ub
        }

        self.setSchema(buildfunc=None)
        self.setHhgqDimsAndCaps()

    def setSchema(self, buildfunc):
        """ Set which schema is used"""
        self.schema = schemamaker.buildSchema(name=CC.DAS_1940)
        assert self.hist_shape == self.schema.shape, f"Histogram shape in data {self.hist_shape} doesn't correspond to histogram shape in chosen " \
            f"schema {self.schema.shape}"

    def setHhgqDimsAndCaps(self):
        """
        Set dimension of hhgq axis and caps
        """
        # Find index of HHGQ variable in the schema and look the dimension in the histogram shape
        self.gqt_dim = self.hist_shape[self.schema.dimnames.index(HHGQ)]
        # ub cap on number of of people in a gq or hh
        self.hhgq_cap = np.repeat(99999, self.gqt_dim)
        # households
        self.hhgq_cap[0] = 99
        # low bound
        self.hhgq_lb = np.repeat(1, self.gqt_dim)

    def hhgq_total_lb(self, name):
        """
        Lower bound on number of people in each GQ type
        Right-hand side calculation is implemented in the parent class in querybase_constraints.py
        """
        rhs = self.calculateRightHandSideHhgqTotal(upper=False)
        self.addToConstraintsDict(name, HHGQ, rhs, "ge")

    def hhgq_total_ub(self, name):
        """
        Upper bound on number of people in each GQ type
        Right-hand side calculation is implemented in the parent class in querybase_constraints.py
        """
        rhs = self.calculateRightHandSideHhgqTotal(upper=True)
        self.addToConstraintsDict(name, HHGQ, rhs, "le")


import os
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("MYSQL_URL")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

from controller.user.UserController import *
from controller.userdata.controller import *
from middleware.user.UserMiddleware import *

# route for user data
@app.route('/list-user-data', methods=['GET'])
@UserMiddleware
def userDataList():
    controller = UserController(request)
    return controller.ListUserData()

@app.route('/insert-user-data', methods=['POST'])
@UserMiddleware
def addUserData():
    controller = UserController(request)
    return controller.AddUserData()

@app.route('/update-user-data/<int:id>', methods=['PUT'])
@UserMiddleware
def updateUserData(id):
    controller = UserController(request)
    return controller.UpdateUserData(id)

@app.route('/delete-user-data/<int:id>', methods=['DELETE'])
@UserMiddleware
def deleteUserData(id):
    controller = UserController(request)
    return controller.DeleteUserData(id)

@app.route('/login', methods=['POST'])
def login():
    controller = UserDataController(request)
    return controller.login()

@app.route('/insert-user-profile', methods=['POST'])
@UserMiddleware
def addUserProfile():
    controller = UserDataController(request)
    return controller.addUserProfileData()

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


class Client:
    def __init__(self,dpi,name,idEmpresa,idPunto,idDesk,tiempoEspera,tiempoAtencion) :
        self.dpi=dpi
        self.name=name
        self.idEmpresa=idEmpresa
        self.idPunto=idPunto
        self.idDesk=idDesk
        self.tiempoEspera=tiempoEspera
        self.tiempoAtencion=tiempoAtencion
        self.siguienteCliente=None
        self.anteriorCliente=None


import paddle
import paddle.nn as nn
import paddle.nn.functional as F

class PointwiseMatching(nn.Layer):
    def __init__(self, pretrained_model, dropout=None):
        super().__init__()
        self.ptm = pretrained_model
        self.dropout = nn.Dropout(dropout if dropout is not None else 0.1)
        self.classifier = nn.Linear(self.ptm.config["hidden_size"], 2)

    def forward(
        self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None
    ):
        _, cls_embedding = self.ptm(
            input_ids, token_type_ids, position_ids, attention_mask
        )
        cls_embedding = self.dropout(cls_embedding)
        logits = self.classifier(cls_embedding)
        return logits

class PairwiseMatching(nn.Layer):
    def __init__(self, pretrained_model, dropout=None, margin=0.1):
        super().__init__()
        self.ptm = pretrained_model
        self.dropout = nn.Dropout(dropout if dropout is not None else 0.1)
        self.margin = margin
        self.similarity = nn.Linear(self.ptm.config["hidden_size"], 1)

    def predict(
        self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None
    ):
        _, cls_embedding = self.ptm(
            input_ids, token_type_ids, position_ids, attention_mask
        )
        cls_embedding = self.dropout(cls_embedding)
        sim_score = self.similarity(cls_embedding)
        sim_score = F.sigmoid(sim_score)
        return sim_score

    def forward(
        self,
        pos_input_ids,
        neg_input_ids,
        pos_token_type_ids=None,
        neg_token_type_ids=None,
        pos_position_ids=None,
        neg_position_ids=None,
        pos_attention_mask=None,
        neg_attention_mask=None,
    ):
        _, pos_cls_embedding = self.ptm(
            pos_input_ids, pos_token_type_ids, pos_position_ids, pos_attention_mask
        )
        _, neg_cls_embedding = self.ptm(
            neg_input_ids, neg_token_type_ids, neg_position_ids, neg_attention_mask
        )
        pos_embedding = self.dropout(pos_cls_embedding)
        neg_embedding = self.dropout(neg_cls_embedding)
        pos_sim = self.similarity(pos_embedding)
        neg_sim = self.similarity(neg_embedding)
        pos_sim = F.sigmoid(pos_sim)
        neg_sim = F.sigmoid(neg_sim)
        labels = paddle.full(
            shape=[pos_cls_embedding.shape[0]], fill_value=1.0, dtype="float32"
        )
        loss = F.margin_ranking_loss(pos_sim, neg_sim, labels, margin=self.margin)
        return loss


import numpy as np

def ibvs_jacobian(K, pt, z):
    """
    Determine the Jacobian for IBVS.

    The function computes the Jacobian for image-based visual servoing,
    given the camera matrix K, an image plane point, and the estimated
    depth of the point. The x and y focal lengths in K are guaranteed
    to be identical.

    Parameters:
    -----------
    K  - 3x3 np.array, camera intrinsic calibration matrix.
    pt - 2x1 np.array, image plane point.
    z  - Scalar depth value (estimated).

    Returns:
    --------
    J  - 2x6 np.array, Jacobian. The matrix must contain float64 values.
    """

    #--- FILL ME IN ---
    f=K[0][0]
    ubar=pt[0][0]-K[0][2]
    vbar=pt[1][0]-K[1][2]
    J=np.array([[-f/z, 0, ubar/z, (ubar*vbar)/f, -((f**2)+(ubar**2))/f, vbar],
                [0, -f/z, vbar/z, ((f**2)+(vbar**2))/f, -(ubar*vbar)/f, -ubar]])

    #------------------

    correct = isinstance(J, np.ndarray) and \
        J.dtype == np.float64 and J.shape == (2, 6)

    if not correct:
        raise TypeError("Wrong type or size returned!")

    return J


"""Correct Approach: O(n): TIME AND SPACE"""

def productExceptSelf(nums):

    size = len(nums)
    L = [1]*size
    R = [1]*size

    #Create the left product array

    for i in range(1,size):

        L[i] = L[i-1]*nums[i-1]
    print(f" L: {L}")

    for j in range(size-2,-1,-1):
            R[j] = R[j+1]*nums[j+1]

    print(f"R : {R}")

    res = []

    for i in range(size):

        res.append(L[i]*R[i])

    print(f"res : {res}")

    return res

"""Works but werent supposed to use division operator"""

def productExceptSelf(nums):

    hash = {}
    product = nums[0]
    if nums[0] == 0:
        product = 1
        hash[0] = 0
    res = []

    #to calculate the product of all the elements inthe array
    for i in range(1,len(nums)):

        if nums[i] == 0:
            # product = 0
            hash[i] = 0
            continue

        product = product*nums[i]

        print(f"product in loop: {product}")

    print(f"product at the end: {product}")

    print(f"hash: {hash}")
    #when we have a zero in the arrau

    if hash: #implies we have indices with zero in them
        if len(hash) > 1:
            res = [0 for i in range(len(nums))]

        else:
            res = [0 if i not in hash else product for i in range(len(nums))]
        return res

    #to update the result array

    for i in range(len(nums)):

        res.append(int(product/nums[i]))

    return res

if __name__ == "__main__":

    nums = [0,2,3,4]
    # [-1,1,0,-3,3]
    #  [0,0]
    output = productExceptSelf(nums)
    print(f"output: {output}")


from glob import glob
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

sns.set_style("ticks")
sns.set_palette("hls", 8)

RUN = 100
GENS = 1000

run_directory = "/home/sam/Archive/skriegma/XENO_3/run_{}/".format(RUN)
all_of_gen_files = glob(run_directory + "allIndividualsData/Gen_*.txt")
sorted_all_of_gen_files = sorted(all_of_gen_files, reverse=False)

line_hist = []
gen_age_fit_dict = {}

for gen in range(GENS+1):

    gen_age_fit_dict[gen] = {0: 0.0}

    with open(sorted_all_of_gen_files[gen], 'r') as infile:

        next(infile)  # skip header

        for line in infile:
            # this_gen = int(line.split()[0])
            # print this_gen == gen
            # this_id = int(line.split()[1])
            # parent_id = int(line.split()[3])
            # variation_type = line.split()[4]
            this_fit = float(line.split()[5])
            this_age = int(line.split()[7])

            if this_age not in gen_age_fit_dict[gen] or this_fit > gen_age_fit_dict[gen][this_age]:  # ord by fit anyway
                gen_age_fit_dict[gen][this_age] = this_fit  # most fit at each age level

            # if gen > 0 and this_age > 0 and gen_age_fit_dict[gen-1][this_age-1] > gen_age_fit_dict[gen][this_age]:
            #     print "asdf"

    if gen > 0:

        for age in gen_age_fit_dict[gen-1]:

            if age+1 not in gen_age_fit_dict[gen] or gen == 1000:  # extinction

                this_line = []
                n = 0
                while age-n > -1:
                    this_line += [gen_age_fit_dict[gen-1-n][age-n] * 6 / 8.0]
                    n += 1

                # if len(this_line) > 1 and this_line[1] > this_line[0]:
                #     print this_line

                pre_fill = [None]*(gen-age)
                # post_fill = [None]*(GENS-gen)
                line_hist += [pre_fill + list(this_line[::-1])]

fig, ax = plt.subplots(1, 1, figsize=(4, 3))

for line in line_hist:
    ax.plot(range(len(line)), line, linewidth=0.8)

ax.set_xlim([-10, GENS])
ax.set_ylim([-0.1, 8])
ax.text(50, 7, "Run {}".format(RUN), fontsize=20)
# ax.set_ylabel('Displacement')
# ax.set_xlabel('Generation')
# ax.set_xticklabels([0, 0, 200/50, 400/50, 600/50, 800/50, 1000/50], fontsize=12)
ax.set_xticklabels([0, 0, 200, 400, 600, 800, 1000], fontsize=12)
ax.set_yticklabels([0, 0, 2, 4, 6, 8], fontsize=12)

sns.despine()
plt.tight_layout()
plt.savefig("plots/XENO_3_rainbow_waterfall_run_{}.png".format(RUN), bbox_inches='tight', dpi=600)


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

from adi.forumail import bool_false_symbolic_strings
from adi.forumail import bool_true_symbolic_strings
from adi.forumail import forum_portal_type
from adi.forumail import post_portal_type

from plone import api

from Acquisition import aq_inner, aq_parent

from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

class View(BrowserView):

    index = ViewPageTemplateFile("forum_main.pt")

    forum_head = ViewPageTemplateFile("forum_head.pt")

    forum_body = ViewPageTemplateFile("forum_body.pt")

    def __call__(self):
        return self.render()

    def exchangeParaVal(self, query, para, val):
        query_splits = query.split(para)
        query_start = query_splits[0]
        query_rest = query_splits[1]
        query_rest_splits = query_rest.split('&')
        query_end_splits = query_rest_splits[1:]
        if type(query_end_splits) == list:
            query_end = '&'.join(query_end_splits)
        else:
            query_end = query_end_splits
        if query_end != '': query_end = '&' + query_end
        query = query_start + para + '=' + val + query_end
        return query

    def getAddUrl(self):
        add_url = None
        forum_url = self.getForumUrl()
        if forum_url:
            add_url = forum_url + '/createObject?type_name=' + post_portal_type
        return add_url

    def getForumUrl(self):
        forum_url = None
        context = aq_inner(self.context)
        if context.Type() == post_portal_type:
            context = aq_parent(self.context)
        forum_url = context.absolute_url()
        return forum_url

    def getPosts(self):
        context = aq_inner(self.context)
        if context.Type() == post_portal_type:
            context = aq_parent(self.context)
        if self.getUrlParaVal('Subject'):
            cats = self.getUrlParaVal('Subject')
            posts = api.content.find(context=context,
                                     portal_type=post_portal_type,
                                     Subject=cats,
                                     sort_on='created',
                                     sort_order='reverse')
        else:
            posts = api.content.find(context=context,
                                 portal_type=post_portal_type,
                                 sort_on='created',
                                 sort_order='reverse')
        return posts

    def getReplyDepth(self, post_id):
        return self.getReplyIdAndDepth(post_id)[1]

    def getReplyDepthIter(self, post_id):
        reply_iters = ()
        i = self.getReplyDepth(post_id) + 1
        for j in range(i-1): # minus one to exclude orig-text-ele
            reply_iters += ('i',)
        return reply_iters

    def getReplyId(self, post_id):
        return self.getReplyIdAndDepth(post_id)[0]

    def getReplyIdAndDepth(self, post_id):
        reply_id = None
        reply_depth = 0
        nrs = ['1','2','3','4','5','6','7','8','9','0']
        i = len(post_id)
        while i > 0:
            i -= 1
            if post_id[i] in nrs:
                while post_id[i] in nrs or post_id[i] == '-':
                    if post_id[i] == '-':
                        reply_depth += 1
                    i -= 1
                reply_id = post_id[i+1:]
            else:
                break
        if reply_id:
             reply_id = '1' + reply_id
        else:
            reply_id = '1'
        return reply_id, reply_depth

    def getResults(self):
        """ Main function to return list of posts to view, depending on user's selection via URL-para."""
        posts = None
        context = aq_inner(self.context)
        threaded = self.getUrlParaVal('threaded')
        if context.Type() == post_portal_type:
            posts = self.getThread(context.getId(), threaded)
        else:
            if threaded in bool_true_symbolic_strings:
                posts = self.getThreads()
            else:
                posts = self.getPosts()
        return posts

    def getThreads(self):
        posts = self.getPosts()
        posts_ids = ()
        for post in posts:
            posts_ids += (post['id'],)

        new_ids = ()
        new_posts = ()

        threads_ids = self.getThreadsIds(posts_ids)

        for thread_id in threads_ids:
            thread_ids = self.getThreadIds(posts_ids, thread_id)
            thread_ids = sorted(thread_ids)
            for thread_id in thread_ids:
                new_ids += (thread_id,)

        for new_id in new_ids:
            for post in posts:
                if post['id'] == new_id:
                    new_posts += (post,)

        return new_posts

    def getThread(self, post_id, threaded):
        thread_posts = ()
        posts = self.getPosts()
        thread_id = self.getThreadId(post_id)
        for post in posts:
            post_id = post['id']
            if post_id == thread_id or self.isReplyOf(post_id, thread_id):
                thread_posts += (post,)
        if threaded in bool_true_symbolic_strings:
            thread_posts = sorted(thread_posts, key = lambda post: (post.id))
        return thread_posts

    def getThreadId(self, post_id):
        thread_id = None
        nrs = ['0','1','2','3','4','5','6','7','8','9']
        i = len(post_id)-1
        if post_id[-1] in nrs:
            while i > 0 and post_id[i] in nrs or post_id[i] == '-':
                i -= 1
                if post_id[i] == '-' and not post_id[i-1] in nrs:
                    break
            thread_id = post_id[:i]
        else:
            thread_id = post_id
        return thread_id

    def getThreadIds(self, posts_ids, thread_id):
        thread_ids = ()
        for post_id in posts_ids:
            if post_id == thread_id or self.isReplyOf(post_id, thread_id):
                thread_ids += (post_id,)
        return thread_ids

    def getThreadsIds(self, posts_ids):
        threads_ids = ()
        for post_id in posts_ids:
            if self.getThreadId(post_id) not in threads_ids:
                threads_ids += (self.getThreadId(post_id),)
        return threads_ids

    def getUrlParaVal(self, para):
        val = None
        forum_form = self.request.form
        if para in forum_form:
            val = forum_form[para]
        return val

    def isForum(self):
        context = aq_inner(self.context)
        if context.Type() == forum_portal_type: return True
        else: return False

    def isIniPost(self, post_id):
        reply_depth = self.getReplyDepth(post_id)
        if reply_depth == 0: return True
        else: return False

    def isReplyOf(self, post_id, thread_id):
        IS_REPLY = False
        nrs = ['1','2','3','4','5','6','7','8','9','0']
        if len(post_id) >= len(thread_id) + 1 \
        and post_id.startswith(thread_id + '-')\
        and post_id[len(thread_id) + 1] in nrs:
            IS_REPLY =  True
        return IS_REPLY

    def isThreaded(self):
        val = self.getUrlParaVal('threaded')
        if val in bool_true_symbolic_strings: return True
        else: return False

    def render(self):
        return self.index()

    def renderForumHead(self):
        return self.forum_head()

    def renderForumBody(self):
        return self.forum_body()

    def removeParaPair(self, query, para_pair):
        query_splits = query.split(para_pair)
        query_start = query_splits[0][:-1]
        query_end_splits = query_splits[1]
        if type(query_end_splits) == list:
            query_end = '&'.join(query_end_splits)
        else:
            query_end = query_end_splits
        query = query_start + query_end
        if query.startswith('&'):
            query = query[1:]
        return query

    def showInThreadedView(self, id_):
        SHOW = True
        if not self.isIniPost(id_) and self.isThreaded():
            SHOW = False
        return SHOW

    def updateUrlQuery(self, para_pair):
        new_url = None
        url = self.context.absolute_url()
        query = self.request['QUERY_STRING']
        para = para_pair.split('=')[0]
        val = para_pair.split('=')[1]

        if query != '':
            if query.find(para + '=') != -1:
                if para.endswith('%3Alist'):
                    if query.find(para_pair) != -1:
                        query = self.removeParaPair(query, para_pair)
                    else:
                        query += '&' + para_pair
                else:
                    query = self.exchangeParaVal(query, para, val)
            else:
                query += '&' + para_pair
        else:
            query = para_pair
        if query != '': query = '?' + query
        new_url = url + query
        return new_url

#EOF


import os
import re

import flask
from flask import (Blueprint, current_app, flash, g, redirect, render_template,
                   request, session, url_for)
from flask.cli import with_appcontext
from werkzeug.exceptions import abort

from app.auth import login_required
from app.db import get_db

bp = Blueprint('rating', __name__)

@bp.before_request
def csrf_protect():
    if request.method == "POST":
        token = str(session.get('_csrf_token', None))

        form_token = request.form.get('_csrf_token')

        if not token or token != form_token:
            abort(403)

# @bp.after_request
# def make_nav_active():
#     pass

def select_images():
    db = get_db()

    current_group = db.execute(
        'SELECT * FROM current_group'
    )

    curr_group = None
    for group in current_group:
        curr_group = group['current_group_name']

    images = db.execute(
        'SELECT DISTINCT *'
        ' FROM images'
        ' WHERE images.group_status = ?'
        ' ORDER BY RANDOM()'
        ' LIMIT 6',
        (curr_group, )
    )
    return images

@bp.route('/sortable1', methods=['GET'])
@login_required
def sortable1():
    return render_template('rating/sortable.html', images=select_images())

@bp.route('/sortable1', methods=['POST'])
@login_required
def sortable11():
    if not request.form['ratings']:
        flash("Error while selecting images.")
        return redirect(url_for('rating.sortable1'))

    ratings = request.form['ratings']
    int_ratings = [int(i) for i in ratings.split()]

    if not len(int_ratings) == 6:
        flash("Error while posting images.")
        return redirect(url_for('rating.sortable1'))

    db = get_db()
    message = None

    db.execute(
        'INSERT INTO rating (rated_first, rated_second, rated_third, rated_forth, rated_fifth, rated_sixth, author_id) VALUES (?, ?, ?, ?, ?, ?, ?)',
        (int_ratings[0], int_ratings[1], int_ratings[2], int_ratings[3], int_ratings[4], int_ratings[5], g.user['id'])
    )
    db.commit()

    return redirect(url_for('rating.sortable1'))

@bp.route("/sortable2", methods=['GET'])
@login_required
def sortable2():
    return render_template("rating/sortable2.html", images=select_images())

@bp.route("/sortable2", methods=['POST'])
@login_required
def sortable22():
    if not request.form['ratings']:
        flash("Error while selecting images.")
        return redirect(url_for('rating.sortable2'))

    ratings = request.form['ratings']
    int_ratings = [int(i) for i in ratings.split()]

    if not len(int_ratings) == 2:
        flash("Error while posting images.")
        return redirect(url_for('rating.sortable2'))

    db = get_db()
    message = None

    db.execute(
        'INSERT INTO selection (rated_first, rated_second, author_id) VALUES (?, ?, ?)',
        (int_ratings[0], int_ratings[1], g.user['id'])
    )
    db.commit()

    return redirect(url_for('rating.sortable2'))

@bp.route("/sortable3", methods=['GET'])
@login_required
def sortable3():
    return render_template("rating/sortable3.html", images=select_images())

@bp.route("/sortable3", methods=['POST'])
@login_required
def sortable33():
    if request.form['ratings'] and request.form['type']:
        if request.form['type'] == 'sortable':
            ratings = request.form['ratings']
            int_ratings = [int(i) for i in ratings.split()]

            db = get_db()
            message = None

            db.execute(
                'INSERT INTO rating (rated_first, rated_second, rated_third, rated_forth, rated_fifth, rated_sixth, author_id) VALUES (?, ?, ?, ?, ?, ?, ?)',
                (int_ratings[0], int_ratings[1], int_ratings[2], int_ratings[3], int_ratings[4], int_ratings[5], g.user['id'])
            )
            db.commit()
            # last_id = db.last_insert_rowid()

            return redirect(url_for('rating.sortable3'))
        elif request.form['type'] == 'select':
            ratings = request.form['ratings']
            int_ratings = [int(i) for i in ratings.split()]

            db = get_db()
            message = None

            db.execute(
                'INSERT INTO selection (rated_first, rated_second, author_id) VALUES (?, ?, ?)',
                (int_ratings[0], int_ratings[1], g.user['id'])
            )
            db.commit()

            return redirect(url_for('rating.sortable3'))
        else:
            ratings = request.form['ratings']
            int_ratings = [int(i) for i in ratings.split()]
            amount = request.form['amount']

            db = get_db()
            message = None

            db.execute(
                'INSERT INTO amount (image_rated, rating, author_id) VALUES (?, ?, ?)',
                (int_ratings[0], amount, g.user['id'])
            )
            db.commit()

            return redirect(url_for('rating.sortable3'))

    flash("Error while selecting images.")
    return redirect(url_for('rating.sortable3'))

@bp.route("/sortable4", methods=['GET'])
@login_required
def sortable4():
    return render_template("rating/sortable4.html", images=select_images())

@bp.route("/sortable4", methods=['POST'])
@login_required
def sortable44():
    if not request.form['ratings']:
        flash("Error while selecting images.")
        return redirect(url_for('rating.sortable4'))

    ratings = request.form['ratings']
    int_ratings = [int(i) for i in ratings.split()]
    amount = request.form['amount']

    db = get_db()
    message = None

    db.execute(
        'INSERT INTO amount (image_rated, rating, author_id) VALUES (?, ?, ?)',
        (int_ratings[0], amount, g.user['id'])
    )
    db.commit()

    return redirect(url_for('rating.sortable4'))