# coding: utf-8

"""
    BioLink API

    API integration layer for linked biological objects.   __Source:__ https://github.com/biolink/biolink-api/  # noqa: E501

    OpenAPI spec version: 1.1.14

    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class AbstractPropertyValue(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """
    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'val': 'str',
        'pred': 'str',
        'xrefs': 'list[str]'
    }

    attribute_map = {
        'val': 'val',
        'pred': 'pred',
        'xrefs': 'xrefs'
    }

    def __init__(self, val=None, pred=None, xrefs=None):  # noqa: E501
        """AbstractPropertyValue - a model defined in Swagger"""  # noqa: E501
        self._val = None
        self._pred = None
        self._xrefs = None
        self.discriminator = None
        if val is not None:
            self.val = val
        if pred is not None:
            self.pred = pred
        if xrefs is not None:
            self.xrefs = xrefs

    @property
    def val(self):
        """Gets the val of this AbstractPropertyValue.  # noqa: E501

        value part  # noqa: E501

        :return: The val of this AbstractPropertyValue.  # noqa: E501
        :rtype: str
        """
        return self._val

    @val.setter
    def val(self, val):
        """Sets the val of this AbstractPropertyValue.

        value part  # noqa: E501

        :param val: The val of this AbstractPropertyValue.  # noqa: E501
        :type: str
        """

        self._val = val

    @property
    def pred(self):
        """Gets the pred of this AbstractPropertyValue.  # noqa: E501

        predicate (attribute) part  # noqa: E501

        :return: The pred of this AbstractPropertyValue.  # noqa: E501
        :rtype: str
        """
        return self._pred

    @pred.setter
    def pred(self, pred):
        """Sets the pred of this AbstractPropertyValue.

        predicate (attribute) part  # noqa: E501

        :param pred: The pred of this AbstractPropertyValue.  # noqa: E501
        :type: str
        """

        self._pred = pred

    @property
    def xrefs(self):
        """Gets the xrefs of this AbstractPropertyValue.  # noqa: E501

        Xrefs provenance for property-value  # noqa: E501

        :return: The xrefs of this AbstractPropertyValue.  # noqa: E501
        :rtype: list[str]
        """
        return self._xrefs

    @xrefs.setter
    def xrefs(self, xrefs):
        """Sets the xrefs of this AbstractPropertyValue.

        Xrefs provenance for property-value  # noqa: E501

        :param xrefs: The xrefs of this AbstractPropertyValue.  # noqa: E501
        :type: list[str]
        """

        self._xrefs = xrefs

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value
        if issubclass(AbstractPropertyValue, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, AbstractPropertyValue):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


import json
import re
from json import JSONEncoder

from SPARQLWrapper import SPARQLWrapper, JSON
from executor.query import general_query_dbpedia, general_query_fuseki, get_thumbnails

dbpedia_enpoint = 'http://dbpedia.org/sparql'
fuseki_endpoint = 'http://localhost:3030/Data/query'

class CountryInfo(object):
    json_number = None

    def __init__(self, **kwargs):
        input = self.search(kwargs['query'])
        for field in ('query', 'res'):
            setattr(self, field, input[field])

    def search(self, query):
        try:
            res = {'query': query, 'res': self.query(query)}
        except Exception:
            res = {'query': query, 'res': []}
        return res

    @staticmethod
    def remote(endpoint, query, keyword):
        sparql = SPARQLWrapper(endpoint)
        sparql_query = query(keyword)
        sparql.setQuery(sparql_query)
        sparql.setReturnFormat(JSON)
        results = sparql.query().convert()
        res = []
        xsd = 'http://www.w3.org/2001/XMLSchema#'
        for result in results['results']['bindings']:
            res_i = {}
            for key in result.keys():
                if not result[key]['value'] == '':
                    try:
                        if result[key]['datatype'] == xsd + 'float':
                            res_i[key] = float(result[key]['value'])
                        elif result[key]['datatype'] == xsd + 'integer':
                            res_i[key] = int(result[key]['value'])
                    except Exception:
                        if re.search(r'\|', result[key]['value']):
                            res_i[key] = re.split(r'\|', result[key]['value'])
                        elif key == 'type':
                            res_i[key] = re.search(r'/([^/]+)$', result[key]['value']).group(1)
                        else:
                            res_i[key] = result[key]['value']
            if res_i.keys().__contains__('area'):
                res_i['area']=str(res_i['area'])+' sq. mi.'
            if res_i.keys().__contains__('density'):
                res_i['density']=str(res_i['density'])+' per sq. mi.'
            if res_i.keys().__contains__('coastline'):
                res_i['coastline']=str(res_i['coastline'])+' coast/area ratio'
            if res_i.keys().__contains__('infantMortality'):
                res_i['infantMortality']=str(res_i['infantMortality'])+' per 1000 births'
            if res_i.keys().__contains__('gdp'):
                res_i['gdp']=str(res_i['gdp'])+' $ per capita'
            if res_i.keys().__contains__('phoneSubs'):
                res_i['phoneSubs']=str(res_i['phoneSubs'])+' per 1000'
            res.append(res_i)
        return res
        pass

    def query(self, query):
        # print(general_query_dbpedia(query.lower()))
        res_dbpedia = self.remote(dbpedia_enpoint, general_query_dbpedia, query.lower())
        uris = '((' + query.lower() + ')'
        try:
            for object in res_dbpedia:
                uris = uris + '|(' +re.sub('\'', '\\\\\'',(re.sub('\\)', '\\\\\\\\)', re.sub('\\(', '\\\\\\\\(', object['object'])) + ')'))
        except Exception:
            uris = uris + '|' + str(res_dbpedia['object'])
        uris = uris + ')'
        try:
            res_fuseki = self.remote(fuseki_endpoint, general_query_fuseki, uris)
            res = self.join_result(res_dbpedia, res_fuseki)
        except Exception:
            res = res_dbpedia
        return res

    def join_result(self, dict1, dict2):
        res = []
        res_len = 0
        # print(json.dumps(dict1, indent=4))
        # print(json.dumps(dict2, indent=4))
        try:
            if dict1 and dict2:
                for object1 in dict1:
                    for object2 in dict2:
                        if object1['object'] == object2['sameAs']:
                            dict_merge = lambda a, b: a.update(b) or a
                            new_object = dict_merge(object1, object2)
                            res.append(new_object)
            elif dict2:
                res = dict2
        except Exception:
            if dict1['object'] == dict2['sameAs']:
                dict_merge = lambda a, b: a.update(b) or a
                # res = dict_merge(dict1, dict2)
                res.append(dict_merge(dict1, dict2))
        if not res:
            res = dict2
        try:
            for obj in res:
                if obj['type'] == 'Country':
                    obj['relatedCountry'] = self.get_thumbnail_dbpedia(obj['relatedCountry'], obj['relatedCountryName'])
                    del obj['relatedCountryName']
                else:
                    obj['member'] = self.get_thumbnail_dbpedia(obj['member'], obj['memberName'])
                    del obj['memberName']
        except Exception:
            pass
        return res

    def get_thumbnail_dbpedia(self, uris, name):
        pattern = ''
        for uri in uris:
            pattern = pattern + '|' + re.search(r'/([^/]+$)', uri).group(1)
        data = self.remote(dbpedia_enpoint, get_thumbnails, pattern[1:])
        res = {}
        for obj in data:
            res[name[uris.index(obj['object'])]] = obj['thumbnail']
        return res

class MyEncoder(JSONEncoder):
    def default(self, o):
        return o.__dict__


"""
    This is a script that trains 2D U-Nets in the XY, YZ and XZ direction from scratch
    Usage:
        python train.py --method 2D
"""

"""
    Necessary libraries
"""
import argparse
import datetime
import torch
import torch.optim as optim
import os
from torch.utils.data import DataLoader

from data.datasets import *
from networks.unet import UNet2D
from util.preprocessing import get_augmenters_2d
from util.io import imwrite3D
from util.losses import CrossEntropyLoss
from util.validation import segment
from util.metrics import jaccard, dice, accuracy_metrics
from util.tools import get_sampling_ordering

