from PySide import QtCore, QtGui, phonon, QtUiTools
from PySide.phonon import Phonon

class Reproductor(QtGui.QWidget):

    def __init__(self):
        super(Reproductor, self).__init__(parent=None)
        self.setWindowTitle("Python Simple Player")
        self.gridLayout = QtGui.QGridLayout(self)
        loader = QtUiTools.QUiLoader()
        file = QtCore.QFile("repro.ui")
        file.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(file, self)
        file.close()
        self.gridLayout.addWidget(self.ui, 0, 0, 1, 1)
        # seccion multimedia
        self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
        self.mediaObject = Phonon.MediaObject(self)
        Phonon.createPath(self.mediaObject, self.audioOutput)
        self.mediaObject.setTickInterval(1000)
        #
        self.ui.lcdAvance.display("00:00")
        QtCore.QObject.connect(
            self.ui.btnArchivo,
            QtCore.SIGNAL("clicked()"),
            self.openFile)
        QtCore.QObject.connect(
            self.ui.btnPlay,
            QtCore.SIGNAL("clicked()"),
            self.play)
        self.mediaObject.tick.connect(self.alAvanzar)
        self.mediaObject.stateChanged.connect(self.alCambiarEstado)

    def alAvanzar(self, tiempo):
        displayTime = QtCore.QTime(
            0, (tiempo / 60000) %
            60, (tiempo / 1000) %
            60)
        self.ui.lcdAvance.display(displayTime.toString('mm:ss'))

    def alCambiarEstado(self, new, old):
        if self.mediaObject.state() == Phonon.State.StoppedState:
            self.ui.btnPlay.setEnabled(True)
        elif self.mediaObject.state() == Phonon.State.PlayingState:
            self.ui.btnPlay.setText("||")  # .setEnabled(False)

    def play(self):
        if self.mediaObject.state() == Phonon.State.PlayingState:
            self.mediaObject.pause()
            self.ui.btnPlay.setText(">")
        else:
            self.mediaObject.play()
            #///////////////////////////////////
            meta = self.mediaObject.metaData()
            meta["TITLE"] = meta["TITLE"] if "TITLE" in meta else "Indefinido"
            meta["ARTIST"] = meta[
                "ARTIST"] if "ARTIST" in meta else "SinNombre"
            label = "{TITLE} by {ARTIST}".format(**meta)
            self.ui.lblNombre.setText(label)
            #///////////////////////////////////
            self.ui.btnPlay.setText("||")

    def openFile(self):
        fileName = QtGui.QFileDialog.getOpenFileName(
            self,
            u"Abrir Archivo",
            u"/",
            u"Archivos de Audio (*.mp3 *.wav *.ogg)")
        if fileName[1]:
            self.path = fileName[0]
            self.mediaObject.setCurrentSource(
                Phonon.MediaSource(self.path))
            self.ui.btnPlay.setText(">")
            self.ui.lblNombre.setText("...")
            self.ui.lcdAvance.display("00:00")

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    rep = Reproductor()
    rep.show()
    sys.exit(app.exec_())


from dataclasses import dataclass
import json
import sys
import numpy as np
from models import from_dict
from matplotlib import pyplot as plt
from matplotlib import cm
from typing import Any, Dict, List, Union

@dataclass
@from_dict
class RoundSummary:
    dt:                         float
    escapesByRun:               List[List[int]]

def parse_state(data: Dict[str, Any]) -> RoundSummary:
        return RoundSummary.from_dict(data)

def main(data_path):
    with open(data_path, 'r') as particles_fd:
        rounds: RoundSummary = json.load(particles_fd, object_hook=parse_state)

    freed_particles = list(map(lambda e: np.array(e), rounds.escapesByRun))

    W = 100

    q = caudal(freed_particles, rounds.dt, W)

    fig = plt.figure(figsize=(16, 10))
    ax = fig.add_subplot(1, 1, 1)

    ax.tick_params(labelsize=16)

    ax.set_title(f'W ={W}')
    ax.set_xlabel(r'$t$ (s)', size=20)
    ax.set_ylabel(r'caudal(t) ($s^{-1}$)', size=20)

    ax.scatter(np.linspace(0, len(q)*rounds.dt, len(q)), q)

    ax.grid(which="both")
    ax.set_axisbelow(True)
    plt.show()

def caudal(particles, dt, window_size=200) -> np.ndarray:

    max_iterations = min(map(lambda p: p.shape[0], particles)) - 1
    truncated_particles = np.array(list(map(lambda e: e[:max_iterations], particles)))

    window = np.lib.stride_tricks.sliding_window_view(np.mean(truncated_particles, axis=0), window_size)
    return np.sum(window, axis=1) / window_size / dt

if __name__ == '__main__':
    if len(sys.argv) < 2:
        raise ValueError('Config path must be given by argument')
    try:
        main(sys.argv[1])
    except KeyboardInterrupt:
        pass


#!/bin/python3
# Hackerrank plus minus problem

import math
import os
import random
import re
import sys

#
# Complete the 'plusMinus' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#
def plusMinus(arr):
    """
    thought process: I'm going to initialize each
    proportion as their own float and initially they
    will store the amount of positives, negatives... etc.
    They will then be turned into ratios by dividing the
    length of arr. Python has a cheeky round function so
    I will end up using that. Otherwise I would have added
    .5*10^(-4) and floored it. I'm not sure that would have
    worked though.
    """
    # Write your code here
    # initialize variables
    positives = negatives = zeros = float(0)

    # calculate the number of positives, negatives, and zeros
    for i in range(len(arr)):
        # positive number
        if arr[i] > 0:
            positives += 1
        # negative number
        elif arr[i] < 0:
            negatives += 1
        # zero
        else:
            zeros += 1

    # calculate ratios
    positives = round(positives/len(arr), 6)
    negatives = round(negatives/len(arr), 6)
    zeros = round(zeros/len(arr), 6)

    # print
    print(positives)
    print(negatives)
    print(zeros)

if __name__ == '__main__':
    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)


"""Objects that implement schema validation"""

import warnings
import re

from .utils import isnumeric

class SchemaValidationError(Exception):
    pass

class ValidatorList(list):
    """Instantiate a list of validators given a JSONSchema

    Parameters
    ----------
    obj : JSONSchema object
        The schema object from which to build the validators

    Methods
    -------
    validate(self, value):
        validate value with all validators in list
    """
    def __init__(self, obj):
        super(ValidatorList, self).__init__()
        validator_classes = [cls for cls in Validator.__subclasses__()
                             if cls._matches(obj.schema)]

        used_keys = {'definitions', 'description', 'title', '$schema'}
        for cls in validator_classes:
            cls_schema = {key:val for key, val in obj.schema.items()
                          if key in cls.recognized_keys}
            used_keys |= cls_schema.keys()
            self.append(cls(cls_schema, parent=obj))
        unused = obj.schema.keys() - used_keys
        if unused:
            warnings.warn("Unused keys {0} in {1}"
                          "".format(unused, self))

    def validate(self, obj):
        for validator in self:
            validator.validate(obj)

class Validator(object):
    """Abstract base class for JSONSchema validation.

    This class should not be used directly; rather use the ValidatorList class
    """
    recognized_keys = set()

    def __init__(self, schema, parent):
        self.schema = schema
        self.parent = parent

        unrecognized = set(schema.keys()) - set(self.recognized_keys)
        if unrecognized:
            warnings.warn('Unrecognized keys {0} in class {1}'
                          ''.format(unrecognized, self.__class__.__name__))

    def _init_child(self, schema):
        """Initialize a child JSONSchema object from a schema dict"""
        return self.parent.initialize_child(schema)

    def __repr__(self):
        if len(self.schema) > 3:
            args = ', '.join(sorted(self.schema.keys())[:3]) + '...'
        else:
            args = ', '.join(sorted(self.schema.keys()))
        return "{0}({1})".format(self.__class__.__name__, args)

    @classmethod
    def _matches(cls, schema):
        raise NotImplementedError()

    def validate(self, value):
        raise NotImplementedError()

class ObjectValidator(Validator):
    recognized_keys = {'type', 'properties', 'additionalProperties',
                       'patternProperties', 'required'}
    # TODO: handle pattern properties
    @classmethod
    def _matches(cls, schema):
        return (schema.get('type', None) == 'object'
                 or 'properties' in schema
                 or 'additionalProperties' in schema)

    def validate(self, obj):
        required = self.schema.get('required', [])
        if not isinstance(obj, dict):
            if self.schema.get('type', None) == 'object':
                raise SchemaValidationError("{0} is not of type='object'"
                                            "".format(obj))
            if required:
                raise SchemaValidationError("{0} is missing properties {1}"
                                            "".format(obj, required))
        if not all(key in obj for key in self.schema.get('required', [])):
            raise SchemaValidationError("{0} does not contain required keys {1}"
                                        "".format(obj, self.schema['required']))
        if isinstance(obj, dict):
            for key, val in obj.items():
                properties = self.schema.get('properties', {})
                patternProperties = self.schema.get('patternProperties', {})
                additionalProperties = self.schema.get('additionalProperties', True)
                if key in properties:
                    self._init_child(properties[key]).validate(val)
                elif patternProperties:
                    raise NotImplementedError('patternProperties validation')
                elif isinstance(additionalProperties, dict):
                    self.parent.initialize_child(additionalProperties).validate(val)
                elif not additionalProperties:
                    raise SchemaValidationError("{0} property {1} is invalid"
                                                "".format(obj, key))

class ArrayValidator(Validator):
    recognized_keys = {'type', 'items', 'minItems', 'maxItems', 'numItems'}
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'array' or 'items' in schema

    def validate(self, obj):
        if not isinstance(obj, list):
            raise SchemaValidationError()
        if 'minItems' in self.schema and len(obj) < self.schema['minItems']:
            raise SchemaValidationError()
        if 'maxItems' in self.schema and len(obj) > self.schema['maxItems']:
            raise SchemaValidationError()
        if 'numItems' in self.schema and len(obj) != self.schema['numItems']:
            raise SchemaValidationError()
        if 'items' in self.schema:
            itemtype = self._init_child(self.schema['items'])
            for val in obj:
                itemtype.validate(val)

class NumberTypeValidator(Validator):
    recognized_keys = {'type', 'minimum', 'maximum', 'default',
                       'exclusiveMinimum', 'exclusiveMaximum'}
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'number'

    def validate(self, obj):
        if not isnumeric(obj):
            raise SchemaValidationError("{0} is not a numeric type"
                                        "".format(obj))
        if 'minimum' in self.schema and obj < self.schema['minimum']:
            raise SchemaValidationError("{0} is less than minimum={1}"
                                        "".format(obj, self.schema['minimum']))
        if 'maximum' in self.schema and obj > self.schema['maximum']:
            raise SchemaValidationError("{0} is greater than maximum={1}"
                                        "".format(obj, self.schema['maximum']))
        if 'exclusiveMinimum' in self.schema and obj <= self.schema['exclusiveMinimum']:
            raise SchemaValidationError("{0} is less than exclusiveMinimum={1}"
                                        "".format(obj, self.schema['exclusiveMinimum']))
        if 'exclusiveMaximum' in self.schema and obj >= self.schema['exclusiveMaximum']:
            raise SchemaValidationError("{0} is greater than exclusiveMaximum={1}"
                                        "".format(obj, self.schema['exclusiveMaximum']))

class IntegerTypeValidator(Validator):
    recognized_keys = {'type', 'mimimum', 'maximum', 'default',
                       'exclusiveMinimum', 'exclusiveMaximum'}
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'integer'

    def validate(self, obj):
        if not isnumeric(obj):
            raise SchemaValidationError("{0} is not a numeric type"
                                        "".format(obj))
        if not int(obj) == obj:
            raise SchemaValidationError("{0} is not an integer".format(obj))
        if 'minimum' in self.schema and obj < self.schema['minimum']:
            raise SchemaValidationError("{0} is less than minimum={1}"
                                        "".format(obj, self.schema['minimum']))
        if 'maximum' in self.schema and obj > self.schema['maximum']:
            raise SchemaValidationError("{0} is greater than maximum={1}"
                                        "".format(obj, self.schema['maximum']))
        if 'exclusiveMinimum' in self.schema and obj <= self.schema['exclusiveMinimum']:
            raise SchemaValidationError("{0} is less than exclusiveMinimum={1}"
                                        "".format(obj, self.schema['exclusiveMinimum']))
        if 'exclusiveMaximum' in self.schema and obj >= self.schema['exclusiveMaximum']:
            raise SchemaValidationError("{0} is greater than exclusiveMaximum={1}"
                                        "".format(obj, self.schema['exclusiveMaximum']))

