from agent import *
from mcs_wrapper import *

def check_unique_id(objects):
    ids = []
    for a in objects:
        ids.append(a.id)
    return len(ids) == len(set(ids))

def create_corridor_from_dict(d):
    idx, t, left, right, center, v_limit = d["id"], d["type"], d["left"], d["right"], d["center"], d["v_limit"]
    left_id = d["left_id"] if d["left_id"] != "none" else None
    right_id = d["right_id"] if d["right_id"] != "none" else None
    c = Corridor(idx, t, left, right, center, v_limit)
    c.set_left_and_right_corridor_id(left_id, right_id)
    return c

class Map:
    def __init__(self, corridors):
        # corridors: list of Corridor
        self.corridors = {}
        self.corridors_by_type = {}
        if not check_unique_id(corridors):
            print("Corridors have duplicate ids")
        for c in corridors:
            self.corridors[c.id] = c
            if c.type in self.corridors_by_type.keys():
                self.corridors_by_type[c.type].append(c)
            else:
                self.corridors_by_type[c.type] = [c]

    def corridor_match(self, vehicle):
        for c_id, corridor in self.corridors.items():
            if Point([vehicle.x, vehicle.y]).within(corridor.polygon):
                return corridor
        return False

    def on_merging_lane(self, point):
        for corridor in self.corridors_by_type["merging"]:
            if Point(point).within(corridor.polygon):
                return True
        return False

    def truck_lane_id(self):
        if "merging" in self.corridors_by_type.keys():
            merging_lane = self.corridors_by_type["merging"][0]
            return merging_lane.left_id
        elif "exit" in self.corridors_by_type.keys():
            exit_lane = self.corridors_by_type["exit"][0]
            return exit_lane.left_id
        else:
            for i, c in self.corridors.items():
                if c.right_id is None:
                    return i

    def to_dict(self):
        out = {}
        for i, c in self.corridors.items():
            left_id = "none" if c.left_id is None else c.left_id
            right_id = "none" if c.right_id is None else c.right_id
            c_dict = {"id": c.id, "type": c.type,
                      "left": c.left, "right": c.right, "center": c.center, "v_limit": c.v_limit,
                      "left_id": left_id, "right_id": right_id}
            out[i] = c_dict
        return out

class Environment:
    def __init__(self, m, agents):
        self.map = m
        self.agents = {}
        self.removed_agents = {}
        self.matched_agents = {}  # {agent_id: MatchedAgent, ...}
        self.matched_agents_sorted_by_arc_length = {}  # {corridor_id: [[dis, agent], ...]}
        for a in agents:
            self.agents[a.id] = a
        if not check_unique_id(agents):
            print("Agents have duplicate ids")
        self.match_agents_on_corridor()
        self.sim_time = 0

    def match_agents_on_corridor(self):
        self.matched_agents = {}
        self.matched_agents_sorted_by_arc_length = {}
        for i, agent in self.agents.items():
            matched_corridor = self.map.corridor_match(agent)
            if matched_corridor:
                agent.corridor_history.append(matched_corridor.id)
                if matched_corridor.type == "main" and "merging" in agent.behavior_model:
                    agent.set_behavior_model("idm_mobil_lane_change_safe")
                if matched_corridor.type == "exit" and "exit" in agent.behavior_model:
                    agent.set_behavior_model("idm")
                self.matched_agents[i] = MatchedAgent(agent, matched_corridor.id)
        for i in list(self.agents.keys()):
            if i not in self.matched_agents.keys():
                self.removed_agents[i] = self.agents.pop(i)
        for i in self.map.corridors.keys():
            self.matched_agents_sorted_by_arc_length[i] = self.agents_in_corridor_by_arc_length(i)

    def corridor_for_agent(self, idx):
        if idx not in self.agents.keys():
            print("Agent with id {} is not in agent list".format(idx))
            return None
        if idx not in self.matched_agents.keys():
            print("Agent with id {} is not matched on any corridor".format(idx))
            return None
        corridor_id = self.matched_agents[idx].corridor_id
        return self.map.corridors[corridor_id]

    def dis_to_lane_border(self, idx, direction):
        corridor = self.corridor_for_agent(idx)
        return corridor.distance_to_border(self.matched_agents[idx].agent.hull, direction)

    def limit_of_vehicles(self):
        xmin, xmax, ymin, ymax = 1e10, -1e10, 1e10, -1e10
        for i, agent in self.agents.items():
            xmin = min(agent.x, xmin)
            xmax = max(agent.x, xmax)
            ymin = min(agent.y, ymin)
            ymax = max(agent.y, ymax)
        return xmin - 30, xmax + 30, ymin - 10, ymax + 10

    def limit_of_maps(self):
        xmin, xmax, ymin, ymax = 1e10, -1e10, 1e10, -1e10
        for i, c in self.map.corridors.items():
            xmin = min(c.x_min, xmin)
            xmax = max(c.x_max, xmax)
            ymin = min(c.y_min, ymin)
            ymax = max(c.y_max, ymax)
        return xmin, xmax, ymin, ymax

    def agents_in_corridor_by_arc_length(self, corridor_id):
        agents_by_arc_length = []  # [[dis, agent], ...] sorted by distance
        center = self.map.corridors[corridor_id].centerLine
        lane_agents = [a.agent for _, a in self.matched_agents.items() if a.corridor_id == corridor_id]
        for obj in lane_agents:
            obj_arc_length = center.project(Point([obj.x, obj.y]))
            agents_by_arc_length.append([obj_arc_length, obj])
        agents_by_arc_length.sort(key=lambda x: x[0])
        return agents_by_arc_length

    def sorted_agents_on_corridor_within_range_of_vehicle(self, corridor_id, ego_id, max_l_dis):
        agents_by_arc_length = []  # [[dis, agent], ...] sorted by distance
        center = self.map.corridors[corridor_id].centerLine
        ego = self.agents[ego_id]
        ego_arc_length = center.project(Point([ego.x, ego.y]))
        lane_agents = [a.agent for _, a in self.matched_agents.items() if a.corridor_id == corridor_id]
        for obj in lane_agents:
            obj_arc_length = center.project(Point([obj.x, obj.y]))
            if abs(obj_arc_length - ego_arc_length) <= max_l_dis:
                agents_by_arc_length.append([obj_arc_length, obj])
        agents_by_arc_length.sort(key=lambda x: x[0])
        return [x[1] for x in agents_by_arc_length]

    def agent_to_idm_match(self, ego_id, obj_id):
        ego = self.agents[ego_id]
        obj = self.agents[obj_id]
        obj_center_line = self.map.corridors[self.matched_agents[obj_id].corridor_id].centerLine
        ego_arc = obj_center_line.project(Point([ego.x, ego.y]))
        obj_arc = obj_center_line.project(Point([obj.x, obj.y]))
        return IdmMatch(obj_arc - ego_arc, obj)

    def front_agent(self, idx):
        if idx not in self.agents.keys():
            print("Agent with id {} is not in agent list".format(idx))
            return None
        if idx not in self.matched_agents.keys():
            print("Agent with id {} is not matched on any corridor".format(idx))
            return None
        ego = self.agents[idx]
        corridor_id = self.matched_agents[idx].corridor_id
        center = self.map.corridors[corridor_id].centerLine
        ego_arc_length = center.project(Point([ego.x, ego.y]))
        agents_on_ego_lane = self.matched_agents_sorted_by_arc_length[corridor_id]
        for a in agents_on_ego_lane:
            if a[0] > ego_arc_length:
                return IdmMatch(a[0] - ego_arc_length, a[1])
        return None

    def following_agent(self, idx):
        if idx not in self.agents.keys():
            print("Agent with id {} is not in agent list".format(idx))
            return None
        if idx not in self.matched_agents.keys():
            print("Agent with id {} is not matched on any corridor".format(idx))
            return None
        ego = self.agents[idx]
        corridor_id = self.matched_agents[idx].corridor_id
        center = self.map.corridors[corridor_id].centerLine
        ego_arc_length = center.project(Point([ego.x, ego.y]))
        agents_on_ego_lane = reversed(self.matched_agents_sorted_by_arc_length[corridor_id])
        for a in agents_on_ego_lane:
            if a[0] < ego_arc_length:
                return IdmMatch(a[0] - ego_arc_length, a[1])
        return None

    def neighbor_around_agent(self, idx):
        left_ff, left_f, left_b, left_bb = None, None, None, None
        right_ff, right_f, right_b, right_bb = None, None, None, None
        if idx not in self.agents.keys():
            print("Agent with id {} is not in agent list".format(idx))
            return Neighbor(left_ff, left_f, left_b, left_bb, None, None, right_ff, right_f, right_b, right_bb)
        if idx not in self.matched_agents.keys():
            print("Agent with id {} is not matched on any corridor".format(idx))
            return Neighbor(left_ff, left_f, left_b, left_bb, None, None, right_ff, right_f, right_b, right_bb)
        ego = self.agents[idx]
        corridor_id = self.matched_agents[idx].corridor_id
        left_corridor_id = self.map.corridors[corridor_id].left_id
        right_corridor_id = self.map.corridors[corridor_id].right_id

        if left_corridor_id is not None:
            left_agents_by_arc = self.matched_agents_sorted_by_arc_length[left_corridor_id]
            left_center = self.map.corridors[left_corridor_id].centerExtended
            ego_arc_on_left = left_center.project(Point([ego.x, ego.y]))
            for a in left_agents_by_arc:
                if not left_f and a[0] >= ego_arc_on_left:
                    left_f = IdmMatch(a[0] - ego_arc_on_left, a[1])
                    continue
                if left_f and a[0] > left_f.dis + ego_arc_on_left:
                    left_ff = IdmMatch(a[0] - ego_arc_on_left, a[1])
                    break
            for a in reversed(left_agents_by_arc):
                if not left_b and a[0] < ego_arc_on_left:
                    left_b = IdmMatch(a[0] - ego_arc_on_left, a[1])
                    continue
                if left_b and a[0] < left_b.dis + ego_arc_on_left:
                    left_bb = IdmMatch(a[0] - ego_arc_on_left, a[1])
                    break
        if right_corridor_id is not None:
            right_agents_by_arc = self.matched_agents_sorted_by_arc_length[right_corridor_id]
            right_center = self.map.corridors[right_corridor_id].centerExtended
            ego_arc_on_right = right_center.project(Point([ego.x, ego.y]))
            for a in right_agents_by_arc:
                if not right_f and a[0] >= ego_arc_on_right:
                    right_f = IdmMatch(a[0] - ego_arc_on_right, a[1])
                    continue
                if right_f and a[0] > right_f.dis + ego_arc_on_right:
                    right_ff = IdmMatch(a[0] - ego_arc_on_right, a[1])
                    break
            for a in reversed(right_agents_by_arc):
                if not right_b and a[0] < ego_arc_on_right:
                    right_b = IdmMatch(a[0] - ego_arc_on_right, a[1])
                    continue
                if right_b and a[0] < right_b.dis + ego_arc_on_right:
                    right_bb = IdmMatch(a[0] - ego_arc_on_right, a[1])
                    break
        return Neighbor(left_ff, left_f, left_b, left_bb, self.front_agent(idx), self.following_agent(idx),
                        right_ff, right_f, right_b, right_bb)

    def all_finish_merging(self):
        for _, agent in self.agents.items():
            if "merging_flag" in agent.__dict__:
                if not agent.merging_flag.finish_merging:
                    return False
        return True

    def all_finish_exit(self):
        for _, agent in self.agents.items():
            if "exit" in agent.behavior_model:
                if self.map.corridors[agent.corridor_history[-1]].type != "exit":
                    return False
        return True

    def agents_once_on_merging_lane(self):
        agents = []
        for _, agent in self.agents.items():
            if "merging_flag" in agent.__dict__:
                agents.append(agent)
        return agents

    def agents_never_on_merging_lane(self):
        agents = []
        for _, agent in self.agents.items():
            if "merging_flag" not in agent.__dict__:
                agents.append(agent)
        return agents

    def average_normalized_acc_agents(self, agents):
        sum_average_normalized_acc_over_objects = 0
        for a in agents:
            average_acc = sum(a.acceleration_history) / len(a.acceleration_history)
            average_normalized_acc = (average_acc - a.idm_param.acc_min) / (a.idm_param.acc_max - a.idm_param.acc_min)
            sum_average_normalized_acc_over_objects += average_normalized_acc
        return sum_average_normalized_acc_over_objects / max(len(agents), 1)

    def set_flag(self, agent):
        # finish merging, time for finishing merging, once fallback
        for b in agent.behavior_model_history:
            if "merging" in b:
                if not agent.merging_flag.flag_freeze:
                    corridor_id = self.matched_agents[agent.id].corridor_id
                    if self.map.corridors[corridor_id].type == "main":
                        agent.merging_flag.finish_merging = True
                        agent.merging_flag.t_finish_merging = self.sim_time
                        agent.merging_flag.flag_freeze = True
                    if self.map.corridors[corridor_id].type == "merging":
                        if agent.v <= 2 and agent.had_harsh_brake(): # and d_to_merging_end < 20
                            agent.merging_flag.once_fallback = True
                break
        for b in agent.behavior_model_history:
            if "exit" in b:
                if not agent.exit_flag.flag_freeze:
                    corridor_id = self.matched_agents[agent.id].corridor_id
                    if self.map.corridors[corridor_id].type == "exit":
                        agent.exit_flag.finish_exit = True
                        agent.exit_flag.t_finish_exit = self.sim_time
                        agent.exit_flag.flag_freeze = True
                    if self.map.corridors[corridor_id].type == "main":
                        if agent.had_harsh_brake():
                            agent.merging_flag.once_fallback = True
                break

    def step(self, dt):
        observed_env = self
        for _, a in self.agents.items():
            act = a.plan(observed_env)
            a.step(act, dt)
            self.set_flag(a)
        self.match_agents_on_corridor()
        self.sim_time += dt


