from typing import Optional, Literal

import attrs
import numpy as np
import scipy.sparse as sp
from Recommenders.BaseRecommender import BaseRecommender
from Recommenders.Recommender_utils import check_matrix
from recsys_framework_extensions.data.io import DataIO
from recsys_framework_extensions.recommenders.base import SearchHyperParametersBaseRecommender
from recsys_framework_extensions.recommenders.mixins import MixinLoadModel
from recsys_framework_extensions.recommenders.rank import rank_data_by_row
from skopt.space import Categorical

T_SIGN = Literal[-1, 1]

@attrs.define(kw_only=True, frozen=True, slots=False)
class SearchHyperParametersFrequencyRecencyRecommender(SearchHyperParametersBaseRecommender):
    sign_frequency: Categorical = attrs.field(
        default=Categorical(
            categories=[-1, 1],
        )
    )
    sign_recency: Categorical = attrs.field(
        default=Categorical(
            categories=[-1, 1],
        )
    )

@attrs.define(kw_only=True, frozen=True, slots=False)
class SearchHyperParametersRecencyRecommender(SearchHyperParametersBaseRecommender):
    sign_recency: Categorical = attrs.field(
        default=Categorical(
            categories=[-1, 1],
        )
    )

class FrequencyRecencyRecommender(MixinLoadModel, BaseRecommender):
    RECOMMENDER_NAME = "FrequencyRecencyRecommender"

    def __init__(
        self,
        urm_train: sp.csr_matrix,
        uim_frequency: sp.csr_matrix,
        uim_timestamp: sp.csr_matrix,
        **kwargs,
    ):
        """
        Parameters
        ----------
        urm_train: csr_matrix
            A sparse matrix of shape (M, N), where M = #Users, and N = #Items. The content of this matrix is the latest
            recorded implicit interaction for the user-item pair (u,i), i.e., urm_train[u,i] = 1

        uim_frequency: csr_matrix
            A sparse matrix of shape (M, N), where M = #Users, and N = #Items. The content of this matrix is the
            frequency of impressions (number of times a given item has been impressed to a given user) for the
            user-item pair (u,i), i.e., uim_frequency[u,i] = frequency.

        uim_timestamp: csr_matrix
            A sparse matrix of shape (M, N), where M = #Users, and N = #Items. The content of this matrix is the
            latest recorded timestamp for the user-item pair (u,i), i.e., uim_timestamp[u,i] = timestamp.
        """
        super().__init__(
            URM_train=urm_train,
            verbose=True,
        )

        self._uim_frequency: sp.csr_matrix = uim_frequency.copy()
        self._uim_timestamp: sp.csr_matrix = uim_timestamp.copy()

        self._sign_frequency: T_SIGN = 1
        self._sign_recency: T_SIGN = 1
        self._sp_matrix_frequency_scores: sp.csr_matrix = sp.csr_matrix([], dtype=np.float32)
        self._sp_matrix_timestamp_scores: sp.csr_matrix = sp.csr_matrix([], dtype=np.float32)

    def fit(
        self,
        sign_frequency: T_SIGN,
        sign_recency: T_SIGN,
        **kwargs,
    ) -> None:

        sp_matrix_frequency_scores = sign_frequency * self._uim_frequency
        sp_matrix_timestamp_scores = sign_recency * self._uim_timestamp

        self._sign_frequency = sign_frequency
        self._sign_recency = sign_recency
        self._sp_matrix_frequency_scores = check_matrix(X=sp_matrix_frequency_scores, dtype=np.float32, format="csr")
        self._sp_matrix_timestamp_scores = check_matrix(X=sp_matrix_timestamp_scores, dtype=np.float32, format="csr")

    def save_model(
        self,
        folder_path: str,
        file_name: str = None
    ) -> None:
        if file_name is None:
            file_name = self.RECOMMENDER_NAME

        DataIO.s_save_data(
            folder_path=folder_path,
            file_name=file_name,
            data_dict_to_save={
                "_sign_frequency": self._sign_frequency,
                "_sign_recency": self._sign_recency,
                "_sp_matrix_frequency_scores": self._sp_matrix_frequency_scores,
                "_sp_matrix_timestamp_scores": self._sp_matrix_timestamp_scores,
            }
        )

    def load_model(
        self,
        folder_path: str,
        file_name: str = None,
    ) -> None:
        super().load_model(
            folder_path=folder_path,
            file_name=file_name,
        )

        assert hasattr(self, "_sign_frequency")
        assert hasattr(self, "_sign_recency")
        assert hasattr(self, "_sp_matrix_frequency_scores")
        assert hasattr(self, "_sp_matrix_timestamp_scores")

        self._sp_matrix_frequency_scores = check_matrix(X=self._sp_matrix_frequency_scores, format="csr", dtype=np.float32)
        self._sp_matrix_timestamp_scores = check_matrix(X=self._sp_matrix_timestamp_scores, format="csr", dtype=np.float32)

    def _compute_item_score(
        self,
        user_id_array: list[int],
        items_to_compute: list[int] = None
    ) -> np.ndarray:
        """
        Return
        ------
        np.ndarray
            A matrix with shape (M, N), where M = |user_id_array|, and N = #Items .
        """
        assert user_id_array is not None
        assert len(user_id_array) > 0

        num_score_users: int = len(user_id_array)
        num_score_items: int = self.URM_train.shape[1]

        arr_scores_timestamp = self._sp_matrix_timestamp_scores[user_id_array, :].toarray()
        arr_scores_frequency = self._sp_matrix_frequency_scores[user_id_array, :].toarray()

        assert arr_scores_frequency.shape == arr_scores_timestamp.shape
        assert num_score_items == arr_scores_timestamp.shape[1]

        item_scores = rank_data_by_row(
            keys=(arr_scores_timestamp, arr_scores_frequency)
        )

        # If we want to compute for only a certain group of items (`items_to_compute` not being None),
        # then we must have a mask that sets -INF to items outside this list.
        if items_to_compute is None:
            arr_mask_items: np.ndarray = np.ones_like(arr_scores_frequency, dtype=np.bool8)
        else:
            arr_mask_items = np.zeros_like(arr_scores_frequency, dtype=np.bool8)
            arr_mask_items[:, items_to_compute] = True

        item_scores = np.where(
            (arr_scores_frequency != 0) & (arr_scores_timestamp != 0) & arr_mask_items,
            item_scores,
            np.NINF,
        )

        assert item_scores.shape == (num_score_users, num_score_items)
        assert item_scores.shape == arr_scores_timestamp.shape
        assert item_scores.shape == arr_scores_frequency.shape

        return item_scores

class RecencyRecommender(MixinLoadModel, BaseRecommender):
    RECOMMENDER_NAME = "RecencyRecommender"

    def __init__(
        self,
        urm_train: sp.csr_matrix,
        uim_timestamp: sp.csr_matrix,
        **kwargs,
    ):
        """
        Parameters
        ----------
        urm_train: csr_matrix
            A sparse matrix of shape (M, N), where M = #Users, and N = #Items. The content of this matrix is the latest
            recorded implicit interaction for the user-item pair (u,i), i.e., urm_train[u,i] = 1

        uim_timestamp: csr_matrix
            A sparse matrix of shape (M, N), where M = #Users, and N = #Items. The content of this matrix is the
            latest recorded timestamp for the user-item pair (u,i), i.e., uim_timestamp[u,i] = timestamp.
        """
        super().__init__(
            URM_train=urm_train,
            verbose=True,
        )

        self._uim_timestamp: sp.csr_matrix = uim_timestamp.copy()

        self._sign_recency: T_SIGN = 1
        self._sp_matrix_timestamp_scores: sp.csr_matrix = sp.csr_matrix([], dtype=np.float32)

    def fit(
        self,
        sign_recency: T_SIGN,
        **kwargs,
    ) -> None:

        sp_matrix_timestamp_scores = sign_recency * self._uim_timestamp

        self._sign_recency = sign_recency
        self._sp_matrix_timestamp_scores = check_matrix(X=sp_matrix_timestamp_scores, dtype=np.float32, format="csr")

    def _compute_item_score(
        self,
        user_id_array: list[int],
        items_to_compute: Optional[list[int]] = None,
    ) -> np.ndarray:
        """
        Computes the preference scores of users toward items.

        Notes
        -----
            :class:`BaseRecommender` ranks items in a further step by their score. Items with the highest scores are
            positioned first in the list.

        Return
        ------
        np.ndarray
            A matrix with shape (M, N), where M = |user_id_array|, and N = #Items .
        """
        assert user_id_array is not None
        assert len(user_id_array) > 0

        num_score_users: int = len(user_id_array)
        num_score_items: int = self.URM_train.shape[1]

        arr_timestamp_users: np.ndarray = self._sp_matrix_timestamp_scores[user_id_array, :].toarray()
        if items_to_compute is None:
            arr_mask_items: np.ndarray = np.ones_like(arr_timestamp_users, dtype=np.bool8)
        else:
            arr_mask_items = np.zeros_like(arr_timestamp_users, dtype=np.bool8)
            arr_mask_items[:, items_to_compute] = True

        item_scores = np.where(
            (arr_timestamp_users != 0) & arr_mask_items,
            arr_timestamp_users,
            np.NINF,
        )

        assert item_scores.shape == (num_score_users, num_score_items)

        return item_scores

    def save_model(
        self,
        folder_path: str,
        file_name: str = None
    ) -> None:
        if file_name is None:
            file_name = self.RECOMMENDER_NAME

        DataIO.s_save_data(
            folder_path=folder_path,
            file_name=file_name,
            data_dict_to_save={
                "_sign_recency": self._sign_recency,
                "_sp_matrix_timestamp_scores": self._sp_matrix_timestamp_scores,
            }
        )

    def load_model(
        self,
        folder_path: str,
        file_name: str = None,
    ) -> None:
        super().load_model(
            folder_path=folder_path,
            file_name=file_name,
        )

        assert hasattr(self, "_sign_frequency")
        assert hasattr(self, "_sp_matrix_timestamp_scores")

        self._sp_matrix_timestamp_scores = check_matrix(X=self._sp_matrix_timestamp_scores, format="csr", dtype=np.float32)


import pysrt

# Function to load srt file
def load(fname):
    # Error check
    return pysrt.open(fname)

# Compute time diff between two time objects
def compute_diff(t1, t2):
    tms1 = t1.hours*t1.HOURS_RATIO+t1.minutes*t1.MINUTES_RATIO+t1.seconds*t1.SECONDS_RATIO + t1.milliseconds
    tms2 = t2.hours*t2.HOURS_RATIO+t2.minutes*t2.MINUTES_RATIO+t2.seconds*t2.SECONDS_RATIO + t2.milliseconds
    return abs(tms1 - tms2)/t1.SECONDS_RATIO

filename = 'deneme.srt'
subs = load(filename)
for i in range(0, len(subs)):
    if i==0:
        print(subs[i].text)
        tprev = subs[i].end
    else:
        tcurr = subs[i].start
        tdiff = compute_diff(tprev, tcurr)
        print("[[addpause %.3f]]" % tdiff)
        print(subs[i].text)
        tprev = subs[i].end


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from googlecloudsdk.api_lib.compute import constants
from googlecloudsdk.api_lib.compute import instance_utils
from googlecloudsdk.api_lib.compute import path_simplifier
from googlecloudsdk.core.resource import resource_transform
import six

