#Here is the flowchart: https://drive.google.com/file/d/134gBSn2lonyUi0JtRUANsTwld2XO6LVm/view?usp=sharing

import random
from day7_hangman_game_art import stages, logo
from day7_hangman_game_words import word_list

print(logo)

chosen_word = random.choice(word_list)
len_chosen_word = len(chosen_word)
lives = 6 # keeps track of the number of lives.

# Create blanks - For each letter in the chosen_word, add a "_" to 'display'.
display = []
for _ in range(len_chosen_word):
  display += "_"
print(display)

# Let the user guess again. The loop stops once the user has guessed all the letters in the chosen_word and 'display' has no more blanks ("_").
end_of_game = False

while not end_of_game :
    guess = input("Choose a letter: ").lower()

    if guess in display:
        print(f"The letter \"{guess}\" has already been used! Try another letter")

    # Check guessed letter - If the letter at that position matches 'guess' then reveal that letter in the display at that position.
    for pos in range(len_chosen_word):
        letter = chosen_word[pos]
        if letter == guess:
            display[pos] = letter
    # Check if user is wrong - If guess is not a letter in the chosen_word, Then reduce 'lives' by 1.
    if guess not in chosen_word:
        print(f"Sorry, the letter \"{guess}\" is not in this word! You have {lives - 1} lives!")
        lives -= 1
        if lives == 0:
            end_of_game = True
            print("you lose! :(")
            print(f"The word is: {chosen_word}")

    #Join all the elements in the list and turn it into a String.
    print(f"{' '.join(display)}")

    #Check if user has got all letters.
    if "_" not in display:
        end_of_game = True
        print("You win!")

    # print the ASCII art from 'stages' that corresponds to the current number of 'lives' the user has remaining.
    print(stages[lives])


# %%
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import datetime
# %%
df = pd.read_csv('/Users/hungyanyi/Documents/GitHub/predict_drought/data/test_timeseries.csv',parse_dates=['date'])
print(df.info())
# %%
print(df.isnull().sum())

# %%
# number of regions
df['fips'].nunique()

# %%

df.info()

# find out if any multicolinearity

# T2M highly collerated (corr > 0.7) with QV2M,T2MDEW,T2MWET, T2M_MAX, T2M_MIN, and TS

# WS10M highly collerated  with W10M_MAX, WS10M_MIN, WS50M, WS550M_MAX, WS550M_MIN

# columns left: PRECTOT, PS, T2M, T2M_RANGE, WS10M, WS50M_RANGE

# plt.figure(figsize=(20,20))
# sns.heatmap(df.drop(columns=['fips','date']).corr(),annot=True)

# %%
df['date'].head()

# %%

new_df = df[['fips','date', 'PRECTOT', 'PS', 'T2M', 'T2M_RANGE', 'WS10M', 'WS50M_RANGE','score']]
#new_df = df

# %%

# proving that the dought score was released every Tuesday, reviewing the dought condtion in the past week

print(new_df[new_df['date'].dt.weekday == 1]['score'].isnull().sum())

# %%

# filter out row at the date that is after the last Tuesday of 2020 and row at the first Tuesday.

new_df = new_df[(df['date'] > pd.Timestamp(2019,1,1)) & (df['date'] < pd.Timestamp(2020,12,30))]

# %%

new_df
# %%

# get the mean of data per week
week_df = new_df.groupby('fips').resample('W-TUE', on='date', label='right',closed='right').mean()

week_df = week_df.drop(columns=['fips']).reset_index()

# %%
# plt hist of drought
week_df['score'].plot.hist(bins=50)
plt.title('Histogram of level of drought')
plt.xlabel('Class')

# %%

# check for week dataframe having any null score
week_df[week_df['score'].isnull()]

# %%

# round score column of week dataframe and make bar plot
week_df['score'] = week_df['score'].round().astype(int)
week_df['score'].plot.hist(bins=50)
plt.title('Histogram of level of drought after rounding')
plt.xlabel('Class')
plt.xticks(rotation=0)

# %%
week_df.to_csv('/Users/hungyanyi/Documents/GitHub/predict_drought/data/week_score.csv', index=False)

plt.figure(figsize=(20,20))

sns.heatmap(week_df.corr(),annot=True)
# %%

week_df['score'].value_counts().plot.bar()

# %%

# %%
df.head()
# %%
df.shape
# %%


from django.utils.html import escape

from sscr import texts

import os

with open('template/template.html', 'rb') as f:
    TemplateData = f.read()

with open('template/s2d.js', 'rb') as f:
    s2dData = f.read()

class PageGenerator:
    def __init__(self, title=''):
        self.title = title
        self.body = ''

    def addP(self, text, doEscape=True, doFormat=False):
        if doEscape:
            text = escape(text)

        if doFormat:
            text = texts.formatText(text)

        if type(text) == unicode:
            text = text.encode('utf-8')

        try:
            self.body += '
%s
' % text

        except UnicodeDecodeError:
            self.body += '
%s
' % text.decode('utf-8')

    def addH1(self, h1, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h1))

    def addH2(self, h2, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h2))

    def addH3(self, h3, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h3))

    def addH4(self, h4, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h4))

    def addH5(self, h5, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h5))

    def addH6(self, h6, center=False):
        self.body += '
%s
' % (' align="center"' if center else '', escape(h6))

    def addImage(self, filename, width='', height='', align='center'):
        with open(filename, 'rb') as f:
            data = f.read()

        self.body += '
'
        self.body += '<image src="data:image/png;base64,%s"' % data.encode('base64').replace('\n', '')
        if width: self.body += ' width="%s"' % width
        if height: self.body += ' height="%s"' % height

        self.body += '></image>
'

    def addHr(self):
        self.body += '
'

    def addRaw(self, data):
        self.body += data

    def addPlot(self, fn):
        divId = 'plot-%s' % os.urandom(8).encode('hex')

        data = '''

''' % locals()

        self.body += data

    def addPlotExt(self, fn, derivative='', closed=''):
        divId = 'plot-%s' % os.urandom(8).encode('hex')

        data = '''

''' % locals()

        self.body += data

    def addPlotMult(self, plots):
        divId = 'plot-%s' % os.urandom(8).encode('hex')

        fndata = []

        for plot in plots:
            fn = plot['fn']
            derivative = plot.get('derivative', '')
            closed = plot.get('closed', '')

            if derivative:
                fndata.append('''{fn: '%(fn)s', derivative: {
                                  fn: '%(derivative)s', updateOnMouseMove: true},
                                  closed: '%(closed)s'}''' % locals())

            else:
                fndata.append('''{fn: '%(fn)s', closed: '%(closed)s'}''' % locals())

        fndata = ','.join(fndata)

        data = '''

''' % locals()

        self.body += data

    def output(self):
        return TemplateData % {'title': self.title, 'body': self.body, 's2dData': s2dData}


from django.shortcuts import render

def home(request):
    import requests
    import json
    #Price API
    price_request = requests.get("https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,BCH,LTC,XLM,ADA,USDT,MIOTA,TRX,EOS,XRP&tsyms=CAD")
    price = json.loads(price_request.content)

    #News API
    api_request = requests.get("https://min-api.cryptocompare.com/data/v2/news/?lang=EN")
    api = json.loads(api_request.content)

    return render(request, 'home.html', {'api': api, 'price': price})

def prices(request):
    if request.method == "POST":
        import requests
        import json
        quote = request.POST['quote']
        quote = quote.upper()
        crypto_request = requests.get("https://min-api.cryptocompare.com/data/pricemultifull?fsyms=" + quote + "&tsyms=CAD")
        crypto = json.loads(crypto_request.content)
        return render(request, 'prices.html', {'quote': quote, 'crypto':crypto})
    else:
        notfound = "Enter a crypto symbol into the search bar"
        return render(request, 'prices.html', {'notfound': notfound})


import json
import tweepy
import logging

def tokens(tokens_file):
    # Get Twitter twitter_tokens
    try:
        f = open(tokens_file, mode='r')
        twitter_tokens = json.load(f)
        f.close()
    except FileNotFoundError as err:
        raise err
    except IOError as err:
        raise err

    return twitter_tokens

def auth(t_tokens):
    # tokens can be found under "Apps > RainBowDashBOT"
    try:
        t_auth = tweepy.OAuthHandler(t_tokens['consumer_api_key'],
                                     t_tokens['consumer_api_key_secret'])

        t_auth.set_access_token(t_tokens['access_token'],
                                t_tokens['access_token_secret'])

        api = tweepy.API(t_auth)

    except Exception as err:
        raise err

    return api

def tweet(api, tweet):
    # post the tweet on twitter
    status = 0

    try:
        if isinstance(tweet, str):
            orig_tweet = api.update_status(status=tweet)
        else:
            # multi-line message - create twitter thread
            orig_tweet = api.update_status(status=tweet[0])
            for i in tweet[1:]:
                reply_tweet = api.update_status(status=i,
                                                in_reply_to_status_id=orig_tweet.id,
                                                auto_populate_reply_metadata=True)
                orig_tweet = reply_tweet

    except tweepy.error.TweepError as err:
        status = 1
        raise err

    return status


from utility.utility import *
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation

class Visualizer:
    def __init__(self, data):
        # 数据处理

        # stroke_data 指的是表示画笔移动的原始数据，用于机器学习
        stroke_data_raw = np.array(data, dtype=np.ndarray)
        # local 是去除画笔移动向量的其余向量，同时也忽略第一个向量
        stroke_data_local = np.empty([0, 0])
        flag = 0
        for item in stroke_data_raw[1:, :]:
            if flag == 0:
                stroke_data_local = np.append(stroke_data_local, item[:2])
            else:
                flag = 0
            if item[2] == 1:
                flag = 1
        stroke_data_local = stroke_data_local.reshape(-1, 2)

        # sketch_data 指的是在累加后，转换为在画板上的xy轴坐标，仅用于可视化
        sketch_data = np.transpose(np.vstack((stroke_data_raw[:, 0],
                                              -stroke_data_raw[:, 1])))  # 加负号上下颠倒，用transpose恢复到一列x，一列y
        sketch_data = np.cumsum(sketch_data, axis=0, dtype=float)  # 按行累加，得到
        # 向量前两个值为移动方向，最后一个值lift为是否提笔，1说明该向量的下一个向量只移动画笔，不作图
        sketch_lift = stroke_data_raw[:, 2]
        # 以每次提笔作为分割，得到代表绘画顺序的三维列表
        sketch_data_sequential = []
        values = np.empty([0, 0])
        for count, value in enumerate(sketch_lift):
            values = np.append(values, sketch_data[count])
            if value == 1:
                values = values.reshape(-1, 2)
                sketch_data_sequential.append(values)
                values = np.empty([0, 0])

        # 将值传入self
        self.sketch_data = sketch_data
        self.sketch_lift = sketch_lift
        self.sketch_data_sequential = sketch_data_sequential
        self.stroke_data_raw = stroke_data_raw
        self.stroke_data_local = stroke_data_local

        self.ims = []  # 每一帧需要plot的artistic集合

    def xy_plotter(self):
        """绘制特征在平面上的分布"""
        fig, ax = plt.subplots(figsize=(10, 10), dpi=80)
        # ax.scatter(self.stroke_data_raw[:, 0], self.stroke_data_raw[:, 1],
        #            label='With idle movement', s=10, c='blue', marker='o')
        ax.scatter(self.stroke_data_local[:, 0], self.stroke_data_local[:, 1],
                   label='Only local stroke', s=10, c='red', marker='o')
        plt.legend(loc='lower right', fontsize=10)  # 标签位置
        plt.show()

    def visualizer_stroke(self):
        """可视化笔触"""
        fig, ax = plt.subplots(figsize=(3, 3), dpi=80)
        # 设置绘图范围
        plot_range = get_plot_range(self.sketch_data)
        ax.set_xlim(plot_range[0, :])
        ax.set_ylim(plot_range[1, :])
        # 关闭坐标轴显示
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])

        for count, data in enumerate(self.sketch_data_sequential):
            for k in range(data.shape[0]):
                # for i in range(count):
                #     ax.plot(self.stroke_data_sequential[i][:, 0],
                #             self.stroke_data_sequential[i][:, 1],
                #             linewidth='1', color='black', linestyle='-', marker='.')
                ax.plot(data[0:(k + 1), 0], data[0:(k + 1), 1],
                        linewidth='1', color='black', linestyle='-', marker='.')
                plt.pause(0.01)  # 暂停

        plt.show()

    def visualizer_stroke_gif(self):
        """可视化笔触，并生成gif动图"""
        fig, ax = plt.subplots(figsize=(3, 3), dpi=80)
        # 设置绘图范围
        plot_range = get_plot_range(self.sketch_data)
        ax.set_xlim(plot_range[0, :])
        ax.set_ylim(plot_range[1, :])
        # 关闭坐标轴显示
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])

        for count, data in enumerate(self.sketch_data_sequential):
            for k in range(data.shape[0]):
                im = []
                for i in range(count):
                    im.append(ax.plot(self.sketch_data_sequential[i][:, 0],
                                      self.sketch_data_sequential[i][:, 1],
                                      linewidth='1', color='black', linestyle='-', marker='.')[0])
                im.append(ax.plot(data[0:(k + 1), 0], data[0:(k + 1), 1],
                                  linewidth='1', color='black', linestyle='-', marker='.')[0])
                self.ims.append(im)
                plt.pause(0.01)  # 暂停

        ani = ArtistAnimation(fig, self.ims, interval=50, repeat_delay=500, repeat=True, blit=True)
        ani.save(filename="figure/test.gif", writer='pillow')
        plt.show()


