import pandas as pd
import numpy as np

'''
chunks = pd.read_csv(path+'TCGA_breast_gene_expression.txt',sep='\t',iterator = True)
chunk = chunks.get_chunk(100000)

chunk = chunk[["Sample name"]]
chunk = chunk.drop_duplicates(keep='first')

print(chunk)
'''

#-------------------------------------------EXPRESSION------------------------------------------------------------------
# breast cancer cell lines
'''
df = pd.read_table(path + 'CCLE_RNAseq_rsem_genes_tpm_20180929.txt', sep='\t')
Cell_lines = list(df.columns.values)  # all cancer line information
Breast_Cell_lines = ['gene_id', 'transcript_ids']

# Other Cell Lines....
for i in range(len(Cell_lines)):
    if Cell_lines[i].find( 'BREAST') != -1:  # BREAST pattern match
        Breast_Cell_lines.append(Cell_lines[i])

print(Breast_Cell_lines)  # see all the breast cancer lines
print(len(Breast_Cell_lines))

f = df[Breast_Cell_lines]
f.to_csv(path + 'CCLE_gene_expression_train(breast).txt', sep='\t', index=False)
'''

# eliminate useless genes
'''
from numpy import *
path = "D:\Study\CIS\数据集\CCLE\\"
df = pd.read_table(path + 'CCLE_gene_expression_train(cgc).txt', sep='\t')
# df.drop('transcript_ids', axis=1, inplace=True)

dfList = []

for i in df.index:
    rowData = df.loc[i].values[:]
    rowData = rowData.tolist()
    if mean(rowData[1:])<1 or var(rowData[1:])<0.5:
        continue
    dfList.append(rowData)

df = pd.DataFrame({dfList[i][0]: dfList[i][1: ] for i in range(0, len(dfList))})
df = df.T

df2 = pd.read_table(path + 'CCLE_gene_expression_train(breast).txt', sep='\t')
df.columns = df2.columns.values[2: ]

df.to_csv(path + 'CCLE_gene_expression_train(cgc+eliminated).txt', sep='\t', index=False)
'''

# Find corresponding cancer cell lines in GDSC
'''
def CCLE2GDSC(str):
    index = str.find('_')
    res = str[0: index]
    iter = 0
    while iter<len(res) and (res[iter]<'0' or res[iter]>'9'):
        iter += 1
    strList = list(res)
    strList.insert(iter, '-')

    return ''.join(strList), str[0: index]

path = "D:\Study\CIS\数据集\CCLE\\"
df = pd.read_table(open(path + 'CCLE_gene_expression_train(breast).txt'), sep='\t')
df = df.drop('gene_id', axis=1)
df = df.drop('transcript_ids', axis=1)
CCLE_CellLines = list(df.columns.values)  # all cancer line information
Revised_CellLines = CCLE_CellLines[:]
iter = 0
for iter in range(0, len(CCLE_CellLines)):
    temp = CCLE_CellLines[iter]
    Revised_CellLines[iter], CCLE_CellLines[iter] = CCLE2GDSC(temp)

path = "D:\Study\CIS\数据集\GDSC\\"
df = pd.read_table(open(path + 'GDSC1_fitted_dose_response_25Feb20.txt'), sep='\t')
df = df[['CELL_LINE_NAME', 'DRUG_ID', 'DRUG_NAME', 'LN_IC50']]

CCLE_CellLines = set(CCLE_CellLines)
Revised_CellLines = set(Revised_CellLines)
combine = list(CCLE_CellLines | Revised_CellLines)

df = df.loc[df['CELL_LINE_NAME'].isin(combine)]

df.to_csv(path + 'GDSC1_(breast).txt', sep='\t', index=False)
'''

# Hugo nomenclature
'''
Breast_Cell_lines = ['gene_id', 'transcript_ids']
df = pd.read_table(path + 'CCLE_gene_expression_train(breast).txt', sep='\t')
gene_id_raw = df[Breast_Cell_lines]
mat1 = np.array(gene_id_raw)

df2 = pd.read_table(path + 'CCLE_RNAseq_genes_rpkm_20180929.txt')
Breast_Cell_lines = ['Name', 'Description']
gene_id_done = df2[Breast_Cell_lines]
mat2 = np.array(gene_id_done)

for j in range(mat1.shape[0]):
    # print(j)
    for i in range(mat2.shape[0]):
        if mat1[j][0] == mat2[i][0]:
            mat1[j][1] = mat2[i][1]
            # print("1")
            break
        else:
            mat1[j][1] = 0

mat1 = pd.DataFrame(mat1)
df['transcript_ids'] = mat1[1]
df.rename(columns={'transcript_ids':'Description'},inplace=True)
print(df.head())
df.to_csv(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t', index=False)
'''

# Intersection with CGC
'''
df = pd.read_table(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t')

path = "D:\Study\CIS\数据集\CGC\\"
df2 = pd.read_csv(path + 'Census_allThu Jul 30 07_07_22 2020.csv')
kk = df2['Gene Symbol']
kk = np.array(kk)
kk = kk.tolist()
kk = set(kk)

#df = pd.read_table(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t')
gene = df['Description']
gene = np.array(gene)
gene = gene.tolist()
gene = set(gene)

combine = list(kk & gene)

#df2 = pd.read_table(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t', index_col=0)
# print(df2['Gene'][1])

print(df.head())
ff = df.loc[df['Description'].isin(combine)]
print(ff.shape[0])
ff.to_csv(path + 'CCLE_gene_expression_train(cgc).txt', sep='\t')
'''

# Additional process
'''
path = "D:\Study\CIS\数据集\CCLE\\"
df = pd.read_table(path + 'CCLE_gene_expression_train(cgc).txt', sep='\t')
df = df.drop('num', axis=1)
# df = df.drop('gene_id', axis=1)
df.to_csv(path + 'CCLE_gene_expression_train(cgc).txt', sep='\t', index=False)
'''

#-------------------------------------------------MUTATION--------------------------------------------------------------
# breast cancer cell lines
'''
path = "D:\Study\CIS\数据集\CCLE\mutation\\"
df = pd.read_table(path + 'CCLE_MUT_CNA_AMP_DEL_binary_Revealer.tsv', sep='\t')
Cell_lines = list(df.columns.values)  # all cancer line information
Breast_Cell_lines = ['Name']

# Other Cell Lines....
for i in range(len(Cell_lines)):
    if Cell_lines[i].find( 'BREAST') != -1:  # BREAST pattern match
        Breast_Cell_lines.append(Cell_lines[i])

print(Breast_Cell_lines)  # see all the breast cancer lines
print(len(Breast_Cell_lines))
df = df[Breast_Cell_lines]
df = df.loc[ : 1631]

# df = df.loc[IsMUT(df['Name'])]
df["Name"] = df.apply(lambda row: row["Name"].split('_')[0],axis = 1)
df.to_csv(path + 'CCLE_gene_mutation_train(breast).txt', sep='\t', index=False)
'''

# Intersection with CGC
'''
path = "D:\Study\CIS\数据集\CCLE\mutation\\"
df = pd.read_table(path + 'CCLE_gene_mutation_train(breast).txt', sep='\t')

path = "D:\Study\CIS\数据集\CGC\\"
df2 = pd.read_csv(path + 'Census_allThu Jul 30 07_07_22 2020.csv')
kk = df2['Gene Symbol']
kk = np.array(kk)
kk = kk.tolist()
kk = set(kk)

#df = pd.read_table(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t')
gene = df['Name']
gene = np.array(gene)
gene = gene.tolist()
gene = set(gene)

combine = list(kk & gene)

#df2 = pd.read_table(path + 'CCLE_gene_expression_train(hugo).txt', sep='\t', index_col=0)
# print(df2['Gene'][1])
path = "D:\Study\CIS\数据集\CCLE\mutation\\"
print(df.head())
ff = df.loc[df['Name'].isin(combine)]
print(ff.shape[0])
ff.to_csv(path + 'CCLE_gene_mutation_train(cgc).txt', sep='\t', index=False)
'''

#-------------------------------------------DRUG_RESPONSE---------------------------------------------------------------
#
'''
path = "D:\Study\CIS\数据集\GDSC\\"
df = pd.read_table(open(path + 'PUBCHEM_id_GDSC1.txt'), sep='\t')
df2 = pd.read_table(open(path + 'SMILES_GDSC1.txt'), sep='\t')

df = pd.merge(df, df2)
df.to_csv(path + 'GDSC1_DRUG_SMILES.txt', sep='\t', index=False)
'''

# breast data in GDSC incoporated with SMILES representation of drugs
'''
path = "D:\Study\CIS\数据集\GDSC\\"
df = pd.read_table(open(path + 'DRUG_SMILES.txt'), sep='\t')
df2 = pd.read_table(open(path + 'GDSC1_(breast).txt'), sep='\t')
df = pd.merge(df, df2)

df = df[['CELL_LINE_NAME', 'DRUG_ID', 'SMILES_expression', 'LN_IC50']]
df.to_csv(path + 'CellLine_Smiles_IC50.txt', sep='\t', index=False)
'''

# CSI --> latent vectors of drugs + gene expression
'''
path = "D:\Study\CIS\数据集\GDSC\\"
df = pd.read_table(open(path + 'CellLine_Smiles_IC50.txt'), sep='\t')
df2 = pd.read_table(open(path + 'LatentVec_Drug(sampled).txt'), sep=' ')
df = pd.merge(df, df2, how='inner')

# df = df.drop('DRUG_ID', axis=1)
df = df.drop('SMILES_expression', axis=1)
# df = df.drop('LN_IC50', axis=1)
df["CELL_LINE_NAME"] = df.apply(lambda row: row["CELL_LINE_NAME"].replace('-', ''),axis = 1)

df2 = pd.read_table(open(path + 'LatentVec_GeneExp(cgc).tsv'), sep='\t')
df = pd.merge(df, df2, on="CELL_LINE_NAME", how='inner')

# df = df.drop('CELL_LINE_NAME', axis=1)
df.to_csv(path + 'LatentVec_Drug+GeneExp(cgc).txt', sep='\t', index=False)
'''

# CSI --> latent vectors of drugs + gene expression + gene mutation
'''
path = "D:\Study\CIS\数据集\GDSC\\"
df = pd.read_table(open(path + 'CellLine_Smiles_IC50.txt'), sep='\t')
df2 = pd.read_table(open(path + 'LatentVec_Drug.txt'), sep=' ')
df = pd.merge(df, df2, how='inner')

# df = df.drop('DRUG_ID', axis=1)
df = df.drop('SMILES_expression', axis=1)
# df = df.drop('LN_IC50', axis=1)
df["CELL_LINE_NAME"] = df.apply(lambda row: row["CELL_LINE_NAME"].replace('-', ''),axis = 1)

df2 = pd.read_table(open(path + 'LatentVec_GeneExp.tsv'), sep='\t')
df = pd.merge(df, df2, on="CELL_LINE_NAME", how='inner')
df2 = pd.read_table(open(path + 'LatentVec_GeneMut.tsv'), sep='\t')
df = pd.merge(df, df2, on="CELL_LINE_NAME", how='inner')

# df = df.drop('CELL_LINE_NAME', axis=1)
df.to_csv(path + 'LatentVec_Drug+GeneExp+GeneMut.txt', sep='\t', index=False)
'''

# Dispose of the column name
'''
path = "C:\\Users\dhy\Desktop\新建文件夹\\"
df = pd.read_table(open(path + 'ge_encoded_rnaseq_twohidden_warmup_batchnorm(cgc).tsv'), sep='\t')

df.columns = df.columns.map(lambda x: 'ge'+str(int(x)-1))
df.to_csv(path + 'LatentVec_GeneExp(cgc).tsv', sep='\t', index=False)
'''

# Dispose of the row name

path = "D:\Study\CIS\数据集\CCLE\\"
df2 = pd.read_table(open(path + 'CCLE_gene_expression_train(cgc).txt'), sep='\t')
path = "C:\\Users\dhy\Desktop\新建文件夹\\"
df = pd.read_table(open(path + 'LatentVec_GeneExp(cgc).tsv'), sep='\t')

for i in range(0, df.shape[0]):
    df.loc[i, 'CELL_LINE_NAME'] = df2.columns.values[i+1].split('_')[0]

df.to_csv(path + 'LatentVec_GeneExp(cgc).tsv', sep='\t', index=False)

