def func(l1, s, l, r1, l2, r2):
    if l + l2 == s:
        print(l2, l)
        exit(0)
    elif l2 > r2:
        print("-1")
        exit(0)
    elif l + l2 < s:
        if l == r1:
            func(l1, s, l1, r1, l2 + 1, r2)
        func(l1, s, l + 1, r1, l2, r2)
    elif l + l2 > s:
        print("-1")
        exit(0)

s, l1, r1, l2, r2 = map(int, input("s, l1, r1, l2, r2 ").split())
func(l2, s, l2, r2, l1, r1)


#https://leetcode.com/problems/reverse-linked-list/
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        listNode = []
        pointer = head

        if head == None: return head
        while(pointer):
            listNode.append(pointer)
            pointer = pointer.next
        newList = ListNode()
        pointer = newList
        for i in listNode[::-1]:
            pointer.next = ListNode(i.val)
            pointer = pointer.next
        return newList.next


import random
import typing
from collections import Counter
from pathlib import Path
import numpy as np
import pandas as pd
import torch
import pickle
from tqdm import tqdm
from torch.utils.data import Dataset
from torchtext.vocab import vocab
from torchtext.data.utils import get_tokenizer
from gensim.models import Word2Vec
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class TULDataset(Dataset):
    TOKENIZED_SEQ = 'Tokenized_seq'
    ORIGINAL_SEQ = 'Original_seq'
    USER_LABEL = 'User'
    ORIGINAL_LENGTH = 'Original_seq_length'

    def __init__(self, path, word_dim=256):

        self.ds: pd.Series = pd.read_csv(path)
        self.tokenizer = get_tokenizer(None)
        self.counter = Counter()
        self.vocab = None
        self.max_length = 0
        self.num_users = 0
        self.columns = [self.ORIGINAL_SEQ, self.TOKENIZED_SEQ ,self.USER_LABEL , self.ORIGINAL_LENGTH]
        self.pad_value = 0
        self.word_dim = word_dim
        self.df = self.prepare_dataset()

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        item = self.df.iloc[idx]
        inp = torch.Tensor(item[self.TOKENIZED_SEQ]).long()
        user_label = torch.Tensor([item[self.USER_LABEL]]).long()
        un_padded_length = torch.Tensor([item[self.ORIGINAL_LENGTH]]).long()
        return (
            inp.to(device),
            user_label.to(device),
            un_padded_length.to(device)
            )

    def prepare_dataset(self) -> pd.DataFrame:
        output=[]
        sentences=[]
        length=[]
        users=set()
        for i in self.ds['poi']:
          sentences.append(i.split())
          length.append(len(i.split()))
        for i in self.ds['user']:
          users.add(i)
        self.num_users = len(users)
        self.max_length = max(length)
        print("Create vocabulary From word2vec model")
        model = Word2Vec(sentences=sentences, window=10, min_count=1, workers=4, size=self.word_dim)
        self._fill_vocab_pre_trained(model)
        print("Done !!")
        print("Preprocessing dataset")
        for index, row in tqdm(self.ds.iterrows(), total=self.ds.shape[0]):
            seq = self.tokenizer(row['poi'])
            output.append(self._create_item(seq,row["user"]))
        #print(output)
        df = pd.DataFrame(output, columns=self.columns)
        return df

    def _fill_vocab_pre_trained(self , model):
        self.vocab = vocab({}, min_freq=1)
        for i in model.wv.index2entity:
          self.vocab.insert_token(i, len(self.vocab))
        self.pad_value = len(self.vocab)

    def _create_item(self, seq: typing.List[str], user_label:int):
        # We tokenize the input, converting seq into an array of Integers
        original_seq = seq
        original_len = len(seq)
        tokenized_seq = self.vocab.lookup_indices(seq.copy())
        s = tokenized_seq + [self.pad_value] * (self.max_length - original_len)
        return (original_seq,s,user_label,original_len)


import logging

def init_stream_logger(name, level=logging.INFO):
    log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logger = logging.getLogger(name)
    logger.setLevel(level)

    # Use StreamHandler() to log to the console
    handler = logging.StreamHandler()
    formatter = logging.Formatter(log_format)
    handler.setFormatter(formatter)

    # Don't forget to add the stream handler
    logger.addHandler(handler)
    return logger


import csv
import argparse
import re

from hdt import HDTDocument

OWL_CLASS = "http://www.w3.org/2002/07/owl#Class"
RDF_CLASS = "http://www.w3.org/2000/01/rdf-schema#Class"
RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
LABEL = "http://www.w3.org/2000/01/rdf-schema#label"
COMMENT = "http://www.w3.org/2000/01/rdf-schema#comment"
OCCURENCIES = "http://purl.org/vocommons/voaf#occurrencesInDatasets"
REUSED = "http://purl.org/vocommons/voaf#reusedByDatasets"
SUB_CLASSES = "http://www.w3.org/2000/01/rdf-schema#subClassOf"
EQUIVALENT_CLASSES = "http://www.w3.org/2002/07/owl#equivalentClass"
DEFINED_BY = "http://www.w3.org/2000/01/rdf-schema#isDefinedBy"

OWL_PROPERTY = "http://www.w3.org/2002/07/owl#ObjectProperty"
RDF_PROPERTY = "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"
DOMAIN = "http://www.w3.org/2000/01/rdf-schema#domain"
RANGE = "http://www.w3.org/2000/01/rdf-schema#range"
SUB_PROPERTY = "http://www.w3.org/2000/01/rdf-schema#subPropertyOf"
EQUIVALENT_PROPERTY = "http://www.w3.org/2000/01/rdf-schema#subPropertyOf"

try:
    LOV = HDTDocument("lov_hdt/lov.hdt")
except RuntimeError:
    raise RuntimeError("LOV dump not found: Put lov dump into lov_hdt/ folder")

def generate_classes_dataset():
    with open('lov_csv/lov_classes.csv', 'w') as f:
        writer = csv.writer(f, delimiter=";")
        writer.writerow(['uri', 'uri_prefix', 'uri_suffix', 'label', 'description', 'language', 'occurencies_in_datasets', 'reused_by_datasets', 'sub_classes', 'equivalent_classes', 'equivalent_classes_suffix', 'defined_by'])
        (classe_triples, cardinality) = LOV.search_triples("", RDF_TYPE, OWL_CLASS)
        (classe_triples2, cardinality2) = LOV.search_triples("", RDF_TYPE, RDF_CLASS)
        hdt_iterators = [classe_triples, classe_triples2]
        already_added_uri = []
        for hdt_iterator in hdt_iterators:
            for classe_triple in hdt_iterator:
                uri_classe = classe_triple[0].encode('utf-8')
                if 'http' in uri_classe and uri_classe not in already_added_uri:
                    uri_prefix = get_uri_prefix(uri_classe)
                    uri_suffix = clean(get_uri_suffix(uri_classe))
                    already_added_uri.append(uri_classe)
                    sub_classes = []
                    equivalent_classes = []
                    equivalent_classes_suffix = [uri_suffix]
                    (sub_triples, cardinality) = LOV.search_triples(uri_classe, SUB_CLASSES, "")
                    for sub_triple in sub_triples:
                        if 'http' in sub_triple[2]:
                            sub_classes.append(sub_triple[2].encode('utf-8'))
                    if sub_classes:
                        sub_classes = ", ".join(sub_classes)
                    else:
                        sub_classes = ''
                    (equi_triples, cardinality) = LOV.search_triples(uri_classe, EQUIVALENT_CLASSES, "")
                    for equi_triple in equi_triples:
                        if 'http' in equi_triple[2]:
                            equivalent_classes.append(equi_triple[2].encode('utf-8'))
                            equivalent_classes_suffix.append(get_uri_suffix(equi_triple[2].encode('utf-8')))
                    if equivalent_classes:
                        equivalent_classes = ", ".join(equivalent_classes)
                    else:
                        equivalent_classes = ''
                    if equivalent_classes_suffix:
                        equivalent_classes_suffix = ", ".join(equivalent_classes_suffix)
                    else:
                        equivalent_classes_suffix = ''
                    (defined_triples, cardinality) = LOV.search_triples(uri_classe, DEFINED_BY, "")
                    defined_by = ''
                    for defined_triple in defined_triples:
                        defined_by = defined_triple[2].replace('"', '').encode('utf-8')
                    (occurences_triples, cardinality) = LOV.search_triples(uri_classe, OCCURENCIES, "")
                    if cardinality > 0:
                        occurencies = int(occurences_triples.next()[2].split('^^')[0].replace('"', ''))
                    else:
                        occurencies = 0
                    (reused_triples, cardinality) = LOV.search_triples(uri_classe, REUSED, "")
                    if cardinality > 0:
                        reused = int(reused_triples.next()[2].split('^^')[0].replace('"', ''))
                    else:
                        reused = 0
                    labels_and_descriptions = {}
                    (label_triples, cardinality) = LOV.search_triples(uri_classe, LABEL, "")
                    for label_triple in label_triples:
                        label, language = split_string_lang(label_triple[2])
                        if language not in labels_and_descriptions:
                            labels_and_descriptions[language] = {}
                        labels_and_descriptions[language]['label'] = label
                    (description_triples, cardinality) = LOV.search_triples(uri_classe, COMMENT, "")
                    for description_triple in description_triples:
                        description, language = split_string_lang(description_triple[2])
                        if language not in labels_and_descriptions:
                            labels_and_descriptions[language] = {}
                        labels_and_descriptions[language]['description'] = description
                    if labels_and_descriptions:
                        for language, value in labels_and_descriptions.iteritems():
                            if 'label' not in value:
                                label = uri_suffix
                            else:
                                label = value['label']
                            if 'description' not in value:
                                description = label
                            else:
                                description = value['description']
                            writer.writerow([uri_classe, uri_prefix, uri_suffix, label, description, language, occurencies, reused, sub_classes, equivalent_classes, equivalent_classes_suffix, defined_by])
                    else:
                        writer.writerow([uri_classe, uri_prefix, uri_suffix, uri_suffix, uri_suffix, 'undefined', occurencies, reused, sub_classes, equivalent_classes, equivalent_classes_suffix, defined_by])

