import deepdish as dd
import lasagne
import numpy as np
import theano
import theano.tensor as T
from gensim.models import Doc2Vec

from ..utils import MovieIdToDocVec, UserIdToDocVec

class BaseSideInfoModel(object):
    def step(self, input, label, lr):
        raise NotImplementedError

class NNSideInfoModel(BaseSideInfoModel):
    """Neural Network"""
    def __init__(self, dim, reg_lambda=None, cosine_lambda=None, feature_vec_dict=None, topn=None):
        self.vector_dict = feature_vec_dict
        self.topn = topn

        input_vec = T.matrix(dtype=theano.config.floatX)
        label_vec = T.vector(dtype=theano.config.floatX)

        network = lasagne.layers.InputLayer(shape=(None, dim[0]), input_var=input_vec)
        for i in range(1, len(dim)-1):
            n_out = dim[i]
            network = lasagne.layers.DenseLayer(network, num_units=n_out)
        network = lasagne.layers.DenseLayer(network, num_units=dim[-1], nonlinearity=lasagne.nonlinearities.identity)
        label_predict = lasagne.layers.get_output(network)

        self.predict = theano.function([input_vec], label_predict, allow_input_downcast=True)
        self.param_values = lasagne.layers.get_all_param_values(network)
        self.network = network

        if reg_lambda is not None and feature_vec_dict is not None:
            loss = ((label_vec - label_predict)**2).sum()
            # cosine similarity
            loss += cosine_lambda * (1.0 - (T.sum(label_vec * label_predict)) / (T.sqrt(T.sum(T.square(label_vec))) * T.sqrt(T.sum(T.square(label_predict)))))
            loss += reg_lambda * lasagne.regularization.regularize_network_params(network, lasagne.regularization.l2)

            d_input_vec = theano.grad(loss, input_vec)
            lr = T.scalar('lr', dtype=theano.config.floatX)

            # create parameter update expressions
            params = lasagne.layers.get_all_params(network, trainable=True)
            updates = lasagne.updates.adagrad(loss, params, lr)

            self._get_loss = theano.function([input_vec, label_vec], loss, allow_input_downcast=True)

            self._gradient_step = theano.function([input_vec, label_vec, lr], outputs=[loss, d_input_vec], updates=updates, allow_input_downcast=True)

    def step(self, input_vec, user_or_item_id, lr):
        label_vec = self.vector_dict[user_or_item_id]
        loss, grad = self._gradient_step(input_vec, label_vec, lr)

        if self.topn:
            imdb_id = self.vector_dict.movie_to_imdb[user_or_item_id]
            key = '{}.txt'.format(imdb_id)
            topn_similar = self.vector_dict.store.most_similar(key, topn=self.topn)

            for k, s in topn_similar:
                l, g = self._gradient_step(input_vec, self.vector_dict.store[k], lr)
                loss += l
                grad += g

            loss /= (self.topn+1)
            grad /= (self.topn+1)
        return loss, grad

    def set_params(self, param_values):
        params_float32 = map(lambda arr: arr.astype(np.float32), param_values)
        lasagne.layers.set_all_param_values(self.network, params_float32)

    def get_loss(self, input_vec, user_or_item_id):
        label_vec = self.vector_dict[user_or_item_id]
        return self._get_loss(input_vec, label_vec)

class ATSideInfoModel(BaseSideInfoModel):
    """Affine Transformation"""
    def __init__(self, dim, reg_lambda=None, cosine_lambda=None, feature_vec_dict=None):
        self.vector_dict = feature_vec_dict

        nb_latent_f = dim[0]
        nb_d2v_features = dim[-1]

        self.weights = np.random.uniform(low=-0.001, high=0.001, size=(nb_d2v_features, nb_latent_f + 1))  # side info weight matrix
        self.ada_cache_weights = np.zeros_like(self.weights)
        self.reg_lambda = reg_lambda
        self.cosine_lambda = cosine_lambda

    def step(self, input_vec, user_or_item_id, lr):
        label_vec = self.vector_dict[user_or_item_id]

        bias = np.array([1]).reshape((-1, 1))
        biased_input_vec = np.hstack([bias, input_vec])

        label_predict = np.dot(self.weights, biased_input_vec.T).reshape((-1,))  # vector
        error = label_vec - label_predict
        loss = np.sum(np.square(error))
        loss += self.reg_lambda * np.sqrt(np.sum(np.square(self.weights)))
        #loss += self.cosine_lambda * (1.0 - (np.sum(label_vec * label_predict) /
        #                                     (np.sqrt(np.sum(np.square(label_vec))) * np.sqrt(np.sum(np.square(label_predict))))))

        delta_weights = np.dot(error.reshape((-1, 1)), biased_input_vec.reshape((1, -1))) - self.reg_lambda * self.weights
        delta_input_vec = np.dot(self.weights[:, 1:].T, error)  # without bias

        self.ada_cache_weights += delta_weights ** 2

        # update parameters
        self.weights += lr * delta_weights / (np.sqrt(self.ada_cache_weights) + 1e-6)

        return loss, delta_input_vec

    def predict(self, input_vec):
        bias = np.array([1]).reshape((-1, 1))
        biased_input_vec = np.hstack([bias, input_vec])

        label_predict = np.dot(self.weights, biased_input_vec.T).reshape((-1,))  # vector
        return label_predict

def create_si_item_model(config, ratings):
    if 'si_item_d2v_model' in config:
        d2v_model = Doc2Vec.load(config['si_item_d2v_model'])
        feature_vec_dict = MovieIdToDocVec(d2v_model.docvecs, ratings)
    elif 'si_item_vector_dict' in config:
        feature_vec_dict = dd.io.load(config['si_item_vector_dict'])

    si_item_nn = list(config['si_item_nn_hidden'])
    si_item_nn.insert(0, config['nb_latent_f'])
    si_item_nn.append(int(feature_vec_dict[config['si_item_valid_id']].shape[0]))
    config['si_item_nn'] = si_item_nn
    #si_item_model = ATSideInfoModel(config['si_item_nn'], config['si_item_reg_lambda'], feature_vec_dict=feature_vec_dict)
    si_item_model = NNSideInfoModel(config['si_item_nn'],
                                    config['si_item_reg_lambda'],
                                    config['si_item_cosine_lambda'],
                                    feature_vec_dict,
                                    config['si_item_topn_similar'])
    return si_item_model, config

def create_si_user_model(config, ratings):
    if 'si_user_d2v_model' in config:
        d2v_model = Doc2Vec.load(config['si_user_d2v_model'])
        feature_vec_dict = UserIdToDocVec(d2v_model.docvecs, ratings)
    elif 'si_user_vector_dict' in config:
        feature_vec_dict = dd.io.load(config['si_user_vector_dict'])

    si_user_nn = list(config['si_user_nn_hidden'])
    si_user_nn.insert(0, config['nb_latent_f'])
    si_user_nn.append(int(feature_vec_dict[config['si_user_valid_id']].shape[0]))
    config['si_user_nn'] = si_user_nn
    si_user_model = NNSideInfoModel(config['si_user_nn'],
                                    config['si_user_reg_lambda'],
                                    config['si_user_cosine_lambda'],
                                    feature_vec_dict)
    return si_user_model, config


from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render
from django.template import Template,Context
from Familiares.models import Familiar

# Create your views here.
def listado_familiares(request):
    familiar1=Familiar(nombre='Nicolas',parentesco='hermano',dni=30123456,fecha_nacimiento='1980-08-11')
    familiar1.save()

    familiar2=Familiar(nombre='Carolina',parentesco='hermana',dni=31256987,fecha_nacimiento='1980-10-25')
    familiar2.save()

    familiar3=Familiar(nombre='Roberto',parentesco='padre',dni=10235478,fecha_nacimiento='1956-12-20')
    familiar3.save()

    familiar4=Familiar(nombre='Beatriz',parentesco='madre',dni=11247985,fecha_nacimiento='1950-04-11')
    familiar4.save()

    plantilla=loader.get_template('template.html')
    familia= {'familia':[familiar1,familiar2,familiar3,familiar4]}
    vista = plantilla.render(familia)
    return HttpResponse(vista)


#描述：requests模块post爬取 百度翻译接口
import requests
import json

post_url = 'https://fanyi.baidu.com/extendtrans'

en = input('请输入你要翻译的内容：')
post_data = {
    'query': en,
    'from': 'zh',
    'to': 'en'
}
headers = {"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"}

r = requests.post(post_url, headers=headers, data=post_data)

dict_response = json.loads(r.content.decode())
print('要翻译的内容是',en,'翻译为', dict_response['data']['st_tag'])


import io_wrap.reader as reader
import common.classes as classes
import numpy as np
import matplotlib.pyplot as plt

correlations = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
indirects = []
variations = []
files_list = []
path = "/Users/egozi/Documents/IDC/Thesis/src/python/thesis/basic_random_choice_model/data/initial_correlation_variable"

for i in correlations:
    files_list = np.array(reader.read_files_in_directory(path, title=classes.StatsTitle(traits_correlation=i)))
    mean_indirects = []
    for file in files_list:
        mean_indirects.append(np.mean(np.array([float(file[j][2]) for j in range(int(len(file) - len(file) * 0.1), len(file), 1)])))
    indirects.append(np.mean(mean_indirects))
    variations.append(np.var(mean_indirects))

optimal_indicator = float(files_list[0][0][5])
differences = np.abs(np.array([optimal_indicator for j in correlations]) - np.array(indirects))

plt.xlabel(r'$\chi$ - traits correlation')
plt.ylabel(r'$\hat{A} - \bar{B}$')
plt.plot(correlations, differences)

plt.figure()
plt.xlabel(r'$\chi$ - traits correlation')
plt.ylabel('Var(B)')
plt.plot(correlations, variations)

plt.show()


import requests
import json
import pandas as pd
import os

def load_schools(length):
    school_list = []
    with open('schools.txt', 'r') as f:
        for row in f:
            school_list.append(row.rstrip("\n"))
    school_list = school_list[0:length]
    return school_list

def load_positions():
    position_list = []
    with open('positions.txt', 'r') as f:
        for row in f:
            position_list.append(row.rstrip("\n"))
    return position_list

def get_schools():
    url = "https://api.collegefootballdata.com/conferences"
    params = {"searchTerm": ""}
    response = requests.get(url, params=params)
    c = json.loads(response.content)
    for conference in c:
        print(conference)
        params = {"conference": conference['name']}
        response = requests.get(url, params=params)
        teams = json.loads(response)
        for t in teams:
            print(t)
    #result = list(map(lambda x: x['school'], dictionary))
    #with open('schools.txt', 'w') as f:
    #    for item in result:
    #        f.write('%s\n' % item)

def write_list_to_file(l, file_name):
    with open(file_name, 'w') as f:
        for item in l:
            f.write('%s\n' % item)

def get_positions():
    player_dict = {}
    url = "https://api.collegefootballdata.com/player/search"
    teams = ["Georgia", "Alabama", "Florida", "Pittsburgh", "Baylor", "Army", "Arizona", "BYU", "Iowa Wesleyan",
             "Ithaca"]  # has to be a wide enough net
    # a through c in alphabet
    letters = [chr(i) for i in range(ord('a'), ord('c') + 1)]

    positions = set()
    for team in teams:
        for letter in letters:
            params = {"team": team, "searchTerm": letter}
            response = requests.get(url, params=params)
            dictionary = json.loads(response.content)
            position_list = set(list(map(lambda x: x['position'], dictionary)))
            positions = positions.union(position_list)

    positions = list(positions)

    with open('positions.txt', 'w') as f:
        for item in positions:
            f.write('%s\n' % item)