def TransformFirewallRule(r, undefined=''):
  """Returns a compact string describing a firewall rule.

  The compact string is a comma-separated list of PROTOCOL:PORT_RANGE items.
  If a particular protocol has no port ranges then only the protocol is listed.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A compact string describing the firewall rule in r.
  """
  protocol = resource_transform.GetKeyValue(r, 'IPProtocol', None)
  if protocol is None:
    return undefined
  rule = []
  port_ranges = resource_transform.GetKeyValue(r, 'ports', None)
  try:
    for port_range in port_ranges:
      rule.append('{0}:{1}'.format(protocol, port_range))
  except TypeError:
    rule.append(protocol)
  return ','.join(rule)

def TransformOrganizationFirewallRule(rule, undefined=''):
  """Returns a compact string describing an organization firewall rule.

  The compact string is a comma-separated list of PROTOCOL:PORT_RANGE items.
  If a particular protocol has no port ranges then only the protocol is listed.

  Args:
    rule: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A compact string describing the organizatin firewall rule in the rule.
  """
  protocol = resource_transform.GetKeyValue(rule, 'ipProtocol', None)
  if protocol is None:
    return undefined
  result = []
  port_ranges = resource_transform.GetKeyValue(rule, 'ports', None)
  try:
    for port_range in port_ranges:
      result.append('{0}:{1}'.format(protocol, port_range))
  except TypeError:
    result.append(protocol)
  return ','.join(result)

def TransformImageAlias(r, undefined=''):
  """Returns a comma-separated list of alias names for an image.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A comma-separated list of alias names for the image in r.
  """
  name = resource_transform.GetKeyValue(r, 'name', None)
  if name is None:
    return undefined
  project = resource_transform.TransformScope(
      resource_transform.GetKeyValue(r, 'selfLink', ''),
      'projects').split('/')[0]
  aliases = [alias for alias, value in constants.IMAGE_ALIASES.items()
             if name.startswith(value.name_prefix)
             and value.project == project]
  return ','.join(aliases)

def TransformLocation(r, undefined=''):
  """Return the region or zone name.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The region or zone name.
  """
  for scope in ('zone', 'region'):
    location = resource_transform.GetKeyValue(r, scope, None)
    if location:
      return resource_transform.TransformBaseName(location, undefined)
  return undefined

def TransformLocationScope(r, undefined=''):
  """Return the location scope name, either region or zone.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The location scope name, either region or zone.
  """
  for scope in ('zone', 'region'):
    location = resource_transform.GetKeyValue(r, scope, None)
    if location:
      return scope
  return undefined

def TransformMachineType(r, undefined=''):
  """Return the formatted name for a machine type.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The formatted name for a machine type.
  """
  if not isinstance(r, six.string_types):
    return undefined
  custom_family, custom_cpu, custom_ram = \
    instance_utils.GetCpuRamVmFamilyFromCustomName(r)
  if not custom_family or not custom_cpu or not custom_ram:
    return r
  # Restricting output to 2 decimal places
  custom_ram_gb = '{0:.2f}'.format(float(custom_ram) / (2**10))
  return 'custom ({0}, {1} vCPU, {2} GiB)'.format(custom_family, custom_cpu,
                                                  custom_ram_gb)

def TransformNextMaintenance(r, undefined=''):
  """Returns the timestamps of the next scheduled maintenance.

  All timestamps are assumed to be ISO strings in the same timezone.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The timestamps of the next scheduled maintenance or undefined.
  """
  if not r:
    return undefined
  next_event = min(r, key=lambda x: x.get('beginTime', None))
  if next_event is None:
    return undefined
  begin_time = next_event.get('beginTime', None)
  if begin_time is None:
    return undefined
  end_time = next_event.get('endTime', None)
  if end_time is None:
    return undefined
  return '{0}--{1}'.format(begin_time, end_time)

def TransformOperationHttpStatus(r, undefined=''):
  """Returns the HTTP response code of an operation.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if there is no response code.

  Returns:
    The HTTP response code of the operation in r.
  """
  if resource_transform.GetKeyValue(r, 'status', None) == 'DONE':
    return (resource_transform.GetKeyValue(r, 'httpErrorStatusCode', None) or
            200)  # httplib.OK
  return undefined

def TransformProject(r, undefined=''):
  """Returns a project name from a selfLink.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A project name for selfLink from r.
  """
  project = resource_transform.TransformScope(
      resource_transform.GetKeyValue(r, 'selfLink', ''),
      'projects').split('/')[0]
  return project or undefined

def TransformName(r, undefined=''):
  """Returns a resource name from an URI.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A project name for selfLink from r.
  """
  if r:
    try:
      return r.split('/')[-1]
    except AttributeError:
      pass
  return undefined

def TransformQuota(r, undefined=''):
  """Formats a quota as usage/limit.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The quota in r as usage/limit.
  """
  usage = resource_transform.GetKeyValue(r, 'usage', None)
  if usage is None:
    return undefined
  limit = resource_transform.GetKeyValue(r, 'limit', None)
  if limit is None:
    return undefined
  try:
    if usage == int(usage) and limit == int(limit):
      return '{0}/{1}'.format(int(usage), int(limit))
    return '{0:.2f}/{1:.2f}'.format(usage, limit)
  except (TypeError, ValueError):
    pass
  return undefined

def TransformScopedSuffixes(uris, undefined=''):
  """Get just the scoped part of the object the uri refers to."""

  if uris:
    try:
      return sorted([path_simplifier.ScopedSuffix(uri) for uri in uris])
    except TypeError:
      pass
  return undefined

def TransformStatus(r, undefined=''):
  """Returns the machine status with deprecation information if applicable.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The machine status in r with deprecation information if applicable.
  """
  status = resource_transform.GetKeyValue(r, 'status', None)
  deprecated = resource_transform.GetKeyValue(r, 'deprecated', '')
  if deprecated:
    return '{0} ({1})'.format(status, deprecated.get('state', ''))
  return status or undefined

def TransformVpnTunnelGateway(vpn_tunnel, undefined=''):
  """Returns the gateway for the specified VPN tunnel resource if applicable.

  Args:
    vpn_tunnel: JSON-serializable object of a VPN tunnel.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    The VPN gateway information in the VPN tunnel object.
  """
  target_vpn_gateway = resource_transform.GetKeyValue(vpn_tunnel,
                                                      'targetVpnGateway', None)
  if target_vpn_gateway is not None:
    return target_vpn_gateway

  vpn_gateway = resource_transform.GetKeyValue(vpn_tunnel, 'vpnGateway', None)
  if vpn_gateway is not None:
    return vpn_gateway

  return undefined

def TransformZone(r, undefined=''):
  """Returns a zone name from a selfLink.

  Args:
    r: JSON-serializable object.
    undefined: Returns this value if the resource cannot be formatted.

  Returns:
    A zone name for selfLink from r.
  """
  project = resource_transform.TransformScope(
      resource_transform.GetKeyValue(r, 'selfLink', ''), 'zones').split('/')[0]
  return project or undefined

def TransformTypeSuffix(uri, undefined=''):
  """Get the type and the name of the object the uri refers to."""

  try:
    # Since the path is assumed valid, we can just take the last two pieces.
    return '/'.join(uri.split('/')[-2:]) or undefined
  except (AttributeError, IndexError, TypeError):
    pass
  return undefined

_TRANSFORMS = {
    'firewall_rule': TransformFirewallRule,
    'org_firewall_rule': TransformOrganizationFirewallRule,
    'image_alias': TransformImageAlias,
    'location': TransformLocation,
    'location_scope': TransformLocationScope,
    'machine_type': TransformMachineType,
    'next_maintenance': TransformNextMaintenance,
    'name': TransformName,
    'operation_http_status': TransformOperationHttpStatus,
    'project': TransformProject,
    'quota': TransformQuota,
    'scoped_suffixes': TransformScopedSuffixes,
    'status': TransformStatus,
    'type_suffix': TransformTypeSuffix,
    'vpn_tunnel_gateway': TransformVpnTunnelGateway,
    'zone': TransformZone,
}

def GetTransforms():
  """Returns the compute specific resource transform symbol table."""
  return _TRANSFORMS


import cv2
import numpy as np

cap = cv2.VideoCapture(0)

# config save video
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# 25: fps(frame per seconds)
out = cv2.VideoWriter("image/02.save_to_video.avi", fourcc, 25, (640, 360))

while True:
    ret, frame = cap.read()

    if not ret:
        break

    # 1: vertical flip by frame
    # 0: horizontal flip by frame
    frame_output = cv2.flip(frame, 0)
    cv2.imshow("input", frame)
    cv2.imshow("output", frame_output)

    # save output
    out.write(frame_output)

    # wait bread
    key = cv2.waitKey(1)
    if key == 27:
        break
# destroy
out.release()
cap.release()
cv2.destroyAllWindows()


import threading, requests

class MyThread(threading.Thread):
    def __init__(self, data=None):
        threading.Thread.__init__(self)
        self.add = data

    # 重写run()方法
    def run(self):
        for arc in self.add["data"]:
            # 调用 getName() 方法获取当前执行该程序的线程名
            # print(threading.current_thread().getName() + " " + str(arc))
            self.add_gift(uid=self.add["uid"], prop_id=arc[0], pricing_id=arc[1], nums=self.add["nums"],
                          business=self.add["business"])

    def add_gift(self, uid, prop_id, pricing_id, nums, business):
        print(
            "uid:{}, prop_id:{}, pricing_id:{}, nums:{}, business:{}".format(uid, prop_id, pricing_id, nums, business))
        url = "http://turnover-bg-test.yy.com/props/addProps/" + str(business)
        data = {
            "propId": prop_id,
            "pricingId": pricing_id,
            "uid": str(uid),
            "nums": nums,
            "countryCode": "cn"
        }
        response = requests.get(url=url, params=data)
        print("response.url:{}".format(response.url))
        print("response.text:{}".format(response.text))

    def use_threading(self, data):
        mythread = MyThread(data)
        # 启动子线程
        mythread.start()
        # 主线程执行此循环
        for i in range(len(data["data"])):
            print("主线程执行此循环:threading.current_thread().getName():{}".format(threading.current_thread().getName()))


#!/usr/local/bin/python3
__author__ = 'apaeffgen'
# -*- coding: utf-8 -*-

    # This file is part of Panconvert.
    #
    # Panconvert is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # Panconvert is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with Panconvert.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtCore import QSettings
from PyQt5.QtCore import QPoint, QSize
from source.gui.panconvert_dialog_batch import Ui_DialogBatch
from distutils.util import strtobool as str2bool
import platform

global openfiles, batch_open_path

def strtobool(input):
    """
        safe strtobool : if input is a boolean
        it return the input
    """
    if isinstance(input,bool):
        return input
    return str2bool(input)

