# -*- coding: utf-8 -*-
"""
Created on Tue Nov 24 18:12:19 2020

@author: Jasmine
"""

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

# read the input image
img_base = cv2.imread("./images/base.jpg")
img_rotated = cv2.imread("./images/rot266.jpg")

#     *********************************
#     *                               *
#     *   Detects Circles in Images   *
#     *                               *
#     *********************************

img = cv2.imread("./images/base.jpg", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img_base, cv2.COLOR_BGR2GRAY)
output = img.copy()

# Blur using 3 * 3 kernel.
gray_blurred = cv2.blur(gray, (3, 3))

# Apply Hough transform on the blurred image.
detected_circles = cv2.HoughCircles(gray_blurred,
                   cv2.HOUGH_GRADIENT, 1, 20, param1 = 50,
               param2 = 30, minRadius = 520, maxRadius = 550)

max_pt = [0, 0, 0]

# Draw circles that are detected.
if detected_circles is not None:

    # Convert the circle parameters a, b and r to integers.
    detected_circles = np.uint16(np.around(detected_circles))
    for pt in detected_circles[0, :]:
        a, b, r = pt[0], pt[1], pt[2]
        if max_pt[2] < r:
            max_pt[0] = a
            max_pt[1] = b
            max_pt[2] = r

    a, b, r = max_pt[0], max_pt[1], max_pt[2]
    print("origin: ", a, b)

    # Draw the circumference of the circle.
    cv2.circle(img, (a, b), r, (255, 0, 0), 2)

    # Draw a small circle (of radius 1) to show the center.
    cv2.circle(img, (a, b), 1, (255, 0, 0), 3)

#     *********************************
#     *                               *
#     *    Applies Filter on Image    *
#     *                               *
#     *********************************

img_base_hsv = cv2.cvtColor(img_base, cv2.COLOR_BGR2HSV)
img_rot_hsv = cv2.cvtColor(img_rotated, cv2.COLOR_BGR2HSV)

# find HSV using BGR
blue = np.uint8([[[138,76,16]]])
print(cv2.cvtColor(blue,cv2.COLOR_BGR2HSV))

# define range of blue color in HSV
# (H - 10, 100, 100)
# (H + 10, 255, 255)
lower_blue = np.array([95,100,100])
upper_blue = np.array([115,255,255])

# Threshold the HSV image to get only blue colors
mask_base = cv2.inRange(img_base_hsv, lower_blue, upper_blue)
mask_rot = cv2.inRange(img_rot_hsv, lower_blue, upper_blue)

# Bitwise-AND mask and base image
res_base = cv2.bitwise_and(img_base,img_base, mask=mask_base)
res_base = cv2.cvtColor(res_base, cv2.COLOR_BGR2RGB)

# Bitwise-AND mask and rotated image
res_rot = cv2.bitwise_and(img_rotated,img_rotated, mask=mask_rot)
res_rot = cv2.cvtColor(res_rot, cv2.COLOR_BGR2RGB)

# make copys of the results to display them
filtered_base = res_base.copy()
filtered_rot = res_rot.copy()

cv2.putText(filtered_base, "Filtered Base", (500, 1600),cv2.FONT_HERSHEY_SIMPLEX, 4.5, (255, 255, 255), 5)
plt.imshow(filtered_base),plt.show()
cv2.putText(filtered_rot, "Filtered Rotated", (450, 1600),cv2.FONT_HERSHEY_SIMPLEX, 4.5, (255, 255, 255), 5)
plt.imshow(filtered_rot),plt.show()

#     *********************************
#     *                               *
#     *    Finds Center of Shape      *
#     *                               *
#     *********************************

# Calculate the center of the shape after filter is applied
gray_base = cv2.cvtColor(res_base, cv2.COLOR_BGR2GRAY)
blurred_base = cv2.GaussianBlur(gray_base, (5, 5), 0)
thresh_base = cv2.threshold(blurred_base, 60, 255, cv2.THRESH_BINARY)[1]

gray_rot = cv2.cvtColor(res_rot, cv2.COLOR_BGR2GRAY)
blurred_rot = cv2.GaussianBlur(gray_rot, (5, 5), 0)
thresh_rot = cv2.threshold(blurred_rot, 60, 255, cv2.THRESH_BINARY)[1]

