import torch
import torch.nn.functional as F
from torch import nn

class LuongAttention(nn.Module):
    def __init__(self, units: int):
        super().__init__()
        self.attn = nn.Linear(units, units, bias=False)

    def forward(self, hidden: torch.Tensor, encoder_outputs: torch.Tensor, mask: torch.Tensor, mask_value: float = -1e9) -> torch.Tensor:
        """Calculate attention weights

        :param hidden: [batch size; embedding size]
        :param encoder_outputs: [batch size; seq len; embedding size]
        :param mask: [batch size; seq len]
        :return: [batch size; seq len]
        """
        mask = mask.T
        batch_size, seq_len = mask.shape
        # [batch size; units]
        attended_hidden = self.attn(hidden)
        # [batch size; seq len]
        score = torch.bmm(encoder_outputs, attended_hidden.view(batch_size, -1, 1)).squeeze(-1)
        mask_atten = score.new_ones(score.size())*mask_value
        score = torch.where((mask==False), score, mask_atten)

        # [batch size; seq len]
        weights = F.softmax(score, dim=1)
        return weights

class LocalAttention(nn.Module):
    def __init__(self, units: int):
        super().__init__()
        self.attn = nn.Linear(units, 1, bias=False)

    def forward(self, encoder_outputs: torch.Tensor, mask: torch.Tensor, mask_value: float = -1e9) -> torch.Tensor:
        """Calculate attention weights

        :param encoder_outputs: [seq len; batch size; units]
        :param mask: [seq len; batch size]
        :return: [seq len; batch size; 1]
        """
        # [seq len; batch size; 1]
        attended_encoder_outputs = self.attn(encoder_outputs)
        mask_atten = attended_encoder_outputs.new_ones(attended_encoder_outputs.size())*mask_value
        attended = torch.where((mask.unsqueeze(2)==False), attended_encoder_outputs, mask_atten)

        # [seq len; batch size; 1]
        weights = torch.softmax(attended, dim=1)

        return weights


#!/usr/bin/python

import json
from . import trello
import requests
import sys
from cork import Cork

def getBoards():
    url = 'https://api.trello.com/1/members/me{}'.format(trello.getSecurity())
    userJson = requests.get(url).json()

    url = 'https://api.trello.com/1/members/{}/boards{}'.format(userJson['id'],trello.getSecurity())
    response = requests.get(url)

    boards = response.json()
    print ('Please select a board id and add it to the config from the following list:')
    for board in boards:
        print (" * {}: {}".format(board['id'], board['name']))

def getLists(boardId):
    if not boardId:
        getBoards()
        raise EnvironmentError("Please select a board.")

    url = 'https://api.trello.com/1/boards/{}/lists{}&cards=none&fields=name'.format(boardId, trello.getSecurity())
    listsJson = requests.get(url).json()
    nameList = {}
    idList = {}
    for trelloList in listsJson:
        nameList[trelloList['name']] = trelloList['id']
        idList[trelloList['id']] = trelloList['name']

    return (nameList, idList) # return lists both ways, one with name as key and one id as a key

def getListDetail(boardId):
    url = 'https://api.trello.com/1/boards/{}/lists?cards=none&fields=all'.format(boardId)
    response = trello.get(url)
    #print(json.dumps(response.json(),indent=4))
    return response.json()

def addList(boardId, name):
    url = "https://api.trello.com/1/boards/{}/lists?name={}&pos=bottom".format(boardId, name)

    response = trello.post(url)
    if not response.ok:
        print ("Failed to add list due to {}: {}".format(response.status_code, response.text))

    return response

def getLabels(boardId):
    url = "https://api.trello.com/1/boards/{}/labels{}".format(boardId, trello.getSecurity())
    querystring = {"fields":"all","limit":"0"}
    response = requests.request("GET", url, params=querystring)

    labels = {}
    for label in response.json():
        labels[label['name']] = label['id']

    return labels

def getLabelDetail(boardId):
    url = "https://api.trello.com/1/boards/{}/labels?fields=all".format(boardId)
    response = trello.get(url)
    print(json.dumps(response.json(),indent=4))
    return response.json()

def addLabel(boardId, name, colour):
    url = "https://api.trello.com/1/boards/{}/labels?name={}&color={}".format(boardId, name, colour)

    response = trello.post(url)
    if not response.ok:
        print ("Failed to add label due to {}: {}".format(response.status_code, response.text))
    return response

if __name__ == "__main__":
    ## create user json
    board_id = '5c4adffeb3561e33298068b6'


from django.shortcuts import render
from django.views.generic import TemplateView

from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status

from .models import Post
from .serializers import *

@api_view(['GET', 'POST'])
def posts(request):
    print(request.__dict__)
    print('method: ', request.method)
    print('data: ', request.data)
    if request.method == 'GET':
        posts = Post.objects.all()
        serializer = PostSerializer(posts, many=True)
        return Response({'data': serializer.data}, headers={'Access-Control-Allow-Origin': '*'})
    elif request.method == 'POST':
        post = Post()
        post.text = request.data['text']
        post.save()
        return Response(status=status.HTTP_200_OK, headers={'Access-Control-Allow-Origin': '*'})

@api_view(['DELETE'])
def delete_post(request, post_id):
    try:
        post = Post.objects.get(id=post_id)
    except:
        return Response(status=status.HTTP_400_BAD_REQUEST, headers={'Access-Control-Allow-Origin': '*'})
    post.delete()
    return Response(status=status.HTTP_200_OK, headers={'Access-Control-Allow-Origin': '*'})

@api_view(['GET'])
def like_post(request, post_id):
    if request.method == 'GET':
        try:
            post = Post.objects.get(id=post_id)
        except:
            return Response(status=status.HTTP_400_BAD_REQUEST)

    setattr(post, 'likesCount', post.likesCount + 1)
    post.save()
    return Response(post.likesCount, status.HTTP_200_OK)

class AppView(TemplateView):
    template_name = 'default.html'


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Sample DNA sequence data with labels
data = {
    'sequence': ['ATCGATCGATCG', 'GGGGTTAGTTAC', 'AGCTTAGCGGCT', 'CCCTAGGCGGAC', 'TAGGCTAAGCTA'],
    'label': ['coding', 'noncoding', 'coding', 'noncoding', 'coding']
}

# Create a DataFrame to store the data
df = pd.DataFrame(data)

# One-hot encode the DNA sequences
encoded_sequences = []
for seq in df['sequence']:
    encoded_seq = []
    for base in seq:
        if base == 'A':
            encoded_seq.extend([1, 0, 0, 0])  # A: 1000
        elif base == 'T':
            encoded_seq.extend([0, 1, 0, 0])  # T: 0100
        elif base == 'C':
            encoded_seq.extend([0, 0, 1, 0])  # C: 0010
        elif base == 'G':
            encoded_seq.extend([0, 0, 0, 1])  # G: 0001
    encoded_sequences.append(encoded_seq)

df['encoded_sequence'] = encoded_sequences

# Split the data into training and testing sets
X = list(df['encoded_sequence'])  # Features (one-hot encoded DNA sequences)
y = list(df['label'])             # Labels
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train a random forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)

# Make predictions on the test set
y_pred = clf.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")


from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from .serializers import CategorySerializer, ProductSerializer
from .models import Category, Product
from .paginations import CustomPagination

class ListCategories(APIView):

    permission_classes = [permissions.BasePermission]

    def get(self, request, format=None):
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return Response({"results": serializer.data}, status=200)

class ListProducts(APIView, CustomPagination):

    #authentication_classes = [authentication.TokenAuthentication]
    permission_classes = [permissions.BasePermission]
    serializer_class = ProductSerializer
    pagination_class = CustomPagination

    def get(self, request, format=None):
        if request.GET.get('categoryId') is not None:
            category_id = request.GET.get('categoryId')
            try:
                category_id = int(category_id)
            except ValueError:
                return Response({"error": "categoryId invalid"}, status=400)
            products = Product.objects.filter(categoryId=category_id).order_by('id')
        else:
            products = Product.objects.all().order_by('id')
        results = self.paginate_queryset(products, request, view=self)
        serializer = ProductSerializer(results, many=True)
        return self.get_paginated_response(serializer.data)


from django.db import models
from django.conf import settings
from django.core.validators import MinValueValidator

