import morse, keras
import numpy as np
from scipy import signal

channels = 1
samples_per_sec = 100
max_seconds = 5
max_samples = max_seconds * samples_per_sec
trans_seconds = 5
trans_samples = trans_seconds * samples_per_sec
latent_dim = 100
TOKENS = "$^0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
num_decoder_tokens = len(TOKENS)
target_token_index = dict([(char, i) for i, char in enumerate(TOKENS)])
max_translation_length = 62
use_lstm = True

# detection model
def make_model(input_shape = (max_samples,channels)):
    input_layer = keras.layers.Input(input_shape)
    nf = 64
    ks = 7

    conv1 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(input_layer)
    conv1 = keras.layers.BatchNormalization()(conv1)
    conv1 = keras.layers.MaxPooling1D()(conv1)
    conv1 = keras.layers.ReLU()(conv1)
    conv1 = keras.layers.Dropout(0.3)(conv1)

    conv2 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv1)
    conv2 = keras.layers.BatchNormalization()(conv2)
    conv2 = keras.layers.MaxPooling1D()(conv2)
    conv2 = keras.layers.ReLU()(conv2)
    conv2 = keras.layers.Dropout(0.2)(conv2)

    conv3 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv2)
    conv3 = keras.layers.BatchNormalization()(conv3)
    conv3 = keras.layers.MaxPooling1D()(conv3)
    conv3 = keras.layers.ReLU()(conv3)
    conv3 = keras.layers.Dropout(0.1)(conv3)

    conv4 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv3)
    conv4 = keras.layers.BatchNormalization()(conv4)
    conv4 = keras.layers.MaxPooling1D()(conv4)
    conv4 = keras.layers.ReLU()(conv4)
    conv4 = keras.layers.Dropout(0.1)(conv4)

    conv5 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv4)
    conv5 = keras.layers.BatchNormalization()(conv5)
    conv5 = keras.layers.MaxPooling1D()(conv5)
    conv5 = keras.layers.ReLU()(conv5)

    gap = keras.layers.GlobalAveragePooling1D()(conv5)

    output_layer = keras.layers.Dense(1, activation="sigmoid")(gap)

    return keras.models.Model(inputs=input_layer, outputs=output_layer)

# translation model
# https://keras.io/examples/nlp/lstm_seq2seq/
def make_trans_model(input_shape = (trans_samples,channels)):
    input_layer = keras.layers.Input(input_shape)
    nf = 96
    ks = 7

    conv1 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(input_layer)
    conv1 = keras.layers.BatchNormalization()(conv1)
    conv1 = keras.layers.AveragePooling1D()(conv1)
    conv1 = keras.layers.ReLU()(conv1)
    conv1 = keras.layers.Dropout(0.6)(conv1)

    conv2 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv1)
    conv2 = keras.layers.BatchNormalization()(conv2)
    conv2 = keras.layers.AveragePooling1D()(conv2)
    conv2 = keras.layers.ReLU()(conv2)
    conv2 = keras.layers.Dropout(0.4)(conv2)

    conv3 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv2)
    conv3 = keras.layers.BatchNormalization()(conv3)
    conv3 = keras.layers.AveragePooling1D()(conv3)
    conv3 = keras.layers.ReLU()(conv3)
    conv3 = keras.layers.Dropout(0.2)(conv3)

    if use_lstm:
        conv8 = keras.layers.LSTM(nf*2, return_sequences=True)(conv3)
    else:
        conv7 = keras.layers.Conv1D(filters=nf, kernel_size=ks, activation="relu", padding="same")(conv3)
        conv8 = keras.layers.TimeDistributed(keras.layers.Dense(nf*2, activation="relu"))(conv7)

    concat = keras.layers.Concatenate(axis=2)([conv3, conv8])
    dense = keras.layers.TimeDistributed(keras.layers.Dense(num_decoder_tokens, activation="softmax"))(concat)
    return keras.models.Model(inputs=input_layer, outputs=dense)

    #encoder_lstm = keras.layers.LSTM(latent_dim, return_sequences=True, dropout=0.1)(conv5)
    #decoder_lstm = keras.layers.LSTM(num_decoder_tokens, dropout=0.1, return_sequences=True)(encoder_lstm)
    #return keras.models.Model(inputs=input_layer, outputs=decoder_lstm)

    #encoder_inputs = conv5
    #encoder = keras.layers.LSTM(latent_dim, return_state=True, dropout=0.2)
    #encoder_outputs, state_h, state_c = encoder(encoder_inputs)
    # We discard `encoder_outputs` and only keep the states.
    #encoder_states = [state_h, state_c]
    # Set up the decoder, using `encoder_states` as initial state.
    #decoder_inputs = keras.Input(shape=(None, num_decoder_tokens))
    # We set up our decoder to return full output sequences,
    # and to return internal states as well. We don't use the
    # return states in the training model, but we will use them in inference.
    #decoder_lstm = keras.layers.LSTM(latent_dim, return_sequences=True, return_state=True)
    #decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
    #decoder_dense = keras.layers.Dense(num_decoder_tokens, activation="softmax")
    #decoder_outputs = decoder_dense(decoder_outputs)
    #return keras.models.Model(inputs=[input_layer, decoder_inputs], outputs=decoder_outputs)

class DataGenerator(keras.utils.Sequence):
    'Generates detection data for Keras'
    def __init__(self, batch_size=256):
        'Initialization'
        self.dim = (batch_size,max_samples,channels)
        self.batch_size = batch_size
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return 1000

    def __getitem__(self, index):
        'Generate one batch of data'
        x_train = []
        y_train = []
        for i in range(0,self.batch_size):
            msg, x, posns = morse.generate_detection_training_sample(max_samples)
            x = np.reshape(x, (-1,1))
            y = len(msg) > 0 and msg[0] != '~'
            x_train.append(x)
            y_train.append(y)
        return np.array(x_train), np.array(y_train)

    def on_epoch_end(self):
        'Updates indexes after each epoch'

class TranslationGenerator(keras.utils.Sequence):
    'Generates detection data for Keras'
    def __init__(self, batch_size=256):
        'Initialization'
        self.dim = (batch_size,trans_samples,channels)
        self.batch_size = batch_size
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return 1000

    def __getitem__(self, index):
        'Generate one batch of data'
        x_train = []
        y_train = []
        for i in range(0, self.batch_size):
            msg, x, posns = morse.generate_translation_training_sample(trans_samples)
            assert(len(posns) == len(msg)+1)
            x = np.reshape(x, (-1,1))
            y = np.zeros((max_translation_length, num_decoder_tokens))
            str = ['.'] * max_translation_length
            # iterate over all bins
            for i, char in enumerate(msg):
                if use_lstm:
                    # lstm, bin goes at end of symbol
                    pos = posns[i+1] / trans_samples * max_translation_length
                    ofs = int(round(pos))+1
                else:
                    # put bin smack dab in middle of the feature
                    pos = ((posns[i] + posns[i+1]) / trans_samples / 2) * max_translation_length
                    ofs = int(round(pos))
                # is this symbol in the window?
                if ofs > 0 and ofs < max_translation_length-1 and posns[i] > 0 and posns[i+1] < trans_samples:
                    tti = target_token_index[msg[i]]
                    #y[ofs-1, tti] = 1/3
                    y[ofs+0, tti] = 1/1
                    #y[ofs+1, tti] = 1/3
                    str[ofs] = char
            # set "no symbol" probability for bins
            for ofs in range(0,max_translation_length):
                y[ofs, 0] = max(0.0, 1.0 - np.sum(y[ofs]))
            x_train.append(x)
            y_train.append(y)
            #print(''.join(str))
        return np.array(x_train), np.array(y_train)

    def on_epoch_end(self):
        'Updates indexes after each epoch'