"""
    Parse all the arguments
"""
print('[%s] Parsing arguments' % (datetime.datetime.now()))
parser = argparse.ArgumentParser()
# logging parameters
parser.add_argument("--log_dir", help="Logging directory", type=str, default="logs")
parser.add_argument("--write_dir", help="Writing directory", type=str, default=None)
parser.add_argument("--data", help="Dataset for training", type=str, default="epfl") # options: 'epfl', 'embl_mito', 'embl_er', vnc, med
parser.add_argument("--print_stats", help="Number of iterations between each time to log training losses", type=int, default=100)

# network parameters
parser.add_argument("--resolution", help="Pixel resolution of the dataset in (z,y,x) ordering", type=str, default="5,5,5")
parser.add_argument("--input_size_xy", help="Size of the XY blocks that propagate through the network", type=str, default="1,512,512")
parser.add_argument("--input_size_zx", help="Size of the XZ blocks that propagate through the network", type=str, default="1,160,160")
parser.add_argument("--input_size_yz", help="Size of the YZ blocks that propagate through the network", type=str, default="1,160,160")
parser.add_argument("--fm", help="Number of initial feature maps in the segmentation U-Net", type=int, default=64)
parser.add_argument("--levels", help="Number of levels in the segmentation U-Net (i.e. number of pooling stages)", type=int, default=4)
parser.add_argument("--group_norm", help="Use group normalization instead of batch normalization", type=int, default=0)
parser.add_argument("--augment_noise", help="Use noise augmentation", type=int, default=1)
parser.add_argument("--class_weight", help="Percentage of the reference class", type=float, default=(0.5))

# optimization parameters
parser.add_argument("--lr", help="Learning rate of the optimization", type=float, default=1e-3)
parser.add_argument("--step_size", help="Number of epochs after which the learning rate should decay", type=int, default=10)
parser.add_argument("--gamma", help="Learning rate decay factor", type=float, default=0.9)
parser.add_argument("--epochs", help="Total number of epochs to train", type=int, default=100)
parser.add_argument("--test_freq", help="Number of epochs between each test stage", type=int, default=1)
parser.add_argument("--train_batch_size", help="Batch size in the training stage", type=int, default=1)
parser.add_argument("--test_batch_size", help="Batch size in the testing stage", type=int, default=1)

args = parser.parse_args()
args.resolution = [int(item) for item in args.resolution.split(',')]
args.input_size_xy = [int(item) for item in args.input_size_xy.split(',')]
args.input_size_zx = [int(item) for item in args.input_size_zx.split(',')]
args.input_size_yz = [int(item) for item in args.input_size_yz.split(',')]
sampling_ordering, input_sizes = get_sampling_ordering(args.resolution, (args.input_size_xy, args.input_size_zx, args.input_size_yz))
num_networks = len(sampling_ordering)
weight = torch.FloatTensor([1-args.class_weight, args.class_weight]).cuda()
loss_fn_seg = CrossEntropyLoss(weight=weight)

"""
    Setup logging directory
"""
if not os.path.exists(args.log_dir):
    os.mkdir(args.log_dir)
net_dirs = ['unets']
for dir in net_dirs:
    if not os.path.exists(os.path.join(args.log_dir, dir)):
        os.mkdir(os.path.join(args.log_dir, dir))
    for subdir in sampling_ordering:
        if not os.path.exists(os.path.join(args.log_dir, dir, str(subdir))):
            os.mkdir(os.path.join(args.log_dir, dir, str(subdir)))
if args.write_dir is not None:
    if not os.path.exists(args.write_dir):
        os.mkdir(args.write_dir)
    for subdir in sampling_ordering:
        if not os.path.exists(os.path.join(args.write_dir, str(subdir))):
            os.mkdir(os.path.join(args.write_dir, str(subdir)))
        os.mkdir(os.path.join(args.write_dir, str(subdir), 'segmentation_last_checkpoint'))
        os.mkdir(os.path.join(args.write_dir, str(subdir), 'segmentation_best_checkpoint'))

"""
    Load the data
"""
input_shapes = input_sizes[0]
# load data
print('[%s] Loading data' % (datetime.datetime.now()))
train_xtransform, train_ytransform, test_xtransform, test_ytransform = get_augmenters_2d(augment_noise=(args.augment_noise==1))
if args.data == 'epfl':
    train = EPFLTrainDataset(input_shapes=input_shapes, transform=train_xtransform, target_transform=train_ytransform)
    test = EPFLTestDataset(input_shapes=input_shapes, transform=test_xtransform, target_transform=test_ytransform)
elif args.data == 'vnc':
    train = VNCTrainDataset(input_shapes=input_shapes, transform=train_xtransform, target_transform=train_ytransform)
    test = VNCTestDataset(input_shapes=input_shapes, transform=test_xtransform, target_transform=test_ytransform)
elif args.data == 'med':
    train = MEDTrainDataset(input_shapes=input_shapes, transform=train_xtransform, target_transform=train_ytransform)
    test = MEDTestDataset(input_shapes=input_shapes, transform=test_xtransform, target_transform=test_ytransform)
else:
    if args.data == 'embl_mito':
        train = EMBLMitoTrainDataset(input_shapes=input_shapes, transform=train_xtransform, target_transform=train_ytransform)
        test = EMBLMitoTestDataset(input_shapes=input_shapes, transform=test_xtransform, target_transform=test_ytransform)
    else:
        train = EMBLERTrainDataset(input_shapes=input_shapes, transform=train_xtransform, target_transform=train_ytransform)
        test = EMBLERTestDataset(input_shapes=input_shapes, transform=test_xtransform, target_transform=test_ytransform)
train_loader = DataLoader(train, batch_size=args.train_batch_size)
test_loader = DataLoader(test, batch_size=args.test_batch_size)

"""
    Train the network for each required orientation
"""
print('[%s] Training 2D U-Nets from scratch...' % (datetime.datetime.now()))
for i, so in enumerate(sampling_ordering):
    # logging stuff
    log_dir = os.path.join(args.log_dir, net_dirs[0], str(so))
    if args.write_dir is not None:
        write_dir = os.path.join(args.write_dir, str(so))

    input_shapes = input_sizes[i]
    train_loader.dataset.input_shapes = input_shapes
    test_loader.dataset.input_shapes = input_shapes
    train_loader.dataset.orientations = so
    test_loader.dataset.orientations = so

    """
        Setup optimization for training
    """
    print('[%s] Setting up optimization for training' % (datetime.datetime.now()))
    net = UNet2D(feature_maps=args.fm, levels=args.levels, group_norm=(args.group_norm==1))
    optimizer = optim.Adam(net.parameters(), lr=args.lr)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)

    print('[%s] Initiating training of network %d...' % (datetime.datetime.now(), i+1))
    # actual training
    net.train_net(train_loader=train_loader, test_loader=test_loader,
                  loss_fn=loss_fn_seg, optimizer=optimizer, scheduler=scheduler,
                  epochs=args.epochs, test_freq=args.test_freq, print_stats=args.print_stats,
                  log_dir=log_dir)

    """
        Validate the trained network
    """
    for j, o in enumerate(so):
        input_size = input_shapes[j]
        print('[%s] Validating trained network %d in orientation %s...' % (datetime.datetime.now(), i+1, str(o)))
        test_data = np.transpose(test.data, o)
        test_labels = np.transpose(test.labels, o)
        segmentation_last_checkpoint = segment(test_data, net, input_size, batch_size=args.test_batch_size)
        j = jaccard(segmentation_last_checkpoint, test_labels)
        d = dice(segmentation_last_checkpoint, test_labels)
        a, p, r, f = accuracy_metrics(segmentation_last_checkpoint, test_labels)
        print('[%s] Results last checkpoint:' % (datetime.datetime.now()))
        print('[%s]     Jaccard: %f' % (datetime.datetime.now(), j))
        print('[%s]     Dice: %f' % (datetime.datetime.now(), d))
        print('[%s]     Accuracy: %f' % (datetime.datetime.now(), a))
        print('[%s]     Precision: %f' % (datetime.datetime.now(), p))
        print('[%s]     Recall: %f' % (datetime.datetime.now(), r))
        print('[%s]     F-score: %f' % (datetime.datetime.now(), f))
        net = torch.load(os.path.join(log_dir, 'best_checkpoint.pytorch'))
        segmentation_best_checkpoint = segment(test_data, net, input_size, batch_size=args.test_batch_size)
        j = jaccard(segmentation_best_checkpoint, test_labels)
        d = dice(segmentation_best_checkpoint, test_labels)
        a, p, r, f = accuracy_metrics(segmentation_best_checkpoint, test_labels)
        print('[%s] Results best checkpoint:' % (datetime.datetime.now()))
        print('[%s]     Jaccard: %f' % (datetime.datetime.now(), j))
        print('[%s]     Dice: %f' % (datetime.datetime.now(), d))
        print('[%s]     Accuracy: %f' % (datetime.datetime.now(), a))
        print('[%s]     Precision: %f' % (datetime.datetime.now(), p))
        print('[%s]     Recall: %f' % (datetime.datetime.now(), r))
        print('[%s]     F-score: %f' % (datetime.datetime.now(), f))

        """
            Write out the results
        """
        if args.write_dir is not None:
            print('[%s] Writing the output' % (datetime.datetime.now()))
            imwrite3D(segmentation_last_checkpoint, os.path.join(write_dir, 'segmentation_last_checkpoint', str(o)), rescale=True)
            imwrite3D(segmentation_best_checkpoint, os.path.join(write_dir, 'segmentation_best_checkpoint', str(o)), rescale=True)