def get_players(schools, years):

    if os.path.exists("raw_players.csv"):
        player_df = pd.read_csv("raw_players.csv")
    else:
        column_names = ["id", "first_name", "last_name", "weight", "height", "jersey", "year",
                        "position", "home_city", "home_state", "home_country", "school"]
        player_df = pd.DataFrame(columns=column_names)

    url = "https://api.collegefootballdata.com/roster"
    for school in schools:
        for year in years:
            params = {"team": school, "year": year}
            response = requests.get(url, params=params)
            temp_dict = json.loads(response.content)
            for value in temp_dict:
                value['school'] = school
                value['year'] = year
                df = pd.DataFrame([value], columns=value.keys())
                player_df = player_df.append(df, ignore_index=True)

    columns_order = ["id", "first_name", "last_name", "school", "year", "weight", "height", "jersey",
                     "position", "home_city", "home_state", "home_country"]

    player_df = player_df[columns_order]
    player_df = player_df[player_df["id"].notna()]
    player_df['id'] = player_df['id'].astype(int)
    player_df = player_df.where(player_df['id'] > 0)  # remove negative values (teams)
    player_df = player_df.sort_values('id', ascending=False)
    player_df.drop_duplicates(subset="id", keep="last", inplace=True)
    player_df = player_df[player_df["id"].notna()]
    player_df = player_df[player_df["last_name"].notna()]
    player_df['id'] = player_df['id'].astype(int)
    player_df['year'] = player_df['year'].astype(int)
    player_df.to_csv("raw_players.csv", index=False)

    return None  # end function

years = list(range(2010, 2012, 1))
print(years)
get_players(load_schools(738), years)


import json
import torch
from torch import nn
from transformers import RobertaModel, RobertaTokenizer

JSON_CONTENT_TYPE = 'application/json'
PRE_TRAINED_MODEL_NAME = 'roberta-base'
CLASS_NAMES = ['negative', 'neutral', 'positive']

class SentimentClassifier(nn.Module):

    def __init__(self, n_classes):
        super(SentimentClassifier, self).__init__()
        self.roberta = RobertaModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
        self.drop = nn.Dropout(p=0.3)
        self.out = nn.Linear(self.roberta.config.hidden_size, n_classes)

    def forward(self, input_ids, attention_mask):
        _, pooled_output = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
        output = self.drop(pooled_output)
        return self.out(output)

# defines the model
def model_fn(model_dir):
    model_path = f'{model_dir}/model.pth'
    model = SentimentClassifier(len(CLASS_NAMES))
    model.load_state_dict(torch.load(model_path, map_location='cpu'))
    return model

# Perform prediction on the deserialized object, with the loaded model
def predict_fn(input_data, model):
    MAX_LEN = 160
    tokenizer = RobertaTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)

    review_text = input_data['text']

    encoded_review = tokenizer.encode_plus(
      review_text,
      max_length=MAX_LEN,
      add_special_tokens=True,
      return_token_type_ids=False,
      pad_to_max_length=True,
      return_attention_mask=True,
      return_tensors='pt',
    )
    input_ids = encoded_review['input_ids']
    attention_mask = encoded_review['attention_mask']

    output = model(input_ids, attention_mask)
    _, prediction = torch.max(output, dim=1)
    return CLASS_NAMES[prediction]

def input_fn(serialized_input_data, content_type=JSON_CONTENT_TYPE):
    if content_type == JSON_CONTENT_TYPE:
        input_data = json.loads(serialized_input_data)
        return input_data
    else:
        pass

def output_fn(prediction_output, accept=JSON_CONTENT_TYPE):
    if accept == JSON_CONTENT_TYPE:
        return json.dumps(prediction_output), accept
    raise Exception('Requested unsupported ContentType in Accept: ' + accept)


"""empty message

Revision ID: cdbf41aea838
Revises:
Create Date: 2019-03-09 19:54:02.333712

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'cdbf41aea838'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('areas',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('area_name', sa.String(), nullable=False),
    sa.Column('label', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('candidates',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('candname', sa.String(), nullable=False),
    sa.Column('label', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=10), nullable=False),
    sa.Column('full_name', sa.String(length=120), nullable=False),
    sa.Column('password', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('username')
    )
    op.create_table('candidate_areas',
    sa.Column('candidate_id', sa.Integer(), nullable=False),
    sa.Column('area_id', sa.Integer(), nullable=False),
    sa.Column('tfidf', sa.Float(), nullable=False),
    sa.ForeignKeyConstraint(['area_id'], ['areas.id'], ),
    sa.ForeignKeyConstraint(['candidate_id'], ['candidates.id'], ),
    sa.PrimaryKeyConstraint('candidate_id', 'area_id')
    )
    op.create_table('user_areas',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('area_id', sa.Integer(), nullable=False),
    sa.Column('rate', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['area_id'], ['areas.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('user_id', 'area_id')
    )
    op.create_table('user_candidates',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('candidate_id', sa.Integer(), nullable=False),
    sa.Column('rate', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['candidate_id'], ['candidates.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('user_id', 'candidate_id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('user_candidates')
    op.drop_table('user_areas')
    op.drop_table('candidate_areas')
    op.drop_table('users')
    op.drop_table('candidates')
    op.drop_table('areas')
    # ### end Alembic commands ###


from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome(executable_path=r"C:\Users\Manoj\Desktop\Python - Selenium Practice\Drivers\chromedriver.exe")

driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')

source_elem = driver.find_element_by_xpath('//*[@id="box6"]')
target_elem = driver.find_element_by_xpath('//*[@id="box106"]')

actions = ActionChains(driver)

actions.drag_and_drop(source_elem,target_elem).perform()


"""empty message

