import collections
import time

import six
from six.moves.urllib import parse
try:
    from ws4py.client import WebSocketBaseClient
    from ws4py.manager import WebSocketManager
    _ws4py_installed = True
except ImportError:  # pragma: no cover
    WebSocketBaseClient = object
    _ws4py_installed = False

from pylxd import managers
from pylxd.models import _model as model

class ContainerState(object):
    """A simple object for representing container state."""

    def __init__(self, **kwargs):
        for key, value in six.iteritems(kwargs):
            setattr(self, key, value)

_ContainerExecuteResult = collections.namedtuple(
    'ContainerExecuteResult',
    ['exit_code', 'stdout', 'stderr'])

class Container(model.Model):
    """An LXD Container.

    This class is not intended to be used directly, but rather to be used
    via `Client.containers.create`.
    """

    architecture = model.Attribute()
    config = model.Attribute()
    created_at = model.Attribute()
    devices = model.Attribute()
    ephemeral = model.Attribute()
    expanded_config = model.Attribute()
    expanded_devices = model.Attribute()
    name = model.Attribute(readonly=True)
    description = model.Attribute()
    profiles = model.Attribute()
    status = model.Attribute(readonly=True)
    last_used_at = model.Attribute(readonly=True)

    status_code = model.Attribute(readonly=True)
    stateful = model.Attribute(readonly=True)

    snapshots = model.Manager()
    files = model.Manager()

    @property
    def api(self):
        return self.client.api.containers[self.name]

    class FilesManager(object):
        """A pseudo-manager for namespacing file operations."""

        def __init__(self, client, container):
            self._client = client
            self._container = container

        def put(self, filepath, data, mode=None, uid=None, gid=None):
            """Push a file to the container.

            This pushes a single file to the containers file system named by
            the `filepath`.

            :param filepath: The path in the container to to store the data in.
            :type filepath: str
            :param data: The data to store in the file.
            :type data: bytes or str
            :param mode: The unit mode to store the file with.  The default of
                None stores the file with the current mask of 0700, which is
                the lxd default.
            :type mode: oct | int | str
            :param uid: The uid to use inside the container. Default of None
                results in 0 (root).
            :type uid: int
            :param gid: The gid to use inside the container.  Default of None
                results in 0 (root).
            :type gid: int
            :returns: True if the file store succeeded otherwise False.
            :rtype: Bool
            """
            headers = {}
            if mode is not None:
                if isinstance(mode, int):
                    mode = format(mode, 'o')
                if not isinstance(mode, six.string_types):
                    raise ValueError("'mode' parameter must be int or string")
                if not mode.startswith('0'):
                    mode = '0{}'.format(mode)
                headers['X-LXD-mode'] = mode
            if uid is not None:
                headers['X-LXD-uid'] = str(uid)
            if gid is not None:
                headers['X-LXD-gid'] = str(gid)
            response = (self._client.api.containers[self._container.name]
                        .files.post(params={'path': filepath},
                                    data=data,
                                    headers=headers or None))
            return response.status_code == 200

        def delete_available(self):
            """File deletion is an extension API and may not be available.
            https://github.com/lxc/lxd/blob/master/doc/api-extensions.md#file_delete
            """
            return u'file_delete' in self._client.host_info['api_extensions']

        def delete(self, filepath):
            if self.delete_available():
                response = self._client.api.containers[
                    self._container.name].files.delete(
                    params={'path': filepath})
                return response.status_code == 200
            else:
                raise ValueError(
                    'File Deletion is not available for this host')

        def get(self, filepath):
            response = (self._client.api.containers[self._container.name]
                        .files.get(params={'path': filepath}))
            return response.content

    @classmethod
    def exists(cls, client, name):
        """Determine whether a container exists."""
        try:
            client.containers.get(name)
            return True
        except cls.NotFound:
            return False

    @classmethod
    def get(cls, client, name):
        """Get a container by name."""
        response = client.api.containers[name].get()

        container = cls(client, **response.json()['metadata'])
        return container

    @classmethod
    def all(cls, client):
        """Get all containers.

        Containers returned from this method will only have the name
        set, as that is the only property returned from LXD. If more
        information is needed, `Container.sync` is the method call
        that should be used.
        """
        response = client.api.containers.get()

        containers = []
        for url in response.json()['metadata']:
            name = url.split('/')[-1]
            containers.append(cls(client, name=name))
        return containers

    @classmethod
    def create(cls, client, config, wait=False):
        """Create a new container config."""
        response = client.api.containers.post(json=config)

        if wait:
            client.operations.wait_for_operation(response.json()['operation'])
        return cls(client, name=config['name'])

    def __init__(self, *args, **kwargs):
        super(Container, self).__init__(*args, **kwargs)

        self.snapshots = managers.SnapshotManager(self.client, self)
        self.files = self.FilesManager(self.client, self)

    def rename(self, name, wait=False):
        """Rename a container."""
        response = self.api.post(json={'name': name})

        if wait:
            self.client.operations.wait_for_operation(
                response.json()['operation'])
        self.name = name

    def _set_state(self, state, timeout=30, force=True, wait=False):
        response = self.api.state.put(json={
            'action': state,
            'timeout': timeout,
            'force': force
        })
        if wait:
            self.client.operations.wait_for_operation(
                response.json()['operation'])
            if 'status' in self.__dirty__:
                del self.__dirty__[self.__dirty__.index('status')]
            if self.ephemeral and state == 'stop':
                self.client = None
            else:
                self.sync()

    def state(self):
        response = self.api.state.get()
        state = ContainerState(**response.json()['metadata'])
        return state

    def start(self, timeout=30, force=True, wait=False):
        """Start the container."""
        return self._set_state('start',
                               timeout=timeout,
                               force=force,
                               wait=wait)

    def stop(self, timeout=30, force=True, wait=False):
        """Stop the container."""
        return self._set_state('stop',
                               timeout=timeout,
                               force=force,
                               wait=wait)

    def restart(self, timeout=30, force=True, wait=False):
        """Restart the container."""
        return self._set_state('restart',
                               timeout=timeout,
                               force=force,
                               wait=wait)

    def freeze(self, timeout=30, force=True, wait=False):
        """Freeze the container."""
        return self._set_state('freeze',
                               timeout=timeout,
                               force=force,
                               wait=wait)

    def unfreeze(self, timeout=30, force=True, wait=False):
        """Unfreeze the container."""
        return self._set_state('unfreeze',
                               timeout=timeout,
                               force=force,
                               wait=wait)

    def execute(self, commands, environment={}):
        """Execute a command on the container.

        In pylxd 2.2, this method will be renamed `execute` and the existing
        `execute` method removed.
        """
        if not _ws4py_installed:
            raise ValueError(
                'This feature requires the optional ws4py library.')
        if isinstance(commands, six.string_types):
            raise TypeError("First argument must be a list.")
        response = self.api['exec'].post(json={
            'command': commands,
            'environment': environment,
            'wait-for-websocket': True,
            'interactive': False,
        })

        fds = response.json()['metadata']['metadata']['fds']
        operation_id = response.json()['operation'].split('/')[-1]
        parsed = parse.urlparse(
            self.client.api.operations[operation_id].websocket._api_endpoint)

        with managers.web_socket_manager(WebSocketManager()) as manager:
            stdin = _StdinWebsocket(self.client.websocket_url)
            stdin.resource = '{}?secret={}'.format(parsed.path, fds['0'])
            stdin.connect()
            stdout = _CommandWebsocketClient(
                manager, self.client.websocket_url)
            stdout.resource = '{}?secret={}'.format(parsed.path, fds['1'])
            stdout.connect()
            stderr = _CommandWebsocketClient(
                manager, self.client.websocket_url)
            stderr.resource = '{}?secret={}'.format(parsed.path, fds['2'])
            stderr.connect()

            manager.start()

            while len(manager.websockets.values()) > 0:
                time.sleep(.1)

            operation = self.client.operations.get(operation_id)

            return _ContainerExecuteResult(
                operation.metadata['return'], stdout.data, stderr.data)

    def migrate(self, new_client, wait=False):
        """Migrate a container.

        Destination host information is contained in the client
        connection passed in.

        If the container is running, it either must be shut down
        first or criu must be installed on the source and destination
        machines.
        """
        if self.api.scheme in ('http+unix',):
            raise ValueError('Cannot migrate from a local client connection')

        return new_client.containers.create(
            self.generate_migration_data(), wait=wait)

    def generate_migration_data(self):
        """Generate the migration data.

        This method can be used to handle migrations where the client
        connection uses the local unix socket. For more information on
        migration, see `Container.migrate`.
        """
        self.sync()  # Make sure the object isn't stale
        response = self.api.post(json={'migration': True})
        operation = self.client.operations.get(response.json()['operation'])
        operation_url = self.client.api.operations[operation.id]._api_endpoint
        secrets = response.json()['metadata']['metadata']
        cert = self.client.host_info['environment']['certificate']

        return {
            'name': self.name,
            'architecture': self.architecture,
            'config': self.config,
            'devices': self.devices,
            'epehemeral': self.ephemeral,
            'default': self.profiles,
            'source': {
                'type': 'migration',
                'operation': operation_url,
                'mode': 'pull',
                'certificate': cert,
                'secrets': secrets,
            }
        }

    def publish(self, public=False, wait=False):
        """Publish a container as an image.

        The container must be stopped in order publish it as an image. This
        method does not enforce that constraint, so a LXDAPIException may be
        raised if this method is called on a running container.

        If wait=True, an Image is returned.
        """
        data = {
            'public': public,
            'source': {
                'type': 'container',
                'name': self.name,
            }
        }

        response = self.client.api.images.post(json=data)
        if wait:
            operation = self.client.operations.wait_for_operation(
                response.json()['operation'])

            return self.client.images.get(operation.metadata['fingerprint'])

class _CommandWebsocketClient(WebSocketBaseClient):  # pragma: no cover
    def __init__(self, manager, *args, **kwargs):
        self.manager = manager
        super(_CommandWebsocketClient, self).__init__(*args, **kwargs)

    def handshake_ok(self):
        self.manager.add(self)
        self.buffer = []

    def received_message(self, message):
        if len(message.data) == 0:
            self.close()
            self.manager.remove(self)
        if message.encoding:
            self.buffer.append(message.data.decode(message.encoding))
        else:
            self.buffer.append(message.data.decode('utf-8'))

    @property
    def data(self):
        return ''.join(self.buffer)

class _StdinWebsocket(WebSocketBaseClient):  # pragma: no cover
    """A websocket client for handling stdin.

    The nature of stdin in Container.execute means that we don't
    ever use this connection. It is closed as soon as it completes
    the handshake.
    """

    def handshake_ok(self):
        self.close()

class Snapshot(model.Model):
    """A container snapshot."""

    name = model.Attribute()
    created_at = model.Attribute()
    stateful = model.Attribute()

    container = model.Parent()

    @property
    def api(self):
        return self.client.api.containers[
            self.container.name].snapshots[self.name]

    @classmethod
    def get(cls, client, container, name):
        response = client.api.containers[
            container.name].snapshots[name].get()

        snapshot = cls(
            client, container=container,
            **response.json()['metadata'])
        # Snapshot names are namespaced in LXD, as
        # container-name/snapshot-name. We hide that implementation
        # detail.
        snapshot.name = snapshot.name.split('/')[-1]
        return snapshot

    @classmethod
    def all(cls, client, container):
        response = client.api.containers[container.name].snapshots.get()

        return [cls(
                client, name=snapshot.split('/')[-1],
                container=container)
                for snapshot in response.json()['metadata']]

    @classmethod
    def create(cls, client, container, name, stateful=False, wait=False):
        response = client.api.containers[container.name].snapshots.post(json={
            'name': name, 'stateful': stateful})

        snapshot = cls(client, container=container, name=name)
        if wait:
            client.operations.wait_for_operation(response.json()['operation'])
        return snapshot

    def rename(self, new_name, wait=False):
        """Rename a snapshot."""
        response = self.api.post(json={'name': new_name})
        if wait:
            self.client.operations.wait_for_operation(
                response.json()['operation'])
        self.name = new_name

    def publish(self, public=False, wait=False):
        """Publish a snapshot as an image.

        If wait=True, an Image is returned.

        This functionality is currently broken in LXD. Please see
        https://github.com/lxc/lxd/issues/2201 - The implementation
        here is mostly a guess. Once that bug is fixed, we can verify
        that this works, or file a bug to fix it appropriately.
        """
        data = {
            'public': public,
            'source': {
                'type': 'snapshot',
                'name': '{}/{}'.format(self.container.name, self.name),
            }
        }

        response = self.client.api.images.post(json=data)
        if wait:
            operation = self.client.operations.wait_for_operation(
                response.json()['operation'])
            return self.client.images.get(operation.metadata['fingerprint'])