class StringTypeValidator(Validator):
    recognized_keys = {'type', 'pattern', 'format',
                       'minLength', 'maxLength', 'default'}
    valid_formats = ['date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri']
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'string'

    def validate(self, obj):
        if not isinstance(obj, str):
            raise SchemaValidationError("{0} is not a string".format(obj))
        if 'minLength' in self.schema and len(obj) < self.schema['minLength']:
            raise SchemaValidationError("{0} is shorter than minLength={1}"
                                        "".format(obj, self.schema['minLength']))
        if 'maxLength' in self.schema and len(obj) > self.schema['maxLength']:
            raise SchemaValidationError("{0} is longer than maxLength={1}"
                                        "".format(obj, self.schema['maxLength']))
        if 'pattern' in self.schema and not re.match(self.schema['pattern'], obj):
            raise SchemaValidationError("{0} does not match pattern {1}"
                                        "".format(obj, self.schema['pattern']))
        if 'format' in self.schema:
            if self.schema['format'] not in self.valid_formats:
                raise SchemaValidationError('format not recognized')
            warnings.warn("format constraint not implemented in StringTypeValidator")

class NullTypeValidator(Validator):
    recognized_keys = {'type'}
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'null'

    def validate(self, obj):
        if obj is not None:
            raise SchemaValidationError()

class BooleanTypeValidator(Validator):
    recognized_keys = {'type', 'default'}
    @classmethod
    def _matches(cls, schema):
        return schema.get('type', None) == 'boolean'

    def validate(self, obj):
        if not isinstance(obj, bool):
            raise SchemaValidationError()

class EnumValidator(Validator):
    recognized_keys = {'enum', 'default'}
    @classmethod
    def _matches(cls, schema):
        return 'enum' in schema

    def validate(self, obj):
        print(obj, self.schema['enum'], obj in self.schema['enum'])
        if obj not in self.schema['enum']:
            raise SchemaValidationError()

class MultiTypeValidator(Validator):
    recognized_keys = {'type', 'minimum', 'maximum'}
    @classmethod
    def _matches(cls, schema):
        return isinstance(schema.get('type', None), list)

    def validate(self, obj):
        schema_copy = self.schema.copy()
        for typ in self.schema['type']:
            schema_copy['type'] = typ
            try:
                self._init_child(schema_copy).validate(obj)
            except:
                pass
            else:
                return
        raise SchemaValidationError()

class RefValidator(Validator):
    recognized_keys = {'$ref'}

    @classmethod
    def _matches(cls, schema):
        return '$ref' in schema

    @property
    def name(self):
        return self.schema['$ref']

    @property
    def refschema(self):
        keys = self.schema['$ref'].split('/')
        if keys[0] != '#':
            raise ValueError("$ref = {0} not recognized".format(self.schema['$ref']))
        refschema = self.parent.root.schema
        for key in keys[1:]:
            refschema = refschema[key]
        return refschema

    def __repr__(self):
        return "RefValidator('{0}')".format(self.name)

    def validate(self, obj):
        self._init_child(self.refschema).validate(obj)

class AnyOfValidator(Validator):
    recognized_keys = {'anyOf'}
    @classmethod
    def _matches(cls, schema):
        return 'anyOf' in schema

    def validate(self, obj):
        for child in self.schema['anyOf']:
            print(child, obj)
            try:
                self._init_child(child).validate(obj)
            except SchemaValidationError:
                pass
            else:
                return
        raise SchemaValidationError()

class OneOfValidator(Validator):
    recognized_keys = {'oneOf'}
    @classmethod
    def _matches(cls, schema):
        return 'oneOf' in schema

    def validate(self, obj):
        count = 0
        for child in self.schema['oneOf']:
            try:
                self._init_child(child).validate(obj)
            except:
                pass
            else:
                count += 1
        if count != 1:
            raise SchemaValidationError()

class AllOfValidator(Validator):
    recognized_keys = {'allOf'}
    @classmethod
    def _matches(cls, schema):
        return 'allOf' in schema

    def validate(self, obj):
        for child in self.schema['allOf']:
            self._init_child(child).validate(obj)

class NotValidator(Validator):
    recognized_keys = {'not'}
    @classmethod
    def _matches(cls, schema):
        return 'not' in schema

    def validate(self, obj):
        try:
            self._init_child(self.schema['not']).validate(obj)
        except SchemaValidationError:
            pass
        else:
            raise SchemaValidationError()


import os
import fire
import numpy as np
from OpenGL.GL import glLineWidth
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
from kitti import read_label_obj, read_calib_file, compute_lidar_box_3d, corner_to_center_box3d
from utils import remove_points_in_boxes

class plot3d(object):
    def __init__(self, title='null'):
        #
        self.glview = gl.GLViewWidget()
        coord = gl.GLAxisItem()
        glLineWidth(2)
        coord.setSize(1,1,1)
        self.glview.addItem(coord)
        self.glview.setMinimumSize(QtCore.QSize(600, 500))
        self.glview.pan(1, 0, 0)
        self.glview.setCameraPosition(azimuth=180)
        self.glview.setCameraPosition(elevation=0)
        self.glview.setCameraPosition(distance=5)
        self.items = []
        #
        self.view = QtGui.QWidget()
        self.view.window().setWindowTitle(title)
        hlayout = QtGui.QHBoxLayout()
        snap_btn = QtGui.QPushButton('&Snap')
        def take_snap():
            qimg = self.glview.readQImage()
            qimg.save('1.jpg')
        snap_btn.clicked.connect(take_snap)
        hlayout.addWidget(snap_btn)
        hlayout.addStretch()
        layout = QtGui.QVBoxLayout()
        #
        layout.addLayout(hlayout)
        layout.addWidget(self.glview)
        self.view.setLayout(layout)
    def add_item(self, item):
        self.glview.addItem(item)
        self.items.append(item)
    def clear(self):
        for it in self.items:
            self.glview.removeItem(it)
        self.items.clear()
    def add_points(self, points, colors):
        points_item = gl.GLScatterPlotItem(pos=points, size=0.5, color=colors)
        self.add_item(points_item)
    def add_line(self, p1, p2, color, width=3):
        lines = np.array([[p1[0], p1[1], p1[2]],
                          [p2[0], p2[1], p2[2]]])
        lines_item = gl.GLLinePlotItem(pos=lines, mode='lines',
                                       color=color, width=width, antialias=True)
        self.add_item(lines_item)
    def plot_bbox_mesh(self, gt_boxes3d, color=(0,1,0,1)):
        b = gt_boxes3d
        for k in range(0,4):
            i,j=k,(k+1)%4
            self.add_line([b[i,0],b[i,1],b[i,2]], [b[j,0],b[j,1],b[j,2]], color)
            i,j=k+4,(k+1)%4 + 4
            self.add_line([b[i,0],b[i,1],b[i,2]], [b[j,0],b[j,1],b[j,2]], color)
            i,j=k,k+4
            self.add_line([b[i,0],b[i,1],b[i,2]], [b[j,0],b[j,1],b[j,2]], color)

def value_to_rgb(pc_inte):
    minimum, maximum = np.min(pc_inte), np.max(pc_inte)
    ratio = (pc_inte-minimum) / (maximum - minimum)
    r = (np.maximum((1 - ratio), 0))
    b = (np.maximum((ratio - 1), 0))
    g = 1 - b - r
    return np.stack([r, g, b, np.ones_like(r)]).transpose()

SPLIT = 'training'
KITTI_PATH = '/mine/KITTI_DAT/' + SPLIT
KITTI_CALIB = '/mine/KITTI_DAT/calib/' + SPLIT

def plot_obj(viewer, objs, calib, color):
    for obj in objs:
        box3d_pts_3d = compute_lidar_box_3d(obj,
            calib['R0_rect'].reshape([3,3]),
            calib['Tr_velo_to_cam'].reshape([3,4]))
        viewer.plot_bbox_mesh(box3d_pts_3d, color)
        center = corner_to_center_box3d(box3d_pts_3d)
        print(center)
        min_xyz = center[:3] - center[3:6] / 2.0
        max_xyz = center[:3] + center[3:6] / 2.0
        viewer.add_line(min_xyz, max_xyz, (1,0,1,1))

def view(id='000018'):
    app = QtGui.QApplication([])

    f = os.path.join(KITTI_PATH, 'velodyne', id+'.bin')
    lidar = np.fromfile(f, dtype=np.float32).reshape(-1, 4)
    f = os.path.join(KITTI_PATH, 'label_2', id+'.txt')
    gt_obj = read_label_obj(f)
    f = os.path.join(KITTI_CALIB, id+'.txt')
    calib_dict = read_calib_file(f)

    glview = plot3d()
    # points cloud
    points = lidar[:,:3]
    intensity = lidar[:,3]
    points_color = value_to_rgb(intensity)
    glview.add_points(points, points_color)
    # gt bounding box
    plot_obj(glview, gt_obj, calib_dict, (0,1,0,1))
    # predict bounding box
    f = id + '.txt'
    if os.path.exists(f):
        pred_obj = read_label_obj(f)
        plot_obj(glview, pred_obj, calib_dict, (0,0,1,1))

    glview.view.show()
    return app.exec()

def view_points_cloud(pc=None):
    app = QtGui.QApplication([])
    glview = plot3d()
    if pc is None:
        pc = np.random.rand(1024, 3)
    pc_color = np.ones([pc.shape[0], 4])
    glview.add_points(pc, pc_color)
    glview.view.show()
    return app.exec()

def view_pc(pc=None, boxes3d=None):
    app = QtGui.QApplication([])
    glview = plot3d()
    if pc is None:
        points = np.random.rand(1024, 3)
        pc_color = np.ones([1024, 4])
        s = 0.5
        boxes3d = np.array([[[s,s,s],[-s,s,s],[-s,-s,s],[s,-s,s],
                             [s,s,-s],[-s,s,-s],[-s,-s,-s],[s,-s,-s]]])
        points, indices = remove_points_in_boxes(points, boxes3d)
        pc_color = pc_color[indices]
    else:
        if pc.shape[1] == 3:
            points = pc[:,:3]
            pc_color = np.ones([pc.shape[0], 4])
        elif pc.shape[1] == 4:
            points = pc[:,:3]
            pc_color = value_to_rgb(pc[:,3])
    if boxes3d is not None:
        for box3d in boxes3d:
            glview.plot_bbox_mesh(box3d)
    glview.add_points(points, pc_color)
    glview.view.show()
    return app.exec()

if __name__ == "__main__":
    fire.Fire()


from dataclasses import dataclass
from header import *
import struct

@dataclass
class Question():

    url : str
    query_type : int

    def build_question(self):
        query_packet = "".encode()
        for i in self.url.split("."):
            encoded = i.encode()
            bytelen = len(i)
            query_packet += struct.pack(f"! {bytelen+1}B", bytelen, *encoded)
        query_packet += struct.pack(
            ">B2H", 0, self.query_type, 1 # End of String,# Query Type,# Query Class
        )
        return query_packet

    def parse_question(self,data):
        self.url=self.parse_hostname(data)
        self.query_type=self.parse_query_type(data)
        query_class = self.parse_query_class(data)
        return f"\nDomain : {self.url}      Query Type :  {query_class}    Query Class :  {self.query_type}"

    def parse_hostname(self, data_recieved):
        url = []
        chunk_size = data_recieved[12]
        index = 13
        while chunk_size != 0:
            for i in range(chunk_size):
                url.append(data_recieved[index])
                index += 1
            url.append(46)
            chunk_size = data_recieved[index]
            index += 1
        hostname = ""
        for i in url:
            hostname += chr(i)
        return hostname

    def parse_query_type(self, data_recieved):
        index = 12
        while data_recieved[index] != 0:
            index += 1
        index += 1
        query_type = ""
        query_type_list = list(data_recieved[index : index + 2])
        index += 2
        if (query_type_list[0] * 10 + query_type_list[1] * 1) == 1:
            query_type += "  A   "
        return query_type

    def parse_query_class(self,data_recieved):
        index = 12
        while data_recieved[index] != 0:
            index += 1
        index += 3
        query_class = ""
        class_list = list(data_recieved[index : index + 2])
        if (class_list[0] * 10 + class_list[1] * 1) == 1:
            query_class += " IN "
        return query_class


