def 第10章():
    print('\n\n第10章')

    # 10章で使うファイルの作成
    fout = open('Oops.txt', 'wt')
    print('Oops, I created a file.', file=fout)
    fout.close()
    print('Oops.txt作成')

    print('exists()によるファイルが存在することのチェック')
    import os
    # trueを返す
    os.path.exists('Oops.txt')
    # trueを返す
    os.path.exists('./Oops.txt')
    # falseを返す
    os.path.exists('waffles')
    # trueを返す
    os.path.exists('..')  # ドット１個はカレントディレクトリを表すため必ずtrueになる
    # trueを返す
    os.path.exists('../..')  # ドット2個は親ディレクトリを表すため必ずtrueになる

    print('ファイルタイプのチェック')
    # ファイルかチェック
    name = 'Oops.txt'
    os.path.isfile(name)  # trueを返す
    # ディレクトリかチェック
    os.path.isdir(name)  # falseを返す

    # isabs()は引数が絶対パスかどうか返してくる。引数は存在するファイルでなくても構わない。
    os.path.isabs(name)  # false
    os.path.isabs('/big/fake/name')  # true
    os.path.isabs('big/fake/name/without/a/leading/slash')  # false

    print('copy()によるコピー')
    import shutil
    shutil.copy('Oops.txt', 'ohno.txt')
    print('Oops.txtをohno.txtにコピーする')

    # 　↓　unixコマンドのためやらない
    """ print('rename()によるファイル名の変更')
    import os
    os.rename('ohno.txt', 'ohwell.txt') #ohno.txtのファイル名をohwell.txtに変更
    """

    """print('ハードリンクの作成')
    os.link('Oops.txt', 'yikes.txt')
    os.path.isfile('yikes.txt')
    """

    """
    print('シンボリックリンクの作成')
    os.symlink('Oops.txt', 'yikes.txt')
    os.path.islink('jeeper.txt')
    """
    # 　↑　unixコマンドのためやらない

    # abspath()によるパス名の取得、相対パスを絶対パスにして返す
    print(os.path.abspath('Oops.txt'))

    # remove()によるファイルの削除
    os.remove('Oops.txt')
    print(os.path.exists('Oops.txt'))  # 削除を確認 falseを返す

def 第10章区切り():
    import os
    import shutil

    # 実行するときに前回のフォルダが存在していたらツリーごと消す
    if os.path.exists('../poems'):
        shutil.rmtree('../poems/')

    # mkdirによるディレクトリの作成
    os.mkdir('../poems')
    print(os.path.exists('../poems'))

    # rmdir()によるディレクトリの削除
    os.rmdir('../poems')
    print(os.path.exists('../poems'))

    # listdir()による内容リストの作成
    os.mkdir('../poems')
    os.listdir('../poems')  # ディレクトリに含まれる内容を取得
    os.mkdir('../poems/mcintype')  # サブディレクトリの作成
    os.listdir('../poems')

    fout = open('../poems/mcintype/the_good_man', 'wt')
    fout.write('''Cheerful and happy was his mood,
    He to the poor was kind and good,
    And he oft' times did find them food
    ''')
    fout.close()
    print(os.listdir('../poems/mcintype'))  # printで作ったファイルの確認をする

    # chdir()によるカレントディレクトリの変更
    os.chdir('../poems')
    print(os.listdir('..'))  # printでカレントディレクトリの変更を確認する

    # glob()によるパターンにマッチするファイルのリストの作成
    import glob
    print(glob.glob('m*'))  # print
    print(glob.glob('??'))  # print

    # 10.3 プログラムとプロセス
    import os
    print(os.getpid())  # インタープリタのID
    print(os.getcwd())  # インタープリタのカレントディレクトリ
    # print(os.getuid())#自分のユーザーID
    # print(os.getgid())#自分のグループID

    #unixのため
    '''import subprocess
    ret = subprocess.getoutput('date')#dateプログラムの出力(unix)
    ret'''

    #↓なぜかうまくいかない
    ''' # multiprocessingによるプロセスの作成
    import multiprocessing
    import os

    def do_this(what):
        whoami(what)

    def whoami(what):
        print("Process %s says: %s" % (os.getpid(), what))

    if __name__ == '__main__':
        whoami("I'm the main program")
        for n in range(4):
            p = multiprocessing.Process(target=do_this, args=("I'm function %s" % n,))
            p.start()'''


import sys
import MDAnalysis as mda
import numpy as np

infile = sys.argv[1] #GRO file
outtop = sys.argv[2] #FULL file name TOP
new = mda.Universe(infile)

# Count the number of each molecule.
numDBPC = new.select_atoms("resname DBPC")
numDPPC = new.select_atoms("resname DPPC")
numW = new.select_atoms("resname W")

#Account for the number of atoms in each molecule
ndb = len(np.unique(numDBPC.resids))
ndp = len(np.unique(numDPPC.resids))
nwat = len(np.unique(numW.resids))

#Comment out water if there isn't any
comm = ''
if nwat == 0:
    comm = comm + ';'

top = open(outtop, 'w')
top.write('#include "top-martini-v2.1.itp"'+ '\n'
           '#include "top-dppc-posre-all.itp"'+'\n'
           '#include "top-dbpc-posre-all.itp"'+'\n'
           '\n'
          '[ system ]' +'\n'
          'ALL RESTRAINED MIXED BILAYER'+'\n'
          '\n'
          '[ molecules ]'+'\n'
          'DBPC %s' % ndb +'\n'
          'DPPC %s' % ndp +'\n'
          '%sW %s' % (comm, nwat) +'\n')
top.close()


from MotionDetector_Video_Bokeh import df #This will call the MotionDetector Programs
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool, ColumnDataSource

#Since ColumnDataSource can't transalate Start and End column from df to datetime format we convert is manually.

df["Start_string"]=df["Start"].dt.strftime("%Y-%m-%d %H:%M:%S")
df["End_string"]=df["End"].dt.strftime("%Y-%m-%d %H:%M:%S")
#Convereted Start and END column values to datetime format Year Month Day Hour Min Sec no we will supply it to the plot

cds=ColumnDataSource(df) #Creates cds object specifyng the datasource i.e. pandas dataframe for Columns to be used in bokeh plotting

p=figure(x_axis_type="datetime", width = 500, height=100, responsive=True, title="Motion_Capture_Graph")
#x axis data type is set to datetime
p.yaxis.minor_tick_line_color = None #To remove minor ticks from y axis
p.ygrid[0].ticker.desired_num_ticks=1 #To remove horizontal y axis grids from plot.
#ygrid is a list and we have to access first object in the grid and remove the lines byt settng it to 1

#hover = HoverTool(tooltips=[("Start","@Start"),("End", "@End")]) specify the hover text with array of values with @
hover = HoverTool(tooltips=[("Start","@Start_string"),("End","@End_string")]) #Specifying the date tome converted df column values
p.add_tools(hover)
#q=p.quad(left=df["Start"] ,right=df["End"], bottom=0, top=1, color="green", source=cds)
#left, right, bottom, top are the values for the quadrant to be drawn
#df["Start"] is the start time of object entry from the MotionDetector_Video_Bokeh py file
#df["End"] is the exit time of object from the MotionDetector_Video_Bokeh py file

q= p.quad(left="Start",right="End",bottom=0, top=1, color="green", source=cds )
#Here we are using cds as the column data source which is fetched from dataframe in Montion_Detetctor_video_bokeh
#Instead of df["Start"]/df["End"] we can/have to use the actual column name itself from the dataframe

output_file("Motion_Graph.html")
show(p)


import sh
from typing import List, Tuple, Union, Iterable
import signal
import subprocess

def command(pathes: Iterable[str]):
    try:
        cmd = [c for c in pathes if sh.which(c)][0]
        return sh.Command(cmd)
    except IndexError:
        raise sh.CommandNotFound(", ".join(pathes))

def run_commands(*commands: Union[List[str], Tuple[str]],
                 _out=None, _err=None):
    ps = []
    try:
        ps = [sh.Command(c[0])(c[1:],
                               _bg=True, _bg_exc=False,
                               _out=_out, _err=_err)
              for c in commands]
        for p in ps:
            try:
                p.wait()
            except sh.ErrorReturnCode:
                pass
    except KeyboardInterrupt as e:
        for p in ps:
            alive, _ = p.process.is_alive()
            if alive:
                print("结束运行进程：%s" % p.ran)
                p.process.signal(signal.SIGINT)

    return ps

def exec_command_by_subprocess(cmd: str, args: List[str]):
    p = subprocess.Popen([cmd] + args)
    try:
        p.wait()
    except KeyboardInterrupt as e:
        p.send_signal(signal.SIGINT)
        p.wait()
        raise e


from index_lib import index_records
import timeit

def cli():
    print ("Hello Tony")

    start = timeit.timeit()
    for path in range (31,46):
        for row in range (26,38):
            for year in range(2013,2019+1):
                print(path, row, year)
                index_records(path,row,year)
    end = timeit.timeit()
    print("TIMED = ", end - start)

if __name__ == '__main__':
    cli()


#!/bin/python

#from filtering import PPFilterDetectionEfficiencyThreshold

from ..readOif import readOif
from ..PPFilterDetectionEfficiencyThreshold import PPFilterDetectionEfficiencyThreshold

"""
test_detectionEfficencyThreshold.py

Input:  1. file 'oiftrestoutput'

Action: 1. count lines from raw output
        2. count lines after applying filter
        3. see if threshold is statistically solid

Author: Grigori Fedorets
"""

def test_filterDetectionEfficiencyThreshold():

    padafr=readOif('./data/test/oiftestoutput')
    nrows=len(padafr.index)
    pada1=PPFilterDetectionEfficiencyThreshold(padafr,1.00)
    nr1=len(pada1.index)
    pada2=PPFilterDetectionEfficiencyThreshold(padafr,0.00)
    nr2=len(pada2.index)

    assert nr1 == nrows
    assert nr2 == 0


import xarray as xr
import cdsapi
import pandas as pd

c = cdsapi.Client()

years = [
            '1979','1980','1981',
            '1982','1983','1984',
            '1985','1986','1987',
            '1988','1989','1990',
            '1991','1992','1993',
            '1994','1995','1996',
            '1997','1998','1999',
            '2000','2001','2002',
            '2003','2004','2005',
            '2006','2007','2008',
            '2009','2010','2011',
            '2012','2013','2014',
            '2015','2016','2017',
            '2018'
        ]

year_stor = []

for year in years:

    ####################################
    c.retrieve(
    'reanalysis-era5-single-levels',
    {
        'product_type':'reanalysis',
        'variable':'total_column_water',
        'year':year,
        'month':[
            '01','02','03',
            '10','11','12'
        ],
        'day':[
            '01','02','03',
            '04','05','06',
            '07','08','09',
            '10','11','12',
            '13','14','15',
            '16','17','18',
            '19','20','21',
            '22','23','24',
            '25','26','27',
            '28','29','30',
            '31'
        ],
        'time':[
            '00:00','01:00','02:00',
            '03:00','04:00','05:00',
            '06:00','07:00','08:00',
            '09:00','10:00','11:00',
            '12:00','13:00','14:00',
            '15:00','16:00','17:00',
            '18:00','19:00','20:00',
            '21:00','22:00','23:00'
        ],
        'format':'netcdf'
    },
        'year_meta.nc')
    ####################################

    c.retrieve(
    'reanalysis-era5-single-levels',
    {
        'product_type':'reanalysis',
        'variable':'total_column_water',
        'year':str(int(year) + 1),
        'month':'01',
        'day':'01',
        'time':[
            '00:00','01:00','02:00',
            '03:00','04:00','05:00',
            '06:00','07:00','08:00'
        ],
        'format':'netcdf'
    },
    'additional_meta.nc')

    ####################################

    # The first dat set is the year in question

    # The second data set is the first 8 hours of the next year. This is because I am taking the 9am to
    # 9am average. This means I need the first 8 hours e.g 00:00 to 08:00.
    data = xr.open_dataset('year_meta.nc')
    data_2 = xr.open_dataset('additional_meta.nc')

    data = data.combine_first(data_2)

    #####
    #Only need the region for Australia, these below are the bounds that I have for AWAP
    data = data.sel(latitude = slice(-10, -23), longitude = slice(112, 156.25))

    # Subsetting the time and subtracting 9 hours so that I am getting the 9am to 9am mean
    end = data.time.values[-1] # This is only needed as I need to specify and end
    # Selecting the first 9am, this ensure that I don't go back into the previos year
    data = data.sel(time = slice(year + '-01-01T09:00:00.000000000',end))
    data['time'] = data.time.values - pd.to_timedelta('9h')

    # This gets the mean of all days
    data = data.resample('D', dim = 'time', how = 'mean')

    # Dropping anything that has gone outside the year in question
    data = data.where(data.time.dt.year == year, drop = True)

    # Storing the results so I can later combine_firest into an xarray file
    year_stor.append(data)

