from typing import Any
from django.db import models
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, Http404, JsonResponse, HttpResponse
from django.urls import reverse
from django.views import generic
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from .models import Question, Choice, Vote, Category
from .serializers import CategorySerializer
from .forms import CreateQuestionForm, CreateChoiceForm
# Create your views here.

class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_object_name = "latest_question_list"

    def get_queryset(self):
        """Return last five published questions"""
        return Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")

def detail_view(request, pk):
    question = get_object_or_404(Question, pk=pk)
    if question.pub_date > timezone.now():
        raise Http404()
    if request.user.is_authenticated:
        if question.created_by == request.user:
            return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
        try:
            Vote.objects.get(voted_by=request.user, question=question)
            return render(request, "polls/results.html", {"question": question})
        except Vote.DoesNotExist:
            pass
    return render(request, "polls/details.html", {"question": question})

class Result(generic.DetailView):
    template_name = "polls/results.html"
    context_object_name = "question"

    def get_queryset(self):
        return Question.objects.filter(pub_date__lte=timezone.now())

def vote(request, question_id):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    question = get_object_or_404(Question, pk=question_id)

    try:
        selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
        return render(request, "polls/details.html",
                      {
                            "question": question,
                            "error_message": "You didn't select a choice"
                       })
    else:
        Vote.objects.create(question=question, voted_on=selected_choice, voted_by=request.user)
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse("polls:results", args=(question.id, )))

def new_poll(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    if request.method == "POST":
        created_by = request.user
        question_text = request.POST["question_text"]
        pub_date = timezone.now()
        new_question = Question(question_text=question_text, created_by=created_by, pub_date=pub_date, category_id=6)
        new_question.save()

        index = 1
        while True:
            value = request.POST.get(f"choice_text_{index}", None)
            print(value)
            if value:
                new_question.choice_set.create(choice_text=value)
            else:
                break
            index += 1

        return HttpResponseRedirect(reverse("polls:index"))

    elif request.method == "GET":
        q_form = CreateQuestionForm()

        return render(request, "polls/new_poll.html",
                      {
                        "question_form": q_form,
                      })

def delete_poll(request, pk):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    poll = get_object_or_404(Question, pk=pk)
    if request.method == "GET":
        return render(request, "polls/delete_poll.html", {"question": poll})

    elif request.method == "POST":
        if poll.created_by != request.user:
            return render(request, "polls/delete_poll.html", {
                "error_messages": ["Permission denied"],
                "question": poll
            })
        poll.delete()
    return HttpResponseRedirect(reverse("polls:index"))

def edit_poll(request, pk):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse("login:index"))

    poll = get_object_or_404(Question, pk=pk)

    if poll.created_by != request.user:
        return render(request, "polls/results.html", {"error_messages": ["You can edit this poll"], "question": poll})

    if request.method == "GET":
        q_form = CreateQuestionForm()
        q_form.data["question_text"] = poll.question_text
        return render(request, "polls/edit_poll.html", {
            "question_form": q_form,
            "question": poll
        })

    elif request.method == "POST":
        index = 1
        while True:
            value = request.POST.get(f"choice_text_{index}", None)
            if value:
                pass
            else:
                break

        poll.question_text = request.POST["question_text"]
        poll.save()
        return HttpResponseRedirect(reverse("polls:results", args=(poll.id,)))

class Categories(generic.ListView):
    template_name = "polls/all_categories.html"
    context_object_name = "categories"

    def get_queryset(self):
        return Category.objects.all().order_by("-pub_date")

def category(request, cat):
    polls = Question.objects.filter(category__category_name=cat, pub_date__lte=timezone.now()).order_by("-pub_date")
    return render(request, "polls/category.html", {"polls": polls, "category": cat})

def about(request):
    return render(request, 'polls/about.html', {})

@csrf_exempt
def category_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CategorySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


import requests
import os
slack_webhook_url = os.environ.get('SLACK_WEBHOOK_URL', None)
# This just the URL for an incoming webhook set up on slack.
# from config.secrets import slack_webhook_url

def eternal_season_slack_game_notifier(game):
    # accepts a game and sends it to slack
    payload = game.slack_report()
    requests.post(slack_webhook_url, json=payload)

def eternal_season_slack_league_notifier(league):
    # accepts a game and sends it to slack
    payload = league.slack_report()
    requests.post(slack_webhook_url, json=payload)

# An array of dictionaries.
#   function: a function that the game object should be passed to. It should notify or send something.
#   leagues: an array of leagues that
NOTIFIERS = [
    {
        'leagues': ['the-eternal-season'],
        'game_notifier': eternal_season_slack_game_notifier,
        'league_notifier': eternal_season_slack_league_notifier
    }
]


import re
import time
import threading
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from ui.test.record.Util import TFile
from ui.test.record import Configs
from ui.test.record import String
from  ui.test.record.UiView.settingsView import settingsView
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QAbstractItemView,QPushButton
from  ui.test.record.Util import TAdb
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QComboBox, QPushButton

class CustomComboBox(QComboBox):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.Devicelist = TAdb.get_device_ip_dict()
        default_value = TFile.get_config_value("CurrentDevice")
        if default_value in self.Devicelist:
            TAdb._device_name = default_value
            self.addItem(default_value)
        else:
            self.addItem('请选择设备号')
            TFile.set_config_file('', "CurrentDevice") # 如果不存在则将config文件清空

    def showPopup(self):
        self.clear()
        self.addItem('请选择设备号 ')
        self.Devicelist = TAdb.get_device_ip_dict()
        if not self.Devicelist:
            print("无设备，需要刷新")
            return
        for device in self.Devicelist:
            TAdb._device_name = device
            self.addItem(device)
            super().showPopup()

class ToolBarView(QWidget):
    console_signal = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.mainwindow = parent
        self.select_device = ''
        self.background_color = TFile.get_config_value('background_color')
        self.IniUi()

    def IniUi(self):
        self.toolbar_layout = QHBoxLayout()
        self.adb_combobox = CustomComboBox(self)
        self.adb_combobox.setMinimumWidth(250)
        self.renovate_button = QPushButton()
        self.run_button = QPushButton()
        self.settings_button = QPushButton()
        self.recordMode_button = QPushButton('开启录制模式')
        self.run_num = QPushButton('运行次数:%s' % TFile.get_run_num())
        self.run_button.setObjectName("Run")

        self.label = QLabel(" ")

        self.renovate_button.setIcon(QIcon("%s刷新.png"%TFile.get_scrcpy_icons_path()))
        self.run_button.setIcon(QIcon("%s运行.png"%TFile.get_scrcpy_icons_path()))
        self.settings_button.setIcon(QIcon("%s设置.png"%TFile.get_scrcpy_icons_path()))

        self.renovate_button.setFixedSize(35,35)
        self.run_button.setFixedSize(35,35)
        self.settings_button.setFixedSize(35,35)
        self.label.setMinimumWidth(800)

        self.adb_combobox.activated.connect(self.adb_combobox_connect)
        self.recordMode_button.clicked.connect(self.recordMode_connect)
        self.settings_button.clicked.connect(self.settings_connect)
        self.run_button.clicked.connect(self.run_connect)
        self.run_num.clicked.connect(self.run_num_connect)

        self.toolbar_layout.addWidget(self.adb_combobox, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.renovate_button, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.settings_button, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.run_button, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.recordMode_button, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.run_num, 0, Qt.AlignLeft | Qt.AlignTop)
        self.toolbar_layout.addWidget(self.label, 0, Qt.AlignRight)
        self.toolbar_layout.setSpacing(10)

        self.setLayout(self.toolbar_layout)

        self.settings = settingsView(self.mainwindow)

    def recordMode_connect(self):
        self.console_signal.emit(self.sender().text())
        if self.recordMode_button.text() == '开启录制模式':
            self.recordMode_button.setText('关闭录制模式')
        else:
            self.recordMode_button.setText('开启录制模式')

    def set_num(self):
        num = (re.findall(r'运行次数:(.*?)$', self.run_num.text())[0])
        if num:
            num = int(num) + 1
        else:
            num = 1
        self.console_signal.emit('*****运行次数:%s' % num)
        TFile.set_run_num(num)
        self.run_num.setText('运行次数:%s' % num)

    def adb_combobox_connect(self):
        thread = threading.Thread(target=self.adb_connect_thread)
        thread.start()

    def run_num_connect(self):
        self.settings = settingsView(self.mainwindow)
        self.settings.show()
        self.settings.list.setCurrentRow(1)  # 设置列表默认选中行

    def adb_connect_thread(self):
        self.select_device = self.adb_combobox.currentText()  # 获取选择项
        TFile.set_config_file(self.select_device, "CurrentDevice")
        if not self.select_device:
            return
        if self.select_device.count('.') < 2:  # 如果是ip则启动tcpip命令
            TAdb.start_tcpip(self.select_device)
        else:
            out = TAdb.adb_connect(self.select_device)
            if out.count('拒绝') > 0:
                self.console_signal.emit(out)
                return
            TAdb._device_name = self.select_device
            self.console_signal.emit(out)
        self.console_signal.emit('已连接设备：'+self.select_device)

    def renovate_connect(self):
        thread = threading.Thread(target=self.renovate_thread)
        thread.setDaemon(True)
        thread.start()

    def renovate_thread(self):
         self.device_dict = TAdb.get_device_ip_dict()
         for device in self.device_dict:
            self.adb_combobox.addItem(self.device_dict.get(device))
         self.adb_combobox.addItem(device)
         Configs.device_dict = self.device_dict

    def run_connect(self):
        self.mainwindow.FileTreeView.setSelectionMode(QAbstractItemView.NoSelection)  # 设置不可选择脚本文件
        self.console_signal.emit('手动点击:%s' % self.run_button.objectName())
        if self.run_button.objectName() == 'Run':
            self.recordMode_button.setText('开启录制模式')
            self.run_button.setIcon(QIcon("%s停止.png"%TFile.get_scrcpy_icons_path()))
            self.mainwindow.run_thread.run_thread_is_on = True
            self.mainwindow.run_thread.start()
            self.run_button.setObjectName("Stop")
        else:
            self.run_stop()

    def run_stop(self):
        self.run_button.setObjectName("Run")
        self.mainwindow.run_thread.run_thread_is_on = False
        self.mainwindow.run_thread.terminate()
        self.mainwindow.run_thread.quit()
        self.console_signal.emit('关闭测试')
        self.mainwindow.FileTreeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.run_button.setIcon(QIcon("%s运行.png"%TFile.get_scrcpy_icons_path()))

    def settings_connect(self):
        self.settings.show()


import asyncio

import cv2
import numpy as np
import websockets
import base64
import json
import os
# CORONA_TEMPLATE_PATH = os.path.dirname(os.path.abspath(__file__)) + '/characters/character-1.png'

CORONA_TEMPLATE_PATH = os.path.dirname(os.path.abspath(__file__)) + '/characters/character-1.png'

CORONA_SCALE_RATIO = 0.5

corona_template_image = cv2.imread(CORONA_TEMPLATE_PATH, 0)

corona_template_image = cv2.resize(corona_template_image, None, fx=CORONA_SCALE_RATIO, fy=CORONA_SCALE_RATIO)
hinhbs = []
hinhbs.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-6.png'))
hinhbs.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-5.png'))
hinh = []

hinh.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-4.png'))
hinh.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-3.png'))
hinh.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-2.png'))
hinh.append(cv2.imread(r'D:/CodePython/gotcha-corona-player/characters/character-1.png'))

