from abc import ABC, abstractmethod
import numpy as np
import pandas as pd
from typing import Dict, List, Optional

from .utils import KeyHashable
from .utils.rules import Rule, make_single_feature_rule

class RuleSeeder(ABC, KeyHashable):
    """Base class for seed rule/LF generators."""

    @abstractmethod
    def get_seed_rules(self,
                       classes: np.array,
                       train_binary_features: pd.DataFrame,
                       train_y: pd.Series,
                       rngseed: int) -> List[Rule]:
        """Generates a set of seed rules/LFs."""
        pass

class BlankRS(RuleSeeder):
    """Generates no seed rules"""

    def get_seed_rules(self,
                       classes: np.array,
                       train_binary_features: pd.DataFrame,
                       train_y: pd.Series,
                       rngseed: int) -> List[Rule]:
        return []

class AccRS(RuleSeeder):
    """Generates seed rules for random features within a range of
    acceptable accuracies:

    min_cov_ratio: The minimum coverage for each seed rule.
    min_accuracy_gap: The minimum accuracy gap above a random classifier of seed rules.
    max_accuracy_gap: The maximum accuracy gap above a random classifier of seed rules.
    rpc: The number of seed rules to generate for each class.
    """

    def __init__(self,
                 min_cov_ratio: float = 0.02,
                 min_acc_gap: float = 0.2,
                 max_acc_gap: float = 0.35,
                 rpc: int = 2):
        self.min_cov_ratio = min_cov_ratio
        self.min_accuracy_gap = self.min_acc_gap = min_acc_gap
        self.max_accuracy_gap = self.max_acc_gap = max_acc_gap
        self.rules_per_class = self.rpc = rpc

    def check_accuracy(self, accuracy: float, *, class_count: int) -> bool:
        """Check whether the given accuracy is within the acceptable range."""
        chance_accuracy = 1 / class_count
        if (self.min_accuracy_gap is not None
                and accuracy < (chance_accuracy + self.min_accuracy_gap)):
            return False
        if (self.max_accuracy_gap is not None
                and accuracy > (chance_accuracy + self.max_accuracy_gap)):
            return False
        return True

    def get_potential_class_rules(self,
                                  classes: np.array,
                                  train_binary_features: pd.DataFrame,
                                  train_y: pd.Series) -> Dict[str, List[Rule]]:
        """Return all potential rules for each class that meet requirements."""
        column_mask = train_binary_features.mean() >= self.min_cov_ratio
        train_binary_features = train_binary_features.loc[:, column_mask]

        potential_class_rules: Dict[str, List[Rule]] = {
            target_class: [] for target_class in classes
        }
        for feature_name in train_binary_features.columns:
            covered_mask = train_binary_features[feature_name]
            covered_y = train_y[covered_mask]
            class_proportions = covered_y.value_counts(normalize=True, sort=True)
            most_frequent_class = class_proportions.index[0]
            if self.check_accuracy(class_proportions[most_frequent_class],
                                   class_count=classes.shape[0]):
                rule = make_single_feature_rule(most_frequent_class, feature_name)
                potential_class_rules[most_frequent_class].append(rule)
        return potential_class_rules

    def get_seed_rules(self,
                       classes: np.array,
                       train_binary_features: pd.DataFrame,
                       train_y: pd.Series,
                       rngseed: int) -> List[Rule]:
        potential_class_rules = self.get_potential_class_rules(
            classes=classes,
            train_binary_features=train_binary_features,
            train_y=train_y,
        )

        rng = np.random.RandomState(rngseed)
        seed_rules = []
        for target_class in classes:
            if len(potential_class_rules[target_class]) < self.rules_per_class:
                raise ValueError(f'Not enough potential seed rules for class: {target_class}')
            seed_rules += rng.choice(potential_class_rules[target_class],
                                     self.rules_per_class,
                                     replace=False).tolist()
        return seed_rules

class ClassSubsetAccRS(AccRS):
    """Generates seed rules for random features within a range of
    acceptable accuracies:

    min_cov_ratio: The minimum coverage for each seed rule.
    min_accuracy_gap: The minimum accuracy gap above a random classifier of seed rules.
    max_accuracy_gap: The maximum accuracy gap above a random classifier of seed rules.
    rpc: The number of seed rules to generate for each class.
    c: Number of random classes to generate seed rules for (all classes if None).
    """
    def __init__(self,
                 min_cov_ratio: float = 0.02,
                 min_acc_gap: float = 0.2,
                 max_acc_gap: float = 0.35,
                 rpc: int = 2,
                 c: Optional[int] = None):
        super().__init__(
            min_cov_ratio=min_cov_ratio,
            min_acc_gap=min_acc_gap,
            max_acc_gap=max_acc_gap,
            rpc=rpc,
        )
        self.class_count = self.c = c

    def get_seed_rules(self,
                       classes: np.array,
                       train_binary_features: pd.DataFrame,
                       train_y: pd.Series,
                       rngseed: int) -> List[Rule]:
        potential_class_rules = self.get_potential_class_rules(
            classes=classes,
            train_binary_features=train_binary_features,
            train_y=train_y,
        )
        potential_classes = [
            target_class for target_class in classes
            if len(potential_class_rules[target_class]) >= self.rules_per_class
        ]
        seed_class_count = self.c if self.c is not None else len(classes)
        if len(potential_classes) < seed_class_count:
            raise ValueError(f'Not enough potential rules for {seed_class_count} classes.')

        rng = np.random.RandomState(rngseed)
        seed_classes = (rng.choice(potential_classes, self.c, replace=False)
                        if self.c is not None else classes)
        seed_rules = []
        for target_class in seed_classes:
            seed_rules += rng.choice(potential_class_rules[target_class],
                                     self.rules_per_class,
                                     replace=False).tolist()
        return seed_rules


'''
Created on Nov 10, 2012

@author: peterb
'''

from sqlalchemy.types import Integer, String
from sqlalchemy.schema import Column
from model.base import Base, Common

ADMIN_ROLE = u'admin'
USER_ROLE = u'user'
DEV_ROLE = u'dev'
AUTOMATIC_ROLE = 'automatic'
REMOTE_ROLE = "remote"

class Permission(Base, Common):

    ROLES = [ADMIN_ROLE, USER_ROLE, DEV_ROLE, AUTOMATIC_ROLE, REMOTE_ROLE]

    id = Column(Integer, primary_key=True)
    name =  Column(String(80), nullable=False, unique=True)

    @classmethod
    def find_or_create(cls, session, name):
        result = session.query(cls).filter(cls.name==name).first()
        if result is None:
            result = cls(name=name)
            session.add(result)
            session.flush()
        return result


#Get the difference between a given number and 17
#if the number is greater than 17 return double the absolute difference.

n =int(input("Enter Number :"))

if n > 17 :
  a = 17 - n
  a = abs (a) *2
  print(a)

else :
  a = (17-n)
  print(a)

math-related/Difference Between a Number And 17.py


from PyQt5.QtWidgets import QDialog
from PyQt5 import uic
from PyQt5.Qt import Qt
from PyQt5.Qt import QShortcut
from PyQt5.Qt import QKeySequence
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QInputDialog
from PyQt5.Qt import QDateTime
from PyQt5.Qt import QStyle
from PyQt5.Qt import QNetworkCookie
from PyQt5.QtWidgets import QTreeWidgetItem
from mc.common.globalvars import gVar
from mc.app.Settings import Settings
from mc.common import const
from mc.tools.TreeWidget import TreeWidget
from mc.tools.IconProvider import IconProvider

class HashableTreeWidgetItem(QTreeWidgetItem):
    def __hash__(self):
        return id(self)

