import tensorflow.compat.v1 as tf
print(tf.__version__)
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
from graphnnSiamese import graphnn
from utils import *
import os
import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='0,1,2,3',
        help='visible gpu device')
parser.add_argument('--use_device', type=str, default='/gpu:1',
        help='used gpu device')
parser.add_argument('--fea_dim', type=int, default=76,
        help='feature dimension')
parser.add_argument('--embed_dim', type=int, default=64,
        help='embedding dimension')
parser.add_argument('--embed_depth', type=int, default=5,
        help='embedding network depth')
parser.add_argument('--output_dim', type=int, default=64,
        help='output layer dimension')
parser.add_argument('--iter_level', type=int, default=5,
        help='iteration times')
parser.add_argument('--lr', type=float, default=1e-4,
        help='learning rate')
parser.add_argument('--epoch', type=int, default=100,
        help='epoch number')
parser.add_argument('--batch_size', type=int, default=128,
        help='batch size')
# parser.add_argument('--load_path', type=str,
        # default='../data/saved_model/graphnn_model_ghidra/saved_ghidra_model_best',
        # help='path for model loading, "#LATEST#" for the latest checkpoint')
parser.add_argument('--load_path', type=str,
        default='../data/saved_model/graphnn_model_ghidra_depth5/graphnn_model_ghidra_best',
        help='path for model loading, "#LATEST#" for the latest checkpoint')
parser.add_argument('--log_path', type=str, default=None,
        help='path for training log')

if __name__ == '__main__':
    args = parser.parse_args()
    args.dtype = tf.float32
    print("=================================")
    print(args)
    print("=================================")

    os.environ["CUDA_VISIBLE_DEVICES"]=args.device
    Dtype = args.dtype
    NODE_FEATURE_DIM = args.fea_dim
    EMBED_DIM = args.embed_dim
    EMBED_DEPTH = args.embed_depth
    OUTPUT_DIM = args.output_dim
    ITERATION_LEVEL = args.iter_level
    LEARNING_RATE = args.lr
    MAX_EPOCH = args.epoch
    BATCH_SIZE = args.batch_size
    LOAD_PATH = args.load_path
    LOG_PATH = args.log_path
    DEVICE = args.use_device

    SHOW_FREQ = 1
    TEST_FREQ = 1
    SAVE_FREQ = 5
    # DATA_FILE_NAME_VALID = '../data/validation_arm2non_arm_gemini_data/'
    DATA_FILE_NAME_TRAIN_TEST = '../data/vector_deduplicate_ghidra_format_less_compilation_cases/train_test'
    F_PATH_TRAIN_TEST = get_f_name(DATA_FILE_NAME_TRAIN_TEST)
    FUNC_NAME_DICT_TRAIN_TEST = get_f_dict(F_PATH_TRAIN_TEST)

    print("start reading data")
    Gs_train_test, classes_train_test = read_graph(F_PATH_TRAIN_TEST, FUNC_NAME_DICT_TRAIN_TEST, NODE_FEATURE_DIM)
    print("train and test ---- 8:2")
    print("{} graphs, {} functions".format(len(Gs_train_test), len(classes_train_test)))

    perm = np.random.permutation(len(classes_train_test))
    Gs_train, classes_train, Gs_test, classes_test =\
            partition_data(Gs_train_test, classes_train_test, [0.8, 0.2], perm)
    print("Train: {} graphs, {} functions".format(
            len(Gs_train), len(classes_train)))
    print("Test: {} graphs, {} functions".format(
            len(Gs_test), len(classes_test)))

    print("valid")
    DATA_FILE_NAME_VALID = '../data/vector_deduplicate_ghidra_format_less_compilation_cases/valid'
    F_PATH_VALID = get_f_name(DATA_FILE_NAME_VALID)
    FUNC_NAME_DICT_VALID = get_f_dict(F_PATH_VALID)
    Gs_valid, classes_valid = read_graph(F_PATH_VALID, FUNC_NAME_DICT_VALID, NODE_FEATURE_DIM)
    print("{} graphs, {} functions".format(len(Gs_valid), len(classes_valid)))
    Gs_valid, classes_valid = partition_data(Gs_valid, classes_valid, [1], list(range(len(classes_valid))))

    # Model
    gnn = graphnn(
            N_x = NODE_FEATURE_DIM,
            Dtype = Dtype,
            N_embed = EMBED_DIM,
            depth_embed = EMBED_DEPTH,
            N_o = OUTPUT_DIM,
            ITER_LEVEL = ITERATION_LEVEL,
            lr = LEARNING_RATE,
            device = DEVICE
        )
    gnn.init(LOAD_PATH, LOG_PATH)

    auc0, fpr0, tpr0, thres0 = get_auc_epoch_batch(gnn, Gs_train, classes_train,
            BATCH_SIZE)
    gnn.say("Initial training auc = {0} @ {1}".format(auc0, datetime.now()))

    print(auc0)
    print(max((1-fpr0+tpr0)/2))
    index = np.argmax((1-fpr0+tpr0)/2)
    print("index:", index)
    print("fpr", fpr0[index])
    print("tpr", tpr0[index])
    print(thres0[index])

    auc1, fpr1, tpr1, thres1 = get_auc_epoch_batch(gnn, Gs_test, classes_test,
            BATCH_SIZE)
    gnn.say("Initial testing auc = {0} @ {1}".format(auc1, datetime.now()))

    print(auc1)
    print(max((1-fpr1+tpr1)/2))
    index = np.argmax(1-fpr1+tpr1)
    print("index:", index)
    print("fpr", fpr1[index])
    print("tpr", tpr1[index])
    print(thres1[index])

    auc2, fpr2, tpr2, thres2 = get_auc_epoch_batch(gnn, Gs_valid, classes_valid,
            BATCH_SIZE)
    gnn.say("Initial validation auc = {0} @ {1}".format(auc2, datetime.now()))

    print(auc2)
    print(max((1-fpr2+tpr2)/2))
    index = np.argmax((1-fpr2+tpr2)/2)
    print("index:", index)
    print("fpr", fpr2[index])
    print("tpr", tpr2[index])
    print(thres2[index])

    plt.figure()
    plt.title('ROC CURVE')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.plot(fpr1,tpr1,color='b')
    plt.plot(fpr1, 1-fpr1+tpr1, color='b')
    plt.plot(fpr2, tpr2,color='r')
    plt.plot(fpr2, 1-fpr2+tpr2, color='r')
#     plt.plot([0, 1], [0, 1], color='m', linestyle='--')
    plt.savefig('auc_depth5.png')


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

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd

import requests

# For storing png files in memory
import io

# For generating GIF
import imageio

###########################################################
########## Globals....
###########################################################
# Top value to use in scale, 0 = mean + 2 std devs
max_value = 0

# Bottom value to use in scale, should be zero
min_value = 0

# SANDAG API Link
sd_api = "https://opendata.arcgis.com/datasets/854d7e48e3dc451aa93b9daf82789089_0.geojson"

# Zipcode shape file link
zip_shape_full = "https://github.com/mulroony/State-zip-code-GeoJSON/raw/master/ca_california_zip_codes_geo.min.json"

# File to write gif to. Leave blank to just render inline
# Probably won't work in this script...
gif_path = "/tmp/SD_Covid_cases.gif"
#gif_path = ""

# Select ColorMap: https://matplotlib.org/3.2.1/tutorials/colors/colormaps.html
color_map = 'YlOrRd'
###########################################################
###########################################################

r = requests.get(sd_api)
rd =  [ _r['properties'] for _r in r.json()['features'] ]

case_df = pd.DataFrame(rd)

# Cleanup, reduce mem
del [r, rd]

known_zips = list(case_df['ziptext'].unique())
print("Zipcodes in data: %s"%(len(known_zips)))

# Got API link from: https://sdgis-sandag.opendata.arcgis.com/datasets/covid-19-statistics-by-zip-code

r = requests.get(zip_shape_full)
rd = r.json()

zip_shape_known = {}
zip_shape_known['type'] = rd['type']
zip_shape_known['features'] = []

for i in rd['features']:
    if i['properties']['ZCTA5CE10'] in known_zips:
        zip_shape_known['features'].append(i)

print("Found %s matching zip codes in shape file"%(len(zip_shape_known['features'])))

del [r, rd]

gdf = gpd.GeoDataFrame.from_features(zip_shape_known)
gdf.rename(columns={'ZCTA5CE10': 'zipcode'}, inplace=True)
gdf.set_index('zipcode',inplace=True)

# Drop time from date, not useful
case_df['date'] = case_df['updatedate'].apply(lambda x: x.split(" ")[0])

# Drop unused fields
case_df.drop(inplace=True, columns=['FID',
                                    'zipcode_zip',
                                    'created_date',
                                    'updatedate',
                                    'created_date',
                                    'created_user',
                                    'last_edited_date',
                                    'last_edited_user',
                                    'globalid'])

# Missing data becomes zeros
case_df.fillna(0, inplace=True)

# Rename column
case_df.rename(columns={'ziptext': 'zipcode'}, inplace=True)

# Drop duplicates, have seen some
case_df.drop_duplicates(subset=['zipcode','date'], inplace=True)

# Ugly, but creates table I want
case_df = case_df.groupby(['zipcode', 'date']).sum().unstack().fillna(0)

# End up with nested column name, remove it
case_df.columns = case_df.columns.droplevel()

# Create super list of all case values so we can get stats if we are going to use it
if max_value == 0:
    _tmp_case_list = []

# Not necessary, but can't hurt
dates = sorted(case_df.columns.values)

# subtract tomorrow from today, and set that as the value for today. repeat, skipping last day...
for i in range(len(dates)-1):
    today = dates[i]
    tomorrow = dates[i+1]

    case_df[today] = case_df[tomorrow] - case_df[today]

#     #Uncomment to find all negative values. Happens due to adjusting the numbers, we handle it
#     #Good to do though
#     _tmp_df = case_df[today].apply(lambda x: x if x < -1 else None).dropna()
#     if _tmp_df.values.size > 0:
#         print("%s"%(today))
#         print(_tmp_df)

    if max_value == 0:
        _tmp_case_list += list(case_df[today].values)

if max_value == 0:
    _tmp_case_df = pd.DataFrame(_tmp_case_list)
    max_value = int(_tmp_case_df.mean()[0] + (2 * _tmp_case_df.std()[0]))

print("max_value = %s"%(max_value))

# Limit values based on max / min
for i in dates[:-1]:
    case_df[i] = case_df[i].apply(lambda x: min_value if x < min_value else x)
    case_df[i] = case_df[i].apply(lambda x: max_value if x > max_value else x)

# Remove last day
case_df.drop(inplace=True, columns=[case_df.columns[-1]])

# ## Merge shape file with zipcodes gdf and case_df, create case_gdf

case_gdf = gdf.merge(case_df, left_on='zipcode', right_on='zipcode')

output_files = []

for idx in dates[:-1]:

    # Create inmemory file
    output_files.append(io.BytesIO())

    fig = case_gdf.plot(cmap=color_map,
             column=idx,
             linewidth=0.8,
             edgecolor='0.8',
             vmax=int(max_value),
             vmin=min_value,
             legend=True,
             figsize=(14,8))

    fig.axis('off')

    fig.annotate("Daily Cases COVID-19 : %s - %s"%(dates[0],dates[-2]),
                xy=(.1, .9), xycoords='figure fraction',
                horizontalalignment='left', verticalalignment='top',
                fontsize=20)
    fig.annotate("""P. Mulrooney <mulroony@gmail.com>
* Upper case count limited to mean + 2 std devs
* Missing replaced with zeros
* Decreases between days set to zero
* https://sdgis-sandag.opendata.arcgis.com/datasets/covid-19-statistics-by-zip-code""",
                xy=(.5, .1), xycoords='figure fraction',
                horizontalalignment='left', verticalalignment='top',
                fontsize=8)

    fig.annotate(idx,
                xy=(0.1, .1), xycoords='figure fraction',
                horizontalalignment='left', verticalalignment='top',
                fontsize=20)

    fig.annotate(idx,
                xy=(0.1, .1), xycoords='figure fraction',
                horizontalalignment='left', verticalalignment='top',
                fontsize=20)

    chart = fig.get_figure()
    chart.savefig(output_files[-1], dpi=150)
    plt.close('all')

    output_files[-1].seek(0)

