import sys
import json
from ViterbiAlgorithm import *
from featureExtraction import *

#Read the json files - input name of file
def readJson(name):
    with open(path.relpath("ViterbiData/" + name +'.json')) as f:
        data = json.load(f)
    return data

def readData(i):
    trans_p = readJson("trans_p"+str(i))
    emit_p = readJson("emit_p"+str(i))
    states = readJson("states"+str(i))
    start_p = readJson("start_p"+str(i))
    return trans_p,emit_p,states,start_p

#Takes in the text and the number of different models to iterate over.
def main(text,numOfMod):
    print("Running Viterbi on " + str(numOfMod) + " different models:");
    #Splitting the input observations into tuple e.g ('Joe', 'is', 'a', 'person')
    text = text.replace(".","") #Removing .
    text = text.replace(",","")
    text = text.replace(":","")

    obs = tuple(text.split(' '))

    bigV = []

    #Run the viterbi algorithm over the different models and save the result in finV
    for i in range(numOfMod):
        trans_p,emit_p,states,start_p = readData(i)
        #Run the viterbi algorithm
        V = viterbi(obs,states,start_p,trans_p,emit_p);
        result = []
        for j in range(0,len(V)):
            result.append(max(V[j],key=V[j].get))
        bigV.append(result);
        print("Iteration" + str(i) + " is completed.")

    mlp = []

    #Based on the different models, find the most likely path
    for i in range(len(obs)):
        temp = [];
        for j in range(numOfMod):
            temp.append(bigV[j][i])
        mlp.append(temp);

    result = []
    for i in range(len(obs)):
        result.append(max(set(mlp[i]),key=mlp[i].count))
    print("Viterbi result:")
    for i in range(len(result)):
        print(obs[i],result[i])
    return result;

main("This is a test. Five is a number",3)


#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
"""
依赖组件
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pymongo
"""
import sys, os
import json
from getopt  import getopt
from time    import time
from pymongo import MongoClient
#===================================================
usage = '''
Walnut 对象索引恢复程序 (采用 MongoDB.$refs 存储引用)

# 参数表

    --limit 10      # 最多10个桶，0 表示不限，默认 10
    --skip  0       # 跳过前面多少个桶，默认 0
    --conf  {..}    # 指定数据库连接方式，以及过滤条件等

# 调用示例

    python rebuild-refs-mongo --limit 5 --skip 10 --conf rebuild.json
'''
#===================================================
VERSION = "1.0"
def printVersion():
  print(VERSION)
def printUsage():
  print(usage)
#===================================================
# 获取命令行参数
limit = 10
skip  = 0
config = {}
flt = None
opts, args = getopt(sys.argv[1:], "hv", [
    "version","limit=","skip=", "conf="
  ])
for op, val in opts:
  if "-h" == op:
    printUsage()
    sys.exit()
  elif "--version" == op or "-v" == op:
    printVersion()
    sys.exit()
  elif "--limit" == op:
    limit = int(val)
  elif "--skip" == op:
    skip = int(val)
  elif "--conf" == op:
    fph = os.path.abspath(val)
    with open(fph, "r") as f:
      txt = f.read()
      config = json.loads(txt)
#===================================================
# Mongo 数据库连接
_mongo = config['mongo']
# 数据过滤条件
flt = config['filter']
#===================================================
print("#" * 60)
print("#")
print("#   Walnut 索引恢复程序(采用 MongoDB.$refs 存储引用)")
print("#   > 根据 MongoDB 恢复 Redis 索引")
print("#  ", VERSION)
print("#")
print("#" * 60)
if flt:
  print("过滤条件：")
  print(json.dumps(flt, indent=3))
else:
  print("~ 无过滤条件 ~")
print("#" * 60)
print("# MongoDB:")
print(json.dumps(_mongo, indent=3))
print("#" * 60)
#===================================================
#
# 连接 MongoDB
#
monUrl = 'mongodb://%s:%d/'%(_mongo['host'], _mongo['port'])
print("准备连接 MongoDB: ", monUrl)
monCli = MongoClient(monUrl)
monDB  = monCli[_mongo['db']]
print("...成功的连接了 MongoDB")
#===================================================
# 准备计数
now = time() * 1000
#===================================================
#
# 根据 MONGO 的索引寻找桶
#
print("\n开始从 MongoDB 查找索引 ...")
print("-" * 60)
count = 0
sha1_count = 0
objs = monDB['obj'].find(
  filter = flt,
  skip   = skip,
  limit  = limit
)
refCollection  = monDB['refs']
#-----------------------------------------------------
for obj in objs:
  oid  = obj.get('id')
  onm  = obj.get('nm')
  sha1 = obj.get('sha1')
  race = obj.get('race')
  #-------------------------------------
  # 看来有些对象是坏的
  if not oid or not onm:
    continue
  #-------------------------------------
  # 打印一下
  print("\n%d. %s %s : '%s'"%(count, oid, sha1, onm))
  count += 1
  #-------------------------------------
  # 无视
  if "DIR" == race or not sha1:
    print("  ~~ skip ~~")
    continue

  print("  Add to Refs")
  sha1_count += 1
  #-------------------------------------
  # 记录到索引中
  refDoc = {
    "tid" : sha1,
    "rid" : oid
  }
  refCollection.update_one(refDoc, {'$setOnInsert': refDoc}, upsert=True)
  #-------------------------------------
  # 计数
# end of for
#===================================================
# 结束
stop = time() * 1000
duInMs = stop - now
duInS  = int(round(duInMs/1000))
#
# 打印一下
#
print("-"*60)
print("Found %d objs (%d files)"%(count, sha1_count))
print("-"*60)
print("All done in %d seconds (%dms)\n"%(duInS, duInMs))


def thirdMax(nums):
    nums = list(set(nums))
    if len(nums) < 3:
        return max(nums)
    nums.sort(reverse=True)
    count = 0
    for i in range(len(nums)):
        if count == 2:
            return nums[i]
        if i > 0 and nums[i] < nums[i-1]:
            count += 1
    return nums[0]


import sched
import time

s = sched.scheduler(time.time, time.sleep)

def main(sc):
    # Do something
    print("driver.title")

    # Intervals for function in sec
    sc.enter(10, 1, main, (sc,))

if __name__ == '__main__':
    # Initial sched starter
    s.enter(1, 1, main, (s,))
    s.run()


import numpy as np
import tensorflow as tf
# import tensorflow.compat.v1 as tf
import logging

# tf.compat.v1.disable_eager_execution()
np.random.seed(1)

#####################  hyper parameters  ####################
MAX_EPISODES = 1000
# MAX_EPISODES = 50000

LR_A = 0.001  # learning rate for actor
LR_C = 0.002  # learning rate for critic
# LR_A = 0.1  # learning rate for actor
# LR_C = 0.2  # learning rate for critic
GAMMA = 0.001  # optimal reward discount
# GAMMA = 0.999  # reward discount
TAU = 0.01  # soft replacement
VAR_MIN = 0.01
# MEMORY_CAPACITY = 5000
MEMORY_CAPACITY = 100
BATCH_SIZE = 64
OUTPUT_GRAPH = False

###############################  DDPG  ####################################
class DDPG(object):
    def __init__(self, a_dim, s_dim, a_bound):
        self.memory = np.zeros((MEMORY_CAPACITY, s_dim * 2 + a_dim + 1), dtype=np.float32)  # memory里存放当前和下一个state，动作和奖励
        self.pointer = 0
        self.sess = tf.Session()

        self.a_dim, self.s_dim, self.a_bound = a_dim, s_dim, a_bound,
        self.S = tf.placeholder(tf.float32, [None, s_dim], 's')  # 输入
        self.S_ = tf.placeholder(tf.float32, [None, s_dim], 's_')
        self.R = tf.placeholder(tf.float32, [None, 1], 'r')

        with tf.variable_scope('Actor'):
            self.a = self._build_a(self.S, scope='eval', trainable=True)
            a_ = self._build_a(self.S_, scope='target', trainable=False)
        with tf.variable_scope('Critic'):
            # assign self.a = a in memory when calculating q for td_error,
            # otherwise the self.a is from Actor when updating Actor
            q = self._build_c(self.S, self.a, scope='eval', trainable=True)
            q_ = self._build_c(self.S_, a_, scope='target', trainable=False)

        self.ae_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/eval')
        self.at_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/target')
        self.ce_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/eval')
        self.ct_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/target')

        # target net replacement
        self.soft_replace = [tf.assign(t, (1 - TAU) * t + TAU * e)
                             for t, e in zip(self.at_params + self.ct_params, self.ae_params + self.ce_params)]

        q_target = self.R + GAMMA * q_
        # in the feed_dic for the td_error, the self.a should change to actions in memory
        td_error = tf.losses.mean_squared_error(labels=q_target, predictions=q)
        self.ctrain = tf.train.AdamOptimizer(LR_C).minimize(td_error, var_list=self.ce_params)

        a_loss = - tf.reduce_mean(q)  # maximize the q
        self.atrain = tf.train.AdamOptimizer(LR_A).minimize(a_loss, var_list=self.ae_params)
        self.a_cost = []
        self.c_cost = []
        self.sess.run(tf.global_variables_initializer())

        if OUTPUT_GRAPH:
            tf.summary.FileWriter("logs/", self.sess.graph)

    def plot_cost(self):
        f1 = open("result/a_cost.csv", "w")
        for i in range(len(self.a_cost)):
            f1.write(str(i)+","+str(self.a_cost[i])+"\n")
        f1.close()

        f1 = open("result/c_cost.csv", "w")
        for i in range(len(self.c_cost)):
            f1.write(str(i)+","+str(self.c_cost[i])+"\n")
        f1.close()

    def choose_action(self, s):
        temp = self.sess.run(self.a, {self.S: s[np.newaxis, :]})
        return temp[0]

    def learn(self):
        self.sess.run(self.soft_replace)

        indices = np.random.choice(MEMORY_CAPACITY, size=BATCH_SIZE)
        bt = self.memory[indices, :]
        bs = bt[:, :self.s_dim]
        ba = bt[:, self.s_dim: self.s_dim + self.a_dim]
        br = bt[:, -self.s_dim - 1: -self.s_dim]
        bs_ = bt[:, -self.s_dim:]

        a_cost = self.sess.run(self.atrain, {self.S: bs})
        self.a_cost.append(a_cost)
        c_cost = self.sess.run(self.ctrain, {self.S: bs, self.a: ba, self.R: br, self.S_: bs_})
        self.c_cost.append(c_cost)

    def store_transition(self, s, a, r, s_):
        transition = np.hstack((s, a, [r], s_))
        # transition = np.hstack((s, [a], [r], s_))
        index = self.pointer % MEMORY_CAPACITY  # replace the old memory with new memory
        self.memory[index, :] = transition
        self.pointer += 1

    def _build_a(self, s, scope, trainable):
        with tf.variable_scope(scope):
            net = tf.layers.dense(s, 400, activation=tf.nn.relu6, name='l1', trainable=trainable)
            net = tf.layers.dense(net, 300, activation=tf.nn.relu6, name='l2', trainable=trainable)
            net = tf.layers.dense(net, 100, activation=tf.nn.tanh, name='l3', trainable=trainable)
            net = tf.layers.dense(net, 10, activation=tf.nn.relu, name='l4', trainable=trainable)
            a = tf.layers.dense(net, self.a_dim, activation=tf.nn.tanh, name='a', trainable=trainable)
            return tf.multiply(a, self.a_bound, name='scaled_a')

    def _build_c(self, s, a, scope, trainable):
        with tf.variable_scope(scope):
            n_l1 = 400
            w1_s = tf.get_variable('w1_s', [self.s_dim, n_l1], trainable=trainable)
            w1_a = tf.get_variable('w1_a', [self.a_dim, n_l1], trainable=trainable)
            b1 = tf.get_variable('b1', [1, n_l1], trainable=trainable)
            net = tf.nn.relu6(tf.matmul(s, w1_s) + tf.matmul(a, w1_a) + b1)
            net = tf.layers.dense(net, 300, activation=tf.nn.relu6, name='l2', trainable=trainable)
            net = tf.layers.dense(net, 200, activation=tf.nn.relu, name='l3', trainable=trainable)
            net = tf.layers.dense(net, 100, activation=tf.nn.relu, name='l5', trainable=trainable)
            net = tf.layers.dense(net, 10, activation=tf.nn.relu, name='l4', trainable=trainable)
            return tf.layers.dense(net, 1, trainable=trainable)  # Q(s,a)

    def save_net(self):
        saver = tf.train.Saver()
        now = "DDPGTO"
        # time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
        fname="models/"+now+".ckpt"
        # fname = "baselinemodels/DRLTOmodel.ckpt"
        save_path = saver.save(self.sess, fname)
        print("Save to path: ", save_path)

    def restore_net(self):
        saver = tf.train.Saver()
        now = "DDPGTO"
        # time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
        fname="models/"+now+".ckpt"
        # fname = "baselinemodels/DRLTOmodel.ckpt"
        saver.restore(self.sess, fname)
        print("Model restored.")
        print('Initialized')


