#!/usr/bin/env python2
# encoding: utf-8

# Author: adrien.lescourt@hesge.ch
# 04.2014, hepia

import sys
import os
from circ_manager import CircManager

def error_message(message, error_level='FATAL ERROR'):
    if error_level is not None:
        print('***', error_level, '***')
    print(message)
    sys.exit(0)

class LogiCompiler():

    specialchar = ['+', '-', '<<', '>>', '=', '[', ']', '#']

    comment_char = '#'

    opcode = {'+': '0000',
              '-': '0001',
              '<<': '0010',
              '>>': '0011',
              'asr': '0100',
              'and': '0101',
              'or': '0110',
              'not': '0111',
              '=': '1000',
              'par' : '1001',
              'bcz': '1010',
              'bcn': '1010',
              'bcc': '1010',
              'bcv': '1010',
              'b': '1011',
              '[': '1100',
              ']': '1101',
              'gr': '1110',
              'gw': '1111',
              }

    condition = {'bcz': '1000',
                 'bcn': '0100',
                 'bcc': '0010',
                 'bcv': '0001'}

    register = {'r0': '000',
                'r1': '001',
                'r2': '010',
                'r3': '011',
                'r4': '100',
                'r5': '101',
                'r6': '110',
                'r7': '111'}

    def get_binary_list(self, assembly_code):
        """
            Read the assembly_code list and return the corresponding binary_code list
        """
        self.current_line = 0
        binary_list = []
        (all_operands, label_list) = self.__get_operands(assembly_code)

        if self.__has_multiple_label([elem[0][0] for elem in label_list]):
            error_message('A label is present more than once!')

        for operands in all_operands:
            self.current_line += 1
            if operands is None:  # blank line or comment
                continue
            try:
                instruction = self.Instruction(operands, label_list)
            except:
                error_message('On line ' + str(self.current_line))
            if instruction.data is not None:
                if instruction.data is 'label':
                    print(operands[0][0])
                else:
                    print('N°', self.current_line-1, ':', instruction.data, '-', str.format('0x{:04X}', int(hex(int(instruction.data, 2)), 16)), ':', ' '.join(operands[0]))
                    binary_list.append(instruction.data)
            else:
                error_message('On line ' + str(self.current_line))
        return binary_list

    def __get_operands(self, assembly_code):
        """
            Return an operands list for every assembly line (operands, instruction_line),
            and an array of label (label, instruction_line)
        """
        current_line = 0
        instruction_line = 1
        all_operands = []
        label_list = []
        for assembly_line in assembly_code:
            current_line += 1
            assembly_line = self.__add_spaces(assembly_line)
            if len(assembly_line) < 1 or self.__is_comment(assembly_line):
                all_operands.append(None)
            else:
                operands = assembly_line.split()
                if self.__is_label(operands):
                    label_list.append((operands, instruction_line))
                else:
                    instruction_line += 1
                all_operands.append((operands, instruction_line - 1))
        return (all_operands, label_list)

    def __add_spaces(self, line):
        if '=' in line or '[' in line:
            for op in LogiCompiler.specialchar:
                line = line.replace(op, ' ' + op + ' ')
            return ' '.join(line.split()).strip()
        return line.strip()

    def __is_comment(self, line):
        return line[0] == LogiCompiler.comment_char

    def __is_label(self, operands):
        return operands[0].find(':') != -1

    def __has_multiple_label(self, label_list):
        return len(label_list) != len(set(label_list))

    class Instruction():
        def __init__(self, operands_with_line, label_with_line):
            self.load_size = 8
            self.jump_cond_size = 8
            self.jump_size = 12
            self.perif_size = 8
            operands = operands_with_line[0]
            line_nbr = operands_with_line[1]
            self.data = None
            operands_size = len(operands)
            if operands_size == 1:
                self.data = self.__get_binary_from_1_operands(operands)
            if operands_size == 2:
                self.data = self.__get_binary_from_2_operands(operands, line_nbr, label_with_line)
            if operands_size == 3:
                self.data = self.__get_binary_from_3_operands(operands)
            elif operands_size == 4:
                self.data = self.__get_binary_from_4_operands(operands)
            elif operands_size == 5:
                self.data = self.__get_binary_from_5_operands(operands)
            elif operands_size == 10:
                self.data = self.__get_binary_from_10_operands(operands)

        def __get_binary_from_1_operands(self, operands):
                if operands[0][-1] != ':' or not operands[0][:-1].isalnum():
                    error_message('Label must be alphanumeric and end with colon', None)
                    return
                return 'label'

        def __get_binary_from_2_operands(self, operands, line_nbr, label_with_line):
                if operands[0] not in LogiCompiler.opcode:
                    print('Error:', operands[0], 'not reconized')
                    return
                opcode = LogiCompiler.opcode[operands[0]]
                # bcz / bcn / bcc / bcv
                if operands[0] in LogiCompiler.condition:
                    cond = LogiCompiler.condition[operands[0]]
                    jump_offset = self.__get_jump_offset(operands[1], line_nbr, label_with_line)
                    jump = self.__num_to_binary(jump_offset, self.jump_cond_size)
                    return opcode + cond + jump
                # b
                elif operands[0] == 'b':
                    jump_offset = self.__get_jump_offset(operands[1], line_nbr, label_with_line)
                    jump = self.__num_to_binary(jump_offset, self.jump_size)
                    return opcode + jump

        def __get_jump_offset(self, operand, line_nbr, label_with_line):
            if self.__is_integer(operand):
                return operand
            else:
                label_pos = [elem[1] for elem in label_with_line if elem[0][0] == (operand + ':')]
                if not label_pos:
                    error_message('"' + operand + '" label not found')
                else:
                    return str(label_pos[0] - line_nbr)

        def __get_binary_from_3_operands(self, operands):
                if operands[1] not in LogiCompiler.opcode:
                    print('Error:', operands[1], 'not reconized')
                    return
                # Affectation
                opcode = LogiCompiler.opcode[operands[1]]
                result = LogiCompiler.register[operands[0]]
                reserved = '0'
                load = self.__num_to_binary(operands[2], self.load_size)
                return opcode + result + reserved + load

        def __get_binary_from_4_operands(self, operands):
                if operands[2] not in LogiCompiler.opcode:
                    print('Error:', operands[2], 'not reconized')
                    return
                # asr / not /parity
                opcode = LogiCompiler.opcode[operands[2]]
                result = LogiCompiler.register[operands[0]]
                source_0 = LogiCompiler.register[operands[3]]
                source_1 = '000'
                reserved = '000'
                return opcode + result + source_0 + source_1 + reserved

        def __get_binary_from_5_operands(self, operands):
                # ldr
                if operands[2] == '[' and operands[4] == ']':
                    opcode = LogiCompiler.opcode[operands[2]]
                    result = LogiCompiler.register[operands[0]]
                    reserved = '0'
                    perif = self.__num_to_binary(operands[3], self.perif_size)
                    return opcode + result + reserved + perif

                # str
                if operands[0] == '[' and operands[2] == ']':
                    opcode = LogiCompiler.opcode[operands[2]]
                    result = LogiCompiler.register[operands[4]]
                    reserved = '0'
                    perif = self.__num_to_binary(operands[1], self.perif_size)
                    return opcode + result + reserved + perif

                # add / sub / shift / or / and
                else:
                    if operands[3] not in LogiCompiler.opcode:
                        print('Error:', operands[3], 'not reconized')
                        return
                    opcode = LogiCompiler.opcode[operands[3]]
                    result = LogiCompiler.register[operands[0]]
                    source_0 = LogiCompiler.register[operands[2]]
                    source_1 = ''

                    if operands[3] == '<<' or operands[3] == '>>':
                        source_1 = self.__num_to_binary(operands[4], 3)
                    else:
                        source_1 = LogiCompiler.register[operands[4]]
                    reserved = '000'
                    return opcode + result + source_0 + source_1 + reserved

        def __get_binary_from_10_operands(self, operands):
            # GR / GW
            if operands[0] not in LogiCompiler.opcode:
                print('Error:', operands[0], 'not reconized')
                return
            opcode = LogiCompiler.opcode[operands[0]]
            data = LogiCompiler.register[operands[2]]
            ah = LogiCompiler.register[operands[5]]
            al = LogiCompiler.register[operands[8]]
            reserved = '000'
            return opcode + data + ah + al + reserved

        def __num_to_binary(self, num, bits):
            if len(num) >= 2:
                prefix = num[:2]
                if prefix == '0x' or prefix == '0X':
                    return self.__hex_to_binary(num, bits)
                elif prefix == '0b' or prefix == '0B':
                    return self.__bin_to_binary(num, bits)
            return self.__int_to_binary(num, bits)

        def __int_to_binary(self, num, bits):
            num = int(num)
            binary = ''
            while bits:
                binary = ('1' if num & 1 else '0') + binary
                bits = bits - 1
                num = num >> 1
            return binary

        def __hex_to_binary(self, hexnum, bits):
            return bin(int(hexnum, 16))[2:].zfill(bits)

        def __bin_to_binary(self, binnum, bits):
            if len(binnum) > bits+2:
                print(binnum, 'size must not be longer than', bits, 'bits')
                return
            elif len(binnum) <= 2:
                print(binnum, 'is too short')
                return
            return binnum[2:].zfill(bits)

        def __is_integer(self, nbr):
            try:
                int(nbr)
                return True
            except ValueError:
                return False

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('usage: ./logi_compiler lsn_assembly_file output_file.circ')
        sys.exit(0)
    in_file = sys.argv[1]
    out_file = sys.argv[2]
    assembly = [line.rstrip().lower() for line in open(in_file, 'r')]
    logicomp = LogiCompiler()
    binary_list = logicomp.get_binary_list(assembly)
    if len(binary_list) > 128:
        print(len(binary_list), 'instructions found. Max limit is 128')
        sys.exit(0)
    else:
        while(len(binary_list) < 128):
            binary_list.append('0000000000000000')
    circ_manager = CircManager()
    circ_manager.append_constant_from_binary_list(binary_list)
    if os.path.isfile(out_file):
        os.remove(out_file)
    f = open(out_file, 'w')
    for char in circ_manager.get_XML():
        f.write(char)
    f.close()
    print(out_file, 'successfully generated')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.utils.timezone