class CWDetectorTranslator:
    def __init__(self, sample_rate, overlap=0.5, wndsizes=[512,256,128]):
        self.sr = sample_rate
        self.overlap = overlap
        self.wndsizes = wndsizes
        self.nsamples = int((128 * (1-overlap) + 1) * max_samples / 2)
        self.wnd = np.zeros((self.nsamples * 8,))
        self.detections = []

        detect_checkpoint_fn = "weights_detect.h5"
        self.detect_model = make_model()
        self.detect_model.load_weights(detect_checkpoint_fn)

        trans_checkpoint_fn = "weights_translate.h5"
        self.trans_model = make_trans_model()
        self.trans_model.load_weights(trans_checkpoint_fn)

    def clear(self):
        self.wnd[:] = 0

    def add_samples(self, samples):
        # shift window by 1/2
        n = len(samples)
        self.wnd[0:-n] = self.wnd[n:]
        # add new samples
        self.wnd[-n:] = samples
        # convert to spectrogram at three different scales
        specs = []
        for nps in self.wndsizes:
            nov = int(nps * self.overlap)
            wndsamp = max_samples * (nps-nov+1)
            w = self.wnd[-wndsamp:]
            frequencies, times, spectrogram = signal.spectrogram(w, fs=self.sr, nperseg=nps, noverlap=nov)
            specs.append(spectrogram[:, 0:max_samples])
        self.spec = np.concatenate(specs, axis=0)

    def detect(self):
        xy = self.spec #[:, 0:max_samples]
        # normalize spectrogram
        ymin = np.min(xy, axis=1)
        ymax = np.max(xy, axis=1)
        xy = (xy - ymin[:,None]) / (ymax - ymin + 1e-6)[:,None]
        #xy = (xy - np.min(xy)) / (np.max(xy) - np.min(xy) + 1e-6)
        self.xy = xy
        xy = np.reshape(xy, (xy.shape[0], xy.shape[1], 1))
        p = self.detect_model.predict(xy[:, 0:max_samples])
        self.detections = np.argwhere(p > 0.5)
        # combine adjacent bins
        for i in range(0, len(self.detections)-1):
            y = self.detections[i][0]
            if y == self.detections[i+1][0] - 1:
                #self.xy[y+1] = np.maximum(self.xy[y], self.xy[y+1])
                self.xy[y+1] = (self.xy[y] + self.xy[y+1]) / 2
                self.detections[i] = (-1,-1)

    def translate(self):
        results = []
        for y,i in self.detections:
            if y >= 0:
                row = self.xy[y]
                row = np.reshape(row, (1, row.shape[0], 1))
                t = self.trans_model.predict(row)[0]
                results.append((y, bins2msg(t)))
        return results

def bins2msg(t):
    nbins = t.shape[0]
    # pick the best choice for each bin
    msg = ['.'] * nbins
    for j in range(0, nbins):
        k = np.argmax(t[j])
        if k>0:
            msg[j] = TOKENS[k]
    return ''.join(msg)


import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from typing import List

engine = create_engine('sqlite:///../data/data.db', echo=False)
con = engine.connect()
df = pd.read_sql('select * from patient', con=con)
con.close()

def detect_duplicates(df:pd.DataFrame) -> pd.DataFrame:
    # remove the patients with duplicated id
    df = df[~df.patient_id.duplicated()]
    # remove the patients with null names
    df['name'] = df['given_name'] + ' ' + df['surname']
    df = df[~df.name.isnull()]

    # Using phone number to detect duplicates
    # first remove the rows with phone numbers being None
    df = df.dropna(subset=["phone_number"])
    # then remove patient ids corresponding to duplicated phone numbers
    df = df[~df.patient_id.isin(set(df[df.phone_number.duplicated()].patient_id.values))]

    def rm_gb(cols:List[str], data:pd.DataFrame) -> pd.DataFrame:
        '''groups by columns and removes rows where groupby count resulted in more than 1 example

        if there are more than 1 occurences of "name" == "Gerard" and "state"=="nsw" then
        rm_gb(['name', 'state']) => should remove it
        '''
        gb_cols = data.groupby(cols).count()
        gb_cols = gb_cols[gb_cols.patient_id > 1].reset_index()
        dup = data[data[cols[0]].isin(gb_cols[cols[0]])]
        for col in cols[1:]:
            dup = dup[df[col].isin(gb_cols[col])]
        data = data[~data.patient_id.isin(dup[dup.name.duplicated()].patient_id.values)]
        return data

    df = rm_gb(['name', 'postcode'], df)
    df = rm_gb(['name', 'address_1'], df)
    df = rm_gb(['name', 'suburb'], df)
    return df

df = detect_duplicates(df)


# third party
import torch
import torch.nn as nn

class IMDBDataset:
    def __init__(self, reviews, targets):
        """
        Argument:
        reviews: a numpy array
        targets: a vector array
        """
        self.reviews = reviews
        self.target = targets

    def __len__(self):
        # return length of dataset
        return len(self.reviews)

    def __getitem__(self, index):
        # return review and target of that index in torch tensor
        review = torch.tensor(self.reviews[index, :], dtype=torch.long)
        target = torch.tensor(self.target[index], dtype=torch.float)

        return {"review": review, "target": target}

class LSTM(nn.Module):
    def __init__(self, embedding_matrix):

        super(LSTM, self).__init__()

        # Number of words = number of rows in embedding matrix
        num_words = embedding_matrix.shape[0]
        embedding_dim = embedding_matrix.shape[1]

        # input embedding layer
        self.embedding = nn.Embedding(
            num_embeddings=num_words,
            embedding_dim=embedding_dim,
        )

        self.embedding.weight = nn.Parameter(
            torch.tensor(embedding_matrix, dtype=torch.float32),
        )

        # requires grad is false since we use pre-trained word embeddings
        self.embedding.weight.requires_grad = False

        # instantiate LSTM
        self.lstm = nn.LSTM(
            embedding_dim,
            128,
            bidirectional=True,
            batch_first=True,
        )
        self.out = nn.Linear(512, 2)

    def forward(self, x):
        # embed
        x = self.embedding(x)

        # pass embedding to lstm
        hidden, _ = self.lstm(x)

        # apply mean and max pooling on lstm output
        avg_pool = torch.mean(hidden, 1)
        max_pool, index_max_pool = torch.max(hidden, 1)

        out = torch.cat((avg_pool, max_pool), 1)

        # final pred
        out = self.out(out)
        return out


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

# # Integración con Python
#
# Esta sección pretende ser un compendio (esperemos que claro y ordenado) de todo el `Python`
# que hemos ido usando en el Capítulo 4.
#
# Esta sección también puede servir como repaso de los conceptos más aplicados que hemos introducido en dicho capítulo.

# ## Integrales iteradas con `Sympy`
#
# Mostramos, sobre un ejemplo, el cálculo de integrales iteradas con la ayuda de `Sympy`.
#
# En este caso vamos a calcular la integral
#
# $$
# \int_{1}^{2}\left[\int_{1}^{x} (2xy + 3y^2)dy \right] dx.
# $$
# Lo haremos de dos maneras diferentes:
#
# 1. integrando primero respecto a $y$ y luego respecto a $x$,
# 2. integrando primero respecto a $x$ y luego respecto a $y$.

# In[2]:

import sympy as sp

x, y = sp.symbols('x y', real=True) # define as variables simbólicas x, y

f_expr = 2*x*y + 3*y**2
f = sp.Lambda((x,y), f_expr)

res1 = sp.integrate(f_expr, (y, 1, x), (x, 1, 2))
display(res1)
res2 = sp.integrate(f(x,y), (y, 1, x), (x, 1, 2))
display(res2)

# ## Cálculo del área de una superficie utilizando ' Sympy'
#
# En este caso, debemos integrar la función $1$ en la región de que se trate.
#
# Como ejemplo, vamos a calcular la superficie de la región que mostramos a continuación:
#
#

# In[3]:

import sympy as sp

x, y = sp.symbols('x y', real=True) # define as variables simbólicas x, y

# res = sp.integrate(1, (y, sp.cos(x), sp.sin(x)), (x, 1, 2))
res = sp.integrate(1, (y, 0, sp.sin(x)), (x, 1, 2))
display(res)
display(sp.N(res))

# ## Cálculo de volúmenes con ' Sympy'
#
# Para calcular el volumen entre el plano $XY$ y el gráfico de una función positiva, debemos integrar esa función en el dominio correspondiente.
#
# Como ejemplo, vamos a calcular (de dos maneras distintas, según el orden elegido para las variables) el volumen de la región  sólida delimitada por el plano $z=f(x,y)=2–x–2y$ y los tres planos de coordenadas
#
#
#

# In[4]:

import sympy as sp
x, y = sp.symbols('x y', real=True)
f = sp.Lambda((x,y),2-x-2*y)
volumen = sp.integrate(f(x,y), (y, 0, (2-x)/2), (x, 0, 2))
display(volumen)
volumen2 = sp.integrate(f(x,y), (x, 0, 2-2*y), (y, 0, 1))
display(volumen2)

# ## Cálculo del valor medio con ' Sympy'
#
# Si queremos calcular el valor medio de una función de dos variables en una región plana, $R$, debemos aplicar la fórmula
#
# $$
# \textrm{Valor medio}=\dfrac{1}{\textrm{Área}\,(R)}\int_R\int f(x,y)\,dA.
# $$
#
# Calculamos a continuación el valor medio de $f(x,y)=\dfrac{1}{2}xy$ sobre la región del plano $R$ dada por un rectángulo con vértices $(0,0)$, $(4,0)$, $(4,3)$ y $(0,3)$. Para ello, observamos que el área de la región rectangular $R$ es $4\times 3=12$, y que los límites para $x$ e $y$ son $0\leq x\leq 4$ e $0\leq y\leq 3$, como se muestra en la figura
#
#

# In[5]:

import sympy as sp
x, y = sp.symbols('x y', real=True)
f = sp.Lambda((x,y),1/12*1/2*x*y)
val_m = sp.integrate(f(x,y), (y, 0, 3), (x, 0, 4))
display(val_m)

# ## Integración con cambio de variable en `Sympy`
#
# Para realizar un cambio de variable debemos aplicar la fórmula
#
# $$
# \displaystyle \int_{R} \int G(x,y) \, dx \, dy = \int_{S} \int (G \circ \mathbf{F})(u,v) |\det \mathrm{J}\mathbf{F}(u,v)| \, du \, dv.
# $$
#
# Como ejemplo, vamos a integrar $\displaystyle \int_R \int 9xy \, dA$, siendo $R$ la región que mostramos en la siguiente figura
#
#
#
# Consideramos el siguiente cambio de variable:
#
# $$
# \left\{\begin{array}{lcl}
# u &=& x + y \\
# v &=& x - 2y
# \end{array}\right.
# $$

# In[6]:

import sympy as sp

x, y, u, v = sp.symbols('x y u v', real=True) # define las variables simbólicas x, y, u, v

# Definimos las funciones F y G como matrices
F = sp.Matrix([ 1/3*(2*u+v), 1/3*(u-v) ])
G = sp.Matrix([ 9*x*y ])

# Definimos la nueva función a integrar
GoF_expr = G.subs(x,1/3*(2*u+v)).subs(y,1/3*(u-v))

# Calculamos el determinante de la matriz jacobiana asociada a F
det_jac_F = sp.det( F.jacobian([u,v]) )

# Calculamos la integral sobre la nueva región de integración
res = sp.integrate(GoF_expr*abs(det_jac_F), (v, -4, 0), (u, 1, 4))
display('Valor de la integral: ')
display(sp.simplify(res[0]))

# ## Integración con cambio a polares con ' Sympy'
#
# Como caso particular de especial interés, al cambiar de coordenadas cartesianas a polares, la fórmula anterior resulta
#
# $$
# \int_{R} \int G(x,y) \, dA = \int_{\alpha}^{\beta} \int_{g_1(\theta)}^{g_2(\theta)} G(r\cos(\theta),r\sin(\theta)) r \, dr \, d\theta.
# $$
#
# Vamos a aplicarlo a un ejemplo concreto, en el que calcularemos la integral $\displaystyle \int_R \int (x^2 + y) \, dA$, siendo
# $R$ la región anular mostrada en la siguiente figura:
#
#

# In[7]:

import sympy as sp

x, y, r, th = sp.symbols('x y r th', real=True) # define las variables simbólicas x, y, r, th

# Definimos las funciones F y G como matrices
F = sp.Matrix([ r*sp.cos(th), r*sp.sin(th) ])
G = sp.Matrix([ x**2 + y ])

# Definimos la nueva función a integrar
GoF_expr = G.subs(x,r*sp.cos(th)).subs(y,r*sp.sin(th))

# Calculamos el determinante de la matriz jacobiana asociada a F
det_jac_F = sp.det( F.jacobian([r,th]) )

# Calculamos la integral sobre la nueva región de integración
res = sp.integrate(GoF_expr*abs(det_jac_F), (r, 1, sp.sqrt(5)), (th, 0, 2*sp.pi))
display('Valor de la integral: ')
display(sp.simplify(res[0]))


import telebot
from telebot import types
import sqlite3

bot = telebot.TeleBot('1835870307:AAHlXuytmI_rtPbjNLj3PzBU3oaeGe7yboY')

# Получение списка администраторов
def get_administrators():
    list = []
    for i in get_db_connection().execute('SELECT * FROM administrators').fetchall():
        list.append(i[1])
    return list

def get_db_connection():
    conn = sqlite3.connect('bot_db')
    conn.row_factory = sqlite3.Row
    return conn

def get_welcome_message():
    information_about_company = get_db_connection().execute('SELECT * FROM welcome')
    text = ''
    for i in information_about_company:
        text = ''.join(i)
    return text

def get_start_message():
    information_about_company = get_db_connection().execute('SELECT * FROM start_message')

    for i in information_about_company:
        return i[0]

def send_new_admin(admin):
    conn = sqlite3.connect('bot_db')
    conn.row_factory = sqlite3.Row

    conn.execute('INSERT INTO administrators(admin_id) VALUES  (?) ', (admin, )).fetchall()
    conn.commit()
    conn.close()

def edit_start_message(message):
    try:
        chat_id = message.chat.id
        text = message.text
        msg = bot.send_message(chat_id, 'Напишите новый текст стартового сообщения')
        bot.register_next_step_handler(msg, edit_start_message_next_step)
    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def edit_start_message_next_step(message):
    try:
        conn = sqlite3.connect('bot_db')
        chat_id = message.chat.id
        text = message.text
        conn.execute('UPDATE start_message SET title = (?) WHERE ROWID=1', (text, ))
        conn.commit()
        bot.send_message(chat_id, 'Стартовое сообщение было успешно изменено')
        conn.close()
    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def edit_information_company(message):
    try:
        chat_id = message.chat.id
        bot.send_message(chat_id, get_welcome_message())
        msg = bot.send_message(chat_id, 'Напишите новый текст приветствия')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, edit_information_company_next_step)

    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def edit_information_company_next_step(message):
    try:
        chat_id = message.chat.id
        text = message.text
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE welcome SET title=?', (text,))
        conn.commit()
        conn.close()
        bot.send_message(chat_id, 'Сообщение приветствия было успешно изменено')
    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def show_last_post():
    list = []
    for i in get_db_connection().execute('SELECT * FROM bot ORDER BY ID DESC LIMIT 1').fetchall():
        for j in range(len(i)):
            list.append(i[j])
    text = f'Название вакансии: {list[1]}\n\n' \
        f'Описание вакансии: {list[2]}\n\n' \
        f'Локализация: {list[3]}\n\n' \
        f'Почасовая ставка: {list[4]}'

    return text

# Редактирование вакансии

id = int

def edit_offer(message):
    try:
        chat_id = message.chat.id
        msg = bot.send_message(chat_id, 'Напиши ID поста для редактирования')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, edit_offer_next_step)
    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def edit_offer_next_step(message):
    try:
        global id
        chat_id = message.chat.id
        id = message.text
        bot.send_message(chat_id, show_offer(id))
        if message.text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        msg = bot.send_message(chat_id, 'Напиши новое название вакансии:')
        bot.register_next_step_handler(msg, edit_offer_title)
    except:
        bot.send_message(chat_id, 'Какая-то ошибка')

def edit_offer_title(message):
    try:
        global id
        chat_id = message.chat.id
        title = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if title.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET title=(?) WHERE id=(?)', (title, id))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Теперь введите описание')
        bot.register_next_step_handler(msg, edit_offer_description)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def edit_offer_description(message):
    try:
        global id
        chat_id = message.chat.id
        text = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET description=(?) WHERE id=(?)', (text, id))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Теперь введите местоположение работы')
        bot.register_next_step_handler(msg, edit_offer_location)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def edit_offer_location(message):
    try:
        global id
        chat_id = message.chat.id
        text = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET location=(?) WHERE id=(?)', (text, id))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Теперь введите почасову оплату')
        bot.register_next_step_handler(msg, edit_offer_salary)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def edit_offer_salary(message):
    try:
        global id
        chat_id = message.chat.id
        text = message.text
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET salary=(?) WHERE id=(?)', (text, id))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Вакансия была успешно отредактирована')
        id = int
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

# Удаление вакансии
def remove_offer(message):
    try:
        chat_id = message.chat.id
        name = message.text
        msg = bot.reply_to(message, 'Введите ID вакансии, которую хотите удалить')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, remove_offer_next_step)
    except:
        bot.reply_to(message, 'Что-то пошло не так')

def remove_offer_next_step(message):
    text = message.text
    if text.lower() == 'отмена':
        bot.send_message(message.chat.id, 'Действие отменено')
        return
    if not text.isdigit():
        msg = bot.reply_to(message, 'Ошибка. ИД включает в себя только номерные знаки.')
        bot.register_next_step_handler(msg, remove_offer_next_step)
        return
    try:
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('DELETE FROM bot WHERE id = ?', (int(text),))
        conn.commit()
        conn.close()
        msg = bot.reply_to(message, 'Вакансия была успешно удалена', reply_markup=admin_keyboard())
    except:
        msg = bot.reply_to(message, 'Какая-то ошибка')
# конец удаления вакансии

