#
# Copyright (c) dushin.net  All Rights Reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#    * Neither the name of dushin.net nor the
#      names of its contributors may be used to endorse or promote products
#      derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY dushin.net ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL dushin.net BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import os
import gc
from ulog import logger
import uhttpd

CONTENT_TYPE_MAP = {
    '.txt': "text/plain",
    '.html': "text/html",
    '.xhtml': "application/xhtml+xml",
    '.js': "text/javascript",
    '.css': "text/css",
    '.jpg': "image/jpeg",
    '.jpeg': "image/jpeg",
    '.png': "image/png",
    '.gif': "image/gif",
}

class Handler:
    def __init__(self, root_path='/www', block_size=1024):
        if not self.exists(root_path) or self.try_listdir(root_path) is None:
            msg = "Root path {} is not a valid directory".format(root_path)
            raise Exception(msg)
        self._root_path = root_path
        self._block_size = block_size

    #
    # callbacks
    #

    def handle_request(self, http_request):
        #
        # We only support GET
        #
        verb = http_request['verb']
        if verb != 'get':
            raise uhttpd.BadRequestException("Unsupported HTTP verb: {}".format(verb))
        # the relative path is the path on the HTTP request stripped of the
        # prefix used to register the file handler
        relative_path = uhttpd.get_relative_path(http_request)
        # the effective path is the relative path with the root path
        # prefixed, and normalized to remove '.' and '..'
        absolute_path = self.effective_path(relative_path)
        #
        # If the path is forbidden, 403 out
        #
        if not self.is_prefix(self._root_path, absolute_path):
            logger.info(
                "FORBIDDEN {}".format(absolute_path))
            raise uhttpd.ForbiddenException(absolute_path)
        #
        # If the path doesn't exist, 404 out
        #
        if not self.exists(absolute_path):
            logger.info(
                "NOT_FOUND {}".format(absolute_path))
            raise uhttpd.NotFoundException(absolute_path)
        #
        # Otherwise, generate a file listing or a file
        #
        dirlist = self.try_listdir(absolute_path)
        if dirlist is not None:
            if not relative_path.endswith('/'):
                return self.redirect_response(http_request['path']+'/')
            index_path = absolute_path + "/index.html"
            if self.exists(index_path):
                response = self.create_file_response(index_path)
                logger.info("INDEX {}".format(index_path))
                return response
            else:
                logger.info("DIR {}".format(absolute_path))
                prefix = http_request['prefix']
                return self.create_dir_listing_response(http_request['prefix'],relative_path,dirlist)
        else:
            logger.info("FILE {}".format(absolute_path))
            return self.create_file_response(absolute_path)

    #
    # internal operations
    #

    def create_file_response(self, path):
        length, body = self.generate_file(path)
        suffix = self.get_suffix(path)
        if suffix in CONTENT_TYPE_MAP:
            content_type = CONTENT_TYPE_MAP[suffix]
        else:
            content_type = "application/octet-stream"
        return self.create_response(200, content_type, length, body)

    def generate_file(self, path):
        f = open(path, 'r')
        serializer = lambda stream: self.stream_file(stream, f)
        return self.file_size(path), serializer

    def create_buffer(self):
        gc.collect()
        size = self._block_size
        while True:
            if size < 1:
                raise Exception("Unable to allocate buffer")
            try:
                return bytearray(size)
            except MemoryError:
                size //= 2

    def stream_file(self, stream, f):
        buf = self.create_buffer()
        while True:
            n = f.readinto(buf)
            if n:
                stream.write(buf[:n])
            else:
                break

    def effective_path(self, path):
        full_path = "{}/{}".format(self._root_path, path).rstrip('/')
        components = full_path.split('/')
        tmp = []
        for component in components:
            if component == '':
                pass
            elif component == "..":
                tmp = tmp[:len(tmp) - 1]
            elif component == '.':
                pass
            else:
                tmp.append(component)
        return "/{}".format('/'.join(tmp))

    @staticmethod
    def try_listdir(path):
        try:
            return os.listdir(path)
        except OSError:
            pass

    @staticmethod
    def exists(path):
        try:
            os.stat(path)
            return True
        except OSError:
            return False

    def create_dir_listing_response(self, prefix, path, dirlist):
        length, body = self.generate_dir_listing(prefix, path, dirlist)
        return self.create_response(200, "text/html", length, body)

    def generate_dir_listing(self, prefix, path, dirlist):
        def write_dir_listing(stream):
            stream.write("
uhttpd/{}: Directory {}{}

".format(
                uhttpd.VERSION, prefix, path).encode('UTF-8'))
            if len(path) > 1:
                stream.write("
..
\n")
            for f in dirlist:
                stream.write("
{0}
\n".format(f).encode('UTF-8'))
                gc.collect()
            stream.write("
")
        return (None, lambda stream: write_dir_listing(stream))

    @staticmethod
    def redirect_response(newloc):
        return {
            'code': 302,
            'headers': {
                'Location': newloc
            }
        }

    @staticmethod
    def create_response(code, content_type, length, body):
        ret = {
            'code': code,
            'headers': {
                'Content-Type': content_type
            },
            'body': body
        }
        if length is not None:
            ret['Content-Length'] = length
        return ret

    @staticmethod
    def file_size(path):
        return os.stat(path)[6]

    @staticmethod
    def get_suffix(path):
        idx = path.rfind('.')
        return "" if idx == -1 else path[idx:]

    @staticmethod
    def is_prefix(prefix, str):
        return len(prefix) <= len(str) and str[:len(prefix)] == prefix


# -*- coding: utf-8 -*-
from functools import reduce
from sklearn.metrics.pairwise import cosine_similarity
import os,copy
import sys,json
import string
from nltk.stem.lancaster import LancasterStemmer
from nltk.tokenize import word_tokenize
sys.path.append("..")
from sklearn import feature_extraction
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from utils import preprocess_sen_new,load_json,split_Uppercase,remove_stopwords,dump_json
import numpy as np
def get_all_class_path(dir_parh):
    count_name_dict={}
    class_name_list={}
#    class_index_name={}
    class_count=0
    path_list=[]
    for root,j,k in os.walk(dir_parh):
        root=root.replace('\\',"/")
        for k_each in k:
#            print("k_each: ",root,root[-1],k_each)
            k_each=k_each.replace('\\',"/")
            if len(k_each.split("."))>2:
                continue
            class_name_list[k_each.split(".")[0]]=class_count
            count_name_dict[class_count]=k_each.split(".")[0]
#            class_index_name[]=
            class_count=class_count+1
            if root[-1]=="/":
                path_list.append(root+k_each)
#                print("path: ",root+k_each)
            else:
                path_list.append(root+"/"+k_each)
#                print("path: ",root+"/"+k_each)
    return  path_list,class_name_list,count_name_dict
'''
类信息：类名和类描述

每个方法API信息：方法描述，形参描述，返回值描述，形参名和方法名

is_signature  是否加入形参名，方法名
'''
def extrac_class_des(path,is_class_name_des=True,is_method_des=False,is_para_des=False,is_return_des=False,is_signature=False) :
#    print("path",path)

    json_data = load_json(path)
    class_all_des=""
    if is_class_name_des:
        class_name=split_Uppercase(json_data["class_name"])
        class_all_des=class_all_des+class_name+" "+json_data["class_description"]
    if is_method_des:#对每个方法提取相应的单元
        method_list=json_data["Methods"]
        for i,method in enumerate(method_list):
            class_all_des=class_all_des+" "+method["method_description"]+" "+split_Uppercase(method["method_name"])
            if is_return_des:
#                print("len: ",method['return_value']['return_description'])
                if len(method['return_value']['return_description'])>0:
                    class_all_des=class_all_des+" "+method['return_value']['return_description'][0]
            if is_para_des:
                if len(method['params'])>0:
                    for i,para in enumerate(method['params']):
                        class_all_des=class_all_des+" "+para["param_description"]
                        if is_signature:
                            class_all_des=class_all_des+" "+" ".join(para["param_name"])
#    print(class_all_des)
    return class_all_des
'''
对若干文档，TF-IDF计算
corpus是list，每一个元素是一个文档
'''
def Tfidf(corpus) :
#    print("corpus",corpus)
    vectorizer = CountVectorizer()
    transformer = TfidfTransformer()
    tfidf = transformer.fit_transform(vectorizer.fit_transform(corpus))

    word = vectorizer.get_feature_names() #所有文本的关键字
    weight = tfidf.toarray()              #对应的tfidf矩阵
#    print("word: ",len(word))
#    print("weight: ",weight,weight.shape)
    return weight,word
#    sFilePath = './tfidffile'
#    if not os.path.exists(sFilePath) :
#        os.mkdir(sFilePath)
#
#    # 这里将每份文档词语的TF-IDF写入tfidffile文件夹中保存
#    for i in range(len(weight)) :
#　　　　 print(u"--------Writing all the tf-idf in the",i,u" file into ",sFilePath+'/'+string.zfill(i,5)+'.txt',"--------")
#        f = open(sFilePath+'/'+string.zfill(i,5)+'.txt','w+')
#        for j in range(len(word)) :
#            f.write(word[j]+"    "+str(weight[i][j])+"\n")
#        f.close()
def cos_sim(v1, v2):
    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
'''
获得每篇文档的top_k个关键词
将所有文档的关键词进行合并
获得每篇文档的向量表示
生成相似度

'''
def get_all_key_words(weight,top_k):
    index_sort=copy.deepcopy(np.argsort(weight, axis=1)[:,::-1][:,:top_k])
    print(weight.shape)
    sigle_words_index=list(set(index_sort.flatten()))
#    sigle_words_index=list(set(reduce(lambda x,y: x+y, index_sort)))
#    for
    modify_weight=weight[:,sigle_words_index]
    print(modify_weight.shape)
    return modify_weight
#计算两个类的余弦相似矩阵
def cos_two_matrixs(matrix1,matrix2):
    import scipy.spatial as sp
    return 1 - sp.distance.cdist(matrix1, matrix2, 'cosine')
def modify_cos_by_keywords(cos_matrix,index_filter):
    return new_cos_matrix
#获得最相近的k个类
def get_topk_sim_class(top_k_class,sim_matrix):
    index_sort=copy.deepcopy(np.argsort(sim_matrix, axis=1)[:,::-1][:,:top_k_class])
    all_class_cos=[]
    for i,sim in enumerate(sim_matrix):
        cos_list=list(map(lambda x:sim[x] ,index_sort[i]))
        all_class_cos.append(cos_list)
    return index_sort,all_class_cos
'''
weight 每一行是一个文档   每一行的内容是每个词的tf-idf
index_sort 为每个类提取k个最大的tf-idf值
java_swift_words 存放重叠词的下标
java_swift_num 存放重叠的词的数目
java_swift_cos #存储向量的cos近似度

java_swift_map_index 相似度从大到小存储 每个java类对应的swift的类 下标
java_swift_map_words_index 按相似度从大到小存储 每个java类对应的swift的类的重叠词的下标 每个元素是一个列表 列表的每个元素

1.为每个文档提取k个关键词 index_sort(按tf-idf从大到小)
2.计算重叠的词的数目和重叠词的余弦值
这里的weight是java和swift的文档混合，java_num是分界点
for 每个java文档
    for 每个swift文档
        不同类的相交的关键词的数目
        不同类相交的关键词的向量列表
'''
def extract_keywords(top_k,java_weight,swift_weight):
#    index_sort=np.argsort(weight, axis=1)[::-1][:top_k]
#    print("java_weight.shape: ",java_weight.shape)
    swift_class_num=swift_weight.shape[0]
    java_class_num=java_weight.shape[0]
    index_java_sort=copy.deepcopy(np.argsort(java_weight, axis=1)[:,::-1][:,:top_k])
    index_swift_sort=copy.deepcopy(np.argsort(swift_weight, axis=1)[:,::-1][:,:top_k])
#    print("index_java_sort: ",index_java_sort.shape,index_swift_sort.shape)
    java_swift_num=np.zeros((java_class_num,swift_class_num))#存储重叠的词汇数目
    java_swift_cos=np.zeros((java_class_num,swift_class_num))#存储向量的cos近似度
    java_swift_words=[[] for i in range(len(java_weight))]#存储每个java类和swift类的重叠词汇的下标
    for i,java_class in enumerate(index_java_sort):
        for j,swift_class in enumerate(index_swift_sort):
            index_intersect=list(set(java_class)&set(swift_class))#存储相交的下标，即重叠的词汇
            if len(index_intersect)>0:
                java_swift_words[i].append(index_intersect)
                java_swift_num[i][j]=len(index_intersect)*-1
                java_vec=[java_weight[i][word_index] for word_index in index_intersect]
                swift_vec=[swift_weight[j][word_index] for word_index in index_intersect]
                java_swift_cos[i][j]=cos_sim(java_vec,swift_vec)*-1
            else:
                java_swift_words[i].append([])
                java_swift_num[i][j]=swift_class_num#这里设置成了最大值 ，实际应该是0
                java_swift_cos[i][j]=1#这里设置成了最大值1 ，实际应该是0
    java_swift_map_index=np.zeros((java_class_num,swift_class_num),dtype=int)#每一个元素存储swift的类下标
    java_swift_map_words_index=[]
    for row in range(java_swift_num.shape[0]):
        java_swift_map_index[row] = np.lexsort((java_swift_cos[row],java_swift_num[row]))
#        print("index: ",java_swift_words[row][0])
        java_swift_map_words_index.append( [java_swift_words[row][swift_index] for swift_index in java_swift_map_index[row]])
    return  index_java_sort,java_swift_map_index,java_swift_map_words_index
'''
1. 生成tf-idf的数据集，所有类文件路径
2. 训练tf-idf矩阵
3. 生成java到swift的类映射的排序
    确定每个类需要提取的关键词的数目
    按照关键词的重叠数目
    之后按照重叠关键词的余弦相似度
4. 对java类 提取前k个最相近的类写入文件
    映射的swift的类名+“------------”+关键词的数目 空格分开
'''
if __name__ == "__main__" :
#    (allfile,path) = getFilelist(sys.argv)

    all_path_list=[]
    java_class_name_list={}#存放tf-idf的每一行下标对应的类名  下标索引
    swift_class_name_list={}#存放tf-idf的每一行下标对应的类名  下标索引
    java_count_name_dict={}
    swift_count_name_dict={}
    java_dir_path="../android_api_doc_new/json_3_class_type/"#test
    swift_dir_path="../ios_api_doc/newest/all_2/"
    all_path_list,java_class_name_list,java_count_name_dict=get_all_class_path(java_dir_path)
    java_class_num=len(all_path_list)
    print("java_class_num: ",java_class_num)
    swift_path_list,swift_class_name_list,swift_count_name_dict=get_all_class_path(swift_dir_path)
    dump_json("../data/java_class_index.json",java_class_name_list)
    dump_json("../data/swift_class_index.json",swift_class_name_list)
    sim_matrixs=np.load("../data/tf_idf_class_map.npy")
    print("string map to string: ",sim_matrixs[java_class_name_list["String"]][swift_class_name_list["NSString"]])
    all_path_list.extend(swift_path_list)
    print("all_class_num: ",len(all_path_list))

    #制作文档集
    all_class_corpus=[]#每个元素是一个类
    for i,class_info_path in  enumerate(all_path_list):
        #def extrac_class_des(path,is_class_name_des=True,is_method_des=False,is_para_des=False,is_return_des=False,is_signature=False) :
        sen= extrac_class_des(class_info_path,True,True,True,True,True)
#        print("sen: ",sen)
        sen=preprocess_sen_new(sen,True)
#        print("sen_pre: ",sen)
        lancaster_stemmer = LancasterStemmer()
        input_str=word_tokenize(sen)
        sen=[lancaster_stemmer.stem(value) for value in input_str]
        sen=" ".join(remove_stopwords(sen))
#        print("sen: ",sen)
#        if i>2:
#            break
        all_class_corpus.append(sen)
#    all_class_corpus=["Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets. Without any other information, Swift creates an array that includes the specified values, automatically inferring the array’s Element type."
 #                      ,"Arrays are one of the most commonly used data types in an app. You use arrays to organize your app’s data. Specifically, you use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind of elements—from integers to strings to classes."]
#    for class_1 in all_class_corpus:
#        print("class: ",class_1)
#        break
    weight,word=Tfidf(all_class_corpus)

    top_k=10
    filter_weight=get_all_key_words(weight,top_k)
    sim_matrixs=cos_two_matrixs(np.array(filter_weight[:java_class_num]),np.array(filter_weight[java_class_num:]))
    np.save("../data/tf_idf_class_map.npy",sim_matrixs)
    map_java_list_name=["String","CharSequence","StringBuffer","StringBuilder","File","FileInputStream","FileOutputStream","ArrayList","LinkedList","Hashtable","HashSet"]

    top_k_class=10
    #获得与java最相近的10个swift 类
    index_sort,all_class_cos=get_topk_sim_class(top_k_class,sim_matrixs)
    #获得最相近的top_k_class的名字
    dir_class_map_path="../data/class_map_all_keywords/"
    java_index_list=list(map(lambda x: java_class_name_list[x],map_java_list_name ))
    swift_class_map_name_list=[]
    for i,each_java in enumerate(java_index_list):
        swift_class_map_name=list(map(lambda x: swift_count_name_dict[x],index_sort[each_java]))
        swift_class_map_name_list.append(swift_class_map_name)
        print("class: ",map_java_list_name[i],swift_class_map_name)
        print("sim_cos: ",all_class_cos[i])
        with open(dir_class_map_path+map_java_list_name[i]+".txt", 'w',encoding='utf-8', errors='ignore') as f_w:
            f_w.write(map_java_list_name[i]+"\n")
            for j in range(len(swift_class_map_name)):
                f_w.write(str(swift_class_map_name[j])+" "+str(all_class_cos[i][j])+"\n")

#            f_w.close()
#    map_java_list_name=java_class_name_list
##    map_java_list_name=["String","CharSequence","StringBuffer","StringBuilder","File","FileInputStream","FileOutputStream","ArrayList","LinkedList","Hashtable","HashSet"]
#    map_java_to_swift=[]
#    for i,value  in enumerate(map_java_list_name):
#        map_java_to_swift.append(list(weight[java_class_name_list[value]]))
#    java_key_words_index,java_swift_map_index,java_swift_map_words_index=extract_keywords(top_k,np.array(map_java_to_swift),np.array(weight[java_class_num:]))
##
#
#    dir_class_map_path="../data/class_map/"
#    like_class_num=20
#    for i,file_name in enumerate(map_java_list_name):
#        print("write file : ",file_name)
#        with open(dir_class_map_path+file_name+".txt", 'w',encoding='utf-8', errors='ignore') as f_w:
#            java_key_words=[]
#            for index in java_key_words_index[i]:
#                java_key_words.append(word[index])
#            f_w.write(" ".join(java_key_words)+"\n")
#            for j in range(like_class_num):
#                swift_index=java_swift_map_index[i][j]
#                key_words_list=[]
#                for key_word_index in java_swift_map_words_index[i][j]:
##                    print("java_swift_map_words_index[i][j]: ",java_swift_map_words_index[i][j],key_word_index)
#                    key_words_list.append(word[key_word_index])
#                f_w.write(swift_count_name_dict[swift_index]+": "+" ".join(key_words_list)+"\n")
##            f_w.close()
##    print("all_path_list: ",all_path_list)


import logging

from ESCGProject import app, database, paypal
#from ESCGProject import app, paypal
from flask import Flask, render_template, request, redirect, url_for, flash, session
from ESCGProject.forms import RegistrationForm, LoginForm, PaymentForm, WithdrawForm
from ESCGProject.utils import getImages, encrypt, decrypt

from ESCGProject.database import db_session, CheckUser, GetUser
from ESCGProject.models import *

from ESCGProject.paypal import MakeAPayment, PayOut

from sqlalchemy.exc import StatementError, SQLAlchemyError

import random
import os
import array

@app.route('/', methods=['GET', 'POST'])
def empty():
	if 'username' in session:
		return render_template('info.html')
	return redirect(url_for('login'))

@app.route('/CheckWinServer', methods=['GET', 'POST'])
def CheckWinServer():
	thecard = request.form['card_id']
	# Taken from https://www.youtube.com/watch?v=lsflaKpeB7Q
	inlist = [int(s) for s in thecard[1:-1].split(',')]
	decypted_card_id = decrypt(array.array('B', inlist).tostring())
	current_user = database.CheckCard(int(decypted_card_id),session['username'])
	balance = current_user.balance
	return str(balance)

@app.route('/login', methods=['GET', 'POST'])
def login():
	form = LoginForm(request.form)
	if request.method =='POST':
		overlay_img=url_for('static', filename='overlay.jpg')
		user = database.CheckUser(form.username.data,form.password.data)
		if not user:
			flash('User name or password incorrect')
			return render_template('login.html',form=form)
		else:
			session['username'] = form.username.data
			return render_template('info.html')
	return render_template('login.html',form=form)

@app.route('/logout', methods=['GET', 'POST'])
def logout():
	form = LoginForm(request.form)
	session.pop('username', None)
	return render_template('login.html',form=form)

@app.route('/main', methods=['GET', 'POST'])
def main():
	if 'username' in session:
		current_user = database.GetUser(session['username'])
		overlay_img=url_for('static', filename='overlay.jpg')
		return render_template('main.html',balance=current_user.balance,overlay_img=overlay_img)
	return redirect(url_for('login'))

@app.route('/register', methods=['GET', 'POST'])
def register():
	form = RegistrationForm(request.form)
	if request.method == 'POST' and form.validate():
		user = database.GetUser(form.username.data)
		if user is None:
			database.AddUser(form.username.data,form.email.data,form.password.data)
			flash('Thanks for registering')
			session['username'] = form.username.data
			return render_template('info.html')
		flash('Username already exists')
		return render_template('register.html', form=form)
	return render_template('register.html', form=form)

@app.route('/buyCard', methods=['GET', 'POST'])
def buyCard():
	if 'username' in session:
		current_user = database.GetUser(session['username'])
		if current_user.balance < 1:
			flash('You do not have enough funds')
			return render_template('main.html',balance=current_user.balance,overlay_img="./static/overlay.jpg")
		else:
			current_user.balance = current_user.balance - 1
			a_card = database.RetrieveCard()
			current_user = database.GetUser(session['username'])
			database.AddCardToUser(a_card.id,current_user.id)
			encrypted_card_id = encrypt(a_card.id)
			if a_card.value == 0:
				amount = random.choice([1,2,5,10,20])
				image_list = getImages(False,6)
			else:
				db_session.commit()
				image_list = getImages(True,6)
				amount = a_card.value
			return render_template('main.html',amount=amount,balance=current_user.balance,imagelist=image_list,card_id=encrypted_card_id)
	return redirect(url_for('login'))

@app.route('/Deposit', methods=['GET', 'POST'])
def DepositMoney():
	if 'username' in session:
		form = PaymentForm(request.form)
		if request.method == 'POST' and form.validate():
			current_user = database.GetUser(session['username'])
			payment_result = paypal.MakeAPayment(form.card_type.data,form.number.data,form.month.data,form.year.data,form.cvv2.data,form.firstname.data,form.lastname.data,form.totalAmount.data)
			if payment_result == True:
				topUp = form.totalAmount.data
				current_user.balance = current_user.balance + float(topUp)
				db_session.commit()
				return render_template('main.html',balance=current_user.balance,overlay_img="./static/overlay.jpg")
			else:
				flash("Payment unsuccessful")
				return render_template('main.html',balance=current_user.balance,overlay_img="./static/overlay.jpg")
		return render_template('deposit.html',form=form)
	return redirect(url_for('login'))

@app.route('/withdraw', methods=['GET', 'POST'])
def withdraw():
	if 'username' in session:
		form = WithdrawForm(request.form)
		current_user = database.GetUser(session['username'])
		if request.method == 'POST'  and form.validate():
			if int(form.totalAmount.data) > current_user.balance:
				flash("You do not have enough funds")
				return render_template('withdraw.html',balance=current_user.balance,form=form)
			payout_result = PayOut(current_user.email,form.totalAmount.data)
			print(payout_result)
			if payout_result == "SUCCESS":
				current_user.balance = current_user.balance - int(form.totalAmount.data)
				db_session.commit()
			else:
				flash("There has been a problem in the payout process. Please try again")
				return render_template('withdraw.html',balance=current_user.balance,form=form)
		return render_template('withdraw.html',balance=current_user.balance,form=form)
	return redirect(url_for('main'))

@app.route('/info', methods=['GET', 'POST'])
def info():
	if 'username' in session:
		return render_template('info.html')
	return redirect(url_for('login'))


import argparse

from app import convertFile, convertFolder

def main():
    parser = argparse.ArgumentParser(
    description='A Python CLI program for converting audio files to opus')
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-d', '--directory',
                       help='Converts whole directory of files')
    group.add_argument('-s', '--single', help='Converts single file')
    args = parser.parse_args()
    if args.directory:
        convertFolder(args.directory)
    elif args.single:
        convertFile(args.single)

if __name__ == '__main__':
    main()


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.7-x86_64/egg/sisua/inference/inference_scvi.py
# Compiled at: 2019-06-27 08:22:47
# Size of source mod 2**32: 15035 bytes
from __future__ import print_function, division, absolute_import
import io
from io import BytesIO
from collections import defaultdict
import numpy as np
from tqdm import tqdm
from odin.utils import batching, stdio, get_formatted_datetime
from .inference_base import Inference
from ._consts import PREDICTION_BATCH_SIZE
from sisua.data import get_library_size, apply_artificial_corruption
from sisua.data.const import UNIVERSAL_RANDOM_SEED
try:
    import torch
    torch_device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    torch.manual_seed(UNIVERSAL_RANDOM_SEED)
except ImportError:
    torch_device = None

def _create_gene_dataset(X, local_means=None, local_vars=None):
    from scvi.dataset import GeneExpressionDataset
    if local_means is None or local_vars is None:
        local_means, local_vars = get_library_size(X)
    col_names = np.array(['Gene#%d' % i for i in range(X.shape[1])])
    gene_dataset = GeneExpressionDataset(X=X,
      local_means=local_means,
      local_vars=local_vars,
      batch_indices=np.zeros(shape=(X.shape[0], 1)),
      labels=None,
      gene_names=col_names,
      cell_types=None)
    gene_dataset.labels = X
    gene_dataset.cell_types = col_names
    return gene_dataset

def _create_data_iter(X, log_variational):
    import torch
    for start, end in batching(batch_size=(PREDICTION_BATCH_SIZE // 3), n=(X.shape[0])):
        x = _to_tensor(X[start:end])
        if bool(log_variational):
            x = torch.log(1 + x)
        yield x

def _to_tensor(x):
    import torch
    return torch.from_numpy(x.astype('float32')).to(torch_device)

def _to_array(x):
    if hasattr(x, 'todense'):
        return np.array(x.todense())
    else:
        if hasattr(x, 'cpu'):
            return x.data.cpu().numpy()
        return x

def _use_cuda():
    import torch
    return torch.cuda.is_available()

class InferenceSCVI(Inference):
    __doc__ = ' InferenceSCVI '

    def __init__(self, gene_dim, dispersion='gene-cell', xnorm='log', tnorm='raw', ynorm='prob', xclip=0, yclip=0, xdist='zinb', ydist='bernoulli', zdist='normal', xdrop=0.3, edrop=0, zdrop=0, ddrop=0, hdim=128, zdim=32, nlayer=2, batchnorm=True, analytic=True, kl_weight=1.0, warmup=400, y_weight=10.0, extra_module_path=None, no_library_size=False, **kwargs):
        try:
            import scvi
        except ImportError as e:
            raise RuntimeError('scVI package at https://github.com/YosefLab/scVI is required')

        model = kwargs.get('model', 'vae')
        prot_dim = None
        if model not in ('vae', 'scvi'):
            raise RuntimeError("InferenceSCVI only support 'vae' model")
        if xdist not in ('zinb', 'nb'):
            raise ValueError("Only support 'zinb' and 'nb' for `xdist`")
        if xnorm not in ('log', 'raw'):
            raise ValueError('Only support log variational or raw count variational for scVI')
        if tnorm not in ('raw', ):
            raise ValueError('Only support raw count target value for scVI')
        self._name = 'scvinl' if no_library_size else 'scvi'
        self._history = {'train':defaultdict(list),
         'valid':defaultdict(list)}
        self._training_log = ''
        self._configs = dict(locals())
        self._configs.update(kwargs)
        self._gmm_threshold = {}
        self._gene_dim = int(gene_dim)
        self._prot_dim = 0 if prot_dim is None else max(int(prot_dim), 0)
        self._is_fitted = False
        self._ps = 0
        self._trained_n_epoch = 0
        self._batch_size = 0
        self._learning_rate = 0
        self._n_mcmc_train = 0
        self._corruption_rate = None
        self._corruption_dist = None
        self._initialize_model()
        self._no_library_size = bool(no_library_size)

    @property
    def no_library_size(self):
        return self._no_library_size

    def _initialize_model(self, weights=None):
        from scvi.models import VAE
        self._vae = VAE(n_input=(self.gene_dim), n_batch=0, n_labels=0, n_hidden=(self.hdim),
          n_latent=(self.zdim),
          n_layers=(self.nlayer),
          dropout_rate=(self.xdrop),
          dispersion=(self.dispersion),
          log_variational=(True if self.xnorm == 'log' else False),
          reconstruction_loss=(self.xdist))
        if weights is not None:
            data = BytesIO(weights)
            self._vae.load_state_dict(torch.load(data))
            self._vae.to(torch_device)

    def __getstate__(self):
        import torch
        states = self.get_states()
        weights = BytesIO()
        torch.save(self._vae.state_dict(), weights)
        weights.seek(0)
        weights = weights.read()
        return (states, weights, self._no_library_size)

    def __setstate__(self, states):
        states, weights, self._no_library_size = states
        self.set_states(states)
        self._initialize_model(weights=weights)

    def fit(self, X, y=None, supervised_percent=0.8, validation_percent=0.1, n_mcmc_samples=1, corruption_rate=0.25, corruption_dist='binomial', batch_size=64, n_epoch=120, learning_rate=0.0001, monitoring=False, fig_dir=None, detail_logging=False):
        training_log = io.StringIO()
        stdio(training_log)
        if self._corruption_rate is None:
            self._corruption_rate = corruption_rate
            self._corruption_dist = corruption_dist
        else:
            if not (self._corruption_rate == corruption_rate and self._corruption_dist == corruption_dist):
                raise AssertionError('Model have been fitted with following corruption configurations: %s, %s;but given: %s, %s' % (
                 self._corruption_rate, self._corruption_dist,
                 corruption_rate, corruption_dist))
            elif not n_mcmc_samples == 1:
                raise AssertionError('Only support 1 MCMC sample during training for scVI')
            self._ps = supervised_percent
            self._batch_size = batch_size
            self._learning_rate = learning_rate
            self._n_mcmc_train = n_mcmc_samples
            X = apply_artificial_corruption(X,
              dropout=corruption_rate, distribution=corruption_dist)
            assert X.shape[1] == self.gene_dim, 'Number of gene expression mismatch: %d != %d' % (self.gene_dim, X.shape[1])
        if validation_percent == 0:
            raise RuntimeError("scVI doesn't support 0% validation percent!")
        from scvi.inference import UnsupervisedTrainer
        gene_dataset = _create_gene_dataset(X)
        if 'dataset' in self.configs:
            if self.configs['dataset'] == 'cortex':
                batch_size = batch_size - 1
        trainer = UnsupervisedTrainer((self._vae), gene_dataset, use_cuda=(_use_cuda()),
          train_size=(1 - validation_percent),
          frequency=1,
          benchmark=False,
          data_loader_kwargs=dict(batch_size=batch_size),
          metrics_to_monitor=None,
          verbose=detail_logging)
        trainer.train(n_epochs=n_epoch, lr=learning_rate)
        self._scvi_trainer = trainer
        self._history['train']['loss'] += trainer.history['ll_train_set'][1:]
        self._history['valid']['loss'] += trainer.history['ll_test_set'][1:]
        self._trained_n_epoch += n_epoch
        self._is_fitted = True
        stdio()
        training_log.seek(0)
        self._training_log += '\n%s\n' % get_formatted_datetime(only_number=False)
        self._training_log += training_log.read()
        training_log.close()

    def get_train_loss(self):
        return self._history['train']['loss']

    def get_valid_loss(self):
        return self._history['valid']['loss']

    def get_train_history(self, name):
        return self._history['train'][name]

    def get_valid_history(self, name):
        return self._history['valid'][name]

    def score(self, X, y=None, n_mcmc_samples=100, return_mean=True):
        """Compute the per-sample average of recorded metrics of the given data X.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_dimensions)
        List of n_features-dimensional data points. Each row
        corresponds to a single data point.

    Returns
    -------
    dictionary : metric_name -> values
    """
        self._vae.eval()
        from scvi.inference import Posterior
        pos = Posterior(model=(self._vae), gene_dataset=(_create_gene_dataset(X)), shuffle=False,
          use_cuda=(_use_cuda()),
          data_loader_kwargs=dict(batch_size=(PREDICTION_BATCH_SIZE // 3)))
        ll = pos.ll(verbose=False)
        marginal_ll = pos.marginal_ll(verbose=False, n_mc_samples=n_mcmc_samples)
        return {'loss':ll,  'llk':marginal_ll}

    def predict_Z(self, X):
        """ Return mean of the latent posterior
    (i.e. mean of Normal distribution) """
        self._vae.eval()
        from scvi.inference import Posterior
        pos = Posterior(model=(self._vae), gene_dataset=(_create_gene_dataset(X)), shuffle=False,
          use_cuda=(_use_cuda()),
          data_loader_kwargs={'batch_size': 64})
        return pos.get_latent(sample=False)[0]

    def predict_y(self, X, y, n_mcmc_samples=100):
        """ Predicting the protein marker """
        assert self.is_semi_supervised and y is not None, 'Only semi-supervised model has this prediction, and `y` must not be None'

    def predict_L(self, X, n_mcmc_samples=100):
        """ Return library size prediction """
        self._vae.eval()
        L = []
        for x in _create_data_iter(X, log_variational=(self._vae.log_variational)):
            ql_m, ql_v, library = self._vae.l_encoder(x)
            L.append(_to_array(ql_m))

        return np.concatenate(L, axis=0)

    def predict_Lstddev(self, X, n_mcmc_samples=100):
        """ Return library size stddev prediction """
        self._vae.eval()
        Lstddev = []
        for x in _create_data_iter(X, log_variational=(self._vae.log_variational)):
            ql_m, ql_v, library = self._vae.l_encoder(x)
            Lstddev.append(_to_array(ql_v))

        return np.concatenate(Lstddev, axis=0)

    def predict_W(self, X, n_mcmc_samples=100):
        """ Return mean of the reconstructed value """
        self._vae.eval()
        W = []
        for x in _create_data_iter(X, log_variational=False):
            px_scale, px_r, px_rate, px_dropout, qz_m, qz_v, z, ql_m, ql_v, library = self._vae.inference(x,
              batch_index=None, y=None, n_samples=n_mcmc_samples)
            if self.no_library_size:
                px_rate = px_scale * x.sum(1).unsqueeze(1)
            w = (1 - px_dropout) * px_rate
            w = w.mean(0)
            W.append(_to_array(w))

        return np.concatenate(W, axis=0)

    def predict_Wstddev(self, X, n_mcmc_samples=100):
        """ Return stddev of reconstructed value
    if not a variational model, then V_stddev is None
    """
        self._vae.eval()
        import torch
        Wstddev = []
        for x in _create_data_iter(X, log_variational=False):
            px_scale, px_r, px_rate, px_dropout, qz_m, qz_v, z, ql_m, ql_v, library = self._vae.inference(x,
              batch_index=None, y=None, n_samples=n_mcmc_samples)
            w_mean = (1 - px_dropout) * px_rate
            w_var = (1 - px_dropout) * (px_r + px_rate ** 2) - w_mean ** 2
            w_std = torch.sqrt(w_var).mean(0)
            Wstddev.append(_to_array(w_std))

        return np.concatenate(Wstddev, axis=0)

    def predict_V(self, X, n_mcmc_samples=100):
        """ Return mean of the denoised value """
        self._vae.eval()
        V = []
        for x in _create_data_iter(X, log_variational=False):
            if self._no_library_size:
                out = self._vae.get_sample_scale(x, n_samples=n_mcmc_samples).mean(0)
                out = out * x.sum(1).unsqueeze(1)
            else:
                out = self._vae.get_sample_rate(x, n_samples=n_mcmc_samples).mean(0)
            V.append(_to_array(out))

        return np.concatenate(V, axis=0)

    def predict_Vstddev(self, X, n_mcmc_samples=100):
        """ Return stddev of denoised value
    if not a variational model, then V_stddev is None
    """
        self._vae.eval()
        Vstddev = []
        for x in _create_data_iter(X, log_variational=False):
            n = x.shape[0]
            out = self._vae.inference(x,
              batch_index=None, y=None, n_samples=n_mcmc_samples)[1]
            if self.dispersion == 'gene-cell':
                out = np.mean((_to_array(out)), axis=0)
            else:
                out = np.expand_dims((_to_array(out)), axis=0)
                out = np.repeat(out, repeats=n, axis=0)
            Vstddev.append(out)

        return np.concatenate(Vstddev, axis=0)

    def predict_PI(self, X, n_mcmc_samples=100):
        """ Return a matrix (n_sample, n_gene) of Zero-inflated
    rate

    if not a zero-inflated model, then return None
    """
        self._vae.eval()
        import torch
        op = torch.nn.Sigmoid()
        PI = []
        for x in _create_data_iter(X, log_variational=False):
            out = self._vae.inference(x,
              batch_index=None, y=None, n_samples=n_mcmc_samples)[3]
            out = op(out.mean(0))
            out = _to_array(out)
            PI.append(out)

        return np.concatenate(PI, axis=0)

    def sample_Z(self, X, n_mcmc_samples=1):
        self._vae.eval()
        from scvi.inference import Posterior
        pos = Posterior(model=(self._vae), gene_dataset=(_create_gene_dataset(X)), shuffle=False,
          use_cuda=(_use_cuda()),
          data_loader_kwargs={'batch_size': 64})
        return np.concatenate([np.expand_dims((pos.get_latent(sample=True)[0]), axis=0) for i in range(n_mcmc_samples)],
          axis=0)

    def sample_y(self, X, n_mcmc_samples=1):
        raise NotImplementedError

    def sample_W(self, X, n_mcmc_samples=1):
        raise NotImplementedError

    def sample_V(self, X, n_mcmc_samples=1):
        self._vae.eval()
        V = []
        for x in _create_data_iter(X, log_variational=False):
            out = self._vae.get_sample_rate(x, n_samples=n_mcmc_samples)
            V.append(_to_array(out))

        return np.concatenate(V, axis=1)

    def sample_L(self, X, n_mcmc_samples=1):
        raise NotImplementedError


import os
import sys
import pathlib
import pytest

# src/mainフォルダパスを追加し、src/mainフォルダ起点でインポートする(#402 Lint Error抑制と合わせて使用)
sys.path.append(os.path.join(
    str(pathlib.Path(__file__).resolve().parent.parent.parent), 'main'))
from stock.main.stock_crawler import KabutanCrawler  # noqa: #402

class TestKabutanCrawler():
    """
    KabutanCrawlerクラスのユニットテスト

    クローリングの自動テストは相手サーバーの負荷とテスト速度の観点から最低限に絞る
    """
    @pytest.mark.smoke
    def test_get_stock_price(self, application_logger):
        """
        存在する銘柄コードを指定した際に、期待している全てのカラムを持つ株価データを
        取得できることをテスト
        """
        stock_crawler = KabutanCrawler()
        stock_df = stock_crawler.get_stock_price('6028',
                                                 application_logger)
        assert len(stock_df) == 1
        assert 'Open' in stock_df.columns
        assert 'High' in stock_df.columns
        assert 'Low' in stock_df.columns
        assert 'Close' in stock_df.columns
        assert 'Volume' in stock_df.columns

    @pytest.mark.smoke
    def test_get_stock_price_none(self, application_logger):
        """
        存在しない銘柄コードを指定した際に、Noneが返されることをテスト
        """
        stock_crawler = KabutanCrawler()
        stock_df = stock_crawler.get_stock_price('NotExistCode',
                                                 application_logger)
        assert stock_df is None


import math
import numpy as np
from numpy.linalg import solve
from scipy.stats import f, t
from functools import partial
from random import randint
from prettytable import PrettyTable

# *********** Змінні за варіантом ***********
m = 3
N = 8

X1max = 40
X1min = 10
X2max = 35
X2min = -15
X3max = 5
X3min = -15

Xmax_average = (X1max + X2max + X3max) / 3
Xmin_average = (X1min + X2min + X3min) / 3

y_max = round(200 + Xmax_average)
y_min = round(200 + Xmin_average)

# *********** *********** *********** ***********

def kriteriy_fishera(y, y_aver, y_new, n, m, d):
    """
    Функція для знаходження критерія фішера
    """
    S_kv_ad = (m / (n - d)) * sum([(y_new[i] - y_aver[i])**2 for i in range(len(y))])
    S_kv_b = s_kv(y, y_aver, n, m)
    S_kv_b_aver = sum(S_kv_b) / n

    return S_kv_ad / S_kv_b_aver

def s_kv(y, y_aver, n, m):
    """
    Функція для знаходження квадратної дисперсії
    """
    res = []
    for i in range(n):
        s = sum([(y_aver[i] - y[i][j])**2 for j in range(m)]) / m
        res.append(s)
    return res

def getDispersion(y_rows) -> list:
    disp_array = [0] * 8

    for k in range(len(disp_array)):
        for i in range(m):
            disp_array[k] += ((np.average(y_rows[k]) - y_rows[k][i])**2) / m

    return disp_array

def getCodableX(factorsArray):
    x1, x2, x3 = [], [], []
    for factorNumber in range(len(factorsArray)):
        factor = factorsArray[factorNumber]
        for i in factor:
            if i > 0:
                if factorNumber == 0:
                    x1.append(X1max)
                elif factorNumber == 1:
                    x2.append(X2max)
                else:
                    x3.append(X3max)
            else:
                if factorNumber == 0:
                    x1.append(X1min)
                elif factorNumber == 1:
                    x2.append(X2min)
                else:
                    x3.append(X3min)
    return x1, x2, x3

def getRandomY():
    y1, y2, y3 = [], [], []

    for _ in range(0, 8):
        y1.append(randint(y_min, y_max))
        y2.append(randint(y_min, y_max))
        y3.append(randint(y_min, y_max))

    return y1, y2, y3

def getYRows(y1, y2, y3):
    y_rows = []
    for i in range(8):
        y_rows.append([y1[i], y2[i], y3[i]])

    return y_rows

def getYAverage(y_rows) -> list:
    y_average_all = []

    for i in range(len(y_rows)):
        y_average_all.append(np.average(y_rows[i]))

    return y_average_all

def cohren(f1, f2, q=0.05):
    q1 = q / f1
    fisher_value = f.ppf(q=1 - q1, dfn=f2, dfd=(f1 - 1) * f2)
    return fisher_value / (fisher_value + f1 - 1)

def plan_matrix(n, m):
    """
    Функція для знаходження матриці планування
    """
    y = np.zeros(shape=(n,m))
    for i in range(n):
        for j in range(m):
            y[i][j] = randint(y_min, y_max)

    x_norm = np.array([[1, -1, -1, -1],
                       [1, -1,  1,  1],
                       [1,  1, -1,  1],
                       [1,  1,  1, -1],
                       [1, -1, -1,  1],
                       [1, -1,  1, -1],
                       [1,  1, -1, -1],
                       [1,  1,  1,  1]])
    x_norm = x_norm[:len(y)]

    x_range = [(X1min, X1max), (X1min, X1max), (X1min, X1max)]
    x = np.ones(shape=(len(x_norm), len(x_norm[0])))
    for i in range(len(x_norm)):
        for j in range(1, len(x_norm[i])):
            if x_norm[i][j] == -1:
                x[i][j] = x_range[j-1][0]
            else:
                x[i][j] = x_range[j-1][1]

    print('\nМатриця планування')
    matrix = np.concatenate((x,y),axis=1)
    table = PrettyTable()

    yFieldNames = []
    yFieldNames += (f'Y{i+1}' for i in range(m))

    fieldNames = ["X0c", "X1c", "X2c", "X3c"] + yFieldNames
    table.field_names = fieldNames

    for i in range(len(matrix)):
        table.add_row(matrix[i])

    print(table)

    return x, y

def find_coefficient(x, y_aver, n):
    """
    Функція для знаходження коефіціентів B
    """
    mx1 = sum(x[:, 1]) / n
    mx2 = sum(x[:, 2]) / n
    mx3 = sum(x[:, 3]) / n
    my = sum(y_aver) / n

    a12 = sum([x[i][1] * x[i][2] for i in range(len(x))]) / n
    a13 = sum([x[i][1] * x[i][3] for i in range(len(x))]) / n
    a23 = sum([x[i][2] * x[i][3] for i in range(len(x))]) / n
    a11 = sum([i ** 2 for i in x[:, 1]]) / n
    a22 = sum([i ** 2 for i in x[:, 2]]) / n
    a33 = sum([i ** 2 for i in x[:, 3]]) / n
    a1 = sum([y_aver[i] * x[i][1] for i in range(len(x))]) / n
    a2 = sum([y_aver[i] * x[i][2] for i in range(len(x))]) / n
    a3 = sum([y_aver[i] * x[i][3] for i in range(len(x))]) / n

    X = [[  1, mx1, mx2, mx3],
         [mx1, a11, a12, a13],
         [mx2, a12, a22, a23],
         [mx3, a13, a23, a33]]

    Y = [my, a1, a2, a3]
    # Вирішимо систему рівнянь для коефіціентів
    B = [round(i, 2) for i in solve(X, Y)]
    print('\nРівняння регресії')
    print(f'{B[0]} + {B[1]}*x1 + {B[2]}*x2 + {B[3]}*x3')

    return B

def main(m, effectVzaemodiy = False, isSetEffectVzaemodiy = False):
    N = 8
    fisher = partial(f.ppf, q=1-0.05)
    student = partial(t.ppf, q=1-0.025)

    # y
    y1, y2, y3 = getRandomY()

    # y_rows
    y_rows = getYRows(y1, y2, y3)

    # середнє значення y_rows
    y_average = getYAverage(y_rows)

    disp_list = []

    list_bi = []

    if effectVzaemodiy:
        # З Ефектом взаємодії

        x0_factor = [ 1,  1,  1,  1,  1,  1,  1,  1]
        x1_factor = [-1, -1,  1,  1, -1, -1,  1,  1]
        x2_factor = [-1,  1, -1,  1, -1,  1, -1,  1]
        x3_factor = [-1,  1,  1, -1,  1, -1, -1,  1]

        x1x2_factor = [a*b for a, b in zip(x1_factor, x2_factor)]
        x1x3_factor = [a*b for a, b in zip(x1_factor, x3_factor)]
        x2x3_factor = [a*b for a, b in zip(x2_factor, x3_factor)]
        x1x2x3_factor = [a*b*c for a, b, c in zip(x1_factor, x2_factor, x3_factor)]

        # Кодовані значення
        x1, x2, x3 = getCodableX([x1_factor, x2_factor, x3_factor])

        x1x2 = [a*b for a, b in zip(x1, x2)]
        x1x3 = [a*b for a, b in zip(x1, x3)]
        x2x3 = [a*b for a, b in zip(x2, x3)]
        x1x2x3 = [a*b*c for a, b, c in zip(x1, x2, x3)]

        list_for_solve_b = [x0_factor, x1_factor, x2_factor, x3_factor, x1x2_factor, x1x3_factor, x2x3_factor, x1x2x3_factor]
        list_for_solve_a = list(zip(x0_factor, x1, x2, x3, x1x2, x1x3, x2x3, x1x2x3))

        # Коефіцієнти регресії для нормованих значень факторів коефіцієнтів регресії для нормованих значень факторів
        list_bi = []
        for k in range(N):
            S = 0
            for i in range(N):
                S += (list_for_solve_b[k][i] * y_average[i]) / N
            list_bi.append(round(S, 5))

        # Список з дисперсіями
        disp_list = getDispersion(y_rows)

        # Округлені значення дисперсії та yAverage для таблиці
        dispListToTable = [round(i, 3) for i in disp_list]
        yAverageToTable = [round(i, 3) for i in y_average]

        # Створюємо 2 таблиці
        pt1 = PrettyTable()
        pt2 = PrettyTable()

        # Данні для наповняння таблиці
        column_names = ["X0", "X1", "X2", "X3", "X1X2", "X1X3", "X2X3", "X1X2X3", "Y1", "Y2", "Y3", "Y_aver", "S^2"]
        column_values1 = [x0_factor, x1_factor, x2_factor, x3_factor, x1x2_factor, x1x3_factor, x2x3_factor, x1x2x3_factor, y1, y2, y3, yAverageToTable, dispListToTable]
        column_values2 = [x0_factor, x1, x2, x3, x1x2, x1x3, x2x3, x1x2x3, y1, y2, y3, yAverageToTable, dispListToTable]

        # Наповнюємо першу таблицю
        for i in range(len(column_names)):
            pt1.add_column(column_names[i], column_values1[i])

        # Виводимо першу таблицю
        print(pt1, "\n")

        print("y = {} + {}*x1 + {}*x2 + {}*x3 + {}*x1x2 + {}*x1x3 + {}*x2x3 + {}*x1x2x3 \n".format(list_bi[0], list_bi[1],
                                                                                list_bi[2], list_bi[3],
                                                                                list_bi[4], list_bi[5],
                                                                                list_bi[6], list_bi[7]))                                                                                # list_bi[2], list_bi[3]))

        # Наповнюємо другу таблицю
        for i in range(len(column_names)):
            pt2.add_column(column_names[i], column_values2[i])

        # Виводимо педругуршу таблицю
        print(pt2, '\n')

        # Визначення коефіцієнтів рівняння регресії ПФЕ
        list_ai = [round(i, 5) for i in solve(list_for_solve_a, y_average)]

        print("y = {} + {}*x1 + {}*x2 + {}*x3 + {}*x1x2 + {}*x1x3 + {}*x2x3 + {}*x1x2x3".format(list_ai[0], list_ai[1], list_ai[2], list_ai[3], list_ai[4], list_ai[5], list_ai[6], list_ai[7]))

    else:
        # Без ефекту взаємодії
        x, y = plan_matrix(N, m)
        list_bi = find_coefficient(x, y_average, N)
        disp_list = getDispersion(y)

    # Теоретичне
    Gp = max(disp_list) / sum(disp_list)

    F1 = m-1
    N = len(y1)
    F2 = N

    # Табличне
    Gt = cohren(F1, F2)
    print("\nGp = ", Gp, " Gt = ", Gt)

    if Gp < Gt:
        print("Gp < Gt -> Дисперсія однорідна!\n")

        Dispersion_B = sum(disp_list) / N
        Dispersion_beta = Dispersion_B / (m * N)
        S_beta = math.sqrt(abs(Dispersion_beta))

        t_list = []

        for i in range(len(list_bi)):
            t_list.append(abs(list_bi[i]) / S_beta)

        F3 = F1 * F2
        d = 0
        T = student(df=F3)
        print("t стьюдента табличне = ", T)

        for i in range(len(t_list)):
            if t_list[i] < T:
                print("Коефіціент {} не є значущим, виключаємо його".format(t_list[i]))
                list_bi[i] = 0

            else:
                d += 1

        # Критерії які підходять
        Y_counted_for_Student = []

        for i in range(8):
            if effectVzaemodiy:
                Y_counted_for_Student.append(list_bi[0] + list_bi[1] * x1[i] + list_bi[2] * x2[i] + list_bi[3] * x3[i] + list_bi[4] * x1x2[i] \
                + list_bi[5]*x1x3[i] + list_bi[6]*x2x3[i] + list_bi[7]*x1x2x3[i])
            else:
                Y_counted_for_Student.append(list_bi[0] + list_bi[1] * x[i][1] + list_bi[2] * x[i][2] + list_bi[3] * x[i][3])

        # Табличне
        F4 = N - d
        Ft = fisher(dfn=F4, dfd=F3)

        # Практичне
        Fp = kriteriy_fishera(y_rows, y_average, Y_counted_for_Student, N, m, d)

        effectVzaemodiy = True

        if Fp > Ft:
            print("Модель не адекватна")
            m = 3

            # Якщо вже було спробувано ефект взаємодії повернутися на початок
            if isSetEffectVzaemodiy:
                main(m, False, False)
            else:
                print("Спробуємо ефект взаємодії")
                main(m, effectVzaemodiy, True)

        else:
            print("МОДЕЛЬ АДЕКВАТНА")

    else:
        print("Дисперсія неоднорідна. Спробуємо з m = {}".format(m + 1))
        m += 1
        main(m, effectVzaemodiy, isSetEffectVzaemodiy)

if __name__ == "__main__":
    main(m)


import pandas as pd
import numpy as np
import scipy

raw_data = pd.read_csv("us-daily-covid-vaccine-doses-administered.csv")

s = set()
for i, r in raw_data.iterrows():
    s.add(r["Entity"])

df = pd.DataFrame(columns=["State", "Vacc_Rate", "Case_Rate"])

def in_march(s):
    if s[6] == "3":
        return True
    return False

for state in s:
    data = raw_data.loc[(raw_data["Entity"] == state) & (raw_data["Day"].apply(in_march))]["daily_vaccinations"]
    #print (data.shape)
    arr = np.array(data)
    df.loc[df.shape[0]] = [state, scipy.stats.linregress(np.arange(1, arr.size+1), y=arr).slope, ""]
    #if state == "Alabama":
        #print (arr)


#!/usr/bin/env python
#cd ~/Desktop/SU_Aur_backup/channels/bg_sub/flat/im_distort/align/OE/QU/rotated_QU/aligned_QU/flux_scaling/PA_diskmajoraxis
'''
This file combines the radial PI distribution along PA=15 and 195 (major axis)
in a single plot.
See 15radial_PI_dist.py to see individual results.
'''
from pylab import *
import pyfits
from scipy.ndimage.interpolation import rotate
import numpy as np
from matplotlib.patches import Ellipse
cmap=cm.jet
cmap.set_bad('black',1)

#z=pyfits.open('obj_PI_fs_conv_sigma08.fits')
#z=pyfits.open('obj_PI_fs_conv_sig1.fits')
z=pyfits.open('obj_PI_fs_conv_sig2.fits')
obj_PI_fs_conv=z[0].data-1.1e-7
z.close()

#define image center
scale=9.53e-3*140
xcenter=271
ycenter=318

#obj_PI_fs_conv_cropped=obj_PI_fs_conv[ycenter-270:ycenter+271,xcenter-270:xcenter+271]

#angle of image rotation along the major axis
angle1, angle2= 15, 195
rotated_image1=rotate(obj_PI_fs_conv,angle1)
rotated_image2=rotate(obj_PI_fs_conv,angle2)
dim1=rotated_image1.shape[0]
dim2=rotated_image2.shape[0]
midpoint1   =dim1/2
midpoint2   =dim2/2

extent_PI1=array([-midpoint1,midpoint1,-midpoint1,midpoint1])*scale
extent_PI2=array([-midpoint2,midpoint2,-midpoint2,midpoint2])*scale

#This combines subplots in figure 1 in one figure
figure(0)
clf()
imshow(log10(rotated_image1),extent=extent_PI1,vmin=-7.4,vmax=-5,cmap=cmap)
plot([0,0], [15*scale, midpoint1*scale], color='w', linestyle='--', linewidth=2)
plot([0,0], [-midpoint1*scale,-15*scale], color='w', linestyle='--', linewidth=2)
xlabel('AU')
ylabel('AU')
axis([-150,150,-150,150])
colorbar()

figure(1)
subplot(121)
imshow(log10(rotated_image1),extent=extent_PI1,vmin=-7.4,vmax=-5,cmap=cmap)
plot([0,0], [0, midpoint1*scale], color='w', linestyle='--', linewidth=1)
xlabel('AU')
ylabel('AU')
axis([-150,150,-150,150])
colorbar()
title('P.A.= %d deg.' % angle1)
# 195
subplot(122)
imshow(log10(rotated_image2),extent=extent_PI2,vmin=-7.4,vmax=-5,cmap=cmap)
plot([0,0], [0, midpoint2*scale], color='w', linestyle='--', linewidth=1)
xlabel('AU')
ylabel('AU')
axis([-150,150,-150,150])
colorbar()
title('P.A.= %d deg.' % angle2)

figure(2)
clf()
#get the average of of flux along the vertical (PA=15) +-2 pixels (5 columns) to increase SNR
image1a=rotated_image1[midpoint1:,midpoint1+1]*scale
image2a=rotated_image1[midpoint1:,midpoint1-1]*scale
image3a=rotated_image1[midpoint1:,midpoint1+2]*scale
image4a=rotated_image1[midpoint1:,midpoint1-2]*scale
image5a=rotated_image1[midpoint1:,midpoint1]*scale
image_ave1=(image1a+image2a+image3a+image4a+image5a)/5

image1b=rotated_image2[midpoint2:,midpoint2+1]*scale
image2b=rotated_image2[midpoint2:,midpoint2-1]*scale
image3b=rotated_image2[midpoint2:,midpoint2+2]*scale
image4b=rotated_image2[midpoint2:,midpoint2-2]*scale
image5b=rotated_image2[midpoint2:,midpoint2]*scale
image_ave2=(image1b+image2b+image3b+image4b+image5b)/5

#new_rscale=log((arange(rotated_image.shape[0])-midpoint)*scale)
new_rscale1=(arange(rotated_image1.shape[0])-midpoint1)*scale
new_rscale2=(arange(rotated_image2.shape[0])-midpoint2)*scale
#get only the positive values of flux; midpoint>=0
new_rscale1=new_rscale1[midpoint1:]
new_rscale2=new_rscale2[midpoint2:]
p1, p2 = -2.9, -2.8
constant1= 0.1037
constant2= 0.0498

label15 = 'PI flux for PA = %d deg.' % angle1
label195 = 'PI flux for PA = %d deg.' % angle1

#fit a power law with exponent p=-1
power_law_m1= constant1 * new_rscale1 ** p1
power_law_m2= constant2 * new_rscale2 ** p2
loglog(new_rscale1,image_ave1,'ko-',label=label15)
loglog(new_rscale2,image_ave2,'ks',mfc="None",label=label195) #unfilled circle
#You can draw multiple lines with a single plot command and  plot() returns a "list" of lines that were added.  In this case, the return value is a list of a single line, which is  unpacked with the comma .
t,=loglog(new_rscale2,image_ave2,'ko',mfc="None")
t.set_mec(t.get_color())

#log log plot of power law
label1 = str(constant1)+'r ** %.1f (PA = %d deg.)' % (p1,angle1)
label2 = str(constant2)+'r ** %.1f (PA = %d deg.)' % (p2,angle2)

loglog(new_rscale1,power_law_m1,'k-',label=label1)
loglog(new_rscale2,power_law_m2,'k--',label=label2)
#plot([0,0], [5e-8, 3e-5], color='r', linestyle='--', linewidth=1) #this is a vertical line along the origin

#input the last pixel = boundary of disk
end_pix_disk= 60
label3= 'mask boundary (r=20 AU); disk boundary r=%.1f AU' % ((end_pix_disk+1)*scale)

#mark the point marking the boundary of the mask at r=15, before of which the flux is unreliable, and also the last pixel used in fitting (flux>0)
loglog(15*scale,image_ave1[15], 'ro',label=label3)
loglog(15*scale,image_ave2[15], 'rs')

loglog(end_pix_disk*scale,image_ave1[end_pix_disk], 'ro') #for PA=15
loglog(end_pix_disk*scale,image_ave2[end_pix_disk], 'rs')
axis([0,1000,5e-8,3e-5]) #AU scale
ylabel('log (n_PI / n*)')
xlabel('log(r) (AU)')
#title('Radial PI distribution for PA=%d and %d' % (angle1,angle2))
legend(loc='upper right', shadow=True)

#Takami+13 used 0."3 arcsec in diameter mask
#0."3 arcsec/*0.00948arcsec/pix = 31.65 pixels
hmask,wmask= 30*scale,30*scale #radius = 15 pix = 20 AU
l=Ellipse(xy=(0,0),height=hmask,width=wmask)
figure(0).add_subplot(111).add_artist(l)
m=Ellipse(xy=(0,0),height=hmask,width=wmask)
figure(1).add_subplot(121).add_artist(m)
n=Ellipse(xy=(0,0),height=hmask,width=wmask)
figure(1).add_subplot(122).add_artist(n)
l.set_color('k')
m.set_color('k')
n.set_color('k')
show()

'''
np.savetxt('image_ave_PA15.txt',image_ave)
np.savetxt('new_rscale_PA15.txt',new_rscale)

import powerlaw
data = array([1.7, 3.2 ...]) # data can be list or numpy array
results = powerlaw.Fit(data)
print results.power_law.alpha
print results.power_law.xmin
R, p = results.distribution_compare('power_law', 'lognormal')
'''


#######################
# Author: slurpxbt
#######################
import binance_candle_data as bcd
from pathlib import Path
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import numpy as np

# pandas display, pycharm otherwise doesn't display all columns
pd.set_option('display.max_columns', 100)
desired_width = 500
pd.set_option('display.width', desired_width)
# ----------------------------------------------------------

root = Path("..")

# USER INPUTS --------------------------------------------------------
# --------------------------------------------------------------------
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2020, 10, 1)
# --------------------------------------------------------------------
time_frame = "1h"
ticker = "ETHUSDT"
# --------------------------------------------------------------------
start_size = 5000  # contracts
size = start_size
# --------------------------------------------------------------------
market_order_fee = 0.075    # unit [%]
slippage = 0.1              # unit [%]
# --------------------------------------------------------------------
tp_pct = 0.01               # unit => 1 - 100%, 0.1 - 10%, 0.01 - 1%
# --------------------------------------------------------------------
compounding = True
# END OF USER INPUTS -------------------------------------------------

filepath = f"data/{ticker}_{time_frame}.p"
data = bcd.get_data_by_date(start_date, end_date, filepath)

# ----------------------------------------------------------------
# Add MA and EMAs here
data["EMA5"] = round(data["close"].ewm(span=5).mean(), 2)
data["EMA10"] = round(data["close"].ewm(span=10).mean(), 2)
# ----------------------------------------------------------------

# format =  [open_time, open, high, low, close, volume, close_time, number_of_trades, EMA5,  EMA10]
candle_data_list = data.values.tolist()     # convert df to list

short_long = ""
trade_opened = False

pnl = []
tps = []
sls = []
cum_size = []

for candle in candle_data_list:
    close = candle[4]           # candle open prc
    low = candle[3]             # candle low prc
    high = candle[2]            # candle high prc
    ema5 = candle[8]            # 5 ema
    ema10 = candle[9]           # 10 ema

    # YOUR STRATEGY GOES HERE
    if not trade_opened:
        trade_entry = 0
        tp = 0
        if close < ema10 and close < ema5:
            short_long = "short"
            trade_opened = True
            trade_entry = close
            tp = round(trade_entry - (trade_entry * tp_pct), 2)
            print(f"short entry @ {trade_entry} with tp @ {tp} -> size {size}")

        elif close > ema5 and close > ema10:
            short_long = "long"
            trade_opened = True
            trade_entry = close
            tp = round(trade_entry + (trade_entry * tp_pct), 2)
            print(f"long entry @ {trade_entry} with tp @ {tp} -> size {size}")

    if trade_opened:

        if short_long == "short":   # short

            if close <= tp or low <= tp:
                gain = round((tp / trade_entry - 1) * 100 * (-1) - market_order_fee - slippage, 2)
                trade_profit = gain * size / 100

                cum_size.append(size + trade_profit)
                if compounding:
                    size = size + trade_profit
                pnl.append(trade_profit)
                tps.append(trade_profit)

                print(f"tp executed @ {tp} -> gain {gain}% -> profit {trade_profit} $")
                trade_opened = False
                print("-"*100)

            elif close > ema10 and close > ema5:
                gain = round((close / trade_entry - 1) * 100 * (-1) - market_order_fee - slippage, 2)
                trade_profit = gain * size / 100

                cum_size.append(size + trade_profit)
                if compounding:
                    size = size + trade_profit
                pnl.append(trade_profit)
                sls.append(trade_profit)

                print(f"sl executed @ {close} -> gain {gain}% -> profit {trade_profit} $")
                trade_opened = False
                print("-" * 100)

        elif short_long == "long":  # long

            if close >= tp or high >= tp:
                gain = round((tp / trade_entry - 1) * 100 - market_order_fee - slippage,2)
                trade_profit = gain * size / 100

                cum_size.append(size + trade_profit)
                if compounding:
                    size = size + trade_profit
                pnl.append(trade_profit)
                tps.append(trade_profit)
                print(f"tp executed @ {tp} -> gain {gain}% -> profit {trade_profit} $")
                trade_opened = False
                print("-" * 100)

            elif close < ema10 and close < ema5:
                gain = round((close / trade_entry - 1) * 100 - market_order_fee - slippage, 2)
                trade_profit = gain * size / 100

                cum_size.append(size + trade_profit)
                if compounding:
                    size = size + trade_profit
                pnl.append(trade_profit)
                sls.append(trade_profit)
                print(f"sl executed @ {close} -> gain {gain}% -> profit {trade_profit} $")
                trade_opened = False
                print("-" * 100)

print("-" * 100)
print("-" * 100)
print(f"strategy on {ticker} on {time_frame} timespan => {start_date} -> {end_date}")
print(f"strategy $ pnl is {round(sum(pnl), 2)}")
if compounding:
    print(f"strategy % pnl is {round((cum_size[-1] / start_size - 1) * 100, 2)}")
    print(f"W/L ratio: {round((len(tps)/(len(sls)+len(tps)))*100, 2)}%")
    print(f"average profit {round(np.average(tps), 2)}")
    print(f"average loss {round(np.average(sls), 2)}")
else:
    print(f"strategy % pnl is {round((sum(pnl) / start_size - 1) * 100, 2)}")
    print(f"W/L ratio: {len(tps) / len(sls)}")
    print(f"average profit {round(np.average(tps), 2)}")
    print(f"average loss {round(np.average(sls), 2)}")

cum_sum = []
cum = 0
for profit in pnl:
    cum += profit
    cum_sum.append(cum)

plt.plot(cum_sum)
plt.title(f"strategy $ return {start_date} -> {end_date}")
plt.grid(alpha=0.3)
plt.show()

plt.plot(cum_size)
plt.title(f"strategy cum size")
plt.grid(alpha=0.3)
plt.show()

x = [i for i in range(len(pnl))]
plt.title("pnl histogram")
plt.bar(x, pnl, width=0.3)
plt.show()


import cv2
import base64
import os
import RPi.GPIO as GPIO

#переводим полученное изображение cv2 в массив байт
def encode_send(image):
    retval, buffer = cv2.imencode('.jpg', image)
    jpg_as_byte = base64.b64encode(buffer)
    return jpg_as_byte

#разделяем строку комманд на метки(accel,brake...) и значения(1,0)
def data_recv(data):
    datasplit = data.split('*')
    data = 0
    for i in datasplit:
        str1 = datasplit[datasplit.index(i)].replace('(','')
        str2 = str1.replace(')','')
        str3 = str2.split(' ')
        if str3[0] == 'accel':
            accel = int(str3[1])
            #break
        if str3[0] == 'breake':
            breake = int(str3[1])
            #break
        if str3[0] == 'left':
            left = int(str3[1])
            # break
        if str3[0] == 'right':
            right = int(str3[1])
            #break
    return accel, breake, left, right

def print_comm(accel, breake, left, right):
    print('accel :',accel)
    print('breake :',breake)
    print('left :',left)
    print('right :',right)

#управляем моторами
def motor_control(accel, breake, left, right):
    if accel == 1:
        GPIO.output(7, 1)
        GPIO.output(11, 0)
    if breake == 1:
        GPIO.output(11, 1)
        GPIO.output(7, 0)
    if left == 1:
        GPIO.output(29, 1)
        GPIO.output(31, 0)
    if right == 1:
        GPIO.output(31, 1)
        GPIO.output(29, 0)

#считываем изображение с камеры
def camera(cap):
    success, image = cap.read()
    return image

def motors_clean(accel=0, breake=0, left=0, right=0):
    GPIO.output(7, accel)
    GPIO.output(11, breake)
    GPIO.output(29, left)
    GPIO.output(31, right)


#import PIL
import sys
#from Naked.toolshed.shell import muterun_js
#from subprocess import call
import shlex, subprocess
import serial
ser =  serial.Serial('COM11', 56000)
print(ser.name)         # check which port was really used

#response = muterun_js('nodeDMD.js')
#print (response)
var=subprocess.check_output("node nodeDMD.js", shell=True)
var=var[:-1]

print(type(var),var)
ser.write(var)     # write a string
ser.close()


import math  # I am guessing that you will need to use the math module
import random
from point import Point

import numpy as np
import scipy.spatial as ss

class PointPattern(object):

    def __init__(self):
        self.points = []

    def add_pt (self,point):
        self.points.append(point)

    def remove_pt (self,index):
        del(self.points[index])

    def number_coincident_points(self):
        num=0;
        coincident_list=[]
        for i, p1 in enumerate(self.points):
            for j, p2 in enumerate(self.points):
                if i!=j:
                    if p2 not in coincident_list:
                        if p1==p2:
                            num+=1
                            coincident_list.append(p2)
        return num

    def list_marks(self):
        mark_list=[]
        for i in self.points:
            if i.mark not in mark_list:
                mark_list.append(i.mark)
        return mark_list

    def points_by_mark(self):
    #return a subset of points by the mark
        return 0;

    def n_rand_pts(self,n=None,marks=None):
        temp=[]
        if(n==None):
            n=len(self.points);

        for i in range(n):
            temp.append(Point(random.uniform(0,1),random.uniform(0,1),random.choice(self.marks)));

        return temp

    def gen_rand_pts(self,upper_bound=1,lower_bound=0,num_pts=100):

        return np.random.uniform(lower_bound,upper_bound, (num_pts,2));

    def critical_pts(distances):
        return min(distances), max(distances)

    def nearest_neighbor_dist_numpy(self):

        return

    def average_nearest_neighbor_distance_kd(self,pts=None):
        mean=0;

        if pts==None:
            points = self.points
        else:
            points=pts

        kdtree = ss.KDTree(points);
        for i in points:
            dist_nearest, nn_pt = kdtree.query(i, k=2)
            mean+=dist_nearest.item(1);

            #print(dist_nearest.item(1))
        return mean/len(points);

    def average_nearest_neighbor_distance_numpy(self,pts=None):
        '''
            computing using numpy
        '''
        points=[]
        if pts==None:
            points = self.points
        else:
            points=pts

        nn_dists = np.array([])

        n_dist_current=math.inf
        #print(pts)
        for i, point1 in enumerate(points):
            for j, point2 in enumerate(points):

                if i==j:
                    continue
                elif(ss.distance.euclidean(point1, point2)<n_dist_current):
                    n_dist_current=ss.distance.euclidean(i,j);

            nn_dists=np.concatenate((nn_dists,[n_dist_current]),axis=0);
            n_dist_current=math.inf;

        return np.mean(nn_dists);

    def g_func(self,nsteps):
        '''
            computing using numpy
        '''

        nn_dists = []

        for i, point1 in enumerate(self.points):
            nn_dist=math.inf
            for j, point2 in enumerate(self.points):
                if i==j:
                    continue
                elif(ss.distance.euclidean(point1,point2)<nn_dist):
                    nn_dist = ss.distance.euclidean(point1,point2)
            nn_dists.append(nn_dist)

        max_nn_dist=max(nn_dists)
        min_nn_dist=min(nn_dists)
        big_n = len(self.points)
        #print(big_n)

        ds = np.linspace(0,max_nn_dist+0.1,nsteps) #apply numpy

        #print(nn_dists)
        g_func_results=[]
        ds_outputs=[]
        for i in range(nsteps):
            #print(ds[i])
            count=0
            for curr_nn_dist in nn_dists:
                if curr_nn_dist<ds[i]:
                    count+=1
            ds_outputs.append(ds[i])
            g_func_results.append(count/big_n)

        return (g_func_results,ds_outputs)


# -*- coding: utf-8 -*-
from datetime import datetime
import random

from invoke import task
import requests
from summarizer import summarize, sanitize, Summarizer, Parser

from app import create_app
from summary import process_article_summaries
from db import mongo

app = create_app()
app.test_request_context().push()

@task
def run(host='0.0.0.0', port=3000):
    app.run(host=host, port=port)

@task
def summaries(override=False):
    print(process_article_summaries(mongo.db, override))

@task
def reprocess():
    articles = mongo.db.SummaryReview.find({})
    process(articles, False)

@task
def tokenize_article(articleid=None):
    if articleid is not None:
        articles = mongo.db.Article.find({'article_id': int(articleid)})
    else:
        print('grabbing random article...')
        articles = mongo.db.Article.find({'body': { '$ne': '' }})

    if not articles.count():
        print('Article id {} not found'.format(articleid) if articleid is not None else 'No articles fonud')
        return

    index = random.randint(0, articles.count() - 1)
    article = articles[index]

    sentences = parser.sentences(article['body'])

    print('Processing with {}'.format(parser.__class__.__name__))
    print('Sentences for Article {}'.format(article['article_id']))
    print('-' * 80)
    for s in sentences:
        print(s)
        print('-' * 80)

@task
def fetch_articles(update_all=False):
    r = requests.get('https://api.michigan.com/v1/news/?limit=20')
    r.raise_for_status()
    data = r.json()
    process(data['articles'], update_all=update_all)

def summary_indices(sentences, summary):
    indices = []
    for i, sentence in enumerate(sentences):
        if sentence in summary:
            indices.append(i)

    return indices

def process(articles, query_db=True, update_all=False):
    parser = Parser()
    summar = Summarizer(parser)

    num_added = 0
    num_updated = 0
    num_invalid_body = 0
    for article in articles:
        article_id = article['article_id']
        article_headline = article['headline']
        article_url = article['url']
        body = article.get('body', None)
        review = article

        if query_db:
            review = mongo.db.SummaryReview.find_one({
                'article_id': article_id
            })

        if not body:
            art = mongo.db.Article.find_one({ 'article_id': article_id })
            if not art or not art['body']:
                print("Article {} does not have a body, skipping".format(article_id))
                num_invalid_body += 1
                continue

            body = art['body']

        sentences = parser.sentences(body)
        summary = summarize(article['headline'], body, count=3, summarizer=summar)
        bot_indices = summary_indices(sentences, summary)

        if review is None:
            mongo.db.SummaryReview.insert({
                'article_id': article_id,
                'headline': article_headline,
                'url': article_url,
                'sentences': sentences,
                'summary': { 'Bot': bot_indices },
            })

            num_added += 1
            continue

        # remove all votes and flags if new sentences dont match old ones
        updated = False
        if 'sentences' not in review or len(review['sentences']) != len(sentences):
            updated = True
        else:
            for cur_sentence, new_sentence in zip(review['sentences'], sentences):
                if cur_sentence != new_sentence:
                    updated = True
                    break

        if updated or update_all:
            review['invalid'] = []
            review['summary'] = {
                'Bot': bot_indices
            }
            review['sentences'] = sentences
            review['updated_at'] = datetime.utcnow()
            review['tokens_valid'] = False
            mongo.db.SummaryReview.update({ '_id': review['_id'] }, review)
            num_updated += 1
        else:
            if 'summary' in review:
                review['summary']['Bot'] = bot_indices
            else:
                review['summary'] = { 'Bot': bot_indices }
            mongo.db.SummaryReview.update({ '_id': review['_id'] }, review)

    print("-" * 80)
    print("Articles fetched:\n")
    print("\tNumber added: {}".format(num_added))
    print("\tNumber updated: {}".format(num_updated))
    print("\tNumber invalid body: {}".format(num_invalid_body))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import timedelta
from django.apps import apps as imported_apps
from django.db import migrations, transaction
from django.utils import timezone

from v1.util.migrations import get_or_create_page, get_page

class JobConverter(object):
    def __init__(self, parent, apps=None):
        self.parent = parent
        self.apps = apps or imported_apps

    def convert(self, job):
        page_attributes = self.get_page_attributes(job)

        page = get_or_create_page(
            apps=self.apps,
            page_cls_app='jobmanager',
            page_cls_name='JobListingPage',
            title=job.title,
            slug=job.slug,
            parent_page=self.parent,
            **page_attributes
        )

        panels = []
        panels.extend(self.make_email_link_panels(page, job))
        panels.extend(self.make_usajobs_link_panels(page, job))
        panels.extend(self.make_grade_panels(page, job))
        panels.extend(self.make_region_panels(page, job))

        for panel in panels:
            panel.save()

        return page

    def get_or_init_page(self, slug):
        page_cls = self.apps.get_model('jobmanager', 'JobListingPage')

        try:
            return (page_cls.objects.get(slug=slug), False)
        except page_cls.DoesNotExist:
            return (page_cls(slug=slug), True)

    @staticmethod
    def get_page_attributes(job):
        return {
            'description': job.description or job.title,
            'open_date': job.open_date,
            'close_date': job.close_date,
            'salary_min': job.salary_min or 0,
            'salary_max': job.salary_max or 0,
            'division': job.category,
        }

    def make_email_link_panels(self, page, job):
        link_cls = self.apps.get_model('jobmanager', 'EmailApplicationLink')

        return [
            link_cls(
                job_listing=page,
                address=at.announcement_email,
                label=at.application_type.applicant_type,
                description=at.application_type.description
            ) for at in job.jobapplicanttype_set.filter(is_usajobs=False)
        ]

    def make_usajobs_link_panels(self, page, job):
        applicant_types = job.jobapplicanttype_set.filter(is_usajobs=True)
        if not all(at.announcement_number for at in applicant_types):
            raise ValueError('empty usajobs announcement number')

        link_cls = self.apps.get_model('jobmanager', 'USAJobsApplicationLink')

        return [
            link_cls(
                job_listing=page,
                announcement_number=at.announcement_number,
                url=at.usajobs_url,
                applicant_type=at.application_type,
            ) for at in applicant_types
        ]

    def make_grade_panels(self, page, job):
        panel_cls = self.apps.get_model('jobmanager', 'GradePanel')

        return [
            panel_cls(job_listing=page, grade=grade)
            for grade in job.grades.all()
        ]

    def make_region_panels(self, page, job):
        panel_cls = self.apps.get_model('jobmanager', 'RegionPanel')

        return [
            panel_cls(job_listing=page, region=region)
            for region in job.locations.all()
        ]

@transaction.atomic
def migrate_job_pages(apps, schema_editor):
    Job = apps.get_model('jobmanager', 'Job')
    date_cutoff = timezone.now().date() - timedelta(days=180)
    jobs = Job.objects.filter(close_date__gte=date_cutoff)

    careers_page = get_page(apps, slug='careers')
    converter = JobConverter(parent=careers_page, apps=apps)

    for job in jobs:
        converter.convert(job)

class Migration(migrations.Migration):
    dependencies = [
        ('jobmanager', '0007_create_careers_pages'),
    ]

    operations = [
        migrations.RunPython(migrate_job_pages, migrations.RunPython.noop),
    ]


# Expand Games data with Elo and Win percentages
# Returns panda of all games
def expand_elo(games):
    from app_voetbalelo.uefa_leagues.get_elo_data import get_elo_data

    import json
    import pickle
    import numpy as np
    import pandas as pd
    import datetime
    import distance
    import scipy.stats # For Poisson Distribution, numpy doesn't have it

    # Expand games with EloHT/EloAT/OddsHomeWinOdd/OddsAwayWin/OddTie
    games["HomeElo"] = len(games["HomeTeam"])*[0]
    games["AwayElo"] = len(games["HomeTeam"])*[0]
    games["ChanceHomeWin"] = len(games["HomeTeam"])*[0]
    games["ChanceAwayWin"] = len(games["HomeTeam"])*[0]
    games["ChanceTie"] = len(games["HomeTeam"])*[0]

    # Make pandas from games
    panda = pd.DataFrame(games)
    panda = panda[(~panda.TYPE.str.contains("qualifying")) & (~panda.TYPE.str.contains("Play-offs"))] # Only Group Phase and later
    # panda = panda.set_index("DATE")
    panda = panda.sort_index()

    # Get ELO data for every dat in panda
    dates = list(set(panda.DATE))
    for date in dates:
        # Get Elo BEFORE game
        date_elo = date - datetime.timedelta(days=1)
        team_elo_date = get_elo_data(date_elo)

        # Match elo data with panda
        for i in range(len(panda)):
            home_team_elo = 0
            away_team_elo = 0
            if panda.DATE.iloc[i] == date:
                # Fixed assign
                team_names_panda_fixed = ["AZ","Astana","Asteras","Rubin","Sion","Celtic","Shakhtar Donetsk","Mönchengladbach","Paris","Krasnodar","Club Brugge","Lokomotiv Moskva","Qarabağ","St-Étienne","Sporting CP","Qäbälä","Plzeň","Liberec","Gent","Athletic"]
                team_names_elo_fixed = ["Alkmaar","FK Astana","Asteras Tripolis","Rubin Kazan","Sion","Celtic","Shakhtar","Gladbach","Paris SG","FC Krasnodar","Brugge","Lok Moskva","Karabakh Agdam","Saint-Etienne","Sporting","Gabala","Viktoria Plzen","Slovan Liberec","Gent","Bilbao"]

                for k in range(len(team_names_panda_fixed)):
                    if panda.HomeTeam.iloc[i] == team_names_panda_fixed[k]:
                        for j in range(len(team_elo_date)):
                            if team_elo_date[j][0] == team_names_elo_fixed[k]:
                                panda.loc[panda.index[i],"HomeElo"] = team_elo_date[j][1]
                                home_team_elo = 1

                    if panda.AwayTeam.iloc[i] == team_names_panda_fixed[k]:
                        for j in range(len(team_elo_date)):
                            if team_elo_date[j][0] == team_names_elo_fixed[k]:
                                panda.loc[panda.index[i],"AwayElo"] = team_elo_date[j][1]
                                away_team_elo = 1

                # Rest
                # HomeTeam
                if home_team_elo == 0:
                    for j in range(len(team_elo_date)):
                        if distance.levenshtein(panda.HomeTeam.iloc[i],team_elo_date[j][0]) == 0:
                            panda.loc[panda.index[i],"HomeElo"] = team_elo_date[j][1]
                            break
                        elif distance.levenshtein(panda.HomeTeam.iloc[i],team_elo_date[j][0]) == 1:
                            panda.loc[panda.index[i],"HomeElo"] = team_elo_date[j][1]
                            break
                        elif distance.levenshtein(panda.HomeTeam.iloc[i],team_elo_date[j][0]) == 2:
                            panda.loc[panda.index[i],"HomeElo"] = team_elo_date[j][1]
                            break

                # AwayTeam
                if away_team_elo == 0:
                    for j in range(len(team_elo_date)):
                        if distance.levenshtein(panda.AwayTeam.iloc[i],team_elo_date[j][0]) == 0:
                            panda.loc[panda.index[i],"AwayElo"] = team_elo_date[j][1]
                            break
                        elif distance.levenshtein(panda.AwayTeam.iloc[i],team_elo_date[j][0]) == 1:
                            panda.loc[panda.index[i],"AwayElo"] = team_elo_date[j][1]
                            break
                        elif distance.levenshtein(panda.AwayTeam.iloc[i],team_elo_date[j][0]) == 2:
                            panda.loc[panda.index[i],"AwayElo"] = team_elo_date[j][1]
                            break

    # This prints something if no Elo value found for team
    for i in range(len(panda)):
        if panda.AwayElo.iloc[i] == 0:
            print(panda.AwayTeam.iloc[i])

    # Convert Elo columns to float
    panda.HomeElo = panda.HomeElo.astype(float)
    panda.AwayElo = panda.AwayElo.astype(float)
    # Calculate Win/Lose/Tie probabilities for every game
    # Parameters ELO
    home_field_advantage = 100
    print("Elo Algorithm")
    for i in range(len(panda)):
        if (i == 0):
            print(str(i+1) + '/' + str(len(panda)))
        elif i%10 == 0:
            print(str(i) + '/' + str(len(panda)))
        elif (i == len(panda)-1):
            print(str(i+1) + '/' + str(len(panda)))
        # panda.HomeElo.iloc[i]
        W_home_e = 1/(10**(-(panda.HomeElo.iloc[i]+home_field_advantage-panda.AwayElo.iloc[i])/400)+1)

        # First estimate expected goals
        # Goals for Home team
        if W_home_e < 0.5:
            home_goals = 0.2 + 1.1*np.sqrt(W_home_e/0.5)
        else:
            home_goals = 1.69 / (1.12*np.sqrt(2 - W_home_e/0.5)+0.18)

        # Goals for the Away team:
        if W_home_e < 0.8:
            away_goals = -0.96 + 1/(0.1+0.44*np.sqrt((W_home_e+0.1)/0.9))
        else:
            away_goals = 0.72*np.sqrt((1 - W_home_e)/0.3)+0.3

        # Now use poisson distribution to determine for each team the chance it scores x goals
        # Combine these for both teams to calculate Win/Loss/Draw expectancy

        # j and k are number of goals
        for j in range(15):
            for k in range(15):
                if j > k:
                    # Home Win
                    panda.loc[panda.index[i],"ChanceHomeWin"] = panda.ChanceHomeWin.iloc[i] + scipy.stats.distributions.poisson.pmf(j,home_goals)*scipy.stats.distributions.poisson.pmf(k,away_goals)
                if j == k:
                    # Tie
                    panda.loc[panda.index[i],"ChanceTie"] = panda.ChanceTie.iloc[i] + scipy.stats.distributions.poisson.pmf(j,home_goals)*scipy.stats.distributions.poisson.pmf(k,away_goals)
                if j < k:
                    # Away Win
                    panda.loc[panda.index[i],"ChanceAwayWin"] =panda.ChanceAwayWin.iloc[i] + scipy.stats.distributions.poisson.pmf(j,home_goals)*scipy.stats.distributions.poisson.pmf(k,away_goals)

        # Make sure probabilities sum up to 1 (otherwise problem for montecarlo simulation)
        percentages = np.array([panda.ChanceHomeWin.iloc[i],panda.ChanceTie.iloc[i],panda.ChanceAwayWin.iloc[i]])
        percentages /= percentages.sum()

        panda.loc[panda.index[i],"ChanceHomeWin"] = percentages[0]
        panda.loc[panda.index[i],"ChanceTie"] = percentages[1]
        panda.loc[panda.index[i],"ChanceAwayWin"] = percentages[2]

    return panda


# coding: utf-8

from __future__ import absolute_import
from datetime import date, datetime  # noqa: F401

from typing import List, Dict  # noqa: F401

from datavengers_2020.models.base_model_ import Model
from datavengers_2020.models.indicator_value import IndicatorValue  # noqa: F401,E501
from datavengers_2020 import util

class Indicator(Model):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """
    def __init__(self, symbol: str=None, indicator: str=None, start_date: datetime=None, end_date: datetime=None, window_size: int=None, bandwidth: float=None, values: List[IndicatorValue]=None):  # noqa: E501
        """Indicator - a model defined in Swagger

        :param symbol: The symbol of this Indicator.  # noqa: E501
        :type symbol: str
        :param indicator: The indicator of this Indicator.  # noqa: E501
        :type indicator: str
        :param start_date: The start_date of this Indicator.  # noqa: E501
        :type start_date: datetime
        :param end_date: The end_date of this Indicator.  # noqa: E501
        :type end_date: datetime
        :param window_size: The window_size of this Indicator.  # noqa: E501
        :type window_size: int
        :param bandwidth: The bandwidth of this Indicator.  # noqa: E501
        :type bandwidth: float
        :param values: The values of this Indicator.  # noqa: E501
        :type values: List[IndicatorValue]
        """
        self.swagger_types = {
            'symbol': str,
            'indicator': str,
            'start_date': datetime,
            'end_date': datetime,
            'window_size': int,
            'bandwidth': float,
            'values': List[IndicatorValue]
        }

        self.attribute_map = {
            'symbol': 'symbol',
            'indicator': 'indicator',
            'start_date': 'start_date',
            'end_date': 'end_date',
            'window_size': 'windowSize',
            'bandwidth': 'bandwidth',
            'values': 'values'
        }
        self._symbol = symbol
        self._indicator = indicator
        self._start_date = start_date
        self._end_date = end_date
        self._window_size = window_size
        self._bandwidth = bandwidth
        self._values = values

    @classmethod
    def from_dict(cls, dikt) -> 'Indicator':
        """Returns the dict as a model

        :param dikt: A dict.
        :type: dict
        :return: The Indicator of this Indicator.  # noqa: E501
        :rtype: Indicator
        """
        return util.deserialize_model(dikt, cls)

    @property
    def symbol(self) -> str:
        """Gets the symbol of this Indicator.

        :return: The symbol of this Indicator.
        :rtype: str
        """
        return self._symbol

    @symbol.setter
    def symbol(self, symbol: str):
        """Sets the symbol of this Indicator.

        :param symbol: The symbol of this Indicator.
        :type symbol: str
        """

        self._symbol = symbol

    @property
    def indicator(self) -> str:
        """Gets the indicator of this Indicator.

        :return: The indicator of this Indicator.
        :rtype: str
        """
        return self._indicator

    @indicator.setter
    def indicator(self, indicator: str):
        """Sets the indicator of this Indicator.

        :param indicator: The indicator of this Indicator.
        :type indicator: str
        """
        if indicator is None:
            raise ValueError("Invalid value for `indicator`, must not be `None`")  # noqa: E501

        self._indicator = indicator

    @property
    def start_date(self) -> datetime:
        """Gets the start_date of this Indicator.

        :return: The start_date of this Indicator.
        :rtype: datetime
        """
        return self._start_date

    @start_date.setter
    def start_date(self, start_date: datetime):
        """Sets the start_date of this Indicator.

        :param start_date: The start_date of this Indicator.
        :type start_date: datetime
        """

        self._start_date = start_date

    @property
    def end_date(self) -> datetime:
        """Gets the end_date of this Indicator.

        :return: The end_date of this Indicator.
        :rtype: datetime
        """
        return self._end_date

    @end_date.setter
    def end_date(self, end_date: datetime):
        """Sets the end_date of this Indicator.

        :param end_date: The end_date of this Indicator.
        :type end_date: datetime
        """

        self._end_date = end_date

    @property
    def window_size(self) -> int:
        """Gets the window_size of this Indicator.

        :return: The window_size of this Indicator.
        :rtype: int
        """
        return self._window_size

    @window_size.setter
    def window_size(self, window_size: int):
        """Sets the window_size of this Indicator.

        :param window_size: The window_size of this Indicator.
        :type window_size: int
        """
        if window_size is None:
            raise ValueError("Invalid value for `window_size`, must not be `None`")  # noqa: E501

        self._window_size = window_size

    @property
    def bandwidth(self) -> float:
        """Gets the bandwidth of this Indicator.

        Only used for bollinger bands, this indicates the bandwidth  # noqa: E501

        :return: The bandwidth of this Indicator.
        :rtype: float
        """
        return self._bandwidth

    @bandwidth.setter
    def bandwidth(self, bandwidth: float):
        """Sets the bandwidth of this Indicator.

        Only used for bollinger bands, this indicates the bandwidth  # noqa: E501

        :param bandwidth: The bandwidth of this Indicator.
        :type bandwidth: float
        """

        self._bandwidth = bandwidth

    @property
    def values(self) -> List[IndicatorValue]:
        """Gets the values of this Indicator.

        :return: The values of this Indicator.
        :rtype: List[IndicatorValue]
        """
        return self._values

    @values.setter
    def values(self, values: List[IndicatorValue]):
        """Sets the values of this Indicator.

        :param values: The values of this Indicator.
        :type values: List[IndicatorValue]
        """

        self._values = values


import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import argparse

# List of parser command
parser = argparse.ArgumentParser(prog='Create plot', description='create plot and save/show it.')
parser.add_argument('-o', '--o', default='output-mode', type=int, nargs=1, choices=[0, 1],  help='Mode of output. 0 - show plot, 1- save plot')
parser.set_defaults(o=1)
args = parser.parse_args()

inpt = args.o[0]

def read_dataframe(path):
    """read_dataframe
    This function load the csv file into dataframe

    :param path: string to the csv file
    :return: df: dataframe with the csv file
    """
    df = pd.read_csv(path)
    return df

def get_average(df):
    """get_average
    This function count the average temperature for each year for each country

    :param df: the dataframe with the temperature data
    :return: result_df: dataframe with year, month-year date, average temperature and city column
    """
    df = df.loc[df['Country'] == 'Japan']
    df = df[['year', 'date', 'AverageTemperatureCelsius']]
    return df

def create_plot(df):
    """create_plot
    This function create the scatter plot
    each color correspond to different city
    with month-year date at the x-axis and average temperature column as y-axis
    Then it shows it to the user or save two figures:
    html with the interactive plot showing monthly differences
    png with the yearly plot

    :param df: datafram with the date and temperature for cities
    """
    global inpt
    df = df.groupby(['date'], as_index=False).mean()
    fig = px.scatter(df, x='date', y="AverageTemperatureCelsius",
                     template='plotly_white', title='Average temperature for Japan by month')
    if inpt == 0:
        fig.show()
    elif inpt == 1:
        fig.write_html('./images/fig2.html')
        create_yearly_plot(df)
        print('Image save in: ./images/fig2.png')
    else:
        raise Exception('incorrect command!')

def create_yearly_plot(df):
    """create_yearly_plot
    This function create the scatter plot
    each color correspond to different city
    with year date at the x-axis and average temperature column as y-axis
    then it save the plot as png file

    :param df: datafram with the date and temperature for cities
    """
    df = df.groupby(['year'], as_index=False).mean()
    fig, ax = plt.subplots(figsize=(10, 8))
    ax.scatter(df['year'], df['AverageTemperatureCelsius'], linewidth=1)
    ax.grid(True, linewidth=1, color='lightgrey', linestyle='-')
    ax.set_axisbelow(True)
    ax.set_title('Japan')
    fig.autofmt_xdate(rotation=30)
    fig.supxlabel('year', fontsize=16, weight=200)
    fig.suptitle('Average temperature', fontsize=22, weight=500)
    fig.supylabel('countryAverage', fontsize=16, weight=200)
    plt.tight_layout()
    plt.savefig('./images/fig2.png')

def main():
    df = read_dataframe('./data/temperature_clean.csv')
    result_df = get_average(df)
    create_plot(result_df)

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import os

from setuptools import setup, find_packages

from minode import shared

README = open(os.path.join(
    os.path.abspath(os.path.dirname(__file__)), 'README.md')).read()

name, version = shared.user_agent.strip(b'/').split(b':')

setup(
    name=name.decode('utf-8'),
    version=version.decode('utf-8'),
    description='Python 3 implementation of the Bitmessage protocol.'
    ' Designed only to route objects inside the network.',
    long_description=README,
    license='MIT',
    author='Krzysztof Oziomek',
    url='https://github.com/g1itch/MiNode',
    packages=find_packages(),
    package_data={'': ['*.csv', 'tls/*.pem']},
    entry_points={'console_scripts': ['minode = minode.main:main']},
    classifiers=[
        "License :: OSI Approved :: MIT License"
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3",
        "Topic :: Internet",
        "Topic :: Security :: Cryptography",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
)


import numpy as np
#import pandas as pd
import pickle
#import cv2
from sklearn.svm import SVC
from sklearn.cross_validation import train_test_split
import math

p = open('np_final_version.pickle', 'rb')
np_final_v = pickle.load(p)
p.close()

p = open('feature_vector.pickle', 'rb')
feature_vector = pickle.load(p)
p.close()

#p = open('/home/youssef/Downloads/Machine_Learning/Support_Vector_Machine/gender_classification/Feature_Vector_Final_CSV/image_path.pickle', 'rb')
#image_path = pickle.load(p)
#p.close()

#p = open('/home/youssef/Downloads/Machine_Learning/Support_Vector_Machine/gender_classification/Feature_Vector_Final_CSV/corrupted_image.pickle', 'rb')
#corrupted_image = pickle.load(p)
#p.close()

#p = open('SVM_3_Linear.pickle', 'rb')
#s = pickle.load(p)
#p.close()

#print(len(corrupted_image))
print(len(feature_vector))
feature_vector = np.array(feature_vector)
np_final_v = np_final_v[:, 3]

l = np_final_v.tolist()
# print(l)
for i in range(0,len(l)):
    if math.isnan(l[i]):
        l[i] = float(-1)

np_final_v = np.array(l)

X_train, X_test, y_train, y_test = train_test_split(feature_vector, np_final_v, test_size=0.2)
print(X_test.shape)
print(y_test.shape)
print(X_train.shape, y_train.shape)

clf = SVC(C=3, gamma='auto', kernel='linear')
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print(accuracy*100)