def sorted(s):
    st = []
    while len(s)!=0:
        x =s[-1]
        s.pop()
        while len(st)!=0 and x<st[-1]:
            s.append(st[-1])
            st.pop()
        st.append(x)
    return st
print(sorted([11,2,32,3,41]))


import operator
from SVM import*
import numpy as np

def load_data(filename):
    """
    数据加载函数
    :param filename: 数据文件名
    :return:
        data_mat - 加载处理后的数据集
        label_mat - 加载处理后的标签集
    """
    num_feat = len(open(filename).readline().split(';')) - 1
    data_mat = []
    label_mat = []
    fr = open(filename)
    for line in fr.readlines():
        line_arr = []
        cur_line = line.strip().split(';')
        # 得到分数为5和6的数据集
        if cur_line[-1] == '5' or cur_line[-1] == '6':
            # 循环特征数据加入line_arr
            for i in range(num_feat):
                line_arr.append(float(cur_line[i]))
            data_mat.append(line_arr)
            if cur_line[-1] == '5':
                label_mat.append(-1)
            else:
                label_mat.append(1)
    return data_mat, label_mat

# 利用kNN模型对数据集进行分类
def classify(test, data_set, label, k):
    """
    kNN算法
    :param test: 待分类的数据
    :param data_set: 已分好类的数据集
    :param label: 分类标签
    :param k: kNN算法参数，选择距离最小的k个数据
    :return: classify_result —— kNN算法分类结果
    """
    # 计算两组数据的欧氏距离
    test_copy = np.tile(test, (data_set.shape[0], 1)) - data_set
    # 二维特征相减后平方
    sq_test_copy = test_copy ** 2
    # sum() 所有元素相加，sum(0)列相加，sum(1)行相加
    row_sum = sq_test_copy.sum(axis=1)
    # 开方，得到数据点间的距离
    distance = row_sum ** 0.5
    # 返回 distances 中元素从小到大排序后的索引值
    sorted_index = distance.argsort()
    # 定义一个记录类别次数的字典
    class_count = {}
    # 遍历距离最近的前n个数据，统计类别出现次数
    for v in range(k):
        # 取出前 k 个元素的类别
        near_data_label = label[sorted_index[v]]
        # dict.get(key,default=None),字典的get()方法,返回指定键的值,如果值不在字典中返回默认值。
        # 计算类别次数
        class_count[near_data_label] = class_count.get(near_data_label, 0) + 1
    # 根据字典的值进行降序排序
    classify_result = sorted(class_count.items(), key=operator.itemgetter(1), reverse=True)
    # print(classify_result)
    # 返回次数最多的类别，即待分类点的类别
    return classify_result[0][0]

def test_for_kNN(filename, horatio=0.1, k=4):
    """
    利用kNN对5、6分数类别进行分类
    :param filename: 文件名
    :param horatio: 测试集比例
    :param k: kNN参数
    :return: 无
    """
    data, label = load_data(filename)
    data = np.array(data)
    m = np.shape(data)[0]
    test_num = int(m * horatio)
    error_count = 0.0
    for i in range(test_num):
        classify_result = classify(data[i, :], data[test_num:m, :], label[test_num:m], k)
        if classify_result != label[i]:
            error_count += 1.0
    print("kNN模型的预测准确率： %.1f%%" % (100 * (1 - error_count / test_num)))

# 利用支持向量机模型对数据集进行分类
def test_rbf(filename, k1=20, horatio=0.1):
    """
    测试函数
    :param horatio: 测试集比例
    :param filename: 文件名
    :param k1: 使用高斯核函数的时候表示到达率
    :return: 无
    """
    data_arr, label_arr = load_data(filename)
    m = len(data_arr)
    test_num = int(m * horatio)
    test_arr = data_arr[0:test_num]
    test_label = label_arr[0:test_num]
    train_arr = data_arr[test_num:]
    train_label = label_arr[test_num:]
    b, alphas = smo_P(train_arr, train_label, 200, 0.0001, 100, ('rbf', k1))
    train_mat = np.mat(train_arr)
    train_label_mat = np.mat(train_label).transpose()
    # 获得支持向量
    sv_ind = np.nonzero(alphas.A > 0)[0]
    svs = train_mat[sv_ind]
    label_sv = train_label_mat[sv_ind]
    print(f'支持向量个数: {np.shape(svs)[0]}')
    m, n = np.shape(train_mat)
    error_count = 0
    for i in range(m):
        # 计算各点的核
        kernel_eval = kernel_trans(svs, train_mat[i, :], ('rbf', k1))
        # 根据支持向量的点，计算超平面，返回预测结果
        predict = kernel_eval.T * np.multiply(label_sv, alphas[sv_ind]) + b
        # 返回数组中各元素的正负符号，用1和-1表示，并统计错误个数
        if np.sign(predict) != np.sign(train_label[i]):
            error_count += 1
    print(f'训练集准确率：{(1 - float(error_count) / m) * 100}')
    # 加载测试集
    error_count = 0
    test_mat = np.mat(test_arr)
    m, n = np.shape(test_mat)
    for i in range(m):
        kernel_eval = kernel_trans(svs, test_mat[i, :], ('rbf', k1))
        predict = kernel_eval.T * np.multiply(label_sv, alphas[sv_ind]) + b
        if np.sign(predict) != np.sign(test_label[i]):
            error_count += 1
    print(f'测试集准确率:{(1 - float(error_count) / m) * 100}')

# 利用AdaBoost模型对数据集进行分类
def stump_classify(data_matrix, col, thresh_val, thresh_flag):
    """
    单层决策树分类函数
    :param data_matrix: 数据矩阵
    :param col: 第cal列，也就是第几个特征
    :param thresh_val: 阈值
    :param thresh_flag: 标志
    :return:
        ret_array - 分类结果
    """
    # 初始化预测分类结果
    ret_array = np.ones((np.shape(data_matrix)[0], 1))
    if thresh_flag == 'lt':
        # col列的特征数据小于（'lt'）分界值（阈值 thresh_val）时，将其类别设置为负类，值为-1.0（基于某阈值的预测）
        ret_array[data_matrix[:, col] <= thresh_val] = -1.0
    else:
        # col列的特征数据大于（'gt'）分界值（阈值 thresh_val）时，将其类别设置为负类，值为-1.0（基于某阈值的预测）
        ret_array[data_matrix[:, col] > thresh_val] = -1.0
    return ret_array

def build_stump(data_arr, class_labels, D):
    """
    找到数据集上最佳的单层决策树
    :param data_arr: 数据矩阵
    :param class_labels: 数据标签
    :param D: 样本权重
    :return:
        best_stump - 最佳单层决策树信息
        min_error - 最小误差
        best_result - 最佳分类结果
    """
    data_matrix = np.mat(data_arr)
    label_mat = np.mat(class_labels).T
    m, n = np.shape(data_matrix)
    num_steps = 10.0
    best_stump = {}
    best_result = np.mat(np.zeros((m, 1)))
    # 初始化最小错误率为无穷大
    min_error = float('inf')
    # 遍历不同特征（遍历列）
    for i in range(n):
        # 找出特征数据极值（一列中的最大和最小值），设置步长 step_size（即增加阈值的步长）
        range_min = data_matrix[:, i].min()
        range_max = data_matrix[:, i].max()
        step_size = (range_max - range_min) / num_steps
        # 设置不同的阈值，计算以该阈值为分界线的分类结果 ———— 不同阈值不同分类情况（'lt', 'gt'）找到错误率最小的分类方式
        # 阈值的设置从最小值-步长到最大值，以步长为间隔逐渐增加阈值
        # 分类结果设置按小于（'lt'）阈值为负类和大于（'gt'）阈值为负类分别进行设置，计算最后分类结果
        for j in range(-1, int(num_steps) + 1):
            for situation in ['lt', 'gt']:
                thresh_val = (range_min + float(j) * step_size)
                predicted_val = stump_classify(data_matrix, i, thresh_val, situation)
                err_arr = np.mat(np.ones((m, 1)))
                # 将分类正确的设置为0
                err_arr[predicted_val == label_mat] = 0
                # 计算错误率
                weighted_error = D.T * err_arr
                # print('\n split:dim %d, thresh %.2f, thresh situation: %s \
                # the weighted error is %.3f' % (i, thresh_val, situation, weighted_error))
                # 记录最小错误率时的信息，生成最佳单层决策树
                if weighted_error < min_error:
                    min_error = weighted_error
                    best_result = predicted_val.copy()
                    best_stump['dim'] = i
                    best_stump['thresh'] = thresh_val
                    best_stump['situation'] = situation
    return best_stump, min_error, best_result

def ada_boost_train_DS(data_arr, class_labels, num_iter=40):
    """
    基于单层决策树的AdaBoost训练
    :param data_arr: 数据集
    :param class_labels: 数据标签
    :param num_iter: 迭代次数
    :return:
        weak_class_arr - 多次训练后得到的弱分类器
    """
    # 存放分类器提升过程中的弱分类器
    weak_class_arr = []
    m = np.shape(data_arr)[0]
    # 初始化权重
    D = np.mat(np.ones((m, 1)) / m)
    agg_class_result = np.mat(np.zeros((m, 1)))
    for i in range(num_iter):
        # 构建单层决策树
        # 弱分类器的错误率 error -> 分类器的权重 alpha -> 数据类别结果权重 -> 弱分类器错误率
        # 弱分类器的错误率 error -> 分类器的权重 alpha -> 累计结果估计值 agg_class_result -> 为0时结束训练
        best_stump, error, class_result = build_stump(data_arr, class_labels, D)
        # print(D.T)
        # 计算alpha,为每个分类器分配的一个权重值alpha，基于每个弱分类器的错误率进行计算
        # max(error, 1e-16)是为避免当弱分类器的错误率为零时进行除零运算
        alpha = float(0.5 * np.log((1.0 - error) / max(error, 1e-16)))
        # 记录求得的alpha，和该弱分类器的分类结果
        best_stump['alpha'] = alpha
        weak_class_arr.append(best_stump)
        # print(f'class_result: {class_result}')
        # 计算改变样本权重的e的指数，分类正确为-alpha,错误则为alpha，利用label与result相乘判断正负
        e_exponent = np.multiply(-1 * alpha * np.mat(class_labels).T, class_result)
        D = np.multiply(D, np.exp(e_exponent))
        D = D / D.sum()
        # 记录每个数据点的类别估计积累值
        agg_class_result += alpha * class_result
        # print(f'agg_class_result: {agg_class_result.T}')
        # 计算累加错误率
        agg_errors = np.multiply(np.sign(agg_class_result) != np.mat(class_labels).T, np.ones((m, 1)))
        error_rate = agg_errors.sum() / m
        # print(f'total error: {error_rate}')
        if error_rate == 0.0:
            break
    return weak_class_arr

def ada_classify(test_data, classifier_arr):
    """
    测试分类函数
    :param test_data: 测试数集
    :param classifier_arr: AdaBoost训练得到的弱分类器集合
    :return:
        sign(agg_class_result) - 分类结果
    """
    test_matrix = np.mat(test_data)
    m = np.shape(test_matrix)[0]
    # 累计分类估计值
    agg_class_result = np.mat(np.zeros((m, 1)))
    # 遍历得到的每一个弱分类器，
    for i in range(len(classifier_arr)):
        # 根据该分类器进行分类
        class_result = stump_classify(test_matrix, classifier_arr[i]['dim'],
                                      classifier_arr[i]['thresh'], classifier_arr[i]['situation'])
        # 利用分类器权重累加分类估计值
        agg_class_result += classifier_arr[i]['alpha'] * class_result
        # print(agg_class_result)
    # 利用sign函数得到分类结果,其实是根据概率进行分类
    # 根据累加的估计分类值，值属于正样本的概率大（这里为值大于0），则判为正类，
    # 属于负样本的概率大（小于0），则判为负类。实质上这里的分类阈值为0.5
    return np.sign(agg_class_result)