import pandas as pd
import requests
from bs4 import BeautifulSoup

ds = pd.read_excel("WKTlength_test.xlsx")  # 讀取excel檔

for URL in ds['Concat']:  # 批次讀取網址
    response = requests.get(URL[5:])  # 爬蟲
    soup = BeautifulSoup(response.text, "html.parser")  # 設定原始碼條件"div", ...
    div_web = soup.find_all("div", class_="mapdata") and soup.find_all("div", id="map_google3_1")
    div_str = "".join(map(str, div_web))  # 轉換為string

    if ',"pos":[{' in div_str and div_str.count(',"pos":[{') == 1:  # 標示為Linestring
        # print(URL[:5] + ": is Linestring.")
        L_startsplit = div_str.split(',"pos":')  # 去除非坐標訊息
        for L_1 in L_startsplit:
            if L_1.startswith('[{"lat":'):
                L_2 = L_1.split(',"polygons":')
                for L_3 in L_2:
                    if L_3.startswith('[{"lat":') and L_3.endswith('}]'):
                        L_str = L_3.replace('[', '').replace('{"lat":', '') \
                            .replace(',"lon":', ',').replace('},', '; ').replace('}]}]', '')
                        Lturn_1 = L_str.split('; ')
                        my_Linestring = ''
                        for Lturn_2 in Lturn_1:  # 經緯度交換
                            Lturn_3 = Lturn_2.split(',')
                            Lturn_4 = Lturn_3[1] + ' ' + Lturn_3[0]
                            my_Linestring += str(Lturn_4 + ", ")  # 整合WKT格式
                        result_2 = (URL[:5] + ":LINESTRING (" + my_Linestring[:-2] + ")")

                        if len(result_2) >= 32762:  # WKT文字太長標記
                            print(URL[:5] + ": Too long. " + str(len(result_2)))

                        elif len(result_2) < 32762:
                            print(result_2)

    elif ',"pos":[{' in div_str and div_str.count(',"pos":[{') > 1:  # 標示為Multilinestring
        # print(URL[:5] + ": is Multilinestring.")
        M_step1 = div_str.split(',"pos":')  # 去除前段非坐標訊息
        Mmid_final = ''  # 整合最後一段以外的Linestring預留
        Mend_list = ''  # 為了加入最後一段的Linestring預留
        for M_step2 in M_step1:
            if M_step2.startswith('[{"lat":'):
                if M_step2.endswith(',"strokeWeight":"2"'):  # 去除中段非坐標訊息
                    M_step3_mid = M_step2.split(',{"text":')
                    for M_step4_mid in M_step3_mid:
                        if M_step4_mid.startswith('[{"lat":'):
                            # print(M_step4_mid)
                            M_str_mid = M_step4_mid.replace('[', '').replace('{"lat":', '') \
                                .replace(',"lon":', ',').replace('},', '; ').replace('}]}', '')
                            M_turn1_mid = M_str_mid.split('; ')
                            my_Mmid = ''
                            for M_turn2_mid in M_turn1_mid:  # 經緯度交換
                                M_turn3_mid = M_turn2_mid.split(',')
                                M_turn4_mid = M_turn3_mid[1] + ' ' + M_turn3_mid[0]
                                my_Mmid += str(M_turn4_mid + ", ")
                            Mmid_list = "(" + my_Mmid[:-2] + "), "  # 整合WKT格式
                            # print(Mmid_list)
                            for concat in Mmid_list:  # 最後一段以外的Linestring整併
                                Mmid_final += concat

                elif M_step2.endswith('</div>'):  # 去除後段非坐標訊息
                    M_step3_end = M_step2.split('],"polygons":')
                    for M_step4_end in M_step3_end:
                        if M_step4_end.startswith('[{"lat":'):
                            # print(M_step4_end)
                            M_str_end = M_step4_end.replace('[', '').replace('{"lat":', '') \
                                .replace(',"lon":', ',').replace('},', '; ').replace('}]}', '')
                            M_turn1_end = M_str_end.split('; ')
                            my_Mend = ''
                            for M_turn2_end in M_turn1_end:  # 經緯度交換
                                M_turn3_end = M_turn2_end.split(',')
                                M_turn4_end = M_turn3_end[1] + ' ' + M_turn3_end[0]
                                my_Mend += str(M_turn4_end + ", ")
                            Mend_list = ("(" + my_Mend[:-2] + "))")  # 整合WKT格式
                            # print(Mend_list)

        result_3 = (URL[:5] + ":MULTILINESTRING (" + Mmid_final + Mend_list)  # 加最後一段Linestring

        if len(result_3) >= 32762:  # WKT文字太長標記
            print(URL[:5] + ": Too long. " + str(len(result_3)))

        elif len(result_3) < 32762:
            print(result_3)

print("Finish!")

Web Crawler WKT and check string length.py


""" Copied from  """

import torch

class L2Normalization(torch.nn.Module):
    def __init__(self, alpha=1.0):
        super(L2Normalization, self).__init__()
        self.alpha = alpha

    def forward(self, v):
        # v_ = v / norm * alpha
        # norm = sqrt(norm_sq)
        # norm_sq = sum(v ** 2)
        v_sq= torch.pow(v, 2)

        norm_sq = torch.sum(v_sq, dim=1).add(1e-5)
        norm = torch.sqrt(norm_sq).view(-1,1).expand_as(v)

        v_ = v.div(norm).mul(self.alpha)

        return v_


import sys
from collections import deque

input = sys.stdin.readline
N = int(input())
blocks=[list(map(int,input().split())) for _ in range(N)] # t, x, y,
# t=1 1x1,   t=2 (x,y)(x,y+1),    t=3 (x,y)(x+1,y)

green=deque([[0,0,0,0] for _ in range(6)])
blue=deque([[0,0,0,0] for _ in range(6)])
score=0

def pb():
    for i in range(3,-1,-1):
        for j in range(6):
            print(blue[j][i], end=" ")
        print()
    print()

def pg():
    for a in green:
        print(a)
    print()

def move(type,board,t,x,y):
    x=0
    if t == 1:
        while x<6 and board[x][y] == 0 :
            x+=1
        board[x-1][y]=1

    elif t == 2:
        if type =='g':  #green
            while x < 6 and board[x][y] == 0 and board[x][y + 1] == 0:
                x+=1
            board[x - 1][y] = 1
            board[x - 1][y + 1] = 1
        else:           #blue
            while x < 5 and board[x][y] == 0 and board[x+1][y] == 0:
                x+=1
            board[x-1][y] = 1
            board[x][y] = 1

    elif t == 3:
        if type =='g':  #green
            while x < 5 and board[x][y] == 0 and board[x+1][y] == 0 :
                x+=1
            board[x - 1][y] = 1
            board[x][y] = 1
        else:           #blue
            x+=1
            while x < 6 and board[x][y] == 0 and board[x][y-1] == 0:
                x+=1
            board[x-1][y-1] = 1
            board[x-1][y] = 1

def check(board):
    global score
    for i in range(2,6):
        if 0 not in board[i]:
            del board[i]
            board.appendleft([0,0,0,0])
            score+=1

    for i in range(0,2):
        if 1 in board[i]:
            board.pop()
            board.appendleft([0,0,0,0])

def solve(t,x,y):
    move('g',green,t,x,y)
    move("b",blue, t,y,3-x)
    check(green)
    check(blue)

def count():
    cnt=0
    for i in range(2,6):
        for j in range(4):
            cnt+=blue[i][j]
            cnt+=green[i][j]
    return cnt
for t,x,y in blocks:

    solve(t,x,y)
print(score)
print(count())


def compute_probs(neg,pos):
  p0 = neg/(neg+pos)
  p1 = pos/(neg+pos)
  return [p0,p1]

def test_it():
  return 'loaded'

def cond_prob(table, evidence, evidence_value, target, target_value):
  t_subset = up_table_subset(table, target, 'equals', target_value)
  e_list = up_get_column(t_subset, evidence)
  p_b_a = sum([1 if v==evidence_value else 0 for v in e_list])/len(e_list)
  return p_b_a + .01

def cond_probs_product(table, evidence_values, target_column, target_val):
  table_columns = up_list_column_names(table)
  evidence_columns = table_columns [:-1]
  evidence_complete = up_zip_lists (evidence_columns, evidence_values)
  cond_prob_list = []
  for evidence_column, evidence_val in evidence_complete:
    cond_prob_value = cond_prob (table, evidence_column, evidence_val, target_column, target_val)
    cond_prob_list += [cond_prob_value]
  partial_numerator = up_product (cond_prob_list)
  return partial_numerator

def prior_prob (table, target, target_value):
  t_list = up_get_column(table, target)
  p_a = sum([1 if v==target_value else 0 for v in t_list])/len(t_list)
  return p_a

def naive_bayes(table, evidence_row, target):
  target_value = 0
  result1 = cond_probs_product(table, evidence_row, target, target_value)* prior_prob(table, target, target_value)
  target_value = 1
  result2 = cond_probs_product(table, evidence_row, target, target_value)* prior_prob(table, target, target_value)
  neg, pos = compute_probs(result1, result2)
  return [neg, pos]

def metrics (zipped_list):
  assert isinstance(zipped_list, list), f'parameter must be a list'
  assert all([isinstance(item, list) for item in zipped_list]), f'parameter must be a list of lists'
  assert all([len(item)==2 for item in zipped_list]), f'parameter must be a zipped list'
  assert all([isinstance(a, (int, float)) and isinstance(b, (int, float)) for a,b in zipped_list]), f'each value must be an int or a float'
  assert all(a >=0 and b>=0 for a,b in zipped_list), f'each value must be >= 0'
  tn = sum([1 if pair==[0,0] else 0 for pair in zipped_list])
  tp = sum([1 if pair==[1,1] else 0 for pair in zipped_list])
  fp = sum([1 if pair==[1,0] else 0 for pair in zipped_list])
  fn = sum([1 if pair==[0,1] else 0 for pair in zipped_list])
  precision = tp/(tp + fp) if tp + fp > 0 else 0
  recall = tp/(tp + fn) if tp + fn > 0 else 0
  f1 = (2* precision * recall) / (precision + recall) if precision + recall > 0 else 0
  accuracy = (tp + tn) / (tn+tp+fn+fp) if (tn+tp+fn+fp) >0 else 0
  return {'Precision': precision, 'Recall': recall, 'F1': f1, 'Accuracy': accuracy}

