import requests, json, turtle

iss = turtle.Turtle()

def ustawienia(ekran):
    global iss
    ekran.setup(1000, 500)
    ekran.bgpic('earth.gif')
    ekran.setworldcoordinates(-180, -90, 180, 90)
    turtle.register_shape('iss.gif')
    iss.shape('iss.gif')

def rusz_iss(dlugosc, szerokosc):
    global iss
    iss.penup()
    iss.goto(szerokosc, dlugosc)
    iss.pendown()

def sledz_iss():
    url = 'http://api.open-notify.org/iss-now.json'
    odpowiedz = requests.get(url)
    if (odpowiedz.status_code == 200):
        odpowiedz_slownik = json.loads(odpowiedz.text)
        polozenie = odpowiedz_slownik['iss_position']
        print('Współrzędne ISS to ' + polozenie['latitude'] + ', ' + polozenie['longitude'])
        dlugosc = float(polozenie['latitude'])
        szerokosc = float(polozenie['longitude'])
        rusz_iss(dlugosc, szerokosc)
    else:
        print("Houston, mamy problem:", odpowiedz.status_code)
    widget = turtle.getcanvas()
    widget.after(5000, sledz_iss)

def main():
    global iss
    ekran = turtle.Screen()
    ustawienia(ekran)
    sledz_iss()

if __name__ == "__main__":
    main()
    turtle.mainloop()


from django.contrib import admin

from .models import UserProfile, CompanyProfile

class ProfileAdmin(admin.ModelAdmin):
    list_display = (
        'first_name',
        'last_name',
        'birthday_ddmm',
        'start_date',
        'end_date',
        'level',
        'role',
        'team',
        'contract_type',
        'contract_percentage',
        'agent_goal',
        'company_id'
    )

    ordering = ('last_name',)

class AdminCompany(admin.ModelAdmin):
    list_display = (
        'company_name',
        'street_address1',
        'street_address2',
        'country',
        'postcode',
        'town_or_city',
        'plan',
        'signup_date',
        'renewal_date',
        'payment',
        'setting_daystart',
        'setting_dayend'
    )

    ordering = ('company_name',)

admin.site.register(CompanyProfile, AdminCompany)
admin.site.register(UserProfile, ProfileAdmin)


# // Time Complexity :O(n)
# // Space Complexity :O(h)
# // Did this code successfully run on Leetcode :yes
# // Any problem you faced while coding this :no

# // Your code here along with comments explaining your approach

"""
# Definition for Employee.

class Employee:
    def __init__(self, id: int, importance: int, subordinates: List[int]):
        self.id = id
        self.importance = importance
        self.subordinates = subordinates
"""

class Solution:
    def __init__(self):
        self.result=0
        self.dict={}
    def getImportance(self, employees: List['Employee'], id: int) -> int:
        for i in range(len(employees)):
            temp=employees[i]
            self.dict[temp.id]=temp

        self.dfs(id)
        return self.result
    def dfs(self,id):
        #base
        #logic
        emp=self.dict[id]
        self.result+=emp.importance
        for i in emp.subordinates:
            self.dfs(i)