'''
PROGRAM-REQUEST
(a) Write a function that takes as its argument a non-negative integer and
prints out that number of terms of the Fibonacci Series.
This function should not return an explicit value.
(b) Write a program that prompts the user for an integer and checks that
the number entered is non-negative.
If it is, it calls the function defined in part(a);
if not, it prints out an appropriate error message.

PSEUDO-CODE
# a. Function
Define function fibonacci: fib(x)
# x is the limit of terms
    Initialize index_a to 0, index_b to 1, index_fib to 0, i to 0
    For i in range (0, x):
        index_fib = index_b + index_a
        print(index_b, " ", end = "")
        index_a, index_b = index_b, index_fib
# b. Program
number= int(input('Enter a number (an int >= 0): '))
if number >= 0:
    print('The fibonacci terms are: ')
    fib(number)
else:
    print('Error: can only return fibonacci numbers with a non-negative number (>= 0')
'''
# a. Function
def fib(x):
    index_a, index_b, index_fib, i = 0, 1, 0, 0
    for i in range (0, x):
        index_fib = index_b + index_a
        print(index_b, " ", end = "")
        index_a, index_b = index_b, index_fib
# b. Program
number= int(input('Enter a number (an int >= 0): '))
if number >= 0:
    print('The fibonacci terms are: ')
    fib(number)
else:
    print('Error: can only calcualte the factorial of a non-negative number (>= 0')


# We’re given a sorted array(ascending order) with unique integer elements.
# Write an algorithm to create a binary search tree with minimal height.
# Credits https://pythonwife.com/interview-questions-on-trees-in-python/

#Initializing the Binary Search Tree class
class BSTNode:
    def __init__(self, data=None, left = None, right= None):
        self.data = data
        self.left = left
        self.right = right

#Function to create a binary search tree of minimum height
def minimalTree(sortedArray):
    if len(sortedArray) == 0:
        return None
    if len(sortedArray) == 1:
        return BSTNode(sortedArray[0])
    mid = int(len(sortedArray)/2)
    left = minimalTree(sortedArray[:mid])
    right = minimalTree(sortedArray[mid+1:])
    return BSTNode(sortedArray[mid], left, right)

sortedArray = [1,2,3,4,5,6,7,8,9]
bst = minimalTree(sortedArray)


"""
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example,
the array nums = [0,1,4,4,5,6,7] might become:

[4,5,6,7,0,1,4] if it was rotated 4 times.
[0,1,4,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the
 array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.

You must decrease the overall operation steps as much as possible.
"""

def findMin(nums):
    left = 0
    right = len(nums) - 1
    if len(nums) == 1:
        return nums[0]
    if nums[0] < nums[right]:
        return nums[0]
    if len(set(nums)) == 1:
        return nums[0]

    while left < right:
        mid = (left + right) // 2
        while left + 1 < len(nums) and nums[left] == nums[left + 1]:
            left += 1
        if left == len(nums):
            return None
        while right >= 0 and nums[right] == nums[right - 1]:
            right -= 1
        if right < 0:
            return None
        if nums[mid] > nums[mid + 1]:
            return nums[mid + 1]
        elif nums[mid] < nums[left]:
            right = mid
        elif nums[mid] > nums[right]:
            left = mid

LeetCode/Sorting/154. [hard] Find Minimum In Rotated Sorted Array II.py


import copy
import sys
import math
import random

def standardSolution_LIS(a):
    """
    别人的算法，用来检验正确性
    """
    L = []
    for (k,v) in enumerate(a):
        L.append(max([L[i] for (i,n) in enumerate(a[:k]) if n<v] or [[]], key=len) + [v])
    return max(L, key=len)

def longestIncrementalSubsequence(l):
    '''找出l的最长单调递增子序列'''
    n = len(l)  #l的长度
    x = [copy.copy([0,-1]) for i in range(n)]  #x[i]=[a, b]表示以l[i]结尾的最长单调递增子序列，长度为a，倒数第二个元素为l[b]

    x[0] = [1, -2]  #以l[0]结尾的子序列长度只能为1，没有倒数第二个元素

    for i in range(1, n):
        maxLength = -math.inf
        lastIndexOfSubsequenceThatHasMaxLength = -1
        for j in range(0, i):
            if x[j][0] > maxLength and l[j] < l[i]:
                maxLength = x[j][0]
                lastIndexOfSubsequenceThatHasMaxLength = j  #找到了一个l[i]可以加到后面去的子序列
        if lastIndexOfSubsequenceThatHasMaxLength == -1:
            #没有找到可以让l[i]加到后面的子序列，故l[i]自成一序列
            x[i] = [1, -2]
        else:
            x[i] = [x[lastIndexOfSubsequenceThatHasMaxLength][0] + 1, lastIndexOfSubsequenceThatHasMaxLength]

    maxLength = -math.inf
    lastIndexOfSubsequenceThatHasMaxLength = -1
    for i in range(0, n):
        if x[i][0] > maxLength:
            maxLength = x[i][0]
            lastIndexOfSubsequenceThatHasMaxLength = i

    assert(lastIndexOfSubsequenceThatHasMaxLength != -1)

    resultSubsequence = []
    currentIndex = lastIndexOfSubsequenceThatHasMaxLength

    while(currentIndex != -2):
        resultSubsequence[0:0] = [l[currentIndex]]
        currentIndex = x[currentIndex][1]

    return (maxLength, resultSubsequence)

def binaryReplace(l, LISLeastEndIndex, start, end, i):
    currStart = start
    currEnd = end
    if currStart == currEnd:
        LISLeastEndIndex[currStart] = i
        return currStart
    if l[i] > l[LISLeastEndIndex[end]]:
        return -1
    while(True):
        if currStart == currEnd:
            LISLeastEndIndex[currStart] = i
            return currStart
        pivot = (currStart + currEnd) // 2
        if l[i] == l[LISLeastEndIndex[pivot]]:
            return pivot
        if l[i] < l[LISLeastEndIndex[pivot]]:
            currEnd = pivot
        if l[i] > l[LISLeastEndIndex[pivot]]:
            currStart = pivot + 1

def longestIncrementalSubsequence_NlgN(l):
    '''找出l的最长单调递增子序列，NlgN 时间复杂度'''
    LISLeastEndIndex = [-1] * len(l)
    LISPrevIndex = [-1] * len(l)
    LISPointer = 0
    for i in range(len(l)):
        if (i == 0):
            LISLeastEndIndex[LISPointer] = 0
            LISPointer = LISPointer + 1
        elif (l[i] > l[LISLeastEndIndex[LISPointer - 1]]):
            LISLeastEndIndex[LISPointer] = i
            LISPrevIndex[i] = LISLeastEndIndex[LISPointer - 1]
            LISPointer = LISPointer + 1
        else:
            replacedIndex = binaryReplace(l, LISLeastEndIndex, 0, LISPointer - 1, i)
            if replacedIndex >= 0:
                LISPrevIndex[i] = LISLeastEndIndex[replacedIndex - 1]
            else:
                LISPrevIndex[i] = -1

    subseqLastI = LISLeastEndIndex[LISPointer - 1]
    currI = subseqLastI
    subseq = []
    while(currI >= 0):
        subseq[0:0] = [l[currI]]
        currI = LISPrevIndex[currI]

    return (LISPointer, subseq)

def main():
    testTimeNo = 1000
    listMaxLength = 100
    correctNo = 0
    for k in range(testTimeNo):
        currLen = random.randint(0, listMaxLength) + 1
        if(k == 0):
            currLen = 10
        l = [random.randint(-listMaxLength, listMaxLength) for i in range(currLen)]

        myAnswerMaxLength, myAnswerSubsequence = longestIncrementalSubsequence_NlgN(l)
        standardSubsequence = (standardSolution_LIS(l))
        standardMaxLength = len(standardSubsequence)
        if(k == 0):
            print("第一次测试题面：", l)
            print("我的答案的长度：", myAnswerMaxLength)
            print("我的答案的最长递增子序列：", myAnswerSubsequence)
            print("标准答案的长度：", standardMaxLength)
            print("标准答案的最长递增子序列：", standardSubsequence)
            print("{}".format(["错误", "正确"][myAnswerMaxLength == standardMaxLength]))
        #if(myAnswerSubsequence == standardSubsequence):
        if(myAnswerMaxLength == standardMaxLength):
            correctNo = correctNo + 1
        print("经过 {} 次测试后，有 {} 次为正确。".format(k+1, correctNo), end='\r')

    print("\n运行完成。")

main()


import os
import sys
import subprocess
import platform
from distutils.core import setup#, Extension
import distutils.command.install_lib
#from setuptools import setup, find_packages

def sp(cmd):
    '''
    Call shell cmd or software and return its stdout
    '''
    a=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell='TRUE')
    ac = a.communicate()
    return ac

def compile_bedtools():
    curdir = os.getcwd()
    os.chdir('refpackage/bedtools')
    sp('make 1>/dev/null 2>&1 ')
    #sp('chmod 755 *')
    os.chdir(curdir)

def check_bedtools():
    checkhandle = sp('which bedtools')
    if checkhandle[0].strip() == "":
        return 0
    else:
        return 1
def check_R():
    checkhandle = sp('which Rscript')
    if checkhandle[0].strip() == "":
        return 0
    else:
        return 1

class my_install_lib(distutils.command.install_lib.install_lib):
    def run(self):
        distutils.command.install_lib.install_lib.run(self)
        mode = 755
        # here we start with doing our overriding and private magic ..
        for filepath in self.get_outputs():
            if "bigWigSummary" in filepath or "bedtools" in filepath:
            #if self.install_scripts in filepath:
            #    log.info("Overriding setuptools mode of scripts ...")
            #    log.info("Changing ownership of %s to uid:%s gid %s" %
            #             (filepath, uid, gid))
            #    os.chown(filepath, uid, gid)
            #    log.info("Changing permissions of %s to %s" %
            #             (filepath, oct(mode)))
                os.chmod(filepath, mode)

def main():
#    if sys.version_info[0] != 2 or sys.version_info[1] < 7:
#	    print >> sys.stderr, "ERROR: ncHMR_detector requires Python 2.7"
#	    sys.exit()
    has_R = check_R()
    if has_R == 0:
	    print("ERROR: ncHMR_detector requires R & Rscript under default PATH", file=sys.stderr)
	    sys.exit()

    OS = platform.system()
    if OS == "Linux":
        bwsum_software = "bigWigSummary_linux"
    elif OS == "Darwin":
        bwsum_software = "bigWigSummary_mac"
    else:
        wlog("detected system is nither linux nor mac, try linux version of bigWigSummary",logfile)
        bwsum_software = "bigWigSummary_linux"