n, k = map(int, input().split(' '))
arr = [[0, 0]]
for i in range(n):
    arr.append(list(map(int, input().split(' '))))

def solution(k, arr):
    answer = 0
    dp = []
    for _ in range(len(arr)):
        dp.append([])
    for i in range(len(arr)):
        dp[i] = [0] * (k + 1)

    for i in range(1, len(arr)):
        for j in range(1, k + 1):
            if j < arr[i][0]:
                dp[i][j] = dp[i - 1][j]
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i - 1]
                               [j - arr[i][0]] + arr[i][1])
    return dp

print(solution(k, arr))


import json
from dash import html
from dash.dependencies import Input, Output

import dash_datatables as ddt
import dash_html_components as html

import pandas as pd

from app import app

# https://dash.plot.ly/datatable

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

# Blank column for checkboxes

df.insert(0, '', '')

columns = [{"title": i, "data": i} for i in df.columns]

def solar_table():
    return ddt.DashDatatables(
        id='solar',
        columns=columns,
        data=df.to_dict('records'),
        width="100%",
        order=[2, 'asc'],

        # Following is needed for checkbox based row selection
        # https://datatables.net/extensions/select/examples/initialisation/checkbox

        column_defs = [ {
            "width": "5%", "targets": 0 ,
            'orderable': False,
            'className': 'select-checkbox',
            }],

        select = {
            'style':    'os',
            'selector': 'td:first-child'
            },
    )

layout = html.Div([
    html.Div([
        html.Div([], className="col-md-1"),
        html.Div([
            html.H2('US Solar Capacity'),
            html.Br(),
            html.Div(solar_table(), id='table-container'),
            html.Div(id='solar#output')
        ], className="col-md-10"),
        html.Div([], className="col-md-1")
    ], className='row')
], className="container-fluid")

@app.callback(Output('solar#output', 'children'), [Input('solar', 'table_event')])
def display_output(value):
    selected = None

    if value and value['action'] == 'selectItems':
        selected = value['indexes']
        selected = format(json.dumps(selected))

    print('Select rows: {}'.format(selected))
    return 'Select rows: {}'.format(selected)


"""
******************************************************************************
* Purpose: Take in three arguments P, Y, and R from user and calculates
           the monthly payments.
* @author:  Krishna Murthy A V
* @version: 1.2
* @since:   28-2-2019
*
******************************************************************************
"""

from Utilities.utility import FunctionalCode

mp = FunctionalCode

class MonthlyPayment:
    try:
        print("Enter P value")
        p = int(input())
        while p < 0:
            p = int(input("Enter valid number"))

        print("Enter Y value")
        y= int(input())
        while y < 0:
            y = int(input("Enter valid number"))

        print("Enter R value")
        r = int(input())
        while r < 0:
            r = int(input("Enter valid number"))

        mp.MonthPay(p,y,r)

    except RuntimeError:
        print("Error OCcured")


from dynaconf import Dynaconf, Validator

settings = Dynaconf(
    envvar_prefix=False,
    merge_enabled=True,
    load_dotenv=True,
    environments=True,
    validators=[
        Validator("DB_CONNECTION_STRING", must_exist=True),
        Validator("ENVIRONMENT", is_type_of=str, default="production"),
        Validator("HOSTNAME", is_type_of=str, default=""),
        Validator("ACCESS_LOG", is_type_of=bool, default=False),
        Validator("PORT", is_type_of=int, default=8080),
        Validator("HOST", is_type_of=str, default="0.0.0.0"),
        Validator("LOGGING_LEVEL", is_type_of=str, default="INFO"),
    ],
)

settings.INSTANCE = settings.HOSTNAME
settings.DEBUG = settings.ENVIRONMENT == "development"

