"""

https://leetcode.com/problems/powx-n/submissions/
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100
"""

def brute_force(x, n):
    num = abs(n)
    ans = 1.0
    while(num > 0):
        ans = ans * x
        num -= 1
    if n < 0:
        ans = 1.0 / ans
    return ans

def better_solution(x, n):
    ans = 1.0
    num = abs(n)

    while num > 0:
        #print("line", ans, x, num)
        if num % 2 == 1:
            ans = ans * x
        x = x * x
        num = num // 2

    if n < 0:
        ans = 1.0 / ans

    return ans

x = 2
n = -10
print(better_solution(x, n),  pow(x, n))
print(brute_force(x, n))


import os
import zipfile
import tempfile

def parse(filename):
    basename_less_final_extension = os.path.splitext(os.path.basename(filename))[0]
    extension = get_lowercase_extension(filename)
    if extension == ".zip":
        assert zipfile.is_zipfile(filename), \
            "Input filename {} with extension .zip is not a zip file.".format(basename_less_final_extension)
        with tempfile.TemporaryDirectory() as tempdirname:
            with zipfile.ZipFile(filename) as zip:
                files = zip.namelist()
                assert len(files) == 1 and files[0] == basename_less_final_extension, \
                    "Only single file archives allowed with a file whose name matches the name of the archive"
                zip.extract(basename_less_final_extension, tempdirname)
                filename = os.path.join(tempdirname, basename_less_final_extension)
                return _parse(filename)
    return _parse(filename)

def _parse(filename):
    extension = get_lowercase_extension(filename)
    if extension in [".edf", ".edif"]:
        from spydrnet.parsers.edif.parser import EdifParser
        parser = EdifParser.from_filename(filename)
    elif extension in [".v", ".vh"]:
        from spydrnet.parsers.verilog.parser import VerilogParser
        parser = VerilogParser.from_filename(filename)
    else:
        raise RuntimeError("Extension {} not recognized.".format(extension))
    parser.parse()
    return parser.netlist

def get_lowercase_extension(filename):
    extension = os.path.splitext(filename)[1]
    extension_lower = extension.lower()
    return extension_lower


#coding=UTF-8
import threading
import time

semaphore=threading.Semaphore(5)

def foo():
	global num
	semaphore.acquire()
	temp=num
	time.sleep(2)
	num=temp-1
	print('ok')
	semaphore.release()
l=[]
num=10
for i in range(10):
    t=threading.Thread(target=foo,args=())
    t.start()
    l.append(t)

for i in l:
	i.join()
print(num)

# 也是一把锁，可以指定有几个线程可以同时获得这把锁，比如设置最多是5个


import math

import numpy

from chainer import cuda
from chainer import function
from chainer.utils import type_check

def _as_mat(x):
    if x.ndim == 2:
        return x
    return x.reshape(len(x), -1)

class Linear(function.Function):

    """Linear function (a.k.a. fully-connected layer or affine transformation).

    This function holds a weight matrix ``W`` and a bias vector ``b``.

    The weight matrix ``W`` has shape ``(out_size, in_size)``.
    This matrix is initialized with i.i.d. Gaussian samples, each of which has
    zero mean and deviation :math:`\sqrt{1/\\text{in_size}}`.
    The deviation is scaled by factor ``wscale`` if specified.

    The bias vector ``b`` is of size ``out_size``.
    Each element is initialized with the ``bias`` value.
    If ``nobias`` argument is set to True, then this function does not hold a
    bias vector.

    Let :math:`X` be an input matrix, and :math:`W, b` the weight matrix and
    the bias vector, respectively.
    Then, the output matrix :math:`Y` is computed by :math:`Y = XW^\\top + b`,
    where the addition by :math:`b` is broadcasted across the minibatch.

    Args:
        in_size (int): Dimension of input vectors.
        out_size (int): Dimension of output vectors.
        wscale (float): Scaling factor of the weight matrix.
        bias (float): Initial bias value.
        nobias (bool): If True, then this function does not use the bias.
        initialW (2-D array): Initial weight value. If ``None``, then this
            function uses to initialize ``wscale``.
        initial_bias (1-D array): Initial bias value. If ``None``, then this
            function uses to initialize ``bias``.

    .. note::

       This function accepts an input variable of a non-matrix array.
       In this case, the leading dimension is treated as the batch dimension,
       and the other dimensions are reduced to one dimension.

    """
    def __init__(self, in_size, out_size, wscale=1, bias=0, nobias=False,
                 initialW=None, initial_bias=None):
        self.W = None
        self.gW = None
        self.b = None
        self.gb = None

        if initialW is not None:
            assert initialW.shape == (out_size, in_size)
            self.W = initialW
        else:
            self.W = numpy.random.normal(
                0, wscale * math.sqrt(1. / in_size),
                (out_size, in_size)).astype(numpy.float32)
        xp = cuda.get_array_module(self.W)
        self.gW = xp.full_like(self.W, numpy.nan)

        if initial_bias is not None:
            assert initial_bias.shape == (out_size,)
            self.b = initial_bias
        elif not nobias:
            self.b = numpy.repeat(numpy.float32(bias), out_size)

        if self.b is not None:
            self.gb = xp.full_like(self.b, numpy.nan)

    @property
    def parameter_names(self):
        if self.b is None:
            return 'W',
        return 'W', 'b'

    @property
    def gradient_names(self):
        if self.gb is None:
            return 'gW',
        return 'gW', 'gb'

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 1)
        x_type, = in_types

        type_check.expect(
            x_type.dtype == numpy.float32,
            x_type.ndim >= 2,
            (type_check.prod(x_type.shape[1:]) ==
             type_check.Variable(self.W.shape[1], 'W.shape[1]')),
        )

    def zero_grads(self):
        self.gW.fill(0)
        if self.gb is not None:
            self.gb.fill(0)

    def forward(self, x):
        x = _as_mat(x[0])
        Wx = x.dot(self.W.T)
        if self.b is not None:
            Wx += self.b
        return Wx,

    def backward(self, x, gy):
        _x = _as_mat(x[0])
        self.gW += gy[0].T.dot(_x)
        if self.gb is not None:
            self.gb += gy[0].sum(0)
        return gy[0].dot(self.W).reshape(x[0].shape),

class NonparameterizedLinear(function.Function):

    """Nonparameterized linear class.

    .. seealso:: :class:`Linear`

    """

    def check_type_forward(self, in_types):
        type_check.expect(
            2 <= in_types.size(),
            in_types.size() <= 3,
        )
        x_type = in_types[0]
        w_type = in_types[1]

        type_check.expect(
            x_type.dtype == numpy.float32,
            w_type.dtype == numpy.float32,
            x_type.ndim >= 2,
            w_type.ndim == 2,
            type_check.prod(x_type.shape[1:]) == w_type.shape[1],
        )
        if in_types.size().eval() == 3:
            b_type = in_types[2]
            type_check.expect(
                b_type.ndim == 1,
                b_type.shape[0] == w_type.shape[0],
            )

    def forward(self, x):
        W = x[1]
        out_size, in_size = W.shape
        if len(x) == 3:
            func = Linear(
                in_size, out_size, initialW=W, initial_bias=x[2])
        else:
            func = Linear(
                in_size, out_size, initialW=W, nobias=True)
        self.func = func
        if any(isinstance(i, cuda.ndarray) for i in x):
            func.to_gpu()
        return func.forward(x[:1])

    def backward(self, x, gy):
        func = self.func
        func.zero_grads()
        gx = func.backward(x[:1], gy)
        if func.gb is None:
            return (gx[0], func.gW)
        return (gx[0], func.gW, func.gb)

def linear(x, W, b=None):
    """Nonparameterized linear function.

    Args:
        x (~chainer.Variable): Input variable.
        W (~chainer.Variable): Weight variable.
        b (~chainer.Variable): Bias variable (optional).

    Returns:
        ~chainer.Variable: Output variable.

    .. seealso:: :class:`Linear`

    """
    if b is None:
        return NonparameterizedLinear()(x, W)
    else:
        return NonparameterizedLinear()(x, W, b)


from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.interfaces import IPropertiesTool
from Products.ZCatalog.ProgressHandler import ZLogHandler
from collective.contentleadimage import config

def importVarious(self):
    if self.readDataFile('contentleadimage.txt') is None:
        return

    portal = self.getSite()
    ptool = getToolByName(portal, 'portal_properties')
    props = ptool.cli_properties

    if not props.hasProperty('image_width'):
        props.manage_addProperty('image_width', config.IMAGE_SCALE_SIZE[0], 'int')
    if not props.hasProperty('image_height'):
        props.manage_addProperty('image_height', config.IMAGE_SCALE_SIZE[1], 'int')
    if not props.hasProperty('desc_scale_name'):
        props.manage_addProperty('desc_scale_name', 'thumb', 'string')
    if not props.hasProperty('body_scale_name'):
        props.manage_addProperty('body_scale_name', 'mini', 'string')

    ctool = getToolByName(portal, 'portal_catalog')
    ctool.reindexIndex(['hasContentLeadImage'], portal.REQUEST, pghandler=ZLogHandler())

def removeConfiglet(self):
    if self.readDataFile('cli-uninstall.txt') is None:
        return
    portal_conf=getToolByName(self.getSite(),'portal_controlpanel')
    portal_conf.unregisterConfiglet('ContentLeadImage')


#grafik dengan bentuk subplot

import matplotlib.pyplot as plt
import numpy as np

x=np.arange(-10,11)
y1=x**3 - x**2 + x
y2=x**2

plt.figure(figsize=(9,9))
plt.subplot(2,2,1) #subplot(nrows,ncols,index(posisi grafik, mulai dari angka 1, dari kiri ke kanan))
plt.plot(x,y1,"g^-")
plt.title("Kurva pertama")

plt.subplot(2,2,4)
plt.plot(x,y2,"b.-")
plt.title("Kurva kedua")

plt.suptitle("Belajar menggambar kurva")
plt.show()

#Sumber: https://towardsdatascience.com/matplotlib-tutorial-learn-basics-of-pythons-powerful-plotting-library-b5d1b8f67596


import unittest
import fibo
import sys
from unittest.mock import patch

class TestFibo(unittest.TestCase):

    def test_output(self):
        fibo.writeFile(10, 2)
        read_text = open("result.txt", 'r')
        self.assertEqual(read_text.read(),"3\n5\n8\n13\n21\n34\n55\n89\n144\n233\n")
        read_text.close()

    def test_input(self):
        self.assertEqual(fibo.fib(10), 55)

    def test_faulty_input(self):
        self.assertEqual(fibo.fib("cat"), 0)

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


import tensorflow as tf
import numpy as np
import sys
import math
from matplotlib import pyplot  as plt
import matplotlib.gridspec as gridspec
import os
#x_pad = np.pad(x, ((0, 0), (0, 0), (pad, pad), (pad, pad)), 'constant',
#constant_values=0)
#tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,'base_model')
x_train = None
y_train = None
choose_sample_items = 150
image_size = 40
data_split_ratio = 0.9

#use pca data will not collapse
use_pca = False
#60 samples maybe small, can not support 32 principle components
if True:
    pca_image_size = 144
else:
    pca_image_size = 224

do_pick_data = False
if False:
    pick_data_start_idx = 127
    pick_data_end_idx = 178
else:
    pick_data_start_idx = 127
    pick_data_end_idx = 300

