# -*- coding: utf-8 -*-
"""
Created on Wed May 13 13:46:38 2015

@author: andric
"""

import os
import numpy as np
import pandas as pd
import networkx as nx

if __name__ == '__main__':

    dat_dir = os.environ['pnd']
    modularity_dir = dat_dir+'/modularity'
    graph_dir = dat_dir+'/graphs'
    dat_file = 'pandit_data.csv'
    df = pd.read_csv(os.path.join(dat_dir, dat_file))
    ctnames = pd.Series(df.columns[1:])
    subj_list = ['pandit', 'ctrl']
    cols = ['nodes', 'coms']
    dfcols = ['region', 'community']

    for subjid in subj_list:
        for thresh_dens in np.arange(.1, .51, .1):
            q_name = '%s.dens_%s.Qval' % (subjid, thresh_dens)
            qscores = np.loadtxt(os.path.join(modularity_dir, q_name))
            maxiter = qscores.argmax()
            t_name = '%s.dens_%s.trees' % (subjid, thresh_dens)
            trees = np.loadtxt(os.path.join(modularity_dir, t_name))
            g_name = '%s.dens_%s.edgelist.gz' % (subjid, thresh_dens)
            g = nx.read_edgelist(os.path.join(graph_dir, g_name), nodetype=int)

            tmp_mx = np.column_stack((np.arange(148), trees[:, maxiter]))
            max_tree = pd.DataFrame(tmp_mx, columns=cols)
            max_tree = max_tree.iloc[g.nodes(), :]

            # max_tree = trees[:, maxiter]
            nnames = []
            modid = []
            for com in set(max_tree.loc[:, 'coms']):
                ns = max_tree.loc[:, 'nodes'][max_tree.loc[:, 'coms'] == com]
                list_nodes = map(int, ns)
                nnames.append(ctnames[list_nodes])
                modid.append(np.repeat(com, len(list_nodes)))
            nnames = list(np.concatenate(nnames))
            modid = list(np.concatenate(modid))
            newd = pd.DataFrame(zip(nnames, modid), columns=dfcols)
            mod_list_name = '%s.inclusionlist.dens_%s.csv' % \
                (subjid, thresh_dens)
            newd.to_csv(os.path.join(dat_dir, mod_list_name))


import time

# remember that name of your class and file has to include 'module__' string
class module__exampleF:    # change the name of that class, the name must match the name of the file
#---------------------------------------------------------------------------------------------------------
# main prepared functions - do not edit the code or parameters

    invoke_words = []    # list of key words of the module
    name_of_the_class = ""
    developer_test = 1
    log_ = []            # object for storing logs from modules

    # init function let module start properly

    def __init__(self):

        name_of_the_class = str(self.__class__.__name__ )
        #----------------------DATA-TO-EDIT-FOR-THE-PROGRAMER-----------------------
        self.version = "v 1.0.1"    # insert your own version number
        self.date = "04.2020"       # insert your own date of publish
        self.short_desc = ""        # insert your short descryption of the module
        #---------------------------------------------------------------------------

        print( "Module : " + str(self.__class__.__name__ ) + " imported : " + str(time.asctime()) )
        print( " version: "+ self.version )
        print( " date: " + self.date )

        self.load_invoke_words()    # loading invoke keys for the module

    # function return name of the class and module
    def module_name(self):
        return str(self.__class__.__name__ )

    # fuction for loging behaviour of the module
    def log(self,text):

        if type(text) == str :
            self.log_.append(text)

        elif type(text) == list:
            for line in text:
                self.log_.append(line)

    def stop(self):
        self.log(self, "Module " + str(self.module_name) + " stopped.")
        return self.log_

    # funcion write help for the user about the module
    def help(self):
        print ( "Help for module : " + str(str(self.__class__.__name__ )))
        print ( self.version + " " + self.date )
        print ( self.short_desc )
        print ( "help content: ")

        #----------------------DATA-TO-EDIT-FOR-THE-PROGRAMER-----------------------

        print ( "here add your help" )  # here add your help for the user, make descyption for every function

        #---------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------------

    # function for adding invoke key words of the module
    def load_invoke_words(self):

        self.invoke_words.append("hello")

    # function checking if text contains any key words, returns list of that key words
    def check_invoke(self,text):

        keys_in_input = []      # object for storing found keys

        for key in self.invoke_words:
            key.upper()

            for word in text.split(" "):
                word.upper()

                if key == word:
                    keys_in_input.append(word)

        return keys_in_input

    # main fuction of the module, here enter your code
    def run(self, user_input):

        keys = self.check_invoke(self,user_input)   # in keys variable we have all founded keys

        if keys.count != 0 :   # here we get at least 1 key

            if self.developer_test == 1:

                print ( "module: " + self.name_of_the_class + " run funcion:")
                self.log( self,"module: " + self.name_of_the_class + " run funcion:" )
                print ( "keys found : "+str(keys))
                self.log( self,"keys found : "+str(keys) )
                print ( "echo: " + user_input )
                self.log( self,"echo: " + user_input )

            else:

                if self.invoke_words.count == 0:

                    self.log_( "No key words in the dictionary." )

                else:
                #----------------------DATA-TO-EDIT-FOR-THE-PROGRAMER-----------------------
                    pass
                #---------------------------------------------------------------------------

        else:                                   # no matching keys with that module
            log.append("No keys found - "+ self.name_of_the_class)


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

import slamBase
from pcl import save
import readyaml

RGBFileNameList = ['./data/rgb1.png', './data/rgb2.png']
DepthFileNameList = ['./data/depth1.png', './data/depth2.png']
CalibrationDataFile = './calibration/asus/camera.yml'
CloudFilename = './data/cloud.pcd'

def main():
	CameraIntrinsicData, DistortionCoefficients = readyaml.parseYamlFile(CalibrationDataFile)
	camera = slamBase.CameraIntrinsicParameters(CameraIntrinsicData[0][2], CameraIntrinsicData[1][2], CameraIntrinsicData[0][0], CameraIntrinsicData[1][1], 1000.0)
	frame1 = slamBase.Frame(RGBFileNameList[0], DepthFileNameList[0])
	frame2 = slamBase.Frame(RGBFileNameList[1], DepthFileNameList[1])
	pnp = slamBase.SolvePnP(DistortionCoefficients, CameraIntrinsicData, camera, frame1, frame2)

	p0, c0 = slamBase.imageToPointCloud(RGBFileNameList[0], DepthFileNameList[0], camera)
	p1, c1 = slamBase.imageToPointCloud(RGBFileNameList[1], DepthFileNameList[1], camera)
	colors = c0 + c1
	p = slamBase.transformPointCloud(p0, pnp.T)
	pointcloud = slamBase.addPointCloud(p, p1)
	save(pointcloud, CloudFilename, format = 'pcd')
	slamBase.addColorToPCDFile(CloudFilename, colors)

if __name__ == '__main__':
	main()


# Time Complexity: O(n)
# Space Complexity: O(n)

def isValid(s):
	hashmap = {'(':')', '[':']', '{':'}'}
	stack = []
	for char in s:
		if char in hashmap:
			stack.append(char)
		# leaving char!=hashmap[stack.pop()] will throw an exception if the
		# string starts with a closing parenthesis or had all matched parentheses
		# before, leaving the stack empty and then a closing parenthesis.
		elif not stack or char != hashmap[stack.pop()]:
			return False
	return True if not stack else False

print(isValid('()[]{[]}'))
print(isValid('()[]}'))


from django.contrib import messages
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from .forms import CustomerForm, OrderForm
from .models import Customer, Order

def create_customer(request):

    if request.method == "POST":
        form = CustomerForm(request.POST, request.FILES)
        if form.is_valid():
            obj = Customer.objects.create(
                **form.cleaned_data
            )
            obj.save()
    else:
        form = CustomerForm()
    return render(request, 'orders/create.html', {'form': form})

def create_order(request):
    if request.method == "POST":
        form = OrderForm(request.POST)
        if form.is_valid():
            obj = Order.objects.create(
                **form.cleaned_data
            )
            obj.save()
    else:
        form = OrderForm()
    return render(request, 'orders/create.html', {'form': form})

class OrderAPIViewSet(APIView):

    def get(self, request, *args, **kwargs):
        pk = self.kwargs['pk']

        if Order.objects.filter(customer_id=pk).exists():
            orders_price = [order.price for order in Order.objects.filter(customer_id=pk)]
            price = sum(orders_price)
            count = len(orders_price)
            avg = price/count
            context = {
                'price': price,
                'count': count,
                'avg': avg
            }
            return Response(context)
        else:
            return Response('No orders')


import random

# n=int(input())
# m=int(input())
n=5
m=4

a=(0,0)
mark_position = set()
# mark_position.add(a)

MAP = {i:c for i,c in enumerate("RLUD")}

def createA(n,m):
    B=list()
    for i in range(m):
        l= list()
        for j in range(n):
            l.append(MAP[random.randint(0,3)])
        B.append(l)
    return B

def printF(F):
    for f in F:
        print("".join(f))
A=createA(n,m)
printF(A)

def left(a):
    r,c = a
    return (r,c-1)

def right(a):
    r,c = a
    return (r,c+1)
def up(a):
    r,c = a
    return (r-1,c)

def down(a):
    r,c = a
    return (r+1,c)

def prog1(a):
    print("going to -->", a)
    if a not in mark_position:
        mark_position.add(a)
        prog2(a)
    else:
        print("cell does not exist")
        exit(0)

def is_out(a):
    r,c = a
    if r>=m or r<0:
        return True
    if c>=n or c<0:
        return True
    return False

def prog2(a):
    r,c = a
    if A[r][c]=="R":
        new_a = right(a)
    if A[r][c]=="L":
        new_a = left(a)
    if A[r][c]=="U":
        new_a = up(a)
    if A[r][c]=="D":
        new_a = down(a)

    q = is_out(new_a)
    if q == True:
        print("out from ", a)
        exit(0)
    else:
        prog1(new_a)

prog1((0,0))


from spacy.language import Language
from spacy.tokens import Doc

@Language.component("sub_text")
def create_subtexts(doc):
    # restore「,」index list
    punct_idxs = [0]
    subtexts = []
    for token in doc:
        if token.is_punct:
            punctype = token.morph.get("PunctType")
            punctype = "".join(punctype)
            if punctype == "Comm":
                punct_idxs.append(token.i + 1)
                subtexts.append(doc[punct_idxs[-2]:  punct_idxs[-1]])
    if subtexts:
        Doc.set_extension("sub_texts", default=subtexts, force=True)
    return doc


import pandas as pd
import sklearn.feature_extraction.text as sk
import retrieve_data
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