from typing import Tuple
import torch
import os
import numpy as np
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer, AutoTokenizer, PreTrainedTokenizer
from datasets import load_metric, DatasetDict

from .utils.dataset import load_sdg_dataset

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    metric = load_metric("accuracy")
    return metric.compute(predictions=predictions, references=labels)

def to_binary(dataset: DatasetDict, label_to_extract: int) -> DatasetDict:
    def map_function(line):
        label = line["label"]
        if label == label_to_extract:
            label = 1
        else:
            label = 0
        line["label"] = label
        return line

    return {
        "train": dataset["train"].map(map_function),
        "test": dataset["test"].map(map_function)
    }

def train_17_classifiers(save_directory: str, n_epochs: int):
    base_model = "bert-base-cased"
    tokenizer = AutoTokenizer.from_pretrained(base_model)

    os.makedirs(save_directory, exist_ok=True)
    labels = [
        "SDG1: No Poverty",
        "SDG2: Zero Hunger",
        "SDG3: Good Health and Well-being",
        "SDG4: Quality Education",
        "SDG5: Gender Equality",
        "SDG6: Clean Water and Sanitation",
        "SDG7: Affordable and Clean Energy",
        "SDG8: Decent Work and Economic Growth",
        "SDG9: Industry, Innovation and Infrastructure",
        "SDG10: Reduced Inequality",
        "SDG11: Sustainable Cities and Communities",
        "SDG12: Responsible Consumption and Production",
        "SDG13: Climate Action",
        "SDG14: Life Below Water",
        "SDG15: Life on Land",
        "SDG16: Peace and Justice Strong Institutions",
        "SDG17: Partnerships to achieve the Goal"
    ]
    for i, label in enumerate(labels):
        dataset = load_sdg_dataset(tokenizer)
        model_dataset = to_binary(dataset, i + 1)
        model = AutoModelForSequenceClassification.from_pretrained(base_model, num_labels=2, id2label={0: "None", 1: label}, device=-1)
        training_args = TrainingArguments(
            output_dir="test_trainer",
            evaluation_strategy="epoch",
            num_train_epochs=n_epochs
        )

        trainer = Trainer(
            model=model,
            args=training_args,
            train_dataset=model_dataset["train"],
            eval_dataset=model_dataset["test"],
            compute_metrics=compute_metrics
        )

        trainer.train()
        model.save_pretrained(os.path.join(save_directory, f"model_sdg_{i}"))

def fine_tune_bert(save_location: str, n_epochs=3) -> Tuple[torch.nn.Module, PreTrainedTokenizer]:
    tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
    dataset = load_sdg_dataset(tokenizer)
    id2label = {
        0: "Easter egg",
        1: "SDG1: No Poverty",
        2: "SDG2: Zero Hunger",
        3: "SDG3: Good Health and Well-being",
        4: "SDG4: Quality Education",
        5: "SDG5: Gender Equality",
        6: "SDG6: Clean Water and Sanitation",
        7: "SDG7: Affordable and Clean Energy",
        8: "SDG8: Decent Work and Economic Growth",
        9: "SDG9: Industry, Innovation and Infrastructure",
        10: "SDG10: Reduced Inequality",
        11: "SDG11: Sustainable Cities and Communities",
        12: "SDG12: Responsible Consumption and Production",
        13: "SDG13: Climate Action",
        14: "SDG14: Life Below Water",
        15: "SDG15: Life on Land",
        16: "SDG16: Peace and Justice Strong Institutions",
        17: "SDG17: Partnerships to achieve the Goal"
    }
    model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=len(id2label), id2label=id2label)
    training_args = TrainingArguments(
        output_dir="test_trainer",
        evaluation_strategy="epoch",
        num_train_epochs=n_epochs
    )

    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=dataset["train"],
        eval_dataset=dataset["test"],
        compute_metrics=compute_metrics
    )

    trainer.train()
    model.save_pretrained(save_location)
    return model, tokenizer


import numpy as np
from collections import Counter

def process_text_file(fileName):
	identifier = []
	sex = []
	population = []
	nucleobases = []
	with open(fileName) as f:
		for line in f:
			line_list_form = line.split(' ')
			identifier.append(line_list_form.pop(0))
			sex.append(line_list_form.pop(0))
			population.append(line_list_form.pop(0))
			line_list_form[-1] = line_list_form[-1][:-1]
			#remove the non-genetic information
			#print(line_list_form)
			nucleobases.append(line_list_form) #add it to a list of lists that we can convert to np.array
			#print(len(nucleobases))
			#break

	nucleobases_array = np.array(nucleobases)

	return identifier, sex, population, nucleobases_array

def most_common_element_list(lst):
	c = Counter(lst)
	#print(c.most_common(1))
	element, count = c.most_common(1)[0]
	return element

def convert_array_to_binary(nucleobases):
	#get most common element in each column and store as list of elements
	most_common_list = []
	for j in range(nucleobases.shape[1]):
		list_form_curr_column = nucleobases[:, j].tolist()
		most_common_list.append(most_common_element_list(list_form_curr_column))

	#print(len(most_common_list))

	binary_array = np.zeros_like(nucleobases)
	for j in range(nucleobases.shape[1]): #we want to go down each column and replace the value and I think this is how you do it
		most_common_curr = most_common_list[j]
		for i in range(nucleobases.shape[0]):
			if nucleobases[i][j] != most_common_curr:
				binary_array[i][j] = 1
			else:
				binary_array[i][j] = 0

	#print(binary_array)
	#print(binary_array.shape)
	return binary_array

from sklearn.decomposition import PCA

def run_PCA(nucleobases, comp_number):
	pca = PCA(comp_number)
	dim_reduced_nucleobases = pca.fit_transform(nucleobases)
	#print(dim_reduced_nucleobases.shape)
	return dim_reduced_nucleobases

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import warnings
from collections import defaultdict
import matplotlib.cm as cm

#1B

def make_plot_PCA(nucleobases_PCA, population_tags, file_name): #assumes that the number of components is 2
	#print(population_tags)

	population_tags_dict = defaultdict(int)
	for popIndex in range(len(population_tags)):
		population_tags_dict[population_tags[popIndex]] = popIndex

	inv_dict = {v: k for k, v in population_tags_dict.items()}
	population_tags_int = [population_tags_dict[x] for x in population_tags]
	colors = ['red', 'green', 'blue', 'purple', 'yellow', 'orange', 'brown', 'cyan']
	cdict = {}
	unique_pop_tags_int = list(set(population_tags_int))
	print(len(unique_pop_tags_int))
	for pop in range(len(unique_pop_tags_int)): #assign each group a color
		cdict[unique_pop_tags_int[pop]] = colors[pop]

	v1_components = np.array(nucleobases_PCA[:, 0].tolist())
	v2_components = np.array(nucleobases_PCA[:, 1].tolist())
	populations = np.array(population_tags_int)

	fig, ax = plt.subplots()
	for g in np.unique(populations):
		ix = np.where(populations == g)
		ax.scatter(v1_components[ix], v2_components[ix], c = cdict[g], label = inv_dict[g])
	ax.legend()
	ax.set_title("PCA Analysis of 1000 Genomes Project, Part 1B")
	ax.set_xlabel('v1')
	ax.set_ylabel('v2')
	#plt.show()
	plt.savefig(file_name + ".png", format = 'png')
	plt.close()

# identifiers, sexes, population_tag, nucleobases = process_text_file('p4dataset2018.txt')
# nucleobases_binary = convert_array_to_binary(nucleobases)

#1B
# principal_components = run_PCA(nucleobases_binary, 2)
# make_plot_PCA(principal_components, population_tag, "1b")

#1DDDDDDDD
def make_plot_PCA_1D(nucleobases_PCA, population_tags, file_name): #assumes that the number of components is 3
	#print(population_tags)

	population_tags_dict = defaultdict(int)
	for popIndex in range(len(population_tags)):
		population_tags_dict[population_tags[popIndex]] = popIndex

	inv_dict = {v: k for k, v in population_tags_dict.items()}
	population_tags_int = [population_tags_dict[x] for x in population_tags]
	colors = ['red', 'green', 'blue', 'purple', 'yellow', 'orange', 'brown', 'cyan']
	cdict = {}
	unique_pop_tags_int = list(set(population_tags_int))
	print(len(unique_pop_tags_int))
	for pop in range(len(unique_pop_tags_int)): #assign each group a color
		cdict[unique_pop_tags_int[pop]] = colors[pop]

	v1_components = np.array(nucleobases_PCA[:, 0].tolist())
	v2_components = np.array(nucleobases_PCA[:, 1].tolist())
	v3_components = np.array(nucleobases_PCA[:, 2].tolist())
	populations = np.array(population_tags_int)

	fig, ax = plt.subplots()
	for g in np.unique(populations):
		ix = np.where(populations == g)
		ax.scatter(v1_components[ix], v3_components[ix], c = cdict[g], label = inv_dict[g])
	ax.legend()
	ax.set_title("PCA Analysis of 1000 Genomes Project, Part 1D")
	ax.set_xlabel('v1')
	ax.set_ylabel('v3')
	#plt.show()
	plt.savefig(file_name + ".png", format = 'png')
	plt.close()

#1DDDD
# principal_components = run_PCA(nucleobases_binary, 3)
# make_plot_PCA_1D(principal_components, sexes, "1d_sex")

#1FFFFF
def run_PCA_get_v(nucleobases, comp_number, interesting_v):
	pca = PCA(comp_number)
	dim_reduced_nucleobases = pca.fit_transform(nucleobases)
	v_vector = pca.components_[interesting_v - 1:]
	#print(v_vector.shape)

	return v_vector

import math

def make_plot_1F(nucleobases_size, interesting_vector, file_name):
	interesting_vector = list(interesting_vector.T)
	interesting_vector = [abs(x) for x in interesting_vector]
	size_list = range(0, nucleobases_size)
	plt.plot(size_list, interesting_vector)
	plt.xlabel('Nucleobase Index')
	plt.ylabel('Absolute Value of v3')
	plt.title('Nucleobases Analysis with PCA, Part 1F')
	#plt.show()
	plt.savefig(file_name + ".png", format = 'png')
	plt.close()

# interesting_vector = run_PCA_get_v(nucleobases_binary, 3, 3)
# make_plot_1F(10101, interesting_vector, "1F")