# Добавление новой вакансии
def add_new_offer(message):
    try:
        chat_id = message.chat.id
        name = message.text
        msg = bot.send_message(chat_id, 'Напишите название новой вакансии: ')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, add_title)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def add_title(message):
    try:
        chat_id = message.chat.id
        title = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if title.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('INSERT INTO bot(title) VALUES (?)', (title,))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Теперь введите описание')
        bot.register_next_step_handler(msg, add_description)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def add_description(message):
    try:
        chat_id = message.chat.id
        description = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if description.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET description=(?) where id = (select MAX(id) from bot)', (description,))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Введите местоположение работы')
        bot.register_next_step_handler(msg, add_location)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def add_location(message):
    try:
        chat_id = message.chat.id
        location = message.text
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        if location.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET location=(?) where id = (select MAX(id) from bot)', (location,))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Напишите почасовую ставку для данной вакансии')
        bot.register_next_step_handler(msg, add_salary)
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def add_salary(message):
    try:
        chat_id = message.chat.id
        salary = message.text
        if salary.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        conn.execute('UPDATE bot SET salary=(?) where id = (select MAX(id) from bot)', (salary,))
        conn.commit()
        conn.close()
        msg = bot.send_message(chat_id, 'Вакансия была успешно добавлена')
        bot.reply_to(msg, show_last_post())
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

# Получение вакансии
def get_offer(id):
    conn = get_db_connection()
    post = conn.execute('SELECT * FROM bot WHERE id = ?',
                        (id,)).fetchone()
    conn.close()
    if post is None:
        return 'Nie ma takiej oferty'
    return post

def show_offer(id):
    try:
        list = []
        for i in get_db_connection().execute('SELECT * FROM bot WHERE id = ?', (int(id),)).fetchall():
            for j in range(len(i)):
                list.append(i[j])
                list.append('\n')
        text = ''
        text = ''.join(list[1::])
        return text
    except:
        return

# Добавление нового администратора

def add_admin(message):
    try:
        chat_id = message.chat.id
        name = message.text
        msg = bot.reply_to(message, 'Напишите ID аккаунта, который хотите добавить в администраторы: ')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, process_age_step)
    except Exception as e:
        bot.reply_to(message, 'Что-то пошло не так')

def process_age_step(message):
    try:
        chat_id = message.chat.id
        text = message.text
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        if not text.isdigit():
            msg = bot.reply_to(message, 'Ошибка. ИД включает в себя только номерные знаки.')
            bot.register_next_step_handler(msg, process_age_step)
            return
        try:
            conn = sqlite3.connect('bot_db')
            conn.row_factory = sqlite3.Row
            conn.execute('INSERT INTO administrators(admin_id) VALUES (?)', (int(text),)).fetchall()
            conn.commit()
            conn.close()
            msg = bot.reply_to(message, 'Администратор был успешно добавлен', reply_markup=admin_keyboard())
        except:
            msg = bot.reply_to(message, 'Какая-то ошибка')
    except:
        bot.reply_to(message, 'oooops')
# конец добавлние администратора

# Удаление администратора

def remove_admin(message):
    try:
        chat_id = message.chat.id
        name = message.text
        bot.send_message(message.chat.id, str(get_administrators()))
        msg = bot.reply_to(message, 'Напишите ID аккаунта, который хотите удалить из администраторов: ')
        bot.send_message(chat_id, 'Для отмены напиши \"Отмена\" без кавычек')
        bot.register_next_step_handler(msg, remove_admin_next_step)
    except Exception as e:
        bot.reply_to(message, 'Что-то пошло не так %s' % (e))

def remove_admin_next_step(message):
    try:
        chat_id = message.chat.id
        text = message.text
        if text.lower() == 'отмена':
            bot.send_message(chat_id, 'Действие отменено')
            return
        if not text.isdigit():
            msg = bot.reply_to(message, 'Ошибка. ИД включает в себя только номерные знаки.')
            bot.register_next_step_handler(msg, process_age_step)
            return
        try:
            conn = sqlite3.connect('bot_db')
            conn.row_factory = sqlite3.Row
            conn.execute('DELETE FROM administrators WHERE admin_id=?', (int(text),)).fetchall()
            conn.commit()
            conn.close()
            msg = bot.reply_to(message, 'Администратор был успешно удален', reply_markup=admin_keyboard())
        except:
            msg = bot.reply_to(message, 'Какая-то ошибка')
    except:
        bot.reply_to(message, 'ooooops')

# Конец удаление администраторе
def show_contact_list(message):
    try:
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        chat_id = message.chat.id
        list = []
        text = ''
        for i in conn.execute('SELECT * FROM contact'):
            for j in i:
                list.append(str(j) + ' ')
            list.append('\n')
        text = text.join(list)
        bot.send_message(chat_id, text)
        conn.close()
    except:
        bot.send_message(message.chat.id, 'Что-то пошло не так')

def contact(message):
    try:
        conn = sqlite3.connect('bot_db')
        conn.row_factory = sqlite3.Row
        chat_id = message.chat.id
        text = message.text
        bot.send_message(get_administrators()[0], 'Поступила новая заявка: \n' + text)
        conn.execute('INSERT INTO contact(text) VALUES (?)', (text,))
        conn.commit()
        conn.close()
        bot.send_message(chat_id, 'Ваша заявка была успешно отправлена')
    except:
        bot.send_message(chat_id, 'Что-то пошло не так')

def start_keyboard():
    keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    about = types.KeyboardButton(text='Информация о фирме')
    offers = types.KeyboardButton(text='Предложения по работе')
    contact = types.KeyboardButton(text='Сконтактироваться')
    keyboard.add(about, offers, contact)
    return keyboard

def admin_keyboard():
    keyboard = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
    add_offer = types.KeyboardButton(text='Добавить новую вакансию')
    delete_offer = types.KeyboardButton(text='Удалить вакансию')
    add_admin = types.KeyboardButton(text='Добавить администратора')
    delete_admin = types.KeyboardButton(text='Удалить администратора')
    edit_offer = types.KeyboardButton(text='Редактировать вакансию')
    list_admins = types.KeyboardButton(text='Список администраторов')
    edit_info_about_company = types.KeyboardButton(text='Редактировать информацию о фирме')
    show_contact_list = types.KeyboardButton(text='Показать список заявок')
    edit_start_message = types.KeyboardButton(text='Редактировать стартовое сообщение')
    keyboard.add(add_offer, edit_offer, delete_offer, add_admin, delete_admin, list_admins, edit_info_about_company,
                 show_contact_list, edit_start_message)
    return keyboard

@bot.message_handler(commands=["start"])
def send_welcome(message):
    bot.send_chat_action(message.chat.id, 'upload_photo')
    bot.send_message(message.chat.id, get_start_message() , reply_markup=start_keyboard())

@bot.message_handler(commands=["adminmenu"])
def adminmenu(message):
    if message.from_user.id in get_administrators():
        bot.send_message(message.from_user.id, text='Добро пожаловать в меню администратора',
                         reply_markup=admin_keyboard())
    else:
        bot.send_message(message.from_user.id, text='У тебя нет прав для входа в это меню')

@bot.message_handler(content_types=['text'])
def get_text_message(message):
    if message.text == 'Предложения по работе':
        keyboard = types.InlineKeyboardMarkup()
        for i in get_db_connection().execute('SELECT * FROM bot').fetchall():
            keyboard.add(types.InlineKeyboardButton(text=i[1], callback_data=i[0]))
        bot.send_message(message.from_user.id,
                         text='Список актуальных вакансий \n(нажми на интересующую тебя вакансию чтобы получить подробную информацию): ',
                         reply_markup=keyboard)
    elif message.text == 'Информация о фирме':
        bot.send_message(message.chat.id, text=get_welcome_message(), reply_markup=start_keyboard())
    elif message.text == 'Сконтактироваться':
        bot.register_next_step_handler(bot.send_message(message.chat.id,
                                                        'Введите свое Имя, номер телефона, и напишите какая вакансия вас интересует.'),
                                       contact)
    elif message.text == 'Список администраторов' and message.chat.id in get_administrators():
        bot.send_message(message.chat.id, text=str(get_administrators()), reply_markup=admin_keyboard())
    elif message.text == 'Добавить администратора' and message.chat.id in get_administrators():
        add_admin(message)
    elif message.text == 'Удалить администратора' and message.chat.id in get_administrators():
        remove_admin(message)
    elif message.text == 'Показать список заявок' and message.chat.id in get_administrators():
        show_contact_list(message)
    elif message.text == 'Добавить новую вакансию' and message.chat.id in get_administrators():
        add_new_offer(message)
    elif message.text == 'Редактировать информацию о фирме' and message.chat.id in get_administrators():
        edit_information_company(message)
    elif message.text == 'Редактировать стартовое сообщение' and message.chat.id in get_administrators():
        edit_start_message(message)
    elif message.text == 'Редактировать вакансию' and message.chat.id in get_administrators():
        keyboard = types.InlineKeyboardMarkup()
        for i in get_db_connection().execute('SELECT * FROM bot').fetchall():
            keyboard.add(types.InlineKeyboardButton(text=f'id = {i[0]} // {i[1]}', callback_data=i[0]))
        msg = bot.send_message(message.from_user.id, text='Cписок актуальных вакансий: ', reply_markup=keyboard)
        edit_offer(message)
    elif message.text == 'Удалить вакансию' and message.chat.id in get_administrators():
        keyboard = types.InlineKeyboardMarkup()
        for i in get_db_connection().execute('SELECT * FROM bot').fetchall():
            keyboard.add(types.InlineKeyboardButton(text=f'id = {i[0]} // {i[1]}', callback_data=i[0]))
        bot.send_message(message.from_user.id, text='Список актуальных вакансий: ', reply_markup=keyboard)
        remove_offer(message)
    else:
        bot.send_message(message.from_user.id, 'Не понимаю вас...')