class Solution(object):
    def minCut(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        if n < 2:
            return 0
        self.memPalin = {}
        dp = [ 0 for _ in range(n)]
        isPa = [[ False for _  in range(n)] for _ in range(n)]
        isPa[-1][-1] = True
        for start in range(n-2, -1, -1):
            isPa[start][start] = True
            dp[start] = 1 + dp[start+1]
            for end in range(start+1, n):
                if self.isPalindrome(s, start, end, isPa):
                    if end == n-1:
                        dp[start] = 0
                        break
                    else:
                        dp[start] = min (dp[start], 1 + dp[end+1])
        return dp[0]

    def isPalindrome(self, s, start, end, isPa):
        if s[start] == s[end] and (start+1 > end -1 or isPa[start+1][end-1]):
            isPa[start][end] = True
            return True
        else:
            isPa[start][end] = False
            return False
##################
#
# forward method
#
##################
class Solution(object):
    def minCut(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        if n < 2:
            return 0
        cut = [ 0 for _ in range(n)]
        isPa = [[False for _ in range(n)] for _ in range(n)]
        isPa [0][0] = True
        for i in range(1,n):
            isPa[i][i] = True
            cut[i] = cut[i-1] + 1
            for j in range(i+1):
                if s[i] == s[j] and ( j+1> i-1 or isPa[j+1][i-1]):
                    isPa[j][i] = True
                    cut[i] = min(cut[i], cut[j-1]+1) if j != 0 else 0
        return cut[n-1]


"""
Self Check

Write two Python functions to find the minimum number in a list.

    The first function should compare each number to every other number
    on the list. 𝑂(𝑛^2).

    The second function should be linear 𝑂(𝑛).
"""
import time
from random import randrange

def findMin_1(alist):
    overallMin = alist[0]

    for i in alist:
        isSmallest = True
        for j in alist:
            if i > j:
                isSmallest = False
        if isSmallest:
            overallMin = i

    return overallMin

# print(findMin([15, 42, 21, 71, 8]))

'''
Why is this O(n^2)? Because of the nested loops.

'''

'''
for listSize in range(1000, 10001, 1000):
    alist = [randrange(100000) for x in range(listSize)]
    start = time.time()
    print(findMin_1(alist))
    end = time.time()
    print("Size: %d, Time: %f" % (listSize, end-start))
'''

# Linear Function
def findMin_2(alist):
    minsofar = alist[0]
    for i in alist:
        if i < minsofar:
            minsofar = i
    return minsofar

for listSize in range(1000, 10001, 1000):
    alist = [randrange(100000) for x in range(listSize)]
    start = time.time()
    print(findMin_2(alist))
    end = time.time()
    print("Size: %d, Time: %f" % (listSize, end-start))


from tkinter import *
from tkinter import ttk
import re
from tkinter import messagebox

Library = []

def update6():
    b6['state'] = DISABLED
    b7['state'] = NORMAL
    b2['state'] = DISABLED
    e1.insert(0,emp.getFirstname())
    e2.insert(0,emp.getLastname())
    e4.insert(0,emp.getCourse())
    e5.insert(0,emp.getSection())
    e6.insert(0,emp.getTitle())
def update7():
    b6['state'] = NORMAL
    b7['state'] = DISABLED

    MsgBox = messagebox.askquestion ('Update ','Are you sure you want to Update?',icon = 'warning')
    if MsgBox == 'yes':
        selected_item = tv.selection()[0]
        tv.item(selected_item, text=e1.get(),values=(e2.get(),self_gender.get(),e4.get(),e5.get(),e6.get()))
        e1.delete(0, END)
        e2.delete(0, END)
        e4.delete(0, END)
        e5.delete(0, END)

    else:
        print("returning...")

def add_to_list():
    global emp

    print("Name: " + " " + e1.get(), e2.get() + "  " + "Gender: " + " " + self_gender.get()+ "  " +"Course: " + " " + e4.get()+ "  " +"Section: " + " " + e5.get()+ "  " + "Title: " + " " + e6.get())
    emp = Student(e1.get(), e2.get(),self_gender.get(),e4.get(),e5.get(),e6.get())
    a = e1.get()
    b = e2.get()
    c = self_gender.get()
    d = e4.get()
    e = e5.get()
    f = e6.get()
    students.append(Student(a,b,c,d,e,f))
    students.append(emp)
    messagebox.showinfo("Dialog box", "Added")
    e1.delete(0, END)
    e2.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    e6.delete(0, END)
    b2['state'] = NORMAL

def read_list():
    tv.insert('','end', text=emp.getName(),values=(emp.getGender(),emp.getCourse(), emp.getSection(), emp.getTitle()))
    window.i = window.i + 1
    b2['state'] = DISABLED

def exit_prog():
    MsgBox = messagebox.askquestion ('Exit','Are you sure you want to Exit?',icon = 'warning')
    if MsgBox == 'yes':
       window.destroy()
    else:
        print("welcome back!")

def delete_to_list():
        MsgBox = messagebox.askquestion ('Delete','Are you sure you want to Delete?',icon = 'warning')
        if MsgBox == 'yes':
            selected_item = tv.selection()
            remove_emp = tv.detach(selected_item)
            e1.delete(0, END)
            e2.delete(0, END)
            e4.delete(0, END)
            e5.delete(0, END)
            e6.delete(0, END)
        else:
            messagebox.showinfo('Return','You will now return to the Main Screen')

class Student:
    def __init__(self, first_name, last_name, gender, course, section, title):
        self.first_name = first_name
        self.last_name = last_name
        self.gender = gender
        self.course = course
        self.section = section
        self.title = title

    def getName(self):
        return self.first_name  + " " + self.last_name
    def getGender(self):
        self_gender.get()
        return self.gender

    def getCourse(self):
        return self.course

    def getSection(self):
        return self.section

    def getFirstname(self):
        return self.first_name

    def getLastname(self):
        return self.last_name

    def getTitle(self):
        return self.title

window = Tk()
window.resizable(1, 50)
window.i = 10000000
self_gender = StringVar()
window.configure(background='Purple')
window.title("Library")
Label(window, text="Full Name: ").grid(row=0, column=0, padx=10, pady=5)
Label(window, text="Students Number: ").grid(row=1, column=0, padx=10, pady=5)
Label(window, text="Course: ").grid(row=3, column=0, padx=10, pady=5)
Label(window, text="Section: ").grid(row=4, column=0, padx=10, pady=5)

e1 = Entry(window, width=15)

e2 = Entry(window, width=15)

e4 = Entry(window, width=15)

e5 = Entry(window, width=15)

e1.grid(row=0, column=1, padx=10, pady=5)
e2.grid(row=1, column=1, padx=10, pady=5)
e4.grid(row=3, column=1, padx=10, pady=5)
e5.grid(row=4, column=1, padx=10, pady=5)
e6 = Entry(window, width=15)

b1 = Button(window, text="        Insert        ", command=add_to_list)
b1.grid(row=4, column= 1, padx=10, pady=5,columnspan=4)

b2 = Button(window, text="       View      ", command=read_list)
b2.grid(row=4, column=2, padx=12, pady=10,columnspan=4)

b4 = Button(window, text="      Delete      ", command=delete_to_list)
b4.grid(row=4, column=3, padx=10, pady=5,columnspan=4)

b6 = Button(window, text="     Update     ",state=NORMAL, command=update6)
b6.grid(row=4, column=4 ,padx=10, pady=5, columnspan=4)

b5 = Button(window, text="        Exit         ", command=exit_prog)
b5.grid(row=4, column=5 ,padx=10, pady=5,columnspan=4)

b7 = Button(window, text="     Update     ",state=DISABLED, command=update7)
b7.grid(row=4, column=6 ,padx=10, pady=5,columnspan=4)

tv = ttk.Treeview(window, height=15,columns=('gender','Course','Section','title'))

tv.grid(row=5, column=0 , columnspan=6)

tv.heading('#0', text="Full Name")
tv.heading('#1', text="Students Number")
tv.heading('#2', text="Course")
tv.heading('#3', text="Section")

window.mainloop()


# System imports
import logging as log
import matplotlib.pyplot as plt
import numpy as np
import os
import traceback
# Custom imports
from check_network_output import check_output
from network_creation import create_synapses
from network_training import Network_Layers
from network_training import train_synapses

'''
    Main class used for neural network construction, training and testing
    Neural_Network class supports:
        * selectable directory
            - load and save trained synapses
            - save data for loss history during training
            - store results for testing phase
            - heatmap of each neuron

        * selectable training and testing data => Data_Package
            - data will be collected from specialised class
            - will include original dimensions for input before reshape
            - includes neuron counts for input and output

        * configurable training setup => Training_Config class
            - selectable epochs
            - debugging error from each epoch
            - target error
            - type of activation function
            - learning rate

        * configurable archeitecture
            - can select total hidden layers
            - can determine number of hidden neurons in each hidden layer

    Classes that this class depends on includes:
        * Network_Layers
            - used when training and testing
            - includes methods to feed forward input neurons for training and testing
            - has batch gradient descent and backprop for training

        * Neuron_Count
            - updated when synapse is loaded => selects input, output and hidden
            - updated when training data is provided => select input and output
            - when the neuron count is determined, it checks data to see if
              archeitecture is compatible, including:
                - loading synapses then trying to train with incorrectly sized data
                - loading synapses then trying to test with incorrectly sized data
                - training network then testing with incorrectly sized data

        * Training_Config
            - contains information used to determine type of training
            - selectable activation functions including:
                - sigmoid
                - inverse_tan
                - relu (not working)
                - leaky_relu (not working)
            - NOTE: Can be replaced with a dictionary for higher performance, but
                    less aesthetic code syntax
'''
class Neural_Network:
    def __init__(self, directory='./default_dir'):
        self.training_config = Training_Config()
        self.neuron_count = Neuron_Count(None,None,[])
        self.synapses_built = False
        self.input_dimensions = None
        self.error_history = []
        self.set_directory(directory)

    '''
        set_training_cycles = Accepts a dictionary and configures the training
                              configuration of the network
        set_hidden_layer = Determining the hidden section of the network, by
                           specifying the number of hidden neurons for each layer
        train = Accepts training data and trains the synapses
        validate_synapse = If a synapse was preloaded, determine if it fits the
                           provided training data
    '''
    def set_training_config(self, **options):
        self.training_config.set(**options)

    def set_hidden_layer(self, *hidden_neurons):
        self.neuron_count.hidden = list(hidden_neurons)

    def train(self, training_data):
        if(self.synapses_built == False):
            self.neuron_count.input = training_data.total_input_neurons
            self.neuron_count.output = training_data.total_output_neurons
            self.synapses = create_synapses(self.neuron_count)
            self.synapses_built = True
        elif(self.validate_synapse(training_data) == False):
            log.error("Aborting training of neural network!")
            return
        self.input_dimensions = training_data.input_dimensions  # train if data is suitable
        train_synapses(training_data, self.synapses, self.training_config, self.error_history)

    def validate_synapse(self, data):
        synapse_match = True
        if(data.total_input_neurons != self.neuron_count.input):
            synapse_match = False
        if(data.total_output_neurons != self.neuron_count.output):
            synapse_match = False
        if(synapse_match == False):
            log.error("Provided synapses have mismatching dimensions!")
            log.error("For inputs, expected: {0}, got: {1}".format(self.neuron_count.input, data.total_input_neurons))
            log.error("For outputs: expected {0}, got: {1}".format(self.neuron_count.input, data.total_output_neurons))
        return synapse_match

    '''
        set_directory = Sets the directory to save and load files from
        load_synapses = Gets the synapses from a filename, and updates the
                        neuron_count class to configure the network
        save_synapses = Saves the synapses to a filename
        get_neuron_count_from_synapse = Update neuron count for a differently
                                        structured set of synapses
    '''
    def set_directory(self, directory):
        self.folders = {
            "root":     "{0}".format(directory),
            "training": "{0}/training".format(directory),
            "synapses": "{0}/synapses".format(directory),
            "testing":  "{0}/testing".format(directory),
            "hidden":   "{0}/hidden_neurons".format(directory)
        }
        for folder in self.folders: # Create folders if they dont exist
            folder_path = self.folders[folder]
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
                log.info("Generated: {0}".format(folder_path))

    def load_synapses(self, filename):
        try:
            load_path = "{0}/{1}".format(self.folders["synapses"], filename)
            self.synapses = np.load(load_path)
            log.info("Loaded synapses from: {0}".format(load_path))
            self.synapses_built = True
            self.get_neuron_count_from_synapses()
        except Exception as e:
            log.error(traceback.format_exc())
            log.error("Couldn't load synapses from: {0}".format(load_path))

    def save_synapses(self, filename):
        if(self.synapses_built == False):
            log.error("Cannot save since synapses have not been built!")
            return
        save_path = "{0}/{1}.npy".format(self.folders["synapses"], filename)
        np.save(save_path, self.synapses)
        log.info("Saved to: " + filename + ".npy")

    def get_neuron_count_from_synapses(self):
        self.neuron_count.input = self.synapses[0].shape[0]
        self.neuron_count.output = self.synapses[-1].shape[1]
        self.neuron_count.hidden = []
        for i in range(len(self.synapses)-1):
            self.neuron_count.hidden.append(self.synapses[i].shape[1])

    '''
        show_synapses = Give a standard numpy output of the synapses
        show_c_synapses = Give a c-formattd output of the synapses
    '''

    def show_synapses(self):
        if(self.synapses_built == False):
            log.error("Cannot show since synapses have not been built!")
            return
        for i in range(len(self.synapses)):
            log.info("[Synapse Layer %d]", i)
            log.info(self.synapses[i])

    def show_c_synapses(self, filename="c_synapses.txt"):
        if(self.synapses_built == False):
            log.error("Cannot show since synapses have not been built!")
            return
        save_path = "{0}/{1}".format(self.folders["synapses"], filename)
        file = open(save_path, "w")
        for index, synapse in enumerate(self.synapses):
            file.write("[Synapse Layer {0}]\n".format(index))
            file.write("double m[{0}][{1}] = ".format(synapse.shape[0], synapse.shape[1]))
            file.write("{\n")
            for row in range(synapse.shape[0]):
                file.write("\t{")
                for col in range(synapse.shape[1]):
                    file.write("{0}".format(synapse[row][col]))
                    if col is not synapse.shape[1]-1:
                        file.write(",")

                file.write("},\n")
            file.write("}\n")

    '''
        test_data = Give it test data to check the error of the network
        get_output = Give it input data and get the output from the network
    '''
    def test(self, test_data, **options):
        if(self.synapses_built == False):
            log.error("Cannot test, synapses not built")
            return
        elif(self.validate_synapse(test_data) == False):
            log.error("Aborting testing of neural network!")
            return
        elif(self.input_dimensions != test_data.input_dimensions):
            if(self.input_dimensions == None):
                log.warn("Input dimensions not specified, but input neurons match")
                log.warn("Substituting input dimensions from test data")
                self.input_dimensions = test_data.input_dimensions
            else:
                log.warn("Testing data has different dimensions to network, continuing...")
        given_output = self.get_output(test_data.input) # get test data and compare
        check_output(given_output, test_data, self.folders["testing"], **options)

    def get_output(self, test_input_data):
        if(self.synapses_built == False):
            log.error("Cannot get sample output, synapses not built")
            return
        total_layers = len(self.synapses)+1
        layers = Network_Layers(total_layers, function=self.training_config.function)
        layers.initialise_input(test_input_data)
        layers.apply_feed_forward(self.synapses)
        return layers.values[-1]    #

    '''
        get_hidden_neuron_heatmap:
            Gets a heatmap of each hidden neuron in each layer of the network,
            and shows how each pixel of the input contributes to its final weight
        graph_loss_history: Produces and saves a plot of the loss
    '''
    def get_hidden_neuron_heatmaps(self):
        if(self.synapses_built == False):
            log.error("Can't generate heatmap without building synapses!")
            return
        if(self.input_dimensions == None):
            log.error("Dimensions of input not specified. Cannot show heatmap!")
            return
        layer_weights = self.synapses[0]    # at least one for input to output
        save_path = self.folders["hidden"]
        for layer in range(len(self.synapses)):
            if(layer != 0):
                layer_weights = np.dot(layer_weights, self.synapses[layer])
            # Get weights as a dot product chain of previous layers
            for neuron in range(layer_weights.shape[1]):
                hidden_weights = layer_weights[:,neuron]
                hidden_matrix = hidden_weights.reshape(*self.input_dimensions)
                plt.title("Layer {0}, Neuron {1}".format(layer, neuron))
                plt.imshow(hidden_matrix, cmap='Spectral', interpolation='nearest')
                plt.colorbar()
                plt.savefig("{0}/{1}_{2}.png".format(save_path, layer, neuron))
                plt.close()

    def graph_loss_history(self, filename='loss'):
        if(len(self.error_history) == 0):
            log.warn("Loss history has not been created yet!")
            return
        plt.plot(np.array(self.error_history).T)
        plt.ylabel("Training loss")
        plt.xlabel("Epoch")
        plt.savefig("{0}/{1}.png".format(self.folders["training"], filename))
        plt.close()

    '''
        describe = Show the number of neurons in each layer
        help = Explain the functions available in the class
    '''
    def describe(self):
        log.info("[Description of neural network]")
        log.info("input_neurons  = {0}".format(self.neuron_count.input))
        log.info("output_neurons = {0}".format(self.neuron_count.output))
        log.info("hidden_neurons = {0}".format(self.neuron_count.hidden))

class Neuron_Count:
    def __init__(self, input_count, output_count, hidden_count):
        self.input = input_count
        self.output = output_count
        self.hidden = hidden_count

class Data_Package:
    def __init__(self, module, shuffle=False):
        if(shuffle == True):
            rng_state = np.random.get_state()
            np.random.shuffle(module.input_data)
            np.random.set_state(rng_state)
            np.random.shuffle(module.output_data)
        self.input = module.input_data
        self.output = module.output_data
        self.input_dimensions = module.dimensions
        self.total_sets = self.input.shape[0]
        self.total_input_neurons = self.input.shape[1]
        self.total_output_neurons = self.output.shape[1]

class Training_Config:
    def __init__(self, **options):
        # Default training config
        self.epochs = 1000
        self.debug = True
        self.target_error = 0   # Default target is no error
        self.learning_rate = 1
        self.function = 'sigmoid'
        self.valid_options = ['epochs', 'debug', 'target_error',
                              'learning_rate', 'function']
        self.set(**options)

    def set(self, **options):
        for key in options:
            if(key in self.valid_options):
                setattr(self, key, options[key])
            else:
                log.warn("class Training_Config has no attribute: " + key)

    def describe(self):
        log.info("[Training configuration]")
        for key in self.valid_options:
            log.info("{0} = {1}".format(key, getattr(self, key)))


'''
Created on Nov 28, 2018

@author: penumr
'''

def search(arr, x):

    for i in range(len(arr)):
        if arr[i] == x:
            return i;
    return -1;

a = [2, 3, 4, 56, 7, 8, 9, 34, 66, 99]
s = 7;
print(search(a, s), a[search(a, s)])


#!/usr/bin/env python2

import argparse, os, json, glob, math, sys
import requests
import shutil

parser = argparse.ArgumentParser(description='Process a neural style job directory')
parser.add_argument('directory', type=str,
                    help='Directory containing the job information, content and styles')
parser.add_argument('--shutdown', action='store_true',
                    default=False,
                    help='Shutdown on completion')
parser.add_argument('--test', action='store_true',
                    default=False,
                    help='Do not execute commands')
parser.add_argument('--generate-job', action='store_true',
                    default=False,
                    help='Reads images from directory and prints a job.json definition')
parser.add_argument('--size', type=int,
                    default=None,
                    help='Override target size of output images in job')
parser.add_argument('--overwrite', action='store_true',
                    default=False,
                    help='Overwrite existing outputs')
parser.add_argument('--upload-to',
                    default=None,
                    help='URL of the http end-point where the results should be uploaded to')

args = parser.parse_args()

def run(cmd, force=False):
    print(cmd)

    if args.test and not force:
        return 0

    return os.system(cmd)

try:
    directory = args.directory

    if directory.endswith(".tar.gz"):
        job_name = os.path.basename(directory).replace('.tar.gz', '')
        print("Job: " + job_name)
        print("Downloading: " + directory)
        run("rm *.tar.gz")

        r = requests.get(directory, stream=True)
        if r.status_code == 200:
            with open(os.path.basename(directory), 'wb') as f:
                for chunk in r:
                    f.write(chunk)

        run("tar -xvf {}.tar.gz".format(job_name))
        run("rm *.tar.gz")
        directory = job_name

    if not os.path.exists(directory):
        raise RuntimeError("Directory does not exist")

    if args.generate_job:
        # Read all images
        files = map(os.path.basename, glob.glob("{}/*.png".format(args.directory)))
        output_json = {"artworks": []}

        for f in files:
            style, content, opts = f.split("+")
            opts = filter(lambda opt: not (opt["option"] in ["save_iter", "image_size"]),
                    map(lambda opt: {"option": "_".join(opt.split("_")[:-1]),
                                     "value": opt.split("_")[-1] },
                                opts.replace(".png", "").split("-"))
                        )

            args = {}
            for opt in opts:
                args[opt["option"]] = opt["value"]

            output_json["artworks"].append({
                    "contents": [content + ".jpg"],
                    "styles": [style + ".jpg"],
                    "args": args
                })
        print(json.dumps(output_json))
        sys.exit(0)

    job_file = os.path.join(directory, "job.json")
    if not os.path.exists(job_file):
        raise RuntimeError("job.json not in directory")

    with open(job_file, 'r') as f:
        job = json.loads(f.read())

    print("Read configuration: ")
    print(json.dumps(job, sort_keys=True, indent=4, separators=(',', ': ')))

    out_dir = os.path.join(directory, "output")
    if os.path.exists(out_dir):
        if not args.overwrite:
            raise RuntimeError("ERROR: Output directory exists (and no --overwrite): {}".format(out_dir))
        else:
            print("Output directory exists: {}".format(out_dir))
    else:
        os.makedirs(out_dir)
        print("Created directory: {}".format(out_dir))

    for artwork in job['artworks']:
        contents, styles = artwork['contents'], artwork['styles']
        art_args = artwork['args'] if 'args' in artwork else {}

        if isinstance(contents, str) or isinstance(contents, unicode):
            if contents == '*':
                contents = [os.path.splitext(os.path.basename(f))[0] for f in glob.glob(os.path.join(directory, "contents", "*.jpg"))]
                print("Found wildcard contents: " + str(contents))
            else:
                raise RuntimeError("Invalid contents value: " + contents)

        if isinstance(styles, str) or isinstance(styles, unicode):
            if styles == '*':
                styles = [os.path.splitext(os.path.basename(f))[0] for f in glob.glob(os.path.join(directory, "styles", "*.jpg"))]
                print("Found wildcard styles: " + str(styles))
            else:
                raise RuntimeError("Invalid contents value: " + styles)

        if args.size:
            print("Override image_size to {}".format(args.size))
            art_args['image_size'] = args.size
        else:
            art_args['image_size'] = 512

        for content in contents:
            for style in styles:
                print("Transferring {} style to {} content".format(style, content))
                out_filename = "{}+{}+{}.png".format(style, content, '-'.join(['{}_{}'.format(k, art_args[k]) for k in art_args]))

                out_path = os.path.abspath(os.path.join(out_dir, out_filename))
                style_path = os.path.abspath(os.path.join(directory, "styles", style + ".jpg"))
                content_path = os.path.abspath(os.path.join(directory, "contents", content + ".jpg"))

                extra_args = {}
                extra_args['num_iterations'] = 1000
                extra_args['backend'] = 'cudnn'
                extra_args['optimizer'] = 'adam'
                extra_args['tv_weight'] = 0

                current_resolution = 512

                while art_args['image_size'] > current_resolution:
                    print("Generating intermediate {}px image".format(current_resolution))

                    intermediate_out_filename = "{}+{}+{}+inter-{}.png".format(style, content, '-'.join(['{}_{}'.format(k, art_args[k]) for k in art_args]), current_resolution)
                    intermediate_out_path = os.path.abspath(os.path.join(out_dir, intermediate_out_filename))

                    if not os.path.exists(intermediate_out_path):
                        if run("qlua neural_style.lua -save_iter 0 -style_image {} -content_image {} {} {} -output_image {} -image_size {}".format(
                            style_path, content_path, ' '.join(['-{} {}'.format(k, extra_args[k]) for k in extra_args]), ' '.join(['-{} {}'.format(k, art_args[k]) for k in art_args]), intermediate_out_path, current_resolution
                        )) != 0:
                            raise RuntimeError("ERROR: lua error")
                    else:
                        print("Intermediate image {}px already exists".format(current_resolution))

                    current_resolution *= 2
                    extra_args['num_iterations'] = math.floor(extra_args['num_iterations'] / 2)

                    extra_args['init'] = 'image'
                    extra_args['init_image'] = intermediate_out_path

                if run("qlua neural_style.lua -style_image {} -content_image {} {} {} -output_image {}".format(
                        style_path, content_path, ' '.join(['-{} {}'.format(k, extra_args[k]) for k in extra_args]), ' '.join(['-{} {}'.format(k, art_args[k]) for k in art_args]), out_path
                    )) != 0:
                    raise RuntimeError("ERROR: lua error")

    if args.upload_to:
        job_name = os.path.basename(directory)
        print("Archiving...")
        archive_filename = "{}-output.tar.gz".format(job_name)
        run("tar -cvzf {} {}".format(archive_filename, out_dir), True)

        print("Uploading to {}".format(args.upload_to))

        files = [('files', (os.path.basename(archive_filename), open(archive_filename, 'rb'), 'application/tar+gzip'))]
        res = requests.post(args.upload_to, files=files)
        print(res.content)
        if res.status_code == 200 and "success" in str(res.content):
            # Cleanup
            run("rm {}".format(archive_filename))
            run("rm -r {}".format(directory))
        else:
            print("ERROR: Could not upload file to server!")

    if args.shutdown:
        run("sudo shutdown -h now")

except RuntimeError as e:
    print("ERROR: " + str(e))
    if args.shutdown:
        run("sudo shutdown -h now")
    exit(1)


from flask import Flask, render_template, request
import json
from pymongo import MongoClient

from main import Psql

app = Flask(__name__)

psql = Psql('mysecretpassword',
            dbname='postgres',
            user='postgres',
            host='localhost')

client = MongoClient("mongodb://localhost:27017/")

@app.route("/", methods=['post', 'get'])
def mongo_display():
    data = {}
    for collection in ("canvasses", "notes", "placements", "users"):
        d = client["db"][collection].find({})
        data[collection] = list(d)

    return render_template("main.html", data=data)

@app.route("/p", methods=['post', 'get'])
def postgre_display():
    if request.method == 'POST':
        command = request.form.get('command')
        message = psql.execute_fetch(command)
        return render_template("response.html", command=command, message=message)

    table_list = psql.execute_fetch(
        "SELECT table_name FROM information_schema.tables WHERE (table_schema = 'public') ORDER BY table_name;")
    data = {}
    for table in table_list:
        table_data = psql.get_table_contents(table[0])
        data.update({table[0]: table_data})

    return render_template("main.html", data=data)

@app.route("/c", methods=['post', 'get'])
def convert():
    data = {}
    for collection in ("canvasses", "notes", "placements", "users"):
        d = client["db"][collection].find({})
        data[collection] = [rm_id(i) for i in list(d)]

    tables = ("users", "canvasses", "notes", "placements",)
    for collection in tables:
        table = data[collection]
        for row in table:
            k = [str(i) for i in row.keys()]
            v = [stringy(i) for i in row.values()]
            fields = "(" + ",".join(k) + ")"
            values = "(" + ",".join(v) + ")"
            sql_quer = f"INSERT INTO {str(collection)} {fields} VALUES {values};"
            psql.cursor.execute(sql_quer)

    return render_template("main.html", data=data)

def stringy(i):
    if isinstance(i, int):
        return str(i)
    else:
        return "'" + str(i) + "'"

def rm_id(dictoin):
    res = {}
    for key in dictoin:
        if key != "_id":
            res[key] = dictoin[key]
        else:
            continue
    return res

def init():
    psql.execute_file("./sql_files/CREATE_TABLE.sql")
    # psql.execute_file("./sql_files/load_sample_data.sql")
    print("postgre database initialised")

    # for table in ("canvasses", "notes", "placements", "users"):
    #     with open(f"./json/{table}.json", "r") as file:
    #         file_data = json.load(file)
    #
    #     collection = client["db"][table]
    #     if isinstance(file_data, list):
    #         collection.insert_many(file_data)
    #     else:
    #         collection.insert_one(file_data)

    print("mongo database initialised")

init()

app.run()


#!/usr/bin/env python3

from General_comportement.inventory import look_inventory
from General_comportement.inventory import get_food
from General_comportement.mov_to_tile import mov_to_tile
from General_comportement.ressources import IsThereItem
from General_comportement.broadcast import EmptyCacheIgnoreBroadcast

#
# @brief: Function that is called when the food level are not critical but not
# enough to level_up therefore just like our survive function this function
# will get food however there is a twist whenever there is no food on the look
# then it takes a useful rock if no rock then moves forward
#
# @param: level [in], int, the current level of the player
# @param: food [out], int, the current food of the player
# @param: socket [in], socket class variable to interact with socket class
#
# @return: None
#
def search_food_incant_mode(lvl, food, socket):
    while food < lvl + 8:
        location, item, level = IsThereItem(socket, ["food"], lvl)
        if level != lvl:
            return level
        if (location >= 0):
            mov_to_tile(location, lvl, socket)
            socket.Take(item)
            resp, level = EmptyCacheIgnoreBroadcast(socket, lvl)
            if level != lvl:
                return level
            inventory, level = look_inventory(socket, lvl)
            if level != lvl:
                return level
            food = get_food(inventory)
            print ("search food incant: food i got: " + str(food))
        else:
            for i in range(lvl):
                socket.Forward()
                resp, level = EmptyCacheIgnoreBroadcast(socket, lvl)
                if level != lvl:
                    return level


#!/usr/bin/env python
from time import time
from sys import maxsize as INFINITY

def read_file(file_path):
    lines = []
    with open(file_path, 'r') as fp:
        lines = fp.read().splitlines()

    numbers = []
    for line in lines:
        numbers.append(int(line))

    return numbers

def bruteforce(numbers):
    N = len(numbers)
    maximum_begin = -1
    maximum_end = -1
    maximum = 0
    for i in range(N):
        for j in range(i, N):
            if numbers[j] - numbers[i] > maximum:
                maximum = numbers[j] - numbers[i]
                maximum_begin = i
                maximum_end = j

    return (maximum_begin, maximum_end, maximum)

def get_number_differences(numbers):
    numbers_diff = [0]
    for i in range(1, len(numbers)):
        numbers_diff.append(numbers[i] - numbers[i-1])
    return numbers_diff

def find_max_crossing_subarray(numbers):
    max_sum = -INFINITY
    curr_sum = 0
    idx = 0
    for i, x in enumerate(numbers):
        curr_sum = curr_sum + x
        if curr_sum > max_sum:
            max_sum = curr_sum
            idx = i
    return 0, idx, max_sum

def max_crossing_subarray(numbers, mid):
    _, start_mid_low, max_mid_low = find_max_crossing_subarray(numbers[mid::-1])
    _, end_mid_high, max_mid_high = find_max_crossing_subarray(numbers[mid+1:])
    return start_mid_low, end_mid_high, max_mid_low + max_mid_high

def max_subarray_rec(diff, low, high):
    if low == high - 1:
        return low, low, diff[0]
    mid = int((low + high)/2)
    start_mid, end_mid, max_mid = max_crossing_subarray(diff, mid-low)
    start_low, end_low, max_low = max_subarray_rec(diff[:mid-low], low, mid)
    start_high, end_high, max_high = max_subarray_rec(diff[mid-low:], mid, high)
    if max_mid >= max_low and max_mid >= max_high:
        return mid - 1 - start_mid, mid + end_mid + 1, max_mid
    elif max_low >= max_high and max_low >= max_mid:
        return start_low, end_low, max_low
    elif max_high >= max_low and max_high >= max_mid:
        return start_high, end_high, max_high

def max_subarray_d_a_c(numbers):
    numbers_diff = get_number_differences(numbers)
    return max_subarray_rec(numbers_diff, 0, len(numbers_diff))

def max_sub(numbers):
	numbers_diff = get_number_differences(numbers)
	max_sum = 0
	sum_so_far = 0
	start_index_sum_so_far = 0
	start = 0
	end = 0
	for i, p in enumerate(numbers_diff):
		sum_so_far = max(p, sum_so_far + p)
		if sum_so_far == p:
			start_index_sum_so_far = i
		max_sum = max(max_sum, sum_so_far)
		if max_sum == sum_so_far:
			start = start_index_sum_so_far
			end = i
	return start, end, max_sum

def benchmark(fn, numbers):
    start_time = time()
    start, end, maximum = fn(numbers)
    duration = time() - start_time
    print(start)
    print(end)
    print(maximum)
    return duration

if __name__ == "__main__":
    numbers = read_file("input")
    from random import randint
    #brute = benchmark(bruteforce, numbers)
    divide = benchmark(max_subarray_d_a_c, numbers)
    lin = benchmark(max_sub, numbers)

    #print("brute: {0}".format(brute))
    print("divide: {0}".format(divide))
    print("lin: {0}".format(lin))


#quadratic4.py

import math

def main():

    print("This program finds the real solutions of a quadratic")

    print()

    a,b,c = eval(input(" Please enter the coefficients ( a, b, c) : "))

    discrim = b*b - 4*a*c

    if discrim < 0:
       print(" The equation has no real roots!" )

    elif discrim == 0:

       root = -b/(2*a)
       print(" There is a double root at", root)

    else:
       discRoot = math.sqrt( b*b -4*a*c)
       root1 = (-b + discRoot)/ (2*a)

       root2 = (-b - discRoot)/ (2*a)

       print("The solutions are: ", root1,root2)

main()


# coding: utf-8

# In[255]:

from __future__ import division
import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns ; sns.set()

# put the name of all the data files in a list
from os import listdir
from os.path import isfile, join

filepath = '..\Data Files\\'
file_list = [f for f in listdir(filepath) if isfile(join(filepath, f))]

file_list.remove('SampleSubmission.csv')
for file in file_list:
    print(file)

# read in all files as a dataframe and make a dictionary with them
df_dict = {}
for file in file_list:
    df_dict[file.replace('.csv', '')] = pd.read_csv(filepath + file)

# add Team_Name map Team_Id  to the dataframe
team_dict = dict(zip(df_dict['Teams']['Team_Id'].values, df_dict['Teams']['Team_Name'].values))
for key in df_dict.keys():
    if key == 'Teams' or key == 'covers_ncaab_data_mt': continue
    headers = list(df_dict[key].columns.values)
    for h in headers:
        if 'team' in h:
            df_dict[key][h + '_name'] = df_dict[key][h].map(team_dict)

# In[259]:

# print the top ten lines of the dataframes
for key in df_dict.keys():
    print('\n' + '='*50 + '\n' + key + ' data:\n' + '='*50)
    print(df_dict[key].head(10))

# In[260]:

# Print the summary statistics of the dataframes
for key in df_dict.keys():
    print('\n' + '='*50 + '\n' + key + ' data:\n' + '='*50)
    print(df_dict[key].describe())

# In[261]:

# calculate the percantages of winning games and where they were won
def calculate_HTA(key):
    L = df_dict[key]['Wloc']
    count = float(len(L))
    N_count = sum(x == 'N' for x in L)
    H_count = sum(x == 'H' for x in L)
    A_count = sum(x == 'A' for x in L)
    print(('percent of neutral games: {:.2f}%\n'
           'percent of home team wins: {:.2f}%\n'
           'percent of visiting team wins: {:.2f}%\n'
          ).format(N_count/count*100, H_count/count*100, A_count/count*100))

# print the results
for key in df_dict.keys():
    if 'Wloc' in list(df_dict[key].columns.values):
        print('\n' + '='*50 + '\n' + key + ' Home Team Advantage:\n' + '='*50)
        calculate_HTA(key)

# Only regular season games are played on non-neutral ground.
#
# The results suggest that there is a home team advantage (HTA).
#
# Q: Do some teams play better at home than others?

# In[275]:

# create dataframe
RS_compact_results_df = df_dict['RegularSeasonCompactResults']
Teams_df = df_dict['Teams'].copy()

# add columns to a Teams_df
n = len(Teams_df['Team_Id'])
for name in ['H_win_count', 'H_loss_count', 'A_win_count', 'A_loss_count', 'N_win_count', 'N_loss_count']:
    Teams_df[name] = pd.Series(np.zeros(n), index = Teams_df.index)

# count the number of wins and losses and where they occur
for index, row in RS_compact_results_df.iterrows():
    W_ind = np.where(Teams_df['Team_Id']==row['Wteam'])[0][0]
    L_ind = np.where(Teams_df['Team_Id']==row['Lteam'])[0][0]

    if row['Wloc']=='H':
        Teams_df.ix[W_ind,'H_win_count'] += 1
        Teams_df.ix[L_ind,'H_loss_count'] += 1
    elif row['Wloc']=='A':
        Teams_df.ix[W_ind,'A_win_count'] += 1
        Teams_df.ix[L_ind,'A_loss_count'] += 1
    else:
        Teams_df.ix[W_ind,'N_win_count'] += 1
        Teams_df.ix[L_ind,'N_loss_count'] += 1

# In[293]:

def calculate_HTA(win_count, loss_count):
    total = win_count + loss_count
    return np.log(win_count/loss_count)*np.log(total)

total_win_count = 0
total_loss_count = 0
for Wloc in ['H', 'A', 'N']:
    Teams_df[Wloc + '_win_frac'] = calculate_HTA(Teams_df[Wloc + '_win_count'], Teams_df[Wloc + '_loss_count'])
    total_win_count += Teams_df[Wloc + '_win_count']
    total_loss_count += Teams_df[Wloc + '_loss_count']

Teams_df['HTA'] = calculate_HTA(total_win_count,total_loss_count)

fig = plt.figure(figsize=(12, 7))
ax = fig.add_subplot(111, projection='3d')
ax.set_title('Home Team Advantage')
ax.scatter(total_loss_count, total_win_count, Teams_df['HTA'], c='#0080ff')
ax.set_xlabel('Total Number of Losses')
ax.set_ylabel('Total Number of Wins')
ax.set_zlabel('HTA metric')
ax.view_init(elev=10, azim=0)

# In[292]:

Teams_df

# In[ ]:


import codecs;
import random;
import re;
import glob;
import os;

import math
import re
import sys

import json

# add hyper paramter if unbalanced
hyper_parameter = 200

# random seed
# and inner loop random seed change
rand_seed_parameter_initialization = 0.1
rand_seed_offset = 0.01

fileDomainRelatedIntent = ['file_search', 'file_open', 'file_share', 'file_download', 'file_other', 'file_navigate', "teamspace_search"]

teamsDomainToFileDomain = {
    # carina need small but bellevue needs big
    "teams" : "files",
    "TEAMS" : "FILES"
}

defaultFileTypeifMissed =[
        'document',
	'documents',
	'file',
	'files',
	'powerpoints',
	'power point',
	'slide',
	'slides',
	'doc',
	'docx',
	'docs',
	'spec',
	'excel',
	'excels',
	'xls',
	'xlsx',
	'spreadsheet',
	'spreadsheets',
	'workbook',
	'worksheet',
	'csv',
	'tsv',
	'note',
	'notes',
	'onenote',
	'onenotes',
	'onenote',
	'notebook',
	'notebooks',
	'pdf',
	'pdfs',
	'pdf',
	'jpg',
	'jpeg',
	'gif',
	'png',
	'image',
	'msg',
	'ics',
	'vcs',
	'vsdx',
	'vssx',
	'vstx',
	'vsdm',
	'vssm',
	'vstm',
	'vsd',
	'vdw',
	'vss',
	'vst',
	'mpp',
	'mpt',
	'word',
	'words'
#    'file',
#    'files',
#    'document',
#    'documents'
]

# for o(1) loook up
defaultFileTypeifMissedSet = set(defaultFileType.lower() for defaultFileType in defaultFileTypeifMissed)

outputs = [];
outputsSet = set([]);

outputsWithSource = [];

outputFileKeyWordAndFileNameUserContext = set([]);

fileKeyWordAndFileNameCandidateSet = set()

OutputSlotEvaluation = [];

OutputIntentEvaluation = [];

OutputSTCAIntentEvaluation = [];

# leave it but actually it is not being used
dsatTraining = "dsat_training.tsv"

outputFile = 'contexual_filekeyword_filetype_training.tsv'

outputFileUnique = 'contexual_filekeyword_filetype_unique_training.tsv'
# replace directly
#outputTrainingFolderFile = '..\\files_slot_training.tsv'
# for STCA test
#outputSTCATrainingFolderFile = '..\\sharemodeltest\\files_slot_training.tsv'

outputFileWithSource = "contexual_filekeyword_filetype_training_with_source.tsv"

outputFileKeyWordAndFileNameLexiconFilfe = 'contexual_filekeyword_filename_lexicon.txt'

outputFileKeyWordAndFileNameUserContextFiles = 'user_filekeyword_filename.txt'

# validaitng opened dataset
# old format work
inputFile = "files_file_keyword_positive.tsv"

#initial rand seed
rand_seed_parameter = rand_seed_parameter_initialization

ConversationIdPrefix = 'contexual_filekeyword_fileType'
ConversationId = 0;

#with codecs.open('Teams-golden.tsv', 'r', 'utf-8') as fin:
with codecs.open(inputFile, 'r', 'utf-8') as fin:

    for line in fin:
        line = line.strip();
        if not line:
            continue;
        array = line.split('\t');

        # make sure it at least has
        # Query	ExternalFeature	Weight	Intent	Domain	Slot
        if len(array) < 6:
            continue;

        if array[1] != 'FILES':
            continue

        # update seed for each query
        rand_seed_parameter=rand_seed_parameter+rand_seed_offset;
        random.seed(rand_seed_parameter);

        #add new here
        slot = array[3]
        # for extra target slot you want
        xmlpairs = re.findall("(<.*?>.*?<\/.*?>)", slot)

        # heuristic solution
        # for one query
        # only extra a pair of file_keyword/ file_name and file_type

        # default using null
        fileKeyWordAndFileNameXml = None
        fileTypeXml = None
        fileKeyWordAndFileName = None
        fileType = None

        for xmlpair in xmlpairs:

            # extra type and value for xml tag
            xmlTypeEndInd = xmlpair.find(">")

            xmlType = xmlpair[1:xmlTypeEndInd]

            xmlValue = xmlpair.replace("<"+xmlType+">", "")
            xmlValue = xmlValue.replace("</"+xmlType+">", "")
            xmlValue = xmlValue.strip()

            if xmlType.lower() == "file_keyword" or xmlType.lower() == "file_name":
                fileKeyWordAndFileNameCandidateSet.add(xmlValue)
                if fileKeyWordAndFileNameXml is None:
                    fileKeyWordAndFileNameXml = xmlpair
                    fileKeyWordAndFileName = xmlValue

            # file type might be noise in originla dataset but it is fine
            if xmlType.lower() == "file_type":
                if fileTypeXml is None:
                    fileTypeXml = xmlpair
                    fileType = xmlValue

        # document if filekeyword or filename exist
        if fileKeyWordAndFileNameXml is not None:

            # generate filetype if missed
            # for those cases they are do not have xml_tag
            if fileTypeXml is None:
                indexInRange = random.randint(0, len(defaultFileTypeifMissed)-1)

                if indexInRange >=0 and indexInRange <=3:
                    fileTypeXml =  defaultFileTypeifMissed[indexInRange]
                else:
                    fileTypeXml =  "<file_type>" +  defaultFileTypeifMissed[indexInRange] + "</file_type>"
                fileType = defaultFileTypeifMissed[indexInRange]
            else:
                # if invalid file, modified
                if fileType not in defaultFileTypeifMissedSet:
                    indexInRange = random.randint(0, len(defaultFileTypeifMissed)-1)

                    if indexInRange >=0 and indexInRange <=3:
                        fileTypeXml =  defaultFileTypeifMissed[indexInRange]
                    else:
                        fileTypeXml =  "<file_type>" +  defaultFileTypeifMissed[indexInRange] + "</file_type>"
                    fileType = defaultFileTypeifMissed[indexInRange]

        # for dsatTraining
        # replace file name with the last column
        if inputFile == dsatTraining:
            newline ='\t'.join(array[0:len(array)-1])
            newfile = array[len(array)-1]
            outputs.append(newline);
            outputsWithSource.append(newline+'\t'+ newfile);
        else:

            if fileKeyWordAndFileNameXml is not None and fileTypeXml is not None:

                '''
                #ConversationId,	MessageId, 'MessageText', 'JudgedIntent', 'JudgedDomain', 'JudgedConstraints', 'ConversationContext', 'Cat'

                #outputs.append("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);
                outputs.append(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);

                #unique for dedup
                #outputsSet.add("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);
                outputsSet.add(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);

                #'id', 'query', 'intent', 'domain', 'QueryXml','source'
                #outputsWithSource.append("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]+'\t'+inputFile);
                outputsWithSource.append(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]+'\t'+inputFile);
                '''

                #ConversationId,	MessageId, MessageTimestamp, MessageFrom, 'MessageText', 'JudgedIntent', 'JudgedDomain', 'JudgedConstraints', 'ConversationContext', 'MetaData', 'Frequency'  'Cat'

                #outputs.append("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);
                outputs.append(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+'\t'+'user'+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+'[]'+'\t'+'1'+'\t'+array[5]);

                #unique for dedup
                #outputsSet.add("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]);
                outputsSet.add(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+'\t'+'user'+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+'[]'+'\t'+'1'+'\t'+array[5]);

                #'id', 'query', 'intent', 'domain', 'QueryXml','source'
                #outputsWithSource.append("0"+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+array[5]+'\t'+inputFile);
                outputsWithSource.append(ConversationIdPrefix +"_"+str(ConversationId)+'\t' +"0"+'\t'+'\t'+'user'+'\t'+ fileKeyWordAndFileName + " " + fileType +'\t' + "file_search" + '\t' + array[1] + '\t' + fileKeyWordAndFileNameXml + " " + fileTypeXml+ '\t'+array[4]+'\t'+'[]'+'\t'+'1'+'\t'+array[5]+'\t'+inputFile);

                ConversationId= ConversationId+1;

                # extra from json

                # to prevent from inside content apostrophy

                fileKeyWordAndFileNameXmlPostProcessing = array[4]

                #{'
                #':
                #['
                #',
                #, '
                #']
                # typical example
                #{'UserFileNames': ['Running Experiments', 'OneNote_DeletedPages', 'Running Experiments

                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace("{'","{\"")
                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace("':","\":")
                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace("['","[\"")
                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace("',","\",")
                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace(", '",", \"")
                fileKeyWordAndFileNameXmlPostProcessing = fileKeyWordAndFileNameXmlPostProcessing.replace("']","\"]")

                # for deubg
                #print(fileKeyWordAndFileNameXmlPostProcessing)

                fileKeyWordAndFileNameXmlJson = json.loads(fileKeyWordAndFileNameXmlPostProcessing)

                #print(fileKeyWordAndFileNameXmlJson['UserFileNames'])

                #print(type(fileKeyWordAndFileNameXmlJson['UserFileNames']))

                for element in fileKeyWordAndFileNameXmlJson['UserFileNames']:
                    outputFileKeyWordAndFileNameUserContext.add(element)

            #outputs.append(line);
            #outputsWithSource.append(line+'\t'+ file);

        '''
        # id / message / intent / domain / constraint / ConversationContext / cat
        # for training purpose's format

        OutputSlotEvaluation.append("0"+"\t"+linestrs[0]+"\t"+linestrs[2]+"\t" +linestrs[1].lower()+"\t"+linestrs[3]+"\t"+linestrs[4]+"\t"+linestrs[5]);

        # TurnNumber / PreviousTurnIntent / query /intent /ConversationContext / cat
        # for training purpose's format
        OutputIntentEvaluation.append("0"+"\t"+""+"\t"+linestrs[0]+"\t" +linestrs[2]+"\t"+linestrs[4]+"\t"+linestrs[5]);

        #UUID\tQuery\tIntent\tDomain\tSlot\r\n
        #OutputSTCAIntentEvaluation.append("0"+"\t"+linestrs[4]+"\t" +linestrs[6]+"\t"+linestrs[5].lower()+"\t"+linestrs[7])

        #id\tquery\tintent\tdomain\tQueryXml\ConversationContext\tcat \r\n"
        OutputSTCAIntentEvaluation.append("0"+"\t"+linestrs[0]+"\t"+linestrs[2]+"\t" +linestrs[1].lower()+"\t"+linestrs[3]+"\t"+linestrs[4]+"\t"+linestrs[5])
        '''

"""
# comment shuffle in the first place
#random.shuffle(OutputSet);
"""

# output soted order for easy check
#outputs = ['\t'.join(['id', 'query', 'intent', 'domain', 'QueryXml'])] + sorted(outputs);
#outputsWithSource = ['\t'.join(['id', 'query', 'intent', 'domain', 'QueryXml', 'source'])] + sorted(outputsWithSource);

#MessageText	JudgedDomain	JudgedIntent	JudgedConstraints	ConversationContext	Cat
outputs = ['\t'.join(['ConversationId','MessageId','MessageTimestamp', 'MessageFrom', 'MessageText', 'JudgedIntent', 'JudgedDomain', 'JudgedConstraints', 'ConversationContext', 'MetaData','Frequency','Cat'])] + sorted(outputs);
outputsWithSource = ['\t'.join(['ConversationId', 'MessageId','MessageTimestamp', 'MessageFrom', 'MessageText','JudgedIntent', 'JudgedDomain', 'JudgedConstraints', 'ConversationContext', 'MetaData', 'Frequency','Cat','source'])] + sorted(outputsWithSource);

with codecs.open(outputFile, 'w', 'utf-8') as fout:
    for item in outputs:
        fout.write(item + '\r\n');

print("dedup size = ")
print(len(outputsSet))
with codecs.open(outputFileUnique, 'w', 'utf-8') as fout:
    fout.write('\t'.join(['ConversationId','MessageId','MessageTimestamp','MessageFrom', 'MessageText', 'JudgedIntent', 'JudgedDomain', 'JudgedConstraints', 'ConversationContext', 'MetaData','Frequency','Cat']) + '\r\n');
    for item in sorted(outputsSet):
        fout.write(item + '\r\n');

print(len(outputsSet))
with codecs.open(outputFileWithSource, 'w', 'utf-8') as fout:
    for item in outputsWithSource:
        fout.write(item + '\r\n');

with codecs.open(outputFileKeyWordAndFileNameLexiconFilfe, 'w', 'utf-8') as fout:
    # sort it to easy check
    for item in sorted(fileKeyWordAndFileNameCandidateSet):
        fout.write(item + '\r\n');

with codecs.open(outputFileKeyWordAndFileNameUserContextFiles, 'w', 'utf-8') as fout:
    # sort it to easy check
    for item in sorted(outputFileKeyWordAndFileNameUserContext):
        fout.write(item + '\r\n');

# for CMF slot evaluation format
#with codecs.open((inputFile.split("."))[0] +'slot_evaluation.tsv', 'w', 'utf-8') as fout:
#
#    # if output for traing
#    fout.write("id\tquery\tintent\tdomain\tQueryXml\tConversationContex\tcat\r\n")
#    for item in OutputSlotEvaluation:
#        fout.write(item + '\r\n');
#

'''
# for STCA evaluation
with codecs.open("sharemodeltest\\"+(inputFile.split("."))[0] +'slot_evaluation.tsv', 'w', 'utf-8') as fout:

    # if output for traing
    fout.write("id\tquery\tintent\tdomain\tQueryXml\r\n")
    for item in OutputSlotEvaluation:
        fout.write(item + '\r\n');

# for CMF intent evaluation format
with codecs.open((inputFile.split("."))[0] +'intent_evaluation.tsv', 'w', 'utf-8') as fout:

    # if output for traing
    fout.write("TurnNumber\tPreviousTurnIntent\tquery\tintent\r\n")
    for item in OutputIntentEvaluation:
        fout.write(item + '\r\n');

# for STCAevaluation

with codecs.open("sharemodeltest\\"+(inputFile.split("."))[0] +'intent_evaluation.tsv', 'w', 'utf-8') as fout:

    print("sharemodeltest\\"+(inputFile.split("."))[0] +'intent_evaluation.tsv')

    # if output for traing
    #fout.write("UUID\tQuery\tIntent\tDomain\tSlot\r\n")
    fout.write("id\tquery\tintent\tdomain\tQueryXml\r\n")
    for item in OutputSTCAIntentEvaluation:
        fout.write(item + '\r\n');

'''

#######################
# intent level output
#######################
'''
with codecs.open('teams_slot_training_after_filtering_teamspace_search.tsv', 'w', 'utf-8') as fout:
    for item in teamspaceSearchCandidateSet:
        fout.write(item + '\r\n');
'''

#######################
# slot level output
#######################

'''
with codecs.open('teams_slot_training_after_filtering_file_keyword.tsv', 'w', 'utf-8') as fout:
    for item in fileKeywordCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_file_name.tsv', 'w', 'utf-8') as fout:
    for item in fileNameCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_meeting_starttime.tsv', 'w', 'utf-8') as fout:
    for item in meetingStarttimeCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_file_type.tsv', 'w', 'utf-8') as fout:
    for item in fileTypeCandidateSet:
        fout.write(item + '\r\n');

# this is to deduplication
with codecs.open('teams_slot_training_after_filtering_file_recency.tsv', 'w', 'utf-8') as fout:
    for item in fileRecencyCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_sharetarget_type.tsv', 'w', 'utf-8') as fout:
    for item in sharetargetTypeCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_sharetarget_name.tsv', 'w', 'utf-8') as fout:
    for item in sharetargetNameCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_contact_name.tsv', 'w', 'utf-8') as fout:
    for item in contactNameCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_file_action.tsv', 'w', 'utf-8') as fout:
    for item in fileActionCandidateSet:
        fout.write(item + '\r\n');

with codecs.open('teams_slot_training_after_filtering_order_ref.tsv', 'w', 'utf-8') as fout:
    for item in orderRefCandidateSet:
        fout.write(item + '\r\n');
'''

#######################
# query replacement revert
#######################


from datetime import datetime
import io
import os
from pathlib import Path
import tempfile
import subprocess
import sys

from aqt import mw

from .config import gc
from .utils import is_executable, find_executable

def get_editor():
    user_choice = gc("editor")
    if is_executable(user_choice):
        return user_choice
    editors = [
        user_choice,
        user_choice + ".exe",
        "notepad++.exe",
        "notepad.exe",
        "code --wait",
        "gvim -f",
        "atom",
        "atom.exe",
        "gedit",
    ]
    if sys.platform == "darwin":
        editors.append("open -t")
    for editor in editors:
        command = find_executable(editor)
        if command:
            return command

    raise RuntimeError("Could not find external editor")

def edit(text, ext, named=False) -> tuple:
    editor = get_editor()
    if named and named [0] == "clayout":
        # some users might want to have a history of all versions of their templates.
        # For those the most discoverable/stable order would probably be:
        #   profilename/modelid__modelname/{Front|Back|Css}
        # The easiest solution seems to add a config option to store the files
        # created for each external edit.
        # note to self : VSCode's "Local History" extension only shows prior versions for files
        # of the same name, so it doesn't work if the filename has a stimestamp in it.
        tvf = gc("template_versions_folder")
        if tvf:
            basefolder = tvf
        else:
            basefolder = tempfile.gettempdir()
        mid_name = f"{named[1]}__{named[2]}"
        folder = os.path.join(basefolder, mw.pm.name, mid_name, named[3])
        Path(folder).mkdir(parents=True, exist_ok=True)
        now = datetime.now().strftime('%Y-%m-%d__%H-%M-%S')  # ":" is not allowed in ntfs
        path_and_filename = os.path.join(folder, now + ext)
    else:
        path_and_filename = tempfile.mktemp(suffix=ext)

    config = mw.addonManager.getConfig(__name__)
    do_beautify = config.get("beautify", True)
    if do_beautify and text.find(">") > -1:
        from bs4 import BeautifulSoup
        # From python docs: "Since it adds whitespace (in the form of newlines), prettify() changes the meaning of
        # an HTML document and should not be used to reformat one. The goal of prettify() is to help you visually
        # understand the structure of the documents you work with."
        # However, despite this, I think the benefits of pretty print outweigh the cons
        soup = BeautifulSoup(text, features='html.parser')
        text = soup.prettify()   # prettify the html

    with io.open(path_and_filename, 'w', encoding='utf-8') as file:
        file.write(text)

    cmd_list = editor.split() + [path_and_filename]
    proc = subprocess.Popen(cmd_list, close_fds=True)
    proc.communicate()

    with io.open(path_and_filename, 'r', encoding='utf-8') as file:
        return file.read(), path_and_filename


import os

import cv2
import numpy as np
import matplotlib.pyplot as plt

def get_image(n):
    dir_path = os.path.dirname(os.path.abspath(__file__))
    image_path = f"{dir_path}/images/{n}.jpg"

    return cv2.imread(image_path, 0)

def dft_trans(img):
    trans_img = np.fft.fft2(img)
    shifted_trans_img = np.fft.fftshift(trans_img)
    magnitude_spectrum = np.log(1 + np.abs(shifted_trans_img))
    return shifted_trans_img, magnitude_spectrum

def idft_trans(img):
    shifted_trans = np.fft.ifftshift(img)
    return np.fft.ifft2(shifted_trans).real.clip(0, 255).astype(np.uint8)

def plot(img):
    plt.imshow(img, cmap="gray")
    plt.show()

def gaussian_lowpass_filter(shape=(3, 3), sigma=0.5):
    m, n = [(ss - 1.) / 2. for ss in shape]
    y, x = np.ogrid[-m:m + 1, -n:n + 1]
    h = np.exp(-(x * x + y * y) / (2. * sigma ** 2))
    h[h < np.finfo(h.dtype).eps * h.max()] = 0
    sumh = h.sum()
    if sumh != 0:
        h /= sumh
    return h

def image_1():
    img = get_image(1)
    plot(img)

    shifted_trans_img, magnitude_spectrum = dft_trans(img)
    plot(magnitude_spectrum)

    width, height = img.shape
    mask = np.ones(img.shape, dtype=np.uint8)
    for i in range(width):
        if np.mean(magnitude_spectrum[i, :]) >= 9.:
            mask[i, :] = 0
    for j in range(height):
        if np.mean(magnitude_spectrum[:, j]) >= 9.:
            mask[:, j] = 0
    mask[width // 2 - 5:width // 2 + 5, height // 2 - 5:height // 2 + 5] = 1

    plot(mask * magnitude_spectrum)

    filtered_trans = mask * shifted_trans_img
    inv_img = cv2.GaussianBlur(idft_trans(filtered_trans), (5, 5), 0)

    plot(inv_img)

def image_2():
    img = get_image(2)
    plot(img)

    shifted_trans_img, magnitude_spectrum = dft_trans(img)
    plot(magnitude_spectrum)

    width, height = img.shape
    mask = np.ones(img.shape, dtype=np.uint8)
    mask[0:width // 2 - 5, height // 2 - 2:height // 2 + 2] = 0
    mask[width // 2 + 5:width, height // 2 - 2:height // 2 + 2] = 0

    plot(magnitude_spectrum * mask)

    filtered_trans = mask * shifted_trans_img
    inv_img = cv2.blur(idft_trans(filtered_trans), (2, 2))

    plot(inv_img)

def image_3():
    img = get_image(3)
    plot(img)

    shifted_trans_img, magnitude_spectrum = dft_trans(img)
    plot(magnitude_spectrum)

    mask = np.ones((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    cv2.circle(mask, (96, 117), 10, (0, 0, 0), -1)
    cv2.circle(mask, (42, 85), 10, (0, 0, 0), -1)
    cv2.circle(mask, (204, 178), 10, (0, 0, 0), -1)
    cv2.circle(mask, (258, 210), 10, (0, 0, 0), -1)
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

    plot(magnitude_spectrum * mask)

    filtered_trans = mask * shifted_trans_img
    inv_img = idft_trans(filtered_trans)

    plot(inv_img)

def image_4():
    pass

def image_5():
    img = get_image(5)
    plot(img)

    filtered_img = np.array(255 * (img / 255) ** 1.5, dtype=np.uint8)
    filtered_img = cv2.GaussianBlur(filtered_img, (5, 5), 0)

    plot(filtered_img)

def image_6():
    img = get_image(6)
    plot(img)

    clahe = cv2.createCLAHE(clipLimit=5, tileGridSize=(3, 3))
    filtered_img = clahe.apply(img)
    blurred_img = cv2.GaussianBlur(filtered_img, (3, 3), 1)
    filtered_img = cv2.addWeighted(filtered_img, 2, blurred_img, -1, -2)
    plot(np.clip(filtered_img, 0, 255))

def image_7():
    img = get_image(7)
    plot(img)

    clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(3, 3))
    filtered_img = clahe.apply(img)
    plot(filtered_img)

def image_8():
    img = get_image(8)
    plot(img)

    filtered_img = np.array(255 * (img / 255) ** .5, dtype=np.uint8)
    filtered_img = cv2.medianBlur(filtered_img, 3)

    plot(filtered_img)

def image_9():
    img = get_image(9)
    plot(img)

    filtered_img = np.array(255 * (img / 255) ** 0.9, dtype=np.uint8)
    blurred_img = cv2.GaussianBlur(filtered_img, (3, 3), 3)
    mask = img - blurred_img
    filtered_img = img + 0.1 * mask
    plot(filtered_img)

def image_10():
    img = get_image(10)
    plot(img)

    filtered_img = np.array(255 * (img / 255) ** 0.5, dtype=np.uint8)
    blurred_img = cv2.GaussianBlur(filtered_img, (3, 3), 1.5)
    mask = img - blurred_img
    filtered_img = img + 0.1 * mask
    plot(filtered_img)

def image_11():
    img = get_image(11)
    plot(img)

    shifted_trans_img, magnitude_spectrum = dft_trans(img)
    plot(magnitude_spectrum)

    h = cv2.normalize(gaussian_lowpass_filter(img.shape, sigma=40), None, 0, 1, cv2.NORM_MINMAX)

    transformed_img = shifted_trans_img * h

    inv_image = idft_trans(transformed_img)

    plot(inv_image)

def image_12():
    img = get_image(12)
    plot(img)

    shifted_trans_img, magnitude_spectrum = dft_trans(img)
    plot(magnitude_spectrum)

    h = cv2.normalize(gaussian_lowpass_filter(img.shape, sigma=40), None, 0, 1, cv2.NORM_MINMAX)

    transformed_img = shifted_trans_img * h

    inv_image = idft_trans(transformed_img)

    plot(inv_image)

def main():
    image_1()
    image_2()
    image_3()
    image_4()
    image_5()
    image_6()
    image_7()
    image_8()
    image_9()
    image_10()
    image_11()
    image_12()

if __name__ == '__main__':
    main()


from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import Q
from .forms import ProgramForm
from .models import Programs, Specialty
from django.views.generic import ListView

# Create your views here.
def index(request):
    return render(request, 'program/home.html', {'title': 'Main page'})

def create(request):
    error = ''
    if request.method == 'POST':
        form = ProgramForm(request.POST)
        if form.is_valid():
            form.save()
        else:
            error = 'Неверно введены значения'

    form = ProgramForm()
    data = {
        'form': form,
        'title': 'Creation',
        'error': error
    }

    return render(request, 'program/create.html', data)

class SearchListView(ListView):
    model = Programs

    template_name = 'program/search_results.html'

    # queryset = Programs.objects.filter(Name__contains="Math")
    def queryset(self):
        query = self.request.GET.get('q')
        object_list = Programs.objects.filter(
            Q(Name__contains=query) | Q(Dep__Code__contains=query) | Q(Specialty__Name__contains=query)
        ).distinct()
        return object_list


import torch

class TransformerDataset(torch.utils.data.Dataset):
    def __init__(self, texts, labels, tokenizer, max_length):
        self.texts = texts
        self.labels = labels
        self.tokenizer = tokenizer
        self.max_length = max_length

    def __getitem__(self, idx):
        text = str(self.texts[idx])
        text = " ".join(text.split())
        inputs = self.tokenizer.encode_plus(
            text, None, add_special_tokens=True, max_length=self.max_length, truncation=True
        )

        ids = inputs["input_ids"]
        token_type_ids = inputs["token_type_ids"]
        mask = inputs["attention_mask"]
        padding_length = self.max_length - len(ids)

        ids = ids + ([0] * padding_length)
        mask = mask + ([0] * padding_length)
        token_type_ids = token_type_ids + ([0] * padding_length)
        return {
            "input_ids": torch.tensor(ids, dtype=torch.long),
            "attention_mask": torch.tensor(mask, dtype=torch.long),
            "token_type_ids": torch.tensor(token_type_ids, dtype=torch.long),
            "label": torch.tensor(self.labels[idx], dtype=torch.float),
        }

    def __len__(self):
        return len(self.labels)


# -*- coding: utf8 -*-
import os
import math
import re

def eval_map(rele_dict, res_dict):
    map_list = []
    for topic in rele_dict.keys():
        map = 0
        count = 0
        for i in range(len(rele_dict[topic]['rele_list'])):
            pid = rele_dict[topic]['rele_list'][i]
            if pid in res_dict[topic].keys():
                count += 1
                map += count / res_dict[topic][pid]['rank'] * 1.0
        map = map / rele_dict[topic]['rele_num']
        map_list.append(map)

    avg = sum(map_list) / len(map_list) * 1.0
    return avg

def get_rank(rate):
    if rate < 0.2:
        return 1
    elif rate < 0.4:
        return 2
    elif rate < 0.6:
        return 3
    elif rate < 0.8:
        return 4
    else:
        return 5

def eval_ndcg(res_dict, p):
    NDCG_list = []
    for topic in res_dict.keys():
        DCG = 0
        IDCG = 0
        i = 1
        for pid in res_dict[topic]:
            rel = get_rank(res_dict[topic][pid]['rate'])
            if i <= p:
                DCG += (math.pow(2, rel) - 1) / math.log(1+i, 2) * 1.0
            IDCG += (math.pow(2, rel) - 1) / math.log(1 + i, 2) * 1.0
            i += 1
        NDCG = DCG / IDCG * 1.0
        NDCG_list.append(NDCG)

    avg = sum(NDCG_list) / len(NDCG_list) * 1.0
    return avg

if __name__ == '__main__':
    qrel_path = '../2017_test_qrels/qrel_abs_test.txt'
    res_path = './testing/10152130122_钱庭涵_point-wise.res'

    file1 = open(qrel_path, 'r')
    lines = file1.readlines()
    file1.close()
    file0 = open('./qrels_testing.res', 'w')
    rele_dict = {}
    for line in lines:
        tmp1 = line.strip('\n').strip(' ')
        tmp2 = re.split('[ ]+', tmp1)
        file0.write(tmp2[0] + ' ' + tmp2[1] + ' ' + tmp2[2] + ' ' + tmp2[3] + '\n')
        if tmp2[0] not in rele_dict.keys():
            rele_dict[tmp2[0]] = {'rele_num': 0, 'rele_list': []}
        if int(tmp2[3]) == 1:
            rele_dict[tmp2[0]]['rele_num'] += 1
            rele_dict[tmp2[0]]['rele_list'].append(tmp2[2])
    file0.close()
    print('qrel dictionary succeed')

    file2 = open(res_path, 'r')
    lines = file2.readlines()
    file2.close()
    res_dict = {}
    for line in lines:
        tmp1 = line.strip('\n')
        tmp2 = re.split(' ', tmp1)
        if tmp2[0] not in res_dict.keys():
            res_dict[tmp2[0]] = {}
        res_dict[tmp2[0]][tmp2[2]] = {'rank': int(tmp2[3]), 'rate': float(tmp2[4])}
    print('result dictionary succeed')

    MAP = eval_map(rele_dict, res_dict)
    print('MAP =', MAP)

    NDCG = eval_ndcg(res_dict, 10)
    print('NDCG@10 =', NDCG)
    NDCG = eval_ndcg(res_dict, 20)
    print('NDCG@20 =', NDCG)
    NDCG = eval_ndcg(res_dict, 50)
    print('NDCG@50 =', NDCG)
    NDCG = eval_ndcg(res_dict, 100)
    print('NDCG@100 =', NDCG)
    NDCG = eval_ndcg(res_dict, 200)
    print('NDCG@200 =', NDCG)


class Solution(object):
    def longestIncreasingPath(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: int
        """
        maxlen =0
        if not matrix:
            return 0
        m = len(matrix)
        n = len(matrix[0])
        def dfs(i,j,m,n):
            if not dp[i][j]:#当之前dfs中，某一个点出发的递增序列得到了，那么在下一轮迭代中，再次经过该节点一样不变，所以这一项就是去重复遍历操作
                tmp = matrix[i][j]
                dp[i][j] = 1+ max(dfs(i+1,j,m,n) if i<m-1 and tmp <matrix[i+1][j]  else 0
                                ,dfs(i-1,j,m,n) if i and tmp <matrix[i-1][j]  else 0
                                ,dfs(i,j+1,m,n) if j<n-1 and tmp<matrix[i][j+1]  else 0
                                ,dfs(i,j-1,m,n) if j and tmp < matrix[i][j-1]  else 0
                            )

            return dp[i][j]
        dp = [[0] * n for i in range(m)]
        return max(dfs(x, y,m,n) for x in range(m) for y in range(n))

算法面试题汇总/8动态规划/8矩阵中的最长递增路径-通过-参考别人的.py

8矩阵中的最长递增路径-通过-参考别人的.py


import os
import sys
import pandas as pd

# df = pd.read_csv("input/phrases_interview.csv", header=None, names=["Phrase"])
df = pd.read_csv("input/phrases.csv", header=0)
TITLES = [line.rstrip("\n") for line in open("input/titles.csv")]
# TITLES = [
#     "firefighter",
#     "substitute teacher",
#     "dental hygienist",
#     "teacher",
#     "licensed practical nurse",
#     "dental assistant",
#     "nursing assistant",
#     "surgical technician",
# ]

# TITLES = ["teacher", "project manager", "business analyst", "accountant", "administrative assistant", "product manager"]

if __name__ == "__main__":
    if len(sys.argv) >= 3:
        low = int(sys.argv[1])
        high = int(sys.argv[2])
    else:
        low = 0
        high = len(TITLES)
    for TITLE in TITLES[low:high]:
        df["query"] = df.apply(lambda x: x["Phrase"].replace("{keyword}", TITLE), axis=1)
        input_file = "input/faq_queries_{}.csv".format(TITLE.lower().replace(" ", "_"))
        df[["query"]].to_csv(input_file, index=False, header=False)
        output_fs_file = "output/fs_" + TITLE.lower().replace(" ", "_") + ".csv"
        output_paa_file = "output/paa_" + TITLE.lower().replace(" ", "_") + ".csv"
        command = """poetry run python google_paa.py query {} --output_paa={} --skip-fs --headless --cleantext --num=10""".format(
            input_file, output_paa_file
        )
        os.system(command)


class Solution:
    def shortestToChar(self, S: str, C: str) -> List[int]:

        #         def getClosestIndex(index, char_index):

        #             distance = float('inf')

        #             for i, candidate_index in enumerate(char_index):

        #                 if abs(candidate_index-index) < distance:
        #                     distance = abs(candidate_index - index)

        #             return distance

        #         ans = []
        #         char_index = []

        #         for index, c in enumerate(S):
        #             if c == C:
        #                 char_index.append(index)

        #         for index, s in enumerate(S):

        #             if s == C:
        #                 ans.append(0)
        #             else:
        #                 ans.append(getClosestIndex(index, char_index))

        #         return ans

        closest = []
        ans = []

        prev = -float('inf')
        for index, char in enumerate(S):

            if char == C:
                prev = index
            closest.append(index - prev)

        prev = float('inf')
        for index in range(len(S) - 1, -1, -1):

            if S[index] == C:
                prev = index
            ans.append(min(closest[index], prev - index))

        return reversed(ans)


import os
from flask import Flask, request, redirect, url_for, send_from_directory, jsonify
from werkzeug.utils import secure_filename
from flask import send_file
from config import root_directory

#allowed extension files are only accepeted on the service
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
from model_api import *
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(root_directory,'model-building','user-images')
def allowed_file(filename):
    return filename[-3:].lower() in ALLOWED_EXTENSIONS

#-----------------------------------------------------
# @author: DANDE TEJA          <cs17b010@iittp.ac.in>
# @date: 21/05/2021
#-----------------------------------------------------
#route to pool the models specified by the user
#and uploading the predicted pools to a specific user
@app.route('/predict/<pool_name>/<user>', methods=['GET', 'POST'])
def upload_file_pronoun(pool_name,user):
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            print('**found file', file.filename)
            print(user)
            filename = secure_filename(file.filename)
            if(user in os.listdir(app.config['UPLOAD_FOLDER'])):
                clean = os.popen('rm -rf '+os.path.join(root_directory,'model-building','user-images',user,"*")).read()
                print(clean)
                print('cleaned')
            else:
                make = os.popen('mkdir '+os.path.join(root_directory,'model-building','user-images',user)).read()
                print(make)
                print('directory created!')
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],user, filename))
            request_data = pool_name
            print('request-data: ',request_data)
            last = request_data[len(request_data)-1]
            if(last == ','):
                request_data = request_data[:len(request_data)-1]
            file_path = os.path.join(app.config['UPLOAD_FOLDER'],user, filename)
            print(file_path)
            print("*********__************")
            json_str = get_model_data(user,file_path,request_data)
            print("*********__************")
            return json_str
    return '''
    <!doctype html>
    <title>Model Prediction Server</title>

Auto Annotation Service

    '''

#----------------------------------------------------
# @author: DANDE TEJA          <cs17b010@iittp.ac.in>
# @date: 21/05/2021
#----------------------------------------------------
#main route running on port number 7001 and hosting on the
#IP address 0.0.0.0
if __name__ == '__main__':
	app.run(host='0.0.0.0', port=7001, debug=True)


# -*- coding: utf-8 -*-
import logging
import commons
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.httpexceptions import (HTTPFound, HTTPNotFound)
from bproject.models.facades import SettingsFacade, HistoryFacade
from bproject.models.models import SetupCategory, SetupDictionary
from bproject.models.view import Return
from bproject.logic import static, helpers
from datetime import datetime

log = logging.getLogger(__name__)

class Settings(object):
    """docstring for Settings"""
    def __init__(self, request):
        super(Settings, self).__init__()
        self.request = request
        self.history = HistoryFacade(request)
        self.facade = SettingsFacade(request)

    @view_config(route_name="settings", request_method="GET", renderer="bproject:/templates/settings/index.html", permission="view", http_cache=60)
    def main(self):
        log.debug("settings_main")
        return dict()

    @helpers.response
    @view_config(route_name="settings_all", request_method="GET", renderer="json", permission="view", http_cache=10)
    def all(self):
        log.debug("settings_all_json")
        return  self.facade.get_dictionary()

    @helpers.response
    @view_config(route_name="settings_categories", request_method="POST", renderer="json", permission="add")
    def add_category(self):
        """
        Add new category to settings
        """
        log.debug("Add category: started")
        json_data = self.request.json_body.get("category")
        category = helpers.from_json(json_data, SetupCategory())
        response = self.facade.add_category(category)
        if response:
            self.history.add(static.options.categories, static.actions.add, "Code[{0}], Name[{1}], Options[{2}]".format(category.code, category.name, category.options))
        log.debug("Add category: finished")
        return response

    @helpers.response
    @view_config(route_name="settings_categories_manage", request_method="PUT", permission="edit", renderer="json")
    def edit_category(self):
        """
        Modifies given category by id
        """
        log.debug("Edit category: started")
        idx = self.request.matchdict["id"]
        category = helpers.from_json(self.request.json_body["category"], SetupCategory())
        response = self.facade.edit_category(idx, category)
        if response:
            self.history.add(static.options.categories, static.actions.edit, "Category {0} updated".format(category.code))
        log.debug("Edit category: finished")
        return response

    @helpers.response
    @view_config(route_name="settings_categories_manage", request_method="DELETE", permission="delete", renderer="json")
    def delete_category(self):
        """
        Deletes settings category by id
        """
        log.debug("Delete category: started")
        idx = self.request.matchdict["id"]
        response = self.facade.delete_category(idx)
        if response:
            self.history.add(static.options.categories, static.actions.delete, "Category {0} removed".format(idx))
        log.debug("Delete category: finished")
        return response

    @helpers.response
    @view_config(route_name="settings_entries", request_method="POST", permission="add", renderer="json")
    def add_entry(self):
        """
        Inserts new dictionary entry
        """
        log.debug("Add entry: started")
        data = self.request.json_body.get("entry")
        entry = helpers.from_json(data, SetupDictionary())
        category = data.get("category")
        log.info(category)
        response =  self.facade.add_entry(entry, category)
        if response:
            self.history.add(static.options.settings, static.actions.add,"Setting entry {0} added".format(entry.code))
        log.debug("Add entry: finished")
        return response

    @helpers.response
    @view_config(route_name="settings_entries_manage", request_method="PUT", permission="edit", renderer="json")
    def edit_entry(self):
        log.debug("Edit entry: started")
        idx = self.request.matchdict["id"]
        data = self.request.json_body.get("entry")
        entry = helpers.from_json(data, SetupDictionary())
        response = self.facade.edit_entry(idx, entry)
        if response:
            self.history.add(static.options.settings, static.actions.edit,"Setting entry {0} updated".format(entry.code))
        log.debug("Edit entry: finished")
        return response

    @helpers.response
    @view_config(route_name="settings_entries_manage", request_method="DELETE", permission="delete", renderer="json")
    def delete_entry(self):
        log.debug("Delete entry: started")
        idx = self.request.matchdict["id"]
        ret = self.facade.delete_entry(idx)
        if ret:
            self.history.add(static.options.settings, static.actions.delete,"Setting entry {0} removed".format(idx))
        log.debug("Delete entry: finished")
        return ret


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import binascii
import struct
import socket

def main():
    server_address = (
        sys.argv[1] if len(sys.argv) >= 2 else "localhost",
        int(sys.argv[2]) if len(sys.argv) >= 3 else 10000
    )

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(server_address)

    values = (1, b"ab", 2.7)
    packer = struct.Struct("I 2s f")
    packed_data = packer.pack(*values)

    try:
        print("sending {!r}".format(binascii.hexlify(packed_data)))
        sock.sendall(packed_data)
    finally:
        print("closing socket")
        sock.close()

if __name__ == '__main__':
    main()


def getSection(h,roi,dist):
    import numpy as np
    import random as rd

    roi_out     = []
    dist_out    = []
    site_out    = []

    for ct1, roiL in enumerate(roi):

        if 'soma' in roiL:
            site_out.append(h.soma[0](0.5))
            dist_out.append(dist[ct1])
            roi_out.append(roiL)
        else:

            D = np.empty((100,2))
            D[:] = np.NaN
            for count,sec in enumerate(h.allsec()):
                D[count,1] = h.distance(sec(1),h.soma[0](0.5))
                D[count,0] = h.distance(sec(0),h.soma[0](0.5))
                if roiL not in str(sec):
                    D[count,0:2] = np.NaN
            D = D[0:count+1, :]

            idDist  = np.where(((D[:,0]<dist[ct1]) & (D[:,1]>dist[ct1])))

            if len(idDist[0])>0:
                sel     = rd.randrange(len(idDist[0]))

                for count,sec in enumerate(h.allsec()):
                    if count == idDist[0][sel]:
                        segPart = (dist[ct1] - D[count, 0])/sec.L

                        site_out.append(sec(segPart))
                        dist_out.append(dist[ct1])
                        roi_out.append(roiL)

    return site_out, dist_out, roi_out


"""
# ===========================================================================
# Interface between the ION's implementation of the CCSDS CFDP and Python.
# Internally, all classes call the C Extension _cfdp.
#
# Author: Marc Sanchez Net
# Date:   04/17/2019
# Copyright (c) 2019, California Institute of Technology ("Caltech").
# U.S. Government sponsorship acknowledged.
# ===========================================================================
"""

# General imports
from unittest.mock import Mock
from pathlib import Path
from threading import Event, Thread
from warnings import warn

# Module imports
import pyion
import pyion.utils as utils
from pyion.constants import CfdpEventEnum

# Import C Extension
try:
	import _cfdp
except ImportError:
	warn('_cfdp extension not available. Using mock instead.')
	_cfdp = Mock()

# Define all methods/vars exposed at pyion
__all__ = ['Entity']

# ============================================================================
# === Entity class
# ============================================================================

class Entity():
	""" Class to represent a CFDP entity. """
	def __init__(self, proxy, peer_entity_nbr, param_addr, endpoint, mode,
				 closure_latency, seg_metadata):
		""" Class initializer """
		# Store variables
		self.proxy        = proxy
		self.entity_nbr   = peer_entity_nbr
		self.node_dir     = proxy.node_dir
		self._param_addr  = param_addr
		self.endpoint     = endpoint
		self.mode         = mode
		self.closure_lat  = closure_latency
		self.seg_metadata = seg_metadata

		# Map {event_id}: event handler function
		self.event_handers = {}

		# Create a condition to signal end of transaction
		self._end_transaction = Event()
		self._ok_transaction = False

		# Start a thread to monitor all events
		self.th = Thread(target=self._monitor_events, daemon=True)
		self.th.start()

	def __del__(self):
		# If you have already been closed, return
		if not self.is_open:
			return

		# Close the Entity and free C memory
		self.proxy.cfdp_close(self.entity_nbr)

	@property
	def is_open(self):
		""" Returns True if entity is opened """
		return (self.proxy is not None and self._param_addr is not None)

	def _cleanup(self):
		""" Clean entity after closing. Do not call directly,
			use ``proxy.cfdp_close``
		"""
		# Clear variables
		self.proxy       = None
		self.entity_nbr  = None
		self._param_addr = None
		self.endpoint    = None

		# Stop the thread monitoring CFDP events
		_cfdp.cfdp_interrupt_events()

		# Wait until the event thread has finished
		self.th.join()

		# Mark end of transactions to wake up threads
		self._mark_transaction_end(False)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_send(self, source_file, dest_file=None, mode=None,
				  closure_lat=None, seg_metadata=None):
		""" Send a file using CFDP to the peer engine for this entity

			:param source_file: str or Path of file to send
			:param dest_file: str or Path. Name of file at receiving
							  engine. It defaults to source_file
			:param **kwargs: See ``proxy.cfdp_send``
		"""
		# Initialize variables
		src_file = Path(source_file).resolve().absolute()

		# Set default values
		dst_file = source_file if dest_file is None else dest_file

		# Check that the source file exists
		if not src_file.exists():
			raise IOError('Source file {} does not exist'.format(src_file))

		# Set default values if necessary
		if mode is None: mode = self.mode
		if closure_lat is None: closure_lat = self.closure_lat
		if seg_metadata is None: seg_metadata = self.seg_metadata

		# Mark that the current transaction has not succeeded yet
		self._ok_transaction = False

		# Trigger CFDP send
		_cfdp.cfdp_send(self._param_addr, str(src_file), str(dst_file),
							  closure_lat, seg_metadata, mode)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_request(self, source_file, dest_file=None, mode=None,
				  	 closure_lat=None, seg_metadata=None):
		""" Request a file to be sent to this node using CFDP

			:param source_file: str or Path of file to request
			:param dest_file: str or Path. Name of file at this node
							  once it is received. Defaults to ``source_file``
			:param **kwargs: See ``proxy.cfdp_send``
		"""
		# Set default values if necessary
		if mode is None: mode = self.mode
		if closure_lat is None: closure_lat = self.closure_lat
		if seg_metadata is None: seg_metadata = self.seg_metadata

		# Mark that the current transaction has not succeeded yet
		self._ok_transaction = False

		# Trigger CFDP request
		_cfdp.cfdp_request(self._param_addr, str(source_file), str(dest_file),
								 closure_lat, seg_metadata, mode)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_cancel(self):
		""" Cancel the current CFDP transaction """
		_cfdp.cfdp_cancel(self._param_addr)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_suspend(self):
		""" Suspend the current CFDP transaction """
		_cfdp.cfdp_suspend(self._param_addr)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_resume(self):
		""" Resume the current CFDP transaction """
		_cfdp.cfdp_resume(self._param_addr)

	@utils._chk_is_open
	@utils.in_ion_folder
	def cfdp_report(self):
		""" Request issuance on the transmission/reception progress of the current CFDP
			transaction.
		"""
		_cfdp.cfdp_report(self._param_addr)

	@utils._chk_is_open
	def add_usr_message(self, msg):
		""" Add a user message to all CFDP PDUs in the next transaction

			:param str: User message to add
		"""
		_cfdp.cfdp_add_usr_msg(self._param_addr, msg)

	@utils._chk_is_open
	def add_filestore_request(self, action, file1, file2=None):
		""" Add a user message to all CFDP PDUs in the next transaction

			:param action: See ``pyion.CFDP_CREATE_FILE``, etc.
			:param file1: String or Path-object
			:param file2: None, string or Path-object.
		"""
		# Transform files to string
		file1 = str(file1)
		if file2 is not None: file2 = str(file2)

		# Call pyion function
		_cfdp.cfdp_add_filestore_request(self._param_addr, action, file1, file2)

	def register_event_handler(self, event, func):
		""" Register and event handler for this entity

			:param event: See ``pyion.constants.CFDP_CREATE_FILE``, etc.
			:param func: Function handle with the following signature
						 ``def ev_handler(params)``, where params is a
						 dictionary with contents that depend on the type
						 of event (see CCSDS CFDP spec, section 3.5.6)
		"""
		self.event_handers[event] = func

	def wait_for_transaction_end(self, timeout=None):
		""" Blocks the calling thread until the transaction has
			finished or the timeout expires.

			:param timeout: Time to wait in [seconds]
			:return: True if transaction finished successfully
		"""
		# Wait for the end of the transaction
		self._end_transaction.wait(timeout=timeout)

		# Return state of transaction
		return self._ok_transaction

	def _mark_transaction_end(self, success):
		""" Mark that the current CFDP transaction has ended

			:param success: True/False
		"""
		# Mark the transaction as successful
		self._ok_transaction = success

		# Awake all threads that were waiting
		self._end_transaction.set()

		# Reset the event
		self._end_transaction.clear()

	def _monitor_events(self):
		""" Monitor all CFDP events """
		while self.is_open:
			# Get the next event
			evt, ev_params = _cfdp.cfdp_next_event()

			# Create event type class from integer code
			evt = CfdpEventEnum(evt)

			# If no event, continue
			if evt == CfdpEventEnum.CFDP_NO_EVENT:
				continue

			# If you have an event handler for this event type, use it
			try:
				func = self.event_handers[evt]
				func(evt, ev_params)
			except KeyError:
				pass

			# If you have a handler for all events, use it
			try:
				func = self.event_handers[CfdpEventEnum.CFDP_ALL_IND]
				func(evt, ev_params)
			except KeyError:
				pass

			# If transaction finished ok, report it
			if evt == CfdpEventEnum.CFDP_TRANSACTION_FINISHED_IND:
				self._mark_transaction_end(True)

			# If transaction failed, report it
			if evt == CfdpEventEnum.CFDP_ABANDONED_IND:
				self._mark_transaction_end(False)

	def __enter__(self):
		""" Allows an endpoint to be used as context manager """
		return self

	def __exit__(self, exc_type, exc_val, exc_tb):
		""" Allows an endpoint to be used as context manager """
		pass

	def __str__(self):
		return '<Entity: {} ({})>'.format(self.entity_nbr, 'Open' if self.is_open else 'Closed')

	def __repr__(self):
		return '<Entity: {} ({})>'.format(self.entity_nbr, self._param_addr)