print('[%s] Finished!' % (datetime.datetime.now()))


from django.conf.urls import url, include
from rest_framework import routers, serializers, viewsets
from .models import Tricycle
from rest_framework.authentication import SessionAuthentication
from . import views

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r"tricycles", views.TricycleViewSet)
router.register(r"garages", views.GarageViewSet)
router.register(r"payments", views.PaymentViewSet)
router.register(r"claims", views.ClaimsViewSet)
router.register(r"partclaims", views.PartClaimViewSet)
router.register(r"parts", views.PartViewSet)
router.register(r"funds", views.FundViewSet)

urlpatterns = [
    url(r"^", include(router.urls)),
    url(r"^api-auth/", include("rest_framework.urls", namespace="rest_framework")),
    url(r"^rest-auth/", include("rest_auth.urls")),
    url(r"^fund-data/", views.get_fund_data),
    url(r"^claims-data/", views.get_claims_data),
]


from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView
from rest_framework import generics

from news.forms import AddingNewsForm, NewsCommentForm, LikeForm
from news.models import News
from news.serializer import NewsSerializer
from news.services.services import get_unique_slug
from service.services import get_notifications_count

def home(request):
    context = dict()
    context.update(get_notifications_count(request.user))
    context.update({"title": 'Home page', 'nav_active': 'home', 'block_content': 'full_screen', })
    return render(request, 'news/home.html', context=context)

class NewsPage(ListView):
    model = News
    queryset = News.objects.all().prefetch_related('users_who_liked')
    template_name = 'news/news.html'
    context_object_name = 'news'
    extra_context = {"title": 'News page', 'nav_active': 'news'}
    paginate_by = 5

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(get_notifications_count(self.request.user))
        return context

def article(request, article_slug):
    article_data = get_object_or_404(News.objects.select_related('author'), slug=article_slug)
    comments = article_data.newscomments_set.all().select_related('author')

    user = request.user
    form = None
    like_form = None
    context = dict()

    context.update(get_notifications_count(request.user))

    if user.is_authenticated:
        if request.method == "POST":
            form = NewsCommentForm(request.POST)
            like_form = LikeForm(article_data, user, request.POST)
            if form.is_valid():
                comment = form.save(commit=False)
                comment.author = user
                comment.news = article_data
                comment.save()
                return redirect(to='article', article_slug=article_slug)
            if like_form.is_valid():
                like = like_form.cleaned_data['like']
                if like:
                    article_data.users_who_liked.add(user)
                    article_data.save()
                else:
                    article_data.users_who_liked.remove(user)
                    article_data.save()
        else:
            form = NewsCommentForm()
            like_form = LikeForm(article_data, user)

    context.update({
        'title': f'{article_data.title}',
        'nav_active': 'news',
        'article_data': article_data,
        'form': form,
        'like_form': like_form,
        'comments': comments,
        'block_content': 'full_screen',
    })
    return render(request, 'news/article.html', context=context)

@login_required
def add_news(request):
    user = request.user
    context = dict()

    context.update(get_notifications_count(request.user))

    if not user.is_staff:
        raise PermissionDenied

    if request.method == "POST":
        form = AddingNewsForm(user, request.POST)
        if form.is_valid():
            new_news = form.save(commit=False)
            new_news.slug = get_unique_slug(new_news, new_news.title)
            new_news.save()
            return redirect('news')
    else:
        form = AddingNewsForm(user)

    context.update({
        'title': 'Create news',
        'nav_active': 'news',
        'form': form,
    })

    return render(request, 'news/add_news.html', context=context)

class NewsInProfileView(generics.ListAPIView):
    serializer_class = NewsSerializer

    def get_queryset(self):
        current_user = self.request.user
        if current_user.is_authenticated:
            return News.objects.filter(author=current_user)
        return None


from mathutils.geometry import box_pack_2d

def get_nester(method):
    return rect_pack_custom if method == 'CCOR' else rect_pack_bpy

class Page:
    """Container for several Islands"""
    __slots__ = ('islands', 'name', 'image_path')

    def __init__(self, num=1, islands=None):
        self.islands = islands or list()
        self.name = "page{}".format(num)  # note: this is only used in svg files naming
        self.image_path = None

def can_pack_bpy(islands, cage_size):
    if len(islands) <= 1:
        return True
    aspect = cage_size.y / cage_size.x
    boxes = [[0, 0, isle.bounding_box.x, isle.bounding_box.y / aspect] for isle in islands]
    width, height = box_pack_2d(boxes)
    if width < cage_size.x > height:
        for (x, y, *_), isle in zip(boxes, islands):
            isle.pos.xy = x, y * aspect
        return True
    return False

def branch_and_bound(can_pack_fn):
    # exhaustive search for minimal number of pages
    def result(islands, cage_size):
        best = [i for i, _ in enumerate(islands)]
        path = [0]
        while True:
            if path[-1] > max(path[:-1], default=0) + 1 or max(path) >= max(best):
                # invalid step
                path.pop()
                if len(path) <= 1:
                    break
                path[-1] += 1
                continue
            page = [isle for i, isle in zip(path, islands) if i == path[-1]]
            if can_pack_fn(page, cage_size):
                # good step
                if len(path) == len(islands):
                    best = list(path)
                    path.pop()
                    path[-1] += 1
                else:
                    path.append(0)
            else:
                # bad step
                path[-1] += 1
        pages = [list() for _ in range(max(best) + 1)]
        for i, isle in zip(best, islands):
            pages[i].append(isle)
        for page in pages:
            can_pack_fn(page, cage_size)
        return [Page(i, page) for i, page in enumerate(pages)]
    return result

rect_pack_bpy = branch_and_bound(can_pack_bpy)


from configs import *
from utils import *

from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['POST'])