settings.LOGGING = {
    "version": 1,
    "formatters": {
        "json": {
            "class": "pythonjsonlogger.jsonlogger.JsonFormatter",
            "format": "%(asctime)-15s %(levelname)s %(message)s",
        }
    },
    "filters": {
        "container": {
            "()": "project.libs.monitoring.logging.filters.Container",
            "environment": settings.ENVIRONMENT,
            "instance": settings.INSTANCE,
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "json",
            "filters": ["container"],
        },
    },
    "loggers": {
        "lore": {
            "level": settings.LOGGING_LEVEL,
            "handlers": ["console"],
        },
        "alembic": {
            "level": "INFO",
            "qualname": "alembic",
            "handlers": ["console"],
        },
        "sqlalchemy": {
            "level": "WARN",
            "qualname": "sqlalchemy.engine",
            "handlers": ["console"],
        },
    },
}


from glob import glob
from setuptools import setup
from pybind11.setup_helpers import Pybind11Extension, build_ext

ext_modules = [
    Pybind11Extension(
        "lazyk",
        sorted(glob("src/*.cpp"))
    ),
]

setup(
    name="lazyk",
    version="0.1.0",
    author="Arthur Hemmer",
    author_email="arthur.hemmer@shift-technology.com",
    description="A Python library with a C++ implementation of the lazy-k algorithm",
    ext_modules=ext_modules,
    cmdclass={"build_ext": build_ext},
    zip_safe=False,
)


import math

class Geodistance:

    def __init__(self):
        return

    def distance(self, lat1, lon1, lat2, lon2):

        # distance between latitudes
        # and longitudes
        dLat = (lat2 - lat1) * math.pi / 180.0
        dLon = (lon2 - lon1) * math.pi / 180.0

        # convert to radians
        lat1 = (lat1) * math.pi / 180.0
        lat2 = (lat2) * math.pi / 180.0

        # apply haversine formulae
        a = (pow(math.sin(dLat / 2), 2) +
            pow(math.sin(dLon / 2), 2) *
                math.cos(lat1) * math.cos(lat2))
        rad = 6371
        c = 2 * math.asin(math.sqrt(a))
        return int(rad * c)


import json
import collections
import plac
import sklearn_crfsuite
from sklearn.externals import joblib
from sklearn.metrics import f1_score

# only for word vector features
import spacy

def load_data(test_type='test'):
    path = '../data/kvret/preprocessed/'

    with open(path + 'fold_train.json') as json_file:
        train = json.load(json_file)

    if test_type == 'finaltest':
        test_file = 'final_test.json'
    else:
        test_file = 'fold_test.json'

    with open(path + test_file) as json_file:
        test = json.load(json_file)

    return train, test

def prepare_data(dataset, features='words', nlp=None):
    # get the single sequence of words and intent (duplicating by words in the sentence)
    # words will store all the [words] concatenated
    # intents is the list of (duplicated by the number of words for each sentence) intents
    # slots is the list of annotate IOB
    # bounds is a list of integer containing the indexes for the original sentence split
    sessions = dataset['data']
    result = {'words': [], 'intents':[], 'slots': [], 'bounds':[]}
    last_bound = 0
    for s in sessions:
        for m in s:
            if m['turn'] == 'b':
                pass # agent turn are not considered
            if m['turn'] == 'u' and m['length']:
                if features == 'words':
                    # only consider the lowercase word as feature
                    word_features = [{'w': w.lower()} for w in m['words']]
                elif features == 'word_vectors':
                    # consider the word vector as features
                    # rebuild the original sentence
                    sentence = ' '.join(m['words']).replace(' \'', '\'')
                    doc = nlp(sentence)
                    if len(doc) != m['length']:
                        # this should never happen, tokenization problem
                        print('different length: {} doc {} length. Sentence: {}'.format(len(doc), m['length'], sentence))
                        m['length'] = len(doc)
                    word_features = []
                    for w in doc:
                        word_vector = w.vector.tolist()
                        # consider each vector component
                        word_features.append({'v{}'.format(key) : value for (key, value) in enumerate(word_vector)})
                result['words'] += word_features
                # duplicate the intent value for each word in the sentence
                result['intents'] += [m['intent']] * m['length']
                result['slots'] += m['slots']
                # compute the slice indexes on the resulting array for this sentence
                new_bound = last_bound + m['length']
                result['bounds'] += [(last_bound, new_bound)]
                last_bound = new_bound

    print('{} sentences {} words {} intents'.format(len(result['bounds']), len(result['words']), len(result['intents'])))
    # CRF suite expects a list of list of dicts of features: a list of features for each document
    # here we are considering a single document composed of all the sentences concatenated
    result['words'] = [result['words']]
    result['intents'] = [result['intents']]

    return result

def intents_collapse_from_word_level(intents, bounds, mode='all'):
    """Collapses the intent values putting together the output labels at word level,
    emitting a single value for each sentence according to its bounds.

    'mode' can be 'all' meaning that all the values must be unanimous from the words,
    or can be 'majority' to perform a majority vote """
    # flat the list of list wanted by CRF suite
    flattened_intents = [item for sublist in intents for item in sublist]
    # split the intents in groups corresponding to the sentences
    intent_groups = [flattened_intents[start:end] for start, end in bounds]
    # result is one intent for each group
    results = []
    for intent_group in intent_groups:
        if mode == 'all':
            # all should be the same
            if all(intent_group[0] == item for item in intent_group):
                # agreeing
                results.append(intent_group[0])
            else:
                # conflicting values
                results.append('<UNK>')
        elif mode == 'majority':
            # count the votes
            counter = collections.Counter(intent_group)
            # pick the winner
            winner, _ = counter.most_common(1)[0]
            results.append(winner)
        else:
            raise ValueError('unknown mode ' + mode)
    return results

def train_crf(inputs, outputs):
    """Train the CRF, inputs and outputs must be already preprocessed"""
    x_train = inputs
    y_train = outputs

    crf = sklearn_crfsuite.CRF(
        algorithm='ap',
        max_iterations=100,
        all_possible_transitions=True,
        verbose=True
    )

    crf.fit(x_train, y_train)
    print('trained')
    return crf

def test_crf(model, inputs):
    """Obtain the predicted values on the CRF, inputs must be preprocessed"""
    outputs = model.predict(inputs)
    #print(outputs)
    return outputs

def train_and_eval(features='words', test_type='test'):
    if features == 'word_vectors':
        nlp = spacy.load('en_vectors_web_lg')
    else:
        nlp = None

    train_raw, test_raw = load_data(test_type)
    train = prepare_data(train_raw, features, nlp)
    test = prepare_data(test_raw, features, nlp)

    x_train = train['words']
    y_train = train['intents']

    crf = train_crf(x_train, y_train)

    x_test = test['words']
    y_test = test['intents']
    bounds_test = test['bounds']
    y_test_collapsed = intents_collapse_from_word_level(y_test, bounds_test)

    y_predict = test_crf(crf, x_test)

    f1_word_level = f1_score([item for sublist in y_test for item in sublist], [item for sublist in y_predict for item in sublist], average='micro')
    print(f1_word_level)

    y_predict_collapsed_all = intents_collapse_from_word_level(y_predict, bounds_test)
    y_predict_collapsed_majority = intents_collapse_from_word_level(y_predict, bounds_test, 'majority')
    f1_intent_all = f1_score(y_test_collapsed, y_predict_collapsed_all, average='micro')
    f1_intent_majority = f1_score(y_test_collapsed, y_predict_collapsed_majority, average='micro')
    print(f1_intent_all, f1_intent_majority)

    with open('results/' + test_type + '_crf_' + features + '.json', 'w') as out_file:
        json.dump({'f1_intent_all_agree': f1_intent_all, 'f1_intent_majority': f1_intent_majority}, out_file)

    return f1_word_level, f1_intent_all, f1_intent_majority

def main(test_type='test'):
    d,e,f = train_and_eval('word_vectors', test_type)
    a,b,c = train_and_eval('words', test_type)
    print('Basic CRF results:')
    print('considering words, f1 on sequence', a, 'f1 on all_agree intent', b, 'f1 on majority_vote intent', c)
    print('considering word vectors, f1 on sequence', d, 'f1 on all_agree intent', e, 'f1 on majority_vote intent', f)

if __name__ == '__main__':
    plac.call(main)


import paho.mqtt.client as mqtt
import time
import json
import datetime
from connection import *

# Callback function - executed when the program successfully connects to the broker
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

#Callback function - executed when the program gracefully disconnects from the broker
def on_disconnect(client, userdata, rc):
    print("Disconnected with result code "+str(rc))

#Callback function - executed whenever a message is published to the topics that
#this program is subscribed to
def on_message(client, userdata, msg):
    # print(msg.topic, str(msg.payload), "retain", msg.retain, "qos", msg.qos, str(userdata) )
    data = msg.payload.decode('utf-8')
    data = json.loads(data)
    device_id = data["device_id"]
    location = data["location"]
    temperature = data["temperature"]
    humidity = data["humidity"]
    pressure = data["pressure"]
    # reading_time = data["reading_time"]
    reading_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    create_device_data(device_id, location, temperature, humidity, pressure, reading_time)