import django.core.validators
import django.contrib.gis.db.models.fields

class Migration(migrations.Migration):

    dependencies = [
        ('auth', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Player',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('password', models.CharField(max_length=128, verbose_name='password')),
                ('last_login', models.DateTimeField(default=django.utils.timezone.now, verbose_name='last login')),
                ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
                ('username', models.CharField(help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, max_length=30, verbose_name='username', validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username.', 'invalid')])),
                ('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
                ('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
                ('email', models.EmailField(max_length=75, verbose_name='email address', blank=True)),
                ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
                ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
                ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
                ('name', models.CharField(max_length=20)),
            ],
            options={
                'abstract': False,
                'verbose_name': 'user',
                'verbose_name_plural': 'users',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Business',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('business_name', models.TextField(max_length=100)),
                ('business_id', models.TextField(max_length=100)),
                ('full_address', models.TextField(max_length=200, null=True)),
                ('instagram_info', models.TextField(max_length=50, null=True)),
                ('twitter_info', models.TextField(max_length=50, null=True)),
                ('hours_start', models.DecimalField(help_text=b'Can Be Bank', null=True, max_digits=5, decimal_places=2, blank=True)),
                ('hours_end', models.DecimalField(help_text=b'Can Be Bank', null=True, max_digits=5, decimal_places=2, blank=True)),
                ('bu_latitude', models.DecimalField(help_text=b'Can Be Bank', null=True, max_digits=25, decimal_places=17, blank=True)),
                ('bu_longitude', models.DecimalField(help_text=b'Can Be Bank', null=True, max_digits=25, decimal_places=17, blank=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Categories',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('type', models.CharField(max_length=100)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Checkin',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('checkin_date', models.DateField(help_text=b'Date Created', null=True)),
                ('checkin_day', models.CharField(max_length=2, null=True, choices=[(b'1', b'Monday'), (b'2', b'Tuesday'), (b'3', b'Wednesday'), (b'4', b'Thursday'), (b'5', b'Friday'), (b'6', b'Saturday'), (b'7', b'Sunday')])),
                ('checkin_time', models.CharField(max_length=2, null=True, choices=[(b'A', b'10am to 11am'), (b'B', b'11am to 12pm'), (b'C', b'12pm to 1pm'), (b'D', b'1pm to 2pm'), (b'E', b'2pm to 3pm'), (b'F', b'3pm to 4pm'), (b'G', b'5pm to 6pm'), (b'H', b'6pm to 7pm'), (b'I', b'7pm to 8pm'), (b'J', b'8pm to 9pm'), (b'K', b'9pm to 10pm'), (b'L', b'10pm to 11pm'), (b'K', b'11pm to 12pm'), (b'M', b'12pm to 1am')])),
                ('checkin_total', models.IntegerField(max_length=1000, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='City',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('my_city', models.CharField(max_length=20, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Country',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('my_country', models.CharField(max_length=20, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Place',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=256, null=True)),
                ('location', django.contrib.gis.db.models.fields.PointField(srid=4326, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Rating',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('rating_date', models.DateField(help_text=b'Date Created', null=True)),
                ('rating_day', models.CharField(max_length=1, choices=[(b'A', b'Monday'), (b'B', b'Tuesday'), (b'C', b'Wednesday'), (b'D', b'Thursday'), (b'E', b'Friday'), (b'F', b'Saturday'), (b'G', b'Sunday')])),
                ('rating_text', models.TextField(null=True)),
                ('rating_number', models.DecimalField(help_text=b'Can Be Bank', null=b'True', max_digits=2, decimal_places=1)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='ZipCode',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('zip', models.IntegerField(max_length=6, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='business',
            name='bu_categories',
            field=models.ManyToManyField(related_name=b'business', to='my_yelp.Categories'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='business',
            name='bu_checkin',
            field=models.ManyToManyField(related_name=b'business', null=True, to='my_yelp.Checkin'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='business',
            name='bu_city',
            field=models.ForeignKey(related_name=b'business', to='my_yelp.City', null=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='business',
            name='bu_country',
            field=models.ForeignKey(related_name=b'business', to='my_yelp.Country', null=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='business',
            name='bu_rating',
            field=models.ManyToManyField(related_name=b'business', null=True, to='my_yelp.Rating'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='business',
            name='bu_zipcode',
            field=models.ForeignKey(related_name=b'business', to='my_yelp.ZipCode', null=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='player',
            name='checkin_user',
            field=models.ManyToManyField(related_name=b'player', null=True, to='my_yelp.Checkin'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='player',
            name='favorite_restaurant',
            field=models.ManyToManyField(related_name=b'player', null=True, to='my_yelp.Business'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='player',
            name='groups',
            field=models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of his/her group.', verbose_name='groups'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='player',
            name='rating_user',
            field=models.ManyToManyField(related_name=b'player', null=True, to='my_yelp.Rating'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='player',
            name='user_permissions',
            field=models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Permission', blank=True, help_text='Specific permissions for this user.', verbose_name='user permissions'),
            preserve_default=True,
        ),
    ]


import logging
from datetime import datetime, timedelta

from grab.spider import Task
from crawlers.zkb_base import ZkbBaseSpider
from esi.base import ESISpider

logger = logging.getLogger(__name__)

class ExtractAbyssalLoses(ESISpider, ZkbBaseSpider):
    zkb_api = 'https://zkillboard.com/api/kills/abyssal/page/{}/'
    esi_killmail = '/latest/killmails/{killmail_id}/{killmail_hash}/'

    number_of_pages = 100

    def task_generator(self):
        for page in range(1, self.number_of_pages):
            priority = page + 10
            yield Task('kills', url=self.zkb_api.format(page),
                       page=page, priority=priority)

    def task_kills(self, grab, task):
        data = self.load_api_data(grab)
        logger.info("Loaded page {} with {} kills".format(
            task.page, len(data.keys())))

        for killmail_id, killmail_hash in data.items():
            # if killmail already there continue
            obj = self.conn.kills.find_one({
                '_id': int(killmail_id),
                'abyssal': True
            })
            if obj is not None:
                logger.info('Killmail {} already there'.format(
                    killmail_id))
                continue

            # extract killmail from esi
            url = self.get_esi_url(self.esi_killmail.format(
                **{
                    'killmail_id': killmail_id,
                    'killmail_hash': killmail_hash
                }))
            yield Task('esi_killmail', url=url, priority=10)

    def task_esi_killmail(self, grab, task):
        data = self.load_api_data(grab)
        res = self.save_killmail(data, meta={'abyssal': True})
        if res:
            logger.info('Killmail {} saved.'.format(data['killmail_id']))

class ExtractLatestLosses(ZkbBaseSpider):
    zkb_api = 'https://zkillboard.com/api/losses/page/{}/'
    time_delta = None
    till_datetime = None

    def task_generator(self):
        api_page = 1
        if self.time_delta:
            self.till_datetime = datetime.utcnow() - \
                timedelta(days=self.time_delta)
        yield Task('losses',
                   url=self.zkb_api.format(api_page),
                   api_page=1)

    def task_losses(self, grab, task):
        """Kill id example: 69906212"""
        data = self.load_api_data(grab)
        print(data)
        logger.info('[{}] Got {} losses'.format(
            task.api_page, len(data)))

        need_next_page = True
        saved_count = 0
        for d in data:
            killmail_time = datetime.strptime(
                d['killmail_time'], "%Y-%m-%dT%H:%M:%SZ")

            if self.till_datetime:
                if killmail_time > self.till_datetime:

                    obj = self.conn.kills.find_one({
                        '_id': int(d['killmail_id']),
                    })
                    if obj is not None:
                        logger.info('Killmail {} already there'.format(
                            d['killmail_id']))
                        continue

                    self.save_killmail(d)
                    saved_count += 1
                else:
                    need_next_page = False

        logger.info('{} killmails saved.'.format(saved_count))

        # next page
        # if (task.api_page <= 1000) and data and need_next_page:
        if need_next_page:
            api_page = task.api_page + 1
            yield Task('losses',
                       url=self.zkb_api.format(api_page),
                       api_page=api_page)


import numpy as np
import chainer.functions as F
from chainer import cuda
from chainercv.links.model.fpn.misc import smooth_l1

class SmoothL1(object):
    def __init__(self, beta=1):
        self._beta = beta
        self._eps = 1e-5

    def __call__(self, loc, gt_loc, gt_label):
        xp = cuda.get_array_module(loc.array)
        loc = loc[xp.where(gt_label > 0)[0]]
        gt_loc = gt_loc[xp.where(gt_label > 0)[0]]
        n_sample = loc.shape[0] + self._eps
        loss = F.sum(smooth_l1(loc, gt_loc, self._beta)) / n_sample
        return loss

class SoftmaxCrossEntropy(object):
    def __call__(self, conf, gt_label):
        loss = F.softmax_cross_entropy(conf, gt_label)
        return loss

class FocalLoss(object):
    def __init__(self, alpha=0.25, gamma=2.0):
        self._alpha = alpha
        self._gamma = gamma
        self._eps = 1e-5

    def __call__(self, conf, gt_label):
        xp = cuda.get_array_module(gt_label)
        n_fg = max(xp.where(gt_label > 0)[0].shape[0], 1)
        n_class = conf.shape[-1]
        gt_label = xp.eye(n_class + 1, dtype=xp.int32)[gt_label][:, 1:]

        logit = F.sigmoid(conf)
        logit = F.clip(logit, self._eps, 1 - self._eps)

        alpha_factor = xp.ones_like(gt_label, dtype=xp.float32) * self._alpha
        alpha_factor = F.where(gt_label == 1, alpha_factor, 1 - alpha_factor)
        focal_weight = F.where(gt_label == 1, 1 - logit, logit)
        focal_weight = alpha_factor * focal_weight ** self._gamma

        loss = focal_weight.reshape(-1) * \
            F.sigmoid_cross_entropy(
                conf.reshape(-1), gt_label.reshape(-1), reduce='no')
        loss = F.sum(loss) / n_fg
        return loss


from common import Config, VocabType
from argparse import ArgumentParser
from interactive_pharo_predict import InteractivePredictor
from predictor_server import PredictorServer
from model import Model
import sys

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument("-te", "--test", dest="test_path",
                        help="path to test file", metavar="FILE", required=True)
    parser.add_argument("-l", "--load", dest="load_path",
                        help="path to save file", metavar="FILE", required=True)
    parser.add_argument("-o", "--output", dest="output_file",
                        help="path to save file", metavar="FILE", required=True)
    args = parser.parse_args()

    config = Config.get_default_config(args)

    model = Model(config)
    print('Created model')
    model.predict_dataset()
    model.close_session()


import sys, os
sys.path.append(os.pardir)
import cv2
import numpy as np
import common.function_03h as F

def main():

	path = "../../Data/Lane_Detection_Images/"
	roadImage_01 = "solidWhiteCurve.jpg"
	openPath = path+roadImage_01

	roadColor = F.imageRead(openPath, cv2.IMREAD_COLOR)
	roadHLS = F.convertColor(roadColor, cv2.COLOR_BGR2HLS)

	F.imageShow("roadHLS", roadHLS)
	h2, l2, s2 = F.splitImageByCV(roadHLS)

	F.imageShow("h2", h2)
	F.imageShow("l2", l2)
	F.imageShow("s2", s2)

	cv2.destroyAllWindows()

if __name__ == '__main__':
	main()


import json

from .database import db, Base
import core.case.subscription

class CaseSubscription(Base):
    __tablename__ = 'case_subscription'

    name = db.Column(db.String(255), nullable=False)
    subscription = db.Column(db.Text())

    def __init__(self, name, subscription='{}'):
        self.name = name
        try:
            json.loads(subscription)
            self.subscription = subscription
        except:
            self.subscription = '{}'

    def as_json(self):
        return {"name": self.name,
                "subscription": json.loads(self.subscription)}

    @staticmethod
    def update(case_name):
        case = CaseSubscription.query.filter_by(name=case_name).first()
        if case:
            if case_name in core.case.subscription.subscriptions:
                case.subscription = json.dumps(core.case.subscription.subscriptions_as_json()[case_name])

    @staticmethod
    def from_json(name, subscription_json):
        return CaseSubscription(name, subscription=json.dumps(subscription_json))

    @staticmethod
    def sync_to_subscriptions():
        subscriptions = {case.name: core.case.subscription.CaseSubscriptions.from_json(json.loads(case.subscription))
                         for case in CaseSubscription.query.all()}
        core.case.subscription.set_subscriptions(subscriptions)


import os
import sys

##############################################################
#Variable you need to modify
png_file_number = 75    #rgb file and depth file number 486 450
##############################################################

depth_origin_folder = "./depth"
rgb_origin_folder = "./rgb"

depth_out_folder = './depth_out'
rgb_out_folder = './rgb_out'

def cleanAllFilesInFolder(dirs):
    if os.path.exists(dirs):
        for i in os.listdir(dirs):
            #print("name: ",i)
            os.remove(dirs+"/"+i)
        print ("Now in folder, there exists : %s"%(os.listdir(dirs)))
    else:
        print("dirs not exist: ",dirs)

def tryCreateFolder(dirs):
    if not os.path.exists(dirs):
        os.makedirs(dirs)

if __name__ == "__main__":

    #create rgb_out folder and depth_out folder if needed
    tryCreateFolder(depth_out_folder)
    tryCreateFolder(rgb_out_folder)

    #try clean folder
    cleanAllFilesInFolder(depth_out_folder)
    cleanAllFilesInFolder(rgb_out_folder)

    #run ShapeLab command
    main = "ShapeLab.exe" + " RGB "+ str(png_file_number) + " DEPTH "+str(png_file_number)
    r_v = os.system(main)
    print (r_v )

    _dir = './'
    #change depth_out_folder and rgb_out_folder name to others
    os.rename(os.path.join(_dir, "rgb_out"), os.path.join(_dir,"rgb_ppm"))
    os.rename(os.path.join(_dir, "depth_out"), os.path.join(_dir,"depth_pgm"))


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

import json
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from gensim.models import Word2Vec
from keras_contrib.layers import CRF
from keras_contrib.losses import crf_loss
from keras_contrib.metrics import crf_accuracy, crf_viterbi_accuracy
from keras.models import Model, Input, Sequential
from keras.layers import Embedding, Dense, Bidirectional, Dropout, LSTM, TimeDistributed, Masking
from keras.utils import to_categorical, plot_model
from seqeval.metrics import classification_report
import matplotlib.pyplot as plt

from dataset import data_trans

MAX_SEQ_LEN = 200 #训练集中最长语句长度为1080

# 读取训练集，验证集和测试集原始数据
_, origin_train_X, origin_train_y = data_trans('dataset/train.txt')
_, origin_dev_X, origin_dev_y = data_trans('dataset/dev.txt')
_, origin_test_X, origin_test_y = data_trans('dataset/test.txt')

# 利用ALBERT提取文本特征

# label2id字典
label_id_dict = {
  "O": 1,
  "B-SUB": 2,
  "I-SUB": 3,
  "B-BOD": 4,
  "I-BOD": 5,
  "B-DEC": 6,
  "I-DEC": 7,
  "B-FRE": 8,
  "I-FRE": 9,
  "B-ITE": 10,
  "I-ITE": 11,
  "B-DIS": 12,
  "I-DIS": 13,
}

id_label_dict = {v:k for k,v in label_id_dict.items()}

#词向量训练,返回序列化的输入值和embedding矩阵
def word2sequence ():

    sentences = origin_train_X + origin_test_X + origin_dev_X
    tokenizer = Tokenizer(lower=False, char_level=True)
    tokenizer.fit_on_texts(sentences)
    word_index = tokenizer.word_index  # 针对语料中所有字

    train_X = tokenizer.texts_to_sequences(origin_train_X)
    dev_X = tokenizer.texts_to_sequences(origin_dev_X)
    test_X = tokenizer.texts_to_sequences(origin_test_X)

    train_X = pad_sequences(train_X, maxlen=MAX_SEQ_LEN, padding='post')  #序列化的语料
    dev_X = pad_sequences(dev_X, maxlen=MAX_SEQ_LEN, padding='post')
    test_X = pad_sequences(test_X, maxlen=MAX_SEQ_LEN, padding='post')

    model = Word2Vec(sentences, size=100, min_count=3)
    # word_vec_dict = dict((k, model.wv[k]) for k, v in model.wv.vocab.items())
    # model.save( 'word2vec')

    embedding_word2vec_matrix = np.zeros((len(word_index) + 1, 100))
    for word, i in word_index.items():
        embedding_vector = model[word] if word in model else None
        if embedding_vector is not None:
            embedding_word2vec_matrix[i] = embedding_vector
        else:
            unk_vec = np.random.random(100) * 0.5
            unk_vec = unk_vec - unk_vec.mean()
            embedding_word2vec_matrix[i] = unk_vec

    return train_X, dev_X, test_X, word_index

# 将标签序列y转化为one-hot编码
def one_hot_trans(tags):

    # 对y值统一长度为MAX_SEQ_LEN
    new_y = []
    for seq in tags:
        num_tag = [label_id_dict[_] for _ in seq]
        if len(seq) < MAX_SEQ_LEN:
            num_tag = num_tag + [0] * (MAX_SEQ_LEN-len(seq))
        else:
            num_tag = num_tag[: MAX_SEQ_LEN]

        new_y.append(num_tag)

    np.array(new_y)

    # 将y中的元素编码成ont-hot encoding
    y = np.empty(shape=(len(tags), MAX_SEQ_LEN, len(label_id_dict.keys())+1))

    for i, seq in enumerate(new_y):
        y[i, :, :] = to_categorical(seq, num_classes=len(label_id_dict.keys())+1)

    return y

# Build model
def build_model(n_tags, vocab):
    # Bert Embeddings
    #bert_output = Input(shape=(max_para_length, 312, ), name="bert_output")

    # model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

    input_layer = Input(shape=(MAX_SEQ_LEN,))
    embedding_layer = Embedding(
        input_dim=len(vocab) + 1,
        output_dim=100,
        input_length=MAX_SEQ_LEN)   #trainable=False使用预训练的词向量
    bi_lstm_layer = Bidirectional(LSTM(128, return_sequences=True))
    bi_lstm_drop_layer = Dropout(0.5)
    dense_layer = TimeDistributed(Dense(n_tags, activation="softmax"))
    crf_layer = CRF(n_tags)

    input = input_layer
    embedding = embedding_layer(input)
    bi_lstm = bi_lstm_layer(embedding)
    bi_lstm_drop = bi_lstm_drop_layer(bi_lstm)
    dense = dense_layer(bi_lstm_drop)
    crf = crf_layer(dense)

    model = Model(input=[input], output=[crf])
    model.compile(optimizer='adam', loss=crf_layer.loss_function, metrics=[crf_layer.accuracy])

    """check_pointer = ModelCheckpoint(filepath='best_model.hdf5', verbose=1, save_best_only=True)
    hist = model.fit(train_x, train_y, batch_size=32, epochs=20, verbose=2, validation_data=[val_x, val_y],
                     callbacks=[check_pointer])
    model.load_weights('best_model.hdf5')

    test_y_pred = model.predict(test_x).argmax(-1)
    test_y_true = test_y[:, :, 0]"""

    # 模型结构总结
    model.summary()
    #plot_model(model, to_file="lstm_crf.png", show_shapes=True)

    return model

# 模型训练
def train_model():

    # 得到模型可直接使用的训练集，验证集和测试集数据
    train_x, dev_x, test_x, vocab = word2sequence()
    train_y = one_hot_trans(origin_train_y)
    dev_y = one_hot_trans(origin_dev_y)
    #test_y = one_hot_trans(origin_test_y)

    # 模型训练
    model = build_model(len(label_id_dict.keys())+1, vocab)
    history = model.fit(train_x, train_y, validation_data=(dev_x, dev_y), batch_size=16, epochs=10)

    model.save("lstm_crf_ner.h5")

    # 绘制loss和acc图像
    plt.subplot(2, 1, 1)
    epochs = len(history.history['loss'])
    plt.plot(range(epochs), history.history['loss'], label='loss')
    plt.plot(range(epochs), history.history['val_loss'], label='val_loss')
    plt.legend()

    plt.subplot(2, 1, 2)
    epochs = len(history.history['crf_viterbi_accuracy'])
    plt.plot(range(epochs), history.history['crf_viterbi_accuracy'], label='crf_viterbi_accuracy')
    plt.plot(range(epochs), history.history['val_crf_viterbi_accuracy'], label='val_crf_viterbi_accuracy')
    plt.legend()
    plt.savefig("lstm_crf_loss_acc.png")

    # 模型在测试集上的表现
    """
    # 预测标签
    y = np.argmax(model.predict(test_x), axis=2)
    pred_tags = []
    for i in range(y.shape[0]):
        pred_tags.append([id_label_dict[_] for _ in y[i] if _])

    # 因为存在预测的标签长度与原来的标注长度不一致的情况，因此需要调整预测的标签
    test_sents, test_tags = origin_test_X, origin_test_y
    final_tags = []
    for test_tag, pred_tag in zip(test_tags, pred_tags):
        if len(test_tag) == len(pred_tag):
            final_tags.append(pred_tag)
        elif len(test_tag) < len(pred_tag):
            final_tags.append(pred_tag[:len(test_tag)])
        else:
            final_tags.append(pred_tag + ['O'] * (len(test_tag) - len(pred_tag)))

    # 利用seqeval对测试集进行验证
    print(classification_report(test_tags, final_tags, digits=4))

    """

if __name__ == '__main__':
    train_model()


# -*- coding: utf-8 -*

import lxml.etree
import pickle

JOBS = {"1" : "Воин", "2":"Лучница", "3":"Волшебница", "4":"Клирик", "5": "Механик", "6": "Кали", "11": "Мечник",
        "12": "Наемник", "14": "Охотница", "15": "Амазонка", "17": "Колдунья", "18":"Заклинательница",
        "20": "Паладин", "22": "Священник", "23": "Гладиатор", "24": "Мастер клинка",
        "25": "Берсерк", "26": "Джаггернаут", "29": "Снайпер", "30": "Следопыт",
        "31": "Валькирия", "32": "Повелительница ветра", "35": "Пиромант", "36": "Криомант",
        "37": "Ведьма", "38": "Чернокнижница", "41": "Хранитель", "42": "Крестоносец",
        "43": "Епископ", "44": "Инквизитор"}

SKILLTYPE = {"0" : "Моментальное", "1": "Пассивное", "2": "2?", "3": "Специальное"}

TARGETTYPE = {"0": "Игрок", "1": "Противник", "2": "2", "3": "Группа", "4": "Смешанная"}

ELEMENT = {"-1": "Нет", "0": "Огонь", "1": "Лёд", "2": "Свет", "3": "Тьма"}

def findElem(elem, param, value):
    for tag in a:
        if tag.get(param) == value:
            return tag

def choice(table, param, value):
    l = []
    for i in table:
        if i[param] == value:
            l.append(i)

    return l

def getTemplate(name, html):
    temp = """<!DOCTYPE html>

      <meta charset="UTF-8">
      <title>""" + name + """</title>
      <link rel="stylesheet" href="style.css">

""" + html + """

      """

    return temp

def getTable(l):
    body = ""
    for i in l:
        body += "
"
        for elem in i:
            body += "" + str(elem) + "	"
        body += "
"

    return body

def readTable(path):
    tableFile = open(path, 'r', encoding='utf-8')
    head = [i.rstrip() for i in tableFile.readline().split(',')]

    table = []

    for line in tableFile.readlines():
        l = {}
        for i, elem in enumerate(line.split(',')):
            try:
                l[head[i]] = elem.rstrip()
            except:
                pass
        table.append(l)

    return table

a = lxml.etree.parse('uistring.xml').getroot()

tableSkill = readTable('tables/skilltable_character.dnt.csv')
tableClass = readTable('tables/skillleveltable_characterwarriorpve.dnt.csv')

for line in tableSkill:
    print(line["id"])

    name = findElem(a, "mid", line["NameID"]).text
    skill = choice(tableClass, "SkillIndex", line["id"])
    body = '
'

    description = findElem(a, "mid", skill[0]["SkillExplanationID"]).text.replace("\\n", "
")
    description = description.replace("#y", '')
    description = description.replace("#w", '')

    print(skill[0]["SkillExplanationIDParam"])
    print(skill[0]["SkillExplanationIDParam"].split(';'))
    for i, elem in enumerate(skill[0]["SkillExplanationIDParam"].split(';')):
        description = description.replace("{" + str(i) + "}", elem)

    desk = [
        ["Название умения:", name],
        ["Требуемый класс:", JOBS[line["NeedJob"]]],
        ["Тип умения:", SKILLTYPE[line["SkillType"]]],
        ["Требуемый уровень персонажа:", skill[0]["LevelLimit"]],
        ["Максимальный уровень умения:", line["MaxLevel"]],
        ["Время перезарядки:", str(int(skill[0]["DelayTime"]) / 1000) + " сек"],
        ["Цель:", TARGETTYPE[line["TargetType"]]],
        ["Суперброня", skill[0]["StartSuperArmor"]],
        ["Стихия:", ELEMENT[line["Element"]]],
        ["Требуемое оружие:", "0"],
        ["Стоимость умения:", skill[0]["NeedSkillPoint"] + " очко(а) умения"],
        ["Стоимость изучения:", str(int(line["UnlockPrice"]) / 10000) + " золота"]
    ]

    body += getTable(desk) + "
"

    body += "
" + description + "
"

    open(line["id"] + ".html", 'w', encoding="UTF-8").write(getTemplate(name, body))


from numpy import hamming, array, isnan
from repoze.lru import lru_cache
from scikits.talkbox.linpred import levinson_lpc

class LPCExtractor(object):
    def __init__(self, fs, win_length_ms, win_shift_ms, n_lpc,
                 pre_emphasis_coef):
        self.PRE_EMPH = pre_emphasis_coef
        self.n_lpc = n_lpc

        self.FRAME_LEN = int(float(win_length_ms) / 1000 * fs)
        self.FRAME_SHIFT = int(float(win_shift_ms) / 1000 * fs)
        self.window = hamming(self.FRAME_LEN)

    def lpcc(self, signal):
        lpc = levinson_lpc.lpc(signal, self.n_lpc)[0]
        return lpc[1:]

    def extract(self, signal):
        frames = (len(signal) - self.FRAME_LEN) / self.FRAME_SHIFT + 1
        feature = []
        for f in xrange(frames):
            frame = signal[f * self.FRAME_SHIFT: f * self.FRAME_SHIFT +
                                                 self.FRAME_LEN] * self.window
            frame[1:] -= frame[:-1] * self.PRE_EMPH
            feature.append(self.lpcc(frame))

        feature = array(feature)
        feature[isnan(feature)] = 0
        return feature

@lru_cache(maxsize=2)
def get_lpc_extractor(fs, win_length_ms=32, win_shift_ms=16,
                      n_lpc=15, pre_emphasis_coef=0.95):
    ret = LPCExtractor(fs, win_length_ms, win_shift_ms, n_lpc, pre_emphasis_coef)
    return ret

def extract(fs, signal=None, **kwargs):
    ret = get_lpc_extractor(fs, **kwargs).extract(signal)
    return ret


"""
<description>
"""

import pytest as pt
import teetool as tt

def test_visual_2d():
    """
    produce figures
    """

    mdim = 2
    mtraj = 20

    # build world
    world_1 = tt.World(name="Example 3D", ndim=2, resolution=100)

    # extreme reduced resolution
    #world_1.setResolution(xstep=3, ystep=3, zstep=3)

    # add trajectories
    for mtype in [0, 1]:
        correct_cluster_name = "toy {0}".format(mtype)
        correct_cluster_data = tt.helpers.get_trajectories(mtype, ndim=mdim, ntraj=mtraj)
        world_1.addCluster(correct_cluster_data, correct_cluster_name)

    list_iclusters = [0, 1]

    # model all trajectories
    settings = {}
    settings["model_type"] = "resampling"
    settings["ngaus"] = 10

    world_1.buildModel(settings)

    # visuals by
    visual = tt.visual_2d.Visual_2d(world_1)
    # visualise trajectories
    visual.plotTrajectories(list_iclusters)
    # visualise intersection
    visual.plotLogLikelihood()
    # close
    visual.close()


#!/usr/bin/env python3

from aiohttp import web
import traceback
import logging

from pony.orm import Database, Set, Required, db_session, Json, composite_key
from pony.orm.core import MappingError
from validate_email import validate_email

log = logging.getLogger("server")

db = Database()
db.bind('sqlite', ':memory:')
max_name_len = 255

class Error(Exception):
    """ Some module-specific exception as recommended by best practices. """

def my_dict(e):
    """ My super-duper recursive serializer.
        Like pony.orm.serialization.to_dict,
        but dereferences collections into dict/list instead of ids """
    if isinstance(e, db.Entity):
        return e.to_dict()
    if isinstance(e, list):
        return [my_dict(e) for e in e]
    elif isinstance(e, dict):
        return {k: my_dict(v) for k, v in e.items()}
    return e

def validate_emails(emails):
    """ Performs very basic email verification, as many best-practices suggest.
        It could also verify existence of domain name via DNS, but that would
        be overkill for this task, imho.
    """
    if not isinstance(emails, list):
        return False
    if len(emails) < 1:   # as specified in the task
        return False
    if len(emails) > 10:  # suspiciously too many emails
        return False
    return all(validate_email(e) for e in emails)

class Person(db.Entity):
    """ Defines person's representation in the DB.
        The tuple (first name, last name) must be unique (ensured by composite_key)
    """

    fname = Required(str, max_name_len)
    lname = Required(str, max_name_len)
    composite_key(fname, lname)  # unique constraint on first and last names
    emails = Required(Json, py_check=validate_emails)
    groups = Set("Group")
    address_books = Set("AddressBook")

class Group(db.Entity):
    name = Required(str, max_name_len, unique=True)
    members = Set(Person)
    address_books = Set("AddressBook")

class AddressBook(db.Entity):
    name = Required(str, max_name_len, unique=True)
    people = Set(Person)
    groups = Set(Group)

def args_from_uri(f):
    """ Extracts arguments from URI according to URI pattern.
        Like for a path "/plans/678/" and pattern "/plans/{id}/"
        it would extract id=678 and pass it as an argument.
    """
    async def match(self, request):
        args = request.match_info
        return await f(self, request, **args)
    return match

class OhMyRestRouter:
    """ Simple REST router for models like rest_framework.serializers.ModelSerializer. """

    def __init__(self, app, model, name=None):
        self.app = app
        self.model = model
        if name is None:
            name = model.__name__.lower()
        prefix = "/{name}".format(name=name)
        uri_name = lambda method: "{name}_{method}".format(
            name=name, method=method)
        app.router.add_route(
            'GET', prefix + r"/{id:\d+}", self.get, name=uri_name('get'))
        app.router.add_route(
            'PUT',
            prefix +
            r"/{id:\d+}/addto/{field:[a-z]+}",
            self.add,
            name=uri_name('add'))
        app.router.add_route('PUT', prefix, self.put, name=uri_name('put'))
        app.router.add_route('DELETE', prefix +
                             r"/{id:\d+}", self.delete, name=uri_name('del'))

    @args_from_uri
    async def get(self, request, id):
        id = int(id)
        with db_session:
            instance = self.model[id]
            instance_data = instance.to_dict(with_collections=True)
            result = my_dict(instance_data)
        return web.json_response(result)

    async def put(self, request):
        json_data = await request.json()
        log.debug("adding to the database: %s", json_data)
        with db_session:
            instance = self.model(**json_data)
            instance.flush()  # commit instance to populate id
            instance_data = instance.to_dict(with_collections=True)
        return web.json_response(instance_data)

    @args_from_uri
    async def add(self, request, id, field):
        id = int(id)
        if not hasattr(self.model, field):
            raise Error("no such field: %s" % field)
        field = getattr(self.model, field)
        assert isinstance(field, Set), "we can add only to Set() field"
        model = field.py_type  # model of item we are going to add

        json_data = await request.json()

        with db_session:
            instance = self.model[id]
            item = model[json_data['id']]
            collection = getattr(instance, field.name)  # get related field
            collection.add(item)
            data = instance.to_dict(with_collections=True)
            result = my_dict(data)
        return web.json_response(result)

    @args_from_uri
    async def delete(self, request, id):
        id = int(id)
        with db_session:
            self.model[id].delete()

async def error_middleware(app, handler):
    """ Formats backend errors into json message. """
    def json_error(message):
        return web.json_response({'error': message}, status=500)

    async def middleware_handler(request):
        try:
            response = await handler(request)
            return response
        except Exception as ex:
            tb = traceback.format_exc(limit=100)
            log.error("error while serving request:\n%s", tb)

            return json_error(str(ex))
    return middleware_handler

async def find_by_name(request):
    params = request.GET
    fname = params.get('fname')
    lname = params.get('lname')
    response = []
    assert fname or lname, "you need at least one parameter: fname, lname or both"

    with db_session:
        query = Person.select()
        if fname:
            query = query.filter(fname=fname)
        if lname:
            query = query.filter(lname=lname)

        for person in list(query):
            person = person.to_dict(with_collections=True)
            response.append(person)
    return web.json_response(response)

async def find_by_email(request):
    email = request.GET['email']
    response = []
    with db_session:
        query = Person.select(lambda p: email in p.emails)
        for person in list(query):
            person = person.to_dict(with_collections=True)
            response.append(person)
    return web.json_response(response)

def setup_app(loop=None):
    app = web.Application(middlewares=[error_middleware], loop=loop)
    OhMyRestRouter(app=app, model=Person)
    OhMyRestRouter(app=app, model=Group)
    OhMyRestRouter(app=app, model=AddressBook)
    try:

        db.generate_mapping(create_tables=True)
    # ignoring "Mapping was already generated"
    # when py.test runs setup_app second/third time to make new fixtures
    except MappingError:
        pass
    app.router.add_route('GET', r"/person/find/",
                         find_by_name, name='person-find')

    app.router.add_route('GET', r"/person/find-by-email/",
                         find_by_email, name='person-find-by-email')

    return app

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(
        description="Welcome to the most advanced calendar backend in the World!")
    parser.add_argument('--routes', default=False,
                        action='store_true', help="show routes")
    parser.add_argument('--shell', default=False,
                        action='store_true', help="launch IPython shell")
    parser.add_argument('--no-setup', default=False,
                        action='store_true', help="do not setup routes")
    parser.add_argument('-d', '--debug', default=False,
                        action='store_true', help="debug logging")
    args = parser.parse_args()

    if not args.no_setup:
        app = setup_app()

    if args.routes:
        if args.no_setup:
            print("please remove --no-setup to see routes")
        else:
            print("===== routes ======")
            for resource in app.router.resources():
                print("  ", resource)
            print("== end of routes ==")

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    if args.shell:
        from IPython import embed
        print("nothing really to do, spawning an interactive shell")
        with db_session:
            embed()
    else:
        web.run_app(app)


from types import SimpleNamespace
import itertools
import numpy as np
from pyscf import gto, scf, dft
import shutil
from .calculator_base import CalculatorBase

class CalculatorPySCF(CalculatorBase):

    def __init__(self, atoms, atom_coords, basis, scf, units, molname, savefile):

        CalculatorBase.__init__(
            self, atoms, atom_coords, basis, scf, units, molname, 'pyscf', savefile)

    def run(self):
        """Run the scf calculation using PySCF."""

        # refresh the atom positions if necessary
        atom_str = self.get_atoms_str()

        # pyscf calculation
        mol = gto.M(
            atom=atom_str,
            basis=self.basis_name,
            unit=self.units,
            cart=False)

        if self.scf.lower() == 'hf':
            pyscf_data = scf.RHF(mol).run()

        elif self.scf.lower() == 'dft':
            pyscf_data = dft.RKS(mol)
            pyscf_data.xc = 'lda, vwn'
            pyscf_data = pyscf_data.newton()
            pyscf_data.kernel()

        if self.savefile:
            save_file_name = self.molname + '_pyscf.chkfile'
            shutil.copyfile(pyscf_data.chkfile, save_file_name)
            self.savefile = save_file_name

        basis = self.get_basis_data(mol, pyscf_data)
        return basis

    def get_basis_data(self, mol, rhf):
        """Save the data to HDF5

        Arguments:
            mol {pyscf.gto.M} -- psycf Molecule
            rhf {pyscf.scf} -- scf object
        """

        kx = {0: [0], 1: [1, 0, 0], 2: [2, 1, 1, 0, 0, 0]}
        ky = {0: [0], 1: [0, 1, 0], 2: [0, 1, 0, 2, 1, 0]}
        kz = {0: [0], 1: [0, 0, 1], 2: [0, 0, 1, 0, 1, 2]}

        basis = SimpleNamespace()
        basis.TotalEnergy = rhf.e_tot
        basis.radial_type = 'gto_pure'
        basis.harmonics_type = 'cart'

        # number of AO / MO
        # can be different if d or f orbs are present
        # due to cart 2 sph harmonics
        basis.nao = len(mol.cart_labels())
        basis.nmo = rhf.mo_energy.shape[0]

        # nshells is the number of bas per atoms
        nshells = [0] * mol.natm

        # init bas properties
        bas_coeff, bas_exp = [], []
        index_ctr = []
        bas_n, bas_l = [], []
        bas_kx, bas_ky, bas_kz = [], [], []
        bas_n = []
        bas_n_ori = self.get_bas_n(mol)

        iao = 0
        for ibas in range(mol.nbas):

            # number of contracted gaussian in that bas
            # nctr = mol.bas_nctr(ibas)
            nctr = mol.bas_nprim(ibas)

            # number of ao from that bas
            mult = mol.bas_len_cart(ibas)

            # quantum numbers
            n = bas_n_ori[ibas]
            lval = mol.bas_angular(ibas)

            # get qn per bas
            bas_n += [n] * nctr * mult
            bas_l += [lval] * nctr * mult

            # coeffs/exp
            bas_coeff += mol.bas_ctr_coeff(
                ibas).flatten().tolist() * mult
            bas_exp += mol.bas_exp(ibas).flatten().tolist() * mult

            # number of shell per atoms
            nshells[mol.bas_atom(ibas)] += nctr * mult

            for m in range(mult):
                index_ctr += [iao] * nctr
                iao += 1

            for k in kx[lval]:
                bas_kx += [k] * nctr

            for k in ky[lval]:
                bas_ky += [k] * nctr

            for k in kz[lval]:
                bas_kz += [k] * nctr

        bas_norm = []
        for expnt, lval in zip(bas_exp, bas_l):
            bas_norm.append(mol.gto_norm(lval, expnt))

        basis.nshells = nshells
        basis.index_ctr = index_ctr
        intervals = np.concatenate(([0], np.cumsum(nshells)))

        basis.nao_per_atom = []
        for i in range(len(intervals)-1):
            s, e = intervals[i], intervals[i+1]
            nao = len(np.unique(basis.index_ctr[s:e]))
            basis.nao_per_atom.append(nao)

        # determine the number of contraction per
        # atomic orbital
        basis.nctr_per_ao = np.array(
            [len(list(y)) for _, y in itertools.groupby(index_ctr)])

        basis.bas_coeffs = np.array(bas_coeff)
        basis.bas_exp = np.array(bas_exp)
        basis.bas_norm = np.array(bas_norm)

        basis.bas_n = bas_n
        basis.bas_l = bas_l

        # the cartesian gto are all :
        #   x^a y^b z^c exp(-zeta r)
        # i.e. there is no r dependency
        basis.bas_kr = np.zeros_like(basis.bas_exp)

        basis.bas_kx = np.array(bas_kx)
        basis.bas_ky = np.array(bas_ky)
        basis.bas_kz = np.array(bas_kz)

        # molecular orbitals
        basis.mos = mol.cart2sph_coeff() @ rhf.mo_coeff
        basis.mos = self.normalize_columns(basis.mos)

        # atom coords
        basis.atom_coords_internal = self.atom_coords

        return basis

    def get_atoms_str(self):
        """Refresh the atom string (use after atom move). """
        atoms_str = ''
        natom = len(self.atoms)

        for iA in range(natom):
            atoms_str += self.atoms[iA] + ' '
            atoms_str += ' '.join(str(xi)
                                  for xi in self.atom_coords[iA])
            atoms_str += ';'
        return atoms_str

    @staticmethod
    def get_bas_n(mol):

        label2int = {'s': 1, 'p': 2, 'd': 3}
        labels = [l[:3] for l in mol.cart_labels(fmt=False)]
        unique_labels = []
        for l in labels:
            if l not in unique_labels:
                unique_labels.append(l)
        nlabel = [l[2][1] for l in unique_labels]
        n = [label2int[nl] for nl in nlabel]
        return n


#!/bin/python3

import numpy
import sounddevice as SD
from time import time

SAMPLE_RATE = 44100
FREQ = 440
CHANNELS = 1
WINDOW_SIZE = 1024

SD.default.device = 'system'
SD.default.samplerate = SAMPLE_RATE
SD.default.blocksize =WINDOW_SIZE
SD.default.channels = CHANNELS
Stim = SD.OutputStream(dtype='float64')

def makewave(blocks=1, freq=FREQ, samplerate=SAMPLE_RATE):
    return [numpy.sin(2 * numpy.pi * freq * x / samplerate) for x in range(0, blocks*WINDOW_SIZE)]

def play(wave, samplerate=SAMPLE_RATE):
    Stim.write(wave)

def stop():
    SD.stop()

def play_block(blocks=1, freq=FREQ, samplerate=SAMPLE_RATE):
    start_time = time()
    wave = makewave(blocks, freq, samplerate)
    print("make wave:", time() - start_time)
    start_time = time()
    print("wave len:", len(wave))
    play(wave, samplerate)
    SD.wait()
    print("play wave time:", time() - start_time)

Stim.start()
twI = makewave(2, 440)
twII = makewave(2, 800)
testwave = [0.25 * x + 0.5 * y for x, y in zip(twI, twII)]
Stim.write(testwave)


from itertools import takewhile

from regulations.generator.node_types import label_to_text

class SectionBySectionLayer(object):
    shorthand = 'sxs'

    def __init__(self, layer):
        self.layer = layer
        # Perform the computations we'll use when applying the layer only once
        self.precomputations = {}
        for key in layer:
            key_parts = key.split('-')

            # Interpretations have an added complication:
            # 1005-2-Interp-2 is a child of 1005-Interp
            key_prefix = list(takewhile(lambda k: k != 'Interp', key_parts))
            self.precomputations[key] = (key_parts, key_prefix,
                                         'Interp' in key_parts)

    def to_template_dict(self, key):
        """Take the reference associated with this SxS and turn it into data
        which can be used in the template"""
        # We care only about the latest
        doc_number, label_id = self.layer[key][-1]['reference']
        return [{'doc_number': doc_number,
                 'label_id': label_id,
                 'text': label_to_text(label_id.split('-'),
                                       include_section=False,
                                       include_marker=True)}]

    def apply_layer(self, text_index):
        """Return a pair of field-name + analyses if they apply; include all
        children"""
        analyses = []
        requested = text_index.split('-')
        requested_prefix = list(takewhile(lambda k: k != 'Interp', requested))

        for key in self.layer:
            key_parts, key_prefix, key_is_interp = self.precomputations[key]
            key_prefix = key_prefix[:len(requested_prefix)]
            if (requested[-1] == 'Interp' and key_prefix == requested_prefix
                    and key_is_interp):
                analyses.extend(self.to_template_dict(key))

            # Simple Case: lexical child
            elif (key_parts[:len(requested)] == requested
                  and ('Interp' in requested) == key_is_interp):
                analyses.extend(self.to_template_dict(key))

        if analyses:
            return 'analyses', sorted(analyses, key=lambda a: a['label_id'])


from tqdm import tqdm
import argparse
import math
import model
import cv2
import sys
import pandas as pd
sys.setrecursionlimit(15000)
import os
import torch
import numpy as np
from torch.autograd import Variable
from sklearn import metrics
from PIL import Image
import numpy as np
import torch.utils.data
import torchvision.transforms as transforms
from statistics import mean

parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default ='dataset')
parser.add_argument('--test_set', default ='dataset/validation')
parser.add_argument('--real', default ='validation/real')
parser.add_argument('--fake', default ='validation/fake')
parser.add_argument('--batchSize', type=int, default=10)
parser.add_argument('--imageSize', type=int, default=128)
parser.add_argument('--gpu_id', type=int, default=0)
parser.add_argument('--outf', default='checkpoints/deepfakes')
parser.add_argument('--random_sample', type=int, default=0)
parser.add_argument('--random', action='store_true', default=False)
parser.add_argument('--id',type=int,default=100)
parser.add_argument('--input_dim', type=int, default=3, help='input dimension of lstm')
parser.add_argument('--hidden_dim', type=int, default=256, help='hidden dimension of lstm')
parser.add_argument('--layer_dim', type=int, default=1, help='layer dimension of lstm')
parser.add_argument('--output_dim', type=int, default=2, help='output dimension of lstm')
opt = parser.parse_args()
print(opt)

img_ext_lst = ('jpg', 'jpeg', 'bmp', 'png', 'ppm', 'gif', 'tiff')
vid_ext_lst = ('avi', 'mkv', 'mpeg', 'mpg', 'mp4')

def get_file_list(path, ext_lst):
    file_lst = []

    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path, f)):
            if f.lower().endswith(ext_lst):
                file_lst.append(f)

    return file_lst

def extract_file_name_without_count(in_str, sep_char='@'):
    n = len(in_str)
    pos = 0
    for i in range(n):
        if in_str[i] == sep_char:
            pos = i

    return in_str[0:pos]

def process_file_list(file_lst, sep_char='@'):
    result_lst = []

    for i in range(len(file_lst)):
        #remove extension
        filename = os.path.splitext(file_lst[i])[0]

        filename = extract_file_name_without_count(filename, sep_char)
        result_lst.append(filename)

    return result_lst

def classify_batch(vgg_ext, model, batch):
    n_sub_imgs = len(batch)

    batchSize =n_sub_imgs
    batch = torch.stack(batch)
    t,s,c,h,w=batch.shape
    batch = batch.view(t * s, c, h, w)

    img_tmp=batch

    if opt.gpu_id >= 0:
        img_tmp = img_tmp.cuda(opt.gpu_id)

    input_v = Variable(img_tmp, requires_grad = False)
    # print(input_v.shape)
    x = vgg_ext(input_v)
    # print(x.shape,"shape &&&&&&&&&&&&&")
    class_ = model(x)
    output_p = []
    for t in class_:
        if t[0]>t[1]:
            output_p.append(0)
        else:
            output_p.append(1)

    # output_pred = np.concatenate((output_pred, output_p))
    return output_p

transform_fwd = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])

def classify_frames(vgg_ext, model, path, label):
    file_lst = get_file_list(path, img_ext_lst)
    file_lst.sort()
    length = len(file_lst)
    frames =[]
    if (length == 0):
        return
    elif (length == 1):
        print('Error: Only one file!')
        return

    correct = 0
    count_vid = 0
    file_edited = process_file_list(file_lst)
    vid_name = file_edited[0]
    test_img = transform_fwd(Image.open(os.path.join(path, file_lst[0])))
    # print(type(test
    frames.append(test_img.unsqueeze(0))

    for i in range(1, length):
        if file_edited[i] == vid_name:
            test_img = transform_fwd(Image.open(os.path.join(path, file_lst[i])))
            frames.append(test_img.unsqueeze(0))
        else:
            # clasify the previous frames
            if(len(frames)==10):
                m=classify_batch(vgg_ext, model, frames)
                # print(m)
                o=0
                if(mean(m)>0.5):
                    o=1
                else:
                    o=0
                if o == label:
                    correct = correct + 1
                count_vid = count_vid + 1

            # get new items
            del frames
            frames =[]
            vid_name = file_edited[i]

            #print(vid_name)
            test_img = transform_fwd(Image.open(os.path.join(path, file_lst[i])))
            frames.append(test_img.unsqueeze(0))

    #classify the last frames
    # cls = classify_batch(vgg_ext, model, frames)
    # if cls == label:
    #     correct = correct + 1
    if(len(frames)==10):
        m=classify_batch(vgg_ext, model, frames)
        o=0
        if(mean(m)>0.5):
            o=1
        else:
            o=0
        if o == label:
            correct = correct + 1
        count_vid = count_vid + 1
        # print(path)
        # print('Number of files: %d' %(length))
        # print('Number of videos: %d' %(count_vid))
        # print('Number of correct classifications: %d' %(correct))

    return count_vid, correct

if __name__ == '__main__':
    path_real = os.path.join(opt.dataset, opt.real)
    path_fake = os.path.join(opt.dataset, opt.fake)

    # print(os.listdir(path_real))
    vgg_ext = model.VggExtractor()
    acc=[]
    for i in range(76,100):
        lstm = model.LSTM_Model(opt.gpu_id,opt.input_dim, opt.hidden_dim, opt.layer_dim, opt.output_dim)
        lstm.load_state_dict(torch.load(os.path.join(opt.outf,'lstm_' + str(i+1) + '.pt')))
        lstm.eval()

        if opt.gpu_id >= 0:
            vgg_ext.cuda(opt.gpu_id)
            lstm.cuda(opt.gpu_id)

        ###################################################################
        tol_count_vid = 0
        tol_correct = 0

        # real data
        count_vid, correct = classify_frames(vgg_ext, lstm, path_real, 1)
        tol_count_vid = tol_count_vid + count_vid
        tol_correct = tol_correct + correct

        # fake data
        count_vid, correct = classify_frames(vgg_ext, lstm, path_fake, 0)
        tol_count_vid = tol_count_vid + count_vid
        tol_correct = tol_correct + correct

    # print('##################################')
    # print('Number of videos: %d' %(tol_count_vid))
    # print('Number of correct classifications: %d' %(tol_correct))
    # print('Accuracy: %.2f' %(tol_correct/tol_count_vid*100))
        print('epoch : ',i,' , val acc %.2f' %(tol_correct/tol_count_vid*100))
        acc.append((tol_correct/tol_count_vid*100))
    accuracy={'validation Acc.' :acc }
    df = pd.DataFrame(accuracy)
    df.to_csv('val.csv')


# -*- coding:utf-8 -*-

import json
import socket, time

class Address():
    def __init__(self, ip, mask: int, port: int):
        self.ip = ip;
        self.mask = mask;
        self.port = port;

    def __eq__(self, another):
        return self.ip == another.ip and self.mask == another.mask and self.port == another.port;

class Packet():
    def __init__(self, src: Address = None, dest: Address = None, payload=None, packetType: int = None):
        self.src = src;
        self.dest = dest;
        self.payload = payload;
        self.packetType = packetType;  # 0表示普通数据包，1表示OSPF的链路状态信息数据包,2表示该数据包是一条发送数据包的指令

    # 序列化之后，就可以把序列化后的内容写入磁盘，或者通过网络传输到别的机器上
    def serialize(self):
        return json.dumps(self, default=lambda obj: obj.__dict__);

    def deserialize(self, serialization):
        d = json.loads(serialization);
        self.src = Address(d['src']['ip'], d['src']['mask'], d['src']['port']);
        self.dest = Address(d['dest']['ip'], d['dest']['mask'], d['dest']['port']);
        self.payload = d['payload'];
        self.packetType = int(d['packetType']);

class OSPF_ForwardingTableEntry():
    def __init__(self, dest: str = None, nextHop: str = None):
        self.dest = dest;
        self.nextHop = nextHop;

    def __str__(self):
        return "dest: " + str(self.dest) + ", next-hop: " + str(self.nextHop);

class RIP_RoutingTableEntry():
    def __init__(self, dest: str = None, nextHop: str = None, hopsToDest: int = 0):
        self.dest = dest;
        self.nextHop = nextHop;
        self.hopsToDest = hopsToDest;

    def __str__(self):
        return "dest: " + str(self.dest) + ", next-hop: " + str(self.nextHop) + ", hops-to-dest: " + str(
            self.hopsToDest);

def address_To_name(addr: Address):
    for nodeName in {'D', 'B', 'C', 'A', 'E'}:  # ...................
        if addr == name_To_address(nodeName):
            return nodeName;

def name_To_address(name):
    with open('../Configuration/Nodes/' + name + '.txt', 'r') as f:
        f.readline();

        ip = f.readline().strip();  # strip()把末尾的'\n'删掉
        mask = int(f.readline().strip());
        port = int(f.readline().strip());
        return Address(ip, mask, port);

def get_node_neighbors(name):
    neighbors = {};

    with open('../Configuration/Topo/Cost.txt', 'r') as f:
        line = '';
        for i in range(ord(name) - ord('A') + 1):
            line = f.readline().strip();

        count = 0;
        for n in line.split():
            if int(n) > 0:
                neighbors[chr(65 + count)] = (name_To_address(chr(65 + count)), int(n));
            count += 1;
    return neighbors;

class Node():
    '''
    name: str  # eg: 'A', 'B'
    address: Address
    neighbors: dict  # eg: {'D': (<__main__.Address object at 0x7f3c575d7a90>, 60), 'E': (<__main__.Address object at 0x7f3c575d7ac8>, 20), 'C': (<__main__.Address object at 0x7f3c575d7a58>, 80)}
    socket: socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    '''

    def __init__(self, name):
        self.name = name;
        self.address = name_To_address(name);
        self.neighbors = get_node_neighbors(name);

        self.receiveSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);  # 采用UDP
        self.receiveSocket.bind((self.address.ip, self.address.port));  # 用于接收数据，相当于UDP server
        self.sendSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);  # 用于发送数据

        self.OSPF_forwardingTable = [];
        self.RIP_routingTable = [];

    def printOutputMessageHeader(self):
        print(self.name + '(' + time.strftime("%H:%M:%S", time.localtime()) + ') >>> ', end='');

    def printOSPFForwardingTable(self):
        print('   OSPF Forwarding Table of', self.name, ':');
        for i in range(len(self.OSPF_forwardingTable)):
            print(i, ' ****** ', self.OSPF_forwardingTable[i]);

    def printRIPRoutingTable(self):
        print('            RIP Table of', self.name, ':');
        for i in range(len(self.RIP_routingTable)):
            print(i, ' ****** ', self.RIP_routingTable[i]);

    def send_a_normal_packet(self, dest: Address, payload, packetType):
        sendpkt = Packet(self.address, dest, payload, packetType);
        self.forward_a_normal_packet(sendpkt);

    def forward_a_normal_packet(self, recvPkt: Packet):
        packetDest = address_To_name(recvPkt.dest);
        packetSrc = address_To_name(recvPkt.src);

        if packetDest == self.name:
            self.printOutputMessageHeader();
            print('Receive a normal packet. [', 'FROM:', packetSrc, ', CONTENT:', recvPkt.payload, ']');
        # response
        else:
            # OSPF查找自己的forwarding table
            if self.OSPF_forwardingTable:
                for i in range(len(self.OSPF_forwardingTable)):

                    if self.OSPF_forwardingTable[i].dest == packetDest:
                        nextHopAddr = name_To_address(self.OSPF_forwardingTable[i].nextHop);
                        self.sendSocket.sendto(recvPkt.serialize().encode(), (nextHopAddr.ip, nextHopAddr.port));

                        self.printOutputMessageHeader();
                        if packetSrc == self.name:
                            print('Sending a normal packet. [ TO:', packetDest, ', CONTENT:', recvPkt.payload,
                                  ', NEXT-HOP:', self.OSPF_forwardingTable[i].nextHop, ']');
                        else:
                            print('Forwarding a normal packet. [', 'FROM:', packetSrc, ', TO:', packetDest,
                                  ', NEXT-HOP:', self.OSPF_forwardingTable[i].nextHop, ']');

            # RIP
            elif self.RIP_routingTable:
                for i in range(len(self.RIP_routingTable)):

                    if self.RIP_routingTable[i].dest == packetDest:
                        nextHopAddr = name_To_address(self.RIP_routingTable[i].nextHop);
                        self.sendSocket.sendto(recvPkt.serialize().encode(), (nextHopAddr.ip, nextHopAddr.port));

                        self.printOutputMessageHeader();
                        if packetSrc == self.name:
                            print('Sending a normal packet. [ TO:', packetDest, ', CONTENT:', recvPkt.payload,
                                  ', NEXT-HOP:', self.RIP_routingTable[i].nextHop, ']');
                        else:
                            print('Forwarding a normal packet. [', 'FROM:', packetSrc, ', TO:', packetDest,
                                  ', NEXT-HOP:', self.RIP_routingTable[i].nextHop, ']');