# 2AAAAAAAAAAAv - can use for 2b if needed
def pca_recover(X, Y):
	vector_list = [X, Y]
	vector_array = np.array(vector_list)
	print("shape of vector array: ", vector_array.shape)
	pca = PCA(n_components = 2)
	pca.fit(vector_array.T)
	return pca.components_[0][1] / pca.components_[0][0]

def ls_recover(X, Y):
	X_mean = np.mean(X)
	Y_mean = np.mean(Y)
	numerator = np.dot(X - X_mean, Y - Y_mean)
	denominator = ((X - X_mean)**2).sum()
	#print(numerator/denominator)
	return numerator/denominator

# 2bbbbbbbbbbb
for i in range(10):
	X = np.random.uniform(size = 5)
	Y = np.random.uniform(size = 5)
	pca_0 = pca_recover(X, Y)
	ls_0 = ls_recover(X, Y)
	print("pca: ", pca_0)
	print("ls: ", ls_0)
	print("X: ", X)
	print("Y: ", Y)

# X_a = [x * 0.001 for x in range(1, 1001)]
# y_a = [2 * x for x in X_a]

# print("pca_recover: ", pca_recover(X_a, y_a))
# print("ls recover: ", ls_recover(X_a, y_a))

from numpy.random import randn

# 2CCCCCCCC
cs = [c * .05 for c in range(11)]
# print(cs)

def make_Y(c):
	Y = np.array([2 * i * .001 for i in range(1, 1001)])
	noise = randn(1000) * math.sqrt(c)
	Y += noise
	return Y

import time

def make_X(c):
	X = np.array([i * .001 for i in range(1, 1001)])
	test_rand = randn(1000)
	test_c = math.sqrt(c)
	noise = randn(1000) * math.sqrt(c)
	X += noise
	#print("shape of X: ", X.shape)
	return X

def make_plot_2(filename, title, X, flag):
	# c on horizontal axis
	# pca-recover on vertical - red dot
	# ls-recover on vertical - blue dot
	plt.title(title)
	plt.xlabel("Noise Level")
	plt.ylabel("Slope")

	for i in range(30):
		for c in cs:
			# noise = randn(1000) * np.sqrt(c)
			Y = make_Y(c)
			if flag != 'c':
				X = make_X(c)
			# print(X)
			# print("X", np.sum(X))
			# print("Y", np.sum(Y))

			# if (np.sum(Y) / np.sum(X)) >= 2:
			# 	print(c)
			# 	print("X", np.sum(X))
			# 	print("Y", np.sum(Y))
			pca = pca_recover(X, Y)
			print("value of pca: ", pca)
			ls = ls_recover(X, Y)
			plt.plot(c, pca, 'rs', label = 'pca', alpha=0.3)
			plt.plot(c, ls, 'bs', label = 'ls', alpha=0.3)
			#X = None
	plt.savefig(filename + ".png", format = 'png')
	plt.close()

X = [x * .001 for x in range(1, 1001)]
# make_plot_2("2c", "Noise on Y", X, flag = 'c')
# make_plot_2("2d", "Noise on X and Y", X, flag = 'd')

# Y = np.array([2 * i * .001 for i in range(1, 1001)])
# noise = randn(1000) * math.sqrt(0.4)
# noisy_Y = Y + noise

# noise = randn(1000) * math.sqrt(0.4)
# noisy_X = X + noise

# def make_plot_temp(filename, x, y):
# 	plt.scatter(x, y)
# 	plt.savefig(filename + ".png", format = 'png')
# 	plt.close()

# make_plot_temp("noisy on y and x, c = 0.4", noisy_Y, noisy_X)

# identifiers, sexes, population_tag, nucleobases = process_text_file('p4dataset2018.txt')
# nucleobases_binary = convert_array_to_binary(nucleobases)

#


import os
from typing import Union, List, Optional
from rich.text import Text
from ideadatamodel.common import SocaKeyValue

BANNER = """
'########::'########::'######::
 ##.... ##: ##.....::'##... ##:
 ##:::: ##: ##::::::: ##:::..::
 ########:: ######:::. ######::
 ##.. ##::: ##...:::::..... ##:
 ##::. ##:: ##:::::::'##::: ##:
 ##:::. ##: ########:. ######::
..:::::..::........:::......:::

"""

def ascii_banner(title: str = 'Research and Engineering Studio on AWS', meta_info: Optional[List[SocaKeyValue]] = None, rich=False) -> Union[Text, str]:
    banner_lines = BANNER.splitlines()
    banner_width = len(banner_lines[2])
    banner_lines.append(str.center(title, banner_width))

    if not rich:
        meta_lines = []
        if meta_info:
            for meta in meta_info:
                tokens = []
                if meta.key:
                    tokens.append(meta.key)
                if meta.value:
                    tokens.append(f'({meta.value})')
                meta_lines.append(str.center(' '.join(tokens), banner_width))
        return os.linesep.join(banner_lines + meta_lines)

    banner_content = os.linesep.join(banner_lines)

    banner_text = Text(banner_content)
    banner_text.highlight_words(['.', ':'], style='orange1 dim')
    banner_text.highlight_words(['\''], style='white')
    banner_text.highlight_words([title, '#'], style='bright_white')

    if not meta_info:
        return banner_text

    meta_content = []
    for meta in meta_info:
        meta_content.append(os.linesep)
        tokens = []
        if meta.key:
            tokens.append(meta.key)
        if meta.value:
            tokens.append(meta.value)
        if len(tokens) == 0:
            continue
        meta_text_content = ' '.join(tokens)
        meta_text = Text(str.center(meta_text_content, banner_width))
        meta_content.append(meta_text)
        if meta.key:
            meta_text.highlight_words([meta.key], style='bright_cyan bold')
        if meta.value:
            meta_text.highlight_words([meta.value], style='bright_green')

    return Text.assemble(banner_text, *meta_content)


from flask import Flask, render_template
from flask_socketio import SocketIO, send

app = Flask(__name__)
socket = SocketIO(app, cors_allowed_origins='*')

# default route
@app.route('/')
def home():
    return render_template('index.html')

# send message socket system
@socket.on('message')
def msg_manager(message):
    send(message, broadcast=True)

# runs app using cors config and socketio
socket.run(app, host='192.168.1.4', debug=True)


from ast import literal_eval
from pathlib import Path

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.metrics.pairwise import cosine_similarity

from config.default import RUTA_SAVE

def load_csv_pokemon():
    lista = ['index', 'pokedex_id', 'name', 'hp', 'attack', 'defense', 'special', 'speed', 'tipos']
    df = pd.read_csv('pokemon_csv6.csv', sep=';', names=lista, engine='python')

    # La columna tipos se lee como un string "[]", para que se una lista hacemos el apply
    df.tipos = df.tipos.apply(literal_eval)
    return df

class Recomendador:
    def __init__(self, dataframe):
        self.dataframe = dataframe.sort_values(by=['pokedex_id']).reset_index(drop=True)
        self.df_cosine = self.load()

    def create_dummies_tipos(self):
        '''
        Create dummies de los tipos de los pokemons
        :param dataframe:
        :return:
        '''
        df_tipos = self.dataframe[['pokedex_id', 'tipos']]
        dummies_df = pd.get_dummies(
            df_tipos.join(pd.Series(df_tipos['tipos'].apply(pd.Series).stack().reset_index(1, drop=True),
                                    name='tipos1')).drop('tipos', axis=1).rename(columns={'tipos1': 'tipos'}),
            columns=['tipos']).groupby('pokedex_id', as_index=False).sum()
        return dummies_df

    def scale(self):
        scaler = StandardScaler()
        scaled_df = scaler.fit_transform(
            self.dataframe.drop(columns=['index', 'pokedex_id', 'name', 'tipos']))
        scaled_df = pd.DataFrame(scaled_df, columns=['hp', 'attack', 'defense', 'special', 'speed'])
        return scaled_df

    def join_dataframes(self):
        new_poke_df = pd.concat([self.create_dummies_tipos(), self.scale()], axis=1)
        return new_poke_df.drop(columns=['pokedex_id'])

    def cosine_similarity(self):
        cos_sim = cosine_similarity(self.join_dataframes().values, self.join_dataframes().values)
        self.save(cos_sim)
        return cos_sim

    @staticmethod
    def load():
        my_file = Path(RUTA_SAVE + 'cosine_similarity.csv')
        if my_file.exists():
            return pd.read_csv(RUTA_SAVE + 'cosine_similarity.csv')

    @staticmethod
    def save(matrix):
        matrix.to_csv(RUTA_SAVE + 'cosine_similarity.csv')

    def get_index_pokemon(self, pokedex_id):
        poke_index = pd.Series(self.dataframe.index, index=self.dataframe['pokedex_id'])
        return poke_index[pokedex_id]

    def recommend(self, pokedex_id, recommendations=5):
        index = self.get_index_pokemon(pokedex_id)
        if not self.df_cosine:
            self.df_cosine = self.cosine_similarity()
        similarity_score = list(enumerate(self.df_cosine[index]))
        sorted_score = sorted(similarity_score, key=lambda x: x[1], reverse=True)
        similar_pokemon = sorted_score[1:recommendations + 1]
        poke_indices = [i[0] for i in similar_pokemon]
        return self.dataframe.iloc[poke_indices].to_dict(orient="records")


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

# Author : Nixawk
# Python : python2/python3

LowerAlpha = 'abcdefghijklmnopqrstuvwxyz'
UpperAlpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Numerals   = '0123456789'

def converge_sets(sets, idx, offsets, length):
    """https://github.com/rapid7/rex-text/blob/master/lib/rex/text.rb
    """
    buf = sets[idx][offsets[idx]]

    if ((idx + 1) < len(sets)) and (sets[idx + 1]):
        buf += converge_sets(sets, idx + 1, offsets, length)
    else:
        offsets[idx] = (offsets[idx] + 1) % len(sets[idx])
        while (idx >= 0 and offsets[idx] == 0):
            idx -= 1
            offsets[idx] = (offsets[idx] + 1) % len(sets[idx])

        if (idx < 0):
            return buf

    return buf

def pattern_create(length, sets=[]):
    """https://github.com/rapid7/metasploit-framework/blob/master/tools/exploit/pattern_create.rb
    """

    buf = ''
    offsets = []

    sets.extend([UpperAlpha, LowerAlpha, Numerals])

    if length < 1:
        return ''

    if len(sets) == 1 and len(sets[0]) == 1:
        return sets[0][0]

    for _ in range(len(sets)):
        offsets.append(0)

    while len(buf) < length:
        buf += converge_sets(sets, 0, offsets, length)

    return buf[0: length]

def pattern_offset(pattern, value, start=0):
    """https://github.com/rapid7/metasploit-framework/blob/master/tools/exploit/pattern_offset.rb
    """
    if isinstance(value, str):
        return pattern.index(value, start)
    elif isinstance(value, int):
        import binascii
        value2 = binascii.unhexlify(value)
        return pattern.index(value2, start)
    else:
        raise Exception("Invalid class for value: %s" % value.__class__)

if __name__ == '__main__':

    import sys

    if len(sys.argv) < 3:
        print("[*] Usage: python %s [option]" % sys.argv[0])
        print("    python pattern.py create 5000")
        print("    python pattern.py offset 5000 [Ae2A | 41653241]")
        sys.exit(0)

    if sys.argv[1].upper() == "CREATE":
        if sys.argv[2].isdigit():
            length = int(sys.argv[2])
            patbuf = pattern_create(length)
            print(patbuf)
        else:
            print("need a length arg")

    elif sys.argv[1].upper() == "OFFSET":
        if sys.argv[2].isdigit():
            length = int(sys.argv[2])
            offstr = sys.argv[3]
            patbuf = pattern_create(length)
            print("offset: %d" % pattern_offset(patbuf, offstr))
        else:
            print("need a length arg")

    else:
        print("Unknown pattern action")

## References