from sklearn.ensemble import RandomForestClassifier  #make sure this makes it into your library
def run_random_forest(train, test, target, n):
  X = up_drop_column(train, target)
  y = up_get_column(train,target)
  k_feature_table = up_drop_column(test, target)
  k_actuals = up_get_column(test, target)
  clf = RandomForestClassifier(n_estimators=n, max_depth=2, random_state=0)
  clf.fit(X, y)  #builds the trees as specified above
  probs = clf.predict_proba(k_feature_table)
  pos_probs = [p for n,p in probs]  #probs is list of [neg,pos] like we are used to seeing.
  pos_probs[:5]
  all_mets = []
  for t in thresholds:
    all_predictions = [1 if pos>t else 0 for pos in pos_probs]
    pred_act_list = up_zip_lists(all_predictions, k_actuals)
    mets = metrics(pred_act_list)
    mets['Threshold'] = t
    all_mets = all_mets + [mets]
  all_mets[:2]
  metrics_table = up_metrics_table(all_mets)
  metrics_table
  print(metrics_table)  #output we really want - to see the table
  return None

def try_archs(full_table, target, architectures, thresholds):
  train_table, test_table = up_train_test_split(full_table, target, .4)
  for arch in all_architectures:
    all_results = up_neural_net(train_table, test_table, arch, target)
  all_mets = []
  for t in thresholds:
    all_predictions = [1 if pos>=t else 0 for neg,pos in all_results]
    pred_act_list = up_zip_lists(all_predictions, up_get_column(test_table, target))
    mets = metrics(pred_act_list)
    mets['Threshold'] = t
    all_mets += [mets]
    all_mets[:2]
  print(f'Architecture: {arch}')
  print(up_metrics_table(all_mets))
  return None
  print(up_metrics_table(all_mets))


from scipy.linalg import sqrtm
import random
from Pauli import *
from metrics.density_runner import apply_channel, run_by_matrices, ops
import math
from typing import List, Iterable, Any, Dict, Optional

def trace_norm(m1: np.ndarray, m2: np.ndarray) -> float:
    diff = complex(1, 0) * (m1 - m2)
    return np.trace(sqrtm(diff @ diff.transpose().conjugate())).real / 2

def random_state(dim: int) -> np.ndarray:
    # might want to put a different distribution on this, idk
    c = np.array([complex(random.random() - 0.5, random.random() - 0.5) for _ in range(dim)])
    squared_modulus = c @ c.conjugate()
    return c / (squared_modulus ** 0.5)

def pure_density_from_state(state: np.ndarray) -> np.ndarray:
    return np.outer(state.conjugate(), state)

def random_densities(dim: int, n_trials: int) -> List[np.ndarray]:
    return [sum([r * pure_density_from_state(random_state(dim)) for r in np.random.dirichlet(np.ones(dim), size=1)[0]])
            for _ in range(n_trials)]

def monte_carlo_f_algorithm(channel1: Iterable[np.ndarray], channel2: Iterable[np.ndarray], n_trials: int) -> float:
    # channels: a list of numpy arrays (Kraus matrices)
    dim = channel1[0].shape[0]
    max_norm = max([trace_norm(apply_channel(channel1, xi), apply_channel(channel2, xi)) for xi in
                    random_densities(dim, n_trials)])
    for k in range(int(math.log(dim, 2))):
        channel1 = [np.kron(e, np.eye(2)) for e in channel1]
        channel2 = [np.kron(e, np.eye(2)) for e in channel2]
        with_ancilla = max([trace_norm(apply_channel(channel1, xi), apply_channel(channel2, xi)) for xi in
                            random_densities(2 ** (k + 1) * dim, n_trials)])
        max_norm = max(max_norm, with_ancilla)
    return max_norm

def monte_carlo_from_circuit(circuit_string: Iterable[Any], unitary: np.ndarray, n_trials: int,
                             noise_channels: Optional[Iterable] = None,
                             circuit_key: Dict[Any, np.ndarray] = None) -> float:
    if circuit_key is None:
        circuit_key = ops
    dim = unitary.shape[0]
    max_norm = max([trace_norm(run_by_matrices(circuit_string, xi, noise_channels, circuit_key), apply_channel([unitary], xi))
                    for xi in random_densities(dim, n_trials)])
    for k in range(int(math.log(dim, 2))):
        circuit_key = {i: np.kron(v, np.eye(2)) for i, v in circuit_key.items()}
        unitary = np.kron(unitary, np.eye(2))
        noise_channels = [[np.kron(e, np.eye(2)) for e in c] for c in noise_channels]
        with_ancilla = max([trace_norm(run_by_matrices(circuit_string, xi, noise_channels, circuit_key),
                            apply_channel([unitary], xi)) for xi in random_densities(2 ** (k + 1) * dim, n_trials)])
        max_norm = max(max_norm, with_ancilla)
    return max_norm

def effect_of_noise(circuit_description: Iterable[Any], noise_channels: Iterable,  n_qubits: int = 1,
                    circuit_key: Optional[Dict[Any, np.ndarray]] = None) -> float:
    d = 2 ** n_qubits
    unitary = np.eye(d)
    for s in circuit_description[::-1]:
        unitary = circuit_key[s] @ unitary
    return monte_carlo_from_circuit(circuit_description, unitary, 100, noise_channels, circuit_key=circuit_key)


import urllib.request
import json

def printResults(data):
    theJSON = json.loads(data)
    if "title" in theJSON["metadata"]:
        print(theJSON["metadata"]["title"])
    count = theJSON["metadata"]["count"]
    print(str(count) + "events recorded")
    for i in theJSON["features"]:
        print(i["properties"]["place"])
    print("----------------- \n")
    for i in theJSON["features"]:
        if i["properties"]["mag"] >= 4.0:
            print(str("%2.1f")% i["properties"]["mag"], i["properties"]["place"])
            print("-------------------\n")
    print("Events that were felt:\n")
    for i in theJSON["features"]:
        feltReports = i["properties"]["felt"]
        if feltReports != None:
            if feltReports >= 0:
                print(str("%2.1f")% i["properties"]["mag"], i["properties"]["place"], "reported " + str(feltReports) + " times")

def main():
    urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
    webUrl = urllib.request.urlopen(urlData)
    print(webUrl.getcode())
    if (webUrl.getcode() == 200):
        data = webUrl.read()
        printResults(data)
    else:
        print("Receive error, can not parse results.")

if __name__=="__main__":
    main()


from django import template
register = template.Library()

@register.filter(name="getObjectCount")
def getObjectCount(value):
    """
    Бүртгэлтэй төхөөрөмжүүд эсвэл хэрэглэгчдийг тоолох
    return integer - төхөөрөмжийн тоо эсвэл хэрэглэгчийн тоо
    """
    if value == "user" or value == "User":
        user_list = User.objects.raw('SELECT * FROM auth_user AS u INNER JOIN dashboard_profile AS p ON u.id = p.user_id WHERE p.deleted=False')
        return len(user_list)
    elif value == "device" or value == "Device":
        device_list = models.Device.objects.filter(deleted=False)
        return len(device_list)

@register.filter(name="getCurrentUserField")
def getCurrentUsername(user, arg):
    """
    Нэвтэрсэн хэрэглэгчийн field_name буюу талбарын утгыг буцаана
    """


import logging
from abc import (
    ABC,
    abstractmethod,
)
from collections import defaultdict
from typing import List
from uuid import uuid4

import numpy as np
import matplotlib.pyplot as plt

class NoBanditsError(Exception):
    ...

class Bandit:
    def __init__(self, m: float, lower_bound: float = None, upper_bound: float = None):
        """
        Simulates bandit.
        Args:
            m (float): True mean.
            lower_bound (float): Lower bound for rewards.
            upper_bound (float): Upper bound for rewards.
        """

        self.m = m
        self.lower_bound = lower_bound
        self.upper_bound = upper_bound
        self.id = uuid4()

    def pull(self):
        """
        Simulate pulling the arm of the bandit.
        Normal distribution with mu = self.m and sigma = 1. If lower_bound or upper_bound are defined then the
        distribution will be truncated.
        """
        n = 10
        possible_rewards = np.random.randn(n) + self.m

        allowed = np.array([True] * n)
        if self.lower_bound is not None:
            allowed = possible_rewards >= self.lower_bound
        if self.upper_bound is not None:
            allowed *= possible_rewards <= self.upper_bound

        return possible_rewards[allowed][0]


from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends, status

from app.common.di import AppContainer
from app.common.schemas import BaseResponse
from app.domain.proxy.schemas import ProxiesAddRequest, ProxiesResponse
from app.domain.proxy.service import ProxyService

router = APIRouter(prefix="/proxies", tags=["proxy"])

provide_proxy_service = Provide[AppContainer.proxy_service]

@router.get("", description="프록시 목록 조회 API", response_model=ProxiesResponse)
@inject
async def proxies(proxy_service: ProxyService = Depends(provide_proxy_service)) -> dict:
    proxies = await proxy_service.get_proxies()
    return {"proxies": proxies}

@router.post("/{proxy}", description="프록시 추가 API", status_code=status.HTTP_201_CREATED)
@inject
async def add_proxy(proxy: str, proxy_service: ProxyService = Depends(provide_proxy_service)) -> BaseResponse:
    await proxy_service.add_proxy(proxy)
    return BaseResponse()

@router.post("", description="프록시 벌크 추가 API", status_code=status.HTTP_201_CREATED)
@inject
async def add_proxies(
    request: ProxiesAddRequest, proxy_service: ProxyService = Depends(Provide[AppContainer.proxy_service])
) -> BaseResponse:
    await proxy_service.add_proxies(request.proxies)
    return BaseResponse()

@router.delete("/{proxy}", description="프록시 삭제 API", status_code=status.HTTP_204_NO_CONTENT)
@inject
async def delete_proxy(proxy: str, proxy_service: ProxyService = Depends(Provide[AppContainer.proxy_service])) -> None:
    await proxy_service.delete_proxy(proxy)


import random
import math
import numpy as np
import matplotlib.pyplot as plt

class Node:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.parent = None
        self.cost = 0