# Final Result
'''
from numpy import *
df = pd.read_table(open('.\mlps_drug_exp\R2_Score(cgc+sampledDrug).txt'), sep='\t')
result = var(df.loc[: , 'R2_test'])
print(result)
pass
'''


# -*- coding: utf-8 -*-
"""
Created on Fri Feb  2 11:31:38 2018

@author: dungnq9
"""
import numpy as np

# test
w = np.array([[2], [2]])
print(len(w))
print(np.linalg.norm(w))
print(np.linalg.norm(w)/len(w))
print(np.linalg.norm(w)**2)

print(np.zeros_like(w))

# check convergence
def has_converged(theta_new, grad):
    return np.linalg.norm(grad(theta_new))/len(theta_new) < 1e-3

""" Momentum GD """
def GD_momentum(theta_init, grad, eta, gamma):
    # Suppose we want to store history of theta
    theta = [theta_init]
    v_old = np.zeros_like(theta_init)
    for it in range(100):
        v_new = gamma*v_old + eta*grad(theta[-1])
        theta_new = theta[-1] - v_new
        theta.append(theta_new)
        if has_converged(theta_new, grad):
            break
        v_old = v_new
    return theta
    # this variable includes all points in the path
    # if you just want the final answer
    # use 'return theta[-1]'

""" Nesterov accelerated gradient (NAG) """
def GD_NAG(theta_init, grad, eta, gamma):
    # SUppose we want to stor history of theta
    theta = [theta_init]
    v_old = np.zeros_like(theta_init)
    for it in range(100):
        v_new = gamma*v_old + eta*grad(theta[-1] - gamma*v_old)
        theta_new = theta[-1] - v_new
        theta.append(theta_new)
        if has_converged(theta_new, grad):
            break
        v_old = v_new
    return theta

""" Batch Gradient Descent """
""" Gradient is calculated on all data points for each loop """

""" Stochastic Gradient Descent """
""" At a time, gradient is only calculated based on only one data point,
    then update w based on this gradient
"""
"""
SGD chỉ yêu cầu một lượng epoch rất nhỏ (thường là 10 cho lần đầu tiên,
sau đó khi có dữ liệu mới thì chỉ cần chạy dưới một epoch là đã có nghiệm tốt).
Vì vậy SGD phù hợp với các bài toán có lượng cơ sở dữ liệu lớn
(chủ yếu là Deep Learning mà chúng ta sẽ thấy trong phần sau của blog)
và các bài toán yêu cầu mô hình thay đổi liên tục, tức online learning.

Một điểm cần lưu ý đó là: sau mỗi epoch, chúng ta cần shuffle (xáo trộn)
thứ tự của các dữ liệu để đảm bảo tính ngẫu nhiên. Việc này cũng ảnh hưởng
tới hiệu năng của SGD.
"""
# single point gradient
def sgrad(w, i, rd_id):
    true_i = rd_id[i]
    xi = Xbar[true_i, :]
    yi = y[true_i]
    a = np.dot(xi, w) - yi
    return (xi*a).reshape(2, 1)

def SGD(w_init, sgrad, eta):
    w = [w_init]
    w_last_check = w_init
    iter_check_w = 10
    N = X.shape[0]
    count = 0
    for it in range(10):
        # shuffle data
        rd_id = np.random.permutation(N)
        for i in range(N):
            count += 1
            g = sgrad(w[-1], i, rd_id)
            w_new = w[-1] - eta*g
            w.append(w_new)
            if count%iter_check_w == 0:
                w_this_check = w_new
                if np.linalg.norm(w_this_check - w_last_check)/len(w_init) < 1e-3:
                    return w
                w_last_check = w_this_check
    return w

""" Mini_batch Gradient Descent """
""" The code below is applied for Linear Regression
    For other algorithms, we need to update Gradient function
"""

X = np.random.rand(1000, 1)
y = 4 + 3*X + .2*np.random.randn(1000, 1) # noise added

# Building Xbar
one = np.ones((X.shape[0],1))
Xbar = np.concatenate((one, X), axis = 1)

# mini-batch gradient
def mbgrad(w, istart, iend, rd_id):
    range_i = rd_id[istart:iend]
    Xi = Xbar[range_i, :]
    Yi = y[range_i]
    A = np.dot(Xi, w) - Yi
    return np.dot(Xi.T, A).reshape(Xbar.shape[1], 1)

def mbGD(w_init, mbgrad, eta):
    w = [w_init]
    w_last_check = w_init
    iter_check_w = 10
    N = X.shape[0]
    batch_size = 50
    no_of_batch = int(N/batch_size)
    print(no_of_batch)
    count = 0
    for it in range(10):
        # shuffle data
        rd_id = np.random.permutation(N)
        for i in range(no_of_batch):
            count += 1
            g = mbgrad(w[-1], i*50, i*50 + 49, rd_id)
            print("g", i, " = ", g)
            w_new = w[-1] - eta*g
            w.append(w_new)
            if count%iter_check_w == 0:
                w_this_check = w_new
                """
                Stopping criteria: compare w solutions after 10 updates
                """
                if np.linalg.norm(w_this_check - w_last_check)/len(w_init) < 1e-3:
                    return w
                w_last_check = w_this_check
    return w
""" Great learning rate eta causes the algorithm to be diverged, NOT converged """
#w_list = mbGD(np.random.rand(2,1), mbgrad, 1)
#w_list = mbGD(np.random.rand(2,1), mbgrad, 0.1)
w_list = mbGD(np.random.rand(2,1), mbgrad, 0.01)
#print("Result of mini-batch GD = ", w_list)
print("Parameter w of mini-batch GD = ", w_list[-1])


import tkinter as tk

counter = 0
def counter_label(label):
    counter = 0
    def count():
        global counter
        counter +=1
        label.config(text=str(counter))
        label.after(1000, count)
    count()

root = tk.Tk()
root.title("Counting")


__author__ = 'shmakovs'

def GetContigToTaxIDDict():
    ContigToTaxCategory = dict()

    TaxTabFileName = "//panfs/pan1/prokdata/db_tmp/all1603.tax.tab"
    ContigsFileName = "//panfs/pan1/prokdata/db_tmp/all1603.pp.txt"

    OrganismNameToTaxID = dict()

    with open(TaxTabFileName, "r") as TaxTabFile:
        for Line in TaxTabFile:
            if Line[0] == "#":
                continue
            LineValues = Line[:-1].split("\t")
            OrganismNameToTaxID[LineValues[0]] = LineValues[3].split(";")[3]

    with open(ContigsFileName, "r") as ContigsFile:
        for Line in ContigsFile:
            if Line[0] == "#":
                continue
            LineValues = Line[:-1].split("\t")
            ContigToTaxCategory[LineValues[1]] = OrganismNameToTaxID[LineValues[0]]

    return ContigToTaxCategory

# to get stats from uniq spacers per CRISPR array
def GetCRISPRID(SpacerID):
    LineValues = SpacerID.split("_")
    return LineValues[0] + "_" + LineValues[1] + "_" + LineValues[2]

ContigToTaxCategory = GetContigToTaxIDDict()

CRISPRTypesFileName = "//panfs/pan1/prokdata/CRISPRicity/SpacerAnalysis/AllIteration/TmpFolder/CRISPR_info_known_091216.tsv"
CRISPRArrayInfoFileName = "//panfs/pan1/prokdata/CRISPRicity/SpacerAnalysis/AllIteration/TmpFolder/SpacersSummary_new.tsv"
UniqSpacerSetFileName = "//panfs/pan1/prokdata/CRISPRicity/SpacerAnalysis/AllIteration/TmpFolder/KnownSpacers_Unique_121416.fna"

ClearTaxCategoryDict = dict()
for Line in open(CRISPRTypesFileName, "r"):
    LineValues = Line[:-1].split("\t")
    ClearTaxCategoryDict[LineValues[1] + "_" + LineValues[2] + "_" + LineValues[3]] = LineValues[9]

SpacersInfoDict = dict()
with open(CRISPRArrayInfoFileName, "r") as CRISPRArrayInfo:
    for Line in CRISPRArrayInfo:
        LineValues = Line[:-1].split("\t")
        SpacersInfoDict[LineValues[0]] = LineValues[1:]

ClearCRISPRTypesDict = dict()
for Line in open(CRISPRTypesFileName, "r"):
    LineValues = Line[:-1].split("\t")
    ClearCRISPRTypesDict[LineValues[1] + "_" + LineValues[2] + "_" + LineValues[3]] = LineValues[9]

TaxCategoryDict = dict()

DICT_NoHits = 0
DICT_VirusHits = 1
DICT_Prok = 2
DICT_Prok_ORF = 0
DICT_Prok_Intergenic = 1

INFO_HitType = 0
INFO_Profile = 1
INFO_PhageNearby = 2
INFO_VirusHit = 3

AllCounter = 0
NoBadCounter = 0

print("Loading done")

#with open(ResFileName, "w") as ResFile:
Count = 0
for Line in open(UniqSpacerSetFileName, "r"):
    if Line[0] != ">":
        continue

    SpacerID = Line[1:-1]
    CRISPRID = GetCRISPRID(SpacerID)

    TaxCategory = ContigToTaxCategory[SpacerID.split("_")[0]]
    if not TaxCategory in TaxCategoryDict:
        TaxCategoryDict[TaxCategory] = dict({"CAS-I" : 0, "CAS-I-A" : 0, "CAS-I-B" : 0, "CAS-I-C" : 0, "CAS-I-D" : 0, "CAS-I-E" : 0, "CAS-I-F" : 0, "CAS-I-U" : 0, "CAS-II-A" : 0, "CAS-II-B" : 0, "CAS-II-C" : 0, "CAS-III" : 0, "CAS-III-A" : 0, "CAS-III-B" : 0, "CAS-III-C" : 0, "CAS-III-D" : 0, "CAS-IV-A" : 0, "CAS-V-A" : 0, "CAS-V-B" : 0, "CAS-VI-A" : 0, "CAS-VI-B" : 0, "CAS-VI-C" : 0, "CAS-V-U" : 0, "Unidentified" : 0})

    CRISPRType = ClearCRISPRTypesDict[CRISPRID]
    if CRISPRType == "CAS-II-U":
        continue

    TaxCategoryDict[TaxCategory][CRISPRType] += 1

    # if not SpacerID in SpacersInfoDict:
    #     continue
    #
    # SpacerInfo = SpacersInfoDict[SpacerID]
    #
    # if SpacerInfo[INFO_HitType] == "CRISPR":
    #     continue

    # # TaxCategoryDict[TaxCategory][CRISPRType] += 1
    #
    # if SpacerInfo[INFO_HitType] == "Phage": # good blast phage hit
    #     TaxCategoryDict[TaxCategory][CRISPRType] += 1
    # elif SpacerInfo[INFO_HitType] == "Intergenic":
    #     if (SpacerInfo[INFO_PhageNearby] == "PhageNearby") or (SpacerInfo[INFO_VirusHit] == "VirusHit"):
    #         TaxCategoryDict[TaxCategory][CRISPRType] += 1
    #     # else:
    #     #     TaxCategoryDict[TaxCategory][DICT_Prok][DICT_Prok_Intergenic] += 1
    # else: # ORF hit
    #     if (SpacerInfo[INFO_PhageNearby] == "PhageNearby") or (SpacerInfo[INFO_VirusHit] == "VirusHit"):
    #         TaxCategoryDict[TaxCategory][CRISPRType] += 1
    #     # else:
    #     #     TaxCategoryDict[TaxCategory][DICT_Prok][DICT_Prok_ORF] += 1