def main():

    #Ações que devem ser realizadas pelos comandos
    #####################################################################

    #Videoke - Listas Prontas
    #############################
    def consultar_videoke(call,listaVideoke):
        bot.send_message(call.message.chat.id,'Você selecionou a opção Consultar Videokê Tutu! Baixando a lista do BQ...',parse_mode='html')
        bot.send_chat_action(call.message.chat.id,"typing")

        select = f"SELECT Interprete, Nome, Codigo FROM `videokebot.videoke.videoke` where {listaVideoke} = 'Sim' order by Interprete Asc, Nome Asc"
        df_query = query_table(select)

        bot.send_chat_action(call.message.chat.id,"typing")

        listaFinalVideoke = df_query.values.tolist()
        lenListaFinalVideoke = len(str(listaFinalVideoke))

        enviador_lista_call(call,lenListaFinalVideoke,listaFinalVideoke)
    #############################

    #Videoke - Ferramenta de Busca
    #############################
    def buscador(call,tipoBusca):
        mensagemBot = bot.send_message(call.message.chat.id,'Você selecionou a opção por Busca! Por favor, digite o nome do Artista ou Canção',parse_mode='html')

        if tipoBusca == "Artista":
            bot.register_next_step_handler(mensagemBot, busca_handler_videoke_artista)
        else:
            bot.register_next_step_handler(mensagemBot, busca_handler_videoke_cancao)
    #############################

    #Handler para pegar o input do usuário - Artista
    #####################################################################
    def busca_handler_videoke_artista(message):
        busca = message.text.lower()

        bot.send_message(message.chat.id, f"Abrindo o livro do Videoke...")

        select = f"SELECT Interprete, Nome, Codigo FROM `videokebot.videoke.videoke` where REGEXP_CONTAINS(lower(Interprete),'{busca}') order by Interprete Asc, Nome Asc"
        bot.send_chat_action(message.chat.id,"typing")
        df_query = query_table(select)
        listaFinalVideoke = df_query.values.tolist()
        lenListaFinalVideoke = len(str(listaFinalVideoke))
        enviador_lista_message(message,lenListaFinalVideoke,listaFinalVideoke)
    #####################################################################

    #Handler para pegar o input do usuário - Canção
    #####################################################################
    def busca_handler_videoke_cancao(message):
        busca = message.text.lower()

        bot.send_message(message.chat.id, f"Abrindo o livro do Videoke...")

        select = f"SELECT Interprete, Nome, Codigo FROM `videokebot.videoke.videoke` where REGEXP_CONTAINS(lower(Nome),'{busca}') order by Interprete Asc, Nome Asc"
        bot.send_chat_action(message.chat.id,"typing")
        df_query = query_table(select)
        listaFinalVideoke = df_query.values.tolist()
        lenListaFinalVideoke = len(str(listaFinalVideoke))
        enviador_lista_message(message,lenListaFinalVideoke,listaFinalVideoke)
    #####################################################################

    #Acionador Inicial
    #####################################################################
    @bot.message_handler(content_types=["text"])
    def responder(message):
        print(message) #Útil para debugar
        if message.chat.id == USER_ID_1 or message.chat.id == USER_ID_2:
            first_name = message.from_user.first_name
            last_name = message.from_user.last_name
            textoEntrada = f'Olá, <b>{first_name} {last_name}</b>! Bem vindo ao VideokêBot!'
            bot.reply_to(message,textoEntrada,parse_mode='html')

            envia_menu(message)
        else:
            texto = f"O seu id de usuário ({message.chat.id}) não é um id autorizado! Caso conheça o Tutu, mande este número para que ele te dê acesso."
            bot.reply_to(message,texto)
    #####################################################################

    #Acionador Funções
    #####################################################################
    @bot.callback_query_handler(func=lambda message: True)
    def callback_query(call):
        if call.data == "consultarVideokeTutuMpb":
            bot.answer_callback_query(call.id, "Consultar Videokê Tutu - MPB!")
            listaVideoke = "FavoritoTutuNacionalMPB"
            consultar_videoke(call,listaVideoke)

        if call.data == "consultarVideokeTutuNacionais":
            bot.answer_callback_query(call.id, "Consultar Videokê Tutu - Nacionais!")
            listaVideoke = "FavoritoTutuNacionalOutros"
            consultar_videoke(call,listaVideoke)

        if call.data == "consultarVideokeTutuInternacionais":
            bot.answer_callback_query(call.id, "Consultar Videokê Tutu - Internacionais!")
            listaVideoke = "FavoritoTutuInternacional"
            consultar_videoke(call,listaVideoke)

        if call.data == "consultarVideokeCarolNacionais":
            bot.answer_callback_query(call.id, "Consultar Videokê Carol - Nacionais!")
            listaVideoke = "FavoritoCarolNacional"
            consultar_videoke(call,listaVideoke)

        if call.data == "consultarVideokeCarolInternacionais":
            bot.answer_callback_query(call.id, "Consultar Videokê Carol - Internacionais!")
            listaVideoke = "FavoritoCarolInternacional"
            consultar_videoke(call,listaVideoke)

        if call.data == "buscarArtista":
            bot.answer_callback_query(call.id, "Você optou por Buscar Artista!")
            tipoBusca = "Artista"
            buscador(call,tipoBusca)

        if call.data == "buscarCancao":
            bot.answer_callback_query(call.id, "Você optou por Buscar Canção!")
            tipoBusca = "Cancao"
            buscador(call,tipoBusca)
    #####################################################################

    #Ativa o bot.
    bot.infinity_polling(timeout=10, long_polling_timeout = 5)

    return "OK"

#Comente quando for subir pra núvem. Útil para testes locais.
print(main())


"""User and Feedback models detailed."""

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import backref

db = SQLAlchemy()

def connect_db(app):
    """Connect to the database."""
    db.app = app
    db.init_app(app)

class Pinned(db.Model):
    """Connetion of a pinner and the pinned person."""

    __tablename__='pinned'

    pinned_user = db.Column(db.Integer,
                            db.ForeignKey('users.id', ondelete='cascade'),
                            primary_key=True)
    pinning_user = db.Column(db.Integer,
                             db.ForeignKey('users.id', ondelete="cascade"),
                             primary_key=True)

class User(db.Model):
    """User model."""

    __tablename__ = "users"

    id = db.Column(db.Integer,
                   primary_key=True,
                   autoincrement=True)
    username = db.Column(db.String(20),
                         unique=True,
                         nullable=False)
    first_name = db.Column(db.String(30),
                           nullable=False)
    last_name = db.Column(db.String(30),
                          nullable=False)
    job_title = db.Column(db.String(50))
    availability = db.Column(db.String(20))
    rooms = db.relationship(
        "Room",
        secondary="users_rooms",
        back_populates='users')
    pinned_people = db.relationship(
        "User",
        secondary="pinned",
        primaryjoin=(Pinned.pinned_user == id),
        secondaryjoin=(Pinned.pinning_user == id),
        backref=backref('pinning_user', lazy='dynamic'),
        lazy='dynamic'
    )

    def __repr__(self):
        return f"<User {self.id} {self.username} {self.first_name} {self.last_name} {self.job_title} {self.availability} {self.rooms} {self.pinned_people} >"

class Room(db.Model):
    """Room model."""

    __tablename__ = "rooms"

    id = db.Column(db.Integer,
                   primary_key=True,
                   autoincrement=True)
    name = db.Column(db.String(50),
                      nullable=False,
                      unique=True)
    notes = db.Column(db.String())
    users = db.relationship(
        "User",
        secondary="users_rooms",
        back_populates='rooms')
    tasks = db.relationship('Task')
    events = db.relationship('Event')

    def __repr__(self):
        return f"<Room {self.id} {self.name} {self.notes} {self.users} {self.tasks} {self.events} >"

class Task(db.Model):
    """Task model."""

    __tablename__ = "tasks"

    id = db.Column(db.Integer,
                   primary_key=True,
                   autoincrement=True)
    complete = db.Column(db.String(5))
    item = db.Column(db.String(100),
                     nullable=False)
    room = db.Column(db.Integer,
                     db.ForeignKey('rooms.id', ondelete='CASCADE'),
                     nullable=False)

    def __repr__(self):
        return f"<Task {self.id} {self.complete} {self.item} {self.room} >"

class Event(db.Model):
    """Event model."""

    __tablename__ = "events"

    id = db.Column(db.Integer,
                   primary_key=True,
                   autoincrement=True)
    name = db.Column(db.String(50),
                     nullable=False)
    datetime = db.Column(db.DateTime,
                     nullable=False)
    room = db.Column(db.String(),
                     db.ForeignKey('rooms.name', ondelete='CASCADE'),
                     nullable=False)

    def __repr__(self):
        return f"<Event {self.id} {self.name} {self.datetime} {self.room} >"

class User_Room(db.Model):
    """Many to many table for users and rooms."""

    __tablename__ = "users_rooms"

    user_id = db.Column(db.Integer,
                        db.ForeignKey('users.id'),
                        primary_key=True)
    room_id = db.Column(db.Integer,
                        db.ForeignKey('rooms.id'),
                        primary_key=True)


#show data from table : data (id,rate)

import sqlite3

con = sqlite3.connect("mydb.db") #connect to database mydb or create mydb

sql = 'select * from data'
result = con.execute(sql) #read database table to memory
#print(result)
for cols in result:
    print(cols)
    print(cols[0])
    print(cols[1])
    print('-------')

del result #delete result in memory
con.close() #close connect database mydb


#Function takes an item from list and prints one after another with comma and space + and before last item