# https://github.com/rapid7/metasploit-framework/blob/master/tools/exploit/pattern_create.rb
# https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/exploit.rb#L1220
# https://github.com/rapid7/rex-text/blob/master/lib/rex/text/pattern.rb
# https://github.com/rapid7/rex-text/blob/master/lib/rex/text.rb


import json
import traceback
from datetime import datetime, timedelta
from dateutil.parser import parse
from django.conf import settings
import math
from django.db import models
from django.db.models.aggregates import Count
from pillowtop.feed.couch import change_from_couch_row, force_to_change

ERROR_MESSAGE_LENGTH = 512

def _get_extra_args(limit, reduce, skip):
    extra_args = dict()
    if not reduce and limit is not None:
            extra_args.update(
                limit=limit,
                skip=skip
            )
    return extra_args

def path_from_object(obj):
    path = "{0}.{1}".format(obj.__class__.__module__, obj.__class__.__name__)
    return path

class PillowError(models.Model):
    doc_id = models.CharField(max_length=255, null=False, db_index=True)
    pillow = models.CharField(max_length=255, null=False)
    date_created = models.DateTimeField()
    date_last_attempt = models.DateTimeField()
    date_next_attempt = models.DateTimeField(db_index=True, null=True)
    total_attempts = models.IntegerField(default=0)
    current_attempt = models.IntegerField(default=0, db_index=True)
    error_type = models.CharField(max_length=255, null=True)
    error_traceback = models.TextField(null=True)
    change = models.TextField(null=True)
    domains = models.CharField(max_length=255, db_index=True, null=True)
    doc_type = models.CharField(max_length=255, db_index=True, null=True)
    doc_date = models.DateTimeField(null=True)
    queued = models.BooleanField(default=False)

    @property
    def change_object(self):
        return change_from_couch_row(json.loads(self.change) if self.change else {'id': self.doc_id})

    class Meta:
        app_label = 'pillow_retry'
        unique_together = ('doc_id', 'pillow',)

    def add_attempt(self, exception, traceb, date=None):
        self.current_attempt += 1
        self.total_attempts += 1
        self.date_last_attempt = date or datetime.utcnow()
        self.error_type = path_from_object(exception)

        self.error_traceback = "{}\n\n{}".format(exception.message, "".join(traceback.format_tb(traceb)))

        if self.current_attempt <= settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS:
            time_till_next = settings.PILLOW_RETRY_REPROCESS_INTERVAL * math.pow(self.current_attempt, settings.PILLOW_RETRY_BACKOFF_FACTOR)
            self.date_next_attempt = self.date_last_attempt + timedelta(minutes=time_till_next)
        else:
            self.date_next_attempt = None

    def reset_attempts(self):
        self.current_attempt = 0
        self.date_next_attempt = datetime.utcnow()

    def has_next_attempt(self):
        return self.current_attempt == 0 or (
            self.total_attempts <= self.multi_attempts_cutoff() and
            self.current_attempt <= settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS
        )

    @classmethod
    def get_or_create(cls, change, pillow, change_meta=None):
        pillow_path = path_from_object(pillow)
        change = force_to_change(change)
        change.document
        doc_id = change.id
        try:
            error = cls.objects.get(doc_id=doc_id, pillow=pillow_path)
        except cls.DoesNotExist:
            now = datetime.utcnow()
            error = PillowError(
                doc_id=doc_id,
                pillow=pillow_path,
                date_created=now,
                date_last_attempt=now,
                date_next_attempt=now,
                change=json.dumps(change.to_dict())
            )

            if change_meta:
                date_string = change_meta.get('date')
                date = parse(date_string) if date_string is not None else None
                domains = ','.join(change_meta.get('domains'))
                error.domains = (domains[:252] + '...') if len(domains) > 255 else domains
                error.doc_type = change_meta.get('doc_type')
                error.doc_date = date

        return error

    @classmethod
    def get_errors_to_process(cls, utcnow, limit=None, skip=0, fetch_full=False):
        """
        Get errors according the following rules:

            date_next_attempt <= utcnow
            AND
            (
                total_attempts <= multi_attempt_cutoff & current_attempt <= max_attempts
                OR
                total_attempts > multi_attempt_cutoff & current_attempt 0
            )

        where:
        * multi_attempt_cutoff = settings.PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF
        * max_attempts = settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS

        :param utcnow:      The current date and time in UTC.
        :param limit:       Paging limit param.
        :param skip:        Paging skip param.
        :param fetch_full:  If True return the whole PillowError object otherwise return a
                            a dict containing 'id' and 'date_next_attempt' keys.
        """
        max_attempts = settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS
        multi_attempts_cutoff = cls.multi_attempts_cutoff()
        query = PillowError.objects \
            .filter(queued=False) \
            .filter(date_next_attempt__lte=utcnow) \
            .filter(
                models.Q(current_attempt=0) |
                (models.Q(total_attempts__lte=multi_attempts_cutoff) & models.Q(current_attempt__lte=max_attempts))
            )

        # temporarily disable queuing of ConfigurableIndicatorPillow errors
        query = query.filter(~models.Q(pillow='corehq.apps.userreports.pillow.ConfigurableIndicatorPillow'))

        if not fetch_full:
            query = query.values('id', 'date_next_attempt')
        if limit is not None:
            return query[skip:skip+limit]
        else:
            return query

    @classmethod
    def multi_attempts_cutoff(cls):
        default = settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS * 3
        return getattr(settings, 'PILLOW_RETRY_MULTI_ATTEMPTS_CUTOFF', default)

    @classmethod
    def bulk_reset_attempts(cls, last_attempt_lt, attempts_gte=None):
        if attempts_gte is None:
            attempts_gte = settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS

        multi_attempts_cutoff = cls.multi_attempts_cutoff()
        return PillowError.objects.filter(
            models.Q(queued=False),
            models.Q(date_last_attempt__lt=last_attempt_lt),
            models.Q(current_attempt__gte=attempts_gte) | models.Q(total_attempts__gte=multi_attempts_cutoff)
        ).update(
            current_attempt=0,
            date_next_attempt=datetime.utcnow()
        )

    @classmethod
    def get_pillows(cls):
        results = PillowError.objects.values('pillow').annotate(count=Count('pillow'))
        return (p['pillow'] for p in results)

    @classmethod
    def get_error_types(cls):
        results = PillowError.objects.values('error_type').annotate(count=Count('error_type'))
        return (e['error_type'] for e in results)

# Stub models file, also used in tests
from dimagi.ext.couchdbkit import Document

class Stub(Document):
    pass


# Shortest path from node to the target
#     o
#     o
#     o
#    o o
#   o o
#  .. None

# from collections import deque
# class Solution:

#     def __init__(self,edges):

#         self.adjList = {}
#         for src, dst in edges:
#             if src not in self.adjList:
#                 self.adjList[src] = []
#             if dst not in self.adjList:
#                 self.adjList[dst] = []
#             self.adjList[src].append(dst)

#     def bfs(self,node,target):

#         length = 0
#         quene = deque()
#         quene.append(node)
#         visit = set()
#         visit.add(node)

#         while quene:
#             for i in range(len(quene)):
#                 curr = quene.popleft()
#                 if curr == target:
#                     return length
#                 for neighbor in self.adjList[curr]:
#                     if neighbor not in visit:
#                         visit.add(neighbor)
#                         quene.append(neighbor)

#             length+=1
#         return length

# edges = [["A", "B"], ["B", "C"], ["B", "E"], ["C", "E"], ["E", "D"]]
# s = Solution(edges)
# print(s.bfs("A","E"))

# https://stackoverflow.com/questions/69471667/two-implementation-methods-of-bfs-for-finding-the-shortest-path-which-one-is-th
from collections import deque
class Solution:

    def __init__(self,edges):

        self.adjList = {}
        for src, dst in edges:
            if src not in self.adjList:
                self.adjList[src] = []
            if dst not in self.adjList:
                self.adjList[dst] = []
            self.adjList[src].append(dst)
   #ab,abc,abe,abce
    def bfs(self,node,target):

        quene = deque()
        quene.append([node])
        visit = set()
        visit.add(node)

        while quene:
            for i in range(len(quene)):
                path = quene.popleft()
                curr = path[-1]
                if curr == target:
                    return path
                for neighbor in self.adjList.get(curr,[]):
                    if neighbor not in visit:
                        new_path=path[:]
                        new_path.append(neighbor)
                        visit.add(neighbor)
                        quene.append(new_path)

        return []

edges = [["A", "B"], ["B", "C"], ["B", "E"], ["C", "E"], ["E", "D"]]
s = Solution(edges)
print(s.bfs("A","E"))


import os
import logging
import argparse
from contextlib import contextmanager
import importlib
import sys

import hglib

from fluent.migrate.context import MigrationContext
from fluent.migrate.errors import MigrationError
from fluent.migrate.changesets import convert_blame_to_changesets
from fluent.migrate.blame import Blame

@contextmanager
def dont_write_bytecode():
    _dont_write_bytecode = sys.dont_write_bytecode
    sys.dont_write_bytecode = True
    yield
    sys.dont_write_bytecode = _dont_write_bytecode

class Migrator:
    def __init__(self, locale, reference_dir, localization_dir, dry_run):
        self.locale = locale
        self.reference_dir = reference_dir
        self.localization_dir = localization_dir
        self.dry_run = dry_run
        self._client = None

    @property
    def client(self):
        if self._client is None:
            self._client = hglib.open(self.localization_dir, 'utf-8')
        return self._client

    def close(self):
        # close hglib.client, if we cached one.
        if self._client is not None:
            self._client.close()

    def run(self, migration):
        print('\nRunning migration {} for {}'.format(
            migration.__name__, self.locale))

        # For each migration create a new context.
        ctx = MigrationContext(
            self.locale, self.reference_dir, self.localization_dir
        )

        try:
            # Add the migration spec.
            migration.migrate(ctx)
        except MigrationError as e:
            print('  Skipping migration {} for {}:\n    {}'.format(
                migration.__name__, self.locale, e))
            return

        # Keep track of how many changesets we're committing.
        index = 0
        description_template = migration.migrate.__doc__

        # Annotate localization files used as sources by this migration
        # to preserve attribution of translations.
        files = ctx.localization_resources.keys()
        blame = Blame(self.client).attribution(files)
        changesets = convert_blame_to_changesets(blame)
        known_legacy_translations = set()

        for changeset in changesets:
            snapshot = self.snapshot(
                ctx, changeset['changes'], known_legacy_translations
            )
            if not snapshot:
                continue
            self.serialize_changeset(snapshot)
            index += 1
            self.commit_changeset(
                description_template, changeset['author'], index
            )

    def snapshot(self, ctx, changes_in_changeset, known_legacy_translations):
        '''Run the migration for the changeset, with the set of
        this and all prior legacy translations.
        '''
        known_legacy_translations.update(changes_in_changeset)
        return ctx.serialize_changeset(
            changes_in_changeset,
            known_legacy_translations
        )

    def serialize_changeset(self, snapshot):
        '''Write serialized FTL files to disk.'''
        for path, content in snapshot.items():
            fullpath = os.path.join(self.localization_dir, path)
            print(f'  Writing to {fullpath}')
            if not self.dry_run:
                fulldir = os.path.dirname(fullpath)
                if not os.path.isdir(fulldir):
                    os.makedirs(fulldir)
                with open(fullpath, 'wb') as f:
                    f.write(content.encode('utf8'))
                    f.close()

    def commit_changeset(
        self, description_template, author, index
    ):
        message = description_template.format(
            index=index,
            author=author
        )

        print(f'  Committing changeset: {message}')
        if self.dry_run:
            return
        try:
            self.client.commit(
                message, user=author.encode('utf-8'), addremove=True
            )
        except hglib.error.CommandError as err:
            print(f'    WARNING: hg commit failed ({err})')

def main(locale, reference_dir, localization_dir, migrations, dry_run):
    """Run migrations and commit files with the result."""
    migrator = Migrator(locale, reference_dir, localization_dir, dry_run)

    for migration in migrations:
        migrator.run(migration)

    migrator.close()