#    has_bedtools = check_bedtools()
    has_bedtools=0
    print('Intalling ncHMR_detector, may take "several" minutes')
    if has_bedtools == 0:
        compile_bedtools()
        sp('mv refpackage/bedtools/bin/bedtools lib/')
        setup(name="HMRpipe",
              version="1.3",
              description="ncHMR detector: a computational framework to systematically reveal non-classical functions of histone-modification regulators",
              author='Shengen Hu',
              author_email='Tarelahu@gmail.com',
              url='https://github.com/Tarela/ncHMR_detector.git',
              package_dir={'HMRpipe' : 'lib'},
              packages=['HMRpipe'],
              package_data={'HMRpipe': ['%s'%bwsum_software,'bedtools',#'Config/template.conf',
                                      #'Rscript/analysis.r',
                                      #'Rscript/individual_qc.r',
                                      #'Rscript/readsbulkQC.r',
                                      #'Rscript/detectNonCanonical.r'
                                         ]},
              scripts=['bin/ncHMR_detector_py3'],#,'refpackage/bedtools/bin/bedtools','refpackage/bwsummary/%s'%bwsum_software],

              classifiers=[
            'Development Status :: version1.3 finish',
            'Environment :: Console',
            'Intended Audience :: Developers',
            'License :: OSI Approved :: Artistic License',
            'Operating System :: POSIX',
            'Programming Language :: Python',
            'Topic :: pipeline',
            ],
              requires=[],
              cmdclass={'install_lib':my_install_lib}
          )
        print('bedtools is not detected under default PATH, bedtools is also installed')
        #print('Installation of ncHMR_detector is DONE')

    else:
        setup(name="HMRpipe",
              version="1.3",
              description="ncHMR detector: a computational framework to systematically reveal non-classical functions of histone-modification regulators",
              author='Shengen Hu',
              author_email='Tarelahu@gmail.com',
              url='https://github.com/Tarela/ncHMR_detector.git',
              package_dir={'HMRpipe' : 'lib'},
              packages=['HMRpipe'],
              #package_data={}
              package_data={'HMRpipe': ['%s'%bwsum_software]},#,#'Config/template.conf',
                                      #'Rscript/analysis.r',
                                      #'Rscript/individual_qc.r',
                                      #'Rscript/readsbulkQC.r',
                                      #'Rscript/detectNonCanonical.r'
                                      #   ]},
              #scripts=['bin/ncHMR_detector_py3','refpackage/bwsummary/%s'%bwsum_software],
              scripts=['bin/ncHMR_detector_py3'],#,'refpackage/bwsummary/%s'%bwsum_software],
              #data_files=[('/Users/sh8tv/bin',['refpackage/bwsummary/%s'%bwsum_software])],
              classifiers=[
            'Development Status :: version1.3 finish',
            'Environment :: Console',
            'Intended Audience :: Developers',
            'License :: OSI Approved :: Artistic License',
            'Operating System :: POSIX',
            'Programming Language :: Python',
            'Topic :: pipeline',
            ],
              requires=[],
              cmdclass={'install_lib':my_install_lib}
          )

    #import HMRpipe

    print('Installation of ncHMR_detector is DONE')

if __name__ == '__main__':
    main()


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

from os.path import split, join, normpath, isfile
from tempfile import mkdtemp
from shutil import rmtree
import sys
import unittest

magic_number = 5
magic_text = "It's pure magic."

def _good_simple():
    return magic_number

def _good_io(f):
    f.write(magic_text)
    f.flush()
    return (magic_number, 3)

def _bad_io(badfile):
    with open(badfile, 'w') as f:
        f.write(magic_text)
    return magic_number

def _do_import():
    import base64

class TestJailedProcess(unittest.TestCase):

    def setUp(self):
        self.path = mkdtemp(prefix='prctljail_')
        self.good_path = join(self.path, "good.txt")
        self.bad_path = join(self.path, "bad.txt")

    def test_valid_simple(self):
        a = JailedProcess(_good_simple)
        ret = a.run()
        self.assertEqual(ret, 0)

    def test_valid_io(self):
        with open(self.good_path, 'w') as f:
            a = JailedProcess(_good_io, args=[f])
            ret = a.run()
            self.assertEqual(ret, 0)
        self.assertTrue(isfile(self.good_path))

        with open(self.good_path, 'r') as f:
            data = f.read()

        self.assertEqual(data, magic_text)

    def test_invalid_import(self):
        a = JailedProcess(_do_import)
        ret = a.run()
        self.assertEqual(ret, 9)

    def test_invalid_io(self):
        a = JailedProcess(_bad_io, args=[self.bad_path])
        ret = a.run()
        self.assertEqual(ret, 9)

    def tearDown(self):
        rmtree(self.path)

if __name__ == '__main__':
    base = split(sys.argv[0])[0]
    path = normpath(join(base, "../src/"))
    sys.path.insert(0, path)
    from prctljail import JailedProcess
    if JailedProcess.isAvailable():
        unittest.main()
    else:
        sys.stderr.write("Sorry, this feature isn't available on your\
 platform")


import datetime
import json
import os
import shutil
import sys
from json import JSONDecodeError

import requests

import console
import util

proxies = None
auth = None

base_cache_dir = './checkConfig'
clear_cache = 'none'
cacheTimeout = 365

class CardRetrievalError(Exception):
    def __init__(self, message, card_name, error_code):
        super(CardRetrievalError, self).__init__(message)
        self.cardName = card_name
        self.errorCode = error_code

class ConfigurationError(Exception):
    def __init__(self, message):
        super(ConfigurationError, self).__init__(message)

def get_cache_dir():
    base_dir = os.path.join(os.path.dirname(sys.argv[0]), base_cache_dir, ".scryfallCache")
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
    return base_dir

def flushCache():
    try:
        shutil.rmtree(get_cache_dir())
    except OSError as e:
        print("Error where clearing cache directory at " + get_cache_dir() + ": %s - %s." % (e.filename, e.strerror))

def initCache(collection):
    last_length = 0
    count = 1

    for card in collection:

        status_line = 'Fetching card info (' + str(count) + '/' + str(len(collection)) + ', ...' + str(
            collection[card].sourceFile)[-50:-2] + '): ' + card + " ..."

        count += 1
        currentLength = len(status_line)
        if currentLength < last_length:
            status_line = status_line + (last_length - currentLength) * ' '
        # newline before doing "status"
        if last_length == 0:
            sys.stdout.write('\n')
        last_length = currentLength

        sys.stdout.write('\r' + status_line)
        sys.stdout.flush()
    doneMessage = ''
    sys.stdout.write('\r' + doneMessage + (last_length - len(doneMessage)) * " " + '\r')
    sys.stdout.flush()

def fetchCardJson(card, json_file, retry_times=3):
    response = requests.get("http://api.scryfall.com/cards/named", params={'exact': card.techName}, proxies=proxies,
                            auth=auth)
    fuzzy_result = False
    if response.status_code == 404:
        print()
        print(console.CRED + "Card '" + card.techName + "' (" + util.absolutePaths(
            card.sourceFile) + ") Was not found in scryfall using exact search." + console.CEND + " Trying fuzzy search.")
        response = requests.get("http://api.scryfall.com/cards/named", params={'fuzzy': card.techName}, proxies=proxies,
                                auth=auth)
        fuzzy_result = True
        if response.status_code < 400:
            print("\'" + card.techName + "\' found as \'" + response.json()[
                "name"] + "\'. " + console.CRED + "Fix files " + util.absolutePaths(card.sourceFile) + console.CEND)
    if (response.status_code == 503 or response.status_code == 504) and retry_times > 0:
        sys.stderr.write('Retrying ' + response.url)
        fetchCardJson(card, json_file, retry_times - 1)
    elif response.status_code >= 400 or retry_times == 0:
        raise CardRetrievalError(
            'Bad response ' + str(response.status_code) + ' for ' + card.techName + "' (" + util.absolutePaths(
                card.sourceFile) + ")",
            card.techName, response.status_code)
    if not fuzzy_result:
        with open(json_file, 'w') as f:
            json.dump(response.json(), f)
    return response.json()

def getCachedCardJson(card):
    jsonFile = os.path.join(get_cache_dir(), util.cleanFilename(card) + ".json")
    if os.path.exists(jsonFile):
        fileAge = datetime.date.today() - datetime.date.fromtimestamp(os.path.getmtime(jsonFile))

        if (clear_cache == 'always' or (clear_cache == 'timeout' and fileAge.days > cacheTimeout) or (
                clear_cache == 'price' and fileAge.days > 1)):
            return fetchCardJson(card, jsonFile)
        else:
            # print("Loading cached " + jsonFile)
            with open(jsonFile, encoding='utf-8') as json_data:
                try:
                    return json.load(json_data)
                except JSONDecodeError:
                    print("Deleting and retrying invalid " + jsonFile)
                    json_data.close()
                    os.remove(jsonFile)
                    return fetchCardJson(card, jsonFile)
    else:
        # print("Loading online " + jsonFile)
        return fetchCardJson(card, jsonFile)

def searchByCard(card):
    url = card.jsonData['prints_search_uri']

    json_file = os.path.join(get_cache_dir(), card.jsonData["oracle_id"] + ".prints.json")
    if os.path.exists(json_file):
        file_age = datetime.date.today() - datetime.date.fromtimestamp(os.path.getmtime(json_file))

        if (clear_cache == 'always' or (clear_cache == 'timeout' and file_age.days > cacheTimeout) or (
                clear_cache == 'price' and file_age.days > 1)):
            return fetchCardJson(card, json_file)
        else:
            # print("Loading cached " + jsonFile)
            with open(json_file, encoding='utf-8') as json_data:
                try:
                    return json.load(json_data)
                except JSONDecodeError:
                    print("Deleting and retrying invalid " + json_file)
                    json_data.close()
                    os.remove(json_file)
                    return searchByCard(card)

    response = requests.get(url, proxies=proxies, auth=auth)

    foundCardsJson = []

    while response is not None and response.status_code == 200:

        jsonResponse = response.json()

        for card in jsonResponse['data']:
            foundCardsJson.append(card)

        if jsonResponse['has_more']:
            response = requests.get(jsonResponse['next_page'], proxies=proxies, auth=auth)
        else:
            response = None

    with open(json_file, 'w') as f:
        json.dump(foundCardsJson, f)

    return foundCardsJson

def search(query):
    response = requests.get('https://api.scryfall.com/cards/search', params={'q': query}, proxies=proxies, auth=auth)

    foundCardNames = []

    while response is not None and response.status_code == 200:

        jsonResponse = response.json()

        for card in jsonResponse['data']:
            foundCardNames.append(card['name'])

        if jsonResponse['has_more']:
            response = requests.get(jsonResponse['next_page'], proxies=proxies, auth=auth)
        else:
            response = None

    return foundCardNames

def getTokenByUrl(url):
    response = requests.get(url, proxies=proxies, auth=auth)
    if response is not None and response.status_code == 200:
        return response.json()
    else:
        return None

def get_set_data(set_code):
    json_file = os.path.join(get_cache_dir(), set_code + "_set.json")
    if os.path.exists(json_file):
        with open(json_file, encoding='utf-8') as json_data:
            try:
                return json.load(json_data)
            except JSONDecodeError:
                print("Deleting and retrying invalid " + json_file)
                json_data.close()
                os.remove(json_file)
                return get_set_data(set_code)
    else:
        response = requests.get('https://api.scryfall.com/sets/' + set_code, proxies=proxies, auth=auth)
        if response.status_code == 200:
            with open(json_file, 'w') as f:
                json.dump(response.json(), f)
            return response.json()
        else:
            return dict()


"""
Labeling every ns in the triple as 1, others as 0
"""
import numpy as np
import dataprocess.readdata
import dataprocess.ltpprocess
import re
import copy

def datalabeling(all_ns_sentences, all_pos, status):
    """
    Compare the triple entity with all ns sentence, sentences have entity as 1, else as 0
    :return: X: Sentences, Y: Labels
    """
    if status == "train":
        triples_dict, triple_sentences = dataprocess.readdata.read_triple("train")
    else:
        triples_dict, triple_sentences = dataprocess.readdata.read_triple("test")

    combinedSentence = {}
    combinedPoses = {}

    X_Sentences = {}
    X_Poses = {}
    Y_label = {}

    for index in triples_dict:
        entities = []
        X_Sentences.update({index: []})
        X_Poses.update({index: []})
        combinedSentence.update({index: []})
        combinedPoses.update({index: []})
        Y_label.update({index: []})
        sentences_segs = all_ns_sentences[str(index)]
        sentences_pos = all_pos[str(index)]

        for t in triples_dict[index]:
            entities.append(t[0])
            entities.append(t[2])

        entities = set(entities)

        # combine the ns word, save the long ns word.
        for sid in range(len(sentences_segs)):
            # A new sentence
            combinedSentence[index].append([])
            combinedPoses[index].append([])

            ns_word = ''
            positivetime = 0
            negtivetime = 0
            ns_wordlist = []
            for wid in range(len(sentences_segs[sid])):
                if sentences_pos[sid][wid] != 'ns':
                    combinedSentence[index][-1].append(sentences_segs[sid][wid])
                    combinedPoses[index][-1].append(sentences_pos[sid][wid])
                    continue

                # if ns, combine the word and pos
                while wid < len(sentences_segs[sid]) and sentences_pos[sid][wid] == 'ns':
                    ns_word += sentences_segs[sid][wid]
                    wid += 1
                wid -= 1

                combinedSentence[index][-1].append(ns_word)
                combinedPoses[index][-1].append('ns')

                ns_wordlist.append(ns_word)
                ns_word = ''

        # For each sentence, search the ns word, search the entity in the long ns word, if find, set positive.
        for sid in range(len(combinedSentence[index])):
            for wid in range(len(combinedSentence[index][sid])):
                if combinedPoses[index][sid][wid] == 'ns':
                    e_flag = 0
                    X_Sentences[index].append(copy.deepcopy(combinedSentence[index][sid]))
                    X_Sentences[index][-1][wid] = '↑' + combinedSentence[index][sid][wid] + '↑'
                    X_Poses[index].append(combinedPoses[index][sid])
                    for e in entities:
                        match = re.search(e, combinedSentence[index][sid][wid])
                        if match:
                            positivetime += 1
                            Y_label[index].append(1)
                            e_flag = 1
                            break
                    if e_flag == 0:
                        negtivetime += 1
                        Y_label[index].append(0)

    return X_Sentences, X_Poses, Y_label


