import collections
from distutils.version import LooseVersion
import hashlib as md5
import os
import re
import sys
import warnings

from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst import directives
from docutils.parsers.rst import roles

from . import generate_rst
from .environment import Environment

py3k = sys.version_info >= (3, 0)

def _comma_list(text):
    return re.split(r"\s*,\s*", text.strip())

def _parse_content(content):
    d = {}
    d["text"] = []
    idx = 0
    for line in content:
        idx += 1
        m = re.match(r" *\:(.+?)\:(?: +(.+))?", line)
        if m:
            attrname, value = m.group(1, 2)
            d[attrname] = value or ""
        elif idx == 1 and line:
            # accomodate a unique value on the edge of .. change::
            continue
        else:
            break
    d["text"] = content[idx:]
    return d

class EnvDirective(object):
    @property
    def env(self):
        return Environment.from_document_settings(self.state.document.settings)

    @classmethod
    def get_changes_list(cls, env, hash_on_version):
        key = ("ChangeLogDirective_changes", hash_on_version)
        if key not in env.temp_data:
            env.temp_data[key] = collections.OrderedDict()
        return env.temp_data[key]

class ChangeLogDirective(EnvDirective, Directive):
    """Implement the ``.. changelog::`` directive.

    """

    has_content = True

    default_section = "misc"

    def run(self):
        self._parse()

        if not ChangeLogImportDirective.in_include_directive(self.env):
            return generate_rst.render_changelog(self)
        else:
            return []

    def _parse(self):
        # 1. pull in global configuration from conf.py
        self.sections = self.env.changelog_sections
        self.caption_classes = self.env.changelog_caption_class.split(" ")
        self.inner_tag_sort = self.env.changelog_inner_tag_sort + [""]
        self.hide_sections_from_tags = bool(
            self.env.changelog_hide_sections_from_tags
        )
        self.hide_tags_in_entry = bool(self.env.changelog_hide_tags_in_entry)

        # 2. examine top level directives inside the .. changelog::
        # directive.  version, release date
        self._parsed_content = parsed = _parse_content(self.content)
        self.version = version = parsed.get("version", "")
        self.release_date = parsed.get("released", None)
        self.is_released = bool(self.release_date)
        self.env.temp_data["ChangeLogDirective"] = self

        content = self.content

        # 3. read extra per-file included notes
        if "include_notes_from" in parsed:
            if content.items and content.items[0]:
                source = content.items[0][0]

                # seems we are now getting strings like:
                # changelog/changelog_11.rst <included from
                # /home/classic/dev/sqlalchemy/doc/build/changelog/changelog_12.rst>
                source = source.split(" ")[0]

                path = os.path.join(
                    os.path.dirname(source), parsed["include_notes_from"]
                )
            else:
                path = parsed["include_notes_from"]
            if not os.path.exists(path):
                raise Exception("included nodes path %s does not exist" % path)

            files = [
                fname for fname in os.listdir(path) if fname.endswith(".rst")
            ]
            for fname in self.env.status_iterator(
                files, "reading changelog note files (version %s)..." % version
            ):
                fpath = os.path.join(path, fname)
                with open(fpath) as handle:
                    content.append("", path, 0)
                    for num, line in enumerate(handle):
                        if not py3k:
                            line = line.decode("utf-8")
                        if "\t" in line:
                            warnings.warn(
                                "file %s has a tab in it! please "
                                "convert to spaces." % fname
                            )
                            line = line.replace("\t", "    ")
                        line = line.rstrip()
                        content.append(line, path, num)

        # 4. parse the content of the .. changelog:: directive. This
        # is where we parse individual .. change:: directives and construct
        # a list of items, stored in the env via self.get_changes_list(env)
        p = nodes.paragraph("", "")
        self.state.nested_parse(content[1:], 0, p)

class ChangeLogImportDirective(EnvDirective, Directive):
    """Implement the ``.. changelog_imports::`` directive.

    Here, we typically load in other changelog.rst files which may feature
    elements that also apply to our current changelog.rst file, when they
    specify the ``:version:`` modifier.

    """

    has_content = True

    @classmethod
    def in_include_directive(cls, env):
        return "ChangeLogDirective_includes" in env.temp_data

    def run(self):
        # tell ChangeLogDirective we're here, also prevent
        # nested .. include calls
        if not self.in_include_directive(self.env):
            self.env.temp_data["ChangeLogDirective_includes"] = True
            p = nodes.paragraph("", "")
            self.state.nested_parse(self.content, 0, p)
            del self.env.temp_data["ChangeLogDirective_includes"]
        return []

class SeeAlsoDirective(EnvDirective, Directive):
    """implement a quick version of Sphinx "seealso" when running outside
    of sphinx."""

    has_content = True

    def run(self):
        text = "\n".join(self.content)
        ad = nodes.admonition(rawsource=text)
        st = nodes.strong()
        st.append(nodes.Text("See also:"))
        ad.append(st)
        ad.append(nodes.Text("\n"))
        self.state.nested_parse(self.content, 0, ad)
        return [ad]

