#! /usr/bin/env python3
"""
ONTAP REST API Python Client Library Sample Scripts

This script was developed by NetApp to help demonstrate NetApp
technologies.  This script is not officially supported as a
standard NetApp product.

Purpose: Script to create volume using ONTAP REST API Python Client Library.

usage: python3 create_volume.py [-h] -c CLUSTER -v VOLUME_NAME -vs VSERVER_NAME -a
                            AGGR_NAME -sz VOLUME_SIZE(MBs) [-u API_USER]
                            [-p API_PASS]
create_volume.py:  the following arguments are required: -c/--cluster, -v/--volume_name,
                                                             -vs/--vserver_name, -a/--aggr_name,
                                                             -sz/--volume_size

Copyright (c) 2020 NetApp, Inc. All Rights Reserved.

Licensed under the BSD 3-Clause "New" or "Revised" License (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
https://opensource.org/licenses/BSD-3-Clause

"""

from netapp_ontap import NetAppRestError
from netapp_ontap.resources import Volume

from utils import Argument, parse_args, setup_logging, setup_connection

def make_volume_pycl(
        volume_name: str,
        svm_name: str,
        aggr_name: str,
        volume_size: int) -> None:
    """Creates a new volume in a SVM"""

    v_size = int(volume_size) * 1024 * 1024  # MB -> Bytes
    volume = Volume.from_dict({
        'name': volume_name,
        'svm': {'name': svm_name},
        'aggregates': [{'name': aggr_name}],
        'size': v_size,
    })

    try:
        volume.post()
        print("Volume %s created successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume was not created: %s" % err)

def main() -> None:
    """Main function"""

    arguments = [
        Argument("-c", "--cluster", "API server IP:port details"),
        Argument("-v", "--volume_name", "Volume Name"),
        Argument("-vs", "--svm_name", "SVM Name"),
        Argument("-a", "--aggr_name", "Aggregate Name"),
        Argument("-sz", "--volume_size", "Size of the volume(MBs).")]
    args = parse_args("This script will create a new volume.", arguments)
    setup_logging()
    setup_connection(args.cluster, args.api_user, args.api_pass)

    make_volume_pycl(
        args.volume_name,
        args.svm_name,
        args.aggr_name,
        args.volume_size)

if __name__ == "__main__":
    main()


from pyomo.environ import ConcreteModel, Var, Reals, ExternalFunction, sin, sqrt, Constraint, Objective
from pyomo.opt import SolverFactory

m = ConcreteModel()
m.z = Var(range(3), domain=Reals, initialize=2.)
m.x = Var(range(2), initialize=2.)
m.x[1] = 1.0

def blackbox(a,b):
   return sin(a-b)
bb = ExternalFunction(blackbox)

m.obj = Objective(
   expr=(m.z[0]-1.0)**2 + (m.z[0]-m.z[1])**2 + (m.z[2]-1.0)**2 \
       + (m.x[0]-1.0)**4 + (m.x[1]-1.0)**6 # + m.bb(m.x[0],m.x[1])
)
m.c1 = Constraint(expr=m.x[0] * m.z[0]**2 + bb(m.x[0],m.x[1]) == 2*sqrt(2.0))
m.c2 = Constraint(expr=m.z[2]**4 * m.z[1]**2 + m.z[1] == 8+sqrt(2.0))

m.pprint()

optTRF = SolverFactory('trustregion')
optTRF.solve(m, [bb])

m.display()


import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import datetime

target_name = '1_min_tp4_sl4_10yuan_1delay_target'

data = pd.read_csv('data/data_night_shifted_au.csv.gz')
data = data.set_index('time')
data.index = pd.to_datetime(data.index, format='%Y-%m-%d %H:%M:%S.%f')

period = 2*60*1
delay_period = 1
sl_ratio = 4
tp_ratio = 4

potential_list = []
wealth_factor = {'long': [], 'short': []}

for i, (d, df) in enumerate(data.groupby('date')):
    print(f"{d}", end='\r')
    # if i < 60:
    #     continue
    df = df[~df.index.duplicated()]
    night_hour = (df.index.time >= datetime.time(21,5,0)) | (df.index.time < datetime.time(2,25,0))
    morning_hour = (df.index.time >= datetime.time(9,5,0)) & (df.index.time < datetime.time(11,25,0))
    afternoon_hour = (df.index.time >= datetime.time(13,35,0)) & (df.index.time < datetime.time(14,55,0))
    trading_idx = df.index[night_hour | morning_hour | afternoon_hour]
    bid = df['bid_1']
    ask = df['ask_1']
    full_half_sec = pd.date_range(df.index.min(), df.index.max(), freq=pd.Timedelta(0.5, 's'))
    # break

    filled_bid = bid[~bid.index.duplicated()].reindex(full_half_sec).interpolate(method='pad', limit=period, limit_direction='forward').dropna()
    filled_ask = ask[~ask.index.duplicated()].reindex(full_half_sec).interpolate(method='pad', limit=period, limit_direction='forward').dropna()
    spread = (ask/bid) - 1
    spread = spread.rolling(50).mean() + (10/1000)/((bid + ask)/2) #10 yuan per lot
    # spread = spread.clip(lower=) #10 yuan per lot
    spread = spread.reindex(trading_idx)
    sl_short = 1/(1+sl_ratio*spread)
    sl_long = 1-sl_ratio*spread
    tp_short = 1/(1-tp_ratio*spread)
    tp_long = 1+tp_ratio*spread
    long_s = []
    short_s = []

    for i in range(1+delay_period, period+1+delay_period):
        short_s.append(2 - (filled_ask.shift(-i).reindex(trading_idx)/filled_bid.shift(-delay_period).reindex(trading_idx)))
        long_s.append((filled_bid.shift(-i).reindex(trading_idx)/filled_ask.shift(-delay_period).reindex(trading_idx)))

    short_pnl = pd.concat(short_s, axis=1, ignore_index=True)
    long_pnl = pd.concat(long_s, axis=1, ignore_index=True)

    short_tp_breach = np.argmax((short_pnl >= tp_short.values[:,np.newaxis]), axis=1)
    short_tp_breach[short_tp_breach == 0] = short_pnl.shape[1]
    short_sl_breach = np.argmax((short_pnl <= sl_short.values[:,np.newaxis]), axis=1)
    short_sl_breach[short_sl_breach == 0] = short_pnl.shape[1]

    long_tp_breach = np.argmax((long_pnl >= tp_short.values[:,np.newaxis]), axis=1)
    long_tp_breach[long_tp_breach == 0] = long_pnl.shape[1]
    long_sl_breach = np.argmax((long_pnl <= sl_short.values[:,np.newaxis]), axis=1)
    long_sl_breach[long_sl_breach == 0] = long_pnl.shape[1]

    short_tp_trig = (short_tp_breach < short_sl_breach)
    short_sl_trig = (short_sl_breach < short_tp_breach)
    long_tp_trig = (long_tp_breach < long_sl_breach)
    long_sl_trig = (long_sl_breach < long_tp_breach)

    short_tp_breach = np.argmax((short_pnl >= tp_short.values[:,np.newaxis]), axis=1)
    short_tp_breach[short_tp_breach == 0] = short_pnl.shape[1]
    short_sl_breach = np.argmax((short_pnl <= sl_short.values[:,np.newaxis]), axis=1)
    short_sl_breach[short_sl_breach == 0] = short_pnl.shape[1]

    long_tp_breach = np.argmax((long_pnl >= tp_short.values[:,np.newaxis]), axis=1)
    long_tp_breach[long_tp_breach == 0] = long_pnl.shape[1]
    long_sl_breach = np.argmax((long_pnl <= sl_short.values[:,np.newaxis]), axis=1)
    long_sl_breach[long_sl_breach == 0] = long_pnl.shape[1]

    short_tp_trig = (short_tp_breach < short_sl_breach)
    short_sl_trig = (short_sl_breach < short_tp_breach)
    long_tp_trig = (long_tp_breach < long_sl_breach)
    long_sl_trig = (long_sl_breach < long_tp_breach)

    # print(f"ltp={long_tp_trig.sum()} lsl={long_sl_trig.sum()} stp={short_tp_trig.sum()} ssl={short_sl_trig.sum()}")

    short_wealth_factor = short_pnl.iloc[:,-1].copy()
    # short
    if short_tp_trig.sum() > 0:
        idx = np.vstack([np.arange(short_tp_trig.sum()), short_tp_breach[short_tp_trig]])
        short_wealth_factor[short_tp_trig] = short_pnl.loc[short_tp_trig].values[tuple(idx)]

    if short_sl_trig.sum() > 0:
        idx = np.vstack([np.arange(short_sl_trig.sum()), short_sl_breach[short_sl_trig]])
        short_wealth_factor[short_sl_trig] = short_pnl.loc[short_sl_trig].values[tuple(idx)]

    long_wealth_factor = long_pnl.iloc[:,-1].copy()
    # long
    if long_tp_trig.sum() > 0:
        idx = np.vstack([np.arange(long_tp_trig.sum()), long_tp_breach[long_tp_trig]])
        long_wealth_factor[long_tp_trig] = long_pnl.loc[long_tp_trig].values[tuple(idx)]

    if long_sl_trig.sum() > 0:
        idx = np.vstack([np.arange(long_sl_trig.sum()), long_sl_breach[long_sl_trig]])
        long_wealth_factor[long_sl_trig] = long_pnl.loc[long_sl_trig].values[tuple(idx)]

    wealth_factor['short'].append(short_wealth_factor.copy())
    wealth_factor['long'].append(long_wealth_factor.copy())

    del long_pnl
    del short_pnl

    long_potential = (long_wealth_factor > 1).mean()
    short_potential = (short_wealth_factor > 1).mean()

    long_mean_profit = long_wealth_factor[long_wealth_factor > 1].mean()
    short_mean_profit = short_wealth_factor[short_wealth_factor > 1].mean()

    # print(f"{d}: {long_potential=:.5f}, {long_mean_profit=:.5f}, {short_potential=:.5f}, {short_mean_profit=:.5f}")
    potential_list.append({'date': d, 'count': trading_idx.shape[0], 'long_signal_pct': long_potential, 'long_mean_profit': long_mean_profit,
                           'short_signal_pct': short_potential, 'short_mean_profit': short_mean_profit})

pot_df = pd.DataFrame(potential_list)
pot_df['date'] = pd.to_datetime(pot_df['date'].astype(str))

ax = pot_df.set_index('date')[['long_signal_pct', 'short_signal_pct']].plot()
ax.figure.savefig(f'label/{target_name}_imbalance.png')

short_wealth = pd.concat(wealth_factor['short'])
short_wealth.name = 'short_wealth'
long_wealth = pd.concat(wealth_factor['long'])
long_wealth.name = 'long_wealth'

pd.concat([long_wealth, short_wealth], axis=1).reindex(data.index).to_csv(fr'label/{target_name}.csv')


"""cargo bike + gbfs

Revision ID: eca8c5dd7d03
Revises: 877460bb55ba
Create Date: 2021-05-28 15:24:16.469093

"""
from alembic import op
import sqlalchemy as sa
from webapp.storage.base import Point