Revision ID: 48edaf71b1c0
Revises: fbb243905463
Create Date: 2023-04-11 07:11:26.643501

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '48edaf71b1c0'
down_revision = 'fbb243905463'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('pars_config',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number_pars', sa.Integer(), nullable=True),
    sa.Column('created_ad', sa.TIMESTAMP(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

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


import csv
import numpy as np
import random
import Levenshtein as ls
from py_stringmatching import GeneralizedJaccard, Soundex

from data import Dataset

class EquivalenceClasses:
    def __init__(self, path):
        self.dataset = Dataset('train.csv')
        self.contents = []
        self.gj = GeneralizedJaccard(sim_func=Soundex().get_raw_score)

        with open(path, 'r', encoding='utf-8') as f:
            reader = csv.reader(f)
            contents = []
            for line in reader:
                elements = []
                for element in line[1:]:
                    elements.append(self.dataset.rows[int(element)])
                contents.append(elements)
            self.contents = [content for content in contents if len(content) != 0]

    def calcEquivalentSsn(self):
        print('ssn-eq')
        ssn_info = []
        for content in self.contents:
            for i in range(len(content) - 2):
                ssn1 = content[i].__getitem__('ssn')
                if ssn1 != '000000000':
                    for j in range(i + 1, len(content) - 1):
                        ssn2 = content[j].__getitem__('ssn')
                        if ssn2 != '000000000':
                            ssn_info.append(ls.ratio(ssn1, ssn2))
        narray = np.array(ssn_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcInequivalentSsn(self):
        print('ssn-neq')
        ssn_info = []
        random_array = self.generateRandomArray(len(self.contents) - 2, 100)
        for i in random_array:
            ssn1 = self.contents[i][0].__getitem__('ssn')
            if ssn1 != '000000000':
                for j in range(i + 1, len(self.contents) - 1):
                    ssn2 = self.contents[j][0].__getitem__('ssn')
                    if ssn2 != '000000000':
                        ssn_info.append(ls.ratio(ssn1, ssn2))
        narray = np.array(ssn_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcEquivalentName(self):
        print('name-eq')
        name_info = []

        for content in self.contents:
            for i in range(len(content) - 2):
                name1 = [content[i].__getitem__('fname'), content[i].__getitem__('lname')]
                if len(content[i].__getitem__('minit')) != 0:
                    name1.append(content[i].__getitem__('minit'))
                for j in range(i + 1, len(content) - 1):
                    name2 = [content[j].__getitem__('fname'), content[j].__getitem__('lname')]
                    if len(content[j].__getitem__('minit')) != 0:
                        name2.append(content[j].__getitem__('minit'))
                    name_info.append(self.gj.get_sim_score(name1, name2))
        narray = np.array(name_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcInequivalentName(self):
        print('name-neq')
        name_info = []
        random_array = self.generateRandomArray(len(self.contents) - 2, 10)
        print(random_array)
        for i in random_array:
            name1 = [self.contents[i][0].__getitem__('fname'), self.contents[i][0].__getitem__('lname')]
            if len(self.contents[i][0].__getitem__('minit')) != 0:
                name1.append(self.contents[i][0].__getitem__('minit'))
            print(name1)
            for j in range(i + 1, len(self.contents) - 1):
                name2 = [self.contents[j][0].__getitem__('fname'), self.contents[j][0].__getitem__('lname')]
                if len(self.contents[j][0].__getitem__('minit')) != 0:
                    name2.append(self.contents[j][0].__getitem__('minit'))
                name_info.append(self.gj.get_sim_score(name1, name2))
        narray = np.array(name_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcEquivalentStadd(self):
        print('stadd-eq')
        stadd_info = []
        for content in self.contents:
            for i in range(len(content) - 2):
                stadd1 = content[i].__getitem__('stadd').split(' ')[0]
                for j in range(i + 1, len(content) - 1):
                    stadd2 = content[j].__getitem__('stadd').split(' ')[0]
                    stadd_info.append(ls.ratio(stadd1, stadd2))
        narray = np.array(stadd_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcInequivalentStadd(self):
        print('stadd-neq')
        stadd_info = []
        random_array = self.generateRandomArray(len(self.contents) - 2, 100)
        for i in random_array:
            stadd1 = self.contents[i][0].__getitem__('stadd').split(' ')[0]
            for j in range(i + 1, len(self.contents) - 1):
                stadd2 = self.contents[j][0].__getitem__('stadd').split(' ')[0]
                stadd_info.append(ls.ratio(stadd1, stadd2))
        narray = np.array(stadd_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcEquivalentOtherAttributes(self, attr_name):
        print(attr_name + '-eq')
        attr_info = []
        for content in self.contents:
            for i in range(len(content) - 2):
                attr1 = content[i].__getitem__(attr_name)
                for j in range(i + 1, len(content) - 1):
                    attr2 = content[j].__getitem__(attr_name)
                    attr_info.append(ls.ratio(attr1, attr2))
        narray = np.array(attr_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def calcInequivalentOtherAttributes(self, attr_name):
        print(attr_name + '-neq')
        attr_info = []
        random_array = self.generateRandomArray(len(self.contents) - 2, 100)
        for i in random_array:
            attr1 = self.contents[i][0].__getitem__(attr_name)
            for j in range(i + 1, len(self.contents) - 1):
                attr2 = self.contents[j][0].__getitem__(attr_name)
                attr_info.append(ls.ratio(attr1, attr2))
        narray = np.array(attr_info)
        print('mean: ' + str(np.mean(narray)))
        print('max: ' + str(np.max(narray)))
        print('min: ' + str(np.min(narray)))

    def generateRandomArray(self, max_value, number):
        random_array = []
        for i in range(0, number):
            random_array.append(random.randint(0, max_value))
        return set(random_array)

def main():
    equivalenceClasses = EquivalenceClasses('bm_real.csv')

    equivalenceClasses.calcEquivalentSsn()
    equivalenceClasses.calcInequivalentSsn()

    equivalenceClasses.calcEquivalentName()
    equivalenceClasses.calcInequivalentName()

    equivalenceClasses.calcEquivalentOtherAttributes('stnum')
    equivalenceClasses.calcInequivalentOtherAttributes('stnum')

    equivalenceClasses.calcEquivalentStadd()
    equivalenceClasses.calcInequivalentStadd()

    equivalenceClasses.calcEquivalentOtherAttributes('apmt')
    equivalenceClasses.calcInequivalentOtherAttributes('apmt')

    equivalenceClasses.calcEquivalentOtherAttributes('city')
    equivalenceClasses.calcInequivalentOtherAttributes('city')

    equivalenceClasses.calcEquivalentOtherAttributes('state')
    equivalenceClasses.calcInequivalentOtherAttributes('state')

    equivalenceClasses.calcEquivalentOtherAttributes('zip')
    equivalenceClasses.calcInequivalentOtherAttributes('zip')

if __name__ == '__main__':
    main()


import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import networkx as nx
import os
from sklearn.model_selection import KFold, train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
import seaborn as sns
from CINN_customized_structure_v2 import CINN

### Load data
data_name = 'TEP_Faulty_Training_Sample.csv'
target_variable_name = 'xmeas_15'

df = pd.read_csv(data_name)
columns_names = list(df.columns)
node_labels = columns_names
target_node = list(df.columns).index(target_variable_name)

print('target variable name:',target_variable_name, ', node ID: ', target_node, node_labels)

# 'layers_of_intermediate_nodes': [[22,23,24,25,31],[5,7],[8],[6],[21],[10],[12,26,13],[28],[14],[16],[29],[18]]

causality_structure = { 'root_nodes':[0,1,2,3,4,9,11,20], 'intermediate_nodes':[5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31], 'leaf_nodes':[15,17,19,27,30,32],
'layers_of_intermediate_nodes': [[22,23,24,25,31],[5,7],[8],[6],[21],[10],[12,26,13],[28],[14],[16],[29],[18]] }

print(causality_structure)

### Load causality structure into neural network
scaler = StandardScaler()
scaler.fit(df)
data = scaler.transform(df)

train, test, _ , _ = train_test_split(data, data, test_size=0.2, random_state=42)

# without root_nodes (root_nodes are [4,7])
nodes_list = [5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31,15,17,19,27,30,32]

# To check target_node position in the nodes_list
target_node_index = nodes_list.index(target_node)  # 5

root_nodes = [0,1,2,3,4,9,11,20]
intermediate_nodes = [5,6,7,8,10,12,13,14,16,18,21,22,23,24,25,26,28,29,31]
leaf_nodes = [15,17,19,27,30,32]

kf = KFold(n_splits = 10, random_state = 2, shuffle=True)

X_test = test[:, root_nodes]
y_test_int = test[:, intermediate_nodes]
y_test_output = test[:, leaf_nodes]

batch_size = 256
no_epochs = 600
results = []
mse_best = []

for fold, (train_idx, val_idx) in enumerate(kf.split(train)):
    print(f"Fold {fold}:")

    X_train = train[train_idx][:, root_nodes]
    X_val = train[val_idx][:, root_nodes]

    y_train_int = train[train_idx][:, intermediate_nodes]
    y_train_output = train[train_idx][:, leaf_nodes]

    y_val_int = train[val_idx][:, intermediate_nodes]
    y_val_output = train[val_idx][:, leaf_nodes]

    model = CINN(causality_structure, target_node, batch_size, 'original')
    # plot the architecture of NN model
    tf.keras.utils.plot_model(model.model, to_file= 'architecture.png', show_shapes=True, show_layer_names=True)
    model.assign_data(X_train, y_train_int, y_train_output, X_val, y_val_int, y_val_output,
                      X_test, y_test_int, y_test_output)

    initial_weights = model.model.get_weights()
    optim = tf.keras.optimizers.Adam()
    model.train(no_epochs, optim)

    # Choose the best weights on the validation data from 10 fold results
    model.model.set_weights(model.best_weights)
    y = model.y_test_combined[:, target_node_index]
    y_pred = model.model.predict(model.X_test)[:, target_node_index]
    model.model.reset_states()

    mse = mean_squared_error(y, y_pred)
    results.append([fold, 'No_PCGrad', mse])
    print (fold, mse)

df_results = pd.DataFrame(results, columns = ['Run', 'Method', 'MSE'])

df_results.to_csv('results.csv', index = False)

print (df_results['MSE'].mean())
print (df_results['MSE'].std())


import pygame.midi
import sys
import time
import bridge

debug = not True

cm = ""

buttons = [*range(16, 23 + 1), 64]
dials = [*range(1, 8 + 1)]
dialdata = {}

named_buttons = {
    16: "STOP",
    17: "PLAY",
    18: "RECORD",
    19: "UP",
    20: "DOWN",
    21: "RIGHT",
    22: "LEFT",
    23: "SELECT",
    64: "SUSTAIN"
}

modes = ["manual", "hsv", "rgb", "hsi"]

stime = 0
position = 0
last_fill = [-1, -1, -1, -1]

def dial_handler(event):
    pos = event[2] / 127
    pos = pos if event[1] <= 4 else (pos * (1 / 127)) - (1 / 127 / 2)
    print(f"Dial {event[1]} at position {round(pos, 10)}")
    dialdata[event[1]] = pos

def send_data():
    global stime
    global last_fill
    global position
    global modes
    ptime = int(round(time.time() * 1000)) - stime
    if (cm and ptime > 10):
        fill = [-1, -1, -1, -1]
        for i in range(1, 4 + 1):
            fdata = dialdata.get(i, -1)
            if (fdata == -1):
                continue
            total = fdata + dialdata.get(i + 4, 0.)
            total = total if total <= 1 else 1
            total = total if total >= 0 else 0
            fill[i - 1] = total
        if (fill == last_fill):
            return
        last_fill = fill
        bridge.send_command(cm, "set", [modes[position], *fill])
        stime = int(round(time.time() * 1000))

def button_handler(event):
    state = "released"
    if (event[2] == 127):
        state = "pressed"
    if (event[1] in named_buttons.keys()):
        name = named_buttons[event[1]]
        print(f"{name} {state}")
        global position
        global modes
        if (name == "LEFT" and state == "released"):
            position = position - 1 if position - 1 >= 0 else len(modes) - 1
            print(f"Mode set to: {modes[position]}")
        elif (name == "RIGHT" and state == "released"):
            position = position + 1 if position + 1 < len(modes) else 0
            print(f"Mode set to: {modes[position]}")
    else:
        print(f"Button {event[1]} {state}")

def readInput(input_device):
    while True:
        if input_device.poll():
            event = input_device.read(1)[0][0]
            if (event[0] != 176):
                if (debug or event[0] not in [144,]):
                    print (event)
                continue
            if (event[1] in dials):
                dial_handler(event)
            elif (event[1] in buttons):
                button_handler(event)
            else:
                print (f"ID: {event[1]}, State: {round(event[2]/127,2)}")
        send_data()

def print_devices():
    devices = []
    for n in range(pygame.midi.get_count()):
        info = pygame.midi.get_device_info(n)
        if (info[2]):
            print (n,info[1])
            devices.append(n)
    return devices

def select_device(devices):
    while True:
        device_id = input("ID: ")
        try:
            device_id = int(device_id)
        except ValueError:
            continue
        if (device_id in devices):
            return device_id

def main():
    pygame.midi.init()
    devices = print_devices()
    device_id = select_device(devices)
    print(f"Opening device {device_id}:")
    my_input = pygame.midi.Input(device_id)
    try:
        readInput(my_input)
    except KeyboardInterrupt:
        my_input.close()
        print("Returning to device selection!")
        main()

if __name__ == '__main__':
    bc = bridge.open_listener()
    ip = bridge.await_controller(bc)
    cm = bridge.open_sender(ip)
    try:
        main()
    except KeyboardInterrupt:
        print("Exiting!")
        sys.exit()


import cv2
import numpy as np
import torch
import torch.nn as nn
import torchvision
import torchvision.models as model
from torchsummary import summary
from collections import OrderedDict
import torch.nn.init as init
from dataset import voc_loader as loader
import torchvision.transforms as transforms

class L2Norm(nn.Module):
    def __init__(self, n_channels, scale):
        super(L2Norm, self).__init__()
        self.n_channels = n_channels
        self.gamma = scale or None
        self.eps = 1e-10
        self.weight = nn.Parameter(torch.Tensor(self.n_channels))
        self.reset_parameters()

    def reset_parameters(self):
        init.constant_(self.weight, self.gamma)

    def forward(self, x):
        norm = x.pow(2).sum(dim=1, keepdim=True).sqrt()+self.eps
        #x /= norm
        x = torch.div(x, norm)
        out = self.weight.unsqueeze(0).unsqueeze(2).unsqueeze(3).expand_as(x) * x
        return out

class SSD(nn.Module):
    def __init__(self, pretrained=True):
        super(SSD, self).__init__()

        # module list 로 둔 이유는
        self.vgg = nn.ModuleList(vgg(cfg['300'], 3))
        # self.vgg = vgg(cfg['300'], 3)
        if pretrained:
            vgg_weights = torch.load('./vgg16_reducedfc.pth')
            self.vgg.load_state_dict(vgg_weights)

        conv_8_1 = nn.Conv2d(in_channels=1024, out_channels=256, kernel_size=1)
        conv_8_2 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, stride=2)
        conv_9_1 = nn.Conv2d(in_channels=512, out_channels=128, kernel_size=1)
        conv_9_2 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1, stride=2)
        conv_10_1 = nn.Conv2d(in_channels=256, out_channels=128, kernel_size=1)
        conv_10_2 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3,)
        conv_11_1 = nn.Conv2d(in_channels=256, out_channels=128, kernel_size=1)
        conv_11_2 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3)

        self.extras = nn.ModuleList([conv_8_1,  nn.ReLU(True),
                                     conv_8_2,  nn.ReLU(True),
                                     conv_9_1,  nn.ReLU(True),
                                     conv_9_2,  nn.ReLU(True),
                                     conv_10_1,  nn.ReLU(True),
                                     conv_10_2,  nn.ReLU(True),
                                     conv_11_1,  nn.ReLU(True),
                                     conv_11_2,  nn.ReLU(True)])

        self.L2Norm = L2Norm(512, 20)
        self.dbox = dbox['300']

        loc4_3 = nn.Conv2d(in_channels=512, out_channels=self.dbox[0] * 4, kernel_size=3, padding=1)
        loc7 = nn.Conv2d(in_channels=1024, out_channels=self.dbox[1] * 4, kernel_size=3, padding=1)
        loc8_2 = nn.Conv2d(in_channels=512, out_channels=self.dbox[2] * 4, kernel_size=3, padding=1)
        loc9_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[3] * 4, kernel_size=3, padding=1)
        loc10_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[4] * 4, kernel_size=3, padding=1)
        loc11_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[5] * 4, kernel_size=3, padding=1)

        cls4_3 = nn.Conv2d(in_channels=512, out_channels=self.dbox[0] * 21, kernel_size=3, padding=1)
        cls7 = nn.Conv2d(in_channels=1024, out_channels=self.dbox[1] * 21, kernel_size=3, padding=1)
        cls8_2 = nn.Conv2d(in_channels=512, out_channels=self.dbox[2] * 21, kernel_size=3, padding=1)
        cls9_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[3] * 21, kernel_size=3, padding=1)
        cls10_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[4] * 21, kernel_size=3, padding=1)
        cls11_2 = nn.Conv2d(in_channels=256, out_channels=self.dbox[5] * 21, kernel_size=3, padding=1)

        self.loc = nn.ModuleList([loc4_3,
                                  loc7,
                                  loc8_2,
                                  loc9_2,
                                  loc10_2,
                                  loc11_2])

        self.cls = nn.ModuleList([cls4_3,
                                  cls7,
                                  cls8_2,
                                  cls9_2,
                                  cls10_2,
                                  cls11_2])

    def forward(self, x):
        """
        여기서 output 은 결과적으로 loc prediction 과 cls prediction 입니다.
        :param x: input 이미지 batch data
        :return: loc, cls 입니다.
        """

        features = []  # 필요한 feature 를 담는 부분 : conv4_3 , ... , conv11_2
        loc = []
        cls = []

        for i in range(22):
            x = self.vgg[i](x)

        conv4_3 = self.L2Norm(x)
        features.append(conv4_3) # conv 4_3

        for i in range(23, len(self.vgg)):
            x = self.vgg[i](x)

        conv7 = x
        features.append(conv7)  # conv 7

        for i, ex_layer in enumerate(self.extras):
            x = ex_layer(x)
            # if i % 4 == 3:  # 4개의 한번꼴로
            #     features.append(x)
            if i == 3:
                conv8_2 = x    # conv 8_2
                features.append(conv8_2)
            if i == 7:
                conv9_2 = x    # conv 9_2
                features.append(conv9_2)
            if i == 11:
                conv10_2 = x    # conv 10_2
                features.append(conv10_2)
            if i == 15:
                conv11_2 = x    # conv 11_2
                features.append(conv11_2)

        for feature, l, c in zip(features, self.loc, self.cls):

            loc.append(l(feature).permute(0, 2, 3, 1).contiguous())
            cls.append(c(feature).permute(0, 2, 3, 1).contiguous())

        loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1)  # torch.Size([B, 34928])
        cls = torch.cat([o.view(o.size(0), -1) for o in cls], 1)  # torch.Size([B, 183372])

        loc = loc.view(loc.size(0), -1, 4),  # torch.Size([B, 8732, 4])
        cls = cls.view(cls.size(0), -1, 21),  # torch.Size([B, 8732, 21])

        # issue
        # view 를 사용했는데 loc 가 갑자기 tuple 이 되버림????

        # return 이 tuple 이다.
        return loc, cls