print("Generated %s in memory PNG files\n"%(len(output_files)))

images = []
for output_file in output_files:
    images.append(imageio.imread(output_file))

if not gif_path:
    gif_path = io.BytesIO()

imageio.mimsave(gif_path, images, format="gif", duration=0.75, loop=1)


"""A Mailman newsletter subscription interface.

To use this plugin, enable the newsletter module and set the newsletter module and name settings
in the admin settings page.
"""

from django.utils.translation import ugettext as _
from Mailman import MailList, Errors
from models import Subscription
from livesettings import config_value
import logging
import sys

log = logging.getLogger('newsletter.mailman')

class UserDesc: pass

def is_subscribed(contact):
    return Subscription.email_is_subscribed(contact.email)

def update_contact(contact, subscribe, attributes={}):
    email = contact.email
    current = Subscription.email_is_subscribed(email)
    attributesChanged = False
    sub = None

    if attributes:
        sub, created = Subscription.objects.get_or_create(email=email)
        if created:
            attributesChanged = True
        else:
            oldAttr = [(a.name,a.value) for a in sub.attributes.all()]
            oldAttr.sort()
        sub.update_attributes(attributes)
        newAttr = [(a.name,a.value) for a in sub.attributes.all()]
        newAttr.sort()

        if not created:
            attributesChanged = oldAttr != newAttr

    if current == subscribe:
        if subscribe:
            if attributesChanged:
                result = _("Updated subscription for %(email)s.")
            else:
                result = _("Already subscribed %(email)s.")
        else:
            result = _("Already removed %(email)s.")

    else:
        if not sub:
            sub, created = Subscription.objects.get_or_create(email=email)
        sub.subscribed = subscribe
        sub.save()

        if subscribe:
            mailman_add(contact)
            result = _("Subscribed: %(email)s")
        else:
            mailman_remove(contact)
            result = _("Unsubscribed: %(email)s")

    return result % { 'email' : email }

def mailman_add(contact, listname=None, send_welcome_msg=None, admin_notify=None):
    """Add a Satchmo contact to a mailman mailing list.

    Parameters:
        - `Contact`: A Satchmo Contact
        - `listname`: the Mailman listname, defaulting to whatever you have set in settings.NEWSLETTER_NAME
        - `send_welcome_msg`: True or False, defaulting to the list default
        - `admin_notify`: True of False, defaulting to the list default
    """
    mm, listname = _get_maillist(listname)
    print >> sys.stderr, 'mailman adding %s to %s' % (contact.email, listname)

    if send_welcome_msg is None:
        send_welcome_msg = mm.send_welcome_msg

    userdesc = UserDesc()
    userdesc.fullname = contact.full_name
    userdesc.address = contact.email
    userdesc.digest = False

    if mm.isMember(contact.email):
        print >> sys.stderr, _('Already Subscribed: %s' % contact.email)

    else:
        try:
            try:
                mm.Lock()
                mm.ApprovedAddMember(userdesc, send_welcome_msg, admin_notify)
                mm.Save()
                print >> sys.stderr, _('Subscribed: %(email)s') % { 'email' : contact.email }

            except Errors.MMAlreadyAMember:
                print >> sys.stderr, _('Already a member: %(email)s') % { 'email' : contact.email }

            except Errors.MMBadEmailError:
                if userdesc.address == '':
                    print >> sys.stderr, _('Bad/Invalid email address: blank line')
                else:
                    print >> sys.stderr, _('Bad/Invalid email address: %(email)s') % { 'email' : contact.email }

            except Errors.MMHostileAddress:
                print >> sys.stderr, _('Hostile address (illegal characters): %(email)s') % { 'email' : contact.email }

        finally:
            mm.Unlock()

def mailman_remove(contact, listname=None, userack=None, admin_notify=None):
    """Remove a Satchmo contact from a Mailman mailing list

    Parameters:
        - `contact`: A Satchmo contact
        - `listname`: the Mailman listname, defaulting to whatever you have set in settings.NEWSLETTER_NAME
        - `userack`: True or False, whether to notify the user, defaulting to the list default
        - `admin_notify`: True or False, defaulting to the list default
    """

    mm, listname = _get_maillist(listname)
    print >> sys.stderr, 'mailman removing %s from %s' % (contact.email, listname)

    if mm.isMember(contact.email):
        try:
            mm.Lock()
            mm.ApprovedDeleteMember(contact.email, 'satchmo_ext.newsletter',  admin_notify, userack)
            mm.Save()
        finally:
            mm.Unlock()

def _get_maillist(listname):
    try:
        if not listname:
            listname = config_value('NEWSLETTER', 'NEWSLETTER_NAME')

        if listname == "":
            log.warn("NEWSLETTER_NAME not set in store settings")
            raise NameError('No NEWSLETTER_NAME in settings')

        return MailList.MailList(listname, lock=0), listname

    except Errors.MMUnknownListError:
        print >> sys.stderr, "Can't find the MailMan newsletter: %s" % listname
        raise NameError('No such newsletter, "%s"' % listname)


from time import time

from flask import render_template, redirect, url_for, flash, make_response, current_app, request, abort
from flask.json import jsonify
from flask.ext.login import login_required, current_user
from etherpad_lite import EtherpadLiteClient

from . import main
from .forms import UserDefaultRoom
from .. import db, avatars, generator
from ..models import User, Room, RoomAuthorization, RoomPermissions, Widget, EventTypes, FileStorage

def get_available_rooms():
    pass

@main.route('/')
def index():
    return redirect(url_for('.home'))

@main.route('/home')
def home():
    if not current_user.is_authenticated():
        return redirect(url_for('auth.login'))

    if current_user.guest:
        return redirect(url_for('auth.logout'))

    rs = Room.query.order_by('componentId').all()

    form = UserDefaultRoom()

    rc = [('0', 'None')]
    rooms = []
    my_rooms = []
    for room in rs:
        if room.ownerId == current_user.id:
            my_rooms += [room]
            rc += [(str(room.id), room.name)]
        elif room.get_permissions(current_user):
            rooms += [room]
            rc += [(str(room.id), room.name)]

    form.selectRoom.choices = rc
    form.selectRoom.data = str(current_user.defaultRoomId)

    if current_app.config['IA_ENABLED']:
        return render_template('home_ia.html', my_rooms=my_rooms, rooms=rooms, User=User, form=form, avatar_url=avatar_url(current_user), current_user=current_user, current_app=current_app)
    else:
        return render_template('home.html', my_rooms=my_rooms, rooms=rooms, User=User, form=form, avatar_url=avatar_url(current_user), current_user=current_user, current_app=current_app)

@main.route("/set/room", methods=['POST'])
@login_required
def set_room():
    form = UserDefaultRoom()

    rs = Room.query.order_by('componentId').all()

    rc = [('0', 'None')]
    for room in rs:
        if room.ownerId == current_user.id:
            rc += [(str(room.id), room.name)]
        elif room.get_permissions(current_user):
            rc += [(str(room.id), room.name)]

    form.selectRoom.choices = rc

    if form.validate_on_submit():
        if form.selectRoom.data == '0':
            current_user.defaultRoomId = None
        else:
            current_user.defaultRoomId = form.selectRoom.data

        db.session.commit()
        return redirect(url_for('.home'))
    else:
        for form_error in form.errors:
            for field_error in form[form_error].errors:
                flash(form[form_error].label.text+" - "+field_error, 'error')

    return redirect(url_for('.home'))

@main.route('/lobby')
def lobby():
    if not current_user.is_authenticated():
        return redirect(url_for('auth.login'))

    if current_user.guest:
        return redirect(url_for('auth.logout'))

    rs = Room.query.all()

    rooms = []
    for room in rs:
        if get_room_permissions(room, current_user):
            rooms += [room]

    return render_template('lobby.html', rooms=rooms, User=User)

@main.route('/room/<roomId>')
def room(roomId):
    room = Room.query.filter_by(id=roomId).first()

    if not current_user.is_authenticated():
        if room.guest_permission():
            return redirect(url_for('auth.guest_user', roomId=roomId))

        return redirect(url_for('auth.login', next=url_for('.room', roomId=1)))

    cl = room.events.filter_by(type=EventTypes.ROOM_CHAT).order_by('datetime').limit(20)

    return render_template(room.component.template, room=room, chat=cl, current_user=current_user, current_app=current_app)

@login_required
@main.route('/upload_avatar', methods=['POST'])
def upload_avatar():
    if 'avatar' in request.files:
        filename = avatars.save(request.files['avatar'])
        url = avatars.url(filename)
        file = FileStorage(type='avatar', filename=current_user.email, url=url)
        db.session.add(file)
        db.session.commit()

    return redirect(url_for('.home'))

def avatar_url(user):
    file = FileStorage.query.filter_by(type='avatar', filename=current_user.email).first()
    if file:
        return file.url
    else:
        return url_for('.identicon', user_id=user.id)

@login_required
@main.route('/avatar/<user_id>')
def identicon(user_id):
    user = User.query.filter_by(id=user_id).first()
    identicon = generator.generate(user.email, 100, 100, output_format='png')
    response = make_response(identicon)
    response.headers["Content-Type"] = "image/png"
    response.headers["Cache-Control"] = "public, max-age=43200"
    return response


#!/usr/bin/env python3
import ast
import astor

expr_l = """
for e in g.Edges():
    e.dst["sum1"] += e.data123
for e in g.Edges():
    e["data1"] = e.data2 / e.dst.sum1
"""
expr_l_ast = ast.parse(expr_l)
print(ast.dump(expr_l_ast))

def is_call_edges(call):
    if isinstance(call, ast.Call):
        f_func = call.func
        if isinstance(f_func, ast.Attribute) and isinstance(
            f_func.value, ast.Name
        ):
            f_object = f_func.value.id
            f_method = f_func.attr
            if f_object == "g" and f_method == "Edges":
                return True

    return False

def dst_node_var(n, e):
    if (
        isinstance(n, ast.Subscript)
        and isinstance(n.value, ast.Attribute)
        and n.value.value.id == e
        and n.value.attr == "dst"
    ):
        return n.slice.value
    if (
        isinstance(n, ast.Attribute)
        and isinstance(n.value, ast.Attribute)
        and n.value.value.id == e
        and n.value.attr == "dst"
    ):
        return n.attr
    else:
        return None

global_vars = {}

for node in ast.walk(expr_l_ast):
    if (
        isinstance(node, ast.For)
        and is_call_edges(node.iter)
        and isinstance(node.target, ast.Name)
    ):
        edge_var = node.target.id
        if len(node.body) == 1:
            body = node.body[0]
            if isinstance(body, ast.AugAssign) and isinstance(
                body.target, ast.Subscript
            ):
                var_name = dst_node_var(body.target, edge_var)
                if var_name is not None:
                    if (
                        isinstance(body.value, ast.Attribute)
                        and body.value.value.id == edge_var
                        and body.value.attr != var_name
                    ):
                        global_vars[var_name] = [node]
                        body.target = ast.Name(id="n_var")
            elif (
                isinstance(body, ast.Assign)
                and len(body.targets) == 1
                and isinstance(body.targets[0], ast.Subscript)
            ):
                assign_var = dst_node_var(body.targets[0], edge_var)
                if assign_var not in global_vars:
                    if (
                        isinstance(body.value, ast.BinOp)
                        and isinstance(body.value.left, ast.Attribute)
                        and isinstance(body.value.right, ast.Attribute)
                    ):
                        var_left = dst_node_var(body.value.left, edge_var)
                        var_right = dst_node_var(body.value.right, edge_var)
                        if var_left in global_vars:
                            global_vars[var_left].append(node)
                            body.value.left = ast.Name(id="n_var")
                        elif var_right in global_vars:
                            global_vars[var_right].append(node)
                            body.value.right = ast.Name(id="n_var")

