# documentation https://radimrehurek.com/gensim/auto_examples/core/run_corpora_and_vector_spaces.html

import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

# create a small corpus
documents = [
    "Human machine interface for lab abc computer applications",
    "A survey of user opinion of computer system response time",
    "The EPS user interface management system",
    "System and human system engineering testing of EPS",
    "Relation of user perceived response time to error measurement",
    "The generation of random binary unordered trees",
    "The intersection graph of paths in trees",
    "Graph minors IV Widths of trees and well quasi ordering",
    "Graph minors A survey",
]

# next we will tokenize the text, remove stopwords, and words that appear once

from pprint import pprint  # pretty-printer
from collections import defaultdict

# remove common words and tokenize
stoplist = set('for a of the and to in'.split())
texts = [
    [word for word in document.lower().split() if word not in stoplist]
    for document in documents
]

# remove words that appear only once
frequency = defaultdict(int)
for text in texts:
    for token in text:
        frequency[token] += 1

texts = [
    [token for token in text if frequency[token] > 1]
    for text in texts
]

pprint(texts)

# next we will convert words to vectors using document representation called "bag-of-words"
# each document is represented by one vector
# each vector represents question-answer pair

from gensim import corpora
dictionary = corpora.Dictionary(texts)
dictionary.save('/tmp/deerwester.dict')  # store the dictionary, for future reference
print(dictionary)

# mapping between questions and IDs is called dictionary
print(dictionary.token2id)

# we can also convert tokenized documents to vectors
new_doc = "Human computer interaction"
new_vec = dictionary.doc2bow(new_doc.lower().split())
print(new_vec)  # the word "interaction" does not appear in the dictionary and is ignored

# result of the code above is [(0, 1), (1, 1)]
# where (0,1) is for human, and (1,1) is for computer (both words appear in the dictionary)
# interaction didn't appear in the text, thus not part of the dictionary

corpus = [dictionary.doc2bow(text) for text in texts]
corpora.MmCorpus.serialize('/tmp/deerwester.mm', corpus)  # store to disk, for later use
print(corpus)

from smart_open import open  # for transparently opening remote files

# we are going to open document that is stored online

class MyCorpus:
    def __iter__(self):
        for line in open('https://radimrehurek.com/mycorpus.txt'):
            # assume there's one document per line, tokens separated by whitespace
            yield dictionary.doc2bow(line.lower().split())

# Gensim accepts any corpus (no need for it to be pandas, list or array)
# the document can we created on the fly
 # doesn't load the corpus into memory!
corpus_memory_friendly = MyCorpus()
print(corpus_memory_friendly)

# load one vector into memory at a time
for vector in corpus_memory_friendly:
    print(vector)

# construct dictionary without loading text into memory
# collect statistics about all tokens
dictionary = corpora.Dictionary(line.lower().split() for line in open('https://radimrehurek.com/mycorpus.txt'))
# remove stop words and words that appear only once
stop_ids = [
    dictionary.token2id[stopword]
    for stopword in stoplist
    if stopword in dictionary.token2id
]
once_ids = [tokenid for tokenid, docfreq in dictionary.dfs.items() if docfreq == 1]
dictionary.filter_tokens(stop_ids + once_ids)  # remove stop words and words that appear only once
dictionary.compactify()  # remove gaps in id sequence after words that were removed
print(dictionary)


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri May 25 02:16:27 2018

@author: arjumand

