import numpy as np
from numpy.matlib import repmat

import sys
import matplotlib.pyplot as plt
from scipy.io import loadmat
import time

def loaddata(filename):
    """
    Returns xTr,yTr,xTe,yTe
    xTr, xTe are in the form nxd
    yTr, yTe are in the form nx1
    """
    data = loadmat(filename)
    xTr = data["xTr"]; # load in Training data
    yTr = np.round(data["yTr"]); # load in Training labels
    xTe = data["xTe"]; # load in Testing data
    yTe = np.round(data["yTe"]); # load in Testing labels
    return xTr.T,yTr.T,xTe.T,yTe.T

def row_vectorize(x):
    return x.reshape(1,-1)

def perceptronUpdate(x, y, w):
    """
    function w=perceptronUpdate(x,y,w);

    Implementation of Perceptron weights updating
    Input:
    x : input vector of d dimensions (1xd)
    y : corresponding label (-1 or +1)
    w : weight vector before updating

    Output:
    w : weight vector after updating
    """
    # just in case x, w are accidentally transposed (prevents future bugs)
    x, w = map(row_vectorize, [x, w])  # map is for return a list
    assert(y in {-1,1})
    print("w.shape")
    print(w.shape)
    print("x.shape")
    print(x.shape)
    w = w + y * x
    ## fill in code here
    return w

#xTr,yTr,xTe,yTe=loaddata("../resource/lib/digits.mat")

def perceptron(x, y):
    """
    function w=perceptron(x,y);

    Implementation of a Perceptron classifier
    Input:
    x : n input vectors of d dimensions (nxd)
    y : n labels (-1 or +1)

    Output:
    w : weight vector (1xd)
    """

    n, d = x.shape
    w = np.zeros(d)
    count = 0
    while count < 100:
        m = 0
        for i in range(n):
            x_temp = x[i]
            y_temp = y[i]
            if (y_temp * np.dot(w, x_temp.T) <= 0).all():  # dot(w, x.T) return a matrix type
                w = perceptronUpdate(x_temp, y_temp, w)
                m += 1
        count += 1
        if m == 0:
            break
    w = np.asarray(w).reshape(-1)
    return w

def classifyLinear(x, w, b=0):
    """
    function preds=classifyLinear(x,w,b)

    Make predictions with a linear classifier
    Input:
    x : n input vectors of d dimensions (dxn)
    w : weight vector (dx1)
    b : bias (scalar)

    Output:
    preds: predictions (1xn)
    """
    w = w.reshape(-1)
    n, d = x.shape
    preds = np.zeros(n)
    for i in range(n):
        x_temp = x[i]
        if (np.dot(w, x_temp.T) + b < 0).all():  # if wx + b < 0 is true, then label should be -1
            preds[i] = -1
        else:
            preds[i] = 1
    return preds

N = 100
# Define the symbols and colors we'll use in the plots later
symbols = ['ko', 'kx']
mycolors = [[0.5, 0.5, 1], [1, 0.5, 0.5]]
classvals = [-1, 1]

# generate random (linarly separable) data
trainPoints = np.random.randn(N, 2) * 1.5

# defining random hyperplane
w = np.random.rand(2)

# assigning labels +1, -1 labels depending on what side of the plane they lie on
trainLabels = np.sign(np.dot(trainPoints, w))
i = np.random.permutation([i for i in range(N)])

# shuffling training points in random order
trainPoints = trainPoints[i, :]
trainLabels = trainLabels[i]

# call perceptron to find w from data
w = perceptron(trainPoints,trainLabels)
b = 0

res=300
xrange = np.linspace(-5, 5,res)
yrange = np.linspace(-5, 5,res)
pixelX = repmat(xrange, res, 1)
pixelY = repmat(yrange, res, 1).T

testPoints = np.array([pixelX.flatten(), pixelY.flatten(), np.ones(pixelX.flatten().shape)]).T
testLabels = np.dot(testPoints, np.concatenate([w.flatten(), [b]]))

Z = testLabels.reshape(res,res)
plt.contourf(pixelX, pixelY, np.sign(Z), colors=mycolors)
plt.scatter(trainPoints[trainLabels == classvals[0],0],
            trainPoints[trainLabels == classvals[0],1],
            marker='o',
            color='k'
           )
plt.scatter(trainPoints[trainLabels == classvals[1],0],
            trainPoints[trainLabels == classvals[1],1],
            marker='x',
            color='k'
           )
#plt.quiver(0, 0, w[0, 0], w[0, 1], linewidth=0.5, color=[0, 1, 0])
plt.axis('tight')
plt.show()


from random import SystemRandom

from pygal import Bar
from pygal.style import Style
from webbrowser import open_new_tab
from os import path

#
# IO
dice = [6]
string = input("Enter N strictly positive integers (number of sides of N dice): ")
if string == "":
    print("You did not enter any value, a single standard six-sided die will be rolled.")
else:
    dice.clear()
    i = 0
    six = 0
    while i < len(string):
        start = i
        flag = 1
        while i < len(string) and string[start] == ' ':
            start += 1
            i += 1
        while i < len(string) and string[i] != ' ':
            if not string[i].isdigit():
                flag = 0
            i += 1
        if flag == 0:
            six = 1
            dice.append(6)
        else:
            if start < i:
                num = int(string[start: i])
                if num <= 0:
                    six = 1
                    num = 6
                dice.append(num)
    if six == 1:
        print("Some of the values, incorrect, have been replaced with the default value of 6.")
n = 1000
string = input("Enter the desired number of rolls: ")
if string == "":
    print("Input was not provided or invalid, so the default value of 1,000 will be used.")
else:
    try:
        n = int(string)
    except ValueError:
        print("Input was not provided or invalid, so the default value of 1,000 will be used.")
    if n <= 0:
        print("Input was not provided or invalid, so the default value of 1,000 will be used.")
        n = 1000
#
#
lb = len(dice)
ub = sum(dice)
count = [0] * (ub + 1)
rand = SystemRandom()
for i in range(n):
    sm = 0
    for sn in dice:
        if sn == 1:
            sm = 1
        else:
            sm += rand.randint(1, sn)
    count[sm] += 1
#
# Graph preparation
xlab = []
items = []
begin = lb
end = ub
while count[begin] == 0:
    begin += 1
while count[end] == 0:
    end -= 1
for i in range(begin, end):
    xlab.append(i)
    dic = {"value": count[i],
           "label": f"Frequency: {count[i] / n:.2f}"}
    items.append(dic)
#
# Render
barch = Bar()
barch.title = f"Simulation for {n} rolls of the dice: {dice}"
barch.x_labels = xlab
barch.x_title = "Possible sums"
barch.y_title = "Counts"
barch.show_legend = False
barch.force_uri_protocol = "http"
barch.add('', items)
barch.style = Style(colors=("#228B22",),
                    label_font_size=12)
barch.render()
barch.render_to_file("dice_rolls.svg")
#
# Open browser window
open_new_tab("file://" + path.realpath("dice_rolls.svg"))


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

# Description:
# Date: 2021/12/8 10:59
# Author: dupei

import argparse
import os
import pickle
import platform
import shutil
import torch
import DataSet
from evaluations import pairwise_similarity
from util import extract_feature, load_image, create_model
from utils.serialization import load_checkpoint

def save_retrieved_images(gallery, topk_index, topk_sim, output_dir):
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.makedirs(output_dir, exist_ok=True)
    for i in range(len(topk_index)):
        image_path = gallery[topk_index[i]][2]
        _, filename = os.path.split(image_path)
        name, ext = os.path.splitext(filename)
        shutil.copyfile(image_path, os.path.join(output_dir, '%d_' % i + name + '_%f' % topk_sim[i] + ext))

def parse_args():
    parser = argparse.ArgumentParser(description='PyTorch Testing')

    parser.add_argument('--data', type=str, default='cub')
    parser.add_argument('--data_root', type=str, default='data/Oxford')

    parser.add_argument('--net', type=str, default='BN-Inception')
    parser.add_argument('--resume', '-r', type=str, default='ckp_ep600.pth.tar', metavar='PATH')

    parser.add_argument('--dim', '-d', type=int, default=512,
                        help='Dimension of Embedding Feather')
    parser.add_argument('--width', type=int, default=227,
                        help='width of input image')

    parser.add_argument('--batch_size', type=int, default=100)
    parser.add_argument('--nThreads', '-j', default=8, type=int, metavar='N',
                        help='number of data loading threads (default: 2)')

    parser.add_argument('--topk', '-k', type=int, default=10, help='top-k')
    parser.add_argument('--query_img', '-query', type=str, help='path to query image')
    parser.add_argument('--gallery_pickle', '-gallery', type=str, help='path to gallery image feature pickle file',
                        default='oxford_gallery_feature.pickle')
    parser.add_argument('--output', type=str, default='tmp/retrieved', help='path to output dir')

    args = parser.parse_args()
    return args

def main(args):
    if platform.system() == 'Windows':
        torch.multiprocessing.freeze_support()

    checkpoint = load_checkpoint(args.resume)

    # 修改state_dict中的keys，去除module.
    from collections import OrderedDict
    new_state_dict = OrderedDict()
    for name, param in checkpoint['state_dict'].items():
        new_state_dict[name.replace('module.', '')] = param
    checkpoint['state_dict'] = new_state_dict

    model = create_model(net=args.net, dim=args.dim, checkpoint=checkpoint)

    # data = DataSet.create(args.data, width=args.width, root=args.data_root)
    data = DataSet.create_general_dataset(root=args.data_root)

    # gallery_loader = torch.utils.data.DataLoader(
    #     data.gallery, batch_size=args.batch_size, shuffle=False,
    #     drop_last=False, pin_memory=True, num_workers=args.nThreads)
    # gallery_feature, gallery_labels = extract_features(model, gallery_loader, print_freq=1e5, metric=None)

    with open(args.gallery_pickle, 'rb') as handle:
        gallery_feature = pickle.load(handle)

    # query_loader = torch.utils.data.DataLoader(
    #     data.query, batch_size=args.batch_size,
    #     shuffle=False, drop_last=False,
    #     pin_memory=True, num_workers=args.nThreads)
    # query_feature, query_labels = extract_features(model, query_loader, print_freq=1e5, metric=None)

    images = load_image(args.query_img)
    query_feature = extract_feature(model, images)

    sim_mat = pairwise_similarity(query_feature, gallery_feature)
    topk_result = torch.topk(sim_mat, args.topk, dim=1, largest=True, sorted=True)
    topk_sim = topk_result[0]
    topk_index = topk_result[1]

    topk_sim = torch.squeeze(topk_sim).detach().numpy()
    topk_index = torch.squeeze(topk_index).detach().numpy()

    os.makedirs(args.output, exist_ok=True)
    save_retrieved_images(data.gallery, topk_index, topk_sim, args.output)

# --data_root data\CUB_200_2011 --query_img data\CUB_200_2011\train\010.Red_winged_Blackbird\Red_Winged_Blackbird_0001_3695.jpg --gallery_pickle cub_train_feature.pickle --output tmp\cub_retrieved -k 20
# --data_root data/Oxford --query_img data/Oxford/query/all_souls_000013.jpg --output tmp/retrieved -k 20
if __name__ == '__main__':
    main(parse_args())


import json
import os
import socket
import time
import _thread

MIME_TYPES = {
    'css': 'text/css',
    'html': 'text/html',
    'jpeg': 'image/jpeg',
    'jpg': 'image/jpeg',
    'js': 'text/javascript',
    'json': 'application/json',
    'rtf': 'application/rtf',
    'svg': 'image/svg+xml',
    'ico': 'application/ico',
    'bin': "file/bin"
}