for var, nodes in global_vars.items():
    if len(nodes) == 2:
        re_ast = ast.For(
            target=ast.Name(id="n"),
            iter=ast.Call(
                func=ast.Attribute(value=ast.Name(id="g"), attr="Nodes"),
                args=[],
                keywords=[],
            ),
            body=[
                ast.Assign(
                    targets=[ast.Name(id="n_var")],
                    value=ast.Constant(value=0.0),
                ),
                ast.For(
                    target=ast.Name(id="e"),
                    iter=ast.Call(
                        func=ast.Attribute(
                            value=ast.Name(id="n"), attr="incoming_edges"
                        ),
                        args=[],
                        keywords=[],
                    ),
                    body=nodes[0].body,
                    orelse=[],
                ),
                ast.For(
                    target=ast.Name(id="e"),
                    iter=ast.Call(
                        func=ast.Attribute(
                            value=ast.Name(id="n"), attr="incoming_edges"
                        ),
                        args=[],
                        keywords=[],
                    ),
                    body=nodes[1].body,
                    orelse=[],
                ),
            ],
            orelse=[],
        )
        expr_l_ast.body.append(re_ast)
        expr_l_ast.body.remove(nodes[0])
        expr_l_ast.body.remove(nodes[1])

print(astor.to_source(expr_l_ast))


import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

class LinearDynamicSystem:
    def __init__(self, gamma, sigma):
        self.gamma = gamma
        self.sigma = sigma

        self.log_p_x_history = []
        self.pred_mu_history = []
        self.mu_history = []
        self.pred_mu_history = []
        self.p_history = []
        self.pred_p_history = []

    def fit(self, observations, mu, p):
        self._filter(observations, mu, p)
        smoothed_mu_list = self._smoothing()

        plt.plot(observations, label="Observation")
        plt.plot(self.pred_mu_history, label="Predicted Latent Variable")
        plt.plot(self.mu_history, label="Updated Latent Variable")
        plt.plot(smoothed_mu_list, label="Smoothed Latent Variable")
        plt.legend()
        plt.show()

    def _filter(self, observations, mu, p):
        _mu, _p = mu, p
        for i, obs in enumerate(observations):
            pred_mu, pred_p, log_p_x = self._predict(_mu, _p, obs)
            self.log_p_x_history.append(log_p_x)
            _mu, _p = self._update(obs, pred_mu, pred_p)

            self.pred_mu_history.append(pred_mu)
            self.pred_p_history.append(pred_p)

            self.mu_history.append(_mu)
            self.p_history.append(_p)

            print("Answer {}: ".format(i+1))

            if i > 0:
                print("predicted p.d.f. of latent variable: mu = {:.3f}, sigma = {:.3f}, joint_prob. = {:.3f}"
                      .format(pred_mu, pred_p, np.sum(self.log_p_x_history)))

            print("log-scaled likelihood = {:.3f}, "
                  "updated p.d.f. of latent value: mu = {:.3f}, sigma = {:.3f}"
                  .format(log_p_x, _mu, _p))

    def _predict(self, mu, p, obs):
        pred_mu = mu
        pred_p = p + self.gamma
        log_p_x = norm.logpdf(x=obs, loc=pred_mu, scale=(pred_p + self.sigma))
        return pred_mu, pred_p, log_p_x

    def _kalman_gain(self, p, s):
        return p / (p + s)

    def _update(self, obs, mu, p):
        k = self._kalman_gain(p, self.sigma)
        new_mu = mu + k * (obs - mu)
        new_p = (1 - k) * p
        return new_mu, new_p

    def _smoothing(self):
        smoothed_mu_list = []
        smoothed_p_list = []

        last_smoothed_mu = self.mu_history[-1]
        last_smoothed_p = self.p_history[-1]

        smoothed_mu_list.append(last_smoothed_mu)
        smoothed_p_list.append(last_smoothed_p)

        for i in reversed(range(len(self.mu_history)-1)):
            current_mu = self.mu_history[i]
            pred_mu = self.pred_mu_history[i+1]

            current_p = self.p_history[i]
            pred_p = self.pred_p_history[i+1]

            j = current_p / pred_p

            last_smoothed_mu = current_mu + j * (last_smoothed_mu - pred_mu)
            last_smoothed_p = current_p + j ** 2 * (last_smoothed_p - pred_p)

            smoothed_mu_list.insert(0, last_smoothed_mu)
            smoothed_p_list.insert(0, last_smoothed_p)

        for mu, p in zip(smoothed_mu_list, smoothed_p_list):
            print("smoothed μ = {:.3f}, smoothed P = {:.3f}".format(mu, p))

        return smoothed_mu_list

def generate_observation(b):
    return 10 * b + 20

STUDENT_ID = "2011039"
print("Your ID is {}".format(STUDENT_ID))

b_1, b_2, b_3, b_4 = [int(sid) for sid in STUDENT_ID[-4:]]
print("b_1 = {}, b_2 = {}, b_3 = {}, b_4 = {}".format(b_1, b_2, b_3, b_4))

INPUT_X = np.array([generate_observation(b) for b in [b_1, b_2, b_3, b_4]])
INIT_P = 50
INIT_MU = 200
INIT_GAMMA = 20
INIT_SIGMA = 10
print("initialized value...: X = {}, P_0 = {}, μ_0 = {}, Γ = {}, Σ = {}"
      .format(INPUT_X, INIT_P, INIT_MU, INIT_GAMMA, INIT_SIGMA))

lds_model = LinearDynamicSystem(INIT_GAMMA, INIT_SIGMA)
lds_model.fit(INPUT_X, INIT_MU, INIT_P)


import numpy as np
import os
from PIL import Image
from PIL import ImageFont, ImageDraw
import time
import core.utils as utils
import core.v_detection as detect
import cv2
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-i', type=str, default="./1.mp4", help="input video")
parser.add_argument('-o', type=str, default="./output.avi", help="input video")
args = parser.parse_args()
#videos
if __name__ == "__main__":
    input = args.i
    output = args.o
    cap = cv2.VideoCapture(input)
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(args.o, cv2.VideoWriter_fourcc(
        'M', 'J', 'P', 'G'), 25, (frame_width, frame_height))

    while(True):
        ret, image = cap.read()
        if ret == True:
            start = time.time()
            bboxes = detect.vehicle_detection(image)
            # print("bboxes: ", bboxes)
            image = utils.draw_bbox(image, bboxes)
            print("processing time: ", time.time() - start)
            out.write(image)
    cap.release()
    out.release()


import tensorflow as tf
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Dense, Activation, Flatten

# import tensorflow as tf
# tf.python.control_flow_ops = tf  # some hack to get tf running with Dropout

# 224x224
def alex_net_keras(x, num_classes=2, keep_prob=0.5):
    x = Conv2D(92, kernel_size=(11, 11), strides=(4, 4), padding='same')(x)  # conv 1
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    # LRN is missing here - Caffe.
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)  # pool 1

    x = Conv2D(256, kernel_size=(5, 5), padding='same')(x)  # miss group and pad param # conv 2
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x)  # pool 2

    x = Conv2D(384, kernel_size=(3, 3), padding='same')(x)  # conv 3
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    # x = MaxPooling2D(pool_size=(3, 3))(x)

    x = Conv2D(384, kernel_size=(3, 3), padding='same')(x)  # conv 4
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    # x = MaxPooling2D(pool_size=(3, 3))(x)

    x = Conv2D(256, kernel_size=(3, 3), padding='same')(x)  # conv 5
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Flatten()(x)
    x = Dense(4096, kernel_initializer='normal')(x)  # fc6
    # dropout 0.5
#    x = tf.nn.dropout(x, keep_prob=keep_prob)
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(4096, kernel_initializer='normal')(x)  # fc7
    # dropout 0.5
#暂时没有过拟合，暂时不用
#    x = tf.nn.dropout(x, keep_prob=keep_prob)
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(num_classes)(x)
    # x = BatchNormalization()(x)
    # x = Activation('softmax')(x)
    return x


from alarmageddon.publishing.hipchat import HipChatPublisher
from alarmageddon.result import Failure
from alarmageddon.result import Success
from alarmageddon.publishing.exceptions import PublishFailure
from alarmageddon.validations.validation import Validation, Priority
import pytest

#Successes aren't sent, so monkeypatch out post and then
#only failures should notice
@pytest.fixture
def no_post(monkeypatch):
    monkeypatch.delattr("requests.post")

def new_publisher():
    return HipChatPublisher(
        api_end_point="fakeurl",
        api_token="faketoken",
        environment="UnitTest",
        room_name="Alarmageddon")

def test_requires_api_end_point():
    with pytest.raises(ValueError):
        HipChatPublisher(api_end_point="",
                         api_token="faketoken",
                         environment="UnitTest",
                         room_name="Alarmageddon")

def test_requires_api_token():
    with pytest.raises(ValueError):
        HipChatPublisher(api_end_point="fakeurl",
                         api_token="",
                         environment="UnitTest",
                         room_name="Alarmageddon")

def test_requires_environment():
    with pytest.raises(ValueError):
        HipChatPublisher(api_end_point="fakeurl",
                         api_token="token",
                         environment="",
                         room_name="Alarmageddon")

def test_requires_room():
    with pytest.raises(ValueError):
        HipChatPublisher(api_end_point="fakeurl",
                         api_token="token",
                         environment="UnitTest",
                         room_name="")

def test_repr():
    hipchat = new_publisher()
    hipchat.__repr__()

def test_str():
    hipchat = new_publisher()
    str(hipchat)

def testSendSuccess(no_post, monkeypatch):
    hipchat = new_publisher()
    v = Validation("low", priority=Priority.LOW)
    success = Success("bar", v)
    hipchat.send(success)

def testSendFailure(no_post, monkeypatch):
    hipchat = new_publisher()
    v = Validation("low", priority=Priority.LOW)
    failure = Failure("foo", v, "unable to frobnicate")
    with pytest.raises(AttributeError):
        hipchat.send(failure)

def test_publish_failure(httpserver):
    httpserver.serve_content(code=300, headers={"content-type": "text/plain"},
                             content='{"mode":"NORMAL"}')
    pub = HipChatPublisher(httpserver.url, "token", "env", "room")
    v = Validation("low", priority=Priority.CRITICAL)
    failure = Failure("bar", v, "message")
    with pytest.raises(PublishFailure):
        pub.send(failure)


#=============================================================================================================#
#  HOW TO RUN?                                                                                #
#  python3 chop_segments.py                                                                                #
#  Please provide LABEL, SEGMENT_LENGTH, OUTPUT_PATH, INPUT_FILE_TO_CHOP.                                     #
#=============================================================================================================#
import csv
import sys
import nltk
from nltk import tokenize
#=============================================================================================================#
LABEL = 1
SEGMENT_LENGTH = 100
OUTPUT_PATH = "APPROPRIATE.CSV"
INPUT_FILE_TO_CHOP = "SAMPLE_INPUT_FILE.txt"
#=============================================================================================================#
#When we run the script for appropriate channels, we set the LABEL as 1 else we set it as 0 for inappropriate #
#=============================================================================================================#
visited = set()
## To ensure there are no duplicate video IDs
s = ""
cur = ""
prev = []
flag = 0
identifier = 0

