from __future__ import print_function, unicode_literals
from pprint import pprint
from PyInquirer import prompt, Separator
from examples import custom_style_2
import cvgenerator as cv
import os

# from PyInquirer import style_from_dict, Token, prompt, print_json
# from PyInquirer import Validator, ValidationError

class choice():
    def __init__(self, name, method=None):
        self.name = name
        self.connector = method

    def call_connector(self):
        '''calls the method assigned to the choice object'''
        try:
            self.connector()
        except Exception as e:
            print(str(e))

class menu():
    def __init__(self, name, message, escapable=True, exitable=True):
        self.type = 'list'
        self.name = name
        self.message = message
        self.choices = []
        self.has_been_shown = False
        self.include_back_option = escapable
        self.include_exit_option = exitable
        self.back_option = choice('<<< Back', method=self.__escape_loop)
        self.exit_option = choice('<X> Exit', method=self.__exit)

    def show(self):
        '''displays the list menu'''
        output = None
        self.escaped = False

        if self.has_been_shown == False:
            self.choices.append(Separator())
            if self.include_back_option == True:
                self.choices.append(self.back_option)
            if self.include_exit_option == True:
                self.choices.append(self.exit_option)
            self.has_been_shown = True

        while self.escaped == False and cv.IS_EXITED == False:
            # os.system('clear') #! This may cause issues!
            answer = prompt({
                'type': self.type,
                'name': self.name,
                'message': self.message,
                'choices': self.__get_choice_names()
            }, style=custom_style_2)
            choice = self.__return_choice(answer[self.name])
            output = choice.name
            if not choice == None and not choice.connector == None:
                choice.call_connector()
            else:
                self.escaped = True
        return output

    def add_choice(self, input: choice):
        self.choices.append(input)

    def add_choices(self, input: list):
        for i in input:
            self.add_choice(i)

    def __get_choice_names(self):
        '''takes the choice objects in self.choices and creates a list of their names'''
        output = []
        for choice in self.choices:
            if hasattr(choice, 'name'):
                output.append(choice.name)
            else:
                output.append(choice)
        return output

    def __return_choice(self, choice_name):
        '''simply a filter to retrieve the choice object with the choice_name input'''
        output = None
        for x in self.choices:
            try:
                if x.name == choice_name:
                    output = x
            except Exception as e:
                pass
        return output

    def __escape_loop(self):
        '''when called, this breaks the while loop in self.show'''
        self.escaped = True

    def __exit(self):
        '''instructs all forms to exit'''
        cv.IS_EXITED = True

class prompts():
    '''
    Dicts for PyInquirer affect the readability of user workflows.
    This class abstracts them away so questions take up little space for complex methods.
    '''
    @staticmethod
    def input_prompt(name: str, message: str, prompt_now=True, default=None):
        '''
        If the prompt is used at invocation, input_prompt will return the user's selection;
        if not it returns the question dict for later use.
        '''
        output = None
        question = {
            'type': 'input',
            'name': name,
            'message': message,
            'default': default
        }
        if prompt_now == True:
            answer = prompt(question)
            output = answer[name]
        else:
            output = question #? Returns the question dict if the question is not asked immediately.
        return output

    @staticmethod
    def list_prompt(name: str, message: str, list_array: list, prompt_now=True):
        '''
        If the prompt is used at invocation, list_prompt will return the user's selection;
        if not it returns the question dict for later use.
        '''
        output = None
        question = {
            'type': 'list',
            'name': name,
            'message': message,
            'choices': list_array
        }
        if prompt_now == True:
            answer = prompt(question)
            output = answer[name]
        else:
            output = question #? Returns the question dict if the question is not asked immediately.
        return output

    @staticmethod
    def question_sequence(questions: list):
        '''
        Simple wrapper for the 'prompt' method. This will return a dict containing user inputs.
        '''
        return prompt(questions)

    @staticmethod
    def edit_existing_file(file_path: str):
        question = {
                'type': 'editor',
                'name': 'edits',
                'message': 'Edits:\n',
                'eargs': {
                    'editor':'nano',
                    'filename': file_path
                }
            }

        prompt(question, style=custom_style_2)

    @staticmethod
    def checkbox(checkbox_list: list, message: str):

        question = [
            {
                'type': 'checkbox',
                'qmark': 'ðŸ˜ƒ',
                'message': message,
                'name': 'selection',
                'choices': checkbox_list
            }
        ]

        answers = prompt(question, style=custom_style_2)
        return answers['selection']

    @staticmethod
    def confirm(message: str, default=False):
        '''Confirms whether a user wants to continue.'''
        answer = prompt({
                'type': 'confirm',
                'message': message,
                'name': 'confirm',
                'default': default,
            }, style=custom_style_2)

        return answer['confirm']

    @staticmethod
    def get_separator(input=None):
        return Separator(input)


# standard Python imports
import os
import argparse
import time
import pickle

# 3rd party libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import utils
from knn import KNN

def load_dataset(filename):
    with open(os.path.join('..','data',filename), 'rb') as f:
        return pickle.load(f)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-m','--module', required=True,
        choices=["1"])

    io_args = parser.parse_args()
    module = io_args.module

    # K-Nearest Neighbors
    if module == '1':
        dataset = load_dataset("citiesSmall.pkl")
        X, y = dataset["X"], dataset["y"]
        X_test, y_test = dataset["Xtest"], dataset["ytest"]
        #model = KNN(1)
        model = KNN(3)
        #model = KNN(10)

        model.fit(X, y)

        # training error
        y_pred = model.predict(X)
        tr_error = np.mean(y_pred != y)

        # test error
        y_pred = model.predict(X_test)
        te_error = np.mean(y_pred != y_test)

        utils.plotClassifier(model, X, y)
        fname = os.path.join("..", "figs", "knn_classification.pdf")
        plt.savefig(fname)
        pass


import heapq

# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class SummaryRanges(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.intervals = []

    def addNum(self, val):
        """
        :type val: int
        :rtype: void
        """
        heapq.heappush(self.intervals, (val, [val, val]))

    def getIntervals(self):
        """
        :rtype: List[Interval]
        """
        stack = []

        while self.intervals:
            _, heap_top = heapq.heappop(self.intervals)
            if stack:
                _, stack_top = stack[-1]
                if stack_top[1] + 1 >= heap_top[0]:
                    stack[-1] = (stack_top[0], [stack_top[0], max(stack_top[1], heap_top[1])])
                else:
                    stack.append((heap_top[0], heap_top))
            else:
                stack.append((heap_top[0], heap_top))

        self.intervals = stack
        return list(map(lambda x: x[1], self.intervals))

# Your SummaryRanges object will be instantiated and called as such:
# obj = SummaryRanges()
# obj.addNum(val)
# param_2 = obj.getIntervals()


from pip._vendor.distlib.compat import raw_input
from funcoes import inverte, concatena

print("Informe um texto")

texto = raw_input()

texto = inverte.inversao(texto)

texto = concatena.concat(texto)

print(texto)


class Solution:
    def groupThePeople(self, arr: List[int]) -> List[List[int]]:
        mp = collections.defaultdict(list)

        for i,x in enumerate(arr):
            mp[x].append(i)

        res = []

        for key in mp:
            groups = mp[key]
            n = len(groups)

            if n == key:
                res.append(groups)
            else:
                for i in range(0, n, key):
                    res.append(groups[i:i+key])

        return res


#client bloccante: se tutte le operazioni sono sequenziali possiamo usare questo client single threaded.

import paho.mqtt.client as paho
import pandas as pd
import numpy as np
import json
import datetime
import os

from log_to_json import JsonFormatter
import logging

def setup_logger(name, log_file, level=logging.INFO):
    """To setup as many loggers as you want"""

    handler = logging.FileHandler(log_file)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger, handler

# first file logger
logger_client_error, handler_client_error = None, None
json_formatter = JsonFormatter(
    #keys=("message", "name")
)

# second file logger
logger_output, handler_output = None, None

# second file logger
logger_topic, handler_topic = None, None

class BrokerNameException(Exception):
    """Raised when the broker name is none or empty """
    def __init__(self, message="the broker name is none or empty"):
        self.message = message
        logger_client_error.error({
            'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
            "topic": None,
            "msg": message
        })
        super().__init__(self.message)

class PortNumberException(Exception):
    """Raised when the port number is none"""
    def __init__(self, message="the port number is none"):
        self.message = message
        logger_client_error.error({
            'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
            "topic": None,
            "msg": message
        })
        super().__init__(self.message)

class EmptyMessageException(Exception):
    """Raised when the message is empty"""
    def __init__(self, topic, message="the message is empty"):
        self.topic = topic
        self.message = self.topic +': '+ message
        logger_client_error.error({
            'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
            "topic": self.topic,
            "msg": message
        })
        super().__init__(self.message)

#initialize weights and variable

FTD_MAX_PUBLISH = 1

FTD = 1

IDC = 0
IDV = 0
threshold_i_c = 1
threshold_i_v = 2
weight = 1.01
threshold_v = 300
decimals = 4
weight_anger = 0.125
weight_happiness = 0.125
weight_fear = 0.083
weight_sadness = 0.083
weight_neutral = 0
weight_disgust = 0.042
weight_sorprise = 0.042
weights_emozioni = pd.Series([weight_anger, weight_happiness, weight_fear, weight_sadness, weight_neutral, weight_disgust, weight_sorprise])
s = 0 #speed value
Ei = 0
DCi = 0
DVi = 0
timestamp_relab=0;

flagE = False
flagD = False
flagV = False

#variable for log
anger = 0
happiness = 0
fear = 0
sadness = 0
neutral = 0
disgust = 0
surprise = 0
cd = 0 #cognitive distraction value
vd = 0 #visual distraction value
arousal=0

anger_buffer = [0,0,0,0]
happiness_buffer = [0,0,0,0]
fear_buffer = [0,0,0,0]
sadness_buffer = [0,0,0,0]
neutral_buffer = [0,0,0,0]
disgust_buffer = [0,0,0,0]
surprise_buffer = [0,0,0,0]
speed_buffer = [0,0,0,0]
arousal_buffer = [0, 0, 0, 0]  # 1 arousal max, 0 arousal min

user = ''

UNITO_TOPIC = "NP_UNITO_DCDC"
ARAUSAL_TOPIC = "NP_UNIPR_AROUSAL"
FTD_TOPIC = "NP_UNIBO_FTD"
EMOJI_TOPIC = "Emotions"
RELAB_TOPIC = "RL_VehicleDynamics"
RULEX_TOPIC = "DSS"
AITEK_TOPIC = "AITEK_EVENTS"

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

def logTopic(topic, msg):
    logger_topic.critical({
        "topic": topic,
        "msg": msg
    })