def test_for_Ada(filename, horatio=0.1, num_item=30):
    # 利用AdaBoost算法对数据集进行训练预测
    data_arr, label_arr = load_data(filename)
    m = len(data_arr)
    # 划分数据集为训练集和测试集
    test_num = int(m * horatio)
    test_arr = data_arr[0:test_num]
    test_label = label_arr[0:test_num]
    train_arr = data_arr[test_num:]
    train_label = label_arr[test_num:]
    # 基于单层决策树训练训练集
    classifier_arr = ada_boost_train_DS(train_arr, train_label, num_item)
    # 对测试集进行预测并统计其错误率
    prediction = ada_classify(test_arr, classifier_arr)
    m = np.shape(test_arr)[0]
    error_arr = np.mat(np.ones((m, 1)))
    print("AdaBoost模型预测准确率： %.1f%%" % (100 * (1 - error_arr[prediction != np.mat(test_label).T].sum() / m)))

test_for_Ada('red_wine', horatio=0.1, num_item=35)
test_for_kNN('red_wine', k=4)
test_rbf('red_wine')


import datetime as dt
import numpy as np
import pandas as pd

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect

from flask import Flask, jsonify

#Setup
engine=create_engine("sqlite:///hawaii.sqlite")
base=automap_base()
    #reflect tables
base.prepare(engine, reflect=True)
measurement=base.classes.measurement
station=base.classes.station
    #start session
session=Session(engine)

    #flask
app=Flask(__name__)

#routes

@app.route("/") #home
def welcome():
    return(
        f"Hawaii Climate Analysis App
"
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/start/end"
    )

@app.route("/api/v1.0/precipitation") #precipitation page
def precipitation():
    last_year=dt.date(2017,8,23) - dt.timedelta(days=365)
    precipitation= session.query(measurement.date,measurement.prcp).filter(measurement.date >= last_year).all()
    precip_dict={date:prcp for date, prcp in precipitation}
    return jsonify(precip_dict)

@app.route("/api/v1.0/stations") #station page
def stations():
    stations_query=session.query(station.station).all()
    stations_list=list(np.ravel(stations_query))
    return jsonify(stations_list=stations_list)
@app.route("/api/v1.0/tobs") #temperature page
def temps():
    last_year=dt.date(2017,8,23) - dt.timedelta(days=365)
    results = session.query(measurement.tobs).filter(measurement.station == 'USC00519281').filter(measurement.date >= last_year).all()
    temps_list=list(np.ravel(results))
    return jsonify(temps_list=temps_list)

@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def stats(start=None,end=None):
    #select statement
    select=[func.min(measurement.tobs), func.max(measurement.tobs), func.avg(measurement.tobs)]

    if not end:
        results1=session.query(*select).filter(measurement.date >= start).all()
        temps_list1=list(np.ravel(results1))
        return jsonify(temps_list1)

    results2= session.query(*select).filter(measurement.date >=start).filter(measurement.date <= end).all()
    temps_list2=list(np.ravel(results2))
    return jsonify(temps_list2=temps_list2)

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


import sys
from itertools import permutations

n = [1, 2, 3, 4, 5, 6, 7, 8, 9]

num = list(permutations(n, 3))	# 순열로 3개씩 뽑음

t = int(sys.stdin.readline())
for _ in range(t):
    test, s, b = map(int, sys.stdin.readline().split())
    test = list(str(test))
    removed_cnt = 0     # 배열에서 제거된 튜플 개수

    # num : 3개 리스트
    leng = len(num)
    for i in range(leng):
        s_cnt = b_cnt = 0   # 스트 개수, 볼 개수 0 초기화
        i -= removed_cnt

        for j in range(3):
            test[j] = int(test[j])
            if test[j] in num[i]:
                if j == num[i].index(test[j]):
                    s_cnt += 1
                else:
                    b_cnt += 1

        if s_cnt != s or b_cnt != b:
            num.remove(num[i])      # 스트 개수, 볼 개수 다르면 배열에서 제거
            removed_cnt += 1

print(len(num))


import os
import torch
import numpy as np
import visdom
import time
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt

from MTAGN_utils.train_utils import MyDataset, set_seed
from MTAGN_utils.plot_utils import plot_confusion_matrix
from data_generator.CWdata import CW_0, CW_1, CW_2, CW_3
from data_generator.SQdata import SQ_39, SQ_29, SQ_19
from data_generator.EBdata import EB
from models.jlcnn import JLCNN
from models.mt1dcnn import MT1DCNN
from models.mtagn import MTAGN

# ==================== train and test =====================
class multi_task_trainer:
    def __init__(self, model_):
        self.model = model_

    def train(self, train_loader_, valid_loader_, valid_set_, optimizer_, scheduler_, epochs=100):
        # ----------- model initial ----------
        loss_fuc = torch.nn.CrossEntropyLoss()

        train_batch = len(train_loader_)
        valid_batch = len(valid_loader_)

        print('--------------------Training--------------------')
        counter = 1
        T = 2
        avg_cost = np.zeros([epochs, 6])  # 0\1\2 train loss; 3\4\5 valid loss
        lambda_weight = np.ones([2, epochs])
        weight = np.zeros((100, 2))
        for epoch in range(epochs):
            train_loss_1 = 0.0
            train_loss_2 = 0.0
            train_loss = 0.0
            train_acc_1 = 0
            train_acc_2 = 0

            cost = np.zeros(6, dtype=np.float32)

            if epoch == 0 or epoch == 1:
                lambda_weight[:, epoch] = 1.0
            else:
                w_1 = avg_cost[epoch - 1, 0] / avg_cost[epoch - 2, 0]
                w_2 = avg_cost[epoch - 1, 1] / avg_cost[epoch - 2, 1]
                lambda_weight[0, epoch] = 2 * np.exp(w_1 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T))
                lambda_weight[1, epoch] = 2 * np.exp(w_2 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T))

            self.model.train()
            for batch_idx, data in enumerate(train_loader_):
                optimizer_.zero_grad()
                inputs, label1, label2 = data
                inputs, label1, label2 = inputs.to(device), label1.to(device), label2.to(device)

                # forward + backward + update
                output1, output2 = self.model(inputs)
                tr_loss = [loss_fuc(output1, label1),
                           loss_fuc(output2, label2)]
                loss = sum(lambda_weight[i, epoch] * tr_loss[i] for i in range(2))

                train_loss_1 += tr_loss[0].item()
                train_loss_2 += tr_loss[1].item()
                train_loss += loss.item()

                loss.backward()
                optimizer_.step()

                cost[0] = tr_loss[0].item()
                cost[1] = tr_loss[1].item()
                cost[2] = loss.item()

                avg_cost[epoch, :3] += cost[:3] / train_batch

                train_acc_1 += np.sum(np.argmax(output1.cpu().data.numpy(), axis=1) == label1.cpu().numpy())
                train_acc_2 += np.sum(np.argmax(output2.cpu().data.numpy(), axis=1) == label2.cpu().numpy())
            train_acc_1 = (train_acc_1 / train_set.__len__()) * 100
            train_acc_2 = (train_acc_2 / train_set.__len__()) * 100

            # valid
            self.model.eval()
            with torch.no_grad():
                valid_loss_1 = 0.0
                valid_loss_2 = 0.0
                valid_loss = 0.0
                valid_acc_1 = 0
                valid_acc_2 = 0
                if epoch + 1 == epochs:
                    pred_label1 = np.array([], dtype=np.int64)
                    pred_label2 = np.array([], dtype=np.int64)
                for data in valid_loader_:
                    inputs, label1, label2 = data
                    inputs, label1, label2 = inputs.to(device), label1.to(device), label2.to(device)
                    output1, output2 = self.model(inputs)
                    val_loss = [loss_fuc(output1, label1),
                                loss_fuc(output2, label2)]
                    loss = sum(lambda_weight[i, epoch] * val_loss[i] for i in range(2))

                    valid_loss_1 += val_loss[0].item()
                    valid_loss_2 += val_loss[1].item()
                    valid_loss += loss.item()

                    cost[3] = val_loss[0].item()
                    cost[4] = val_loss[1].item()
                    cost[5] = loss.item()

                    avg_cost[epoch, 3:6] += cost[3:6] / valid_batch

                    valid_acc_1 += np.sum(np.argmax(output1.cpu().data.numpy(), axis=1) == label1.cpu().numpy())
                    valid_acc_2 += np.sum(np.argmax(output2.cpu().data.numpy(), axis=1) == label2.cpu().numpy())

                    if epoch + 1 == epochs:
                        pred_label1 = np.append(pred_label1, torch.max(output1, dim=1)[1].cpu().numpy().astype('int64'))
                        pred_label2 = np.append(pred_label2, torch.max(output2, dim=1)[1].cpu().numpy().astype('int64'))

                valid_acc_1 = 100 * valid_acc_1 / valid_set.__len__()
                valid_acc_2 = 100 * valid_acc_2 / valid_set.__len__()

                if epoch + 1 == epochs:
                    plot_confusion_matrix(valid_set_.labels1.numpy(), pred_label1, norm=False, task=1)
                    plot_confusion_matrix(valid_set_.labels2.numpy(), pred_label2, norm=False, task=2)
                    plt.show()
            # vis.line(Y=[[avg_cost[epoch, 0]*train_batch, avg_cost[epoch, 1]*train_batch, avg_cost[epoch, 2]*train_batch, avg_cost[epoch, 3]*train_batch,
            #              avg_cost[epoch, 4]*train_batch, avg_cost[epoch, 5]*train_batch]], X=[counter],
            #          update=None if counter == 0 else 'append', win='loss',
            #          opts=dict(legend=['train_loss_1', 'train_loss_2', 'train_loss', 'valid_loss_1', 'valid_loss_2',
            #                            'valid_loss'], title='Loss', ))
            vis.line(Y=[
                [avg_cost[epoch, 0] * train_batch, avg_cost[epoch, 1] * train_batch, avg_cost[epoch, 2] * train_batch]],
                X=[counter], update=None if counter == 0 else 'append', win='loss',
                opts=dict(legend=['train_loss_1', 'train_loss_2', 'train_loss', 'valid_loss_1', 'valid_loss_2',
                                  'valid_loss'], title='Loss', ))
            vis.line(Y=[[train_acc_1, train_acc_2, valid_acc_1, valid_acc_2]], X=[counter],
                     update=None if counter == 0 else 'append', win='accuracy',
                     opts=dict(legend=['train_acc_1', 'train_acc_2', 'valid_acc_1', 'valid_acc_2'], title='Accuracy'))
            vis.line(Y=[[lambda_weight[0, epoch], lambda_weight[1, epoch]]], X=[counter],
                     update=None if counter == 0 else 'append', win='weight',
                     opts=dict(legend=['weight1', 'weight2'], title='Weight'))
            counter += 1

            scheduler_.step()
            print(
                'epoch: [{}/{}] | Loss: {:.5f} | FTI_acc_tr: {:.2f}% | FSI_acc_tr: {:.2f}% | w1:{:.3f} w2:{:.3f}'.format(
                    epoch + 1, epochs, train_loss, train_acc_1, train_acc_2, lambda_weight[0, epoch],
                    lambda_weight[1, epoch]))
            weight[epoch, 0], weight[epoch, 1] = lambda_weight[0, epoch], lambda_weight[1, epoch]
        print('Finish training!')
        loss_out = avg_cost[:, :3] * train_batch
        print(loss_out.shape)
        np.savetxt('EB-train_loss-DWA.csv', loss_out, delimiter=',')
        print(weight.shape)
        print(weight)
        np.savetxt('lambda-weight-DWA.csv', weight, delimiter=',')
        order_save = input('Save model?(Y/N): ')
        if order_save == 'Y' or order_save == 'y':
            self.save(filename=model_dir, model_name_pkl=model_name)

    def test(self, test_set_, test_loader_):
        self.load(model_path)
        self.model.eval()

        print('-------------------- Testing --------------------')
        acc_1 = 0
        acc_2 = 0
        pred_label1 = np.array([], dtype=np.int64)
        pred_label2 = np.array([], dtype=np.int64)
        with torch.no_grad():
            t0 = time.time()
            for data in test_loader_:
                inputs, label1, label2 = data
                inputs = inputs.to(device)
                output1, output2 = self.model(inputs)

                acc_1 += np.sum(torch.max(output1, dim=1)[1].cpu().numpy() == label1.numpy())
                acc_2 += np.sum(torch.max(output2, dim=1)[1].cpu().numpy() == label2.numpy())

                pred_label1 = np.append(pred_label1, torch.max(output1, dim=1)[1].cpu().numpy().astype('int64'))
                pred_label2 = np.append(pred_label2, torch.max(output2, dim=1)[1].cpu().numpy().astype('int64'))
                t1 = time.time()

            FTI_acc = 100 * acc_1 / test_set_.__len__()
            FSI_acc = 100 * acc_2 / test_set_.__len__()
            te_time = t1 - t0

            # print('pred label1:')
            # for i in range(10):
            #     print(pred_label1[150 * i:150 * (i + 1)])
            # print('pred label2:')
            # for i in range(7):
            #     print(pred_label2[150 * i:150 * (i + 1)])
            print('Accuracy on test_dataset:')
            print(f'FTI-task: {FTI_acc:.2f}%   [{acc_1}/{test_set_.__len__()}]')
            print(f'FSI-task: {FSI_acc:.2f}%   [{acc_2}/{test_set_.__len__()}]')
            print(f'Test time: {te_time:.4f}')
            plot_confusion_matrix(test_set_.labels1.numpy(), pred_label1, norm=False, task=1)
            plot_confusion_matrix(test_set_.labels2.numpy(), pred_label2, norm=False, task=2)
            plt.show()

    def save(self, filename, model_name_pkl):
        if os.path.exists(filename):
            filename = os.path.join(filename, model_name_pkl)
        torch.save(self.model.state_dict(), filename)
        print(f'This model is saved at: {filename}')

    def load(self, filename):
        state = torch.load(filename)
        self.model.load_state_dict(state)
        print('Load model successfully from [%s]' % filename)