#Defining an MQTT client object
client = mqtt.Client()

#Setting callback functions for various client operations
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect

#Reading the configuration file
f=open("config-sub.json")
config = json.loads(f.read())
f.close()

# Initialising devices from the config.json file and assigning device_ids to each device
device_config = []
for device in config['devices']:
    # dev = {}
    # dev['device_id'] = device['device_id']
    # dev['publish_frequency'] = device['publish_frequency']
    # dev['location'] = device['location']
    # dev['temperature_std_val'] = device['temperature_std_val']
    # dev['humidity_std_val'] = device['humidity_std_val']
    # dev['pressure_std_val'] = device['pressure_std_val']
    # dev['publish_topic'] = device['publish_topic']
    # device_config.append(dev)
    # Create the each device in the devices table
    create_device(device['device_id'], device['description'])

#Connecting to broker
client.connect(host=config["broker_host"], port=config["broker_port"], keepalive=60)

'''
Start the MQTT client non-blocking loop to listen the broker for messages
in subscribed topics and other operations for which the callback functions
are defined
'''
client.loop_start()

for device in config['devices']:
    # if clock % device['publish_frequency'] == 0:
        print("Subscribed to topic " + device["publish_topic"])
        #Subscribe to the topic
        client.subscribe(device["publish_topic"])

while True:
    try:
        # Iterating through the items in device configuration dictionary, every second
        time.sleep(1)

    #Disconnect the client from MQTT broker and stop the loop gracefully at
    # Keyboard interrupt (Ctrl+C)
    except KeyboardInterrupt:
        client.disconnect()
        client.loop_stop()
        break


#!/usr/bin/env python3

'''
These are some of the common commands to be used with i3 keybindings and polybar displays
'''

import argparse
import math
import os
import sys
from requests import Request
from json import loads, dumps

VOLUME_CACHE_FILENAME = "VOLUME_CACHE"

def kiwi_main(kiwi):
	pass

	parser = argparse.ArgumentParser(description=kiwi.module_desc,
									 epilog=__doc__,
									 formatter_class=argparse.RawDescriptionHelpFormatter)

	subparsers = parser.add_subparsers(dest="action")
	parser.add_argument("-q", "--quiet", help="do not display any output", action="store_true")

	# system volume control
	volume_subparser = subparsers.add_parser("volume")
	volume_subparser.add_argument("-I", "--intervals", type=int, metavar="AMOUNT", help="intervals of the entire volume range")
	volume_subparser.add_argument("-s", "--sink", type=int, metavar="INDEX", help="target sink index", default=0)
	volume_subparser.add_argument("--range-symbol", type=str, metavar="SYMBOL", help="symbol that represents a unit of range", default="─")
	volume_subparser.add_argument("--value-symbol", type=str, metavar="SYMBOL", help="symbol that represents the current value of range", default="|")
	volume_control_group = volume_subparser.add_mutually_exclusive_group()
	volume_control_group.add_argument("-t", "--toggle-mute", action="store_true", help="toggle sound mute")
	volume_control_group.add_argument("-m", "--mute", action="store_true", help="mute sound")
	volume_control_group.add_argument("-u", "--unmute", action="store_true", help="unmute sound")
	volume_control_group.add_argument("-i", "--increment", type=int, metavar="PERCENTAGE", help="increment volume by specified percentage", choices=range(1, 100))
	volume_control_group.add_argument("-d", "--decrement", type=int, metavar="PERCENTAGE", help="decrement volume by specified percentage", choices=range(1, 100))

	# system information
	info_subparser = subparsers.add_parser("info")
	info_subparser.add_argument("-j", "--jsonpath", type=str, metavar="PATH", help="fetch specific fields from result")
	info_subparsers = info_subparser.add_subparsers(dest="info_action")

	# system information
	for item in [ "wan", "lan", "ram", "swap", "cpu", "net", "temp", "fans", "battery" ]:
		info_subparsers.add_parser(item)

	args = parser.parse_args()

	# write stdout to devnull if quiet
	stdout = sys.stdout
	devnull = open(os.devnull, 'w')
	if args.quiet:
		sys.stdout = devnull

	try:

		# system volume control
		if args.action == "volume":

			import pulsectl

			# must provide a range to print it out
			if not args.quiet:
				if not args.intervals:
					parser.error("must provide interval range if not using -q")
			else:
				args.intervals = 0

			# pulseaudio client
			with pulsectl.Pulse('client') as pulse:

				# get target sink
				target_sink = pulse.sink_list()[args.sink]

				# base off of the left channel volume
				current_volume = target_sink.volume.values[0]

				# if volume should be altered
				if args.increment or args.decrement or args.toggle_mute or args.mute or args.unmute:

					# if volume should be incremented
					if args.increment or args.decrement:

						# decide on a value
						increment_value = args.increment if args.increment else -args.decrement

						# keep target volume in range
						target_volume = current_volume + (increment_value / 100.0)
						if target_volume > 1 or target_volume < 0:
							target_volume = abs(target_volume) - (abs(target_volume) % 1)

					# mute/unmute
					else:

						# action related functions
						def mute(current_volume):
							with open(VOLUME_CACHE_FILENAME, 'w') as VOLUME_CACHE:
								VOLUME_CACHE.write(str(current_volume))
							return 0

						def unmute():
							if os.path.isfile(VOLUME_CACHE_FILENAME):
								with open(VOLUME_CACHE_FILENAME, 'r') as VOLUME_CACHE:
									return float(VOLUME_CACHE.read())
							return 1.0

						def toggle():

							if current_volume == 0:
								return unmute()
							else:
								return mute(current_volume)

						# act based on parameter
						if args.toggle_mute:
							target_volume = toggle()
						elif args.mute:
							target_volume = mute(current_volume)
						else:
							target_volume = unmute()

					# set result volume
					pulse.volume_set_all_chans(target_sink, target_volume)

					# update current volume value
					current_volume = target_volume

				# print volume state
				volume_range = args.range_symbol * args.intervals
				value_index = max(0, math.floor(args.intervals * current_volume) - 1)
				print(volume_range[:value_index] + args.value_symbol + volume_range[value_index + 1:])

		# networking information
		elif args.action == "info":

			import psutil
			import jsonpath

			# get appropriate info
			info_json = {
				"wan": lambda: loads(kiwi.request(Request('GET', '/info/wan')).text),
				"lan": lambda: psutil.net_if_addrs(),
				"ram": lambda: psutil.virtual_memory()._asdict(),
				"swap": lambda: psutil.swap_memory()._asdict(),
				"cpu": lambda: {
					'system-wide': psutil.cpu_percent(interval=1),
					'per-cpu': psutil.cpu_percent(percpu=True),
					'stats': psutil.cpu_stats()._asdict(),
					'freq': psutil.cpu_freq()._asdict()
				},
				"net": lambda: psutil.net_io_counters()._asdict(),
				"temp": lambda: psutil.sensors_temperatures(),
				"fans": lambda: psutil.sensors_fans(),
				"battery": lambda: psutil.sensors_battery()._asdict(),
			}.get(args.info_action, lambda: None)()
			info_json = info_json if info_json else {}

			# print query if specified, otherwise print everything
			if args.jsonpath:
				result = jsonpath.jsonpath(loads(dumps(info_json)), args.jsonpath)

				# format result
				if result:
					if len(result) == 1:
						result = result[0]
				else:
					result = ""

				print(result, end="")
			else:
				print(dumps(info_json, indent=4), end="")

	finally:
		sys.stdout = stdout


# from PyQt5.QtWidgets import QWidget, QLabel, QSlider
# from PyQt5.QtCore import Qt
from PySide6.QtWidgets import QWidget, QLabel, QSlider
from PySide6.QtCore import Qt
from .constants import WIDTH, HEIGHT

LABEL_MARGIN = WIDTH // 25
LABEL_CENTERING_OFFSET = HEIGHT // 100