def catch_doctor(wave_image, threshold=0.6):
    wave_image_gray = cv2.cvtColor(wave_image, cv2.COLOR_BGRA2GRAY)
    width, height = corona_template_image.shape[::-1]
    doctors = []

    # template_gray = cv2.cvtColor(hinh, cv2.COLOR_BGRA2GRAY)
    # cv2.imwrite(os.path.join(f'waves/test/', 'nghia1.jpg'), template_gray)
    for hinhmau in hinhbs:
        hinhmau = cv2.resize(hinhmau, None, fx=CORONA_SCALE_RATIO, fy=CORONA_SCALE_RATIO)
        template_gray = cv2.cvtColor(hinhmau, cv2.COLOR_BGRA2GRAY)

        res = cv2.matchTemplate(wave_image_gray, template_gray, cv2.TM_CCOEFF_NORMED)

        # min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        # if max_val > threshold:
        #     return []

        # top_left = max_loc
        # bottom_right = (top_left[0] + width, top_left[1] + height)
        loc = np.where( res >= threshold)
        for pt in zip(*loc[::-1]):
            doctors.append([pt,(pt[0] + width, pt[1] + height)])

    # return [[top_left, bottom_right]]
    return doctors

def catch_corona(wave_image, threshold=0.75):
    wave_image_gray = cv2.cvtColor(wave_image, cv2.COLOR_BGRA2GRAY)
    width, height = corona_template_image.shape[::-1]
    coronas = []

    # template_gray = cv2.cvtColor(hinh, cv2.COLOR_BGRA2GRAY)
    # cv2.imwrite(os.path.join(f'waves/test/', 'nghia1.jpg'), template_gray)
    for hinhmau in hinh:
        hinhmau = cv2.resize(hinhmau, None, fx=CORONA_SCALE_RATIO, fy=CORONA_SCALE_RATIO)
        template_gray = cv2.cvtColor(hinhmau, cv2.COLOR_BGRA2GRAY)

        res = cv2.matchTemplate(wave_image_gray, template_gray, cv2.TM_CCOEFF_NORMED)

        # min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        # if max_val > threshold:
        #     return []

        # top_left = max_loc
        # bottom_right = (top_left[0] + width, top_left[1] + height)
        loc = np.where( res >= threshold)
        for pt in zip(*loc[::-1]):
            coronas.append([pt,(pt[0] + width, pt[1] + height)])

    # return [[top_left, bottom_right]]
    return coronas

def base64_to_image(base64_data):
    encoded_data = base64_data.split(',')[1]
    nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)

    return img

async def play_game(websocket, path):
    print('Corona Killer is ready to play!')
    catchings = []
    last_round_id = ''
    wave_count = 0

    while True:

        ### receive a socket message (wave)
        try:
            data = await websocket.recv()
        except Exception as e:
            print('Error: ' + e)
            break

        json_data = json.loads(data)

        ### check if starting a new round
        if json_data["roundId"] != last_round_id:
            print(f'> Catching corona for round {json_data["roundId"]}...')
            last_round_id = json_data["roundId"]

        ### catch corona in a wave image
        wave_image = base64_to_image(json_data['base64Image'])
        doctors = catch_doctor(wave_image)

        results = catch_corona(wave_image)

        ### save result image file for debugging purpose
        for result in results:
            cv2.rectangle(wave_image, result[0], result[1], (0, 0, 255), 2)

        # waves_dir = f'waves/{last_round_id}/'
        # if not os.path.exists(waves_dir):
        #     os.makedirs(waves_dir)
        # waves_dir = f'waves/Tu/'
        # if not os.path.exists(waves_dir):
        #     os.makedirs(waves_dir)
        # cv2.imwrite(os.path.join(waves_dir, f'{json_data["waveId"]}.jpg'), wave_image)

        print(f'>>> Wave #{wave_count:03d}: {json_data["waveId"]}')
        wave_count = wave_count + 1
        ketqua=[]
        for result in results:
            i=0

            for doctor in doctors:
                if(((result[0][0] + result[1][0]) / 2 > doctor[0][0] and (result[0][0] + result[1][0]) / 2 < doctor[0][0])
                or ((result[0][1] + result[1][1]) / 2 > doctor[0][1] and (result[0][1] + result[1][1]) / 2 < doctor[1][1])):
                    break
                else :
                    i=i+1
            if(len(doctors)==i):
                ketqua.append(result)

        ### store catching positions in the list
        catchings.append({
            "positions": [

                {"x": (kq[0][0] + kq[1][0]) / 2, "y": (kq[0][1] + kq[1][1]) / 2} for kq in ketqua
            ],
            "waveId": json_data["waveId"]
        })

        ### send result to websocket if it is the last wave
        if json_data["isLastWave"]:
            round_id = json_data["roundId"]
            print(f'> Submitting result for round {round_id}...')

            json_result = {
                "roundId": round_id,
                "catchings": catchings,
            }

            await websocket.send(json.dumps(json_result))
            print('> Submitted.')

            catchings = []
            wave_count = 0