def cli():
    parser = argparse.ArgumentParser(
        description='Migrate translations to FTL.'
    )
    parser.add_argument(
        'migrations', metavar='MIGRATION', type=str, nargs='+',
        help='migrations to run (Python modules)'
    )
    parser.add_argument(
        '--locale', '--lang', type=str,
        help='target locale code (--lang is deprecated)'
    )
    parser.add_argument(
        '--reference-dir', type=str,
        help='directory with reference FTL files'
    )
    parser.add_argument(
        '--localization-dir', type=str,
        help='directory for localization files'
    )
    parser.add_argument(
        '--dry-run', action='store_true',
        help='do not write to disk nor commit any changes'
    )
    parser.set_defaults(dry_run=False)

    logger = logging.getLogger('migrate')
    logger.setLevel(logging.INFO)

    args = parser.parse_args()

    # Don't byte-compile migrations.
    # They're not our code, and infrequently run
    with dont_write_bytecode():
        migrations = map(importlib.import_module, args.migrations)

    main(
        locale=args.locale,
        reference_dir=args.reference_dir,
        localization_dir=args.localization_dir,
        migrations=migrations,
        dry_run=args.dry_run
    )

if __name__ == '__main__':
    cli()


#!/usr/bin/env python
"""
DBS 3 Client Example.   This script is called

./listSampleInfoFromDAS.py

"""

# api info https://github.com/dmwm/DBS/blob/master/Client/src/python/dbs/apis/dbsClient.py

import sys
from dbs.apis.dbsClient import DbsApi
from python.Samples import *
import gc
import re

def get_sample_list(sample_group):
  sample_list = []
  if sample_group == 'all':
    # get all objects in sample
    sample_list = [o for o in gc.get_objects() if isinstance(o, sample)]
  elif sample_group == 'background':
    # get only background
    sample_list = Background
  elif sample_group == 'signal':
    # get only the signal
    sample_list = Signal
  else:
    print('No valid sample group')
  return sample_list

def main():
#  args=sys.argv[1:]
#  data=args[0]

  sample_group = 'signal' # signal, background, data, all
  sample_list = get_sample_list(sample_group)
  sample_list.sort()

  url="https://cmsweb.cern.ch/dbs/prod/global/DBSReader"
  api=DbsApi(url=url)

  for samp in sample_list:
    outputDataSets = ''
    #print('Checking {1}'.format(samp.DAS))
    outputDataSets = api.listDatasets(dataset=samp.DAS, detail = True, dataset_access_type='VALID')

    if outputDataSets:
      for ds in outputDataSets:
       #print('{0}'.format(ds['dataset']))
       #print('{0}'.format(ds['primary_ds_name']))
       #print('{0}'.format(ds['xtcrosssection']))
       nevents = api.listBlockSummaries(dataset=ds['dataset'])
       #print(nevents[0]['num_event'])
       # this to create a table for the paper with dataset name and number of events
       print('verb@ {0} @ & {1:.2e} & XX \\\\ '.format(ds['primary_ds_name'],nevents[0]['num_event']))
  sys.exit(0);

if __name__ == "__main__":
    main()
    sys.exit(0);


"""Add shows and relationships

Revision ID: 756b6d587a2d
Revises: b83ff006d4d6
Create Date: 2021-10-15 17:26:27.678524

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '756b6d587a2d'
down_revision = 'b83ff006d4d6'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('shows',
    sa.Column('artist_id', sa.Integer(), nullable=False),
    sa.Column('venue_id', sa.Integer(), nullable=False),
    sa.Column('start_time', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['artist_id'], ['artists.id'], ),
    sa.ForeignKeyConstraint(['venue_id'], ['venues.id'], ),
    sa.PrimaryKeyConstraint('artist_id', 'venue_id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('shows')
    # ### end Alembic commands ###


Version = '3.0.0a1'
VersionTuple = (3, 0, 0, 'alpha', 1)

MinCompatibleVersion = '3.0.0a1'
MinCompatibleVersionTuple = (3, 0, 0, 'alpha', 1)

####
def convertVersionStringToTuple(s):
    versionNum = [0, 0, 0]
    releaseType = 'final'
    releaseTypeSubNum = 0
    if s.find('a') != -1:
        num, releaseTypeSubNum = s.split('a')
        releaseType = 'alpha'
    elif s.find('b') != -1:
        num, releaseTypeSubNum = s.split('b')
        releaseType = 'beta'
    elif s.find('rc') != -1:
        num, releaseTypeSubNum = s.split('rc')
        releaseType = 'candidate'
    else:
        num = s
    num = num.split('.')
    for i in range(len(num)):
        versionNum[i] = int(num[i])
    if len(versionNum) < 3:
        versionNum += [0]
    releaseTypeSubNum = int(releaseTypeSubNum)

    return tuple(versionNum + [releaseType, releaseTypeSubNum])


import argparse
import logging
import sys

from watermarks import __version__ as version

class WMParser(argparse.ArgumentParser):
    def error(self, message):
        sys.stderr.write('error: {0}\n'.format(message))
        self.print_help()
        sys.exit(2)

def handle_version():
    if '--version' in sys.argv:
        print(version)
        sys.exit(0)

def handle_help(parser):
    if '-h' in sys.argv or '--help' in sys.argv:
        parser.print_help()
        sys.exit(0)

def get_log_level(args):
    if args.verbose:
        ll = logging.DEBUG
    elif args.quiet:
        ll = logging.ERROR
    else:
        ll = None
    return ll

def add_basic_args(parser):
    vq_group = parser.add_mutually_exclusive_group()
    vq_group.add_argument(
        '-q', '--quiet', action='store_true', help='Be quiet.'
    )
    vq_group.add_argument(
        '-v', '--verbose', action='store_true', help='Be verbose.'
    )
    parser.add_argument(
        '--version', action='store_true', help='Show version and quit.'
    )
    parser.add_argument(
        '-h', '--help', action='store_true',
        help='Show this help message and exit.'
    )

def add_common_args(parser):
    parser.add_argument(
        'sources', metavar='PTH', nargs='+',
        help='List of files/directories that will be processed. Directories '
             'will be listed (but not recursive).'
    )
    parser.add_argument(
        '-d', '--dest-dir', required=True,
        help='Directory where the extracted watermarks will be stored.'
    )
    parser.add_argument(
        '--format', default='',
        help='Format of generated images.'
    )
    parser.add_argument(
        '-s', '--suffix', default='',
        help='Suffix of watermarked files (default: %(default)s).'
    )


# Crie um programa que leia o nome de uma cidade e diga se ela começa ou não com o nome "SANTO".

cidade = str(input('Informe o nome da sua cidade: ')).strip()
divisao = cidade.split()
if divisao[0].upper() == 'SANTO':
    simnao = 'SIM'
else:
    simnao = 'NÃO'

print('O nome da cidade começa com SANTO? {}'.format(simnao))


from collections.abc import Iterable

from ._helper import getAttribute
from ._isArrayLike import _isArrayLike
from ._isFunction import _isFunction

def _createReduce(arrayReduce, methodReduce, iterableReduce):
  def _reduce(xf, acc, arr):
    if arr is None:
      return acc
    if _isArrayLike(arr):
      return arrayReduce(xf, acc, arr)
    if _isFunction(getAttribute(arr, 'fantasy-land/reduce')):
      return methodReduce(xf, acc, arr, 'fantasy-land/reduce')
    if isinstance(arr, Iterable):
      return iterableReduce(xf, acc, arr)
    if _isFunction(getattr(arr, 'reduce', None)):
      return methodReduce(xf, acc, arr, 'reduce')
    raise ValueError('reduce: list must be array or iterable')
  return _reduce


# Add customer Page
from selenium import webdriver
class SearchCustomer:
    txtEmail_id = "SearchEmail"
    txtFirstName_id = "SearchFirstName"
    txtLastName_id = "SearchLastName"
    btnSearch_id = "search-customers"
    tblSearchResults_xpath = "//table[@role='grid']"
    table_xpath = "//table[@id='customers-grid']"
    tableRows_xpath = "//table[@id='customers-grid']//tbody/tr"
    tableColumns_xpath = "//table[@id='customers-grid']//tbody/tr/td"

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

    def setemail(self,email):
        self.driver.find_element_by_id(self.txtEmail_id).clear()
        self.driver.find_element_by_id(self.txtEmail_id).send_keys(email)

    def setFirstName(self, FirstName):
        self.driver.find_element_by_id(self.txtFirstName_id).clear()
        self.driver.find_element_by_id(self.txtFirstName_id).send_keys(FirstName)

    def setLastName(self,LastName):
        self.driver.find_element_by_id(self.txtLastName_id).clear()
        self.driver.find_element_by_id(self.txtLastName_id).send_keys(LastName)

    def clickSearchBtn(self):
        self.driver.find_element_by_id(self.btnSearch_id).click()

    def getNosRows(self):
        return len(self.driver.find_elements_by_xpath(self.tableRows_xpath))

    def getNosColumn(self):
        return len(self.driver.find_elements_by_xpath(self.tableColumns_xpath))

    def getCustomerEmail(self,email):
        flag=False
        for r in range(1, self.getNosRows() + 1):
            table=self.driver.find_element_by_xpath(self.table_xpath)
            emailid=table.find_element_by_xpath("//*[@id='customers-grid']/tbody/tr["+str(r)+"]/td[2]").text
            if emailid==email:
                flag=True
                break
        return flag

    def getCustomerName(self,Name):
        flag=False
        for r in range(1, self.getNosRows() + 1):
            table = self.driver.find_element_by_xpath(self.table_xpath)
            name=self.find_element_by_xpath("//*[@id='customers-grid']/tbody/tr["+str(r)+"]/td[3]").text
            if name==Name:
                flag=True
                break
                return flag


import os.path

import matplotlib.pyplot as plt
import numpy as np
from Bio import Phylo

def _fix_node_names(tree):
    """
    Fix the node names as follows:
    - Give all anonymous nodes a name (ID = index)
    - In each node name, replace '#' by '_' (for reticulations).

    :param tree: A tree (of type BaseTree.Tree)
    :return: The tree with relabeled nodes
    """
    for i, clade in enumerate(tree.find_clades()):
        # Give all anonymous nodes a name
        if not clade.name:
            clade.name = '_' + str(i)
        else:
            clade.name = clade.name.replace('#', '_')

def read(filename: str):
    """
    Read the input file and give each node a name
    :param filename: A file name
    :return: The trees
    """
    if not os.path.isfile(filename):
        myFile = f'../Cass_data/restricted_grass_trees/{filename}'
        if os.path.isfile(myFile):
            filename = myFile
        else:
            myFile = f'../Cass_data/restricted_grass_clusters/{filename}'
            if os.path.isfile(myFile):
                filename = myFile
            else:
                raise RuntimeError(f"The file '{filename}' does not exist.")

    # If this is a Newick file, parse the trees
    ext = os.path.splitext(filename)[1]  # Get the file extension
    if ext == '.nwk':
        trees = list(Phylo.parse(filename, "newick"))
        for t in trees:
            _fix_node_names(t)
        clusters, _, _ = get_clusters(trees)
        leaves = get_term_names(trees)

        # short_name = Path(filename).stem
        # draw_trees(trees, 'Input Trees', filename=(short_name + '_trees'))
    else:
        # Otherwise, assume that the file contains a list of clusters
        clusters, leaves = _read_clusters(filename)
    print(f"SUCCESS: '{filename}' has been read successfully.")

    return clusters, leaves

def _read_clusters(filename: str):
    clusters = []
    leaves = set()
    with open(filename) as file:
        for line in file:
            if line[0:2] == '//':  # If this is a comment, skip it
                continue
            if line[0] == '.':  # If this is the end of the file
                break
            cl = line.rstrip().split(' ')
            clusters.append(cl)
            leaves.update(cl)

    return clusters, list(leaves)

def tree2newick(tree):
    """
    Get a Newick formatted string of the tree
    :param tree: A tree
    :return: The tree in the Newick format
    """
    items = []
    for clade in tree.root.clades:
        s = ''
        if len(clade.clades) > 0:
            subtree = tree2newick(clade)
            if subtree != '':
                s += subtree
        s += '' if clade.name is None else clade.name
        items.append(s)
    return '(' + ','.join(items) + ')'

def get_term_names(trees: list):
    """
    Get the sorted union of the leaf names in the given trees
    :param trees: A list of trees (of type BaseTree.Tree)
    :return: A list of terminal names
    """
    names = set()
    node_list = []
    for t in trees:
        for clade in t.find_clades():
            if clade.name not in node_list:
                node_list.append(clade.name)
                if clade.is_terminal():
                    names.add(clade.name)
    return sorted(names)

def get_terminals(tree):
    """
    Get the terminals in the tree, ordered by name
    :param tree: A tree (of type Phylo.BaseTree.Tree)
    :return: A sorted list
    """
    terms = tree.get_terminals()
    return sorted(terms, key=lambda t: t.name)

def get_clusters(trees: list):
    """
    Get the union of all clusters in the trees.
    Don't return the cluster on all taxa
    :param trees: A list of trees (of type BaseTree.Tree)
    :return: A list of clusters
    """
    clusters = []
    taxa = set()
    tree_ind = []
    for i, tree in enumerate(trees):
        t_cl, _taxa = get_tree_clusters(tree)
        taxa.update(_taxa)
        for cl in t_cl:
            if cl not in clusters:
                clusters.append(cl)
                tree_ind.append([i])
            else:
                tree_ind[clusters.index(cl)].append(i)
    return clusters, tree_ind, sorted(taxa)

def get_tree_clusters(tree):
    """ Get a list of clusters, where each cluster is a list of taxa, AND a list of the taxa
    Don't return the cluster on all taxa
    """
    clusters = []
    taxa = set()
    for clade in tree.find_clades(terminal=False):
        cl = []
        cl_to_visit = [clade]
        for subclade in cl_to_visit:
            for child in subclade:
                if child.name[0] == '_':
                    cl_to_visit.append(child)
                else:
                    cl.append(child.name)
        cl.sort()
        clusters.append(cl)
        taxa.update(cl)
    return clusters, sorted(taxa)

def ensure_dir(filename):
    # Create the path if it does not exist
    path = os.path.dirname(filename)
    if len(path) and not os.path.exists(path):
        os.makedirs(path)

def draw_trees(trees, title=None, filename=None):
    """
    Draw the trees in one figure
    :param trees: A list of trees
    :param title: A title
    :param filename: A file name to save the figure
    """
    n_figs = len(trees)  # #figures
    if n_figs == 0:  # If there is nothing to show, then return
        print("INFO: There is nothing to show.")
        return
    plt.clf()
    n_cols = int(np.ceil(np.sqrt(n_figs)))
    n_rows = n_cols - 1 if n_figs <= n_cols * (n_cols - 1) else n_cols
    fig, ax = plt.subplots(n_rows, n_cols, num=1)
    for i, t in enumerate(trees):
        if n_cols == 1:
            axes = ax
        else:
            ix = np.unravel_index(i, ax.shape)
            axes = ax[ix]
        Phylo.draw(t, do_show=False, axes=axes)
    if title is not None:
        fig.suptitle(title)
    if filename is not None:
        # filename = 'output/' + filename
        plt.savefig(filename, bbox_inches='tight')  # Save the figure
    plt.show()


# 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (leetcode)
class Solution:
    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
        l = list(map(str, sentence.split()))
        x = len(searchWord)
        for i in range(len(l)):
            if len(l[i]) >= x:
                if l[i][:x] == searchWord:
                    return i + 1
        return -1


"""MY_CMDB URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.urls import path,re_path
from assets.views import home
from assets.views import cmdb