class ChangeDirective(EnvDirective, Directive):
    """Implement the ``.. change::`` directive.

    """

    has_content = True

    def run(self):
        # don't do anything if we're not inside of a version
        if "ChangeLogDirective" not in self.env.temp_data:
            return []

        content = _parse_content(self.content)

        sorted_tags = _comma_list(content.get("tags", ""))
        changelog_directive = self.env.temp_data["ChangeLogDirective"]
        declared_version = changelog_directive.version
        versions = (
            set(_comma_list(content.get("versions", "")))
            .difference([""])
            .union([declared_version])
        )

        # if we don't refer to any other versions and we're in an include,
        # skip
        if len(
            versions
        ) == 1 and ChangeLogImportDirective.in_include_directive(self.env):

            return []

        body_paragraph = nodes.paragraph(
            "", "", classes=changelog_directive.caption_classes
        )
        self.state.nested_parse(content["text"], 0, body_paragraph)

        raw_text = _text_rawsource_from_node(body_paragraph)
        tickets = set(_comma_list(content.get("tickets", ""))).difference([""])
        pullreq = set(_comma_list(content.get("pullreq", ""))).difference([""])
        tags = set(sorted_tags).difference([""])

        for hash_on_version in versions:
            issue_hash = _get_robust_version_hash(
                raw_text, hash_on_version, tickets, tags
            )

            rec = ChangeLogDirective.get_changes_list(
                changelog_directive.env, hash_on_version
            ).setdefault(issue_hash, {})
            if not rec:
                sorted(versions, key=_str_version_as_tuple)
                rec.update(
                    {
                        "hash": issue_hash,
                        "render_for_version": hash_on_version,
                        "tags": tags,
                        "tickets": tickets,
                        "pullreq": pullreq,
                        "changeset": set(
                            _comma_list(content.get("changeset", ""))
                        ).difference([""]),
                        "node": body_paragraph,
                        "raw_text": raw_text,
                        "type": "change",
                        "title": content.get("title", None),
                        "sorted_tags": sorted_tags,
                        "versions": versions,
                        "version_to_hash": {
                            version: _get_legacy_version_hash(
                                raw_text, version
                            )
                            for version in versions
                        },
                        "source_versions": [declared_version],
                        "sorted_versions": list(
                            reversed(
                                sorted(versions, key=_str_version_as_tuple)
                            )
                        ),
                    }
                )
            else:
                # This seems to occur repeated times for each included
                # changelog, not clear if sphinx has changed the scope
                # of self.env to lead to this occurring more often
                self.env.log_debug(
                    "Merging changelog record '%s' from version(s) %s "
                    "with that of version %s",
                    _quick_rec_str(rec),
                    ", ".join(rec["source_versions"]),
                    declared_version,
                )
                rec["source_versions"].append(declared_version)

                assert rec["raw_text"] == raw_text
                assert rec["tags"] == tags
                assert rec["render_for_version"] == hash_on_version

                rec["tickets"].update(tickets)
                rec["pullreq"].update(pullreq)
                rec["changeset"].update(
                    set(_comma_list(content.get("changeset", ""))).difference(
                        [""]
                    )
                )
                rec["versions"].update(versions)

                rec["version_to_hash"].update(
                    {
                        version: _get_legacy_version_hash(raw_text, version)
                        for version in versions
                    }
                )
                rec["sorted_versions"] = list(
                    reversed(
                        sorted(rec["versions"], key=_str_version_as_tuple)
                    )
                )

        return []

def _quick_rec_str(rec):
    """try to print an identifiable description of a record"""

    if rec["tickets"]:
        return "[tickets: %s]" % ", ".join(rec["tickets"])
    else:
        return "%s..." % rec["raw_text"][0:25]

def _get_legacy_version_hash(raw_text, version):
    # this needs to stay like this for link compatibility
    # with thousands of already-published changelogs
    to_hash = "%s %s" % (version, raw_text[0:100])
    return md5.md5(to_hash.encode("ascii", "ignore")).hexdigest()

def _get_robust_version_hash(raw_text, version, tickets, tags):
    # this needs to stay like this for link compatibility
    # with thousands of already-published changelogs
    to_hash = "%s %s %s %s" % (
        version,
        ", ".join(tickets),
        ", ".join(tags),
        raw_text,
    )
    return md5.md5(to_hash.encode("ascii", "ignore")).hexdigest()

def _text_rawsource_from_node(node):
    src = []
    stack = [node]
    while stack:
        n = stack.pop(0)
        if isinstance(n, nodes.Text):
            src.append(str(n))
        stack.extend(n.children)
    return "".join(src)

_VERSION_IDS = {}

def _str_version_as_tuple(ver):
    if ver in _VERSION_IDS:
        return _VERSION_IDS[ver]

    result = LooseVersion(ver)
    _VERSION_IDS[ver] = result
    return result