class Slider(object):
    def __init__(self, label: str, parent: QWidget, pos_x: int, pos_y: int):
        self._x = pos_x
        self._y = pos_y
        self._value: float = 0.0
        self._update_callback = None
        self._mouseup_callback = None

        self._init_label(label, parent, pos_x, pos_y + LABEL_CENTERING_OFFSET)
        self._init_slider(parent, pos_x + LABEL_MARGIN, pos_y)
        self._init_value_label(
            parent, WIDTH - WIDTH // 10, pos_y + LABEL_CENTERING_OFFSET
        )

    def _init_slider(self, parent: QWidget, pos_x: int, pos_y: int):
        slider = QSlider(Qt.Orientation.Horizontal, parent)
        slider.setGeometry(pos_x, pos_y, WIDTH - WIDTH // 3, 30)
        slider.setRange(0, 100)
        slider.show()
        slider.valueChanged.connect(lambda: self._update_value(self._slider.value()))
        self._slider = slider

    def _init_label(self, text: str, parent: QWidget, pos_x: int, pos_y: int):
        label = QLabel(parent)
        label.setText(text)
        label.move(pos_x, pos_y)
        label.show()
        self._text_label = label

    def _init_value_label(self, parent: QWidget, pos_x: int, pos_y: int):
        label = QLabel(parent)
        label.setText("0.0")
        label.move(pos_x, pos_y)
        label.show()
        self._value_label = label

    def _update_value(self, value: float):
        self._value = value
        self._value_label.setText(str(self.value))
        if self._update_callback is not None:
            self._update_callback(value)

    def register_update_callback(self, func):
        self._update_callback = func

    def register_mouseup_callback(self, func):
        self._slider.sliderReleased.connect(lambda: func(self._slider.value()))

    @property
    def value(self) -> float:
        return self._value / 10


# This problem was asked by Amazon.
#
# Huffman coding is a method of encoding characters
# based on their frequency. Each letter is assigned
# a variable-length binary string, such as 0101 or 111110,
# where shorter lengths correspond to more common letters.
# To accomplish this, a binary tree is built such that the
# path from the root to any leaf uniquely maps to a character.
# When traversing the path, descending to a left child corresponds
# to a 0 in the prefix, while descending right corresponds to 1.
#
# Here is an example tree (note that only the leaf nodes have letters):
#
#         *
#       /   \
#     *       *
#    / \     / \
#   *   a   t   *
#  /             \
# c               s
#
# With this encoding, cats would be represented as 0000110111.

class Node:
    def __init__(self,v,name):
        self.v = v
        self.name = name
        self.r = None
        self.l = None

def trie(words):
    trie = {}
    for word in words:
        current = trie
        for letter in word:
            current = current.setdefault(letter, {})
        current['#']='#'
    return trie

def sort_letters(word):
    ans = []
    for letter in word:
        if letter not in ans:
            ans.append((letter,word.count(letter)))
    return sorted(ans, key=lambda x:x[1], reverse=False)

def make_tree(word):
    height = len(word)
    print(height)

word = sort_letters('cat')
print(word)
print(make_tree(word))


"""Types package of ispec.

Contains handy type definitions.
"""

from collections.abc import Callable
from typing import Any

__all__ = ["AnyCallable", "Class", "Metaclass", "Method"]

Class = type[object]
Metaclass = type[type]

AnyCallable = Callable[..., Any]
Method = AnyCallable


from rest_framework.serializers import ModelSerializer

from documentos_pessoais_imoveis.api.serializers import DocumentosPessoaisImoveisSerializer
from documentos_pessoais_imoveis.models import DocumentosPessoaisImoveis
from endereco.api.serializers import EnderecoSerializer
from endereco.models import Endereco
from imovel.api.serializers import ImovelSerializer
from imovel.models import Imovel
from vistoria_imoveis.models import VistoriaImoveis

class VistoriaImoveisSerializer(ModelSerializer):

    documentosPessoaisImoveis = DocumentosPessoaisImoveisSerializer()
    imovel = ImovelSerializer()
    endereco = EnderecoSerializer()

    class Meta:
        model = VistoriaImoveis
        fields = ('id', 'nome', 'dataRegistro', 'documentosPessoaisImoveis', 'endereco', 'imovel')

    def create(self, validated_data):

        documentosPessoaisImoveis = validated_data['documentosPessoaisImoveis']
        del validated_data['documentosPessoaisImoveis']

        endereco = validated_data['endereco']
        del validated_data['endereco']

        imovel = validated_data['imovel']
        del validated_data['imovel']

        imov = Imovel.objects.create(**imovel)
        imov.save()

        docPessoaisImoveis = DocumentosPessoaisImoveis.objects.create(**documentosPessoaisImoveis)
        docPessoaisImoveis.save()

        end = Endereco.objects.create(**endereco)
        end.save()

        vistoriaImovel = VistoriaImoveis.objects.create(**validated_data)

        vistoriaImovel.documentosPessoaisImoveis = docPessoaisImoveis
        vistoriaImovel.imovel = imov
        vistoriaImovel.endereco = end

        vistoriaImovel.save()

        return vistoriaImovel


# read file as string

with open("input.txt") as file:
    data = file.read()

leng = 14
for i in range(len(data) - leng):
    part = data[i: i + leng]
    if not any(part.count(l) > 1 for l in part):
        print(i + leng)
        print(part)
        break


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 17-9-21 下午6:39
# @Author  : MaybeShewill-CV
# @Site    : https://github.com/MaybeShewill-CV/CRNN_Tensorflow
# @File    : crnn_net.py
# @IDE: PyCharm Community Edition
"""
Implement the crnn model mentioned in An End-to-End Trainable Neural Network for Image-based Sequence
Recognition and Its Application to Scene Text Recognition paper
"""
import time

import numpy as np
import tensorflow as tf
from tensorflow_core.contrib import rnn

from crnn_model import cnn_basenet
from crnn_model import loss
from local_utils import config_utils

class CrnnNet(cnn_basenet.CNNBaseModel):
    """

    """
    def __init__(self, phase, cfg):
        """

        :param phase:
        :param cfg:
        :return:
        """
        super(CrnnNet, self).__init__()
        self._phase = phase
        self._is_training = self._is_net_for_training()
        self._cfg = cfg

        self._hidden_nums = self._cfg.MODEL.CRNN.HIDDEN_UNITS
        self._layers_nums = self._cfg.MODEL.CRNN.HIDDEN_LAYERS
        self._num_classes = self._cfg.DATASET.NUM_CLASSES
        self._seq_length = self._cfg.MODEL.CRNN.SEQ_LENGTH

        self._loss_type = self._cfg.SOLVER.LOSS_TYPE.lower()
        self._loss_func = getattr(loss, '{:s}_loss'.format(self._loss_type))
        self._class_nums = self._cfg.DATASET.NUM_CLASSES
        self._weights_decay = self._cfg.SOLVER.WEIGHT_DECAY
        self._enable_dropout = self._cfg.TRAIN.DROPOUT.ENABLE
        if self._enable_dropout:
            self._dropout_keep_prob = self._cfg.TRAIN.DROPOUT.KEEP_PROB

    def _is_net_for_training(self):
        """
        if the net is used for training or not
        :return:
        """
        if isinstance(self._phase, tf.Tensor):
            phase = self._phase
        else:
            phase = tf.constant(self._phase, dtype=tf.string)
        return tf.equal(phase, tf.constant('train', dtype=tf.string))

    def _conv_stage(self, inputdata, out_dims, name):
        """
        Standard VGG convolutional stage: 2d conv, relu, and maxpool
        :param inputdata: 4D tensor batch x width x height x channels
        :param out_dims: number of output channels / filters
        :return: the maxpooled output of the stage
        """
        with tf.variable_scope(name_or_scope=name):
            conv = self.conv2d(
                inputdata=inputdata, out_channel=out_dims,
                kernel_size=3, stride=1, use_bias=True, name='conv'
            )
            bn = self.layerbn(
                inputdata=conv, is_training=self._is_training, name='bn'
            )
            relu = self.relu(
                inputdata=bn, name='relu'
            )
            max_pool = self.maxpooling(
                inputdata=relu, kernel_size=2, stride=2, name='max_pool'
            )
        return max_pool

    def _feature_sequence_extraction(self, inputdata, name):
        """
        Implements section 2.1 of the paper: "Feature Sequence Extraction"
        :param inputdata: eg. batch*32*100*3 NHWC format
        :param name:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):
            conv1 = self._conv_stage(
                inputdata=inputdata, out_dims=64, name='conv1'
            )
            conv2 = self._conv_stage(
                inputdata=conv1, out_dims=128, name='conv2'
            )
            conv3 = self.conv2d(
                inputdata=conv2, out_channel=256, kernel_size=3, stride=1, use_bias=False, name='conv3'
            )
            bn3 = self.layerbn(
                inputdata=conv3, is_training=self._is_training, name='bn3'
            )
            relu3 = self.relu(
                inputdata=bn3, name='relu3'
            )
            conv4 = self.conv2d(
                inputdata=relu3, out_channel=256, kernel_size=3, stride=1, use_bias=False, name='conv4'
            )
            bn4 = self.layerbn(
                inputdata=conv4, is_training=self._is_training, name='bn4'
            )
            relu4 = self.relu(
                inputdata=bn4, name='relu4')
            max_pool4 = self.maxpooling(
                inputdata=relu4, kernel_size=[2, 1], stride=[2, 1], padding='VALID', name='max_pool4'
            )
            conv5 = self.conv2d(
                inputdata=max_pool4, out_channel=512, kernel_size=3, stride=1, use_bias=False, name='conv5'
            )
            bn5 = self.layerbn(
                inputdata=conv5, is_training=self._is_training, name='bn5'
            )
            relu5 = self.relu(
                inputdata=bn5, name='bn5'
            )
            conv6 = self.conv2d(
                inputdata=relu5, out_channel=512, kernel_size=3, stride=1, use_bias=False, name='conv6'
            )
            bn6 = self.layerbn(
                inputdata=conv6, is_training=self._is_training, name='bn6'
            )
            relu6 = self.relu(
                inputdata=bn6, name='relu6'
            )
            max_pool6 = self.maxpooling(
                inputdata=relu6, kernel_size=[2, 1], stride=[2, 1], name='max_pool6'
            )
            conv7 = self.conv2d(
                inputdata=max_pool6, out_channel=512, kernel_size=2, stride=[2, 1], use_bias=False, name='conv7'
            )
            bn7 = self.layerbn(
                inputdata=conv7, is_training=self._is_training, name='bn7'
            )
            relu7 = self.relu(
                inputdata=bn7, name='bn7'
            )

        return relu7

    def _map_to_sequence(self, inputdata, name):
        """
        Implements the map to sequence part of the network.
        This is used to convert the CNN feature map to the sequence used in the stacked LSTM layers later on.
        Note that this determines the length of the sequences that the LSTM expects
        :param inputdata:
        :param name:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):
            shape = inputdata.get_shape().as_list()
            assert shape[1] == 1  # H of the feature map must equal to 1
            ret = self.squeeze(inputdata=inputdata, axis=1, name='squeeze')
        return ret

    def _sequence_label(self, inputdata, name):
        """
        Implements the sequence label part of the network
        :param inputdata:
        :param name:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):
            # construct stack lstm rcnn layer
            # forward lstm cell
            fw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for
                            nh in [self._hidden_nums] * self._layers_nums]
            # Backward direction cells
            bw_cell_list = [tf.nn.rnn_cell.LSTMCell(nh, forget_bias=1.0) for
                            nh in [self._hidden_nums] * self._layers_nums]

            stack_lstm_layer, _, _ = rnn.stack_bidirectional_dynamic_rnn(
                fw_cell_list, bw_cell_list, inputdata,
                dtype=tf.float32
            )
            # stack_lstm_layer, _, _ = tf.nn.bidirectional_dynamic_rnn(
            #     fw_cell_list, bw_cell_list, inputdata,
            #     dtype=tf.float32
            # )
            if self._enable_dropout:
                stack_lstm_layer = tf.cond(
                    self._is_training,
                    true_fn=lambda: self.dropout(
                        inputdata=stack_lstm_layer,
                        keep_prob=self._dropout_keep_prob,
                        name='sequence_dropout_train'
                    ),
                    false_fn=lambda: tf.identity(stack_lstm_layer, name='sequence_dropout_test')
                )
            shape = tf.shape(stack_lstm_layer)
            rnn_reshaped = tf.reshape(stack_lstm_layer, [shape[0] * shape[1], shape[2]])

            w = tf.get_variable(
                name='w',
                shape=[512, self._num_classes],
                initializer=tf.truncated_normal_initializer(stddev=0.02),
                trainable=True
            )

            # Doing the affine projection
            logits = tf.matmul(rnn_reshaped, w, name='logits')

            logits = tf.reshape(logits, [shape[0], shape[1], self._num_classes], name='logits_reshape')

            raw_pred = tf.argmax(tf.nn.softmax(logits), axis=2, name='raw_prediction')

            # Swap batch and batch axis
            rnn_out = tf.transpose(logits, [1, 0, 2], name='transpose_time_major')  # [width, batch, n_classes]

        return rnn_out, raw_pred

    def _build_net(self, input_tensor, name, reuse):
        """

        :param input_tensor:
        :param name:
        :param reuse:
        :return:
        """
        with tf.variable_scope(name_or_scope=name, reuse=reuse):
            # first apply the cnn feature extraction stage
            cnn_out = self._feature_sequence_extraction(
                inputdata=input_tensor, name='feature_extraction_module'
            )
            # second apply the map to sequence stage
            sequence = self._map_to_sequence(
                inputdata=cnn_out, name='map_to_sequence_module'
            )
            # third apply the sequence label stage
            net_out, raw_pred = self._sequence_label(
                inputdata=sequence, name='sequence_rnn_module'
            )

        return net_out

    def inference(self, input_tensor, name, reuse=False):
        """

        :param input_tensor:
        :param name:
        :param reuse:
        :return:
        """
        with tf.variable_scope(name_or_scope=name, reuse=reuse):
            logits = self._build_net(
                input_tensor=input_tensor,
                name='inference',
                reuse=reuse
            )

        return logits

    def compute_loss(self, input_tensor, label, name, reuse=False):
        """

        :param input_tensor:
        :param label:
        :param name:
        :param reuse:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):
            logits = self._build_net(
                input_tensor=input_tensor,
                name='inference',
                reuse=reuse
            )

            with tf.variable_scope('crnn_loss', reuse=reuse):
                ret = self._loss_func(
                    logits=logits,
                    labels=label,
                    seq_length=self._seq_length,
                    batch_size=input_tensor.get_shape().as_list()[0]
                )
        return ret