print("Phylum" + "\t" + "\t".join(["CAS-I", "CAS-I-A", "CAS-I-B", "CAS-I-C", "CAS-I-D", "CAS-I-E", "CAS-I-F", "CAS-I-U", "CAS-II-A", "CAS-II-B", "CAS-II-C", "CAS-III", "CAS-III-A", "CAS-III-B", "CAS-III-C", "CAS-III-D", "CAS-IV-A", "CAS-V-A", "CAS-V-B", "CAS-VI-A", "CAS-VI-B", "CAS-VI-C", "CAS-V-U", "Unidentified"]))
for TaxCategory in TaxCategoryDict:
    CasHits = ""
    # for CRISPRCas in ["CAS-I", "CAS-I-A", "CAS-I-B", "CAS-I-C", "CAS-I-D", "CAS-I-E", "CAS-I-F", "CAS-I-U", "CAS-II-A", "CAS-II-B", "CAS-II-C", "CAS-III", "CAS-III-A", "CAS-III-B", "CAS-III-C", "CAS-III-D", "CAS-IV-A", "CAS-V-A", "CAS-V-B", "CAS-VI-A", "CAS-VI-B", "CAS-VI-C", "CAS-V-U", "Unidentified"]:
    #     CasHits += TaxCategoryDict[TaxCategory][CRISPRCas] + ""

    CasHits = "\t".join([str(TaxCategoryDict[TaxCategory][x]) for x in ["CAS-I", "CAS-I-A", "CAS-I-B", "CAS-I-C", "CAS-I-D", "CAS-I-E", "CAS-I-F", "CAS-I-U", "CAS-II-A", "CAS-II-B", "CAS-II-C", "CAS-III", "CAS-III-A", "CAS-III-B", "CAS-III-C", "CAS-III-D", "CAS-IV-A", "CAS-V-A", "CAS-V-B", "CAS-VI-A", "CAS-VI-B", "CAS-VI-C", "CAS-V-U", "Unidentified"]])

    print(TaxCategory + "\t" + CasHits)


import re

from ansiblelater.candidate import Template
from ansiblelater.standard import StandardBase

class CheckCompareToEmptyString(StandardBase):

    sid = "ANSIBLE0012"
    description = "Don't compare to empty string \"\""
    helptext = ("use `when: var` rather than `when: var !=` (or conversely `when: not var`)")
    version = "0.1"
    types = ["playbook", "task", "handler", "template"]

    def check(self, candidate, settings):
        yamllines, errors = self.get_normalized_yaml(candidate, settings)

        if not errors:
            if isinstance(candidate, Template):
                matches = []
                jinja_string = re.compile("({{|{%)(.*?)(}}|%})")

                for i, line in yamllines:
                    match = jinja_string.findall(line)
                    if match:
                        for item in match:
                            matches.append((i, item[1]))

                yamllines = matches

            empty_string_compare = re.compile("[=!]= ?[\"'][\"']")

            for i, line in yamllines:
                if empty_string_compare.findall(line):
                    errors.append(self.Error(i, self.helptext))

        return self.Result(candidate.path, errors)


from roblib import *  # available at https://www.ensta-bretagne.fr/jaulin/roblib.py
fig = figure()
ax = Axes3D(fig)

m,g,b,d,l=10,9.81,2,1,1
I=array([[10,0,0],[0,10,0],[0,0,20]])
dt = 0.01
B=array([[b,b,b,b],[-b*l,0,b*l,0],[0,-b*l,0,b*l],[-d,d,-d,d]])

def draw_quadri(x): # vecteur d'état x,y,z, angles d'Euler
    ax.clear()
    clean3D(ax,-30,30,-30,30,0,30)
    draw_quadrotor3D(ax,x,α,5*l)    # we infate the robot, just to see something

def f(x,w):
    x=x.flatten()
    φ,θ,ψ=x[3:6]
    vr=(x[6:9]).reshape(3,1)
    wr=(x[9:12]).reshape(3,1)
    w2=w*abs(w)
    τ=B@w2.flatten()
    E=eulermat(φ,θ,ψ)
    dp=E@vr
    dvr=-adjoint(wr)@vr+inv(E)@array([[0],[0],[g]])+array([[0],[0],[-τ[0]/m]])
    dφθψ= eulerderivative(φ,θ,ψ) @ wr
    dwr= inv(I)@(-adjoint(wr)@I@wr+τ[1:4].reshape(3,1))
    return  vstack((dp,dφθψ,dvr,dwr))

def control(x):
    return array([[6],[5],[5],[5]])

x = array([[0,0,-5,0,0,0,10,0,0, 0,0,0]]).T  #x,y,z,   φ,θ,ψ   vr  wr (front,right,down)
α=array([[0,0,0,0]]).T #angles for the blades

for t in arange(0,0.1*3,dt):
    w=control(x)
    xdot=f(x,w)
    x  = x + dt*xdot
    draw_quadri(x)
    α=α+dt*10*w
    pause(0.001)
pause(1)


import math
import time

time.time()
factors = []
primes = []
a = math.sqrt(600851475143)
a = math.ceil(a)
b = 600851475143

#making the factors list
for number in range(2,a):
	if b % number == 0:
		factors.append(number)

print(factors)

#Checking which of the factors are primes, and storing them in the list
#of factors
#But for our huge number 600851475143 this program will not work as it
#takes too long
#But we can use the square root of the number as there cannot
#be any more factors after the square root of the number

for number in factors:
	for i in range(2,number+1):
		if i != number and number % i == 0:
			break
		elif number == i:
			primes.append(number)
			break

print(primes)
print(max(primes))
print(60/time.time())


"""
Created on Wed Mar 15 16:52:03 2017

@author: elinlarsen
"""

#import libraries
import os
import pandas as pd

# importing python scripts

#PUT your directory path where the wordSegComprehension package is stored
PATH_PIPELINE="/Users/elinlarsen/Google Drive/"
os.chdir(PATH_PIPELINE + '/WordSegComprehension/wordsegcomp/pipeline')

import translate
import visualize
import model
import get_lexical_class
import analyze
import read

# parameters
from CDI import df_CDI_lexical_classes
from CDI import length_type

#PUT your directory storing results from word segmentation algorithm directory here
path_res='/Users/elinlarsen/Google Drive/CDSwordSeg_Pipeline/results/res-brent-CDS'
#folder directory storing the results for the correlation
folder_res="Analysis_algos_CDI"
os.chdir(path_res+folder_res)
path_ortho="/Users/elinlarsen/Google Drive/CDSwordSeg_Pipeline/recipes/childes/data/Brent/ortholines.txt"
path_gold="/Users/elinlarsen/Google Drive/CDSwordSeg_Pipeline/recipes/childes/data/Brent/gold.txt"

# *******  parameters *****
ALGOS=['TPs','DiBs','PUDDLE','AGu', 'gold']
SUB=['full_corpus']
SUBS=['sub0','sub1','sub2','sub3','sub4','sub5','sub6','sub7','sub8','sub9']
lexical_classes=['nouns','function_words', 'adjectives', 'verbs', 'other']
unit="syllable"
CDI_file="CDI_data/PropUnderstandCDI.csv" #CDI_file="CDI_data/PropProduceCDI.csv"
freq_file="/freq-words.txt"
nb_i_file="CDI_data/CDI_NbInfantByAge"
ages=range(8,19) #ages=range(16, 31)
df_gold=analyze.freq_token_in_corpus(path_ortho)

##get lexical classes of the corpus
df_lc_corpus=get_lexical_class.part_of_seech_tagger(path_file=path_ortho)
df_gold_lc=pd.merge(df_gold, df_lc_corpus, on="Type", how="inner")
df_gold_lc.columns=['Type', 'Freqgold', 'abbrev_tags', 'lexical_class']

# **** dictionnary
d=translate.build_phono_to_ortho(path_gold,path_ortho)
dic_corpus= translate.build_phono_to_ortho_representative(d)[0]

#algo results
df_tp=read.create_df_freq_by_algo_all_sub(path_res, ['full_corpus'], 'tps','syllable', freq_file="/freq-words.txt")
df_CDI=read.read_CDI_data_by_age(CDI_file, age=8, save_file=False) #age does not matter here

# ******* Model selection : Linear or Logistics *******

# LINEAR
results_full_corpus_ph=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS, "phoneme", ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)
len(results_full_corpus_ph['df_data'])

R2_ALGOs_CDI_phoneme=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS, "phoneme", ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)['R2']
R2_ALGOs_CDI_syllable=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS, "syllable", ages, CDI_file, freq_file,evaluation='true_positive', miss_inc=False)['R2']

std_err_ALGOs_CDI_phoneme=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS, "phoneme", ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)['std_err']
std_err_ALGOs_CDI_syllable=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS, "syllable", ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)['std_err']

R2_lin=pd.concat([R2_ALGOs_CDI_syllable,R2_ALGOs_CDI_phoneme])
R2_lin.set_index([['TPs', 'DiBS', 'PUDDLE', 'AGu', 'Gold', 'TPs', 'DiBS', 'PUDDLE', 'AGu', 'Gold']], drop=True, inplace=True, verify_integrity=False)

std_err_lin=pd.concat([std_err_ALGOs_CDI_syllable,std_err_ALGOs_CDI_phoneme])
std_err_lin.set_index([['TPs', 'DiBS', 'PUDDLE', 'AGu', 'Gold', 'TPs', 'DiBS', 'PUDDLE', 'AGu', 'Gold']], drop=True, inplace=True, verify_integrity=False)

# correlation on phonologized forms :
model.linear_algo_CDI_phono(path_gold,path_res, "full_corpus", ALGOS, 'syllable',ages, CDI_file,"/freq-top.txt", "true_positive", False)

# test evaluation on recall
R2_recall_ph=R2_ALGOs_CDI_phoneme=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS_, "phoneme", ages, CDI_file, freq_file, evaluation='recall')
R2_recall_syl=R2_ALGOs_CDI_phoneme=model.linear_algo_CDI(path_ortho,path_res,["full_corpus"], ALGOS_, "syllable", ages, CDI_file, freq_file, evaluation='recall')

# LOGISTIC
R2_log_phoneme=model.logistic_nb_infant_algo_CDI(path_ortho,path_res, SUB, ALGOS,'phoneme', ages, CDI_file,nb_i_file ,freq_file, Test_size=0.20)['R2']
R2_log_syllable=model.logistic_nb_infant_algo_CDI(path_ortho,path_res, SUB, ALGOS,'syllable', ages,CDI_file,nb_i_file ,freq_file, Test_size=0.20) ['R2']

R2_log=pd.concat([R2_log_syllable, R2_log_phoneme])

std_err_log_ph=model.logistic_nb_infant_algo_CDI(path_ortho,path_res, SUB, ALGOS,'phoneme', ages,  CDI_file,nb_i_file ,freq_file, Test_size=0.20) ['std_err']
std_err_log_syl=model.logistic_nb_infant_algo_CDI(path_ortho,path_res, SUB, ALGOS,'syllable', ages,CDI_file,nb_i_file ,freq_file, Test_size=0.20)['std_err']

std_err_log=pd.concat([std_err_log_syl, std_err_log_ph])

# for one subcorpus (length divided by 10) => looking at the effect size of the corpus
R2_ALGOs_CDI_sub0=model.linear_algo_CDI(path_ortho,path_res,["sub0"], ALGOS, "", ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)['R2']
std_err_ALGOs_CDI_sub0=model.linear_algo_CDI(path_ortho,path_res,["sub0"], ALGOS, "",ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)['std_err']

results_sub0=model.linear_algo_CDI(path_ortho,path_res,["sub0"], ALGOS, "",ages, CDI_file, freq_file, evaluation='true_positive', miss_inc=False)

len(results_sub0['df_data'])

# *******  Visualisation *******
### scatter plot

visualize.plot_algos_CDI_by_age(path_ortho,path_res, False , ALGOS +['gold'],ages, CDI_file,freq_file,name_vis= "CDIScore_AlgoScore_sans_fit")

visualize.plot_algos_CDI_by_age(path_ortho,path_res, ["full_corpus"], ALGOS,[8,18], CDI_file,freq_file, name_vis="plot_all_algos")

### R2 for differents ages

# linear
#production for different ages
visualize.plot_bar_R2_algos_unit_by_age(R2_lin, std_err_lin, ages,ALGOS, ['syllable', 'phoneme'],name_vis="Correlation with infant lexicon production for 16-30-month-old")

visualize.plot_bar_R2_algos_unit_by_age(R2_lin[[13,'unit']], std_err_lin[[13,'unit']], 13,ALGOS, ['syllable', 'phoneme'],name_vis="Age 13 months")

#logistic
#production for different ages
visualize.plot_bar_R2_algos_unit_by_age(R2_log, std_err_log, ages,ALGOS, ['syllable', 'phoneme'],name_vis="Logistic Reg with infant lexicon production for 16-30-month-old ")

visualize.plot_bar_R2_algos_unit_by_age(R2_log, std_err_log,ages,ALGOS, ['syllable', 'phoneme'], name_vis="LOG R2 ALGOs versus CDI with phoneme representation")

# miss included
R2_lin_missed=pd.concat([lin_missed_syll, lin_missed_ph])
std_err_missed=pd.concat([lin_missed_syll_err, lin_missed_ph_err])
visualize.plot_bar_R2_algos_unit_by_age(R2_lin_missed, std_err_missed,ages, ALGOS, ['syllable', 'phoneme'],name_vis="R2 ALGOs versus CDI - syllable and phoneme - missed word by algo included")