def generate_properties_dataset():
    with open('lov_csv/lov_properties.csv', 'w') as f:
        writer = csv.writer(f, delimiter=";")
        writer.writerow(['uri', 'uri_prefix', 'uri_suffix', 'label', 'description', 'language', 'occurencies_in_datasets', 'reused_by_datasets', 'domain', 'range', 'sub_properties', 'equivalent_properties', 'equivalent_properties_suffix', 'defined_by'])
        (property_triples, cardinality) = LOV.search_triples("", RDF_TYPE, OWL_PROPERTY)
        (property_triples2, cardinality2) = LOV.search_triples("", RDF_TYPE, RDF_PROPERTY)
        hdt_iterators = [property_triples, property_triples2]
        already_added_uri = []
        for hdt_iterator in hdt_iterators:
            for property_triple in hdt_iterator:
                uri_property = property_triple[0].encode('utf-8')
                if 'http' in uri_property and uri_property not in already_added_uri:
                    uri_prefix = get_uri_prefix(uri_property)
                    uri_suffix = clean(get_uri_suffix(uri_property))
                    already_added_uri.append(uri_property)
                    sub_properties = []
                    equivalent_properties = []
                    equivalent_properties_suffix = [uri_suffix]
                    (sub_triples, cardinality) = LOV.search_triples(uri_property, SUB_PROPERTY, "")
                    for sub_triple in sub_triples:
                        if 'http' in sub_triple[2]:
                            sub_properties.append(sub_triple[2].encode('utf-8'))
                    if sub_properties:
                        sub_properties = ", ".join(sub_properties)
                    else:
                        sub_properties = ''
                    (equi_triples, cardinality) = LOV.search_triples(uri_property, EQUIVALENT_PROPERTY, "")
                    for equi_triple in equi_triples:
                        if 'http' in equi_triple[2]:
                            equivalent_properties.append(equi_triple[2].encode('utf-8'))
                            equivalent_properties_suffix.append(get_uri_suffix(equi_triple[2].encode('utf-8')))
                    if equivalent_properties:
                        equivalent_properties = ", ".join(equivalent_properties)
                    else:
                        equivalent_properties = ''
                    if equivalent_properties_suffix:
                        equivalent_properties_suffix = ", ".join(equivalent_properties_suffix)
                    else:
                        equivalent_properties_suffix = ''
                    (defined_triples, cardinality) = LOV.search_triples(uri_property, DEFINED_BY, "")
                    defined_by = ''
                    for defined_triple in defined_triples:
                        defined_by = defined_triple[2].replace('"', '').encode('utf-8')
                    (domain_triples, cardinality) = LOV.search_triples(uri_property, DOMAIN, "")
                    domain = ''
                    for domain_triple in domain_triples:
                        domain = domain_triple[2].replace('"', '').encode('utf-8')
                    (range_triples, cardinality) = LOV.search_triples(uri_property, RANGE, "")
                    p_range = ''
                    for range_triple in range_triples:
                        p_range = range_triple[2].replace('"', '').encode('utf-8')
                    (occurences_triples, cardinality) = LOV.search_triples(uri_property, OCCURENCIES, "")
                    if cardinality > 0:
                        occurencies = int(occurences_triples.next()[2].split('^^')[0].replace('"', ''))
                    else:
                        occurencies = 0
                    (reused_triples, cardinality) = LOV.search_triples(uri_property, REUSED, "")
                    if cardinality > 0:
                        reused = int(reused_triples.next()[2].split('^^')[0].replace('"', ''))
                    else:
                        reused = 0
                    labels_and_descriptions = {}
                    (label_triples, cardinality) = LOV.search_triples(uri_property, LABEL, "")
                    for label_triple in label_triples:
                        label, language = split_string_lang(label_triple[2])
                        if language not in labels_and_descriptions:
                            labels_and_descriptions[language] = {}
                        labels_and_descriptions[language]['label'] = label
                    (description_triples, cardinality) = LOV.search_triples(uri_property, COMMENT, "")
                    for description_triple in description_triples:
                        description, language = split_string_lang(description_triple[2])
                        if language not in labels_and_descriptions:
                            labels_and_descriptions[language] = {}
                        labels_and_descriptions[language]['description'] = description
                    if labels_and_descriptions:
                        for language, value in labels_and_descriptions.iteritems():
                            if 'label' not in value:
                                label = uri_suffix
                            else:
                                label = value['label']
                            if 'description' not in value:
                                description = label
                            else:
                                description = value['description']
                            writer.writerow([uri_property, uri_prefix, uri_suffix, label, description, language, occurencies, reused, domain, p_range, sub_properties, equivalent_properties, equivalent_properties_suffix, defined_by])
                    else:
                        writer.writerow([uri_property, uri_prefix, uri_suffix, uri_suffix, uri_suffix, 'undefined', occurencies, reused, domain, p_range, sub_properties, equivalent_properties, equivalent_properties_suffix, defined_by])

def split_string_lang(obj):
    splitted_obj = obj.replace('"', '').split('@')
    string = splitted_obj[0].replace('^^http://www.w3.org/2001/XMLSchema#string', '').encode('utf-8')
    language = 'undefined'
    if len(splitted_obj) > 1:
        language = splitted_obj[1].encode('utf-8')
    return string, language

def get_uri_suffix(uri):
    if '#' in uri:
        return uri.rsplit('#', 1)[-1]
    else:
        return uri.rsplit('/', 1)[-1]

def get_uri_prefix(uri):
    if '#' in uri:
        return '{}#'.format(uri.rsplit('#', 1)[0])
    else:
        return '{}/'.format(uri.rsplit('/', 1)[0])

first_cap_re = re.compile('(.)([A-Z][a-z]+)')
all_cap_re = re.compile('([a-z0-9])([A-Z])')

def clean(str):
    # UnCamel, UnSnake ...
    s1 = first_cap_re.sub(r'\1_\2', str)
    s2 = all_cap_re.sub(r'\1_\2', s1).lower()
    return s2.replace('_', ' ').replace('-', ' ')

def main():
    parser = argparse.ArgumentParser(prog='lovcsv', description='LOV rdf to csv')
    parser.add_argument('properties_or_classes', metavar='PC', type=str, nargs='+',
                        help='select the csv dataset to generate')
    args = parser.parse_args()
    dataset = args.properties_or_classes[0]
    if dataset == "classes":
        generate_classes_dataset()
    else:
        generate_properties_dataset()

if __name__ == "__main__":
    main()


import re
import string
import copy
import os
from yap_comparison import *

class yap_cuff_merge_compare_check(yap_comparison_parent):

    '''
    Provides methods required to validate comparison files used in
    yap post processing
    '''

    def __init__(self, dict):
        yap_comparison_parent.__init__(self, dict)

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

    def get_groups(self, file_data):
        '''
        Given data from input file,
        Returns the dictionary of groups from this file
        '''
        if len(file_data) == 0:
            return {}  # return empty o/p
        groups = {}  # output variable
        for i in range(0, len(file_data)):
            line = file_data[i].strip('\n')  # remove new line character
            if len(line) > 0:  # continue only if line has some content
                # search current line for occurences of whitespace characters
                matchobj = re.match(r'\s*(\S*)(\s*).*', line, re.M | re.I)
                if matchobj is None:
                    return {}  # return empty if any invalid line
                delimiter = matchobj.groups()[1]  # get separator character
                if len(delimiter) <= 0:
                    return {}  # return empty if any invalid line
                all_sets = line.split(delimiter)  # get sets of group
                # remove empty elements
                all_sets = filter((lambda x: len(x) > 0), all_sets)
                # convert each elements to single element list to get common
                # formatting
                all_sets = map((lambda x: [x]), all_sets)
                # append list of sets to group only if non-empty
                if len(all_sets) > 0:
                    groups[i + 1] = all_sets
        return groups  # return output
#-------------------------------------------------------------------------


from pathlib import Path
import pickle
import re

from matplotlib import cm, lines, pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from utils import sort_file_with_tSiz

window=3

def get_paths():
    paths = list(Path("results/travel_dist/pickle").glob(f"BM{bm_size}_*{agent}*a0.1*g0.9*greedy0.1*.pickle"))
    paths = sort_file_with_tSiz(paths)
    pathr = list(Path("results/travel_dist/pickle").glob(f"BM{bm_size}_*RandomAgent*a0.1*g0.9*greedy0.1*.pickle"))
    return paths, pathr

def out_dist(pash):
    f_distr, r_distr = distribution(pash)
    d = pd.DataFrame({"first":f_distr, "last":r_distr})
    d.to_csv(f"{agent}_BM{bm_size}_first_last.csv")

def distribution(path):
    with open(path, "rb") as f:
        travel_dists = pickle.load(f)
    first_distr = []
    for travel_dist in travel_dists:
        first_distr += travel_dist[:window]
    last_distr = []
    for travel_dist in travel_dists:
        last_distr += travel_dist[-window:]
    return first_distr, last_distr

# plot histograms of first and last 5 trials, x is lo
def plot():
    paths, pathr = get_paths()
    fig, ax = plt.subplots(1, 1, figsize=(10, 5))
    # カラーマップから色を取得
    n_colors = len(paths)
    colors = cm.jet(np.linspace(0, 1, n_colors))
    for i, path in enumerate(paths):
        first_distr, last_distr = distribution(path)
        table_width = int(re.search(r'table_width_(\d*)\D', path.name).group(1))
        sns.kdeplot(first_distr, color=colors[i], linestyle="--", bw_adjust=0.5, log_scale=True)
        sns.kdeplot(last_distr, label=f'tSiz={table_width}', color=colors[i], bw_adjust=0.5, log_scale=True)
        print("plotting", path.name)
    first_distr_r, last_distr_r = distribution(pathr[0])
    sns.kdeplot(first_distr_r, color="gray",label='Random', bw_adjust=0.5, log_scale=True)
    print("plotting", pathr[0].name)

    # カスタム凡例エントリの作成
    legend_line_a = lines.Line2D([], [], color='black', marker='', linestyle='--', label=f'First {window} trials')
    legend_line_b = lines.Line2D([], [], color='black', marker='', linestyle='-', label=f'Last {window} trials')

    # 既存のプロットから凡例エントリを取得
    existing_legend = plt.gca().get_legend_handles_labels()

    # 既存の凡例エントリにカスタムエントリを追加
    handles, labels = existing_legend
    handles.extend([legend_line_a, legend_line_b])
    labels.extend([f'First {window} trials', f'Last {window} trials'])

    # 凡例をプロットに追加
    plt.legend(handles=handles, labels=labels)
    ax.set_xlabel("travel distance")
    ax.set_ylabel("frequency")
    ax.set_title(f"BM{bm_size} {agent}")
    plt.savefig(f"{save_path}")
    print(f"{save_path} saved")

for agent in ["Q", "SR"]:
    for bm_size in [1,3,5,10,20]:
        save_path = Path(f"results2/kde/{agent}_BM{bm_size}.png")
        save_path.parent.mkdir(parents=True, exist_ok=True)
        plot()


#!/usr/bin/env python3
# _*_ coding: utf-8 _*_

from datetime import datetime
from elasticsearch import Elasticsearch
import pprint

pp = pprint.PrettyPrinter(width=41, compact=True)

def main():
    es = Elasticsearch(
        ['https://3.231.208.103:64297/es/'],
        http_auth=('webuser', 'LINUX@4ever'),
        verify_certs=False,
    )

    res = es.search(
        index="logstash*",
        body={
            "size": 100,
            "query": {
                "range": {
                    "@timestamp": {
                        "gte": "now-1h"
                    }
                }
            }
        },
        scroll = '1m'
    )

    hits = res['hits']['hits']
    scroll_id = res['_scroll_id']
    doc_count = res['hits']['total']['value']

    while doc_count > 0:
        secEvents = []
        for event in hits:
            try:
                secEvent = {
                    'src_ip': event['_source']['src_ip'],
                    'dest_ip': event['_source']['dest_ip'],
                    'dest_port': event['_source']['dest_port']
                }
                if secEvent not in secEvents:
                    secEvents.append(secEvent)
            except:
                continue
        doc_count = doc_count - 100
        res = es.scroll(
            scroll_id=scroll_id,
            scroll = '1m'
        )
        hits = res['hits']['hits']
        scroll_id = res['_scroll_id']

    pp.pprint(secEvents)

if __name__ == "__main__":
    main()


class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        mapNum = {}
        for num in nums:
            if num in mapNum.keys():
                mapNum[num] += 1
            else:
                mapNum[num] = 1
        listMapNum = sorted(mapNum.items(), key = lambda x:x[1], reverse = True)
        res = []
        for i in range(k):
            res.append(listMapNum[i][0])
        return res


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import gin.tf
from disentanglement_lib.evaluation.metrics.beta_vae import compute_beta_vae_sklearn
from disentanglement_lib.evaluation.metrics.dci import _compute_dci, compute_dci
from disentanglement_lib.evaluation.metrics.factor_vae import compute_factor_vae
from disentanglement_lib.evaluation.metrics.mig import _compute_mig, compute_mig
from disentanglement_lib.evaluation.metrics.modularity_explicitness import modularity, explicitness_per_factor, \
    compute_modularity_explicitness