if __name__ == '__main__':
    # ==================== Hyper parameters =====================
    EPOCHS = 100
    BATCH_SIZE = 50
    LR = 0.001
    # set_seed(2021)

    # define model, vis, optimiser
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    vis = visdom.Visdom(env='dwa')
    model = MTAGN().to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=LR)
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=1)

    train_X, train_Y1, train_Y2, valid_X, valid_Y1, valid_Y2, test_X, test_Y1, test_Y2 = CW_0()
    # ----------- train data ----------
    train_set = MyDataset(train_X, train_Y1, train_Y2)
    train_loader = DataLoader(train_set, shuffle=True, batch_size=BATCH_SIZE)
    print('train_set')
    print("train_X:", train_X.shape)
    print("train_Y1:", train_Y1.shape)
    print("train_Y2:", train_Y2.shape)
    # ----------- valid data ----------
    valid_set = MyDataset(valid_X, valid_Y1, valid_Y2)
    valid_loader = DataLoader(valid_set, shuffle=False, batch_size=BATCH_SIZE)
    print('valid_set')
    print("valid_X:", valid_X.shape)
    print("valid_Y1:", valid_Y1.shape)
    print("valid_Y2:", valid_Y2.shape)
    # ----------- data ----------
    test_set = MyDataset(test_X, test_Y1, test_Y2)
    test_loader = DataLoader(test_set, shuffle=False, batch_size=BATCH_SIZE)
    print("test_X:", test_X.shape)
    print("test_Y1:", test_Y1.shape)
    print("test_Y2:", test_Y2.shape)

    trainer = multi_task_trainer(model)

    # 模型名字及保存地址
    model_dir = r'F:\PycharmProject\MTNet\ablation experiments'
    model_name = 'mtan_eca6_dwa-cw0-100-3.pkl'
    # model_name = 'test.pkl'
    model_path = os.path.join(model_dir, model_name)
    print("Model path: ", model_dir)
    if not os.path.exists(model_dir):
        print(f'Root dir {model_dir} does not exit.')
        exit()
    else:
        print('File exist? ', os.path.exists(model_dir))
    # order_tr = input('Train or not?(Y/N): ')
    # if order_tr == 'Y' or order_tr == 'y':
    trainer.train(train_loader, valid_loader, valid_set, optimizer, scheduler, EPOCHS)

    order_te = input("Test or not?(Y/N): ")
    if order_te == "Y" or order_te == "y":
        trainer.test(test_set, test_loader)


import pytest

from .crud_helper import CrudHelper
from .fixtures import TravelFactory, RoleFactory, PersonFactory, CompletePersonFactory

class TestTravel(CrudHelper):
    @pytest.yield_fixture(autouse=True)
    def prep_types(self, db_session):
        # Initial permission check gets very unhappy if reviewer role is missing
        self.user = PersonFactory(roles=[RoleFactory(name='funding_reviewer'), RoleFactory(name='organiser')])
        db_session.commit()
        yield

    def test_permissions(self, app, db_session):
        # Must have BOTH org and funding_reviewer
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'])
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'], bad_roles=['organiser'])
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'], bad_roles=['funding_reviewer'])

    def test_new(self, app, db_session):
        data = {
                # NOTE: No facility to enter the flight details
                "origin_airport" : "Here",
                "destination_airport" : "There",
               }

        CrudHelper.test_new(self, app, db_session, data=data, user=self.user)

    def test_view(self, app, db_session):
        target = TravelFactory(person = CompletePersonFactory())
        db_session.commit()
        # NOTE: No facility to view the flight details
        expected = [target.person.fullname, target.origin_airport, target.destination_airport]
        CrudHelper.test_view(self, app, db_session, target=target, expected=expected, user=self.user)

    def test_index(self, app, db_session):
        groups = [TravelFactory(person=CompletePersonFactory()) for i in range(10)]
        db_session.commit()
        entries = { s.id : [str(s.id), s.person.fullname, s.origin_airport, s.destination_airport] for s in groups }

        CrudHelper.test_index(self, app, db_session, entries = entries, user=self.user)

    def test_edit(self, app, db_session):
        target = TravelFactory()
        db_session.commit()

        initial_values = {
                "origin_airport" : target.origin_airport,
                "destination_airport" : target.destination_airport,
                }

        new_values = {
                # NOTE: No facility to enter the flight details
                "origin_airport" : "Here",
                "destination_airport" : "There",
               }

        CrudHelper.test_edit(self, app, db_session, initial_values=initial_values, new_values=new_values, target=target, user=self.user)

    def test_delete(self, app, db_session):
        CrudHelper.test_delete(self, app, db_session, user=self.user)


try:
    from flask_bcrypt import Bcrypt
    from flask_sqlalchemy import SQLAlchemy
    from flask import Flask
    import os
    import logging
except Exception as e:
    print("Could not import module in my_website package")

#get the root logger
logger = logging.getLogger()

#create a formatter object
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s : %(message)s')

# add console handler to the root logger
consoleHanlder = logging.StreamHandler()
consoleHanlder.setFormatter(log_formatter)
logger.addHandler(consoleHanlder)
logger.setLevel(logging.INFO)

#add file handler to the root logger
fileHandler = logging.FileHandler("logs.log")
fileHandler.setFormatter(log_formatter)
logger.addHandler(fileHandler)
logger.setLevel(logging.DEBUG)

# creating object of Flask class named app then setting SECRET_KEY
app = Flask(__name__)

# write first log in record.log
app.logger.debug("App object created of flask class.")

#setting up app config for SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config["SECRET_KEY"] = os.environ.get('SECRET_KEY')
db = SQLAlchemy(app)
app.logger.info("Object created of SQLAlchemy class.")

#binding Bcrypt to mathing plain text to hash value
bcrypt = Bcrypt(app)

# #config app for email server
# app.config['MAIL_SERVER'] = 'smtp.gmail.com'
# app.config['MAIL_PORT']   = 465
# app.config['MAIL_USERNAME'] = os.environ.get('COMPANY_EMAIL')
# app.config['MAIL_PASSWORD'] = ''
# app.config['MAIL_USE_TLS']  = False
# app.config['MAIL_USE_SSL']  = True


from bs4 import BeautifulSoup
import numpy as np
from sklearn.decomposition import LatentDirichletAllocation as LDA
from sklearn.feature_extraction.text import CountVectorizer
import matplotlib.pyplot as plt
import pyLDAvis
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import pyLDAvis.sklearn
from nltk.corpus import reuters
from nltk.corpus import stopwords
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import Counter

n_classes = 90
labels = reuters.categories()

def load_data(config={}):
    """
    Load the Reuters dataset.

    Returns
    -------
    Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
    """
    stop_words = stopwords.words("english")
    vectorizer = TfidfVectorizer(stop_words=stop_words)
    mlb = MultiLabelBinarizer()

    documents = reuters.fileids()
    test = [d for d in documents if d.startswith('test/')]
    train = [d for d in documents if d.startswith('training/')]

    docs = {}
    docs['train'] = [reuters.raw(doc_id) for doc_id in train]
    docs['test'] = [reuters.raw(doc_id) for doc_id in test]
    xs = {'train': [], 'test': []}
    xs['train'] = vectorizer.fit_transform(docs['train']).toarray()
    xs['test'] = vectorizer.transform(docs['test']).toarray()
    ys = {'train': [], 'test': []}
    ys['train'] = mlb.fit_transform([reuters.categories(doc_id)
                                     for doc_id in train])
    ys['test'] = mlb.transform([reuters.categories(doc_id)
                                for doc_id in test])
    data = {'x_train': xs['train'], 'y_train': ys['train'],
            'x_test': xs['test'], 'y_test': ys['test'],
            'labels': globals()["labels"]}
    return data

d = load_data()
X=d["x_train"]

lab = d["y_train"]

#changer le max_iter pour de meilleurs performances.
clf = LDA(n_components=n_classes,max_iter=10,topic_word_prior=0.9)
clf.fit(X)

#calcul de pureté du premier cluster
pred_cluster1=[]
for k in range(len(X)):
    pred = np.argmax(clf.transform([X[k]]))
    if(pred==1):
        pred_cluster1.append(k)
lab_clust1=[]
for p in pred_cluster1:
    lab_clust1.append(np.argmax(lab[p]))
c = Counter(lab_clust1)
somme = sum(c.values())
classe_majoritaire = np.max(list(c.values()))
print("Pureté de la classe numero 1 : ")
print(classe_majoritaire/somme)


import re

class Parser():
	def __ParseCommand(self, expr):
		if'_+_' in expr.replace(" ", ""):
			# forced
			self.__mFormalArgs = "x,y"
			self.__mReturnStmt = "x+y"
		elif 'x=>(x,1)' in expr.replace(" ", ""):
			# forced
			self.__mFormalArgs = ""
			self.__mReturnStmt = "wc"
		elif 'x=>x.toNum' in expr.replace(" ", ""):
			# forced
			self.__mFormalArgs = ""
			self.__mReturnStmt = "stoi"
		elif 'x=>1' in expr.replace(" ", ""):
			# forced
			self.__mFormalArgs = ""
			self.__mReturnStmt = "count"
		else:
			lhs, rhs = re.split(r"\=\>", expr.replace(" ", ""))

			for elem in lhs:
				if re.match(r"[\+|\-|\*\*|\/|\^]", elem):
					raise TypeError("TinySpark: Syntax Error")

			# lhsTemp, rhsTemp only for syntax check
			lhsTemp = re.split(",", lhs.replace(" ", ""))
			lhsTemp = "".join(lhsTemp)
			rhsTemp = re.sub(r"[\+|\-|\*\*|\/|\^|\<|\<\=|\>|\>\=|\=\=|\!\=|\d*|\b(.length)\b]", "", rhs)
			# print(lhsTemp)
			# print(rhsTemp)

			self.__CheckExprSyntax(lhsTemp, rhsTemp)
			self.__CheckExprArgsNumber(lhsTemp, rhsTemp)
			self.__mFormalArgs = lhs
			self.__mReturnStmt = rhs

			# print(self.__mFormalArgs)
			# print(self.__mReturnStmt)

	def __CheckExprArgsNumber(self, left, right):
		"""
		Checks if length all elements of lhs, rhs are same, e.g:

		x, y => x ** y : true

		x, y => -x : false
		"""
		if len(left) != len(right):
			raise TypeError("TinySpark: Syntax Error")
		return 1

	def __CheckExprSyntax(self, left, right):
		"""
		Checks if parameters used on left and right have same case, e.g:

		x => x : true

		x => X : false (and vice versa)
		"""
		lhs = set(left)
		rhs = set(right)

		mixed1 = any(elem.islower() for elem in lhs) and any(elem.isupper() for elem in lhs)
		mixed2 = any(elem.islower() for elem in rhs) and any(elem.isupper() for elem in rhs)
		if mixed1 != mixed2:
			raise TypeError("TinySpark: Syntax Error")
		return 1

	def GetFormalArgs(self):
		return self.__mFormalArgs

	def GetReturnStmt(self):
		return self.__mReturnStmt

	def __init__(self, expr: str):
		self.__ParseCommand(expr)