### Lexical classes
visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ['TPs'], unit,[13], lexical_classes, False, CDI_file, freq_file, name_vis="lexical_classes_TPs_13")
visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ['AGu'], unit,[13], lexical_classes, False, CDI_file, freq_file, name_vis="lexical_classes_AGu_13")
visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ['puddle_py'],unit, [13], lexical_classes, False, CDI_file, freq_file, name_vis="lexical_classes_puddle_py_13")
visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ['dibs'], unit,[13], lexical_classes, False, CDI_file, freq_file, name_vis="lexical_classes_dibs_13")
visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ['gold'], unit,[18], lexical_classes, False, CDI_file, freq_file, name_vis="lexical_classes_gold_18")

# lc gold
lexical_classes=['nouns','function_words', 'adjectives', 'verbs', 'other']
unit="syllable"
R2_lc_13_syl=visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ALGOS,unit, [13], lexical_classes, False, CDI_file,
                                  freq_file, name_vis="lexical_classes_algos_13")

R2_lc_13_ph=visualize.plot_by_lexical_classes(path_res, ['full_corpus'], ALGOS,'phoneme', [13], lexical_classes, False, CDI_file,
                                  freq_file, name_vis="lexical_classes_algos_13_phoneme")
R2_lc_13=pd.concat([R2_lc_13_ph,R2_lc_13_syl])

#### algo vers gold
#when using words in CDI it should be "lexical_classes"
res_algo_gold=visualize.plot_algo_gold_lc(path_res,['full_corpus'], ['tps','dibs','puddle_py','AGu'],df_gold_lc, 'syllable',CDI_file, "lexical_class", lexical_classes, freq_file, name_vis="plot_algos_vs_gold_log_scale")

results_gold_TP=visualize.plot_algo_gold_lc(path_res,['full_corpus'], ['tps'],df_gold_lc, 'syllable',"", "lexical_class", lexical_classes, freq_file, name_vis="plot_TP_vs_gold_log_scale")

sup10=df_gold_lc.loc[lambda df_gold_lc: df_gold_lc.Freqgold > 10, :]
inf100=df_gold_lc.loc[lambda df_gold_lc:  df_gold_lc.Freqgold < 100, :]

pd.merge(sup10, inf100)

# no lexical class filter
res_algo_vs_gold=visualize.plot_algo_gold(path_res, ['full_corpus'], ['tps','dibs','puddle_py','AGu'], df_gold, 'syllable', CDI_file, freq_file="/freq-words.txt", name_vis="algo vs gold ")

# ******** test the effect of missed word by algo  *******
lin_missed=model.linear_algo_CDI(path_ortho,path_res, ['full_corpus'], ALGOS,'syllable',[13], CDI_file ,"/freq-words.txt","true_positive",True)

# *******  EFFECT OF ... *******
### Lexical classes
R2_lexical_classes_syl=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'syllable', [13], df_CDI_lexical_classes, "lexical_classes", CDI_file, freq_file)['R2']
R2_lexical_classes_ph=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'phoneme', [13], df_CDI_lexical_classes, "lexical_classes", CDI_file, freq_file)['R2']
#pd.concat([R2_lexical_classes_syl,R2_lexical_classes_ph]).round(3).to_csv("correlation_CDI_algos/13_month_old/R2_lexical_classes_13_mo.txt", sep='\t', header=True)
R2_lexical_classes=pd.concat([R2_lexical_classes_syl,R2_lexical_classes_ph]).round(3)

err_lexical_classes_syl=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'syllable', [13], df_CDI_lexical_classes, "lexical_classes", CDI_file, freq_file)['std_err']
err_lexical_classes_ph=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'phoneme', [13], df_CDI_lexical_classes, "lexical_classes", CDI_file, freq_file)['std_err']
err_lexical_classes=pd.concat([err_lexical_classes_syl,err_lexical_classes_ph]).round(3)

visualize.plot_R2_by_parameter_for_one_age(R2_lexical_classes, err_lexical_classes, ALGOS, ['syllable', 'phoneme'],  name_vis="R2_lexical_classes")

### Type length
results_length_13_syl=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'syllable', [13], length_type, "num_syllables", CDI_file, freq_file)
results_length_13_ph=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'phoneme', [13], length_type, "num_syllables", CDI_file, freq_file)
R2_length_in_syl_13=pd.concat([results_length_13_syl['R2'],results_length_13_ph['R2']])
#R2_length_in_syl_13.round(3).to_csv("R2_for_length_type_in_syl_13_mo.txt", sep='\t', header=True)
err_length_in_syl=pd.concat([results_length_13_syl['std_err'],results_length_13_ph['std_err']])

res_length_13_syl=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'syllable', [13], length_type, "num_phonemes", CDI_file, freq_file)
res_length_13_ph=model.R2_by_parameter(path_res, ['full_corpus'], ALGOS,'phoneme', [13], length_type, "num_phonemes", CDI_file, freq_file)
R2_length_in_ph_13=pd.concat([res_length_13_syl['R2'],res_length_13_ph['R2']])
err_length_in_ph_13=pd.concat([res_length_13_syl['std_err'],res_length_13_ph['std_err']])
#R2_length_in_ph_13.round(3).to_csv("R2_for_length_type_in_ph_13_mo.txt", sep='\t', header=True)

visualize.plot_R2_by_parameter_for_one_age(R2_length_in_ph_13, err_length_in_ph_13, ['TPs', 'DiBS', 'PUDDLE', 'AGu', 'Gold'], ['syllable', 'phoneme'], name_vis="R2_length_in_num_of_phonemes")


#Crie uma lista com os preços dos jogos que você mais gosta.
#a) Imprima o preço mais caro;
#b) Imprima o preço mais barato.

n=int(input('informe quantos são os jogos preferidos: '))
preços_jogos=[ ]

for _ in range(n):
    preços_jogos.append(input('informe o preço de cada jogo: '))

print('o maior preço é: ', max(preços_jogos))
print('o menor preço é: ',min(preços_jogos))

Aula4 - Vetores e estrutura de repetição/jogos.py


import string

class User:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def __eq__(self, other):
        return self.email == other.email if isinstance(other, User) else False

class UserForm(User):
    def validate_name(self):
        if not self.name:
            raise ValueError
        # a = "qwertyuiopasdfghjklzxcvbnm_"
        # b = "qwertyuiopasdfghjklzxcvbnm0123456789_"
        # c = "qwertyuiopasdfghjklzxcvbnm0123456789_."
        if self.name[0] not in (string.ascii_lowercase + "_"):
            raise ValueError
        elif self.name[-1] not in (string.ascii_lowercase + string.digits + "_"):
            raise ValueError
        for x in self.name[1:-1]:
            if x not in (string.ascii_lowercase + string.digits + "_."):
                raise ValueError

    def validate_email(self):
        if not self.email:
            raise ValueError
        if "@" not in self.email:
            raise ValueError
        # a = "qwertyuiopasdfghjklzxcvbnm_"
        # b = "qwertyuiopasdfghjklzxcvbnm0123456789_"
        # c = "qwertyuiopasdfghjklzxcvbnm0123456789_."
        # d = "qwertyuiopasdfghjklzxcvbnm0123456789_+."
        listemail = self.email.split("@")
        if len(listemail) != 2:
            raise ValueError
        login = listemail[0]
        domain = listemail[1]
        if not login:
            raise ValueError
        if not domain:
            raise ValueError
        if login[0] not in (string.ascii_lowercase + "_"):
            raise ValueError
        if login[-1] not in (string.ascii_lowercase + string.digits + "_"):
            raise ValueError
        for x in login[1:-1]:
            if x not in (string.ascii_lowercase + string.digits + "_.+"):
                raise ValueError
        if domain[0] not in (string.ascii_lowercase + "_"):
            raise ValueError
        if domain[-1] not in (string.ascii_lowercase + string.digits + "_"):
            raise ValueError
        for x in domain[1:-1]:
            if x not in (string.ascii_lowercase + string.digits + "_."):
                raise ValueError


from django.urls import path

from . import views

urlpatterns = [
	#Leave as empty string for base url

    path('about/', views.about, name="about"),
    path('bill/<str:pk>/', views.bill, name="bill"),
    path('bill/', views.bill_list, name="bill_list"),

	path('blog_rightsidebar/', views.blog_rightsidebar, name="blog_rightsidebar"),
    path('blog/', views.blog, name="blog"),
    path('brand_product/', views.brand_product, name="brand_product"),
    path('checkout/', views.checkout, name="checkout"),
    path('contact/', views.contact, name="contact"),
    path('faq/', views.faq, name="faq"),
    path('index/', views.home_default, name="home_default"),
    path('home_search/', views.home_search, name="home_search"),
    path('home_slider/', views.home_slider, name="home_slider"),
    path('home_slider2/', views.home_slider2, name="home_slider2"),
    path('index_icon/', views.index_icon, name="index_icon"),
	path('index/', views.index, name="index"),
    path('order_details/', views.order_details, name="order_details"),
    path('product_detail/', views.product_detail, name="product_detail"),
    path('', views.product_fullwidth, name="product_fullwidth"),
    path('product_leftsidebar/', views.product_leftsidebar, name="product_leftsidebar"),
	path('product_list/', views.product_list, name="product_list"),
    path('profile/', views.profile, name="profile"),
    path('single/', views.single, name="single"),
    path('track_order_single/', views.track_order_single, name="track_order_single"),
	path('track_order/', views.track_order, name="track_order"),
    path('user_dashbord/', views.user_dashbord, name="user_dashbord"),
    path('wishlist/', views.wishlist, name="wishlist"),

    path('update_item/',views.updateItem,name="update_item"),

    path('process_order/',views.processOrder,name="process_order")

]