spam = ['apples', 'bananas', 'tofu', 'cats']

def PrintWithComas(spam):

    stored = ""
    for number in range(1, len(spam)):

        stored += spam[number-1] + ", "

        if number == len(spam)-1:

            stored = stored + "and " + spam[number]

    print(stored)

PrintWithComas(spam)


# program for area and peri of circle using static method
import math
class circle:
    @classmethod
    def pival(cls):
        cls.pi=round(math.pi,2)

    def getval(self):
        self.r=int(input("enter the value of radius: "))
        self.pival()

class calcu:
    @staticmethod
    def calc(obj):
        print(obj.__dict__)
        area=obj.pi*obj.r**2
        peri=2*obj.pi*obj.r
        print("Area = {}".format(area))
        print("peri ={}".format(peri))
c=circle()
c.getval()
calcu.calc(c)


import sqlite3

GROUPS_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS groups (
    group_id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    UNIQUE(name)
)
"""

DATAS_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS datas (
    data_id INTEGER PRIMARY KEY AUTOINCREMENT,
    group_id INTEGER,
    name TEXT,
    data BLOB,
    UNIQUE(group_id, name),
    foreign key (group_id) references groups(group_id)
)
"""

class DB:
    def __init__(self, path):
        self.conn = sqlite3.connect(path, check_same_thread=(False if sqlite3.threadsafety == 3 else True))
        self.ensure_tables()

    def ensure_tables(self):
        with self.conn:
            self.conn.execute(GROUPS_TABLE_SQL)
            self.conn.execute(DATAS_TABLE_SQL)

    def is_exists_group(self, group_name):
        exists = self.conn.execute("SELECT 1 from groups WHERE name = ?;", (group_name,)).fetchone()
        return not (exists is None)

    def is_exists_data(self, group_name, data_name):
        exists = self.conn.execute("SELECT 1 FROM datas WHERE group_id IN (SELECT group_id FROM groups WHERE name = ?) AND name = ?;", (group_name, data_name)).fetchone()
        return not (exists is None)

    def iter_group_names(self):
        for row in self.conn.execute("SELECT name FROM groups"):
            yield row[0]

    def iter_group_data_names(self, group_name):
        for row in self.conn.execute("SELECT name FROM datas WHERE group_id IN (SELECT group_id FROM groups WHERE name = ?)", (group_name,)):
            yield row[0]

    def _get_data_rowid(self, group_name, data_name):
        result = self.conn.execute("SELECT rowid FROM datas WHERE group_id IN (SELECT group_id FROM groups WHERE name = ?) AND name = ?;", (group_name, data_name)).fetchone()
        if not result:
            return
        return result[0]

    def get_data(self, group_name, data_name):
        rowid = self._get_data_rowid(group_name, data_name)
        if not rowid:
            return

        return self.conn.blobopen("datas", "data", rowid)

    def write_data(self, group_name, data_name, data, offset=0):
        with self.conn as conn:
            old = self.get_data(group_name, data_name).read()

            size = offset + len(data)
            self.clear_data(group_name, data_name, size)

            rowid = self._get_data_rowid(group_name, data_name)
            if not rowid:
                return

            with conn.blobopen("datas", "data", rowid) as blob:
                blob.write(old)
                blob[offset:] = data

    def clear_data(self, group_name, data_name, size=0):
        with self.conn as conn:
            conn.execute("UPDATE datas SET data = zeroblob(?) WHERE group_id IN (SELECT group_id FROM groups WHERE name = ?) AND name = ?;", (size, group_name, data_name))

    def create_group(self, group_name):
        with self.conn as conn:
            conn.execute("INSERT INTO groups(name) VALUES(?);", (group_name,))

    def create_data(self, group_name, data_name):
        with self.conn as conn:
            conn.execute("INSERT INTO datas(group_id, name, data) VALUES((SELECT group_id FROM groups WHERE name = ?), ?, zeroblob(0));", (group_name, data_name))

if __name__ == '__main__':
    db = DB(":memory:")
    db.create_group("test")
    db.create_data("test", "test")
    db.write_data("test", "test", b'{"a": 100')
    db.write_data("test", "test", b', "b": 200}', 9)
    print(list(db.iter_group_names()))
    print(list(db.iter_group_data_names("test")))
    print(db.get_data("test", "test").read())
    print(db.conn.execute("SELECT json_extract(data, '$.b') FROM datas;").fetchall())
    breakpoint()


from libnn import *

import csv
import copy

import operator

def sort_table(table, cols):
    """ sort a table by multiple columns
        table: a list of lists (or tuple of tuples) where each inner list
               represents a row
        cols:  a list (or tuple) specifying the column numbers to sort by
               e.g. (1,0) would sort by column 1, then by column 0
    """
    for col in reversed(cols):
        table = sorted(table, key=operator.itemgetter(col))
    return table

def sortt(mytable):
    for row in sort_table(mytable, (2,0)):
        print (row)

iris_network = createNetwork([4,5,3,3],-1,1)
iris_copy = copy.deepcopy(iris_network)

data = []
training_data = []
validation_data = []

with open('shuffled.csv','r') as d :
    reader = csv.reader(d,delimiter = ',')
    for row in reader:
        data.append(row)

training_data = data[:(int(len(data)*0.8))]
validation_data = data[int(len(data)*0.8):]
ml = iris_copy
lr = 0.1
for _ in range(5000):
    for data_point in training_data:
        #print(data_point)
        for i in range(4):
            data_point[i] = float(data_point[i])
        #print(data_point[4])
        if data_point[4] == 'Iris-virginica':
           # print("fucking hell")
            ml.learn(data_point[0:4],lr,[0,0,1])
        elif data_point[4]== 'Iris-versicolor':
            ml.learn(data_point[0:4],lr,[0,1,0])
        else:
            ml.learn(data_point[0:4],lr,[1,0,0])
win     = len(validation_data)
correct = 0
for validation_point in validation_data:
    #print(ml.result(validation_point))
    #print(validation_point)
    for i in range(4):
        validation_point[i] = float(validation_point[i])
    #print(ml.result(validation_point[0:4]))

    if validation_point[4] == 'Iris-virginica':
        if tobin(ml.result(validation_point[0:4])) == [0,0,1]:
            print(1)
            correct = correct + 1
    elif validation_point[4]== 'Iris-versicolor':
        if tobin(ml.result(validation_point[0:4]))==[0,1,0]:
            print(2)
            correct = correct + 1
    else:
        if tobin(ml.result(validation_point[0:4]))==[1,0,0]:
            print(3)
            correct = correct +1

print (" total validation points are :", win)
print ("total correct are ", correct)
print ("score =" , win / correct *100)


from sklearn.metrics.pairwise import pairwise_distances

from modules.word_embedding import WordEmbedding
from modules.utils.text_util import split_to_sentences
from modules.similarity_checker import SimilarityChecker

class SectionAssigner:
    @staticmethod
    def assign_section(sentences: [str], wikipedia: dict, word_embedding: WordEmbedding) -> (str, int):
        nearest_section = None
        nearest_distance = None

        text = ''
        for sentence in sentences:
            text += sentence + ' '

        text = text.strip()
        sentence_vector = word_embedding.calculate_vector_avg(text)

        centroids = {}
        for section, content in wikipedia.items():
            paragraph = split_to_sentences(content)
            centroids[section] = word_embedding.calculate_paragraph_vector_avg(paragraph)

        for section, vector in centroids.items():
            if nearest_section is None or nearest_distance is None:
                nearest_section = section
                nearest_distance = pairwise_distances([vector], [sentence_vector], metric='euclidean')
            else:
                current_distance = pairwise_distances([vector], [sentence_vector], metric='euclidean')
                if current_distance < nearest_distance:
                    nearest_section = section
                    nearest_distance = current_distance

        return nearest_section, nearest_distance

    @staticmethod
    def assign_section_cosine(sentences: [str], wikipedia: dict, word_embedding: WordEmbedding) -> (str, int):
        most_similar_section = None
        highest_similarity = None

        for section, content in wikipedia.items():
            wiki_paragraph = split_to_sentences(content)

            if most_similar_section is None or highest_similarity is None:
                most_similar_section = section
                highest_similarity = SimilarityChecker.calculate_paragraph_similarity(
                    sentences, wiki_paragraph, word_embedding
                )
            else:
                current_similarity = SimilarityChecker.calculate_paragraph_similarity(
                    sentences, wiki_paragraph, word_embedding
                )
                if current_similarity > highest_similarity:
                    most_similar_section = section
                    highest_similarity = current_similarity

        return most_similar_section, highest_similarity


