import pandas as pd
import numpy as np
from scipy import stats
from collections import defaultdict
import re
from sklearn.linear_model import LinearRegression
from tqdm import tqdm
from collections import defaultdict
from scipy.interpolate import interp1d
from tslearn.clustering import TimeSeriesKMeans, to_time_series_dataset

class always_zero:
    '''Класс - заглушка, возвращающая всегда 0
    '''
    def predict(self, x):
        return np.zeros_like(x)

def add_quadratic_trend(df_train, df_test):

    models_dict=defaultdict(dict)
    column = 'y'
    for galaxy in tqdm(df_train['galaxy'].unique()):
        index_train = df_train[(df_train.galaxy == galaxy) & (df_train[column].notnull())].index
        y = df_train.loc[index_train, column].to_numpy().reshape(-1, 1)
        X = df_train.loc[index_train, 'galactic year']
        min_year = min(X)
        max_year = max(X)
        X_scaled = X.apply(lambda x: (10*(x-min_year)/(max_year-min_year))**2).to_numpy().reshape(-1, 1)
        if len(y):
            model = LinearRegression().fit(X_scaled, y)
        else:
            model = always_zero()
        models_dict[galaxy] = [model, min_year, max_year]

        if len(y):
            model, min_year, max_year = models_dict[galaxy]
            index_train = df_train[df_train.galaxy == galaxy].index
            X_scaled = df_train.loc[index_train, 'galactic year'].apply(lambda x: (10*(x-min_year)/(max_year-min_year))**2).to_numpy().reshape(-1, 1)
            trend = model.predict(X_scaled).reshape(-1)
            df_train.loc[index_train,  f'quadratic_{column}'] =  trend

    for galaxy in tqdm(df_train['galaxy'].unique()):
        index_test = df_test[df_test.galaxy == galaxy].index
        X_scaled = df_test.loc[index_test, 'galactic year'].apply(lambda x: (10*(x-min_year)/(max_year-min_year))**2).to_numpy().reshape(-1, 1)
        if len(X_scaled):
            model, min_year, max_year = models_dict[galaxy]
            trend = pd.Series(model.predict(X_scaled).reshape(-1))
            trend.index = index_test
            df_test.loc[index_test, f'quadratic_{column}'] =  trend

    return df_train, df_test

def preprocessing_add_epoch_statistics(train, test):

    # add_year_dummy = lambda year, num, p: True if year>=(990000 + num * p) and year<=(990000 + num * (p + 1)) else False

    train['epoch'] = train['galactic year'].apply(lambda x: int((x-990000)/5000))
    train['ten_years'] = train['galactic year'].apply(lambda x: int((x-990000 - int((x-990000)/5000)*5000)/1000))
    train = pd.concat([train, pd.get_dummies(train['epoch'], drop_first=True)], axis=1)

    test['epoch'] = test['galactic year'].apply(lambda x: int((x-990000)/5000))
    test['ten_years'] = test['galactic year'].apply(lambda x: int((x-990000 - int((x-990000)/5000)*5000)/1000))
    test = pd.concat([test, pd.get_dummies(test['epoch'], drop_first=True)], axis=1)

    # Оставим толь колонки которые есть в тесте
    train = train.loc[:, list(test.columns) + ['y']]
    return train, test

def delete_trend(df_train, df_test):
    models_dict=defaultdict(dict)
    for column in tqdm(df_train.columns):
        if column not in ['galactic year', 'galaxy']:
            for galaxy in df_train['galaxy'].unique():
                index_train = df_train[(df_train.galaxy == galaxy) & (df_train[column].notnull())].index
                y = df_train.loc[index_train, column].to_numpy().reshape(-1, 1)
                X = df_train.loc[index_train, 'galactic year'].to_numpy().reshape(-1, 1)
                if len(y):
                    model = LinearRegression().fit(X, y)
                else:
                    model = always_zero()

                models_dict[column][galaxy] = model

                if len(y):
                    trend = pd.Series(models_dict[column][galaxy].predict(X).reshape(-1))
                    trend.index = index_train
                    df_train.loc[index_train, column] = df_train.loc[index_train, column] - trend

    for column in tqdm(df_test.columns):
        if column not in ['galactic year', 'galaxy']:
            for galaxy in df_test['galaxy'].unique():
                index_test = df_test[(df_test.galaxy == galaxy) & (df_train[column].notnull())].index
                y = df_test.loc[index_test, column].to_numpy().reshape(-1, 1)
                X = df_test.loc[index_test, 'galactic year'].to_numpy().reshape(-1, 1)
                if len(X):
                    trend = pd.Series(models_dict[column][galaxy].predict(X).reshape(-1))
                    trend.index = index_test
                    df_test.loc[index_test, column] =  trend

    return df_train, df_test, models_dict

def add_y_shift(df_train, df_test):
    df_train_new = pd.DataFrame()
    df_test_new = pd.DataFrame()
    df_train['train'] = 1
    df_test['train'] = 0
    df_all = pd.concat([df_train,df_test])
    for galactic in df_train.galaxy.unique():
        df_galactic = df_all[df_all.galaxy == galactic].sort_values(['galactic year']).copy()
        df_galactic = pd.concat([df_galactic, df_galactic['y'].shift(1)], axis=1)
        df_galactic.columns = list(df_all.columns) + ['y_shift']
        df_galactic['y_shift'].iloc[0] = np.mean(df_galactic.y_shift)
        for i,j in enumerate(df_galactic.y_shift):
            if np.isnan(j):
                df_galactic['y_shift'].iloc[i] = df_galactic['y_shift'].iloc[i-1]
        df_train_new = pd.concat([df_train_new, df_galactic[df_galactic.train == 1].drop(['train'], axis=1)], axis=0)
        df_test_new = pd.concat([df_test_new, df_galactic[df_galactic.train == 0].drop(['train'], axis=1)], axis=0)
    # df_train_new.loc[df_train_new.galaxy=='NGC 5253','y_shift'] = df_train_new[df_train_new.galaxy=='NGC 5253']['y'].iloc[0] # Выделила отдельно галактику, так как по ней всего 1 строчка
    return df_train_new, df_test_new.drop('y', axis=1)

def my_add_feature(df_train, df_test, columns, fill_value, coef, num_k):
    edge_right= defaultdict()
    edge_left = defaultdict()

    for column in columns:

        df_col = df_train[df_train[column].isnull()==False][[column,'y']]
        st = stats.binned_statistic(df_col[column], df_col['y'], statistic='mean', bins=20)

        # Ищем фичи и границу для них, распределение которых выглядит как нижний правый угол
        min_value = st.statistic[0] * (1 - coef)
        max_value = st.statistic[0] * (1 + coef)
        k=0
        mask = ~np.isnan(st.statistic)
        for value, edge in zip(st.statistic[mask], st.bin_edges[:-1][mask]):
            if (value>=min_value) & (value<=max_value):
                k+=1
            else:
                if k>num_k:
                    #print(k, round(edge,2),'right', column)
                    edge_right[column] = edge
                break
        # Ищем фичи и границу для них, распределение которых выглядит как нижний левый угол
        min_value = st.statistic[-1] * (1 - coef)
        max_value = st.statistic[-1] * (1 + coef)
        k=0
        mask = ~np.isnan(st.statistic)
        for value, edge in zip(st.statistic[mask][::-1], st.bin_edges[1:][mask][::-1]):
            if (value >= min_value) & (value <= max_value):
                k+=1
            else:
                if k>num_k:
                    #print(k, np.round(edge,2), 'left', column)
                    edge_left[column] = edge
                break
    # Теперь создаём новые фичи на основе отобранных
    for column in edge_left.keys():
        column1 = 'const__' + column
        column2 = 'varios__' + column
        column3 = 'nan__' + column
        df_train[column1] = df_train[column].apply(lambda x: 1 if x >= edge_left[column] else 0)
        df_train[column2] = df_train[column].apply(lambda x: x if x < edge_left[column] else fill_value)
        df_train[column3] = df_train[column].apply(lambda x: 1 if np.isnan(x) else 0)
        #df_train = df_train.drop(column, axis=1)
    for column in edge_right.keys():
        column1 = 'const__' + column
        column2 = 'varios__' + column
        column3 = 'nan__' + column
        df_train[column1] = df_train[column].apply(lambda x: 1 if x <= edge_right[column] else 0 )
        df_train[column2] = df_train[column].apply(lambda x: x if x > edge_right[column] else fill_value)
        df_train[column3] = df_train[column].apply(lambda x: 1 if np.isnan(x) else 0)
        #df_train = df_train.drop(column, axis=1)

    # Для тестовой выборки проделаем то же самое,  с уже имеющимися границами
    for column in edge_left.keys():
        column1 = 'const__' + column
        column2 = 'varios__' + column
        column3 = 'nan__' + column
        df_test[column1] = df_test[column].apply(lambda x: 1 if x >= edge_left[column] else 0)
        df_test[column2] = df_test[column].apply(lambda x: x if x < edge_left[column] else fill_value)
        df_test[column3] = df_test[column].apply(lambda x: 1 if np.isnan(x) else 0)
        #df_train = df_train.drop(column, axis=1)
    for column in edge_right.keys():
        column1 = 'const__' + column
        column2 = 'varios__' + column
        column3 = 'nan__' + column
        df_test[column1] = df_test[column].apply(lambda x: 1 if x <= edge_right[column] else 0 )
        df_test[column2] = df_test[column].apply(lambda x: x if x > edge_right[column] else fill_value)
        df_test[column3] = df_test[column].apply(lambda x: 1 if np.isnan(x) else 0)
        #df_train = df_train.drop(column, axis=1)
    return  df_train, df_test, edge_right, edge_left

def fillna(df_train, df_test, columns, value):

    appended = df_train.append(df_test, ignore_index=True)

    columns = [i for i in appended.columns if i not in ['galaxy', 'galactic year', 'y']]

    for galaxy in tqdm(appended.galaxy.unique()):
        for column in columns:
            appended_index = appended[(appended.galaxy == galaxy) & (appended[column].notnull())].index
            index_train = df_train[(appended.galaxy == galaxy) & df_train[column].isnull()].index
            index_test = df_test[(appended.galaxy == galaxy) & df_test[column].isnull()].index
            if (index_train.shape[0] + index_test.shape[0]) > 0 and (appended_index.shape[0]) > 2:
                year = appended.loc[appended_index, 'galactic year'].to_numpy()
                column_to_predict = appended.loc[appended_index, column].to_numpy()
                model = interp1d(year, column_to_predict, kind='linear',fill_value="extrapolate")
                df_train.loc[index_train, column] = model(df_train.loc[index_train, 'galactic year'].to_numpy().reshape(-1, 1))
                df_test.loc[index_test, column] = model(df_test.loc[index_test, 'galactic year'].to_numpy().reshape(-1, 1))

    for column in columns:
        if column in df_train.columns:
            df_train[column] = df_train[column].fillna(value)
    for column in columns:
        if column in df_test.columns:
            df_test[column] = df_test[column].fillna(value)
    return df_train, df_test

def add_features_from_regression(df_train, df_test):
    models_dict=defaultdict(dict)
    column = 'y'
    df_train[f'regres_{column}'] =  0
    df_train[f'coef_regr_{column}'] = 0
    df_train[f'inter_regr__{column}'] = 0