class CookieManager(QDialog):
    def __init__(self, parent=None):
        '''
        @param parent QWidget
        '''
        super().__init__(parent)
        self._ui = uic.loadUi('mc/cookies/CookieManager.ui', self)

        self._domainHash = {}  # QHash<QString, QTreeWidgetItem>
        self._itemHash = {}  # QHash<QTreeWidgetItem, QNetworkCookie>

        self.setAttribute(Qt.WA_DeleteOnClose)

        gVar.appTools.centerWidgetOnScreen(self)

        if self.isRightToLeft():
            self._ui.cookieTree.headerItem().setTextAlignment(0, Qt.AlignRight | Qt.AlignVCenter)
            self._ui.cookieTree.headerItem().setTextAlignment(1, Qt.AlignRight | Qt.AlignVCenter)
            self._ui.cookieTree.setLayoutDirection(Qt.LeftToRight)
            self._ui.whiteList.setLayoutDirection(Qt.LeftToRight)
            self._ui.blackList.setLayoutDirection(Qt.LeftToRight)

        # Stored Cookies
        self._ui.cookieTree.currentItemChanged.connect(self._currentItemChanged)
        self._ui.removeAll.clicked.connect(self._removeAll)
        self._ui.removeOne.clicked.connect(self._remove)
        self._ui.close.clicked.connect(lambda: self._close())
        self._ui.close2.clicked.connect(lambda: self._close())
        self._ui.close3.clicked.connect(lambda: self._close())
        self._ui.search.textChanged.connect(self._filterString)

        # Cookie Filtering
        self._ui.whiteAdd.clicked.connect(self._addWhitelist)
        self._ui.whiteRemove.clicked.connect(self._removeWhitelist)
        self._ui.blackAdd.clicked.connect(self._addBlacklist)
        self._ui.blackRemove.clicked.connect(self._removeBlacklist)

        # Cookie Settings
        settings = Settings()
        settings.beginGroup('Cookie-Settings')
        self._ui.saveCookies.setChecked(settings.value('allCookies', True))
        self._ui.filter3rdParty.setChecked(settings.value('filterThirdPartyCookies', False))
        self._ui.filterTracking.setChecked(settings.value('filterTrackingCookie', False))
        self._ui.deleteCookiesOnClose.setChecked(settings.value('deleteCookiesOnClose', False))
        self._ui.whiteList.addItems(settings.value('whitelist', []))
        self._ui.blackList.addItems(settings.value('blacklist', []))
        settings.endGroup()

        if const.QTWEBENGINEWIDGETS_VERSION < const.QT_VERSION_CHECK(5, 11, 0):
            self._ui.filter3rdParty.hide()

        self._ui.search.setPlaceholderText(_('Search'))
        self._ui.cookieTree.setDefaultItemShowMode(TreeWidget.ItemsCollapsed)
        self._ui.cookieTree.sortItems(0, Qt.AscendingOrder)
        self._ui.cookieTree.header().setDefaultSectionSize(220)
        self._ui.cookieTree.setFocus()

        self._ui.whiteList.setSortingEnabled(True)
        self._ui.blackList.setSortingEnabled(True)

        self._removeShortcut = QShortcut(QKeySequence('Del'), self)
        self._removeShortcut.activated.connect(self._deletePressed)

        self._ui.search.textChanged.connect(self._filterString)
        cookieJar = gVar.app.cookieJar()
        cookieJar.cookieAdded.connect(self._addCookie)
        cookieJar.cookieRemoved.connect(self._removeCookie)

        # Load cookies
        for cookie in cookieJar.getAllCookies():
            self._addCookie(cookie)

        gVar.appTools.setWmClass('Cookies', self)

    def _close(self):
        super().close()

    # private Q_SLOTS:
    def _currentItemChanged(self, current, parent):
        '''
        @param: current QTreeWidgetItem
        @param: parent QTreeWidgetItem
        '''
        if not current:
            return

        if not current.text(1):
            self._ui.name.setText(_('<cookie not selected>'))
            self._ui.value.setText(_("<cookie not selected>"))
            self._ui.server.setText(_("<cookie not selected>"))
            self._ui.path.setText(_("<cookie not selected>"))
            self._ui.secure.setText(_("<cookie not selected>"))
            self._ui.expiration.setText(_("<cookie not selected>"))

            self._ui.removeOne.setText(_("Remove cookies"))
            return

        cookie = current.data(0, Qt.UserRole + 10)
        self._ui.name.setText(cookie.name().data().decode())
        self._ui.value.setText(cookie.value().data().decode())
        self._ui.server.setText(cookie.domain())
        self._ui.path.setText(cookie.path())
        if cookie.isSecure():
            self._ui.secure.setText(_('Secure only'))
        else:
            self._ui.secure.setText(_('All connections'))
        if cookie.isSessionCookie():
            self._ui.expiration.setText(_('Session cookie'))
        else:
            self._ui.expiration.setText(
                QDateTime(cookie.expirationDate()).toString('hh:mm:ss dddd d. MMMM yyyy')
            )

        self._ui.removeOne.setText(_('Remove cookie'))

    def _remove(self):
        current = self._ui.cookieTree.currentItem()
        if not current:
            return

        cookies = []  # QList<QNetworkCookie>

        if current.childCount():
            for idx in range(current.childCount()):
                # QTreeWidgetItem
                item = current.child(idx)
                if item and item in self._itemHash:
                    cookies.append(self._itemHash[item])
        elif current in self._itemHash:
            cookies.append(self._itemHash[current])

        cookieJar = gVar.app.cookieJar()
        for cookie in cookies:
            cookieJar.deleteCookie(cookie)

    def _removeAll(self):
        button = QMessageBox.warning(self, _('Confirmation'),
            _('Are you sure you want to delete all cookies on your computer?'),
            QMessageBox.Yes | QMessageBox.No)

        if button != QMessageBox.Yes:
            return

        gVar.app.cookieJar().deleteAllCookies()

        self._itemHash.clear()
        self._domainHash.clear()
        self._ui.cookieTree.clear()

    def _addWhitelist(self):
        server, ok = QInputDialog.getText(self, _('Add to whitelist'),
                _('Server:'))
        if not server:
            return

        if self._ui.blackList.findItems(server, Qt.MatchFixedString):
            QMessageBox.information(self, _('Already blacklisted!'),
                _("The server \"%s\" is already in blacklist, please remove it first.") % server)
            return

        if not self._ui.whiteList.findItems(server, Qt.MatchFixedString):
            self._ui.whiteList.addItem(server)

    def _removeWhitelist(self):
        item = self._ui.whiteList.currentItem()
        self._removeTreeItem(self._ui.whiteList, item)

    def _addBlacklist(self):
        server, ok = QInputDialog.getText(self, _('Add to blacklist'),
                _('Server:'))
        self._addBlacklistByServer(server)

    def _removeBlacklist(self):
        item = self._ui.blackList.currentItem()
        self._removeTreeItem(self._ui.blackList, item)

    def _deletePressed(self):
        if self._ui.cookieTree.hasFocus():
            self._remove()
        elif self._ui.whiteList.hasFocus():
            self._removeWhitelist()
        elif self._ui.blackList.hasFocus():
            self._removeBlacklist()

    def _filterString(self, string):
        '''
        @param: string QString
        '''
        print('=====>', string)
        if not string:
            for idx in range(self._ui.cookieTree.topLevelItemCount()):
                item = self._ui.cookieTree.topLevelItem(idx)
                item.setHidden(False)
                item.setExpanded(self._ui.cookieTree.defaultItemShowMode() == TreeWidget.ItemsExpanded)
        else:
            strLower = string.lower()
            for idx in range(self._ui.cookieTree.topLevelItemCount()):
                item = self._ui.cookieTree.topLevelItem(idx)
                text = '.' + item.text(0)
                item.setHidden(text.lower() not in strLower)
                item.setExpanded(True)

    def _addCookie(self, cookie):
        '''
        @param: cookie QNetworkCookie
        '''
        item = None  # QTreeWidgetItem
        domain = self._cookieDomain(cookie)

        findParent = self._domainHash.get(domain)
        if findParent:
            item = HashableTreeWidgetItem(findParent)
        else:
            newParent = HashableTreeWidgetItem(self._ui.cookieTree)
            newParent.setText(0, domain)
            newParent.setIcon(0, IconProvider.standardIcon(QStyle.SP_DirIcon))
            newParent.setData(0, Qt.UserRole + 10, cookie.domain())
            self._ui.cookieTree.addTopLevelItem(newParent)
            self._domainHash[domain] = newParent

            item = HashableTreeWidgetItem(newParent)

        cookie = QNetworkCookie(cookie)
        item.setText(0, '.' + domain)
        item.setText(1, cookie.name().data().decode())
        item.setData(0, Qt.UserRole + 10, cookie)
        self._ui.cookieTree.addTopLevelItem(item)

        self._itemHash[item] = cookie

    def _removeCookie(self, cookie):
        '''
        @param: cookie QNetworkCookie
        '''
        # QTreeWidgetItem
        item = self._cookieItem(cookie)
        if not item:
            return

        self._itemHash.pop(item, None)

        itemParent = item.parent()
        if itemParent and itemParent.childCount() == 1:
            self._domainHash.pop(self._cookieDomain(cookie), None)
            self._removeTreeItem(self._ui.cookieTree, itemParent)
            item = None

        if item:
            self._removeTreeItem(self._ui.cookieTree, item)

    def _removeTreeItem(self, tree, item):
        if not item: return
        (item.parent() or tree.invisibleRootItem()).removeChild(item)

    # private:
    # override
    def closeEvent(self, event):
        '''
        @param event QCloseEvent
        '''
        whitelist = []
        blacklist = []

        for idx in range(self._ui.whiteList.count()):
            item = self._ui.whiteList.item(idx)
            whitelist.append(item.text())

        for idx in range(self._ui.blackList.count()):
            item = self._ui.blackList.item(idx)
            blacklist.append(item.text())

        settings = Settings()
        settings.beginGroup('Cookie-Settings')
        settings.setValue('allowCookies', self._ui.saveCookies.isChecked())
        settings.setValue('filterThirdPartyCookies', self._ui.filter3rdParty.isChecked())
        settings.setValue('filterTrackingCookie', self._ui.filterTracking.isChecked())
        settings.setValue('deleteCookiesOnClose', self._ui.deleteCookiesOnClose.isChecked())
        settings.setValue('whitelist', whitelist)
        settings.setValue('blacklist', blacklist)
        settings.endGroup()

        gVar.app.cookieJar().loadSettings()

        event.accept()

    # override
    def keyPressEvent(self, event):
        '''
        @param event QKeyEvent
        '''
        if event.key() == Qt.Key_Escape:
            self._close()

        super().keyPressEvent(event)

    def _addBlacklistByServer(self, server):
        '''
        @param: server QString
        '''
        if not server:
            return

        if self._ui.whiteList.findItems(server, Qt.MatchFixedString):
            QMessageBox.information(self, _('Already whitelisted!'),
                _("The server \"%s\" is already in whitelist, please remove it first.") % server)
            return

        if not self._ui.blackList.findItems(server, Qt.MatchFixedString):
            self._ui.blackList.addItem(server)

    def _cookieDomain(self, cookie):
        '''
        @param: cookie QNetworkCookie
        @return: QString
        '''
        domain = cookie.domain()
        domain = domain.lstrip('.')
        return domain

    def _cookieItem(self, cookie):
        '''
        @param: cookie QNetworkCookie
        @return: QTreeWidgetItem
        '''
        for key, val in self._itemHash.items():
            if val == cookie:
                return key
        return None


import random
from tkinter import Button, Label

FONT = ("Comfortaa")
CLR = ["#ccc1c1", "#eae5e5", "#93ce4f", "#b5dd87"]
RC = [["#FFA24C", "#ffc289"], ["#28C0CB", "#9de6eb"], ["#a05fe8", "#c298f0"]]

class MySubButton(Button):
    def __init__(self, master, text, command="", anchor="center", width=0, height=0):
        super().__init__(master=master, text=text, command=command,
                         anchor=anchor, width=width, height=height)

        self["font"] = FONT
        self["bg"] = CLR[2]
        self["activebackground"] = CLR[3]
        self["bd"] = 0
        self.bind("<Enter>", lambda e: self.configure(bg=CLR[3]))
        self.bind("<Leave>", lambda e: self.configure(bg=CLR[2]))

class ActionButton(Button):
    def __init__(self, master, text, command):
        super().__init__(master=master, text=text, command=command,
                         width=200,
                         height=2,
                         anchor="w",
                         padx= 20)
        self["bd"] = 0
        self["font"] = ("Comfortaa", 16)
        random_color = random.choice(RC)
        self.configure(bg=random_color[0], activebackground=random_color[1])

class MyLabel(Label):
    def __init__(self, master, text):
        super().__init__(master=master, text=text)
        self["font"] = FONT
        self["bg"] = CLR[2]


from django.db import models

TEST_TYPES = [
    ('RT', 'RT-PCR'),
    ('S', 'Sorologia'),
    ('AG', 'Teste Rápido - Antígenos'),
    ('AC', 'Teste Rápido - Anticorpos'),
]

TEST_RESULTS = [
    ('P', 'Positivo'),
    ('N', 'Negativo'),
]

class Pacient(models.Model):
    name            = models.TextField(max_length=100)
    birth           = models.DateField(auto_now=False, auto_now_add=False)
    test_type       = models.CharField(max_length=2, choices=TEST_TYPES)
    test_result     = models.CharField(max_length=1, choices=TEST_RESULTS)

    def __str__(self):
        return self.name


import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
import torch.utils.data as data

class PairwiseInteractionsMLP(data.Dataset):
    """
    Sample data from an interactions matrix in a pairwise fashion. The row is
    treated as the main dimension, and the columns are sampled pairwise.
    """

    def __init__(self, data, n_items, n_challenges_per_user,
                 use_item_attr, seen_items_dct_all, item_attr_dct,
                 seed=1):
        self.df = data
        self.seen_items_dct_all = seen_items_dct_all
        self.item_attr_dct = item_attr_dct
        self.seed = seed
        self.n_items = n_items
        self.n_challenges_per_user = n_challenges_per_user
        self.use_item_attr = use_item_attr
        self.user_cols = ['user_id']
        self.item_cols = ['programming_language', 'challenge_series_ID',
                          'author_ID', 'author_gender',
                          'author_org_ID', 'category_id']
        self.joint_cols = ['challenge_sequence']
        if self.use_item_attr:
            self.cat_cols = self.user_cols + ['challenge'] + self.item_cols + self.joint_cols
        else:
            self.cat_cols = self.user_cols + ['challenge']
        self.num_cols = ['total_submissions']

    def __getitem__(self, index):
        row = self.df.iloc[index]
        user_id = row['user_id']
        user_values = [user_id]

        pos_cat_values = np.array([row[col] for col in self.cat_cols])
        pos_num_values = np.array([row[col] for col in self.num_cols])

        np.random.seed(self.seed)
        neg_item = np.random.permutation(
            [x for x in range(self.n_items) if x not in
             self.seen_items_dct_all[user_id]])[0]
        neg_item_values = [self.item_attr_dct[neg_item][i] for i, col in
                           enumerate(self.item_cols)]
        neg_num_values = np.array([self.item_attr_dct[neg_item][6]])

        if self.use_item_attr:
            neg_joint_values = [self.n_challenges_per_user]
            neg_cat_values = np.array(user_values + [neg_item] + neg_item_values +
                                      neg_joint_values)
        else:
            neg_cat_values = np.array(user_values + [neg_item])

        return pos_cat_values, pos_num_values, neg_cat_values, neg_num_values

    def __len__(self):
        return self.df.shape[0]