import argparse
import os
import sys
import random
import struct
from time import sleep

import socket

parser = argparse.ArgumentParser()
parser.add_argument("interface", help="The sending interface", type=str)
parser.add_argument("--mac", help="which mac to send to (default = Broadcast)")
parser.add_argument("--interval", help="interval between packets in seconds (default = 0, 0 = as fast as possible)")
parser.add_argument("--count", help="How Manny packets to send in total (default = 0, 0 = infinite)")

args = parser.parse_args()

if os.geteuid():
    sys.stderr.write("You need to be root.")
    exit(1)

mac = "FF:FF:FF:FF:FF:FF"
if args.mac: mac = args.mac

interval = 0
if args.interval: interval = args.interval

count = 0
if args.count: count = int(args.count)

#default behaviour is "spoofing" your own ip with your own mac

prelogue = mac.replace(":", "").decode("hex")
epilogue = "1337FFFFFFFFFFFF".decode("hex")

sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
sock.bind((args.interface,0))

def loop():
    sock.send(prelogue + struct.pack("L", random.getrandbits(8*6) & 0xFFFFFFFFFFFE )[:6] + epilogue)
    if interval: sleep(interval)

if count:
    for x in xrange(count):
        loop()
else:
    while True:
        loop()


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
import time
import json
import subprocess
import threading

from datetime import datetime
from socketserver import ThreadingMixIn
from http.server import HTTPServer, BaseHTTPRequestHandler

from ProcReader.settings import (READER_PATH, READERS_LIST)

def not_insance_address_string(self):
    host, port = self.client_address[:2]
    return '%s (no getfqdn)' % host

BaseHTTPRequestHandler.address_string = not_insance_address_string

class Handler(BaseHTTPRequestHandler):
    content = {}
    intvl = 10
    ts_get = ''
    reader_path = READER_PATH
    readersters = READERS_LIST

    def do_GET(self):
        args_str = 'python %s/ReaderManager.py restart' % (Handler.reader_path)
        if self.path == '/getdata':
            self.send_response(200)
            self.send_header("Content-type", "text/json")
            Handler.ts_get = time.asctime(time.localtime())

            t_content = datetime.strptime(
                Handler.content['timestamp'], "%a %b %d %H:%M:%S %Y")
            t_last_get = datetime.strptime(
                Handler.ts_get, "%a %b %d %H:%M:%S %Y")

            if (t_last_get - t_content).seconds < 60:
                Handler.content['status'] = 'NORMAL'
            else:
                Handler.content['status'] = 'READERING_TIMEOUT'
                Handler.content['data'] = {}
                ping_Popen = subprocess.Popen(args=args_str, shell=True)
            obj_str = json.dumps(Handler.content)
            self.send_header("Content-Length", str(len(obj_str)))
            self.end_headers()
            self.wfile.write(obj_str.encode())
            self.wfile.write('\n')
        elif self.path == '/getintvl':
            self.send_response(200)
            self.end_headers()
            self.wfile.write(Handler.intvl)
            self.wfile.write('\n')
        elif self.path == '/getreadersters':
            self.send_response(200)
            self.send_header("Content-Length", "text/json")
            reader_str = json.dumps(Handler.readersters)
            self.send_header("Content-Length", str(len(reader_str)))
            self.end_headers()
            self.wfile.write(reader_str.encode())
            self.wfile.write('\n')
        elif self.path == '/rstsvr':
            ping_Popen = subprocess.Popen(args=args_str, shell=True)
            self.send_response(200)
            self.end_headers()
            self.wfile.write('Restart server OK!')
            self.wfile.write('\n')
        else:
            self.send_response(404)
            self.end_headers()

    def do_POST(self):
        if self.path == '/setdata':
            length = self.headers['content-length']
            data = self.rfile.read(int(length))
            Handler.content = eval(data.encode())
            self.send_response(200)
            self.end_headers()
            self.wfile.write(str(Handler.content))
            self.write.write('\n')
        elif self.path == '/setintvl1':
            length = self.headers['content-length']
            data = self.rfile.read(int(length))
            Handler.intvl = eval(data.encode())
            self.send_response(200)
            self.end_headers()
            self.wfile.write(str(Handler.intvl))
            self.write.write('\n')
        elif self.path == '/setreadersters':
            length = self.headers['content-length']
            data = self.rfile.read(int(length))
            Handler.readersters = eval(data.decode())
            self.send_response(200)
            self.end_headers()
            self.wfile.write(str(Handler.readersters))
            self.wfile.write('\n')
        else:
            self.send_response(404)
            self.end_headers()

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

if __name__ == '__main__':
    server = None
    try:
        server = ThreadedHTTPServer(('0.0.0.0', 4396), Handler)
        if sys.argv[1]:
            Handler.reader_path = sys.argv[1]
        print('Starting server, use <Ctrl-C> to stop')
        server.serve_forever()
    except Exception as e:
        if server:
            server.socket.close()


"""Modified Pajek format writer for NetworkX graphs.

This writer is a fixed-up copy from the NX one that has been adapted to deal
with some bugs in Rosvall's infomap Pajek reader.
"""

from networkx.utils import is_string_like, open_file, make_str

def quote_spaces(x):
    """Called by make_quoted_str."""
    try:
        has_space = ' ' in x
    except TypeError:
        return x
    if has_space:
        return '"%s"' % x
    else:
        return x

def make_quoted_str(x):
    """Called by generate_pajek."""
    if not isinstance(x, basestring):
        x = str(x)
    return quote_spaces(x)

def generate_pajek(G):
    """Generate lines in Pajek graph format.

    Called by write_pajek.

    Parameters
    ----------
    G : graph
       A Networkx graph

    References
    ----------
    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
    """
    name = 'NetworkX' if G.name=='' else G.name
    yield '*network %s' % name

    # write nodes with attributes
    yield '*vertices %s'%(G.order())
    nodes = G.nodes()

    # sorting to work around infomap's bugs
    nodes.sort()
    # /end infomap workaround

    # make dictionary mapping nodes to integers
    nodenumber = dict(zip(nodes, range(1, len(nodes)+1)))
    for n in nodes:
        na = G.node.get(n, {})
        x = na.get('x', 0.0)
        y = na.get('y', 0.0)
        id = int(na.get('id', nodenumber[n]))
        nodenumber[n] = id
        shape = na.get('shape', 'ellipse')
        s = map(make_quoted_str, (id, n, x, y, shape))
        for k,v in na.items():
            s += map(make_quoted_str, (k, v))
        yield ' '.join(s)

    # write edges with attributes
    if G.is_directed():
        yield '*arcs'
    else:
        yield '*edges'
    for u, v, edgedata in G.edges(data=True):
        d = edgedata.copy()
        value = d.pop('weight', 1.0) # use 1 as default edge value
        s = map(make_str,(nodenumber[u], nodenumber[v], value))
        for k, v in d.items():
            s += map(make_quoted_str, (k, v))
        yield ' '.join(s)

@open_file(1,mode='wb')
def write_pajek(G, path, encoding='UTF-8'):
    """Write graph in Pajek format to path.

    Parameters
    ----------
    G : graph
       A Networkx graph
    path : file or string
       File or filename to write.
       Filenames ending in .gz or .bz2 will be compressed.

    Examples
    --------
    >>> import networkx as nx
    >>> G=nx.path_graph(4)
    >>> nx.write_pajek(G, "test.net")

    References
    ----------
    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
    """
    for line in generate_pajek(G):

        # Work around a bug in infomap that doesn't understand this header
        if line.startswith('*network'):
            continue
        # /end infomap workaround

        line+='\n'
        path.write(line.encode(encoding))


# This file is part of CRFSuiteTagger.
#
# CRFSuiteTagger is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CRFSuiteTagger is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CRFSuiteTagger.  If not, see <http://www.gnu.org/licenses/>.
__author__ = 'Aleksandar Savkov'

import os
import time
import io
import copy
import crfsuitetagger.features as fts
import crfsuitetagger.win_features as wf
from unittest import TestCase

from crfsuitetagger.ftex import *
from crfsuitetagger.utils import *
from crfsuitetagger.eval import *

class TestUtils(TestCase):

    def setUp(self):
        self.data_str = '''The\tD
quick\tA
fox\tN
jumped\tV
across\tR
the\tD
river\tN
.\t.

The\tD
stupid\tA
wolf\tN
fell\tV
in\tI
the\tD
trap\tN
.\t.'''
        self.dp = 'tmp/data.%s.tmp' % time.asctime()
        open(self.dp, 'w').write(self.data_str)
        self.dp_large = 'tmp/data_large.%s.tmp' % time.asctime()
        with open(self.dp_large, 'w') as fh:
            fh.write('\n\n'. join([self.data_str + '\n.\t.' for _ in range(10)]))
        self.data = parse_tsv(self.dp, ('form', 'postag'))
        self.data_large = parse_tsv(self.dp_large, ('form', 'postag'))

    def tearDown(self):
        os.remove(self.dp)

    def test_parse_tsv(self):
        dt = 'a60,a10,a10,int32'
        mock_data = np.zeros(16, dtype=dt)
        mock_data.dtype.names = ('form', 'postag', 'guesstag', 'eos')
        for idx, line in enumerate(
                x for x in self.data_str.split('\n') if x.strip()):
            if '\t' in line:
                mock_data[idx] = tuple(line.strip().split('\t')) + ('', -1)
        mock_data[0]['eos'] = 8
        mock_data[8]['eos'] = 16

        self.assertItemsEqual(self.data, mock_data)

    def test_count_records(self):
        # in case the string is changed
        rc = len([x for x in self.data_str.strip().split('\n') if x.strip()])
        self.assertEqual(count_records(open(self.dp, 'r')), rc)
        fp = 'tmp/count.%s.tmp' % time.asctime()
        open(fp, 'w').write('%s\n\n' % self.data_str)
        self.assertEqual(count_records(open(fp, 'r')), rc)
        os.remove(fp)

    def test_export(self):
        ex = io.StringIO()
        export(self.data, ex, cols=['form', 'postag'])
        self.assertEqual(ex.getvalue().strip(), self.data_str)

    def test_gsequence(self):
        gs = [list(x) for x in gsequences(self.data)]
        dt = 'a60,a10,a10,int32'
        mock_data = np.zeros(16, dtype=dt)
        mock_data.dtype.names = ('form', 'postag', 'guesstag', 'eos')
        for idx, line in enumerate(
                x for x in self.data_str.split('\n') if x.strip()):
            if '\t' in line:
                mock_data[idx] = tuple(line.strip().split('\t')) + ('', -1)
        mock_data[0]['eos'] = 8
        mock_data[8]['eos'] = 16

        mgs = [list(mock_data[:8]), list(mock_data[8:])]

        self.assertSequenceEqual(gs, mgs)

    def test_count(self):
        self.assertEqual(count_sequences(self.data), 2)
        self.assertEqual(count_sequences(self.data_large), 20)

    def test_weighted_split(self):
        trd, ted = weighed_split(self.data_large, proportion=0.89)
        self.assertEqual(len(trd), 144)
        self.assertEqual(len(ted), 26)
        self.assertEqual(ted[0]['eos'], 9)
        self.assertEqual(trd[0]['eos'], 8)
        s = 0
        sh = 0
        while s < len(trd):
            s = trd[s]['eos']
            self.assertNotEqual(s, sh)
            sh = s
        self.assertEqual(s, len(trd))
        s = 0
        sh = 0
        while s < len(ted):
            s = ted[s]['eos']
            self.assertNotEqual(s, sh)
            sh = s
        self.assertEqual(s, len(ted))

    def test_set_seq_start_idx(self):
        d = copy.deepcopy(self.data_large)
        eos_idx = 1
        while d[eos_idx]['eos'] < 0:
            eos_idx += 1
        idx = 10
        set_sequence_start_idx(d, idx)
        s = 0
        sh = 0
        while s < len(d):
            s = d[s]['eos'] - idx
            self.assertNotEqual(s, sh)
            sh = s
        self.assertEqual(s, len(d))
        self.assertEqual(d[0]['eos'], eos_idx + idx)
        idx = 15
        set_sequence_start_idx(d, idx)
        s = 0
        sh = 0
        while s < len(d):
            s = d[s]['eos'] - idx
            self.assertNotEqual(s, sh)
            sh = s
        self.assertEqual(s, len(d))
        self.assertEqual(d[0]['eos'], eos_idx + idx)
        idx = 0
        set_sequence_start_idx(d, idx)
        s = 0
        sh = 0
        while s < len(d):
            s = d[s]['eos'] - idx
            self.assertNotEqual(s, sh)
            sh = s
        self.assertEqual(s, len(d))
        self.assertEqual(d[0]['eos'], eos_idx + idx)
        with self.assertRaises(AssertionError):
            set_sequence_start_idx(d, -10)

    def test_cv_splits(self):
        pass