start_server = websockets.serve(play_game, "localhost", 8765, max_size=100000000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


import os
import codecs
import re
import math
import jieba
import operator

def freqword(word,file):  # 统计词频，并返回字典
    freword = 0
    for i in file:
        if word is i:
            freword=freword+1
    return freword

def wordinfilecount(word, filelist):  # 查出包含该词的文档数
    count = 0  # 计数器
    for i in filelist:
        if word in i:
            count = count+1
    return count

def tf_idf(filelist):  # 针对filelist中的每个词计算TF-IDF,并返回前500个
    outdic = {}
    for file in filelist:
        for word in file:
            dic = freqword(word,file)
            tf = dic/ len(file)
            idf = math.log(len(filelist) / (wordinfilecount(word, filelist)))
            tfidf = tf * idf  # 计算TF-IDF
            outdic[word]=tfidf
    orderdic = sorted(outdic.items(), key=operator.itemgetter(1), reverse=True)#给字典排序，第一个参数是将字典转化为可排序的列表，第二个参数是key,第三个参数是表示倒序
    return orderdic[:500]

def rlistsSpam(filepath):
    stoppath = "C:/Users/lenovo/Desktop/email/ting_yong_ci.txt"#停用词表
    stopwords = {}.fromkeys(open(stoppath).read())#建立停用词表字典
    str = filepath
    pattern = re.compile('[\u4e00-\u9fa5]+')#正则表达式匹配，unicode编码中[\u4e00-\u9fa5]表示汉字,+表示匹配1个或多个
    regex = re.compile(pattern)
    results = regex.findall(str)#提取出所有的汉字
    lists = []
    lists_spam=[]
    for result in results:
        k = jieba.cut(result,HMM=True)
        for i in k:
            if i not in stopwords:
                lists.append(i)

    for i in range(0,len(lists)):
        if lists[i] not in lists0_spam:
            lists0_spam.append(lists[i])
    for i in range(0,len(lists)):
        if lists[i] not in lists_spam:
            lists_spam.append(lists[i])
            if lists[i] not in stopwords:
                if lists[i] in frespam:
                    frespam[lists[i]]+=1#之前出现过，则加1
                else:
                    frespam[lists[i]]=1#之前未出现过则赋为1
    '''for i in range(0,len(lists)):
        if lists[i] not in lists0:
            lists0.append(lists[i])
    for i in range(0,len(lists0)):
        if i not in stopwords:
            if i in frespam:
                frespam[i]+=1#之前出现过，则加1
            else:
                frespam[i]=1#之前未出现过则赋为1'''
    return lists#返回的是邮件中所有的词

def rlistsHam(filepath):
    stoppath = "C:/Users/lenovo/Desktop/email/ting_yong_ci.txt"#停用词表
    stopwords = {}.fromkeys(open(stoppath).read())#建立停用词表字典
    str = filepath
    pattern = re.compile('[\u4e00-\u9fa5]+')#正则表达式匹配，unicode编码中[\u4e00-\u9fa5]表示汉字,+表示匹配1个或多个
    regex = re.compile(pattern)
    results = regex.findall(str)#提取出所有的汉字
    lists = []
    lists_ham=[]
    for result in results:
        k = jieba.cut(result,HMM=True)
        for i in k:
            if i not in stopwords:
                lists.append(i)

    for i in range(0,len(lists)):
        if lists[i] not in lists0_ham:
            lists0_ham.append(lists[i])
    for i in range(0,len(lists)):
        if lists[i] not in lists_ham:
            lists_ham.append(lists[i])
            if lists[i] not in stopwords:
                if lists[i] in freham:
                    freham[lists[i]]+=1#之前出现过，则加1
                else:
                    freham[lists[i]]=1#之前未出现过则赋为1
    '''for i in range(0,len(lists0)):
        if i not in stopwords:
            if i in freham:
                freham[i]+=1#之前出现过，则加1
            else:
                freham[i]=1#之前未出现过则赋为1'''
    return lists#返回的是邮件中所有的词

if __name__=='__main__':

    #DicS="C:/Users/lenovo/Desktop/email/dicS.txt"
    #DicH="C:/Users/lenovo/Desktop/email/dicH.txt"

    f = open("C:/Users/lenovo/Desktop/email/trec06c/full/index")
    frespam={}#建立词出现频率的字典
    freham={}
    filelistH=[]
    filelistS=[]
    lists0_spam=[]
    lists0_ham=[]
    i=0
    count_spam=0
    count_ham=0
    for line in f:
        i+=1
        if i == 3000:#2000行
            P_spam=count_spam/(i-1)#训练集中垃圾邮件出现的概率
            P_ham=count_ham/(i-1)#训练集中正常邮件出现的概率
            break
        line=f.readline()
        if "spam" in line:#如果是垃圾邮件
            count_spam+=1
            line1=line.replace("spam ..","C:/Users/lenovo/Desktop/email/trec06c")#将内容换掉是为了打开
            line1 = line1.replace("\n", "")#去掉回车符
            file = open(line1, 'r', errors='ignore')#以只读模式打开文件并且忽略错误
            file1 = file.read()#读取文件内容
            filelistS.append(rlistsSpam(file1))#进行训练,对垃圾邮件的词汇列表进行丰富,并且计算每个词出现的频率
            file.close()
        elif "ham" in line:#如果是正常邮件
            count_ham+=1
            line1=line.replace("ham ..","C:/Users/lenovo/Desktop/email/trec06c")
            line1=line1.replace("\n","")
            file=open(line1,'r',errors = 'ignore')
            file1=file.read()
            filelistH.append(rlistsHam(file1))
            file.close()

    f.close()
    dicS=tf_idf(filelistS)#进行排序，排出垃圾邮件特征中权重最高的100个并且储存起来
    dicH=tf_idf(filelistH)
    #print(str(dicS))
    S=open("C:/Users/lenovo/Desktop/email/try1_3000_500/dicS.txt",'w')#可写打开
    H=open("C:/Users/lenovo/Desktop/email/try1_3000_500/dicH.txt",'w')
    fres=open("C:/Users/lenovo/Desktop/email/try1_3000_500/fres.txt",'w')
    freh=open("C:/Users/lenovo/Desktop/email/try1_3000_500/freh.txt",'w')
    p_spam=open("C:/Users/lenovo/Desktop/email/try1_3000_500/p_spam.txt",'w')
    p_ham=open("C:/Users/lenovo/Desktop/email/try1_3000_500/p_ham.txt",'w')
    S.write(str(dicS))#写入
    S.close()
    H.write(str(dicH))
    H.close()
    #print(frespam)
    #print(freham)
    for thing in lists0_spam:
        #print(value)
        frespam[thing]=frespam[thing]/count_spam
        #print(value,"\n")
    for thing in lists0_ham:
        freham[thing]=freham[thing]/count_ham
    fres.write(str(frespam))
    fres.close()
    freh.write(str(freham))
    freh.close()
    p_spam.write(str(P_spam))
    p_spam.close()
    p_ham.write(str(P_ham))
    p_ham.close()
    print("train over")
    print(count_spam)
    print(count_ham)


from telethon import TelegramClient
from os import path
from time import sleep
import aiocron
from telethon.tl.types import PeerUser
import logging
logging.basicConfig(filename="log.txt", filemode="a",format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

config = {
    "apiID" : ...,
    "apiHash" : "",
    "botToken" : "",
    "directory" : r"etc/x-ui/x-ui.db", #The path of the file you want to send
    "adminUserId" : ..., # int
    "timeSleep" : 60 # min
}
class CheckFile:
    def __init__(self) -> None:
        self.directory = config["directory"]
    def check_file(self):
        if not path.exists(self.directory):
            text = "file zekr shode yaft nashod :|\n"
            for char in text:
                sleep(0.1)
                print(char, end= "", flush= True)
            exit()
        return True

CheckFile().check_file()

client = TelegramClient(
    session= "bot",
    api_id= config["apiID"],
    api_hash= config["apiHash"]
)

client.start(bot_token= config["botToken"])

@aiocron.crontab("*/{} * * * *".format(config["timeSleep"]))
async def main():
    CheckFile().check_file()
    try:
        await client.send_file(PeerUser(config["adminUserId"]), config["directory"], caption= 'with love')
    except Exception as ex:
        print(ex)

print("bot is online")
client.run_until_disconnected()


import serial
import time
import tkinter

# Define what happens when you push the  different buttons------------------------------------------------------------------------------------
def set_ActuatorSelection_left_state():
    global ActuatorSelection_state
    ActuatorSelection_state = 0
    ActuatorSelectionLabel.set("Left")

Automated_Controls_state = 0

def set_ActuatorSelection_right_state():
    global ActuatorSelection_state
    ActuatorSelection_state = 1
    ActuatorSelectionLabel.set("Right")

def set_ActuatorSelection_both_state():
    global ActuatorSelection_state
    ActuatorSelection_state = 2
    ActuatorSelectionLabel.set("Both")

def set_automated_controls_state():
    global Automated_Controls_state
    if Automated_Controls_state == 1:
        Automated_Controls_state = 0
        varLabel.set("Automated Controls: Off ")
    else:
        Automated_Controls_state = 1
        varLabel.set("Automated Controls: On ")

def set_ButtonUp_state():
    global Manual_Control_State

def set_ButtonDown_state():
    global Manual_Control_State

#Set up Serial Communication with Arduino---------------------------------------------------------------------------------------------------------------------------------
ser = serial.Serial('com5', 9600) #create Serial Object

time.sleep(3) #delay 3 seconds to allow serial com to get established
ser.write(bytes('L', 'UTF-8')) # send "L" to arduino to reset it when first connecting
print("Reset Arduino")

# Build GUI------------------------------------------------------------------------------------------------------------------------------------------------------------
tkTop = tkinter.Tk()  # Create GUI Box
tkTop.geometry('1200x800')  # size of GUI
tkTop.title("Test Stand Controller")  # title in top left of window

Title = tkinter.Label(text='Test Stand Controls', font=("Courier", 14, 'bold')).pack()  # Title on top middle of screen

# Fill in the Manual controls Side----------------------------------------------------------------------------------------------------------------------------------------
ManualFrame = tkinter.Frame(master=tkTop, width=600) # create frame for the manual controls
ManualLable = tkinter.Label(master=ManualFrame, text='Manual Controls',
                            font=("Courier", 12, 'bold')).pack()  # manual controls lable
ManualFrame.pack(fill=tkinter.BOTH, side=tkinter.LEFT, expand=True)

LeftButtonsFrame = tkinter.Frame(master=ManualFrame, width=100)
LeftButtonsLable = tkinter.Label(master=LeftButtonsFrame, text='Actuator Selection',
                                 font=("Courier", 12, 'bold')).pack()

RightButtonsFrame = tkinter.Frame(master=ManualFrame, width=100)
RightButtonsLable = tkinter.Label(master=RightButtonsFrame, text='Up/Down Controls',
                                  font=("Courier", 12, 'bold')).pack()

button_left_state = tkinter.Button(LeftButtonsFrame,
                                   text="Left",
                                   command=set_ActuatorSelection_left_state,
                                   height=4,
                                   fg="black",
                                   width=8,
                                   bd=5,
                                   activebackground='green'
                                   )
button_left_state.pack(side='top', ipadx=10, padx=10, pady=40)

button_right_state = tkinter.Button(LeftButtonsFrame,
                                    text="Right",
                                    command=set_ActuatorSelection_right_state,
                                    height=4,
                                    fg="black",
                                    width=8,
                                    bd=5,
                                    activebackground='green'
                                    )
button_right_state.pack(side='top', ipadx=10, padx=10, pady=40)

button_both_state = tkinter.Button(LeftButtonsFrame,
                                   text="Both",
                                   command=set_ActuatorSelection_both_state,
                                   height=4,
                                   fg="black",
                                   width=8,
                                   bd=5,
                                   activebackground='green'
                                   )
button_both_state.pack(side='top', ipadx=10, padx=10, pady=40)

ActuatorSelectionLabel = tkinter.IntVar()
ActuatorSelection = tkinter.Label(master=LeftButtonsFrame, textvariable=ActuatorSelectionLabel)
ActuatorSelection.pack()

button_up_state = tkinter.Button(RightButtonsFrame,
                                 text="Up",
                                 command=set_ButtonUp_state,
                                 height=4,
                                 fg="black",
                                 width=8,
                                 bd=5,
                                 activebackground='green'
                                 )
button_up_state.pack(side='top', ipadx=10, padx=10, pady=40)

button_down_state = tkinter.Button(RightButtonsFrame,
                                   text="Down",
                                   command=set_ButtonDown_state,
                                   height=4,
                                   fg="black",
                                   width=8,
                                   bd=5,
                                   activebackground='green'
                                   )
button_down_state.pack(side='top', ipadx=10, padx=10, pady=40)

LeftButtonsFrame.pack(fill=tkinter.BOTH, side=tkinter.LEFT, expand=True)
RightButtonsFrame.pack(fill=tkinter.BOTH, side=tkinter.LEFT, expand=True)

# Fill in the Automated controls Side----------------------------------------------------------------------------------------------------------------------------------------
AutoFrame = tkinter.Frame(master=tkTop, width=600, bg="gray")
AutoLable = tkinter.Label(master=AutoFrame, text='Automated Controls', font=("Courier", 12, 'bold'), bg="gray").pack(
    side='top', ipadx=10, padx=10, pady=40)  # Automated controls lable

button_Automated_on_off = tkinter.Button(AutoFrame,
                                         text="Turn Automated Controls on/off",
                                         command=set_automated_controls_state,
                                         height=4,
                                         fg="black",
                                         width=25,
                                         bd=5,
                                         activebackground='green'
                                         )
button_Automated_on_off.pack(side='top', ipadx=0, padx=0, pady=0)

varLabel = tkinter.IntVar()
tkLabel = tkinter.Label(master=AutoFrame, textvariable=varLabel, bg="gray")
tkLabel.pack()

TargetHeightLable = tkinter.Label(master=AutoFrame, text='Enter Target Height: ', font=("Courier", 12), bg="gray").pack(
    side='left', ipadx=10, padx=10, pady=40)  # Automated controls lable
TargetHeightEntry = tkinter.Entry(AutoFrame)
TargetHeightEntry.pack(side='left', ipadx=0, padx=0, pady=0)

AutoFrame.pack(fill=tkinter.BOTH, side=tkinter.LEFT, expand=True)

TargetHeight = TargetHeightEntry.get()

tkinter.mainloop() # run loop watching for gui interactions


#!/usr/bin/env python3
# This script was written and tested with Python 3.6.8

import os
import sys

from functools import reduce

def count_files_per_subdir_in(topLevelDir, filterExtensions=[]):
    results = {}

    subDirs = next(os.walk(topLevelDir))[1]
    for sd in sorted(subDirs):
        subDirPath = os.path.join(topLevelDir, sd)
        sdFilesCount = 0
        for root, dirs, files in os.walk(subDirPath):
            if not filterExtensions:
                sdFilesCount += len(files)
            else:
                sdFilesCount += count_files_with_extensions(files,
                    filterExtensions)

        results[sd] = sdFilesCount
        printLabel = ('Number of files in directory "%s" and its ' + \
            'sub-directories:') % subDirPath
        print('%s %i' % (printLabel.ljust(110), sdFilesCount))

    return results

def count_files_with_extensions(files, extensions):
    filesWithExts = filter(lambda f: get_file_extension(f) in extensions, files)
    return len(list(filesWithExts))

def get_file_extension(file):
    filePath, extension = os.path.splitext(file)
    if extension:
        extension = extension[1:]
    return extension

def map_results_to_tex_vars(results):
    subdirToVarname = {
        '__modelFileCounts': 'ReconstructedModelsCount',
        'customer-core': 'CustomerCoreFileCount',
        'customer-management-backend': 'CustomerManagementBackendFileCount',
        'customer-self-service-backend': 'CustomerSelfServiceBackendFileCount',
        'policy-management-backend': 'PolicyManagementBackendFileCount'
    }

    texVars = {}
    for (subdir, value) in results.items():
        varName = subdirToVarname[subdir]
        texVars[varName] = value
    return texVars

def write_tex_vars(targetFile, vars):
    with open(targetFile, 'w') as file:
        for (name, value) in vars.items():
            file.write('\def \zeval%s{%s}\n' % (name, str(value)))

if __name__ == '__main__':
    results = count_files_per_subdir_in('Evaluated LM Source Code')

    modelFileExtensions = ["data", "mapping", "services", "operation",
        "technology"]
    modelFileResults = count_files_per_subdir_in('Reconstructed Models',
        modelFileExtensions)
    results['__modelFileCounts'] = reduce(lambda v1, v2: v1 + v2,
        modelFileResults.values())

    if 'write_tex_vars' in sys.argv:
        texVars = map_results_to_tex_vars(results)
        write_tex_vars(os.path.join('..', 'tex_vars.tex'), texVars)


# import the necessary packages
from imutils.video import VideoStream
import imutils
import time
import cv2

# initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)

# loop over the frames from the video stream
while True:
	# grab the frame from the threaded video stream and resize it to
	# have a maximum width of 400 pixels
	frame = vs.read()
	frame = imutils.resize(frame, width=600)

	hsv = cv2.cvtColor(frame.copy(), cv2.COLOR_BGR2HSV)
	(h, s, v) = cv2.split(hsv)

	blurreds = cv2.GaussianBlur(s, (11, 11), 0)
	blurredv = cv2.GaussianBlur(v, (11, 11), 0)

	masks = cv2.inRange(blurreds, 230, 255)
	maskv = cv2.inRange(blurredv, 110, 150)
	maskvWhite = cv2.inRange(blurredv, 150, 255)

	bitwiseAnd = cv2.bitwise_and(masks, maskv)
	bitwiseOr = cv2.bitwise_or(bitwiseAnd, maskvWhite)

	kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (11,11))
	closing = cv2.morphologyEx(bitwiseOr, cv2.MORPH_CLOSE, kernel)
	opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
	closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)

	green = cv2.bitwise_or(frame, frame, mask=closing)
	cv2.imshow('green', green)

	key = cv2.waitKey(1) & 0xFF

	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break

print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.stop()


import datetime
from typing import List

from fastapi import APIRouter, Request
from pydantic import BaseModel, Field

from source.util.util_base.date_util import convert_date_to_datetime, obj_contain_datetime_convert_to_str
from source.util.util_data.basic_info import BasicInfo

symbol = APIRouter(prefix="/symbol", tags=["产品代码"])

class ActiveSymbolInfoRequest(BaseModel):
    data_date: datetime.date

class ActiveSymbolInfoResponse(BaseModel):
    ts_code: str
    exchange: str
    name: str