class RRTStar:
    def __init__(self, start, goal, circle_obstacle_list, poly_obstacle_list, rand_area, max_iter=1000, expand_dist=0.2, goal_sample_rate=5, max_distance=1.0):
        self.start = Node(start[0], start[1])
        self.goal = Node(goal[0], goal[1])
        self.min_rand_x = rand_area[0]
        self.max_rand_x = rand_area[1]
        self.min_rand_y = rand_area[2]
        self.max_rand_y = rand_area[3]

        self.circle_obstacle_list = circle_obstacle_list
        self.poly_obstacle_list = poly_obstacle_list
        self.obstacle_list = circle_obstacle_list
        self.max_iter = max_iter
        self.expand_dist = expand_dist
        self.goal_sample_rate = goal_sample_rate
        self.max_distance = max_distance

    def plan(self):
        self.node_list = [self.start]
        for i in range(self.max_iter):
            rnd = self.get_random_node()                                # Get a random node
            nearest_index = self.get_nearest_node_index(rnd)            # Find the nearest node index
            nearest_node = self.node_list[nearest_index]                # Get the nearest node

            new_node = self.steer(nearest_node, rnd)                    # Steer from the random node based on the nearest node

            if self.check_collision(new_node, self.circle_obstacle_list, self.poly_obstacle_list):          # Check if the new node collides with an obstacle
                near_indices = self.find_near_nodes(new_node)                                               # Find the near nodes in a radius of max_distance
                node_with_min_cost = self.choose_parent(new_node, near_indices)                             # Choose the parent node with the lowest cost
                self.node_list.append(new_node)                                                             # Add the new node to the node list
                self.rewire(node_with_min_cost, new_node, near_indices)                                     # Rewire the tree if a better path is found

            # if i % 20 == 0:
            #     self.draw_graph(rnd)                                                                        # Draw the graph every 5 iterations

        last_index = self.get_best_last_index()                                                             # Get the index of the node with the lowest cost
        if last_index is None:
            return None

        path = self.generate_final_course(last_index)                                                       # Generate the final path
        return path

    # Returns a random node within the specified bounds.
    def get_random_node(self):
        if random.randint(0, 100) > self.goal_sample_rate:
            rnd = Node(random.uniform(self.min_rand_x, self.max_rand_x),
                       random.uniform(self.min_rand_y, self.max_rand_y))
        else:
            rnd = Node(self.goal.x, self.goal.y)

        return rnd

    # Connect two nodes with a line segment and calculate the cost
    def steer(self, from_node, to_node):
        new_node = Node(from_node.x, from_node.y)
        d = math.sqrt((to_node.x - new_node.x) ** 2 + (to_node.y - new_node.y) ** 2)
        theta = math.atan2(to_node.y - new_node.y, to_node.x - new_node.x)
        new_node.x += self.expand_dist * math.cos(theta)
        new_node.y += self.expand_dist * math.sin(theta)
        new_node.parent = from_node
        new_node.cost = from_node.cost + d
        return new_node

    # Chooses the parent node that results in the minimum cost for a given node.
    def choose_parent(self, new_node, near_indices):
        if len(near_indices) == 0:
            return None

        costs = []
        for i in near_indices:
            near_node = self.node_list[i]
            if self.check_collision(new_node, self.obstacle_list, self.poly_obstacle_list):
                d = math.sqrt((near_node.x - new_node.x) ** 2 + (near_node.y - new_node.y) ** 2)
                cost = near_node.cost + d
                costs.append(cost)
            else:
                costs.append(float("inf"))

        min_cost = min(costs)
        if min_cost == float("inf"):
            return None

        min_index = near_indices[costs.index(min_cost)]
        new_node.cost = min_cost
        new_node.parent = self.node_list[min_index]
        return self.node_list[min_index]

    # Rewires the tree by updating the parent and cost of nearby nodes if a better path is found.
    def rewire(self, node_with_min_cost, new_node, near_indices):
        for i in near_indices:
            near_node = self.node_list[i]
            d = math.sqrt((near_node.x - new_node.x) ** 2 + (near_node.y - new_node.y) ** 2)
            scost = new_node.cost + d
            if near_node.cost > scost and self.check_collision(new_node, self.circle_obstacle_list, self.poly_obstacle_list):
                near_node.parent = new_node
                near_node.cost = scost
                self.propagate_cost_to_leaves(near_node)

    # Propagates the cost of a node to all of its descendants.
    def propagate_cost_to_leaves(self, parent_node):
        for node in self.node_list:
            if node.parent == parent_node:
                d = self.node_distance(node, parent_node)
                node.cost = parent_node.cost + d
                self.propagate_cost_to_leaves(node)

    # Returns the indices of nodes that are within a certain distance of a given node.
    def find_near_nodes(self, new_node):
        n = len(self.node_list) + 1
        r = self.max_distance * math.sqrt((math.log(n) / n))
        indices = []
        for i, node in enumerate(self.node_list):
            if self.node_distance(node, new_node) <= r:
                indices.append(i)
        return indices

    # Find the index of the node in the tree that is closest to a given node
    def get_nearest_node_index(self, node):
        dlist = [(n.x - node.x) ** 2 + (n.y - node.y) ** 2 for n in self.node_list]
        min_index = dlist.index(min(dlist))

        return min_index

    # Check if a node collides with any obstacle in the obstacle list
    def check_collision(self, node, circle_obstacle_list, poly_obstacle_list):
        for i, obstacle in enumerate(poly_obstacle_list):
            x, y = node.x, node.y
            n = len(obstacle)
            p1x, p1y = obstacle[0]
            if_Safe_poly = True
            for i in range(n + 1):
                p2x, p2y = obstacle[i % n]
                if y > min(p1y, p2y):
                    if y <= max(p1y, p2y):
                        if x <= max(p1x, p2x):
                            if p1y != p2y:
                                x_inters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                            if p1x == p2x or x <= x_inters:
                                if_Safe_poly = not if_Safe_poly
                p1x, p1y = p2x, p2y
            if if_Safe_poly == False:
                break

        for (ox, oy, size) in circle_obstacle_list:
            IF_Safe_circle = True
            dx = ox - node.x
            dy = oy - node.y
            d = math.sqrt(dx ** 2 + dy ** 2)
            if d <= size:
                IF_Safe_circle = False  # collision
                break

        IF_Safe = IF_Safe_circle and if_Safe_poly
        return IF_Safe

    def get_best_last_index(self):
        goal_node = Node(self.goal.x, self.goal.y)
        n = len(self.node_list)
        r = self.max_distance * math.sqrt((math.log(n) / n))
        near_goal_indices = []
        for i, node in enumerate(self.node_list):
            if self.node_distance(node, goal_node) <= r:
                near_goal_indices.append(i)

        if len(near_goal_indices) == 0:
            return None

        min_cost = min([self.node_list[i].cost for i in near_goal_indices])
        for i in near_goal_indices:
            if self.node_list[i].cost == min_cost:
                return i
        return None

    # Generate the final path from the start to the goal
    def generate_final_course(self, goal_index):
        path = [[self.goal.x, self.goal.y]]
        node = self.node_list[goal_index]
        while node.parent is not None:
            path.append([node.x, node.y])
            node = node.parent
        path.append([self.start.x, self.start.y])
        return path

    # Distance between points and nodes
    def distance(self, n1, n2):
        return math.sqrt((n1.x - n2[0]) ** 2 + (n1.y - n2[1]) ** 2)

    # Distance between two nodes
    def node_distance(self, n1, n2):
        return math.sqrt((n1.x - n2.x) ** 2 + (n1.y - n2.y) ** 2)

    # Draw the RRT tree
    def draw_graph(self, rnd=None):
        plt.clf()
        if rnd is not None:
            plt.plot(rnd.x, rnd.y, "^k")

        for node in self.node_list:
            if node.parent:
                plt.plot([node.x, node.parent.x], [node.y, node.parent.y], "-g", linewidth=0.2)

        for (ox, oy, size) in self.circle_obstacle_list:
            self.plot_circle(ox, oy, size)

        for i, obstacle in enumerate(self.poly_obstacle_list):
            self.plot_polygon(obstacle, 'darkgray')

        plt.plot(self.start.x, self.start.y, "xr")
        plt.plot(self.goal.x, self.goal.y, "xr")
        plt.axis([self.min_rand_x, self.max_rand_x, self.min_rand_y, self.max_rand_y])
        plt.grid(True)
        plt.pause(0.01)

    # Plot a circle obstacle
    def plot_circle(self, x, y, size):
        deg = list(range(0, 360, 5))
        deg.append(0)
        xl = [x + size * math.cos(math.radians(d)) for d in deg]
        yl = [y + size * math.sin(math.radians(d)) for d in deg]
        plt.plot(xl, yl)

    # Define functions to plot obstacles
    def plot_polygon(self, polygon, color):
        x, y = zip(*polygon)
        plt.fill(x, y, color=color)

def main():
    start = (-2, -2)
    goal = (8, 6)
    circle_obstacle_list = [(2, 2, 1), (3, 3, 1), (4, 4, 1)]

    # Define the area
    rand_area = [-5, 15, -5, 15]
    static_obstacles = [
                        [(2, 2), (2, 8), (3, 8), (3, 3), (8, 3), (8, 2)],
                        [(6, 6), (7, 6), (7, 7), (6, 7)]
                        ]
    rrt_star = RRTStar(start, goal, circle_obstacle_list, static_obstacles, rand_area)
    path = rrt_star.plan()
    if path is None:
        print("Cannot find path")
    else:
        print("Found path")
        print(path)
        rrt_star.draw_graph()
        plt.plot([x for (x, y) in path], [y for (x, y) in path], color='blue', linewidth=3)
        plt.show()

if __name__ == '__main__':
    main()


from modules.agents import REGISTRY as agent_REGISTRY
from modules.comm import REGISTRY as comm_REGISTRY
from components.action_selectors import REGISTRY as action_REGISTRY
import math
import torch as th
import torch.distributions as D
import torch.nn.functional as F
import numpy as np
import random