def model(trainData,testData):
    vectorizer = sk.TfidfVectorizer(max_features = 500, use_idf=True, stop_words='english')
    X = vectorizer.fit_transform(trainData["content"]).toarray()
    Y = list(trainData["type"])
    clf_svm = svm.SVC(C=1.0, kernel='rbf', class_weight='balanced', gamma=0.1, probability=True)
    clf_kNeighbors = KNeighborsClassifier(n_neighbors = 5, weights='distance')
    clf_MultiNB = MultinomialNB(alpha=0.1)
    ensemble = VotingClassifier(estimators=[('SVM', clf_svm), ('KNeighBors', clf_kNeighbors), ('MultiNB', clf_MultiNB)],weights=[1,1,1], voting='soft', n_jobs=-1)
    ensemble.fit(X,Y)
    #c_array= [0.01, 0.1, 1]

    # Best linear C=0.1
    # Best rbf C=1.0
    '''
    for c in c_array:
        clf_svm = svm.SVC(C=c,kernel='rbf', class_weight='balanced', gamma=0.1, probability=True)
        clf_svm.fit(X,Y)
        Y1 = clf_svm.predict(test_content)
        print("Number of test samples: {}. C-value={}".format(len(real_types),c))
        print("SVM Accuracy: {}".format(accuracy_score(real_types, Y1)))
        print("SVM classification report \n {}".format(classification_report(real_types,Y1)))
    '''
    test_content = vectorizer.transform(testData["content"]).toarray()
    real_types = list(testData["type"])

    Y7 = ensemble.predict(test_content)

    print("Ensemble Accuracy: {}".format(accuracy_score(real_types, Y7)))
    print("Ensemble classification report \n {}".format(classification_report(real_types,Y7)))

    kaggle_data = pd.read_csv('test_data.csv')
    content_data = vectorizer.transform(kaggle_data["article"].values.astype('U')).toarray()
    labels = ensemble.predict(content_data)

    real_counter = 0
    fake_counter = 0
    for i, y in enumerate(labels):
        if y == "reliable":
            labels[i] = "REAL"
            real_counter += 1
        if y == "NULL":
            labels[i] = "FAKE"
            fake_counter += 1
        if y == "fake":
            labels[i] = "FAKE"
            fake_counter += 1
    print("Reals in Kaggle pred: {}".format(real_counter))
    print("Fakes in Kaggle pred: {}".format(fake_counter))
    kaggle_data["label"] = labels
    id_with_predictions = kaggle_data.drop("article", axis = 1)
    id_with_predictions.to_csv("predictions.csv", index=False)

    liar_data = pd.read_csv('liar_test.tsv', delimiter='\t')
    liar_content = vectorizer.transform(liar_data['content'].values.astype('U')).toarray()
    true_labels = liar_data['type']
    for i, y in enumerate(true_labels):
        if y == "true":
            true_labels[i] = "reliable"
        if y == "mostly-true":
            true_labels[i] = "reliable"
        else:
            true_labels[i] = "fake"

    pred_labels = ensemble.predict(liar_content)

    print("Ensemble Accuracy (Liar-Liar): {}".format(accuracy_score(true_labels, pred_labels)))
    print("Ensemble classification report (Liar-Liar) \n {}".format(classification_report(true_labels,pred_labels)))


from handlers.main.base import Chat, WebSocket, Rules, CreateRoom, Messages
from handlers.room_chat.room_handler import ChatRoom, WebSocketRoom
from handlers.interlocutor_handler import Companion

routes = [
    ('*', '/', Chat, 'main'),
    ('GET', '/ws', WebSocket, 'sockets'),
    ('GET', '/ws/{name}/{slug}', WebSocketRoom, 'room_sockets'),
    ('GET', '/rules', Rules, 'rules'),
    ('*', '/rooms', CreateRoom, 'rooms'),
    ('*', r'/{name}/{slug}', ChatRoom, 'current_room'),
    ('*', '/messages', Messages, 'messages'),
    ('*', '/companion', Companion, 'companion'),
]


def solution(n, lost, reserve):
    answer = 0

    cnt = 0 # lost중 빌려서 체육수업을 들을 수 있는 학생 수

    save = []
    for i in range(len(lost)):
        if lost[i] in reserve:
            del reserve[reserve.index(lost[i])]
            save.append(i)

    for i in range(len(save)):
        del lost[save[i]]

    save2 = []

    for i in range(len(lost)):

        if lost[i]-1 <= 0:
            if lost[i]+1 in reserve:
                cnt += 1
                del reserve[reserve.index(lost[i]+1)]
                save2.append(i)
        elif lost[i]-1 > 0:
            if lost[i] + 1 <= n:
                if lost[i]+1 in reserve:
                    cnt += 1
                    del reserve[reserve.index(lost[i]+1)]
                    save2.append(i)

            elif lost[i] + 1 > n:
                if lost[i]-1 in reserve:
                    cnt += 1
                    del reserve[reserve.index(lost[i]-1)]
                    save2.append(i)
    # print("lost" + str(lost))
    # del lost[save2[0]]
    # print(lost)
    # print("save2" + str(save2))
    # num = len(save2)
    # print(num)
    # for i in range(num):
    #     del lost[save2[i]]

    answer += cnt + ( n - len(lost) - len(save2) )

    return answer

n = 5
lost = [2, 4]
reserve = [1,3,5]
print(solution(n, lost, reserve))


def nhap():
    print('Nhap 3 so:')
    a = int(input('a='))
    b = int(input('b='))
    c = int(input('c='))
    return a, b, c
def trungvi(a,b,c):
    if a<b<c or c<b<c:
        return b
    elif b<c<a or a<c<b:
        return c
    elif b<a<c or c<a<b:
        return a
def inkq(kq):
    print('Trung vi cua 3 gia tri la:',kq)
a,b,c=nhap()
kq=trungvi(a,b,c)
inkq(kq)


import tkinter as tk
from time import sleep
import numpy as np

UNIT = 18   # pixels
EXPAND = [(0,-1),(1,0),(0,1),(-1,0)]

class Vision(tk.Tk, object):
    def __init__(self):
        super(Vision, self).__init__()
        self._MapData = []
        self.parkingdict = dict()
        self.robotdict = dict()

        self.resizable(width=False, height=True)

    def initwindows(self, mapfile = "map.txt"):
        map = open(mapfile,'r')

        while 1:
            linestr = map.readline()
            if linestr == "":
                break
            else:
                self._MapData.append(list(linestr.split()))
        self.Height = len(self._MapData)
        self.Width = len(self._MapData[0])

        self.geometry('{0}x{1}'.format((self.Width) * UNIT, (self.Height) * UNIT))
        self.plotform = tk.Canvas(self, height=(self.Height) * UNIT, width=(self.Width) * UNIT, bg='#888888888')

        self._initparkings()

    def _initparkings(self):

        for i in range(self.Height):
            for j in range(self.Width):
                if self._MapData[i][j] == 'P':
                    self.parkingdict[(j,i)] = self.plotform.create_rectangle(j * UNIT, i * UNIT, (j+1)*UNIT, (i+1)*UNIT, fill='white')
                elif self._MapData[i][j] == 'B':
                    self.plotform.create_rectangle(j * UNIT, i * UNIT, (j+1)*UNIT, (i+1)*UNIT, fill='black')
                elif self._MapData[i][j] == 'I':
                    self.plotform.create_rectangle(j * UNIT, i * UNIT, (j+1)*UNIT, (i+1)*UNIT, fill = 'yellow')
                    self.imports = (j, i)
                elif self._MapData[i][j] == 'E':
                    self.plotform.create_rectangle(j * UNIT, i * UNIT, (j+1)*UNIT, (i+1)*UNIT, fill = 'green')
                    self.exit = (j, i)

        self.plotform.pack()

    def setparking(self, pos, havecar = 0):
        if havecar:
            color = 'blue'
        else:
            color = 'white'
        self.plotform.itemconfig(self.parkingdict[pos],fill = color)

    def initrobot(self,robotnu=5):
        for i in range(robotnu):
            self.robotdict[i] = self.plotform.create_rectangle(self.imports[0]*UNIT+UNIT/6,self.imports[1]*UNIT+UNIT/6,
                                             self.imports[0]*UNIT+UNIT*5/6,self.imports[1]*UNIT+UNIT*5/6,fill = 'yellow')
        self.plotform.pack()

    def setrobot(self, robotname, robotstate):
        if robotstate == 0:
            color = 'yellow'
        elif robotstate == 1:
            color = 'blue'
        elif robotstate == 2:
            color = 'green'
        else:
            color = 'red'

        self.plotform.itemconfig(self.robotdict[robotname],fill = color)

    def moverobot(self,robotname, step):
        self.plotform.move(self.robotdict[robotname], *(step*UNIT))

    def astar(self,spos,gpos):

        temp = self._MapData[gpos[1]][gpos[0]]
        self._MapData[gpos[1]][gpos[0]] = 'X'
        openlist = dict()
        closelist = dict()

        openlist[spos] = [(0,0),spos,np.inf,0]

        while not self._expend(openlist,closelist,gpos) and len(openlist):
            pass

        node = gpos
        path = []
        path.append(gpos)
        while 1:
            try:
                node = closelist[node]
                if node == spos:
                    break
                path.append(node)
            except:
                pass
        path.reverse()

        self._MapData[gpos[1]][gpos[0]] = temp
        return path

    def _expend(self,openlist,closelist,gpos):
        node = sorted(openlist.values(),key= lambda x: x[2],reverse= True).pop()
        openlist.pop(node[1])
        if node[1] == gpos:
            closelist[gpos] = node[0]
            return 1
        for i in range(4):
            tempnode = np.array(node[1]) + np.array(EXPAND[i])
            try:
                closelist[tuple(tempnode)]
            except:
                try:
                    if self._MapData[tempnode[1]][tempnode[0]] == 'X':
                        g = node[-1]+1
                        heuris = abs(tempnode[0]-gpos[0]) + abs(tempnode[1]-gpos[1])

                        try:
                            existnode = openlist[tuple(tempnode)]
                            if heuris + g < existnode[2]:
                                openlist[tuple(tempnode)][2] = heuris + g
                        except:
                            nextnode = [node[1], tuple(tempnode), heuris + g, g]
                            openlist[tuple(tempnode)] = nextnode
                except:
                    pass
        closelist[node[1]] = node[0]
        return 0

    def updatesim(self, time=0.1):
        self.update()
        sleep(time)

    def getstate(self):
        print("MapSize:{}x{}, Parking Number:{}".format(self.Height, self.Width, len(self.parkingdict)))

if __name__ == '__main__':

    from env.robot import Robot

    vision = Vision()
    vision.initwindows()
    vision.initrobot(2)
    vision.setrobot(1, 1)

    path = vision.astar(vision.imports, (0, 12))
    robot = Robot(0,vision.imports)
    robot.setpath(path)

    while not robot.IsArrive:
        step = robot.move()
        if tuple(step) == (0,0):
            break
        vision.moverobot(1,step)

        vision.updatesim(0.5)

    stop = input('stop Y/n : ')
    if stop == 'y':
        vision.destroy()

    vision.mainloop()


from itertools import  product
n,m,x=map(int,input().split())
ca=[list(map(int,input().split())) for _ in range(n)]
for i in range(1,m+1):
    count=0
    for j in range(n):
        count+=ca[j][i]
    if count<x:
        print(-1)
        exit()

ans=10**9

for z in product((0, 1), repeat=n):#bit全探索のくみあわせを勝手に作ってくれるので使うとよい
    count=0
    count2=[0]*m
    for i in range(n):
        if z[i]==1:
            count+=ca[i][0]
            for j in range(1,m+1):
                count2[j-1]+=ca[i][j]

    for i in range(m):
        if count2[i]<x:
            break
    else:
        ans=min(count,ans)
print(ans)


'''
数据库操作
1.数据库从mysql换成sqlite,脚本层不用改动caw里面的mysqlop.py
'''
from ZongHe.caw import MySqlOp
def deleteUser(db,phone):
    '''
    根据手机号删除用户
    :param db: 一个字典，存储数据库信息
    :param phone: 手机号
    :return:
    '''
    conn = MySqlOp.connect(db)
    MySqlOp.execute(conn,f'delete from Member where MobilePhone = 18012345678;')
    MySqlOp.disconnect(conn)


from dgl.data import DGLDataset
import json
import dgl
import torch
import os
from dgl import save_graphs, load_graphs

