from flask import *
from flask import Flask, render_template
from flask import request, redirect
from db_connector.db_connector import connect_to_database, execute_query
webapp = Flask(__name__)

#Dashboard pages/links for Home, Drinks, Toppings, Customers, and Order.
@webapp.route('/')
def index():
    return render_template("index.html")

# ------ ORDERS ------
# SELECT for Order Page | Shows the Order Table.
@webapp.route('/order')
def order():
    db_connection = connect_to_database()
    # Prints message to console.
    print("Executing a query on the database using the credentials from db_credentials.py: orders table, select all columns.")  # Prints message to console.
    # Table showing all of the details of the orders.
    query = "SELECT orders.order_id, orders.customer_id, customers.first_name, customers.last_name, drinks_instances.instance_id, base_drinks.drink_name, drinks_instances.size, IF( size = 0, base_drinks.small_cost, IF( size = 1, base_drinks.medium_cost, IF( size = 2, base_drinks.large_cost, NULL ) ) ) AS 'Size Cost' FROM orders LEFT JOIN drinks_instances ON orders.order_id = drinks_instances.order_id LEFT JOIN customers ON orders.customer_id = customers.customer_id LEFT JOIN base_drinks ON drinks_instances.drink_id = base_drinks.drink_id LEFT JOIN drinks_toppings ON drinks_instances.instance_id = drinks_toppings.instance_id ORDER BY order_id;"
    data = execute_query(db_connection, query)
    cust_query = 'SELECT customer_id, first_name, last_name FROM customers'
    cust_results = execute_query(db_connection, cust_query).fetchall()
    # Show the most recent Order as an option to add a new drink.
    order_id_query = "SELECT MAX(order_id) FROM orders;"
    order_id =  execute_query(db_connection, order_id_query)
    # Shows the list of ID (names) possible to be shown on the select.
    drink_name = 'SELECT drink_id, drink_name FROM base_drinks'
    dn_result = execute_query(db_connection, drink_name).fetchall()
    # Show the most recent Instance ID as an option to add a new toppings.
    instance_id_query = "SELECT MAX(instance_id) FROM drinks_instances;"
    instance_id =  execute_query(db_connection, instance_id_query)
    # Shows the list of Toppings (names) possible to be shown on the select.
    topping_name = 'SELECT topping_id, topping_name FROM toppings'
    tn_result = execute_query(db_connection, topping_name).fetchall()
    return render_template('order.html', data = data, customers = cust_results, order_id = order_id, drinks = dn_result, instance_id = instance_id, toppings = tn_result)

# CREATE for Order Page
@webapp.route('/order', methods=['POST', 'GET'])
def order_create():
    # All Post Requests
    if request.method == 'POST':  # Post Forms.
        # Create new Order Form
        if 'customer_id' in request.form: # looks for customer_id in the form, then proceeds.
            db_connection = connect_to_database()
            customer_id = request.form['customer_id']  # Searches for element named customer_id to use as customer_id in the query.
            query = 'INSERT INTO orders (customer_id) VALUES (%s)'  # Inserts previous elements into orders table.
            data = (customer_id)
            print("Added new order!") # Prints to console that a new order was added.
            execute_query(db_connection, query, data)
            return redirect(url_for('order'))
        # Add Drink to Order
        if 'order_id' and 'drink_id' and 'size' in request.form: # Looks for order_id, drink_id, and size in the form, then proceeds.
            db_connection = connect_to_database()
            order_id = request.form['order_id']  # Searches for element named order_id to use as order_id in the query.
            drink_id = request.form['drink_id']  # Searches for element named drink_id to use as drink_id in the query.
            size = request.form['size']  # Searches for element named size to use as size in the query.
            query = 'INSERT INTO drinks_instances (order_id, drink_id, size) VALUES (%s,%s,%s)'  # Inserts previous elements into drinks_instances table.
            data = (order_id, drink_id, size)
            execute_query(db_connection, query, data)
            print("Added new drinks_instances!")# Prints to console that a new drink was added.
            return redirect(url_for('order'))
        # Add Toppings to Drinks
        if 'instance_id' and 'topping_id' in request.form:  # looks for instance_id and topping_id in the form, then proceeds.
            db_connection = connect_to_database()
            instance_id = request.form['instance_id']  # Searches for element named instance_id to use as instance_id in the query.
            topping_id = request.form['topping_id']  # Searches for element named topping_id to use as topping_id in the query.
            query = 'INSERT INTO drinks_toppings (instance_id, topping_id) VALUES (%s,%s)'  # Inserts previous elements into drinks_toppings table.
            data = (instance_id, topping_id)
            execute_query(db_connection, query, data)
            print("Added new drinks_toppings!")  # Prints to console that a new drink was added.
            return redirect(url_for('order'))
    # All Get Requests
    elif request.method == 'GET':  # Get Forms.
        return redirect(url_for('order'))

# ------ DRINKS ------
# SELECT for Drinks Page | Shows the Drinks Table.
@webapp.route('/drink')
def drink():
    db_connection = connect_to_database()
    print("Executing a query on the database using the credentials from db_credentials.py: base_drinks table, select all columns.")  # Prints message to console.
    query = "SELECT * FROM base_drinks;"
    data = execute_query(db_connection, query)
    return render_template('drink.html', data = data)

# CREATE for Drinks Page
@webapp.route('/drink', methods=['POST', 'GET'])
def base_drinks_create():
    # All Post Requests
    if request.method == 'POST':  # Post Forms.
        db_connection = connect_to_database()
        drink_name = request.form['drink_name']  # Searches for element named drink_name to use as drink_name in the query.
        small_cost = request.form['small_cost']  # Searches for element named small_cost to use as small_cost in the query.
        medium_cost = request.form['medium_cost']  # Searches for element named medium_cost to use as medium_cost in the query.
        large_cost = request.form['large_cost']  # Searches for element named large_cost to use as large_cost in the query.
        query = 'INSERT INTO base_drinks (drink_name, small_cost, medium_cost, large_cost) VALUES (%s,%s,%s,%s)'  # Inserts previous elements into base_drinks table.
        print("Added new drink!")  # Prints to console that a new drink was added.
        data = (drink_name, small_cost, medium_cost, large_cost)
        execute_query(db_connection, query, data)
        return redirect(url_for('drink'))
    # All Get Requests
    elif request.method == 'GET':  # Get Forms.
        return redirect(url_for('order'))

# ------ TOPPINGS ------
# SELECT for Topping Page | Shows the Topping Table.
@webapp.route('/topping')
def topping():
    db_connection = connect_to_database()
    print("Executing a query on the database using the credentials from db_credentials.py: toppings table, select all columns.")  # Prints message to console.
    query = "SELECT * FROM toppings;"
    data = execute_query(db_connection, query)
    return render_template('topping.html', data = data)

# CREATE for Topping Page
@webapp.route('/topping', methods=['POST', 'GET'])
def topping_create():
    # All Post Requests
    if request.method == 'POST':  # Post Forms.
        db_connection = connect_to_database()
        topping_name = request.form['topping_name']  # Searches for element named topping_name to use as topping_name in the query.
        total_cost = request.form['total_cost']  # Searches for element named total_cost to use as total_cost in the query.
        query = 'INSERT INTO toppings (topping_name, total_cost) VALUES (%s,%s)'  # Inserts previous elements into toppings table.
        data = (topping_name, total_cost)
        print("Added new topping!")  # Prints to console that a new topping was added.
        execute_query(db_connection, query, data)
        return redirect(url_for('topping'))
    # All Get Requests
    elif request.method == 'GET':  # Get Forms.
        return redirect(url_for('order'))

# ------ CUSTOMERS ------
# SELECT for Customer Page | Shows the Customer Table.
@webapp.route('/customer')
def customer():
    db_connection = connect_to_database()
    print("Executing a query on the database using the credentials from db_credentials.py: customer table, select all columns.")  # Prints message to console.
    query = "SELECT * FROM customers;"
    data = execute_query(db_connection, query)
    return render_template('customer.html', data = data)

# CREATE for Customer Page
@webapp.route('/customer', methods=['POST', 'GET'])
def customer_create():
    # All Post Requests
    if request.method == 'POST':  # Post Forms.
        db_connection = connect_to_database()
        first_name = request.form['first_name']  # Searches for element named first_name to use as first_name in the query.
        last_name = request.form['last_name']  # Searches for element named last_name to use as last_name in the query.
        phone_number = request.form['phone_number']  # Searches for element named phone_number to use as phone_number in the query.
        query = 'INSERT INTO customers (first_name, last_name, phone_number) VALUES (%s,%s,%s)'  # Inserts previous elements into customers table.
        data = (first_name, last_name, phone_number)
        print("Added new customer!") # Prints to console that a new customer was added.
        execute_query(db_connection, query, data)
        return redirect(url_for('customer'))
    # All Get Requests
    elif request.method == 'GET':  # Get Forms.
        return redirect(url_for('order'))


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

from django.db import migrations, models
import filebrowser.fields

class Migration(migrations.Migration):

    dependencies = [
        ('filebrowser', '0002_auto_20180301_2039'),
    ]

    operations = [
        migrations.AddField(
            model_name='filedescription',
            name='file_path_hash',
            field=models.CharField(db_index=True, max_length=64, editable=False, blank=True),
        ),
        migrations.AlterField(
            model_name='filedescription',
            name='file_path',
            field=filebrowser.fields.FileBrowseField(max_length=500, verbose_name='File path'),
        ),
    ]


import time
from concurrent.futures import ThreadPoolExecutor

import tornado.web
import tornado.escape
import tornado.ioloop
import tornado.httpserver
from tornado import gen
from tornado.concurrent import Future, run_on_executor
from tornado.httpclient import AsyncHTTPClient
from tornado.options import define, options, parse_command_line
from tornado.log import app_log