class BPRModuleMLP(nn.Module):
    """
    Defines the neural network for challenge recommendation
    """

    def __init__(self, embedding_sizes, n_cont):
        super().__init__()
        self.embeddings = nn.ModuleList([nn.Embedding(categories, size) for
                                         categories, size in embedding_sizes])
        n_emb = sum(e.embedding_dim for e in self.embeddings)
        self.n_emb, self.n_cont = n_emb, n_cont
        self.lin1 = nn.Linear(self.n_emb + self.n_cont, 1)
        self.bn1 = nn.BatchNorm1d(self.n_cont)
        self.emb_drop = nn.Dropout(0.6)

    def calc_pred(self, x_cat, x_cont):
        x = [e(x_cat[:, i]) for i, e in enumerate(self.embeddings)]
        x = torch.cat(x, 1)
        x = self.emb_drop(x)
        x2 = self.bn1(x_cont)
        x = torch.cat([x, x2], 1)
        x = self.lin1(x)
        return x

    def forward(self, pos_cat, pos_num, neg_cat, neg_num):
        pos_preds = self.calc_pred(pos_cat, pos_num)
        neg_preds = self.calc_pred(neg_cat, neg_num)
        return pos_preds - neg_preds


# -*- coding: utf-8 -*-
import numpy as np
#from bitbots.kick.bezier import Bezier
Bezier=[]

#import Gnuplot

class BezierDurchMergeWiederHierZumVergleichenDerImplementationen(object):
    """Repräsentiert eine Bezier-Kurve."""

    def __init__(self, p0, p1, p2, p3):
        """Konstruktor"""
        # Speichere die Start-, End- und Stützpunkte
        self.p0 = np.array(p0)
        self.p1 = np.array(p1)
        self.p2 = np.array(p2)
        self.p3 = np.array(p3)

        # Speichere Dimension der Punkte
        self.dim = len(p0)

    def posAxis(self, t, axis):
        """Liefert die Position für die angegebene Achse an der Stelle t."""
        if t < 0:
            t = 0
        elif t > 1:
            t = 1
        return (1.0-t)**3 * self.p0[axis] + 3.0 * t * (1.0 - t)**2 * self.p1[axis] + 3.0 * t**2 * (1.0-t) * self.p2[axis] + t**3 * self.p3[axis]

    def pos(self, t):
        """Liefert den Positionsvektor an der Stelle t"""
        p = []
        for i in xrange(self.dim):
            p.append(self.posAxis(t, i))
        return p

    def visualize(self, res, only_data=False, phase=0):
        """Plottet die Bezierkurve."""
        file = open("plot/bezier_curve%i.gp" % phase, "w")
        for t in [x / float(res) for x in xrange(0, res)]:
            x,y,z = self.pos(t)
            file.write("%f %f %f\n" % (x,y,z))
        file.close()

        file = open("plot/bezier_points%i.gp" % phase, "w")
        for i in range(0,3):
            file.write("%f %f %f\n" % tuple(self.p0))
            file.write("%f %f %f\n" % tuple(self.p1))
            file.write("%f %f %f\n" % tuple(self.p2))
            file.write("%f %f %f\n" % tuple(self.p3))
        file.close()

        if not only_data:
            g = Gnuplot.Gnuplot(debug=1)
            g('set terminal wxt persist')
            g('set output "bezier.ps"')
            g('set xlabel "x"')
            g('set ylabel "y"')
            g('set zlabel "z"')
            g('splot "plot/bezier_curve%i.gp" with lines, "plot/bezier_points%i.gp"' % (phase,phase))

if __name__ == '__main__':

    p0 = np.array((1,1,1), dtype=np.float32)
    p1 = np.array((0.5,1.5,1), dtype=np.float32)
    p2 = np.array((-1,0.0,1), dtype=np.float32)
    p3 = np.array((-1.5,1,1), dtype=np.float32)

    bezier = Bezier(p0,p1,p2,p3)
    bezier.visualize(100)


import logging
import cv2
import numpy as np
from deepface.commons.functions import load_image
import tensorflow as tf

tf_version = tf.__version__
tf_major_version = int(tf_version.split(".")[0])
tf_minor_version = int(tf_version.split(".")[1])

if tf_major_version == 1:
    from keras.preprocessing import image
elif tf_major_version == 2:
    from tensorflow.keras.preprocessing import image

log = logging.getLogger("redis")