"""
#Adapted from https://gist.github.com/nailo2c/09c3fd3a92fe212dea8f97ac5c7a1043

import argparse
import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical

parser = argparse.ArgumentParser(description='PyTorch REINFORCE example')
parser.add_argument('--gamma', type=float, default=0.99, metavar='G',
                    help='discount factor (default: 0.99)')
parser.add_argument('--seed', type=int, default=543, metavar='N',
                    help='random seed (default: 543)')
parser.add_argument('--render', action='store_true',
                    help='render the environment')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                    help='interval between training status logs (default: 10)')
args = parser.parse_args()

state_dim = 2
action_dim = 4

class Policy(nn.Module):
    def __init__(self):
        super(Policy, self).__init__()
        self.affine1 = nn.Linear(state_dim, 128)
        self.affine2 = nn.Linear(128, action_dim)

        self.saved_log_probs = []
        self.rewards = []
        self.states = []

    def forward(self, x):
        x = F.relu(self.affine1(x))
        action_scores = self.affine2(x)
        return F.softmax(action_scores, dim=1)

def exp_kernel(x,y,h = 1.0, vec = True):
    if x.ndim == 1:
        x = x.reshape((1,-1))
    if y.ndim == 1:
        y = y.reshape((1,-1))
    return np.exp(-1.0*np.sum((x - y)**2,1)/h**2/2.0)

def mmd_multi_policy(env, n_policies = 4, mmd_alpha = 0.2, max_episodes = 100, max_episode_length = 20, test_episodes = 5):

    terminal_states = []
    known_trajectories_x1 = []
    known_trajectories_x2 = []

    policies = []

    def states_kernel():
        ic = len(policy.states)

        kernel_vals = []
        p1s = []
        p2s = []
        for policy_x1, policy_x2 in zip(known_trajectories_x1, known_trajectories_x2):
            ip = len(policy_x1)

            p1 = np.zeros((np.max([ip,ic]),2))
            p2 = np.zeros((np.max([ip,ic]),2))

            p1[:,0] = policy_x1[-1]
            p1[:,1] = policy_x2[-1]

            p2[:,0] = policy.states[-1][0]
            p2[:,1] = policy.states[-1][1]

            p1[0:len(policy_x1),0] = np.array(policy_x1)
            p1[0:len(policy_x2),1] = np.array(policy_x2)

            p2[0:len(policy.states),:] = np.array(policy.states)
            kernel_vals.append(exp_kernel(p1.flatten(), p2.flatten())/len(known_trajectories_x1))

            p1s.append(p1)
            p2s.append(p2)

        return p1s,p2s, kernel_vals

    def select_action(state, epsilon = 0):
        state = torch.from_numpy(np.array(state)).float().unsqueeze(0)
        probs = policy(state)
        m = Categorical(probs)
        action = m.sample()
        policy.saved_log_probs.append(m.log_prob(action))
        return action.item()

    def finish_episode():
        R = 0
        policy_loss = []
        rewards = []
        for r in policy.rewards[::-1]:
            R = r + args.gamma * R
            rewards.insert(0, R)
        rewards = torch.tensor(rewards)
        rewards = (rewards - rewards.mean()) / (rewards.std() + eps)

        for log_prob, reward in zip(policy.saved_log_probs, rewards):
            policy_loss.append(-log_prob*reward)
        if len(known_trajectories_x1) > 0:
            p1s, p2s, kernels = states_kernel()
            idx = np.argmax(kernels)
            s1 = p1s[idx]
            s2 = p2s[idx]
            kss = kernels[idx]

            kernel_terms = np.exp(-1*np.sum((s1 - s2)**2,1))
            kernel_list = []
            for kt in kernel_terms:
                kernel_list.append(torch.tensor(kt))

            kernel_list = torch.tensor(kernel_list)
            kernel_list = (kernel_list - kernel_list.mean())/(kernel_list.std() + eps)
            for log_prob, k_term in zip(policy.saved_log_probs, kernel_list):
                policy_loss.append(log_prob*(2-2*k_term)*mmd_alpha)

        optimizer.zero_grad()
        policy_loss = torch.cat(policy_loss).sum()
        policy_loss.backward()
        optimizer.step()

        del policy.rewards[:]
        del policy.saved_log_probs[:]

    states_policies = []
    rewards_policies = []

    N_REWARD_LIST = []
    for i in range(n_policies):

        policy = Policy()
        optimizer = optim.Adam(policy.parameters(), lr=1e-2)
        eps = np.finfo(np.float32).eps.item()

        running_reward = 0
        KD_running = 0

        REWARD_LIST = []
        for i_episode in range(max_episodes):
            env.reset()
            state = env.observe()
            policy.states.append(state)
            reward_list = []
            for t in range(max_episode_length):
                action = select_action(state)
                state, reward, done = env.perform_action(action)
                policy.states.append(state)
                policy.rewards.append(reward)
                reward_list.append(reward)
                if done:
                    break
            running_reward = (i_episode*running_reward  + reward)/(i_episode + 1)
            finish_episode()
            REWARD_LIST.append(reward_list)
            if i_episode % args.log_interval == 0:
                if len(known_trajectories_x1) > 0:
                    p1s, p2s, kernels = states_kernel()
                    idx = np.argmax(kernels)
                    s1 = p1s[idx]
                    s2 = p2s[idx]
                    kss = kernels[idx]
                    kernel_mean = np.mean( np.exp(-1*np.sum((s1 - s2)**2,1)))
                    KD_running = 0.95*KD_running + 0.05*kernel_mean
                    print('Episode {}\tLast length: {:5d}\tAverage award: {:.2f}\tAverage Kernel: {:.2f}'.format(
                        i_episode, t, running_reward, kernel_mean))
                else:
                    print('Episode {}\tLast length: {:5d}\tAverage award: {:.2f}\tNo Kernel'.format(
                        i_episode, t, running_reward ))

            if i_episode > max_episodes:
                break
            del policy.states[:]

        N_REWARD_LIST.append(REWARD_LIST)

        states_list_ep = []
        rewards_list_ep = []

        for i in range(test_episodes):
            x1 = []
            x2 = []
            env.reset()
            state = env.observe()
            x1.append(state[0])
            x2.append(state[1])

            states_i = []
            rewards_i = []
            for t in range(max_episode_length):  # Don't infinite loop while learning
                action = select_action(state)
                states_i.append(state)
                state, reward, done = env.perform_action(action)
                rewards_i.append(reward)
                x1.append(state[0])
                x2.append(state[1])
                policy.rewards.append(reward)
                if done:
                    terminal_states.append(state)
                    break

        states_list_ep.append(states_i)
        rewards_list_ep.append(rewards_i)

        policies.append(policy)
        known_trajectories_x1.append(np.array(x1))
        known_trajectories_x2.append(np.array(x2))

        states_policies.append(states_list_ep)
        rewards_policies.append(rewards_list_ep)

    return policies, states_policies, rewards_policies, terminal_states


from flask import *
from config import Config
import work
import forms
import os

app = Flask(__name__)
app.config.from_object(Config)

@app.route('/', methods=['GET', 'POST'])
def index():
    form = forms.MerchantForm()

    if form.validate_on_submit():
        if any([form.pig_bool.data, form.chicken_bool.data, form.snake_bool.data, form.gunpowder_bool.data]):
            flash("We're going on an adventure from {} to {}!".format(form.starting_position.data,
                                                                      form.destination.data))
            return render_template('main.html', form=form, nav_messages=work.navigate(form.starting_position.data, form.destination.data, work.determineResources(form.pig_bool.data, form.chicken_bool.data, form.snake_bool.data, form.gunpowder_bool.data)))

        elif form.starting_position.data != form.destination.data:
            return render_template('main.html', form=form, nav_messages=work.navigateNoResources(form.starting_position.data, form.destination.data))

    print("nothing selected")
    return render_template('main.html', form=form)

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static', 'styles'),
                               'favicon.ico', mimetype='image/vnd.microsoft.icon')

# if __name__ == "__main__":
    # app.run(host="0.0.0.0", threaded=True)


from flask import Flask, jsonify, request, render_template
import numpy as np
import pandas as pd
from sklearn import linear_model
import joblib

import re
from sklearn.feature_extraction.text import TfidfVectorizer

import flask
app = Flask(__name__)
######################################
#load model

clf = joblib.load('RF.pkl')
count_vect = joblib.load('tfidf.pkl')

######################
#load Arabic stop words

stops = open('stopwords2.txt',encoding = 'utf-8')
stop_words = stops.read().split('\n')

###################################################
#Clean Text
def clean_text(text, stop_words):

    text = re.sub(r'https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
    text = re.sub(r'\<a href', ' ', text)
    text = re.sub(r'\...', ' ', text)
    text = re.sub(r'&', '', text)
    text = re.sub(r'[_"\-;%()|+&=*%.,!?«»:#$@\[\]/]', ' ', text)
    text = re.sub(r'<br />', ' ', text)
    text = re.sub(r'\'', ' ', text)
    text = re.sub(r'[0-9]+', ' ', text)
    text = re.sub(r'[a-z]+', ' ', text)
    text = re.sub(r'[A-Z]+', ' ', text)
    text = text.split()
    text = [w for w in text if not w in stop_words]
    text = " ".join(text)

    return text

###################################################
#Clean emoji
def remove_emoji(text):
    emoji_pattern = re.compile("["
                                   u"\U0001F600-\U0001F64F"  # emoticons
                                   u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                                   u"\U0001F680-\U0001F6FF"  # transport & map symbols
                                   u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                                   u"\U00002702-\U000027B0"
                                   u"\U000024C2-\U0001F251"
                                   "]+", flags=re.UNICODE)
    text = emoji_pattern.sub(r'', text)
    return text

@app.route('/')
def index():
    return flask.render_template('index.html')

#Retrieve text and Prediction Country
@app.route('/predict', methods=['POST'])
def predict():
    to_predict_list = request.form.to_dict()
    text = to_predict_list['text']

    text_clean1 = clean_text(text,stop_words)

    text_clean2=remove_emoji(text_clean1)
    pred = clf.predict(count_vect.transform([text_clean2]))

    if pred==0:
        prediction = "EG"
    elif pred ==1:
        prediction = "PL"
    elif pred ==2:
        prediction = "KW"
    elif pred ==3:
        prediction = "LY"
    elif pred ==4:
        prediction = "QA"
    elif pred ==5:
        prediction = "JO"
    elif pred ==6:
        prediction = "LB"
    elif pred==7:
        prediction = "SA"
    elif pred==8:
        prediction = "AE"
    elif pred==9:
        prediction = "SA"
    elif pred==10:
        prediction = "BH"
    elif pred==11:
        prediction = "OM"
    elif pred==12:
        prediction = "SY"
    elif pred==13:
        prediction = "DZ"
    elif pred==14:
        prediction = "IQ"
    elif pred==15:
        prediction = "SD"
    elif pred==16:
        prediction = "MA"
    elif pred==17:
        prediction = "YE"
    elif pred ==18:
        prediction = "TN"
    else:
        prediction = "Model can not prediction"

    return render_template('index.html', prediction='Country Prediction: {}'.format(prediction))

if __name__ == '__main__':

    app.run(debug=True)


'''
'''
import dash
import dash_bootstrap_components as dbc
import logging
from dash import html
from dash.dependencies import Input, Output, State

from lib.brstockmanager import BrStockManager
from app import app
from apps.investing_dash_components import style_center, style_white, get_operations_dbc

################ Logging information
logger = logging.getLogger(__name__)

################ APP LAYOUT
app_layout = html.Center(html.Div([
# INPUT DATA
    dbc.Card([
        dbc.CardHeader("ADD INFO", className="card-body"),
        dbc.CardBody([
            dbc.Row([
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Broker"),
                        dbc.Input(placeholder="(required)", id='broker', style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Data"),
                        dbc.Input(placeholder="YYYY-MM-DD", id='date', style=style_white),
                    ]),
                ),
            ]),
            dbc.Row([
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Ticker"),
                        dbc.Input(placeholder="(required)", id='ticker', style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Quantity"),
                        dbc.Input(placeholder="(negative for Selling)",
                                id='quantity',
                                style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Value"),
                        dbc.Input(placeholder="(float)", id='value', style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Taxes"),
                        dbc.Input(placeholder="(float)", id='taxes', style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("IRRF"),
                        dbc.Input(placeholder="(float)", id='irrf', style=style_white),
                    ]),
                ),
            ]),
            dbc.Row([
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Class"),
                        dbc.Input(placeholder="(optional)", id='class', style=style_white),
                    ]),
                ),
                dbc.Col(
                    dbc.InputGroup([
                        dbc.InputGroup("Comment"),
                        dbc.Input(placeholder="(optional)", id='comment', style=style_white),
                    ]),
                ),
            ]),
            html.Br(),
            dbc.Row(
                dbc.Button("REGISTER",
                            id="register",
                            color="primary",
                            className="mr-1"
                )
            )
        ]),
    ]),
    html.Br(),
# OPERATIONS TABLE
    dbc.Row(
        html.Div(
            get_operations_dbc(),
            id="operations-table",
            style={"font-size":"13px"}
        ),
    )
],style=style_center))

################ CALLBACK DEFINITION
@app.callback(
    [
        Output("operations-table", "children"),
    ],
    [
        Input('register', 'n_clicks'),
    ],
    [
        State('broker', 'value'),
        State('date', 'value'),
        State('ticker', 'value'),
        State('quantity', 'value'),
        State('value', 'value'),
        State('taxes', 'value'),
        State('irrf', 'value'),
        State('class', 'value'),
        State('comment', 'value'),
    ]
)
def register_info(register_n, broker, date, ticker, quantity, value, taxes, irrf,
        stock_class, comment):
    if register_n is not None:
        logger.info("Registering new info: broker %s, date %s, ticker %s, quantity %s, value %s",
            broker, date, ticker, quantity, value)
        logger.info("taxes %s, irrf %s, stock_class %s, comment %s",
            taxes, irrf, stock_class, comment)
        # Do nothing if any of the required values are not filled
        if [x for x in (broker, date, ticker, quantity, value) if x is None]:
            logger.info("Some of the required values are not filled! Returning...")
            return dash.no_update

        if not taxes: taxes = 0.0
        if not irrf: irrf = 0.0
        if not comment: comment = ""
        if not stock_class: stock_class = ""

        # Adding info
        br_stock_manager = BrStockManager()
        br_stock_manager.load_brstock_operations_pkl()
        br_stock_manager.add_info(broker=broker, date=date, ticker=ticker, value=float(value),
            quantity=int(quantity), taxes=float(taxes), irrf=float(irrf), comments=comment,
            stock_class=stock_class)

        br_stock_manager.save_brstock_operations_pkl()
        br_stock_manager.save_brstock_operations_csv()

        return (get_operations_dbc(),)

    return dash.no_update


import math
import numpy as np

from arch_parser.models.room import Room
from arch_parser.models.wall_room_assignment import WallRoomAssignment

def ray_test(mask, ray_origin: tuple, ray_direction: tuple, ray_length: float) -> tuple:
    """
    Perform a ray-hit-test on the top-down view of the house geometry.
    :param mask: Top down view mask of walls
    :param ray_origin: Ray start point
    :param ray_direction: Ray direction
    :param ray_length: Maximum length of ray
    :return: Tuple (min_distance, hit point)
    """
    assert len(ray_origin) == 2
    assert len(ray_direction) == 2
    from shapely.geometry import GeometryCollection, LineString
    ray = [ray_origin, (ray_origin[0] + ray_direction[0] * ray_length, ray_origin[1] + ray_direction[1] * ray_length)]

    ray_segment = LineString(ray)
    ray_intersection = ray_segment.intersection(mask)

    intersection_point = None
    min_distance = ray_length

    hit_points = []
    if isinstance(ray_intersection, GeometryCollection):
        if not ray_intersection.is_empty:
            for g in ray_intersection.geoms:
                hit_points.extend([(r.x, r.y) for r in g.boundary])
    else:
        if not ray_intersection.is_empty:
            hit_points = [(r.x, r.y) for r in ray_intersection.boundary]

    for hit_point in hit_points:
        d = math.sqrt((hit_point[0] - ray_origin[0]) ** 2 + (hit_point[1] - ray_origin[1]) ** 2)
        if min_distance is None or d < min_distance:
            min_distance = d
            intersection_point = hit_point

    return min_distance, intersection_point

def get_room_mask(room: Room):
    """
    Compute top-down view wall mask of a room.
    :param room: Room considered
    :return Top down view wall mask as a shapely geometry:
    """
    from shapely.geometry import LineString
    processed_walls = []
    all_lines = []

    for wall_assignment in room.walls:
        assert isinstance(wall_assignment, WallRoomAssignment)
        wall = wall_assignment.wall

        if wall in processed_walls:
            continue

        ls = LineString([(wall.p1[0], wall.p1[2]), (wall.p2[0], wall.p2[2])]).buffer(1)

        for hole in wall.holes:
            h_start_x = ((wall.p2[0] - wall.p1[0]) * hole.start) + wall.p1[0]
            h_end_x = ((wall.p2[0] - wall.p1[0]) * hole.end) + wall.p1[0]
            h_start_y = ((wall.p2[2] - wall.p1[2]) * hole.start) + wall.p1[2]
            h_end_y = ((wall.p2[2] - wall.p1[2]) * hole.end) + wall.p1[2]

            hs = LineString([(h_start_x, h_start_y), (h_end_x, h_end_y)]).buffer(1)
            ls = ls.difference(hs)

        all_lines.append(ls)

    from shapely.ops import cascaded_union
    u = cascaded_union(all_lines)
    return u

def get_transform(x: float, y: float, z: float, angle: float, scale_x: float = 1, scale_z: float = 1) -> list:
    """
    Compute transformation matrix
    :param x: X position of center
    :param y: Y position of center
    :param z: Z position of center
    :param angle: Rotation of object about Y axis, in degrees.
    :param scale_x: Scaling factor along X axis
    :param scale_z: Scaling factor along Z axis
    :return: Transformation matrix as a nested list
    """
    from scipy.spatial.transform import Rotation as R
    scale_mat = np.array([[scale_x, 0, 0, 0], [0, 1, 0, 0], [0, 0, scale_z, 0], [0, 0, 0, 1]], dtype=float)
    r = R.from_rotvec((0, angle / 180 * np.pi, 0))
    m = r.as_dcm()
    m = np.vstack([m, [x, y, z]])
    m = np.hstack([m, np.transpose(np.array([[0, 0, 0, 1]]))])
    m = np.matmul(scale_mat, m)
    return m.flatten().tolist()

def find_angle(p: tuple) -> float:
    """
    Computes angle of a 2D vector.
    Angles are measured with respect to positive x axis, on the counter clockwise direction. Negative y is considered up. Positive x is considered right.
    :param p: Vector considered in the form (x, y)
    :return: Angle in degrees.
    """
    assert len(p) == 2
    h = math.sqrt(p[0] ** 2 + p[1] ** 2)
    ang = math.acos(p[0] / h) * 180 / math.pi
    if p[1] > 0:
        ang = -ang
    return ang

def l2_dist_sq(p1: tuple, p2: tuple) -> float:
    """
    Compute squared L2 distance between p1 and p2
    :param p1: Point 1 in the form (x, y)
    :param p2: Point 2 in the form (x, y)
    :return: Squared L2 distance
    """
    assert len(p1) == 2
    assert len(p2) == 2
    dx = p1[0] - p2[0]
    dy = p1[1] - p2[1]
    return dx ** 2 + dy ** 2

def hole_to_line(wall_p1: tuple, wall_p2: tuple, hole_start: float, hole_end: float) -> tuple:
    """
    Converts a 2D hole definition to a line segment.
    :param wall_p1: Top-down x,y coordinate of wall start point
    :param wall_p2: Top-down x,y coordinate of wall end point
    :param hole_start: Distance from wall_p1 to start of hole
    :param hole_end: Distance from wall_p1 to end of hole
    :return: Line segment as tuple(tuple(x1,y1), tuple(x2,y2))
    """
    assert len(wall_p1) == 2
    assert len(wall_p2) == 2

    wall_length = math.sqrt(l2_dist_sq(wall_p1, wall_p2))
    hole_start = hole_start / wall_length
    hole_end = hole_end / wall_length
    start_x = (wall_p2[0] - wall_p1[0]) * hole_start + wall_p1[0]
    start_y = (wall_p2[1] - wall_p1[1]) * hole_start + wall_p1[1]
    end_x = (wall_p2[0] - wall_p1[0]) * hole_end + wall_p1[0]
    end_y = (wall_p2[1] - wall_p1[1]) * hole_end + wall_p1[1]
    return (start_x, start_y), (end_x, end_y)

def point_line_seg_distance(p1: tuple, p2: tuple, p: tuple, extend_line: bool = False) -> tuple:
    """
    Compute distance from 2D point p to line segment (p1, p2).
    :param p1: Start point of line in the form (x, y)
    :param p2: End point of line in the form (x, y)
    :param p: Point to compute distance to, in the form (x, y)
    :param extend_line: Should the line be extended as necessary to reduce the distance.
    :return: Tuple(Distance, (x,y) of closest point)
    """
    # Code adapted from https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
    assert len(p1) == 2
    assert len(p2) == 2
    assert len(p) == 2

    x1, y1 = p1
    x2, y2 = p2
    x3, y3 = p
    x1, x2, x3, y1, y2, y3 = float(x1), float(x2), float(x3), float(y1), float(y2), float(y3)
    px = x2 - x1
    py = y2 - y1

    norm = px * px + py * py
    if norm == 0:
        dx = x1 - x3
        dy = y1 - y3

        dist = (dx * dx + dy * dy) ** .5
        return dist, (x1, y1)

    u = ((x3 - x1) * px + (y3 - y1) * py) / float(norm)

    if not extend_line:
        if u > 1:
            u = 1
        elif u < 0:
            u = 0

    x = x1 + u * px
    y = y1 + u * py

    dx = x - x3
    dy = y - y3

    dist = (dx * dx + dy * dy) ** .5

    return dist, (x, y)


from flask import flash, Flask, render_template, request, redirect
from forms import QueryForm
from flask_wtf.csrf import CSRFProtect
import os, sys, houndify
import speech_recognition as sr
from phrase_occurrences import find_occurrences

app = Flask(__name__)
app.config.from_pyfile('config.py', silent=True)
csrf = CSRFProtect(app)

search_string = ''
RESULT_LIMIT = 100
SECRET_KEY = 'go bears'
app.config['SECRET_KEY'] = SECRET_KEY
client_id = app.config['HOUNDIFY_ID']
client_key = app.config['HOUNDIFY_KEY']
user_id = app.config['HOUNDIFY_USR']
client = houndify.StreamingHoundClient(client_id, client_key, user_id, sampleRate=8000)

@app.route('/', methods=('GET', 'POST'))
def home():
    select = QueryForm(request.form)
    if request.method == 'POST':
        prof_name = request.form['profs']
        return redirect('/'+prof_name)
    opt_list = [x[0] for x in os.walk('examples')]
    opt_list = [i.replace('examples\\', '') for i in opt_list if 'raw_subtitles' not in i]
    opt_list = [i.replace('examples/', '') for i in opt_list if 'raw_subtitles' not in i]
    opt_list = [(i.replace('_', ' ').title(), i) for i in opt_list][1:]
    opt_list = [(open("examples/"+i[1]+"/"+i[1]+".txt", "r").read()+" - "+i[0], i[1]) for i in opt_list]
    return render_template('index.html', form=select, option_list=opt_list, dropdown = True, search_menu = False)

@app.route('/<prof_name>', methods=('GET', 'POST'))
def pick_prof(prof_name):
    search = QueryForm(request.form)
    if request.method == 'POST':
        if request.form['search'] == "voice":
            search_string = listen()
        else:
            search_string = request.form['query']
        if search_string != "":
            search_string = '/' + search_string
        return redirect('/'+prof_name+search_string)
    f = open("examples/"+prof_name+"/"+prof_name+".txt", "r")
    prof_name = prof_name.replace("_", " ").title()
    course_name = f.read()
    return render_template('index.html', form=search, dropdown = False, search_menu = True, course_name=course_name, prof_name=prof_name)

@app.route('/<prof_name>/<search_term>', methods=('GET', 'POST'))
def results(prof_name, search_term):
    titles, urls, raw_phrases, term_idxes = find_occurrences(search_term, "examples/"+prof_name+"/"+prof_name+".json")
    leftContexts = []
    rightContexts = []
    titleContexts = []
    for idx, phrase in enumerate(raw_phrases):
        leftContexts.append(phrase[:term_idxes[idx]])
        rightContexts.append(phrase[term_idxes[idx]+len(search_term):])
        titleContexts.append(titles[idx])
    search = QueryForm(request.form)
    if request.method == 'POST':
        if request.form['search'] == "voice":
            search_string = listen()
        else:
            search_string = request.form['query']
        if search_string != "":
            search_string = '/' + search_string
        return redirect('/'+prof_name+search_string)
    f = open("examples/"+prof_name+"/"+prof_name+".txt", "r")
    prof_name = prof_name.replace("_", " ").title()
    course_name = f.read()
    return render_template('index.html', form=search,
        name=search_term, urls=urls[:RESULT_LIMIT], leftContexts=leftContexts,
        rightContexts=rightContexts, titleContexts=titleContexts, showViewer=len(urls)>0, dropdown = False, search_menu = True, course_name=course_name, prof_name=prof_name)

def listen():
    search = QueryForm(request.form)
    rec = sr.Recognizer()
    with sr.Microphone() as source:
        rec.adjust_for_ambient_noise(source)
        app.logger.info("I'm listening...")
        audio = rec.listen(source, phrase_time_limit=3)
    search_string = rec.recognize_houndify(audio, client_id, client_key)
    return search_string

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


import numpy as  np

n = 10

r = [1]*n
phi = np.linspace(0, 2*np.pi, n, endpoint=False )

def pol2cart(r, phi):
    x = r*np.cos(phi)
    y = r*np.sin(phi)
    return x, y

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

f = [0.1, 0]

x = []
y = []
for a, b in zip(r,phi):
    xx, yy = pol2cart(a,b)
    x.append(xx)
    y.append(yy)

def convert(v, p):
    phi = np.arctan2(p[1], p[0])
    a = v[0]*np.sin(phi+np.pi/2) - v[1]*np.cos(phi+np.pi/2)
    b = v[0]*np.cos(phi+np.pi/2) + v[1]*np.sin(phi+np.pi/2)
    return a, b

def convert2(v, p):
    phi = np.arctan2(p[1], p[0])
    a = v[0]*np.cos(phi) + v[1]*np.sin(phi)
    b = -v[0]*np.sin(phi) + v[1]*np.cos(phi)
    return a, b

for a, b in zip(x,y):
    ft, fr = convert2(f, [a,b])
    print(ft, fr)
    ax.text(a, b+0.05, f'({round(ft, 3)}, {round(fr, 3)})')
    ax.plot([a, a+f[0]],[b, b+f[1]], 'b->')
ax.plot(x, y, 'ko')

plt.show()


from conans import ConanFile, CMake, tools
import shutil
import os

CMakeOptions = [("NCNN_OPENMP", True), ("NCNN_STDIO", True), ("NCNN_STRING", True), ("NCNN_INSTALL_SDK", True),
                ("NCNN_OPENCV", False), ("NCNN_BENCHMARK",
                                         False), ("NCNN_PIXEL", True), ("NCNN_PIXEL_ROTATE", False),
                ("NCNN_CMAKE_VERBOSE", False), ("NCNN_VULKAN", False), ("NCNN_REQUANT", False), ("NCNN_AVX2", False),
                ("NCNN_BUILD_TOOLS", False)]

class NcnnConan(ConanFile):
    name = "ncnn"
    version = "20190908"
    license = "BSD-3-Clause"
    author = "Tencent"
    url = "https://github.com/Tencent/ncnn"
    description = "ncnn is a high-performance neural network inference framework optimized for the mobile platform"
    topics = ("C++", "machine learning")
    settings = "os", "compiler", "build_type", "arch"

    options = {"shared": [True, False], "NCNN_DISABLE_RTTI": [True, False, "Default"],
               "NCNN_DISABLE_EXCEPTION": [True, False, "Default"]}
    options.update({CMakeOption[0]: [True, False]
                    for CMakeOption in CMakeOptions})

    default_options = [
        "shared=False", "NCNN_DISABLE_RTTI=Default", "NCNN_DISABLE_EXCEPTION=Default"]
    default_options.extend(
        ["{}={}".format(CMakeOption[0], CMakeOption[1]) for CMakeOption in CMakeOptions])
    default_options = tuple(default_options)

    generators = "cmake"

    exports_sources = "CMakeLists.txt"

    _source_dir = "ncnn"

    def source(self):
        git = tools.Git(folder=self._source_dir)
        git.clone("https://github.com/Tencent/ncnn.git", self.version)

    def configure_cmake(self):
        cmake = CMake(self)
        cmake.definitions["BUILD_SHARED_LIBS"] = "ON" if self.options.shared else "OFF"

        if self.options.NCNN_VULKAN:
            raise RuntimeError("Sorry, vulkan not supported for now.")

        disableRtti = self.options.NCNN_DISABLE_RTTI
        if disableRtti != "Default":
            cmake.definitions["NCNN_DISABLE_RTTI"] = disableRtti

        disableException = self.options.NCNN_DISABLE_EXCEPTION
        if disableException != "Default":
            cmake.definitions["NCNN_DISABLE_EXCEPTION"] = disableException

        for CMakeOption in CMakeOptions:
            cmake.definitions[CMakeOption[0]] = getattr(
                self.options, CMakeOption[0])

        cmake.configure()
        return cmake

    def build(self):
        cmake = self.configure_cmake()
        cmake.build()

    def package(self):
        cmake = self.configure_cmake()
        cmake.install()

        # These steps are needed because headers of ncnn is installed in child folder...
        shutil.move(self.package_folder + "/include/ncnn",
                    self.package_folder + "/include_tmp")
        os.rmdir(self.package_folder + "/include")
        shutil.move(self.package_folder + "/include_tmp",
                    self.package_folder + "/include")

    def package_info(self):
        self.cpp_info.libs = tools.collect_libs(self)

    def package_id(self):
        del self.info.options.NCNN_CMAKE_VERBOSE


from aiohttp import ClientSession

class HttpRequest:
    def __init__(self, bot):
        self.bot = bot
        self.session = ClientSession(loop = bot.loop)

    async def request(method, url, **kwargs):
        headers = {
            "Authorization": f"Bot {self.bot.token}",
            "User-Agent": "DiscordBot (https://github.com/tuna2134/discord-interaction.py, 0.0.1)"
        }
        kwargs["headers"] = headers
        async with session.request(method, "https://discord.com/api/v8" + url, **kwargs) as r:
            return await r.json()


import sqlite3
import pandas

# establish connection to database
con = sqlite3.connect('database.db')

# create cursor object that looks at the rows and gives you data
cur = con.cursor()

# run SQL query & print the results
# this one is to get all rows and all columns by order
df = pandas.read_sql_query("SELECT * FROM 'ips' ORDER BY asn", con)
print(df)

# df.to_csv('database.csv', index=None)
df.to_excel('database.xlsx', index=None)


import customtkinter
import tkinter

customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")

app = customtkinter.CTk()
app.geometry("400x240")

textbox = customtkinter.CTkTextbox(app)
textbox.grid(row= 0 , column = 0)
text = """
        welcome to my github