class BatchDialog(QtWidgets.QDialog):

    def __init__(self, parent=None):

        global batch_open_path, openfile, batch_open_path_output

        QtWidgets.QWidget.__init__(self, parent)
        self.ui = Ui_DialogBatch()
        self.ui.setupUi(self)
        self.ui.ButtonSave.clicked.connect(self.batch_settings)
        self.ui.ButtonCancel.clicked.connect(self.closeEvent)
        self.ui.Button_Open_Path.clicked.connect(self.directory_dialog)
        self.ui.Button_Open_Path_Output.clicked.connect(self.directory_dialog_Output)

        #Initialize Settings
        batch_settings = QSettings('Pandoc', 'PanConvert')
        settings = QSettings('Pandoc', 'PanConvert')

        self.resize(settings.value("Batch_size", QSize(270, 225)))
        self.move(settings.value("Batch_pos", QPoint(50, 50)))

        # Path Settings
        batch_open_path = batch_settings.value('batch_open_path')
        self.ui.OpenPath.insert(batch_open_path)
        batch_open_path_output = batch_settings.value('batch_open_path_output')
        self.ui.OpenPath_Output.insert(batch_open_path_output)

        # Filter Settings
        batch_convert_filter = batch_settings.value('batch_convert_filter')
        self.ui.Filter.insert(batch_convert_filter)

        #Parameter Settings

        parameterBatchconvertDirectory = batch_settings.value('batch_convert_directory', True)
        parameterBatchconvertFiles = batch_settings.value('batch_convert_files', False)
        parameterBatchconvertRecursive = batch_settings.value('batch_convert_recursive', True)

        if batch_settings.value('batch_convert_directory') is not None:
            if platform.system() == 'Darwin':
                self.ui.ParameterBatchconvertDirectory.setChecked(parameterBatchconvertDirectory)
                self.ui.ParameterBatchconvertFiles.setChecked(parameterBatchconvertFiles)
                self.ui.ParameterBatchconvertRecursive.setChecked(parameterBatchconvertRecursive)
            else:
                self.ui.ParameterBatchconvertDirectory.setChecked(strtobool(parameterBatchconvertDirectory))
                self.ui.ParameterBatchconvertFiles.setChecked(strtobool(parameterBatchconvertFiles))
                self.ui.ParameterBatchconvertRecursive.setChecked(strtobool(parameterBatchconvertRecursive))

    def closeEvent(self, event):

        settings = QSettings('Pandoc', 'PanConvert')
        Dialog_Size = settings.value('Dialog_Size')
        if Dialog_Size is True or Dialog_Size == 'true':
            settings.setValue("Batch_size", self.size())
            settings.setValue("Batch_pos", self.pos())

        settings.sync()
        settings.status()
        BatchDialog.close(self)

    def batch_settings(self):
        global batch_open_path, openfiles, batch_open_path_output

        batch_settings = QSettings('Pandoc', 'PanConvert')
        batch_settings.setValue('batch_convert_directory', self.ui.ParameterBatchconvertDirectory.isChecked())
        batch_settings.setValue('batch_convert_files', self.ui.ParameterBatchconvertFiles.isChecked())
        batch_settings.setValue('batch_convert_recursive', self.ui.ParameterBatchconvertRecursive.isChecked())
        batch_settings.setValue('batch_open_path', self.ui.OpenPath.text())
        batch_settings.setValue('batch_open_path_output', self.ui.OpenPath_Output.text())
        batch_settings.setValue('batch_convert_filter', self.ui.Filter.text())
        batch_settings.sync()
        batch_settings.status()

        BatchDialog.close(self)

    def directory_dialog(self):

        global data, openfiles, batch_open_path
        self.ui.OpenPath.clear()

        fd = QtWidgets.QFileDialog(self)

        if batch_open_path == '':
            fd.setDirectory(QtCore.QDir.homePath())
        else:
            fd.setDirectory(batch_open_path)

        batch_directory = fd.getExistingDirectory()
        self.ui.OpenPath.insert(batch_directory)

    def directory_dialog_Output(self):

        global data, openfiles, batch_open_path_output
        self.ui.OpenPath_Output.clear()

        fd = QtWidgets.QFileDialog(self)

        if batch_open_path_output == '':
            fd.setDirectory(QtCore.QDir.homePath())
        else:
            fd.setDirectory(batch_open_path_output)

        batch_directory = fd.getExistingDirectory()
        self.ui.OpenPath_Output.insert(batch_directory)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    myapp = BatchDialog()
    myapp.show()
    myapp.exec_()


#!/usr/bin/python3

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from typing import List, Tuple
import config as CONF
import sys

"""
Reads datapts from the file data.pts and plots them in an interactive 3D plot.
"""

def read_datapts(file="data.pts"):
    with open(file, 'r') as f:
        lines = f.readlines()
        n = len(lines)
        x = np.ndarray(n)
        y = np.ndarray(n)
        z = np.ndarray(n)
        for n, line in enumerate(lines):
            x[n], y[n], z[n] = line.strip().split("\t")
    return (x, y, z)

if __name__ == "__main__":
    data_x, data_y, data_z = read_datapts("data.pts")

    fig = plt.figure(figsize=(12, 8))
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(data_x, data_y, data_z, label="datapoints",
               marker=',', s=1) #,
               #linewidths=0.1, edgecolors='b')

    # plot corners of test domain
    domain_x = [0,  CONF.Lx, CONF.Lx,        0, 0]
    domain_y = [0,        0, CONF.Ly,  CONF.Ly, 0]
    domain_z = [0,        0,       0,        0, 0]
    ax.plot3D(domain_x, domain_y, domain_z, c='red')
    domain_x = [0, CONF.Lx, CONF.Lx,       0,       0]
    domain_y = [0,       0, CONF.Ly, CONF.Ly,       0]
    domain_z = [CONF.Lz, CONF.Lz, CONF.Lz, CONF.Lz, CONF.Lz]
    ax.plot3D(domain_x, domain_y, domain_z, c='red', label="domain bounds")

    ax.set_xlim((-CONF.Lx/2, CONF.Lx*3/2))
    ax.set_ylim((-CONF.Ly/4, CONF.Ly*3/4))
    ax.set_zlim((0, CONF.Lz*2))
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    ax.legend()

    fig.tight_layout()

    if len(sys.argv) > 1:
        plt.suptitle(" ".join([str(arg) for arg in sys.argv[1:]]))
    else:
        print("You may add a suptitle by passing arguments.")
    plt.show()


# https://realpython.com/pipenv-guide/#pipenv-introduction
# 1. Install virtual environment if not installed
# pip install pipenv
# 2. Switch to virtual environment
# pipenv shell
# 3. Install pytest if not installed
# pipenv install pytest
# 4. Run test
# pytest -v
#       - only works when test file start with test_ or end with _test
#         Report in verbose mode
# pytest -q
#       - Run in quiet mode (can be helpful when running hundreds or thousands of tests at once).
# pytest -s
#       - Don't capture console output (show print statements on the console).
# pytest --ignore
#       - Ignore the specified path when discovering tests.
# pytest --maxfail
#       - Stop after the specified number of failures.
# pytest "testname"
#       - specifies the module name to run only the tests in that module
# DirectoryName/
#       - runs any tests found in the specified directory
# -k "expression"
#       - matches tests found that match the evaluatable expression in the string.
#         The string values can include module, class and function names
#         (i.e. "TestClass and TestFunction").
# -m "expression"
#       - matches tests found that have a "pytest.mark" decorator that matches the
#       specified expression.
# 5. Deactivate virtual environment
# deactivate
# exit
# 6. Show dependency graph
# pipenv graph
import os
import pytest
from pytest import raises
from unittest.mock import MagicMock
from Checkout import Checkout

lineCnt = 0

@pytest.fixture()
def checkout():
    checkout = Checkout()
    checkout.addItemPrice("a", 1)
    checkout.addItemPrice("b", 2)
    return checkout

def test_canCalculateTotal(checkout):
    checkout.addItem("a")
    assert checkout.calculateTotal() == 1

def test_getCorrectTotalWithMultipleItems(checkout):
    checkout.addItem("a")
    checkout.addItem("b")
    assert checkout.calculateTotal() == 3

def test_canAddDiscountRule(checkout):
    checkout.addDiscount("a", 3, 2)

def test_canApplyDiscountRule(checkout):
    checkout.addDiscount("a", 3, 2)
    checkout.addItem("a")
    checkout.addItem("a")
    checkout.addItem("a")
    assert checkout.calculateTotal() == 2

def test_exceptionWithBadItem(checkout):
    with pytest.raises(Exception):
        checkout.addItem("c")

def test_verifyReadPricesFile(checkout, monkeypatch):
    mock_file = MagicMock()
    mock_file.__enter__.return_value.__iter__.return_value = ["c 3"]
    mock_open = MagicMock(return_value = mock_file)
    monkeypatch.setattr("builtins.open", mock_open)
    mock_exists = MagicMock(return_value=True)
    monkeypatch.setattr("os.path.exists", mock_exists)
    checkout.readPricesFile("testfile")
    checkout.addItem("c")
    result = checkout.calculateTotal()
    mock_open.assert_called_once_with("testfile")
    assert result == 3

def test_verifyReadFileException(checkout, monkeypatch):
    mock_file = MagicMock()
    mock_file.__enter__.return_value.__iter__.return_value = ["c 3"]
    mock_open = MagicMock(return_value=mock_file)
    monkeypatch.setattr("builtins.open", mock_open)
    mock_exists = MagicMock(return_value=False)
    monkeypatch.setattr("os.path.exists", mock_exists)
    with raises(Exception):
        checkout.readPricesFile("testfile")


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

# Python program to compute and visualize the
# histogram of Blue channel of image

# importing libraries
import sys, time, os, datetime, glob
import cv2
import argparse
import numpy as np
from matplotlib import pyplot as plt

from platform import python_version

print(f"(Sys version) :|: {sys.version} :|:")
os.system("which python")
print(f"(Python version) :#: {python_version()} :#:")

# reading the input image
img = cv2.imread('mountain.png')
#img = cv2.imread('dark_tones.jpg')

img_blue = img[:,:,2]
img_green = img[:,:,1]
img_red = img[:,:,0]

# split the image into its BGR components
(B, G, R) = cv2.split(img)
# find the maximum pixel intensity values for each
# (x, y)-coordinate,, then set all pixel values less
# than M to zero
M = np.maximum(np.maximum(R, G), B)

print(B.min(),B.max())
print(G.min(),G.max())
print(R.min(),R.max())

print(M.min(),M.max())

R[R < M] = 0
G[G < M] = 0
B[B < M] = 0

# print(R)
# print(G)
# print(B)
# merge the channels back together and return the image
mergeimg=cv2.merge([B, G, R])

# print(max(img_blue))
# print(min(img_blue))
# print(max(img_green))
# print(min(img_green))
# print(max(img_red))
# print(min(img_red))

# plot the above computed histogram
plt.imshow(mergeimg)
plt.title('Image Histogram For Blue Channel GFG')
plt.show()

# computing the histogram of the blue channel of the image
hist = cv2.calcHist([img],[0],None,[256],[0,256])

print(hist.min(),hist.max())

# np.savetxt(time.strftime("%Y%m%d_%H%M%S")+"_Blue.csv", np.vstack((hist)).T, delimiter=', ')
# np.savetxt(time.strftime("%Y%m%d_%H%M%S")+"_Blue.csv", np.vstack((hist)), delimiter=', ')
np.savetxt("mountain_Blue.csv", np.vstack((hist)), delimiter=', ')

# plot the above computed histogram
plt.plot(hist, color='b')
plt.title('Image Histogram For Blue Channel GFG')
plt.show()

# reading the input image
img = cv2.imread('mountain.png')
#img = cv2.imread('dark_tones.jpg')

# computing the histogram of the green channel of the image
hist = cv2.calcHist([img],[1],None,[256],[0,256])

# plot the above computed histogram
plt.plot(hist, color='g')
plt.title('Image Histogram For Green Channel GFG')
plt.show()

# reading the input image
img = cv2.imread('mountain.png')
#img = cv2.imread('dark_tones.jpg')

# computing the histogram of the Red channel of the image
hist = cv2.calcHist([img],[2],None,[256],[0,256])

# plot the above computed histogram
plt.plot(hist, color='r')
plt.title('Image Histogram For Red Channel GFG')
plt.show()

# reading the input image
img = cv2.imread('mountain.png')
#img = cv2.imread('dark_tones.jpg')

# define colors to plot the histograms
colors = ('b','g','r')

# compute and plot the image histograms
for i,color in enumerate(colors):
	hist = cv2.calcHist([img],[i],None,[256],[0,256]) # bit depth to 8-bit "BIT DEPTH"
	plt.plot(hist,color = color) # bit depth to 8-bit "BIT DEPTH"
plt.title('Image Histogram GFG')
plt.show()