#     for column in tqdm(df_train.columns):
#         df_train[f'regres_{column}'] =  0
#         df_train[f'coef_regr_{column}'] = 0
#         df_train[f'inter_regr__{column}'] = 0
#         if column not in ['galactic year', 'galaxy']:
    for galaxy in df_train['galaxy'].unique():
        index_train = df_train[(df_train.galaxy == galaxy) & (df_train[column].notnull())].index
        y = df_train.loc[index_train, column].to_numpy().reshape(-1, 1)
        X = df_train.loc[index_train, 'galactic year'].to_numpy().reshape(-1, 1)
        if len(y):
            model = LinearRegression().fit(X, y)
        else:
            model = always_zero()

        models_dict[galaxy] = model

        if len(y):
            index_train = df_train[df_train.galaxy == galaxy].index
            X = df_train.loc[index_train, 'galactic year'].to_numpy().reshape(-1, 1)
            trend = pd.Series(models_dict[galaxy].predict(X).reshape(-1))
            trend.index = index_train
            df_train.loc[index_train, f'regres_{column}'] =  trend
            df_train.loc[index_train, f'coef_regr_{column}'] = models_dict[galaxy].coef_[0][0]*1000000 # Добавили умножение на 100000, чтобы число было не слишком маленьким
            df_train.loc[index_train, f'inter_regr_{column}'] = models_dict[galaxy].intercept_[0]

#     for column in tqdm(df_test.columns):
#         df_test[f'regres_{column}'] =  0
#         df_test[f'coef_regr_{column}'] = 0
#         df_test[f'inter_regr__{column}'] = 0
#         if column not in ['galactic year', 'galaxy']:
    for galaxy in df_train['galaxy'].unique():
        index_test = df_test[df_test.galaxy == galaxy].index
        X = df_test.loc[index_test, 'galactic year'].to_numpy().reshape(-1, 1)
        if len(X):
            trend = pd.Series(models_dict[galaxy].predict(X).reshape(-1))
            trend.index = index_test
            df_test.loc[index_test, f'regres_{column}'] =  trend
            df_test.loc[index_test, f'coef_regr_{column}'] = models_dict[galaxy].coef_[0][0]*1000000
            df_test.loc[index_test, f'inter_regr__{column}'] = models_dict[galaxy].intercept_[0]
    return df_train, df_test

def add_label_from_clusterization(df_train, df_test):
    y_galaxy = []
    for galactic in df_train.galaxy.unique():
        y_galaxy.append(df_train[df_train.galaxy == galactic].sort_values(['galactic year']).y.values)
    X_galaxy = to_time_series_dataset(y_galaxy)
    km_dba = TimeSeriesKMeans(n_clusters=3, metric="dtw", max_iter=10,
                              max_iter_barycenter=5,
                              random_state=0).fit(X_galaxy)
    labels = list(km_dba.labels_)
    df_train['label_cluster'] = 0
    df_test['label_cluster'] = 0
    for i, galactic in enumerate(df_train.galaxy.unique()):
        print(i, labels[i], galactic)
        df_train.loc[df_train.galaxy == galactic, 'label_cluster'] = labels[i]
        df_test.loc[df_test.galaxy == galactic,'label_cluster'] = labels[i]
    return df_train, df_test

def add_male_plus_female(train, test):

    train['Life expectancy at birth, all (galactic years)'] = (train['Life expectancy at birth, male (galactic years)'] + train['Life expectancy at birth, female (galactic years)'])

    train['Labour force participation rate (% ages 15 and older), all'] = (train['Labour force participation rate (% ages 15 and older), female'] + train['Labour force participation rate (% ages 15 and older), male'])

    train['Mortality rate, all grown up (per 1,000 people)'] = (train['Mortality rate, female grown up (per 1,000 people)'] + train['Mortality rate, male grown up (per 1,000 people)'])

    train['Intergalactic Development Index (IDI), all'] = (train['Intergalactic Development Index (IDI), male'] + train['Intergalactic Development Index (IDI), female'])

    train['Population with at least some secondary education, all (% ages 25 and older)'] = (train['Population with at least some secondary education, male (% ages 25 and older)'] + train['Population with at least some secondary education, female (% ages 25 and older)'])

    train['Estimated gross galactic income per capita, all'] = (train['Estimated gross galactic income per capita, male'] + train['Estimated gross galactic income per capita, female'])

    train['Expected years of education, all (galactic years)'] = (train['Expected years of education, male (galactic years)'] + train['Expected years of education, female (galactic years)'])

    train['Mean years of education, all (galactic years)'] = (train['Mean years of education, male (galactic years)'] + train['Mean years of education, female (galactic years)'])

    test['Life expectancy at birth, all (galactic years)'] = (test['Life expectancy at birth, male (galactic years)'] + test['Life expectancy at birth, female (galactic years)'])

    test['Labour force participation rate (% ages 15 and older), all'] = (test['Labour force participation rate (% ages 15 and older), female'] + test['Labour force participation rate (% ages 15 and older), male'])

    test['Mortality rate, all grown up (per 1,000 people)'] = (test['Mortality rate, female grown up (per 1,000 people)'] + test['Mortality rate, male grown up (per 1,000 people)'])

    test['Intergalactic Development Index (IDI), all'] = (test['Intergalactic Development Index (IDI), male'] + test['Intergalactic Development Index (IDI), female'])

    test['Population with at least some secondary education, all (% ages 25 and older)'] = (test['Population with at least some secondary education, male (% ages 25 and older)'] + test['Population with at least some secondary education, female (% ages 25 and older)'])

    test['Estimated gross galactic income per capita, all'] = (test['Estimated gross galactic income per capita, male'] + test['Estimated gross galactic income per capita, female'])

    test['Expected years of education, all (galactic years)'] = (test['Expected years of education, male (galactic years)'] + test['Expected years of education, female (galactic years)'])

    test['Mean years of education, all (galactic years)'] = (test['Mean years of education, male (galactic years)'] + test['Mean years of education, female (galactic years)'])

    return train, test

# Функция, которая объединяет в себе весь препроцессинг
def preprocessing_all(df_train, df_test, trend_features = False, trend_y = False, fill_value = -10, coeff = 0.2, num_k = 7):

    df_train = df_train[df_train['galaxy'] != 'NGC 5253']

    df_train, df_test = add_quadratic_trend(df_train, df_test)

    true_existence_expectancy_index = df_test['existence expectancy index']

    if trend_features:
        df_train, df_test, models_dict = delete_trend(df_train, df_test)

    df_train, df_test = add_features_from_regression(df_train, df_test)
    columns = [i for i in df_test.columns if i not in ['galaxy']]
    df_train, df_test = add_label_from_clusterization(df_train, df_test)

    galaxy_train = df_train['galaxy'].unique()
    galaxy_test = df_test['galaxy'].unique()
    df_train_galaxy = df_train['galaxy']
    df_test_galaxy = df_test['galaxy']
    # Получим колонки - galaxy_del, которые нужно выкинуть из train, так как их не будет в тесте
    galaxy_del = list(set(galaxy_train)- set(galaxy_test))
    galaxy_del = ['galaxy_' + name for name in galaxy_del]
    # one hot encoding переменной galaxy
    df_train = pd.get_dummies(df_train, columns=['galaxy'])
    df_test = pd.get_dummies(df_test, columns=['galaxy'])
    df_train = df_train.drop(galaxy_del, axis=1)

    df_train, df_test, edge_right, edge_left = my_add_feature(df_train, df_test, columns, fill_value, coeff, num_k)

    df_train['galaxy'] = df_train_galaxy
    df_test['galaxy'] = df_test_galaxy

    df_train, df_test = preprocessing_add_epoch_statistics(df_train, df_test)

    if trend_y:
        column = 'y'

        for galaxy in df_train['galaxy'].unique():
            index_train = df_train[(df_train.galaxy == galaxy)].index
            X = df_train.loc[index_train, 'galacticyear'].to_numpy().reshape(-1, 1)
            trend = pd.Series(models_dict[column][galaxy].predict(X).reshape(-1))
            trend.index = index_train
            df_train.loc[index_train, 'y_trend'] = trend

        for galaxy in df_test['galaxy'].unique():
            index_test = df_test[df_test.galaxy == galaxy].index
            X = df_test.loc[index_test, 'galacticyear'].to_numpy().reshape(-1, 1)
            trend = pd.Series(models_dict[column][galaxy].predict(X).reshape(-1))
            trend.index = index_test
            df_test.loc[index_test, 'y_trend'] = trend

    df_train, df_test = add_y_shift(df_train, df_test)

    df_test.sort_index(inplace=True)
    df_train.sort_index(inplace=True)

    df_train, df_test = fillna(df_train, df_test, columns, fill_value)

    df_train, df_test = add_male_plus_female(df_train, df_test)

    df_train = df_train.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '', str(x)))
    df_test = df_test.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '', str(x)))

    return df_train, df_test, true_existence_expectancy_index

if __name__ == '__main__':

    train = pd.read_csv("data/train.csv")
    test = pd.read_csv("data/test.csv")

    train_upd, test_upd, true_existence_expectancy_index = preprocessing_all(train, test)
    # preprocessing_add_epoch_statistics(train, test)


def permute(arr):
    result = [arr[:]]
    c = [0] * len(arr)
    i = 0
    while i < len(arr):
        if c[i] < i:
            if i % 2 == 0:
                arr[0], arr[i] = arr[i], arr[0]
            else:
                arr[c[i]], arr[i] = arr[i], arr[c[i]]
            result.append(arr[:])
            c[i] += 1
            i = 0
        else:
            c[i] = 0
            i += 1
    return result

# itertools.permutation를 이용하면, for문을 사용하지 않고도 순열을 구할 수 있다.

import itertools

pool = ['A', 'B', 'C']
print(list(map(''.join, itertools.permutations(pool)))) # 3개의 원소로 수열 만들기
print(list(map(''.join, itertools.permutations(pool, 2)))) # 2개의 원소로 수열 만들기

workspace/프로그래밍강의_파이썬을 파이썬답게/순열과 조합 - combinations, permutations.py


import torch as th
from torch import nn
from torch.optim import Adam, RMSprop
from math import exp

import numpy as np
from copy import deepcopy

from Model import ActorNetwork, CriticNetwork
from utils import to_tensor_var
from Memory import ReplayMemory

EVAL_EPISODES = 10