def on_message(client, userdata, msg):
    global FTD, IDC, IDV, weight, decimals, threshold_v, threshold_i_v, threshold_i_c, DCi, DVi, s, Ei, flagD, flagE, flagV
    global anger, happiness, fear, sadness, neutral, disgust, surprise, cd, vd , arousal
    global anger_buffer, happiness_buffer, fear_buffer, sadness_buffer, neutral_buffer, disgust_buffer, surprise_buffer, speed_buffer, timestamp_relab, arousal_buffer
    global user
    global UNITO_TOPIC, AITEK_TOPIC, ARAUSAL_TOPIC, FTD_TOPIC, EMOJI_TOPIC, RELAB_TOPIC, RULEX_TOPIC
    #print("topic: "+msg.topic)

    if msg.topic == RELAB_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                raise EmptyMessageException(topic=RELAB_TOPIC)
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
                s = json.loads(str(msg.payload.decode("utf-8")))
                timestamp_relab = s['VehicleDynamics']['timestamp']
                speed_buffer.pop(0)
                speed_buffer.append(s['VehicleDynamics']['speed']['x'])

        except Exception as exception:
            print(exception)

        #flagV = True
    elif msg.topic == UNITO_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                raise EmptyMessageException(topic=UNITO_TOPIC)
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
                D = json.loads(str(msg.payload.decode("utf-8")))

                cd = D['cognitive_distraction'] if D['cognitive_distraction_confidence'] != 0 else 0.0
                if D['cognitive_distraction_confidence'] == 0.0:
                    logger_client_error.warning({
                        'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
                        "topic": UNITO_TOPIC,
                        "msg": 'NO cognitive distraction value'
                    })
                    print('NO cognitive distraction value')

        except Exception as exception:
            cd = 0.0
            print(exception)

        speed_mean = np.mean(speed_buffer)
        if (cd):
            IDC +=1
        else:
            IDC = 0
        DCi = round(cd * speed_mean/threshold_v * weight **(IDC - threshold_i_c), decimals)

        flagD = True

    elif msg.topic == AITEK_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                raise EmptyMessageException(topic=AITEK_TOPIC)
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
                D = json.loads(str(msg.payload.decode("utf-8")))
                vd = 1 if D['start'] else 0

        except Exception as exception:
            vd = 0
            print(exception)

    elif msg.topic == EMOJI_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                e = {"predominant" : "0","neutral":"0","happiness": "0","surprise":"0","sadness": "0","anger": "0","disgust": "0","fear": "0","engagement": "0","valence": "0"}
                raise EmptyMessageException(topic=EMOJI_TOPIC)

            if len(json.loads(str(msg.payload.decode("utf-8")))) == 0:
                e = {"predominant" : "0","neutral":"0","happiness": "0","surprise":"0","sadness": "0","anger": "0","disgust": "0","fear": "0","engagement": "0","valence": "0"}
                logger_client_error.warning({
                    'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
                    "topic": EMOJI_TOPIC,
                    "msg": 'NO emotion value'
                })
                print('NO emotion value')
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
                e = json.loads(str(msg.payload.decode("utf-8")))[user]
        except Exception as exception:
                print(exception)

        #flagE = True

        anger_buffer.pop(0)
        happiness_buffer.pop(0)
        fear_buffer.pop(0)
        sadness_buffer.pop(0)
        neutral_buffer.pop(0)
        disgust_buffer.pop(0)
        surprise_buffer.pop(0)

        anger_buffer.append(float(e['anger']))
        happiness_buffer.append(float(e['happiness']))
        fear_buffer.append(float(e['fear']))
        sadness_buffer.append(float(e['sadness']))
        neutral_buffer.append(float(e['neutral']))
        disgust_buffer.append(float(e['disgust']))
        surprise_buffer.append(float(e['surprise']))
        #emotions_total= Ei

    elif msg.topic == ARAUSAL_TOPIC:

        try:
            data = json.loads(str(msg.payload.decode("utf-8")))
            print(data)
            logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
            if len(str(msg.payload.decode('utf-8'))) == 0:
                logger_client_error.warning({
                    'timestamp_unibo': int(datetime.datetime.now().timestamp() * 1000),
                    "topic": "Arousal",
                    "msg": 'NO arousal value'
                })
                print('NO arousal value')
            elif "arousal" in data:
                arousal_value = data['arousal']
                if arousal_value < 0:
                    #case undefined value -> arousal value = -1
                    arousal_value = np.mean(arousal_buffer)

                arousal_buffer.pop(0)
                arousal_buffer.append(arousal_value)
                arousal = np.mean(arousal_buffer)
        except Exception as exception:
                print(exception)

    elif msg.topic == FTD_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                raise EmptyMessageException(topic=FTD_TOPIC)
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
                FTD = json.loads(str(msg.payload.decode("utf-8")))[user]['ftd']
        except Exception as exception:
            print(exception)

    elif msg.topic == RULEX_TOPIC:
        try:
            if len(str(msg.payload.decode('utf-8'))) == 0:
                raise EmptyMessageException(topic=RULEX_TOPIC)
            else:
                logTopic(msg.topic, json.loads(str(msg.payload.decode("utf-8"))))
        except Exception as exception:
            print(exception)

    if flagD: #flagE and flagD and flagV:

        anger = np.mean(anger_buffer)
        happiness = np.mean(happiness_buffer)
        fear = np.mean(fear_buffer)
        sadness = np.mean(sadness_buffer)
        neutral = np.mean(neutral_buffer)
        disgust = np.mean(disgust_buffer)
        surprise = np.mean(surprise_buffer)

        emotions = pd.Series([anger, happiness, fear, sadness, neutral, disgust, surprise])

        Ei =  round(((emotions * weights_emozioni).sum() / weights_emozioni.sum()) * arousal, decimals)

        if (vd):
            IDV +=1
        else:
            IDV = 0
        speed_mean = np.mean(speed_buffer)
        DVi = round(vd * speed_mean/threshold_v * weight **(IDV - threshold_i_v), decimals)

        print(f"DCi = {DCi}, DVi = {DVi}, Ei = {Ei}")

        ftd = {user:{
            'timestamp': timestamp_relab,
            'ftd' : max(0, 1 - (DCi + DVi + Ei))
            }}
        client.publish(FTD_TOPIC, json.dumps(ftd))

        msg = {
            'FTD': max(0, 1 - (DCi + DVi + Ei)),
            'cognitive distraction' : cd,
            'IDC': IDC,
            'visual distraction': vd,
            'IDV': IDV,
            'emotion': {
                    'anger': anger,
                    'happiness': happiness,
                    'fear': fear,
                    'sadness': sadness,
                    'neutral': neutral,
                    'disgust': disgust,
                    'surprise': surprise
            },
            'arousal': arousal,
        'speed': np.mean(speed_buffer)
        }

        logger_output.critical({
            'timestamp_relab': timestamp_relab, #ftd[user]['timestamp']
            'timestamp_unibo': datetime.datetime.now().timestamp() * 1000, #convert to milliseconds
            'FTD': max(0, 1 - (DCi + DVi + Ei)),
            'cognitive distraction' : cd,
            'IDC': IDC,
            'visual distraction': vd,
            'IDV': IDV,
            'emotion': {
                    'anger': anger,
                    'happiness': happiness,
                    'fear': fear,
                    'sadness': sadness,
                    'neutral': neutral,
                    'disgust': disgust,
                    'surprise': surprise
            },
            'arousal': arousal,
        'speed': np.mean(speed_buffer)
        })

        #flagE = False
        flagD = False
        #flagV = False
        #FTDs.append(max(0, 1 - (DCi + DVi + Ei)))
        print()
        print('FTD =', max(0, 1 - (DCi + DVi + Ei)))
        print()

        #TODO INSERT INTO DATABASE

def main():
    global user, logger_client_error, handler_client_error, logger_output, handler_output, logger_topic, handler_topic
    global UNITO_TOPIC, AITEK_TOPIC, ARAUSAL_TOPIC, FTD_TOPIC, EMOJI_TOPIC, RELAB_TOPIC, RULEX_TOPIC

    broker_name = None #'tools.lysis-iot.com'
    port = None #1883

    try:

        with open((os.path.dirname(os.path.abspath(__file__)) +'/config.json').replace ('\\', '/'),'r') as json_file:
            file = json.load(json_file)
            data = file['client_mqtt_config']
            print(data['broker_name'])
            broker_name = data['broker_name']
            print(data['port'])
            port = int(data['port'])
            user = file['person_config']
            print(user)
            logger_client_error, handler_client_error = setup_logger('main_logger', user+'_client.log')
            handler_client_error.setFormatter(json_formatter)
            logger_output, handler_output = setup_logger('output_logger', user+'_result.log')
            handler_output.setFormatter(json_formatter)
            logger_topic, handler_topic = setup_logger('topic_logger', user+'_topic_logger.log')
            handler_topic.setFormatter(json_formatter)

    except Exception as exception:
        print(exception)

    try:
        if broker_name is None:
            raise BrokerNameException
        elif port is None:
            raise PortNumberException

        client = paho.Client()
        client.on_subscribe = on_subscribe
        client.on_message = on_message
        client.connect(broker_name, port)
        client.subscribe(UNITO_TOPIC, qos=1)
        client.subscribe(EMOJI_TOPIC, qos=1)
        client.subscribe(AITEK_TOPIC, qos=1)
        client.subscribe(RELAB_TOPIC, qos=1)# Effective speed
        client.subscribe(FTD_TOPIC, qos=1)
        client.subscribe(ARAUSAL_TOPIC, qos=1)  # Arousal
        client.subscribe(RULEX_TOPIC, qos=1)  #TODO
        client.loop_forever()
    except Exception as exception:
        print('connect to client error')
        print(exception)

if __name__=="__main__":
    main()


import os
from time import time
import numpy as np
import pandas as pd
from optparse import OptionParser

from scripts.helper import load_from_json, write_to_json
from scripts.box import Box
from scripts.iou import IoU

def map_cats_to_scene_objects(df):
    cat_to_scene_objects = {}
    cats = df['mpcat40'].values
    scene_objects = df[['room_name', 'objectId']].apply(lambda x: '-'.join([x['room_name'], str(x['objectId'])]), axis=1).values
    for i, cat in enumerate(cats):
        if cat not in cat_to_scene_objects:
            cat_to_scene_objects[cat] = [scene_objects[i]]
        else:
            cat_to_scene_objects[cat].append(scene_objects[i])
    return cat_to_scene_objects

def map_cat_to_objects(scene, source_node):
    cat_to_objects = {}
    # map each category to its object id except for the source node
    for obj, node_info in scene.items():
        if obj != source_node:
            cat = node_info['category'][0]
            if cat not in cat_to_objects:
                cat_to_objects[cat] = [obj]
            else:
                cat_to_objects[cat].append(obj)
    return cat_to_objects

def RandomRank(query_info, model_names, scene_dir, mode, topk):
    # shuffle the objects in all scenes
    np.random.shuffle(model_names)

    # for each object sample context objects randomly from the object's scene
    num_subscenes = 0
    target_subscenes = []
    query_scene_name = query_info['example']['scene_name']
    num_context_objects = len(query_info['example']['context_objects'])
    model_idx = 0
    while num_subscenes < topk:
        model_name = model_names[model_idx]
        model_idx += 1
        target = model_name.split('-')[1].split('.')[0]
        scene_name = model_name.split('-')[0] + '.json'
        # exclude the scene if it is the query scene
        if query_scene_name == scene_name:
            continue
        num_subscenes += 1
        scene = load_from_json(os.path.join(scene_dir, mode, scene_name))
        all_except_target = [obj for obj in scene.keys() if obj != target]
        if num_context_objects <= len(all_except_target):
            sample_context = np.random.choice(all_except_target, num_context_objects, replace=False).tolist()
            num_sample = num_context_objects
        else:
            sample_context = all_except_target
            num_sample = len(sample_context)

        # assign a random correspondence between the sample and the query objects.
        correspondence = {}
        for i in range(num_sample):
            correspondence[sample_context[i]] = query_info['example']['context_objects'][i]

        subscene = {'scene_name': scene_name, 'target': target, 'correspondence': correspondence,
                    'context_match': len(correspondence)}
        target_subscenes.append(subscene)

    # sort the target subscenes based on the number of matching context objects
    target_subscenes = sorted(target_subscenes, reverse=True, key=lambda x: x['context_match'])

    return target_subscenes