@symbol.post("/active_symbol_info", response_model=List[ActiveSymbolInfoResponse])
async def active_symbol_info(request: Request, symbol_info: ActiveSymbolInfoRequest):
    """
    获取当前生效的ts_code
    """
    db_conn = request.state.db_conn

    # data_date = convert_date_to_datetime(symbol_info.data_date)

    active_ts_code_info_raw = await BasicInfo(db_conn).get_active_ts_code_info(symbol_info.data_date)
    active_ts_code_info = []
    for ts_code, exchange, name in active_ts_code_info_raw:
        active_ts_code_info.append({
            "ts_code": ts_code,
            "exchange": exchange,
            "name": name
        })

    return active_ts_code_info

class SymbolCodeInfo(BaseModel):
    ts_code: str

@symbol.post("/get_main_ts_code_by_ts_code", response_model=str)
async def get_main_ts_code_by_ts_code(request: Request, symbol_info: SymbolCodeInfo):
    """
    使用ts_code获取其连续 ts_code 代码
    """
    db_conn = request.state.db_conn

    main_ts_code = await BasicInfo(db_conn).get_main_ts_code_by_ts_code(symbol_info.ts_code)

    return main_ts_code

class SymbolCodeInfo2(BaseModel):
    main_ts_code: str
    data_date: datetime.date

@symbol.post("/get_ts_code_by_main_ts_code", response_model=str)
async def get_ts_code_by_main_ts_code(request: Request, symbol_info: SymbolCodeInfo2):
    """
    使用连续ts_code代码获取其在日期对应的ts_code
    """
    # symbol_info.data_date = convert_date_to_datetime(symbol_info.data_date)
    db_conn = request.state.db_conn

    ts_code = await BasicInfo(db_conn).get_ts_code_by_main_ts_code(symbol_info.main_ts_code, symbol_info.data_date)

    return ts_code

class SymbolCodeInfo3(BaseModel):
    main_ts_code: str
    start_date: datetime.date
    end_date: datetime.date

@symbol.post("/get_contract_change_date_by_main_ts_code", response_model=List[str])
async def get_contract_change_date_by_main_ts_code(request: Request, symbol_info: SymbolCodeInfo3):
    """
    获取主力合约换约的日期
    """
    # symbol_info.start_date = convert_date_to_datetime(symbol_info.start_date)
    # symbol_info.end_date = convert_date_to_datetime(symbol_info.end_date)
    db_conn = request.state.db_conn

    result_ori = await BasicInfo(db_conn).get_ts_code_by_main_ts_code_with_date(symbol_info.main_ts_code, symbol_info.start_date, symbol_info.end_date)
    result_ori.sort(key=lambda x: x[0])

    date_list = []
    previous_mapping_ts_code = None
    for trade_date, mapping_ts_code in result_ori:
        if mapping_ts_code != previous_mapping_ts_code:
            date_list.append(trade_date)
            previous_mapping_ts_code = mapping_ts_code
    date_list = date_list[1:]

    return obj_contain_datetime_convert_to_str(date_list)

class SymbolCodeInfo4(BaseModel):
    main_ts_code: str = Field(..., example="A.DCE")

@symbol.post("/get_per_unit_by_fut_code", response_model=int)
async def get_per_unit_by_fut_code(request: Request, symbol_info: SymbolCodeInfo4):
    """
    获取单位点位变动价格
    """
    db_conn = request.state.db_conn

    fut_code = symbol_info.main_ts_code.split(".")[0]
    per_unit = await BasicInfo(db_conn).get_per_unit_by_fut_code(fut_code)
    return per_unit


"""
Air quality instrument control program.

Reads incoming data from serial instruments and stores it in a shared data
structure, which is subsequently written to log files.

Functions:

    main()

"""
import logging
import queue
import sys

import bocs_control.data_reader as dr
import bocs_control.data_writer as dw
import bocs_control.config as cfg

def main():
    """
    Main entry point for the program.
    """
    global_queue = queue.Queue()
    reader_threads = []
    for instrument in cfg.INSTRUMENTS:
        try:
            reader = dr.DataReader(
                f"{instrument}", f"/dev/{instrument}", global_queue
            )
        except RuntimeError:
            logging.error(
                f"Unable to connect to {instrument}, terminating execution."
            )
            sys.exit(1)

        reader_threads.append(reader)

    writer_thread = dw.DataWriter(global_queue)
    writer_thread.start()
    for thread in reader_threads:
        thread.start()

# ===============================================================================
if __name__ == "__main__":
    main()


import keras.backend as K
from data import *
import cv2
import argparse
from keras.applications import vgg16

def get_output_layer(model, layer_name):
  # get the symbolic outputs of each "key" layer (we gave them unique names).
  layer_dict = dict([(layer.name, layer) for layer in model.layers])
  layer = layer_dict[layer_name]
  return layer

def visualize_class_activation_map( img_path, output_path):
        model = vgg16.VGG16(weights='imagenet')
        original_img = cv2.imread(img_path, 1)
        original_img= cv2.resize(original_img,(224,224))
        import matplotlib.pyplot as plt

        print("original_img shape:",original_img.shape)
        width, height, _ = original_img.shape

        #Reshape to the network input shape (3, w, h).
        # img = np.array([np.transpose(np.float32(original_img), (2, 0, 1))])
        img = np.array([original_img])
        print("IMG  shape:",img.shape)
        #Get the 512 input weights to the softmax.
        class_weights = model.layers[-1].get_weights()[0]
        print("class_weights",class_weights.shape)
        final_conv_layer = get_output_layer(model, "block5_conv3")
        get_output = K.function([model.layers[0].input], [final_conv_layer.output])
        [conv_outputs] = get_output([img])
        print(conv_outputs.shape)
        conv_outputs = conv_outputs[0, :, :, :]
        print(conv_outputs.shape)
        print(class_weights.shape)
        #Create the class activation map.
        cam = np.zeros(dtype = np.float32, shape = conv_outputs.shape[0:2])
        for i, w in enumerate(class_weights[:512,2]):
                cam += w * conv_outputs[ :, :,i]
        # print("predictions", predictions)
        cam /= np.max(cam)
        plt.imshow(cam)
        plt.show()
        cam = cv2.resize(cam, (height, width))
        heatmap = cv2.applyColorMap(np.uint8(255*cam), cv2.COLORMAP_JET)
        heatmap[np.where(cam < 0.2)] = 0
        img = heatmap*0.5 + original_img
        cv2.imwrite(output_path, img)

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--train", type = bool, default = False, help = 'Train the network or visualize a CAM')
    parser.add_argument("--image_path", type = str, help = "Path of an image to run the network on")
    parser.add_argument("--output_path", type = str, default = "heatmap.jpg", help = "Path of an image to run the network on")
    parser.add_argument("--model_path", type = str, help = "Path of the trained model")
    parser.add_argument("--dataset_path", type = str, help = \
        'Path to image dataset. Should have pos/neg folders, like in the inria person dataset. \
        http://pascal.inrialpes.fr/data/human/')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
  args = get_args()
  visualize_class_activation_map("test.jpg", "out.png")


import abc
import functools
from typing import List

from ..common.child import Child

class BaseScheduler:
    """ Launches a set of tasks on a particular infrastructure """

    def __init__(self,
                 parent   : str,
                 interval : int = 5,
                 quiet    : bool = True) -> None:
        self.parent   = parent
        self.interval = interval
        self.quiet    = quiet

    @property
    @functools.lru_cache()
    def scheduler_id(self) -> str:
        return type(self).__name__.lower().replace("scheduler", "")

    @property
    @functools.lru_cache()
    def base_command(self) -> List[str]:
        return ["python3", "-m", "gator",
                "--parent", self.parent,
                "--interval", f"{self.interval}",
                "--scheduler", self.scheduler_id,
                ["--all-msg", "--quiet"][self.quiet]]

    def create_command(self, child : Child) -> str:
        """
        Build a command for launching a job on the compute infrastructure using
        details from the child object.

        :param child:   Describes the task to launch
        :returns:       String of the full command
        """
        return " ".join(self.base_command + ["--id", child.id,
                                             "--tracking", child.tracking.as_posix()])

    @abc.abstractmethod
    async def launch(self, tasks : List[Child]) -> None:
        """
        Launch all given tasks onto the compute infrastructure, this function is
        asynchronous but should return as soon as all tasks are launched (i.e.
        it should not block until tasks complete).

        :param tasks:   List of Child objects to schedule
        """
        return

    @abc.abstractmethod
    async def wait_for_all(self) -> None:
        """
        Wait for all tasks previously launched to complete by polling the compute
        infrastructure.
        """
        return


"""Base test module."""
from model_bakery import baker
from snapshottest.django import TestCase

class TestBase(TestCase):
    """Base test class."""

    def setUp(self):
        """Set up."""
        super().setUp()
        self.user = baker.make(
            "auth.User", first_name="Bob", last_name="Ndoe", email="bob@example.com"
        )
        self.staffprofile = baker.make("small_small_hr.StaffProfile", user=self.user)
        self.business = baker.make("locations.Business", name="X Inc")
        self.location = baker.make("locations.Location", name="Voi")
        self.department = baker.make("locations.Department", name="Science")


#referenced Wikipedia Ford Fulkerson Algorithm
import argparse
import networkflow
import xlsxwriter

parser = argparse.ArgumentParser(description='Produces a matching for ANova members.')
parser.add_argument('preferences', help="preferences input")
parser.add_argument('schools', help="schools input")

args = parser.parse_args()

def main():
	people = parse_preferences(args.preferences)
	schools = parse_schools(args.schools)
	school_names = [school[0] for school in schools]
	people_names = [person[0] for person in people]

	g = networkflow.FlowNetwork()
	[g.add_vertex(v) for v in ["s", "t"] + school_names + people_names]
	for person in people:
		name = person[0]
		preferences = person[1:]
		g.add_edge("s", name, 1)
		for preference in preferences:
			g.add_edge(name, preference, 1)

	for school in schools:
		name = school[0]
		flow = school[1]
		g.add_edge(name, "t", flow)

	matching = g.max_flow("s", "t", school_names)
	write_workbook(matching)

def write_workbook(matching):
	workbook = xlsxwriter.Workbook('anova_matching.xlsx')
	worksheet = workbook.add_worksheet()
	row = 0
	col = 0
	for item in matching:
		worksheet.write(row, col, item)
		for person in matching[item]:
			worksheet.write(row, col + 1, person)
			col += 1
		row += 1
		col = 0
	workbook.close()

def parse_preferences(preferences):
	people = []
	with open (preferences, "r") as file:
		for line in file:
			person = line.strip("\n ").split(",")
			people.append(person)
	return people

def parse_schools(schools):
	values = []
	with open (schools, "r") as file:
		for line in file:
			school = line.strip("\n ").split(",")
			school[1] = int(school[1])
			values.append(school)
	return values

if __name__ == "__main__":
	main()


import matplotlib.pyplot as plt
import sys, os
import numpy as np

benchmark = sys.argv[1]
num_bins = int(sys.argv[2])
min_val = float(sys.argv[3])
max_val = float(sys.argv[4])

filenames = ["cpython_capi","cpython_hpy","graalpython_capi", "graalpython_hpy"]
data = [[],[],[],[]]
bins = []
labels = filenames

datapath = "../" + benchmark + "/output/"
datafiles = []
for file in filenames:
    datafiles.append(open(datapath+file))

for i in range(4):
    for line in datafiles[i]:
        data[i].append(float(line))

for i in range(num_bins):
    bins.append(round(min_val+i/num_bins*(max_val-min_val),8))

plt.hist(data[0], bins, color='gray', label=labels[0], alpha=0.8)
plt.hist(data[1], bins, color='blue', label=labels[1], alpha=0.5)
plt.hist(data[2], bins, color='orange', label=labels[2], alpha=0.8)
plt.hist(data[3], bins, color='red', label=labels[3], alpha=0.5)
plt.legend()
plt.xlabel("Time [s]")
plt.ylabel("Frequency")
plt.show()


# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.

import argparse
import analyzer