class NestDataset(DGLDataset):
    """ 用于在DGL中自定义图数据集的模板：

    Parameters
    ----------
    file_graph_path : str, 文件级图。
    func_graph_path : str, 函数级图。
    name: 使用数据集的类型
    type:训练或测试
    ratio:训练/测试的比例

    url:
    raw_dir : str
        指定下载数据的存储目录或已下载数据的存储目录。默认: ~/.dgl/
    save_dir : str
        处理完成的数据集的保存目录。默认：raw_dir指定的值
    force_reload : bool
        是否重新导入数据集。默认：False
    verbose : bool
        是否打印进度信息。
    """

    def __init__(self, file_graph_path, func_graph_path, name, type = 'train',ratio=1,url=None, raw_dir=None, save_dir=None, force_reload=True,
                 verbose=False):
        self.file_graph_path = file_graph_path
        self.func_graph_path = func_graph_path
        self.type = type
        self.ratio = ratio
        self.file_graphs = []
        self.func_graphs = []
        self.num_funcs = []
        self.labels = []

        # super(path_no_v, path_v).__init__(path_no_v=path_no_v,
        #                                   path_v=path_v,
        #                                   name='dataset_name',
        #                                   url=url,
        #                                   raw_dir=raw_dir,
        #                                   save_dir=save_dir,
        #                                   force_reload=force_reload,
        #                                   verbose=verbose)
        # super().__init__(name, url, raw_dir, save_dir, force_reload, verbose)
        super().__init__(name, url, raw_dir, save_dir, force_reload, verbose)

    def download(self):
        # 将原始数据下载到本地磁盘
        pass

    def process(self):
        print("loading data")
        f = open(self.file_graph_path,'r')
        ff = open(self.func_graph_path,'r')

        if self.type == 'train':
            # generate file_g_list
            file_data = json.load(f)
            print("number of files:",len(file_data))
            num_train = int(self.ratio * len(file_data))
            num_funcs = 0
            for i in range(num_train):
                data = file_data[i]

                #num_funs***********************************
                self.num_funcs.append(data[0])
                self.labels.append(data[1])
                num_funcs += len(data[1])
                if(data[0]!=len(data[1])):
                    print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!++++' + str(i))
                g = dgl.graph((data[2], data[3]))
                g = g.to('cuda:0')
                self.file_graphs.append(g)
            print(num_funcs)
            # generate func_g_list
            func_data = json.load(ff)
            # print(num_funcs,len(func_data))
            for i in range(num_funcs):
                data = func_data[i]
                g = dgl.graph((data[0], data[1]))
                g.ndata['n_attr'] = torch.Tensor(data[2])
                g.edata['e_attr'] = torch.Tensor(data[3])
                # g = dgl.add_self_loop(g)
                g = g.to('cuda:0')
                self.func_graphs.append(g)
            # print(num_funcs,len(self.func_graphs))
        elif self.type == 'test':
            #skip train data
            file_data = json.load(f)
            num_train = int(self.ratio * len(file_data))
            func_begin = 0
            num_funs = 0
            for i in range(num_train):
                data = file_data[i]
                func_begin += len(data[1])

            # generate file_g_list
            for i in range(num_train,len(file_data)):
                data = file_data[i]
                self.num_funcs.append(data[0])
                self.labels.append(data[1])
                num_funs += len(data[1])
                if(data[0]!=len(data[1])):
                    print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!++++' + str(i))
                g = dgl.graph((data[2], data[3]))
                g = g.to('cuda:0')
                self.file_graphs.append(g)
                # print(len(g.nodes()),data[0])

            # generate func_g_list
            func_data = json.load(ff)
            for i in range(func_begin,len(func_data)):
                data = func_data[i]
                g = dgl.graph((data[0], data[1]))
                g.ndata['n_attr'] = torch.Tensor(data[2])
                g.edata['e_attr'] = torch.Tensor(data[3])
                # g = dgl.add_self_loop(g)
                g = g.to('cuda:0')
                self.func_graphs.append(g)
            # n_nodes = 0
            # for g in self.file_graphs:
            #     n_nodes += len(g.nodes())
            # print(n_nodes,len(self.func_graphs))
        print("successfully load data")
        # print(len(self.file_graphs))

    def __getitem__(self, idx):
        # 通过idx得到与之对应的一个样本, label with no meaning
        return self.file_graphs[idx],1

    def __len__(self):
        # 数据样本的数量
        # l = 0
        # with open(self.path_v,'r') as f:
        #     vul_data = json.load(f)
        #     l += len(vul_data)
        # with open(self.path_v, 'r') as f:
        #     no_vul_data = json.load(f)
        #     l += len(no_vul_data)
        return len(self.file_graphs)

    def save(self):
        # 将处理后的数据保存至 `self.save_path` _dgl_graph.bin
        # graph_path = self.save_dir
        # labels = torch.tensor(self.labels)
        # save_graphs(graph_path, self.graphs, {'labels': labels})
        # 在Python字典里保存其他信息
        # info_path = os.path.join(self.save_path, self.mode + '_info.pkl')
        # save_info(info_path, {'num_classes': self.num_classes})
        pass
    def load(self):
        # 从 `self.save_path` 导入处理后的数据
        graph_path = self.save_dir
        self.graphs, label_dict = load_graphs(graph_path)
        for g in self.graphs:
            g.to("cuda:0")
        self.labels = label_dict['labels']

    def has_cache(self):
        # 检查在 `self.save_path` 中是否存有处理后的数据
        # graph_path = os.path.join(self.save_path)
        # info_path = os.path.join(self.save_path, self.mode + '_info.pkl')
        # return os.path.exists(self.save_dir)
        return False


#2.Desarrollar un programa para generar una lista por comprensión con los números entre 1 y N que sean múltiplos de 7 o que terminen con el dígito 7. El valor de N se ingresa desde el teclado. Luego se solicita ordenar la lista utilizando como criterio de ordenamiento la suma de los dígitos de cada número. Mostrar por pantalla la lista obtenida antes y después del ordenamiento. No se permite el uso de cadenas de caracteres. Ejemplo:Límite superior? 100[7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57, 63, 67, 70, 77, 84, 87, 91, 97, 98][21, 14, 42, 7, 70, 17, 35, 27, 63, 28, 37, 91, 47, 56, 57, 84, 49, 67, 77, 87, 97, 98]
def sumardig(num):
    suma = 0
    while num!=0:
        suma = suma + num%10
        num = num // 10
    return suma

n = int(input("Límite superior? "))
lista = [i for i in range(1, n+1) if i%10==7 or i%7==0]
print(lista)
lista.sort(key=sumardig)
print(lista)


def largestRange(array):
    mset = set()
    maxres = [array[0]]*2
    res = [array[0]]*2
    for el in array:
        mset.add(el)
    for el in array:
        low, high = -1, 1
        res = [el, el]
        while el + low in mset:
            mset.remove(el + low)
            res[0] = el+low
            low -= 1
        while el + high in mset:
            mset.remove(el + high)
            res[1] = el+high
            high += 1
        if res[1]-res[0] > maxres[1]-maxres[0]:
            maxres = res
    return maxres

arr = [1]

print(largestRange(arr))


import math
import numpy as np

DIRECTIONS = {"N": 0x1, "S": 0x2, "E": 0x4, "W": 0x8, "F": 0x10, "B": 0x20 }

def get_open_room():
    val = 0x0;
    for x in DIRECTIONS.values():
        val += x
    return val

def convert_maze_to_list_of_lines(maze):
    lines = []
    shape = np.shape(maze)
    for i in range(shape[0]):
        for j in range(shape[1]):
            for k in range(shape[2]):
                val = maze[i,j,k]
                if (val & DIRECTIONS["E"]):
                    lines.append(((i, j, k), (i+1, j, k)))
                if (val & DIRECTIONS["N"]):
                    lines.append(((i, j, k), (i, j+1, k)))
                if (val & DIRECTIONS["F"]):
                    lines.append(((i, j, k), (i, j, k+1)))

    return lines

def output_txt_file(filename, grid):
    with open(filename, "wb") as f:
        w, h, l = np.shape(grid)
        f.write(bytes("WIDTH: %i\n" % w, 'utf-8'));
        f.write(bytes("HEIGHT: %i\n" % h, 'utf-8'));
        f.write(bytes("LENGTH: %i\n" % l, 'utf-8'))
        f.write(bytes("ENDIAN: BIG\n", 'utf-8'));
        f.write(grid.tobytes());
    return

def import_txt_file(filename):
    with open(filename, "rb") as f:
        lines = f.readlines()

        w = int(lines[0].decode('utf-8').split(' ')[1])
        h = int(lines[1].decode('utf-8').split(' ')[1])
        l = int(lines[2].decode('utf-8').split(' ')[1])

        b_str = b""
        for i in range(4, len(lines)):
            b_str += lines[i]

        grid = np.frombuffer(b_str, dtype=int).reshape((w, h, l))
    return grid

def create_viewer_image(grid, cell_size):
    w, h, l = np.shape(grid)
    maze_w = cell_size * w + (w + 1)
    maze_h = cell_size * h + (h + 1)

    img_w = maze_w * l + 2 * (l + 1)
    img_h = maze_h + 4

    # print(maze_w, maze_h, img_w, img_h)

    # init image
    img = np.ones((img_w, img_h, 3))

    # set image borders
    img[0,:] = img[:,0] = img[-1,:] = img[:,-1] = [0.5, 0.5, 0.5]

    # set maze walls
    for i in range(l):
        row=i*(maze_h+2)

        for j in range(w+1):
            sub_row=j*math.floor(maze_h/h)
            img[2+row+sub_row,2:2+(maze_w)] = [0.0, 0.0, 0.0]

        for j in range(h+1):
            sub_col=j*math.floor(maze_w/w)
            img[2+row:2+(maze_h)+row,2+sub_col] = [0.0, 0.0, 0.0]

    # set maze hallways and portals
    for i in range(l):
        curr_grid = grid[:,:,i]
        hall_size=cell_size-2
        row=2+i*(2+maze_w)
        col=2

        for j in range(0, w):
            for k in range(0, h):
                # hallways
                if (curr_grid[j,k] & DIRECTIONS["N"]):
                    sub_row = 2+j*math.floor(maze_h/h)
                    sub_col = (k+1)*math.floor(maze_w/w)
                    img[row+sub_row:row+sub_row+hall_size, col+sub_col] = [1.0, 1.0, 1.0]
                if (curr_grid[j,k] & DIRECTIONS["E"]):
                    sub_row = (j+1)*math.floor(maze_h/h)
                    sub_col = 2+k*math.floor(maze_w/w)
                    img[row+sub_row, col+sub_col:col+sub_col+hall_size] = [1.0, 1.0, 1.0]

                # portals
                if (curr_grid[j,k] & DIRECTIONS["F"]):
                    sub_row=1+j*math.floor(maze_h/h)
                    sub_col=1+k*math.floor(maze_w/w)
                    img[row+sub_row,col+sub_col]=[1.0, 0.0, 0.0]
                if (curr_grid[j,k] & DIRECTIONS["B"]):
                    sub_row=-1+(j+1)*math.floor(maze_h/h)
                    sub_col=-1+(k+1)*math.floor(maze_w/w)
                    img[row+sub_row,col+sub_col]=[0.0, 0.0, 1.0]

    return img

def create_viewer_image_with_path(grid, path):
    cell_size = 5
    w, h, l = np.shape(grid)
    maze_w = cell_size * w + (w + 1)
    maze_h = cell_size * h + (h + 1)
    img = create_viewer_image(grid, cell_size)

    for edge in path:
        p_a, p_b = edge

        if p_a[0] > p_b[0] or p_a[1] > p_b[1] or p_a[2] > p_b[2]:
            tmp = p_a
            p_a = p_b
            p_b = tmp

        x_a, y_a, z_a = p_a
        x_b, y_b, z_b = p_b

        row_a = 5+x_a*math.floor(maze_h/h)+z_a*(2+maze_h)
        col_a = 5+y_a*math.floor(maze_w/w)

        row_b = 5+x_b*math.floor(maze_h/h)+z_b*(2+maze_h)
        col_b = 5+y_b*math.floor(maze_w/w)

        if z_a != z_b:
            img[row_a,col_a] = [0.0, 0.0, 1.0]
            img[row_b,col_b] = [0.0, 0.0, 1.0]
        else:
            img[row_a:row_b+1,col_a:col_b+1] = [0.0, 0.0, 1.0]

    return img