####################################3

for i in range(len(year_stor)):
    if i == 0:
        era5 = year_stor[i]
    else:
        era5.combine_first(year_stor[i])

era5.to_netcdf('/home/student.unimelb.edu.au/aborowiak/Desktop/Code/Scripts/big_files/era_5.nc')


import random
import sys
from math import exp

def readData(genoFile, snpFile, indFile):
    """
    Function for creating the relevant data types
    """
    snpData, snpDataCols = open(snpFile).read().split(), []
    for index in range(len(snpData) // 6):
        snpDataCols.append(snpData[6 * index: 6 * index + 6])
    genoData = open(genoFile)
    genoData = genoData.read().split()
    indData, indDataCols = open(indFile).read().split(), []
    for index in range(len(indData) // 3):
        indDataCols.append(indData[3 * index: 3 * index + 3])
    return {"ind": indDataCols, "geno": genoData, "snp": snpDataCols}

def initializeState(numInd, alpha, geno, numA, numB):
    """
    Initializes populations, assigning each admixed individual to an individual
    from a sample population based on the admixture proportion.
    """
    usedInd, freeInd, newGeno = {}, {'A': [x for x in range(numA)], 'B': [x for x in range(numB)]}, ""
    for ind in range(numInd):
        chosenPop = 'A' if random.uniform(0, 1) < alpha else 'B'
        random.shuffle(freeInd[chosenPop])
        sampleInd = freeInd[chosenPop].pop()
        usedInd[ind] = {'pop': chosenPop, 'indNum': sampleInd}
        newGeno += geno[chosenPop][0][sampleInd]
    return newGeno, usedInd, freeInd

def swapSample(usedInd, freeInd, ind, geno, currPos, alpha):
    chosenPop = 'A' if random.uniform(0, 1) < alpha else 'B'
    prevAncestor = usedInd[ind]
    random.shuffle(freeInd[chosenPop])
    newInd = freeInd[chosenPop].pop()
    usedInd[ind] = {'pop': chosenPop, 'indNum': ind}
    freeInd[prevAncestor['pop']].append(prevAncestor['indNum'])
    return geno[chosenPop][currPos][newInd], usedInd, freeInd

def haploidToDiploid(haploidGeno):
    diploidGeno = ''
    for index in range(0, len(haploidGeno), 2):
        currInd, nextInd = int(haploidGeno[index]), int(haploidGeno[index + 1])
        diploidInd = str(currInd + nextInd)
        diploidGeno += diploidInd
    return diploidGeno

def writeSnp(snp, popName):
    with open(popName + '.snp', 'w') as f:
        for entry in snp:
            f.write(str(entry[0]) + " " + str(entry[1]) + " " + str(entry[2]) + " " + str(entry[3]) + " " +
                    str(entry[4]) + " " + str(entry[5]) + "\n")

def writeGeno(geno, popName):
    with open(popName + '.geno', 'w') as f:
        for entry in geno:
            f.write(entry + "\n")

def writeInd(ind, popName):
    with open(popName + '.ind', 'w') as f:
        f.write(ind)

def writeAncestry(ancestry, popName):
    with open(popName + '.anc', 'w') as f:
        for entry in ancestry:
            f.write(entry + "\n")

def singleAdmixture(popA, popB, numGenerations, alpha, numInd, haploid, track, popName):
    """
    Model for a single admixture event.
    Takes in dictionaries as created by readData for each population A and B.
    Alpha is the portion of population A.
    NumGenerations is the number of generations since admixture.
    """
    admixedGeno, admixedSnp, ancestry = [], [], []
    ind = {'A': popA["ind"], 'B': popB["ind"]}
    geno = {'A': popA["geno"], 'B': popB["geno"]}
    snp = {'A': popA["snp"], 'B': popB["snp"]}
    numA, numB = len(ind['A']), len(ind['B'])

    if not haploid:
        numInd *= 2
        newGeno, currUsed, free = initializeState(numInd, alpha, geno, numA, numB)
        newGeno = haploidToDiploid(newGeno)
    else:
        newGeno, currUsed, free = initializeState(numInd, alpha, geno, numA, numB)
    admixedGeno.append(newGeno)
    admixedSnp.append(snp['A'][0])
    totalLength = len(snp['A'])
    currPercent, lastPercent = 0, -1

    for currPos in range(1, totalLength):
        distance = float(snp['A'][currPos][2]) - float(snp['A'][currPos - 1][2])
        currGeno = ""
        if snp['A'][currPos][1] == snp['A'][currPos - 1][1]:
            #Same Chromosome
            currAncestry = ""
            for ind in range(numInd):
                if random.uniform(0, 1) < 1 - exp(- numGenerations * distance):
                    #Recombination Event-we switch ancestors
                    newPos, currUsed, free = swapSample(currUsed, free, ind, geno, currPos, alpha)
                    currGeno += newPos
                else:
                    #No recombination- continue as usual
                    chosenPop, chosenInd = currUsed[ind]['pop'], currUsed[ind]['indNum']
                    currGeno += geno[chosenPop][currPos][chosenInd]

                if track:
                    currAncestry += currUsed[ind]['pop']
        else:
            #Next Chromosome
            currGeno, currUsed, free = initializeState(numInd, alpha, geno, numA, numB)

            if track:
                currAncestry = ""
                for ind in range(numInd):
                    currAncestry += currUsed[ind]['pop']

        if not haploid:
            currGeno = haploidToDiploid(currGeno)

        admixedSnp.append(snp['A'][currPos])
        admixedGeno.append(currGeno)

        if track:
            ancestry.append(currAncestry)

        currPercent = int((currPos / totalLength) * 100)
        if currPercent > lastPercent:
            print("Completed: " + str(currPercent))
            lastPercent = currPercent

    admixedInd, numIters = '', numInd if haploid else numInd // 2
    for indNum in range(numIters):
        admixedInd += popName + str(indNum) + " " + "U" + " " + popName + "\n"

    return admixedGeno, admixedSnp, admixedInd, ancestry

params = open(sys.argv[1], "r").read().split('\n')
popA = readData(params[0], params[1], params[2])
popB = readData(params[3], params[4], params[5])
admixedGeno, admixedSnp, admixedInd, ancestry = singleAdmixture(popA, popB, int(params[7]), float(params[8]), int(params[9]), eval(params[10]), eval(params[11]), params[12])
outFile = params[6]
writeGeno(admixedGeno, outFile)
writeSnp(admixedSnp, outFile)
writeInd(admixedInd, outFile)
if eval(params[11]):
    writeAncestry(ancestry, params[12])


from math import cos, sin

import numpy
from threading import Thread
from time import sleep

# http://southpark.wikia.com/wiki/Nut_Gobbler
class BrainFuck(Thread):
	def __init__(self, motion_driver):
		self.map = {} # rotation: [(itensity, x-coord)]
		self.fov = 3 / 4 * 75 / 2 # 90 degrees rotated fov drift when scaled -1 to 1
		self.motion_driver = motion_driver

		Thread.__init__(self)
		self.daemon = True
		self.running = True
		self.start()

	@property
	def rotation(self): # MOCK
		return self.motion_driver.rotation

	def itensity(self): # MOCK
		tuples = []
		for k,v in self.map.items():
			for d, r in v:
				tuples.append((d, k + r * self.fov))
		return sorted(tuples)

	def where_do_i_go(self): # MOCK
		ordered_options = self.itensity()
		intensity, rotation = ordered_options[0]
		Fx, Fy, Fw = cos(rotation), sin(rotation), 0
		self.motion_driver.load_data({"Fx": Fx, "Fy": Fy})

	def report(self, id, data):
		self.map[id] = data

	def run(self):
		while self.running:
			intensity, cx = self.map[0][0] if 0 in self.map else (-1, 0)
			# print("Brainfeck: {:.4f} {:.4f}".format(intensity, cx))
			if 0.005 > intensity or intensity > 0.25:
				self.motion_driver.load_data({"Fx": 0, "Fy": 0, "Fw":0, "TYPE":"B"})
			else:
				self.motion_driver.load_data({"Fx": cx, "Fy": 0.1 + round(0.25-intensity,2)*3, "Fw": cx, "TYPE":"B"})
				# self.motion_driver.load_data({"Fx": cx / 1, "Fy": 0.2 + round(0.25-intensity,2)*2, "Fw": round(cx/1.3,2), "TYPE":"B"})
			sleep(0.008)


#!/usr/bin/python
#Filename: CGM_Load_Data.py
#Part of the compare gene model tool
#Maintainer Jeroen Kamsteeg

#imports
import itertools

class loadData:

	def __init__(self, gff3_input_files, fasta_input_files):

		# start class functions
		# -------------------------------------------

		# makes all the needed variabels
		self.setVariables(gff3_input_files, fasta_input_files)

		# loop tru the input files and put them in a dictionary
		for file_id in self.gff3_main_dict:
			self.fillDictionaryGFF(gff3_input_files, file_id)
			self.fillDictionaryFasta(fasta_input_files, file_id)

		# make the ch list
		for dic in range(len(self.gff3_main_dict)):
			for item in range(1, (len(self.gff3_main_dict[dic]))):
				if item in self.ch_list[dic]:
					if self.search_range == len(self.ch_list[dic])-1:
						range_start = self.ch_list[dic][self.search_range]
						range_stop = len(self.gff3_main_dict[dic])
					else:
						try:
							range_start = self.ch_list[dic][self.search_range]
							range_stop = self.ch_list[dic][self.search_range+1]
							self.search_range = self.search_range + 1
						except:
							pass

			self.search_range = 0

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

	# set al the variables and dicts, lists, etz
	def setVariables(self, gff3_input, fasta_input):
		#make dictionarys and lists based on the number of input files

		# GFF3 dict
		self.gff3_main_dict = { new_dict:{} for new_dict in range(len(gff3_input))}

		# Fasta dict
		self.fasta_main_dict = { new_dict:{} for new_dict in range(len(gff3_input))}

		# ch range list
		self.ch_list = [[] for _ in range(len(gff3_input))]

		self.search_range = 0

	# open a file
	def openFiles(self, infile):

		return infile

	# gets the data from the gff3 files
	def fillDictionaryGFF(self, gff3_input_files, item_id):

		# open the gff file
		gff3file = gff3_input_files[item_id]

		# loop trough gff3 file
		dict_key_count = 1
		ch_list_count = 1

		# gene model items
		new_gene_model = 0

		for line in gff3file:
			if line[0] == "#":
				if line[0:17] == "##sequence-region":
					ch_info = line.rstrip('\n').split(' ')
					self.ch_list[item_id].extend([dict_key_count])
				else:
					pass
			else:
				# parse the gff3 data (gff3 is tab seperated)
				split_line = line.rstrip('\n').split('\t')

				# fill the dictionary
				if split_line[2].upper() == "GENE" and new_gene_model == 1:
					itemInfo = split_line[8].rstrip('\n').replace('=',';').replace(':',';').split(';')
					for item in range(len(itemInfo)):
						if itemInfo[item] == "ID":
							itemID = itemInfo[item + 2]
							itemID = itemID.split('.')
							itemID = itemID[0]

					#combine gene model items
					itemList = split_line[0:7]
					itemList.append(itemID)

					self.gff3_main_dict[item_id][dict_key_count] = [itemList, new_gene_model_item]
					dict_key_count = dict_key_count + 1
					new_gene_model = 0

				if split_line[2].upper() == "GENE" and new_gene_model == 0:
					new_gene_model = 1
					new_gene_model_item = []

				if split_line[2].upper() != "GENE" and new_gene_model == 1:
					new_gene_model_item.append(split_line[2])

	# gets the data from the fasta files
	def fillDictionaryFasta(self, fasta_input_files, item_id):
		fastaFile = fasta_input_files[item_id]
		for line in fastaFile:
			if line[0] == ">":
				split_line = line.rstrip('\n').replace('.',' ').split(' ')
				header = split_line[0][1:len(split_line[0])]
				nextline = "seq"
			if line[0] != ">" and nextline == "seq":
				seq = line
				self.fasta_main_dict[item_id][header] = line.rstrip('\n')
				nextline = ""


from Qt import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import QVariant
from .models import LogModel

class SearchComboBox(QtWidgets.QComboBox):
    """Searchable ComboBox with empty placeholder value as first value"""

    def __init__(self, parent=None, placeholder=""):
        super(SearchComboBox, self).__init__(parent)

        self.setEditable(True)
        self.setInsertPolicy(self.NoInsert)
        self.lineEdit().setPlaceholderText(placeholder)

        # Apply completer settings
        completer = self.completer()
        completer.setCompletionMode(completer.PopupCompletion)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)

        # Force style sheet on popup menu
        # It won't take the parent stylesheet for some reason
        # todo: better fix for completer popup stylesheet
        if parent:
            popup = completer.popup()
            popup.setStyleSheet(parent.styleSheet())

        self.currentIndexChanged.connect(self.onIndexChange)

    def onIndexChange(self, index):
        print(index)

    def populate(self, items):
        self.clear()
        self.addItems([""])     # ensure first item is placeholder
        self.addItems(items)

    def get_valid_value(self):
        """Return the current text if it's a valid value else None

        Note: The empty placeholder value is valid and returns as ""

        """

        text = self.currentText()
        lookup = set(self.itemText(i) for i in range(self.count()))
        if text not in lookup:
            return None

        return text

class CheckableComboBox2(QtWidgets.QComboBox):
    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)
        self.view().pressed.connect(self.handleItemPressed)
        self._changed = False

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)
        self._changed = True

    def hidePopup(self):
        if not self._changed:
            super(CheckableComboBox, self).hidePopup()
        self._changed = False

    def itemChecked(self, index):
        item = self.model().item(index, self.modelColumn())
        return item.checkState() == QtCore.Qt.Checked

    def setItemChecked(self, index, checked=True):
        item = self.model().item(index, self.modelColumn())
        if checked:
            item.setCheckState(QtCore.Qt.Checked)
        else:
            item.setCheckState(QtCore.Qt.Unchecked)

