#!/usr/bin/env python
"""
Script to prepare nextstrain alpha files
"""
###TODO
# Assumptions : Second column sample_name is unique by itself and only the latest run for the sample is retained in submission manifest
#1) Get files in the submission manifest from seq_path
#2) Exclude submitted files as GISAID will already have it
#3) Add submitted files to local build only
#4) If metadata exists use metadata
#5) Else create fake metadata for the non existing samples

import pandas as pd
import numpy as np
import json , os
import argparse
import random , time
from datetime import datetime , date , timedelta
from pandas.io.json import json_normalize
import glob ,re , tempfile ,shutil
import random , time
from datetime import datetime , date , timedelta
from pyfaidx import Fasta
from datetime import date
import warnings
from pytz import timezone
from Bio import SeqIO

warnings.simplefilter(action='ignore', category=FutureWarning)
pd.options.mode.chained_assignment = None  # default='warn'

class PrepareAlphaNextstrain:
    """
    Prepare data for Nextstrain
    """

    def __init__(self,sub_manifest,SEQ_PATH,FASTA_PATH,MASTER_META,SUBMITTED_SEQ,OUTPUT_DIR):
        """
        Class to prepare alpha data consensus data for nextstrain

        :param sub_manifest: Submission Manifest file for a sequencing run
        :param SEQ_PATH: path to sequencing runs
        :param FASTA_PATH: path to fasta file within sequencing runs
        :param MASTER_META: path to master metadata excel sheet
        :param SUBMITTED_SEQ: path to fasta file of already submitted sequences
        :param OUTPUT_DIR: path to alpha input files
        """
        self.id_mapping_file = sub_manifest
        self.SEQ_PATH = SEQ_PATH
        self.FASTA_PATH = FASTA_PATH
        self.MASTER_META = MASTER_META
        self.SUBMITTED_SEQ = SUBMITTED_SEQ
        self.OUTPUT_DIR = OUTPUT_DIR
        self.CONSENSUS_FASTA = "*.nanopolish.consensus.fasta"
        self.DATE_FMT ="%Y%m%d"
        self.SUB_FMT ="%Y-%m-%d"

        self.REF_LENGTH = 29903
        self.count_Ns = 5000

        self.CUR_DATE = datetime.now(timezone('US/Eastern'))
        self.SUB_DATE = self.CUR_DATE.strftime(self.SUB_FMT)
        self.CUR_DATE = self.CUR_DATE.strftime(self.DATE_FMT)

        self.ALPHA_PREFIX= os.path.join(self.OUTPUT_DIR, self.CUR_DATE)
        self.STATE_PREFIX = "MD"
        self.header_pat = re.compile('([A-Z]+-[0-9]*_[A-Z]+[0-9]+)')

        ### Setting default metadata for all samples
        self.loc_default = "North America / USA / Maryland"
        self.host = "Human"
        self.specimen_source = "Nasopharyngeal swab"
        self.seq_tech = "Nanopore MinION"
        self.assembly_method = "ARTIC 1.0.0"
        self.dep = "Johns Hopkins Hospital Department of Pathology"
        self.address = "600 N. Wolfe St; Baltimore, MD 21287"
        self.authors = "Peter M. Thielen, Thomas Mehoke, Shirlee Wohl, Srividya Ramakrishnan, Melanie Kirsche, Amanda Ernlund, Oluwaseun Falade-Nwulia, Timothy Gilpatrick, Paul Morris, Norah Sadowski, Nídiá Trovao, Victoria Gniazdowski, Michael Schatz, Stuart C. Ray, Winston Timp, Heba Mostafa"
        self.submitter = "JHU"
        self.next_fields = [ "Virus name",
                             "Type",
                             "Passage details/history",
                             "Collection date",
                             "Location",
                             "Additional location information",
                             "Host",
                             "Additional host information",
                             "Gender",
                             "Patient age",
                             "Patient status",
                             "Specimen source",
                             "Outbreak",
                             "Last vaccinated",
                             "Treatment",
                             "Sequencing technology",
                             "Assembly method",
                             "Coverage",
                             "Originating lab",
                             "Address",
                             "Sample ID given by the sample provider",
                             "Submitting lab",
                             "Address.1",
                             "Sample ID given by the submitting laboratory",
                             "Authors",
                             "Submitter",
                             "Submission date",
                             "Run date",
                             "Run folder" ]

    def log(self,message):
        """ Log messages to standard output. """
        print(time.ctime() + ' --- ' + message, flush=True)

    def get_fasta_lengths(self,fasta):
        fa_lens = []
        N_counts = []
        for seq_record in SeqIO.parse(fasta, "fasta"):
               A_count = seq_record.seq.count('A')
               C_count = seq_record.seq.count('C')
               G_count = seq_record.seq.count('G')
               T_count = seq_record.seq.count('T')
               N_count = seq_record.seq.count('N')
               fa_lens.append(len(seq_record))
               N_counts.append(N_count)
        return fa_lens, N_counts

    def getKeysByValues(self,dictOfElements, listOfValues):
        listOfKeys = list()
        listOfItems = dictOfElements.items()
        for item  in listOfItems:
            if item[1] in listOfValues:
                listOfKeys.append((item[1], item[0]))
        return  listOfKeys

    def get_file_list(self,file_list,file_pattern):
        """
        get file list of consensus fasta from SEQ_PATH and FASTA_PATH

        """
        c_list = []
        for i in file_list:
            cfile = glob.glob(i + self.CONSENSUS_FASTA)[0]
            c_list.append(cfile)
        return c_list

    def get_fasta_header(self,seq):
        """
         get header from fasta file
        """
        with open(seq,'r') as fd:
           header = []
           for line in fd:
               if line[0]=='>':
                   header.append(line.strip().split(">")[1].split(" ")[0])
        return header

    def map_submission_names(self):
        """
        Extract submission names from submission_manifest.txt
        """
        sub_file = self.id_mapping_file
        self.sname_dict= {}
        self.sheader_dict = {}
        self.only_sname_dict = {}
        self.only_sheader_dict = {}
        with open(sub_file,'r') as fs:
             for line in fs:
                 run_id, sample_name, sample_header , renamed_header, coveragex = line.strip().split("\t")
                 s_key = run_id + ":" + sample_name
                 h_key = run_id + ":" + sample_header
                 sn_key = sample_name
                 sh_key = sample_header
                 if  s_key not in self.sname_dict:
                     self.sname_dict[s_key] = renamed_header
                 if  h_key not in self.sheader_dict:
                     self.sheader_dict[h_key] = renamed_header
                 if  sn_key not in self.only_sname_dict:
                     self.only_sname_dict[sn_key] = renamed_header
                 if  sh_key not in self.only_sheader_dict:
                     self.only_sheader_dict[sh_key] = renamed_header
        return ( self.sname_dict , self.sheader_dict, self.only_sname_dict, self.only_sheader_dict )

    def concat_fasta_files(self,fa_list, out):
        """
        Function to concat fasta files from a list
        """
        filt_fnames = []
        with open(out, 'w') as outfile:
             for fname in fa_list:
                 ffile = glob.glob(fname)[0]
                 g_lengths, n_counts = self.get_fasta_lengths(ffile)
                 if g_lengths[0] == 29903 and n_counts[0] < 5000:
                    filt_fnames.append(ffile)
                    with open(ffile) as infile:
                         for line in infile:
                             outfile.write(line)
                 else:
                    self.log("INFO : Skipping {0} from alpha Genome Length :  {1} , N_counts : {2}".format(fname,g_lengths[0],n_counts[0]))
        return filt_fnames

    def filter_metadata(self,sample_list,out,fill_meta=None):
        """
        Function to filter metadata from a list
        """
        meta_tsv = pd.read_table(self.MASTER_META)
        existing_meta_list = meta_tsv["Virus name"].tolist()
        self.meta_columns = meta_tsv.columns.values.tolist()
        self.no_meta_ids = [ i for i in sample_list if i not in existing_meta_list ]
        meta_tsv = meta_tsv[ meta_tsv["Virus name"].isin(sample_list) ]
        # Change all unknown collection date to run_date
        for index, row in meta_tsv.iterrows():
            if meta_tsv.loc[index,'Collection date'] in ["", "?", "??","unknown", None] :
               #(meta_tsv.loc[index,'Collection date'])
               meta_tsv.loc[index,'Collection date'] = meta_tsv.loc[index,'Run date']
            if meta_tsv.loc[index,'Submission date'] in ["", "?", "??","unknown", None] :
               meta_tsv.loc[index,'Submission date'] = self.SUB_DATE
            #if meta_tsv.loc[index,'Location'].astype(str).str.contains("??|unknown") :
            #   meta_tsv.loc[index,'Location'].replace("??",)
        meta_tsv[['Collection date', 'Submission date']] = meta_tsv[['Collection date','Submission date']].fillna(value=self.SUB_DATE)

        # Change all unknown State to MT (  unknown )
        meta_tsv.to_csv(out, sep='\t', encoding='utf-8',  index=False, line_terminator='\r\n')
        return out

    def rename_submission_fasta(self,alpha_fa_file,rename_dict):
        """
        Get the GISAID submission names for all the files selected for submission

        """
        fa_file = Fasta(alpha_fa_file, key_function=lambda key: rename_dict[key])
        renamed_fa_file = alpha_fa_file + ".renamed"
        with open(renamed_fa_file, 'w') as renamed:
             for entry in fa_file:
                 print(">" + entry.name, file=renamed)
                 for line in entry:
                      print(line, file=renamed)
        shutil.copy(renamed_fa_file, alpha_fa_file)
        if os.path.isfile(renamed_fa_file):
            os.remove(renamed_fa_file)
        fa_file = Fasta(alpha_fa_file)
        return alpha_fa_file

    def prepare_fasta(self):
        """
        Function to the final Alpha nextstrain fasta files
        - Get sname_dict  , sheader_dict mappings from submission manifest
        - Get existing_seq , headers of the submitted files
        - If key in already submitted , remove the file from alpha_file_list; concat files
        - append submitted files to a new file all_jhu_sequences.fasta
        """
        self.sname_dict , self.sheader_dict , self.only_sname_dict , self.only_header_dict =  self.map_submission_names()
        alpha_tuple_list = [ (i.split(":")[0] , i.split(":")[1]) for i in self.sname_dict.keys() ]
        self.alpha_file_list = [x[1] for x in alpha_tuple_list]

        self.existing_seq = self.get_fasta_header(self.SUBMITTED_SEQ)
        self.existing_snames = [ name.split(":")[1] for name, rname in self.sname_dict.items() if rname in self.existing_seq ]
        self.alpha_file_list = [e for i, e in enumerate(self.alpha_file_list) if e not in self.existing_snames]
        alpha_file_path_list = [ self.SEQ_PATH + "/" + r + "/" + self.FASTA_PATH + "/" + s for (r,s) in alpha_tuple_list if s in self.alpha_file_list ]
        alpha_file_path_list = self.get_file_list(alpha_file_path_list, self.CONSENSUS_FASTA)

        self.outseq = self.ALPHA_PREFIX + "-jhu_sequences.fasta"
        self.all_outseq = self.ALPHA_PREFIX + "-all_local_jhu_sequences.fasta"
        filt_fnames = self.concat_fasta_files(alpha_file_path_list,self.outseq)

        ori_headers = self.get_fasta_header(self.outseq)
        #new_headers = [ i.rsplit("/",3)[1].split(".nanopolish")[0] for i in ori_headers ]
        new_headers = {}
        for header in ori_headers:
            m = re.search(self.header_pat,header)
            new_head = m.group(0)
            if header not in new_headers:
                new_headers[header] = new_head

        #new_headers = { i : re.search(self.header_pat,i).group(0) for i in ori_headers }
        #new_headers = [ i.split("4-draft-consensus/")[1] for i in new_headers ]
        self.rename_submission_fasta(self.outseq,new_headers)
        # Rename header if not in self.only_header_dict
        ### This part of the code is to deal with names of format MDHP-18 to be renamed to MDHP-00018
        #for k ,i in new_headers.items():
        #    if i not in self.only_header_dict:
        #        parts = i.split("-")
        #        if len(parts[1].split("_")[0]) < 5:
        #            new_value = "-".join([parts[0],parts[1].split("_")[0].zfill(5)+"_"+parts[1].split("_")[1]])
        #            new_headers[k] = new_value

        rename_dict =  { key : self.only_header_dict[key] if key in self.only_header_dict else key for key in new_headers.values() }
        self.rename_submission_fasta(self.outseq,rename_dict)
        self.all_jhu_list = [self.SUBMITTED_SEQ ,self.outseq ]
        self.concat_fasta_files(self.all_jhu_list,self.all_outseq)
        return ( self.outseq  , self.all_outseq )
        # return final fasta

    def prepare_metadata(self):
        """
        Function to get metadata for the fasta files
        - get all submitted seq metadata
        - Get strain ids , cut by sep "/" index 2, cut "-" [1] to get HP name
        - Append STATE prefix to HP name
        - If present. Filter rows based on ids
        - If not present, create fake metadata based on run_date, default location (Maryland); unknown for everything else
        """
        sample_names = self.get_fasta_header(self.outseq)
        all_sample_names = self.get_fasta_header(self.all_outseq)
        self.outmeta = self.ALPHA_PREFIX + "-jhu_metadata.tsv"
        self.all_outmeta = self.ALPHA_PREFIX + "-all_local_jhu_metadata.tsv"
        self.filter_metadata(sample_names,self.outmeta)
        self.filter_metadata(all_sample_names,self.all_outmeta)
        if len(self.no_meta_ids) > 0:
            self.log("INFO : No metadata for ids : {0}".format(",".join(self.no_meta_ids)))
            self.log("INFO : Adding metadata for the ids")
            no_meta_mapped_list = [ (k, i.split(":")[1] , i.split(":")[0]) for k,i in self.getKeysByValues(self.sname_dict,self.no_meta_ids) ]
            missing_df_lists = []
            missing_df = pd.DataFrame(columns = self.meta_columns)
            self.add_meta_cols = [ i for i in missing_df.columns.tolist() if i not in self.next_fields ]
            #print("\n".join(self.add_meta_cols))
            add_rows = []
            for i,j,k in no_meta_mapped_list:

                run_date = datetime.strptime(k.split("_")[0],self.DATE_FMT)
                run_date = run_date.strftime(self.SUB_FMT)
                row = [ i ,
                       "betacoronavirus",
                       "Original",
                       run_date ,
                       self.loc_default , "unknown" ,
                       self.host,
                       "unknown", "unknown" , "unknown" , "unknown" ,
                       self.specimen_source ,
                       "unknown", "unknown" , "unknown" ,
                       self.seq_tech , self.assembly_method ,
                       "unknown" , self.dep  , self.address , j , self.dep  , self.address, j,
                       self.authors , self.submitter , self.SUB_DATE, run_date , k ]
                row = row + ["unknown" for i in self.add_meta_cols]
                add_rows.append(row)
                #missing_df.loc[len(missing_df)] = row
                #missing_df = missing_df.append(row)
                #missing_df = pd.concat([missing_df, pd.DataFrame(row)], axis=0)
                #missing_df.loc[ missing_df.index.max() +  1 ] =  row
                #missing_df[self.meta_columns] = row

                #missing_df = missing_df.append(pd.Series(row),index = self.meta_columns )
            missing_df = pd.DataFrame(add_rows, columns=self.meta_columns )
            missing_df.to_csv(self.outmeta, mode='a', sep="\t",header=False)
            missing_df.to_csv(self.all_outmeta, mode='a', sep="\t",header=False)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Script to Parse new sequencing runs from alpha nextstrain")
    parser.add_argument("--submission_manifest",dest="sub_manifest",type=str, required=True, help="Path to the tsv file with run_id,sample_name,sample_header,rename_header,coveragex")
    parser.add_argument("--seq-path",dest="SEQ_PATH",type=str, required=True, help="Path to the sequencing runs folder")
    parser.add_argument("--fasta-path",dest="FASTA_PATH",type=str, required=True, help="Path to the draft consensus fasta folder")
    parser.add_argument("--submitted-fasta",dest="SUBMITTED_SEQ",type=str, required=True, help="Path to a single fasta of already submitted fasta files")
    parser.add_argument("--master-meta",dest="MASTER_META",type=str, required=True, help="File containing the master metadata for GISAID submission")
    parser.add_argument("-out",dest="OUTPUT_DIR",type=str, required=True, help="Path to output directory for alpha nextstrain")

    args = parser.parse_args()
    prepAlpha = PrepareAlphaNextstrain(args.sub_manifest,args.SEQ_PATH,args.FASTA_PATH,args.MASTER_META,args.SUBMITTED_SEQ,args.OUTPUT_DIR)
    try:
        prepAlpha.prepare_fasta()
        prepAlpha.prepare_metadata()
    except:
        raise