class TestFtEx(TestCase):

    def setUp(self):
        self.data_str = '''The\tD
quick\tA
fox\tN
jumped\tV
across\tR
the\tD
river\tN
.\t.

The\tD
stupid\tA
wolf\tN
fell\tV
in\tI
the\tD
trap\tN
.\t.'''
        self.dp = 'tmp/data.%s.tmp' % time.asctime()
        open(self.dp, 'w').write(self.data_str)
        self.data = parse_tsv(self.dp, ('form', 'postag'))
        self.cols = {
            'form': 'form',
            'postag': 'postag',
            'chunktag': 'chunktag',
            'netag': 'netag'
        }

    def tearDown(self):
        os.remove(self.dp)

    @staticmethod
    def fakeres(data, i, cols, rel=0, b=None, p=None, *args, **kwargs):
        if b is None and p is None:
            return 'fakeres'
        try:
            v = b[data[i + rel][cols['form']]][int(p)]
        except KeyError:
            v = None
        return 'fakeres[%s]=%s' % (rel, v)

    @staticmethod
    def winfakeres(fn, fw, fp, *args, **kwargs):
        prms = tuple() if fp is None else tuple(fp)
        for i in fw:
            yield (fn, i) + prms

    def test_parse_range(self):
        rr = [1, 2, 3, 10]
        rs = '1:3,10'
        r = wf.parse_range(rs)
        self.assertSequenceEqual(r, rr)

    def test_ftvec_templ(self):

        fr = {'fox': list(range(10)), 'wolf': list(range(10))}
        fr_dict = {'fakeres': fr}

        ftt_real = FeatureTemplate(fnx=[self.fakeres])
        ftt_real.add_win_features('word', [-3, -2, -1, 0, 1, 4], ())
        ftt_real.add_win_features('pos', [-2, 0], ())
        ftt_real.add_win_features('fakeres', [0], (fr, '0'))
        ftt_real.add_feature('fakeres')

        ftvex_spaceing = [
            '\tword:[-3:1,4];pos :   [-2,0];fakeres:[0],0;fakeres\t',
            'word:[-3:1,4];pos:[-2,0] ;fakeres:[0] , 0; fakeres',
            'word:[-3:1,4] ; pos:[-2,0] ; fakeres:[0],0;fakeres',
            'word:[ -3 : 1 , 4 ];pos:[-2,0];fakeres:[0],0;fakeres',
            'word:[-3:1,4];pos : [-2,0];fakeres : [0],0;fakeres',
            ' word:[-3:1,4];pos:[-2,0];fakeres:[0],0 ;fakeres',
            '    word :[-3:1,4];   pos :   [-2,0];fakeres:[0],0 ;fakeres',
            'word:[-3:1,4];pos:[-2,0];fakeres:[0],  0 ;fakeres',
            'word:[-3:1,4];pos:[-2,0];fakeres:[0],  0; ;fakeres'
        ]

        for ftvec in ftvex_spaceing:
            ftt = FeatureTemplate()
            ftt.parse_ftvec_templ(ftvec, fr_dict)
            self.assertSequenceEqual(ftt.vec, ftt_real.vec)

        ftt_real.fnx['fakeres'](self.data, 0, self.cols, *ftt_real.vec[-1][1:])

    def test_ftt_constructor(self):
        ftt = FeatureTemplate(fnx=[self.fakeres], win_fnx=[self.winfakeres])
        self.assertEqual(ftt.vec, [])
        self.assertIn(self.fakeres, list(ftt.fnx.values()))
        self.assertIn(self.winfakeres, list(ftt.win_fnx.values()))

    def test_make_features(self):
        ftt = FeatureTemplate(fnx=[self.fakeres])

        fr = {'fox': list(range(10)), 'wolf': list(range(10))}

        ftt.add_win_features('word', [-3, -2, 4], ())
        ftt.add_win_features('pos', [-2, 0], ())
        ftt.add_win_features('fakeres', [0], (fr, '0'))

        real_fts = [
            [
                ['The', 'w[-3]=None', 'w[-2]=None', 'w[4]=across', 'p[-2]=None',
                 'p[0]=D', 'fakeres[0]=None'],
                ['quick', 'w[-3]=None', 'w[-2]=None', 'w[4]=the', 'p[-2]=None',
                 'p[0]=A', 'fakeres[0]=None'],
                ['fox', 'w[-3]=None', 'w[-2]=The', 'w[4]=river', 'p[-2]=D',
                 'p[0]=N', 'fakeres[0]=0'],
                ['jumped', 'w[-3]=The', 'w[-2]=quick', 'w[4]=.', 'p[-2]=A',
                 'p[0]=V', 'fakeres[0]=None'],
                ['across', 'w[-3]=quick', 'w[-2]=fox', 'w[4]=None', 'p[-2]=N',
                 'p[0]=R', 'fakeres[0]=None'],
                ['the', 'w[-3]=fox', 'w[-2]=jumped', 'w[4]=None', 'p[-2]=V',
                 'p[0]=D', 'fakeres[0]=None'],
                ['river', 'w[-3]=jumped', 'w[-2]=across', 'w[4]=None',
                 'p[-2]=R', 'p[0]=N', 'fakeres[0]=None'],
                ['.', 'w[-3]=across', 'w[-2]=the', 'w[4]=None', 'p[-2]=D',
                 'p[0]=.', 'fakeres[0]=None']
            ],
            [
                ['The', 'w[-3]=None', 'w[-2]=None', 'w[4]=in', 'p[-2]=None',
                 'p[0]=D', 'fakeres[0]=None'],
                ['stupid', 'w[-3]=None', 'w[-2]=None', 'w[4]=the', 'p[-2]=None',
                 'p[0]=A', 'fakeres[0]=None'],
                ['wolf', 'w[-3]=None', 'w[-2]=The', 'w[4]=trap', 'p[-2]=D',
                 'p[0]=N', 'fakeres[0]=0'],
                ['fell', 'w[-3]=The', 'w[-2]=stupid', 'w[4]=.', 'p[-2]=A',
                 'p[0]=V', 'fakeres[0]=None'],
                ['in', 'w[-3]=stupid', 'w[-2]=wolf', 'w[4]=None', 'p[-2]=N',
                 'p[0]=I', 'fakeres[0]=None'],
                ['the', 'w[-3]=wolf', 'w[-2]=fell', 'w[4]=None', 'p[-2]=V',
                 'p[0]=D', 'fakeres[0]=None'],
                ['trap', 'w[-3]=fell', 'w[-2]=in', 'w[4]=None', 'p[-2]=I',
                 'p[0]=N', 'fakeres[0]=None'],
                ['.', 'w[-3]=in', 'w[-2]=the', 'w[4]=None', 'p[-2]=D', 'p[0]=.',
                 'fakeres[0]=None']
            ]
        ]

        data = list(gsequences(self.data, cols=['form', 'postag']))

        for d, rfv in zip(data, real_fts):
            for i, rf in enumerate(rfv):
                fts = ftt.make_fts(d, i)
                self.assertItemsEqual(fts, rf)

    def test_word(self):
        for i in [-4, -1, 0, 2]:
            w = fts.ft_word(self.data, 2, self.cols, i)
            rel = 2 + i
            rw = 'w[%s]=%s' % (i, self.data[rel][0] if rel >= 0 else None)
            self.assertEqual(w, rw)

    def test_pos(self):
        for i in [-4, -1, 0, 2]:
            w = fts.ft_pos(self.data, 2, self.cols, i)
            rel = 2 + i
            rw = 'p[%s]=%s' % (i, self.data[rel][1] if rel >= 0 else None)
            self.assertEqual(w, rw)

    def test_chunk(self):
        dtn = self.data.dtype.names
        self.data.dtype.names = (dtn[0], 'chunktag',) + dtn[2:]
        for i in [-4, -1, 0, 2]:
            w = fts.ft_chunk(self.data, 2, self.cols, i)
            rel = 2 + i
            rw = 'ch[%s]=%s' % (i, self.data[rel][1] if rel >= 0 else None)
            self.assertEqual(w, rw)

    def test_can(self):
        for i in [-4, -1, 0, 2]:
            w = fts.ft_can(self.data, 2, self.cols, i)
            r = 2 + i
            rwv = re.sub('.',
                         'x',
                         self.data[r][0]) if len(self.data) > r >= 0 else None
            rw = 'can[%s]=%s' % (i, rwv)
            self.assertEqual(w, rw)

    def test_isnum(self):
        self.data[3][0] = '10'
        for i in [-4, -1, 0, 2]:
            w = fts.ft_isnum(self.data, 2, self.cols, i)
            r = 2 + i
            rwv = i == 3 if len(self.data) > r >= 0 else None
            rw = 'isnum[%s]=%s' % (i, rwv)
            self.assertEqual(w, rw)

    def test_emb(self):
        e = {'fox': list(range(100)), 'quick': list(reversed(list(range(100))))}
        i = 2
        for rel in [-4, -1, 0, 2]:
            for j in [4, 6, 8]:
                w = fts.ft_emb(self.data, i, self.cols, rel, j, e)
                r = i + rel
                v = None
                if len(self.data) > r >= 0:
                    v = e.get(self.data[r][0], [None for _ in range(100)])[j]
                rw = 'emb[%s][%s]=%s' % (rel, j, v)
                self.assertEqual(w, rw)

    def test_cls(self):
        c = {'fox': 1, 'quick': 2}
        i = 2
        for rel in [-4, -1, 0, 2]:
            w = fts.ft_cls(self.data, i, self.cols, rel, c)
            r = i + rel
            v = None
            if len(self.data) > r >= 0:
                v = c.get(self.data[r][0], None)
            rw = 'cnum[%s]=%s' % (rel, v)
            self.assertEqual(w, rw)

    def test_brown(self):
        b = {'fox': '00011110011010', 'quick': '11001001110011'}
        i = 2
        p = 10
        for rel in [-4, -1, 0, 2]:
            w = fts.ft_brown(self.data, i, self.cols, rel, b, p)
            r = i + rel
            v = None
            if len(self.data) > r >= 0:
                v = b.get(self.data[r][0], None)
            if v:
                v = v[:10]
            rw = 'cn[%s]:%s=%s' % (rel, p, v)
            self.assertEqual(w, rw)

    def test_suff(self):
        b = {'ox', 'ick', 'across'}
        i = 2
        p = 5
        for rel in [-4, -1, 0, 2]:
            w = fts.ft_suff(self.data, i, self.cols, rel, b, p)
            r = i + rel
            v = None
            if len(self.data) > r >= 0:
                if self.data[r]['form'].endswith('ox'):
                    v = 'ox'
                elif self.data[r]['form'].endswith('ick'):
                    v = 'ick'
            rw = 'sfx[%s]=%s' % (rel, v)
            self.assertEqual(w, rw)

    def test_pref(self):
        b = {'fo', 'qui', 'across'}
        i = 2
        p = 5
        for rel in [-4, -1, 0, 2]:
            w = fts.ft_pref(self.data, i, self.cols, rel, b, p)
            r = i + rel
            v = None
            if len(self.data) > r >= 0:
                if self.data[r]['form'].startswith('fo'):
                    v = 'fo'
                elif self.data[r]['form'].startswith('qui'):
                    v = 'qui'
            rw = 'sfx[%s]=%s' % (rel, v)
            self.assertEqual(w, rw)

    def test_nrange(self):
        rng = wf.nrange(0, 10, 3)
        self.assertSequenceEqual(rng, list(range(0, 9, 1)))
        rng = wf.nrange(0, 11, 3)
        self.assertSequenceEqual(rng, list(range(0, 10, 1)))
        rng = wf.nrange(0, 12, 3)
        self.assertSequenceEqual(rng, list(range(0, 11, 1)))
        rng = wf.nrange(0, 13, 1)
        self.assertSequenceEqual(rng, list(range(0, 14, 1)))
        rng = wf.nrange(0, 14, 2)
        self.assertSequenceEqual(rng, list(range(0, 14, 1)))

    def test_parse_ng_range(self):
        nrng = wf.parse_ng_range(list(range(10)), 3)
        self.assertSequenceEqual(nrng, list(range(8)))
        nrng = wf.parse_ng_range(list(range(10)) + list(range(12, 15)), 3)
        self.assertSequenceEqual(nrng, list(range(8)) + list(range(12, 15, 3)))
        nrng = wf.parse_ng_range(list(range(10)) + list(range(12, 16)) + list(range(20, 22)), 2)
        self.assertSequenceEqual(nrng, list(range(9)) + list(range(12, 15)) + list(range(20, 21)))

    def test_nword(self):
        ft = fts.ft_nword(self.data[:8], 3, self.cols, 2, 2)
        self.assertEqual(ft, '2w[2]=theriver')
        ft = fts.ft_nword(self.data[:8], 5, self.cols, 2, 2)
        self.assertEqual(ft, '2w[2]=None')
        ft = fts.ft_nword(self.data[:8], 5, self.cols, -1, 2)
        self.assertEqual(ft, '2w[-1]=acrossthe')
        ft = fts.ft_nword(self.data[:8], 1, self.cols, -2, 3)
        self.assertEqual(ft, '3w[-2]=None')

    def test_npos(self):
        ft = fts.ft_npos(self.data[:8], 3, self.cols, 2, 2)
        self.assertEqual(ft, '2p[2]=DN')
        ft = fts.ft_npos(self.data[:8], 5, self.cols, 2, 2)
        self.assertEqual(ft, '2p[2]=None')
        ft = fts.ft_npos(self.data[:8], 5, self.cols, -1, 2)
        self.assertEqual(ft, '2p[-1]=RD')
        ft = fts.ft_npos(self.data[:8], 1, self.cols, -2, 3)
        self.assertEqual(ft, '3p[-2]=None')