#=============================================================================================================#
f = open(INPUT_FILE_TO_CHOP)
with open(OUTPUT_PATH, 'w') as csvfile:
    fieldnames = ['','Unnamed: 0','Unnamed: 0.1','Transcript','ID','Label']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writerow({'': '', 'Unnamed: 0': 'Unnamed: 0', 'Unnamed: 0.1': 'Unnamed: 0.1', 'Transcript' : 'Transcript', 'ID' : 'VIDEO ID', 'Label' : 'Label'})
    for line in f:
        # Channel IDs are present in lines containing ||||
        if("||||" in line):
            continue
        # Video IDs are present in lines containing ####
        if("####" in line):
            cur = line.strip()
            if(flag == 1):
                identifier  +=1
                text = s.strip()
                groups = nltk.word_tokenize(text)
                n_split_groups = []
                while len(groups):
                    n_split_groups.append(' '.join(groups[:SEGMENT_LENGTH]))
                    groups = groups[SEGMENT_LENGTH:]
                for part in n_split_groups:
                    writer.writerow({'': identifier, 'Unnamed: 0': identifier, 'Unnamed: 0.1': identifier, 'Transcript' : part, 'ID' : prev[1], 'Label' : LABEL })
                print("PROCESSED VIDEO ID",str(prev[1]))
                flag = 0

        if("####" in line  and (line.strip() not in visited)):
            s = ""
            flag = 1
            visited.add(line.strip())
            prev = cur.split("####")
        if("####" not in line and flag == 1):
            s += line.strip() + " "
    if(flag == 1):
        identifier +=1
        text = s.strip()
        groups = nltk.word_tokenize(text)
        n_split_groups = []
        while len(groups):
            n_split_groups.append(' '.join(groups[:SEGMENT_LENGTH]))
            groups = groups[SEGMENT_LENGTH:]
        for part in n_split_groups:
            writer.writerow({'': identifier, 'Unnamed: 0': identifier, 'Unnamed: 0.1': identifier, 'Transcript' : part, 'ID' : prev[1], 'Label' : LABEL })
#=============================================================================================================#


import requests
from bs4 import BeautifulSoup
import csv
import os

URL = 'https://citaty.info/book/quotes'
HEADERS = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
           'accept': '*/*'}
FILE_CSV = 'quotations_csv.csv'

def get_html(url, params=None):
    r = requests.get(url, headers=HEADERS, params=params)
    return r

def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all('div', class_='node__content')

    quotations = []
    for item in items:

        topic_tags = item.find('div', class_='node__topics')
        if topic_tags:
            topic_tags = topic_tags.get_text(', ')
        else:
            topic_tags = 'No tags'

        quotations.append({
            'autor': item.find('a', title='Автор цитаты').get_text(),
            'book': item.find('a', title='Цитата из книги').get_text(),
            'text_quotation': item.find('div', class_='field-item even last').get_text().replace('\xa0', ' ').replace('\n', ' '),
            'topic_tags': topic_tags
        })
    print(*quotations, sep='\n')
    save_file_csv(quotations, FILE_CSV)
    os.startfile(FILE_CSV)

def save_file_csv (items, path):
    with open(path, 'w', newline='') as file:
        writer=csv.writer(file, delimiter=';')
        writer.writerow(['Autor', 'Book', 'Quotation', 'Tags'])
        for item in items:
            writer.writerow([item['autor'], item['book'], item['text_quotation'], item['topic_tags']])

def parse():
    html = get_html(URL)
    if html.status_code == 200:
        get_content(html.text)
    else:
        print('Error!')

parse()


# #
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

card = dbc.Card(
    [dbc.CardHeader("Header"), dbc.CardBody("Body", style={"height": 250})],
    className="h-100",
)

graph_card = dbc.Card([dbc.CardHeader("Here's a graph"), dbc.CardBody([dcc.Graph()])])

app.layout = dbc.Container(
    dbc.Row(
        [
            dbc.Col(
                [
                    dbc.CardDeck([card] * 4),
                    html.H2("Here is some important information to highlight..."),
                    dbc.CardDeck([graph_card] * 2),
                ],
                width=10,
            ),
            dbc.Col(card, width=2),
        ]
    ),
    fluid=True,
    className="m-3",
)

if __name__ == "__main__":
    app.run_server(debug=True)


import wx

import PropToolPanel
import CollisionToolPanel
import POIToolPanel

class ToolBrowser(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        self.notebook = wx.Notebook(self, -1)
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged, self.notebook)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.notebook, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.observers = []
        self.selectedTool = None
        self.selectedEditor = None

        self.collisionToolPanel = CollisionToolPanel.CollisionToolPanel(self.notebook, -1)
        #self.notebook.AddPage(self.collisionToolPanel, "Collision")
        self.notebook.AddPage(self.collisionToolPanel, "Col")

        self.poiToolPanel = POIToolPanel.POIToolPanel(self.notebook, -1)
        self.notebook.AddPage(self.poiToolPanel, "POI")

        self.propToolPanel = PropToolPanel.PropToolPanel(self.notebook, -1)
        self.notebook.AddPage(self.propToolPanel, "Props")

    def addObserver(self, observer): self.observers.append(observer)
    def removeObserver(self, observer): self.observers.remove(observer)

    def onPageChanged(self, event):
        if self.selectedEditor and self.selectedTool:
            self.selectedTool.detachFromEditor(self.selectedEditor)
        self.selectedTool = self.notebook.GetPage(self.notebook.GetSelection())
        if self.selectedEditor:
            self.selectedTool.attachToEditor(self.selectedEditor)
            self.selectedEditor.Refresh()

    def onEditorChanged(self, editor):
        if self.selectedEditor:
            self.selectedTool.detachFromEditor(self.selectedEditor)
        self.selectedEditor = editor
        if editor:
            self.selectedTool.attachToEditor(editor)

    def _notify(self):
        for observer in self.observers: observer(self)


import unittest
from pyunitreport import HTMLTestRunner
import parse_api_test_cases
import os
import time
from api_test_case import ApiTestCase
from parse_api_test_cases import TestCaseParser
import argparse
import logging

if __name__ == '__main__':
    # Argument Parse
    parser = argparse.ArgumentParser(
        description='API Test Assistant, Help you to do api test.')
    parser.add_argument(
        'test_suite_name', help="Please add your test suite's path"
    )
    args = parser.parse_args()
    test_suite_name = args.test_suite_name

    # create log file
    log_file__folder_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests/log/')
    if not os.path.exists(log_file__folder_path):
        os.makedirs(log_file__folder_path)
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    test_log_file_path = os.path.join(log_file__folder_path, str(test_suite_name)+time_str+'.log')
    logging.basicConfig(filename=test_log_file_path, level=logging.INFO,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filemode='w')

    logging.info("=========Start test execution!========")
    suite = unittest.TestSuite()
    tcp = TestCaseParser(test_suite_name)
    test_cases_list = tcp.get_test_case_list()
    logging.info("Got est cases list of {test_suite_name}".format(test_suite_name=test_suite_name))

    for test_case in test_cases_list:
        ApiTestCase.runTest.__doc__ = test_case['name']
        test = ApiTestCase(test_case)
        suite.addTest(test)
        logging.info("Added {tc_name} to test suite".format(tc_name=test_case['name']))

    test_report_folder_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests/reports/')
    if not os.path.exists(test_report_folder_path):
        os.makedirs(test_report_folder_path)
        logging.info("Report Folder Created at {path}".format(path=test_report_folder_path))
    else:
        logging.info("Report Folder already exists at {path}".format(path=test_report_folder_path))

    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    test_report_file_path = str(test_suite_name) + time_str
    kwargs = {
        "output": test_report_folder_path,
        "report_name": test_report_file_path
    }
    runner = HTMLTestRunner(**kwargs)
    runner.run(suite)
    logging.info("Test suite execution done!")


import os
import itertools
import scipy.io
import scipy.stats as stt
import numpy as np
import matplotlib.pyplot as plt
from mou_model import MOU

_RES_DIR = 'model_parameter/'
_I_REST_RUN = 0
_I_NBACK_RUN = 1
_I_NO_TIMESHIFT = 0
_I_ONE_TIMESHIFT = 1
_SUBJECT_AXIS = 0

plt.close('all')

## Create a local folder to store results.
if not os.path.exists(_RES_DIR):
    print('created directory:', _RES_DIR)
    os.makedirs(_RES_DIR)

## Read in data and define constants.
fMRI_data_and_labels = scipy.io.loadmat('data/DATA_TASK_3DMOV_HP_CSF_WD.mat')
regionalized_preprocessed_fMRI_data = fMRI_data_and_labels['TASKEC'][0][0]
roi_labels = fMRI_data_and_labels['ROIlbls'][0]
rest_run_data = regionalized_preprocessed_fMRI_data['Rest']
n_back_run_data = regionalized_preprocessed_fMRI_data['nBack']
flanker_run_data = regionalized_preprocessed_fMRI_data['Flanker']
m_rotation_run_data = regionalized_preprocessed_fMRI_data['mRotation']
odd_man_out_run_data = regionalized_preprocessed_fMRI_data['OddManOut']

n_subjects = rest_run_data.shape[2]
n_runs = len(fMRI_data_and_labels['TASKEC'][0][0])
n_rois = rest_run_data.shape[1]
n_ts_samples = rest_run_data.shape[0]
# Structure data to match the format used at
# https://github.com/mb-BCA/notebooks_review2019/blob/master/1_MOUEC_Estimation.ipynb
# to enhance comparability.
filtered_ts_emp = np.zeros([n_subjects, n_runs, n_rois, n_ts_samples])
run = list(regionalized_preprocessed_fMRI_data.dtype.fields.keys())
for k in range(len(run)):
    filtered_ts_emp[:, k, :, :] = np.transpose(
        regionalized_preprocessed_fMRI_data[run[k]], (2, 1, 0))

## Calculate functional connectivity (BOLD covariances) [Q0 und Q1].
# time_shift = np.arange(4, dtype=float) # for autocovariance plots
time_shift = np.arange(2, dtype=float)
n_shifts = len(time_shift)

FC_emp = np.zeros([n_subjects, n_runs, n_shifts, n_rois, n_rois])
n_ts_span = n_ts_samples - n_shifts + 1
for i_subject in range(n_subjects):
    for i_run in range(n_runs):
        # Center the time series (around zero).
        filtered_ts_emp[i_subject, i_run, :, :] -=  \
                np.outer(filtered_ts_emp[i_subject, i_run, :, :].mean(1),
                         np.ones([n_ts_samples]))
        # Calculate covariances with various time shifts.
        for i_shift in range(n_shifts):
            FC_emp[i_subject, i_run, i_shift, :, :] = \
                np.tensordot(filtered_ts_emp[i_subject, i_run, :,
                                             0:n_ts_span],
                             filtered_ts_emp[i_subject, i_run, :,
                                             i_shift:n_ts_span+i_shift],
                             axes=(1, 1)) / float(n_ts_span - 1)

rescale_FC_factor = (0.5 / FC_emp[:, _I_REST_RUN, _I_NO_TIMESHIFT, :,
                                  :].diagonal(axis1=1, axis2=2).mean())
FC_emp *= rescale_FC_factor
# filtered_ts_emp /= np.sqrt(0.135) # Rescale to get the same order of magnitude for locale variability as in paper.

print('most of the FC values should be between 0 and 1')
print('mean FC0 value:', FC_emp[:, :, _I_NO_TIMESHIFT, :, :].mean(),
      FC_emp.mean())
print('max FC0 value:', FC_emp[:, :, _I_NO_TIMESHIFT, :, :].max())
print('mean BOLD variance (diagonal of each FC0 matrix):',
      FC_emp[:, :, _I_NO_TIMESHIFT, :, :].diagonal(axis1=2, axis2=3).mean())
print('rescaleFactor: ' + str(rescale_FC_factor))
# Show distibution of FC0 values.
plt.figure()
plt.hist(FC_emp[:, :, _I_NO_TIMESHIFT, :, :].flatten(),
         bins=np.linspace(-1, 5, 30))
plt.xlabel('FC0 value', fontsize=14)
plt.ylabel('matrix element count', fontsize=14)
plt.title('distribution of FC0 values')
# Show FC0 averaged over subjects first run (rest).
plt.figure()
FC_avg_over_subj = FC_emp[:, _I_REST_RUN,
                          _I_NO_TIMESHIFT, :, :].mean(axis=_SUBJECT_AXIS)
plt.imshow(FC_avg_over_subj, origin='lower', cmap='Blues', vmax=1, vmin=0)
plt.colorbar()
plt.xlabel('target ROI', fontsize=14)
plt.ylabel('source ROI', fontsize=14)
plt.title('FC0 (functional connectivity with no time lag)')
# Show FC1 averaged over subjects first run (rest).
plt.figure()
FC_avg_over_subj = FC_emp[:, _I_REST_RUN,
                          _I_ONE_TIMESHIFT, :, :].mean(axis=_SUBJECT_AXIS)