def CatRank(query_info, query_mode, scene_dir, mode, cat_to_scene_objects, topk):
    # load the query scene and extract the category of the objects
    query_scene_name = query_info['example']['scene_name']
    query = query_info['example']['query']
    context_objects = query_info['example']['context_objects']
    query_scene = load_from_json(os.path.join(scene_dir, query_mode, query_scene_name))
    query_cat = query_scene[query]['category'][0]
    context_obj_to_cat = {obj: query_scene[obj]['category'][0] for obj in context_objects}

    # map the category of each context object to the objects having that category
    q_context_cat_to_objects = {}
    for obj, cat in context_obj_to_cat.items():
        if cat not in q_context_cat_to_objects:
            q_context_cat_to_objects[cat] = [obj]
        else:
            q_context_cat_to_objects[cat].append(obj)

    # for each target object that has the same category as query cat sample context objects
    target_objects = cat_to_scene_objects[query_cat]
    target_subscenes = []
    for target_object in target_objects:
        scene_name = target_object.split('-')[0] + '.json'
        # exclude the scene if it is the query scene
        if scene_name == query_scene_name:
            continue
        target_object = target_object.split('-')[-1]

        # load the scene and map its cats to the objects
        scene = load_from_json(os.path.join(scene_dir, mode, scene_name))
        t_cat_to_objects = map_cat_to_objects(scene, target_object)

        # for each context object in the query scene sample an object with the same category from the current scene.
        correspondence = {}
        for cat, objects in q_context_cat_to_objects.items():
            if cat in t_cat_to_objects:
                if len(objects) <= len(t_cat_to_objects[cat]):
                    sample = np.random.choice(t_cat_to_objects[cat], len(objects), replace=False).tolist()
                    num_sample = len(objects)
                else:
                    sample = t_cat_to_objects[cat]
                    num_sample = len(sample)

                # assign a random correspondence between the context objects in query and selected candidates in target.
                for i in range(num_sample):
                    correspondence[sample[i]] = objects[i]

        # populate the subscene attributes and save it.
        target_subscene = {'scene_name': scene_name, 'target': target_object, 'correspondence': correspondence,
                           'context_match': len(correspondence)}
        target_subscenes.append(target_subscene)

    # sort the target subscenes based on the number of matching context objects
    target_subscenes = sorted(target_subscenes, reverse=True, key=lambda x: x['context_match'])[:topk]

    return target_subscenes

def get_args():
    parser = OptionParser()
    parser.add_option('--mode', dest='mode', default='val', help='val|test')
    parser.add_option('--accepted_cats_path', default='../../data/matterport3d/accepted_cats_top10.json')
    parser.add_option('--metadata_path', default='../../data/matterport3d/metadata_non_equal_full_top10.csv')
    parser.add_option('--scene_dir', default='../../results/matterport3d/scenes_top10')
    parser.add_option('--query_dir', default='../../queries/matterport3d/')
    parser.add_option('--query_input_file_name', default='query_dict_val_top10.json')
    parser.add_option('--results_dir', default='../../results/matterport3d')
    parser.add_option('--model_name', dest='model_name', default='CatRank', help='RandomRank|CatRank')
    (options, args) = parser.parse_args()
    return options

def main():
    # get the arguments
    args = get_args()

    # define initial parameters
    query_dict_input_path = os.path.join(args.query_dir, args.query_input_file_name)
    results_dir = os.path.join(args.results_dir, args.model_name)
    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    query_dict_output_path = os.path.join(results_dir, 'query_dict_{}_top10_{}.json'.format(args.mode, args.model_name))
    accepted_cats = set(load_from_json(args.accepted_cats_path))
    topk = 100

    # read the query dict and the metadata
    query_dict = load_from_json(query_dict_input_path)
    df_metadata = pd.read_csv(args.metadata_path)
    df_metadata = df_metadata[df_metadata['split'] == args.mode]

    # filter the metadata to only include objects with accepted category
    cat_is_accepted = df_metadata['mpcat40'].apply(lambda x: x in accepted_cats)
    df_metadata = df_metadata.loc[cat_is_accepted]

    # map the categories to scene objects if necessary
    if args.model_name == 'RandomRank':
        model_names = df_metadata[['room_name', 'objectId']].apply(lambda x: '-'.join([x['room_name'],
                                                                                str(x['objectId'])]), axis=1)
        model_names = model_names.values
        for query, query_info in query_dict.items():
            target_subscenes = RandomRank(query_info, model_names, args.scene_dir, args.mode, topk)
            query_info['target_subscenes'] = target_subscenes

    elif args.model_name == 'CatRank':
        cat_to_scene_objects = map_cats_to_scene_objects(df_metadata)
        for query, query_info in query_dict.items():
            target_subscenes = CatRank(query_info, args.mode, args.scene_dir, args.mode, cat_to_scene_objects, topk)
            query_info['target_subscenes'] = target_subscenes

    else:
        raise Exception('Model not defined')

    # test if for each query the query node is not in the context nodes
    for query, query_info in query_dict.items():
        for target_subscene in query_info['target_subscenes']:
            t = target_subscene['target']
            context_objects = target_subscene['correspondence'].keys()
            if t in context_objects:
                raise Exception('Target node was included in the context objects.')

    # save the changes to query dict
    write_to_json(query_dict, query_dict_output_path)

if __name__ == '__main__':
    t = time()
    main()
    duration = time() - t
    print('Running model took {} minutes'.format(round(duration / 60, 2)))


from collections import defaultdict
def oddString(words)->str:
    l = [[ord(c) for c in word] for word in words]
    d = defaultdict(int)

    for x in l:
        diff = []
        for i in range(len(x) - 1):
            diff += [x[i+1]] - [x[i]]
            d[tuple(diff)] += 1

    for j,x in enumerate(l):
        dif = []
        for i in range(len(x) - 1):
            dif += [x[i+1]] - [x[i]]
            if d[tuple(dif)] == 1:
                return words[j]


def notas(*n, sit=False):
    """
    -> Funcao para analisar notas e situaçoes da varios alunos
    :param n: Uma ou mais notas dos alunos (aceita varias).
    :param sit: valor opcional, indicando se deve ou nao a situaçao.
    :return: Dicionário com várias informaçoes sobre a situaçao da turma.
    """
    tot = dict()
    tot['total'] = len(n)
    tot['maior'] = max(n)
    tot['menor'] = min(n)
    tot['media'] = sum(n) / len(n)
    if sit:
        if tot['media'] >= 7:
            tot['sit'] = 'boa'
        elif tot['media'] >= 5:
            tot['sit'] = 'razoavel'
        else:
            tot['sit'] = 'ruim'
    return tot

# Programa principal
resp = notas(5.5, 2.5, 8.5)
help(notas)


import math
import cmath
import numpy as np
import scipy.integrate as integrate
from pylab import *

#defining functions
pi=math.pi
def e(x):
	return exp(x)
def cosine(x):
	return cos(cos(x))

#1
data=np.arange(-2*pi,4*pi,0.1)
data1=np.arange(0,2*pi,0.1)
figure(1)
xlim(-2*pi,4*pi)
semilogy(data,e(data),label='Plot of exponential function')
legend()
semilogy(data,np.concatenate((e(data1),e(data1),e(data1))),label='Expected function generated by Fourier series')
legend()
grid(True)

figure(2)
semilogy(data,cosine(data),label='Plot of cosine function')
legend()
grid(True)

#2

def u(x,k,f):
	return f(x)*cos(k*x)
def v(x,k,f):
	return f(x)*sin(k*x)
coeffea=np.zeros((26,1))
coeffeb=np.zeros((25,1))
coeffca=np.zeros((26,1))
coeffcb=np.zeros((25,1))
coeffe=np.zeros((51,1,))
coeffc=np.zeros((51,1))
na=np.zeros((26,1))
na[0]=0
nb=np.zeros((25,1))
n=np.zeros((51,1))
coeffea[0]=(integrate.quad(u,0,2*pi,args=(0,e)))[0]/(2*pi)
coeffca[0]=(integrate.quad(u,0,2*pi,args=(0,cosine)))[0]/(2*pi)
coeffe[0]=coeffea[0]
coeffc[0]=coeffca[0]
for i in range(1,25):
	coeffea[i]=abs((integrate.quad(u,0,2*pi,args=(i,e)))[0]/pi)
	coeffeb[i-1]=abs((integrate.quad(v,0,2*pi,args=(i,e)))[0]/pi)
	coeffca[i]=abs((integrate.quad(u,0,2*pi,args=(i,cosine)))[0]/pi)
	coeffcb[i-1]=abs((integrate.quad(v,0,2*pi,args=(i,cosine)))[0]/pi)
	na[i]=i
	nb[i-1]=i
	coeffe[2*i-1]=coeffea[i]
	coeffe[2*i+1]=coeffeb[i-1]
	coeffc[2*i-1]=coeffca[i]
	coeffc[2*i+1]=coeffcb[i-1]
	n[2*i-1]=i
	n[2*i]=i
na[25]=25
nb[24]=25
n[50]=25
n[49]=25

figure(3)
semilogy(na,coeffea,linewidth=0.0,marker='o',color='r',label='Semilogy plot of "a" coefficients : e(x)')
legend()
semilogy(nb,coeffeb,linewidth=0.0,marker='o',color='g',label='Semilogy plot of "b" coefficients: e(x)')
legend()
grid(True)

figure(4)
loglog(na,coeffea,linewidth=0.0,marker='o',color='r',label='Loglog plot of "a" coefficients: e(x)')
legend()
loglog(nb,coeffeb,linewidth=0.0,marker='o',color='g',label='Loglog plot of "b" coefficients: e(x)')
legend()
grid(True)

figure(5)
semilogy(na,coeffca,linewidth=0.0,marker='o',color='r',label='Semilogy plot of "a" coefficients: cos(cos(x))')
legend()
semilogy(nb,coeffcb,linewidth=0.0,marker='o',color='g',label='Semilogy plot of "b" coefficients: cos(cos(x))')
legend()
grid(True)

figure(6)
loglog(na,coeffca,linewidth=0.0,marker='o',color='r',label='Loglog plot of "a" coefficients: cos(cos(x))')
loglog(nb,coeffcb,linewidth=0.0,marker='o',color='g',label='Loglog plot of "b" coefficients: cos(cos(x))')
legend()
grid(True)

#4
x=linspace(0,2*pi,401)
x=x[:-1]
A=zeros((400,51))
A[:,0]=1
for k in range(1,26):
	A[:,2*k-1]=cos(k*x)
	A[:,2*k]=sin(k*x)

#5
c1=lstsq(A,e(x),rcond=-1)[0]
c2=lstsq(A,cos(cos(x)),rcond=-1)[0]