def preprocess_face(img, target_size=(224, 224), grayscale=False, enforce_detection=True):

    img = load_image(img)
    base_img = img.copy()

    # img, region = detect_face(img=img, detector_backend=detector_backend, grayscale=grayscale,
    #                           enforce_detection=enforce_detection, align=align)

    # --------------------------

    if img.shape[0] == 0 or img.shape[1] == 0:
        if enforce_detection == True:
            raise ValueError("Detected face shape is ", img.shape,
                             ". Consider to set enforce_detection argument to False.")
        else:  # restore base image
            img = base_img.copy()

    # --------------------------

    # post-processing
    if grayscale == True:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # ---------------------------------------------------
    # resize image to expected shape

    # img = cv2.resize(img, target_size) #resize causes transformation on base image, adding black pixels to resize will not deform the base image

    if img.shape[0] > 0 and img.shape[1] > 0:
        factor_0 = target_size[0] / img.shape[0]
        factor_1 = target_size[1] / img.shape[1]
        factor = min(factor_0, factor_1)

        dsize = (int(img.shape[1] * factor), int(img.shape[0] * factor))
        img = cv2.resize(img, dsize)

        # Then pad the other side to the target size by adding black pixels
        diff_0 = target_size[0] - img.shape[0]
        diff_1 = target_size[1] - img.shape[1]
        if grayscale == False:
            # Put the base image in the middle of the padded image
            img = np.pad(img, ((diff_0 // 2, diff_0 - diff_0 // 2), (diff_1 // 2, diff_1 - diff_1 // 2), (0, 0)),
                         'constant')
        else:
            img = np.pad(img, ((diff_0 // 2, diff_0 - diff_0 // 2), (diff_1 // 2, diff_1 - diff_1 // 2)), 'constant')

    # ------------------------------------------

    # double check: if target image is not still the same size with target.
    if img.shape[0:2] != target_size:
        img = cv2.resize(img, target_size)

    # ---------------------------------------------------

    # cv2.imwrite("C:\\Users\\Kaustav\\Downloads\\istockphoto-1364222624-170667a__preprocessed___{face_count}.jpg",img)

    # normalizing the image pixels

    img_pixels = image.img_to_array(img)  # what this line doing? must?
    img_pixels = np.expand_dims(img_pixels, axis=0)
    img_pixels /= 255  # normalize input in [0, 1]

    # ---------------------------------------------------

    return img_pixels


from tensorflow.keras import Input, Model
from tensorflow.keras.layers import (GRU, LSTM, Dense, Dropout,
                                     SimpleRNN, TimeDistributed, Layer)
from tensorflow import expand_dims, reduce_sum, multiply, concat, split, nn, reduce_mean
from tensorflow import print as pr
from numpy import zeros

class BahdanauAttention(Layer):
    def __init__(self, units):
        super(BahdanauAttention, self).__init__()
        self.W1 = Dense(units)
        self.W2 = Dense(units)
        self.V = Dense(1)

    def call(self, query, values):

        if(len(query.shape) == 2):
            # values : (batch_size, max_len, hidden_size)
            # query : (batch_size, hidden_size)
            query = expand_dims(query, 1)
            s1 = self.W1(query)
            # s1 : (batch_size, 1, units)
            # s2 : (batch_size, max_len, units)
            s2 = self.W2(values)
            # score : (batch_size, max_len, 1)
            score = self.V(nn.tanh(s1 + s2))
            # attention_weights : (batch_size, max_len, 1)
            attention_weights = nn.softmax(score, axis=1)
            # context_vector : (batch_size, max_len, hidden_size)
            context_vector = attention_weights * values
            # context_vector : (batch_size, hidden_size)
            context_vector = reduce_sum(context_vector, axis=1)
            return context_vector, attention_weights
        else:
            # query : (batch_size, max_len2, hidden_size)
            # values : (batch_size, max_len1, hidden_size)
            query = expand_dims(query, 2)
            values = expand_dims(values, 1)

            s1 = self.W1(query)
            s2 = self.W2(values)
            # s1 : (batch_size, max_len2,1, units)
            # s2 : (batch_size, 1,max_len1, units)
            tsum = s1+s2
            # tsum : (batch_size, max_len2, max_len1, units)
            score = self.V(nn.tanh(tsum))
            # score : (batch_size, max_len2, max_len1, 1)
            attention_weights = nn.softmax(score, axis=2)
           # attention_weights : (batch_size, max_len2, max_len1,1)
            # values : (batch_size, 1,max_len1, hidden_size)
            context_vector = attention_weights * values
            # context_vector : (batch_size, max_len2, max_len1, hidden_size)
            context_vector = reduce_sum(context_vector, axis=2)
            # context_vector : (batch_Size, max_len2, hidden_size)
            return context_vector, attention_weights

def make_model(config, enc_timesteps, enc_vsize, dec_timesteps, dec_vsize):
    # Take inputs for enc and decoder
    enc_inputs = Input(shape=(enc_timesteps, enc_vsize),
                       name="Train_encoder_input")
    dec_inputs = Input(shape=(dec_timesteps, dec_vsize),
                       name="Train_decoder_input")

    # Imp variables
    nlayers = len(config.layer_dimensions)
    latent_dims = config.layer_dimensions
    dispatch_dict = {"LSTM": LSTM, "GRU": GRU, "RNN": SimpleRNN}
    factor = (2**(config.cell_type == "LSTM"))

    # Create enc layers
    enc_layers, enc_states = [], []
    prev_layer_out = enc_inputs
    for i in range(nlayers):
        # We don't need all the timesteps for last layer.
        return_sequences = True
        enc_lstm = dispatch_dict[config.cell_type](
            latent_dims[i], return_state=True, recurrent_dropout=config.recurrent_dropout, dropout=config.dropout, return_sequences=return_sequences, name=f"train_encoder_{config.cell_type}_{i+1}")
        enc_lstm_out = enc_lstm(prev_layer_out)
        enc_lstm_states, enc_lstm_out = concat(
            list(enc_lstm_out[1:]), axis=-1), enc_lstm_out[0]
        prev_layer_out = enc_lstm_out
        enc_layers.append(enc_lstm)
        enc_states.append(enc_lstm_states)

    # Add attention layer
    # Attention layer will take last encoder layer output and decoder input as input
    # We will combine the attention context with decoder input
    # Create decoder layers
    dec_layers, dec_outs, dec_states = [], [], []
    prev_layer_out = dec_inputs
    for i in range(nlayers):
        # Here, we want all the layers to output sequence, as we are using it in softmax
        dec_lstm = dispatch_dict[config.cell_type](
            latent_dims[i], return_state=True, return_sequences=True, recurrent_dropout=config.recurrent_dropout, dropout=config.dropout, name=f"train_decoder_{config.cell_type}_{i+1}")
        dec_lstm_out = dec_lstm(
            prev_layer_out, initial_state=split(enc_states[i], factor, -1))
        dec_lstm_states, dec_lstm_out = list(dec_lstm_out[1:]), dec_lstm_out[0]
        prev_layer_out = dec_lstm_out
        dec_layers.append(dec_lstm)
        dec_outs.append(dec_lstm_out)
        dec_states.append(dec_lstm_states)

    attn = BahdanauAttention(config.attention_shape)
    attn_context, attn_weights = attn(
        prev_layer_out, enc_lstm_out)
    if(config.attention):
        prev_layer_out = multiply(
            attn_context, prev_layer_out)

    # Add dropout layer
    dec_dropout = Dropout(config.dropout, name="dropout")
    dec_dropout_out = dec_dropout(prev_layer_out)

    # Add softmax layer.
    # Note: the timedistributed layer, this applies softmax for each timestep
    dec_dense = Dense(dec_vsize, activation="softmax",
                      name="train_softmax_dense")
    dec_time_distri = TimeDistributed(dec_dense, name="train_timedist_softmax")
    dec_output = dec_time_distri(dec_dropout_out)

    # Final model is ready
    full_model = Model([enc_inputs, dec_inputs],
                       dec_output, name="Train_model")

    # We can use the above layers to create the model for inference as well

    # Inference encoder model

    # Take input
    inf_enc_inputs = Input(shape=(enc_timesteps, enc_vsize),
                           name="Inference_encoder_inputs")

    # Use the encoder layers and collect final states
    inf_enc_states = []
    prev_layer_out = inf_enc_inputs
    for i in range(nlayers):
        inf_enc_lstm_out = enc_layers[i](prev_layer_out)
        # Note: we are concating states for ease, as LSTM outputs 2 states at once.
        inf_enc_lstm_states, inf_enc_lstm_out = concat(
            list(inf_enc_lstm_out[1:]), axis=-1), inf_enc_lstm_out[0]
        inf_enc_states.append(inf_enc_lstm_states)
        prev_layer_out = inf_enc_lstm_out

    # Inference encoder model is ready
    inf_enc_model = Model(
        inf_enc_inputs, [inf_enc_states, prev_layer_out], name="Inference_encoder_model")

    # Inference Decoder model

    # Take inputs
    # Note the 1 here, as we will pass 1 char after another for inference
    inf_dec_inputs = Input(shape=(1, dec_vsize),
                           name="Inference_decoder_inputs")
    inf_dec_init_states = [Input(shape=(factor*latent_dims[i],), name=f"Inference_decoder_init_states_{i+1}")
                           for i in range(nlayers)]
    inf_enc_out = Input(
        shape=(enc_timesteps, latent_dims[nlayers-1]), name="Inference_encoder_output")

    # Use the decoder layers from training and collect their states
    # These states will be passed for each char separately
    prev_layer_out = inf_dec_inputs
    inf_dec_states = []
    for i in range(nlayers):
        inf_dec_lstm_out = dec_layers[i](
            prev_layer_out, initial_state=split(inf_dec_init_states[i], factor, axis=-1))
        inf_dec_lstm_states, inf_dec_lstm_out = concat(list(
            inf_dec_lstm_out[1:]), axis=-1), inf_dec_lstm_out[0]
        prev_layer_out = inf_dec_lstm_out
        inf_dec_states.append(inf_dec_lstm_states)

    # Use the attention layer to get the context
    # prev_layer_out = reduce_sum(prev_layer_out, axis=1)
    attn_context, attn_weights = attn(
        prev_layer_out, inf_enc_out)
    if(config.attention):
        prev_layer_out = multiply(
            attn_context, prev_layer_out)

    # Add dropout
    inf_dec_dropout = dec_dropout(prev_layer_out)
    inf_dec_output = TimeDistributed(dec_dense)(
        inf_dec_dropout)

    # Inference decoder model is ready
    inf_dec_model = Model([inf_dec_inputs,
                           inf_dec_init_states, inf_enc_out], [inf_dec_output, inf_dec_states, attn_weights], name="Inference_decoder")

    return full_model, inf_enc_model, inf_dec_model

if __name__ == "__main__":
    class default_config:
        cell_type = "LSTM"
        layer_dimensions = [128]
        attention = True
        attention_shape = 64
        dropout = 0.1
        recurrent_dropout = 0.1

    full_model, inf_enc_model, inf_dec_model = make_model(
        default_config(), 20, 30, 21, 31)
    # print(full_model.summary())
    # print(inf_enc_model.summary())
    # print(inf_dec_model.summary())


import pandas as pd
import time
import spacy
import pickle

nlp = spacy.load("en_core_web_md")
sub_ids = pd.read_csv("../imdb_sub_ids.tsv", sep="\t")

docs_tokens = []
docs_lemmatized = []
docs_pos_tags = []
srts_cleaned = []

srts = []

count = 0
chunk_size=100
chunk_count = 0

doc_index = 0

for index, row in sub_ids.iterrows():
    count += 1

    srt = parse_srt(gunzip(os.path.join("subtitles",str(row["sub_id"]) + ".gz")))
    srt = clean_srt(srt)

    srt = get_continous_text(srt)

    srts.append(srt)

    if count == chunk_size or index == len(sub_ids)-1:
      t0 = time.time()
      count = 0

      for doc in nlp.pipe(srts, batch_size=10000, n_threads=2):
        doc_tokens = []
        doc_lemmatized = []
        doc_pos_tags = []

        for t in doc:
          if t.is_sent_start:
            doc_pos_tags.append("#")
          if t.is_alpha and not t.is_punct and not t.is_stop:
            if t.ent_type not in [378,381,386]:
              doc_tokens.append(t.lower)
              doc_lemmatized.append(t.lemma_)
            else:
              doc_lemmatized.append("-NE-")
          doc_pos_tags.append(t.tag_)

        docs_tokens.append(doc_tokens)
        docs_lemmatized.append(doc_lemmatized)
        docs_pos_tags.append(doc_pos_tags)

        doc_index += 1
        return

      pickle.dump(docs_tokens, open("../pickles/docs_tokens/docs_tokens_{:04d}.pkl".format(chunk_count), "wb"))
      pickle.dump(docs_lemmatized, open("../pickles/docs_lemmatized/docs_lemmatized_{:04d}.pkl".format(chunk_count), "wb"))
      pickle.dump(docs_pos_tags, open("../pickles/docs_pos_tags/docs_pos_tags_{:04d}.pkl".format(chunk_count), "wb"))
      pickle.dump(srts_cleaned, open("../pickles/srts_cleaned/srts_cleaned_{:04d}.pkl".format(chunk_count), "wb"))

      print(time.time()-t0, end=",", flush=True)
      srts_cleaned = []
      docs_tokens = []
      docs_lemmatized = []
      docs_pos_tags = []
      srts = []

      chunk_count += 1


import itertools

from django import template
from django.db.models import Q
from django.utils.safestring import mark_safe

from .. import bases_mask
from ..models import (
    FlowCell,
    REFERENCE_CHOICES,
    pretty_range,
    SEQUENCING_STATUS_CHOICES,
    CONVERSION_STATUS_CHOICES,
    DELIVERY_STATUS_CHOICES,
)

register = template.Library()

FLOWCELL_STATUS = {
    "initial": {
        "class": "text-muted fc-super-muted",
        "icon": "mdi:asterisk",
        "title": "not started",
    },
    "ready": {
        "class": "text-info",
        "icon": "mdi:timer-sand",
        "title": "ready to start",
    },
    "in_progress": {
        "class": "",
        "icon": "mdi:timer-sand-paused",
        "title": "in progress",
    },
    "complete": {
        "class": "text-success",
        "icon": "mdi:timer-sand-complete",
        "title": "complete (but unconfirmed)",
    },
    "complete_warnings": {
        "class": "text-warning",
        "icon": "mdi:alert-box",
        "title": "complete with warnings",
    },
    "failed": {
        "class": "text-danger",
        "icon": "mdi:timer-sand-complete",
        "title": "failed / canceled",
    },
    "closed": {
        "class": "text-success",
        "icon": "mdi:check-bold",
        "title": "released confirmed",
    },
    "closed_warnings": {
        "class": "text-warning",
        "icon": "mdi:alert-box",
        "title": "complete with warnings",
    },
    "canceled": {
        "class": "text-danger",
        "icon": "mdi:close-thick",
        "title": "canceled confirmed",
    },
    "skipped": {
        "class": "text-muted",
        "icon": "mdi:minus-thick",
        "title": "skipped or N/A",
    },
}

@register.simple_tag
def count_barcode_reads(flow_cell):
    """Count number of barcode reads in the planned reads of ``flow_cell``."""
    try:
        return len(
            [op for op, count in bases_mask.split_bases_mask(flow_cell.planned_reads) if op == "B"]
        )
    except bases_mask.BaseMaskConfigException:
        return None

@register.simple_tag
def get_details_flowcells(project):
    """Return sequencers for the project details page"""
    return FlowCell.objects.filter(project=project)[:5]

REF_CHOICE_MAP = dict(REFERENCE_CHOICES)

@register.filter
def reference_label(reference):
    return REF_CHOICE_MAP.get(reference, "unknown")

@register.filter(name="pretty_range")
def _pretty_range(value):
    return pretty_range(value)

@register.filter
def divide(value, arg):
    """Divides the value; argument is the divisor. Returns empty string on any error."""
    try:
        value = float(value)
        arg = float(arg)
        if not arg:
            if value:
                return "x/0"
            else:
                return "0.0"
        if arg:
            return value / arg
    except ValueError:
        pass
    return ""

@register.filter
def multiply(value, arg):
    """Multiply the value"""
    try:
        return float(value) * float(arg)
    except ValueError:
        pass
    return ""

@register.simple_tag
def get_lane_index_errors(flowcell, lane):
    result = []
    for (err_lane, _, _), val in flowcell.get_index_errors().items():
        if lane == err_lane:
            result.append(val)
    return result

@register.simple_tag
def get_index_error_lanes(flowcell, ignore_suppressions=False):
    if ignore_suppressions:
        result = list(flowcell.lanes_suppress_no_sample_found_for_observed_index_warning)
    else:
        result = []
    for (lane, _, _), _ in flowcell.get_index_errors().items():
        if (
            ignore_suppressions
            or lane not in flowcell.lanes_suppress_no_sample_found_for_observed_index_warning
        ):
            result.append(lane)
    return list(sorted(set(result)))

@register.simple_tag
def get_index_errors(flowcell, lane, index_read_no, sequence):
    return flowcell.get_index_errors().get((lane, index_read_no, sequence))

@register.simple_tag
def get_reverse_index_errors(flowcell, library_uuid):
    return flowcell.get_reverse_index_errors().get(
        str(library_uuid), {"barcode": [], "barcode2": []}
    )

@register.simple_tag
def get_sheet_name_errors(flowcell, entry):
    return flowcell.get_sample_sheet_errors().get(str(entry.sodar_uuid), {}).get("name")

@register.simple_tag
def get_sheet_lane_errors(flowcell, entry):
    return flowcell.get_sample_sheet_errors().get(str(entry.sodar_uuid), {}).get("lane")

@register.simple_tag
def get_sheet_barcode_errors(flowcell, entry):
    return flowcell.get_sample_sheet_errors().get(str(entry.sodar_uuid), {}).get("barcode")

@register.simple_tag
def get_sheet_barcode2_errors(flowcell, entry):
    return flowcell.get_sample_sheet_errors().get(str(entry.sodar_uuid), {}).get("barcode2")

@register.filter
def all_n(seq):
    return all(s == "N" for s in seq)

@register.filter(name="max")
def max_value(iter):
    return max(iter)

@register.filter
def chain(lhs, rhs):
    return itertools.chain(lhs or (), rhs or ())

@register.simple_tag
def get_known_contaminations(flowcell):
    return flowcell.get_known_contaminations()

@register.simple_tag
def has_sheet_for_lane(flowcell, lane):
    return flowcell.has_sheet_for_lane(lane)

@register.simple_tag
def get_libraries_with_suppressed_reverse_index_errors(flowcell):
    """Return libraries of flowcell with suppressed reversed index errors"""
    return flowcell.libraries.filter(
        Q(suppress_barcode1_not_observed_error=True) | Q(suppress_barcode2_not_observed_error=True)
    )

@register.simple_tag
def get_lanes_with_missing_sheets(flowcell, ignore_suppressions=False):
    result = []
    for lane in range(1, flowcell.num_lanes + 1):
        if not flowcell.has_sheet_for_lane(lane) and (
            ignore_suppressions or lane not in flowcell.lanes_suppress_no_sample_sheet_warning
        ):
            result.append(lane)
    return result

@register.simple_tag
def get_contamination(contaminations, seq):
    return contaminations.get(seq)

@register.simple_tag
def get_status_class(status):
    return FLOWCELL_STATUS.get(status).get("class")

@register.simple_tag
def get_status_icon(status):
    return FLOWCELL_STATUS.get(status, {}).get("icon")

# TODO: This doesn't seem to be used anywhere?
@register.filter
def status_to_title(status):
    return FLOWCELL_STATUS.get(status, {}).get("title")

@register.simple_tag
def valid_status(attribute, value):
    if attribute == "status_sequencing":
        return value in dict(SEQUENCING_STATUS_CHOICES)
    elif attribute == "status_conversion":
        return value in dict(CONVERSION_STATUS_CHOICES)
    elif attribute == "status_delivery":
        return value in dict(DELIVERY_STATUS_CHOICES)
    else:
        return False

@register.simple_tag
def is_user_watching_flowcell(user, flowcell):
    """Wehther the ``user`` is watching ``flowcell``"""
    return flowcell.is_user_watching(user)

@register.filter(is_safe=True)
def format_basemask(x):
    if not x:
        return x
    try:
        split_result = bases_mask.split_bases_mask(x)
    except bases_mask.BaseMaskConfigException:
        return x
    safe = [(op if op.upper() in "BMTS" else "?", c) for op, c in split_result]
    return mark_safe(",".join("%d%s" % (c, op) for op, c in safe))

@register.simple_tag
def get_adapter_siblings(flowcell, seq):
    max_dist = 1
    seqs = flowcell.get_cached_adapter_seqs()
    result = []
    for s in seqs:
        if len([x for x, y in zip(seq, s) if x != y]) <= max_dist:
            result.append(s)
    return result


# https://replit.com/@andy8787main/a009#main.py
# https://zerojudge.tw/ShowProblem?problemid=a009

# 要用ASCII
s = input()

# 解
ans = ""
for i in s: # 一个一个数字慢慢带
    ans += chr(ord(i)-7) # 转换
print(ans)


import pandas as pd
import cv2
# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.config import get_cfg
from detectron2.modeling import build_model
from detectron2.checkpoint import DetectionCheckpointer
import detectron2.data.transforms as T
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

# # prepare training images
# train_df = pd.read_csv('ball_net_train_v2.csv')
# for index, row in train_df.iterrows():
#     cap = cv2.VideoCapture('../m-%03d.mp4' % row.match_id)
#     for t in [row.time_ms, row.tm1, row.tp1]:
#         cap.set(0, t)
#         ret, frame = cap.read()
#         cv2.imwrite('images4/m-%03d-%08d.jpg'%(row.match_id, t), frame)
#         print('images4/m-%03d-%08d.jpg'%(row.match_id, t))

image_folder = 'images4/'
DT_THRESHOLD = 0.98
DT_WEIGHTS = 'soccer-ball-20220607-2034.pth'

cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file('COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml'))
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ball)
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = DT_THRESHOLD # Set threshold for this model
our_predictor = build_model(cfg) # returns a torch.nn.Module
DetectionCheckpointer(our_predictor).load(DT_WEIGHTS)
our_predictor.eval()

def extract_feature(frame):
    inputs = []
    original_height, original_width = frame.shape[:2]
    img = T.ResizeShortestEdge(
        [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST
    ).get_transform(frame).apply_image(frame)
    img = torch.as_tensor(img.astype("float32").transpose(2, 0, 1))
    inputs.append({"image": img, "height": original_height, "width": original_width})
    images = our_predictor.preprocess_image(inputs)
    with torch.no_grad():
        features = our_predictor.backbone(images.tensor)  # set of cnn features
        features_ = features['p2']
    height, width = features_.shape[2:4]
    features_ = features_.reshape(256, height, width)
    features_ = features_.cpu().numpy()
    return features_

class BallDataset(object):
    def __init__(self, ball_df, img_folder='images4/'):
        self.ball_df = ball_df
        self.img_folder = img_folder

    def __getitem__(self, idx):
        row = self.ball_df.iloc[idx].to_numpy()
        match_id,time_ms,x,y,tm1,tp1 = row[0:6]
        features = []
        for t in [tm1, time_ms, tp1]:
            img = cv2.imread(self.img_folder+'m-%03d-%08d.jpg'%(match_id,t))
            original_height, original_width = img.shape[:2]
            features_ = extract_feature(img)
            height, width = features_.shape[1:3]
            features.append(features_)
        features = np.array(features).reshape(3 * 256, height, width)
        target = np.zeros((height, width), dtype=np.uint8)
        cv2.circle(target, tuple(np.round(np.array([x, y]) * height / original_height).astype(np.int)), 3, 1, -1)
        target = torch.as_tensor(target.astype(np.longlong))
        return features, target

    def __len__(self):
        return self.ball_df.shape[0]

train_df = pd.read_csv('ball_net_train_v2.csv')
train_dataset = BallDataset(train_df)

device = torch.device("cuda")
class BallNet(nn.Module):
    def __init__(self):
        super(BallNet, self).__init__()
        self.conv1 = nn.Conv2d(3 * 256, 768, 3, padding=1)
        self.conv2 = nn.Conv2d(768, 2, 1)

    def forward(self, x):
        out = self.conv2(F.relu(self.conv1(x)))
        return out

model = BallNet()
model.to(device)

def train_ballnet(model, model_saved):
    lr = 1e-4
    loss_function = nn.functional.cross_entropy
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)

    def accuracy(out, yb):
        preds = torch.argmax(out, dim=1)
        return torch.mean((preds == yb).float())

    batch_size = 4
    train_loader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
    epochs = 5
    # model = torch.load('BallNet_v7.pth')  # Transfer learning
    model.train()
    for i in range(epochs):
        j = 0
        for inputs, labels in train_loader:
            inputs, labels = inputs.to(device), labels.to(device)
            output = model(inputs)
            loss = loss_function(output, labels, weight=torch.tensor([1.0, 100.0]).to(device))
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
            j += 1
            print(i,j)

    torch.save(model, model_saved)

train_ballnet(model, 'Soccer_BallNet_v4.pth')


# -*- coding: utf-8 -*-
"""
Created on Wed Aug  7 09:39:48 2019

@author: Edward Carney
"""

import math
import matplotlib.pyplot as plt

class MaskHandler():
    """
    A class to handle creation of azimuth-elevation files for STK. Intended
    to be used for the BridgeSat program to generate azimuth-files for partial
    roof opening/closure.
    """
    def __init__(self, pivotAz, shutterEl_r, shutterEl_l):

        # define mask file fidelity (assumed 360 for now)
        self._fidelity = 360

        # define pivot azimuth in degrees CW from North (e.g. 0 => pivot aligns
        # with NS line, 90 => pivot aligns with EW line)
        self.pivotAz = pivotAz

        # define left and right shutter elevation
        self.shutterEl_r = shutterEl_r
        self.shutterEl_l = shutterEl_l

        # define azimuth and elevation lists; will use 1-deg fidelity; need to
        # have both 0 and 360 points (total of fidelity + 1 points)
        self.az = list(range(self._fidelity + 1))
        self.el = [0] * (self._fidelity + 1)

        # define azimuths for start, final, and max elevation of left and right
        # shutter
        self._shutterStartAz_r = int(math.floor(self.pivotAz))
        self._shutterMaxElAz_r = int(math.floor(self.pivotAz + (self._fidelity / 4)))
        self._shutterFinalAz_r = int(math.floor(self.pivotAz + (self._fidelity / 2)))

        self._shutterStartAz_l = int(math.floor(self.pivotAz + (self._fidelity / 2)))
        self._shutterMaxElAz_l = int(math.floor(self.pivotAz + (3 * self._fidelity / 4)))
        self._shutterFinalAz_l = int(math.floor(self.pivotAz + self._fidelity))

        # generate mask values
        self._generateMask()

    def _setElevation(self, startAz, finalAz, maxEl):
        """
        Sets elevation values for a defined range of azimuth. Will do a sinusoidal progression
        from zero to the max elevation and back to zero.
        """

        # define the elevation function based on the max elevation to be used
        if maxEl == 90.0:
            elFunc = lambda az: 90.0
        elif maxEl > 90.0:
            elFunc = lambda az: 180.0 - abs(math.sin(math.pi / 180.0 * (az - self.pivotAz)) * (180.0 - maxEl))
        else:
            elFunc = lambda az: abs(math.sin(math.pi / 180.0 * (az - self.pivotAz)) * maxEl)

        # iterate through all azimuth values and set elevation
        for az in range(startAz, finalAz):
            self.el[az % self._fidelity] = elFunc(az)

    def _generateMask(self):
        """
        Sets object mask list based on left and right shutter settings.
        """

        # set elevation for the left and right shutters
        self._setElevation(self._shutterStartAz_r, self._shutterFinalAz_r, self.shutterEl_r)
        self._setElevation(self._shutterStartAz_l, self._shutterFinalAz_l, self.shutterEl_l)

        # make sure the first and last values are the same
        self.el[-1] = self.el[0]

    def plotData(self):
        """
        Plots the elevation vs azimuth for easy visualization.
        """
        plt.plot(self.az[:-1], self.el[:-1])
        plt.xlabel("Azimuth (deg)")
        plt.ylabel("Elevation (deg)")
        plt.title("Elevation vs. Azimuth")

        print("INFO: Plotting data. Close out window to continue.")

        plt.show()

    def printToFile(self, fileName):
        """
        Prints azimuth-elevation data to a provided file in STK *.aem file format.
        """

        with open(fileName, 'w') as outFile:
            # write out default file header data
            outFile.write("stk.v.11.2.1\nBEGIN AzElMask\nNumberOfPoints {0}\nBEGIN AzElMaskData\n\n".format(int(self._fidelity + 1)))

            # write out actual data
            for i in range(self._fidelity + 1):
                outFile.write("{0} {1}\n".format(self.az[i], self.el[i]))

            # note do NOT add these lines to the end or else things break
            # outFile.write("\nEND AzElMaskData\nEND AzElMask")


import os
from collections import defaultdict

MAX_N = 3
CORPORA_PATH = './corpora/'

def make_freq():
	freqs = []
	for i in range(MAX_N):
		freqs.append(defaultdict(int))

	for file in os.listdir(CORPORA_PATH):
		if file.endswith(".txt"):
			with open(CORPORA_PATH + file, 'r') as f:
				contents = f.read()
				for i in range(len(contents)):
					for j in range(1, MAX_N+1):
						if i + j <= len(contents) and contents[i:i+j].isalpha():
							freqs[j-1][contents[i:i+j]] += 1
	return freqs


import random

from pokemon import *

lista_nomes = ['Luan', 'Jorge', 'Carlos', 'Maria', 'Julia', 'Fernanda']
lista_pokemons = [PokemonAgua('Squirtle'),
                  PokemonEletrico('Pikachu'),
                  PokemonFogo('Charmander')]

class Pessoa:

    def __init__(self, nome=None, pokemons=[]):

        if nome:
            self.nome = nome
        else:
            self.nome = random.choice(lista_nomes)

        self.pokemons = pokemons

    def __str__(self):

        return self.nome

    def mostrar_pokemons(self):

        if self.pokemons:

            print(f"Pokemons de {self}:")

            for pokemon in self.pokemons:

                print(pokemon)

        else:

            print("você não tem pokemons")

class Player(Pessoa):

    tipo = 'player'

    def capturar(self, pokemon):

        self.pokemons.append(pokemon)
        print(f"{self} capturou {pokemon}")

class Inimigo(Pessoa):

    tipo = 'inimigo'

    def __init__(self, nome=None, pokemons=[]):

        if not pokemons:

            for pokemons_sorteados in range(random.randint(1, 2)):

                pokemons.append(random.choice(lista_pokemons))

        super().__init__(nome=nome, pokemons=pokemons)

meu_inimigo = Inimigo()

print(meu_inimigo)
meu_inimigo.mostrar_pokemons()

meu_pok = PokemonFogo('charmander')


import math
from time import time

import datasets
import numpy as np
import torch

from model_code.retriever_bert_qa_train import make_qa_retriever_model

def embed_questions_for_retrieval(q_ls, qa_tokenizer, qa_embedding, max_length=128, device='cuda:0'):
    q_toks = qa_tokenizer.batch_encode_plus(q_ls, max_length=max_length, truncation=True, padding='max_length')
    q_ids, q_mask = (
        torch.LongTensor(q_toks['input_ids']).to(device),
        torch.LongTensor(q_toks['attention_mask']).to(device),
    )
    with torch.no_grad():
        q_reps = qa_embedding.embed_questions(q_ids, q_mask).cpu().type(torch.float)
    return q_reps.numpy()

def make_qa_dense_index(
    qa_embedding,
    qa_tokenizer,
    qustions_set,
    batch_size=512,
    max_length=128,
    index_name='kilt_passages_reps.dat',
    dtype='float32',
    device='cuda:0',
):
    st_time = time()
    fp = np.memmap(index_name, dtype=dtype, mode='w+', shape=(qustions_set.num_rows, 128))
    n_batches = math.ceil(qustions_set.num_rows / batch_size)
    for i in range(n_batches):
        questions = [q for q in qustions_set[i * batch_size: (i + 1) * batch_size]['title']]
        reps = embed_questions_for_retrieval(questions, qa_tokenizer, qa_embedding, max_length, device)
        fp[i * batch_size: (i + 1) * batch_size] = reps
        if i % 2 == 0:
            print(i, time() - st_time)

if __name__ == '__main__':
    tokenizer, model = make_qa_retriever_model(from_file='models/eli5c_retriever_model_9.pth')
    eli5c = datasets.load_dataset('jsgao/eli5_category')
    make_qa_dense_index(model, tokenizer, eli5c['train'], device='cuda:0', index_name='embeds/eli5c_train.dat')
    make_qa_dense_index(model, tokenizer, eli5c['validation1'], device='cuda:0', index_name='embeds/eli5c_val1.dat')
    make_qa_dense_index(model, tokenizer, eli5c['validation2'], device='cuda:0', index_name='embeds/eli5c_val2.dat')


from selenium import webdriver
import time

class driverCall():
    def __init__(self):
        options = webdriver.ChromeOptions()
        options.add_argument("--incognito")
        options.add_argument("window-size=500,500")
        driver = webdriver.Chrome(chrome_options=options)
        driver.get('https://youtu.be/8rqgGZrsB7g')
        driver.find_element_by_xpath("//button[@class='ytp-play-button ytp-button']").click()
        driver.find_element_by_xpath("//button[@class='ytp-mute-button ytp-button']").click()
        driver.find_element_by_xpath("//button[@class='ytp-button ytp-settings-button']").click()
        driver.find_element_by_xpath("//div[@class='ytp-menuitem-label']").click()
        element = driver.find_element_by_xpath("//div[@class='ytp-menuitem-content']")
        driver.minimize_window()
        time.sleep(601)
        driver.quit()
while(True):
    objs = {}
    for x in range(10) :
        objs[x]= driverCall()


import sys
sys.path.append('../../')
import argparse
import random
import numpy as np
import time
import pickle
import torch
from collections import defaultdict
from itertools import chain
from pathlib import Path
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torch.distributions import Categorical
from collections import namedtuple
from random import sample

SavedAction = namedtuple('SavedAction', ['log_prob', 'value'])
from data.path.rl_model import *

import math

class Prepared_data:
    def __init__(self, **kargs):
        self.ui_dict = dict()
        self.iu_dict = dict()
        self.ic_dict = dict()
        self.ci_dict = dict()
        self.ib_dict = dict()
        self.bi_dict = dict()
        self.usize = kargs.get('usize')
        self.isize = kargs.get('isize')
        self.csize = kargs.get('csize')
        self.bsize = kargs.get('bsize')
        self.embeddings = np.zeros((self.usize + self.isize + self.csize + self.bsize, 100))
        self.user_embedding = np.zeros((self.usize, 100))
        self.item_embedding = np.zeros((self.isize, 100))
        self.category_embedding = np.zeros((self.csize, 100))
        self.brand_embedding = np.zeros((self.bsize, 100))

        self.userid = torch.arange(0, self.usize)
        self.itemid = torch.arange(self.usize, self.usize + self.isize)
        self.categoryid = torch.arange(self.usize + self.isize, self.usize + self.isize + self.csize)
        self.brandid = torch.arange(self.usize + self.isize + self.csize,
                                    self.usize + self.isize + self.csize + self.bsize)

        print('Begin to load data')
        start = time.time()

        self.load_embedding(kargs.get('node_emb_dic'))

        self.load_ui(kargs.get('ui_relation_file'))
        self.load_ic(kargs.get('ic_relation_file'))
        self.load_ib(kargs.get('ib_relation_file'))

        end = time.time()
        print('Load data finished, used time %.2fs' % (end - start))

    def load_embedding(self, embfile):
        nodewv_dic = pickle.load(open(embfile, 'rb'))
        nodewv_tensor = []
        all_nodes = list(range(len(nodewv_dic.keys())))
        for node in all_nodes:
            nodewv_tensor.append(nodewv_dic[node].numpy())
        nodewv_tensor = torch.Tensor(nodewv_tensor)
        self.embeddings = nodewv_tensor
        # because when we save all embeddings, the order is user, item, category and brand
        self.user_embedding = nodewv_tensor[:self.usize, :]
        self.item_embedding = nodewv_tensor[self.usize : self.usize + self.isize, :]
        self.category_embedding = nodewv_tensor[self.usize + self.isize : self.usize + self.isize + self.csize, :]
        self.brand_embedding = nodewv_tensor[self.usize + self.isize + self.csize : self.usize + self.isize + self.csize + self.bsize, :]

        return self.user_embedding, self.item_embedding, self.category_embedding, self.brand_embedding

    def load_ui(self, uifile):
        user_item_data = open(uifile, 'r').readlines()
        for user_item_ele in user_item_data:
            user_item_ele_list = user_item_ele.strip().split(',')
            user = int(user_item_ele_list[0])
            item = int(user_item_ele_list[1])
            if item not in self.iu_dict.keys():
                self.iu_dict[item] = [user]
            else:
                self.iu_dict[item].append(user)

            if user not in self.ui_dict.keys():
                self.ui_dict[user] = [item]
            else:
                self.ui_dict[user].append(item)
        return self.ui_dict, self.iu_dict

    def load_ib(self, ibfile):
        item_brand_data = open(ibfile, 'r').readlines()
        for item_brand_ele in item_brand_data:
            item_brand_ele_list = item_brand_ele.strip().split(',')
            item = int(item_brand_ele_list[0])
            brand = int(item_brand_ele_list[1])
            if item not in self.ib_dict.keys():
                self.ib_dict[item] = [brand]
            else:
                self.ib_dict[item].append(brand)

            if brand not in self.bi_dict.keys():
                self.bi_dict[brand] = [item]
            else:
                self.bi_dict[brand].append(item)
        return self.ib_dict, self.bi_dict

    def load_ic(self, icfile):
        item_category_data = open(icfile, 'r').readlines()
        for item_category_ele in item_category_data:
            item_category_ele_list = item_category_ele.strip().split(',')
            item = int(item_category_ele_list[0])
            category = int(item_category_ele_list[1])
            if item not in self.ic_dict.keys():
                self.ic_dict[item] = [category]
            else:
                self.ic_dict[item].append(category)

            if category not in self.ci_dict.keys():
                self.ci_dict[category] = [item]
            else:
                self.ci_dict[category].append(item)
        return self.ic_dict, self.ci_dict

def find_candidate(start, data, no_neighobor=[]):

    # The strategy to explore user-item paths and item-item paths is the same.

    userid = data.userid
    itemid = data.itemid
    categoryid = data.categoryid
    brandid = data.brandid
    candidate_neighbors = []
    try:
        if start in userid:
            candidate_neighbors = data.ui_dict[start]

        elif start in itemid:
            candidate_neighbors = data.ic_dict[start] + data.ib_dict[start]

        elif start in categoryid:
            candidate_neighbors = data.ci_dict[start]

        elif start in brandid:
            candidate_neighbors = data.bi_dict[start]

        if len(candidate_neighbors) < 50:
            candidate_neighbors_without_noneighbor = [item for item in candidate_neighbors if item not in no_neighobor]
            return candidate_neighbors_without_noneighbor
        else:
            return candidate_neighbors
    except KeyError:
        return -1

def top_k(start, candidate_neighbors, data):
    refinefolder = args.refinefolder
    ic_relation_file = refinefolder + 'item_category.relation'
    ib_relation_file = refinefolder + 'item_brand.relation'
    ui_relation_file = refinefolder + 'user_item.relation'
    node_emb_dic = args.basedatafolder + 'nodewv.dic'
    user_history_file = args.userhistoryfile
    usize = args.usize
    isize = args.isize
    csize = args.csize
    bsize = args.bsize

    h1 = data.embeddings[start]
    h1 = torch.Tensor(h1)

    action_values = []
    candidate_id = []

    if len(candidate_neighbors) <= 50:
        # if this neighbor doesn't have a neighbor, then not visit it.
        for n in candidate_neighbors:
            if find_candidate(n, data) == -1:
                candidate_neighbors.remove(n)

    if len(candidate_neighbors) > 50:
        candidate_neighbors = sample(candidate_neighbors, 50)

    for n in candidate_neighbors: # candidate_neighbors without no neighbor nodes
        # calculate score
        h2 = data.embeddings[n]
        h2 = torch.Tensor(h2)

        cos = nn.CosineSimilarity(dim=0, eps=1e-6)
        action_value = cos(h1, h2)

        action_values.append(action_value)
        candidate_id.append(n)

    action_values = torch.Tensor(action_values)
    if len(action_values) >= args.k:
        topk = torch.topk(action_values, args.k)
        topk_value = topk[0]
        topk_index = topk[1]

        neighbour_id = []
        for n in topk_index:
            neighbour_id.append(candidate_id[n])
    else:
        neighbour_id = candidate_id
        topk_value = action_values
    return neighbour_id, topk_value

def action_to_index(action, topk_index, topk_value):
    action_1 = action.detach().numpy()
    action_2 = int(action_1)
    h = len(topk_index)
    if action_2 > h:
        action_3 = int(action_2 / ACTION_RANGE / 2 * h)
        next_state = topk_index[action_3-1]
        next_state_value = topk_value[action_3-1]
    else:
        next_state = topk_index[action_2-1]
        next_state_value = topk_value[action_2 - 1]
    return next_state, next_state_value

def train_path(args, start, end, data):
    rl_planner = SMCP()

    refinefolder = args.refinefolder
    ic_relation_file = refinefolder + 'item_category.relation'
    ib_relation_file = refinefolder + 'item_brand.relation'
    ui_relation_file = refinefolder + 'user_item.relation'
    node_emb_dic = args.basedatafolder + 'nodewv.dic'
    user_history_file = args.userhistoryfile
    usize = args.usize
    isize = args.isize
    csize = args.csize
    bsize = args.bsize

    outputinstancesfolder = args.outputinstancesfolder
    output_ui_paths_filename = outputinstancesfolder + 'ui.paths'
    output_ii_paths_filename = outputinstancesfolder + 'ii.paths'

    # The strategy to explore user-item paths and item-item paths is the same.

    userid = data.userid
    itemid = data.itemid
    categoryid = data.categoryid
    brandid = data.brandid

    candidate_neighbors = find_candidate(start, data)

    topk_index, topk_value = top_k(start, candidate_neighbors, data)

    state = start
    state_emb = data.embeddings[state]
    done = 2

    reward = 1
    paths_list = []
    path_score_list = []
    no_neighobor = [] # do not visit these nodes again
    start_time = time.time()
    for epi in range(args.max_episode):
        epi_time = time.time()
        print(f'{epi} try time: {epi_time-start_time}')
        path = []
        values = []
        path.append(start)

        for k in range(args.max_length - 1):

            # state 100,
            action, _ = rl_planner.policy.get_action(state_emb)

            next_state, next_state_value = action_to_index(action, topk_index, topk_value)

            next_state_emb = data.embeddings[next_state]

            path.append(next_state)
            values.append(next_state_value)

            if next_state == end:
                done = 1

                reward += 100

                reward = torch.from_numpy(np.array(reward))
                done = torch.from_numpy(np.array(done))
                next_state_emb = next_state_emb.squeeze()

                if type(state_emb) is np.ndarray:
                    state_emb = torch.Tensor(state_emb)
                state_emb = state_emb.detach().numpy()
                action = action.detach().numpy()
                reward = reward.detach().numpy()
                next_state_emb = next_state_emb.detach().numpy()
                done = done.detach().numpy()

                rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)
                print('end_path:', path)
                path_score = np.average(values)
                paths_list.append(path)
                path_score_list.append(path_score)
                break

            candidate_neighbors=find_candidate(next_state, data, no_neighobor)
            if k != 0 and k != args.max_length - 2:
                if candidate_neighbors != -1:
                    if end in candidate_neighbors:
                        done = 1
                        reward += 100
                        reward = torch.from_numpy(np.array(reward))
                        done = torch.from_numpy(np.array(done))
                        next_state_emb = next_state_emb.squeeze()

                        if type(state_emb) is np.ndarray:
                            state_emb = torch.Tensor(state_emb)
                        state_emb = state_emb.detach().numpy()
                        action = action.detach().numpy()
                        reward = reward.detach().numpy()
                        next_state_emb = next_state_emb.detach().numpy()
                        done = done.detach().numpy()

                        rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)
                        state = next_state
                        state_emb = data.embeddings[state]
                        end_emb = data.embeddings[end]
                        # score of state to end
                        cos = nn.CosineSimilarity(dim=0, eps=1e-6)
                        next_state_value = cos(torch.Tensor(state_emb), torch.Tensor(end_emb))

                        path.append(end)
                        values.append(next_state_value)
                        print('end_path:', path)
                        path_score = np.average(values)
                        paths_list.append(path)
                        path_score_list.append(path_score)
                        break

            reward = torch.from_numpy(np.array(reward))
            done = torch.from_numpy(np.array(done))
            next_state_emb = next_state_emb.squeeze()

            if type(state_emb) is np.ndarray:
                state_emb = torch.Tensor(state_emb)
            state_emb = state_emb.detach().numpy()
            action = action.detach().numpy()
            reward = reward.detach().numpy()
            next_state_emb = next_state_emb.detach().numpy()
            done = done.detach().numpy()

            state_1 = state_emb

            rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)

            state = next_state

            state_emb = data.embeddings[state]

            if candidate_neighbors == -1 or len(candidate_neighbors) == 0:
                no_neighobor.append(state)
                reward -= 10
                done = 1

                rl_planner.buffer.push(state_1, action, reward, state_emb, done)
                break

            topk_index, topk_value = top_k(state, candidate_neighbors, data)

            if k == args.max_length - 1:
                if state != end:
                    reward -= 100

            if len(rl_planner.buffer) > args.batch_size:
                rl_planner.soft_q_update()

        print('path:', path)

    return paths_list, path_score_list