class SelectableMenu(QtWidgets.QMenu):

    selection_changed = QtCore.Signal()

    def mouseReleaseEvent(self, event):
        action = self.activeAction()
        if action and action.isEnabled():
            action.trigger()
            self.selection_changed.emit()
        else:
            super(SelectableMenu, self).mouseReleaseEvent(event)

class CustomCombo(QtWidgets.QWidget):

    selection_changed = QtCore.Signal()

    def __init__(self, title, parent=None):
        super(CustomCombo, self).__init__(parent)
        toolbutton = QtWidgets.QToolButton(self)
        toolbutton.setText(title)

        toolmenu = SelectableMenu(self)

        toolbutton.setMenu(toolmenu)
        toolbutton.setPopupMode(QtWidgets.QToolButton.MenuButtonPopup)

        layout = QtWidgets.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(toolbutton)

        self.setLayout(layout)

        # toolmenu.selection_changed.connect(self.on_selection_changed)
        toolmenu.selection_changed.connect(self.selection_changed)

        self.toolbutton = toolbutton
        self.toolmenu = toolmenu
        self.main_layout = layout

    def populate(self, items):
        self.toolmenu.clear()
        self.addItems(items)

    def addItems(self, items):
        for item in items:
            action = self.toolmenu.addAction(item)
            action.setCheckable(True)
            action.setChecked(True)
            self.toolmenu.addAction(action)

    def items(self):
        for action in self.toolmenu.actions():
            yield action

class CheckableComboBox(QtWidgets.QComboBox):
    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)

        view = QtWidgets.QTreeView()
        view.header().hide()
        view.setRootIsDecorated(False)

        model = QtGui.QStandardItemModel()

        view.pressed.connect(self.handleItemPressed)
        self._changed = False

        self.setView(view)
        self.setModel(model)

        self.view = view
        self.model = model

    def handleItemPressed(self, index):
        item = self.model.itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)
        self._changed = True

    def hidePopup(self):
        if not self._changed:
            super(CheckableComboBox, self).hidePopup()
        self._changed = False

    def itemChecked(self, index):
        item = self.model.item(index, self.modelColumn())
        return item.checkState() == QtCore.Qt.Checked

    def setItemChecked(self, index, checked=True):
        item = self.model.item(index, self.modelColumn())
        if checked:
            item.setCheckState(QtCore.Qt.Checked)
        else:
            item.setCheckState(QtCore.Qt.Unchecked)

    def addItems(self, items):
        for text, checked in items:
            text_item = QtGui.QStandardItem(text)
            checked_item = QtGui.QStandardItem()
            checked_item.setData(QVariant(checked), QtCore.Qt.CheckStateRole)
            self.model.appendRow([text_item, checked_item])

class LogsWidget(QtWidgets.QWidget):
    """A widget that lists the published subsets for an asset"""

    active_changed = QtCore.Signal()

    def __init__(self, parent=None):
        super(LogsWidget, self).__init__(parent=parent)

        model = LogModel()

        filter_layout = QtWidgets.QHBoxLayout()

        # user_filter = SearchComboBox(self, "Users")
        user_filter = CustomCombo("Users", self)
        users = model.dbcon.distinct("user")
        user_filter.populate(users)
        user_filter.selection_changed.connect(self.user_changed)

        level_filter = CustomCombo("Levels", self)
        # levels = [(level, True) for level in model.dbcon.distinct("level")]
        levels = model.dbcon.distinct("level")
        level_filter.addItems(levels)

        date_from_label = QtWidgets.QLabel("From:")
        date_filter_from = QtWidgets.QDateTimeEdit()

        date_from_layout = QtWidgets.QVBoxLayout()
        date_from_layout.addWidget(date_from_label)
        date_from_layout.addWidget(date_filter_from)

        # now = datetime.datetime.now()
        # QtCore.QDateTime(
        #     now.year,
        #     now.month,
        #     now.day,
        #     now.hour,
        #     now.minute,
        #     second=0,
        #     msec=0,
        #     timeSpec=0
        # )
        date_to_label = QtWidgets.QLabel("To:")
        date_filter_to = QtWidgets.QDateTimeEdit()

        date_to_layout = QtWidgets.QVBoxLayout()
        date_to_layout.addWidget(date_to_label)
        date_to_layout.addWidget(date_filter_to)

        filter_layout.addWidget(user_filter)
        filter_layout.addWidget(level_filter)

        filter_layout.addLayout(date_from_layout)
        filter_layout.addLayout(date_to_layout)

        view = QtWidgets.QTreeView(self)
        view.setAllColumnsShowFocus(True)

        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addLayout(filter_layout)
        layout.addWidget(view)

        view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        view.setSortingEnabled(True)
        view.sortByColumn(
            model.COLUMNS.index("started"),
            QtCore.Qt.AscendingOrder
        )

        view.setModel(model)
        view.pressed.connect(self._on_activated)
        # prepare
        model.refresh()

        # Store to memory
        self.model = model
        self.view = view

        self.user_filter = user_filter
        self.level_filter = level_filter

    def _on_activated(self, *args, **kwargs):
        self.active_changed.emit()

    def user_changed(self):
        for action in self.user_filter.items():
            print(action)

    def on_context_menu(self, point):
        # TODO will be any actions? it's ready
        return

        point_index = self.view.indexAt(point)
        if not point_index.isValid():
            return

        # Get selected subsets without groups
        selection = self.view.selectionModel()
        rows = selection.selectedRows(column=0)

    def selected_log(self):
        selection = self.view.selectionModel()
        rows = selection.selectedRows(column=0)
        if len(rows) == 1:
            return rows[0]
        return None

class OutputWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(OutputWidget, self).__init__(parent=parent)
        layout = QtWidgets.QVBoxLayout(self)
        output_text = QtWidgets.QTextEdit()
        output_text.setReadOnly(True)
        # output_text.setLineWrapMode(QtWidgets.QTextEdit.FixedPixelWidth)

        layout.addWidget(output_text)

        self.setLayout(layout)
        self.output_text = output_text

    def add_line(self, line):
        self.output_text.append(line)

    def set_detail(self, node):
        self.output_text.clear()
        for log in node["_logs"]:
            level = log["level"].lower()

            line_f = "{message}"
            if level == "debug":
                line_f = (
                    " -"
                    " {{  {loggerName}  }}: ["
                    " {message}"
                    " ]"
                )
            elif level == "info":
                line_f = (
                    ">>> ["
                    " {message}"
                    " ]"
                )
            elif level == "warning":
                line_f = (
                    "*** WRN:"
                    "  >>> {{ {loggerName} }}: ["
                    " {message}"
                    " ]"
                )
            elif level == "error":
                line_f = (
                    "!!! ERR:"
                    " {timestamp}"
                    " >>> {{ {loggerName} }}: ["
                    " {message}"
                    " ]"
                )

            exc = log.get("exception")
            if exc:
                log["message"] = exc["message"]

            line = line_f.format(**log)

            self.add_line(line)

            if not exc:
                continue
            for _line in exc["stackTrace"].split("\n"):
                self.add_line(_line)

class LogDetailWidget(QtWidgets.QWidget):
    """A Widget that display information about a specific version"""
    data_rows = [
        "user",
        "message",
        "level",
        "logname",
        "method",
        "module",
        "fileName",
        "lineNumber",
        "host",
        "timestamp"
    ]

    html_text = u"""

{user} - {timestamp}

User
{user}

Level
{level}

Message
{message}

Log Name
{logname}

Method
{method}

File
{fileName}

Line
{lineNumber}

Host
{host}

Timestamp
{timestamp}

"""

    def __init__(self, parent=None):
        super(LogDetailWidget, self).__init__(parent=parent)

        layout = QtWidgets.QVBoxLayout(self)

        label = QtWidgets.QLabel("Detail")
        detail_widget = QtWidgets.QTextEdit()
        detail_widget.setReadOnly(True)
        layout.addWidget(label)
        layout.addWidget(detail_widget)

        self.detail_widget = detail_widget

        self.setEnabled(True)

        self.set_detail(None)

    def set_detail(self, detail_data):
        if not detail_data:
            self.detail_widget.setText("")
            return

        data = dict()
        for row in self.data_rows:
            value = detail_data.get(row) or "< Not set >"
            data[row] = value

        self.detail_widget.setHtml(self.html_text.format(**data))


"""
Plot the evaporation rate data from the RHTargets table for each test.

his script should be called as follows:

   $ python -m chamber.scripts.plot_res

Where the filepath is the tdms file to be quality checkd and database is the
name of the schema to insert the results into.
"""