from disentanglement_lib.evaluation.metrics.sap_score import _compute_sap, compute_sap
import pandas as pd
from disentanglement_lib.evaluation.metrics.utils import _histogram_discretize

class GT:
    def __init__(self, discr=False, limit=True):
        d = pd.read_csv("../data/aling.csv")
        self.mu = np.array(d[['X0', 'X1']]).T
        self.y = np.array(d[['beta_untrans', 'persv']]).T

        dmu = self.mu.copy()
        dy = self.y.copy()

        if discr:
            dmu[0] = pd.qcut(dmu[0], q=15, labels=False)
            dmu[1] = pd.qcut(dmu[1], q=15, labels=False)
            self.mu = dmu

        dy[0] = pd.qcut(dy[0], q=15, labels=False)
        dy[1] = pd.qcut(dy[1], q=15, labels=False)
        self.y = dy

        self.num_factors = 2

        self.d = {}
        for i in range(self.y.shape[1]):
            self.d[str(self.y[:, i])] = i

        np.random.seed(00)
        self.cur_sample = np.arange(0, self.y.shape[1])
        np.random.shuffle(self.cur_sample)
        self.cur_index = 0
        self.limit = limit

    def sample(self, n_points, state):
        out = np.zeros((2, n_points))
        i = 0
        cur_i = []
        while i < n_points:
            out[:, i] = self.y[:, self.cur_sample[self.cur_index]]
            cur_i.append(self.cur_sample[self.cur_index])
            self.cur_index += 1
            if not self.limit:
                if self.cur_index >= 1500:
                    self.cur_index = 0
            i += 1

        return out.T, cur_i

    def sample_observations(self, p, r):
        _, o = self.sample(p, r)
        return np.array(o)

    def sample_factors(self, num, random_state):
        return self.sample(num, random_state)[0]

    def sample_observations_from_factors(self, factors, r):
        out = []
        for f in range(factors.shape[0]):
            out.append(self.d[str(factors[f, :])])
        return out

def reps_fun(gt):
    def reprs(obs):
        reps = np.zeros((2, len(obs)))
        for f in range(len(obs)):
            reps[:, f] = gt.mu[:, obs[f]]
        return reps.T
    return reprs

if __name__ == "__main__":
    gin.bind_parameter("discretizer.discretizer_fn", _histogram_discretize)
    gin.bind_parameter("discretizer.num_bins", 10)

    gt = GT(True)
    print(compute_mig(gt, reps_fun(gt), num_train=1500, random_state=2020))
    #
    gt = GT(True)
    print(compute_dci(gt, reps_fun(gt), num_train=1000, random_state=2020, num_test=500))
    #
    gt = GT(True)
    print(compute_sap(gt, reps_fun(gt), num_train=1000, random_state=np.random.RandomState(0), num_test=500, continuous_factors=False))
    #
    gt = GT(True)
    print(compute_modularity_explicitness(gt, reps_fun(gt), num_train=1000, random_state=np.random.RandomState(0), num_test=500))

    gt = GT(False, False)
    print(compute_beta_vae_sklearn(gt, reps_fun(gt), num_train=1000, batch_size=64, num_eval=500,
                                   random_state = np.random.RandomState(0)))
    gt = GT(False, False)
    print(compute_factor_vae(gt, reps_fun(gt), num_train=1000, batch_size=64, num_eval=500,
                                   random_state = np.random.RandomState(0), num_variance_estimate=10))


#!/usr/bin/env python3

# PURPOSE
#
# Provides local machine access to do system-administrative actions:
#  - Use SQLAlchemy and alembic to perform git-controlled database migrations:
#        python manage.py db revision --autogenerate -m "An explanatory message"
#        python manage.py db upgrade
#  - Seed the database:
#        python manage.py seed_db
#
# HOW TO USE
#
# If you need to make a change to a model, generate an upgrade script with the
# "db revision" command above, then modify/commit the resulting script in
# /migrations/
#
# As part of the deploy process, you should run the "db upgrade" command above,
# then the "seed_db" command above. These two commands are automatically called
# in the heroku deploy because they are referenced in ./Procfile

import os

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from app_init import app, db
from models import User, Customer, Employee, Store, UnitType, ItemType

migrate = Migrate(app, db)
manager = Manager(app)

manager.add_command('db', MigrateCommand)

@manager.command
def seed_db():
    try:
        # Obviously get rid of these before moving to production
        if User.query.count() == 0:
                db.session.add(Customer(67417, "hunter2", "Test", "Customer"))
                db.session.add(Employee(67416, "hunter3", "Test", "User"))

        test_customer = Customer.query.filter_by(loyalty_id=67417).first()
        if not test_customer.phone or test_customer.phone == "+1-111-555-1212":
            test_customer.phone = "+12128675309"
        if not test_customer.email:
            test_customer.email = "testemail@example.com"
        if not test_customer.address1:
            test_customer.address1 = "321 Anywhere St"
            test_customer.address2 = ""
            test_customer.city = "Columbus"
            test_customer.state = "NC"
            test_customer.zip_code = "12343"

        if Store.query.count() == 0:
            db.session.add(Store("Goodwill Omaha Headquarters"))

        if ItemType.query.count() == 0:
            db.session.add(ItemType("Clothing"))
            db.session.add(ItemType("Furniture"))
            db.session.add(ItemType("Wares"))
            db.session.add(ItemType("Misc"))

        if UnitType.query.count() == 0:
            db.session.add(UnitType("Box"))
            db.session.add(UnitType("Bag"))
            db.session.add(UnitType("Each"))

        db.session.commit()
    except:
        db.session.rollback()
        raise

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


import cv2
import numpy as np
import tensorflow as tf

from tensorflow.keras.layers import Input

try:
    from src.yolo3.model import *
    from src.yolo3.detect import *

    from src.utils.image import *
    from src.utils.datagen import *
    from src.utils.fixes import *
except:
    from script.src.yolo3.model import *
    from script.src.yolo3.detect import *

    from script.src.utils.image import *
    from script.src.utils.datagen import *
    from script.src.utils.fixes import *
import base64

fix_tf_gpu()