# This multi-agent controller shares parameters between agents
class TarCommMAC:
	def __init__(self, scheme, groups, args):
		self.n_agents = args.n_agents
		self.args = args
		self.comm_embed_dim = args.comm_embed_dim
		input_shape_for_comm = 0
		if args.comm:
			input_shape, input_shape_for_comm = self._get_input_shape(scheme)
		else:
			input_shape = self._get_input_shape(scheme)

		self._build_agents(input_shape)
		self.agent_output_type = args.agent_output_type

		self.action_selector = action_REGISTRY[args.action_selector](args)

		self.hidden_states = None

		if args.comm:
			self.comm = comm_REGISTRY[self.args.comm_method](input_shape_for_comm, args)

		self.is_print_once = False

		if self.args.env_args['print_rew']:
			self.c_step = 0
			self.cut_mean_num_list = []

	def select_actions(self, ep_batch, t_ep, t_env, bs=slice(None), test_mode=False, thres=0., prob=0.):
		# Only select actions for the selected batch elements in bs
		avail_actions = ep_batch["avail_actions"][:, t_ep]
		# if self.args.comm:
		#     agent_outputs, (_, _), _, _ = self.forward(ep_batch, t_ep, test_mode=test_mode)
		# else:
		agent_outputs = self.forward(ep_batch, t_ep, test_mode=test_mode, thres=thres, prob=prob)
		chosen_actions = self.action_selector.select_action(agent_outputs[bs], avail_actions[bs], t_env,
		                                                    test_mode=test_mode)
		return chosen_actions

	def forward(self, ep_batch, t, test_mode=False, thres=0., prob=0.):
		agent_inputs = self._build_inputs(ep_batch, t)

		if self.args.comm:
			(_, _), messages, _ = self._communicate(ep_batch.batch_size, agent_inputs,
			                                        self.hidden_states.detach(), test_mode,
			                                        thres=thres, prob=prob)
			agent_inputs = th.cat([agent_inputs, messages], dim=1)

		avail_actions = ep_batch["avail_actions"][:, t]
		agent_outs, self.hidden_states = self.agent(agent_inputs, self.hidden_states)

		# Softmax the agent outputs if they're policy logits
		# is not used in qmix
		if self.agent_output_type == "pi_logits":

			if getattr(self.args, "mask_before_softmax", True):
				# Make the logits for unavailable actions very negative to minimise their affect on the softmax
				reshaped_avail_actions = avail_actions.reshape(ep_batch.batch_size * self.n_agents, -1)
				agent_outs[reshaped_avail_actions == 0] = -1e10

			agent_outs = th.nn.functional.softmax(agent_outs, dim=-1)
			if not test_mode:
				# Epsilon floor
				epsilon_action_num = agent_outs.size(-1)
				if getattr(self.args, "mask_before_softmax", True):
					# With probability epsilon, we will pick an available action uniformly
					epsilon_action_num = reshaped_avail_actions.sum(dim=1, keepdim=True).float()

				agent_outs = ((1 - self.action_selector.epsilon) * agent_outs
				              + th.ones_like(agent_outs) * self.action_selector.epsilon / epsilon_action_num)

				if getattr(self.args, "mask_before_softmax", True):
					# Zero out the unavailable actions
					agent_outs[reshaped_avail_actions == 0] = 0.0

		# shape = (bs, self.n_agents, -1)
		return agent_outs.view(ep_batch.batch_size, self.n_agents, -1)

	def init_hidden(self, batch_size):
		self.hidden_states = self.agent.init_hidden().unsqueeze(0).expand(batch_size, self.n_agents, -1)  # bav

	def parameters(self):
		return list(self.agent.parameters()) + list(self.comm.parameters())

	def load_state(self, other_mac):
		self.agent.load_state_dict(other_mac.agent.state_dict())
		self.comm.load_state_dict(other_mac.comm.state_dict())

	def cuda(self):
		self.agent.cuda()
		self.comm.cuda()

	def save_models(self, path):
		th.save(self.agent.state_dict(), "{}/agent.th".format(path))
		th.save(self.comm.state_dict(), "{}/comm.th".format(path))

	def load_models(self, path):
		self.agent.load_state_dict(th.load("{}/agent.th".format(path), map_location=lambda storage, loc: storage))
		self.comm.load_state_dict(th.load("{}/comm.th".format(path), map_location=lambda storage, loc: storage))

	def _build_agents(self, input_shape):
		self.agent = agent_REGISTRY[self.args.agent](input_shape, self.args)

	def _build_inputs(self, batch, t):
		# Assumes homogenous agents with flat observations.
		# Other MACs might want to e.g. delegate building inputs to each agent
		# shape = (bs * self.n_agents, -1)
		bs = batch.batch_size
		inputs = []
		inputs.append(batch["obs"][:, t])  # b1av
		if self.args.obs_last_action:
			if t == 0:
				inputs.append(th.zeros_like(batch["actions_onehot"][:, t]))
			else:
				inputs.append(batch["actions_onehot"][:, t - 1])
		if self.args.obs_agent_id:
			inputs.append(th.eye(self.n_agents, device=batch.device).unsqueeze(0).expand(bs, -1, -1))

		inputs = th.cat([x.reshape(bs * self.n_agents, -1) for x in inputs], dim=1)
		return inputs

	def _get_input_shape(self, scheme):
		# shape = (bs * self.n_agents, -1 + self.comm_embed_dim * self.n_agents)
		input_shape = scheme["obs"]["vshape"]
		ms_shape = 0
		if self.args.obs_last_action:
			input_shape += scheme["actions_onehot"]["vshape"][0]
		if self.args.obs_agent_id:
			input_shape += self.n_agents
		if self.args.comm:
			ms_shape = self.comm_embed_dim

		if self.args.comm:
			return input_shape + ms_shape, input_shape
		else:
			return input_shape

	def _cut_alpha(self, attn, thres=0., prob=0.):
		if self.args.is_cur_mu:
			s_num = 0
			attn = attn.detach().cpu()
			attn_2 = attn.detach().numpy()
			for i in range(self.n_agents):
				for j in range(self.n_agents):
					if i != j:
						if attn_2[0, i, j] <= thres or random.random() < prob:
							attn[0, i, j] = 0.
							s_num += 1
			attn = attn.cuda()
			self.c_step += 1
			self.cut_mean_num_list.append(1. * s_num / (self.n_agents * (self.n_agents - 1) + 1e-3))
			if self.c_step >= self.args.env_args['print_steps'] and not self.is_print_once:
				print('cut num ratio:', np.mean(self.cut_mean_num_list))
				self.c_step = 0
				self.is_print_once = True
		elif thres > 0:
			attn = attn.detach().cpu()
			index = np.argsort(abs(attn).reshape(-1))
			rank = np.zeros_like(index)
			for i, item in enumerate(index):
				rank[item] = i
			rank = 100. * rank / rank.size
			rank = th.Tensor(rank.reshape(attn.shape))
			attn[rank < thres] = 0.
			attn = attn.cuda()
		return attn

	def _communicate(self, bs, inputs, hidden_states, test_mode, thres=0., prob=0.):
		# shape = (bs * self.n_agents, -1)
		inputs = th.cat([inputs, hidden_states.view(bs * self.n_agents, -1)], dim=1).detach()
		message, signature, query = self.comm(inputs)

		message = message.view(bs, self.n_agents, -1)
		signature = signature.view(bs, self.n_agents, -1)
		query = query.view(bs, self.n_agents, -1)

		# Concatenate and weighting message

		if test_mode:
			all_m = message.clone()
			scores = th.matmul(query, signature.transpose(-2, -1)) / math.sqrt(self.args.comm_embed_dim)
			attn = F.softmax(scores, dim=-1)
			attn = self._cut_alpha(attn, thres=thres, prob=prob)
			comm = th.matmul(attn, all_m)
			comm = comm.reshape(bs * self.n_agents, -1)
			return (None, None), comm, None
		else:
			all_m = message.clone()
			scores = th.matmul(query, signature.transpose(-2, -1)) / math.sqrt(self.args.comm_embed_dim)
			attn = F.softmax(scores, dim=-1)
			comm = th.matmul(attn, all_m)
			comm = comm.reshape(bs * self.n_agents, -1)
			return (None, None), comm, None

	def clean(self):
		if self.args.env_args['print_rew']:
			self.c_step = 0
			self.cut_mean_num_list = []
			self.is_print_once = False


from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.datasets import load_files
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Task 1.3
#load the BBC text data into a training set
path = input("Enter the path to the directory of the dataset you would like to analyze: (Ex: \'BBC\')\n")
try:
    bbc_train = load_files(path,encoding='latin1')
except:
    print("File path was not valid.")
    exit(0)
# Task 1.2
n, bins, patches = plt.hist(bbc_train.target, color='blue', edgecolor='black')
plt.title('Distribution of Instances in Each Class')
xticks = [0] * len(bbc_train.target_names)
for i in range(len(xticks)):
    xticks[i] = i
plt.xticks(xticks, labels=bbc_train.target_names)
plt.savefig('BBC-distribution.pdf')

# Task 1.4
#initialize a count vectorizer for the data
count_vect = CountVectorizer()

# Task 1.5 Split dataset into 80% training and 20% test
X_train, X_test, y_train, y_test = train_test_split(bbc_train.data, bbc_train.target, test_size=0.2, train_size=0.8, random_state=None)
X_train_counts = count_vect.fit_transform(X_train)

# Task 1.6 Train a multinomial Naive Bayes Classifier on the training set
text_clf = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', MultinomialNB()),
])
text_clf.fit(X_train, y_train)
y_pred = text_clf.predict(X_test)

# The following section is used to help answer questions 1.7.e, 1.7.f, 1.7.i, 1.7.j, 1.7.k
vocab_size = len(count_vect.get_feature_names())
# Get tokens in entire corpus (train and test set)
num_tokens = sum(sum(X_train_counts.toarray())) + sum(sum(count_vect.fit_transform(X_test).toarray()))
print(num_tokens)
class_sums = [0] * len(bbc_train.target_names)
class_zeros = [0] * len(bbc_train.target_names)
class_docs = [0, 0, 0, 0, 0]
for x in y_train:
    class_docs[x] += 1
print(len(y_train))
priors = list(zip(bbc_train.target_names, [x/len(y_train) for x in class_docs]))

# The indices of each of the inner lists corresponds to the indices of the list count_vect.get_feature_names()
class_frequencies = [
    [0] * vocab_size,
    [0] * vocab_size,
    [0] * vocab_size,
    [0] * vocab_size,
    [0] * vocab_size
]
corpus_frequencies = [0] * vocab_size
i = 0
# Add up the frequencies for each word based on class and whole corpus
print('Processing lots of data, this may take a while...')
for row, category in zip(X_train_counts.toarray(), y_train):
    for index, word_freq in enumerate(row):
        class_frequencies[category][index] += word_freq
        corpus_frequencies[index] += word_freq
    class_sums[category] += row.sum()
class_sums = list(zip(bbc_train.target_names, class_sums))

num_ones =  sum([1 for freq in corpus_frequencies if freq == 1])
freq_one = (num_ones, num_ones/num_tokens)

# Writes different statistics about the model to a file
def add_to_file(title, file, y_test, y_pred):
    file.write(f'(a) ******************************** {title} ********************************\n')
    file.write('\n(b) Confusion Matrix:\n')
    file.write(str(confusion_matrix(y_test, y_pred)))
    file.write('\n\n(c)(d) Classification Report:\n')
    file.write(str(classification_report(y_test, y_pred, target_names=bbc_train.target_names)))
    file.write('\n(e) Prior Probability:\n')
    for name, prob in priors:
        file.write(f'{name}: {prob}\n')
    file.write(f'\n(f) Size of vocabulary: {vocab_size} different words')
    file.write('\n\n(g) Number of word tokens in each class:\n')
    for name, num in class_sums:
        file.write(f'{name}: {num}\n')
    file.write(f'\n(h) Number of word tokens in total: {num_tokens}\n')
    file.write('\n(i) Number of words with frequency 0 in each class:\n')
    i = 0
    for name, num in class_sums:
        file.write(f'{name}:\n')
        num_zeros =  sum([1 for freq in class_frequencies[i] if freq == 0])
        freq_zeros = (num_zeros, num_zeros/num)
        file.write(f'words: {freq_zeros[0]} percentage: {freq_zeros[1]}\n')
        i += 1
    file.write('\n(j) Number of words with frequency 1 in the corpus:\n')
    file.write(f'words: {freq_one[0]} percentage: {freq_one[1]}\n')
    file.write(f'\n(k) Log Prob of the word \'programme\':\n')
    logProb = np.log(corpus_frequencies[count_vect.get_feature_names().index('programme')]/num_tokens)/np.log(2)
    file.write(f'{str(logProb)}')
    file.write(f'\nLog Prob of the word \'laptops\':\n')
    logProb = np.log(corpus_frequencies[count_vect.get_feature_names().index('laptops')]/num_tokens)/np.log(2)
    file.write(f'{str(logProb)}\n\n\n')

# Open bbc-performance.txt to write to
with open('bbc-performance.txt', 'w') as file:
    # Task 1.7
    title = "MultinomialNB default values, try 1"
    print(f'Writing to file, {title}')
    add_to_file(title, file, y_test, y_pred)

    # Task 1.8
    text_clf = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', MultinomialNB()),
    ])
    text_clf.fit(X_train, y_train)
    y_pred = text_clf.predict(X_test)
    title = "MultinomialNB default values, try 2"
    print(f'Writing to file, {title}')
    add_to_file(title, file, y_test, y_pred)

    # Task 1.9
    text_clf = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', MultinomialNB(alpha=0.0001)),
    ])
    text_clf.fit(X_train, y_train)
    y_pred = text_clf.predict(X_test)
    title = "MultinomialNB default values, try 3"
    print(f'Writing to file, {title}')
    add_to_file(title, file, y_test, y_pred)

    # Task 1.10
    text_clf = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', MultinomialNB(alpha=0.9)),
    ])
    text_clf.fit(X_train, y_train)
    y_pred = text_clf.predict(X_test)
    title = "MultinomialNB default values, try 4"
    print(f'Writing to file, {title}')
    add_to_file(title, file, y_test, y_pred)
    print('Finished!')


"""
This script takes a CSV file and sums all numeric values in columns for rows with a specified
header, mapping them to a single row with a new replacement header.

Input:
########
    * -i : CSV file to sum as a list

    * -v : The index of the headers.

    * -d : Mapping dictionary
        * *key*: value to sum to,
        * *value*: values to sum

Output:
########
    * Summed CSV file

Usage:
########
    * python csv_value_replacer.py --version

    *This is the option that show you the program's version.*

    * python csv_value_replacer.py-h

    *This can show you some help information.*

    * python csv_value_replacer.py -i <csv_file_list.txt> -v <header_index> -d <mapping_dict.txt>

    *Runs the program given the specified CSV file.*

"""
import sys
from argparse import ArgumentParser
import ast

__version__ = "1.0"