def make_ticket_link(
    name, rawtext, text, lineno, inliner, options={}, content=[]
):
    env = Environment.from_document_settings(inliner.document.settings)

    render_ticket = env.changelog_render_ticket or "%s"
    prefix = "#%s"
    if render_ticket:
        ref = render_ticket % text
        node = nodes.reference(rawtext, prefix % text, refuri=ref, **options)
    else:
        node = nodes.Text(prefix % text, prefix % text)
    return [node], []

def make_generic_attrref(
    name, rawtext, text, lineno, inliner, options={}, content=[]
):
    text = text.lstrip(".")
    lt = nodes.literal(rawtext=rawtext)
    lt.append(nodes.Text(text))
    return [lt], []

def make_generic_funcref(
    name, rawtext, text, lineno, inliner, options={}, content=[]
):
    if not text.endswith("()"):
        text += "()"
    return make_generic_attrref(
        name, rawtext, text, lineno, inliner, options, content
    )

def make_generic_docref(
    name, rawtext, text, lineno, inliner, options={}, content=[]
):
    lt = nodes.literal(rawtext=rawtext)
    lt.append(nodes.Text(text))
    return [lt], []

def setup_docutils():
    """register docutils directives and roles assuming Sphinx is not in use."""

    directives.register_directive("changelog", ChangeLogDirective)
    directives.register_directive("change", ChangeDirective)
    directives.register_directive(
        "changelog_imports", ChangeLogImportDirective
    )
    directives.register_directive("seealso", SeeAlsoDirective)
    roles.register_canonical_role("ticket", make_ticket_link)

    # sphinx autodoc stuff we don't have in this context
    roles.register_canonical_role("func", make_generic_funcref)
    roles.register_canonical_role("class", make_generic_attrref)
    roles.register_canonical_role("paramref", make_generic_attrref)
    roles.register_canonical_role("attr", make_generic_attrref)
    roles.register_canonical_role("mod", make_generic_attrref)
    roles.register_canonical_role("meth", make_generic_funcref)
    roles.register_canonical_role("obj", make_generic_attrref)
    roles.register_canonical_role("exc", make_generic_attrref)

    roles.register_canonical_role("doc", make_generic_docref)
    roles.register_canonical_role("ref", make_generic_docref)


import cv2
import numpy as np
import onnxruntime as ort
import sys