def prepare_model(approach):
    '''
    Prepare the YOLO model
    '''
    global input_shape, class_names, anchor_boxes, num_classes, num_anchors, model

    # shape (height, width) of the imput image
    input_shape = (416, 416)

    # class names
    if approach == 1:
        class_names = ['H', 'V', 'W']

    elif approach == 2:
        class_names  = ['W','WH','WV','WHV']

    elif approach == 3:
        class_names  = ['W']

    else:
        raise NotImplementedError('Approach should be 1, 2, or 3')

    # anchor boxes
    if approach == 1:
        anchor_boxes = np.array(
            [
            np.array([[ 76,  59], [ 84, 136], [188, 225]]) /32, # output-1 anchor boxes
            np.array([[ 25,  15], [ 46,  29], [ 27,  56]]) /16, # output-2 anchor boxes
            np.array([[ 5,    3], [ 10,   8], [ 12,  26]]) /8   # output-3 anchor boxes
            ],
            dtype='float64'
        )
    else:
        anchor_boxes = np.array(
            [
            np.array([[ 73, 158], [128, 209], [224, 246]]) /32, # output-1 anchor boxes
            np.array([[ 32,  50], [ 40, 104], [ 76,  73]]) /16, # output-2 anchor boxes
            np.array([[ 6,   11], [ 11,  23], [ 19,  36]]) /8   # output-3 anchor boxes
            ],
            dtype='float64'
        )

    # number of classes and number of anchors
    num_classes = len(class_names)
    num_anchors = anchor_boxes.shape[0] * anchor_boxes.shape[1]

    # input and output
    input_tensor = Input( shape=(input_shape[0], input_shape[1], 3) ) # input
    num_out_filters = ( num_anchors//3 ) * ( 5 + num_classes )        # output

    # build the model
    model = yolo_body(input_tensor, num_out_filters)

    # load weights
    weight_path = f'script/model_data/weights/pictor-ppe-v302-a{approach}-yolo-v3-weights.h5'
    model.load_weights( weight_path )

def get_detection(img):
    # save a copy of the img
    act_img = img.copy()

    # shape of the image
    ih, iw = act_img.shape[:2]

    # preprocess the image
    img = letterbox_image(img, input_shape)
    img = np.expand_dims(img, 0)
    image_data = np.array(img) / 255.

    # raw prediction from yolo model
    prediction = model.predict(image_data)

    # process the raw prediction to get the bounding boxes
    boxes = detection(
        prediction,
        anchor_boxes,
        num_classes,
        image_shape = (ih, iw),
        input_shape = (416,416),
        max_boxes = 10,
        score_threshold=0.3,
        iou_threshold=0.45,
        classes_can_overlap=False)

    # convert tensor to numpy
    boxes = boxes[0].numpy()

    # draw the detection on the actual image
    return draw_detection(act_img, boxes, class_names)

def search_items(img):
    prepare_model(approach=1)
    img = base64.b64decode(img)
    img = np.frombuffer(img, np.uint8)
    img = cv2.imdecode(img, cv2.IMREAD_COLOR)
    img = letterbox_image(img, input_shape)
    img = get_detection(img)
    _, img = cv2.imencode('.jpg', img)
    img = base64.b64encode(img)
    return img.decode('utf-8')


import cv2
import numpy as np
from random import expovariate

refPT = list()

class ModelTransformer:
    def __init__(self, model, first_frame, hsv_values, with_points=False):
        self.H = np.identity(3, np.float32)
        self.hsv_low, self.hsv_high = hsv_values

        self.last_frame = first_frame
        self.rows, self.cols, channels = first_frame.shape
        self.model = cv2.resize(model.image, (self.cols, self.rows))

        frame_pts = list()
        model_pts = list()
        if with_points:
            self.refPT = list()
            cv2.imshow('frame', first_frame)
            cv2.setMouseCallback("frame", self.click_gather_point)
            cv2.waitKey()
            cv2.destroyAllWindows()
            frame_pts = self.refPT
            self.refPT = list()

            cv2.imshow('model', self.model)
            cv2.setMouseCallback("model", self.click_gather_point)
            cv2.waitKey()
            cv2.destroyAllWindows()
            model_pts = self.refPT

        else:
            frame_pts.append((85, 125))
            frame_pts.append((353, 107))
            frame_pts.append((167, 258))
            frame_pts.append((681, 197))

            model_pts.append((503, 49))
            model_pts.append((605, 49))
            model_pts.append((505, 289))
            model_pts.append((606, 289))

        self.H = cv2.getPerspectiveTransform(np.float32(frame_pts), np.float32(model_pts))
        self.last_H = self.H
        self.last_good_H = self.H
        self.idx = 0

    def click_gather_point(self, event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            print(len(self.refPT), (x, y))
            point = (x, y)
            self.refPT.append(point)

    def new_frame(self, frame):
        self.idx += 1
        #warped_frame = cv2.warpPerspective(frame, self.H, (self.cols, self.rows))
        #warped_mask = cv2.warpPerspective(np.ones(frame.shape, np.uint8)*255, self.H, (self.cols, self.rows))

        #lines = self.find_global_lines(frame)
        counter = 0
        max_percent_good_lines = 200000
        best_transform = [max_percent_good_lines, self.last_good_H]
        while 1:#best_transform[0] >= max_percent_good_lines:
            #M = self.get_homography_between_frames(frame, self.last_frame)
            M = self.get_homography_between_lines(frame, self.last_frame)
            if M is None or M.shape != (3, 3):
                break
            #M = np.linalg.inv(M)
            tmp_H = np.dot(self.last_H, M)
            #tmp_H = np.dot(self.H, M)
            percent_good_lines = np.power(tmp_H - self.H, 2).sum() / 9
            #percent_good_lines = self.are_lines_vertical_or_horizontal_in_model(lines, tmp_H)

            # if percent_good_lines > best_transform[0]:
            if percent_good_lines < best_transform[0]:
                best_transform[0] = percent_good_lines
                best_transform[1] = tmp_H

            if counter >= 0:
                break
            counter += 1

        print(best_transform[0])
        if best_transform[0] < max_percent_good_lines:
            self.last_good_H = best_transform[1]

        self.H = best_transform[1]
        #self.last_frame = frame

    def get_homography_between_frames(self, img1, img2, mask=None):
        cv2.ocl.setUseOpenCL(False)

        # Initiate SIFT detector
        kernel_size = 9
        orb = cv2.ORB_create(1000, 1.2, 8, kernel_size, 0, 4, cv2.ORB_HARRIS_SCORE, kernel_size, 20)

        mask1 = mask
        mask2 = mask

        if mask is None:
            mask1 = 255 - self.get_terrain_mask(img1)
            mask2 = 255 - self.get_terrain_mask(img2)

            #line_mask1, _ = self.line_mask(img1)
            #line_mask2, _ = self.line_mask(img2)

            #mask1 = cv2.bitwise_or(mask1, line_mask1[:, :, 0])
            #mask2 = cv2.bitwise_or(mask2, line_mask2[:, :, 0])

        # find the keypoints and descriptors with SIFT
        kp1, des1 = orb.detectAndCompute(img1, None)#mask1)
        kp2, des2 = orb.detectAndCompute(img2, None)#mask2)

        # create BFMatcher object
        bf = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=True)

        # Match descriptors.
        matches = bf.match(des1, des2)

        # Sort them in the order of their distance.
        matches = sorted(matches, key=lambda x: x.distance)
        matches_int = np.asarray([(m.queryIdx, m.trainIdx) for m in matches], np.int)

        random_draw = list()
        for i in range(8):
            value = int(expovariate(1/4))
            while value in random_draw:
                value = int(expovariate(1/4))
            random_draw.append(value)
        random_draw = np.asarray(random_draw, np.int)

        src_pts = np.float32([kp1[m[0]].pt for m in matches_int[random_draw]]).reshape(-1, 1, 2)
        dst_pts = np.float32([kp2[m[1]].pt for m in matches_int[random_draw]]).reshape(-1, 1, 2)

        img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:4], None,  matchColor=(0, 255, 0), flags=0)
        cv2.imshow('matches', img3)
        #cv2.waitKey()

        M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 40)
        #M = cv2.getPerspectiveTransform(src_pts, dst_pts)
        return M

    def get_terrain_mask(self, frame):
        hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv_image, self.hsv_low, self.hsv_high)

        mask = cv2.erode(mask, np.ones((3, 3)))
        mask = cv2.dilate(mask, np.ones((31, 31)))
        ret, thresh = cv2.threshold(mask, 127, 255, 0)
        im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(mask, contours, -1, 255, cv2.FILLED)

        return mask

    def find_lines(self, frame):
        list_of_line_points = list()

        mask = self.get_terrain_mask(frame)

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        blured_frame = cv2.blur(gray, (7, 7))

        #cv2.imshow('blured_frame', blured_frame)

        edges = cv2.Canny(blured_frame, 20, 50, apertureSize=3)
        edges = cv2.blur(edges, (3, 3))

        #cv2.imshow('Canny_blurred', edges)

        edges = cv2.bitwise_and(mask, edges)

        #cv2.imshow('Canny_cut', edges)

        lines = cv2.HoughLinesP(edges, 1, np.pi/180, 400, 50, 125)[:, 0, :]
        for x1, y1, x2, y2 in lines:
            list_of_line_points.append(((x1, y1, 1), (x2, y2, 1)))

        list_of_line_points = np.float32(list_of_line_points)

        #cv2.imshow('all_lines', self.line_mask(np.zeros(frame.shape, np.uint8), list_of_line_points[:, :, :2]))
        #cv2.waitKey()

        return list_of_line_points

    def are_lines_vertical_or_horizontal_in_model(self, list_of_line_points, H):
        count_good_lines = 0

        for p1, p2 in list_of_line_points:
            p1_prime = np.dot(H, (p1[0], p1[1], 1))
            p2_prime = np.dot(H, (p2[0], p2[1], 1))

            p1_prime /= p1_prime[-1]
            p2_prime /= p2_prime[-1]

            x_pos = [69, 171, 197, 223, 248, 274, 298, 325, 349, 375, 401, 426, 478, 504, 555, 579,
                     606, 630, 656, 683, 707, 733, 834]
            y_pos = [49, 432]

            if p2_prime[0] - 10 <= p1_prime[0] <= p2_prime[0] + 10:
                mid_x = p1_prime[0] + p2_prime[0]
                mid_x /= 2
                for x in x_pos:
                    if x - 10 <= mid_x <= x + 10:
                        count_good_lines += 1
                        break

            if p2_prime[1] - 10 <= p1_prime[1] <= p2_prime[1] + 10:
                mid_y = p1_prime[1] + p2_prime[1]
                mid_y /= 2
                for y in y_pos:
                    if y - 10 <= mid_y <= y + 10:
                        count_good_lines += 1
                        break

        return count_good_lines / len(list_of_line_points)

    def find_global_lines(self, frame):
        pts = self.find_lines(frame)
        clusters = []
        cluster_values = []

        new_lines = []

        for i, (pt1, pt2) in enumerate(pts):

            if pt1[0] > pt2[0]:
                pt_tmp = pt1
                pt2 = pt1
                pt1 = pt2

                pts[i] = (pt1, pt2)

            pente = (pt2[1] - pt1[1]) / (pt2[0] - pt1[0])
            if np.isnan(pente):
                pente = frame.shape[0]
            elif np.isinf(pente):
                pente = 0

            y_intercept = pt1[1] - pente * pt1[0]

            x_intercept = (frame.shape[0] / 2 - y_intercept) / pente
            if np.isnan(x_intercept) or np.isinf(x_intercept):
                x_intercept = y_intercept

            x1 = 0
            y1 = pente * x1 + y_intercept
            x2 = frame.shape[1]
            y2 = pente * x2 + y_intercept

            pt1 = np.asarray([x1, y1], np.int)
            pt2 = np.asarray([x2, y2], np.int)
            rectangle = (0, 0, frame.shape[1], frame.shape[0])
            _, pt1, pt2 = cv2.clipLine(rectangle, (pt1[0], pt1[1]), (pt2[0], pt2[1]))

            new_lines.append([pt1, pt2])

            found = False
            for index, values in enumerate(cluster_values):
                if np.abs(pente) <= 0.1:
                    if y_intercept - 10 <= values['y_intercept'] <= y_intercept + 10:
                        clusters[index].append(i)
                        found = True
                        break
                else:
                    if x_intercept - 20 <= values['x_intercept'] <= x_intercept + 20:
                        clusters[index].append(i)
                        found = True
                        break

            if not found:
                cluster_values.append({'pente': pente, 'x_intercept': x_intercept, 'y_intercept': y_intercept})
                clusters.append([i])

        new_lines = np.asarray(new_lines, np.int)

        global_lines = []
        for index, cluster in enumerate(clusters):
            if len(cluster) < 2:
                continue

            pt1, pt2 = (new_lines[cluster].sum(axis=0) / len(cluster)).astype(int)
            rectangle = (0, 0, frame.shape[1], frame.shape[0])
            clipped = cv2.clipLine(rectangle, (pt1[0], pt1[1]), (pt2[0], pt2[1]))
            _, pt1, pt2 = clipped
            global_lines.append([np.asarray(pt1), np.asarray(pt2)])

        for i in range(len(global_lines)):
            for j in range(len(global_lines)):
                if i == j:
                    continue

                o1, p1 = global_lines[i]
                o2, p2 = global_lines[j]

                x = o2 - o1
                d1 = p1 - o1
                d2 = p2 - o2

                cross = (d1[0] * d2[1]) - (d1[1] * d2[0])

                if np.abs(cross) > 1e-8:
                    t1 = ((x[0] * d2[1]) - (x[1] * d2[0])) / cross
                    r = (o1 + d1 * t1).astype(int)

                    max_1 = np.max([o1, o2], axis=0)
                    min_1 = np.min([o1, o2], axis=0)
                    max_2 = np.max([p1, p2], axis=0)
                    min_2 = np.min([p1, p2], axis=0)

                    max = np.max([max_1, min_1, max_2, min_2], axis=0)
                    min = np.min([max_1, min_1, max_2, min_2], axis=0)

                    if min[0] <= r[0] <= max[0] and min[1] <= r[1] <= max[1]:
                        pente_1 = (p1[1] - o1[1]) / (p1[0] - o1[1])
                        pente_2 = (p2[1] - o2[1]) / (p2[0] - o2[1])
                        if np.abs(pente_1) >= 0.1:
                            if pente_1 >= 0:
                                global_lines[i][0] = r
                            else:
                                global_lines[i][1] = r
                        if np.abs(pente_2) >= 0.1:
                            if pente_2 >=0:
                                global_lines[j][0] = r
                            else:
                                global_lines[j][1] = r

        return np.asarray(global_lines)

    def line_mask(self, mask, global_lines):
        #print(global_lines)

        for line in global_lines:
            (o1, p1), (o2, p2) = line
            cv2.line(mask, (o1, p1), (o2, p2), (255, 255, 255), 3)

        return mask

    def match_lines(self, lines_1, lines_2):
        matches_dst = np.zeros((len(lines_1), len(lines_2)), np.float32)

        for i, line_i in enumerate(lines_1):
            for j, line_j in enumerate(lines_2):
                matches_dst[i, j] = np.power(line_i - line_j, 2).sum() / 4

        matches = list()
        for i in range(len(lines_1)):
            best_match = None
            for j in range(len(lines_2)):
                if best_match is None:
                    best_match = [j, matches_dst[i, j]]
                elif best_match[1] > matches_dst[i, j]:
                    best_match[0] = j
                    best_match[1] = matches_dst[i, j]

            matches.append([[i, best_match[0]], best_match[1]])

        return matches

    def get_homography_between_lines(self, img1, img2):
        mask = np.zeros(img1.shape, np.uint8)
        global_lines_1 = self.find_global_lines(img1)
        global_lines_2 = self.find_global_lines(img2)

        matches = self.match_lines(global_lines_1, global_lines_2)

        matches = sorted(matches, key=lambda x: x[1])

        print(matches)

        src_pts = list()
        dst_pts = list()

        colors1 = ((255, 0, 0), (0, 255, 0), (0, 0, 255))
        colors2 = ((128, 0, 0), (0, 128, 0), (0, 0, 128))
        for i in range(min(3, len(matches) - 1)):
            index_1, index_2 = matches[i][0]

            pt11, pt12 = global_lines_1[index_1]
            pt21, pt22 = global_lines_2[index_2]

            src_pts.append(pt11)
            src_pts.append(pt12)
            dst_pts.append(pt21)
            dst_pts.append(pt22)

            cv2.line(mask, (pt11[0], pt11[1]), (pt12[0], pt12[1]), colors1[i], 2, cv2.LINE_AA)
            cv2.line(mask, (pt21[0], pt21[1]), (pt22[0], pt22[1]), colors2[i], 2, cv2.LINE_AA)

        cv2.imshow('matching lines', mask)
        cv2.waitKey(1)

        src_pts = np.float32(src_pts)
        dst_pts = np.float32(dst_pts)

        try:
            M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 20)
        except Exception as e:
            print(e)
            M = None

        return M