app_name = "assets"

urlpatterns = [

    path('',home.CmdbView.as_view(),name="assets_index"),

    re_path('^asset/$', cmdb.AssetView.as_view(), name="asset"),
    re_path('^asset/(?P<asset_id>\d+)/$', cmdb.AssetDetailView.as_view(), name="asset_detail"),
    re_path('^assets/$', cmdb.AssetsView.as_view(), name="assets"),

    re_path('^bind_host/$', cmdb.BindHostView.as_view(), name="bind_host"),
    re_path('^bind_hosts/$', cmdb.BindHostsView.as_view(), name="bind_hosts"),

    re_path('^idc/$', cmdb.IdcView.as_view(), name="idc"),
    re_path('^idc_info/$', cmdb.IdcInfoView.as_view(), name="idc_info"),

    re_path('^tag/$', cmdb.TagView.as_view(), name="idc_info"),
    re_path('^tags/$', cmdb.TagsView.as_view(), name="idc_info"),

    re_path('^asset_log/$', cmdb.AssetLog.as_view(), name="idc_info"),
    re_path('^asset_logs/$', cmdb.AssetLogs.as_view(), name="idc_info"),

    re_path('^chart-(?P<chart_type>\w+)/$', home.ChartView.as_view()),
]


import re
from xml.dom.minidom import Element, Document, parse

from invoiceconverterlib.InvoiceDocument import InvoiceDocument, InvoiceHeader, InvoiceParty, InvoiceSummary, InvoiceTaxSummary, \
    InvoiceLine
from invoiceconverterlib.ParserInterface import ParserInterface
from invoiceconverterlib.utils.XmlUtil import xmlExtractText, xmlExtractDate, xmlExtractTextOrEmpty

class OptimaXmlParser(ParserInterface):
    """TODO: not ready, trim name and remove new lines"""

    def test(self, filename: str) -> bool:
        if not filename.endswith('.xml'):
            return False
        doc = parse(filename)
        return len(doc.getElementsByTagName("NAGLOWEK")) > 0

    def parse(self, filename: str) -> InvoiceDocument:
        doc = parse(filename)
        return self.parseXml(doc)

    def parseXml(self, xmlRef: Document) -> InvoiceDocument:
        result = InvoiceDocument()
        result.header = self.parseHeader(xmlRef.getElementsByTagName("NAGLOWEK")[0])
        result.buyer = self.parseParty(xmlRef.getElementsByTagName("ODBIORCA")[0])
        result.seller = self.parseParty(xmlRef.getElementsByTagName("SPRZEDAWCA")[0])
        result.summary = self.parseSummary(xmlRef.getElementsByTagName("NAGLOWEK")[0])
        result.lines = self.parseLines(xmlRef.getElementsByTagName("POZYCJA"))
        result = self.processResult(result)
        return result

    def parseLines(self, xmlLines):
        result = []
        for sl in xmlLines:
            result.append(self.parseLine(sl))
        return result

    def parseHeader(self, xmlHeader: Element) -> InvoiceHeader:
        header = InvoiceHeader()
        header.number = xmlExtractText(xmlHeader, 'NUMER_PELNY')
        header.invoiceDate = xmlExtractDate(xmlHeader, 'DATA_WYSTAWIENIA')
        header.salesDate = xmlExtractDate(xmlHeader, 'DATA_OPERACJI')
        header.currency = xmlExtractText(xmlHeader, 'SYMBOL')
        payment_xml = xmlHeader.getElementsByTagName("PLATNOSC")[0]
        header.paymentDueDate = xmlExtractDate(payment_xml, 'TERMIN')
        header.paymentTerms = xmlExtractText(payment_xml, 'FORMA')
        header.documentFunctionCode = ""
        return header

    def parseParty(self, xmlParty: Element) -> InvoiceParty:
        party = InvoiceParty()
        party.taxId = xmlExtractText(xmlParty, 'NIP')
        party.accountNumber = xmlExtractTextOrEmpty(xmlParty, 'NUMER_KONTA_BANKOWEGO')
        party.name = xmlExtractText(xmlParty, 'NAZWA')
        party.streetAndNumber = xmlExtractText(xmlParty, 'ULICA')
        party.cityName = xmlExtractText(xmlParty, 'MIASTO')
        party.postalCode = xmlExtractText(xmlParty, 'KOD_POCZTOWY')
        party.country = xmlExtractText(xmlParty, 'KRAJ')
        return party

    def parseSummary(self, xmlSummary: Element) -> InvoiceSummary:
        summary = InvoiceSummary()
        summary.totalLines = 0
        summary.totalNetAmount = xmlExtractText(xmlSummary, 'RAZEM_NETTO')
        summary.totalTaxableBasis = xmlExtractText(xmlSummary, 'RAZEM_NETTO')
        summary.totalTaxAmount = xmlExtractText(xmlSummary, 'RAZEM_VAT')
        summary.totalGrossAmount = xmlExtractText(xmlSummary, 'RAZEM_BRUTTO')
        summary.grossAmountInWords = ""
        summary.taxSummary = None
        return summary

    def parseTaxSummary(self, xmlTaxSummary: Element) -> InvoiceTaxSummary:
        tax_summary = InvoiceTaxSummary()
        tax_summary.taxRate = xmlExtractText(xmlTaxSummary, 'TaxRate')
        tax_summary.taxCategoryCode = xmlExtractText(xmlTaxSummary, 'TaxCategoryCode')
        tax_summary.taxAmount = xmlExtractText(xmlTaxSummary, 'TaxAmount')
        tax_summary.taxableBasis = xmlExtractText(xmlTaxSummary, 'TaxableBasis')
        tax_summary.taxableAmount = xmlExtractText(xmlTaxSummary, 'TaxableAmount')
        tax_summary.grossAmount = xmlExtractText(xmlTaxSummary, 'GrossAmount')
        return tax_summary

    def parseLine(self, xmlLine: Element) -> InvoiceLine:
        line = InvoiceLine()
        line.LineNumber = xmlExtractText(xmlLine, 'LP')
        line.EAN = xmlExtractText(xmlLine, 'EAN')
        line.BuyerItemCode = xmlExtractText(xmlLine, 'KOD')
        line.SupplierItemCode = xmlExtractText(xmlLine, 'NUMER_KATALOGOWY')
        line.ItemDescription = xmlExtractText(xmlLine, 'NAZWA')
        line.ItemType = ""
        line.InvoiceQuantity = xmlExtractText(xmlLine, "ILOSC")
        line.UnitOfMeasure = xmlExtractText(xmlLine, "JM")
        line.InvoiceUnitPacksize = xmlExtractText(xmlLine, "ILOSC")
        line.PackItemUnitOfMeasure = xmlExtractText(xmlLine, "JM")
        line.InvoiceUnitNetPrice = xmlExtractText(xmlLine, "PO_RABACIE_PLN")
        line.TaxRate = xmlExtractText(xmlLine, "STAWKA")
        line.TaxCategoryCode = xmlExtractText(xmlLine, "FLAGA")
        line.ReferenceType = ""
        line.ReferenceNumber = ""
        line.NetAmount = xmlExtractText(xmlLine, "WARTOSC_NETTO")
        grossAmount = xmlExtractText(xmlLine, "WARTOSC_BRUTTO")
        tax = str(round(float(grossAmount) - float(line.NetAmount), 2))
        line.TaxAmount = tax
        return line

    def processResult(self, result):
        result.buyer.shortName = self.shortName(result.buyer.name)
        result.seller.shortName = self.shortName(result.seller.name)
        result.summary.totalLines = len(result.lines)
        return result

    def shortName(self, name):
        match = re.search('\\A[@\\w]+', name)
        if match:
            return match.group()
        else:
            return name


from src.types.node import ListNode

def is_palindrome(head: ListNode) -> bool:
    x = ''
    ln = 0
    while head is not None:
        x += f'{head.val}'
        ln += 1
        head = head.next

    def palindrome(_ln: int, _x: str) -> bool:
        for i in range(_ln):
            if _x[i] != _x[ln - i - 1]:
                return False
        return True

    return palindrome(ln, x)


import numpy as np

D = 5
K = 3

def sigmoid(a):
    return 1 / (1 + np.exp(-a))