# encoding: utf-8

import ast
from IPython.display import display, HTML
from ipywidgets import widgets
from Appearance import LayoutSytle
from SearchFiles import FindFiles, RefineJson, ApplyFilters

_lt = LayoutSytle()

class BuildFilterPanel:

    def __init__(self, path):

        self._css = """
            <style>
            /*.container{
                    width: 98%;
                }*/

            .output{
                height:100%;
                }

            .output_scroll{
                height: 100%;
                }

            .widget-label{
                    height: 100px;
                    white-space: wrap;
                    font-weight: bold;
                    font-size: 16px;
                    text-align: left;
                    }

            .widget-checkbox {
                    width: 90%;
                }

            .space{
                text-align: left;
                font-size: 13px;
                }

            .space-checkBox{
                text-align: left;
                font-size: 13px;
                margin-left: 3%;
                }

            .vazio{
                color: red;
                font-size: 12px;
                text-align: left;
                }

            .center{
                text-align: center;
                font-size: 17px;
                }

            .blue{
                color: blue;
                font-size: 11.5px;
                }

            .vertical-line{
                border-right: 1px groove black;
                }

            .horizontal-line{
                border-bottom: 1px groove black;
                }

            .top-line{
                border-top: 1px groove black;
                }
            .border{
                border: 1px groove black;
                border-radius: 5px;
                }
            </style>"""

        ###### Boxes Creation ######
        self._static = widgets.HBox()
        self._multiple_box = widgets.VBox()
        self._uniquePlusCollapse = widgets.HBox()
        self._selectMultiple = widgets.SelectMultiple()
        self._select = widgets.VBox()

        ###### Buttons Creation ######
        self._collapse = widgets.Button(description="", tooltip="Collapse", icon="fa-angle-double-up")
        self._filter_btn = widgets.Button(description="Aplicar Filtros", icon="fa-paper-plane", button_style="primary")
        self._applyChooses = widgets.Button(description="Aplicar", button_style="primary", icon="fa-check-circle")

        ###### Variables Creation ######
        self._path = path
        self._unicos = list()
        self._multiplas = list()
        self.finded = list()
        self._filtro = dict()
        self._arquivo = dict()
        self._arquivos = dict()
        self._refined = dict()
        self._fullFiles = dict()
        self._file_Path = dict()

        ###### Variable that receive external methods return - Creation ######
        self._emExibicao = widgets.Label(value="", align_self="center")
        self._buildJson = FindFiles(self._path)
        self._mj = self._buildJson.montaJson()
        self._refineJson = RefineJson(self._mj)
        self._json = self._refineJson.build()

        self._leftBox()

    def _buildSingularFilters(self):
        uniKey = self._json['unique'].keys()
        for i in uniKey:
            key = str(i.encode("ascii", "replace"))
            value = str(self._json['unique'][i])
            value = value.encode("ascii", "replace")
            description = str(self._json['unique'][i])

            self._unicos.append(self._labelSingularFilter(key, value ,description))

        if len(self._unicos) == 0:
            self._unicos.append(self._notUnicos())

        return self._unicos

    @staticmethod
    def _notUnicos():
        return widgets.Label(value="Nao Ha Caracteristicas Unicas", description="Nao ha", display="flex-shrink", layout = _lt.notUnicos_layout()).add_class('vazio')

    @staticmethod
    def _labelSingularFilter(key, value, description):
        return widgets.Label(value=key + ": " + value, description=description, display="flex-wrap", width="95%", style={'description_width':'initial'}).add_class('space').add_class('blue')

    def _buildMultipleFilters(self):
        multiplos = dict()
        multKey = self._json['multiple'].keys()
        for j in multKey:
            listaDeCheckBox = list()
            for k in self._json['multiple'][j]:
                option = self._checkBoxMultipleFilters(unicode(str(k)), str(j))
                option.observe(self._change, names='value')
                listaDeCheckBox.append(option)

            multiplos[j] = listaDeCheckBox

        for k, v in multiplos.iteritems():
            sBox = widgets.VBox()
            sBox.children=[self._labelMultipleFilter(str(k), str(k)), self._boxMultipleFilters(v)]
            sBox.log.info = str(k)
            # print(sBox.log.info)
            self._multiplas.append(sBox)
            # self._multiplas.append(self._labelMultipleFilter(str(k),str(k)))
            # self._multiplas.append(self._boxMultipleFilters(v))

    def _change(self, x):
        key = x.owner.style.description_width
        value = x.owner.description
        if x.new:
            if not key in self._filtro.keys():
                self._filtro[key] = list()
            if not value in self._filtro[key]:
                self._filtro[key].append(value)
        else:
            if len(self._filtro[key]) == 1:
                del (self._filtro[key])
            else:
                self._filtro[key].remove(value)

    @staticmethod
    def _checkBoxMultipleFilters(description, auxiliar):
        return widgets.Checkbox(description=description, encoding="ascii", width="100%", display='flex', indent=False, style={"description_width": auxiliar}).add_class('space-checkBox')

    @staticmethod
    def _labelMultipleFilter(value, description):
        return widgets.Label(value=value.capitalize(), description=description.capitalize(), display="flex-shrink", layout=_lt.labelFilterMultiple_layout()).add_class("space")

    def _boxMultipleFilters(self, childrens):
        boxMultipleFilter = widgets.HBox([v for v in childrens], layout=_lt.boxMultipleFilter_layout())
        return boxMultipleFilter

    def _staticBox(self):
        unicos = self._buildSingularFilters()
        self._static.children = [i for i in unicos]
        self._static.layout = _lt.staticBox_layout()
        return self._static

    def _multipleBox(self):
        self._buildMultipleFilters()
        self._multiple_box.layout = _lt.multipleBox_layout()
        self._multiple_box.style = {'description_width': 'initial'}
        self._multiple_box.children = [i for i in self._multiplas]
        return self._multiple_box

    def _filterButton(self):
        self._filter_btn.layout = _lt.filterBtn_layout()
        self._filter_btn.style.button_color = '#009850'  # '#2CD660'
        self._filter_btn.on_click(self._aplicaFiltros)
        return self._filter_btn

    def _multiSelection(self):
        self._selectMultiple.layout = _lt.selectMultiple_layout()
        return self._selectMultiple

    def _applyChooseButton(self):
        self._applyChooses.layout = _lt.applyChooseButton_layout()
        self._applyChooses.style.button_color = "#009850"
        self._applyChooses.on_click(self._renderJson)
        return self._applyChooses

    def _getIndices(self):
        indices = dict()
        for i in self._selectMultiple.index:
            for j in self._mj:
                if self._file_Path[self._selectMultiple.options[i]] in self._mj[j]['file']:
                    indices[j] = self._selectMultiple.options[i]

        self._arquivos = indices
        return indices

    def _getProperties(self, dictionary):
        intersection = dict()
        indexes = dictionary.keys()
        keys = self._mj[indexes[0]]['content'].keys()
        for i in indexes:
            for j in keys:
                if not dictionary[i] in intersection:
                    intersection[dictionary[i]] = dict()
                intersection[dictionary[i]][j] = self._mj[i]['content'][j]  # - intersection[j]

        self._refined = intersection
        return intersection

    def _renderJson(self, n):
        indices = self._getIndices()
        intersection = self._getProperties(indices)

        from PreRenderJson import BuildTree
        mt = BuildTree()

        for i in intersection.keys():
            for j in intersection[i]['message']:
                if isinstance(j, unicode) and ":" in j:
                    k = j.split(":")
                    j = str("{'"+k[0]+"'"+":'"+k[1]+"'}")
                if not isinstance(j, unicode):
                    mt.fillNumProperties(ast.literal_eval(j))
            self._arquivo[i] = mt.dict

        from RenderJson import RenderJSON
        for k, v in self._arquivo.iteritems():
            pass
            # print k
            # display(RenderJSON(v))

    def exibeFiltrados(self, jsons):
        self._selectMultiple = self._multiSelection()
        items = list()

        for i in jsons:
            for j in self._mj:
                if i == self._mj[j]['file']:
                    fullPath = str(i).split("/")
                    arq = fullPath[-1]
                    self._file_Path[arq] = i
                    items.append(arq)
        options = [i for i in items]
        self._selectMultiple.options = options
        self._selectMultiple.layout.visibility = "visible"
        self._select.children = [self._selectMultiple, self._applyChooseButton()]
        self._fullFiles = self._file_Path
        display(self._select)

    def _aplicaFiltros(self, n):
        af = ApplyFilters(self._filtro, self._mj)
        filtrado = af.make()
        capturados = set()
        for k in filtrado.keys():
            for v in filtrado[k]:
                capturados.add(v)

        if len(filtrado.keys()) != 0:
            self._emExibicao.value = "Sua busca retornou " + str(len(capturados)) + " de " + str(len(self._mj.keys())) + " arquivos"

        self.finded = list(capturados)
        # display('Os arquivos estao salvos na varivel finded. Para visualizar solicite a impressao da mesma. Caso queira exibir na caixa de dialogo, invoque o metodo exibeFiltrados.')

    def _uniqueCollapse(self):
        self._uniquePlusCollapse.children = [widgets.Label(value="Caracteristicas Unicas", description="Caracteristicas Unicas", display="flex-shrink", layout = _lt.uniqueCollapse_layout()), self._collapsable()]
        return self._uniquePlusCollapse

    def _collapseUnicas(self, n):
        if (self._static.layout.visibility == "hidden"):
            self._collapse.icon = "fa-angle-double-up"
            self._collapse.tooltip = "Collapse"
            self._static.layout.height = "100%"
            self._static.layout.visibility = "visible"
        else:
            self._static.layout.visibility = "hidden"
            self._static.layout.height = "0"
            self._collapse.tooltip = "Expand"
            self._collapse.icon = "fa-angle-double-down"

    def _collapsable(self):
        self._collapse.layout = _lt.collapseButton()
        self._collapse.style.button_color = "white"
        self._collapse.on_click(self._collapseUnicas)
        return self._collapse

    def _exibicao(self):
        self._emExibicao = widgets.Label(value="Foram encontrados "+str(len(self._mj))+ " arquivos.").add_class("center")
        return self._emExibicao

    def _leftBox(self):
        left_box = widgets.VBox()
        left_box.children = [self._exibicao(),
                            widgets.Label(value="Filtros", description="Filtros", display="flex-shrink", layout=_lt.leftBox_layout()),
                             self._uniqueCollapse(),
                             self._staticBox(),
                             widgets.Label(value="Caracteristicas Multiplas", description="Caracteristicas Multiplas", display="flex-shrink",
                                           layout=_lt.labelCaracteristicasMultiplas_layout()).add_class('top-line'),
                             self._multipleBox(),
                             self._filterButton()]

        left_box.layout = _lt.leftBox_layout()
        display(HTML(self._css))
        display(left_box)