# This function is derived from torchvision VGG make_layers()
# https://github.com/pytorch/vision/blob/master/torchvision/models/vgg.py
def vgg(cfg, input_ch):
    '''
    fc-reduced vgg 사용하기 위해서 vgg 만드는 부분!
    :param cfg: str arr : vgg 의 구성을 나타내는 string array
    :return: nn.module 들의 layer 리스트!
    '''
    layers = []
    in_channels = input_ch

    # feature extractor

    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        elif v == 'C':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)]
        else:
            layers += [nn.Conv2d(in_channels, v, kernel_size=3, padding=1)]
            layers += [nn.ReLU(inplace=True)]
            in_channels = v

    # classifier

    layers += [nn.MaxPool2d(kernel_size=3, stride=1, padding=1)]
    layers += [nn.Conv2d(512, 1024, kernel_size=3, dilation=6, padding=6)]
    layers += [nn.ReLU(True)]
    layers += [nn.Conv2d(1024, 1024, kernel_size=1)]
    layers += [nn.ReLU(True)]

    # return nn.Sequential(*layers)
    return layers

cfg = {
    '300': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'C', 512, 512, 512, 'M',
            512, 512, 512],
    '512': [],
}

dbox = {
    '300': [4, 6, 6, 6, 4, 4],  # number of boxes per feature map location
    '512': [],
}

if __name__ == "__main__":
    transform = transforms.Compose(
        [
            transforms.Resize((300, 300)),
            transforms.ToTensor()
         ])

    # 얘는 img 에 대해서만 하는거임

    root_dir = "D:\Data\\voc\\2012"
    trainset = loader.VOC_loader(root_dir, transform=transform)

    # # img
    # # 이미지 하나씩 가져오는 부분
    # for i in range(len(trainset)):
    #     image, annotation, scale = trainset[i]
    #
    #     img = np.array(image)
    #     img = img.transpose((1, 2, 0))
    #     img *= 255
    #     img = img[:, :, ::-1].astype(np.uint8)
    #
    #     annotation = np.array(annotation)
    #     annotation[:, 0] *= scale[1]
    #     annotation[:, 2] *= scale[1]
    #     annotation[:, 1] *= scale[0]
    #     annotation[:, 3] *= scale[0]
    #
    #     for i in range(len(annotation)):
    #         img = cv2.rectangle(img, (int(annotation[i][0]), int(annotation[i][1])),
    #                             (int(annotation[i][2]), int(annotation[i][3])), (255, 255, 0), 3)
    #
    #     cv2.imshow('image', img)
    #     cv2.waitKey(0)

    trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True, num_workers=0)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    net = SSD().to(device)

    for epoch in range(10):

        running_loss = 0.0
        for i, (images, labels) in enumerate(trainloader):
            images = images.to(device)
            # labels = labels.to(device)

            out = net(images)
            print(out)
            out = 1


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

import numpy as np
import cv2
import time
import subprocess

# cap = cv2.VideoCapture(r'C:\Users\Mr.Five_SSD\Desktop\Material\One_Bike_Cross.mp4')
# cap = cv2.VideoCapture(r'C:\Users\Mr.Five_SSD\Desktop\Material\Many.mp4')
cap = cv2.VideoCapture(r'C:\Users\Mr.Five_SSD\Desktop\Material\Three_people_cross.mp4')

body_cascade = cv2.CascadeClassifier(
    r'C:\Users\Mr.Five_SSD\AppData\Local\Programs\Python\Python36\Lib\site-packages\cv2\data\haarcascade_fullbody.xml')
# cap = cv2.VideoCapture(0)

# fgbg = cv2.createBackgroundSubtractorMOG()
kernel3x3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
kernel6x6 = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))

fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True)
# fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
# fgbg = cv2.BackgroundSubtractor()

params = cv2.SimpleBlobDetector_Params()
# Change thresholds
# params.minThreshold = 10
# params.maxThreshold = 200

params.minDistBetweenBlobs = 30

# 检测白色
params.filterByColor = True
params.blobColor = 255

# Filter by Area. 面积
params.filterByArea = True
params.minArea = 100
params.maxArea = 2500

# Filter by Circularity 圆度
params.filterByCircularity = False
params.minCircularity = 0.1

# Filter by Convexity 凸性
params.filterByConvexity = False
params.minConvexity = 0.3

# Filter by Inertia 惯性
params.filterByInertia = False
params.minInertiaRatio = 0.01

time_last = -1.0  # 避免刚运行程序时时间差为0，
frame_count = 0

