import inspect
import ast

from migen.fhdl.structure import *
from migen.fhdl.visit import TransformModule
from migen.fhdl.specials import Memory
from migen.genlib.ioo import UnifiedIOObject
from migen.pytholite.reg import *
from migen.pytholite.expr import *
from migen.pytholite import transel
from migen.pytholite.io import gen_io
from migen.pytholite.fsm import *

def _is_name_used(node, name):
	for n in ast.walk(node):
		if isinstance(n, ast.Name) and n.id == name:
			return True
	return False

class _Compiler:
	def __init__(self, ioo, symdict, registers):
		self.ioo = ioo
		self.symdict = symdict
		self.registers = registers
		self.ec = ExprCompiler(self.symdict)

	def visit_top(self, node):
		if isinstance(node, ast.Module) \
		  and len(node.body) == 1 \
		  and isinstance(node.body[0], ast.FunctionDef):
			states, exit_states = self.visit_block(node.body[0].body)
			return states
		else:
			raise NotImplementedError

	# blocks and statements
	def visit_block(self, statements):
		sa = StateAssembler()
		statements = iter(statements)
		statement = None
		while True:
			if statement is None:
				try:
					statement = next(statements)
				except StopIteration:
					return sa.ret()
			if isinstance(statement, ast.Assign):
				# visit_assign can recognize a I/O pattern, consume several
				# statements from the iterator and return the first statement
				# that is not part of the I/O pattern anymore.
				statement = self.visit_assign(sa, statement, statements)
			else:
				if isinstance(statement, ast.If):
					self.visit_if(sa, statement)
				elif isinstance(statement, ast.While):
					self.visit_while(sa, statement)
				elif isinstance(statement, ast.For):
					self.visit_for(sa, statement)
				elif isinstance(statement, ast.Expr):
					self.visit_expr_statement(sa, statement)
				else:
					raise NotImplementedError
				statement = None

	def visit_assign(self, sa, node, statements):
		if isinstance(node.value, ast.Call):
			is_special = False
			try:
				value = self.ec.visit_expr_call(node.value)
			except NotImplementedError:
				is_special = True
			if is_special:
				return self.visit_assign_special(sa, node, statements)
		else:
			value = self.ec.visit_expr(node.value)
		if isinstance(value, (int, bool, Value)):
			r = []
			for target in node.targets:
				if isinstance(target, ast.Attribute) and target.attr == "store":
					treg = target.value
					if isinstance(treg, ast.Name):
						r.append(self.symdict[treg.id].load(value))
					else:
						raise NotImplementedError
				else:
					raise NotImplementedError
			sa.assemble([r], [r])
		else:
			raise NotImplementedError

	def visit_assign_special(self, sa, node, statements):
		value = node.value
		assert(isinstance(value, ast.Call))
		if isinstance(value.func, ast.Name):
			callee = self.symdict[value.func.id]
		else:
			raise NotImplementedError

		if callee == transel.Register:
			if len(value.args) != 1:
				raise TypeError("Register() takes exactly 1 argument")
			bits_sign = ast.literal_eval(value.args[0])
			if isinstance(node.targets[0], ast.Name):
				targetname = node.targets[0].id
			else:
				targetname = "unk"
			reg = ImplRegister(targetname, bits_sign)
			self.registers.append(reg)
			for target in node.targets:
				if isinstance(target, ast.Name):
					self.symdict[target.id] = reg
				else:
					raise NotImplementedError
		else:
			return self.visit_io_pattern(sa, node.targets, callee, value.args, value.keywords, statements)

	def visit_io_pattern(self, sa, targets, model, args, keywords, statements):
		# first statement is <modelname> = <model>(<args>)
		if len(targets) != 1 or not isinstance(targets[0], ast.Name):
			raise NotImplementedError("Unrecognized I/O pattern")
		modelname = targets[0].id
		if modelname in self.symdict:
			raise NotImplementedError("I/O model name is not free")

		# second statement must be yield <modelname>
		try:
			ystatement = next(statements)
		except StopIteration:
			raise NotImplementedError("Incomplete or fragmented I/O pattern")
		if not isinstance(ystatement, ast.Expr) \
		  or not isinstance(ystatement.value, ast.Yield) \
		  or not isinstance(ystatement.value.value, ast.Name) \
		  or ystatement.value.value.id != modelname:
			print(ast.dump(ystatement))
			raise NotImplementedError("Unrecognized I/O pattern")

		# following optional statements are assignments to registers
		# with <modelname> used in expressions.
		from_model = []
		while True:
			try:
				fstatement = next(statements)
			except StopIteration:
				fstatement = None
			if not isinstance(fstatement, ast.Assign) \
			  or not _is_name_used(fstatement.value, modelname):
				break
			tregs = []
			for target in fstatement.targets:
				if isinstance(target, ast.Attribute) and target.attr == "store":
					if isinstance(target.value, ast.Name):
						tregs.append(self.symdict[target.value.id])
					else:
						raise NotImplementedError
				else:
					raise NotImplementedError
			from_model.append((tregs, fstatement.value))

		states, exit_states = gen_io(self, modelname, model, args, keywords, from_model)
		sa.assemble(states, exit_states)
		return fstatement

	def visit_if(self, sa, node):
		test = self.ec.visit_expr(node.test)
		states_t, exit_states_t = self.visit_block(node.body)
		states_f, exit_states_f = self.visit_block(node.orelse)
		exit_states = exit_states_t + exit_states_f

		test_state_stmt = If(test, AbstractNextState(states_t[0]))
		test_state = [test_state_stmt]
		if states_f:
			test_state_stmt.Else(AbstractNextState(states_f[0]))
		else:
			exit_states.append(test_state)

		sa.assemble([test_state] + states_t + states_f,
			exit_states)

	def visit_while(self, sa, node):
		test = self.ec.visit_expr(node.test)
		states_b, exit_states_b = self.visit_block(node.body)

		test_state = [If(test, AbstractNextState(states_b[0]))]
		for exit_state in exit_states_b:
			exit_state.insert(0, AbstractNextState(test_state))

		sa.assemble([test_state] + states_b, [test_state])

	def visit_for(self, sa, node):
		if not isinstance(node.target, ast.Name):
			raise NotImplementedError
		target = node.target.id
		if target in self.symdict:
			raise NotImplementedError("For loop target must use an available name")
		it = self.visit_iterator(node.iter)
		states = []
		last_exit_states = []
		for iteration in it:
			self.symdict[target] = iteration
			states_b, exit_states_b = self.visit_block(node.body)
			for exit_state in last_exit_states:
				exit_state.insert(0, AbstractNextState(states_b[0]))
			last_exit_states = exit_states_b
			states += states_b
		del self.symdict[target]
		sa.assemble(states, last_exit_states)

	def visit_iterator(self, node):
		if isinstance(node, ast.List):
			return ast.literal_eval(node)
		elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
			funcname = node.func.id
			args = map(ast.literal_eval, node.args)
			if funcname == "range":
				return range(*args)
			else:
				raise NotImplementedError
		else:
			raise NotImplementedError

	def visit_expr_statement(self, sa, node):
		if isinstance(node.value, ast.Yield):
			yvalue = node.value.value
			if not isinstance(yvalue, ast.Call) or not isinstance(yvalue.func, ast.Name):
				raise NotImplementedError("Unrecognized I/O pattern")
			callee = self.symdict[yvalue.func.id]
			states, exit_states = gen_io(self, None, callee, yvalue.args, yvalue.keywords, [])
			sa.assemble(states, exit_states)
		else:
			raise NotImplementedError

class Pytholite(UnifiedIOObject):
	def __init__(self, func):
		self.func = func

	def do_finalize(self):
		UnifiedIOObject.do_finalize(self)
		if self.get_dataflow():
			self.busy.reset = 1
		self.memory_ports = dict()
		for mem in self.__dict__.values():
			if isinstance(mem, Memory):
				port = mem.get_port(write_capable=True, we_granularity=8)
				self.specials += port
				self.memory_ports[mem] = port
		self._compile()

	def _compile(self):
		tree = ast.parse(inspect.getsource(self.func))
		symdict = self.func.__globals__.copy()
		registers = []

		states = _Compiler(self, symdict, registers).visit_top(tree)

		for register in registers:
			if register.source_encoding:
				register.finalize()
				self.submodules += register

		fsm = implement_fsm(states)
		self.submodules += TransformModule(LowerAbstractLoad().visit, fsm)


from django.shortcuts import render,redirect
from django.views.generic import ListView
from .forms import UserDataForm
from .models import UserData

def submitform(request):
    form = UserDataForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect('/list/')

    context= {'form': form }

    return render(request, 'form.html', context)

class UserListView(ListView):
    model = UserData
    template_name = 'list.html'


import serial
from time import sleep
import R2Protocol

#print(R2Protocol.encode(b"NUC", b"MOTOR", b"", b"M10001M20001"))
try:
	s = serial.Serial("COM8", 9600, timeout=2)
	print("Connected")
except serial.SerialException:
	print("No serial port found")
	exit()

try:
	str = R2Protocol.encode(b"NUC", b"FLAP", b"T", b"C")
	print(str)
	s.write(str)
	print("sent")
	sleep(1)
	line = s.readline()
	print(line)
except serial.SerialTimeoutException:
	print("Serial port write timed out 1!")


# coding=utf-8
# 对全行业的原始数据进行聚类/清洗全零行
# 将聚类结果中位数等结果，进行可视化显示/散点图/横向条形图/折线图

import pandas as pd
import numpy as np
from pandas import DataFrame
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

A = pd.read_excel(r'D:\Program Files\JetBrains\PyCharmFile\Cluster\Data\d2.xlsx', sheet_name="Sheet1", header=0)
tabtop = A.columns.values.tolist()        # 读出表头
B = np.array(A)                           # 转化为矩阵
C1 = B[0:1543, 0:1]                       # 取出第一列数据
C2 = C1.reshape(-1)                       # 转为行矩阵
C = C2.astype(str)                        # 变换为字符串形式
lenth1 = len(C)
count = []
temp = []
linelabel = list(range(1, 13))
year1 = ['2016年', '2017年', '2018年']

print(C)

for k in range(lenth1-1):
    if (C[k])[0:2] == (C[k+1])[0:2]:
        temp.append((C[k])[0:2])
        continue
    else:
        count.append(k+1)
count.append(lenth1+1)
print(count)              # 取出第一列数据的聚类起始位置
temp = sorted(set(temp), key=temp.index)     # 去掉列表中的重复值
print(temp)

