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

import ssl
import asyncio

log = get_logger("MN-WebServer")

from cilantro_ee.crypto import transaction

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

        return super().default(self, o)

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

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

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

        self.static_headers = {}

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

        self.port = port

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

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

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

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

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

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

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

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

        self.coroutine = None

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        funcs = parser.methods_for_contract(contract_code)

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

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

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

        variables = parser.variables_for_contract(contract_code)

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

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

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

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

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

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

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

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

    async def get_latest_block_number(self, request):

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

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

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

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

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

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

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

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

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

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

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

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

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


""" Command Line Interface Module """
import optparse
import sys
import os
from ansibledocgen.parser.dir import DirParser
from ansibledocgen.formatter.markup import MarkupFormatter

class Cli(object):
    """ Command Line Interface for ansible-docgen """

    def __init__(self):
        """ Setup Arguments and Options for CLI """
        # Parse CLI Arguments
        parser = optparse.OptionParser()
        parser.add_option("-p", "--project", dest="project",
                          help="Path to Ansible project",
                          metavar="PROJECT",
                          default="./")
        parser.add_option("-s", "--style", dest="style",
                          help="Choose the format for the documentation.\
                          Default is markup. Example: --style=[markup]",
                          metavar="STYLE",
                          default="markup")
        (options, args) = parser.parse_args()

        # Make sure there is a trailing /
        self.project = os.path.join(options.project, "")
        self.style = options.style

        # Used to Parse Roles and Playbooks
        self.dirparser = None
        self.formatter = None

    def run(self):
        """ EntryPoint Of Application """
        # Parse Project for Roles and Playbooks
        self.dirparser = DirParser(self.project)

        # Based on chosen style, use the associated formatter
        if self.style == "markup":
            self.formatter = MarkupFormatter(
                self.dirparser.get_parserdata(), self.project)
            self.formatter.parse_data()
            self.formatter.write_files()
        else:
            print("Error: Use of an unsupported style.\
                The supported styles are: markup")
            sys.exit(1)


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

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

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

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

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

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

    return tot_x, tot_y

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

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

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

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

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

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

#

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


import torch
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
import os
import sys

model_urls = {
    'imagenet': 'http://webia.lip6.fr/~cadene/Downloads/inceptionresnetv2-d579a627.pth'
}

class BasicConv2d(nn.Module):

    def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) # verify bias false
        self.bn = nn.BatchNorm2d(out_planes)
        self.relu = nn.ReLU(inplace=False)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.relu(x)
        return x

class Mixed_5b(nn.Module):

    def __init__(self):
        super(Mixed_5b, self).__init__()

        self.branch0 = BasicConv2d(192, 96, kernel_size=1, stride=1)

        self.branch1 = nn.Sequential(
            BasicConv2d(192, 48, kernel_size=1, stride=1),
            BasicConv2d(48, 64, kernel_size=5, stride=1, padding=2)
        )

        self.branch2 = nn.Sequential(
            BasicConv2d(192, 64, kernel_size=1, stride=1),
            BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1),
            BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1)
        )

        self.branch3 = nn.Sequential(
            nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
            BasicConv2d(192, 64, kernel_size=1, stride=1)
        )

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)
        x3 = self.branch3(x)
        out = torch.cat((x0, x1, x2, x3), 1)
        return out

class Block35(nn.Module):

    def __init__(self, scale=1.0):
        super(Block35, self).__init__()

        self.scale = scale

        self.branch0 = BasicConv2d(320, 32, kernel_size=1, stride=1)

        self.branch1 = nn.Sequential(
            BasicConv2d(320, 32, kernel_size=1, stride=1),
            BasicConv2d(32, 32, kernel_size=3, stride=1, padding=1)
        )

        self.branch2 = nn.Sequential(
            BasicConv2d(320, 32, kernel_size=1, stride=1),
            BasicConv2d(32, 48, kernel_size=3, stride=1, padding=1),
            BasicConv2d(48, 64, kernel_size=3, stride=1, padding=1)
        )

        self.conv2d = nn.Conv2d(128, 320, kernel_size=1, stride=1)
        self.relu = nn.ReLU(inplace=False)

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)
        out = torch.cat((x0, x1, x2), 1)
        out = self.conv2d(out)
        out = out * self.scale + x
        out = self.relu(out)
        return out

class Mixed_6a(nn.Module):

    def __init__(self):
        super(Mixed_6a, self).__init__()

        self.branch0 = BasicConv2d(320, 384, kernel_size=3, stride=2)

        self.branch1 = nn.Sequential(
            BasicConv2d(320, 256, kernel_size=1, stride=1),
            BasicConv2d(256, 256, kernel_size=3, stride=1, padding=1),
            BasicConv2d(256, 384, kernel_size=3, stride=2)
        )

        self.branch2 = nn.MaxPool2d(3, stride=2)

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)
        out = torch.cat((x0, x1, x2), 1)
        return out

class Block17(nn.Module):

    def __init__(self, scale=1.0):
        super(Block17, self).__init__()

        self.scale = scale

        self.branch0 = BasicConv2d(1088, 192, kernel_size=1, stride=1)

        self.branch1 = nn.Sequential(
            BasicConv2d(1088, 128, kernel_size=1, stride=1),
            BasicConv2d(128, 160, kernel_size=(1,7), stride=1, padding=(0,3)),
            BasicConv2d(160, 192, kernel_size=(7,1), stride=1, padding=(3,0))
        )

        self.conv2d = nn.Conv2d(384, 1088, kernel_size=1, stride=1)
        self.relu = nn.ReLU(inplace=False)

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        out = torch.cat((x0, x1), 1)
        out = self.conv2d(out)
        out = out * self.scale + x
        out = self.relu(out)
        return out

class Mixed_7a(nn.Module):

    def __init__(self):
        super(Mixed_7a, self).__init__()

        self.branch0 = nn.Sequential(
            BasicConv2d(1088, 256, kernel_size=1, stride=1),
            BasicConv2d(256, 384, kernel_size=3, stride=2)
        )

        self.branch1 = nn.Sequential(
            BasicConv2d(1088, 256, kernel_size=1, stride=1),
            BasicConv2d(256, 288, kernel_size=3, stride=2)
        )

        self.branch2 = nn.Sequential(
            BasicConv2d(1088, 256, kernel_size=1, stride=1),
            BasicConv2d(256, 288, kernel_size=3, stride=1, padding=1),
            BasicConv2d(288, 320, kernel_size=3, stride=2)
        )

        self.branch3 = nn.MaxPool2d(3, stride=2)

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)
        x3 = self.branch3(x)
        out = torch.cat((x0, x1, x2, x3), 1)
        return out

class Block8(nn.Module):

    def __init__(self, scale=1.0, noReLU=False):
        super(Block8, self).__init__()

        self.scale = scale
        self.noReLU = noReLU

        self.branch0 = BasicConv2d(2080, 192, kernel_size=1, stride=1)

        self.branch1 = nn.Sequential(
            BasicConv2d(2080, 192, kernel_size=1, stride=1),
            BasicConv2d(192, 224, kernel_size=(1,3), stride=1, padding=(0,1)),
            BasicConv2d(224, 256, kernel_size=(3,1), stride=1, padding=(1,0))
        )

        self.conv2d = nn.Conv2d(448, 2080, kernel_size=1, stride=1)
        if not self.noReLU:
            self.relu = nn.ReLU(inplace=False)

    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        out = torch.cat((x0, x1), 1)
        out = self.conv2d(out)
        out = out * self.scale + x
        if not self.noReLU:
            out = self.relu(out)
        return out