def get_model(phase, cfg):
    """

    :param phase:
    :param cfg:
    :return:
    """
    return CrnnNet(phase=phase, cfg=cfg)

def _stats_graph(graph):
    """

    :param graph:
    :return:
    """
    flops = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.float_operation())
    params = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())
    print('FLOPs: {}; Trainable params: {}'.format(flops.total_float_ops, params.total_parameters))

    return

def _inference_time_profile():
    """

    :return:
    """
    tf.reset_default_graph()
    cfg = config_utils.get_config(config_file_path='./config/synth_chinese_dataset.yaml')
    test_input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 32, 280, 3], name='test_input')
    test_label_tensor = tf.placeholder(dtype=tf.int32, shape=[10], name='test_label')
    test_label_tensor = tf.contrib.layers.dense_to_sparse(test_label_tensor)
    model = get_model(phase='train', cfg=cfg)
    test_result = model.compute_loss(
        input_tensor=test_input_tensor,
        label=test_label_tensor,
        name='CRNN',
        reuse=False
    )
    tmp_logits = model.inference(input_tensor=test_input_tensor, name='CRNN', reuse=True)
    print(test_result)
    print(tmp_logits)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        test_input = np.random.random((1, 32, 280, 3)).astype(np.float32)
        t_start = time.time()
        loop_times = 100
        for i in range(loop_times):
            _ = sess.run(tmp_logits, feed_dict={test_input_tensor: test_input})
        t_cost = time.time() - t_start
        print('Cost time: {:.5f}s'.format(t_cost / loop_times))
        print('Inference time: {:.5f} fps'.format(loop_times / t_cost))

    print('Complete')

def _model_profile():
    """

    :return:
    """
    tf.reset_default_graph()
    cfg = config_utils.get_config(config_file_path='./config/synth_chinese_dataset.yaml')
    test_input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 32, 280, 3], name='test_input')
    model = get_model(phase='test', cfg=cfg)
    _ = model.inference(input_tensor=test_input_tensor, name='CRNN', reuse=False)

    with tf.Session() as sess:
        _stats_graph(sess.graph)

    print('Complete')

if __name__ == '__main__':
    """
    main func
    """
    _model_profile()

    _inference_time_profile()


import pytest
from urdf_kit.edit_links import grab_elems_dict_by_joint_name
from urdf_kit.maths import get_X_JointChild, get_X_ParentJoint, get_X_CparentCchild
from urdf_kit.maths import write_origin, get_origin

from urdf_kit.misc import floatList_from_vec3String # for implementing the tests, which is not perfect.

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element

import numpy as np

def standalone_joint() -> Element:
    # TODO some parametrization...
    cfg = dict(
        origin_xyz="-0.2 0.123 0.200",
        origin_rpy="0 0.01 0.2",
        axis_xyz="0 0 1.0"
    )
    joint_elem = Element("joint",
        name="mybot/arm_j1",
        type="revolute"
    )

    child_elem_list = [
        Element("parent", name="mybot/base_link"),
        Element("child", name="mybot/arm_l1"),
        Element("origin", xyz=cfg['origin_xyz'], rpy=cfg['origin_rpy']),
        Element("axis", xyz=cfg['axis_xyz']),
        Element("limit", lower = "-1.2", upper = "0.6") # incomplete
    ]

    for child_elem in child_elem_list:
        joint_elem.append(child_elem)

    return joint_elem, cfg# TODO expected transforms, ...

def standalone_origin() -> Element:
    joint_elem = standalone_joint()[0]
    return joint_elem.find("origin")

@pytest.fixture(scope="function")
def standalone_joint_fixture():
    return standalone_joint()
# @pytest.fixture(scope="function")
# def standalone_origin_fixture():
#     return standalone_origin()

@pytest.mark.skip(reason="already some basic smoke test for `get_X_ParentJoint` which depends on `get_origin`")
def test_get_origin(standalone_joint_fixture):
    raise NotImplementedError()
    # TODO