t = 0
b = 0
for i in count:
    a = b
    b = i
    x = 6
    y = 18
    for j in year1:
        # 对某一行业的进行聚类(三次)
        E1 = B[a:b, x:y]
        E2 = DataFrame(E1)
        D1 = E2.loc[~(E2 == 0).all(axis=1), :]
        E3 = np.array(D1)
        model = KMeans(n_clusters=4)           # 聚类函数参数设置
        model.fit(D1)                          # 开始聚类
        R = DataFrame(model.cluster_centers_)  # 找出聚类中心
        D = DataFrame(D1)
        label1 = model.labels_
        label2 = list(set(label1))     # 删除矩阵中重复值
        len5 = len(label2)             # 类别数的长度
        len4 = D.shape[0]

        m1 = np.median(R, axis=0)    # 求出每列的中位数
        Rm1 = np.vstack((R, m1))     # 将中位数的列合并
        len1 = Rm1.shape[0]          # 求出行的数目
        print(len1)

        m2 = np.median(D, axis=0)   # 求出每列的中位数
        Dm2 = np.vstack((D, m2))    # 将中位数的列合并
        len2 = Dm2.shape[0]         # 求出行的数目
        print(len2)

        Dm21 = np.vstack((Dm2, m1))  # 将原始数据和两组中位数合并
        len3 = Dm21.shape[0]  # 求出行的数目

        print(len3)

        plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示正常中文标签
        plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
        color1 = ['red', 'blue', 'orange', 'green', 'black', 'gray', 'pink', 'brown',  'yellow', 'purple']
        plt.figure(figsize=(10, 6))
        ind = np.arange(len(linelabel))  # the x locations for the groups
        width = 0.2                      # the width of the bars
        dis = [-0.3, -0.15, 0, 0.15, 0.3]
        for i1 in range(len1):
            if i1 == (len1 - 1):
                plt.barh(ind + dis[i1], Rm1[i1], width, color=color1[i1], label='聚类中位数')
            else:
                plt.barh(ind + dis[i1], Rm1[i1], width, color=color1[i1], label='类别' + str(i1 + 1))

        plt.legend(loc='best')
        plt.yticks(ind, ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'))  # x轴的刻度
        title1 = j + '企业月用电量聚类中心图'
        plt.title(title1)
        plt.xlabel('聚类中心值')
        plt.ylabel('月份')

        name1 = temp[t] + '行业' + j + '企业聚类1'
        plt.savefig(name1)
        plt.close()
        plt.figure()
        for i2 in range(len2):
            if i2 == (len2 - 1):
                plt.plot(linelabel, Dm2[i2], color='blue', linestyle='--', marker='^', linewidth=0.5, label='用电量中位数')
            elif i2 == (len2 - 2):
                lengedname = '各企业'
                plt.plot(linelabel, Dm2[i2], color='darkgray', linewidth=1.0, label=lengedname)
            else:
                plt.plot(linelabel, Dm2[i2], color='darkgray', linewidth=1.0)

        plt.legend(loc='best')
        title2 = j + '企业月用电量图1'
        plt.title(title2)
        plt.ylabel('用电量/千瓦时')
        plt.xlabel('月份')
        name2 = temp[t] + '行业' + j + '企业聚类2'
        plt.savefig(name2)
        plt.close()

        plt.figure()
        for i3 in range(len3):
            if i3 == (len3 - 1):
                plt.plot(linelabel, Dm21[i3], color='red', linestyle='--', marker='*', linewidth=0.5, label='聚类中位数')
            elif i3 == (len3 - 2):
                plt.plot(linelabel, Dm21[i3], color='blue', linestyle='--', marker='^', linewidth=0.5, label='用电量中位数')
            elif i3 == (len3 - 3):
                lengedname = '各企业'
                plt.plot(linelabel, Dm21[i3], color='darkgray', linewidth=1.0, label=lengedname)
            else:
                plt.plot(linelabel, Dm21[i3], color='darkgray', linewidth=1.0)

        plt.legend(loc='best')
        title3 = j + '企业月用电量图'
        plt.title(title3)
        plt.ylabel('用电量/千瓦时')
        plt.xlabel('月份')
        name3 = temp[t] + '行业' + j + '企业聚类3'
        plt.savefig(name3)
        plt.close()

        plt.figure()
        for i4 in range(len5):
            count = 0
            for j2 in range(len4):
                if (label1[j2] == label2[i4]) and (count == 0):
                    plt.scatter(linelabel, E3[j2], color=color1[i4], linewidth=1.0, label='类别' + str(i4 + 1))
                    count = count + 1
                if (label1[j2] == label2[i4]) and (count > 0):
                    plt.scatter(linelabel, E3[j2], color=color1[i4], linewidth=1.0)

        plt.legend(loc='best')
        title4 = j + '企业月用电量聚类类别图'
        plt.title(title4)
        plt.ylabel('用电量/千瓦时')
        plt.xlabel('月份')
        name4 = temp[t] + '行业' + j + '企业聚类4'
        plt.savefig(name4)
        plt.close()
        x = y
        y = y + 12
    t = t + 1


from multi_rake import Rake
#Rake library om keyworden te extraheren uit een string

rake = Rake()
def getKeywordsFromBody(var):
    keywords = rake.apply(var)
    print(keywords[:10])

    #Declaratie mogelijke keywords
    vacature = ['Bachelor', 'master ', 'manager', 'vacature', 'spontane sollicitatie', 'veel ervaring', 'CV', 'kennis en vaardigheden', "sollicitatie"]
    sponsering = ['Sponsering', 'sponsor', 'sponser', 'reclame', 'ondersteunen', 'steunen', 'sponsorbrief', 'sponserbrief']
    offerte= ['pricing', 'offerte', 'prijsaanvraag', 'quotation request', 'prijsvraag', 'enquiring', 'quote',
             'catalogus', 'prijs lijst', 'beste prijzen', 'prijslijst'
             'verzendtijd', 'levertijd', 'levertermijn', 'delivery conditions', 'request for quotation']
    aanmaning= ['tweede herinnering', 'aanmaning', 'deurwaarder', 'openstaande factuur', 'vandaag de nodige betalingen te verrichten']
    array = [vacature,sponsering,offerte,aanmaning]
    arrayWordcounter = []
    #Opsplitsen van body in kleinere strings
    wordlist = var.split()
    # n = 5
    # newText = [' '.join(wordlist[i:i + n]) for i in range(0, len(wordlist), n)]

    wordCounter = 0

    # print(newText)
    # for word in vacature:
    #     for sentence in newText:
    #         if sentence.__contains__(word):
    #
    #             wordCounter= wordCounter + 1
    #             print("word: " + str(word) + " counter: " + str(wordCounter)
    arraynumber = 0
    for subarray in array:
        for word in subarray:
            wordCounter += wordlist.count(word)
            if wordlist.__contains__(word):
                print(word)
        arrayWordcounter.append(wordCounter)
        wordCounter = 0
    print(arrayWordcounter)

    return keywords


#This program uses nested for loop to identify which meals have and don't have spam.
#Lists each ingredients in either and calculates a spam score for the relevant meal.

menu = [
    ["egg", "bacon"],
    ["egg", "sausage", "bacon"],
    ["egg", "spam"],
    ["egg", "bacon", "spam"],
    ["egg", "bacon", "sausage", "spam"],
    ["spam", "bacon", "sausage", "spam"],
    ["spam", "sausage", "spam", "bacon", "spam", "tomato", "spam"],
    ["spam", "egg", "spam", "spam", "bacon", "spam"],
]

for meal in menu:
    if "spam" not in meal:
        print(meal)

        for item in meal:
            print(item)
    else:
        print("{0} has a spam score of {1}"
              .format(meal, meal.count("spam")))


import os
import re
import sys
import json
from termcolor import colored
from string import punctuation
import argparse
from ntpath import split as ntsplit
from nltk.corpus.reader import PlaintextCorpusReader
from nltk.stem.snowball import FrenchStemmer, SnowballStemmer

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity

import operator

# def getVectorSpace(cleanSet):
#     vocab = {}
#     for data in cleanSet:
#         for word in data.split():
#             print("woard", word)
#             vocab[data] = 0
#     return vocab.keys()

def calculateSimilarity(sentence, doc):
    if doc == []:
        return 0
    vocab = {}
    for word in sentence.split(" "):
        vocab[word] = 0

    docInOneSentence = ""
    for t in doc:
        docInOneSentence += t + " "
        for word in t.split():
            vocab[word] = 0

    cv = CountVectorizer(vocabulary=vocab.keys())

    docVector = cv.fit_transform([docInOneSentence])
    sentenceVector = cv.fit_transform([sentence])
    return cosine_similarity(docVector, sentenceVector)[0][0]

def load_stopwords(stopwords_file, lang):
    with open(stopwords_file) as f:
        return json.load(f)[lang]

def cleanData(sentence):
    # remove stopwords
    # sentence = re.sub('[^A-Za-z0-9 ]+', '', sentence)
    # sentence filter(None, re.split("[.!?", setence))
    ret = []
    sentence = stemmer.stem(sentence)
    for word in sentence.split():
        if not word in stopwords:
            ret.append(word)
    return " ".join(ret)

def stemmize(stemmer, sent, stopwords):
    clean_sent = []
    for word in sent:
        stem = stemmer.stem(word)
        if word not in stopwords and word not in punctuation:
            clean_sent.append(stem)
    return clean_sent

def load_sentences(text_file, stopwords, lang):
    path, f = ntsplit(text_file)
    reader = PlaintextCorpusReader(path, f)
    sentences = [sent for sent in reader.sents()]
    clean = []
    originalSentenceOf = {}
    if lang == "fr":
        stemmer = FrenchStemmer()
    elif lang == "en":
        stemmer = SnowballStemmer("english")
    # Data cleansing
    for sent in sentences:
        s = stemmize(stemmer, sent, stopwords)
        clean.append(" ".join(s))
        originalSentenceOf[clean[-1]] = sent
    setClean = set(clean)
    return setClean, originalSentenceOf, sentences, clean

def load_data(stopwords_file, text_file, lang):
    stopwords = load_stopwords(stopwords_file, lang)
    setClean, originalSentenceOf, sentences, clean = load_sentences(
        text_file, stopwords, lang
    )

    return stopwords, setClean, originalSentenceOf, sentences, clean

def compute_similarity_scores(clean, setClean):
    # calculate Similarity score each sentence with whole documents
    scores = {}
    for data in clean:
        temp_doc = setClean - set([data])
        score = calculateSimilarity(data, list(temp_doc))
        scores[data] = score
    return scores

def summarize_mmr(scores, sentences, percent):
    # calculate MMR
    n = percent * len(sentences) / 100
    alpha = 0.5
    summarySet = []
    while n > 0:
        mmr = {}
        for sentence in scores.keys():
            if not sentence in summarySet:
                mmr[sentence] = alpha * scores[sentence] - (
                    1 - alpha
                ) * calculateSimilarity(sentence, summarySet)
        selected = max(mmr.items(), key=operator.itemgetter(1))[0]
        summarySet.append(selected)
        n -= 1
    return summarySet

def show_results(summarySet, originalSentenceOf, clean):

    print("\nSummary:\n")
    for sentence in summarySet:
        print(" ".join(originalSentenceOf[sentence]))
    print()

    print("=============================================================")
    print("\nOriginal Passages:\n")
    for sentence in clean:
        if sentence in summarySet:
            print(colored(" ".join(originalSentenceOf[sentence]), "red"))
        else:
            print(" ".join(originalSentenceOf[sentence]))

def run_mmr(stopwords_file, text_file, lang, percent):
    stopwords, setClean, originalSentenceOf, sentences, clean = load_data(
        stopwords_file, text_file, lang
    )
    scores = compute_similarity_scores(clean, setClean)
    summary = summarize_mmr(scores, sentences, percent)
    show_results(summary, originalSentenceOf, clean)

def main():
    def parse_arguments():
        parser = argparse.ArgumentParser(description="simple mmr for French")
        parser.add_argument(
            "-t", "--text", help="file to summarize", default="sample.txt"
        )
        parser.add_argument(
            "-s",
            "--stopwords",
            help="json file containing stopwords for multiple languages ",
            default="stopwords.json",
        )
        parser.add_argument(
            "-p",
            "--percent",
            type=int,
            help="percentage of sentences to use for summary",
            default=20,
        )
        parser.add_argument(
            "-l", "--lang", type=str, help="language of the input text", default="fr"
        )

        args = parser.parse_args()

        if args.lang.lower() in ("french", "fr"):
            args.lang = "fr"
        elif args.lang.lower() in ("english", "en"):
            args.lang = "en"
        else:
            raise Exception("Unknown lang ", args.lang)

        return args

    args = parse_arguments()
    stopwords_file = args.stopwords
    text_file = args.text
    percent = args.percent

    lang = args.lang
    print("text file :", text_file, file=sys.stderr)
    print("stopwords file", stopwords_file, file=sys.stderr, end="\n\n\n")

    run_mmr(stopwords_file, text_file, lang)

if __name__ == "__main__":
    main()


"""
$Id$
$HeadURL$

Copyright (c) 2005, 2006 Peter Kropf. All rights reserved.

Module file for the graphviz trac wiki processor.
"""

__revision__  = '$LastChangedRevision$'
__id__        = '$Id$'
__headurl__   = '$HeadURL$'
__docformat__ = 'restructuredtext'

from graphviz import *


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# checks if diffusion coefficients from umbrellas are similar to standard trajectories

protname = ['1E0G','1R69','SH3']
std_path_1E0G = '/home/tab10/work/simdata/6-20-15_native/'+protname[0]+'ca_avg_gaussian/'+protname[0]+'/iteration_0/'
umb_path_1E0G = '/home/tab10/work/simdata/6-20-15_native/'+protname[0]+'ca_avg_gaussian/'+protname[0]+'/umbrellas/'
std_path_1R69 = '/home/tab10/work/simdata/6-20-15_native/'+protname[1]+'ca_avg_gaussian/'+protname[1]+'/iteration_0/'
umb_path_1R69 = '/home/tab10/work/simdata/6-20-15_native/'+protname[1]+'ca_avg_gaussian/'+protname[1]+'/umbrellas/'
std_path_SH3 = '/home/tab10/work/simdata/6-20-15_native/'+protname[2]+'ca_avg_gaussian/'+protname[2]+'/iteration_0/'
umb_path_SH3 = '/home/tab10/work/simdata/6-20-15_native/'+protname[2]+'ca_avg_gaussian/'+protname[2]+'/umbrellas/'
model = ['Standard Trajectory', 'Umbrella Trajectory w/ integration', 'Umbrella Trajectory w/ fitting']

std_paths = [std_path_1E0G,std_path_1R69,std_path_SH3]
umb_paths = [umb_path_1E0G,umb_path_1R69,umb_path_SH3]

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

#start = 120.45 # start and stop temps
#stop = 124.45
#temps = np.arange(start,stop+1,1)

std_data_1E0G = np.zeros((5,19))
std_data_1R69 = np.zeros((5,19))
std_data_SH3 = np.zeros((5,19))
umb_data_1E0G = np.zeros((5,3))
umb_data_1R69 = np.zeros((5,3))
umb_data_SH3 = np.zeros((5,3))
umb_data_1E0G_fits = np.zeros((5,3))
umb_data_1R69_fits = np.zeros((5,3))
umb_data_SH3_fits = np.zeros((5,3))
temps_1E0G = []
temps_1R69 = []
temps_SH3 = []
################################
os.chdir(std_path_1E0G)
with open('long_temps') as f:
    temps_1E0G = f.readlines()
for i in range(len(temps_1E0G)):
    temps_1E0G[i] = temps_1E0G[i].split('_')[0]
temps_1E0G = np.sort(list(set(temps_1E0G)))
std_data_1E0G[:,:] = np.loadtxt('TPTanalysis',skiprows=1)
fold_1E0G = np.loadtxt('short_Tf')
x_1E0G = std_data_1E0G[:,0]/fold_1E0G # T/T_f

os.chdir(std_path_1R69)
with open('long_temps') as f:
    temps_1R69 = f.readlines()
for i in range(len(temps_1R69)):
    temps_1R69[i] = temps_1R69[i].split('_')[0]
temps_1R69 = np.sort(list(set(temps_1R69)))
std_data_1R69[:,:] = np.loadtxt('TPTanalysis',skiprows=1)
fold_1R69 = np.loadtxt('short_Tf')
x_1R69 = std_data_1R69[:,0]/fold_1R69 # T/T_f

os.chdir(std_path_SH3)
with open('long_temps') as f:
    temps_SH3 = f.readlines()
for i in range(len(temps_SH3)):
    temps_SH3[i] = temps_SH3[i].split('_')[0]
temps_SH3 = np.sort(list(set(temps_SH3)))
std_data_SH3[:,:] = np.loadtxt('TPTanalysis',skiprows=1)
fold_SH3 = np.loadtxt('short_Tf')
x_SH3 = std_data_SH3[:,0]/fold_SH3 # T/T_f
#################################
os.chdir(umb_path_1E0G)
for i in range(len(temps_1E0G)):
    for j in [0,1,2]:
        os.chdir('%s_%s' % (temps_1E0G[i],j+1))
        umb_data_1E0G[i,j] = np.loadtxt('diff_umb_D')[2] # assumes TS umbrella is position 2
        umb_data_1E0G_fits[i,j] = np.loadtxt('diff_umb_D_fit')[2] # assumes TS umbrella is position 2
        os.chdir('..')

umb_data_1E0G_mean = np.mean(umb_data_1E0G,axis=1)
umb_data_1E0G_std = np.std(umb_data_1E0G,axis=1)
umb_data_1E0G_fits_mean = np.mean(umb_data_1E0G_fits,axis=1)
umb_data_1E0G_fits_std = np.std(umb_data_1E0G_fits,axis=1)

os.chdir(umb_path_1R69)
for i in range(len(temps_1R69)):
    for j in [0,1,2]:
        os.chdir('%s_%s' % (temps_1R69[i],j+1))
        umb_data_1R69[i,j] = np.loadtxt('diff_umb_D')[2] # assumes TS umbrella is position 2
        umb_data_1R69_fits[i,j] = np.loadtxt('diff_umb_D_fit')[2] # assumes TS umbrella is position 2
        os.chdir('..')

umb_data_1R69_mean = np.mean(umb_data_1R69,axis=1)
umb_data_1R69_std = np.std(umb_data_1R69,axis=1)
umb_data_1R69_fits_mean = np.mean(umb_data_1R69_fits,axis=1)
umb_data_1R69_fits_std = np.std(umb_data_1R69_fits,axis=1)

os.chdir(umb_path_SH3)
for i in range(len(temps_SH3)):
    for j in [0,1,2]:
        os.chdir('%s_%s' % (temps_SH3[i],j+1))
        umb_data_SH3[i,j] = np.loadtxt('diff_umb_D')[2] # assumes TS umbrella is position 2
        umb_data_SH3_fits[i,j] = np.loadtxt('diff_umb_D_fit')[2] # assumes TS umbrella is position 2
        os.chdir('..')

umb_data_SH3_mean = np.mean(umb_data_SH3,axis=1)
umb_data_SH3_std = np.std(umb_data_SH3,axis=1)
umb_data_SH3_fits_mean = np.mean(umb_data_SH3_fits,axis=1)
umb_data_SH3_fits_std = np.std(umb_data_SH3_fits,axis=1)
##################################
os.chdir('/home/tab10/scratch/')
if not os.path.exists('plots'):
    os.makedirs('plots')
os.chdir('plots')

f, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.errorbar(x_1E0G,std_data_1E0G[:,3],yerr=std_data_1E0G[:,4],label=protname[0]+' '+model[0],lw=1.5)
ax2.errorbar(x_1E0G,umb_data_1E0G_mean,yerr=umb_data_1E0G_std,label=protname[0]+' '+model[1],lw=1.5)
ax1.errorbar(x_1R69,std_data_1R69[:,3],yerr=std_data_1R69[:,4],label=protname[1]+' '+model[0],lw=1.5)
ax2.errorbar(x_1R69,umb_data_1R69_mean,yerr=umb_data_1R69_std,label=protname[1]+' '+model[1],lw=1.5)
ax1.errorbar(x_SH3,std_data_SH3[:,3],yerr=std_data_SH3[:,4],label=protname[2]+' '+model[0],lw=1.5)
ax2.errorbar(x_SH3[:-1],umb_data_SH3_mean[:-1],yerr=umb_data_SH3_std[:-1],label=protname[2]+' '+model[1],lw=1.5)
ax3.errorbar(x_1E0G,umb_data_1E0G_fits_mean,yerr=umb_data_1E0G_fits_std,label=protname[0]+' '+model[2],lw=1.5)
ax3.errorbar(x_1R69,umb_data_1R69_fits_mean,yerr=umb_data_1R69_fits_std,label=protname[1]+' '+model[2],lw=1.5)
ax3.errorbar(x_SH3[:-1],umb_data_SH3_fits_mean[:-1],yerr=umb_data_SH3_fits_std[:-1],label=protname[2]+' '+model[2],lw=1.5)
ax1.set_title('D* from $t_{TP}$')
ax2.set_title('D* from umbrellas (integration)')
ax3.set_title('D* from umbrellas (fitting)')
ax1.set_xlabel('$T/T_f$')
ax2.set_xlabel('$T/T_f$')
ax3.set_xlabel('$T/T_f$')
ax1.set_ylabel('$D^*$ ($Q^{2}frames^{-1}$)')
plt.legend(prop={'size':10})
plt.savefig('diff_compare.png')
plt.savefig('diff_compare.pdf')
plt.show()


from django.shortcuts import render, HttpResponseRedirect
from django.contrib import messages
from .models import Clientgallery
from client.models import Client
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

# Create your views here.

@login_required(login_url='login')
def addclig(request):
    cli = Client.objects.all()
    if request.method == 'POST':
        cliid = request.POST['cliid']
        cligimg = request.FILES.getlist('cligimg')
        sta = request.POST['sta']
        for cligimg1 in cligimg:
            add = Clientgallery(cli_id_id=cliid, clig_img=cligimg1, status=sta)

            add.save()

        messages.success(request, 'Client Gallery Image Added Successfully', extra_tags='success')

        return HttpResponseRedirect('/cligly/manageclientgallery/')

        # msg = {'serr':'Client Gallery Image Added Successfully'}
        # return render(request, 'clientgallery/add_clig.html', {'err':msg, 'cli':cli, 'name': request.user})
    else:
        # msg = {'ferr':'Please Fill All Field Either Client Gallery Image Do Not Add.'}
        return render(request, 'clientgallery/add_clig.html', {'cli':cli, 'name': request.user})
    return render(request, 'clientgallery/add_clig.html', {'cat':cat, 'name': request.user})

@login_required(login_url='login')
def edtclig(request, eid):
    eclig = Clientgallery.objects.get(pk=eid)
    cli = Client.objects.all()
    return render(request, 'clientgallery/edit_clig.html', {'eclig':eclig, 'cli':cli, 'name': request.user})

@login_required(login_url='login')
def updclig(request, ucligid):
    if request.method == 'POST':
        try:
            if request.FILES['cligimg'] != 0:
                cliid = request.POST['cliid']
                cligimg = request.FILES['cligimg']
                sta = request.POST['sta']

                edt = Clientgallery.objects.get(clig_id = ucligid)

                edt.cli_id_id = cliid
                edt.clig_img = cligimg
                edt.status = sta

                edt.save()
        except:
            cliid = request.POST['cliid']
            sta = request.POST['sta']

            edt = Clientgallery.objects.get(clig_id = ucligid)

            edt.cli_id_id = cliid
            edt.clig_img = edt.clig_img
            edt.status = sta

            edt.save()

            messages.success(request, 'Client Gallery Image Updated Successfully', extra_tags='success')

            return HttpResponseRedirect('/cligly/manageclientgallery/')

        messages.success(request, 'Client Gallery Image Updated Successfully', extra_tags='success')

        return HttpResponseRedirect('/cligly/manageclientgallery/')

@login_required(login_url='login')
def delclig(request, did):
    de = Clientgallery.objects.get(pk=did)
    de.delete()
    messages.success(request, 'Client Gallery Image Deleted Successfully', extra_tags='danger')
    return HttpResponseRedirect('/cligly/manageclientgallery/')

@login_required(login_url='login')
def manclig(request):
    clig = Clientgallery.objects.all()
    return render(request, 'clientgallery/man_clig.html', {'clig': clig, 'name': request.user})


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Python 2.7
#

import urllib, re, base64

strData = urllib.urlopen('http://localhost/lesson12/RArticleM.aspx').read()

strViewState = base64.b64decode(re.findall(r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />', strData)[0])

print [strViewState]
#print strViewState


from google.appengine.ext import ndb
from handlers.handler import Handler
from helpers import *

from models.projects import Projects
from models.categories import Categories
from models.user import User

class FilterOtherProjects(Handler):
    """ Used to Filter the Projects in the Projects page """
    def post(self, user_name):
        user_other = User.by_name(user_name)
        if user_other:
            name_category = self.request.get("name_c")

            if (name_category == 'all'):
                self.redirect('/visit/projects/%s' % (user_other.name))
            else:
                if (name_category):
                    category = Categories.by_name(name_category)
                projects = Projects.query(Projects.category_name == name_category).fetch()
                categories = Categories.query(Categories.user == user_other).fetch()
                self.render('visitprojects.html',
                            category_filter = category,
                            user_other = user_other,
                            projects = projects,
                            categories = categories)
        else:
            self.render('errorpage.html', error = "Sorry, that information could not be found.")


from dice import Pipeline
from dice.evaluation import Tracker
from dice.evaluation import PairEvaluator

from functools import reduce
import hyperopt
import pickle
import os

class BayesianTuner:

    def __init__(self, annotation_file, feature, *folders):
        self.annotation_file = annotation_file
        self.folders = folders
        def objective(args):
            trackers = list()
            for folder in self.folders:
                pipeline = Pipeline(
                    folder,
                    reduce(lambda x,y: dict(x, **y), (args, {"log": ""}))
                )
                pipeline.load()
                if feature == "evidence":
                    pipeline.step_detective()
                if feature == "confidence":
                    pipeline.step_assigner()
                tracker = Tracker()
                tracker.build(pipeline)
                trackers.append(tracker)
            PairEvaluator.FEATURE = feature
            return PairEvaluator(self.annotation_file, *trackers).evaluate()
        self.objective = objective
        self.choices = dict()

    def parse_space(self, args):
        space = dict()
        for arg in args.split(" "):
            param, rng = arg.split("=")
            if "-" in rng:
                low, high = list(map(float, rng.split("-")))
                space[param] = hyperopt.hp.uniform(param, low, high)
            elif "," in rng:
                values = list(map(float, rng.split(",")))
                space[param] = hyperopt.hp.choice(param, values)
                self.choices[param] = values
            else:
                space[param] = hyperopt.hp.choice(param, (float(rng),))
                self.choices[param] = [float(rng)]
        return space

    def optimize(self, args, max_evals=200):
        space = self.parse_space(args)
        if os.path.isfile("trials.pickle"):
            with open("trials.pickle", "rb") as file:
                trials = pickle.load(file)
        else:
            trials = hyperopt.Trials()
        best = hyperopt.fmin(
            self.objective,
            space,
            algo=hyperopt.tpe.suggest,
            max_evals=max_evals,
            trials=trials,
        )
        pickle.dump(trials, open("trials.pickle", "wb"))
        for param, values in self.choices.items():
            best[param] = values[best[param]]
        return best


"""
Palindrome Permutation
https://leetcode.com/problems/palindrome-permutation/

Time O(n)
Space O(n)
"""
from collections import Counter

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        charFreq = Counter(s)
        oddSeen = False
        for count in charFreq.values():
            if count % 2 != 0:
                if oddSeen:
                    return False
                oddSeen = True
        return True


import sklearn
import matplotlib.pyplot as plt
import numpy as np
import copy

def execute_knn(path : str, title : str, filename : str, X, y, labels_map : list, k_value : int, test_size, seed : int):

    # KNN model ---- Holdout Method
    knn = sklearn.neighbors.KNeighborsClassifier(n_neighbors=k_value)

    # Holdout Method split
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=test_size, random_state=seed)

    # train knn
    knn.fit(X=X_train, y=y_train)

    # predictions and labels
    train_predictions = knn.predict(X=X_train)
    predictions = knn.predict(X=X_test)

    print('KNN Model, scores (Holdout Method):')

    print('Confusion Matrix (test set):')
    print(sklearn.metrics.confusion_matrix(y_true=y_test, y_pred=predictions, labels=np.unique(y_test)) )
    print("Accuracy (train set):", sklearn.metrics.accuracy_score(y_true=y_train, y_pred=train_predictions))
    print("Accuracy (test set):", sklearn.metrics.accuracy_score(y_true=y_test, y_pred=predictions))
    print('Classification results (test set):')
    print(sklearn.metrics.classification_report(y_true=y_test, y_pred=predictions) )

    # Confusion Matrix Plot Test Accuracy
    sklearn.metrics.plot_confusion_matrix(estimator=knn, X=X_test, y_true=y_test, normalize='true', display_labels=labels_map)

    plt.title(title + ':\nKNN Classifier Confusion Matrix\nHoldout Method, Test Set')

    plt.savefig(path + '/' + filename + '_knn_confusion_matrix_hm.png')
    plt.clf()  # clear plot for next method

    # ----KFold Cross Validation----

    # KNN model
    knn = sklearn.neighbors.KNeighborsClassifier(n_neighbors=k_value)

    # At first get train and set with Holdout Method using next value after seed
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=test_size,
                                                                                random_state=(seed + 1))
    # KFold Method
    kf = sklearn.model_selection.KFold(n_splits=10, random_state=seed, shuffle=True)
    kf.get_n_splits(np.array(X_train))

    # KFold Cross Validation
    best_accuracy = 0.0
    for subtrain_index, validation_index in kf.split(np.array(X_train)):
        X_subtrain_fold, X_validation_fold = np.array(X_train)[subtrain_index], np.array(X_train)[validation_index]
        y_subtrain_fold, y_validation_fold = np.array(y_train)[subtrain_index], np.array(y_train)[validation_index]

        knn = sklearn.neighbors.KNeighborsClassifier(n_neighbors=k_value)
        knn.fit(X_subtrain_fold, y_subtrain_fold)

        y_pred = knn.predict(X_validation_fold)
        actual_score = sklearn.metrics.accuracy_score(y_validation_fold, y_pred)

        if actual_score > best_accuracy:
            best_accuracy = actual_score
            best_knn = copy.deepcopy(knn)

    # predictions and labels
    train_predictions = best_knn.predict(X_train)
    predictions = best_knn.predict(X_test)

    print('KNN Model, scores (K-Fold Method):')

    print('Confusion Matrix (test set):')
    print(sklearn.metrics.confusion_matrix(y_true=y_test, y_pred=predictions, labels=np.unique(y_test)))

    print("Best K-Fold Classifier Accuracy (train set):", sklearn.metrics.accuracy_score(y_true=y_train, y_pred=train_predictions))
    print("Best K-Fold Classifier Accuracy (test set):", sklearn.metrics.accuracy_score(y_true=y_test, y_pred=predictions))

    print('Classification results (test set):')
    print(sklearn.metrics.classification_report(y_true=y_test, y_pred=predictions))

    # Confusion Matrix plot Test Accuracy
    sklearn.metrics.plot_confusion_matrix(estimator=best_knn, X=X_test, y_true=y_test, normalize='true', display_labels=labels_map)

    plt.title(
        title + ':\nKNN Classifier Confusion Matrix\nK-Fold Best Classifier, Test Set')

    plt.savefig(path + '/' + filename + '_knn_confusion_matrix_kf.png')
    plt.clf()  # clear plot for next method


def LetterCapitalize(str):
    parced = [x for x in str.split()]
    result = []
    for i in parced:
        result.append(i[0].upper()+i[1:])
    final = ''
    final = ' '.join(result)
    return final

print(LetterCapitalize('hello world'))


# coding=utf-8
'''
Created on 2013-2-28

@author: lijie1
'''

import logging;
import os;
from datetime import date;
from lib import CMail;
from configs import Config;

class Log(object):
    def __init__(self):
        """Do nothing, by default."""

    @staticmethod
    def write(msg, sType = 'info'):

        sCurrentDir = os.getcwd();
        sLogDir = sCurrentDir + '/Logs/';
        if not os.path.exists(sLogDir):
            os.makedirs(sLogDir);

        today = date.today();
        sLogFile = '';
        if sType == 'info':
            sLogFile = 'Log_{0:04d}_{1:02d}_{2:02d}.log'.format(today.year, today.month, today.day);
        if sType == 'debug':
            sLogFile = 'Debug_{0:04d}_{1:02d}_{2:02d}.log'.format(today.year, today.month, today.day);
        if sType == 'error':
            sLogFile = 'Error_{0:04d}_{1:02d}_{2:02d}.log'.format(today.year, today.month, today.day);

        logging.basicConfig(level=logging.DEBUG,
                format   ='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt  ='%Y-%m-%d %H:%M:%S',
                filename = sLogDir + sLogFile,
                filemode ='a');
        msg = msg.encode('gb2312');
        if sType == 'info':
            logging.info(msg);
        if sType == 'debug':
            logging.debug(msg);
#         if sType == 'error':
#             mail = CMail(True);
#             mail.sendMail(Config.YL_MAIL['server'], Config.YL_MAIL['from'], Config.YL_MAIL['to'], [], ' coolshow python task error', str(msg));
            logging.error(msg);


from flask import *
from whitenoise import WhiteNoise
app = Flask(__name__)
app.wsi_app = WhiteNoise(app.wsgi_app, root='static/', prefix='static/', index_file="index.html", autorefresh=True)

@app.route('/', methods=['GET'])
def home():
  # return render_template("index.html")
  return "Jenna Russell: https://jjr265-info4310-hw2.herokuapp.com/static/index.html"

if __name__ == "__main__":
  app.debug = True
  app.run(threaded=True, port=5000)


from semeval2014_dataset import *
import numpy as np
from gensim import corpora
from gensim import models
from gensim.matutils import softcossim
from tqdm import tqdm
from copy import deepcopy

np.random.seed(0)

dataset = SimpleDataset('../../datas/ABSA14_Restaurants_Train.xml', '../../datas/Restaurants_Test_Data_phaseB.xml',1.0)

def find_best_threshold(multi_cat_sentence_pred_label, multi_cat_sentence_gtruth_label):
    threshold = 0.19
    best_res = [0.0, 0.0, 0.0, 0.0]
    while threshold < 0.5:
        TP = 0
        FP = 0
        FN = 0
        for i in range(len(multi_cat_sentence_gtruth_label)):
            pred_labels = deepcopy(multi_cat_sentence_pred_label[i])
            gtruth_labels = deepcopy(multi_cat_sentence_gtruth_label[i])
            for idx in range(len(pred_labels)):
                if pred_labels[idx] > threshold:
                    pred_labels[idx] = 1
                else:
                    pred_labels[idx] = 0
            for idx in range(len(pred_labels)):
                if pred_labels[idx] == 1:
                    if idx in gtruth_labels:
                        TP += 1
                    else:
                        FP += 1
                else:
                    if idx in gtruth_labels:
                        FN += 1
        try:
            precision = TP / (TP + FP)
            recall = TP / (TP + FN)
            f1 = 2 * (precision * recall) / (precision + recall)
        except ZeroDivisionError:
            f1 = 0.0
        if f1 > best_res[0]:
            best_res = [f1, precision, recall, threshold]
        threshold += 0.0001
        threshold = round(threshold, 4)
    return best_res

def softmax(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum(axis=0)

category_label_num = {
            'service': 0,
            'food': 1,
            'price': 2,
            'ambience': 3,
            'anecdotes/miscellaneous': 4
}
category_num_label = {
            0: 'service',
            1: 'food',
            2: 'price',
            3: 'ambience',
            4: 'anecdotes/miscellaneous'
}
categories = [
            'service',
            'food',
            'price',
            'ambience',
            'anecdotes/miscellaneous'
]

test_sentences = []
test_sentences_with_label = []
for item in dataset.test_data:
    test_sentences.append(item[0])
    test_sentences_with_label.append(item)

category_seed_words = np.load('../../w2vec_candid_dict_2014.npy').item()
# category_seed_words = np.load('../../tempseed_pruned.npy').item()
list_category_seed_words = []
for key in category_seed_words.keys():
    list_category_seed_words.append(category_seed_words[key])

w2v_model = models.KeyedVectors.load_word2vec_format('../../yelp_W2V_300_orig.bin', binary=True)
dictionary = corpora.Dictionary(test_sentences)
corpus = [dictionary.doc2bow(document) for document in test_sentences]
similarity_matrix = w2v_model.similarity_matrix(dictionary)

Y = [dictionary.doc2bow(sentence) for sentence in test_sentences]

def cos_sim(a, b):
    dot_product = np.dot(a, b)
    norm_a = np.linalg.norm(a)
    norm_b = np.linalg.norm(b)
    return dot_product / (norm_a * norm_b)

def average_of_sentence(sentence):
    sum_of_words = np.zeros(300)
    for word in sentence:
        try:
            sum_of_words += w2v_model[word]
        except KeyError:
            continue
    return sum_of_words / len(sentence)

def initialize_clusters(points, k):
    random_points = []
    while True:
        random_num = np.random.randint(0, len(points))
        random_points.append(average_of_sentence(points[random_num]))
        if len(random_points) == k:
            break
    return random_points

def get_distances(center, points):
    """Returns the distance the centroid is from each data point in points."""
    distances = []
    for point in points:

        distances.append(1 - cos_sim(center, point))
    return distances

def get_category_seed_similarity(sentence, seeds, similarity_matrix):
    result = 0
    length = len(seeds)
    for word in seeds.keys():
        if word not in seeds[word]:
            seeds[word].append(word)
        seedsentence_d2b = dictionary.doc2bow(seeds[word])
        result += softcossim(sentence, seedsentence_d2b, similarity_matrix)
    return result / length

k = 17
maxiter = 23
centroids = initialize_clusters(test_sentences, k)

classes = len(Y) * [0]

for i in tqdm(range(maxiter)):
    distances = []
    loss = 0
    for j in range(len(test_sentences)):
        centroid_distances = get_distances(average_of_sentence(test_sentences[j]), centroids)
        distances.append(centroid_distances)
    for j in range(len(distances)):
        try:
            classes[j] = int(np.argmin(distances[j]))
            loss += distances[j][classes[j]]
        except:
            print(distances[j])
            exit()
    # classes = np.argmin(np.array(distances), axis=1)
    for c in range(k):
        if len([j for j in range(len(test_sentences)) if classes[j] == c]) < 2:
            continue
        centroids[c] = np.mean([average_of_sentence(test_sentences[j])
                                for j in range(len(test_sentences))
                                if classes[j] == c], 0)
    print(loss)

TP = 0
FP = 0
FN = 0

classes_pred_label = []

for c in range(len(centroids)):
    cluster_sentences = [test_sentences[j] for j in range(len(test_sentences)) if classes[j] == c]
    cluster_sentences_labels = [test_sentences_with_label[j][1]
                                for j in range(len(test_sentences_with_label))
                                if classes[j] == c]
    pred_labels = len(categories) * [0.0]
    for sentence in cluster_sentences:
        for cat in categories:
            seeds = category_seed_words[cat]
            pred_labels[category_label_num[cat]] += get_category_seed_similarity(dictionary.doc2bow(sentence), seeds, similarity_matrix)
    pred_labels = [pred_labels[j] / len(cluster_sentences) for j in range(len(pred_labels))]
    # pred_labels[int(np.argmax(pred_labels))] = 1
    # for j in range(len(pred_labels)):
    #     if j != np.argmax(pred_labels):
    #         pred_labels[j] = 0
    classes_pred_label.append(pred_labels)
print(classes)
threshold = 0.204
rho = 0.8

predicted_labels = []
gtruth_labels = []

for i, sentence in enumerate(test_sentences):
    sentence_class_label = classes_pred_label[classes[i]]
    gtruth_label = test_sentences_with_label[i][1]

    sentence_seed_word_label = len(categories) * [0.0]
    for cat in categories:
        seeds = category_seed_words[cat]
        sentence_seed_word_label[category_label_num[cat]] += get_category_seed_similarity(dictionary.doc2bow(sentence), seeds, similarity_matrix)
    sentence_pred_label = (1 - rho) * softmax(np.array(sentence_class_label)) + rho * softmax(np.array(sentence_seed_word_label))
    #print(sentence_pred_label)
    #if i > 10:
    #    exit()
    predicted_labels.append(deepcopy(sentence_pred_label))
    gtruth_labels.append(deepcopy(gtruth_label))
    for j in range(len(sentence_pred_label)):
        if sentence_pred_label[j] > threshold:
            sentence_pred_label[j] = 1
        else:
            sentence_pred_label[j] = 0

    for idx in range(len(sentence_pred_label)):
        if sentence_pred_label[idx] == 1:
            if idx in gtruth_label:
                TP += 1
            else:
                FP += 1
        else:
            if idx in gtruth_label:
                FN += 1

print(find_best_threshold(predicted_labels, gtruth_labels))

precision = TP / (TP + FP)
recall = TP / (TP + FN)
print(2 * (precision * recall) / (precision + recall))
print(precision)
print(recall)
print()
print(TP)
print(FP)


#!/bin/python3

import sys

# n = length of array of integers
# k = number of right circular rotations to perform
# q = number of queries asked by Dr. Watson.
# a = the array of integers
# m = the index value that should be returned (an array with Dr. Watson's questions)

n,k,q = input().strip().split(' ')
n,k,q = [int(n),int(k),int(q)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]

m = []

for i in range(q):
    m.append( int(input().strip()))

def betterRCR(theList, numberOfRotations):
    offset = len(theList) - (numberOfRotations % len(theList))      # adjust offset when theList < numberOfRotations
    tmp = [-1] * len(theList)                                       # build new array, with offset indices
    for i in range(len(theList)):
        tmp[i] = theList[(i + offset) % len(theList)]
    return tmp                                                      # return a new array, leaving the original intact

rotatedList = betterRCR(a,k)

[print (rotatedList[i], sep='\n') for i in m]                          # Answer Dr. Watson's q questions (values stored in m).


#!/usr/bin/env python
"""reducer.py"""

from operator import itemgetter
import sys

debug=False

def outputPatentInfo(key, values):

    #
    # Our inputs are either 3070801	1963,1096,,"BE","",,1,,269,6,69,,1,,0,,,,,,,
    # or 6009554	y
    #
    #
    # So the "values" either have zero commas or multiple commas
    #

    cites = [ pat for pat in values if pat.count(',') == 0 ]
    info = [ pat for pat in values if  pat.count(',') > 2 ]

    if debug:
        print("values is ", values)
        print("cites is ", cites)
        print("info is ", info)

    try:
        if len(info) == 0:
            print("%s\tbad missing patent info " % (key))
        else:
            try:
                cites_official = int( info[0].split(',')[11] )
            except ValueError as e:
                cites_official = 0

            if len(cites) == cites_official:
                print("%s\tok" % (key))
#
# It would be nice to use the counters in this example but it
# causes the Java process to run out of heap space
#
#                sys.stderr.write("reporter:counter:PatentReducer,REDUCE_GOOD,1")
            else:
                print("%s\tbad cites got %s expected %d " % (key, len(cites), cites_official))
#                sys.stderr.write("reporter:counter:PatentReducer,REDUCE_BAD,1")
    except ValueError as e:
            #
            # Something wrong in number format
            #
        pass
    except Exception as e:
        print("Something died", e)

def main():
    current_patent = None
    values = []

    debug = False

    # input comes from STDIN
    for line in sys.stdin:
        line = line.rstrip('\n')

        # parse the input we got from mapper.py
        try:
            key, value = line.split('\t', 1)
        except:
            print('Improperly formatted: *', line, '*')
            # Improperly formatted, so ignore
            continue

        # convert count (currently a string) to int
        try:
            patent = int(key)
        except ValueError:
            # key was not a number, so silently
            # ignore/discard this line
            continue

        # this IF-switch only works because Hadoop sorts map output
        # by key (here: word) before it is passed to the reducer

        if current_patent != patent:
            if current_patent:
                outputPatentInfo(current_patent, values)
            current_patent = None
            values = []

        current_patent = patent
        values.append(value)

    # do not forget to output the last word if needed!
    if current_patent:
        outputPatentInfo(current_patent, values)

main()


# O(n)
# n = len(nums)

class Solution:
    def isIdealPermutation(self, nums: List[int]) -> bool:
        for i in range(len(nums)):
            if abs(i - nums[i]) > 1:
                return False
        return True


import torch
import torch.nn.functional as F
import models.networks as networks
import util.util as util
import pickle
from models.networks.txt_enc import EncoderText
from models.networks.perturbation import PerturbationNet
import random

def l2norm(x, norm_dim=1):
    norm = torch.pow(x, 2).sum(dim=norm_dim, keepdim=True).sqrt()
    x = torch.div(x, norm)
    return x

class OpenEditModel(torch.nn.Module):

    def __init__(self, opt):
        super().__init__()
        self.opt = opt
        self.FloatTensor = torch.cuda.FloatTensor
        self.ByteTensor = torch.cuda.ByteTensor
        self.perturbation = opt.perturbation

        self.netG, self.netD, self.netE = self.initialize_networks(opt)

        self.generator = opt.netG

        if self.perturbation:
            self.netP = PerturbationNet(opt)
            self.netP.cuda()
        if self.opt.manipulation:
            self.vocab = pickle.load(open(opt.vocab_path, 'rb'))
            self.txt_enc = EncoderText(len(self.vocab), 300, 1024, 1)
            self.txt_enc.load_state_dict(torch.load(opt.vse_enc_path, map_location='cpu')['model'][1])
            self.txt_enc.eval().cuda()

        # set loss functions
        if self.perturbation:
            self.criterionPix = torch.nn.L1Loss()
            self.criterionVGG = networks.VGGLoss(self.opt.gpu)
        elif opt.isTrain:
            self.loss_l1pix = opt.l1pix_loss
            self.loss_gan = not opt.no_disc
            self.loss_ganfeat = not opt.no_ganFeat_loss
            self.loss_vgg = not opt.no_vgg_loss

            if self.loss_l1pix:
                self.criterionPix = torch.nn.L1Loss()
            if self.loss_gan:
                self.criterionGAN = networks.GANLoss(
                    opt.gan_mode, tensor=self.FloatTensor, opt=self.opt)
            if self.loss_ganfeat:
                self.criterionFeat = torch.nn.L1Loss()
            if self.loss_vgg:
                self.criterionVGG = networks.VGGLoss(self.opt.gpu)

    def forward(self, data, mode, ori_cap=None, new_cap=None, alpha=1, global_edit=False):
        if not data['image'].is_cuda:
            data['image'] = data['image'].cuda()
        if mode == 'generator':
            g_loss, generated = self.compute_generator_loss(data)
            return g_loss, generated
        elif mode == 'discriminator':
            d_loss = self.compute_discriminator_loss(data)
            return d_loss
        elif mode == 'inference':
            with torch.no_grad():
                fake_image, _ = self.generate_fake(data['image'])
            return fake_image
        elif mode == 'manipulate':
            with torch.no_grad():
                fake_image = self.manipulate(data['image'], ori_cap, new_cap, alpha, global_edit=global_edit)
            return fake_image
        elif mode == 'optimize':
            g_loss, generated = self.optimizeP(data, ori_cap, new_cap, alpha, global_edit=global_edit)
            return g_loss, generated
        else:
            raise ValueError("|mode| is invalid")

    def create_P_optimizers(self, opt):
        P_params = list(self.netP.parameters())

        beta1, beta2 = 0, 0.9
        P_lr = opt.lr

        optimizer_P = torch.optim.Adam(P_params, lr=P_lr, betas=(beta1, beta2))

        return optimizer_P

    def create_optimizers(self, opt):
        G_params = list(self.netG.parameters())
        if opt.isTrain and self.loss_gan:
            D_params = list(self.netD.parameters())

        if opt.no_TTUR:
            beta1, beta2 = opt.beta1, opt.beta2
            G_lr, D_lr = opt.lr, opt.lr
        else:
            beta1, beta2 = 0, 0.9
            G_lr, D_lr = opt.lr / 2, opt.lr * 2

        optimizer_G = torch.optim.Adam(G_params, lr=G_lr, betas=(beta1, beta2))
        if self.loss_gan and opt.isTrain:
            optimizer_D = torch.optim.Adam(D_params, lr=D_lr, betas=(beta1, beta2))
        else:
            optimizer_D = None

        return optimizer_G, optimizer_D

    def save(self, epoch):
        util.save_network(self.netG, 'G', epoch, self.opt)
        if self.loss_gan:
            util.save_network(self.netD, 'D', epoch, self.opt)

    def initialize_networks(self, opt):
        print(opt.isTrain)
        netG = networks.define_G(opt)
        netD = networks.define_D(opt) if opt.isTrain and not opt.no_disc else None
        netE = networks.define_E(opt)

        if not opt.isTrain or opt.continue_train or opt.manipulation:
            netG = util.load_network(netG, 'G', opt.which_epoch, opt)
            if opt.isTrain or opt.needs_D:
                netD = util.load_network(netD, 'D', opt.which_epoch, opt)
                print('network D loaded')

        return netG, netD, netE

    def compute_generator_loss(self, data):
        G_losses = {}

        fake_image, spatial_embedding = self.generate_fake(data['image'])

        if self.loss_l1pix:
            G_losses['Pix'] = self.criterionPix(fake_image, data['image'])
        if self.loss_gan:
            pred_fake, pred_real = self.discriminate(fake_image, data)

        if self.loss_ganfeat:
            actual_num_D = 0
            num_D = len(pred_fake)
            GAN_Feat_loss = self.FloatTensor(1).fill_(0)
            for i in range(num_D):  # for each discriminator
                # last output is the final prediction, so we exclude it
                num_intermediate_outputs = len(pred_fake[i]) - 1
                if num_intermediate_outputs == 0:
                    continue
                else:
                    actual_num_D += 1
                for j in range(num_intermediate_outputs):  # for each layer output
                    unweighted_loss = self.criterionFeat(
                        pred_fake[i][j], pred_real[i][j].detach())
                    GAN_Feat_loss += unweighted_loss * self.opt.lambda_feat
            G_losses['GAN_Feat'] = GAN_Feat_loss / actual_num_D

        if self.loss_gan:
            G_losses['GAN'] = self.criterionGAN(pred_fake, True,
                                                for_discriminator=False)

        if self.loss_vgg:
            G_losses['VGG'] = self.criterionVGG(fake_image, data['image']) \
                * self.opt.lambda_vgg

        return G_losses, fake_image

    def compute_discriminator_loss(self, data):
        D_losses = {}
        with torch.no_grad():
            fake_image, spatial_embedding = self.generate_fake(data['image'])

        pred_fake, pred_real = self.discriminate(fake_image, data)

        D_losses['D_Fake'] = self.criterionGAN(pred_fake, False,
                                               for_discriminator=True)
        D_losses['D_real'] = self.criterionGAN(pred_real, True,
                                               for_discriminator=True)

        return D_losses

    def generate_fake(self, real_image, return_skip = False, return_edge = False):

        with torch.no_grad():
            spatial_embedding, edge = self.netE(real_image)
        fake_image = self.netG(spatial_embedding, edge)
        if return_edge:
            return fake_image, spatial_embedding, edge
        else:
            return fake_image, spatial_embedding

    def generate_fake_withP(self, real_image):
        with torch.no_grad():
            spatial_embedding, edge = self.netE(real_image)
        p, P_reg = self.netP()
        fake_image = self.netG(spatial_embedding, edge, perturbation=True, p=p)

        return fake_image, P_reg

    def discriminate(self, fake_image, data, edge=None):

        fake_and_real_img = torch.cat([fake_image, data['image']], dim=0)

        discriminator_out = self.netD(fake_and_real_img)

        pred_fake, pred_real = self.divide_pred(discriminator_out)

        return pred_fake, pred_real

    def manipulate(self, real_image, ori_txt, new_txt, alpha, global_edit=False):

        spatial_embedding, edge = self.netE(real_image, norm=True)

        with torch.no_grad():
            ori_txt = self.txt_enc(ori_txt, [ori_txt.shape[1]])
            new_txt = self.txt_enc(new_txt, [new_txt.shape[1]])

        proj = spatial_embedding * ori_txt.unsqueeze(2).unsqueeze(3).repeat(1,1,spatial_embedding.shape[2],spatial_embedding.shape[3])
        proj_s = proj.sum(1, keepdim=True)
        proj = proj_s.repeat(1,1024,1,1)
        # proj = F.sigmoid(proj)

        if global_edit:
            proj[:] = 1 # for global attributes, don't need to do grounding

        spatial_embedding = spatial_embedding - alpha * proj * ori_txt.unsqueeze(2).unsqueeze(3).repeat(1,1,spatial_embedding.shape[2],spatial_embedding.shape[3])
        spatial_embedding = spatial_embedding + alpha * proj * new_txt.unsqueeze(2).unsqueeze(3).repeat(1,1,spatial_embedding.shape[2],spatial_embedding.shape[3])

        spatial_embedding = l2norm(spatial_embedding)

        if self.perturbation:
            p, P_reg = self.netP()
            fake_image = self.netG(spatial_embedding, edge, perturbation=True, p=p)
            return fake_image
        else:
            fake_image = self.netG(spatial_embedding, edge)
            return fake_image

    def optimizeP(self, data, ori_cap, new_cap, alpha, global_edit=False):

        P_losses = {}

        fake_image, P_reg = self.generate_fake_withP(data['image'])

        manip_image = self.manipulate(data['image'], ori_cap, new_cap, alpha, global_edit=global_edit)
        cycle_image = self.manipulate(manip_image, new_cap, ori_cap, alpha, global_edit=global_edit)

        P_losses['Pix'] = self.criterionPix(fake_image, data['image'])
        P_losses['VGG'] = self.criterionVGG(fake_image, data['image']) * self.opt.lambda_vgg

        P_losses['cyclePix'] = self.criterionPix(cycle_image, data['image'])
        P_losses['cycleVGG'] = self.criterionVGG(cycle_image, data['image']) * self.opt.lambda_vgg

        P_losses['Reg'] = P_reg

        return P_losses, fake_image


'''
@author: Andrew
Created on Jan 6, 2013
Last modified Feb 17, 2014
'''

import numpy as n
from numpy.fft import fft, ifft, fftshift
import correlation as corr

def diffusion(obj):
    """
    @param obj: An object which has cg_positions, cg_velocities and cg_forces attribites.
                This is very convienient as both the System and the Timestep objects
                have these attributes.
    @return: an n x n of diffusion coefficients, where n_cg is the dimensionality
    of given velocity (or force) arrays.

    """

    #

    cg_shape = obj.cg_positions.shape
    return n.diagflat(n.ones(cg_shape[1]*cg_shape[3])*stokes(obj.temperature, r=5))

def diff_from_vel(obj):
    """
    @param src: nensemble * nsubsystem * nframe * ncg
    @return: the diffusion tensor
    """
    avg_velocities = n.mean(obj.cg_velocities, axis = 0)
    Ncg = avg_velocities.shape[2]
    dt = obj.dt

    dtensor = n.zeros((avg_velocities.shape[0]*Ncg, avg_velocities.shape[0]*Ncg), 'f')

    for ss in n.arange(avg_velocities.shape[0]):
        for cg in n.arange(Ncg):
            dtensor[ss*Ncg + cg, ss*Ncg + cg] = diff_coef_from_corr(avg_velocities[ss,:,cg],avg_velocities[ss,:,cg],dt)

    return dtensor

def diff_coef_from_corr(vi, vj, dt, int_points = 4):
    """
    calculate the diffusion coefficient for a pair of time series vi, vj
    @param vi: an n * nt array of time series
    @param vj: an n * nt array of time series
    sampled at interval dt
    """

    # the velocity correlation function for a time average.
    corr = n.min(n.array([vi.shape[1],vj.shape[1]]))

    for i in n.arange(vi.shape[0]):
        corr += corr.FFT_Correlation(vi[i,:],vj[i,:])

    # average correlation func
    corr /= float(vi.shape[0])

    # inaccurate integration (for only 4 points)
    # Best to use orthogonal polynomials for fitting
    # the ACs, but for now keep this for comparison
    # with snw.
    return n.trapz(corr[:int_points],dx=dt)


import sqlite3

class Sqlite3ORM:
    fields = []
    dbpath = ""
    dbtable = ""
    create = ""

    def __init__(self):
        raise NotImplementedError

    def save(self):
        if self.pk is None:
            self._insert()
        else:
            self._update()

    @classmethod
    def _create_insert(cls):
        columnlist = ", ".join(cls.fields)
        qmarks = ", ".join("?" for val in cls.fields)
        SQL = """ INSERT INTO {tablename} ({columnlist})
        VALUES ({qmarks}) """
        return SQL.format(tablename=cls.dbtable, columnlist=columnlist, qmarks=qmarks)

    def _insert(self):
        with sqlite3.connect(self.dbpath) as conn:
            cur = conn.cursor()
            SQL = self._create_insert()
            propvals = [getattr(self, propname) for propname in self.fields]

            cur.execute(SQL, propvals)
            self.pk = cur.lastrowid

    @classmethod
    def _create_update(cls):
        """ TODO: IMPLEMENT THIS. Return a generic UPDATE SQL command like
        _create_insert did. You will want to be updating WHERE pk = ? """
        # field=?
        update_column_list = ", ".join(field+"=?" for field in cls.fields)
        SQL = """
UPDATE {tablename} SET {update_column_list} WHERE pk = ?; """
        return SQL.format(tablename=cls.dbtable, update_column_list=update_column_list)

    def _update(self):
        with sqlite3.connect(self.dbpath) as conn:
            cur = conn.cursor()
            SQL = self._create_update()
            propvals = [getattr(self, field) for field in self.fields + ["pk"]]
            cur.execute(SQL, propvals)

    @classmethod
    def one_where(cls, whereclause="TRUE", values=tuple()):
        SQL = "SELECT * FROM {tablename} WHERE " + whereclause
        with sqlite3.connect(cls.dbpath) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.cursor()

            cur.execute(SQL.format(tablename=cls.dbtable), values)
            row = cur.fetchone()
            if row is None:
                return None
            return cls(**row)

    @classmethod
    def many_where(cls, whereclause="TRUE", values=tuple()):
        """ equivalent of one_where but with fetchall, returns a list of objects or an
        empty list """
        SQL = "SELECT * FROM {tablename} WHERE " + whereclause
        with sqlite3.connect(cls.dbpath) as conn:
            conn.row_factory = sqlite3.Row
            cur = conn.cursor()

            cur.execute(SQL.format(tablename=cls.dbtable), values)
            rows = cur.fetchall()
            return [cls(**row) for row in rows]

    @classmethod
    def from_pk(cls, pk):
        return cls.one_where("pk=?", (pk,))

    @classmethod
    def all(cls):
        """ return a list of every row in the table as instances of the class """
        return cls.many_where()

    def delete(self):
        SQL = "DELETE FROM {tablename} WHERE pk=?"
        with sqlite3.connect(self.dbpath) as conn:
            cur = conn.cursor()
            cur.execute(SQL.format(tablename=self.dbtable), (self.pk,))
            self.pk = None

    def __repr__(self):
        reprstring = "<{cname} {fieldvals}>"
        # read over this:
        fieldvals = " ".join("{key}:{value}".format(key=key, value=getattr(self, key))
                             for key in ["pk", *self.fields])
        cname = type(self).__name__
        return reprstring.format(cname=cname, fieldvals=fieldvals)


import face_recognition
from fer import FER
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
detector = FER(mtcnn=True)
font = cv2.FONT_HERSHEY_DUPLEX

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

navaneeth_image = face_recognition.load_image_file("navaneeth.jpg")
navaneeth_face_encoding = face_recognition.face_encodings(navaneeth_image)[0]

# Load a second sample picture and learn how to recognize it.
arjun_image = face_recognition.load_image_file("arjun.jpg")
arjun_face_encoding = face_recognition.face_encodings(arjun_image)[0]

known_face_encodings = [
    navaneeth_face_encoding,
    arjun_face_encoding
]
known_face_names = [
    "navaneeth",
    "arjun"
]

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
	ret, frame = cap.read()
	small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
	# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
	rgb_frame = small_frame[:, :, ::-1]

	if process_this_frame:
		# Find all the faces and face encodings in the current frame of video
		face_locations = face_recognition.face_locations(rgb_frame)
		face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

		face_names = []
		for face_encoding in face_encodings:
			# See if the face is a match for the known face(s)
			matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
			name = "Unknown"

			# Or instead, use the known face with the smallest distance to the new face
			face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
			best_match_index = np.argmin(face_distances)
			if matches[best_match_index]:
				name = known_face_names[best_match_index]

			face_names.append(name)

	process_this_frame = not process_this_frame

	# Display the results
	for (top, right, bottom, left), name in zip(face_locations, face_names):

		# face_frame = frame[(bottom*4)+35:(top*4)-45,(left*4)-25:(right*4)+35]
		# Scale back up face locations since the frame we detected in was scaled to 1/4 size
		top *= 4
		right *= 4
		bottom *= 4
		left *= 4

		# Draw a box around the face
		cv2.rectangle(frame, (left-25, top-45), (right+35, bottom+35), (0, 0, 255), 2)

		try:
			face_frame = frame[top-45:bottom+35,left-25:right+35]
			face = cv2.resize(face_frame, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA)
			emotion, score = detector.top_emotion(face)
			print(emotion,"\t",score)
			cv2.putText(frame, name+":"+emotion, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
		except:
			# print("No Face")
			cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

	# Display the resulting image
	cv2.imshow('Video', frame)

	# Hit 'q' on the keyboard to quit!
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break

# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python
"""
Taken from -- kupfer      A convenient access and command tool

Copyright 2007--2010 Ulrik Sverdrup <ulrik.sverdrup@gmail.com>

This source/ is a standalone program that can be used together
with luancher.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import os
import sys
import xml.etree.cElementTree as ElementTree

NEEDED_KEYS= set(("title", "artist", "album", "track-number", "location", ))
UNICODE_KEYS = set(("title", "artist", "album"))

def _tounicode(bstr):
	# the XML parser returns `str' only when it's ascii, but we want
	# unicode objects all the time
	if isinstance(bstr, unicode):
		return bstr
	return bstr.decode("ascii")

def _lookup_string(string, strmap):
	"""Look up @string in the string map,
	and return the copy in the map.

	If not found, update the map with the string.
	"""
	string = string or ""
	try:
		return strmap[string]
	except KeyError:
		strmap[string] = string
		return string

def get_rhythmbox_songs(dbfile, typ="song", keys=NEEDED_KEYS):
	"""Return a list of info dictionaries for all songs
	in a Rhythmbox library database file, with dictionary
	keys as given in @keys.
	"""
	rhythmbox_dbfile = os.path.expanduser(dbfile)

	lSongs = []
	strmap = {}

	# Parse with iterparse; we get the elements when
	# they are finished, and can remove them directly after use.

	for event, entry in ElementTree.iterparse(rhythmbox_dbfile):
		if not (entry.tag == ("entry") and entry.get("type") == typ):
			continue
		info = {}
		for child in entry.getchildren():
			if child.tag in keys:
				if child.tag in UNICODE_KEYS:
					childtext = _tounicode(child.text)
				else:
					childtext = child.text
				tag = _lookup_string(child.tag, strmap)
				text = _lookup_string(childtext, strmap)
				info[tag] = text
		lSongs.append(info)
		entry.clear()
	return lSongs

def sort_album(album):
	"""Sort album in track order"""
	def get_track_number(rec):
		try:
			tnr = int(rec["track-number"])
		except (KeyError, ValueError):
			tnr = 0
		return tnr
	album.sort(key=get_track_number)

def sort_album_order(songs):
	"""Sort songs in order by album then by track number

	>>> songs = [
	... {"title": "a", "album": "B", "track-number": "2"},
	... {"title": "b", "album": "A", "track-number": "1"},
	... {"title": "c", "album": "B", "track-number": "1"},
	... ]
	>>> sort_album_order(songs)
	>>> [s["title"] for s in songs]
	['b', 'c', 'a']
	"""
	def get_album_order(rec):
		try:
			tnr = int(rec["track-number"])
		except (KeyError, ValueError):
			tnr = 0
		return (rec["album"], tnr)
	songs.sort(key=get_album_order)

def parse_rhythmbox_albums(songs):
	albums = {}
	for song in songs:
		song_artist = song["artist"]
		if not song_artist:
			continue
		song_album = song["album"]
		if not song_album:
			continue
		album = albums.get(song_album, [])
		album.append(song)
		albums[song_album] = album
	# sort album in track order
	for album in albums:
		sort_album(albums[album])
	return albums

def parse_rhythmbox_artists(songs):
	artists = {}
	for song in songs:
		song_artist = song["artist"]
		if not song_artist:
			continue
		artist = artists.get(song_artist, [])
		artist.append(song)
		artists[song_artist] = artist
	# sort in album + track order
	for artist in artists:
		sort_album_order(artists[artist])
	return artists

def out(msg, fil):
	fil.write(msg.encode("UTF-8"))

def usecache(cachefile, datafile):
	try:
		cstat = os.stat(cachefile)
		dstat = os.stat(datafile)
		return cstat.st_mtime >= dstat.st_mtime
	except OSError:
		return False

if __name__ == '__main__':
	rhcache = "./rhcache"
	fil = os.path.expanduser("~/.local/share/rhythmbox/rhythmdb.xml")

	if not usecache(rhcache, fil):
		sys.stderr.write("Parsing '%s'\n" % fil)
		songs = get_rhythmbox_songs(fil)
		outfile = open("./rhcache", "w")
		for song in songs:
			out(
u"""Bookmark {
  url = "%s",
  name = [==[%s]==],
  description = [=[From %s by %s]=],
  icon_name ="audio-x-generic",
}
""" % (song["location"], song["title"], song["album"], song["artist"]),
			fil=outfile)
		outfile.close()
	sys.stdout.write(open(rhcache).read())
	sys.exit(0)


__copyright__ = "Copyright (c) 2020 Jina AI Limited. All rights reserved."
__license__ = "Apache-2.0"

import ctypes
import random
from typing import Iterator, Union

from ...helper import batch_iterator
from ...proto import jina_pb2

def _generate(data: Union[Iterator[bytes], Iterator['jina_pb2.Document']], batch_size: int = 0,
              first_doc_id: int = 0, first_request_id: int = 0,
              random_doc_id: bool = False, mode: str = 'index', top_k: int = 50,
              in_proto: bool = False,
              *args, **kwargs) -> Iterator['jina_pb2.Message']:
    for pi in batch_iterator(data, batch_size):
        req = jina_pb2.Request()
        req.request_id = first_request_id

        if mode == 'search':
            if top_k <= 0:
                raise ValueError('"top_k: %d" is not a valid number' % top_k)
            else:
                req.search.top_k = top_k

        for raw_bytes in pi:
            d = getattr(req, mode).docs.add()
            if in_proto:
                d.CopyFrom(raw_bytes)
            else:
                d.raw_bytes = raw_bytes
            d.doc_id = first_doc_id if not random_doc_id else random.randint(0, ctypes.c_uint(-1).value)
            d.weight = 1.0
            first_doc_id += 1
        yield req
        first_request_id += 1

def index(*args, **kwargs):
    """Generate indexing request"""
    yield from _generate(*args, **kwargs)

def train(*args, **kwargs):
    """Generate training request """
    yield from _generate(*args, **kwargs)
    req = jina_pb2.Request()
    req.request_id = 1
    req.train.flush = True
    yield req

def search(*args, **kwargs):
    """Generate search request """
    yield from _generate(*args, **kwargs)


import os
from os import path
import json
from datetime import date

import numpy as np
import matplotlib.pyplot as plt

dataset_dir = "../fakenewsnet_dataset/politifact/fake"
dataset_dir = path.abspath( path.join(path.dirname(__file__), dataset_dir))

DATE_MAX = date(2095, 12, 31)
DATE_MIN = date(1995, 2, 5)

# convert month string to number
def month_str_to_num(month_string):
    month_dict = {
        "Jan": 1,
        "Feb": 2,
        "Mar": 3,
        "Apr": 4,
        "May": 5,
        "Jun": 6,
        "Jul": 7,
        "Aug": 8,
        "Sep": 9,
        "Oct": 10,
        "Nov": 11,
        "Dec": 12
    }

    if month_string in month_dict:
        return month_dict[month_string]
    else:
        return -1

def parse_tweet_date(date_string):

    # example of date string: "Fri Dec 08 17:08:28 +0000 2017"
    y = int(date_string.split(" ")[5])
    m = month_str_to_num(date_string.split(" ")[1])
    assert m >= 1
    d = int(date_string.split(" ")[2])

    return date(y, m, d)

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{:.3f}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

# [STATISTICS] results
NEWS_COUNT = 0
LIFESPAN_PER_CASCADE = [] # lifespan of all cascades
LIFESPAN_PER_CASCADE_RT = [] # lifespan of cascades having Replies / Quotes / Retweets
LIFESPAN_PER_NEWS = []

CASCADE_SIZE = []

# iterate all news
for item in os.listdir(dataset_dir):

    news_dir_path = f"{dataset_dir}/{item}"

    if not path.isdir(news_dir_path):
        continue

    # check existence of news article
    if not path.exists(f"{news_dir_path}/news content.json"):
        continue

    # check existence of cascade file
    if not path.exists(f"{news_dir_path}/cascade.json"):
        continue

    NEWS_COUNT += 1
    print(f"{NEWS_COUNT}: {item}")

    # if NEWS_COUNT > 5:
    #     break

    news_post_count = 0

    with open(f"{news_dir_path}/cascade.json", "r") as cascade_file:
        forest = json.loads(cascade_file.read())

        news_first_date = DATE_MAX
        news_last_date = DATE_MIN

        for cascade in forest:

            cascade_first_date = DATE_MAX
            cascade_last_date = DATE_MIN

            post_count = 0

            # collect IDs from source tweet, replies, quotes using BFS
            i = 0
            tweet_ids = []
            bfs_queue = [cascade]
            while i < len(bfs_queue):
                traversed = bfs_queue[i]
                tweet_ids.append(traversed['id'])

                with open(f"{news_dir_path}/tweets/{traversed['id']}.json", "r") as tweet_file:
                    tweet = json.loads(tweet_file.read())
                    created_at = parse_tweet_date(tweet['created_at'])

                    # update last / first date of cascade / news
                    news_first_date = min(created_at, news_first_date)
                    news_last_date = max(created_at, news_last_date)
                    cascade_first_date = min(created_at, cascade_first_date)
                    cascade_last_date = max(created_at, cascade_last_date)

                if len(traversed['quoted_by']) > 0:
                    bfs_queue += traversed['quoted_by']

                if len(traversed['replied_by']) > 0:
                    bfs_queue += traversed['replied_by']

                i += 1

            # length of tweet_ids is #(source tweet + replies + quites)
            post_count += len(tweet_ids)

            for id in tweet_ids:
                with open(f"{news_dir_path}/retweets/{id}.json", "r") as retweet_list_file:
                    retweet_list = json.loads(retweet_list_file.read())

                    post_count += len(retweet_list['retweets'])
                    for retweet in retweet_list['retweets']:
                        created_at = parse_tweet_date(retweet['created_at'])

                        # update last / first date of cascade / news
                        news_first_date = min(created_at, news_first_date)
                        news_last_date = max(created_at, news_last_date)
                        cascade_first_date = min(created_at, cascade_first_date)
                        cascade_last_date = max(created_at, cascade_last_date)

            cascade_lifespan = (cascade_last_date - cascade_first_date).days # calculated in days

            if post_count > 1:
                LIFESPAN_PER_CASCADE_RT.append(cascade_lifespan)

            LIFESPAN_PER_CASCADE.append(cascade_lifespan)
            CASCADE_SIZE.append(post_count)

            if post_count == 1 and cascade_lifespan > 0:
                print(f"lifespan: {cascade_lifespan} days, start: {cascade_first_date}, end: {cascade_last_date}")

        news_lifespan = (news_last_date - news_first_date).days # calculated in days
        LIFESPAN_PER_NEWS.append(news_lifespan)

#------------------------------------------PRINT RESULT------------------------------------------------

# [RESULT] lifespan per news
LIFESPAN_PER_NEWS = np.array(LIFESPAN_PER_NEWS)
print(f"[lifespan per news] min: {LIFESPAN_PER_NEWS.min()}, max: {LIFESPAN_PER_NEWS.max()}, average: {LIFESPAN_PER_NEWS.mean()}, median: {np.median(LIFESPAN_PER_NEWS)}")

bin_frequency = {
    '1 day': 0 ,
    '1 week': 0,
    '1 month': 0,
    'half year': 0,
    '1 year': 0,
    '> 1 year': 0
}

for n in LIFESPAN_PER_NEWS:
    if n <= 1:
        bin_frequency['1 day'] += 1
    elif n > 1 and n <= 7:
        bin_frequency['1 week'] += 1
    elif n > 7 and n <= 30:
        bin_frequency['1 month'] += 1
    elif n > 30 and n <= 180:
        bin_frequency['half year'] += 1
    elif n > 180 and n <= 365:
        bin_frequency['1 year'] += 1
    else:
        bin_frequency['> 1 year'] += 1

time_lengths = bin_frequency.keys()
news_number = np.array([bin_frequency[n] for n in time_lengths], dtype=np.float)
news_number = news_number / len(LIFESPAN_PER_NEWS)

fig, ax = plt.subplots()
bars = ax.bar(time_lengths, news_number, width=0.8)
autolabel(bars)
ax.set_ylabel('News Number'), ax.set_xlabel('Lifespan')
ax.set_title('Lifespan of News')
plt.show()

# [RESULT] lifespan per cascade
LIFESPAN_PER_CASCADE = np.array(LIFESPAN_PER_CASCADE)
print(f"[lifespan per cascade] min: {LIFESPAN_PER_CASCADE.min()}, max: {LIFESPAN_PER_CASCADE.max()}, average: {LIFESPAN_PER_CASCADE.mean()}, median: {np.median(LIFESPAN_PER_CASCADE)}")

bin_frequency = {
    '1 day': 0 ,
    '3 days': 0,
    '1 week': 0,
    'half month': 0,
    '1 month': 0,
    '> 1 month': 0
}

for n in LIFESPAN_PER_CASCADE:
    if n <= 1:
        bin_frequency['1 day'] += 1
    elif n > 1 and n <= 3:
        bin_frequency['3 days'] += 1
    elif n > 3 and n <= 7:
        bin_frequency['1 week'] += 1
    elif n > 7 and n <= 14:
        bin_frequency['half month'] += 1
    elif n > 14  and n <= 30:
        bin_frequency['1 month'] += 1
    else:
        bin_frequency['> 1 month'] += 1

time_lengths = bin_frequency.keys()
news_number = np.array([bin_frequency[n] for n in time_lengths], dtype=np.float)
news_number = news_number / len(LIFESPAN_PER_CASCADE)
fig, ax = plt.subplots()
bars = ax.bar(time_lengths, news_number, width=0.8)
autolabel(bars)
ax.set_ylabel('Cascade Number'), ax.set_xlabel('Lifespan')
ax.set_title('Lifespan of Cascade')
plt.show()

# [RESULT] lifespan per cascade
LIFESPAN_PER_CASCADE_RT = np.array(LIFESPAN_PER_CASCADE_RT)
print(f"[lifespan per cascade (>= 2 posts)] min: {LIFESPAN_PER_CASCADE_RT.min()}, max: {LIFESPAN_PER_CASCADE_RT.max()}, average: {LIFESPAN_PER_CASCADE_RT.mean()}, median: {np.median(LIFESPAN_PER_CASCADE_RT)}")

bin_frequency = {
    '1 day': 0 ,
    '3 days': 0,
    '1 week': 0,
    'half month': 0,
    '1 month': 0,
    '> 1 month': 0
}

for n in LIFESPAN_PER_CASCADE_RT:
    if n <= 1:
        bin_frequency['1 day'] += 1
    elif n > 1 and n <= 3:
        bin_frequency['3 days'] += 1
    elif n > 3 and n <= 7:
        bin_frequency['1 week'] += 1
    elif n > 7 and n <= 14:
        bin_frequency['half month'] += 1
    elif n > 14  and n <= 30:
        bin_frequency['1 month'] += 1
    else:
        bin_frequency['> 1 month'] += 1

time_lengths = bin_frequency.keys()
news_number = np.array([bin_frequency[n] for n in time_lengths], dtype=np.float)
news_number = news_number / len(LIFESPAN_PER_CASCADE_RT)

fig, ax = plt.subplots()
bars = ax.bar(time_lengths, news_number, width=0.8)
autolabel(bars)
ax.set_ylabel('Cascade Number'), ax.set_xlabel('Lifespan')
ax.set_title('Lifespan of Cascade(#post > 1)')
plt.show()

# [RESULT] cascade lifespan vs cascade size
plt.scatter(CASCADE_SIZE, LIFESPAN_PER_CASCADE, alpha=0.2)
plt.xlabel("Cascade Size")
plt.ylabel("Cascade Lifespan")
plt.title("Cascade: Size vs. Lifespan")
plt.show()


from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from threading import *
import logging

class myHandler(BaseHTTPRequestHandler):
    #Handler for the GET requests
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        if self.path == '/get_storage_tp':
            storage_tp = self.server.mon.get_storage_tp()
            self.wfile.write(json.dumps(storage_tp))
        elif self.path == '/date':
            self.wfile.write("Hello World !")
        else:
            self.wfile.write("Endpoint not supported!")

    def do_POST(self):
        length = self.headers.getheader('content-length')
        data = self.rfile.read(int(length))
        logging.info('Received data:%s', data)
        if self.path == '/newpod':
            self.server.mon.set_monitoring_pods(data)

class Listener:
    def __init__(self,port,mon):
        self._port=port
        self.server = HTTPServer(('', port), myHandler)
        self.server.mon = mon

    def start(self):
        logging.info("Listener is running on port %d", self._port)
        self.server.serve_forever()


def add_tw0_nums(x, y):  # normal function which returns data
    return x + y

def add_three_nums(x, y, z):  # normal function which returns data
    return x + y + z

def get_appropriate_function(num_len):  # function which returns functions depending on the logic
    if num_len == 3:
        return add_three_nums
    else:
        return add_tw0_nums

if __name__ == "__main__":
    args = [1, 2, 3]
    num_len = len(args)
    res_function = get_appropriate_function(num_len)
    print(res_function)       # <function add_three_nums at 0x7f8f34173668>
    print(res_function(*args))  # unpack the args, output 6

    args = [1, 2]
    num_len = len(args)
    res_function = get_appropriate_function(num_len)
    print(res_function)       # <function add_tw0_nums at 0x7f1630955e18>
    print(res_function(*args))  # unpack the args, output 3


from scipy.stats import zscore
from sklearn.metrics import confusion_matrix as conm
import numpy as np

def mvpa_applycrossvalclassifier(cfg:dict, model:dict, data:dict):

	'''
	cfg.fold = 10;
	cfg.classifier = 20; % How many classifiers? ex.3
	cfg.classifiernumber = 20; % Which classifier? [4 8 9]
	cfg.timebins = 115; % How many time bins?
	cfg.timebinsnumber = 3; % Which timebins? [4 8 9]
	cfg.category_predict = {'Face' 'Landmark' 'Object'};
	cfg.category_model = {'Face' 'Landmark' 'Object'};
	cfg.trials = 'selection';
	cfg.trialselection = 'trialinfo(:,3) == 1';

	selected the trails in the data that are going to be preditectd!!!!!!

	select observations based on cfg.trailinfo
	'''

	if cfg['trials'] == 'selection':#Check if trialselection exists for non selection
		indiceObs = data[cfg['trialselection']]
		cat_name = data['category_name'][indiceObs]
	else:
		cat_name = data['category_name']
		indiceObs = np.ones(len(cat_name))

	cat_name = cat_name[np.in1d(cat_name, cfg['category_predict'])]

	#Observation Indices for feature selection!
	indiceObs2 = np.in1d(data['category_name'], cfg['category_predict'])

	indiceObs = np.logical_and(indiceObs, indiceObs2)

	rc = len(cfg['category_model'])#num cats in the model!

	#Store the indices of the data['feature_name'] as dict, the lookup should be faster than to loop over in1d
	fLookup = dict((item, index) for index, item in enumerate(data['feature_name']))

	#Vectorize the lookup function to be able to retrieve multiple items at the same time, increasing time more
	arrLookup = np.vectorize(fLookup.__getitem__)

	def timebinFeatures(feature_tb):

		feature_tb = zscore(feature_tb[indiceObs])

		def classifier(classy):

			folds = classy['fold']

			confmatfinal = np.zeros((rc,rc))

			def folding(fold):

				#If we're gonna change the value of something out of scope we need to specify it by using nonlocal, this is however usually best to avoid. Even more so with the Global keyword.
				nonlocal confmatfinal

				if fold:
					#extract feature for that cassifier and fold
					#Should be faster to lookup the indices in this manner
					indiceFeature = arrLookup(fold['feature'])
					feature = feature_tb[:, indiceFeature]
					#precit!!!!
					label = fold['model'].predict(feature)
					#confusion matrix
					score = np.mean(label == cat_name)#Already predicted the labels, could just manually calculate the mean as we do here.

					confmat = conm(y_true = cat_name, y_pred = label, labels = np.unique(cat_name))

					#If we use the fact that confusion matrices have equal dimension length then the total size must be larger as well if one dimension is
					#and again np.size is faster than len, then we have the same results
					if np.size(confmat) < np.size(confmatfinal):
						id = np.flatnonzero(np.in1d(cfg['category_model'], label).T)
						rw = np.flatnonzero(confmat)[0]
						confmat_new = np.zeros((rc,rc))

						#vectorization of numpy arrays are faster if you can afford the memory because it's done in c instead of doing a slow python for loop
						# the equivalent of the vectorization is shown below
						confmat_new[rw, id[:]] = confmat[rw, :np.size(id)]
						#Above is equivalent to
						#for index, item in enumerate(id):
						#    confmat_new[rw, item] = confmat[rw, index]

						confmat = confmat_new

					confmatfinal += confmat

					return {'predict': label,
							'truelabel': cat_name,
							'feature': fold['feature'],
							'likelihood': score,
							'confmat': confmat}

				elif not fold:
					pass#Return None if fold is empty
				else:
					print('unkown error, model folds are not empty but couldnt execute')
					print(fold)
					exit()

			return {'fold': np.array(list(map(folding, folds)), dtype = dict)}, confmatfinal/10

		#zip repacks the returns: (1,2),(1,2),(1,2) becoms (1,1,1),(2,2,2) here
		predictedClass, predictedConfmatfinal = zip(*map(classifier, model['classifier']))

		return {'classifier': np.array(predictedClass, dtype = dict),
				'confmatfinal': np.array(predictedConfmatfinal)}#Usually best to let numpy decide datatype to decrease memory usage unless necessary with certain precision or certain datatype.

	return {'timebin': np.array(list(map(timebinFeatures, data['feature'])), dtype = dict)}

if __name__ == '__main__':
	'''
	Placeholder main function for fast profiling tests, or to see if working.
	Tests should however mainly be carried out using unittests. Please only use for executing a specific file or for profiling.
	'''
	import pickle
	import scipy.io as sio
	import time

	cdata = sio.loadmat(f'./Visual/Subj01/6-ClassificationData/Subj01_test_data_visual',
						chars_as_strings = True, simplify_cells = True)[f'Subj01_test_data_visual']

	file = open('./data/Visual/Subj01/7-ClassifierTraining/Subj01_study_crossvalclass.pkl', 'rb')
	crossdata = pickle.load(file)[f'Subj01_study_crossvalclass']#give classifier
	file.close()

	cfg = {'fold': 10,
			'classifiernumber': 20,
			'timebinsnumber': 20,
			'category_predict': np.array(['Face', 'Landmark', 'Object']),
			'trials': 'all',
			'category_model': np.array(['Face', 'Landmark', 'Object'])}
	t = time.time()
	predtest_visual = mvpa_applycrossvalclassifier(cfg = cfg, model = crossdata, data = cdata)
	print(time.time()-t)
	print(f'Subj01\nDone')

	with open('test.pkl', 'wb') as file:
		pickle.dump({f'Subj01_study_predtest_visual': predtest_visual},file)


def get_data_by_id(idcode):

    gender_num = idcode[0]
    by_num = idcode[1:3]
    bm_num = idcode[3:5]
    bd_num = idcode[5:7]
    region_num = idcode[7:10]
    chk_num = idcode[10]

    # if gender_num == '1':
    #     cent_id = '18'
    #     gend_id = 'Male'
    # elif gender_num == '2':
    #     cent_id = '18'
    #     gend_id = 'Female'
    # if gender_num == '3':
    #     cent_id = '19'
    #     gend_id = 'Male'
    # elif gender_num == '4':
    #     cent_id = '19'
    #     gend_id = 'Female'
    # if gender_num == '5':
    #     cent_id = '20'
    #     gend_id = 'Male'
    # elif gender_num == '6':
    #     cent_id = '20'
    #     gend_id = 'Female'

    if int(gender_num) % 2 == 0:
        gend_id = 'Female'
    else:
        gend_id = 'Male'

    if gender_num == '1' or gender_num == '2':
        cent_id = '18'
    elif gender_num == '3' or gender_num == '4':
        cent_id = '19'
    elif gender_num == '5' or gender_num == '6':
        cent_id = '20'

    if 1 <= int(region_num) <= 10:
        region_id = 'Kuressaare haigla'
    elif 11 <= int(region_num) <= 19:
        region_id = 'Tartu Ülikooli Naistekliinik'
    elif 21 <= int(region_num) <= 150:
        region_id = 'Ida-Tallinna keskhaigla, Pelgulinna sünnitusmaja (Tallinn)'
    elif 161 <= int(region_num) <= 220:
        region_id = 'Rapla haigla, Loksa haigla, Hiiumaa haigla (Kärdla)'
    elif 221 <= int(region_num) <= 270:
        region_id = 'Ida-Viru keskhaigla (Kohtla-Järve, endine Jõhvi)'
    elif 271 <= int(region_num) <= 370:
        region_id = 'Maarjamõisa kliinikum (Tartu), Jõgeva haigla'
    elif 371 <= int(region_num) <= 420:
        region_id = 'Narva haigla'
    elif 421 <= int(region_num) <= 470:
        region_id = 'Pärnu haigla'
    elif 471 <= int(region_num) <= 490:
        region_id = 'Haapsalu haigla'
    elif 491 <= int(region_num) <= 520:
        region_id = 'Järvamaa haigla (Paide)'
    elif 521 <= int(region_num) <= 570:
        region_id = 'Rakvere haigla, Tapa haigla'
    elif 571 <= int(region_num) <= 600:
        region_id = 'Valga haigla'
    elif 601 <= int(region_num) <= 650:
        region_id = 'Viljandi haigla'
    elif 651 <= int(region_num) <= 700:
        region_id = 'Lõuna-Eesti haigla (Võru), Põlva haigla'

    print(gender_num, by_num, bm_num, bd_num, region_num, chk_num)
    print('You are ' + gend_id)
    print('You were born on ' + bd_num + '.' + bm_num + '.' + cent_id + by_num)
    print('Region code is ' + region_num + ' - ' + region_id)
    user_menu()

def valid_id(idcode):

    # I astme kaal: 1 2 3 4 5 6 7 8 9 1
    summ_id_1 = 1 * int(idcode[0]) + 2 * int(idcode[1]) + 3 * int(idcode[2]) + 4 * int(idcode[3]) + 5 * int(idcode[4]) + 6 * int(idcode[5]) + 7 * int(idcode[6]) + 8 * int(idcode[7]) + 9 * int(idcode[8]) + 1 * int(idcode[9])
    rem_div_1 = summ_id_1 % 11
    val_1 = summ_id_1 - 11 * (summ_id_1 // 11)

    # II astme kaal: 3 4 5 6 7 8 9 1 2 3
    summ_id_2 = 3 * int(idcode[0]) + 4 * int(idcode[1]) + 5 * int(idcode[2]) + 6 * int(idcode[3]) + 7 * int(idcode[4]) + 8 * int(idcode[5]) + 9 * int(idcode[6]) + 1 * int(idcode[7]) + 2 * int(idcode[8]) + 3 * int(idcode[9])
    rem_div_2 = summ_id_2 % 11
    val_2 = summ_id_2 - 11 * (summ_id_2 // 11)

    # Test
    if rem_div_1 < 10 and rem_div_1 == int(idcode[10]) and val_1 == rem_div_1:
        print('The code was validated!')
    elif rem_div_2 == int(idcode[10]) and val_2 == rem_div_2 or rem_div_2 == 0:
        print('The code was validated!')
    else:
        print('The code did not pass validation!')

    user_menu()

def user_menu():
    user_choice = input('Please choose:\n1. Get data by ID code\n2. Check if ID is valid\n3. Exit\n--> ')
    if user_choice == '1':
        get_data_by_id(user_id)

    elif user_choice == '2':
        valid_id(user_id)
    elif user_choice == '3':
        quit()
    else:
        print('Choice out of range')
        user_menu()

condition = True
while condition:
    try:
        user_id = input('Please enter ID: ')
        int(user_id)
        if len(user_id) != 11:
            raise UserWarning
    except UserWarning:
        if len(user_id) < 11:
            print('Code is too short')
        elif len(user_id) > 11:
            print('Code is too long')
    except:
        print('Code you entered is not numeric')
    else:
        condition = False
        user_menu()