def main():
    """
    The function to receive the arguments
    from the command line
    """

    parser = argparse.ArgumentParser(
        description="Analysis for perf workloads",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "-if",
        "--input_path",
        help="Path to the parquet file that contains generated requests",
        default="../generator/requests.parquet",
        type=str,
    )
    parser.add_argument(
        "-sf",
        "--send_file_path",
        help="Path to the parquet file that contains the submitted requests",
        default="../submitter/cpp_send.parquet",
        type=str,
    )
    parser.add_argument(
        "-rf",
        "--response_file_path",
        help="Path to the parquet file that contains the responses\
            from the submitted requests",
        default="../submitter/cpp_respond.parquet",
        type=str,
    )

    args = parser.parse_args()
    analyzer.default_analysis(
        args.input_path, args.send_file_path, args.response_file_path
    )

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Estoy usando server = ... para las dos funciones, buscar como mejorar eso"""

import xmlrpc.client
import gzip
import base64
import shutil
import os
from dotenv import load_dotenv
load_dotenv()
SECRET_UA = os.getenv("OPENSUBTITLE_USER_AGENT")

server = xmlrpc.client.ServerProxy("http://api.opensubtitles.org:80/xml-rpc")

def ConnectAPI():
    # Argumentos que la API necesita, se puede cambiar a los del usuario
    user = ""  # str(input("Usuario de OpenSubtitles: ")) ??
    pwd = ""  # str(input("Contraseña: ")) ??
    ua = SECRET_UA
    # Acceder a la api y tomar el token para validar los procesos
    return server.LogIn(user, pwd, "en", ua)  # json

def DisconnectAPI(login):
    if login:
        server.LogOut(login['token'])

def SearchAPI(movie_hash, movie_size, login, lang):
    search_data = [{"moviehash": movie_hash,
                    "moviebytesize": movie_size,
                    "sublanguageid": lang}]
    return server.SearchSubtitles(login["token"], search_data)  # json

def SearchName(name, season, episode, login, lang):
    search_data = [{"query": name,
                    "season": eval("int(season) if season != \"\" else \"\""),
                    "episode": eval("int(episode) if season != \"\" else \"\""),
                    "sublanguageid": lang}]
    return server.SearchSubtitles(login["token"], search_data)  # json

def ShowSubs(data):
    if not data["data"]:
        return []  # list
    else:
        sublist = []
        for i in data["data"]:
            sublist.append(i["MovieReleaseName"])
    return sublist  # list

def DownSubs(data, sub_index, subfile_name, login):
    subfile = server.DownloadSubtitles(login["token"],
                                       [data["data"][sub_index]["IDSubtitleFile"]])
    sub_format = data["data"][sub_index]["SubFormat"]
    if subfile:
        # Save file in the computer
        with open(subfile_name+"."+sub_format, "wb") as f:
            f.write(gzip.decompress(
                base64.b64decode(subfile["data"][0]["data"])))
        return True
    else:
        return False


import numpy as np
import matplotlib.pyplot as plt

import tools
import regression as reg
import statistics as stats
import franke_function as frank

import sklearn
from sklearn.model_selection import train_test_split

from bias_variance import bias_variance_analysis

def task_b_1():
    n = 1000 # 1000 test data points
    complexity = 20

    r, labels = frank.get_dataset(n, stddev = 1) # Noisy data
    x_train, y_train, x_test, y_test = tools.split_data(r, labels)

    input_scaler = tools.Scaler(x_train)
    scaled_train_x = input_scaler(x_train)
    scaled_test_x = input_scaler(x_test)

    train_mse = np.zeros(complexity)
    test_mse = np.zeros(complexity)

    # Recreate 2.11 in Hastie et al. Compute test/train MSE up to 20th order polynomial
    for p in range(complexity):
        model = reg.Linear(p) # linear regression

        model.fit(scaled_train_x, y_train)
        # compute train/test MSE
        train_preds = model.predict(scaled_train_x)
        test_preds = model.predict(scaled_test_x)
        # Compute statistics
        train_mse[p] = stats.mse(train_preds, y_train)
        test_mse[p] = stats.mse(test_preds, y_test)

    plt.semilogy(np.arange(complexity), test_mse, '-o', label = 'Test', linewidth = 1)
    plt.semilogy(np.arange(complexity), train_mse, '-o', label = 'Train', linewidth = 1)
    plt.xticks(np.arange(0,complexity, 2))
    plt.legend(frameon = False)

    plt.xlabel('Model Complexity (Polynomial degree)', fontsize = 12)
    plt.ylabel('Mean Squared Error', fontsize = 12)
    plt.savefig('./results/task_b_train_test_mse.png')
    plt.close()

def task_b_2():
    options = {'n': 1000, 'complexity': np.arange(20), 'stddev': 0, 'n_bootstraps': 100}
    model = lambda p: reg.Linear(p) # model constructor function
    test_mse, test_bias, test_var = bias_variance_analysis(model, options)
    # run bias variance analysis
    title = './results/task_b_bias_variance.png'

    degrees = options['complexity']
    plt.semilogy(degrees, test_mse,'-o', label = 'MSE', linewidth = 1)
    plt.semilogy(degrees, test_bias,'-o', label = 'Bias', linewidth = 1)
    plt.semilogy(degrees, test_var, '-o', label = 'Variance', linewidth = 1)
    plt.xlabel('Polynomial Degree', fontsize = 12)
    plt.legend(frameon = False)
    plt.savefig(title)

if __name__ == '__main__':
    task_b_1()
    task_b_2()


from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, String, Integer, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql import Select, desc
from sqlalchemy.engine import Result
import uuid

from database.database import Base

class Pricestamp(Base):
    __tablename__ = "pricestamps"
    pricestamp_idx = Column(UUID, primary_key=True, default=uuid.uuid4)
    ticker = Column(String(8), nullable=False)
    price = Column(Integer, nullable=False)
    timestamp = Column(Integer, nullable=False)

    @staticmethod
    async def create_pricestamp(ticker: str, price: int, timestamp: int, session: AsyncSession):
        new_pricestamp = Pricestamp(ticker=ticker, price=price, timestamp=timestamp)
        session.add(new_pricestamp)
        try:
            await session.commit()
            print(f"Pricestamp {str(new_pricestamp.pricestamp_idx)} has been saved")
            return new_pricestamp
        except:
            await session.rollback()
            raise

    @staticmethod
    async def get_pricestamps(session: AsyncSession, ticker: str, min_timestamp: int = None, max_timestamp: int = None):
        query: Select = select(Pricestamp)
        query: Select = query.filter(Pricestamp.ticker == ticker)
        query: Select = query.filter(Pricestamp.timestamp >= min_timestamp) if min_timestamp is not None else query
        query: Select = query.filter(Pricestamp.timestamp <= max_timestamp) if max_timestamp is not None else query
        query: Select = query.order_by(Pricestamp.timestamp)
        query_result: Result = await session.execute(query)
        return query_result.scalars().all()

    @staticmethod
    async def get_last_pricestamp(session: AsyncSession, ticker: str):
        query: Select = select(Pricestamp)
        query: Select = query.filter(Pricestamp.ticker == ticker)
        query: Select = query.order_by(desc(Pricestamp.timestamp))
        query: Select = query.limit(1)
        query_result: Result = await session.execute(query)
        return query_result.scalars().first()


import telebot
from json import load
from random import choice
from dotenv import load_dotenv
from os import getenv

load_dotenv()
bot = telebot.TeleBot(getenv('TELEGRAM_BOT_TOKEN'))
phrases = load(open('phrases.json'))
phrase_count = len(phrases)

@bot.message_handler(commands=['start'])
def main(message):
    bot.send_message(
        message.chat.id,
        'Это чат-бот имитирующий робота Бендера из Футурамы. Что бы начать просто отправь мне сообщение')

@bot.message_handler(content_types=['text'])
def reply(message):
    bot.send_message(message.chat.id, choose_phrase(message))

def choose_phrase(message):
    for w in message.text.lower().split():
        for phrase in phrases:
            for word in phrase.lower().split():
                if w in word:
                    return phrase
    return choice(phrases)

bot.polling()


import torch
import types
import os
from tqdm import tqdm
import numpy as np

from detectron2.config import get_cfg
from detectron2.data.detection_utils import read_image
from detectron2.projects.deeplab import add_deeplab_config
import glob
from mask2former import add_maskformer2_config
from predictor import VisualizationDemo
import matplotlib
import matplotlib.pyplot as plt
import argparse

import json

def setup_cfg(args):
    # load config from file and command-line arguments
    cfg = get_cfg()
    add_deeplab_config(cfg)
    add_maskformer2_config(cfg)
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()
    return cfg

MASK2FORMER_CONFIG_FILE = "./maskformer2_swin_large_IN21k_384_bs16_100ep.yaml"
MASK2FORMER_WEIGHTS_FILE = "./model_final_e5f453.pkl"

if __name__ == "__main__":
    torch.autograd.set_grad_enabled(False)

    parser = argparse.ArgumentParser(description="Specify dirs")
    parser.add_argument("--scene_dir_path", default="./masked_rdp_data/", type=str)
    parser.add_argument("--save_dir_path", default="./maskformer_masks/", type=str)
    args = parser.parse_args()

    scene_dir = args.scene_dir_path
    save_dir = args.save_dir_path

    os.makedirs(os.path.join(save_dir), exist_ok=True)

    for scan in tqdm(os.listdir(scene_dir)):
        os.makedirs(os.path.join(save_dir, scan), exist_ok=True)

        rgb_list = glob.glob(os.path.join(scene_dir, scan, "*png"))

        for img2_idx in range(len(rgb_list)):
            IMGFILE = os.path.join(scene_dir, scan, str(img2_idx) + ".png")
            MASK_LOAD_FILE = os.path.join(save_dir, scan, str(img2_idx) + ".pt")
            LOAD_IMG_HEIGHT = 512
            LOAD_IMG_WIDTH = 512

            cfgargs = types.SimpleNamespace()
            cfgargs.imgfile = IMGFILE
            cfgargs.config_file = MASK2FORMER_CONFIG_FILE
            cfgargs.opts = ["MODEL.WEIGHTS", MASK2FORMER_WEIGHTS_FILE]

            cfg = setup_cfg(cfgargs)
            demo = VisualizationDemo(cfg)

            img = read_image(IMGFILE, format="BGR")

            predictions, visualized_output = demo.run_on_image(img)
            masks = torch.nn.functional.interpolate(
                predictions["instances"].pred_masks.unsqueeze(0), [LOAD_IMG_HEIGHT, LOAD_IMG_WIDTH], mode="nearest"
            )
            masks = masks.half()
            torch.save(masks[0].detach().cpu(), MASK_LOAD_FILE)


import pytest
import glia
import numpy as np
from uuid import uuid4
import tracemalloc
import os
import requests
import yaml
from bs4 import BeautifulSoup
import linecache
from random import randint
# from data.stimulus_list import gratings_stimulus_list

@pytest.fixture(scope="module")
def sampling_rate():
    return 25000

@pytest.fixture(scope="module")
def unit_spike_trains():
    return glia.read_spyking_results("tests/data/gratings.result.hdf5", sampling_rate())

@pytest.fixture(scope="module")
def stimulus_start_times():
    return glia.get_stimulus_start_times("tests/data/gratings.analog")

@pytest.fixture(scope="module")
def stimulus_list():
    return gratings_stimulus_list

@pytest.fixture(scope="module")
def spike_train(unit_spike_trains):
    return unit_spike_trains["temp_16"]

@pytest.fixture(scope="module")
def units():
    return read_plexon_txt_file("tests/data/E1_R1_DAD_45min_movingbar.txt", uuid4())

@pytest.fixture(scope="module")
def plexon_txt_filepath():
    return "tests/data/E1_R1_DAD_45min_movingbar.txt"

@pytest.fixture(scope="module")
def units():
    total_time = 10000
    retina_id = "TEST"
    units = {}
    for channel_x in range(1,3):
        for channel_y in range(1,3):
            for unit_j in range(1,randint(1,5)):
                u = glia.hz_unit(total_time, 60, retina_id,
                          (channel_x, channel_y), unit_j)
                units[u.id] = u

    return units

@pytest.fixture(scope="module")
def unit():
    return next(iter(units().values()))

@pytest.fixture(scope="module")
def spike_train():
    return next(iter(units().values())).spike_train

@pytest.fixture(scope="module")
def stimulus_list():
    return glia.load_stimulus("tests/data/160615/E1_R1_DAD_55min_contrastgratings.stimulus")

def display_top(snapshot, group_by='lineno', limit=10):
    snapshot = snapshot.filter_traces((
        tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
        tracemalloc.Filter(False, "<unknown>"),
    ))
    top_stats = snapshot.statistics(group_by)

    print("Top %s lines" % limit)
    for index, stat in enumerate(top_stats[:limit], 1):
        frame = stat.traceback[0]
        # replace "/path/to/module/file.py" with "module/file.py"
        filename = os.sep.join(frame.filename.split(os.sep)[-2:])
        print("#%s: %s:%s: %.1f KiB"
              % (index, filename, frame.lineno, stat.size / 1024))
        line = linecache.getline(frame.filename, frame.lineno).strip()
        if line:
            print('    %s' % line)

    other = top_stats[limit:]
    if other:
        size = sum(stat.size for stat in other)
        print("%s other: %.1f KiB" % (len(other), size / 1024))
    total = sum(stat.size for stat in top_stats)
    print("Total allocated size: %.1f KiB" % (total / 1024))

eyecandy_url = 'http://localhost:3000'
@pytest.fixture(scope="module")
def programs_notebook():
    s = requests.Session()
    index = s.get(eyecandy_url)
    soup = BeautifulSoup(index.content)
    raw_programs = soup.select("select[name=program] option")
    programs = list(filter(lambda x: x!="custom",
                    [p["value"] for p in raw_programs]))

    s.post(eyecandy_url + '/window',
                      headers={
                           'windowHeight': "1140",
                           'windowWidth': "912",
                           })

    lab_notebook_str = ""
    for p in programs:
        r = s.post(eyecandy_url + '/start-program',
                         data={
                              'filename': p,
                              'program': p,
                              'seed': "12345",
                              'submitButton': 'start',
                              })
        if r.status_code != 200:
            raise(ValueError(f"Internal Server Error for {p}"))
        lab_notebook_str+=r.text
        lab_notebook = list(yaml.safe_load_all(lab_notebook_str))

    return (programs, lab_notebook)


import tensorflow as tf
import tensorflow_hub as hub
from sklearn.metrics.pairwise import cosine_similarity

import numpy as np
import pandas as pd
from langchain.embeddings import OpenAIEmbeddings

import json
import os
with open('../key.json') as f:
    keys = json.load(f)
os.environ['OPENAI_API_KEY'] = keys['Open-AI']

# Simple spambot function, not memory persistent, cant be scaled/subclassed/reused
def spam_bot(query:str, spam_database:np.array):
    """accepts
            a query string and compares it's embedding to
            a known set of spam
        returns
            spam/no spam,
            pseudo-confidence score,
    """
    threshold = 0.5235092

    model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
    query_embed = model([query])

    similarities = cosine_similarity(spam_database, query_embed)
    idx = similarities.flatten().argmax()
    score = similarities[idx]
    return score > threshold, score

# Spambot class for development
class SpamBot:
    def __init__(self, spam_dataset=None):
        self.threshold = 0.5235092
        self.model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
        if spam_dataset:
            self.embeddings = self.create_embeddings()
        else:
            self.embeddings = None

    def create_embeddings(self, data:pd.DataFrame) -> np.array:
        embeddings = []
        for _, line in data[:-1].iterrows():  # all but the last fella
            embeddings.append(model(line).numpy()[0])
        self.embeddings = np.array(embeddings)

    def detect_spam(self, query:str):
        if self.embeddings is None:
            raise Exception('Spam embedding database not yet created!')

        query_embed = model([query])
        similarities = cosine_similarity(embeddings, query_embed)
        idx = similarities.flatten().argmax()
        score = similarities[idx]
        return score>self.threshold, score

if __name__=='__main__':

    ''' Scripting example '''
    data = pd.read_excel('data/Crypto Scams in telegram.xlsx')
    inputs = data.values
    # model = hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128/2")
    open_ai = OpenAIEmbeddings()

    queries = []
    for _, line in data[:-1].iterrows():  # all but the last fella
        queries.append(str(line))
    a = open_ai.embed_documents(queries)

    max_scores = []
    for i in range(len(data)):
        test_data = data.iloc[i]
        train_data = data.drop(i).reset_index(drop=True)
        # embeddings = []
        # for _, line in train_data[:-1].iterrows(): #all but the last fella
        #     embeddings.append(model(line).numpy()[0])

        queries = []
        for _, line in train_data[:-1].iterrows():  # all but the last fella
            queries.append(str(line))
        embeddings = open_ai.embed_documents(queries)

        embed_array = np.array(embeddings)

        query = test_data
        # query_embed = model(query)
        query_embed = open_ai.embed_documents([str(query)])

        similarities = cosine_similarity(embeddings, query_embed)

        idx = similarities.flatten().argmax()
        score = similarities[idx]
        if score < 0.4:
            print(data.iloc[idx])
        max_scores.append(score)

    example_spam = data.iloc[idx]
    print(example_spam)

    print(query)
    std = np.std(max_scores)
    mean = np.mean(max_scores)
    lower_thresh = mean - std

    # """ It's managed to find a nearly identical piece of spam """
    #
    # ''' Function example '''
    # is_spam, score = spam_bot('Hey come get this 10/10 100% pump free guaruanteed profits',
    #              embed_array)
    #
    #
    #
    # ''' Class Example '''
    # bot = SpamBot()
    # try:
    #     bot.detect_spam('Promise it is not a scam')
    # except Exception as e:
    #     print(e)
    #     pass
    #
    # bot.create_embeddings(data.iloc[:-1])
    # bot.detect_spam('Yo man what is the price of eth?')
    #
    #
    #