do_split_data = False
#550nm-600nm 400 530
#650nm-690nm 678 795
#870nm-910nm 1341 1469
if True:
    split_start_idx = [20]
    split_img_size = [25*25]
    #split_start_idx = [400]
    #split_img_size = [11*11]
elif False:
    #this segment looks which has most peaks, such as 410,435,460,480,etc.
    split_start_idx = [0,660]
    split_img_size = [17*17,72] #407nm-512nm,644nm-668nm

    split_start_idx = [660]
    split_img_size = [29*29] #407nm-512nm,644nm-668nm
else:
    #100 2.0+ 200 2.0+ 300 1.9 400 1.8 500 1.8 600 2.0
    split_start_idx = [600]
    split_img_size = [10*10] #407nm-512nm,644nm-668nm

#127-300 surprised curve shape, diff from others
pre_first_derivative=False
#127-300 with others is not visible distinguishable
pre_second_derivative=False
pre_wavelet_decomposition=False

load_raw_data_save_to_file=False
add_val_data_to_training_data=False
limit_data_precision=False
class NavelOrange(object):
    """description of class"""
    def __init__(self):
        self.X_wave_length = None
        self.X_train = None
        self.y_train = None
        self.X_train_origin = None
        self.y_train_origin = None

    def load_spectrum_data_preprocess_from_wenxin(self):
        dir = "F:/Datasets/NavelOrange/save/"
        item_num = orange_item_num
        global image_size
        size = image_size
        dim = size * size
        use_sample_num_per_item = 1
        use_sample_num = use_sample_num_per_item
        X_train = np.zeros((item_num * use_sample_num,dim))
        y_train_origin = np.zeros((item_num * use_sample_num,))
        y_train = np.zeros((item_num * use_sample_num,))
        #file_name = "初始数据.txt"
        #file_name = "多元散射校正.txt"
        #file_name = "标准正态变量交换snv.txt"
        #file_name = "S-G.txt"
        file_name = "初始数据.txt"

        path = dir + file_name
        with open(path) as f:
            lines = []
            idx = 0
            it = 0
            start = 1
            end = start + 1531
            for line in f:
                if line != '\n':
                    line,_ = line.split('\n')
                    lines.append(float(line))
                    it+=1
                    if it >= 1531:
                        one_range = np.array(lines)
                        X_train[idx] = one_range[0:dim]
                        idx+=1
                        it = 0
                        lines = []

        dir = "F:/Datasets/NavelOrange/"
        file_name = "sugar_values_origin.txt"
        path = dir + file_name
        with open(path) as f:
            it = 1
            start = 1
            end = start + item_num
            for line in f:
                if it >= start and it < end:
                    orange_idx,sugar_value = line.split()
                    sugar_value = float(sugar_value)
                    y_train_origin[it - 1] = sugar_value
                it+=1
        for i in range(1,use_sample_num):
            y_train_origin[item_num * (i):item_num * (i + 1)] = y_train_origin[0:item_num]

        y_min = np.min(y_train_origin)
        y_max = np.max(y_train_origin)
        ranges = np.linspace(y_min,y_max,11)

        if do_fake_data:
            for i in range(use_sample_num):
                y_train_origin[42 + 60 * i] = 14.0

        y_train = y_train_origin
        if do_split_data:
            split_img_size = 20
            X_train,y_train = self.split_data(X_train,y_train)
            size = split_img_size
            image_size = size

        if use_pca:
            if use_spectrum_info_matrix:
                x_train_info_matrix = self.get_spectral_info_matrix(x_train_pca)
                X_train = x_train_info_matrix.reshape(-1,pca_image_size,pca_image_size,1)
                image_size = pca_image_size
            else:
                size = int(np.sqrt(pca_image_size))
                X_train = x_train_pca.reshape(-1,size,size,1)
                image_size = size
        else:
            X_train = X_train.reshape(-1,size,size,1)

        self.X_train = X_train
        self.y_train = y_train
        self.y_train_origin = y_train_origin
        data = {}
        if fake_3_dimensions:
            X_train = self.construct_3_dimention_image(X_train)

        if use_random_sample:
            cache = self.k_fold_cross_validation(X_train,y_train,data_split_ratio)
            [x_train_data,y_train_data,x_val_data,y_val_data,x_test_data,y_test_data] = cache

            data['X_train'] = x_train_data
            data['y_train'] = y_train_data
            data['X_val'] = x_val_data
            data['y_val'] = y_val_data
            data['X_test'] = x_test_data
            data['y_test'] = y_test_data
        else:
            start_idx = 0
            total_num = item_num * use_sample_num
            train_ratio = data_split_ratio
            train_num = int(total_num * train_ratio)
            test_num = int(total_num * (1 - train_ratio))
            data['X_train'] = X_train[start_idx:train_num]
            data['y_train'] = y_train[start_idx:train_num]
            data['X_val'] = X_train[train_num:train_num + test_num]
            data['y_val'] = y_train[train_num:train_num + test_num]
            data['X_test'] = X_train[train_num:train_num + test_num]
            data['y_test'] = y_train[train_num:train_num + test_num]
        return data

#origin train accuracy 100% range is (-2.1,2.1)s

#regression may be batch size should be larger, because it can represent more patterns
#batch_size = 155
class NeuralNetworks(object):
    pass

def main():
    pass

if __name__ == '__main__':
    main()


# Create your views here.
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from .models import Company, Comment, Recommendations
from .forms import CommentForm, RecommendForm

def company_detail(request, slug):
    template_name = 'company_detail.html'
    company = get_object_or_404(Company, slug=slug)
    comments = company.comments.filter(active=True)
    new_comment = None
    # Comment posted
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        # if comment_form.is_valid(): this can be used to add CSRF token validation

        # Create Comment object but don't save to database yet
        new_comment = comment_form.save(commit=False)
            # Assign the current post to the comment
        new_comment.company = company
            # Save the comment to the database
        new_comment.save()
    else:
        comment_form = CommentForm()

    return render(request, template_name, {'company': company,
                                           'comments': comments,
                                           'new_comment': new_comment,
                                           'comment_form': comment_form})

class companyListView(ListView):
    model = Company
    template_name = 'company_list.html'

class companyDetailView(DetailView):
    model = Company
    template_name = 'company_detail.html'

def recommend_detail(request):
    template_name = 'index.html'
    new_recommend = None
    if request.method == 'POST':
        recommendation_form = RecommendForm(data=request.POST)
        new_recommend = recommendation_form.save(commit=False)
        new_recommend.save()
    else:
        recommendation_form = RecommendForm()
    return render(request, template_name, {'new_recommendation': new_recommend,
                                           'recommendation_form': recommendation_form})


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 25 19:09:30 2017

@author: Achin Jain (achinj@seas.upenn.edu)