#!/usr/bin/env python

__version__ = "0.10"
__author__ = [
    "Brian Ray <brianhray@gmail.com>",
]
__license__ = "TBD"

from document_base import *
import model_base

Date = model_base.DateFieldType
URL = model_base.URLFieldType


from dataclasses import dataclass
import random
import numpy as np
from Framework.task import AbstractTask

@dataclass
class ShuffleSamplesTask(AbstractTask):
    sample_var_name: str
    labels_var_name: str

    def process(self, data_store):
        """Shuffle the samples and store them back in the datastore"""
        samples = data_store[self.sample_var_name]
        labels = data_store[self.labels_var_name]
        sample_arr = list(zip(samples, labels))

        random.shuffle(sample_arr)

        samples, labels = zip(*sample_arr)
        data_store[self.sample_var_name] = np.array(samples)
        data_store[self.labels_var_name] = np.array(labels)

        print(f"Shuffled {self.sample_var_name} and {self.labels_var_name}")


import numpy as np
from keras.utils import to_categorical

def transform_to_1d_index_vector(data_source, data_dest):
    assert np.min(data_source) >= 1
    for data in data_source:
        data_dest[data-1] = 1

def transform_to_index_vector(data_source, max_data):
    assert np.max(data_source) <= max_data
    assert np.min(data_source) >= 1
    data_shape = data_source.shape
    data_transform = None
    if len(data_shape) == 1:
        data_transform = np.zeros((max_data,))
        transform_to_1d_index_vector(data_source, data_transform)
    else:
        data_transform = np.zeros((len(data_source), max_data)) #no 0, index 0 means 1
        for row_index, row in enumerate(data_source):
            transform_to_1d_index_vector(row, data_transform[row_index])

    return data_transform

def increasing_segmentation_transform(data_source, segmentation_span=33, max_data=30, min_data=1, **kwargs):
    '''

    :param data_source:
    :param segmentation_span: every sample length, len(x):segmentation_span-1, len(y):1
    :param kwargs:
    :return:
    '''
    data_len = len(data_source)
    assert segmentation_span > 0
    assert data_len >= segmentation_span
    data_index = 0
    segmentation_tail_index = lambda : data_index + segmentation_span - 1
    label_one_hot_encoding_func = kwargs.get('label_one_hot_encoding_func')
    if not label_one_hot_encoding_func:
        label_one_hot_encoding_func = lambda data:transform_to_index_vector(data, max_data)
    x_train = None
    y_train = None
    while segmentation_tail_index() < data_len:
        x_data = data_source[data_index : segmentation_tail_index()]
        y_data = data_source[segmentation_tail_index()]
        assert np.max(x_data) <= max_data
        assert np.min(x_data) >= min_data
        assert np.max(y_data) <= max_data
        assert np.min(y_data) >= min_data
        # process x_data(2d) to 1d data
        x_data = x_data.reshape(1, -1)
        if data_index == 0:
            x_train = x_data
            y_train = y_data
        else:
            x_train = np.vstack((x_train, x_data))
            y_train = np.vstack((y_train, y_data))

        data_index += 1
    # process y_data(1d) to 1d one hot vector data
    y_train = label_one_hot_encoding_func(y_train)
    return x_train, y_train


from typing import List

# 枚举法, 超时
class Solution_1:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        self.res = 0
        def get_list(length, cur):
            if length == 0:
                if sum([a * b for a, b in zip(cur, nums)]) == S:
                    self.res += 1
                return
            get_list(length-1, cur + [1])
            get_list(length-1, cur + [-1])
        get_list(len(nums), [])
        return self.res

# 0, 1背包
class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        target = sum(nums) + S
        if target % 2 != 0:
            return 0
        target = target // 2
        if target > sum(nums):
            return 0
        dp = [0] * (target + 1)
        dp[0] = 1
        for num in nums:
            for i in range(target, num-1, -1):
                dp[i] += dp[i-num]
            print(dp)
        return dp[target]

nums = [1,2,7,9,981]
S = 1000000000
s = Solution()
print(s.findTargetSumWays(nums, S))


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

import os
from codecs import open
from setuptools import setup
from setuptools import find_packages

version_info = {}
current_dir = os.path.abspath(os.path.dirname(__file__))
print(current_dir)
with open(os.path.join(current_dir, 'patan', '__version__.py'), 'r', 'utf-8') as f:
    exec(f.read(), version_info)

setup(
    name='Patan',
    version=version_info['__version__'],
    description='A lightweight web crawling framework',
    project_urls={'Source': 'https://github.com/20perline/patan'},
    url='https://github.com/20perline/patan',
    author='20perline',
    author_email='tlhk2@163.com',
    license='MIT',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    entry_points={'console_scripts': ['patan = patan.cli:main']},
    install_requires=['aiohttp>=3.6.2', 'glom>=20.5.0', 'click>=6.7', 'aiohttp-sse-client>=0.2.1'],
    python_requires='>=3.7',
)

# local development
# pip install --upgrade .


import logging

from monitor.sys import system
from monitor.ui.menu.pgm.menu import Menu
from monitor.ui.menu.pgm import events as pge
from monitor.ui.menu.sensors.heater import HeaterMenu
from monitor.ui.menu.confirmation import ConfirmationMenu
from monitor.ui.static.settings import UISettings as uis
from monitor.events.registry import Registry as events

class ServiceMenu:

    def __init__(self, main_surface):
        """
        :param main_surface: screen or main view frame
        :param bgfun: background redraw canvas
        """
        self._logger = logging.getLogger(__name__)
        self.main_surface = main_surface
        self.test_in_progress = False
        # keep a list of options to quickly disable them all
        self.menu_options_to_disable = []
        self.main = Menu(
            main_surface,
            color_selected=uis.COLOR_SELECTED,
            loop=False,
            bgfun=self._background_redraw,
            dopause=True,
            font=uis.FONT_PATH,
            mouse_enabled=False,
            menu_color=uis.MENU_COLOR,
            menu_color_title=uis.MENU_COLOR_TITLE,
            menu_height=uis.HEIGHT,
            menu_width=uis.WIDTH,
            menu_alpha=100,
            onclose=pge.PYGAME_MENU_CLOSE,
            option_shadow=False,
            rect_width=4,
            title='Service Menu',
            title_offsety=5,
            window_height=uis.HEIGHT,
            window_width=uis.WIDTH,
        )

        self.heater = HeaterMenu(self.main, main_surface)

        # self.temp_benchmark = ConfirmationMenu(
        #     main=self.main,
        #     surface=main_surface,
        #     name='Benchmark Temp.',
        #     callback=system.temp_benchmark,
        #     args=()
        # )

        # self.co2_benchmark = ConfirmationMenu(
        #     main=self.main,
        #     surface=main_surface,
        #     name='Benchmark CO\u2082',
        #     callback=system.co2_benchmark,
        #     args=()
        # )

        # self.o2_benchmark = ConfirmationMenu(
        #     main=self.main,
        #     surface=main_surface,
        #     name='Benchmark O\u2082',
        #     callback=system.o2_benchmark,
        #     args=()
        # )

        # self.full_benchmark = ConfirmationMenu(
        #     main=self.main,
        #     surface=main_surface,
        #     name='Benchmark All',
        #     callback=system.benchmark,
        #     args=()
        # )

        self.network_reset = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Reset Networking',
            callback=system.reset_network,
            args=()
        )

        self.factory_reset = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Factory Reset',
            callback=system.factory_reset,
            args=()
        )

        self.calibrate_dpc_background = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Calibrate Background',
            callback=system.calibrate_dpc,
            args=()
        )

        self.calibrate_co2 = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Calibrate CO\u2082',
            callback=system.calibrate_co2,
            args=()
        )

        self.firmware_flash = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Flash Firmware',
            callback=system.flash_firmware,
            args=()
        )

        self.update_snap = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Update Software',
            callback=system.update_snap,
            args=()
        )

        self.main_menu = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Return',
            callback=events.mode_switch.trigger,
            args=(True,)
        )

        pygame_exit = ConfirmationMenu(
            main=self.main,
            surface=main_surface,
            name='Shutdown',
            callback=system.shutdown,
            args=()
        )

        self.aux_heater_option = self.main.add_option(
            self.heater.get_title(), self.heater.menu)

        # self.temp_benchmark_option = self.main.add_option(self.temp_benchmark.get_title(), self.temp_benchmark.menu)
        # self.menu_options_to_disable.append(self.temp_benchmark_option)

        # self.co2_benchmark_option = self.main.add_option(self.co2_benchmark.get_title(), self.co2_benchmark.menu)
        # self.menu_options_to_disable.append(self.co2_benchmark_option)

        # self.o2_benchmark_option = self.main.add_option( self.o2_benchmark.get_title(), self.o2_benchmark.menu)
        # self.menu_options_to_disable.append(self.o2_benchmark_option)
        #self.full_benchmark_option = self.main.add_option(self.full_benchmark.get_title(), self.full_benchmark.menu)

        self.menu_options_to_disable.append(
            self.main.add_option(self.network_reset.get_title(), self.network_reset.menu))
        self.menu_options_to_disable.append(
            self.main.add_option(self.factory_reset.get_title(), self.factory_reset.menu))
        self.menu_options_to_disable.append(
            self.main.add_option(self.calibrate_dpc_background.get_title(), self.calibrate_dpc_background.menu))
        self.menu_options_to_disable.append(
            self.main.add_option(self.calibrate_co2.get_title(), self.calibrate_co2.menu))
        self.firmware_flash_option = self.main.add_option(
            self.firmware_flash.get_title(), self.firmware_flash.menu)
        self.menu_options_to_disable.append(self.firmware_flash_option)
        self.update_snap_option = self.main.add_option(
            self.update_snap.get_title(), self.update_snap.menu)
        self.menu_options_to_disable.append(self.update_snap_option)
        # don't make the exit option disabled-able!
        # Add menu option for navigating to main menu
        self.main.add_option(self.main_menu.get_title(), self.main_menu.menu)
        self.main.add_option(pygame_exit.get_title(), pygame_exit.menu)

    def disable_benchmark_options(self, benchmark_test_type):
        for opt in self.menu_options_to_disable:
            opt.set_disable()

    def enable_benchmark_options(self):
        for opt in self.menu_options_to_disable:
            opt.unset_disable()

    def _background_redraw(self):
        self.aux_heater_option.label = self.heater.get_title()


#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt

def ft(t, f_t):
	F_f = np.fft.fft(f_t)*np.array([np.exp(1j*np.pi*i) for i in range(len(t))])
	f = np.fft.fftfreq(len(t))
	return f, F_f

if __name__ == '__main__':
	t = np.arange(-100, 100, 0.1)
	f_t = np.sinc(t)*np.sin(2*np.pi*1*t)
	f, F_f = ft(t, f_t)

	fig1 = plt.figure()
	ax1 = fig1.add_axes([0.1, 0.1, 0.8, 0.8])
	l1 = ax1.plot(t, f_t)
	ax1.set_xlim(-10, 10)
	ax1.set_ylim(-1, 1)

	fig2 = plt.figure()
	ax2 = fig2.add_axes([0.1, 0.1, 0.8, 0.8])
	l2 = ax2.plot(f, F_f.imag)
	ax2.set_xlim(-0.5, 0.5)
	ax2.set_ylim(-10, 10)
	plt.show()


from datetime import datetime
from logging import INFO, Formatter, Logger, LogRecord, StreamHandler

from pytz import UTC, timezone

__all__ = ("ColoredLogger",)

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
BOLD_SEQ = "\033[1m"