import numpy as np, cv2

def contain(p, shape):                              # 좌표(y,x)가 범위내 인지 검사
    return 0<= p[0] < shape[0] and 0<= p[1] < shape[1]

def translate(img, pt):
    dst = np.zeros(img.shape, img.dtype)            # 목적 영상 생성
    for i in range(img.shape[0]):                           # 목적 영상 순회 - 역방향 사상
        for j in range(img.shape[1]):
            x, y = np.subtract((j, i) , pt)
            if contain((y, x), img.shape):
                dst[i, j] = img[y, x]
    return dst

def onMouse(event, x, y, flags, param):
    global pt1, pt2, mouse_mode

    if event == cv2.EVENT_LBUTTONUP:  # 왼쪽 버튼 떼기
        pt2 = (x, y)                        # 종료좌표 저장
        mouse_mode = 1                      # 버튼 떼기 상태 지정
        dx, dy = np.subtract(pt2, pt1).astype(int)
        dst = translate(image, (dx, 10))
        cv2.imshow(title, dst)

    elif event == cv2.EVENT_LBUTTONDOWN:  # 왼쪽 버튼 누르기
        pt1 = (x, y)  # 시작좌표 저장
        mouse_mode = 2

    if mouse_mode >= 2:  # 왼쪽 버튼 누르기 또는 드래그
        pt2 = (x, y)
        tmp = np.copy(image)
        cv2.line(tmp, pt1, pt2, (255, 0, 0), 2)
        cv2.imshow(title, tmp)

image = cv2.imread('images/rotate.jpg', cv2.IMREAD_COLOR)
mouse_mode = 0

title = "ex14"
cv2.imshow(title, image)  # 윈도우에 영상 띄우기
cv2.setMouseCallback(title, onMouse)  # 마우스 콜백 함수 등록
cv2.waitKey(0)


import sys
sys.path.append('/home/wangzihang/FL-DP/')
from FL_and_DP.fl_utils.center_average_model_with_weights import set_averaged_weights_as_main_model_weights, \
    set_averaged_weights_as_main_model_weights_fully_averaged
from FL_and_DP.fl_utils.local_clients_train_process import local_clients_train_process_without_dp_one_epoch, \
    local_clients_train_process_without_dp_one_batch, local_clients_train_process_one_epoch_with_ldp_PM
from FL_and_DP.fl_utils.send_main_model_to_clients import send_main_model_to_clients
from data.fed_data_distribution.dirichlet_nonIID_data import fed_dataset_NonIID_Dirichlet
from FL_and_DP.fl_utils.optimizier_and_model_distribution import create_model_optimizer_criterion_dict
from data.fed_data_distribution.pathological_nonIID_data import pathological_split_noniid
from data.get_data import get_data
from model.modelUtil import mnist_fully_connected, mnist_fully_connected_IN, mnist_fully_connected_IN1,Cifar10CNN,Cifar10CNN_IN,Cifar10CNN_IN1,ResNet18,ResNet18_IN,ResNet18_IN1
from train_and_validation.validation import validation
import torch
import matplotlib.pyplot as plt
import argparse
import pandas as pd
import math

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('--data', type=str, default='mnist',
                        choices=['mnist', 'cifar10', 'cifar100', 'fmnist', 'emnist', 'purchase', 'chmnist'])
    parser.add_argument('--client', type=int, default=10)
    parser.add_argument('--batchsize', type=int, help='the number of class for this dataset', default=64)
    parser.add_argument('--epoch', type=int, default=1)
    parser.add_argument('--iters', type=int, default=100)

    parser.add_argument('--lr', type=float, default=1e-2,
                        help='learning rate')
    parser.add_argument('--alpha', type=float, default=0.5,
                        help='狄立克雷的异质参数')
    parser.add_argument('--seed', type=int, default=1,
                        help='随机种子')
    parser.add_argument('--sr', type=float, default=0.1,
                        help='采样率')
    parser.add_argument('--eps', type=float, default=0,
                        help='隐私预算')
    parser.add_argument('--personal', type=int, default=0,
                        help='是否用个性化模型')
    parser.add_argument('--ptype', type=str, default='no',
                        help='是否用双个性化模型')
    parser.add_argument('--usedp', type=int, default=0,
                        help='是否用dp')
    args = parser.parse_args()
    return args