@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
    toInt = int(call.data)
    if type(toInt) == int:
        msg = get_offer(toInt)
        a = f'Название вакансии: {msg[1]}\n\n' \
            f'Описание вакансии: {msg[2]}\n\n' \
            f'Локализация: {msg[3]}\n\n' \
            f'Почасовая ставка: {msg[4]}'
        bot.send_message(call.message.chat.id, a)

bot.polling(none_stop=True)


import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

"""使用scikit-learn提供的数值归一化"""

if __name__ == "__main__":
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)

    standardScaler = StandardScaler()
    standardScaler.fit(X_train)

    print("特征均值 : {}".format(standardScaler.mean_))  # 各个特征的均值
    print("特征方差 : {}".format(standardScaler.scale_))  # 各个特征标准差

    X_train = standardScaler.transform(X_train)  # 归一化处理
    X_test = standardScaler.transform(X_test)

    knn_clf = KNeighborsClassifier(n_neighbors=3)
    knn_clf.fit(X_train, y_train)

    print("归一化处理后的准确率 : {}".format(knn_clf.score(X_test, y_test)))


import cv2
import numpy as np

img = cv2.imread('water_coins.jpg')
gr_img = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
_ , thresh = cv2.threshold(gr_img , 0 , 255 , cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

#noise removal

kernel = np.ones((3 , 3) , np.uint8)
opening = cv2.morphologyEx(thresh , cv2.MORPH_OPEN , kernel , iterations = 2)

#Sure background

kernel = np.ones((3 , 3) , np.uint8)
sure_bg = cv2.dilate(opening , kernel , iterations = 3)

#FINDING SURE FOREGROUND AREA

dist_transform = cv2.distanceTransform(opening , cv2.DIST_L2 , 5)
ret , sure_fg = cv2.threshold(dist_transform , 0.7*dist_transform.max() , 255 , 0)

#finding unknown region

#cv2.imshow('sure_fg0' , sure_fg)
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg , sure_fg)
#cv2.imshow('sure_fg' , sure_fg)
#cv2.imshow('sure_bg' , sure_bg)

ret , markers = cv2.connectedComponents(sure_fg)
for i in range(markers.shape[0]):
    print(set(markers[i]))
#cv2.imshow('here' , markers)
markers = markers + 1

markers[unknown==255] = 0

markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]

#cv2.imshow('img' , img)


import numpy as np
from scipy import signal

# input:
# data(type:numpy array)(shape:time * 2)
# model(sklearn model or pytorch model)
# flatten(type: bool)(whether to flatten the input as 200 or use 100*2 as the model input)

# output:
# probanility_map(number of split, 12)
def stroke_probability_map(data, model, flatten):
    split_list = [1, 2, 4, 8, 3, 6, 9]
    probability_map = np.zeros((int(np.sum(split_list)), 12))
    N = data.shape[0]
    count = 0
    for split_idx in range(len(split_list)):
        n_ = int(np.floor(N/split_list[split_idx]))
        for i in range(split_list[split_idx]):
            data_cur = signal.resample(data[(i*n_):((i+1)*n_), :], 100, axis=0)
            if flatten:
                data_cur = data_cur.reshape((1, -1))
            probability_map[count] = model(data_cur)
            count += 1

    return probability_map

Continuous CNN/stroke probability map/stroke_probability_map.py


from collections import deque

노드개수, 간선개수 = map(int, input().split())
result = 0

# 빈 그래프 그리기
graph = [ [] for i in range(노드개수+1) ]
for i in range(간선개수):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

visited = [False] * (노드개수 + 1)

def 너비우선탐색(graph, start, visited):
    queue = deque([start])
    visited[start] = True
    while queue:
        v = queue.popleft()
        for i in graph[v]:
            if not visited[i]:
                queue.append(i)
                visited[i] = True

for i in range(노드개수):
    if not visited[i+1]:
        너비우선탐색(graph, i+1, visited)
        result += 1

print(result)

"""
7
6
1 2
2 3
1 5
5 2
5 6
4 7
"""

연결 요소의 개수.py


from setuptools import setup

with open('README.md', 'r', encoding='utf-8') as f:
    readme = f.read()

setup(
    name='gearbest_api',
    version='0.0.4',
    description='Retrieve info from gearbest api.',
    long_description=readme,
    long_description_content_type='text/markdown',
    url='https://github.com/matteobaldelli/python-gearbest-api',
    license='MIT',
    author='Matteo Baldelli',
    author_email='baldelli.matteo2@google.com',
    packages=['gearbest_api'],
    install_requires=['requests'],
    python_requires='>=3.7',
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ]
)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('qsource_user', '0005_auto_20151118_2237'),
    ]

    operations = [
        migrations.CreateModel(
            name='QuestionsAnswered',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('questionID', models.IntegerField()),
                ('user', models.ForeignKey(to='qsource_user.UserData')),
            ],
        ),
        migrations.CreateModel(
            name='QuestionsAsked',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('questionID', models.IntegerField()),
                ('user', models.ForeignKey(to='qsource_user.UserData')),
            ],
        ),
    ]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: mcxiaoke
# @Date:   2016-01-04 11:18:06
import codecs
import os
import sys
import requests
import shutil
import time

HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.1234.0 Safari/537.36',
           'Referer': 'https://google.com/'}

def to_dict(o):
    d = {}
    for n in dir(o):
        if n.startswith('__'):
            continue
        v = getattr(o, n)
        # print(type(v),v, callable(v))
        if callable(v):
            try:
                d[n] = v()
            except Exception as e:
                pass
        else:
            d[n] = v
    return d

def write_list(name, ls):
    if not ls:
        return
    with codecs.open(name, 'w', 'utf-8') as f:
        for s in ls:
            f.write(s + '\n')

def read_list(name):
    if not os.path.isfile(name):
        return None
    with codecs.open(name, 'r', 'utf-8') as f:
        return [line.rstrip('\n') for line in f]

def download_insta_file(media, output='output'):
    url = media.get_standard_resolution_url().replace('s640x640/sh0.08/', '')
    name = url.split('/')[-1]
    fname = media.created_time.strftime("%Y%m%d_%H%M%S") + "_" + name
    #print(fname)
    tmpname = name + ".tmp"
    fpath = os.path.abspath(os.path.join(output, fname))
    path = os.path.abspath(os.path.join(output, name))
    tmppath = os.path.abspath(os.path.join(output, tmpname))
    if os.path.isfile(fpath):
        print('skip exists %s' % path)
        return fpath
    if os.path.isfile(path):
        print('skip exists %s' % path)
        shutil.move(path,fpath)
        return fpath
    try:
        r = requests.get(url, stream=True, headers=HEADERS)
        length = int(r.headers['Content-Length'])
        print('downloading %s (%sk)' % (url, length / 2014))
        if r.status_code == requests.codes.ok:
            with open(tmppath, 'wb') as f:
                for chunk in r.iter_content(chunk_size=4096):
                    f.write(chunk)
            shutil.move(tmppath, fpath)
            print('saved to %s' % path)
            return fpath
        else:
            print('failed: %s' % r)
    except Exception as e:
        print("error:%s on downloading file:%s" % (e, url))

def download_insta_files(medias, output='output'):
    if not os.path.exists(output):
        os.makedirs(output)
    for media in medias:
        download_insta_file(media, output)

def download_files_multi(urls, output='files', pool_size=4):
    if not os.path.exists(output):
        os.makedirs(output)
    from multiprocessing.dummy import Pool
    from functools import partial
    partial_download_file = partial(download_file, output=output)
    pool = Pool(pool_size)
    pool.map(partial_download_file, urls)
    pool.close()
    pool.join()