# import prodigy
# from prodigy.components.preprocess import add_tokens
# import requests
# import spacy

# @prodigy.recipe("summary-error-analysis")
# def summary_error_analysis(dataset, lang="en"):
#     # We can use the blocks to override certain config and content, and set
#     # "text": None for the choice interface so it doesn't also render the text
#     blocks = [
#         # {"view_id": "ner_manual"},
#         # {"view_id": "choice", "text": None},
#         # {"view_id": "text_input", "field_rows": 3, "field_label": "Explain your decision"}
#         {"view_id": "html"},
#         {"view_id": "spans_manual"},
#     ]

#     def get_stream():
#         res = requests.get("https://cat-fact.herokuapp.com/facts").json()
#         for fact in res["all"]:
#             yield {"text": fact["text"], "options": options}

#     # Load the stream from a JSONL file and return a generator that yields a
#     # dictionary for each example in the data.
#     stream = JSONL(source)

#     # Tokenize the incoming examples and add a "tokens" property to each
#     # example. Also handles pre-defined selected spans. Tokenization allows
#     # faster highlighting, because the selection can "snap" to token boundaries.
#     stream = add_tokens(nlp, stream)

#     nlp = spacy.blank(lang)           # blank spaCy pipeline for tokenization
#     stream = get_stream()             # set up the stream
#     stream = add_tokens(nlp, stream)  # tokenize the stream for ner_manual

#     return {
#         "dataset": dataset,          # the dataset to save annotations to
#         "view_id": "blocks",         # set the view_id to "blocks"
#         "stream": stream,            # the stream of incoming examples
#         "config": {
#             "labels": ["RELEVANT"],  # the labels for the manual NER interface
#             "blocks": blocks         # add the blocks to the config
#         }
#     }

import prodigy
from prodigy.components.loaders import JSONL
from prodigy.components.preprocess import add_tokens
from prodigy.util import split_string
import spacy
from typing import List, Optional

# Recipe decorator with argument annotations: (description, argument type,
# shortcut, type / converter function called on value before it's passed to
# the function). Descriptions are also shown when typing --help.
@prodigy.recipe(
    "summary.manual",
    dataset=("The dataset to use", "positional", None, str),
    spacy_model=("The base model", "positional", None, str),
    source=("The source data as a JSONL file", "positional", None, str),
    label=("One or more comma-separated labels", "option", "l", split_string),
    exclude=("Names of datasets to exclude", "option", "e", split_string),
)
def summary_manual(
    dataset: str,
    spacy_model: str,
    source: str,
    label: Optional[List[str]] = None,
    exclude: Optional[List[str]] = None,
):
    """
    Mark spans manually by token. Requires only a tokenizer and no entity
    recognizer, and doesn't do any active learning.
    """
    # Load the spaCy model for tokenization
    nlp = spacy.load(spacy_model)

    # Load the stream from a JSONL file and return a generator that yields a
    # dictionary for each example in the data.
    stream = JSONL(source)

    # Tokenize the incoming examples and add a "tokens" property to each
    # example. Also handles pre-defined selected spans. Tokenization allows
    # faster highlighting, because the selection can "snap" to token boundaries.
    stream = add_tokens(nlp, stream)

    # We can use the blocks to override certain config and content, and set
    # "text": None for the choice interface so it doesn't also render the text
    blocks = [
        # {"view_id": "ner_manual"},
        # {"view_id": "choice", "text": None},
        # {"view_id": "text_input", "field_rows": 3, "field_label": "Explain your decision"}
        {"view_id": "html"},
        {"view_id": "spans_manual"},
    ]

    return {
        "view_id": "blocks",  # Annotation interface to use
        "dataset": dataset,  # Name of dataset to save annotations
        "stream": stream,  # Incoming stream of examples
        "exclude": exclude,  # List of dataset names to exclude
        "config": {  # Additional config settings, mostly for app UI
            "lang": nlp.lang,
            "labels": label,  # Selectable label options
            "blocks": blocks         # add the blocks to the config
        },
    }


import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
import cartons

load_dotenv()

TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

bot = commands.Bot(command_prefix='!')

@bot.command(name="combien", help="Donne les totaux des cartons")
async def combien(ctx):
    message = []
    for carton in cartons.fethAllCartons():
        user = discord.utils.find(lambda u: u.id == carton.user, bot.users)
        emoji = discord.utils.find(lambda e: e.id == carton.carton, bot.emojis)
        message.append(f'{user.name} à {carton.nb} <:{emoji.name}:{carton.carton}>')
    if len(message) == 0:
        message.append('Aucun carton')
    await ctx.send("\n".join(message))

@bot.command(name="carton", help="Donne les totaux de tes cartons")
async def carton(ctx, pseudo=''):
    if pseudo == '':
        pseudo = ctx.author.name
    u = discord.utils.find(lambda u: u.name == pseudo, bot.users)
    print(f'showing carton pour {pseudo}({u.id})')
    message = []
    for carton in cartons.fethAllCartons():
        if carton.user != u.id:
            continue
        emoji = discord.utils.find(lambda e: e.id == carton.carton, bot.emojis)
        message.append(f'{u.name} à {carton.nb} <:{emoji.name}:{carton.carton}>')
    if len(message) == 0:
        message.append('Aucun carton')
    await ctx.send("\n".join(message))

@bot.event
async def on_ready():
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

bot.run(TOKEN)


import os
from tqdm import tqdm
import cv2
from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from transformers import TransfoXLCorpus
import helper
import torch
import torch.optim  as optim
import torch.nn as nn
import torchvision
from torchvision import datasets
from torch.utils.data import Dataset,DataLoader
from  torchsummary import summary
import albumentations as A
import segmentation_models_pytorch as smp
from segmentation_models_pytorch.losses import DiceLoss,FocalLoss,JaccardLoss
from sklearn.model_selection import train_test_split

os.chdir('C:/ritika/kaggle_hackathons')

CSV_FILE='road_dataset/train.csv'
DATA_DIR='road_dataset/'

DEVICE='cpu'
EPOCHS=2
LR=0.003
BATCH_SIZE=8
IMG_SIZE=512

ENCODER= 'timm-efficientnet-b0'
WEIGHTS='imagenet'

sample_img= cv2.imread('road_dataset/images/10528675_15.png')
print(sample_img.shape)
df=pd.read_csv(CSV_FILE)

print(df.head())

#idx=20
#row=df.iloc[idx]

#image_path= DATA_DIR + row.images
#mask_path= DATA_DIR + row.masks
# reading the sample image
#image=cv2.imread(image_path)
#image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

##mask= cv2.imread(mask_path)
#mask= cv2.cvtColor(mask,cv2.IMREAD_GRAYSCALE)/255

#f,(ax1,ax2)= plt.subplots(1,2, figsize=(10,5))

#ax1.set_title('IMAGE')
#ax1.imshow(image)

#ax2.set_title('GROUND TRUTH')
#ax2.imshow(mask,cmap='gray')

#plt.show()

train_df,valid_df= train_test_split(df,test_size=0.20,random_state=42)
print(len(train_df))
print(len(valid_df))

# Augmentation transforms
def train_augs():
    return A.Compose([A.Resize(IMG_SIZE,IMG_SIZE),A.HorizontalFlip(p=0.5),A.VerticalFlip(p=0.5)])

def valid_augs():
    return A.Compose([A.Resize(IMG_SIZE,IMG_SIZE)])

# create custom dataset
class SegmentationDataset(Dataset):

    def __init__(self,df,augmentations):
        self.df=df
        self.augmentations=augmentations

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        row=self.df.iloc[idx]
        image_path= DATA_DIR + row.images
        mask_path= DATA_DIR + row.masks
        image=cv2.imread(image_path)
        image= cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        mask= cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
        mask= np.expand_dims(mask,axis=-1)

        if self.augmentations:
            data= self.augmentations(image=image,mask=mask)
            image= data['image']
            mask= data['mask']

        image= np.transpose(image,(2,0,1)).astype(np.float32)
        mask= np.transpose(mask,(2,0,1)).astype(np.float32)
        image= torch.Tensor(image) / 255.0
        mask= torch.round(torch.Tensor(mask) /255.0)

        return image,mask

trainset= SegmentationDataset(train_df,train_augs())
validset= SegmentationDataset(valid_df,valid_augs())

print(len(trainset))
print(len(validset))

idx=25

image,mask= trainset[idx]
helper.show_image(image,mask)
plt.show()

# Load the dataset into batches

train_loader= DataLoader(trainset,batch_size=BATCH_SIZE,num_workers=0,shuffle=True)
valid_loader= DataLoader(validset,batch_size=BATCH_SIZE)
## total no of batches in trainloader ####

print(len(train_loader))

print(len(valid_loader))

for i,m in train_loader:
    print(f"One batch image shape :{i.shape }")
    print(f"One batch mask shape :{m.shape}")
    break;

##### Creating Segmentation model ####################################
class SegmentationModel(nn.Module):
    def __init__(self):
        super(SegmentationModel,self).__init__()
        self.backbone= smp.Unet(encoder_name=ENCODER, encoder_weights=WEIGHTS, in_channels=3,classes=1,activation=None)

    def forward(self,images,masks=None):
        logits= self.backbone(images)
        if mask != None :
            return logits,DiceLoss(mode='binary')(logits,masks) + nn.BCEWithLogitsLoss()(logits,masks)
        else:
            return logits

model = SegmentationModel()
print(model)
model.to(DEVICE)

###### Creating train and valid function #####

def train_fn(dataloader,model,optimizer):
    ### set the model to training mode
    model.train()
    total_loss=0.0
    for images,masks in tqdm(dataloader):
        images=images.to(DEVICE)
        masks= masks.to(DEVICE)
        optimizer.zero_grad()
        logits,loss =model(images,masks)
        loss.backward()
        optimizer.step()
        total_loss+=loss.item()

    return total_loss/len(dataloader)

def valid_fn(dataloader,model):
    # set the model to evaluation mode
    model.eval()
    total_loss=0.0
    with torch.no_grad():
        for images,masks in tqdm(dataloader):
            images=  images.to(DEVICE)
            masks= masks.to(DEVICE)
            logits,loss= model(images,masks)
            total_loss+=loss.item()
        return total_loss/len(dataloader)

optimizer= torch.optim.Adam(model.parameters(),lr=LR)
best_loss= np.Inf

for i in range(EPOCHS):
    train_loss= train_fn(train_loader,model,optimizer)
    valid_loss= valid_fn(valid_loader,model)

    if valid_loss < best_loss:
        torch.save(model.state_dict(),'road_dataset/best_model.pth')
        print("saved model")
        best_loss= valid_loss

    print(f"Epoch:{i+1} Train Loss:{train_loss} Valid_loss :{valid_loss}")

idx=30
model.load_state_dict(torch.load('road_dataset/best_model.pth'))
image,mask= validset[idx]
# adding extra dimension for the batch
logits_mask= model(image.to(DEVICE).unsqueeze(0))
pred_mask = torch.sigmoid(logits_mask)
pred_mask= (pred_mask>0.5) *1.0

helper.show_image(image,mask,pred_mask.detach().cpu().squeeze(0))
plt.show()


import math
import sys

print("ax2 + bx + c = 0")

a = float(input("a? "))
b = float(input("b? "))
c = float(input("c? "))

if a == 0:
    print("a = 0 :이차방정식이 아닙니다.")
    sys.exit()

D = b*b-4*a*c