class MAPPO(object):
    """
    An agent learned with PPO using Advantage Actor-Critic framework
    - Actor takes state as input
    - Critic takes both state and action as input
    - agent interact with environment to collect experience
    - agent training with experience to update policy
    - adam seems better than rmsprop for ppo
    """
    def __init__(self, env, state_dim, action_dim, n_agents, action_lower_bound, action_higher_bound,
                 noise=0, tau=300,
                 memory_capacity=10, max_steps=None,
                 roll_out_n_steps=10, target_tau=1.0,
                 target_update_steps=5, clip_param=0.2,
                 reward_gamma=0.99, reward_scale=1.,
                 actor_output_act=nn.functional.softmax, critic_loss="mse",
                 actor_lr=0.01, critic_lr=0.01,
                 optimizer_type="adam", entropy_reg=0.00,
                 max_grad_norm=None, batch_size=10, episodes_before_train=0,
                 use_cuda=False):

        self.env = env
        self.state_dim = state_dim
        self.action_dim = action_dim
        self.env_state = self.env.reset()
        self.n_episodes = 0
        self.n_steps = 0
        self.max_steps = max_steps
        self.n_agents = n_agents

        self.reward_gamma = reward_gamma
        self.reward_scale = reward_scale

        self.action_lower_bound = action_lower_bound
        self.action_higher_bound = action_higher_bound

        self.memory = ReplayMemory(memory_capacity)

        self.actor_output_act = actor_output_act
        self.critic_loss = critic_loss
        self.actor_lr = actor_lr
        self.critic_lr = critic_lr
        self.optimizer_type = optimizer_type
        self.entropy_reg = entropy_reg
        self.max_grad_norm = max_grad_norm
        self.batch_size = batch_size
        self.episodes_before_train = episodes_before_train
        self.noise = noise
        self.tau = tau

        self.use_cuda = use_cuda and th.cuda.is_available()

        self.roll_out_n_steps = roll_out_n_steps
        self.target_tau = target_tau
        self.target_update_steps = target_update_steps
        self.clip_param = clip_param

        self.actors = [ActorNetwork(self.state_dim, self.action_dim, self.actor_output_act)] * self.n_agents
        critic_state_dim = self.n_agents * self.state_dim
        critic_action_dim = self.n_agents * self.action_dim
        self.critics = [CriticNetwork(critic_state_dim, critic_action_dim, 1)] * self.n_agents
        # to ensure target network and learning network has the same weights
        self.actors_target = deepcopy(self.actors)
        self.critics_target = deepcopy(self.critics)

        if optimizer_type == "adam":
            self.actors_optimizer = [Adam(a.parameters(), lr=self.actor_lr) for a in self.actors]
            self.critics_optimizer = [Adam(c.parameters(), lr=self.critic_lr) for c in self.critics]
        elif optimizer_type == "rmsprop":
            self.actors_optimizer = [RMSprop(a.parameters(), lr=self.actor_lr) for a in self.actors]
            self.critics_optimizer = [RMSprop(c.parameters(), lr=self.critic_lr) for c in self.critics]

        if self.use_cuda:
            for a in self.actors:
                a.cuda()
            for c in self.critics:
                c.cuda()
        self.eval_rewards = []
        self.mean_rewards = []
        self.episodes = []
        self.eval_phi = []
        self.mean_phi = []

    # agent interact with the environment to collect experience
    def interact(self):
        if (self.max_steps is not None) and (self.n_steps >= self.max_steps):
            self.env_state = self.env.reset()
            self.n_steps = 0
        states = []
        actions = []
        rewards = []
        # take n steps
        for i in range(self.roll_out_n_steps):
            states.append(self.env_state)
            action = self.choose_action(self.env_state)
            next_state, reward, done, _, phi = self.env.step(action)
            # done = done[0]
            actions.append(action)
            rewards.append(reward)
            final_state = next_state
            self.env_state = next_state
            if done:
                self.env_state = self.env.reset()
                break
        # discount reward
        if done:
            final_r = [0.0] * self.n_agents
            self.n_episodes += 1
            self.episode_done = True
        else:
            self.episode_done = False
            final_action = self.choose_action(final_state)
            final_r = self.value(final_state, final_action)

        rewards = np.array(rewards)
        for agent_id in range(self.n_agents):
            rewards[:,agent_id] = self._discount_reward(rewards[:,agent_id], final_r[agent_id])
        rewards = rewards.tolist()
        self.n_steps += 1

        self.eval_rewards.append(np.sum(reward))
        self.eval_phi.append(np.sum(phi))
        if self.episode_done and ((self.n_episodes+1)%EVAL_EPISODES == 0):
            mean_reward = np.mean(np.array(self.eval_rewards))
            self.mean_rewards.append(mean_reward)
            self.mean_phi.append(np.mean(np.array(self.eval_phi)))
            self.episodes.append(self.n_episodes+1)
            print("Episode:", self.n_episodes+1, "  Average Reward: ", mean_reward)
            self.eval_rewards = []
            self.eval_phi = []

        self.memory.push(states, actions, rewards)

    # train on a roll out batch
    def train(self):
        if self.n_episodes <= self.episodes_before_train:
            pass

        batch = self.memory.sample(self.batch_size)
        states_var = to_tensor_var(batch.states, self.use_cuda).view(-1, self.n_agents, self.state_dim)
        actions_var = to_tensor_var(batch.actions, self.use_cuda).view(-1, self.n_agents, self.action_dim)
        rewards_var = to_tensor_var(batch.rewards, self.use_cuda).view(-1, self.n_agents, 1)
        whole_states_var = states_var.view(-1, self.n_agents*self.state_dim)
        whole_actions_var = actions_var.view(-1, self.n_agents*self.action_dim)

        for agent_id in range(self.n_agents):
            # update actor network
            self.actors_optimizer[agent_id].zero_grad()
            values = self.critics[agent_id](whole_states_var, whole_actions_var).detach()
            advantages = rewards_var[:,agent_id,:] - values
            # # normalizing advantages seems not working correctly here
            # advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-5)
            action_log_probs = self.actors[agent_id](states_var[:,agent_id,:]).detach()
            action_log_probs = th.sum(action_log_probs * actions_var[:,agent_id,:], 1)
            old_action_log_probs = self.actors_target[agent_id](states_var[:,agent_id,:]).detach()
            old_action_log_probs = th.sum(old_action_log_probs * actions_var[:,agent_id,:], 1)
            ratio = th.exp(action_log_probs - old_action_log_probs)
            surr1 = ratio * advantages
            surr2 = th.clamp(ratio, 1.0 - self.clip_param, 1.0 + self.clip_param) * advantages
            # PPO's pessimistic surrogate (L^CLIP)
            actor_loss = -th.mean(th.min(surr1, surr2))
            actor_loss.requires_grad_(True)
            actor_loss.backward()
            if self.max_grad_norm is not None:
                nn.utils.clip_grad_norm(self.actors[agent_id].parameters(), self.max_grad_norm)
            self.actors_optimizer[agent_id].step()

            # update critic network
            self.critics_optimizer[agent_id].zero_grad()
            target_values = rewards_var[:,agent_id,:]
            # if self.critic_loss == "huber":
            #     critic_loss = nn.functional.smooth_l1_loss(values, target_values)
            # else:
            #     critic_loss = nn.MSELoss()(values, target_values)
            critic_loss = 0.5 * (values - target_values).pow(2).mean()
            critic_loss.requires_grad_(True)
            critic_loss.backward()
            if self.max_grad_norm is not None:
                nn.utils.clip_grad_norm(self.critics[agent_id].parameters(), self.max_grad_norm)
            self.critics_optimizer[agent_id].step()

            # update actor target network and critic target network
            if self.n_steps % self.target_update_steps == 0 and self.n_steps > 0:
                self._soft_update_target(self.actors_target[agent_id], self.actors[agent_id])
                self._soft_update_target(self.critics_target[agent_id], self.critics[agent_id])

    def _soft_update_target(self, target, source):
        for t, s in zip(target.parameters(), source.parameters()):
            t.data.copy_(
                (1. - self.target_tau) * t.data + self.target_tau * s.data)

    def getactionbound(self, a, b, x, i):
        x = (x - a) * (self.action_higher_bound[i] - self.action_lower_bound[i]) / (b - a) \
            + self.action_lower_bound[i]
        return x

    def choose_action(self, state):
        state_var = to_tensor_var([state], self.use_cuda)
        action = np.zeros((self.n_agents, self.action_dim))

        for agent_id in range(self.n_agents):
            action_var = (self.actors[agent_id](state_var[:,agent_id,:]))
            if self.use_cuda:
                action[agent_id] = action_var.data.cpu().numpy()[0]
            else:
                action[agent_id] = action_var.data.numpy()[0]

        for n in range(self.n_agents):
            for i in range(6):
                if (self.n_episodes < 600): e = self.n_episodes
                else: e = self.n_episodes
                action[n][i] = -exp(-e/self.tau) + self.noise
        b = 1
        a = -1
        if self.action_dim > 6:
            print("Wrong!")
        for n in range(self.n_agents):
            action[n][0] = 0 if action[n][0] <= 0 else 1
            action[n][1] = round(self.getactionbound(a, b, action[n][1], 1))
            action[n][2] = self.getactionbound(a, b, action[n][2], 2)
            action[n][3] = self.getactionbound(a, b, action[n][3], 3)
            action[n][4] = self.getactionbound(a, b, action[n][4], 4)
            action[n][5] = self.getactionbound(a, b, action[n][5], 5)

        return action

    # evaluate value for a state-action pair
    def value(self, state, action):
        state_var = to_tensor_var([state], self.use_cuda)
        action_var = to_tensor_var([action], self.use_cuda)
        whole_state_var = state_var.view(-1, self.n_agents*self.state_dim)
        whole_action_var = action_var.view(-1, self.n_agents*self.action_dim)
        values = np.zeros(self.n_agents)
        for agent_id in range(self.n_agents):
            value_var = self.critics[agent_id](whole_state_var, whole_action_var)
            if self.use_cuda:
                values[agent_id] = value_var.data.cpu().numpy()[0]
            else:
                values[agent_id] = value_var.data.numpy()[0]
        return values

    def _discount_reward(self, rewards, final_value):
        discounted_r = np.zeros_like(rewards)
        running_add = final_value
        for t in reversed(range(0, len(rewards))):
            running_add = running_add * self.reward_gamma + rewards[t]
            discounted_r[t] = running_add
        return discounted_r


# to use this first: # > conda activate pygmt import os import pygmt from pathlib import Path
# Note this script could be run with pygmt 0.4.1

import os
import pygmt
from pathlib import Path

# directory to the aspect Lab
ASPECT_LAB_DIR = os.environ['ASPECT_LAB_DIR']
# This is a list of names of subduction zones
loc = ['alu','cal','cam','car','cas','cot','hal','hel','him','hin','izu',
	'ker','kur','mak','man','mue','pam',
    'phi','png','puy','ryu','sam','sco','sol','sul','sum','van'];
# Here, we use pygmt to create a figure
fig = pygmt.Figure()
pygmt.config(FONT='Times-Roman')
pygmt.config(FONT_LABEL='Times-Roman,12p')
# Initiate a basemap within that figure
fig.basemap(region="g", projection="W15c", frame=True)
grid = pygmt.datasets.load_earth_relief(resolution="10m")
fig.grdimage(grid=grid,cmap="gray")
fig.coast(shorelines="1/0.5p,black")
# make cpt(color) file
cptfile = os.path.join(ASPECT_LAB_DIR, "shilofue", "gmt", "slabdepth.cpt")
assert(os.path.isfile(cptfile))
# pygmt.makecpt(cmap="buda",series=(-700,0,100), color_model="+c",output=cptfile)
# pygmt.makecpt(cmap="batlow",series=(-700,0,100), color_model="+c-700-0",output=cptfile)
# plot the slab 2d data set
slab2dir = os.path.join(ASPECT_LAB_DIR, "large_data_files", "slab2", "Slab2Distribute_Mar2018")
for i in range(len(loc)):
    #print(loc[i])
    # use path to look for grd file, this is implemented because there is an appendix in the file name
    grdfile_name_base = loc[i] + '_slab2_dep'
    pathlist = Path(slab2dir).rglob(grdfile_name_base + '*'+ '.grd')
    for path in pathlist:
        grdfile = str(path)
        print(grdfile)
    # grdfile = os.path.join(slab2dir, loc[i] + '_slab2_dep.grd')
    # plot, if the cptfile (below) shoots error, use the Buda color scheme instead "on top"
    # fig.grdimage(grid=grdfile, nan_transparent="gray", cmap='buda')
    # note: in Magali's original notebook, there is a "Q" option, which should be the same as the "nan_transparent" command, but that doesn't work for me.
    fig.grdimage(grid=grdfile, nan_transparent="gray", cmap=cptfile)
# fig.colorbar(cmap=cptfile,position="JMR",box=False,frame=["x+lDepth", "y+lkm"])
fig.savefig('map_topo_slabs.png')


import unittest
from word_search import WordSearch