# revision identifiers, used by Alembic.
revision = 'eca8c5dd7d03'
down_revision = '877460bb55ba'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('alert',
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('created', sa.DateTime(), nullable=False),
    sa.Column('modified', sa.DateTime(), nullable=False),
    sa.Column('operator_id', sa.BigInteger(), nullable=True),
    sa.Column('location_id', sa.BigInteger(), nullable=True),
    sa.Column('resource_id', sa.BigInteger(), nullable=True),
    sa.Column('url', sa.String(length=255), nullable=True),
    sa.Column('type', sa.Enum('other', 'closure', name='alerttype'), nullable=True),
    sa.Column('summary', sa.Text(), nullable=True),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('start', sa.DateTime(timezone=True), nullable=True),
    sa.Column('end', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['location_id'], ['location.id'], ),
    sa.ForeignKeyConstraint(['operator_id'], ['operator.id'], ),
    sa.ForeignKeyConstraint(['resource_id'], ['resource.id'], ),
    sa.PrimaryKeyConstraint('id'),
    mysql_charset='utf8',
    mysql_collate='utf8_unicode_ci'
    )
    op.create_table('location_file',
    sa.Column('location_id', sa.BigInteger(), nullable=True),
    sa.Column('file_id', sa.BigInteger(), nullable=True),
    sa.ForeignKeyConstraint(['file_id'], ['file.id'], ),
    sa.ForeignKeyConstraint(['location_id'], ['location.id'], )
    )
    op.create_table('regular_hours',
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('created', sa.DateTime(), nullable=False),
    sa.Column('modified', sa.DateTime(), nullable=False),
    sa.Column('location_id', sa.BigInteger(), nullable=True),
    sa.Column('weekday', sa.SmallInteger(), nullable=True),
    sa.Column('period_begin', sa.Integer(), nullable=True),
    sa.Column('period_end', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['location_id'], ['location.id'], ),
    sa.PrimaryKeyConstraint('id'),
    mysql_charset='utf8',
    mysql_collate='utf8_unicode_ci'
    )
    op.create_table('resource_file',
    sa.Column('resource_id', sa.BigInteger(), nullable=True),
    sa.Column('file_id', sa.BigInteger(), nullable=True),
    sa.ForeignKeyConstraint(['file_id'], ['file.id'], ),
    sa.ForeignKeyConstraint(['resource_id'], ['resource.id'], )
    )
    op.add_column('location', sa.Column('twentyforseven', sa.Boolean(), nullable=True))
    op.add_column('location', sa.Column('geometry', Point(), nullable=False))
    op.add_column('operator', sa.Column('slug', sa.String(length=255), nullable=True))
    op.add_column('operator', sa.Column('url', sa.String(length=255), nullable=True))
    op.add_column('operator', sa.Column('email', sa.String(length=255), nullable=True))
    op.create_index(op.f('ix_operator_slug'), 'operator', ['slug'], unique=True)
    op.add_column('resource', sa.Column('photo_id', sa.BigInteger(), nullable=True))
    op.create_foreign_key(None, 'resource', 'file', ['photo_id'], ['id'], use_alter=True)

    op.execute("CREATE SPATIAL INDEX geometry_index ON location (geometry)")
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'resource', type_='foreignkey')
    op.drop_column('resource', 'photo_id')
    op.drop_index(op.f('ix_operator_slug'), table_name='operator')
    op.drop_column('operator', 'email')
    op.drop_column('operator', 'url')
    op.drop_column('operator', 'slug')
    op.drop_column('location', 'geometry')
    op.drop_column('location', 'twentyforseven')
    op.drop_table('resource_file')
    op.drop_table('regular_hours')
    op.drop_table('location_file')
    op.drop_table('alert')
    # ### end Alembic commands ###


from flask import Flask, render_template, redirect, flash, request, Response, send_from_directory, jsonify
from pandas import DataFrame
import pandas
import os, time, random
from forms import *
import re

from functions import *

app = Flask(__name__)
app.config['CSRF_ENABLED'] = True
app.config['SECRET_KEY'] = "jk3k43l"

MAX_UID = 1
DATASETS = [] # Stores DataFrames for multiuser mode
while len(DATASETS) <= MAX_UID:
    DATASETS.append(DataFrame())
uid = 0

@app.route("/table")
def table():
    global uid
    if uid != None:
        if DATASETS[uid].shape[1] > 0:
            return DATASETS[uid].head(50).to_html(classes=['main_table'])
        else:
            return ""
    else:
        return ""

@app.route("/")
def index():
    if uid is None:
        redirect("/login")
    return render_template("index.html")

@app.route("/upload", methods=["GET","POST"])
def upload():
    global uid, DATASETS
    if uid == None:
        redirect("/login")
    form = CSVForm()
    fnm = ""
    if form.validate_on_submit():
        try:
            fileid = "uploads/csv-%s.csv" % (time.time()+random.randint(1,100))
            form.csvf.data.save(fileid)
            if form.rewrite.data == False and DATASETS[uid].shape[0] != 0:
                tmp = pandas.read_csv(fileid, engine='c')
                if form.join_cols.data == False:
                    if len(tmp.columns) == len(DATASETS[uid].columns):
                        DATASETS[uid] = pandas.concat([DATASETS[uid], tmp])
                else:
                        DATASETS[uid] = pandas.concat([DATASETS[uid], tmp], axis=1)
            else:
                DATASETS[uid] = pandas.read_csv(fileid)
            os.remove(fileid)
            fnm = "Uploaded!"
        except Exception as e:
            fnm = "Wrong file content! Error: %s" % str(e)
    return render_template("upload.html", form=form, filename=fnm)

@app.route("/calc", methods=["GET","POST"])
def calc():
    global DATASETS, uid
    if uid == None:
        return redirect("/login")
    form = CALCform()
    result = ""
    if form.validate_on_submit():
        data = form.expr.data
        if data:
            args = re.findall("\w", data)
            cols = DATASETS[uid].columns
            if not all([i in cols for i in args]):
                result = "Missing columns!"
            else:
                method = calculate(args, data)
                result = "Calculated! Using method: %s" % method
    return render_template("calc.html", form=form, result=result)

@app.route("/export.csv")
def export():
    global DATASETS, uid
    if uid != None:
        DATASETS[uid].to_csv(os.path.dirname(os.path.realpath(__file__))+'/static/export.csv')
        return send_from_directory(os.path.dirname(os.path.realpath(__file__))+'/static', 'export.csv')
    else:
        return "Nothing to export!"

@app.route("/delete", methods=['GET','POST'])
def delete():
    global DATASETS, uid
    if uid == None:
        return redirect("/login")
    form = DELform()
    if form.validate_on_submit():
        if uid != None and form.col.data:
            del DATASETS[uid][form.col.data]
            return render_template("delete.html", form=form, result="Deleted!")
        return render_template("delete.html", form=form, result="Nothing to delete!")
    return render_template("delete.html", form=form, result="")

@app.route('/jquery.js', methods=['GET'])
def jq():
     return send_from_directory(os.path.dirname(os.path.realpath(__file__))+'/static', 'jquery.js')

@app.route('/info.json')
def info():
    global uid
    if uid != None:
        data = {'len':DATASETS[uid].shape[0]*DATASETS[uid].shape[1],'cols':DATASETS[uid].shape[1],'rows':DATASETS[uid].shape[0]}
    else:
        data = {'len':0,'cols':0,'rows':0}
    return jsonify(data)

def calculate(args, expr):
    global DATASETS, uid
    data = []
    result = np.array([])
    is_opencl = False
    for a in args:
        arr = np.asarray(DATASETS[uid][a])
        if not (arr.dtype == np.int or arr.dtype == np.float):
            is_opencl = False
            data.append(arr)
        else:
            data.append(arr.astype(np.float32))

    if is_opencl:
        ocl = OpenCL()
        if ocl.success:
            ocl.getkernel(args, expr)
            result = ocl.compute(*data)
        method = "OpenCL"
    else:
        lmb = getlambda(args, expr)
        result = np.array(list(map(lmb, *data)))
        method = "IOMAP"

    df_new = DataFrame(result.reshape(result.size, 1), columns = ['result(%s)' % expr])
    DATASETS[uid] = pandas.concat([DATASETS[uid], df_new], axis=1)
    return method

# run the app
app.run(debug=True, host="0.0.0.0")


from concurrent.futures import ProcessPoolExecutor
from functools import partial
import numpy as np
import os
import glob
from util import audio
from hparams import hparams as hp

def build_from_path(in_dir, out_dir, num_workers=1, tqdm=lambda x: x):
  '''Preprocesses the THCHS30 dataset from a given input path into a given output directory.

    Args:
      in_dir: The directory where you have downloaded the THCHS30 dataset
      out_dir: The directory to write the output into
      num_workers: Optional number of worker processes to parallelize across
      tqdm: You can optionally pass tqdm to get a nice progress bar

    Returns:
      A list of tuples describing the training examples. This should be written to train.txt
  '''

  # We use ProcessPoolExecutor to parallize across processes. This is just an optimization and you
  # can omit it and just call _process_utterance on each input if you want.
  executor = ProcessPoolExecutor(max_workers=num_workers)
  futures = []
  index = 1

  # trn_files = glob.glob(os.path.join(in_dir, 'biaobei_48000', '*.trn')) # 标贝数据集
  trn_files = glob.glob(os.path.join(in_dir, 'data', '*.trn')) # 若是单独训练train文件则data改成train
  print("trn_files:",trn_files)
  for trn in trn_files:
    # print("trn:",trn)
    with open(trn) as f:
      pinyin = f.readline().strip('\n')
      # wav_file = trn[:-4] + '.wav' # 标贝数据集
      wav_file = trn[:-4]
      print("wav_file:",wav_file)
      task = partial(_process_utterance, out_dir, index, wav_file, pinyin)
      futures.append(executor.submit(task))
      index += 1
  return [future.result() for future in tqdm(futures) if future.result() is not None]

def _process_utterance(out_dir, index, wav_path, pinyin):
  '''Preprocesses a single utterance audio/text pair.

  This writes the mel and linear scale spectrograms to disk and returns a tuple to write
  to the train.txt file.

  Args:
    out_dir: The directory to write the spectrograms into
    index: The numeric index to use in the spectrogram filenames.
    wav_path: Path to the audio file containing the speech input
    pinyin: The pinyin of Chinese spoken in the input audio file

  Returns:
    A (spectrogram_filename, mel_filename, n_frames, text) tuple to write to train.txt
  '''

  # Load the audio to a numpy array:
  wav = audio.load_wav(wav_path)

  # rescale wav for unified measure for all clips
  wav = wav / np.abs(wav).max() * 0.999

  # trim silence
  wav = audio.trim_silence(wav)

  # Compute the linear-scale spectrogram from the wav:
  spectrogram = audio.spectrogram(wav).astype(np.float32)
  n_frames = spectrogram.shape[1]
  if n_frames > hp.max_frame_num:
    return None

  # Compute a mel-scale spectrogram from the wav:
  mel_spectrogram = audio.melspectrogram(wav).astype(np.float32)

  # Write the spectrograms to disk:
  spectrogram_filename = 'thchs30-spec-%05d.npy' % index
  mel_filename = 'thchs30-mel-%05d.npy' % index
  np.save(os.path.join(out_dir, spectrogram_filename), spectrogram.T, allow_pickle=False)
  np.save(os.path.join(out_dir, mel_filename), mel_spectrogram.T, allow_pickle=False)

  # Return a tuple describing this training example:
  return (spectrogram_filename, mel_filename, n_frames, pinyin)