plt.imshow(FC_avg_over_subj, origin='lower', cmap='Blues', vmax=1, vmin=0)
plt.colorbar()
plt.xlabel('target ROI', fontsize=14)
plt.ylabel('source ROI', fontsize=14)
plt.title('FC1 (functional connectivity with time lag 1TR)')
# Show the autocovariance for the first run (rest).
ac = FC_emp.diagonal(axis1=3, axis2=4)
plt.figure()
ac_avg_over_subj = np.log(np.maximum(ac[:, _I_REST_RUN, :, :].
                                     mean(axis=_SUBJECT_AXIS), np.exp(-4.0)))
plt.plot(range(n_shifts), ac_avg_over_subj)
plt.xlabel('time lag', fontsize=14)
plt.ylabel('log autocovariance', fontsize=14)
plt.title('rest', fontsize=16)
plt.ylim((-3, 0))
plt.xlim((0, 3))
# Show the autocovariance for the 2nd run (nBack).
plt.figure()
ac_avg_over_subj = np.log(np.maximum(ac[:, _I_NBACK_RUN, :, :].
                                     mean(axis=_SUBJECT_AXIS), np.exp(-3.1)))
plt.plot(range(n_shifts), ac_avg_over_subj)
plt.xlabel('time lag', fontsize=14)
plt.ylabel('log autocovariance', fontsize=14)
plt.title('nBack', fontsize=16)
plt.ylim((-3, 0))
plt.xlim((0, 3))

## Include structural connectivity.
# Load the binary structural connectivity matrix.
mask_EC = np.array(scipy.io.loadmat('data/BINARY_EC_MASK.mat')
                   ['grouped_umcu50_60percent'], dtype=bool)
# Enforce hermispheric connections.
for i in range(int(n_rois/2)):
    mask_EC[i, int(n_rois/2)+i] = True
    mask_EC[int(n_rois/2)+i, i] = True
# Visualise the structural connectivity mask.
plt.figure()
plt.imshow(mask_EC, origin='lower')
plt.xlabel('target ROI', fontsize=14)
plt.ylabel('source ROI', fontsize=14)
plt.title('Mask for existing connections', fontsize=12)

## Calculate EC-matrix.
# Construct diagonal mask for input noise matrix
# (here, no input cross-correlation).
mask_Sigma = np.eye(n_rois, dtype=bool)
# Run the model optimization.
# Initialize the source arrays.

# Jacobian (off-diagonal elements = EC)
J_mod = np.zeros([n_subjects, n_runs, n_rois, n_rois])
# Local variance (input covariance matrix, chosen to be diagonal)
Sigma_mod = np.zeros([n_subjects, n_runs, n_rois, n_rois])
# Model error
dist_mod = np.zeros([n_subjects, n_runs])
# Approximation of variance about the fitted data (FC covariance matrices)
R2_mod = np.zeros([n_subjects, n_runs])
# Between-region EC matrix
C_mod = np.zeros([n_subjects, n_runs, n_rois, n_rois])

mou_model = MOU()

for i_subject in range(n_subjects):
    for i_run in range(n_runs):
        # Run the estimation of model parameters, for all sessions.
        # All parameters/restrictions not explicitly passed, have the
        # correct defaults in fit_LO@MOU.
        mou_model.fit(filtered_ts_emp[i_subject, i_run, :, :].T,
                      mask_Sigma=mask_Sigma, mask_C=mask_EC)
        # Organize the optimization results into arrays.
        # Extract Jacobian of the model.
        J_mod[i_subject, i_run, :, :] = mou_model.J
        # Extract noise (auto-)covariance matrix.
        Sigma_mod[i_subject, i_run, :, :] = mou_model.Sigma
        # Extract the matrix distance between the empirical objective
        # covariances and their model counterparts
        # (normalized for each objective matrix).
        dist_mod[i_subject, i_run] = mou_model.d_fit['distance']
        # The squared Pearson correlation is taken as an approximation
        # of the variance.
        R2_mod[i_subject, i_run] = mou_model.d_fit['correlation']**2
        # The between-region EC matrix of the model
        C_mod[i_subject, i_run, :, :] = mou_model.get_C()

        print('sub / run:', i_subject, i_run, ';\t model error, R2:',
              dist_mod[i_subject, i_run], R2_mod[i_subject, i_run])
# Store the results in files.
np.save(_RES_DIR + 'FC_emp.npy',
        FC_emp)  # Empirical spatiotemporal FC
np.save(_RES_DIR + 'mask_EC.npy',
        mask_EC)  # Mask of optimized connections
np.save(_RES_DIR + 'mask_Sigma.npy',
        mask_Sigma)  # Mask of optimized Sigma elements
np.save(_RES_DIR + 'Sigma_mod.npy',
        Sigma_mod)  # Estimated Sigma matrices
np.save(_RES_DIR + 'dist_mod.npy',
        dist_mod)  # Model error
np.save(_RES_DIR + 'J_mod.npy',
        J_mod)  # Estimated Jacobian, EC + inverse time const. on diag.
print('\nFinished.')

# Plot C-matrix for resting state data.
plt.figure()
plt.imshow(C_mod[:, _I_REST_RUN, :, :].mean(axis=_SUBJECT_AXIS),
           origin='lower', cmap='Reds')
plt.colorbar()
plt.xlabel('target ROI', fontsize=14)
plt.ylabel('source ROI', fontsize=14)
plt.title('effective connectivity C_{ij}')
plt.show()

## Calculate local variability for rich club and periphery.
mean_rc_var = np.zeros([n_runs])
mean_periph_var = np.zeros([n_runs])
conf_int_rc = np.zeros([n_runs, 2])
conf_int_periph = np.zeros([n_runs, 2])
# Create a 1D-mask for rich club regions.
mask_rc = np.zeros(n_rois, dtype=bool)
indexes_rich_club = [23, 26, 27, 57, 60, 61]
mask_rc[indexes_rich_club] = True
print('rich club regions: '
      + str(np.concatenate(roi_labels[indexes_rich_club]).tolist()))

for i_run in range(n_runs):
    local_var = Sigma_mod[:, i_run, :, :].diagonal(axis1=1, axis2=2)
    rc_var = local_var[:, mask_rc].mean(axis=1)
    periph_var = local_var[:,  ~mask_rc].mean(axis=1)
    mean_rc_var[i_run] = rc_var.mean()
    mean_periph_var[i_run] = periph_var.mean()
    sigma_rc_var = rc_var.std(ddof=1)
    sigma_periph_var = periph_var.std(ddof=1)
    conf_int_rc[i_run, :] = stt.norm.interval(0.95,
                                              loc=mean_rc_var[i_run],
                                              scale=sigma_rc_var)
    conf_int_periph[i_run, :] = stt.norm.interval(0.95,
                                                  loc=mean_periph_var[i_run],
                                                  scale=sigma_periph_var)
print('Mittel der lokalen Variabilität (rich club): ' + str(mean_rc_var))
print('Mittel der lokalen Variabilität (periphery): ' + str(mean_periph_var))
print('95% Konfidenz Interval (rich cluc): ' + str(conf_int_rc))
print('95% Konfidenz Interval (periphery): ' + str(conf_int_periph))

## Calculate the input-output ratio.
# Create a 2D-mask for rich club regions.
mask_inter_rc = np.zeros([n_rois, n_rois], dtype=bool)
# The entries on the diagonal of C are 0 anyway, so that they can be
# ignored when it comes to the mask:
# mask_inter_rc[indexes_rich_club, indexes_rich_club] = True
rc_ind_combin = np.array(list(itertools.permutations(indexes_rich_club, 2))).T
mask_inter_rc[rc_ind_combin[0], rc_ind_combin[1]] = True

mean_rc_io = np.zeros([n_runs])
mean_periph_io = np.zeros([n_runs])
for i_run in range(n_runs):
    # Examine input-output ratio ignoring inter-rich-club connections.
    no_rc_connections_C = C_mod[:, i_run, :, :]
    no_rc_connections_C[:, mask_inter_rc] = 0
    roi_input = no_rc_connections_C[:, :, :].sum(axis=1)
    roi_output = no_rc_connections_C[:, :, :].sum(axis=2)
    io_rc = (roi_input[:, mask_rc].sum(axis=1) /
                              roi_output[:, mask_rc].sum(axis=1))
    io_periph = (roi_input[:, ~mask_rc].sum(axis=1) /
                               roi_output[:, ~mask_rc].sum(axis=1))
    mean_rc_io[i_run] = io_rc.mean()
    mean_periph_io[i_run] = io_periph.mean()

    sigma_io_rc = io_rc.std(ddof=1)
    sigma_io_periph = io_periph.std(ddof=1)
    conf_int_rc[i_run, :] = stt.norm.interval(0.95,
                                              loc=mean_rc_io[i_run],
                                              scale=sigma_io_rc)
    conf_int_periph[i_run, :] = stt.norm.interval(0.95,
                                                  loc=mean_periph_io[i_run],
                                                  scale=sigma_io_periph)
print('input-output ratio rich club: ', str(mean_rc_io))
print('input-output ratio periphery: ', str(mean_periph_io))
print('95% Konfidenz Interval (rich cluc): ' + str(conf_int_rc))
print('95% Konfidenz Interval (periphery): ' + str(conf_int_periph))


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor

df = pd.read_csv('Franchise_Dataset.csv')
df.head()

X =df.iloc[:, 1:2].values
y =df.iloc[:, 2].values

model = RandomForestRegressor(n_estimators = 10, random_state = 0)
model.fit(X, y)

y_pred =model.predict([[6]])
print(y_pred)