class Category(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Categories"

class Drink(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    price = models.FloatField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Order(models.Model):
    customer_name = models.CharField(max_length=128, default="guest")
    order_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    drinks = models.ManyToManyField(
        Drink,
        through='OrderDrink',
        through_fields=('order', 'drink')
    )

    def __str__(self):
        return f'{self.id} - {self.customer_name}'

class OrderDrink(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    drink = models.ForeignKey(Drink, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1, validators=[MinValueValidator(1)])
    item_price = models.FloatField(null=True)
    total_price = models.FloatField(null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        if self.drink is not None:
            self.item_price = self.drink.price
            self.total_price = self.item_price * self.quantity
        super().save(force_insert, force_update, using, update_fields)


import socket
import tqdm
import os

SERVER_HOST = socket.gethostbyname(socket.gethostname())
SERVER_PORT = 50002
SEPARATOR = "<1111>"
BUFFER_SIZE = 4096

s= socket.socket()

s.bind((SERVER_HOST, SERVER_PORT))

s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
conn, addr = s.accept()
print(f"[+] {addr} is connected.")

received = conn.recv(BUFFER_SIZE).decode()
print(received)
filename, filesize = received.split(SEPARATOR)

filename = os.path.basename(filename)

filesize = int(filesize)
progress = tqdm.tqdm(range(filesize), f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)

with open("C:/Users/huy123/OneDrive/Desktop/file_server/"+filename,"wb") as f:
	while True:
		bytes_read = conn.recv(BUFFER_SIZE)
		if not bytes_read:
			break
		f.write(bytes_read)
		progress.update(len(bytes_read))

conn.close()
s.close()


from django.urls import path

from . import views

# recipes:recipe
app_name = 'recipes'

urlpatterns = [
     path('', views.RecipeListViewHome.as_view(), name='home'),
     path('recipes/search/', views.RecipeListViewSearch.as_view(), name='search'),  # noqa
     path('recipes/tags/<slug:slug>', views.RecipeViewTag.as_view(), name='tag'),  # noqa
     path('recipes/theory', views.theory, name='theory'),
     path('recipes/api/v1/',
          views.RecipeListViewHomeAPI.as_view(), name='api_home'),
     path('recipes/category/<int:category_id>/',
          views.RecipeListViewCategory.as_view(), name='category'),
     path('recipes/<int:recipe_id>/',
          views.RecipeDetailViewDetail.as_view(), name='recipe'),
     path('recipes/api/v1/<int:recipe_id>/',
          views.RecipeDetailViewDetailAPI.as_view(), name='api_detail'),

]


#!/usr/bin/env python
import cv2
import torch
import random

import rospy

from cv_bridge import CvBridge

from ultralytics import RTDETR

from sensor_msgs.msg import Image
from vision_msgs.msg import Detection2D
from vision_msgs.msg import ObjectHypothesisWithPose
from vision_msgs.msg import Detection2DArray
from std_srvs.srv import SetBool

class RT_DETR():

    def __init__(self) -> None:

        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        # params
        # self.model = rospy.get_param("model", "rtdetr-l.pt")

        # self.img_topic = rospy.get_param("img_topic", "/airsim_node/Drone_1/camera_1/Scene")

        # self.threshold = rospy.get_param("threshold", 0.5)

        # self.enable = rospy.get_param("enable", True)

        # # params
        self.model1 = "rtdetr-l.pt"
        self.model2 = "rtdetr-x.pt"
        self.bool_rtdetr_l = True

        self.img_topic = "/airsim_node/Drone_1/camera_1/Scene"

        self.threshold =  0.5

        self.enable =  True

        self._class_to_color = {}
        self.cv_bridge = CvBridge()
        if self.bool_rtdetr_l:
            self.rtdetr = RTDETR(self.model1)

        else:
            self.rtdetr = RTDETR(self.model2)

        rospy.sleep(1)

        # topics
        self._pub = rospy.Publisher("detections",Detection2DArray, queue_size= 10)
        self._detection_image_pub = rospy.Publisher("detection_image", Image, queue_size= 10)
        rospy.sleep(1)
        self._sub = rospy.Subscriber(self.img_topic, Image, self.image_cb)

        self.coco_names = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck',
                           8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird',
                           15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe',
                           24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
                           32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard',
                           38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
                           46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza',
                           54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet',
                           62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven',
                           70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear',
                           78: 'hair drier', 79: 'toothbrush'}

    def image_cb(self, msg: Image) -> None:

        if self.enable:

            # convert image + predict
            cv_image = self.cv_bridge.imgmsg_to_cv2(msg)

            results = self.rtdetr.predict(source = cv_image, verbose = False, show = False, save = False)

            # create detections msg
            detections_msg = Detection2DArray()
            detections_msg.header = msg.header

            results = results[0].cpu()

            for b in results.boxes:

                label = self.coco_names[int(b.cls)]
                score = float(b.conf)

                if score < self.threshold:
                    continue

                detection = Detection2D()

                detection.header = msg.header

                detection.source_img = msg

                box = b.xywh[0]

                # get boxes values
                detection.bbox.center.x = float(box[0])
                detection.bbox.center.y = float(box[1])
                detection.bbox.size_x = float(box[2])
                detection.bbox.size_y = float(box[3])

                # get track id
                track_id = 0
                if not b.id is None:
                    track_id = int(b.id)
                #detection.id = track_id

                # get hypothesis
                hypothesis = ObjectHypothesisWithPose()
                hypothesis.id = int(b.cls)
                hypothesis.score = score
                detection.results.append(hypothesis)

                # draw boxes for debug
                if label not in self._class_to_color:
                    r = random.randint(0, 255)
                    g = random.randint(0, 255)
                    b1 = random.randint(0, 255)
                    self._class_to_color[label] = (r, g, b1)
                color = self._class_to_color[label]

                min_pt = (round(detection.bbox.center.x - detection.bbox.size_x / 2.0),
                          round(detection.bbox.center.y - detection.bbox.size_y / 2.0))
                max_pt = (round(detection.bbox.center.x + detection.bbox.size_x / 2.0),
                          round(detection.bbox.center.y + detection.bbox.size_y / 2.0))
                cv_image = cv2.rectangle(cv_image, min_pt, max_pt, color, 2)

                label = "{}:({}) {:.3f}".format(label, str(track_id), score)
                pos = (min_pt[0], max(15, int(min_pt[1] - 10)))
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv_image = cv2.putText(cv_image, label, pos, font,
                            0.5, color, 1, cv2.LINE_AA)

                # append msg
                detections_msg.detections.append(detection)

            # publish detections and dbg image
            self._pub.publish(detections_msg)

            self._detection_image_pub.publish(self.cv_bridge.cv2_to_imgmsg(cv_image,
                                                            encoding=msg.encoding))
            cv2.imshow("Real-Time Detection with RTDETR",cv_image)
            cv2.waitKey(1)

            if rospy.is_shutdown():
                cv2.destroyAllWindows()

if __name__ == "__main__":
    rospy.init_node("RTDETR_Sea", anonymous= True)
    RT_DETR()
    rospy.spin()


import copy
from os.path import dirname, exists, join

import numpy as np
import pytest
import torch

def _demo_mm_inputs(num_kernels=0, input_shape=(1, 3, 300, 300),
                    num_items=None):  # yapf: disable
    """Create a superset of inputs needed to run test or train batches.

    Args:
        input_shape (tuple): Input batch dimensions.

        num_items (None | list[int]): Specifies the number of boxes
            for each batch item.
    """

    (N, C, H, W) = input_shape
    rng = np.random.RandomState(0)
    imgs = rng.rand(*input_shape)

    img_metas = [{
        'img_shape': (H, W, C),
        'ori_shape': (H, W, C),
        'pad_shape': (H, W, C),
        'filename': '<demo>.png',
    } for _ in range(N)]
    relations = [torch.randn(10, 10, 5) for _ in range(N)]
    texts = [torch.ones(10, 16) for _ in range(N)]
    gt_bboxes = [torch.Tensor([[2, 2, 4, 4]]).expand(10, 4) for _ in range(N)]
    gt_labels = [torch.ones(10, 11).long() for _ in range(N)]

    mm_inputs = {
        'imgs': torch.FloatTensor(imgs).requires_grad_(True),
        'img_metas': img_metas,
        'relations': relations,
        'texts': texts,
        'gt_bboxes': gt_bboxes,
        'gt_labels': gt_labels
    }
    return mm_inputs

def _get_config_directory():
    """Find the predefined detector config directory."""
    try:
        # Assume we are running in the source mmocr repo
        repo_dpath = dirname(dirname(dirname(__file__)))
    except NameError:
        # For IPython development when this __file__ is not defined
        import mmocr
        repo_dpath = dirname(dirname(mmocr.__file__))
    config_dpath = join(repo_dpath, 'configs')
    if not exists(config_dpath):
        raise Exception('Cannot find config path')
    return config_dpath

def _get_config_module(fname):
    """Load a configuration as a python module."""
    from mmcv import Config
    config_dpath = _get_config_directory()
    config_fpath = join(config_dpath, fname)
    config_mod = Config.fromfile(config_fpath)
    return config_mod

def _get_detector_cfg(fname):
    """Grab configs necessary to create a detector.

    These are deep copied to allow for safe modification of parameters without
    influencing other tests.
    """
    config = _get_config_module(fname)
    config.model.class_list = None
    model = copy.deepcopy(config.model)
    return model

@pytest.mark.parametrize('cfg_file', [
    'kie/sdmgr/sdmgr_novisual_60e_wildreceipt.py',
    'kie/sdmgr/sdmgr_unet16_60e_wildreceipt.py'
])
def test_sdmgr_pipeline(cfg_file):
    model = _get_detector_cfg(cfg_file)
    model['pretrained'] = None

    from mmocr.models import build_detector
    detector = build_detector(model)

    input_shape = (1, 3, 128, 128)

    mm_inputs = _demo_mm_inputs(0, input_shape)

    imgs = mm_inputs.pop('imgs')
    img_metas = mm_inputs.pop('img_metas')
    relations = mm_inputs.pop('relations')
    texts = mm_inputs.pop('texts')
    gt_bboxes = mm_inputs.pop('gt_bboxes')
    gt_labels = mm_inputs.pop('gt_labels')

    # Test forward train
    losses = detector.forward(
        imgs,
        img_metas,
        relations=relations,
        texts=texts,
        gt_bboxes=gt_bboxes,
        gt_labels=gt_labels)
    assert isinstance(losses, dict)

    # Test forward test
    with torch.no_grad():
        batch_results = []
        for idx in range(len(img_metas)):
            result = detector.forward(
                imgs[idx:idx + 1],
                None,
                return_loss=False,
                relations=[relations[idx]],
                texts=[texts[idx]],
                gt_bboxes=[gt_bboxes[idx]])
            batch_results.append(result)

    # Test show_result
    results = {'nodes': torch.randn(1, 3)}
    boxes = [[1, 1, 2, 1, 2, 2, 1, 2]]
    img = np.random.rand(5, 5, 3)
    detector.show_result(img, results, boxes)


from pickle import NONE
from re import sub
from tkinter.constants import ANCHOR, FALSE, TRUE, Y
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from tkinter import PhotoImage, filedialog
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
import time
from datetime import date
import webbrowser
import sys
import os
import threading
from PyQt5.QtWidgets import QApplication, QMainWindow
from FacebookUI import Ui_Form
from PyQt5 import QtGui

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

LOGIN_URL = 'https://m.facebook.com/login'

status = None
msgG = None
frame = None
intervalG = None
keywordG = None
maxGrps = None
postToGroups = None

class FaceBookGroupPoster():
    def __init__(self):
        self.groups = []
        self.images = []
        self.videos = []
        self.discussGrp = False

    #If you delete "do not delethis", it wont work, even that value is not used.
    def startFaceBookGroupPoster(self, frame, doNotDeleteThis):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("detach", True)
        chrome_options.add_argument("--disable-infobars")
        chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
        prefs = {"profile.default_content_setting_values.notifications": 2}
        chrome_options.add_experimental_option("prefs", prefs)
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
        self.driver.get(LOGIN_URL)
        frame.setText("Make sure you are logged in before proceeding")
        frame.setStyleSheet("color: green;")

    def terminate(self):
        self.driver.close()

    def postGroups(self):
        global msgG, frame, intervalG, keywordG, maxGrps, postToGroups

        msg = msgG.toPlainText().encode("utf-8")
        interval =  intervalG.text()
        keyword = keywordG.text()
        maxGrp = int(maxGrps.text())
        frame.setText(f'Ok Executing the program, Please wait patiently, once its done we will update here.')
        try:
            int(interval)
        except ValueError:
            frame.setText("Please enter a number as value as timout")
            frame.setStyleSheet("color: red;")
            return
        try:
            if len(self.groups) <= 0:
                if len(keyword) <= 0:
                    frame.setText("No groups and keyword loaded \n Make sure you load the groups or keyword before continue")
                    frame.setStyleSheet("color: red;")
                    return
                else:
                    try:
                        maxGrpInt = int(maxGrp)
                        self.getJoinedGroups(frame,keyword, maxGrpInt)
                    except ValueError:
                        frame.setText("Please enter a number as maximum groups.")
                        frame.setStyleSheet("color: red;")
                        return

            elif len(self.images) == 0 and len(msg) == 0:
                frame.setText("First select images or type a message")
                frame.setStyleSheet("color: red;")
                return
            frame.setText(f' We will be trying to post to total of {len(self.groups)} groups with a interval of {interval} seconds')
            try:
                f = open("logs.txt", "a", encoding="utf-8")
                f.write(
                    f'\n\n\n Logs for {msg} at {date.today()} \n ==========================================================================================\n')
                f.close()
                for group in self.groups:
                    self.driver.get(group)
                    time.sleep(5)
                    print(f'trying to post to {group}')
                    try:
                        print("Checking for group TYPE")
                        discuss = self.driver.find_element_by_css_selector("button[value='Discuss']")
                        self.discussGrp = True
                        print("Group Type: Buy and Sell grp (Try to post as discuss)")
                        discuss.click()
                        time.sleep(3)
                    except Exception as e:
                        print("Group Type: Normal Facebook Group")
                        self.discussGrp = False

                    try:
                        if self.discussGrp:
                            postStart = WebDriverWait(self.driver, 15).until(EC.presence_of_element_located(
                            (By.XPATH, '//div[starts-with(text(),"Write something")]')))
                        else:
                            postStart = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
                            (By.XPATH, '//div[starts-with(text(),"Write something...")]')))
                    except Exception:
                        try:
                            postStart = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
                                (By.XPATH, '//div[starts-with(text(),"Create a public")]')))
                        except Exception:
                            try:
                                postStart = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
                                (By.XPATH, '//div[starts-with(text(),"What\'s on your mind")]')))
                            except Exception:
                                try:
                                    postStart = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
                                    (By.XPATH, "//textarea[@id='uniqid_1']")))
                                except Exception:
                                    try:
                                        postStart = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
                                        (By.TAG_NAME, 'textarea')))
                                    except Exception as e:
                                        print(f'Tried to get the postStart Btn but could not with the error {e}\n Tried "Write Something, Write something..., Create a public post, whats on your mind, unique_id1 and tag name == textarea')

                    time.sleep(1)
                    try:
                        postStart.click()
                    except Exception as e:
                        try:
                            ActionChains(self.driver).move_to_element(
                            postStart).click().perform()
                        except Exception as e:
                            print(f'The create post btn found but could not click, Trying to send text to input box anyway.')
                    try:
                        txtBox = WebDriverWait(self.driver, 5).until(
                        EC.presence_of_element_located((By.XPATH, "//textarea[last()]")))
                    except Exception:
                        try:
                            txtBox = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                            (By.XPATH, "//textarea[@id='uniqid_1']")))
                        except Exception:
                            try:
                                txtBox = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                (By.TAG_NAME, 'textarea')))
                            except Exception as e:
                                print(f'Could not find the txtBox with the error {e}\n Tried "textareal[last()], unique_id1 and tag name == textarea')
                    try:
                        txtBox.send_keys(msg.decode('utf8', 'ignore'))
                        time.sleep(2)
                    except Exception as e:
                        try:
                            ActionChains(self.driver).move_to_element(
                            txtBox).send_keys(msg.decode('utf8', 'ignore')).perform()
                        except Exception as e:
                            print(f'Text box found but could not send texts with error {e}')
                        time.sleep(2)

                    for photo in self.images:
                        try:
                            photo_element = WebDriverWait(self.driver, 5).until(
                                EC.presence_of_element_located((By.XPATH, "//input[@name='file1']")))
                            photo_element.send_keys(photo)
                            time.sleep(2)
                        except Exception as e:
                            print(f'Could not send {photo} with the error {e}')

                    for video in self.videos:
                        try:
                            video_element = WebDriverWait(self.driver, 5).until(
                                EC.presence_of_element_located((By.XPATH, "//input[@name='file2']")))
                            video_element.send_keys(video)
                            time.sleep(2)
                        except Exception as e:
                            print(f'Could not send Video {video} with the error {e}')

                    time.sleep(5)
                    try:
                        time.sleep(2)
                        post = WebDriverWait(self.driver, 5).until(
                            EC.presence_of_element_located((By.CSS_SELECTOR, "div[class='acw'] button[value='Post']")))
                        post.click()
                    except Exception as e:
                        print('Trying to post using the last method. (usually this happens in buy and sell grp). But you can ignore this as long as program executes.')
                        time.sleep(5)
                        try:
                            txtBox = WebDriverWait(self.driver, 5).until(
                            EC.presence_of_element_located((By.XPATH, "//textarea[last()]")))
                            txtBox.submit()
                        except Exception:
                            pass
                    time.sleep(5)
                    f = open("logs.txt", "a")
                    f.write(f'{group} processed.\n ')
                    f.close()
                    print(f'Hey I am waiting for {interval} seconds, as you mentioned')
                    time.sleep(int(interval))
                    time.sleep(2)
            except Exception as e:
                print(f'ERROR OCCURRED: {e}')
                f = open("logs.txt", "a")
                f.write(f'FAILED Posting to {msg}: {e}\n')
                f.close()
        except Exception as e:
            print(f'ERROR OCCURRED: {e}')
            f = open("logs.txt", "a")
            f.write(f'FAILED Posting to {msg}: {e}\n')
            f.close()
            postToGroups.exit()
        print("PROGRAM EXECUTING FINISHED \n =============================================== \n Thanks for using facebook group poster pro.")
        frame.setText("All done, Check the logs.txt file at your current folder")
        frame.setStyleSheet("color: green;")
        self.groups = []

    def getImages(self, frame):
        filenames = filedialog.askopenfilenames(
            initialdir="/", title="Select Image", filetypes=(("JPG", "*.jpg"), ("PNG Files", "*.png"), ("all files", "*.*")))
        for img in filenames:
            self.images.append(img)

        frame.setText(f'{frame.text()}\n{len(self.images)} images loaded')
        frame.setStyleSheet("color: green;")

    def addGroups(self, frame):
        filename = filedialog.askopenfilename(
            initialdir="/", title="Select file", filetypes=(("Texts Files", "*.txt"), ("all files", "*.*")))
        print(f'{filename}')
        with open(filename, 'r') as f:
            try:
                tempGroups = f.read()
                tempGroups = tempGroups.split(',')
                tempAllGroups = [x for x in tempGroups if x.strip()]
                for group in tempAllGroups:
                    group = group.replace("www.facebook.com", "m.facebook.com")
                    self.groups.append(group)
                frame.setText(f'{frame.text()}\n {len(self.groups)} groups loaded')

                frame.setStyleSheet("color: green;")
            except Exception as e:
                frame.setText(f'error: {e}')
                frame.setStyleSheet("color: red;")

    def clearImagesVideos(self, frame):
        self.images.clear()
        frame.setText("All images have been cleared")
        frame.setStyleSheet("color: green;")

    def ClearGroups(self, frame):
        self.groups.clear()
        frame.setText("All groups have been cleared. Add another '.txt' file containing group links.")
        frame.setStyleSheet("color: green;")

    def getJoinedGroups(self, frame, keyword, maxGrp):
            frame.setText(f"Collectting some groups for the keyword {keyword}")
            frame.setStyleSheet("color: green;")
            self.driver.get(f'https://m.facebook.com/search/groups/?q={keyword}')
            time.sleep(5)
            if True:
                i = 1
                getGroups = True
                repeat = 1
                while getGroups:
                    try:
                        if (len(self.groups)>= maxGrp):
                            getGroups = False
                        path = f"/html[1]/body[1]/div[1]/div[1]/div[4]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[{i}]"
                        grpLink = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, path)))
                        grpLink.location_once_scrolled_into_view
                        grpLink.click()

                        try:
                            postStart = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                (By.XPATH, '//div[starts-with(text(),"Write something")]')))
                        except Exception:
                            try:
                                postStart = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                    (By.XPATH, '//div[starts-with(text(),"Create a public")]')))
                            except Exception:
                                try:
                                    postStart = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                    (By.XPATH, '//div[starts-with(text(),"What\'s on your mind")]')))
                                except Exception:
                                    try:
                                        postStart = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                        (By.XPATH, "//textarea[@id='uniqid_1']")))
                                    except Exception:
                                        try:
                                            postStart = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
                                            (By.TAG_NAME, 'textarea')))
                                        except Exception as e:
                                            pass
                        if str(type(postStart)) == "<class 'selenium.webdriver.remote.webelement.WebElement'>":
                            self.groups.append(self.driver.current_url)
                        else:
                            print("You dont have permission to post to this group, so ignoring this group...")
                        self.driver.back()

                        i = i+1
                        repeat = repeat * 0
                        frame.setText(f"Ok We have collected {len(self.groups)} groups so far")
                        frame.setStyleSheet("color: green;")
                        time.sleep(3)
                    except Exception as e:
                        repeat = repeat +1
                        if (repeat > 10):
                            getGroups = False
                        i = i+1
                        print("There was a issue processing this group, so ignoring... You can check logs.txt for details")
                        f = open("logs.txt", "a", encoding="utf-8")
                        f.write(
                        f'\n\n\n A group was ignored because of  \n {e}')
                        f.close()