from bert import bert_model_for_PAGE
from bert.bert_model_for_PAGE import get_shape_list,layer_norm
import tensorflow as tf
from modules import ff,  multihead_attention, label_smoothing, noam_scheme
from utils import convert_idx_to_token_tensor
from tqdm import tqdm
import logging

from data_load import load_vocab

class VaeModel:

    '''
    这是模型的主题框架
    '''
    def __init__(self,hp):
        self.hp = hp
        self.token2idx, self.idx2token = load_vocab(hp.vocab)

    # 变积分自动编码器的编码器
    def encoder_vae(self, xs, training, mode):

        '''
        :param xs:
        :param training:
        :param mode: TPAGE表示训练复述模型     LM 表示训练transformer的解码器  PPAGE：代表预测的时候
        return:
        '''

        input_ids_vae_encoder, input_masks_vae_encoder, segment_ids_vae_encoder, sents1, sents2 = xs

        if mode == "TPAGE" or mode == "PPAGE":
            # 这是整体训练 vae
            encoder_vae = bert_model_for_PAGE.BertModel(
                config=self.hp,
                is_training=training,
                input_ids=input_ids_vae_encoder,
                input_mask=input_masks_vae_encoder,
                token_type_ids=segment_ids_vae_encoder)

            self.embeddings = encoder_vae.get_embedding_table()
            self.full_position_embeddings = encoder_vae.get_full_position_embeddings()
            pattern_para = tf.squeeze(encoder_vae.get_sequence_output()[:, 0:1, :], axis=1)
            gaussian_params = tf.layers.dense(pattern_para, 2 * self.hp.z_dim)
            mean = gaussian_params[:, :self.hp.z_dim]
            stddev = 1e-6 + tf.nn.softplus(gaussian_params[:, self.hp.z_dim:])
        else:
            raise("您好，你选择的mode 出现错误")

        return mean, stddev

    # 变积分自动编码器的解码器
    def decoder_vae(self, ys, z, training, mode):
        input_ids_vae_decoder_enc, input_ids_vae_decoder_dec,output_ids_vae_decoder_dec,  sents1, sents2 = ys
        if mode == "TPAGE" or mode == "PPAGE":
            with tf.variable_scope("decoder_enc_vae", reuse=tf.AUTO_REUSE):
                enc = tf.nn.embedding_lookup(self.embeddings, input_ids_vae_decoder_enc)  # (N, T1, d_model)
                input_shape = get_shape_list(enc, expected_rank=3)
                seq_length = input_shape[1]
                width = input_shape[2]
                output = enc
                assert_op = tf.assert_less_equal(seq_length, self.hp.max_position_embeddings)
                with tf.control_dependencies([assert_op]):
                    position_embeddings = tf.slice(self.full_position_embeddings, [0, 0],
                                                   [seq_length, -1])
                    num_dims = len(output.shape.as_list())
                    position_broadcast_shape = []
                    for _ in range(num_dims - 2):
                        position_broadcast_shape.append(1)
                    position_broadcast_shape.extend([seq_length, width])
                    position_embeddings = tf.reshape(position_embeddings,
                                                     position_broadcast_shape)

                    output += position_embeddings  # 添加位置信息

                enc = output
                ## Blocks
                for i in range(self.hp.num_blocks):
                    with tf.variable_scope("num_blocks_{}".format(i), reuse=tf.AUTO_REUSE):
                        # self-attention
                        enc = multihead_attention(seq_k=input_ids_vae_decoder_enc,
                                                  seq_q=input_ids_vae_decoder_enc,
                                                  queries=enc,
                                                  keys=enc,
                                                  values=enc,
                                                  num_heads=self.hp.num_heads,
                                                  dropout_rate=self.hp.dropout_rate,
                                                  training=training,
                                                  causality=False,
                                                  scope="self_attention")
                        # feed forward
                        enc = ff(enc, num_units=[self.hp.d_ff, self.hp.d_model])

            memory = enc

            # 下面是VAE的decoder中的 decoder
            with tf.variable_scope("decoder_dec_vae", reuse=tf.AUTO_REUSE):
                dec = tf.nn.embedding_lookup(self.embeddings, input_ids_vae_decoder_dec)  # (N, T, d_model)
                input_shape = get_shape_list(dec, expected_rank=3)
                seq_length = input_shape[1]
                width = input_shape[2]
                output = dec
                assert_op = tf.assert_less_equal(seq_length, self.hp.max_position_embeddings)
                with tf.control_dependencies([assert_op]):
                    position_embeddings = tf.slice(self.full_position_embeddings, [0, 0],
                                                   [seq_length, -1])
                    num_dims = len(output.shape.as_list())
                    position_broadcast_shape = []
                    for _ in range(num_dims - 2):
                        position_broadcast_shape.append(1)
                    position_broadcast_shape.extend([seq_length, width])
                    position_embeddings = tf.reshape(position_embeddings,
                                                     position_broadcast_shape)

                    output += position_embeddings                          # 添加位置信息

                dec=output

                # 在这里加上 规则模式 采用的是concate的方式!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                z = tf.expand_dims(z, axis=1)
                z = tf.tile(z, multiples=[1, seq_length, 1])
                dec = tf.concat([dec, z], axis=-1)
                dec = tf.layers.dense(dec, self.hp.d_model)
                # Blocks
                for i in range(self.hp.num_blocks):
                    with tf.variable_scope("num_blocks_{}".format(i), reuse=tf.AUTO_REUSE):
                        # Masked self-attention (Note that causality is True at this time)
                        dec = multihead_attention(seq_k=input_ids_vae_decoder_dec,
                                                  seq_q=input_ids_vae_decoder_dec,
                                                  queries=dec,
                                                  keys=dec,
                                                  values=dec,
                                                  num_heads=self.hp.num_heads,
                                                  dropout_rate=self.hp.dropout_rate,
                                                  training=training,
                                                  causality=True,
                                                  scope="self_attention")

                        # Vanilla attention
                        dec = multihead_attention(seq_k=input_ids_vae_decoder_enc,
                                                  seq_q=input_ids_vae_decoder_dec,
                                                  queries=dec,
                                                  keys=memory,
                                                  values=memory,
                                                  num_heads=self.hp.num_heads,
                                                  dropout_rate=self.hp.dropout_rate,
                                                  training=training,
                                                  causality=False,
                                                  scope="vanilla_attention")
                        ### Feed Forward
                        dec = ff(dec, num_units=[self.hp.d_ff, self.hp.d_model])

        # Final linear projection (embedding weights are shared)
        weights = tf.transpose(self.embeddings) # (d_model, vocab_size)
        # 这里为了适应迎合 强制加上一个 FF
        logits = tf.einsum('ntd,dk->ntk', dec, weights) # (N, T2, vocab_size)
        y_hat = tf.to_int32(tf.argmax(logits, axis=-1))
        return logits, y_hat, output_ids_vae_decoder_dec, sents2

    def train(self, xs, ys,mode):
        '''
        Returns
        loss: scalar.
        train_op: training operation
        global_step: scalar.
        summaries: training summary node
        '''
        # forward

        mu, sigma = self.encoder_vae(xs, training=True, mode=mode)
        if mode == "TPAGE" or mode == "PPAGE":
            # 表示 训练VAE
            # 这里提醒自己一下 将embeding 全部设为True
            z = mu + sigma * tf.random_normal(tf.shape(mu), 0, 1, dtype=tf.float32)
        else:
            raise ("许海明在这里提醒你：出现非法mode")

        logits, preds, y, sents2 = self.decoder_vae(ys, z,training=True,mode=mode)

        # train scheme

        ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
        nonpadding = tf.to_float(tf.not_equal(y, self.token2idx["[PAD]"]))  # 0: <pad>
        loss_decoder = tf.reduce_sum(ce * nonpadding) / tf.to_float(get_shape_list(xs[0],expected_rank=2)[0])

        # 这里加上KL loss
        if mode == "TPAGE":
            KL_loss = tf.reduce_mean(0.5 * tf.reduce_sum(tf.square(mu) + tf.square(sigma) - tf.log(1e-8 + tf.square(sigma)) - 1, [1]))
        else:
            KL_loss = 0.0

        loss = loss_decoder + KL_loss

        global_step = tf.train.get_or_create_global_step()
        lr = noam_scheme(self.hp.lr, global_step, self.hp.warmup_steps)
        optimizer = tf.train.AdamOptimizer(lr)
        train_op = optimizer.minimize(loss, global_step=global_step)

        # # monitor a random sample
        n = tf.random_uniform((), 0, tf.shape(preds)[0] - 1, tf.int32)
        print_demo=(xs[0][n],
                    y[n],
                    preds[n])

        tf.summary.scalar('lr', lr)
        tf.summary.scalar("KL_loss", KL_loss)
        tf.summary.scalar("loss_decoder", loss_decoder)
        tf.summary.scalar("loss", loss)
        tf.summary.scalar("global_step", global_step)

        summaries = tf.summary.merge_all()

        return loss, train_op, global_step, summaries, print_demo

    def eval(self, xs,ys,mode):

        mu, sigma = self.encoder_vae(xs, training=False, mode=mode)   #这里主要是为了获取 embeding 其他的都没用

        if mode == "TPAGE" or mode == "PPAGE":
              # 表示 训练VAE
              # 这里提醒自己一下 将embeding 全部设为True
              z = mu + sigma * tf.random_normal(tf.shape(mu), 0, 1, dtype=tf.float32)

        else:
              raise ("许海明在这里提醒你：出现非法mode")

        # z = tf.random_normal([get_shape_list(xs[0], expected_rank=2)[0], self.hp.z_dim]) #自动生成采样因子

        input_ids_vae_decoder_enc, input_ids_vae_decoder_dec, output_ids_vae_decoder_dec,  sents1, sents2 = ys

        decoder_inputs = tf.ones((tf.shape(xs[0])[0], 1), tf.int32) * self.token2idx["<S>"]
        ys = (input_ids_vae_decoder_enc, decoder_inputs, output_ids_vae_decoder_dec,  sents1, sents2)

        logging.info("Inference graph is being built. Please be patient.")
        for _ in tqdm(range(self.hp.maxlen_vae_Decoder_de)):
            logits, y_hat, y, sents2 = self.decoder_vae(ys, z, training=False, mode=mode)
            if tf.reduce_sum(y_hat, 1) == self.token2idx["[PAD]"]:
                break

            _decoder_inputs = tf.concat((decoder_inputs, y_hat), 1)
            ys = (input_ids_vae_decoder_enc, _decoder_inputs, output_ids_vae_decoder_dec, sents1, sents2)

        # monitor a random sample
        n = tf.random_uniform((), 0, tf.shape(y_hat)[0]-1, tf.int32)
        sent1 = sents1[n]
        pred = convert_idx_to_token_tensor(y_hat[n], self.idx2token)
        sent2 = sents2[n]

        tf.summary.text("sent1", sent1)
        tf.summary.text("pred", pred)
        tf.summary.text("sent2", sent2)
        summaries = tf.summary.merge_all()

        return y_hat, summaries


import numpy as np