class request :
    def __init__(self):
        '''
        请求对象
        :param path: 请求路径
        :param method: 请求方法
        :param head: 请求头
        :param body: 请求体
        '''
        self.method = None
        self.path = None
        self.head = None
        self.body = None

    def procParams(self):
        '''
        处理请求参数 GET POST
        :return: 请求参数处理结果
        '''
        if self.method == "POST" :
            params = self.body.decode().split("&")
        elif self.method == "GET" :
            params = self.path.split("?")
        else:
            params = []
        params_res = {}
        for i in params:
            spl = i.split("=")
            if len(spl) == 2:
                params_res[spl[0]] = spl[1]
        return params_res

    @property
    def json(self):
        try :
            return json.dumps(self.body)
        except Exception as e :
            return None

class BaseResponse:
    def __init__(self,status_code:int=500,head:dict={},body:bytes=b''):
        self.status_code = status_code
        self.head = head
        self.body = body

class CodeResponse:
    def __init__(self,code:int,message=""):
        self.status_code = code
        self.head = {}
        self.body = '''<!DOCTYPE html>

            <meta charset="UTF-8">
            <title>{}</title>

Response Code : {}

Message : {}

            '''.format(code,code,message).encode()

class FileResponse(BaseResponse):
    def __init__(self,status_code:int,fpath:str,head:dict={}):
        super(FileResponse, self).__init__()
        self.read_finish = False
        self.head = head
        self.status_code = status_code
        fname = fpath.split("/")[-1]
        fsplit = fname.split(".")
        fsplen = len(fsplit)
        if fsplen == 1:
            extension = "bin"
        else:
            extension = fsplit[-1]
        if extension in MIME_TYPES.keys():
            self.head['Content-Type'] = MIME_TYPES[extension]
        else:
            self.head['Content-Type'] = "application/file"

class MagicWEB:
    GET = 'GET'
    POST = 'POST'
    PUT = 'PUT'
    DELETE = 'DELETE'

    # 响应状态码
    OK = b"200 OK"
    NOT_FOUND = b"404 Not Found"
    FOUND = b"302 Found"
    FORBIDDEN = b"403 Forbidden"
    BAD_REQUEST = b"400 Bad Request"
    ERROR = b"500 Internal Server Error"

    def __init__(self, address, port):
        self.address = address
        self.port = port
        self.debug = False
        self.routes_dict = {}
        self.__client_dict = {}
        self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((address,port))
        self.sock.listen(10)
        self.control = False
        _thread.start_new_thread(self.__server,())

    #BACKEND SERVER METHODS
    def setRoutes(self, routes={}):
        '''
        设置路由表
        :param routes:
        :return:
        '''
        self.routes_dict = routes

    def addRouters(self,routers:dict={}):
        '''
        添加路由
        :param routers:
        :return:
        '''
        self.routes_dict.update(routers)

    def start(self):
        '''
        开启服务线程
        :return: None
        '''
        self.control = True
        print("MyltyWeb web server started...")
        print("version: v1.2.0")
        print("*"*20)

    def __server(self):
        while True :
            if self.control :
                cli,addr = self.sock.accept()
                _thread.start_new_thread(self.__router,(cli,))
            else:
                time.sleep(1)
        pass

    def __router(self,client):
        recv = client.recv(1024)
        req = self.__processRequest(recv,client)  # type:request
        print("[{}] : {}".format(req.method,req.path))
        # 有请求方法
        if req.method != None :
            # 已声明，执行请求方法
            reqpath = req.path.split("?")[0]
            if reqpath in self.routes_dict.keys():
                response = self.routes_dict[reqpath](req)
                self.__send_response(client,response)
            # 未声明,按文件取
            else:
                path = "./static/" + reqpath[1:]
                response = FileResponse(200,path)
                try :
                    f = open(path,"rb")
                    length = os.stat(path)[6]
                    response.head["Content-Length"] = "{}".format(length)
                    self.__sendStatus(client, status_code=response.status_code)
                    self.__sendHeaders(client, headers_dict=response.head)
                    # self.__sendBody(client, body_content=response.body)
                    client.send(b'\n')
                    while True :
                        buf = f.read(102400)
                        if buf :
                            client.send(buf)
                        else:
                            break
                    client.send(b'\n\n')
                    f.close()
                    self.__send_response(client,response)
                except Exception as e:
                    self.__send_response(client,CodeResponse(404,str(e)))
        # 没有请求方法
        else:
            self.__send_response(client,CodeResponse(500))
        pass

    def __processRequest(self,reqdata,client) -> request:
        '''
        处理请求
        :return: request对象
        '''
        try :
            request_line, rest_of_request = reqdata.split(b'\r\n', 1)
            request_line = request_line.decode().strip().split(' ')
        except ValueError :
            self.render(client,self.NOT_FOUND,status=self.NOT_FOUND)
            pass
        # 处理请求行
        req = request()
        if len(request_line) > 1:
            req.method = request_line[0]
            req.path = request_line[1]
            req.version = request_line[2]
        req.head = {}
        raw_headers, body = rest_of_request.split(b'\r\n\r\n', 1)
        raw_headers = raw_headers.split(b'\r\n')
        # 拆分请求头
        for header in raw_headers:
            split_header = header.decode().strip().split(': ')
            req.head[split_header[0]] = split_header[1]
        # 处理请求体
        req.body = body
        return req

    def __sendStatus(self,client,status_code:int):
        '''
        发送HTTP响应状态码
        :param status_code: 状态码
        :return:
        '''
        response_line = b"HTTP/1.1 "
        client.send(response_line + "{}".format(status_code).encode() + b'\n')

    def __sendHeaders(self,client,headers_dict:dict={}):
        '''
        发送HTTP响应头
        :param headers_dict: 响应头数组
        :return:
        '''
        for key, value in headers_dict.items():
            client.send(b"%s: %s\n" % (key.encode(), value.encode()))

    def __sendBody(self,client,body_content:bytes):
        '''
        发送HTTP响应体
        :param body_content:
        :return:
        '''
        client.send(b'\n' + body_content + b'\n\n')
        client.close()

    def __send_response(self,client,response:BaseResponse):
        '''
        发送响应对象
        :param client: client 套接字
        :param response: 响应对象
        :return:
        '''
        self.__sendStatus(client, status_code=response.status_code)
        self.__sendHeaders(client, headers_dict=response.head)
        self.__sendBody(client, body_content=response.body)


#! /usr/bin/env python3

import re
import os
import sys
from fnmatch import fnmatch
import collections
import argparse
import xml.etree.ElementTree as ET

class ExtractJtext:
    """Extract all language strings from php and xml files"""

    def __init__(self, basepath, ignorepath=None, ignorestrings=None):
        self.__basepath = basepath
        self.__current = ''
        self.__found = {}
        self.__ignorePaths = self.__getIgnorePaths(ignorepath)
        self.__ignoreStrings = self.__getIgnoreStrings(ignorestrings)

    def parse(self):
        for root, dirs, files in os.walk(self.__basepath):
            for filename in files:
                filepath = os.path.join(root, filename)

                if self.__isIgnoredPath(filepath):
                    continue

                self.__current = filepath

                if filename.endswith('.php'):
                    self.__parsePhp()

                if filename.endswith('.xml'):
                    self.__parseXml()

        return self.__found

    def printResults(self):
        od = collections.OrderedDict(sorted(self.__found.items()))
        for key, value in od.items():
            self.__printFileHeader(key)
            for element in value:
                print(element)

    def findString(self, str):
        locations = []
        needle = str.upper()

        for key, val in self.__found.items():
            if needle in val:
                locations.append(key)

        return locations

    def __parsePhp(self):
        p = re.compile(r'JText::(?:_|sprintf|script)\(([\'"])(?P<lang>[a-zA-Z0-9_]+)')
        matches = p.findall(self.__getText(self.__current))

        if matches:
            for match in matches:
                self.__addString(match[1])

    def __parseXml(self):
        try:
            root = ET.parse(self.__current).getroot()
        except ET.ParseError:
            print('Error: Invalid xml', self.__current)
            return

        if root.tag == 'access':
            self.__parseXmlAccess(root)
        elif root.tag == 'config':
            self.__parseXmlConfig(root)
        elif root.tag == 'extension':
            self.__parseXmlExtension(root)
        elif root.tag == 'form':
            self.__parseXmlForm(root)

    def __parseXmlAccess(self, root):
        for el in root.findall(".//action"):
            self.__addString(el.get('title'))
            self.__addString(el.get('description'))

    def __parseXmlConfig(self, root):
        return self.__parseXmlForm(root)

    def __parseXmlExtension(self, root):
        if root.find('name') is not None:
            self.__addString(root.find('name').text)

        if root.find('description') is not None:
            self.__addString(root.find('description').text)

        for el in root.findall(".//menu"):
            self.__addString(el.text)

    def __parseXmlForm(self, root):
        for el in root.findall(".//action"):
            if el.get('label') is not None:
                self.__addString(el.get('label'))
            if el.get('description') is not None:
                self.__addString(el.get('description'))

        for el in root.findall(".//field"):
            if el.get('label') is not None:
                self.__addString(el.get('label'))
            if el.get('description') is not None:
                self.__addString(el.get('description'))

            for opt in el.findall("option"):
                self.__addString(opt.text)

    def __getText(self, filepath):
        try:
            textfile = open(filepath, 'r')
            filetext = textfile.read()
            textfile.close()
            return filetext
        except:
            print('error opening file:', filepath)
            return ''

    def __printFileHeader(self, filepath):
        print('')
        print('#', os.path.relpath(filepath, self.__basepath))

    def __addString(self, string):
        if not string or self.__isIgnoredString(string):
            return

        if not self.__current in self.__found:
            self.__found[self.__current] = []

        self.__found[self.__current].append(string.upper())

    def __getIgnorePaths(self, ignorepath=None):
        if ignorepath and not os.path.exists(os.path.abspath(ignorepath)):
            sys.exit('Wrong path for ignorepath')
        elif not ignorepath:
            ignorepath = os.path.join(os.path.dirname(__file__), 'pathignore')
        else:
            ignorepath = os.path.abspath(ignorepath)

        f = open(ignorepath, 'r')

        res = []
        for line in f:
            if line and not line.startswith('#'):
                res.append(line)

        return res

    def __isIgnoredPath(self, filepath):
        for pattern in self.__ignorePaths:
            if fnmatch(filepath, pattern):
                return True
        return False

    def __getIgnoreStrings(self, ignorestrings=None):
        if ignorestrings and not os.path.exists(os.path.abspath(ignorestrings)):
            sys.exit('Wrong path for ignorestrings')
        elif not ignorestrings:
            ignorestrings = os.path.join(os.path.dirname(__file__), 'ignorestrings')
        else:
            ignorestrings = os.path.abspath(ignorestrings)

        f = open(ignorestrings, 'r')

        res = []
        for line in f:
            if line and not line.startswith((';', '#')):
                res.append(line.upper().strip())

        return res

    def __isIgnoredString(self, str):
        return str.upper() in self.__ignoreStrings

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Parses a joomla file, and find language strings from php and xml files.')
    parser.add_argument('-p', '--path', required=False, help='the base path to parse', default=os.getcwd())
    parser.add_argument('-ip', '--ignorepath', required=False, help='default ignore file, using unix pattern matching')
    parser.add_argument('-is', '--ignorestrings', required=False, help='default strings ignore file')
    args = parser.parse_args()
    x = ExtractJtext(args.path, args.ignorepath, args.ignorestrings)
    res = x.parse()
    x.printResults()


from http import HTTPStatus

import allure
import pytest
from allure_commons.types import LinkType
from jsonschema import validate

from base.api.courses import get_courses, get_course, delete_course, update_course, create_course, \
    courses_token_required, courses_send_file
from models.courses import CreateCourse, UpdateCourse, Course

# http://localhost:50853/index.html#behaviors/362548e62425745a6269d682cefebd3e/705479de203ca840/