"""
Using pre-trained onnx model to do inference of classification,
reference: https://github.com/onnx/models#image_classification
"""
def main():
    # get image path and model path from command arguments
    if len(sys.argv) < 3:
        sys.exit('please input args. 1st is image path, 2nd is model path')
    img_path = sys.argv[1]
    model_path = sys.argv[2]

    # image preprocessing
    # load image with format of BGR
    img = cv2.imread(img_path)
    # convert to RGB
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # model requires 224 * 224, so input image better be square
    img = cv2.resize(img, (224, 224))

    # some transform to fit model
    img = np.array(img).astype(np.float32)
    img = np.expand_dims(img, 0)
    img = np.swapaxes(img, 1, 3)
    img = np.swapaxes(img, 2, 3)
    # print(img.shape)
    # The images have to be loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406]
    # and std = [0.229, 0.224, 0.225]. The transformation should preferably happen at preprocessing.
    mean_vec = np.array([0.485, 0.456, 0.406])
    stddev_vec = np.array([0.229, 0.224, 0.225])
    norm_img_data = np.zeros(img.shape).astype('float32')
    for i in range(img.shape[0]):
         # for each pixel in each channel, divide the value by 255 to get value between [0, 1] and then normalize
        norm_img_data[i,:,:] = (img[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]

    # load model
    ort_session = ort.InferenceSession(model_path)
    # name of input and output
    onnx_input_name = ort_session.get_inputs()[0].name
    onnx_outputs_names = ort_session.get_outputs()
    output_names = []
    for o in onnx_outputs_names:
        output_names.append(o.name)
    # do inference
    onnx_ret = ort_session.run(output_names, {onnx_input_name: norm_img_data})
    # onnx_ret is a list, res is numpy.ndarray with shape (1, 1000)
    res = onnx_ret[0]
    idx = np.argmax(res)

    # get result by index
    i = 0
    for line in open("imageNet1000.txt"):
        if (i == idx):
            print (line)
        i += 1
if __name__ =='__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 26 23:21:58 2022

@author: frederik
"""

#%% Modules

import jax.numpy as jnp
from jax import vmap

#%% Functions

def bayes_reg(X_training, y_training, X_test, sigman=1.0, Sigmap = None, basis_fun=None):

    if basis_fun is None:
        Phi = X_training
        phi_test = X_test
    else:
        Phi = vmap(basis_fun)(X_training).T
        phi_test = vmap(basis_fun)(X_test).T

    if Phi.ndim == 1:
        Phi = Phi.reshape(1,-1)
    if phi_test.ndim == 1:
        phi_test = phi_test.reshape(1,-1)

    n_dim, N_training = Phi.shape

    if Sigmap is None:
        Sigmap = jnp.eye(n_dim)

    sigman2 = sigman**2

    SigmaPhi = Sigmap.dot(Phi)

    K = Phi.T.dot(Sigmap).dot(Phi)+jnp.eye(N_training)*sigman2
    solved = jnp.linalg.solve(K.T, SigmaPhi.T).T

    mu_post = phi_test.T.dot(solved).dot(y_training)
    cov_post = phi_test.T.dot(Sigmap).dot(phi_test)-phi_test.T.dot(solved).dot(SigmaPhi.T).dot(phi_test)

    return mu_post, cov_post


#https://stellar-sdk.readthedocs.io/_/downloads/en/latest/pdf/
from stellar_sdk import Keypair
from stellar_sdk import TransactionBuilder, Server, Network, Keypair
from stellar_sdk import Server
from stellar_sdk import Asset

class XLM_Account_Managment:
    def QUERY_HORIZON(account,url): #Querying Horizon "https://horizon-testnet.stellar.org"
        server = Server(horizon_url=url)

        # get a list of transactions that occurred in ledger 1400
        transactions = server.transactions().for_ledger(1400).call()
        print(transactions)

        # get a list of transactions submitted by a particular account
        transactions = server.transactions() \
        .for_account(account_id=account) \
        .call()
        print(transactions)

    def BUILDING_REQUEST(url,account_id): # "https://horizon-testnet.stellar.org"
        server = Server(horizon_url=url)
        transactions = server.transactions().for_ledger(1400).call()
        print(transactions)

        transactions = server.transactions() \
            .for_account(account_id=account_id) \
            .call()

        print(transactions)

    def STREAMING_REQUEST(url,account_id,last_cursor):
        server = Server(horizon_url=url)
        account_id = account_id
        last_cursor = last_cursor #'now' or load where you left off

        def tx_handler(tx_response):
            print(tx_response)

        for tx in server.transactions().for_account(account_id).cursor(last_cursor).stream():
            tx_handler(tx)

    def ASSETS():
        print("test")

#


#Даны значения величины заработной платы заемщиков банка (zp)
# и значения их поведенческого кредитного скоринга (ks):
# zp = [35, 45, 190, 200, 40, 70, 54, 150, 120, 110],
# ks = [401, 574, 874, 919, 459, 739, 653, 902, 746, 832].
# Используя математические операции, посчитать коэффициенты линейной регрессии,
# приняв за X заработную плату (то есть, zp - признак), а за
# y - значения скорингового балла (то есть, ks - целевая переменная).
# Произвести расчет как с использованием intercept, так и без.

import numpy as np
import sklearn

x1 = np.array([35, 45, 190, 200, 40, 70, 54, 150, 120, 110])
y1 = np.array([401, 574, 874, 919, 459, 739, 653, 902, 746, 832])

b1 = (np.mean(x1*y1) - np.mean(x1)*np.mean(y1))/(np.mean(x1**2)-np.mean(x1)**2)
b0 = np.mean(y1) - b1 * np.mean(x1)
print(b1, b0)
print()
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# X = np.array([35, 45, 190, 200, 40, 70, 54, 150, 120, 110])
# y = np.array([401, 574, 874, 919, 459, 739, 653, 902, 746, 832])
#
# model = LinearRegression()
# s = s.reshape(-1, 1)     # нет установленной функции
# # array([[35],
# #       [45],
# #       [190],
# #       [200],
# #       [40],
# #       [70],
# #       [54],
# #       [150],
# #       [120],
# #       [110]])
#
# regres = model.fit(s, y)
# print(regres.intercept_)
# print(regres.coef_)

x1 = x1.reshape(10, 1)
y1 = y1.reshape(10, 1)
XTX = x1.T.dot(x1)

XTX_inv = np.linalg.inv(XTX)
b = XTX_inv.dot(x1.T).dot(y1)
print(b)
print()

model = LinearRegression(fit_intercept=False)
model.fit(x1, y1)
const = model.intercept_
beta = model.coef_[0]
print(const, beta)

# Посчитать коэффициент линейной регрессии при заработной плате
# (zp), используя градиентный спуск (без intercept).
x1 = np.array([35, 45, 190, 200, 40, 70, 54, 150, 120, 110])
y1 = np.array([401, 574, 874, 919, 459, 739, 653, 902, 746, 832])
alpha = 1e-6
b1 = 0.1
n = len(x1)
def mse_(b1):
    return np.sum((b1 * x1 -y1)**2) / n
for i in range(100000):
    b1 -= alpha * (2 / n) * np.sum((b1 * x1 - y1) * x1)
    if i % 5000 ==0:
        print(f'iteration = {i}, b1 = {b1}, mse = {mse_(b1)}')
print('y =', b1, '* x')
print()

# Произвести вычисления как в пункте 2, но с вычислением intercept.
# Учесть, что изменение коэффициентов должно производиться на каждом
# шаге одновременно (то есть изменение одного коэффициента
# не должно влиять на изменение другого во время одной итерации).

x1 = np.array([35, 45, 190, 200, 40, 70, 54, 150, 120, 110])
y1 = np.array([401, 574, 874, 919, 459, 739, 653, 902, 746, 832])
alpha = 5e-6
b1 = 0.1
b0 = 0.1
n = len(x1)
def mse_2(b0, b1):
    return np.sum((b0 + b1 * x1 -y1)**2) / n
for i in range(1000000):
    y_hat = b0 + b1 * x1
    b1 -= alpha * (2 / n) * np.sum((y_hat - y1) * x1)
    b0 -= alpha * (2 / n) * np.sum(y_hat - y1)
    if i % 100000 == 0:
        print(f'iteration = {i}, b0 = {b0}, b1 = {b1}, mse = {mse_2(b0, b1)}')
print(f'y ={b0} + {b1} * x')


from manim.utils.color import *
from manim import *
from PySide6.QtCore import Signal, QObject

from intermediate.imobject import IMobject, INone,IGroup
from intermediate.itext import IMarkupText
import controllers.mobject_helper as mh

class SceneController(QObject):
    """
    Handles any rendering on the manim scene
    """

    selectedMobjectChange = Signal(IMobject)

    def __init__(self, scene, renderer):
        super().__init__()
        self.scene = scene
        self.renderer = renderer
        scene.handler = self
        self.selected = {}
        self.fsm_controller = None  # to set
        self.ctrldown = False

    def set_fsm_controller(self, fsm_controller):
        self.fsm_controller = fsm_controller

    def add_copy(self, imobject):
        self.scene.add(mh.get_copy(imobject))

    def remove(self, imobject):
        self.scene.remove(mh.get_copy(imobject))

    """ Selection functions """

    def set_selected_mobject(self, mobject, ctrldown=False):
        if ctrldown:
            self.ctrldown = True

        if not self.ctrldown:  # TODO
            self.unselect_mobjects()

        imobject = mh.get_original(mobject)
        self.set_selected_imobject(imobject)

    def set_selected_imobject(self, imobject):
        if imobject.parent_imobject is not None:
            imobject = imobject.parent_imobject

        if imobject.group is not None:
            imobject = imobject.group

        mobject = mh.get_copy(imobject)
        if mobject in self.selected:
            return

        self.selected[mobject] = mobject.get_color().hex_l
        # print("SELECT", self.selected)

        if not isinstance(imobject, IMarkupText) and not isinstance(imobject, IGroup):
            mobject.set_color("#8fbc8f")

        self.fsm_controller.curr.capture_prev(mobject)

        # print(imobject)
        self.selectedMobjectChange.emit(imobject)

    def unselect_mobjects(self):
        self.ctrldown = False
        for mobject, color in self.selected.items():
            imobject = mh.get_original(mobject)

            if not isinstance(mobject, MarkupText) and not isinstance(imobject, IGroup):
                mobject.set_color(color)

        self.selected = {}

        self.selectedMobjectChange.emit(INone())

    """" Movement functions """
    # TODO: refactor non-scene related functions out
    def confirm_selected_shift(self, delta, altdown):
        for mcopy in self.selected:
            self.fsm_controller.confirm_move(mcopy, delta, altdown)

    def created_at_curr_state_with_anim(self, mcopy):
        imobject = mh.get_original(mcopy)

        if imobject is None:
            return True  # block any interaction with it

        return self.fsm_controller.created_at_curr_state_with_anim(imobject)

    def move_selected_by(self, delta):
        if not self.selected:
            return
        # target = new_point.get_bounding_box_point(ORIGIN)
        # old = old_point.get_bounding_box_point(ORIGIN)
        for mobject in self.selected:
            # mobject.shift((target - old) * np.array([1, 1, 1]))
            mobject.shift(delta)


# importando os pacotes necessários
import itertools
import os
from sklearn.model_selection import train_test_split
from localbinarypatterns import LocalBinaryPatterns
from sklearn.neighbors import RadiusNeighborsClassifier
from imutils import paths
import cv2
import pandas as pd
import numpy as np
import time
from PIL import Image

inicio = time.time()

def tulis_hasil(hasil, fname):
    hasil = np.array(hasil)
    pd.DataFrame({"r": hasil[:, 0], "points": hasil[:, 1], "radius": hasil[:, 2],
                  "akurasi": hasil[:, 3]}).to_csv(fname, index=False, header=True)

db = 'gt_db'
data_training = 'db/%s' % db

points = range(8, 70, 4)
radius = range(4, 21)
par = list(itertools.product(points, radius))
hasil = []

dataset = [file for file in paths.list_images(data_training)]

# Essa é nossa contribuição
count = 0
for imagePath in paths.list_images(data_training):
    count += 1
    size = 150, 113
    im = Image.open(imagePath)
    width, height = im.size
    if(width!=150 and height!=113):
        im_resized = im.resize(size, Image.ANTIALIAS)
        im_resized.save(str(imagePath)[:13]+str(count)+".jpg", "JPEG")
        os.remove(imagePath, dir_fd=None)

for p in par:
    print("##### points: %d, radius:%d #####" % p)
    # inicializar o descritor de padrões binários locais
    desc = LocalBinaryPatterns(p[0], p[1])
    data = []
    labels = []

    # loop nas imagens de treinamento
    print(data_training, len(dataset))
    for imagePath in paths.list_images(data_training):
        # carregue a imagem, converta-a em tons de cinza e descreva-a
        # print("test:", imagePath)
        image = cv2.imread(imagePath)
        try:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        except:
            print('ERROR di:', imagePath)
        hist = desc.describe(gray)
        # extraia o rótulo do caminho da imagem e atualiza o
        # rótulo e listas de dados
        # print(imagePath.split("/"))  # use "\\" no Windows
        labels.append(imagePath.split("/")[-2])  # use "\\" no Windows
        data.append(hist)
    X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.1, random_state=0)

    # treinar um RNN Linear nos dados
    r_nn = [0.005, 0.01, 0.015, 0.02]
    for r in r_nn:
        # neigh = RadiusNeighborsClassifier(radius=k, outlier_label=0.1)
        # outlier_label=0.1
        '''
        outlier_label{manual label, ‘most_frequent’}, default=None
            label for outlier samples (samples with no neighbors in given radius).
            * manual label: str or int label (should be the same type as y) or list of manual labels if multi-output is used.
            * ‘most_frequent’ : assign the most frequent label of y to outliers.
            * None : when any outlier is detected, ValueError will be raised.
        '''
        neigh = RadiusNeighborsClassifier(radius=r, outlier_label='most_frequent')
        neigh.fit(X_train, y_train)

        benar = 0
        jml = 0
        for i in range(len(y_test)):
            jml += 1
            hist = X_test[i]
            prediction = neigh.predict([hist])[0]
            if prediction == y_test[i]:
                benar += 1
        akurasi = float(benar*100/jml)
        print(benar, jml, r, ": Akurasi", akurasi, "%")
        hasil.append([r, p[0], p[1], akurasi])