''' This program is responsible for
instantiating the GUI, connecting
to the microphone and speaker, and
managing the program flow '''

import intake, understander, processor, generator
import os

# tts
from gtts import gTTS
from io import BytesIO
import pygame
from pygame import mixer
from tempfile import TemporaryFile

# stt
import speech_recognition as sr

UNK = -1
COUNSELOR = 0
CLIENT = 1

class App():
	def __init__(self):
		# First, instantiate all the relevant objects
		self.client = Client()
		self.counselor = Counselor()
		self.conversation = Conversation(self.counselor, self.client)

class Conversation():
	def __init__(self, counselor, client):
		self.statements = []	# a list of statements
		self.counselor = counselor
		self.client = client

	def start(self):
		self.counselor.introduce(self.client)
		self.counselor.speak("Ok, " + self.client.name + ". Let's start by learning about who you are.")
		self.client.personality = intake.conduct_interview()
		self.counselor.speak("Great, thank you. This will be helpful as I listen to you.")
		self.counselor.speak("Why don't you tell me what's on your mind?")

		while True:
			# Get the user's response and parse it
			response = Statement(input("[YOU]\t\t"), self.client)

			# append it to the conversation
			self.statements.append(response)

			# select a microskill
			self.most_recent_statement().microskill_response = processor.pick_microskill(self)

			# generate it
			processor.test_all_microskills(self)
			#processor.generate_response(self.most_recent_statement().microskill_response, self)

	def most_recent_statement(self):
		return self.statements[len(self.statements) - 1]

class Statement():
	def __init__(self, raw_msg, speaker):
		self.speaker = speaker
		self.raw_msg = raw_msg	# each sentence in the statement
		self.parsed_msg = understander.parse_msg(self)	# parsed_msg is a nlp() object
		self.noun_phrases = [chunk.text for chunk in self.parsed_msg.noun_chunks]
		self.msg_emotions = understander.get_sentence_emotions(self.raw_msg)
		self.msg_topic = understander.get_topic(self.raw_msg, self.parsed_msg)
		self.msg_category = understander.get_category(self.raw_msg, self.parsed_msg)
		self.microskill_response = None

class Client():
	def __init__(self):
		self.name = ""
		self.personality = []	# a dict of trait-->value
		self.mood = {}			# a dict of descriptor-->value

	def is_client(self):
		return True

class Counselor():
	def __init__(self):
		self.persona = []		# the same as client personality
								# except more variable and
								# complementary to the client

	def is_client(self):
		return False

	def speak(self, sentence):
		print("[COUNSELOR]\t" + sentence)

		#tts = gTTS(sentence, lang="en")
		#mixer.init()

		#sf = TemporaryFile()
		#tts.write_to_fp(sf)
		#sf.seek(0)
		#mixer.music.load(sf)
		#mixer.music.play()

		#while mixer.music.get_busy():
		#	pygame.time.wait(100)

	def listen(self):
		print("[YOU]\t\t", end="")

		transcription = input("")# self.get_speech()

		#print(transcription)

		return transcription

	def introduce(self, client):
		self.speak("Hello. Welcome to the Virtual Counselor.")
		self.speak("What's your name?")
		client.name = self.listen()

	def get_speech(self):
		r = sr.Recognizer()
		mic = sr.Microphone()

		with mic as source:
			r.adjust_for_ambient_noise(source)
			audio = r.listen(source)

		return(r.recognize_google(audio))

if __name__ == "__main__":
	app = App()					# instantiate the app, client, counselor, and convo

	app.conversation.start()	# start the conversation


import subprocess
import re
import time
from shutil import move
from os import remove
from os import path
import json

import PBConfig

def get_git_version():
    installed_version = subprocess.getoutput(["git", "--version"])
    installed_version_parsed = re.findall("(\d+\.)(\d+\.)(\d)", str(installed_version))
    if len(installed_version_parsed) == 0 or len(installed_version_parsed[0]) == 0:
        return ""

    return installed_version_parsed[0]

def get_lfs_version():
    installed_version = subprocess.getoutput(["git-lfs", "--version"])
    installed_version_parsed = re.findall("(\d+\.)(\d+\.)(\d)", str(installed_version))
    if len(installed_version_parsed) == 0 or len(installed_version_parsed[0]) == 0:
        return ""

    # Index 0 is lfs version, other matched version is Go compiler version
    return installed_version_parsed[0]

# -2: Parse error
# -1: Old version
# 0: Expected version
# 1: Newer version
def compare_git_version(compared_version):
    installed_version = str(get_git_version()).split(".")
    if len(installed_version) != 3:
        return -2

    expected_version = str(compared_version).split(".")
    if len(expected_version) != 3:
        return -2

    if (int(installed_version[0]) == int(expected_version[0])) and (int(installed_version[1]) == int(expected_version[1])) and (int(installed_version[2]) == int(expected_version[2])):
        # Same version
        return 0

    # Not same version:
    if int(installed_version[0]) < int(expected_version[0]):
        return -1
    elif int(installed_version[1]) < int(expected_version[1]):
        return -1
    elif int(installed_version[2]) < int(expected_version[2]):
        return -1

    # Not older version:
    if int(installed_version[0]) > int(expected_version[0]):
        return 1
    elif int(installed_version[1]) > int(expected_version[1]):
        return 1
    elif int(installed_version[2]) > int(expected_version[2]):
        return 1

    # Something went wrong, return parse error
    return -2

# -2: Parse error
# -1: Old version
# 0: Expected version
# 1: Newer version
def compare_lfs_version(compared_version):
    installed_version = str(get_lfs_version()).split(".")
    if len(installed_version) != 3:
        return -2

    expected_version = str(compared_version).split(".")
    if len(installed_version) != 3:
        return -2

    if (int(installed_version[0]) == int(expected_version[0])) and (int(installed_version[1]) == int(expected_version[1])) and (int(installed_version[2]) == int(expected_version[2])):
        # Same version
        return 0

    # Not same version:
    if int(installed_version[0]) < int(expected_version[0]):
        return -1
    elif int(installed_version[1]) < int(expected_version[1]):
        return -1
    elif int(installed_version[2]) < int(expected_version[2]):
        return -1

    # Not older version:
    if int(installed_version[0]) > int(expected_version[0]):
        return 1
    elif int(installed_version[1]) > int(expected_version[1]):
        return 1
    elif int(installed_version[2]) > int(expected_version[2]):
        return 1

    # Something went wrong, return parse error
    return -2

def is_versionator_symbols_enabled():
    if not path.isfile(PBConfig.get('versionator_config_path')):
        # Config file somehow isn't generated yet, only get a response, but do not write anything into config
        response = input("Do you want to also download debugging symbols for accurate crash logging? You can change that choice later in .ue4v-user config file [y/n]")
        if response == "y" or response == "Y":
            return True
        else:
            return False

    try:
        with open(PBConfig.get('versionator_config_path'), "r") as config_file:
            for ln in config_file:
                if "Symbols" in ln or "symbols" in ln:
                    if "False" in ln or "false" in ln:
                        return False
                    elif "True" in ln or "true" in ln:
                        return True
                    else:
                        # Incorrect config
                        return False
    except:
        return False

    # Symbols configuration variable is not on the file, let's add it
    try:
        with open(PBConfig.get('versionator_config_path'), "a+") as config_file:
            response = input("Do you want to also download debugging symbols for accurate crash logging? You can change that choice later in .ue4v-user config file [y/n]")
            if response == "y" or response == "Y":
                config_file.write("\nsymbols = true")
                return True
            else:
                config_file.write("\nsymbols = false")
                return False
    except:
        return False