@pytest.mark.allure_homework
@allure.severity(allure.severity_level.NORMAL)
@allure.epic('API')
@allure.feature('Courses')
@pytest.mark.courses
class TestCourses:
    schema = Course.manager.to_schema

    @allure.title('Getting all courses')
    @allure.description('В этом тесте мы получаем все курсы')
    @allure.story('Getting courses')
    @allure.issue(url='https://www.atlassian.com/software/jira', name='URGY-1025')
    @allure.link(
        url='https://www.artlebedev.ru/orfograf/',
        link_type=LinkType.TEST_CASE,
        name='Some test case'
    )
    def test_get_all_courses(self):
        get_courses_response = get_courses()
        get_courses_response_json = get_courses_response.json()
        schema = Course.manager.to_array_schema

        assert get_courses_response.status_code == HTTPStatus.OK
        validate(instance=get_courses_response_json, schema=schema)

    @allure.title('Creating course')
    @allure.description('В этом тесте мы создаем курс')
    def test_create_course_view(self):
        body = CreateCourse.manager.to_dict()

        create_course_response = create_course(body)
        create_course_response_json = create_course_response.json()

        with allure.step('Проверяем статус-код'):
            assert create_course_response.status_code == HTTPStatus.OK
        with allure.step('Проверяем title из response'):
            assert create_course_response_json['title'] == body['title']
        with allure.step('Проверяем description из response'):
            assert create_course_response_json['description'] == body['description']
        with allure.step('Проверяем image из response'):
            assert create_course_response_json['image'] == body['image']

        validate(instance=create_course_response_json, schema=self.schema)

    @allure.story('Getting courses')
    @allure.title('Getting course')
    @allure.description('В этом тесте мы получаем курс')
    def test_get_course(self, course_function):
        get_course_response = get_course(course_function['id'])
        get_course_response_json = get_course_response.json()

        with allure.step('Проверяем статус-код'):
            assert get_course_response.status_code == HTTPStatus.OK
        with allure.step('Валидируем response'):
            validate(instance=get_course_response_json, schema=self.schema)

    @allure.title('Updating course')
    @allure.description('В этом тесте мы обновляем курс')
    def test_update_course(self, course_function):
        body = UpdateCourse.manager.to_dict()

        update_course_response = update_course(course_function['id'], body)
        update_course_response_json = update_course_response.json()

        assert update_course_response.status_code == HTTPStatus.OK
        assert update_course_response_json['title'] == body['title']

        validate(instance=update_course_response_json, schema=self.schema)
        allure.attach.file('./files/hello.json', name='Some json response', attachment_type=allure.attachment_type.JSON)
        allure.attach.file('./files/hello.txt', name='Json  txt response', attachment_type=allure.attachment_type.TEXT)

    @allure.title('Deleting course')
    @allure.description('В этот тесте мы удаляем курс и проверяем, что он удален')
    def test_delete_course(self, course_function):
        deleted_course_response = delete_course(course_function['id'])

        assert deleted_course_response.status_code == HTTPStatus.NO_CONTENT
        allure.attach.file('./files/used.jpg', name='Some photo', attachment_type=allure.attachment_type.JPG)

    @allure.description('В этот тесте мы проверяем, что при вводе несуществующей почты, мы не получим токен,'
                        'с помощью которого можно получить доступ к курсу')
    @pytest.mark.parametrize('email', ['blablabla', 'abracadabra', 'daria.pishchulinana'])
    def test_courses_token_required(self, email):
        body = {
            "email": f"{email}@gmail.com"
        }
        courses_token_response = courses_token_required(body)
        courses_token_response_json = courses_token_response.json()
        assert courses_token_response.status_code == HTTPStatus.OK

        if email == 'daria.pishchulinana':
            allure.dynamic.title('Positive test')
            assert courses_token_response_json['success'] == 'yes'
        else:
            allure.dynamic.title('Negative test')
            assert courses_token_response_json['success'] == 'no'

    @allure.title('Sending file')
    @allure.description('В этот тесте мы отправляем файлы разных форматов')
    @allure.severity(allure.severity_level.MINOR)
    def test_courses_send_file(self):
        send_file_response = courses_send_file()
        send_file_response_json = send_file_response.json()

        with allure.step('Проверяем статус-код'):
            assert send_file_response.status_code == HTTPStatus.OK
        with allure.step('Проверяем название файла'):
            assert send_file_response_json['filename'] == 'hello.txt'
        with allure.step('Проверяем тип данных названия файла'):
            assert isinstance(send_file_response_json['filename'], str) # лучше порверять через json schema


import pandas as pd
import sklearn.cluster, sklearn.preprocessing
from sklearn import metrics

alco2009 = pd.read_csv("niaaa-report2009.csv", index_col="State")
columns = ["Wine", "Beer"]
# 클러스터링 객체를 생성하고 모델을 학습시킨다.
kmeans = sklearn.cluster.KMeans(n_clusters=9)
kmeans.fit(alco2009[columns])
# cluster 변수는 사람의 직관에 의해 설정되고 수행된 결과에 따라 재조정한다.
# 클러스터링된 결과가 의미가 있다고 판단했을 때 최종 클러스터링 변수 확정
alco2009["Clusters"] = kmeans.labels_
alco2009.to_csv("Clustering_Result.csv", index=False)

data = alco2009[["Wine", "Beer"]]
label = alco2009["Clusters"]

predicted_result = kmeans.predict(data)
print("예측 클러스터링 결과")
print(predicted_result)

print("정답률: %s %%" %(metrics.accuracy_score(label,predicted_result)*100))

03. AI/01. Machine Learning/Cluster/01/clusters.py


from flask import Flask, render_template, url_for, request
import numpy as np
from sklearn.externals import joblib
app = Flask(__name__)

filename = './data/model.sav'
model = joblib.load(filename)

def find_species(a,b,c,d):
	return model.predict(np.array([a,b,c,d]).reshape(1,-1))[0]

@app.route("/")
@app.route("/index.html")
def first_index():
	return render_template('index.html', data=' ')

@app.route("/", methods=['POST'])
def second_index():
	sepall = request.form['sepall']
	sepalw = request.form['sepalw']
	petall = request.form['petall']
	petalw = request.form['petalw']
	species = find_species(sepall, sepalw, petall, petalw)
	return render_template('index.html', data=species)

IRIS Flower Classification Project/main.py


import sys
import re
from docx import Document

pmid_re = re.compile('PMID: (\d*)')

def process_document(document: Document):
    pmids = set()
    for para in document.paragraphs:
        text = para.text

        re_results = pmid_re.search(text)
        if re_results is None:
            continue

        pmid_groups = re_results.groups()
        if pmid_groups:
            for x in pmid_groups:
                pmids.add(int(x))

    for pmid in sorted(pmids):
        print(pmid)

if __name__ == '__main__':
    doc_file = sys.argv[1]
    process_document(Document(doc_file))


#! /usr/bin/env python
# -*- coding: utf-8 -*-
from django.conf.urls import url, include
from monitor import system, manage, api

urlpatterns = [
    url(r'^system/$', system.index, name='monitor'),
    url(r'^manage/delall/$', manage.drop_sys_info, name='drop_all'),
    url(r'^hosttree/$', system.tree_node, name='host_tree'),
    url(r'^manage/delrange/(?P<timing>[0-9])/$', manage.del_monitor_data, name='del_monitor_data'),
    url(r'^manage/$', manage.index, name='monitor_manage'),
    url(r'^system/(?P<hostname>.+)/(?P<timing>\d+)/$', system.host_info, name='host_info'),
    url(r'^getcpu/(?P<hostname>.+)/(?P<timing>\d+)/$', system.get_cpu, name='get_cpu'),
    url(r'^getmem/(?P<hostname>.+)/(?P<timing>\d+)/$', system.get_mem, name='get_mem'),
    url(r'^getdisk/(?P<hostname>.+)/(?P<timing>\d+)/(?P<partition>\d+)/$', system.get_disk, name='get_disk'),
    url(r'^getnet/(?P<hostname>.+)/(?P<timing>\d+)/(?P<net_id>\d+)/$', system.get_net, name='get_net'),
    url(r'^received/sys/info/$', api.received_sys_info, name='received_sys_info'),
]


# python -m lilab.photometry.s1_tdms_2_photpkl A/B/C
# %%
from nptdms import TdmsFile
import glob
import os.path as osp
import pickle
import numpy as np
import argparse

tdms_f = glob.glob('/mnt/liying.cibr.ac.cn_Data_Temp/LS_NAC_fiberphotometry/PHO_11_NAC/0324行为记录/*.tdms')[0]