def main():
    """Sum CSV file"""
    usage = "\n%prog  [options]"
    parser = ArgumentParser(description=usage)
    parser.add_argument("-i", "--csvfile", action="store", dest="csvfile",
                        help="Name of CSV file [as list] to be summed")
    parser.add_argument("-v", "--header_index", action="store", dest="header_index",
                        help="Header Index")
    parser.add_argument("-d", "--mapping_dict", action="store", dest="mapping_dict",
                        help="Name of file containing mapping_dict")
    parser.add_argument('--version', '-V', action='version',
                        version="%(prog)s " + __version__)

    options = parser.parse_args()

    # First argument: index of the header value
    try:
        header_index = int(options.header_index)
    except ValueError:
        print("Invalid header index")
        parser.print_help()
        sys.exit(0)

    # Second argument: name of the mapping file
    try:
        mapping_dict = ast.literal_eval(open(options.mapping_dict).read())
    except FileNotFoundError:
        print("invalid mapping dict")
        parser.print_help()
        sys.exit(0)

    # Third argument:
    try:
        csv_as_list = ast.literal_eval(open(options.csvfile).read())
    except FileNotFoundError:
        print("Invalid csv")
        parser.print_help()
        sys.exit(0)

    # Summed lines to add to new CSV file
    # Key: Replacement value
    # Value: Line to sum
    lines_to_append = {}

    # List containing all contents for new CSV file
    new_csv_file = []

    for row in csv_as_list:
        # If row contains data to be summed
        if row[header_index] in mapping_dict:
            # If there currently is no line replacement row, initialize one
            if mapping_dict[row[header_index]] not in lines_to_append:
                # If the row contains the data to sum to
                if row[header_index].strip(" ") == mapping_dict[row[header_index]]:
                    lines_to_append[mapping_dict[row[header_index]]] = row
                else:
                    # Create blank line
                    lines_to_append[mapping_dict[row[header_index]]] = [""] * len(row)
                    for index, value in enumerate(row):
                        if isinstance(value, float):
                            lines_to_append[mapping_dict[row[header_index]]][index] = value
                        else:
                            lines_to_append[mapping_dict[row[header_index]]][index] = ""
                lines_to_append[mapping_dict[row[header_index]]][1] = \
                    mapping_dict[row[header_index]]
            # else, there is already an instance of a replacement row
            else:
                # For each item in the row
                for index, value in enumerate(row):
                    # check if value is a float
                    if isinstance(value, float):
                        # sum it to current value in that column
                        lines_to_append[mapping_dict[row[header_index]]][index] += float(value)
                    # If item is non float and belongs to replacement value
                    elif row[header_index] == mapping_dict[row[header_index]]:
                        # Set text in replacement line to be text from replacement value
                        lines_to_append[mapping_dict[row[header_index]]][index] = value

        # If data does not contain a value to be summed, just add the new row to the new CSV
        # contents
        else:
            new_csv_file.append(row)

    # Add new lines at the end of the CSV contents
    for value in lines_to_append:
        new_csv_file.append((lines_to_append[value]))

    # Print contents to csv_file
    for line in new_csv_file:
        print(",".join((str(v) for v in line)))

if __name__ == "__main__":
    main()


from osc_lib.command import command
from openstackclient.i18n import _
from osc_lib import utils
from openstackclient_plugin.pepper.pepper_utils import PepperWrapper

def get_dhcp_agent_host(manager, network_id):
    dhcp_ports = manager.ports(network_id=network_id, device_owner="network:dhcp")
    for port in dhcp_ports:
        return port.binding_host_id

class PingServer(command.Command):
    def get_parser(self, prog_name):
        parser = super(PingServer, self).get_parser(prog_name)
        parser.add_argument(
            "server",
            metavar="<server>",
            help=_("Server to add the port to (name or ID)"),
        )
        return parser

    def take_action(self, parsed_args):
        """
        ping server from dhcp namespace
        :param parsed_args:
        :return:
        """

        compute_client = self.app.client_manager.compute
        network_client = self.app.client_manager.network
        server = utils.find_resource(compute_client.servers, parsed_args.server)
        executor = PepperWrapper()

        for port in network_client.ports(device_id=server.id):
            fixed_ips = port.fixed_ips
            network_id = port.network_id
            host = get_dhcp_agent_host(network_client, network_id)

            for ip in fixed_ips:
                address = ip.get('ip_address', None)
                if address:
                    print(executor.execute('%s*' % host, 'ip netns exec qdhcp-%s ping -c 3 %s' % (network_id, address)))


"""
94. 二叉树的中序遍历
简单
1.9K
相关企业
给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。

示例 1：

输入：root = [1,null,2,3]
输出：[1,3,2]
示例 2：

输入：root = []
输出：[]
示例 3：

输入：root = [1]
输出：[1]

提示：

树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100

进阶: 递归算法很简单，你可以通过迭代算法完成吗？

"""

from typing import Optional, List
from DataStructure.BinaryTree import *

class Solution:
    # 递归求解
    def inorder(self, root: Optional[TreeNode], res: list):
        if root is None:
            return
        self.inorder(root.left, res)
        res.append(root.val)
        self.inorder(root.right, res)

    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        self.inorder(root, res)
        return res

if __name__ == '__main__':
    sol = Solution()
    cases = [
        (TreeNode([1, None, 2, 3]), [1, 3, 2]),
        (TreeNode([]), []),
        (TreeNode([1]), [1]),
    ]
    for idx, case in enumerate(cases):
        res = list2Tree(sol.inorderTraversal(case[0]))
        res = tree2List(res)
        print(f"case_id: {idx} || {case[1] == res} || {case[1]} || {res} ")


import pandas as pd
import os
import openai
import re
from tqdm import tqdm
import numpy as np
import argparse
import config
import warnings
warnings.filterwarnings("ignore")

parser = argparse.ArgumentParser(description='\
Generates a dataset given line-separated list of idioms. \
Requires prompt.txt to be in local directory.')

parser.add_argument('-f','--file',
    help='Comma-separated list, having a list of idioms as one of its columns',
    required=True
)
parser.add_argument('-s','--sample',
    help='Number of samples. Samples all if not specified.',
    type=int, default=-1
)
parser.add_argument('-r', '--random',
    help='Random seed. Does not randomize rows if not specified.',
    type=int, default=-1
)
parser.add_argument('-o', '--output',
    help='Output file name. Must use the extension ".csv".',
    default='output_idiom.csv'
)
parser.add_argument('-i', '--iter',
    help='The number of iteration',
    type=int, default=1
)

args = parser.parse_args()

if not os.path.isfile(args.file):
    raise Exception('The specified idiom file does not exist.')
if not os.path.isfile('PROMPT_generate_pairs.txt'):
    raise Exception('"PROMPT_generate_pairs.txt" must exist in local directory.')
if args.output.split('.')[-1] != 'csv':
    raise Exception('Use ".csv" extension for your output file name.')

with open('PROMPT_generate_pairs.txt', 'r') as file:
    general_prompt = file.read()
    print()
    print()
    print("CURRENT PROMPT:")
    print()
    print(general_prompt + '[queried idiom]\n')
    print()

df = pd.read_csv(args.file)
subdf = df.drop_duplicates(subset=['idiom']).reset_index(drop=True)
if args.random >= 0:
    subdf = subdf.sample(frac=1, random_state=args.random).reset_index(drop=True)
if args.sample >= 0:
    subdf = subdf[:args.sample]

openai.organization = config.openai_organization
openai.api_key = config.openai_api_key

def generate(row):
    try:
        return generate_helper(row)
    except:
        return generate_helper(row)

def generate_helper(row):
    idiom = row['idiom']
    text = openai.Completion.create(
        model="text-davinci-003",
        prompt = general_prompt + f"\"{idiom}\"\n",
        max_tokens=400,
        temperature=1, # the higher this value, the less deterministic
        top_p=1, # the higher this value, the wider range of vocab is used
    ).choices[0].text.strip()

    phrase = re.search('ambiguous phrase: (.+?)\n', text)
    if phrase: row['subsentence'] = phrase.group(1).strip('\"')

    label = []
    sentence = []
    reason = []

    A = re.search('figurative: (.+?)\n', text)
    if A:
        A = A.group(1)
        A = A.split(' Here, ')
        sentence.append(A[0].strip('\"'))
        label.append(0)
        if len(A) > 1:
            reason.append('Here, ' + A[1])

    label += [np.nan] * (1 - len(label))
    sentence += [np.nan] * (1 - len(sentence))
    reason += [np.nan] * (1 - len(reason))

    B = re.search('literal: (.+?)$', text)
    if B:
        B = B.group(1)
        B = B.split(' Here, ')
        sentence.append(B[0].strip('\"'))
        label.append(1)
        if len(B) > 1:
            reason.append('Here, ' + B[1])

    label += [np.nan] * (2 - len(label))
    sentence += [np.nan] * (2 - len(sentence))
    reason += [np.nan] * (2 - len(reason))

    row['label'] = label
    row['sentence'] = sentence
    row['reason'] = reason
    return row

print('INFERENCE IN PROGRESS...')
tqdm.pandas()
subdf_gpt = pd.DataFrame(columns=['idx', 'idiom', 'meaning', 'subsentence', 'sentence', 'label', 'reason'])
for i in range(args.iter):
    subdf_gpt_local = subdf.progress_apply(generate, axis=1)
    subdf_gpt_local = subdf_gpt_local.explode(['label', 'sentence', 'reason']).reset_index(drop=True)
    print(subdf_gpt_local)
    subdf_gpt_local = subdf_gpt_local[['idiom', 'meaning', 'subsentence', 'sentence', 'label', 'reason']]
    subdf_gpt_local['idx'] = 0
    subdf_gpt_local['idx'][::2] = subdf_gpt_local.index[::2] * args.iter + 2 * i
    subdf_gpt_local['idx'][1::2] = subdf_gpt_local.index[::2] * args.iter + 2 * i + 1
    subdf_gpt = pd.concat([subdf_gpt, subdf_gpt_local])
subdf_gpt = subdf_gpt.set_index('idx').sort_index(ascending=True)
subdf_gpt.to_csv(args.output, index=False)
print('COMPLETED.')


#!/usr/bin/env python

#
# Requries WMI and pywin32 modules. Install with
# ```
# pip install wmi
# pip install pywin32
# ````
#

import wmi, logging

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)

c = wmi.WMI()

process_watcher = c.Win32_Process.watch_for("creation")

while True:
    new_process = process_watcher()
    processName = new_process.Caption
    processId   = new_process.ProcessId
    logging.info("Process started. Name: %s | PID: %d", processName, processId)


# encoding: utf-8
from __future__ import absolute_import
import types
import requests
import sys
import pdef
import pdef.descriptors
from pdef.invoke import Invocation
from pdefc.lang import TypeEnum

try:
    # Python 2.7
    import httplib as http_codes
except ImportError:
    # Python 3
    import http.client as http_codes

GET = 'GET'
POST = 'POST'
UTF8 = 'utf-8'
APPLICATION_JSON_CONTENT_TYPE = 'application/json; charset=utf-8'
FORM_URLENCODED_MIME_TYPE = 'application/x-www-form-urlencoded'
TEXT_PLAIN_CONTENT_TYPE = 'text/plain; charset=utf-8'

def rpc_client(interface, url, session=None):
    '''Create an RPC client.'''
    return RpcClient(interface, url, session=session)

def rpc_handler(interface, service):
    '''Create an RPC handler.'''
    return RpcHandler(interface, service)

def wsgi_app(handler):
    '''Create a WSGI RPC server.'''
    return WsgiRpcApp(handler)

class RpcException(Exception):
    def __init__(self, status, message=None):
        super(RpcException, self).__init__(message)
        self.message = message
        self.status = status

class RpcRequest(object):
    def __init__(self, method=GET, path='', query=None, post=None):
        self.method = method
        self.path = path
        self.query = dict(query) if query else {}
        self.post = dict(post) if post else {}

    def __str__(self):
        return '%s %s' % (self.method, self.path)

    def __repr__(self):
        return '<RpcRequest %s %s>' % (self.method, self.path)

    @property
    def is_post(self):
        return self.method == POST