class InceptionResnetV2(nn.Module):

    def __init__(self, num_classes=1001):
        super(InceptionResnetV2, self).__init__()
        self.conv2d_1a = BasicConv2d(3, 32, kernel_size=3, stride=2)
        self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1)
        self.conv2d_2b = BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1)
        self.maxpool_3a = nn.MaxPool2d(3, stride=2)
        self.conv2d_3b = BasicConv2d(64, 80, kernel_size=1, stride=1)
        self.conv2d_4a = BasicConv2d(80, 192, kernel_size=3, stride=1)
        self.maxpool_5a = nn.MaxPool2d(3, stride=2)
        self.mixed_5b = Mixed_5b()
        self.repeat = nn.Sequential(
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17),
            Block35(scale=0.17)
        )
        self.mixed_6a = Mixed_6a()
        self.repeat_1 = nn.Sequential(
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10),
            Block17(scale=0.10)
        )
        self.mixed_7a = Mixed_7a()
        self.repeat_2 = nn.Sequential(
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20),
            Block8(scale=0.20)
        )
        self.block8 = Block8(noReLU=True)
        self.conv2d_7b = BasicConv2d(2080, 1536, kernel_size=1, stride=1)
        self.avgpool_1a = nn.AdaptiveAvgPool2d((1,1))
        self.classif = nn.Linear(1536, num_classes)

    def forward(self, x):
        x = self.conv2d_1a(x)
        x = self.conv2d_2a(x)
        x = self.conv2d_2b(x)
        x = self.maxpool_3a(x)
        x = self.conv2d_3b(x)
        x = self.conv2d_4a(x)
        x = self.maxpool_5a(x)
        x = self.mixed_5b(x)
        x = self.repeat(x)
        x = self.mixed_6a(x)
        x = self.repeat_1(x)
        x = self.mixed_7a(x)
        x = self.repeat_2(x)
        x = self.block8(x)
        x = self.conv2d_7b(x)
        x = self.avgpool_1a(x)
        x = x.view(x.size(0), -1)
        x = self.classif(x)
        return x

def inceptionresnetv2(pretrained=True):
    r"""InceptionResnetV2 model architecture from the
    `"InceptionV4, Inception-ResNet..." <https://arxiv.org/abs/1602.07261>`_ paper.

    Args:
        pretrained ('string'): If True, returns a model pre-trained on ImageNet
    """
    model = InceptionResnetV2()
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['imagenet']))
    return model

######################################################################
## Load parameters from HDF5 to Dict
######################################################################

def load_conv2d(state_dict, name_pth, name_tf):
    h5f = h5py.File('dump/InceptionResnetV2/'+name_tf+'.h5', 'r')
    state_dict[name_pth+'.conv.weight'] = torch.from_numpy(h5f['weights'][()]).permute(3, 2, 0, 1)
    out_planes = state_dict[name_pth+'.conv.weight'].size(0)
    state_dict[name_pth+'.bn.weight'] = torch.ones(out_planes)
    state_dict[name_pth+'.bn.bias'] = torch.from_numpy(h5f['beta'][()])
    state_dict[name_pth+'.bn.running_mean'] = torch.from_numpy(h5f['mean'][()])
    state_dict[name_pth+'.bn.running_var'] = torch.from_numpy(h5f['var'][()])
    h5f.close()

def load_conv2d_nobn(state_dict, name_pth, name_tf):
    h5f = h5py.File('dump/InceptionResnetV2/'+name_tf+'.h5', 'r')
    state_dict[name_pth+'.weight'] = torch.from_numpy(h5f['weights'][()]).permute(3, 2, 0, 1)
    state_dict[name_pth+'.bias'] = torch.from_numpy(h5f['biases'][()])
    h5f.close()

def load_linear(state_dict, name_pth, name_tf):
    h5f = h5py.File('dump/InceptionResnetV2/'+name_tf+'.h5', 'r')
    state_dict[name_pth+'.weight'] = torch.from_numpy(h5f['weights'][()]).t()
    state_dict[name_pth+'.bias'] = torch.from_numpy(h5f['biases'][()])
    h5f.close()