def convert(tdms_f:str):
    tdms_file = TdmsFile.read(tdms_f, raw_timestamps=True)
    outfile = osp.splitext(tdms_f)[0]+'.photpkl'
    Fs = 10
    group = tdms_file['FberPhotometry']
    channels = group.channels()
    maxlen = len(group['Time']) // 3 * 3
    channel0_470 = group['Sig470-ROI0'][2:maxlen:3]
    channel0_405 = group['Sig405-ROI0'][1:maxlen:3]
    channel0_568 = group['Sig568-ROI0'][0:maxlen:3]

    channel1_470 = group['Sig470-ROI1'][2:maxlen:3]
    channel1_405 = group['Sig405-ROI1'][1:maxlen:3]
    channel1_568 = group['Sig568-ROI1'][0:maxlen:3]

    data = np.zeros((2, 3, maxlen//3))
    data[0, 0, :] = channel0_405
    data[0, 1, :] = channel0_470
    data[0, 2, :] = channel0_568
    data[1, 0, :] = channel1_405
    data[1, 1, :] = channel1_470
    data[1, 2, :] = channel1_568

    # check channel
    data_c = data.reshape(data.shape[0], -1)
    ind_c = np.mean(np.abs(data_c), axis=-1)>0.01
    print('Number of areas:', ind_c.sum())
    data = data[ind_c]
    datadict = dict()
    datadict['data'] = data
    datadict['Fs'] = Fs
    pickle.dump(datadict, open(outfile, 'wb'))

if __name__=="__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('tdms_path', type=str)

    args = parser.parse_args()
    tdms_path = args.tdms_path
    assert osp.exists(tdms_path), "video_path not exists"
    if osp.isfile(tdms_path):
        tdms_path_l = [tdms_path]
    elif osp.isdir(tdms_path):
        tdms_path_l = glob.glob(osp.join(tdms_path, '*.tdms')) +\
                    glob.glob(osp.join(tdms_path, '*/*.tdms')) +\
                    glob.glob(osp.join(tdms_path, '*/*/*.tdms'))
    else:
        raise ValueError("tdms_path must be a file or a directory")

    for tdms_path in tdms_path_l:
        convert(tdms_path)


from selenium import webdriver

# browser exposes an executable file
# through selenium test involve the executable which will invoke actual browser

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("headless")
chrome_options.add_argument("--ignore-certificate-errors")

#chrome_options.binary_location= "C:\\Users\\oluto\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe"

driver = webdriver.Chrome(executable_path='C:\\selenium\chromedriver.exe', options=chrome_options)

driver.get('https://www.rahulshettyacademy.com/angularpractice/')

print(driver.title)


from flask import url_for, request, jsonify
import requests
import ssl
from datetime import datetime
import time
import os
backend_url = os.environ.get('backend_url')
google_api_key = os.environ.get('google_api_key')
if backend_url is None:
    backend_url = 'https://kinhkcovid19dataengine.herokuapp.com/'

def update_building_geo_sc():
    print("update_building_geo start :" + time.strftime("%A, %d. %B %Y %I:%M:%S %p"))
    url_GetXY_Pre = "https://nominatim.openstreetmap.org/search?q="
    url_GetXY_Post = "&format=json&polygon=1&addressdetails=1"

    url_GetXY_Pre_google ="https://maps.googleapis.com/maps/api/geocode/json?address="
    url_GetXY_Post_google = ",Hong Kong&key=" + google_api_key

    header = {
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
    }
    data_obj = []
    response_building = requests.get(backend_url + "getBuildingWithoutGeoData",data = data_obj, headers=header)
    response_building_json_data = response_building.json()
    inserted = 0
    print("No Geo Data Building : " +str(len(response_building_json_data)))
    for building_data in response_building_json_data:
        district = building_data["district"]
        building_name = building_data["building_name"]
        try:
            map_request_starttime = datetime.now()
            response = requests.get(url_GetXY_Pre+building_name+","+district+url_GetXY_Post,data = data_obj, headers=header)
            json_data = response.json()
            if len(json_data) > 0:
                for data in json_data:
                    new_data_obj = {"district":district,
                        "building_name":building_name,
                        "lat": data["lat"],
                        "lon": data["lon"]}
                    response = requests.post(backend_url + "/saveBuildingGEO",data = new_data_obj, headers=header)
                    inserted+=1
                    print('Updated by Open Street:' + str(inserted))
                    break
            else:
                print("No result from Open Street Map - Go for Google")
                urlToGoogle = url_GetXY_Pre_google+building_name+","+district+url_GetXY_Post_google
                response = requests.get(urlToGoogle,data = data_obj, headers=header)
                json_data = response.json()
                result_data = json_data["results"]
                if len(result_data) > 0:
                    for data in result_data:
                        geometry = data["geometry"]
                        location = geometry["location"]
                        new_data_obj = {"district":district,
                        "building_name":building_name,
                        "lat": location["lat"],
                        "lon": location["lng"]}
                        response = requests.post(backend_url + "/saveBuildingGEO",data = new_data_obj, headers=header)
                        inserted+=1
                        print('Updated by Google:' + str(inserted))
                        break
                else:
                    print("No result from Open Street Map & Google " + building_name +","+district)
                    new_data_obj = {"district":district,
                    "building_name":building_name}
                    response = requests.post(backend_url + "/saveEmptyBuildingGEO",data = new_data_obj, headers=header)
                    inserted+=1
                    print('Updated Empty GEO:' + str(inserted))

        except:
             print('Request except')
        if inserted >= 150:
            break
        else:
            map_request_endtime = datetime.now()
            difference = (map_request_endtime - map_request_starttime)
            total_seconds = difference.total_seconds()
            if total_seconds < 1:
                time.sleep(1)
    print("GEO Data added :" + str(inserted))
    print("update_building_geo end :" + time.strftime("%A, %d. %B %Y %I:%M:%S %p"))

update_building_geo_sc()


from __future__ import print_function

import os
""" First change the following directory link to where all input files do exist """
os.chdir("C:\\Users\\prata\\Documents\\book_codes\\NLP_DL")

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
import nltk
import numpy as np

import pandas as pd
import random

from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import string

from nltk import pos_tag
from nltk.stem import PorterStemmer

def preprocessing(text):
    text2 = " ".join("".join([" " if ch in string.punctuation else ch for ch in text]).split())

    tokens = [word for sent in nltk.sent_tokenize(text2) for word in
              nltk.word_tokenize(sent)]

    tokens = [word.lower() for word in tokens]

    stopwds = stopwords.words('english')
    tokens = [token for token in tokens if token not in stopwds]

    tokens = [word for word in tokens if len(word)>=3]

    stemmer = PorterStemmer()
    tokens = [stemmer.stem(word) for word in tokens]

    tagged_corpus = pos_tag(tokens)

    Noun_tags = ['NN','NNP','NNPS','NNS']
    Verb_tags = ['VB','VBD','VBG','VBN','VBP','VBZ']

    lemmatizer = WordNetLemmatizer()

    def prat_lemmatize(token,tag):
        if tag in Noun_tags:
            return lemmatizer.lemmatize(token,'n')
        elif tag in Verb_tags:
            return lemmatizer.lemmatize(token,'v')
        else:
            return lemmatizer.lemmatize(token,'n')

    pre_proc_text =  " ".join([prat_lemmatize(token,tag) for token,tag in tagged_corpus])

    return pre_proc_text

lines = []

fin = open("alice_in_wonderland.txt", "rb")

#fin = open("shakespeare.txt", "rb")

for line in fin:
    line = line.strip().decode("ascii", "ignore").encode("utf-8")

    if len(line) == 0:
        continue
    lines.append(preprocessing(line))
fin.close()

import collections
counter = collections.Counter()

for line in lines:
    for word in nltk.word_tokenize(line):
        counter[word.lower()]+=1

word2idx = {w:(i+1) for i,(w,_) in enumerate(counter.most_common())}
idx2word = {v:k for k,v in word2idx.items()}

xs = []
ys = []

for line in lines:
    embedding = [word2idx[w.lower()] for w in nltk.word_tokenize(line)]
    triples = list(nltk.trigrams(embedding))
    w_lefts = [x[0] for x in triples]
    w_centers = [x[1] for x in triples]
    w_rights = [x[2] for x in triples]
    xs.extend(w_centers)
    ys.extend(w_lefts)
    xs.extend(w_centers)
    ys.extend(w_rights)

print (len(word2idx))

vocab_size = len(word2idx)+1

ohe = OneHotEncoder(n_values=vocab_size)
X = ohe.fit_transform(np.array(xs).reshape(-1, 1)).todense()
Y = ohe.fit_transform(np.array(ys).reshape(-1, 1)).todense()
Xtrain, Xtest, Ytrain, Ytest,xstr,xsts = train_test_split(X, Y,xs, test_size=0.3,
                                                random_state=42)
print(Xtrain.shape, Xtest.shape, Ytrain.shape, Ytest.shape)

from keras.layers import Input,Dense,Dropout
from keras.models import Model

np.random.seed(42)

BATCH_SIZE = 128
NUM_EPOCHS = 20

input_layer = Input(shape = (Xtrain.shape[1],),name="input")
first_layer = Dense(300,activation='relu',name = "first")(input_layer)
first_dropout = Dropout(0.5,name="firstdout")(first_layer)

second_layer = Dense(2,activation='relu',name="second")(first_dropout)

third_layer = Dense(300,activation='relu',name="third")(second_layer)
third_dropout = Dropout(0.5,name="thirdout")(third_layer)

fourth_layer = Dense(Ytrain.shape[1],activation='softmax',name = "fourth")(third_dropout)

history = Model(input_layer,fourth_layer)
history.compile(optimizer = "rmsprop",loss="categorical_crossentropy",metrics=["accuracy"])

history.fit(Xtrain, Ytrain, batch_size=BATCH_SIZE,epochs=NUM_EPOCHS, verbose=1,validation_split = 0.2)

# Extracting Encoder section of the Model for prediction of latent variables
encoder = Model(history.input,history.get_layer("second").output)

# Predicting latent variables with extracted Encoder model
reduced_X = encoder.predict(Xtest)

final_pdframe = pd.DataFrame(reduced_X)
final_pdframe.columns = ["xaxis","yaxis"]
final_pdframe["word_indx"] = xsts
final_pdframe["word"] = final_pdframe["word_indx"].map(idx2word)

rows = random.sample(final_pdframe.index, 100)
vis_df = final_pdframe.ix[rows]

labels = list(vis_df["word"]);xvals = list(vis_df["xaxis"])
yvals = list(vis_df["yaxis"])

#in inches
plt.figure(figsize=(10, 10))

for i, label in enumerate(labels):
    x = xvals[i]
    y = yvals[i]
    plt.scatter(x, y)
    plt.annotate(label,xy=(x, y),xytext=(5, 2),textcoords='offset points',
                 ha='right',va='bottom')

plt.xlabel("Dimension 1")
plt.ylabel("Dimension 2")
plt.show()


import aws_cdk as cdk
import time
from deploy.config import config

from constructs import Construct
from aws_cdk import (aws_ec2 as ec2,
                     Stack,)

class ProteinDCVpcDeployStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # self.vpc_name = 'vpc-demo'
        # self.vpc_cidr = '20.0.0.0/16'

        # vpc_construct_id = 'NYU-VPC'

        # audit_bucket_construct_id = 'audit-bucket'
        # audit_bucket_name = config.WEB_BUCKET_NAME
        # self.audit_bucket = s3.Bucket.from_bucket_name(
        #     self, audit_bucket_construct_id, audit_bucket_name
        # )

        vpc = ec2.Vpc(self, config.VPC_NAME,
            ip_addresses=ec2.IpAddresses.cidr(config.VPC_CIDR),
            max_azs=2,
            subnet_configuration=[
                ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PUBLIC,
                    name='Public',
                    cidr_mask=20
                ), ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS,
                    name='Compute',
                    cidr_mask=20
                ), ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PRIVATE_ISOLATED,
                    name='RDS',
                    cidr_mask=20
                )
            ],
            nat_gateways=1,
        )

        # Alternatively gateway endpoints can be added on the VPC
        s3_endpoint = vpc.add_gateway_endpoint("S3Endpoint",
            service=ec2.GatewayVpcEndpointAwsService.S3
        )

        self.vpc = vpc
        # config.VPC_ID = vpc.vpc_id

        time.sleep(30)

        cdk.CfnOutput(
            self, 'VPC ID',
            value=vpc.vpc_id,
        )


#!/usr/bin/python
# -*- coding: utf-8 -*-
## @file processing.py
# @brief Файл содержит основные функции генерации графики

import astrocv.castrocv as acv
from astrocv.zernike import *