class RpcProtocol(object):
    def __init__(self, jsonformat=None):
        self.jsonformat = jsonformat or pdef.jsonformat

    def get_request(self, invocation):
        if not invocation:
            raise ValueError('Invocation required')

        method = invocation.method
        if not method.is_terminal:
            raise ValueError('Last invocation method must be terminal')

        request = RpcRequest()
        request.method = POST if method.is_post else GET
        for inv in invocation.to_chain():
            self._write_invocation(request, inv)

        return request

    def _write_invocation(self, request, invocation):
        method = invocation.method
        kwargs = invocation.kwargs

        post = request.post
        query = request.query
        path = '/' + method.name

        for argd in method.args:
            name = argd.name
            kwarg = kwargs.get(name)
            value = self._to_json(kwarg, argd.type)

            if argd.is_post:
                post[name] = value
            elif argd.is_query:
                query[name] = value
            else:
                path += '/' + urlencode(value)

        request.path += path

    def _to_json(self, kwarg, descriptor):
        '''Serialize a kwarg to json, strip quotes.'''
        s = self.jsonformat.write(kwarg, descriptor)
        return s.strip('"')

    def get_invocation(self, request, interface_descriptor):
        '''Parse an invocation from an rpc request using an interface descriptor.'''
        if not request:
            raise ValueError('Request required')
        if not interface_descriptor:
            raise ValueError('Interface descriptor required')

        invocation = None
        parts = request.path.strip('/').split('/')

        while parts:
            part = parts.pop(0)

            # Find a method by a name.
            method = interface_descriptor.find_method(part)
            if not method:
                raise RpcException(http_codes.BAD_REQUEST, 'Method not found')

            # Check the required HTTP method.
            if method.is_post and not request.is_post:
                raise RpcException(http_codes.METHOD_NOT_ALLOWED, 'Method not allowed, POST required')

            # Parse keyword arguments.
            kwargs = self._read_kwargs(method, parts, request.query, request.post)

            # Create a root invocation,
            # or a next invocation in a chain.
            invocation = invocation.next(method, **kwargs) \
                if invocation else Invocation(method, kwargs=kwargs)

            if method.is_terminal:
                break

            # It's an interface method.
            # Get the next interface descriptor and proceed parsing the parts.
            interface_descriptor = method.result

        if parts:
            # No more interface descriptors in a chain, but the parts are still present.
            raise RpcException(http_codes.BAD_REQUEST, 'Failed to parse an invocation chain')

        if not invocation:
            raise RpcException(http_codes.BAD_REQUEST, 'Methods required')

        if not invocation.method.is_terminal:
            raise RpcException(http_codes.BAD_REQUEST, 'The last method must be a terminal one. '
                                                       'It must return a data type or be void.')

        return invocation

    def _read_kwargs(self, method, parts, query, post):
        kwargs = {}

        for argd in method.args:
            name = argd.name

            if argd.is_post:
                value = post.get(name)
            elif argd.is_query:
                value = query.get(name)
            elif not parts:
                raise RpcException(http_codes.NOT_FOUND, 'Wrong number of method args: "%s"'
                                                      % method.name)
            else:
                value = urldecode(parts.pop(0))

            arg = self._from_json(value, argd.type)
            kwargs[name] = arg

        return kwargs

    def _from_json(self, s, descriptor):
        if s is None:
            return None

        type0 = descriptor.type
        if type0 in (TypeEnum.STRING, TypeEnum.DATETIME, TypeEnum.ENUM):
            if not (s.startswith('"') and s.endswith('"')):
                # Return the quotes to get a valid json string.
                s = '"' + s + '"'

        return self.jsonformat.read(s, descriptor)

class RpcClient(object):
    def __init__(self, interface, url, session=None, protocol=None):
        if not interface:
            raise ValueError('Interface required')
        if not url:
            raise ValueError('Url required')

        self.interface = interface
        self.interface_descriptor = interface.descriptor

        self.url = url
        self.session = session or requests.session()
        self.protocol = protocol or RpcProtocol()

    def proxy(self):
        return pdef.proxy(self.interface, self)

    def __call__(self, invocation):
        if not invocation:
            raise ValueError('Invocation required')

        rpc_request = self.protocol.get_request(invocation)

        method = invocation.method
        resultd = method.result
        excd = self.interface_descriptor.exc

        request = self._build_request(rpc_request)
        return self._send(request, resultd, excd)

    def _build_request(self, rpc_request):
        url = self._build_url(rpc_request.path)
        return requests.Request(method=rpc_request.method,
                                url=url,
                                data=rpc_request.post,
                                params=rpc_request.query)

    def _build_url(self, path):
        return self.url + path

    def _send(self, request, resultd, excd=None):
        session = self.session
        prepared = request.prepare()
        response = session.send(prepared)
        return self._parse_response(response, resultd, excd)

    def _parse_response(self, response, resultd, excd=None):
        code = response.status_code

        if code not in (http_codes.OK, http_codes.UNPROCESSABLE_ENTITY):
            # It's an HTTP error.
            return self._parse_error(response)

        # It's a successful rpc result.
        text = response.text

        # Create a generic rpc result class.
        result_class = rpc_result_class(resultd, excd)
        result = result_class.from_json(text)

        if code == http_codes.OK:
            return result.data
        else:
            exc = result.error or RpcException(code, 'Unsupported application exception')
            raise exc

    def _parse_error(self, response):
        try:
            text = response.text
        except Exception as e:
            text = 'Failed to get the response text, e=%s' % e

        # Limit the text to use in as an exception description.
        text = text if len(text) < 255 else text[:255]
        raise RpcException(response.status_code, text)

class RpcHandler(object):
    def __init__(self, interface, service, protocol=None):
        if not interface:
            raise ValueError('Interface required')
        if not service:
            raise ValueError('Service required')
        self.interface = interface
        self.interface_descriptor = interface.descriptor
        self.service = service
        self.protocol = protocol or RpcProtocol()

    def __call__(self, rpc_request):
        return self.handle(rpc_request)

    def handle(self, rpc_request):
        '''Handle an rpc request and return a tuple (is_successful, rpc_result).'''
        if not rpc_request:
            raise ValueError('Rpc request required')

        invocation = self.protocol.get_invocation(rpc_request, self.interface_descriptor)

        method = invocation.method
        datad = method.result
        excd = self.interface_descriptor.exc
        result_class = rpc_result_class(datad, excd)

        try:
            data = invocation.invoke(self.service)
            return True, result_class(data)
        except Exception as e:
            if excd and isinstance(e, excd.pyclass):
                # It's an expected application exception.
                return False, result_class(data=None, error=e)

            # Not an application exception, reraise it.
            raise

class WsgiRpcApp(object):
    '''WSGI RPC application.'''
    def __init__(self, handler):
        if not handler:
            raise ValueError('Handler required')
        self.handler = handler

    def __call__(self, environ, start_response):
        return self.handle(environ, start_response)

    def handle(self, environ, start_response):
        request = self._parse_request(environ)
        try:
            success, result = self.handler(request)
        except RpcException as e:
            status = e.status or http_codes.INTERNAL_SERVER_ERROR
            content = e.message or 'Internal server error'
            return self._response(start_response, status, content)

        status_code = http_codes.OK if success else http_codes.UNPROCESSABLE_ENTITY
        content = result.to_json(indent=True)
        return self._response(start_response, status_code, content,
                              content_type=APPLICATION_JSON_CONTENT_TYPE)

    def _parse_request(self, env):
        '''Create an http server request from a wsgi request.'''
        method = env['REQUEST_METHOD']
        path = env['PATH_INFO']
        query = self._read_wsgi_query(env)
        post = self._read_wsgi_post(env)

        return RpcRequest(method, path=path, query=query, post=post)

    def _read_wsgi_query(self, env):
        if 'QUERY_STRING' not in env:
            return {}
        s = env['QUERY_STRING']
        return self._parse_query(s)

    def _read_wsgi_post(self, env):
        ctype = env.get('CONTENT_TYPE', '')
        clength = self._read_wsgi_clength(env)

        body = None
        if clength > 0 and ctype.lower().startswith(FORM_URLENCODED_MIME_TYPE):
            body = env['wsgi.input'].read(clength)

        if not body:
            return {}

        if sys.version > '3':
            body = body.decode(UTF8)

        return self._parse_query(body)

    def _read_wsgi_clength(self, env):
        clength = env.get('CONTENT_LENGTH') or 0
        try:
            return int(clength)
        except (ValueError, TypeError):
            return 0

    def _parse_query(self, s):
        d = parse_query(s)
        result = {}
        for key, values in d.items():
            result[key] = values[0] if values else ''
        return result

    def _response(self, start_response, status_code, unicode_content, content_type=None):
        reason = http_codes.responses.get(status_code)
        status = '%s %s' % (status_code,  reason)

        content = unicode_content.encode(UTF8)
        content_type = content_type or TEXT_PLAIN_CONTENT_TYPE
        headers = [('Content-Type', content_type),
                   ('Content-Length', str(len(content)))]

        start_response(status, headers)
        return [content]

def rpc_result_class(datad, excd=None):
    '''Create a generic RpcResult class with a given data and exception descriptors.'''
    class RpcResult(pdef.Message):
        data = pdef.descriptors.field('data', datad)
        error = pdef.descriptors.field('error', excd or pdef.descriptors.string0)
        descriptor = pdef.descriptors.message(lambda: RpcResult, fields=[data, error])

        has_data = data.has_property
        has_error = error.has_property

        def __init__(self, data=None, error=None):
            self.data = data
            self.error = error

    return RpcResult

try:
    # Python 2.7
    import urllib
    import urlparse

    def urlencode(s, safe='[]{},.-"'):
        if isinstance(s, types.UnicodeType):
            s = s.encode(UTF8)

        if isinstance(safe, types.UnicodeType):
            safe = safe.encode(UTF8)

        result = urllib.quote_plus(s, safe)
        return result.decode(UTF8)

    def urldecode(s):
        if isinstance(s, types.UnicodeType):
            s = s.encode(UTF8)

        result = urllib.unquote_plus(s)
        return result.decode(UTF8)

    def parse_query(s):
        if isinstance(s, types.UnicodeType):
            s = s.encode(UTF8)

        q = urlparse.parse_qs(s)

        # Decode the result to unicode.
        result = {}
        for key, values in q.items():
            k = key.decode(UTF8)
            vv = [v.decode(UTF8) for v in values] if values else []
            result[k] = vv

        return result

except ImportError:
    # Python 3
    import urllib.parse

    def urlencode(s, safe='[]{},.-"'):
        return urllib.parse.quote_plus(s, safe, encoding=UTF8)

    def urldecode(s):
        return urllib.parse.unquote_plus(s, encoding=UTF8)

    def parse_query(s):
        return urllib.parse.parse_qs(s)


# -*- coding: utf-8 -*-
import pyaudio
import sys
import time
import wave
import requests
import os
import json
import numpy as np
from datetime import datetime
import subprocess
import pathlib

# 音声録音
def recognize_rec():

    chunk = 1024
    threshold = 0.002
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 16000
    STOP_SECONDS = 1  # レコード後の無音区間の継続時間
    RECORD_SECONDS = 10 # レコード時間の最大値

    A = int(RATE) / int(chunk) * int(RECORD_SECONDS)
    B = int(RATE / chunk * STOP_SECONDS)
    p = pyaudio.PyAudio()

    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    frames_per_buffer = chunk)

    print("RATE = " + str(RATE))
    print("chunk = " + str(chunk))
    print("RECORD_SECONDS = " + str(RECORD_SECONDS))
    print("RATE / chunk * RECORD_SECONDS = " + str(A))
    print("")

    cnt = 0

    while True:
        data = stream.read(chunk)
        x = np.frombuffer(data, dtype="int16") / 32768.0

        #まずサンプルを取る！
        xmax = x.max()

        if xmax > threshold: #録音レベルがしきい値を上回ったら
            filename = datetime.today().strftime("%Y%m%d%H%M%S") + ".wav"
            print(cnt, filename)
            all = []
            n = 0
            for _ in range(0, int(A)):
                data = stream.read(chunk)
                all.append(data)
                x = np.frombuffer(data, dtype="int16") / 32768.0
                xmax = x.max()

                if xmax <= threshold:
                    n += 1
                else:
                    n = 0
                # 中断判定
                if n == B:
                    break

            data = b''.join(all)
            out = wave.open("result/" + filename,'w')
            out.setnchannels(1) #mono
            out.setsampwidth(2) #16bits
            out.setframerate(RATE)
            out.writeframes(data)
            out.close()
            cnt += 1

        if cnt > 5 :
            print("Recording done.")
            break

    stream.close()
    p.terminate()