if D > 0:
    x1 = (-b+math.sqrt(D))/(2*a)
    x2 = (-b-math.sqrt(D))/(2*a)
    print("2개의 해 :", x1, x2)
if D == 0:
    x = -b/(2*a)
    print("1개의 해 :", x)
if D < 0:
    print("해가 없습니다.")


# encoding: utf-8
# ------------ 网易 ---------------
# 共有n个怪兽，每个怪兽欧有两个属性，破防能力和伤害力
# 当勇者的防御力大于怪兽破防能力，勇者不受伤害，否则勇者受到怪兽受害力
# 勇者初时防御力D，每挑战成功一只怪兽防御力可以+1，求打败所有怪兽最少承受多少伤害
# 输入
# 3 50            n = 3；D = 50
# 100 50 51       怪兽的破防能力
# 1000 1000 1000  怪兽的伤害
# 输出
# 1000
# 先挑战第一只怪兽，勇者防御力50<怪兽破防能力100，勇者受到1000伤害，勇者防御力+1
# 之后挑战第二只怪兽，勇者防御力51>怪兽破防能力50，勇者不受伤害，勇者防御力+1
# 最后挑战第三只怪兽，勇者防御力52>怪兽破防能力51，勇者不受伤害，勇者防御力+1
# 一共受到1000伤害

import operator
import sys

# ---------- 双指针 ----------------------
# 如果有能不吃伤害打的，直接打
# 如果没有能不吃伤害打的，直接打破防最强的
def main():
    tmp = list(map(int, sys.stdin.readline().strip().split(' ')))
    n, D = tmp[0], tmp[1]
    defend = list(map(int, sys.stdin.readline().strip().split(' ')))
    attack = list(map(int, sys.stdin.readline().strip().split(' ')))
    monster = []
    for i in range(n):
        monster.append((defend[i], attack[i]))
    monster.sort(key=operator.itemgetter(0))
    res = 0  # 受到的总伤害
    left = 0  # 从前向后打：怪兽攻防能力越来越强，如果D< defend[j]，则后面的都打不过
    right = n - 1  # 从后向前打：怪兽攻防能力越来越若
    while left <= right:  # 判断怪兽是否被打完
        # 从前向后
        while left <= right and D > monster[left][0]:  # 可以打这个怪兽
            D += 1  # 防御力 + 1
            left += 1  # 判断下一个
        # 从后向前
        if left <= right:
            res += monster[right][1]
            D += 1  # 防御力 + 1
            right -= 1  # 判断下一个
    print(res)

main()


import re
import pandas as pd
from sklearn.externals import joblib

def clean_html(raw_html):
  cleanr = re.compile('<.*?>')
  cleantext = re.sub(cleanr, '', raw_html)
  return cleantext

def extract_sku_text_features(train_file, test_file, feat_train_file, feat_test_file,
                                analyzer='char', ngram_range=(1, 4), lowercase=True, stem=False, stop_words=None):
    feat_names = []
    df = pd.read_csv(train_file, header=None, names=['country','sku_id','title','category_lvl_1','category_lvl_2','category_lvl_3','short_description','price','product_type'])
    df1 = pd.read_csv(test_file, header=None, names=['country','sku_id','title','category_lvl_1','category_lvl_2','category_lvl_3','short_description','price','product_type'])

    n_trains = df.shape[0]

    df = pd.concat((df,df1))

    df['sku1'] = df['sku_id'].map(lambda x: x[:5])
    df['sku2'] = df['sku_id'].map(lambda x: x[5:9])

    feat= pd.crosstab(index=df['category_lvl_1'], columns=df['sku1'])
    feat.columns = ['cat1_%s' % x for x in feat.columns]
    feat = feat.reset_index()
    df = df.merge(feat, how='left', on='category_lvl_1')
    feat_names.extend(feat.columns[1:].tolist())

    feat= pd.crosstab(index=df['category_lvl_2'], columns=df['sku1'])
    feat.columns = ['cat2_%s' % x for x in feat.columns]
    feat = feat.reset_index()
    df = df.merge(feat, how='left', on='category_lvl_2')
    feat_names.extend(feat.columns[1:].tolist())

    feat= pd.crosstab(index=df['category_lvl_3'], columns=df['sku1'])
    feat.columns = ['cat3_%s' % x for x in feat.columns]
    feat = feat.reset_index()
    df = df.merge(feat, how='left', on='category_lvl_3')
    feat_names.extend(feat.columns[1:].tolist())

    X = df[feat_names].astype(float).values

    X_tr = X[:n_trains,:]
    X_val = X[n_trains:,:]

    print('saving %s' % feat_train_file)
    joblib.dump(X_tr, feat_train_file)

    print('saving %s' % feat_test_file)
    joblib.dump(X_val, feat_test_file)

if __name__ == "__main__":
    # Data loading

    train_file = '../data/data_train.csv'
    test_file = '../data/data_valid.csv'

    feat_train_file = '../features/sku_feat.trn'
    feat_test_file = '../features/sku_feat.tst'
    extract_sku_text_features(train_file, test_file, feat_train_file, feat_test_file)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('gatekeeper', '0038_accountcheckattempt'),
    ]

    operations = [
        migrations.AlterField(
            model_name='user',
            name='last_login',
            field=models.DateTimeField(verbose_name='last login', null=True, blank=True),
        ),
    ]


import sys
import os
import yaml
import json
from pickle import load, dump
from tqdm import tqdm

from skopt import BayesSearchCV
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_score
from xgboost import XGBClassifier

from src.models_training.utils import select_candidate, tqdm_skopt

def prepare_data(candidate, data, features):
    candidate_name = candidate['training_field']
    candidate_feature = candidate['candidate']
    X_train, y_train = data['X_train'], data['y_train']
    X_train, y_train, _ = select_candidate(candidate_name, candidate_feature, features, X_train, y_train)

    return X_train, y_train

if __name__ == "__main__":

    if len(sys.argv) != 5:
        sys.stderr.write("Arguments error. Usage:\n")
        sys.stderr.write("\tpython train.py features-dir-path scheme-path model-dir-path param_name\n")
        sys.exit(1)

    train_input = os.path.join(sys.argv[1], "data.pickle")
    pipe_file = os.path.join(sys.argv[1], "pipe.pickle")
    scheme_file = sys.argv[2]

    os.makedirs(os.path.join(sys.argv[3]), exist_ok=True)

    # load data scheme and features
    with open(train_input, 'rb') as f1:
        data = load(f1)
    with open(scheme_file, 'rb') as f_s:
        scheme = json.load(f_s)
    with open(pipe_file, 'rb') as f1:
        pipe_feature = load(f1)
    features = pipe_feature.get_feature_names()

    params = yaml.safe_load(open("params.yaml"))[sys.argv[4]]

    optimize = params['optimize']
    n_iter = params['n_iter']
    n_estimators = params['n_estimators']

    if not optimize:
        learning_rate = params['learning_rate']
        max_depth = params['max_depth']
        max_leaf_nodes = params['max_leaf_nodes']

    n_scheme = len(scheme.keys())
    n_cpu = os.cpu_count()
    n_cv = 3  # cross validation for optimization
    n_points = max(n_cpu // n_cv, 1)

    for candidate, candidate_name in tqdm(zip(scheme.values(), scheme.keys())):

        X_train, y_train = prepare_data(candidate, data, features)
        # pipe_feature_post = Pipeline([
        #     ('power_transformer', PowerTransformer()),
        # ])
        #
        # X_train = pipe_feature_post.fit_transform(X_train)
        model = {}
        # model['pipe_feature_post'] = pipe_feature_post

        if optimize:
            model_output = os.path.join(sys.argv[3], candidate_name + '-' + "optimized_GB")
            pipe = BayesSearchCV(
                GradientBoostingClassifier(n_estimators=n_estimators, verbose=0),
                {
                    'max_leaf_nodes': (10, 60),
                    'learning_rate': (0.05, 0.2, 'uniform'),
                    'max_depth': (3, 100),  # integer valued parameter
                },
                n_points=n_points,
                n_jobs=n_cv * n_points,
                n_iter=n_iter,
                cv=n_cv,
                verbose=0,
                scoring='f1_macro',
            )
            pipe.fit(X_train, y_train)

            print("val. score: %s" % pipe.best_score_)
            model['model'] = pipe

        else:
            model_output = os.path.join(sys.argv[3], candidate_name + '-' + "GB")
            gbc = XGBClassifier(learning_rate=learning_rate, n_estimators=n_estimators,
                                max_depth=max_depth, verbosity=1, n_jobs=n_cpu,
                                eval_metric=f1_score)
            gbc.fit(X_train, y_train)
            model['model'] = gbc

        with open(model_output, 'wb') as f1:
            dump(model, f1)


import os
import sys
import time

from wisdem import run_wisdem

"""
Example showing how WISDEM values can be changed programmatically in Python.

This uses the `overridden_values` dict given to `run_wisdem`.
Specifically, you can supply a dictionary of values to overwrite after
setup is called.
"""

mydir = os.path.dirname(os.path.realpath(__file__))  # get path to this file
fname_wt_input = os.path.join(os.path.dirname(mydir), "02_reference_turbines", "IEA-15-240-RWT.yaml")
fname_modeling_options = mydir + os.sep + "modeling_options.yaml"
fname_analysis_options = mydir + os.sep + "analysis_options.yaml"

wt_opt, modeling_options, analysis_options = run_wisdem(fname_wt_input, fname_modeling_options, fname_analysis_options)
print(f"Tip deflection: {wt_opt['rotorse.rs.tip_pos.tip_deflection'][0]} meters")

# Construct a dict with values to overwrite
overridden_values = {}
overridden_values["rotorse.wt_class.V_mean_overwrite"] = 11.5

# Run the modified simulation with the overwritten values
wt_opt, modeling_options, opt_options = run_wisdem(
    fname_wt_input,
    fname_modeling_options,
    fname_analysis_options,
    overridden_values=overridden_values,
)
print(f"Tip deflection: {wt_opt['rotorse.rs.tip_pos.tip_deflection'][0]} meters")


import numpy as np
import tensorflow as tf
from bench.core.executer import Executer

def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False, eps=1e-6):
    """Calculate overlap between two set of bboxes.

    If ``is_aligned`` is ``False``, then calculate the ious between each bbox
    of bboxes1 and bboxes2, otherwise the ious between each aligned pair of
    bboxes1 and bboxes2.

    Args:
        bboxes1 (Tensor): shape (m, 4) in <x1, y1, x2, y2> format or empty.
        bboxes2 (Tensor): shape (n, 4) in <x1, y1, x2, y2> format or empty.
            If is_aligned is ``True``, then m and n must be equal.
        mode (str): "iou" (intersection over union) or iof (intersection over
            foreground).

    Returns:
        ious(Tensor): shape (m, n) if is_aligned == False else shape (m, 1)

    Example:
        >>> bboxes1 = torch.FloatTensor([
        >>>     [0, 0, 10, 10],
        >>>     [10, 10, 20, 20],
        >>>     [32, 32, 38, 42],
        >>> ])
        >>> bboxes2 = torch.FloatTensor([
        >>>     [0, 0, 10, 20],
        >>>     [0, 10, 10, 19],
        >>>     [10, 10, 20, 20],
        >>> ])
        >>> bbox_overlaps(bboxes1, bboxes2)
        tensor([[0.5000, 0.0000, 0.0000],
                [0.0000, 0.0000, 1.0000],
                [0.0000, 0.0000, 0.0000]])

    Example:
        >>> empty = torch.FloatTensor([])
        >>> nonempty = torch.FloatTensor([
        >>>     [0, 0, 10, 9],
        >>> ])
        >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1)
        >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0)
        >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0)
    """

    assert mode in ['giou', 'iou', 'iof']
    # Either the boxes are empty or the length of boxes's last dimenstion is 4
    assert (bboxes1.shape[-1] == 4 or bboxes1.shape[0] == 0)
    assert (bboxes2.shape[-1] == 4 or bboxes2.shape[0] == 0)

    # Batch dim must be the same
    # Batch dim: (B1, B2, ... Bn)
    assert bboxes1.shape[:-2] == bboxes2.shape[:-2]
    batch_shape = bboxes1.shape[:-2]

    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    if is_aligned:
        assert rows == cols

    if rows * cols == 0:
        return tf.zeros([rows, 1]) if is_aligned else tf.zeros([rows, cols])

    area1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (
        bboxes1[:, 3] - bboxes1[:, 1])
    area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (
        bboxes2[:, 3] - bboxes2[:, 1])

    if is_aligned:
        lt = tf.math.maximum(bboxes1[:, :2], bboxes2[:, :2])  # [rows, 2]
        rb = tf.math.minimum(bboxes1[:, 2:], bboxes2[:, 2:])  # [rows, 2]

        wh = tf.clip_by_value((rb - lt), clip_value_min=0, clip_value_max=tf.float32.max)  # [rows, 2]
        overlap = wh[:, 0] * wh[:, 1]

        if mode == 'iou':
            union = area1 + area2 - overlap
        else:
            union = area1
        if mode == 'giou':
            enclosed_lt = tf.math.minimum(bboxes1[:, :2], bboxes2[:, :2])
            enclosed_rb = tf.math.maximum(bboxes1[:, 2:], bboxes2[:, 2:])
    else:
        lt = tf.math.maximum(bboxes1[:, None, :2], bboxes2[:, :2])  # [rows, cols, 2]
        rb = tf.math.minimum(bboxes1[:, None, 2:], bboxes2[:, 2:])  # [rows, cols, 2]

        wh = tf.clip_by_value((rb - lt), clip_value_min=0, clip_value_max=tf.float32.max)  # [rows, cols, 2]
        overlap = wh[:, :, 0] * wh[:, :, 1]

        if mode == 'iou' or mode == 'giou':
            union = area1[:, None] + area2 - overlap
        else:
            union = area1[:, None]
        if mode == 'giou':
            enclosed_lt = tf.math.minimum(bboxes1[:, :, None, :2],
                                    bboxes2[:, None, :, :2])
            enclosed_rb = tf.math.maximum(bboxes1[:, :, None, 2:],
                                    bboxes2[:, None, :, 2:])

    eps = tf.constant([eps])
    union = tf.math.maximum(union, eps)
    ious = overlap / union

    if mode in ['iou', 'iof']:
        return ious
    # calculate gious
    enclose_wh = tf.clip_by_value((enclosed_rb - enclosed_lt), clip_value_min=0, clip_value_max=tf.float32.max)
    enclose_area = enclose_wh[:, 0] * enclose_wh[:, 1]
    enclose_area = tf.math.maximum(enclose_area, eps)
    gious = ious - (enclose_area - union) / enclose_area
    return gious