X_grid_data = np.arange(min(X), max(X), 0.01)
X_grid_data = X_grid_data.reshape((len(X_grid_data), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid_data,model.predict(X_grid_data), color = 'blue')
plt.title('Random Forest Regression')
plt.xlabel('Counter Sales')
plt.ylabel('Net Profit')
plt.show()


from __future__ import division
import re
import sys
import threading
import pyautogui
import pyperclip
import os
import speech_recognition as sr
from pywinauto import Application
import time
import openai
import win32com.client as wincl

#발급 받은 API 키 설정
OPENAI_API_KEY = "..."

# openai API 키 인증
openai.api_key = OPENAI_API_KEY

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"..."

from google.cloud import speech
import pyaudio
from six.moves import queue

# Audio recording parameters
RATE = 20000  ## 음성인식 성능향상
CHUNK = int(RATE / 30)  # 100ms ## 음성인식 성능향상

class MicrophoneStream(object):
    """Opens a recording stream as a generator yielding the audio chunks."""

    def __init__(self, rate, chunk):
        self._rate = rate
        self._chunk = chunk

        # Create a thread-safe buffer of audio data
        self._buff = queue.Queue()
        self.closed = True

    def __enter__(self):
        self._audio_interface = pyaudio.PyAudio()
        self._audio_stream = self._audio_interface.open(
            format=pyaudio.paInt16,
            # The API currently only supports 1-channel (mono) audio
            # https://goo.gl/z757pE
            channels=1, rate=self._rate,
            input=True, frames_per_buffer=self._chunk,
            # Run the audio stream asynchronously to fill the buffer object.
            # This is necessary so that the input device's buffer doesn't
            # overflow while the calling thread makes network requests, etc.
            stream_callback=self._fill_buffer,
        )

        self.closed = False

        return self

    def __exit__(self, type, value, traceback):
        self._audio_stream.stop_stream()
        self._audio_stream.close()
        self.closed = True
        # Signal the generator to terminate so that the client's
        # streaming_recognize method will not block the process termination.
        self._buff.put(None)
        self._audio_interface.terminate()

    def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
        """Continuously collect data from the audio stream, into the buffer."""
        self._buff.put(in_data)
        return None, pyaudio.paContinue

    def generator(self):
        while not self.closed:
            # Use a blocking get() to ensure there's at least one chunk of
            # data, and stop iteration if the chunk is None, indicating the
            # end of the audio stream.
            chunk = self._buff.get()
            if chunk is None:
                return
            data = [chunk]

            # Now consume whatever other data's still buffered.
            while True:
                try:
                    chunk = self._buff.get(block=False)
                    if chunk is None:
                        return
                    data.append(chunk)
                except queue.Empty:
                    break

            yield b''.join(data)

def type_text(text):
    pyperclip.copy(text)
    pyautogui.hotkey('ctrl', 'v')

# 기존 임포트 및 설정 코드 생략 ...

def speak(text, rate=1.0):
    speak = wincl.Dispatch("SAPI.SpVoice")
    speak.Rate = rate
    speak.Speak(text)

waiting = False

def waiting_message():
    global waiting
    while waiting:
        print("잠시만 기다려 주세요...")
        speak("잠시만 기다려 주세요...")
        time.sleep(1)

def is_windows_spoken():

    global waiting
    # 음성 인식 객체 생성
    r = sr.Recognizer()
    mic = sr.Microphone()

    with mic as source:
        r.adjust_for_ambient_noise(source)

    messages = []
    while True:
        print("Listening...")
        speak("음성인식 실행")
        with mic as source:
            r.non_blocking = True
            audio = r.listen(source)

        try:
            text = r.recognize_google_cloud(audio, language='ko-KR')
            if "윈도우" in text:
                speak("네 말씀하세요.")
                print("명령어를 말씀해주세요.")
                return True
            elif "입력" in text:
                speak("잠시만 기다려주세요")
                print("잠시만 기다려주세요")
                return False
            #gpt api 사용.

            elif "검색" in text:
                print("검색 질문을 말씀해주세요.")
                speak("검색 질문을 말씀해주세요.")
                with mic as source:
                    audio = r.listen(source)

                waiting = True
                waiting_thread = threading.Thread(target=waiting_message)
                waiting_thread.start()

                question = r.recognize_google_cloud(audio, language='ko-KR')
                messages.append({"role": "user", "content": question})

                answer = speech_to_gpt(messages)

                waiting = False
                waiting_thread.join()

                print(f'GPT: {answer}')
                messages.append({"role": "assistant", "content": answer})
                type_text(answer)
                #speak(answer)

            elif "종료" in text:
                speak("종료하겠습니다.")
                sys.exit("Exiting")
            else:
                speak("다시 말씀해주세요.")
                print("다시 말씀해주세요.")
        except sr.UnknownValueError:
            speak("다시 말씀해주세요.")
            print("다시 말씀해주세요.")
        except sr.WaitTimeoutError:
            # 타임아웃 에러 발생 시 다시 음성 수집 시작
            continue

def listen_print_loop(responses):
    transcript = ""
    final_transcript = ""

    for response in responses:
        if not response.results:
            continue

        result = response.results[0]

        if not result.alternatives:
            continue

        transcript = result.alternatives[0].transcript

        # 기존에 출력된 텍스트 지우기
        sys.stdout.write("\033[F\033[K")

        # 최종 결과만 출력하고 나머지는 저장
        if result.is_final:
            final_transcript += transcript + " "
            print(final_transcript)

            if re.search(r'\b(음성인식 꺼 줘|quit)\b', final_transcript, re.I):
                speak("종료하겠습니다.")
                sys.exit("Exiting")

            if re.search(r'\b(음성인식 종료|quit)\b', final_transcript, re.I):
                speak("종료하겠습니다.")
                sys.exit("Exiting")

            if "메모장" in final_transcript:
                app = Application().start("notepad.exe")
                time.sleep(0.5)
                break

            if "크롬" in final_transcript:
                app = Application().start("C:\Program Files\Google\Chrome\Application\chrome.exe")
                time.sleep(0.5)
                break

            if "한글" in final_transcript:
                app = Application().start("C:\Program Files (x86)\HNC\Office 2022\HOffice120\Bin\Hwp.exe")
                time.sleep(0.5)
                break

            if "검색" in final_transcript:
                pyautogui.keyDown("ctrl")
                pyautogui.press("f")
                pyautogui.keyUp("ctrl")
                time.sleep(0.5)
                break

            if "복사" in final_transcript:
                pyautogui.keyDown("ctrl")
                pyautogui.press("c")
                pyautogui.keyUp("ctrl")
                time.sleep(0.5)
                break

            if "붙여 넣기" in final_transcript:
                pyautogui.keyDown("ctrl")
                pyautogui.press("v")
                pyautogui.keyUp("ctrl")
                time.sleep(0.5)
                break

            if "뒤로 가기" in final_transcript:
                pyautogui.keyDown("ctrl")
                pyautogui.press("z")
                pyautogui.keyUp("ctrl")
                time.sleep(0.5)
                break

            if "닫기" in final_transcript:
                pyautogui.keyDown("alt")
                pyautogui.press("f4")
                pyautogui.keyUp("alt")
                time.sleep(0.5)
                break

            if "다음" in final_transcript:
                pyautogui.press("space")
                time.sleep(0.5)
                break

            if "나가기" in final_transcript:
                pyautogui.press("esc")
                time.sleep(0.5)
                break

            if "엔터" in final_transcript:
                pyautogui.press("enter")
                time.sleep(0.5)
                break

            # ------------------------------------------5/7
            if "한글" in transcript:
                pyautogui.press("hangul")
                time.sleep(0.5)
                break

            if "영어" in transcript:
                pyautogui.press("hangul")
                time.sleep(0.5)
                break

            if "저장" in transcript:
                pyautogui.hotkey('ctrl', 's')
                time.sleep(0.5)
                break

            if "전체 선택" in transcript:
                pyautogui.hotkey('ctrl', 'a')
                time.sleep(0.5)
                break

            if "캡처" in transcript:
                pyautogui.hotkey('shift', 'win', 's')
                time.sleep(0.5)
                break

            if "캡스락" in transcript:
                pyautogui.press("capslocck")
                time.sleep(0.5)
                break

            if "자르기" in transcript:
                pyautogui.hotkey('ctrl', 'x')
                time.sleep(0.5)
                break

            # ------------------------------------------5/13

            if "새로고침" in transcript:
                pyautogui.press("f5")
                time.sleep(0.5)
                break

            if "시작" in transcript:
                pyautogui.press("f5")
                time.sleep(0.5)
                break

            if "바탕화면" in transcript:
                pyautogui.hotkey('win', 'd')
                time.sleep(0.5)
                break

            if "최대화" in transcript:
                pyautogui.hotkey('win', 'up')
                time.sleep(0.5)
                break

            if "삭제" in transcript:
                pyautogui.press("delete")
                time.sleep(0.5)
                break

            if "실행" in transcript:
                pyautogui.hotkey('ctrl', 'alt', 'f10')
                time.sleep(0.5)
                break

            if "작업 관리자" in transcript:
                pyautogui.hotkey('ctrl', 'shift', 'esc')
                time.sleep(0.5)
                break

            final_transcript = ""
        else:
            sys.stdout.write(transcript + '\r')
            sys.stdout.flush()

    return final_transcript

def speech_to_text():
    # create a recognizer instance
    r = sr.Recognizer()
    # use the system default microphone as the audio source
    with sr.Microphone() as source:
        print("Speak:")
        # adjust for ambient noise
        r.adjust_for_ambient_noise(source)
        # listen for audio
        audio = r.listen(source)
    try:
        # recognize speech using Google Speech Recognition
        text = r.recognize_google(audio, language='ko-KR')
        speak("입력하겠습니다.")
        print("You said: " + text)
        return text
    except sr.WaitTimeoutError:
        print("Listening timed out. Please try again.")
        return None
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))
        return None

def speech_to_gpt(messages):
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    chat_response = completion.choices[0].message.content
    return chat_response

def main():
    # See http://g.co/cloud/speech/docs/languages
    # for a list of supported languages.
    language_code = 'ko-KR'  # a BCP-47 language tag

    client = speech.SpeechClient()
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=RATE,
        language_code=language_code)
    streaming_config = speech.StreamingRecognitionConfig(
        config=config,
        interim_results=True)

    while True:
        windows_spoken = is_windows_spoken()
        if windows_spoken:
            end_time = time.time() + 5  # 5 seconds
            while time.time() < end_time:
                with MicrophoneStream(RATE, CHUNK) as stream:
                    audio_generator = stream.generator()
                    requests = (speech.StreamingRecognizeRequest(audio_content=content)
                                for content in audio_generator)
                    responses = client.streaming_recognize(streaming_config, requests)
                    listen_print_loop(responses)
                    break

        elif not windows_spoken:
            command = speech_to_text()
            if command:
                type_text(command)
        else:
            time.sleep(0.05)

if __name__ == '__main__':
    main()

# [END speech_transcribe_streaming_mic]


try:
	import sys
	import os.path
	sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))
	import os, errno
	import logging # http://www.onlamp.com/pub/a/python/2005/06/02/logging.html
	from logging import handlers
	import argparse
	#sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pysunspec'))
	from datetime import datetime, date, time, timedelta
#	import jsonpickle # pip install jsonpickle
	import json
	from sit_logger import SitLogger
	from sit_constants import SitConstants
	#from sit_date_time import SitDateTime
except ImportError as l_err:
	print("ImportError: {0}".format(l_err))
	raise l_err

class SitJsonConf(object):

# CONSTANTS

# VARIABLES
	_logger = None
	_config_dir = None #Setted by init
	_config_file = None #File name only
	_config_data = None # Where json.dump are put

# FUNCTIONS DEFINITION

	def __init__(self, a_config_file_name=SitConstants.SIT_JSON_DEFAULT_CONFIG_FILE_NAME):
		"""
			Initialize
		"""
		self._config_dir = SitConstants.DEFAULT_CONF_DIR
		self._config_file = a_config_file_name
		if not os.path.isdir(self._config_dir):
			raise Exception('init->Config dir {} does not exist, sudo mkdir -m 777 {}'.format(self._config_dir, self._config_dir))
		try:
			self._logger = SitLogger().new_logger(__name__)
			if __name__ == '__main__':
				self.init_arg_parse()
			self.read_config(self.configuration_file_path())
		except OSError as l_e:
			self._logger.warning("init-> OSError, probably rollingfileAppender:{}".format(l_e))
			if l_e.errno != errno.ENOENT:
				raise l_e
		except Exception as l_e:
			self._logger.error('Error in init: {}'.format(l_e))
			raise l_e

	def configuration_file_path(self):
		"""
		"""
		l_res = os.path.join(self._config_dir, self._config_file)
		return l_res

	def read_config(self, a_config_file_path=None):
		"""
			puts into self._config_data json data
		"""
		if a_config_file_path is None:
			l_config_file_path = self.configuration_file_path()
		else:
			l_config_file_path = a_config_file_path
		with open(l_config_file_path, "r") as l_file:
			self._config_data = json.load(l_file)

	def item(self, a_key):
		"""
		returns value of given key exception if not found
		"""
		l_res = None
		return l_res

# SCRIPT ARGUMENTS

	def init_arg_parse(self):
		"""
			Parsing arguments
		"""
		self._logger.debug('init_arg_parse-> begin')
		self._parser = argparse.ArgumentParser(description='Actions with sunspec through TCP')
		self._add_arguments()
		l_args = self._parser.parse_args()
		self._args = l_args

	def _add_arguments(self):
		"""
		Add arguments to parser (called by init_arg_parse())
		"""
		self._parser.add_argument('-v', '--verbose', help='increase output verbosity', action="store_true")
		self._parser.add_argument('-u', '--update_leds_status', help='Updates led status according to spec', action="store_true")
		self._parser.add_argument('-t', '--test', help='Runs test method', action="store_true")

		#self._parser.add_argument('-u', '--base_url', help='NOT_IMPLEMENTED:Gives the base URL for requests actions', nargs='?', default=self.DEFAULT_BASE_URL)
		l_required_named = self._parser.add_argument_group('required named arguments')
#		l_required_named.add_argument('-i', '--host_ip', help='Host IP', nargs='?', required=True)
#		l_required_named.add_argument('-u', '--slave_address', help='Slave address of modbus device', nargs='?', required=True)
		l_required_named.add_argument('-m', '--host_mac', help='Host MAC', nargs='?', required=True)
