#coding:utf-8
"""
    author:cheng star
"""

import logging
import logging.handlers
import os

# get current module path
module_path = os.path.dirname(__file__)
print(module_path)
print(os.getcwd())
logger = None

def parse_log_init() :
    """parse log into config text file"""
    log_init = {}

    # file path use absolute path
    with open(module_path + "/conf/log_init.conf" , encoding = "utf-8") as f_obj :
        for line in f_obj.readlines() :
            if line.startswith("#") :
                continue
            else :
                [log_key , log_value] = [x.strip() for x in line.split("=")]
                print("log_key -> %s" %log_key)
                print("log_value -> %s" %log_value)

                # setting log level
                if log_key == 'log_level' :
                    if log_value.lower() == 'debug' :
                        log_value = logging.DEBUG
                    elif log_value.lower() == 'info' :
                        log_value = logging.INFO
                    elif log_value.lower() == 'warn' :
                        log_value = logging.WARN
                    elif log_value.lower() == 'error' :
                        log_value = logging.ERROR
                    elif log_value.lower() == 'fatal' :
                        log_value = logging.FATAL
                    else :
                        log_value = logging.DEBUG
                # parse log init config file save to dictionary
                log_init[log_key] = log_value
    return log_init

def logger_init() :
    """set logger info"""
    log_init = parse_log_init()

    # get logger obejct
    logger = logging.getLogger("logger")
    logger.setLevel(log_init["log_level"])

    # create fileHandler object wirte log to file
    # create consoleHandler object output log to console
    # fileHandler = logging.FileHandler(log_init["log_save_path"])
    fileHandler = logging.handlers.RotatingFileHandler(filename = module_path + "/log/log.txt" ,
                                                       maxBytes = 1024 * 1024 * int(log_init["log_size"]) ,
                                                       backupCount = int(log_init["log_duplicates"])
                                                       )
    consoleHandler = logging.StreamHandler()

    fileHandler.setLevel(log_init["log_level"])
    consoleHandler.setLevel(log_init["log_level"])

    log_format = log_init["log_format"]
    formatter = logging.Formatter(log_format)

    fileHandler.setFormatter(formatter)
    consoleHandler.setFormatter(formatter)

    logger.addHandler(fileHandler)
    logger.addHandler(consoleHandler)

    return logger

def get_logger() :
    """get logger object"""
    if logger is None :
        return logger_init()
    else :
        return logger

logger = get_logger()

if __name__ == '__main__' :
    # print(parse_log_init())
    # logger = get_logger()
    print(logger)
    logger.info("test")


from flask import Flask, current_app, render_template, request
import logging
import time

from sush_utils.sush_utils import system_uptime, DS18B20_temperature_get
from switcher3.shared_data import shared_data

HTML_NEWLINE = "
"

def start_simple_flask_not_returning(logger):
    app = Flask(__name__)

    @app.route("/index.html", methods=['GET', 'POST'])
    def index_html():
        return ROOT()

    @app.route('/')
    def ROOT():
        errorString = ""
        upTimeString = system_uptime.string_get()
        sharedData = current_app._get_current_object().config["_SHAREDDATA"]
        logger = current_app._get_current_object().config["_LOGGER"]
        #logger.debug("starting reading temperature")
        temperature = DS18B20_temperature_get(devicename = '/sys/bus/w1/devices/28-0317019e9eff/w1_slave')
        #logger.debug("Done reading temperature : " + temperature)

        if request.form.get('action') == "setAlarmTimes":
            startTime = request.form.get('start')
            stopTime = request.form.get('stop')
            active = str("true" == request.form.get('active')).lower()
            logger.debug("setAlarmTimes: "  + " " + (active) + " " + (startTime) + " " + (stopTime))
            try:
                sharedData.store( startTime, stopTime, active)
            except:
                errorString = "could not write data to cfg file "
                logger.error(errorString)

            #errorString = _GDATA.updateConfigFile(uid = UID,active = active, start = startTime, end = stopTime)
        else:
            logger.debug("no action")

        [start, end, active] = sharedData.load(returnType = "string")

        try:
            templateData = {
                'error_text' : errorString,
                'title' : 'Switcher!',
                'time': time.strftime("%Y-%m-%d %H:%M"),
                'uid0_active': active,
                'uid0_start' : start,
                'uid0_stop'  : end,
                'current_status_active' : "FIXME_curent_status",
                'temperature' : temperature,
                'upTime' : upTimeString
            }
            return render_template('main_jinja2_template.html', **templateData)
        except Exception as e:
            logger.error("could not render index.html: " + str(e))
            try:
                templateData = {
                        'tbd' : ""
                    }
                return render_template('error.html', **templateData)
            except:
                logger.error("could not render error.html")

        html = "Uptime: " + upTimeString  + HTML_NEWLINE + HTML_NEWLINE
        html = html + "Start : " + start + HTML_NEWLINE
        html = html + "End   : " + end + HTML_NEWLINE
        html = html + "Active: " + active + HTML_NEWLINE + HTML_NEWLINE
        html = html + ' Change Timer ' + HTML_NEWLINE
        return html

    @app.route("/timepicker.html", methods=['GET'])
    def timepicker():

        templateData = {
            'start' : request.args.get('start'),
            'stop' : request.args.get('stop')
        }
        return render_template('timepicker.html', **templateData)

    #use_reloader = False is to prevent this file to be started multiple times, resulting in multiple threads
    #                and duplicated data structurs

    sharedData = shared_data()

    # that is the ONLY ! Place where the flask app can access global data !
    app.config.update(_SHAREDDATA = sharedData)

    # that is the ONLY ! Place where the flask app can access global data !
    app.config.update(_SHAREDDATA = sharedData)
    app.config.update(_LOGGER = logger)

    app.run(host='0.0.0.0', port=5000, debug=False,use_reloader=False)


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

"""
@version: 0.1
@author: Yang Reid
@license: Apache Licence
@contact: yangtao584@126.com
@site: http://www.phpgao.com
@software: PyCharm Community Edition
@file: new_stock.py
@time: 2018/4/12 18:56
"""
import tushare as ts
import time
import pandas as pd

def func():
    new_stock_df = ts.new_stocks()
    return new_stock_df[['code','name','ipo_date','issue_date']]

if __name__ == '__main__':
    stock_df = func()
    stock_df = stock_df[stock_df['issue_date'] > '2017-12-01']
    all_stocks = stock_df['code'].tolist()
    po_ban_stocks = []
    for stock in all_stocks:
        df = ts.get_hist_data(stock)
        try:
            df = df[['open', 'close', 'p_change']]
            start_date = df[df['p_change'] < 9.8].tail(1).index[0]
            po_ban_price = df.loc[start_date,'close']
            stock_dict = {'code':stock,
                          'date':start_date,
                        'break_price':po_ban_price}
            po_ban_stocks.append(stock_dict)
            # print('Stock:{} opened on Date:{} at {}'.format(stock,start_date,po_ban_price))
        except (IndexError,TypeError):
            pass
    pd.DataFrame(po_ban_stocks).to_excel('po_ban' + time.strftime("_%Y-%m-%d_%H_%M_%S", time.localtime()) + '.xlsx',
                                           index=False)


#!/usr/bin/env python
"""
Script Header

$Id: cmCC24845_3pcc_BS_Functional_134_NetworkThreeWayAddonPartyHangsUp

Copyright (c) 2016-2017 Cisco Systems, Inc.

Name:
    cmCC24845_3pcc_BS_Functional_134_NetworkThreeWayAddonPartyHangsUp.py

Purpose:
    This test case verifies a network three-way conference in which
    the add-on party hangs up first.

Author:
    Yashashwini M B(ymuddena@cisco.com)

References:
    US24845

Description:
    Originate a call from DUT 2 to DUT 1.Answer the call. From DUT 1, make
    a call to DUT 3 by selecting the conference button on the phone or
    invoking conference on DUT 1.After DUT 3 answers, complete the conference
    to join the parties.DUT 3 hangs up.DUT 1 hangs up.

Topology:
    3 3pcc phones

Pass/Fail Criteria:
    DUT 3 phone display shows DUT 1 caller ID

Test Steps:
    1. Originate a call from DUT 2 to DUT 1
    2. DUT 1 Answer the call.
    3. From DUT 1, make a call to DUT 3 by selecting the conference button
       on the phone or invoking conference on DUT 1.
    4. After DUT 3 answers, complete the conference to join the parties.
    5. DUT 3 hangs up.
    6. DUT 1 hangs up.
    Verify:
    1. DUT 2 dials DUT 1.
    2. DUT 1 answers the call.
        1. Two-way voice path is established.
    3. DUT 1 initiates a conference with DUT 3 by selecting the
       conference button and dialing DUT 3.
        1. DUT 2 is still in conference.
        2. DUT 3s phone display shows DUT 1 caller ID.
    4. DUT 3 answers the call.
        1. Two-way voice path is established between DUT 1 and DUT 3
    5. DUT 1 completes the conference to join the parties.
        1. All three parties are joined in a conference call
    6. DUT 3 hangs up.
        1. DUT 3 is released
        2. DUT 1 and DUT 2 are still connected
    7. DUT 1 hangs up.
        1. The call ends for all parties.

 Notes:

 Known Bugs:
"""

import tng
import logging
from tng_sl.device.endpoint.synergylite.synergylite_3pcc_extended\
    import wait_for_ccapi_call_states
from tng_sl.contrib.mpp.phone_line_reg_helper import PhoneLineRegHelper
from tng_sl.contrib.mpp.phone_line_reg_helper import PhoneConfigHelper
from tng_sl.contrib.mpp.tshark_helper import TsharkHelper
from tng_sl.contrib.mpp.broadsoft.broadsoft_config import BroadsoftConfig
from tng_sl.contrib.setup_helper import SetupHelpersTestCase

log = logging.getLogger('NetworkThreeWayAddonPartyHangsUp')