def fed_avg(train_data, test_data, number_of_clients, learning_rate, momentum, numEpoch, iters, alpha, seed, q, per,
            ptype, usedp, epsilon):
    epoch_list = []
    acc_list = []
    # 客户端的样本分配
    clients_data_list, weight_of_each_clients, batch_size_of_each_clients = fed_dataset_NonIID_Dirichlet(train_data,
                                                                                number_of_clients,
                                                                                alpha, seed, q)
    # clients_data_list, weight_of_each_clients,batch_size_of_each_clients =pathological_split_noniid(train_data,number_of_clients,alpha,seed,q)

    # 初始化中心模型,本质上是用来接收客户端的模型并加权平均进行更新的一个变量
    center_model = mnist_fully_connected(10)
    #center_model = ResNet18()
    all_train_loss=[]
    # 各个客户端的model,optimizer,criterion的分配

    if per == 0:
        clients_model_list, clients_optimizer_list, clients_criterion_list = create_model_optimizer_criterion_dict(
            number_of_clients, learning_rate, center_model)

    else:
        if ptype == 'liner':
            clients_model_list, clients_optimizer_list, clients_criterion_list = create_model_optimizer_criterion_dict(
                number_of_clients, learning_rate, mnist_fully_connected_IN(10))
            #clients_model_list, clients_optimizer_list, clients_criterion_list = create_model_optimizer_criterion_dict(number_of_clients, learning_rate, ResNet18_IN())
        if ptype == 'double':
            clients_model_list, clients_optimizer_list, clients_criterion_list = create_model_optimizer_criterion_dict(
                number_of_clients, learning_rate, mnist_fully_connected_IN1(10))
            # clients_model_list, clients_optimizer_list, clients_criterion_list = create_model_optimizer_criterion_dict(number_of_clients, learning_rate, ResNet18_IN1())
    test_dl = torch.utils.data.DataLoader(
        test_data, batch_size=256, shuffle=False)

    print("联邦学习整体流程开始-------------------")
    test_accuracy_record = []
    test_loss_record = []

    for i in range(iters):

        print("现在进行和中心方的第{:3.0f}轮联邦训练".format(i + 1))

        if usedp == 0:
            train_loss = local_clients_train_process_without_dp_one_epoch(number_of_clients, clients_data_list, clients_model_list,
                                                             clients_criterion_list, clients_optimizer_list, numEpoch,
                                                             q)
            all_train_loss.append(train_loss)
        else:
            train_loss = local_clients_train_process_one_epoch_with_ldp_PM(number_of_clients, clients_data_list, clients_model_list,
                                                              clients_criterion_list, clients_optimizer_list, numEpoch,
                                                              q, epsilon)
            all_train_loss.append(train_loss)

        main_model = set_averaged_weights_as_main_model_weights(center_model, clients_model_list,
                                                                weight_of_each_clients)
        '''
        if i==iters-1:
            print('train finish')
            with torch.no_grad():
                for j in range(len(clients_model_list)):
                    if j ==9:
                        for key, value in clients_model_list[j].state_dict().items():
                            #if 'norm'  in key or 'bn' in key or 'downsample.1' in key:  # 这个downsample是resnet里特有的，norm就是个性化层
                            print(j)
                            print(key)
                            print(value)
                print('global model')
                for key, value in main_model.state_dict().items():
                    # if 'norm'  in key or 'bn' in key or 'downsample.1' in key:  # 这个downsample是resnet里特有的，norm就是个性化层
                    print(key)
                    print(value)
        '''
        clients_model_list = send_main_model_to_clients(center_model, clients_model_list)
        '''
        if i==iters-1:
            print('fuhe')
            with torch.no_grad():
                for j in range(len(clients_model_list)):
                    if j ==9:
                        print('local model')
                        for key, value in clients_model_list[j].state_dict().items():
                            #if 'norm'  in key or 'bn' in key or 'downsample.1' in key:  # 这个downsample是resnet里特有的，norm就是个性化层
                            print(j)
                            print(key)
                            print(value)
        '''
        '''
        if per == 1:
            for j in range(len(clients_model_list)):
                p_test_loss, p_test_accuracy = validation(clients_model_list[j], test_dl)
                print(
                    f'第{j + 1}个客户端模型' f'Test set: Average loss: {p_test_loss:.4f}, 'f'Accuracy: ({p_test_accuracy:.2f}%)')
        '''
        # 查看效果中心方模型效果
        test_loss, test_accuracy = validation(main_model, test_dl)
        print(f'服务器模型:')
        print(f'Test set: Average loss: {test_loss:.4f}, 'f'Accuracy: ({test_accuracy:.2f}%)')

        test_loss_record.append(test_loss)
        test_accuracy_record.append(test_accuracy)

        epoch_list.append(i + 1)
        acc_list.append(test_accuracy)

    plt.figure(figsize=(24, 16))
    plt.plot(epoch_list, acc_list)
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.xticks(range(0, 101, 10), rotation=45)
    plt.yticks(range(0, 101, 5), rotation=45)
    plt.savefig('./result/fedavg_result_' + 'iters' + str(iters) + '_appha' + str(alpha) + '_clients' + str(
        number_of_clients) + '_lr' + str(learning_rate) + '_personal' + str(per) + '_ptype_' + str(
        ptype) + '_usedp' + str(usedp) + '_eps' + str(epsilon) + '.png')
    data = {'Epoch': epoch_list, 'Accuracy': acc_list, 'test_loss': test_loss_record}
    df = pd.DataFrame(data)
    df.to_csv('./result/fedavg_result_' + 'iters' + str(iters) + '_appha' + str(alpha) + '_clients' + str(
        number_of_clients) + '_lr' + str(learning_rate) + '_personal' + str(per) + '_ptype_' + str(
        ptype) + '_usedp' + str(usedp) + '_eps' + str(epsilon) + '.csv', index=False)
    print(all_train_loss)
    # record=[iters,numEpoch,test_loss_record,test_accuracy_record]

    # torch.save(record, "../record/{}.pth".format(int(numEpoch)))

if __name__ == "__main__":
    args = parse_arguments()
    train_data, test_data = get_data(args.data, augment=False)
    # print(train_data.data)
    #
    # print(train_data.__dict__)
    batch_size = args.batchsize  # 小批量
    learning_rate = args.lr  # 学习率
    numEpoch = args.epoch  # 客户端本地下降次数
    number_of_clients = args.client  # 客户端数量
    momentum = 0.9  # 动量
    iters = args.iters  # 联邦学习中的全局迭代次数
    alpha = args.alpha  # 狄立克雷的异质参数
    seed = args.seed  # 随机种子
    q_for_batch_size = args.sr  # 基于该数据采样率组建每个客户端的batchsize
    epsilon = args.eps

    per = args.personal
    ptype = args.ptype
    usedp = args.usedp
    fed_avg(train_data, test_data, number_of_clients, learning_rate, momentum, numEpoch, iters, alpha, seed,
            q_for_batch_size, per, ptype, usedp, epsilon)


import requests
import datetime
from pyquery import PyQuery as pq

def getUserNameFromId(userId):
    url=f'https://www.nicovideo.jp/user/{str(userId)}'
    print(f'Access to {url}...')
    dom = pq(url)
    result=dom('head').find('meta[property="profile:username"]').attr['content']
    print(f'Success! userName = {result}')
    return result

def getVideoInfo():
    '''
    投稿者と動画名を組みにしたリストを取得する
    '''
    # niconico contents search APIに投げるparameter
    options = {
        'q': 'たべるんごのうた',
        'targets': 'tagsExact',
        'fields': 'userId,title',
        '_sort': '-startTime',
        '_context': 'taberungo-creators-stats',
    }
    temp = []
    now_month = datetime.datetime.now()
    for i in range(now_month.month):
        for j in range(16):
            data_range = {'_limit': 100,
                          'filters[startTime][gte]': f'2020-0{1+i}-01T00:00:00+09:00',
                          'filters[startTime][lt]': f'2020-0{2+i}-01T00:00:00+09:00',
                          '_offset': j*100}
            print(f'getting data: [{j*100}, {j*100+99}[')
            response = requests.get(
                'https://api.search.nicovideo.jp/api/v2/video/contents/search', params={**options, **data_range})
            # 全て取得し終えたら終了する
            if (response.json()['data'] == []):
                print('skip')
                break
            # print(response.json())
            temp += response.json()['data']

    print('Finish loading all data')

    print('Analyzing data...')

    # 投稿者ごとに動画をまとめる
    # - userIdを投稿者名に変換する
    # - 上位20人分を取り出す
    result = {temp[i]['userId']: [temp[j]['title'] for j in range(
        len(temp)) if temp[j]['userId'] == temp[i]['userId']]for i in range(len(temp))}
    sorted_result = { getUserNameFromId(item[0]):item[1] for item in sorted(
        result.items(), key=lambda x: len(x[1]), reverse=True)[:20] }

    print('Finish analyzing')
    # 取得結果の確認
    for key in sorted_result.keys():
        print(
            f'creator = {key}\tn = {len(sorted_result[key])}')

    print('Writing to the text file...')
    # fileに書き込む
    with open(f'dist/taberungo-list.txt', encoding='utf-8', mode='w') as file:
        for key in sorted_result.keys():
            file.write(f' 投稿者: [{key}]')
            file.write('\n')
            file.write(
                '\n'.join([f'  [{title}]'for title in sorted_result[key]]))
            file.write('\n\n')
    print('Successfully finished!')

if __name__ == "__main__":
    getVideoInfo()


import time
from http import HTTPStatus
from io import BytesIO

from flask import Flask, Response, abort, request, send_file
from PIL import Image
from pyotp import TOTP

from . import api, cameraClient

def create_api():
    app = Flask(__name__)
    app.register_blueprint(api)

    return app

@api.route('/toggle', methods=['POST'])
def toggle():
    totp = TOTP('VALID')
    token = request.headers.get('Authorization')

    if not token or not totp.verify(token):
        abort(HTTPStatus.UNAUTHORIZED)

    return ('', HTTPStatus.ACCEPTED)

@api.route('/capture', methods=['GET'])
def capture():
    try:
        capture = Image.open('tests/fixtures/gatePhoto.jpeg')
        img_io = BytesIO()
        capture.save(img_io, 'JPEG')
        cameraClient.capture()
        return send_file(img_io, mimetype='image/jpeg')

    except Exception as e:
        print(e)
        abort()


import numpy as np
import matplotlib

matplotlib.rc('xtick', labelsize=12)
matplotlib.rc('ytick', labelsize=12)
font = {'family' : 'normal',
        'size'   : 12}

matplotlib.rc('font', **font)
import matplotlib.pyplot as plt
import waypoints

# s0x,s0y     = np.load("S0.npy")
s45x,s45y   = np.load("S45.npy")
s_45x,s_45y = np.load("S-45.npy")
s90x,s90y   = np.load("S90.npy")
s_90x,s_90y = np.load("S_90.npy")
s135x,s135y   = np.load("S135.npy")
s_135x,s_135y = np.load("S_135.npy")
s180x,s180y   = np.load("S180.npy")

print(len(s180x),len(s45x))

wp,x_0,y_0,L         = waypoints.straight_line(250,0)
wp,x_45,y_45,L       = waypoints.straight_line(250,45)
wp,x_45m,y_45m,L     = waypoints.straight_line(250,-45)
wp,x_90,y_90,L       = waypoints.straight_line(250,90)
wp,x_90m,y_90m,L         = waypoints.straight_line(250,-90)
wp,x_135,y_135,L       = waypoints.straight_line(250,135)
wp,x_135m,y_135m,L       = waypoints.straight_line(250,-135)
wp,x_180,y_180,L       = waypoints.straight_line(250,180)

plt.figure(figsize=(9,6))
###################################### Target Path
# plt.plot(x_0[::14],y_0[::14],color="cyan",marker="8",alpha=0.7,linestyle='dashed',label="0 \N{DEGREE SIGN} Heading")
plt.plot(x_45m[::14],y_45m[::14],alpha=0.7,linestyle='dashed',color="red",marker="8",label="-45 \N{DEGREE SIGN} Heading")
plt.plot(x_90m[::14],y_90m[::14],alpha=0.7,linestyle='dashed',color="b",marker="8",label="-90 \N{DEGREE SIGN} Heading")
plt.plot(x_135m[::14],y_135m[::14],alpha=0.7,linestyle='dashed',color="lime",marker="8",label="-135 \N{DEGREE SIGN} Heading")
plt.plot(x_180[::14],y_180[::14],alpha=0.7,linestyle='dashed',color="purple",marker="8",label="-180 \N{DEGREE SIGN} Heading")

# ###################################### DQN Path
# plt.plot(s45x[:320],s45y[:320],color="green",label="DQN Trained Path")
plt.plot(s_45x[:320],s_45y[:320],color="green",label="DQN Trained Path")
plt.plot(s_135x[:370],s_135y[:370],color="green")
# plt.plot(s90x,s90y,color="green")
plt.plot(s_90x[:280],s_90y[:280],color="green")

# # plt.plot(s135x,s135y,color="green")
# # plt.plot(s_135x,s_135y,color="green")
plt.plot(s180x[:320],s180y[:320],color="green")
# plt.plot(s0x,s0y,color="green")
###############################################
plt.legend(loc="best")
plt.title("Heading Action in Calm Water")
plt.xlabel("Advance (in meters)")
plt.ylabel("Transfer (in meters)")
plt.grid()
plt.savefig("HCCW.jpg",dpi=480)
plt.show()