#		l_required_named.add_argument('-l', '--longitude', help='Longitude coordinate (beware timezone is set to Chile)', nargs='?', required=True)
#		l_required_named.add_argument('-a', '--lattitude', help='Lattitude coordinate (beware timezone is set to Chile)', nargs='?', required=True)
#		l_required_named.add_argument('-d', '--device_type', help='Device Type:' + ('|'.join(str(l) for l in self.DEVICE_TYPES_ARRAY)), nargs='?', required=True)

	def execute_corresponding_args( self ):
		"""
			Parsing arguments and calling corresponding functions
		"""
		if self._args.verbose:
			self._logger.setLevel(logging.DEBUG)
		else:
			self._logger.setLevel(logging.INFO)
		if self._args.test:
			self.test()
		#if self._args.store_values:

# TEST

	def test(self):
		"""
			Test function
		"""
		try:
			self._logger.info("################# BEGIN #################")
			self._logger.info("--> ************* device models *************: {}".format(l_d.models))	 #Lists properties to be loaded with l_d.<property>.read() and then access them
		except Exception as l_e:
			self._logger.exception("Exception occured:  {}".format(l_e))
			print('Error: {}'.format(l_e))
			self._logger.error('Error: {}'.format(l_e))
			sys.exit(1)

#################### END CLASS ######################

def main():
	"""
	Main method
	"""
	#logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, filemode="a+", format="%(asctime)-15s %(levelname)-8s %(message)s")
	logger = logging.getLogger(__name__)

	try:
		l_obj = SitJsonConf()
		l_obj.execute_corresponding_args()
#		l_id.test()
	except KeyboardInterrupt:
		logger.exception("Keyboard interruption")
	except Exception as l_e:
		logger.exception("Exception occured:{}".format(l_e))
		raise l_e

#Call main if this script is executed
if __name__ == '__main__':
    main()


from scipy import sparse
import time

import gradient
import vector_fields

def enforce_boundaries(phi, img_shape, dim):
    # make sure we are inside the image
    phi[:, 1] = phi[:, 1].clip(0, img_shape[1])
    phi[:, 0] = phi[:, 0].clip(0, img_shape[0])
    # 3d case
    if dim == 3:
        phi[:, 2] = phi[:, 2].clip(0, img_shape[2])
    return phi

def integrate(x_0, kernels, alpha, c_sup, dim, steps = 10, compute_gradient = True):
    start = time.time()
    if (compute_gradient):
        S_i = vector_fields.evaluation_matrix_blowup(kernels, x_0, c_sup, dim)
    else:
        S_i = vector_fields.evaluation_matrix(kernels, x_0, c_sup, dim)
    #print("FE -- initial S_i ", (time.time() - start) / 60)
    start = time.time()
    V_i = vector_fields.make_V(S_i, alpha, dim)
    #print("FE -- initial V_i ", (time.time() - start) / 60)
    x_i = x_0
    dphi_dalpha_i = sparse.csc_matrix((S_i.shape[0], S_i.shape[1]))

    for i in range(steps):
        if compute_gradient:
            start = time.time()
            dv_dphit_i = gradient.dv_dphit(x_i, kernels, alpha, c_sup, dim)
            print("FE -- dv_dphi_i ", (time.time() - start) / 60)
            start = time.time()
            dphi_dalpha_i = gradient.next_dphi_dalpha(S_i, dv_dphit_i, dphi_dalpha_i, steps, dim)
            print("FE -- dphi_dalpha_i ", (time.time() - start) / 60)
        start = time.time()
        # Make a step
        x_i = x_i + V_i / steps

        # Compute evaluatation matrix based on updated evaluation points
        if (i < steps - 1):
            if (compute_gradient):
                S_i = vector_fields.evaluation_matrix_blowup(kernels, x_i, c_sup, dim)
            else:
                S_i = vector_fields.evaluation_matrix(kernels, x_i, c_sup, dim)
            V_i = vector_fields.make_V(S_i, alpha, dim)

        print("FE -- Euler step ", (time.time() - start) / 60)
    # Enforce boundary conditions
    img_shape = []
    for d in range(dim):
        img_shape.append(max(x_0[:, d]))
    x_1 = enforce_boundaries(x_i, img_shape, dim)

    if compute_gradient:
        return x_1, dphi_dalpha_i
    else:
        return x_1


import re
from pyspark import SparkConf, SparkContext

def normalizeWords(text):
    return re.compile(r'\W+', re.UNICODE).split(text.lower())

conf = SparkConf().setMaster("local").setAppName("WordCount")
sc = SparkContext(conf = conf)

input = sc.textFile("file:///sparkcourse/book.txt")
words = input.flatMap(normalizeWords)

# Note: Count by value creates a python object instead of a RDD hence .collect() not required to fetch the data
wordCounts = words.countByValue()

for word, count in wordCounts.items():
    cleanWord = word.encode('ascii', 'ignore')
    if (cleanWord):
        print(cleanWord.decode() + " " + str(count))


import numpy as np
import pandas as pd
from collections import defaultdict
import matplotlib.pyplot as plt
from datetime import datetime

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim

import os, sys