def get_path(args, start, end, data):
    paths_list, path_score_list = train_path(args, start, end, data)
    rl_planner = SMCP()

    state = start
    state_emb = data.embeddings[state]
    done = 2

    reward = 1

    candidate_neighbors = find_candidate(start, data)

    topk_index, topk_value = top_k(start, candidate_neighbors, data)

    good_path = []
    values = []

    for k in range(args.max_length - 1):
        # state 100,

        path = []
        scores = []
        path.append(start)

        # state 100,
        action, _ = rl_planner.policy.get_action(state_emb)

        next_state, next_state_value = action_to_index(action, topk_index, topk_value)
        next_state_emb = data.embeddings[next_state]
        path.append(next_state)
        values.append(next_state_value)

        if next_state == end:
            done = 1

            reward += 10

            reward = torch.from_numpy(np.array(reward))
            done = torch.from_numpy(np.array(done))
            next_state_emb = next_state_emb.squeeze()

            state_emb = state_emb.detach().numpy()
            action = action.detach().numpy()
            reward = reward.detach().numpy()
            next_state_emb = next_state_emb.detach().numpy()
            done = done.detach().numpy()

            rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)
            print('end_path:', path)
            good_path.append(path)
            path_score = np.average(values)
            paths_list.append(path)
            path_score_list.append(path_score)
            break

        candidate_neighbors = find_candidate(next_state, data)
        if k != 0 and k != args.max_length - 2:
            if candidate_neighbors != -1:
                if end in candidate_neighbors:
                    done = 1
                    reward += 100
                    reward = torch.from_numpy(np.array(reward))
                    done = torch.from_numpy(np.array(done))
                    next_state_emb = next_state_emb.squeeze()

                    if type(state_emb) is np.ndarray:
                        state_emb = torch.Tensor(state_emb)
                    state_emb = state_emb.detach().numpy()
                    action = action.detach().numpy()
                    reward = reward.detach().numpy()
                    next_state_emb = next_state_emb.detach().numpy()
                    done = done.detach().numpy()
                    rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)
                    state = next_state
                    state_emb = data.embeddings[state]
                    end_emb = data.embeddings[end]
                    # score of state to end
                    cos = nn.CosineSimilarity(dim=0, eps=1e-6)
                    next_state_value = cos(torch.Tensor(state_emb), torch.Tensor(end_emb))

                    path.append(end)
                    values.append(next_state_value)
                    print('end_path:', path)
                    path_score = np.average(values)
                    paths_list.append(path)
                    path_score_list.append(path_score)
                    break

        reward = torch.from_numpy(np.array(reward))
        done = torch.from_numpy(np.array(done))
        next_state_emb = next_state_emb.squeeze()

        state_emb = state_emb.detach().numpy()
        action = action.detach().numpy()
        reward = reward.detach().numpy()
        next_state_emb = next_state_emb.detach().numpy()
        done = done.detach().numpy()

        rl_planner.buffer.push(state_emb, action, reward, next_state_emb, done)

        state = next_state

        state_emb = data.embeddings[state]

        if candidate_neighbors == -1 or len(candidate_neighbors) == 0:
            break

        topk_index, topk_value = top_k(state, candidate_neighbors, data)

        if k == args.max_length - 1:
            if state != end:
                reward -= 100

        if len(rl_planner.buffer) > args.batch_size:
            rl_planner.soft_q_update()

    print('get path:', path)

    # return good_path
    return paths_list, path_score_list