"""
textbox.insert("0.0",text)
textbox.configure(state="normal")

button = customtkinter.CTkButton(master=app , text="CTkButton")
button.place(relx=0.7, rely=0.3 , anchor = tkinter.CENTER)

app.mainloop()


"""
 * @author [Tenzing Dolmans]
 * @email [t.c.dolmans@gmail.com]
 * @create date 2023-05-11 12:06:43
 * @modify date 2023-05-11 12:06:43
 * @desc [description]
"""
import os
import sys
import time
import torch
import numpy as np
from PIL import Image
from scipy import stats
from torch.utils.data import DataLoader
sys.path.insert(1, os.path.join(sys.path[0], '../'))
from pipeline.utils import list_files # noqa

def time_it(func):
    """"Wrapper for timing of functions."""
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} took {(end - start):.6f} seconds")
        return result
    return wrapper

def alert_deprecation(func):
    """"Wrapper that warns for deprecation of functions, prints time taken."""
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} deprecated and took {(end - start):.6f} seconds")
        return result
    return wrapper

# @time_it
def replace_nans(input_data, need_sanity_check=False):
    """
    Takes some input tensor suitable for PyTorch, then linearly interpolates the Nans away.
    This works decently well, but can be improved by being tested for speed and finding different
    interpolation methods.
    Assumes that the input dimension you care to evaluate by is returned by simply calling len().
    TODO: Generalise to differently ranked inputs.
    """
    def pad_with_interp(data):
        bad_indexes = np.isnan(data)
        good_indexes = np.logical_not(bad_indexes)
        good_data = data[good_indexes]
        if len(good_data) == 0:
            return False
        elif len(good_data) / len(data) < 0.85:
            #  The sample is rejected because it did not met the threshold of .85.
            return False
        # Craft linearly interpolated data over the selected data.
        interpolated = np.interp(bad_indexes.nonzero()[0], good_indexes.nonzero()[0], good_data)
        data[bad_indexes] = interpolated
        return data

    output = torch.tensor(np.apply_along_axis(pad_with_interp, 0, input_data))
    if need_sanity_check:
        print(output.shape)
    return output