tulis_hasil(hasil, "results/{0}_rnn.csv".format(db))

fim = time.time()
print(fim-inicio)


from PIL import Image
import random
import numpy

class RandomBezier:
    def __init__(self, levels,
                    gradient=0.001,
                    img=None,
                    img_size=(800, 800),
                    dpi=(800, 800),
                    img_file_name='beziersample.bmp',
                    default_bg_color=(0, 0, 0, 0),
                    default_fg_color_fn = (lambda x: (255, 255, 0, 0))):
        if img == None:
            self.img = Image.new('RGBA', img_size, default_bg_color)
        else:
            self.img = img
        self.levels = levels
        self.gradient = gradient
        self.dpi = dpi
        self.img_file_name = img_file_name
        self.default_bg_color = default_bg_color
        self.default_fg_color_fn = default_fg_color_fn

    @staticmethod
    def find_ratio_point(p1, p2, r):
        return p1[0]+(p2[0]-p1[0])*r, p1[1]+(p2[1]-p1[1])*r

    def random_points(self):
        return [(random.randint(0, self.img.size[0]), random.randint(0,self.img.size[1])) for _ in range(0, self.levels)]

    def bezier(self):
        points_orig = self.random_points()
        color = self.default_fg_color_fn(None)
        for grad in numpy.arange(0, 1, self.gradient):
            points_array = points_orig.copy()
            for levels in range(self.levels, 1, -1):
                points_new = [self.find_ratio_point(points_array[i], points_array[i+1], grad) for i in range(0, levels-1)]
                if len(points_new) == 1:
                    x,y = int(points_new[0][0]), int(points_new[0][1])
                    if x >= 0 and x <= self.img.size[0]-1 and y >= 0 and y <= self.img.size[1]-1:
                        self.img.putpixel((x, y), color)
                else:
                    points_array = points_new

    def save(self):
        self.img.save(self.img_file_name, 'PNG', dpi=self.dpi)

    def show(self):
        self.img.show()