class FacebookGroupPosterGUI(FaceBookGroupPoster):
    def __init__(self):
        self.main_win = QMainWindow()
        self.ui = Ui_Form()
        self.ui.setupUi(self.main_win)
        super().__init__()
        self.ui.statusText.setText("In a moment, The Chrome browser will open. Once opened login to your fb account.")
        self.ui.statusText.setStyleSheet("color: green;")
        self.ui.clearGrpsBtn.clicked.connect(lambda: self.ClearGroups(self.ui.statusText))
        self.ui.clearImages.clicked.connect(lambda:self.clearImagesVideos(self.ui.statusText))
        self.ui.addImageBtn.clicked.connect(lambda:self.getImages(self.ui.statusText))
        self.ui.addGrpBtn.clicked.connect(lambda:self.addGroups(self.ui.statusText))
        global msgG, frame, intervalG, keywordG, maxGrps, postToGroups
        msgG = self.ui.postText
        frame = self.ui.statusText
        intervalG =  self.ui.timer
        keywordG = self.ui.keyword
        maxGrps = self.ui.maxGrpToFetch
        self.ui.start.clicked.connect(self.startPosting)
        self.ui.exitBtn.clicked.connect(self.close)
        autoStart = threading.Thread(target=self.startFaceBookGroupPoster, args=(self.ui.statusText,"1"))
        autoStart.run()

    def startPosting(self):
        postToGroups = threading.Thread(target=self.postGroups)
        if postToGroups.is_alive():
            postToGroups.join()
            postToGroups.start()
        else:
            postToGroups.start()

    def show(self):
        self.main_win.show()

    def close(self):
        self.main_win.close()
        self.terminate()