## @brief - Функция добавления шума
# @param img - исходное изображение
# @param background - средний фон
# @param dNoise - дельта
# @param bpp - bits per pixel
# @param Stride - stride изображения
# @return - Вернет изображение
def addNoiseUniform(img, background, dNoise):
    bpp = int(img.nbytes / img.size) * 8
    if bpp != 8 and bpp != 16 and bpp != 32:
        raise Exception("Unsupported pixel type {}".format(img.dtype))
    Stride = len(img[0]) * (bpp // 8)
    w = len(img[0])
    h = len(img)
    params = [img.data, w, Stride, h, int(background), int(-dNoise), int(+dNoise), bpp]
    w, h = acv.addNoiseUniform(*params)
    return img

## @brief - Функция добавления шума с нормальным распределением
# @param img - исходное изображение
# @param M - математическое ожидание
# @param S - сигма
# @param bpp - bits per pixel
# @param Stride - stride изображения
# @return - Вернет изображение
def addNoiseNorm(img, M, S):
    bpp = int(img.nbytes / img.size) * 8
    if bpp != 8 and bpp != 16 and bpp != 32:
        raise Exception("Unsupported pixel type {}".format(img.dtype))
    Stride = len(img[0]) * (bpp // 8)
    w = len(img[0])
    h = len(img)
    params = [img.data, w, Stride, h, M, S, bpp]
    w, h = acv.addNoiseNorm(*params)
    return img

## @brief - Функция рисования закрашенного круга с распределением яркости по косинусу
# @param img - исходное изображение
# @param X - координата X
# @param Y - координата Y
# @param Diameter - диаметр круга
# @param bpp - bits per pixel
# @param Stride - stride изображения
# @param Signal - яркость круга
# @return - Вернет изображение
def drawObject(img, X, Y, Diameter, Signal=None):
    bpp = int(img.nbytes / img.size) * 8
    if bpp != 8 and bpp != 16 and bpp != 32:
        raise Exception("Unsupported pixel type {}".format(img.dtype))
    Stride = len(img[0]) * (bpp // 8)
    if Signal is None:
        Signal = (1 << bpp) - 1
        if bpp == 32:
            Signal //= 2
    w = len(img[0])
    h = len(img)
    params = [img.data, w, Stride, h, X, Y, Signal, int(Diameter/2), bpp]
    return acv.drawObject(*params)

## @brief - Функция рисования закрашенного круга с распределением яркости по косинусу
# @param img - исходное изображение
# @param X - координата X
# @param Y - координата Y
# @param Diameter - диаметр круга
# @param bpp - bits per pixel
# @param Stride - stride изображения
# @param Signal - яркость круга
# @return - Вернет изображение
def drawObjectPolinomials(img, X, Y, coefficients, power=1e-8, diamAper=0.2, blindZone=0.1,
                          wavelength=0.5e-6, pixSize=5e-6, focus=2.0,exposition=1.0, electronWell=8e3, quantumEffect=1.0):
    bpp = int(img.nbytes / img.size) * 8
    if bpp != 8 and bpp != 16 and bpp != 32:
        raise Exception("Unsupported pixel type {}".format(img.dtype))
    w = len(img[0])
    h = len(img)
    offsetX, offsetY = coefficients[1], coefficients[2]
    coefficients[1] = coefficients[2] = 0
    size = 200
    ampl, poli = makeNearZone(size, coefficients, power, blindZone)
    far = makeFarZone(ampl, poli, diamAper, wavelength, pixSize, focus, exposition, electronWell, quantumEffect)

    left = int(X-len(far)/2)
    right = int(X+len(far)/2)
    top = int(Y - len(far)/2)
    bottom = int(Y + len(far) / 2)
    if left < 0:
        left = 0
    if right > w:
        right = w
    if top < 0:
        top = 0
    if bottom > h:
        bottom = h

    for i in range(0, len(far)):
        img[top + i][left:right] += far[i][0:len(far)]

    return 0

## @brief - Функция выделения объекта на изображении
# @param img - исходное изображение
# @param X - координата X
# @param Y - координата Y
# @param Left - координата левой границы объекта
# @param Top - координата верхней границы объекта
# @param Right - координата правой границы объекта
# @param Bottom - координата нижней границы объекта
# @return - Вернет изображение
def addMarker(img, X, Y, Left, Top, Right, Bottom):
    bpp = int(img.nbytes / img.size) * 8
    if bpp != 8 and bpp != 16 and bpp != 32:
        raise Exception("Unsupported pixel type {}".format(img.dtype))
    Stride = len(img[0]) * (bpp // 8)
    w = len(img[0])
    h = len(img)
    params = [img.data, w, Stride, h, int(X), int(Y), Left, Top, Right, Bottom, bpp]
    w, h = acv.addMarker(*params)
    return img

if __name__ == '__main__':
    import cv2
    import numpy as np

    width = 800
    height = 600

    Noise = 30
    dNoise = 7
    bpp = 8
    type = {8: np.uint8, 16: np.uint16, 32: np.int32}[bpp]
    white_level = (1 << 8) - 1
    if bpp != 8:
        white_level = (1 << 16) - 1

    while True:
        img = np.zeros((height, width), dtype=type)
        #addNoiseNorm(img, Noise, dNoise)
        K = [np.random.random() for i in range(15)]
        #K = [1.0, 0.0, 0.0, 0.1, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0]

        drawObjectPolinomials(img, int(width/2), int(height/2), K, exposition=2)

        cv2.imshow('img', img)

        val = cv2.waitKey(1)
        if val & 0xff == ord('q'):
            exit(0)


import pyrealsense2 as rs
# from ..sensor.sensor import Sensor
import numpy as np
# from ketisdk.gui.gui import GuiModule, GUI
from app.sensors.sensor import Sensor
# from ketisdk.vision.utils.rgbd_utils_v2 import RGBD

class RSSensor(Sensor):
    def get_device_sn(self):
        realsense_ctx = rs.context()
        for i in range(len(realsense_ctx.devices)):
            detected_camera = realsense_ctx.devices[i].get_info(rs.camera_info.serial_number)

            print(detected_camera)
    def start(self, device_serial=None, size=(1280, 720)):

        if device_serial is not None:
            self.start_(device_serial=device_serial)
        else:
            realsense_ctx = rs.context()
            for i in range(len(realsense_ctx.devices)):
                detected_serial = realsense_ctx.devices[i].get_info(rs.camera_info.serial_number)
                try:
                    self.start_(device_serial=detected_serial, size=size)
                    break
                except:
                    pass

    def start_(self, device_serial, size=(1280, 720)):
        # >>>>>>>>> Configs for REALSENSE CAMERA
        self.pipeline = rs.pipeline()
        config = rs.config()
        config.enable_device(device_serial)
        config.enable_stream(rs.stream.depth, size[0], size[1], rs.format.z16, 6)
        config.enable_stream(rs.stream.color, size[0], size[1], rs.format.bgr8, 15)

        self.align = rs.align(rs.stream.color)
        profile = self.pipeline.start(config)

        self.intr_params = self.get_intrinsic_params(profile)
        print(f'Camera {device_serial} with intrinsic params:')
        print(self.intr_params)
        print("size : ",size)

        print('sensor initialized ... ')

    def stop(self):
        self.pipeline.stop()
        print('sensor terminated ... ')

    def get_data(self):
        frames = self.pipeline.wait_for_frames()
        aligned_frames = self.align.process(frames)

        color_frame = aligned_frames.get_color_frame()  # bgr
        rgb = np.asanyarray(color_frame.get_data())[:, :, ::-1]

        depth_frame = aligned_frames.get_depth_frame()
        depth = np.asanyarray(depth_frame.get_data())

        return rgb, depth

    # def get_rgbd(self, workspace=None, depth_min=300, depth_max=1200, rot_90=False):
    #     rgb, depth = self.get_data()
    #     if rot_90: rgb, depth = np.rot90(rgb), np.rot90(depth)
    #     if rgb is None and depth is None: return None
    #     rgbd =  RGBD(rgb=rgb, depth=depth,workspace=workspace,depth_min=depth_min, depth_max=depth_max)
    #     return rgbd

    def get_intrinsic_params(self, profile):
        return  profile.get_stream(rs.stream.color).as_video_stream_profile().get_intrinsics()


from flask import Flask, render_template
from application.body.usersgenerator import print_result_of_generation_users
from application.body.requesttoapi import get_astronaut_from_json
from application.body.actions_with_files import return_data_from_csv_average_function, to_create_file, to_read_file
import asyncio

app = Flask(__name__)

@app.route("/")
def hello_world():  # put application's code here
    return "Hello!"

@app.route("/generate-users/")
def generate_users():  # put application's code here
    users = print_result_of_generation_users(number=5)
    return render_template("users.html", users=users)

@app.route("/space/")
def space():  # put application's code here
    astronauts = asyncio.run(get_astronaut_from_json())
    return render_template("space.html", astronauts=astronauts)

@app.route("/mean/")
def mean():  # put application's code here
    average = return_data_from_csv_average_function(
        link="https://drive.google.com/uc?export=download&id=13nk_FYpcayUck2Ctrela5Tjt9JQbjznt"
    )
    return render_template("average.html", average=average)

@app.route("/get-content/")
def get_content():
    to_create_file(file_name="text")  # put application's code here
    get_content_from_file = to_read_file(file_name="text")
    return render_template("get_content_from_file.html", get_content_from_file=get_content_from_file)

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


from django.contrib.auth.models import Group, User
from rest_framework import serializers

from nutep.models import DateQueryEvent, Employee, File, News, Nomenclature, \
    UserProfile

class ProfileSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('middle_name', 'position')

class UserSerializer(serializers.HyperlinkedModelSerializer):
    profile = ProfileSerializer()

    class Meta:
        model = User
        fields = ('url', 'username', 'first_name', 'last_name', 'email', 'profile')

class FileSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = File
        fields = ('title', 'file', 'extension')

class DateQueryReviseSerializer(serializers.HyperlinkedModelSerializer):
    files = FileSerializer(many=True)
    user = UserSerializer()
    type = serializers.SerializerMethodField()

    def get_type(self, obj):
        return obj.get_type_display()

    class Meta:
        depth = 1
        model = DateQueryEvent
        fields = ('id', 'date', 'user', 'type', 'files', 'status', 'note')

class EventStatusSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer()
    class Meta:
        depth = 1
        model = DateQueryEvent
        fields = ('id', 'date', 'type', 'status', 'user')

class EmployeesSerializer(serializers.ModelSerializer):
    users = UserSerializer(many=True)
    class Meta:
        depth = 1
        model = Employee
        fields = ('domainname','crm_id','portal_id','first_name','last_name','middle_name','job_title','image','head','mobile','phone','email','skype','users')

class NewsSerializer(serializers.ModelSerializer):
    class Meta:
        depth = 1
        model = News
        fields = '__all__'

class NomenclatureSerializer(serializers.ModelSerializer):
    class Meta:
        depth = 1
        model = Nomenclature
        fields = '__all__'


import re
from typing import List
from traceback import format_exc

from fastapi import APIRouter, HTTPException, status

from ..config import db
from ..models import LabelingFunction, LabelingFunctionDTO, SampleDTO, Sample, TestRunResponse, LabelDTO
from sqlalchemy.sql.expression import func

labeling_functions_router = APIRouter()

@labeling_functions_router.get("/", response_model=List[LabelingFunctionDTO])
async def get_dataset_labeling_functions(dataset_id: int):
    """Return the functions used for automatic labeling for the given dataset."""

    labeling_functions = db.query(LabelingFunction).filter(LabelingFunction.dataset_id == dataset_id).all()

    if labeling_functions is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="No labeling-functions found for data-set: {}.".format(dataset_id)
        )
    return labeling_functions

@labeling_functions_router.post("/")
async def post_dataset_labeling_functions(dataset_id: int, labeling_functions_dto: List[LabelingFunctionDTO]):
    """Update or insert labeling-functions for the given dataset."""

    # compare old functions and delete
    new_functions = {lf.id: lf for lf in labeling_functions_dto}
    existing_functions: List[LabelingFunction] = db.query(LabelingFunction).filter(
        LabelingFunction.dataset_id == dataset_id).all()
    for existing_lf in existing_functions:
        if existing_lf.id in new_functions:
            new_lf: LabelingFunctionDTO = new_functions.get(existing_lf.id)
            if existing_lf.function_body != new_lf.function_body:
                existing_lf.function_body = new_lf.function_body
            new_functions.pop(existing_lf.id)
        else:
            db.delete(existing_lf)

    # add new
    for new_lf in new_functions.values():
        lf = LabelingFunction(function_body=new_lf.function_body, dataset_id=dataset_id)
        db.add(lf)
    db.commit()

@labeling_functions_router.post("/testrun/", response_model=TestRunResponse)
async def test_run(dataset_id: int, labeling_functions_dto: List[LabelingFunctionDTO]):
    """Run given labeling-functions on a random sample."""

    # get random sample
    sample: SampleDTO = db.query(Sample).filter_by(dataset_id = dataset_id).order_by(func.random()).limit(5).first()
    if sample is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="No sample found for data-set: {}.".format(dataset_id)
        )
    # execute functions on sample
    results: List[str] = []
    for labeling_function in labeling_functions_dto:
        # try to find a function definition
        function_name = re.search(r"def(.+)\(", labeling_function.function_body)
        if function_name is None:
            results.append("No function definition found.")
            continue
        function_name = function_name.group(1).strip()
        function_call = labeling_function.function_body + f"\nlabel.append({function_name}(sample))"
        try:
            label= [] # using list as container with clear reference (preventing multi-assignment)
            exec(function_call,{"sample":sample}, {"label": label})
            if len(label) > 0 and label[0] is not None:
                if isinstance(label[0], LabelDTO):
                    results.append(label[0].name)
                else:
                    results.append(label[0])
            else:
                results.append("No result")
        except Exception as e:
            results.append(str(e))

    return TestRunResponse(result=results, sample=sample)


#! /usr/bin/env python

import os
import numpy as np
from astropy.io import fits
from astropy.cosmology import FlatLambdaCDM

cosmo = FlatLambdaCDM(H0=70, Om0=0.3)

def get_stamp(redshift):
    # Do calculations that depend on redshift
    pixscale = 0.262
    in_dir = os.getenv('HOME')+'/redmapper/' # push to a function
    out_dir = in_dir+'cutouts/'
    rmap = fits.getdata(in_dir+'rmap.fits',1)
    theta = 500.0*cosmo.arcsec_per_kpc_comoving(rmap['z']).value
    stamp = theta/pixscale
    return stamp

def main():
    """This script will return a set of n .jpeg and .fits files, which are cutouts of galaxy clusters from the get_grz_redmapper.py script.

    Cutout size is determined by the function get_stamp, which uses a cluster's red shift to determine an optimal pixel width.

    """
    in_dir = os.getenv('HOME')+'/redmapper/' # push to a function
    out_dir = in_dir+'cutouts/'

    rmapper = fits.getdata(in_dir+'rmap.fits',1)
    rmapper2 = np.where(rmapper['bcg_spec_z']>0)
    rmap = rmapper[rmapper2]
    rmap_fits = fits.writeto(in_dir+'rmap.fits',rmap,clobber=True)

    nrich = 100
    rich = np.argsort(rmap['lambda_chisq'])[::-1][0:nrich]
    print('The top '+str(nrich)+' richest clusters: '+str(rmap['lambda_chisq'][rich]))

    for ii in range(nrich):
        print('Working on cluster {}'.format(ii))
        ra = '{:6f}'.format(rmap['ra'][rich[ii]])
        dec = '{:6f}'.format(rmap['dec'][rich[ii]])
        cluster = 'cluster_{:06}'.format(rich[ii])
        stamp = '{:n}'.format(np.floor(get_stamp(rmap['z'])[rich[ii]])/10*10)

        jpeg_url = 'http://imagine.legacysurvey.org/jpeg-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)
        fits_url = 'http://imagine.legacysurvey.org/fits-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)
        model_jpeg_url = 'http://imagine.legacysurvey.org/jpeg-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)+'&tag=decals-model'
        model_fits_url = 'http://imagine.legacysurvey.org/fits-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)+'&tag=decals-model'
#        resid_jpeg_url = 'http://imagine.legacysurvey.org/jpeg-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)+'&tag=decals-'
#        resid_fits_url = 'http://imagine.legacysurvey.org/fits-cutout-decals-dr1?ra='+ra+'&dec='+dec+'&pixscale=0.262&size='+str(stamp)+'&tag=decals-'

        #print(jpeg_url, cluster)
        #print(fits_url, cluster)
        os.system('wget "'+jpeg_url+'" -O '+out_dir+cluster+'.jpg')
        os.system('wget "'+fits_url+'" -O '+out_dir+cluster+'.fits')
        #print(model_jpeg_url, cluster)
        #print(model_jpeg_url, cluster)
        os.system('wget "'+model_jpeg_url+'" -O '+out_dir+'model_'+cluster+'.jpg')
        os.system('wget "'+model_fits_url+'" -O '+out_dir+'model_'+cluster+'.fits')

if __name__ == '__main__':
    main()


# coding=utf-8
from __future__ import absolute_import

import time
import base64
import hashlib
import envs

from Crypto import Random
from Crypto.Cipher import AES

from inflection import underscore, pluralize
from mongokit import (Document, OR, ObjectId, Connection, Cursor,
                      DocumentMigration,
                      INDEX_DESCENDING, INDEX_ASCENDING)
from mongokit.document import DocumentProperties

# aliases
MongodbConn = Connection
ObjectId = ObjectId
INDEX_DESC = INDEX_DESCENDING
INDEX_ASC = INDEX_ASCENDING
Cursor = Cursor
DocumentMigration = DocumentMigration
OR = OR

class MetaDoc(DocumentProperties):

    def __init__(cls, name, bases, attrs):
        super(MetaDoc, cls).__init__(name, bases, attrs)
        if not name.startswith('Callable'):
            if '__collection__' not in cls.__dict__:
                cls.__collection__ = pluralize(underscore(cls.__name__))
        return

# models
class BaseDocument(Document):
    __metaclass__ = MetaDoc
    use_schemaless = False

    secret_key_prefix = envs.SECRET_KEY or '$b02NT~P=I$e*QZ'
    # migrate all encrypted data when change it.

    encrypt_secret_field = '_id'
    encrypt_mode = AES.MODE_ECB
    encrypt_block_size = AES.block_size

    sensitive_fields = []

    @staticmethod
    def _sensitive_filter(obj, repl='*'):
        if not dfa_filter.status:
            return obj
        elif isinstance(obj, basestring):
            return dfa_filter.filter(obj, repl)
        elif isinstance(obj, dict):
            res = {}
            for k, v in obj.iteritems():
                res[k] = dfa_filter.filter(v, repl)
            return res
        elif isinstance(obj, list):
            res = []
            for item in obj:
                res.append(dfa_filter.filter(item, repl))
            return res
        else:
            return obj

    def save(self, *args, **kwargs):
        for key in self.sensitive_fields:
            self[key] = self._sensitive_filter(self[key])
        if 'updated' in self:
            self['updated'] = int(time.time())
        return super(BaseDocument, self).save(*args, **kwargs)

    def write(self, *args, **kwargs):
        return super(BaseDocument, self).save(*args, **kwargs)

    # encryption (for unicode only)
    def encrypt(self, key, raw):
        if not raw:
            self[key] = raw
            return
        try:
            self[key] = self._encrypt(raw)
        except Exception as e:
            raise Exception('Document encrypt failed: {}'.format(str(e)))
        return self[key]

    def decrypt(self, key, key_prefix=None):
        if not self[key]:
            return self[key]
        try:
            return self._decrypt(self[key], key_prefix)
        except Exception:
            return self[key]

    def _encrypt(self, raw):
        if isinstance(raw, unicode):
            raw = raw.encode('utf-8')
        aes_key = self._get_aes_key()
        raw = self._pad(raw)
        iv = Random.new().read(self.encrypt_block_size)
        cipher = AES.new(aes_key, self.encrypt_mode, iv)
        return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')

    def _decrypt(self, enc, key_prefix=None):
        enc = base64.b64decode(enc)
        iv = enc[:self.encrypt_block_size]
        aes_key = self._get_aes_key(key_prefix)
        cipher = AES.new(aes_key, self.encrypt_mode, iv)
        return self._unpad(
            cipher.decrypt(enc[self.encrypt_block_size:])
        ).decode('utf-8')

    def _get_aes_key(self, key_prefix=None):
        key = self[self.encrypt_secret_field]
        _prefix = key_prefix or self.secret_key_prefix
        secret_key = u'{}.{}'.format(_prefix or u'', key)
        return hashlib.md5(secret_key).hexdigest()

    def _pad(self, s):
        _bs = self.encrypt_block_size
        return s + (_bs - len(s) % _bs) * chr(_bs - len(s) % _bs)

    def _unpad(self, s):
        return s[:-ord(s[len(s) - 1:])]

# filters
class DFAFilter():

    """Filter Messages from keywords
    Use DFA to keep algorithm perform constantly
    >>> f = DFAFilter()
    >>> f.add('fuck')
    >>> f.filter('hello fuck buddy')
    hello **** buddy
    """

    def __init__(self, path=None):
        self.keyword_chains = {}
        self.delimit = '\x00'
        if path:
            self.parse(path, True)

    @property
    def status(self):
        return bool(self.keyword_chains)

    def parse(self, path, required=True):
        try:
            with open(path) as f:
                for line in f:
                    self.add(line.strip())
        except Exception as e:
            if required:
                msg = 'DFAFilter: Dictionary text parse failed. '
                raise Exception(msg + str(e))

    def add(self, keyword):
        if not isinstance(keyword, unicode):
            keyword = keyword.decode('utf-8')
        chars = keyword.lower().strip()
        if not chars:
            return
        level = self.keyword_chains
        for i in xrange(len(chars)):
            if chars[i] in level:
                level = level[chars[i]]
            else:
                if not isinstance(level, dict):
                    break
                for j in xrange(i, len(chars)):
                    level[chars[j]] = {}
                    last_level, last_char = level, chars[j]
                    level = level[chars[j]]
                last_level[last_char] = {self.delimit: 0}
                break
        if i == len(chars) - 1:
            level[self.delimit] = 0

    def in_latin_word(self, message, start, step_ins):
        start_char = message[start]
        if 0x4e00 <= ord(start_char) < 0x9fa6:  # is chinese
            return False
        if start > 0 and \
           (ord(message[start - 1]) < 0x4e00 or
            ord(message[start - 1]) >= 0x9fa6) and \
           message[start - 1].isalnum():
            return True
        if start + step_ins < len(message) and \
           (ord(message[start + step_ins]) < 0x4e00 or
            ord(message[start + step_ins]) >= 0x9fa6) and \
           message[start + step_ins].isalnum():
            return True
        return False

    def filter(self, message, repl="*"):
        if not isinstance(message, str) and not isinstance(message, unicode):
            return message

        if isinstance(message, str):
            message = message.decode('utf-8')
        content_list = []
        start = 0
        while start < len(message):
            level = self.keyword_chains
            step_ins = 0
            for char in message[start:]:
                if char in level:
                    step_ins += 1
                    if self.delimit not in level[char]:
                        level = level[char]
                    else:
                        if self.in_latin_word(message, start, step_ins):
                            step_end = start + step_ins
                            content_list.append(message[start:step_end])
                            start += step_ins - 1
                            break
                        else:
                            content_list.append(repl * step_ins)
                            start += step_ins - 1
                            break
                else:
                    content_list.append(message[start])
                    break
            else:
                content_list.append(message[start])
            start += 1
        result = ''.join(content_list)
        return unicode(result)

    def check_legal(self, message, user_id):
        if not isinstance(message, unicode):
            message = message.decode('utf-8')
        message = message.lower()
        start = 0
        while start < len(message):
            level = self.keyword_chains
            step_ins = 0
            for char in message[start:]:
                if char in level:
                    step_ins += 1
                    if self.delimit not in level[char]:
                        level = level[char]
                    else:
                        return False
            start += 1
        return True

dfa_filter = DFAFilter('badwords.txt')


"""Unit test for posydon.active_learning.psy_cris classes
"""
import unittest
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math

from posydon.active_learning.psy_cris.data import TableData
from posydon.active_learning.psy_cris.regress import Regressor
from posydon.active_learning.psy_cris.classify import Classifier
from posydon.active_learning.psy_cris.sample import Sampler

from posydon.active_learning.psy_cris.utils import (
    get_regular_grid_df,
    get_random_grid_df,
)
from posydon.active_learning.psy_cris.synthetic_data.synth_data_3D import get_output_3D

SKIP_TEST_PLOTS = False
SHOW_PLOTS = False

class TestSampler(unittest.TestCase):
    """Test Sampler class on the 3d synthetic data set."""

    @classmethod
    def setUpClass(cls):
        np.random.seed(12345)
        cls.TEST_DATA_GRID = get_regular_grid_df(N=10 ** 3, dim=3)
        cls.TEST_DATA_RAND = get_random_grid_df(N=10 ** 3, dim=3)
        cls.UNIQUE_CLASSES = [1, 2, 3, 4, 6, 8]

        cls.TEST_INPUT_POINTS = np.array([[0, 0, 0], [-0.5, 0.5, 0.5]])
        cls.TEST_OUTPUT_TRUTH = get_output_3D(*cls.TEST_INPUT_POINTS.T)

    def setUp(self):
        my_kwargs = {"n_neighbors": [2, 3, 5]}
        self.table_grid = self.create_TableData(self.TEST_DATA_GRID, **my_kwargs)
        self.regr_grid = Regressor(self.table_grid)
        self.cls_grid = Classifier(self.table_grid)

    def create_TableData(self, data_frame, **kwargs):
        files = None
        input_cols = ["input_1", "input_2", "input_3"]
        output_cols = ["class", "output_1"]
        class_col_name = "class"
        table_obj = TableData(
            files,
            input_cols,
            output_cols,
            class_col_name,
            my_DataFrame=data_frame,
            verbose=False,
            **kwargs
        )
        return table_obj

    def train_everything_grid(self, cls_names, regr_names):
        if cls_names is not None:
            self.cls_grid.train_everything(cls_names)
        if regr_names is not None:
            self.regr_grid.train_everything(regr_names)

    def test_init_0(self):
        test_cases = [
            (None, None),
            (self.cls_grid, self.regr_grid),
            (None, self.regr_grid),
        ]
        for i, tup_input in enumerate([(None, None), ()]):
            with self.subTest("Sampler init", iter=i):
                samp = Sampler(*tup_input)

    def test_mcmc(self):
        self.train_everything_grid(["rbf"], None)
        samp = Sampler(classifier=self.cls_grid, regressor=None)

        steps, acc, rej = samp.run_MCMC(
            15, 0.25, [0, 0, 0], samp.TD_classification, "rbf", T=1, **{"TD_BETA": 2}
        )
        self.assertTrue(len(steps) == (acc + 1), msg="steps taken should match acc.")

    def test_ptmcmc(self):
        self.train_everything_grid(["rbf"], ["rbf"])
        samp = Sampler(classifier=self.cls_grid, regressor=self.regr_grid)

        chain_step_hist, T_list = samp.run_PTMCMC(
            5,
            15,
            samp.TD_classification_regression,
            ("rbf", "rbf"),
            init_pos=[0, 0, 0],
            alpha=0.25,
            verbose=False,
            trace_plots=False,
            TD_BETA=1,
        )
        # try with default values
        chain_step_hist, T_list = samp.run_PTMCMC(
            10, 15, samp.TD_classification_regression, ("rbf", "rbf"),
            verbose=False, trace_plots=False)

    def test_simple_density_logic(self):
        self.cls_grid.train("rbf")
        samp = Sampler(classifier=self.cls_grid, regressor=None)
        steps, acc, rej = samp.run_MCMC(
            200, 0.25, [0, 0, 0], samp.TD_classification, "rbf", T=1
        )
        acc_pts, rej_pts = samp.do_simple_density_logic(steps, 10, 0.05)
        return samp, steps

    def test_get_proposed_points(self):
        N = 10
        samp, step_hist = self.test_simple_density_logic()
        prop_points, kappa = samp.get_proposed_points(step_hist, N, 0.046)
        self.assertTrue(len(prop_points) == N)

    @unittest.skipIf(SKIP_TEST_PLOTS, "Plotting C, C+R target distributions")
    def test_TD_plots(self):
        self.train_everything_grid(["rbf"], ["rbf"])
        samp = Sampler(classifier=self.cls_grid, regressor=self.regr_grid)

        N = 70 if SHOW_PLOTS else 5
        zed = 0
        x, y = np.meshgrid(np.linspace(-1, 1, N), np.linspace(-1, 1, N))
        z = np.ones(x.shape) * zed
        data_points = np.concatenate(
            (x.flatten()[:, None], y.flatten()[:, None], z.flatten()[:, None]), axis=1
        )

        max_probs, pos, cls_keys = samp.get_TD_classification_data("rbf", data_points)

        kwargs = {"TD_BETA": 2, "TD_TAU": 0.5}
        cls_regr_td_vals = [
            float(samp.TD_classification_regression(["rbf", "rbf"], dat, **kwargs))
            for dat in data_points
        ]

        fig, subs = plt.subplots(1, 2, figsize=(13, 5))
        subs[0].set_title("TD_classification at z = {}".format(zed))
        cls_plot = subs[0].pcolormesh(
            x, y, (1 - max_probs).reshape(N, N), shading="auto"
        )

        subs[1].set_title("TD_classification_regression at z = {}".format(zed))
        cls_regr_plot = subs[1].pcolormesh(
            x, y, np.array(cls_regr_td_vals).reshape(N, N), shading="auto"
        )

        fig.colorbar(cls_plot, ax=subs[0])
        fig.colorbar(cls_regr_plot, ax=subs[1])

        for i in range(2):
            subs[i].set_xlabel("input_1")
            subs[1].set_ylabel("input_2")
            subs[i].axis("equal")

        if SHOW_PLOTS:
            plt.show()
        plt.close()

if __name__ == "__main__":
    unittest.main()


#Author: Alvin & Shawn (Prepared on: 12/04/2020)

#Chapter 2: Hypothesis testing
#Hypothesis testing for difference between 2 means (Equal/Unequal variance)
#Accounted for if different sample size

import numpy as np
from scipy import stats

###############
# Input data  #
###############
#Please go thru every single input parameter
data1 = [70.00,62.00,80.00,81.00]
data2 = [47.00,51.00,85.00,88.00,80.00]
equal = True #Equal/ Unequal variance

#For hypothesis testing
confidence_lvl = 0.95 #in decimals
given_diff = 0 #Given diff for hypothesis testing (Given by qns)
tail = 2

#If need to change sample size, mean or std, refer to line 53-65

###############
# DONT CHANGE #
###############
#Null hypothesis: data1 - data2 = given_diff
#Alternate hypothesis: data1 - data2 != given_diff

#Compute standard parameters
def compute_para(data):
    #Compute length of data (Sample size)
    n = len(data)

    #Compute sample mean
    x_bar = np.mean(data)

    #Compute sample std
    sum_sq = 0
    for i in range(0,n):
        sum_sq += (data[i]-x_bar)**2
    sample_std = np.sqrt(sum_sq/(n-1))

    #Input into para array
    para = [n, x_bar, sample_std]

    return para

#Compute parameters for data 1
# In the event that any values are given, change here #
para1 = compute_para(data1)
n1 = para1[0]
x_bar1 = para1[1]
std1 = para1[2]
print('n1:', n1)
print('x_bar1:', x_bar1)
print('std1:', std1, '\n')

#Compute parameters for data 2
# In the event that any values are given, change here #
para2 = compute_para(data2)
n2 = para2[0]
x_bar2 = para2[1]
std2 = para2[2]
print('n2:', n2)
print('x_bar2:', x_bar2)
print('std2:', std2, '\n')

#Compute dof, t_stat
if equal == True:
    #Assume equal variance
    #Compute dof
    dof = n1 + n2 - 2

    #Compute sp
    sp = np.sqrt( ((n1 - 1)*(std1**2) + (n2 - 1)*(std2**2)) / dof )
    print('sp (pool variance): ', sp)

    #Compute t_stat
    t_stat = ((x_bar1 - x_bar2) - given_diff) / (sp * np.sqrt((1/n1)+(1/n2)))
    print('t_stat: ', t_stat)

else:
    #Assume unequal variance
    #Compute dof
    dof = round(  ((std1**2/n1 + std2**2/n2)**2)\
                 / ((std1**2/n1)**2 / (n1-1) +\
                    (std2**2/n2)**2 / (n2-1)) )

    #Compute t_stat
    t_stat = ((x_bar1 - x_bar2) - given_diff) / np.sqrt((std1**2/n1)+(std2**2/n2))
    print('t_stat: ', t_stat)

#Compute t_crit
t_crit = stats.t.ppf(1-((1-confidence_lvl)/tail), dof)
print('t_crit: ', t_crit)

#Conclusion
if (np.abs(t_stat) > t_crit):
    print('Reject NULL hypothesis. Data support a difference in mean between the two groups')
else:
    print('Unable to reject the NULl hypothesis. Data failed to support any difference in mean between the two groups')

#Compute p_value
p_value = tail*(1 - stats.t.cdf(np.abs(t_stat), dof))
print('p_value: ', p_value)

#-End-


import cv2
import numpy as np
import streamlit as st
from collections import Counter
import matplotlib.pyplot as plt
from keras_retinanet import models
from app.helper.visualize import visualize_image
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image

model_path = 'app/model/object_detection.h5'
model = models.load_model(model_path, backbone_name='resnet50')

model_labels = {0: 'Bookcase', 1: 'Bathtub', 2: 'Pillow', 3: 'Couch', 4: 'Gas stove', 5: 'Washing machine', 6: 'Bed',
                7: 'Refrigerator', 8: 'Bathroom accessory', 9: 'Kitchen & dining room table', 10: 'Television',
                11: 'Sink', 12: 'Sofa bed', 13: 'Kitchenware', 14: 'Toilet', 15: 'Ceiling fan', 16: 'Microwave oven',
                17: 'Furniture', 18: 'Coffeemaker', 19: 'Cupboard', 20: 'Dishwasher', 21: 'Shower', 22: 'Clock',
                23: 'Countertop', 24: 'Mug', 25: 'Table'}

room_labels = {'kitchen': ['Gas stove', 'Refrigerator', 'Kitchen & dining room table', 'Sink', 'Kitchenware',
                           'Microwave oven', 'Coffeemaker', 'Dishwasher', 'Countertop', 'Mug'],
               'BedRoom': ['Bed', 'Bookcase', 'Pillow', 'Couch', 'Television', 'Sofa bed', 'Ceiling fan', 'Furniture',
                           'Cupboard', 'Clock', 'Table'],
               'Bathroom': ['Bathtub', 'Washing machine', 'Bathroom accessory', 'Toilet', 'Shower']
               }

def categorize_entity(entity):
    for category, items in room_labels.items():
        if entity in items:
            return category
    return "Unknown"

def show(image_path):
    predicted_items = []
    image = read_image_bgr(image_path)

    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    image = preprocess_image(image)
    image, scale = resize_image(image)

    boxes, scores, labels = model.predict(np.expand_dims(image, axis=0))
    boxes /= scale

    for box, score, label in zip(boxes[0], scores[0], labels[0]):

        if score < 0.5:
            break

        visualize_image(draw, box, score, label)
        predicted_items.append({'predicted_label': model_labels[label],
                                'score': score})

    if predicted_items:
        predicted_rooms = []

        for i in predicted_items:
            category = categorize_entity(i['predicted_label'])
            predicted_rooms.append(category)

        counts = Counter(predicted_rooms)

        room = counts.most_common(1)[0][0]

        st.json({'model_predictions': predicted_items,
                 'room': room})

    else:
        st.json({'model_predictions': None})

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    st.set_option('deprecation.showPyplotGlobalUse', False)
    st.pyplot()

st.title("Household Object Detection")
uploaded_image = st.file_uploader("Choose a image file", type=["jpg", "png", "jpeg"])

if uploaded_image is not None:
    show(uploaded_image)


import os

from dotenv import load_dotenv
from fastapi import FastAPI
from pymongo import MongoClient

from models.telemetry import (
    ActorDeathEvent,
    GameTickEvent,
    GrandExchangeOfferChangedEvent,
    HitsplatAppliedEvent,
    StatChangedEvent,
    LootReceivedEvent,
)

load_dotenv()

URI = os.environ["MONGO_URI"]

client = MongoClient(URI)
db = client.get_database("runelite-wrapped-raw")
game_tick_collection = db.get_collection("game_ticks")
stat_changed_collection = db.get_collection("stat_changed")
grand_exchange_offer_changed_collection = db.get_collection(
    "grand_exchange_offer_changed"
)
hitsplat_applied_collection = db.get_collection("hitsplat_applied")
actor_death_collection = db.get_collection("actor_death")
loot_received_collection = db.get_collection("loot_received")

app = FastAPI()

@app.post("/api/v1/event/game-tick/")
async def game_tick(event: GameTickEvent):
    game_tick_collection.insert_one(event.dict())
    return {"message": "OK"}

@app.post("/api/v1/event/stat-changed/")
async def stat_change(event: StatChangedEvent):
    stat_changed_collection.insert_one(event.dict())
    return {"message": "OK"}

@app.post("/api/v1/event/grand-exchange-offer-changed/")
async def grand_exchange_offer_change(event: GrandExchangeOfferChangedEvent):
    grand_exchange_offer_changed_collection.insert_one(event.dict())
    return {"message": "OK"}

@app.post("/api/v1/event/hitsplat-applied/")
async def hitsplat_applied(event: HitsplatAppliedEvent):
    hitsplat_applied_collection.insert_one(event.dict())
    return {"message": "OK"}

@app.post("/api/v1/event/actor-death/")
async def actor_death(event: ActorDeathEvent):
    actor_death_collection.insert_one(event.dict())
    return {"message": "OK"}

@app.post("/api/v1/event/loot-received/")
async def loot_received(event: LootReceivedEvent):
    loot_received_collection.insert_one(event.dict())
    return {"message": "OK"}

def main():
    from uvicorn import run

    run(app, host="0.0.0.0", port=8000)

if __name__ == "__main__":
    import logging

    logging.basicConfig(level=logging.INFO)
    main()


from django import forms
from .models import Properties

class PropertyForm(forms.ModelForm):
    class Meta:
        model = Properties
        fields = ('landlord', 'street_address', 'house_name',
                  'post_code', 'no_of_bedrooms', 'activate',)

    def __init__(self, *args, **kwargs):
        """
        Add placeholders, remove auto-generated labels
        """
        super().__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].label = False
            if self.fields[field].required:
                placeholder = f'{field}*'
            else:
                placeholder = field
            placeholder = placeholder.replace('_',  ' ')
            self.fields[field].widget.attrs['placeholder'] = \
                placeholder.capitalize()


import config
import urllib.parse
import hashlib
import hmac
import base64
import requests

def get_kraken_signature(urlpath, data, secret):

    postdata = urllib.parse.urlencode(data)
    encoded = (str(data['nonce']) + postdata).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()

    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    sigdigest = base64.b64encode(mac.digest())
    return sigdigest.decode()

# Read Kraken API key and secret stored in environment variables
api_url = "https://api.kraken.com"
api_key = config.API_KEY
api_sec = config.API_SECRET

# Attaches auth headers and returns results of a POST request
def kraken_request(uri_path, data, api_key, api_sec):
    headers = {}
    headers['API-Key'] = api_key
    # get_kraken_signature() as defined in the 'Authentication' section
    headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec)
    req = requests.post((api_url + uri_path), headers=headers, data=data)
    return req