"""
-Link: https://app.codesignal.com/tournaments/n8qAhcgHZLy37xtPp
"""

#
def zFunctionNaive(s):

    result = []

    for i in range(len(s)):
        result.append(0)
        for j in range(i, len(s)):
            if s[j] == s[result[i]]:
                result[i] += 1
            else:
                break

    return result

#
def reachNextLevel(experience, threshold, reward):
    experience += reward
    if experience >= threshold:
        return True
    return False

#
def oddNumbersBeforeZero(sequence):
    count = 0
    for x in sequence:
        if x == 0:
            break
        elif x % 2 != 0:
            count += 1
    return count

#
def computeDefiniteIntegral(l, r, p):

    res = 0
    tmp1, tmp2 = l, r

    for i in range(len(p)):
        res += p[i] * (tmp2-tmp1) / (i + 1)

        tmp1 *= l
        tmp2 *= r
    return res

#
def create2DArray(lengths):

    res = []
    for i in lengths:
        res.append([j for j in range(i)])
    return res


import requests,json,random,string,os
from datetime import datetime

from captcha.image import ImageCaptcha
from PIL import Image
from PIL.ImageDraw import Draw
from PIL.ImageFont import truetype
import base64
from uuid import uuid4
from Crypto.Cipher import AES

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

class ArithmeticCaptchaAbstract(ImageCaptcha):
    def __init__(self, width=160, height=60, fonts=None, font_sizes=None):
        self._width= width
        self._height = height
        self._fonts = fonts
        self._font_sizes = font_sizes

    def create_captcha_image(self,chars, color, background):
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(c):
            font =  truetype(font=self._fonts,size=self._font_sizes)
            w, h =  draw.textsize(c,font=font)
            dx = random.randint(0, 10)
            dy = random.randint(0, 10)
            im = Image.new("RGBA",(w + dx, h + dy),background)
            Draw(im).text((dx,dy),c,font=font,fill=random_color())
            return im
        images = []
        for c in chars:
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images])
        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.10 * average)
        offset = int(average * 0.1)
        for im in images:
            w,h = im.size
            image.paste(im, (offset, int((self._height - h) / 2)))
            offset = offset + w + random.randint(-rand, 0)
        if width > self._width:
            image = image.resize((self._width, self._height))

        return image

#获取随机颜色
def random_color():
    return (random.randint(0,255),random.randint(0,255),random.randint(0,255))

#随机生成文件名
def generate_name():
    nowTime= datetime.now().strftime("%Y%m%d%H%M%S")
    randomNum = random.randint(0,100)
    if randomNum <=10:
        randomNum = str(0) + str(randomNum)
    uniqueNum = str(nowTime) + str(randomNum)
    return uniqueNum

def get_public_ip():
    html_text = requests.get("http://ip.42.pl/raw").text
    return html_text

###获取IP归属地
def getIpInfo(ip):
    try:
        ipinfo=requests.get("http://ip.taobao.com/service/getIpInfo.php?ip={}&accessKey=alibaba-inc".format(ip)).text
        ipinfo=json.loads(ipinfo).get('data')
        ipinfo_str=ipinfo.get('country')+'|'+ipinfo.get('region')+"|"+ipinfo.get('city')+"|"+ipinfo.get('isp')
        return ipinfo_str
    except Exception as e:
         print(e)

def menu_tree(data,root,root_filed,node_filed):
    """
    解析list数据为树结构
    :param data:  被解析的数据
    :param root: 根节点值
    :param root_field: 根节点字段
    :param node_field: 节点字段
    :return: list
    """
    menu = [ item for item in data if item.get(root_filed) == root ]

    for i in data:
        node = i.get(node_filed)
        children = []
        for j in data:
            parent = j.get(root_filed)
            if node == parent:
                j['component']=j.get('component')
                del j['alwaysShow']
                del j['redirect']
                children.append(j)
                i['children'] = children
    return menu

def dept_tree(data,root,root_filed,node_filed):
    menu = [ item for item in data if item.get(root_filed) == root ]

    for i in data:
        node = i.get(node_filed)
        children = []
        for j in data:
            parent = j.get(root_filed)
            if node == parent:
                children.append(j)
                i['children'] = children
    return menu

def getColor():
    return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

if __name__ == "__main__":
    pass


#For fast I/O
import sys
input = lambda: sys.stdin.readline().strip()

'''Idea
As long as a tree exists, then intersections can be uncrossed like
in the given example where (2,4),(3,6) became (2,3),(4,6).
Just keep uncrossing the edges until the sum of |v2-v1| over all
edges (v1,v2) is minimized, which necessitates no intersections.
Uncrossing decreases this sum by at least 1 each time and a minimum must
exist since there are finite ways to connect it.
Therefore it will eventually reach the minimum and be uncrossed.
Now just have to construct...

Can be shown that as long as there's at least 2 odd degrees and that the
sum of the degrees is even, the tree exists.
Now just need to make it not intersect on the circle.

Idea:
1.) Split into contiguous blocks of 100...00 (1 followed by k 0s for some
    k>=0). Call the 1 in front the tail and the last 0 (possibly the 1
    itself if there are no 0s) the head.
2.) Connect them consecutively. The tail will be a leaf, and only the head
    will get anymore connections after this
3.) Designate one head as the central power. Connect all other heads to
    this central power.
4.) Since there are an even number of 1s, there will be an even number of
    blocks. It can then be checked that all parities are satisfied when
    connecting all block's heads to a central power.
'''

for Homu in range(int(input())):
    n = int(input())
    s = input()
    parity = [int(s[i]) for i in range(n)]
    n1 = parity.count(1)

    if n1 == 0 or n1%2 == 1:   #If not possible
        print('nO')    #Print nO
    else:   #If possible
        print('YeS')   #Print YeS

        #Print out all edges within a 10...0 block (clockwise)
        if parity[0] == 0:
            print(1,n)
        for i in range(1,n):
            if parity[i] == 0:
                print(i, i+1)

        #Find indices with 1 (0-indexed, take 0 as n)
        ind = []
        for i in range(1,n):
            if parity[i] == 1:
                ind.append(i)
        if parity[0] == 1:
            ind.append(n)

        #Print out all edges connecting heads of blocks
        #The indices of heads when 1-indexed coincides with the
        #positions of the 1s when 0-indexed
        central_power = ind.pop()
        for i in ind:
            print(i, central_power)


import numpy as np
from PIL import Image
# We can add more of these later...keeping it simple for now!
COLOR_CODES = {
"COLOR_1": [107, 174, 193],
"COLOR_2": [30,65,69],
"COLOR_3": [11,135,182],
"COLOR_4": [28,46,37],
"COLOR_5": [171,195,197],
"COLOR_6": [62,99,99],
"COLOR_7": [91,133,137],
"COLOR_8": [59,108,127],

}
HORIZONTAL_PIXELS = 25;
VERTICAL_PIXELS = 25;

class MarkovBobRoss:

    def __init__(self, transition_matrix):

     self.transition_matrix = transition_matrix
     self.colors = list(transition_matrix.keys())

    def get_next_color(self, current_color):

        return np.random.choice(
        self.colors,
        p=[self.transition_matrix[current_color][next_color] \
        for next_color in self.colors]
    )

    def compose_art(self, current_color="COLOR_1", art_length = HORIZONTAL_PIXELS*VERTICAL_PIXELS):
        index = 1
        artwork = np.zeros((HORIZONTAL_PIXELS, VERTICAL_PIXELS, 3), dtype=np.uint8)
        for row in range(HORIZONTAL_PIXELS):
            for col in range (VERTICAL_PIXELS):

                next_color = self.get_next_color(current_color)
                artwork [row, col] = COLOR_CODES[next_color]
                current_color = next_color
                index += 1
        return artwork

    def write_drawing(self, artwork):
        """turns  array into painting"""
        bobbyross = Image.fromarray(artwork, 'RGB')
        bobbyross.save('bobbyross.png')
        return bobbyross

def main():
        Bob_maker = MarkovBobRoss({
            "COLOR_1": {"COLOR_1": 0.3, "COLOR_2": 0.1, "COLOR_3": 0.2,"COLOR_4": 0.05, "COLOR_5": 0.02, "COLOR_6": 0.1,"COLOR_7": 0.08, "COLOR_8": 0.15},
            "COLOR_2": {"COLOR_1": 0.06, "COLOR_2": 0.25, "COLOR_3": 0.04,"COLOR_4": 0.2, "COLOR_5": 0.05, "COLOR_6": 0.15,"COLOR_7": 0.1, "COLOR_8": 0.15},
            "COLOR_3": {"COLOR_1": 0.2, "COLOR_2": 0.1, "COLOR_3": 0.35,"COLOR_4": 0.05, "COLOR_5": 0.15, "COLOR_6": 0.06,"COLOR_7": 0.06, "COLOR_8": 0.03},
            "COLOR_4": {"COLOR_1": 0.09, "COLOR_2": 0.25, "COLOR_3": 0.02,"COLOR_4": 0.3, "COLOR_5": 0.02, "COLOR_6": 0.15,"COLOR_7": 0.07, "COLOR_8": 0.1},
            "COLOR_5": {"COLOR_1": 0.2, "COLOR_2": 0.06, "COLOR_3": 0.15,"COLOR_4": 0.02, "COLOR_5": 0.35, "COLOR_6": 0.05,"COLOR_7": 0.08, "COLOR_8": 0.09},
            "COLOR_6": {"COLOR_1": 0.05, "COLOR_2": 0.1, "COLOR_3": 0.06,"COLOR_4": 0.2, "COLOR_5": 0.09, "COLOR_6": 0.2,"COLOR_7": 0.15, "COLOR_8": 0.15},
            "COLOR_7": {"COLOR_1": 0.04, "COLOR_2": 0.11, "COLOR_3": 0.04,"COLOR_4": 0.1, "COLOR_5": 0.06, "COLOR_6": 0.15,"COLOR_7": 0.3, "COLOR_8": 0.2},
            "COLOR_8": {"COLOR_1": 0.1, "COLOR_2": 0.1, "COLOR_3": 0.1,"COLOR_4": 0.06, "COLOR_5": 0.1, "COLOR_6": 0.2,"COLOR_7": 0.1, "COLOR_8": 0.24},

    })

        new_art = Bob_maker.compose_art(current_color="COLOR_1", art_length=HORIZONTAL_PIXELS*VERTICAL_PIXELS)
        """make the art with current color and length"""

        print("Writing song to file...")
        """print something cool here"""

        img = Bob_maker.write_drawing(new_art)
        img.show()
        print("Process completed!")
if __name__ == "__main__":
        main()


import os, argparse
import sys
import scipy
import timeit
import gzip
import torch
from numpy import save
import numpy as np
from sys import stdout
import pickle as pkl
import scipy.io as scio
import torchvision
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader, Dataset

def to_numpy(x):
	if x.is_cuda:
		return x.data.cpu().numpy()
	else:
		return x.data.numpy()

path = 'stl10_matlab/'
data=scio.loadmat(path+'train.mat')
train_X = data['X']
train_Y = data['y'].squeeze()
data=scio.loadmat(path+'test.mat')
test_X = data['X']
test_Y = data['y'].squeeze()
X = []
Y = []
X.append(train_X)
X.append(test_X)
Y.append(train_Y)
Y.append(test_Y)
X = np.concatenate(X,axis=0)
Y = np.concatenate(Y,axis=0)
X = np.reshape(X,(-1,3,96,96))
X = np.transpose(X,(0,1,3,2))
image_train = X.astype('float32')/255
image_train[:,0,:,:] = (image_train[:,0,:,:] - 0.485)/0.229
image_train[:,1,:,:] = (image_train[:,1,:,:] - 0.456)/0.224
image_train[:,2,:,:] = (image_train[:,2,:,:] - 0.406)/0.225
label_train = Y.astype('float32')-1