@time_it
def downsample(input_tensor, dsf=10):
    """
    Takes input_tensor and donsamples the data by down_sampling_frequency(dsf.
    Returns a shortened array which contains the mode for each of the sample windows.
    Can be altered to return !mode, e.g.,mean, distribution, etc.
    """
    ds = []
    for i in range(0, len(input_tensor), dsf):
        selection = stats.mode(input_tensor[i: i+dsf], axis=0, keepdims=True)[0][0]
        ds.append(torch.tensor(np.array(selection)).unsqueeze(0))
    return torch.cat(ds)

@time_it
def downsample_remove_nans(file, dsf=10):
    """
    Takes a file and down samples it by dsf, then removes nans.
    Little bit of unpacking and looping, hence the decorator;
    please remember to switch it off when you are not testing.
    """
    tensor = torch.load(file)
    for i, t in enumerate(tensor):
        labels = t[0].unsqueeze(0)
        _data = t[1:]
        _data = replace_nans(downsample(_data))
        tensor[i] = torch.cat((labels, _data))
    return tensor

def split_tensor(tensor, sampling_rate=100, selection_length=3):
    """
    Splits every input tensor into multiple usable sections.
    Outputs a composite tensor that contains trainable samples.
    """
    selection_samples = int(sampling_rate * selection_length)
    labels = [data[0] for data in tensor]
    selections = [data[1:, :] for data in tensor]
    data_tensors = []
    for j, selection in enumerate(selections):
        for i in range(0, len(selection) - selection_samples, selection_samples):
            end = i + selection_samples
            _slice = selection[i:end]
            if _slice is not False:
                _labels = labels[j].unsqueeze(0)
                data_tensors.append(torch.cat((_labels, _slice)).unsqueeze(0))
            else:
                print("Selection {} rejected".format(j))
    return torch.cat(data_tensors)

@alert_deprecation
def split_ds_tensor(tensor, sampling_rate=100, selection_length=3):
    selection_samples = int(sampling_rate * selection_length)
    label = tensor[0].unsqueeze(0)
    data = tensor[1:]
    print(label.shape, data.shape)
    data_t = []
    for i in range(0, len(data) - selection_samples, selection_samples):
        end = i + selection_samples
        _slice = data[i:end]
        if _slice is not False:
            data_t.append(torch.cat((label, _slice)).unsqueeze(0))
        else:
            print("Selection rejected")
    return data_t

@time_it
def return_data_loaders(files, train_idxs, test_idxs, batch_size=64):
    """
    Takes a list of files, and returns a train and test dataloader.
    """
    train = files[:train_idxs]  # NOTE: usually is :train_idxs, but this is for testing
    test = files[-test_idxs:]  # NOTE: usually is -test_idxs:, but this is for testing
    train_data_tensors = []
    test_data_tensors = []
    print("Loading training data")
    for file in train:
        split_tensors = split_tensor(torch.load(file))
        train_data_tensors.append(split_tensors)
    print("Loading testing data")
    for file in test:
        split_tensors = split_tensor(torch.load(file))
        test_data_tensors.append(split_tensors)
    train_dataloader = DataLoader(torch.cat(train_data_tensors),
                                  batch_size=batch_size, shuffle=True)
    test_dataloader = DataLoader(torch.cat(test_data_tensors),
                                 batch_size=batch_size, shuffle=True)
    return train_dataloader, test_dataloader

def osie_img_to_file(img_path):
    """
    Takes an image path and saves a list of all images in RGB as a npy file.
    Don't use this though, it uses about 20x the memory.
    """
    all_images = []
    for img in img_path:
        print(img)
        img = np.asarray(Image.open(img))
        all_images.append(img.transpose(2, 0, 1))
    np.save("OSIE_imgs.npy", all_images)

if __name__ == "__main__":
    """@Ippa"""
    # NOTE: Please teach me how to do this elegantly:
    folder = os.path.abspath(os.path.join(os.path.dirname(__file__), 'OSIE_imgs'))
    files = list_files(folder)
    # Thank you.


import cafa3
import constant as const
import taxon
#import keyword_counts
import argparse

cobj = cafa3.Cafa3(True)

#cobj.create_author_kwd_taxon_dict()
#cobj.fmax_kwdscores_by_taxonID(1, 1, 9606)

#print(cobj.author_list)

parser = argparse.ArgumentParser(description="This command line program will output csv files or plots for CAFA3"
                                             " file data. The csv files output can be of the raw keyword counts, raw"
                                             "keyword counts by model number, or a scoring plot. Plots can either be of"
                                             " several different types. or th")
#parser.add_argument('raw data directory path', metavar='raw data path', type='string', nargs='+', help=
#                    "This is the path to the directory that contains the CAFA raw submissions "
#                    "files for each lab team. ")
parser.add_argument('raw_submissions_path', help="The path to the directory that contains the CAFA raw submissions "
                    "files for each lab team. ")
parser.add_argument('final_results_path', nargs="?", help="The path to the directory that contains the CAFA final "
                                                          "results files. This is only needed when using the optional "
                                                          "arguments -? -? ")

'''
group1 = parser.add_argument_group("frequency commands", "This group of positional arguments deals exclusively with outputting csv files"
                                       " and plots for the unweighted frequency of CAFA keywords.")

group1.add_argument('raw_submissions_path', help="The path to the directory that contains the CAFA raw submissions "
                                                 "files for each lab team")

group1.add_argument('--create_csv_file', help="This option will generate a csv file of keyword frequencies",
                    action="store_true")

group1.add_argument('--plot', help="This option will generate a plot of the keyword frequencies. By itself, "
                                           "the plot output is the frequency of the keywords regardless of the model "
                                           "number. If combined with the -m(--model) option, the plot will be the "
                                           "keyword frequencies by model number.", action="store_true")

group2 = parser.add_argument_group("relative frequency commands", "This group of positional arguments deals exclusively with outputting"
                                                " csv files and plots for the weighted frequency of CAFA keywords.")

group2.add_argument('raw_submissions_path', help="The path to the directory that contains the CAFA raw submissions "
                                                 "files for each lab team")

group2.add_argument('final_results_path', nargs="?", help="The path to the directory that contains the CAFA final results files. "
                                               "This is only needed when using the optional arguments -? -? ") '''

#group = parser.add_mutually_exclusive_group()
#group.add_argument("-r", "--rawcounts", help="", action="store_true");
#group.add_argument("-w", "--weightedcounts", help="", action="store_true");

args = parser.parse_args()
#print(args)
print(args.raw_submissions_path)
print(args.final_results_path)
# Test raw keyword counts