import time
import numpy as np
import pandas as pd
import scipy as sp
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import accuracy_score, roc_auc_score
from Utils import pickle

np.random.seed(123)
time_0 = time.time()

X_train = sp.sparse.load_npz("Objects/X_train.npz")  # [1000:1050,:40]
Y_train = np.load("Objects/Y_train.npy")  # [1000:1050]

X_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size=0.5, random_state=456)
#X_test = sp.sparse.load_npz("Objects/X_train.npz")[3000:3050,:40]
#Y_test = np.load("Objects/Y_train.npy")[3000:3050]

num_tasks = Y_train.shape[1]
time_1 = time.time()
classifier = OneVsRestClassifier(LinearSVC())
classifier.fit(X_train, Y_train)
time_2 = time.time()

results = {}
results.update({"train_time": time_2-time_1})
pred = classifier.predict(X_train)
results.update({"acc": np.array([accuracy_score(Y_train[:,i], pred[:,i]) for i in range(num_tasks)])})
results.update({"acc_mean": np.mean(results["acc"])})
results.update({"roc": np.array([roc_auc_score(Y_train[:,i], pred[:,i]) for i in range(num_tasks)])})
results.update({"roc_mean": np.mean(results["roc"])})
pred = classifier.predict(X_test)
results.update({"val_acc": np.array([accuracy_score(Y_test[:,i], pred[:,i]) for i in range(num_tasks)])})
results.update({"val_acc_mean": np.mean(results["val_acc"])})
results.update({"val_roc": np.array([roc_auc_score(Y_test[:,i], pred[:,i]) for i in range(num_tasks)])})
results.update({"val_roc_mean": np.mean(results["val_roc"])})

#nrows = 10
X_test = sp.sparse.load_npz("Objects/X_test.npz")  # [:nrows,:X_train.shape[1]]
Y_test = classifier.predict(X_test)
submission = pd.read_csv("../Data/sample_submission.csv")  # , nrows=nrows)
submission.iloc[:,1:] = Y_test
submission.to_csv("Objects/submission.csv", index=False)

results.update({"script_time": time.time()-time_0})

pickle.save("Objects/results.dict", results)


from django.conf.urls import url
from . import views

app_name = 'election'

urlpatterns = [
    url(r'^createElection/(?P<ecId>\w+)/$', views.createElection, name='createElection'),
    url(r'^candidateList/(?P<voterId>\w+)/$', views.candidateList, name='candidateList'),
    url(r'^adminPanel/(?P<ecId>\w+)$', views.adminPanel, name='adminPanel'),
    url(r'^addCandidate/(?P<ecId>\w+)$', views.addCandidate, name='addCandidate'),
    url(r'^resultAnalysis/(?P<ecId>\w+)$', views.resultAnalysis, name='resultAnalysis'),
]


import os
from warnings import warn
from collections.abc import Mapping
from .. import yaml_io
import numpy as np

from .. Error import GroupMissingDataError
from . Group import Group, Descriptor
from . Scheme import GroupAdditivityScheme
from . DataDir import get_data_dir

class GroupLibrary(Mapping):
    """Represent library of contributing properties organized by group.

    The set of properties that may be represented in a :class:`GroupLibrary` is
    extensible.  Contributing properties are represented by *property sets*
    organized by group.  See the manual for a list of available property sets.

    .. note::
        Because the *property set* system is extensible, the module within
        which a particular *property set* is defined and registered must be
        imported before loading a group library that contains data for that
        type of *property set*.

    To estimate properties, call :meth:`GroupLibrary.estimate()` with the
    *property set* `name` and  set of groups contained in the chemical
    structure of interest.  The properties estimate will be returned as an
    object whose type depends on the particular *property set*.

    To determine which groups are present in a particular chemical structure,
    use :meth:`GroupLibrary.match_groups()`.

    Data in multiple group libraries can be combined so long as the groups
    they contain are defined within compatible schemes.  See
    :meth:`GroupLibrary.update()`.

    """
    _property_set_estimator_types = {}
    _property_set_group_yaml_types = {}

    @classmethod
    def register_property_set_type(cls, name, group_yaml_type, estimator_type):
        """(class method) Register new property set type.

        Parameters
        ----------
        name : str
            Name of new property set type.
        group_yaml_type : str
            Name of property set type in the YAML type namespace.
        estimator_type : class
            The provided class is instantiated when an estimate is to be made
            for a particular set of groups.  The constructor should accept the
            following parameters:

                library : :class:`GroupLibrary`
                    library which is to be used to estimate these properties.
                groups : mapping
                    Map from :class:`Group` to int or float specifying counts
                    of each group in the chemical structure.
        """
        if name in cls._property_set_group_yaml_types:
            raise KeyError('Property set %r already registered.' % name)
        cls._property_set_group_yaml_types[name] = group_yaml_type
        cls._property_set_estimator_types[name] = estimator_type

    def __init__(self, scheme, contents={}, uq_contents={}, path=None):
        """Initialize library of contributing properties organized by group.

        Parameters
        ----------
        scheme : :class:`GroupScheme`
            Specify group-additivity scheme to use.
        contents : mapping or list
            Define initial contents of the library either as mapping or list of
            (`key`, `value`) pairs.  See the last paragraph of the class
            documentation for information on the format.

        Other Parameters
        ----------------
        path : str
            File-system path library was loaded from or should be saved to by
            default.
        """
        self.scheme = scheme
        self.path = path
        if isinstance(contents, Mapping):
            contents = list(contents.items())
        self.contents = dict((group, property_sets)
                             for (group, property_sets) in contents)
        self.uq_contents = uq_contents

    def GetDescriptors(self, mol):
        """Determine groups appearing in chemical structure `chem`.

        Parameters
        ----------
        mol : :class:`rdkit.mol`
            Specify chemical structure to match groups for.
        manual_descriptors : mapping, optional
            Specify value(s)/degree(s) of influence of additional descriptors
            to include.

        Returns
        -------
        groups : mapping
            Map from :class:`Group` to int or float identifying groups and
            their number of occurence in the structure.
        """
        self.name = mol
        return self.scheme.GetDescriptors(mol)

    def Estimate(self, groups, property_set_name):
        """Estimate set of properties for chemical.

        Parameters
        ----------
        groups : mapping (dictionary)
            Map from :class:`Group` to int or float specifying counts of each
            group in the chemical structure.
        property_set_name : str
            Name of property set to estimate.

        Returns
        -------
        estimated_properties : (varies)
            The estimated properties, an object whose type depends on the
            particular property set.
        """
        if property_set_name not in self._property_set_estimator_types:
            raise KeyError('Invalid property_set name: %r' % property_set_name)
        # Verify groups present.
        missing_groups = [group for group in groups
                          if property_set_name not in self[group]]
        if missing_groups:
            raise GroupMissingDataError(missing_groups, property_set_name)
        estimator_type = self._property_set_estimator_types[property_set_name]
        return estimator_type(self, groups)

    def __contains__(self, group):
        """Test if this library contains contributing properties for `group`.

        Parameters
        ----------
        group : :class:`Group`
            Group whose membership is being tested.

        Returns
        -------
        result : bool
            True if this library has properties for `group`.
        """
        return group in self.contents

    def __iter__(self):
        """Return iterator over all groups with property data in this library.
        """
        return iter(self.contents)

    def __len__(self):
        """Return number of groups with properties in this library."""
        return len(self.contents)

    def __getitem__(self, group):
        """Return contributing properties sets for `group`.

        If no properties exist for `group`, then return ``{}`` instead of
        raising an exception.

        Parameters
        ----------
        group : :class:`Group`
            Identify group whose property sets are to be retrieved.

        Returns
        -------
        property_sets : dict
            Sets of contributing properties for `group`.
        """
        return self.contents.get(group, {})

    @classmethod
    def Load(cls, path):
        """(class method) Load group-additivity library from file-system
        `path` or builtin.

        Parameters
        ----------
        path : str
            Specify either the path to a file containing the data or a symbolic
            name of a builtin library to load (*e.g.* ``gas_benson`` to load
            gas phase Benson groups.)

        Returns
        -------
        lib : :class:`GroupLibrary`
            Group library containing the loaded data.
        """
        if os.sep not in path and '.' not in path and not os.path.exists(path):
            # [JTF] where's our data directory?
            base_path = os.path.join(get_data_dir(), path)
            # We want to load the library.yaml in that directory:
            path = os.path.join(base_path, 'library.yaml')
        else:
            # The base path is the directory containing whatever file/directory
            # is referenced by path:
            base_path = os.path.dirname(path)

        # Load the scheme.yaml from the selected data directory:
        scheme = GroupAdditivityScheme.Load(os.path.join(base_path,
                                                         'scheme.yaml'))

        # Use that scheme to load the rest of the library:
        return cls._do_load(path, base_path, scheme)

    @classmethod
    def _Load(cls, path, scheme):
        if os.sep not in path and '.' not in path and not os.path.exists(path):
            # [JTF] where's our data directory?
            base_path = os.path.join(get_data_dir(), path)
            # We want to load the library.yaml in that directory:
            path = os.path.join(base_path, 'library.yaml')
        else:
            # The base path is the directory containing whatever file/directory
            # is referenced by path:
            base_path = os.path.dirname(path)

        # Use the scheme passed to us to load the rest of the library:
        return cls._do_load(path, base_path, scheme)

    @classmethod
    def _do_load(cls, path, base_path, scheme):
        # Read data from file.
        context = {'base_path': base_path}
        with open(path) as f:
            lib_data = yaml_io.load(
                yaml_io.parse(f.read()), context, loader=cls._yaml_loader)

        context['units'] = lib_data.units

        group_properties = lib_data.groups
        other_descriptor_properties = lib_data.other_descriptors
        UQ = lib_data.UQ

        if cls._property_set_group_yaml_types:
            # Prepare property_sets loader.
            property_sets_loader = yaml_io.make_object_loader(yaml_io.parse(
                '\n'.join(('%r:\n    type: %r\n    optional: true'
                          % (str(name),
                             str(cls._property_set_group_yaml_types[name])))
                          for name in cls._property_set_group_yaml_types)))
            # Read all properties.
            lib_contents = {}
            for name in group_properties:
                group = Group.parse(scheme, name)
                if group in lib_contents:
                    raise KeyError('Multiple definitions of group %s' % group)
                property_sets = yaml_io.load(
                    group_properties[name], context,
                    loader=property_sets_loader)
                lib_contents[group] = property_sets
            for name in other_descriptor_properties:
                descriptor = Descriptor(scheme, name)
                if descriptor in lib_contents:
                    raise KeyError('Multiple definitions of descriptor %s' %
                                   descriptor)
                property_sets = yaml_io.load(
                    other_descriptor_properties[name], context,
                    loader=property_sets_loader)
                lib_contents[descriptor] = property_sets

            # Read UQ data
            uq_contents = {}
            if UQ:
                uq_contents['RMSE'] = yaml_io.load(
                    UQ['RMSE'], context,
                    loader=property_sets_loader)
                uq_contents['descriptors'] = UQ['InvCovMat']['groups']
                uq_contents['mat'] = np.array(UQ['InvCovMat']['mat'])
                uq_contents['dof'] = UQ['DOF']
        else:
            # No property sets defined.
            warn('GroupLibrary.load(): No property sets defined.')
            lib_contents = {}
            uq_contents = {}

        new_lib = cls(scheme, lib_contents, uq_contents, path=path)
        # Update with included content.
        for include_path in lib_data.include:
            new_lib.Update(cls._Load(os.path.join(base_path,
                                                  include_path), scheme))
        return new_lib

    def Update(self, lib, overwrite=False):
        """Add complete contents of `lib` into this library.

        Parameters
        ----------
        lib : :class:`GroupLibrary`
            Library to import from.
        overwrite : bool
            If True, then existing data may be overwritten by data from `lib`.
        """
        for (group, other_property_sets) in list(lib.items()):
            if group not in self.contents:
                self.contents[group] = {}
            property_sets = self.contents[group]

            for name in other_property_sets:
                if name not in property_sets:
                    property_sets[name] = other_property_sets[name].copy()
                else:
                    property_sets[name].update(
                        other_property_sets[name], overwrite)
        # UQ stuff can only be loaded once
        if self.uq_contents and lib.uq_contents:
            raise ValueError('More than one uncertainty quantification',
                             'information provided')
        if not self.uq_contents:
            self.uq_contents = lib.uq_contents
    _yaml_loader = yaml_io.make_object_loader(yaml_io.parse("""
units:
    type: mapping
    default: {}

include:
    type: list
    item_type: string
    default: []

groups:
    type: mapping
    default: {}

other_descriptors:
    type: mapping
    default: {}

UQ:
    type: mapping
    default: {}
"""))