class NetworkThreeWayAddonPartyHangsUp(SetupHelpersTestCase, tng.api.TestCase):
    helpers = (
        PhoneConfigHelper, TsharkHelper, PhoneLineRegHelper)
    helper_num_devices = 3

    def setUp(self):
        log.info("Start of setUp")

        self.conference_url = self.toolkit.get_test_env_info(
            section='phone',
            parameter_name="conference_url")
        self.xsi_user_id1 = self.toolkit.get_test_env_info(
            section='bsoft', parameter_name="xsi_user_id1")
        self.broadsoft = BroadsoftConfig()
        self.p1_fname, self.p1_lname = self.broadsoft.get_first_and_last_name(
            user_id_proxy=self.xsi_user_id1, user_id=self.user_id1)
        log.info("End of setUp")

    def test_network_three_way_addon_party_hangs_up(self):
        log.info("Start of test_network_three_way_addon_party_hangs_up")
        log.info('Set and verify Conference Bridge URL on DUT')
        # Set Conference Bridge URL in Call Feature settings
        self.oPhone1.ui.set_web_parameter_http(
            Call_Feature_Settings=[
                'Ext 1', 'Conference Bridge URL',
                str(
                    self.conference_url)])

        # verify Conference Bridge URL in Call Feature settings
        call_feature = self.oPhone1.ui.get_web_parameter_http(
            'Ext 1', 'Conference Bridge URL')
        self.assertEqual(call_feature, str(self.conference_url))

        # Set Refer-To Target Contact in SIP settings
        self.oPhone1.ui.set_web_parameter_http(
            SIP_Settings=['Ext 1', 'Refer-To Target Contact', 1])

        # verify Refer-To Target Contact is set in SIP Settings
        refer_to_target = self.oPhone1.ui.get_web_parameter_http(
            'Ext 1', 'Refer-To Target Contact')
        self.assertEqual(refer_to_target[0], '1')
        log.info('Start tshark on linux')
        filter_cmd = (
            'port sip and (host {} or host {} or host {})'.format(
                self.oPhone1.ip, self.oPhone2.ip, self.oPhone3.ip))
        self.tshark.tshark_start(filter_cmd)
        self.oPhone3.register_call_event('VOIP_MSG_CALL_EVENT_INCOMING')
        log.info("Phone2 dial Phone1: {}".format(self.user_id1))
        self.oPhone2.ccapi.dial('null', self.user_id1, '', 1, 0, 1)
        # check phone2 ringout status and Phone1 ringing status
        wait_for_ccapi_call_states(
            (self.oPhone1, self.oPhone2), ("RINGING", "PROCEEDING"),
            timeout=30)

        log.info("Phone1 receive the call")
        self.oPhone1.ccapi.accept("0000")
        # check two phones are in connected status
        wait_for_ccapi_call_states(
            (self.oPhone1, self.oPhone2), ("CONNECTED", "CONNECTED"))

        # Conference call from Phone1
        log.info("Phone1 press conference")
        self.oPhone1.ccapi.conference("0000")

        ph1_identity_string = '{} {}'.format(self.p1_fname, self.p1_lname)
        log.info("Phone1 dial Phone3's  number: {}".format(self.user_id3))
        self.oPhone3.ccapi.feedback_subscribe(self.oPhone3.subscribed_callback)
        self.oPhone1.dial_digits('0001', self.user_id3)
        wait_for_ccapi_call_states(
            (self.oPhone1, self.oPhone1, self.oPhone3),
            ("PROCEEDING", "HOLD", "RINGING"), (1, 1, 1), (1, 0, 0),
            timeout=30)
        self.oPhone3.check_caller_id(ph1_identity_string, self.user_id1[-4:])
        self.oPhone3.ccapi.feedback_unsubscribe(
            self.oPhone3.subscribed_callback)

        log.info("Phone3 receive the call")
        self.oPhone3.ccapi.accept("0000")
        wait_for_ccapi_call_states(
            (self.oPhone1, self.oPhone1, self.oPhone3),
            ("CONNECTED", "HOLD", "CONNECTED"), (1, 1, 1), (1, 0, 0),
            timeout=30)

        log.info("Phone1 press conference and all three are connected")
        self.oPhone1.ccapi.conference("0001")
        wait_for_ccapi_call_states(
            self.devices, ("CONNECTED", "CONNECTED", "CONNECTED"),
            timeout=30)

        log.info(
            "Phone3 hangup call.Phone1 and Phone2 are in connected state")
        self.oPhone3.ccapi.hangUp('0000')
        # check phone1 and phone2 status are connected and phone3 status is
        # Sidle
        wait_for_ccapi_call_states(
            self.devices, ("CONNECTED", "CONNECTED", "IDLE"))

        log.info("Phone1 Disconnect call")
        self.oPhone1.ccapi.hangUp('0000')
        # check phone1 and phone2's and phone3's status are idle
        wait_for_ccapi_call_states(
            self.devices, ("IDLE", "IDLE", "IDLE"))
        self.oPhone3.unregister_call_event('VOIP_MSG_CALL_EVENT_INCOMING')
        log.info("End of test_network_three_way_addon_party_hangs_up")

# this is called by 'tng run'
def main():
    tng.api.runner()

if __name__ == '__main__':
    tng.run(main)


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 17 12:21:02 2016

@author: atschmidt
"""

from nipype.pipeline.engine import Node, Workflow
import nipype.interfaces.utility as util
import nipype.interfaces.io as nio
import nipype.interfaces.fsl as fsl
import nipype.algorithms.rapidart as ra
from functions import motion_regressors, selectindex, nilearn_denoise, fix_hdr

# read in subjects and file names

subjects = [['P46','XXXX']]
sessions=['d0']

# directories
working_dir = '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/working_dir'
out_dir = '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/final/'

# main workflow
preproc_nuisance_regress = Workflow(name='func_preproc_nuisance_regress')
preproc_nuisance_regress.base_dir = working_dir
preproc_nuisance_regress.config['execution']['crashdump_dir'] = preproc_nuisance_regress.base_dir + "/crash_files"

# iterate over subjects
subject_infosource = Node(util.IdentityInterface(fields=['subjectlist']),
                  name='subject_infosource')
subject_infosource.iterables=[('subjectlist', subjects)]

# iterate over sessions
session_infosource = Node(util.IdentityInterface(fields=['session']),
                  name='session_infosource')
session_infosource.iterables=[('session', sessions)]

# select files

templates={'unwarped_file' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/working_dir/func_preproc/_session_{session}/_subjectlist_{proband}.{subject}/fugue/corr_{subject}_{session}_S3_RS_ep2d_SMS6_TR1p1_TE22_FA40_1p2_FatSat_Echo_0_Te22_roi_unwarped.nii.gz',
           #'unwarped_file' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/fugue/corr_{subject}_{session}_S3_RS_ep2d_SMS6_TR1p1_TE22_FA40_1p2_FatSat_Echo_0_Te22_roi_unwarped.nii.gz',
           'slicemoco_par_file' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/working_dir/func_preproc/_session_{session}/_subjectlist_{proband}.{subject}/spacetime_realign/{subject}_{session}_S3_RS_ep2d_SMS6_TR1p1_TE22_FA40_1p2_FatSat_Echo_0_Te22_roi.nii.gz.par',
           # need brain mask, csf and wm mask in functional space - from cbstools coreg pipeline
#           'gm_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/mappings/{subject}_{session}_S7_MP2RAGE_0p7_INV2_Te2_transform_clone_stripmask_clone_transform_def.nii.gz',
#           'gm_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/masks/{subject}_{session}_brainmask_rsfmri.nii.gz',
           # use gm mask instead of gm
           'gm_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/masks/{subject}_{session}_GM_mask_rsfmri.nii.gz',
#           'csf_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/mappings/{subject}_{session}_S5_MP2RAGE_0p7_T1_Images_Te2_clone_transform_strip_clone_reg_bound_seg_binmask_CSF_def.nii.gz',
#           'wm_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/mappings/{subject}_{session}_S5_MP2RAGE_0p7_T1_Images_Te2_clone_transform_strip_clone_reg*_bound_seg_binmask_WM_def.nii.gz'}
           'csf_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/masks/{subject}_{session}_CSF_mask_rsfmri.nii.gz',
           'wm_mask' : '/nobackup/eminem2/schmidt/MMPIRS/preprocessing/{subject}/masks/{subject}_{session}_WM_mask_corrected_rsfmri.nii.gz'}

selectfiles = Node(nio.SelectFiles(templates),
                   name="selectfiles")

preproc_nuisance_regress.connect([(subject_infosource, selectfiles, [(('subjectlist', selectindex, 0), 'subject'),
                                                    (('subjectlist', selectindex, 1), 'proband')]),
                 (session_infosource, selectfiles, [('session', 'session')])
                 ])

# set fsl output type to nii.gz
fsl.FSLCommand.set_default_output_type('NIFTI_GZ')

# fix header of brain mask
fix_header_brainmask = Node(util.Function(input_names=['data_file', 'header_file'],
                            output_names=['out_file'],
                            function=fix_hdr),
                  name='fix_header_brainmask')
preproc_nuisance_regress.connect([(selectfiles, fix_header_brainmask, [('gm_mask', 'data_file'),
                                        ('unwarped_file', 'header_file')]),
])

# fix header of csf mask
fix_header_csfmask = Node(util.Function(input_names=['data_file', 'header_file'],
                            output_names=['out_file'],
                            function=fix_hdr),
                  name='fix_header_csfmask')
preproc_nuisance_regress.connect([(selectfiles, fix_header_csfmask, [('csf_mask', 'data_file'),
                                        ('unwarped_file', 'header_file')]),
])

# fix header of csf mask
fix_header_wmmask = Node(util.Function(input_names=['data_file', 'header_file'],
                            output_names=['out_file'],
                            function=fix_hdr),
                  name='fix_header_wmmask')
preproc_nuisance_regress.connect([(selectfiles, fix_header_wmmask, [('wm_mask', 'data_file'),
                                        ('unwarped_file', 'header_file')]),
])

# merge images into list
masklist = Node(util.Merge(2),name='masklist')
preproc_nuisance_regress.connect([(fix_header_csfmask, masklist, [('out_file', 'in1')]),
                 (fix_header_wmmask, masklist, [('out_file', 'in2')]),
                 ])

# perform artefact detection
artefact=Node(ra.ArtifactDetect(save_plot=True,
                                use_norm=True,
                                parameter_source='NiPy',
                                mask_type='file',
                                norm_threshold=1,
                                zintensity_threshold=3,
                                use_differences=[True,False]
                                ),
             name='artefact')

preproc_nuisance_regress.connect([(selectfiles, artefact, [('unwarped_file', 'realigned_files'),
                                        ('slicemoco_par_file', 'realignment_parameters')]),
                 (selectfiles, artefact, [('gm_mask', 'mask_file')]),
                 ])

# calculate motion regressors

motreg = Node(util.Function(input_names=['motion_params', 'order','derivatives'],
                            output_names=['out_files'],
                            function=motion_regressors),
                 name='motion_regressors')
motreg.inputs.order=1
motreg.inputs.derivatives=1
preproc_nuisance_regress.connect([(selectfiles, motreg, [('slicemoco_par_file','motion_params')])])

def makebase(subject, out_dir):
    return out_dir + subject[1]

denoise = Node(util.Function(input_names=['in_file',
                                          'gm_mask',
                                          'wm_mask',
                                          'csf_mask',
                                          'motreg_file',
                                          'outlier_file',
                                          'bandpass',
                                          'tr'],
                             output_names=['denoised_file',
                                           'confounds_file'],
                             function=nilearn_denoise),
               name='denoise')

denoise.inputs.tr = 1.13
denoise.inputs.bandpass = [0.1, 0.01]

preproc_nuisance_regress.connect([(selectfiles, denoise, [('unwarped_file', 'in_file')]),
                                  (fix_header_brainmask, denoise, [('out_file', 'gm_mask')]),
                                  (fix_header_wmmask, denoise, [('out_file', 'wm_mask')]),
                                  (fix_header_csfmask, denoise, [('out_file', 'csf_mask')]),
                                 #(motreg, denoise, [('out_files', 'motreg_file')]),
                                 (motreg, denoise, [(('out_files',selectindex,[0]), 'motreg_file')]),
                                 (artefact, denoise, [('outlier_files', 'outlier_file')])])

# sink relevant files
sink = Node(nio.DataSink(parameterization=False),
             name='sink')

preproc_nuisance_regress.connect([(session_infosource, sink, [('session', 'container')]),
                 (subject_infosource, sink, [(('subjectlist', makebase, out_dir), 'base_directory')]),
                (fix_header_brainmask, sink, [('out_file', 'confounds@new_brainmask_header')]),
                (fix_header_csfmask, sink, [('out_file', 'confounds@new_csfmask_header')]),
                (fix_header_wmmask, sink, [('out_file', 'confounds@new_wmmask_header')]),
                (artefact, sink, [('norm_files', 'confounds.@norm_motion'),
                                  ('outlier_files', 'confounds.@outlier_files'),
                                  ('intensity_files', 'confounds.@intensity_files'),
                                  ('statistic_files', 'confounds.@outlier_stats'),
                                  ('plot_files', 'confounds.@outlier_plots')]),
                 (motreg, sink, [('out_files', 'confounds.@motreg')]),
                 (denoise, sink, [('denoised_file', 'final.@final'),
                                  ('confounds_file', 'confounds.@all')])])

#preproc_nuisance_regress.run(plugin='MultiProc', plugin_args={'n_procs' : 9})
preproc_nuisance_regress.run()

#preproc_nuisance_regress.write_graph(dotfilename='func_preproc_nuisance_regress.dot', graph2use='colored', format='pdf', simple_form=True)


from asgiref.sync import async_to_sync
from channels.generic.websocket import JsonWebsocketConsumer
import json

from core.models import ChatUser, Group

# data['type'] = user or group
# data['body'] = {
#   type,
#   args
# }
#
#
#

# out going data
# {
#     'command': # false | true (add group / remove group / change name)
#     'message': # message - carries additional args
#     'username': # username
#     'group_id': # group_id
# }

class ChatAppConsumer(JsonWebsocketConsumer):

    # received incoming WebSocket connection
    def connect(self):

        self.user = self.scope['user']

        self.chatuser = ChatUser.get_chat_user(self.user)

        for group in self.chatuser.groups.all():
            self.listen_to_group(group.id)

        self.accept()

    # disconnects from WebSocket connection
    def disconnect(self, close_code):
        for group in self.user.groups.all():
            self.unlisten_to_group(group.id)

    # Subscribes this ChatAppConsumer instance to the channel
    def listen_to_group(self, group_id):
        print(str(group_id) + '\n\n\n\n')
        async_to_sync(self.channel_layer.group_add)(
            str(group_id),
            self.channel_name
        )

    # Unsubscribes this ChatAppConsumer instance to the channel
    def unlisten_to_group(self, group_id):
        async_to_sync(self.channel_layer.group_discard)(
            str(group_id),
            self.channel_name
        )

    # Sends the payload to the channel specified by the given group_id
    def group_send(self, group_id, payload):
        print('\n\nGROUP SEND TO ' + str(group_id))
        async_to_sync(self.channel_layer.group_send)(
            str(group_id),
            payload
        )
        # async_to_sync(self.channel_layer.group_send)(
        #     str(group_id),
        #     json.dumps(payload)
        # )

    # Receives
    def receive_json(self, data):

        e = data['type']

        if e == 'USER': # events outside the scope chat group, such as opening a group -- only affect user
            self.receive_user_event(data['body'])
        elif e == 'CHAT': # events within a chat group
            print(data['body'])
            self.receive_chat_event(data['body'])
        elif e == 'INIT':
            self.receive_user_event(data)
        else:
            print(data)
            raise 'Invalid message type!'

    # receives and processes user events
    def receive_user_event(self, body):

        # chat_user = ChatUser.get_chat_user(self.user)

        # initializes user's groups
        if body['type'] == 'INIT':
            # print('\n\nINIT222\n\n')
            groups_init = {
                'command_type': 'INIT',
                'groups': {},
            }
            # print('\n\nINIT---55\n\n')
            for group in self.chatuser.groups.all():
                groups_init['groups'][group.id] = {
                    'group_id': group.id,
                    'group_name': group.group_name,
                    'messages': [{'message': message.content, 'username': message.username} for message in group.message_set.all()],
                    'read':  self.chatuser.has_read(group.id),
                }

            # print('\n\nINIT----6\n\n')
            self.sendObj(groups_init)

        # creates group
        elif body['type'] == 'CREATE':
            # print('\n\n\nHUH?')
            default_group_name = 'New Group'

            group = self.chatuser.create_group(default_group_name)

            self.sendObj(ChatAppConsumer.out('GROUP_CREATE', group.id, default_group_name))
            self.sendObj(ChatAppConsumer.out('MESSAGE', group.id, 'Group created'))

        # removes user from group and deletes group if no other users are in it
        elif body['type'] == 'DELETE':
            self.chatuser.delete_group(body['group_id'])
            self.chatuser.save()
            self.sendObj(ChatAppConsumer.out('GROUP_DELETE', body['group_id']))
        elif body['type'] == 'READ':
            notifications = self.chatuser.get_notifications_obj(body['group_id'])
            notifications.read = True
            notifications.save()

    # processes top-level chat events
    def receive_chat_event(self, body):

        # standard message
        if body['type'] == 'MESSAGE':

            group = Group.objects.get(id=body['group_id'])
            username = body['username']
            message = body['message']
            group.send_message(message, username)
            self.group_send(group.id, ChatAppConsumer.out('MESSAGE', group.id, message, username))

        # special chat command
        elif body['type'] == 'COMMAND':
            self.receive_chat_special_command(body)

    # processes special chat commands
    def receive_chat_special_command(self, command):

        # if invalid command, only send back to user
        group = Group.objects.get(id=command['group_id'])

        print(group)

        # adds user to group
        if command['command'] == 'ADD':
            message = 'Added ' + command['username'] + ' to the group.'

            group.add_user(command['username'])
            group.send_message(message)

            self.group_send(group.id, ChatAppConsumer.out('MESSAGE', group.id, message))

        # changes group name
        elif command['command'] == 'CHANGE_NAME':

            new_name = command['username']
            message = 'Group name has been changed'

            group.change_name(new_name)
            group.send_message(message)

            self.group_send(group.id, ChatAppConsumer.out('GROUP_NAME_CHANGE', group.id, new_name))
            self.group_send(group.id, ChatAppConsumer.out('MESSAGE', group.id, message))

        # lists users in group
        elif command['command'] == 'LIST': # individual group command
            self.sendObj(ChatAppConsumer.out('MESSAGE', group.id, group.get_users()))
        # invalid command
        else:
            self.sendObj(ChatAppConsumer.out('MESSAGE', group.id, 'Invalid Command'))

    def sendObj(self, obj):
        self.send(text_data=json.dumps(obj))

    def chat_message(self, obj):
        self.sendObj(obj)

    @staticmethod
    def out(command_type, group_id, message='', username='', type=''):
        return {
            'command_type': command_type,
            'group_id': str(group_id),
            'message': message,
            'username': username,
            'type': 'chat_message',
        }


import random

from environs import Env
import pytimeparse

import ptbot

def format_progress_message(seconds_total, seconds_left):
    return render_progressbar(
        total = seconds_total,
        iteration = seconds_total - seconds_left,
        prefix = f"Осталось {seconds_left} секунд\n"
    )

def render_progressbar(total, iteration, prefix='', suffix='', length=30, fill='█', zfill='░'):
    iteration = min(total, iteration)
    percent = "{0:.1f}"
    percent = percent.format(100 * (iteration / float(total)))
    filled_length = int(length * iteration // total)
    pbar = fill * filled_length + zfill * (length - filled_length)
    return '{0} |{1}| \n{2}% {3}'.format(prefix, pbar, percent, suffix)

def notify_progress(seconds_left, seconds_total, message_id):
    bot.update_message(
        tg_chat_id, message_id, format_progress_message(seconds_total, seconds_left)
    )

def notify_time_is_over():
    bot.send_message(tg_chat_id, "Время вышло!")

def reply(time):
    parsed_time = pytimeparse.parse(time)
    if parsed_time is None:
        error_message = 'Я так не понимаю.\nНапишите что-то вроде "5s" или "1.2 minutes"'
        bot.send_message(tg_chat_id, error_message)
        return
    message_id = bot.send_message(
        tg_chat_id, format_progress_message(parsed_time, parsed_time)
    )
    bot.create_countdown(parsed_time, notify_progress, parsed_time, message_id)
    bot.create_timer(parsed_time, notify_time_is_over)

if __name__ == "__main__":
    env = Env()
    env.read_env()

    tg_token = env.str("tg_token")
    tg_chat_id = env.int("tg_chat_id")

    bot = ptbot.Bot(tg_token)
    bot.send_message(tg_chat_id, "На какое время поставить таймер?")
    bot.reply_on_message(reply)
    bot.run_bot()


import matplotlib.pyplot as plt
import numpy as np

from variational_mlp import SimpleVariationalMLP

"""
n_examples = 2500
n_in = 100
n_hidden = 250
n_out = 10