import matplotlib.pyplot as plt
from matplotlib import rc
from matplotlib import rcParams
import numpy as np
import seaborn as sns

from chamber.data import sqldb
from chamber.scripts import db_check

TUBE_A = np.power(0.015, 2)*np.pi

ALL_P_T = ('SELECT DISTINCT Pressure, Temperature FROM RHTargets NATURAL JOIN '
           'Test NATURAL JOIN Setting')

GET_RESERVOIR = ('SELECT Reservoir FROM Setting INNER JOIN Test ON '
                 'Test.SettingId=Setting.SettingId WHERE TestId={}')

def format_plot():
    """Format the plots."""
    sns.set_palette('colorblind')
    font = dict(family='Times New Roman',
                weight='normal',
                size=20)
    rc('font', **font)
    rcParams['figure.figsize'] = 6, 4
    rcParams['mathtext.fontset'] = 'stix'
    rcParams['font.size'] = 12
    rcParams['font.weight'] = 'normal'
    rcParams['axes.linewidth'] = 0.3
    rcParams['lines.linewidth'] = 1
    rcParams['errorbar.capsize'] = 2

def _get_tid_list(cur, p, t):
    """Prompt the user to get p and t setting."""
    tid_list = sqldb.get_high_low_testids(cur, p, t)
    return tid_list

def _check_reservoir(cur, tid):
    """Check if the reservoir was open for a TestId."""
    cur.execute(GET_RESERVOIR.format(tid))
    return cur.fetchall()[0][0]

def _get_res_dict(cnx, tid_list):
    """Get a `dict` of result where each TestId is a key."""
    res_dict = dict()
    for tid in tid_list:
        res_dict['{}'.format(tid)] = sqldb.get_rht_results(cnx, tid)
    return res_dict