"""

from __future__ import division

import numpy as np
from utils import load_data
import matplotlib.pyplot as plt
from plot_data import plot_experiment_design
import cPickle as pickle

building_type = 'LargeHotel'
n_samples = 2000

data_type = 'unconstrained'

load_model = False
#model_type = 'constrained'
model_type = 'unconstrained'
#model_type = 'rulebased'

# load model
if load_model:
    filename = ('../models/gp-%s-%s-numsamples-%s.pickle' %(model_type, building_type, n_samples))
    with open(filename, 'rb') as f:
        (model, buildingType, features_c, features_d, output,
         normalizer_c, normalizer_d, normalizer_y) = pickle.load(f)

Xd_test, Xc_test, y_test = load_data(data_type, building_type, features_d, features_c, output)

# select a sample
idx = 3005

Xd_test = Xd_test.iloc[idx,:]
Xc_test = Xc_test.iloc[idx,:].as_matrix()
print(Xc_test)
y_test = y_test.iloc[idx,:].as_matrix()
Xd_test = normalizer_d.transform(Xd_test.reshape(1,-1))
#Xc_test = normalizer_c.transform(Xc_test.reshape(1,-1))

# vary control variables from min to max
ClgSP = np.linspace(22,32,50).reshape(-1,1)
KitchenClgSP = np.linspace(24,32,50).reshape(-1,1)
GuestClgSP = np.linspace(22,26,50).reshape(-1,1)
SupplyAirSP = np.linspace(12,14,50).reshape(-1,1)
ChwSP = np.linspace(3.7,9.7,50).reshape(-1,1)

#Xc_design = np.concatenate((ClgSP,KitchenClgSP,GuestClgSP,SupplyAirSP,ChwSP), axis=1)
Xc_design = np.concatenate((ClgSP, np.tile(Xc_test[1:], (50,1))), axis=1)
Xc_design = normalizer_c.transform(Xc_design)

Xd_design = np.tile(Xd_test, (50,1))

# predict using GP
X_design = np.concatenate((Xd_design, Xc_design), axis=1)
y_mean, y_std = model.predict(X_design, return_std=True)
y_mean = normalizer_y.inverse_transform(y_mean.reshape(-1, 1))
y_mean = y_mean.flatten()
y_std =y_std*(normalizer_y.data_max_-normalizer_y.data_min_)/2

plt.close('all')
plot_experiment_design(ClgSP.flatten(), y_mean, y_std, Xc_test[0], y_test[0])
#plot_experiment_design(KitchenClgSP.flatten(), y_mean, y_std)
#plot_experiment_design(GuestClgSP.flatten(), y_mean, y_std)
#plot_experiment_design(SupplyAirSP.flatten(), y_mean, y_std)
#plot_experiment_design(ChwSP.flatten(), y_mean, y_std)


import turtle

def draw_square(brad):
    for i in range(0, 4):
        brad.forward(100)
        brad.right(90)

def draw_arc():
    window = turtle.Screen()
    window.bgcolor("red")
    brad = turtle.Turtle()
    for i in range(0, 360, 10):
        draw_square(brad)
        brad.right(10)
    # angie = turtle.Turtle()
    # angie.shape("arrow")
    # angie.color("blue")
    # angie.circle(100)
    window.exitonclick()

draw_arc()


#! /usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
import time
import sys
import os
# import ffmpeg
import datetime
import random
import imp

# imp.reload(sys)
# sys.setdefaultencoding('utf-8')
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from wayang_core.WayangRobotAndroid import WayangRobotAndroid
from wayang_common import verify_utils
from wayang_common import wayang_constance as wc
from wayang_common import pytest_utils

class TestWayangAndroid:

    def setup_class(self):
        self.wayang = WayangRobotAndroid()
        self.server = "ws://qa.agoralab.co:8081/JavaWebSocket/websocket"
        self.live_app_id = wc.APP_ID.LIVE_STATIC
        self.primary_uid = 12345
        self.helper_uid = 67890
        now_time = int(round(time.time() * 1000))
        self.channel_name = "WYrtmpAndroid"
        self.primary = "Harry Potter"
        self.helper = "Ron Weasley"

    def teardown(self):
        try:
            self.wayang.removeAllViews(self.primary, self.server)
            self.wayang.removeAllViews(self.helper, self.server)

            self.wayang.destroy(self.primary, self.server)
            self.wayang.destroy(self.helper, self.server)
        except Exception as e:
            pass

    def teardown_class(self):
        self.wayang.closeConnection()

    def __set_device_qr(self, primary, helper):
        self.primary = primary
        self.helper = helper

    def test_addUser_before_set(self, primary, helper):
        self.__set_device_qr(primary, helper)

        wy = self.wayang
        server = self.server
        app_id = wc.APP_ID.LIVE_STATIC

        # primary join channel
        wy.create(primary, server, app_id)
        wy.setLocalUid(primary, server, localUid=self.primary_uid)
        wy.setChannelProfile(primary, server, wc.CHANNEL_PROFILE_TYPE.BROADCASTING)
        wy.enableVideo(primary, server)
        localview = wy.createRendererView(primary, server)
        remoteview = wy.createRendererView(primary, server)
        wy.setupLocalVideo(primary, server, renderMode=2, view=localview, uid=self.primary_uid)
        wy.setupRemoteVideo(primary, server, renderMode=2, view=remoteview, uid=self.helper_uid)
        wy.setupLayout(primary, server, layout=2)
        wy.setClientRole(primary, server, role=wc.CLIENT_ROLE_TYPE.BROADCASTER)
        wy.joinChannel(primary, server, channelName=self.channel_name)
        verify_utils.verify_android_join_success(wy, primary, server)

        # helper join channel
        wy.create(helper, server, app_id)
        wy.setLocalUid(helper, server, localUid=self.helper_uid)
        wy.setChannelProfile(helper, server, wc.CHANNEL_PROFILE_TYPE.BROADCASTING)
        wy.enableVideo(helper, server)
        localview_h = wy.createRendererView(helper, server)
        remoteview_h = wy.createRendererView(helper, server)
        wy.setupLocalVideo(helper, server, renderMode=2, view=localview_h, uid=self.helper_uid)
        wy.setupRemoteVideo(helper, server, renderMode=2, view=remoteview_h, uid=self.primary_uid)
        wy.setupLayout(helper, server, layout=2)
        wy.setClientRole(helper, server, role=wc.CLIENT_ROLE_TYPE.BROADCASTER)
        wy.joinChannel(helper, server, channelName=self.channel_name)
        verify_utils.verify_android_join_success(wy, helper, server)

        # push stream
        rtmp_room = "2504"
        rtmp_url = "rtmp://vid-130451.push.fastweb.broadcastapp.agoraio.cn/live/" + rtmp_room

        user1 = wy.formatTranscodingUser(self.primary_uid, 0, 0, 360, 320, 1, 1.0, 0)
        user2 = wy.formatTranscodingUser(self.helper_uid, 0, 320, 180, 320, 1, 1.0, 0)

        # wy.addUser(primary, server, user1)
        # wy.addUser(primary, server, user2)
        wy.setLiveTranscoding(primary, server, width=360, height=640,
                              videoBitrate=800, videoFramerate=10, lowLatency=False,
                              videoGop=15,
                              videoCodecProfile="HIGH",
                              audioSampleRate="TYPE_32000",
                              audioBitrate=64, audioChannels=1,
                              userConfigs=[user1],
                              userConfigExtraInfo="",
                              watermark_dict=None,
                              backgroundColor=None)

        wy.addPublishStreamUrl(primary, server, url=rtmp_url, transcodingEnabled=True)

        pytest_utils.execute_manual_step("Publish done, expect both primary and helper video stream")

        wy.leaveChannel(primary, server)
        wy.leaveChannel(helper, server)


import json
import os
import tarfile
import tempfile

import dask.dataframe as dd
import numpy as np
import pandas as pd

from featuretools.entityset.relationship import Relationship
from featuretools.entityset.serialize import FORMATS
from featuretools.utils.gen_utils import check_schema_version, import_or_raise
from featuretools.utils.s3_utils import get_transport_params, use_smartopen_es
from featuretools.utils.wrangle import _is_local_tar, _is_s3, _is_url
from featuretools.variable_types import LatLong, find_variable_types

def description_to_variable(description, entity=None):
    '''Deserialize variable from variable description.

    Args:
        description (dict) : Description of :class:`.Variable`.
        entity (Entity) : Instance of :class:`.Entity` to add :class:`.Variable`. If entity is None, :class:`.Variable` will not be instantiated.

    Returns:
        variable (Variable) : Returns :class:`.Variable`.
    '''
    is_type_string = isinstance(description['type'], str)
    variable = description['type'] if is_type_string else description['type'].pop('value')
    if entity is not None:
        variable_types = find_variable_types()
        variable_class = variable_types.get(variable, variable_types.get('unknown'))
        kwargs = {} if is_type_string else description['type']
        variable = variable_class(description['id'], entity, **kwargs)
        interesting_values = pd.read_json(description['properties']['interesting_values'], typ='series')
        variable.interesting_values = interesting_values
        variable_description = description['properties'].get('description')
        if variable_description is not None and variable_description != 'the "{}"'.format(variable.name):
            variable.description = variable_description

    return variable

def description_to_entity(description, entityset, path=None):
    '''Deserialize entity from entity description and add to entityset.

    Args:
        description (dict) : Description of :class:`.Entity`.
        entityset (EntitySet) : Instance of :class:`.EntitySet` to add :class:`.Entity`.
        path (str) : Root directory to serialized entityset.
    '''
    if path:
        dataframe = read_entity_data(description, path=path)
    else:
        dataframe = empty_dataframe(description)
    variable_types = {variable['id']: (description_to_variable(variable), variable)
                      for variable in description['variables']}
    es = entityset.entity_from_dataframe(
        description['id'],
        dataframe,
        index=description.get('index'),
        time_index=description.get('time_index'),
        secondary_time_index=description['properties'].get('secondary_time_index'),
        variable_types={variable: variable_types[variable][0] for variable in variable_types})
    for variable in es[description['id']].variables:
        interesting_values = variable_types[variable.id][1]['properties']['interesting_values']
        interesting_values = pd.read_json(interesting_values, typ="series")
        variable.interesting_values = interesting_values
        variable_description = variable_types[variable.id][1]['properties'].get('description')
        if variable_description is not None and variable_description != 'the "{}"'.format(variable.name):
            variable.description = variable_description

def description_to_entityset(description, **kwargs):
    '''Deserialize entityset from data description.

    Args:
        description (dict) : Description of an :class:`.EntitySet`. Likely generated using :meth:`.serialize.entityset_to_description`
        kwargs (keywords): Additional keyword arguments to pass as keywords arguments to the underlying deserialization method.

    Returns:
        entityset (EntitySet) : Instance of :class:`.EntitySet`.
    '''
    check_schema_version(description, 'entityset')

    from featuretools.entityset import EntitySet

    # If data description was not read from disk, path is None.
    path = description.get('path')
    entityset = EntitySet(description['id'])

    last_time_index = []
    for entity in description['entities'].values():
        entity['loading_info']['params'].update(kwargs)
        # If path is None, an empty dataframe will be created for entity.
        description_to_entity(entity, entityset, path=path)
        if entity['properties']['last_time_index']:
            last_time_index.append(entity['id'])

    for relationship in description['relationships']:
        relationship = Relationship.from_dictionary(relationship, entityset)
        entityset.add_relationship(relationship)

    if len(last_time_index):
        entityset.add_last_time_indexes(updated_entities=last_time_index)

    return entityset

def empty_dataframe(description):
    '''Deserialize empty dataframe from entity description.

    Args:
        description (dict) : Description of :class:`.Entity`.

    Returns:
        df (DataFrame) : Empty dataframe for entity.
    '''
    columns = [variable['id'] for variable in description['variables']]
    dtypes = description['loading_info']['properties']['dtypes']
    return pd.DataFrame(columns=columns).astype(dtypes)

def read_entity_data(description, path):
    '''Read description data from disk.

    Args:
        description (dict) : Description of :class:`.Entity`.
        path (str): Location on disk to read entity data.

    Returns:
        df (DataFrame) : Instance of dataframe.
    '''
    file = os.path.join(path, description['loading_info']['location'])
    kwargs = description['loading_info'].get('params', {})
    load_format = description['loading_info']['type']
    entity_type = description['loading_info'].get('entity_type', 'pandas')
    dtypes = description['loading_info'].get('properties', {}).get('dtypes')
    read_kwargs = {}
    if entity_type == 'dask':
        lib = dd
        read_kwargs['dtype'] = dtypes
    elif entity_type == 'koalas':
        import_error = 'Cannot load Koalas entityset - unable to import Koalas. ' \
                       'Consider doing a pip install with featuretools[koalas] to install Koalas with pip'
        lib = import_or_raise('databricks.koalas', import_error)
        read_kwargs['multiline'] = True
        kwargs['compression'] = str(kwargs['compression'])
    else:
        lib = pd
    if load_format == 'csv':
        dataframe = lib.read_csv(
            file,
            engine=kwargs['engine'],
            compression=kwargs['compression'],
            encoding=kwargs['encoding'],
            **read_kwargs
        )
    elif load_format == 'parquet':
        dataframe = lib.read_parquet(file, engine=kwargs['engine'])
    elif load_format == 'pickle':
        dataframe = pd.read_pickle(file, **kwargs)
    else:
        error = 'must be one of the following formats: {}'
        raise ValueError(error.format(', '.join(FORMATS)))
    if entity_type == 'koalas':
        for col, dtype in dtypes.items():
            if dtype == 'object':
                dtypes[col] = 'str'
            if dtype == 'datetime64[ns]':
                dtypes[col] = np.datetime64
    dataframe = dataframe.astype(dtypes)

    if load_format in ['parquet', 'csv']:
        latlongs = []
        for var_description in description['variables']:
            if var_description['type']['value'] == LatLong.type_string:
                latlongs.append(var_description["id"])

        def parse_latlong_tuple(x):
            return tuple(float(y) for y in x[1:-1].split(","))

        def parse_latlong_list(x):
            return list(float(y) for y in x[1:-1].split(","))

        for column in latlongs:
            if entity_type == 'dask':
                meta = (column, tuple([float, float]))
                dataframe[column] = dataframe[column].apply(parse_latlong_tuple,
                                                            meta=meta)
            elif entity_type == 'koalas':
                dataframe[column] = dataframe[column].apply(parse_latlong_list)

            else:
                dataframe[column] = dataframe[column].apply(parse_latlong_tuple)

    return dataframe

def read_data_description(path):
    '''Read data description from disk, S3 path, or URL.

        Args:
            path (str): Location on disk, S3 path, or URL to read `data_description.json`.

        Returns:
            description (dict) : Description of :class:`.EntitySet`.
    '''

    path = os.path.abspath(path)
    assert os.path.exists(path), '"{}" does not exist'.format(path)
    filepath = os.path.join(path, 'data_description.json')
    with open(filepath, 'r') as file:
        description = json.load(file)
    description['path'] = path
    return description

def read_entityset(path, profile_name=None, **kwargs):
    '''Read entityset from disk, S3 path, or URL.

        Args:
            path (str): Directory on disk, S3 path, or URL to read `data_description.json`.
            profile_name (str, bool): The AWS profile specified to write to S3. Will default to None and search for AWS credentials.
                Set to False to use an anonymous profile.
            kwargs (keywords): Additional keyword arguments to pass as keyword arguments to the underlying deserialization method.
    '''
    if _is_url(path) or _is_s3(path) or _is_local_tar(str(path)):
        with tempfile.TemporaryDirectory() as tmpdir:
            local_path = path
            transport_params = None

            if _is_s3(path):
                transport_params = get_transport_params(profile_name)

            if _is_s3(path) or _is_url(path):
                local_path = os.path.join(tmpdir, "temporary_es")
                use_smartopen_es(local_path, path, transport_params)

            with tarfile.open(str(local_path)) as tar:
                tar.extractall(path=tmpdir)

            data_description = read_data_description(tmpdir)
            return description_to_entityset(data_description, **kwargs)
    else:
        data_description = read_data_description(path)
        return description_to_entityset(data_description, **kwargs)


import tensorflow as tf

def vat_decay(iterator):
    exponent = iterator / 100
    return 1 / (10 ** exponent)

def kl_divergence_with_logit(q_logit, p_logit):
    q = tf.nn.softmax(q_logit)
    qlogq = tf.reduce_mean(input_tensor=tf.reduce_sum(input_tensor=q * tf.nn.log_softmax(q_logit), axis=1))
    qlogp = tf.reduce_mean(input_tensor=tf.reduce_sum(input_tensor=q * tf.nn.log_softmax(p_logit), axis=1))
    return qlogq - qlogp

def get_normalized_vector(d):
    d /= (1e-12 + tf.reduce_max(input_tensor=tf.abs(d), axis=range(1, len(d.get_shape())), keepdims=True))
    d /= tf.sqrt(1e-6 + tf.reduce_sum(input_tensor=tf.pow(d, 2.0), axis=range(1, len(d.get_shape())), keepdims=True))
    return d

def generate_virtual_adversarial_perturbation(x, logit, forward, xi=1e-8, num_power_iterations=1,
                                              epsilon=8., is_training=True, forward_index=0):
    '''
    :param x:
    :param logit:
    :param forward:
    :param xi: the small constant for finite difference
    :param num_power_iterations: the number of power iterations
    :param epsilon: the norm length for (virtual) adversarial training
    :param is_training:
    :param forward_index:
    :return:
    '''
    d = tf.random.normal(shape=tf.shape(input=x))

    for _ in range(num_power_iterations):
        with tf.GradientTape() as tape:
            tape.watch(d)
            d = xi * get_normalized_vector(d)
            logit_p = logit

            x = x + d
            for i in range(len(forward)):
                x = forward[i](x, training=is_training)
            logit_m = x

            if len(logit_m) == 1:
                dist = kl_divergence_with_logit(logit_p, logit_m)
            else:
                dist = kl_divergence_with_logit(logit_p, logit_m[forward_index])
        grad = tape.gradient(target=dist, sources=d)
        d = grad

    return epsilon * get_normalized_vector(d)

def virtual_adversarial_loss(x, logit, forward, xi=1e-8, num_power_iterations=1, epsilon=8.,
                             is_training=True, name="vat_loss", forward_index=0):
    '''
    :param x:
    :param logit:
    :param forward:
    :param xi: the small constant for finite difference
    :param num_power_iterations: the number of power iterations
    :param epsilon: the norm length for (virtual) adversarial training
    :param is_training:
    :param name:
    :param forward_index:
    :return:
    '''
    r_vadv = generate_virtual_adversarial_perturbation(x, logit, forward, xi=xi, epsilon=epsilon,
                                                       num_power_iterations=num_power_iterations,
                                                       is_training=is_training,
                                                       forward_index=forward_index)
    logit = tf.stop_gradient(logit)
    logit_p = logit

    x = x + r_vadv
    for i in range(len(forward)):
        x = forward[i](x, training=is_training)
    logit_m = x

    if len(logit_m) == 1:
        loss = kl_divergence_with_logit(logit_p, logit_m)
    else:
        loss = kl_divergence_with_logit(logit_p, logit_m[forward_index])
    return tf.identity(loss, name=name)

def generate_adversarial_perturbation(x, loss, epsilon=8.):
    '''
    :param x:
    :param loss:
    :param epsilon: the norm length for (virtual) adversarial training
    :return:
    '''
    grad = tf.gradients(ys=loss, xs=[x], aggregation_method=2)[0]
    grad = tf.stop_gradient(grad)
    return epsilon * get_normalized_vector(grad)

def adversarial_loss(x, y, loss, forward, is_training=True):
    r_adv = generate_adversarial_perturbation(x, loss)
    logit = forward(x + r_adv, is_training=is_training)
    loss = tf.reduce_mean(input_tensor=tf.nn.softmax_cross_entropy_with_logits(logits=logit,
                                                                               labels=tf.stop_gradient(y)))
    return loss


import logging
import re
from urllib.parse import unquote_plus

import markdown
from markdown.preprocessors import Preprocessor
from markdown.inlinepatterns import Pattern
from markdown.extensions import Extension
from flask import url_for

from . import db

class QuotedExtension(Extension):
    def extendMarkdown(self, md, md_globals):
        md.preprocessors.add('inlinezip', InlineZipPreprocessor(), "_begin")
        md.preprocessors.add('inlineimage', InlinePreprocessor(), "_begin")
        md.preprocessors.add('inlinequotation', InlineQuotationPreprocessor(), "_begin")
        md.preprocessors.add('checklist', ChecklistPreprocessor(), "_begin")
        md.preprocessors.add('poem', PoemPreprocessor(), "_begin")
        md.inlinePatterns.add('sourceurl', ObjectUrlPattern("source", db.Source), "_begin")
        md.inlinePatterns.add('seriesurl', ObjectUrlPattern("series", db.Series), "_begin")
        md.inlinePatterns.add('termurl', ObjectUrlPattern("term", db.Term), "_begin")
        md.inlinePatterns.add('scite', ObjectCitePattern("s", db.Source, markdown_instance=md), "_begin")
        md.inlinePatterns.add('secite', ObjectCitePattern("se", db.Series, markdown_instance=md), "_begin")
        md.inlinePatterns.add('ncite', ObjectCitePattern("n", db.Name, markdown_instance=md), "_begin")

class InlinePreprocessor(Preprocessor):
    regex = re.compile(r"\[inline (?P<file_id>\d+)(?:\|(?P
.+?))?\]$")
    def run(self, lines):
        new_lines = []
        for line in lines:
            m = self.regex.fullmatch(line)
            if m:
                try:
                    file_id = int(m.group('file_id'))
                    the_file = db.Session.query(db.File).get(file_id)
                    if not the_file:
                        raise ValueError("File not found.")
                    new_lines.append('
')
                    new_lines.append(the_file.html_display)
                    if m.group('caption'):
                        new_lines.append('
{}
'.format(m.group('caption')))
                    new_lines.append('
')
                except ValueError:
                    new_lines.append(line)
            else:
                new_lines.append(line)
        return new_lines

class InlineQuotationPreprocessor(Preprocessor):
    regex = re.compile(r"\[inlinequotation (?P<quotation_id>\d+)(?:\|(?P
.+?))?\]$")
    def run(self, lines):
        new_lines = []
        for line in lines:
            m = self.regex.fullmatch(line)
            if m:
                try:
                    qlines = []
                    quotation_id = int(m.group('quotation_id'))
                    the_quotation = db.Session.query(db.Quotation).get(quotation_id)
                    if not the_quotation:
                        raise ValueError("Quotation not found.")
                    qlines.append('
')
                    q = ('
'
                         '{}'
                         '{}'
                         '
').format(
                            ''.join(markdown.markdown(line) for line in the_quotation.quotation.splitlines()),
                            "{}".format(the_quotation.source.__class__.__name__, the_quotation.source.internal_url, the_quotation.source))
                    qlines.append(q)
                    if m.group('caption'):
                        qlines.append('
{}
'.format(m.group('caption')))
                    qlines.append('
')
                    new_lines.extend(qlines)
                except ValueError:
                    new_lines.append(line)
            else:
                new_lines.append(line)
        return new_lines

class InlineZipPreprocessor(Preprocessor):
    regex = re.compile(r"\[inlinezip (?P<file_id>\d+)\|(?P<file_name>.+?)(?:\|(?P
.+?))?\]$")
    def run(self, lines):
        new_lines = []
        for line in lines:
            m = self.regex.fullmatch(line)
            if m:
                try:
                    file_id = int(m.group('file_id'))
                    file_name = unquote_plus(m.group('file_name'))
                    the_file = db.Session.query(db.File).get(file_id)
                    if not the_file:
                        raise ValueError("File not found.")
                    new_lines.append('
')
                    new_lines.append(''.format(url_for('viewers.view_cbz_file', file_id=file_id, file=file_name)))
                    if m.group('caption'):
                        new_lines.append('
{}
'.format(m.group('caption')))
                    new_lines.append('
')
                except ValueError:
                    logging.exception("inlinezip failed")
                    new_lines.append(line)
            else:
                new_lines.append(line)
        return new_lines

class ObjectUrlPattern(Pattern):
    def __init__(self, text, object_type):
        Pattern.__init__(self, r"\[{} (\d+)\]".format(text))
        self.text = text
        self.object_type = object_type

    def handleMatch(self, m):
        try:
            object_id = int(m.group(2))
            the_object = db.Session.query(self.object_type).get(object_id)
            if not the_object:
                raise ValueError("Object not found.")
            return the_object.internal_url
        except ValueError:
            return "[{} {}]".format(self.text, m.group(2))

class ObjectCitePattern(Pattern):
    def __init__(self, text, object_type, markdown_instance=None):
        Pattern.__init__(self, r"\[{}cite (\d+)\]".format(text), markdown_instance=markdown_instance)
        self.text = text
        self.object_type = object_type

    def handleMatch(self, m):
        try:
            object_id = int(m.group(2))
            the_object = db.Session.query(self.object_type).get(object_id)
            if not the_object:
                raise ValueError("Object not found.")
            return self.markdown.htmlStash.store("{}".format(the_object.__class__.__name__, the_object.internal_url, the_object))
        except ValueError:
            return "[{}cite {}]".format(self.text, m.group(2))

class ChecklistPreprocessor(Preprocessor):
    start_regex = re.compile(r"```(?:CHECKLIST|checklist)(?: (.+))?$")
    end_regex = re.compile(r"```$")
    cl_regex = re.compile(r"(-|X) (.+)$")

    def run(self, lines):
        checklist_mode = False
        new_lines = []
        checkboxes = 0
        for line in lines:
            if not checklist_mode:
                start_match = self.start_regex.fullmatch(line)
                if start_match:
                    checklist_mode = True
                    new_lines.append('<fieldset class="checklist">')
                    if start_match.group(1):
                        new_lines.append("<legend>{}</legend>".format(start_match.group(1)))
                    continue
                else:
                    new_lines.append(line)
                    continue
            else:
                end_match = self.end_regex.fullmatch(line)
                if end_match:
                    new_lines.append("</fieldset>")
                    checklist_mode = False
                    continue
                else:
                    # this should be a checklist item
                    cl_match = self.cl_regex.fullmatch(line)
                    if cl_match:
                        checkboxes = checkboxes + 1
                        checked = 'checked="checked"' if (cl_match.group(1) == "X") else ''
                        item = cl_match.group(2)
                        new_lines.append('
<input class="autoCheckbox" onchange="toggleCheckbox(event)" type="checkbox" name="checkbox_{}" id="checkbox_{}" {} />{}
'.format(checkboxes, checkboxes, checked, checkboxes, checkboxes, item))
                    elif line[:4] == "    ":
                        new_lines.append("
" + line[4:] + "
")
                    else:
                        # throw away lines not in the right format
                        pass
        return new_lines

class PoemPreprocessor(Preprocessor):
    start_regex = re.compile(r"```(?:POEM|poem)(?: (.+))?$")
    end_regex = re.compile(r"```$")

    def run(self, lines):
        poem_mode = False
        new_lines = []
        caption = None
        for line in lines:
            if not poem_mode:
                start_match = self.start_regex.fullmatch(line)
                if start_match:
                    poem_mode = True
                    new_lines.append('
')
                    if start_match.group(1):
                        caption = start_match.group(1)
                    new_lines.append('
')
                    continue
                else:
                    new_lines.append(line)
                    continue
            else:
                end_match = self.end_regex.fullmatch(line)
                if end_match:
                    new_lines.append("
")
                    if caption:
                        new_lines.append('
{}
'.format(caption))
                    new_lines.append("
")
                    poem_mode = False
                    continue
                else:
                    # this is a line of the poem
                    new_lines.append(line)
        return new_lines


# -*- coding: utf-8 -*-
import PIL.Image as Image
import io
import subprocess
import sys
import os
import tempfile
tesseract_cmd = 'tesseract-bin\\tesseract'

class TesseractError(Exception):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)

def run_tesseract(input_filename, output_filename_base, tess_lang=None, boxes=False):
    command = [tesseract_cmd, input_filename, output_filename_base]
    if tess_lang is not None:
        command += ['-l', tess_lang]
    if boxes:
        command += ['batch.nochop', 'makebox']
    proc = subprocess.Popen(command, stderr=subprocess.PIPE)
    return proc.wait(), proc.stderr.read()

def cleanup(temp_filename):
    try:
        os.remove(temp_filename)
    except OSError:
        pass

def get_errors(error_string):
    lines = error_string.splitlines()
    error_lines = tuple(line for line in lines if line.find('Error') >= 0)
    if len(error_lines) > 0:
        return '\n'.join(error_lines)
    else:
        return error_string.strip()

def tempnam():
    stderr = sys.stderr
    try:
        sys.stderr = io.StringIO()
        tmpfile = tempfile.TemporaryFile(prefix='tess_')
        return tmpfile.name
    finally:
        sys.stderr = stderr

def image_to_string(image_inp, tess_lang=None, boxes=False):
    input_file_name = '%s.bmp' % tempnam()
    output_file_name_base = tempnam()
    if not boxes:
        output_file_name = '%s.txt' % output_file_name_base
    else:
        output_file_name = '%s.box' % output_file_name_base
    try:
        image_inp.save(input_file_name)
        status, error_string = run_tesseract(input_file_name,
                                             output_file_name_base,
                                             tess_lang=tess_lang,
                                             boxes=boxes)
        if status:
            errors = get_errors(error_string)
            raise TesseractError(status, errors)
        f = open(output_file_name, 'r')
        try:
            return f.read().strip()
        finally:
            f.close()
    finally:
        cleanup(input_file_name)
        cleanup(output_file_name)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        filename = sys.argv[1]
        try:
            image = Image.open(filename)
        except IOError:
            raise Exception('ERROR: Could not open file "%s"\n' % filename)
        print(image_to_string(image))
    elif len(sys.argv) == 4 and sys.argv[1] == '-l':
        lang = sys.argv[2]
        filename = sys.argv[3]
        try:
            image = Image.open(filename)
        except IOError:
            raise Exception('ERROR: Could not open file "%s"\n' % filename)
        print(image_to_string(image, tess_lang=lang))
    else:
        sys.stderr.write('Usage: python tesseract.py [-l language] input_file\n')
        exit(2)


from flask import Flask, request, render_template, send_from_directory
from protolib.python import document_pb2, rpc_pb2
from utils.rpc.grpcapi import GrpcInterface
from utils.helper import DocHelper
import json

app = Flask(__name__, static_url_path='/brat', static_folder='brat')

def process_one_document(request):
    interface = GrpcInterface(host='128.4.20.169')
    # interface = grpcapi.GrpcInterface(host='localhost')
    response = interface.process_document(request)
    assert len(response.document) == 1
    return response.document[0]

def parse_using_bllip(doc):
    request = rpc_pb2.Request()
    request.request_type = rpc_pb2.Request.PARSE_BLLIP
    request.document.extend([doc])
    return process_one_document(request)

def parse_using_stanford(doc):
    request = rpc_pb2.Request()
    request.request_type = rpc_pb2.Request.PARSE_STANFORD
    request.document.extend([doc])
    return process_one_document(request)

def split_sentence_using_stanford(doc):
    request = rpc_pb2.Request()
    request.request_type = rpc_pb2.Request.SPLIT
    request.document.extend([doc])
    return process_one_document(request)

def get_brat_data(parsed_doc):
    helper = DocHelper(parsed_doc)
    # Convert the dependency parse to json, which can be visualized by brat.
    # Support multiple sentences.
    brat_sentences = {}
    for sentence in parsed_doc.sentence:
        brat_data = helper.dependency_for_brat(sentence)
        brat_sentences[sentence.index] = brat_data
    return brat_sentences

@app.route('/brat/<path:path>')
def send_js(path):
    return send_from_directory('brat', path)

@app.route("/", methods=['GET', 'POST'])
def parse():
    if request.method == 'POST':
        text = request.form['text']
        split = request.form.getlist('split')

        doc = document_pb2.Document()
        doc.text = text

        parse_doc_bllip_brat = ''
        parse_doc_stanford_brat = ''
        split_doc_brat = ''
        cst_parses = ''

        if len(split) == 0:
            parse_doc_bllip = parse_using_bllip(doc)
            parse_doc_stanford = parse_using_stanford(doc)
            parse_doc_bllip_brat = json.dumps(get_brat_data(parse_doc_bllip))
            parse_doc_stanford_brat = json.dumps(get_brat_data(parse_doc_stanford))
            parses = {}
            for sentence in parse_doc_bllip.sentence:
                parses[sentence.index] = sentence.parse
            cst_parses = json.dumps(parses)
        else:
            split_doc = split_sentence_using_stanford(doc)
            split_doc_brat = json.dumps(get_brat_data(split_doc))

        return render_template('index.html', text=text,
                               parse_bllip=parse_doc_bllip_brat,
                               parse_stanford=parse_doc_stanford_brat,
                               split_stanford=split_doc_brat,
                               bllip_cst_parses=cst_parses)
    else:
        return render_template('index.html')

@app.route('/highlight', methods=['GET', 'POST'])
def highlight():
    if request.method == 'POST':
        text = request.form['text'].strip()
        text = text.replace('<Gene>', '')
        text = text.replace('</Gene>', '')
        text = text.replace('<MiRNA>', '')
        text = text.replace('</MiRNA>', '')
        text = text.replace('<Subcellular_location>', '')
        text = text.replace('</Subcellular_location>', '')

        lines = text.split('\n')
        return render_template('highlight.html', lines=lines)
    else:
        return render_template('highlight.html')

if __name__ == "__main__":
    app.debug = False
    app.run(host='0.0.0.0')


from urllib.parse import quote_plus
from tbl.interfaces.base import BaseInterface

class Facebook(BaseInterface):
    api_url = 'https://graph.facebook.com/v2.7/{page_id}/feed?message={message}\
    &link={link}&access_token={access_token}'

    def __init__(self, token, page_id):
        self.token = token
        self.page_id = page_id
        super().__init__()

    async def post(self, link, *args, **kwargs):
        url = self.api_url.format(
            page_id=self.page_id,
            message=quote_plus(kwargs.get('message', '')),
            link=quote_plus(link),
            access_token=self.token,
        )

        with self.session as session:
            async with session.post(url) as resp:
                return resp.status == 200


from __future__ import division
import torch
import math
import random
import cv2
import numpy as np
import numbers
import types
import collections
import warnings

def _is_tensor_image(img):
    return torch.is_tensor(img) and img.ndimension() == 3

def _is_numpy_image(img):
    return isinstance(img, np.ndarray) and (img.ndim in {2, 3})

def to_tensor(pic):
    """Convert a ``numpy.ndarray`` to tensor.

    See ``ToTensor`` for more details.

    Args:
        pic (numpy.ndarray): Image to be converted to tensor.

    Returns:
        Tensor: Converted image.
    """
    if not _is_numpy_image(pic):
        raise TypeError('pic should be ndarray. Got {}'.format(type(pic)))
    # handle numpy array
    img = torch.from_numpy(pic.transpose((2, 0, 1)))
    # backward compatibility
    if isinstance(img, torch.ByteTensor):
        return img.float().div(255)
    else:
        return img

'''
def to_pil_image(pic, mode=None):
    """Convert a tensor or an ndarray to PIL Image.

    See :class:`~torchvision.transforms.ToPIlImage` for more details.

    Args:
        pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.
        mode (`PIL.Image mode`_): color space and pixel depth of input data (optional).

    .. _PIL.Image mode: http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#modes

    Returns:
        PIL Image: Image converted to PIL Image.
    """
    if not(_is_numpy_image(pic) or _is_tensor_image(pic)):
        raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))

    npimg = pic
    if isinstance(pic, torch.FloatTensor):
        pic = pic.mul(255).byte()
    if torch.is_tensor(pic):
        npimg = np.transpose(pic.numpy(), (1, 2, 0))

    if not isinstance(npimg, np.ndarray):
        raise TypeError('Input pic must be a torch.Tensor or NumPy ndarray, ' +
                        'not {}'.format(type(npimg)))

    if npimg.shape[2] == 1:
        expected_mode = None
        npimg = npimg[:, :, 0]
        if npimg.dtype == np.uint8:
            expected_mode = 'L'
        elif npimg.dtype == np.int16:
            expected_mode = 'I;16'
        elif npimg.dtype == np.int32:
            expected_mode = 'I'
        elif npimg.dtype == np.float32:
            expected_mode = 'F'
        if mode is not None and mode != expected_mode:
            raise ValueError("Incorrect mode ({}) supplied for input type {}. Should be {}"
                             .format(mode, np.dtype, expected_mode))
        mode = expected_mode

    elif npimg.shape[2] == 4:
        permitted_4_channel_modes = ['RGBA', 'CMYK']
        if mode is not None and mode not in permitted_4_channel_modes:
            raise ValueError("Only modes {} are supported for 4D inputs".format(permitted_4_channel_modes))

        if mode is None and npimg.dtype == np.uint8:
            mode = 'RGBA'
    else:
        permitted_3_channel_modes = ['RGB', 'YCbCr', 'HSV']
        if mode is not None and mode not in permitted_3_channel_modes:
            raise ValueError("Only modes {} are supported for 3D inputs".format(permitted_3_channel_modes))
        if mode is None and npimg.dtype == np.uint8:
            mode = 'RGB'

    if mode is None:
        raise TypeError('Input type {} is not supported'.format(npimg.dtype))

    return Image.fromarray(npimg, mode=mode)
'''

def normalize(tensor, mean, std):
    """Normalize a tensor image with mean and standard deviation.

    See ``Normalize`` for more details.

    Args:
        tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
        mean (sequence): Sequence of means for each channel.
        std (sequence): Sequence of standard deviations for each channely.

    Returns:
        Tensor: Normalized Tensor image.
    """
    if not _is_tensor_image(tensor):
        raise TypeError('tensor is not a torch image.')
    # TODO: make efficient
    for t, m, s in zip(tensor, mean, std):
        t.sub_(m).div_(s)
    return tensor

def normalize_caffe(tensor, mean, scale):
    if not _is_tensor_image(tensor):
        raise TypeError('tensor is not a torch image.')
    for t, m, s in zip(tensor, mean, scale):
        t.sub_(m).mul_(s)
    return tensor

def resize(img, size, interpolation=cv2.INTER_LINEAR):
    """Resize the input CV2 Image to the given size.

    Args:
        img (CV2 Image): Image to be resized.
        size (sequence or int): Desired output size. If size is a sequence like
            (h, w), the output size will be matched to this. If size is an int,
            the smaller edge of the image will be matched to this number maintaing
            the aspect ratio. i.e, if height > width, then image will be rescaled to
            (size * height / width, size)
        interpolation (int, optional): Desired interpolation. Default is
            ``CV2.INTER_LINEAR``

    Returns:
        CV2 Image: Resized image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparrary Image. Got {}'.format(type(img)))
    if not (isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)):
        raise TypeError('Got inappropriate size arg: {}'.format(size))

    if isinstance(size, int):
        h, w = img.shape[:2]
        if (w <= h and w == size) or (h <= w and h == size):
            return img
        if w < h:
            ow = size
            oh = int(size * h / w)
            return cv2.resize(img, (ow, oh), interpolation)
        else:
            oh = size
            ow = int(size * w / h)
            return cv2.resize(img, (ow, oh), interpolation)
    else:
        return cv2.resize(img, tuple(size[::-1]), interpolation)