figure(3)
semilogy(n,c1,linewidth=0.0,marker='o',color='y',label='Semilogy plot of coefficients computed by least squares: e(x)')
legend()
grid(True)

figure(4)
loglog(n,c1,linewidth=0.0,marker='o',color='y',label='Loglog plot of coefficients computed by least squares: e(x)')
legend()
grid(True)

figure(5)
semilogy(n,c2,linewidth=0.0,marker='o',color='y',label='Semilogy plot of coefficients computed by least squares: cos(cos(x))')
legend()
grid(True)

figure(6)
loglog(n,c2,linewidth=0.0,marker='o',color='y',label='Loglog plot of coefficients computed by least squares: cos(cos(x))')
legend()
grid(True)
#6
diffe=abs(c1-coeffe)
diffc=abs(c2-coeffc)
maxe=np.max(diffe)
maxc=np.max(diffc)

print("Maximum error between the two methods:")
print("e(x)",maxe)
print("cos(cos(x))",maxc)

#7
figure(1)
semilogy(x,np.dot(A,c1),linewidth=0.0,marker='o',color='g',label='Estimates for e(x)')
legend()
grid(True)

figure(2)
semilogy(x,np.dot(A,c2),linewidth=0.0,marker='o',color='g',label='Estimates for cos(cos(x))')
legend()
grid(True)

show()


#!/usr/bin/python

import sys

def dump_data(out_stream, data):
	while data:
		line = "\t" + ",".join(map(lambda x: 'x"{:02X}"'.format(x), data[:16]))
		data = data[16:]
		out_stream.write(line + (",\n" if data else "\n"))

def doit(arg, in_stream, out_stream):
	files = {}
	ID_STR = "---INCLUDE-BIN:"
	for a in arg:
		with open(a, "rb") as f:
			files[a] = bytearray(f.read())
	in_context = ""
	lineno = 0
	for line in in_stream:
		lineno += 1
		if ID_STR in line:
			a = line.split(ID_STR, 1)[1].split(":")
			if in_context:
				if len(a) < 2:
					return "Syntax error (too few include elements) in line " + str(lineno)
				if a[0] != "STOP":
					return "Waiting for STOP in context of " + in_context + " but got \"" + ":".join(a) + "\" in line " + str(lineno)
				if a[1] != in_context:
					return "Waiting for STOP of " + in_context + " but got \"" + ":".join(a) + "\" in line " + str(lineno)
				if files:
					dump_data(out_stream, files[in_context])
				else:
					out_stream.write("-- DATA IS STRIPPED OUT\n")
				out_stream.write(line)
				in_context = ""
			else:
				if len(a) < 3:
					return "Syntax error (too few include elements) in line " + str(lineno)
				if a[0] != "START":
					return "Waiting for START but got " + a[1] + " in line " + str(lineno)
				if files and a[1] not in files:
					return "Unknown file to include \"" + a[1] + "\" in line " + str(lineno)
				in_context = a[1]
				if files:
					try:
						size = int(a[2])
					except:
						return "Wrong size element \"" + a[2] + "\" in line " + str(lineno)
					if size != len(files[in_context]):
						return "Size mismatch, requested size is " + str(size) + " file size is " + len(files[in_context]) + " in line " + str(lineno)
				out_stream.write(line)
		elif not in_context:
			out_stream.write(line)
	if in_context:
		return "Include context for \"" + in_context + "\" has not been closed, but end of file"
	return True

if __name__ == "__main__":
	ret = doit(sys.argv[1:], sys.stdin, sys.stdout)
	if ret is True:
		sys.exit(0)
	else:
		sys.stderr.write("ERROR: " + str(ret) + "\n")
		sys.exit(1)


import numpy as np
from ipyatom.render_ivol import _get_cylinder_mesh, create_ivol
from jsonextended import edict

def test_get_cylinder_mesh():
    p1 = [0.8452500000000001, -0.45025399999999993, 2.3686249999999993]
    p2 = [-2.5357499999999997, -0.45025399999999993, 2.3686249999999993]
    radius = 0.1
    segments = 50
    mesh_pts = _get_cylinder_mesh(p1, p2, radius, segments)
    assert np.count_nonzero(~np.isnan(mesh_pts))

def test_render_atoms_simple():
    dstruct = {
        'type': 'repeat_density',
        'dtype': 'charge',
        'name': '',
        'dcube': np.ones((3, 3, 3)),
        'centre': [0, 0, 0],
        'cell_vectors': {
            'a': [2., 0, 0],
            'b': [0, 2., 0],
            'c': [0, 0, 2.]},
        'color_bbox': 'black',
        'transforms': []
    }
    cstruct = {
        'type': 'repeat_cell',
        'name': '',
        'centre': [0, 0, 0],
        'cell_vectors': {
            'a': [2., 0, 0],
            'b': [0, 2., 0],
            'c': [0, 0, 2.]},
        'color_bbox': 'black',
        'sites': [{
            'label': "Fe",
            'ccoord': [1, 1, 1],
            'color_fill': "red",
            'color_outline': None,
            'transparency': 1,
            'radius': 1,
        }],
        'bonds': [],
        'transforms': []
    }
    vstruct = {"elements": [dstruct, cstruct], "transforms": []}
    new_struct, fig, controls = create_ivol(vstruct)

    output = edict.apply(edict.filter_keys(new_struct, ["ivol"], list_of_dicts=True),
                         "ivol", lambda x: [v.__class__.__name__ for v in x], list_of_dicts=True)
    expected = {'elements': [{'ivol': ['Figure', 'Mesh']}, {'ivol': ['Mesh', 'Scatter']}]}

    assert output == expected


from collections import deque
import sys

input = sys.stdin.readline

def sol():
    n, m = map(int, input().split())
    farm = [[0] * (n + 1) for i in range(n + 1)]
    visited = [[0] * (n + 1) for i in range(n + 1)]
    farm[1][1] = 1
    visited[1][1] = 1
    switch = {}

    for _ in range(m):
        i, j, k, l = map(int, input().split())

        if (i, j) in switch:
            switch[(i, j)].append((k, l))
        else:
            switch[(i, j)] = [(k, l)]

    q = deque()
    q.append((1, 1))

    ans = 0

    while q:
        now = q.popleft()
        ans += 1
        if now in switch:
            for i in switch[now]:
                if farm[i[0]][i[1]] == 0:
                    farm[i[0]][i[1]] = 1
            bfs(farm, visited, n, q)
        else:
            continue

    print(ans)

def bfs(farm, visited, n, queue):
    q = deque()
    dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    q.append((1, 1))
    tmpvisited=set()

    while q:
        now = q.popleft()
        for dir in dirs:
            move = (now[0] + dir[0], now[1] + dir[1])
            if move[0] <= 0 or move[0] > n or move[1] <= 0 or move[1] > n:
                continue
            if farm[move[0]][move[1]] == 0:
                continue
            if visited[move[0]][move[1]] == 0:
                visited[move[0]][move[1]] = 1
                queue.append((move[0], move[1]))
            if move not in tmpvisited:
                tmpvisited.add(move)
                q.append(move)

sol()

코딩테스트_파이썬/백준/불켜기.py