def ui_path(args):
    refinefolder = args.refinefolder
    ic_relation_file = refinefolder + 'item_category.relation'
    ib_relation_file = refinefolder + 'item_brand.relation'
    ui_relation_file = refinefolder + 'user_item.relation'
    node_emb_dic = args.basedatafolder + 'nodewv.dic'
    usize = args.usize
    isize = args.isize
    csize = args.csize
    bsize = args.bsize

    outputinstancesfolder = args.outputinstancesfolder
    output_ui_paths_filename = outputinstancesfolder + 'ui.paths'+'.'+str(args.start_user)+'-'+str(args.end_user)

    # load data
    data = Prepared_data(ib_relation_file=ib_relation_file, ic_relation_file=ic_relation_file,
                         ui_relation_file=ui_relation_file, node_emb_dic=node_emb_dic,
                         usize=usize, isize=isize, csize=csize, bsize=bsize)

    userid = data.userid
    itemid = data.itemid
    categoryid = data.categoryid
    brandid = data.brandid
    ui_file = open(output_ui_paths_filename, 'w+')

    part_user_id = torch.arange(args.start_user, args.end_user)
    for m in part_user_id:
        start = m.item()

        if find_candidate(start, data) == -1:
            break

        for n in data.ui_dict[start]:
            end = n
            print(f'\n\nstart:{start}\t end:{end}')

            paths_list, path_score_list = get_path(args, start, end, data)
            paths_list_unique = []
            path_score_list_unique = []
            #delete same paths
            for index,i in enumerate(paths_list):
                if i not in paths_list_unique:
                    paths_list_unique.append(i)
                    path_score_list_unique.append(path_score_list[index])
            zipped_paths_scores = list(zip(paths_list_unique, path_score_list_unique))
            res = sorted(zipped_paths_scores, key=lambda x: x[1],reverse=True)

            if len(paths_list_unique) >= args.top_n_paths:
                res = res[:args.top_n_paths]
            print(res)

            res_len = len(res)
            if res_len > 0:
                ui_file.write(str(start) + ',' + str(end) + '\t')
                ui_file.write(str(res_len)+'\t')
                for path,score in res:
                    path_str = [str(i) for i in path]
                    ui_file.write(' '.join(path_str))
                    ui_file.write('\t')
                ui_file.write('\n')
    ui_file.close()