print(type(img))
print(img.shape)
print(type(img.shape))

h, w, c = img.shape
print('width:  ', w)
print('height: ', h)
print('channel:', c)

h, w, _ = img.shape
print('width: ', w)
print('height:', h)

print('width: ', img.shape[1])
print('height:', img.shape[0])

print(img.shape[1::-1])

print(len(hist))
print(len(color))

image = cv2.imread('dark_tones.jpg')

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
plt.plot(histogram, color='k')
plt.show()

image = cv2.imread('dark_tones.jpg')

for i, col in enumerate(['b', 'g', 'r']):
    hist = cv2.calcHist([image], [i], None, [256], [0, 256])
    plt.plot(hist, color = col)
    plt.xlim([0, 256])

plt.show()

w=cv2.imread('mountain.png',1)
cv2.imshow('image',w)
b,g,r = cv2.split(w)
height = np.size(w, 0)
width = np.size(w, 1)
bw = np.zeros((height,width))

# for i in range(1,height):
#     for j in range(1,width):
#         if(b[i,j]<g[i,j] and r[i,j]<g[i,j] and g[i,j]>125):
#             bw[i,j]=1
# cv2.imshow('Black and White image',bw)
# kernel = np.ones((5,5),np.uint8)
# bw= cv2.erode(bw,kernel,iterations = 4)
# cv2.imshow('Eroded image',bw)
# bw = cv2.dilate(bw,kernel,iterations = 3)
# cv2.imshow('Final Image',bw)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import torchvision.models as models
from model.utils.config import cfg
import cv2
import pdb
import random

def save_net(fname, net):
    import h5py
    h5f = h5py.File(fname, mode='w')
    for k, v in net.state_dict().items():
        h5f.create_dataset(k, data=v.cpu().numpy())

def load_net(fname, net):
    import h5py
    h5f = h5py.File(fname, mode='r')
    for k, v in net.state_dict().items():
        param = torch.from_numpy(np.asarray(h5f[k]))
        v.copy_(param)

def weights_normal_init(model, dev=0.01):
    if isinstance(model, list):
        for m in model:
            weights_normal_init(m, dev)
    else:
        for m in model.modules():
            if isinstance(m, nn.Conv2d):
                m.weight.data.normal_(0.0, dev)
            elif isinstance(m, nn.Linear):
                m.weight.data.normal_(0.0, dev)

def clip_gradient(optimizer, clip_norm):
    """Computes a gradient clipping coefficient based on gradient norm."""
    combined_grads = optimizer.get_optimizer_combined_grads()[0]
    total_norm = torch.sqrt(torch.sum(torch.norm(combined_grads.detach()) ** 2)).item()

    norm = (clip_norm / max(total_norm, clip_norm))
    combined_grads.mul_(norm)

def vis_detections(im, class_name, dets, thresh=0.8):
    """Visual debugging of detections."""
    for i in range(np.minimum(10, dets.shape[0])):
        bbox = tuple(int(np.round(x)) for x in dets[i, :4])
        score = dets[i, -1]
        if score > thresh:
            cv2.rectangle(im, bbox[0:2], bbox[2:4], (0, 204, 0), 2)
            cv2.putText(im, '%s: %.3f' % (class_name, score), (bbox[0], bbox[1] + 15), cv2.FONT_HERSHEY_PLAIN,
                        1.0, (0, 0, 255), thickness=1)
    return im

def adjust_learning_rate(optimizer, decay=0.1):
    """Sets the learning rate to the initial LR decayed by 0.5 every 20 epochs"""
    for param_group in optimizer.param_groups:
        param_group['lr'] = decay * param_group['lr']

def save_checkpoint(state, filename):
    torch.save(state, filename)

def _smooth_l1_loss(bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights, sigma=1.0, dim=[1]):

    sigma_2 = sigma ** 2
    box_diff = bbox_pred - bbox_targets
    in_box_diff = bbox_inside_weights * box_diff
    abs_in_box_diff = torch.abs(in_box_diff)
    smoothL1_sign = (abs_in_box_diff < 1. / sigma_2).detach().float()
    in_loss_box = torch.pow(in_box_diff, 2) * (sigma_2 / 2.) * smoothL1_sign \
                  + (abs_in_box_diff - (0.5 / sigma_2)) * (1. - smoothL1_sign)
    out_loss_box = bbox_outside_weights * in_loss_box
    loss_box = out_loss_box
    for i in sorted(dim, reverse=True):
      loss_box = loss_box.sum(i)
    loss_box = loss_box.mean()
    return loss_box

def _crop_pool_layer(bottom, rois, max_pool=True):
    # code modified from
    # https://github.com/ruotianluo/pytorch-faster-rcnn
    # implement it using stn
    # box to affine
    # input (x1,y1,x2,y2)
    """
    [  x2-x1             x1 + x2 - W + 1  ]
    [  -----      0      ---------------  ]
    [  W - 1                  W - 1       ]
    [                                     ]
    [           y2-y1    y1 + y2 - H + 1  ]
    [    0      -----    ---------------  ]
    [           H - 1         H - 1      ]
    """
    rois = rois.detach()
    batch_size = bottom.size(0)
    D = bottom.size(1)
    H = bottom.size(2)
    W = bottom.size(3)
    roi_per_batch = rois.size(0) / batch_size
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = bottom.size(2)
    width = bottom.size(3)

    # affine theta
    zero = Variable(rois.data.new(rois.size(0), 1).zero_())
    theta = torch.cat([\
      (x2 - x1) / (width - 1),
      zero,
      (x1 + x2 - width + 1) / (width - 1),
      zero,
      (y2 - y1) / (height - 1),
      (y1 + y2 - height + 1) / (height - 1)], 1).view(-1, 2, 3)

    if max_pool:
      pre_pool_size = cfg.POOLING_SIZE * 2
      grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, pre_pool_size, pre_pool_size)))
      bottom = bottom.view(1, batch_size, D, H, W).contiguous().expand(roi_per_batch, batch_size, D, H, W)\
                                                                .contiguous().view(-1, D, H, W)
      crops = F.grid_sample(bottom, grid)
      crops = F.max_pool2d(crops, 2, 2)
    else:
      grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, cfg.POOLING_SIZE, cfg.POOLING_SIZE)))
      bottom = bottom.view(1, batch_size, D, H, W).contiguous().expand(roi_per_batch, batch_size, D, H, W)\
                                                                .contiguous().view(-1, D, H, W)
      crops = F.grid_sample(bottom, grid)

    return crops, grid

def _affine_grid_gen(rois, input_size, grid_size):

    rois = rois.detach()
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = input_size[0]
    width = input_size[1]

    zero = Variable(rois.data.new(rois.size(0), 1).zero_())
    theta = torch.cat([\
      (x2 - x1) / (width - 1),
      zero,
      (x1 + x2 - width + 1) / (width - 1),
      zero,
      (y2 - y1) / (height - 1),
      (y1 + y2 - height + 1) / (height - 1)], 1).view(-1, 2, 3)

    grid = F.affine_grid(theta, torch.Size((rois.size(0), 1, grid_size, grid_size)))

    return grid

def _affine_theta(rois, input_size):

    rois = rois.detach()
    x1 = rois[:, 1::4] / 16.0
    y1 = rois[:, 2::4] / 16.0
    x2 = rois[:, 3::4] / 16.0
    y2 = rois[:, 4::4] / 16.0

    height = input_size[0]
    width = input_size[1]

    zero = Variable(rois.data.new(rois.size(0), 1).zero_())

    # theta = torch.cat([\
    #   (x2 - x1) / (width - 1),
    #   zero,
    #   (x1 + x2 - width + 1) / (width - 1),
    #   zero,
    #   (y2 - y1) / (height - 1),
    #   (y1 + y2 - height + 1) / (height - 1)], 1).view(-1, 2, 3)

    theta = torch.cat([\
      (y2 - y1) / (height - 1),
      zero,
      (y1 + y2 - height + 1) / (height - 1),
      zero,
      (x2 - x1) / (width - 1),
      (x1 + x2 - width + 1) / (width - 1)], 1).view(-1, 2, 3)

    return theta


import datetime
nascimento = int(input('DIGITE SEU ANO DE NASCIMENTO: '))
ano = datetime.date.today().year
idade = ano - nascimento

if idade <= 9:
    print('MIRIM')
elif idade <= 14:
    print('INFANTIL')
elif idade <= 19:
    print('JUNIOR')
elif idade <= 25:
    print('SÊNIOR')
elif idade > 25:
    print('MASTER')


from game import Game, GameState
# from visualize import Visualize
from time import sleep
from gib2match import gib2match,dir2match,tot_match, match_str2dict
import pickle
from utils import make_action_space
import numpy as np
# dir2match('gib_raw')
# match_str=tot_match('gib_str')
# print(len(match_str))
# match_lst=match_str2dict(match_str)
# match_lst

def actions2policy(match_lst):
    tot_x=[]
    tot_y=[]
    action_space=make_action_space()
    eye=np.eye(len(action_space))
    for iter, match_info in enumerate(match_lst):
        error=False
        if len(match_info)!=6:
            for key in (match_info.keys()):
                if len(key)>20:
                    error=True

        action_lst=match_info['action_lst']
        for action in action_lst:
            if action=='':
                error=True
        if error:
            continue

        if iter%500==0:
            print(iter)
        env=Game(match_info['초차림'], match_info['한차림'])
        state=env.gameState
        #input : 현 state에 대한 설명  output : action
        for i, action in enumerate(action_lst):
            num_turn=i+1
            tot_x.append(state.BoardToInput)
            action=''.join(s for s in action if s.isdigit())

            if len(action)!=4:
                print(match_info)
            # else:
            #     if action[1]=='0':
            #         print(iter)

            tot_y.append(action_space.index(action))
            state, value, done, info=env.step(action)

    return tot_x, tot_y

             # for num_turn in range(end_turn):
        #     action=action_lst[num_turn]
        #     action=action[:5]

        #     num_turn+=1
        #     if win=='초':
        #         win_constant=1
        #     else:
        #         win_constant=0
        #     if '한수쉼' in action:
        #         action=None
        #         continue
        #     else:
        #         before=[int(i) for i in action[0:2]]
        #         after=[int(i) for i in action[3:5]]
        #         action=[before,after]
        #     next_action.append(action)
        #     new_state, value, done, _=env.step(action)
        #     # window.show(new_state)
        #     input=new_state.BoardToInput
        #     score=discount_ratio**(end_turn-num_turn)
        #     score*=(-1)**(num_turn+win_constant)
        #     input_lst.append(input)
        #     score_lst.append(score)

        # tot_input.append(input_lst)
        # tot_score.append(score_lst)
        # tot_action.append(next_action)
        # if num%50==0:
        #     print('총 {}중 iter {}'.format(len(data),num))

# data = pickle.load( open( 'match_lst_len13744.p',   "rb" ) )
# for i in range(3):
#     pickle.dump(data[i*5000:(i+1)*5000], open( "match_lst_ver{}.p".format(i), "wb"))
#     print('iter',i)

# for i in range(3):
#     data = pickle.load( open( 'match_lst_ver{}.p'.format(i),   "rb" ) )
#     tot_x,tot_y=actions2policy(data)
#     pickle.dump(tot_x, open( "policy_input_ver{}.p".format(i), "wb"))
#     pickle.dump(tot_y, open( "policy_output_ver{}.p".format(i), "wb"))
#     print('end ver{}'.format(i))
# # # #

# tot_x,tot_y=actions2policy(data)
# pickle.dump(tot_x, open( "policy_input_len{}.p".format(len(tot_x)), "wb"))
# pickle.dump(tot_y, open( "policy_output_len{}.p".format(len(tot_y)), "wb"))\