def collatz(number):
    if (number % 2 == 0):
        print(number // 2)
        return number // 2
    elif (number % 2 == 1):
        print(number * 3 + 1)
        return number * 3 + 1

if __name__ == "__main__":
    try:
        num = int(input("Enter a number: "))
        output = collatz(num)
        while output != 1:
            output = collatz(output)
    except ValueError:
        print("Please enter a valid number")


from flask_restful import reqparse, Resource, inputs
from models.CanaryCheck import CanaryCheck
from app import db
import json

parser = reqparse.RequestParser()
parser.add_argument('vpn', type=str, help='Exception error')
parser.add_argument('status', type=inputs.boolean, help='Missing status')

class CanaryCheck_Resource(Resource):

    def get(self):
        data = CanaryCheck.query.all()

        ret =""
        for d in data:
            ret += "%d - %s - %s " % (d.id, d.vpn, d.status)

        return ret
    def post(self):
        args = parser.parse_args(strict=True)
        m = CanaryCheck(**args)
        db.session.add(m)
        db.session.commit()

        return args


import sys

sys.stderr.write("this is stdrtext\n")
sys.stdout.write("this is stdrouttext\n")
print(sys.argv)

def main(argv):
    print(sys.argv[1])# usisng sys we can pass as many
                      # arguments we want also from command line
main(sys.argv[1])


# encoding: utf-8

"""
@author: 江哲

@file: myDataLoader.py

@time: 2018/4/25 20:29

@desc:

"""
import struct
import numpy
import torch
from PIL import Image

class DataLoader(object):
    def __init__(self):
        self.url = {
            'train-images-set': 'train-images.idx3-ubyte',
            'train-labels-set': 'train-labels.idx1-ubyte',
            'test-images-set' : 't10k-images.idx3-ubyte',
            'test-labels-set' : 't10k-labels.idx1-ubyte',
        }
        return

    def loadImages(self, url, count):
        f = open(url, 'rb')
        f.read(16)
        images = numpy.frombuffer(f.read(count * 784), dtype = numpy.ubyte).reshape(count, 784)

        images = numpy.where(images > 0, 1, 0)  # 将灰度值转变为二值

        f.close()
        return images

    def loadLabels(self, url, n):
        f = open(url, 'rb')
        f.read(8)
        labels = numpy.frombuffer(f.read(n), dtype = numpy.ubyte)
        f.close()
        print(labels)
        return labels


# Sample input console program
age = int(input('Enter your age in years:'))
try:
    if age<=0:
        print('your age must be possitive!')
    else:
        max_heart_rate = 206.9-(0.67*age)
        targetNum = 0.65 * max_heart_rate
        print('Your target fat-burning heart rate is' , targetNum)
except Exception as e:
    print('Invalid values')


import logging

def make_response(status_code, body, header=None):
    return {
        "statusCode": status_code,
        "headers": header,
        "body": body
    }

def configure_log(logger):
    logger.setLevel(logging.INFO)

    # create a file handler
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    # create a logging format
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(handler)

def validate_request(required_keys, request_body):
    for key in required_keys:
        if key not in request_body:
            return False
    return True

def validator_usage():
    pass


print("Problem 2 - Even Fibonacci numbers");
n1 = 1;
n2 = 2;

# Initial even sum
nSum = 0 + 2;

go = True;
while(go):
    temp = n2;
    n2 = n2 + n1;
    n1 = temp;
    if(n2 > 4*10**6):
        go = False;
    else:
        if(n2%2 == 0):
            nSum = nSum + n2;
print("Ans:" + str(nSum));


i=int(input())
d=i%10
h=i%100
if d==1 and h!=11:
    s=""
elif 1<d<5 and not 11<h<15:
    s="а"
else:
    s="ов"
print(i," программист"+s)

Программирование на Python/чужой робот.py

чужой робот.py


#######  字典的交并差！！！
'''
集合有交并差运算。 对应于& | -符号
dict是键值对集合,试了没有|-&运算，用items替代
keys是一个key集合。有|-&运算
'''

a = {
    'x' : 1,
    'y' : 2,
    'z' : 3
}

b = {
    'w' : 10,
    'x' : 11,
    'y' : 2
}
#print(a |-& b)
print(a.keys() | b.keys())  # {'y', 'w', 'z', 'x'}
print(type(a.keys()))  #<class 'dict_keys'>
print(type(a.keys() | b.keys() )) # <class 'set'>
print(type(a.items()))  #<class 'dict_items'>

print(a.items() | b.items())  #{('z', 3), ('y', 2), ('w', 10), ('x', 1), ('x', 11)}
print(type(a.items()| b.items()))  #<class 'set'>

###########常用写法
c = {key:a[key] for key in a.keys() - {'z', 'w'}}
# c is {'x': 1, 'y': 2}
# key:a[key]是外面符号{}的元素，key是遍历变量


#!/usr/bin/env python

import roslib
import sys
import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge , CvBridgeError
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image

class getdepth (object):

    def __init__(self) :
        self.bridge_object = CvBridge()
        self.image_sub = rospy.Subscriber("/drone/hires/image_raw" , Image , self.camera_callback)

    def camera_callback (self , data)    :
        try :
            cv_image = self.bridge_object.imgmsg_to_cv2 (data , desired_encoding="bgr8")
        except CvBridgeError as e :
            print (e)

        cv2.imshow ("Image window" , cv_image)
        cv2.waitkey (1)

def main():
    depth = getdepth ()
    rospy.init_node   ("depth" , anonymous=True)
    try :
        rospy.spin()
    except KeyboardInterrupt :
        print ("shutting down")
    cv2.destroyAllWindows()

if __name__== '__main__' :
    main ()


"""
Broker Application Implementation
This will connect to the Core server and retrieve configuration.
"""
import logging
import threading
import json
import websocket

from time import sleep

from Broker.broker import BrokerServer
from Broker.settings import ApplicationSettings

logger = logging.getLogger("Application")

class Application:

    def __init__(self, settings: ApplicationSettings):
        self.running = False
        self.keep_running = True

        self.broker = None
        self.broker_settings = None
        self.broker_thread = None

        self.incoming_actions = []
        self.server_updates = []

        self.ws_url = settings.websocket_url
        self.uuid = settings.uuid
        self.token = settings.token

        websocket.enableTrace(True)
        self.ws = websocket.WebSocketApp(
            self.ws_url,
            header=[
                'AUTH-UUID: {}'.format(self.uuid),
                'AUTH-TOKEN: {}'.format(self.token),
                'AUTH-TYPE: broker',
            ],
            on_close=self._on_close,
            on_message=self._on_message,
            on_error=self._on_error,
        )
        self.ws.on_open = self._on_open

        logger.info("Application Init Completed")

    @property
    def callbacks(self):
        return {
            'update_server_list': self.update_server_list
        }

    def _on_open(self):
        self.broker_thread = threading.Thread(name='BrokerTCP', target=self._broker_thread)
        self.broker_thread.start()

    def _on_message(self, event):
        data = json.loads(event)
        logger.debug("Message Received {}".format(data))
        if data.get('type') and hasattr(self, "recv_{}".format(data['type'])):
            getattr(self, "recv_{}".format(data['type']))(data)
        elif data.get('type'):
            logger.error("Invalid Action {}".format(data['type']))
        else:
            logger.error("Unparsed Message")

    def _on_error(self, event):
        ...

    def _on_close(self):
        ...

    def start(self, *args, **kwargs):
        logger.info("Running Websocket Main Loop")
        self.ws.run_forever(*args, **kwargs)
        logger.info("Stop Signal Received, or WS Run End Unexpected")
        self.stop()

    def stop(self):
        self.keep_running = False
        self.broker.stop()

    def _broker_thread(self):
        logger.info("Broker Thread Started")
        logger.info("Waiting Settings from Websocket Server")

        while not self.broker_settings and self.keep_running:
            sleep(0.5)

        if not self.keep_running:
            logger.error("Cannot Start Broker because Keep running was set to False")
            return None

        self.broker = BrokerServer(self.broker_settings, self.callbacks)

        self.broker.listen()

    def recv_update_info(self, event):
        if event.get('broker'):
            self.broker_settings = event['broker']
        else:
            logger.error("Update Info Not Related")

    def recv_server_list(self, event):
        self.server_updates.append(event['servers'])

    def update_server_list(self):
        self.ws.send(data=json.dumps({
            'type': 'update_server_list'
        }))

        logging.info("Waiting Updated Server List")

        while not self.server_updates and self.keep_running:
            sleep(0.5)

        if not self.keep_running:
            raise BrokenPipeError("Application Stop Before Sending Updated Server List")

        return self.server_updates.pop()


#!/usr/bin/env python

actor = 'Dan Ackroyd'

def spam():
    actor = 'William Shatner'
    x = 100
    print("x is", x)
    print("local actor is", actor)
    print("locals:", locals())
    print(globals()['actor'])

g = globals()
print("global actor is", g['actor'])

spam()

g['montreal_food'] = 'smoked meat'

print(montreal_food)

airports = {
    'EWR': 'Newark',
    'MCI': 'Kansas City',
    'SFO': 'San Francisco',
    'RDU': 'Raleigh-Durham',
    'SJC': 'San Jose',
    'MCO': 'Orlando',
    'ABQ': 'Albuquerque',
    'OAK': 'Oakland',
    'SAC': 'Sacramento',
    'IAD': 'Dulles',
}

for abbr, name in airports.items():
    g[abbr] = name

print(IAD, OAK, RDU)

foo = ['a', 'b', 'c']

g['foo'].append('mango')
print(foo)

f = g['spam']
f()
g['spam']()


class Solution(object):
    def numberOfLines(self, widths, S):
        """
        :type widths: List[int]
        :type S: str
        :rtype: List[int]
        """
        cur = 0
        lines = list()
        for ch in S:
            cur_width = widths[ord(ch) - ord('a')]
            if cur + cur_width > 100:
                lines.append(cur)
                cur = cur_width
            else:
                cur += cur_width
        if cur > 0:
            lines.append(cur)
        return len(lines), lines[-1]


import requests

from .settings import NOAA_URL, WEATHER_URL, ACCUWEATHER_URL

class BaseService(object):
    NAME = None

    def __init__(self):
        print('Base class')

    def get_temperature(self, lat, long):
        raise NotImplementedError()

    @classmethod
    def check_name(cls, name):
        return name == cls.NAME

class NoaaService(BaseService):
    NAME = 'noaa'

    def get_temperature(self, lat, lon):
        print('Noaa Called')
        r = requests.get(NOAA_URL.format(lat, lon))

        if r.status_code == 200:
            json_str = r.json()
            print("Status: {} and reason: {}".format(r.status_code, json_str))
            r.close()
            res = dict()
            res['fahrenheit'] = dict()
            res['fahrenheit']['current_temperature'] = float(json_str['today']['current']['fahrenheit'])
            res['celsius'] = dict()
            res['celsius']['current_temperature'] = float(json_str['today']['current']['celsius'])
            return res
        else:
            return None

class WeatherdotcomService(BaseService):
    NAME = 'weatherdotcom'

    def get_temperature(self, lat, lon):
        print('Weatherdotcom Called')
        body = '{"lat":%f,"lon":%f}' % (lat, lon)
        r = requests.post(WEATHER_URL, json=body)

        if r.status_code == 200:
            json_str = r.json()
            print("Status: {} and reason: {}".format(r.status_code, json_str))
            r.close()
            res = dict()
            farenheit = float(json_str['query']['results']['channel']['condition']['temp'])
            res['fahrenheit'] = dict()
            res['fahrenheit']['current_temperature'] = farenheit

            celcius = (farenheit - 32.0) * 5.0 / 9.0
            res['celsius'] = dict()
            res['celsius']['current_temperature'] = celcius
            return res
        else:
            return None

class AcuweatherService(BaseService):
    NAME = 'accuweather'

    def get_temperature(self, lat, lon):
        print('Accuweather Called')
        r = requests.get(ACCUWEATHER_URL.format(lat, lon))

        if r.status_code == 200:
            json_str = r.json()
            print("Status: {} and reason: {}".format(r.status_code, json_str))
            r.close()
            res = dict()
            res['fahrenheit'] = dict()
            res['fahrenheit']['current_temperature'] = float(
                json_str['simpleforecast']['forecastday'][0]['current']['fahrenheit'])
            res['celsius'] = dict()
            res['celsius']['current_temperature'] = float(
                json_str['simpleforecast']['forecastday'][0]['current']['celsius'])
            return res
        else:
            return None

class WeatherServiceManager(object):
    source_systems = {
        'noaa': NoaaService,
        'weatherdotcom': WeatherdotcomService,
        'accuweather': AcuweatherService
    }

    def __init__(self, source):
        self.engine = self.choose_engine(source)

    def check_source(self):
        return self.engine

    def choose_engine(self, source):
        if source in self.source_systems:
            return self.source_systems[source]()
        else:
            return None

    def get_temp(self, lat, long):
        return self.engine.get_temperature(lat, long)


import os
import gpflow
from sklearn import preprocessing
import numpy as np
import gzip
import pickle
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1'  # 这是默认的显示等级，显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'  # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'  # 只显示 Error

# read data
with gzip.open('./data/predict.pkl.gz') as fp:
    predict = np.array(pickle.load(fp))

with gzip.open('./data/start.pkl.gz') as fp:
    start = np.array(pickle.load(fp))

# clean data
start[start == -1.0E20] = 'nan'
start = start[:, ~np.all(np.isnan(start), axis=0)]

predict[predict == -1.0E20] = 'nan'
predict = predict[:, ~np.all(np.isnan(predict), axis=0)]

# preprocess
start = preprocessing.scale(start)
predict = preprocessing.scale(predict)
predict = predict-start

M = 3
start = start[0:10, 0:M]
predict = predict[0:10, 0:M]

m = gpflow.saver.Saver().load('./model/gp.mdl')

# construct x
X_augmented = np.vstack(
    (np.hstack((start[:, 0:1], np.zeros_like(start[:, 0:1]))), np.hstack((start[:, 1:2], np.ones_like(start[:, 1:2]))))
)
for i in range(M-2):
    X_augmented = np.vstack(
        (X_augmented, np.hstack((start[:, i+2:i+3], (i+2)*np.ones_like(start[:, i+2:i+3]))))
    )
# construct y
Y_augmented = np.vstack(
    (np.hstack((predict[:, 0:1], np.zeros_like(start[:, 0:1]))), np.hstack((predict[:, 1:2], np.ones_like(start[:, 1:2]))))
)
for i in range(M-2):
    Y_augmented = np.vstack(
        (Y_augmented, np.hstack((predict[:, i+2:i+3], (i+2)*np.ones_like(start[:, i+2:i+3]))))
    )

print(X_augmented)
pY, pYv = m.predict_f(X_augmented)
print(pY)
# print(pYv)
print(Y_augmented)
# print(pY - Y_augmented[:, 0])


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import gym
import matplotlib.pyplot as plt
import copy
import collections
import random
# it is actually Dueling DDQN
# hyper-parameters
BATCH_SIZE = 128
LR = 0.01
GAMMA = 0.9
MEMORY_CAPACITY = 4000
Q_NETWORK_ITERATION = 100

# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cuda')
env = gym.make("CartPole-v1")
env = env.unwrapped
NUM_ACTIONS = env.action_space.n
NUM_STATES = env.observation_space.shape[0]
ENV_A_SHAPE = 0 if isinstance(env.action_space.sample(), int) else env.action_space.sample.shape

def setup_seed(seed):
    env.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True
    # cudnn.benchmark = False
    # cudnn.enabled = False

setup_seed(1234)

class DDQN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(NUM_STATES, 128)
        self.fc2 = nn.Linear(128, 128)

        self.V = nn.Linear(128, 1)
        self.A = nn.Linear(128, NUM_ACTIONS)

    def forward(self,x):
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        x = F.relu(x)
        Value = self.V(x)
        Advantage = self.A(x)

        Q_value = Value + (Advantage - Advantage.mean(dim=1, keepdim=True))
        return Q_value

class Agent(nn.Module):
    """docstring for DQN"""

    def __init__(self):
        super().__init__()
        self.eval_net, self.target_net = DDQN(), DDQN()

        self.learn_step_counter = 0
        self.memory_counter = 0
        self.memory = collections.deque(maxlen=MEMORY_CAPACITY)
        # why the NUM_STATE*2 +2
        # When we store the memory, we put the state, action, reward and next_state in the memory
        # here reward and action is a number, state is a ndarray
        self.optimizer = torch.optim.Adam(self.eval_net.parameters(), lr=LR)
        self.loss_func = nn.MSELoss()

    def choose_action(self, state, epsilon):
        state = torch.unsqueeze(torch.FloatTensor(state), 0).to(device)  # get a 1D array
        if np.random.rand() > epsilon:  # greedy policy
            action_value = self.eval_net.forward(state)
            action = torch.max(action_value, 1)[1].cpu().data.numpy()
            action = action[0] if ENV_A_SHAPE == 0 else action.reshape(ENV_A_SHAPE)
        else:  # random policy
            action = np.random.randint(0, NUM_ACTIONS)
            action = action if ENV_A_SHAPE == 0 else action.reshape(ENV_A_SHAPE)
        return action

    def store_transition(self, state, action, reward, next_state):
        transition = np.hstack((state, [action, reward], next_state))
        index = self.memory_counter % MEMORY_CAPACITY
        self.memory.append(transition)
        self.memory_counter += 1

    def learn(self):

        # update the parameters
        if self.learn_step_counter % Q_NETWORK_ITERATION == 0:
            self.target_net.load_state_dict(self.eval_net.state_dict())
        self.learn_step_counter += 1

        # sample batch from memory

        batch_memory = torch.Tensor(random.sample(self.memory, BATCH_SIZE)).to(device)

        batch_state = batch_memory[:, :NUM_STATES]
        batch_action = batch_memory[:, NUM_STATES:NUM_STATES + 1].long()
        batch_reward = batch_memory[:, NUM_STATES + 1:NUM_STATES + 2]
        batch_next_state = batch_memory[:, -NUM_STATES:]

        # q_eval
        q_eval = self.eval_net(batch_state).gather(1, batch_action)
        target_action = self.eval_net(batch_state).argmax(dim=1).unsqueeze(1).long()
        # 使用eval_net找到最大Q值对应的动作，再用此动作带入target_Qnet计算target_Q
        q_next = self.target_net(batch_next_state).gather(1, target_action).detach()
        # q_next:[batch_sz, 2 (action dim)]
        q_target = batch_reward + GAMMA * q_next
        loss = self.loss_func(q_eval, q_target)

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

def reward_func(env, x, x_dot, theta, theta_dot):
    r1 = (env.x_threshold - abs(x)) / env.x_threshold - 0.5
    r2 = (env.theta_threshold_radians - abs(theta)) / env.theta_threshold_radians - 0.5
    reward = r1 + r2
    return reward

def main():
    dqn = Agent().to(device)

    time_list = []
    episodes = 300
    max_time = 500
    print("Collecting Experience....")
    reward_list = []
    plt.ion()
    fig, ax = plt.subplots()
    for i in range(episodes):
        state = env.reset()
        epsilon = max(0.01, 0.1 - (0.1 - 0.01) * (i / 200))
        ep_reward = 0
        play_times = 0
        for t in range(max_time):
            env.render()
            action = dqn.choose_action(state, epsilon)
            next_state, reward, done, info = env.step(action)
            x, x_dot, theta, theta_dot = next_state
            reward = reward_func(env, x, x_dot, theta, theta_dot)

            dqn.store_transition(state, action, reward, next_state)
            ep_reward += reward
            play_times += 1

            if dqn.memory_counter >= 2000:
                dqn.learn()
                if done or t == max_time - 1:
                    print("episode: {} , epsilon: {} , the episode reward is {}, it has played {} times".\
                          format(i, round(epsilon, 3), round(ep_reward, 3), play_times))
            if done or t == max_time - 1:
                break
            state = next_state

        time_list.append(play_times)
        r = copy.copy(ep_reward)
        reward_list.append(r)
        # ax.set_xlim(0, 300)
        # ax.cla()
        ax.plot(reward_list, 'g-', label='total_loss')
        plt.pause(0.001)

    plt.figure()
    plt.plot(time_list)
    plt.legend(['times it insists'])

if __name__ == '__main__':
    main()


import cv2
import numpy as np
from matplotlib import pyplot as plt
from pathlib import Path
import os
from PIL import Image
import natsort

def automatic_roi(img):

	M,N = img.shape
	print(M,N)
	wmean = round(0.1 *N)
	wmax = round(0.25*N)

	array = np.array(img)
	w = array
	#to successfully print the numpy aray
	np.set_printoptions(threshold = np.inf)

	#print(array)

	prevend = 0
	for i in range(0,N-1):

		start = 0
		end = M
		for j in range(0,M-1):
			if w[j][i] == 0 and w[j+1][i] == 255:
				start = j
				break

		for j in range(start+1,N-1):
			if w[j][i] == 255 and w[j+1][i] == 0:
				end = j
				break

		wj = end - start

		if wj < wmax:
			for r in range(start,end):
				w[r][i] = 0
			wmean = wj
			# wmax = min(end-1, round(0.3*N))
			prevend = max(end, wmax)
			print(str(prevend)+"__")

		if wj > wmax:
			print(prevend)
			for r in range(0, prevend):
				w[r][i] = 0

		# print(wj,wmean,wmax,prevend, start, end)
	#print(w)

	print(np.array_equal(array,w))

	img = Image.fromarray(w)

	roi = w#[100:380,150:380]

	return roi

input_path = Path("C:/Users/sahalab/Desktop/MACHINE LEARNING IN IMAGE PROCESSING/PythonCorey/pngs/eq2mask2roi/mask/")
output_path = Path("C:/Users/sahalab/Desktop/MACHINE LEARNING IN IMAGE PROCESSING/PythonCorey/pngs/eq2mask2roi/roi/")
image_folder = os.listdir(input_path)
image_folder = natsort.natsorted(image_folder,reverse = False)
print(image_folder)

for index,image in enumerate(image_folder,0):

	img = cv2.imread((os.path.join(input_path,image_folder[index])),0)
	ROI = automatic_roi(img)
	#roiImg = Image.fromarray(ROI)
	cv2.imwrite(os.path.join(output_path,"roi_{}.png".format(image)),ROI)


from __future__ import absolute_import

from unittest import TestCase as BaseTestCase
import os
import sys

class TestCase(BaseTestCase):

    """Extensions to :class:`python:unittest.TestCase` which understand entities,
    and backport many methods from Python2.7.

    """

    def failIfLocal(self, msg=None):
        if os.environ.get('TRAVIS', 'false') != 'true':
            self.fail(msg or 'not Travis CI')

    def assertSameEntity(self, a, b, msg=None):
        """Assert that the two given entities refer to the same object.

        E.g.: Both are dicts and their types and ids match.

        """
        errors = []

        for loc, x in (('1st', a), ('2nd', b)):
            if not isinstance(x, dict):
                errors.append('%s is a %r, not dict: %r' % (loc, type(x), x))
                continue
            if 'type' not in x:
                errors.append('%s has no type: %r' % (loc, x))
            if 'id' not in x:
                errors.append('%s has no id: %r' % (loc, x))

        if not errors:
            if a['type'] != b['type']:
                errors.append('types do not match; %r != %r' % (a['type'], b['type']))
            elif a['id'] != b['id']:
                errors.append('ids do not match; %r != %r' % (a['id'], b['id']))

        if errors:
            self.fail(msg or '; '.join(errors))
            return

    def assertNotSameEntity(self, a, b, msg=None):
        """Assert that the two given entities do not refer to the same object.

        E.g.: Both are dicts and their types and ids do not match.

        """
        errors = []

        for loc, x in (('1st', a), ('2nd', b)):
            if not isinstance(x, dict):
                errors.append('%s is a %r, not dict: %r' % (loc, type(x), x))
                continue
            if 'type' not in x:
                errors.append('%s has no type: %r' % (loc, x))
            if 'id' not in x:
                errors.append('%s has no id: %r' % (loc, x))

        if not errors and a['type'] == b['type'] and a['id'] == b['id']:
            errors.append('both entities are %s %s' % (a['type'], a['id']))

        if errors:
            self.fail(msg or '; '.join(errors))
            return

    # Add some of the unittest methods that we love from 2.7.
    if sys.version_info < (2, 7):

        def assertIs(self, a, b, msg=None):
            if a is not b:
                self.fail(msg or '%r at 0x%x is not %r at 0x%x; %r is not %r' % (type(a), id(a), type(b), id(b), a, b))

        def assertIsNot(self, a, b, msg=None):
            if a is b:
                self.fail(msg or 'both are %r at 0x%x; %r' % (type(a), id(a), a))

        def assertIsNone(self, x, msg=None):
            if x is not None:
                self.fail(msg or 'is not None; %r' % x)

        def assertIsNotNone(self, x, msg=None):
            if x is None:
                self.fail(msg or 'is None; %r' % x)

        def assertIn(self, a, b, msg=None):
            if a not in b:
                self.fail(msg or '%r not in %r' % (a, b))

        def assertNotIn(self, a, b, msg=None):
            if a in b:
                self.fail(msg or '%r in %r' % (a, b))

        def assertIsInstance(self, instance, types, msg=None):
            if not isinstance(instance, types):
                self.fail(msg or 'not an instance of %r; %r' % (types, instance))

        def assertNotIsInstance(self, instance, types, msg=None):
            if isinstance(instance, types):
                self.fail(msg or 'is an instance of %r; %r' % (types, instance))


# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

from msrest.serialization import Model

class FaceAttributes(Model):
    """Face Attributes.

    :param age: Age in years
    :type age: float
    :param gender: Possible gender of the face. Possible values include:
     'male', 'female'
    :type gender: str or ~azure.cognitiveservices.vision.face.models.Gender
    :param smile: Smile intensity, a number between [0,1]
    :type smile: float
    :param facial_hair:
    :type facial_hair:
     ~azure.cognitiveservices.vision.face.models.FacialHairProperties
    :param glasses: Glasses type if any of the face. Possible values include:
     'noGlasses', 'readingGlasses', 'sunglasses', 'swimmingGoggles'
    :type glasses: str or
     ~azure.cognitiveservices.vision.face.models.GlassesTypes
    :param head_pose:
    :type head_pose:
     ~azure.cognitiveservices.vision.face.models.HeadPoseProperties
    :param emotion:
    :type emotion:
     ~azure.cognitiveservices.vision.face.models.EmotionProperties
    :param hair:
    :type hair: ~azure.cognitiveservices.vision.face.models.HairProperties
    :param makeup:
    :type makeup: ~azure.cognitiveservices.vision.face.models.MakeupProperties
    :param occlusion:
    :type occlusion:
     ~azure.cognitiveservices.vision.face.models.OcclusionProperties
    :param accessories:
    :type accessories:
     list[~azure.cognitiveservices.vision.face.models.AccessoryItem]
    :param blur:
    :type blur: ~azure.cognitiveservices.vision.face.models.BlurProperties
    :param exposure:
    :type exposure:
     ~azure.cognitiveservices.vision.face.models.ExposureProperties
    :param noise:
    :type noise: ~azure.cognitiveservices.vision.face.models.NoiseProperties
    """

    _validation = {
        'smile': {'maximum': 1, 'minimum': 0},
    }

    _attribute_map = {
        'age': {'key': 'age', 'type': 'float'},
        'gender': {'key': 'gender', 'type': 'Gender'},
        'smile': {'key': 'smile', 'type': 'float'},
        'facial_hair': {'key': 'facialHair', 'type': 'FacialHairProperties'},
        'glasses': {'key': 'glasses', 'type': 'GlassesTypes'},
        'head_pose': {'key': 'headPose', 'type': 'HeadPoseProperties'},
        'emotion': {'key': 'emotion', 'type': 'EmotionProperties'},
        'hair': {'key': 'hair', 'type': 'HairProperties'},
        'makeup': {'key': 'makeup', 'type': 'MakeupProperties'},
        'occlusion': {'key': 'occlusion', 'type': 'OcclusionProperties'},
        'accessories': {'key': 'accessories', 'type': '[AccessoryItem]'},
        'blur': {'key': 'blur', 'type': 'BlurProperties'},
        'exposure': {'key': 'exposure', 'type': 'ExposureProperties'},
        'noise': {'key': 'noise', 'type': 'NoiseProperties'},
    }

    def __init__(self, age=None, gender=None, smile=None, facial_hair=None, glasses=None, head_pose=None, emotion=None, hair=None, makeup=None, occlusion=None, accessories=None, blur=None, exposure=None, noise=None):
        self.age = age
        self.gender = gender
        self.smile = smile
        self.facial_hair = facial_hair
        self.glasses = glasses
        self.head_pose = head_pose
        self.emotion = emotion
        self.hair = hair
        self.makeup = makeup
        self.occlusion = occlusion
        self.accessories = accessories
        self.blur = blur
        self.exposure = exposure
        self.noise = noise


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from users.models import CustomUser

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column, Field

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = UserChangeForm.Meta.fields

class CustomUserUpdateForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'avatar', 'phone','email', 'address')

    def __init__(self, *args, **kwargs):
        super(CustomUserUpdateForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Column('first_name', css_class='form-group col-md-6 mb-0'),
                Column('last_name', css_class='form-group col-md-6 mb-0'),
                css_class='form-row'
            ),
            Row(
                Column('phone', css_class='form-group col-md-6 mb-0'),
                Column('email', css_class='form-group col-md-6 mb-0'),
                css_class='form-row'
            ),
            Row(
                Column('address', css_class='form-group col-md-12 mb-0'),
                css_class='form-row'
            ),
            'avatar',

            Submit('submit', 'Update Information')
        )