class SDAE(nn.Module):
    def __init__(self, input_dim, hidden_dim, embed_dim):
        super(SDAE, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.embed_dim = embed_dim
        self.enc1 = nn.Linear(input_dim, hidden_dim)
        self.enc2 = nn.Linear(hidden_dim, hidden_dim)
        self.enc3 = nn.Linear(hidden_dim, embed_dim)

        self.dec1 = nn.Linear(embed_dim, hidden_dim)
        self.dec2 = nn.Linear(hidden_dim, hidden_dim)
        self.dec3 = nn.Linear(hidden_dim, input_dim)

    def forward(self, x):
        x = F.relu(self.enc1(x))
        x = F.relu(self.enc2(x))
        latent = F.relu(self.enc3(x))
        x = F.relu(self.dec1(latent))
        x = F.relu(self.dec2(x))
        x = self.dec3(x)
        return latent, x

class CDL:

    def __init__(self, train_imp, test_imp, input_dim, hidden_dim, dim_f, dataloader, seed, device, config):
        self.dim_f = dim_f
        self.user_num = train_imp.shape[0]
        self.item_num = train_imp.shape[1]
        self.input_dim = input_dim

        self.R_tr = train_imp
        self.R_tst = test_imp
        self.C = np.where(self.R_tr > 0, 1, 0)
        self.C_u = np.zeros((self.item_num, self.item_num))
        self.C_i = np.zeros((self.user_num, self.user_num))

        np.random.seed(seed)
        self.X = np.random.standard_normal((self.user_num, dim_f))
        self.Y = np.random.standard_normal((self.item_num, dim_f))

        self.loss_tr = defaultdict(float)
        self.loss_ae = defaultdict(float)
        self.loss_tst = defaultdict(float)

        self.ae = SDAE(input_dim=input_dim, hidden_dim=hidden_dim, embed_dim=dim_f).to(device)
        self.optimizer = optim.Adam(self.ae.parameters(), lr=config.learning_rate, weight_decay=config.lambda_w)
        self.dataloader = dataloader

        self.lambda_u = config.lambda_u
        self.lambda_w = config.lambda_w
        self.lambda_v = config.lambda_v
        self.lambda_n = config.lambda_n

        self.device = device
        self.config = config

    def ae_train(self):
        latent_np = np.zeros((self.item_num, self.dim_f))
        loss_ae = []
        for batch in self.dataloader:
            y = batch['clean'].to(self.device)
            x = batch['corrupt'].to(self.device)
            idx = batch['idx']
            latent, pred = self.ae(x)
            latent_ = latent.detach().cpu().numpy()
            latent_np[idx.numpy()] = latent_

            loss = self.loss_fn(pred, y, idx.to(self.device), latent_)
            loss.backward()
            self.optimizer.step()
            loss_ae.append(loss.item())

        return latent_np, np.mean(loss_ae)

    def fit(self):
        start = datetime.now()
        for epoch in range(self.config.epochs):
            start_epoch = datetime.now()
            self.ae.train()
            self.latent_feat, self.loss_ae[epoch] = self.ae_train()
            n = 0
            for u in range(self.user_num):
                yty = np.dot(self.Y.T, self.Y)
                self.X[u, :] = self.update_user_vector(u, yty)

            for i in range(self.item_num):
                xtx = np.dot(self.X.T, self.X)
                self.Y[i, :] = self.update_item_vector(i, xtx)

            phat = self.scoring()

            train_loss = self.evaluate(train_eval=True)
            test_loss = self.evaluate(train_eval=False)

            self.loss_tr[epoch] = train_loss
            self.loss_tst[epoch] = test_loss

            print(f'EPOCH {epoch+1} : TRAINING RANK {self.loss_tr[epoch]:.5f}, VALID RANK {self.loss_tst[epoch]:.5f}')

            print(f'Time per one epoch {datetime.now() - start_epoch}')
        end = datetime.now()
        print(f'Training takes time {end-start}')

    def scoring(self):

        return np.dot(self.X, self.Y.T)

    def update_user_vector(self, u, yty):
        np.fill_diagonal(self.C_u, (self.C[u, :] - 1))
        comp1 = yty
        comp2 = np.dot(self.Y.T, self.C_u).dot(self.Y)
        comp3 = np.identity(self.config.dim_f) * self.config.lambda_u
        comp = np.linalg.inv(comp1 + comp2 + comp3)
        self.C_u = self.C_u + np.identity(self.C_u.shape[0])
        comp = np.dot(comp, self.Y.T).dot(self.C_u)

        return np.dot(comp, self.R_tr[u, :])

    def update_item_vector(self, i, xtx):
        np.fill_diagonal(self.C_i, (self.C[:, i] - 1))
        comp1 = xtx
        comp2 = np.dot(self.X.T, self.C_i).dot(self.X)
        comp3 = np.identity(self.config.dim_f) * self.config.lambda_v
        comp = np.linalg.inv(comp1 + comp2 + comp3)
        self.C_i = self.C_i + np.identity(self.C_i.shape[0])
        comp4 = self.X.T.dot(self.C_i).dot(self.R_tr[:, i])
        comp5 = self.lambda_v * self.latent_feat[i, :]

        return np.dot(comp, comp4+comp5)

    def loss_fn(self, pred, xc, idx, latent_feat):
        X = torch.tensor(self.X).to(self.device)
        Y = torch.tensor(self.Y).to(self.device)[idx, :]
        R = torch.tensor(self.R_tr).float().to(self.device)[:, idx]
        C = torch.tensor(self.C).float().to(self.device)[:, idx]
        latent = torch.tensor(latent_feat).to(self.device)

        comp1 = (X**2).sum(axis=1).sum() * self.lambda_u/2
        comp2 = ((Y - latent)**2).sum(axis=1).sum() * self.lambda_v/2
        comp3 = ((pred - xc)**2).sum(axis=1).sum() * self.lambda_n/2
        comp4 = torch.sum((torch.mm(X, Y.T) - R)**2 * C/2)

        return comp1+comp2+comp3+comp4

    def evaluate(self, train_eval):
        if train_eval:
            R = self.R_tr
        else:
            R = self.R_tst

        phat = self.scoring()
        rank_mat = np.zeros(phat.shape)
        for u in range(self.user_num):
            pred_u = phat[u, :] * -1
            rank = pred_u.argsort().argsort()
            rank = rank / self.item_num
            rank_mat[u, :] = rank

        return np.sum(R * rank_mat) / np.sum(R)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Bernoulli
from torch.autograd import Variable
import torch.optim as optim
import numpy as np
import random
import math
import sys
import os

torch.manual_seed(0)

class armax_model(nn.Module):
    def __init__(self, no_of_glucose_inputs = 10, no_of_insulin_inputs = 10, no_of_meal_inputs = 10):

        output_dim = 1
        super(armax_model, self).__init__()

        self.glucose_weights = nn.Parameter(torch.randn((no_of_glucose_inputs, output_dim)))
        self.insulin_weights = nn.Parameter(torch.randn((no_of_insulin_inputs, output_dim)))
        self.meal_weights = nn.Parameter(torch.randn((no_of_meal_inputs, output_dim)))

        self.bias = nn.Parameter(torch.randn((output_dim,)))

    def forward(self, glucose, insulin, meal):
        glucose_term = torch.matmul(glucose, self.glucose_weights)
        # Enforcing insulin having negative effects
        insulin_term = torch.matmul(insulin, -torch.abs(self.insulin_weights))
        meal_term = torch.matmul(meal, self.meal_weights)

        y = glucose_term + insulin_term + meal_term
        return y

def fit_model(all_states, all_controls, all_next_states, glucose_normalizer, insulin_normalizer, meal_normalizer, filename):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    episode_count = 100
    batch_size = 64

    model = armax_model().to(device)
    learning_rate = 1e-3
    # criterion = nn.SmoothL1Loss()
    criterion = nn.MSELoss()

    optimizer = optim.Adam(model.parameters(), lr = learning_rate)

    all_train_states = np.concatenate((all_states['main']['train'], all_states['omega']['train']), axis=0)
    all_train_controls = np.concatenate((all_controls['main']['train'], all_controls['omega']['train']), axis=0)
    all_train_next_states = np.concatenate((all_next_states['main']['train'], all_next_states['omega']['train']), axis=0)
    T = 10

    num_datapoints = len(all_train_states)
    for episode in range(episode_count):
        number_of_batches = math.ceil(float(num_datapoints) / float(batch_size))

        total_loss = 0.0

        for batch_index in range(number_of_batches):
            start = batch_index * batch_size
            end = min(start + batch_size, num_datapoints)
            loss_list = []
            optimizer.zero_grad()

            # for entry in range(start, end):
            #     glucose_input = all_train_states[entry, :T] * glucose_normalizer
            #     glucose_input = torch.from_numpy(glucose_input).float().to(device)
            #     insulin_input = all_train_states[entry, T:2*T] * insulin_normalizer
            #     insulin_input = torch.from_numpy(insulin_input).float().to(device)
            #     meal_input = all_train_states[entry, 2*T:3*T] * meal_normalizer
            #     meal_input = torch.from_numpy(meal_input).float().to(device)
            #     target = all_train_next_states[entry, :] * glucose_normalizer
            #     target = torch.from_numpy(target).float().to(device)
            #     prediction = model.forward(glucose_input, insulin_input, meal_input)
            #     loss = criterion(prediction, target).to(device)
            #     loss_list.append(loss)
            #     # Computing loss for tracking
            #     total_loss += loss.item()

            glucose_input = all_train_states[start:end, :T] * glucose_normalizer
            glucose_input = torch.from_numpy(glucose_input).float().to(device)

            insulin_input = all_train_states[start:end, T:2*T] * insulin_normalizer
            insulin_input = torch.from_numpy(insulin_input).float().to(device)

            meal_input = all_train_states[start:end, 2*T:3*T] * meal_normalizer
            meal_input = torch.from_numpy(meal_input).float().to(device)

            target = all_train_next_states[start:end, :] * glucose_normalizer
            target = torch.from_numpy(target).float().to(device)

            prediction = model.forward(glucose_input, insulin_input, meal_input)

            loss = criterion(prediction, target).to(device)

            # Computing loss for tracking
            total_loss += loss.item() * (end-start)

            loss.backward()
            optimizer.step()

        print("At episode - ", episode, " avg loss computed - ", total_loss / float(len(all_train_states)))
        if (episode+1)%10 == 0 or episode == 0:
            test_loss = test_model(model, all_states, all_controls, all_next_states, glucose_normalizer, insulin_normalizer, meal_normalizer, T)
            print("At episode - ", episode, " avg test loss computed - ", test_loss )

        torch.save(model.state_dict(), filename)

    return model

def test_model(model, all_states, all_controls, all_next_states, glucose_normalizer, insulin_normalizer, meal_normalizer, T):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    all_test_states = np.concatenate((all_states['main']['test'], all_states['omega']['test']), axis=0)
    all_test_controls = np.concatenate((all_controls['main']['test'], all_controls['omega']['test']), axis=0)
    all_test_next_states = np.concatenate((all_next_states['main']['test'], all_next_states['omega']['test']), axis=0)

    total_loss = 0.0
    for index in range(len(all_test_states)):

        glucose_input = all_test_states[index, :T] * glucose_normalizer
        glucose_input = torch.from_numpy(glucose_input).float().to(device)

        insulin_input = all_test_states[index, T:2*T] * insulin_normalizer
        insulin_input = torch.from_numpy(insulin_input).float().to(device)

        meal_input = all_test_states[index, 2*T:3*T] * meal_normalizer
        meal_input = torch.from_numpy(meal_input).float().to(device)

        target = all_test_next_states[index, :] * glucose_normalizer
        target = torch.from_numpy(target).float().to(device)

        prediction = model.forward(glucose_input, insulin_input, meal_input)
        prediction = prediction.cpu().detach().numpy()[0]

        loss = abs( prediction - target)**2
        total_loss += loss

    avg_error = total_loss / float(len(all_test_states))

    return avg_error.item()

def extract_state_dict():

    filename = os.path.join("./networks/", "diabetes_armax_model.nt")
    model = torch.load(filename)
    torch.save(model.state_dict(), "./networks/diabetes_armax_model_state_dict.nt")


from typing import Dict
import json as _json
import datetime as _dt

def get_all_events() -> Dict:
    with open("events.json", encoding='utf-8') as events_file:
        data = _json.load(events_file)

    return data

def get_all_month_events(month: str) -> Dict:
    events = get_all_events()
    month = month.lower()
    try:
        month_events = events[month]
        return month_events
    except KeyError:
        return "This is not a month name"

def get_all_day_events(month: str, day: int) -> Dict:
    events = get_all_events()
    month = month.lower()
    try:
        day_events = events[month][str(day)] # Here, I passed day as a string becasue in our json, days are written in string format
        return day_events
    except KeyError:
        return "This is not a valid input"

def get_all_today_events():
    today = _dt.date.today()
    month = today.strftime("%B")
    return get_all_day_events(month, today.day)


from cgitb import reset
from flask import Flask, jsonify, request, render_template
import Crypto
import Crypto.Random
from Crypto.PublicKey import RSA
import binascii
from collections import OrderedDict
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
import webbrowser

class Transaction:

    def __init__(self, sender_public_key, sender_private_key, recipient_public_key, amount):
        self.sender_public_key = sender_public_key
        self.sender_private_key = sender_private_key
        self.recipient_public_key = recipient_public_key
        self.amount = amount

    #Function to convert transaction details to dictionary format
    def to_dict(self):
        return OrderedDict({
            'sender_public_key' : self.sender_public_key,
            'recipient_public_key' : self.recipient_public_key,
            'amount' : self.amount
        })

    #Function to sign the transaction
    def sign_transaction(self):
        private_key = RSA.importKey(binascii.unhexlify(self.sender_private_key))
        signer = PKCS1_v1_5.new(private_key)
        hash = SHA.new(str(self.to_dict()).encode('utf8'))
        return binascii.hexlify(signer.sign (hash)).decode('ascii')

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/make/transactions')
def make_transactions():
    return render_template('make_transactions.html')

@app.route('/view/transactions')
def view_transactions():
    return render_template('view_transactions.html')

#Public and private key pair generation for wallet
@app.route('/wallet/new')
def new_wallet():
    random_gen = Crypto.Random.new().read
    private_key = RSA.generate(1024, random_gen)
    public_key = private_key.publickey()

    response={
        'private_key' : binascii.hexlify(private_key.export_key(format('DER'))).decode('ascii'),
        'public_key' :binascii.hexlify(public_key.export_key(format('DER'))).decode('ascii')
    }

    return response

#Code to generate the transactions
@app.route('/generate/transactions', methods=['POST'])
def generate_transactions():
    #Extract transaction details from form
    sender_public_key = request.form['sender_public_key']
    sender_private_key =  request.form['sender_private_key']
    recipient_public_key = request.form['recipient_public_key']
    amount = request.form['amount']

    #Make a Transaction object
    transaction = Transaction(sender_public_key, sender_private_key, recipient_public_key, amount)

    #Convert Transaction object into dictionary and sign it
    response ={'transaction' : transaction.to_dict(),
                'signature' : transaction.sign_transaction()}

    return jsonify(response), 200

if __name__ == '__main__':
    from argparse import ArgumentParser

    parser = ArgumentParser()
    parser.add_argument('-p', '--port', default = 8081, type = int, help ="Port to listen")
    args = parser.parse_args()
    port = args.port

    #Open url in browser
    webbrowser.open('http://127.0.0.1:'+str(port))
    #Run flask app
    app.run(host="0.0.0.0",port=port, debug=True)


from matplotlib import pyplot as plt
import numpy as np
import random
import utils

features = np.array([1,2,3,5,6,7])
labels = np.array([155, 197, 244, 356,407,448])

print(features)
print(labels)

utils.plot_points(features, labels)

# Feature cross / synthetic feature
def feature_cross(num_rooms, population):
    room_per_person_feature = num_rooms / population
    return room_per_person_feature

def simple_trick(base_price, price_per_room, num_rooms, price):
    # select random learning rate
    small_random_1 = random.random()*0.1
    small_random_2 = random.random()*0.1
    # calculate the prediction.
    predicted_price = base_price + price_per_room*num_rooms
    # check where the point is with respect to the line.
    if price > predicted_price and num_rooms > 0:
        # translate the line
        price_per_room += small_random_1
        # rotate the line
        base_price += small_random_2
    if price > predicted_price and num_rooms < 0:
        price_per_room -= small_random_1
        base_price += small_random_2
    if price < predicted_price and num_rooms > 0:
        price_per_room -= small_random_1
        base_price -= small_random_2
    if price < predicted_price and num_rooms < 0:
        price_per_room -= small_random_1
        base_price += small_random_2
    return price_per_room, base_price

def absolute_trick(base_price, price_per_room, num_rooms, price, learning_rate):
    predicted_price = base_price + price_per_room*num_rooms
    if price > predicted_price:
        price_per_room += learning_rate*num_rooms
        base_price += learning_rate
    else:
        price_per_room -= learning_rate*num_rooms
        base_price -= learning_rate
    return price_per_room, base_price

def square_trick(base_price, price_per_room, num_rooms, price, learning_rate):
    predicted_price = base_price + price_per_room*num_rooms
    price_per_room += learning_rate*num_rooms*(price-predicted_price)
    base_price += learning_rate*(price-predicted_price)
    return price_per_room, base_price

# We set the random seed in order to always get the same results.
random.seed(0)

def linear_regression(features, labels, learning_rate=0.01, epochs = 1000):
    price_per_room = random.random()
    base_price = random.random()
    for epoch in range(epochs):
        # Uncomment any of the following lines to plot different epochs
        if epoch == 1:
            utils.draw_line(price_per_room, base_price, starting=0, ending=8)
        elif epoch <= 10:
            utils.draw_line(price_per_room, base_price, starting=0, ending=8)
        elif epoch <= 50:
            utils.draw_line(price_per_room, base_price, starting=0, ending=8)
        elif epoch > 50:
            utils.draw_line(price_per_room, base_price, starting=0, ending=8)
        i = random.randint(0, len(features)-1)
        num_rooms = features[i]
        price = labels[i]
        # Uncomment any of the 2 following lines to use a different trick
        #price_per_room, base_price = absolute_trick(base_price,
        price_per_room, base_price = square_trick(base_price,
                                                  price_per_room,
                                                  num_rooms,
                                                  price,
                                                  learning_rate=learning_rate)
    utils.draw_line(price_per_room, base_price, 'black', starting=0, ending=8)
    utils.plot_points(features, labels)
    print('Price per room:', price_per_room)
    print('Base price:', base_price)
    return price_per_room, base_price

# This line is for the x-axis to appear in the figure
plt.ylim(0,500)

linear_regression(features, labels, learning_rate = 0.01, epochs = 1000)

# The root mean square error function
def rmse(labels, predictions):
    n = len(labels)
    differences = np.subtract(labels, predictions)
    return np.sqrt(1.0/n * (np.dot(differences, differences)))