def _gen_plot(cnx, res_dict, p, t):
    """Plot the evaporation rate as a function of RH."""
    cur = cnx.cursor()
    for tid in res_dict.keys():
        rh_val = _check_reservoir(cur, tid)
        res_df = res_dict[tid]
        rh = res_df['RH']
        sig_rh = res_df['SigRH']
        b = -res_df['B']
        sig_b = res_df['SigB']
        mdpp = b/TUBE_A
        sig_mdpp = sig_b/TUBE_A
        plt.errorbar(rh, mdpp, xerr=3*sig_rh, yerr=3*sig_mdpp, fmt='.',
                     label='High RH' if rh_val == 1 else 'Low RH')
    plt.title(r"$\dot m$'' vs. RH at {0}kPa {1}K".format(int(p)//1000, t))
    plt.legend()
    plt.xlabel('RH')
    plt.ylabel(r"$\dot m''/(kg/m^2s)$")
    plt.grid()
    plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
    plt.show()

def _db_status_plot(cnx):
    """
    Plot the P vs. T settings for the tests in the MySQL database.

    Plot the Pressure vs. Temperature settings for the tests currently in the
    MySQL database. Also provides visual feedback denoting which tests are
    analyzed.

    Parameters
    ----------
    cnx : mysql.connector.connection.MySQLConnection
        Connection to MySQL database.

    Returns
    -------
    `True` or `None`
        `True` if sucessful, else `None`.

    Examples
    --------
    Get the TestIds that have been analyzed from the database.

    >>> cnx = connect('my-schema')
    >>> db_status_plot(cnx)
    True

    """
    t_max = 313.15
    gamma = 11.4
    p_not = 101325.0
    t = np.linspace(273, 312, 100)
    p = p_not*(t/t_max)**gamma
    plt.plot(t, p)
    sdf = db_check._get_setting_df(cnx)
    for pnt in db_check.PT_LIST:
        plt.plot(pnt[0], pnt[1], c='grey', marker='.')
    tid_df = db_check._get_analysis_tid_df(cnx)
    label_list = []
    for tid in sdf.TestId:
        if tid in sdf[sdf.TestId.isin(tid_df.TestId)].TestId.values:
            plt.scatter(
                sdf.loc[(sdf['TestId'] == tid) & (sdf['Reservoir'] == 1),
                        'Temperature'],
                sdf.loc[(sdf['TestId'] == tid) & (sdf['Reservoir'] == 1),
                        'Pressure'],
                c='blue', marker='+', s=300,
                label='HighRH A' if 'HighRH A' not in label_list else '')
            plt.scatter(
                sdf.loc[(sdf['TestId'] == tid) & (sdf['Reservoir'] == 0),
                        'Temperature'],
                sdf.loc[(sdf['TestId'] == tid) & (sdf['Reservoir'] == 0),
                        'Pressure'],
                c='orange', marker='x', s=250,
                label='LowRH A' if 'LowRH A' not in label_list else '')
            if 'HighRH A' not in label_list:
                label_list = label_list + ['HighRH A', 'LowRH A']
    plt.xlim(273, 312)
    plt.ylim(20000, 102000)
    plt.xlabel('T/K')
    plt.ylabel('P/Pa')
    plt.title('Tests In Database')
    plt.grid()
    plt.legend()
    plt.show()
    return True

def _plot_evap(cnx, p, t):
    """Get data to plot m-dot'' vs. RH and pass it on to plot function."""
    cur = cnx.cursor()
    tid_list = _get_tid_list(cur, p, t)
    res_dict = _get_res_dict(cnx, tid_list)
    _gen_plot(cnx, res_dict, p, t)

if __name__ == '__main__':
    print('Starting results plotting script...')
    format_plot()
    cnx = sqldb.connect('chamber')
    cur = cnx.cursor()
    plot = True
    while plot is True:
        _db_status_plot(cnx)
        p = input('Specify a pressure setting in kPa, "a" if all: ')
        t = input('Specify a temperature setting in K, "a" if all: ')
        if p == 'a' or t == 'a':
            cur.execute(ALL_P_T)
            p_t_list = [p_t for p_t in cur.fetchall()]
            for p_t in p_t_list:
                _plot_evap(cnx, p_t[0], p_t[1])
        else:
            _plot_evap(cnx, int(p)*1000, t)
        if input('Plot another setting? [y/n]') != 'y':
            plot = False
    print('End.')


from .mtgnn_layers import *

class MTGNN(nn.Module):
    r"""An implementation of the Multivariate Time Series Forecasting Graph Neural Networks.
    For details see this paper: `"Connecting the Dots: Multivariate Time Series Forecasting with Graph Neural Networks." <https://arxiv.org/pdf/2005.11650.pdf>`_

    Args:
        gcn_true (bool) : whether to add graph convolution layer.
        buildA_true (bool) : whether to construct adaptive adjacency matrix.
        gcn_depth (int) : graph convolution depth.
        num_nodes (int) : number of nodes in the graph.
        dropout (float, optional) : droupout rate, default 0.3.
        static_feat (Pytorch Float Tensor, optional) : static feature, default None.
        predefined_A (Pytorch Float Tensor, optional) : predefined adjacency matrix, default None.
        subgraph_size (int, optional) : size of subgraph, default 20.
        node_dim (int, optional) : dimension of nodes, default 40.
        dilation_exponential (int, optional) : dilation exponential, default 1.
        conv_channels (int, optional) : convolution channels, default 32.
        residual_channels (int, optional) : residual channels, default 32.
        skip_channels (int, optional) : skip channels, default 64.
        end_channels (int, optional): end channels, default 128.
        seq_length (int, optional) : length of input sequence, default 12.
        in_dim (int, optional) : input dimension, default 2.
        out_dim (int, optional) : output dimension, default 12.
        layers (int, optional) : number of layers, default 3.
        propalpha (float, optional) : prop alpha, ratio of retaining the root nodes's original states in mix-hop propagation, a value between 0 and 1, default 0.05.
        tanhalpha (float, optional) : tanh alpha for generating adjacency matrix, alpha controls the saturation rate, default 3.
        layer_norm_affline (bool, optional) : whether to do elementwise affine in Layer Normalization, default True.
    """
    def __init__(self, gcn_true, buildA_true, gcn_depth, num_nodes, predefined_A=None, static_feat=None, dropout=0.3, subgraph_size=20, node_dim=40, dilation_exponential=1, conv_channels=32, residual_channels=32, skip_channels=64, end_channels=128, seq_length=12, in_dim=2, out_dim=12, layers=3, propalpha=0.05, tanhalpha=3, layer_norm_affline=True):
        super(MTGNN, self).__init__()
        self.gcn_true = gcn_true
        self.buildA_true = buildA_true
        self.num_nodes = num_nodes
        self.dropout = dropout
        self.predefined_A = predefined_A
        self.filter_convs = nn.ModuleList()
        self.gate_convs = nn.ModuleList()
        self.residual_convs = nn.ModuleList()
        self.skip_convs = nn.ModuleList()
        self.gconv1 = nn.ModuleList()
        self.gconv2 = nn.ModuleList()
        self.norm = nn.ModuleList()
        self.start_conv = nn.Conv2d(in_channels=in_dim,
                                    out_channels=residual_channels,
                                    kernel_size=(1, 1))
        self.gc = graph_constructor(num_nodes, subgraph_size, node_dim, alpha=tanhalpha, static_feat=static_feat)

        self.seq_length = seq_length
        kernel_size = 7
        if dilation_exponential>1:
            self.receptive_field = int(1+(kernel_size-1)*(dilation_exponential**layers-1)/(dilation_exponential-1))
        else:
            self.receptive_field = layers*(kernel_size-1) + 1

        for i in range(1):
            if dilation_exponential>1:
                rf_size_i = int(1 + i*(kernel_size-1)*(dilation_exponential**layers-1)/(dilation_exponential-1))
            else:
                rf_size_i = i*layers*(kernel_size-1)+1
            new_dilation = 1
            for j in range(1,layers+1):
                if dilation_exponential > 1:
                    rf_size_j = int(rf_size_i + (kernel_size-1)*(dilation_exponential**j-1)/(dilation_exponential-1))
                else:
                    rf_size_j = rf_size_i+j*(kernel_size-1)

                self.filter_convs.append(dilated_inception(residual_channels, conv_channels, dilation_factor=new_dilation))
                self.gate_convs.append(dilated_inception(residual_channels, conv_channels, dilation_factor=new_dilation))
                self.residual_convs.append(nn.Conv2d(in_channels=conv_channels,
                                                    out_channels=residual_channels,
                                                 kernel_size=(1, 1)))
                if self.seq_length>self.receptive_field:
                    self.skip_convs.append(nn.Conv2d(in_channels=conv_channels,
                                                    out_channels=skip_channels,
                                                    kernel_size=(1, self.seq_length-rf_size_j+1)))
                else:
                    self.skip_convs.append(nn.Conv2d(in_channels=conv_channels,
                                                    out_channels=skip_channels,
                                                    kernel_size=(1, self.receptive_field-rf_size_j+1)))

                if self.gcn_true:
                    self.gconv1.append(mixprop(conv_channels, residual_channels, gcn_depth, dropout, propalpha))
                    self.gconv2.append(mixprop(conv_channels, residual_channels, gcn_depth, dropout, propalpha))

                if self.seq_length>self.receptive_field:
                    self.norm.append(LayerNorm((residual_channels, num_nodes, self.seq_length - rf_size_j + 1),elementwise_affine=layer_norm_affline))
                else:
                    self.norm.append(LayerNorm((residual_channels, num_nodes, self.receptive_field - rf_size_j + 1),elementwise_affine=layer_norm_affline))

                new_dilation *= dilation_exponential

        self.layers = layers
        self.end_conv_1 = nn.Conv2d(in_channels=skip_channels,
                                             out_channels=end_channels,
                                             kernel_size=(1,1),
                                             bias=True)
        self.end_conv_2 = nn.Conv2d(in_channels=end_channels,
                                             out_channels=out_dim,
                                             kernel_size=(1,1),
                                             bias=True)
        if self.seq_length > self.receptive_field:
            self.skip0 = nn.Conv2d(in_channels=in_dim, out_channels=skip_channels, kernel_size=(1, self.seq_length), bias=True)
            self.skipE = nn.Conv2d(in_channels=residual_channels, out_channels=skip_channels, kernel_size=(1, self.seq_length-self.receptive_field+1), bias=True)

        else:
            self.skip0 = nn.Conv2d(in_channels=in_dim, out_channels=skip_channels, kernel_size=(1, self.receptive_field), bias=True)
            self.skipE = nn.Conv2d(in_channels=residual_channels, out_channels=skip_channels, kernel_size=(1, 1), bias=True)

        self.idx = torch.arange(self.num_nodes)

    def forward(self, input, idx=None):
        """
        Making a forward pass of MTGNN.

        Arg types:
            * input (PyTorch Float Tensor) - input sequence, with shape (batch size, input dimension, number of nodes, input sequence length).
            * idx (Tensor, optional): input indices, a permutation of the number of nodes, default None (no permutation).

        Return types:
            * x (PyTorch Float Tensor) - output sequence for prediction, with shape (batch size, input sequence length, number of nodes, 1).
        """
        seq_len = input.size(3)
        assert seq_len==self.seq_length, 'input sequence length not equal to preset sequence length'

        if self.seq_length<self.receptive_field:
            input = nn.functional.pad(input,(self.receptive_field-self.seq_length,0,0,0))

        if self.gcn_true:
            if self.buildA_true:
                if idx is None:
                    adp = self.gc(self.idx.to(input.device))
                else:
                    adp = self.gc(idx)
            else:
                adp = self.predefined_A

        x = self.start_conv(input)
        skip = self.skip0(F.dropout(input, self.dropout, training=self.training))
        for i in range(self.layers):
            residual = x
            filter = self.filter_convs[i](x)
            filter = torch.tanh(filter)
            gate = self.gate_convs[i](x)
            gate = torch.sigmoid(gate)
            x = filter * gate
            x = F.dropout(x, self.dropout, training=self.training)
            s = x
            s = self.skip_convs[i](s)
            skip = s + skip
            if self.gcn_true:
                x = self.gconv1[i](x, adp)+self.gconv2[i](x, adp.transpose(1,0))
            else:
                x = self.residual_convs[i](x)

            x = x + residual[:, :, :, -x.size(3):]
            if idx is None:
                x = self.norm[i](x,self.idx)
            else:
                x = self.norm[i](x,idx)

        skip = self.skipE(x) + skip
        x = F.relu(skip)
        x = F.relu(self.end_conv_1(x))
        x = self.end_conv_2(x)
        return x


from pyramid.httpexceptions import HTTPSeeOther
from pyramid.view import forbidden_view_config
from webapp.libapp.models.user_account import UserAccount
__author__ = 'burchardtist'

@forbidden_view_config()
def forbidden_view(request):
    if request.authenticated_userid:
        return request.context
    url = request.route_url('user', action='login')
    return HTTPSeeOther(url)

def get_user(request):
    user_id = request.authenticated_userid
    return request.db.query(UserAccount).filter_by(user_id=user_id).first()


def read_file(filename):
	lines = []
	with open(filename, 'r', encoding = 'utf-8') as f:
		for line in f:
			lines.append(line.strip())
	return lines

def count(lines):
	person = None
	count1 = 0
	count2 = 0
	for line in lines:
		s = line.split(' ')
		time = s[0]
		name = s[1]
		if name == '柏維':
			for m in s[2:]:
				count1 += len(m)
		elif name == '林芳鈴（緣心、香香媽咪）':
			for m in s[2:]:
				count2 += len(m)
	print('柏維說了', count1, '個字')
	print('芳鈴說了', count2, '個字')

def write_file(filename, lines):
	with open(filename, 'w') as f:
		for line in lines:
			f.write(line + '\n')

def main():
	lines = read_file('line.txt')
	lines = count(lines)
	# write_file('output.txt', lines)

main()


import torch
import numpy as np
import matplotlib.pyplot as plt
from torchvision import transforms

from time import time as t
import datetime
from tqdm import tqdm
from IPython import display

from bindsnet.datasets import MNIST
from bindsnet.encoding import PoissonEncoder
from bindsnet.network import Network, load
from bindsnet.learning import PostPre, WeightDependentPostPre
from bindsnet.network.monitors import Monitor, NetworkMonitor
from bindsnet.network.nodes import AdaptiveLIFNodes, Input
from bindsnet.network.topology import LocalConnection, Connection
from bindsnet.analysis.plotting import (
    plot_input,
    plot_conv2d_weights,
    plot_voltages,
    plot_spikes
    )

import streamlit as st

class LC_SNN:
    def __init__(self, norm=0.5, competitive_weight=-100., n_iter=1000, load=False):
        self.n_iter = n_iter
        self.calibrated = False
        if not load:
            self.create_network(norm=norm, competitive_weight=competitive_weight)
        else:
            self.norm = None
            self.competitive_weight = None
            self.n_iter = None
            self.time_max = 100

    def plot(self):
        plt.figure()
        test_dataloader = torch.utils.data.DataLoader(
            self.train_dataset, batch_size=1, shuffle=True)

        for whatever, batch in list(zip([0], test_dataloader)):
            #Processing
            inpts = {"X": batch["encoded_image"].transpose(0, 1)}
            label = batch["label"]
            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            #Visualization
            # Optionally plot various simulation information.
            inpt_axes = None
            inpt_ims = None
            spike_ims = None
            spike_axes = None
            weights1_im = None
            voltage_ims = None
            voltage_axes = None
            image = batch["image"].view(28, 28)

            inpt = inpts["X"].view(self.time_max, 784).sum(0).view(28, 28)
            weights_XY = self.connection_XY.w
            weights_YY = self.connection_YY.w

            self._spikes = {
                "X": self.spikes["X"].get("s").view(self.time_max, -1),
                "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                }
            _voltages = {"Y": self.voltages["Y"].get("v").view(self.time_max, -1)}

            inpt_axes, inpt_ims = plot_input(
                image, inpt, label=label, axes=inpt_axes, ims=inpt_ims
                )
            spike_ims, spike_axes = plot_spikes(self._spikes, ims=spike_ims, axes=spike_axes)
            f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10))
            weights_XY = weights_XY.reshape(28, 28, -1)
            weights_to_display = torch.zeros(0, 28*25)
            i = 0
            while i < 625:
                for j in range(25):
                    weights_to_display_row = torch.zeros(28, 0)
                    for k in range(25):
                        weights_to_display_row = torch.cat((weights_to_display_row, weights_XY[:, :, i]), dim=1)
                        i += 1
                    weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)
            im1 = ax1.imshow(weights_to_display.numpy())
            im2 = ax2.imshow(weights_YY.reshape(5*5*25, 5*5*25).numpy())
            f.colorbar(im1, ax=ax1)
            f.colorbar(im2, ax=ax2)
            ax1.set_title('XY weights')
            ax2.set_title('YY weights')
            f.show()
            voltage_ims, voltage_axes = plot_voltages(
                _voltages, ims=voltage_ims, axes=voltage_axes
                )
        self.network.reset_()  # Reset state variables
        return weights_to_display

    def create_network(self, norm=0.5, competitive_weight=-100.):
        self.norm = norm
        self.competitive_weight = competitive_weight
        self.time_max = 30
        dt = 1
        intensity = 127.5

        self.train_dataset = MNIST(
            PoissonEncoder(time=self.time_max, dt=dt),
            None,
            "MNIST",
            download=False,
            train=True,
            transform=transforms.Compose(
                [transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]
                )
            )

        # Hyperparameters
        n_filters = 25
        kernel_size = 12
        stride = 4
        padding = 0
        conv_size = int((28 - kernel_size + 2 * padding) / stride) + 1
        per_class = int((n_filters * conv_size * conv_size) / 10)
        tc_trace = 20.  # grid search check
        tc_decay = 20.
        thresh = -52
        refrac = 5

        wmin = 0
        wmax = 1

        # Network
        self.network = Network(learning=True)
        self.GlobalMonitor = NetworkMonitor(self.network, state_vars=('v', 's', 'w'))

        self.input_layer = Input(n=784, shape=(1, 28, 28), traces=True)

        self.output_layer = AdaptiveLIFNodes(
            n=n_filters * conv_size * conv_size,
            shape=(n_filters, conv_size, conv_size),
            traces=True,
            thres=thresh,
            trace_tc=tc_trace,
            tc_decay=tc_decay,
            theta_plus=0.05,
            tc_theta_decay=1e6)

        self.connection_XY = LocalConnection(
            self.input_layer,
            self.output_layer,
            n_filters=n_filters,
            kernel_size=kernel_size,
            stride=stride,
            update_rule=PostPre,
            norm=norm, #1/(kernel_size ** 2),#0.4 * kernel_size ** 2,  # norm constant - check
            nu=[1e-4, 1e-2],
            wmin=wmin,
            wmax=wmax)

        # competitive connections
        w = torch.zeros(n_filters, conv_size, conv_size, n_filters, conv_size, conv_size)
        for fltr1 in range(n_filters):
            for fltr2 in range(n_filters):
                if fltr1 != fltr2:
                    # change
                    for i in range(conv_size):
                        for j in range(conv_size):
                            w[fltr1, i, j, fltr2, i, j] = competitive_weight

        self.connection_YY = Connection(self.output_layer, self.output_layer, w=w)

        self.network.add_layer(self.input_layer, name='X')
        self.network.add_layer(self.output_layer, name='Y')

        self.network.add_connection(self.connection_XY, source='X', target='Y')
        self.network.add_connection(self.connection_YY, source='Y', target='Y')

        self.network.add_monitor(self.GlobalMonitor, name='Network')

        self.spikes = {}
        for layer in set(self.network.layers):
            self.spikes[layer] = Monitor(self.network.layers[layer], state_vars=["s"], time=self.time_max)
            self.network.add_monitor(self.spikes[layer], name="%s_spikes" % layer)
            #print('GlobalMonitor.state_vars:', self.GlobalMonitor.state_vars)

        self.voltages = {}
        for layer in set(self.network.layers) - {"X"}:
            self.voltages[layer] = Monitor(self.network.layers[layer], state_vars=["v"], time=self.time_max)
            self.network.add_monitor(self.voltages[layer], name="%s_voltages" % layer)

    def train(self, n_iter=None, plot=False, vis_interval=10):
        n_iter = self.n_iter
        self.network.train(True)
        print('Training network...')
        train_dataloader = torch.utils.data.DataLoader(
            self.train_dataset, batch_size=1, shuffle=True)
        progress_bar = st.progress(0)
        status = st.empty()
        t_start = t()
        cnt = 0

        if plot:
            f = plt.figure(figsize=(15, 15))
            plt.imshow(torch.randint(0, 2, (700, 700)).numpy(), cmap='YlOrBr')
            plt.colorbar()
            plot_weights = st.pyplot(f)

        for smth, batch in tqdm(list(zip(range(n_iter), train_dataloader))):
            progress_bar.progress(int((smth + 1)/n_iter*100))
            t_now = t()
            time_from_start = str(datetime.timedelta(seconds=(int(t_now - t_start))))
            speed = round((smth + 1) / (t_now - t_start), 2)
            time_left = str(datetime.timedelta(seconds=int((n_iter - smth) / speed)))
            status.text(f'{smth + 1}/{n_iter} [{time_from_start}] < [{time_left}], {speed}it/s')
            inpts = {"X": batch["encoded_image"].transpose(0, 1)}
            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            if plot:
                if (t_now - t_start) / vis_interval > cnt:
                    weights_XY = self.connection_XY.w
                    weights_XY = weights_XY.reshape(28, 28, -1)
                    weights_to_display = torch.zeros(0, 28*25)
                    i = 0
                    while i < 625:
                        for j in range(25):
                            weights_to_display_row = torch.zeros(28, 0)
                            for k in range(25):
                                weights_to_display_row = torch.cat((weights_to_display_row, weights_XY[:, :, i]), dim=1)
                                i += 1
                            weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)
                    self.weights_XY = weights_to_display.numpy()

                    self._spikes = {
                        "X": self.spikes["X"].get("s").view(self.time_max, -1),
                        "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                        }
                    fig_w = plt.figure(figsize=(15, 15))
                    plt.title('Weights_XY')
                    plt.imshow(self.weights_XY, cmap='YlOrBr')
                    plt.colorbar()
                    plot_weights.pyplot(fig_w)
                    plt.close(fig_w)
                    cnt += 1
                else:
                    pass
        self.network.reset_()  # Reset state variables
        self.network.train(False)

    def fit(self, X, y, n_iter=None):
        n_iter = self.n_iter
        self.network.train(True)
        print('Fitting network...')
        smth = 0

        for inp, label in tqdm(zip(X[:n_iter], y[:n_iter])):
            inpts = {"X": inp}
            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            weights_XY = self.connection_XY.w
            weights_XY = weights_XY.reshape(28, 28, -1)
            weights_to_display = torch.zeros(0, 28*25)
            i = 0
            while i < 625:
                for j in range(25):
                    weights_to_display_row = torch.zeros(28, 0)
                    for k in range(25):
                        weights_to_display_row = torch.cat((weights_to_display_row, weights_XY[:, :, i]), dim=1)
                        i += 1
                    weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)

            self.weights_XY = weights_to_display.numpy()

            self._spikes = {
                "X": self.spikes["X"].get("s").view(self.time_max, -1),
                "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                }

        self.network.reset_()  # Reset state variables

        self.network.train(False);

    def class_from_spikes(self):
        sum_output = self.spikes['Y'].get('s').reshape(625, self.time_max).sum(1)
        res = torch.matmul(self.votes.type(torch.LongTensor), sum_output)
        return res.argmax()

    def debug_predictions(self, n_iter):
        train_dataloader = torch.utils.data.DataLoader(
            self.train_dataset, batch_size=1, shuffle=True)

        for i, batch in tqdm(list(zip(range(n_iter), train_dataloader))):
            inpts = {"X": batch["encoded_image"].transpose(0, 1)}
            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)
            print(f'Network top class:{self.class_from_spikes()}\n Correct label: {batch["label"]}')

    def predict_many(self, n_iter=6000):
        y = []
        x = []
        self.network.train(False)
        train_dataloader = torch.utils.data.DataLoader(
            self.train_dataset, batch_size=1, shuffle=True)

        for i, batch in tqdm(list(zip(range(n_iter), train_dataloader))):
            inpts = {"X": batch["encoded_image"].transpose(0, 1)}

            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            self._spikes = {
                "X": self.spikes["X"].get("s").view(self.time_max, -1),
                "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                }

            y.append(int(batch['label']))
            output = self._spikes['Y'].type(torch.int).sum(0)
            x.append(output)

        self.network.reset_()  # Reset state variables

        return tuple([x, y])

    def plot_weights_XY(self):
        weights_XY = self.network.connections[('X', 'Y')].w.reshape(28, 28, -1)
        weights_to_display = torch.zeros(0, 28*25)
        i = 0
        while i < 625:
            for j in range(25):
                weights_to_display_row = torch.zeros(28, 0)
                for k in range(25):
                    weights_to_display_row = torch.cat((weights_to_display_row, weights_XY[:, :, i]), dim=1)
                    i += 1
                weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)
        plt.figure(figsize=(15, 15))
        plt.imshow(weights_to_display, cmap='YlOrBr')
        plt.colorbar()

    def load(self, file_path):
        self.network = load(file_path)
        self.n_iter = 60000

        dt = 1
        intensity = 127.5

        self.train_dataset = MNIST(
            PoissonEncoder(time=self.time_max, dt=dt),
            None,
            "MNIST",
            download=False,
            train=True,
            transform=transforms.Compose(
                [transforms.ToTensor(), transforms.Lambda(lambda x: x * intensity)]
                )
            )

        self.spikes = {}
        for layer in set(self.network.layers):
            self.spikes[layer] = Monitor(self.network.layers[layer], state_vars=["s"], time=self.time_max)
            self.network.add_monitor(self.spikes[layer], name="%s_spikes" % layer)
            #print('GlobalMonitor.state_vars:', self.GlobalMonitor.state_vars)

        self.voltages = {}
        for layer in set(self.network.layers) - {"X"}:
            self.voltages[layer] = Monitor(self.network.layers[layer], state_vars=["v"], time=self.time_max)
            self.network.add_monitor(self.voltages[layer], name="%s_voltages" % layer)

        weights_XY = self.network.connections[('X', 'Y')].w

        weights_XY = weights_XY.reshape(28, 28, -1)
        weights_to_display = torch.zeros(0, 28*25)
        i = 0
        while i < 625:
            for j in range(25):
                weights_to_display_row = torch.zeros(28, 0)
                for k in range(25):
                    weights_to_display_row = torch.cat((weights_to_display_row, weights_XY[:, :, i]), dim=1)
                    i += 1
                weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)

        self.weights_XY = weights_to_display.numpy()

        # TODO formatted weights to display

    def show_neuron(self, n):
        weights_to_show = self.network.connections[('X', 'Y')].w.reshape(28, 28, -1).clone()
        weights_to_show[:, :, n-1] = torch.ones(28, 28)
        weights_to_display = torch.zeros(0, 28*25)
        i = 0
        while i < 625:
            for j in range(25):
                weights_to_display_row = torch.zeros(28, 0)
                for k in range(25):
                    weights_to_display_row = torch.cat((weights_to_display_row, weights_to_show[:, :, i]), dim=1)
                    i += 1
                weights_to_display = torch.cat((weights_to_display, weights_to_display_row), dim=0)

        plt.figure(figsize=(15, 15))
        plt.title('Weights XY')

        plt.imshow(weights_to_display.numpy(), cmap='YlOrBr')
        plt.colorbar()

    def calibrate_top_classes(self, n_iter=100):
        print('Calibrating top classes for each neuron...')
        (x, y) = self.predict_many(n_iter=n_iter)
        votes = torch.zeros(11, 625)
        votes[10, :] = votes[10, :].fill_(1/(2*n_iter))
        for (label, layer) in zip(y, x):
            for i, spike_sum in enumerate(layer):
                votes[label, i] += spike_sum
        for i in range(10):
            votes[i, :] = votes[i, :] / len((np.array(y) == i).nonzero()[0])
        top_classes = votes.argmax(dim=0).numpy()
        # top_classes_formatted = np.where(top_classes!=10, top_classes, None)
        self.top_classes = top_classes
        self.votes = votes
        self.calibrated = True
        return top_classes, votes

    def accuracy(self, n_iter):
        self.network.train(False)
        if not self.calibrated:
            self.calibrate_top_classes(n_iter=self.n_iter)

        train_dataloader = torch.utils.data.DataLoader(
            self.train_dataset, batch_size=1, shuffle=True)

        print('Calculating accuracy...')

        x = []
        y = []

        for i, batch in tqdm(list(zip(range(n_iter), train_dataloader))):
            inpts = {"X": batch["encoded_image"].transpose(0, 1)}

            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            self._spikes = {
                "X": self.spikes["X"].get("s").view(self.time_max, -1),
                "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                }

            output = self._spikes['Y'].type(torch.int).sum(0)
            top3 = output.argsort()[0:3]
            label = int(batch['label'])
            n_1, n_2, n_3 = top3[0], top3[1], top3[2]
            n_best = n_1
            if output[n_1] * self.votes[label][n_1] > output[n_2] * self.votes[label][n_2]:
                if output[n_2] * self.votes[label][n_2] > output[n_3] * self.votes[label][n_3]:
                    pass
                else:
                    if output[n_1] * self.votes[label][n_1] > output[n_3] * self.votes[label][n_3]:
                        pass
            else:
                if output[n_2] * self.votes[label][n_2] > output[n_3] * self.votes[label][n_3]:
                    n_best = n_2
                else:
                    if output[n_3] * self.votes[label][n_3] > output[n_1] * self.votes[label][n_1]:
                        n_best = n_3

            x.append(self.top_classes[n_best])
            y.append(label)

        corrects = []
        for i in range(len(x)):
            if x[i] == y[i]:
                corrects.append(1)
            else:
                corrects.append(0)
        corrects = np.array(corrects)

        self.network.reset_()
        print(f'Accuracy: {corrects.mean()}')
        return corrects.mean()

    def score(self, X, y_correct):
        self.network.train(False)
        if not self.calibrated:
            self.calibrate_top_classes()
        print('Calculating score...')

        x = []

        for inp, label in zip(X, y_correct):
            inpts = {'X': inp}

            self.network.run(inpts=inpts, time=self.time_max, input_time_dim=1)

            self._spikes = {
                "X": self.spikes["X"].get("s").view(self.time_max, -1),
                "Y": self.spikes["Y"].get("s").view(self.time_max, -1),
                }

            output = self._spikes['Y'].type(torch.int).sum(0)
            top3 = output.argsort()[0:3]
            n_1, n_2, n_3 = top3[0], top3[1], top3[2]
            x.append(self.top_classes[n_1])

        corrects = []
        for i in range(len(x)):
            if x[i] == y_correct[i]:
                corrects.append(1)
            else:
                corrects.append(0)
        corrects = np.array(corrects)

        self.network.reset_()
        print(f'Accuracy: {corrects.mean()}')
        return corrects.mean()

    def get_params(self, **args):
        return {'norm': self.norm,
                'competitive_weight': self.competitive_weight,
                'n_iter': self.n_iter
                }

    def set_params(self, norm, competitive_weight, n_iter):
        display.clear_output(wait=True)
        return LC_SNN(norm=norm, competitive_weight=competitive_weight, n_iter=n_iter)

    def __repr__(self):
        # return f'LC_SNN network with parameters:\nnorm = {self.norm}\ncompetitive_weights={self.competitive_weight}' \
        #        f'\nn_iter={self.n_iter}'
        return f'LC_SNN network with parameters:\n {self.get_params()}'


from django.shortcuts import render
from .models import Post

from assets.python import std_template_funcs as template_funcs

def post_view(request, post_slug):
    post = Post.objects.get(post_slug=post_slug)
    return render(
        request,
        'standard_template.html',
        {
            'title': post.post_title,
            'page_destinations': ['post_view'],
            'display_elements': ['sidebar', 'post_viewer'],
            'sidebar': {
                'avaible_tags' : template_funcs.get_tags_list(),
                'recent_posts' : template_funcs.get_recent_posts(),
            },
            'post': post,
        }
    )


import pandas as pd
from numpy import ravel
from numpy.core.umath_tests import inner1d

from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn import linear_model
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import cross_validate
from output import *

def main():
    dfs = ['../data/dataset/db1.csv', '../data/dataset/db2.csv', '../data/dataset/db3.csv', '../data/dataset/db4.csv', '../data/dataset/db5.csv', '../data/dataset/db6.csv', '../data/dataset/db7.csv']

    f = createFile("Ensemble")

    for path in dfs:
        print('\n'+path)
        df = pd.read_csv(path)
        X = df.ix[:,1:df.shape[1]-1]
        y = df.ix[:,df.shape[1]-1:df.shape[1]]

        X = X.values
        y = y.values

        y = ravel(y)

        clf1 = RandomForestClassifier(n_estimators=500, random_state=0)
        clf2 = linear_model.LogisticRegression(C=0.01)
        clf3 = svm.SVC()
        clf4 = GaussianNB()
        clf5 = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(10, 5), random_state=1)

        eclf = VotingClassifier(estimators=[('rf', clf1), ('lr', clf2), ('svm', clf3), ('nb', clf4), ('mlp', clf5)], voting='hard', n_jobs=1)
        folders = 10

        scores = cross_validate(eclf, X, y, cv=folders, scoring=('accuracy', 'precision', 'recall'), return_train_score=True)

        printResults(scores)
        saveResults(f, scores, path)

        # Saving Model
        # eclf.fit(X,y)
        # with open ("./ensemble.ml", 'wb') as fp:
        #     pickle.dump(eclf, fp)

        title = "Ensemble_" + path[-7:-4]
        plot_learning_curve(eclf, title, X, y, cv=folders, n_jobs=1)