# find contours in the binary image
contours_base, hierarchy = cv2.findContours(thresh_base,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours_rot, hierarchy = cv2.findContours(thresh_rot,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# calculate moments for each contour
M = cv2.moments(contours_base[0])

# calculate x,y coordinate of center
if M["m00"] != 0:
 cX = int(M["m10"] / M["m00"])
 cY = int(M["m01"] / M["m00"])
else:
 cX, cY = 0, 0
cv2.circle(img_base, (cX, cY), 5, (255, 255, 255), 10)
cv2.putText(img_base, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 3.5, (0, 0, 0), 3)

# convert from BGR to RGB so we can plot using matplotlib
img_base = cv2.cvtColor(img_base, cv2.COLOR_BGR2RGB)
plt.imshow(img_base),plt.show()

img = cv2.arrowedLine(img, (a, b), (cX, cY), (0, 0, 255), 5)
print("base center:", cX, cY)
s = np.array([cX - a, -1*(cY - b), 0])

# calculate moments for each contour
M = cv2.moments(contours_rot[0])

# calculate x,y coordinate of center
if M["m00"] != 0:
 cX = int(M["m10"] / M["m00"])
 cY = int(M["m01"] / M["m00"])
else:
 cX, cY = 0, 0
cv2.circle(img_rotated, (cX, cY), 5, (255, 255, 255), 10)
cv2.putText(img_rotated, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 3.5, (0, 0, 0), 3)
# convert from BGR to RGB so we can plot using matplotlib
img_rotated = cv2.cvtColor(img_rotated, cv2.COLOR_BGR2RGB)
plt.imshow(img_rotated),plt.show()
print("rot center:", cX, cY)
t = np.array([(cX - a), -1*(cY - b), 0])

img = cv2.arrowedLine(img, (a, b), (cX, cY), (0, 0, 255), 5)

#     *********************************
#     *                               *
#     *       Calculates Angle        *
#     *                               *
#     *********************************

omega = np.array([0, 0, 1])
y = np.dot(np.transpose(omega), np.cross(s,t))
x = np.dot(s, t)

theta = math.atan2(y, x)
degree = np.rad2deg(theta)
degree = '%1.3f' % (degree)

cv2.putText(img, degree, (a, b + 100),cv2.FONT_HERSHEY_SIMPLEX, 3.5, (0, 0, 0), 3)
plt.imshow(img),plt.show()


import time

from tests.base import TestBase
from tests.utils import remove_test_data

from aim.sdk import Run

class TestRunResourceTracker(TestBase):
    def tearDown(self):
        remove_test_data()

    def test_default_tracking_interval(self):
        run = Run()  # default tracking interval 10s
        run_hash = run.hash
        run.track(1, name='metric')
        time.sleep(.1)  # allow tracker to add resource usage metrics
        del run

        metrics = list(self.repo.query_metrics(f'run.hash == "{run_hash}" and metric.name.startswith("__")'))
        self.assertEqual(4, len(metrics))
        expected_metrics = {'__system__cpu', '__system__disk_percent',
                            '__system__memory_percent', '__system__p_memory_percent'}
        self.assertSetEqual(expected_metrics, set(m.name for m in metrics))

    def test_custom_tracking_interval(self):
        run = Run(system_tracking_interval=1)
        run_hash = run.hash
        run.track(1, name='metric')
        time.sleep(3)  # allow tracker to add resource usage metrics
        del run

        metrics = list(self.repo.query_metrics(f'run.hash == "{run_hash}" and metric.name.startswith("__")'))
        self.assertEqual(4, len(metrics))
        expected_metrics = {'__system__cpu', '__system__disk_percent',
                            '__system__memory_percent', '__system__p_memory_percent'}
        self.assertSetEqual(expected_metrics, set(m.name for m in metrics))
        for metric in metrics:
            # 3 sec. runtime, 1 sec. interval
            self.assertGreaterEqual(len(metric.values), 3)
            self.assertLessEqual(len(metric.values), 4)

    def test_disable_resource_tracking(self):
        run = Run(system_tracking_interval=None)
        run_hash = run.hash
        run.track(1, name='metric')
        time.sleep(.1)  # allow tracker to add resource usage metrics
        del run

        metrics = list(self.repo.query_metrics(f'run.hash == "{run_hash}" and metric.name.startswith("__")'))
        self.assertListEqual([], metrics)

    def test_resource_tracking_interval_limits(self):
        run = Run(system_tracking_interval=0)
        self.assertIsNone(run._system_resource_tracker)
        run = Run(system_tracking_interval=2 * 24 * 3600)  # two days
        self.assertIsNone(run._system_resource_tracker)


import pytest
from webdriverwrapper import Chrome

import sys

sys.path.append("/Users/sridhariyer/Documents/testing/python_framework")
from features.exceptions.error import UnsupportedBrowserException

def pytest_addoption(parser):
    parser.addoption("--driver", action="store", default="chrome", help="Type in browser type")
    parser.addoption("--url", action="store", default="http://localhost:9292/login", help="url")
    parser.addoption("--username", action="store", default="tomsmith", help="username")
    parser.addoption("--password", action="store", default="SuperSecretPassword!", help="password")

@pytest.fixture(scope="module", autouse=True)
def driver(request):
    print("I am in the driver method")
    browser_name = request.config.getoption("--driver")
    if browser_name == 'chrome':
        browser_driver = Chrome()
        browser_driver.get(request.config.getoption("--url"))
        browser_driver.implicitly_wait(10)
        browser_driver.maximize_window()
        return browser_driver
    else:
        raise UnsupportedBrowserException("{} is not supported.".format(browser_name))

def pytest_bdd_before_scenario():
    print('This runs before all scenarios')


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

import traceback
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import random

# 存储为文本
def write2txt(data, path):
    f = open(path, 'a')
    f.write(data)
    f.write('\n')
    f.close()

# 获取id所喜欢的音乐列表
def catch_songs(url_id, url):
    user = url_id.split('=')[-1].strip()
    print('excute user: ', user)

    driver = webdriver.PhantomJS()
    driver.get(url)
    driver.switch_to.frame('g_iframe')

    try:
        wait = ui.WebDriverWait(driver, 15)
        wait.until(lambda driver: driver.find_element_by_xpath('//*[@class="j-flag"]/table/tbody'))
        try:
            song_key = 1
            wrong_time = 0
            while wrong_time < 5:
                try:
                    songs = driver.find_elements_by_xpath('//*[@class="j-flag"]/table/tbody/tr[%s]' % song_key)
                    info_ = songs[0].text.strip().split("\n")
                    if len(info_) == 5:
                        info_.insert(2, 'None')  # 没有MV选项的进行插入None
                    new_line = '%s|' % user + '|'.join(info_)
                    song_key += 1
                    print(new_line)
                    write2txt(new_line.encode('utf-8'), user)
                except Exception as ex:
                    wrong_time += 1
                    print(wrong_time)
        except Exception as ex:
            pass

    except Exception as ex:
        traceback.print_exc()
    finally:
        driver.quit()

# 获取id所喜爱的音乐url
def catch_play_url(url):
    driver = webdriver.PhantomJS()
    driver.get(url)
    driver.switch_to.frame('g_iframe')

    try:
        wait = ui.WebDriverWait(driver, 15)
        wait.until(lambda driver: driver.find_element_by_xpath('//*[@class="m-cvrlst f-cb"]/li[1]/div/a'))
        # urls = driver.find_elements_by_xpath('//*[@class="m-cvrlst f-cb"]/li[1]/div/a')
        urls = driver.find_elements_by_css_selector('[href^="/playlist"]')
        url_hrefs = []
        for url in list(urls):
            url_hrefs.append(url.get_attribute("href"))
    except Exception as ex:
        traceback.print_exc()
    finally:
        driver.quit()

    return list(set(url_hrefs))

if __name__ == '__main__':
    # for url in ['http://music.163.com/#/user/home?id=550572996']:
    #     time.sleep(random.randint(2, 4)) # 随机休眠2-4秒
    #     url_playlist = catch_play_url(url)
    #     for href in url_playlist:
    #         print href
    #         time.sleep(random.randint(1, 2))
    #         catch_songs(url, href)

    time.sleep(random.randint(2, 4))  # 随机休眠2-4秒
    catch_songs('http://music.163.com/#/playlist?id=825643580', 'http://music.163.com/#/playlist?id=825643580')


import requests, json

def get_api_url():
    url_pulse_base = "{}://{}:{}/{}".format(
        Pulse.dj_portocol, Pulse.dj_host, Pulse.dj_port,
        Pulse.api_base
    )
    return url_pulse_base

class Pulse:
    dj_host  = 'mpcore-vm'
    dj_port = '80'
    dj_portocol = 'http'
    api_base = 'api/v1/'

    def __init__(self):
        pass

    @property
    def get_attribute(self):
        resolume_base_url = get_api_url()
        query_url = "{}interface/osc/?name_unique=resolume".format(
            resolume_base_url
        )
        resolume_json = requests.get( query_url )
        try:
            resolume_attrib = json.loads( resolume_json.text)
            resolume_attrib = resolume_attrib['results'][0]
        except IndexError as err:
            print( 'Resolume.get_attribute:{}'.format(
                err
            ))
        return resolume_attrib


import hashlib
from pandas import isnull

def hash_object(obj, encoding='latin-1'):

    def _create_hash(_obj):
        if isinstance(_obj, (tuple, list)):
            return str(tuple(_create_hash(i) for i in _obj))
        elif isinstance(_obj, set):
            return str(frozenset(_obj))
        elif isinstance(_obj, dict):
            return str(
                tuple((k, _create_hash(v)) for k, v in sorted(_obj.items()))
            )
        elif callable(_obj):
            return _obj.__name__
        return str(_obj)

    hashed = _create_hash(obj)
    if encoding is None:
        hashed_bytes = bytes(str(hashed))
    else:
        hashed_bytes = bytes(str(hashed), encoding=encoding)
    return hashlib.md5(hashed_bytes).hexdigest()

def is_empty(val):
    if val is None:
        return True
    elif isnull(val):
        return True
    elif val.lower() in ('none', 'nan'):
        return True
    return False


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

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append('../')

from SpiderInterface.queue.QueueFactory import QueueFactory
from SpiderInterface.field.FieldFactory import FieldFactory
from SpiderInterface.browser.BrowserFactory import BrowserFactory
from SpiderInterface.log.Logging import Logging
import json
import time
import private_config
from common import config
import random

'''
新浪四川板块：[资讯，运动，教育，艺术，财经]在未登陆状态下，只能加载最多5页数据
新浪新闻版块：【滚动,国内，国际，社会，评论】
http://interface.sina.cn/wap_api/layout_col.d.json?&col=56261&page=2
http://interface.sina.cn/wap_api/layout_col.d.json?col=38790&level=1%2C2&page=2

'''

class zw_Producer():

    def __init__(self):
        # 实例化工厂对象
        self.queue = QueueFactory()
        self.field_factory = FieldFactory(u'新浪四川')
        self.browser_factory = BrowserFactory()
        # 实例化具体对象
        self.log = Logging('./Log/logNote').get_logging()
        self.browser = self.browser_factory.create(config.browser_type)
        self.queue = self.queue.create(config.queue_type, private_config.queue_table,
                                                            config.queue_host, config.queue_port)

    def main(self):#获取首页新闻
        sina_lanmu=['76905','178710','173917', '173585','74162','38790','56261','56262','56264','56266']#资讯，运动，教育，艺术，财经
        # sina_lanmu=['38790']
        link=''
        for id in sina_lanmu:
            # print id
            page_num = 1
            while(True):
                try:
                    if id in ['38790','56261','56262','56264','56266']:
                        link = 'http://interface.sina.cn/wap_api/layout_col.d.json?col=%s&page=%s' % (id,str(page_num))
                    else:
                        link='http://interface.sina.cn/dfz/outside/wap/news/list.d.html?col=%s&page=%s'%(id,str(page_num))
                    page_data=self.browser.visit(link)
                    if page_data:
                        flag = self.getUrl(page_data)
                        if flag ==0:
                            break
                    else:
                        break
                    # print page_num
                    page_num += 1
                except Exception as e:
                    self.log.info('%s worng,error %s'%(link,e))
                    break
        self.log.info('get the url success!')

    def getUrl(self,page_data):
        datas=json.loads(page_data)
        data_list = datas['result']['data']
        if len(data_list['list'])>0:
            for item in data_list['list']:
                url=item['URL']
                comment =item['comment']
                # print url
                self.queue.put(url+'@@@@@@'+str(comment))
        else:
            # self.log.info('page is empty!')
            return 0

if __name__ == '__main__':
    producer = zw_Producer()
    while(True):
        producer.main()
        time.sleep(60*60*4)


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

execfile('../model_data.py')
execfile('../captionTexts.py')
from postprocess.xcVtk.FE_model import vtk_FE_graphic

defDisplay= vtk_FE_graphic.RecordDefDisplayEF()
setToDisp=overallSet
defDisplay.FEmeshGraphic(xcSets=[setToDisp],caption='',viewNm='XYZPos',defFScale=1.0)


# Import Library
import numpy as np
import sklearn
import scipy
import pandas
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import KFold, RepeatedKFold
from sklearn import metrics
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.metrics import (brier_score_loss, precision_score, recall_score, f1_score)
from sklearn.metrics import precision_recall_fscore_support
from sklearn.multiclass import OneVsOneClassifier
from sklearn.multiclass import OutputCodeClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.neighbors import KNeighborsClassifier

# Read data from csv file #
data = pandas.read_csv("data.csv");
# print(data.head());

# Divide data into training data and ground truth #
X = data.drop('QN', axis=1).drop('ground_truth', axis=1).drop('ground_truth_binary', axis=1);
y2 = data['ground_truth']; # full categorical ground truth
y = data['ground_truth_binary']; # binary ground truth
# print(X.head());
# print(Y);ß
# print(Y2);

##########
# K-Fold #
##########
########################################################################################################################
repeated_times = 10;
kf = RepeatedKFold(n_splits=5, n_repeats=repeated_times, random_state=np.random);
kf.get_n_splits(X);
# print(kf);

# Perform 5-fold cross validation #
svm_score = [0, 0, 0]; # single class score for svm
svm_score2 = [0, 0, 0]; # multi-class score for svm
gnb_score = [0, 0, 0]; # single class score for gnb
gnb_score2 = [0, 0, 0]; # multi-class score for gnb
knn_score = [0, 0, 0]; # single class score for knn
knn_score2 = [0, 0, 0]; # multi-class score for knn
dtree_score = [0, 0, 0]; # single class score for dtree
dtree_score2 = [0, 0, 0]; # multi-class score for dtree

for train_index, test_index in kf.split(X):
    split_training_sets = "TRAIN:" + str(train_index) + "\n" + "TEST:" + str(test_index);
    # print(split_training_sets);

    ##################################
    # Split into train and test sets #
    ##################################
    X_train, X_test = np.array(X)[train_index], np.array(X)[test_index];
    y_train, y_test = np.array(y)[train_index], np.array(y)[test_index];
    X2_train, X2_test = np.array(X)[train_index], np.array(X)[test_index];
    y2_train, y2_test = np.array(y2)[train_index], np.array(y2)[test_index];

    #######
    # SVM #
    #######
    svm = SVC(kernel='rbf', gamma=0.05, C=1);
    svm.fit(X_train, y_train);
    y_pred_svm = svm.predict(X_test);
    # svm2 = SVC(kernel='rbf', gamma=0.1, C=1);
    svm2 = OneVsRestClassifier(LinearSVC(random_state=0));
    svm2.fit(X2_train, y2_train);
    y2_pred_svm = svm2.predict(X2_test);
    # print(confusion_matrix(y_test, y_pred_svm));
    # print(classification_report(y_test, y_pred_svm));
    # print(svm.score(X_test, y_test));  # accuracy
    # print("\tF1: %1.3f\n" % f1_score(y_test, y_pred_svm));
    # print("\tLOSS: %1.3f\n" % brier_score_loss(y_test, y_pred_svm));
    # print("\tPrecision: %1.3f\n" % precision_score(y_test, y_pred_svm));
    # print("\tRecall: %1.3f\n" % recall_score(y_test, y_pred_svm));

    # average=None # include all information of classification report;
    # 'micro' compute the f1-score using the global count of true positives / false negatives, etc.
    # 'macro' makes the average of the f1-score for each class: that's the avg / total result above
    # 'weighted' computes a weighted average of the f1-score. the more elements a class has,
    # the more important the f1-score for this class in the computation.
    score_svm = precision_recall_fscore_support(y_test, y_pred_svm, beta=1.0, labels=None, pos_label=1, average='macro',
                                            warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report_svm = classification_report(y_test,y_pred_svm);
    # print(score);
    score2_svm = precision_recall_fscore_support(y2_test, y2_pred_svm, beta=1.0, average='macro', pos_label=None,
                                             warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report2_svm = classification_report(y2_test,y2_pred_svm);
    # print(score2);

    # Final score of svm calculate
    svm_score[0] = svm_score[0] + score_svm[0];
    svm_score[1] = svm_score[1] + score_svm[1];
    svm_score[2] = svm_score[2] + score_svm[2];
    svm_score2[0] = svm_score2[0] + score2_svm[0];
    svm_score2[1] = svm_score2[1] + score2_svm[1];
    svm_score2[2] = svm_score2[2] + score2_svm[2];

    # Store scores to database

    ####################################################################################################################
    ###############
    # Naive Bayes #
    ###############
    gnb = GaussianNB().fit(X_train, y_train);
    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y_pred_gnb = gnb.predict(X_test);

    # Report #
    # print(confusion_matrix(y_test, y_pred_gnb))
    # print(classification_report(y_test, y_pred_gnb))
    # print(gnb.score(X_test, y_test));  # accuracy

    # Naive Bayes multi-class
    gnb2 = GaussianNB().fit(X2_train, y2_train);
    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y2_pred_gnb = gnb2.predict(X2_test);

    # Score
    score_gnb = precision_recall_fscore_support(y_test, y_pred_gnb, beta=1.0, labels=None, pos_label=1,
                                                average='macro',
                                                warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report_gnb = classification_report(y_test, y_pred_gnb);
    # print(score);
    score2_gnb = precision_recall_fscore_support(y2_test, y2_pred_gnb, beta=1.0, average='macro', pos_label=None,
                                                 warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report2_gnb = classification_report(y2_test, y2_pred_gnb);

    # Final score of gnb calculate
    gnb_score[0] = gnb_score[0] + score_gnb[0];
    gnb_score[1] = gnb_score[1] + score_gnb[1];
    gnb_score[2] = gnb_score[2] + score_gnb[2];
    gnb_score2[0] = gnb_score2[0] + score2_gnb[0];
    gnb_score2[1] = gnb_score2[1] + score2_gnb[1];
    gnb_score2[2] = gnb_score2[2] + score2_gnb[2];
    ####################################################################################################################
    ###########################
    # KNN k-nearest neighbours#
    ###########################
    knn = KNeighborsClassifier(n_neighbors=7).fit(X_train, y_train);

    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y_pred_knn = knn.predict(X_test);

    # KNN k-nearest neighbours multi-class
    knn2 = KNeighborsClassifier(n_neighbors=7).fit(X2_train, y2_train);

    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y2_pred_knn = knn2.predict(X2_test);

    # Score
    score_knn = precision_recall_fscore_support(y_test, y_pred_knn, beta=1.0, labels=None, pos_label=1,
                                                average='macro',
                                                warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report_knn = classification_report(y_test, y_pred_knn);
    # print(score);
    score2_knn = precision_recall_fscore_support(y2_test, y2_pred_knn, beta=1.0, average='macro', pos_label=None,
                                                 warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report2_knn = classification_report(y2_test, y2_pred_knn);

    # Final score of knn calculate
    knn_score[0] = knn_score[0] + score_knn[0];
    knn_score[1] = knn_score[1] + score_knn[1];
    knn_score[2] = knn_score[2] + score_knn[2];
    knn_score2[0] = knn_score2[0] + score2_knn[0];
    knn_score2[1] = knn_score2[1] + score2_knn[1];
    knn_score2[2] = knn_score2[2] + score2_knn[2];
    ####################################################################################################################
    #################
    # Decision Tree #
    #################
    dtree = DecisionTreeClassifier(max_depth=3).fit(X_train, y_train);

    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y_pred_dtree = dtree.predict(X_test);

    # Decision Tree multi-class
    dtree2 = DecisionTreeClassifier(max_depth=3).fit(X2_train, y2_train);

    # Testfrom sklearn.metrics import classification_report, confusion_matrix
    y2_pred_dtree = dtree2.predict(X2_test);

    # Score
    score_dtree = precision_recall_fscore_support(y_test, y_pred_dtree, beta=1.0, labels=None, pos_label=1,
                                                average='macro',
                                                warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report_dtree = classification_report(y_test, y_pred_dtree);
    # print(score);
    score2_dtree = precision_recall_fscore_support(y2_test, y2_pred_dtree, beta=1.0, average='macro', pos_label=None,
                                                 warn_for=('precision', 'recall', 'f-score'), sample_weight=None);
    report2_dtree = classification_report(y2_test, y2_pred_dtree);

    # Final score of dtree calculate
    dtree_score[0] = dtree_score[0] + score_dtree[0];
    dtree_score[1] = dtree_score[1] + score_dtree[1];
    dtree_score[2] = dtree_score[2] + score_dtree[2];
    dtree_score2[0] = dtree_score2[0] + score2_dtree[0];
    dtree_score2[1] = dtree_score2[1] + score2_dtree[1];
    dtree_score2[2] = dtree_score2[2] + score2_dtree[2];
    ####################################################################################################################

print("\nSVM");
print("Binary Classifier:");
svm_score[0] = svm_score[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % svm_score[0]);
svm_score[1] = svm_score[1]/(5*repeated_times);
print("\tRecall: %1.4f" % svm_score[1]);
svm_score[2] = svm_score[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % svm_score[2]);
print("Multi-class Classifier:");
svm_score2[0] = svm_score2[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % svm_score2[0]);
svm_score2[1] = svm_score2[1]/(5*repeated_times);
print("\tRecall: %1.4f" % svm_score2[1]);
svm_score2[2] = svm_score2[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % svm_score2[2]);

print("\nNaive Bayes");
print("Binary Classifier:");
gnb_score[0] = gnb_score[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % gnb_score[0]);
gnb_score[1] = gnb_score[1]/(5*repeated_times);
print("\tRecall: %1.4f" % gnb_score[1]);
gnb_score[2] = gnb_score[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % gnb_score[2]);
print("Multi-class Classifier:");
gnb_score2[0] = gnb_score2[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % gnb_score2[0]);
gnb_score2[1] = gnb_score2[1]/(5*repeated_times);
print("\tRecall: %1.4f" % gnb_score2[1]);
gnb_score2[2] = gnb_score2[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % gnb_score2[2]);

print("\nK-Nearest Neighbours");
print("Binary Classifier:");
knn_score[0] = knn_score[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % knn_score[0]);
knn_score[1] = knn_score[1]/(5*repeated_times);
print("\tRecall: %1.4f" % knn_score[1]);
knn_score[2] = knn_score[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % knn_score[2]);
print("Multi-class Classifier:");
knn_score2[0] = knn_score2[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % knn_score2[0]);
knn_score2[1] = knn_score2[1]/(5*repeated_times);
print("\tRecall: %1.4f" % knn_score2[1]);
knn_score2[2] = knn_score2[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % knn_score2[2]);

print("\nDesicion Tree");
print("Binary Classifier:");
dtree_score[0] = dtree_score[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % dtree_score[0]);
dtree_score[1] = dtree_score[1]/(5*repeated_times);
print("\tRecall: %1.4f" % dtree_score[1]);
dtree_score[2] = dtree_score[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % dtree_score[2]);
print("Multi-class Classifier:");
dtree_score2[0] = dtree_score2[0]/(5*repeated_times);
print("\tPrecision: %1.4f" % dtree_score2[0]);
dtree_score2[1] = dtree_score2[1]/(5*repeated_times);
print("\tRecall: %1.4f" % dtree_score2[1]);
dtree_score2[2] = dtree_score2[2]/(5*repeated_times);
print("\tF1-score: %1.4f" % dtree_score2[2]);

########################################################################################################################
# End of K-Fold #
########################################################################################################################

Experiment/5 SVM KNN .../kfold_svm_bayes_cnn_macro.py


class Solution:
    def nthUglyNumber(self, n):
        ele = [2, 3, 5]
        stack = []
        res = {}
        res[0] = 1
        for i in range(n):
            for j in range(len(ele)):
                if res[i] * ele[j] not in stack:
                    stack.append(res[i] * ele[j])
            if i+1 < n:
                res[i+1] = stack.pop()
        return res[n-1]
    def nthUglyNumber2(self, n):
        import pdb;pdb.set_trace()
        ugly = [1]
        i2, i3, i5 = 0, 0, 0
        while n > 1:
            u2, u3, u5 = 2*ugly[i2], 3*ugly[i3], 5*ugly[i5]
            umin = min(u2, u3, u5)
            if umin == u2:
                i2 +=1
            if umin == u3:
                i3 +=1
            if umin == u5:
                i5 +=1
            ugly.append(umin)
            n -=1
        return ugly[-1]

ins = Solution()
result = ins.nthUglyNumber2(10)
print(result)


# -*- coding: utf-8 -*-
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.

""" This module is responsible to capture the compiler invocation of any
build process. The result of that should be a compilation database.

This implementation is using the LD_PRELOAD or DYLD_INSERT_LIBRARIES
mechanisms provided by the dynamic linker. The related library is implemented
in C language and can be found under 'libear' directory.

The 'libear' library is capturing all child process creation and logging the
relevant information about it into separate files in a specified directory.
The input of the library is therefore the output directory which is passed
as an environment variable.

This module implements the build command execution with the 'libear' library
and the post-processing of the output files, which will condensates into a
(might be empty) compilation database. """

import logging
import subprocess
import argparse
import json
import sys
import os
import os.path
import re
import glob
import shlex
import pkg_resources
from analyzer import create_parser
from analyzer.decorators import to_logging_level, trace, entry
from analyzer.command import parse as cmdparse
from analyzer.command import Action

if 'darwin' == sys.platform:
    ENVIRONMENTS = [("ENV_OUTPUT", "BEAR_OUTPUT"),
                    ("ENV_PRELOAD", "DYLD_INSERT_LIBRARIES"),
                    ("ENV_FLAT", "DYLD_FORCE_FLAT_NAMESPACE")]
else:
    ENVIRONMENTS = [("ENV_OUTPUT", "BEAR_OUTPUT"),
                    ("ENV_PRELOAD", "LD_PRELOAD")]

@entry
def bear():
    """ Entry point for 'bear'.

        This part initializes some parts and forwards to the main method. """

    parser = initialize_command_line(create_parser())
    advanced = parser.add_argument_group('advanced options')
    advanced.add_argument(
        '--append',
        action='store_true',
        help="""Append new entries to existing compilation database.""")
    advanced.add_argument(
        '--disable-filter', '-n',
        dest='filtering',
        action='store_true',
        help="""Disable filter, unformated output.""")

    args = parser.parse_args()

    logging.getLogger().setLevel(to_logging_level(args.verbose))
    logging.debug(args)

    if args.help or 0 == len(args.build):
        parser.print_help()
        return 0

    return main(args)

def main(args):
    """ The reusable entry point of 'bear'.

        The 'scan-build' and 'bear' are the two entry points of this code.
        Both provide the parsed argument object as input for this job. """

    def load_current_cdb(filename):
        """ Load existing cdb elements when cdb file is readable. """
        if os.path.exists(filename):
            with open(filename) as handle:
                return json.load(handle)
        else:
            return []

    exit_code = 0
    with TemporaryDirectory(prefix='bear-') as tmpdir:
        exit_code = run_build(args.build, tmpdir)
        append = 'append' in args and args.append
        currents = load_current_cdb(args.cdb) if append else []
        filtering = 'filtering' in args and args.filtering
        commands = merge(currents, collect(not filtering, tmpdir))
        with open(args.cdb, 'w+') as handle:
            json.dump(commands, handle, sort_keys=True, indent=4)
    return exit_code

@trace
def initialize_command_line(parser):
    """ Add task related argument to the command line parser. """
    parser.add_argument(
        dest='build',
        nargs=argparse.REMAINDER,
        help="""Command to run.""")

    return parser

@trace
def run_build(command, destination):
    """ Runs the original build command.

    It sets the required environment variables and execute the given command.
    The exec calls will be logged by the 'libear' preloaded library. """

    def get_ear_so_file():
        lib_name = 'libear.dylib' if 'darwin' == sys.platform else 'libear.so'
        return pkg_resources.resource_filename('analyzer', lib_name)

    environment = dict(os.environ)
    for alias, key in ENVIRONMENTS:
        value = '1'
        if alias == 'ENV_PRELOAD':
            value = get_ear_so_file()
        elif alias == 'ENV_OUTPUT':
            value = destination
        environment.update({key: value})

    child = subprocess.Popen(command, env=environment)
    child.wait()
    return child.returncode

@trace
def collect(filtering, destination):
    """ Collect the execution information from the output directory. """
    def parse(filename):
        """ Parse the file generated by the 'libear' preloaded library. """
        RS = chr(0x1e)
        US = chr(0x1f)
        with open(filename, 'r') as handler:
            content = handler.read()
            records = content.split(RS)
            return {'pid': records[0],
                    'ppid': records[1],
                    'function': records[2],
                    'directory': records[3],
                    'command': records[4].split(US)[:-1]}

    def general_filter(iterator):
        """ Filter out the non compiler invocations. """
        def known_compiler(command):
            patterns = [
                re.compile(r'^([^/]*/)*c(c|\+\+)$'),
                re.compile(r'^([^/]*/)*([^-]*-)*g(cc|\+\+)(-[2345].[0-9])?$'),
                re.compile(r'^([^/]*/)*([^-]*-)*clang(\+\+)?(-[23].[0-9])?$'),
                re.compile(r'^([^/]*/)*llvm-g(cc|\+\+)$'),
            ]
            executable = command[0]
            for pattern in patterns:
                if pattern.match(executable):
                    return True
            return False

        def cancel_parameter(command):
            patterns = [
                re.compile(r'^-cc1$')
            ]
            for pattern in patterns:
                for arg in command[1:]:
                    if pattern.match(arg):
                        return True
            return False

        for record in iterator:
            command = record['command']
            if known_compiler(command) and not cancel_parameter(command):
                yield record

    def format_record(iterator):
        """ Generate the desired fields for compilation database entries. """
        def join_command(args):
            """ Create a single string from list.

            The major challenge, which is not solved yet, to deal with white
            spaces. Which are used by the shell as separator.
            (Eg.: -D_KEY="Value with spaces") """
            return ' '.join(args)

        for record in iterator:
            atoms = cmdparse({'command': record['command']}, lambda x: x)
            if atoms['action'] == Action.Compile:
                for filename in atoms['files']:
                    yield {'directory': record['directory'],
                           'command': join_command(record['command']),
                           'file': os.path.abspath(filename)}

    chain = lambda x: format_record(general_filter(x))

    generator = [parse(record)
                 for record
                 in glob.iglob(os.path.join(destination, 'cmd.*'))]
    return list(chain(generator)) if filtering else generator

@trace
def merge(old, new):
    """ Merge two list of commands into one. """
    def duplicate(state, entry):
        """ Find out repetition amongst the merged items. """
        def hash_cdb(entry):
            """ Make a unique hash for cdb entries to detect duplicates. """
            # On OS X the 'cc' and 'c++' compilers are wrappers for 'clang'
            # therefore both call would be logged. To avoid this the hash does
            # not contain the first word of the command.
            command = ' '.join(shlex.split(entry['command'])[1:])
            # For faster lookup in set filename is reverted
            filename = entry['file'][::-1]
            # For faster lookup in set directory is reverted
            directory = entry['directory'][::-1]
            return '<>'.join([filename, directory, command])

        if os.path.exists(entry['file']):
            entry_hash = hash_cdb(entry)
            if entry_hash not in state:
                state.add(entry_hash)
                return False
        return True

    state = set()
    return [entry for entry in old + new if not duplicate(state, entry)]

if sys.version_info.major >= 3 and sys.version_info.minor >= 2:
    from tempfile import TemporaryDirectory
else:
    class TemporaryDirectory(object):
        """ This function creates a temporary directory using mkdtemp() (the
        supplied arguments are passed directly to the underlying function).
        The resulting object can be used as a context manager. On completion
        of the context or destruction of the temporary directory object the
        newly created temporary directory and all its contents are removed
        from the filesystem. """
        def __init__(self, **kwargs):
            from tempfile import mkdtemp
            self.name = mkdtemp(*kwargs)

        def __enter__(self):
            return self.name

        def __exit__(self, _type, _value, _traceback):
            self.cleanup()

        def cleanup(self):
            from shutil import rmtree
            if self.name is not None:
                rmtree(self.name)


#	This script loads the network and trains it
from LeNet import LeNet

from keras.datasets import cifar10
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import backend as K
K.set_image_dim_ordering('th')

import tensorflow as tf
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

import numpy as np
import cv2
from sklearn.metrics import confusion_matrix

save = False
load = True
loadModelWeightsPath = 'weights_cifar.h5'

#	Initialize random seed
seed = 7
np.random.seed(seed)

#	Load CIFAR10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

#	Normalize data
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train / 255.0
x_test = x_test / 255.0

#	One hot encoding
y_train = np_utils.to_categorical(y_train)
y_test_OneHot = np_utils.to_categorical(y_test)
num_classes = y_test_OneHot.shape[1]

#	Use LeNet
model = LeNet.build(32, 32, 3, num_classes, load, loadModelWeightsPath)

#	Compile model
epochs = 25
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr = lrate, momentum = 0.9, decay = decay, nesterov=False)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
print(model.summary())

#	Fit the model if didn't load
if not load:
	model.fit(x_train, y_train, validation_data=(x_test, y_test_OneHot), epochs = epochs, batch_size=32, verbose=2, shuffle=True)

#	Save weights if set to
if save:
	model.save_weights(loadModelWeightsPath, overwrite=True)

# Evaluation of model

# Accuracy
scores = model.evaluate(x_test, y_test_OneHot, verbose=1)
print("Test accuracy: %.2f%%" % (scores[1]*100))

#	Confusion matrix
y_test_list = y_test.flatten().tolist()
pred = model.predict(x_test)
y_pred = [x.tolist().index(max(x)) for x in pred]

print(confusion_matrix(y_test_list, y_pred))


"""
File: parser.py
Author: Aaron Bach
Email: bachya1208@gmail.com
Github: https://github.com/bachya/regenmaschine
"""

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

import maya

import regenmaschine.api as api

class Watering(api.BaseAPI):
    """ An alternate object to deal with any watering-related activities """

    def log(self, date=None, num_of_days=None, details=False):
        """ Returns the current watering log """
        if details:
            api_route = 'watering/log/details'
        else:
            api_route = 'watering/log'

        if date and num_of_days:
            parser = maya.when(date)
            date = parser.datetime().strftime('%Y-%m-%d')
            api_route = '{}/{}/{}'.format(api_route, date, num_of_days)

        return self.get(api_route).object.json()

    def queue(self):
        """ Returns the queue of active watering activities """
        return self.get('watering/queue').object.json()

    def runs(self, date, num_of_days):
        """ Similar to log, but returns et0 and qpf info, as well """
        parser = maya.when(date)
        date = parser.datetime().strftime('%Y-%m-%d')
        return self.get(
            'watering/past/{}/{}'.format(date, num_of_days)).object.json()

    def stop_all(self):
        """ Stops all programs and zones from running """
        return self.post('watering/stopall').object.json()


__author__ = 'PB'
__copyright__ = 'Copyright 2015, Compugene LLC'
__status__ = 'Development'

import data.settings as loc
import common.pcaica2 as cpi
import numpy as np
from time import sleep
__COMP_DIR__ = loc.Data.Macro.ETFsCEFs.Composite()
__COMP_DIR_CLEAN__ = __COMP_DIR__.Clean()
__FACTOR_DIR__ = __COMP_DIR__.Factors()
__FACTOR_DIR_RAW__ = __FACTOR_DIR__.Raw()
__FACTOR_DIR_CLEAN__ = __FACTOR_DIR__.Clean()
__FACTOR_DIR_RETURN__ = __FACTOR_DIR__.Returns()
__FACTOR_DIR_BETA__ = __FACTOR_DIR__.Betas()
__OPTIM_BETA__ = __COMP_DIR__.Optim.Betas()

import common.util.frames as cuf
import datetime as dt
import common.filters as cf
import gc
import pandas as pd

def _get_tick_cols(x):
    return [i for i in x.tolist() if i != 'Date']

# import data.yahoo.process as dyp
# dyp.display_dead()

def refresh_pca():
    window = 200
    comb_ret = __COMP_DIR_CLEAN__.retrieve('Returns')
    n = comb_ret.shape[0]
    tick_cols = _get_tick_cols(comb_ret.columns)
    for i in range(window+1, n):
        comb_ret_ext = comb_ret[i-window:i-1, :]
        y1 = np.all(~np.isnan(comb_ret_ext[tick_cols].values), axis=0).tolist()
        ticks = [k for k, j in zip(tick_cols, y1) if j]
        comb_ret_ext_ = np.transpose(comb_ret_ext[ticks].values)
        # get the pca, ica factor weights and returns
        pca_wgt, pca_ret = cpi.compute_fast_pca(comb_ret_ext_)
        f_name = comb_ret_ext['Date', list][-1]
        f_name = f_name.__str__()[0:10].replace('-', '')
        __FACTOR_DIR_RAW__.save(f_name, ticks, pca_wgt, pca_ret)  # , ica_wgt, ica_ret

def update_pca():
    window = 200
    comb_ret = __COMP_DIR_CLEAN__.retrieve('Returns')
    n = comb_ret.shape[0]
    tick_cols = _get_tick_cols(comb_ret.columns)
    f_list_cur = __FACTOR_DIR_RAW__.listdir()
    f_dates_new = [i.__str__()[0:10].replace('-', '') for i in comb_ret['Date', list]]
    f_dates_new = [i for i in f_dates_new if i > max(f_list_cur)]
    f_dates_new_idx = [i for i, j in enumerate(comb_ret['Date', list]) if j.__str__()[0:10].replace('-', '') in
                       f_dates_new]
    for i in f_dates_new_idx:
        # i = f_dates_new_idx[0]
        comb_ret_ext = comb_ret[i-window+1:i, :]
        y1 = np.all(~np.isnan(comb_ret_ext[tick_cols].values), axis=0).tolist()
        ticks = [k for k, j in zip(tick_cols, y1) if j]
        comb_ret_ext_ = np.transpose(comb_ret_ext[ticks].values)
        # get the pca, ica factor weights and returns
        pca_wgt, pca_ret = cpi.compute_fast_pca(comb_ret_ext_)
        f_name = comb_ret_ext['Date', list][-1]
        f_name = f_name.__str__()[0:10].replace('-', '')
        __FACTOR_DIR_RAW__.save(f_name, ticks, pca_wgt, pca_ret)  # , ica_wgt, ica_ret

def assign(w1, w2):
    # w1 is current
    # w2 is previous
    n = w1.shape[0]
    as_mat = np.zeros((n, n))
    for k1 in range(0, n):
        for k2 in range(0, n):
            as_mat[k1, k2] = 1-np.abs(np.dot(w1[k1], w2[k2])/np.sqrt(np.dot(w1[k1], w1[k1])*np.dot(w2[k2], w2[k2])))
    as_min = np.amin(as_mat, axis=1)
    for k1 in range(0, n):
        as_mat[k1] = as_mat[k1] - as_min[k1]
    as_min = np.amin(as_mat, axis=0)
    for k2 in range(0, n):
        as_mat[:, k2] = as_mat[:, k2]-as_min[k2]
    as_mat = np.round(as_mat, decimals=4)
    as_vec = np.empty(n)*np.nan
    # make the assignment
    for k1 in range(0, n):
        as_vec_idx = (as_mat[k1] < 0.0001).nonzero()[0].tolist()
        if (len(as_vec_idx) == 1) and (as_vec_idx[0] not in as_vec[~np.isnan(as_vec)].tolist()):
            as_vec[k1] = as_vec_idx[0]
    # if any unassigned, arbitrarily assign them
    if any(np.isnan(as_vec)):
        wh_rows = [i for i, j in enumerate(as_vec.tolist()) if np.isnan(j)]
        wh_cols = list(set(range(0, n)).difference(set(as_vec[~np.isnan(as_vec)])))
        for i, j in enumerate(wh_rows):
            as_vec[j] = wh_cols[i]
    # check the signs
    as_sign = np.empty(n)*np.nan
    for k1 in range(0, n):
        as_sign[k1] = np.sign(np.dot(w1[k1], w2[as_vec[k1]])/np.sqrt(np.dot(w1[k1], w1[k1])*np.dot(w2[as_vec[k1]],
                                                                                w2[as_vec[k1]])))
    return as_vec, as_sign

np.set_printoptions(linewidth=150)

def refresh_pca_clean():
    f_files = __FACTOR_DIR_RAW__.listdir()
    f_files = sorted(f_files)
    for i, j in enumerate(f_files):
        if i == 0:
            t1 = __FACTOR_DIR_RAW__.load(f_files[0])
            __FACTOR_DIR_CLEAN__.save(f_files[0], t1['arr_0'], t1['arr_1'], t1['arr_2'], np.array(range(0, 20),
                                                                                dtype=np.int64))
        else:
            t1 = __FACTOR_DIR_CLEAN__.load(f_files[i-1])
            if t1 is None:
                sleep(0.2)
                t1 = __FACTOR_DIR_CLEAN__.load(f_files[i-1])
            t2 = __FACTOR_DIR_RAW__.load(f_files[i])
            t1_ticks = list(t1['arr_0'])
            t2_ticks = list(t2['arr_0'])
            t_ticks = list(set(t1_ticks).intersection(set(t2_ticks)))
            t1_ticks_idx = [t1_ticks.index(i) for i in t_ticks]
            t2_ticks_idx = [t2_ticks.index(i) for i in t_ticks]
            w1p = t1['arr_1'][:, t1_ticks_idx] # previous
            w2c = t2['arr_1'][:, t2_ticks_idx] # current
            as_vec, as_sign = assign(w2c, w1p)
            as_vec.astype(np.int64)
            w2a = t2['arr_1']
            w2r = t2['arr_2']
            w2b = np.empty((w2a.shape[0], w2a.shape[1]))*np.nan
            w2rn = np.empty(w2r.shape[0])
            for k in range(0, w2b.shape[0]):
                w2b[as_vec[k]] = w2a[k]*as_sign[k]
                w2rn[as_vec[k]] = w2r[k]*as_sign[k]
            __FACTOR_DIR_CLEAN__.save(f_files[i], t2['arr_0'], w2b, w2rn, as_vec)

def update_pca_clean():
    f_files = __FACTOR_DIR_RAW__.listdir()
    f_files_c = __FACTOR_DIR_CLEAN__.listdir()
    for i, j in enumerate(f_files):
        if j not in f_files_c:
            t1 = __FACTOR_DIR_CLEAN__.load(f_files[i-1])
            if t1 is None:
                sleep(0.2)
                t1 = __FACTOR_DIR_CLEAN__.load(f_files[i-1])
            t2 = __FACTOR_DIR_RAW__.load(f_files[i])
            t1_ticks = list(t1['arr_0'])
            t2_ticks = list(t2['arr_0'])
            t_ticks = list(set(t1_ticks).intersection(set(t2_ticks)))
            t1_ticks_idx = [t1_ticks.index(i) for i in t_ticks]
            t2_ticks_idx = [t2_ticks.index(i) for i in t_ticks]
            w1p = t1['arr_1'][:, t1_ticks_idx] # previous
            w2c = t2['arr_1'][:, t2_ticks_idx] # current
            as_vec, as_sign = assign(w2c, w1p)
            as_vec.astype(np.int64)
            w2a = t2['arr_1']
            w2r = t2['arr_2']
            w2b = np.empty((w2a.shape[0], w2a.shape[1]))*np.nan
            w2rn = np.empty(w2r.shape[0])
            for k in range(0, w2b.shape[0]):
                w2b[as_vec[k]] = w2a[k]*as_sign[k]
                w2rn[as_vec[k]] = w2r[k]*as_sign[k]
            __FACTOR_DIR_CLEAN__.save(f_files[i], t2['arr_0'], w2b, w2rn, as_vec)
    gc.collect()

def _conv_str_dates(f):
    f = [int(i) for i in f]
    f_yr = [i // 10000 for i in f]
    f_mth = [(i % 10000) // 100 for i in f]
    f_day = [(i % 100) for i in f]
    f_num = [dt.datetime(i, j, k) for i, j, k in zip(f_yr, f_mth, f_day)]
    return f_num

def collect_pca_factors():
    f_files = __FACTOR_DIR_CLEAN__.listdir()
    for i in f_files:
        if i == f_files[0]:
            t1 = np.array([__FACTOR_DIR_CLEAN__.load(i)['arr_2']])
        else:
            t1 = np.append(t1, [__FACTOR_DIR_CLEAN__.load(i)['arr_2']], axis=0)
    t2 = cuf.DataFrame(t1)
    x = ['PCA01', 'PCA02', 'PCA03', 'PCA04', 'PCA05', 'PCA06', 'PCA07', 'PCA08', 'PCA09', 'PCA10', 'PCA11',
         'PCA12', 'PCA13', 'PCA14', 'PCA15', 'PCA16', 'PCA17', 'PCA18', 'PCA19', 'PCA20']
    t2.set_columns(x)
    # add dates
    f_files = [int(i) for i in f_files]
    f_files_yr = [i // 10000 for i in f_files]
    f_files_mth = [(i % 10000) // 100 for i in f_files]
    f_files_day = [(i % 100) for i in f_files]
    f_files_num = [dt.datetime(i, j, k) for i, j, k in zip(f_files_yr, f_files_mth, f_files_day)]
    t2['Date'] = f_files_num
    t3 = t2[['Date']+x]
    __FACTOR_DIR_RETURN__.store('Returns', t3)
    # convert this into price series
    for i in x:
        t3[i] = np.cumprod(t3[i]+1)
    __FACTOR_DIR_RETURN__.store('Prices', t3)
    gc.collect()

def calculate_betas():
    pca_pr = __FACTOR_DIR_RETURN__.retrieve('Prices')
    tick_pr = __COMP_DIR_CLEAN__.retrieve('Close')
    pca_cols = [i for i in pca_pr.columns.tolist() if i != 'Date']
    tick_cols = [i for i in tick_pr.columns.tolist() if i != 'Date']
    for i in pca_cols:
        pca_pr_i = pca_pr[['Date', i]]
        tick_pr_i = cuf.DataFrame.merge(tick_pr, pca_pr_i, on='Date')
        tick_pr_i_beta = tick_pr.copy()
        for j in tick_cols:
            tick_pr_i_beta[j] = cf.beta_cc(tick_pr_i[j].values, tick_pr_i[i].values, 120)
        __FACTOR_DIR_BETA__.store(i+'_Beta', tick_pr_i_beta)
        del pca_pr_i, tick_pr_i, tick_pr_i_beta
    gc.collect()

def refresh_exposure_matrix():
    f_files = __FACTOR_DIR_CLEAN__.listdir()
    f_files_dt = _conv_str_dates(f_files)
    # read the various factor betas first into memory as retrieval takes a long time
    PCA_01 = __FACTOR_DIR_BETA__.retrieve('PCA01_Beta')
    PCA_02 = __FACTOR_DIR_BETA__.retrieve('PCA02_Beta')
    PCA_03 = __FACTOR_DIR_BETA__.retrieve('PCA03_Beta')
    PCA_04 = __FACTOR_DIR_BETA__.retrieve('PCA04_Beta')
    PCA_05 = __FACTOR_DIR_BETA__.retrieve('PCA05_Beta')
    PCA_06 = __FACTOR_DIR_BETA__.retrieve('PCA06_Beta')
    PCA_07 = __FACTOR_DIR_BETA__.retrieve('PCA07_Beta')
    PCA_08 = __FACTOR_DIR_BETA__.retrieve('PCA08_Beta')
    PCA_09 = __FACTOR_DIR_BETA__.retrieve('PCA09_Beta')
    PCA_10 = __FACTOR_DIR_BETA__.retrieve('PCA10_Beta')
    PCA_11 = __FACTOR_DIR_BETA__.retrieve('PCA11_Beta')
    PCA_12 = __FACTOR_DIR_BETA__.retrieve('PCA12_Beta')
    PCA_13 = __FACTOR_DIR_BETA__.retrieve('PCA13_Beta')
    PCA_14 = __FACTOR_DIR_BETA__.retrieve('PCA14_Beta')
    PCA_15 = __FACTOR_DIR_BETA__.retrieve('PCA15_Beta')
    PCA_16 = __FACTOR_DIR_BETA__.retrieve('PCA16_Beta')
    PCA_17 = __FACTOR_DIR_BETA__.retrieve('PCA17_Beta')
    PCA_18 = __FACTOR_DIR_BETA__.retrieve('PCA18_Beta')
    PCA_19 = __FACTOR_DIR_BETA__.retrieve('PCA19_Beta')
    PCA_20 = __FACTOR_DIR_BETA__.retrieve('PCA20_Beta')
    p_dt = [pd.tslib.Timestamp.to_datetime(i) for i in PCA_01['Date', list]]
    tick_cols = _get_tick_cols(PCA_01.columns)
    for i, i_dt in zip(f_files, f_files_dt):
        i_loc = [k for k, k2 in enumerate(p_dt) if k2 == i_dt]

        t1 = __FACTOR_DIR_CLEAN__.load(i)['arr_3']
        # retain the first 5
        t1_ = np.array(t1[0:5], dtype=np.int64)
        # first PCA
        Cons1 = t1_[0]+1
        if Cons1 <= 9:
            pca_test = locals()['PCA_0'+str(Cons1)][i_loc, :]
        else:
            pca_test = locals()['PCA_'+str(Cons1)][i_loc, :]
        if any(~np.isnan(pca_test[0,tick_cols].values.astype(np.float64))):
            beta = pca_test
            for j in range(0, 4):
                Cons1 = t1_[j+1]+1
                if Cons1 <= 9:
                    pca_test = locals()['PCA_0'+str(Cons1)][i_loc, :]
                else:
                    pca_test = locals()['PCA_'+str(Cons1)][i_loc, :]
                beta.row_bind(pca_test)
            del beta['Date']
        else:
            beta = None
        if beta is not None:
            __OPTIM_BETA__.store(i, beta)
    gc.collect()

def update_exposure_matrix():
    f_files1 = __FACTOR_DIR_CLEAN__.listdir()
    f_files2 = __OPTIM_BETA__.listdir()
    f_files = [i for i in f_files1 if i > max(f_files2)]
    f_files_dt = _conv_str_dates(f_files)
    # read the various factor betas first into memory as retrieval takes a long time
    PCA_01 = __FACTOR_DIR_BETA__.retrieve('PCA01_Beta')
    PCA_02 = __FACTOR_DIR_BETA__.retrieve('PCA02_Beta')
    PCA_03 = __FACTOR_DIR_BETA__.retrieve('PCA03_Beta')
    PCA_04 = __FACTOR_DIR_BETA__.retrieve('PCA04_Beta')
    PCA_05 = __FACTOR_DIR_BETA__.retrieve('PCA05_Beta')
    PCA_06 = __FACTOR_DIR_BETA__.retrieve('PCA06_Beta')
    PCA_07 = __FACTOR_DIR_BETA__.retrieve('PCA07_Beta')
    PCA_08 = __FACTOR_DIR_BETA__.retrieve('PCA08_Beta')
    PCA_09 = __FACTOR_DIR_BETA__.retrieve('PCA09_Beta')
    PCA_10 = __FACTOR_DIR_BETA__.retrieve('PCA10_Beta')
    PCA_11 = __FACTOR_DIR_BETA__.retrieve('PCA11_Beta')
    PCA_12 = __FACTOR_DIR_BETA__.retrieve('PCA12_Beta')
    PCA_13 = __FACTOR_DIR_BETA__.retrieve('PCA13_Beta')
    PCA_14 = __FACTOR_DIR_BETA__.retrieve('PCA14_Beta')
    PCA_15 = __FACTOR_DIR_BETA__.retrieve('PCA15_Beta')
    PCA_16 = __FACTOR_DIR_BETA__.retrieve('PCA16_Beta')
    PCA_17 = __FACTOR_DIR_BETA__.retrieve('PCA17_Beta')
    PCA_18 = __FACTOR_DIR_BETA__.retrieve('PCA18_Beta')
    PCA_19 = __FACTOR_DIR_BETA__.retrieve('PCA19_Beta')
    PCA_20 = __FACTOR_DIR_BETA__.retrieve('PCA20_Beta')
    p_dt = [pd.tslib.Timestamp.to_datetime(i) for i in PCA_01['Date', list]]
    tick_cols = _get_tick_cols(PCA_01.columns)
    for i, i_dt in zip(f_files, f_files_dt):
        # i = f_files[0]
        # i_dt = f_files_dt[0]
        i_loc = [k for k, k2 in enumerate(p_dt) if k2 == i_dt]

        t1 = __FACTOR_DIR_CLEAN__.load(i)['arr_3']
        # retain the first 5
        t1_ = np.array(t1[0:5], dtype=np.int64)
        # first PCA
        Cons1 = t1_[0]+1
        if Cons1 <= 9:
            pca_test = locals()['PCA_0'+str(Cons1)][i_loc, :]
        else:
            pca_test = locals()['PCA_'+str(Cons1)][i_loc, :]
        if any(~np.isnan(pca_test[0,tick_cols].values.astype(np.float64))):
            beta = pca_test
            for j in range(0, 4):
                Cons1 = t1_[j+1]+1
                if Cons1 <= 9:
                    pca_test = locals()['PCA_0'+str(Cons1)][i_loc, :]
                else:
                    pca_test = locals()['PCA_'+str(Cons1)][i_loc, :]
                beta.row_bind(pca_test)
            del beta['Date']
        else:
            beta = None
        if beta is not None:
            __OPTIM_BETA__.store(i, beta)
    gc.collect()

def refresh_statistics():
    comb_prices = __COMP_DIR_CLEAN__.retrieve('Close')
    tick_cols = _get_tick_cols(comb_prices.columns)
    comb_vol = comb_prices.copy()
    for i in tick_cols:
        comb_vol[i] = cf.qrma(cf.vol_cc(comb_vol[i].values, 120), 15, lg=True)
    __COMP_DIR_CLEAN__.store('Volatility_120D', comb_vol)
    for j in range(1, 5):
        comb_ret = comb_prices.copy()
        for i in tick_cols:
            comb_ret[i] = cf.qrbeta(comb_ret[i].values, j*30)
        __COMP_DIR_CLEAN__.store('QrBeta_'+str(j*30)+'D', comb_ret)
        del comb_ret

def update_all():
    update_pca()
    update_pca_clean()
    collect_pca_factors()
    calculate_betas()
    update_exposure_matrix()
    refresh_statistics()

if __name__ == '__main__':
    update_all()


import random
import json
import Utils

class Game(object):
    """description of class"""
    def __init__(self, player1, player2, state_machine):
        self.__players[0] = player1
        self.__players[1] = player2
        self.__state_machine = state_machine

    def go(self):
        self.__game_is_going = True
        random.seed()
        current_player = random.randint(0, 1)

        while(not self.__word_accepted):
            self.__last_word = self.__get_first_word()
            answer = self.__notify(self.__players[current_player], "first_turn", self.__last_word)
            self.__translate_answer(answer)

        while(self.__game_is_going):
            if self.__word_accepted:
                while not self.__validate_word(self.__last_word):
                    self.__last_word = self.__previous_word
                    answer = self.__notify(self.__players[current_player], "repeat_turn", self.__last_word)
                    self.__translate_answer(answer)

                self.__add_to_used_words(self.__last_word)
                self.__save_state()

                current_player = 1 - current_player

                answer = self.__notify(self.__players[current_player], "your_turn", self.__last_word)
            else:
                self.__last_word = self.__previous_word
                answer = self.__notify(self.__players[1 - current_player], "repeat_turn", self.__last_word)
            self.__translate_answer(answer)

    def __get_first_word(self):
        start_letter = 'abcdefghijklmnopqrstuvwxyz'[random.randint(0, 25)]
        return start_letter

    def __translate_answer(self, answer):
        if answer[0] == '-':
            self.__word_accepted = False
        if answer[0] == '!':
            self.__word_accepted = False
            self.__game_is_going = False
            return
        self.__word_accepted = True
        self.__previous_word = self.__last_word
        self.__last_word = answer

    def __add_to_used_words(self, word):
        self.__notify(self.__state_machine, 'used', word)

    def __validate_word(self, word):
        if word[0] != self.__previous_word[-1]:
            return False
        answer = self.__notify(self.__state_machine, 'validate', word)
        result = json.loads(answer)
        return result['answer'] == "validated"

    def __notify(self, address, notification, data):
        return address.notify(Utils.format_command(notification, data))

    def __save_state(self):
        pass

    __players = [None, None]
    __state_machine = None
    __word_accepted = False
    __game_is_going = False
    __last_word = ''
    __previous_word = ''


import sys
import os
import os.path
from flask import Flask
from flask_s3_viewer import FlaskS3Viewer
from flask_s3_viewer.aws.ref import Region

import subprocess
from os import remove
import sys

if os.getenv("NB_PREFIX"):
    PREFIX = os.getenv("NB_PREFIX")
else:
    PREFIX = ""

app = Flask(__name__)

#app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

if PREFIX:
    from flask import url_for, send_from_directory
    from flask_reverse_proxy_fix.middleware import ReverseProxyPrefixFix
    app.config['REVERSE_PROXY_PATH'] = PREFIX + '/proxy/8765'
    ReverseProxyPrefixFix(app)

def _get_minio_client(tenant):
    """ Get the variables out of vault to create Minio Client. """

    d = {
        'MINIO_URL'        : None,
        'MINIO_ACCESS_KEY' : None,
        'MINIO_SECRET_KEY' : None
    }

    if tenant not in ("minimal", "premium", "pachyderm"):
        print("Not a valid resource! Options are")
        print("minimal, premium, pachyderm.")
        print("We will try anyway...")

    vault = f"/vault/secrets/minio-{tenant}-tenant1"

    for var in d:
        CMD = f'echo $(source {vault}; echo ${var})'
        p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True,
                             executable='/bin/bash')
        d[var] = p.stdout.readlines()[0].strip().decode("utf-8")

    return d

def get_minimal_client():
    """Get a connection to the minimal Minio tenant"""
    return __get_minio_client__("minimal")

def get_pachyderm_client():
    """Get a connection to the pachyderm Minio tenant"""
    return __get_minio_client__("pachyderm")

def get_premium_client():
    """Get a connection to the premium Minio tenant"""
    return __get_minio_client__("premium")

if len(sys.argv) > 1:
    tenant = sys.argv[0]
else:
    tenant = "minimal"

d = _get_minio_client(tenant)

NB_PREFIX = os.getenv("NB_PREFIX") + '/proxy/8765'
BUCKET = os.getenv("NB_PREFIX").split('/')[2]

app.config['DOWNLOAD_DIR'] = os.path.join(os.getenv("HOME"), "downloads")

creds_dir = os.path.join(os.getenv("HOME"), ".aws")
os.makedirs(creds_dir, exist_ok=True)

# These don't have to be correct!! Anything works.
with open(creds_dir + "/credentials", "w") as f:
    f.write("""
[minio]
aws_access_key_id=minioadmin
aws_secret_access_key=minioadmin
region = us-east-1
""")

# # For test, disable template caching
# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1
# app.config['TEMPLATES_AUTO_RELOAD'] = True

# FlaskS3Viewer Init
s3viewer = FlaskS3Viewer(
    app, # Flask app
    namespace=BUCKET, # namespace be unique
    template_namespace='mdl', # set template
    object_hostname='http://flask-s3-viewer.com', # file's hostname
    allowed_extensions={}, # allowed extension
    config={ # Bucket configs and else
        'profile_name': 'minio',
        'access_key': d['MINIO_ACCESS_KEY'],
        'secret_key': d['MINIO_SECRET_KEY'],
        'region_name': Region.SEOUL.value,
        'endpoint_url': d['MINIO_URL'],
        'bucket_name': BUCKET,
        'use_ssl': False,
        'cache_dir': '/tmp/flask_s3_viewer',
        'use_cache': False,
        'ttl': 86400
    }
)

# Init another one
s3viewer.add_new_one(
    namespace='shared',
    template_namespace='mdl', # set template
    object_hostname='http://example.com',
    config={
        'profile_name': 'minio',
        'access_key': d['MINIO_ACCESS_KEY'],
        'secret_key': d['MINIO_SECRET_KEY'],
        'endpoint_url': d["MINIO_URL"],
        'bucket_name': 'shared',
        'use_ssl': False,
        'region_name': Region.SEOUL.value
    }
)

# You can see registerd configs
# print(s3viewer.FLASK_S3_VIEWER_BUCKET_CONFIGS)

# You can use boto3's session and client if you want
# print(FlaskS3Viewer.get_boto_client(FS3V_NAMESPACE))
# print(FlaskS3Viewer.get_boto_session(FS3V_NAMESPACE))

# Apply FlaskS3Viewer blueprint
s3viewer.register()

@app.route('/')
def index ():
    return f"""
This is the root of the storage explorer! We will add a nice splash page later.

You probably want to go to one of these:

{BUCKET}

Shared

"""

print(f"""

Thanks for using the DAaaS Jupyter Storage System! Go to

https://kubeflow.covid.cloud.statcan.ca{NB_PREFIX}

""")

# Usage: python example.py test (run debug mode)
if __name__ == '__main__':
    debug = False
    if len(sys.argv) > 1:
        if sys.argv[1] == 'test':
            debug = True
    app.run(debug=debug, port=8765)


from app.model import PodcastEpisodeModel
from app.app import db
import uuid
import datetime

class Episode():
	def getEpisode(self, episode_id) -> PodcastEpisodeModel:
		return PodcastEpisodeModel.query.filter_by(episode_id=episode_id).first()

	def setEpisode(self, username, args) -> PodcastEpisodeModel:
		episode = PodcastEpisodeModel(
			episode_id=str(uuid.uuid4()),
			episode_number=int(args['episode_number']),
			title=str(args['title']),
			description=str(args['description']),
			uri=str(args['uri']),
			published_date=datetime.datetime.now(),
			show_id=str(args['show_id'])
		)
		db.session.add(episode)
		db.session.commit()
		return episode


#1. 정렬 안 된 경우
def sequentialsearch1(a, n, key):
    i = 0
    while i<n and a[i] != key:
        i += 1
    if i<n : return i
    else: return -1

data = [4, 9, 11, 23, 2, 19, 7]
key = 19
print(sequentialsearch1(data, len(data), key))
print(data[sequentialsearch1(data, len(data), key)])

#2. 정렬 된 경우

def sequentialsearch2(a, n, key):
    i = 0
    i += 1
    while i<n and a[i]<key:
        i += 1
    if i<n and a[i] == key : return i
    else : return -1


# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

KOZAK_CONSENSUS_SEQUENCE_BEFORE_START = "gccRcc"
ALT_START_CODONS = {"CTG", "GTG", "TTG"}
KOZAK_CONSENSEUS_SEQUENCE_AFTER_START = "Gcg"

def translation_initiation_score(sequence_before_start, start_codon, sequence_after_start):
    if start_codon == "ATG":
        score = 100
    elif start_codon in ALT_START_CODONS:
        score = 10
    else:
        score = 0

    for pair in [
            zip(KOZAK_CONSENSUS_SEQUENCE_BEFORE_START[-len(sequence_before_start):], sequence_before_start),
            zip(KOZAK_CONSENSEUS_SEQUENCE_AFTER_START, sequence_after_start)]:
        for (consensus, observed) in pair:
            consensus_upper = consensus.upper()
            if consensus_upper == "R":
                valid = {"A", "G"}
            else:
                valid = {consensus_upper}
            match = observed in valid
            if consensus == consensus_upper:
                score += 10 * match
            else:
                score += match
    max_score = 100 + 5 + 10 + 10 + 2
    return score / max_score


# -----------------------------------------------------------------------------
# From Numpy to Python
# Copyright (2017) Nicolas P. Rougier - BSD license
# More information at https://github.com/rougier/numpy-book
# -----------------------------------------------------------------------------

import numpy as np
import numba as nb

@nb.jit(nopython=True, parallel=True, fastmath=True)
def linspace(start, stop, num, dtype):
    X = np.empty((num, ), dtype=dtype)
    dist = (stop - start) / (num - 1)
    for i in range(num):
        X[i] = start + i * dist
    return X

@nb.jit(nopython=True, parallel=True, fastmath=True)
def mandelbrot(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):
    # Adapted from https://www.ibm.com/developerworks/community/blogs/jfp/...
    #              .../entry/How_To_Compute_Mandelbrodt_Set_Quickly?lang=en
    # X = np.linspace(xmin, xmax, xn, dtype=np.float64)
    # Y = np.linspace(ymin, ymax, yn, dtype=np.float64)
    X = linspace(xmin, xmax, xn, dtype=np.float64)
    Y = linspace(ymin, ymax, yn, dtype=np.float64)
    # C = X + Y[:,None]*1j
    C = X + np.reshape(Y, (yn, 1)) * 1j
    N = np.zeros(C.shape, dtype=np.int64)
    Z = np.zeros(C.shape, dtype=np.complex128)
    for n in range(maxiter):
        # I = np.less(abs(Z), horizon)
        I = np.less(np.absolute(Z), horizon)
        # N[I] = n
        for j in range(C.shape[0]):
            for k in range(C.shape[1]):
                if I[j, k]:
                    N[j, k] = n
        # Z[I] = Z[I]**2 + C[I]
        for j in range(C.shape[0]):
            for k in range(C.shape[1]):
                if I[j, k]:
                    Z[j, k] = Z[j, k]**2 + C[j, k]
    # N[N == maxiter-1] = 0
    for j in range(C.shape[0]):
        for k in range(C.shape[1]):
            if N[j, k] == maxiter - 1:
                N[j, k] = 0
    return Z, N


#https://www.codewars.com/kata/categorize-new-member/train/python

def openOrSenior(data):
    output = []
    for x in data:
        if x[0]>=55 and x[1]>=8:
            output.append('Senior')
        else:
            output.append('Open')
    return output


#!C:/python/python.exe
from test import *
from level0 import *

class game:
    def __init__(self):#构造函数，初始化各种东西
        pygame.init()
        pygame.mixer.init()
        self.src = pygame.display.set_mode((WIDTH, HEIGHT))
        self.running=True
        self.upLock=1
        self.moves=[]

        self.allSprite=pygame.sprite.Group()
        #加载关卡
        self.l0=level0()
        for e in self.l0.all:
            self.allSprite.add(e)
    def run(self):#主循环
        while self.running:
            self.update()#很重要！！下面两个方法不重要
            self.draw()
            self.events()
    def update(self):
        #获取键盘
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.moveCommand(-100,0)
            self.upLock=1
        elif keys[pygame.K_RIGHT]:
            self.moveCommand(100,0)
            self.upLock = 1
        elif keys[pygame.K_DOWN]:
            self.moveCommand(0,100)
            self.upLock = 1
        elif keys[pygame.K_UP]:
            self.moveCommand(0,-100)
            self.upLock = 1
        elif keys[pygame.K_z]:
            self.withdraw()
            self.upLock=1
        #每下行动后更新规则，50行的if
        if self.upLock==1:
            self.l0.allRule.clear()
            for e in self.l0.be:
                #寻找名词是动词规则
                for ee in self.l0.verb:
                    #“是”的右边有没有动词
                    if ee.rect.x==e.rect.x+100 and ee.rect.y==e.rect.y:
                        for eee in self.l0.noun:
                            #如果有，那左边有没有名词
                            if eee.rect.x==e.rect.x-100 and eee.rect.y==e.rect.y:
                                if self.l0.allRule.__contains__(ee.bit|eee.bit):
                                    pass
                                else:
                                    self.l0.allRule.append(ee.bit|eee.bit)

                    #“是”的下面有没有动词，同上
                    if ee.rect.y==e.rect.y+100 and ee.rect.x==e.rect.x:
                        for eee in self.l0.noun:
                            # 如果有，那上边有没有名词
                            if eee.rect.y==e.rect.y-100 and eee.rect.x==e.rect.x:
                                if self.l0.allRule.__contains__(ee.bit | eee.bit):
                                    pass
                                else:
                                    self.l0.allRule.append(ee.bit | eee.bit)

                #寻找名词是名词规则
                for ee in self.l0.noun:
                    # “是”的右边有没有名词
                    if ee.rect.x == e.rect.x + 100 and ee.rect.y == e.rect.y:
                        for eee in self.l0.noun:
                            # 如果有，那左边有没有名词
                            if eee.rect.x == e.rect.x - 100 and eee.rect.y == e.rect.y:
                                if self.l0.allRule.__contains__(eee.bit | ee.bit):
                                    pass
                                else:
                                    self.l0.allRule.append(ee.bit | eee.bit)
                                    self.changeObject(eee.bit, ee.bit)
                    #“是”的下面有没有名词，同上
                    if ee.rect.y==e.rect.y+100 and ee.rect.x==e.rect.x:
                        for eee in self.l0.noun:
                            # 如果有，那上边有没有名词
                            if eee.rect.y==e.rect.y-100 and eee.rect.x==e.rect.x:
                                if self.l0.allRule.__contains__(ee.bit | eee.bit):
                                    pass
                                else:
                                    self.l0.allRule.append(ee.bit | eee.bit)
                                    self.changeObject(eee.bit,ee.bit)
            #判断我是不是在赢的物体上，是就结束游戏
            for e in self.l0.allRule:
                if e&1<<2:
                    for ee in self.l0.object:
                        if e&ee.bit:
                            for eee in self.l0.allRule:
                                if eee & 2:
                                    for me in self.l0.object:
                                        if eee & me.bit:
                                            if me.rect.x==ee.rect.x and me.rect.y==ee.rect.y:
                                                self.running=False
            for e in self.l0.allRule:
                if e&1<<5:
                    for ee in self.l0.object:
                        if e&ee.bit:
                            for eee in self.l0.allRule:
                                if eee & 2:
                                    for me in self.l0.object:
                                        if eee & me.bit:
                                            if me.rect.x==ee.rect.x and me.rect.y==ee.rect.y:
                                                me.__del__()

            self.upLock=0
            time.sleep(0.2)
    def draw(self):
        #背景白色
        self.src.fill((255, 255, 255))
        #显示相关
        self.allSprite.draw(self.src)
        pygame.display.flip()
        pass
    def events(self):
        #检测关闭
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.running = False
    #获取键盘按键后，找谁是我
    def moveCommand(self,x,y):
        for e in self.l0.allRule:
            if e&2:
                for ee in self.l0.object:
                    if e&ee.bit:
                        self.move(ee,x,y)
    #movecommand找到谁是我后调用move（）
    def move(self,unit,x,y):
        cm=1
        self.moves.append(unit)#moves列表用于存储一条直线上要移动的物体
        for e in self.l0.all:
            if e.rect.x == unit.rect.x + x and e.rect.y == unit.rect.y + y:#移动方向上的东西
                if not self.l0.object.__contains__(e):#如果e不是物体（就是汉字，可以移动）
                    self.move(e,x,y)#递归，看前面物体的前面有没有物体
                elif self.l0.allRule.__contains__(e.bit|1<<4):#如果当前规则里e是推（1<<4代表推）
                    self.move(e,x,y)
                elif self.l0.allRule.__contains__(e.bit|1<<3):#如果当前规则里e是定（不能推）
                    cm=0
                    break
        for e in  self.l0.wall:
            if e.rect.x == unit.rect.x + x and e.rect.y == unit.rect.y + y:
                cm=0
        #前面没东西了，移动
        if cm:
            #保存移动记录，用于撤回键功能
            lll=[]
            for e in self.moves:
                ll=[e,e.rect.x,e.rect.y]
                lll.append(ll)
            if lll:
                self.l0.moveHistory.append(lll.copy())

            #遍历moves，移动里面每个单位
            for e2 in self.moves:
                e2.rect.x+=x
                e2.rect.y+=y
        self.moves.clear()
    #出现名词是名词的规则后调用
    def changeObject(self,changeFrom,to):
        xx=0
        for e in self.l0.object:
            if e.bit==to:
                xx=e.image
                break
        for e in self.l0.object:
            if e.bit==changeFrom:
                e.image=xx
                e.bit=to
    def withdraw(self):
        if self.l0.moveHistory:
            temp=self.l0.moveHistory.pop()
            for e in temp:
                unit=e[0]
                unit.rect.x=e[1]
                unit.rect.y=e[2]

#程序从这里开始，run是死循环
g = game()
g.run()
pygame.quit()


# PyWAD is open-source software and consists of a set of plugins written in python for the WAD-Software medical physics quality control software.
# The WAD Software can be found on https://github.com/wadqc
#
# The pywad package includes plugins for the automated analysis of QC images for various imaging modalities.
# PyWAD has been originaly initiated by Dennis Dickerscheid (AZN), Arnold Schilham (UMCU), Rob van Rooij (UMCU) and Tim de Wit (AMC)
#
#
# Changelog:
#
#
# Description of this plugin:
# This is a simple plugin to print the location of the dcm4chee dataset

__version__='01062015'
__author__='DD'

import dicom

def fileLocation(data,results,**kwargs):
    '''
    Function receives a data class object and returns for each series a link to the first instance
    '''

    relevantFileList = data.series_filelist

    for series in relevantFileList:
        tmpSeriesDescription = dicom.read_file(series[0]).SeriesDescription
        results.addChar('Series',series[0],level='2',quantity=tmpSeriesDescription)


from PIL import Image
import numpy as np
from IPython.display import display
import pickle
from sklearn.neighbors import KNeighborsClassifier

basedir_data = "./data/"
rel_path = basedir_data + "cifar-10-batches-py/"

def unpickle(file):
    with open(file, "rb") as fo:
        dict = pickle.load(fo, encoding="bytes")
    return dict

def default_label_fn(i, original):
    return original

def show_img(img_arr, label_arr, meta, index, label_fn=default_label_fn):
    """
    Given a numpy array of image from CIFAR-10 labels this method transform the data so that PIL can read and show the image.
    Check here how CIFAR encodes the image http://www.cs.toronto.edu/~kriz/cifar.html
    """
    one_img = img_arr[index, :]
    # Assume image size is 32 x 32. First 1024 px is r, next 1024 px is g, last 1024 px is b from the (r,g b) channel
    r = one_img[:1024].reshape(32, 32)
    g = one_img[1024:2048].reshape(32, 32)
    b = one_img[2048:].reshape(32, 32)
    rgb = np.dstack([r, g, b])
    img = Image.fromarray(np.array(rgb), "RGB")
    display(img)
    print(label_fn(index, meta[label_arr[index][0]].decode("utf-8")))

X = unpickle(rel_path + "data_batch_1")
img_data = X[b"data"]
img_label_orig = img_label = X[b"labels"]
img_label = np.array(img_label).reshape(-1, 1)

print(img_data)
print("shape", img_data.shape)

test_X = unpickle(rel_path + "test_batch")
test_data = test_X[b"data"]
test_label = test_X[b"labels"]
test_label = np.array(test_label).reshape(-1, 1)

sample_img_data = img_data[0:10, :]
print(sample_img_data)
print("shape", sample_img_data.shape)

batch = unpickle(rel_path + "batches.meta")
meta = batch[b"label_names"]
print(meta)

for i in range(0, 10):
    show_img(sample_img_data, img_label, meta, i)

def pred_label_fn(i, original):
    return original + "::" + meta[YPred[i]].decode("utf-8")

data_point_no = 10
sample_test_data = test_data[:data_point_no, :]

nbrs = KNeighborsClassifier(n_neighbors=3, algorithm="brute").fit(
    img_data, img_label_orig
)
YPred = nbrs.predict(sample_test_data)

for i in range(0, len(YPred)):
    show_img(sample_test_data, test_label, meta, i, label_fn=pred_label_fn)


import numpy as np
from shapely.geometry import Point, Polygon, LineString
import time

from .base import Trimesh

from copy import deepcopy

from scipy.spatial import cKDTree
from collections import deque
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())

try:
    import occmodel as occ
    from geotools import Transform
except:
    log.error('Failed to import occmodel!')

def solid_to_mesh(solid):
    occ_mesh = solid.createMesh()
    occ_mesh.optimize()
    faces    = np.array(list(occ_mesh.triangles)).reshape((-1,3)).astype(int)
    vertices = np.array(list(occ_mesh.vertices)).reshape((-1,3)).astype(float)
    mesh     = Trimesh(vertices=vertices, faces=faces, process=True)
    if not mesh.is_watertight():
        #raise NameError('Mesh returned from openCASCADE isn\'t watertight!')
        log.error('Mesh returned from openCASCADE isn\'t watertight!')
    return mesh

def three_dimensionalize(points):
    shape = np.shape(points)
    if len(shape) != 2: raise NameError('Points must be 2D array!')
    if shape[1] == 2:
        points = np.column_stack((points, np.zeros(len(points))))
    elif shape[1] != 3: raise NameError('Points must be (n,2) or (n,3)!')
    return points

def extrude_shapely(polygon,
                    height,
                    height_pad   = [0,0],
                    contour_pad  = 0,
                    return_solid = False):

    shell = np.array(polygon.exterior.coords)
    holes = [np.array(i.coords) for i in polygon.interiors]
    mesh  = extrude_points(shell        = shell,
                           holes        = holes,
                           height       = height,
                           height_pad   = height_pad,
                           return_solid = return_solid)
    return mesh

def extrude_points(shell,
                   vectors    = None,
                   height     = None,
                   holes      = None,
                   return_solid = False,
                   height_pad = [0,0]):

    if vectors is None:
        pad_signed = np.sign(height) * np.array(height_pad)
        vectors    = np.array([[0.0, 0.0, -pad_signed[0]],
                               [0.0, 0.0,  pad_signed[1] + height]])
    else:
        vectors = np.array(vectors)

    shell_3D      = three_dimensionalize(shell)
    shell_3D[:,2] = vectors[0][2]

    shell_face  = occ.Face().createPolygonal(shell_3D)
    shell_solid = occ.Solid().extrude(shell_face, *vectors)

    if holes is None:
        if return_solid: return shell_solid
        else:            return solid_to_mesh(shell_solid)

    pad_vector  = np.diff(vectors, axis=0)[0]
    cut_vectors = vectors + [-pad_vector, pad_vector]

    for hole in holes:
        cut_face  = occ.Face().createPolygonal(three_dimensionalize(hole))
        cut_solid = occ.Solid().extrude(cut_face, *cut_vectors)
        shell_solid.cut(cut_solid)

    if return_solid: return shell_solid
    else:            return solid_to_mesh(shell_solid)

def remove_close(points, radius):
    '''
    Given an (n, m) set of points where n=(2|3) return a list points
    where no point is closer than radius
    '''
    tree     = cKDTree(points)
    consumed = np.zeros(len(points), dtype=np.bool)
    unique   = np.zeros(len(points), dtype=np.bool)
    for i in xrange(len(points)):
        if consumed[i]: continue
        neighbors = tree.query_ball_point(points[i], r=radius)
        consumed[neighbors] = True
        unique[i]           = True
    return points[unique]

def remove_close_set(points_fixed, points_reduce, radius):
    '''
    Given two sets of points and a radius, return a set of points
    that is the subset of points_reduce where no point is within
    radius of any point in points_fixed
    '''
    tree_fixed  = cKDTree(points_fixed)
    tree_reduce = cKDTree(points_reduce)
    reduce_duplicates = tree_fixed.query_ball_tree(tree_reduce, r = radius)
    reduce_duplicates = np.unique(np.hstack(reduce_duplicates).astype(int))
    reduce_mask = np.ones(len(points_reduce), dtype=np.bool)
    reduce_mask[reduce_duplicates] = False
    points_clean = points_reduce[reduce_mask]
    return points_clean

def sample_polygon(polygon, count=None):
    '''
    Randomly sample inside a polygon using rejection sampling,
    returning count number of points
    '''
    if count is None:
        count = len(polygon.exterior.coords) * 2
    bounds   = np.reshape(polygon.bounds, (2,2))
    box_size = np.ptp(bounds, axis=0)
    samples  = deque()
    while len(samples) < count:
        test_point = (np.random.random(2) * box_size) + bounds[0]
        if polygon.intersects(Point(test_point)):
            samples.append(test_point)
    return np.array(samples)

def nodes_random(polygon, radius=None, return_index=False):
    points_fixed = np.array(polygon.exterior.coords)
    polygon_idx  = [[0,len(points_fixed)]]
    if len(polygon.interiors) != 0:
        points_interior = np.vstack([i.coords for i in polygon.interiors])
        points_fixed    = np.vstack((points_fixed, points_interior))

    if radius is None:
        radius = np.ptp(np.reshape(polygon.bounds, (2,2)),axis=0).min() / 15.0

    # sample random points from inside the polygon with re
    points_random = sample_polygon(polygon)
    points_random = remove_close(points_random, radius)
    points_random = remove_close_set(points_fixed, points_random, radius)

    nodes = np.vstack((points_fixed, points_random))
    return nodes

def delaunay_polygon(polygon, points=None):
    '''
    Calculates Delaunay trianglation of a polygon,
    discarding triangles which are not inside the polygon

    If points is not specified, it is calculated as the exterior of
    the polygon and a number of points randomly sampled from
    inside the polygon
    '''
    if points is None:
        points = nodes_random(polygon)
    faces       = Delaunay(points).simplices.copy()
    test_points = points[faces].mean(axis=1)
    intersects  = np.array([polygon.intersects(Point(i)) for i in test_points])
    faces       = faces[intersects]
    return points, faces

if __name__ == '__main__':
    formatter = logging.Formatter("[%(asctime)s] %(levelname)-7s (%(filename)s:%(lineno)3s) %(message)s", "%Y-%m-%d %H:%M:%S")
    handler_stream = logging.StreamHandler()
    handler_stream.setFormatter(formatter)
    level = logging.DEBUG
    handler_stream.setLevel(level)
    for logger in [log, logging.getLogger('trimesh')]:
        logger.addHandler(handler_stream)
        logger.setLevel(level)

    from scipy.spatial import Delaunay
    from shapely.geometry import MultiPoint
    polygon = Point([0,0]).buffer(1).union(Point([.95,0]).buffer(1)).difference(Point([0,0]).buffer(.5))

    points, faces = delaunay_polygon(polygon)

    import matplotlib.pyplot as plt
    plt.plot(*polygon.exterior.xy)
    plt.triplot(*points.T, triangles=faces)
    plt.show()

    a = Point([0,0]).buffer(1)
    m = extrude_shapely(a, height=1, height_pad=[-.1,.1])


#!/usr/bin/python
# coding=utf-8
import platform

from selenium import webdriver

from com_zxl_common import LogUtils

class BaseRequest:

    @staticmethod
    def get_web_content(url):
        LogUtils.i("get_web_content::" + url)
        #chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
        chrome_driver = "/Users/zxl/Downloads/chromedriver"
        sys_str = platform.system()
        LogUtils.i("get_web_content::" + sys_str)
        if sys_str == 'Darwin':
            chrome_driver = "/Users/zxl/Downloads/chromedriver"
        elif sys_str == 'Windows':
            chrome_driver = "D:\\my_github_workspace\\chromedriver.exe"
        elif sys_str == 'Linux':
            # chrome_driver = "/Users/zxl/Downloads/chromedriver"
            chrome_driver = "/home/mi/下载/chromedriver"

        # 创建chrome参数对象
        opt = webdriver.ChromeOptions()

        opt.add_experimental_option('excludeSwitches', ['enable-automation'])

        # 把chrome设置成无界面模式，不论windows还是linux都可以，自动适配对应参数
        # opt.set_headless()
        opt.add_argument('--headless')
        # opt.add_argument('--proxy-server=http://127.0.0.1:8080')

        pre_fs = {"profile.managed_default_content_settings.images": 2}
        opt.add_experimental_option("prefs", pre_fs)

        # 创建chrome无界面对象
        driver = webdriver.Chrome(executable_path=chrome_driver, options=opt)

        driver.maximize_window()

        driver.get(url)

        return driver


from model import *
import numpy as np
import csv
import pandas as pd
import torch
import torchvision as ptv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from model import Net

def load_data(dataname, labelname):
	data = pd.read_csv(dataname)
	label = pd.read_csv(dataname)
	print (data.shape)
	data = data.fillna(0)
	label = data.fillna(0)
	data = data.values
	label = label.values
	data1 = data[0:1000]
	label1 = label[0:1000]
	label1 = label1[:, 0]
	data1 = np.array(data1, dtype = np.float64)
	label1 = np.array(label1, dtype = np.float64)
	tensor = torch.from_numpy(data1)
	label_tensor = torch.from_numpy(label1)
	tensor = tensor.type(torch.FloatTensor)
	label_tensor = label_tensor.type(torch.FloatTensor)
	label_tensor = label_tensor.view(-1, 1)
	return tensor, label_tensor
if __name__ == '__main__':
	dataname = 'data.csv'
	labelname = 'label.csv'
	data, label = load_data(dataname, labelname)
	net = Net(input_dim = 54, hidden1 = 108, hidden2 = 108, hidden_pred = 27, output_dim = 1)
	encoder = Encoder(factor_dim = 27,  hidden_dim = 10, time_step = 5, num_layer = 3)
	# optimizer = torch.optim.SGD([{'params':net.parameters()},{'params':encoder.parameters()}], lr=0.2, momentum = 0.9)
	optimizer = torch.optim.SGD(net.parameters(), lr = 0.2, momentum = 0.9)
	loss_func = torch.nn.MSELoss()
	encoder = Encoder(factor_dim = 27,  hidden_dim = 10, time_step = 5, num_layer = 3)
	# attention = Attention(hidden_dim = 10, encoder_dim = 10)
	epoch = 500

	for i in range(epoch):
		print (i)
		prediction = net(data)
		# deep_factor = net(data)
		# pack_factor = deep_factor.view(4, 250, 27)
		# label = label.view(4, 250, 1)
		# prediction = encoder(pack_factor)
		# num_layer, batch, hidden_dim = prediction.shape
		# for i in range(2):
		# 	print (i)
		# 	weight = attention(out[:, i, :], hn[:, i, :], cn[:, i, :])
		# pack_factor = deep_factor.permute(1, 0, 2)

		loss = loss_func(prediction, label)
		optimizer.zero_grad()
		loss.backward()
		optimizer.step()
		print (loss)

		# if i % 5 == 0:
		# 	plot(data, label, prediction)


# -*-coding:utf-8 -*-
'''
    类 = 特征 + 行为
'''

class Student(object):
    #name = ''
    #age = 0
    __count = 0

    #对象初始化函数
    def __init__(self, name, age):
        self.name = name
        self.age = age
        #Student.count += 1
        #实例方法访问类变量
        #self.__class__.count += 1
        print('age: ', age, ' name: ', name)

    def print_profile(self):
        print('name: ', self.name)
        print('age: ', str(self.age))

    #类方法-类方法访问类变量
    @classmethod
    def countSum(cls):
        cls.__count += 1
        print(cls.__count)

    @staticmethod
    def add(x, y):
        return x+y

    @classmethod
    def getCount(cls):
        return cls.__count

stu = Student('Jack', 20)
stu.countSum()
#print(stu.count)
stu1 = Student('JackJones', 22)
stu1.countSum()

print('Student.count: ', Student.getCount())
print(Student.add(1, 2))