res50_model = torchvision.models.resnet50(pretrained=True)
res50_conv = nn.Sequential(*list(res50_model.children())[:-2])
res50_conv.eval()
data = torch.from_numpy(image_train)
dataloader = DataLoader(TensorDataset(data),batch_size=200,shuffle=False)
res50_conv = res50_conv.cuda()
total_output = []
for batch_idx, batch in enumerate(dataloader):
    inputs = batch[0].cuda()
    output = res50_conv(inputs)
    total_output.append(output.data)
total_output = torch.cat(total_output,dim=0)

feature_train = torch.sum(torch.sum(total_output,dim=-1),dim=-1)/9

image_train = to_numpy(feature_train)

save('stl_features.npy', image_train)
save('stl_label.npy', label_train)


import os
import sys
import glob
import subprocess

# Destination folder.
for arg in sys.argv:
    if arg in ['-h', '--help']:
        print('Usage: %s [DESTINATION_FOLDER]' % sys.argv[0])
        sys.exit(1)

dst_dir = '.'
if len(sys.argv) == 2:
    if not os.path.isdir(sys.argv[1]):
        print("ERROR: destination '%s' is not a folder." % sys.argv[1])
        sys.exit(1)
    dst_dir = sys.argv[1]

# Find available locales.
langs = subprocess.check_output(['localedef', '--list'], universal_newlines = True)
langs = set(langs.split(os.linesep))
langs.remove('')

# Find source folder.
script = os.path.abspath(__file__).replace('.pyc', '.py')
wrk_dir = os.path.dirname(script)
top_dir = os.path.abspath(os.path.join(wrk_dir, '..', '..'))
etc_dir = os.path.abspath(os.path.join(top_dir, 'etc'))
i18n_dir = os.path.abspath(os.path.join(top_dir, 'i18n'))

# Find config files.
list_ini = []
inis = glob.glob(os.path.join(etc_dir, '*.ini'))
for ini in inis:
    f = os.path.splitext(os.path.basename(ini))[0]
    list_ini.append(f)

# Find translations.
i18n_set = set()
i18n_set.add('en_US.utf8')
i18ns = glob.glob(os.path.join(i18n_dir, '*'))
for i18n in i18ns:
    if os.path.isdir(i18n):
        d = os.path.basename(i18n)
        i18n_set.add(d + '.utf8')

# Find locales with UTF-8 encoding.
sint = i18n_set.intersection(langs)
sdif = i18n_set.difference(langs)

if len(sdif) != 0:
    print("ERROR: your system does not support the following languages: %s" % ' '.join(sdif))
    sys.exit(1)

# Execute DUNE.
if not os.path.exists('./dune'):
    print("ERROR: this script must be executed in DUNE's build folder.")
    sys.exit(1)

for i18n in sint:
    name = i18n.split('.')[0]
    lang = name + '.UTF-8'

    for ini in list_ini:
        os.environ['LC_ALL'] = lang
        os.environ['LANG'] = lang
        os.environ['LANGUAGE'] = lang
        sys.stderr.write('\n* Processing %s:\n\n' % ini)
        sys.stderr.flush()
        subprocess.check_call(['./dune', '-c', ini, '-p', 'Hardware', '-X', dst_dir])

xmls = glob.glob(os.path.join(dst_dir, '*.xml'))
for xml in xmls:
    try:
        subprocess.check_call(['xmllint', '--format', xml, '-o', 'tmp.xml'])
        os.remove(xml)
        os.rename('tmp.xml', xml)
    except:
        pass


try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO
import json
from subprocess import check_output, CalledProcessError, TimeoutExpired, STDOUT
import os
import re

from concurrent.futures import ThreadPoolExecutor
from threading import Event

from ipython_genutils.tempdir import TemporaryDirectory
from notebook.base.handlers import APIHandler
from tornado import gen, web
from tornado.ioloop import IOLoop
from tornado.concurrent import run_on_executor

from jupyterlab.jlpmapp import which, YARN_PATH, HERE as jlab_dir
from jupyterlab.commands import (
    get_app_info, install_extension, uninstall_extension,
    enable_extension, disable_extension, _read_package,
    _AppHandler, _fetch_package_metadata, _semver_key,
    _validate_compatibility, _validate_extension
)

try:
    from urllib.error import URLError
except ImportError:
    from urllib2 import URLError

def _make_extension_entry(name, description, enabled, core, latest_version,
                          installed_version, status, installed=None):
    """Create an extension entry that can be sent to the client"""
    ret = dict(
        name=name,
        description=description,
        enabled=enabled,
        core=core,
        latest_version=latest_version,
        installed_version=installed_version,
        status=status,
    )
    if installed is not None:
        ret['installed'] = installed
    return ret

def _ensure_compat_errors(info, app_dir, logger):
    """Ensure that the app info has compat_errors field"""
    handler = _AppHandler(app_dir, logger)
    info['compat_errors'] = handler._get_extension_compat()

_message_map = {
    'install': re.compile(r'(?P<name>.*) needs to be included in build'),
    'uninstall': re.compile(r'(?P<name>.*) needs to be removed from build'),
    'update': re.compile(r'(?P<name>.*) changed from (?P<oldver>.*) to (?P<newver>.*)'),
}

def _build_check_info(app_dir, logger):
    """Get info about packages scheduled for (un)install/update"""
    handler = _AppHandler(app_dir, logger)
    messages = handler.build_check(fast=True)
    # Decode the messages into a dict:
    status = {'install': [], 'uninstall': [], 'update': []}
    for msg in messages:
        for key, pattern in _message_map.items():
            match = pattern.match(msg)
            if match:
                status[key].append(match.group('name'))
    return status

class ExtensionManager(object):
    executor = ThreadPoolExecutor(max_workers=5)

    def __init__(self, log, app_dir):
        self.log = log
        self.app_dir = app_dir
        self._outdated = None
        # Start fetching data on outdated extensions immediately
        IOLoop.current().spawn_callback(self._get_outdated)

    @gen.coroutine
    def list_extensions(self):
        """Handle a request for all installed extensions"""
        info = get_app_info(app_dir=self.app_dir, logger=self.log)
        build_check_info = _build_check_info(self.app_dir, self.log)
        _ensure_compat_errors(info, self.app_dir, self.log)
        extensions = []
        # TODO: Ensure loops can run in parallel
        for name, data in info['extensions'].items():
            status = 'ok'
            pkg_info = yield self._get_pkg_info(name, data)
            if info['compat_errors'].get(name, None):
                status = 'error'
            else:
                for packages in build_check_info.values():
                    if name in packages:
                        status = 'warning'
            extensions.append(_make_extension_entry(
                name=name,
                description=pkg_info['description'],
                enabled=(name not in info['disabled']),
                core=False,
                # Use wanted version to ensure we limit ourselves
                # within semver restrictions
                latest_version=pkg_info['latest_version'],
                installed_version=data['version'],
                status=status,
            ))
        for name in build_check_info['uninstall']:
            data = yield self._get_scheduled_uninstall_info(name)
            extensions.append(_make_extension_entry(
                name=name,
                description=data['description'],
                installed=False,
                enabled=False,
                core=False,
                latest_version=data['version'],
                installed_version=data['version'],
                status='warning',
            ))
        raise gen.Return(extensions)

    @run_on_executor
    def install(self, extension):
        """Handle an install/update request"""
        try:
            install_extension(extension, app_dir=self.app_dir, logger=self.log)
        except ValueError as e:
            raise gen.Return(dict(status='error', message=str(e)))
        raise gen.Return(dict(status='ok',))

    @run_on_executor
    def uninstall(self, extension):
        """Handle an uninstall request"""
        did_uninstall = uninstall_extension(extension, app_dir=self.app_dir, logger=self.log)
        raise gen.Return(dict(status='ok' if did_uninstall else 'error',))

    @run_on_executor
    def enable(self, extension):
        """Handle an enable request"""
        enable_extension(extension, app_dir=self.app_dir, logger=self.log)
        raise gen.Return(dict(status='ok',))

    @run_on_executor
    def disable(self, extension):
        """Handle a disable request"""
        disable_extension(extension, app_dir=self.app_dir, logger=self.log)
        raise gen.Return(dict(status='ok',))

    @gen.coroutine
    def _get_pkg_info(self, name, data):
        """Get information about a package"""
        info = _read_package(data['path'])

        # Get latest version that is compatible with current lab:
        outdated = yield self._get_outdated()
        if outdated and name in outdated:
            info['latest_version'] = outdated[name]
        else:
            # Fallback to indicating that current is latest
            info['latest_version'] = info['version']

        raise gen.Return(info)

    def _get_outdated(self):
        """Get a Future to information from `npm/yarn outdated`.

        This will cache the results. To refresh the cache, set
        self._outdated to None before calling. To bypass the cache,
        call self._load_outdated directly.
        """
        # Ensure self._outdated is a Future for data on outdated extensions
        if self._outdated is None:
            self._outdated = self._load_outdated()
        # Return the Future
        return self._outdated

    def refresh_outdated(self):
        self._outdated = self._load_outdated()
        return self._outdated

    @gen.coroutine
    def _load_outdated(self):
        """Get the latest compatible version"""
        info = get_app_info(app_dir=self.app_dir, logger=self.log)
        data = yield self.executor.submit(
            self._latest_compatible_package_versions,
            tuple(info['extensions'].keys())
        )
        return data

    def _latest_compatible_package_versions(self, names):
        """Get the latest compatible version of a list of packages.

        This is a variant of similar code in lab app, but optimized
        for checking several packages in one go.
        """
        handler = _AppHandler(self.app_dir, self.log)
        core_data = handler.info['core_data']

        keys = []
        for name in names:
            try:
                metadata = _fetch_package_metadata(handler.registry, name, self.log)
            except URLError:
                continue
            versions = metadata.get('versions', [])

            # Sort pre-release first, as we will reverse the sort:
            def sort_key(key_value):
                return _semver_key(key_value[0], prerelease_first=True)

            for version, data in sorted(versions.items(),
                                        key=sort_key,
                                        reverse=True):
                deps = data.get('dependencies', {})
                errors = _validate_compatibility(name, deps, core_data)
                if not errors:
                    # Found a compatible version
                    keys.append('%s@%s' % (name, version))
                    break  # break inner for

        versions = {}
        if not keys:
            return versions
        with TemporaryDirectory() as tempdir:
            ret = handler._run([which('npm'), 'pack'] + keys, cwd=tempdir, quiet=True)
            if ret != 0:
                msg = '"%s" is not a valid npm package'
                raise ValueError(msg % keys)

            for key in keys:
                fname = key[0].replace('@', '') + key[1:].replace('@', '-').replace('/', '-') + '.tgz'
                data = _read_package(os.path.join(tempdir, fname))
                # Verify that the version is a valid extension.
                if not _validate_extension(data):
                    # Valid
                    versions[key] = data['version']
        return versions

    @run_on_executor
    def _get_scheduled_uninstall_info(self, name):
        """Get information about a package that is scheduled for uninstallation"""
        target = os.path.join(
            self.app_dir, 'staging', 'node_modules', name, 'package.json')
        if os.path.exists(target):
            with open(target) as fid:
                return json.load(fid)
        else:
            return None