class TestEval(TestCase):

    def test_pos(self):
        data = [{'postag': 'N' if x % 2 else 'V',
                 'guesstag': 'N' if x % 2 else 'V'} for x in range(10)]
        data[0]['guesstag'] = 'N'
        r = pos(data)
        self.assertEqual(r['Total']['accuracy'], 0.9)
        self.assertEqual(r['V']['accuracy'], 0.8)
        self.assertEqual(r['N']['accuracy'], 1.0)

    def test_conll(self):
        """This tests only the data is correctly processed and parsed. It does
        not test the CoNLL evaluation script results.

        """
        dt = 'a10,a10,a10,a10,int32'
        data = np.zeros(10, dtype=dt)
        data.dtype.names = ['form', 'postag', 'chunktag', 'guesstag', 'eos']
        for x in range(10):
            data[x] = ('bla', 'N', 'B-NP' if x % 2 else 'B-VP',
                       'B-NP' if x % 2 else 'B-VP', 9 if x == 0 else -1)
        data[2]['guesstag'] = 'B-NP'
        data[2]['guesstag'] = 'I-NP'
        r = conll(data)
        self.assertAlmostEqual(float(r['Total']['fscore']), 84.21)
        self.assertAlmostEqual(float(r['VP']['fscore']), 88.89)
        self.assertAlmostEqual(float(r['NP']['fscore']), 80.00)

    def test_bio(self):
        """This tests BIO evaluation and compares it to output of CoNLL-2000
        script.

        """
        dt = 'a10,a10,a10,a10,int32'
        data = np.zeros(10, dtype=dt)
        data.dtype.names = ['form', 'postag', 'chunktag', 'guesstag', 'eos']
        for x in range(10):
            data[x] = ('bla', 'N', 'B-NP' if x % 2 else 'B-VP',
                       'B-NP' if x % 2 else 'B-VP', 9 if x == 0 else -1)
        data[2]['guesstag'] = 'B-NP'
        data[2]['guesstag'] = 'I-NP'
        r = bio(data)
        self.assertAlmostEqual(float(r['Total']['fscore']), 84.21)
        self.assertAlmostEqual(float(r['Total']['precision']), 88.89)
        self.assertAlmostEqual(float(r['Total']['recall']), 80.00)

class TestTagger(TestCase):
    pass


from __future__ import annotations

from urllib.parse import urlencode

import streamlit as st
import streamlit_antd_components as sac

import dev_utils.dashboard_app.streamlit_utils as st_utils
from dev_utils.core.sec_edgar_reports_getter import SecEdgarReportsGetter
from dev_utils.dashboard_app.constants import example_queries_items
from dev_utils.dashboard_app.core.download_metadatas import global_get_report_metadatas
from dev_utils.dashboard_app.python_utils import smart_join

def render_select_reports():
    st.markdown(
        """

        """,
        unsafe_allow_html=True,
    )

    class OnChangeHandler:
        def __init__(self, key):
            self.key = key

        def __call__(self, value):
            if isinstance(value, list):
                if len(value) >= 2 and 0 in value and value[0] != 0:
                    return [0]  # [v for v in value if v != 0]
                if len(value) >= 2 and value[0] == 0:
                    return [v for v in value if v != 0]
                elif len(value) == 0:
                    return [0]
            return value

    handler = OnChangeHandler("actual")
    st_utils.st_unkeep("select_reports__example_queries")
    selected_example_queries = handler(
        sac.chip(
            items=[k[0] for k in example_queries_items],
            label="Get started with examples:",
            size="xs",
            format_func="title",
            align="start",
            index=st.session_state.select_reports__example_queries,
            variant="outline",
            multiple=True,
            return_index=True,
            key="_select_reports__example_queries",
            on_change=lambda: st_utils.st_keep(
                "select_reports__example_queries",
                handler,
            ),
        ),
    )
    avoidlist = [
        k[1]
        for i, k in enumerate(example_queries_items)
        if i not in selected_example_queries
    ]
    addlist = [
        example_queries_items[i][1]
        for i in selected_example_queries
        if example_queries_items[i][1]
    ]

    current = [
        k
        for k in SecEdgarReportsGetter.raw_query_to_list(
            st.session_state.select_reports__queries,
        )
        if k not in avoidlist
    ]

    to_add = [k for k in addlist if k not in current]
    current.extend(to_add)
    queries = smart_join(current, max_length=50)

    st.session_state.select_reports__queries = queries
    st_utils.st_unkeep("select_reports__queries")
    queries = st.text_area(
        "Enter your queries separated by commas and/or newlines:",
        key="_select_reports__queries",
        on_change=lambda: st_utils.st_keep("select_reports__queries"),
    )

    query_list, report_metadatas = global_get_report_metadatas()

    if any(m.form_type != "10-Q" for m in report_metadatas):
        st.warning(
            "**Warning:** Only 10-Q form types are supported at this time. Parsing other form types may lead to unexpected results.",
        )
    table_container = st.empty()
    if report_metadatas:
        reports_dict_list = SecEdgarReportsGetter.to_dict_list(report_metadatas)
        table_container.dataframe(reports_dict_list, use_container_width=False)

    st.session_state.select_reports__report_metadatas = report_metadatas

    query_elements = [("q", query) for query in query_list]
    queries = urlencode(query_elements, doseq=True)

    if query_list:
        sac.buttons(
            [
                sac.ButtonsItem(label="share", icon="share-fill", href=f"/?{queries}"),
            ],
            format_func="title",
            align="center",
            index=-1,
            compact=True,
        )


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def load_data(file):
    return pd.read_csv(file, sep = ",", header = None)

#load data

data = load_data("prod_dem.csv")
data = data.values.flatten()

#define function to forecast all exponential smoothing values given alpha

def ES(data, alpha):

    ES_pred = np.zeros(len(data))

    for period in range(0, len(data)):

        if (period == 0 or period == 1):
            ES_pred[period] = data[0]
        else:
            ES_pred[period] = alpha * data[period - 1] + (1 - alpha) * ES_pred[period - 1]

    return ES_pred

#forecast all exponential smoothing values for alpha = 0.5 and alpha = 0.9

ES_pred_alpha8 = ES(data, 0.8)
ES_pred_alpha5 = ES(data, 0.5)
ES_pred_alpha9 = ES(data, 0.9)

#plot forecasts vs ground truth

plt.figure()
fig, ax = plt.subplots(figsize=(20,5))

ax.plot(range(1, len(data) + 1), data)
ax.plot(range(1, len(data) + 1), ES_pred_alpha5)
ax.plot(range(1, len(data) + 1), ES_pred_alpha9)
ax.legend(["actual","ES alpha5", "ES alpha10"])


import os
import cv2
from skimage.filters import threshold_otsu, threshold_adaptive
from skimage import filters
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal
from smooth import smooth
from savitzky_golay_filter import savitzky_golay
from scipy.signal import argrelextrema
import scipy.fftpack
from skimage.filters import threshold_otsu, threshold_adaptive

# def getseperatingPoints (points,up=0,down=1):
#     result = []
#     for i in range(down, len(points)):
#         if ((points[i]-1) != points[i-1]):
#             result.append(np.mean(points[down-1:i], dtype=int))
#             down = i+1
#         elif (i == (len(points)-1)):
#             result.append(np.mean(points[down-1:i+1], dtype=int))
#     return result

def getseperatingPoints(points):
	pos = 0
	results = [pos]
	for i,j in zip(points[:-1], points[1:]):
		# print(points.index(i))
		if (i != (j-1)):
			# print(points[pos:points.index(i)])
			# print(pos, points.index(i))
			# print(np.mean(points[pos:points.index(i)]))
			results.append(np.mean(points[pos:points.index(j)], dtype=int))
			# results.append(points[pos])
			pos = points.index(i)
		elif (points.index(j) == len(points)-1):
			# print(points.index(i))
			results.append(np.mean(points[pos:points.index(j)], dtype=int))
			# results.append(points[pos])
	return results

def basic_parameters_estimation(img, path, nth):
	# blur_img = filters.gaussian_filter(img, sigma=1, truncate=1)
	# otsu_threshold = threshold_otsu(blur_img)
	# bin_img = (blur_img < otsu_threshold) > 0
	# gray_img = (bin_img*255).astype('uint8')

	h, w = np.shape(img)[:2]
	print(w, h)
	# bin_img = ~(img > 0)*1
	otsu_threshold = threshold_otsu(img)
	bin_img = (img < otsu_threshold) > 0
	gray_img = (bin_img*255).astype('uint8')
	# num, labels = cv2.connectedComponents(gray_img, connectivity = 8)

	ret, labels = cv2.connectedComponents(gray_img, connectivity = 8)

	# Map component labels to hue val
	label_hue = np.uint8(179*labels/np.max(labels))
	blank_ch = 255*np.ones_like(label_hue)
	labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

	# cvt to BGR for display
	labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

	# set bg label to black
	labeled_img[label_hue==0] = 0
	# cv2.imwrite(os.path.join(os.getcwd(), "{}.png".format(nth)), labeled_img)
	# plt.imshow(labeled_img)
	# plt.show()

	count = 0
	for i in range(1,ret):
		cc_img = np.zeros_like(img)
		cc_img[labels == i] = 255
		# plt.imshow(cc_img)
		# plt.show()
		x, y, width, height = cv2.boundingRect(cc_img)
		# cv2.imwrite(os.path.join(path, "{}.png".format(count)), cc_img)
		cv2.imwrite(os.path.join(path, "{}_{}.png".format(nth, count)), img[y:y+height, x:x+width])
		# plt.imshow(img[y:y+width, x:x+width])
		# plt.show()
		count += 1

	# plt.imshow(gray_img)
	# plt.show()

if __name__ == "__main__":

    import os
    import glob
    import argparse
    import shutil

    parser = argparse.ArgumentParser(description="Word Segmentation")
    parser.add_argument('--folder', dest='folder', type=str, help="folder to save")
    parser.add_argument('--path', dest='path', type=str, help="path to linecuts")
    # parser.add_argument('--folder', dest='folder', type=str, help="folder to save")
    args = parser.parse_args()

    # save_folder = os.path.join(os.getcwd(), "training_data_")
    save_folder = args.folder
    # if os.path.exists(save_folder):
    # 	shutil.rmtree(save_folder)
    # os.makedirs(save_folder)

    # path = "/home/thanh/Desktop/test_data_difficult-4/line_1.png"
    # img = cv2.imread(path, 0)
    # plt.imshow(img)
    # plt.show()
    # blur_img = filters.gaussian_filter(img, sigma=1, truncate=1)
    # otsu_threshold = threshold_otsu(blur_img)
    # bin_img = (blur_img < otsu_threshold) > 0
    # gray_img = (bin_img*255).astype('uint8')

    # plt.imshow(gray_img)
    # plt.show()
    # cv2.imwrite("debug.png", gray_img)
    # basic_parameters_estimation(img, save_folder, 0)

    for nth, folder in enumerate(os.listdir(args.path)):
    	# img = cv2.imread(os.path.join(args.path,file), 0)
    	# basic_parameters_estimation(img, save_folder, nth)
    	# name = os.path.basename(os.path.dirname(os.path.join(args.path, folder)))
    	name = os.path.basename(os.path.join(args.path, folder))
    	print(name)
    	for file in os.listdir(os.path.join(args.path, folder)):
    		filename = os.path.join(args.path, folder, file)
    		# print(filename)
    		print(os.path.join(save_folder, name+"_"+file))
    		im = cv2.imread(filename)
    		cv2.imwrite(os.path.join(save_folder, name+"_"+file), im)


