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

#
The Hitters Dataset: Preprocessing and Preparation

# #### Importing the Libraries

# In[13]:

import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

from sklearn.preprocessing import StandardScaler

from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV

from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

import warnings
warnings.filterwarnings("ignore")

# #### Load the data

# In[4]:

data = pd.read_csv('./data/Hitters.csv')
data.head()

# #### make a copy of the original DF

# In[7]:

df_hitters = data.copy()
df_hitters
#Salary target variable = predict a baseball players salary based on the rest of the features

# #### Categorical Variables

# In[8]:

print('The league types are:', df_hitters['League'].unique())
print('The division types are:', df_hitters['Division'].unique())
print('The new league options are:', df_hitters['NewLeague'].unique())

# #### Converts categorical variables into 0s and 1s

# In[9]:

df_hitters_num = pd.get_dummies(df_hitters, columns = ['League', 'Division', 'NewLeague'], drop_first=True)
df_hitters_num

# #### Checking NaN values

# In[10]:

df_hitters_num.isnull().sum()

# #### Fill NaNs with predicted values

# In[11]:

df_hitters_num_nonull = df_hitters_num.dropna()
df_hitters_num_nonull.isnull().sum()

# In[17]:

numeric_df = df_hitters.select_dtypes(include='number')

# In[18]:

# check for multicolinearity
plt.figure(figsize=(13,6))
sns.heatmap(numeric_df.corr(),
            vmin = -1,
            vmax = 1,
            cmap = "GnBu",
            annot=True)
plt.show()

#
Exploratory Data Analysis

# #### Plot salary from df_hitter_num_nonull

# In[14]:

sns.displot(df_hitters_num_nonull['Salary']);

# #### Check the relationship of  dependent and the independent variable in the dataset

# In[15]:

correlation = df_hitters_num_nonull.corr()
correlation['Salary'].sort_values(ascending=True)
#salaray = dependent variable
# the others one are independent variables
# pearson correlation ( 1 stron positive correlation / -1 strong negative correlation / 0 there is no relationship)

# #### Declare the dependent and independent variables

# In[19]:

X = df_hitters_num_nonull.drop('Salary', axis = 1)
y = df_hitters_num_nonull['Salary']

# #### Split the data into train and test sets

# In[21]:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state = 365)

# In[23]:

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# #### Perform linear regression

# In[26]:

lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)

# In[28]:

print("Linear Regression coefficients are: ", lin_reg.coef_)
print("Linear Regression y-intercept is: ", lin_reg.intercept_)

# #### create a new variable

# In[29]:

lin_reg_y_pred = lin_reg.predict(X_test)
lin_reg_y_pred

# #### Compre the predicted values with the actual ones

# In[30]:

#create a DF containing the results
lin_comp = pd.DataFrame({'Predicted': lin_reg_y_pred, 'Actual': y_test})
lin_comp

# #### Performing Linear Regression

# In[32]:

print("Linear Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, lin_reg_y_pred)))
print("Linear Regression Model Training Score: ", lin_reg.score(X_train, y_train))
print("Linear Regression Model Testing Score: ", lin_reg.score(X_test, y_test))

# #### Performing Ridge Regression with Cross-validation

# In[34]:

cv = RepeatedKFold(n_splits=5, n_repeats=3, random_state=1)

# In[35]:

ridge = RidgeCV(alphas=np.arange(0.1, 10, 0.1), cv=cv, scoring='neg_mean_squared_error')

# In[36]:

ridge.fit(X_train, y_train)
ridge_reg_y_pred = ridge.predict(X_test)

print("Ridge tuning parameter:", (ridge.alpha_))
print("Ridge model cofficients:", (ridge.coef_))
print("Ridge model intercept:", (ridge.intercept_))

# In[43]:

print("Ridge Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, ridge_reg_y_pred)))
print("Ridge Regression Model Training Score: ", ridge.score(X_train, y_train))
print("Ridge Regression Model Testing Score: ", ridge.score(X_test, y_test))

# #### Performing Lasso Regresion with Cross-validation
#
# Lasso regression decreases the coefficients values in a way that the least significant of them can fall all the way to 0

# In[37]:

lasso = LassoCV(alphas=np.arange(0.1, 10, 0.1), cv=cv, tol=1) # 1 to prevents dualiuty gap and #tol = optimization tolerance tell the algorithm to stop searching once the tolerance between minimum and maximum is achieved

# In[38]:

lasso.fit(X_train, y_train)
lasso_reg_y_pred = lasso.predict(X_test)

print("Lasso tuning parameter:", (lasso.alpha_))
print("Lasso model cofficients:", (lasso.coef_))
print("Lasso model intercept:", (lasso.intercept_))

# In[47]:

print("Lasso Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, lasso_reg_y_pred)))
print("Lasso Regression Model Training Score: ", lasso.score(X_train, y_train))
print("Lasso Regression Model Testing Score: ", lasso.score(X_test, y_test))

#
Compare the Scores

# In[44]:

print("Linear Regression Model Training Score: ", lin_reg.score(X_train, y_train)) #highes value
print("Linear Regression Model Testing Score: ", lin_reg.score(X_test, y_test)) #Only 35% of the data managed to fit the model properly

print("Ridge Regression Model Training Score: ", ridge.score(X_train, y_train))
print("Ridge Regression Model Testing Score: ", ridge.score(X_test, y_test))

print("Lasso Regression Model Training Score: ", lasso.score(X_train, y_train))
print("Lasso Regression Model Testing Score: ", lasso.score(X_test, y_test))

#
Compare the root mean squared error | RMSE

# In[46]:

print("Linear Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, lin_reg_y_pred)))
#the average distance between predicted and actual values is shortest when applying ridge regression
print("Ridge Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, ridge_reg_y_pred)))

print("asso Regression Model RMSE is: ", math.sqrt(mean_squared_error(y_test, lasso_reg_y_pred)))

# #### Conclusion
#  **Ridge** and **Lasso** handle the data better than the linear regression
#
#  In this case **Ridge Regression** is the properly algorithm
#
#  It is essential to understand the data and what problem you are facin in order to choose the most suitable approach every time

# #### Replacing the Missing Values in the DataFrame

# ##### Store null values in a new variable

# In[48]:

# 59 register as a NaN values would be replaced after predictions
df_hitters_nan = df_hitters_num[df_hitters_num['Salary'].isnull()]
df_hitters_nan

# ##### Assign the dependent and independent variables

# In[49]:

X_nan = df_hitters_nan.drop('Salary', axis=1)
y_nan = df_hitters_nan['Salary']

# In[50]:

scaler = StandardScaler()
X_nan = scaler.fit_transform(X_nan)

# In[51]:

nan_pred = ridge.predict(X_nan)
nan_pred

# #### Populate the dataframe

# In[53]:

df_nan_full = df_hitters_nan.copy()
df_nan_full['Salary'] = nan_pred
df_nan_full

Applied Python for DS & ML/Machine Learning with Python/Machine Learning with Ridge and Lasso Regression/the hitters dataset preprocessing and preparation.py


import torch
from experiments import constants

# from sidd.pipeline import process_sidd_image
from matplotlib import pyplot as plt
from experiments.data_model.edge_position.edge_image_generator import EdgeImageGenerator
from experiments.analysis.analysis_helpers import rggb2rgb

# def unpack_raw(raw4ch):
#     """Unpacks 4 channels to Bayer image (h/2, w/2, 4) --> (h, w)."""
#     img_shape = raw4ch.shape
#     h = img_shape[0]
#     w = img_shape[1]
#     # d = img_shape[2]
#     bayer = np.zeros([h * 2, w * 2], dtype=np.float32)
#     # bayer = raw4ch
#     # bayer.reshape((h * 2, w * 2))
#     bayer[0::2, 0::2] = raw4ch[:, :, 0]
#     bayer[0::2, 1::2] = raw4ch[:, :, 1]
#     bayer[1::2, 1::2] = raw4ch[:, :, 2]
#     bayer[1::2, 0::2] = raw4ch[:, :, 3]
#     return bayer
#
#
# with open("../metadata_edge.pickle", "rb") as f:
#     bayer_2by2, wb, cst2 = pickle.load(f)

batch_size = 1
gef = EdgeImageGenerator(patch_size=32)
generate_image = gef.get_image_function(2, color_swip=False)
cross_point = 16

theta_vector = cross_point * torch.ones(batch_size, requires_grad=True).to(constants.DEVICE)
I = generate_image(theta_vector)
I = I.cpu().detach().numpy()[0, :, :, :]
# I = process_sidd_image(unpack_raw(I), bayer_2by2, wb, cst2)
# plt.subplot(2, 2, 1)
# plt.imshow(rggb2rgb(I))
# plt.axis('off')
# plt.title(f"Position:{cross_point}, Edge Width:{2}")
plt.subplot(1, 2, 1)
cross_point = 8
theta_vector = cross_point * torch.ones(batch_size, requires_grad=True).to(constants.DEVICE)
I = generate_image(theta_vector)
I = I.cpu().detach().numpy()[0, :, :, :]
# I = process_sidd_image(unpack_raw(I), bayer_2by2, wb, cst2)
plt.imshow(rggb2rgb(I))
plt.axis('off')
plt.title(f"Position:{cross_point}, Edge Width:{2}")
# plt.subplot(2, 2, 3)
# cross_point = 16
# theta_vector = cross_point * torch.ones(batch_size, requires_grad=True).to(constants.DEVICE)
# generate_image = gef.get_image_function(4, color_swip=False)
# I = generate_image(theta_vector)
# I = I.cpu().detach().numpy()[0, :, :, :]
# # I = process_sidd_image(unpack_raw(I), bayer_2by2, wb, cst2)
# plt.imshow(rggb2rgb(I))
# plt.axis('off')
# plt.title(f"Position:{cross_point}, Edge Width:{4}")
plt.subplot(1, 2, 2)
cross_point = 16
theta_vector = cross_point * torch.ones(batch_size, requires_grad=True).to(constants.DEVICE)
generate_image = gef.get_image_function(1, color_swip=False)
I = generate_image(theta_vector)
I = I.cpu().detach().numpy()[0, :, :, :]
plt.imshow(rggb2rgb(I))
plt.axis('off')
plt.title(f"Position:{cross_point}, Edge Width:{1}")
# plt.gca().set_position([0, 0, 1, 1])
plt.savefig("clean_edge_example.svg")
plt.show()


from flask import Blueprint
from flask_socketio import join_room, emit, close_room
from cryptography.fernet import Fernet
import unicodedata
import json

from src.database.db_pg import db
from src.services.common.message_service import MessageService

class MessageHandler:
    def __init__(self, socketio_instance):
        self.key = Fernet.generate_key()
        self.cipher_suite = Fernet(self.key)
        self.private_rooms = {}
        self.socketio = socketio_instance
        self.message_service = MessageService()

    def encrypt(self, message):
        encrypted_message = self.cipher_suite.encrypt(message.encode())
        return encrypted_message.decode("ascii")

    def decrypt(self, encrypted_message):
        decrypted_message = self.cipher_suite.decrypt(encrypted_message.encode("ascii"))
        decrypted_message = decrypted_message.decode("utf-8")
        decrypted_message = unicodedata.normalize("NFKD", decrypted_message)
        return decrypted_message

    def assign_user_to_room(self, data):
        room_name = data["room_name"]
        user = data["user"]

        if room_name not in self.private_rooms:
            self.private_rooms[room_name] = [user]
        else:
            if user["fullname"] not in [
                u["fullname"] for u in self.private_rooms[room_name]
            ]:
                if len(self.private_rooms[room_name]) >= 2:
                    emit(
                        "error_joining_room",
                        {
                            "message": "La sala privada ya tiene 2 usuarios asignados. No puedes unirte.",
                            "success": False,
                        },
                    )
                    return
                self.private_rooms[room_name].append(user)

        join_room(room_name)

        if len(self.private_rooms[room_name]) < 2:
            random_user = self.message_service.get_random_user()
            self.private_rooms[room_name].append(random_user)

    def handle_send_message(self, data):
        room_name = data["room_name"]
        fullname = data["fullname"]
        message = data["message"]
        id_user = data["id"]
        date = data["date"]

        encrypted_message = self.encrypt(message)
        decrypted_message = self.decrypt(encrypted_message)

        users_in_room = self.private_rooms.get(room_name, [])
        users = self.private_rooms.get(room_name, [])

        # Cliente
        user_sender = next((user for user in users if user["id"] == id_user), None)

        # Usuario
        user_receiver = next((user for user in users if user["id"] != id_user), None)

        if fullname in [u["fullname"] for u in users_in_room]:
            self.socketio.emit(
                "get_messages",
                data={
                    "room_name": room_name,
                    "fullname": user_sender["fullname"],
                    "message": message,
                    "id": user_sender["id"],
                    "date": date,
                },
                room=room_name,
            )
            conversation_uuid = self.message_service.save_conversation(
                {
                    "user_id": user_receiver["id"],
                    "client_conversation_id": user_sender["id"],
                }
            )
            self.message_service.save_message(
                {
                    "uuid_conversation": conversation_uuid,
                    "id_user_sender": user_sender["id"],
                    "id_user_receiver": user_receiver["id"],
                    "message_text": message,
                    "message_traslated_text": "",
                    "message_read": 0,
                }
            )

        else:
            emit(
                "error_send_message",
                {
                    "error": "No tienes permiso para enviar mensajes en esta sala privada."
                },
            )

    def handle_close_private_room(self, data):
        room_name = data["room_name"]
        user = data["user"]

        if user["fullname"] in self.private_rooms.get(room_name, []):
            del self.private_rooms[room_name]
            close_room(room_name)
            emit(
                "private_room_closed",
                {
                    "room_name": room_name,
                    "message": f"Sala privada '{room_name}' cerrada por {user['fullname']}.",
                },
            )
        else:
            emit(
                "error", {"message": "No tienes permiso para cerrar esta sala privada."}
            )


'''Dada una lista de superhéroes de comics, de los cuales se conoce su nombre, año aparición, casa de comic a la que pertenece (Marvel o DC) y biografía, implementar la funciones necesarias para poder realizar las siguientes actividades:

a. eliminar el nodo que contiene la información de Linterna Verde;
b. mostrar el año de aparición de Wolverine;
c. cambiar la casa de Dr. Strange a Marvel;
d. mostrar el nombre de aquellos superhéroes que en su biografía menciona la palabra
“traje” o “armadura”;
e. mostrar el nombre y la casa de los superhéroes cuya fecha de aparición
sea anterior a 1963;
f. mostrar la casa a la que pertenece Capitana Marvel y Mujer Maravilla;
g. mostrar toda la información de Flash y Star-Lord;
h. listar los superhéroes que comienzan con la letra B, M y S;
i. determinar cuántos superhéroes hay de cada casa de comic.'''

import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath("algoritmos_2023"))))

from classList import List

heroesList = List("name")

class Hero():
    def __init__(self, name, year, comicHouse, biography):
        self.name = name
        self.year = year
        self.comicHouse = comicHouse
        self.biography = biography

    def __str__(self) -> str:
        return f"{self.year} | {self.name} | {self.comicHouse} \n {self.biography} \n"

superheroes = [
  {
    "name": "Green Lantern",
    "year": 1940,
    "comicHouse": "DC",
    "biography": "Linterna Verde, también conocido como Hal Jordan..."
  },
  {
    "name": "Wolverine",
    "year": 1974,
    "comicHouse": "Marvel",
    "biography": "Wolverine, también conocido como Logan, es un mutante con garras retráctiles, sentidos agudizados y un factor de curación. Su pasado está envuelto en misterio, pero es conocido por su actitud hosca y su disposición para hacer lo que sea necesario para proteger a los mutantes y a sus aliados."
  },
  {
    "name": "Doctor Strange",
    "year": 1963,
    "comicHouse": "Marvel",
    "biography": "Doctor Strange, también conocido como Stephen Strange, es un antiguo cirujano convertido en el Hechicero Supremo. Posee habilidades místicas y es capaz de manipular la realidad, viajar en el tiempo y lanzar hechizos poderosos. Utiliza su conocimiento de las artes místicas para proteger al mundo de amenazas sobrenaturales."
  },
  {
    "name": "Captain Marvel",
    "year": 1967,
    "comicHouse": "Marvel",
    "biography": "Capitana Marvel, también conocida como Carol Danvers, es una ex piloto de la Fuerza Aérea de los Estados Unidos. Obtuvo sus poderes cuando fue expuesta a la tecnología alienígena y se convirtió en una híbrida humana-Kree. Posee fuerza sobrehumana, vuelo y la capacidad de generar energía cósmica. Capitana Marvel defiende la Tierra de amenazas extraterrestres y lucha por la justicia."
  },
  {
    "name": "Wonder Woman",
    "year": 1941,
    "comicHouse": "DC",
    "biography": "Mujer Maravilla, también conocida como Diana Prince, es una princesa guerrera amazona. Posee fuerza sobrehumana, agilidad y la capacidad de volar. Mujer Maravilla lucha por la justicia, el amor y la paz en un mundo lleno de conflictos."
  },
  {
    "name": "Flash",
    "year": 1940,
    "comicHouse": "DC",
    "biography": "Flash, también conocido como Barry Allen, es un velocista sobrehumano. Puede correr a velocidades increíbles y atravesar el tiempo. Utiliza sus habilidades para proteger Central City y mantener la paz. El traje de Flash está diseñado para soportar sus velocidades extremas y protegerlo mientras corre a través del espacio-tiempo."
  },
  {
    "name": "Star-Lord",
    "year": 1976,
    "comicHouse": "Marvel",
    "biography": "Star-Lord, también conocido como Peter Quill, es un aventurero espacial y líder de los Guardianes de la Galaxia. Posee habilidades de combate, manejo de armas y utiliza un traje especial con una armadura avanzada. Star-Lord viaja por el cosmos protegiendo a la galaxia de amenazas intergalácticas."
  }
]

for data in superheroes:
    hero = Hero(data["name"], data["year"], data["comicHouse"], data["biography"])
    heroesList.insert(hero)

# A
heroesList.delete("Green Lantern")

# B
print(heroesList.getByValue("Wolverine").year)

# C
heroesList.getByValue("Doctor Strange").comicHouse = "DC"

# D
counter = 0

while counter != heroesList.size():
    biography = heroesList.getByIndex(counter).biography

    if ("traje" in biography or "armadura" in biography):
        print(biography)
        break

    counter += 1

# E
heroesList.orderBy("year")
print(heroesList.getByIndex(heroesList.search(1963, "year")).name)

# F
heroesList.orderBy()
print(heroesList.getByIndex(heroesList.search("Captain Marvel")).comicHouse)
print(heroesList.getByIndex(heroesList.search("Wonder Woman")).comicHouse)

# G
print(heroesList.getByValue("Star-Lord"))
print(heroesList.getByValue("Flash"))

# H
counter = 0

while counter != heroesList.size():
    heroName = heroesList.getByIndex(counter).name

    firstLetter = heroName[0].lower()

    if (firstLetter == "b" or firstLetter == "m" or firstLetter == "s"):
        print(heroName)

    counter += 1

# I

counter = 0

marvel = 0
dc = 0

while counter != heroesList.size():
    comicHouse = heroesList.getByIndex(counter).comicHouse.lower()

    if (comicHouse == "dc"):
        dc += 1
    else:
        marvel += 1

    counter += 1

print(f"Superheroes de DC: {dc}")
print(f"Superheroes de Marvel: {marvel}")


import pandas as pd
import numpy as np
from pypfopt.efficient_frontier import EfficientFrontier
from src.varianceHedge.tools.op_w import compute_op_w
from tqdm import tqdm

pd.options.plotting.backend = "plotly"
pd.set_option('display.width', 1000)
pd.set_option('display.max_columns', 500)

init_port_val = 10000

w_threshold = 0.20
price_sampling_size = 61

mids = pd.read_pickle('datasets/consolidated_binance.pkl').astype('float64').ffill().resample('1min').last()

syms = ['btcgbp', 'ethgbp', 'xrpgbp'] # mids.columns

mids = mids[syms]

# st_bt = mids.index[0] + pd.Timedelta(f"{price_sampling_size}M")
st_bt = pd.to_datetime('2020-09-01 00:00:00')

hist_mid = pd.DataFrame(columns=syms)
hist_p = pd.DataFrame(columns=['MV','NRP', 'RP', 'MS', 'UW'])
hist_w = {}
hist_n = {}
hist_opt_w ={}

for dt, mid in tqdm(mids.iterrows(), total=mids.shape[0]):
    # print(f"current dt: {dt}")
    ## Append Current Prices
    hist_mid.loc[dt] = mid
    for port in hist_p.columns:
        if dt <pd.to_datetime(st_bt):

            continue
        if dt==pd.to_datetime(st_bt):
            hist_w[port] = pd.DataFrame([getattr(compute_op_w, f"compute_{port}_weights")(
                cov=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().cov(),
                exp_ret=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().mean())],
                columns=syms,
                index=[st_bt])

            hist_n[port] = (init_port_val * (hist_w[port].loc[st_bt]) / mids.loc[st_bt]).to_frame().T

            hist_opt_w[port] = pd.DataFrame([getattr(compute_op_w, f"compute_{port}_weights")(
                cov=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().cov(),
                exp_ret=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().mean())],
                columns=syms,
                index=[st_bt])

        ## Previous n holdings multiply current prices
        curr_port_val = np.sum(hist_n[port].iloc[-1]*mids.loc[dt])
        hist_p.loc[dt, port] = curr_port_val

        # print('port')
        # print(port)
        # print('syms')
        # print(syms)
        # print('cov')
        # print(hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().cov())
        # print('w')
        # print(getattr(compute_op_w,
        #                           f"compute_{port}_weights")(cov=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().cov(),
        #                                                      exp_ret=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().mean()))
        #

        opt_w = pd.Series(getattr(compute_op_w,
                                  f"compute_{port}_weights")(cov=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().cov(),
                                                             exp_ret=hist_mid[-price_sampling_size:].dropna(how='all', axis=1).pct_change().mean()),
                          index=syms)

        hist_opt_w[port].loc[dt] = opt_w

        ## sort by diff, making always selling first before buying in rebalancing
        diff_w = (opt_w - hist_w[port].iloc[-1]).sort_values(ascending=True)
        prev_dt = hist_n[port].iloc[-1].name

        # print(f"{port} prev dt: {prev_dt}")
        ## TODO changed from mean to max
        if diff_w.abs().max() > w_threshold:
            for sym, diff_w_i in diff_w.items():
                n_i = diff_w_i*curr_port_val / mids.loc[dt, sym]
                hist_n[port].loc[dt, sym] = hist_n[port].loc[prev_dt][sym] + n_i

        # print(hist_n[port])
        hist_w[port].loc[dt] = hist_n[port].iloc[-1] * mids.loc[dt] / np.sum(hist_n[port].iloc[-1] * mids.loc[dt])

hist_p.to_pickle(f"results/binance/bt_mid_sampling_{price_sampling_size}M.pkl")

fig = hist_p.plot()
fig.update_yaxes(title_text='GBP')
fig.write_html(f"results/binance/bt_mid_sampling_{price_sampling_size}M.html")
fig.write_image(f"results/binance/bt_mid_sampling_{price_sampling_size}M.png")

    # hist_w_MV = hist_w['MV']
    # hist_opt_w_MV = hist_opt_w['MV']
    #
    # print('hist_w_MV')
    # print(hist_w_MV)
    #
    # print('hist_opt_w_MV')
    # hist_opt_w_MV = hist_opt_w_MV.shift(1).fillna(0)
    # print(hist_opt_w_MV)
    #
    # hist_opt_w_MV.loc['2021-04-01 01:00:00'] = 0
    # hist_opt_port = ((hist_opt_w_MV * mids.pct_change()).loc['2021-04-01 01:00:00':].fillna(0).sum(
    #     axis=1) + 1).cumprod() * init_port_val
    #
    # print('apply_w_MV')
    # hist_cov = mids.pct_change().rolling('60min', min_periods=60).cov().dropna()
    # print('apply_cov')
    # print(hist_cov)
    # apply_w_MV = hist_cov.groupby(level=0, axis=0).apply(apply_compute_MV_weights).apply(pd.Series).dropna().shift(1)
    # apply_w_MV.columns = mids.columns
    # print(apply_w_MV)
    #
    # hist_w_MV = hist_w_MV.shift(1).fillna(0)
    # mv_port = ((hist_w_MV * mids.pct_change()).loc['2021-04-01 01:00:00':].fillna(0).sum(axis=1) + 1).cumprod() * init_port_val
    #
    # apply_w_MV.loc['2021-04-01 01:00:00'] = 0
    # apply_mv_port = ((apply_w_MV * mids.pct_change()).loc['2021-04-01 01:00:00':].fillna(0).sum(axis=1) + 1).cumprod() * init_port_val
    # print('apply_mv_port')
    # print(apply_mv_port)
    #
    # compare_df = pd.concat([mv_port, hist_p['MV'], apply_mv_port.rename('apply_w_MV'), hist_opt_port.rename('hist_opt')], axis=1).rename(columns={0:'hist_w_MV'}).astype('float64')
    # print('compare_df')
    # print(compare_df)
    #fig = compare_df.plot()
    #fig.show()

    # print(month_dt)
    # print(hist_p)


#!/usr/bin/python3
"""
Test model - Amenity
"""

from models.base_model import BaseModel
from models.amenity import Amenity
import unittest
import pep8

class TestAmenity(unittest.TestCase):
    """
    Amenity test class
    """

    @classmethod
    def setUpClass(cls):
        """
        Setting up class attributes
        """
        cls.amenity = Amenity()
        cls.amenity.name = ""

    @classmethod
    def tearDownClass(cls):
        """
        Teardown at the end of the test
        """
        del cls.amenity

    def test_is_subclass(self):
        """
        Method that checks:
            if the subclass is set correctly
        """
        amenity = Amenity()
        self.assertTrue(issubclass(amenity.__class__, BaseModel))

    def test_name_attr(self):
        """
        Method that checks:
            if the name of class is set correctly
        """
        amenity = Amenity()
        self.assertEqual(amenity.__class__.__name__, "Amenity")

    def test_to_dict_creates_dict(self):
        """
        Method that checks:
            if to_dict is set correctly
        """
        amenity = Amenity()
        self.assertTrue(hasattr(amenity, 'to_dict') is True)
        am_dict = amenity.to_dict
        self.assertTrue(type(am_dict), dict)

    def test_str(self):
        """
        Method that checks:
            if name is set correctly
        """
        amenity = Amenity()
        self.assertTrue(type(amenity.name) is str)

    def test_pep8_conformance_file_storage(self):
        """
        Method that tests:
            if a file meet with pep8 criteria
        """
        style = pep8.StyleGuide()
        check = style.check_files(['models/amenity.py'])
        self.assertEqual(
            check.total_errors,
            0,
            'PEP8 style errors: %d' % check.total_errors
        )

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


import unittest
from pcbdl import *

class TestNet(unittest.TestCase):
    def test_create(self):
        """Net creation test"""
        n = Net("test_create")

    def test_duplicate(self):
        Net("create_duplicate")
        with self.assertRaises(Exception):
            Net("create_duplicate")

    def test_str(self):
        """Net naming, both uppercasing and anonymous nets"""
        n = Net("test_str")
        self.assertEqual(str(n), "TEST_STR")

        n = Net()
        self.assertIn("anon", str(n).lower())

    def test_repr(self):
        """Net representations should pretty and contain some info about what's connected"""
        n = Net()
        self.assertIn("unconnected", repr(n))

        r0 = R()
        n << r0
        self.assertIn("connected", repr(n))
        self.assertIn(r0.refdes, repr(n))

        r1 = R()
        n << r1
        self.assertIn(",", repr(n), "there should be a comma separated list of components")
        self.assertIn(r1.refdes, repr(n))

        for i in range(100):
            n << R()
        self.assertIn(str(len(n.connections)), repr(n),
                      "we should show the number of connections after we can't display them all")

    def test_connections(self):
        """Various Net connection tests"""
        n = Net()
        rs = R(), R()

        # Adding by part, chaining
        n << rs[0] << rs[1]

        self.assertEqual({c.part for c in n.connections}, set(rs), "<< failed")

        # Adding by specific pin
        n << R().P1
        self.assertEqual(len(n.connections), 3, "<< on a pin failed")

        # Adding a whole list of things instead of chaining
        n << (R() for i in range(10))

        self.assertEqual(len(n.connections), 13, "<< on a list failed")

        with self.assertRaises(TypeError, msg="this would be silly to work, connecting something of a random type to a net"):
            n << 2

class DefinedAtTest(unittest.TestCase):
    """Make sure all the part/net .defined_at point to this file, not something inside the library proper."""

    def check_defined_at(self, o):
        self.assertIn(__file__, o.defined_at, "%r.defined_at should point to this file, not something inside the library proper." % o)

    def test_created_net(self):
        n = Net()
        self.check_defined_at(n)

    def test_part(self):
        p = Part()
        self.check_defined_at(p)

    def test_implicit_net(self):
        r = R()
        n = r.P1.net
        self.check_defined_at(n)

        tp = TP()
        n = tp.net
        self.check_defined_at(n)

class PartTest(unittest.TestCase):
    def test_automatic_name_collision(self):
        """Can we make a lot of uniquely named resistors"""
        parts = []
        names = set()
        for i in range(1000):
            r = R()
            parts.append(r)
            refdes = r.refdes
            self.assertNotIn(refdes, names)
            names.add(refdes)

    def test_duplicate(self):
        """We shouldn't allow 2 parts with the same refdes."""
        Part(refdes="duplicate_part")
        with self.assertRaises(Exception):
            Part(refdes="duplicate_part")

    def test_part_naming(self):
        p = Part()
        p.refdes = "naming_test"
        self.assertEqual(p.refdes, "NAMING_TEST")

    def test_repr_str(self):
        """Part __repr__ and __str__ contains part refdes"""
        p = Part()
        self.assertIn(p.refdes, str(p))
        self.assertIn(p.refdes, repr(p))

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


# coding=utf-8

import unittest
from XX.unittest.init import *

class BaiduSo(Init):
    '''
    分离测试固件,固件存在init.py文件中
    '''

    def test_baidu_news(self):
        self.driver.find_element_by_id('kw').send_keys('webdriver')

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


import datetime
from enum import IntEnum
import peewee as pw

dbname = "threshold_share"
dbuser = dbname
database = pw.MySQLDatabase(dbname, user=dbuser,
                            unix_socket="/run/mysqld/mysqld.sock")

class KeyStatus(IntEnum):
    not_sent, sent, recvd = range(3)

class KeyStatusField(pw.IntegerField):
    def db_value(self, value):
        return int(value)

    def python_value(self, value):
        for status in KeyStatus:
            if value == status:
                return status

class BaseModel(pw.Model):
    class Meta:
        database = database

class User(BaseModel):
    name = pw.CharField(max_length=100)

class Message(BaseModel):
    sender = pw.ForeignKeyField(User)
    filename = pw.CharField()
    is_encrypted = pw.BooleanField(default=True)
    threshold_number = pw.IntegerField()
    number_of_subkeys = pw.IntegerField(default=0)
    valid_till = pw.DateTimeField(default=datetime.datetime.now)

class MessageToReceiver(BaseModel):
    message = pw.ForeignKeyField(Message)
    receiver = pw.ForeignKeyField(User)
    subkey = pw.CharField()
    status = KeyStatusField(default=KeyStatus.not_sent)

    class Meta:
        primary_key = pw.CompositeKey("message", "receiver")


from setuptools import setup, find_packages
import os

FULL_VERSION = '0.0.0'
GPU = bool(os.getenv('GPU'))
if GPU:
    tensorflow_name = 'tensorflow-gpu>=1.10.1,<=1.13.1'
else:
    tensorflow_name = 'tensorflow'

with open('README.md') as f:
    readme = f.read()

setup(
    name='deep_semantic_segmentation',
    version=FULL_VERSION,
    description='Semantic Segmentation Models',
    url='to be appeared',
    long_description=readme,
    author='Asahi Ushio',
    author_email='to be appeared',
    packages=find_packages(exclude=('test', 'dataset', 'random', 'tfrecord', 'checkpoint')),
    include_package_data=True,
    test_suite='test',
    install_requires=[
        'Pillow',
        'numpy',
        tensorflow_name,
        'scipy>=1.2.0',
        'cython'
        # 'toml>=0.10.0',
        # 'pandas',
        # 'nltk',
        # 'sklearn',
        # 'flask',
        # 'werkzeug',
    ]
)


'''
377. Combination Sum IV
Medium

Given an array of distinct integers nums and a target integer target,
return the number of possible combinations that add up to target.

The answer is guaranteed to fit in a 32-bit integer.

Example 1:

Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Example 2:

Input: nums = [9], target = 3
Output: 0

Constraints:

1 <= nums.length <= 200
1 <= nums[i] <= 1000
All the elements of nums are unique.
1 <= target <= 1000

Follow up: What if negative numbers  are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

'''

## Extra question
## if negative numbers are allow, then you may have infinitly many solutions
## say -1 and 1 both in the set, then you get zeros. so if you don't enforce
## each number being used at most once, then you get infinitely many solutions
## constraints could be: no combination = 0
class Solution:
    def combinationSum4_knapsack(self, nums: List[int], target: int) -> int:
        ##knapsack

        min_n = min(nums)
        if min_n > target:
            return 0
        if min_n == target:
            return 1
        nums = [ss for ss in nums if ss <= target]
        nums.sort()

        sz = len(nums)

        ## to get target kk, number of ways
        dp = [0 for _ in range(target+1)]
        dp[0] = 1

        for tar in range(1,target+1):
            for num in nums:
                if num > tar: break
                else:
                    dp[tar] += dp[tar-num]

        return (dp[-1])
    def combinationSum4(self, nums: List[int], target: int) -> int:

        ## number of ways to get w

        min_n = min(nums)
        if min_n > target:
            return 0
        if min_n == target:
            return 1

        nums.sort()
        dp = [ -1 for _ in range(target+1)]
        dp[0] = 1

        def searching(nums, tar, dp):
            if tar <0:
                return 0

            if dp[tar]!=-1:
                return dp[tar]
            dp[tar] = 0
            for nn in nums:
                dp[tar] += searching(nums, tar-nn, dp)
            return dp[tar]

        return searching(nums, target, dp)


import json
import math
import queue
import struct
import sys
from collections import namedtuple
from typing import NamedTuple

import numpy as np
import sounddevice as sd
from vosk import KaldiRecognizer, Model, SetLogLevel

class VadConfig(NamedTuple):
    """発話区間検出を設定するクラス.

    threshold (int): 発話区間検出を判定するパワーのしきい値 (dB)
    vad_start (float): 発話区間を開始判定する秒数（sec）
    vad_end (float): 発話区間を終了判定する秒数 (sec)
    """

    threshold: int = 45
    vad_start: float = 0.3
    vad_end: float = 1.0

class MicrophoneStream:
    """マイク音声入力のためのクラス."""

    def __init__(self, rate, chunk, vad_config):
        """音声入力ストリームを初期化する.

        Args:
           rate (int): サンプリングレート (Hz)
           chunk (int): 音声データを受け取る単位（サンプル数）
           vad_config (VadConfig): 発話区間検出の設定
        """
        # マイク入力のパラメータ
        self.rate = rate
        self.chunk = chunk

        # 入力された音声データを保持するデータキュー（バッファ）
        self.buff = queue.Queue()

        # 発話区間検出のパラメータ
        self.vad_config = {
            "threshold": vad_config.threshold,
            "vad_start": vad_config.vad_start,
            "vad_end": vad_config.vad_end,
        }

        # 発話区間検出の作業用変数
        self.workspace = {
            "is_speaking": False,  # 現在発話区間を認定しているか
            "count_on": 0,  # 現在まででしきい値以上の区間が連続している数
            "count_off": 0,  # 現在まででしきい値以下の区間が連続している数
            "voice_end": False,  # 発話が終了したか
            "str_current_power": "",  # 現在のパワーの値を確認するための文字列（音声認識のクラスから参照）
        }

        # マイク音声入力の初期化
        self.input_stream = None

    def open_stream(self):
        """マイク音声入力の開始"""
        self.input_stream = sd.RawInputStream(
            samplerate=self.rate,
            blocksize=self.chunk,
            dtype="int16",
            channels=1,
            callback=self.callback,
        )

    def callback(self, indata, frames, time, status):
        """音声入力の度に呼び出される関数.
        音声パワーに基づいて発話区間を判定.
        """
        if status:
            print(status, file=sys.stderr)

        # 入力された音声データをキューへ保存
        self.buff.put(bytes(indata))

        # 音声のパワー（音声データの二乗平均）を計算する
        indata2 = struct.unpack(f"{len(indata) / 2:.0f}h", indata)
        rms = math.sqrt(np.square(indata2).mean())
        power = 20 * math.log10(rms) if rms > 0.0 else -math.inf  # RMSからデシベルへ

        self.workspace["str_current_power"] = f"音声パワー {power:5.1f}[dB] "
        print("\r" + self.workspace["str_current_power"], end="")

        # 音声パワーがしきい値以上、かつ発話区間をまだ認定していない場合
        if (
            power >= self.vad_config["threshold"]
            and self.workspace["is_speaking"] is False
        ):

            # しきい値以上の区間のカウンタを増やす
            self.workspace["count_on"] += 1

            # しきい値以上の区間の長さを秒単位に変換
            count_on_sec = float(self.workspace["count_on"] * self.chunk) / self.rate

            # 発話区間の開始を認定するしきい値と比較
            if count_on_sec >= self.vad_config["vad_start"]:
                self.workspace["is_speaking"] = True
                self.workspace["count_on"] = 0

        if power < self.vad_config["threshold"] and self.workspace["is_speaking"]:

            # しきい値以下の区間のカウンタを増やす
            self.workspace["count_off"] += 1

            # しきい値以下の区間の長さを秒単位に変換
            count_off_sec = float(self.workspace["count_off"] * self.chunk) / self.rate

            # 発話区間の終了を認定するしきい値と比較
            if count_off_sec >= self.vad_config["vad_end"]:
                self.workspace["voice_end"] = True
                self.workspace["count_off"] = 0

            # しきい値と比較して、反対の条件のカウンタをリセット
            if power >= self.vad_config["threshold"]:
                self.workspace["count_off"] = 0
            else:
                self.workspace["count_on"] = 0

    def generator(self):
        """音声認識に必要な音声データを取得するための関数."""
        while True:  # キューに保存されているデータを全て取り出す
            # 先頭のデータを取得
            chunk = self.buff.get()
            if chunk is None:
                return
            data = [chunk]

            # まだキューにデータが残っていれば全て取得する
            while True:
                try:
                    chunk = self.buff.get(block=False)
                    if chunk is None:
                        return
                    data.append(chunk)
                except queue.Empty:
                    break

            # yieldにすることでキューのデータを随時取得できるようにする
            yield b"".join(data)

def get_asr_result(vosk_asr):
    """音声認識APIを実行して最終的な認識結果を得る.

    Args:
       vosk_asr (VoskStreamingASR): 音声認識モジュール

    Returns:
       recog_text (str): 音声認識結果
    """
    mic_stream = vosk_asr.microphone_stream
    mic_stream.open_stream()
    with mic_stream.input_stream:
        audio_generator = mic_stream.generator()
        for content in audio_generator:
            if vosk_asr.recognizer.AcceptWaveform(content):
                recog_result = json.loads(vosk_asr.recognizer.Result())
                recog_text = recog_result["text"].split()
                recog_text = "".join(recog_text)  # 空白記号を除去
                return recog_text
        return None

def main(chunk_size=8000, threshold=45, vad_start=0.3, vad_end=1.0):
    """音声認識デモンストレーションを実行.

    Args:
       chunk_size (int): 音声データを受け取る単位（サンプル数）
       threshold (int): 発話区間検出を判定するパワーのしきい値 (dB)
       vad_start (float): 発話区間を開始判定する秒数（sec）
       vad_end (float): 発話区間を終了判定する秒数 (sec)
    """
    SetLogLevel(-1)  # VOSK起動時のログ表示を抑制

    # 入力デバイス情報に基づき、サンプリング周波数の情報を取得
    input_device_info = sd.query_devices(kind="input")
    sample_rate = int(input_device_info["default_samplerate"])

    # 発話区間検出の設定
    vad_config = VadConfig(threshold, vad_start, vad_end)

    # マイク入力を初期化・開始
    mic_stream = MicrophoneStream(sample_rate, chunk_size, vad_config)

    # 音声認識器を構築
    recognizer = KaldiRecognizer(Model("model"), sample_rate)

    # マイク入力ストリームおよび音声認識器をまとめて保持
    VoskStreamingASR = namedtuple(
        "VoskStreamingASR", ["microphone_stream", "recognizer"]
    )
    vosk_asr = VoskStreamingASR(mic_stream, recognizer)

    print("＜認識開始＞")
    recog_result = get_asr_result(vosk_asr)
    print(f"認識結果: {recog_result}")
    print("＜認識終了＞")

if __name__ == "__main__":
    main()


from invest_guru.handlers.simple_moving_average_analytic_handler import handle

expected_symbol = "MSFT"
event = {"symbol": expected_symbol}

def test__handle__success_verify_result(mocker):
    mocker.patch('invest_guru.handlers.simple_moving_average_analytic_handler.simple_moving_average',
                 side_effect=[{"2019-03-09": "105.6878"}, {"2019-03-09": "106.1234"}])

    result = handle(event, None)

    assert result is not None
    assert "company" in result

def test__handle__success_verify_collaborators(mocker):
    sma_patch = mocker.patch('invest_guru.handlers.simple_moving_average_analytic_handler.simple_moving_average',
                             side_effect=[
                                 [
                                     {"2019-03-09": "105.6878"},
                                     {"2019-03-08": "105.3456"},
                                     {"2019-03-07": "105.1122"},
                                 ],
                                 [
                                     {"2019-03-09": "104.8885"},
                                     {"2019-03-08": "104.6844"},
                                     {"2019-03-07": "104.5561"},
                                 ]
                             ])

    handle(event, None)

    sma_patch.assert_any_call(symbol=expected_symbol, time_period=50)
    sma_patch.assert_any_call(symbol=expected_symbol, time_period=300)


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

"""WordNet connector.

Usage:
    connect_wordnets.py translations [--console]
    connect_wordnets.py synsets [--console]
    connect_wordnets.py definitions [--console]
    connect_wordnets.py examples [--console]
    connect_wordnets.py all [--console]
    connect_wordnets.py test
    connect_wordnets.py -h | --help

Options:
    -h --help    Show this screen.
    --console    Write to console.
"""

import os, re
from docopt import docopt
import xml.etree.ElementTree as ET
from collections import defaultdict

def get_id(raw_id, pos_needed=False):
    offset = re.search(r'.*(.)([0-9]{8})', raw_id)
    if offset:
        if pos_needed:
            return offset.group(2), offset.group(1)
        return offset.group(2)
    return None

def pos2ud(pos):
    return {'n' : 'NOUN', 'v' : 'VERB', 'b' : 'ADV', 'a' : 'ADJ'}[pos]

def connecting_wordnets(lang1, lang2, lang1_name, lang2_name):
    connected_wordnets = defaultdict(dict)
    synset_offsets = set(lang1.keys()).intersection(set(lang2.keys()))
    for offset in synset_offsets:
        lang1_pos = lang1[offset]
        lang2_pos = lang2[offset]
        poses = set(list(lang1_pos.keys()) + list(lang2_pos.keys()))
        for pos in poses:
            if pos not in connected_wordnets[offset]:
                connected_wordnets[offset][pos] =  {lang1_name : set(), lang2_name : set()}
            try:
                connected_wordnets[offset][pos][lang1_name].update(lang1[offset][pos])
            except KeyError:
                pass
            try:
                connected_wordnets[offset][pos][lang2_name].update(lang2[offset][pos])
            except KeyError:
                pass
    return connected_wordnets

def extract_fin(fi_wn):
    fin_vocab = {}
    with open(fi_wn, encoding='utf-8') as fi:
        for line in fi:
            data = line.split('\t')
            if len(data) == 6:
                fid, fword = data[0:2]
                if data[4] != 'synonym':
                    continue
                fi_offset, fi_pos = get_id(fid, pos_needed=True)
                if fi_pos == 'r':
                    fi_pos = 'b'
                if fi_offset in fin_vocab:
                    if fi_pos in fin_vocab[fi_offset]:
                        fin_vocab[fi_offset][fi_pos].add(fword)
                    else:
                        fin_vocab[fi_offset][fi_pos] = set([fword])
                else:
                    fin_vocab[fi_offset] = {fi_pos : set([fword])}
    return fin_vocab

def extract_hun(hu_wn):
    hun_vocab = {}
    tree = ET.parse(hu_wn)
    hun_defs = defaultdict(dict)
    hun_examps = defaultdict(dict)
    for synset in tree.getroot():
        huid, hupos = None, None
        huword_list = []
        for element in synset:
            if element.tag == 'ID3':
                huid = element.text
            elif element.tag == 'POS':
                hupos = element.text
            elif element.tag == 'SYNONYM':
                # each synonym tag can have more than one literal element
                for syno_child in element:
                    if syno_child.tag == 'LITERAL':
                        if syno_child.text.startswith('nl_') or syno_child.text.startswith('tnl_'):
                            continue
                        huword_list.append(syno_child.text)
            elif element.tag == 'DEF':
                hudef = element.text
                if not huword_list:
                    continue
            elif element.tag == 'USAGE':
                huexamp = element.text
                if not huword_list:
                    continue
                if huid:
                    hu_offset = get_id(huid)
                    for word in huword_list:
                        if hupos not in hun_examps[hu_offset]:
                            hun_examps[hu_offset][hupos] = dict()
                        if word not in hun_examps[hu_offset][hupos]:
                            hun_examps[hu_offset][hupos][word] = set()
                        hun_examps[hu_offset][hupos][word].add(huexamp)

        if huid and huword_list and hupos:
            hu_offset = get_id(huid)
            if hudef:
                for word in huword_list:
                    if hupos not in hun_defs[hu_offset]:
                        hun_defs[hu_offset][hupos] = dict()
                    if word not in hun_defs[hu_offset][hupos]:
                        hun_defs[hu_offset][hupos][word] = set()
                    hun_defs[hu_offset][hupos][word].add(hudef)
                hudef = None
            if hu_offset in hun_vocab:
                if hupos in hun_vocab[hu_offset]:
                    hun_vocab[hu_offset][hupos].update(huword_list)
                else:
                    hun_vocab[hu_offset][hupos] = set(huword_list)
            else:
                hun_vocab[hu_offset] = {hupos : set(huword_list)}
    return hun_vocab, hun_defs, hun_examps

def write_file(script_dir, pairs, hudefs, huexs, type='translations', to_console=False):
    output_file = {
        'synsets'        : 'wordnet_synsets.tsv',
        'translations'   : 'wordnet_wordpairs.tsv',
        'definitions'    : 'wordnet_definitions_hu.tsv',
        'examples'       : 'wordnet_examples_hu.tsv',
    }[type]

    foutput = os.path.join(script_dir, 'output', output_file)

    if not to_console:
        w = open(foutput, 'w', encoding='utf-8')
    if type == 'translations':
        for offset in pairs:
            for pos in pairs[offset]:
                lang1_words = pairs[offset][pos]['fi']
                lang2_words = pairs[offset][pos]['hu']
                udpos = pos2ud(pos)
                for l1_word in lang1_words:
                    for l2_word in lang2_words:
                        to_print = '{}\t{}\t{}\t{}'.format(l1_word, l2_word, udpos, offset)
                        if to_console:
                            print(to_print)
                        else:
                            w.write(to_print + '\n')
    elif type == 'synsets':
        for offset, worddt in pairs.items():
            for pos in worddt:
                for lang in ['fi', 'hu']:
                    udpos = pos2ud(pos)
                    synsets = '\t'.join(pairs[offset][pos][lang])
                    to_print = '{}\t{}\t{}\t{}'.format(udpos, offset, lang, synsets)
                    if to_console:
                        print(to_print)
                    else:
                        w.write(to_print + '\n')
    elif type == 'definitions':
        if to_console:
            print(write_sentences(hudefs), end='')
        else:
            w.write(write_sentences(hudefs))
    elif type == 'examples':
        if to_console:
            print(write_sentences(huexs), end='')
        else:
            w.write(write_sentences(huexs))

    if not to_console:
        w.close()

def write_sentences(dataset):
    complete_str = ''
    for offset, data in dataset.items():
        for pos, pair in data.items():
            for word, sent_list in pair.items():
                for sent in sent_list:
                    complete_str += '\t'.join([pos2ud(pos), offset, word, sent]) + '\n'
    return complete_str

def main():
    arguments = docopt(__doc__)
    to_console = False
    if arguments['--console']:
        to_console = True
    options = ['translations', 'synsets', 'definitions', 'examples']
    for mode_option in [*options, 'all', 'test']:
        if arguments[mode_option]:
            mode = mode_option

    script_dir = os.path.dirname(__file__)
    if not os.path.exists('output'):
        os.makedirs('output')

    fi_wn = os.path.join(script_dir, 'wordnets', 'fiwn.tsv')
    fin_vocab = extract_fin(fi_wn)

    hu_wn = os.path.join(script_dir, 'wordnets', 'huwn.xml')
    hungarian, hudefs, huexs = extract_hun(hu_wn)

    connected_wordnets = connecting_wordnets(fin_vocab, hungarian, 'fi', 'hu')

    if mode == 'test':
        return

    if mode == 'all':
        for opt in options:
            write_file(script_dir, connected_wordnets, hudefs, huexs, type=opt, to_console=to_console)
    else:
        write_file(script_dir, connected_wordnets, hudefs, huexs, type=mode, to_console=to_console)

if __name__ == '__main__':
    main()


import numpy as np
import torch as th
from rlkit.torch.core import PyTorchModule
import rlkit.torch.pytorch_util as ptu
from rlkit.torch.distributions import (
    Delta
)
from rlkit.torch.networks.stochastic.distribution_generator import DistributionGenerator
import random
from sklearn.svm import LinearSVR

class EncDecPolicy(PyTorchModule):
    def __init__(self, policy, features_keys, vaes=None, incl_state=True, sample=False, latent_size=None,
                 deterministic=False, random_latent=False, window=None, prev_vae=None, prev_incl_state=False,
                 goal_baseline=False):
        super().__init__()

        self.vaes = vaes if vaes is not None else []
        self.policy = policy
        if deterministic:
            assert isinstance(policy, DistributionGenerator)
            self.policy = EncDecMakeDeterministic(self.policy)
        self.features_keys = features_keys
        self.incl_state = incl_state
        self.sample = sample
        self.latent_size = latent_size
        if self.sample:
            assert self.latent_size is not None
        self.random_latent = random_latent
        self.episode_latent = None
        self.curr_vae = None
        self.window = window if window is not None else 1
        self.past_means = []
        self.past_logvars = []

        # use encoder to map to goals for prev vae
        self.prev_vae = prev_vae
        self.prev_incl_state = prev_incl_state

        self.goal_baseline = goal_baseline
        if self.goal_baseline:
            self.x_svr_estimator = LinearSVR(max_iter=5000)
            self.y_svr_estimator = LinearSVR(max_iter=5000)

    def get_action(self, obs):
        features = [obs[k] for k in self.features_keys]
        with th.no_grad():
            raw_obs = obs['raw_obs']
            encoder_obs = obs.get('encoder_obs', raw_obs)
            goal_set = obs.get('goal_set')

            if self.random_latent:
                pred_features = self.episode_latent.detach().cpu().numpy()
            elif self.goal_baseline:
                # baseline specific to valve env
                x_pred = self.x_svr_estimator.predict(np.concatenate(features)[None])[0]
                y_pred = self.y_svr_estimator.predict(np.concatenate(features)[None])[0]
                self.past_means.append([x_pred, y_pred])
                self.past_means = self.past_means[-self.window:]
                avg_pred = np.mean(self.past_means, axis=0)
                valve_pos = encoder_obs[-3:]
                valve_xy = np.delete(valve_pos, 1)
                avg_pred = avg_pred - valve_xy
                angle_pred = np.arctan2(avg_pred[1], -avg_pred[0])
                prev_encoder_inputs = [th.Tensor([np.sin(angle_pred), np.cos(angle_pred)]).to(ptu.device)]
                if self.prev_incl_state:
                    prev_encoder_inputs.append(th.Tensor(encoder_obs).to(ptu.device))
                pred_features, _ = self.prev_vae.encode(th.cat(prev_encoder_inputs))
                pred_features = pred_features.cpu().numpy()

            elif len(self.vaes):
                if self.incl_state:
                    features.append(encoder_obs)
                    if goal_set is not None:
                        features.append(goal_set.ravel())
                encoder_input = th.Tensor(np.concatenate(features)).to(ptu.device)
                mean, logvar = self.curr_vae.encode(encoder_input)
                self.past_means.append(mean)
                self.past_logvars.append(logvar)

                self.past_means = self.past_means[-self.window:]
                self.past_logvars = self.past_logvars[-self.window:]

                # use current encoder to map to latent
                if self.prev_vae is None:
                    mean, sigma_squared = self._product_of_gaussians(self.past_means, self.past_logvars)

                    if self.sample:
                        posterior = th.distributions.Normal(mean, th.sqrt(sigma_squared))
                        pred_features = posterior.rsample()
                    else:
                        pred_features = mean

                # use current encoder to map to goal for prev vae
                else:
                    prev_encoder_inputs = []
                    prev_encoder_inputs.append(th.mean(th.stack(self.past_means), dim=0))
                    if self.prev_incl_state:
                        prev_encoder_inputs.append(th.Tensor(encoder_obs).to(ptu.device))
                    pred_features, _ = self.prev_vae.encode(th.cat(prev_encoder_inputs))

                pred_features = pred_features.cpu().numpy()

            else:
                pred_features = np.concatenate(features)

            obs['latents'] = pred_features

            policy_input = [raw_obs, pred_features]
            if goal_set is not None:
                policy_input.insert(1, goal_set.ravel())
            action = self.policy.get_action(*policy_input)
            return action

    def reset(self):
        if self.random_latent:
            self.episode_latent = th.normal(ptu.zeros(self.latent_size), 1).to(ptu.device)
        self.policy.reset()
        if len(self.vaes):
            self.curr_vae = random.choice(self.vaes)
        self.past_means = []
        self.past_logvars = []

    def _product_of_gaussians(self, means, logvars):
        sigmas_squared = th.clamp(th.exp(th.stack(logvars)), min=1e-7)
        sigma_squared = 1. / th.sum(th.reciprocal(sigmas_squared), dim=0)
        mean = sigma_squared * th.sum(th.stack(means) / sigmas_squared, dim=0)
        return mean, sigma_squared

class EncDecMakeDeterministic(PyTorchModule):
    def __init__(
            self,
            policy,
    ):
        super().__init__()
        self.policy = policy

    def forward(self, *args, **kwargs):
        dist = self.policy.forward(*args, **kwargs)
        return Delta(dist.mle_estimate())

    def get_action(self, *obs_np):
        return self.policy.get_action(*obs_np)

    def get_actions(self, *obs_np):
        return self.policy.get_actions()

    def reset(self):
        self.policy.reset()


from battle_city.player import Player
from battle_city.bonus import Bonus
from battle_city.castle import *
from random import randrange
from battle_city.tile import Tile, BRICK, BETON
from battle_city.globals import DISPLAY

def get_game_map(number):
    game_map = []
    with open(f'maps/{number}.txt', 'r') as f:
        y = 0
        for line in f:
            line = line.strip()
            for x in range(len(line)):
                if line[x] != '.':
                    tile_type = int(line[x])
                    if tile_type == BRICK or tile_type == BETON:
                        for i in range(4):
                            game_map.append(Tile(tile_type,
                                                 (x * 16 + 8 * (i % 2),
                                                  y * 16 + 8 * (i // 2))))
                    else:
                        game_map.append(Tile(tile_type, (x * 16, y * 16)))
            y += 1
    return game_map

class Level:
    def __init__(self, number, count, game, players=None):
        self.game_map = get_game_map(number)
        self.number = number
        self.game = game
        self.volume_level = self.game.volume_level
        self.protecting_blocks = (
            (88, 200),
            (88, 208),
            (88, 216),
            (96, 200),
            (104, 200),
            (112, 200),
            (112, 208),
            (112, 216))
        for protecting_block in self.protecting_blocks:
            self.game_map.append(Tile(BRICK, protecting_block))
        if not players and count == 1:
            self.players = [Player(0, 0, 3 / 4, 0, (64, 208), self)]
        elif count == 1:
            self.players = players
            players[0].is_alive = True
            players[0].current_bullets = players[0].max_bullets
            players[0].level = self
        if not players and count == 2:
            self.players = [Player(0, 0, 3 / 4, 0, (64, 208), self), Player(1, 0, 3 / 4, 0, (128, 208), self)]
        elif count == 2:
            self.players = players
            for player in players:
                player.is_alive = True
                player.current_bullets = player.max_bullets
                player.level = self
        self.bullets = []
        self.enemies = []
        self.explosions = []
        self.bonuses = [Bonus(randrange(4),
                              (randrange(DISPLAY.get_width() - 16),
                               randrange(DISPLAY.get_height() - 15)), self),
                        Bonus(randrange(4),
                              (randrange(DISPLAY.get_width() - 16),
                              randrange(DISPLAY.get_height() - 15)), self)]
        self.castle = Castle(self)
        self.goal = 1

    def get_score(self):
        return sum((sum(player.score) for player in self.players))

    def kill_tile(self, tile):
        self.game_map.remove(tile)

    def kill_player(self, player):
        self.players.remove(player)


'''
In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.

Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.

If it cannot be done, return -1.

Example 1:

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
Example 2:

Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
Output: -1
Explanation:
In this case, it is not possible to rotate the dominoes to make one row of values equal.

Constraints:

2 <= tops.length <= 2 * 104
bottoms.length == tops.length
1 <= tops[i], bottoms[i] <= 6
'''
import unittest

from typing import *
from collections import OrderedDict

class Solution:
    def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
        store = [[0, 0, 0] for _ in range(7)]
        for i in range(len(tops)):
            if tops[i] != bottoms[i]:
                store[tops[i]][0] += 1
                store[bottoms[i]][1] += 1
            else:
                store[tops[i]][2] += 1

        res = float('inf')
        for i in range(1, 7):
            if store[i][0] + store[i][1] + store[i][2] == len(tops):
                swaps = min(store[i][0], store[i][1])
                res = min(swaps, res)
        return res if res != float('inf') else -1

class TestSolution(unittest.TestCase):

    def test_case(self):
        examples = (
            (([3,5,1,2,3], [3,6,3,3,4]), -1),
        )
        for first, second in examples:
            self.assert_function(first, second)

    def assert_function(self, first, second):
        self.assertEqual(Solution().minDominoRotations(*first), second,
                         msg="first: {}; second: {}".format(first, second))

unittest.main()

Leetcode/1007. Minimum Domino Rotations For Equal Row.py


"""Just for visualizing."""

import os

import imageio
import numpy as np
import torch
from tqdm import tqdm
from renders import NeRFRender

to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)

@torch.no_grad()
def render_video(nerf_render:NeRFRender, H, W, focal, K, poses, ndc: bool, use_viewdirs: bool, white_bkgd: bool, log_dir: str,
                 prefix: str, fps: int):

    rgbs = []
    disps = []
    depths = []
    for pose in tqdm(poses, desc="Render Video"):
        all_res = nerf_render.render_full_image(H,
                                                W,
                                                focal,
                                                K,
                                                pose,
                                                ndc=ndc,
                                                use_viewdirs=use_viewdirs,
                                                white_bkgd=white_bkgd)
        rgb = all_res.get("rgb_map", None)
        if rgb is not None:
            rgbs.append(rgb.detach().cpu().numpy())
        disp = all_res.get("disp_map", None)
        if disp is not None:
            disps.append(disp.detach().cpu().numpy())
        depth = all_res.get("depth_map", None)
        if depth is not None:
            depths.append(depth.detach().cpu().numpy())
    if len(rgbs) > 0:
        rgbs = np.stack(rgbs, 0)
        rgb_path = os.path.join(log_dir, f'{prefix}_rgb.mp4')
        imageio.mimwrite(rgb_path, to8b(rgbs), fps=fps, quality=8)
    if len(disps) > 0:
        disps = np.stack(disps, 0)
        disp_path = os.path.join(log_dir, f'{prefix}_disp.mp4')
        imageio.mimwrite(disp_path, to8b(disps / np.max(disps)), fps=fps, quality=8)
    if len(depths) > 0:
        depths = np.stack(depths, 0)
        depth_path = os.path.join(log_dir, f'{prefix}_depth.mp4')
        imageio.mimwrite(depth_path, to8b(depths / np.max(depths)), fps=fps, quality=8)
        print(f'Write video done: \n\t{rgb_path}\n\t{disp_path}\n\t{depth_path}')


# encoding=utf8

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import re
import argparse
import sys

######################### Box styles ##############################

def set_box_color(bp, options):
    colors = options["colors"]
    # "|||||||||", "\/\/\/\/\/\/", ""
    patterns = options["patterns"]
    double_counter = 0

    for i, box in enumerate(bp['boxes']):
        plt.setp(bp['boxes'][i], facecolor=colors[i],
                linewidth=0.5, hatch=patterns[i])

        plt.setp(bp['caps'][double_counter], color='black', linewidth=0.5)
        plt.setp(bp['caps'][double_counter + 1], color='black', linewidth=0.5)

        plt.setp(bp['whiskers'][double_counter],
                 color='black', linewidth=0.5, linestyle="--")
        plt.setp(bp['whiskers'][double_counter + 1],
                 color='black', linewidth=0.5, linestyle="--")

        plt.setp(bp['medians'][i], color='black')
        double_counter += 2

def plot(nums, axes, plot_positions, tick, options, line):

    stats = []
    micro_to_milli = 1000
    for num in nums:
        item = {}
        item["med"] = num[3]/micro_to_milli
        item["q1"] = num[2]/micro_to_milli
        item["q3"] = num[4]/micro_to_milli
        item["whislo"] = num[6]/micro_to_milli  # required
        item["whishi"] = num[1]/micro_to_milli  # required
        item["fliers"] = []  # required if showfliers=True
        stats.append(item)

    # print(plot_positions[0], plot_positions[1], stats)
    # axes.set_axis_off()
    box = axes.bxp(stats, positions=plot_positions,
                   widths=0.6, patch_artist=True)
    print(plot_positions)

    if line:
        axes.plot([tick], [stats[0]["med"]], marker='^',
                  markersize=0.2, linewidth=0.5, color="green")
        axes.plot([tick], [stats[1]["med"]], marker='v',
                  markersize=0.2, linewidth=0.5, color="green")
        axes.plot([tick, tick], [stats[0]["med"], stats[1]["med"]],
                  linewidth=0.3, color="green", linestyle="--")

    # print("{}     {}  {} {}".format(int(nums[0][0]), round(stats[0]["med"] / stats[3]["med"], 2), round(stats[1]["med"] / stats[3]["med"], 2), round(stats[2]["med"] / stats[3]["med"], 2)))
    # axes.text(tick, (item["med"] + item2["med"]) / 2, round(item["med"] / item2["med"], 2),
    #             fontsize=2.8, color="red", ha="center", va="center", fontweight="bold", backgroundcolor='white', bbox={'facecolor':'white', 'pad':0.01, 'edgecolor':'none'})

    set_box_color(box, options)

if __name__ == "__main__":

    font = {'family': 'Arial',
            'weight': 'normal',
            'size': 18}

    plt.rc('font', **font)

    def custom_list(s): return [int(item) for item in s.split(',')]
    def str_custom_list(s): return s.split(',')

    textFontSize = 9
    parser = argparse.ArgumentParser(
        description='Box and whisker plotting script for experiments.')
    parser.add_argument('--xlabel', default="No. of Procedures per second",
                        type=str, help='X-Axis label for the graph.')
    parser.add_argument('--ylabel', default="PCT (μs)",
                        type=str, help='Y-Axis label for the graph.')
    parser.add_argument('--regions', default=[], type=custom_list,
                        help='Regions after which you want to place delimiting line.')
    parser.add_argument('--legends', required=True, type=str_custom_list,
                        help='Legends for each boxplot comma separated')
    parser.add_argument('--colors', required=True, type=str_custom_list,
                        help='Colors for each boxplot comma separated')
    parser.add_argument('--patterns', required=True, type=str_custom_list,
                        help='Patterns for each boxplot comma separated')
    parser.add_argument('--output_image', default="image.png",
                        type=str, help='Path to output image.')
    parser.add_argument('--w_space', default=5, type=int,
                        help='Space to leave from the max value to the border, this value will be multipled by the max')
    parser.add_argument('-l', action='store_true', help="Flag which indicates whether to show improvement line or not.\
                                                        Default is false and does not work for more than two files.")

    parser.add_argument('paths', nargs='+',
                        help='Path to files.')

    args = parser.parse_args()

    total_files = len(args.paths)

    if args.l and total_files != 2:
        exit("-l option should only be used with two files.")

    options = {}
    options["colors"] = args.colors
    options["patterns"] = args.patterns
    options["legends"] = args.legends

    fig = plt.figure(figsize=(6, 2.5),  facecolor='w')
    axes = fig.add_subplot(111)
    mpl.rcParams['hatch.linewidth'] = 0.1

    ################################ Legends #########################################
    legends = []
    for i in range(total_files):
        legends.append(mpatches.Patch(linewidth=0.5, edgecolor='black',
                                    facecolor=options["colors"][i], alpha=0.6,
                                    hatch=options["patterns"][i], label=options["legends"][i]))

    lines = plt.legend(handles=legends, bbox_to_anchor=(0.18, 0.45, 0.80, 0.3),
                        loc="lower left", borderaxespad=0, ncol=1, fontsize=textFontSize)

    #################################################################################

    x_labels = []

    fds = [open(path, 'r') for path in args.paths]
    plot_positions = [i*0.85 + 0.5 for i in range(total_files)]

    plot_pos_mean = np.mean(plot_positions)
    x_ticks = [plot_pos_mean]
    max_v = 0

    j = 0
    for files in list(zip(*fds)):

        nums = []
        for f in files:
            nums.append(np.array(re.split(r"\s+", f.strip())).astype(np.float))

        plot(nums, axes, plot_positions, x_ticks[-1], options, args.l)

        for i in range(len(plot_positions)):
            plot_positions[i] += (total_files)

        x_ticks.append(x_ticks[-1] + total_files)

        if j == 0:
            x_labels.append("Normal")
        if j == 1:
            x_labels.append("T-Straggler")
        if j == 2:
            x_labels.append("P-Straggler")

        j += 1
        for num in nums:
            max_v = np.amax(np.array([max_v, num[6]]))

    axes.set_yscale('log')
    plt.grid(True, which="both", ls="-", linewidth=0.1)

    plt.xlim(0, 1)
    x_ticks[-1] -= (plot_pos_mean)
    x_ticks[-1] -= 0.75

    for xcoord in args.regions:
        plt.axvline(x=np.mean(np.array([x_ticks[xcoord - 1], x_ticks[xcoord]])), c='red', linewidth=0.5, linestyle="--")

    x_labels.append("")
    axes.set_xticks(x_ticks)
    axes.set_xticklabels(x_labels)
    axes.xaxis.set_tick_params(width=0.4)
    axes.yaxis.set_tick_params(width=0.4)
    axes.tick_params(labelsize=textFontSize)
    plt.xlabel(args.xlabel, fontsize=textFontSize)
    plt.ylabel(args.ylabel, fontsize=textFontSize)
    plt.ylim(0.1, 70000)

    plt.savefig(args.output_image, dpi=300, bbox_inches='tight', pad_inches = 0.05)


import sys
from PIL import Image
from random import randint
from datetime import datetime
import imageio.v2 as imageio

def timeFunc():
    """
    Donne la date et l'heure actuelles, formatées pour être utilisées comme nom de fichier.
    """
    return datetime.now().strftime("%Y-%m-%d-%H-%M-%S")

try: # Récupération des valeurs passées dans la commande. Si un argument est invalide une erreur est levée
    args = [int(sys.argv[i]) for i in (1,2,3,4)]

    assert sys.argv[5] == "0" or sys.argv[5] == "1"
    assert sys.argv[6] == "0" or sys.argv[6] == "1"

    args.append(bool(int(sys.argv[5])))
    args.append(bool(int(sys.argv[6])))
    args.append(int(sys.argv[7]))

    assert args[0] == 1 or args[0] == 2
except:
    raise ValueError("/!\ Paramètres incorrects.")

if args[0] == 1: # Si l'automate est en 1D
    size = (args[1], args[3]) # Largeur, hauteur

    if args[5]: # Si on utilise une génération initiale aléatoire
        gen = [randint(0, args[6]-1) for i in range(size[0])]
    else:
        with open("start.txt", 'r') as f:
            gen = f.readlines()

        if len(gen) > 1 or len(gen[0]) > size[0]: # Si le contenu du fichier est trop grand, une erreur est levée
            raise ValueError("/!\ Génération de départ invalide.")
        else:
            right = True
            gen = list(gen[0]) # str -> list[str]
            gen = [int(i) for i in gen] # str -> int
            while len(gen) < size[0]: # On complète avec des 0
                if right:
                    gen.append(0)
                else:
                    gen.insert(0,0)
                right = not right # Alternance gauche/droite

    im = Image.new(mode="RGB", size=size) # Création de l'image avec le module PIL

    with open("rule.txt", 'r') as f: # Récupération de la règle
        rule = f.read()

    for i in range(size[0]): # On imprime la première génération en haut de l'image
        color = (255//(args[6]-1)) * (-gen[i]+(args[6]-1))
        im.putpixel((i,0),(color,color,color))

    for turn in range(1, size[1]):
        newLine = [] # Génération
        for i in range(size[0]):
            pixel = eval(rule) # On évalue la règle avec les valeurs actuelles pour chaque cellule
            newLine.append(pixel)
            color = (255//(args[6]-1)) * (-pixel+(args[6]-1))
            im.putpixel((i,turn),(color,color,color))

    im.save(f"images/{timeFunc()}.jpg", "JPEG") # On enregistre l'image

else: # Si l'automate est en 2D
    size = (args[1], args[2])

    if args[5]: # Si on utilise une génération initiale aléatoire
        gen = [[randint(0, args[6]-1) for j in range(size[0])] for i in range(size[1])]
    else:
        with open("start.txt", 'r') as f:
            gen = f.readlines()

        for i in range(len(gen)): # Conversion en list[list[int]]
            if i == len(gen)-1:
                gen[i] = list(gen[i])
            else:
                gen[i] = list(gen[i][:-1])
            if len(gen[i]) > size[0]: # Si la génération donnée est trop grande on lève une erreur
                raise ValueError("/!\ Génération de départ invalide.")
        if len(gen) > size[1]:
            raise ValueError("/!\ Génération de départ invalide.")

        down = True
        gen = [[int(j) for j in i] for i in gen]
        while len(gen) < size[1]: # On complète avec des lignes de 0
            if down:
                gen.append([0 for i in range(size[0])])
            else:
                gen.insert(0, [0 for i in range(size[0])])
            down = not down # Alternance haut/bas
        for i in range(len(gen)):
            right = True
            while len(gen[i]) < size[0]: # On complète la ligne avec des 0
                if right:
                    gen[i].append(0)
                else:
                    gen[i].insert(0,0)
                right = not right # Alternance gauche/droite

    im = Image.new(mode="RGB", size=size) # Création de l'image avec le module PIL

    with open("rule2.txt", 'r') as f: # Récupération de la règle
        rule = f.read()

    for i in range(size[1]): # Première image, correspondant à la génération initiale
        for j in range(size[0]):
            color = (255//(args[6]-1)) * (-gen[i][j]+(args[6]-1))
            im.putpixel((j,i),(color,color,color))

    time = timeFunc() # Heure de début
    filenames = [f"images/{time}--0.jpg"]
    im.save(filenames[0], "JPEG") # On enregistre la première image

    for turn in range(1, args[3]):
        newGen = []
        for i in range(size[1]):
            newLine = []
            for j in range(size[0]):
                pixel = eval(rule) # Evaluation de la règle avec les valeurs actuelles pour chaque cellule
                newLine.append(pixel)
                color = (255//(args[6]-1)) * (-pixel+(args[6]-1))
                im.putpixel((j,i),(color,color,color))
            newGen.append(newLine)
        gen = newGen
        if args[4] or turn == args[3]-1: # Si on enregistre chaque génération ou si c'est la dernière génération
            filenames.append(f"images/{time}--{str(turn)}.jpg")
            im.save(filenames[turn], "JPEG")
    if args[4]: # Si on enregistre chaque génération
        images = []
        for filename in filenames:
            images.append(imageio.imread(filename))
        imageio.mimsave(f'images/{time}.gif', images) # On fait un gif

print(f"Terminé ! Fin de l'exécution : {timeFunc()}") # On indique l'heure de fin à l'utilisateur, pour qu'il puisse connaître le temps d'exécution


from arroyo import pluginlib

import asyncio
import collections
import functools
import re
import time
from datetime import datetime
from urllib import parse

from appkit import utils

class EliteTorrent(pluginlib.Provider):
    __extension_name__ = 'elitetorrent'

    DEFAULT_URI = 'http://www.elitetorrent.net/descargas/'
    SEARCH_URI = ('http://www.elitetorrent.net/resultados/{query}'
                  '/orden:fecha')
    URI_PATTERNS = [
        r'^http(s)?://([^.]+\.)?elitetorrent.net/'
    ]

    type_map = {
        'series': 'episode',
        'series vose': 'episode',
        'peliculas': 'movie',
        'peliculas microhd': 'movie',
        'peliculas hdrip': 'movie',
        'estrenos': 'movie'
    }

    DEFAULT_LANGUAGE = 'spa-es'
    SUPPORTED_LANGUAGES = [
        'spa-es',
        'lat-es'
    ]

    @staticmethod
    @functools.lru_cache(maxsize=8)
    def re_cache(re_str):
        return re.compile(re_str)

    def paginate(self, uri):
        if self.re_cache(r'/torrent/\d+/').search(uri):
            yield uri
            return

        parsed = parse.urlparse(uri)

        # elitetorrent passes thru extenal site to set cookies.
        # paginating that url leads to incorrect url and 404 errors.
        if not parsed.netloc.endswith('elitetorrent.net'):
            yield uri
            return

        # Split paths and params from parsed URI
        tmp = [x for x in parsed.path.split('/') if x]
        paths = []
        params = collections.OrderedDict({
            'orden': 'fecha',
            'pag': 1
        })

        for c in tmp:
            if ':' in c:
                k, v = c.split(':', 1)
                params[k] = v
            else:
                paths.append(c)

        pag_n = params.pop('pag')
        try:
            pag_n = int(pag_n)
        except ValueError:
            pag_n = 1

        while True:
            params['pag'] = pag_n
            pag_n += 1

            parsed = parsed._replace(
                path='/'.join(
                    paths +
                    ['{}:{}'.format(k, v) for (k, v) in params.items()]
                )
            )

            yield parse.urlunparse(parsed)

    def get_query_uri(self, query):
        # Only supported languages for elitetorrent
        if query.get('language', None) not in self.SUPPORTED_LANGUAGES:
            return

        # FIXME: Query.base_string should return something always
        # In special cases it should raise an Exception
        q = query.base_string
        if not q:
            return None

        # For episdes we replace some strings with the schema used by
        # elitetorrent community
        if query['type'] == 'episode':
            q = re.sub(r' S0*(\d+)$',
                       lambda m: ' Temporada ' + m.group(1),
                       query.base_string)
            q = re.sub(r' S0*(\d+)E(\d+)$',
                       lambda m: ' ' + m.group(1) + 'x' + m.group(2),
                       query.base_string)

        # elitetorrent only allows searches with 6 words
        words = [x for x in q.split(' ') if x]
        if len(words) > 6:
            diff = (len(words) - 6) // 2
            words = words[diff:diff+6]
        q = ' '.join(words)

        return self.SEARCH_URI.format(query=parse.quote_plus(q))

    @asyncio.coroutine
    def fetch(self, fetcher, uri):
        def _is_redirect(content):
            soup = self.parse_buffer(content)

            for meta in soup.select('meta'):
                attrs = {k.lower(): v.lower() for (k, v) in meta.attrs.items()}
                if attrs.get('http-equiv') == 'refresh':
                    dummy, location = attrs.get('content').split(';', 1)
                    loctype, url = location.split('=', 1)
                    if loctype.lower() == 'url':
                        return True

            return False

        resp, content = yield from fetcher.fetch_full(uri)

        if _is_redirect(content):
            # Get Cookies
            resp, content = yield from fetcher.fetch_full(
                uri, skip_cache=True)
            # Get URL with cookies
            resp, content = yield from fetcher.fetch_full(
                uri, skip_cache=True)

        return content

    def parse(self, buff):
        soup = self.parse_buffer(buff)
        info = soup.select_one('.info-tecnica')
        if info:
            return self.parse_detailed(soup)
        else:
            return self.parse_listing(soup)

    def parse_listing(self, soup):
        torrent_href_re = re.compile(r'(https?://(www.)?elitetorrent.net)?/torrent/\d+/')  # nopep8

        def parse_link(x):
            href = x.attrs.get('href', '')
            text = x.text

            if text == '' or href == '':
                return None

            if not torrent_href_re.search(href):
                return None

            parent = x
            while parent != soup and parent.name.lower() not in ('tr', 'li'):
                parent = parent.parent

            if parent == soup:
                return None

            if href[0] == '/':
                href = 'http://www.elitetorrent.net' + href

            return dict(node=parent, name=text, uri=href)

        # Filter and torrent links that point to this site.
        links = [parse_link(x) for x in soup.select('a')]
        cards = [x for x in links if x]

        ret = []
        for x in cards:
            typ, language = self.parse_type_and_language(
                x['node'].select_one('.categoria')
            )
            created = self.parse_uploaded(x['node'].select_one('.fecha'))

            ret.append(dict(
                name=x['name'],
                uri=x['uri'],
                type=typ,
                language=language,
                created=created
            ))

        return ret

    def parse_detailed(self, soup):
        card = soup  # .select_one('#principal')

        name = card.select_one('h2').text
        uri = soup.select_one('a[href^=magnet:?]').attrs['href']
        typ, language = self.parse_type_and_language(
            card.select_one('.info-tecnica').select('dd')[1]
        )
        created = self.parse_uploaded(
            card.select_one('.info-tecnica').select('dd')[0]
        )

        name = soup.select_one('#box-ficha h2').text

        seeds, leechers = self.parse_seeds_and_leechers(
            card.select_one('#torrent-info')
        )

        return [dict(
            name=name,
            uri=uri,
            created=created,
            type=typ,
            language=language,
            seeds=seeds,
            leechers=leechers
        )]

    def parse_type_and_language(self, node):

        if node is None:
            return None

        text = node.text.lower()

        return (
            self.type_map.get(text, None),
            self.DEFAULT_LANGUAGE if '(vose)' not in text else None
        )

    def parse_uploaded(self, node):
        ago_table = dict(
            seg=1,
            mi=60,
            h=60*60,
            d=60*60*24,
            sem=60*60*24*7,
            me=60*60*24*30,
            a=60*60*24*365
        )

        if node is None:
            return None

        text = node.text

        # In listings, dates are specified as "Hace 3 semanas"
        # (3 weeks ago)

        ago_re = r'Hace (un|\d+) (seg|mi|h|d|sem|me|a)'
        m = re.search(ago_re, text, re.IGNORECASE)
        if m:
            if m.group(2) not in ago_table:
                return None

            amount = 0
            if m.group(1) == 'un':
                amount = 1
            else:
                amount = int(m.group(1))

            return utils.now_timestamp() - amount * ago_table[m.group(2)]

        # In detail mode dates are more readable

        dmy_re = r'(\d+-\d+-\d+)'
        m = re.search(dmy_re, text, re.IGNORECASE)
        if m:
            tmp = datetime.strptime(m.group(1), '%d-%m-%Y')
            return int(time.mktime(tmp.timetuple()))

        # There are other formats like 'Hoy, 20:32'. It's simplier to just drop
        # it and go to defaults

        return None

    def parse_seeds_and_leechers(self, node):
        if node is None:
            return None

        text = node.text.lower()

        seeds_re = r'semillas\W+(\d+)'
        leechers_re = r'clientes\W+(\d+)'

        seeds_m = re.search(seeds_re, text)
        leechers_m = re.search(leechers_re, text)

        return (
            int(seeds_m.group(1)) if seeds_m else None,
            int(leechers_m.group(1)) if leechers_m else None,
        )

__arroyo_extensions__ = [
    EliteTorrent
]


from mctools import RCONClient  # Import the RCONClient

Host = 'localhost'
Port = 38016

rcon = RCONClient(Host, port=Port)

# Login to RCON:

if rcon.login('minecraft'):
    # Send command to RCON - broadcast message to all players:
    resp = rcon.command("say §eAll Done! Live long and Prosper")

rcon.stop()