def load_mixed_5b(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0', name_tf+'/Branch_0/Conv2d_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_0b_5x5')
    load_conv2d(state_dict, name_pth+'.branch2.0', name_tf+'/Branch_2/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch2.1', name_tf+'/Branch_2/Conv2d_0b_3x3')
    load_conv2d(state_dict, name_pth+'.branch2.2', name_tf+'/Branch_2/Conv2d_0c_3x3')
    load_conv2d(state_dict, name_pth+'.branch3.1', name_tf+'/Branch_3/Conv2d_0b_1x1')

def load_block35(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0', name_tf+'/Branch_0/Conv2d_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_0b_3x3')
    load_conv2d(state_dict, name_pth+'.branch2.0', name_tf+'/Branch_2/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch2.1', name_tf+'/Branch_2/Conv2d_0b_3x3')
    load_conv2d(state_dict, name_pth+'.branch2.2', name_tf+'/Branch_2/Conv2d_0c_3x3')
    load_conv2d_nobn(state_dict, name_pth+'.conv2d', name_tf+'/Conv2d_1x1')

def load_mixed_6a(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0', name_tf+'/Branch_0/Conv2d_1a_3x3')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_0b_3x3')
    load_conv2d(state_dict, name_pth+'.branch1.2', name_tf+'/Branch_1/Conv2d_1a_3x3')

def load_block17(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0', name_tf+'/Branch_0/Conv2d_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_0b_1x7')
    load_conv2d(state_dict, name_pth+'.branch1.2', name_tf+'/Branch_1/Conv2d_0c_7x1')
    load_conv2d_nobn(state_dict, name_pth+'.conv2d', name_tf+'/Conv2d_1x1')

def load_mixed_7a(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0.0', name_tf+'/Branch_0/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch0.1', name_tf+'/Branch_0/Conv2d_1a_3x3')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_1a_3x3')
    load_conv2d(state_dict, name_pth+'.branch2.0', name_tf+'/Branch_2/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch2.1', name_tf+'/Branch_2/Conv2d_0b_3x3')
    load_conv2d(state_dict, name_pth+'.branch2.2', name_tf+'/Branch_2/Conv2d_1a_3x3')

def load_block8(state_dict, name_pth, name_tf):
    load_conv2d(state_dict, name_pth+'.branch0', name_tf+'/Branch_0/Conv2d_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.0', name_tf+'/Branch_1/Conv2d_0a_1x1')
    load_conv2d(state_dict, name_pth+'.branch1.1', name_tf+'/Branch_1/Conv2d_0b_1x3')
    load_conv2d(state_dict, name_pth+'.branch1.2', name_tf+'/Branch_1/Conv2d_0c_3x1')
    load_conv2d_nobn(state_dict, name_pth+'.conv2d', name_tf+'/Conv2d_1x1')

def load():
    state_dict={}

    load_conv2d(state_dict, name_pth='conv2d_1a', name_tf='Conv2d_1a_3x3')
    load_conv2d(state_dict, name_pth='conv2d_2a', name_tf='Conv2d_2a_3x3')
    load_conv2d(state_dict, name_pth='conv2d_2b', name_tf='Conv2d_2b_3x3')

    load_conv2d(state_dict, name_pth='conv2d_3b', name_tf='Conv2d_3b_1x1')
    load_conv2d(state_dict, name_pth='conv2d_4a', name_tf='Conv2d_4a_3x3')

    load_mixed_5b(state_dict, name_pth='mixed_5b', name_tf='Mixed_5b')

    for i in range(10):
        load_block35(state_dict, name_pth='repeat.'+str(i), name_tf='Repeat/block35_'+str(i+1))

    load_mixed_6a(state_dict, name_pth='mixed_6a', name_tf='Mixed_6a')

    for i in range(20):
        load_block17(state_dict, name_pth='repeat_1.'+str(i), name_tf='Repeat_1/block17_'+str(i+1))

    load_mixed_7a(state_dict, name_pth='mixed_7a', name_tf='Mixed_7a')

    for i in range(9):
        load_block8(state_dict, name_pth='repeat_2.'+str(i), name_tf='Repeat_2/block8_'+str(i+1))

    load_block8(state_dict, name_pth='block8', name_tf='Block8')
    load_conv2d(state_dict, name_pth='conv2d_7b', name_tf='Conv2d_7b_1x1')
    load_linear(state_dict, name_pth='classif', name_tf='Logits')

    return state_dict

######################################################################
## Test
######################################################################

def test(model):
    from scipy import misc
    img = misc.imread('lena_299.png')
    inputs = torch.ones(1,299,299,3)
    #inputs[0] = torch.from_numpy(img)

    inputs[0,0,0,0] = -1
    inputs.transpose_(1,3)
    inputs.transpose_(2,3)

    print(inputs.mean())
    print(inputs.std())

    #inputs.sub_(0.5).div_(0.5)
    #inputs.sub_(inputs)
    # 1, 3, 299, 299

    outputs = model.forward(torch.autograd.Variable(inputs))
    h5f = h5py.File('dump/InceptionResnetV2/Logits.h5', 'r')
    outputs_tf = torch.from_numpy(h5f['out'][()])
    h5f.close()
    outputs = torch.nn.functional.softmax(outputs)
    print(outputs.sum())
    print(outputs[0])
    print(outputs_tf.sum())
    print(outputs_tf[0])
    print(torch.dist(outputs.data, outputs_tf))
    return outputs

def test_conv2d(module, name):
    #global output_tf
    h5f = h5py.File('dump/InceptionResnetV2/'+name+'.h5', 'r')
    output_tf_conv = torch.from_numpy(h5f['conv_out'][()])
    output_tf_conv.transpose_(1,3)
    output_tf_conv.transpose_(2,3)
    output_tf_relu = torch.from_numpy(h5f['relu_out'][()])
    output_tf_relu.transpose_(1,3)
    output_tf_relu.transpose_(2,3)
    h5f.close()
    def test_dist_conv(self, input, output):
        print(name, 'conv', torch.dist(output.data, output_tf_conv))
    module.conv.register_forward_hook(test_dist_conv)
    def test_dist_relu(self, input, output):
        print(name, 'relu', torch.dist(output.data, output_tf_relu))
    module.relu.register_forward_hook(test_dist_relu)

def test_conv2d_nobn(module, name):
    #global output_tf
    h5f = h5py.File('dump/InceptionResnetV2/'+name+'.h5', 'r')
    output_tf = torch.from_numpy(h5f['conv_out'][()])
    output_tf.transpose_(1,3)
    output_tf.transpose_(2,3)
    h5f.close()
    def test_dist(self, input, output):
        print(name, 'conv+bias', torch.dist(output.data, output_tf))
    module.register_forward_hook(test_dist)

def test_mixed_5b(module, name):
    test_conv2d(module.branch0, name+'/Branch_0/Conv2d_1x1')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_0b_5x5')
    test_conv2d(module.branch2[0], name+'/Branch_2/Conv2d_0a_1x1')
    test_conv2d(module.branch2[1], name+'/Branch_2/Conv2d_0b_3x3')
    test_conv2d(module.branch2[2], name+'/Branch_2/Conv2d_0c_3x3')
    test_conv2d(module.branch3[1], name+'/Branch_3/Conv2d_0b_1x1')

def test_block35(module, name):
    test_conv2d(module.branch0, name+'/Branch_0/Conv2d_1x1')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_0b_3x3')
    test_conv2d(module.branch2[0], name+'/Branch_2/Conv2d_0a_1x1')
    test_conv2d(module.branch2[1], name+'/Branch_2/Conv2d_0b_3x3')
    test_conv2d(module.branch2[2], name+'/Branch_2/Conv2d_0c_3x3')
    test_conv2d_nobn(module.conv2d, name+'/Conv2d_1x1')

def test_mixed_6a(module, name):
    test_conv2d(module.branch0, name+'/Branch_0/Conv2d_1a_3x3')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_0b_3x3')
    test_conv2d(module.branch1[2], name+'/Branch_1/Conv2d_1a_3x3')

def test_block17(module, name):
    test_conv2d(module.branch0, name+'/Branch_0/Conv2d_1x1')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_0b_1x7')
    test_conv2d(module.branch1[2], name+'/Branch_1/Conv2d_0c_7x1')
    test_conv2d_nobn(module.conv2d, name+'/Conv2d_1x1')

def test_mixed_7a(module, name):
    test_conv2d(module.branch0[0], name+'/Branch_0/Conv2d_0a_1x1')
    test_conv2d(module.branch0[1], name+'/Branch_0/Conv2d_1a_3x3')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_1a_3x3')
    test_conv2d(module.branch2[0], name+'/Branch_2/Conv2d_0a_1x1')
    test_conv2d(module.branch2[1], name+'/Branch_2/Conv2d_0b_3x3')
    test_conv2d(module.branch2[2], name+'/Branch_2/Conv2d_1a_3x3')

def test_block8(module, name):
    test_conv2d(module.branch0, name+'/Branch_0/Conv2d_1x1')
    test_conv2d(module.branch1[0], name+'/Branch_1/Conv2d_0a_1x1')
    test_conv2d(module.branch1[1], name+'/Branch_1/Conv2d_0b_1x3')
    test_conv2d(module.branch1[2], name+'/Branch_1/Conv2d_0c_3x1')
    test_conv2d_nobn(module.conv2d, name+'/Conv2d_1x1')

######################################################################
## Main
######################################################################

if __name__ == "__main__":

    import h5py

    model = InceptionResnetV2()
    state_dict = load()
    model.load_state_dict(state_dict)
    model.eval()

    os.system('mkdir -p save')
    torch.save(model, 'save/inceptionresnetv2.pth')
    torch.save(state_dict, 'save/inceptionresnetv2_state.pth')

    test_conv2d(model.conv2d_1a, 'Conv2d_1a_3x3')
    test_conv2d(model.conv2d_2a, 'Conv2d_2a_3x3')
    test_conv2d(model.conv2d_2b, 'Conv2d_2b_3x3')
    test_conv2d(model.conv2d_3b, 'Conv2d_3b_1x1')
    test_conv2d(model.conv2d_4a, 'Conv2d_4a_3x3')

    test_mixed_5b(model.mixed_5b, 'Mixed_5b')

    for i in range(len(model.repeat._modules)):
        test_block35(model.repeat[i], 'Repeat/block35_'+str(i+1))

    test_mixed_6a(model.mixed_6a, 'Mixed_6a')

    for i in range(len(model.repeat_1._modules)):
        test_block17(model.repeat_1[i], 'Repeat_1/block17_'+str(i+1))

    test_mixed_7a(model.mixed_7a, 'Mixed_7a')

    for i in range(len(model.repeat_2._modules)):
        test_block8(model.repeat_2[i], 'Repeat_2/block8_'+str(i+1))

    test_block8(model.block8, 'Block8')

    test_conv2d(model.conv2d_7b, 'Conv2d_7b_1x1')

    outputs = test(model)
    # test_conv2d(model.features[1], 'Conv2d_2a_3x3')
    # test_conv2d(model.features[2], 'Conv2d_2b_3x3')
    # test_conv2d(model.features[3].conv, 'Mixed_3a/Branch_1/Conv2d_0a_3x3')
    #test_mixed_4a_7a(model.features[4], 'Mixed_4a')


from ts3tools import ts3tools

"""
jonni.afkMover
This plugin moves clients on special events like getting away or muting the speaker or microphone.
It's fully configurable via config file.
"""

name = 'jonni.afkmover'

base = None
config = None
core_TS3clients = None
core_TS3chat = None

afk_users = {}
move_status = []

def setup(ts3base):
    global base
    global config

    base = ts3base
    base.register_callback(name, 'ts3.start', startup)
    base.register_callback(name, 'ts3.client.away', event_away)
    base.register_callback(name, 'ts3.client.notaway', event_notaway)
    base.register_callback(name, 'ts3.client.mutedmic', event_mutedmic)
    base.register_callback(name, 'ts3.client.unmutedmic', event_unmutedmic)
    base.register_callback(name, 'ts3.client.mutedspeaker', event_mutedspeaker)
    base.register_callback(name, 'ts3.client.unmutedspeaker', event_unmutedspeaker)
    base.register_callback(name, 'ts3.client.move', event_client_move)
    base.register_callback(name, 'ts3.client.leave', event_client_leave)
    base.register_callback(name, 'ts3.chat.cmd.afk', chat_afk)

    base.register_class('jonni.afkmover', AfkMover_API)

    config = ts3tools.get_instance_config(base, 'jonni.afkmover')
    if config is None:
        config = ts3tools.get_global_config('jonni.afkmover')

def startup(values):
    global core_TS3clients
    global core_TS3chat
    core_TS3clients = base.get_class('core.TS3clients')
    core_TS3chat = base.get_class('core.TS3chat')
    core_TS3chat.add_command_help('!afk', 'Set\'s you to AFK mode and moves you to an AFK channel.', '[TODO]')

def afk(event, user, afk_message=None):
    # only do something when the user isn't afk already
    clid = user['clid']
    cid = user['cid']
    if user['clid'] not in afk_users.keys():
        afk_users[clid] = {'old_channel': cid, 'reasons': [event], 'message': afk_message}
        if event == 'away':
            move(user, config['AwayMove']['channel_id'])
        elif event == 'mutedmic':
            move(user, config['MicMuteMove']['channel_id'])
        elif event == 'mutedspeaker':
            move(user, config['SpeakerMuteMove']['channel_id'])
        elif event == 'command':
            move(user, config['CommandMove']['channel_id'])
        if config['CommandMove']['away_msg_in_desc'] == 'true' and afk_message is not None:
            pass # [TODO]
        base.debprint('[jonni.afkMover] User ' + user['client_nickname'] + ' is now afk! He will be moved now.')
    else:
        # the user is afk, but there is a new reason for being afk now
        if event not in afk_users[clid]['reasons']:
            afk_users[clid]['reasons'].append(event)

def unafk(event, user):
    # only do something when the user is afk
    if user['clid'] in afk_users.keys():
        clid = user['clid']
        # if the event matches the event the user is afk
        if event in afk_users[clid]['reasons'] and len(afk_users[clid]['reasons']) == 1:
            # delete the user from afk cache and move him
            if config['General']['moveback'] == 'true':
                move(user, afk_users[clid]['old_channel'])
            del afk_users[clid]
            base.debprint('[jonni.afkMover] User ' + user['client_nickname'] + ' isn\'t afk anymore!')
            return True
        elif event == 'all':
            del afk_users[clid]
            base.debprint('[jonni.afkMover] User ' + user['client_nickname'] + ' isn\'t afk anymore! (all)')
            # no move here, because the user want to be in the channel he went
        else:
            afk_users[clid]['reasons'].remove(event)

def move(user, new_cid):
    # if the user is already in channel, we don't need to move him
    if user['cid'] != new_cid:
        move_status.append(user['clid'])
        core_TS3clients.clientmove(user['clid'], new_cid)

def event_away(user):
    if config['AwayMove']['enabled'] == 'true':
        afk('away', user)

def event_notaway(user):
    if config['AwayMove']['enabled'] == 'true':
        unafk('away', user)

def event_mutedmic(user):
    if config['MicMuteMove']['enabled'] == 'true':
        afk('mutedmic', user)

def event_unmutedmic(user):
    if config['MicMuteMove']['enabled'] == 'true':
        unafk('mutedmic', user)

def event_mutedspeaker(user):
    if config['SpeakerMuteMove']['enabled'] == 'true':
        afk('mutedspeaker', user)

def event_unmutedspeaker(user):
    if config['SpeakerMuteMove']['enabled'] == 'true':
        unafk('mutedspeaker', user)

def event_client_move(event):
    global move_status
    # this is a little fix for the problem that the plugin moves someone and then this someone get unafk
    if event['user']['clid'] in move_status:
        move_status.remove(event['user']['clid'])
    # if the user moves to another channel, maybe he is not afk?
    else:
        if config['General']['move_unafk'] == 'true':
            unafk('all', event['user'])

def event_client_leave(user):
    clid = user['clid']
    if clid in afk_users.keys():
        del afk_users[clid]

def chat_afk(event):
    global user_cache
    clid = event['sender']['clid']
    if clid not in afk_users.keys():
        # get client info because there are no client information delivered by chat event
        user = core_TS3clients.clientinfo(event['sender']['clid'])
        user['clid'] = event['sender']['clid']
        # away message handling
        if len(event['args']) != 0 and config['CommandMove']['away_msg'] == 'true':
            string = ''
            for arg in event['args']:
                string += arg + ' '
            print(string)
            afk('command', user, string)
        else:
            afk('command', user)
        # send text message for info
        if config['General']['moveback'] == 'true':
            if config['General']['name'] != 'false':
                ts3tools.set_nickname(base, config['General']['name'], True)
            core_TS3chat.send_pm(base, clid, 'You\'re now AFK! Please type in [color=blue]!afk[/color] to come back! If so, you will be moved to your old channel automatically! ;-)', True)
        else:
            if config['General']['name'] != 'false':
                ts3tools.set_nickname(base, config['General']['name'], True)
            core_TS3chat.send_pm(base, clid, 'You\'re now AFK! Please type in [color=blue]!afk[/color] to come back!', True)
    else:
        # there is no user info in event, so we need more
        user = core_TS3clients.clientinfo(event['sender']['clid'])
        user['clid'] = event['sender']['clid']
        unafk('command', user)
        # send text message for info
        if config['General']['moveback'] == 'true':
            if config['General']['name'] != 'false':
                ts3tools.set_nickname(base, config['General']['name'], True)
            core_TS3chat.send_pm(base, clid, 'You\'re now BACK! The server will move you back now! :-)', True)
        else:
            if config['General']['name'] != 'false':
                ts3tools.set_nickname(base, config['General']['name'], True)
            core_TS3chat.send_pm(base, clid, 'You\'re now BACK!', True)

class AfkMover_API:
    def __init__(self, base):
        pass

    def get_users(self):
        """
        Returns a dictionary with client id as key and another dict as value which contains the old channel and the reasons for being afk.
        """
        return afk_users

    def get_status(self, clid):
        """
        Returns true if the user with the given client id is afk.
        Returns false if the user with the given client isn't afk (isn't in afk_users).
        """
        if clid in afk_users.keys():
            return True
        else:
            return False

    def get_message(self, clid):
        """
        Returns the message set by AFK user, if set by user.
        If no text message was found, but the user is AFK, returns None.
        Otherwise returns False.
        """
        if clid in afk_users.keys():
            if afk_users[clid]['message'] is not None:
                return afk_users[clid]['message']
            else:
                return None
        else:
            return False

    def set_status(self):
        """
        (Unimplemented) Sets a user with the given client id to AFK. If already AFK, set NOT AFK.
        """
        pass


from .basesimulator import BaseSimulator
from .logger import *

class Controller(BaseSimulator):
    def initGVT(self):
        """
        Initialise the GVT algorithm. Should ONLY be called at the controller,
        though things 'should' work elsewhere...
        """
        assert debug("initGVT called")
        self.simlock.acquire()
        self.nestedlock.acquire()
        self.Tmin = float('inf')
        self.color = True
        msg = [self.model.timeLast[0], float('inf'), self.V]
        self.V = {}
        assert debug("Cleared V")
        self.nextLP.receiveControl(msg)
        self.nestedlock.release()
        self.simlock.release()
        # Wait until the lock is released elsewhere
        self.waitForGVT.wait()
        self.waitForGVT.clear()
        return self.GVT

    def GVTdone(self):
        """
        Notify this simulation kernel that the GVT calculation is finished
        """
        self.waitForGVT.set()

    #TODO never used yet...
    def printRootOutput(self):
        """
        Prints the Root Output of a simulation
        """
        if len(self.model.OPorts) > 0:
            # don't print if root model is autonomous
            # (no output ports)
            print(("\n\tROOT DEVS <%s> Output Port Configuration:" % self.model.getModelFullName()))
            for I in range(0, len(self.model.OPorts) ):
                if self.model.OPorts[I] in self.model.myOutput:
                    print(("\t    port <%s>: %s" % (\
                        self.model.OPorts[I].getPortName(), \
                        self.model.myOutput[self.model.OPorts[I]])))
                else:
                    print(("\t    port <%s>: NoEvent" % self.model.OPorts[I].getPortName()))

    def notifyWait(self):
        """
        Notify the controller that a simulation kernel is waiting.
        """
        self.waitingLock.acquire()
        self.waiting += 1
        self.finishCheck.set()
        self.waitingLock.release()

    def notifyRun(self):
        """
        Notify the controller that a simulation kernel has started running again.
        """
        self.waitingLock.acquire()
        self.waiting -= 1
        self.waitingLock.release()

    def isFinished(self, running):
        """
        Checks if all kernels have indicated that they have finished simulation.
        If each kernel has indicated this, a final (expensive) check happens to
        prevent premature termination.
        """
        # NOTE make sure that GVT algorithm is not running at the moment, otherwise we deadlock!
        # it might be possible that the GVT algorithm starts immediately after the wait(), causing deadlock again
        # Now we are sure that the GVT algorithm is not running when we start this
        self.waitingLock.acquire()
        if self.waiting == running:
            # It seems that we should be finished, so just ACK this with every simulation kernel before proceeding
            #   it might be possible that the kernel's 'notifyRun' command is still on the way, making the simulation
            #   stop too soon.
            done = self.nextLP.finishRing(self.totalCounter)
        else:
            done = False
        self.waitingLock.release()
        return done

    def waitFinish(self, running):
        """
        Wait until the specified number of kernels have all told that simulation
        finished.
        """
        while True:
            # Force a check after each second
            self.finishCheck.wait(1)
            self.finishCheck.clear()
            if self.isFinished(running):
                # All simulation kernels have told us that they are idle at the moment
                break
        self.runGVT = False
        self.eventGVT.set()
        self.gvtthread.join()


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

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

from definitions import API_UTILS_PATH

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

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

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

s = requests.Session()

app_access_token = FACEBOOK_USER_ACCESS_TOKEN

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

    print(len(all_events))

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

    complete_events = []

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

            for id in sub_ids:

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

                expanded_event_dict = resp.json()

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

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

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

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

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

    # filtered_events.extend(additional_events)

    filtered_events = []

    for event in complete_events:

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

            # don't add to filtered_events
            continue

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

            # don't add to filtered_events
            continue

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

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

        filtered_events.append(event)

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

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

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

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

    return categorized_clean_events


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

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


def check_pallin(s):
	if(s == s[::-1]):
		return True
	return False

def finf_longest_pallin(s):
	aux = ""
	d = {"string":0}
	for i in s:
		aux = aux + i
		if(len(aux)>0 and check_pallin(aux)):
			d[aux] = len(aux)
	#print(d)
	return max(d.items(), key = lambda x : x[1])

if __name__ == '__main__':
	print(finf_longest_pallin(input("Enter the string : ")))


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

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

kernel_size = 3
num_filters = 3

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

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

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

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

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

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

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

    # zero weights
    total = 0

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

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

    # generate mask
    index = 0

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

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

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

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

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

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

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

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

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

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

plt.show()


from __future__ import annotations

import copy
import json
import logging
from typing import (
    TYPE_CHECKING,
    Any,
    Dict,
    List,
    Literal,
    Optional,
    TypedDict,
    Union,
    overload,
)

from langchain_core.callbacks import (
    AsyncCallbackManagerForLLMRun,
    CallbackManagerForLLMRun,
)
from langchain_core.language_models.llms import LLM
from langchain_core.pydantic_v1 import PrivateAttr

if TYPE_CHECKING:
    import openllm

ServerType = Literal["http", "grpc"]

class IdentifyingParams(TypedDict):
    """Parameters for identifying a model as a typed dict."""

    model_name: str
    model_id: Optional[str]
    server_url: Optional[str]
    server_type: Optional[ServerType]
    embedded: bool
    llm_kwargs: Dict[str, Any]

logger = logging.getLogger(__name__)

class OpenLLM(LLM):
    """OpenLLM, supporting both in-process model
    instance and remote OpenLLM servers.

    To use, you should have the openllm library installed:

    .. code-block:: bash

        pip install openllm

    Learn more at: https://github.com/bentoml/openllm

    Example running an LLM model locally managed by OpenLLM:
        .. code-block:: python

            from langchain_community.llms import OpenLLM
            llm = OpenLLM(
                model_name='flan-t5',
                model_id='google/flan-t5-large',
            )
            llm("What is the difference between a duck and a goose?")

    For all available supported models, you can run 'openllm models'.

    If you have a OpenLLM server running, you can also use it remotely:
        .. code-block:: python

            from langchain_community.llms import OpenLLM
            llm = OpenLLM(server_url='http://localhost:3000')
            llm("What is the difference between a duck and a goose?")
    """

    model_name: Optional[str] = None
    """Model name to use. See 'openllm models' for all available models."""
    model_id: Optional[str] = None
    """Model Id to use. If not provided, will use the default model for the model name.
    See 'openllm models' for all available model variants."""
    server_url: Optional[str] = None
    """Optional server URL that currently runs a LLMServer with 'openllm start'."""
    server_type: ServerType = "http"
    """Optional server type. Either 'http' or 'grpc'."""
    embedded: bool = True
    """Initialize this LLM instance in current process by default. Should
    only set to False when using in conjunction with BentoML Service."""
    llm_kwargs: Dict[str, Any]
    """Keyword arguments to be passed to openllm.LLM"""

    _runner: Optional[openllm.LLMRunner] = PrivateAttr(default=None)
    _client: Union[
        openllm.client.HTTPClient, openllm.client.GrpcClient, None
    ] = PrivateAttr(default=None)

    class Config:
        extra = "forbid"

    @overload
    def __init__(
        self,
        model_name: Optional[str] = ...,
        *,
        model_id: Optional[str] = ...,
        embedded: Literal[True, False] = ...,
        **llm_kwargs: Any,
    ) -> None:
        ...

    @overload
    def __init__(
        self,
        *,
        server_url: str = ...,
        server_type: Literal["grpc", "http"] = ...,
        **llm_kwargs: Any,
    ) -> None:
        ...

    def __init__(
        self,
        model_name: Optional[str] = None,
        *,
        model_id: Optional[str] = None,
        server_url: Optional[str] = None,
        server_type: Literal["grpc", "http"] = "http",
        embedded: bool = True,
        **llm_kwargs: Any,
    ):
        try:
            import openllm
        except ImportError as e:
            raise ImportError(
                "Could not import openllm. Make sure to install it with "
                "'pip install openllm.'"
            ) from e

        llm_kwargs = llm_kwargs or {}

        if server_url is not None:
            logger.debug("'server_url' is provided, returning a openllm.Client")
            assert (
                model_id is None and model_name is None
            ), "'server_url' and {'model_id', 'model_name'} are mutually exclusive"
            client_cls = (
                openllm.client.HTTPClient
                if server_type == "http"
                else openllm.client.GrpcClient
            )
            client = client_cls(server_url)

            super().__init__(
                **{
                    "server_url": server_url,
                    "server_type": server_type,
                    "llm_kwargs": llm_kwargs,
                }
            )
            self._runner = None  # type: ignore
            self._client = client
        else:
            assert model_name is not None, "Must provide 'model_name' or 'server_url'"
            # since the LLM are relatively huge, we don't actually want to convert the
            # Runner with embedded when running the server. Instead, we will only set
            # the init_local here so that LangChain users can still use the LLM
            # in-process. Wrt to BentoML users, setting embedded=False is the expected
            # behaviour to invoke the runners remotely.
            # We need to also enable ensure_available to download and setup the model.
            runner = openllm.Runner(
                model_name=model_name,
                model_id=model_id,
                init_local=embedded,
                ensure_available=True,
                **llm_kwargs,
            )
            super().__init__(
                **{
                    "model_name": model_name,
                    "model_id": model_id,
                    "embedded": embedded,
                    "llm_kwargs": llm_kwargs,
                }
            )
            self._client = None  # type: ignore
            self._runner = runner

    @property
    def runner(self) -> openllm.LLMRunner:
        """
        Get the underlying openllm.LLMRunner instance for integration with BentoML.

        Example:
        .. code-block:: python

            llm = OpenLLM(
                model_name='flan-t5',
                model_id='google/flan-t5-large',
                embedded=False,
            )
            tools = load_tools(["serpapi", "llm-math"], llm=llm)
            agent = initialize_agent(
                tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
            )
            svc = bentoml.Service("langchain-openllm", runners=[llm.runner])

            @svc.api(input=Text(), output=Text())
            def chat(input_text: str):
                return agent.run(input_text)
        """
        if self._runner is None:
            raise ValueError("OpenLLM must be initialized locally with 'model_name'")
        return self._runner

    @property
    def _identifying_params(self) -> IdentifyingParams:
        """Get the identifying parameters."""
        if self._client is not None:
            self.llm_kwargs.update(self._client._config())
            model_name = self._client._metadata()["model_name"]
            model_id = self._client._metadata()["model_id"]
        else:
            if self._runner is None:
                raise ValueError("Runner must be initialized.")
            model_name = self.model_name
            model_id = self.model_id
            try:
                self.llm_kwargs.update(
                    json.loads(self._runner.identifying_params["configuration"])
                )
            except (TypeError, json.JSONDecodeError):
                pass
        return IdentifyingParams(
            server_url=self.server_url,
            server_type=self.server_type,
            embedded=self.embedded,
            llm_kwargs=self.llm_kwargs,
            model_name=model_name,
            model_id=model_id,
        )

    @property
    def _llm_type(self) -> str:
        return "openllm_client" if self._client else "openllm"

    def _call(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:
        try:
            import openllm
        except ImportError as e:
            raise ImportError(
                "Could not import openllm. Make sure to install it with "
                "'pip install openllm'."
            ) from e

        copied = copy.deepcopy(self.llm_kwargs)
        copied.update(kwargs)
        config = openllm.AutoConfig.for_model(
            self._identifying_params["model_name"], **copied
        )
        if self._client:
            res = self._client.generate(
                prompt, **config.model_dump(flatten=True)
            ).responses[0]
        else:
            assert self._runner is not None
            res = self._runner(prompt, **config.model_dump(flatten=True))
        if isinstance(res, dict) and "text" in res:
            return res["text"]
        elif isinstance(res, str):
            return res
        else:
            raise ValueError(
                "Expected result to be a dict with key 'text' or a string. "
                f"Received {res}"
            )

    async def _acall(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:
        try:
            import openllm
        except ImportError as e:
            raise ImportError(
                "Could not import openllm. Make sure to install it with "
                "'pip install openllm'."
            ) from e

        copied = copy.deepcopy(self.llm_kwargs)
        copied.update(kwargs)
        config = openllm.AutoConfig.for_model(
            self._identifying_params["model_name"], **copied
        )
        if self._client:
            async_client = openllm.client.AsyncHTTPClient(self.server_url)
            res = (
                await async_client.generate(prompt, **config.model_dump(flatten=True))
            ).responses[0]
        else:
            assert self._runner is not None
            (
                prompt,
                generate_kwargs,
                postprocess_kwargs,
            ) = self._runner.llm.sanitize_parameters(prompt, **kwargs)
            generated_result = await self._runner.generate.async_run(
                prompt, **generate_kwargs
            )
            res = self._runner.llm.postprocess_generate(
                prompt, generated_result, **postprocess_kwargs
            )

        if isinstance(res, dict) and "text" in res:
            return res["text"]
        elif isinstance(res, str):
            return res
        else:
            raise ValueError(
                "Expected result to be a dict with key 'text' or a string. "
                f"Received {res}"
            )


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

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

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

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

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

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

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

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

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

    filtered_points = point_cloud
    # 屏蔽结束

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

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

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

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

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

if __name__ == '__main__':
    main()


import unittest

import discretize
import matplotlib.pyplot as plt
import numpy as np
from pymatsolver import Pardiso as Solver
from scipy.constants import mu_0
from scipy.interpolate import interp1d
from SimPEG import maps
from SimPEG.electromagnetics import analytics
from SimPEG.electromagnetics import time_domain as tdem
from SimPEG.electromagnetics import utils

def halfSpaceProblemAnaDiff(
    meshType,
    srctype="MagDipole",
    sig_half=1e-2,
    rxOffset=50.0,
    bounds=None,
    plotIt=False,
    rxType="MagneticFluxDensityz",
):
    if bounds is None:
        bounds = [1e-5, 1e-3]
    if meshType == "CYL":
        cs, ncx, ncz, npad = 15.0, 30, 10, 15
        hx = [(cs, ncx), (cs, npad, 1.3)]
        hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
        mesh = discretize.CylindricalMesh([hx, 1, hz], "00C")

    elif meshType == "TENSOR":
        cs, nc, npad = 20.0, 20, 7
        hx = [(cs, npad, 1.5), (cs, nc), (cs, npad, 1.5)]
        hy = [(cs, npad, 1.5), (cs, nc), (cs, npad, 1.5)]
        hz = [(cs, npad, 1.5), (cs, nc), (cs, npad, 1.5)]
        mesh = discretize.TensorMesh([hx, hy, hz], "CCC")

    active = mesh.cell_centers_z < 0.0
    actMap = maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.shape_cells[2])
    mapping = maps.ExpMap(mesh) * maps.SurjectVertical1D(mesh) * actMap

    time_steps = [(1e-3, 5), (1e-4, 5), (5e-5, 10), (5e-5, 10), (1e-4, 10)]
    time_mesh = discretize.TensorMesh(
        [
            time_steps,
        ]
    )
    times = time_mesh.nodes_x
    out = utils.VTEMFun(times, 0.00595, 0.006, 100)
    wavefun = interp1d(times, out)
    t0 = 0.006
    waveform = tdem.Src.RawWaveform(off_time=t0, waveform_function=wavefun)

    rx = getattr(tdem.Rx, "Point{}".format(rxType[:-1]))(
        np.array([[rxOffset, 0.0, 0.0]]), np.logspace(-4, -3, 31) + t0, rxType[-1]
    )

    if srctype == "MagDipole":
        src = tdem.Src.MagDipole(
            [rx], waveform=waveform, location=np.array([0, 0.0, 0.0])
        )
    elif srctype == "CircularLoop":
        src = tdem.Src.CircularLoop(
            [rx], waveform=waveform, location=np.array([0.0, 0.0, 0.0]), radius=13.0
        )
    elif srctype == "LineCurrent":
        side = 100.0 * np.sqrt(np.pi)
        loop_path = np.c_[
            [-side / 2, -side / 2, 0],
            [side / 2, -side / 2, 0],
            [side / 2, side / 2, 0],
            [-side / 2, side / 2, 0],
            [-side / 2, -side / 2, 0],
        ].T
        src = tdem.sources.LineCurrent([rx], waveform=waveform, location=loop_path)

    survey = tdem.Survey([src])
    prb = tdem.Simulation3DMagneticFluxDensity(
        mesh, survey=survey, sigmaMap=mapping, time_steps=time_steps
    )
    prb.solver = Solver

    sigma = np.ones(mesh.shape_cells[2]) * 1e-8
    sigma[active] = sig_half
    sigma = np.log(sigma[active])

    if srctype == "MagDipole":
        bz_ana = mu_0 * analytics.hzAnalyticDipoleT(
            rx.locations[0][0] + 1e-3, rx.times - t0, sig_half
        )
    elif srctype == "CircularLoop":
        bz_ana = mu_0 * analytics.hzAnalyticCentLoopT(13, rx.times - t0, sig_half)
    elif srctype == "LineCurrent":
        bz_ana = mu_0 * analytics.hzAnalyticCentLoopT(100.0, rx.times - t0, sig_half)

    bz_calc = prb.dpred(sigma)
    ind = np.logical_and(rx.times - t0 > bounds[0], rx.times - t0 < bounds[1])
    log10diff = np.linalg.norm(
        np.log10(np.abs(bz_calc[ind])) - np.log10(np.abs(bz_ana[ind]))
    ) / np.linalg.norm(np.log10(np.abs(bz_ana[ind])))

    print(
        " |bz_ana| = {ana} |bz_num| = {num} |bz_ana-bz_num| = {diff}".format(
            ana=np.linalg.norm(bz_ana),
            num=np.linalg.norm(bz_calc),
            diff=np.linalg.norm(bz_ana - bz_calc),
        )
    )
    print("Difference: {}".format(log10diff))

    if plotIt is True:
        plt.loglog(
            rx.times[bz_calc > 0] - t0,
            bz_calc[bz_calc > 0],
            "r",
            rx.times[bz_calc < 0] - t0,
            -bz_calc[bz_calc < 0],
            "r--",
        )
        plt.loglog(rx.times - t0, abs(bz_ana), "b*")
        plt.title("sig_half = {:e}".format(sig_half))
        plt.show()

    return log10diff

class TDEM_SimpleSrcTests(unittest.TestCase):
    def test_source(self):
        waveform = tdem.Src.StepOffWaveform()
        assert waveform.eval(0.0) == 1.0

class TDEM_bTests(unittest.TestCase):
    def test_analytic_p0_CYL_1m_MagDipole(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff("CYL", rxOffset=1.0, sig_half=1e0) < 0.01
        )

    def test_analytic_m1_CYL_1m_MagDipole(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff("CYL", rxOffset=1.0, sig_half=1e-1) < 0.01
        )

    def test_analytic_m2_CYL_1m_MagDipole(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff("CYL", rxOffset=1.0, sig_half=1e-2) < 0.01
        )

    def test_analytic_m3_CYL_1m_MagDipole(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff("CYL", rxOffset=1.0, sig_half=1e-3) < 0.01
        )

    def test_analytic_p0_CYL_0m_CircularLoop(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "CYL", srctype="CircularLoop", rxOffset=0.0, sig_half=1e0
            )
            < 0.01
        )

    def test_analytic_m1_CYL_0m_CircularLoop(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "CYL", srctype="CircularLoop", rxOffset=0.0, sig_half=1e-1
            )
            < 0.01
        )

    def test_analytic_m2_CYL_0m_CircularLoop(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "CYL", srctype="CircularLoop", rxOffset=0.0, sig_half=1e-2
            )
            < 0.01
        )

    def test_analytic_m3_CYL_0m_CircularLoop(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "CYL",
                srctype="CircularLoop",
                rxOffset=0.0,
                sig_half=1e-3,
            )
            < 0.01
        )

    def test_analytic_m1_TENSOR_0m_LineCurrent(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "TENSOR",
                srctype="LineCurrent",
                rxOffset=0.0,
                sig_half=1e-1,
            )
            < 0.01
        )

    def test_analytic_m2_TENSOR_0m_LineCurrent(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "TENSOR",
                srctype="LineCurrent",
                rxOffset=0.0,
                sig_half=1e-2,
            )
            < 0.01
        )

    def test_analytic_m3_TENSOR_0m_LineCurrent(self):
        self.assertTrue(
            halfSpaceProblemAnaDiff(
                "TENSOR",
                srctype="LineCurrent",
                rxOffset=0.0,
                sig_half=1e-3,
            )
            < 0.01
        )