#

import config
from policy_model import Policy_Residual_CNN
import random
import numpy as np
import time
from utils import make_action_space
from keras.callbacks import ModelCheckpoint

policy_NN =Policy_Residual_CNN(config.REG_CONST, config.LEARNING_RATE, (15,10,9), 2450, config.HIDDEN_CNN_LAYERS)
model=policy_NN

for version in range(3):
    tot_x = (pickle.load( open( "policy_input_ver{}.p".format(version),   "rb" ) ))
    tot_y = (pickle.load( open( "policy_output_ver{}.p".format(version),  "rb" ) ))

    action_space=make_action_space()
    print(len(action_space))
    eyes=np.eye(len(action_space))
    tot_y = [eyes[action_idx] for action_idx in tot_y]
    tot_data=list(zip(tot_x,tot_y))

    for i in range(len(tot_data)//50000):
        total_data=tot_data[i*50000:(i+1)*50000]
        training_states = np.array([row[0] for row in total_data],dtype=float)
        training_targets = np.array([row[1] for row in total_data],dtype=float)

        fit = model.fit(training_states, training_targets, epochs=1, verbose=1, validation_split=0.03, batch_size =256)

        print('NEW LOSS %s'% fit.history)

        model.printWeightAverages()
model.write('gibo_policy',127)

# for i in range(int(len(tot_y)/3000)):
#     filename = 'checkpoint-version-{}-iter-{}.h5'.format(version, i)
#     checkpoint = ModelCheckpoint(filename,
#                                 monitor='acc',           # 로그를 출력합니다
#                                 save_best_only=True,  # 가장 best 값만 저장합니다
#                                 mode='auto'           # auto는 알아서 best를 찾습니다. min/max
#                                 )
#     minibatch = random.sample(total_data, 4096)
#     training_states = np.array([row[0] for row in minibatch],dtype=float)
#     training_targets = {'policy_head': np.array([row[1] for row in minibatch],dtype=float)}

#     fit = model.fit(training_states, training_targets, epochs=config.EPOCHS, verbose=1, validation_split=0, batch_size = 32, callbacks=[checkpoint])

#     print('NEW LOSS %s'% fit.history)

#     # train_overall_loss.append(round(fit.history['loss'][config.EPOCHS - 1],4))
#     # train_policy_loss.append(round(fit.history['policy_head_loss'][config.EPOCHS - 1],4))

#     # plt.plot(self.train_overall_loss, 'k')
#     # plt.plot(self.train_value_loss, 'k:')
#     # plt.plot(self.train_policy_loss, 'k--')

#     # plt.legend(['train_overall_loss', 'train_value_loss', 'train_policy_loss'], loc='lower left')

#     # display.clear_output(wait=True)
#     # display.display(pl.gcf())
#     # pl.gcf().clear()
#     # time.sleep(1.0)

#     model.printWeightAverages()
#     model.write('janggi', version)


import pytesseract
from PIL import Image

# 使用tesseract引擎识别图片，返回列表
# (\d+.\d+.\d+.\d+:\d+).*  '\1',
def ocr_ip_http(filename_list):
	ip_list = []
	print('正在调用tesseract引擎...')
	for name in filename_list:
		image = Image.open(name)
		text = pytesseract.image_to_string(image, lang='num')
		proxies = text.replace(' ','')
		# proxies = {'http': 'http://' + text}
		ip_list.append(proxies)
	print('返回一组结果')
	return ip_list


from mappening.utils.database import events_facebook_processed_collection
from mappening.utils.secrets import FACEBOOK_USER_ACCESS_TOKEN
from mappening.ml.autocategorization import categorizeEvents
from mappening.ml.autofood import labelFreeFood

import os
import sys
import requests
import json
import time, datetime, dateutil.parser, pytz
from tqdm import tqdm   # a progress bar, pretty

from definitions import API_UTILS_PATH

# need this to run file locally, or else won't know where to find mappening.utils.*
sys.path.insert(0, './../../..')

# Specify version in case most updated version (default if not specified) removes functionality, causing errors
API_VERSION_STR = 'v5.0/'
BASE_URL = 'https://graph.facebook.com/' + API_VERSION_STR

# Straight from fb2_event_collector.py
EVENT_FIELDS = ['id', 'name', 'cover', 'description', 'start_time', 'end_time',
                'place', 'event_times']

s = requests.Session()

app_access_token = FACEBOOK_USER_ACCESS_TOKEN

# Process for frontend to use it
def process_events(all_events):

    print(len(all_events))

    # URL parameters for refreshing / updating events info, including subevents
    sub_event_call_args = {
        'fields': ','.join(EVENT_FIELDS),
        # 'fields': "id,name,cover,description,start_time,end_time,place,event_times",
        'access_token': app_access_token
    }

    complete_events = []

    # check for multiday events
    for event in all_events:
        expanded_event_dict = {}
        # check for multi-day events, need API call again
        if 'event_times' in event:
            # need to call API again for event fields
            # batch call with multiple IDs: slightly faster from less network traffic, Facebook's end
            sub_ids = []
            for sub_event_header in event['event_times']:
                sub_ids.append(sub_event_header['id'])
            # sub_event_call_args['ids'] = ','.join(sub_ids)

            for id in sub_ids:

                # print(BASE_URL + id)
                # print(sub_event_call_args)
                resp = s.get(BASE_URL + id, params=sub_event_call_args)
                # print(resp.json())
                # print(resp.url)
                if resp.status_code != 200:
                    print(
                        'Error trying to retrieve sub-event data of event {0}: Status code {1}'
                        .format(event['id'], resp.status_code)
                    )
                    # just skip this multi-day event if sub-events not successfully retrieved
                    continue

                expanded_event_dict = resp.json()

                # not sure if expanded_event_dict is needed --haki
                # # add special "duplicate_occurrence" tag for event occurrences in the future that
                # # won't be searchable, because the 1st event start time has passed already
                # # don't need to refresh for the 1st event, since that matches the total event start time
                # for sub_event in expanded_event_dict.values():
                #     # using dict.values() and editing each item in list still changes original dictionary
                #     if sub_event['start_time'] != event['start_time']:
                #         sub_event['duplicate_occurrence'] = True

                # additional_events.append(expanded_event_dict)
                complete_events.append(expanded_event_dict)

            # finally we want to remove this top level event to prevent duplication,
            # so just don't add it to complete_events

        # More expanded_event_dict stuff: --haki
        else:
            complete_events.append(event)
            # expanded_event_dict.update({event['id']: event})

    # print("additional facebook events")
    # print(len(additional_events))

    # filtered_events.extend(additional_events)

    filtered_events = []

    for event in complete_events:

        # some events don't have a place, if they don't maybe just remove them for now and later try to get the place
        # TODO: find place from owner's location if place is not present
        if "place" not in event:
            # print("no place")
            # print(event)

            # don't add to filtered_events
            continue

        # some events have "place" but no "place.location"
        # this happens with some events that are imported to facebook from eventbrite
        # I.E. they have "owner" == Eventbrite
        # if this happens we can just exclude them from the facebook database collection
        # this might even be a good thing because if they are eventbrite events, it would be difficult to dedupe across collections anyways
        if "location" not in event["place"]:
            # print ("no location")
            # print(event)

            # don't add to filtered_events
            continue

        # change "zip" of place.location to "zipcode"
        if 'location' in event['place'] and 'zip' in event['place']['location']:
            event['place']['location']['zipcode'] = event['place']['location'].pop('zip')

        # if event does not have an endtime, give it an endtime 1 hour into the future
        if "end_time" not in event:
            # print("no end_time")
            event["end_time"] = (datetime.datetime.strptime(event["start_time"], '%Y-%m-%dT%H:%M:%S%z') + datetime.timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%S%z')
            # print(event["end_time"])

        filtered_events.append(event)

    print("filtered facebook events")
    print(len(filtered_events))

    # need to map facebook categories to bmaps categories
    # also do we need to run the ML for figuring out the categories?
    categorized_clean_events = categorizeEvents(filtered_events)
    categorized_clean_events = labelFreeFood(categorized_clean_events)

    # Autocategorization has a cleaner way to do this path switching
    savedPath = os.getcwd()
    os.chdir(API_UTILS_PATH)
    with open('facebk.json', 'w') as f:
        json.dump(categorized_clean_events, f, sort_keys=True, indent=4, separators=(',', ': '))
    os.chdir(savedPath)

    # add filtered events to database by deleting them all and readding (this prevents duplication hahaha)
    events_facebook_processed_collection.delete_many({})
    events_facebook_processed_collection.insert_many(categorized_clean_events)

    return categorized_clean_events


import pytest
import numpy as np
import raf
from raf.testing import check_type, run_infer_type, get_dist_comm_info, with_dialect
from tvm.relay import TensorType, FuncType, TupleType

@with_dialect(["tvm", "cuda"])
@pytest.mark.skipif(not raf.build.with_cuda(), reason="CUDA is not enabled")
@pytest.mark.parametrize("shape", [(128, 128), (64, 64), (32, 64, 128)])
@pytest.mark.parametrize("dtype", ["float32", "float16"])
@pytest.mark.parametrize("n", [2, 3, 4])
def test_fuse_tensor_type(shape, dtype, n):
    class Model(raf.Model):
        def build(self):
            pass

        @raf.model.trace
        def forward(self, x):
            y = raf.fuse_tensor([x] * n)
            return y

    model = Model()
    _, _, local_rank = get_dist_comm_info()
    device = f"cuda({local_rank})"
    x = np.ones(shape=shape, dtype=dtype)
    x = raf.array(x, device=device)
    m_func = model._internal(x).mod["main"]
    m_func = run_infer_type(m_func)
    t_a = TensorType(shape, dtype=dtype)
    size = 1
    for axis in shape:
        size *= axis
    size *= n
    t_b = TensorType((size,), dtype=dtype)
    desire_type = FuncType([t_a], t_b)
    check_type(m_func, desire_type)

@with_dialect(["tvm", "cuda"])
@pytest.mark.skipif(not raf.build.with_cuda(), reason="CUDA is not enabled")
@pytest.mark.parametrize(
    "shapes,shape_indices",
    [
        [(32, 32, 64, 64), (2, 4)],
        [(16, 32, 64, 64, 128), (2, 5)],
        [(32, 32, 64, 64, 32), (3, 5)],
    ],
)
@pytest.mark.parametrize("dtype", ["float32", "float16"])
def test_defuse_tensor_type(shapes, shape_indices, dtype):
    tuple_shape = []
    tensor_types = []
    total_size = 0
    sizes = []
    start_index = 0
    for end_index in shape_indices:
        size = 1
        for axis in shapes[start_index:end_index]:
            size *= axis
        total_size += size
        sizes.append(size)
        tuple_shape.append(tuple(shapes[start_index:end_index]))
        tensor_types.append(TensorType(shapes[start_index:end_index], dtype=dtype))
        start_index = end_index

    class Model(raf.Model):
        def build(self):
            pass

        @raf.model.trace
        def forward(self, x):
            y = raf.defuse_tensor(x, sizes, shapes, shape_indices)
            return y

    model = Model()
    _, _, local_rank = get_dist_comm_info()
    device = f"cuda({local_rank})"
    x = np.ones(shape=(total_size,), dtype=dtype)
    x = raf.array(x, device=device)
    m_func = model._internal(x).mod["main"]
    m_func = run_infer_type(m_func)
    t_a = TensorType((total_size,), dtype=dtype)
    t_b = TupleType(tensor_types)
    desire_type = FuncType([t_a], t_b)
    check_type(m_func, desire_type)

if __name__ == "__main__":
    pytest.main([__file__])


import requests

url = 'http://127.0.0.1:5000/'
url_login = 'http://127.0.0.1:5000/login'
# 创建会话对象
session = requests.Session()

# 会话访问
# res = session.get(url_login)

res1 = session.get(url)
print(res1.text)

"""
data 和 header 是什么类型？？？ 字典类型
res = request.get(url, params=data, header=headers)
# 响应对象
# res.text, res.content, res.json()
# res.cookies 获取 cookie

post 请求 params, data, json 三种传递数据
用哪一种？？？根据接口文档。
res = request.post(url, params=data, header=headers)

requests 的session机制
作用是动态管理 cookie, 几种管理所有的资源，不需要我们手工进行设置
s = request.Session(),  requests.get 和 s.get()
s.get()

"""


from autobahn.twisted.websocket import WebSocketServerProtocol, \
    WebSocketServerFactory
from json import loads as json_loads
from dispatcher import *
from sys import stdout
from twisted.python import log
from twisted.internet import reactor
from gc import collect as gc_collect
from pprint import pprint

DEBUG = True
PORT = 8000
URL = "ws://8.8.8.8:%d"
MAX_CON = 100

"""@package docstring
Websocket interface module.

Websocket interface implementation for prediction model implementation, input
interfaces are: studentID, and a list of courseID, provides the quality and
prediction values after classification/estimation process.
"""

class BackendServerProtocol(WebSocketServerProtocol):
    dispatchers = {}

    def onConnect(self, request):
        print("Client connecting: %s"%( request.peer ))

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        # dispatcher = WSDispatcher()
        # pprint("Send message")
        if isBinary:
            print("Binary message received: %d bytes"%( len( payload ) ))
        else:
            json_string = format(payload.decode('utf8'))
            json_input = json_loads( json_string )
            if 'source' in json_input.keys():
                in_source = json_input['source']
            elif u'source' in json_input.keys():
                in_source = json_input[u'source']
            else:
                in_source = 'kuleuven'
#            except:
            # print in_source
            # pprint( json_input )
        try:
            #dispatcher = self.dispatchers[json_input['requestId']]
            if self.dispatchers[self.peer]['source'] != in_source:
               raise Exception
            dispatcher = self.dispatchers[self.peer]['dispatcher']
        except:
            dispatcher = WSDispatcher(source=in_source)
            #self.dispatchers[json_input['requestId']] = dispatcher
            self.dispatchers[self.peer] = {}
            self.dispatchers[self.peer]['dispatcher'] = dispatcher
            self.dispatchers[self.peer]['source'] = in_source

        # echo back message verbatim
        #self.sendMessage( self.dispatcher.risk( json_input ), False )
        risk = dispatcher.risk( json_input )
        pprint( "Risk %s"%risk )
        self.sendMessage( risk, False )

    def onClose(self, wasClean, code, reason):
        try:
            del(self.dispatchers[self.peer])
        except:
            pass
        gc_collect()
        print("WebSocket connection closed: %s"%( reason ))

if __name__ == '__main__':

    log.startLogging( stdout )

    factory = WebSocketServerFactory(URL%PORT, debug=DEBUG)
    factory.protocol = BackendServerProtocol
    factory.setProtocolOptions(maxConnections=MAX_CON)

    reactor.listenTCP( PORT, factory )
    reactor.run()


from sanic import Sanic
from sanic import response
from cilantro_ee.logger.base import get_logger
import json as _json
from contracting.client import ContractingClient
from contracting.db.encoder import encode, decode
from contracting.db.driver import ContractDriver
from contracting.compilation import parser
from cilantro_ee import storage
from cilantro_ee.crypto.canonical import tx_hash_from_tx
from cilantro_ee.crypto.transaction import TransactionException

import ssl
import asyncio

log = get_logger("MN-WebServer")

from cilantro_ee.crypto import transaction

class ByteEncoder(_json.JSONEncoder):
    def default(self, o, *args):
        if isinstance(o, bytes):
            return o.hex()

        return super().default(self, o)

class WebServer:
    def __init__(self, contracting_client: ContractingClient, driver: ContractDriver, wallet, blocks, queue=[], port=8080, ssl_port=443, ssl_enabled=False,
                 ssl_cert_file='~/.ssh/server.csr',
                 ssl_key_file='~/.ssh/server.key',
                 workers=2, debug=True, access_log=False,
                 max_queue_len=10_000,
                 ):

        # Setup base Sanic class and CORS
        self.app = Sanic(__name__)
        self.app.config.update({
            'REQUEST_MAX_SIZE': 10000,
            'REQUEST_TIMEOUT': 5,
            'KEEP_ALIVE': False,
        })
        self.cors = None

        # Initialize the backend data interfaces
        self.client = contracting_client
        self.driver = driver
        self.nonces = storage.NonceStorage()
        self.blocks = blocks

        self.static_headers = {}

        self.wallet = wallet
        self.queue = queue
        self.max_queue_len = max_queue_len

        self.port = port

        self.ssl_port = ssl_port
        self.ssl_enabled = ssl_enabled
        self.context = None

        # Create the SSL Context if needed
        if self.ssl_enabled:
            self.context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
            self.context.load_cert_chain(ssl_cert_file, keyfile=ssl_key_file)

        # Store other Sanic constants for when server starts
        self.workers = workers
        self.debug = debug
        self.access_log = access_log

        # Add Routes
        self.app.add_route(self.submit_transaction, '/', methods=['POST', 'OPTIONS'])
        self.app.add_route(self.ping, '/ping', methods=['GET', 'OPTIONS'])
        self.app.add_route(self.get_id, '/id', methods=['GET'])
        self.app.add_route(self.get_nonce, '/nonce/<vk>', methods=['GET'])

        # State Routes
        self.app.add_route(self.get_methods, '/contracts/<contract>/methods', methods=['GET'])
        self.app.add_route(self.get_variables, '/contracts/<contract>/variables')
        self.app.add_route(self.get_variable, '/contracts/<contract>/<variable>')
        self.app.add_route(self.get_contracts, '/contracts', methods=['GET'])
        self.app.add_route(self.get_contract, '/contracts/<contract>', methods=['GET'])
        self.app.add_route(self.get_constitution, '/constitution', methods=['GET'])
        #self.app.add_route(self.iterate_variable, '/contracts/<contract>/<variable>/iterate')

        # Latest Block Routes
        self.app.add_route(self.get_latest_block, '/latest_block', methods=['GET', 'OPTIONS', ])
        self.app.add_route(self.get_latest_block_number, '/latest_block_num', methods=['GET'])
        self.app.add_route(self.get_latest_block_hash, '/latest_block_hash', methods=['GET'])

        # General Block Route
        self.app.add_route(self.get_block, '/blocks', methods=['GET'])

        # TX Route
        self.app.add_route(self.get_tx, '/tx', methods=['GET'])

        self.coroutine = None

    async def start(self):
        # Start server with SSL enabled or not
        if self.ssl_enabled:
            self.coroutine = asyncio.ensure_future(
                self.app.create_server(
                    host='0.0.0.0',
                    port=self.ssl_port,
                    debug=self.debug,
                    access_log=self.access_log,
                    ssl=self.context,
                    return_asyncio_server=True
                )
            )
        else:
            self.coroutine = asyncio.ensure_future(
                self.app.create_server(
                    host='0.0.0.0',
                    port=self.port,
                    debug=self.debug,
                    access_log=self.access_log,
                    return_asyncio_server=True
                )
            )

    # Main Endpoint to Submit TXs
    async def submit_transaction(self, request):
        log.debug(f'New request: {request}')
        # Reject TX if the queue is too large
        if len(self.queue) >= self.max_queue_len:
            return response.json({'error': "Queue full. Resubmit shortly."}, status=503, headers={'Access-Control-Allow-Origin': '*'})

        # Check that the payload is valid JSON
        tx = decode(request.body)
        if tx is None:

            return response.json({'error': 'Malformed request body.'}, headers={'Access-Control-Allow-Origin': '*'})

        # Check that the TX is correctly formatted
        try:
            transaction.check_tx_formatting(tx, self.wallet.verifying_key)

            transaction.transaction_is_valid(
                transaction=tx,
                expected_processor=self.wallet.verifying_key,
                client=self.client,
                nonces=self.nonces
            )

            nonce, pending_nonce = transaction.get_nonces(
                sender=tx['payload']['sender'],
                processor=tx['payload']['processor'],
                driver=self.nonces
            )

            pending_nonce = transaction.get_new_pending_nonce(
                tx_nonce=tx['payload']['nonce'],
                nonce=nonce,
                pending_nonce=pending_nonce
            )

            self.nonces.set_pending_nonce(
                sender=tx['payload']['sender'],
                processor=tx['payload']['processor'],
                value=pending_nonce
            )
        except TransactionException as e:
            log.error(f'Tx has error: {type(e)}')
            return response.json(
                transaction.EXCEPTION_MAP[type(e)], headers={'Access-Control-Allow-Origin': '*'}
            )

        # Add TX to the processing queue
        self.queue.append(tx)

        # Return the TX hash to the user so they can track it
        tx_hash = tx_hash_from_tx(tx)

        return response.json({
            'success': 'Transaction successfully submitted to the network.',
            'hash': tx_hash
        }, headers={'Access-Control-Allow-Origin': '*'})

    # Network Status
    async def ping(self, request):
        return response.json({'status': 'online'}, headers={'Access-Control-Allow-Origin': '*'})

    # Get VK of this Masternode for Nonces
    async def get_id(self, request):
        return response.json({'verifying_key': self.wallet.verifying_key}, headers={'Access-Control-Allow-Origin': '*'})

    # Get the Nonce of a VK
    async def get_nonce(self, request, vk):
        latest_nonce = self.nonces.get_latest_nonce(sender=vk, processor=self.wallet.verifying_key)

        return response.json({
            'nonce': latest_nonce,
            'processor': self.wallet.verifying_key,
            'sender': vk
        }, headers={'Access-Control-Allow-Origin': '*'})

    # Get all Contracts in State (list of names)
    async def get_contracts(self, request):
        contracts = self.client.get_contracts()
        return response.json({'contracts': contracts}, headers={'Access-Control-Allow-Origin': '*'})

    # Get the source code of a specific contract
    async def get_contract(self, request, contract):
        contract_code = self.client.raw_driver.get_contract(contract)

        if contract_code is None:
            return response.json({'error': '{} does not exist'.format(contract)}, status=404, headers={'Access-Control-Allow-Origin': '*'})
        return response.json({'name': contract, 'code': contract_code}, status=200, headers={'Access-Control-Allow-Origin': '*'})

    async def get_methods(self, request, contract):
        contract_code = self.client.raw_driver.get_contract(contract)

        if contract_code is None:
            return response.json({'error': '{} does not exist'.format(contract)}, status=404, headers={'Access-Control-Allow-Origin': '*'})

        funcs = parser.methods_for_contract(contract_code)

        return response.json({'methods': funcs}, status=200, headers={'Access-Control-Allow-Origin': '*'})

    async def get_variables(self, request, contract):
        contract_code = self.client.raw_driver.get_contract(contract)

        if contract_code is None:
            return response.json({'error': '{} does not exist'.format(contract)}, status=404, headers={'Access-Control-Allow-Origin': '*'})

        variables = parser.variables_for_contract(contract_code)

        return response.json(variables, headers={'Access-Control-Allow-Origin': '*'})

    async def get_variable(self, request, contract, variable):
        contract_code = self.client.raw_driver.get_contract(contract)

        if contract_code is None:
            return response.json({'error': '{} does not exist'.format(contract)}, status=404, headers={'Access-Control-Allow-Origin': '*'})

        key = request.args.get('key')
        if key is not None:
            key = key.split(',')

        k = self.client.raw_driver.make_key(contract=contract, variable=variable, args=key)
        value = self.client.raw_driver.get(k)

        if value is None:
            return response.json({'value': None}, status=404, headers={'Access-Control-Allow-Origin': '*'})
        else:
            return response.json({'value': value}, status=200, dumps=encode, headers={'Access-Control-Allow-Origin': '*'})

    # async def iterate_variable(self, request, contract, variable):
    #     contract_code = self.client.raw_driver.get_contract(contract)
    #
    #     if contract_code is None:
    #         return response.json({'error': '{} does not exist'.format(contract)}, status=404)
    #
    #     key = request.args.get('key')
    #     # if key is not None:
    #     #     key = key.split(',')
    #
    #     k = self.client.raw_driver.make_key(contract=contract, variable=variable, args=key)
    #
    #     values = self.client.raw_driver.driver.iter(k, length=500)
    #
    #     if len(values) == 0:
    #         return response.json({'values': None}, status=404)
    #     return response.json({'values': values, 'next': values[-1]}, status=200)

    async def get_latest_block(self, request):
        index = self.blocks.get_last_n(n=1, collection=storage.BlockStorage.BLOCK)
        if len(index) == 0:
            block = {
                'hash': (b'\x00' * 32).hex(),
                'number': 0,
                'previous': (b'\x00' * 32).hex(),
                'subblocks': []
            }
        else:
            block = index[0]
        return response.json(block, dumps=ByteEncoder().encode, headers={'Access-Control-Allow-Origin': '*'})

    async def get_latest_block_number(self, request):

        return response.json({'latest_block_number': storage.get_latest_block_height(self.driver)}, headers={'Access-Control-Allow-Origin': '*'})

    async def get_latest_block_hash(self, request):
        return response.json({'latest_block_hash': storage.get_latest_block_hash(self.driver)}, headers={'Access-Control-Allow-Origin': '*'})

    async def get_block(self, request):
        num = request.args.get('num')
        _hash = request.args.get('hash')

        if num is not None:
            block = self.blocks.get_block(int(num))
        elif _hash is not None:
            block = self.blocks.get_block(_hash)
        else:
            return response.json({'error': 'No number or hash provided.'}, status=400, headers={'Access-Control-Allow-Origin': '*'})

        if block is None:
            return response.json({'error': 'Block not found.'}, status=400, headers={'Access-Control-Allow-Origin': '*'})

        return response.json(block, dumps=ByteEncoder().encode, headers={'Access-Control-Allow-Origin': '*'})

    async def get_tx(self, request):
        _hash = request.args.get('hash')

        if _hash is not None:
            try:
                int(_hash, 16)
                tx = self.blocks.get_tx(_hash)
            except ValueError:
                return response.json({'error': 'Malformed hash.'}, status=400, headers={'Access-Control-Allow-Origin': '*'})
        else:
            return response.json({'error': 'No tx hash provided.'}, status=400, headers={'Access-Control-Allow-Origin': '*'})

        if tx is None:
            return response.json({'error': 'Transaction not found.'}, status=400, headers={'Access-Control-Allow-Origin': '*'})

        return response.json(tx, dumps=ByteEncoder().encode, headers={'Access-Control-Allow-Origin': '*'})

    async def get_constitution(self, request):
        masternodes = self.client.get_var(
            contract='masternodes',
            variable='S',
            arguments=['members']
        )

        delegates = self.client.get_var(
            contract='delegates',
            variable='S',
            arguments=['members']
        )

        return response.json({
            'masternodes': masternodes,
            'delegates': delegates
        }, headers={'Access-Control-Allow-Origin': '*'})


from .conditions import *
from .commands import *

import os
import sys
import argparse
import shutil
import glob
from io import BytesIO
from typing import Any, Union, List, BinaryIO
from .utils import __version__, PACKAGE_NAME, IS_WINDOWS

class Sed:
    def __init__(self):
        self._commands = SedCommandGroup(None)
        self._expression_number = 0
        self._files = []
        self._newline:bytes = b'\n'
        # Parse files in place instead of to stdout
        self.in_place:bool = False
        # The suffix to use for creating backup files when in_place is True
        self.in_place_backup_suffix:Union[str,None] = None
        # When True, follow symbolic links when in_place is True
        self.follow_symlinks:bool = False
        # True to suppress printing of pattern space
        self.suppress_pattern_print:bool = False
        # True to use extended regex mode
        self.extended_regex:bool = False
        # The line length to use for l command
        self.unambiguous_line_len:int = 70
        # When true, restart line count when a new file is opened
        self.separate:bool = False
        # Disable e/r/w commands when True
        self.sandbox_mode:bool = False

    @property
    def newline(self) -> bytes:
        ''' Returns the newline sequence the input data is split by '''
        return self._newline

    @newline.setter
    def newline(self, newline:Union[str,bytes]):
        if isinstance(newline, str):
            self._newline = newline.encode()
        else:
            self._newline = newline

    def add_expression(self, script:str) -> None:
        '''
        Adds an expression string (i.e. a command line expression string).
        Expressions are parsed and added to my internal list of commands.
        '''
        self._expression_number += 1
        self._commands.add_expression(script, self._expression_number, self.sandbox_mode)

    def add_command(self, command_or_commands:Union[SedCommand, List[SedCommand]]) -> None:
        ''' Adds a command object or list of commands (one of SedCommand)'''
        self._commands.add_commands(command_or_commands)

    def clear_commands(self) -> None:
        ''' Clears all set commands and expressions '''
        self._commands.clear_commands()
        self._expression_number = 0

    def add_file(self, file_or_files:Union[str, List[str]]) -> None:
        ''' Adds a file to parse '''
        if isinstance(file_or_files, list):
            self._files.extend(file_or_files)
        else:
            self._files.append(file_or_files)

    def clear_files(self) -> None:
        ''' Clears all files set by add_file '''
        self._files.clear()

    def execute(self, out_buffer:BinaryIO=sys.stdout.buffer) -> int:
        '''
        Executes Sed with the settings, commands, and files set.
        Inputs: out_buffer - when in_place is false, this is the buffer written to (stdout by default)
        Returns the exit code (0 on success)
        '''
        self._commands.check_labels()

        if not self._files:
            files = [StdinIterable(end=self.newline, label='-')]
        else:
            files = [AutoInputFileIterable(f, newline_str=self.newline) for f in self._files]

        dat = WorkingData()
        dat.suppress_pattern_print = self.suppress_pattern_print
        dat.extended_regex = self.extended_regex
        dat.unambiguous_line_len = self.unambiguous_line_len
        dat.separate = self.separate
        dat.newline = self.newline
        for file in files:
            dat.set_in_file(file)

            if self.in_place and not isinstance(file, StdinIterable):
                # Write to temporary file to be copied to target when it changes
                tmp_file = BytesIO()
                dat.out_file = tmp_file
            else:
                tmp_file = None
                dat.out_file = out_buffer

            while dat.next_line():
                # Start at the beginning
                dat.jump_to = 0
                try:
                    while dat.jump_to is not None:
                        jump_to = dat.jump_to
                        dat.jump_to = None
                        # jump_to may be -1, 0, or label
                        if jump_to == 0:
                            # Jump to beginning
                            self._commands.handle(dat)
                        elif isinstance(jump_to, str):
                            if not self._commands.jump_to_label(dat, jump_to):
                                # Shouldn't reach here due to self._commands.check_labels() above
                                raise SedExecutionException(f"can't find label for jump to `{jump_to}'")
                        # else: jump to end (flush and read next line)
                except SedQuitException as ex:
                    return ex.exit_code
                except SedFileCompleteException:
                    # This will happen if a next command was used, and there is nothing else to read
                    break

                dat.flush_all_data()

            # Final pattern flush just in case there was something left
            dat.flush_all_data()

            if dat.file_modified and tmp_file:
                # Write data from temp file to destination
                tmp_file.flush()
                if self.follow_symlinks:
                    file_name = os.path.abspath(os.path.realpath(file.name))
                else:
                    file_name = os.path.abspath(file.name)
                if self.in_place_backup_suffix is not None:
                    backup_name = file_name + self.in_place_backup_suffix
                    shutil.copy2(file_name, backup_name)
                with open(file_name, 'wb') as fp:
                    fp.write(tmp_file.getvalue())
        return 0

class SedArgParser:
    def __init__(self, cliargs:List[str]) -> None:
        self.args = __class__._parse_args(cliargs)

    @staticmethod
    def _parse_args(cliargs:List[str]):
        ''' Parses all arguments from command line '''
        parser = argparse.ArgumentParser(
            prog=PACKAGE_NAME,
            description='A sed clone in Python with both CLI and library interfaces'
        )

        parser.add_argument('script', type=str, nargs='?', default=None,
                            help='script, only if no other script defined below')
        parser.add_argument('input_file', metavar='input-file', type=str, nargs='*', default=[],
                            help='Input file(s) to parse')

        parser.add_argument('-n', '--quiet', '--silent', dest='quiet', action='store_true',
                            help='suppress automatic printing of pattern space')
        # No debug data
        parser.add_argument('--debug', action='store_true', help='annotate program execution')
        parser.add_argument('-e', '--expression', metavar='script', type=str, default=[], action='append',
                            help='add the script to the commands to be executed')
        parser.add_argument('-f', '--file', metavar='script-file', type=str, default=[], action='append',
                            help='add the contents of script-file to the commands to be executed')
        parser.add_argument('--follow-symlinks', action='store_true',
                            help='follow symlinks when processing in place')
        parser.add_argument('-i', '--in-place', metavar='SUFFIX', nargs='?', type=str, default=None,
                            const=True,
                            help='edit files in place (makes backup if SUFFIX supplied)')
        parser.add_argument('-l', '--line-length', metavar='N', type=int, default=70,
                            help='specify the desired line-wrap length for the `l\' command')
        # This option currently has no effect
        parser.add_argument('--posix', action='store_true', help='disable all extensions.')
        parser.add_argument('-E', '-r', '--regexp-extended', action='store_true',
                            help='use extended regular expressions in the script')
        parser.add_argument('-s', '--separate', action='store_true',
                            help='consider files as separate rather than as a single, '
                            'continuous long stream.')
        parser.add_argument('--sandbox', action='store_true',
                            help='operate in sandbox mode (disable e/r/w commands).')
        # This is just done by default - here for compatibility only
        parser.add_argument('-u', '--unbuffered', action='store_true',
                            help='load minimal amounts of data from the input files and flush '
                            'the output buffers more often')
        parser.add_argument('--end', type=str, default='\n',
                            help='end-of-line character for parsing search files (default: \\n); '
                            'this does not affect file parsing for -f or --exclude-from')
        parser.add_argument('-z', '--null-data', action='store_true',
                            help='same as --end=\'\\0\'')
        parser.add_argument('--version', action='store_true',
                            help='output version information and exit')
        parser.add_argument('--verbose', action='store_true', help='show verbose errors')
        args = parser.parse_args(cliargs)

        if args.expression or args.file:
            # The first positional is instead an input file
            if args.script is not None:
                args.input_file.insert(0, args.script)
                args.script = None
        elif args.script is None and not args.version:
            parser.print_help()
            sys.exit(1)

        return args

    @staticmethod
    def _expand_cli_path(path:str) -> List[str]:
        if IS_WINDOWS and '*' in path or '?' in path:
            # Need to manually expand this out
            expanded_paths = [f for f in glob.glob(path)]
            if not expanded_paths:
                print('No match for: {}'.format(path), file=sys.stderr)
        else:
            # *nix and *nix based systems do this from command line
            expanded_paths = [path]
        return expanded_paths

    @staticmethod
    def _expand_cli_paths(paths:List[str]) -> List[str]:
        return [y for x in paths for y in __class__._expand_cli_path(x)]

    def parse(self, sed:Sed) -> None:
        ''' Parses command line arguments into Sed object '''
        args = self.args
        if args.version:
            print('{} {}'.format(PACKAGE_NAME, __version__))
            sys.exit(0)
        sed.suppress_pattern_print = args.quiet
        sed.extended_regex = args.regexp_extended
        sed.unambiguous_line_len = args.line_length
        sed.separate = args.separate
        sed.sandbox_mode = args.sandbox
        sed.follow_symlinks = args.follow_symlinks
        if args.null_data:
            sed.newline = '\0'
        else:
            sed.newline = bytes(args.end, "utf-8").decode("unicode_escape")
        if args.script:
            sed.add_expression(args.script)
        for expression in args.expression:
            sed.add_expression(expression)
        for file in __class__._expand_cli_paths(args.file):
            with open(file, 'r') as fp:
                sed.add_expression(fp.read())
        if args.input_file:
            sed.add_file(__class__._expand_cli_paths(args.input_file))
        if args.in_place is not None:
            sed.in_place = True
            if isinstance(args.in_place, str):
                sed.in_place_backup_suffix = args.in_place

def main(cliargs:List[str]) -> int:
    ''' Command line main execution point '''

    sed = Sed()
    sed_arg_parser = SedArgParser(cliargs)

    try:
        sed_arg_parser.parse(sed)
        return sed.execute(sys.stdout.buffer)
    except Exception as ex:
        if sed_arg_parser.args.verbose:
            raise ex
        else:
            print(f'{PACKAGE_NAME}: {ex}', file=sys.stderr)
            return 1


#!/usr/bin/env python3
import sys
import requests
from zeep import Client

class NumberConversion:
    def __init__(self, number, url):
        self.number=number
        self.url = url

    def buscar_infos(self):
        cliente = Client(self.url + '?WSDL')

        response = cliente.service.NumberToWords(self.number)

        return response

if __name__ == "__main__":
    number = 123
    if len(sys.argv) > 1:
        number = sys.argv[1]

    number_obj = NumberConversion(number=number, url='https://www.dataaccess.com/webservicesserver/NumberConversion.wso')

    print(number_obj.buscar_infos())


# 实现voxel滤波，并加载数据集中的文件进行验证

import open3d as o3d
import os
import numpy as np
from pandas import DataFrame
from pyntcloud import PyntCloud
import math

# 功能：对点云进行voxel滤波
# 输入：
#     point_cloud：输入点云
#     leaf_size: voxel尺寸
def voxel_filter(point_cloud, leaf_size):
    filtered_points = []
    # 作业3
    # 屏蔽开始
    point_cloud = np.array(point_cloud, dtype=np.float64)
    # print(point_cloud)
    x_array = point_cloud[:, 0]
    y_array = point_cloud[:, 1]
    z_array = point_cloud[:, 2]
    x_max, x_min = np.max(x_array), np.min(x_array)
    y_max, y_min = np.max(y_array), np.min(y_array)
    z_max, z_min = np.max(z_array), np.min(z_array)

    D_x = math.ceil((x_max - x_min) / leaf_size)
    D_y = math.ceil((y_max - y_min) / leaf_size)
    D_z = math.ceil((z_max - z_min) / leaf_size)

    h_array = np.array([], dtype=np.float64)
    for point in point_cloud:
        point = np.array(point, dtype=np.float64)
        h_x = np.floor((point[0] - x_min) / leaf_size)
        h_y = np.floor((point[1] - y_min) / leaf_size)
        h_z = np.floor((point[2] - z_min) / leaf_size)
        h = h_x + h_y * D_x + h_z * D_x * D_y
        h_array = np.append(h_array, h)

    sort = h_array.argsort()
    # print(h_array)
    # print(sort)
    point_cloud = point_cloud[sort]
    # print(point_cloud[0:7, :])
    # print(h_array[sort])
    # print(len(point_cloud))

    voxel = []
    for i in range(len(point_cloud)):
        # print(i)
        if i < len(point_cloud) - 1 and h_array[sort][i] == h_array[sort][i+1]:
            voxel.append(i)
        else:
            voxel.append(i)
            # random
            # rand_array = np.arange(point_cloud[i-len(voxel)+1:i+1, :].shape[0])
            # np.random.shuffle(rand_array)
            # choice_point = point_cloud[i-len(voxel)+1:i+1, :][rand_array[0:1]]

            # centroid
            choice_point = np.mean(point_cloud[i-len(voxel)+1:i+1, :], axis=0)

            point_cloud[i-len(voxel)+1:i+1, :] = choice_point
            voxel = []

    filtered_points = point_cloud
    # 屏蔽结束

    # 把点云格式改成array，并对外返回
    filtered_points = np.array(filtered_points, dtype=np.float64)
    return filtered_points

def main():
    # # 从ModelNet数据集文件夹中自动索引路径，加载点云
    # cat_index = 10 # 物体编号，范围是0-39，即对应数据集中40个物体
    # root_dir = '/Users/renqian/cloud_lesson/ModelNet40/ply_data_points' # 数据集路径
    # cat = os.listdir(root_dir)
    # filename = os.path.join(root_dir, cat[cat_index],'train', cat[cat_index]+'_0001.ply') # 默认使用第一个点云
    # point_cloud_pynt = PyntCloud.from_file(file_name)

    """txt格式点云
    """
    points = np.genfromtxt("/home/tfwang/PointCloud/homework/modelnet40_normal_resampled/glass_box/glass_box_0072.txt", delimiter=",")
    points = DataFrame(points[:, 0:3])
    points.columns = ['x', 'y', 'z']
    point_cloud_pynt = PyntCloud(points)

    # 转成open3d能识别的格式
    point_cloud_o3d = point_cloud_pynt.to_instance("open3d", mesh=False)
    o3d.visualization.draw_geometries([point_cloud_o3d]) # 显示原始点云

    # 调用voxel滤波函数，实现滤波
    filtered_cloud = voxel_filter(point_cloud_pynt.points, 0.08)
    # filtered_cloud = point_cloud_o3d.voxel_down_sample(voxel_size=0.05)
    point_cloud_o3d.points = o3d.utility.Vector3dVector(filtered_cloud)
    # 显示滤波后的点云
    o3d.visualization.draw_geometries([point_cloud_o3d])
    # o3d.visualization.draw_geometries([filtered_cloud])

if __name__ == '__main__':
    main()


import torch
import matplotlib.pyplot as plt
from torch import nn, utils
from torchvision import datasets, transforms
from lib.models.cifar10 import fvgg16_bn, fresnet18
from lib.helper import cifar10_tester
from lib.models.module import builder

batch_size = 200
device = "cuda"
model_name = "vgg16"
rebuild = True

kernel_size = 3
num_filters = 3

load_path = f"./checkpoint/cifar10_vgg16_{num_filters}_3_model.pth"

ratio = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

origin_acc = []
origin_loss = []
prune_acc = []
prune_loss = []

for r in ratio:
    # model load
    model = fvgg16_bn(num_filters=num_filters).to(device)
    model.load_state_dict(torch.load(load_path))

    # build
    conv_model = builder(model, num_filters=num_filters)

    # cost
    criterion = nn.CrossEntropyLoss().to(device)

    # test
    test_loss, test_acc, _ = cifar10_tester(conv_model, batch_size=batch_size)

    # zero weights
    total = 0

    for m in conv_model.modules():
        if isinstance(m, nn.Conv2d):
            total += m.weight.data.numel()

    conv_weight = torch.zeros(total).cuda()

    # generate mask
    index = 0

    for m in conv_model.modules():
        if isinstance(m, nn.Conv2d):
            size = m.weight.data.numel()
            conv_weight[index:(index + size)] = m.weight.data.view(-1).abs().clone()
            index += size

    y, i = torch.sort(conv_weight)
    thre_index = int(total * r)
    thre = y[thre_index]

    # prune
    pruned = 0
    print(f'Pruning Threshold : {thre}')
    zero_flag = False

    for k, m in enumerate(conv_model.modules()):
        if isinstance(m, nn.Conv2d):
            weight_copy = m.weight.data.abs().clone()
            mask = weight_copy.gt(thre).float().cuda()
            pruned = pruned + mask.numel() - torch.sum(mask)
            m.weight.data.mul_(mask)
            if int(torch.sum(mask)) == 0:
                zero_flag = True
            print(f'layer index: {k} \t total params: {mask.numel()} \t remaining params: {int(torch.sum(mask))}')

    # prune test
    prune_test_loss, prune_test_acc, _ = cifar10_tester(conv_model, batch_size=batch_size)

    # log
    origin_acc.append(test_acc)
    origin_loss.append(test_loss)
    prune_acc.append(prune_test_acc)
    prune_loss.append(prune_test_loss)

    print(f'Total conv params: {total}, Pruned conv params: {pruned}, Pruned ratio: {pruned / total}')

fig, axes = plt.subplots(1, 2, figsize=(10, 10))

axes[0].plot(origin_acc, label='origin acc')
axes[0].plot(prune_acc, label='prune acc')
axes[0].set_title("ACC")
axes[0].legend()
axes[0].grid()

axes[1].plot(origin_loss, label='origin loss')
axes[1].plot(prune_loss, label='prune loss')
axes[1].set_title("Loss")
axes[1].legend()
axes[1].grid()

plt.show()


#This case is for 3D but everything except the 'rosen' function is general and can do higher dimensions by changing a few 3's
#Preamble

import torch
import gpytorch
import math
import numpy as np
import numpy.linalg as linalg
from matplotlib import pyplot as plt

#You have to load the GP class also

#Analytic functions and training data

def F(X):
    a = 1
    b = 10
    N = X.size()[0]
    result = 0
    for i in range(N-1):
        result += b*(X[i+1]-X[i]**2)**2 + (a-X[i])**2
    return result

def J(X):
    X = torch.autograd.Variable(X, requires_grad=True)
    dtX = torch.autograd.functional.jacobian(F,X)
    return dtX

def rosen(X,Y,Z):
    a = 1
    b = 10
    f = b*(Y-X**2)**2 + (a-X)**2 + b*(Z-Y**2)**2 + (a-Y)**2
    dx = -4*b*X*(Y-X**2) - 2*(a-X)
    dy = 2*b*(Y-X**2) - 4*b*Y*(Z-Y**2) - 2*(a-Y)
    dz = 2*b*(Z-Y**2)
    return f, dx, dy, dz

xv, yv, zv = torch.meshgrid([torch.linspace(0, 2, 10), torch.linspace(0, 2, 10), torch.linspace(0, 2, 10)])
train_x = torch.cat((
    xv.contiguous().view(xv.numel(), 1),
    yv.contiguous().view(yv.numel(), 1),
    zv.contiguous().view(yv.numel(), 1)),
    dim=1
)

f, dfx, dfy, dfz = rosen(train_x[:, 0], train_x[:, 1], train_x[:, 2])
train_y = torch.stack([f, dfx, dfy, dfz], -1).squeeze(-1)

#First model, seeing the parameters, and then initalizing new model

likelihood, model = GP.CreateGP(train_x,train_y,dim_num=3,training_iter=800,show=True)
GP.paramshow(model)
para = (torch.tensor([1.0000e-04]),torch.tensor([0.4729, 0.4617, 1.6312]),[38.05531311035156])
likelihood, model = GP.CreateGPinitial(train_x,train_y,3,para,20,True)

#c1 plot and quasi-newton method

X = torch.tensor([0.8,0.8,0.8])
GP.c1plot(F,J,X,0.6,3)
GP.minimiseActive(F,J,X,train_x,train_y,3,0.6,0.9)