class RandomBezierSmall(RandomBezier):
    def __init__(self, levels=4, diff=20):
        default_fg_color_fn = lambda x: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        super().__init__(levels=4, default_fg_color_fn=default_fg_color_fn )
        self.diff = diff

    @staticmethod
    def limit_within_boundaries(val, min_allowed, max_allowed):
        return max(min(val, max_allowed), min_allowed)

    def random_points(self):
        points = [(random.randint(0, self.img.size[0]), random.randint(0, self.img.size[1]))]
        for _ in range(0, self.levels-1) :
            rand_x = random.randint(points[0][0]-self.diff, points[0][0]+self.diff)
            x = self.limit_within_boundaries(rand_x, 0, self.img.size[0])
            rand_y = random.randint(points[0][1]-self.diff, points[0][1]+self.diff)
            y = self.limit_within_boundaries(rand_y, 0, self.img.size[1]-1)
            points.append((x, y))
        return points


import argparse

import torch
import pickle
import dgcn
import numpy as np
import os
from tqdm import tqdm
import time
import logging

log = dgcn.utils.get_logger()

def setup_logging(log_file='log.txt'):
	"""
	Setup logging configuration
	"""
	logging.basicConfig(level=logging.INFO,
						format="%(asctime)s - %(levelname)s - %(message)s",
						datefmt="%Y-%m-%d %H:%M:%S",
						filename=log_file,
						filemode='w')

	console = logging.StreamHandler()
	console.setLevel(logging.INFO)
	formatter = logging.Formatter('%(message)s')
	console.setFormatter(formatter)
	logging.getLogger('').addHandler(console)