class TestWordSearch(unittest.TestCase):
    def test_word_search(self):
        res1 = WordSearch(12, '1) строка разбивается на набор строк через выравнивание по заданной ширине.', 'выравнивание')
        res2 = WordSearch(5, 'Chromium OS дистрибутив операционной системы GNU/Linux', 'Chrom')

        self.assertEqual(res1, [0, 0, 0, 0, 1, 0, 0])
        self.assertEqual(res2, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

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


from django.shortcuts import render
from mainapp.models import Product, ProductCategory
from basketapp.models import Basket
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
    context = {
        'title': 'GeekShop'
    }
    return render(request, 'mainapp/index.html', context)

def products(request, page=1, category_id=0):
    goods = Product.objects.filter(category_id=category_id).order_by(
        'price') if category_id > 0 else Product.objects.all().order_by('price')
    paginator = Paginator(goods, 3)

    # if request.user.is_authenticated:
    #     baskets = Basket.objects.filter(user=request.user)
    #     for basket in baskets:
    #         print(basket.product)

    try:
        products_paginator = paginator.page(page)
    except PageNotAnInteger:
        products_paginator = paginator.page(1)
    except EmptyPage:
        products_paginator = paginator.page(paginator.num_pages)

    context = {
        'title': 'GeekShop - Продукты',
        'catalog': ProductCategory.objects.all(),
        'products': products_paginator,
        'category_num': category_id
    }
    return render(request, 'mainapp/products.html', context)


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(4, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
motor = GPIO.PWM(6,50)
steer = GPIO.PWM(4, 50)
theta = 0
rev = 0 #theta equivalent for motor
steer.start(theta)
motor.start(rev)
# Right Turn: 5
# Left Turn: 10
motor_cal = [15,7,4] #minimum voltage is 4.4V
#userInput = ''
def pwmMap(x): #maps input values of (inmin, inmax) for our understood range of angles to output values of(outmin, outmax) that are used for the ChangeDutyCycle
    outmax = float(5)
    outmin = float(10)
    inmax = float(30)
    inmin = float(-30)
    A = float(outmax-outmin)
    B = float(inmax - inmin)
    C = float(x - inmin)
    D = float(outmin)
    return float(float(A/B)*C+D)

print('Calibrating...')
for x in range(len(motor_cal)):
    motor.ChangeDutyCycle(motor_cal[x])
    time.sleep(5)

print('Finished Calibration')
time.sleep(1)

timeDelay = .5
startSpeed = 7
newSpeed = 0
speedInc = .5
try:
    while True:

        newSpeed = startSpeed+speedInc

        motor.ChangeDutyCycle(newSpeed)

        time.sleep(timeDelay)

        steer.ChangeDutyCycle(pwmMap(30))

        time.sleep(timeDelay)

        newSpeed = newSpeed + speedInc

        motor.ChangeDutyCycle(newSpeed)

        time.sleep(timeDelay)

        steer.ChangeDutyCycle(pwmMap(0))

        time.sleep(timeDelay)

        newSpeed = newSpeed + speedInc

        motor.ChangeDutyCycle(newSpeed)

        time.sleep(timeDelay)

        steer.ChangeDutyCycle(pwmMap(-30))

        time.sleep(timeDelay)
        break
except KeyboardInterrupt:
    motor.stop()
    steer.stop()
    GPIO.cleanup()


# _*_ coding: utf-8 _*_
# home.py
from app import db
from flask import request,render_template,flash,abort,url_for,redirect,session,Flask,g
from . import main
from flask_login import login_required, current_user
from ..forms import LoginForm
from ..backend.utils.token_create import TToken

@main.route('/')
def index():
    form = LoginForm()
    return render_template('index.html', form=form)

@main.route('/api/get_key', methods=['GET'])
@login_required
def get_key():
    username = session.get('username')
    if username:
        ttoken = TToken(username)
        token_bytes = ttoken.generate_auth_token(expiration=300)
        token_str = str(token_bytes, encoding='utf-8')
        print(token_str)
    return '
get_key
'

@main.route('/show')
@login_required
def show():
    form = LoginForm()
    return render_template('show.html', form=form)


from turtle import Turtle
from turtle import Screen
from tkinter import *
ALIGMENT = "center"
FONT = ("Courier", 24, "normal")
screen= Screen()
def close_the_screen():

    screen.bye()

def restart_game():

    screen.clearscreen()
    # screen.reset()
    # screen.update()

class Scoreboard(Turtle):

    def __init__(self):
        super().__init__()
        self.color("white")
        self.penup()
        self.goto(0, 265)
        self.hideturtle()

        self.score = 0
        self.update_scoreboard()

        self.exit_btn = self.exit_button()
        #self.restart_button()

    def update_scoreboard(self):
        self.write(f"Score : {self.score}", align=ALIGMENT,
                   font=FONT)

    def game_over(self):
        self.goto(0,0)
        self.write(f"Game Over", align=ALIGMENT,
                   font=FONT)

    def increase_score(self):

        self.score+=1
        self.clear()
        self.update_scoreboard()

    def exit_button(self):

        canvas = screen.getcanvas()
        exit_button = Button(canvas.master, text="Exit", command=close_the_screen)
        exit_button.pack()
        exit_button.place(x=100, y=10)

    def restart_button(self):

        canvas = screen.getcanvas()
        restart_button = Button(canvas.master, text="Restart", command=restart_game)
        restart_button.pack()
        restart_button.place(x=300, y=10)


import random

lists = ["gladys", "michael", "choja"]

ran = random.choice(lists)
print(ran)

user = input("Enter a letter\n")

for n in ran:
    if n == user:
        print("good")
    else:
        print("wrong")


from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
from concurrent.futures import ThreadPoolExecutor

class Server:
	def __init__(self, ip, puerto, name):
		self.s = self._start_sock(ip,puerto)
		self.file = open(name,"w")
		self.conexiones = []

	def run(self,ip,puerto):
		print("El Server esta activo")

		with ThreadPoolExecutor() as executor:
			while True:

				#Se acepta la conexion entrante
				clientsocket, direc = self.s.accept()

				print(f"Conneccion con {direc} ha sido establecida")

				#Se envia el mensaje
				clientsocket.send(bytes("Se ha conectado al servidor con el ip: "+str(ip)+" y puerto: "+str(puerto),"utf-8"))

				self.conexiones.append(clientsocket)

				executor.submit(self.envio_msg, clientsocket, direc)

	def envio_msg(self, clientsocket, direc):
		while True:
			data = clientsocket.recv(4096)

			IP = str(direc[0] + " - ").encode('utf-8')

			if not data:
				fail = str("No Recibido").encode('utf-8')

				clientsocket.send(IP + fail)

			else:
				self.file.write(direc[0] + " - " + data.decode('utf-8'))
				self.file.write("\n")
				self.file.flush()
				succ = str("Recibido").encode('utf-8')
				clientsocket.send(IP + succ)

	@staticmethod
	def _start_sock(ip,puerto):
		s = socket(AF_INET, SOCK_STREAM)
		s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
		s.bind((ip,puerto))
		s.listen()
		return s

if __name__ == '__main__':
	server = Server('',5000,"./log.txt")
	server.run('',5000)


'''ny10a'''

T = int(input())

for _ in range(1, T + 1):
    n = int(input())
    data = input().replace('T', '0').replace('H', '1')
    freq = [0] * 8
    for i in range(0, len(data) - 2):
        freq[int(data[i:i + 3], 2)] += 1
    print(n, end=' ')
    for i, point in enumerate(freq):
        if i != 7:
            print(point, end=' ')
            continue
        print(point)


from web_admin import api_settings, setup_logger, RestFulClient

class SystemUserClient:
    @classmethod
    def search_system_user(cls, headers=None, logger=None, username=None, email=None, user_id=None, status=None, paging=None, page_index=None):
        params = {}

        if username is not '' and username is not None:
            params['username'] = username
        if email is not '' and email is not None:
            params['email'] = email
        if user_id is not '' and user_id is not None:
            params['user_id'] = user_id
        if status is not None:
            if status == 'Active':
                params['is_suspended'] = False
            if status == 'Suspended':
                params['is_suspended'] = True
        if paging is not '' and paging is not None:
            params['paging'] = paging
        if page_index and page_index.isdigit():
            params['page_index'] = int(page_index)

        is_success, status_code, status_message, data = RestFulClient.post(api_settings.SEARCH_SYSTEM_USER,
                                                                           headers, logger, params)
        return is_success, status_code, status_message, data


import math
import os
import random
import re
import sys

# Complete the stockmax function below
def stockmax(prices):
    max = 0
    profit = 0
    for i in range(len(prices)-1, -1, -1):
    	if max < prices[i]:
    		max = prices[i]
    	profit += max - prices[i]
    return profit

if __name__ == "__main__":
    prices = [1, 2, 100]
    result = stockmax(prices)
    print(result)


#!/usr/bin/env python

"""
This test case verifies that annotation are correctly computed.
"""
import os
from json import load
from glob import glob
from typing import List

from inscriptis import get_annotated_text
from inscriptis.css_profiles import CSS_PROFILES
from inscriptis.model.config import ParserConfig

TESTCASE_PATTERN = os.path.join(os.path.dirname(__file__), 'html/*.json')

def assert_equal_ignoring_whitespace(reference: List[str],
                                     converted: List[str]) -> bool:
    for (ref_tag, ref_str), (conv_tag, conv_str) in zip(reference, converted):

        assert ref_tag == conv_tag
        assert ''.join(ref_str.split()) == ''.join(conv_str.split())

def test_html_annotations(filter_str=''):
    for annotation_file in glob(TESTCASE_PATTERN):
        if filter_str not in annotation_file:
            continue

        with open(annotation_file) as f:
            reference = load(f)

        with open(annotation_file.replace('.json', '.html')) as f:
            print(f.name)
            html = '{}'.format(f.read())

        for indentation_strategy in ('strict', 'relaxed'):
            result = get_annotated_text(html, ParserConfig(
                css=CSS_PROFILES[indentation_strategy],
                annotation_rules=reference['annotation_rules']))

            converted = [[a[2], result['text'][a[0]:a[1]]]
                        for a in result['label']]

            if reference['result'] != converted:
                print("Reference:")
                print(reference['result'])
                print("\nConverted (indentation strategy: {})".format(indentation_strategy))
                print(converted)

            if indentation_strategy == 'strict':
                assert reference['result'] == converted
            else:
                assert_equal_ignoring_whitespace(reference['result'],
                                                 converted)

if __name__ == '__main__':
    from sys import argv

    filter_str = argv[1] if len(argv) > 1 else ''
    test_html_annotations(filter_str)


class Solution:
    def destCity(self, paths: List[List[str]]) -> str:

        outgoing = set()
        incoming = set()

        # record outgoing and incoming cities
        for path in paths:
            outgoing.add(path[0])
            incoming.add(path[1])

        # find destination city with no outgoing path
        destination_city = incoming.difference(outgoing).pop()

        return destination_city


"""
 Universidad Adolfo Ibañez
 Facultad de Ingeniería y Ciencias
 TICS 585 - Reconocimiento de Patrones en imágenes

 Ejemplo que interpola una elipse en la region
 Autor:. Miguel Carrasco (02-08-2021)
 rev.1.0

 >> metodo implementado
 Fitzgibbon, A. W., Pilu, M and Fischer, R. B.: Direct least squares
 fitting of ellipses. In Proc. of the 13th International Conference on
 Pattern Recognition, pages 253–257, Vienna, September 1996
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
from math import sin, cos, pi
import numpy.matlib

def cambio_escala (binary, factor):
    factor_times = factor
    height = int(binary.shape[0] * factor_times)
    width  = int(binary.shape[1] * factor_times)
    dim    = (width, height)

    #cambio de escala
    binary= cv2.resize(binary, dim, interpolation=cv2.INTER_AREA)
    return binary

def fitzgibbon(x, y):
    # algoritmo de Fitzgibbon (1996) para la estimación de una elipse
    # a partir de las coordenadas de la frontera de una región

    n = x.shape[0]                                  #puntos de la frontera
    D = [x**2, x*y, y**2, x, y, np.ones((n,1))]     # matriz X
    D = np.hstack(D)
    S = np.matmul(D.T,D)                            # matriz dispersión
    C = np.zeros((6,6))
    C[0,2]=2; C[1,1]=-1; C[2,0]=2;                  # matriz restricción

    # Problema: S·a = lambda·C·a
    # --> Solución a traves de Eigenvalues
    E,V = np.linalg.eig(np.dot(np.linalg.inv(S),C))
    idx = np.argwhere(E>0 & ~np.isinf(E))
    params = V[:,idx]                                    # seleccionamos el valor propio
    return params

def parametros(x, y):
    # Funcion que busca los parametros parametricos
    # de la elipse segun las coordendas de los bordes.

    # parametros de fitzgibbon
    a,b,c,d,e,f = fitzgibbon(x, y)

    #estimados de angulos y factores de Fitzgibbon
    alpha = np.arctan2(b,a-c)/2

    ct = np.cos(alpha)
    st = np.sin(alpha)
    ap = a*ct*ct + b*ct*st + c*st*st
    cp = a*st*st - b*ct*st + c*ct*ct

    #% get translations
    T = np.array([[a,  b/2],
                  [b/2, c]])
    T = T.reshape(2,2)
    t = -1* np.matmul(np.linalg.inv(2*T),np.array([d, e]).reshape(2,1))
    cx = t[0]
    cy = t[1]

    #% factor de escala
    val = np.matmul(t.T,np.matmul(T,t))
    scale = 1 / (val- f)

    #% parametros
    r1 = 1/np.sqrt(scale*ap)
    r2 = 1/np.sqrt(scale*cp)
    v = np.array([r1, r2, cx, cy, alpha])
    return v

def draw_ellipse(binary, v, N=100):
# funcion auxiliar que permite dibujar una ellipse
    ae, be, x0, y0, alfa = v
    #angulo de rotación interno
    theta = np.linspace(0, 2*pi, N)

    #parametro estimado anteriormente
    R = np.array([[cos(alfa), -sin(alfa)],
                  [sin(alfa), cos(alfa)]])
    R = R.reshape(2,2)

    X = ae*np.cos(theta)
    Y = be*np.sin(theta)
    M = np.array([X,Y]).reshape(2,-1)
    CC=np.matmul(R,M) + \
    np.matlib.repmat(np.array([x0,y0]).reshape(2,1),1,N)

    plt.figure()
    plt.imshow(binary, cmap="gray")
    plt.scatter(CC[0,:], CC[1,:], marker='x', s=100, c='red')
    plt.plot(CC[0,:], CC[1,:])
    plt.show()

    return CC

#******************************
#      PROGRAMA PRINCIPAL     *
#******************************

#puntos de la elipse
N= 50

#imagen inventada
bw=[[0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0]]

#transformamos los datos a uint8
binary = np.array(bw, dtype='uint8')

#cambiamos la escala de la imagen
binary = cambio_escala (binary, 50)

#buscamos las coordenadas de los contornos
contours, hierarchy = cv2.findContours(binary,
                                        cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_NONE)

#generamos una matriz (fila/columna) con los contornos
contornos = np.vstack(contours[0])

#extraemos las coordenadas x,y
x , y = zip(*contornos)
x = np.array(x).reshape(-1,1)
y = np.array(y).reshape(-1,1)

#extraemos los parámetros de la elipse
# xc, yc, a, b, theta
v = parametros(x, y)

#extraemos las coodenadas de la elipse
CC = draw_ellipse(binary, v, N)

#EOF


 #creating an empty node
class BTreeNode:
    def __init__(self, leaf = False):
        self.leaf = leaf
        self.keys = []
        self.children = []

 #tree
class ABtree:
    def __init__(self, t):
        self.root = BTreeNode(True)
        self.height = t #it seems like it is the height of the tree

     #insert method
    def insert(self, k): #k for keys
        root = self.root
        if len(root.keys) == (2*self.height) - 1:
            temp = BTreeNode() #creating a temporary btree node
            self.root = temp
            temp.child.insert(0, root)
            self.split_child(temp, 0)


class Solution:

    def  result(self, n1, n2, op):
        if op == '+':
            return n1+n2
        elif op == '-':
            return n1-n2
        elif op == '*':
            return n1*n2
        else:
            return int(n1 / n2)

    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        operators = ('+', '-', '*', '/')
        r = None

        for item in tokens:
            if item not in operators:
                stack.append(item)
            else:
                val2 = stack.pop()
                val1 = stack.pop()

                r = self.result(int(val1), int(val2), item)
                #print(r)
                #print(val1, item, val2, r)
                stack.append(r)

        return int(stack.pop())


import os
from tqdm import tqdm

# For uniform facility cost, using unit cost for the file to avoid storing multiple copies of the same dataset. Can be changed once loaded in the testing code
f = 1
seeds = range(42,42+50) # [42,43,44,45,46]

cmds = []
def exec(cmd):
    cmds.append(cmd)

#synth
out_dir = "final_synth"
os.system("mkdir %s" % out_dir)
for n in [1000,5000,10000]:
    for d in ["mixture"]:
        for mix_cnt in [5,10,25,50,100]:
            for s in seeds:
                fname = f"{out_dir}/{d}-m{mix_cnt}-{n}-f{f}-s{s}"
                exec(f"python3 gen_synth.py {n} {fname} -d {d} -mixtures {mix_cnt} -f {f} -s {s} --save-plot")

for cmd in tqdm(cmds):
    os.system(cmd)


import pandas as pd
import random as rand
import numpy as np
from progress.bar import Bar
# Set seed
SEED = 500; rand.seed(SEED)
mu_noise = 0
sigma_noise = 100
n_samples = 200 # number of members of the population to generate

# Generate a dataset from the model with gaussian random inputs and the addition of noise drawn randomly from a gaussian distribution applied to the output
model = pd.read_csv('model.csv', index_col=0)
model_epistatic = pd.read_csv('model_epistatic.csv', index_col=0)

simulated_dataset = []
epistatic_dataset = []
# Create a matrix of inputs and generate outputs using the model on each output add an element of noise to the output
bar = Bar('Processing...', max=n_samples)
for i in range(n_samples):
    # Create sample
    split = rand.randrange(model.size)
    # rand var inputs 0 or 1
    inputs = np.ones(model.size)
    split = rand.randrange(model.size)
    inputs[:split] = 0
    rand.shuffle(inputs)
    # Calc output + noise to output
    output = np.dot(model.loc[0,:].to_numpy(), np.transpose(inputs))
    output = output + rand.gauss(mu_noise, sigma_noise)
    inputs = np.append(inputs,output)
    simulated_dataset.append(inputs)
    bar.next()
bar.finish()

columns = model.columns
columns = np.append(columns, 'phenotype')
simulated_dtfm = pd.DataFrame(data=simulated_dataset, columns=columns)
simulated_dtfm.to_csv('simulated_data.csv')

# Create a a matrix of inputs and generate outputs
bar = Bar('Processing...', max=n_samples)
for i in range(n_samples):
    temp_model = model_epistatic.copy()
    # Create sample
    split = rand.randrange(temp_model.shape[1])
    # rand var inputs 0 or 1
    inputs = np.ones(temp_model.shape[1])
    split = rand.randrange(temp_model.shape[1])
    inputs[:split] = 0
    rand.shuffle(inputs)

    temp_model.loc[3] = np.ones(temp_model.shape[1])
    # Calc many to one epistatic multiplications and add them to model
    #for j in range(model_epistatic.shape[1]):
        # Add multipliers together for each row and then save mul sum
     #   multiplier = 1
     #   current_snp = model_epistatic.columns[j]
     #   for k in range(model_epistatic.shape[1]):
     #       to_include = inputs[k]
     #       snp = model_epistatic.iloc[2,k]
     #       if snp == current_snp and to_include == 1:
     #           multiplier = multiplier + float(model_epistatic.iloc[1,k])
     #       bar.next()
     #   model_epistatic.iloc[3,j] = multiplier
     #   bar.next()
    # bar.next()
    for j in range(model_epistatic.shape[1]):
        if inputs[j] == 1:
            snp = temp_model.iloc[2,j]
            mul = temp_model.iloc[1,j]
            temp_model.loc[3,snp] = temp_model.loc[3,snp] + float(mul)

    # replace original with orig * multiplier
    originals = temp_model.iloc[0,:].to_numpy().astype(np.float)
    multipliers = temp_model.iloc[3,:].to_numpy().astype(np.float)

    temp_model.iloc[0,:] = np.multiply(originals, np.transpose(multipliers))

    # Calc output + noise to output
    effects_arr = temp_model.iloc[0,:].to_numpy().astype(np.float)
    output = np.dot(effects_arr, np.transpose(inputs))

    output = output + rand.gauss(mu_noise, sigma_noise)
    inputs = np.append(inputs,output)
    epistatic_dataset.append(inputs)
    bar.next()
bar.finish()

columns = model.columns
columns = np.append(columns, 'phenotype')
epistatic_dtfm = pd.DataFrame(data=epistatic_dataset, columns=columns)
epistatic_dtfm.to_csv('simulated_epistatic_data.csv')


import numpy as np
import rllab.misc.logger as logger
from rllab.misc import special2 as special

class SimpleReplayPool(object):
    def __init__(
            self, max_pool_size, observation_dim, action_dim,
            replacement_policy='stochastic', replacement_prob=1.0,
            max_skip_episode=10, env=None):
        self._observation_dim = observation_dim
        self._action_dim = action_dim
        self._max_pool_size = max_pool_size
        self._replacement_policy = replacement_policy
        self._replacement_prob = replacement_prob
        self._max_skip_episode = max_skip_episode
        self._observations = np.zeros(
            (max_pool_size, observation_dim),
            )
        if env is not None and env.action_space.is_discrete:
            self._actions = np.zeros((max_pool_size,),dtype=np.int64)
            self._n = env.action_space.n
            self._is_action_discrete = True
        else:
            self._actions = np.zeros(
                (max_pool_size, action_dim),
            )
            self._is_action_discrete = False
        self._rewards = np.zeros(max_pool_size)
        self._terminals = np.zeros(max_pool_size, dtype='uint8')
        self._initials = np.zeros(max_pool_size, dtype='uint8')
        self._observations.fill(0) # pre-allocate
        self._actions.fill(0) # pre-allocate
        self._terminals.fill(0) # pre-allocate
        self._initials.fill(0) # pre-allocate
        self._rewards.fill(0) # pre-allocate
        self._bottom = 0
        self._top = 0
        self._size = 0

    def add_sample(self, observation, action, reward, terminal, initial):
        self.check_replacement()
        self._observations[self._top] = observation
        if self._is_action_discrete and not isinstance(action,
                (int, np.int64)):
            action = special.from_onehot(action)
        self._actions[self._top] = action
        self._rewards[self._top] = reward
        self._terminals[self._top] = terminal
        self._initials[self._top] = initial
        self.advance()

    def check_replacement(self):
        if self._replacement_prob < 1.0:
            if self._size < self._max_pool_size or \
                not self._initials[self._top]: return
            self.advance_until_terminate()

    def get_skip_flag(self):
        if self._replacement_policy == 'full': skip = False
        elif self._replacement_policy == 'stochastic':
            skip = np.random.uniform() > self._replacement_prob
        else: raise NotImplementedError
        return skip

    def advance_until_terminate(self):
        skip = self.get_skip_flag()
        n_skips = 0
        old_top = self._top
        new_top = (old_top + 1) % self._max_pool_size
        while skip and old_top != new_top and n_skips < self._max_skip_episode:
            n_skips += 1
            self.advance()
            while not self._initials[self._top]:
                self.advance()
            skip = self.get_skip_flag()
            new_top = self._top
        logger.log("add_sample, skipped %d episodes, top=%d->%d"%(
            n_skips, old_top, new_top))

    def advance(self):
        self._top = (self._top + 1) % self._max_pool_size
        if self._size >= self._max_pool_size:
            self._bottom = (self._bottom + 1) % self._max_pool_size
        else:
            self._size += 1

    def last_batch(self, batch_size):
        assert self._size >= batch_size
        if self._top >= batch_size:
            observations=self._observations[self._top-batch_size:self._top]
        else:
            assert self._size == self._max_pool_size
            obs1 = self._observations[self._max_pool_size+
                    self._top-batch_size:]
            obs2 = self._observations[:self._top]
            observations = np.concatenate((obs1, obs2), axis=0)
        return dict(
            observations = observations,
        )

    def random_batch(self, batch_size):
        assert self._size >= batch_size
        indices = np.zeros(batch_size, dtype='uint64')
        transition_indices = np.zeros(batch_size, dtype='uint64')
        count = 0
        while count < batch_size:
            index = np.random.randint(self._bottom, self._bottom + self._size) % self._max_pool_size
            # make sure that the transition is valid: if we are at the end of the pool, we need to discard
            # this sample
            if index == self._size - 1 and self._size <= self._max_pool_size:
                continue
            # if self._terminals[index]:
            #     continue
            transition_index = (index + 1) % self._max_pool_size
            # make sure that the transition is valid: discard the transition if it crosses horizon-triggered resets
            if not self._terminals[index] and self._initials[transition_index]:
                continue
            indices[count] = index
            transition_indices[count] = transition_index
            count += 1
        actions = self._actions[indices]
        if self._is_action_discrete:
            actions = special.to_onehot_n(actions, self._n)
        return dict(
            observations=self._observations[indices],
            actions=actions,
            rewards=self._rewards[indices],
            terminals=self._terminals[indices],
            initials=self._initials[indices],
            next_observations=self._observations[transition_indices]
        )

    @property
    def size(self):
        return self._size


from sys import stdin
def idioma(palabra):
    if palabra=="HELLO":
        return "ENGLISH"
    elif palabra=="HOLA":
        return "SPANISH"
    elif palabra=="HALLO":
        return "GERMAN"
    elif palabra=="BONJOUR":
        return "FRENCH"
    elif palabra=="CIAO":
        return "ITALIAN"
    elif palabra=="ZDRAVSTVUJTE":
        return "RUSSIAN"
    else:
        return "UNKNOWN"
def main():
    renglon=0
    x=str(stdin.readline().strip())
    while x !="#":
        print("Case",str(renglon+1)+":",idioma(x))
        renglon+=1
        x=str(stdin.readline().strip())
main()


#!/usr/bin/python
# -*- coding: utf-8 -*-
# www.pythondiario.com

from Tkinter import *
from tkMessageBox import *
import sqlite3

ventana = Tk()
ventana.title ("------- Login Python Diario -------")
ventana.geometry ("350x150+500+250")
Label(ventana, text = "Usuario:").pack()
caja1 = Entry(ventana)
caja1.pack()

Label(ventana, text = "Contraseña:").pack()
caja2 = Entry(ventana, show = "*")
caja2.pack()

def login():
	# Connect to database
	db = sqlite3.connect('/home/diego123/Escritorio/login.db')
	c = db.cursor()

	usuario = caja1.get()
	contr = caja2.get()

	c.execute('SELECT * FROM usuarios WHERE usuario = ? AND pass = ?', (usuario, contr))

	if c.fetchall():
		showinfo(title = "Login correcto", message = "Usuario y contraseña correctos")
	else:
		showerror(title = "Login incorrecto", message = "Usuario o contraseña incorrecta")

	c.close()

Button (text = "Login", command = login).pack()

ventana.mainloop()


import subprocess
import numpy as np
import itertools
from feature_extraction import get_group_embedding, get_head_embedding, \
    parse_gram_attribute, get_distance_features, fillin_global_variables
from nn_model import Model, PROB_THRESHOLD

def run_parser(input_file, output_file):
    bash_command = 'cd /Users/annakupriyanova/Desktop/parser/CoreNLP/target/classes'

    params = {
        'tagger': '/Users/annakupriyanova/Desktop/parser/russian-ud-pos.tagger',
        'taggerMF': '/Users/annakupriyanova/Desktop/parser/russian-ud-mf.tagger',
        'parser': '/Users/annakupriyanova/Desktop/parser/nndep.rus.modelMFWiki100HS400_80.txt',
        'mf': '',
        'pLemmaDict': '/Users/annakupriyanova/Desktop/parser/dict.tsv',
        'pText': input_file,
        'pResults': output_file
    }

    parser = 'java -Xmx8g edu.stanford.nlp.international.russian.process.Launcher '
    for k, val in params.items():
        parser += '-{} {} '.format(k, val)

    bash_command += '\n' + parser

    process = subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
    output = process.communicate()[0].strip()

    print("Parsed file is ready")

def annotate(text):
    input_file = '/Users/annakupriyanova/PycharmProjects/CoreferenceResolution/input_data/input.txt'
    output_file = '/Users/annakupriyanova/PycharmProjects/CoreferenceResolution/input_data/output.conll'

    with open('./input_data/input.txt', 'w') as fout:
        fout.write(text)

    run_parser(input_file, output_file)

    return output_file

def get_conll(filepath):
    conll = [[]]
    i = 0

    with open(filepath, 'r') as fin:
        for line in fin:
            if line == '\n':
                conll.append([])
                i += 1
            else:
                row = line.split('\t')
                conll[i].append(row)

    return conll

def get_mentions(conll, indices):
    '''
    :param conll:
    :param indices:
    :return: list of mentions [[[group_lemmas], head_lemma, gram_str], ...]
    '''
    mentions = []
    word_forms = []

    for (sentNum, start, end, head) in indices:
        i = start
        group = []
        wf = []
        head_lemma = ''
        gram = ''

        while i <= end:
            token = conll[sentNum][i]

            lem = token[2]
            group.append(lem)
            wf.append(token[1])

            if i == head:
                head_lemma = lem
                gram = get_gram_string(token[4], token[5])

            i += 1

        mentions.append([group, head_lemma, gram])
        word_forms.append(' '.join(wf))

    return mentions, word_forms

def get_gram_string(pos, features):

    def morp_dict(features):
        d = {}

        for f in features:
            pair = f.split('=')
            d[pair[0]] = pair[1]

        return d

    gram = '------'
    morpo_features = features.split('|')

    if pos == 'NOUN':
        gram = 'N-'
    elif pos == 'ADJ':
        gram = 'A--'
    elif pos == 'PRON':
        gram = 'P--'

    morpho_dict = morp_dict(morpo_features)
    if 'Case' in morpho_dict:
        case = morpho_dict['Case'][0].lower()
    else:
        case = '-'

    if 'Gender' in morpho_dict:
        gender = morpho_dict['Gender'][0].lower()
    else:
        gender = '-'

    if 'Number' in morpho_dict:
        number = morpho_dict['Number'][0].lower()
    else:
        number = '-'

    gram += gender + number + case + '-'

    return gram

def prepare_data(mentions):
    fillin_global_variables()

    mentions_data = []
    data = []
    pairs = []

    for m in mentions:
        grp_embed = get_group_embedding(m[0])
        head_embed = get_head_embedding(m[1])
        morph = parse_gram_attribute(m[2])
        vec = np.r_[grp_embed, head_embed, morph]
        mentions_data.append(vec)

    indices = list(range(len(mentions)))

    for (i, j) in itertools.combinations(indices, 2):
        str_match = get_str_match_features(mentions[i][0], mentions[j][0], mentions[i][1], mentions[j][1])
        distance = get_distance_features(i, j)

        data.append(np.r_[mentions_data[i], mentions_data[j], str_match, distance])
        pairs.append((i, j))

    return np.array(data), pairs

def get_str_match_features(lem_list1, lem_list2, head1, head2):
    exact_match = int(lem_list1 == lem_list2)
    head_match = int(head1 == head2)
    part_match = int(not not list(set(lem_list1) & set(lem_list2)))

    return np.array([exact_match, head_match, part_match])

def predict(dataset):
    model = Model(mode='test', filepath='./models/CRModel_final.h5')
    print('Model is downloaded.')

    scores = model.predict(dataset)

    pred_labels = (scores > PROB_THRESHOLD)

    return scores, pred_labels

def print_results(pairs, labels, predictions, word_forms):
    for i, (m1, m2) in enumerate(pairs):
        print("{} {} {}".format((word_forms[m1], word_forms[m2]), labels[i], predictions[i]))

def CRPipeline(text, indices):
    out_file = annotate(text)

    conll = get_conll(out_file)

    mentions, word_forms = get_mentions(conll, indices)

    data, pairs = prepare_data(mentions)

    scores, pred_labels = predict(data)

    print_results(pairs, pred_labels, scores, word_forms)

def main():
    text = '''
    Привет.
    Меня зовут Аня.
    И я люблю пиццу.
    '''

    indices = [(1, 0, 0, 0), (1, 2, 2, 2), (2, 1, 1, 1), (2, 3, 3, 3)]

    # out_file = annotate(text)

    conll = get_conll('/Users/annakupriyanova/PycharmProjects/CoreferenceResolution/input_data/output.conll')

    mentions, word_forms = get_mentions(conll, indices)

    data, pairs = prepare_data(mentions)

    scores, pred_labels = predict(data)

    print_results(pairs, pred_labels, scores, word_forms)

if __name__ == '__main__':
    main()


def fib(n, k):
    s = [0, 1]
    if n == 2:
        return (1)
    else:
        for i in range(2, n + 1):
            s.append(s[i - 1] + k * s[i - 2])
            print(s)
        return (s[-1])

n, k = map(int, input().split())
print(fib(n, k))


from uuid import uuid4
from datetime import datetime

from unittest import TestCase, mock

from app.domain.enums import CountryEnum, UserEnum
from app.domain.service import UserService, StoreService
from app.domain.models.info import Address, ContactInfo, ChangeLog
from app.domain.models.stores import Store
from app.domain.models.users import User, UpdateUser
from app.infrastructure.repository import UserRepository, ChangeLogRepository, StoreRepository

class UserServiceTest(TestCase):

    @mock.patch('app.infrastructure.repository.ChangeLogRepository')
    @mock.patch('app.infrastructure.repository.UserRepository')
    def setUp(self, user_repository, change_log_repository):
        self._user_repository = user_repository
        self._change_log_repository = change_log_repository

        self._user = User(
            id=str(uuid4()),
            first_name=str(uuid4()),
            last_name=str(uuid4()),
            username=str(uuid4()),
            user_type=UserEnum['EMPLOYEE'],
            address=Address(
                address=str(uuid4()),
                city=str(uuid4()),
                province=str(uuid4()),
                country=CountryEnum['CAN'],
                postal_code=str(uuid4())
            ),
            contact_info=ContactInfo(
                emails=[],
                phone_numbers=[]
            ),
            created_date=datetime.now()
        )

        self._user_repository.find_by_id.return_value = self._user
        self._user_service = UserService(self._user_repository, self._change_log_repository)

    def test_update_user(self):
        new_user_info = UpdateUser(
            first_name=str(uuid4()),
            last_name=str(uuid4()),
            username=str(uuid4()),
            user_type=UserEnum['OWNER'],
            address=Address(
                address=str(uuid4()),
                city=str(uuid4()),
                province=str(uuid4()),
                country=CountryEnum['CAN'],
                postal_code=str(uuid4())
            ),
            contact_info=ContactInfo(
                emails=['new@email.ca'],
                phone_numbers=['0803432342']
            )
        )

        updated_user_dict = self._user_service.update_user(self._user.get_id(), new_user_info).to_dict()

        self._user_repository.save_user.assert_called_once_with(
            User(id=self._user.get_id(), created_date=self._user.get_created_date(), **new_user_info.to_dict(is_nested=False))
        )
        self._change_log_repository.save_user_change_log.assert_called_once_with(
            ChangeLog(id=self._user.get_id(), message=ChangeLog.USER_INFO_CHANGED, created_date=mock.ANY)
        )

        for key, value in new_user_info.to_dict().items():
            self.assertEqual(updated_user_dict[key], value)

class StoreServiceTest(TestCase):

    @mock.patch('app.infrastructure.repository.ChangeLogRepository')
    @mock.patch('app.infrastructure.repository.StoreRepository')
    def setUp(self, store_repository: StoreRepository, change_log_repository: ChangeLogRepository):
        self._store_repository = store_repository
        self._change_log_repository = change_log_repository

        self._store = Store(
            id=str(uuid4),
            name=str(uuid4),
            store_type=str(uuid4),
            phone_number=str(uuid4),
            address=Address(
                address=str(uuid4()),
                city=str(uuid4()),
                province=str(uuid4()),
                country=CountryEnum['CAN'],
                postal_code=str(uuid4())
            ),
            employees=[],
            created_date= datetime.now()
        )

        self._store_service = StoreService(self._store_repository, self._change_log_repository)

    def test_create_store(self):
        self._store_repository.save_store.return_value = self._store

        created_store = self._store_service.create_store(self._store)
        self._store_repository.save_store.assert_called_once_with(
            self._store
        )
        self._change_log_repository.save_store_change_log.assert_called_once_with(
            ChangeLog(self._store.get_id(), ChangeLog.STORE_CREATED, self._store.get_created_date())
        )
        self.assertEqual(created_store, self._store)


from utils import login_required
import flask.views
from flask import jsonify,json
from flask_weasyprint import HTML, render_pdf
from models.bdCreator import Session
from models.solicitudCambioModelo import SolicitudCambio
from models.itemModelo import Item, Relacion
from models.tipoItemModelo import TipoItem
from models.usuarioModelo import Usuario
from models.faseModelo import Fase
from models.proyectoModelo import Proyecto, comiteProyectoTabla
from models.faseModelo import Fase
from flask.templating import render_template
from models.atributosModelo import Atributos
from models.historialModelo import HistorialItem;
from flask.helpers import make_response
#from flask_weasyprint import HTML, render_pdf
from models.instanciaAtributos import InstanciaCadena, InstanciaEntero, InstanciaFecha, InstanciaNumerico, InstanciaTipoItem
#from flask_weasyprint import HTML, render_pdf
sesion=Session()

class Respuesta():
    """
    Clase utilizada cuando se hace una peticion de listado de
    roles de proyecto al servidor. Se obtienen de la bd las filas a
    devolver dentro de la lista y se convierte a un formato
    json para que pueda ser interpretado por el navegador
    """
    totalPages=1
    currPage=1
    totalRecords=1
    rows=1

    def __init__(self, totalPages,currPage,totalRecords,rows):
        self.totalPages=totalPages
        self.currPage=currPage
        self.totalRecords=totalRecords
        self.rows=rows

class InformeHistorialItems(flask.views.MethodView):
    """
    Clase que es utilizada para crear el Informe de Costo Economico y Complejidad

    """
    def comaPorPunto(self, string):
        r=''
        for i in string:
            if i==',':
                r=r+'.'
            elif i=='.':
                r=r+','
            else:
                r=r+i

        return r

    def recorrerHijos(self, i, dicTotal, dicDelItem):
        #se obtienen todos los hijos
        dicTotal[i.idItem]=i;
        dicDelItem[i.idItem]=i;
        s=sesion.query(Item).filter(Relacion.padre_id==i.idItem).join(Relacion, Relacion.hijo_id==Item.idItem).all()

        if (s!=None): #si es que es el ultimo elemento de la LB
            for son in s:
                self.recorrerHijos(son, dicTotal, dicDelItem)

        return

    def armarHtml(self, nombreItem, sc, scpj):
        r='
'
        r=r+''+nombreItem + ': 	'+'CE:'+str(sc)+ '	'+'CC:'+str(scpj) +     '	'
        r=r+'
'

        return r;
    def format_fecha(self, fecha):
        print("es" + fecha)

        r=(fecha[8:10]+"/"+fecha[5:7]+"/"+fecha[0:4])
        return r
    @login_required
    def get(self):

        """
        Recibe la peticion de listar el informe, segun los parametros que incluya la peticion.
        @type idSC: string
        @param idSC : parametro que indica el id solicitud de cambio.
        """
        #se obtiene los datos de post del server

        parametros=['idProyecto', 'idItem'];
        for par in parametros:
            if par not in flask.request.args:
                return make_response('No se enviaron todos los parametros')
        idP=flask.request.args.get('idProyecto', '')
        idI=flask.request.args.get('idItem', '')

        #itemsEnSC=sesion.query(Item).join(Fase).join(Proyecto).filter(Proyecto.idProyecto==int(idP)).all()
        #fasesProyecto=sesion.query(Fase).filter(Fase.idProyecto==int(idP)).all()

        #auxItem=aux;
        pr=sesion.query(Proyecto).filter(Proyecto.idProyecto==idP).first()
        pl=sesion.query(Usuario).filter(Usuario.id==pr.projectLeaderId).first()
        '''usuariosComite=sesion.query(Usuario).join(comiteProyectoTabla)\
                                    .filter(Proyecto.idProyecto==idP)\
                                    .filter(Usuario.activo=="true")'''
        #sesion.close()

        item=sesion.query(Item).filter(Item.idItem==idI).first()
        faseDelItem=sesion.query(Fase).join(Item).filter(Item.idFase==item.idFase).first()
        itemsEnFase=sesion.query(Item.idItem).filter(Item.idFase==item.idFase).order_by('id asc').all();
        cont=0
        for a in itemsEnFase:
            cont=cont+1
            if(a.idItem==item.idItem):
                break;
        h=sesion.query(HistorialItem).filter(HistorialItem.idItemFK==int(item.idItem)).order_by('id_item_fk asc').all()
        ti=None;iti=None;listaAtributo=None;
        if item.tipoItem_id is not None:
            listaAtributo=sesion.query(Atributos).filter(Atributos.tipoItemId==int(item.tipoItem_id)).all()
            ti=sesion.query(TipoItem).filter(TipoItem.idTipoItem==item.tipoItem_id).first()
            iti=sesion.query(InstanciaTipoItem)\
                                    .filter(InstanciaTipoItem.idItem==int(item.idItem)).all();
        html=''
        html=html+''
        html=html+'
Informe Historial de Item
'

        html=html+'
Proyecto: ' + pr.nombreProyecto + '
'
        html=html+'
Lider de Proyecto: ' + pl.apellidos + ',' + pl.nombres + '[' + pl.username + ']' + '
';
        html=html+'
Presupuesto: Gs. ' + self.comaPorPunto("{:,}".format( pr.presupuesto))+ '
'
        '''html=html+'
Miembros del Comite:

 '
        for u in usuariosComite:
            html=html+' - '+ u.apellidos + ', '+u.nombres +' ['+ u.username+ ']

'
        html=html[0:len(html)-9] '''
        #html=html+'
'
        html=html+'
Observacion: '+pr.observacion + '
'
        html=html+'
Historial del Item: ' + item.nombreItem + '
'
        html=html+'
Codigo : ' + faseDelItem.tag +'.'+ str(cont) + '_' + item.nombreItem+'
'
        html=html+'
Fase : ' + faseDelItem.nombreFase +'
'
        if(ti is None):
            html=html+'
Tipo de Item : ' + 'Ninguno' +'
'
        else:
            html=html+'
Tipo de Item : ' + ti.nombreTipoItem +'
'
        html=html+'
Version ' + str(item.version ) + ' (Actual)
'

        html=html+'
'
        html= html+'
- Nombre item: ' + item.nombreItem + '	'
        html= html+'- Prioridad: ' + str(item.prioridad) + '
'
        #html= html+'
- Version: ' + str(i.version) + '	'
        html= html+'- Costo: Gs. ' + self.comaPorPunto("{:,}".format( item.costo))+ '
'
        html= html+'
- Complejidad: ' + str(item.complejidad) + '	'
        html= html+'- Estado: ' + item.estado + '
	'
        html= html+'
- Descripcion: ' + item.descripcion + '
'
        #html=html+'
A continuacion se cita el historial del Itemlos items clasificados por sus fases correspondientes
'

        #html=html+'
'
        #html=html+'
Fases
'
        html= html+'
  ' + '
'
        if(listaAtributo is not None and iti is not None):
            for s in iti:
                idt=s.tipoPrimario;
                print('lalalal'+s.tipoPrimario)
                if(idt=='Texto'):
                    aux=sesion.query(InstanciaCadena).filter(InstanciaCadena.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                     .filter(InstanciaCadena.version==item.version).first()
                    html=html+'
- '+ s.nombreCampo + ': ' + aux.cadena + '
'

                elif(idt=='Numerico'):
                    aux=sesion.query(InstanciaNumerico).filter(InstanciaNumerico.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                     .filter(InstanciaNumerico.version==item.version).first()
                    html=html+'
- '+ s.nombreCampo + ': ' + str(aux.numerico) + '
'
                elif(idt=='Entero'):
                    aux=sesion.query(InstanciaEntero).filter(InstanciaEntero.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                     .filter(InstanciaEntero.version==item.version).first()
                    html=html+'
- '+ s.nombreCampo + ': ' + str(aux.entero) + '
'
                elif(idt=='Fecha'):
                    aux=sesion.query(InstanciaFecha).filter(InstanciaFecha.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                    .filter(InstanciaFecha.version==item.version).first()
                    fecha_formateada=self.format_fecha(str(aux.fecha))
                    html=html+'
- '+ s.nombreCampo + ': ' + fecha_formateada + '
'

        html= html+'
Relacionado con (Padres):' + '
'
        listaRel=sesion.query(Relacion).filter(Relacion.hijo_id==item.idItem).all()
        if(listaRel is not None):
            for f in listaRel:
                i=sesion.query(Item).filter(Item.idItem==f.padre_id).first()
                if(i is None):
                        return make_response ('t,No existe item con ese id')
                #p=p+json.dumps({"idRelacion": f.idRelacion,"relacion":r_descripcion , "idItemRelacionado":id_item_relacionado,  \
                #                "nombreItem": i.tag , "estado": i.estado}, separators=(',',':'))+",";
                html=html+'
__ '+i.tag+' '
            html=html[0:len(html)-1]
        else:
            html=html+ '
'+'(ninguno)'
        html=html+'
'
        html=html + '
'

        #html=html+'
Versiones
'

        if h is not None:
            for i in h:
                #indice=indice+1;
                html= html+'
Version '+ str(i.version)+'
'
                html=html+'
'
                html= html+'
- Nombre item: ' + i.nombreItem + '	'
                html= html+'- Prioridad: ' + str(i.prioridad) + '
'
                #html= html+'
- Version: ' + str(i.version) + '	'
                html= html+'- Costo: Gs. ' + self.comaPorPunto("{:,}".format( i.costo))+ '
'
                html= html+'
- Complejidad: ' + str(i.complejidad) + '	'
                html= html+'- Estado: ' + i.estado + '
	'
                html= html+'
- Descripcion: ' + i.descripcion + '
'
                #html= html+'
Datos TipoItem : ' + ti.nombreTipoItem + '
'
                html= html+'
  ' + '
'
                if(listaAtributo is not None and iti is not None):
                    for s in iti:
                        idt=s.tipoPrimario;
                        print('lalalal'+s.tipoPrimario)
                        if(idt=='Texto'):
                            aux=sesion.query(InstanciaCadena).filter(InstanciaCadena.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                             .filter(InstanciaCadena.version==i.version).first()
                            html=html+'
- '+ s.nombreCampo + ': ' + aux.cadena + '
'

                        elif(idt=='Numerico'):
                            aux=sesion.query(InstanciaNumerico).filter(InstanciaNumerico.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                             .filter(InstanciaNumerico.version==i.version).first()
                            html=html+'
- '+ s.nombreCampo + ': ' + str(aux.numerico) + '
'
                        elif(idt=='Entero'):
                            aux=sesion.query(InstanciaEntero).filter(InstanciaEntero.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                             .filter(InstanciaEntero.version==i.version).first()
                            html=html+'
- '+ s.nombreCampo + ': ' + str(aux.entero) + '
'
                        elif(idt=='Fecha'):
                            aux=sesion.query(InstanciaFecha).filter(InstanciaFecha.instanciaTipoItem_id==int(s.idInstanciaTipoItem))\
                                                            .filter(InstanciaFecha.version==i.version).first()
                            fecha_formateada=self.format_fecha(str(aux.fecha))
                            html=html+'
- '+ s.nombreCampo + ': ' + fecha_formateada + '
'

                from models.historialModelo import HistorialRelacion
                listaRel=sesion.query(HistorialRelacion).filter(HistorialRelacion.versionHijo==int(i.version)).filter(HistorialRelacion.hijo_id==item.idItem)\
                                                        .all()
                html=html+'
- Relacionado con (Padres):
 '
                entro=False
                for f in listaRel:
                    entro=True
                    i=sesion.query(Item).filter(Item.idItem==f.padre_id).first()
                    if(i is None):
                            return make_response ('t,No existe item con ese id')
                    #p=p+json.dumps({"idRelacion": f.idRelacion,"relacion":r_descripcion , "idItemRelacionado":id_item_relacionado,  \
                    #                "nombreItem": i.tag , "estado": i.estado}, separators=(',',':'))+",";
                    html=html+'
__ '+i.tag+' '

                if entro is False:
                    html=html+ '
'+'(Ninguno)'
                html=html+'
'
                html=html + '
'
        else:
            html=html + '(El item no cuenta con version)

'

        #html=html+'
Costos detallado

'
        #html=html+'
Item	Economico (Gs)	Complejidad
'

        html=html+'
*Observacion: La escala de prioridad es de 0-19, donde 0 significa la menor prioridad
\
          \

La escala de complejidad es de 0-19, donde 0 significa que no tiene complejidad
 '
        #return render_pdf(HTML(string=html))
        sesion.close()
        return render_pdf(HTML(string=html))
        #return html
        #return respuesta

    @login_required
    def post(self):
        return "{\"totalpages\": \"1\", \"currpage\": \"1\",\"totalrecords\": \"1\",\"invdata\" : [{\"NombreUsuario\": \"lafddadsdsalaal\",\"Nombre\": \"lalal\", \"idUsuario\": \"1000\",\"email\": \"lalalalal\", \"Apellido\": \"prerez\"}]}"


import json
import os
import pathlib
from typing import List, Optional

ASSETS_DIR: str = os.path.join(pathlib.Path(__file__).parent.parent.absolute(), "assets")

def load_schema(name: str, version: str) -> dict:
    fpath: str = os.path.join(ASSETS_DIR, "schemas", name, version, "schema.json")
    if not os.path.exists(fpath):
        raise FileNotFoundError(fpath)
    with open(fpath, "rt") as fin:
        return json.load(fin)

def get_schema_icon_filepath(name: str, version: str) -> Optional[str]:
    fpath: str = os.path.join(ASSETS_DIR, "schemas", name, version, "icon.png")
    return fpath if os.path.exists(fpath) else None

def get_schema_html_filepath(name: str, version: str, component: str) -> Optional[str]:
    fpath: str = os.path.join(ASSETS_DIR, "schemas", name, version, component)
    return fpath if os.path.exists(fpath) else None

def load_dtproject(name: str, version: str) -> List:
    fpath: str = os.path.join(ASSETS_DIR, "dtprojects", name, version, ".dtproject")
    if not os.path.exists(fpath):
        raise FileNotFoundError(fpath)
    with open(fpath, "rt") as metastream:
        lines: List[str] = metastream.readlines()
        return lines

def load_template(name: str, version: str) -> dict:
    fpath: str = os.path.join(ASSETS_DIR, "templates", name, version, "template.json")
    if not os.path.exists(fpath):
        raise FileNotFoundError(fpath)
    with open(fpath, "rt") as fin:
        return json.load(fin)

__all__ = [
    "ASSETS_DIR",
    "load_schema",
    "get_schema_icon_filepath",
    "get_schema_html_filepath",
    "load_dtproject",
    "load_template",
]


import code

import torch
import numpy as np
from models.q_networks import Q_Network, Dueling_Q_Network, UnitQ_Network
from trainers.base_trainer import BaseTrainer

class DQN(BaseTrainer):

    def init_model(self):
        self.print("init DQN model")
        if 'dueling' in self.args and self.args.dueling:
            ModelClass = Dueling_Q_Network
        else:
            ModelClass = Q_Network
        self.q_learner = ModelClass(self.expl_env.action_space()).to(self.args.device)
        self.q_target = ModelClass(self.expl_env.action_space()).to(self.args.device)
        self.optimizer = torch.optim.Adam(
            params=self.q_learner.parameters(),
            lr=self.args.lr)
        self.q_target.load_state_dict(self.q_learner.state_dict())

    def sample_batch(self, batch_size):
        return self.replay_buffer.sample(batch_size)

    def expl_action(self, obs) -> int:
        if np.random.rand() < self.epsilon():
            return np.random.randint(self.expl_env.action_space())
        with torch.no_grad():
            obs = torch.unsqueeze(obs, dim=0)
            q = torch.squeeze(self.q_learner(obs), dim=0)
            a = torch.argmax(q).item()
            return a

    def eval_action(self, obs) -> int:
        with torch.no_grad():
            obs = torch.unsqueeze(obs, dim=0)
            q = torch.squeeze(self.q_learner(obs), dim=0)
            a = torch.argmax(q).item()
            return a

    def V(self, s_):
        with torch.no_grad():
            max_a_qsa, a_indicies = self.q_target(s_).max(dim=1)
            max_a_qsa = torch.unsqueeze(max_a_qsa, dim=1)
            return max_a_qsa

    def before_learn_on_batch(self):
        # PER (prioritized experience replay) anneal
        self.replay_buffer.anneal_priority_weight()

    def learn_on_batch(self, treeidx, s, a, r, s_, d, w):

        # learn on batch
        a = torch.nn.functional.one_hot(a, num_classes=self.expl_env.action_space())
        r = torch.unsqueeze(r, dim=1)
        w = torch.unsqueeze(w, dim=1)
        self.optimizer.zero_grad()
        # compute loss
        target = r + (1 - d) * self.args.discount * self.V(s_)
        pred = torch.unsqueeze(torch.sum(self.q_learner(s) * a, dim=1), dim=1)
        loss = (pred - target) ** 2
        # PER Importance Sampling
        IS_mseloss = torch.mean(loss * w)
        IS_mseloss.backward()
        self.optimizer.step()
        # PER update
        abs_loss = torch.sqrt(loss).detach().view(-1).cpu().numpy()
        self.replay_buffer.update_priorities(treeidx, abs_loss)

        # logging
        with torch.no_grad():
            self.exp_path['batch_stats[t|num_done|pred_mean|q_loss|target_mean]'].csv_writerow(
                [self.t, d.sum().item(), pred.mean().item(), IS_mseloss.item(), target.mean().item()])

    def after_learn_on_batch(self):
        # target network update
        if self.t % self.args.target_update_freq == 0:
            self.q_target.load_state_dict(self.q_learner.state_dict())

    def state_value_pred(self, s):
        with torch.no_grad():
            s = s.view(1, 4, 84, 84)
            return self.V(s).mean().item()

class UnitDQN(DQN):

    def init_model(self):
        self.print("init UnitDQN model")
        ModelClass = UnitQ_Network
        self.q_learner = ModelClass(self.expl_env.action_space()).to(self.args.device)
        self.q_target = ModelClass(self.expl_env.action_space()).to(self.args.device)
        self.optimizer = torch.optim.Adam(
            params=self.q_learner.parameters(),
            lr=self.args.lr)
        self.q_target.load_state_dict(self.q_learner.state_dict())


def train_test_split(df, test_size=0.1, n_prev = 100):
    """
    This just splits data to training and testing parts
    """
    ntrn = round(len(df) * (1 - test_size))
    ntrn = int(ntrn)
    X_train, y_train = _load_data(df.iloc[0:ntrn], n_prev)
    X_test, y_test = _load_data(df.iloc[ntrn:], n_prev)

    return (X_train, y_train), (X_test, y_test)

def _load_data(data, n_prev = 100):
    """
    data should be pd.DataFrame()
    """
    import numpy as np
    docX, docY = [], []
    for i in range(len(data)-n_prev):
        docX.append(data.iloc[i:i+n_prev].values)
        docY.append(data.iloc[i+n_prev].values)
    alsX = np.array(docX)
    alsY = np.array(docY)

    return alsX, alsY

def standard_0_1(df):
    xmax = df.max()
    xmin = df.min()

    return  (df - xmin )/(xmax -xmin)


import sys

if len(sys.argv) < 3 or any( i == "" for i in sys.argv):
    print("error")
    exit()

def indexwanted(table):
    table1 = table[0:-1]
    for i in table:
        if i != table[-1]:
            break
    else:
        result = 0
        return result
    for i in range(0, len(table1)):
        if table1[i] == table[-1]:
            return i + 1
    else:
        return -1

arg1 = sys.argv[1:]

print(indexwanted(arg1))


from flask import Flask, render_template, request
from calculations import find_best_dividend
app = Flask(__name__)

@app.route("/", methods=['POST', 'GET'])
def find_stocks():
   if request.method == 'POST':
      budget = request.form['money']
      if budget == "":
          return render_template('home.html')
      list_of_combinations = find_best_dividend(float(budget))
      return render_template('results.html', stock_combos=list_of_combinations, budget=budget)
   else:
      return render_template('home.html')


import costmap
import matplotlib.pyplot as plt
from a_star import indexToWorld

my_data = costmap.my_costmap
print ("my_costmap:", my_data)

map_width = 74
map_height = 74

map_2d = [[0]*map_width for _ in range(map_height)]
print("Map:", map_2d, len(map_2d), len(map_2d[6])) ## perfect

for x in range(map_width):
    for y in range(map_height):
        map_2d[x][y] = my_data[((x*74)+y)]

## debug
'''print ("XY:",map_2d )
counter =0
for x in range (74):
    for y in range (74):
        if map_2d[x][y] == my_data[(((x*74)+y))]:
            counter = counter +1

print ("Counter: ",counter )'''

new_data = []
for i in range (73, -1, -1):
    for j in range (74):
        new_data.append(my_data[i*74+j])
print ("len", len(new_data))

xy_new_data = [0] * len (new_data)

for i in range (len(new_data)):
    xy_new_data[i] = indexToWorld(new_data[i], 74, 0.2 , [0,0])

x_new_data = [0] * len(new_data)
y_new_data = [0] * len(new_data)

for i in range (len(new_data)):
    x_new_data[i] = round(xy_new_data[i][0],2)
    y_new_data[i] = round(xy_new_data[i][1],2)

for i in range (74):
    for j in range (74):
        print (i,j, my_data[i*74+j], new_data[i*74+j])
#plt.plot(x_new_data,y_new_data,"o")
#plt.show()

### Extracting obstacle

static_obstacle = []

for i in range (len(new_data)):
    if (new_data[i] == 254 or new_data[i] == 255):
        static_obstacle.append(i)

print("static_obstacle:", static_obstacle, len(static_obstacle))

##  We got the obstacle index
## convert it into XY world

alg_width = 74
alg_resolution =0.2
alg_origin = [0,0]

x_static_obs = [0] * len (static_obstacle)
y_static_obs = [0] * len (static_obstacle)
xy_static_obs = [0] * len (static_obstacle)

for i in range (len(static_obstacle)):
    xy_static_obs[i] = indexToWorld(static_obstacle[i], alg_width, alg_resolution, alg_origin)
print ("xy_obstacel:", xy_static_obs, len(xy_static_obs), "lop")

for i in range (len(static_obstacle)):
    x_static_obs[i] = round(xy_static_obs[i][0],2)
    y_static_obs[i] = round(xy_static_obs[i][1],2)

##plots2
plt.plot(x_static_obs, y_static_obs, 'o')
plt.show()