# Test raw counts plot and csv
#cobj.create_keycount_csv()

#cobj.count_kwds('1')
#cobj.plot_raw_keyword_counts(8, 6, 8, 8, 12, '5C89C4')

#cobj.count_kwds('2')
#cobj.plot_raw_keyword_counts(8, 6, 8, 8, 12, '5C89C4')

#cobj.count_kwds('2')
#cobj.plot_raw_keyword_counts(4, 3, 6, 6, 8, 'ff00ff')
# End test raw counts plot and csv

# Test relative fmax scores and equal weights scores
#cobj.kwdscores_by_taxon('BPO', taxonName='all')
#cobj.plot_ontology_score_results('BPO', taxonName='all')
#cobj.csv_ontology_score_results('BPO', 1, 1, 'all')

#cobj.kwdscores_by_taxon('MFO', taxonName='all')
#cobj.plot_ontology_score_results('MFO', taxonName='all')
#cobj.csv_ontology_score_results('MFO', 1, 1, 'all')

#cobj.kwdscores_by_taxon('CCO', taxonName='all')
#cobj.plot_ontology_score_results('CCO', taxonName='all')
#cobj.csv_ontology_score_results('CCO', 1, 1, 'all')
# End test relative fmax scores and equal weights scores

# Test relative fmax scores and equal weights scores, specific taxon
#cobj.kwdscores_by_taxon('BPO', taxonID=9606)
#cobj.plot_ontology_score_results('BPO', taxonID=9606)
#cobj.csv_ontology_score_results('BPO', 1, 1, taxonID=9606)

#cobj.kwdscores_by_taxon('MFO', taxonID=9606)
#cobj.plot_ontology_score_results('MFO', taxonID=9606)
#cobj.csv_ontology_score_results('MFO', 1, 1, taxonID=9606)

#cobj.kwdscores_by_taxon('CCO', taxonID=9606)
#cobj.plot_ontology_score_results('CCO', taxonID=9606)
#cobj.plot_ontology_score_results('CCO', 8, 6, 6, 8, 12, "00ff00", "10aa10", taxonID=9606, mode=1, type=2)

#cobj.kwdscores_by_taxon('CCO', taxonName='all', mode=1, type=1)
#cobj.plot_ontology_score_results(8, 6, 6, 8, 12, "00ff00", "10aa10")
#cobj.csv_ontology_score_results()

#cobj.kwdscores_by_taxon('BPO', taxonName='all', mode=1, type=1)
#cobj.plot_ontology_score_results(8, 6, 6, 8, 12, "00ff00", "10aa10")
#cobj.csv_ontology_score_results()

#cobj.kwdscores_by_taxon('BPO', taxonName='HUMAN', mode=1, type=1)
#cobj.plot_ontology_score_results(8, 6, 6, 8, 12, "00ff00", "10aa10")
#cobj.csv_ontology_score_results()

# End test relative fmax scores and equal weights scores, specific taxon

#cobj.fmax_kwdscores_by_taxon('BPO', taxonID=9606)
#cobj.plot_ontology_fmax_results('BPO', taxonID=9606)
#cobj.csv_ontology_fmax_results('BPO', 1, 1, 9606)

#cobj.fmax_kwdscores_by_taxon('CCO', taxonID=9606)
#cobj.plot_ontology_fmax_results('CCO', taxonID=9606)

#cobj.fmax_kwdscores_by_taxon('MFO', taxonID=9606)
#cobj.plot_ontology_fmax_results('MFO', taxonID=9606)

#cobj.plot_ontology_fmax_results('CCO')
#cobj.plot_ontology_fmax_results('MFO')
#cobj.csv_ontology_fmax_results(1, 1, 9606)

#print("\n\nAuthor List:")
#print(cobj.keyword_relative_fmax_score)
#print(cobj.keyword_fmax_score_template)
#print(cobj.author_list)
#cobj.create_taxon_id_dictionary()
#print(cobj.taxon_id_counts)
#cobj.create_taxon_id_list()
#print(cobj.taxon_id_list)

#ontologies_list = ('BPO', 'CCO', 'MFO')
#bpo_taxons = ("ARATH", "BACSU", "CANAX", "DANRE", "DICDI", "DROME", "ECOLI", "eukarya", "HUMAN", "MOUSE", "prokarya", "RAT", "SCHPO")
#cco_taxons = ("ARATH", "CANAX", "DICDI", "DROME", "ECOLI", "eukarya", "HUMAN", "MOUSE", "prokarya", "RAT")
#mfo_taxons = ("ARATH", "BACSU", "DROME", "ECOLI", "eukarya", "HUMAN", "MOUSE", "prokarya", "RAT", "SALTY")

#cobj.fmax_kwdscores_by_ontology_taxon_list(1, 1, ontologies=ontologies_list, bpo_taxons = bpo_taxons, cco_taxons = cco_taxons, mfo_taxons=mfo_taxons)

#DEBUG CODE
#print('TAXON NAME: '+cobj.taxon_name)
#print('TAXON NAME: '+cobj.taxon_name)

#print(cobj.keyword_fmax_score)
#print(cobj.keyword_relative_fmax_score)

#sum = 0
#for kwd in cobj.keyword_relative_fmax_score:
#    sum += cobj.keyword_relative_fmax_score[kwd]
#print(sum)
#sum = 0
#for kwd in cobj.keyword_fmax_score:
#    sum += cobj.keyword_fmax_score[kwd]
#print(sum)
#print("Taxon:", taxon.taxon_name_converter(7227))
# END DEBUG CODE


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

import io

import sys

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import requests
import re