def main(args):
	dgcn.utils.set_seed(args.seed)

	# load data
	log.info("Loaded data.")
	with open('./Audio_IEMOCAP.pickle', 'rb') as handle:
		(train_audio, train_label, train_speaker, test_audio, test_label, test_speaker, maxlen, train_length, test_length) = pickle.load(handle)
	with open('./Text_IEMOCAP.pickle', 'rb') as handle:
		(train_text, train_label, train_speaker, test_text, test_label, test_speaker, maxlen, train_length, test_length) = pickle.load(handle)

	with open('./Knowledge_nrc_IEMOCAP.pickle','rb')as handle:
		(Concept_data_train,Concept_data_test,Anew_data_train,Anew_data_test)=pickle.load(handle)

	trainset = dgcn.Dataset(train_audio,train_text,train_label,train_speaker,train_length, Concept_data_train, Anew_data_train, args.batch_size)
	testset = dgcn.Dataset(test_audio,test_text,test_label, test_speaker,test_length, Concept_data_test, Anew_data_test, args.batch_size)

	log.debug("Building model...")

	model = dgcn.CONSKGCN(args).to(args.device)
	opt = dgcn.Optim(args.learning_rate, args.max_grad_value, args.weight_decay)
	opt.set_parameters(model.parameters(), args.optimizer)

	coach = dgcn.Coach(trainset, testset, model, opt,args) # Calculate f1 and loss

	# Train.
	log.info("Start training...")
	ret = coach.train()

	# Save.
	checkpoint = {
		"best_dev_f1": ret[0],
		"best_epoch": ret[1],
		"best_state": ret[2],
	}
	torch.save(checkpoint, model_file) # entire net

if __name__ == "__main__":

	save_path="./"
	setup_logging(os.path.join(save_path, 'log_test_ConSK-GCN_0.0001.txt'))

	parser = argparse.ArgumentParser(description="train.py")
	parser.add_argument("--data", type=str,default=False,
						help="Path to data")

	# Training parameters
	parser.add_argument("--from_begin", action="store_true", default=True,
						help="Training from begin.")
	parser.add_argument("--device", type=str, default="cuda",
						help="Computing device.")
	parser.add_argument("--epochs", default=60, type=int,
						help="Number of training epochs.")
	parser.add_argument("--batch_size", default=32, type=int,
						help="Batch size.")
	parser.add_argument("--optimizer", type=str, default="adam",
						choices=["sgd", "rmsprop", "adam"],
						help="Name of optimizer.")
	parser.add_argument("--learning_rate", type=float, default=0.0001,
						help="Learning rate.")
	parser.add_argument("--weight_decay", type=float, default=1e-8,
						help="Weight decay.")
	parser.add_argument("--max_grad_value", default=-1, type=float,
						help="""If the norm of the gradient vector exceeds this,
						normalize it to have the norm equal to max_grad_norm""")
	parser.add_argument("--drop_rate", type=float, default=0.5,
						help="Dropout rate.")

	# Model parameters
	parser.add_argument("--wp", type=int, default=10,
						help="Past context window size. Set wp to -1 to use all the past context.")
	parser.add_argument("--wf", type=int, default=10,
						help="Future context window size. Set wp to -1 to use all the future context.")
	parser.add_argument("--n_speakers", type=int, default=2,
						help="Number of speakers.")
	parser.add_argument("--hidden_size", type=int, default=100,
						help="Hidden size of two layer GCN.")
	parser.add_argument("--rnn1", type=str, default="lstm",
						choices=["lstm", "gru"], help="Type of RNN cell.")
	parser.add_argument("--rnn2", type=str, default="lstm",
						choices=["lstm", "gru"], help="Type of RNN cell.")
	parser.add_argument("--class_weight", action="store_true",
						help="Use class weights in nll loss.")
	parser.add_argument('-kernel-num', type=int, default=100, help='number of each kind of kernel')
	parser.add_argument('-kernel-sizes', type=str, default='3,4,5', help='comma-separated kernel size to use for convolution')
	parser.add_argument('-static', action='store_true', default=False, help='fix the embedding')
	# others
	parser.add_argument("--seed", type=int, default=24,
						help="Random seed.")
	args = parser.parse_args()
	log.debug(args)
	args.kernel_sizes = [int(k) for k in args.kernel_sizes.split(',')]
	main(args)