# -*- encoding: utf-8 -*-

__author__ = 'shuhao.wang'

import os
import uuid

from Desire.BaseFramework.Resource.comm import *
from Desire.BaseFramework.Logger import logger
from Desire.BaseFramework.Utils import host

from .rpc_stubs import *

if os.name == "posix":
    _LOCAL_UUID = "/etc/desire/node/local.uuid"
    _AUTH_UUID = "/etc/desire/node/auth.uuid"
else:
    _LOCAL_UUID = "c:/desire/etc/node/local.uuid"
    _AUTH_UUID = "c:/desire/etc/node/auth.uuid"

_LOGGER_NAME = "Node Server"

_SERVER_NODE = None

def _check_local():
    """
        检查本地环境

        本地UUID，服务授权ID
    :return:
        True or False
        成功返回True，失败返回False
    """

    global _LOCAL_UUID, _AUTH_UUID

    # 检查本地UUID文件
    if not os.path.exists(_LOCAL_UUID):
        return False

    # 检查服务授权ID文件
    if not os.path.exists(_AUTH_UUID):
        return False

def _read_self_uuid():

    global _LOCAL_UUID, _AUTH_UUID

    _LOGGER = logger.get_logger()
    # 打开文件
    local_uuid = None
    try:
        with open(_LOCAL_UUID, "rb") as f:
            # 36 len(uuid)
            local_uuid = f.read(36)
    except IOError as e:
        _LOGGER.warn(_LOGGER_NAME, "Open local uuid error: %s" % str(e))

    auth_uuid = None
    try:
        with open(_AUTH_UUID, "rb") as f:
            auth_uuid = f.read(36)
    except IOError as e:
        _LOGGER.warn(_LOGGER_NAME, "Open auth uuid error: %s" % str(e))

    return local_uuid, auth_uuid