class ExtensionHandler(APIHandler):

    def initialize(self, manager):
        self.manager = manager

    @web.authenticated
    @gen.coroutine
    def get(self):
        """GET query returns info on all installed extensions"""
        if self.get_argument('refresh', False) == '1':
            yield self.manager.refresh_outdated()
        extensions = yield self.manager.list_extensions()
        self.finish(json.dumps(extensions))

    @web.authenticated
    @gen.coroutine
    def post(self):
        """POST query performs an action on a specific extension"""
        data = self.get_json_body()
        cmd = data['cmd']
        name = data['extension_name']
        if (cmd not in ('install', 'uninstall', 'enable', 'disable') or
                not name):
            raise web.HTTPError(
                422, 'Could not process instrution %r with extension name %r' % (
                    cmd, name))

        # TODO: Can we trust extension_name? Does it need sanitation?
        #       It comes from an authenticated session, but its name is
        #       ultimately from the NPM repository.
        ret_value = None
        try:
            if cmd == 'install':
                ret_value = yield self.manager.install(name)
            elif cmd == 'uninstall':
                ret_value = yield self.manager.uninstall(name)
            elif cmd == 'enable':
                ret_value = yield self.manager.enable(name)
            elif cmd == 'disable':
                ret_value = yield self.manager.disable(name)
        except gen.Return as e:
            ret_value = e.value
        except Exception as e:
            raise web.HTTPError(500, str(e))

        if ret_value is None:
            self.set_status(200)
        else:
            self.finish(json.dumps(ret_value))

# The path for lab extensions handler.
extensions_handler_path = r"/discovery/api/extensions"


import pygame as pg
import numpy as np
import logging, log

import threading

log = logging.getLogger("othello")

''' ===================================================  color setting '''
GREEN = (34, 116, 28)
black = (0, 0, 0)

color = GREEN

''' ===================================================  size setting '''
window_size = [450, 450]
tile_size = 50

width = tile_size
height = tile_size
margin = 5
stone_margin = 6

''' ===================================================  array ( back ground ) '''
# grid = [ [0] * 8 for x in range(8)]

grid = np.zeros((8,8), dtype=int)

''' ===================================================  image setting '''
white_stone = pg.image.load("white_stone.png")
black_stone = pg.image.load("black_stone.png")

''' ===================================================  pygame main '''

class Othello():
	def __init__(self):
		pg.init()

		self.screen = pg.display.set_mode(window_size) # window size setting
		pg.display.set_caption("Othello") # title setting
		self.clock = pg.time.Clock() # frame to seconds, normal 30, 60

		pg.image.load("white_stone.png")
		pg.image.load("black_stone.png")

		self.show_board()

	def run_game(self):
		self.done = False

		self.cnt = 0
		while not self.done:
			for self.event in pg.event.get():
				if self.event.type == pg.QUIT:
					self.done = True
				elif self.event.type == pg.MOUSEBUTTONDOWN:
					self.pos = pg.mouse.get_pos()
					self.col = self.pos[0] // (width + margin)
					self.row = self.pos[1] // (height + margin)

					try: # 돌 중복 및 턴 체크
						if grid[self.row][self.col] == 1 or grid[self.row][self.col] == 2:
							print(grid[self.col][self.row])
							print (self.col, self.row)
							log.warning("location exist")
							continue
						else:
							if (self.cnt % 2 == 0):
								#
								# 돌을 놓을 수 있는곳만 정해주는 함수.
								# 놓았을 경우, 뒤집는거까지 해야한다.
								#
								if not self.check_location(self.col, self.row, self.cnt):
									log.error("unavailable location")
									continue

								self.put_stone(black_stone, self.col, self.row, self.cnt)
								# grid[self.col][self.row] = 1 # <value> black 1 , white 2

							else:
								if not self.check_location(self.col, self.row, self.cnt):
									log.error("unavailable location")
									continue

								self.put_stone(white_stone, self.col, self.row, self.cnt)
								# grid[self.col][self.row] = 2

							self.cnt += 1
						log.debug("col : " + str(self.col) + "\trow : " + str(self.row) + "\tdata : " + str(grid[self.col][self.row]) )

					except IndexError:
						log.error(" out of boundary")

			pg.display.update()
		pg.quit()

	def show_board(self): # 초기 보드판
		for self.row in range(8):
			for self.col in range(8):
				self.init_stone((self.col, self.row))

		self.clock.tick(30)
		pg.display.flip()

	def init_stone(self, location): # 초기 보드판 세팅, (흰 검 검 흰)
		self.location = location

		if (self.location == (3,3) or self.location == (4,4)):
			self.init_put_stone(white_stone, self.location[0], self.location[1])
			grid[self.location[0]][self.location[1]] = 2

		elif (self.location == (3,4) or self.location == (4,3)):
			self.init_put_stone(black_stone, self.location[0], self.location[1])
			grid[self.location[0]][self.location[1]] = 1

		else:
			pg.draw.rect(self.screen, color, [(margin + width) * self.location[0] + margin, (margin + height) * self.location[1] + margin, width, height])

	def init_put_stone(self, obj, col, row): # 돌 놓을때마다 이미지 추가
		self.obj = obj
		self.col = col
		self.row = row

		self.screen.blit(self.obj, ((margin + width) * self.col + margin, (margin + height) * self.row + margin))

	def put_stone(self, obj, col, row, turn): # 돌 놓을때마다 이미지 추가
		self.obj = obj
		self.col = col
		self.row = row
		self.turn = turn

		if self.turn % 2 == 0:
			reverse_stone_list = self.getReversedPosition(grid, 1, self.row, self.col)
			#print(reverse_stone_list)
			for y, x in reverse_stone_list:
				self.screen.blit(self.obj, ((margin + width) * x + margin, (margin + height) * y + margin))
				grid[y][x] = 1
			self.screen.blit(self.obj, ((margin + width) * self.col + margin, (margin + height) * self.row + margin))
			grid[self.row][self.col] = 1

		else:
			reverse_stone_list = self.getReversedPosition(grid, 2, self.row, self.col)
			#print(reverse_stone_list)
			for y, x in reverse_stone_list:
				self.screen.blit(self.obj, ((margin + width) * x + margin, (margin + height) * y + margin))
				print ( y, x)
				grid[y][x] = 2
			self.screen.blit(self.obj, ((margin + width) * self.col + margin, (margin + height) * self.row + margin))
			grid[self.row][self.col] = 2

		print (grid)
		#self.screen.blit(self.obj, ((margin + width) * self.col + margin, (margin + height) * self.row + margin))

	def check_location(self, col, row, turn):
		self.col = col
		self.row = row
		self.turn = turn

		if (self.turn % 2 == 0):
			result = self.getAvailablePosition(grid, 1)
			print(result)
			if not ((self.row, self.col) in result):
				return False
		else:
			result = self.getAvailablePosition(grid, 2)
			print(result)
			if not ((self.row, self.col) in result):
				return False

		return True

	def chkArrow(self, map, color, x, y, direction):  # x,y 좌표로부터 direction방향까지 뒤집힐 수 있는 돌을 반환하는 함수
		result = []
		while True:
			x += direction[0]     #direction 방향으로 이동
			y += direction[1]
			if not ((0 <= x <= 7) and (0 <= y <= 7)): # 맵 바운더리 체크
				break
			nowPositionColor = map[x][y]   # 해당 위치에 있는 돌을 가져옴
			if nowPositionColor == 0:   # 공백이면 반환 x
				break
			if nowPositionColor == color:   # 내 돌 색깔이면 리스트 리턴
				return result
			result.append((x, y))     # 상대 돌 색깔이면 리스트에 삽입
		return []

	def getReversedPosition(self, map, color, x, y):
		self.direction = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]   # 8개의 방향
		result = []
		for dir in self.direction:   # 모든 방향으로 탐색
			tmpList = self.chkArrow(map,color,x,y,dir)   # 해당 방향에 뒤집을 돌 리스트
			if tmpList:  # 뒤집을 돌 있냐?
				result += tmpList  # 리스트 추가
		return result  # 뒤집을 수 있는 돌 모두 리턴

	def getAvailablePosition(self, board, color):
		result = []
		for i, j in zip(*np.where(board == 0)):
			#print (i, j)
			if self.getReversedPosition(board,color,i,j):  # 해당 좌표가 비어있고, 해당 방향에서 뒤집을 수 있는 돌 리스트가 비어있지 않으면
				result.append((int(i),int(j)))  #좌표 기입
		return result  # 착수 가능한 좌표 리스트 반환

if __name__ == "__main__":
	othello = Othello()
	othello.run_game()

	#print(grid)


#!/usr/bin/python
# -*- coding: UTF-8 -*-

from topic import get_topic
from realtimehot import get_realtimehot
from relevantTopic import get_relevant_topic
from sqlite import insert, select
import time

def main():
    key = "南昌大学"
    realtimehot = get_realtimehot()
    topic = get_topic(key)
    users, relevant_topic, words = get_relevant_topic(key)
    print("-------------Get函数结束-------------")
    print("即时热榜"+str(realtimehot))
    print(key+"搜索到的话题"+str(topic))
    print(key+"话题内微博相关话题"+str(relevant_topic))
    print(key+"话题内活跃用户"+str(users))
    print(key+"话题内博文常见关键词"+str(words))
    insert(realtimehot, topic, relevant_topic, users, words)

if __name__ == '__main__':
  while True:
      main()
      time.sleep(60)


from flask import Blueprint, jsonify
from flask_restful import Api
from marshmallow import ValidationError

from src.api.resources import CompanyList, CompanyResource
from src.api.resources import PositionList, PositionResource
from src.api.resources import ReadingRuleResource, ReadingRuleList
from src.api.resources import SensorResource, SensorList
from src.api.resources import ServerList, ServerResource, generate_new_token, generate_file
from src.api.resources import UserResource, UserList
from src.api.resources import ReadDataResource
from .resources import EquipmentList, EquipmentResource
from .resources import reports
from .resources import create_order

blueprint = Blueprint("src", __name__, url_prefix="/api/v1")
api = Api(blueprint)

api.add_resource(UserResource, "/users/<int:user_id>", endpoint="user_by_id")
api.add_resource(UserList, "/users", endpoint="users")

api.add_resource(ServerResource, "/servers/<int:server_id>", endpoint="server_by_id")
api.add_resource(ServerList, '/servers', endpoint='servers')
blueprint.route("/servers/<int:server_id>/refresh", methods=["POST"])(generate_new_token)
blueprint.route("/servers/<int:server_id>/file", methods=["GET"])(generate_file)

api.add_resource(CompanyResource, "/companies/<int:company_id>", endpoint="company_by_id")
api.add_resource(CompanyList, '/companies', endpoint='companies')

api.add_resource(PositionResource, "/positions/<int:position_id>", endpoint="position_by_id")
api.add_resource(PositionList, '/positions', endpoint='positions')

api.add_resource(SensorResource, "/sensors/<int:sensor_id>", endpoint="sensor_by_id")
api.add_resource(SensorList, '/sensors', endpoint='sensors')

api.add_resource(ReadingRuleResource, "/rule/<server_token>", endpoint="reading_rule_by_token")
api.add_resource(ReadingRuleResource, "/rule/<int:rule_id>", endpoint="reading_rule_by_id")
api.add_resource(ReadingRuleList, '/rule', endpoint='reading_rules')

api.add_resource(ReadDataResource, "/data/<int:server_id>", endpoint="read_data_by_id")
api.add_resource(ReadDataResource, "/data/<server_token>", endpoint="read_data_by_token")

api.add_resource(EquipmentResource, "/equipments/<int:equipment_id>", endpoint="equipments_by_id")
api.add_resource(EquipmentList, "/equipments", endpoint="equipments")

blueprint.route("/reports/number_employees_in_companies", methods=["GET"])(reports.number_employees_in_companies)
blueprint.route("/reports/number_employees_in_companies/print", methods=["GET"])(reports.number_employees_in_companies_print)

blueprint.route("/reports/number_servers_in_companies", methods=["GET"])(reports.number_servers_in_companies)
blueprint.route("/reports/number_servers_in_companies/print", methods=["GET"])(reports.number_servers_in_companies_print)

blueprint.route("/reports/count_sensors_on_server", methods=["GET"])(reports.count_sensors_on_server)
blueprint.route("/reports/count_sensors_on_server/print", methods=["GET"])(reports.count_sensors_on_server_print)