import pytesseract
from PIL import Image

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


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

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

"""@package docstring
Websocket interface module.

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

class BackendServerProtocol(WebSocketServerProtocol):
    dispatchers = {}

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

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

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

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

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

if __name__ == '__main__':

    log.startLogging( stdout )

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

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


from django.shortcuts import render,redirect

from django.views import View
from .models import Information
from .forms import InformationForm

class SurveillanceView(View):

    def get(self, request, *args, **kwargs):

        informations  = Information.objects.all()
        context = { "informations":informations }
        return render(request,"surveillance/index.html",context)

    def post(self, request, *args, **kwargs):

        """
        posted  = Information( url = request.POST["url"],email = request.POST["email"] )
        posted.save()
        """

        #forms.pyを使ったほうが、フィールドが増えてもビューの書き換えは発生しないし、バリデーション(入力値のチェック)までできる
        form    = InformationForm(request.POST)

        if form.is_valid():
            print("バリデーションOK")
            form.save()

        else:
            print("バリデーションNG")

        return redirect("surveillance:index")

index   = SurveillanceView.as_view()

#レコードを削除する専用のビュー
class SurveillanceDeleteView(View):

    def post(self, request, pk, *args, **kwargs):

        #主キーが合致するものを検索、対象を削除する。
        #TIPS:urls.pyにて、入力値のチェックは行っているのであえてforms.pyにてクラスを定義してバリデーションをする必要はない。
        informations  = Information.objects.filter(id=pk)

        #レコードが存在する場合、削除する。
        if informations:
            informations.delete()
        else:
            print("指定されたデータがありません")

        return redirect("surveillance:index")

delete  = SurveillanceDeleteView.as_view()


# patricia.samantha.kwarwp7.py
# SPDX-License-Identifier: GPL-3.0-or-later
""" Projeto final - três desafios
FASE 1.Na primeira aventura o índio precisa vencer obstáculos
para chegar até a caverna!
FASE 2. Na segunda fase o índio precisa tomar cuidado para não cair na toca do tatu!
FASE 3. Na terceira fase o índio precisa chegar até a caverna sem cair no córrego!
PARA JOGAR, COMENTE OU DESCOMENTE OS DESAFIOS NA DEF MAIN!
.. codeauthor:: Raquel M. M. Fernandes - raquelmachado4993@gmail.com