features = np.random.randn(n_examples, n_in).astype('float32')
beta = np.random.uniform(size = (n_in, n_out)).astype('float32')
targets = np.dot(features * np.sqrt(np.abs(features)), beta)
targets += np.random.randn(n_examples, n_out)

train_idx = np.arange(n_examples) < (0.7*n_examples)
data = {
        'X_train': features[train_idx],
        'Y_train': targets[train_idx],
        'X_val': features[~train_idx],
        'Y_val': targets[~train_idx]
        }

mlp_args = {
        'n_in': n_in,
        'n_hidden': n_hidden,
        'n_out': n_out,
        'activations': {'hidden': 'relu', 'output': 'linear'},
        'likelihood': 'standard_gaussian',
        'learning_algorithm': 'adadelta',
        'hyperparams': {
            'prior_precision': 1e-5,
            'learning_rate': 0.001,
            'annealing_rate': 0.99,
            'momentum_coeff': 0.99,
            'decay_rate': 0.99,
            'damping_factor': 1e-6
            }
        }
mlp = SimpleVariationalMLP(**mlp_args)
mlp.train(
        data = data,
        n_epochs = 1000,
        minibatch_size = 128,
        tracking_frequency = 25,
        n_sim = 5
        )
"""

n_examples = 5000
n_in = 1
n_hidden = 150
n_out = 1

x = 0.5 * np.random.random(n_examples)
noise = np.random.normal(0.0, 0.02, n_examples)
y = (x + 0.3 * np.sin(2.0 * np.pi * (x + noise)) +
            0.3 * np.sin(4.0 * np.pi * (x + noise)) + noise)

train_idx = np.arange(n_examples) < (0.70 * n_examples)
data = {
        'X_train': x[train_idx],
        'Y_train': y[train_idx],
        'X_val': x[~train_idx],
        'Y_val': y[~train_idx]
        }

mlp_args = {
        'n_in': n_in,
        'n_hidden': n_hidden,
        'n_out': n_out,
        'activations': {'hidden': 'relu', 'output': 'linear'},
        'likelihood': 'gaussian_0_0.02',
        'learning_algorithm': 'adadelta',
        'hyperparams': {
            'prior_precision': 1e-2,
            'learning_rate': 0.001,
            'annealing_rate': 0.99,
            'momentum_coeff': 0.99,
            'decay_rate': 0.9,
            'damping_factor': 1e-6
            }
        }
mlp = SimpleVariationalMLP(**mlp_args)
mlp.train(
        data = data,
        n_epochs = 1000,
        minibatch_size = 256,
        tracking_frequency = 25,
        n_sim = 5
        )

grid_x = np.linspace(-0.05, 0.55, 125)
for i in xrange(100):
    plt.scatter(grid_x, mlp.predict(grid_x, 1), c = 'green')
plt.scatter(x, y)
plt.scatter(x, mlp.predict(x, 25), c = 'red')
plt.show()


from flask_classy import FlaskView, route
from flask import render_template, redirect, session, request, flash
from utils.twitter_oauth import TwitterOauth
from flask_login import login_user
from models import User

class OauthView(FlaskView):
    route_base = '/oauth/'

    # /oauth/twitter
    def twitter(self):
        twitter = TwitterOauth()
        token, secret, url = twitter.request_parmas()

        # callback先で使うのでsessionに保存しておく
        session['request_token'] = token
        session['request_token_secret'] = secret

        return redirect(url)

    # /oauth/twitter/callback
    @route('/twitter/callback')
    def twitter_callback(self):
        twitter = TwitterOauth()
        user_infomation = twitter.fetch_access_token(
            request.args.get('oauth_token'),
            session.get('request_token_secret'),
            request.args.get('oauth_verifier')
        )

        # セッションから削除
        session.pop('request_token_secret', None)
        session.pop('request_token', None)

        user = User.query.filter_by(twitter_user_id=user_infomation['twitter_user_id']).first()

        if user:
            # 既存ユーザー
            flash("ログインしました")
        else:
            # 新規ユーザーを登録
            user = User(
                screen_name=user_infomation['screen_name'],
                twitter_user_id=user_infomation['twitter_user_id'],
                access_token=user_infomation['access_token'],
                access_token_secret=user_infomation['access_token_secret']
            )
            user.add().commit()
            flash("登録しました")

        # login情報の保存
        login_user(user, True)

        return redirect('/')


from gui.meus_grids import MeuGridColTitles, MeuFormMutt
from entidades import Lancamento, Conta
from repositorios import ContaRepositorio
from funcoes import saldo
from auxiliares import formata_numero, formata, vai_para_inicio_da_lista
import curses, decimal

class SaldoContaList(MeuGridColTitles):
    def __init__(self, *args, **keywords):
        super(SaldoContaList, self).__init__(*args, **keywords)

    def registra_menus(self):
        super(SaldoContaList, self).registra_menus()
        self.adiciona_comando('resultado', self.filtrar_contas_resultado, self.parent.GRUPO == 'PATRIMONIO')
        self.adiciona_comando('patrimonio', self.filtrar_contas_patrimonio, self.parent.GRUPO == 'RESULTADO')
        self.adiciona_comando('todas', self.todas_contas, self.parent.GRUPO is not None)
        self.adiciona_comando('conciliados', self.filtrar_lancamentos_conciliados)
        self.adiciona_comando('listar', self.lancamentos_da_conta)

        self.adiciona_menu(curses.KEY_LEFT, self.ir_contas, 'Contas', principal=True)
        self.adiciona_menu(curses.KEY_RIGHT, self.ir_lancamentos, 'Lançamentos', principal=True)

    def lancamentos_da_conta(self, *args, **keywords):
        self.parent.parentApp.getForm('LANCAMENTOS').conta_filtro = self.get_selecionado()
        self.parent.parentApp.getForm('LANCAMENTOS').atualiza_lista()
        self.parent.parentApp.switchForm('LANCAMENTOS')
        vai_para_inicio_da_lista()

    def todas_contas(self, *args, **keywords):
        self.parent.set_grupo(None)
        self.parent.atualiza_lista()

    def filtrar_contas_resultado(self, *args, **keywords):
        self.parent.set_grupo('RESULTADO')
        self.parent.atualiza_lista()

    def filtrar_contas_patrimonio(self, *args, **keywords):
        self.parent.set_grupo('PATRIMONIO')
        self.parent.atualiza_lista()

    def filtrar_lancamentos_conciliados(self, *args, **keywords):
        if self.parent.SOMENTE_CONCILIADOS:
            self.parent.set_somente_conciliados(False)
        else:
            self.parent.set_somente_conciliados(True)
        self.parent.atualiza_lista()

class SaldoContaListDisplay(MeuFormMutt):
    MAIN_WIDGET_CLASS = SaldoContaList
    GRUPO = 'PATRIMONIO'
    SOMENTE_CONCILIADOS = True

    def __init__(self, *args, **keywords):
        super(SaldoContaListDisplay, self).__init__(*args, **keywords)
        self.wStatus1.value = "Contas e seus Saldos"

    def beforeEditing(self):
        self.atualiza_lista()

    def set_somente_conciliados(self, arg):
        self.SOMENTE_CONCILIADOS = arg

    def set_grupo(self, grupo):
        self.GRUPO = grupo
        self.new_menu()

    def atualiza_legenda_filtro(self):
        self.wStatus2.value = 'Filtros ativos:  '
        self.wStatus2.value += '☑ Conciliados' if self.SOMENTE_CONCILIADOS else '☐ Conciliados'
        self.wStatus2.value += '  🞊 Resultado' if self.GRUPO == 'RESULTADO' else '  🞅 Resultado'
        self.wStatus2.value += '  🞊 Patrimônio' if self.GRUPO == 'PATRIMONIO' else '  🞅 Patrimônio'

    def get_tupla_conciliados(self):
        if self.SOMENTE_CONCILIADOS:
            return (1,)
        return (0, 1,)

    def atualiza_lista(self):
        self.atualiza_legenda_filtro()
        # filtrar os lançamento por grupo de tipo de conta
        self.wMain.col_titles = ['Conta', 'Débito', 'Crédito', 'Saldo']

        try:
            contas = ContaRepositorio().listar_com_saldo(self.get_tupla_conciliados(), self.GRUPO)

            self.wMain.values = []
            for conta in contas:
                saldo_conta = decimal.Decimal(saldo(conta))
                debitos = decimal.Decimal(conta.debitos)
                creditos = decimal.Decimal(conta.creditos)
                nome = "({0}) {1}".format(conta.tipo, conta.nome)
                self.wMain.values.append([formata(nome), formata_numero(debitos), formata_numero(creditos),
                                          formata_numero(saldo_conta), conta])

            self.wMain.display()
            self.display()
        except (Conta.DoesNotExist, Lancamento.DoesNotExist):
            pass


import json
from unittest import mock

from django.test import TestCase
from django.test import Client

from ticketer.recordlocator import views
from .models import Ticket

def mock_generator(length=1):
    """ A mock record locator generator that returns the same record locator
    each time. """

    return 'XXXXXXXX'

class TicketCreationTests(TestCase):
    def test_create_new_ticket(self):
        """ Test the creation of a new ticket. """
        views.create_new_ticket(20852, 'XXYZZYCD')
        ticket = Ticket.objects.filter(record_locator='XXYZZYCD')[0]
        self.assertEqual(ticket.zip_code, '20852')

    def test_generate_backup_locator(self):
        locator = views.generate_backup_locator()
        self.assertTrue('-' in locator)
        self.assertEqual(16, len(locator))

    def test_create_tickets(self):
        tickets, failures = views.create_tickets(20852, 5)
        self.assertEqual(5, len(tickets) + len(failures))

        # Failures should be extremely rare
        self.assertTrue(len(tickets) > 0)

        locators = [t.record_locator for t in tickets]

        # Locators are unique with respect to each other.
        self.assertEqual(len(locators), len(set(locators)))

        for l in locators:
            ticket = Ticket.objects.get(record_locator=l)
            self.assertNotEqual(None, ticket)
            self.assertEqual(ticket.zip_code, '20852')

    @mock.patch(
        'ticketer.recordlocator.views.generator.safe_generate', mock_generator)
    def test_create_unique_ticket(self):
        ticket_one = views.create_unique_ticket(16801)
        self.assertEqual(ticket_one.record_locator, 'XXXXXXXX')

        ticket_two = views.create_unique_ticket(16801)
        self.assertTrue('-' in ticket_two.record_locator)
        self.assertTrue('XXXXXXXX' in ticket_two.record_locator)

        ticket_three = views.create_unique_ticket(20002)
        self.assertTrue('-' in ticket_three.record_locator)
        self.assertTrue('XXXXXXXX' in ticket_three.record_locator)

        self.assertNotEqual(
            ticket_two.record_locator, ticket_three.record_locator)

class RecordLocatorAPITests(TestCase):
    """ Tests for the RecordLocator API. """

    def test_api_generate_locators(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=20852')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertTrue('locators' in r)
        self.assertEqual(len(r['locators']), 1)

    def test_api_multiple_locators(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=20852&num_locators=50')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertTrue('locators' in r)
        self.assertEqual(len(r['locators']), 50)

class TicketGeneratorTest(TestCase):
    """ Tests for the RecordLocator API. """

    def test_ticket_zip_match(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=91381')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertEqual(
            '91381',
            Ticket.objects.get(
                record_locator=r['locators'][0]['record_locator']).zip_code)


import os
import re
import numpy as np
from scipy.signal import argrelmin, argrelmax
from FT import DFT, IDFT

class Folder:
    def __init__(self, path):
        self.path = path
        self.filesData = dict()
        self.filesDFT = dict()
        self.filesIDFT = dict()
        self.filesExtremumIntervals = dict()

    def getCountFilesForMask(self, mask=".*.txt"):
        count = 0
        for filename in os.listdir(self.path):
            if re.match(mask, filename):
                count+=1
        return count

    def read(self, mask= ".*.txt"):
        for filename in os.listdir(self.path):
            if re.match(mask, filename):
                with open(os.path.join(self.path, filename), 'r') as f:
                    self.filesData[filename] = []
                    for line in f:
                        self.filesData[filename].append(float(line.replace(",", ".")))

    def getDFT(self):
        for filename in self.filesData:
            ft = DFT(self.filesData[filename])
            self.filesDFT[filename] = ft
            f = open(os.path.join(self.path, "DFT-" + filename), "a")
            for x in ft:
                f.write(str(x.real) + " " + str(x.imag) + "\n")
            f.close()

    def getIntervalIDFT(self, start = 0, end = 0):
        for filename in self.filesDFT:
            intervalDFT = np.copy(self.filesDFT[filename])
            suffix = ""
            if start != 0 or end != 0:
                suffix = str(start) + "-" + str(end) + "-" + filename
                intervalDFT[:start] = 0+0j
                intervalDFT[end:] = 0+0j
            else:
                suffix = "-full-" + filename

            ift = IDFT(intervalDFT)
            self.filesIDFT[filename] = ift
            np.savetxt(os.path.join(self.path, "IDFT-" + suffix),
                       np.real(ift), delimiter='\n')

            minima = argrelmin(self.filesIDFT[filename])
            maxima = argrelmax(self.filesIDFT[filename])
            np.savetxt(os.path.join(self.path, "MIN-" + suffix),
                       np.diff(minima), delimiter='\n', fmt='%i')
            np.savetxt(os.path.join(self.path, "MAX-" + suffix),
                       np.diff(maxima), delimiter='\n', fmt='%i')


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 09:33:43 2020

@author: pc
"""
# this script is used to construct the framework of U-net