def gaussian_mask(sigma, trim):
    """
    Computes the values of a 2D gaussian function with the desired sigma for the positions in a kernel with desired size

    :param sigma: integer or float representing the standard deviation
    :param trim: float, used to compute kernel size. Default is 4.0.
    :return: kernel as ndarray of shape size containing values of gaussian function
    """
    # compute distance d from center to edge of the filter kernel
    d = int(trim * sigma + 0.5)
    # empty list for storage
    vector = []
    for i in range(-d, d + 1):
        # compute value of one dimensional gaussian function (with mean = 0)
        gaussian_1d = np.exp(-i * i / (2 * sigma * sigma)) / np.sqrt(2 * np.pi * sigma * sigma)
        # store value in empty list
        vector.append(gaussian_1d)
    # compute outer product of vector with itself to obtain values of two dimensional gaussian function
    kernel = np.outer(vector, vector)
    # normalize kernel so that it sums to 1
    kernel /= np.sum(kernel)
    return kernel

def convolve(im, kernel):
    """
    Applies a filter kernel to an array (e.g. an image).
    Computes the values for pixels near the edge by virtually flipping the image on edges and corners.
    :param im: ndarray (e.g. an image)
    :param kernel: ndarray
    :return: filtered array
    """
    im_row, im_col = im.shape
    kernel_row, kernel_col = kernel.shape

    # increase size of image to be able to apply filter on pixels near the edge
    pad_height = int((kernel_row - 1) / 2)
    pad_width = int((kernel_col - 1) / 2)
    padded_im = np.zeros((im_row + (2 * pad_height), im_col + (2 * pad_width)))
    pad_row, pad_col = padded_im.shape
    # insert the original image into the middle of the padded image
    padded_im[pad_height:pad_row - pad_height, pad_width:pad_col - pad_width] = im

    filtered_image = np.zeros(im.shape)
    for row in range(im_row):
        for col in range(im_col):
            filtered_image[row, col] = np.sum(kernel * padded_im[row:row + kernel_row, col:col + kernel_col])

    return filtered_image

def gaussian_blurr(im, sigma, trim=4.0):
    """
    Applies a gaussian filter to an image
    :param im: ndarray
    :param sigma: integer or float representing the standard deviation
    :param trim: float, used to compute kernel size. Default is 4.0.
    :return: filtered image as ndarray
    """
    kernel = gaussian_mask(sigma, trim)
    filtered = convolve(im, kernel)
    return filtered

if __name__ == '__main__':
    # from skimage import io
    import matplotlib.pyplot as plt
    from scipy import ndimage

    # img = io.imread(r"C:\Users\marik\Pictures\BilderBioinfo\jw-1h 1_c5.TIF", as_gray=True)
    img = plt.imread(r"C:\Users\marik\Pictures\BilderBioinfo\lena.png")
    img2 = ndimage.gaussian_filter(img, sigma=1)
    img3 = gaussian_blurr(img, 1)
    img4 = img2 - img3

    # assert np.allclose(img2, img3, atol=1/255)
    # --> does not work because of corner region

    plt.subplot(2, 2, 1)
    plt.imshow(img)
    plt.colorbar()
    plt.title('original')

    plt.subplot(2, 2, 2)
    plt.imshow(img2)
    plt.colorbar()
    plt.title('scipy gaussian')

    plt.subplot(2, 2, 3)
    plt.imshow(img3)
    plt.colorbar()
    plt.title('my gaussian')

    plt.subplot(2, 2, 4)
    plt.imshow(img4)
    plt.colorbar()
    plt.title('difference scipy gaussian minus my gaussian')

    plt.show()


from collections import deque
import sys

input = lambda : sys.stdin.readline().rstrip()

N, M = map(int, input().split())
graph = [[] for _ in range(N+1)]

# 각 과목의 선수과목 개수
indegree = [0] * (N+1)

# 각 과목을 수강하는데 걸리는 시간
time = [1] * (N+1)

for _ in range(M):
    A, B = map(int, input().split())
    # A-> B
    graph[A].append(B)
    # B의 선수과목 개수 추가
    indegree[B] += 1

q = deque()
# 선수과목이 없는 과목들을 큐에 넣음
for x in range(1, N+1):
    if indegree[x] == 0:
        q.append(x)

while q:
    now = q.popleft()
    for x in graph[now]:
        # 과목을 수강하는데 걸리는 시간 갱신
        # time[x] : 기존 수강 걸리는 시간
        # time[now] + 1 : now 과목이 수강하는데 걸리는 시간 + 한 학기 이후
        time[x] = max(time[x], time[now]+1)
        indegree[x] -= 1
        # x가 더이상 들어야할 선수과목이 없으면 큐에 넣음
        if indegree[x] == 0:
            q.append(x)