import sys
from setuptools import setup

install_requires = ["h2>=2.2.0,<3.0.0"]
if sys.version_info < (3, 5):
    install_requires.append("typing")

setup(
    name='asyncio-apns',
    version='0.0.1',
    install_requires=install_requires,
    packages=['asyncio_apns'],
    url='https://github.com/etataurov/asyncio-apns',
    license='MIT',
    author='etataurov',
    author_email='tatauroff@gmail.com',
    description='asyncio client for Apple Push Notification Service'
)


from kernel.base.base import *
import os
import zipfile
import shutil
import pickle
import base64
import time
import pytesseract
import re
from paddleocr import PaddleOCR, draw_ocr
from urllib.parse import urlparse
from PIL import Image
import filetype
# from pysubs2 import SSAFile, SSAEvent, make_time
import pysubs2

class File(BaseClass):

    def __init__(self,args):
        pass

    def b64encode(self,data_file):
        if self.isfile(data_file):
            print(os.path.splitext(data_file))
            extent_file = os.path.splitext(data_file)[1]
            extent_file = extent_file.replace(".","")
            suffix = "data:image/{};base64,".format(extent_file)
            data = self.open(data_file, 'b')
        else:
            suffix = ""
            data = data_file
        data = base64.b64encode(data)
        data = self.com_string.byte_to_str(data)
        data = suffix + data
        return data

    def image_to_str(self,image_url, lang):
        im = Image.open(image_url)
        im = im.convert('L')
        print(f"im {im}")
        im_str = pytesseract.image_to_string(im, lang=lang)
        return im_str

    def image_to_str_from_paddleorc(self,img_path,lang="ch"):
        # ocr = PaddleOCR(use_angle_cls=True, )
        # # 输入待识别图片路径
        # img_path = r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png"
        # # 输出结果保存路径
        # result = ocr.ocr(img_path, cls=True)
        # for line in result:
        #     print(line)
        #
        ocr = PaddleOCR(use_angle_cls=True, lang="en")  # need to run only once to download and load model into memory
        result = ocr.ocr(img_path, cls=True)
        return {
            "text":result[0][1][0],
            "data":result
        }
    def rename_remove_space(self,filename):
        space_pattern  = re.compile(r"\s+")
        file_space = re.findall(space_pattern, filename)
        if (len(file_space) > 0):
            new_file_name = os.path.basename(filename)
            new_file_name = re.sub(space_pattern,"",new_file_name )
            file_basedir = os.path.dirname(filename)
            new_file = os.path.join(file_basedir, new_file_name)
            self.cut(filename, new_file)
            filename =new_file
        return filename
    def read_file(self,file_name,encoding="utf-8",info=False):
        return self.load_file(file_name,encoding,info=info)

    def load_file(self,file_name,encoding="utf-8",info=False):
        file_name = self.get_path(file_name)
        file_object = self.get_file_encode(file_name,encoding=encoding,info=info)
        content = None
        if file_object != None:
            content = file_object.get("content")
        return content

    def get_file_encode(self,file_name,encoding=None,info=False):
        codings = [
            "utf-8",
            "utf-16",
            "utf-16le",
            "utf-16BE",
            "gbk",
            "gb2312",
            "us-ascii",
            "ascii",
            "IBM037",
            "IBM437",
            "IBM500",
            "ASMO-708",
            "DOS-720",
            "ibm737",
            "ibm775",
            "ibm850",
            "ibm852",
            "IBM855",
            "ibm857",
            "IBM00858",
            "IBM860",
            "ibm861",
            "DOS-862",
            "IBM863",
            "IBM864",
            "IBM865",
            "cp866",
            "ibm869",
            "IBM870",
            "windows-874",
            "cp875",
            "shift_jis",
            "ks_c_5601-1987",
            "big5",
            "IBM1026",
            "IBM01047",
            "IBM01140",
            "IBM01141",
            "IBM01142",
            "IBM01143",
            "IBM01144",
            "IBM01145",
            "IBM01146",
            "IBM01147",
            "IBM01148",
            "IBM01149",
            "windows-1250",
            "windows-1251",
            "Windows-1252",
            "windows-1253",
            "windows-1254",
            "windows-1255",
            "windows-1256",
            "windows-1257",
            "windows-1258",
            "Johab",
            "macintosh",
            "x-mac-japanese",
            "x-mac-chinesetrad",
            "x-mac-korean",
            "x-mac-arabic",
            "x-mac-hebrew",
            "x-mac-greek",
            "x-mac-cyrillic",
            "x-mac-chinesesimp",
            "x-mac-romanian",
            "x-mac-ukrainian",
            "x-mac-thai",
            "x-mac-ce",
            "x-mac-icelandic",
            "x-mac-turkish",
            "x-mac-croatian",
            "utf-32",
            "utf-32BE",
            "x-Chinese-CNS",
            "x-cp20001",
            "x-Chinese-Eten",
            "x-cp20003",
            "x-cp20004",
            "x-cp20005",
            "x-IA5",
            "x-IA5-German",
            "x-IA5-Swedish",
            "x-IA5-Norwegian",
            "x-cp20261",
            "x-cp20269",
            "IBM273",
            "IBM277",
            "IBM278",
            "IBM280",
            "IBM284",
            "IBM285",
            "IBM290",
            "IBM297",
            "IBM420",
            "IBM423",
            "IBM424",
            "x-EBCDIC-KoreanExtended",
            "IBM-Thai",
            "koi8-r",
            "IBM871",
            "IBM880",
            "IBM905",
            "IBM00924",
            "EUC-JP",
            "x-cp20936",
            "x-cp20949",
            "cp1025",
            "koi8-u",
            "iso-8859-1",
            "iso-8859-2",
            "iso-8859-3",
            "iso-8859-4",
            "iso-8859-5",
            "iso-8859-6",
            "iso-8859-7",
            "iso-8859-8",
            "iso-8859-9",
            "iso-8859-13",
            "iso-8859-15",
            "x-Europa",
            "iso-8859-8-i",
            "iso-2022-jp",
            "csISO2022JP",
            "iso-2022-jp",
            "iso-2022-kr",
            "x-cp50227",
            "euc-jp",
            "EUC-CN",
            "euc-kr",
            "hz-gb-2312",
            "GB18030",
            "x-iscii-de",
            "x-iscii-be",
            "x-iscii-ta",
            "x-iscii-te",
            "x-iscii-as",
            "x-iscii-or",
            "x-iscii-ka",
            "x-iscii-ma",
            "x-iscii-gu",
            "x-iscii-pa",
            "utf-7",
       ]
        if encoding != None:
            codings = [encoding] + codings
        index = 0
        while index < len(codings):
            encoding = codings[index]
            try:
                f = open(file_name, f"r+",encoding=encoding)
                content = f.read()
                if info==True:
                    self.com_util.print_info(f"Successfully mode {encoding} to {file_name}")
                f.close()
                result = {
                    "encoding":encoding,
                    "content":content
                }
                return result
            except Exception as e:
                self.com_util.print_warn(f"file open error, {file_name}")
                self.com_util.print_warn(e)
                index+=1
        return None

    def open(self,file_name,encoding="utf-8"):
        content = self.load_file(file_name,encoding=encoding)
        return content

    def read(self,file_name,encoding="utf-8"):
        content = self.load_file(file_name,encoding=encoding)
        return content

    def load_js(self,file_name,encoding="utf-8"):
        template_dir = self.com_config.get_js_dir()
        file_path = os.path.join(template_dir,file_name)
        content = self.load_file(file_path,encoding)
        return content

    def load_html(self,file_name,encoding="utf-8"):
        template_dir = self.com_config.get_template_dir()
        file_path = os.path.join(template_dir,file_name)
        content = self.load_file(file_path,encoding)
        return content

    def get_default_save_dir(self):
        save_dir = self.com_config.get_public("save_dir")
        return save_dir

    def save(self,file_name=None,content=None,encoding=None,overwrite=False):
        return self.save_file(file_name, content, encoding=encoding, overwrite=overwrite)

    def create_file_name(self,prefix='',suffix=""):
        filename = self.com_string.random_string(16)
        save_time = self.save_time()
        if prefix != '':
            prefix = f"{prefix}_"
        filename = f"{prefix}{save_time}_{filename}{suffix}"
        filedir = self.com_config.get_public("save_dir")
        filesavedir = os.path.join(filedir,filename)
        return filesavedir

    def save_time(self,):
        t = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
        return t

    def save_file(self,file_name=None,content=None,encoding=None,overwrite=False):
        if file_name == None:
            print("the save Content not accessed.{file_name}%s}" % file_name)
            return None
        if content == None:
            content = file_name
            file_name = self.create_file_name()
        basename = os.path.dirname(file_name)
        if basename == "":
            basename = self.get_default_save_dir()
            file_name = os.path.join(basename,file_name)
        print(f"save_file ：{file_name}")
        if os.path.exists(basename) is not True and os.path.isdir(basename) is not True:
            self.mkdir(basename)
        if not self.isfile(file_name):
            overwrite = True

        if encoding == None and type(content) == str:
            encoding = "utf-8"
        if overwrite ==True:
            m = "w"
        else:
            m = "a"
        print(f"encoding {encoding}")
        if encoding == None or encoding == "binary":
            f = open(file_name, f"{m}b+")
            content = content.encode(encoding)
        else:
            f = open(file_name, f"{m}+",encoding=encoding)
        try:
            # self.com_util.print_warn(f"savefile_encoding {encoding}")
            f.write(content)
        except:
            f.close()
            content = self.com_string.byte_to_str(content)
            f = open(file_name, f"{m}+",encoding="utf-8")
            f.write(content)
        f.close()
        return file_name
    def read_file_bytes(self,file_name):
        file_name = self.get_path(file_name)
        if self.isfile(file_name):
            with open(file_name, "rb") as f:
                data = f.read()
            return data
        else:
            return None

    def zip_extract(self,file,member,o=None):
        if o == None:
            o = os.path.dirname(file)
        with zipfile(file) as f:
            f.extract(member,o)

    def zip_extractall(self,file,odir=None,member=None):
        if odir == None:
            odir = os.path.dirname(file)
        with zipfile.ZipFile(file) as f:
            f.extractall(odir,member)
        return odir
    def file_extract(self, filename):
        """
        #输助函数,将最后下载的文件解压和删除。
        :param filename:
        :return:
        """
        extract_dir = os.path.dirname(filename)
        print(f"extract file {filename} to {extract_dir}")
        return self.zip_extractall(filename,extract_dir)

    def remove(self,top_path):
        print("delete : " ,top_path)
        for root, dirs, files in os.walk(top_path, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))
        if os.path.isdir(top_path):
            os.rmdir(top_path)
        elif os.path.isfile(top_path):
            os.remove(top_path)

    def mkdir(self, dir):
        if os.path.exists(dir) and os.path.isdir(dir):
            return False
        else:
            os.makedirs(dir,exist_ok=True)
            return True

    def isFile(self, path):
        return self.isfile(path)
    def isfile(self, path):
        if type(path) is not str:
            return False
        path = self.get_path(path)
        if os.path.exists(path) and os.path.isfile(path) and os.path.getsize(path) > 0:
            return True
        else:
            return False

    def get_path(self,path):
        if os.path.exists(path) and os.path.isfile(path):
            return path
        public_dir = self.com_config.get_public(path)
        if os.path.exists(public_dir) and os.path.isfile(public_dir):
            return public_dir
        path = os.path.join(self.getcwd(),path)
        if os.path.exists(path) and os.path.isfile(path):
            return path
        else:
            return path

    def isdir(self, path):
        if type(path) is not str:
            return False
        if os.path.exists(path) and os.path.isdir(path) :
            return True
        else:
            return False

    def copy(self, src, dst):
        return shutil.copyfile(src, dst)
    def cut(self, src, dst):
        dst_basedir = os.path.dirname(dst)
        self.mkdir(dst_basedir)
        shutil.copyfile(src, dst)
        os.remove(src)
    def rmtree(self, dir):
        shutil.rmtree(dir)

    def dir_to_localurl(self, filename):
        control_name = self.load_module.get_control_name()
        filename = filename.split(control_name)[-1]
        filename = self.dir_normal(filename,linux=True)
        filename = filename.lstrip('/')
        return filename

    def file_type(self, filename):
        if self.isfile(filename) == False:
            return None
        kind = filetype.guess(filename)
        if kind is None:
            return ""
        else:
            if kind.extension == None:
                return ""
            return kind.extension
    def file_to(self,filename,encoding="utf-8"):
        file_object = self.get_file_encode(filename)
        if file_object == None:
            return False
        file_encode = file_object.get('encoding')
        if file_encode != encoding:
            content = self.read_file(filename)
            # content = content.encode(encoding)
            os.remove(filename)
            self.save(filename,content,encoding=encoding)
        return True

    def dir_to(self,filedir,encoding="utf-8"):
        files = os.listdir(filedir)
        for file in files:
            file = os.path.join(filedir, file)
            self.file_to(file,encoding)

    # def srt_to_doc(self,filename):
    #

    def srt(self, filename,language="en"):
        pattern = re.compile(r'^.+}')
        english_pattern = re.compile(r'^[a-zA-Z\-0-9\"]')
        chinese_pattern = re.compile(r"^([\u4e00-\u9fa5]|[\ufe30-\uffa0]|[\u4e00-\uffa5])+")
        encoding = self.get_file_encode(filename)
        encoding = encoding.get('encoding')
        subs = pysubs2.load(filename, encoding=encoding)
        subs.shift(s=2.5)
        strs = []
        for line in subs:
            if language == "en":
                text = line.text
                text = re.sub(pattern,"",text)
                if re.search(chinese_pattern,text) == None:
                    strs.append(text)
                else:
                    try:
                        origin_text = text
                        trans = self.com_translate.translate_from_google(origin_text,dest="en",src="zh-CN",)
                        text = trans.get('text')
                        print(f"be translated {origin_text} to {text}")
                        strs.append(text)
                    except Exception as e:
                        self.com_util.print_warn(e)
            else:
                text = line.text
        return strs

    def file_suffix(self,file_path,suffix):
        file_path = file_path.split('.')[0:-1]
        suffix = suffix.replace('.',"")
        file_path.append(suffix)
        file_path = ".".join(file_path)
        return file_path

    def get_template_dir(self,filename=None,fulllink=False):
        control_dir = self.load_module.get_control_dir()
        if filename == None:
            filename = ""
        elif filename and fulllink == True:
            top_symbol = re.compile(r'^\/')
            filename = re.sub(top_symbol, '', filename)
        template_folder = self.com_config.get_global("flask_template_folder")
        template_folder = os.path.join(control_dir,template_folder)
        filename = os.path.join(template_folder, filename)
        return filename

    def get_static_dir(self,filename=None,fulllink=False):
        control_dir = self.load_module.get_control_dir()
        print(control_dir)
        if filename == None:
            filename = ""
        elif filename and fulllink == True:
            top_symbol = re.compile(r'^\/')
            filename = re.sub(top_symbol, '', filename)
        template_folder = self.com_config.get_global("flask_static_folder")
        template_folder = os.path.join(control_dir,template_folder)
        filename = os.path.join(template_folder, filename)
        return filename


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-