import math
import random

import pygame as pg
import pygame.time

CAMERA_POS = 0, -100, 0
CAMERA_MOVE_SPEED = 0.4
CAMERA_TILT_SPEED = 0.01
CAMERA_FOCAL_DISTANCE = 700

SPHERE_POINTS = 100

BALL_RADIUS = 2

GRAVITY = 0.0002
FRICTION = 0.0005

WIN = pg.display.set_mode((0, 0), pg.FULLSCREEN, 64)
WIN_WIDTH = pg.display.get_window_size()[0]
WIN_HEIGHT = pg.display.get_window_size()[1]

pg.init()
pg.display.set_caption("Projection of 3D points onto 2D screen")
font = pg.font.SysFont('Comic Sans MS', 30)

MOUSE_POS = (WIN_WIDTH/2, WIN_HEIGHT/2)

def point_sorter(p):
    return -p.cords.distance_to(camera.pos)

def rotate(pointlist, axisvect, rotationamt):

    for p in pointlist:
        p.cords = p.cords.rotate_rad(rotationamt, axisvect)

def project(vector_a, vector_b):
    return (vector_a.dot(vector_b) / vector_b.dot(vector_b)) * vector_b

def calculate_pos_on_screen(camera_perspective, point_pos):
    displacement = point_pos - camera_perspective.pos

    true_focal_length = CAMERA_FOCAL_DISTANCE / math.cos(math.pi * camera_perspective.dir.angle_to(displacement) / 180)

    ratio = true_focal_length / displacement.length()

    if project(displacement, camera_perspective.sidedir).magnitude() == 0:
        x_pos = 0
    elif camera_perspective.sidedir.dot(project(displacement, camera_perspective.sidedir)) < 0:
        x_pos = (ratio * project(displacement, camera_perspective.sidedir)).magnitude()
    else:
        x_pos = -(ratio * project(displacement, camera_perspective.sidedir)).magnitude()

    if project(displacement, camera_perspective.updir).magnitude() == 0:
        y_pos = 0
    elif camera_perspective.updir.dot(project(displacement, camera_perspective.updir)) < 0:
        y_pos = (ratio * project(displacement, camera_perspective.updir)).length()
    else:
        y_pos = -(ratio * project(displacement, camera_perspective.updir)).length()

    return x_pos + WIN_WIDTH/2, y_pos + WIN_HEIGHT/2