from typing import Optional, Callable
from functools import wraps, partial

from phidata.task.python_task import PythonTask, PythonTaskType
from phidata.workflow import Workflow
from phidata.utils.log import logger

def workflow(func: PythonTaskType) -> Callable[..., Workflow]:
    """Converts a function into a python workflow"""

    wraps(func)

    def decorator(
        *args,
        name: Optional[str] = None,
        task_id: Optional[str] = None,
        dag_id: Optional[str] = None,
        # create_db_engine_from_conn_id: bool = True,
        version: Optional[str] = None,
        enabled: bool = True,
        **kwargs,
    ) -> Workflow:
        # logger.info(f"workflow func: {func}")
        # logger.info(f"workflow kwargs: {kwargs}")
        task_name = name or func.__name__
        try:
            # create a partial function that can be called by the PythonTask
            entrypoint = partial(func, *args, **kwargs)
            python_task = PythonTask(
                name=task_name,
                entrypoint=entrypoint,
                task_id=task_id,
                dag_id=dag_id,
                # create_db_engine_from_conn_id=create_db_engine_from_conn_id,
                entrypoint_args=args,
                entrypoint_kwargs=kwargs,
                version=version,
                enabled=enabled,
            )
            python_wf = Workflow(
                name=task_name,
                tasks=[python_task],
                dag_id=dag_id,
            )
            return python_wf
        except Exception as e:
            logger.error(f"Could not create workflow: {task_name}")
            logger.error(e)
            raise

    return decorator

"""
@workflow
def simple_workflow(**kwargs) -> bool:
    logger.info(f"simple_workflow kwargs: {kwargs}")
    return True

sw = simple_workflow()
"""


#!/usr/bin/env python3.6

import json
import sys
import os
import datetime
from githubsearch import GithubSearch
from githubevents import GithubEvents
from githubcards import GithubCards
import tempfile

CURRENT_ROOT = os.path.abspath(os.path.dirname(__file__))
TMP_ISSUES_EVENTS_FILE = os.path.join(tempfile.gettempdir(), 'issue-events.json')
TMP_METRIC_FILE = os.path.join(tempfile.gettempdir(), 'metrics.json')
TMP_CARDS_SUMMARY_FILE = os.path.join(tempfile.gettempdir(), 'cards_summary.json')
TMP_WIP_CARDS_FILE = os.path.join(tempfile.gettempdir(), 'cards_wip.json')

def pretty_json(response_json):
    return json.dumps(response_json, sort_keys=True, indent=4)

def load_issues_events():
    with open(TMP_ISSUES_EVENTS_FILE, 'r') as read_file:
        issues_events = json.load(read_file)

    return issues_events

def write_metrics_to_file(file_name, data):
    file = open(file_name, 'w+')
    file.write(data)
    file.flush()
    file.close()

def main():
    file_path = os.path.join(CURRENT_ROOT, 'config', 'config.json')
    with open(file_path, 'r') as read_file:
        config = json.load(read_file)

    until_date = datetime.datetime.now()
    if len(sys.argv) > 1:
        until_date = datetime_object = datetime.datetime.strptime(sys.argv[1], '%Y-%m-%d')

    githubEvents = GithubEvents(config)

    githubCards = GithubCards(config, githubEvents)
    githubCards.loadProjectCards()

    githubSearch = GithubSearch(config, until_date, githubEvents)

    githubSearch.issues = githubSearch.getIssuesFromMonth()

    issuesEvents = githubEvents.getIssuesEvents(githubSearch.issues['items'])
    issuesEventsContent = str(pretty_json(issuesEvents))
    write_metrics_to_file(TMP_ISSUES_EVENTS_FILE, issuesEventsContent)

    githubEvents.issuesEvents = load_issues_events()

    result = []
    for week_date in githubSearch.weeks_date.values():
        result.append(githubSearch.getResultByWeek(week_date))

    write_metrics_to_file(TMP_METRIC_FILE, str(pretty_json(result)))

    result = []
    result = githubCards.getSummaryMetric()
    write_metrics_to_file(TMP_CARDS_SUMMARY_FILE, str(pretty_json(result)))

    result = []
    result = githubCards.getCardsWorkInProgress(githubSearch.issues)
    write_metrics_to_file(TMP_WIP_CARDS_FILE, str(pretty_json(result)))

if __name__ == "__main__":
    main()