"""Some helpful utility functions for working with csv files"""

from csv import DictReader

# reading the lines of a csv file
def read_csv_rows(file_name: str) -> list[dict[str, str]]:
    """Read the rows of a csv into a 'table'."""
    result: list[dict[str, str]] = []
    # Open a handle to the data file
    file_handle = open(file_name, "r", encoding="utf8")
    # Prepare to read the data file as a csv rather than just strings
    csv_reader = DictReader(file_handle)
    # Read each row of the csv line by line.
    for row in csv_reader:
        result.append(row)
    # Close that file to free up resources that it was using.
    file_handle.close()

    return result

def column_values(table: list[dict[str, str]], column: str) -> list[str]:
    """Reads the rows of a csv into a column based thing."""
    result: list[str] = []
    for row in table:
        item: str = row[column]
        result.append(item)
    return result

def columnar(row_table: list[dict[str, str]]) -> dict[str, list[str]]:
    """Morph a row-oriented table to a column-oriented (or representation) of a table."""
    result: dict[str, list[str]] = {}
    first_row: dict[str, str] = row_table[0]
    for column in first_row:
        result[column] = column_values(row_table, column)
    return result


import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

root = tk.Tk()

root.geometry("800x650")
root.title("My Application")

def clear_all_fields():
    all_entries = [entry2, entry3, entry4, entry5]
    all_chk = [c1, c2, c3]

    for entry in all_entries:
        entry.delete(0, "end")
    v.set("F")
    for chk in all_chk:
        chk.set(False)

def btn1_fun():
    all_labels = [label2, label3, label4, label5]
    for lbl in all_labels:
        lbl.config(fg='black')
    if entry2.get() == "" or entry3.get() == "":
        messagebox.askokcancel(title="Error Processing Form", message="You need to enter both first name and last name.")
        if entry2.get() == "":
            label2.config(fg='red')
        if entry3.get() == "":
            label3.config(fg='red')
    elif entry4.get() == "" and entry5.get() == "":
        messagebox.askokcancel(title="Error Processing Form",
                               message="You need to enter either an email or phone number for us to contact you.")
        label4.config(fg='red')
        label5.config(fg='red')
    else:
        fn = entry2.get()
        ln = entry3.get()
        email = entry4.get()
        pn = entry5.get()
        gn = v.get()
        coi = [str(c1.get()), str(c2.get()), str(c3.get())]

        with open("info.txt", "w") as file:
            file.write("'{}', '{}', '{}', '{}', '{}', '{}'".format(fn, ln, email, pn, gn, coi))

        messagebox.showinfo("Success", "Your information was saved successfully!")

txt = "Information Collection Form"

label1 = tk.Label(root, text=txt, font="Arial 18 bold")

label2 = tk.Label(root, text="First Name:", font="Arial 14")
entry2 = tk.Entry(root, width=40)

label3 = tk.Label(root, text="Last Name:", font="Arial 14")
entry3 = tk.Entry(root, width=40)

label4 = tk.Label(root, text="Email Address:", font="Arial 14")
entry4 = tk.Entry(root, width=40)

label5 = tk.Label(root, text="Phone Number:", font="Arial 14")
entry5 = tk.Entry(root, width=40)

frame1 = tk.Frame(root)
label6 = tk.Label(frame1, text="Gender:", font="Arial 14")

v = tk.StringVar()
v.set("F")
radio1 = tk.Radiobutton(frame1, text="Male", font="Arial 14", variable=v, value="M")
radio2 = tk.Radiobutton(frame1, text="Female", font="Arial 14", variable=v, value="F")

frame2 = tk.Frame(root)
label7 = tk.Label(frame2, text="Classes of Interest:", font="Arial 14")

c1 = tk.BooleanVar()
c2 = tk.BooleanVar()
c3 = tk.BooleanVar()
chk1 = tk.Checkbutton(frame2, text="Data Science", font="Arial 14", variable=c1)
chk2 = tk.Checkbutton(frame2, text="Application Development", font="Arial 14", variable=c2)
chk3 = tk.Checkbutton(frame2, text="Robotic Process Automation", font="Arial 14", variable=c3)

label8 = tk.Label(root, text="What kind of classroom setup are you interested in?", font="Arial 14")
combo1 = ttk.Combobox(root, font="Arial 14")
combo1['values'] = ("In Person", "Online Only", "Hybrid", "Don't care")
combo1.current(0)

btn1 = tk.Button(root, text='Submit', font="Arial 14", command=btn1_fun)
btn2 = tk.Button(root, text='Reset', font="Arial 14", command=clear_all_fields)

################### GRIDDING ######################

label1.grid(row=0, columnspan=2, padx=(75, 0))

label2.grid(row=1, column=0, pady=(20, 0), padx=(60, 0), sticky='W')
label3.grid(row=1, column=1, pady=(20, 0), padx=(60, 0), sticky='W')

entry2.grid(row=2, column=0)
entry3.grid(row=2, column=1)

label4.grid(row=3, column=0, pady=(20, 0), padx=(60, 0), sticky='W')
label5.grid(row=3, column=1, pady=(20, 0), padx=(60, 0), sticky='W')

entry4.grid(row=4, column=0)
entry5.grid(row=4, column=1)

frame1.grid(row=5, column=0, pady=(35, 0), columnspan=2, padx=(50, 0))
label6.grid(row=0, column=0, columnspan=2)
radio1.grid(row=1, column=0, pady=(10, 0), padx=(50, 50))
radio2.grid(row=1, column=1, pady=(10, 0), padx=(50, 50))

frame2.grid(row=6, column=0, pady=(35, 0), columnspan=2, padx=(50, 0))
label7.grid(row=0, column=0, columnspan=3)
chk1.grid(row=1, column=0, pady=(15, 0), padx=(5, 5))
chk2.grid(row=1, column=1, pady=(15, 0), padx=(5, 5))
chk3.grid(row=1, column=2, pady=(15, 0), padx=(5, 5))

label8.grid(row=7, column=0, pady=(45, 0), columnspan=2)
combo1.grid(row=8, column=0, pady=(15, 0), columnspan=2)

btn1.grid(row=9, column=0, pady=(35, 0))
btn2.grid(row=9, column=1, pady=(35, 0))

root.mainloop()


from rest_framework import generics, status
from rest_framework.response import Response
from rest_framework.generics import UpdateAPIView
from rest_framework.permissions import IsAuthenticated
from hospital_bed_map_backend.beds.api.serializer import BedsActiveUpdateSerializer, BedsStatusUpdateSerializer, BedsListSerializer, TypeSerializer, BedsSerializer, TypeOccupationSerializer
from hospital_bed_map_backend.beds.models import TypeOccupation, Type, Beds
from hospital_bed_map_backend.sectors.models import Sectors
from django.db.models import F

class TypeView(generics.GenericAPIView):
    serializer_class = TypeSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request):
        data = Type.objects.all()
        serializer = self.serializer_class(data, many=True).data

        return Response({'message': 'Dados recuperados com sucesso', 'data': serializer}, status=status.HTTP_200_OK)

class TypeOccupationView(generics.GenericAPIView):
    serializer_class = TypeOccupationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request):
        data = TypeOccupation.objects.all()
        serializer = self.serializer_class(data, many=True).data

        return Response({'message': 'Dados recuperados com sucesso', 'data': serializer}, status=status.HTTP_200_OK)

class BedsListView(generics.GenericAPIView):
    serializer_class = BedsListSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, sector=None):

        data = Beds.objects.filter(sector=sector)\
                .values('id', 'sector__hospital', 'sector',
                'name', 'type_occupation', 'type_occupation__status', 'type_occupation__description',
                'type__description', 'is_active', 'is_extra')\
                .annotate(  hospital_id=F('sector__hospital'),
                            sector_id=F('sector'),
                            type_occupation_id=F('type_occupation'),
                            type_occupation_status=F('type_occupation__status'),
                            type_occupation_description=F('type_occupation__description'),
                            type=F('type__description'))

        serializer = self.serializer_class(data, many=True).data

        return Response({'message': 'Dados recuperados com sucesso', 'data': serializer}, status=status.HTTP_200_OK)