url = "https://baike.baidu.com"
headers = {
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Accept-Encoding': 'gzip, deflate, br',
	'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
	'Connection': 'keep-alive',
	'Host': 'baike.baidu.com',
	'Cookie': '0',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
	'Upgrade-Insecure-Requests': '1',
}
response = requests.get('https://baike.baidu.com/item/{}'.format("北京地铁"), headers=headers, allow_redirects=True)
txt = response.content
content = txt.decode('utf-8')
pattern = re.compile(
	r'
(.*?)\s*(.*?)<')
result = re.findall(pattern, content)
print(result)

# second mod
lines={}
for i in result:
	line = list(i)
	if len(line) == 3:
		line[1] = line[1] + line[2]
	print(line[0], line[1])
	lines[line[1]]=line[0]
site={}
pattern2=re.compile(r'	([\u4e00-\u9fa5]+)	')
pattern3=re.compile(r'

([\u4e00-\u9fa5]+)	')
partern4=re.compile(r'

([\u4e00-\u9fa5]+)	')
for item in lines:
	print(item)
	response=requests.get(url+lines[item],headers=headers)
	content=response.content.decode('utf-8')
	sites=re.findall(pattern2,content)
	if len(sites)==0:
		sites=re.findall(pattern3,content)
		# if len(sites)==0:
		# 	sites=re.findall(partern4,content)
	site[item]=sites

for i in site:
	print(i,site[i])


import json
from pickletools import read_float8
import pyodbc
import pandas as pd
import Config as C

UserTable = 'user_table'

def InsertRowToDB(row):
    UserJson = GenUserJson(row)
    SQLInsert(UserJson, UserTable)

#   Connect to SQL Server Database
def SqlConnect():
    conn = pyodbc.connect(
        'Driver={SQL Server};'
        f'Server={C.SERVER};'
        f'Database={C.DATABASE};'
        f'UID={C.USERNAME};'
        f'PWD={C.PASSWORD};'
        'Trusted_Connection=no;'
    )
    cursor = conn.cursor()
    return cursor

#   Generate User Dictionary (Json) to be added to the User table
def GenUserJson(row):
    JsonUser = {}

    JsonUser['first_name'] = row['name']['first']
    JsonUser['last_name'] = row['name']['last']
    JsonUser['date_of_birth'] = (row['dob']['date'])[0:10]
    JsonUser['gender'] = row['gender']
    JsonUser['nationality'] = row['nat']
    JsonUser['country'] = row['location']['country']
    JsonUser['city'] = row['location']['city']
    JsonUser['latitude'] = row['location']['coordinates']['latitude']
    JsonUser['longitude'] = row['location']['coordinates']['longitude']

    return JsonUser

#   To Insert into tables in SQL Server
def SQLInsert(Data, SQLTable):

    cursor = SqlConnect()
    columns = ', '.join(str(x).replace('/', '_') for x in Data.keys())
    values = ', '.join("'" + str(x).replace('/', '_') + "'" for x in Data.values())
    SQLQuery = """INSERT INTO %s ( %s ) OUTPUT Inserted.ID VALUES ( %s );""" % (SQLTable, columns, values)
    cursor.execute(SQLQuery)

    ID = cursor.fetchone()[0]
    cursor.commit()
    cursor.close()
    return ID


# flake8: noqa: F811
# pylint: disable=unused-argument
# pylint: disable=too-few-public-methods

"""download module"""

import os.path
import signal
from concurrent.futures import ThreadPoolExecutor
from threading import Event
from typing import Iterable, Any

import requests
import rich
from rich import console
from rich.progress import (BarColumn, DownloadColumn, Progress, TaskID,
                           TextColumn, TimeElapsedColumn, TransferSpeedColumn)

console = rich.get_console()
done_event = Event()

def handle_sigint(signum: Any, frame: Any) -> None:
    """Progress bar handler"""
    done_event.set()

signal.signal(signal.SIGINT, handle_sigint)

class DownloadProgressBar():
    """
    Object to manage Download process with Progress Bar from Rich
    """

    def __init__(self) -> None:
        """
        Class Constructor
        """
        self.progress = Progress(
            TextColumn("ðŸ’¾  Downloading [bold blue]{task.fields[filename]}", justify="right"),
            BarColumn(bar_width=None),
            "[progress.percentage]{task.percentage:>3.1f}%",
            "â€¢",
            TransferSpeedColumn(),
            "â€¢",
            DownloadColumn(),
            "â€¢",
            TimeElapsedColumn(),
            "â€¢",
            console=console
        )

    def _copy_url(self, task_id: TaskID, url: str, path: str, block_size: int = 1024) -> bool:
        """Copy data from a url to a local file."""
        response = requests.get(url, stream=True, timeout=5)
        # This will break if the response doesn't contain content length
        self.progress.update(task_id, total=int(response.headers['Content-Length']))
        with open(path, "wb") as dest_file:
            self.progress.start_task(task_id)
            for data in response.iter_content(chunk_size=block_size):
                dest_file.write(data)
                self.progress.update(task_id, advance=len(data))
                if done_event.is_set():
                    return True
        # console.print(f"Downloaded {path}")
        return False

    def download(self, urls: Iterable[str], dest_dir: str) -> None:
        """Download multuple files to the given directory."""
        with self.progress:
            with ThreadPoolExecutor(max_workers=4) as pool:
                for url in urls:
                    filename = url.split("/")[-1].split('?')[0]
                    dest_path = os.path.join(dest_dir, filename)
                    task_id = self.progress.add_task("download", filename=filename, start=False)
                    pool.submit(self._copy_url, task_id, url, dest_path)


import base64
import re

import click
from pulses import (
    BroadlinkEncoder, RemoteCode, BlindAction, build_preamble, PhaseDuration)
from ruamel.yaml import YAML

yaml = YAML()

# Number of time for the broadlink to repeat the transmission
BROADLINK_REPEATS = 1
# Number of repetitions of the remote payload within the broadlink payload
PAYLOAD_REPEATS = 3

CUSTOMIZE_BASE = {
    'assumed_state': True,
}

def get_broadlink_send_action(host: str, packet: str):
    send_action = {
        'service': 'broadlink.send',
        'data': {
            'host': host,
            'packet': packet
        }
    }

    return send_action

def encode_packet(encoder: BroadlinkEncoder, remote_code: RemoteCode):
    data = []
    data += build_preamble()
    for _ in range(PAYLOAD_REPEATS):
        data += remote_code.get_phase_durations()
        # Drop a little bit of padding between payloads within a transmission
        # 13832us should do
        data += [PhaseDuration(not data[-1].phase, 13832/4)]
        data += [PhaseDuration(not data[-1].phase, 13832/4)]
        data += [PhaseDuration(not data[-1].phase, 13832/4)]
        data += [PhaseDuration(not data[-1].phase, 13832/4)]

    packet = encoder.encode(data)
    return base64.b64encode(packet).decode('utf-8')

@click.command(help='Generate Home Assistant config for RAEX blind '
                    'transmission via Broadlink')
@click.option('--input', required=True, type=click.File('r'),
              help='Path to the input config file')
@click.option('--output', required=True, type=click.File('w'),
              help='Destination path for the generated Home Assistant package')
@click.option('--support-pairing/--no-support-pairing', default=False,
              help='Whether switches should be generated to support pairing '
                   'of blinds')
def main(input, output, support_pairing):
    seed_config = yaml.load(input)
    host = seed_config['host_device_ip']
    encoder = BroadlinkEncoder(repeats=BROADLINK_REPEATS)

    action = lambda packet: get_broadlink_send_action(host, packet)

    switches = {}
    covers = {}
    customize = {}

    for blind in seed_config['blinds']:
        open_code = RemoteCode(channel=blind['channel'],
                               remote=blind['remote'], action=BlindAction.UP)
        close_code = RemoteCode(channel=blind['channel'],
                                remote=blind['remote'],
                                action=BlindAction.DOWN)
        stop_code = RemoteCode(channel=blind['channel'],
                               remote=blind['remote'], action=BlindAction.STOP)
        pair_code = RemoteCode(channel=blind['channel'],
                               remote=blind['remote'], action=BlindAction.PAIR)
        open_packet = encode_packet(encoder, open_code)
        close_packet = encode_packet(encoder, close_code)
        stop_packet = encode_packet(encoder, stop_code)
        pair_packet = encode_packet(encoder, pair_code)

        cover = {
            'friendly_name': blind['name'],
            'device_class': 'blind',
            'open_cover': action(open_packet),
            'close_cover': action(close_packet),
            'stop_cover': action(stop_packet),
        }
        covers[camelize(blind['name'])] = cover
        customize['cover.' + camelize(blind['name'])] = CUSTOMIZE_BASE

        pairing_switch = {
            'friendly_name': blind['name'] + ' Blind Pairing',
            'value_template': 'off',
            'turn_on': action(pair_packet),
            'turn_off': [],
        }
        switches[camelize(pairing_switch['friendly_name'])] = pairing_switch

    package = {
        'homeassistant': {
            'customize': customize,
        },
        'cover': [
            {
                'platform': 'template',
                'covers': covers,
            }
        ]
    }

    if support_pairing:
        package['switch'] = {
            'platform': 'template',
            'switches': switches,
        }

    yaml.dump(package, output)

def camelize(s):
    s = re.sub(r'[^A-Za-z0-9\s]', '', s).lower()
    return '_'.join(s.split(' '))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*
"""SixTrack Tools - Particle Generator

  SixTrack Tools - Particle Generator
 =====================================
  By: Veronica Berglyd Olsen
      CERN (BE-ABP-HSS)
      Geneva, Switzerland

  Generate a particle distribution file for SixTrack

"""

import logging
import timeit
import numpy as np

from os                import path, getcwd
from sttools.constants import Const
from sttools.physics   import Physics
from sttools.functions import parsePath

logger = logging.getLogger(__name__)

class PartDist():

    partMass = None
    randSeed = None

    def __init__(self, initData):

        keyVals = initData.keys()

        # Distributions
        self.genXXP = None
        self.genYYP = None
        self.genZ   = None
        self.genP   = None
        self.genDDP = None
        self.genE   = None

        self.hasError = False
        logger.info("Initialising particle distribution:")
        if "energy" in keyVals:
            self.beamEnergy = initData["energy"]
            logger.info(" * Reference Energy:       %16.9e" % self.beamEnergy)
        else:
            self.beamEnergy = 1.0

        if "mass" in keyVals:
            self.partMass = initData["mass"]
            logger.info(" * Reference Mass:         %16.9e" % self.partMass)
        else:
            self.partMass = 1.0

        if "nemit" in keyVals:
            self.normEmit = initData["nemit"]
            if len(self.normEmit) == 2:
                logger.info(" * Normalised Emittance:   %16.9e %16.9e" % (self.normEmit[0],self.normEmit[1]))
            else:
                logger.error("Value 'nemit' must be a vector of [x,y] values")
                self.hasError = True
        else:
            self.normEmit = [0.0, 0.0]

        if "gemit" in keyVals:
            self.geomEmit = initData["gemit"]
            if len(self.geomEmit) == 2:
                logger.info(" * Geometric Emittance:    %16.9e %16.9e" % (self.geomEmit[0],self.geomEmit[1]))
            else:
                logger.error("Value 'gemit' must be a vector of [x,y] values")
                self.hasError = True
        else:
            self.geomEmit = [0.0, 0.0]

        if "twalpha" in keyVals:
            self.twissAlpha = initData["twalpha"]
            if len(self.twissAlpha) == 2:
                logger.info(" * Twiss Alpha:            %16.9e %16.9e" % (self.twissAlpha[0],self.twissAlpha[1]))
            else:
                logger.error("Value 'twalpha' must be a vector of [x,y] values")
                self.hasError = True
        else:
            self.twissAlpha = [0.0, 0.0]

        if "twbeta" in keyVals:
            self.twissBeta = initData["twbeta"]
            if len(self.twissBeta) == 2:
                logger.info(" * Twiss Beta:             %16.9e %16.9e" % (self.twissBeta[0],self.twissBeta[1]))
            else:
                logger.error("Value 'twbeta' must be a vector of [x,y] values")
                self.hasError = True
        else:
            self.twissBeta = [1.0, 1.0]

        if "offset" in keyVals:
            self.beamOffset = initData["offset"]
            if len(self.beamOffset) == 4:
                logger.info(" * Beam Offset X,XP:       %16.9e %16.9e" % (self.beamOffset[0],self.beamOffset[1]))
                logger.info(" * Beam Offset Y,YP:       %16.9e %16.9e" % (self.beamOffset[2],self.beamOffset[3]))
            else:
                logger.error("Value 'offset' must be a vector of [x,xp,y,yp] values")
                self.hasError = True
        else:
            self.beamOffset = [0.0, 0.0, 0.0, 0.0]

        if "sigmaxxp" in keyVals:
            self.sigmaXXP = initData["sigmaxxp"]
            if len(self.twissBeta) == 2:
                logger.info(" * Beam Sigma X,XP:        %16.9e %16.9e" % (self.sigmaXXP[0],self.sigmaXXP[1]))
            else:
                logger.error("Value 'sigmaxxp' must be a vector of [x,xp] values")
                self.hasError = True
        else:
            self.sigmaXXP = [1.0, 1.0]

        if "sigmayyp" in keyVals:
            self.sigmaYYP = initData["sigmayyp"]
            if len(self.twissBeta) == 2:
                logger.info(" * Beam Sigma Y,YP:        %16.9e %16.9e" % (self.sigmaYYP[0],self.sigmaYYP[1]))
            else:
                logger.error("Value 'sigmayyp' must be a vector of [y,yp] values")
                self.hasError = True
        else:
            self.sigmaYYP = [1.0, 1.0]

        if "sigmaz" in keyVals:
            self.sigmaZ = initData["sigmaz"]
            logger.info(" * Beam Sigma Z:           %16.9e" % self.sigmaZ)
        else:
            self.sigmaZ = 0.0

        if "spreade" in keyVals:
            self.spreadE = initData["spreade"]
            logger.info(" * Beam Energy Spread:     %16.9e" % self.spreadE)
        else:
            self.spreadE = 0.0

        if "spreadp" in keyVals:
            self.spreadP = initData["spreadp"]
            logger.info(" * Beam Momentum Spread:   %16.9e" % self.spreadP)
        else:
            self.spreadP = 0.0

        if "format" in keyVals:
            self.colFormat = initData["format"]
            logger.info("Output Column Formats:")
            logger.info(", ".join(self.colFormat))
        else:
            self.colFormat = []

        bG, bB, bP0 = Physics.energyGammaBeta(self.beamEnergy,self.partMass)
        self.beamGamma = bG
        self.beamBeta  = bB
        self.beamMom   = bP0

        if not "gemit" in keyVals:
            if self.beamBeta > 0.0:
                gEmitX = self.normEmit[0]/(self.beamGamma*self.beamBeta)
                gEmitY = self.normEmit[1]/(self.beamGamma*self.beamBeta)
                self.geomEmit = [gEmitX,gEmitY]
            else:
                self.geomEmit = [1.0,1.0]

        return

    def setSeed(self, newSeed, seedFile=None):

        if seedFile is not None:
            seedFile = parsePath(seedFile, getcwd(), "seeds.txt")
            with open(seedFile,"a+") as outFile:
                outFile.write(str(newSeed)+"\n")

        np.random.seed(newSeed)

        return True

    #
    # Particle Generators
    #

    def genDist(self, nPairs):

        startTime = timeit.default_timer()
        self.genTransverseDist(nPairs)
        self.genLongitudinalDist(nPairs)
        timePassed = timeit.default_timer() - startTime

        logger.info("Generated %d particle pairs in %.4f sec" % (nPairs,timePassed))

        return

    def genNormDist(self, nPairs):

        nPart = nPairs * 2

        startTime   = timeit.default_timer()
        self.genXXP = np.random.normal(0.0, 1.0, (nPart,2)) * self.sigmaXXP
        self.genYYP = np.random.normal(0.0, 1.0, (nPart,2)) * self.sigmaYYP
        self.genZ   = np.random.normal(0.0, 1.0, nPart)     * self.sigmaZ
        self.genDDP = np.random.normal(0.0, 1.0, nPart)     * self.spreadP
        self.genE   = np.zeros(nPart)
        self.genP   = np.zeros(nPart)
        timePassed  = timeit.default_timer() - startTime

        logger.info("Generated %d particle pairs in %.4f sec" % (nPairs,timePassed))

        return

    def genTransverseDist(self, nPairs):
        """Generate x,xp and y,yp bivariate distributions via covariance matrices
        See: http://se.mathworks.com/help/matlab/ref/randn.html
        """

        nPart  = nPairs * 2

        gEmitX = self.geomEmit[0]
        gEmitY = self.geomEmit[1]
        alphaX = self.twissAlpha[0]
        alphaY = self.twissAlpha[1]
        betaX  = self.twissBeta[0]
        betaY  = self.twissBeta[1]
        gammaX = 1+alphaX**2 / betaX
        gammaY = 1+alphaY**2 / betaY

        # Covariant matrices
        sigmaX = np.array([[betaX, -alphaX], [-alphaX, gammaX]])
        sigmaY = np.array([[betaX, -alphaY], [-alphaY, gammaY]])

        # Cholesky decomposition
        cholX  = np.linalg.cholesky(sigmaX*gEmitX)
        cholY  = np.linalg.cholesky(sigmaY*gEmitY)

        # Generate uncorrelated distributions
        distX  = np.random.normal(0.0, 1.0, (nPart,2))
        distY  = np.random.normal(0.0, 1.0, (nPart,2))
        distX  = np.dot(distX, cholX)
        distY  = np.dot(distY, cholY)

        # Apply the Cholesky decomposition
        self.genXXP = np.column_stack((distX[:,1], distX[:,0]))
        self.genYYP = np.column_stack((distY[:,1], distY[:,0]))

        return

    def genLongitudinalDist(self, nPairs):

        nPart = nPairs * 2

        self.genZ   = np.random.normal(0, 1, nPart) * self.sigmaZ
        self.genE   = self.beamEnergy * (1 + np.random.normal(0, 1, nPart)*self.spreadE)
        self.genP   = np.sqrt((self.genE - self.partMass) * (self.genE + self.partMass))
        self.genDDP = (self.genP - self.beamMom) / self.beamMom

        return

    #
    # Write Files
    #

    def writeFort13(self, outPath, nPairs):

        startTime = timeit.default_timer()

        if nPairs*2 > len(self.genXXP):
            logger.error("Cannot output more particle pairs than has been generated")
            return False

        beamEnergy = self.beamEnergy * 1e-6 # Unit MeV
        outFile    = parsePath(outPath, getcwd(), "fort.13")
        with open(outFile, "w") as fort13:
            for i in range(nPairs):
                p1 = i*2
                p2 = p1+1

                # Particle 1
                fort13.write("%22.15e\n" % (self.genXXP[p1,0]*1e3))
                fort13.write("%22.15e\n" % (self.genXXP[p1,1]*1e3))
                fort13.write("%22.15e\n" % (self.genYYP[p1,0]*1e3))
                fort13.write("%22.15e\n" % (self.genYYP[p1,1]*1e3))
                fort13.write("%22.15e\n" % (self.genZ[p1]*1e3))
                fort13.write("%22.15e\n" %  self.genDDP[p1])

                # Particle 2
                fort13.write("%22.15e\n" % (self.genXXP[p2,0]*1e3))
                fort13.write("%22.15e\n" % (self.genXXP[p2,1]*1e3))
                fort13.write("%22.15e\n" % (self.genYYP[p2,0]*1e3))
                fort13.write("%22.15e\n" % (self.genYYP[p2,1]*1e3))
                fort13.write("%22.15e\n" % (self.genZ[p2]*1e3))
                fort13.write("%22.15e\n" %  self.genDDP[p2])

                # Energy
                fort13.write("%22.15e\n" % beamEnergy)
                fort13.write("%22.15e\n" % (self.genE[p1]*1e-6))
                fort13.write("%22.15e\n" % (self.genE[p2]*1e-6))

        timePassed = timeit.default_timer() - startTime

        logger.info("Wrote %d particle pairs to file in %.4f sec" % (nPairs,timePassed))

        return True

    def writeCollDist(self, outPath, nPairs):

        startTime = timeit.default_timer()

        if nPairs*2 > len(self.genXXP):
            logger.error("Cannot output more particle pairs than has been generated")
            return False

        outFile = parsePath(outPath, getcwd(), "partDist.dat")
        with open(outFile, "w") as outFile:
            for i in range(2*nPairs):
                outFile.write((("{: 22.15e} "*6)+"\n").format(
                    self.genXXP[i,0],
                    self.genXXP[i,1],
                    self.genYYP[i,0],
                    self.genYYP[i,1],
                    self.genZ[i],
                    self.genDDP[i]
                ))

        timePassed = timeit.default_timer() - startTime

        logger.info("Wrote %d particle pairs to file in %.4f sec" % (nPairs,timePassed))

        return True

    def writeDistBlockFile(self, outPath, nPairs, fPrec="22.15e"):

        startTime = timeit.default_timer()

        if nPairs*2 > len(self.genXXP):
            logger.error("Cannot output more particle pairs than has been generated")
            return False

        iS = 0
        iE = 2*nPairs

        xP = np.sin(np.arctan(self.genXXP[iS:iE,1]))
        yP = np.sin(np.arctan(self.genYYP[iS:iE,1]))
        pX = xP * self.genP[iS:iE]
        pY = yP * self.genP[iS:iE]
        pZ = np.sqrt(self.genP[iS:iE]**2 - pX**2 - pY**2)

        nCol = len(self.colFormat)
        outData = np.zeros((2*nPairs,nCol))
        for i in range(nCol):
            if self.colFormat[i] == "X":
                outData[:,i] = self.genXXP[iS:iE,0]
            elif self.colFormat[i] == "Y":
                outData[:,i] = self.genYYP[iS:iE,0]
            elif self.colFormat[i] == "XP":
                outData[:,i] = xP
            elif self.colFormat[i] == "YP":
                outData[:,i] = yP
            elif self.colFormat[i] == "PX":
                outData[:,i] = pX
            elif self.colFormat[i] == "PY":
                outData[:,i] = pY
            elif self.colFormat[i] in ("PX/P0","PXP0"):
                outData[:,i] = pX/self.beamMom
            elif self.colFormat[i] in ("PY/P0","PYP0"):
                outData[:,i] = pY/self.beamMom
            elif self.colFormat[i] == "SIGMA":
                outData[:,i] = self.genZ[iS:iE]
            else:
                logger.error("Unknown column format '%s'" % self.colFormat[i])

        outFile = parsePath(outPath, getcwd(), "partDist.dat")
        with open(outFile, "w") as outFile:
            outFile.write("# %s\n" % ", ".join(self.colFormat))
            for i in range(2*nPairs):
                outFile.write(((("{: "+fPrec+"} ")*nCol)+"\n").format(
                    *outData[i,:]
                ))

        timePassed = timeit.default_timer() - startTime

        logger.info("Wrote %d particle pairs to file in %.4f sec" % (nPairs,timePassed))

        return True

# End Class Dist


#!python
# repair or merge multiple surface/solid meshes
# v1.0 2022/02 paulo.ernesto
'''
usage: $0 mode%surface,solid input_files#input_path*vtk,obj,msh operation=min,max,order cell_size=10 output_path*vtk,obj,msh display@
'''
'''
Copyright 2022 Vale

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*** You can contribute to the main repository at: ***

https://github.com/pemn/vtk_merge_meshes
---------------------------------
'''
import sys, os.path
import numpy as np
import pandas as pd
import pyvista as pv

# import modules from a pyz (zip) file with same name as scripts
sys.path.insert(0, os.path.splitext(sys.argv[0])[0] + '.pyz')

from _gui import usage_gui, commalist, pd_load_dataframe, pd_save_dataframe,log

from pd_vtk import pv_read, pv_save, vtk_plot_meshes, vtk_Voxel, vtk_meshes_bb

def vtk_merge_surface(meshes, operation, cell_size):
  bb = vtk_meshes_bb(meshes)

  grid = vtk_Voxel.from_bb(bb, cell_size, 2)

  gz = np.full(grid.n_cells, np.nan)

  for mesh in meshes:
    fn = None
    if operation != 'order':
      fn = eval('np.' + operation)
    mz = grid.get_elevation(mesh, fn)

    gz = np.where(np.isnan(gz), mz, gz)
    if fn is not None:
      gz = np.apply_along_axis(fn, 0, [gz, mz])

  grid.cell_data['Elevation'] = gz
  mesh = grid.threshold(None, 'Elevation').ctp()
  mesh.warp_by_scalar('Elevation', inplace=True)

  return mesh

def vtk_merge_solid(meshes, operation, cell_size):

  bb = vtk_meshes_bb(meshes, cell_size)

  grid = vtk_Voxel.from_bb(bb, cell_size, 3)

  gz = np.full(grid.n_cells, np.nan)
  cells = pv.PolyData(grid.cell_centers().points)

  for mesh in meshes:
    # select_enclosed_points(surface, tolerance=0.001, inside_out=False, check_surface=True, progress_bar=False)
    mz = cells.select_enclosed_points(mesh, check_surface=False)
    if operation == 'min':
      gz = np.nanmin([gz, mz['SelectedPoints']], 0)
    else:
      gz = np.nanmax([gz, mz['SelectedPoints']], 0)

  grid.cell_data['SelectedPoints'] = gz
  # contour(isosurfaces=10, scalars=None, compute_normals=False, compute_gradients=False, compute_scalars=True, rng=None, preference='point', method='contour', progress_bar=False)

  return grid.threshold(0.5).extract_surface()

def vtk_merge_meshes(mode, input_files, operation, cell_size, output, display):
  if not mode:
    mode == 'surface'
  # prevent invalid grid sizes
  if not cell_size:
    cell_size = 1
  else:
    cell_size = float(cell_size)

  if not operation:
    operation = 'min'

  input_files = commalist().parse(input_files).split()

  meshes = []
  for input_path in input_files:
    if not os.path.exists(input_path):
      print(input_path,"not found")
      continue
    mesh = pv_read(input_path)
    meshes.append(mesh)

  mesh = None
  if mode == 'surface':
    mesh = vtk_merge_surface(meshes, operation, cell_size)
  if mode == 'solid':
    mesh = vtk_merge_solid(meshes, operation, cell_size)

  if output:
    pv_save(mesh, output)
  if int(display):
    vtk_plot_meshes([mesh] + meshes)

main = vtk_merge_meshes

if __name__=="__main__":
  usage_gui(__doc__)


from django.shortcuts import render
from rest_framework.mixins import (
    CreateModelMixin, ListModelMixin,  RetrieveModelMixin, UpdateModelMixin
)
from django.http import JsonResponse
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
from .models import News
from .serializer import NewsSerializer
from django.utils import timezone
from .forms import ApikeyForm
import requests
from django.shortcuts import redirect

# Create your views here.

class NewsViewSet(GenericViewSet, CreateModelMixin, RetrieveModelMixin,UpdateModelMixin, ListModelMixin):
    serializer_class = NewsSerializer
    queryset = News.objects.all()

    def list(self,request, *args, **kwargs):
        # base class
        request.session.setdefault('wrongkey','')
        request.session.setdefault('apikey','0')
        if request.session['apikey'] == '1':
            request.session['apikey'] = '0'
        else:
            request.session['wrongkey'] = ''
        return render(request, 'news/base.html', {'records':self.queryset.count(), 'wrongkey':request.session['wrongkey']})

    @action(detail=False, methods=['post'])
    def apikey(self,request, *args, **kwargs):
        n = 0
        request.session['wrongkey'] = ''
        request.session['apikey'] = '1'
        if request.method == 'POST':
            r = requests.get('https://newsapi.org/v2/everything?q=tesla&from=2022-01-15&sortBy=publishedAt&apiKey='+request.data['apikey'])
            r.json()
            try:
                r.json()['articles']
            except Exception as e:
                print("no key!")
                request.session['wrongkey']='Enter a valid key.'
                return redirect('/news/')
            for  a in r.json()['articles']:
                print(a["title"])
                news = News()
                news.source_id = a["source"]["id"]
                news.source_name = a["source"]["name"]
                news.author=a["author"]
                news.title= a["title"]
                news.description= a["description"]
                news.url=a["url"]
                news.urlToImage= a["urlToImage"]
                news.publishedAt= a["publishedAt"]
                news.content = a["content"]
                news.save()

        return redirect('/news/')

    @action(detail=False, methods=['get'])
    def news20(self,request, *args, **kwargs):
        # get last 20 news ordered by published date
        #last20 = self.queryset.order_by('-publishedAt')[:20]
        last20 = News.objects.filter(publishedAt__lte=timezone.now()).order_by('-publishedAt')[:20]
        return render(request, 'news/list_news.html', {'news20':last20})
        data = {'articles':[]}
        for a in last20:
            data2 ={
                "source" : {
                    "id":a.source_id,
                    "name":a.source_name
                },
                "author": a.author,
                "title": a.title,
                "description": a.description,
                "url": a.url,
                "urlToImage": a.urlToImage,
                "publishedAt": a.publishedAt,
                "content" : a.content,
            }
            data['articles'].append(data2)
        #return a json response
        return JsonResponse(data, content_type='application/json')

    @action(detail=False, methods=['get'])
    def news100(self,request, *args, **kwargs):
        # get last 100 news ordered by published date
        #last100 = self.queryset.order_by('-publishedAt')[:100]
        last100 = News.objects.filter(publishedAt__lte=timezone.now()).order_by('-publishedAt')[:100]
        data = {'articles':[]}
        for a in last100:
            data2 ={
                "source" : {
                    "id":a.source_id,
                    "name":a.source_name
                },
                "author": a.author,
                "title": a.title,
                "description": a.description,
                "url": a.url,
                "urlToImage": a.urlToImage,
                "publishedAt": a.publishedAt,
                "content" : a.content,
            }
            data['articles'].append(data2)
        #return a json response
        return JsonResponse(data, content_type='application/json')

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    @action(detail=False, methods=['get'])
    def news(self, request, *args, **kwargs):
        #return JsonResponse(request.data, content_type='application/json')
        print(request)
        print(request.data)
        print(request.body)
        return self.list(request, *args, **kwargs)


"""Added 5k

Revision ID: 543d4a576dbd
Revises: ef5be88b8c83
Create Date: 2021-09-09 17:57:32.534329

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '543d4a576dbd'
down_revision = 'ef5be88b8c83'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('fivek',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('seconds', sa.Float(), nullable=False),
    sa.Column('date_completed', sa.Date(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('fivek')
    # ### end Alembic commands ###


""""Declares a Python object mapping to the ``transactions`` relation."""
import sqlalchemy

from .base import Relation

class Transaction(Relation):
    """Maintains information about iDIN transactions.
    """

    __tablename__ = 'transactions'

    #: Transaction ID as specified by the intermediary.
    txid = sqlalchemy.Column(
        sqlalchemy.String,
        name='txid',
        primary_key=True,
        nullable=False
    )

    #: The date/time at which the transaction was created, in milliseconds
    #: since the UNIX epoch.
    created = sqlalchemy.Column(
        sqlalchemy.BigInteger,
        name='created',
        nullable=False
    )

    #: Current status of the transaction, may be one of ``open``,
    #: ``success``, ``cancelled``, ``failure`` or ``expired``.
    status = sqlalchemy.Column(
        sqlalchemy.String,
        name='status',
        nullable=False,
        default='open'
    )

    #: Entrance code to be included in the response from the iDIN
    #: acquirer.
    ec = sqlalchemy.Column(
        sqlalchemy.String,
        name='ec',
        nullable=False
    )

    #: Reference at the intermediary for accounting purposes.
    ref = sqlalchemy.Column(
        sqlalchemy.String,
        name='ref',
        nullable=False
    )

# pylint: skip-file


import logging
import time

import omdb

from utils import config

OMDB_REQUEST_COUNT = 0  # DO NOT CHANGE
logger = logging.getLogger("plex-imdb-updater")

def get_imdb_rating_from_omdb(imdb_id, pbar):
    """
    Getting IMDB rating for imdb_id from OMDB
    :param imdb_id: the imdb_id of the rating
    :return: the whole object from OMDB including the rating
    """
    global OMDB_REQUEST_COUNT

    if not config.OMDB_API_KEY:
        return None
    else:
        omdb.set_default("apikey", config.OMDB_API_KEY)

    # Wait 10 seconds for the TMDb rate limit
    if OMDB_REQUEST_COUNT >= 30:
        pbar.postfix[1] = "Waiting 10 seconds OMDb rate limit"
        time.sleep(10)
        OMDB_REQUEST_COUNT = 0

    try:
        media = omdb.imdbid(imdb_id, timeout=5)
    except:
        logger.debug("Error getting rating from OMDB. Trying again in few seconds")
        time.sleep(10)
        OMDB_REQUEST_COUNT = 0
        return get_imdb_rating_from_omdb(imdb_id)

    OMDB_REQUEST_COUNT += 1

    # checking if there really is a rating and rating is not N/A
    if media is not None and 'imdb_rating' in media and media["imdb_rating"] != str('N/A'):
        return media
    else:
        return None

def get_season_from_omdb(imdb_id, season, pbar):
    """
    Getting specific season for IMDB id, including the ratings for each episode
    :param imdb_id: the IMDB item for which the items should be fetched
    :param season: the season for which to fetch episodes
    :return: a pair episode/rating
    """
    global OMDB_REQUEST_COUNT

    if not config.OMDB_API_KEY:
        return None
    else:
        omdb.set_default("apikey", config.OMDB_API_KEY)

    # Wait 10 seconds for the TMDb rate limit
    if OMDB_REQUEST_COUNT >= 30:
        pbar.postfix[1] = "Waiting 10 seconds OMDb rate limit"
        time.sleep(10)
        OMDB_REQUEST_COUNT = 0

    try:
        season = omdb.imdbid(imdb_id, season=season)
    except:
        logger.debug("Error getting rating from OMDB. Trying again in few seconds")
        time.sleep(10)
        OMDB_REQUEST_COUNT = 0
        return get_season_from_omdb(imdb_id, season)

    OMDB_REQUEST_COUNT += 1

    # checking if there really is a rating and rating is not N/A
    if season is not None and "episodes" in season:
        episodes = {}
        for episode in season["episodes"]:
            episodes[episode["episode"]] = episode["imdb_rating"]
        return episodes
    else:
        return None


import unittest
import nose.tools

from mia.reduction.reducers import *
from ..test_utils import *

class ReducersRegressionTest(unittest.TestCase):

    @classmethod
    def setupClass(cls):
        cls._img_dir = get_file_path("mias")
        cls._msk_dir = get_file_path("mias/masks")

    def test_blob_feature_reduction(self):
        reduction = BlobFeaturesReduction(self._img_dir, self._msk_dir)
        blobs_df = reduction.reduce()

        nose.tools.assert_equal(blobs_df.shape[1], 4)
        expected_columns = ['x', 'y', 'radius', 'breast_area']
        assert_data_frame_columns_match(blobs_df, expected_columns)

    def test_line_feature_reduction(self):
        reduction = LineFeaturesReduction(self._img_dir, self._msk_dir)
        lines_df = reduction.reduce()

        nose.tools.assert_equal(lines_df.shape[1], 6)
        expected_columns = ['area', 'min_row', 'min_col', 'max_row',
                            'max_col', 'breast_area']
        assert_data_frame_columns_match(lines_df, expected_columns)

    def test_patch_intensity_feature_reduction(self):
        reduction = BlobFeaturesReduction(self._img_dir, self._msk_dir)
        blobs_df = reduction.reduce()

        reduction = PatchIntensityFeaturesReduction(self._img_dir,
                                                    self._msk_dir)
        intensity_df = reduction.reduce(blobs_df)

        expected_columns = ['x', 'y', 'radius', 'breast_area', 'count',
                            'mean', 'std', 'min', '25%', '50%', '75%',
                            'max', 'skew', 'kurtosis']

        nose.tools.assert_equal(intensity_df.shape[1], 14)
        assert_data_frame_columns_match(intensity_df, expected_columns)

    def test_patch_texture_feature_reduction(self):
        reduction = BlobFeaturesReduction(self._img_dir, self._msk_dir)
        blobs_df = reduction.reduce()

        reduction = PatchTextureFeaturesReduction(self._img_dir,
                                                  self._msk_dir)
        texture_df = reduction.reduce(blobs_df)

        expected_columns = ['x', 'y', 'radius', 'breast_area', 'contrast',
                            'dissimilarity', 'homogeneity', 'energy']

        nose.tools.assert_equal(texture_df.shape[1], 8)
        assert_data_frame_columns_match(texture_df, expected_columns)


import heapq
from collections import defaultdict

class HeapDict:

    """
    O(logn)で要素の挿入
    O(logn)で要素の削除
    O(1)で要素の存在確認
    O(1)で最小値の取得
    """

    def __init__(self):
        """
        h : 最小値を保持すするヒープ
        d : (値:重複個数) の連想配列
        """
        self.h = []
        self.d = defaultdict(int)

    def insert(self, x):
        heapq.heappush(self.h, x)
        self.d[x] += 1

    def erase(self, x):
        if not self.is_exist(x):
            raise RuntimeError(f"{x} not in Dict.")
        self.d[x] -= 1
        # 最小値を更新
        while len(self.h) != 0:
            if self.d[self.h[0]] == 0:
                heapq.heappop(self.h)
            else:
                break

    def is_exist(self, x):
        if x in self.d and self.d[x] != 0:
            return True
        else:
            return False

    def get_min(self, default=0):
        if len(self.h):
            return self.h[0]
        else:
            return default