def get_project_version():
    try:
        with open(PBConfig.get('defaultgame_path'), "r") as ini_file:
            for ln in ini_file:
                if ln.startswith(PBConfig.get('defaultgame_version_key')):
                    return ln.replace(PBConfig.get('defaultgame_version_key'), '').rstrip()
    except:
        return None
    return None

def set_project_version(version_string):
    temp_path = "tmpProj.txt"
    # Create a temp file, do the changes there, and replace it with actual file
    try:
        with open(PBConfig.get('defaultgame_path'), "r") as ini_file:
            with open(temp_path, "wt") as fout:
                for ln in ini_file:
                    if PBConfig.get('defaultgame_version_key') in ln:
                        fout.write(	"ProjectVersion=" + version_string + "\n")
                    else:
                        fout.write(ln)
        remove(PBConfig.get('defaultgame_path'))
        move(temp_path, PBConfig.get('defaultgame_path'))
    except:
        return False
    return True

def set_engine_version(version_string):
    temp_path = "tmpEng.txt"
    try:
        # Create a temp file, do the changes there, and replace it with actual file
        with open(PBConfig.get('uproject_path'), "r") as uproject_file:
            with open(temp_path, "wt") as fout:
                for ln in uproject_file:
                    if PBConfig.get('uproject_version_key') in ln:
                        fout.write(	"\t\"EngineAssociation\": \"ue4v:" + version_string + "\",\n")
                    else:
                        fout.write(ln)
        remove(PBConfig.get('uproject_path'))
        move(temp_path, PBConfig.get('uproject_path'))
    except:
        return False
    return True

def project_version_increase(increase_type):
    increase_type = increase_type.lower()
    project_version = get_project_version()
    new_version = ""
    if project_version is None:
        return False

    # Split hotfix, stable and release versions into an array
    version_split = project_version.split('.')

    if len(version_split) != 3:
        print("Incorrect project version detected")
        return False

    if increase_type == "hotfix":
        new_version = version_split[0] + "." + version_split[1] + "." + str(int(version_split[2]) + 1)

    elif increase_type == "stable":
        new_version = version_split[0] + "." + str(int(version_split[1]) + 1) + ".0"

    elif increase_type == "public":
        new_version = str(int(version_split[2]) + 1) + ".0.0"

    else:
        return False

    print("Project version will be increased to " + new_version)
    return set_project_version(new_version)

def get_engine_version():
    try:
        with open(PBConfig.get('uproject_path'), "r") as uproject_file:
            data = json.load(uproject_file)
            engine_association = data[PBConfig.get('uproject_version_key')]
            build_version = engine_association[-8:]

            if "}" in build_version:
                # Means we're using local build version in .uproject file
                return None
            return build_version
    except:
        return None
    return None

def get_engine_version_with_prefix():
    engine_ver_number = get_engine_version()
    if engine_ver_number != None:
        return PBConfig.get('engine_version_prefix') + engine_ver_number

    return None

def get_engine_install_root():
    try:
        with open(PBConfig.get('versionator_config_path'), "r") as config_file:
            for ln in config_file:
                if "download_dir" in ln:
                    split_str = ln.split("=")
                    if len(split_str) == 2:
                        return split_str[1].strip()
    except:
        return None

def get_latest_available_engine_version(bucket_url):
    output = subprocess.getoutput(["gsutil", "ls", bucket_url])
    versions = re.findall("[4].[0-9]{2}-PB-[0-9]{8}", str(output))
    if len(versions) == 0:
        return None

    # Find the latest version by sorting
    versions.sort()
    return str(versions[len(versions) - 1])

def ddc_needs_regeneration():
    try:
        if path.isfile(PBConfig.get('ddc_version_path')):
            with open(PBConfig.get('ddc_version_path'), 'r') as ddc_version_file:
                current_version = ddc_version_file.readline(1)
                if int(current_version) < PBConfig.get('ddc_version'):
                    return True
                else:
                    return False
        else:
            # DDC is not runned yet on this system, or it's removed
            return True
    except:
        return False

def ddc_update_version():
    try:
        with open(PBConfig.get('ddc_version_path'), 'w') as ddc_version_file:
            ddc_version_file.write(str(PBConfig.get('ddc_version')))
            return True
    except:
        return False

# True: Error on last run, False: No errors
def check_error_state():
    try:
        with open(PBConfig.get('error_file'), 'r') as error_state_file:
            error_state = error_state_file.readline(1)
            if int(error_state) == 0:
                return False
            elif int(error_state) == 1:
                return True
            else:
                return False
    except:
        return False

# 1: Error happened on last run, 0: No errors
def pbsync_error_state(state):
    with open(PBConfig.get('error_file'), 'w') as error_state_file:
        if state:
            error_state_file.write("1")
        else:
            error_state_file.write("0")


from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

class TestWx():
    def setup(self):
        chromeOptions=Options()
        # 导入options
        # 添加参数
        chromeOptions.add_experimental_option("debuggerAddress","127.0.0.1:9222")
        self.driver = webdriver.Chrome(options=chromeOptions)
        # chrome --remote-debugging-port=9222 打开新的窗口
        self.driver.implicitly_wait(3)

    def test_tianjia(self):
        # self.driver.find_element(By.CSS_SELECTOR, ".index_service_cnt_itemWrap div:nth-child(1)").click()
        # 首页直接点击添加成员
        self.driver.find_element(By.ID, "menu_contacts").click()
        sleep(10)
        # 首页点击通讯录，添加显示等待
        element=(By.CSS_SELECTOR, ".js_has_member div:nth-child(1) .js_add_member")
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(element))
        self.driver.find_element(*element).click()
        self.driver.find_element(By.ID, "username").send_keys("abcd")
        self.driver.find_element(By.ID, "memberAdd_acctid").send_keys("1234567")
        self.driver.find_element(By.ID, "memberAdd_phone").send_keys("13512345677")
        self.driver.find_element(By.CSS_SELECTOR, ".js_btn_save").click()

    def teardown(self):
        sleep(5)
        self.driver.quit()


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import rethinkdb as r

class BoardgamegeekPipeline(object):
    def process_item(self, item, spider):
        r.connect().repl()
        data = dict()
        data.update(item)
        r.db('games').table('bgg').insert(data).run()
        return item


# coding: utf-8

from __future__ import absolute_import

from utils.meta import construct_meta
from science.consts import SCIENCE_CONSTS, subject_name2id

meta_info = {
    0: {
        'subject_name': '%s',
        'knowledge_table': '%s_knowledge',
        'origin_table': '%s_origin',
        'knowledge_tag_table': '%s_knowledge_tag',
        'solution_method_tag_table': '%s_solution_method_tag',
        'analysis_table': '%s_analysis',
        'solution_table': '%s_solution',
        'choice_table': '%s_choice',
        'blank_table': '%s_blank',
        'blank_question_table': '%s_blank_question',
        'response_table': '%s_response',
        'response_question_table': '%s_response_question',

        # 一级知识点包含的子知识点，出题过滤时用
        # subject_name, field: "module_id", 结果为子知识点 list
        'KNOWLEDGE_TREE_INDEX': '/alg/%s/knowledge_tree',
        # KNOWLEDGE_INV_INDEX 所有知识点，出题时使用
        # subject_name, field: "knowledge_id:qtype"
        'KNOWLEDGE_INV_INDEX': '/alg/%s/knowledge_index',

        # QUESTION_INV_INDEX 只有一级知识点，作为索引取题时使用
        # subject_name, field: "exam_kind:qtype:knowledge_id"
        'QUESTION_INV_INDEX': '/alg/%s/question_index'
    },

    1: {
        'USER_FACTOR_KEY': '/alg/%s/user_factor/%s'  # user_id
    },

    2: {
        # qtype, qid
        'QUESTION_FACTOR_KEY': '/alg/%s/question_factor/%s/%s'
    }
}

consts_info = {
    'K_CHOICE': 'K_%s_CHOICE',
    'K_CHOICE_ANALYSIS': 'K_%s_CHOICE_ANALYSIS',

    'K_BLANK': 'K_%s_BLANK',
    'K_BLANK_QUESTION': 'K_%s_BLANK_QUESTION',
    'K_BLANK_ANALYSIS': 'K_%s_BLANK_ANALYSIS',

    'K_RESPONSE': 'K_%s_RESPONSE',
    'K_RESPONSE_QUESTION': 'K_%s_RESPONSE_QUESTION',
    'K_RESPONSE_ANALYSIS': 'K_%s_RESPONSE_ANALYSIS',
}

kind_map_info = {
    'QUESTION_KIND_MAP': {
        'K_%s_CHOICE': 'K_%s_CHOICE',
        'K_%s_BLANK_QUESTION': 'K_%s_BLANK',
        'K_%s_RESPONSE_QUESTION': 'K_%s_RESPONSE'
    },
    'ANALYSIS_KIND_MAP': {
        'K_%s_CHOICE_ANALYSIS': 'K_%s_CHOICE',
        'K_%s_BLANK_ANALYSIS': 'K_%s_BLANK',
        'K_%s_RESPONSE_ANALYSIS': 'K_%s_RESPONSE'
    }
}

PhysicsMeta = construct_meta('PhysicsMeta', 'physics', SCIENCE_CONSTS,
        meta_info, consts_info, kind_map_info, subject_name2id)
ChemistryMeta = construct_meta('ChemistryMeta', 'chemistry', SCIENCE_CONSTS,
        meta_info, consts_info, kind_map_info, subject_name2id)
BiologyMeta = construct_meta('BiologyMeta', 'biology', SCIENCE_CONSTS,
        meta_info, consts_info, kind_map_info, subject_name2id)


import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

import os
import cv2
import numpy as np
import logging

# From  https://stackoverflow.com/questions/32504246/draw-ellipse-in-python-pil-with-line-thickness
def draw_circle(image, bounds, width=1, outline='white', antialias=4):
    """Improved ellipse drawing function, based on PIL.ImageDraw."""
    mask = Image.new(
        size=[int(dim * antialias) for dim in image.size],
        mode='L', color='black')
    draw = ImageDraw.Draw(mask)
    for offset, fill in (width/-2.0, 'white'), (width/2.0, 'black'):
        left, top = [(value + offset) * antialias for value in bounds[:2]]
        right, bottom = [(value - offset) * antialias for value in bounds[2:]]
        draw.ellipse([left, top, right, bottom], fill=fill)
    mask = mask.resize(image.size, Image.LANCZOS)
    image.paste(outline, mask=mask)

def draw_letter(letter):
    font = ImageFont.truetype("unicode.ttf", 75, encoding="unic")
    image=Image.new("RGBA", (200,200),(255,255,255,0))
    draw = ImageDraw.Draw(image)
    draw.text((70,45),letter,(0,0,0),font=font)
    draw = ImageDraw.Draw(image)
    image.save('temp.png')
    return image

def crop_circle(image):
    temp_img = cv2.imread('temp.png', 0)
    os.remove('temp.png')
    ret,thresh = cv2.threshold(temp_img,0,100,cv2.THRESH_BINARY_INV)
    _,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = contours[0]

    ### With Moments
    # M = cv2.moments(cnt)
    # cx = int(M['m10']/M['m00'])
    # cy = int(M['m01']/M['m00'])

    ### With Center
    x,y,w,h = cv2.boundingRect(cnt)
    cx = int(x+w/2)
    cy = int(y+h/2)
    radius = 45
    width=5
    padding = 10
    circle = [int(cx)-radius,int(cy)-radius,int(cx)+radius,int(cy)+radius]
    draw_circle(image,circle, outline =(0,0,0),width=width)
    bound = (int(cx)-radius-width-padding,int(cy)-radius-width-padding,int(cx)+radius+width+padding,int(cy)+radius+width+padding)
    return image.crop(bound)