blueprint.route("/reports/count_fail_on_server/print", methods=["GET"])(reports.count_fail_on_server)

blueprint.route("/order/create", methods=["POST"])(create_order)

@blueprint.errorhandler(ValidationError)
def handle_marshmallow_error(e):
    return jsonify({'error': e.messages}), 400


from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet as wn
from nltk.corpus import sentiwordnet as swn
from nltk import sent_tokenize, word_tokenize, pos_tag
from sklearn import metrics
from sklearn.model_selection import train_test_split

from TSA.Preproc import Preproc
lemmatizer = WordNetLemmatizer()

def convert_tag(tag):
    if tag.startswith("J"):
        return wn.ADJ
    elif tag.startswith("N"):
        return wn.NOUN
    elif tag.startswith("R"):
        return wn.ADV
    elif tag.startswith("V"):
        return wn.VERB
    return None

def clean_text(filepath, filename):
    pp = Preproc.Preproc()
    pp.loadCsv(filepath, filename)
    pp.remove_html_encode()
    return pp.get_twitter_df()

def swn_classifier(tweet):
    sentiment = 0.0
    tokens_count = 0

    sentinized_tweet = sent_tokenize(tweet)

    for sentence in sentinized_tweet:
        #print(sentence)
        # Part of Speach tag
        tagged_tweet = pos_tag(word_tokenize(sentence))

        for word, tag in tagged_tweet:
            wn_tag = convert_tag(tag)
            if wn_tag not in (wn.NOUN, wn.ADJ, wn.ADV):
                continue

            lemma = lemmatizer.lemmatize(word, pos=wn_tag)
            if not lemma:
                continue

            synset = wn.synsets(lemma, pos=wn_tag)
            # If not a synonym skip this word.
            if not synset:
                continue

            synset = synset[0]
            swn_synset = swn.senti_synset(synset.name())

            #print(synset)
            #print(swn_synset)

            sentiment += swn_synset.pos_score() - swn_synset.neg_score()
            tokens_count += 1

    # if not word match the lexicon default to negative
    if not tokens_count:
        return 0

    if sentiment >= 0:
        return 1

    # negative sentiment.
    return 0

def main():
    # for STS dataset
    # df = clean_text("TSA/datasets/STS/", "STS.csv")

    # for SemEval dataset
    df = clean_text("TSA/datasets/SemEval/4A-English/", "SemEval.csv")

    # split data into test 80% train, 20%
    X_train, X_test, y_train, y_test = train_test_split(df.tweet, df.lable, test_size=0.2, random_state=0)
    pred_y = [swn_classifier(tweet) for tweet in X_test]
    target_names = ['Positive', 'Negative']
    print(metrics.classification_report(y_test, pred_y, target_names=target_names, digits=3))


import numpy as np
import pandas
import r_squared
import scipy as sp
from ggplot import *

df = pandas.read_csv('/Users/aha/Documents/workspace/udacity/p1/turnstile_weather_v2.csv')
stationNames = pandas.unique(df['station'])
countName = 'ENTRIESn_hourly'
stationName = 'station'
colNames =[stationName,countName,'longitude','latitude']

rainy = df[df.rain == 1][colNames]
notRainy = df[df.rain == 0][colNames]

columns = ['index','meanDif','latitude','longitude','station']
finalVar = pandas.DataFrame(columns=columns)
f = open('/Users/aha/Documents/workspace/udacity/p1/furtherAnalysis.txt', 'w')

i = 0
for var in stationNames:
	notRainyStation = notRainy[notRainy.station==var]
	rainyStation = rainy[rainy.station==var]

	notRainyAr=notRainyStation[countName]
	rainyAr=rainyStation[countName]

	meanRainy =  np.mean(rainyAr)
	meanNotRainy =  np.mean(notRainyAr)
	meanDif = meanRainy - meanNotRainy
	latitude = notRainyStation['latitude'].head(1)
	longitude = notRainyStation['longitude'].head(1)

	dfOb = {}
	dfOb['index'] = i
	dfOb['meanDif'] = 0
	dfOb['latitude'] = latitude
	dfOb['longitude'] = longitude
	dfOb['station'] = var
	dfOb['meanDif'] = meanDif

	i=+1

	U,p ="",""
	if len(notRainyAr) > 20 and len(rainyAr) > 20 and len(set(notRainyAr)) >1 and len(set(rainyAr))>1:
		U,p =sp.stats.mannwhitneyu(rainyAr,notRainyAr)
		# finalVar[var] = (str(U) + " " + str(p) + ' rainMean: '+ str(meanRainy) +' meanNotRainy: '+ str(meanNotRainy))
		# f.write(var +" "+finalVar[var]+ '\n')
		if p < 0.05:
			print(var + " " + str(p))
	else:
		print(var + "\n")
		dfOut = df[df.station == var]
		# dfOut.to_csv('/Users/aha/Documents/workspace/udacity/p1/'+var+'Excluded.csv')

	dfVar = pandas.DataFrame(dfOb)
	finalVar = finalVar.append(dfVar)
# print(finalVar)
#pandas.DataFrame(stationNames)
minMean = finalVar['meanDif'].min()
maxMean = finalVar['meanDif'].max()
# col = []
# for var in stationNames:
# 	temp = finalVar[finalVar.station==var]
# 	col.append(rgb(minMean,maxMean,temp['meanDif']))

# finalVar['rgb'] = Series(col, index=finalVar.index)
# print(col)
# print(finalVar)

plot1 = ggplot(aes(x='latitude', y='longitude', colour='meanDif'), data=finalVar) + \
    geom_point() + scale_colour_gradient(low = "red", high="green")

# ggplot(finalVar, aes('latitude','longitude')) \
# + ggtitle('Subways riders on non-rainy days') \
# + labs("ENTRIESn_hourly", "Frequency") \
# + geom_point(aes(colour=factor('meanDif'))) + scale_colour_gradient(limits=[minMean,maxMean], low="red")
print(plot1)


#!/usr/bin/env python
from aoc import get_input

def main():
    with get_input(__file__) as ifile:
        groups = [
            [set(q) for q in group.split()]
            for group in ifile.read().split('\n\n')
            ]

    print(sum(len(set.union(*group)) for group in groups)) # 1
    print(sum(len(set.intersection(*group)) for group in groups)) # 2

if __name__ == '__main__':
    main()


# ----- Sound card class -----------------------------------------------------------------------------------------------

import sounddevice as sd
import numpy as np
import scipy.signal as sg

class AudioCard:

    def __init__(self):

        self.device_list = sd.query_devices()                             # List the supported devices.
        self.stop = sd.stop()
        self.duration = None
        self.fs = None
        self.frequency = None
        self.phase = None
        self.waveform = None
        self.loop = None
        self.amplitude = None

    def record(self, fs, duration):                                       # Enter sampling frequency (fs) and duration.

        self.fs = fs
        self.duration = duration

        time = np.linspace(start=0, stop=duration, num=duration*fs)
        recording = sd.rec(frames=int(self.duration * self.fs),
                           samplerate=fs,
                           channels=2,
                           blocking=False,
                           dtype='float32')
        return time, recording

    def playback(self, fs, duration, amplitude, frequency, phase, waveform, loop):
        # loop: boolean; waveform: SIN/SQR/RAMP/PULSE.
        self.fs = fs
        self.duration = duration
        self.amplitude = amplitude
        self.frequency = frequency
        self.phase = phase
        self.waveform = waveform
        self.loop = loop

        # Set the input numpy.array to reproduce.
        time = np.linspace(start=0, stop=duration, num=duration*fs)
        sound = None

        if waveform == 'SIN':
            sound = amplitude * np.sin(2 * np.pi * frequency * time + phase)
        elif waveform == 'SQR':
            sound = amplitude * sg.square(2 * np.pi * frequency * time + phase, 0.5)
        elif waveform == 'RAMP':
            sound = amplitude * sg.sawtooth(2 * np.pi * frequency * time + phase, 0.5)
        elif waveform == 'PULSE':
            sound = amplitude * sg.unit_impulse(shape=np.size(time), idx='mid')

        sd.play(data=sound,
                samplerate=fs,
                blocking=False,
                loop=loop)

        return time, sound

    def playback_record(self, fs, duration, amplitude, frequency, phase, waveform, loop):

        self.fs = fs
        self.duration = duration
        self.amplitude = amplitude
        self.frequency = frequency
        self.phase = phase
        self.waveform = waveform
        self.loop = loop

        # Set the input numpy.array to reproduce
        time = np.linspace(start=0, stop=duration, num=duration*fs)
        sound = None

        if waveform == 'SIN':
            sound = amplitude * np.sin(2 * np.pi * frequency * time + phase)
        elif waveform == 'SQR':
            sound = amplitude * sg.square(2 * np.pi * frequency * time + phase, 0.5)
        elif waveform == 'RAMP':
            sound = amplitude * sg.sawtooth(2 * np.pi * frequency * time + phase, 0.5)
        elif waveform == 'PULSE':
            sound = amplitude * sg.unit_impulse(shape=np.size(time), idx='mid')

        recording = sd.playrec(data=sound,
                               samplerate=fs,
                               blocking=False)  # If False, return immediately (but continue playrec in the background).

        return time, sound, recording


from flask import Flask, render_template, request, redirect, url_for, flash
from functions import get_db_connection, get_item

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your secret key'

@app.route('/')
def stock():
    conn = get_db_connection()
    stock = conn.execute('SELECT * FROM stock ORDER BY lokasyon').fetchall()
    conn.close()
    return render_template('stock.html', stock=stock)

@app.route('/<int:id>/edit_stock', methods=('GET', 'POST'))
def edit_stock(id):
    item = get_item(id)
    if request.method == 'POST':
        conn = get_db_connection()
        malzeme = request.form['malzeme']
        malzeme = malzeme.split()
        if len(malzeme) < 4:
            flash('Malzeme is required!')
        else:
            adet = malzeme[1]
            birim = malzeme[2]
            lokasyon = malzeme[3]
            malzeme = malzeme[0]
            conn.execute('UPDATE stock SET malzeme = ?, adet = ?, birim = ?, lokasyon = ?'
                         'WHERE id = ?', (malzeme, adet, birim, lokasyon, id))
        conn.commit()
        conn.close()
        return redirect(url_for('stock'))
    return render_template('edit_stock_adet.html', item=item)

@app.route('/ticket')
def ticket():
    conn = get_db_connection()
    tickets = conn.execute('SELECT * FROM shoppingTicket ORDER BY tarih DESC')
    return render_template('ticket.html', shoppingTicket=tickets)

@app.route('/<int:id>/delete_ticket', methods=('POST',))
def delete_ticket(id):
    conn = get_db_connection()
    conn.execute('DELETE FROM shoppingTicket WHERE id = ?', (id,))
    conn.commit()
    conn.close()
    return redirect(url_for('ticket'))

@app.route('/wish_list', methods=('GET', 'POST'))
def wish_list():
    conn = get_db_connection()
    wish = conn.execute('SELECT * FROM wish_list').fetchall()
    if request.method == 'POST':
        print('methode oke')
        malzeme = request.form['malzeme']
        malzeme = malzeme.split()
        if len(malzeme) < 4:
            flash('Malzeme is required!')
        else:
            adet = malzeme[1]
            birim = malzeme[2]
            lokasyon = malzeme[3]
            malzeme = malzeme[0]
            conn.execute('INSERT INTO wish_list (malzeme, adet, birim, lokasyon) VALUES (?, ?, ?, ?)',
                         (malzeme, adet, birim, lokasyon))
        conn.commit()
        conn.close()
        return redirect(url_for('wish_list'))
    return render_template('wish_list.html', wish_list=wish)