for x in time[1:]:
    print(x, end=" ")


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
.. $Id: views.py 106507 2017-02-14 15:39:53Z carlos.sanchez $
"""

from __future__ import print_function, unicode_literals, absolute_import, division
__docformat__ = "restructuredtext en"

logger = __import__('logging').getLogger(__name__)

from itertools import chain

from zope import component
from zope import lifecycleevent

from zope.security.interfaces import IPrincipal

from pyramid import httpexceptions as hexc

from pyramid.view import view_config

from nti.app.base.abstract_views import AbstractAuthenticatedView

from nti.app.contentfile import validate_sources
from nti.app.contentfile import get_content_files
from nti.app.contentfile import read_multipart_sources

from nti.app.externalization.error import raise_json_error

from nti.app.externalization.view_mixins import ModeledContentUploadRequestUtilsMixin

from nti.app.messaging import MessageFactory as _

from nti.app.messaging.conversations import Conversation

from nti.app.messaging.interfaces import IConversationProvider

from nti.app.messaging.utils import get_user

from nti.appserver.pyramid_authorization import has_permission

from nti.appserver.ugd_edit_views import UGDPostView

from nti.dataserver import authorization as nauth

from nti.externalization.interfaces import LocatedExternalDict
from nti.externalization.interfaces import StandardExternalFields

from nti.messaging.interfaces import IMailbox
from nti.messaging.interfaces import IMessage
from nti.messaging.interfaces import IReceivedMessage

ITEMS = StandardExternalFields.ITEMS
TOTAL = StandardExternalFields.TOTAL
ITEM_COUNT = StandardExternalFields.ITEM_COUNT

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             context=IMailbox)
class MailboxGetView(AbstractAuthenticatedView):

    def __call__(self):
        return self.context

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             name="conversations",
             context=IMailbox)
class MailboxConversationsGetView(AbstractAuthenticatedView):

    def __call__(self):
        provider = IConversationProvider(self.context)
        conversations = provider.conversations(self.remoteUser)
        result = LocatedExternalDict()
        result.__name__ = self.request.view_name
        result.__parent__ = self.request.context
        result.lastModified = self.context.lastModified
        result[ITEMS] = conversations
        result[TOTAL] = result[ITEM_COUNT] = len(conversations)
        return result

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_CREATE,
             request_method='POST',
             context=IMailbox)
class MailboxPOSTView(UGDPostView):

    content_predicate = IMessage.providedBy

    def validate_attachments(self, user, context, sources=()):
        sources = sources or ()
        validate_sources(user, context, sources)
        for source in sources:
            source.__parent__ = context

    def _validate(self, context):
        sources = get_content_files(context)
        if sources and self.request.POST:
            read_multipart_sources(self.request, sources.values())
        if sources:
            self.validate_attachments(self.remoteUser,
                                      context,
                                      sources.values())
        return context

    def __call__(self):
        message = self.readCreateUpdateContentObject(self.remoteUser,
                                                     search_owner=False)
        message = self._validate(message)
        if message.inReplyTo or message.references:
            raise_json_error(self.request,
                             hexc.HTTPUnprocessableEntity,
                             {
                                 u'message': _("Cannot create replies here."),
                                 u'code': 'CannotCreateReply',
                             },
                             None)

        message.updateLastMod()
        message.creator = self.remoteUser.username
        message.From = IPrincipal(message.creator)

        # validate receivers
        receivers = set()
        for name in message.To:
            user = get_user(name)
            if user is None:
                raise_json_error(self.request,
                                 hexc.HTTPUnprocessableEntity,
                                 {
                                     u'message': _("Invalid user"),
                                     u'code': 'InvalidUser',
                                 },
                                 None)
            receivers.add(IPrincipal(name))
        message.To = tuple(receivers)

        lifecycleevent.created(message)
        mailbox = component.getMultiAdapter((self.remoteUser, message,),
                                            IMailbox)
        # distribute to mailbox.
        mailbox.send(message)
        self.request.response.status_int = 201
        return Conversation(mailbox, message, (message,))

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_UPDATE,
             request_method='POST',
             name="opened",
             context=IReceivedMessage)
class MarkOpenedView(AbstractAuthenticatedView):

    def __call__(self):
        if self.context.ViewDate is not None:
            raise_json_error(self.request,
                             hexc.HTTPUnprocessableEntity,
                             {
                                 u'message': _("Message already opened"),
                                 u'code': 'MessageAlreadyOpened',
                             },
                             None)
        self.context.mark_viewed()
        return self.context.Message

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,
             request_method='GET',
             name="thread",
             context=IMessage)
class ThreadView(AbstractAuthenticatedView):
    """
    Returns all the messages in the current thread.  The
    context should be the top level item (thread root)
    """

    def __call__(self):
        messages = []
        message = self.context
        for x in chain((message,), message.referents or ()):
            if has_permission(nauth.ACT_READ, x, self.request):
                messages.append(x)
        result = LocatedExternalDict()
        result.__name__ = self.request.view_name
        result.__parent__ = self.request.context
        result[ITEMS] = messages
        result[TOTAL] = result[ITEM_COUNT] = len(messages)
        return result

@view_config(route_name='objects.generic.traversal',
             renderer='rest',
             permission=nauth.ACT_READ,  # if you can read you can reply
             request_method='POST',
             name="reply",
             context=IMessage)
class ReplyView(AbstractAuthenticatedView,
                ModeledContentUploadRequestUtilsMixin):
    """
    Creates and dispatches a reply in this conversation. This api
    is specialized to the way messaging currently working in the housing
    app.  The threads are one long thread A<-B<-C.  Our context here
    should be the top level.
    """

    def __call__(self):
        message = self.context
        if message.inReplyTo is not None:
            raise_json_error(self.request,
                             hexc.HTTPBadRequest,
                             {
                                 u'message': _("Expected top level message."),
                                 u'code': 'TopLevelMessageExpected',
                             },
                             None)

        # if nothing screwed up there should only be one message
        # without a reply (one leaf). In the event we find more than one leaf
        # we try to get things back on track by choosing the one that was created
        # most recently.
        all_messages = chain(message.referents or (), (message,))
        leafs = [msg for msg in all_messages if not msg.replies]

        if len(leafs) < 1:
            __traceback_info__ = message, leafs
            raise_json_error(self.request,
                             hexc.HTTPBadRequest,
                             {
                                 u'message': _("Expected at least one leaf."),
                                 u'code': 'ExpectedAtLeastOneLeaf',
                             },
                             None)

        if len(leafs) > 1:
            logger.warn('More than one leaf message found.  Bad client?',
                        leafs)

        reply_to = leafs[0]
        reply = self.readCreateUpdateContentObject(self.remoteUser)

        # make sure sender is set to creator (i.e. app can't change that)?
        # anything else?
        reply.From = IPrincipal(self.remoteUser)

        to_principals = set()
        to_principals.update(reply_to.To)
        to_principals.add(reply_to.From)
        to_principals.discard(reply.From)

        reply.To = tuple(to_principals)
        if not reply.Subject:
            reply.Subject = reply_to.Subject

        reply.inReplyTo = reply_to
        reply.addReference(reply_to)
        for ref in reply_to.references or ():
            reply.addReference(ref)

        # distribute to mailbox.
        mailbox = component.getMultiAdapter((self.remoteUser, reply,),
                                            IMailbox)
        mailbox.send(reply)

        # if we can get to a recieved message for us and what we are replying to,
        # update its ReplyDate
        received_msg = component.queryMultiAdapter((self.remoteUser, reply_to),
                                                   IReceivedMessage)
        if received_msg:
            received_msg.mark_replied_to()

        self.request.response.status_int = 201
        return reply


# coding=utf-8
# __author__ = 'xhm'
import sys
import librosa
import librosa.display
import numpy as np

def get_para():
    return sys.argv[1], sys.argv[2]

def get_music_name(name_path):
    split = name_path.split('/')
    music_name = split[-1].strip()
    return music_name

def mel_data(folder_path, txt_path):
    file = open(txt_path, 'r')
    lines = file.readlines()
    for line in lines:
        y, sr = librosa.load(path=line.strip())
        s = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=40, n_fft=3072, hop_length=768, fmax=8000)
        re = librosa.power_to_db(s)
        music_name = get_music_name(line)
        save_path = folder_path + '/' + music_name
        np.save(save_path, re)
        print("Processing " + line.strip() + "...... done")
    file.close()

def main(argv=None):
    folder_path, txt_path = get_para()
    mel_data(folder_path, txt_path)
    print("Preprocessing and Extracting Features........All done")
if __name__ == '__main__':
    main()


from unet_model import UNet
import torch.optim as optim
from tqdm import tqdm

import os
import random

import numpy as np
import torch
from skimage.io import imread
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import nibabel as nib
from glob import glob
import matplotlib.pyplot as plt
import torch.nn as nn
from aslloss import AsymmetricLossOptimized
from scipy import ndimage

def normalize(x): return (x - np.min(x)) / \
    np.max(x - np.min(x)).astype(np.float32)

class MRIdata(Dataset):
    def __init__(self, onehot=False, Normal=True, transform=True):
        self.onehot = onehot
        self.normalize = Normal
        self.transform = transform
        self.datapath = [x for x in sorted(
            glob(r'tr/images/'+'*.nii.gz'), key=os.path.getmtime)]
        self.maskpath = [x for x in sorted(
            glob(r'tr/masks/'+'*.nii.gz'), key=os.path.getmtime)]

    def __len__(self):
        return len(self.datapath)

    def __getitem__(self, idx):

        image = nib.load(self.datapath[idx])
        mask = nib.load(self.maskpath[idx])
        image_np = np.array(image.get_fdata()).astype(np.float32)
        mask_np = np.array(mask.get_fdata()).astype(np.float32)

        if self.normalize:
            image_np = normalize(image_np).astype(np.float32)

        if mask_np.shape[2] != 64:
            pad_size = 64 - mask_np.shape[2]
            pad_array = np.zeros(
                (image_np.shape[0], image_np.shape[1], pad_size//2), dtype=np.float32)
            if pad_size % 2 == 0:
                image_np = np.concatenate(
                    (pad_array, image_np, pad_array), axis=2)
                mask_np = np.concatenate(
                    (pad_array, mask_np, pad_array), axis=2)
            else:
                pad_array2 = np.zeros(
                    (image_np.shape[0], image_np.shape[1], pad_size//2+1), dtype=np.float32)
                image_np = np.concatenate(
                    (pad_array, image_np, pad_array2), axis=2)
                mask_np = np.concatenate(
                    (pad_array, mask_np, pad_array2), axis=2)

        if self.transform:
            # image = transpose(image_np)
            image_np = flip(image_np)
            image_np = rotate(image_np)

        image_t = transforms.ToTensor()(image_np)
        mask_t = transforms.ToTensor()(mask_np)

        one_hot_mask = torch.zeros((2,)+mask_np.shape)
        (one_hot_mask[0, :, :, :])[mask_t == 0] = 1
        (one_hot_mask[1, :, :, :])[mask_t == 1] = 1

        if self.onehot:
            return image_t, one_hot_mask

        else:
            return image_t, mask_t

class MRIslicedata(Dataset):
    def __init__(self, onehot=False, Normal=True):
        self.onehot = onehot
        self.normalize = Normal
        datapath = [x for x in sorted(
            glob(r'images/'+'*.nii.gz'), key=os.path.getmtime)]
        maskpath = [x for x in sorted(
            glob(r'masks/'+'*.nii.gz'), key=os.path.getmtime)]

        image_stack = [normalize(np.array(nib.load(img).get_fdata()))
                       for img in datapath]
        mask_stack = [normalize(np.array(nib.load(img).get_fdata()))
                      for img in maskpath]

        self.images = np.concatenate(image_stack, axis=2)
        self.masks = np.concatenate(mask_stack, axis=2)

    def __len__(self):
        return self.images.shape[2]

    def __getitem__(self, idx):

        image_np = self.images[:, :, idx].astype(np.float32)
        mask_np = self.masks[:, :, idx].astype(np.float32)

        image_t = transforms.ToTensor()(image_np)
        mask_t = transforms.ToTensor()(mask_np)

        if self.onehot:
            one_hot_mask = torch.zeros((2,)+image_np.shape)
            (one_hot_mask[0, :, :])[mask_t[0, ...] == 0] = 1
            (one_hot_mask[1, :, :])[mask_t[0, ...] == 1] = 1
            return image_t, one_hot_mask

        else:
            return image_t, mask_t

def flip(image):
    p1 = np.random.randint(2)
    if p1:
        image = image[::-1, :, :]
    p2 = np.random.randint(2)
    if p2:
        image = image[:, ::-1, :]

    return image

def rotate(image):
    angles = [90, 270, 180, 0]
    image = ndimage.rotate(image, np.random.choice(angles))
    return image

def transpose(image):
    p1 = np.random.randint(2)
    return [image, image.T][p1]

if __name__ == '__main__':

    trainset = MRIslicedata(onehot=True)
    trainset_Loader = DataLoader(trainset, batch_size=32, shuffle=True)
    image, mask = next(iter(trainset_Loader))

    print(image.size(), mask.size())
    print(len(trainset))


"""Classes holding representations of PEFs."""
import os
import dataclasses
from typing import Sequence, Union

import h5py
import numpy as np

from npeff.util import hdf5_util

###############################################################################
# Utilities for loading logits and labels from a PEFs file. They are
# not technically part of the PEFs but we save them with them for convenience.
# This lets us just load the logits and/or labels from a PEFs file if that is
# only what we want.

def load_logits(pefs_filepath: str) -> np.ndarray:
    with h5py.File(os.path.expanduser(pefs_filepath), "r") as f:
        if 'data/predicted_logits' in f:
            return hdf5_util.load_h5_ds(f['data/predicted_logits'])
        else:
            return hdf5_util.load_h5_ds(f['data/logits'])

def load_labels(pefs_filepath: str) -> np.ndarray:
    with h5py.File(os.path.expanduser(pefs_filepath), "r") as f:
        return hdf5_util.load_h5_ds(f['data/labels'])

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

def infer_pefs_file_class(pefs_filepath: str):
    """Infers the type of PEFs contained in a PEFs file.

    Does a quick heuristic check based on the presence of datasets with the h5 file.
    """
    with h5py.File(os.path.expanduser(pefs_filepath), "r") as f:
        if 'data/fisher/values' in f:
            return SparseDiagonalPefs
        elif 'data/ranks' in f:
            return SparseLvrmPefs
        elif 'data/col_offsets' in f:
            return SparseLrmPefs
        else:
            return None

def load(pefs_filepath: str) -> Union['SparseDiagonalPefs', 'SparseLrmPefs', 'SparseLvrmPefs']:
    pefs_cls = infer_pefs_file_class(pefs_filepath)
    if pefs_cls is None:
        raise ValueError(f'File not recognized as a PEFs file: {pefs_filepath}')
    return pefs_cls.load(pefs_filepath)

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

@dataclasses.dataclass
class SparseDiagonalPefs:
    """Represents the sparse diagonal PEFs of a data set."""

    n_parameters: int

    # values.shape = [n_examples, nnz_per_example]
    values: np.ndarray
    # indices.shape = [n_examples, nnz_per_example]
    indices: np.ndarray

    # pef_norms.shape = [n_examples]
    pef_norms: np.ndarray

    # labels.shape = [n_examples]
    labels: np.ndarray
    # logits.shape = [n_examples, n_classes]
    logits: np.ndarray

    def create_for_subset(self, subset_inds: Sequence[int]) -> 'SparseDiagonalPefs':
        """Creates a SparseLrmPefs object corresponding to examples with indices from subset_inds."""
        if not isinstance(subset_inds, np.ndarray):
            subset_inds = np.array(subset_inds, dtype=np.int32)
        return SparseDiagonalPefs(
            n_parameters=self.n_parameters,
            values=self.values[subset_inds],
            indices=self.indices[subset_inds],
            pef_norms=self.pef_norms[subset_inds],
            labels=self.labels[subset_inds],
            logits=self.logits[subset_inds],
        )

    def save(self, filepath: str):
        with h5py.File(os.path.expanduser(filepath), "w") as f:
            hdf5_util.save_h5_ds(f, 'data/dense_fisher_norms', self.pef_norms)

            hdf5_util.save_h5_ds(f, 'data/labels', self.labels)
            hdf5_util.save_h5_ds(f, 'data/predicted_logits', self.logits)

            hdf5_util.save_h5_ds(f, 'data/fisher/values', self.values)
            hdf5_util.save_h5_ds(f, 'data/fisher/indices', self.indices)

            f['data/fisher'].attrs['dense_fisher_size'] = self.n_parameters

    @classmethod
    def load(cls, filepath: str):
        with h5py.File(os.path.expanduser(filepath), "r") as f:
            return cls(
                n_parameters=f['data/fisher'].attrs['dense_fisher_size'],
                labels=hdf5_util.load_h5_ds(f['data/labels']),
                logits=hdf5_util.load_h5_ds(f['data/logits']),
                values=hdf5_util.load_h5_ds(f['data/fisher/values']),
                indices=hdf5_util.load_h5_ds(f['data/fisher/indices']),
                pef_norms=hdf5_util.load_h5_ds(f['data/dense_fisher_norms']),
            )

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

@dataclasses.dataclass
class SparseLrmPefs:
    """Represents the sparse LRM-PEFs of a data set.

    The each PEF is stored in CSC format. We stored the matrix A_i for each example
    such that the full PEF is equal to A_iA_i^T. We assume that the rank of each
    PEF is the same across all examples. We also assume that the representation
    for each PEF contains the same number of non-zero values.
    """

    n_classes: int
    n_parameters: int

    # values.shape = [n_examples, nnz_per_example]
    values: np.ndarray
    # col_offsets.shape = [n_examples, n_classes + 1]
    col_offsets: np.ndarray
    # row_indices.shape = [n_examples, nnz_pef_example]
    row_indices: np.ndarray

    # pef_norms.shape = [n_examples]
    pef_norms: np.ndarray

    # labels.shape = [n_examples]
    labels: np.ndarray
    # logits.shape = [n_examples, n_classes]
    logits: np.ndarray

    def create_for_subset(self, subset_inds: Sequence[int]) -> 'SparseLrmPefs':
        """Creates a SparseLrmPefs object corresponding to examples with indices from subset_inds."""
        if not isinstance(subset_inds, np.ndarray):
            subset_inds = np.array(subset_inds, dtype=np.int32)
        return SparseLrmPefs(
            n_classes=self.n_classes,
            n_parameters=self.n_parameters,
            labels=self.labels[subset_inds],
            logits=self.logits[subset_inds],
            values=self.values[subset_inds],
            col_offsets=self.col_offsets[subset_inds],
            row_indices=self.row_indices[subset_inds],
            pef_norms=self.pef_norms[subset_inds],
        )

    def save(self, filepath: str):
        with h5py.File(os.path.expanduser(filepath), "w") as f:
            hdf5_util.save_h5_ds(f, 'data/labels', self.labels)
            hdf5_util.save_h5_ds(f, 'data/logits', self.logits)

            hdf5_util.save_h5_ds(f, 'data/values', self.values)
            hdf5_util.save_h5_ds(f, 'data/col_offsets', self.col_offsets)
            hdf5_util.save_h5_ds(f, 'data/row_indices', self.row_indices)

            hdf5_util.save_h5_ds(f, 'data/pef_frobenius_norms', self.pef_norms)

            f['data'].attrs['n_classes'] = self.n_classes
            f['data'].attrs['n_parameters'] = self.n_parameters

    @classmethod
    def load(cls, filepath: str):
        with h5py.File(os.path.expanduser(filepath), "r") as f:
            return cls(
                n_classes=f['data'].attrs['n_classes'],
                n_parameters=f['data'].attrs['n_parameters'],
                labels=hdf5_util.load_h5_ds(f['data/labels']),
                logits=hdf5_util.load_h5_ds(f['data/logits']),
                values=hdf5_util.load_h5_ds(f['data/values']),
                col_offsets=hdf5_util.load_h5_ds(f['data/col_offsets']),
                row_indices=hdf5_util.load_h5_ds(f['data/row_indices']),
                pef_norms=hdf5_util.load_h5_ds(f['data/pef_frobenius_norms']),
            )

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

@dataclasses.dataclass
class SparseLvrmPefs:
    """Represents the sparse LVRM-PEFs of a data set.

    LVRM stands low, variable-rank matrix. This implementation has a
    a fixed NNZ entries per example, which probably leads to examples
    with high rank PEFs have poor sparse approximations.
    """

    n_parameters: int

    # values.shape = [n_examples, nnz_pef_example]
    values: np.ndarray

    # row_indices.shape = [n_examples, nnz_pef_example]
    row_indices: np.ndarray

    # ranks.shape = [n_examples]
    ranks: np.ndarray
    # col_sizes.shape = [sum(ranks)]
    col_sizes: np.ndarray

    # pef_norms.shape = [n_examples]
    pef_norms: np.ndarray

    # labels.shape = [n_examples]
    labels: np.ndarray
    # logits.shape = [n_examples, n_classes]
    logits: np.ndarray

    def create_for_subset(self, subset_inds: Sequence[int]) -> 'SparseLvrmPefs':
        """Creates a SparseLvrmPefs object corresponding to examples with indices from subset_inds."""
        raise NotImplementedError('TODO')

    def save(self, filepath: str):
        raise NotImplementedError('TODO')

    @classmethod
    def load(cls, filepath: str):
        raise NotImplementedError('TODO')


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

from flask import Flask,jsonify,request,abort,Response
from time import strftime,localtime
from uuid import uuid4
import json

# 实例化http服务
app = Flask(__name__)

# 业务逻辑
class Task(object):
    def __init__(self,content):
        self.id = str(uuid4())
        self.content = content
        self.created_at = strftime("%y-%m-%d:%H:%M:%S")
        self.is_finished = False
        self.finished_at = None

    def finish(self):
        self.is_finished = True
        self.finished_at = strftime("%y-%m-%d:%H:%M:%S")

    def json(self):
        return {
            'id':self.id,
            'content':self.content,
            'created_at':self.created_at,
            'is_finished':self.is_finished,
            'finished_at':self.finished_at,
        }

# 模拟数据库接口
tasks_db = {'0':Task('test')}
get_task = tasks_db.get

# 接收请求和做出响应
# 展示
@app.route('/task')
def show():
    return jsonify(data=[task.json() for task in tasks_db.values()])

# 添加
@app.route('/task',methods=['POST'])
def add():
    content = request.form.get('content',None)
    if not content:
        abort(400)
    task = Task(content)
    tasks_db[task.id] = task
    return Response()

# 修改
@app.route('/task/<tid>/finish',methods=['PUT'])
def finish(tid):
    task = get_task(tid)
    if task:
        task.finish()
        tasks_db[tid] = task
        return Response()
    abort(404)

# 删除
@app.route('/task/<tid>',methods=['DELETE'])
def delete(tid):
    task = get_task(tid)
    if task:
        tasks_db.pop(tid)
        return Response()
    abort(404)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8000)


import time
import datetime
import inspect
import logging

def fstr(fstring_text, locals, globals=None):
    """
    Dynamically evaluate the provided fstring_text

    Sample usage:
        format_str = "{i}*{i}={i*i}"
        i = 2
        fstr(format_str, locals()) # "2*2=4"
        i = 4
        fstr(format_str, locals()) # "4*4=16"
        fstr(format_str, {"i": 12}) # "10*10=100"
    """
    locals = locals or {}
    globals = globals or {}
    ret_val = eval(f'f"{fstring_text}"', locals, globals)
    return ret_val

class timelogged(object):
    """
    Decorator class for logging function start, completion, and elapsed time.
    """

    def __init__(
        self,
        desc_text="'{desc_detail}' call to {fn.__name__}()",
        desc_detail="",
        start_msg="Выполняется {desc_text}...",
        success_msg="Готово: {desc_text}  {elapsed}",
        log_fn=logging.info,
        **addl_kwargs,
    ):
        """ All arguments optional """
        self.context = addl_kwargs.copy()  # start with addl. args
        self.context.update(locals())  # merge all constructor args
        self.context["elapsed"] = None
        self.context["start"] = time.time()

    def re_eval(self, context_key: str):
        """ Evaluate the f-string in self.context[context_key], store back the result """
        self.context[context_key] = fstr(self.context[context_key], locals=self.context)

    def elapsed_str(self):
        """ Return a formatted string, e.g. '(HH:MM:SS elapsed)' """
        seconds = time.time() - self.context["start"]
        delta = datetime.timedelta(seconds=seconds)
        str_delta = ""

        seconds = delta.total_seconds()
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        seconds = int(seconds % 60)

        if hours:
            str_delta += str(minutes) + "h "

        if minutes:
            str_delta += str(minutes) + "m "

        if seconds:
            str_delta += str(seconds) + "s "

        milliseconds = round(delta.microseconds / 1000)
        str_delta += str(milliseconds) + "ms"

        return "(за {})".format(str_delta)

    def __call__(self, fn):
        """ Call the decorated function """

        def wrapped_fn(*args, **kwargs):
            """
            The decorated function definition. Note that the log needs access to
            all passed arguments to the decorator, as well as all of the function's
            native args in a dictionary, even if args are not provided by keyword.
            If start_msg is None or success_msg is None, those log entries are skipped.
            """
            self.context["fn"] = fn
            fn_arg_names = inspect.getfullargspec(fn).args
            for x, arg_value in enumerate(args, 0):
                self.context[fn_arg_names[x]] = arg_value
            self.context.update(kwargs)
            desc_detail_fn = None
            log_fn = self.context["log_fn"]
            # If desc_detail is callable, evaluate dynamically (both before and after)
            if callable(self.context["desc_detail"]):
                desc_detail_fn = self.context["desc_detail"]
                self.context["desc_detail"] = desc_detail_fn()

            # Re-evaluate any decorator args which are fstrings
            self.re_eval("desc_detail")
            self.re_eval("desc_text")
            # Remove 'desc_detail' if blank or unused
            self.context["desc_text"] = self.context["desc_text"].replace("'' ", "")
            self.re_eval("start_msg")
            if self.context["start_msg"]:
                # log the start of execution
                log_fn(self.context["start_msg"])
            ret_val = fn(*args, **kwargs)
            if desc_detail_fn:
                # If desc_detail callable, then reevaluate
                self.context["desc_detail"] = desc_detail_fn()
            self.context["elapsed"] = self.elapsed_str()
            # log the end of execution
            log_fn(fstr(self.context["success_msg"], locals=self.context))
            return ret_val

        return wrapped_fn


# ------------------ Application Server code for URL Shortener APP --------------------------------

from flask import Flask,request,render_template,redirect
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from wtforms import validators
import pyshorteners
import os
import re
shorter=""
url=""
# _____________________________________________________________________________________________________

app=Flask(__name__)
# _____________________________________________________________________________________________________

basedir=os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///'+os.path.join(basedir,'data.sqllite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False

db=SQLAlchemy(app)
Migrate(app,db)

class Url(db.Model):
    __tablename__='urlshortener'
    id=db.Column(db.Integer,primary_key=True)
    url=db.Column(db.String(100))
    shorter_url = db.Column(db.String(100))

    def __init__(self,url,shorter_url):
        self.url=url
        self.shorter_url=shorter_url
#____________________________________________________________________________________________

@app.before_first_request
def create_tables():
    db.create_all()

@app.route('/',methods=["GET","POST"])
def home_page():
    global shorter_url,url
    if request.method=='POST':
        url=request.form.get('url')
        regex = re.compile(
        r'^(?:http|ftp)s?://' # http:// or https://
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
        r'localhost|' #localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
        r'(?::\d+)?' # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)

        if url == '' or re.match(regex, url) is None:
            return f"
Please Enter a Valid URL..!!!
"
        else :
            if validators.url(url):
                while True:
                    s=pyshorteners.Shortener()
                    shorter_url=s.tinyurl.short(url)
                    short_url = Url.query.filter_by(url=shorter_url).first()
                    if not short_url:
                        val_pass=Url(url,shorter_url)
                        db.session.add(val_pass)
                        db.session.commit()
                        return render_template('home.html',n=shorter_url,error=0)
            return render_template('home.html',error=1)

    return render_template("home.html")

@app.route('/History',methods=["GET","POST"])
def history_page():
    allurls=Url.query.all()
    return render_template('history.html',allurls=allurls)

@app.route('/delete/<int:id>')
def delete(id):
    item = Url.query.get_or_404(id)
    db.session.delete(item)
    db.session.commit()
    return redirect('/History')
# ____________________________________________________________________________________________________

if __name__=="__main__":
    app.run(debug=True)


from typing import List, Optional

from overrides import overrides
from torch import nn

from archai.discrete_search.api.archai_model import ArchaiModel
from archai.discrete_search.api.model_evaluator import ModelEvaluator

class NonEmbeddingParamsProxy(ModelEvaluator):
    """Total number of non-embedding parameters."""

    def __init__(self, exclude_cls: Optional[List[nn.Module]] = None, trainable_only: Optional[bool] = True) -> None:
        """Initialize the evaluator.

        Used as a proxy for the perplexity of decoder-only transformer LMs.

        Args:
            exclude_cls: List of PyTorch module classes to exclude from parameter counting.
                If `None`, defaults to `[torch.nn.Embedding]`.
            trainable_only: Whether only trainable parameters should be counted.

        Reference:
            "LiteTransformerSearch: Training-free Neural Architecture Search for
                Efficient Language Models", Javaheripi et. al, 2022

        """

        self.exclude_cls = exclude_cls or [nn.Embedding]
        self.trainable_only = trainable_only

    @overrides
    def evaluate(self, model: ArchaiModel, budget: Optional[float] = None) -> float:
        total_params = sum(
            param.numel() for param in model.arch.parameters() if not self.trainable_only or param.requires_grad
        )

        embed_params = sum(
            sum(param.numel() for param in module.parameters())
            for module in model.arch.modules()
            if isinstance(module, tuple(self.exclude_cls))
        )

        return total_params - embed_params


from itertools import combinations
import sys

sys.stdin = open('sample_input.txt')

real = [True for _ in range(9)]
small = [int(input()) for _ in range(9)]
all_sum = sum(small)
comb = list(combinations([N for N in range(9)],2))
bad_guys = []
for c in comb:
    if all_sum-(small[c[0]]+small[c[1]]) == 100:
        bad_guys.append(c)
real[bad_guys[0][0]], real[bad_guys[0][1]] = False, False
answer = sorted([small[idx] for idx in range(9) if real[idx] == True])
for ans in answer:
    print(ans)


import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.autograd import Variable
from dataloader import read_bci_data
import numpy as np
from sklearn.metrics import accuracy_score
from tqdm import tqdm
import argparse
import matplotlib.pyplot as plt

class DeepConvNet(nn.Module):
    def __init__(self, activation='elu'):
        super(DeepConvNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 25, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.conv2 = nn.Conv2d(25, 25, kernel_size=(2, 1), stride=(1, 1), bias=True)
        self.batchnorm1 = nn.BatchNorm2d(25, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout1 = nn.Dropout(p=0.5)
        self.conv3 = nn.Conv2d(25, 50, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm2 = nn.BatchNorm2d(50, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout2 = nn.Dropout(p=0.5)
        self.conv4 = nn.Conv2d(50, 100, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm3 = nn.BatchNorm2d(100, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout3 = nn.Dropout(p=0.5)
        self.conv5 = nn.Conv2d(100, 200, kernel_size=(1, 5), stride=(1, 1), bias=True)
        self.batchnorm4 = nn.BatchNorm2d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout4 = nn.Dropout(p=0.5)
        self.batchnorm5 = nn.BatchNorm1d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.dropout5 = nn.Dropout(p=0.5)
        self.fc = nn.Linear(8600, 2, bias=True)

        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'elu':
            self.activation = nn.ELU(alpha=1.0)
        elif activation == 'leaky_relu':
            self.activation = nn.LeakyReLU(negative_slope=0.01)
        else:
            raise ValueError('Invalid activation function')

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.batchnorm1(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout1(x)
        x = self.conv3(x)
        x = self.batchnorm2(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout2(x)
        x = self.conv4(x)
        x = self.batchnorm3(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout3(x)
        x = self.conv5(x)
        x = self.batchnorm4(x)
        x = self.activation(x)
        x = F.max_pool2d(x, kernel_size=(1, 2), stride=(1, 2))
        x = self.dropout4(x)
        x = x.view(x.size(0), -1)
        # print(x.size())
        x = self.fc(x)

        return x

class EEGNet(nn.Module):
    def __init__(self, activation='elu'):
        super(EEGNet, self).__init__()

        # Layer 1
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(1, 51), stride=(1, 1), padding=(0, 25), bias=False)
        self.batchnorm1 = nn.BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)

        # Layer 2
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(2, 1), stride=(1, 1), groups=16, bias=False)
        self.batchnorm2 = nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.pooling2 = nn.AvgPool2d(kernel_size=(1, 4), stride=(1, 4), padding=0)

        # Layer 3
        self.conv3 = nn.Conv2d(32, 32, kernel_size=(1, 15), stride=(1, 1), padding=(0, 7), bias=False)
        self.batchnorm3 = nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        self.pooling3 = nn.AvgPool2d(kernel_size=(1, 8), stride=(1, 8), padding=0)

        # Activation function
        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'elu':
            self.activation = nn.ELU(alpha=1.0)
        elif activation == 'leaky_relu':
            self.activation = nn.LeakyReLU(negative_slope=0.01)
        else:
            raise ValueError('Invalid activation function')

        # Fully connected layer
        self.fc = nn.Linear(736, 2, bias=True)

    def forward(self, x):
        # Layer 1
        x = self.conv1(x)
        x = self.batchnorm1(x)

        # Layer 2
        x = self.conv2(x)
        x = self.batchnorm2(x)
        x = self.activation(x)
        x = self.pooling2(x)
        x = nn.functional.dropout(x, p=0.25)

        # Layer 3
        x = self.conv3(x)
        x = self.batchnorm3(x)
        x = self.activation(x)
        x = self.pooling3(x)
        x = nn.functional.dropout(x, p=0.25)

        # Flatten
        x = x.view(x.size(0), -1)

        # Fully connected layer
        x = self.fc(x)

        return x

def evaluate(model, Loader):

    for i, batch in enumerate(Loader):
        inputs, labels = batch
        inputs = inputs.to("cuda")
        labels = np.asarray(labels)
        predicted = model(inputs)

        mask = predicted[:, 0] < predicted[:, 1]
        result = np.where(mask.cpu(), 1, 0)
        labels = labels.astype(np.int32)

        results = accuracy_score(labels, result)

        return results

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("model", default="eegnet", help="please input your model name, eegnet or deepconvnet")
    args = parser.parse_args()

    if args.model == "eegnet":
        net_elu = EEGNet(activation="elu").to("cuda")
        net_relu = EEGNet(activation="relu").to("cuda")
        net_lrelu = EEGNet(activation="leaky_relu").to("cuda")
    elif args.model == "deepconvnet":
        net_elu = DeepConvNet(activation="elu").to("cuda")
        net_relu = DeepConvNet(activation="relu").to("cuda")
        net_lrelu = DeepConvNet(activation="leaky_relu").to("cuda")
    criterion = nn.CrossEntropyLoss()  # DOn't forget to add softmax layer if change to another loss function
    Batch_size = 256
    Learning_rate = 2e-3
    Epochs = 150
    optimizer_elu = optim.Adam(net_elu.parameters(), lr=Learning_rate)
    optimizer_relu = optim.Adam(net_relu.parameters(), lr=Learning_rate)
    optimizer_lrelu = optim.Adam(net_lrelu.parameters(), lr=Learning_rate)

    train_X, train_Y, test_X, test_Y = read_bci_data()  # load data from dataloader
    train_dataset = TensorDataset(torch.Tensor(train_X), torch.Tensor(train_Y))
    test_dataset = TensorDataset(torch.Tensor(test_X), torch.Tensor(test_Y))

    # train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
    # test_data = DataLoader(test_dataset, batch_size=1080)

    train_accuracy_elu = []
    test_accuracy_elu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_elu.zero_grad()

            # forward + backward + optimize
            outputs = net_elu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_elu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_elu, test_data))

        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_elu.append(np.mean(train_accuracy_batch))
        test_accuracy_elu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_elu: {}".format(train_accuracy_elu))
    # print("test_accuracy_elu: {}".format(test_accuracy_elu))
    print("highest_accuracy_elu: {}".format(max(test_accuracy_elu)))
    plt.plot(np.arange(len(train_accuracy_elu)), train_accuracy_elu, color='red', linestyle="-", markersize="16", label="train_elu")
    plt.plot(np.arange(len(test_accuracy_elu)), test_accuracy_elu, color='blue', linestyle="-", markersize="16", label="test_elu")

    train_accuracy_relu = []
    test_accuracy_relu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_relu.zero_grad()

            # forward + backward + optimize
            outputs = net_relu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_relu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_relu, test_data))
        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_relu.append(np.mean(train_accuracy_batch))
        test_accuracy_relu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_relu: {}".format(train_accuracy_relu))
    # print("test_accuracy_relu: {}".format(test_accuracy_relu))
    print("highest_accuracy_relu: {}".format(max(test_accuracy_relu)))
    plt.plot(np.arange(len(train_accuracy_relu)), train_accuracy_relu, color='brown', linestyle="-", markersize="16", label="train_relu")
    plt.plot(np.arange(len(test_accuracy_relu)), test_accuracy_relu, color='pink', linestyle="-", markersize="16", label="test_relu")

    train_accuracy_lrelu = []
    test_accuracy_lrelu = []
    for epoch in range(Epochs):
        train_data = DataLoader(train_dataset, batch_size=Batch_size, shuffle=True)
        test_data = DataLoader(test_dataset, batch_size=1080)
        running_loss = 0.0
        # epoch_bar = tqdm(range(len(train_X)//Batch_size + int(bool(len(train_X) % Batch_size))), desc='Epoch %d' % (epoch+1))  # deal with the last one batch
        train_accuracy_batch = []
        test_accuracy_batch = []
        for i, batch in enumerate(tqdm(train_data, desc='Epoch %d' % (epoch+1))):
            inputs, labels = batch
            labels = labels.to("cuda")
            inputs = inputs.to("cuda")
            # zero the parameter gradients
            optimizer_lrelu.zero_grad()

            # forward + backward + optimize
            outputs = net_lrelu(inputs)

            loss = criterion(outputs, torch.squeeze(labels.long()))  # CrossEntropyLoss' target need to be long
            loss.backward()

            optimizer_lrelu.step()

            mask = outputs[:, 0] < outputs[:, 1]
            result = np.where(mask.cpu(), 1, 0)
            labels = np.asanyarray(labels.cpu(), dtype=np.int32)
            train_accuracy_batch.append(accuracy_score(labels, result))

            # print("loss: {}\n".format(loss.data))
            running_loss += loss.data.item()

            test_accuracy_batch.append(evaluate(net_lrelu, test_data))
        tqdm.write(f'Epoch {epoch+1}, Loss: {running_loss:.4f}')
        train_accuracy_lrelu.append(np.mean(train_accuracy_batch))
        test_accuracy_lrelu.append(np.mean(test_accuracy_batch))

    # print("train_accuracy_lrelu: {}".format(train_accuracy_lrelu))
    # print("test_accuracy_lrelu: {}".format(test_accuracy_lrelu))
    print("highest_accuracy_lrelu: {}".format(max(test_accuracy_lrelu)))
    plt.plot(np.arange(len(train_accuracy_lrelu)), train_accuracy_lrelu, color='olive', linestyle="-", markersize="16", label="train_lrelu")
    plt.plot(np.arange(len(test_accuracy_lrelu)), test_accuracy_lrelu, color='cyan', linestyle="-", markersize="16", label="test_lrelu")
    # plt.plot(len(train_accuracy), train_accuracy, color='red', linestyle="-", marker=".", label="train_accuracy")
    # plt.plot(len(test_accuracy), test_accuracy, color='blue', linestyle="-", marker=".", label="test_accuracy")
    plt.xlabel('epoches')
    plt.ylabel('accuracy')
    plt.title('comparison')
    plt.legend()
    plt.savefig('model_{}.png'.format(args.model))
    # plt.legend()
    # plt.show()


from datetime import datetime
from typing import *
from extractor.common import *
from extractor.session_spliter import SessionContainer

class SessionEvaluator:
    def __init__(self, clf):
        self.clf = clf
        self.update_time = None
        self.session_ctx = SessionContainer()

    def evaluate(self, pkts: List[Packet]):
        pkt_info_list = [get_packet_info(pkt) for pkt in pkts]
        self.update_time = datetime.now()
        self.session_ctx.update_pkts(pkt_info_list)
        features = self.session_ctx.get_features()
        res = dict()
        y_pred = self.clf.predict(list(features.values()))
        for i, host in enumerate(features.keys()):
            res[host] = y_pred[i]
        return res


from src.util import *
from collections import deque

cups = deque([int(char) for char in open(SAMPLE).readlines()[0]])

print(cups)
print("")

moves = 100
amount = len(cups)

current_i = 0
current_val = cups[0]
for m in range(0, moves):
    print(cups)
    print("current_i:", current_i)
    print("current_val:", current_val)
    cups.rotate(amount - 3 - (current_i + 1))
    n3 = cups.pop()
    n2 = cups.pop()
    n1 = cups.pop()
    ns = [n1, n2, n3]
    dest = current_val - 1
    while dest in ns or dest < 1:
        if dest < 1:
            dest = 9
        else:
            dest -= 1
    dest_i = cups.index(dest)
    print("dest_i:", dest_i)
    print("dest:", cups[dest_i])
    cups.insert(dest_i + 1, n3)
    cups.insert(dest_i + 1, n2)
    cups.insert(dest_i + 1, n1)
    current_i = cups.index(current_val) + 1
    if current_i > amount - 1:
        current_i = 0
    current_val = cups[current_i]
    print("")

print("")
print(cups)
print("")
index_1 = cups.index(1)
cups.rotate(-1 * (index_1 + 1))
print(cups)
print("")
cups.pop()
print("".join([str(n) for n in cups]))


import inspect
from importlib import import_module

from easy_tbot.utils import Cached
from easy_tbot.loader import load_settings, get_logger, for_app_do
from easy_tbot.handlers import middleware, handler, inline

from .bot import Bot

logger = get_logger()

class Loader:
    """
    A factory kind class, instances works as functions that load some base_class childs from a bot section
    """

    def __init__(self, base_class):
        self.__bc = base_class

    def __load(self, app):
        """
        Inner method, load classes from a section.
        :param app: Section for start loading.
        :return: Generator of class objects.
        """
        for load_class in inspect.getmembers(app, inspect.isclass):
            if issubclass(load_class[1], self.__bc) and load_class[1] is not self.__bc \
                    and not inspect.isabstract(load_class[1]):
                logger.info(f"{load_class[1].__name__} loaded")
                yield load_class[1]

    def __call__(self, app) -> list:
        """
        Load classes from a section.
        :param app: Section for start loading.
        :return: List of class objects.
        """
        return list(self.__load(app))

def easy_subscribe(module, cast_type, bot):
    """
    Subscribes in a bot the class of the passed module childs by inheritance of cast_type.
    :param module: Module to search for classes.
    :param cast_type: Base class of classes to search.
    :param bot: Bot for making subscriptions.
    :return: None.
    """
    lmodule = import_module(module)
    bot.subscribe_all(*Loader(cast_type)(lmodule))

@Cached
def load_bot() -> Bot:
    """
    Main class of loader package, load and return a well formed bot from project
    :return: Bot
    """
    settings = load_settings()
    bot: Bot = Bot(settings.TOKEN, debug=settings.DEBUG)
    if settings.PROXY is not None and len(settings.PROXY) > 0:
        bot.proxy = settings.PROXY
    get_logger().info('Successfully created bot')

    def handle_app(app):
        for name, cast_type in [('middlewares', middleware.Middleware), ('handlers', handler.BaseHandler),
                                ('inlines', inline.InlineHandler)]:
            try:
                easy_subscribe(f'{app}.{name}', cast_type, bot)
            except (ImportError, TypeError):
                logger.warn(f"Can't find {name} file in {app}")
    for_app_do(handle_app)
    bot.setup()
    return bot


import math

import bson
from bson import ObjectId
from fastapi import APIRouter, HTTPException
from pydantic.types import constr
from starlette.requests import Request

from gflbans.internal.config import HOST, MONGO_DB
from gflbans.internal.database.infraction import DInfraction
from gflbans.internal.flags import PERMISSION_VIEW_IP_ADDR
from gflbans.internal.search import do_infraction_search
from gflbans.web.pages import sctx

from gflbans.internal.log import logger
from gflbans.internal.errors import SearchError

infractions_router = APIRouter()

@infractions_router.get('/')
async def infractions(request: Request):
    return request.app.state.templates.TemplateResponse('pages/infractions.html',
                                                        {**await sctx(request), 'page': 'infractions'})

@infractions_router.get('/{infraction_id}/')
async def preload_infraction(request: Request, infraction_id: str, search: constr(min_length=1, max_length=256) = None):
    sc = await sctx(request)

    try:
        obj_id = ObjectId(infraction_id)
    except bson.errors.InvalidId:
        raise HTTPException(detail='Invalid infraction ID', status_code=400)

    dinf = await DInfraction.from_id(request.app.state.db[MONGO_DB], obj_id)

    if dinf is None:
        raise HTTPException(detail='No such infraction exists', status_code=404)

    # Determine what page dinf is on

    s_dict = {'created': {'$gt': dinf.created}}

    if search:
        incl_ip = False

        if sc['user'] is not None:
            incl_ip = sc['user'].permissions & PERMISSION_VIEW_IP_ADDR == PERMISSION_VIEW_IP_ADDR

        try:
            cq, _ = await do_infraction_search(request.app, search, include_ip=incl_ip, strict=False)
        except SearchError:
            raise HTTPException(detail='Query failed to compile or took too long to execute', status_code=400)

        s_dict = {'$and': [s_dict, cq]}

    doc_num = await request.app.state.db[MONGO_DB].infractions.count_documents(s_dict)

    # The most recent infraction will have a doc_num of 1, so to get its index, just subtract 1
    doc_pos = doc_num - 1

    # doc_pos / docs_per_page floored + 1 is the page number that the document is on
    doc_page = (doc_pos // 30) + 1

    return request.app.state.templates.TemplateResponse('pages/infractions.html',
                                                        {**sc, 'page': 'infractions',
                                                         'infraction': dinf, 'set_page': doc_page,
                                                         'GB_HOST': HOST})


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from copy import deepcopy
import utils
import algorithms.modules as m
from algorithms.sac import SAC

class CURL(SAC):
	def __init__(self, obs_shape, action_shape, args):
		super().__init__(obs_shape, action_shape, args)
		self.aux_update_freq = args.aux_update_freq

		self.curl_head = m.CURLHead(self.critic.encoder).cuda()

		self.curl_optimizer = torch.optim.Adam(
			self.curl_head.parameters(), lr=args.aux_lr, betas=(args.aux_beta, 0.999)
		)
		self.train()

	def train(self, training=True):
		super().train(training)
		if hasattr(self, 'curl_head'):
			self.curl_head.train(training)

	def update_curl(self, x, x_pos, L=None, step=None):
		assert x.size(-1) == 84 and x_pos.size(-1) == 84

		z_a = self.curl_head.encoder(x)
		with torch.no_grad():
			z_pos = self.critic_target.encoder(x_pos)

		logits = self.curl_head.compute_logits(z_a, z_pos)
		labels = torch.arange(logits.shape[0]).long().cuda()
		curl_loss = F.cross_entropy(logits, labels)

		self.curl_optimizer.zero_grad()
		curl_loss.backward()
		self.curl_optimizer.step()
		if L is not None:
			L.log('train/aux_loss', curl_loss, step)

	def update(self, replay_buffer, L, step):
		obs, action, reward, next_obs, not_done, pos = replay_buffer.sample_curl()

		self.update_critic(obs, action, reward, next_obs, not_done, L, step)

		if step % self.actor_update_freq == 0:
			self.update_actor_and_alpha(obs, L, step)

		if step % self.critic_target_update_freq == 0:
			self.soft_update_critic_target()

		if step % self.aux_update_freq == 0:
			self.update_curl(obs, pos, L, step)


from models.person import Person
from models.prospectiveClient import ProspectiveClient
from models.client import Client
from models.blacklist import Blacklist
from models.account import Account
from models.lead import Lead
from models.campaign import Campaign
from resources.admin.security import AuthRequiredResource
from flask_restful import Resource
from flask import request, jsonify, render_template
from sqlalchemy.exc import SQLAlchemyError
from flask_mail import Message
import status
from app import db
import requests, json

class DniValidationResource(Resource):
	def post(self):
		requestDict = request.get_json()
		if not requestDict:
			response = {'error': 'No input data provided'}
			return response, status.HTTP_400_BAD_REQUEST

		documentNumber = requestDict['documentNumber']
		try:
			blacklisted = (Blacklist.query.filter_by(documentNumber=documentNumber).first())
			d = {}
			if(blacklisted):
				d['type'] = 3
				return d, status.HTTP_200_OK
			else:
				person = (Person.query.filter_by(documentNumber=documentNumber).first())
				if not person:
					d['error'] = "La persona ingresada no existe en los registros"
					return d, status.HTTP_400_BAD_REQUEST

				prospectiveClient = (ProspectiveClient.query.filter_by(idPerson=person.id).first())

				if not prospectiveClient:
					d['type'] = 2 #Es no cliente, aun no es prospecto
					d.update(person.toJson())
					nationality = json.loads(requests.get('https://restcountries.eu/rest/v2/alpha/' + person.nationality).text)
					d['nationality'] = nationality['name']
					d['flag'] = nationality['flag']
					return d, status.HTTP_200_OK

				client = (Client.query.filter_by(idProspectiveClient=prospectiveClient.id).first())

				if not client:
					d['type'] = 2 #Es no cliente, ya es prospecto
					d.update(person.toJson())
					nationality = json.loads(requests.get('https://restcountries.eu/rest/v2/alpha/' + person.nationality).text)
					d['nationality'] = nationality['name']
					d['flag'] = nationality['flag']
					return d, status.HTTP_200_OK

				d['type'] = 1
				d.update(person.toJson())
				d.update(prospectiveClient.toJson())
				d.update(client.toJson())
				d['activeCampaigns'] = False
				leads = Lead.query.filter_by(idClient=client.id)
				d['leads'] = []
				d['campaigns'] = []
				for lead in leads:
					lead = lead.toJson()
					if lead['active']:
						campaign = Campaign.query.get_or_404(lead['idCampaign'])
						if campaign.month != 'Diciembre':
							continue
						d['activeCampaigns'] = True
						d['leads'].append(lead)
						d['campaigns'].append(campaign.toJson())

				nationality = json.loads(requests.get('https://restcountries.eu/rest/v2/alpha/' + person.nationality).text)
				d['nationality'] = nationality['name']
				d['flag'] = nationality['flag']
				return d, status.HTTP_200_OK

		except SQLAlchemyError as e:
			db.session.rollback()
			response = {'error': str(e)}
			return response, status.HTTP_400_BAD_REQUEST