'''
ccsliders

Midi fader sending cc messages

'''

from tkinter import *

import time
import rtmidi
from rtmidi.midiutil import open_midiinput
from rtmidi.midiconstants import (CHANNEL_PRESSURE, CONTROLLER_CHANGE, NOTE_ON, NOTE_OFF,
                                  PITCH_BEND, POLY_PRESSURE, PROGRAM_CHANGE)

from mido import MidiFile
import mido
import sys
sys.path.append('libs/')
import midi3
midi3.check()

#mididest = 'to Maxwell 1'
mididest =  'electribe2 SOUND'
mididest =  'BCR2000'
cc0 = 0
cc1 = 82
cc2 = 83
cc3 = 84
midichannel = 1

ccs = [0,0,0,0]

# /cc cc number value
def sendcc(ccnumber, value, dest):

    print("Sending Midi channel", midichannel, "cc", ccnumber, "value", value, "to", dest)
    midi3.MidiMsg([CONTROLLER_CHANGE+midichannel-1, ccnumber, value], dest)

class Interface(Frame):

    """Notre fenêtre principale.
    Tous les widgets sont stockés comme attributs de cette fenêtre."""

    def __init__(self, myframe, **kwargs):
        Frame.__init__(self, myframe, width=768, height=576, bg="black", **kwargs)
        self.pack(fill=NONE)
        self.nb_clic = 0

        # Création de nos widgets
        self.message = Label(self, text="Maxwellator",  bg="black", foreground="white")
        self.message.place(x = 0, y = 25)
        self.message.pack()
        #self.message.config(bg="black", foreground="white")

        self.w1 = Scale(self, from_=127, to=0,  bg="black", foreground="white")
        self.w1.pack(side="left")
        self.w2 = Scale(self, from_=127, to=0, bg="black", foreground="white")
        self.w2.pack(side="left")
        self.w3 = Scale(self, from_=127, to=0,  bg="black", foreground="white")
        self.w3.pack(side="left")
        self.w4 = Scale(self, from_=127, to=0,  bg="black", foreground="white")
        self.w4.pack(side="left")

        self.w5 = Scale(self, from_=127, to=0,  bg="black", foreground="white")
        self.w5.pack(padx =1, pady=30)

        self.quit_button = Button(self, text="Quit", command=self.quit)
        #self.quit_button.configure(background = "green")
        self.quit_button.pack(side="bottom")
        self.update_cc()
        '''
        self.bouton_cliquer = Button(self, text="Cliquez ici", fg="red",
                command=self.cliquer)
        self.bouton_cliquer.pack(side="right")
        '''

    def update_cc(self):

        cc0value = int(self.w1.get())
        cc1value = int(self.w2.get())
        cc2value = int(self.w3.get())
        cc3value = int(self.w4.get())

        if cc0value !=  ccs[0]:
            sendcc(cc0, cc0value, mididest)
            ccs[0] = cc0value
            newcc = "CC", cc0, ":", str(cc0value)
            #print(newcc)
            self.message.configure(text = newcc)

        if cc1value !=  ccs[1]:
            sendcc(cc1, cc1value, mididest)
            ccs[1] = cc1value
            newcc = "CC", cc1, ":", str(cc1value)
            #print(newcc)
            self.message.configure(text = newcc)

        if cc2value !=  ccs[2]:
            sendcc(cc2, cc2value, mididest)
            ccs[2] = cc2value
            newcc = "CC", cc2, ":", str(cc2value)
            #print(newcc)
            self.message.configure(text = newcc)

        if cc3value !=  ccs[3]:
            sendcc(cc3, cc3value, mididest)
            ccs[3] = cc3value
            newcc = "CC", cc3, ":", str(cc3value)
            print(newcc)
            self.message.configure(text = newcc)

        #print( 'CC', cc0,':', PrettyFloat(cc1value), ' CC', cc1, ":", PrettyFloat(cc1value),' CC', cc2, ":", PrettyFloat(cc2value)," CC",cc3,":",PrettyFloat(cc3value))
        '''
        with open('/tmp/ws.json', 'w') as outfile:
            json.dump(data, outfile)
        '''

        self.after(100, self.update_cc)
    '''
    def cliquer(self):
        """Il y a eu un clic sur le bouton.

        On change la valeur du label message."""

        self.nb_clic += 1
        self.message["text"] = "Vous avez cliqué {} fois.".format(self.nb_clic)
    '''