def formatter_message(message: str, use_color=True) -> str:
    """Message Formatter

    Parameters
    ----------
    message: str
        Message to format
    use_color: bool = True
        If using colors or not

    Returns
    -------
    str:
        message formatted
    """
    if use_color:
        message = message.replace("$RESET", RESET_SEQ)
        message = message.replace("$BOLD", BOLD_SEQ)
    else:
        message = message.replace("$RESET", "")
        message = message.replace("$BOLD", "")
    return message

COLORS = dict(
    WARNING=YELLOW,
    INFO=BLUE,
    DEBUG=WHITE,
    CRITICAL=YELLOW,
    ERROR=RED,
)

class ColoredFormatter(Formatter):
    def __init__(self, msg: str, use_color=True):
        """Init Method

        Parameters
        ----------
        msg: str
            Format
        use_color: bool = True
            If using color or not
        """
        super().__init__(fmt=msg, datefmt=r"%Y-%m-%d,%H:%M:%S.%f")
        self.use_color = use_color

    def converter(self, timestamp):
        dt = datetime.fromtimestamp(timestamp, tz=UTC)
        return dt.astimezone(timezone("America/Bogota"))

    def formatTime(self, record: LogRecord, datefmt=None):
        dt = self.converter(record.created)
        if datefmt:
            s = dt.strftime(datefmt)
        else:
            try:
                s = dt.isoformat(timespec="milliseconds")
            except TypeError:
                s = dt.isoformat()
        return s

    def format(self, record: LogRecord) -> str:
        """Formatting Method

        Parameters
        ----------
        record : LogRecord
            Record

        Returns
        -------
        str
            Formatted value
        """
        level_name: str = record.levelname
        if self.use_color and level_name in COLORS:
            level_name_color = COLOR_SEQ % (30 + COLORS[level_name]) + level_name
            record.levelname = level_name_color + RESET_SEQ
            pathname = record.pathname.removesuffix(".py")
            record.pathname = pathname.removeprefix("/app")
        return super(ColoredFormatter, self).format(record)

class ColoredLogger(Logger):
    FORMAT = (
        "$BOLD[%(levelname)s]$RESET%(pathname)s/$BOLD%(funcName)s$RESET〕%(message)s"
    )
    COLOR_FORMAT = formatter_message(FORMAT, True)

    def __init__(self, name: str):
        """Init Method

        Parameters
        ----------
        name: str
            Logger's Name
        """
        super(ColoredLogger, self).__init__(name, level=INFO)
        color_formatter = ColoredFormatter(self.COLOR_FORMAT)
        console = StreamHandler()
        console.setFormatter(color_formatter)
        self.addHandler(console)


"""The Loss.

This module and class aim to provide a somewhat useable interface
for the problem of specifying multiple loss functions to compute.

In principle, the canonical way is to provide a list of strings with
names of loss functions. But in practice, we also would like the ability
to use pre-defined collections of lossfs as shortcuts. And sometimes, we
only want to have a single loss, so a list doesn't make sense.

`get_loss` provides an interface to use all of these. The resulting
`Loss` instance then acts as the wrapper to the individual loss functions.
Its arguments are found in a `spec`, from which it can be re-created.

Note that I have declined to use the `config` machinery of `cmlkit` here,
since I think it would be overkill to do so -- the lossfs don't have any
parameters (at least currently). So the `spec` it is, which is just a list
of strings, the same that the constructor would expect.

***

As a user, you should know that typically, when some method in `cmlkit` asks
you for a `loss` argument, you can supply anything that `get_loss` can handle.

If you are instead asked for `lossf`, it needs to be one of the functions in
the `lossf` module.

"""

from .lossfs import get_lossf, lossfs

shortcuts = {
    "default": ["rmse", "mae", "r2"],
    "all_non_pv": [
        "rmse",
        "rmsle",
        "mae",
        "medianae",
        "maxae",
        "r2",
        "cod",
        "one_minus_r2",
    ],
    "all": list(lossfs.keys()),
}

class Loss:
    """Loss wraps multiple loss functions.

    Attributes:
        needs_pv: If True, at least one of the wrapped lossfs needs
            an uncertainty measure.
        longname: Long name of this loss.
        spec: Arguments that can re-create this loss.

    """

    def __init__(self, *args):
        self.lossfs = [get_lossf(l) for l in args]
        self.needs_pv = any(l.needs_pv for l in self.lossfs)
        self.longname = str(self.spec)

    @property
    def spec(self):
        return [l.__name__ for l in self.lossfs]

    def __call__(self, true, pred, pv=None):
        """Compute multiple losses and returns them in dict."""

        return {l.__name__: l(true, pred, pv=pv) for l in self.lossfs}

def get_loss(*args):
    """Obtain a loss.

    Multiple formats are supported:
        get_loss("rmse", "mae") -> Loss("rmse", "mae")
        get_loss(["rmse", "mae"]) -> Loss("rmse", "mae")
        get_loss("default")  -> Loss("rmse", "mae", "r2")
        get_loss(Loss("rmse")) -> Loss("rmse")

    """
    if len(args) == 1:
        arg = args[0]
        if isinstance(arg, Loss):
            return arg
        elif isinstance(arg, (list, tuple)):
            args = arg
        elif arg in shortcuts:
            args = shortcuts[arg]

    return Loss(*args)


# Import sqlite
import sqlite3

# Creating a global variable for context manager
db = 'second_db.sqlite'
# Create table function
def create_table():
    # Make a connection to the sqlite database
        # If DB does not exits it will be created, else will connect
        # Create context manager
            # No longer need to add conn.commit()
    with sqlite3.connect(db) as conn:
        # Run SQL queries
        # conn.execute('CREATE TABLE products (id int, name text)')
            # If table already exits, but will duplicate data
        conn.execute('CREATE TABLE IF NOT EXISTS products (id int, name text)')
    conn.close()    # Closing the function

# Insert example data Function
def insert_example_data():
    with sqlite3.connect(db) as conn:
                # Adding data to table
        conn.execute('INSERT INTO products values (1000, "hat")')
        conn.execute('INSERT INTO products values (2000, "socks")')
        conn.execute('INSERT INTO products values (500, "t-shirt")')
        conn.execute('INSERT INTO products values (2500, "shoes")')
    conn.close()

# Display all data function
def display_all_data():
    conn = sqlite3.connect(db)
    results = conn.execute('SELECT * FROM products')
    print('All products: ')
    # Read from DB
     # Loop over results
    for row in results:
        print(row)  # each row is a Tuple
    conn.close()

def display_one_product(product_name):
    conn = sqlite3.connect(db)
    results = conn.execute('SELECT * FROM products WHERE name like ?', (product_name,))
    first_row = results.fetchone()
    if first_row:
        print('Your product is: ', first_row)
    else:
        print('Product not found.')
    conn.close()

# Create new product function
def create_new_product():
    # Add new product to DB
    new_id = int(input('Enter new id: '))
    new_name = input('Enter new product: ')
    with sqlite3.connect(db) as conn:
        # Adding new product using format string. NOT RECOMMENDED TO USE
            # Will crash program if double quotes or weird value
        # conn.execute(f'INSERT INTO products VALUES ({new_id}, "{new_name}")')

            # Better solution: parameterized queries
                # will not crash with weird values
        conn.execute(f'INSERT INTO products VALUES (?, ?)', (new_id, new_name))
    conn.close()

def update_product():
    # Updating a DB
    updated_product = 'wool hat'
    update_id = 1000
    with sqlite3.connect(db) as conn:
        conn.execute('UPDATE products SET name = ? WHERE id = ?', (updated_product, update_id))
    conn.close()

def delete_product(product_name):
    # Delete from DB
    #delete_product = 'Helmet'
    with sqlite3.connect(db) as conn:
        conn.execute('DELETE from PRODUCTS WHERE name = ?', (product_name, )) # Must have comma after Tuple-(delete_product, ))
    conn.close()

# Close connection when done working with DB

# Functions to be created
    # Function calls
create_table()
insert_example_data()
display_all_data()
display_one_product('jacket')
display_one_product('coat')
create_new_product()
update_product()
delete_product('jacket')
display_all_data()


import torch.nn as nn