@app.route('/<int:id>/delete_wish', methods=('POST',))
def delete_wish(id):
    conn = get_db_connection()
    conn.execute('DELETE FROM wish_list WHERE id = ?', (id,))
    conn.commit()
    conn.close()
    return redirect(url_for('wish_list'))

@app.route('/<int:id>/delete_item', methods=('POST',))
def delete_item(id):
    conn = get_db_connection()
    conn.execute('DELETE FROM stock WHERE id = ?', (id,))
    conn.commit()
    conn.close()
    return redirect(url_for('stock'))

@app.route('/<int:id>/add_stock', methods=('GET', 'POST'))
def add_stock(id):
    conn = get_db_connection()
    compare = conn.execute(
        'SELECT s.malzeme, s.lokasyon, s.adet, wish_list.malzeme, wish_list.lokasyon, wish_list.adet '
        'FROM wish_list '
        'INNER JOIN stock s  '
        'ON wish_list.malzeme = s.malzeme AND wish_list.lokasyon = s.lokasyon;')
    if compare:
        s_adet = conn.execute('SELECT '
                              's.adet FROM wish_list INNER JOIN stock s '
                              'ON wish_list.malzeme = s.malzeme AND wish_list.lokasyon = s.lokasyon;')
        wl_adet = conn.execute('SELECT '
                               'wish_list.adet FROM wish_list INNER JOIN stock s '
                               'ON wish_list.malzeme = s.malzeme AND wish_list.lokasyon = s.lokasyon;')
        conn.execute('UPDATE stock SET adet = ?', (s_adet + wl_adet,))
    else:
        conn.execute('INSERT INTO stock SELECT * FROM wish_list WHERE id = ?', (id,))
        conn.execute('INSERT INTO shoppingTicket SELECT * FROM wish_list WHERE id = ?', (id,))
        conn.execute('DELETE FROM wish_list WHERE id = ?', (id,))
    conn.commit()
    return redirect(url_for('wish_list'))

if __name__ == '__main__':
    app.run()


import argparse
import json
import os
import os.path as osp
import pickle
import random
import sys
from argparse import Namespace

import numpy as np

import cv2
import scipy.misc
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
from torch.utils import data

import scops_trainer
from dataset.dataset_factory import dataset_generator
from model.model_factory import model_generator

# solve potential deadlock https://github.com/pytorch/pytorch/issues/1355
cv2.setNumThreads(0)

EXP_NAME = 'SCOPS-Test'
MODEL = 'DeepLab'
BATCH_SIZE = 10
NUM_WORKERS = 4
DATASET = 'PASCAL'
DATA_DIRECTORY = ''
DATA_LIST_PATH = ''
INPUT_SIZE = '128,128'
LEARNING_RATE = 1e-5
MOMENTUM = 0.9
NUM_CLASSES = 6
NUM_STEPS = 2000
POWER = 0.9
RANDOM_SEED = 1234
RESTORE_FROM = None
SAVE_NUM_IMAGES = 2
SAVE_PRED_EVERY = 5000
SNAPSHOT_DIR = './snapshots/'
WEIGHT_DECAY = 0.0005
CLIP_GRAD_NORM = 5
VIS_TNTERVAL = 100

TPS_SIGMA = 0.01
RAND_SCALE_LOW = 0.7
RAND_SCALE_HIGH = 1.1

NUM_PARTS = 10

LAMBDA_CON = 1e-1
LAMBDA_EQV = 10.0
LAMBDA_SC = 0.1

def get_arguments():
    """Parse all the arguments provided from the CLI.

    Returns:
      A list of parsed arguments.
    """
    parser = argparse.ArgumentParser(
        description="SCOPS: Self-supervised Co-part Segmentation")

    # load args from json files
    parser.add_argument("-f", "--arg-file", type=str, default=None,
                        help="load args from json file")

    # Exp
    parser.add_argument("--exp-name", type=str, default=MODEL,
                        help="Experiment name. Default: Part-Test")
    # Model/Data description
    parser.add_argument("--model", type=str, default=MODEL,
                        help="available options : DeepLab/DeepLab_2branch")
    parser.add_argument("--num-classes", type=int, default=NUM_CLASSES,
                        help="Number of classes to predict (including background).")
    parser.add_argument("--num-workers", type=int, default=NUM_WORKERS,
                        help="number of workers for multithread dataloading.")
    parser.add_argument("--dataset", type=str, default=DATASET,
                        help="Dataset selection")
    parser.add_argument("--data-dir", type=str, default=DATA_DIRECTORY,
                        help="Path to the directory containing the PASCAL VOC dataset.")
    parser.add_argument("--data-list", type=str, default=DATA_LIST_PATH,
                        help="Path to the file listing the images in the dataset.")
    parser.add_argument("--input-size", type=str, default=INPUT_SIZE,
                        help="Comma-separated string with height and width of images.")
    parser.add_argument("--celeba-th", type=float, default=0.0,
                        help="iou_threshold for celebAWild")

    # Data Augmentation
    parser.add_argument("--random-mirror", action="store_true",
                        help="Whether to randomly mirror the inputs during the training.")
    parser.add_argument("--random-scale", action="store_true",
                        help="Whether to randomly scale the inputs during the training.")
    parser.add_argument("--random-seed", type=int, default=RANDOM_SEED,
                        help="Random seed to have reproducible results.")

    # Training hyper parameters
    parser.add_argument("--num-steps", type=int, default=NUM_STEPS,
                        help="Number of training steps.")
    parser.add_argument("--batch-size", type=int, default=BATCH_SIZE,
                        help="Number of images sent to the network in one step.")
    parser.add_argument("--learning-rate", type=float, default=LEARNING_RATE,
                        help="Base learning rate for training with polynomial decay.")
    parser.add_argument("--power", type=float, default=POWER,
                        help="Decay parameter to compute the learning rate.")
    parser.add_argument("--momentum", type=float, default=MOMENTUM,
                        help="Momentum component of the optimiser.")
    parser.add_argument("--weight-decay", type=float, default=WEIGHT_DECAY,
                        help="Regularisation parameter for L2-loss.")
    parser.add_argument("--clip-gradients", type=float, default=CLIP_GRAD_NORM,
                        help="Clip gradients norm. Default:5.")

    # constraints weighting
    parser.add_argument("--lambda-con", type=float, default=LAMBDA_CON,
                        help="weighting parameter for concentration")
    parser.add_argument("--lambda-eqv", type=float, default=LAMBDA_EQV,
                        help="weighting parameter for equivariance")
    parser.add_argument("--lambda-lmeqv", type=float, default=LAMBDA_EQV,
                        help="weighting parameter for equivariance")

    # Equivariance setting
    parser.add_argument("--tps-mode", type=str, default='affine',
                        help="tps mode: affine/projective")
    parser.add_argument("--tps-sigma", type=float, default=TPS_SIGMA,
                        help="peturbation of tps in equivariance loss")
    parser.add_argument("--eqv-random-mirror", action="store_true",
                        help="Whether to use random mirror in equvariance.")
    parser.add_argument("--eqv-border-padding", action="store_true",
                        help="Whether to use border padding in equvariance.")
    parser.add_argument("--random-scale-low", type=float, default=RAND_SCALE_LOW,
                        help="lower bound of random scaling.")
    parser.add_argument("--random-scale-high", type=float, default=RAND_SCALE_HIGH,
                        help="higher bound of random scaling.")

    # part training config
    parser.add_argument("--ignore-saliency-fg", action="store_true",
                        help="Whether to ignore saliency foreground and only enforce BG")
    parser.add_argument("--ignore-small-parts", action="store_true",
                        help="Whether to ignore small parts.")
    parser.add_argument("--center-crop", action="store_true",
                        help="Whether to crop center (MAFL).")
    parser.add_argument("--self-ref-coord", action="store_true",
                        help="Whether to use self-referenced centroid.")
    parser.add_argument("--kp-threshold", type=int, default=6,
                        help="maximum number of missing keypoints/landmarks")

    # Semantic Consistency config
    parser.add_argument("--num-parts", type=int, default=NUM_PARTS,
                        help="Number of parts")
    parser.add_argument("--lambda-sc", type=float, default=LAMBDA_SC,
                        help="weighting parameter for semantic consistency constraint")
    parser.add_argument("--learning-rate-w", type=float, default=1e-3,
                        help="learning rate for DFF basis")
    parser.add_argument("--ref-net", type=str, default='vgg19',
                        help="reference feature network. default: vgg19")
    parser.add_argument("--ref-layer", type=str, default='relu5_4',
                        help="default: vgg19")
    parser.add_argument("--ref-norm", action="store_true",
                        help="normalize reference feature map")
    parser.add_argument("--lambda-orthonamal", type=float, default=1e2,
                        help="weighting parameter for DFF orthonormal loss")

    parser.add_argument("--detach-k", action="store_true",
                        help="detach k")
    parser.add_argument("--no-sal-masking", action="store_true",
                        help="disable saliency constraint")

    parser.add_argument("--restore-part-basis", type=str, default='',
                        help="load part basis weights")

    # Save/visualization
    parser.add_argument("--restore-from", type=str, default=RESTORE_FROM,
                        help="Where restore model parameters from.")
    parser.add_argument("--save-pred-every", type=int, default=SAVE_PRED_EVERY,
                        help="Save summaries and checkpoint every often.")
    parser.add_argument("--snapshot-dir", type=str, default=SNAPSHOT_DIR,
                        help="Where to save snapshots of the model.")
    parser.add_argument("--vis-interval", type=int, default=VIS_TNTERVAL,
                        help="visualization interval.")
    parser.add_argument("--tb-dir", type=str, default='tb_logs',
                        help="tensorbard dir.")

    parser.add_argument("--gpu", type=int, default=0,
                        help="choose gpu device.")
    return parser.parse_args()

def main():

    args = get_arguments()
    args_dict = vars(args)

    if args.arg_file is not None:
        with open(args.arg_file, 'r') as f:
            arg_str = f.read()
            file_args = json.loads(arg_str)
            args_dict.update(file_args)
            args = Namespace(**args_dict)

    args_str = '{}'.format(json.dumps(vars(args), sort_keys=False, indent=4))
    print(args_str)

    if not os.path.exists(os.path.join(args.snapshot_dir, args.exp_name)):
        os.makedirs(os.path.join(args.snapshot_dir, args.exp_name))

    # save args to file
    with open(os.path.join(args.snapshot_dir, args.exp_name, 'exp_args.json'), 'w') as f:
        print(args_str, file=f)

    h, w = map(int, args.input_size.split(','))
    input_size = (h, w)
    args.input_size = input_size

    cudnn.enabled = True
    gpu = args.gpu

    # create network
    model = model_generator(args)
    model.train()
    model.cuda(args.gpu)
    cudnn.benchmark = True

    # Initialize SCOPS trainer
    trainer = scops_trainer.SCOPSTrainer(args, model)

    train_dataset = dataset_generator(args)
    trainloader = data.DataLoader(train_dataset,
                                  batch_size=args.batch_size,
                                  shuffle=True,
                                  num_workers=args.num_workers,
                                  pin_memory=True,
                                  drop_last=True)
    trainloader_iter = enumerate(trainloader)

    for i_iter in range(args.num_steps):

        try:
            _, batch = trainloader_iter.__next__()
        except:
            trainloader_iter = enumerate(trainloader)
            _, batch = trainloader_iter.__next__()

        trainer.step(batch, i_iter)

        if i_iter >= args.num_steps - 1:
            print('save model ...')
            torch.save(model.state_dict(), osp.join(args.snapshot_dir,
                                                    args.exp_name, 'model_' + str(args.num_steps) + '.pth'))
            break

        if i_iter % args.save_pred_every == 0 and i_iter != 0:
            print('taking snapshot ...')
            torch.save(model.state_dict(), osp.join(args.snapshot_dir,
                                                    args.exp_name, 'model_' + str(i_iter) + '.pth'))

if __name__ == '__main__':
    main()