Changelog
---------
.. versionadded::    06.10

"""

from _spy.vitollino.main import Jogo, STYLE
from samantha.kwarwp9 import main as kwarwp_main, Indio
MAPA_INICIO = """
@....&
......
.....#
.#.p`.
"""

MAPA_INICIAL= """
|||||||||
|@..@...|
|..&|.@.|
|..@|...|
|...@...|
|@....^.|
"""

MAPA_INICIAL2= """
T.oaooaoa
a...ao..&
a......ao
o.oaoa..o
a..o....a
oa^..o..o
"""

MAPA_INICIAL3= """
ccccccccc
c...e...c
m....e.ec
e...e...c
e.r.e.e.c
ec^c..e.c
"""

class Kaiowa(Indio):
    def executa(self):
        """ Roteiro do índio. Conjunto de comandos para ele executar.
        """
       #roteiro desafio 1
       #self.esquerda()
       #self.anda()
       #self.anda()
       #self.anda()
       #self.anda()
       #self.direita()
       #self.anda()
       #self.anda()
       #self.anda()
       #self.direita()
       #self.anda()

       #roteiro desafio2
       #self.direita()
       #self.anda()
       #self.anda()
       #self.esquerda()
       #self.anda()
       #self.direita()
       #self.anda()
       #self.anda()
       #self.esquerda()
       #self.anda()
       #self.anda()
       #self.anda()
       #self.direita()
       #self.anda()
       #self.anda()

       #roteiro desafio 3
        self.empurra()
        self.anda()
        self.esquerda()
        self.anda()
        self.direita()
        self.anda()
        self.anda()
        self.esquerda()
        self.anda()

class Vitollino(Jogo):
    """ Empacota o engenho de jogo Vitollino """
    pass

def main(vitollino, medidas):
    #return kwarwp_main(vitollino=vitollino, medidas=medidas, mapa=MAPA_INICIAL, indios=(Kaiowa,))
    #return kwarwp_main(vitollino=vitollino, medidas=medidas, mapa=MAPA_INICIAL2, indios=(Kaiowa,))
    return kwarwp_main(vitollino=vitollino, medidas=medidas, mapa=MAPA_INICIAL3, indios=(Kaiowa,))

if __name__ == "__main__":
    main(Jogo, STYLE)


import os
import pickle
from pathlib import Path
import numpy as np
import cv2
import h5py
import tqdm

def load_pkl(fpath):
    with open(fpath, "rb") as f:
        return pickle.load(f, encoding="latin")

def load_h5py(fpath):
    return h5py.File(fpath, "r")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--root", type=str, required=True, help="path to the PeopleSnapshotData")
    parser.add_argument("--subject", type=str, default="male-3-casual", help="sequence to process")
    args = parser.parse_args()

    dirpath = os.path.join(args.root, args.subject)
    assert os.path.exists(dirpath), f"Cannot open {dirpath}"
    dirpath = Path(dirpath)

    outdir = Path(f"./data/PeopleSnapshot/{args.subject}/")
    os.makedirs(outdir, exist_ok=True)

    # load camera
    camera = load_pkl(dirpath / "camera.pkl")
    K = np.eye(3)
    K[0, 0] = camera["camera_f"][0]
    K[1, 1] = camera["camera_f"][1]
    K[:2, 2] = camera["camera_c"]
    dist_coeffs = camera["camera_k"]

    H, W = camera["height"], camera["width"]
    w2c = np.eye(4)
    w2c[:3, :3] = cv2.Rodrigues(camera["camera_rt"])[0]
    w2c[:3, 3] = camera["camera_t"]

    camera_path = outdir / "cameras.npz"
    np.savez(str(camera_path), **{
        "intrinsic": K,
        "extrinsic": w2c,
        "height": H,
        "width": W,
    })
    print("Write camera to", camera_path)

    # load images
    image_dir = outdir / "images"
    os.makedirs(image_dir, exist_ok=True)

    print("Write images to", image_dir)
    cap = cv2.VideoCapture(str(dirpath / f"{args.subject}.mp4"))
    frame_cnt = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    for i in tqdm.trange(frame_cnt):
        img_path = f"{image_dir}/image_{i:04d}.png"
        ok, frame = cap.read()
        if not ok: break
        frame = cv2.undistort(frame, K, dist_coeffs)
        cv2.imwrite(img_path, frame)

    # load masks
    mask_dir = outdir / "masks"
    os.makedirs(mask_dir, exist_ok=True)

    print("Write mask to", mask_dir)
    masks = np.asarray(load_h5py(dirpath / "masks.hdf5")["masks"]).astype(np.uint8)
    for i, mask in enumerate(tqdm.tqdm(masks)):
        mask_path = f"{mask_dir}/mask_{i:04d}.npy"
        mask = cv2.undistort(mask, K, dist_coeffs)
        np.save(mask_path, mask)

    smpl_params = load_h5py(dirpath / "reconstructed_poses.hdf5")
    smpl_params = {
        "betas": np.asarray(smpl_params["betas"]).astype(np.float32),
        "thetas": np.asarray(smpl_params["pose"]).astype(np.float32),
        "transl": np.asarray(smpl_params["trans"]).astype(np.float32),
    }
    np.savez(str(outdir / "poses.npz"), **smpl_params)


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

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

#You have to load the GP class also

#Analytic functions and training data

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

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

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

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

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

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

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

#c1 plot and quasi-newton method

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


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 11:52:31 2016

48. Rotate Image

Total Accepted: 69231 Total Submissions: 198347 Difficulty: Medium

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

先将矩阵转置，然后将矩阵的每一行翻转，就可以得到所要求的矩阵了。

@author: zeminzhang
"""

class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        for i in range(n):
            for j in range(i,n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        for i in range(n):
            for j in range(n/2):
                matrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]


from db import cnx, cursor

file = open('./data/test/matrikkelenVegadresse.csv', encoding="utf8")

query = "INSERT INTO boligrating_testdata.adresser(id, veinavn,nummer,bokstav,nummer_bokstav, postnummer, poststed, kommunenavn) VALUES(%s, %s, %s, %s, %s, %s, %s, %s)"

for l in file:
    line = l.split(';')
    if line[3] == 'vegadresse':
        data = (line[0],line[7],line[8],line[9],line[8] + line[9],line[19],line[20],line[2])
        cursor.execute(query, data)
        cnx.commit()

cursor.close()
cnx.close()