class SequenceClassifierWithCLSPooling(nn.Module):
    def __init__(self, input_dim, output_dim, pooling_dim=None, dropout=0.1, **kwargs):
        super(SequenceClassifierWithCLSPooling, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.pooling_dim = pooling_dim
        if pooling_dim is None:
            pooling_dim = input_dim
        else:
            self.pooling_linear = nn.Linear(input_dim, pooling_dim)
            self.pooling_activation = nn.Tanh()
        self.classifier = nn.Linear(pooling_dim, output_dim)

    def forward(self, features, features_len=None):
        # features: BxTxF
        # The first token output corresponding to [CLS] token for the BERT model
        cls_tensor = features[:, 0, :]
        if self.pooling_dim is not None:
            cls_tensor = self.pooling_linear(cls_tensor)
            cls_tensor = self.pooling_activation(cls_tensor)
        cls_tensor = self.dropout(cls_tensor)
        logits = self.classifier(cls_tensor)
        return logits, None


import roslib; roslib.load_manifest('path_planning_analysis')
import rospy
import numpy
import scipy.spatial
from path_planning_analysis.translator import *
from geometry_msgs.msg import Pose, TransformStamped, Point32
from geometry_msgs.msg import Polygon as RosPolygon
from tf import Transformer
from tf.transformations import quaternion_from_euler
from math import sqrt
import Polygon #https://github.com/jraedler/Polygon2/

def distance(x0, y0, x1, y1):
    return sqrt(pow(x1-x0,2)+pow(y1-y0,2))

def distance_to_line(pX, pY, x0, y0, x1, y1):
    A = pX - x0
    B = pY - y0
    C = x1 - x0
    D = y1 - y0

    dot = A * C + B * D
    len_sq = C * C + D * D
    param = dot / len_sq

    if param < 0:
        xx = x0
        yy = y0
    elif param > 1:
        xx = x1
        yy = y1
    else:
        xx = x0 + param * C
        yy = y0 + param * D

    return distance(pX, pY, xx, yy)

def min_distance(px, py, polygon):
    min_dist = float("inf")
    for i, pt in enumerate(polygon.points):
        # check the distance from the point to the first vertex
        vertex_dist = distance(px, py, pt.x, pt.y)

        # check the distance from the point to the edge
        if i < len(polygon.points)-1:
            nextpt = polygon.points[i+1]
        else:
            nextpt = polygon.points[0]

        edge_dist = distance_to_line(px, py, pt.x, pt.y, nextpt.x, nextpt.y)

        min_dist = min(min_dist, min(vertex_dist, edge_dist))
    return min_dist

def get_convex_hull(vecs):
    vecs = numpy.array(vecs)
    hull = scipy.spatial.Delaunay(vecs).convex_hull
    ps = set()
    for x, y in hull:
        ps.add(x)
        ps.add(y)
    ps = numpy.array(list(ps))
    center = vecs[ps].mean(axis=0)
    A = vecs[ps] - center
    h = vecs[ps[numpy.argsort(numpy.arctan2(A[:,1], A[:,0]))]]
    return h

def line_intersection_helper(a1,a2,b1,b2):
    denom =      (b2.y-b1.y)*(a2.x-a1.x) - (b2.x-b1.x)*(a2.y-a1.y)
    if denom==0:
        return None
    numerator1 = (b2.x-b1.x)*(a1.y-b1.y) - (b2.y-b1.y)*(a1.x-b1.x)
    numerator2 = (a2.x-a1.x)*(a1.y-b1.y) - (a2.y-a1.y)*(a1.x-b1.x)
    return numerator1/denom, numerator2/denom

def line_intersection_helper2(a1,a2,f):
    x = a1.x + f * (a2.x - a1.x)
    y = a1.y + f * (a2.y - a1.y)
    return x,y

def line_intersection(a1,a2,b1,b2, segments=False):
    intersection = line_intersection_helper(a1,a2,b1,b2)
    if not intersection:
        return None

    ua, ub = intersection
    if segments:
        if ua < 0 or ua > 1 or ub < 0 or ub > 1:
            return None

    return line_intersection_helper2(a1, a2, ua)

def point_in_poly(point, poly):
    inside = False

    for i, pt1 in enumerate(poly.points):
        pt2 = poly.points[i-1]
        if point.y > min(pt1.y,pt2.y):
            if point.y <= max(pt1.y,pt2.y):
                if point.x <= max(pt1.x,pt2.x):
                    if pt1.y != pt2.y:
                        xints = (point.y-pt1.y)*(pt2.x-pt1.x)/(pt2.y-pt1.y)+pt1.x
                    if pt1.x == pt2.x or point.x <= xints:
                        inside = not inside
    return inside

def poly_to_ros(p):
    newpoly = RosPolygon()
    newpoly.points = [Point32(x,y,0) for x,y in p]
    return newpoly

def ros_to_poly(p):
    return Polygon.Polygon( [(pt.x,pt.y) for pt in p.points])

def polygon_intersection(poly1, poly2):
    q = ros_to_poly(poly1)
    t = ros_to_poly(poly2)
    intersection = q & t
    if len(intersection)==0:
        return None
    return poly_to_ros(intersection[0])

POM = [1, -1]
F = ' ab'

def get_vertices(pose, size, shape_type):
    tf = Transformer(True, rospy.Duration(10.0))
    m = TransformStamped()
    m.header.frame_id = 'object'
    m.transform.rotation.w = 1

    keys = []
    if shape_type=='box':
        for mx in POM:
            for my in POM:
                for mz in POM:
                    key = 'frame%s%s%s'%(F[mx], F[my], F[mz])
                    m.child_frame_id = key
                    keys.append(key)
                    m.transform.translation.x = size[0]/2.0*mx
                    m.transform.translation.y = size[1]/2.0*my
                    m.transform.translation.z = size[2]/2.0*mz
                    tf.setTransform(m)
    else:
        return None

    m.transform.translation.x = pose.position.x
    m.transform.translation.y = pose.position.y
    m.transform.translation.z = pose.position.z
    m.transform.rotation.x = pose.orientation.x
    m.transform.rotation.y = pose.orientation.y
    m.transform.rotation.z = pose.orientation.z
    m.transform.rotation.w = pose.orientation.w
    m.header.frame_id = 'map'
    m.child_frame_id = 'object'
    tf.setTransform(m)

    vertices = []
    for key in keys:
        (off,rpy) = tf.lookupTransform('map', key, rospy.Time(0))
        vertices.append(off)
    return vertices

def get_polygon(pose, size, shape_type):
    vertices = get_vertices(pose, size, shape_type)
    projected = [(a[0], a[1]) for a in vertices]
    points = get_convex_hull(projected)
    p = RosPolygon()
    for x,y in points:
        p.points.append(Point32(float(x),float(y),0))
    return p

class ObjectField:
    def __init__(self, scenario, name_map, simulation_states, t0):
        self.static_objects = {}
        self.dynamic_objects = {}

        for name, obj in scenario.iteritems():
            t = obj.type
            size = obj.size
            new_obj = {'type': t, 'size': size, 'person': obj.is_person}
            oname = name_map.get(name, name)
            if obj.movement is None:
                p = get_pose_from_scenario(name, obj)
                new_obj['pose'] = p
                get_polygon(p, size, t)
                self.static_objects[name] = new_obj
            else:
                poses = []
                for t, state in simulation_states:
                    if not oname in state.name:
                        continue
                    p = get_pose_from_state(oname, state)
                    poses.append((t-t0, p))
                new_obj['poses'] = sorted(poses)
                new_obj['polygons'] = [None] * len(poses)
                self.dynamic_objects[name] = new_obj

    def get_polygons(self, t0, person=False):
        polygons = {}
        for name, obj in self.static_objects.iteritems():
            if person and not obj['person']:
                continue
            if 'polygon' not in obj:
                obj['polygon'] = get_polygon(obj['pose'], obj['size'], obj['type'])
            polygons[name] = obj['polygon']

        for name, obj in self.dynamic_objects.iteritems():
            i0 = None
            for i, (t, pose) in enumerate(obj['poses']):
                if i==0 or t < t0:
                    continue
                dt0 = abs(t-t0)
                dt1 = abs(obj['poses'][i-1][0]-t0)
                if dt0 < dt1:
                    i0 = i
                else:
                    i0 = i - 1
                break
            if obj['polygons'][i0] is None:
                obj['polygons'][i0] = get_polygon(obj['poses'][i0][1], obj['size'], obj['type'])

            polygons[name] = obj['polygons'][i0]
        return polygons

    def nearest_distance_helper(self, px, py, polygons):
        min_dist = float('inf')
        for polygon in polygons:
            d = min_distance(px, py, polygon)
            min_dist = min(min_dist, d)
        return min_dist

    def get_nearest_polygon_distance(self, px, py, t, person=False):
        polygons = self.get_polygons(t, person)
        return self.nearest_distance_helper(px, py, polygons.values())

    def get_nearest_distance_in_polygon(self, px, py, t, mask, person=False):
        masked_polygons = []
        for name, poly in self.get_polygons(t, person).iteritems():
            new_poly = polygon_intersection(mask, poly)
            if new_poly:
                masked_polygons.append(new_poly)
        return self.nearest_distance_helper(px, py, masked_polygons)


from unittest import TestCase
from kepler.utils import init_config, initdb, init_model_vectorizer
from tempfile import mkdtemp
from shutil import rmtree
from configparser import ConfigParser, ExtendedInterpolation
import os.path as op
from sqlalchemy import create_engine
import pandas as pd

class TestKepler(TestCase):

    TABLES = {
        'experiments': {
            'id', 'start_time', 'end_time', 'n_epochs', 'start_metrics', 'end_metrics', 'model'},
        'history': {'id', 'experiment', 'epoch', 'metrics'},
        'metadata': {'created', 'location'},
        'models': {
            'id', 'name', 'weights_path', 'created', 'model_config', 'n_layers', 'n_params',
            'keras_type', 'archmat_index'},
        'projectmodels': {'id', 'project_id', 'model_id'},
        'projects': {'name', 'created', 'location', 'desc'}
    }

    @classmethod
    def setUpClass(cls):
        cls.config_dir = mkdtemp()
        init_config(cls.config_dir)
        cls.config = ConfigParser(interpolation=ExtendedInterpolation())
        with open(op.join(cls.config_dir, 'config.ini'), 'r') as fin:
            cls.config.read_file(fin)
        initdb(cls.config.get('default', 'db'))
        init_model_vectorizer(cls.config.get('models', 'vectorizer'))

    @classmethod
    def tearDownClass(cls):
        rmtree(cls.config_dir)

    def check_config(self, path):
        """Check exhaustively if the configuration has been written to path."""
        self.assertTrue(op.isdir(path))
        parser = ConfigParser(interpolation=ExtendedInterpolation())
        with open(op.join(path, 'config.ini'), 'r') as fin:
            parser.read_file(fin)
        home = parser.get('default', 'home')
        dbpath = op.join(home, 'kepler.db')
        self.assertEqual(dbpath, parser.get('default', 'db'))
        self.check_init_db(dbpath)

    def check_init_db(self, dbpath):
        engine = create_engine(f'sqlite:///{dbpath}')
        self.assertSetEqual(set(engine.table_names()), set(self.TABLES.keys()))
        for tname, cols in self.TABLES.items():
            df = pd.read_sql_table(tname, engine)
            self.assertSetEqual(set(df.columns), cols)
            if tname not in 'metadata projects'.split():
                self.assertEqual(df.shape[0], 0)
            elif tname == 'metadata':
                self.assertEqual(df.shape[0], 1)
            else:
                self.assertSequenceEqual(df['name'].tolist(), ['default'])
                self.assertSequenceEqual(df['location'].tolist(), [f'{op.dirname(dbpath)}'])


#SQLite3 = Borrar datos en nuestra tabla PERSONAS

import sqlite3

conexion = sqlite3.connect("basededatos1.db")

cursor = conexion.cursor()

print("Antes de borrar datos")

cursor.execute("SELECT * FROM PERSONAS ORDER BY edad")
personas_ordenadas = cursor.fetchall()

for persona in personas_ordenadas:
	print(persona)

#BORRAR AQUELLA LINEA QUE TENGA EL NOMBRE IGUAL A PAULA:
cursor.execute("DELETE FROM PERSONAS WHERE nombre = 'Paula'")

print("Despues de borrar datos")
cursor.execute("SELECT * FROM PERSONAS ORDER BY edad")
personas_ordenadas = cursor.fetchall()

for persona in personas_ordenadas:
	print(persona)

conexion.commit()
conexion.close()


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

import re

def isUTF8(data):
    try:
        data.decode('utf8')
    except UnicodeDecodeError:
        return False
    else:
        return True

def isShift_JIS(data):
    try:
        data.decode('shift-jis')
    except UnicodeDecodeError:
        return False
    else:
        return True

def isSequenceMatchPattern(pattern, sequence):
    global config;
    #pattern = u"^\u3010[^\u3011]+\u3011\u30ea\u30b9\u30af";
    if re.match(pattern, sequence, re.UNICODE):
        return True;
    else:
        return False;


import os
import sys
import json
import logging
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions, StandardOptions
from JsonReader import GetTopicTable

def GetEnvVar():
    env_var=dict()

    #print(os.environ)
    print(os.getcwd())

    env_var['JOB_NAME'] = os.environ['JOB_NAME']
    env_var['PROJECT'] = os.environ['PROJECT']
    env_var['REGION'] = os.environ['REGION']
    env_var['TMP_LOCATION'] = os.environ['TMP_LOCATION']
    env_var['TEMPLATE_FILE'] = os.environ['TEMPLATE_FILE']
    env_var['SUBSCRIPTION'] = os.environ['SUBSCRIPTION']
    env_var['DATA_SET'] = os.environ['DATA_SET']

    return env_var

class GetPayLoad(beam.DoFn):

    def process(self, element):
        logging.getLogger().setLevel(logging.INFO)
        parsed = json.loads(element.decode("utf-8"))
        event_payload=parsed['event_payload']
        event_type=parsed['event_type']
        logging.info('usagemediation:GetPayLoad:process %s', event_payload)
        logging.info('usagemediation:GetPayLoad:process %s', event_type)
        event_payload['event_type']=event_type
        logging.info('usagemediation:GetPayLoad:process %s', event_payload)
        yield event_payload

class PrintValue(beam.DoFn):

    def process(self, element):
        logging.getLogger().setLevel(logging.INFO)
        logging.info('usagemediation:PrintValue %s', element)

def run():
    logging.getLogger().setLevel(logging.INFO)

    env_var = GetEnvVar()

    print(env_var)

    pipeline_options = PipelineOptions(
        runner='DataflowRunner',
        project=env_var['PROJECT'],
        job_name=env_var['JOB_NAME'],
        region=env_var['REGION'],
        temp_location=env_var['TMP_LOCATION'],
        template_location=env_var['TEMPLATE_FILE'],
	#service_account_email='pubsub-sandbox@mm-ucaas.iam.gserviceaccount.com',
	#service_account_email='template-sa@mm-ucaas.iam.gserviceaccount.com',
    )

    # Creating pipeline options
    #pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(StandardOptions).streaming = True

    #project=pipeline_options.get_all_options()['project']
    #dataset=known_args.data_set
    project=env_var['PROJECT']
    dataset=env_var['DATA_SET']

    topics, tables=GetTopicTable()

    tables=[project + ':' + dataset + '.' + table for table in tables ]

    topic_mappings=list(zip(topics, tables))

    # Defining our pipeline and its steps
    with beam.Pipeline(options=pipeline_options) as p:
        read_data = p | "ReadFromPubSub" >> beam.io.gcp.pubsub.ReadFromPubSub(
                subscription=env_var['SUBSCRIPTION'], timestamp_attribute=None
            )

        table_names = p | "Create Table Name" >> beam.Create(topic_mappings)

        event_payload = read_data | "PayloadParser" >> beam.ParDo(GetPayLoad())

        (
            event_payload
            | "WriteToBigQuery" >> beam.io.WriteToBigQuery(
                table=lambda row, table_name: table_name[row['event_type']],
                table_side_inputs=(beam.pvalue.AsDict(table_names), ),
                create_disposition=beam.io.BigQueryDisposition.CREATE_NEVER,
                write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND,
            )
        )

if __name__ == "__main__":
    #print(os.environ['VIRTUAL_ENV'])
    print('Using version:', sys.version[:5])
    run()