class Point:

    def __init__(self, position, color, vel, lock):
        self.cords = position
        self.color = color
        self.velocity = vel
        self.locked = lock

    def move(self, ps, gravity):
        if self.locked:
            return False
        if gravity:
            self.velocity = self.velocity + pg.Vector3(0, 0, -GRAVITY)

        accuracy = 1
        for x in range(accuracy):
            for point in ps:
                if not point == self:
                    if (point.cords - self.cords - self.velocity).magnitude() <= BALL_RADIUS * 2:
                        reflect_velocity_taken = project(point.velocity, point.cords - self.cords)
                        reflect_velocity_given = project(self.velocity, point.cords - self.cords)

                        self.velocity = self.velocity - reflect_velocity_given
                        point.velocity = point.velocity - reflect_velocity_taken

                        self.velocity = self.velocity + reflect_velocity_taken
                        point.velocity = point.velocity + reflect_velocity_given
        for point in ps:
            if (self.cords + self.velocity - point.cords).magnitude() <= BALL_RADIUS * 2 and not point == self:
                if self.velocity.dot(point.cords - self.cords) > 0:
                    self.velocity = self.velocity - project(self.velocity, point.cords - self.cords)

        self.cords += self.velocity

class Camera:

    def __init__(self, positionvect, dirvect, upvect):
        self.pos = positionvect
        self.dir = dirvect
        self.updir = upvect
        self.sidedir = dirvect.cross(upvect)