if __name__ == "__main__":
    main()


# -*- coding: utf8 -*-

import pymongo
import json
from operator import eq
from pymongo import MongoClient

#커넥션 생성
connection = MongoClient('localhost', 27017)
db = connection.Health_One

#scope에서 비교연산자와 피연산자를 dict자료형으로 추출 (create_keyword_list에서 호출)
def extract(scope):
    return {'operator':scope[:2], 'operand':scope[2]}

#MongoDB에서 전체키워드리스트 추출
def create_keyword_list(survey_string, category_string):
    #키워드가 저장 될 리스트
    result_keyword_list = []

    #인덱스 오름차순으로 각 도큐먼트를 한개씩 조회
    for doc in db.keyword_set.find().sort('index', pymongo.ASCENDING):
        category = doc['category']
        index = doc['index']
        scope = extract(doc['scope'])
        keyword_list = doc['keyword_list']

        #DB에 저장된 조건이 '=='일 때
        if eq(scope['operator'], '=='):
            if int(survey_string[index]) == int(scope['operand']):
                result_keyword_list.append(keyword_list)

        #DB에 저장된 조건이 '<='일 때
        elif eq(scope['operator'], '<='):
            if int(survey_string[index]) <= int(scope['operand']):
                result_keyword_list.append(keyword_list)

        #DB에 저장된 조건이 '>='일 때
        elif eq(scope['operator'], '>='):
            if int(survey_string[index]) >= int(scope['operand']):
                result_keyword_list.append(keyword_list)

    #키워드셋 리스트 리턴
    return result_keyword_list