# import package
import tensorflow as tf
import utils as ut

# the framework is the same as brain paper
def FCN(name, in_data, cat_num, cha_num, is_train, reuse = False):

    # construction
    with tf.variable_scope(name, reuse = reuse):
        # layer 1
        net = ut.conv_layer(in_data, [3, 3, 3, 1, cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, cha_num, 2*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 2
        net = ut.conv_layer(net, [3, 3, 3, 2*cha_num, 2*cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, 2*cha_num, 4*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 3
        net = ut.conv_layer(net, [3, 3, 3, 4*cha_num, 4*cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, 4*cha_num, 8*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 4
        w4 = ut.weight_variable([6, 6, 6, 8*cha_num, cat_num])
        net = ut.conv3d(net, w4, 1, 'VALID')
        net = tf.layers.batch_normalization(net, training = is_train)
        net = tf.nn.relu(net)

        # softmax
        net = tf.nn.softmax(net)

        return net

def FCN_ours(name, in_data, cat_num, is_train, kp1, kp2, reuse = False):
    # construction
    with tf.variable_scope(name, reuse = reuse):

        return


"""
Knowledge graph dataset for Relational-GCN
"""

from __future__ import print_function
from __future__ import absolute_import
import numpy as np
import scipy.sparse as sp
import os
import pandas as pd

np.random.seed(123)

class RGCNUnaryDataset(object):
    """RGCN Unary Classification dataset

    Each instance of unary template is regarded as a node
    its label is the template it satisfying

     An object of this class has 11 member attributes needed for unary
    classification:

    num_nodes: int
        number of entities of knowledge base
    num_rels: int
        number of relations (including reverse relation) of knowledge base
    num_classes: int
        number of classes/labels that of entities in knowledge base
    edge_src: numpy.array
        source node ids of all edges
    edge_dst: numpy.array
        destination node ids of all edges
    edge_type: numpy.array
        type of all edges
    edge_norm: numpy.array
        normalization factor of all edges
    labels: numpy.array
        labels of node entities
    train_idx: numpy.array
        ids of entities used for training
    valid_idx: numpy.array
        ids of entities used for validation
    test_idx: numpy.array
        ids of entities used for testing
    features: numpy.array
        input features of nodes

    When loading data, besides specifying dataset name, user can provide two
    optional arguments:

    Parameters
    ----------
    bfs_level: int
        prune out nodes that are more than ``bfs_level`` hops away from
        labeled nodes, i.e., nodes won't be touched during propagation. If set
        to a number less or equal to 0, all nodes will be retained.
    relabel: bool
        After pruning, whether or not to relabel all nodes with consecutive
        node ids
    """

    def __init__(self, name, type):
        self.name = name
        self.type = type
        self.dir = os.path.join(os.path.abspath('.'), 'dataset')
        self.dir = os.path.join(self.dir, self.type)
        self.dir = os.path.join(self.dir, self.name)

    def load(self, bfs_level=2, relabel=False):
        self.num_nodes, edges, self.num_rels, self.labels, labeled_nodes_idx, self.features = _load_data(self.name, self.dir)

        # bfs to reduce edges
        if bfs_level > 0:
            print("removing nodes that are more than {} hops away".format(bfs_level))
            row, col, edge_type = edges.transpose()
            A = sp.csr_matrix((np.ones(len(row)), (row, col)), shape=(self.num_nodes, self.num_nodes))
            bfs_generator = _bfs_relational(A, labeled_nodes_idx)
            lvls = list()
            lvls.append(set(labeled_nodes_idx))
            for _ in range(bfs_level):
                lvls.append(next(bfs_generator))
            to_delete = list(set(range(self.num_nodes)) - set.union(*lvls))
            eid_to_delete = np.isin(row, to_delete) + np.isin(col, to_delete)
            eid_to_keep = np.logical_not(eid_to_delete)
            self.edge_src = row[eid_to_keep]
            self.edge_dst = col[eid_to_keep]
            self.edge_type = edge_type[eid_to_keep]

            if relabel:  # False, so not run
                uniq_nodes, edges = np.unique((self.edge_src, self.edge_dst), return_inverse=True)
                self.edge_src, self.edge_dst = np.reshape(edges, (2, -1))
                node_map = np.zeros(self.num_nodes, dtype=int)
                self.num_nodes = len(uniq_nodes)
                node_map[uniq_nodes] = np.arange(self.num_nodes)
                self.labels = self.labels[uniq_nodes]
                self.train_idx = node_map[self.train_idx]
                self.test_idx = node_map[self.test_idx]
                print("{} nodes left".format(self.num_nodes))
        else:
            self.edge_src, self.edge_dst, self.edge_type = edges.transpose()

        # normalize by src degree, compute degrees according to edge_type
        _, inverse_index, count = np.unique((self.edge_dst, self.edge_type), axis=1, return_inverse=True, return_counts=True)
        degrees = count[inverse_index]  #c_{i,r} for each relation type
        self.edge_norm = np.ones(len(self.edge_dst), dtype=np.float32) / degrees.astype(np.float32)

        # convert to pytorch label format
        self.num_classes = self.labels.shape[1]
        #self.labels = np.argmax(self.labels, axis=1)

class RGCNBinaryDataset(object):
    """RGCN link prediction dataset

    Each binary template is regard as a binary relation between two entities (nodes)

    An object of this class has 5 member attributes needed for link
    prediction:

    num_nodes: int
        number of entities of rule base
    num_rels: int
        number of relations (including reverse relation) of rule base
    data: numpy.array
        all relation triplets (src, rel, dst)
    relation_features: dateframe

    Usually, user don't need to directly use this class. Instead, DGL provides
    wrapper function to load data (see example below).

    """
    def __init__(self, name):
        self.name = name
        self.dir = os.path.join(os.path.abspath('.'), 'binary_data')
        self.dir = os.path.join(self.dir, self.name)

    def load(self):
        entity_path = os.path.join(self.dir, 'nodes.dict')
        relation_path = os.path.join(self.dir, 'edges.dict')
        data_path = os.path.join(self.dir, 'binary_template_triples.txt')  # all triples
        node_feature_path = os.path.join(self.dir, 'node_features.csv')
        relation_feature_path = os.path.join(self.dir, 'relation_features.csv')
        self.node_features = pd.read_csv(node_feature_path, sep=',', encoding='utf-8')
        self.relation_features = pd.read_csv(relation_feature_path, sep=',', encoding='utf-8')
        entity_dict = _read_dictionary(entity_path)
        relation_dict = _read_dictionary(relation_path)
        self.complete_data = np.array(_read_triplets_as_list(data_path, entity_dict, relation_dict))  # (n_id, r_id, n_id)
        self.num_nodes = len(entity_dict)
        print("# entities: {}".format(self.num_nodes))
        self.num_rels = len(relation_dict)
        print("# relations: {}".format(self.num_rels))
        print("# edges: {}".format(len(self.complete_data)))   # total number of edges

def load_unary(dataset, feature_type, bfs_level, relabel):
    data = RGCNUnaryDataset(dataset, feature_type)
    data.load(bfs_level, relabel)
    return data

def load_binary(dataset):
    data = RGCNBinaryDataset(dataset)
    data.load()
    return data

def _sp_row_vec_from_idx_list(idx_list, dim):
    """Create sparse vector of dimensionality dim from a list of indices."""
    shape = (1, dim)
    data = np.ones(len(idx_list))
    row_ind = np.zeros(len(idx_list))
    col_ind = list(idx_list)
    return sp.csr_matrix((data, (row_ind, col_ind)), shape=shape)

def _get_neighbors(adj, nodes):
    """Takes a set of nodes and a graph adjacency matrix and returns a set of neighbors."""
    sp_nodes = _sp_row_vec_from_idx_list(list(nodes), adj.shape[1])
    sp_neighbors = sp_nodes.dot(adj)
    neighbors = set(sp.find(sp_neighbors)[1])  # convert to set of indices
    return neighbors

def _bfs_relational(adj, roots):
    """
    BFS for graphs with multiple edge types. Returns list of level sets.
    Each entry in list corresponds to relation specified by adj_list.
    """
    visited = set()
    current_lvl = set(roots)

    next_lvl = set()

    while current_lvl:

        for v in current_lvl:
            visited.add(v)

        next_lvl = _get_neighbors(adj, current_lvl)
        next_lvl -= visited  # set difference

        yield next_lvl

        current_lvl = set.union(next_lvl)

def _load_sparse_csr(filename):
    loader = np.load(filename)
    return sp.csr_matrix((loader['data'], loader['indices'], loader['indptr']),
                         shape=loader['shape'], dtype=np.float32)

def _save_sparse_csr(filename, array):
    np.savez(filename, data=array.data, indices=array.indices,
             indptr=array.indptr, shape=array.shape)

def _load_data(dataset_str='wine', dataset_path=None):
    """

    :param dataset_str:
    :param rel_layers:
    :param limit: If > 0, will only load this many adj. matrices
        All adjacencies are preloaded and saved to disk,
        but only a limited a then restored to memory.
    :return:
    """

    print('Loading dataset', dataset_str)
    fea_file = 'node_features.csv'
    feature_file = os.path.join(dataset_path, fea_file)  # node_features: word embedding + analogy space
    edge_file = os.path.join(dataset_path, 'edges_subset.npz')    # {edges: (s, t, r) / n: num_nodes / nrel : num_rels}
    labels_file = os.path.join(dataset_path, 'labels_subset.npz')  # n * l sparse matrix

    # load node features
    node_feature = pd.read_csv(feature_file, sep=',', encoding='utf-8')

    # load precomputed adjacency matrix and labels
    all_edges = np.load(edge_file)
    num_node = all_edges['n'].item()
    edge_list = all_edges['edges']
    num_rel = all_edges['nrel'].item()

    print('Number of nodes: ', num_node)
    print('Number of edges: ', len(edge_list))
    print('Number of relations: ', num_rel)

    labels = _load_sparse_csr(labels_file)
    labeled_nodes_idx = list(labels.nonzero()[0])

    print('Number of classes: ', labels.shape[1])

    return num_node, edge_list, num_rel, labels, labeled_nodes_idx, node_feature

# key = template name, value = id
def _read_dictionary(filename):
    d = {}
    with open(filename, 'r+') as f:
        for line in f:
            line = line.strip().split('\t')
            d[line[1]] = int(line[0])
    return d

# key = id, value = template name
def read_dictionary(filename):
    d = {}
    with open(filename, 'r+') as f:
        for line in f:
            line = line.strip().split('\t')
            d[int(line[0])] = line[1]
    return d

def _read_triplets(filename):
    with open(filename, 'r+') as f:
        for line in f:
            processed_line = line.strip().split('\t')
            yield processed_line

def _read_triplets_as_list(filename, entity_dict, relation_dict):
    l = []
    for triplet in _read_triplets(filename):
        s = entity_dict[triplet[0]]
        r = relation_dict[triplet[1]]
        o = entity_dict[triplet[2]]
        l.append([s, r, o])
    return l


"""
A super-simple minidom recipe for translating a dict to xml. It's simple
to add any feature (recursive conversion, pickle etc) to this if you need.

>>> dict2xml({"ok": 123, 12323: "haha"}).toprettyxml()
<?xml version="1.0" ?>
<dictionary>
    <12323>
        haha
    </12323>
    <ok>
        123
    </ok>
</dictionary>

>>> verbose_dict2xml({"ok": 123, 12323: "haha"}).toprettyxml()
<?xml version="1.0" ?>
<dictionary>
    <item>
        <name>
            12323
        </name>
        <value>
            haha
        </value>
    </item>
    <item>
        <name>
            ok
        </name>
        <value>
            123
        </value>
    </item>
</dictionary>

"""
from xml2dict import object_dict
from xml.dom.minidom import Document

def dict2xml(dictionary, name="dictionary"):
    "The keys are element names and the values are text"
    xml = Document()
    items = xml.createElement(name)
    xml.appendChild(items)
    for key, val in dictionary.items():
        node = xml.createElement(str(key))
        node.appendChild(xml.createTextNode(str(val)))
        items.appendChild(node)
    return xml

def typeCheck(item):
    if (str(type(item)) == "<class 'object_dict.object_dict'>"
        or str(type(item)) == "<type 'dict'>"
        or str(type(item)) == "<class 'object_dict'>"
        or str(type(item)) == "<class '__main__.object_dict'>"):
        return True
    else:
        return False

def recurseXML(dictionary, items, xml):
    for key, val in dictionary.items():
#        print "\tVal Type2", type(val)
        if typeCheck(val):
            recurseXML(val, items, xml)
        else:
            item = xml.createElement("item")
            items.appendChild(item)
            name_node = xml.createElement("name")
            name_node.appendChild(xml.createTextNode(str(key)))
            value_node = xml.createElement("value")
            value_node.appendChild(xml.createTextNode(str(val)))
            item.appendChild(name_node)
            item.appendChild(value_node)

def verbose_dict2xml(dictionary, name="dictionary"):
    "2 nodes per item - one for the name and one for the value"
    xml = Document()
    items = xml.createElement(name)
    xml.appendChild(items)
    for key, val in dictionary.items():
        if typeCheck(val):
            recurseXML(val, items, xml)
        else:
            item = xml.createElement("item")
            items.appendChild(item)
            name_node = xml.createElement("name")
            name_node.appendChild(xml.createTextNode(str(key)))
            value_node = xml.createElement("value")
            value_node.appendChild(xml.createTextNode(str(val)))
            item.appendChild(name_node)
            item.appendChild(value_node)
    return xml

if __name__ == "__main__":
    print("haha")
    xml = dict2xml({"ok": 123, 12323: "haha"})
    print(xml.toprettyxml())
    xml = verbose_dict2xml({"ok": 123, 12323: {"haha":"Go Joe", "Try Again":{"hahaha":"Super Efficiency", 'gaga':"Tired"}}})
    print(xml.toprettyxml())
    print("done")


# Imports
import pygame
import random

# Initialize game engine
pygame.init()

# Window
WIDTH = 1000
HEIGHT = 600
SIZE = (WIDTH, HEIGHT)
TITLE = "Space War"
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption(TITLE)

# Timer
clock = pygame.time.Clock()
refresh_rate = 60

# Colors
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (100, 255, 100)
DARK_BLUE = (2, 0, 17)

# Images
ship_img = pygame.image.load('assets/images/player_ship.png')
hurtflash_img = pygame.image.load('assets/images/player_ship-1.png')
laser_img = pygame.image.load('assets/images/catrunx4-2-1.png')

mob_img1 = pygame.image.load('assets/images/enemy_ship-1.png')
mob_img2 = pygame.image.load('assets/images/enemy_ship-2.png')

bomb_img = pygame.image.load('assets/images/UFO-2.png')

background = pygame.image.load('assets/images/background-stars.png')
start = pygame.image.load('assets/images/start_screen_img.png')

health_img = pygame.image.load('assets/images/health_unit.png')

# Fonts
FONT_SM = pygame.font.Font(None, 24)
FONT_XL = pygame.font.Font("assets/fonts/space_age.ttf", 96)

# Sounds
EXPLOSION = pygame.mixer.Sound('assets/sounds/explosion.ogg')

hongkong97 = pygame.mixer.Sound('assets/sounds/hong_kong_97.ogg')
stickers = pygame.mixer.Sound('assets/sounds/stickers.ogg')
music =  pygame.mixer.Sound('assets/sounds/party-stronger.ogg')
music2 =  pygame.mixer.Sound('assets/sounds/Tung_The_Icelandic.ogg')

shoot = pygame.mixer.Sound('assets/sounds/shoot.ogg')
ouch =  pygame.mixer.Sound('assets/sounds/hit_noise.ogg')

music2.play(-1)

# Stages
START = 0
PLAYING = 1
END = 2

# Game classes
class Ship(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__()

        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.speed = 3
        self.shield = 5

    def move_left(self):
        self.rect.x -= self.speed

    def move_right(self):
        self.rect.x += self.speed

    def shoot(self):
        laser = Laser(laser_img)
        laser.rect.centerx = self.rect.centerx
        laser.rect.centery = self.rect.top
        lasers.add(laser)
        shoot.play()

    def update(self, bombs, image, hurt):
        hit_list = pygame.sprite.spritecollide(self, bombs, True,
                                               pygame.sprite.collide_mask)

        for hit in hit_list:
            # play hit sound
            self.shield -= 1
            ouch.play()

        hit_list = pygame.sprite.spritecollide(self, mobs, False)

        if len(hit_list) > 0:
            self.shield = 0
            self.image = hurt

        if self.shield == 0:
            stickers.play()

            EXPLOSION.play()

            self.kill()

        if self.rect.x < -25:
            self.rect.x = -25

        elif self.rect.x > 950:
            self.rect.x = 950

class Laser(pygame.sprite.Sprite):

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

        self.image = image
        self.rect = self.image.get_rect()

        self.speed = 5

    def update(self):
        self.rect.y -= self.speed

        if self.rect.bottom < 0:
            self.kill()

class Mob(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__()

        self.image = image
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def drop_bomb(self):
        bomb = Bomb(bomb_img)
        bomb.rect.centerx = self.rect.centerx
        bomb.rect.centery = self.rect.bottom
        bombs.add(bomb)

    def update(self, lasers):
        hit_list = pygame.sprite.spritecollide(self, lasers, True,
                                               pygame.sprite.collide_mask)

        if len(hit_list) > 0:

            EXPLOSION.play()
            player.score += 1
            self.kill()
'''
class Health(pygame.sprite.Sprite):
    def __init__(self, x, y, image):
        super().__init__()

        self.image = image

    def update(self, ship, bomb):
        hit_list = pygame.sprite.spritecollide(ship, bombs, True,
                                               pygame.sprite.collide_mask)

        if len(hit_list) > 5:

            EXPLOSION.play()
            player.score += 1
            self.kill()

'''

class Bomb(pygame.sprite.Sprite):

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

        self.image = image
        self.rect = self.image.get_rect()

        self.speed = 3

    def update(self):
        self.rect.y += self.speed

        if self.rect.bottom > 600:
            self.kill()

class Fleet:

    def __init__(self, mobs):
        self.mobs = mobs
        self.moving_right = True
        self.speed = 5
        self.bomb_rate = 60

    def move(self):
        reverse = False

        for m in mobs:
            if self.moving_right:
                m.rect.x += self.speed
                if m.rect.right >= WIDTH:
                    reverse = True
            else:
                m.rect.x -= self.speed
                if m.rect.left <=0:
                    reverse = True

        if reverse == True:
            self.moving_right = not self.moving_right
            for m in mobs:
                m.rect.y += 32

    def choose_bomber(self):
        rand = random.randrange(0, self.bomb_rate)
        all_mobs = mobs.sprites()

        if len(all_mobs) > 0 and rand == 0:
            return random.choice(all_mobs)
        else:
            return None

    def update(self):
        self.move()

        bomber = self.choose_bomber()
        if bomber != None:
            bomber.drop_bomb()

# Make game objects
'''
health1 = Health(0, 715, health_img)
health2 = Health(50, 715, health_img)
health3 = Health(100, 715, health_img)
health4 = Health(150, 715, health_img)
heatlh5 = Health(200, 715, health_img)
'''

ship = Ship(384, 485, ship_img)
mob1 = Mob(128, 0, mob_img1)
mob2 = Mob(256, 0, mob_img1)
mob3 = Mob(384, 0, mob_img1)
mob4 = Mob(512, 0, mob_img1)
mob5 = Mob(640, 0, mob_img2)

# Make sprite groups
player = pygame.sprite.GroupSingle()
player.add(ship)
player.score = 0

lasers = pygame.sprite.Group()

mobs = pygame.sprite.Group()
mobs.add(mob1, mob2, mob3, mob4, mob5)

bombs = pygame.sprite.Group()

fleet = Fleet(mobs)

# set stage
stage = START

# Game helper functions
def show_title_screen():

    screen.blit(start, (0,0))

    title_text = FONT_XL.render("Space War!", 1, WHITE)
    screen.blit(title_text, [128, 204])

def show_stats(player):
    score_text = FONT_SM.render(str(player.score), 1, WHITE)
    screen.blit(score_text, [32, 32])

'''keep thinking on ways to display the health thing
def health_meter():

    h_y = 32
    h_x = 800

    screen.blit(health_img, [h_x, h_y])
'''

# Game loop
done = False

while not done:
    # Event processing (React to key presses, mouse clicks, etc.)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if stage == START:
                if event.key == pygame.K_SPACE:
                    stage = PLAYING
            elif stage == PLAYING:
                if event.key == pygame.K_SPACE:
                    ship.shoot()
                if event.key == pygame.K_p:
                    stage = END

    if stage == PLAYING:
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_LEFT]:
            ship.move_left()
        elif pressed[pygame.K_RIGHT]:
             ship.move_right()

    # Game logic (Check for collisions, update points, etc.)
    if stage == PLAYING:

        player.update(bombs, ship_img, hurtflash_img)
        lasers.update()
        mobs.update(lasers)
        bombs.update()
        fleet.update()

        if len(player) == 0:
            stage == END

    # Drawing code (Describe the picture. It isn't actually drawn yet.)

    screen.blit(background, (0,0))
    lasers.draw(screen)
    player.draw(screen)
    bombs.draw(screen)
    mobs.draw(screen)
    show_stats(player)
    #health_meter()

    if stage == START:
        show_title_screen()

    # Update screen (Actually draw the picture in the window.)
    pygame.display.flip()

    # Limit refresh rate of game loop
    clock.tick(refresh_rate)

# Close window and quit
pygame.quit()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 22 20:55:22 2018

@author: hhanchan
"""

import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
# (c)
A=np.matrix([[-1,0,0,0,0,0],[0,0.5,0,0,0,0],[0,0,0.5,0,0,0],[0,0,0,0.125,0.125,0],[0,0,0,0.125,0.125,0],[0,0,0,0,0,0.25]])
eta1=0.5
b=np.matrix([[1,1,1,1,1,1]]).transpose()
g=list(range(1001))
x=list(range(1001))
x[0]=np.matrix([[0,0,0,0,0,0]]).transpose()
g[0]=(LA.norm(A*x[0]-b))*(LA.norm(A*x[0]-b))
for k in range(1,1001):
    x[k]=x[k-1]+eta1*A.transpose()*(b-A*x[k-1])
    g[k]=(LA.norm(A*x[k]-b))*(LA.norm(A*x[k]-b))

v=np.matrix([[0,0,0,1,-1,0]]).transpose()
xhat=x[1000]+v
print (g[1000])
ghat=(LA.norm(A*xhat-b))*(LA.norm(A*xhat-b))
print (ghat)

print (LA.norm(xhat)*LA.norm(xhat))
print (LA.norm(x[1000])*LA.norm(x[1000]))

# (d)
eta2=0.75
y=list(range(101))
y[0]=np.matrix([[0,0,0,0,0,0]]).transpose()
for k in range(1,100):
    y[k]=y[k-1]+eta2*A.transpose()*(b-A*y[k-1])

kindex=np.arange(1,101,1)
f1=g[1:101]
f2=list(range(100))
f2[0]=(LA.norm(A*y[0]-b))*(LA.norm(A*y[0]-b))
for k in range(1,100):
    f2[k]=(LA.norm(A*y[k]-b))*(LA.norm(A*y[k]-b))
plt.plot(kindex,f1,'g-.',kindex,f2,'ro')
green=mpatches.Patch(color='green',label='$||Ax^{(k)}-b||_2^2$')
red=mpatches.Patch(color='red',label='$||Ay^{(k)}-b||_2^2$')
plt.legend(handles=[green,red])
plt.xlabel('k')
plt.ylabel('g(x)')
plt.title('g(x): Richardson Iteration')
plt.show()


import pygame

import settings

import levelgenerator

from cluster import Cluster
from gameobjects.actor import Actor
from gameobjects.brick import Brick
from gameobjects.coin import Coin
from gameobjects.monster import Monster
from gameobjects.exit import Exit

# Map van levelmap-id naar klasse voor game object.
ID_TO_OBJECT_CLASS = {}
ID_TO_OBJECT_CLASS[levelgenerator.BRICK] = Brick
ID_TO_OBJECT_CLASS[levelgenerator.COIN] = Coin
ID_TO_OBJECT_CLASS[levelgenerator.MONSTER] = Monster
ID_TO_OBJECT_CLASS[levelgenerator.EXIT] = Exit

class Level(object):
	"""Object voor een level.

		Hierin staat opgeslagen waar alle bricks zitten.

	"""

	def __init__(self):

		# Player object.
		self.player = None

		# Boolean voor of je het level hebt gehaald.
		self.finished = False

		# Bepaal de rect van het gehele level.
		self.rect = pygame.Rect(0, 0, settings.TILE_WIDTH*settings.LEVEL_WIDTH, settings.TILE_HEIGHT*settings.LEVEL_HEIGHT)

		# Bepaal de lijst met alle game objects van dit level.
		#
		# Dit doen we door eerst een levelmap te genereren met een level-
		# generator. Vervolgens lopen we de level map door en maken we alle game
		# objects aan.
		#
		levelMap = levelgenerator.generateLevelMap()
		gameObjectList = self.getGameObjectListFromLevelMap(levelMap)

		# Maak cluster aan waar alle game objects in worden opgeslagen.
		staticObjectList = [k for k in gameObjectList if isinstance(k, Actor) == False]
		self.cluster = Cluster(self, staticObjectList, self.rect)

		# Maak lijst aan met alle actors.
		self.actorList = [k for k in gameObjectList if isinstance(k, Actor) == True]

	def getGameObjectListFromLevelMap(self, levelMap):
		"""Return lijst met alle game objects beschreven in 'levelMap'.
		"""
		# De lijst met game objects die we gaan returnen.
		gameObjectList = []

		# Loop door alle tiles heen.
		for y in xrange(len(levelMap)):
			for x in xrange(len(levelMap[0])):

				# Bepaal de x/y positie.
				posX = x * settings.TILE_WIDTH
				posY = y * settings.TILE_HEIGHT

				# Haal tile op.
				tile = levelMap[y][x]

				# Loop door alle game object id's in de tile heen.
				for gameObjectId in tile:

					# Bepaal de class van het game object.
					gameObjectClass = ID_TO_OBJECT_CLASS[gameObjectId]

					# Maak aan en zet in de lijst.
					gameObjectRect = pygame.Rect(posX, posY, settings.TILE_WIDTH, settings.TILE_HEIGHT)
					gameObject = gameObjectClass(self, gameObjectRect)
					gameObjectList.append(gameObject)

		# Return de game objects.
		return gameObjectList

	def getGameObjectList(self, clusterCollisionRect):
		"""Return alle game objects.
			Er worden alleen statische game objects gereturned die colliden met
			'clusterCollisionRect'. Daarnaast worden alle actors gereturned.
		"""
		gameObjectList = self.cluster.getGameObjectList(clusterCollisionRect)
		gameObjectList += self.getActorList()
		return gameObjectList

	def removeGameObject(self, gameObject):
		"""Verwijder een game object uit het level.
		"""
		if isinstance(gameObject, Actor):
			self.actorList.remove(gameObject)
		else:
			self.cluster.removeGameObject(gameObject)

	def getActorList(self):
		"""Return lijst met alle actors.
		"""
		return self.actorList[:]

	def setFinished(self, finished):
		"""Set of het level is gehaald.
		"""
		self.finished = finished

	def isFinished(self):
		"""Return of je het level hebt gehaald.
		"""
		return self.finished

	def getPlayer(self):
		"""Return het player object.
		"""
		return self.player

	def setPlayer(self, player):
		"""Set het player object.
		"""
		self.player = player


from django.urls import path
from . import views as cus_views
urlpatterns =[

    path('',cus_views.show_transations, name='transations'),
    path('new_transation/',cus_views.new_transation, name='new_transation'),
    path('details/<int:id>/',cus_views.details_customer, name='details_customer'),
    path('test/',cus_views.Test),
    # path('delete/<int:id>/',cus_views.delete_customer, name='delete_customer'),
    # path('exa/',cus_views.matltiplae, name='exa'),

]


from nltk import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import json

def data_processing(dataset):
    dataset_lower = [data.lower() for data in dataset]
    dataset_tokenized = [word_tokenize(data) for data in dataset_lower]
    stop_words = stopwords.words('english')
    dataset_filtered = [words for words in dataset_tokenized if words not in stop_words]
    english_styled_punctuations = [',', '.''?', '/', ':', '<', '>', '[', ']', '(', ')', '*', '&', '^', '%', '$', '#',
                                   '@', '!', '~', '`', '+', '_', '-', '“', '”', '~~', '``', '`', '{', '}', str(''), '"',
                                   '\\', '…', '\'', '\"', '\'\'', '...']
    dataset_with_no_punctuations = [[words for words in data if words not in english_styled_punctuations] for data in
                                    dataset_filtered]
    return dataset_with_no_punctuations

def data_stemming(dataset):
    ps = PorterStemmer()
    dataset_stemmed = [ps.stem(word) for word in dataset]
    return dataset_stemmed

def dict_construction(path):
    contents = []
    with open(path) as f:
        content = f.readlines()
        contents.append(content)
    tweets = []
    a = contents[0]
    for i in range(len(a)):
        dict = {'tweet NO': i}
        dataset = [a[i]]
        dataset = data_processing(dataset)
        dataset = dataset[0]
        data = dataset[dataset.index('username') + 1:dataset.index('clusterno')]
        if 'http' not in dataset:
            text = dataset[dataset.index('text') + 1:dataset.index('timestr')]
        else:
            del dataset[dataset.index('http'):dataset.index('timestr')]
            text = dataset[dataset.index('text') + 1:dataset.index('timestr')]
        text = data_stemming(text)
        data.extend(text)
        time = dataset[dataset.index('timestr') + 1:dataset.index('+0000')]
        data.extend(time)
        dict['data'] = data
        tweets.append(dict)
    return tweets

def build_inverted_index(tweets):
    dict_inv = {}
    for i in tweets:
        for j in i['data']:
            if j not in dict_inv.keys():
                dict_inv[j] = [i['tweet NO']]
            else:
                dict_inv[j].append(i['tweet NO'])
    return dict_inv

def merge(dict_inv, query):
    query = query_process(query)
    merge_list = []
    for i in query:
        merge_list.append(dict_inv[i])
    while len(merge_list) != 1:
        merge_list_small = []
        for i in range(1, len(merge_list)):
            temp = []
            for item1 in merge_list[0]:
                for item2 in merge_list[i]:
                    if item1 == item2:
                        temp.append(item1)
                        break
            merge_list_small.append(temp)
        merge_list = merge_list_small
        print(len(merge_list))
    return merge_list

def OR(dict_inv, word1, word2):
    list1 = dict_inv[word1]
    list2 = dict_inv[word2]
    not_list = [word for word in list1 if word in list2]
    return not_list

def NOT(dict_inv, word1, word2):
    list1 = dict_inv[word1]
    list2 = dict_inv[word2]
    or_list = [word for word in list1 if word not in list2]
    return or_list

def query_process(query):
    query = data_processing([query])[0]
    query = data_stemming(query)
    return query

if __name__ == '__main__':
    path = 'C:\\Users\\97653\\Desktop\\人工智能\\IR&DM\\Homework3-tweets\\tweets.txt'
    tweets = dict_construction(path)
    dict_inv = build_inverted_index(tweets)
    query = 'Ron Weasley birthday'
    result = merge(dict_inv, query)
    print(result)


from src.utilities import PrepUtility
from src.rules import rules, slot_list, extractSlot, confirmations
from src.slotDetection import find_connection, find_dates, find_class_type, find_name, find_trip, get_place
from datetime import datetime
import random
import os
import pathlib
from src.utilities import read_json, write_json

root = pathlib.Path(os.path.abspath(__file__)).parent.parent
test_tag_path = str(root) + '/model_tmp/test_results/tagging.test.hyp.txt'

slot_checked = []

def get_response(message):
    user_in = PrepUtility.create_test_seq_in(message)
    tag_ls, word_ls = extract_information(test_tag_path)
    customer_name = find_name(message)
    place_from, place_to, isNewPlace = get_place(tag_ls, word_ls)
    date_departure, date_return, isNewDate = find_dates(message)
    is_round_trip, isNewTrip = find_trip(tag_ls, word_ls)
    if date_departure and date_return:
        is_round_trip = True
    connection_limit, isNewConnection = find_connection(tag_ls, word_ls)
    class_type, isNewClass = find_class_type(tag_ls, word_ls)
    informationDict = dict(date_departure=date_departure, date_return=date_return,
                    place_from=place_from, place_to=place_to, is_round_trip=is_round_trip,
                    customer_name=customer_name, connection_limit=connection_limit, class_type=class_type,
                           isNewPlace = isNewPlace, isNewDate = isNewDate,
                           isNewConnection = isNewConnection, isNewClass = isNewClass)
    checkSlot(place_from, place_to, date_departure, date_return, is_round_trip, connection_limit, customer_name, class_type)

    #print("[INFO] user in = ", user_in)
    print('[INFO] slot_checked before: ', slot_checked)
    slot = extractSlot(user_in)
    if (slot != None) and (slot[0] not in slot_checked):
        slotIndex = slot_list.index(slot[0])
        slot_checked.extend(slot)
        message_response = random.choice(rules[slotIndex]['questions'])
        response_list = prepareResponse(slot[0], message_response, informationDict)
    else:
        slot_left = [s for s in slot_list if s not in slot_checked]
        if slot_left:
            slot_to_check = slot_left[0]
            slot_checked.append(slot_to_check)
        else:
            slot_to_check = 'defaulterror'
        slotIndex = slot_list.index(slot_to_check)
        if slot_to_check == 'searchResult':
            if is_round_trip == True and connection_limit == 'zero':
                message_response = rules[slotIndex]['questions']['roundwaywithout']
            if is_round_trip == True and connection_limit != 'zero':
                message_response = rules[slotIndex]['questions']['roundwaywithconnection']
            if is_round_trip == False and connection_limit == 'zero':
                message_response = rules[slotIndex]['questions']['onewaywithout']
            if is_round_trip == False and connection_limit != 'zero':
                message_response = rules[slotIndex]['questions']['onewaywithconnection']
        else:
            message_response = random.choice(rules[slotIndex]['questions'])
        response_list = prepareResponse(slot_to_check, message_response, informationDict)

    is_new_from_place = True if isNewPlace and place_from else False
    is_new_to_place = True if isNewPlace and place_to else False
    is_new_from_date = True if isNewDate and date_departure else False
    is_new_return_date = True if isNewDate and date_return else False
    is_new_round_trip = None
    if isNewTrip and is_round_trip:
        is_new_round_trip = True
    if isNewTrip and not is_round_trip:
        is_new_round_trip = False

    print('[INFO] slot_checked after: ', slot_checked)
    response = dict(messages=response_list, date_departure=date_departure, date_return=date_return,
                    place_from=place_from, place_to=place_to, is_round_trip=is_round_trip,
                    customer_name=customer_name, class_type=class_type, connection_limit=connection_limit,
                    is_new_from_place = str(is_new_from_place), is_new_to_place = str(is_new_to_place),
                    is_new_from_date = str(is_new_from_date), is_new_return_date = str(is_new_return_date),
                    is_new_round_trip = str(is_new_round_trip),
                    is_new_connection = str(isNewConnection), is_new_class = str(isNewClass))
    return response

def extract_information(test_tag_path):
    f = open(test_tag_path, "r")
    lines = f.readlines()
    tag_ls = []
    word_ls = []
    for line in lines:
        if line.strip().split()[0] == 'EOS':
            break
        elif line.strip().split()[0] != 'BOS':
            word_ls.append(line.strip().split()[0])
            tag_ls.append(line.strip().split()[1])
    print('[INFO] extract_information tag_ls: ', tag_ls, 'word_ls: ', word_ls)
    return tag_ls, word_ls

def checkSlot(place_from, place_to, date_departure, date_return, is_round_trip, connection_limit, customer_name, class_type):
    if 'place_from' not in slot_checked and place_from:
        slot_checked.append('place_from')
        if 'place'not in slot_checked:
            slot_checked.append('place')
    if 'place_to' not in slot_checked and place_to:
        slot_checked.append('place_to')
        if 'place'not in slot_checked:
            slot_checked.append('place')

    if 'date_departure' not in slot_checked and date_departure:
        slot_checked.append('date_departure')
        if 'date' not in slot_checked:
            slot_checked.append('date')
    if 'date_return' not in slot_checked and (date_return or is_round_trip==False):
        slot_checked.append('date_return')
        if 'date' not in slot_checked:
            slot_checked.append('date')

    if 'trip_type' not in slot_checked and (is_round_trip is not None):
        slot_checked.append('trip_type')

    if 'connection_limit' not in slot_checked and connection_limit:
        slot_checked.append('connection_limit')

    if 'name' not in slot_checked and customer_name:
        slot_checked.append('name')
        slot_checked.append('greeting')

    if 'class' not in slot_checked and class_type:
        slot_checked.append('class')

def prepareResponse(slot, response, informationDict):
    response_list = []
    customer_name = informationDict.get('customer_name', '')
    place_from = informationDict.get('place_from', None)
    place_to = informationDict.get('place_to', None)
    date_departure = informationDict.get('date_departure', None)
    date_return = informationDict.get('date_return', None)
    is_round_trip = informationDict.get('is_round_trip', None)
    connection_limit = informationDict.get('connection_limit', None)
    class_type = informationDict.get('class_type', None)
    isNewPlace = informationDict.get('isNewPlace')
    isNewDate = informationDict.get('isNewDate')
    #isNewTrip = informationDict.get('isNewTrip')
    isNewConnection = informationDict.get('isNewConnection')
    isNewClass = informationDict.get('isNewClass')
    if slot == 'greeting':
        response = response.format(name = ' ' + customer_name)
    if slot == 'searchResult':
        if is_round_trip == True:
            if connection_limit == 'zero':
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from, place_to=place_to,
                                       date_departure = date_departure, date_return = date_return)
            else:
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, connection_limit=connection_limit,
                                       date_departure = date_departure, date_return = date_return)
        if is_round_trip == False:
            if connection_limit == 'zero':
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, date_departure = date_departure)
            else:
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, connection_limit=connection_limit,
                                       date_departure = date_departure)

    summary_response = []
    checkList = [isNewPlace, isNewDate, isNewClass, isNewConnection]
    if any(checkList):
        summary_response.append(random.choice(confirmations['opening1']))
        summary_response.append(random.choice(confirmations['opening2']))
        if is_round_trip is None:
            summary_response.append(confirmations['ticket'])
            if date_departure and isNewDate:
                summary_response.append(confirmations['one_date'].format(date_departure=date_departure))
        if is_round_trip == True:
            summary_response.append(confirmations['round'])
            if date_departure and date_return and isNewDate:
                summary_response.append(
                    confirmations['round_date'].format(date_departure=date_departure, date_return=date_return))
        elif is_round_trip == False:
            summary_response.append(confirmations['oneway'])
            if date_departure and isNewDate:
                summary_response.append(confirmations['one_date'].format(date_departure=date_departure))

    if place_from and place_to and isNewPlace:
        summary_response.append(confirmations['place'].format(place_from=place_from, place_to=place_to))
    # if is_round_trip == True and date_departure and date_return and isNewDate:
    #     summary_response.append(confirmations['round_date'].format(date_departure=date_departure, date_return=date_return))
    # if is_round_trip == False and date_departure and isNewDate:
    #     summary_response.append(confirmations['one_date'].format(date_departure=date_departure))
    if connection_limit and isNewConnection:
        if connection_limit == 'zero':
            summary_response.append(confirmations['direct'])
        else:
            summary_response.append(confirmations['connection'].format(connection_limit=connection_limit))
    if class_type and isNewClass:
        summary_response.append(confirmations['class'].format(class_type=class_type))
    if len(summary_response)>=1:
        summary_response.append(confirmations['ending'])
        summary = ' '.join(summary_response)
        response_list.append(dict(message=summary, time=format_current_time()))

    response_list.append(dict(message=response, time=format_current_time()))
    return response_list

def format_current_time():
    return datetime.now().strftime("%H:%M:%S")


#!/usr/bin/env python

import os
from mpi4py import MPI
from baselines.common import set_global_seeds, tf_util as U
from baselines import bench
import os.path as osp
import gym, logging
from baselines import logger
import sys

def train(env_id, num_timesteps, seed):
    from baselines.ppo1 import mlp_policy, pposgd_simple
    rank = MPI.COMM_WORLD.Get_rank()
    os.environ["CUDA_VISIBLE_DEVICES"] = str(rank % 4)
    U.make_session(num_cpu=1, gpu_fraction=.10).__enter__()
    workerseed = seed + 10000 * MPI.COMM_WORLD.Get_rank()
    set_global_seeds(workerseed)
    env = gym.make(env_id)
    if rank != 0: logger.set_level(logger.DISABLED)
    def policy_fn(name, ob_space, ac_space):
        return mlp_policy.MlpPolicy(name=name, ob_space=ob_space, ac_space=ac_space,
            hid_size=256, num_hid_layers=2)
    env = bench.Monitor(env, logger.get_dir() and
        osp.join(logger.get_dir(), "monitor.json"))
    env.seed(seed)
    gym.logger.setLevel(logging.WARN)
    pposgd_simple.learn(env, policy_fn,
            max_timesteps=num_timesteps,
            timesteps_per_batch=1250,
            clip_param=0.2, entcoeff=0.0,
            optim_epochs=20, optim_stepsize=1e-4, optim_batchsize=128,
            gamma=0.995, lam=0.95, schedule='constant',
        )
    env.close()

def main():
    import argparse
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--env', help='environment ID', default='Humanoid-v1')
    parser.add_argument('--seed', help='RNG seed', type=int, default=0)
    args = parser.parse_args()
    train(args.env, num_timesteps=100e6, seed=args.seed)

if __name__ == '__main__':
    main()


import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from mpl_toolkits.mplot3d import Axes3D

def rgb2gray(image):
    return np.dot(image[...,:3], [0.299, 0.587, 0.114])

color = mpimg.imread('lobo.jpg')

red = color.copy()
red[:, :, 1] = 0
red[:, :, 2] = 0

green = color.copy()
green[:, :, 0] = 0
green[:, :, 2] = 0

blue = color.copy()
blue[:, :, 0] = 0
blue[:, :, 1] = 0

yellow = color.copy()
yellow[:, :, 2] = 0

purple = color.copy()
purple[:, :, 1] = 0

cyan = color.copy()
cyan[:, :, 0] = 0

# First Plot
plt.figure(1)
plt.subplot(2,2,1)
plt.imshow(color)
plt.axis('off')

plt.subplot(2,2,2)
plt.imshow(red)
plt.axis('off')

plt.subplot(2,2,3)
plt.imshow(green)
plt.axis('off')

plt.subplot(2,2,4)
plt.imshow(blue)
plt.axis('off')

#Second Plot
plt.figure(2)
plt.subplot(2,2,1)
plt.imshow(color)
plt.axis('off')

plt.subplot(2,2,2)
plt.imshow(yellow)
plt.axis('off')

plt.subplot(2,2,3)
plt.imshow(cyan)
plt.axis('off')

plt.subplot(2,2,4)
plt.imshow(purple)
plt.axis('off')

#Third Plot
plt.figure(3)
plt.subplot(2,2,1)
plt.imshow(color)
plt.axis('off')

plt.subplot(2,2,2)
redgray = rgb2gray(red)
plt.imshow(redgray, cmap = plt.get_cmap('gray'))
plt.axis('off')

plt.subplot(2,2,3)
greengray = rgb2gray(green)
plt.imshow(greengray, cmap = plt.get_cmap('gray'))
plt.axis('off')

plt.subplot(2,2,4)
bluegray = rgb2gray(blue)
plt.imshow(bluegray, cmap = plt.get_cmap('gray'))
plt.axis('off')

#Fourth Plot
plt.figure(4)
plt.subplot(2,2,1)
gray = rgb2gray(color)
plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.axis('off')

plt.subplot(2,2,2)
redgray = rgb2gray(red)
plt.imshow(redgray, cmap = plt.get_cmap('gray'))
plt.axis('off')

plt.subplot(2,2,3)
greengray = rgb2gray(green)
plt.imshow(greengray, cmap = plt.get_cmap('gray'))
plt.axis('off')

plt.subplot(2,2,4)
bluegray = rgb2gray(blue)
plt.imshow(bluegray, cmap = plt.get_cmap('gray'))
plt.axis('off')

#Fifth Plot

xx, yy = np.mgrid[0:gray.shape[0], 0:gray.shape[1]]
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
"""
ax.plot_surface(xx, yy, gray ,rstride=1, cstride=1, cmap=plt.cm.gray,linewidth=2)
ax.view_init(80, 30)
"""
ax.plot_surface(xx, yy, gray, cmap=cm.coolwarm, linewidth=0, antialiased=False)

plt.show()


from torch import nn
import torch.nn.functional as F
from torch.autograd import Variable

class LocationAttention(nn.Module):
    """
    Calculates context vector based on previous decoder hidden state (query vector),
    encoder output features, and convolutional features extracted from previous attention weights.
    Attention-Based Models for Speech Recognition
    https://arxiv.org/pdf/1506.07503.pdf

    Query vector is either the previous output or the last decoder hidden state.

    Note: modified for bilinear dot product attention instead of bahandanau attention
    """

    def __init__(self, dim, num_location_features=32):
        super(LocationAttention, self).__init__()
        self.conv = nn.Conv1d(in_channels=1, out_channels=num_location_features,
                              kernel_size=31, padding=15)
        self.W = nn.Linear(dim, dim, bias=False)
        self.L = nn.Linear(num_location_features, dim, bias=False)

    def score(self, query_vector, encoder_out, mask=None):
        # linear transform encoder out (seq, batch, dim)
        encoder_out = self.W(encoder_out)
        # (batch, seq, dim) | (2, 15, 50)
        encoder_out = encoder_out.permute(1, 0, 2)
        if isinstance(mask, Variable):
            conv_features = self.conv(mask.permute(0, 2, 1))  # (batch, dim , seq)
            encoder_out = encoder_out + self.L(conv_features.permute(0, 2, 1))  # (batch, seq , dim)
        # (2, 15, 50) @ (2, 50, 1)
        return encoder_out @ query_vector.permute(1, 2, 0)

    def forward(self, query_vector, encoder_out, mask=None):
        energies = self.score(query_vector, encoder_out)
        mask = F.softmax(energies, dim=1)
        context = encoder_out.permute(
            1, 2, 0) @ mask  # (batch, dim, seq) @ (batch, seq, dim)
        context = context.permute(2, 0, 1)  # (seq, batch, dim)
        return context, mask


#
# Solution to Project Euler problem 46

import eulerlib, itertools, sys
if sys.version_info.major == 2:
	filterfalse = itertools.ifilterfalse
else:
	filterfalse = itertools.filterfalse

def compute():
	ans = next(filterfalse(test_goldbach, itertools.count(9, 2)))
	return str(ans)

def test_goldbach(n):
	if n % 2 == 0 or eulerlib.is_prime(n):
		return True
	for i in itertools.count(1):
		k = n - 2 * i * i
		if k <= 0:
			return False
		elif eulerlib.is_prime(k):
			return True

if __name__ == "__main__":
	print(compute())