def giou_loss(pred, target, eps=1e-6):
    """IoU loss.

    Computing the IoU loss between a set of predicted bboxes and target bboxes.
    The loss is calculated as negative log of IoU.

    Args:
        pred (torch.Tensor): Predicted bboxes of format (x1, y1, x2, y2),
            shape (n, 4).
        target (torch.Tensor): Corresponding gt bboxes, shape (n, 4).
        eps (float): Eps to avoid log(0).

    Return:
        torch.Tensor: Loss tensor.
    """
    gious = bbox_overlaps(pred, target, mode='giou', is_aligned=True, eps=eps)
    loss = 1 - gious
    return loss

def args_adaptor(np_args):
    pred = tf.convert_to_tensor(np_args[0], tf.float32)
    # pred.requires_grad = True
    target = tf.convert_to_tensor(np_args[1], tf.float32)

    return [pred, target]

def executer_creator():
    return Executer(giou_loss, args_adaptor)


"""
Torch dataset object for synthetically rendered spatial data.
"""

import os
import torch
from scipy.io.wavfile import read
import os
import glob

def get_dataset(dir):
    original_files = glob.glob(os.path.join(dir, "*_original.wav"))
    converted_files = []
    for original_file in original_files:
        converted_file = original_file.replace(
            "_original.wav", "_converted.wav")
        converted_files.append(converted_file)
    return original_files, converted_files

def load_wav(full_path):
    sampling_rate, data = read(full_path)
    return data, sampling_rate

class LLVCDataset(torch.utils.data.Dataset):

    def __init__(
        self,
        dir,
        sr,
        wav_len,
        dset
    ):
        assert dset in [
            "train",
            "val",
            "dev"
        ], "`dset` must be one of ['train', 'val', 'dev']"
        self.dset = dset
        file_dir = os.path.join(dir, dset)
        self.wav_len = wav_len
        self.sr = sr
        self.original_files, self.converted_files = get_dataset(
            file_dir
        )

    def __len__(self):
        return len(self.original_files)

    def __getitem__(self, idx):
        original_wav = self.original_files[idx]
        converted_wav = self.converted_files[idx]

        original_data, o_sr = load_wav(original_wav)
        converted_data, c_sr = load_wav(converted_wav)

        assert o_sr == self.sr, f"Expected {self.sr}Hz, got {o_sr}Hz for file {original_wav}"
        assert c_sr == self.sr, f"Expected {self.sr}Hz, got {c_sr}Hz for file {converted_wav}"

        converted = torch.from_numpy(original_data)
        gt = torch.from_numpy(converted_data)

        converted = converted.unsqueeze(0).to(torch.float) / 32768
        gt = gt.unsqueeze(0).to(torch.float) / 32768

        if gt.shape[-1] < self.wav_len:
            gt = torch.cat(
                (gt, torch.zeros(1, self.wav_len - gt.shape[-1])), dim=1)
        else:
            gt = gt[:, : self.wav_len]

        if converted.shape[-1] < self.wav_len:
            converted = torch.cat(
                (converted, torch.zeros(1, self.wav_len - converted.shape[-1])), dim=1
            )
        else:
            converted = converted[:, : self.wav_len]

        return converted, gt


from ventana import Ventana
from formularios import utils
import pygtk
pygtk.require('2.0')
import gtk, time
from framework import pclases
from collections import defaultdict
from formularios.ventana_progreso import VentanaProgreso