def scale(*args, **kwargs):
    warnings.warn("The use of the transforms.Scale transform is deprecated, " +
                  "please use transforms.Resize instead.")
    return resize(*args, **kwargs)

def pad(img, padding, fill=0, padding_mode='constant'):
    """Pad the given CV2 Image on all sides with speficified padding mode and fill value.

    Args:
        img (CV2 Image): Image to be padded.
        padding (int or tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
            constant: pads with a constant value, this value is specified with fill
            edge: pads with the last value on the edge of the image
            reflect: pads with reflection of image (without repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                will result in [3, 2, 1, 2, 3, 4, 3, 2]
            symmetric: pads with reflection of image (repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                will result in [2, 1, 1, 2, 3, 4, 4, 3]

    Returns:
        CV2 Image: Padded image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    if not isinstance(padding, (numbers.Number, tuple)):
        raise TypeError('Got inappropriate padding arg')
    if not isinstance(fill, (numbers.Number, str, tuple)):
        raise TypeError('Got inappropriate fill arg')
    if not isinstance(padding_mode, str):
        raise TypeError('Got inappropriate padding_mode arg')

    if isinstance(padding, collections.Sequence) and len(padding) not in [2, 4]:
        raise ValueError("Padding must be an int or a 2, or 4 element tuple, not a " +
                         "{} element tuple".format(len(padding)))

    assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric'], \
        'Padding mode should be either constant, edge, reflect or symmetric'

    if isinstance(padding, int):
        pad_left = pad_right = pad_top = pad_bottom = padding
    if isinstance(padding, collections.Sequence) and len(padding) == 2:
        pad_left = pad_right = padding[0]
        pad_top = pad_bottom = padding[1]
    if isinstance(padding, collections.Sequence) and len(padding) == 4:
        pad_left = padding[0]
        pad_top = padding[1]
        pad_right = padding[2]
        pad_bottom = padding[3]

    if padding_mode == 'constant':
        if isinstance(fill, int):
            fill_R = fill_G = fill_B = fill
        else:
            fill_R = fill[0]
            fill_G = fill[1]
            fill_B = fill[2]
        img_b, img_g, img_r = cv2.split(img)
        img_b = np.pad(img_b, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode, constant_values=((fill_B, fill_B),(fill_B, fill_B)))
        img_g = np.pad(img_g, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode, constant_values=((fill_G, fill_G),(fill_G, fill_G)))
        img_r = np.pad(img_r, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode, constant_values=((fill_R, fill_R),(fill_R, fill_R)))
        img = cv2.merge([img_b,img_g,img_r])
    else:
        if len(img.shape) == 3:
            img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right), (0, 0)), padding_mode)

    return img

def crop(img, i, j, h, w):
    """Crop the given CV2 Image.

    Args:
        img (CV2 Image): Image to be cropped.
        i: Upper pixel coordinate.
        j: Left pixel coordinate.
        h: Height of the cropped image.
        w: Width of the cropped image.

    Returns:
        CV2 Image: Cropped image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    return img[i:i+h, j:j+w]