# 暂不使用
def _make_local_uuid():
    global _LOCAL_UUID
    _LOGGER = logger.get_logger()

    if os.path.exists(_LOCAL_UUID):
        _LOGGER.warn(_LOGGER_NAME, "Local uuid file already exists")
        return

    if not os.path.exists(os.path.dirname(_LOCAL_UUID)):
        os.makedirs(os.path.dirname(_LOCAL_UUID))

    local_uuid = uuid.uuid4()

    try:
        with open(_LOCAL_UUID, "w") as f:
            f.write(str(local_uuid))
    except IOError as e:
        _LOGGER.error(_LOGGER_NAME, "Can not write local uuid to file")
        raise e

def _write_auth_uuid(auth_uuid):
    global _AUTH_UUID
    _LOGGER = logger.get_logger()

    if os.path.exists(_AUTH_UUID):
        _LOGGER.error(_LOGGER_NAME, "Auth UUID file already exists!")
        raise IOError("Auth UUID file already exists!")

    if not os.path.exists(os.path.dirname(_AUTH_UUID)):
        os.makedirs(os.path.dirname(_AUTH_UUID))

    try:
        with open(_AUTH_UUID) as f:
            f.write(auth_uuid)
    except IOError as e:
        _LOGGER.error(_LOGGER_NAME, "can not write auth uuid to file")
        raise e

def register():
    """
        想系统管理中心注册本服务器节点

        首次注册会被添加到系统管理中心，属于为认证的服务器，不会被调度和使用
        需要管理员在控制台中认证后才会被正式接纳为可提供服务的机器
    """
    _LOGGER = logger.get_logger()
    # 检查本地信息,本地UUID应在安装系统时确定，而不能动态生成
    if not _check_local():
        raise SystemError("No local uuid file")

    local_uuid, auth_uuid = _read_self_uuid()

    server_node = ResServer()

    server_node["name"] = host.name

    server_node["uuid"] = local_uuid
    server_node["auth_uuid"] = auth_uuid
    server_node["role"] = 0
    server_node["location"] = host.ip_addr
    # FIXME: 处理器的数量
    server_node["num_of_processor"] = 1
    # FIXME: 每处理器的核心数目
    server_node["cores_of_per_processor"] = 6
    # FIXME: 服务器的内存大小
    server_node["memory_size"] = 16

    try:
        global _SERVER_NODE
        server_node = stub_register(server_node)

        # auth_uuid为None时，说明服务器并没有被认证
        if (auth_uuid is None and server_node['auth_uuid'] is not None) or\
            (auth_uuid != server_node['auth_uuid']):
            # 管理中心返回的是已经认证的，本地没有记录
            # 管理中心的记录和本地记录不一致
            # 上述两种情况，说明服务器数据存在问题，按规矩本地应该罢工
            raise ValueError(
                "An error has occurred: local auth uuid is None, but remote auth uuid is %s" %
                server_node['auth_uuid'])

        _SERVER_NODE = server_node
    except Exception as e:
        _LOGGER.error(_LOGGER_NAME, "Register node server error: %s" % str(e))

        raise e

def is_authed():
    """
        判断本节点是否已经通过管理中心授权
    :return:
        True 已经通过管理中心授权
        False 没有通过管理中心授权
    """
    global _SERVER_NODE

    return False if _SERVER_NODE["auth_uuid"] is None else True

def handle_auth(auth_node):
    """
        管理中心认证时触发本地节点进行认证
    :param auth_node: 认证的节点信息，正常情况下指向本节点
    :return: None
    """
    global _SERVER_NODE

    if auth_node != _SERVER_NODE:
        raise ValueError("Invalid Server Node")

    _write_auth_uuid(auth_node["auth_uuid"])
    # 更新本地的ServerNode
    _SERVER_NODE['auth_uuid'] = auth_node["auth_uuid"]


import csv
import requests
'''
0 - course
1 - username
2 - uid
3 - cid
4 - eid
5 - role
'''
def main():
    site = 'school'
    code = 'my cool code'
    csv_file = 'my cool cvsfile'
    base_url = f"https://{site}.instructure.com/api/v1/accounts/1/enrollments/:id"
    header = {'Authorization':f'Bearer {code}'}
    with open(csv_file,'r') as csvfile:
        reader = csv.reader(csvfile,delimiter='\t')
        for row in reader:
            URL = base_url.replace(":id",row[4])
            response = requests.get(URL,headers=header)
            if response.status_code == 200:
                data = response.json()
                for k in data.keys():
                    print(f"{data[k]}\t",end='')
                print('',flush=True)

main()


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

import tkinter as tk
from tkinter import ttk
from itertools import product
from PIL import Image, ImageTk
import numpy as np
import pandas as pd
import csv

class ApplicationState(object):
    def __init__(self):
        self.dataframe = pd.read_csv('data/twcs.csv', index_col=0, encoding='utf8', low_memory=True)
        #self.dataframe.set_index('tweet_id', inplace=True)
        self.dataframe['is_question'] = None
        self.dataframe['q_type'] = None
        self.idx = 0

    def save(self, event):
        self.dataframe.to_csv(
            'data/twcs.lbl.csv',
            sep='\t',
            na_rep='NaN',
            index=True,
            encoding='utf8',
            quoting=csv.QUOTE_ALL,
            line_terminator='\n',
        )

    def prev(self, event):
        self.idx = max(self.idx - 1, 0)

    def next(self, event):
        self.idx = min(self.idx + 1, len(self.dataframe) - 1)