import tensorflow as tf
from PIL import Image
import keras
from keras.datasets import mnist
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.optimizers import Adam, SGD
from keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
from keras.preprocessing import image

(train_x, train_y), (test_x, test_y) = mnist.load_data()
train_x = train_x.astype('float32') / 255
test_x = test_x.astype('float32') / 255

print(train_x.shape)
print(test_x.shape)
print(train_y.shape)
print(test_y.shape)

train_x = train_x.reshape(60000, 784)
test_x = test_x.reshape(10000, 784)
print(train_x.shape)
print(test_x.shape)

train_y = keras.utils.to_categorical(train_y, 10)
test_y = keras.utils.to_categorical(test_y, 10)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784, )))
model.add(Dense(256, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation="softmax"))
model.summary()
model.compile(optimizer=Adam(0.001), loss="categorical_crossentropy", metrics=["accuracy"])
model.load_weights("/home/wasi/Desktop/scrapy_projects/DL_Projects/Project/mnistmodel.h5")
#
# my_callBacks = [EarlyStopping(monitor='val_acc', patience=5, mode=max)]
# model.fit(train_x, train_y, batch_size=32, epochs=15, verbose=1,
#           callbacks=my_callBacks, validation_data=(test_x, test_y))
# model.save("mnistmodel.h5")
# accuracy = model.evaluate(x=test_x, y=test_y, batch_size=32)
# print("Accuracy of the model is --> {}".format(accuracy[1] * 100))

img = image.load_img(path="/home/wasi/Downloads/1_0D7K4JZNABjK2RMQyM5zVQ.png", color_mode='grayscale',
                     target_size=(28, 28, 1))
img = image.img_to_array(img)
test_img = img.reshape((1, 784))
img_class = model.predict_classes(test_img)
prediction = img_class[0]
classname = img_class[0]
print("Class: ", classname)
img = img.reshape((28, 28))
plt.imshow(img)
plt.title(prediction)
plt.show()


# Python program to implement a basic Pong game
from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
from score_keeper import ScoreBoard

PADDLE_RIGHT_X = 390
PADDLE_LEFT_X = -390

# Create the screen
screen = Screen()
screen.title("Python Pong Game")
screen.bgcolor("black")
screen.setup(width=900, height=700)
screen.tracer(0)
print(screen.screensize(800, 600))

# Drawing the border
boundary = Turtle()
boundary.hideturtle()
boundary.pensize(1)
boundary.color("white")
boundary.penup()
boundary.goto(-400, 300)
boundary.pendown()
for _ in range(2):
    boundary.forward(800)
    boundary.right(90)
    boundary.forward(600)
    boundary.right(90)

boundary.penup()
boundary.goto(0, 295)
boundary.pensize(2)
# boundary.color("white")
boundary.right(90)
for _ in range(30):
    boundary.pendown()
    boundary.forward(10)
    boundary.penup()
    boundary.forward(10)

# Create and move paddle objects
paddle_right = Paddle(20, 100, PADDLE_RIGHT_X, 0)
paddle_left = Paddle(20, 100, PADDLE_LEFT_X, 0)
screen.update()

# Create and move the ball object
ball = Ball()

# Create scoreboards
score_board_left = ScoreBoard(-400, "LEFT")
score_board_right = ScoreBoard(400, "RIGHT")

screen.listen()
screen.onkeypress(key="Up", fun=paddle_right.move_up)
screen.onkeypress(key="Down", fun=paddle_right.move_down)
screen.onkeypress(key="w", fun=paddle_left.move_up)
screen.onkeypress(key="s", fun=paddle_left.move_down)

game_on = True
while game_on:
    screen.update()
    ball.move()  # Starts with upward movement for a new game

    # Detect collision with the top and bottom walls and bounce
    if ball.ycor() > 281 or ball.ycor() < -280:  # Check for top and lower walls
        ball.bounce_up_down()

    # Detect collision with the paddle and bounce back
    if ball.xcor() >= PADDLE_RIGHT_X - 21 and ball.distance(paddle_right) < 50:
        print(f"Ball hit paddle {ball.distance(paddle_right)}")
        ball.bounce_front_back()

    if ball.xcor() <= PADDLE_LEFT_X + 21 and ball.distance(paddle_left) < 50:
        ball.bounce_front_back()

    # Detect if ball goes beyond the right and left walls
    if ball.xcor() > 400:  # Right player loses point
        print("Point for Left Player")
        score_board_left.increase_score()  # Keep scorecard
        ball.color("red")
        screen.update()
        ball.reset_position()
    elif ball.xcor() < -400:  # Left player loses point
        print("Point for Right Player")
        score_board_right.increase_score()  # Keep scorecard
        ball.color("red")
        screen.update()
        ball.reset_position()

screen.exitonclick()


"""
Demo the model at test time
"""
import os
import pickle
import yaml
import numpy as np

from utils import load_data, get_wide_deep_model, process_data, parse_cli_args