def drawWindow(win, ps, cam):

    pg.draw.rect(win, (21, 21, 21), (0, 0, WIN_WIDTH, WIN_HEIGHT))

    for point in ps:

        pos = calculate_pos_on_screen(cam, point.cords)

        displacement = point.cords - cam.pos

        size = max(BALL_RADIUS / displacement.magnitude() * CAMERA_FOCAL_DISTANCE, 1) + 1
        #size = max(BALL_RADIUS * 10 / (displacement.magnitude()/30), 1)

        draw_color = pg.Color(point.color).lerp(pg.Color(0, 0, 0), min(displacement.magnitude(), 1000)/1000)

        if displacement.dot(cam.dir) > 0:
            pg.draw.circle(win, draw_color, pos, size)

    f_vector = (project(cam.dir * 40, pg.Vector3(1, 0, 0)).x, project(cam.dir * 40, pg.Vector3(0, 0, 1)).z)
    u_vector = (project(cam.updir * 40, pg.Vector3(1, 0, 0)).x, project(cam.updir * 40, pg.Vector3(0, 0, 1)).z)
    l_vector = (project(cam.sidedir * 40, pg.Vector3(1, 0, 0)).x, project(cam.sidedir * 40, pg.Vector3(0, 0, 1)).z)

    pg.draw.line(win, (200, 255, 255), (50, 50), (50 - u_vector[0], 50 - u_vector[1]), 3)
    pg.draw.line(win, (255, 255, 200), (50, 50), (50 - l_vector[0], 50 - l_vector[1]), 3)
    pg.draw.line(win, (255, 200, 255), (50, 50), (50 - f_vector[0], 50 - f_vector[1]), 3)

if __name__ == '__main__':

    currentTime = 2
    prevTime = 1

    pg.mouse.set_visible(False)
    pg.mouse.set_pos([MOUSE_POS[0], MOUSE_POS[1]])

    circles_up = pg.Vector3(0, 0, 1)

    points = list()
    static_points = list()
    static_points.append(Point(pg.Vector3(0, 0, 0), (255, 255, 255), pg.Vector3(0, 0, 0), True))

    phi = math.pi * (3. - math.sqrt(5.))  # golden angle in radians
    for i in range(SPHERE_POINTS):
        y = 1 - (i / float(SPHERE_POINTS - 1)) * 2  # y goes from 1 to -1
        radius = math.sqrt(1 - y * y)  # radius at y

        theta = phi * i  # golden angle increment

        x = math.cos(theta) * radius
        z = math.sin(theta) * radius

        static_points.append(Point(pg.Vector3(x, y, z).normalize() * (75 + BALL_RADIUS*2), (255, 255, 255), pg.Vector3(0, 0, 0), True))

    campos = pg.Vector3(-200, 0.001, 0)
    camdir = pg.Vector3(1, 0, 0)
    camdirup = pg.Vector3(0, 0, 1)

    camera = Camera(campos, camdir, camdirup)

    iterations = 0
    over = back = forward = left = up = down = right = tright = tleft = tup = tdown = making_points = False
    pg.mouse.set_pos((WIN_WIDTH/2, WIN_HEIGHT/2))
    while not over:
        iterations += 1
        for event in pg.event.get():
            if event.type == pg.QUIT:
                over = True
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    over = True
                if event.key == pg.K_s:
                    back = True
                elif event.key == pg.K_w:
                    forward = True
                if event.key == pg.K_d:
                    right = True
                elif event.key == pg.K_a:
                    left = True
                if event.key == pg.K_LSHIFT:
                    down = True
                elif event.key == pg.K_SPACE:
                    up = True
                if event.key == pg.K_q:
                    making_points = True
                if event.key == pg.K_e:
                    points.clear()
                if event.key == pg.K_f:
                    GRAVITY = -GRAVITY
            if event.type == pg.KEYUP:
                if event.key == pg.K_s:
                    back = False
                elif event.key == pg.K_w:
                    forward = False
                if event.key == pg.K_d:
                    right = False
                elif event.key == pg.K_a:
                    left = False
                if event.key == pg.K_LSHIFT:
                    down = False
                elif event.key == pg.K_SPACE:
                    up = False
                if event.key == pg.K_q:
                    making_points = False

        if making_points and iterations % 80 == 0:
            points.append(Point(pg.Vector3(0, 0, 0), (random.random() * 255, random.random() * 255,
                                random.random() * 255), pg.Vector3(random.random() - 0.5, random.random() - 0.5,
                                random.random() - 0.5).normalize() * 0.3, False))

        if back:
            camera.pos -= camera.dir * CAMERA_MOVE_SPEED
        elif forward:
            camera.pos += camera.dir * CAMERA_MOVE_SPEED
        if right:
            camera.pos -= camera.sidedir * CAMERA_MOVE_SPEED
        elif left:
            camera.pos += camera.sidedir * CAMERA_MOVE_SPEED
        if up:
            camera.pos += camera.updir * CAMERA_MOVE_SPEED
        elif down:
            camera.pos -= camera.updir * CAMERA_MOVE_SPEED

        horizontal_rotation = (pg.mouse.get_pos()[0] - MOUSE_POS[0]) * CAMERA_TILT_SPEED / 5
        vertical_rotation = (MOUSE_POS[1] - pg.mouse.get_pos()[1]) * CAMERA_TILT_SPEED / 5

        camera.dir = camera.dir.rotate_rad(horizontal_rotation, pg.Vector3(0, 0, 1))
        camera.sidedir = camera.sidedir.rotate_rad(horizontal_rotation, pg.Vector3(0, 0, 1))
        camera.updir = camera.updir.rotate_rad(horizontal_rotation, pg.Vector3(0, 0, 1))

        camera.dir = camera.dir.rotate_rad(vertical_rotation, camera.sidedir)
        camera.updir = camera.updir.rotate_rad(vertical_rotation, camera.sidedir)

        pg.mouse.set_pos([MOUSE_POS[0], MOUSE_POS[1]])

        for point in points:
            point.move(points, True)
            point.velocity = point.velocity * (1 - FRICTION)
            if point.cords.magnitude() > 75:
                point.cords = point.cords.normalize() * 75
                point.velocity = point.velocity.reflect(point.cords)

        draw_points = points + static_points
        draw_points.sort(key=point_sorter)
        drawWindow(WIN, draw_points, camera)

        text = font.render(str(len(points)), False, (255, 255, 255), (0, 0, 0))
        textRect = text.get_rect()
        textRect.topright = (WIN_WIDTH - textRect.width, textRect.height)
        WIN.blit(text, textRect)
        pg.display.update()