def center_crop(img, output_size):
    if isinstance(output_size, numbers.Number):
        output_size = (int(output_size), int(output_size))
    h, w = img.shape[:2]
    th, tw = output_size
    i = int(round((h - th) / 2.))
    j = int(round((w - tw) / 2.))
    return crop(img, i, j, th, tw)

def resized_crop(img, i, j, h, w, size, interpolation=cv2.INTER_LINEAR):
    """Crop the given CV2 Image and resize it to desired size.

    Notably used in RandomResizedCrop.

    Args:
        img (CV2 Image): Image to be cropped.
        i: Upper pixel coordinate.
        j: Left pixel coordinate.
        h: Height of the cropped image.
        w: Width of the cropped image.
        size (sequence or int): Desired output size. Same semantics as ``scale``.
        interpolation (int, optional): Desired interpolation. Default is
            ``cv2.INTER_LINEAR``.
    Returns:
        CV2 Image: Cropped image.
    """
    assert _is_numpy_image(img), 'img should be nparray Image'
    img = crop(img, i, j, h, w)
    img = resize(img, size, interpolation)
    return img

def hflip(img):
    """Horizontally flip the given PIL Image.

    Args:
        img (PIL Image): Image to be flipped.

    Returns:
        PIL Image:  Horizontall flipped image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    return cv2.flip(img, 1)

def vflip(img):
    """Vertically flip the given PIL Image.

    Args:
        img (PIL Image): Image to be flipped.

    Returns:
        PIL Image:  Vertically flipped image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    return cv2.flip(img, 0)