#MongoDB에 키워드를 추가
def insert_keyword_list(category, index, scope, keywords):
    #매개변수 인덱스와 스코프에 해당하는 도큐먼트에 키워드를 추가 (해당하는 도큐먼트 없으면 토큐먼트를 새로 생성)
    db.keyword_set.update({'$and':[{'category':category}, {'index':index}, {'scope':scope}]}, {'$addToSet':{'keyword_list':{'$each':keywords}}}, upsert=True)

#MongoDB에서 키워드를 삭제
def delete_keyword_list(category, index, scope, keywords):
    #매개변수 인덱스와 스코프는 프론트에서 도큐먼트 조회 시 유지되는 데이터로 전달받음
    db.keyword_set.update({'$and':[{'category':category}, {'index':index}, {'scope':scope}]}, {'$pull':{'keyword_list':{'$in':keywords}}})

#전체키워드리스트에서 사용자가 선택한 카테고리의 키워드 추출
def extract_keyword_list(keywords):
    return null

#코드 실행
print(create_keyword_list('111111111102410311202001000000', '11000000'))
#insert_keyword_list(0, 10, '<=13', ['테스트키워드1', '테스트키워드2', '테스트키워드3'])
#delete_keyword_list(0, 10, '<=16', ['테스트키워드1', '테스트키워드2'])


# Useful functions for more clear code
import urllib

# Get file content in a given file path
def get_file(filepath):
    file = open(filepath, 'r')
    content = file.read()
    file.close()
    return content

# Get webpage content as a string
# Specify length if you want to get only first "length" characters
def get_webpage(url,length=''):
    webpage = urllib.urlopen(url)
    content = webpage.read()
    if length != '' and type(length) is int:
        return content[0:length]
    return content


import tkinter as TK

def cbm(event):

    ft.set(mtr.get()*3)

def main():

    root = TK.Tk()

    global mtr,ft

    mtr = TK.IntVar()
    ft = TK.IntVar()

    mtr.set(0)
    ft.set(0)

    m= TK.Scale(root,from_=0,to=100,variable=mtr,tickinterval=10,width=20,length=400,command=cbm)

    m.grid(row=0,column=0)

    f = TK.Scale(root,from_=0,to=300,variable=ft,length=400,width=20,tickinterval=25)
    f.grid(row=0,column=1)

    label1 = TK.Label(root,text='Metre')
    label2 = TK.Label(root,text='Feet')

    label1.grid(row=1,column=0)
    label2.grid(row=1,column=1)

    root.mainloop()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
import allure
import pytest
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.firefox.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager

from tests import get_test_data

def pytest_addoption(parser):
    """This method adds options for pytest runner"""
    parser.addoption("--browser", action="store", default="Firefox")
    parser.addoption("--headless", action="store", default="True")

@pytest.fixture
def driver(request):
    """This method creates webdriver object to use in tests.
    webdriver can represent ChromeDriver of Geckodriver (for Firefox)
    and can either be an ordinary browser or a headless browser.
    Those options can be set in cmd when running pytest
    Example:
    pytest tests --headless True --browser 'Firefox'
    pytest tests --headless False --browser 'Chrome'
    """
    if request.config.getoption('--headless') == 'True':
        headless = True
    else:
        headless = False
    browser = request.config.getoption('--browser')

    def create_chrome_driver():
        options = ChromeOptions()
        options.add_argument("--window-size=1920,1080")
        options.add_argument("--start-maximized")
        options.headless = headless
        return webdriver.Chrome(executable_path=ChromeDriverManager().install(),
                                options=options)

    def create_firefox_driver():
        options = Options()
        options.add_argument("--window-size=1920,1080")
        options.add_argument("--start-maximized")
        options.headless = headless
        return webdriver.Firefox(executable_path=GeckoDriverManager().install(),
                                 options=options)

    driver_ = None
    if browser == 'Chrome':
        driver_ = create_chrome_driver()
    elif browser == 'Firefox':
        driver_ = create_firefox_driver()
    driver_.set_script_timeout(10)
    driver_.set_page_load_timeout(30)

    def tear_down():
        call_result = getattr(request.node, 'rep_call', None)
        if call_result and call_result.failed:
            allure.attach(driver_.get_screenshot_as_png(),
                          attachment_type=allure.attachment_type.PNG)
        driver_.quit()

    request.addfinalizer(tear_down)
    yield driver_

def pytest_generate_tests(metafunc):
    """This method links testcase in folder tests
    to the test data in folder resources and creates data fixture"""
    def parametrize(test_data):
        for key, value in test_data.items():
            param_keys = [k.strip() for k in key.split(',')]
            diff = set(param_keys) - set(metafunc.fixturenames)
            if diff:
                raise AssertionError(f'{diff} fixtures were set in JSON data, '
                                     'but not found in test')
            parameters = value if isinstance(value, list) else [value]
            metafunc.parametrize(key, parameters)
    data = get_test_data(metafunc.definition)
    if data:
        parametrize(data)


# !/usr/bin/python3
# --coding:utf-8--
# @Author:ACHIEVE_DREAM
# @Time: 2020Äê10ÔÂ31ÈÕ18Ê±
# @File: main.py
from wsgiref.simple_server import make_server

def demo_app(environ, start_response):
    from io import StringIO
    stdout = StringIO()
    print("Hello world!", file=stdout)
    print(file=stdout)
    h = sorted(environ.items())
    for k, v in h:
        print(k, '=', repr(v), file=stdout)
    start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')])
    return [stdout.getvalue().encode("utf-8")]

if __name__ == '__main__':
    with make_server('', 8000, demo_app) as httpd:
        sa = httpd.socket.getsockname()
        print("Serving HTTP on", sa[0], "port", sa[1], "...")
        import webbrowser

        webbrowser.open('http://localhost:8000/xyz?abc')
        httpd.serve_forever()


#type of triangle
print("\n Enter the three sides of the triangle :")
a=int(input())
b=int(input())
c=int(input())
if(a==b)and(b==c)and(c==a):
		print("\n It is equilateral triangle ")
elif(a==b)or(b==c)or(c==a):
	if(b!=c)or(a!=c)or(a!=b):
		print("\n It is isocleses triangle ")
else:
	print("\n It is a scalant triangle ")


# Definition for singly-linked list.
# 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        l3 = ListNode(0)
        cur = l3
        while l1 is not None and l2 is not None:
            if l1.val < l2.val:
                cur.next = l1
                cur = cur.next
                l1 = l1.next
            else:
                cur.next = l2
                cur = cur.next
                l2 = l2.next
        if l1 is None:
            cur.next = l2
        else:
            cur.next = l1
        return l3.next

if __name__ == '__main__':
    s = Solution()
    l1 = ListNode(3)
    l2 = ListNode(4)
    l3 = ListNode(5)
    l1.next = l2
    l2.next = l3
    l4 = ListNode(4)
    l5 = ListNode(5)
    l6 = ListNode(6)
    l4.next = l5
    l5.next = l6

    print(s.mergeTwoLists(l1, l4))