def main():
    """Main block of code. Loads the data, model and vectoriser and shows a demo"""
    args = parse_cli_args()
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Supress TF warnings

    print("Loading data...")
    X, y = load_data(
        dataset_path=args.data_path,
        feature_field=args.features_field,
        target_field=args.target_field,
    )
    # Choose five random examples to show
    random_idx = (np.random.rand(5)*len(X)).astype(int)
    X = [X[idx] for idx in random_idx]
    y = [y[idx] for idx in random_idx]

    X_wide_deep = process_data(
        text_feature=X,
        count_vec=pickle.load(open(args.vectoriser_path, "rb")),
    )

    print("Constructing Keras model")
    prediction_model = get_wide_deep_model(
        num_wide_features=X_wide_deep[0].shape[1],
        num_deep_features=X_wide_deep[1].shape[1],
        **yaml.safe_load(open(args.model_config, "r")),
    )
    prediction_model.load_weights(args.model_path)
    print("Predicting...")
    predictions = prediction_model.predict([X_wide_deep[0], X_wide_deep[1]], verbose=0)

    for prediction, text, target in zip(predictions, X, y):
        print("="*100)
        print("Text:\n", text)
        print("Target:", target)
        print("Model's prediction:", prediction)

if __name__ == "__main__":
    main()


# https://practice.geeksforgeeks.org/problems/smallest-subarray-with-sum-greater-than-x5651/1#
# see Dhruv Goyel's video for this.
# https://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/
class Solution:
    def sb(self, a, n, x):
        ans=n+1
        l ,r= 0,0
        sum=0
        while r < n:
            while x >= sum and r < n:
                sum+=a[r]
                r+=1
            while sum > x and l < n:
                ans = min(ans , r-l)
                sum-=a[l]
                l+=1
        return ans


## John Saraya CIS261 Wk5CourseProjectPt2 Using Lists and Dictionaries to Store and Retrieve Data

## Create a new function that will input and return the from date and to date for the hours worked and is called inside the loop. This should be the first function called. Dates must be in the format mm/dd/yyyy.
## Store the from date, to date , employee name, total hours, hourly rate and income tax rate in a list object. Note: multiple list objects may be needed to complete this functionality.
## After the user terminates the loop, call a function that will: Read though the list(s) and for each employee calculate the income tax and net pay. Display the from date, to date, employee name, hours worked, hourly rate, gross pay, income tax rate, income taxes and net pay for the employee. Increment the total number of employees, total hours, total tax, total net pay and store the values in a dictionary object.
## Modify the function that displays totals to read the data from the dictionary object and then display the totals.

def get_name():
    name = input("Enter employee name: ")
    return name
def get_dates_worked():
    fromdate = input("Enter Start Date (mm/dd/yyyy): ")
    todate = input("Enter To Date (mm/dd/yyyy): ")
    return fromdate, todate
def get_hours_worked():
    hours = float(input("Enter total hours worked: "))
    return hours
def get_hourly_rate():
    hourly_rate = float(input("Enter hourly rate: "))
    return hourly_rate
def get_tax_rate():
    tax_rate = float(input("Enter tax rate: "))
    return tax_rate
def calculate_tax_and_netpay(hours, hourly_rate, tax_rate):
    grosspay = hours * hourly_rate
    tax = grosspay * tax_rate
    net_pay = grosspay - tax
    return grosspay, tax, net_pay

def print_info(EmpDetailList):
    total_employees = 0
    total_hours = 0.00
    total_gross_pay = 0.00
    total_tax = 0.00
    total_net_pay = 0.00
    for EmpList in EmpDetailList:
        fromdate = EmpList[0]
        todate = EmpList[1]
        name = EmpList[2]
        hours = EmpList[3]
        hourly_rate = EmpList[4]
        tax_rate = EmpList[5]
        grosspay, tax, net_pay = calculate_tax_and_netpay(hours, hourly_rate, tax_rate)
        print(fromdate, todate, name, f"{hours:,.2f}", f"{hourly_rate:,.2f}", f"{grosspay:,.2f}", f"{tax_rate:,.1%}", f"{tax:,.2f}", f"{net_pay:,.2f}")
        total_employees +=1
        total_hours += hours
        total_gross_pay += grosspay
        total_tax += tax
        total_net_pay += net_pay
        EmpTotals["TotEmp"] = total_employees
        EmpTotals["TotHrs"] = total_hours
        EmpTotals["TotGrossPay"] = total_gross_pay
        EmpTotals["TotTax"] = total_tax
        EmpTotals["TotNetPay"] = total_net_pay

def display_total_info(EmpTotals):
    print()
    print(f'Total number of employees: {EmpTotals["TotEmp"]}')
    print(f'Total hours worked: {EmpTotals["TotHrs"]:,.2f}')
    print(f'Total gross pay: {EmpTotals["TotGrossPay"]:,.2f}')
    print(f'Total tax: {EmpTotals["TotTax"]:,.2f}')
    print(f'Total net pay: {EmpTotals["TotNetPay"]:,.2f}')

if __name__=="__main__":
    EmpDetailList = []
    EmpTotals = {}
    while True:
        name = get_name()
        if (name.upper() == "END"):
            break
        fromdate, todate = get_dates_worked()
        hours = get_hours_worked()
        hourly_rate = get_hourly_rate()
        tax_rate = get_tax_rate()
        EmpDetail = [fromdate, todate, name, hours, hourly_rate, tax_rate]
        EmpDetailList.append(EmpDetail)

    print_info(EmpDetailList)
    display_total_info(EmpTotals)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import copy
import torch
import numpy as np
from tqdm import tqdm
from sklearn.metrics import classification_report, accuracy_score

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

best_model = None
best_accuracy = 0.0
best_epoch = 0

class Trainer(object):

    def __init__(self, configs):
        super(Trainer, self).__init__()
        self.configs = configs

    def train(self, epoch, model, dataloader, optimizer):
        predictions_labels = []
        true_labels = []
        total_loss = 0
        total_acc = 0
        model.train()

        for count, (x_batch, y_batch) in enumerate(dataloader, 1):

            x_batch = x_batch.to(device)
            y_batch = y_batch.to(device)

            optimizer.zero_grad()
            outputs = model(x_batch, labels=y_batch)
            loss, y_pred = outputs[0:2]

            loss.backward()
            optimizer.step()
            total_loss += loss.item()

            y_pred = torch.argmax(y_pred, dim=1)
            correct_num = torch.sum(y_pred == y_batch).double()
            total_acc += correct_num / y_pred.shape[0]
            predictions_labels.append(y_pred.cpu().numpy())
            true_labels.append(y_batch.cpu().numpy())

            print("\x1b[2K\rEpoch: {} / {} Loss: {:.5f} Acc: {:.5f}".format(
                epoch, self.configs["model_params"]["scibert_param"]["epoch_num"], total_loss/count, total_acc/count), end="")

        return

    def evaluate(self, epoch, model, tokenizer, dataloader, model_dir=None):
        global best_accuracy, best_epoch
        best_accuracy = 0.0 if epoch == 0 else best_accuracy
        best_epoch = 0 if epoch == 0 else best_epoch

        predictions_labels = []
        true_labels = []
        total_loss = 0
        total_acc = 0
        model.eval()

        with torch.no_grad():
            for count, (x_batch, y_batch) in enumerate(dataloader, 1):
                x_batch = x_batch.to(device)
                y_batch = y_batch.to(device)
                outputs = model(x_batch, labels=y_batch)
                loss, y_pred = outputs[0:2]

                # compute loss
                total_loss += loss.item()

                # compute accuracy
                y_pred = torch.argmax(y_pred, dim=1)
                correct_num = torch.sum(y_pred == y_batch).double()
                total_acc += correct_num / y_pred.shape[0]
                predictions_labels.append(y_pred.cpu().numpy())
                true_labels.append(y_batch.cpu().numpy())

                print("\x1b[2K\rEval Loss: {:.5f} Acc: {:.5f}".format(total_loss/count, total_acc/count), end="")

        predictions_labels = np.hstack(predictions_labels)
        true_labels = np.hstack(true_labels)
        acc = accuracy_score(true_labels, predictions_labels)
        print("\nAccuracy: {:.5f}\n".format(acc))

        if model_dir is not None:

            if acc > best_accuracy:
                best_model = copy.deepcopy(model.state_dict())
                best_accuracy = acc
                best_epoch = epoch

                model.load_state_dict(best_model)
                print("saving best model at epoch {}".format(best_epoch))
                model.save_pretrained(self.saved_model_dir)
                tokenizer.save_pretrained(self.saved_model_dir)

        return acc, predictions_labels, true_labels