myframe = Tk()
interface = Interface(myframe)
interface.mainloop()
interface.destroy()


"""
    * Bump Stalatites con Neat, (Redes neuronales)
"""

# * Declaración de librerías
import pygame
import random
import os
import neat

pygame.font.init()

# * Variables usadas
WIN_WIDTH = 600
WIN_HEIGHT = 800
FLOOR = 730
STAT_FONT = pygame.font.SysFont("Console", 50, bold=True)
END_FONT = pygame.font.SysFont("Console", 70, bold=True)
DRAW_LINES = False

# * Ventana principal
WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Bump Stactite")

# * Llamado de imágenes
pipe_img = pygame.transform.scale2x(pygame.image.load(
    os.path.join("imgs", "pipe.png")).convert_alpha())
bg_img = pygame.transform.scale(pygame.image.load(
    os.path.join("imgs", "bg.png")).convert_alpha(), (600, 900))
bird_images = [pygame.transform.scale2x(pygame.image.load(
    os.path.join("imgs", "bird" + str(x) + ".png"))) for x in range(1, 4)]
base_img = pygame.transform.scale2x(pygame.image.load(
    os.path.join("imgs", "base.png")).convert_alpha())

gen = 0

# * Clase donde se declara la nave

class Pipe():
    """
        * Clase Pipe:
        * Objeto tipo Pipa o estalactita.
    """
    GAP = 200
    VEL = 5

    def __init__(self, x):
        """
            * Inicialización del objeto
        """
        self.x = x
        self.height = 0

        self.top = 0
        self.bottom = 0

        self.PIPE_TOP = pygame.transform.flip(pipe_img, False, True)
        self.PIPE_BOTTOM = pipe_img

        self.passed = False

        self.set_height()

    def set_height(self):
        """
            * Esta función, establece la altura de la Estalactita desde la parte más abajo de la pantalla
        """
        self.height = random.randrange(50, 450)
        self.top = self.height - self.PIPE_TOP.get_height()
        self.bottom = self.height + self.GAP

    def move(self):
        """
            * Mueve la Estalactita en función de la velocidad.
            TODO : Incluir nivel de mas velocidad acorde a la puntuación.
        """
        self.x -= self.VEL * 1.5

    def draw(self, win):
        """
            * Dibuja la parte de arriba y de abajo de la Estalactita
        """
        # Dibuja pipa de arriba.
        win.blit(self.PIPE_TOP, (self.x, self.top))
        # Dibuja pipa de abajo.
        win.blit(self.PIPE_BOTTOM, (self.x, self.bottom))

    def collide(self, bird, win):
        """
            * Función de colisión.
        """
        bird_mask = bird.get_mask()
        top_mask = pygame.mask.from_surface(self.PIPE_TOP)
        bottom_mask = pygame.mask.from_surface(self.PIPE_BOTTOM)
        top_offset = (self.x - bird.x, self.top - round(bird.y))
        bottom_offset = (self.x - bird.x, self.bottom - round(bird.y))

        b_point = bird_mask.overlap(bottom_mask, bottom_offset)
        t_point = bird_mask.overlap(top_mask, top_offset)

        if b_point or t_point:
            return True

        return False

class Bird:
    """
        * Clase Bird que representa el juego
    """
    MAX_ROTATION = 25
    IMGS = bird_images
    ROT_VEL = 20
    ANIMATION_TIME = 5

    def __init__(self, x, y):
        """
            * Inicializando el objeto:
        """
        self.x = x
        self.y = y
        self.tilt = 0  # Inclinación de la nave.
        self.tick_count = 0
        self.vel = 0
        self.height = self.y
        self.img_count = 0
        self.img = self.IMGS[0]

    def jump(self):
        """
            * Hace que la nave salte.
        """
        self.vel = -10.5
        self.tick_count = 0
        self.height = self.y

    def move(self):
        """
            * Movimientos de la nave
        """
        self.tick_count += 1

        # * Ir hacia abajo:
        displacement = self.vel*(self.tick_count) + 0.5 * \
            (3)*(self.tick_count)**2  # * Calcula el desplazamiento.

        # * Velocidad:
        if displacement >= 16:
            displacement = (displacement/abs(displacement)) * 16

        if displacement < 0:
            displacement -= 2

        self.y = self.y + displacement

        if displacement < 0 or self.y < self.height + 50:  # La inclina hacia arriba
            if self.tilt < self.MAX_ROTATION:
                self.tilt = self.MAX_ROTATION
        else:  # Si no, la inclina hacia abajo
            if self.tilt > -90:
                self.tilt -= self.ROT_VEL

    def draw(self, win):
        """
            * Dibujo del la nave (HitBox)
        """

        # * Inclinacion de la nave
        blitRotateCenter(win, self.img, (self.x, self.y), self.tilt)

    def get_mask(self):
        """
            * Obtiene una superficie la imagen y la adapta a la hit box
        """
        return pygame.mask.from_surface(self.img)

class Base:
    """
        * Representa el movimiento de la base de abajo (Especie de superficie marciana)
    """
    VEL = 5
    WIDTH = base_img.get_width()
    IMG = base_img

    def __init__(self, y):
        """
            * Inicialización del objeto.
        """
        self.y = y
        self.x1 = 0
        self.x2 = self.WIDTH

    def move(self):
        """
            * Movimiento del piso.
        """
        self.x1 -= self.VEL
        self.x2 -= self.VEL
        if self.x1 + self.WIDTH < 0:
            self.x1 = self.x2 + self.WIDTH

        if self.x2 + self.WIDTH < 0:
            self.x2 = self.x1 + self.WIDTH

    def draw(self, win):
        """
            * Dibujando la Hitbox de la superficie.
        """
        win.blit(self.IMG, (self.x1, self.y))
        win.blit(self.IMG, (self.x2, self.y))

def blitRotateCenter(surf, image, topleft, angle):
    """
        * Organización de la superficie con respecto a la pantalla.
    """
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(
        center=image.get_rect(topleft=topleft).center)

    surf.blit(rotated_image, new_rect.topleft)

def draw_window(win, birds, pipes, base, score, gen, pipe_ind):
    """
        * Dibujando la pantalla para el la ventana principal main_game loop
    """
    if gen == 0:
        gen = 1
    win.blit(bg_img, (0, 0))

    for pipe in pipes:
        pipe.draw(win)

    base.draw(win)
    for bird in birds:
        # ?
        if DRAW_LINES:
            try:
                pygame.draw.line(win, (255, 0, 0), (bird.x+bird.img.get_width()/2, bird.y + bird.img.get_height(
                )/2), (pipes[pipe_ind].x + pipes[pipe_ind].PIPE_TOP.get_width()/2, pipes[pipe_ind].height), 5)
                pygame.draw.line(win, (255, 0, 0), (bird.x+bird.img.get_width()/2, bird.y + bird.img.get_height(
                )/2), (pipes[pipe_ind].x + pipes[pipe_ind].PIPE_BOTTOM.get_width()/2, pipes[pipe_ind].bottom), 5)
            except:
                pass
        # * Dibuja la hb de la nave en pantalla.
        bird.draw(win)

    # * Texto generaciones
    score_label = STAT_FONT.render("Generación: " + str(gen), 1, (0, 0, 0))
    win.blit(score_label, (10, 10))

    # * Texto de los que aun quedan vivos.
    score_label = STAT_FONT.render(
        "Sobrevivientes: " + str(len(birds)), 1, (0, 0, 0))
    win.blit(score_label, (10, 50))

    # * Puntuación
    score_label = STAT_FONT.render("Puntaje: " + str(score), 1, (0, 0, 0))
    win.blit(score_label, (10, 90))

    pygame.display.update()