def ii_path(args):
    refinefolder = args.refinefolder
    ic_relation_file = refinefolder + 'item_category.relation'
    ib_relation_file = refinefolder + 'item_brand.relation'
    ui_relation_file = refinefolder + 'user_item.relation'
    node_emb_dic = args.basedatafolder + 'nodewv.dic'
    usize = args.usize
    isize = args.isize
    csize = args.csize
    bsize = args.bsize

    outputinstancesfolder = args.outputinstancesfolder
    output_ii_paths_filename = outputinstancesfolder + 'ii.paths' + '.' + str(args.start_user) + '-' + str(
        args.end_user)

    # load data
    data = Prepared_data(ib_relation_file=ib_relation_file, ic_relation_file=ic_relation_file,
                         ui_relation_file=ui_relation_file, node_emb_dic=node_emb_dic,
                         usize=usize, isize=isize, csize=csize, bsize=bsize)

    userid = data.userid
    itemid = data.itemid
    categoryid = data.categoryid
    brandid = data.brandid
    ii_file = open(output_ii_paths_filename, 'w+')

    part_user_id = torch.arange(args.start_user, args.end_user)
    for user in part_user_id:
        user = user.item()

        num_item = len(data.ui_dict[user])
        item = data.ui_dict[user]
        for m in range(num_item - 1):
            start = item[m]
            end = item[m + 1]
            print(f'\n\nuser: {user}\t start item: {start}\t end item: {end}')

            if find_candidate(start, data) == -1:
                break

            paths_list, path_score_list = get_path(args, start, end, data)
            paths_list_unique = []
            path_score_list_unique = []
            for index, i in enumerate(paths_list):
                if i not in paths_list_unique:
                    paths_list_unique.append(i)
                    path_score_list_unique.append(path_score_list[index])
            zipped_paths_scores = list(zip(paths_list_unique, path_score_list_unique))
            res = sorted(zipped_paths_scores, key=lambda x: x[1], reverse=True)

            if len(paths_list_unique) >= args.top_n_paths:
                res = res[:args.top_n_paths]
            print(res)

            res_len = len(res)
            if res_len > 0:
                ii_file.write(str(start) + ',' + str(end) + '\t')
                ii_file.write(str(res_len) + '\t')
                for path, score in res:
                    path_str = [str(i) for i in path]
                    ii_file.write(' '.join(path_str))
                    ii_file.write('\t')
                ii_file.write('\n')
    ii_file.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='generate user item and item item instances')
    parser.add_argument('start_user', type=int, default=0, nargs='?')
    parser.add_argument('end_user', type=int, default=11800, nargs='?')
    parser.add_argument('path_type', type=str, default='user',
                        nargs='?', help='user for user-item paths; item for item-item paths')

    parser.add_argument('basedatafolder', type=str, default='../Amazon_Music/',
                        nargs='?', help='this data base folder')
    parser.add_argument('outputinstancesfolder', type=str,
                        default='../Amazon_Music/path/all_ui_ii_instance_paths/',
                        nargs='?', help='this instances folder')
    parser.add_argument('refinefolder', type=str, default='../Amazon_Music/refine/',
                        nargs='?',
                        help='output to refine folder')
    parser.add_argument('userhistoryfile', type=str,
                        default='../Amazon_Music/path/user_history/user_history.txt',
                        nargs='?',
                        help='user history file')

    parser.add_argument('usize', type=int, default=1450, nargs='?')
    parser.add_argument('isize', type=int, default=11457, nargs='?')
    parser.add_argument('csize', type=int, default=429, nargs='?')
    parser.add_argument('bsize', type=int, default=1185, nargs='?')

    parser.add_argument('k', type=int, default=10, nargs='?')
    parser.add_argument('top_n_paths', type=int, default=10, nargs='?')
    parser.add_argument('max_length', type=int, default=6, nargs='?')
    parser.add_argument('replay_buffer_size', type=int, default=100000, nargs='?')
    parser.add_argument('max_episode', type=int, default=50, nargs='?')

    parser.add_argument('batch_size', type=int, default=1024, nargs='?')
    args = parser.parse_args()

    Path(args.outputinstancesfolder).mkdir(parents=True, exist_ok=True)

    if args.path_type == 'user':
        ui_path(args)
    elif args.path_type == 'item':
        ii_path(args)
    else:
        print('Please enter correct path type: user or item')