def test_get_X_ParentJoint_NoCrash(standalone_joint_fixture):
    # For now, just want to see that at least it runs without crashing
    joint_elem = standalone_joint_fixture[0]
    # expected_X_ParentJoint = standalone_joint_fixture[1]
    res = get_X_ParentJoint(joint_elem)

def test_get_X_JointChild_NoCrash(standalone_joint_fixture):
    joint_elem = standalone_joint_fixture[0]
    res = get_X_JointChild(joint_elem, 0.2)

def test_get_X_Centersofmass(kuka_iiwa_joint4):
    test_input = kuka_iiwa_joint4 # the fixture, see `conftest.py`
    expected_res = test_input["X_CparentCchild"]
    urdf_root = test_input['urdf_root']
    joint_name = test_input['joint_name']

    # the API
    # result = get_X_CparentCchild(joint_elem, parent_elem, child_elem)
    result = get_X_CparentCchild(**grab_elems_dict_by_joint_name(urdf_root, joint_name))

    # really nice that the spatialmath library overloaded == (`__eq__`)
    # `==` means identity up to rounding error
    assert expected_res == result

def test_write_origin(standalone_joint_fixture):
    joint_elem = standalone_joint_fixture[0]
    cfg = standalone_joint_fixture[1]

    origin_elem = joint_elem.find("origin")
    # just made it up, cyclic check
    # shouldn't change the origin elem too much
    X = get_X_ParentJoint(joint_elem)
    write_origin(origin_elem, X) # shouldn't crash

    expected_rpy = np.array(floatList_from_vec3String(cfg["origin_rpy"]))
    new_rpy = np.array(floatList_from_vec3String(origin_elem.get("rpy")))
    np.testing.assert_allclose(new_rpy, expected_rpy, atol=1e-11)

    expected_xyz = np.array(floatList_from_vec3String(cfg["origin_xyz"]))
    new_xyz = np.array(floatList_from_vec3String(origin_elem.get("xyz")))
    np.testing.assert_allclose(new_xyz, expected_xyz)

def test_write_origin_catchInvalidInput(standalone_joint_fixture):
    joint_elem = standalone_joint_fixture[0]

    # preparing an invalid input
    not_really_origin_elem = joint_elem.find("axis")
    # some dummy SE3
    X = get_X_ParentJoint(joint_elem)
    with pytest.raises(ValueError) as e:
        write_origin(not_really_origin_elem, X)

if __name__ == "__main__":
    test_this = standalone_joint()[0]
    ET.indent(test_this)
    print(ET.tostring(test_this).decode('utf-8'))

    X1 = get_X_ParentJoint(test_this)
    X2 = get_X_JointChild(test_this, 0.2)
    print(X1)
    print(X2)
    print(X1@X2)

    origin_elem = test_this.find("origin")
    write_origin(origin_elem, X1) # cyclic test... which should be similar to how it was
    print(ET.tostring(test_this).decode('utf-8'))


def parse_references(self, markup):

        """ Returns a list of references found in the markup.

        References appear inline as <ref> footnotes,
        http:// external links, or {{cite}} citations.
        We replace it with (1)-style footnotes.
        Additional references data is gathered in
        parse_paragraph_references() when we parse paragraphs.

        References can also appear in image descriptions,
        tables and taxoboxes, so they might not always pop up in a paragraph.

        The plain() method finally replaces (1) by [1].

        """

        references = []

        # A Wikipedia reference note looks like:
        # <ref>In 1946, [[ENIAC]] consumed an estimated 174 kW.
        # By comparison, a typical personal computer may use around 400 W;
        # over four hundred times less. {{Ref harvard|kempf1961|Kempf 1961|a}}</ref>
        m = re.findall(self.re["reference"], markup)
        for reference in m:
            reference = re.sub("<ref> {0,1}cite", "<ref>{{cite", reference)
            if not reference.strip().startswith("[http://") and \
               not re.search("\{\{cite", reference):
                r = WikipediaReference()
                r.note = self.plain(re.sub("</{0,1}ref.*?>", "", reference))
                if r.note != "":
                    references.append(r)
                    p = " "+self.ref+"("+str(len(references))+")"
                    markup = markup.replace(reference, p, 1)
            else:
                # References containing a citation or url
                # are better handled by the next patterns.
                pass

        # A Wikipedia citation looks like:
        # {{cite journal
        # | last = Einstein
        # | first = Albert
        # | authorlink = Albert Einstein
        # | title = Sidelights on Relativity (Geometry and Experience)
        # | publisher = P. Dutton., Co
        # | date = 1923}}
        m = re.findall(self.re["citation"], markup)
        for citation in m:
            c = citation.replace("\n", "")
            r = WikipediaReference()
            for key in r.__dict__.keys():
                value = re.search("\| {0,1}"+key+"(.*?)[\|}]", c)
                if value:
                    value = value.group(1)
                    value = value.replace("link", "")
                    value = value.strip().strip(" =[]")
                    value = self.plain(value)
                    setattr(r, key, value)
            if r.first != "" and r.last != "":
                r.author = r.first + " " + r.last
            references.append(r)
            p = " "+self.ref+"("+str(len(references))+")"
            markup = markup.replace(citation, p, 1)

        # A Wikipedia embedded url looks like:
        # [http://www.pbs.org/wnet/hawking/html/home.html ''Stephen Hawking's Universe'']
        m = re.findall(self.re["url"], markup)
        for url in m:
            r = WikipediaReference()
            i = url.find(" ")
            if i > 0:
                r.url = url[:i].strip()
                r.note = self.plain(url[i:])
            else:
                r.url = url.strip()
            references.append(r)
            p = r.note+" "+self.ref+"("+str(len(references))+")"
            markup = markup.replace("["+url+"]", p, 1)

        # Since we parsed all citations first and then all notes and urls,
        # the ordering will not be correct in the markup,
        # e.g. (1) (11) (12) (2) (3).
        sorted = []
        m = re.findall(self.ref+"\(([0-9]*)\)", markup)
        for i in m:
            sorted.append(references[int(i)-1])
            markup = markup.replace(
                self.ref+"("+i+")",
                self.ref+"**("+str(len(sorted))+")"
                )
        markup = markup.replace(self.ref+"**", self.ref)
        for r in references:
            if r not in sorted:
                sorted.append(r)
        references = sorted

        return references, markup.strip()


from typing import Union

import numpy as np
import torch
from torchmetrics import Metric

class MeanAngularError(Metric):
    def __init__(self):
        super().__init__()
        self.add_state(
            "err",
            default=torch.tensor(0.0, dtype=torch.float32),
            dist_reduce_fx="sum",
        )
        self.add_state(
            "total",
            default=torch.tensor(0, dtype=torch.long),
            dist_reduce_fx="sum",
        )

    def update(self, preds: torch.Tensor, target: torch.Tensor):
        ae = angular_dist_score(
            target[:, 0], target[:, 1], preds[:, 0], preds[:, 1], avg=False
        )

        self.err += ae.sum()
        self.total += ae.shape[0]

    def compute(self):
        return self.err / self.total

def angular_dist_score(
    az_true: Union[np.ndarray, torch.Tensor],
    zen_true: Union[np.ndarray, torch.Tensor],
    az_pred: Union[np.ndarray, torch.Tensor],
    zen_pred: Union[np.ndarray, torch.Tensor],
    avg: bool = True,
) -> Union[np.ndarray, torch.Tensor]:
    """
    Calculate the MAE of the angular distance between two directions.
    The two vectors are first converted to cartesian unit vectors,
    and then their scalar product is computed, which is equal to
    the cosine of the angle between the two vectors. The inverse
    cosine (arccos) thereof is then the angle between the two input vectors

    Parameters:
    -----------

    az_true : float (or array thereof)
        true azimuth value(s) in radian
    zen_true : float (or array thereof)
        true zenith value(s) in radian
    az_pred : float (or array thereof)
        predicted azimuth value(s) in radian
    zen_pred : float (or array thereof)
        predicted zenith value(s) in radian

    Returns:
    --------

    dist : float
        mean over the angular distance(s) in radian
    """

    t = np if isinstance(az_pred, np.ndarray) else torch

    if not (
        t.all(t.isfinite(az_true))
        and t.all(t.isfinite(zen_true))
        and t.all(t.isfinite(az_pred))
        and t.all(t.isfinite(zen_pred))
    ):
        raise ValueError("All arguments must be finite")

    # pre-compute all sine and cosine values
    sa1 = az_true.sin()
    ca1 = az_true.cos()
    sz1 = zen_true.sin()
    cz1 = zen_true.cos()

    sa2 = az_pred.sin()
    ca2 = az_pred.cos()
    sz2 = zen_pred.sin()
    cz2 = zen_pred.cos()

    # scalar product of the two cartesian vectors (x = sz*ca, y = sz*sa, z = cz)
    scalar_prod = sz1 * sz2 * (ca1 * ca2 + sa1 * sa2) + (cz1 * cz2)

    # scalar product of two unit vectors is always between -1 and 1, this is against nummerical instability
    # that might otherwise occure from the finite precision of the sine and cosine functions
    scalar_prod = t.clip(scalar_prod, -1, 1)

    ae = t.abs(t.arccos(scalar_prod))

    # convert back to an angle (in radian)
    return t.average(ae) if avg else ae