class ConsultaExistenciasPorTipo(Ventana):

    def __init__(self, objeto = None, usuario = None):
        self.usuario = usuario
        Ventana.__init__(self, 'consulta_existencias_por_tipo.glade',
                         objeto, self.usuario)
        connections = {'b_salir/clicked': self.salir,
                       'b_actualizar/clicked': self.buscar,
                       'b_imprimir/clicked': self.imprimir,
                       'b_exportar/clicked': self.exportar,
                       'cb_almacen/changed': self.buscar}
        self.add_connections(connections)
        for nomtv in ("tv_fibra", "tv_gtx", "tv_cemento"):
            cols = [
                ('Producto', 'gobject.TYPE_STRING', False, True, True, None),#0
                ('A (Kg)', 'gobject.TYPE_STRING', False, True, False, None),
                ('A (#)', 'gobject.TYPE_STRING', False, True, False, None),
                ('B (Kg)', 'gobject.TYPE_STRING', False, True, False, None),
                ('B (#)', 'gobject.TYPE_STRING', False, True, False, None),
                ('C (Kg)', 'gobject.TYPE_STRING', False, True, False, None),
                ('C (#)', 'gobject.TYPE_STRING', False, True, False, None),
                ('Total (Kg)', 'gobject.TYPE_STRING', False, True, False, None),
                ('Total (#)', 'gobject.TYPE_STRING', False, True, False, None),
                ('PUID', 'gobject.TYPE_STRING', False, False, False, None)]#(5)
            if nomtv == "tv_gtx":
                cols.insert(2,
                  ('A (m²)', 'gobject.TYPE_STRING', False, True, False, None))
                cols.insert(5,
                  ('B (m²)', 'gobject.TYPE_STRING', False, True, False, None))
                cols.insert(8,
                  ('C (m²)', 'gobject.TYPE_STRING', False, True, False, None))
                cols.insert(11,
                  ('Total (m²)',
                      'gobject.TYPE_STRING', False, True, False, None))
            tv = self.wids[nomtv]
            utils.preparar_listview(tv, cols)
            tv.connect("row-activated", self.abrir_objeto)
            for ncol in range(1, len(cols) - 1):
                col = tv.get_column(ncol)
                col.get_cell_renderers()[0].set_property('xalign', 1.0)
            col = tv.get_column(0)
            col.set_expand(True)
        almacenes = pclases.Almacen.select(
                pclases.Almacen.q.activo == True,
                orderBy = "id")
        opciones = [(0, "Todos")] + [(a.id, a.nombre) for a in almacenes]
        utils.rellenar_lista(self.wids['cb_almacen'], opciones)
        utils.combo_set_from_db(self.wids['cb_almacen'], 0)
        gtk.main()

    def abrir_objeto(self, tv, path, view_column):
        """
        Abre el producto.
        """
        model = tv.get_model()
        puid = model[path][-1]
        objeto = pclases.getObjetoPUID(puid)
        if objeto.es_rollo() or objeto.es_rollo_c():
            from formularios.productos_de_venta_rollos \
                    import ProductosDeVentaRollos as NuevaVentana
        elif objeto.es_fibra() or objeto.es_bolsa():
            from formularios.productos_de_venta_balas \
                    import ProductosDeVentaBalas as NuevaVentana
        else:
            return  # Si no es nada de lo que pueda abrir, pasando del temita.
        v = NuevaVentana(usuario = self.usuario, objeto = objeto)

    def chequear_cambios(self):
        pass

    def buscar(self, boton):
        """
        Busca todos los productos de venta, los clasifica por tipo y
        genera una estructura de datos que se mostrará después en los TreeView.
        """
        self.productos = defaultdict(lambda: [])
        # productos como propiedad de la clase por si tengo que depurar.
        for pv in pclases.ProductoVenta.select(orderBy = "descripcion"):
            if pv.es_rollo() or pv.es_rollo_c():
                tipo = "gtx"
            elif pv.es_bolsa(): # or pv.es_caja():
                tipo = "cemento"
            elif pv.es_bala() or pv.es_bala_cable() or pv.es_bigbag():
                # Equivale a pv.es_fibra(). O debería.
                tipo = "fibra"
            elif pv.es_especial():
                continue    # Estos los ignoro
            else:
                # ¿Producto vacío, sin descripción ni nada? Casi seguro.
                #raise ValueError, "consulta_existencias_por_tipo::buscar -> "\
                #                  "Producto no es fibra, geotextil ni cemento."
                # TODO: Agregar línea al log o emitir un warning o algo.
                continue
            self.productos[tipo].append(pv)
        totales = self.rellenar_treeviews()
        self.rellenar_totales(totales)

    def rellenar_treeviews(self):
        almacenid = utils.combo_get_value(self.wids['cb_almacen'])
        if almacenid != 0:
            almacen = pclases.Almacen.get(almacenid)
        else:
            almacen = None
        vpro = VentanaProgreso(padre = self.wids['ventana'])
        vpro.mostrar()
        totales = {'kgA':     {},
                   'kgB':     {},
                   'kgC':     {},
                   'kgTotal': {},
                   'mA':      {},
                   'mB':      {},
                   'mC':      {},
                   'mTotal':  {},
                   '#A':      {},
                   '#B':      {},
                   '#C':      {},
                   '#Total':  {}
                  }
        i = 0.0
        tot = sum([len(self.productos[k]) for k in self.productos.keys()])
        for nombre_tv in ("tv_fibra", "tv_gtx", "tv_cemento"):
            tv = self.wids[nombre_tv]
            model = tv.get_model()
            model.clear()
            tipo = nombre_tv.split("_")[-1]
            for pv in self.productos[tipo]:
                i += 1
                vpro.set_valor(i / tot,
                               "Contando existencias de %s (%d) [%s]..." % (
                                    pv.nombre, pv.id, tipo))
                kgA = pv.get_stock_A(almacen = almacen)
                kgB = pv.get_stock_B(almacen = almacen)
                kgC = pv.get_stock_C(almacen = almacen)
                bultosA = pv.get_existencias_A(almacen = almacen)
                bultosB = pv.get_existencias_B(almacen = almacen)
                bultosC = pv.get_existencias_C(almacen = almacen)
                total_kg = pv.get_stock(almacen = almacen,
                                     contar_defectuosos = True)
                total_bultos = pv.get_existencias(almacen = almacen,
                                     contar_defectuosos = True)
                if pclases.DEBUG:
                    assert round(kgA + kgB + kgC) == round(
                            total_kg), pv.puid  # XXX
                    # Esto de arriba, aunque mezcle metros con kilos, no
                    # debe fallar. Ya que en el total de un producto C,
                    # A y B se quedan a 0.0. Y al contrario. Si el producto
                    # no es C, A y B valdrán algo y C siempre es 0.
                    assert round(bultosA + bultosB + bultosC) == round(
                            total_bultos), pv.puid  # XXX
                if nombre_tv != "tv_gtx":
                    fila = [pv.descripcion,
                            utils.float2str(kgA),
                            utils.int2str(bultosA),
                            utils.float2str(kgB),
                            utils.int2str(bultosB),
                            utils.float2str(kgC),
                            utils.int2str(bultosC),
                            utils.float2str(total_kg),
                            utils.int2str(total_bultos),
                            pv.puid]
                    actualizar_totales(totales, tipo,
                            kgA, kgB, kgC, total_kg,
                            bultosA, bultosB, bultosC, total_bultos)
                else:
                    # Los metros ya los tenía calculados. Es lo que devuelve
                    # el get_stock. Solo que arriba lo he llamado kg*
                    metrosA = kgA
                    metrosB = kgB
                    metrosC = "N/A" # No aplicable. Solo se mide en kilos.
                    kilosA = pv.get_stock_kg_A(almacen = almacen)
                    kilosB = pv.get_stock_kg_B(almacen = almacen)
                    kilosC = kgC    # Ya estaba calculado
                    total_kilos = kilosA + kilosB + kilosC
                    total_metros = metrosA + metrosB
                    # total bultos ya se calcula arriba.
                    fila = [pv.descripcion,
                            utils.float2str(kilosA),
                            utils.float2str(metrosA),
                            utils.int2str(bultosA),
                            utils.float2str(kilosB),
                            utils.float2str(metrosB),
                            utils.int2str(bultosB),
                            utils.float2str(kilosC),
                            metrosC,  # No es aplicable. Solo kg. No m² en C.
                            utils.int2str(bultosC),
                            utils.float2str(total_kilos),
                            utils.float2str(total_metros),
                            utils.int2str(total_bultos),
                            pv.puid]
                    actualizar_totales(totales, tipo,
                            kilosA, kilosB, kilosC, total_kilos,
                            bultosA, bultosB, bultosC, total_bultos,
                            metrosA, metrosB, 0.0, total_metros)
                        # m² de C es "N/A". Le paso un 0 aquí para que no pete.
                model.append(fila)
        vpro.ocultar()
        return totales

    def rellenar_totales(self, totales):
        for tipo_stock in totales:
            for tipo_producto in totales[tipo_stock]:
                nomentry = "e_%s_%s" % (tipo_producto, tipo_stock)
                entry = self.wids[nomentry]
                total = totales[tipo_stock][tipo_producto]
                entry.set_text(utils.float2str(total, autodec = True))

    def exportar(self, boton):
        """
        Exporta el contenido del TreeView a un fichero csv.
        """
        from informes.treeview2csv import treeview2csv
        from formularios.reports import abrir_csv
        pagina_activa = self.wids['nb_tipo'].get_current_page()
        if pagina_activa == 0:
            tv = self.wids['tv_fibra']
        elif pagina_activa == 1:
            tv = self.wids['tv_gtx']
        elif pagina_activa == 2:
            tv = self.wids['tv_cemento']
        else:
            return
        abrir_csv(treeview2csv(tv))

    def imprimir(self, boton):
        """
        Prepara la vista preliminar para la impresión del informe
        """
        from formularios import reports
        from informes.treeview2pdf import treeview2pdf
        pagina_activa = self.wids['nb_tipo'].get_current_page()
        almacenid = utils.combo_get_value(self.wids['cb_almacen'])
        if almacenid != 0:
            almacen = pclases.Almacen.get(almacenid)
        else:
            almacen = None
        if pagina_activa == 0:
            tv = self.wids['tv_fibra']
            titulo = "Existencias de productos por tipo: fibra"
        elif pagina_activa == 1:
            tv = self.wids['tv_gtx']
            titulo = "Existencias de productos por tipo: geotextiles"
        elif pagina_activa == 2:
            tv = self.wids['tv_cemento']
            titulo = "Existencias de productos por tipo: fibra de cemento"
        else:
            return
        try:
            titulo += " (%s)" % almacen.nombre
        except AttributeError:
            pass
        totales = range(1, tv.get_model().get_n_columns()-1)
        extra_data = []
        reports.abrir_pdf(treeview2pdf(tv,
                                       titulo = titulo,
                                       apaisado = False,
                                       pijama = True,
                                       numcols_a_totalizar = totales,
                                       extra_data = extra_data))

def actualizar_totales(totales, tipo_de_producto, kgA, kgB, kgC, kg_total,
                       bultosA, bultosB, bultosC, bultos_total,
                       metrosA = None, metrosB = None, metrosC = None,
                       metros_total = None):
    """
    Actualiza el diccionario de totales.
    tipo_de_producto es 'gtx', 'fibra' o 'cemento'.
    totales es un diccionario organizado por tipo de existencias (kgA, kgB,
    kgC, kgTotal, etc.) y dentro de éstas, por tipo de producto (fibra,
    gtx o cemento).
    kgA, kgB, kgC y total son las existencias en sí con las que se actualizará
    el diccionario en función del tipo de existencias y del tipo de producto.
    """
    for tipo_de_stock, stock_de_ese_tipo in (('kgA', kgA),
                                             ('kgB', kgB),
                                             ('kgC', kgC),
                                             ('kgTotal', kg_total),
                                             ('mA', metrosA),
                                             ('mB', metrosB),
                                             ('mC', metrosC),
                                             ('mTotal', metros_total),
                                             ('#A', bultosA),
                                             ('#B', bultosB),
                                             ('#C', bultosC),
                                             ('#Total', bultos_total)):
        if stock_de_ese_tipo is None:
            continue
        try:
            totales[tipo_de_stock][tipo_de_producto] += stock_de_ese_tipo
        except KeyError:
            totales[tipo_de_stock][tipo_de_producto] = stock_de_ese_tipo

if __name__ == '__main__':
    t = ConsultaExistenciasPorTipo()


class Person:
    def __init__(self, name, phonenumber):
        self.name = name
        self.phonenumber = phonenumber

phonebookList = []

def size():
    n = len(phonebookList)
    return "This phonebook contains " + (str(n) + " contacts." if n != 1 else str(n) + " contact.")

def addContact(newname, newnumber):
    for i in range(0, len(phonebookList), 1):
        if phonebookList[i].phonenumber == newnumber:
            return "\nError. number already exists in Phonebook"
    phonebookList.append(Person(newname, newnumber))

def findContact(nametoFind, numbertoFind):
    for i in range(0, len(phonebookList), 1):
        if phonebookList[i].phonenumber == numbertoFind and phonebookList[i].name == nametoFind:
            print("\nPerson found! ---> Name:", str(phonebookList[i].name), end = ", ")
            print("Number:" + str(phonebookList[i].phonenumber))
            return True
        elif phonebookList[i].phonenumber == numbertoFind:
            print("\nPhone number found, but Phoneholder does not match.")
            return False
        elif phonebookList[i].name == nametoFind:
            print("\nName found, but phone number attached does not match.")
            return False
        else:
            print("\nError. Person not found.")
            return False

def findName(numberusedtoFind):
    for i in range(0, len(phonebookList), 1):
        if phonebookList[i].phonenumber == numberusedtoFind:
            print("Name attached to", phonebookList[i].phonenumber, end = ": ")
            print(phonebookList[i].name)
            return

    print("No name found for that number.")

def findNumber(nameusedtoFind):
    for i in range(0, len(phonebookList), 1):
        if phonebookList[i].name == nameusedtoFind:
            print("Number attached to", phonebookList[i].name, end = ": ")
            print(phonebookList[i].phonenumber)
            return

    print("No number found for that name.")

def removeContact(nametoRemove, numbertoRemove):
    if findContact(nametoRemove, numbertoRemove):
        for i in range(0, len(phonebookList)):
            if phonebookList[i].name == nametoRemove:
                phonebookList.pop(i)
        return "\nRemoved an item."
    else:
        for i in range(0, len(phonebookList)):
            if (phonebookList[i].name == nametoRemove and phonebookList[i].phonenumber != numbertoRemove) or (phonebookList[i].name != nametoRemove and phonebookList[i].phonenumber == numbertoRemove):
                print("\nWARNING: Potential Name and Number found that matches 1 of the descriptions: ", phonebookList[i].name, phonebookList[i].phonenumber)
                delete = str(input("Do you wish to delete this contact? (Y/N)"))
                while delete != "N" and delete != "n" and delete != "Y" and delete != "y":
                    delete = str(input("Error: Invalid Key. Do you wish to delete this contact? (Y/N)"))
                if delete == "N" or delete == "n":
                    print("No item deleted.")
                    continue
                else:
                    phonebookList.pop(i)
                    print("item deleted.")
                    continue

def printphoneBook():
    print("\n***************PHONEBOOK LIST****************")
    for i in range(0, len(phonebookList), 1):
        print(i+1, end = ". ")
        print("Name:", phonebookList[i].name, end = ", ")
        print("Number:", phonebookList[i].phonenumber)

addContact("Kevin", 3498172)
findContact("Kevin", 3498172)
addContact("Paul Rosen", 40533617)
printphoneBook()
addContact("Kevin", 8139975307)
print(removeContact("Kevin", 3498162))
printphoneBook()
print(size())
findNumber("Kevin")
findName(3498162)
findName(40533617)
printphoneBook()


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the 'deleteProducts' function below.

# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY ids
#  2. INTEGER m

from numpy import array

def deleteProducts(ids,m):

# Creating an empty dictionary

    freq = {} #to count frequency of ids
    for item in ids:
        if (item in freq):
            freq[item] += 1
        else:
            freq[item] = 1

    sortedfreq = sorted(freq.items(), key=lambda x: x[1], reverse=False)
    uniqueids=len(sortedfreq)
    delete=0

    for i in sortedfreq:
        if i[1]<=m:
            delete=delete+1
            m=m-i[1]
        else:
            break
    return uniqueids-delete

# Driver function
if __name__ == "__main__":
	my_list =array([1, 1, 1, 2, 3, 2])
	m=2
	print(deleteProducts(my_list,m))