def five_crop(img, size):
    """Crop the given PIL Image into four corners and the central crop.

    .. Note::
        This transform returns a tuple of images and there may be a
        mismatch in the number of inputs and targets your ``Dataset`` returns.

    Args:
       size (sequence or int): Desired output size of the crop. If size is an
           int instead of sequence like (h, w), a square crop (size, size) is
           made.
    Returns:
        tuple: tuple (tl, tr, bl, br, center) corresponding top left,
            top right, bottom left, bottom right and center crop.
    """
    if isinstance(size, numbers.Number):
        size = (int(size), int(size))
    else:
        assert len(size) == 2, "Please provide only two dimensions (h, w) for size."

    # w, h = img.size
    h, w = img.shape[:2]
    crop_h, crop_w = size
    if crop_w > w or crop_h > h:
        raise ValueError("Requested crop size {} is bigger than input size {}".format(size,
                                                                                (h, w)))
    # img[i:i+h, j:j+w]
    # tl = img.crop((0, 0, crop_w, crop_h))
    tl = img[0:0+crop_h, 0:0+crop_w]
    # tr = img.crop((w - crop_w, 0, w, crop_h))
    tr = img[0:0+crop_h, w-crop_w:]
    # bl = img.crop((0, h - crop_h, crop_w, h))
    bl = img[h-crop_h:, 0:0+crop_w]
    # br = img.crop((w - crop_w, h - crop_h, w, h))
    br = img[h-crop_h:,w-crop_w:]
    center = center_crop(img, (crop_h, crop_w))
    return (tl, tr, bl, br, center)

def ten_crop(img, size, vertical_flip=False):
    """Crop the given PIL Image into four corners and the central crop plus the
       flipped version of these (horizontal flipping is used by default).

    .. Note::
        This transform returns a tuple of images and there may be a
        mismatch in the number of inputs and targets your ``Dataset`` returns.

       Args:
           size (sequence or int): Desired output size of the crop. If size is an
               int instead of sequence like (h, w), a square crop (size, size) is
               made.
           vertical_flip (bool): Use vertical flipping instead of horizontal

        Returns:
            tuple: tuple (tl, tr, bl, br, center, tl_flip, tr_flip, bl_flip,
                br_flip, center_flip) corresponding top left, top right,
                bottom left, bottom right and center crop and same for the
                flipped image.
    """
    if isinstance(size, numbers.Number):
        size = (int(size), int(size))
    else:
        assert len(size) == 2, "Please provide only two dimensions (h, w) for size."

    first_five = five_crop(img, size)

    if vertical_flip:
        img = vflip(img)
    else:
        img = hflip(img)

    second_five = five_crop(img, size)
    return first_five + second_five

def adjust_brightness(img, brightness_factor):
    """Adjust brightness of an Image.

    Args:
        img (np.ndarray): CV Image to be adjusted.
        brightness_factor (float):  How much to adjust the brightness. Can be
            any non negative number. 0 gives a black image, 1 gives the
            original image while 2 increases the brightness by a factor of 2.

    Returns:
        np.ndarray: Brightness adjusted image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be CV Image. Got {}'.format(type(img)))

    im = img.astype(np.float32) * brightness_factor
    im = im.clip(min=0, max=255)
    return im.astype(img.dtype)

def adjust_contrast(img, contrast_factor):
    """Adjust contrast of an Image.

    Args:
        img (np.ndarray): CV Image to be adjusted.
        contrast_factor (float): How much to adjust the contrast. Can be any
            non negative number. 0 gives a solid gray image, 1 gives the
            original image while 2 increases the contrast by a factor of 2.

    Returns:
        np.ndarray: Contrast adjusted image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be CV Image. Got {}'.format(type(img)))
    im = img.astype(np.float32)
    mean = round(cv2.cvtColor(im, cv2.COLOR_RGB2GRAY).mean())
    im = (1-contrast_factor)*mean + contrast_factor * im
    im = im.clip(min=0, max=255)
    return im.astype(img.dtype)