import pickle as pkl

import pandas as pd
import numpy as np

import nltk

from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import string
import joblib
import streamlit as st

def predict(inp):

  class preprocessor:
    def __init__(self, sentences):
      self.sen = sentences

    def sent_tokenizer(self,sent):

      sent = nltk.sent_tokenize(sent)

      for i in range(len(sent)):

        sent[i] = sent[i][:len(sent[i])-1]
      sent = ' '.join(sent)

      return sent

    def tokenize_it(self, sen):
      # instantiate tokenizer class
      sen_new = nltk.word_tokenize(sen)
      return sen_new

    def remove_stopW_and_punc(self, sen_tokens):
      stopwords_english = stopwords.words('english')
      sen_clean = []

      for word in sen_tokens: # Go through every word in your tokens list
          if (word not in stopwords_english and  # remove stopwords
              word not in string.punctuation):  # remove punctuation
              sen_clean.append(word)
      return sen_clean

    def sen_stemmer(self, sen_clean):
      # Instantiate stemming class
      stemmer = PorterStemmer()

      # Create an empty list to store the stems
      sen_stem = []

      for word in sen_clean:
          stem_word = stemmer.stem(word)  # stemming word
          sen_stem.append(stem_word)  # append to the list

      return sen_stem

    # Now we setup a controller which initiates each mehtod of the above declared
    # class in the correct chronological order.

    def controller(self, sen):
        temp_sen = self.sent_tokenizer(sen)
        temp_sen = self.tokenize_it(temp_sen)
        temp_sen = self.remove_stopW_and_punc(temp_sen)
        temp_sen = self.sen_stemmer(temp_sen)

        return temp_sen

    def run(self):
      processed_sens = []
      while self.sen:
        sen = self.sen.pop(0)
        processed_sen = self.controller(sen)
        processed_sens.append(processed_sen)
      processed_sens = np.array(processed_sens)
      return processed_sens

  model = joblib.load("model.sav")

  Vectorizer = joblib.load("Vectorizer.sav")

  #creating a function for predicting the category

  #write your input code here
  # inp = input("write a question:")
  #write your input code here

  lst = []
  lst.append(inp)
  processed_sentences_predict = preprocessor(lst).run()

  temp = []

  for i in range(len(processed_sentences_predict)):
    temp.append(' '.join(processed_sentences_predict[i]))

  X_predict = Vectorizer.transform(np.array(temp))

  prediction = model.predict(X_predict.toarray())
  prediction_list = prediction.tolist()

  rev_ref_dict = {0:'UPSC',1:'Research',2:'Corporate'}
  result = rev_ref_dict[prediction[0]]
  return result

  # #write your input code here
  # inp = input("write a question:")
  # #write your input code here

  # lst = []
  # lst.append(inp)
  # processed_sentences_predict = preprocessor(lst).run()

  # temp = []

  # for i in range(len(processed_sentences_predict)):
  #   temp.append(' '.join(processed_sentences_predict[i]))

  # X_predict = Vectorizer.transform(np.array(temp))

  # prediction = model.predict(X_predict.toarray())
  # prediction_list = prediction.tolist()

  # rev_ref_dict = {0:'UPSC',1:'Research',2:'Corporate'}
  # result = rev_ref_dict[prediction[0]]

  # print(result)


#! python3
# Spreadsheet to Text Files
# Writes the cells of each column into separate text file.

import openpyxl, logging
from openpyxl.utils import get_column_letter

wb = openpyxl.load_workbook('Chapter 12 - TEXTtoEXCEL.xlsx')
ws = wb.active

for col in range(1, ws.max_column+1):
    content = []
    textFile = open('EXCELtoTEXT_%s.txt' % col, 'w')
    for row in range(1, len(ws[get_column_letter(col)])+1):
        content.append(ws.cell(row, col).value)
        '''try:
            textFile.write(ws.cell(row, col).value)
        except:
            pass'''
    textFile.write(''.join(filter(None, content)))
    textFile.close()

print('Saving to files completed.')

Chapter 12/Chapter 12 - Task5 - Spreadsheet to Text Files.py


import os
from typing import List

import numpy as np
import torch
from tqdm import tqdm

from mapping import Mapping
from sr_eval import metric
from word_mapping import synsets
from vector_load import ModelLoad

DICT_PATH = './data/dictionary'
SR_GOLDEN_DATA = './sr_eval/golden/'
GLOVE_VEC = '/home/jpli/data/glove.6B.100d.txt'
w2v = ModelLoad('glove', GLOVE_VEC)
w2v.load()

def sr_ouput(fn: str, A: List, B: List) -> float:
    rls_spearman = metric.spearman(A, B)
    rls_pearson = metric.pearson(A, B)
    print(fn + ':', end='')
    print('\tspearman:{:.3f}'.format(rls_spearman), end='')
    print('\tpearson:{:.3f}'.format(rls_pearson))
    return rls_spearman, rls_pearson

def read_vec(fn: str, csep: str = ';') -> List:
    rst_word, rst_score = list(), list()
    with open(fn, 'r') as fin:
        for line in fin.readlines():
            line = line.strip().split(sep=csep)
            words = list(map(str.lower, line[:-1]))
            rst_word.append(words)
            rst_score.append(float(line[-1]))
    return rst_word, rst_score

def sr_score(w1: str,
             w2: str,
             device: torch.device,
             func: object,
             strategy: str = 'max',
             **params) -> torch.Tensor:

    w1_synsets, w2_synsets = synsets(w1).to(device), synsets(w2).to(device)
    w1_synsets_vec = func(w1_synsets, **params).to(device)
    w2_synsets_vec = func(w2_synsets, **params).to(device)

    if device.type.startswith('cuda'):
        w1_synsets_vec, w2_synsets_vec = w1_synsets_vec.cpu(), w2_synsets_vec.cpu()
    w1_synsets_vec, w2_synsets_vec = w1_synsets_vec.detach(
    ).numpy(), w2_synsets_vec.detach().numpy()

    rls = [0, 0]
    for i in w1_synsets_vec:
        for j in w2_synsets_vec:
            ij_score = metric.cosine(i, j)
            # if strategy == 'max':
            rls[0] = max(rls[0], ij_score)
            # elif strategy == 'mean':
            rls[1] += ij_score
    if len(w1_synsets_vec) == 0 or len(w2_synsets_vec) == 0:
        rls[1] = 0.862
    else:
        rls[1] = rls[1] / (len(w1_synsets_vec) * len(w2_synsets_vec))
    return rls

def word_sr_score(w1: str, w2: str) -> float:
    if w1 not in w2v.model.keys() or w2 not in w2v.model.keys():
        return 0
    return metric.cosine(w2v.model[w1], w2v.model[w2])

def sr_eval(device: torch.device,
      func: object,
      csep: str = ';',
      **params) -> List[float]:

    sr = []
    for fnt in os.listdir(SR_GOLDEN_DATA):
        rls = [[], []]
        if not fnt.endswith('.csv'):
            continue
        fn = os.path.join(SR_GOLDEN_DATA, fnt)
        golden_words, golden_score = read_vec(fn)
        for _lambda in range(0, 11):
            scores = [[], []]
            for word_pair in golden_words:
                w1, w2 = word_pair[0], word_pair[1]
                ent_scores = sr_score(w1, w2, device, func, 'max', **params)
                word_scores = word_sr_score(w1, w2)
                scores[0].append((_lambda * word_scores + (10 - _lambda) * ent_scores[0]) / 10)
                scores[1].append((_lambda * word_scores + (10 - _lambda) * ent_scores[1]) / 10)
            rls[0].append(sr_ouput(fnt, golden_score, scores[0]))
            rls[1].append(sr_ouput(fnt, golden_score, scores[1]))
        sr.append(rls)
    return sr

def sr_test(device: torch.device,
            func: object,
            strategy: str = 'max',
            csep: str = ';',
            **params) -> List[float]:

    rls = [[], []]
    for fnt in os.listdir(SR_GOLDEN_DATA):
        if not fnt.endswith('.csv'):
            continue
        fn = os.path.join(SR_GOLDEN_DATA, fnt)
        golden_words, golden_score = read_vec(fn)
        test_score = [[], []]
        for word_pair in golden_words:
            w1, w2 = word_pair[0], word_pair[1]
            scores = sr_score(w1, w2, device, func, strategy, **params)
            test_score[0].append(scores[0])
            test_score[1].append(scores[1])
        rls[0].append(sr_ouput(fnt, golden_score, test_score[0]))
        rls[1].append(sr_ouput(fnt, golden_score, test_score[1]))
    return rls