class BedsUpdateActiveView(generics.GenericAPIView):
    serializer_class = BedsActiveUpdateSerializer
    permission_classes = [IsAuthenticated]

    def put(self, request, bed=None):
        bed = Beds.objects.filter(id=bed)

        if bed.exists():
            serializer = self.serializer_class(bed, data=request.data, context={'user': request.user})
            if(not(serializer.is_valid())):
                return Response({'message': 'Falha ao atualizar os dados do leito.', 'data': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
            serializer_data = self.serializer_class(serializer.save()).data

        else:
            return Response({'message': 'Leito não encontrado.'}, status=status.HTTP_404_NOT_FOUND)

        return Response({'message': 'Dados do leito atualizado com sucesso.', 'data': [serializer_data]}, status=status.HTTP_200_OK)

class BedsUpdateStatusView(generics.GenericAPIView):
    serializer_class = BedsStatusUpdateSerializer
    permission_classes = [IsAuthenticated]

    def put(self, request, bed=None):
        bed = Beds.objects.filter(id=bed)

        if bed.exists():
            serializer = self.serializer_class(bed, data=request.data, context={'user': request.user})
            if(not(serializer.is_valid())):
                return Response({'message': 'Falha ao atualizar os dados do leito.', 'data': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
            serializer_data = self.serializer_class(serializer.save()).data
            typeOccupation = TypeOccupation.objects.filter(pk=serializer_data['type_occupation']).first()
            data = {
                'id': serializer_data['id'],
                'type_occupation_id': serializer_data['type_occupation'],
                'type_occupation_status': typeOccupation.status,
                'type_occupation_description': typeOccupation.description
            }

        else:
            return Response({'message': 'Leito não encontrado.'}, status=status.HTTP_404_NOT_FOUND)

        return Response({'message': 'Dados do leito atualizado com sucesso.', 'data': [data]}, status=status.HTTP_200_OK)

class BedsDeleteView(generics.GenericAPIView):
    permission_classes = [IsAuthenticated]

    def delete(self, request, bed=None):
        data = Beds.objects.filter(id=bed)

        if(data.exists()):
            id_leito = data.first().pk
            if(data.values('type_occupation__status').first()['type_occupation__status'] == 'OCUPADO'):
                return Response({'message': 'Leito ocupado não pode ser deletado.'}, status=status.HTTP_400_BAD_REQUEST)
            data.delete()

        else:
            return Response({'message': 'Leito não encontrado.'}, status=status.HTTP_404_NOT_FOUND)

        return Response({'message': 'Dados do leito removido com sucesso.', 'data': [{'id': id_leito}]}, status=status.HTTP_200_OK)

class BedsView(generics.GenericAPIView):
    serializer_class = BedsSerializer
    permission_classes = [IsAuthenticated]

    def post(self, request):

        serializer = self.serializer_class(data=request.data, context={'user': request.user})

        if not(serializer.is_valid()):
            return Response({'message': 'Falha ao cadastrar o leito , verifique os dados e tente novamente.', 'data': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

        data = serializer.save()
        sector = Sectors.objects.filter(id=data.sector.id).first()

        if data.type_occupation is None:
            typeOccupation = TypeOccupation.objects.filter(status='VAGO').first()
        else:
            typeOccupation = TypeOccupation.objects.filter(description=data.type_occupation).first()

        if data.type is None:
            type = None
        else:
            type = Type.objects.filter(description=data.type).first().description

        result = {
            'id': data.pk,
            'hospital_id': sector.hospital.id,
            'sector_id': sector.id,
            'name': data.name,
            'type_occupation_id': typeOccupation.id,
            'type_occupation_status': typeOccupation.status,
            'type_occupation_description': typeOccupation.description,
            'type': type,
            'is_active': data.is_active,
            'is_extra': data.is_extra
        }

        return Response({'message': 'Leito cadasrtado com sucesso', 'data': [result]}, status=status.HTTP_200_OK)


# https://atcoder.jp/contests/abc160/tasks/abc160_d
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

N, X, Y = map(int, input().split())
ans = [0]*(N-1)
for i in range(N-1):
    for j in range(i+1,N):
        dist = min(j-i, min(abs(X-1-i)+1+abs(Y-1-j), abs(Y-1-i)+1+abs(X-1-j)))
        ans[dist-1] += 1
for i in range(N-1):
    print(ans[i])

# ans = [0]*(N-1)
# branch1 = X-1
# branch2 = N-Y
# cycle = Y-X+1
# for i in range(cycle//2):   # 輪の1/2まで両側(2通り）で行ける
#     ans[i] += cycle
# if cycle%2==0:     # 輪は偶数（両方から同じ距離がある）
#     ans[cycle//2-1] -= cycle//2
# ans[0] -= 1

# for i in range(branch1):
#     for j in range(cycle//2):
#         if cycle%2==0 and j==cycle//2-1:
#             ans[i+j+1] += 1
#         elif j == 0:    # 直線経路は除外
#             ans[i+j+1] += 1
#         else:
#             ans[i+j+1] += 2
# for i in range(branch2):
#     for j in range(cycle//2):
#         if cycle%2==0 and j==cycle//2-1:
#             ans[i+j+1] += 1
#         elif j == 0:    # 直線経路は除外
#             ans[i+j+1] += 1
#         else:
#             ans[i+j+1] += 2

# for i in range(branch1+branch2+1):   # 直線経路
#     ans[i] += branch1+branch2+1 - i

# for i in range(N-1):
#     print(ans[i])

# # X = int(input())
# # S = input()
# # A = list(map(int, (input().split())))
# # A = [[int(i) for i in input().split()] for _ in range(N)]


class ContaCorrente:
    def __init__(self, codigo):
        self.codigo = codigo
        self.saldo = 0

    def deposita(self, valor):
        self.saldo += valor

    def __str__(self):
        return f"[>>Codigo {self.codigo} Saldo {self.saldo}<<]"

thiago = ContaCorrente(15)
thiago.deposita(300)
print(thiago)

dani = ContaCorrente(47685)
dani.deposita(1000)
print(dani)

contas = [thiago, dani]
print(contas)

for conta in contas:
    print(conta)

def deposita_contas(contas):
    for conta in contas:
        conta.deposita(100)

deposita_contas(contas)

for conta in contas:
    print(conta)

Python para Web/Introdução a Collections P1/objetos.py


import cv2

# left_eye = (235, 282 )
# right_eye = (245, 279)
# nose = (240,285)
img = cv2.imread(r'B:\Data\retinaface\trian\images\9--Press_Conference\9_Press_Conference_Press_Conference_9_6.jpg')
with open(r'B:\Data\retinaface\trian\customize_txt\9--Press_Conference\9_Press_Conference_Press_Conference_9_6.txt', 'r') as f:
    lines = f.readlines()
    for str_lst in lines:
        str_lst = str_lst.split()

        boxes = list(map(float,str_lst))
        bbox = list(map(int,boxes[:4]))
        # left_eye = (int(boxes[4]), int(boxes[5]))
        # right_eye = (int(boxes[6]),int(boxes[7]))
        # nose = (int(boxes[8]),int(boxes[9]))
        # left_mouth = (int(boxes[10]),int(boxes[11]))
        # right_mouth = (int(boxes[12]),int(boxes[13]))
        cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 1), 1)

        # cv2.circle(img, left_eye, 1, (0, 255, 0), 1)
        # cv2.circle(img, right_eye, 1, (0, 255, 0), 1)
        # cv2.circle(img, nose, 1, (0, 255, 0), 1)
        # cv2.circle(img, left_mouth, 1, (0, 255, 0), 1)
        # cv2.circle(img, right_mouth, 1, (0, 255, 0), 1)
# bbox_str = "449.00000 330.00000 571.00000 479.00000".split()
# bbox = list(map(float,bbox_str))
# bbox = list(map(int,bbox))
# bbox = (233, 277,21+233,23+277 )

# img = cv2.imread(r'D:\Data\widerface\train\images\0--Parade\0_Parade_marchingband_1_205.jpg')
# cv2.rectangle(img,(bbox[0], bbox[1]),(bbox[2], bbox[3]),(0,255,1),1)
#
#
# cv2.circle(img,left_eye,1,(0,255,0),1)
# cv2.circle(img,right_eye,1,(0,255,0),1)
# cv2.circle(img,nose,1,(0,255,0),1)
# cv2.circle(img,left_mouth,1,(0,255,0),1)
# cv2.circle(img,right_mouth,1,(0,255,0),1)
# 109.732,600.411,128.001,621.304
# 809.41,190.884,820.298,203.93
# 306.468,239.821,330.628,267.884
#564.0 151.0 826.0 441.0
cv2.rectangle(img,(564,151),(826,441),(0,255,1),1)

cv2.imshow('x',img)
cv2.waitKey(0)
# cv2.imwrite('xxx.jpg',img)


def MergeSortAlgo(a,low,mid,high):
    left=a[low:mid+1]
    right=a[mid+1:high+1]
    i=j=0
    k=low
    m=len(left)
    n=len(right)
    while i<m and j<n:
        if left[i]<right[j]:
            a[k]=left[i]
            i=i+1
            k=k+1
        else:
            a[k]=right[j]
            j=j+1
            k=k+1

    while i<m:
        a[k]=left[i]
        i=i+1
        k=k+1

    while j<n:
        a[k]=right[j]
        j=j+1
        k=k+1

def merge(arr,l,r):
    if r>l:
        mid=(l+r)//2
        merge(arr,l,mid)
        merge(arr,mid+1,r)
        MergeSortAlgo(arr,l,mid,r)

arr=[10,5,30,15,7]

merge(arr,0,4)
print(*arr)


#Imports
import random
from sense_hat import SenseHat
from time import sleep

#Instance af sensehat for using the joystick events and displaying on the senseHat display
sense = SenseHat()

#Global variables
x, y = 4, 4

curDir = [1,0]

score = 0

fruit = 0

tick = 1

Snake = [[2,4],
         [3,4],
         [4,4]]

Fruit = [0,0]

#Function for calculating and changing the global variable "score"
def calcScore():
    global score, tick
    tick = round(tick,2)
    score = int(score)
    score += (fruit * 5)

#Function for declaring death then displaying the score and speed credits on the senseHat display and allowing closing the game by hitting the middle joyStick
def declareDeath():
    global score, tick
    while True:
        for event in sense.stick.get_events():
            if event.direction == 'middle':
                print("GAME CLOSED")
                quit()
        sense.clear()
        #FYI scroll_speed is amount of pixels it moves per second
        sense.show_message("SCORE: "+str(score), text_colour=(255,0,0), scroll_speed=0.1)
        sense.show_message("SPEED: "+str(tick),text_colour=(255,0,255), scroll_speed=0.1)

#Function for speeding up the global variable time by 25% of current speed per function-call
def speedUp():
    global tick
    tick *= 0.75

#Function for drawing in the snake on the display by looping through the entire Snake array
#And then getting the coordinates for each bodypart and then drawing in a pixel at set coordinate
def drawSnake():
    for bodyPart in Snake:
        sense.set_pixel(bodyPart[0], bodyPart[1],255,0,255)

#Function for drawing in fruits
#Also ensures that the fruit does not spawn ontop of the snake by checking against every possible snake bodypart
#In case that it would spawn ontop of the snake then it returns another drawFruit call which esssentially breaks out of itself and runs itself again
def drawFruit():
    global Fruit
    r1 = random.randint(0,7)
    r2 = random.randint(0,7)
    for bodyPart in Snake:
        if r1 == bodyPart[0] and r2 == bodyPart[1]:
            print("Fruit spawn - Failed - at",r1,",",r2)
            return drawFruit()
    Fruit = [r1,r2]
    sense.set_pixel(Fruit[0], Fruit[1],255,0,0)
    print("Fruit spawn - Success - at",r1,",",r2)

#Function for moving the x,y by adding the numbers given in the direction array
def move(direction):
    global y, x, curDir
    curDir = direction
    x += direction[0]
    y += direction[1]

#Function for checking the current position of the snake and then calling the drawFruit(), calcScore(), declareDeath() and speedUp() functions depending on position
#Checks if the snake head eats the fruit position
#Also removes end of the snake unless a fruit was eaten
def checkPosition():
    global score,fruit
    if y == 8 or x == 8 or y == -1 or x == -1:
        calcScore()
        print("Snake hit the edge")
        declareDeath()
    for sn in Snake:
        if sn == [x,y]:
            calcScore()
            print("Snake ate itself")
            declareDeath()
    if x == Fruit[0] and y == Fruit[1]:
        print('Fruit eaten - at',Fruit[0],",",Fruit[1])
        drawFruit()
        fruit += 1
        if fruit % 3 == 0:
            speedUp()
    else:
        sense.set_pixel(Snake[0][0], Snake[0][1],0,0,0) #fjerner røv-pixelen på slangen
        Snake.remove(Snake[0]) #fjerner røven.
    Snake.append([x,y]) #tilføjer hoved
    score += 0.1

#Initial clears and draws of the snake
sense.clear()
drawSnake()
drawFruit()

#Running Code
while True:
    print("Snake head at",x,",",y,"going",curDir,"at",tick,"speed")
    drawSnake()
    sleep(tick)
    for event in sense.stick.get_events():
        print(event.direction,"was", event.action)
        if event.direction == 'up' and (event.action == 'pressed' or event.action == 'held'):
            move([0,-1])
            break
        elif event.direction == 'down' and (event.action == 'pressed' or event.action == 'held'):
            move([0,1])
            break
        elif event.direction == 'left' and (event.action == 'pressed' or event.action == 'held'):
            move([-1,0])
            break
        elif event.direction == 'right' and (event.action == 'pressed' or event.action == 'held'):
            move([1,0])
            break
    else:
        move(curDir)

    checkPosition()


from skimage import io
from sklearn.cluster import KMeans
import numpy as np
import warnings
import os

warnings.filterwarnings('ignore')

def read_image(file_path: str) -> np.ndarray:
    """
    读取图片文件并返回对应的NumPy数组。
    """
    img = io.imread(file_path)
    return img

def kmeans_clustering(image: np.ndarray, k: int) -> tuple:
    """
    使用K均值聚类对图像进行分组，并返回聚类结果和中心点。
    """
    img_ori_shape = image.shape
    img_flat = image.reshape((img_ori_shape[0] * img_ori_shape[1], img_ori_shape[2]))

    n_channels = img_flat.shape[1]

    estimator = KMeans(n_clusters=k, max_iter=4000, init='k-means++', n_init=50)
    estimator.fit(img_flat)

    centroids = estimator.cluster_centers_
    labels = estimator.labels_

    return centroids, labels, n_channels

def visualize_colors(centroids: np.ndarray, labels: np.ndarray, k: int) -> None:
    """
    可视化聚类结果，输出每个聚类中心的颜色以及对应的比例。
    """
    color_labels = list(labels)
    color_info = {}

    for center_index in range(k):
        color_ratio = color_labels.count(center_index) / len(color_labels)
        color_info[color_ratio] = centroids[center_index]

    color_info = [(k, color_info[k]) for k in sorted(color_info.keys(), reverse=True)]

    for color in color_info:
        print('比例：', color[0], '颜色：', color[1])

def generate_visualization_matrix(centroids: np.ndarray, k: int, result_width: int, result_height_per_center: int) -> np.ndarray:
    """
    使用聚类结果生成一个矩阵，用于数据可视化。
    """
    result = []

    for center_index in range(k):
        result.append(np.full((result_width * result_height_per_center, n_channels), centroids[center_index], dtype=int))

    result = np.array(result)
    result = result.reshape((result_height_per_center * k, result_width, n_channels))

    # 转换数据类型为uint8
    result = result.astype(np.uint8)

    return result

def save_visualization_result(image_file: str, result: np.ndarray) -> None:
    """
    保存可视化结果图片。
    """
    io.imsave(os.path.splitext(image_file)[0] + '_result.png', result)

# 主程序
img_file = 'city.jpg'
k_value = 5
result_width_value = 200
result_height_per_center_value = 80

image = read_image(img_file)
centroids, labels, n_channels = kmeans_clustering(image, k_value)

visualize_colors(centroids, labels, k_value)

visualization_matrix = generate_visualization_matrix(centroids, k_value, result_width_value, result_height_per_center_value)

save_visualization_result(img_file, visualization_matrix)