def setColor(im,R=255,G=255,B=255):
    data = np.array(im)
    # Clear, make all black pixel
    for i,alpha in enumerate(data[:,:,3]) :
        for j,v in enumerate(alpha) :
            if v != 0 :
                data[:,:,0],  data[:,:,1], data[:,:,2] =  0, 0, 0
    red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
    mask = (red == 0) & (green == 0) & (blue == 0)
    data[:,:,:3][mask] = [R, G, B]
    return Image.fromarray(data)

def draw_word_in_circle(letter,path=''):
    letter = str(letter)
    file_name = letter + '.png'
    image = draw_letter(letter)
    image = crop_circle(image)
    image = setColor(image,190,190,190)
    image.save(path + file_name)

# Usage
for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-' :
    try :
        draw_word_in_circle(c,path='./icon/')
    except :
        logging.warning('Directory not found, try to build \"icon\"')
        os.mkdir('icon')
        draw_word_in_circle(c,path='./icon/')


# -*- coding: utf-8 -*-
#
# Copyright (c) 2020, Geoffrey M. Poore
# All rights reserved.
#
# Licensed under the BSD 3-Clause License:
# http://opensource.org/licenses/BSD-3-Clause
#

import argparse
import pathlib
import textwrap
from .version import __version__ as version
from .err import Text2qtiError
from .config import Config
from .quiz import Quiz
from .qti import QTI

def main():
    '''
    text2qti executable main function.
    '''
    parser = argparse.ArgumentParser(prog='text2qti')
    parser.set_defaults(func=lambda x: parser.print_help())
    parser.add_argument('--version', action='version', version=f'text2qti {version}')
    parser.add_argument('--latex-render-url',
                        help='URL for rendering LaTeX equations')
    parser.add_argument('file',
                        help='File to convert from text to QTI')
    args = parser.parse_args()

    config = Config()
    config.load()
    if not config.loaded_config_file:
        latex_render_url = input(textwrap.dedent('''\
            It looks like text2qti has not been installed on this machine
            before.  Would you like to set a default LaTeX rendering URL?  If
            no, press ENTER.  If yes, provide the URL and press ENTER.

            If you use Canvas, the URL will have the form
                https://<institution>.instructure.com/equation_images/
            with "<institution>" replaced with the name or abbreviation for
            your institution.  You can determine "<institution>" by logging
            into Canvas and then looking in the browser address bar for
            something like "<institution>.instructure.com/".

            LaTeX rendering URL:  '''))
        latex_render_url = latex_render_url.strip()
        if latex_render_url:
            config['latex_render_url'] = latex_render_url
            config.save()
    if args.latex_render_url is not None:
        config['latex_render_url'] = args.latex_render_url

    file_path = pathlib.Path(args.file)
    try:
        text = file_path.read_text(encoding='utf-8-sig')  # Handle BOM for Windows
    except FileNotFoundError:
        raise Text2qtiError(f'File "{file_path}" does not exit')
    except UnicodeDecodeError as e:
        raise Text2qtiError(f'File "{file_path}" was not encoded in valid UTF-8:\n{e}')

    quiz = Quiz(text, config=config)
    qti = QTI(quiz, config=config)
    qti.save(file_path.parent / f'{file_path.stem}.zip')


import os
import logging
import argparse
from pkg_resources import resource_filename
from functools import partial
from shutil import copytree, rmtree, copy
from pds_github_util.branches.git_actions import loop_checkout_on_branch
from pds_github_util.gh_pages.summary import write_build_summary
from pds_github_util.gh_pages.root_index import update_index

logger = logging.getLogger('github3')
logger.setLevel(level=logging.WARNING)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()

def copy_resources():
    logger.info("write static resources (img, config)...")
    resources = resource_filename(__name__, 'resources')
    for f in os.listdir(resources):
        i_p = os.path.join(resources, f)
        o_p = os.path.join(os.getcwd(), f)
        if os.path.isdir(i_p):
            if os.path.exists(o_p):
                rmtree(o_p, ignore_errors=True)
            copytree(i_p, o_p)
        else:
            if os.path.exists(o_p):
                os.remove(o_p)
            copy(i_p, os.getcwd())

def build_summaries(token, path=os.getcwd(), format='md'):
    copy_resources()

    herds = []
    # loop on stable releases
    for herd in loop_checkout_on_branch('NASA-PDS/pdsen-corral',
                            '[0-9]+\.[0-9]+',
                            partial(write_build_summary,
                                    root_dir=path,
                                    gitmodules='/tmp/pdsen-corral/.gitmodules',
                                    token=token,
                                    dev=False,
                                    format=format),
                            token=token,
                            local_git_tmp_dir='/tmp'):
        herds.append(herd)

    # dev release on master
    herd = next(loop_checkout_on_branch('NASA-PDS/pdsen-corral',
                            'master',
                            partial(write_build_summary,
                                    root_dir=path,
                                    gitmodules='/tmp/pdsen-corral/.gitmodules',
                                    token=token,
                                    dev=True,
                                    format=format),
                            token=token,
                            local_git_tmp_dir='/tmp'))

    herds.append(herd)

    update_index(path, herds)

def main():
    parser = argparse.ArgumentParser(description='Create new snapshot release')
    parser.add_argument('--token', dest='token',
                        help='github personal access token')
    parser.add_argument('--path', dest='path',
                        help='directory where the summary will be created')
    parser.add_argument('--format', dest='format',
                        help='format of the summary, accepted formats are md and rst')
    args = parser.parse_args()
    build_summaries(args.token, args.path, args.format)

if __name__ == "__main__":
    main()


"""This module will contain fixtures to test graph.py."""

import pytest
from ..depth_first import Graph

@pytest.fixture()
def graph_empty():
    g = Graph()
    return g

@pytest.fixture()
def graph_one():
    g = Graph()
    g.gdict = {
        'A': {'B': 10},
        'B': {'A': 5, 'D': 15, 'C': 20},
        'C': {'E': 1},
        'D': {'A': 5},
        'E': {'F': 2, 'B': 4},
        'F': {'D': 11}
    }
    return g

@pytest.fixture()
def graph_two():
    g = Graph()
    g.gdict = {
        'A': {'B': 10, 'C': 15},
        'B': {'D': 15, 'E': 5, 'C': 2},
        'C': {'F': 50, 'G': 25},
        'D': {},
        'E': {'C': 5},
        'F': {'E': 10},
        'G': {'F': 20}
    }
    return g

@pytest.fixture()
def graph_map():
    map = {
            'Pandora': {'Arendelle': 150, 'Metroville': 82},
            'Arendelle': {'Pandora': 150, 'Metroville': 99, 'New Monstropolis': 42},
            'Metroville': {'Pandora': 82, 'Arendelle': 99, 'New Monstropolis': 105, 'Naboo': 26, 'Narnia': 37},
            'New Monstropolis': {'Arendelle': 42, 'Metroville': 105, 'Naboo': 73},
            'Naboo': {'New Monstropolis': 73, 'Metroville': 26, 'Narnia': 250},
            'Narnia': {'Metroville': 37, 'Naboo': 250}
        }
    g = Graph(map)
    return g

@pytest.fixture()
def graph_three():
    dictionary = {
        'A': {'B': 0, 'D': 0},
        'B': {'A': 0, 'C': 0, 'D': 0},
        'C': {'B': 0, 'G': 0},
        'D': {'A': 0, 'B': 0, 'E': 0, 'H': 0, 'F': 0},
        'E': {'D': 0},
        'F': {'D': 0, 'H': 0},
        'G': {'C': 0},
        'H': {'F': 0, 'D': 0},
        }
    g = Graph(dictionary)
    return g


import time
from typing import List, Tuple,  Union

from bs4.element import Tag

from .models.Team import Team
from .models.Match import Match
from .utils.api import get_page

def get_matches() -> List[Match]:
    """
    Function that searches for the information of all matches.

    RETURN:
    ----------
    matches: List[Match]
    """
    soup = get_page('/matches')
    rounds_upcommingMatch: List[Tag] = soup.findAll(
        'div', {'class': 'upcomingMatch'})
    rounds_live: List[Tag] = soup.findAll(
        'div', {'class': 'liveMatch-container'})
    rounds = rounds_live + rounds_upcommingMatch

    matches: List[Match] = []
    for div in rounds:
        match = div.findAll('a', {'class': 'match a-reset'})[0]
        id_ = int(match['href'].split('/')[2])
        date_hour: Tag = match.findAll('div', {'class': 'matchTime'})[0]
        event: List[Tag] = match.findAll(
            'div', {'class': 'matchEventName gtSmartphone-only'})
        div_empty = match.findAll('div', {'class': 'matchInfoEmpty'})
        if (div_empty == []):
            team1, team2 = _get_teams(div)

            name_event = str(event[0].text) if event != [] else ''
            date_unix = date_hour.text
            if (date_hour.text != 'LIVE'):
                date_unix = _date_unix_to_timestamp(date_hour['data-unix'])

            match = Match(id_, team1, team2, name_event, date_unix)
            matches.append(match)

    return matches

def _get_teams(match: Tag) -> Tuple[Team, Team]:
    return _get_team(match, 0), _get_team(match, 1)

def _get_team(match: Tag, num_team: int) -> Team:
    """
    Function that seeks and treats the information of a team.

    PARAMS:
    ----------
    `match`: Match in which the team will be found.
    `info_teams_id`: BeautifulSoup list with team ID information.
    `num_team`: The team to be found (0 or 1).

    RETURN:
    ----------
    team: Team
    """
    team: Team
    divs_teams = match.findAll(
        'div', {'class': 'matchTeam'})
    div_team_empty = divs_teams[num_team].findAll(
        'div', {'class': 'team text-ellipsis'})
    if (div_team_empty != []):
        team = Team(div_team_empty[0].text)
    else:
        name = divs_teams[num_team].findAll(
            'div', {'class': 'matchTeamName text-ellipsis'})[0]
        div_id = divs_teams[num_team].findAll(
            'div', {'class': 'matchTeamScore'})
        img_logo_url = divs_teams[num_team].findAll(
            'img', {'class': 'matchTeamLogo'})

        id_team: Union[int, None] = None
        if (div_id == []):
            id_team = match['team{}'.format(num_team + 1)]
        else:
            span_id = div_id[0].findAll(
                'span', {'class': 'currentMapScore'})[0]
            id_team = int(span_id['data-livescore-team'])

        team = Team(name.text, id_team,
                    logo_url=_get_logo_url(img_logo_url))

    return team

def _get_logo_url(img_logo_url: List) -> str:
    """
    function that obtains the URL of a team's logo.

    PARAMS:
    ----------
    `img_logo_url`: URL logo that verifed and concated.

    RETURN:
    ----------
    `url_logo`: URL concated.
    """
    url_logo = ''
    if (img_logo_url != []):
        url_logo = img_logo_url[0]['src']
        if (url_logo.startswith('/img')):
            url_logo = 'https://www.hltv.org' + url_logo

    return url_logo

def _date_unix_to_timestamp(date_unix: str) -> str:
    """
    Function that convert unix date to human-readable date.

    PARAMS:
    ----------
    :param: date_unix: Date in format unix.

    RETURN:
    ----------
    date: str in format(dd b yyyy H:M:S +0000)
        - 08 Aug 2020 16:00:00 +0000
    """
    ts = int(date_unix)
    ts /= 1000
    date_hour = time.strftime(
        "%d %b %Y %H:%M:%S +0000", time.localtime(ts))
    return date_hour


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

"""Test Motor with callbacks and asyncio."""

import asyncio
import unittest

from pymongo.errors import DuplicateKeyError

from test.asyncio_tests import asyncio_test, AsyncIOTestCase