x = np.random.randn(D, 1)
c = np.random.randn(K, 1)
v = np.random.randn(K, 1)
W = np.random.randn(K, D)
b = np.random.randn(1, 1)
print(v.shape)
a = np.matmul(W, x) + c
h = sigmoid(a)
print(h.shape)
f = np.matmul(v.T, h) + b

# Create function for error and expensively approximate gradients by finite
# differences:
E_fn = lambda W, x, c, b, v: np.matmul(v.T, sigmoid(np.matmul(W, x) + c)) + b
# E_fn = lambda x, A: np.matmul(x.T, np.matmul(A, x))
def checkgrad(fn, hh, *args):
    """Return all approx partial derivatives of fn wrt args"""
    bars = []
    for arg in args:
        bar = np.zeros_like(arg)
        arg_view = arg.ravel()
        bar_view = bar.ravel()
        for ii in range(arg_view.size):
            cc = arg_view[ii]
            arg_view[ii] = cc + hh/2.0
            f2 = fn(*args)
            arg_view[ii] = cc - hh/2.0
            f1 = fn(*args)
            arg_view[ii] = cc
            bar_view[ii] = (f2 - f1) / hh
        bars.append(bar)
    return bars
W_bar_fd, x_bar_fd, c_bar_fd, b_bar_fd, v_bar_fd = checkgrad(E_fn, 1e-5, W, x, c, b, v)

f_bar = 1
h_bar = v
a_bar = (1-sigmoid(a))*sigmoid(a)*h_bar
x_bar = np.matmul(W.T, a_bar)

err1 = np.max(np.abs(x_bar - x_bar_fd))
print(err1)


# There is a rich set of sequence libraries implemented in python

# Container Sequences
# List, Collections, Tuple can hold objects of different types.

# GENERATOR FUNCTIONS CAN BE USED TO GENERATE STUFFS. IT'S A BETTER ALTERNATIVE TO MANUALLY
# CONSTRUCTING THE ARRAY FROM OTHER TEMPORARY ARRAY. BECUASE THE TEMP ARRAY WILL TAKE UP MEMORY.

color = ['BLACK', 'WHITE']
size = ['S', 'M', 'L']

# PRINT THE VARIOUS COMBINATIONS OF THE STUFF
for tshirt in ('%s %s' % (c, s) for c in color for s in size):
        print(tshirt)


import requests
import multiprocessing as mp
from time import time

N=2000
W=10

def bench(i):
	_i = str(i)
	requests.get('http://127.0.0.1:8080/kv',params=dict(op='set',k='k'+_i,v='v'+_i))

if __name__=="__main__":
	pool = mp.Pool(W)
	t0=time()
	pool.map(bench,range(N))
	dt=time()-t0
	print(dt,1.0*N/dt)


# facerec.py
import cv2, sys, numpy, os
import time
size = 4
capture_duration = 10
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'database'
# Part 1: Create fisherRecognizer
print('Training...')
# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(datasets):
    for subdir in dirs:
        names[id] = subdir
        subjectpath = os.path.join(datasets, subdir)
        for filename in os.listdir(subjectpath):
            path = subjectpath + '\\' + filename
            #print("Path = ",path)
            lable = id
            images.append(cv2.imread(path, 0))
            lables.append(int(lable))
        id += 1
(width, height) = (130, 100)
# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
# OpenCV trains a model from the images
# NOTE FOR OpenCV2: remove '.face'
#model = cv2.createFisherFaceRecognizer()
def faceDetectStatus():
    try:
        # model = cv2.face.FisherFaceRecognizer_create()
        # model = cv2.face.LBPHFaceRecognizer_create()
        # create our LBPH face recognizer
        model = cv2.face.LBPHFaceRecognizer_create()
        # or use EigenFaceRecognizer by replacing above line with
        # model = cv2.face.EigenFaceRecognizer_create()
        # or use FisherFaceRecognizer by replacing above line with
        # model = cv2.face.FisherFaceRecognizer_create()
        model.train(images, lables)
    except Exception as e:
        print(str(e))
        pass
    # Part 2: Use fisherRecognizer on camera stream
    face_cascade = cv2.CascadeClassifier(haar_file)
    webcam = cv2.VideoCapture(0)
    # save current time
    prev_time = time.time()
    # start webcam feed
    start_time = time.time()
    # while True:
    while (int(time.time() - start_time) < capture_duration):
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        rslt = []
        for (x, y, w, h) in faces:
            cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))

            # Try to recognize the face
            prediction = model.predict(face_resize)
            print('Predection is ', prediction)
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
            if prediction[1] > 90:
                rslt.append('True')
                cv2.putText(im, '%s - %.0f' % (names[prediction[0]], prediction[1]), (x - 10, y - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
                break

            else:
                rslt.append('False')
                cv2.putText(im, 'not recognized', (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
                break

        cv2.imshow('OpenCV', im)
        key = cv2.waitKey(10)
        if key == 27:
            # if key == -1:
            break
    cv2.destroyAllWindows()
    return rslt

13 Machine Learning for Efficient Assessment and Prediction of Human Performance/Code/HumanPredections/face_recognize.py


import sys
sys.path.append("..")  # 包含上级目录
import pandas as pd
import json
import math
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import tqdm
import cv2
import tensorflow as tf
from keras.utils.multi_gpu_utils import multi_gpu_model

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import util

from config_reader import config_reader
from config import COCOSourceConfig, GetConfig

from keras import backend as K

import os

# os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
K.set_learning_phase(1)

# the middle joints heatmap correpondence
limbSeq = [[1, 0], [1, 14], [1, 15], [1, 16], [1, 17], [0, 14], [0, 15], [14, 16], [15, 17],
           [1, 2], [2, 3], [3, 4], [1, 5], [5, 6], [6, 7], [1, 8], [8, 9],
           [9, 10], [1, 11], [11, 12], [12, 13], [8, 11], [2, 16], [5, 17]]

mapIdx = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20, 21], [22, 23],
          [24, 25], [26, 27], [28, 29], [30, 31], [32, 33], [34, 35], [36, 37], [38, 39], [40, 41], [42, 43], [44, 45],
          [46, 47]]

# visualize
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0],
          [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255],
          [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [193, 193, 255], [106, 106, 255], [20, 147, 255],
          [128, 114, 250], [130, 238, 238], [48, 167, 238], [180, 105, 255]]

#  TODO: when evaluation, change the keypoint id mapping!! Because we use different ids compared with MSCOCO dataset!
dt_gt_mapping = {0: 0, 1: None, 2: 6, 3: 8, 4: 10, 5: 5, 6: 7, 7: 9, 8: 12, 9: 14, 10: 16, 11: 11, 12: 13, 13: 15,
                 14: 2, 15: 1, 16: 4, 17: 3, 18: None}

def predict(image, params, model, model_params, heat_layers, paf_layers):
    # print (image.shape)
    heatmap_avg = np.zeros((image.shape[0], image.shape[1], heat_layers))
    paf_avg = np.zeros((image.shape[0], image.shape[1], paf_layers))
    multiplier = [x * model_params['boxsize'] / image.shape[0] for x in params['scale_search']]
    for m in range(len(multiplier)):
        scale = multiplier[m]

        imageToTest = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
        imageToTest_padded, pad = util.padRightDownCorner(imageToTest, model_params['stride'],
                                                          model_params['padValue'])

        input_img = np.transpose(np.float32(imageToTest_padded[:, :, :, np.newaxis]),
                                 (3, 0, 1, 2))  # required shape (1, width, height, channels)

        output_blobs = model.predict(input_img)

        # extract outputs, resize, and remove padding
        heatmap = np.squeeze(output_blobs[1])  # output 1 is heatmaps
        heatmap = cv2.resize(heatmap, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                             interpolation=cv2.INTER_CUBIC)
        heatmap = heatmap[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_CUBIC)

        paf = np.squeeze(output_blobs[0])  # output 0 is PAFs
        paf = cv2.resize(paf, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                         interpolation=cv2.INTER_CUBIC)
        paf = paf[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        paf = cv2.resize(paf, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_CUBIC)
        #
        heatmap_avg = heatmap_avg + heatmap / len(multiplier)
        paf_avg = paf_avg + paf / len(multiplier)

    return heatmap_avg, paf_avg

def find_peaks(heatmap_avg, params):
    all_peaks = []
    peak_counter = 0

    for part in range(18):
        map_ori = heatmap_avg[:, :, part]
        map = gaussian_filter(map_ori, sigma=3)  # TODO: fintune the sigma

        map_up = np.zeros(map.shape)
        map_up[1:, :] = map[:-1, :]
        map_down = np.zeros(map.shape)  # todo： NMS with a sliding window of 3*3
        map_down[:-1, :] = map[1:, :]
        map_left = np.zeros(map.shape)
        map_left[:, 1:] = map[:, :-1]
        map_right = np.zeros(map.shape)
        map_right[:, :-1] = map[:, 1:]

        map_left_up = np.zeros(map.shape)
        map_left_up[1:, :] = map_left[:-1, :]
        map_right_up = np.zeros(map.shape)
        map_right_up[1:, :] = map_right[:-1, :]
        map_left_down = np.zeros(map.shape)
        map_left_down[:-1, :] = map_left[1:, :]
        map_right_down = np.zeros(map.shape)
        map_right_down[:-1, :] = map_right[1:, :]

        peaks_binary = np.logical_and.reduce((map >= map_left, map >= map_right,
                                              map >= map_up, map >= map_down, map >= map_right_up,
                                              map >= map_right_down,
                                              map >= map_left_up, map >= map_left_down,
                                              map > params['thre1']))  # fixme: finetue it

        peaks = list(zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0]))  # note reverse
        peaks_with_score = [x + (map_ori[x[1], x[0]],) for x in peaks]
        id = range(peak_counter, peak_counter + len(peaks))
        peaks_with_score_and_id = [peaks_with_score[i] + (id[i],) for i in range(len(id))]

        all_peaks.append(peaks_with_score_and_id)
        peak_counter += len(peaks)

    return all_peaks