class Application(tk.Tk):
    @classmethod
    def dataframe_widget(cls, parent):
        frame = ttk.Frame(parent)

        dataframe = parent.app_state.dataframe

        index = [dataframe.index.name]
        columns = dataframe.columns.tolist()
        tree = ttk.Treeview(frame, columns=columns, selectmode='browse')

        for i,v in enumerate(index + columns):
            c = '#{}'.format(i)
            tree.heading(c, text=v)

            tree.column(c, minwidth=25, width=50, stretch=True)

        k = columns.index('text')
        tree.column(k, minwidth=150, width=300, stretch=True)

        # Only load 500
        count = 31
        for i, r in dataframe.iterrows():
            if count==0: break
            count -= 1

            vals = [ r[v] for v in columns ]
            vals[k] = vals[k].encode('utf8')
            tree.insert('', 'end', iid=i, values=vals, text=i)

        vsb = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
        tree.configure(yscrollcommand=vsb.set)

        tree.grid(row=0, column=0, sticky=tk.N+tk.S+tk.W+tk.E)
        vsb.grid(row=0, column=1, sticky=tk.N+tk.S)

        frame.rowconfigure(0, weight=1)
        frame.columnconfigure(0, weight=1)

        return frame

    @classmethod
    def tweet_view_widget(cls, parent, text='Tweet'):
        frame = ttk.LabelFrame(parent, text=text)

        frame.elemets = {
            'tweet_id': ttk.Entry,
            'author_id': ttk.Entry,
            'inbound': ttk.Entry,
            'created_at': ttk.Entry,
            'text': tk.Text,
            'response_tweet_id': ttk.Entry,
            'in_response_to_tweet_id': ttk.Entry
        }

        # Place a label before each
        frame.elem_frames = {}
        for k in frame.elemets:
            frame.elem_frames[k] = ttk.LabelFrame(frame, text=k)
            kw = dict(cursor='arrow')
            frame.elemets[k] = frame.elemets[k](frame.elem_frames[k], **kw)
            frame.elemets[k].bind('<Key>', lambda _: 'break')
            frame.elemets[k].pack(fill='x')

        # Place the  widgets in layout
        frame.elem_frames['tweet_id'].grid(row=0, column=0, sticky=tk.E+tk.W)
        frame.elem_frames['author_id'].grid(row=0, column=1, sticky=tk.E+tk.W)
        frame.elem_frames['inbound'].grid(row=0, column=2, sticky=tk.E+tk.W)
        frame.elem_frames['created_at'].grid(row=1, column=0, sticky=tk.E+tk.W)
        frame.elem_frames['response_tweet_id'].grid(row=1, column=1, sticky=tk.E+tk.W)
        frame.elem_frames['in_response_to_tweet_id'].grid(row=1, column=2, sticky=tk.E+tk.W)
        frame.elem_frames['text'].grid(row=2, column=0, columnspan=3, sticky=tk.N+tk.S+tk.E+tk.W)

        for i in range(3): frame.rowconfigure(i, weight=1)
        for i in range(3): frame.columnconfigure(i, weight=1)

        return frame

    @classmethod
    def labeler_widget(cls, parent):
        labeler = ttk.Frame(parent)

        q_tv = cls.tweet_view_widget(labeler, 'Tweet')
        btn_frame = ttk.Frame(labeler)
        #a_tv = cls.tweet_view_widget(labeler, 'Answer')

        lbl_frame = ttk.Frame(btn_frame)
        p_btn = ttk.Button(btn_frame, text='<<')
        n_btn = ttk.Button(btn_frame, text='>>')
        s_btn = ttk.Button(btn_frame, text='Save')
        o_frame = ttk.Frame(btn_frame)
        ok_btn = ttk.Button(btn_frame, text='OK')
        nq_btn = ttk.Button(btn_frame, text='Not a Question')
        # ----

        p_btn.bind('<Button-1>', parent.app_state.prev)
        n_btn.bind('<Button-1>', parent.app_state.next)
        s_btn.bind('<Button-1>', parent.app_state.save)

        # ----

        parent.options = [
            {'text': 'Where'},
            {'text': 'When'},
            {'text': 'How'},
            {'text': 'Why'},
        ]

        for i, (r, c) in zip(range(len(parent.options)), product(range(len(parent.options)//2), range(2))):
            parent.options[i]['var'] = tk.BooleanVar(False)
            parent.options[i]['btn'] = ttk.Checkbutton(o_frame, text=parent.options[i]['text'], offvalue=False, onvalue=True, variable=parent.options[i]['var'])
            parent.options[i]['btn'].grid(row=r, column=c, padx=5, sticky=tk.W)
        # ----

        photo = tk.PhotoImage(file='data/hang-in-there.gif')
        label = ttk.Label(lbl_frame, image=photo)
        label.image = photo # keep a reference!
        label.pack()

        lbl_frame.grid(row=0, column=0, columnspan=2, sticky=tk.N)
        p_btn.grid(row=1, column=0, sticky=tk.E+tk.W)
        n_btn.grid(row=1, column=1, sticky=tk.E+tk.W)
        s_btn.grid(row=2, column=0, columnspan=2)
        o_frame.grid(row=3, column=0, columnspan=2)
        ok_btn.grid(row=4, column=0)
        nq_btn.grid(row=4, column=1)

        q_tv.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
        btn_frame.grid(row=0, column=1, sticky=tk.N+tk.S)
        #a_tv.grid(row=0, column=2)

        labeler.rowconfigure(0, weight=1)
        labeler.columnconfigure(0, weight=1)
        labeler.columnconfigure(0, weight=1)

        return labeler

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.tk.call('encoding', 'system', 'utf-8')
        self.title("Hang in There!")

        self.app_state = ApplicationState()

        # Widgets go here
        self.nb = ttk.Notebook(self)
        self.nb.pack(expand=True, fill='both', padx=2, pady=2)
        self.nb.bind("<<NotebookTabChanged>>", self._on_tab_changed)

        self.nb.app_state = self.app_state

        self.tabs = {
            'Labeler': Application.labeler_widget(self.nb),
            'Dataframe': Application.dataframe_widget(self.nb)
        }

        for name, tab in self.tabs.items():
            self.nb.add(tab, text=name)

        self.nb.select(self.tabs['Labeler'])
        self.nb.enable_traversal()

        #self.resizable(False, False)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

    def _on_tab_changed(self, event):
        event.widget.update_idletasks()
        tab = event.widget.nametowidget(event.widget.select())
        event.widget.configure(height=tab.winfo_reqheight(), width=tab.winfo_reqwidth())

if __name__ == '__main__':
    root = Application()
    root.mainloop()


from collections import deque

N, M = map(int, input().split())

board = []
init_virus = []
ans = 0

for y in range(N):
    board.append(list(map(int, input().split())))
    for x in range(M):
        if board[y][x] == 2:
            init_virus.append([x, y])

def get_next_wall(pos):
    x, y = pos

    x += 1
    if x >= M:
        x = 0
        y += 1

    while 1:
        if y == N:
            return 0
        if board[y][x] != 0:
            x += 1
            if x >= M:
                x = 0
                y += 1
        else:
            return [x, y]

dx = (0, 0, -1, 1)
dy = (-1, 1, 0, 0)

def set_wall(pos, wall_num):
    global ans
    if wall_num == 3:
        visit = [[0] * M for _ in range(N)]
        bfs_q = deque()
        for virus_pos in init_virus:
            bfs_q.append(virus_pos)
            visit[virus_pos[1]][virus_pos[0]] = 1

        while bfs_q:
            current = bfs_q.popleft()
            for df in range(4):
                new = [current[0] + dx[df], current[1] + dy[df]]
                if 0 <= new[0] < M and 0 <= new[1] < N and (board[new[1]][new[0]] == 0) and not visit[new[1]][new[0]]:
                    visit[new[1]][new[0]] = 1
                    bfs_q.append(new)

        partial_ans = 0
        for y in range(N):
            for x in range(M):
                if visit[y][x] == 0 and board[y][x] == 0:
                    partial_ans += 1

        if ans < partial_ans:
            ans = partial_ans

        return
    else:
        next_wall = get_next_wall(pos)
        while 1:
            if next_wall == 0:
                return
            board[next_wall[1]][next_wall[0]] = 1
            set_wall(next_wall, wall_num + 1)
            board[next_wall[1]][next_wall[0]] = 0
            next_wall = get_next_wall(next_wall)

set_wall([-1, 0], 0)

print(ans)


import argparse
import sys

from gitopscli.io.yaml_util import yaml_load

def create_cli(args):
    parser, subparsers = __create_cli_parser()
    __add_deploy_command_parser(subparsers)
    __add_sync_apps_command_parser(subparsers)
    __add_pr_comment_command_parser(subparsers)
    __add_create_preview_command_parser(subparsers)
    __add_create_pr_preview_command_parser(subparsers)
    __add_delete_preview_command_parser(subparsers)
    __add_delete_pr_preview_command_parser(subparsers)
    __add_version_command_parser(subparsers)

    if len(args) == 0:
        parser.print_help(sys.stderr)
        sys.exit(2)

    return parser.parse_args(args)

def __create_cli_parser():
    parser = argparse.ArgumentParser(prog="gitopscli", description="GitOps CLI")
    subparsers = parser.add_subparsers(title="commands", dest="command")
    return parser, subparsers

def __add_deploy_command_parser(subparsers):
    deploy_p = subparsers.add_parser("deploy", help="Trigger a new deployment by changing YAML values")
    deploy_p.add_argument("--file", help="YAML file path", required=True)
    deploy_p.add_argument(
        "--values",
        help="YAML/JSON object with the YAML path as key and the desired value as value",
        type=yaml_load,
        required=True,
    )
    deploy_p.add_argument(
        "--single-commit",
        help="Create only single commit for all updates",
        type=__str2bool,
        nargs="?",
        const=True,
        default=False,
    )
    deploy_p.add_argument(
        "--commit-message", help="Specify exact commit message of deployment commit", type=str, default=None,
    )

    __add_git_parser_args(deploy_p)
    __add_branch_pr_parser_args(deploy_p)
    __add_verbose_parser(deploy_p)

def __add_sync_apps_command_parser(subparsers):
    sync_apps_p = subparsers.add_parser(
        "sync-apps", help="Synchronize applications (= every directory) from apps config repository to apps root config"
    )
    __add_git_parser_args(sync_apps_p)
    __add_verbose_parser(sync_apps_p)
    sync_apps_p.add_argument("--root-organisation", help="Root config repository organisation", required=True)
    sync_apps_p.add_argument("--root-repository-name", help="Root config repository name", required=True)

def __add_pr_comment_command_parser(subparsers):
    add_pr_comment_p = subparsers.add_parser("add-pr-comment", help="Create a comment on the pull request")
    __add_git_parser_args(add_pr_comment_p, api_only=True)
    __add_create_prid_parser(add_pr_comment_p)
    __add_verbose_parser(add_pr_comment_p)
    add_pr_comment_p.add_argument("--text", help="the text of the comment", required=True)

def __add_create_preview_command_parser(subparsers):
    add_create_preview_common_p = subparsers.add_parser("create-preview", help="Create a preview environment")
    __add_git_parser_args(add_create_preview_common_p)
    __add_create_githash_previewid_parser(add_create_preview_common_p)
    __add_verbose_parser(add_create_preview_common_p)

def __add_create_pr_preview_command_parser(subparsers):
    add_create_pr_preview_p = subparsers.add_parser("create-pr-preview", help="Create a preview environment")
    __add_git_parser_args(add_create_pr_preview_p)
    __add_create_prid_parser(add_create_pr_preview_p)
    __add_verbose_parser(add_create_pr_preview_p)

def __add_delete_preview_command_parser(subparsers):
    add_delete_preview_p = subparsers.add_parser("delete-preview", help="Delete a preview environment")
    __add_git_parser_args(add_delete_preview_p)
    add_delete_preview_p.add_argument(
        "--preview-id", help="The preview-id for which the preview was created for", required=True
    )
    __add_verbose_parser(add_delete_preview_p)

def __add_delete_pr_preview_command_parser(subparsers):
    add_delete_preview_p = subparsers.add_parser("delete-pr-preview", help="Delete a pr preview environment")
    __add_git_parser_args(add_delete_preview_p)
    add_delete_preview_p.add_argument(
        "--branch", help="The branch for which the preview was created for", required=True
    )
    __add_verbose_parser(add_delete_preview_p)

def __add_version_command_parser(subparsers):
    subparsers.add_parser("version", help="Show the GitOps CLI version information")

def __add_git_parser_args(deploy_p, api_only=False):
    deploy_p.add_argument("--username", help="Git username", required=True)
    deploy_p.add_argument("--password", help="Git password or token", required=True)
    if not api_only:
        deploy_p.add_argument("--git-user", help="Git Username", default="GitOpsCLI")
        deploy_p.add_argument("--git-email", help="Git User Email", default="gitopscli@baloise.dev")
    deploy_p.add_argument("--organisation", help="Apps Git organisation/projectKey", required=True)
    deploy_p.add_argument("--repository-name", help="Git repository name (not the URL, e.g. my-repo)", required=True)
    deploy_p.add_argument("--git-provider", help="Git server provider")
    deploy_p.add_argument("--git-provider-url", help="Git provider base API URL (e.g. https://bitbucket.example.tld)")

def __add_branch_pr_parser_args(deploy_p):
    deploy_p.add_argument(
        "--create-pr", help="Creates a Pull Request", type=__str2bool, nargs="?", const=True, default=False,
    )
    deploy_p.add_argument(
        "--auto-merge",
        help="Automatically merge the created PR (only valid with --create-pr)",
        type=__str2bool,
        nargs="?",
        const=True,
        default=False,
    )

def __add_create_githash_previewid_parser(subparsers):
    subparsers.add_argument("--git-hash", help="the git hash which should be deployed", type=str, required=True)
    subparsers.add_argument(
        "--preview-id", help="the id of folder in the config repo which will be created", type=str, required=True
    )

def __add_create_prid_parser(subparsers):
    subparsers.add_argument("--pr-id", help="the id of the pull request", type=int, required=True)
    subparsers.add_argument("--parent-id", help="the id of the parent comment, in case of a reply", type=int)

def __add_verbose_parser(subparsers):
    subparsers.add_argument(
        "-v", "--verbose", help="Verbose exception logging", type=__str2bool, nargs="?", const=True, default=False,
    )

def __str2bool(value):
    if isinstance(value, bool):
        return value
    if value.lower() in ("yes", "true", "t", "y", "1"):
        return True
    if value.lower() in ("no", "false", "f", "n", "0"):
        return False
    raise argparse.ArgumentTypeError("Boolean value expected.")


import matplotlib.pyplot as plt
import numpy as np

def show(data):
    x=np.arange(0, len(data[0]), 1)
    for d in data:
        plt.plot(x, d)
    plt.legend(['red0', 'red1', 'red2', 'red3', 'red4', 'red5', 'blue'])
    plt.show()

def main():
    data = np.loadtxt("data/history/ssq.csv", delimiter=',', usecols=(2,3,4,5,6,7,8), unpack=True, skiprows=1)
    print(data)
    show(data)

if __name__ == '__main__':
    main()