"""empty message

Revision ID: 36f434379f79
Revises: f3b98673cc02
Create Date: 2020-04-14 19:53:16.995042

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '36f434379f79'
down_revision = 'f3b98673cc02'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('VenueArtist')
    op.alter_column('Show', 'start_time',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('Show', 'start_time',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.create_table('VenueArtist',
    sa.Column('venue_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('artist_id', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['artist_id'], ['Artist.id'], name='VenueArtist_artist_id_fkey'),
    sa.ForeignKeyConstraint(['venue_id'], ['Venue.id'], name='VenueArtist_venue_id_fkey'),
    sa.PrimaryKeyConstraint('venue_id', 'artist_id', name='VenueArtist_pkey')
    )
    # ### end Alembic commands ###


from flask import Flask, request, session

from homer import what_would_homer_say_to

app = Flask(__name__)

@app.route('/', methods=['POST'])
def homer_response():

    if request.json.get('username'):
        # we have a new user, update the session
        session['username'] = request.json['username']

    username = session.get('username', 'You')
    homers_reply = what_would_homer_say_to(request.json['message'])
    return ''.join([
        username,
        ': ',
        request.json['message'],
        '\nHomer: ',
        homers_reply,
    ])

# Don't commit open source code with your secret app key.
# Use os.urandom() to generate a secret key and paste here before running the
# app.
app.secret_key = ''


from data_solve import Parse

from get_vect import per_vect

class start():

    def __init__(self,file_name):
        self.file_name = file_name

    def start_bayes(self):
        parse = Parse(self.file_name)
        per_message_words,vocabulary_list,classes = parse.parse_file()
        doc_vect = []

        for per_message_word in per_message_words:
            vect = per_vect(per_message_word,vocabulary_list)
            doc_vect.append(vect.get_vect())

        return doc_vect,classes


def letter(word):
    used = []
    nums = []

    for char in word:
        num = 0
        for ch2 in word:
            if ch2 in used:
                continue
            if char == ch2:
                num += 1
        used.append(char)
        nums.append(num)

    return nums

two = 0
three = 0

file = open("input.txt", "r")

for line in file:
    if 2 in letter(line):
        two += 1
    if 3 in letter(line):
        three += 1

print(two)
print(three)


import xml.etree.ElementTree as ET

from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from rdkit import Chem
from rdkit.Chem import AllChem, Draw, Atom, rdFMCS, rdDepictor
import re
from rdkit.Chem.Draw.MolDrawing import DrawingOptions
from rdkit.Chem.Draw import rdMolDraw2D
from rest_framework.authtoken.models import Token
from frag.utils.network_utils import get_fragments, canon_input

ISO_COLOUR_MAP = {
    100: (1, 0, 0),
    101: (0, 1, 0),
    102: (0, 0, 1),
    103: (1, 0, 1),
    104: (1, 1, 0),
    105: (0, 1, 1),
    106: (0.5, 0.5, 0.5),
    107: (1, 0.5, 1),
}

def get_token(request):
    """
    Get the authentication token for a given request.
    Should just return an un-authenticated user token if nothing.
    :param request:
    :return:
    """
    try:
        user = User.objects.get(username=request.user)
        token, created = Token.objects.get_or_create(user=user)
        return token.key
    except ObjectDoesNotExist:
        return ""

def _transparentsvg(svg):
    """
    Give an SVG a white background
    :param svg:
    :return:
    """
    # Make the white background transparent
    tree = ET.fromstring(svg)
    rect = tree.find("rect")
    rect.set("style", rect.get("style").replace("#FFFFFF", "none"))
    # Recover some missing attributes for correct browser rendering
    tree.set("version", "1.1")
    tree.set("xmlns", "http://www.w3.org/2000/svg")
    tree.set("xmlns:rdkit", "http://www.rdkit.org/xml")
    tree.set("xmlns:xlink", "http://www.w3.org/1999/xlink")
    return '<?xml version="1.0" encoding="UTF-8"?>' + ET.tostring(tree).strip()

def highlight_diff(prb_mol, ref_mol, width, height):
    """
    Draw a molecule (prb_mol) with the differences from a reference model highlighted
    :param prb_mol: smiles of the probe molecule
    :param ref_mol: smiles of the reference molecule
    :param width: output image width
    :param height: output image height
    :return: svg string of the image
    """
    if not width:
        width = 200
    if not height:
        height = 200

    mols = [Chem.MolFromSmiles(prb_mol), Chem.MolFromSmiles(ref_mol)]
    [Chem.Kekulize(m) for m in mols]
    match = Chem.rdFMCS.FindMCS(mols, ringMatchesRingOnly=True, completeRingsOnly=True)
    match_mol = Chem.MolFromSmarts(match.smartsString)
    rdDepictor.Compute2DCoords(mols[0])
    unconserved = [i for i in range(mols[0].GetNumAtoms()) if i not in mols[0].GetSubstructMatch(match_mol)]

    drawer = rdMolDraw2D.MolDraw2DSVG(width, height)
    drawer.DrawMolecule(mols[0], highlightAtoms=unconserved)
    drawer.FinishDrawing()
    svg = drawer.GetDrawingText()

    return svg

def draw_mol(
        smiles,
        height=34,
        width=150,
        img_type=None,
        highlightAtoms=[],
        atomcolors=[],
        highlightBonds=[],
        bondcolors={},
        mol=None,
):
    """
    Draw a molecule from a smiles
    :param smiles: the SMILES to render
    :param height: the height in px
    :param width: the width in px
    :return: an SVG as a string of the inage
    """
    if mol is None:
        mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return "None Mol"
    # AllChem.Compute2DCoords(mol)
    Chem.Kekulize(mol)
    if not height:
        height = 200
    if not width:
        width = 200
    if img_type == "png":
        img = Draw.MolToImage(
            mol,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        img = img.convert("RGBA")
        datas = img.getdata()
        newData = []
        for item in datas:
            if item[0] == 255 and item[1] == 255 and item[2] == 255:
                newData.append((255, 255, 255, 0))
            else:
                newData.append(item)
        img.putdata(newData)
        response = HttpResponse(content_type="image/png")
        img.save(response, "PNG")
        return response
    else:
        rdMolDraw2D.PrepareMolForDrawing(mol, wedgeBonds=False)
        drawer = rdMolDraw2D.MolDraw2DSVG(height, width)

        drawopt = drawer.drawOptions()
        drawopt.clearBackground = False
        drawopt.fixedBondLength = 30
        drawopt.padding = 0.01
        drawopt.bondLineWidth = 1
        drawopt.additionalAtomLabelPadding = 0.05

        drawer.SetFontSize(1)

        drawer.DrawMolecule(
            mol,
            highlightAtoms=highlightAtoms,
            highlightAtomColors=atomcolors,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )

        drawer.FinishDrawing()
        return drawer.GetDrawingText().replace("svg:", "").replace('stroke-width:2px', 'stroke-width:1.5px')

def parse_vectors(vector_list):
    return [int(x) for x in vector_list.split(",")]

def parse_bool(input_string):
    if input_string.lower() in ("yes", "true", "t", "y", "1"):
        return True
    elif input_string.lower() in ("no", "false", "f", "n", "0"):
        return False
    else:
        raise ValueError("Value not parsable")

def parse_atom_ids(input_list, mol):
    """
    List of the form id,id,isotope,addHs
    e.g. 1,2,104,True
    :param input_list:
    :param mol:
    :return:
    """
    spl_list = input_list.split(",")
    bond_ids = []
    atom_ids = []
    bond_colours = {}
    for i, data in enumerate(spl_list):
        list_len = 4
        if i % list_len in [0, 1]:
            atom_ids.append(int(spl_list[i]))
        if i % list_len == 2:
            iso = int(spl_list[i])
        if i % list_len == 3:
            add_hs = parse_bool(spl_list[i])
            atom_id_1 = atom_ids[0]
            atom_id_2 = atom_ids[1]
            if add_hs:
                mol = AllChem.AddHs(mol)
                # Replace the H with the atom id in atom_ids[0], atom_ids[1] with *
                h_atoms = [x for x in mol.GetAtoms() if x.GetAtomicNum() == 1]
                atom_remove = [
                    x.GetIdx() for x in h_atoms if x.GetIdx() in [atom_id_1, atom_id_2]
                ][0]
                ed_mol = AllChem.EditableMol(mol)
                # Remove the other Hs
                ed_mol.ReplaceAtom(atom_remove, Atom(0))
                # Get a new editable molecule
                mol = ed_mol.GetMol()
                mol = Chem.MolFromSmiles(Chem.MolToSmiles(mol))
                # Record the new Atom Ids
                atom_ids = [
                    [x.GetBonds()[0].GetBeginAtomIdx(), x.GetBonds()[0].GetEndAtomIdx()]
                    for x in mol.GetAtoms()
                    if x.GetAtomicNum() == 0
                ][0]
                atom_id_1 = atom_ids[0]
                atom_id_2 = atom_ids[1]
            bond = mol.GetBondBetweenAtoms(atom_id_1, atom_id_2)
            bond_ids.append(bond.GetIdx())
            bond_colours[bond.GetIdx()] = ISO_COLOUR_MAP[iso]
            atom_ids = []
    return bond_ids, bond_colours, mol

def parse_xenons(input_smi):
    mol = Chem.MolFromSmiles(input_smi)
    e_mol = AllChem.EditableMol(mol)
    xenons = [atom for atom in mol.GetAtoms() if atom.GetAtomicNum() == 54]
    bond_ids = []
    bond_colours = {}
    for xe in xenons:
        bond_id = xe.GetBonds()[0].GetIdx()
        bond_ids.append(bond_id)
        if len(xenons) > 1:
            bond_colours[bond_id] = ISO_COLOUR_MAP[xe.GetIsotope()]
        else:
            bond_colours[bond_id] = ISO_COLOUR_MAP[101]
        e_mol.ReplaceAtom(xe.GetIdx(), Atom(0))
    return bond_ids, bond_colours, e_mol.GetMol()

def get_params(smiles, request):
    # try:
    smiles = canon_input(smiles)
    # except:
    #     smiles = ""
    height = None
    mol = None
    bond_id_list = []
    highlightBondColors = {}
    if "height" in request.GET:
        height = int(request.GET["height"])
    width = None
    if "width" in request.GET:
        width = int(request.GET["width"])
    if "atom_indices" in request.GET:
        mol = Chem.MolFromSmiles(smiles)
        bond_id_list, highlightBondColors, mol = parse_atom_ids(
            request.GET["atom_indices"], mol
        )
    if "Xe" in smiles:
        bond_id_list, highlightBondColors, mol = parse_xenons(smiles)
    img_type = request.GET.get("img_type", None)
    get_mol = draw_mol(
        smiles,
        width=width,
        height=height,
        img_type=img_type,
        highlightBonds=bond_id_list,
        mol=mol,
        bondcolors=highlightBondColors,
    )
    if type(get_mol) == HttpResponse:
        return get_mol
    return HttpResponse(get_mol)

def get_highlighted_diffs(request):
    prb_smiles = request.GET['prb_smiles']
    ref_smiles = request.GET['ref_smiles']
    height = None
    width = None
    if "height" in request.GET:
        height = int(request.GET["height"])
    if "width" in request.GET:
        width = int(request.GET["width"])
    return HttpResponse(highlight_diff(prb_mol=prb_smiles, ref_mol=ref_smiles, height=height, width=width))

def mol_view(request):
    if "smiles" in request.GET:
        smiles = request.GET["smiles"].rstrip(".svg")
        return get_params(smiles, request)
    else:
        return HttpResponse("Please insert SMILES")


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn.model_selection import train_test_split
# fotting knn to the trainging set
from sklearn.neighbors import KNeighborsClassifier
#scaling
from sklearn.preprocessing import StandardScaler

dataset = pd.read_csv("")
dataset = dataset.dropna()
dataset = dataset.reset_index(drop = True)

X = dataset.iloc[:, [0,1]].values
y = dataset.iloc[:, 9].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .25, random_state = 0)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

knn_scores = []

for k in range(1, 21):
    knn_classifier = KNeighborsClassifier(n_neighbors=k)
    knn_classifier.fit(X_train, y_train)

    knn_scores.append(knn_classifier.score(X_test, y_test))

plt.plot([k for k in range(1,21)], knn_scores, color = 'red')

classifier = KNeighborsClassifier(n_neighbors = 2, metric = "minkowski", p = 2)
classifier.fit(X_train, y_train)

classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)

scoring = (metrics.accuracy_score(y_test, y_pred))
print(scoring)


#        封装  继承  多态
#         员工类         || 父类  属性：名字 年龄 体重。。。工作方法
# ios  前端  后台  测试  || 子类  继承：名字 年龄 体重等
# 员工类都有 工作这个方法  子类继承后   每个子类的实例方法都不一样
# 继承同一个方法   方法里面的内容不一样    这就是多态

class Person():
    def __init__(self, name, height, age, sex, like):
        self.name = name
        self.height = height
        self.age = age
        self.sex = sex
        self.like = like

    def work(self, work):
        print("%s的工作是%s"%(self.name, work))

    def sing(self, gequ):
        print("%s正在唱 %s "%(self.name, gequ))

class Teacher(Person):
    def teach(self, say):
        print('我是%s老师,我可以传授你们一些知识'%self.name)
        print('作为一个老师我想说：%s'%say)

class Student(Person):
    def study(self):
        print("我是%s同学,我爱学习，学习使我快乐"%self.name)

class CTO(Person):
    def manager(self):
        print("在%s领导的管理下，技术部越来越好！"%self.name)

LiuQiong = Teacher("刘琼", 175, 18, "男", "泡妞")
LiZhengHua = Teacher("黎振华", 175, 18, "男", "唱歌")

print(LiuQiong.sex, LiuQiong.name, LiuQiong.height, LiuQiong.like, LiuQiong.sex)
print(LiZhengHua.name)
LiuQiong.sing('你是我的爱')
LiuQiong.work('编写前端代码,做一个合格的程序员')
LiuQiong.teach('你们要 好好学习,天天向上')
print('===================================')

WangKun = Student("王昆", 160, 18, "女", "撩汉")
WangKun.sing('啦啦啦阿拉， 我是小可爱')
WangKun.work('进行测试工作，发现bug')
WangKun.study()

print('===================================')
XieDaLao = CTO("谢双龙", 170, 18, "男", "学习")
XieDaLao.work('管理技术团队，指引技术方向')
XieDaLao.sing("啦啦啦啦啦啦，  哈哈哈啊哈哈")
XieDaLao.manager()