class TestAsyncIOCallbacks(AsyncIOTestCase):
    @asyncio_test
    def test_callbacks(self):
        future = asyncio.Future(loop=self.loop)

        def callback(res, error):
            if error:
                future.set_exception(error)
            else:
                future.set_result(res)

        yield from self.collection.remove()
        yield from self.collection.insert({'_id': 1})
        self.collection.find_one(callback=callback)
        result = yield from future
        self.assertEqual({'_id': 1}, result)

        # Reset the Future.
        future = asyncio.Future(loop=self.loop)
        self.collection.insert({'_id': 1}, callback=callback)
        with self.assertRaises(DuplicateKeyError):
            yield from future

if __name__ == '__main__':
    unittest.main()


from torch import nn
import torch

class DNet(nn.Module):
    def __init__(self):
        super(DNet, self).__init__()
        self.layers = nn.Sequential(
            nn.Conv2d(1,64,3,1,padding=1,bias=False),
            nn.BatchNorm2d(64),
            nn.LeakyReLU(),
            nn.Conv2d(64,128,3,2,padding=1,bias=False),
            nn.BatchNorm2d(128),
            nn.LeakyReLU(),
            nn.Conv2d(128,256,3,2,padding=1,bias=False),
            nn.BatchNorm2d(256),
            nn.LeakyReLU(),
            nn.Conv2d(256,512,3,2,bias=False),
            nn.BatchNorm2d(512),
            nn.LeakyReLU(),
            nn.Conv2d(512,1,3,1,bias=False)
        )
    def forward(self,x):
        h = self.layers(x)
        return h.reshape(-1)

class GNet(nn.Module):
    def __init__(self):
        super(GNet, self).__init__()
        self.layers = nn.Sequential(
            nn.ConvTranspose2d(128,512,3,1,0,bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(),
            nn.ConvTranspose2d(512, 256, 3, 2, 0, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(),
            nn.ConvTranspose2d(256, 128, 3, 2, 1,output_padding=1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.ConvTranspose2d(128, 64, 3, 2, 1,output_padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.ConvTranspose2d(64, 1, 3, 1, 1, bias=False),
            nn.Tanh()
        )
    def forward(self,x):
        return self.layers(x)

class DCGAN(nn.Module):
    def __init__(self):
        super(DCGAN, self).__init__()
        self.gnet = GNet().cuda()
        self.dnet = DNet().cuda()

        self.loss_func = nn.BCEWithLogitsLoss()
    def forward(self,noise):
        noise = noise.cuda()
        return self.gnet(noise)
    def get_D_loss(self,noise_d,real_img):
        noise_d = noise_d.cuda()
        real_img = real_img.cuda()
        real_y = self.dnet(real_img)
        g_img = self.gnet(noise_d)
        fake_y = self.dnet(g_img)

        real_tag = torch.ones(real_img.size(0)).cuda()
        fake_tag = torch.zeros(noise_d.size(0)).cuda()

        loss_real = self.loss_func(real_y,real_tag)
        loss_fake = self.loss_func(fake_y,fake_tag)
        loss_d = loss_real + loss_fake
        return loss_d

    def get_G_loss(self,noise_g):
        noise_g = noise_g.cuda()
        _g_img = self.gnet(noise_g)
        _real_y = self.dnet(_g_img)
        _real_tag = torch.ones(noise_g.size(0)).cuda()

        loss_g = self.loss_func(_real_y,_real_tag)
        return loss_g

if __name__ == '__main__':
    dnet = DNet()
    input = torch.randn(1,1,28,28)
    print(dnet(input).shape)

    gnet = GNet()
    x = torch.randn(1,128,1,1)
    print(gnet(x).shape)

    gan = DCGAN()
    x = torch.randn(1,128,1,1)

    d_loss = gan.get_D_loss(x,input)
    g_loss = gan.get_G_loss(x)
    print(d_loss,g_loss)


from multiprocessing import Manager
from Module_Tester.EX_db import db_make
from Module_Tester.EX_RUN_Module import *
from Module_Tester.EX_RUN_Module_FAST import *
from Module_Tester.DB_collector.DB_collector_Module import *
import argparse

FAST = True

class body:
    def __init__(self):
        # 초기 입력 인자 전달 -------------------------------------------------------------------- #
        parser = argparse.ArgumentParser(description='CNS 플랫폼_TE Ver')
        parser.add_argument('--comip', type=str, default='', required=False, help="현재 컴퓨터의 ip [default='']")
        parser.add_argument('--comport', type=int, default=7101, required=False, help="현재 컴퓨터의 port [default=7001]")
        parser.add_argument('--cnsip', type=str, default='192.168.0.93', required=False, help="현재 컴퓨터의 ip [default='']")
        parser.add_argument('--cnsport', type=int, default=7101, required=False, help="현재 컴퓨터의 port [default=7001]")
        self.args = parser.parse_args()
        print('=' * 25 + '초기입력 파라메터' + '=' * 25)

        with open('EX_pro.txt', 'w') as f:                             # 타기능에서 CNS 정보 확인 용
            f.write(f'{self.args.cnsip}\t{self.args.cnsport}')

        print(self.args)
        self.shared_mem = generate_mem().make_mem_structure()
        # ---------------------------------------------------------------------------------------- #
        if FAST:
            pro_list = [RUN_FREEZE_FAST(self.shared_mem, IP=self.args.comip, Port=self.args.comport),  # [1]
                        EX_module(self.shared_mem)  # [3]
                        ]
        else:
            pro_list = [RUN_FREEZE(self.shared_mem, IP=self.args.comip, Port=self.args.comport),    # [1]
                        EX_module(self.shared_mem)                                                  # [3]
                        ]

        self.process_list = pro_list

    def start(self):
        [_.start() for _ in self.process_list]
        [_.join() for _ in self.process_list]

class generate_mem:
    def make_clean_mem(self):
        memory_dict = {'Clean': True, 'Loop': False, 'Run': False}
        print('Clean 메모리 생성 완료')
        return memory_dict

    def make_main_mem_structure(self, max_len_deque=10):
        memory_dict = db_make().make_db_structure(max_len_deque)
        print('Main 메모리 생성 완료')
        return memory_dict

    def make_mem_structure(self):
        print('=' * 25 + '메모리 생성 시작' + '=' * 25)
        memory_list = [Manager().dict(self.make_main_mem_structure(max_len_deque=10)),    # [0]
                       Manager().list(),                                                  # [1]
                       Manager().dict(self.make_clean_mem()),                             # [-1]
                       ]
        print('=' * 25 + '메모리 생성 완료' + '=' * 25)
        return memory_list

if __name__ == '__main__':
    main_process = body()
    main_process.start()


from pygments.lexer import RegexLexer, words, include
from pygments.token import Text, Comment, Keyword, Name, String, Number, Punctuation, Operator

__all__ = ["ExtKLexer"]

class ExtKLexer(RegexLexer):

    name = "ExtK"
    aliases = ["extk"]

    tokens = {
        'whitespace': [
            (r'\s+', Text.Whitespace)
        ],
        'comments': [
            (r'/\*.*?\*/', Comment.Multiline),
            (r'//.*?\n', Comment.Single),
        ],
        'keywords': [
            (words(('kmod', 'endkm', 'including', 'subsort', 'eq', 'ceq', 'load', 'when'), suffix = r'\b'), Keyword),
            (words(('syntax', 'sort', 'op', 'rule'), suffix = r'\b'), Keyword.Declaration),
            (words(('If', 'then', 'else', 'Let', 'Do', "Return"), suffix = r'\b'), Keyword),
        ],
        'literals': [
            (r'"(\\\\|\\"|[^"])*"', String),
        ],
        'identifiers': [
            (r'[$@%.#]*[a-zA-Z_][a-zA-Z_0-9]*', Name.Variable),
            (r'</?[$@%.#]*[a-zA-Z_][a-zA-Z_0-9]*>', Name.Tag),
        ],
        'numbers': [
            (r'[+-]?[0-9]+', Number.Integer),
        ],
        'symbols': [
            (words(('[', ']', '{', '}', '(', ')')), Punctuation),
            (words(('BEGIN', 'END')), Keyword),
            (words((',', ';', ':', ':>', '=>', '::=', '|->', '=/=', '...')), Punctuation),
        ],
        'operators': [
            (words(('|', '=')), Operator),
        ],
        'root': [
            include('comments'),
            include('whitespace'),
            include('keywords'),
            include('literals'),
            include('numbers'),
            include('symbols'),
            include('operators'),
            include('identifiers'),
        ],
    }


import numpy as np
import random
from scipy.optimize import curve_fit
from GaitAnaylsisToolkit.LearningTools.Trainer import GMMTrainer
from GaitAnaylsisToolkit.Session import ViconGaitingTrial
from GaitAnaylsisToolkit.LearningTools.Runner import GMMRunner
from GaitCore.Core import Point
import matplotlib.pyplot as plt
import pickle
from dtw import dtw
from utilities import data, utilities
import numpy.polynomial.polynomial as poly

files = data.files
sides = data.sides
frames = data.frames
hills = utilities.get_index(frames, files, sides)

pathsZ, pathsY = utilities.make_toe(files, hills, sides)

def calculate_imitation_metric_1(demos, imitation):
    M = len(demos)
    T = len(imitation)
    imitation = np.array(imitation)
    metric = 0.0
    paths = []
    t = []
    t.append(1.0)
    alpha = 1.0
    manhattan_distance = lambda x, y: abs(x - y)

    for i in range(1, T):
        t.append(t[i - 1] - alpha * t[i - 1] * 0.01)  # Update of decay term (ds/dt=-alpha s) )
    t = np.array(t)

    for m in range(M):
        d, cost_matrix, acc_cost_matrix, path = dtw(imitation, demos[m], dist=manhattan_distance)
        data_warp = [demos[m][path[1]][:imitation.shape[0]]]
        coefs = poly.polyfit(t, data_warp[0], 20)
        ffit = poly.Polynomial(coefs)
        y_fit = ffit(t)
        paths.append(y_fit)
        metric += np.sum(abs(y_fit - imitation.flatten()))

    return paths, metric/(M*T)

# runner_toeZ = make_toeZ(filesZ, hillsZ, nb_states, "toeZ_all2")
# runner_toeY = make_toeY(filesY, hillsY, nb_states, "toeY_all")

# runnerY = GMMRunner.GMMRunner("toeY_all" + ".pickle")
# runnerZ = GMMRunner.GMMRunner("toeZ_all1" + ".pickle")
#
# imitationY = runnerY.run()
# imitationZ = runnerZ.run()

metricsZ = []
metricsY = []

for i in range(1, len(pathsZ)+1):

    sample_set = random.sample(range(len(pathsZ)), i)
    pathsZ_partial = []

    for k in sample_set:
        pathsZ_partial.append(pathsZ[k])

    trainerZ = GMMTrainer.GMMTrainer(pathsZ_partial, "toeZ_all" + str(i), 6, 0.01)
    trainerZ.train()
    runnerZ = GMMRunner.GMMRunner("toeZ_all" + str(i) + ".pickle")
    imitationZ = runnerZ.run()
    pathZ, metric = calculate_imitation_metric_1(pathsZ_partial, imitationZ)
    metricsZ.append([i, metric])

for i in range(1, len(pathsY)+1):

    sample_set = random.sample(range(len(pathsY)), i)
    pathsY_partial = []

    for k in sample_set:
        pathsY_partial.append(pathsY[k])

    trainerY = GMMTrainer.GMMTrainer(pathsY_partial, "toeY_all" + str(i), 10, 0.01)
    trainerY.train()
    runnerY = GMMRunner.GMMRunner("toeY_all" + str(i) + ".pickle")
    imitationY = runnerY.run()
    pathY, metric = calculate_imitation_metric_1(pathsY_partial, imitationY)
    metricsY.append([i, metric])

plt.rcParams.update({'font.size': 22})
fig0, ax0 = plt.subplots(nrows=1, ncols=1, sharex=True, figsize=(5, 3))
fig1, ax1 = plt.subplots(nrows=1, ncols=1, sharex=True, figsize=(5, 3))
fig2, ax2 = plt.subplots(nrows=1, ncols=1, figsize=(5, 3))
fig3, ax3 = plt.subplots(nrows=1, ncols=1, figsize=(5, 3))

metricsY = np.array(metricsY)
metricsZ = np.array(metricsZ)

ax0.set_xlim(0, 10)
ax1.set_xlim(0, 10)
ax0.plot(metricsY[:, 0], metricsY[:, 1], "-o")
ax1.plot(metricsZ[:, 0], metricsZ[:, 1], "-o")

# def func(x, a, b, c):
#     return a * np.exp(-b * x) + c

# poptY, pcovY = curve_fit(func, metricsY[:, 0], metricsY[:, 1], p0=(1, 4, 1))
# poptZ, pcovZ = curve_fit(func, metricsZ[:, 0], metricsZ[:, 1], p0=(1, 4, 1))
# ax0.plot(metricsY[:, 0], func(metricsY[:, 0], *poptY))
# ax1.plot(metricsZ[:, 0], func(metricsZ[:, 0], *poptZ))

ax0.set_title("Y Imitation cost")
ax1.set_title("Z Imitation cost")
ax0.set_xlabel("K")
ax1.set_xlabel("K")
ax0.set_ylabel("Cost")
ax1.set_ylabel("Cost")
ax0.grid()
ax1.grid()

for p in pathY:
    ax2.plot(p)

for p in pathZ:
    ax3.plot(p)

ax2.set_title("Y Imitation Comparison")
ax3.set_title("Z Imitation Comparison")
ax2.plot(imitationY, linewidth=3)
ax3.plot(imitationZ, linewidth=3)

ax2.set_title("Y Trajectories")
ax3.set_title("Z Trajectories")

ax2.legend(["1", "2", "3", "4", "5", "6", "7", "8", "Learned"])
ax3.legend(["1", "2", "3", "4", "5", "6", "7", "8", "Learned"])

ax2.set_xlabel("Frame")
ax2.set_ylabel("Position (mm)")
ax3.set_xlabel("Frame")
ax3.set_ylabel("Position (mm)")

plt.show()


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

import sys
import os
import math
import pygame

if not sys.argv[0]:
    appdir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
else:
    appdir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), os.pardir))
if not appdir in sys.path:
    sys.path.insert(0, appdir)

import tmxreader
import helperspygame

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

def main():
    """
    Main method.
    """
    args = sys.argv[1:]
    if len(args) < 1:
        path_to_map = os.path.join(os.path.dirname(__file__), os.pardir, "data", "001-1.tmx")
        print(("usage: python %s your_map.tmx\n\nUsing default map '%s'\n" % \
            (os.path.basename(__file__), path_to_map)))
    else:
        path_to_map = args[0]

    demo_pygame(path_to_map)

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

def demo_pygame(file_name):
    """
    Example showing how to use the paralax scrolling feature.
    """

    # parser the map (it is done here to initialize the
    # window the same size as the map if it is small enough)
    world_map = tmxreader.TileMapParser().parse_decode(file_name)

    # init pygame and set up a screen
    pygame.init()
    pygame.display.set_caption("tiledtmxloader - " + file_name + \
                                                        " - keys: arrows, 0-9")
    screen_width = min(1024, world_map.pixel_width)
    screen_height = min(768, world_map.pixel_height)
    screen = pygame.display.set_mode((screen_width, screen_height))

    # load the images using pygame
    resources = helperspygame.ResourceLoaderPygame()
    resources.load(world_map)

    # prepare map rendering
    assert world_map.orientation == "orthogonal"

    # renderer
    renderer = helperspygame.RendererPygame()

    # create hero sprite
    # use floats for hero position
    hero_pos_x = screen_width
    hero_pos_y = screen_height
    hero = create_hero(hero_pos_x, hero_pos_y)

    # dimensions of the hero for collision detection
    hero_width = hero.rect.width
    hero_height = 5

    # cam_offset is for scrolling
    cam_world_pos_x = hero.rect.centerx
    cam_world_pos_y = hero.rect.centery

    # set initial cam position and size
    renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \
                                        screen_width, screen_height)

    # retrieve the layers
    sprite_layers = helperspygame.get_layers_from_map(resources)

    # filter layers
    sprite_layers = [layer for layer in sprite_layers if not layer.is_object_group]

    # add the hero the the right layer, it can be changed using 0-9 keys
    sprite_layers[1].add_sprite(hero)

    # layer add/remove hero keys
    num_keys = [pygame.K_0, pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, \
                    pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9]

    # variables for the main loop
    clock = pygame.time.Clock()
    running = True
    speed = 0.075
    # set up timer for fps printing
    pygame.time.set_timer(pygame.USEREVENT, 1000)

    # mainloop
    while running:
        dt = clock.tick()

        # event handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.USEREVENT:
                print("fps: ", clock.get_fps())
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                elif event.key in num_keys:
                    # find out which layer to manipulate
                    idx = num_keys.index(event.key)
                    # make sure this layer exists
                    if idx < len(world_map.layers):
                        if sprite_layers[idx].contains_sprite(hero):
                            sprite_layers[idx].remove_sprite(hero)
                            print("removed hero sprite from layer", idx)
                        else:
                            sprite_layers[idx].add_sprite(hero)
                            print("added hero sprite to layer", idx)
                    else:
                        print("no such layer or more than 10 layers: " + str(idx))

        # find directions
        direction_x = pygame.key.get_pressed()[pygame.K_RIGHT] - \
                                        pygame.key.get_pressed()[pygame.K_LEFT]
        direction_y = pygame.key.get_pressed()[pygame.K_DOWN] - \
                                        pygame.key.get_pressed()[pygame.K_UP]

        # make sure the hero moves with same speed in all directions (diagonal!)
        dir_len = math.hypot(direction_x, direction_y)
        dir_len = dir_len if dir_len else 1.0

        # update position
        step_x = speed * dt * direction_x / dir_len
        step_y = speed * dt * direction_y / dir_len
        step_x, step_y = check_collision(hero_pos_x, hero_pos_y, step_x, step_y, hero_width, hero_height, sprite_layers[3])
        hero_pos_x += step_x
        hero_pos_y += step_y
        hero.rect.midbottom = (hero_pos_x, hero_pos_y)

        # adjust camera according to the hero's position, follow him
        # (don't make the hero follow the cam, maybe later you want different
        #  objects to be followd by the cam)
        renderer.set_camera_position(hero.rect.centerx, hero.rect.centery)

        # clear screen, might be left out if every pixel is redrawn anyway
        screen.fill((0, 0, 0))

        # render the map
        for sprite_layer in sprite_layers:
            if sprite_layer.is_object_group:
                # we dont draw the object group layers
                # you should filter them out if not needed
                continue
            else:
                renderer.render_layer(screen, sprite_layer)

        pygame.display.flip()

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