def adjust_saturation(img, saturation_factor):
    """Adjust color saturation of an image.

    Args:
        img (np.ndarray): CV Image to be adjusted.
        saturation_factor (float):  How much to adjust the saturation. 0 will
            give a gray image, 1 will give the original image while
            2 will enhance the saturation by a factor of 2.

    Returns:
        np.ndarray: Saturation adjusted image.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    im = img.astype(np.float32)
    degenerate = cv2.cvtColor(cv2.cvtColor(im, cv2.COLOR_RGB2GRAY), cv2.COLOR_GRAY2RGB)
    im = (1-saturation_factor) * degenerate + saturation_factor * im
    im = im.clip(min=0, max=255)
    return im.astype(img.dtype)

def adjust_hue(img, hue_factor):
    """Adjust hue of an image.

    The image hue is adjusted by converting the image to HSV and
    cyclically shifting the intensities in the hue channel (H).
    The image is then converted back to original image mode.

    `hue_factor` is the amount of shift in H channel and must be in the
    interval `[-0.5, 0.5]`.

    See https://en.wikipedia.org/wiki/Hue for more details on Hue.

    Args:
        img (np.ndarray): CV Image to be adjusted.
        hue_factor (float):  How much to shift the hue channel. Should be in
            [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
            HSV space in positive and negative direction respectively.
            0 means no shift. Therefore, both -0.5 and 0.5 will give an image
            with complementary colors while 0 gives the original image.

    Returns:
        np.ndarray: Hue adjusted image.
    """
    if not(-0.5 <= hue_factor <= 0.5):
        raise ValueError('hue_factor is not in [-0.5, 0.5].'.format(hue_factor))

    if not _is_numpy_image(img):
        raise TypeError('img should be CV Image. Got {}'.format(type(img)))

    im = img.astype(np.uint8)
    hsv = cv2.cvtColor(im, cv2.COLOR_RGB2HSV_FULL)
    hsv[..., 0] += np.uint8(hue_factor * 255)

    im = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB_FULL)
    return im.astype(img.dtype)

def adjust_gamma(img, gamma, gain=1):
    """Perform gamma correction on an image.

    Also known as Power Law Transform. Intensities in RGB mode are adjusted
    based on the following equation:

        I_out = 255 * gain * ((I_in / 255) ** gamma)

    See https://en.wikipedia.org/wiki/Gamma_correction for more details.

    Args:
        img (CV2 Image): CV2 Image to be adjusted.
        gamma (float): Non negative real number. gamma larger than 1 make the
            shadows darker, while gamma smaller than 1 make dark regions
            lighter.
        gain (float): The constant multiplier.
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    if gamma < 0:
        raise ValueError('Gamma should be a non-negative real number')

    # input_mode = img.mode
    # img = img.convert('RGB')

    # gamma_map = [255 * gain * pow(ele / 255., gamma) for ele in range(256)] * 3
    img = (255 * gain * ((img / 255) ** gamma)).astype(np.uint8)
    # img = img.point(gamma_map)  # use PIL's point-function to accelerate this part

    # img = img.convert(input_mode)
    return img

def rotate(img, angle, resample=False, expand=False, center=None):
    """Rotate the image by angle.

    Args:
        img (CV2 Image): CV2 Image to be rotated.
        angle ({float, int}): In degrees degrees counter clockwise order.
        resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
            An optional resampling filter.
            See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters
            If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST.
        expand (bool, optional): Optional expansion flag.
            If true, expands the output image to make it large enough to hold the entire rotated image.
            If false or omitted, make the output image the same size as the input image.
            Note that the expand flag assumes rotation around the center and no translation.
        center (2-tuple, optional): Optional center of rotation.
            Origin is the upper left corner.
            Default is the center of the image.
    """

    if not _is_numpy_image(img):
        raise TypeError('img should be nparray Image. Got {}'.format(type(img)))

    if center == None:
        h, w = img.shape[:2]
        center = (w // 2, h // 2)
    if expand == True:
        ratio = math.sin(2*math.pi*angle/360) * w / h + math.cos(2*math.pi*angle/360)
    else:
        ratio = 1
    M = cv2.getRotationMatrix2D(center, angle, ratio)

    return cv2.warpAffine(img, M, (h, w))

def affine(img, angle=0, translate=(0, 0), scale=1, shear=0, resample='BILINEAR', fillcolor=(0,0,0)):
    """Apply affine transformation on the image keeping image center invariant
    Args:
        img (np.ndarray): PIL Image to be rotated.
        angle ({float, int}): rotation angle in degrees between -180 and 180, clockwise direction.
        translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
        scale (float): overall scale
        shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
        resample ({NEAREST, BILINEAR, BICUBIC}, optional):
            An optional resampling filter.
            See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters
            If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST.
        fillcolor (int or tuple): Optional fill color for the area outside the transform in the output image. (Pillow>=5.0.0)
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be CV Image. Got {}'.format(type(img)))

    assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
        "Argument translate should be a list or tuple of length 2"

    assert scale > 0.0, "Argument scale should be positive"

    rows, cols, _ = img.shape
    center = (cols * 0.5, rows * 0.5)
    angle = math.radians(angle)
    shear = math.radians(shear)
    M00 = math.cos(angle)*scale
    M01 = -math.sin(angle+shear)*scale
    M10 = math.sin(angle)*scale
    M11 = math.cos(angle+shear)*scale
    M02 = center[0] - center[0]*M00 - center[1]*M01 + translate[0]
    M12 = center[1] - center[0]*M10 - center[1]*M11 + translate[1]
    affine_matrix = np.array([[M00, M01, M02], [M10, M11, M12]], dtype=np.float32)
    dst_img = cv2.warpAffine(img, affine_matrix, (cols, rows), flags=INTER_MODE[resample],
                             borderMode=cv2.BORDER_CONSTANT, borderValue=fillcolor)
    return dst_img

def perspective(img, fov=45, anglex=0, angley=0, anglez=0, shear=0,
                translate=(0, 0), scale=(1, 1), resample='BILINEAR', fillcolor=(0, 0, 0)):
    """

    This function is partly referred to https://blog.csdn.net/dcrmg/article/details/80273818

    """

    imgtype = img.dtype
    h, w, _ = img.shape
    centery = h * 0.5
    centerx = w * 0.5

    alpha = math.radians(shear)
    beta = math.radians(anglez)

    lambda1 = scale[0]
    lambda2 = scale[1]

    tx = translate[0]
    ty = translate[1]

    sina = math.sin(alpha)
    cosa = math.cos(alpha)
    sinb = math.sin(beta)
    cosb = math.cos(beta)

    M00 = cosb * (lambda1 * cosa ** 2 + lambda2 * sina ** 2) - sinb * (lambda2 - lambda1) * sina * cosa
    M01 = - sinb * (lambda1 * sina ** 2 + lambda2 * cosa ** 2) + cosb * (lambda2 - lambda1) * sina * cosa

    M10 = sinb * (lambda1 * cosa ** 2 + lambda2 * sina ** 2) + cosb * (lambda2 - lambda1) * sina * cosa
    M11 = + cosb * (lambda1 * sina ** 2 + lambda2 * cosa ** 2) + sinb * (lambda2 - lambda1) * sina * cosa
    M02 = centerx - M00 * centerx - M01 * centery + tx
    M12 = centery - M10 * centerx - M11 * centery + ty
    affine_matrix = np.array([[M00, M01, M02], [M10, M11, M12], [0, 0, 1]], dtype=np.float32)
    # -------------------------------------------------------------------------------
    z = np.sqrt(w ** 2 + h ** 2) / 2 / np.tan(math.radians(fov / 2))

    radx = math.radians(anglex)
    rady = math.radians(angley)

    sinx = math.sin(radx)
    cosx = math.cos(radx)
    siny = math.sin(rady)
    cosy = math.cos(rady)

    r = np.array([[cosy, 0, -siny, 0],
                  [-siny * sinx, cosx, -sinx * cosy, 0],
                  [cosx * siny, sinx, cosx * cosy, 0],
                  [0, 0, 0, 1]])

    pcenter = np.array([centerx, centery, 0, 0], np.float32)

    p1 = np.array([0, 0, 0, 0], np.float32) - pcenter
    p2 = np.array([w, 0, 0, 0], np.float32) - pcenter
    p3 = np.array([0, h, 0, 0], np.float32) - pcenter
    p4 = np.array([w, h, 0, 0], np.float32) - pcenter

    dst1 = r.dot(p1)
    dst2 = r.dot(p2)
    dst3 = r.dot(p3)
    dst4 = r.dot(p4)

    list_dst = [dst1, dst2, dst3, dst4]

    org = np.array([[0, 0],
                    [w, 0],
                    [0, h],
                    [w, h]], np.float32)

    dst = np.zeros((4, 2), np.float32)

    for i in range(4):
        dst[i, 0] = list_dst[i][0] * z / (z - list_dst[i][2]) + pcenter[0]
        dst[i, 1] = list_dst[i][1] * z / (z - list_dst[i][2]) + pcenter[1]

    perspective_matrix = cv2.getPerspectiveTransform(org, dst)
    total_matrix = perspective_matrix @ affine_matrix

    result_img = cv2.warpPerspective(img, total_matrix, (w, h), flags=INTER_MODE[resample],
                                     borderMode=cv2.BORDER_CONSTANT, borderValue=fillcolor)
    return result_img.astype(imgtype)

def gaussian_noise(img: np.ndarray, mean, std):
    imgtype = img.dtype
    gauss = np.random.normal(mean, std, img.shape).astype(np.float32)
    noisy = np.clip((1 + gauss) * img.astype(np.float32), 0, 255)
    return noisy.astype(imgtype)

def poisson_noise(img):
    imgtype = img.dtype
    img = img.astype(np.float32)/255.0
    vals = len(np.unique(img))
    vals = 2 ** np.ceil(np.log2(vals))
    noisy = 255 * np.clip(np.random.poisson(img.astype(np.float32) * vals) / float(vals), 0, 1)
    return noisy.astype(imgtype)

def salt_and_pepper(img, prob=0.01):

    ''' Adds "Salt & Pepper" noise to an image.
        prob: probability (threshold) that controls level of noise
    '''

    imgtype = img.dtype
    rnd = np.random.rand(img.shape[0], img.shape[1])
    noisy = img.copy()
    noisy[rnd < prob/2] = 0.0
    noisy[rnd > 1 - prob/2] = 255.0
    return noisy.astype(imgtype)

'''
def _get_inverse_affine_matrix(center, angle, translate, scale, shear):
    # Helper method to compute inverse matrix for affine transformation

    # As it is explained in PIL.Image.rotate
    # We need compute INVERSE of affine transformation matrix: M = T * C * RSS * C^-1
    # where T is translation matrix: [1, 0, tx | 0, 1, ty | 0, 0, 1]
    #       C is translation matrix to keep center: [1, 0, cx | 0, 1, cy | 0, 0, 1]
    #       RSS is rotation with scale and shear matrix
    #       RSS(a, scale, shear) = [ cos(a)*scale    -sin(a + shear)*scale     0]
    #                              [ sin(a)*scale    cos(a + shear)*scale     0]
    #                              [     0                  0          1]
    # Thus, the inverse is M^-1 = C * RSS^-1 * C^-1 * T^-1

    angle = math.radians(angle)
    shear = math.radians(shear)
    scale = 1.0 / scale

    # Inverted rotation matrix with scale and shear
    d = math.cos(angle + shear) * math.cos(angle) + math.sin(angle + shear) * math.sin(angle)
    matrix = [
        math.cos(angle + shear), math.sin(angle + shear), 0,
        -math.sin(angle), math.cos(angle), 0
    ]
    matrix = [scale / d * m for m in matrix]

    # Apply inverse of translation and of center translation: RSS^-1 * C^-1 * T^-1
    matrix[2] += matrix[0] * (-center[0] - translate[0]) + matrix[1] * (-center[1] - translate[1])
    matrix[5] += matrix[3] * (-center[0] - translate[0]) + matrix[4] * (-center[1] - translate[1])

    # Apply center translation: C * RSS^-1 * C^-1 * T^-1
    matrix[2] += center[0]
    matrix[5] += center[1]
    return matrix

def affine(img, angle, translate, scale, shear, resample=0, fillcolor=None):
    """Apply affine transformation on the image keeping image center invariant

    Args:
        img (CV2 Image): CV2 Image to be rotated.
        angle ({float, int}): rotation angle in degrees between -180 and 180, clockwise direction.
        translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
        scale (float): overall scale
        shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
        resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
            An optional resampling filter.
            See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters
            If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST.
        fillcolor (int): Optional fill color for the area outside the transform in the output image. (Pillow>=5.0.0)
    """
    if not _is_pil_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
        "Argument translate should be a list or tuple of length 2"

    assert scale > 0.0, "Argument scale should be positive"

    output_size = img.size
    center = (img.size[0] * 0.5 + 0.5, img.size[1] * 0.5 + 0.5)
    matrix = _get_inverse_affine_matrix(center, angle, translate, scale, shear)
    kwargs = {"fillcolor": fillcolor} if PILLOW_VERSION[0] == '5' else {}
    return img.transform(output_size, Image.AFFINE, matrix, resample, **kwargs)

def to_grayscale(img, num_output_channels=1):
    """Convert image to grayscale version of image.

    Args:
        img (PIL Image): Image to be converted to grayscale.

    Returns:
        PIL Image:  Grayscale version of the image.
                    if num_output_channels == 1 : returned image is single channel
                    if num_output_channels == 3 : returned image is 3 channel with r == g == b
    """
    if not _is_pil_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    if num_output_channels == 1:
        img = img.convert('L')
    elif num_output_channels == 3:
        img = img.convert('L')
        np_img = np.array(img, dtype=np.uint8)
        np_img = np.dstack([np_img, np_img, np_img])
        img = Image.fromarray(np_img, 'RGB')
    else:
        raise ValueError('num_output_channels should be either 1 or 3')

    return img
'''


'''
@Description
@Author: weihan-Chen
@Github: https://github.com/weihan-Chen
@Date: 2020-04-14 01:25:49
LastEditTime: 2022-03-08 13:48:53
'''

# 引入随机数生成模块
import random

# 设置人物数量和队伍数量
while True:
    people_summary = int(input('人物数量：'))
    group_summary = int(input('队伍数量：'))
    a = people_summary % group_summary
    if a != 0:
        print('不能平均分配，请重新输入')
    else:
        break

# 生成人物编号与所在队伍的字典
people_number = [1]
cnt = 2
while cnt <= people_summary:
    people_number.append(cnt)
    cnt += 1
people_group = dict.fromkeys(people_number, 0)

# 随机将所有人放入不同的队伍
cnt = 1
summary = [
    0,
]
i = 2
while i <= group_summary:
    summary.append(0)
    i += 1
while cnt <= people_summary:
    people_group[cnt] = num = random.randint(1, group_summary)
    summary[num - 1] += 1
    if summary[num - 1] >= people_summary / group_summary + 1:
        summary[num - 1] -= 1
        continue
    cnt += 1
# 输出结果
print('每组人数', summary[0])
i = 1
while i <= group_summary:
    print('第{}组:'.format(i))
    for group in people_group.values():
        if group == i:
            print(list(people_group.keys())[list(
                people_group.values()).index(group)],
                  end=', ')
            people_group[list(people_group.keys())[list(
                people_group.values()).index(group)]] = 0
    print('\n')
    i += 1


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:
            return head
        n = 1
        even = None
        odd = None
        even_head = None
        odd_head = None
        while head:
            if n % 2 == 0:
                if not even:
                    even = head
                    even_head = even
                else:
                    even.next = head
                    even = head
            else:
                if not odd:
                    odd = head
                    odd_head = odd
                else:
                    odd.next = head
                    odd = head
            n+=1
            head = head.next
        if even:
            even.next = None
        odd.next = even_head
        return odd_head

if __name__ == '__main__':

    s = Solution()

    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    head.next.next.next.next = ListNode(5)

    new_head = s.oddEvenList(head)

    print(new_head)


#! python
"""
The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the
first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of
the first one hundred natural numbers and the square of the sum.
"""
n = 100
x1 = 0
summa2 = 0
for x in range(1,n+1):
    x1 += x
    summa1 = x1 ** 2
    summa2 += x ** 2
print(summa1 - summa2)


from matplotlib import pyplot as plt
import numpy as np

def przepisFunkcji(x):
    return (1 / (1 + (25 * (x**2))))

def przepisWezlowInterpol(x):
    return (-1 + 2 *(x / n))

def obliczXi(wezlyX, wezlyY):
    # wektory do zapamietania macierzy
    u0 = []; u1 = []; l1 = []
    x = [] #prawa strona rownania
    h = wezlyX[1] - wezlyX[0]

    #dodaje pusty znak do kazdego wektora, bo chce miec indeksy od 1 (potrzebne dla xi[0])
    u0.append(''); u1.append(''); l1.append(''); x.append('')

    for i in range(1, n):
        x.append((6 / (h**2)) * (wezlyY[i-1] - 2*wezlyY[i] + wezlyY[i+1]))
        u0.append(4)
        if i < n-1:
            u1.append(1)
            l1.append(1)

    # inicjalizacja
    l1[1] = l1[1]/u0[1]

    #obliczam macierz LU
    for i in range(2, n):
        u0[i] = u0[i] - l1[i-1] * u1[i-1]
        if i < n-1:
            l1[i] = l1[i]/u0[i]

    #obliczam wektor 'z' z rownania Lz = x forwardsubstitution
    for i in range(2, n):
        x[i] = x[i] - (l1[i-1] * x[i-1])

    #obliczam wektor 'y' rownania Uy = z backsubstitution
    x[n-1] = x[n-1]/u0[n-1]
    x[n-2] = (x[n-2] - (u1[n-2] * x[n-1])) / u0[n-2]
    for i in reversed(range(1, n-2)):
        x[i] = (x[i] - (u1[i] * x[i+1])) / u0[i]

    #wynik xi jest w wektorze 'x' + warunek naturalnego splajnu kubicznego
    x[0] = 0
    x.append(0)
    return x

def splajnyKubiczne(wezlyX, wezlyY, xi, wezelKoncaPrzedzialu):
    def S(x):
        A = (wezlyX[wezelKoncaPrzedzialu] - x) / (wezlyX[wezelKoncaPrzedzialu] - wezlyX[wezelKoncaPrzedzialu-1])
        B = (x - wezlyX[wezelKoncaPrzedzialu-1]) / (wezlyX[wezelKoncaPrzedzialu] - wezlyX[wezelKoncaPrzedzialu-1])
        C = 1/6 * ((A**3) - A) * ((wezlyX[wezelKoncaPrzedzialu] - wezlyX[wezelKoncaPrzedzialu-1])**2)
        D = 1/6 * ((B**3) - B) * ((wezlyX[wezelKoncaPrzedzialu] - wezlyX[wezelKoncaPrzedzialu-1])**2)
        wzorWielomianu = (A * wezlyY[wezelKoncaPrzedzialu-1]) + (B * wezlyY[wezelKoncaPrzedzialu]) + (C * xi[wezelKoncaPrzedzialu-1]) + (D * xi[wezelKoncaPrzedzialu])
        return wzorWielomianu
    return S

def plot(wezlyX, wezlyY, xi):
    x1 = np.arange(-1, 1, 0.01)
    funkcjaY = []
    for i in x1:
        funkcjaY.append(przepisFunkcji(i))

    plt.axis([-1.1,1.1,-0.5,1.1])
    plt.plot(x1, funkcjaY, color="orangered", linewidth=0.5)

    for i in range(1, n+1):
        S = splajnyKubiczne(wezlyX, wezlyY, xi, i)
        x = np.arange(wezlyX[i-1], wezlyX[i], 0.01)
        y = list(map(S, x))
        plt.plot(x, y, linewidth=2, color="tab:blue")

    plt.plot(wezlyX, wezlyY, 'ro')

def main():
    wezlyX = []
    wezlyY = []
    xi = []
    global n, h

    n = int(input("Podaj n = "))

    for i in range(n+1):
        wezlyX.append(przepisWezlowInterpol(i))
        wezlyY.append(przepisFunkcji(wezlyX[i]))

    xi = obliczXi(wezlyX, wezlyY)
    print(xi)
    plot(wezlyX, wezlyY, xi)

    plt.show()

if __name__ == "__main__":
    main()


#!/usr/bin/python
"""
Unique Paths II
	Follow up for "Unique Paths":

	Now consider if some obstacles are added to the grids. How many unique paths would there be?

	An obstacle and empty space is marked as 1 and 0 respectively in the grid.

	For example,
	There is one obstacle in the middle of a 3x3 grid as illustrated below.

	[
		[0,0,0],
		[0,1,0],
		[0,0,0]
	]
	The total number of unique paths is 2.

	Note: m and n will be at most 100.
"""
class Solution:
	# @param {integer[][]} obstacleGrid
	# @return {integer}
	def uniquePathsWithObstacles(self, obstacleGrid):
		if not obstacleGrid : return 0
		d=[]
		x = len(obstacleGrid[0])
		y = len(obstacleGrid)
		for idx in xrange(y):
			tmp=[0 for j in xrange(x)]
			d.append(tmp)
		if obstacleGrid[0][0] == 1  : return 0
		for idx in xrange(y):
			for jdx in xrange(x):
				if obstacleGrid[idx][jdx] == 0 :
					if idx -1  < 0 and jdx -1 < 0 :
						d[idx][jdx] = 1
					else:
						if idx == 0 and jdx > 0  :
							d[idx][jdx] = d[idx][jdx-1]
						elif idx> 0 and jdx == 0 :
							d[idx][jdx] = d[idx-1][jdx]
						else :
							d[idx][jdx] = d[idx-1][jdx] + d[idx][jdx-1]
				else :
					d[idx][jdx] = 0
		return d[-1][-1]


# -*- encoding: utf-8 -*-
'''
generic data to splunk returner

Deliver generic HubbleStack event data into Splunk using the HTTP
event collector. Required config/pillar settings:

.. code-block:: yaml

    hubblestack:
      returner:
        generic:
          - token: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
            indexer: splunk-indexer.domain.tld
            index: hubble
            sourcetype_pulsar: generic
'''

import time
import hubblestack.utils.stdrec as stdrec
from hubblestack.hec import http_event_collector, get_splunk_options

def _get_key(dat, k, d=None):
    if isinstance(dat, dict):
        return dat.pop(k, d)
    return d

def returner(retdata):
    try:
        retdata = retdata['return']
    except KeyError:
        return

    opts_list = get_splunk_options()
    for opts in opts_list:
        http_event_collector_key = opts['token']
        http_event_collector_host = opts['indexer']
        http_event_collector_port = opts['port']
        hec_ssl = opts['http_event_server_ssl']
        proxy = opts['proxy']
        timeout = opts['timeout']
        http_event_collector_ssl_verify = opts['http_event_collector_ssl_verify']

        hec = http_event_collector(http_event_collector_key, http_event_collector_host,
                                   http_event_port=http_event_collector_port, http_event_server_ssl=hec_ssl,
                                   http_event_collector_ssl_verify=http_event_collector_ssl_verify,
                                   proxy=proxy, timeout=timeout)

        t_sourcetype = _get_key(retdata, 'sourcetype', 'hubble_generic')
        t_time       = _get_key(retdata, 'time', time.time())
        events       = _get_key(retdata, 'event', _get_key(retdata, 'events'))

        if events is None:
            return

        if not isinstance(events, (list,tuple)):
            events = [events]

        if len(events) < 1 or (len(events) == 1 and events[0] is None):
            return

        idx = opts.get('index')

        for event in events:
            payload = {
                'host': __grains__.get('fqdn', __grains__.get('id')),
                'event': event,
                'sourcetype': _get_key(event, 'sourcetype', t_sourcetype),
                'time': str(int(_get_key(event, 'time', t_time)))}
            if idx:
                payload['index'] = idx
            # add various std host info data and index extracted fields
            stdrec.update_payload(payload)
            hec.batchEvent(payload)
        hec.flushBatch()


import scipy.io.wavfile as wav
from matplotlib import pyplot as plt
import numpy as np
fs,data=wav.read('nanvoice.wav')
fs1,data1=wav.read('nanvoice.wav')
fs11,data11=wav.read('nanvoice.wav')
print(fs,fs1)
t=np.arange(0,len(data)/fs,1.0/fs)
t1=np.arange(0,len(data1)/fs1,1.0/fs1)
t11=np.arange(0,len(data11)/fs11,1.0/fs11)
print (t11,len(t11),len(data11))
plt.subplot(311);plt.plot(t1,data1);
plt.subplot(312);plt.plot(t11,data11);
plt.subplot(313);plt.plot(t,data);
plt.show()