# PRIME NUMBERS
def is_prime(x):
    for i in range(2, x//2 + 1):
        if x % i == 0:
            return False
    return True

prime = []

# for i in range(2, 101):
#         if is_prime(i):
#             prime.append(i)

prime = [i for i in range(2, 101) if is_prime(i)]
prime_filtered = list(filter(is_prime, range(2, 101)))
if __name__ == '__main__':
    print('using list comprehension: ', prime)
    print('using filter:             ', prime_filtered)

# NAMES WITH A
def is_a(name):
    if name[0].lower() == 'a':
        return True
    return False

names = ['Albert', 'betty', 'Anna', 'andrew', 'Evie', 'harrison', 'Daniel', 'avani']
starts_with_a = [name for name in names if is_a(name)]
starts_with_a_but_filter = list(filter(lambda name: name[0].lower() == 'a', names))
if __name__ == '__main__':
    print('using list comprehension: ', starts_with_a)
    print('using filter:             ', starts_with_a_but_filter)


from django.shortcuts import render, redirect
from .forms import MovieForm
from .models import Movie

# Create your views here.
def index(request):
    movie_lists = Movie.objects.all()
    context = {
        'movie_lists': movie_lists,
    }
    return render(request, 'movies/index.html', context)

def detail(request, pk):
    movie_detail = Movie.objects.get(id=pk)
    context = {
        'movie_detail':movie_detail,
    }
    return render(request, 'movies/detail.html', context)

def create(request):
    if request.method == 'POST':
        form = MovieForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('movies:index')
    else:
        form = MovieForm()
    context = {
        'form': form,
    }
    return render(request, 'movies/create.html', context)

def delete(request, pk):
    Movie.objects.get(id=pk).delete()
    return redirect('movies:index')

def update(request, pk):
    update_data = Movie.objects.get(id=pk)
    if request.method == 'POST':
        update_movie = MovieForm(request.POST, instance=update_data)
        if update_movie.is_valid():
            update_movie.save()
            return redirect('movies:detail', pk)
    else:
        update_movie = MovieForm(instance=update_data)

    context = {
        'update_movie':update_movie,
    }
    return render(request, 'movies/update.html', context)


# Given an integer array nums sorted in non-decreasing order,
# return an array of the squares of each number sorted in non-decreasing order.
# Example 1:
# Input: nums = [-4,-1,0,3,10]
# Output: [0,1,9,16,100]
# Explanation: After squaring, the array becomes [16,1,0,9,100].
# After sorting, it becomes [0,1,9,16,100].

# Example 2:
# Input: nums = [-7,-3,2,3,11]
# Output: [4,9,9,49,121]

# approach: the max will only be on the left or on the right,
# so create a new array, and starting to fill in number from the right -> left (largest -> smallest)
# everytime we only need to compare left and right value of the original array and fill in the new array with the bigger num

class Solution(object):
    def sortedSquares(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        reorder = [0]*len(nums)

        left, right, k = 0, len(nums) - 1, len(nums) - 1
        while k >= 0:
            if nums[left] ** 2 < nums[right] ** 2:
                reorder[k] = nums[right] ** 2
                k -= 1
                right -= 1
            else:
                reorder[k] = nums[left] ** 2
                k -= 1
                left += 1

        return reorder


import time
import requests
from bs4 import BeautifulSoup
import re

def headers():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}
    return headers

def getData(url):
    response = requests.get(url, headers=headers())
    time.sleep(3)
    return response

def soupData(response):
    soup = BeautifulSoup(response.text, 'lxml')
    return soup

def main(url):
    response = getData(url)

    soup = soupData(response)
    print(soup)
    return str(soup)

if __name__ == '__main__':
    url = 'https://www.bbc.co.uk/news/uk-62345010'
    l = re.findall(r'eq5iqo00">(.*?)</p>', main(url), re.S)
    main(url)
    for i in l:
        print(i)


import torch
from torch import nn
from d2l import torch as d2l
import os
import pandas as pd
import numpy as np
from torch import optim
from sklearn.mixture import GaussianMixture
torch.cuda.empty_cache()
from tqdm import tqdm
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
import torch
import torch.utils.data
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torchvision import datasets, transforms
from torchvision.utils import make_grid , save_image
from sklearn import metrics
from transformers import BertTokenizer
from transformers import BertModel
from torch.optim import Adam

# load and clean dataset
def load_clean_data(realFile, fakeFile):
    df_politiReal = pd.read_csv(realFile)
    df_politiFake = pd.read_csv(fakeFile)

    # data cleaning
    df_politiReal = df_politiReal[~df_politiReal.full_text.isna()]
    df_politiFake = df_politiFake[~df_politiFake.full_text.isna()]
    df_politiFake.full_text = df_politiFake.full_text.str.replace("\n{1,}","").str.replace("(http.+)\s{0,}","").to_list()
    df_politiReal.full_text = df_politiReal.full_text.str.replace("\n{1,}","").str.replace("(http.+)\s{0,}","").to_list()
    df_politiReal['label'] = 0
    df_politiFake['label'] = 1

    # combine real data and fake data
    df_politi = pd.concat([df_politiReal.loc[:,['label','full_text']], df_politiFake.loc[:,['label','full_text']]])
    df_cleaned = df_politi

    return df_cleaned

# split the dataset into train, test and validation
def split_data(df_cleaned):
    df_negative = df_cleaned[df_cleaned.label==0]
    df_nonNegative = df_cleaned[df_cleaned.label!=0]

    df_train_neg, df_val_neg, df_test_neg = np.split(df_negative.sample(frac=1, random_state=42),
                        [int(.8*len(df_negative)), int(.9*len(df_negative))])
    df_train_nonNeg, df_val_nonNeg, df_test_nonNeg = np.split(df_nonNegative.sample(frac=1, random_state=42),
                        [int(.8*len(df_nonNegative)), int(.9*len(df_nonNegative))])

    df_train = pd.concat([df_train_neg, df_train_nonNeg])
    df_val = pd.concat([df_val_neg, df_val_nonNeg])
    df_test = pd.concat([df_test_neg, df_test_nonNeg])

    return df_train, df_val, df_test

# data preprocessing for TextCNN model: tokenization
def load_data(df_train, df_val, df_test, batch_size, num_steps=500, shuffle=False):

    train_data = (df_train.full_text.to_list(), df_train.label.to_list())
    val_data = (df_val.full_text.to_list(), df_val.label.to_list())
    test_data = (df_test.full_text.to_list(), df_test.label.to_list())
    if shuffle:
        train_data = (df_train.full_text.to_list(), df_train.label.sample(frac=1,replace=False).to_list())
        val_data = (df_val.full_text.to_list(), df_val.label.sample(frac=1,replace=False).to_list())
        test_data = (df_test.full_text.to_list(), df_test.label.sample(frac=1,replace=False).to_list())

    train_tokens = d2l.tokenize(train_data[0], token='word')
    val_tokens = d2l.tokenize(val_data[0], token='word')
    test_tokens = d2l.tokenize(test_data[0], token='word')

    vocab = d2l.Vocab(train_tokens, min_freq=1)
    train_features = torch.tensor([d2l.truncate_pad(
        vocab[line], num_steps, vocab['<pad>']) for line in train_tokens])
    val_features = torch.tensor([d2l.truncate_pad(
        vocab[line], num_steps, vocab['<pad>']) for line in val_tokens])
    test_features = torch.tensor([d2l.truncate_pad(
        vocab[line], num_steps, vocab['<pad>']) for line in test_tokens])

    train_iter = d2l.load_array((train_features, torch.tensor(train_data[1])),
                                batch_size)
    val_iter = d2l.load_array((val_features, torch.tensor(val_data[1])),
                                batch_size)
    test_iter = d2l.load_array((test_features, torch.tensor(test_data[1])),
                               batch_size,
                               is_train=False)

    return train_iter, val_iter, test_iter, vocab

# embedding the tokenized data
class TokenEmbedding:

    def __init__(self, embedding_name):
        self.idx_to_token, self.idx_to_vec = self._load_embedding(
            embedding_name)
        self.unknown_idx = 0
        self.token_to_idx = {token: idx for idx, token in
                             enumerate(self.idx_to_token)}

    def _load_embedding(self, embedding_name):
        idx_to_token, idx_to_vec = ['<unk>'], []
        data_dir = d2l.download_extract(embedding_name)
        with open(os.path.join(data_dir, 'vec.txt'), 'r', encoding='utf-8') as f:
            for line in f:
                elems = line.rstrip().split(' ')
                token, elems = elems[0], [float(elem) for elem in elems[1:]]
                # Skip header information, such as the top row in fastText
                if len(elems) > 1:
                    idx_to_token.append(token)
                    idx_to_vec.append(elems)
        idx_to_vec = [[0] * len(idx_to_vec[0])] + idx_to_vec
        return idx_to_token, torch.tensor(idx_to_vec)

    def __getitem__(self, tokens):
        indices = [self.token_to_idx.get(token, self.unknown_idx)
                   for token in tokens]
        vecs = self.idx_to_vec[torch.tensor(indices)]
        return vecs

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

# TextCNN model
class TextCNN(nn.Module):
    def __init__(self, vocab_size, embed_size, kernel_sizes, num_channels,
                 **kwargs):
        super(TextCNN, self).__init__(**kwargs)
        self.embedding = nn.Embedding(vocab_size, embed_size)
        # The embedding layer not to be trained
        self.constant_embedding = nn.Embedding(vocab_size, embed_size)
        self.dropout = nn.Dropout(0.5)
        self.decoder = nn.Linear(sum(num_channels), 2)
        # The max-over-time pooling layer has no parameters, so this instance
        # can be shared
        self.pool = nn.AdaptiveAvgPool1d(1)
        self.relu = nn.ReLU()
        # Create multiple one-dimensional convolutional layers
        self.convs = nn.ModuleList()
        for c, k in zip(num_channels, kernel_sizes):
            self.convs.append(nn.Conv1d(2 * embed_size, c, k))

    def forward(self, inputs):
        # Concatenate two embedding layer outputs with shape (batch size, no.
        # of tokens, token vector dimension) along vectors
        embeddings = torch.cat(
            (self.embedding(inputs), self.constant_embedding(inputs)), dim=2)
        # Per the input format of one-dimensional convolutional layers,
        # rearrange the tensor so that the second dimension stores channels
        embeddings = embeddings.permute(0, 2, 1)
        # For each one-dimensional convolutional layer, after max-over-time
        # pooling, a tensor of shape (batch size, no. of channels, 1) is
        # obtained. Remove the last dimension and concatenate along channels
        encoding = torch.cat([
            torch.squeeze(self.relu(self.pool(conv(embeddings))), dim=-1)
            for conv in self.convs], dim=1)
        outputs = self.decoder(self.dropout(encoding))
        return outputs

# initializing model
def init_weights(m):
    if type(m) in (nn.Linear, nn.Conv1d):
        nn.init.xavier_uniform_(m.weight)

# training and validating model
def train_valid_TextCNN(df_train, df_val, df_test, get_model=False):
    # load data loader and vocabulary
    train_iter, val_iter, _, vocab = load_data(df_train, df_val, df_test,batch_size=64)

    # model initialization
    embed_size, kernel_sizes, nums_channels = 100, [3, 4, 5], [100, 100, 100]
    devices = d2l.try_all_gpus()
    net = TextCNN(len(vocab), embed_size, kernel_sizes, nums_channels)
    net.apply(init_weights)

    # use pre-trained GloVe as the embedding layer
    glove_embedding = TokenEmbedding('glove.6b.100d')
    embeds = glove_embedding[vocab.idx_to_token]
    net.embedding.weight.data.copy_(embeds)
    net.constant_embedding.weight.data.copy_(embeds)
    net.constant_embedding.weight.requires_grad = False

    # training and testing model
    lr, num_epochs = 0.001, 50
    optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    loss = nn.CrossEntropyLoss(reduction="none")
    d2l.train_ch13(net, train_iter, val_iter, loss, optimizer, num_epochs, devices)
    if get_model:
        return net

def train_valid_TextCNN_withRandomLabeled(df_train, df_val, df_test):
    # load data loader and vocabulary
    train_iter, val_iter, _, vocab = load_data(df_train, df_val, df_test,batch_size=64, shuffle=True)

    # model initialization
    embed_size, kernel_sizes, nums_channels = 100, [3, 4, 5], [100, 100, 100]
    devices = d2l.try_all_gpus()
    net = TextCNN(len(vocab), embed_size, kernel_sizes, nums_channels)
    net.apply(init_weights)

    # use pre-trained GloVe as the embedding layer
    glove_embedding = TokenEmbedding('glove.6b.100d')
    embeds = glove_embedding[vocab.idx_to_token]
    net.embedding.weight.data.copy_(embeds)
    net.constant_embedding.weight.data.copy_(embeds)
    net.constant_embedding.weight.requires_grad = False

    # training and testing model
    lr, num_epochs = 0.001, 50
    optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    loss = nn.CrossEntropyLoss(reduction="none")
    d2l.train_ch13(net, train_iter, val_iter, loss, optimizer, num_epochs, devices)

# evaluate model on test data
def get_metrics(net, df_test):
    test = Dataset(df_test)

    test_dataloader = torch.utils.data.DataLoader(test, batch_size=2)

    test_prob = None
    test_y = None

    net = net.cpu()
    with torch.no_grad():

      for test_input, test_label in tqdm(test_dataloader):

        test_label = test_label
        mask = test_input['attention_mask']
        input_id = test_input['input_ids'].squeeze(1)
        output = net(input_id, mask)

        if test_prob is None:
            test_prob = torch.softmax(input=output.data,dim=1)
        else:
            test_prob = torch.vstack([test_prob,torch.softmax(input=output.data,dim=1)])
        if test_y is None:
            test_y  = test_label
        else:
            test_y = torch.hstack([test_y,test_label])

    pred_y = test_prob.argmax(dim=1)

    fpr, tpr, thresholds = metrics.roc_curve(test_y, test_prob[:,1], pos_label=1)
    # get the classification report
    print(metrics.classification_report(test_y,pred_y))
    # get the ROC plot and AUC score
    auc = metrics.roc_auc_score(test_y, test_prob[:,1])
    plt.plot([0,1],[0,1])
    plt.plot(fpr,tpr,label='AUC:{}'.format(np.round(auc,3)))
    plt.title('ROC: TextCNN')
    plt.xlabel('TPR')
    plt.ylabel('FPR')
    plt.legend()


# 좌표 압축
n = int(input())
x = list(map(int, input().split()))

x_ = sorted(list(set(x)))
# 이 상태에서 바로 index로 찾으면 매번 O(n)의 시간이 걸림
# dict로 한 번 변환하면 dict에서 O(1)의 시간으로 찾음
x_dict = {x_[i]: i for i in range(len(x_))}
for i in x:
    print(x_dict[i], end=" ")


from django.contrib import admin
from .models import Product, ProductVariant, Topping, Order, OrderItem
from django.utils.html import format_html_join, format_html

# Register your models here.
class ProductVariantInline(admin.TabularInline):
    model = ProductVariant
    extra = 0

class ProductAdmin(admin.ModelAdmin):
    inlines = [
        ProductVariantInline,
    ]

class OrderItemInline(admin.TabularInline):
    readonly_fields = ["item", "quantity", "toppings", "addons"]
    model = OrderItem
    extra = 0

    def toppings(self, instance):
        return format_html_join(
            "{},", (topping.name for topping in list(instance.toppings.all()))
        )

    def addons(self, instance):
        return format_html_join(
            "{},", (addon.variant for addon in list(instance.addons.all()))
        )

class OrderAdmin(admin.ModelAdmin):
    readonly_fields = ["id", "user", "ordered_date"]
    inlines = [
        OrderItemInline,
    ]

    actions = ["make_pendding", "make_done"]

    def make_pendding(self, request, queryset):
        rows_updated = queryset.update(status="p")
        if rows_updated == 1:
            message_bit = "1 order was"
        else:
            message_bit = "%s orders were" % rows_updated
        self.message_user(request, "%s successfully marked as published." % message_bit)

    def make_done(self, request, queryset):
        rows_updated = queryset.update(status="p")
        if rows_updated == 1:
            message_bit = "1 order was"
        else:
            message_bit = "%s orders were" % rows_updated
        self.message_user(request, "%s successfully marked as published." % message_bit)

    make_pendding.short_description = "Mark selected orders as pending"
    make_done.short_description = "Mark selected orders as done"

admin.site.register(Product, ProductAdmin)
admin.site.register(ProductVariant)
admin.site.register(Topping)
admin.site.register(Order, OrderAdmin)


import pandas as pd
import numpy as np
from time import time
import logging
logging.basicConfig(format="%(levelname)s - %(asctime)s: %(message)s", datefmt= '%H:%M:%S', level=logging.INFO)
from ast import literal_eval
import multiprocessing
from gensim.models import Word2Vec

# change path to location where data is stored
DATAPATH = '../data/'
RELMODELPATH = 'streamlitapp/models/'

print('loading data')
df = pd.read_csv(DATAPATH+'newcleaned_trainfile.csv')
df.ingredients = [literal_eval(x) for x in df.ingredients]
df = df.drop(columns = ['Unnamed: 0'])

df['titlelist'] = [str(row).split() for row in df.title.values]
df['words'] = df.ingredients + df.titlelist
print('data loaded')

cores = multiprocessing.cpu_count() # Count the number of cores in a computer
w2v_model = Word2Vec(min_count=200,
                     window=10,
                     vector_size=3000,
                     sample=6e-5,
                     alpha=0.03,
                     min_alpha=0.0007,
                     negative=20,
                     workers=cores-1,
                     epochs=30)
t = time()
w2v_model.build_vocab(df.words, progress_per=10000)
print('Time to build vocab: {} mins'.format(round((time() - t) / 60, 2)))

# Train W2V model (~6 min)
t = time()
w2v_model.train(df.words, total_examples=w2v_model.corpus_count, epochs=30, report_delay=1)
print('Time to train the model: {} mins'.format(round((time() - t) / 60, 2)))

# save model
w2v_model.save(RELMODELPATH+'new_word_embedding_model.model')
print('model trained and saved to', RELMODELPATH+'new_word_embedding_model.model')


from django.shortcuts import render
from django.views import generic
from django.http import Http404, HttpResponse, StreamingHttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.db.models import Q

from .models import Winemag

def IndexView(request):
	text = request.GET.get('text')
	field = request.GET.get('field')

	if(text and field):
		if(field == 'country'):
			return render(request, 'earth/index.html', {
				'list_data': Winemag.objects.filter(country = text)
			})
		elif(field == 'province'):
			return render(request, 'earth/index.html', {
				'list_data': Winemag.objects.filter(province = text)
			})
		elif(field == 'region'):
			results = Winemag.objects.filter(Q(region_1 = text) | Q(region_2 = text))
			return render(request, 'earth/index.html', {
				'list_data': results
			})
	else:
		return render(request, 'earth/index.html', {
			'list_data': Winemag.objects.all()
		})

def OneWineList(request):

	wine_type = request.GET.get('wine_type')
	sort = request.GET.get('sort')
	grape = request.GET.get('grape')
	if(wine_type):
		try:
			results = Winemag.objects.filter(winery = wine_type)
		except (KeyError, Winemag.DoesNotExist):
			return render(request, 'earth/oneWine.html', {
				'wine_type': wine_type,
				'error_message': 'Wine Not Available'
			})
		else:
			if(sort == 'asc'):
				results = results.order_by('price')
			elif(sort == 'desc'):
				results = results.order_by('-price')
			return render(request, 'earth/oneWine.html', {
				'wine_type': wine_type,
				'results': results
			})
	else:
		if(grape):
			return render(request, 'earth/wineList.html', {
				'all_wines': Winemag.objects.filter(variety = grape).values('winery').distinct()
			})
		return render(request, 'earth/wineList.html', {
			'all_wines': Winemag.objects.values('winery').distinct()
		})


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

app = Flask(__name__)
cors = CORS(app)
# CORS(app,resources={r"/*": {"origins": "*"}})
app.config["CACHE_TYPE"] = "null"

@app.route('/')
def hello_world():
    return render_template("index.html")
def fix_json(text):
    return text.replace("\'", "\"")

@app.route('/getbydate/<year>/<quar>/<origin>', methods=["POST", "GET"])
def getbydate(year,quar,origin):
    data = pd.read_csv("revised_flight_latlng.csv")
    dataAll = data[["Year", "Quarter","Origin","Fare","Origin Lat","Origin Lng","Destination Lat","Destination Lng"]]
    # print(dataAll)
    qury = str('Year=='+ year+ ' and Quarter=="'+ quar + '" and Origin=="'+ origin+'"')
    # qury = 'Year == '+ year
    dataAll.query(qury, inplace=True)
    return data[["Origin Lat","Origin Lng","Destination Lat","Destination Lng"]].to_json()
    # return dataAll.to_json()
    # return "Year"

@app.route('/get_origins', methods=["POST", "GET"])
def get_origins():
    data = pd.read_csv("revised_flight_latlng.csv")
    data = data["Origin"].unique()
    print(list(data))
    for i in list(data):
        print('<option value="'+i+'">'+i+'</option>')
    return jsonify(list(data))
    # return jsonify(data)

def getFilteredDF(jsonFromAjax):
    data = pd.read_csv("revised_flight_latlng.csv")
    dataAll = data[["Year", "Quarter","Origin","Destination","Fare","Origin Lat","Origin Lng","Destination Lat","Destination Lng"]]

    newData = data[ ["Year","Quarter","Origin","Destination","Fare","Origin Lat","Origin Lng","Destination Lat", "Destination Lng"] ]
    sortByYear = newData.loc[newData['Year'] == int(jsonFromAjax["year"]) ]
    sortByQuarter = sortByYear.loc[sortByYear['Quarter'] == int(jsonFromAjax["quarter"]) ]
    sortByOrigin = sortByQuarter.loc[sortByQuarter['Origin'] == (jsonFromAjax["city"]) ]

    return sortByOrigin

@app.route("/test", methods=["POST", "GET"])
def test():
    jsonFromAjax = request.get_json()

    sortByOrigin = getFilteredDF(jsonFromAjax)

    coords_All=[]

    for i, row in sortByOrigin.iterrows():
        coords=[]
        originCoords = {"lat": row['Origin Lat'],
                        "lng": row['Origin Lng']
                        }
        destCoords = {"lat": row['Destination Lat'],
                        "lng": row['Destination Lng']
                        }

        coords.append(originCoords)
        coords.append(destCoords)

        coords_All.append(coords)

    return jsonify(coords_All)

@app.route("/get_prices", methods=["POST", "GET"])
def price():

    jsonFromAjax = request.get_json()

    sortByOrigin = getFilteredDF(jsonFromAjax)

    dest=[]

    for p, row in sortByOrigin.iterrows():

        priceDest=[]
        price = row['Fare']
        destCoords = {"lat": row['Destination Lat'],
                        "lng": row['Destination Lng']
                        }
        destination = row['Destination']
        # print(price)
        priceDest.append(price)
        priceDest.append(destCoords)
        priceDest.append(destination)

        dest.append(priceDest)

        # print(price)
    return jsonify(dest)

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


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 03:52:50 2020

@author: praveshj
"""

from sklearn.decomposition import PCA
import numpy as np
import math

print("Data Loading. May Take Time....")
X = np.genfromtxt('../data/tfidf.csv',delimiter = ' ')

#PCA using sklearn, components reduced to 100 form 8266
pca = PCA(n_components = 100)

#Reducting the Principal Components to 100
X = pca.fit_transform(X)

#print(X.shape)

class cluster:
    def __init__(self, vectors, index):
        self.vectors = vectors
        self.index= index

#Cosine Similarity is defined in a similar manner as all the files
def cosine_similarity(X, Y):
    dotP = np.dot(X, Y);
    modX = math.sqrt(X.dot(X))
    modY= math.sqrt(Y.dot(Y))
    return dotP/(modX*modY)

def dist(X, Y):
    return math.exp(-1*cosine_similarity(X, Y));

#Since we are doing both,  Agglomerative as well as KMeans, functions for both of them are present

#Agglomerative Clustering
def clusterDist( a, b, dist_mat):
    min_dis = 1e10
    for i in range(0, len(a.index)):
        for j in range(0, len(b.index)):
            if min_dis > dist_mat[a.index[i]][b.index[j]]:
                min_dis = dist_mat[a.index[i]][b.index[j]]
    return min_dis;

def mergeCluster(a, b):
    vectors = []
    index = []
    vectors.extend(a.vectors);
    index.extend(a.index);
    vectors.extend(b.vectors);
    index.extend(b.index);
    return cluster(vectors, index);

list_cluster = []
num_cluster = len(list_cluster)

for i in range (0, X.shape[0]):
    list_cluster.append(cluster([X[i]], [i] ))

num_cluster = len(list_cluster)
# print(num_cluster)

dist_mat = np.zeros((num_cluster, num_cluster))

for i in range(0, num_cluster):
    for j in range(0, num_cluster):
        if i == j :
            dist_mat[i][j] = 1e10
        else:
            dist_mat[i][j] = dist(X[i], X[j])

while(num_cluster > 8):
    min_dis=1e10
    a = 0
    b = 0
    for i in range(0, num_cluster):
        for j in range(i+1 , num_cluster):
            temp= clusterDist(list_cluster[i], list_cluster[j], dist_mat)
            #print("temp = ", temp)
            #print("min_dist = ", min_dis)
            if( min_dis > temp):
                min_dis = temp
                a = list_cluster[i]
                b = list_cluster[j]
        #print('Done')
    print('Clusters Remaining', num_cluster)
    list_cluster.remove(a)
    list_cluster.remove(b)
    list_cluster.append(mergeCluster(a, b))
    num_cluster = num_cluster-1;

file = open('../data/agglomerative_reduced.txt', 'w')
for clust in list_cluster:
    clust.index.sort()
    for i in range(0, len(clust.index)):
        if(i != len(clust.index ) -1 ):
            file.write(str(clust.index[i])+',')
        else:
            file.write(str(clust.index[i]) + '\n')

file.close()

#Agglomerative Clustering Done
#KMeans Clustering Started for the PCA reduced dataset
def min_dist(centroids, X):
    dis_min = 1e10
    index = 0
    for i in range(0, 8):
        temp = dist(centroids[i], X)
        if dis_min > temp:
            dis_min = temp;
            index = i;
    return index;

num_doc = X.shape[0]

clusterClass = np.zeros((num_doc, 1))
centroids = np.zeros((8, X.shape[1]));
for i in range(0, 8):
    centroids[i]= X[np.random.random_integers(0, X.shape[0])]

for j in range(0, 1000):
    print('Iteration', j)
    for i in range(0, num_doc):
        clusterClass[i] = min_dist(centroids, X[i])

    for thisClass in range(0, 8):
        temp = np.zeros((1, X.shape[1]))
        count = 0
        for i in range(0, num_doc):
            if(clusterClass[i] == thisClass):
                temp = temp + X[i];
                count +=1
        centroids[thisClass] = temp/count

file= open('../data/kmeans_reduced.txt', 'w')
for thisClass in range(0, 8):
    temp = np.zeros((1, X.shape[1]))
    count = 0
    for i in range(0, num_doc):
        if(clusterClass[i] == thisClass):
            if(count == 0):
                file.write(str(i));
                count = 1
            else: file.write(',' + str(i))
    file.write('\n')
file.close()

#KMeans reduced clustering dones


"""
Test functions for models.tools
"""
from statsmodels.compat.pandas import assert_frame_equal, assert_series_equal
from statsmodels.compat.python import lrange

import string

import numpy as np
from numpy.random import standard_normal
from numpy.testing import (
    assert_almost_equal,
    assert_array_equal,
    assert_equal,
    assert_string_equal,
)
import pandas as pd
import pytest

from statsmodels.datasets import longley
from statsmodels.tools import tools
from statsmodels.tools.tools import pinv_extended

@pytest.fixture(scope="module")
def string_var():
    string_var = [
        string.ascii_lowercase[0:5],
        string.ascii_lowercase[5:10],
        string.ascii_lowercase[10:15],
        string.ascii_lowercase[15:20],
        string.ascii_lowercase[20:25],
    ]
    string_var *= 5
    string_var = np.asarray(sorted(string_var))
    series = pd.Series(string_var, name="string_var")
    return series

class TestTools:
    def test_add_constant_list(self):
        x = lrange(1, 5)
        x = tools.add_constant(x)
        y = np.asarray([[1, 1, 1, 1], [1, 2, 3, 4.0]]).T
        assert_equal(x, y)

    def test_add_constant_1d(self):
        x = np.arange(1, 5)
        x = tools.add_constant(x)
        y = np.asarray([[1, 1, 1, 1], [1, 2, 3, 4.0]]).T
        assert_equal(x, y)

    def test_add_constant_has_constant1d(self):
        x = np.ones(5)
        x = tools.add_constant(x, has_constant="skip")
        assert_equal(x, np.ones((5, 1)))

        with pytest.raises(ValueError):
            tools.add_constant(x, has_constant="raise")

        assert_equal(
            tools.add_constant(x, has_constant="add"), np.ones((5, 2))
        )

    def test_add_constant_has_constant2d(self):
        x = np.asarray([[1, 1, 1, 1], [1, 2, 3, 4.0]]).T
        y = tools.add_constant(x, has_constant="skip")
        assert_equal(x, y)

        with pytest.raises(ValueError):
            tools.add_constant(x, has_constant="raise")

        assert_equal(
            tools.add_constant(x, has_constant="add"),
            np.column_stack((np.ones(4), x)),
        )

    def test_add_constant_series(self):
        s = pd.Series([1.0, 2.0, 3.0])
        output = tools.add_constant(s)
        expected = pd.Series([1.0, 1.0, 1.0], name="const")
        assert_series_equal(expected, output["const"])

    def test_add_constant_dataframe(self):
        df = pd.DataFrame([[1.0, "a", 4], [2.0, "bc", 9], [3.0, "def", 16]])
        output = tools.add_constant(df)
        expected = pd.Series([1.0, 1.0, 1.0], name="const")
        assert_series_equal(expected, output["const"])
        dfc = df.copy()
        dfc.insert(0, "const", np.ones(3))
        assert_frame_equal(dfc, output)

    def test_add_constant_zeros(self):
        a = np.zeros(100)
        output = tools.add_constant(a)
        assert_equal(output[:, 0], np.ones(100))

        s = pd.Series([0.0, 0.0, 0.0])
        output = tools.add_constant(s)
        expected = pd.Series([1.0, 1.0, 1.0], name="const")
        assert_series_equal(expected, output["const"])

        df = pd.DataFrame([[0.0, "a", 4], [0.0, "bc", 9], [0.0, "def", 16]])
        output = tools.add_constant(df)
        dfc = df.copy()
        dfc.insert(0, "const", np.ones(3))
        assert_frame_equal(dfc, output)

        df = pd.DataFrame([[1.0, "a", 0], [0.0, "bc", 0], [0.0, "def", 0]])
        output = tools.add_constant(df)
        dfc = df.copy()
        dfc.insert(0, "const", np.ones(3))
        assert_frame_equal(dfc, output)

    def test_recipr(self):
        X = np.array([[2, 1], [-1, 0]])
        Y = tools.recipr(X)
        assert_almost_equal(Y, np.array([[0.5, 1], [0, 0]]))

    def test_recipr0(self):
        X = np.array([[2, 1], [-4, 0]])
        Y = tools.recipr0(X)
        assert_almost_equal(Y, np.array([[0.5, 1], [-0.25, 0]]))

    def test_extendedpinv(self):
        X = standard_normal((40, 10))
        np_inv = np.linalg.pinv(X)
        np_sing_vals = np.linalg.svd(X, 0, 0)
        sm_inv, sing_vals = pinv_extended(X)
        assert_almost_equal(np_inv, sm_inv)
        assert_almost_equal(np_sing_vals, sing_vals)

    def test_extendedpinv_singular(self):
        X = standard_normal((40, 10))
        X[:, 5] = X[:, 1] + X[:, 3]
        np_inv = np.linalg.pinv(X)
        np_sing_vals = np.linalg.svd(X, 0, 0)
        sm_inv, sing_vals = pinv_extended(X)
        assert_almost_equal(np_inv, sm_inv)
        assert_almost_equal(np_sing_vals, sing_vals)

    def test_fullrank(self):
        import warnings

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            X = standard_normal((40, 10))
            X[:, 0] = X[:, 1] + X[:, 2]

            Y = tools.fullrank(X)
            assert_equal(Y.shape, (40, 9))

            X[:, 5] = X[:, 3] + X[:, 4]
            Y = tools.fullrank(X)
            assert_equal(Y.shape, (40, 8))
            warnings.simplefilter("ignore")

def test_estimable():
    rng = np.random.RandomState(20120713)
    N, P = (40, 10)
    X = rng.normal(size=(N, P))
    C = rng.normal(size=(1, P))
    isestimable = tools.isestimable
    assert isestimable(C, X)
    assert isestimable(np.eye(P), X)
    for row in np.eye(P):
        assert isestimable(row, X)
    X = np.ones((40, 2))
    assert isestimable([1, 1], X)
    assert not isestimable([1, 0], X)
    assert not isestimable([0, 1], X)
    assert not isestimable(np.eye(2), X)
    halfX = rng.normal(size=(N, 5))
    X = np.hstack([halfX, halfX])
    assert not isestimable(np.hstack([np.eye(5), np.zeros((5, 5))]), X)
    assert not isestimable(np.hstack([np.zeros((5, 5)), np.eye(5)]), X)
    assert isestimable(np.hstack([np.eye(5), np.eye(5)]), X)
    # Test array_like for design
    XL = X.tolist()
    assert isestimable(np.hstack([np.eye(5), np.eye(5)]), XL)
    # Test ValueError for incorrect number of columns
    X = rng.normal(size=(N, 5))
    for n in range(1, 4):
        with pytest.raises(ValueError):
            isestimable(np.ones((n,)), X)
    with pytest.raises(ValueError):
        isestimable(np.eye(4), X)

def test_pandas_const_series():
    dta = longley.load_pandas()
    series = dta.exog["GNP"]
    series = tools.add_constant(series, prepend=False)
    assert_string_equal("const", series.columns[1])
    assert_equal(series.var(0).iloc[1], 0)

def test_pandas_const_series_prepend():
    dta = longley.load_pandas()
    series = dta.exog["GNP"]
    series = tools.add_constant(series, prepend=True)
    assert_string_equal("const", series.columns[0])
    assert_equal(series.var(0).iloc[0], 0)

def test_pandas_const_df():
    dta = longley.load_pandas().exog
    dta = tools.add_constant(dta, prepend=False)
    assert_string_equal("const", dta.columns[-1])
    assert_equal(dta.var(0).iloc[-1], 0)

def test_pandas_const_df_prepend():
    dta = longley.load_pandas().exog
    # regression test for #1025
    dta["UNEMP"] /= dta["UNEMP"].std()
    dta = tools.add_constant(dta, prepend=True)
    assert_string_equal("const", dta.columns[0])
    assert_equal(dta.var(0).iloc[0], 0)

class TestNanDot:
    @classmethod
    def setup_class(cls):
        nan = np.nan
        cls.mx_1 = np.array([[nan, 1.0], [2.0, 3.0]])
        cls.mx_2 = np.array([[nan, nan], [2.0, 3.0]])
        cls.mx_3 = np.array([[0.0, 0.0], [0.0, 0.0]])
        cls.mx_4 = np.array([[1.0, 0.0], [1.0, 0.0]])
        cls.mx_5 = np.array([[0.0, 1.0], [0.0, 1.0]])
        cls.mx_6 = np.array([[1.0, 2.0], [3.0, 4.0]])

    def test_11(self):
        test_res = tools.nan_dot(self.mx_1, self.mx_1)
        expected_res = np.array([[np.nan, np.nan], [np.nan, 11.0]])
        assert_array_equal(test_res, expected_res)

    def test_12(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_1, self.mx_2)
        expected_res = np.array([[nan, nan], [nan, nan]])
        assert_array_equal(test_res, expected_res)

    def test_13(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_1, self.mx_3)
        expected_res = np.array([[0.0, 0.0], [0.0, 0.0]])
        assert_array_equal(test_res, expected_res)

    def test_14(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_1, self.mx_4)
        expected_res = np.array([[nan, 0.0], [5.0, 0.0]])
        assert_array_equal(test_res, expected_res)

    def test_41(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_4, self.mx_1)
        expected_res = np.array([[nan, 1.0], [nan, 1.0]])
        assert_array_equal(test_res, expected_res)

    def test_23(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_2, self.mx_3)
        expected_res = np.array([[0.0, 0.0], [0.0, 0.0]])
        assert_array_equal(test_res, expected_res)

    def test_32(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_3, self.mx_2)
        expected_res = np.array([[0.0, 0.0], [0.0, 0.0]])
        assert_array_equal(test_res, expected_res)

    def test_24(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_2, self.mx_4)
        expected_res = np.array([[nan, 0.0], [5.0, 0.0]])
        assert_array_equal(test_res, expected_res)

    def test_25(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_2, self.mx_5)
        expected_res = np.array([[0.0, nan], [0.0, 5.0]])
        assert_array_equal(test_res, expected_res)

    def test_66(self):
        nan = np.nan
        test_res = tools.nan_dot(self.mx_6, self.mx_6)
        expected_res = np.array([[7.0, 10.0], [15.0, 22.0]])
        assert_array_equal(test_res, expected_res)

class TestEnsure2d:
    @classmethod
    def setup_class(cls):
        x = np.arange(400.0).reshape((100, 4))
        cls.df = pd.DataFrame(x, columns=["a", "b", "c", "d"])
        cls.series = cls.df.iloc[:, 0]
        cls.ndarray = x

    def test_enfore_numpy(self):
        results = tools._ensure_2d(self.df, True)
        assert_array_equal(results[0], self.ndarray)
        assert_array_equal(results[1], self.df.columns)
        results = tools._ensure_2d(self.series, True)
        assert_array_equal(results[0], self.ndarray[:, [0]])
        assert_array_equal(results[1], self.df.columns[0])

    def test_pandas(self):
        results = tools._ensure_2d(self.df, False)
        assert_frame_equal(results[0], self.df)
        assert_array_equal(results[1], self.df.columns)

        results = tools._ensure_2d(self.series, False)
        assert_frame_equal(results[0], self.df.iloc[:, [0]])
        assert_equal(results[1], self.df.columns[0])

    def test_numpy(self):
        results = tools._ensure_2d(self.ndarray)
        assert_array_equal(results[0], self.ndarray)
        assert_equal(results[1], None)

        results = tools._ensure_2d(self.ndarray[:, 0])
        assert_array_equal(results[0], self.ndarray[:, [0]])
        assert_equal(results[1], None)


class Solution:
    """
    @param: root: the root of tree
    @return: the max node
    """

    def maxNode(self, root):
        if root is None:
            return root
        left = self.maxNode(root.left)
        right = self.maxNode(root.right)
        return self.max(root, self.max(left, right))

    def max(self, left, right):
        if left is None:
            return right
        if right is None:
            return left
        if left.val > right.val:
            return left
        return right


import shutil
import unittest

import paddlehub as hub

class TestHubModule(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.module = hub.Module(name="ernie_vilg")

    @classmethod
    def tearDownClass(cls) -> None:
        shutil.rmtree('ernievilg_output')

    def test_generate_image(self):
        self.module.generate_image(text_prompts=['戴眼镜的猫'],
                                   style="像素风格",
                                   topk=6,
                                   visualization=True,
                                   output_dir='ernievilg_output')

if __name__ == "__main__":
    unittest.main()


#!/usr/bin/python3
from tensorflow import keras
import cv2
import os
import numpy as np
emotion=["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]

model=keras.models.load_model("model2.h5")
cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
    # Capture frame-by-frame
    ret, frames = video_capture.read()
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        crop_image=gray[y:y+h,x:x+w]
        resize_image=cv2.resize(crop_image,(48,48))
        resize_image=resize_image.astype(np.float32)
        resize_image=resize_image/255
        resize_image=np.expand_dims(resize_image,axis=0)
        reshape_image=resize_image.reshape(1,48,48,1)
        predict_vector=model.predict(resize_image)[0]
        index=np.where(predict_vector == np.amax(predict_vector))
        text = emotion[int(index[0])]
        coordinates = (y+10,x+10)
        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
        font = cv2.FONT_HERSHEY_SIMPLEX
        fontScale = 1
        color = (0,0,255)
        thickness = 1
        frames = cv2.putText(frames, text, coordinates, font, fontScale, color, thickness, cv2.LINE_AA)

    # Display the resulting frame
    cv2.imshow('Video', frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()


import pathlib
import re
import typing as t
import argparse

RE_EXTENDS = r"nds [\w<>]* impl"
RE_ENUM = r"enum \w* {(\s*\S*\s*|})"
RE_NAME = r"class \w* e"
RE_IS_CACHEABLE = r"IsCacheable.*\s.*\s.*}"
RE_IS_NOTIFYING = r"IsNotifying.*\s.*\s.*}"
RE_IS_READONLY = r"IsReadOnly.*\s.*\s.*}"
RE_NODE_NAME = r"Name.*\s.*\s.*}"
RE_PROTOTYPE_ARGS = r"Prototype == null(\s*.*\s*|})"

NODE_FMT = """
class %s(%s):
    class Meta:
%s
%s"""

ANONYMOUS_NODE_FMT = """
class %s_nt(Node):
    class Meta:
%s"""

def data_type_from_str(data_type: str) -> int:
    if "U32" in data_type:
        return 0x14
    if "U16" in data_type:
        return 0x13
    if "U8" in data_type:
        return 0x12
    if "S32" in data_type:
        return 0x17
    if "S16" in data_type:
        return 0x16
    if "S8" in data_type:
        return 0x15
    if "E8" in data_type:
        return 0x11
    if "C8" in data_type:
        return 0x10

def get_meta(values: dict) -> str:
    return "\n".join([f"        {key}={value}" for key, value in values.items()])

def new_node(cls_name, base_name, meta: dict, init="") -> str:
    return NODE_FMT % (cls_name, base_name, get_meta(meta), init)

def new_anonymous_node(cls_name: str, meta: dict) -> str:
    return ANONYMOUS_NODE_FMT % (cls_name, get_meta(meta))

def extends(java_class: str) -> str:
    return re.search(RE_EXTENDS, java_class).group()[4:-5]

def get_class_name(java_class: str) -> str:
    return re.search(RE_NAME, java_class).group()[6:-2]

def is_cacheable(java_class: str) -> bool:
    return True if "true" in re.search(RE_IS_CACHEABLE, java_class).group() else False

def is_notifying(java_class: str) -> bool:
    return True if "true" in re.search(RE_IS_NOTIFYING, java_class).group() else False

def is_readonly(java_class: str) -> bool:
    return True if "true" in re.search(RE_IS_READONLY, java_class).group() else False

def get_node_name(java_class: str) -> str:
    node_name = re.search(RE_NODE_NAME, java_class).group()
    return node_name[node_name.find('"') + 1 : node_name.rfind('"')]

def get_enum_class(java_class: str) -> str:
    enum_type = java_class[java_class.find("enum") + 4 :]
    content = enum_type[enum_type.find("{")+1: enum_type.find("}")]
    return content.replace("\n", "").replace(" ", "")

def get_list_prototype(java_class) -> t.List[str]:
    prototype = java_class[java_class.find("Prototype == null") :]
    return prototype[prototype.find("{") + 1 : prototype.find("}")].split("\n")

def create_nodes(path_list: t.Iterable[pathlib.Path]) -> t.Generator[str, t.Any, None]:
    for path in path_list:
        if path.is_dir():
            continue

        with open(str(path), "r", encoding="utf-8") as fp:
            java_class = fp.read()

        base_class = extends(java_class)
        class_name = get_class_name(java_class)
        node_name = get_node_name(java_class)
        meta = {}
        init = ""

        meta["cacheable"] = is_cacheable(java_class)
        meta["notifying"] = is_notifying(java_class)
        meta["readonly"] = is_readonly(java_class)
        meta["path"] = f'"{node_name}"'
        meta["name"] = f'"{class_name}"'

        if "NodeE8" in base_class:
            if "<" in base_class:
                base_class = base_class[: base_class.find("<")]
            # prepare enum class type
            enum_class = get_enum_class(java_class)
            values = []
            for line in enum_class.split(","):
                if line:
                    values.append(line)

            values_fmt = ", ".join([f'{i}: "{val}"' for i, val in enumerate(values)])
            prototype = "[Argument(type=ARG_TYPE_E8)]"
            meta["defaults"] = "{%s}" % values_fmt
        elif base_class == "NodeList":
            content = get_list_prototype(java_class)
            args = []
            for argument in content:
                if "arrayList.add" not in argument:
                    continue

                name, data_type, length = argument[argument.rfind("(") + 1 :].split(
                    ", "
                )
                if '"' in name:
                    name = name[1:-1]
                else:
                    name = '$' + name
                length = length[: length.find(")")]
                args.append(
                    f"Argument(name=\"{name}\", length={length}, type={data_type_from_str(data_type)})"
                )

            prototype = "[" + ",".join(args) + "]"
        else:
            prototype = "[Argument(type=ARG_TYPE_%s)]" % base_class.replace("Node", "")

        meta["prototype"] = prototype
        cls_name = node_name.replace(".", "_") + "_nt"
        yield new_node(cls_name, base_class, meta, init=init)

def is_node_class(java_path: pathlib.Path) -> bool:
    return "Base" in java_path.name and "BaseClasses.java" != java_path.name

def _process_nodes(argv):
    base_path = pathlib.Path(argv.path)
    if argv.from_file:
        nodes = []
        with open(str(base_path), "r", encoding="utf-8") as fp:
            for line in fp.readlines():
                line = line.strip()
                if not line:
                    continue
                name = f'"{line}"'
                cls_name = line.replace(".", "_")
                nodes.append(new_anonymous_node(cls_name, {"path": f'"{line}"', "name": name, "prototype": "dynamic"}))
    else:
        nodes = create_nodes(filter(is_node_class, base_path.iterdir()))

    with open(argv.output, "w", encoding="utf-8") as fp:
        fp.write("from fsapi.netremote.basenode import *\n\n")
        for node in nodes:
            fp.write("\n" + node)

def _list_nodes(argv):
    base_path = pathlib.Path(argv.path)
    for path in filter(is_node_class, base_path.iterdir()):
        if path.is_dir():
            continue

        with open(str(path), "r", encoding="utf-8") as fp:
            java_class = fp.read()

        print(get_node_name(java_class))
        del java_class

def main(cmd=None):
    parser = argparse.ArgumentParser()
    parsers = parser.add_subparsers()

    build_parser = parsers.add_parser("build", help="Create node classes.")
    build_parser.add_argument("path", help="Starting directory.")
    build_parser.add_argument("output", help="Output path.")
    build_parser.add_argument("-f", '--from-file', action="store_true")
    build_parser.set_defaults(fn=_process_nodes)

    list_parser = parsers.add_parser("list", help="Displays all nodes")
    list_parser.add_argument("path", help="Starting directory.")
    list_parser.set_defaults(fn=_list_nodes)
    argv = parser.parse_args(cmd)

    argv.fn(argv)

if __name__ == "__main__":
    main()


from torch import nn
from maskrcnn.config import cfg
import numpy as np

class FPNPredictor(nn.Module):
    def __init__(self):
        super(FPNPredictor, self).__init__()
        num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES
        representation_size = cfg.MODEL.ROI_BOX_HEAD.MLP_HEAD_DIM

        self.cls_score = nn.Linear(representation_size, num_classes)
        self.bbox_pred = nn.Linear(representation_size, num_classes * 4)

        nn.init.normal_(self.cls_score.weight, std=0.01)
        nn.init.normal_(self.bbox_pred.weight, std=0.001)
        for l in [self.cls_score, self.bbox_pred]:
            nn.init.constant_(l.bias, 0)
        nn.init.constant_(self.cls_score.bias, -np.log(99/1))

    def forward(self, x):
        scores = self.cls_score(x)
        bbox_deltas = self.bbox_pred(x)

        return scores, bbox_deltas

_ROI_BOX_PREDICTOR = {
    "FPNPredictor": FPNPredictor,
}

def make_roi_box_predictor():
    func = _ROI_BOX_PREDICTOR[cfg.MODEL.ROI_BOX_HEAD.PREDICTOR]
    return func()