def create_hero(start_pos_x, start_pos_y):
    """
    Creates the hero sprite.
    """
    image = pygame.Surface((25, 45), pygame.SRCALPHA)
    image.fill((255, 0, 0, 200))
    rect = image.get_rect()
    rect.midbottom = (start_pos_x, start_pos_y)
    return helperspygame.SpriteLayer.Sprite(image, rect)

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

# unused in this demo, just here to show how you could check for collision!
def is_walkable(pos_x, pos_y, coll_layer):
    """
    Just checks if a position in world coordinates is walkable.
    """
    tile_x = int(pos_x // coll_layer.tilewidth)
    tile_y = int(pos_y // coll_layer.tileheight)

    if coll_layer.content2D[tile_y][tile_x] is None:
        return True
    return False

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

def check_collision(hero_pos_x, hero_pos_y, step_x, step_y, \
                                    hero_width, hero_height, coll_layer):
    """
    Checks collision of the hero against the world. Its not the best way to
    handle collision detection but for this demo it is good enough.

    :Returns: steps to add to heros current position.
    """
    # create hero rect
    hero_rect = pygame.Rect(0, 0, hero_width, hero_height)
    hero_rect.midbottom = (hero_pos_x, hero_pos_y)

    # find the tile location of the hero
    tile_x = int((hero_pos_x) // coll_layer.tilewidth)
    tile_y = int((hero_pos_y) // coll_layer.tileheight)

    # find the tiles around the hero and extract their rects for collision
    tile_rects = []
    for diry in (-1, 0 , 1):
        for dirx in (-1, 0, 1):
            if coll_layer.content2D[tile_y + diry][tile_x + dirx] is not None:
                tile_rects.append(coll_layer.content2D[tile_y + diry][tile_x + dirx].rect)

    # save the original steps and return them if not canceled
    res_step_x = step_x
    res_step_y = step_y

    # x direction, floor or ceil depending on the sign of the step
    step_x = special_round(step_x)

    # detect a collision and dont move in x direction if colliding
    if hero_rect.move(step_x, 0).collidelist(tile_rects) > -1:
        res_step_x = 0

    # y direction, floor or ceil depending on the sign of the step
    step_y = special_round(step_y)

    # detect a collision and dont move in y direction if colliding
    if hero_rect.move(0, step_y).collidelist(tile_rects) > -1:
        res_step_y = 0

    # return the step the hero should do
    return res_step_x, res_step_y

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

def special_round(value):
    """
    For negative numbers it returns the value floored,
    for positive numbers it returns the value ceiled.
    """
    # same as:  math.copysign(math.ceil(abs(x)), x)
    # OR:
    # ## versus this, which could save many function calls
    # import math
    # ceil_or_floor = { True : math.ceil, False : math.floor, }
    # # usage
    # x = floor_or_ceil[val<0.0](val)

    if value < 0:
        return math.floor(value)
    return math.ceil(value)

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

if __name__ == '__main__':
    main()


import asyncio

async def process_data(data):
    print(f"Processing {data}...")
    await asyncio.sleep(1)  # 데이터 처리하는 동안 1초 대기
    result = data * 2
    print(f"Processed {data}, result={result}")
    return result

async def main():
    # 비동기적으로 데이터를 처리합니다.
    tasks = [asyncio.create_task(process_data(data)) for data in range(1, 6)]
    # 처리 결과를 기다립니다.
    results = await asyncio.gather(*tasks)
    print(f"Results: {results}")

asyncio.run(main())


import logging

from sqlalchemy import Column, ForeignKey, String, orm, Boolean, event
from sqlalchemy.orm import relationship
from sqlalchemy_utils import UUIDType

from walkoff import executiondb
from walkoff.appgateway import get_condition
from walkoff.appgateway.apiutil import split_api_params, get_condition_api, UnknownApp, InvalidArgument, \
    UnknownCondition
from walkoff.appgateway.validator import validate_condition_parameters
from walkoff.events import WalkoffEvent
from walkoff.executiondb.argument import Argument
from walkoff.executiondb.executionelement import ExecutionElement
from walkoff.helpers import ExecutionError, format_exception_message

logger = logging.getLogger(__name__)

class Condition(ExecutionElement, executiondb.Execution_Base):
    __tablename__ = 'condition'
    conditional_expression_id = Column(UUIDType(binary=False),
                                       ForeignKey('conditional_expression.id', ondelete='CASCADE'))
    app_name = Column(String(80), nullable=False)
    action_name = Column(String(80), nullable=False)
    is_negated = Column(Boolean, default=False)
    arguments = relationship('Argument', cascade='all, delete, delete-orphan', passive_deletes=True)
    transforms = relationship('Transform', cascade='all, delete-orphan', passive_deletes=True)
    children = ('arguments', 'transforms')

    def __init__(self, app_name, action_name, id=None, is_negated=False, arguments=None, transforms=None, errors=None):
        """Initializes a new Condition object.

        Args:
            app_name (str): The name of the app which contains this condition
            action_name (str): The action name for the Condition. Defaults to an empty string.
            id (str|UUID, optional): Optional UUID to pass into the Condition. Must be UUID object or valid UUID string.
                Defaults to None.
            is_negated (bool, optional): Should the result of the condition be inverted? Defaults to False.
            arguments (list[Argument], optional): Dictionary of Argument keys to Argument values.
                This dictionary will be converted to a dictionary of str:Argument. Defaults to None.
            transforms(list[Transform], optional): A list of Transform objects for the Condition object.
                Defaults to None.
        """
        ExecutionElement.__init__(self, id, errors)
        self.app_name = app_name
        self.action_name = action_name
        self.is_negated = is_negated

        self.arguments = []
        if arguments:
            self.arguments = arguments

        self.transforms = []
        if transforms:
            self.transforms = transforms

        self._data_param_name = None
        self._api = None
        self._condition_executable = None

        self.validate()

    @orm.reconstructor
    def init_on_load(self):
        """Loads all necessary fields upon Condition being loaded from database"""
        if not self.errors:
            errors = []
            try:
                self._data_param_name, run, self._api = get_condition_api(self.app_name, self.action_name)
                get_condition(self.app_name, run)
            except UnknownApp:
                errors.append('Unknown app {}'.format(self.app_name))
            except UnknownCondition:
                errors.append('Unknown condition {}'.format(self.action_name))
            self.errors = errors

    def validate(self):
        """Validates the object"""
        errors = []
        try:
            self._data_param_name, run, self._api = get_condition_api(self.app_name, self.action_name)
            self._condition_executable = get_condition(self.app_name, run)
            tmp_api = split_api_params(self._api, self._data_param_name)
            validate_condition_parameters(tmp_api, self.arguments, self.action_name)
        except UnknownApp:
            errors.append('Unknown app {}'.format(self.app_name))
        except UnknownCondition:
            errors.append('Unknown condition {}'.format(self.action_name))
        except InvalidArgument as e:
            errors.extend(e.errors)
        self.errors = errors

    def execute(self, action_execution_strategy, data_in, accumulator):
        """Executes the Condition object, determining if the Condition evaluates to True or False.

        Args:
            action_execution_strategy: The strategy used to execute the action (e.g. LocalActionExecutionStrategy)
            data_in (dict): The input to the Transform objects associated with this Condition.
            accumulator (dict): The accumulated data from previous Actions.

        Returns:
            (bool): True if the Condition evaluated to True, False otherwise
        """
        data = data_in

        for transform in self.transforms:
            data = transform.execute(action_execution_strategy, data, accumulator)
        try:
            arguments = self.__update_arguments_with_data(data)
            args = validate_condition_parameters(self._api, arguments, self.action_name, accumulator=accumulator)
        except InvalidArgument as e:
            logger.error('Condition {0} has invalid input {1} which was converted to {2}. Error: {3}. '
                         'Returning False'.format(self.action_name, data_in, data, format_exception_message(e)))
            WalkoffEvent.CommonWorkflowSignal.send(self, event=WalkoffEvent.ConditionError)
            return False

        try:
            logger.debug('Arguments passed to condition {} are valid'.format(self.id))
            ret = action_execution_strategy.execute(self, accumulator, args)
            WalkoffEvent.CommonWorkflowSignal.send(self, event=WalkoffEvent.ConditionSuccess)
            if self.is_negated:
                return not ret
            else:
                return ret

        except ExecutionError:
            logger.exception(
                'Error encountered executing condition {0} with arguments {1} and value {2}: Returning False'.format(
                    self.action_name, arguments, data))
            WalkoffEvent.CommonWorkflowSignal.send(self, event=WalkoffEvent.ConditionError)
            return False

    def __update_arguments_with_data(self, data):
        arguments = []
        for argument in self.arguments:
            if argument.name != self._data_param_name:
                arguments.append(argument)
        arguments.append(Argument(self._data_param_name, value=data))
        return arguments

@event.listens_for(Condition, 'before_update')
def validate_before_update(mapper, connection, target):
    target.validate()


from flask import Flask,render_template,redirect ,request
from mysqlconnection import connectToMySQL
app = Flask(__name__)

##****************************Main Page ***********************************
@app.route('/')
def startup():
    return redirect("/users")

@app.route('/users')
def main():
    print("test")
    mysql=connectToMySQL("users")
    users= mysql.query_db("SELECT * FROM users;")
    return render_template("user.html", all_users=users)
@app.route('/users/new')
def newuser():
    return render_template("new_user.html")

##************************************CREATE USER ****************************
@app.route('/users/new/input', methods=["POST"])
def input():
    query = "INSERT INTO users (first_name,last_name,email,created_at,updated_at) VALUES (%(fname)s,%(lname)s,%(email)s,NOW(),NOW());"
    data = {
        "fname": request.form["f_name"],
        "lname": request.form["l_name"],
        "email": request.form["email"]
    }
    mysql=connectToMySQL("users")
    id = mysql.query_db(query,data)
    print(id)
    id = str(id)
    return redirect ("/users/"+id)

#*****************************SHOW USER *************************************
@app.route('/users/<id>')
def show(id):
    mysql=connectToMySQL("users")
    query = "SELECT* from users where user_id=%(id)s;"
    data = {
        "id" : id
    }
    data = mysql.query_db(query,data)
    person = data[0]
    return render_template("user_info.html", id=id, one_users = person)
##********************************EDIT PAGE *************************************
@app.route('/edit/<id>')
def edit(id):
    mysql=connectToMySQL("users")
    return render_template("edit_user.html", id=id)

@app.route('/edit/users/<id>', methods=['POST'])
def editusers(id):
    mysql=connectToMySQL("users")
    query = "Update users SET first_name=%(f_name)s, last_name=%(l_name)s, email=%(email)s where user_id=%(id)s;"
    data = {
        "f_name": request.form["f_name"],
        "l_name": request.form["l_name"],
        "email": request.form["email"],
        "id": id
    }
    mysql.query_db(query,data)
    return redirect ("/users", id=id)
#**********************************Delete **************************************
@app.route('/delete/<id>')
def delete():
    mysql=connectToMySQL("users")
    query = "Delete from users where user_id=%(id)s;"
    data = {
        "id" : id
    }
    mysql.query_db(query,data)
    return redirect("/users")

#***********************************End*************************************
if __name__ == "__main__":
    app.run(debug=True)


# encoding: utf-8

import json
import sys
import os

if len(sys.argv) < 2:
    sys.exit('Usage: python filter.py <file>')

if not os.path.exists(sys.argv[1]):
    sys.exit('Error: File %s not found' % sys.argv[1])

with open(sys.argv[1], 'r') as f:
    content = f.readlines()
    for line in content:
        try:
            fulltweet = json.loads(line)
            username = fulltweet['user']['screen_name']\
                .encode(encoding='UTF-8')
            name = fulltweet['user']['name'].encode(encoding='UTF-8')
            print("\n" + str(username) + " (" + str(name) + ")")
            print(fulltweet.get('created_at'))
            print(fulltweet.get('text').encode('utf-8'))
        except:
            pass


"""
  datasources_factory.py

  returns various data sources to be used
"""

import torch
from torchvision import datasets, transforms
import os

def datasources_factory(dataset_name, dataset_params):
     """
         creates one of a few implementations of a data source. we return None if we are unable to create
         the requested data source
     """

     if dataset_name == 'font-1':

         data_transforms = {
             'train': transforms.Compose([
                 transforms.Resize(size=(224, 224)),
                 transforms.ToTensor(),
                 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
             ]),
             'val': transforms.Compose([
                 transforms.Resize(size=(224, 224)),
                 transforms.ToTensor(),
                 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
             ]),
         }

         print("Initializing Datasets and Dataloaders...")

         data_type = dataset_params.get('data_type', ['train','val'])
         # Create training and validation datasets
         image_datasets = {x: datasets.ImageFolder(os.path.join(dataset_params['root_dir'], x), data_transforms[x]) for x in data_type}
         # Create training and validation dataloaders
         dataloaders_dict = {
         x: torch.utils.data.DataLoader(image_datasets[x], batch_size=dataset_params['batch_size'], shuffle=True, num_workers=4) for x in data_type}

         return dataloaders_dict

     return None


import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import torchvision
import glob
import os
import numpy as np
from PIL import Image
#[1]
# Creating a custom dataset
class hw2DataSet(Dataset):
    def __init__(self, root, transform=None):
        """ Intialize the hw2_train_val dataset """
        self.filenames = []
        self.root = root
        self.transform = transform
        self.classes = {} #dict

        # read filenames
        self.img_filenames = sorted(glob.glob(os.path.join(root, '*.jpg')))
        digit_len = len(str(len(self.img_filenames)))
        label_filenames = []

        for img_fn in self.img_filenames:
            label_filenames.append(img_fn.split('/')[-1][:-4]+'.txt')
        #[label_filenames.append(("%0"+str(digit_len)+"d.txt") % i) for i in range(len(self.img_filenames))]
        self.label_filenames = label_filenames
        self.len = len(self.img_filenames)

        # define classes
        classes_dict = {}
        classes_list = ['plane', 'ship', 'storage-tank', 'baseball-diamond',
         'tennis-court', 'basketball-court', 'ground-track-field',
         'harbor', 'bridge', 'small-vehicle', 'large-vehicle', 'helicopter',
         'roundabout', 'soccer-ball-field', 'swimming-pool', 'container-crane']
        for index,class_ in zip(range(16),classes_list):
            classes_dict[class_]=index
        self.classes = classes_dict
        self.classesL = classes_list

    def __getitem__(self, index):
        """ Get a sample from the dataset """
        (image_fn, label_fn) = self.img_filenames[index], self.label_filenames[index]
        """ INPUT: image part """
        image = Image.open(image_fn).convert('RGB')
        image = image.resize((448,448))
        if self.transform is not None:
            image = self.transform(image)

        return image, label_fn

    def __len__(self):
        """ Total number of samples in the dataset """
        return self.len

    def decoder(self,pred,filter_lowScore):
        bboxes = []
        image_size = 512
        patch_num = 7
        patch_size = image_size/patch_num
        pred = pred.to('cpu')

        for m_ in range(patch_num): #patch(m_,n_)
            for n_ in range(patch_num):
                pred_26 = pred[m_,n_,:].numpy() #extract
                preclass_index = np.argmax(pred_26[10:])
                for n in range(2): #two box in one patch
                    bbox_conf_score = pred_26[n*5+4]
                    max_class_score = pred_26[10+preclass_index]
                    class_spec_conf_score = bbox_conf_score*max_class_score
                    if (class_spec_conf_score >= filter_lowScore):
                        width, height = pred_26[n*5+2], pred_26[n*5+3]
                        dx, dy = pred_26[n*5], pred_26[n*5+1]
                        x1 = max(0, int(patch_size*(n_+dx)-image_size*width/2))
                        x2 = min(image_size, int(patch_size*(n_+dx)+image_size*width/2))
                        y1 = max(0, int(patch_size*(m_+dy)-image_size*height/2))
                        y2 = min(image_size, int(patch_size*(m_+dy)+image_size*height/2))
                        bboxes.append([x1,y1,x2,y2,bbox_conf_score,preclass_index,class_spec_conf_score])
        return np.array(bboxes)


import librosa
import matplotlib.pyplot as plt

y1, sr1 = librosa.load('../data/orange_uk.wav')
y2, sr2 = librosa.load('../data/orange_ustra.wav')

plt.subplot(1, 2, 1)
mfcc1 = librosa.feature.mfcc(y1, sr1)
plt.imshow(mfcc1)

plt.subplot(1, 2, 2)
mfcc2 = librosa.feature.mfcc(y2, sr2)
plt.imshow(mfcc2)

plt.figure()
from dtw import dtw
from numpy.linalg import norm
dist, cost, acc_cost, path = dtw(mfcc1.T, mfcc2.T, dist=lambda x, y: norm(x - y, ord=1))
print('Normalized distance between the two sounds:', dist)

plt.imshow(cost.T, origin='lower', interpolation='nearest')
plt.plot(path[0], path[1], 'w')
plt.xlim((-0.5, cost.shape[0]-0.5))
plt.ylim((-0.5, cost.shape[1]-0.5))

print(dist)
print(cost)
print(acc_cost)
print(path)

plt.show()


from io import StringIO
from django.core.management import call_command
from django.test import TestCase, RequestFactory
from user_accounts.models import User, Profile
from unittest.mock import patch, create_autospec
from kyc.geth_node_client import GethNodeClient
from web3 import Web3
from kyc.models import WhitelistLog

class AddVerifiedUsersToWhitelistCommandTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
            id='65888f1a-faa4-4385-8973-1d1eabe22836',
            email='user1@example.com')
        self.profile = Profile(
            user_id=self.user.id,
            first_name='John',
            last_name='Doe',
            address_1='23 Somewhere St',
            suburb='Burleigh Heads',
            postcode='4220',
            state='QLD',
            country='AUS',
            eth_address='0x10594B019de165B051c2dD99ED484DC9DAA20005',
            verified=True
        )
        self.profile.save()

    @patch("kyc.management.commands.add_verified_users_to_whitelist.get_geth_node_client", autospec=True)  # noqa: E501
    def test_add_one_verified_user(self, get_geth_node_client_mock):
        # assert preconditions
        self.assertEqual(
            0,
            WhitelistLog.objects.all().count()
        )

        web3_mock = create_autospec(Web3())
        geth_node_client_mock = create_autospec(GethNodeClient(web3_mock))
        get_geth_node_client_mock.return_value = geth_node_client_mock
        whitelist_contract_address = '0x27db42bd2434944f3c175609d07250d75c542371'  # noqa: E501
        # run process
        out = StringIO()
        call_command(
            'add_verified_users_to_whitelist',
            whitelist_contract_address,
            stdout=out
        )
        # assert results
        self.assertIn(
            f'{self.profile.eth_address} was added to {whitelist_contract_address}',  # noqa: E501
            out.getvalue()
        )
        self.assertEqual(
            1,
            WhitelistLog.objects.all().count()
        )
        whitelist_log = WhitelistLog.objects.first()
        self.assertEqual(
            WhitelistLog.ACTION_ADD,
            whitelist_log.action
        )

    @patch("kyc.management.commands.add_verified_users_to_whitelist.get_geth_node_client", autospec=True)  # noqa: E501
    def test_skip_one_verified_user_already_added(
            self,
            get_geth_node_client_mock):

        web3_mock = create_autospec(Web3())
        geth_node_client_mock = create_autospec(GethNodeClient(web3_mock))
        geth_node_client_mock.is_on_whitelist.return_value = True
        get_geth_node_client_mock.return_value = geth_node_client_mock

        whitelist_contract_address = '0x27db42bd2434944f3c175609d07250d75c542371'  # noqa: E501

        WhitelistLog(
            user=self.user,
            contract_address=whitelist_contract_address,
            eth_address=self.profile.eth_address,
            action=WhitelistLog.ACTION_ADD
        ).save()
        # run process
        out = StringIO()
        call_command(
            'add_verified_users_to_whitelist',
            whitelist_contract_address,
            stdout=out
        )
        # assert results
        self.assertIn(
            f'{self.profile.eth_address} is already added to {whitelist_contract_address}. Nothing to do.',  # noqa: E501
            out.getvalue()
        )
        self.assertEqual(
            1,
            WhitelistLog.objects.all().count()
        )
        whitelist_log = WhitelistLog.objects.first()
        self.assertEqual(
            WhitelistLog.ACTION_ADD,
            whitelist_log.action
        )


import sys, time, os
import argparse
from PD_Amino_Acid_dict import * # import AA convertion dictionary
import re

# function to check stability of delta delta G value
def stability_check(data):

	# list for stability categories
	stab_list = ['highly de-stabilizing', 'de-stabilizing', 'slightly de-stabilizing',
	'neutral',
	'slightly stabilizing', 'stabilizing', 'highly stabilizing']

	# stability criterion
	if (data > 1.84):
		return(stab_list[0]) # highly de-stabilizing
	elif (data <= 1.84) & (data > 0.92):
		return(stab_list[1]) # de-stabilizing
	elif (data <= 0.92) & (data > 0.46):
		return(stab_list[2]) # slightly de-stabilizing
	elif (data <= 0.46) & (data > -0.46):
		return(stab_list[3]) # neutral
	elif (data <= -0.46) & (data > -0.92):
		return(stab_list[4]) # slightly stabilizing
	elif (data <= -0.92) & (data > -1.84):
		return(stab_list[5]) # stabilizing
	else:
		return(stab_list[6]) # highly stabilizing

# function to make tabular info for each mutation with info like Position, delta delta G value, stability
def foldx_process(delta_G_file, mutant_file, repair_pdb_name):

	delta_G_list = [] # list to store delta delta G value
	delta_G_stab = [] # list to store category
	repair_pdb_with_no_extention = repair_pdb_name.split('.')[0] # model names common term

	with open(delta_G_file, 'r') as f_in_delta_G: #open avg delta delta G file
		for line in f_in_delta_G:
			line = line.strip('\n')
			if re.search(repair_pdb_with_no_extention, line): # check only models lines
				line = line.split('\t')
				mut_id_AA = line[0].split('_')[-1] # model ID
				delta_G = line[2]	# delta delta G value
				delta_G_list.append(delta_G) # update list

				# check stability of delta delta G
				stability = stability_check(float(delta_G))
				delta_G_stab.append(stability) # update list

	mut_list = [] # list to store mutation info

	with open(mutant_file, 'r') as f_in_mut: # open mutation file
		for line in f_in_mut:
			# mutation as in file containing original AA, PDB chain, position and replaced AA
			mutation = line.strip('\n').split(';')[0]
			mut_pos = mutation[2:-1] # mutation position

			# original AA,position and replaced AA and position separately
			mut_info = mutation[0] + mutation[2:] + '\t' + mut_pos
			mut_list.append(mut_info) # append the list

	# output data
	output_data = ''
	for item in range(len(delta_G_list)):
		output_data += mut_list[item] + '\t' + delta_G_list[item] + '\t' + delta_G_stab[item] + '\n'

	return(output_data)

def main( ):
	start_time = time.time() # our scripts start time
	input_file = args_.input_argument
	output_file = args_.output_argument

	# root dicrectory containing all AA folders
	input_dir_root = input_file[0]

	# repair PDB file
	repair_pdb_file = input_file[1]
	repair_pdb_info = os.path.split(repair_pdb_file)
	repair_pdb_name = repair_pdb_info[1]

	output_txt = 'Mutation' + '\t' + 'Position'+ '\t' + 'Delta_Delta_G' + '\t' + 'Feature' + '\n'

	# iterate for all AA folders
	for res in one_letter.keys():
		if res == 'UNK':continue
		substitite_res = one_letter[res]
		res_dir = os.path.join(input_dir_root, res) # current AA folder

		# file containing mutations for current AA folder
		mutant_file = ('').join([res_dir, '/', 'individual_list_', substitite_res, '.txt'])

		# FoldX models directory
		foldx_dir = ('').join([res_dir, '/', 'foldx_data'])

		# avg delta dela G file
		avg_delta_G_file = ('').join([foldx_dir,'/','Average_',repair_pdb_name])

		# call foldx_process for current AA
		foldx_result = foldx_process(avg_delta_G_file, mutant_file, repair_pdb_name)

		# append the result
		output_txt += foldx_result

	# write output to a file
	with open(output_file[0], 'w') as f_out:
		f_out.write(output_txt)

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description="A script to make make table for foldx energy")
	parser.add_argument('-i', nargs='+', dest="input_argument",default="something.txt", help="something file")
	parser.add_argument('-o', nargs='+', dest="output_argument",default="something_.txt", help="something file")
	args_ = parser.parse_args()
	main(  )
	print('done')


import openpyxl

def tel_mody(tel):
    tel = tel.replace(' ','')
    tel = tel.replace('-', '')
    tel = tel.replace('(', '')
    tel = tel.replace(')', '')
    if len(tel) == 12 : tel = '+7' + tel[2:]
    if len(tel) == 11: tel = '+7' + tel[1:]
    if len(tel) > 12: tel = ' --- ' + tel + ' --- '
    return tel

# читаем excel-файл
wb = openpyxl.load_workbook('30 набор контакты.xlsx')

# получаем активный лист
sheet = wb.active
my_file = open("import-rodit2.csv", "w", encoding='utf-8')

str_csv = 'Name,' +\
          'Given Name,' \
          'Family Name,' \
          'Group Membership,' \
          'E-mail 1 - Type, E-mail 1 - Value,' \
          'Phone 1 - Value,' \
          'Organization 1 - Name,' \
          'Organization 1 - Title,' \
          'Custom Field 1 - Type,' \
          'Custom Field 1 - Value,' \
          'Custom Field 2 - Type,' \
          'Custom Field 2 - Value,' \
          'Custom Field 3 - Type,' \
          'Custom Field 3 - Value,' \
          'Custom Field 4 - Type,' \
          'Custom Field 4 - Value,'\
          'Custom Field 5 - Type,' \
          'Custom Field 5 - Value,' \
          'Custom Field 6 - Type,' \
          'Custom Field 6 - Value,' \
          'Custom Field 7 - Type,' \
          'Custom Field 7 - Value \n'
my_file.write(str_csv)

rows = sheet.max_row
cols = sheet.max_column
for i in range(3, rows + 1):
    fio = str(sheet.cell(row=i, column=8).value).split()
    tel = str(sheet.cell(row=i, column=9).value)
    mail = str(sheet.cell(row=i, column=10).value)
    rodfio1 = str(sheet.cell(row=i, column=1).value)
    rodtel1 = str(sheet.cell(row=i, column=2).value)
    mail1 = str(sheet.cell(row=i, column=3).value)
    rodfio2 = str(sheet.cell(row=i, column=5).value)
    rodtel2 = str(sheet.cell(row=i, column=6).value)
    mail2 = str(sheet.cell(row=i, column=7).value)
    metro = str(sheet.cell(row=i, column=11).value)

    print(fio)
    if fio[0] == 'None':
        continue
    if len(mail) == 4  :
        mail = ','
    else:
        mail = '*,' + mail

    all_str = fio[0] + ' ' + fio[1] + ',' + \
              fio[0] + ',' + \
              fio[1] + ',' + \
              'ДГ ::: * myContacts,' + \
              mail + ',' + \
              tel_mody(tel) + ',' + \
              'ДГ' +  ',' + \
              'Родитель' + ',' + \
              'Ребенок' + ',' + rodfio1 + ',' + \
              'Телефон Ребенка' + ',' + tel_mody(rodtel1) + ',' + \
              'Email Ребенка' + ',' + mail1 + ',' + \
              'Родитель 1' + ',' + rodfio2 + ',' + \
              'Телефон Р1' + ',' + tel_mody(rodtel2) + ',' + \
              'Email Р1' + ',' + mail2 + ',' + \
              'Метро' + ',' + metro + '\n'
    all_str = all_str.replace('None', '')
    print(all_str)
    my_file.write(all_str)

my_file.close()


import requests
from bs4 import BeautifulSoup as bs
from hangul import recompile_hangul

## HTTP GET Request

i=2020

def crawl(soup):
    brand_list = []

    for data in soup.find_all('div',{'class':'li_inner'}):

    # 1~100위 추출
        brand = data.find('img').get('alt')
        brand_list.append(recompile_hangul(brand))

    f = open(f'rank{i-1}.csv','w', encoding='utf-8')

    f.write(f'rank{i-1}\n')
    for text in brand_list:
        f.write(text+'\n')

## HTML 소스 가져오기
req2019 = requests.get('https://store.musinsa.com/app/usr/brand_rank?d_cat_cd=&u_cat_cd=&range=1y&sex=&popup=&page=1&sort=best')
req2018 = requests.get('https://web.archive.org/web/20181204051916/https://store.musinsa.com/app/usr/brand_rank')

html2019 = req2019.text
soup2019 = bs(html2019, 'html.parser')
crawl(soup2019)

i-=1
html2018 = req2018.text
soup2018 = bs(html2018, 'html.parser')
crawl(soup2018)
i-=1
req2017 = requests.get('https://web.archive.org/web/20171228091726/https://store.musinsa.com/app/usr/brand_rank')
html2017 = req2017.text
soup2017 = bs(html2017, 'html.parser')
crawl(soup2017)