def eval_genomes(genomes, config):
    """
        * Ejecuta la simulacion de la población de naves y establece
        * su Fitness basado en la distancia que ellos alcanzan dentro del juego.
    """
    global WIN, gen
    win = WIN
    gen += 1

    # * Inicia con crear listas que contengan su genoma,
    # * la red neuronal asociada con el genoma y
    # * El objeto de tipo nave que usa la red para jugar.
    nets = []
    birds = []
    ge = []
    for genome_id, genome in genomes:
        genome.fitness = 0  # Inicia con un Fitness de 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        nets.append(net)
        birds.append(Bird(230, 350))
        ge.append(genome)

    base = Base(FLOOR)
    pipes = [Pipe(700)]
    score = 0

    clock = pygame.time.Clock()

    run = True
    while run and len(birds) > 0:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()

        pipe_ind = 0
        if len(birds) > 0:
            # Determina cual usar, ya sea el primero o el segundo
            if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                # Estalactita en la pantalla para la entrada input de la red neuronal
                pipe_ind = 1
        # * Le damos a cada nave un Fitness de 0.1 por cada que atraviese el obstaculo
        for x, bird in enumerate(birds):
            ge[x].fitness += 0.1
            bird.move()

            # Envía la posición de la nave, la de la estalactita de arriba y la de abajo y determina desde la red neuronal
            # Sí saltar o no saltar.
            output = nets[birds.index(bird)].activate((bird.y, abs(
                bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom)))

            # Se utiliza una funcion de activacion tanh, así, el resultado será entre -1 y 1. Si es mayor que 0.5, este saltará.
            if output[0] > 0.5:
                bird.jump()

        base.move()

        rem = []
        add_pipe = False
        for pipe in pipes:
            pipe.move()
            # Verifica si hay colisión
            for bird in birds:
                if pipe.collide(bird, win):
                    ge[birds.index(bird)].fitness -= 1
                    nets.pop(birds.index(bird))
                    ge.pop(birds.index(bird))
                    birds.pop(birds.index(bird))

            if pipe.x + pipe.PIPE_TOP.get_width() < 0:
                rem.append(pipe)

            if not pipe.passed and pipe.x < bird.x:
                pipe.passed = True
                add_pipe = True

        if add_pipe:
            score += 1
            # Esta linea dará más y más recompensa al pasar mediante los obstaculos.
            for genome in ge:
                genome.fitness += 4
            pipes.append(Pipe(WIN_WIDTH))

        for r in rem:
            pipes.remove(r)

        for bird in birds:
            if bird.y + bird.img.get_height() - 10 >= FLOOR or bird.y < -50:
                nets.pop(birds.index(bird))
                ge.pop(birds.index(bird))
                birds.pop(birds.index(bird))

        draw_window(WIN, birds, pipes, base, score, gen, pipe_ind)

        # Esta linea, rompe el programa si este supera el tope máximo de puntos.
        if score > 100:
            print(" Felicidades, ha aprendido satisfactoriamente. ")
            break

def run(config_file):
    """
        * Ejecuta la librería NEAT con su respectiva config-file
    """
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                config_file)

    # Creación de la población que se usará para el aprendizaje
    p = neat.Population(config)

    # Estas lineas, muestran información por la terminal de diferentes aspectos del algoritmo
    p.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    # p.add_reporter(neat.Checkpointer(5))

    # Ejecuta hasta 50 generaciones
    winner = p.run(eval_genomes, 50)

    # Muestra los resultados Finales
    print('\n Mejor genoma:\n{!s}'.format(winner))

if __name__ == '__main__':

    # * Aquí se determina la dirección del archivo de configuración
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-feedforward.txt')
    run(config_path)


import graphene

def get_node(info, id, only_type=None):
    """Return node or throw an error if the node does not exist."""
    node = graphene.Node.get_node_from_global_id(info, id, only_type=only_type)
    if not node:
        raise Exception(
            "Could not resolve to a node with the global id of '%s'." % id)
    return node


from flask import request, render_template, redirect, url_for

from lib import template_list
from lib.auth import login_required, HR_ROLE, get_logged_user
from lib.core_integration import get_json_from_core
from routes.common.company import edit_company
from routes.hiring_manager.company_form import HMCompanyForm

@login_required(HR_ROLE)
def company_info():
    company_id = get_user_company_id()

    company = get_json_from_core('/api/company/' + company_id)

    form = HMCompanyForm(request.form)

    form.populate_form_from_core(company)

    return render_template(template_list.COMMON_EDIT_COMPANY,
                           form=form,
                           company_id=company_id,
                           form_action='hr_company_info_post')

@login_required(HR_ROLE)
def company_info_post():
    company_id = get_user_company_id()
    form = HMCompanyForm(request.form)

    edited = edit_company(form, company_id)

    if edited:
        return redirect(url_for('hr_company_info'))

    return render_template(template_list.COMMON_EDIT_COMPANY,
                           form=form,
                           company_id=company_id,
                           form_action='hr_company_info_post')

def get_user_company_id():
    user = get_logged_user()
    return user['company_id']


import argparse
import random
import torch
import torch.nn as nn
import torch.optim as optim
from model import CNN
from loss import Energy_Loss
from utils import *
import numpy as np
import time
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

def build_batch(examples):
    inputs = torch.zeros(1, len(examples), max(len(examples[0])+5, min_len), dtype=torch.long)
    for i, ex in enumerate(examples, 0):
        vec_examples = [tok2index[tok] for tok in ex]
        inputs[0][i][:len(ex)] = torch.tensor(
            vec_examples, dtype=torch.long)
    return inputs

def plot_margins(args, model, vocab):
    model.eval()
    vocabset = set(vocab)
    with torch.no_grad():
        for edit in [1, 2, 3, 4]:
            margin_matrix = torch.zeros(args.num_samples, args.max_k+1)
            for i in range(args.num_samples):
                examples = []
                word = list(random.choice(vocab))
                # compute the k-edit.
                examples.append(word)
                for k in range(1, args.max_k+1):
                    neg = get_edit(word, k, edit)
                    if "".join(neg) in vocabset:
                        examples.append([])
                    else:
                        examples.append(neg)
                inputs = build_batch(examples)
                if args.use_cuda:
                    inputs = inputs.cuda(args.device, non_blocking=True)
                outputs, _ = model(inputs)
                margin = outputs - outputs[0][0]
                margin_matrix[i] = margin

            avgs = [0]
            lbs = [0]
            ubs = [0]
            for col in range(1, args.max_k+1):
                energies = margin_matrix[:,col]
                avgs.append(torch.mean(energies).item())
                lbs.append(np.percentile(energies, 5))
                ubs.append(np.percentile(energies, 95))
            print(avgs)
            print(lbs)
            print(ubs)
            avgs = np.array(avgs)
            lbs = np.array(lbs)
            ubs = np.array(ubs)

            plt.figure()
            plt.errorbar(range(0, args.max_k+1), avgs, yerr=[avgs-lbs, ubs-avgs], fmt='-s')
            plt.savefig('edit{}_margin.png'.format(edit))

def type_edit_pca(args, model, vocab):
    model.eval()
    vocabset = set(vocab)
    with torch.no_grad():
        data_matrix = torch.zeros(args.num_samples, 5, 1000)
        labels = torch.zeros(args.num_samples, 5)
        for i in range(args.num_samples):
            examples = []
            word = list(random.choice(vocab))
            # compute the k-edit.
            examples.append(word)
            for edit in [1, 2, 3, 4]:
                neg = get_edit(word, 1, edit)
                if "".join(neg) in vocabset:
                    examples.append([])
                else:
                    examples.append(neg)
            inputs = build_batch(examples)
            if args.use_cuda:
                inputs = inputs.cuda(args.device, non_blocking=True)
            outputs, reps = model(inputs)
            data_matrix[i] = reps
            labels[i] = torch.tensor([0, 1, 2, 3, 4], dtype=torch.long)
        data_matrix = data_matrix.view(args.num_samples*5,-1)
        labels = labels.view(args.num_samples*5,-1)
        print(data_matrix.size())
        print(labels.numpy().reshape((args.num_samples*5,)).tolist())
        pca = PCA(n_components=2)
        data_pca = pca.fit_transform(data_matrix)
        print(data_pca.shape)
        plt.scatter(data_pca[:,0], data_pca[:,1],s=1, alpha=0.75,
                    c=labels.numpy().reshape((args.num_samples*5,)).tolist(), cmap=plt.cm.get_cmap('Set1', 5))
        plt.colorbar()
        # plt.show()
        plt.savefig("edit_type_pca.png")

def k_edit_pca(args, model, vocab):
    model.eval()
    vocabset = set(vocab)
    with torch.no_grad():
        data_matrix = torch.zeros(args.num_samples, 5, 1000)
        labels = torch.zeros(args.num_samples, 5)
        for i in range(args.num_samples):
            examples = []
            word = list(random.choice(vocab))
            # compute the k-edit.
            examples.append(word)
            for k in range(1, args.max_k+1):
                neg = get_edit(word, k, 4)
                if "".join(neg) in vocabset:
                    examples.append([])
                else:
                    examples.append(neg)
            inputs = build_batch(examples)
            if args.use_cuda:
                inputs = inputs.cuda(args.device, non_blocking=True)
            outputs, reps = model(inputs)
            data_matrix[i] = reps
            labels[i] = torch.tensor([0, 1, 2, 3, 4], dtype=torch.long)
        data_matrix = data_matrix.view(args.num_samples*5,-1)
        labels = labels.view(args.num_samples*5,-1)
        print(data_matrix.size())
        print(labels.numpy().reshape((args.num_samples*5,)).tolist())
        pca = PCA(n_components=2)
        data_pca = pca.fit_transform(data_matrix)
        print(data_pca.shape)
        plt.scatter(data_pca[:,0], data_pca[:,1],s=1, alpha=0.75,
                    c=labels.numpy().reshape((args.num_samples*5,)).tolist(), cmap=plt.cm.get_cmap('Set1', 5))
        plt.colorbar()
        plt.savefig("rep_k_pca.png")

def main():
    parser = argparse.ArgumentParser(
        description='Visualizing margins for energy model')
    parser.add_argument('--vocab_file', type=str, default="data/wikipedia.vocab.txt",
                        help='vocabulary file path')
    parser.add_argument('--model_save_file', type=str, default='models/energy_cnn',
                        help='model save path')
    parser.add_argument('--topk', type=int, default=100000,
                        help="train on top k most common words in vocabulary")
    parser.add_argument('--max_k', type=int, default=4,
                        help="maximum number of edits 0-k")
    parser.add_argument('--num_samples', type=int, default=10000,
                        help="how many samples for each edit value")
    parser.add_argument('--log_rate', type=float, default=1000,
                        help='number of samples per log (default: 1000)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    args = parser.parse_args()

    args.use_cuda = not args.no_cuda and torch.cuda.is_available()
    args.device = torch.device("cuda" if args.use_cuda else "cpu")
    print("Using Device: {}".format(args.device))

    # instantiate CNN, loss, and optimizer.
    print("Loading Model from {}".format(args.model_save_file))
    model = CNN(n_chars, 10, 1, 256, [1, 2, 3, 4, 5, 6, 7, 8], 0.25, 1000, 1).to(
        device=args.device)
    # some weird loading, TODO: test this out on gpu.
    if args.use_cuda:
        model.load_state_dict(torch.load(args.model_save_file))
    else:
        model.load_state_dict(torch.load(
            args.model_save_file, map_location=lambda storage, loc: storage))

    # load vocabulary.
    vocab, _ = read_vocab(args.vocab_file, topk=args.topk)

    # plot_margins(args, model, vocab)
    # type_edit_pca(args, model, vocab)
    k_edit_pca(args, model, vocab)

if __name__ == "__main__":
    main()