while (cap.isOpened()):
    # get a frame
    ret, frame = cap.read()

    if ret == True:
        keycode = cv2.waitKey(1) & 0xff
        if keycode != 0xff:
            print(keycode, '\n', chr(keycode))
            break

        # time.sleep(0.1)
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

        # bodys = body_cascade.detectMultiScale(frame, 1.1, 5)
        # print(bodys)
        # for (x, y, w, h) in bodys:
        #     cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3)

        # cv2.GaussianBlur(frame, (81, 81), 0)

        fgmask = fgbg.apply(gray)
        cv2.imshow('Foreground', fgmask)

        fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel3x3)
        cv2.imshow('Opening', fgmask)

        ret, binary = cv2.threshold(fgmask, 128, 255, cv2.THRESH_BINARY)  # 去掉阴影
        binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel6x6, iterations=2)

        detector = cv2.SimpleBlobDetector_create(params)
        keypoints = detector.detect(binary)

        # _, contours, _ = cv2.findContours(binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
        # cnt = contours[4]
        frame_RGB = cv2.cvtColor(binary, cv2.COLOR_GRAY2RGB)
        # cv2.drawContours(frame_RGB, contours, -1, (0, 0, 255), 1)
        # print(contours)

        frame_RGB = cv2.drawKeypoints(frame_RGB, keypoints, np.array([]), (0, 255, 0),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
        cv2.minEnclosingTriangle()
        for index in range(np.size(keypoints)):
            x = int(keypoints[index].pt[0])
            y = int(keypoints[index].pt[1])
            w = h = int(keypoints[index].size)
            cv2.rectangle(frame, (x - int(0.6 * w), y - int(1.2 * h)), (x + int(0.6 * w), y + int(1.2 * h)),
                          (0, 255, 0), 1)
        # tu = keypoints

        str_t = time.strftime('%Y-%m-%d %H:%M:%S')
        time_now = time.clock()
        time_delta = time_now - time_last
        time_last = time_now
        fps = 1.0 / time_delta
        fps_t = str('fps: %.3f' % fps)
        det_t = str('calculate time:%.3f s' % time_delta)

        frame_count += 1  # 帧数
        fra_t = str('frame count:%d' % frame_count)

        cv2.putText(frame, fra_t, (450, 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0), 1)
        cv2.putText(frame, fps_t, (0, 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0), 1)
        cv2.putText(frame, det_t, (0, 460), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
        cv2.putText(frame, str_t, (450, 460), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)

        cv2.imshow('Original', frame)
        cv2.imshow('Closing And Blob', frame_RGB)

        if frame_count == 333:
            subprocess.call("pause", shell=True)

    else:
        break

cap.release()
cv2.destroyAllWindows()


import requests

def getForecast(apiObj):
    forecast = {}
    forecast['high'] = apiObj['high']['celsius']
    forecast['low'] = apiObj['low']['celsius']
    forecast['dateFor'] = apiObj['date']['epoch']
    forecast['humidity'] = apiObj['avehumidity']
    return forecast

r = requests.get('http://api.wunderground.com/api/58ad3c15401bd354/forecast/q/NC/Cherryville.json')
response = r.json()
forecasts = response['forecast']['simpleforecast']['forecastday']
data = []
for f in forecasts:
    data = getForecast(f)
    requests.post('http://pharylonapi.azurewebsites.net/api/weather/forecast', data)


import argparse
from functools import partial
from typing import Optional

import numpy as np
import torch
import torchvision.datasets as TD
import torchvision.transforms as TT
from torch import Tensor, nn
from torch.utils.data import DataLoader

from modelling import get_discriminator_cls, get_generator_cls
from training import BaseTrainer, BaseTrainerConfig, compute_d_loss, compute_g_loss
from utils import add_args_from_cls, cls_from_args

def build_model(args: argparse.Namespace):
    if args.dataset == "mnist":
        y_dim = 10
        y_encoder = nn.Embedding
        img_channels = 1

    elif args.dataset == "celeba":
        y_dim = 40
        y_encoder = partial(nn.Linear, bias=False)
        img_channels = 3

    else:
        img_channels = 3

    kwargs = dict(img_size=args.img_size, img_channels=img_channels)

    if args.base_depth is not None:
        kwargs.update(base_depth=args.base_depth)

    if args.progressive_growing:
        assert args.model in ("progressive_gan",)
        kwargs.update(
            progressive_growing=args.progressive_growing,
            fade_duration=args.fade_duration,
        )

    d_cls = get_discriminator_cls(args.model)
    g_cls = get_generator_cls(args.model)

    if args.model == "sagan":
        if args.conditional:
            kwargs.update(y_dim=y_dim, y_layer_factory=y_encoder)
        dis = d_cls(**kwargs)
        gen = g_cls(**kwargs, z_dim=args.z_dim)

    elif args.model in ("dcgan", "progressive_gan", "stylegan", "stylegan2"):
        if args.conditional:
            yemb_dim = 128
            dis = get_discriminator_cls("cgan")(
                d_cls(**kwargs, img_channels=kwargs["img_channels"] + yemb_dim),
                y_encoder(y_dim, yemb_dim),
            )
            gen = get_generator_cls("cgan")(
                g_cls(**kwargs, z_dim=args.z_dim + yemb_dim),
                y_encoder(y_dim, yemb_dim),
            )
        else:
            dis = d_cls(**kwargs)
            gen = g_cls(**kwargs)

    else:
        raise ValueError(f"Unsupported model {args.model}")

    return dis, gen

def build_dataset(name: str, img_size: int):
    if name == "mnist":
        transform = TT.Compose([TT.Pad(2), TT.ToTensor(), TT.Normalize(0.5, 0.5)])
        ds = TD.MNIST("data", transform=transform)

    elif name == "celeba":
        transform = TT.Compose([TT.Resize(img_size), TT.CenterCrop(img_size), TT.ToTensor(), TT.Normalize(0.5, 0.5)])
        ds = TD.CelebA("data", split="all", transform=transform, target_transform=lambda x: x / x.sum())

    else:
        transform = TT.Compose([TT.Resize(img_size), TT.CenterCrop(img_size), TT.ToTensor(), TT.Normalize(0.5, 0.5)])
        ds = TD.ImageFolder(name, transform=transform)

    return ds

class Noise2ImgTrainer(BaseTrainer):
    def __init__(self, config, dis, gen, fixed_z: Tensor, fixed_y: Tensor):
        super().__init__(config, dis, gen)
        self.fixed_z = fixed_z.to(self.config.device)
        self.fixed_y = fixed_y.to(self.config.device)

    def _forward(self, m: nn.Module, xs: Tensor, ys: Optional[Tensor]) -> Tensor:
        return m(xs, ys) if self.config.conditional else m(xs)

    def train_step(self, x_reals, ys):
        cfg = self.config
        if cfg.channels_last:
            x_reals = x_reals.to(memory_format=torch.channels_last)

        if self.global_step % cfg.log_img_interval == 0:
            with torch.inference_mode(), self.g_ema.swap_state_dict(self.gen), self.autocast():
                x_fakes = self._forward(self.gen, self.fixed_z, self.fixed_y)
            self.log_images(x_fakes, "generated", self.global_step)

        log_dict = dict()
        log_dict["loss/d"] = self.train_d_step(x_reals, ys).item()

        if self.global_step % cfg.train_g_interval == 0:
            log_dict["loss/g"] = self.train_g_step(x_reals, ys).item()

        for m in (self.dis, self.gen):
            if hasattr(m, "step"):
                m.step()

        return log_dict

    def train_d_step(self, x_reals: Tensor, ys: Optional[Tensor]):
        bsize = x_reals.shape[0]
        cfg = self.config

        # Algorithm 1 in paper clip weights after optimizer step, but GitHub code clip before optimizer step
        # it shouldn't matter much in practice
        if cfg.method == "wgan":
            with torch.no_grad():
                for param in self.dis.parameters():
                    param.clip_(-cfg.wgan_clip, cfg.wgan_clip)

        with self.autocast():
            with torch.no_grad():
                z_noise = torch.randn(bsize, cfg.z_dim, device=x_reals.device)
                x_fakes = self._forward(self.gen, z_noise, ys)

            # if cfg.r1_penalty > 0 and self.counter % cfg.r1_penalty_interval == 0:
            #     x_reals.requires_grad_()

            d_reals = self._forward(self.dis, x_reals, ys)
            d_fakes = self._forward(self.dis, x_fakes, ys)
            loss_d = compute_d_loss(d_reals, d_fakes, cfg.method)

            if cfg.method == "wgan-gp":
                # https://pytorch.org/docs/stable/notes/amp_examples.html#gradient-penalty
                alpha = torch.rand(bsize, 1, 1, 1, device=x_reals.device)
                x_inters = x_reals.lerp(x_fakes.detach().float(), alpha).requires_grad_()
                d_inters = self._forward(self.dis, x_inters, ys).sum()

                with self.autocast(enabled=False):
                    if self.scaler is not None:
                        d_inters = self.scaler.scale(d_inters)
                        scale = self.scaler.get_scale()
                    else:
                        scale = 1

                    (d_grad,) = torch.autograd.grad(d_inters, x_inters, create_graph=True)
                    d_grad = d_grad / scale

                d_grad_norm = torch.linalg.vector_norm(d_grad, dim=(1, 2, 3))
                loss_d = loss_d + cfg.wgan_gp_lamb * (d_grad_norm - 1).square().mean()

            # for Progressive GAN only
            if cfg.drift_penalty > 0:
                loss_d = loss_d + d_reals.square().mean() * cfg.drift_penalty

        # https://arxiv.org/abs/1801.04406, for StyleGAN and StyleGAN2
        # if cfg.r1_penalty > 0 and self.counter % cfg.r1_penalty_interval == 0:
        #     (d_grad,) = torch.autograd.grad(d_reals.sum(), x_reals, create_graph=True)
        #     d_grad_norm2 = d_grad.square().sum() / bsize
        #     loss_d = loss_d + d_grad_norm2 * cfg.r1_penalty / 2

        self.step_optimizer(loss_d, self.optim_d)

        return loss_d

    def train_g_step(self, x_reals: Tensor, ys: Optional[Tensor]):
        bsize = x_reals.shape[0]
        cfg = self.config
        self.dis.requires_grad_(False)

        with self.autocast():
            z_noise = torch.randn(bsize, cfg.z_dim, device=x_reals.device)
            x_fakes = self._forward(self.gen, z_noise, ys)
            d_fakes = self._forward(self.dis, x_fakes, ys)
            loss_g = compute_g_loss(d_fakes, cfg.method)

        self.step_optimizer(loss_g, self.optim_g)
        self.dis.requires_grad_(True)
        return loss_g

def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", default="dcgan")
    parser.add_argument("--base_depth", type=int)
    parser.add_argument("--dataset", choices=["mnist", "celeba"])
    parser.add_argument("--img_size", type=int, default=64)
    parser.add_argument("--batch_size", type=int, default=32)
    parser.add_argument("--num_workers", type=int, default=8)
    parser.add_argument("--n_steps", type=int, default=10_000)
    parser.add_argument("--n_log_imgs", type=int, default=40)
    parser.add_argument("--progressive_growing", action="store_true")
    parser.add_argument("--fade_duration", type=int)
    add_args_from_cls(parser, BaseTrainerConfig)
    return parser

def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.dataset == "celeba" and args.img_size > 256:
        print("CelebA image size is 178x218. Training large image size is not optimal")

    if args.dataset == "mnist":
        print("Only allow 32x32 image for MNIST")
        args.img_size = 32

    dis, gen = build_model(args)

    fixed_z = torch.randn((args.n_log_imgs, args.z_dim))

    ds = build_dataset(args.dataset, args.img_size)
    rand_indices = np.random.choice(len(ds), args.n_log_imgs, replace=False)
    fixed_y = [ds[idx][1] for idx in rand_indices]
    fixed_y = (torch.stack if isinstance(fixed_y[0], Tensor) else torch.tensor)(fixed_y)

    config = cls_from_args(args, BaseTrainerConfig)
    trainer = Noise2ImgTrainer(config, dis, gen, fixed_z, fixed_y)

    if args.progressive_growing:
        schedule = [(4, args.fade_duration)]
        while schedule[-1][0] != args.img_size:
            schedule.append((schedule[-1][0] * 2, args.fade_duration * 2))
    else:
        schedule = [(args.img_size, args.n_steps)]

    for i, (size, n_steps) in enumerate(schedule):
        if i > 0:
            dis.grow()
            gen.grow()

        dloader = DataLoader(
            dataset=build_dataset(args.dataset, size),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=args.num_workers,
            pin_memory=True,
            persistent_workers=True,
            drop_last=True,
        )
        trainer.train(dloader, n_steps)

if __name__ == "__main__":
    main()


import argparse
import json
import codecs
import numpy as np
import h5py
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser()
parser.add_argument('--reddit', default='../Data/Reddit/Sequences.txt')
parser.add_argument('--redditSequences', default='../Data/Reddit/Sequences.json')
parser.add_argument('--wikipedia', default='../Data/Wikipedia/Summaries.txt')
parser.add_argument('--wikipediaSentences', default='../Data/Wikipedia/Sentences.json')
parser.add_argument('--padding', action='store_true', default=True)
parser.add_argument('--alignedSentences', default=20)
parser.add_argument('--redditOutput', default='../Aligned-Dataset/reddit.h5')
parser.add_argument('--wikipediaOutput', default='../Aligned-Dataset/wikipedia.h5')
parser.add_argument('--dictionary', default='../Aligned-Dataset/dictionary.json')
parser.add_argument('--validate', type=float, default=0.1)
parser.add_argument('--test', type=float, default=0.1)
parser.add_argument('--encoding', default='utf-8')
args = parser.parse_args()

id2word = {}
word2id = {}
word2count = {}
vocab_length = 0
id2element = {}
element2id = {}
elements_length = 0

with codecs.open(args.wikipedia, 'r', 'utf-8') as wikipedia:
    wikipedia_dataset = wikipedia.read().split()
    wikipedia.close()

    with codecs.open(args.reddit, 'r', 'utf-8') as reddit:
        reddit_dataset = reddit.read().split()
        reddit.close()
        dataset = wikipedia_dataset + reddit_dataset

        wikipedia_size = len(wikipedia_dataset)
        reddit_size = len(reddit_dataset)
        size = len(dataset)
        print ('Total number of words included in the Wikipedia dataset: %d' % (wikipedia_size))
        print ('Total number of words included in the reddit dataset: %d' % (reddit_size))
        print ('Total number of words included in the aggregated dataset: %d' % (size))

        vocab_length = vocab_length + 1
        word2id['NaN'.decode('utf-8')] = vocab_length
        id2word[vocab_length] = 'NaN'.decode('utf-8')

        # Remove infrequent words from the sequences of comments.

        for i in range(0, wikipedia_size):
             if wikipedia_dataset[i] not in word2count:
                word2count[wikipedia_dataset[i]] = 1
             else:
                 word2count[wikipedia_dataset[i]] = word2count[wikipedia_dataset[i]] + 1
                 if wikipedia_dataset[i] not in word2id:
                     vocab_length = vocab_length + 1
                     word2id[wikipedia_dataset[i]] = vocab_length
                     id2word[vocab_length] = wikipedia_dataset[i]
        print ('Size of the vocabulary after processing the Wikipedia dataset: %d' % (vocab_length))

        for i in range(0, reddit_size):
            if reddit_dataset[i] not in word2count:
                word2count[reddit_dataset[i]] = 1
            elif word2count[reddit_dataset[i]] <= 2:
                word2count[reddit_dataset[i]] = word2count[reddit_dataset[i]] + 1
            else:
                word2count[reddit_dataset[i]] = word2count[reddit_dataset[i]] + 1
                if reddit_dataset[i] not in word2id:
                    vocab_length = vocab_length + 1
                    word2id[reddit_dataset[i]] = vocab_length
                    id2word[vocab_length] = reddit_dataset[i]

if args.padding:
    vocab_length = vocab_length + 1
    word2id['<PAD>'.decode('utf-8')] = vocab_length
    id2word[vocab_length] = '<PAD>'.decode('utf-8')

print ('Size of the vocabulary: %d' % (vocab_length))

# Load Wikipedia sentences along with their corresponding lengths.

with open (args.wikipediaSentences, 'r') as f:
    sentences = json.load(f, 'utf-8')
    f.close()

wiki_sentences = sentences['wiki_sentences']
wiki_elements = sentences['wiki_elements']
wiki_sentences_length = sentences['wiki_sentences_length']
num_wiki_sentences = len(wiki_sentences)

max_tokens = 0
for i in range(0, num_wiki_sentences):
    if len(wiki_sentences[i]) > max_tokens:
        max_tokens = len(wiki_sentences[i])

print ('Max sentence length is: %d' % (max_tokens))

for i in range(0, len(wiki_sentences)):
    if wiki_elements[i] not in element2id:
        elements_length = elements_length + 1
        element2id[wiki_elements[i]] = elements_length
        id2element[elements_length] = wiki_elements[i]

# Load reddit sequences.

with open (args.redditSequences, 'r') as f:
    sequences = json.load(f, 'utf-8')
    f.close()

tmpSequences = []
tmpElements = []
sequences_length = []
num_sequences = 0

for element in sequences:
    for j in range(0, len(sequences[element])):
        tmpSequences.append(sequences[element][j])
        tmpElements.append(element)
        sequences_length.append(len(sequences[element][j].split()))
        num_sequences = num_sequences + 1

reddit_lengths = np.asarray(sequences_length)
reddit_mean = np.mean(reddit_lengths)
reddit_std = np.std(reddit_lengths)
reddit_maximum = np.amax(reddit_lengths)

# Disregard sequences whose length exceeds a specific threshold.
threshold = reddit_mean + 1.0 * reddit_std

deleted = 0
tmpSequencesLength = sequences_length[:]
for i in range(0, num_sequences):
    if tmpSequencesLength[i] > threshold:
        del tmpSequences[i - deleted]
        del tmpElements[i - deleted]
        del sequences_length[i - deleted]
        deleted = deleted + 1

reddit_lengths = np.asarray(sequences_length)
reddit_mean = np.mean(reddit_lengths)
reddit_std = np.std(reddit_lengths)
reddit_maximum = np.amax(reddit_lengths)

print ('Mean sequence length is: %d' % (reddit_mean))
print ('Max sequence length is: %d' % (reddit_maximum))
print ('Standard deviation of the lengths of the sampled sequences is: %d' % (reddit_std))

reddit_validate_sequences = np.floor(args.validate * len(tmpSequences))
reddit_test_sequences = np.floor(args.test * len(tmpSequences))
reddit_train_sequences = len(tmpSequences) - reddit_validate_sequences - reddit_test_sequences

aligned_train_dataset = []
aligned_validate_dataset = []
aligned_test_dataset = []

# The flagElement corresponds to the position of the last sentence that has been aligned with an already processed sequence of comments.
flagElement = {}

for key in element2id:
    flagElement[key] = 0
totalSequences = [0, 0, 0]

while len(tmpSequences) > 0:

    sequence = np.random.randint(0, len(tmpSequences))

    if totalSequences[1] < reddit_validate_sequences and totalSequences[2] < reddit_test_sequences:
        random = np.random.randint(0, 3)
    elif totalSequences[1] >= reddit_validate_sequences and totalSequences[2] < reddit_test_sequences:
        random = np.random.randint(0, 2)
        if random == 1:
            random = random + 1
    elif totalSequences[2] >= reddit_test_sequences and totalSequences[1] < reddit_validate_sequences:
        random = np.random.randint(0, 2)
    else:
        random = 0

    if random == 1 and totalSequences[1] < reddit_validate_sequences:
        aligned_validate_dataset.append({'sentences': [], 'sequence': tmpSequences[sequence].split()})
        totalSequences[1] = totalSequences[1] + 1
        find = 0
        while wiki_elements[find] != tmpElements[sequence]:
            find = find + 1
        included = 0
        while included < args.alignedSentences:
            if find + flagElement[tmpElements[sequence]] + included < len(wiki_sentences):
                if wiki_elements[find + flagElement[tmpElements[sequence]] + included] == tmpElements[sequence]:
                    aligned_validate_dataset[len(aligned_validate_dataset) - 1]['sentences'].append(wiki_sentences[find + flagElement[tmpElements[sequence]] + included])
                    included = included + 1
                else:
                    flagElement[tmpElements[sequence]] = 0
            else:
                flagElement[tmpElements[sequence]] = 0
                print('Resetting end-flag for the Element: %s...' % (tmpElements[sequence]))
        flagElement[tmpElements[sequence]] = flagElement[tmpElements[sequence]] + included
        tmpSequences.pop(sequence)
        tmpElements.pop(sequence)
        sequences_length.pop(sequence)

    elif random == 2 and totalSequences[2] < reddit_test_sequences:
        aligned_test_dataset.append({'sentences': [], 'sequence': tmpSequences[sequence].split()})
        totalSequences[2] = totalSequences[2] + 1
        find = 0
        while wiki_elements[find] != tmpElements[sequence]:
            find = find + 1
        included = 0
        while included < args.alignedSentences:
            if find + flagElement[tmpElements[sequence]] + included < len(wiki_sentences):
                if wiki_elements[find + flagElement[tmpElements[sequence]] + included] == tmpElements[sequence]:
                    aligned_test_dataset[len(aligned_test_dataset) - 1]['sentences'].append(wiki_sentences[find + flagElement[tmpElements[sequence]] + included])
                    included = included + 1
                else:
                    flagElement[tmpElements[sequence]] = 0
            else:
                flagElement[tmpElements[sequence]] = 0
                print('Resetting end-flag for the Element: %s...' % (tmpElements[sequence]))
        flagElement[tmpElements[sequence]] = flagElement[tmpElements[sequence]] + included
        tmpSequences.pop(sequence)
        tmpElements.pop(sequence)
        sequences_length.pop(sequence)

    else:
        #reddit_train_dataset = reddit_train_dataset + tmpSequences[sequence]
        aligned_train_dataset.append({'sentences': [], 'sequence': tmpSequences[sequence].split()})
        totalSequences[0] = totalSequences[0] + 1
        find = 0
        while wiki_elements[find] != tmpElements[sequence]:
            find = find + 1
        included = 0
        while included < args.alignedSentences:
            if find + flagElement[tmpElements[sequence]] + included < len(wiki_sentences):
                if wiki_elements[find + flagElement[tmpElements[sequence]] + included] == tmpElements[sequence]:
                    aligned_train_dataset[len(aligned_train_dataset) - 1]['sentences'].append(wiki_sentences[find + flagElement[tmpElements[sequence]] + included])
                    included = included + 1
                else:
                    flagElement[tmpElements[sequence]] = 0
            else:
                flagElement[tmpElements[sequence]] = 0
                print('Resetting end-flag for the Element: %s...' % (tmpElements[sequence]))
        flagElement[tmpElements[sequence]] = flagElement[tmpElements[sequence]] + included
        tmpSequences.pop(sequence)
        tmpElements.pop(sequence)
        sequences_length.pop(sequence)

aligned_reddit_train = np.zeros((len(aligned_train_dataset), reddit_maximum), dtype=np.uint32)
aligned_reddit_validate = np.zeros((len(aligned_validate_dataset), reddit_maximum), dtype=np.uint32)
aligned_reddit_test = np.zeros((len(aligned_test_dataset), reddit_maximum), dtype=np.uint32)

aligned_wiki_train = np.zeros((args.alignedSentences * len(aligned_train_dataset), max_tokens), dtype=np.uint32)
aligned_wiki_validate = np.zeros((args.alignedSentences * len(aligned_validate_dataset), max_tokens), dtype=np.uint32)
aligned_wiki_test = np.zeros((args.alignedSentences * len(aligned_test_dataset), max_tokens), dtype=np.uint32)

for i in range(0, len(aligned_train_dataset)):
    for j in range(0, len(aligned_train_dataset[i]['sequence'])):
        if aligned_train_dataset[i]['sequence'][j] in word2id:
            aligned_reddit_train[i][j] = word2id[aligned_train_dataset[i]['sequence'][j]]
        else:
            aligned_reddit_train[i][j] = word2id['NaN']
    for z in range(len(aligned_train_dataset[i]['sequence']), reddit_maximum):
                aligned_reddit_train[i][z] = word2id['<PAD>']
    for j in range(0, len(aligned_train_dataset[i]['sentences'])):
        for z in range(0, len(aligned_train_dataset[i]['sentences'][j])):
            if aligned_train_dataset[i]['sentences'][j][z] in word2id:
                aligned_wiki_train[args.alignedSentences * i + j][z] = word2id[aligned_train_dataset[i]['sentences'][j][z]]
            else:
                aligned_wiki_train[args.alignedSentences * i + j][z] = word2id['NaN']
        for t in range(len(aligned_train_dataset[i]['sentences'][j]), max_tokens):
                aligned_wiki_train[args.alignedSentences * i + j][t] = word2id['<PAD>']

for i in range(0, len(aligned_validate_dataset)):
    for j in range(0, len(aligned_validate_dataset[i]['sequence'])):
        if aligned_validate_dataset[i]['sequence'][j] in word2id:
            aligned_reddit_validate[i][j] = word2id[aligned_validate_dataset[i]['sequence'][j]]
        else:
            aligned_reddit_validate[i][j] = word2id['NaN']
    for z in range(len(aligned_validate_dataset[i]['sequence']), reddit_maximum):
                aligned_reddit_validate[i][z] = word2id['<PAD>']
    for j in range(0, len(aligned_validate_dataset[i]['sentences'])):
        for z in range(0, len(aligned_validate_dataset[i]['sentences'][j])):
            if aligned_validate_dataset[i]['sentences'][j][z] in word2id:
                aligned_wiki_validate[args.alignedSentences * i + j][z] = word2id[aligned_validate_dataset[i]['sentences'][j][z]]
            else:
                aligned_wiki_validate[args.alignedSentences * i + j][z] = word2id['NaN']
        for t in range(len(aligned_validate_dataset[i]['sentences'][j]), max_tokens):
                aligned_wiki_validate[args.alignedSentences * i + j][t] = word2id['<PAD>']

for i in range(0, len(aligned_test_dataset)):
    for j in range(0, len(aligned_test_dataset[i]['sequence'])):
        if aligned_test_dataset[i]['sequence'][j] in word2id:
            aligned_reddit_test[i][j] = word2id[aligned_test_dataset[i]['sequence'][j]]
        else:
            aligned_reddit_test[i][j] = word2id['NaN']
    for z in range(len(aligned_test_dataset[i]['sequence']), reddit_maximum):
                aligned_reddit_test[i][z] = word2id['<PAD>']
    for j in range(0, len(aligned_test_dataset[i]['sentences'])):
        for z in range(0, len(aligned_test_dataset[i]['sentences'][j])):
            if aligned_test_dataset[i]['sentences'][j][z] in word2id:
                aligned_wiki_test[args.alignedSentences * i + j][z] = word2id[aligned_test_dataset[i]['sentences'][j][z]]
            else:
                aligned_wiki_test[args.alignedSentences * i + j][z] = word2id['NaN']
        for t in range(len(aligned_test_dataset[i]['sentences'][j]), max_tokens):
                aligned_wiki_test[args.alignedSentences * i + j][t] = word2id['<PAD>']

with h5py.File(args.wikipediaOutput, 'w') as f:
    f.create_dataset('train', data=aligned_wiki_train)
    f.create_dataset('validate', data=aligned_wiki_validate)
    f.create_dataset('test', data=aligned_wiki_test)

with h5py.File(args.redditOutput, 'w') as f:
    f.create_dataset('train', data=aligned_reddit_train)
    f.create_dataset('validate', data=aligned_reddit_validate)
    f.create_dataset('test', data=aligned_reddit_test)

dictionary = {'word2id': word2id, 'id2word': id2word}
with open(args.dictionary, 'w') as f:
    json.dump(dictionary, f, encoding='utf-8')


from itertools import combinations, permutations
n = int(input())

board = [list(map(int, input().split())) for _ in range(n)]

def getScore(players):
    enemies = [i for i in range(1, n+1) if i not in players]
    return abs(sum([board[p1-1][p2-1]+board[p2-1][p1-1] for p1, p2 in combinations(players, 2)]) -
               sum([board[q1-1][q2-1]+board[q2-1][q1-1] for q1, q2 in combinations(enemies, 2)]))

def solution(n, board):
    return min([getScore(players) for players in list(combinations(range(1, n+1), n//2))])

print(solution(n, board))


import boto3
client = boto3.client('s3')

# This Function will Read File from S3 Bucket whenever any file with .txt extension is created in S3
# Bucket - pythondatademo123
# Region - us-east-1

def file_handler(event, context):

    # Read data from S3 Bucket
    data = client.get_object(
        Bucket='pythondatademo123',
        Key='demo/data.txt'
    )

    contents = data['Body'].read()

    # Print data to Log - CloudWatch
    print(contents)


#!/usr/bin/python

import os
from wsgiref.handlers import CGIHandler

from flask import Flask
from flask import request, abort, redirect, url_for, jsonify, escape

import urllib2
import json
import sys

from pprint import pprint

sys.path.append(os.path.dirname(__file__))
from lod.freebase import Freebase
from lod.dbpedia import DBPedia
from lod.isbn import Isbn

application = Flask("dbpedia")
application.debug = True

@application.route('/')

@application.route('/isbn:<path:identifier>')
@application.route('/ISBN:<path:identifier>')

@application.route('/dbp:<path:identifier>')
@application.route('/DBP:<path:identifier>')

@application.route('/fb:/<path:identifier>')
@application.route('/fb:<path:identifier>')
@application.route('/FB:/<path:identifier>')
@application.route('/FB:<path:identifier>')

def lod(identifier = None):
    """

DBP: DBPedia

    http://dev.fe2.nl/DBP:Nasa

    http://dev.fe2.nl/DBP:Nasa?label_nl

    http://dev.fe2.nl/DBP:Nasa?label_nl,depiction

    http://dev.fe2.nl/DBP:Nasa?depiction&mode=html

ISBN: Isbn

    http://dev.fe2.nl/ISBN:9023439155

    http://dev.fe2.nl/ISBN:9023439155?title

    http://dev.fe2.nl/ISBN:9023439155?google_title

FB: FreeBase

    http://dev.fe2.nl/fb:/en/herman_brood

    """
    if not identifier == None:
        request_lod_source = request.path.split(':')[0][1:]
        if not request.args == None:
            req = dict(request.args)
        else:
            req = None
        if "mode" in req:
            if "html" == str(req["mode"][0]):
                mode="html"
                req.__delitem__("mode")
            else:
                mode="json"
                req.__delitem__("mode")
        else:
            mode="json"

        if request_lod_source.lower() == "fb":
            fb = Freebase([escape("/"+identifier)], log_path='/tmp', debug=True, backend='pymongo')
            fb.execute()
            data=fb
        elif request_lod_source.lower() == "isbn":
            isbn = Isbn([escape(identifier)], log_path='/tmp', debug=True, backend='pymongo')
            isbn.execute()
            data = isbn
        elif request_lod_source.lower() == "dbp":
            dbp = DBPedia([escape(identifier)], log_path='/tmp', debug=True, backend='pymongo')
            dbp.execute()
            data = dbp
        else:
            return(lod.__doc__)

        if len(data.keys()) > 0:
            if not req == None:
                if len(req.keys()) > 0:
                    for space in data.keys():
                        ret = ""
                        res = {}
                        for name in req.keys()[0].split(','):
                            if name in data[space]:
                                if str(mode) == "json":
                                    res[name] = data[space][name]
                                else:
                                    if data[space][name].lower().find('.jpg') > -1 or \
                                        data[space][name].lower().find('_thumb') > -1 or \
                                        data[space][name].lower().find('.svg') > -1 or \
                                        data[space][name].lower().find('.gif') > -1:
                                        if data[space][name].lower().find('_thumb') > -1:
                                            ret += "
"
                                        else:
                                            ret += "
"

                                    elif data[space][name].find('http://') > -1:
                                        ret += ""+data[space][name]+"
"
                                    else:
                                        ret += "
"+ name.split('_')[0].title() + ":
" + data[space][name]+"
"
                        if mode == "json":
                            return(jsonify(res))
                        else:
                            return(ret)
                return(jsonify(data))
        return()
    return(lod.__doc__)


from functools import cache

def solve():
    n,m,d = list(map(int,input().split(" ")))
    a = list(map(int,input().split(" ")))

    @cache
    def recursion(prevDay,m,i):
        if m==0 or i==n: return 0
        # take
        ans = a[i]-d*(i+1-prevDay)+recursion(i+1,m-1,i+1)
        # not take
        ans = max(ans,recursion(prevDay,m,i+1))
        return ans

    dp = [[[0,0]]*(m+1) for _ in range(n+1)]
    for mi in range(m,0,-1):
        for ii in range(n):
            # take
            ans = a[ii]-d*(ii+1-prevDay)+recursion(ii+1,mi-1,ii+1)
            # not take
            ans = max(ans,recursion(prevDay,mi,ii+1))
            return ans

    print(dp)

    return recursion(0,m,0)

def getIns():
    for _ in range(int(input())):
        print(solve())

# getIns()

from contextlib import redirect_stdout
with open('out.txt', 'w') as f:
    with redirect_stdout(f): getIns()


import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import SVC

train_data = pd.read_csv("../data_csv/train.csv")
test_data = pd.read_csv("../data_csv/test.csv")

# Split train to x and y
y = train_data["label"]
del train_data["label"]

# Split the data
X_train, X_test, y_train, y_test = train_test_split(train_data, y, random_state=1)

# Scale data
scaler = MinMaxScaler()
# Model
clf = SVC(C=8)
# Pipeline
pipeline = Pipeline([('transformer', scaler), ('estimator', clf)])

scores = cross_val_score(pipeline, X_train, y_train, cv=10)
print("Accuracy: %0.4f (+/- %0.2f)" % (scores.mean(), scores.std()))

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
clf.fit(X_train, y_train)

X_test = scaler.transform(X_test)
cost_train = mean_squared_error(y_train, clf.predict(X_train))
cost_test = mean_squared_error(y_test, clf.predict(X_test))
print("Cost train: %f Cost test: %f" % (cost_train, cost_test))

train_score = clf.score(X_train, y_train)
test_score = clf.score(X_test, y_test)
print("Train score: %f Test score: %f" % (train_score, test_score))

# Fit unlabeled data
test_data = scaler.transform(test_data)
Y = pd.Series(clf.predict(test_data))
Y.name = "Label"
image_id = pd.Series(range(1, Y.shape[0] + 1))
image_id.name = "ImageId"
prediction_data = pd.concat([image_id, Y], axis=1)
# Create prediction csv
prediction_data.to_csv("../data_csv/prediction.csv", index=False)


from datetime import datetime, timedelta

from pydantic import BaseModel

from pol.permission import Role, UserGroup

__all__ = ["PublicUser", "User"]

class PublicUser(BaseModel):
    id: int
    username: str
    nickname: str

class User(Role, BaseModel):
    id: int
    username: str
    nickname: str
    group_id: UserGroup
    registration_date: datetime
    sign: str
    avatar: str

    def allow_nsfw(self) -> bool:
        allow_date = self.registration_date + timedelta(days=60)
        return datetime.utcnow().astimezone() > allow_date

    def get_user_id(self) -> int:
        return self.id


import functools

import tensorflow as tf

from finetune.util.shapes import shape_list, lengths_from_eos_idx
from finetune.optimizers.recompute_grads import recompute_grad
from finetune.nn.activations import gelu
from finetune.base_models.gpt.featurizer import norm, dropout, get_pos_values

def softmax(x, axis=-1):
    x = x - tf.reduce_max(input_tensor=x, axis=axis, keepdims=True)
    ex = tf.exp(x)
    return ex / tf.reduce_sum(input_tensor=ex, axis=axis, keepdims=True)

def split_states(x, n):
    """Reshape the last dimension of x into [n, x.shape[-1]/n]."""
    *start, m = shape_list(x)
    return tf.reshape(x, start + [n, m // n])

def merge_states(x):
    """Smash the last two dimensions of x into a single dimension."""
    *start, a, b = shape_list(x)
    return tf.reshape(x, start + [a * b])

def conv1d(x, scope, nf, *, w_init_stdev=0.02):
    with tf.compat.v1.variable_scope(scope):
        *start, nx = shape_list(x)
        w = tf.compat.v1.get_variable(
            "w",
            [1, nx, nf],
            initializer=tf.compat.v1.random_normal_initializer(stddev=w_init_stdev),
        )
        b = tf.compat.v1.get_variable("b", [nf], initializer=tf.compat.v1.constant_initializer(0))
        c = tf.reshape(
            tf.matmul(tf.reshape(x, [-1, nx]), tf.reshape(w, [-1, nf])) + b,
            start + [nf],
        )
        return c

def attention_mask(nd, ns, *, dtype):
    """1's in the lower triangle, counting from the lower right corner.
    Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs.
    """
    i = tf.range(nd)[:, None]
    j = tf.range(ns)
    m = i >= j - ns + nd
    return tf.cast(m, dtype)

def attn(x, scope, n_state, *, past, hparams, train=False):
    assert x.shape.ndims == 3  # Should be [batch, sequence, features]
    assert n_state % hparams.n_heads == 0
    if past is not None:
        assert (
            past.shape.ndims == 5
        )  # Should be [batch, 2, heads, sequence, features], where 2 is [k, v]

    def split_heads(x):
        # From [batch, sequence, features] to [batch, heads, sequence, features]
        return tf.transpose(a=split_states(x, hparams.n_heads), perm=[0, 2, 1, 3])

    def merge_heads(x):
        # Reverse of split_heads
        return merge_states(tf.transpose(a=x, perm=[0, 2, 1, 3]))

    def mask_attn_weights(w):
        # w has shape [batch, heads, dst_sequence, src_sequence], where information flows from src to dst.
        _, _, nd, ns = shape_list(w)
        b = attention_mask(nd, ns, dtype=w.dtype)
        b = tf.reshape(b, [1, 1, nd, ns])
        w = w * b - tf.cast(1e10, w.dtype) * (1 - b)
        return w

    def multihead_attn(q, k, v, attn_p_drop, train=False):
        # q, k, v have shape [batch, heads, sequence, features]
        w = tf.matmul(q, k, transpose_b=True)
        w = w * tf.math.rsqrt(tf.cast(v.shape[-1], w.dtype))

        w = mask_attn_weights(w)
        w = softmax(w)
        w = dropout(w, attn_p_drop, train)
        a = tf.matmul(w, v)
        return a

    with tf.compat.v1.variable_scope(scope):
        c = conv1d(x, "c_attn", n_state * 3)
        q, k, v = map(split_heads, tf.split(c, 3, axis=2))
        if past is not None:
            pk, pv = tf.unstack(past, axis=1)
            k = tf.concat([pk, k], axis=-2)
            v = tf.concat([pv, v], axis=-2)
        a = multihead_attn(q, k, v, hparams.attn_p_drop, train=train)
        a = merge_heads(a)
        a = conv1d(a, "c_proj", n_state)
        a = dropout(a, hparams.resid_p_drop, train=train)
        return a

def mlp(x, scope, n_state, *, hparams, train=False):
    with tf.compat.v1.variable_scope(scope):
        nx = x.shape[-1]
        h = gelu(conv1d(x, "c_fc", n_state))
        h2 = conv1d(h, "c_proj", nx)
        h2 = dropout(h2, hparams.resid_p_drop, train=train)
        return h2

def block(x, *, past, hparams, train=False):
    nx = x.shape[-1]
    a = attn(norm(x, "ln_1"), "attn", nx, past=past, hparams=hparams, train=train)
    x = x + a
    m = mlp(norm(x, "ln_2"), "mlp", nx * 4, hparams=hparams, train=train)
    x = x + m
    return x

def past_shape(*, hparams, batch_size=None, sequence=None):
    return [
        batch_size,
        hparams.n_layer,
        2,
        hparams.n_heads,
        sequence,
        hparams.n_embd // hparams.n_heads,
    ]

def expand_tile(value, size):
    """Add a new axis of given size."""
    value = tf.convert_to_tensor(value=value, name="value")
    ndims = value.shape.ndims
    return tf.tile(tf.expand_dims(value, axis=0), [size] + [1] * ndims)

def positions_for(tokens, past_length):
    batch_size = tf.shape(input=tokens)[0]
    nsteps = tf.shape(input=tokens)[1]
    return expand_tile(past_length + tf.range(nsteps), batch_size)

def embed(X, we):
    e = tf.gather(we, X)
    h = tf.reduce_sum(input_tensor=e, axis=2)
    return h

def gpt2_featurizer(
    X,
    encoder,
    config,
    train=False,
    reuse=None,
    **kwargs
):
    initial_shape = tf.shape(input=X)
    X = tf.reshape(X, shape=tf.concat(([-1], initial_shape[-1:]), 0))
    X.set_shape([None, None])

    pos_values = get_pos_values(tf.shape(input=X)[1], encoder.vocab_size)
    X = tf.stack((X, tf.tile(pos_values, [tf.shape(input=X)[0], 1])), 2)

    with tf.compat.v1.variable_scope("model/featurizer", reuse=reuse):
        embed_weights = tf.compat.v1.get_variable(
            name="we",
            shape=[encoder.vocab_size + config.max_length, config.n_embed],
            initializer=tf.compat.v1.random_normal_initializer(stddev=config.weight_stddev),
        )
        if config.train_embeddings:
            embed_weights = dropout(embed_weights, config.embed_p_drop, train)
        else:
            embed_weights = tf.stop_gradient(embed_weights)
        h = embed(X, embed_weights)

        # Transformer
        pasts = [None] * config.n_layer
        for layer, past in enumerate(pasts):
            if (
                (config.n_layer - layer) == config.num_layers_trained
                and config.num_layers_trained != config.n_layer
            ):
                h = tf.stop_gradient(h)
                train_layer = False
            else:
                train_layer = train

            with tf.compat.v1.variable_scope("h%d" % layer):
                block_fn = functools.partial(
                    block, past=past, hparams=config, train=train
                )
                if config.low_memory_mode and train_layer:
                    block_fn = recompute_grad(block_fn, use_entire_scope=True)
                h = block_fn(h)

        h = norm(h, "ln_f")

        # Use hidden state at classifier token as input to final proj. + softmax
        clf_h = tf.reshape(h, [-1, config.n_embed])  # [batch * seq_len, embed]
        clf_token = encoder["_classify_"]
        pool_idx = tf.cast(
            tf.argmax(input=tf.cast(tf.equal(X[:, :, 0], clf_token), tf.float32), axis=1), tf.int32
        )
        batch, length, _ = shape_list(X)
        clf_h = tf.gather(
            clf_h,
            tf.range(batch, dtype=tf.int32) * length + pool_idx,
        )
        clf_h = tf.reshape(
            clf_h, shape=tf.concat((initial_shape[:-1], [config.n_embed]), 0)
        )
        seq_feats = tf.reshape(
            h, shape=tf.concat((initial_shape, [config.n_embed]), 0)
        )

        lengths = lengths_from_eos_idx(eos_idx=pool_idx, max_length=shape_list(X)[0])

        return {
            "embed_weights": embed_weights,
            "features": clf_h,
            "sequence_features": seq_feats,
            "eos_idx": pool_idx,
            "length": lengths
        }


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile

import tensorflow as tf
from tensorflow_model_analysis.api import model_eval_lib
from tensorflow_model_analysis.eval_saved_model import testutil
from tensorflow_model_analysis.eval_saved_model.example_trainers import csv_linear_classifier
from tensorflow_model_analysis.eval_saved_model.example_trainers import custom_estimator
from tensorflow_model_analysis.eval_saved_model.example_trainers import fixed_prediction_estimator
from tensorflow_model_analysis.eval_saved_model.example_trainers import linear_classifier
from tensorflow_model_analysis.eval_saved_model.post_export_metrics import metric_keys
from tensorflow_model_analysis.eval_saved_model.post_export_metrics import post_export_metrics
from tensorflow_model_analysis.slicer import slicer

class EvaluateTest(testutil.TensorflowModelAnalysisTest):

  def setUp(self):
    self.longMessage = True

  def _getTempDir(self):
    return tempfile.mkdtemp()

  def _exportEvalSavedModel(self, classifier):
    temp_model_location = os.path.join(self._getTempDir(), 'eval_export_dir')
    _, model_location = classifier(None, temp_model_location)
    return model_location

  def _writeTFExamplesToTFRecords(self, examples):
    data_location = os.path.join(self._getTempDir(), 'input_data.rio')
    with tf.python_io.TFRecordWriter(data_location) as writer:
      for example in examples:
        writer.write(example.SerializeToString())
    return data_location

  def _writeCSVToTextFile(self, examples):
    data_location = os.path.join(self._getTempDir(), 'input_data.csv')
    with open(data_location, 'w') as writer:
      for example in examples:
        writer.write(example + '\n')
    return data_location

  def assertMetricsAlmostEqual(self, got_value, expected_value):
    for (s, m) in got_value:
      self.assertIn(s, expected_value)
      self.assertDictElementsAlmostEqual(m, expected_value[s])

  def assertPlotsAlmostEqual(self, got_value, expected_value):
    for (s, m) in got_value:
      self.assertIn(s, expected_value)
      self.assertDictMatrixRowsAlmostEqual(m, expected_value[s])

  def testRunModelAnalysis(self):
    model_location = self._exportEvalSavedModel(
        linear_classifier.simple_linear_classifier)
    examples = [
        self._makeExample(age=3.0, language='english', label=1.0),
        self._makeExample(age=3.0, language='chinese', label=0.0),
        self._makeExample(age=4.0, language='english', label=1.0),
        self._makeExample(age=5.0, language='chinese', label=1.0)
    ]
    data_location = self._writeTFExamplesToTFRecords(examples)
    slice_spec = [slicer.SingleSliceSpec(columns=['language'])]
    eval_result = model_eval_lib.run_model_analysis(
        model_location,
        data_location,
        slice_spec=slice_spec,
        example_weight_key='age')
    # We only check some of the metrics to ensure that the end-to-end
    # pipeline works.
    expected = {
        (('language', 'chinese'),): {
            'accuracy': 0.5,
            'my_mean_label': 0.5,
            metric_keys.EXAMPLE_WEIGHT: 8.0,
            metric_keys.EXAMPLE_COUNT: 2.0,
        },
        (('language', 'english'),): {
            'accuracy': 1.0,
            'my_mean_label': 1.0,
            metric_keys.EXAMPLE_WEIGHT: 7.0,
            metric_keys.EXAMPLE_COUNT: 2.0,
        }
    }
    self.assertEqual(eval_result.config.model_location, model_location)
    self.assertEqual(eval_result.config.data_location, data_location)
    self.assertEqual(eval_result.config.slice_spec, slice_spec)
    self.assertMetricsAlmostEqual(eval_result.slicing_metrics, expected)
    self.assertFalse(eval_result.plots)

  def testRunModelAnalysisWithPlots(self):
    model_location = self._exportEvalSavedModel(
        fixed_prediction_estimator.simple_fixed_prediction_estimator)
    examples = [
        self._makeExample(prediction=0, label=1.0),
        self._makeExample(prediction=0.7, label=0.0),
        self._makeExample(prediction=0.8, label=1.0),
        self._makeExample(prediction=1.0, label=1.0)
    ]
    data_location = self._writeTFExamplesToTFRecords(examples)
    eval_result = model_eval_lib.run_model_analysis(
        model_location,
        data_location,
        add_metrics_callbacks=[post_export_metrics.auc_plots()])
    # We only check some of the metrics to ensure that the end-to-end
    # pipeline works.
    expected_metrics = {(): {metric_keys.EXAMPLE_COUNT: 4.0,}}
    expected_plots = {
        (): {
            metric_keys.AUC_PLOTS_MATRICES: [(8001, [
                2, 1, 0, 1, 1.0 / 1.0, 1.0 / 3.0
            ])],
        }
    }
    self.assertMetricsAlmostEqual(eval_result.slicing_metrics, expected_metrics)
    self.assertPlotsAlmostEqual(eval_result.plots, expected_plots)

  def testRunModelAnalysisForCSVText(self):
    model_location = self._exportEvalSavedModel(
        csv_linear_classifier.simple_csv_linear_classifier)
    examples = [
        '3.0,english,1.0', '3.0,chinese,0.0', '4.0,english,1.0',
        '5.0,chinese,1.0'
    ]
    data_location = self._writeCSVToTextFile(examples)
    eval_result = model_eval_lib.run_model_analysis(
        model_location, data_location, file_format='text')
    # We only check some of the metrics to ensure that the end-to-end
    # pipeline works.
    expected = {(): {'accuracy': 0.75, metric_keys.EXAMPLE_COUNT: 4.0}}
    self.assertMetricsAlmostEqual(eval_result.slicing_metrics, expected)

  def testMultipleModelAnalysis(self):
    model_location_1 = self._exportEvalSavedModel(
        linear_classifier.simple_linear_classifier)
    model_location_2 = self._exportEvalSavedModel(
        custom_estimator.simple_custom_estimator)
    examples = [
        self._makeExample(age=3.0, language='english', label=1.0),
        self._makeExample(age=3.0, language='chinese', label=0.0),
        self._makeExample(age=4.0, language='english', label=1.0),
        self._makeExample(age=5.0, language='chinese', label=1.0)
    ]
    data_location = self._writeTFExamplesToTFRecords(examples)
    eval_results = model_eval_lib.multiple_model_analysis(
        [model_location_1, model_location_2],
        data_location,
        slice_spec=[slicer.SingleSliceSpec(features=[('language', 'english')])])
    # We only check some of the metrics to ensure that the end-to-end
    # pipeline works.
    self.assertEqual(2, len(eval_results._results))
    expected_result_1 = {
        (('language', 'english'),): {
            'my_mean_label': 1.0,
            metric_keys.EXAMPLE_COUNT: 2.0
        }
    }
    expected_result_2 = {
        (('language', 'english'),): {
            'mean_label': 1.0,
            metric_keys.EXAMPLE_COUNT: 2.0
        }
    }
    self.assertMetricsAlmostEqual(eval_results._results[0].slicing_metrics,
                                  expected_result_1)
    self.assertMetricsAlmostEqual(eval_results._results[1].slicing_metrics,
                                  expected_result_2)

  def testMultipleDataAnalysis(self):
    model_location = self._exportEvalSavedModel(
        linear_classifier.simple_linear_classifier)
    data_location_1 = self._writeTFExamplesToTFRecords([
        self._makeExample(age=3.0, language='english', label=1.0),
        self._makeExample(age=3.0, language='english', label=0.0),
        self._makeExample(age=5.0, language='chinese', label=1.0)
    ])
    data_location_2 = self._writeTFExamplesToTFRecords(
        [self._makeExample(age=4.0, language='english', label=1.0)])
    eval_results = model_eval_lib.multiple_data_analysis(
        model_location, [data_location_1, data_location_2],
        slice_spec=[slicer.SingleSliceSpec(features=[('language', 'english')])])
    self.assertEqual(2, len(eval_results._results))
    # We only check some of the metrics to ensure that the end-to-end
    # pipeline works.
    expected_result_1 = {
        (('language', 'english'),): {
            metric_keys.EXAMPLE_COUNT: 2.0,
        }
    }
    expected_result_2 = {
        (('language', 'english'),): {
            metric_keys.EXAMPLE_COUNT: 1.0,
        }
    }
    self.assertMetricsAlmostEqual(eval_results._results[0].slicing_metrics,
                                  expected_result_1)
    self.assertMetricsAlmostEqual(eval_results._results[1].slicing_metrics,
                                  expected_result_2)

if __name__ == '__main__':
  tf.test.main()


from django.urls import path
from .views import *

urlpatterns = [
    path('get_letter/<int:pk>/', get_letter),
    path('get_letters/', get_letters),
    path('get_responses/', get_responses),
    path('create_letter/', create_letter),
    path('create_response/', create_response),
]


"""
Advent of Code 2020 :: Day 17: Conway Cubes
"""
import sys
from collections import defaultdict
from itertools import product
import pyperclip

def add_v(left, right):
    """Add two vectors (tuples) together."""
    return tuple(a + b for a, b in zip(left, right))

def neighborhood(posn):
    """Return all the neighbors of the given position."""
    offsets = [tuple(p) for p in product([-1, 0, 1], repeat=len(posn))]
    for off in offsets:
        posn0 = add_v(posn, off)
        if posn0 == posn:
            continue
        yield posn0

def tick(live_cells):
    """Return the active cells after the given tick."""
    # Compute number of living neighbor cells for all neighbors
    # that will have a living neighbor.
    live_neighbors = defaultdict(int)
    for posn in live_cells:
        for neighbor in neighborhood(posn):
            live_neighbors[neighbor] += 1

    next_live_cells = set()
    for cell in live_neighbors:
        # If a cube is active and exactly 2 or 3 of its neighbors are also
        # active, the cube remains active. Otherwise, the cube becomes
        # inactive.
        living_neighbors = live_neighbors[cell]
        cell_is_alive = (cell in live_cells)
        if cell_is_alive and (living_neighbors == 2 or living_neighbors == 3):
            next_live_cells.add(cell)
        # If a cube is inactive but exactly 3 of its neighbors are active, the
        # cube becomes active. Otherwise, the cube remains inactive.
        if not cell_is_alive and living_neighbors == 3:
            next_live_cells.add(cell)

    return next_live_cells

def main():
    """Main program."""
    live_cells3 = set()
    live_cells4 = set()
    for y, line in enumerate(sys.stdin):
        for x, char in enumerate(line.strip()):
            if char == '#':
                live_cells3.add((x, -y, 0))
                live_cells4.add((x, -y, 0, 0))

    for _ in range(6):
        live_cells3 = tick(live_cells3)
        live_cells4 = tick(live_cells4)

    soln1 = len(live_cells3)
    print(f"The solution to part 1 is {soln1}")
    # assert soln1 == 362

    soln2 = len(live_cells4)
    print(f"The solution to part 2 is {soln2}")
    # assert soln2 == 362
    pyperclip.copy(soln2)

if __name__ == '__main__':
    main()