"""seed event types

Revision ID: 0311eb0fc2e6
Revises: 61043123657a
Create Date: 2021-02-04 14:27:03.847005

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table, column
from sqlalchemy import String, Integer, Boolean

# revision identifiers, used by Alembic.
revision = '0311eb0fc2e6'
down_revision = '61043123657a'
branch_labels = None
depends_on = None

def upgrade():
    guild_settings = table('event_type',
                           column('id', Integer),
                           column('name', String),
                           column('address_required', Boolean),
                       )
    op.bulk_insert(guild_settings,
                   [
                       {
                           "id": 1,
                           "name": "Virtual Gift Exchange",
                           "address_required": False
                       },
                       {
                           "id": 2,
                           "name": "Shipped Gift Exchange",
                           "address_required": True
                       }
                   ]
                   )

def downgrade():
    pass


import pandas as pd
import numpy as np
import string, re
import nltk
import time,random
import operator
#from tabulate import tabulate
from nltk.stem.snowball import SnowballStemmer

import os.path

stop_list = nltk.corpus.stopwords.words('english')
lemmatizer = nltk.stem.WordNetLemmatizer()
punctuation = list(string.punctuation)
stop_list = stop_list + punctuation +["rt", 'url']
stemmer = SnowballStemmer("english")
HillaryWords = ['hillary clinton','hillaryclinton','hilaryclinton','hillari clinton','hilari clinton','hilary clinton','hillary','clinton']
DonaldWords = ['donald trump','donaldtrump','donald','trump','realdonaldtrump']
CarsonWords = ['realbencarson','bencarson','carson']
BushWords = ['jebbush','bush']
hillary_re = re.compile('|'.join(map(re.escape, HillaryWords)))
donald_re = re.compile('|'.join(map(re.escape, DonaldWords)))

classifier =[]

def preprocess(tweet):

    if type(tweet)!=type(2.0):
        tweet = tweet.lower()
        tweet = " ".join(tweet.split('#'))
        tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
        tweet = " ".join(tweet.split('@'))
        tweet = re.sub(r'@([^\s]+)', r'\1', tweet)
        tweet = re.sub('((www\.[^\s]+)|(https://[^\s]+))','URL',tweet)
        tweet = re.sub("http\S+", "URL", tweet)
        tweet = re.sub("https\S+", "URL", tweet)
        tweet = " ".join(tweet.split(':'))
        #removes @username from text entirely
        #tweet = re.sub('@[^\s]+','AT_USER',tweet)
        #tweet = tweet.replace("AT_USER","")
        tweet = tweet.replace("URL","")
        tweet = tweet.replace(".","")
        tweet = tweet.replace('\"',"")
        tweet = tweet.replace('&amp',"")
        #remove punctuation words
        tweet = " ".join([word for word in tweet.split(" ") if word not in stop_list])
        #remove words ending with special character
        tweet = " ".join([word for word in tweet.split(" ") if re.search('^[a-z]+$', word)])
        #remove common words such as "the"
        tweet = " ".join([lemmatizer.lemmatize(word) for word in tweet.split(" ")])
        #stem similar words such as "hurt" and "hurting"
        tweet = " ".join([stemmer.stem(word) for word in tweet.split(" ")])
        tweet = re.sub('[\s]+', ' ', tweet)
        tweet = tweet.strip('\'"')
        #manually stem similar political words
        tweet = hillary_re.sub("hillary", tweet)
        tweet = donald_re.sub("donald", tweet)
        tweet = carson_re.sub("carson", tweet)
        tweet = bush_re.sub("bush", tweet)
        tweet = huckabee_re.sub("huckabee", tweet)
        tweet = cruz_re.sub("cruz", tweet)
        tweet = kasich_re.sub("kasich", tweet)
        tweet = rubio_re.sub("rubio", tweet)
    else:
        tweet=''
    return tweet


import os
import requests
from requests.exceptions import ReadTimeout
from requests_oauthlib import OAuth1

from helpers.configHelpers import decryptEnvVar
from helpers.logHelpers import createLog
from helpers.errorHelpers import URLFetchError

logger = createLog('hathiCover')

class HathiCover():
    """Manager class for finding a cover image for HathiTrust images. This is
    done by parsing a METS object obtained through the Hathi API, extracting
    the first 25 pages and scoring them based on relevancy as a cover. The
    URI to the most relevant page image is ultimately returned.
    """
    HATHI_BASE_API = os.environ.get('HATHI_BASE_API', None)
    HATHI_CLIENT_KEY = decryptEnvVar('HATHI_CLIENT_KEY')
    HATHI_CLIENT_SECRET = decryptEnvVar('HATHI_CLIENT_SECRET')

    def __init__(self, htid):
        self.htid = htid
        self.logger = logger

    def generateOAuth(self):
        """Helper method that generates an OAuth1 block that authenticates
        requests against the HathiTrust Data API. Due to the structure of the
        API this is formatted as part of the query string.

        Returns:
            [object] -- An OAuth1 authentication block
        """
        return OAuth1(
            self.HATHI_CLIENT_KEY,
            client_secret=self.HATHI_CLIENT_SECRET,
            signature_type='query'
        )

    def getResponse(self, queryURL):
        queryAuth = self.generateOAuth()
        try:
            return requests.get(queryURL, auth=queryAuth, timeout=3)
        except ReadTimeout:
            raise URLFetchError(
                'URL request timed out'.format(queryURL),
                504,
                queryURL
            )

    def getPageFromMETS(self):
        """Query method for the best page URI from the record's METS file

        Returns:
            [uri] -- URI to the page to be used as a cover image
        """
        self.logger.debug('Querying {} for cover image'.format(self.htid))
        structURL = '{}/structure/{}?format=json&v=2'.format(
            self.HATHI_BASE_API,
            self.htid
        )
        try:
            structResp = self.getResponse(structURL)
            if structResp.status_code == 200:
                return self.parseMETS(structResp.json())
        except URLFetchError:
            self.logger.warning('Request for structure file timed out')

        return None

    def parseMETS(self, metsJson):
        """Parser that handles the METS file, parsing the first 25 pages into
        HathiPage objects that contain a score and position. Once parsed it
        sets the "imagePage" as the page that contains the most plausibly
        relevant cover.

        Arguments:
            metsJson {object} -- METS object extracted from the JSON response

        Returns:
            [uri] -- URI to the page to be used as a cover image
        """
        structMap = metsJson['METS:structMap']
        self.logger.info('Retrieved METS for {}'.format(self.htid))
        self.pages = [
            HathiPage(page)
            for page in structMap['METS:div']['METS:div'][:25]
        ]

        self.pages.sort(key=lambda x: x.score, reverse=True)
        self.imagePage = self.pages[0]
        return self.getPageURL()

    def getPageURL(self):
        """Extracts a resolvable URI from the page selected as a cover image.
        This URI can be used to create a local copy of the cover.

        Returns:
            [uri] -- The created URI of the cover page
        """
        return '{}/volume/pageimage/{}/{}?format=jpeg&v=2'.format(
            self.HATHI_BASE_API,
            self.htid,
            self.imagePage.page
        )

class HathiPage():
    """A representation of a single page in a HathiTrust record. This contains
    some basic description of the page as well as some metadata that we derive
    from Hathi's description to rank it in terms of its suitability as a cover
    image.
    """

    # These are the "flags" that denote a potential cover page
    # They are drawn from a larger set of flags that can be attached to a page
    PAGE_FEATURES = set(
        ['FRONT_COVER', 'TITLE', 'IMAGE_ON_PAGE', 'TABLE_OF_CONTENTS']
    )

    def __init__(self, pageData):
        self.pageData = pageData
        self.page = self.getPageNo()
        self.flags = self.parseFlags()
        self.score = self.setScore()

    def getPageNo(self):
        """Extracts the current page number (from the front cover, not number
        on the page) from the METS description of the page

        Returns:
            [integer] -- The current page number
        """
        return self.pageData.get('ORDER', 0)

    def parseFlags(self):
        """Extracts the flags (in METS these are grouped under "LABEL") that
        can be used to determine the contents of a page. These are parsed from
        a comma-delimited string into a set.

        Returns:
            [set] -- Unique set of flags assigned to the page
        """
        flagStr = self.pageData.get('LABEL', '')
        return set(flagStr.split(', '))

    def setScore(self):
        """This takes the union of the flags denoted as potentially interesting
        in the class variable above and the current flags set on the page. The
        total count of the resulting set is the "score" for the page,
        essentially how many potentially interesting elements exist on it. This
        allows the HathiCover class to determine the best possible to cover
        to display.

        Returns:
            [integer] -- The score as derived from the union of the flags set
        """
        return len(list(self.flags & self.PAGE_FEATURES))


import datetime
import json
import time
import numpy as np

from common.args import Runtime
from data.premetheus import DataManger

cpu_data_range = {}

def cpu_data_pretreatment():
    y = datetime.datetime.now().year
    m = datetime.datetime.now().month
    d = datetime.datetime.now().day
    dt = str(y) + '-' + str(m) + '-' + str(d) + ' 00:00:00'
    start = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    end = int(time.time())

    # start = int(Runtime().ENV['data.forecast.start'])
    # end = int(Runtime().ENV['data.forecast.end'])

    # start = int(Runtime().ENV['data.train.start'])
    # end = int(Runtime().ENV['data.train.end'])

    datamanage = DataManger(start, end, int(Runtime().ENV['data.sample.period']))
    datamanage.get_all()
    for item in datamanage.jsonlist:
        if item['status'] == 'success':
            if item['data']['result']:
                for res in item['data']['result']:
                    instance = res['metric']['instance']
                    values = res['values']
                    if instance not in cpu_data_range:
                        cpu_data_range[instance] = {'index': [], 'columns': ['precent'], 'data': []}
                    for time_data in values:
                        cpu_data_range[instance]['index'].append(time_data[0])
                        s = []
                        s.append(time_data[1])
                        cpu_data_range[instance]['data'].append(s)
    return cpu_data_range
    # with open("/home/sorawingwind/workhome/program/python/AnomalyDetection/src/source/10-16-record.json", "w") as f:
    #     json.dump(cpu_data_range, f)

def read_train_data():
    with open("/home/sorawingwind/workhome/program/python/AnomalyDetection/src/source/05-06-record.json",
              'r') as load_f:
        cpu_data_range = json.load(load_f)
        return cpu_data_range

def read_forecast_data():
    with open("/home/sorawingwind/workhome/program/python/AnomalyDetection/src/source/05-07-record.json",
              'r') as load_f:
        cpu_data_range = json.load(load_f)
        return cpu_data_range

def data_reader(cpu_data_range):
    ins = {}
    for instance, data in cpu_data_range.items():
        timestamps = data["index"]
        datas = data["data"]
        items = []
        for index, time in enumerate(timestamps):
            item = [time, datas[index][0]]
            items.append(item)
        ins[instance] = np.array(items)
    return ins


import logging
import glob
import collections
import os

import clang.cindex
from clang.cindex import CursorKind

from . import helpers
from . import enum_decl
from . import class_struct_decl
from . import function_decl

_LOGGER = logging.getLogger(__name__)

def _detect_library_file():
    version = os.getenv("PYCODEGEN_LIBCLANG", "")
    candidates = glob.glob("/usr/lib/llvm-{version}*/lib/libclang*.so*".format(version=version))

    if not candidates:
        raise RuntimeError("Unable to find libclang")

    # Select the latest libclang version
    candidates.sort()
    return candidates[-1]

ParserContext = collections.namedtuple("ParserContext", ["input_file"])

class ParserLibClang:

    def __init__(self, library_file=None):
        self._context = None

        if not clang.cindex.Config.loaded:
            if library_file is None:
                library_file = _detect_library_file()

            _LOGGER.debug("Using libclang from: %s", library_file)
            clang.cindex.Config.set_library_file(library_file)

    def dump(self, filename, arguments=None):
        """
        Generate a tree view of the AST

        :param filename: File to parse
        :param arguments: Extra arguments to pass to clang
        :return: String representation of the AST
        """
        import asciitree

        def format_node(cursor):
            return '{name:<10} ({extra})'.format(
                name=cursor.spelling or cursor.displayname,
                extra=cursor.kind.name)

        def get_children(cursor):
            return helpers.get_children(cursor, self._context)

        translation_unit = self._parse_file(filename, extra_arguments=arguments)
        return asciitree.draw_tree(translation_unit.cursor, get_children, format_node)

    def parse(self, filename, arguments=None):
        """
        Parse and return a simplified version of the AST

        :param filename: File to parse
        :param arguments: Extra arguments to pass to clang
        :return: AST representation
        """
        translation_unit = self._parse_file(filename, extra_arguments=arguments)
        return self._traverse(translation_unit.cursor)

    def _parse_file(self, filename, extra_arguments):
        self._context = ParserContext(input_file=filename)
        index = clang.cindex.Index.create()

        arguments = ["-x", "c++", "-D__CODEGEN__"]
        if extra_arguments is not None:
            arguments += extra_arguments

        options = clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES

        translation_unit = index.parse(filename, args=arguments, options=options)
        return translation_unit

    def _handle_recurse(self, cursor, path=None):
        if path is None:
            path = []

        result = []
        for child in helpers.get_children(cursor, self._context):
            child_data = self._traverse(child, path + [cursor.spelling])
            if child_data is not None:
                if isinstance(child_data, list):
                    result += child_data
                else:
                    result.append(child_data)

        return result

    def _traverse(self, cursor, qualified_path=None):
        if cursor.kind in [CursorKind.TRANSLATION_UNIT,
                           CursorKind.NAMESPACE]:
            return self._handle_recurse(cursor, qualified_path)

        if cursor.kind == CursorKind.ENUM_DECL:
            return enum_decl.visit(cursor, qualified_path, self._context)

        if cursor.kind in [CursorKind.CLASS_DECL, CursorKind.STRUCT_DECL]:
            return class_struct_decl.visit(cursor, qualified_path, self._context)

        if cursor.kind in [CursorKind.FUNCTION_DECL]:
            return function_decl.visit(cursor, qualified_path, self._context)

        _LOGGER.warning("Unhandled %s", str(cursor.kind))
        return None


# author：Nicolo
# time：2017/7/23
# function:生成汉字字库并转换为图片
import codecs
import os
import pygame
start,end = (0x4E00, 0x9FA5)
with codecs.open("chinese.txt", "wb", encoding="utf-8") as f:
 for codepoint in range(int(start),int(end)):
  f.write(chr(codepoint))

chinese_dir = 'chinese'
if not os.path.exists(chinese_dir):
 os.mkdir(chinese_dir)

pygame.init()
start,end = (0x4E00, 0x9FA5)#汉字编码范围
for codepoint in range(int(start),int(end)):
 word = chr(codepoint)
 font = pygame.font.Font("C:\Windows\Fonts\STZHONGS.TTF", 22)#当前目录下要有微软雅黑的字体文件msyh.ttc,或者去c:\Windows\Fonts目录下找
 rtext = font.render(word, True, (0, 0, 0), (255, 255, 255))
 pygame.image.save(rtext, os.path.join(chinese_dir,word+".png"))


from __future__ import (absolute_import, print_function,
                unicode_literals, division)

import time
import numpy as np
import pandas as pd
import requests
from bokeh import plotting
from bokeh.objects import ColumnDataSource

class QlogPlot:
    def __init__(self, base, name, limit, ds):
        self.name = name
        self.var = requests.get("%s/variable/%s" % (base, name)).json()[name]
        self.url = "%s/data/%s?limit=%i" % (base, name, limit)
        ds.add([], "%s value" % name)
        ds.add([], "%s time" % name)
        self.update(ds)
        self.plot(ds)

    def plot(self, ds):
        unit = self.var["unit"]
        if self.var["logarithmic"]:
            unit = unit + " (log10)"
        plotting.line("%s time" % self.name, "%s value" % self.name,
                x_axis_type="datetime", source=ds,
                legend="%s (%s)" % (self.name, unit), title="")
        plotting.circle("%s time" % self.name, "%s value" % self.name,
                source=ds, size=2.,
                legend="%s (%s)" % (self.name, unit), title="")

    def update(self, ds):
        df = pd.read_json(self.url)
        y = np.array(df)
        if self.var["logarithmic"]:
            y = np.log10(y)
        ds.data["%s value" % self.name] = y
        ds.data["%s time" % self.name] = df.index

def simple_line_plot(base, names, limit, interval):
    plotting.output_server("QLog")
    plotting.hold()
    plotting.figure()
    ds = ColumnDataSource(data={})
    plots = [QlogPlot(base, name, limit, ds) for name in names]
    plotting.show()

    while True:
        time.sleep(interval)
        for plot in plots:
            plot.update(ds)
        ds._dirty = True
        plotting.session().store_obj(ds)

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-b", "--base",
            default="http://localhost:6881/1")
    parser.add_argument("-l", "--limit", type=int, default=100)
    parser.add_argument("-i", "--interval", type=float, default=5)
    parser.add_argument("names", nargs="+")
    args = parser.parse_args()

    simple_line_plot(args.base, args.names, args.limit,
            args.interval)

if __name__ == "__main__":
    main()


import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def boxplot(df, output_folder):
    #simple version, only makes the 4 boxplots every dataset has in common
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
    #fig.suptitle('Air Beam', fontsize=20)
    dat = [df['Temperature'].dropna()]
    ax1.boxplot(dat, labels = ['Temperature'], vert = True)
    dat = [df['Humidity'].dropna()]
    ax2.boxplot(dat, labels = ['Humidity'], vert = True)
    dat = [df['PM2.5'].dropna()]
    ax3.boxplot(dat, labels = ['PM 2.5'], vert = True)
    dat = [df['PM10.0'].dropna()]
    ax4.boxplot(dat, labels = ['PM 10.0'], vert = True)
    fig.subplots_adjust(wspace=0.5)
    outpath = os.path.join(output_folder, 'boxplot.png')
    fig.savefig(outpath)
    return outpath

def humidity_graph(df, output_folder):
    plt.close()
    _, axarr = plt.subplots(2, figsize=[10,8], sharex = True)
    axarr[0].plot(df['Datetime'], df['PM2.5'], label='PM 2.5')
    axarr[0].plot(df['Datetime'], df['PM10.0'], label='PM 10.0', linestyle="--")
    axarr[0].legend()
    axarr[0].set_title('Particulate Matter and Humidity')
    axarr[1].plot(df['Datetime'], df['Humidity'], label='Humidity (percent)')
    axarr[1].legend()
    fn = 'humidity_graph.png'
    outpath = os.path.join(output_folder, fn)
    plt.savefig(outpath, dpi='figure')
    return outpath

def threshold_PM25(df, output_folder):
    PM25_ANNUAL_PRIMARY_WHO = 10
    PM25_ANNUAL_PRIMARY_NAAQS = 12
    PM25_ANNUAL_SECONDARY_NAAQS = 15
    PM25_24HR_WHO = 25
    PM25_24HR_NAAQS = 35

    _, axarr = plt.subplots(1, figsize=[10,8], sharex = True)
    axarr.plot(df['Datetime'], df['PM2.5'], label='PM 2.5')

    axarr.hlines(PM25_ANNUAL_PRIMARY_WHO, df['Datetime'][0], df['Datetime'].tail(1), color='#800080', linestyles='--', label='WHO Annual Primary')
    axarr.text(df['Datetime'].tail(1), PM25_ANNUAL_PRIMARY_WHO + 0.2,'WHO AP')
    axarr.hlines(PM25_ANNUAL_PRIMARY_NAAQS, df['Datetime'][0], df['Datetime'].tail(1), color='#006400', linestyles='-.', label='NAAQS Annual Primary')
    axarr.text(df['Datetime'].tail(1), PM25_ANNUAL_PRIMARY_NAAQS + 0.2,'NAAQS AP')
    axarr.hlines(PM25_ANNUAL_SECONDARY_NAAQS, df['Datetime'][0], df['Datetime'].tail(1), color='#FFBE42', linestyles='--', label='NAAQS Annual Secondary')
    axarr.text(df['Datetime'].tail(1), PM25_ANNUAL_SECONDARY_NAAQS + 0.2,'NAAQS AS')
    axarr.hlines(PM25_24HR_WHO, df['Datetime'][0], df['Datetime'].tail(1), color='#EE7600', linestyles='-.', label='WHO 24 Hour')
    axarr.text(df['Datetime'].tail(1), PM25_24HR_WHO + 0.2,'WHO 24 Hr')
    axarr.hlines(PM25_24HR_NAAQS, df['Datetime'][0], df['Datetime'].tail(1), color='r', linestyles='--', label='NAAQS 24 Hour')
    axarr.text(df['Datetime'].tail(1), PM25_24HR_NAAQS + 0.2,'NAAQS 24 Hr')

    axarr.legend()
    axarr.set_title('Particulate Matter 2.5')

    fn = 'pm25_graph.png'
    outpath = os.path.join(output_folder, fn)
    plt.savefig(outpath, dpi='figure')
    return outpath

def threshold_PM10(df, output_folder):
    PM10_24HR_WHO = 50
    PM10_ANNUAL_PRIMARY_WHO = 20
    PM10_24HR_NAAQS = 150

    _, axarr = plt.subplots(1, figsize=[10,8], sharex = True)
    axarr.plot(df['Datetime'], df['PM10.0'], label='PM 10.0')

    axarr.hlines(PM10_ANNUAL_PRIMARY_WHO, df['Datetime'][0], df['Datetime'].tail(1), color='#006400', linestyles='--', label='WHO Annual Primary')
    axarr.text(df['Datetime'].tail(1), PM10_ANNUAL_PRIMARY_WHO + 0.5,'WHO AP')
    axarr.hlines(PM10_24HR_WHO, df['Datetime'][0], df['Datetime'].tail(1), color='#EE7600', linestyles='-.', label='WHO 24 Hour')
    axarr.text(df['Datetime'].tail(1), PM10_24HR_WHO + 0.5,'WHO 24 Hr')
    axarr.hlines(PM10_24HR_NAAQS, df['Datetime'][0], df['Datetime'].tail(1), color='r', linestyles='--', label='NAAQS 24 Hour')
    axarr.text(df['Datetime'].tail(1), PM10_24HR_NAAQS + 0.5,'NAAQS 24 Hr')

    axarr.legend()
    axarr.set_title('Particulate Matter 10.0')

    fn = 'pm10_graph.png'
    outpath = os.path.join(output_folder, fn)
    plt.savefig(outpath, dpi='figure')
    return outpath


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

@app.route('/')
def input():
    return render_template('page2.html')

@app.route('/page3',methods=['POST','GET'])
def page3():
    a=request.form.get('nos1',type=int)
    b=request.form.get('nos2',type=int)
    result=a+b
    resp=make_response(render_template('page3.html',result=result))
    resp.set_cookie('result',str(result))
    return resp

@app.route('/page4',methods=['POST','GET'])
def page4():
    c = request.form.get("nos3", type=int)
    result=int(request.cookies.get('result'))
    final_output=c+result
    return render_template('page4.html',result_1=str(final_output))
if __name__=='__main__':
    app.run(debug=True)


import os
import json
import time
import numpy as np

from .FCNN_FA import FCNN_FA

class FCNN_KP(FCNN_FA):
    '''
            Description: Class to define a Fully Connected Neural Network (FCNN)
                         with the Kolen-Pollack (KP) algorithm as learning algorithm
    '''

    def __init__(self, sizes, save_dir):
        '''
        Description: initialize the biases, forward weights and backward weights using
        a Gaussian distribution with mean 0, and variance 1.
        Params:
            - sizes: a list of size L; where L is the number of layers
                     in the deep neural network and each element of list contains
                     the number of neuron in that layer.
                     first and last elements of the list corresponds to the input
                     layer and output layer respectively
                     intermediate layers are hidden layers.
            - save_dir: the directory where all the data of experiment will be saved
        '''
        super(FCNN_KP, self).__init__(sizes, save_dir)

    def train(self, X_train, y_train, X_test, y_test, batch_size, learning_rate, epochs, test_frequency, weight_decay=1):
        '''
        Description: Batch-wise trains image features against corresponding labels.
                     The forward and backward weights and biases of the neural network are updated through
                     the Kolen-Pollack algorithm on batches using SGD
                     del_b and del_w are of same size as all the forward weights and biases
                     of all the layers. del_b and del_w contains the gradients which
                     are used to update forward weights and biases

        Params:
            - X_train, y_train: lists of training features and corresponding labels
            - X_test, y_test: lists of testing features and corresponding labels
            - batch_size: size of the batch
            - learning_rate: eta which controls the size of changes in weights & biases
            - epochs: no. of times to iterate over the whole data
            - test_frequency: the frequency of the evaluation on the test data
        '''
        n_batches = int(X_train.shape[0] / batch_size)

        for j in range(epochs):
            # initialize the epoch field in the data to store
            self.data['epoch_{}'.format(j)] = {}

            start = time.time()
            epoch_loss = []
            batch_iter = self.get_batch(X_train, y_train, batch_size)

            for i in range(n_batches):
                (batch_X, batch_y) = next(batch_iter)
                batch_loss, delta_del_b, delta_del_w = self.backpropagate(batch_X, batch_y)
                epoch_loss.append(batch_loss)
                del_b = delta_del_b
                del_w = delta_del_w
                # update weight and biases
                self.weights = [weight_decay * w - (learning_rate / batch_size)
                                * delw for w, delw in zip(self.weights, del_w)]
                self.biases = [b - (learning_rate / batch_size)
                               * delb for b, delb in zip(self.biases, del_b)]
                # Update the backward matrices of the Kolen-Pollack algorithm
                # It is worth noticing that updating the backward weight matrices B with the same
                # delw term as the forward matrices W is equivalent to the update equations 16 and 17
                # of the paper manuscript
                self.backward_weights = [weight_decay * w - (learning_rate / batch_size)
                                         * delw.T for w, delw in zip(self.backward_weights, del_w)]
            epoch_loss = np.mean(epoch_loss)
            self.data['epoch_{}'.format(j)]['loss'] = epoch_loss

            log_str = "\nEpoch {} completed in {:.3f}s, loss: {:.3f}".format(j, time.time() - start, epoch_loss)
            self.print_and_log(log_str)

            # Evaluate on test set
            test_accuracy = self.eval(X_test, y_test)
            log_str = "Test accuracy: {}%".format(test_accuracy)
            self.print_and_log(log_str)
            self.data['epoch_{}'.format(j)]['test_accuracy'] = test_accuracy

            # Compute angles between both weights and deltas
            deltas_angles, weights_angles = self.evaluate_angles(X_train, y_train)
            self.data['epoch_{}'.format(j)]['delta_angles'] = deltas_angles
            self.data['epoch_{}'.format(j)]['weight_angles'] = weights_angles

            # save results as a json file
            with open(os.path.join(self.save_dir, 'results.json'), 'w') as f:
                json.dump(self.data, f)