class IndexBadHandler(tornado.web.RequestHandler):
    """
    asynchronous装饰器是让请求变成长连接的方式,
    必须手动调用 self.finish() 才会响应,
    如果没有指定结束, 该长连接会一直保持直到 pending 状态.
    """
    @tornado.web.asynchronous
    def get(self):
        self.write("
Hello, world
")

class IndexGoodHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.write("
Hello, world
")
        self.finish()

class AsyncHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        http_client = AsyncHTTPClient()
        http_client.fetch("http://example.com",
                          callback=self.on_fetch)

    def on_fetch(self, response):
        # do_something_with_response(response)
        self.render("template.html")

class GenAsyncHandler1(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        http_client = AsyncHTTPClient()
        response = yield http_client.fetch("http://example.com")
        # do_something_with_response(response)
        self.render("template.html")

class GenAsyncHandler2(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        http_client = AsyncHTTPClient()
        url1, url2, url3, url4 = ['http://example.com',
                                  'http://example.com',
                                  'http://example.com',
                                  'http://example.com']

        response1, response2 = yield [http_client.fetch(url1),
                                      http_client.fetch(url2)]

        response_dict = yield dict(response3=http_client.fetch(url3),
                                   response4=http_client.fetch(url4))
        response3 = response_dict['response3']
        response4 = response_dict['response4']

class GenAsyncHandler3(tornado.web.RequestHandler):
    @gen.coroutine
    def add(self, a, b):
        val = a + b
        yield gen.sleep(2.0)
        return val

    @gen.coroutine
    def get(self):
        a = self.get_query_argument('a', 0)
        b = self.get_query_argument('b', 0)
        self.write("
Result:
")
        val = yield self.add(a, b)
        self.write(str(val))

class NoBlockingHandler(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        yield gen.sleep(10)
        self.write('
No Blocking Request
')

class BlockingHandler(tornado.web.RequestHandler):
    def get(self):
        time.sleep(10)
        self.write('
Blocking Request
')

class NoBlockingThreadHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(2)

    @run_on_executor
    def sleep(self, seconds):
        time.sleep(seconds)
        return seconds

    @gen.coroutine
    def get(self):
        second = yield self.sleep(5)
        self.write(f'noBlocking Request: {second}')

settings = dict(
    debug=True
)

url_patterns = [
    (r"/bad", IndexBadHandler),
    (r"/good", IndexGoodHandler),
    (r"/async", AsyncHandler),
    (r"/coroutine1", GenAsyncHandler1),
    (r"/coroutine2", GenAsyncHandler2),
    (r"/coroutine3", GenAsyncHandler3),
    (r"/noblocking", NoBlockingHandler),
    (r"/blocking", BlockingHandler),
    (r"/noblockingthread", NoBlockingThreadHandler),
]

class Application(tornado.web.Application):
    def __init__(self):
        super(Application, self).__init__(url_patterns, **settings)

def main():
    define("port", default=8000, help="run on the given port", type=int)
    parse_command_line()

    app = Application()
    server = tornado.httpserver.HTTPServer(app)
    server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()


import requests
import json
import sqlite3
import datetime

#sqlite3 config
con = sqlite3.connect("/var/www/html/database/smart_bus3.db")
sqlite3.Connection

cursor = con.cursor()
cursor.execute("SELECT * FROM enroll_scan_beacon WHERE transmission is null")
enroll_rows = cursor.fetchall()

try:
    if enroll_rows:

        print("now enroll beacon data exist")
        cursor = con.cursor()
        cursor.execute("SELECT config_value FROM gateway_config WHERE config_property='center_id'")
        rows_c = cursor.fetchall()
        center = rows_c[0][0]

        cursor = con.cursor()
        cursor.execute("SELECT config_value FROM gateway_config WHERE config_property='gateway'")
        rows_g = cursor.fetchall()
        gateway = rows_g[0][0]

        data='{"enroll_beacon":['

        for i in range(0,len(enroll_rows)):
            data+='{'
            data+='"gateway":"'+ gateway +'",'
            data+='"center":"'+ center +'",'
            data+='"createtime":"'+ enroll_rows[i][0] +'",'
            data+='"mac_address":"'+ enroll_rows[i][1] +'",'
            data+='"uuid":"'+ enroll_rows[i][2] +'",'
            data+='"major":'+ str(enroll_rows[i][3]) +','
            data+='"minor":'+ str(enroll_rows[i][4]) +','
            data+='"rssi":'+ str(enroll_rows[i][5]) +','
            data+='"tx_power":'+ str(enroll_rows[i][6]) +''
            if i < len(enroll_rows)-1:
                data+='},'
            else:
                data+='}'

        data+=']}'

        for i in range(0,len(enroll_rows)):
            cursor = con.cursor()
            cursor.execute("UPDATE enroll_scan_beacon SET transmission = 1 WHERE createtime = '"+ enroll_rows[i][0] +"' AND mac_address  = '"+ enroll_rows[i][1] +"'")
            con.commit()

        url = "http://192.168.0.20:8080/gateway/enrollbeacon_insert"
        res = requests.post(url, data=(data))

        print("=========")
        print(data)

        print("enroll beacon data gateway to server")

    else:
        print("no enroll beacon data")

finally:
    con.close()
    print(" == enroll scan gateway to server end == ")


import os
from pathlib import Path

from .proto.pps.pps_pb2 import Transform, CreatePipelineRequest, BuildSpec

from google.protobuf import json_format

# Default script for running python code with wheels in a pipeline that was
# deployed with `create_python_pipeline`.
RUNNER_SCRIPT_WITH_WHEELS = """
#!/bin/sh
set -{set_args}

cd /pfs/{source_repo_name}
pip install /pfs/{build_pipeline_name}/*.whl
python main.py
"""

# Default script for running python code without wheels in a pipeline that was
# deployed with `create_python_pipeline`.
RUNNER_SCRIPT_WITHOUT_WHEELS = """
#!/bin/sh
set -{set_args}

cd /pfs/{source_repo_name}
python main.py
"""

# Default script for building python wheels for a pipeline that was deployed
# with `create_python_pipeline`.
BUILDER_SCRIPT = """
#!/bin/sh
set -{set_args}
python --version
pip --version

cd /pfs/{source_repo_name}
test -f requirements.txt && pip wheel -r requirements.txt -w /pfs/out
"""

def put_files(client, source_path, commit, dest_path, **kwargs):
    """
    Utility function for recursively inserting files from the local
    `source_path` to pachyderm. Roughly equivalent to `pachctl put file -r`.

    Params:

    * `client`: The `Client` instance to use.
    * `source_path`: The directory to recursively insert content from.
    * `commit`: The `Commit` object to use for inserting files.
    * `dest_path`: The destination path in PFS.
    * `kwargs`: Keyword arguments to forward. See
    `PutFileClient.put_file_from_fileobj` for details.
    """

    with client.put_file_client() as pfc:
        for root, _, filenames in os.walk(source_path):
            for filename in filenames:
                source_filepath = os.path.join(root, filename)
                dest_filepath = os.path.join(dest_path, os.path.relpath(source_filepath, start=source_path))
                pfc.put_file_from_filepath(commit, dest_filepath, source_filepath, **kwargs)

def create_python_pipeline(client, path, input=None, pipeline_name=None, image_pull_secrets=None,
                           debug=None, env=None, secrets=None, image=None, update=False,
                           **pipeline_kwargs):
    """
    Utility function for creating (or updating) a pipeline specially built for
    executing python code that is stored locally at `path`.

    A normal pipeline creation process requires you to first build and push a
    container image with the source and dependencies baked in. As an alternative
    process, this function circumvents container image creation by using build
    step-enabled pipelines. See the pachyderm core docs for more info.

    If `path` references a directory, it should have:

    * A `main.py`, as the pipeline entry-point.
    * An optional `requirements.txt` that specifies pip requirements.

    Params:

    * `client`: The `Client` instance to use.
    * `path`: The directory containing the python pipeline source, or an
    individual python file.
    * `input`: An optional `Input` object specifying the pipeline input.
    * `pipeline_name`: An optional string specifying the pipeline name.
    Defaults to using the last directory name in `path`.
    * `image_pull_secrets`: An optional list of strings specifying the
    pipeline transform's image pull secrets, which are used for pulling images
    from a private registry. Defaults to `None`, in which case the public
    docker registry will be used. See the pipeline spec document for more
    details.
    * `debug`: An optional bool specifying whether debug logging should be
    enabled for the pipeline. Defaults to `False`.
    * `env`: An optional mapping of string keys to string values specifying
    custom environment variables.
    * `secrets`: An optional list of `Secret` objects for secret environment
    variables.
    * `image`: An optional string specifying the docker image to use for the
    pipeline. Defaults to using pachyderm's official python language builder.
    * `update`: Whether to act as an upsert.
    * `pipeline_kwargs`: Keyword arguments to forward to `create_pipeline`.
    """

    return client.create_pipeline(
        pipeline_name or Path(path).name,
        Transform(
            image_pull_secrets=image_pull_secrets,
            debug=debug,
            env=env,
            secrets=secrets,
            build=BuildSpec(path=path, image=image) if image else BuildSpec(path=path, language="python"),
        ),
        update=update,
        input=input,
        **pipeline_kwargs
    )

def parse_json_pipeline_spec(j):
    """
    Parses a string of JSON into a `CreatePipelineRequest` protobuf.
    """
    return json_format.Parse(j, CreatePipelineRequest())

def parse_dict_pipeline_spec(d):
    """
    Parses a dict of serialized JSON into a `CreatePipelineRequest` protobuf.
    """
    return json_format.ParseDict(d, CreatePipelineRequest())


import json
import os
from ibm_watson import VisualRecognitionV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from models.red_rhino_robot import alert_3r
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
authenticator = IAMAuthenticator(os.getenv("WATSON_VISUAL_RECOG_API_KEY"))

COMBUSTIBLE_OBJ={'plant', 'grass', 'wood', 'paper', 'cardboard', 'cigarette', 'leaf', 'leaves', 'flame', 'fire'}
ALERT_OBJ={'fire', 'flame', 'combustion'}

def visual_recog(frame:str, location_name:str):
    """ IBM Watson visual recognition model"""
    visual_recognition = VisualRecognitionV3(
        version='2018-03-19',
        authenticator=authenticator
    )

    visual_recognition.set_service_url(os.getenv("SERVICE_URL"))

    url = frame
    classes_result = visual_recognition.classify(url=url).get_result()

    pred_classes=set()
    for item in classes_result['images'][0]['classifiers'][0]['classes']:
        pred_classes.add(item['class'])

    # Check for occurrence of fire and activate red rhino robot accordingly
    if(pred_classes & ALERT_OBJ):
        alert_3r(location_name)

    # Check for combustible
    if (pred_classes & COMBUSTIBLE_OBJ):
        return 1
    else:
        return 0


# encoding: utf-8
import os
import logging
import pandas
import pickle
import numpy
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.metrics import roc_auc_score, precision_score, recall_score, accuracy_score
from sklearn.grid_search import GridSearchCV, ParameterGrid
from sklearn.metrics import matthews_corrcoef
from sklearn.cross_validation import StratifiedKFold

APP_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../')
DATA_DIR = os.path.join(APP_ROOT, 'data')

TRAIN_DATA = os.path.join(DATA_DIR, 'train_simple_join.csv.gz')
TEST_DATA = os.path.join(DATA_DIR, 'test_simple_join.csv.gz')
TARGET_COLUMN_NAME = u'Response'

from utils import mcc_optimize, evalmcc_xgb_min
from feature import LIST_FEATURE_COLUMN_NAME
log_fmt = '%(asctime)s %(name)s %(lineno)d [%(levelname)s][%(funcName)s] %(message)s '
logging.basicConfig(format=log_fmt,
                    datefmt='%Y-%m-%d/%H:%M:%S',
                    level='INFO')
logger = logging.getLogger(__name__)

def mcc(y_true, y_pred):
    n = y_true.shape[0]
    true_pos = sum(1. for i in range(n) if y_true[i] == 1 and y_pred[i] == 1)
    true_neg = sum(1. for i in range(n) if y_true[i] == 0 and y_pred[i] == 0)
    false_pos = sum(1. for i in range(n) if y_true[i] == 0 and y_pred[i] == 1)
    false_neg = sum(1. for i in range(n) if y_true[i] == 1 and y_pred[i] == 0)
    """
    s = (true_pos + false_neg) / n
    p = (true_pos + false_pos) / n

    a = true_pos / n - s * p
    b = numpy.sqrt(s * p * (1 - s) * (1 - p))
    """
    a = true_pos * true_neg - false_pos * false_neg
    b = (true_pos + false_pos) * (true_pos + false_neg) * (true_neg + false_pos) * (true_neg + false_neg)
    return a / numpy.sqrt(b)

def mcc_scoring(estimator, X, y):

    y_pred_prb = estimator.predict_proba(X)[:, 1]
    list_thresh = numpy.arange(1, 100) / 100
    max_score = -1
    idx = None
    for thresh in list_thresh:
        y_pred = numpy.where(y_pred_prb >= thresh, 1, 0)
        score = mcc(y, y_pred)
        logger.debug('thresh: %s, score: %s' % (thresh, score))
        if score > max_score:
            max_score = score
            idx = thresh
    return max_score

from numba.decorators import jit

@jit
def mcc_scoring2(y_pred_prb, y):
    list_thresh = numpy.arange(1, 100) / 100
    max_score = -1
    idx = None
    for thresh in list_thresh:
        y_pred = numpy.where(y_pred_prb >= thresh, 1, 0)
        score = mcc(y, y_pred)
        if score > max_score:
            max_score = score
            idx = thresh
    return idx, max_score

if __name__ == '__main__':
    logger.info('load start')
    target = pandas.read_csv('stack_1_target_1.csv')['0'].values
    data = pandas.read_csv('stack_1_data_1.csv').values
    logger.info('load end')
    logger.info('shape %s %s' % data.shape)
    logger.info('shape %s' % target.shape)
    logger.info('pos num: %s, pos rate: %s' % (sum(target), float(sum(target)) / target.shape[0]))

    params = {'subsample': 1, 'learning_rate': 0.1, 'colsample_bytree': 0.3,
              'max_depth': 3, 'min_child_weight': 0.01, 'n_estimators': 200,
              'scale_pos_weight': 10}

    # 4/8 param: {'learning_rate': 0.1, 'colsample_bytree': 1, 'scale_pos_weight': 1, 'n_estimators': 100, 'subsample': 1, 'min_child_weight': 1, 'max_depth': 4}
    # 2016-09-27/15:59:07 __main__ 132 [INFO][<module>] thresh:
    # 0.225158065557, total score: 0.264650750521, max_score: 0.264650750521

    all_params = {'max_depth': [3, 5, 7],
                  'n_estimators': [50, 100, 200],
                  'learning_rate': [0.1],
                  'min_child_weight': [1],
                  'subsample': [1],
                  'reg_alpha': [0, 0.1, 0.01],
                  'colsample_bytree': [1],
                  'scale_pos_weight': [1]}
    _all_params = {'C': [10**i for i in range(-3, 2)],
                   'penalty': ['l2']}
    cv = StratifiedKFold(target, n_folds=5, shuffle=True, random_state=0)
    list_score = []
    max_score = -100
    best_thresh = None
    pg = list(ParameterGrid(all_params))
    for i, params in enumerate(pg):
        logger.info('%s/%s param: %s' % (i + 1, len(pg), params))
        pred_proba_all = []
        y_true = []
        for train_idx, test_idx in cv:
            model = XGBClassifier(seed=0)
            #model = LogisticRegression(n_jobs=-1, class_weight='balanced')
            model.set_params(**params)

            model.fit(data[train_idx], target[train_idx],
                      eval_metric=evalmcc_xgb_min,
                      verbose=False)

            #pred_proba = data[test_idx, -1]
            pred_proba = model.predict_proba(data[test_idx])[:, 1]
            pred_proba_all = numpy.r_[pred_proba_all, pred_proba]
            y_true = numpy.r_[y_true, target[test_idx]]
            score = roc_auc_score(target[test_idx], pred_proba)
            #logger.info('    score: %s' % score)
            #thresh, score = mcc_scoring(model, data[test_idx], target[test_idx])
            list_score.append(score)
            #logger.info('    thresh: %s' % thresh)
        score = numpy.mean(list_score)
        thresh, score = mcc_optimize(pred_proba_all, y_true)
        max_score = max(max_score, score)
        logger.info('thresh: %s, total score: %s, max_score: %s' % (thresh, score, max_score))
        if max_score == score:
            best_param = params
            best_thresh = thresh
    logger.info('best_thresh: %s, total max score: %s' % (best_thresh, max_score))
    model = XGBClassifier(seed=0)
    #model = LogisticRegression(n_jobs=-1, class_weight='balanced')
    model.set_params(**best_param)
    model.fit(data[train_idx], target[train_idx],
              eval_metric=evalmcc_xgb_min,
              verbose=False)

    with open('stack_model_1.pkl', 'wb') as f:
        pickle.dump(model, f, -1)


"""URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from __future__ import print_function
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path

from drf_spectacular.views import (
    SpectacularAPIView,
    SpectacularRedocView,
    SpectacularSwaggerView,
)

# Custom admin header
admin.site.site_header = "OpenFisca Django-API Administration"

urlpatterns = [
    path("api/", include(("api.urls", "api"), namespace="api")),
    path("entities/", include(("entities.urls", "entities"), namespace="entities")),
    path("variables/", include(("variables.urls", "variables"), namespace="variables")),
    url(r"^admin/", admin.site.urls),
    path("plots/", include(("plots.urls", "plots"), namespace="plots")),
    path(
        "activities/",
        include(("activities.urls", "activities"), namespace="activities"),
    ),
    # SWAGGER PATHS
    path(
        "",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
    path(
        "schema/",
        SpectacularAPIView.as_view(),
        name="schema",
    ),
    path(
        "api/",
        SpectacularSwaggerView.as_view(url_name="api-schema"),
        name="swagger-ui",
    ),
    path(
        "api/schema/", SpectacularAPIView.as_view(urlconf="api.urls"), name="api-schema"
    ),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

# Host the static from uWSGI
if settings.IS_WSGI:
    print("uWSGI mode, adding static file patterns")
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()

# Add debug toolbar
if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
        path("__debug__/", include(debug_toolbar.urls)),
    ] + urlpatterns


import dataclasses
from collections import namedtuple
from typing import (
    Any,
    AsyncGenerator,
    AsyncIterable,
    AsyncIterator,
    Generator,
    Iterable,
    Iterator,
    List,
    Optional,
    cast,
)
from typing_extensions import Annotated, Self

import strawberry
from strawberry import relay
from strawberry.permission import BasePermission
from strawberry.relay.utils import to_base64
from strawberry.types import Info

@strawberry.type
class Fruit(relay.Node):
    id: relay.NodeID[int]
    name: str
    color: str

    @classmethod
    def resolve_nodes(
        cls,
        *,
        info: Info[None, None],
        node_ids: Iterable[str],
        required: bool = False,
    ) -> Iterable[Optional[Self]]:
        if node_ids is not None:
            return [fruits[nid] if required else fruits.get(nid) for nid in node_ids]

        return fruits.values()

    @classmethod
    def is_type_of(cls, obj: Any, _info: Info[None, None]) -> bool:
        # This is here to support FruitConcrete, which is mimicing an integration
        # object which would return an object alike Fruit (e.g. the django integration)
        return isinstance(obj, (cls, FruitConcrete))

@dataclasses.dataclass
class FruitConcrete:
    id: int
    name: str
    color: str

@strawberry.type
class FruitAsync(relay.Node):
    id: relay.NodeID[int]
    name: str
    color: str

    @classmethod
    async def resolve_nodes(
        cls,
        *,
        info: Optional[Info[None, None]] = None,
        node_ids: Iterable[str],
        required: bool = False,
    ) -> Iterable[Optional[Self]]:
        if node_ids is not None:
            return [
                fruits_async[nid] if required else fruits_async.get(nid)
                for nid in node_ids
            ]

        return fruits_async.values()

    @classmethod
    async def resolve_id(cls, root: Self, *, info: Info[None, None]) -> str:
        return str(root.id)

@strawberry.type
class FruitCustomPaginationConnection(relay.Connection[Fruit]):
    @strawberry.field
    def something(self) -> str:
        return "foobar"

    @classmethod
    def resolve_connection(
        cls,
        nodes: Iterable[Fruit],
        *,
        info: Optional[Info[None, None]] = None,
        total_count: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        first: Optional[int] = None,
        last: Optional[int] = None,
        **kwargs: Any,
    ) -> Self:
        edges_mapping = {
            to_base64("fruit_name", n.name): relay.Edge(
                node=n,
                cursor=to_base64("fruit_name", n.name),
            )
            for n in sorted(nodes, key=lambda f: f.name)
        }
        edges = list(edges_mapping.values())
        first_edge = edges[0] if edges else None
        last_edge = edges[-1] if edges else None

        if after is not None:
            after_edge_idx = edges.index(edges_mapping[after])
            edges = [e for e in edges if edges.index(e) > after_edge_idx]

        if before is not None:
            before_edge_idx = edges.index(edges_mapping[before])
            edges = [e for e in edges if edges.index(e) < before_edge_idx]

        if first is not None:
            edges = edges[:first]

        if last is not None:
            edges = edges[-last:]

        return cls(
            edges=edges,
            page_info=relay.PageInfo(
                start_cursor=edges[0].cursor if edges else None,
                end_cursor=edges[-1].cursor if edges else None,
                has_previous_page=first_edge is not None
                and bool(edges)
                and edges[0] != first_edge,
                has_next_page=last_edge is not None
                and bool(edges)
                and edges[-1] != last_edge,
            ),
        )

fruits = {
    str(f.id): f
    for f in [
        Fruit(id=1, name="Banana", color="yellow"),
        Fruit(id=2, name="Apple", color="red"),
        Fruit(id=3, name="Pineapple", color="yellow"),
        Fruit(id=4, name="Grape", color="purple"),
        Fruit(id=5, name="Orange", color="orange"),
    ]
}
fruits_async = {
    k: FruitAsync(id=v.id, name=v.name, color=v.color) for k, v in fruits.items()
}

FruitAlike = namedtuple("FruitAlike", ["id", "name", "color"])

@strawberry.type
class FruitAlikeConnection(relay.ListConnection[Fruit]):
    @classmethod
    def resolve_node(
        cls, node: FruitAlike, *, info: Info[None, None], **kwargs: Any
    ) -> Fruit:
        return Fruit(
            id=node.id,
            name=node.name,
            color=node.color,
        )

def fruits_resolver() -> Iterable[Fruit]:
    return fruits.values()

async def fruits_async_resolver() -> Iterable[FruitAsync]:
    return fruits_async.values()

class DummyPermission(BasePermission):
    message = "Dummy message"

    async def has_permission(self, source: Any, info: Info, **kwargs: Any) -> bool:
        return True

@strawberry.type
class Query:
    node: relay.Node = relay.node()
    node_with_async_permissions: relay.Node = relay.node(
        permission_classes=[DummyPermission]
    )
    nodes: List[relay.Node] = relay.node()
    node_optional: Optional[relay.Node] = relay.node()
    nodes_optional: List[Optional[relay.Node]] = relay.node()
    fruits: relay.ListConnection[Fruit] = relay.connection(resolver=fruits_resolver)
    fruits_lazy: relay.ListConnection[
        Annotated["Fruit", strawberry.lazy("tests.relay.schema")]
    ] = relay.connection(resolver=fruits_resolver)
    fruits_async: relay.ListConnection[FruitAsync] = relay.connection(
        resolver=fruits_async_resolver
    )
    fruits_custom_pagination: FruitCustomPaginationConnection = relay.connection(
        resolver=fruits_resolver
    )

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_concrete_resolver(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> List[Fruit]:
        # This is mimicing integrations, like Django
        return [
            cast(
                Fruit,
                FruitConcrete(
                    id=f.id,
                    name=f.name,
                    color=f.color,
                ),
            )
            for f in fruits.values()
            if name_endswith is None or f.name.endswith(name_endswith)
        ]

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_custom_resolver(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> List[Fruit]:
        return [
            f
            for f in fruits.values()
            if name_endswith is None or f.name.endswith(name_endswith)
        ]

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_custom_resolver_lazy(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> List[Annotated["Fruit", strawberry.lazy("tests.relay.schema")]]:
        return [
            f
            for f in fruits.values()
            if name_endswith is None or f.name.endswith(name_endswith)
        ]

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_custom_resolver_iterator(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> Iterator[Fruit]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_custom_resolver_iterable(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> Iterator[Fruit]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(relay.ListConnection[Fruit])
    def fruits_custom_resolver_generator(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> Generator[Fruit, None, None]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(relay.ListConnection[Fruit])
    async def fruits_custom_resolver_async_iterable(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> AsyncIterable[Fruit]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(relay.ListConnection[Fruit])
    async def fruits_custom_resolver_async_iterator(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> AsyncIterator[Fruit]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(relay.ListConnection[Fruit])
    async def fruits_custom_resolver_async_generator(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> AsyncGenerator[Fruit, None]:
        for f in fruits.values():
            if name_endswith is None or f.name.endswith(name_endswith):
                yield f

    @relay.connection(FruitAlikeConnection)
    def fruit_alike_connection_custom_resolver(
        self,
        info: Info[None, None],
        name_endswith: Optional[str] = None,
    ) -> List[FruitAlike]:
        return [
            FruitAlike(f.id, f.name, f.color)
            for f in fruits.values()
            if name_endswith is None or f.name.endswith(name_endswith)
        ]

    @strawberry.relay.connection(strawberry.relay.ListConnection[Fruit])
    def some_fruits(self) -> List[Fruit]:
        return [Fruit(id=x, name="apple", color="green") for x in range(200)]

@strawberry.type
class CreateFruitPayload:
    fruit: Fruit

@strawberry.type
class Mutation:
    @strawberry.mutation
    def create_fruit(
        self,
        info: Info[None, None],
        name: str,
        color: str,
    ) -> CreateFruitPayload:
        ...

schema = strawberry.Schema(query=Query, mutation=Mutation)


import os
import worker
import mrcnn.model as mrcnn
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss
import dataReader as dr
import cfs_coco_train as coco
from keras import backend as K

ROOT_DIR = os.path.abspath("../../")
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

#
# mrcnn_class_logits
# mrcnn_class
# mrcnn_bbox_fc
# mrcnn_bbox
# roi_align_classifier
# mrcnn_class_conv1
# mrcnn_class_bn1
# mrcnn_class_conv2
# mrcnn_class_bn2
# pool_squeeze
#
# head structure
train_conv_layers = ['fpn_c5p5', 'fpn_c4p4', 'fpn_c3p3', 'fpn_c2p2', 'fpn_p5', 'fpn_p2', 'fpn_p3', 'fpn_p4']
train_dense_layers = ['mrcnn_mask_conv1', 'mrcnn_mask_conv2', 'mrcnn_mask_conv3', 'mrcnn_mask_conv4',
                      'mrcnn_bbox_fc', 'mrcnn_mask_deconv', 'mrcnn_class_logits', 'mrcnn_mask']
train_normal_layers = ['mrcnn_mask_bn1', 'mrcnn_mask_bn2', 'mrcnn_mask_bn3', 'mrcnn_mask_bn4']

train_rpn_model = 'rpn_model'
# resnet structure
train_resnet_conv = ['conv1',
                     'res2a_branch2a', 'res2a_branch2b', 'res2a_branch2c',
                     'res2a_branch1',
                     'res2b_branch2a', 'res2b_branch2b', 'res2b_branch2c',
                     'res2c_branch2a', 'res2c_branch2b', 'res2c_branch2c',

                     'res3a_branch2a', 'res3a_branch2b', 'res3a_branch2c',
                     'res3a_branch1',
                     'res3b_branch2a', 'res3b_branch2b', 'res3b_branch2c',
                     'res3c_branch2a', 'res3c_branch2b', 'res3c_branch2c',

                     'res4a_branch2a', 'res4a_branch2b', 'res4a_branch2c',
                     'res4a_branch1',
                     'res4b_branch2a', 'res4b_branch2b', 'res4b_branch2c',
                     'res4c_branch2a', 'res4c_branch2b', 'res4c_branch2c',
                     'res4d_branch2a', 'res4d_branch2b', 'res4d_branch2c',
                     'res4e_branch2a', 'res4e_branch2b', 'res4e_branch2c',
                     'res4f_branch2a', 'res4f_branch2b', 'res4f_branch2c',
                     'res4g_branch2a', 'res4g_branch2b', 'res4g_branch2c',
                     'res4h_branch2a', 'res4h_branch2b', 'res4h_branch2c',
                     'res4i_branch2a', 'res4i_branch2b', 'res4i_branch2c',
                     'res4j_branch2a', 'res4j_branch2b', 'res4j_branch2c',
                     'res4k_branch2a', 'res4k_branch2b', 'res4k_branch2c',
                     'res4l_branch2a', 'res4l_branch2b', 'res4l_branch2c',
                     'res4m_branch2a', 'res4m_branch2b', 'res4m_branch2c',
                     'res4n_branch2a', 'res4n_branch2b', 'res4n_branch2c',
                     'res4o_branch2a', 'res4o_branch2b', 'res4o_branch2c',
                     'res4p_branch2a', 'res4p_branch2b', 'res4p_branch2c',
                     'res4q_branch2a', 'res4q_branch2b', 'res4q_branch2c',
                     'res4r_branch2a', 'res4r_branch2b', 'res4r_branch2c',
                     'res4s_branch2a', 'res4s_branch2b', 'res4s_branch2c',
                     'res4t_branch2a', 'res4t_branch2b', 'res4t_branch2c',
                     'res4u_branch2a', 'res4u_branch2b', 'res4u_branch2c',
                     'res4v_branch2a', 'res4v_branch2b', 'res4v_branch2c',
                     'res4w_branch2a', 'res4w_branch2b', 'res4w_branch2c',

                     'res5a_branch2a', 'res5a_branch2b', 'res5a_branch2c',
                     'res5a_branch1',
                     'res5b_branch2a', 'res5b_branch2b', 'res5b_branch2c',
                     'res5c_branch2a', 'res5c_branch2b', 'res5c_branch2c',
                     ]
train_resnet_conv_a = [
    'res2a_branch2a',
    'res2b_branch2a',
    'res2c_branch2a',
    'res3a_branch2a',
    'res3b_branch2a',
    'res3c_branch2a',
    'res4a_branch2a',

    'res4b_branch2a',
    'res4c_branch2a',
    'res4d_branch2a',
    'res4e_branch2a',
    'res4f_branch2a',
    'res4g_branch2a',
    'res4h_branch2a',
    'res4i_branch2a',
    'res4j_branch2a',
    'res4k_branch2a',
    'res4l_branch2a',
    'res4m_branch2a',
    'res4n_branch2a',
    'res4o_branch2a',
    'res4p_branch2a',
    'res4q_branch2a',
    'res4r_branch2a',
    'res4s_branch2a',
    'res4t_branch2a',
    'res4u_branch2a',
    'res4v_branch2a',
    'res4w_branch2a',

    'res5a_branch2a',
    'res5b_branch2a',
    'res5c_branch2a',
]
train_resnet_conv_b = [
    'res2a_branch2b',
    'res2b_branch2b',
    'res2c_branch2b',

    'res3a_branch2b',
    'res3b_branch2b',
    'res3c_branch2b',
    'res4a_branch2b',
    'res4b_branch2b',
    'res4c_branch2b',
    'res4d_branch2b',
    'res4e_branch2b',
    'res4f_branch2b',
    'res4g_branch2b',
    'res4h_branch2b',
    'res4i_branch2b',
    'res4j_branch2b',
    'res4k_branch2b',
    'res4l_branch2b',
    'res4m_branch2b',
    'res4n_branch2b',
    'res4o_branch2b',
    'res4p_branch2b',
    'res4q_branch2b',
    'res4r_branch2b',
    'res4s_branch2b',
    'res4t_branch2b',
    'res4u_branch2b',
    'res4v_branch2b',
    'res4w_branch2b',

    'res5a_branch2b',
    'res5b_branch2b',
    'res5c_branch2b',
]
train_resnet_conv_c = [
    'res2a_branch2c',
    'res2b_branch2c',
    'res2c_branch2c',

    'res3a_branch2c',
    'res3b_branch2c',
    'res3c_branch2c',

    'res4a_branch2c',
    'res4b_branch2c',
    'res4c_branch2c',
    'res4d_branch2c',
    'res4e_branch2c',
    'res4f_branch2c',
    'res4g_branch2c',
    'res4h_branch2c',
    'res4i_branch2c',
    'res4j_branch2c',
    'res4k_branch2c',
    'res4l_branch2c',
    'res4m_branch2c',
    'res4n_branch2c',
    'res4o_branch2c',
    'res4p_branch2c',
    'res4q_branch2c',
    'res4r_branch2c',
    'res4s_branch2c',
    'res4t_branch2c',
    'res4u_branch2c',
    'res4v_branch2c',
    'res4w_branch2c',

    'res5a_branch2c',
    'res5b_branch2c',
    'res5c_branch2c',
]
train_resnet_conv_r = ['conv1',
                       'res2a_branch1',
                       'res3a_branch1',
                       'res4a_branch1',
                       'res5a_branch1',
                       ]

def load_weight(path, config):
    # Create model
    model1 = mrcnn.MaskRCNN(mode="training", config=config, model_dir=DEFAULT_LOGS_DIR)
    model1.load_weights(path, by_name=True)
    return model1

def compare_two_denselayer(model1, model2, layer_name):
    weight1 = model1.keras_model.get_layer(layer_name).get_weights()
    weight2 = model2.keras_model.get_layer(layer_name).get_weights()

    arw1 = np.array(weight1)
    arw2 = np.array(weight2)

    if (len(arw1) != len(arw2)):
        return 0
    compare_two_matrix(arw1[0], arw2[0], layer_name)
    compare_two_array(arw1[1], arw2[1], layer_name)

def compare_two_convlayer(model1, model2, layer_name):
    weight1 = model1.keras_model.get_layer(layer_name).get_weights()
    weight2 = model2.keras_model.get_layer(layer_name).get_weights()
    arw1 = np.array(weight1)
    arw2 = np.array(weight2)
    if (len(arw1) != len(arw2)):
        return 0
    compare_two_matrix(arw1[0][0][0], arw2[0][0][0], layer_name)
    compare_two_array(arw1[1], arw2[1], layer_name)

def compare_two_normlayer(model1, model2, layer_name):
    weight1 = model1.keras_model.get_layer(layer_name).get_weights()
    weight2 = model2.keras_model.get_layer(layer_name).get_weights()
    arw1 = np.array(weight1)
    arw2 = np.array(weight2)

    if (len(arw1) != len(arw2)):
        return 0
    compare_two_matrix(arw1, arw2, layer_name, order=1)

def compare_two_array(a1, a2, name='default'):
    if (type(a1[0]) != np.float32):
        return
    d = a1 - a2
    if (sum(d) < 0.0001):
        return
    print('Different array')
    x = np.array(range(len(a1)))
    width = 0.35
    plt.bar(x, a1, width=width, label='a1', fc='y')
    plt.bar(x + width, a2, width=width, label='a2', fc='r')
    plt.legend()
    plt.savefig(name)

def compare_two_matrix(a1, a2, name, order=0):
    if order == 0:
        for i in range(len(a1[0])):
            compare_two_array(a1[:, i], a2[:, i], name + str(i))
    else:
        for i in range(len(a1)):
            compare_two_array(a1[i], a2[i], name + str(i))

def calculate_wd_layers(m_array1, m_array2, name):
    wd = 0
    weight1 = m_array1.keras_model.get_layer(name).get_weights()
    weight2 = m_array2.keras_model.get_layer(name).get_weights()
    arw1 = np.array(weight1)
    arw2 = np.array(weight2)
    for i in range(arw1.size):
        a1 = arw1[i].reshape(arw1[i].size)
        a2 = arw2[i].reshape(arw2[i].size)
        wd = wd + ss.wasserstein_distance(a1, a2)
    return wd

def calculate_wd_bnlayers(m_array1, m_array2, name):
    wd = 0
    weight1 = m_array1.keras_model.get_layer(name).get_weights()
    weight2 = m_array2.keras_model.get_layer(name).get_weights()
    arw1 = np.array(weight1)
    arw2 = np.array(weight2)
    wd = ss.wasserstein_distance(arw1.reshape(arw1.size), arw2.reshape(arw2.size))
    return wd

def calculate_wd_models_head(model1, model2):
    wd_rpn = calculate_wd_layers(model1, model2, train_rpn_model)
    wd_conv = 0
    for name in train_conv_layers:
        wd_conv = wd_conv + calculate_wd_layers(model1, model2, name)
    wd_dense = 0
    for name in train_dense_layers:
        wd_dense = wd_dense + calculate_wd_layers(model1, model2, name)
    wd_normal = 0
    for name in train_normal_layers:
        wd_normal = wd_normal + calculate_wd_bnlayers(model1, model2, name)

    return wd_rpn, wd_conv, wd_dense, wd_normal

def calculate_wd_models_backboon(model1, model2):
    wd_conv_array = []
    wd_a = []
    wd_b = []
    wd_c = []
    wd_r = []
    for name in train_resnet_conv:
        wd_conv_array.append(calculate_wd_layers(model1, model2, name))

    for name in train_resnet_conv_a:
        wd_a.append(calculate_wd_layers(model1, model2, name))
    for name in train_resnet_conv_b:
        wd_b.append(calculate_wd_layers(model1, model2, name))
    for name in train_resnet_conv_c:
        wd_c.append(calculate_wd_layers(model1, model2, name))
    for name in train_resnet_conv_r:
        wd_r.append(calculate_wd_layers(model1, model2, name))

    return wd_conv_array, wd_a, wd_b, wd_c, wd_r

def sequence_analysis(set_path, path_target,*calculate_wd):
    file_ls = os.listdir(set_path)
    file_ls.sort()
    wd = []
    counter = 0
    for file_name in file_ls:
        extend = os.path.splitext(file_name)[-1][1:]
        if (extend !='h5'):
            continue
        print(file_name)
        weights_path = os.path.join(set_path, file_name)
        model_source = load_weight(weights_path, worker.WorkerConfig())
        model_target = load_weight(path_target, worker.WorkerConfig())
        wd.append(calculate_wd(model_source, model_target))
        print('**************', len(wd), '**************')
        K.clear_session()
        # if counter>2:
        #     break
        # else:
        #     counter = counter+1
    print('Complete!')
    return wd

def spe_lightweight_sequence_analysis(dataset_path, weight_path, output_path,mark):
    list = np.array(sequence_analysis(dataset_path, weight_path,calculate_wd_models_head))
    plt.plot(range(len(list[:, 0])), list[:, 0], label='rpn_wd')
    plt.plot(range(len(list[:, 1])), list[:, 1], label='conv_wd')
    plt.plot(range(len(list[:, 2])), list[:, 2], label='dense_wd')
    plt.plot(range(len(list[:, 3])), list[:, 3], label='norm_wd')
    plt.legend()
    plt.savefig(os.path.join(output_path, mark+'div_wd.png'))
    plt.close()
    sum = list.sum(axis=1)
    plt.plot(range(len(sum)), sum, label=mark+'summary_wd')
    plt.savefig(os.path.join(output_path, mark+'summary_wd.png'))
    plt.close()
    dr.save_data(list, os.path.join(output_path, mark+'list.csv'))

def calculate_wd_models_all(model1, model2):
    wd_rpn = calculate_wd_layers(model1, model2, train_rpn_model)
    wd_conv = 0
    wd_dense = 0
    wd_normal = 0
    for name in train_conv_layers:
        wd_conv = wd_conv + calculate_wd_layers(model1, model2, name)
    for name in train_dense_layers:
        wd_dense = wd_dense + calculate_wd_layers(model1, model2, name)
    for name in train_normal_layers:
        wd_normal = wd_normal + calculate_wd_bnlayers(model1, model2, name)

    wd_conv_array = 0
    wd_a = 0
    wd_b = 0
    wd_c = 0
    wd_r = 0
    for name in train_resnet_conv:
        wd_conv_array = wd_conv_array + calculate_wd_layers(model1, model2, name)

    final_wd = wd_rpn + wd_conv + wd_dense + wd_normal + wd_conv_array + wd_a + wd_b + wd_c + wd_r

    return final_wd

# def compare_two_network(m1,m2):

# ...................................................................................

# ********************************** sequence compare imagenet coco
# list = np.array(sequence_analysis('logs/Experiments/Sequence_head_compare_imagenet_coco/Set','logs/Experiments/Sequence_head_compare_imagenet_coco/coco.h5',calculate_wd_models_head))
# plt.plot(range(len(list[:,0])),list[:,0],label= 'rpn_wd')
# plt.plot(range(len(list[:,1])),list[:,1],label = 'conv_wd')
# plt.plot(range(len(list[:,2])),list[:,2],label = 'dense_wd')
# plt.plot(range(len(list[:,3])),list[:,3],label = 'norm_wd')
# plt.legend()
# plt.savefig('logs/Experiments/Sequence_head_compare_imagenet_coco/div_wd.png')
# plt.close()
# sum = list.sum(axis=1)
# plt.plot(range(len(sum)),sum,label = 'summary_wd')
# plt.savefig('logs/Experiments/Sequence_head_compare_imagenet_coco/summary_wd')
# dr.save_data(list,'logs/Experiments/Sequence_head_compare_imagenet_coco/list.txt')

# ******************************compare two resnet 50
# m1 = load_weight('U:/Desktop/ryantest/Mask_RCNN - Copy/logs/mask_rcnn_worker_0005.h5',worker.WorkerConfig())
# m2 = load_weight('logs/Experiments/compare_resnet_coco/resnet.h5',worker.WorkerConfig())
#
# wd_array,wd_a,wd_b,wd_c,wd_r = calculate_wd_models_backboon(m1,m2)
# dr.save_data(wd_array, 'logs/Experiments/compare_resnet_coco/result.csv')
# dr.save_data(wd_a, 'logs/Experiments/compare_resnet_coco/result_a.csv')
# dr.save_data(wd_b, 'logs/Experiments/compare_resnet_coco/result_b.csv')
# dr.save_data(wd_c, 'logs/Experiments/compare_resnet_coco/result_c.csv')
# dr.save_data(wd_r, 'logs/Experiments/compare_resnet_coco/result_r.csv')
#
# plt.bar(range(len(wd_array)),wd_array)
# plt.savefig('logs/Experiments/compare_resnet_coco/image.png')
# plt.close()
# plt.bar(range(len(wd_a)),wd_a)
# plt.savefig('logs/Experiments/compare_resnet_coco/image_a.png')
# plt.close()
# plt.bar(range(len(wd_b)),wd_b)
# plt.savefig('logs/Experiments/compare_resnet_coco/image_b.png')
# plt.close()
# plt.bar(range(len(wd_c)),wd_c)
# plt.savefig('logs/Experiments/compare_resnet_coco/image_c.png')
# plt.close()
# plt.bar(range(len(wd_r)),wd_r)
# plt.savefig('logs/Experiments/compare_resnet_coco/image_r.png')
# plt.close()
# m1 = load_weight('U:/Desktop/ryantest/Mask_RCNN - Copy/logs/mask_rcnn_worker_0005.h5',worker.WorkerConfig())
#***********************************************************************************wd calculate
# target_path = '../drive/My Drive/silhouette_weight/worker_real/mask_rcnn_worker_0150.h5'
# output_path = '../drive/My Drive/silhouette_weight/wd'
# spe_lightweight_sequence_analysis('../drive/My Drive/silhouette_weight/stick',target_path,output_path,'stick_real')
# spe_lightweight_sequence_analysis('../drive/My Drive/silhouette_weight/stick_feature',target_path,output_path,'stick_feature_real')
# spe_lightweight_sequence_analysis('../drive/My Drive/silhouette_weight/silhouette',target_path,output_path,'silhouette_real')
# spe_lightweight_sequence_analysis('../drive/My Drive/silhouette_weight/sihouette_feature',target_path,output_path,'silhouette_feature_real')
# spe_lightweight_sequence_analysis('../drive/My Drive/silhouette_weight/worker_real',target_path,output_path,'real_real')
# *********************************************************************************model construction test


from django.urls import path

from . import views

urlpatterns = [
    path('', views.home_page, name='home'),
    path('contact', views.contact_page, name='contact'),
    path('about', views.about_page, name='about'),
    path('blog', views.blog_page, name='blog'),
    path('portfolio', views.portfolio_page, name='portfolio'),
    path('get_in_touch', views.get_in_touch, name='get_in_touch'),
    path('<slug:post>', views.post_detail, name='post_detail'),
]


from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404
from django.db.models import Q
from lab.models import *

def index(request):
    current_projects = Project.objects.filter(state='current').order_by('start_date')
    achieved_projects = Project.objects.filter(state='achieved').order_by('start_date')
    context = {
        'current_projects' : current_projects,
        'achieved_projects' : achieved_projects,
    }
    return render(request, 'lab/projects/index.html', context)

def detail(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    tools = project.tool_set.all().order_by('date')
    context = {
        'project': project,
        'tools' : tools,
    }
    return render(request, 'lab/projects/detail.html', context)

def tool(request, tool_id):
    tool = get_object_or_404(Tool, pk=tool_id)
    context = {
        'tool' : tool,
    }
    return render(request, 'lab/projects/tool.html', context)


# splite3をimportする
import sqlite3
# flaskをimportしてflaskを使えるようにする
from flask import Flask , render_template , request , redirect , session,url_for
#時間ごとに処理する
import time
import re

app = Flask(__name__)

app.secret_key = 'sunabakoza'

#正規表現
def word_re(word):
    re_hiragana = re.compile(r'^[あ-ん]+$')
    return re_hiragana.fullmatch(word)

@app.route('/',methods=["GET", "POST"])
def top():
    if request.method == "GET":
        return render_template("index.html")
    else:
        #テーマ名を入力してDBに保存
        theme = request.form.get("theme")
        conn = sqlite3.connect('service.db')
        c = conn.cursor()
        c.execute("insert into themes values(null,?)", (theme,) )
        #テーマIDをthemeに代入
        c.execute("select id from themes where theme = ?",(theme,))
        theme_id = c.fetchone()
        conn.commit()
        conn.close()
        # user_id が NULL(PythonではNone)じゃなければログイン成功
        if theme_id is None:
            # ログイン失敗すると、ログイン画面に戻す
            return render_template("index.html")
        else:
            session['theme_id'] = theme_id[0]
            return redirect("/shiritori")

@app.route('/shiritori',methods=["GET","POST"])
def shiritori():
    if request.method == "GET":
        return render_template("shiritori.html")
    else:
        # １　開始ボタンを押すとしりとりゲームが始まる
        # 2 60秒のカウントダウン
        # 3 しりとりのデータを入れる
        #sessionからtheme_idを取得
        theme_id = session['theme_id']
        word = request.form.get("words")
        if not word_re(word):
            error_text = "ひらがなで入力してください"
            return render_template('shiritori.html',error_text = error_text)
        else:
            error_text = ""
            conn = sqlite3.connect('service.db')
            c = conn.cursor()
            #db shiritori に theme_idと単語をインサート
            c.execute("insert into shiritori values(?,?,null)", (theme_id,word,) )
            conn.commit()
            #db から最初に入力したテーマと紐づいている単語を取得
            c.execute("select word from shiritori where theme_id = ?",(theme_id,))
            words = []
            word_list = []
            for row in c.fetchall():
                words.append(row[0])
            for i in range(len(words)):
                list_item = words[i]
                word_list.append({"id":i,"word":list_item})

            conn.close()
            return render_template('shiritori.html',word_list = word_list,error_text = error_text)

## おまじない
if __name__ == "__main__":
    app.run(debug=True)


"""A compatible script to install editable distributions."""
import os
import sys
import tokenize

from setuptools.command import easy_install

def install(setup_py, prefix):
    __file__ = setup_py
    bin_dir = "Scripts" if os.name == "nt" else "bin"
    install_dir = os.path.join(prefix, "lib")
    scripts_dir = os.path.join(prefix, bin_dir)

    with getattr(tokenize, "open", open)(setup_py) as f:
        code = f.read().replace("\\r\\n", "\n")
    sys.argv[1:] = [
        "develop",
        "--install-dir={0}".format(install_dir),
        "--no-deps",
        "--prefix={0}".format(prefix),
        "--script-dir={0}".format(scripts_dir),
        "--site-dirs={0}".format(install_dir),
    ]
    # Patches the script writer to inject library path
    easy_install.ScriptWriter.template = easy_install.ScriptWriter.template.replace(
        "import sys",
        "import sys\nsys.path.insert(0, {0!r})".format(install_dir.replace("\\", "/")),
    )
    exec(compile(code, __file__, "exec"))

if __name__ == "__main__":
    setup_py, prefix = sys.argv[1:3]
    install(setup_py, prefix)


import pymongo

# mongodb
# 存储主要数据
client = pymongo.MongoClient('localhost', 27017)

user = client['user']['user']
sign_up = client['sgin_up']['sign_up']
work_process = client['work_process']['worl_process']
work_sheet = client['work_sheet']['work_sheet']

# basic_dict = client['basic_dict']
department = client['department']['department']


import numpy as np
import cv2
import math, time
from datalogger import DataLogger
import webcolors

class Droplet:
    def __init__(self, color=None, lowerhsv=None, upperhsv=None, minSizemm=None, maxSizemm=None,
                 minSizePix=None, maxSizePix=None, dataLoggerName=None, contourColor=None):

        self.lowerhsv = np.array(lowerhsv)
        self.upperhsv = np.array(upperhsv)
        self.__setHSV(color)
        self.minSizeM = minSizemm
        self.maxSizeM = maxSizemm
        self.minSizeP = minSizePix
        self.maxSizeP = maxSizePix
        self.xP = None
        self.yP = None
        self.xM = None
        self.yM = None
        self.areaP = None
        self.areaM = None
        self.frameNr = None
        self.time = None
        self.velocityM = None
        self.velocityP = None
        self.data = None
        self.dataBuf = []
        if dataLoggerName is not None:
            fileName = dataLoggerName + time.strftime("%Y-%m-%d %H%M%S")
            self.dataLogger = DataLogger('experiments/' + fileName, kind='csv')
        else:
            self.dataLogger = None
        self.bufLen = 10000
        self.logTitlesNeeded = True
        self.contour = None
        self.contourBGR = self.__colorName2BGR(contourColor)
        # buffers which hold the average of area and center for noise removal
        self.areaBuf = []
        self.areaBufLen = 100
        self.xPBuf, self.yPBuf = [], []
        self.cntBufLen = 3
        self.count = 0

    def __setHSV(self, color):
        if color is None:
            return
        if color == 'default red':
            self.lowerhsv = np.array([140, 40, 40])
            self.upperhsv = np.array([180, 255, 255])

        if color == 'default blue':
            self.lowerhsv = np.array([60, 50, 50])
            self.upperhsv = np.array([160, 255, 255])

    def __setdataLogger(self, data):

        if self.logTitlesNeeded:
            self.dataLogger(('frame number', 'time(ms)', 'centerX(pixels)', 'centerY(pixels)', 'Area(pixels)',
                             'Speed(PixperFrame)', 'centerX(mm)', 'centerY(mm)', 'Area(mm2)', 'Speed(mmps)'))
            self.logTitlesNeeded = False
        self.dataLogger((str(self.data[0]), str(self.data[1]), str(self.data[2]), str(self.data[3]), str(self.data[4]),
                         str(self.data[5]), str(self.data[6]), str(self.data[7]), str(self.data[8]), str(self.data[9])))

    def __colorName2BGR(self, colorName):

        colorRGB = webcolors.name_to_rgb(colorName)
        colorBGR = (colorRGB[2], colorRGB[1], colorRGB[0])
        return colorBGR

    def setData(self, data):

        self.frameNr = data[0]
        self.time = data[1]

        self.xP = data[2]
        self.yP = data[3]
        self.areaP = data[4]

        self.xM = data[5]
        self.yM = data[6]
        self.areaM = data[7]

        if self.frameNr == 0:
            self.velocityP = 0
            self.velocityM = 0

        elif self.frameNr > 0:
            self.velocityP = self.Distance((self.xP, self.yP), self.dataBuf[0][2:4])
            self.velocityM = self.Distance((self.xM, self.yM), self.dataBuf[0][6:8]) / (self.time - self.dataBuf[0][1])

        data.insert(5, self.velocityP)
        data.append(self.velocityM)

        self.data = data
        if self.dataLogger is not None:
            self.__setdataLogger(self.data)

        if len(self.dataBuf) < self.bufLen:
            self.dataBuf.insert(0, data)

        else:
            dataBufCopy = self.dataBuf
            dataBufCopy.insert(0, data)
            del dataBufCopy[self.bufLen]
            self.dataBuf = dataBufCopy

    def Distance(self, u, v):
        distance = np.linalg.norm(np.array(u) - np.array(v))
        return distance

    def angel(u, v):
        vector = np.array(u) - np.array(v)
        theta = math.atan(vector[1] / vector[0])
        distance = np.linalg.norm(np.array(u) - np.array(v))
        return distance

    def getVelocityMM(self):
        return self.velocityM

    def getVelocityPix(self):
        return self.velocityP

    def getPosMM(self):
        return (self.xM, self.yM)

    def getPosPix(self):
        return (self.xP, self.yP)

    def getPosPix(self):
        return (self.xP, self.yP)

    def getData(self):
        return self.dataBuf[0]

    def getContour(self):
        return self.contour

    def isSpeedBelow(self, threshmmps=None, threshpixpfr=None, time=None):
        result = True
        bufLen = len(self.dataBuf)
        if self.dataBuf[0][1] - self.dataBuf[bufLen - 1][1] < time:
            result = False

        if threshmmps is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[9] > threshpixpfr:
                    result = False
                    break

        if threshpixpfr is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[5] > threshpixpfr:
                    result = False
                    break
        return result

    def isSpeedOver(self, threshmmps=None, threshpixpfr=None, time=None):
        result = True
        bufLen = len(self.dataBuf)
        if self.dataBuf[0][1] - self.dataBuf[bufLen - 1][1] < time:
            result = False

        if threshmmps is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[9] < threshpixpfr:
                    result = False
                    break

        if threshpixpfr is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[5] < threshpixpfr:
                    result = False
                    break
        return result

    def isStatic(self, threshmmps, threshpixpfr):
        result = True
        if threshmmps is not None:
            if self.velocityM > threshmmps:
                result = False

            if threshpixpfr is not None:
                if self.velocityP > threshpixpfr:
                    result = False
        return result

    def isMobile(self, threshmmps, threshpixpfr):
        result = True
        if threshmmps is not None:
            if self.velocityM < threshmmps:
                result = False

            if threshpixpfr is not None:
                if self.velocityP < threshpixpfr:
                    result = False
        return result

    def countBlobs(self):

        return self.count


#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import re
from collections import namedtuple
import peglet

Var = namedtuple('Var', ['name'])
StmtCmp = namedtuple('StmtCmp', ['left', 'op', 'right'])
SubBegin = namedtuple('SubBegin', ['name', 'args'])
SubEnd = namedtuple('SubEnd', [])
StmtAssign = namedtuple('StmtAssign', ['left', 'right'])
StmtElif = namedtuple('StmtElif', ['cond', 'body'])
StmtElse = namedtuple('StmtElse', ['body'])
StmtIf = namedtuple('StmtIf', ['cond', 'body', 'extra'])
StmtWhile = namedtuple('StmtWhile', ['cond', 'body'])
StmtVar = namedtuple('StmtVar', ['name'])
Label = namedtuple('Label', ['name'])
Line = namedtuple('Line', ['file', 'idx'])
Sub = namedtuple('Sub', ['name', 'args', 'body'])
Call = namedtuple('Call', ['name', 'args'])

class Parser(object):

    grammar = r"""
    wrapper     = ^ program $
    program     = statement program | statement
    statement   = space sub space

    _           = [\t\x20]*
    space       = \s* | comments
    newline     = [\r\n]+
    comment     = \# [^\r\n$]+ newline
    comments    = _ newline comments
                | _ newline _
                | _ comment comments
                | _ comment _
    split       = comments
                | comments split
                | _ ; _ split
                | _ ; _

    name        = ([a-zA-Z_][_a-zA-Z0-9]*\b)
    label       = : name                            make_label
    maybe_label = space label space
                | space
    line        = maybe_label stmt split
    lines       = line lines
                | line

    stmt        = var_assign | stmt_if | stmt_while | stmt_var | stmt_call
    var_assign  = var _ \= _ value                  make_assign

    sub         = begin lines end                   make_sub
    begin       = (\.begin\b) _ name_args split     clean_sub
    end         = (\.end\b)                         make_endsub

    name_args   = name sub_args                     make_subbegin
    sub_args    = _ var _ sub_args
                | _ var
                | _

    stmt_call   = name call_types                   make_call
    call_types  = _ label | call_args
    call_args   = _ value _ call_args
                | _ value
                | _

    stmt_var    = (\.var) _ var_or_assign           make_var
    var_or_assign = var_assign
                  | var

    stmt_while  = (\.while\b) space bool split lines space (\.end\b)    make_while

    if_body     = space bool split lines space
    stmt_else   = (\.else\b) space lines space
    stmts_else  = stmt_else                         make_else
                | space
    stmts_elif  = stmt_elif stmts_elif
                | stmt_elif
                | space
    stmt_elif   = (\.elif\b) if_body                make_elif
    if_tail     = stmts_elif stmts_else             listify
    begin_if    = (\.if\b) if_body                  make_if_begin
    end_if      = if_tail (\.end\b)
    stmt_if     = begin_if end_if                   make_if
    bool        = value _ opcmp _ value             make_cmp
    opcmp       = (==|!=)

    value       = var | \( space stmt_call space \)
    var         = \$ name                           token_var
    """

    def __init__(self):
        kwargs = {"make_endsub": lambda *args: SubEnd(),
                  "make_cmp": StmtCmp,
                  "make_assign": StmtAssign,
                  "make_label": Label,
                  "make_var": lambda *x: StmtVar(x[1]),
                  "make_while": lambda *x: StmtWhile(x[1], x[2:-1]),
                  "make_else": lambda *x: StmtElse(x[1:]),
                  "make_if": lambda *x: StmtIf(x[0][0], x[0][1], x[1]),
                  "make_if_begin": lambda *x: (x[1], x[2:]),
                  "make_elif": lambda *x: StmtElif(x[1], x[2:]),
                  "make_call": lambda *x: Call(x[0], x[1:]),
                  "make_sub": lambda *x: Sub(x[0].name, x[0].args, x[1:-1]),
                  "make_subbegin": lambda *args: SubBegin(args[0], args[1:]),
                  "clean_sub": lambda x, y: y,
                  "token_var": Var,
                  "hug"    : peglet.hug,
                  "listify": lambda *x: list(x),
                }
        self.parser = peglet.Parser(self.grammar, **kwargs)

    def __call__(self, program):
        return self.parser(program)

def str_expr(expr, inside=False):
    if isinstance(expr, Var):
        result = "$" + expr.name
    else:
        if isinstance(expr, StmtCmp):
            result = "%s %s %s" % (str_expr(expr.left, True), expr.op, str_expr(expr.right, True))
        elif isinstance(expr, StmtAssign):
            result = "%s = %s" % (str_expr(expr.left), str_expr(expr.right, True))
        elif isinstance(expr, Call):
            result = "%s %s" % (expr.name, str_expr(expr.args))
        elif isinstance(expr, (list, tuple)):
            result = " ".join([str_expr(X) for X in expr])
        else:
            raise RuntimeError("Unknown expression: %r" % (expr,))
        if inside:
            result = "(" + result + ")"
    return result

def print_token(token, level=0):
    indent = (" " * level * 4)
    if isinstance(token, Sub):
        print(indent + ".begin %s %s" % (
            token.name,
            ' '.join([
                '$' + arg.name
                for arg in token.args])))
        for subtoken in token.body:
            print_token(subtoken, level + 1)
        print(indent + ".end")
    elif isinstance(token, Label):
        print(indent + ":" + token.name)
    elif isinstance(token, Call):
        print(indent + token.name, str_expr(token.args))
    elif isinstance(token, StmtIf):
        print(indent + ".if", str_expr(token.cond))
        for subtoken in token.body:
            print_token(subtoken, level + 1)
        for substmt in token.extra:
            print_token(substmt, level)
        print(indent + ".end")
    elif isinstance(token, StmtElif):
        print(indent + ".elif", str_expr(token.cond))
        for subtoken in token.body:
            print_token(subtoken, level + 1)
    elif isinstance(token, StmtElse):
        print(indent + ".else")
        for subtoken in token.body:
            print_token(subtoken, level + 1)
    elif isinstance(token, StmtWhile):
        print(indent + ".while", str_expr(token.cond))
        for subtoken in token.body:
            print_token(subtoken, level + 1)
        print(indent + ".end")
    elif isinstance(token, StmtAssign):
        print(indent + str_expr(token.left), "=", str_expr(token.right))
    elif isinstance(token, StmtVar):
        print(indent + ".var", str_expr(token.name))
    else:
        raise RuntimeError("Unknown token: %r" % (token,))

class Driver(object):
    def tokenize_file(self, handle):
        parser = Parser()
        token_list = parser(handle.read())
        for token in token_list:
            print_token(token)
        return token_list

if __name__ == "__main__":
    compiler = Driver()
    for filepath in sys.argv[1:]:
        with open(filepath, 'r') as handle:
            compiler.tokenize_file(handle)


from rest_framework.serializers import Serializer, BooleanField, CharField
from ..serializers import LBaseSerializer
from .models import Verification, EmailVerification

class VerificationSerializer(LBaseSerializer):
    VERIFICATION_SERIALIZERS = {
        'emailverification': 'EmailVerificationSerializer',
    }

    class Meta(LBaseSerializer.Meta):
        model = Verification
        fields = LBaseSerializer.Meta.fields + \
            ('id', 'code', 'expiration', 'verification', 'user', 'type', )
        read_only_fields = LBaseSerializer.Meta.read_only_fields + \
            ('type', )

    def to_representation(self, instance):
        instance = getattr(instance, instance.type)
        serializer = eval(
            self.VERIFICATION_SERIALIZERS[instance.type]
        )(instance)
        serializer.context = self.context

        return super(VerificationSerializer, serializer) \
            .to_representation(instance)

class EmailVerificationSerializer(VerificationSerializer):

    class Meta(VerificationSerializer.Meta):
        model = EmailVerification

class EmailVerifySerializer(Serializer):
    success = BooleanField()
    message = CharField()


#!/usr/bin/python

import argparse

def find_max_profit(prices):
    # loop over prices, starting from the second price
    # for each price, find min before
    # keep track of current_min_price_so_far (min before) and max_profit_so_far

    max_profit_so_far = None

    for i in range(1, len(prices)):
        current_min_price_so_far = None

        for j in range(0, i):
            if current_min_price_so_far == None or prices[j] < current_min_price_so_far:
                current_min_price_so_far = prices[j]

        if max_profit_so_far == None or prices[i] - current_min_price_so_far > max_profit_so_far:
            max_profit_so_far = prices[i] - current_min_price_so_far

    return max_profit_so_far

if __name__ == '__main__':
    # This is just some code to accept inputs from the command line
    parser = argparse.ArgumentParser(description='Find max profit from prices.')
    parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer price')
    args = parser.parse_args()

    print("A profit of ${profit} can be made from the stock prices {prices}.".format(profit=find_max_profit(args.integers), prices=args.integers))


# coding:"utf_8"
import MeCab

text = '今日は晴れかな？'

#天気の関連語をまとめたgazetteer
weather_set = set([u'晴れ',u'天気',u'雨'])

mecab = MeCab.Tagger("-Ochasen") #MeCabの取得
tokens = mecab.parse(text) #分かち書きを行う
token = tokens.split("\n")

#以下、分かち書き後の単語を抽出
for ele in token:
    element = ele.split("\t")
    if element[0] == "EOS":
        break

    # 単語の表層を取得
    surface = element[0]

    if surface in weather_set:
        print(surface)


#!/usr/bin/env python3
#-----------------------------------------------------------------------------
# Title      : PyRogue febBoard Module
#-----------------------------------------------------------------------------
# File       : SingleNodeTest.py
# Created    : 2016-11-09
# Last update: 2016-11-09
#-----------------------------------------------------------------------------
# Description:
# Rogue interface to FEB board
#-----------------------------------------------------------------------------
# This file is part of the LCLS2-PRL. It is subject to
# the license terms in the LICENSE.txt file found in the top-level directory
# of this distribution and at:
#    https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
# No part of the LCLS2-PRL, including this file, may be
# copied, modified, propagated, or distributed except according to the terms
# contained in the LICENSE.txt file.
#-----------------------------------------------------------------------------

import sys
import pyrogue as pr
import pyrogue.gui
import PyQt4.QtGui
import argparse
import Lsst5vDcPdu  as board5v
import Lsst24vDcPdu as board24v

# Set the argument parser
parser = argparse.ArgumentParser()

# Add arguments
parser.add_argument(
    "--type",
    type     = str,
    required = True,
    help     = "board type (5VC, 24VC, 24VD  or 48VD)",
)

parser.add_argument(
    "--ip",
    type     = str,
    required = True,
    help     = "IP address",
)

parser.add_argument(
    "--hwEmu",
    type     = bool,
    required = False,
    default  = False,
    help     = "hardware emulation (false=normal operation, true=emulation)",
)

parser.add_argument(
    "--pollEn",
    type     = bool,
    required = False,
    default  = False,
    help     = "enable auto-polling",
)

# Get the arguments
args = parser.parse_args()

# Set base
base = pr.Root(name='base',description='')

# Determine the type
if ( args.type == '5VC' ):
    # Add Base Device
    base.add(board5v.Top(
        ip    = args.ip,
        nChannels = 20,
        shunts = (12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 10e5, 10e5, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2, 51.2),
        hwEmu = args.hwEmu,
    ))
elif ( args.type == '24VC'):
   # Add Base Device
    base.add(board24v.Top(
        ip    = args.ip,
        hwEmu = args.hwEmu,
        nChannels = 12,
        shunts = ( 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ),
        VScale = 0.03973,
        IScale = 0.0488,
    ))
elif ( args.type == '24VD'):
   # Add Base Device
    base.add(board24v.Top(
        ip    = args.ip,
        hwEmu = args.hwEmu,
        nChannels = 12,
        shunts = (4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ),
        VScale = 0.03973,
        IScale = 0.0488,
    ))
elif  ( args.type == '48VD'):
   # Add Base Device
    base.add(board24v.Top(
        ip    = args.ip,
        hwEmu = args.hwEmu,
        nChannels = 12,
        shunts = (4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ),
        VScale = 0.06772,
        IScale = 0.0282,
    ))

else:
    raise ValueError("Invalid type (%s). Valid types are 5VC, 24VC, 24VD, 48VD" % (args.type) )

# Start the system
base.start(pollEn=args.pollEn)
base.Top.Fpga.Core.AxiVersion.printStatus()

# Create GUI
appTop = PyQt4.QtGui.QApplication(sys.argv)
appTop.setStyle('Fusion')
guiTop = pyrogue.gui.GuiTop(group='rootMesh')
guiTop.addTree(base)
guiTop.resize(800, 1000)

print("Starting GUI...\n");

# Run GUI
appTop.exec_()

base.stop()
exit()


# Copyright (c) 2020 fortiss GmbH
#
# Authors: Patrick Hart
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.

import pandas as pd
from enum import Enum

class Tracer:
  class QueryTypes(str, Enum):
    """
    Trace: returns the trace
    Mean: calculates the mean of the trace
    """
    TRACE = "TRACE"
    MEAN  = "MEAN"
    SUM  = "SUM"
    LAST_VALUE  = "LAST_VALUE"
    ANY_TRUE  = "ANY_TRUE"

  def __init__(self):
    self._states = []
    self._df = None

  def EvalStateToDict(self, eval_state):
    """Converts an eval_state to a flat eval_dict"""
    eval_dict = {}
    if type(eval_state) is tuple:
      (state, reward, is_terminal, info) = eval_state
      eval_dict["state"] = state
      eval_dict["reward"] = reward
      eval_dict["is_terminal"] = is_terminal
      for info_key, info_value in info.items():
        eval_dict[info_key] = info_value
    if type(eval_state) is dict:
      for info_key, info_value in eval_state.items():
        eval_dict[info_key] = info_value
    return eval_dict

  def Trace(self, eval_state, **kwargs):
    """Traces and stores a state"""
    eval_dict = self.EvalStateToDict(eval_state)
    # additional things to log
    for key, value in kwargs.items():
      eval_dict[key] = value
    self._states.append(eval_dict)

  def Query(
    self,
    key="collision",
    group_by="num_episode",
    agg_type="TRACE"):
    if self.df is None:
      self.ConvertToDf()
    df = self.df.groupby(group_by)[key]
    # NOTE: different aggregation types
    if agg_type == Tracer.QueryTypes.MEAN:
      return df.mean()
    if agg_type == Tracer.QueryTypes.SUM:
      return df.sum()
    if agg_type == Tracer.QueryTypes.LAST_VALUE:
      return df.tail(1)
    elif agg_type == Tracer.QueryTypes.ANY_TRUE:
      return df.any().mean()
    return df

  @property
  def df(self):
    return self._df

  def ConvertToDf(self):
    """Conversts states to pandas dataframe"""
    self._df = pd.DataFrame(self._states)

  def Save(self, filepath="./"):
    """Saves trace as pandas dataframe"""
    if self._df is None:
      self.ConvertoToDf()
    self._df.to_pickle(filepath)

  def Reset(self):
    self._states = []
    self._df = None


import pygame, sys
from pygame.locals import *
from gl import *

from math import cos, sin, pi

mainClock = pygame.time.Clock()

pygame.init()
pygame.display.set_caption('Lab 3 UI')
screen = pygame.display.set_mode((1000,500),0,32)

font = pygame.font.SysFont("Arial", 30)

def updateFPS():
    fps = str(int(clock.get_fps()))
    fps = font.render(fps, 1, pygame.Color("white"))
    return fps

def main_menu():
    while True:

        screen.fill(pygame.Color("dimgray")) #color de fondo
        textobj = font.render('LAB3RINTO', 1, (255, 255, 255))  #titulo del juego
        textrect = textobj.get_rect()
        textrect.topleft = (20, 20) #posicion x,y del juego
        screen.blit(textobj, textrect)

        mx, my = pygame.mouse.get_pos()

        button_1 = pygame.Rect(50, 100, 200, 50)
        button_2 = pygame.Rect(350, 100, 200, 50)
        if button_1.collidepoint((mx, my)):
          if click:
            gotogame()
        if button_2.collidepoint((mx, my)):
          if click:
            exit()

        pygame.draw.rect(screen, (255, 0, 0), button_1)
        pygame.draw.rect(screen, (255, 0, 0), button_2)

        textobj = font.render('Iniciar', 1, (255, 255, 255))  #titulo del juego
        textrect = textobj.get_rect()
        textrect.topleft = (115, 105) #posicion x,y del juego
        screen.blit(textobj, textrect)

        textobj = font.render('Salir', 1, (255, 255, 255))  #titulo del juego
        textrect = textobj.get_rect()
        textrect.topleft = (425, 105) #posicion x,y del juego
        screen.blit(textobj, textrect)

        click = False
        for event in pygame.event.get():
          if event.type == QUIT:
            pygame.quit()
            sys.exit()
          if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
              pygame.quit()
              sys.exit()
          if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
              click = True

        pygame.display.update()
        mainClock.tick(60)

def gotogame():
    running = True
    while running:
        screen.fill(pygame.Color("dimgray"))

        textobj = font.render('Aqui se abre el juego, pero todavia no xD, presione ESC para regresar al menu', 1, (255, 255, 255))  #titulo del juego
        textrect = textobj.get_rect()
        textrect.topleft = (20, 20) #posicion x,y del juego
        screen.blit(textobj, textrect)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False

        pygame.display.update()
        mainClock.tick(60)

def exit():
  pygame.quit()

main_menu()


from __future__ import print_function
import sys
import numpy
import math
import random
import csv
import matplotlib.pyplot as plt
import pystache
import json
from sklearn import mixture

x = []
y = []

toolInput = sys.argv[1]
toolOutput = sys.argv[2]
toolWebsite = sys.argv[3]
inName = sys.argv[4]

with open(toolInput, 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter='\t')
    for i, row in enumerate(spamreader):
        if i != 0:
            x.append(int(row[0]))
            y.append(int(row[1]))

csvfile.close()

# you have to set this manually to weed out all the noise. Every bit of noise should be below it.
threshold = 20
rightLimit = 200

# unravelling histogram into samples.
samples = []
for no, value in enumerate([int(round(i)) for i in y]):
    if value > threshold and no < rightLimit:
        for _ in range(value):
            samples.append(no)

# total number of reads
totalAmp = len(samples)

# reshaping numpy arrays to indicate that we pass a lot of samples, not a lot of features.
xArray = numpy.array(x).reshape(1, -1)
samplesArray = numpy.array(samples).reshape(-1, 1)

# learning a gaussian mixture model.
gmm2 = mixture.BayesianGaussianMixture(n_components=2).fit(samplesArray)

# getting the mean of each gaussian
means = [x[int(round(i[0]))] for i in gmm2.means_]

# rounding errors
roundErr = [i[0] - int(round(i[0])) for i in gmm2.means_]

# getting the coverage of each gaussian
weights = gmm2.weights_

sampleID = inName

#with open(toolOutput, "w") as f:
#    print("sampleID", file=f, end="\t")
#    print("Al1", file=f, end="\t")
#    print("Al2", file=f, end="\t")
#    print("frac1", file=f, end="\t")
#    print("frac2", file=f, end="\t")
#    print(file=f)
#    print(sampleID, file=f, end="\t")
#    print(means[0], file=f, end="\t")
#    print(means[1], file=f, end="\t")
#    print(weights[0], file=f, end="\t")
#    print(weights[1], file=f, end="\t")

template_dir = {
    "sampleID": sampleID,
    "al1": means[0],
    "al2": means[1],
    "freq1": weights[0],
    "freq2": weights[1],
    "x": json.dumps(x),
    "y": json.dumps(y)
    }
with open(toolWebsite) as wt:
    with open(toolOutput, "w") as wr:
        wr.write(pystache.render(wt.read(), template_dir))

wt.close()
wr.close()


import random
import numpy as np
from ozone.wordnet import GetRandomSynset
from ozone.wordnet import get_all_lemmas_from_sense
from ozone.puzzle import PuzzleGenerator
from nltk.corpus import wordnet as wn

class Taxonomy:

    def get_vocab(self):
        raise NotImplementedError("Cannot call this on an abstract class.")

    def random_node(self, specificity_lb, specificity_ub):
        raise NotImplementedError("Cannot call this on an abstract class.")

    def random_hyponyms(self, node, k):
        raise NotImplementedError("Cannot call this on an abstract class.")

    def random_non_hyponym(self, node):
        raise NotImplementedError("Cannot call this on an abstract class.")

class WordnetTaxonomy(Taxonomy):

    def __init__(self, root_synset_name):
        super().__init__()
        self.root_synset = wn.synset(root_synset_name)
        self.synset_gen = GetRandomSynset(root_synset_name)
        self.vocab = self._build_vocab()

    def get_vocab(self):
        return self.vocab

    def _build_vocab(self):
        words = sorted(list(get_all_lemmas_from_sense(self.root_synset)))
        word_to_ix = dict([(v, k) for (k,v) in enumerate(words)])
        print("vocab size: {}".format(len(word_to_ix)))
        return word_to_ix

    def random_node(self, specificity_lb, specificity_ub):
        root = self.synset_gen.random_synset_with_specificity(specificity_lb,
                                                              specificity_ub)
        if root is None:
            return None
        while root == self.root_synset:
            root = self.synset_gen.random_synset_with_specificity(specificity_lb,
                                                                  specificity_ub)
        return root.name()

    def random_hyponyms(self, node, k):
        node = wn.synset(node)
        hyps = get_all_lemmas_from_sense(node) # children?
        return random.sample(hyps, k)

    def random_non_hyponym(self, node):
        node = wn.synset(node)
        hyps = get_all_lemmas_from_sense(node) # children?
        counter = 0
        random_hyp_lemmas = []
        while len(random_hyp_lemmas) == 0:
            if counter > 10000:
                raise Exception('Too difficult to get a non-hyponym for {}; giving up'.format(node.name()))
            random_hyp = self.synset_gen.random_non_hyponym(node.name())
            random_hyp_lemmas = set(get_all_lemmas_from_sense(random_hyp))
            random_hyp_lemmas -= hyps
            counter += 1
        random_word = random.choice(list(random_hyp_lemmas))
        return random_word

class TaxonomyPuzzleGenerator(PuzzleGenerator):

    def __init__(self, taxonomy, num_choices):
        super().__init__()
        self.taxonomy = taxonomy
        self.specificity_lb = 10
        self.specificity_ub = 5000
        self.n_choices = num_choices

    def num_choices(self):
        return self.n_choices

    def max_tokens_per_choice(self):
        return 1

    def get_vocab(self):
        return self.taxonomy.get_vocab()

    def generate(self):
        root = self.taxonomy.random_node(self.specificity_lb,
                                         self.specificity_ub)
        puzzle = self.taxonomy.random_hyponyms(root, self.num_choices() - 1)
        random_word = self.taxonomy.random_non_hyponym(root)
        puzzle.append(random_word)
        result = [(str(choice), 0) for choice in puzzle[:-1]] + [(str(puzzle[-1]), 1)]
        random.shuffle(result)
        xyz = tuple([i for (i, _) in result])
        onehot = [j for (_, j) in result]
        return (xyz, onehot.index(1))


from numpy import *
import numpy as np
import healpy as hp
import time
import astropy
from astropy.time import Time
from astropy.coordinates import SkyCoord

dir='/home/hsinyuchen/research/'

def rot_map(basemap,ra_rot):
    nside=hp.get_nside(basemap)
    rotmap=zeros(hp.nside2npix(nside))
    theta,phi=hp.pix2ang(nside,np.arange(hp.nside2npix(nside)))
    phi=phi+ra_rot
    rotmap[hp.ang2pix(nside,theta,phi)]=basemap
    return rotmap

def vampire(time,nside,dead_zone):
    t = Time(time, format='mjd', scale='utc')
    sun=astropy.coordinates.get_sun(t)
    theta_sun=pi/2.-sun.dec.radian
    phi_sun=sun.ra.radian
    v_sun=array([sin(theta_sun)*cos(phi_sun),sin(theta_sun)*sin(phi_sun),cos(theta_sun)])
    theta,phi=hp.pix2ang(nside,arange(hp.nside2npix(nside)))
    v_pix=array([sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)])
    shield=dot(v_sun,v_pix)<=cos(dead_zone/180.*pi)
    return shield

def bias_factor(hr,amplitude):
    return  (1. + amplitude*sin(2.*pi*hr/24. - pi/8.))/24.

basemap_cbc=hp.fitsfunc.read_map(dir+'obsbias/data/skyprior_cbc_hl.fits')
basemap_cbc=basemap_cbc/sum(basemap_cbc)

mjd,ra_rot=loadtxt(dir+'obsbias/data/2016nov_rot_fine.txt',unpack=True)

bias_amp=array([0.4])
nside=hp.get_nside(basemap_cbc)

n_coadd=0
inst_map_cbc_he=zeros((hp.nside2npix(nside),size(bias_amp)))

for i in range(0,size(ra_rot)):
	rot_map_cbc=rot_map(basemap_cbc,ra_rot[i])
	rot_bias_cbc=rot_map_cbc[:,newaxis]*bias_factor((i*0.25)%24,bias_amp)

	shield_he=vampire(mjd[i],nside,18.)	#decide how many degrees around the sun shall be blocked.
	inst_map_cbc_he=inst_map_cbc_he+shield_he[:,newaxis]*rot_bias_cbc

	n_coadd=n_coadd+1
coaddmap_cbc_he=inst_map_cbc_he/n_coadd

for j in range(0,size(bias_amp)):
	fileinfo='hl_2016nov_sin'+'%.3f'%bias_amp[j]+'_0.125pi_fine'
	hp.fitsfunc.write_map(dir+'obsbias/data/skyprior_cbc_'+fileinfo+'_twilight.fits',coaddmap_cbc_he[:,j]/sum(coaddmap_cbc_he[:,j]))


#!/usr/bin python
# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
import sys

# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'put twilio account sid here'
auth_token = 'put twilio auth token here, do not share it on github'
client = Client(account_sid, auth_token)

call = client.calls.create(
                        # url='http://demo.twilio.com/docs/voice.xml',
                        url='http://cs.unh.edu/online server that upload the message files/'+sys.argv[1],
                        to='phone number of caregiver',
                        from_='phone number of twilio account'
                    )

print(call.sid)


# coding=utf-8
__author__ = 'ryancui'

import codecs
import argparse

from api import *
from config import *

parser = argparse.ArgumentParser()
parser.add_argument("--day", '-d', help="The exact day of the task or 'all' for all of days")
parser.add_argument("--grep", '-g', help="Grep the specific nickname given")
parser.add_argument("--file", '-f', help="The file you want to output the result to")
parser.add_argument("--rank", '-r', help="See the rank")

opts, remains = parser.parse_known_args(sys.argv)

# Overlap the output if provided a file path
if opts.file is not None:
    fd = codecs.open(opts.file, "w", "utf-8")

# Overlap the default watch list if provided new ones
if opts.grep is not None:
    watch_list = opts.grep.split(',')

# See the teak content
if opts.day is not None:
    if opts.day == 'a' or opts.day == 'all':
        for i in range(1, topic_num + 1):
            target = get_per_day(base_url_task, i, watch_list)
            output(fd, target)
    elif opts.day == '0':
        target = get_per_day(base_url_task, topic_num, watch_list)
        output(fd, target)
    else:
        target = get_per_day(base_url_task, opts.day, watch_list)
        output(fd, target)