import sys
from random import randint

from mpi4py import MPI

from mpips import load

class MpiPs(object):
    """
    A parameter server implementation based on PY4MPI.
    """

    def __init__(self, ps_num):
        self.comm = MPI.COMM_WORLD
        self.size = self.comm.Get_size()
        self.rank = self.comm.Get_rank()
        self.ps_num = ps_num
        self.group = self.comm.group
        # make a group for ps
        self.ps_group = self.group.Range_incl([(0, ps_num-1, 1), ])
        self.ps_comm = self.comm.Create(self.ps_group)
        self.ps_group.Free()
        # make a group for worker
        self.wk_group = self.group.Range_excl([(0, ps_num-1, 1), ])
        self.wk_comm = self.comm.Create(self.wk_group)
        self.wk_group.Free()

        self.is_ps = (self.rank < ps_num)
        if self.is_ps:
            self._kv_store = {}
            self._run_ps()
        else:
            self.wk_num = self.wk_comm.Get_size()
            self.wk_rank = self.wk_comm.Get_rank()

    def sync(self):
        self.wk_comm.barrier()

    def end(self):
        self.sync()
        if self.wk_rank == 0:
            for i in range(self.ps_num):
                self.comm.send("", dest=i, tag=1)
        self.sync()
        sys.exit(0)

    def _get_dest_from_key(self, key):
        return hash(key) % self.ps_num

    def _gen_tag(self):
        return randint(1, 102400)

    def push_vector(self, key, vec):
        data = {
            "key": key,
            "vec": vec,
        }
        dest = self._get_dest_from_key(key)
        self.comm.send(data, dest=dest, tag=1)
        return True

    def pull_vector(self, key):
        dest = self._get_dest_from_key(key)
        tag = self._gen_tag()
        self.comm.send(key, dest=dest, tag=tag)
        return self.comm.recv(source=dest, tag=tag)

    def _run_ps(self):
        while True:
            st = MPI.Status()
            data = self.comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=st)
            if isinstance(data, dict):
                key = data.get("key")
                vec = data.get("vec")
                if key not in self._kv_store:
                    self._kv_store[key] = vec
                else:
                    self._kv_store[key] += vec
            elif data:
                vec = self._kv_store.get(data, None)
                self.comm.send(vec, dest=st.source, tag=st.tag)
            else:
                sys.exit(0)

    def load_input(self, input_file):
        return load.load(input_file, self.wk_rank, self.wk_num)


#!/usr/bin/env python
# coding: utf-8

# In[ ]:

import xml.etree.cElementTree as ET
import pprint
from pprint import pprint
from collections import defaultdict
import re
import os
import csv
import codecs
import cerberus
import sqlite3
import pandas as pd

#Taking sample from OSM file:
#creating systematic sample elements from original OSM-file (!Ref. "Project Details"!)

#import xml.etree.ElementTree as ET  # Use cElementTree or lxml if too slow

OSM_FILE = "OSM_munich.osm"  # Replace this with your osm file
SAMPLE_FILE = "sample_file_munich.osm"

k = 6 # Parameter: take every k-th top level element

def get_element(osm_file, tags=('node', 'way', 'relation')):
    """Yield element if it is the right type of tag
    Reference:
    http://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python
    """
    context = iter(ET.iterparse(osm_file, events=('start', 'end')))
    _, root = next(context)
    for event, elem in context:
        if event == 'end' and elem.tag in tags:
            yield elem
            root.clear()

with open(SAMPLE_FILE, 'wb') as output:
    output.write(b'<?xml version="1.0" encoding="UTF-8"?>\n')
    output.write(b'<osm>\n  ')
    # Write every kth top level element
    for i, element in enumerate(get_element(OSM_FILE)):
        if i % k == 0:
            output.write(ET.tostring(element, encoding='utf-8'))

    output.write(b'</osm>')

#Check sizes of OSM- and Sample-Files:
#resource: http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python

def convert_bytes(num):
    """
    this function will convert bytes to MB.... GB... etc
    """
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)
        num /= 1024.0

def file_size(file_path):
    """
    this function will return the file size
    """
    if os.path.isfile(file_path):
        file_info = os.stat(file_path)
        return convert_bytes(file_info.st_size)

#check size of sample file

OSM_size = file_size(OSM_FILE)
sample_size = file_size(SAMPLE_FILE)

print ('OSM File Size', OSM_size)
print ('Sample File Size', sample_size)


class HNode:
    def __init__(self, key, value):
        self.key = key
        self.data = value
        self.pointer = None
        self.bpointer = None

    def __gt__(self, other):
        """Greater than"""
        if not isinstance(other, HNode):
            return -1
        return self.data > other.data

    def __ge__(self, other):
        """Greater than or equal to"""
        if not isinstance(other, HNode):
            return -1
        return self.data >= other.data

    def __lt__(self, other):
        """Less than"""
        if not isinstance(other, HNode):
            return -1
        return self.data < other.data

    def __le__(self, other):
        """Less than or equal to"""
        if not isinstance(other, HNode):
            return -1
        return self.data <= other.data

from heap_link_little import HeapLinkLittle
class Huffman_Tree:
    def __init__(self, text=None):
        self.h = HeapLinkLittle()
        if text:
            self.counter = self.__build_counter(text)
        else:
            self.counter = {}
        self.root = None
        self.__build_huffman_tree()

    def __build_counter(self, text):
        '''text: a list of words.'''
        counter = {}
        for word in text:
            counter[word] = counter.get(word, 0) + 1
        return counter

    def __build_huffman_tree(self):
        for k, v in self.counter.items():
            self.h.push(HNode(k, v))

        while True:
            a, b = self.h.pop(), self.h.pop()
            if a is None or b is None:
                break
            c = HNode(None, a.data + b.data)
            c.pointer = a
            c.bpointer = b
            self.h.push(c)
            self.root = c

        return self.root

if __name__ == "__main__":
    h = Huffman_Tree("abcdasdfasdfcasdfasdf")
    print(h)


import pywikibot, csv
import stanza

site = pywikibot.Site(code='en', fam='wiktionary')
site.login()

nlp = stanza.Pipeline(lang='en', processors='tokenize,mwt,pos,lemma')

tones_to_letters = {
    'MS': 'H',
    'MH': 'I',
    'ML': 'J',
    'HL': 'B',
    'HS': 'D',
    'LS': 'M',
    'LH': 'G',
    'LM': 'F',
    'H': 'E',
    'M': 'C',
    'L': 'A'
}

tones_to_numbers = {
    'LS': '40',
    'LH': '42',
    'LM': '3',
    'MS': '20',
    'MH': '32',
    'ML': '24',
    'HS': '10',
    'HL': '14',
    'H': '1',
    'M': '2',
    'L': '4'
}

edits = 0
with open('new corpus.csv', 'r') as fin:
    reader = csv.reader(fin)
    header = ['CPL', 'PROG', 'HAB', 'POT']
    cur = {}
    for i, data in enumerate(reader):
        if i < 3: continue
        print(data)
        if data[0]:
            if data[0][0] == '[':
                # class and headword
                cur['class'] = data[0][1:-1]
                cur['word'] = data[1]

                # altforms (using stupid find-replace)
                cur['altnums'] = cur['word']
                for key, val in tones_to_numbers.items():
                    cur['altnums'] = cur['altnums'].replace(key, val)
                cur['altletters'] = cur['word']
                for key, val in tones_to_letters.items():
                    cur['altletters'] = cur['altletters'].replace(key, val)

                # other pertinent information
                cur['gloss'] = data[2]
                words = nlp(cur['gloss']).sentences[0].words
                cur['gloss'] = ' '.join([f'[[{word.lemma}|{word.text}]]' for word in words])
                print(cur['gloss'])
                # cur['transitivity'] = ''
                # if 'iv' in data[3]: cur['transitivity'] += 'i'
                # if 'tv' in data[3]: cur['transitivity'] += 't'
                cur['paradigm'] = []
                for cell in data[4:8]:
                    cur['paradigm'].extend([word.strip() for word in cell.split('\n')])

                page = pywikibot.Page(site, cur['word'])
                if '==San Juan Quiahije Chatino==' in page.text:

                    match_text = f"""===References===
* {{{{R:ctp-san:Cruz, et al. 2020}}}}"""
                    pos = page.text.find(match_text)
                    entry = f"""===Verb===
{{{{ctp-san-verb|{cur['word']}|class={cur['class']}}}}}

# {cur['gloss']}

====Conjugation====
{{{{ctp-san-conj|class={cur['class']}|{'|'.join(cur['paradigm'])}}}}}

"""
                    if entry not in page.text:
                        page.text = page.text[:pos] + entry + page.text[pos:]
                        page.save('San Juan Quiahije Chatino verb with inflection from file--existing lang section')
                    edits += 1
                else:
                    entry = f"""
==San Juan Quiahije Chatino==

===Alternative forms===
* {{{{alter|ctp-san|{cur['altnums']}||numeral tones}}}}
* {{{{alter|ctp-san|{cur['altletters']}||alphabetic tones}}}}

===Verb===
{{{{ctp-san-verb|{cur['word']}|class={cur['class']}}}}}

# {cur['gloss']}

====Conjugation====
{{{{ctp-san-conj|class={cur['class']}|{'|'.join(cur['paradigm'])}}}}}

===References===
* {{{{R:ctp-san:Cruz, et al. 2020}}}}
                """
                    if entry not in page.text:
                        page.text = page.text[:pos] + entry + page.text[pos:]
                        page.save('San Juan Quije Chatino verb with inflection from file')
                    edits += 1
                print(edits)


import csv
from django.http import HttpResponse

def export_selected_as_csv(modeladmin, request, queryset):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="selected_records.csv"'
    writer = csv.writer(response)
    writer.writerow([field.name for field in queryset.model._meta.fields])
    for obj in queryset:
        writer.writerow([getattr(obj, field.name) for field in queryset.model._meta.fields])
    return response
export_selected_as_csv.short_description = "Export selected as CSV"