def find_connections(all_peaks, paf_avg, image_width, params):
    connection_all = []
    special_k = []

    for k in range(len(mapIdx)):
        score_mid = paf_avg[:, :, mapIdx[k][0] // 2]
        candA = all_peaks[limbSeq[k][0]]
        candB = all_peaks[limbSeq[k][1]]
        nA = len(candA)
        nB = len(candB)
        indexA, indexB = limbSeq[k]
        if (nA != 0 and nB != 0):
            connection_candidate = []
            for i in range(nA):
                for j in range(nB):
                    vec = np.subtract(candB[j][:2], candA[i][:2])
                    norm = math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
                    mid_num = max(int(norm), 10)  # todo: tune it?
                    # failure case when 2 body parts overlaps
                    if norm == 0:
                        # https://github.com/ZheC/Realtime_Multi-Person_Pose_Estimation/issues/54
                        continue

                    startend = list(zip(np.linspace(candA[i][0], candB[j][0], num=mid_num),
                                        np.linspace(candA[i][1], candB[j][1], num=mid_num)))

                    limb_response = np.array([score_mid[int(round(startend[I][1])), int(round(startend[I][0]))] \
                                              for I in range(len(startend))])
                    score_midpts = limb_response

                    score_with_dist_prior = sum(score_midpts) / len(score_midpts) + min(0.5 * image_width / norm - 1, 0)
                    # https://github.com/michalfaber/keras_Realtime_Multi-Person_Pose_Estimation/issues/48

                    criterion1 = len(np.nonzero(score_midpts > params['thre2'])[0]) >= params['connect_ration'] * len(
                        score_midpts)  # todo　改成了0.8
                    # fixme: tune 手动调整, 0.7 or 0.8
                    criterion2 = score_with_dist_prior > 0
                    if criterion1 and criterion2:
                        connection_candidate.append([i, j, score_with_dist_prior, norm,
                                                     0.5 * score_with_dist_prior + 0.25 * candA[i][2] + 0.25 * candB[j][
                                                         2]])

            connection_candidate = sorted(connection_candidate, key=lambda x: x[4], reverse=True)  # todo: sort by what

            connection = np.zeros((0, 6))
            for c in range(len(connection_candidate)):
                i, j, s, limb_len = connection_candidate[c][0:4]
                if (i not in connection[:, 3] and j not in connection[:, 4]):
                    connection = np.vstack([connection, [candA[i][3], candB[j][3], s, i, j, limb_len]])
                    if (len(connection) >= min(nA, nB)):
                        break
            connection_all.append(connection)
        else:
            special_k.append(k)
            connection_all.append([])

    return connection_all, special_k

def find_people(connection_all, special_k, all_peaks, params):
    # last number in each row is the total parts number of that person
    # the second last number in each row is the score of the overall configuration
    subset = -1 * np.ones((0, 20, 2))
    candidate = np.array([item for sublist in all_peaks for item in sublist])
    for k in range(len(mapIdx)):

        if k not in special_k:
            partAs = connection_all[k][:, 0]
            partBs = connection_all[k][:, 1]
            indexA, indexB = np.array(limbSeq[k])

            for i in range(len(connection_all[k])):

                found = 0
                subset_idx = [-1, -1]
                for j in range(len(subset)):
                    if subset[j][indexA][0].astype(int) == (partAs[i]).astype(int) or subset[j][indexB][0].astype(
                            int) == partBs[i].astype(int):

                        if found >= 2:
                            print('************ error occurs! 3 joints sharing have been found  *******************')
                            continue
                        subset_idx[found] = j
                        found += 1

                if found == 1:
                    j = subset_idx[0]

                    if subset[j][indexB][0].astype(int) == -1 and \
                            params['len_rate'] * subset[j][-1][1] > connection_all[k][i][-1]:

                        subset[j][indexB][0] = partBs[i]
                        subset[j][indexB][1] = connection_all[k][i][2]
                        subset[j][-1][0] += 1
                        # last number in each row is the total parts number of that person

                        subset[j][-2][0] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]
                        subset[j][-1][1] = max(connection_all[k][i][-1], subset[j][-1][1])

                    elif subset[j][indexB][0].astype(int) != partBs[i].astype(int):
                        if subset[j][indexB][1] >= connection_all[k][i][2]:
                            pass

                        else:
                            if params['len_rate'] * subset[j][-1][1] <= connection_all[k][i][-1]:
                                continue
                            subset[j][-2][0] -= candidate[subset[j][indexB][0].astype(int), 2] + subset[j][indexB][1]

                            subset[j][indexB][0] = partBs[i]
                            subset[j][indexB][1] = connection_all[k][i][2]
                            subset[j][-2][0] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]

                            subset[j][-1][1] = max(connection_all[k][i][-1], subset[j][-1][1])
                    else:
                        pass

                elif found == 2:  # if found 2 and disjoint, merge them
                    j1, j2 = subset_idx

                    membership1 = ((subset[j1][..., 0] >= 0).astype(int))[:-2]
                    membership2 = ((subset[j2][..., 0] >= 0).astype(int))[:-2]
                    membership = membership1 + membership2
                    if len(np.nonzero(membership == 2)[0]) == 0:  # if found 2 and disjoint, merge them

                        min_limb1 = np.min(subset[j1, :-2, 1][membership1 == 1])
                        min_limb2 = np.min(subset[j2, :-2, 1][membership2 == 1])
                        min_tolerance = min(min_limb1, min_limb2)

                        if connection_all[k][i][2] < params['connection_tole'] * min_tolerance or params['len_rate'] * \
                                subset[j1][-1][1] <= connection_all[k][i][-1]:
                            # todo: finetune the tolerance of connection
                            continue  #

                        subset[j1][:-2][...] += (subset[j2][:-2][...] + 1)
                        subset[j1][-2:][:, 0] += subset[j2][-2:][:, 0]

                        subset[j1][-2][0] += connection_all[k][i][2]
                        subset[j1][-1][1] = max(connection_all[k][i][-1], subset[j1][-1][1])
                        subset = np.delete(subset, j2, 0)

                    else:
                        if connection_all[k][i][0] in subset[j1, :-2, 0]:
                            c1 = np.where(subset[j1, :-2, 0] == connection_all[k][i][0])
                            c2 = np.where(subset[j2, :-2, 0] == connection_all[k][i][1])
                        else:
                            c1 = np.where(subset[j1, :-2, 0] == connection_all[k][i][1])
                            c2 = np.where(subset[j2, :-2, 0] == connection_all[k][i][0])

                        c1 = int(c1[0])
                        c2 = int(c2[0])
                        assert c1 != c2, "an candidate keypoint is used twice, shared by two people"

                        if connection_all[k][i][2] < subset[j1][c1][1] and connection_all[k][i][2] < subset[j2][c2][1]:
                            continue  # the trick here is useful

                        small_j = j1
                        big_j = j2
                        remove_c = c1

                        if subset[j1][c1][1] > subset[j2][c2][1]:
                            small_j = j2
                            big_j = j1
                            remove_c = c2

                        subset[small_j][-2][0] -= candidate[subset[small_j][remove_c][0].astype(int), 2] + \
                                                  subset[small_j][remove_c][1]
                        subset[small_j][remove_c][0] = -1  # todo
                        subset[small_j][remove_c][1] = -1
                        subset[small_j][-1][0] -= 1

                # if find no partA in the subset, create a new subset
                elif not found and k < 24:
                    # Fixme: 原始的时候是18,因为我加了limb，所以是24,因为真正的limb是0~16，最后两个17,18是额外的不是limb
                    # FIXME: 但是后面画limb的时候没有把鼻子和眼睛耳朵的连线画上，要改进
                    row = -1 * np.ones((20, 2))
                    row[indexA][0] = partAs[i]
                    row[indexA][1] = connection_all[k][i][2]
                    row[indexB][0] = partBs[i]
                    row[indexB][1] = connection_all[k][i][2]
                    row[-1][0] = 2
                    row[-1][1] = connection_all[k][i][-1]
                    row[-2][0] = sum(candidate[connection_all[k][i, :2].astype(int), 2]) + connection_all[k][i][2]
                    row = row[np.newaxis, :, :]
                    subset = np.concatenate((subset, row), axis=0)
        # todo: solve the unmathced keypoint?
    # delete some rows of subset which has few parts occur
    deleteIdx = []
    for i in range(len(subset)):
        if subset[i][-1][0] < 2 or subset[i][-2][0] / subset[i][-1][0] < 0.45:
            deleteIdx.append(i)
    subset = np.delete(subset, deleteIdx, axis=0)

    return subset, candidate

def process(input_image, params, model, model_params, heat_layers, paf_layers):
    oriImg = cv2.imread(input_image)  # B,G,R order
    # print(input_image)
    heatmap_avg, paf_avg = predict(oriImg, params, model, model_params, heat_layers, paf_layers)

    all_peaks = find_peaks(heatmap_avg, params)
    connection_all, special_k = find_connections(all_peaks, paf_avg, oriImg.shape[0], params)
    subset, candidate = find_people(connection_all, special_k, all_peaks, params)

    keypoints = []
    for s in subset[..., 0]:
        keypoint_indexes = s[:18]
        person_keypoint_coordinates = []
        for index in keypoint_indexes:
            if index == -1:
                X, Y = 0, 0
            else:
                X, Y = candidate[index.astype(int)][:2]
            person_keypoint_coordinates.append((X, Y))
        person_keypoint_coordinates_coco = [None] * 17

        for dt_index, gt_index in dt_gt_mapping.items():
            if gt_index is None:
                continue
            person_keypoint_coordinates_coco[gt_index] = person_keypoint_coordinates[dt_index]

        keypoints.append((person_keypoint_coordinates_coco, 1 - 1.0 / s[18]))
        # s[18] is the score, s[19] is the number of keypoint
    return keypoints

def get_image_name(coco, image_id):
    return coco.imgs[image_id]['file_name']

def predict_many(coco, images_directory, validation_ids, params, model, model_params, heat_layers, paf_layers):
    assert (not set(validation_ids).difference(set(coco.getImgIds())))

    keypoints = {}
    for image_id in tqdm.tqdm(validation_ids):
        image_name = get_image_name(coco, image_id)
        image_name = os.path.join(images_directory, image_name)
        keypoints[image_id] = process(image_name, dict(params), model, dict(model_params), heat_layers+1, paf_layers)
        # fixme: heat_layers + 1 if you use background keypoint  !!!
    return keypoints

def format_results(keypoints, resFile):
    format_keypoints = []
    # todo: do we need to sort the detections by scores before evaluation ?
    for image_id, people in keypoints.items():
        for keypoint_list, score in people:
            format_keypoint_list = []
            for x, y in keypoint_list:
                for v in [int(x), int(y), 1 if x > 0 or y > 0 else 0]:
                    format_keypoint_list.append(v)

            format_keypoints.append({
                "image_id": image_id,
                "category_id": 1,
                "keypoints": format_keypoint_list,
                "score": score,
            })

    json.dump(format_keypoints, open(resFile, 'w'))

def validation(model, dump_name, validation_ids=None, dataset='val2017'):
    annType = 'keypoints'
    prefix = 'person_keypoints'

    dataDir = 'dataset/coco/link2coco2017'

    # # #############################################################################
    # 在验证集上测试代码
    annFile = '%s/annotations/%s_%s.json' % (dataDir, prefix, dataset)
    print(annFile)
    cocoGt = COCO(annFile)

    if validation_ids == None:   # todo: we can set the validataion image ids here  !!!!!!
        validation_ids = cocoGt.getImgIds()
    # # #############################################################################

    resFile = '%s/results/%s_%s_%s100_results.json'
    resFile = resFile % (dataDir, prefix, dataset, dump_name)
    print('the path of detected keypoint file is: ', resFile)
    os.makedirs(os.path.dirname(resFile), exist_ok=True)

    keypoints = predict_many(cocoGt, os.path.join(dataDir, dataset), validation_ids, params, model, model_params,
                             config.heat_layers, config.paf_layers)
    format_results(keypoints, resFile)
    cocoDt = cocoGt.loadRes(resFile)
    cocoEval = COCOeval(cocoGt, cocoDt, annType)
    cocoEval.params.imgIds = validation_ids
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()
    return cocoEval

def per_image_scores(eval_result):
    def convert_match_to_score(match):
        matches = match['gtMatches'][:, np.array(match['gtIgnore']) == 0]
        scores = {
            'image_id': match['image_id'],
            'gt_person_count': matches.shape[1],
        }

        for i in range(matches.shape[0]):
            okp_threshold = eval_result.params.iouThrs[i]
            scores['matched_%.2f' % okp_threshold] = sum(matches[i, :] != 0)
        scores['average'] = np.mean(np.sum(matches != 0, axis=1)) / scores['gt_person_count']

        return scores

    evalImgs = eval_result.evalImgs
    scores = [convert_match_to_score(image_match) for image_match in evalImgs if image_match is not None]

    return pd.DataFrame(scores)

if __name__ == "__main__":
    config = GetConfig('Canonical')
    params, model_params = config_reader(figstr='testing/config')
    from posenet.mymodel3 import get_testing_model

    # with tf.device("/cpu:0"):
    #     model_single = get_testing_model(np_branch1=config.paf_layers, np_branch2=config.heat_layers, stages=1)
    # model = multi_gpu_model(model_single, gpus=2)
    model = get_testing_model(np_branch1=config.paf_layers, np_branch2=config.heat_layers+1, stages=3)  # fixme: background + 1
    training_dir = './training/'
    trained_models = [
        'weights'
        # 'weights-cpp-lr'
        # 'weights-python-last',
    ]

    optimal_epoch_loss = 'val_weight_stage6_L1_loss'
    weights_path = 'training/cpu_weights/cpu_model.h5'
    # '../model/keras/model.h5'    # orginal weights converted from caffe
    model.load_weights(weights_path)
    eval_result_original = validation(model, dump_name='pose_focal_4scale', dataset='val2017')  # val2017

    print('over!')