# result内のwavファイル名を取得
# scriptファイルに hoge.wav hoge.mfc ... を書き込み
def script_hcopy():

    wavefiles = []
    all = pathlib.Path('2019_12_result/').glob('*.wav')
    for f in all:
        wavefiles.append(f.name)

    fname = "2019_12_result/script.hcopy"
    fname2 = "input.sh"

    file = open(fname, 'w')
    file2 = open(fname2 ,'w')

    for i in range(len(wavefiles)):
        tmp = wavefiles[i].replace('.wav', '')
        file.write('2019_12_result/' + tmp + '.wav 2019_12_result/' + tmp + '.mfc\n')
        file2.write('HList -r 2019_12_result/' + tmp + '.mfc > 2019_12_MFCC_DATA/' + tmp + '.txt\n')

    file.close()

    print("Writing done.")

# HCopy 実行
def hcopy():
    subprocess.run('HCopy -C 2019_12_result/config.hcopy -S 2019_12_result/script.hcopy', shell=True)
    print("HCopy done.")

def hlist():
    subprocess.run('bash input.sh', shell=True)
    print("HList done.")

if __name__ == '__main__':

    # print(recognize_rec())
    # script_hcopy()
    # hcopy()
    hlist()


import pygame

class Obstacle(pygame.sprite.Sprite):
    def __init__(self, program, x, y, image):
        super().__init__()
        self.program = program
        self.x = x
        self.y = y
        self._image = image
        self.image = self._image
        _x = self.image.get_width()
        _y = self.image.get_height()
        self.rect = self.image.get_rect(topleft=(x * _x, y * _y))
        self.feet = self.rect

    def _draw_rect(self):
        rect = self.program.camera.update_rect(self.rect)
        pygame.draw.rect(self.program.screen, (255, 0, 0), rect, 3)

    def update(self):
        self.display()

    def display(self):
        self.program.camera.camera_draw(self.image, self.rect.topleft)

        DEV = 0
        if DEV:
            self._draw_rect()


from tasksbase import *

__package__ = "allrobotpy"

from .hal import _tasks as hal_tasks
from .halsim_ds_socket import _tasks as halsim_ds_socket_tasks
from .halsim_gui import _tasks as halsim_gui_tasks
from .halsim_ws import _tasks as halsim_ws_tasks
from .ntcore import _tasks as ntcore_tasks
from .romi import _tasks as romi_tasks
from .wpilib import _tasks as wpilib_tasks
from .wpimath import _tasks as wpimath_tasks
from .wpinet import _tasks as wpinet_tasks
from .wpiutil import _tasks as wpiutil_tasks

everything = [
    hal_tasks,
    halsim_ds_socket_tasks,
    halsim_gui_tasks,
    halsim_ws_tasks,
    ntcore_tasks,
    romi_tasks,
    wpilib_tasks,
    wpimath_tasks,
    wpinet_tasks,
    wpiutil_tasks,
]

namespace = Collection()

for module in everything:
    namespace.add_collection(
        Collection.from_module(module, name=module.__package__.split(".")[-1])
    )

allow_dot_subdir_commands(namespace)

@task(pre=[t.develop for t in everything])
def develop(ctx: Context):
    ...

@task(pre=[t.build_wheel for t in everything])
def build_wheel(ctx: Context):
    ...

@task(pre=[t.test for t in everything])
def test(ctx: Context):
    ...

@task(pre=[build_wheel, test])
def build_and_test(ctx: Context):
    ...

@task(pre=[t.clean for t in everything])
def clean(ctx: Context):
    ...

@task
def generate_github_actions(ctx: Context):
    generate_github_actions_()

@task
def python(ctx: Context):
    subprocess.run("python", cwd=Path(ctx.cwd, "nothing/").resolve())

namespace.add_task(develop)  # type: ignore
namespace.add_task(build_wheel)  # type: ignore
namespace.add_task(test)  # type: ignore
namespace.add_task(build_and_test)  # type: ignore
namespace.add_task(clean)  # type: ignore
namespace.add_task(generate_github_actions)  # type: ignore
namespace.add_task(python)  # type: ignore

namespace.configure(
    {
        "python": sys.executable,
        "parallel": True,
        # "parallel": False,
        "cc_launcher": "ccache",
        # "cc_launcher": "",
        "macos_target": "11",
        "strip_libpython": True,
        # "skip_installed_deps": True,
        "skip_installed_deps": False,
        # "no_generation": True,
        "no_generation": False,
        "run": {
            # "hide": "both",
            # "hide": False,
            "echo": True
        },
    }
)


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

import os
import sys
import time

import torch
import numpy as np

sys.path.insert(1, os.path.join("C:\\", "Users", "Soham", "Desktop", "CovNet", "source_codes"))

import CovNetworks as CN
import Important_functions as Ifn
import Other_functions as Ofn

cwd = os.getcwd()
print(cwd)

import current_setup as setup

loss_fn = Ifn.loss_COV

method = input("Model [Shallow/Deepshared/Deep]: ")
if method.lower() == "shallow":
    depths = [1] # for compatibility
    Rs = [5,10,20,40,80] # choice of R's
elif method.lower() == 'deepshared' or method.lower() == 'deep':
    depths = [2,3,4] # choice of depths
    Rs = [5,10,20,40] # choice of R's

if method.lower()=='shallow':
    err_file = 'Err_CovNet_CV_'+method+'.txt'
elif method.lower()=='deepshared' or method.lower() == 'deep':
    err_file = 'Err_CovNet_CV_'+method+'.txt'

folds = setup.folds
from sklearn.model_selection import KFold
kf = KFold(n_splits=folds)
act_fn = setup.act_fn
init = setup.init
dirc = setup.directory

u = np.loadtxt(dirc+'locations.dat',dtype='float32')
if len(u.shape)==1:
    D, d = len(u), 1
    u = u.reshape(D,1)
else:
    D, d = u.shape
x = np.loadtxt(dirc+'Example.dat',dtype='float32')
N = x.shape[0]
if x.shape[1] != D:
    exit('Data shape mismatch!! Aborting..')
print('N='+str(N)+', D='+str(D)+', d='+str(d))
u = torch.from_numpy(u)
x = torch.from_numpy(x)

CV_scores = np.zeros(len(depths)*len(Rs),dtype=float)
keys = []
if method.lower()=="shallow":
    for R in Rs:
        keys.append("{}".format(R))
elif method.lower()=="deepshared" or method.lower()=="deep":
    for depth in depths:
        for R in Rs:
            keys.append("{}_{}".format(depth,R))

f_err = open(err_file,'w')
f_err.write('CV scores:\n\t')
for key in keys:
    f_err.write("\t\t{}" .format(key))
f_err.write("\n")
f_err.close()

print(time.ctime())
k = 0
for tr_idx, va_idx in kf.split(x):
    k += 1
    x_tr, x_va = x[tr_idx], x[va_idx]
    N_tr = x_tr.shape[0]
    models = []
    checkpoints = []
    if method.lower()=='shallow':
        epochs = setup.epochs_shallow
        burn_in = setup.burn_in_shallow
        interval = setup.interval_shallow
        for R in Rs:
            model = CN.CovNetShallow(d,N_tr,R,act_fn,init)
            checkpoint_file = method+'_'+str(R)+'.pt'
            models.append(model)
            checkpoints.append(checkpoint_file)
    elif method.lower()=='deepshared':
        epochs = setup.epochs_deepshared
        burn_in = setup.burn_in_deepshared
        interval = setup.interval_deepshared
        for depth in depths:
            for R in Rs:
                model = CN.CovNetDeepShared(d,N_tr,R,depth,act_fn,init)
                checkpoint_file = method+'_'+str(depth)+'_'+str(R)+'.pt'
                models.append(model)
                checkpoints.append(checkpoint_file)
    elif method.lower()=='deep':
        epochs = setup.epochs_deep
        burn_in = setup.burn_in_deep
        interval = setup.interval_deep
        for depth in depths:
            for R in Rs:
                n_nodes = R
                model = CN.CovNetDeep(d,N_tr,R,depth,n_nodes,act_fn,init)
                checkpoint_file = method+'_'+str(depth)+'_'+str(R)+'.pt'
                models.append(model)
                checkpoints.append(checkpoint_file)
    split = setup.split

    f_err = open(err_file,'a')
    f_err.write("Fold{}:" .format(k))
    f_err.close()
    print(time.ctime())
    for i,model in enumerate(models):
        optimizer = setup.optimizer(model.params,lr=setup.lr)
        Ifn.cnet_optim_best(x_tr,u,model,loss_fn,optimizer,split,epochs,burn_in,interval,checkpoints[i])
        with torch.no_grad():
            err = loss_fn(x_va,model(u)).item()
            CV_scores[i] += err
            f_err = open(err_file,'a')
            f_err.write("\t{:.6f}" .format(err))
            f_err.close()
        os.remove(checkpoints[i])
    f_err = open(err_file,'a')
    f_err.write("\n")
    f_err.close()
    print('Fold{}:' .format(k))
    print(np.round(CV_scores/k,3))
    del x_tr, x_va, models, checkpoints

CV_scores = CV_scores/folds
f_err = open(err_file,'a')
f_err.write("Average:")
for err in CV_scores:
    f_err.write("\t{:.6f}" .format(err))

best = np.argmin(CV_scores)
best = keys[best]
if method.lower()=="shallow":
    R = int(best)
    f_err.write("\n\nThe best CV-score is achieved for R={}\n" .format(R))
    print("The best CV-score is achieved for R={}" .format(R))
elif method.lower()=="deepshared" or method.lower()=="deep":
    best = best.split('_')
    depth = int(best[0])
    R = int(best[1])
    f_err.write("\n\nThe best CV-score is achieved for depth={}, R={}\n" .format(depth,R))
    print("The best CV-score is achieved for depth={}, R={}" .format(depth,R))
f_err.close()

if method.lower()=='shallow':
    model = CN.CovNetShallow(d,N,R,act_fn,init)
    checkpoint_file = method+'_'+str(R)+'.pt'
    epochs = setup.epochs_shallow
    burn_in = setup.burn_in_shallow
    interval = setup.interval_shallow
elif method.lower()=='deepshared':
    model = CN.CovNetDeepShared(d,N,R,depth,act_fn,init)
    checkpoint_file = method+'_'+str(depth)+'_'+str(R)+'.pt'
    epochs = setup.epochs_deepshared
    burn_in = setup.burn_in_deepshared
    interval = setup.interval_deepshared
elif method.lower()=='deep':
    n_nodes = R
    model = CN.CovNetDeep(d,N,R,depth,n_nodes,act_fn,init)
    checkpoint_file = method+'_'+str(depth)+'_'+str(R)+'.pt'
    epochs = setup.epochs_deep
    burn_in = setup.burn_in_deep
    interval = setup.interval_deep
split = setup.split
optimizer = setup.optimizer(model.params,lr=setup.lr)

Ifn.cnet_optim_best(x,u,model,loss_fn,optimizer,split,epochs,burn_in,interval,checkpoint_file)
ellapsed = time.time() - current
del x,u

file = dirc+'True_locations.dat'
loc = np.loadtxt(file,dtype='float32')
u = torch.from_numpy(loc[:,:d])
v = torch.from_numpy(loc[:,d:])
del loc
cov_file = dirc+'True_cov.dat'
err = Ofn.cnet_error_MC(model,u,v,cov_file)

f_err = open(err_file,'a')
f_err.write('\n\n\n')
f_err.write('Error = {:.10f}\n' .format(err))
f_err.close()
print('Error = {:.6f}'.format(err))