from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import CustomUserCreationForm

class CustomUserAdmin(UserAdmin):
    model = CustomUser
    add_form = CustomUserCreationForm
    list_display = (
    'username', 'email', 'first_name', 'last_name', 'is_company_employee', 'is_contractor', 'is_superuser')

    fieldsets = (
        *UserAdmin.fieldsets,
        (
            'Данные пользователья',
            {
                'fields': (
                    'is_company_employee',
                    'is_contractor',
                    'number_phone',
                )
            },
        )
    )

admin.site.register(CustomUser, CustomUserAdmin)


import collections
import logging
import re

class update_term_index(object):

    """
    After replace_dictionary is run:
        1. update the user's index of terms found in each document
        2. remove the prefix tag from the previous dictionary so another round of tagging can occur

    Note: this class should be modified to enhance flexibility with respect to prefix/suffix tagging.
    For now, note that tags should be flanked appear flanked with asterisks and ending with underscore,
        like: *MeSH*_
    TO DO: replace the regex to one which is simply r"^"+vocab_key+([a-zA-Z0-9]+[\_]*)
    """

    def __init__(self, prefix=False, suffix=False,vocab_key="",dict_in=None,doc_key=""):
        """
        Initialize the indexer.

        Args:
            prefix: if the replacer used a prefix, set to true to look for prefix tagged terms
            suffix: if the replacer used a suffix, set to true to look for suffix tagged terms
            vocab_key: the tag prefix/suffix used for the currently tagged dictionary
            dic_in: a dictionary object to be used as a document/vocab index
            doc_key: a document id to be used in the document/vocab index
        """
        self.logger = logging.getLogger(__name__)

        self.prefix = prefix
        self.suffix = suffix
        self.vocab_key = vocab_key
        self.doc_key = doc_key
        self.dict_in = dict_in

    def __call__(self, doc):
        """
        Runs the indexer.

        Args:
            doc: the previously tagged document string
        Returns:
            doc_out: a de-tagged document string
            dict_in: the updated document/term index
        """

        # the list of all hits
        # for input document, extract all dictionary hits and add them to the dictionary
        # TO DO: update to include True/False check for prefix/suffix,
        # TO DO: update to include a regex like: r"^"+vocab_key+([a-zA-Z0-9]+[\_]*)
        #   to allow for more flexibletagging prefix/suffix
        hits = re.findall(r"\*[\_A-Za-z]+\*[\_a-zA-z]+",doc)

        # add an index entry for the given vocabulary for the document
        vocabDict = {self.vocab_key: hits}
        self.dict_in[self.doc_key].update(vocabDict)

        # detag the document
        doc_out = doc
        for h in hits:
            doc_out = doc_out.replace(h,h.replace(self.vocab_key,"").replace("_"," "))

        return [doc_out,self.dict_in]


import json
import logging
from json import JSONDecodeError

import azure.functions as func

from src.quote_api import QuoteAPI

quote_api = QuoteAPI()
quote_api._update_authors()

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    query = req.params.get("query")
    page = req.params.get("page")
    if not query:
        return func.HttpResponse(
            "{error: \"The 'query' param is required.\"}",
            status_code=400,
        )
    try:
        if page:
            result = quote_api.search_by_author(query, page=int(page))
        else:
            result = quote_api.search_by_author(query)
    except JSONDecodeError:
        return func.HttpResponse(
            json.dumps({
                "error": "Something went wrong!"
            }),
            status_code=500,
        )

    return func.HttpResponse(
        json.dumps(result),
        status_code=200,
    )


from cv2 import ROTATE_90_CLOCKWISE, ROTATE_90_COUNTERCLOCKWISE
import numpy as np
import cv2
from matplotlib import pyplot as plt
import pygame as pg
import pickle
import torch
import time

from util import Button

#colors
black = (0,0,0)

#imgs

practiceButtonImg = pg.image.load('practicebutton.png')
trialsButtonImg = pg.image.load('trialsbutton.png')
timedButtonImg = pg.image.load('timedbutton.png')
backButtonImg = pg.image.load('backbutton.png')

class_names = {
    0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G',
    7: 'H', 8: 'I', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O',
    15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V',
    22: 'W',23: 'X', 24: 'Y'
}

cam = cv2.VideoCapture(0)
framex, framey = (0,0)
status, frame = cam.read()

if status:
    framex = int(frame.shape[1]/2)
    framey = int(frame.shape[0]/2)
else:
    print('Camera Not Found')
    quit()

maxval = np.amax(frame)
scalar = int(255/maxval)
prep = frame*scalar

pg.init()

buttons = []
buttons.append(Button(practiceButtonImg,[framey+10,int(.5*framex-150),framey+90,int(.5*framex+150)],'practice'))
buttons.append(Button(trialsButtonImg,[framey+110,int(.5*framex-150),framey+190,int(.5*framex+150)],'trials'))
buttons.append(Button(timedButtonImg,[framey+210,int(.5*framex-150),framey+290,int(.5*framex+150)],'timed'))
backbtn = Button(backButtonImg,[framey+210,int(.5*framex-150),framey+290,int(.5*framex+150)],'home')

with open('model.model','rb') as f:

    model = pickle.load(f)

screen = pg.display.set_mode([framex,framey+300])

running = True
gamemode = 'home'

#trials vars
correctGuesses = 0
rand = np.random.randint(0,24)
while rand == 9:
    rand = np.random.randint(0,24)
currentTrial = class_names[rand]
timestart = False
trippie = False

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        if event.type == pg.MOUSEBUTTONUP:
            mouse_loc = pg.mouse.get_pos()
            if gamemode == 'home':
                for b in buttons:
                    if b.hit(mouse_loc):
                        gamemode = b.tag
            else:
                if backbtn.hit(mouse_loc):
                    gamemode = 'home'
                    correctGuesses = 0
                    rand = np.random.randint(0,24)
                    while rand == 9:
                        rand = np.random.randint(0,24)
                    currentTrial = class_names[rand]
                    timestart = False
                    timer = 0

        if event.type == pg.KEYUP:
            if event.key == pg.K_t:
                trippie = not trippie
            if event.key == pg.K_s:
                rand = np.random.randint(0,24)
                while rand == 9:
                    rand = np.random.randint(0,24)
                currentTrial = class_names[rand]

    screen.fill(black)

    status, frame = cam.read()

    if status:
        framex = int(frame.shape[1]/2)
        framey = int(frame.shape[0]/2)
    else:
        print('Camera Not Found')
        quit()

    prep = frame
    img = cv2.resize(prep,(framex,framey))
    if trippie:
        img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img = cv2.rotate(img,rotateCode=ROTATE_90_COUNTERCLOCKWISE)
    img = pg.surfarray.make_surface(img)

    screen.blit(img,(0,0))

    #translation prep
    mid = int(framex/2)
    left_bound = mid-int(.5*framey)
    right_bound = mid+int(.5*framey)

    tr_prep = cv2.resize(frame,(framex,framey))
    tr_prep = cv2.cvtColor(tr_prep,cv2.COLOR_BGR2GRAY)
    tr_prep = tr_prep[::,left_bound:right_bound]
    tr_prep = cv2.resize(tr_prep,(28,28))
    tr_prep = tr_prep.reshape((1,1,28,28))
    guess = model(torch.tensor(tr_prep, dtype = torch.float))
    guess = np.argmax(guess.detach().numpy())
    guess = class_names[guess]

    if gamemode == 'home':
        for b in buttons:
            screen.blit(b.img, (b.hitbox[1],b.hitbox[0]))
    else:
        screen.blit(backbtn.img, (backbtn.hitbox[1],backbtn.hitbox[0]))
        pg.draw.line(screen,black,(left_bound,0),(left_bound,framey),3)
        pg.draw.line(screen,black,(right_bound,0),(right_bound,framey),3)

    if gamemode == 'practice':
        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render(guess, True, (255,255,255))
        screen.blit(text,(framex/2,framey+100))

    if gamemode == 'trials':

        if guess == currentTrial:
            correctGuesses += 1
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]

        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render('GOAL: ' + currentTrial, True, (255,255,255))
        screen.blit(text,(50,framey+100))
        t0 = font.render(guess,True,(255,255,255))
        screen.blit(t0,(framex/2+50,framey+100))
        t2 = font.render(str(correctGuesses),True,(255,255,255))
        screen.blit(t2,(framex-50,framey+100))

    if gamemode == 'timed':
        if timestart == False:
            timestart = True
            timer = time.time() + 60

        if guess == currentTrial:
            correctGuesses += 1
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]

        font = pg.font.Font('freesansbold.ttf', 32)
        text = font.render('GOAL: ' + currentTrial, True, (255,255,255))
        screen.blit(text,(50,framey+100))
        t0 = font.render(guess,True,(255,255,255))
        screen.blit(t0,(framex/2+50,framey+100))
        t2 = font.render(str(correctGuesses),True,(255,255,255))
        screen.blit(t2,(framex-50,framey+100))
        t4 = font.render("Time: " + str(int(timer-time.time())),True,(255,255,255))
        screen.blit(t4,(50,framey+50))

        if time.time() > timer:
            gamemode = 'home'
            correctGuesses = 0
            rand = np.random.randint(0,24)
            while rand == 9:
                rand = np.random.randint(0,24)
            currentTrial = class_names[rand]
            timestart = False
            timer = 0

    pg.display.flip()

pg.quit()


import numpy as np
import matplotlib.pyplot as plt

def cluster(data, k, iters=150):
    labels = np.zeros(len(data), dtype=np.int32)

    np.random.shuffle(data)
    clusters = np.array_split(data, k)

    centers = []
    for it in range(iters):
        # Flip cluster so each column is a row, compute average of each row, use that as the value for the centroid
        centers = [[np.sum(y)/len(y) for y in np.transpose(x)] for x in clusters]

        for i in range(len(data)):
            min_dist = np.inf
            for j in range(len(centers)):
                if len(centers[j]) > 0:
                    # Euclidean distance
                    dist = np.linalg.norm(np.subtract(centers[j], data[i]))
                    if dist < min_dist:
                        min_dist = dist
                        labels[i] = j

        new_clusters = [[] for _ in range(len(clusters))]
        for i in range(len(data)):
            new_clusters[labels[i]].append(data[i])
        clusters = new_clusters

    if len(data) > 0 and len(data[0]) == 2:
        plt.scatter(np.array(data)[:, 0], np.array(data)[:, 1], c=labels)
        plt.show()

    return clusters, centers

old_faithful_data = [
    [3.600, 79],
    [1.800, 54],
    [2.283, 62],
    [3.333, 74],
    [2.883, 55],
    [4.533, 85],
    [1.950, 51],
    [1.833, 54],
    [4.700, 88],
    [3.600, 85],
    [1.600, 52],
    [4.350, 85],
    [3.917, 84],
    [4.200, 78],
    [1.750, 62],
    [1.800, 51],
    [4.700, 83],
    [2.167, 52],
    [4.800, 84],
    [1.750, 47]
]


from guard import GuardedLock
from atomicCounter import AtomicCounter
import logging
import threading
import time

lock = threading.Lock()

def thread_func(id, counter):
    logging.info('Thread %d starting', id)
    counter.inc()
    logging.info("Thread %d value is: %d", id, counter.counter)
    logging.info('Thread %d sleeping', id)
    time.sleep(1)

if __name__ == '__main__':
    logging.basicConfig(format="%(asctime)s:%(message)s", level=logging.INFO, datefmt="%H:%M:%S")
    logging.info('start')

    threads = []
    a_counter = AtomicCounter()

    for i in range(10):
        t = threading.Thread(target=thread_func, args=(i,a_counter,))
        t.start()
        threads.append(t)

    for t in threads:
        t.join()