# everything shall start from 0
import argparse
import itertools
import json
import logging
import matplotlib
import multiprocessing as mp
import numpy as np
import os
import pprint
matplotlib.rcParams['backend'] = 'agg' if os.environ.get('CI') else 'TkAgg'
import matplotlib.pyplot as plt
from os import path, environ
from collections import deque
from functools import partial

# parse_args to add flag
parser = argparse.ArgumentParser(description='Set flag for functions')
parser.add_argument("-d", "--debug",
                    help="activate debug log",
                    action="store_const",
                    dest="loglevel",
                    const=logging.DEBUG,
                    default=logging.INFO)
args = parser.parse_args([]) if environ.get('CI') else parser.parse_args()

# Goddam python logger
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(
    logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s'))
logger.setLevel(args.loglevel)
logger.addHandler(handler)
logger.propagate = False

pp = pprint.PrettyPrinter(indent=2)

plt.rcParams['toolbar'] = 'None'  # mute matplotlib toolbar
plotters = {}  # hash of matplotlib objects for live-plot

PROBLEMS = json.loads(open(
    path.join(path.dirname(__file__), 'assets', 'problems.json')).read())

# the keys need to be implemented by a sys_var
# the constants (capitalized) are problem configs,
# set in assets/problems.json
required_sys_keys = {
    'RENDER',
    'GYM_ENV_NAME',
    'SOLVED_MEAN_REWARD',
    'MAX_STEPS',
    'MAX_EPISODES',
    'MAX_HISTORY',
    'param',
    'epi',
    'history',
    'mean_rewards',
    'solved'
}

def init_sys_vars(problem='CartPole-v0', param={}):
    '''
    init the sys vars for a problem by reading from
    assets/problems.json, then reset the other sys vars
    on reset will add vars: {param, epi, history, mean_rewards, solved}
    '''
    sys_vars = PROBLEMS[problem]
    if mp.current_process().name != 'MainProcess':
        sys_vars['RENDER'] = False  # mute on parallel
    if environ.get('CI'):
        sys_vars['RENDER'] = False
        sys_vars['MAX_EPISODES'] = 2
    sys_vars['param'] = param
    reset_sys_vars(sys_vars)
    init_plotter(sys_vars)
    return sys_vars

def reset_sys_vars(sys_vars):
    '''reset and check RL system vars before each new session'''
    sys_vars['epi'] = 0
    sys_vars['history'] = deque(maxlen=sys_vars.get('MAX_HISTORY'))
    sys_vars['mean_rewards'] = 0
    sys_vars['solved'] = False
    check_sys_vars(sys_vars)
    return sys_vars

def check_sys_vars(sys_vars):
    '''ensure the requried RL system vars are specified'''
    sys_keys = sys_vars.keys()
    assert all(k in sys_keys for k in required_sys_keys)

def get_env_spec(env):
    '''Helper: return the env specs: dims, actions, reward range'''
    return {
        'state_dim': env.observation_space.shape[0],
        'state_bounds': np.transpose(
            [env.observation_space.low, env.observation_space.high]),
        'action_dim': env.action_space.n,
        'actions': list(range(env.action_space.n)),
        'reward_range': env.reward_range
    }

def report_speed(real_time, total_t):
    '''Report on how fast each time step runs'''
    avg_speed = float(real_time)/float(total_t)
    logger.info('Mean speed: {:.4f} s/step'.format(avg_speed))

def update_history(sys_vars,
                   total_t,
                   total_rewards):
    '''
    update the hisory (list of total rewards)
    max len = MAX_HISTORY
    then report status
    '''

    sys_vars['history'].append(total_rewards)
    # Calculating mean_reward over last 100 episodes
    if (len(sys_vars['history']) > 200):
        mean_rewards = np.mean(sys_vars['history'][-200:])
    else:
        mean_rewards = np.mean(sys_vars['history'])
    solved = (mean_rewards >= sys_vars['SOLVED_MEAN_REWARD'])
    sys_vars['mean_rewards'] = mean_rewards
    sys_vars['solved'] = solved
    live_plot(sys_vars)

    logs = [
        '',
        'Episode: {}, total t: {}, total reward: {}'.format(
            sys_vars['epi'], total_t, total_rewards),
        'Mean rewards over last {} episodes: {:.4f}'.format(
            sys_vars['MAX_HISTORY'], mean_rewards),
        '{:->20}'.format(''),
    ]
    logger.debug('\n'.join(logs))
    check_session_ends(sys_vars)
    return sys_vars

def check_session_ends(sys_vars):
    if (sys_vars['solved'] or
            (sys_vars['epi'] == sys_vars['MAX_EPISODES'] - 1)):
        logger.info('Problem solved? {}. At epi: {}. Params: {}'.format(
            sys_vars['solved'], sys_vars['epi'],
            pp.pformat(sys_vars['param'])))
    if not sys_vars['RENDER']:
        return
    plt.savefig('{}.png'.format(sys_vars['GYM_ENV_NAME']))

def init_plotter(sys_vars):
    if not sys_vars['RENDER']:
        return
    # initialize the plotters
    fig = plt.figure(facecolor='white')

    ax1 = fig.add_subplot(211,
                          frame_on=False,
                          title=str(sys_vars['param']) +
                          '\ntotal rewards per episode',
                          ylabel='total rewards')
    p1, = ax1.plot([], [])
    plotters['total rewards'] = (ax1, p1)

    ax2 = fig.add_subplot(212,
                          frame_on=False,
                          title='mean rewards over last 100 episodes',
                          ylabel='mean rewards')
    p2, = ax2.plot([], [])
    plotters['mean rewards'] = (ax2, p2)

    plt.ion()  # for live plot

def live_plot(sys_vars):
    '''do live plotting'''
    if not sys_vars['RENDER']:
        return
    ax1, p1 = plotters['total rewards']
    p1.set_ydata(np.append(p1.get_ydata(), sys_vars['history'][-1]))
    p1.set_xdata(np.arange(len(p1.get_ydata())))
    ax1.relim()
    ax1.autoscale_view(tight=True, scalex=True, scaley=True)

    ax2, p2 = plotters['mean rewards']
    p2.set_ydata(np.append(p2.get_ydata(), sys_vars['mean_rewards']))
    p2.set_xdata(np.arange(len(p2.get_ydata())))
    ax2.relim()
    ax2.autoscale_view(tight=True, scalex=True, scaley=True)

    plt.draw()
    plt.pause(0.01)

# convert a dict of param ranges into
# a list of cartesian products of param_range
# e.g. {'a': [1,2], 'b': [3]} into
# [{'a': 1, 'b': 3}, {'a': 2, 'b': 3}]
def param_product(param_range):
    keys = param_range.keys()
    range_vals = param_range.values()
    return [dict(zip(keys, vals)) for vals in itertools.product(*range_vals)]

# advanced, experimental code for parallelization
def run_session_average(run_session, problem, param={}):
    '''
    executes the defined run_session function with sys_vars
    run session multiple times for a param
    then average the mean_rewards from them
    '''
    SESSIONS_PER_PARAM = 5
    logger.info(
        'Running average session with param = {}'.format(pp.pformat(param)))
    mean_rewards_history = []
    for i in range(SESSIONS_PER_PARAM):
        sys_vars = run_session(problem, param)
        mean_rewards_history.append(sys_vars['mean_rewards'])
        sessions_mean_rewards = np.mean(mean_rewards_history)
        if sys_vars['solved']:
            break
    logger.info(
        'Sessions mean rewards: {} with param = {}'.format(
            sessions_mean_rewards, pp.pformat(param)))
    return {'param': param, 'sessions_mean_rewards': sessions_mean_rewards}

def select_best_param(run_session, problem, param_grid):
    '''
    Parameter selection
    by running session average for each param parallel
    executes the defined run_session function with problem
    then sort by highest sessions_mean_rewards first
    return the best
    '''
    # NUM_CORES = mp.cpu_count()
    # p = mp.Pool(NUM_CORES)
    # params_means = p.map(
    #     partial(run_session_average, run_session, problem),
    #     param_grid)
    params_means = list(map(
        partial(run_session_average, run_session, problem),
        param_grid))
    params_means.sort(key=lambda pm: pm['sessions_mean_rewards'], reverse=True)
    for pm in params_means:
        logger.info(pp.pformat(pm))
    return params_means[0]


import nibabel as nib
from skimage.measure import find_contours
from skimage.filters import gaussian
import numpy as np
from shapely.geometry import Polygon

class ContoursFromLabelNii(object):
	def __init__(self, labelNii, origNiiPath, nlist):
		#super(ContoursFromLabel, self).__init__()
		self.labelNii = labelNii
		self.origNiiPath = origNiiPath
		self.contourDict = self.getContours( nlist )

	def getContours(self, nlist ):
		self.nList = nlist
		labelImg = self.labelNii.get_data()
		n = 1 # roi number for bitget
		# eventually use for n in self.nList:
		##### BITGET ######
		# should return a binary image with 1's in
		# the voxels where the ROI is
		roiImg = (labelImg & (1<<n)) >> n

		# remove z slices where the structure of interest does not exist
		roiCrop = roiImg[:,:,~(roiImg==0).all(axis=(0,1))]
		zcrop = ~(roiImg==0).all(axis=(0,1))
		#imgCrop = origImg[:,:,~(roiImg==0).all(axis=(0,1))]
		contourDict = {}
		for k, kslice in enumerate(np.rollaxis(roiImg,2)):
			if zcrop[k]:
				filteredRoi = gaussian(kslice, sigma=1, preserve_range =True)
				contours = find_contours(filteredRoi, 0.5, \
					fully_connected='high', positive_orientation='high')
				contourDict = getContourDict(contours, k, self.labelNii.affine, \
					contourDict)
		return contourDict

	def getContourDict(self, contours, k, affine, contourDict, \
		getPolygons = True):
		for contour in contours:
			contourXyz = getContourXyz(contour, k, affine)
			z = contourXyz[0,2]
			if np.abs(z - round(z)) < 1e-3:
				z = round(z)
			contourXyz[:,2] = z
			polygon = Polygon(contourXyz)
			if not contourDict.has_key(z):
				if getPolygons:
					contourDict[z] = [polygon]
				else:
					contourDict[z] = [contourXyz]
			else:
				if getPolygons:
					contourDict[z].append(polygon)
				else:
					contourDict[z].append(contourXyz)
		return contourDict

	def getContourXyz(self, ctr, k, affine):
		ctr = np.c_[ctr, np.ones(ctr.shape[0])*k, np.ones(ctr.shape[0])]
		ctr_xyz1 = np.matmul(affine, ctr[...,np.newaxis]).\
			reshape(ctr.shape)
		return ctr_xyz1[:,0:3]

if __name__ == '__main__':
	labelNii = nib.load("C:\\Users\\megan\\Documents\\label_test.nii")
	origNiiPath = 'N:\\NiiForOptimisation\\p1\\20110905_091632.nii'
	c = ContoursFromLabel(labelnii, origNiiPath)


import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
import random
import sys

from models import setup_db, Question, Category

QUESTIONS_PER_PAGE = 10

def create_app(test_config=None):
    app = Flask(__name__)
    setup_db(app)
    cors = CORS(app)

    @app.after_request
    def after_request(response):
        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add("Access-Control-Allow-Headers", "*")
        response.headers.add("Access-Control-Allow-Methods", "*")
        return response

    @app.route('/categories')
    def get_categories():
        data = Category.query.all()
        dictCategories = {}
        for category in data:
            dictCategories[category.id] = category.type
        return jsonify({
            'categories': dictCategories,
            'success': True
        }), 200

    @app.route('/questions')
    def get_questions():
        page = int(request.args.get('page', 1))
        data = Question.query.all()
        res = list(map(lambda e: e.format(), data))

        catData = Category.query.all()
        dictCategories = {}
        for category in catData:
            dictCategories[category.id] = category.type
        return jsonify({
            'questions': res[(page - 1) * QUESTIONS_PER_PAGE:page * QUESTIONS_PER_PAGE],
            'success': True,
            'total_questions': len(res),
            'categories': dictCategories,
            'current_category': 0
        }), 200

    @app.route('/questions/<int:id>', methods=['DELETE'])
    def delete_question(id):
        question = Question.query.filter_by(id=id).first()
        try:
            question.delete()
            return jsonify({
                'success': True,
                'deleted': question.id
            }), 200
        except BaseException:
            print(sys.exc_info())
            return jsonify({
                'success': False
            }), 422

    @app.route('/questions', methods=['POST'])
    def post_question():
        data = request.get_json()
        response = None
        if data.get('searchTerm') is not None:
            response = search_question(data)
        else:
            response = create_question(data)
        return response

    def create_question(data):
        question = data.get('question')
        answer = data.get('answer')
        category = data.get('category')
        difficulty = data.get('difficulty')
        if question is None or answer is None or category is None or difficulty is None:
            abort(400)
        try:
            q = Question(question, answer, category, difficulty)
            q.insert()
            return jsonify({
                'success': True,
                'question_id': q.id
            }), 201
        except BaseException:
            print(sys.exc_info())
            return jsonify({
                'success': False
            }), 400

    def search_question(data):
        search_term = data.get('searchTerm')
        if search_term is None:
            abort(400)
        try:
            questions = Question.query.filter(
                Question.question.ilike(
                    '%{}%'.format(search_term))).all()
            questions = list(map(lambda e: e.format(), questions))
            return jsonify({
                'success': True,
                'questions': questions,
                'total_questions': len(questions),
                'current_category': 0
            }), 200
        except BaseException:
            return jsonify({
                'success': False
            }), 400

    @app.route('/categories/<int:category_id>/questions')
    def get_category_questions(category_id):
        try:
            questions = Question.query.filter(
                Question.category == category_id).all()
            questions = list(map(lambda e: e.format(), questions))
            return jsonify({
                'success': True,
                'questions': questions,
                'total_questions': len(questions),
                'current_category': category_id
            }), 200
        except BaseException:
            return jsonify({
                'success': False
            }), 422

    @app.route('/quizzes', methods=['POST'])
    def get_quiz_question():
        data = request.get_json()
        prev_questions = data.get('previous_questions')
        quiz_category = data.get('quiz_category', 0)
        try:
            query = Question.query.filter(Question.id.notin_(prev_questions))
            if quiz_category.get('id') is not 0:
                query = query.filter(
                    Question.category == quiz_category.get('id'))
            res = query.all()
            if(len(res) == 0):  # Less than 5 questions on category
                return jsonify({
                    'success': True
                }), 200
            idx = random.randint(0, len(res) - 1)
            question = res[idx].format()
            return jsonify({
                'success': True,
                'question': question
            }), 200
        except BaseException:
            print(sys.exc_info())
            return jsonify({
                'success': False,
                'message': "Not able to get a new quiz question"
            }), 400

    @app.errorhandler(400)
    def handle_request_error(e):
        return jsonify({
            'success': False,
            'message': "Request error!"
        }), 400

    @app.errorhandler(404)
    def handle_not_found(e):
        return jsonify({
            'success': False,
            'message': "This resource was not found"
        }), 404

    @app.errorhandler(422)
    def handle_unprocessable(e):
        return jsonify({
            'success': False,
            'message': "Unable to proccess this request"
        }), 422

    @app.errorhandler(500)
    def handle_internal_error(e):
        return jsonify({
            'success': False,
            'message': "Internal error!"
        }), 500

    return app


#!/usr/bin/env python
import requests
import aiohttp
import asyncio
import dateutil.parser
import datetime
import pytz
import os.path
import functools
import sys
import json
from jinja2 import Template
from base64 import b64encode

COMMENT_URL = 'https://graph.facebook.com/v2.3/{object_id}/comments'

FB_COMMENT_LIMIT = 5000

COMMENT_LIMIT = 500

def get_comment_count(object_id, access_token):
    """Returns comment count for given object id"""

    comments_response = requests.get(
        COMMENT_URL.format(object_id=object_id),
        params=dict(access_token=access_token, summary=1))
    error = comments_response.json().get('error')
    if error:
        raise Exception(error['message'])
    total_count = comments_response.json()['summary']['total_count']
    return total_count

async def get_comments_chunk(session, object_id, access_token, limit, offset, comment_list):
    """
    Future for fetching single chunk of comment
    """
    after_token = b64encode(str(offset).encode('utf-8'))

    async with session.get(COMMENT_URL.format(object_id=object_id), params=dict(
            access_token=access_token,
            limit=limit,
            fields='comment_count,comments.limit(500){created_time,comment_count},created_time',
            after=after_token)) as response:
        response_json = await response.json()
        error = response_json.get('error')
        if error:
            print(error['message'])

        comment_list.extend(response_json['data'])
        for comment in response_json['data']:
            comment_list.extend(comment.get('comments', {}).get('data', []))

def fetch_data(object_id, access_token):
    """
    Asyncronously fetches first 5000 comments, other are fetched syncronously
    """

    comment_count = get_comment_count(object_id, access_token)
    loop = asyncio.get_event_loop()
    with aiohttp.ClientSession(loop=loop) as session:
        tasks = []
        comment_list = []
        session.max_offset_processed = 0

        for chunk_start in range(0, comment_count, COMMENT_LIMIT):
            tasks.append(asyncio.ensure_future(
                get_comments_chunk(session, object_id, access_token, COMMENT_LIMIT, chunk_start, comment_list)))
        loop.run_until_complete(asyncio.gather(*tasks))

    return comment_list

# Processing
def get_report_interval(min_date, max_date):
    """
    Returns best interval for gived min_date. If first comment was yesterday - it uses hours as delimiter,
    if three years ago and last today - years, if both posted three years ago in a same day - also hours
    """

    if max_date - min_date < datetime.timedelta(days=3):
        return 'hours'
    elif max_date - min_date < datetime.timedelta(days=30):
        return 'days'
    elif max_date - min_date < datetime.timedelta(days=365 * 4):
        return 'months'
    else:
        return 'years'

def strip_date_fields(date, interval):
    """Returns date of the period start, based on the interval"""

    interval_strips = {
        'hours': lambda: date.replace(minute=0, second=0),
        'days': lambda: date.replace(hour=0, minute=0, second=0),
        'months': lambda: date.replace(day=1, hour=0, minute=0, second=0),
        'years': lambda: date.replace(month=1, day=1, hour=0, minute=0, second=0),
    }
    return interval_strips[interval]()

def process_data(comment_list):
    """Converts list of comments to aggregated structure"""
    comment_dict = dict([(comment['id'], comment['created_time']) for comment in comment_list])
    dates = [dateutil.parser.parse(created_time) for created_time in comment_dict.values()]
    report_interval = get_report_interval(min(dates), max(dates))
    date_counters = {}

    for date in dates:
        stripped_date = strip_date_fields(date, report_interval)
        date_counters.setdefault(stripped_date, 0)
        date_counters[stripped_date] += 1
    return date_counters, report_interval

def repr_date(report_interval, date):
    """
    Converts date to presentation format, based on interval.
    For example, Wed, 18:00 for hour-based report or 2016 for year-based
    """
    reprs = {
        'hours': '%a, %H:00',
        'days': '%b %d',
        'months': '%b',
        'years': '%Y'
    }
    return date.strftime(reprs[report_interval])

def render_data(date_counters, report_interval):
    """ Renders processed data to template"""

    comment_labels = list(map(functools.partial(repr_date, report_interval), sorted(date_counters.keys())))
    comment_values = [date_counters[label] for label in sorted(date_counters.keys())]

    with open(os.path.dirname(os.path.abspath(__file__)) + '/report_template.j2') as template:
        template_html = Template(
            template.read()).render(
                comment_labels=json.dumps(comment_labels), comment_values=json.dumps(comment_values))

    with open(os.path.dirname(os.path.abspath(__file__)) + '/report.html', 'w') as report:
        report.write(template_html)

def main():
    #  Example: python fetcher.py 10153764743456749 CAAAABHLBW9ABAIjN3Rm9Y9WZCp0st3ZC81RNZAg...
    comment_list = fetch_data(sys.argv[1], sys.argv[2])
    date_counters, report_interval = process_data(comment_list)
    render_data(date_counters, report_interval)

if __name__ == "__main__":
    main()


"""
Create or import top-level classes/methods.
isort:skip_file
"""
from typing import Tuple, Union

from sklearn.base import RegressorMixin as SkLearnModel
from statsmodels.base.wrapper import ResultsWrapper as StatsModel

# Here we list any class we might use. In principle, they should be expected to have a fit(X, y) and a predict(X)
# method.
# However, the statsmodels library provides Model and Results classes. The former only hold X and y data, while the
# latter represents a fitted (parameterised) Model (with a working predict method), plus the result statistics
# (the Model object is also in there).
MODEL_CLASSES = (StatsModel, SkLearnModel)
MODEL_TYPES = Union[MODEL_CLASSES]

# First public import block
from timetomodel.speccing import (
    DBSeriesSpecs,
    DFFileSeriesSpecs,
    ModelSpecs,
    ObjectSeriesSpecs,
)

class ModelState(object):
    """
    This class abstracts all information we need to describe a model and how it was made.
    It is simply a container to hold all model-relevant state information:
    The trained model and the ModelSpecs it is based on."""

    model: MODEL_TYPES  # a fitted model
    specs: ModelSpecs

    def __init__(self, model, specs):
        if not isinstance(model, MODEL_CLASSES):
            raise Exception(
                "ModelState(): model parameter needs to be of type %s instead of type %s"
                % (str(MODEL_CLASSES), type(model))
            )
        self.model = model
        if not isinstance(specs, ModelSpecs):
            raise Exception(
                "ModelState(): specs parameter needs to be of type <ModelSpecs>"
            )
        self.specs = specs

    def split(self) -> Tuple[MODEL_TYPES, ModelSpecs]:
        """Return model and specs separately"""
        return self.model, self.specs

    def __repr__(self):
        return "ModelState: <%s, %s>" % (self.model, self.specs)

# Second public import block
from timetomodel.forecasting import make_forecast_for, make_rolling_forecasts
from timetomodel.modelling import (
    create_fitted_model,
    evaluate_models,
    model_param_grid_search,
)


# ------------------------------------------------------------------------------
# pose.pytorch
# Copyright (c) 2018-present Microsoft
# Licensed under The Apache-2.0 License [see LICENSE for details]
# Written by Bin Xiao (Bin.Xiao@microsoft.com)
# ------------------------------------------------------------------------------

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import os
import pprint
import logging
import time
import numpy as np
from PIL import Image

import torch
import torch.nn.parallel
# import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms

# import pyximport
# pyximport.install()

import _init_paths
from config import cfg
from config import update_config
# from core.loss import JointsMSELoss
# from core.function import validate
# from utils.utils import create_logger

import dataset
import models

def parse_args():
    parser = argparse.ArgumentParser(description='Train keypoints network')
    # general
    parser.add_argument('--cfg',
                        help='experiment configure file name',
                        required=True,
                        type=str)

    parser.add_argument('opts',
                        help="Modify config options using the command-line",
                        default=None,
                        nargs=argparse.REMAINDER)

    parser.add_argument('--modelDir',
                        help='model directory',
                        type=str,
                        default='')
    parser.add_argument('--logDir',
                        help='log directory',
                        type=str,
                        default='')
    parser.add_argument('--dataDir',
                        help='data directory',
                        type=str,
                        default='')
    parser.add_argument('--prevModelDir',
                        help='prev Model directory',
                        type=str,
                        default='')

    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    update_config(cfg, args)

    logger = logging.getLogger()
    # logger, final_output_dir, tb_log_dir = create_logger(
    #     cfg, args.cfg, 'valid')
    #
    # logger.info(pprint.pformat(args))
    # logger.info(cfg)

    # cudnn related setting
    # cudnn.benchmark = cfg.CUDNN.BENCHMARK
    torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
    torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED

    model = eval('models.'+cfg.MODEL.NAME+'.get_pose_net')(cfg, is_train=False)

    logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE))
    model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE, map_location='cpu'), strict=False)

    model = torch.nn.DataParallel(model, device_ids=cfg.GPUS)

    # # define loss function (criterion) and optimizer
    # criterion = JointsMSELoss(
    #     use_target_weight=cfg.LOSS.USE_TARGET_WEIGHT
    # ).cuda()

    # Data loading code
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
    )

    data_transforms = transforms.Compose([transforms.ToTensor(), normalize])

    # valid_dataset = eval('dataset.'+cfg.DATASET.DATASET)(
    #     cfg, cfg.DATASET.ROOT, cfg.DATASET.TEST_SET, False,
    #     transforms.Compose([
    #         transforms.ToTensor(),
    #         normalize,
    #     ])
    # )
    # valid_loader = torch.utils.data.DataLoader(
    #     valid_dataset,
    #     batch_size=cfg.TEST.BATCH_SIZE_PER_GPU*len(cfg.GPUS),
    #     shuffle=False,
    #     num_workers=cfg.WORKERS,
    #     pin_memory=True
    # )

    # # evaluate on validation set
    # validate(cfg, valid_loader, valid_dataset, model, criterion,
    #          final_output_dir, tb_log_dir)

    count_warm_up = 10
    count_actual = 50
    batch_size = 3

    #in_img = torch.zeros((batch_size, 3, 192, 256), dtype=torch.float32)
    in_img = Image.open('test_data/4_yoga.jpg')

    # Normalization
    wd, ht = 256, 192
    in_img = in_img.resize((wd, ht))
    in_img = np.moveaxis(np.array(in_img), -1, 0)  # Convert to channels-first
    in_img = (in_img.astype(np.float32)/255 - np.array([0.485, 0.456, 0.406]).reshape(3, 1, 1)) / np.array([0.229, 0.224, 0.225]).reshape(3, 1, 1)
    in_img = in_img[np.newaxis, :]  # Add new axis for batch
    in_img = np.concatenate([in_img] * batch_size)  # Create a batch

    print('Batch shape: {:}'.format(in_img.shape))
    in_img = torch.Tensor(in_img)

    # switch to evaluate mode
    model.eval()

    # ----------------------------------
    start_time = time.time()
    with torch.no_grad():
        for i in range(count_warm_up):
            outputs = model(in_img)
        print('Time at warm up: {:.0f}ms'.format(1000 * (time.time() - start_time)/(count_warm_up * batch_size)))

    # ----------------------------------
    start_time = time.time()
    with torch.no_grad():
        for i in range(count_actual):
            outputs = model(in_img)

            # if isinstance(outputs, list):
            #     output = outputs[-1]
            # else:
            #     output = outputs

            # preds, maxvals = get_final_preds(config, output.clone().cpu().numpy(), c, s)
            #
            # all_preds = np.zeros((config.MODEL.NUM_JOINTS, 3), dtype=np.float32)
            # all_boxes = np.zeros(6)
            # all_preds[ :, 0:2] = preds[:, :, 0:2]
            # all_preds[ :, 2:3] = maxvals
            # # double check this all_boxes parts
            # all_boxes[0:2] = c[:, 0:2]
            # all_boxes[2:4] = s[:, 0:2]
            # all_boxes[4] = np.prod(s*200, 1)
            # all_boxes[5] = score

        print('Time: {:.0f}ms'.format(1000 * (time.time() - start_time) / (count_actual * batch_size)))

if __name__ == '__main__':
    main()


#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
from logo_detect import *
import pprint
from selenium import webdriver
from scrapy_mop import scrapy_mop
try:
    import tkinter as tk
    from tkinter import messagebox
except:
    import Tkinter as tk
    import tkMessageBox as messagebox

sys.path.append(os.path.join(os.path.dirname(__file__), '../'))

import pygubu

class Myapp:
    def __init__(self, master):

        self.check_1 = 0
        self.check_2 = 0
        self.check_3 = 0
        self.check_4 = 0
        self.master = master
        self.builder = builder = pygubu.Builder()
        fpath = os.path.join(os.path.dirname(__file__),"main_ui.ui")
        builder.add_from_file(fpath)
        mainwindow = builder.get_object('mainwindow', master)
        builder.connect_callbacks(self)

    def local_detect_button_click(self):
        src_image_input_txt = self.builder.get_object('src_image_input_txt')
        dst_image_input_txt = self.builder.get_object('dst_image_input_txt')
        src_path = src_image_input_txt.get()
        dst_path = dst_image_input_txt.get()
        if not os.path.isdir(src_path):
            messagebox.showinfo("title","请输入正确的源路径")
        elif not os.path.isdir(dst_path):
            messagebox.showinfo("title","请输入正确的目标路径")
        elif self.check_1==0 and self.check_2==0:
            messagebox.showinfo("title","请选择一个模型")
        elif self.check_1==1 and self.check_2==1:
            messagebox.showinfo("title","只能选择一个模型")
        else:
            if self.check_1==1:
                #initialize the object
                logo_detect_obj = detector('logo_detect.prototxt','logo_iter_98000.caffemodel','mean.npy',(128,128),0.0078125,1)
                logo_detect_obj.local_detect(src_path,dst_path)
                messagebox.showinfo("title","检测完毕")
            elif self.check_2==1:
                logo_detect_obj = detector('logo_detect.prototxt','logo_iter_198000.caffemodel','mean.npy',(128,128),0.0078125,1)
                logo_detect_obj.local_detect(src_path,dst_path)
                messagebox.showinfo("title","检测完毕")
    def Checkbutton_1_click(self):
        if self.check_1==0:
            self.check_1=1
        else:
            self.check_1=0

    def Checkbutton_2_click(self):
        if self.check_2==0:
            self.check_2=1
        else:
            self.check_2=0
    def Checkbutton_3_click(self):
        if self.check_3==0:
            self.check_3=1
        else:
            self.check_3=0
    def Checkbutton_4_click(self):
        if self.check_4==0:
            self.check_4=1
        else:
            self.check_4=0
    def open_managment_click(self):
        browser = webdriver.Chrome()
        browser.get("http://localhost/admin/sensitive.php")

    def net_detect_button_click(self):

        address_input_txt= self.builder.get_object('address_input_txt')
        address_input = address_input_txt.get()
        if self.check_3==1 and self.check_4==1:
            messagebox.showinfo("title","只能选择一个模型")
        if self.check_3==0 and self.check_4==0:
            messagebox.showinfo("title","必须要选择一个模型")
        if address_input=='mop':
            scarpy_model = scrapy_mop()
            if self.check_3==1:
                logo_detect_obj = detector('logo_detect_full_conv.prototxt','logo_iter_95000_full.caffemodel',111,(333,333),1,1)
                scarpy_model.run(logo_detect_obj)
            else:
                logo_detect_obj = detector('logo_detect_full_conv.prototxt','logo_iter_95000_full.caffemodel',111,(333,333),1,1)
                scarpy_model.run(logo_detect_obj)
            messagebox.showinfo("title","爬取，检测完毕！")

if __name__ == '__main__':
    root = tk.Tk()
    app = Myapp(root)
    root.mainloop()


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

from django.shortcuts import render

from novaclient.client import Client
import uuid

# Create your views here.
nova = 0
def process():
	# api: https://docs.openstack.org/developer/python-novaclient/ref/v2/client.html
	d = {}
	d['version'] = '2.1'
	d['username'] = 'testuser_1u'  # os.environ['OS_USERNAME']
	d['password'] = 'PASSW0RD1'  # os.environ['OS_PASSWORD']
	d['auth_url'] = 'https://acopenstack.rit.edu:5000/v3'  # os.environ['OS_AUTH_URL']
	d['project_name'] = 'testdev_1'  # os.environ['OS_TENANT_NAME']
	# NOTE. If the username is an RIT DCE (contains @ad.rit.edu), use `default` as the domain
	d['user_domain_name'] = 'local'
	d['project_domain_name'] = 'local'
	d['insecure'] = True
	global nova
	nova = Client(**d)

process()

def index(request):
	output = "Spawn a VM\n\n"
	global nova
	for server in nova.servers.list():
        	print(server.networks)
        	try:
            		console = server.get_vnc_console('novnc')
            		line = "Console Delete  {1}  {2}  {3}\n".format(console['console']['url'], server.name, server.id, server.networks['Shared'][0])
        	except Exception:
            		line = "{0} is in motion (running task).\n".format(server.id)
        	output += line

	return render(request, 'createvm/home.html', {'content':[output, 'test1']})

def spawn(request):
    global nova
    id = str(uuid.uuid4())
    image = 'd611ada3-b77b-4727-bbdf-b305eb89ed07'
    flavor = nova.flavors.find(name="m1.small")
    nics = [{'net-id': '027e77e6-6599-4b26-8b6c-f5d953453fdc'}]
    instance = nova.servers.create(name="server-" + id, image=image, flavor=flavor, nics=nics)
    output = "Task started.\n\nID: {0}\nName: {1}".format(instance.id, instance.name)

    return render(request, 'createvm/spawn.html', {'media':[output, 'test1']})

def delete(request, id):
    global nova
    server = nova.servers.get(id)
    server.delete()
    return render(request, 'createvm/delete.html', {'op':['deleted:', server.id]})


#!/usr/bin/env python3

import argparse
import configparser
import json

import trino_api
import utils

def calculate_throughput(size, time):
    return (size / time) if time > 0 else 0

def parse_query_json(query_json):
    query_stats = query_json['queryStats']
    print('=== Query Info ===')
    print('query ID        : ', query_json['queryId'])
    print('state           : ', query_json['state'])
    print('elapsed time    : ', query_stats['elapsedTime'])
    print('total splits    : ', query_stats['totalDrivers'])
    print('completed splits: ', query_stats['completedDrivers'])
    print('running splits  : ', query_stats['runningDrivers'])
    print('queued splits   : ', query_stats['queuedDrivers'])
    print('blocked splits  : ', query_stats['blockedDrivers'])
    stages = utils.build_stages(query_json)
    for stage in stages:
        stage_plan = json.loads(stage['plan']['jsonRepresentation'])
        # we can only care about scan stages
        if 'scan' not in stage_plan['name'].lower():
            continue
        table_name = utils.get_table_name(stage_plan)
        print (' === Source table - {0} ==='.format(table_name))
        print('  Stage ID is: ', stage['stageId'])
        print('  State      : ', stage['state'])
        print('  type       : ', stage_plan['name'])
        print('  table      : ', stage_plan['identifier'])
        stage_catalog_name = utils.get_catalog_name(stage_plan)
        if stage_catalog_name is None:
            stage_catalog_name = "UNKNOWN"
        stage_stats = stage['stageStats']
        physical_input_stage_throughput = calculate_throughput(
            utils.parse_read_data_size(stage_stats['physicalInputDataSize']),
            utils.parse_read_time(stage_stats['physicalInputReadTime'])
        )
        print('  physical input data size        : ', stage_stats['physicalInputDataSize'])
        print('  physical input read time        : ', stage_stats['physicalInputReadTime'])
        print('  physical input throughput (MB/s): ', physical_input_stage_throughput)
        tasks = stage['tasks']
        for task in tasks:
            worker_host = task['taskStatus']['self'].split("//")[1].split(":")[0]
            print('    === Task {0} on worker {1} ==='.format(task['taskStatus']['taskId'], worker_host))
            task_stats = task['stats']
            physical_input_task_throughput = calculate_throughput(
                utils.parse_read_data_size(task_stats['physicalInputDataSize']),
                utils.parse_read_time(task_stats['physicalInputReadTime'])
            )
            print('    physical input data size        : ', task_stats['physicalInputDataSize'])
            print('    physical input read time        : ', task_stats['physicalInputReadTime'])
            print('    physical input throughput (MB/s): ', physical_input_task_throughput)

    session_info = query_json['session']
    print(' === Client Destination - {0} ({1}) ==='.format(session_info['remoteUserAddress'], session_info['source']))
    output_stage_stats = query_json['outputStage']['stageStats']
    print('  buffered                       : ', output_stage_stats['bufferedDataSize'])
    print('  output size                    : ', output_stage_stats['outputDataSize'])
    query_elapsed_time = utils.parse_read_time(query_json['queryStats']['elapsedTime'])
    client_output_throughput = (utils.parse_read_data_size(output_stage_stats['outputDataSize']) / query_elapsed_time) if query_elapsed_time > 0 else 0
    print('  client output throughput (MB/s): ', client_output_throughput)

parser = argparse.ArgumentParser(description='Accepted parameters')
parser.add_argument('-q', '--query', help='query ID', required=True)
args = parser.parse_args()

config = configparser.ConfigParser()
config.read('config.ini')
trino_config = config['trino']

session = trino_api.new_session(trino_config['user'], trino_config['password'])
coordinator_uri = trino_config['http_scheme'] + '://' + trino_config['host'] + ':' + trino_config['port']
verify_certs = trino_config['verify_certs'].lower() == 'true'

parse_query_json(
    trino_api.get_query_json(session, verify_certs, coordinator_uri, trino_config['user'], args.query)
)


import numpy as np
import cv2
import torch
import torch.nn as nn
from torch.nn import functional as F
import albumentations
from albumentations.core.transforms_interface import DualTransform
from albumentations.augmentations import functional as F_alb

class UnNormalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, tensor):
        """
        Args:
            tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
        Returns:
            Tensor: Normalized image.
        """
        for t, m, s in zip(tensor, self.mean, self.std):
            t.mul_(s).add_(m)
            # The normalize code -> t.sub_(m).div_(s)
        return tensor

def erode(img, kernel_size=5, iterations = 1, p=0.5):
    kernel = np.ones((kernel_size, kernel_size), np.uint8)
    erosion = cv2.erode(img,kernel,iterations = iterations)
    return erosion

def dilate(img, kernel_size=5, iterations = 1, p=0.5):
    kernel = np.ones((kernel_size, kernel_size), np.uint8)
    dilation = cv2.erode(img, kernel, iterations = iterations)
    return dilation

def rand_bbox(size, lam):
    W = size[2]
    H = size[3]
    cut_rat = np.sqrt(1. - lam)
    cut_w = np.int(W * cut_rat)
    cut_h = np.int(H * cut_rat)

    # uniform
    cx = np.random.randint(W)
    cy = np.random.randint(H)

    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)

    return bbx1, bby1, bbx2, bby2

def cutmix(data, targets1, targets2, targets3, alpha):
    indices = torch.randperm(data.size(0))
    shuffled_data = data[indices]
    shuffled_targets1 = targets1[indices]
    shuffled_targets2 = targets2[indices]
    shuffled_targets3 = targets3[indices]

    lam = np.random.beta(alpha, alpha)
    bbx1, bby1, bbx2, bby2 = rand_bbox(data.size(), lam)
    data[:, :, bbx1:bbx2, bby1:bby2] = data[indices, :, bbx1:bbx2, bby1:bby2]
    # adjust lambda to exactly match pixel ratio
    lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (data.size()[-1] * data.size()[-2]))

    targets = [targets1, shuffled_targets1, targets2, shuffled_targets2, targets3, shuffled_targets3, lam]
    return data, targets

def mixup(data, targets1, targets2, targets3, alpha):
    indices = torch.randperm(data.size(0))
    shuffled_data = data[indices]
    shuffled_targets1 = targets1[indices]
    shuffled_targets2 = targets2[indices]
    shuffled_targets3 = targets3[indices]

    lam = np.random.beta(alpha, alpha)
    data = data * lam + shuffled_data * (1 - lam)
    targets = [targets1, shuffled_targets1, targets2, shuffled_targets2, targets3, shuffled_targets3, lam]

    return data, targets

def cutmix_criterion(preds1,preds2,preds3, targets, criterion='ohem', rate=0.7):
    targets1, targets2,targets3, targets4,targets5, targets6, lam = targets[0], targets[1], targets[2], targets[3], targets[4], targets[5], targets[6]
    if criterion=='ohem':
        criterion = ohem_loss
        return lam * criterion(rate, preds1, targets1) + (1 - lam) * criterion(rate, preds1, targets2), lam * criterion(rate, preds2, targets3) + (1 - lam) * criterion(rate, preds2, targets4), lam * criterion(rate, preds3, targets5) + (1 - lam) * criterion(rate, preds3, targets6)
    else:
        criterion = nn.CrossEntropyLoss(reduction='mean')
        return lam * criterion(preds1, targets1) + (1 - lam) * criterion(preds1, targets2), lam * criterion(preds2, targets3) + (1 - lam) * criterion(preds2, targets4), lam * criterion(preds3, targets5) + (1 - lam) * criterion(preds3, targets6)

def mixup_criterion(preds1,preds2,preds3, targets, criterion='ohem', rate=0.7):
    targets1, targets2,targets3, targets4,targets5, targets6, lam = targets[0], targets[1], targets[2], targets[3], targets[4], targets[5], targets[6]
    if criterion=='ohem':
        criterion = ohem_loss
        return lam * criterion(rate, preds1, targets1) + (1 - lam) * criterion(rate, preds1, targets2), lam * criterion(rate, preds2, targets3) + (1 - lam) * criterion(rate, preds2, targets4), lam * criterion(rate, preds3, targets5) + (1 - lam) * criterion(rate, preds3, targets6)
    else:
        criterion = nn.CrossEntropyLoss(reduction='mean')
        return lam * criterion(preds1, targets1) + (1 - lam) * criterion(preds1, targets2), lam * criterion(preds2, targets3) + (1 - lam) * criterion(preds2, targets4), lam * criterion(preds3, targets5) + (1 - lam) * criterion(preds3, targets6)

class RandomErasing:
    def __init__(self, p, area_ratio_range, min_aspect_ratio, max_attempt):
        self.p = p
        self.max_attempt = max_attempt
        self.sl, self.sh = area_ratio_range
        self.rl, self.rh = min_aspect_ratio, 1. / min_aspect_ratio

    def __call__(self, image):
        image = np.asarray(image).copy()

        if np.random.random() > self.p:
            return image

        h, w = image.shape[:2]
        image_area = h * w

        for _ in range(self.max_attempt):
            mask_area = np.random.uniform(self.sl, self.sh) * image_area
            aspect_ratio = np.random.uniform(self.rl, self.rh)
            mask_h = int(np.sqrt(mask_area * aspect_ratio))
            mask_w = int(np.sqrt(mask_area / aspect_ratio))

            if mask_w < w and mask_h < h:
                x0 = np.random.randint(0, w - mask_w)
                y0 = np.random.randint(0, h - mask_h)
                x1 = x0 + mask_w
                y1 = y0 + mask_h
                image[y0:y1, x0:x1] = np.random.uniform(0, 1)
                break

        return image

def ohem_loss( rate, cls_pred, cls_target ):

    batch_size = cls_pred.size(0)
    ohem_cls_loss = F.cross_entropy(cls_pred, cls_target, reduction='none', ignore_index=-1)

    sorted_ohem_loss, idx = torch.sort(ohem_cls_loss, descending=True)
    keep_num = min(sorted_ohem_loss.size()[0], int(batch_size*rate) )
    if keep_num < sorted_ohem_loss.size()[0]:
        keep_idx_cuda = idx[:keep_num]
        ohem_cls_loss = ohem_cls_loss[keep_idx_cuda]
    cls_loss = ohem_cls_loss.sum() / keep_num
    return cls_loss


#!/usr/bin/env python3

from neml import history, interpolate
from neml.math import tensors, rotations
from neml.cp import crystallography, slipharden, sliprules

from common import differentiate
from nicediff import *

import unittest
import numpy as np
import numpy.linalg as la

class CommonSlipRule(object):
  def test_d_slip_d_stress(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        d = self.model.d_slip_d_s(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed)
        nd = diff_scalar_symmetric(lambda s: self.model.slip(g, i, s, self.Q, self.H,
          self.L, self.T, self.fixed), self.S)
        self.assertEqual(d, nd)

  def test_d_slip_d_hist(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        d = np.array(self.model.d_slip_d_h(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed))
        nd = np.array(diff_history_scalar(lambda h: self.model.slip(g, i, self.S, self.Q, h,
          self.L, self.T, self.fixed), self.H))
        self.assertTrue(np.allclose(nd.reshape(d.shape), d))

  def test_d_hist_rate_d_stress(self):
    d = np.array(self.model.d_hist_rate_d_stress(self.S, self.Q, self.H, self.L, self.T, self.fixed))
    nd = diff_history_symmetric(lambda s: self.model.hist_rate(s, self.Q, self.H, self.L,
      self.T, self.fixed), self.S)
    self.assertTrue(np.allclose(nd.reshape(d.shape), d))

  def test_d_hist_rate_d_hist(self):
    d = np.array(self.model.d_hist_rate_d_hist(self.S, self.Q, self.H, self.L, self.T, self.fixed))
    nd = diff_history_history(lambda h: self.model.hist_rate(self.S, self.Q, h, self.L,
      self.T, self.fixed), self.H)
    self.assertTrue(np.allclose(nd.reshape(d.shape), d))

class CommonSlipStrengthSlipRule(object):
  def test_init_hist(self):
    H1 = history.History()
    self.model.populate_history(H1)
    self.model.init_history(H1)

    H2 = history.History()
    self.strengthmodel.populate_history(H2)
    self.strengthmodel.init_history(H2)

    self.assertTrue(np.allclose(np.array(H1),
      np.array(H2)))

  def test_slip(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        rs = self.L.shear(g, i, self.Q, self.S)
        strength = self.strength + self.static
        self.assertTrue(np.isclose(self.model.slip(g, i, self.S, self.Q, self.H, self.L, self.T, self.fixed),
          self.model.sslip(g, i, rs, strength, self.T)))

  def test_d_hist_rate(self):
    self.assertTrue(np.allclose(
      np.array(self.model.hist_rate(self.S, self.Q, self.H, self.L, self.T, self.fixed)),
      np.array(self.strengthmodel.hist(self.S, self.Q, self.H, self.L, self.T, self.model, self.fixed))))

  def test_d_sslip_d_tau(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        nd = differentiate(lambda t: self.model.sslip(g, i, t, self.strength, self.T),
            self.tau)
        d = self.model.d_sslip_dtau(g, i, self.tau, self.strength, self.T)
        self.assertTrue(np.isclose(nd,d))

  def test_d_sslip_d_strength(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        nd = differentiate(lambda s: self.model.sslip(g, i, self.tau, s, self.T), self.strength)
        d = self.model.d_sslip_dstrength(g, i, self.tau, self.strength, self.T)
        print(nd)
        print(d)
        self.assertTrue(np.isclose(nd, d))

class TestPowerLawSlip(unittest.TestCase, CommonSlipStrengthSlipRule, CommonSlipRule):
  def setUp(self):
    self.L = crystallography.CubicLattice(1.0)
    self.L.add_slip_system([1,1,0],[1,1,1])

    self.Q = rotations.Orientation(35.0,17.0,14.0, angle_type = "degrees")
    self.S = tensors.Symmetric(np.array([
      [100.0,-25.0,10.0],
      [-25.0,-17.0,15.0],
      [10.0,  15.0,35.0]]))
    self.strength = 35.0
    self.H = history.History()
    self.H.add_scalar("strength")
    self.H.set_scalar("strength", self.strength)

    self.T = 300.0

    self.tau0 = 10.0
    self.tau_sat = 50.0
    self.b = 2.5

    self.strengthmodel = slipharden.VoceSlipHardening(self.tau_sat, self.b, self.tau0)

    self.static = self.tau0

    self.g0 = 1.0
    self.n = 3.0
    self.model = sliprules.PowerLawSlipRule(self.strengthmodel, self.g0, self.n)

    self.tau = 33.0

    self.fixed = history.History()

  def test_scalar_rate(self):
    for g in range(self.L.ngroup):
      for i in range(self.L.nslip(g)):
        self.assertTrue(np.isclose(self.model.sslip(g, i, self.tau, self.strength, self.T),
          self.g0 * np.abs(self.tau/self.strength)**(self.n-1.0) * self.tau/self.strength))

class TestBiVoceSlip(unittest.TestCase, CommonSlipStrengthSlipRule, CommonSlipRule):
  def setUp(self):
    self.L = crystallography.CubicLattice(1.0)
    self.L.add_slip_system([1,1,0],[1,1,1])

    self.Q = rotations.Orientation(35.0,17.0,14.0, angle_type = "degrees")
    self.S = tensors.Symmetric(np.array([
      [100.0,-25.0,10.0],
      [-25.0,-17.0,15.0],
      [10.0,  15.0,35.0]]))
    self.strength_1 = 35.0
    self.strength_2 = 25.0
    self.strength = self.strength_1 + self.strength_2
    self.H = history.History()
    self.H.add_scalar("strength0")
    self.H.set_scalar("strength0", self.strength_1)
    self.H.add_scalar("strength1")
    self.H.set_scalar("strength1", self.strength_2)

    self.T = 300.0

    self.tau0 = 10.0
    self.tau_sat = 50.0
    self.b = 2.5

    self.strengthmodel = slipharden.SumSlipSingleStrengthHardening(
        [slipharden.VoceSlipHardening(self.tau_sat, self.b, self.tau0),
          slipharden.VoceSlipHardening(self.tau_sat/2, self.b/2, self.tau0/2)])

    self.static = self.tau0 + self.tau0 / 2

    self.g0 = 1.0
    self.n = 3.0
    self.model = sliprules.PowerLawSlipRule(self.strengthmodel, self.g0, self.n)

    self.tau = 33.0

    self.fixed = history.History()


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

from __future__ import division, print_function, absolute_import

import os
from timeit import time
import warnings
import sys
import cv2
import numpy as np
from PIL import Image
from src.yolo import YOLO
import colorsys
from deep_sort import preprocessing
from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
from deep_sort.detection import Detection as ddet
import argparse
from src.demo_image import POSE
warnings.filterwarnings('ignore')

def arg_parse():

    parser = argparse.ArgumentParser(description='openpose Video Detection Module')

    parser.add_argument("--video", dest = 'video', help = "Video to run detection upon",
                default = "/home/ftp/DataSet/Hand-Dataset/video/tennis.mp4", type = str)
    parser.add_argument('--model', dest = 'model', type=str, default='model/keras/model.h5', help='path to the weights file')
    return parser.parse_args()

def deep_sort():
    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0
    # deep_sort
    model_filename = 'model/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename,batch_size=1)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)
    return tracker, encoder, nms_max_overlap

def main(yolo):

    # Generate colors for drawing bounding boxes.
    hsv_tuples = [(x / 80, 1., 1.)
                for x in range(80)]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(
        map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
            colors))
    tracker, encoder, nms_max_overlap = deep_sort()
    classes = load_classes('model/coco_classes.txt')
    writeVideo_flag = True
    args = arg_parse()
    videofile = args.video
    keras_weights_file = args.model
    # 加载openpose模型
    pose = POSE(keras_weights_file)
    video_capture = cv2.VideoCapture(videofile)
    assert video_capture.isOpened(), 'Cannot capture source'

    if writeVideo_flag:
    # Define the codec and create VideoWriter object
        w = int(video_capture.get(3))
        h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'MP4V')
        outname = os.path.join('result_video',videofile.split('/')[-1])
        out = cv2.VideoWriter(outname, fourcc, 25, (w, h))
        list_file = open('detection.txt', 'w')
        frame_index = -1

    fps = 0.0
    #athlete_flag = [0,0]

    while video_capture.isOpened():
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break
        t1 = time.time()

        image = Image.fromarray(frame)
        boxs = yolo.detect_image_track(image)
        boxs = np.array(boxs)
        #print("box_num",len(boxs))
        print("box_des",boxs)
        #cls = int(boxs[-1])
        #label = "{0}".format(classes[cls])
        #t_size = cv2.getTextSize(boxs[0][-1], cv2.FONT_HERSHEY_PLAIN, 1 , 1)[0]
        features = encoder(frame,boxs)

        # score to 1.0 here).
        detections = [Detection(bbox[:-1],bbox[-1], 1.0, feature) for bbox, feature in zip(boxs, features)]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)
        flag_boxs = 0
        for track in tracker.tracks:
            if track.is_confirmed() and track.time_since_update >1 :
                continue
            bbox = track.to_tlbr()
            label = "{0}".format(classes[int(track.nameclass)])
            c1 = (int(bbox[0]),int(bbox[1]))
            c2 = (int(bbox[2]),int(bbox[3]))
            if label == 'person':
                if c1[0]>256 and c1[1]>95 and c2[0]<1053 and c2[1]<717:
                    label = 'athlete'
                elif c1[0]>261 and c1[1]>20 and c2[0]<1012 and c2[1]<162:
                    label = 'baseline_referee'
                elif c1[0]>1 and c1[1]>95 and c2[0]<175 and c2[1]<413:
                    label = 'sideline_referee'
                elif c1[0]>1002 and c1[1]>128 and c2[0]<1181 and c2[1]<387:
                    label = 'referee'
                elif c1[0]>1002 and c1[1]>245 and c2[0]<1170 and c2[1]<387:
                    label = 'Caddy'
                elif c1[0]>166 and c1[1]>232 and c2[0]<295 and c2[1]<347:
                    label = 'Caddy'
            color = colors[int(track.nameclass)]
            cv2.rectangle(frame, c1, c2,color, 2)
            t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1 , 2)[0]
            name = label +'_' +str(track.track_id)
            c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
            cv2.rectangle(frame, c1, c2,color, -1)
            cv2.putText(frame, name, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1,(0,0,0), 1);
            #cv2.putText(frame, str(track.nameclass)+'_'+str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)

            #cv2.putText(frame, str(boxs[flag_boxs][-1])+'_'+str(track.track_id),(int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_PLAIN, 1, [225,255,255], 1)
            flag_boxs += 1
            #canvas = pose.detect_pose(frame[37:704,69:1153,:])
            #frame[37:704,69:1153,:] = canvas

        cv2.imshow('', frame)

        if writeVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1
            list_file.write(str(frame_index)+' ')
            if len(boxs) != 0:
                for i in range(0,len(boxs)):
                    list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ')
            list_file.write('\n')

        fps  = ( fps + (1./(time.time()-t1)) ) / 2
        print("fps= %f"%(fps))

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()

def load_classes(namesfile):
    fp = open(namesfile, "r")
    names = fp.read().split("\n")[:-1]
    return names

if __name__ == '__main__':
    main(YOLO())


"""
Imports site and captain data into ROOM.
Intended to be run with the remote_api:
bash> export PYTHONPATH=.:$PYTHONPATH
bash> python $(which remote_api_shell.py) -s rebuildingtogethercaptain.appspot.com
s~rebuildingtogethercaptain-hrd> import import_csv

Great instructions at
https://github.com/babybunny/rebuildingtogethercaptain/issues/181

# or for development..
bash> python $(which remote_api_shell.py) -s localhost:8081
dev~rebuildingtogethercaptain> reload(import_csv)
<module 'import_csv' from '/Users/babybunny/appengine/rebuildingtogethercaptain/import_csv.py'>
dev~rebuildingtogethercaptain> import_csv.import_sites(input_csv="../2012_ROOMS_site_info_sample.csv")
dev~rebuildingtogethercaptain> import_csv.import_captains(input_csv="../2012_ROOMS_Captain_email_sample.csv")
"""

import csv
import logging
import main  # to initialize Django
from room import models
from google.appengine.ext import db

##############
# update me! #
##############
PROGRAM = '2017 NRD'

def import_photos(input_csv="../2012_ROOMS_phote.csv"):
  """Change input_csv to actual input file - the default is test data."""
  reader = csv.DictReader(open(input_csv))
  for s in reader:
    number = s["Site ID"]
    site = models.NewSite.all().filter('number =', number).get()
    if not site:
      continue
    if s['Flickr Pages']:
      site.photo_link = s['Flickr Pages']
      site.put()

def import_sites(input_csv="../2012_ROOMS_site_info_sample.csv"):
  """Change input_csv to actual input file - the default is test data."""
  reader = csv.DictReader(open(input_csv))
  for s in reader:
    number = s["Site ID"]
    site = models.NewSite.all().filter('number =', number).get()
    if site:
      logging.info('site %s exists, skipping', number)
      continue
    else:
      site = models.NewSite(number=number)
    site.program = PROGRAM
    site.budget = int(s["Budgeted Cost in Campaign"]) if s["Budgeted Cost in Campaign"] else 0

    # Because Python 2.x csv module only reads ascii.
    def clean_s(k):
      return s[k].replace('\n', ' ').replace('\xe2', "'").replace('\x80', "'").replace('\x99', '').replace('\xc3', '').replace('\x95', '').replace('\xb1', '').encode('ascii', 'replace')

    site.name = clean_s("Repair Application: Applicant's Name")
    site.street_number = clean_s("Street Address")
    site.city_state_zip = "%s CA, %s" % (
        clean_s("Repair Application: Recipient's City"),
        clean_s("Repair Application: Recipient's Zip Code"))
    site.applicant = clean_s("Repair Application: Applicant's Name")
    site.applicant_home_phone = clean_s("Repair Application: Applicant Home Phone")
    site.applicant_work_phone = clean_s("Repair Application: Applicant Work Phone")
    site.applicant_mobile_phone = clean_s("Repair Application: Applicant Mobile Phone")
    site.sponsor = clean_s("(Sponsor) Campaign Description")
    site.rrp_test = clean_s("Repair Application: RRP Test Results")
    site.rrp_level = clean_s("Repair Application: RRP Result Notes")
    # site.roof = clean_s("Roof?")
    site.jurisdiction = clean_s("Jurisdiction")
    site.announcement_subject = clean_s("Announcement Subject")
    site.announcement_body = clean_s("Announcement Body")
    site.put()
    logging.info('put site %s', number)

def import_captains(input_csv="../2012_ROOMS_Captain_email_sample.csv"):
  """Change input_csv to actual input file - the default is test data."""
  reader = csv.DictReader(open(input_csv))
  for s in reader:
    def clean_s(k):
      return s[k].replace('\n', ' ').replace('\xe2', "'").replace('\x80', "'").replace('\x99', '').replace('\xc3', '').replace('\x95', '').encode('ascii', 'replace')

    key = s.get('key')
    email = clean_s("Email")
    rooms_id = clean_s("ROOMS Captain ID")
    # name = "%s %s" % (clean_s("First Name"),
    #                   clean_s("Last Name"))
    name = clean_s("Name")
    captain = None
    if key:
      captain = models.Captain.get_by_id(int(key))
      if captain:
        logging.info('got captain from key %s', key)
    if not captain:
      captain = models.Captain.all().filter('rooms_id =', rooms_id).get()
      if captain:
        logging.info('got captain from rooms_id %s', rooms_id)
    if not captain:
      captain = models.Captain.all().filter('email =', email).get()
      if captain:
        logging.info('got captain from email %s', email)
    if not captain:
      logging.info('creating captain key %s name %s email %s rooms_id %s',
                   key, name, email, rooms_id)
      captain = models.Captain(name=name, email=email, rooms_id=rooms_id)

    # Over-write these values, assume volunteer database is more up to
    # date.
    captain.name = name
    captain.email = email
    captain.rooms_id = rooms_id
    # captain.phone1 = clean_s("Preferred Phone") or None
    # captain.phone_mobile = clean_s("Phone mobile")
    # captain.phone_work = clean_s("Phone work")
    # captain.phone_home = clean_s("Phone home")
    # captain.phone_fax = clean_s("Phones Fax::number")
    # captain.phone_other = clean_s("Phones Other::number")
    captain.put()

    number = s["Site ID"]
    site = models.NewSite.all().filter('number =', number).get()
    if not site:
      logging.error('site %s does not exist, skipping', number)
      continue

    # In input type is like "Volunteer Captain" but in model it's
    # "Volunteer"
    input_type = s["Captain Type"]
    for t in models.SiteCaptain.type.choices:
      if t in input_type:
        break

    query = models.SiteCaptain.all()
    query.filter('site =', site).filter('captain =', captain)
    sitecaptain = query.get()
    if sitecaptain is None:
      logging.info('Creating new SiteCaptain mapping %s to %s',
                   site.number, captain.name)
      sitecaptain = models.SiteCaptain(site=site, captain=captain, type=t)
    else:
      logging.info('Found existing SiteCaptain')
      sitecaptain.type = t
    sitecaptain.put()

ANNOUNCEMENT_BODY = """Please remember that your Home Depot card will be held until we receive your scope of work form.
Thank you for serving as a captain this year!  Please use this space to include notes/correspondence with staff.
Contact RTP staff for assistance:
Cari, 650-366-6597 x224, cari@rebuildingtogetherpeninsula.org
Adam, 650-366-6597 x223, adam@rebuildingtogetherpeninsula.org
"""
ANNOUNCEMENT_SUBJECT = """Scope of work is due March 2 for CDBG sites; all forms due March 30."""

# SAH
ANNOUNCEMENT_SUBJECT = """Thank you for your help with SAH!"""
ANNOUNCEMENT_BODY = ''

# NRD 2014
ANNOUNCEMENT_BODY = """
Thank you for serving as a captain this year!  Please use this space to include
notes/correspondence with staff.
Contact RTP staff for assistance:
Roger, 650-366-6597 x227, roger@rebuildingtogetherpeninsula.org
Adam, 650-366-6597 x223, adam@rebuildingtogetherpeninsula.org
"""
ANNOUNCEMENT_SUBJECT = """Scope of work is due March 5; all forms due March 28."""

# NRD 2015
ANNOUNCEMENT_BODY = """
Thank you for serving as a captain this year!  REMINDER: Captain's BBQ @RTP
11AM-4PM on Saturday, March 21.
(Deadline to Identify a Runner).  Please use this space to include notes or correspondence with staff.
Contact RTP staff for assistance:
Lindsay, 650-366-6597 x226, lindsay@rebuildingtogetherpeninsula.org
Adam, 650-366-6597 x223, adam@rebuildingtogetherpeninsula.org
"""
ANNOUNCEMENT_SUBJECT = """Scope of work is due March 13; all forms due March 28."""

def set_announcement():
  for s in models.NewSite.all().filter('program =', PROGRAM):
    s.announcement_subject = ANNOUNCEMENT_SUBJECT
    s.announcement_body = ANNOUNCEMENT_BODY
    s.put()


#prepare the data set

import json
import os
home = os.environ["HOME"] +'/Data/yelp/'
# home = '/home/memray/Data/yelp/'

do_checking = True
do_export = False

if do_checking: #check category list
    # f = open("".join([home, "/data/yelp/business.json"]),"r")
    f = open("".join([home, "/yelp_academic_dataset_business.json"]),"r")
    categories_dict = {}
    interested_business_id = set()
    for line in f:
        obj = json.loads(line)
        business_id = obj["business_id"]
        categories = obj["categories"]

        if categories == None:
            continue

        keep_tags = ["Restaurants", "Food"]
        should_filter = True
        for tag in keep_tags:
            if tag in categories:
                should_filter = False
        if should_filter:
            continue

        interested_business_id.add(business_id)
        for category in categories:
            if category in categories_dict:
                categories_dict[category] += 1
            else:
                categories_dict[category] = 1

    categories_items = sorted(categories_dict.items(), key=lambda i:i[1], reverse=True)
    for id, (category, freq) in enumerate(categories_items):
        print('[%d]%s:%d' % (id, category, freq))
    print('Found %d categories' % len(categories_items))
    print('Found %d businesses' % len(interested_business_id))

if do_export: #only export reviews for Restaurants, output to "output/review_restaurant.json"
    f = open("".join([home, "/yelp_academic_dataset_business.json"]),"r")
    interested_business_id = set()

    for line in f:
        obj = json.loads(line)
        business_id = obj["business_id"]
        categories = obj["categories"]
        if categories == None:
            continue
        if "Restaurants" in categories or "Food" in categories:
            interested_business_id.add(business_id)

    f = open("".join([home, "yelp_academic_dataset_review.json"]),"r")
    fu = open("".join([home, "output/raw_review_restaurant.json"]),"w")
    nline = ""
    ncount = 0
    for line in f:
        obj = json.loads(line)
        business_id = obj["business_id"]

        # if this review is about a business of interest, append it to the output
        if business_id in interested_business_id:
            nline = "".join((nline,line))
        ncount += 1
        if ncount % 10000 == 0:
            fu.write(nline)
            print(ncount)
            nline = ""
    fu.write(nline)
    fu.close()


import numpy, pandas, wave, struct
import seqlearn.hmm as seq
import matplotlib.pyplot as plt
from hmmlearn.hmm import GaussianHMM
from pylab import *
from os import listdir
from os.path import isfile, join
from copy import deepcopy

def get_raw_data(file):
    FNAME = 'set_a/' + file
    f = wave.open(FNAME)
    frames = f.readframes(-1)
    samples = struct.unpack('h'*f.getnframes(), frames)
    s = list(samples)
    framerate = f.getframerate()
    t = [float(i)/framerate for i in range(len(samples))]
    return (t, s)

def get_training_data(filename):
    data_frame = pandas.read_csv(filename)
    return data_frame

def generate_training_labels(df, s1, s2):
    xlabels = []
    ylabels = []
    current = 'Systole'
    for i in range (0, len(df)):
        xlabels.append(df[i])
        if i in s1:
            ylabels.append('S1')
            current = 'Systole'
        elif i in s2:
            ylabels.append('S2')
            current = 'Diastole'
        else:
            ylabels.append(current)
    return xlabels, ylabels

def get_set_a_data():
    mypath = 'set_a/'
    filenames = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and f[-4:] == ".wav")]
    samples = []
    times = []

    for file in filenames:
        t, s = get_raw_data(file)
        samples.append(s)
        times.append(t)

    return filenames, times, samples

def make_set_a_labels():
    training_data = get_training_data('set_a_timing.csv')
    training_fnames = list(set(training_data["fname"]))
    s1vals = []
    s2vals = []
    training_xlabels = []
    training_ylabels = []
    training_indices = []
    seq_xlabels = []
    seq_ylabels = []
    seq_lengths = []

    for fname in training_fnames:
        filtered_by_file = training_data.loc[training_data['fname'] == fname]
        s1 = list(filtered_by_file.loc[filtered_by_file['sound'] == 'S1']['location'])
        s2 = list(filtered_by_file.loc[filtered_by_file['sound'] == 'S2']['location'])
        fname_index = filenames.index(fname[6:])
        training_indices.append(fname_index)
        df = samples[fname_index]
        xlabels, ylabels = generate_training_labels(df, s1, s2)

        s1vals.append(s1)
        s2vals.append(s2)
        training_xlabels.append(xlabels)
        training_ylabels.append(ylabels)
        seq_xlabels.extend(xlabels)
        seq_ylabels.extend(ylabels)
        seq_lengths.append(len(df))

    return (s1vals, s2vals, training_xlabels, training_ylabels, training_indices,
            seq_xlabels, seq_ylabels, seq_lengths)

def plot_waves_with_labels(t,s,s1,s2):
    fig = figure()
    plt.plot(t, s)
    for i in range (0, len(s1)):
        axvline(x = t[s1[i]], color = "red")
    for i in range (0, len(s2)):
        axvline(x = t[s2[i]], color = "green")

def HMM_stuff(training_xlabels, training_ylabels):
    X = []
    Y = []
    lengths = []
    for i in range (0,len(training_xlabels)):
        x = numpy.vstack(numpy.array(training_xlabels[i]))
        X.append(x)
        y = numpy.vstack(numpy.array(training_ylabels[i]))
        Y.append(y)
        l = len(training_xlabels[i])
    lengths.append(l)

def find_indices_of(array, target):
    indices = []
    for i in range (0, len(array)):
        if array[i] == target:
            indices.append(i)
    return indices

def calculate_percent_correct(predicted, actual):
    total_correct = 0
    for i in range (0, len(predicted)):
        if predicted[i] == actual[i]:
            total_correct = total_correct + 1
    return total_correct/len(predicted)

filenames, times, samples = get_set_a_data()
s1vals, s2vals, training_xlabels, training_ylabels, training_indices, seq_xlabels, seq_ylabels, seq_lengths = make_set_a_labels()

for i in range(0,len(training_indices)):
    index = training_indices[i]
    plot_waves_with_labels(times[index], samples[index], s1vals[i], s2vals[i])

modelH = GaussianHMM(n_components=4, covariance_type="diag")
modelH.fit(numpy.vstack(numpy.array(training_xlabels[0])))
resultsH = modelH.predict(numpy.vstack(numpy.array(training_xlabels[0])))

actual_results = deepcopy(training_ylabels[0])
actual_results[actual_results == 'S1'] = 0
actual_results[actual_results == 'Systole'] = 1
actual_results[actual_results == 'S2'] = 2
actual_results[actual_results == 'Diastole'] = 3

modelS = seq.MultinomialHMM()
modelS.fit(numpy.vstack(numpy.array(seq_xlabels)), numpy.vstack(numpy.array(seq_ylabels)), numpy.array(seq_lengths))
resultsS = modelS.predict(numpy.vstack(numpy.array(seq_xlabels)), numpy.array(seq_lengths))

HMM_correct = calculate_percent_correct(resultsH, actual_results)
print("Hmmlearn was", HMM_correct, "percent correct\n")
SEQ_correct = calculate_percent_correct(resultsS, seq_ylabels)
print("Seqlearn was", SEQ_correct, "percent correct\n")


from json import load as load_json
from json import dumps as json_dumps
from ast import literal_eval
from math import inf
from os import makedirs, path
from sys import argv as cmd_args

# using floyd warshall's algorithm to get the shortest path between all nodes
def floyd_warshall(node_count, adjacency_list):
    # create a 2d array for all path distances (all distances start at infinity)
    distance_matrix = \
        [[inf for _ in range(node_count)] for _ in range(node_count)]

    # for all the edges in the graph, set the distance matrix value to 1
    # (use 1 since edges have no cost, all that matters is the number of edges)
    for x in adjacency_list:
        for y in adjacency_list[x]:
            distance_matrix[x][y] = 1

    # distance from any node to itself is 0
    for x in range(node_count):
        distance_matrix[x][x] = 0

    # now use floyd warshall
    for k in range(node_count):
        for i in range(node_count):
            for j in range(node_count):
                if distance_matrix[i][j] > (distance_matrix[i][k] + distance_matrix[k][j]):
                    distance_matrix[i][j] = distance_matrix[i][k] + \
                        distance_matrix[k][j]

    # ensure that all nodes are connected (no paths have infinite weight)
    # and set the node_data value for eccentricity and average path distance
    for node in range(node_count):
        eccentricity = max(distance_matrix[node])
        if eccentricity == inf:
            if sum(1 for i in distance_matrix[node] if i == inf) > 1:
                print(f"[{node}] is not fully connected [{distance_matrix[node]}]")
                print("FAIL! GRAPH IS NOT CONNECTED!")
        node_data[node]["eccentricity"] = eccentricity
        node_data[node]["average_path_distance"] = sum(
            distance_matrix[node]) / len(distance_matrix[node])

        # extract the diameter (max eccentricity)
    diameter = max(max(distance_matrix[node]) for node in range(node_count))
    print(f"Diameter [{diameter}]")

    # extract average path length
    average_path_length = sum(
        sum(distance_matrix[node]) / (node_count-1) for node in range(node_count))/node_count
    print(f"Average path length [{average_path_length}]")

    # show paths which have maximum length
    # counter = 0
    # for x in range(node_count):
    #     for y in range(node_count):
    #         if distance_matrix[x][y] == diameter:
    #             print(f"from [{x}] to [{y}]")
    #             counter += 1
    # print(f"# of people [{counter/2}]")

    return diameter, average_path_length

def local_clustering_cofficient(node_index, adjacency_list):
    # get the neighbour node list
    neighbours = adjacency_list[node_index]
    node_data[node_index]["neighbours"] = len(neighbours)

    # if the degree of this vertex is not > 1 then the clustering coefficient is undefined
    if not len(neighbours) > 1:
        # print(f"Local Clustering Coefficient of [{node_index}] is [-1]")
        node_data[node_index]["clustering_coefficient"] = -1
        return -1

    # calculate the number of potential edges in the node induced subgraph of the neighbours
    max_possible_neighbour_edges = (len(neighbours) * (len(neighbours) - 1))/2

    # calculate the actual number of edges between neighbours
    neighbour_edges = 0
    for neighbour in neighbours:
        for other_neighbour in neighbours:
            if other_neighbour in adjacency_list[neighbour]:
                neighbour_edges += 1
    # divide by 2 (dont count duplicates since graph is undirected)
    neighbour_edges /= 2

    # print(f"Neighbours {neighbours}")
    # print(f"Max edges between neighbours [{max_possible_neighbour_edges}]")
    # print(f"Actual between neighbours [{neighbour_edges}]")
    # print(
    #    f"Local Clustering Coefficient of [{node_index}] is [{neighbour_edges/max_possible_neighbour_edges}]")

    node_data[node_index]["clustering_coefficient"] = neighbour_edges / \
        max_possible_neighbour_edges
    return node_data[node_index]["clustering_coefficient"]

# removes the undefined (-1) coefficients from the list
def filter_undefined_coefficients(list_of_coefficients):
    for coefficient in list_of_coefficients:
        if coefficient != -1:
            yield coefficient

# calculate the clustering coefficient for the whole graph
def global_clustering_coefficient(node_count, adjacency_list):
    # calculate the clustering coefficient for each node
    local_coefficients = [local_clustering_cofficient(
        node_index, adjacency_list) for node_index in range(node_count)]

    # sum and count the total coefficients excluding those which are undefined (-1)
    # the filter function allows for a in iterator which yields values,
    # meaning that both operations can be done in one pass or memory instead of two
    total_coefficient_value = 0
    valid_coefficient_count = 0
    for valid_coefficient in filter_undefined_coefficients(local_coefficients):
        valid_coefficient_count += 1
        total_coefficient_value += valid_coefficient

    # calculate and return global clustering coefficient
    # print(
    #     f"Total Coefficient [{total_coefficient_value}]")
    # print(
    #     f"Number of valid coefficients [{valid_coefficient_count}]")
    print(
        f"Global clustering coefficient [{total_coefficient_value / valid_coefficient_count}]")
    return total_coefficient_value / valid_coefficient_count

# creates an adjacancy list for the given graph
def construct_adjacency_list(edges, node_count):
    # make an adjacency matrix of the correct size
    # (using dictionaries for constant lookup time)
    adjacency_list = {i: set() for i in range(node_count)}
    for edge in edges:
        adjacency_list[edge[0]].add(edge[1])
        adjacency_list[edge[1]].add(edge[0])

    return adjacency_list

# put relevant node data from the graph json into node_data
def get_node_data_from_graph(graph):
    for node in graph["users"]:
        node_data[node["id"]]["name"] = node["name"]
        node_data[node["id"]]["weight"] = node["contact"]

if __name__ == "__main__":
    # read the metadata json file
    with open(f"{cmd_args[1]}/graph.json") as json_file:
        graph = load_json(json_file)

    # perform conversions to edges = { tuple(id, id) : weight } and get node_count
    edges = {(edge["source"],  edge["target"]): edge["weight"]
             for edge in graph["edges"]}
    node_count = len(graph["users"])

    # graph size
    graph_stats = {}
    graph_stats["node_count"] = node_count
    graph_stats["edge_count"] = len(edges)
    print(f"Nodes  [{node_count}]")
    print(f"Edges  [{len(edges)}]")

    # create node_data dictionary and initialise the container for each node
    global node_data
    node_data = {index: {} for index in range(node_count)}

    # extract useful node data from graph
    get_node_data_from_graph(graph)

    # create adjacency list and release memory which is no longer required
    adjacency_list = construct_adjacency_list(edges, node_count)
    del edges
    del graph

    # get the graph facts
    graph_stats["diameter"], graph_stats["average_path_length"] = \
        floyd_warshall(node_count, adjacency_list)
    graph_stats["global_clustering_coefficient"] = \
        global_clustering_coefficient(node_count, adjacency_list)

    # output graph stuff to file
    with open(f"{cmd_args[1]}/graph_stats.json", "w") as outputFile:
        print(json_dumps(graph_stats), file=outputFile)

    # create the output dir if it does not exist
    if not path.exists(f"{cmd_args[1]}/node_data/"):
        try:
            makedirs(path.dirname(f"{cmd_args[1]}/node_data/"))
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

    # output the node data to files
    for node_id in node_data:
        with open(f"{cmd_args[1]}/node_data/{node_id}.json", "w") as outputFile:
            print(json_dumps(node_data[node_id]), file=outputFile)


#i have tweaked with this file a little so you may want to go through the code and see line 140 in particular
#if you want to run the code then change the other file too i have attached my other file too so you may know what to change
############## Task1.1 - ArUco Detection ##############

import numpy as np
import cv2
import cv2.aruco as aruco
import sys
import math
import time

def detect_ArUco(img):
	## function to detect ArUco markers in the image using ArUco library
	## argument: img is the test image
	## return: dictionary named Detected_ArUco_markers of the format {ArUco_id_no : corners}, where ArUco_id_no indicates ArUco id and corners indicates the four corner position of the aruco(numpy array)
	## 		   for instance, if there is an ArUco(0) in some orientation then, ArUco_list can be like
	## 				{0: array([[315, 163],
	#							[319, 263],
	#							[219, 267],
	#							[215,167]], dtype=float32)}

    Detected_ArUco_markers = {}

    ## enter your code here ##
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_5X5_250)
    parameters = aruco.DetectorParameters_create()

    corners , ids , _ = aruco.detectMarkers(gray,aruco_dict,parameters=parameters)

    for i in range(len(ids)):
    	Detected_ArUco_markers[str(ids[i])] = corners[i]

    return Detected_ArUco_markers

def Calculate_orientation_in_degree(Detected_ArUco_markers):
	## function to calculate orientation of ArUco with respective to the scale mentioned in Problem_Statement.pdf
	## argument: Detected_ArUco_markers  is the dictionary returned by the function detect_ArUco(img)
	## return : Dictionary named ArUco_marker_angles in which keys are ArUco ids and the values are angles (angles have to be calculated as mentioned in the ProblemStatement.pdf)
	##			for instance, if there are two ArUco markers with id 1 and 2 with angles 120 and 164 respectively, the
	##			function should return: {1: 120 , 2: 164}

	ArUco_marker_angles = {}
	## enter your code here ##
	for ids in Detected_ArUco_markers:
		for corner in Detected_ArUco_markers[ids]:
			topListx = []
			topListy = []
			for i in range(4):
				xcenter = int((corner[0][0]+corner[2][0])/2)							#determining x coordinate of center by midpoint theorem
				ycenter = int((corner[0][1]+corner[2][1])/2)							#determining y coordinate of center by midpoint theorem

				topListx.append(corner[i][0])					#making a list of corners x axis
				topListy.append(corner[i][1])
				#print topList
				#print "works"

			xtop1 = max(topListx)								#top right
			ytop2 = min(topListy)								#top left

			for i in range(4):
				if corner[i][0] == xtop1:
					ytop1 = corner[i][1]

				if corner[i][1] == ytop2:
					xtop2 = corner[i][0]

			lcenterx = int((xtop2+xtop1)/2)											#determining y coordinate of an edge to draw line
			lcentery = int((ytop2+ytop1)/2)											#determining x coordinate of an edge to draw line

			slope = -1*( xcenter - lcenterx )/float(( ycenter - lcentery ))
			angle = 90 - int(57.3*math.atan(slope))
			ArUco_marker_angles[ids] = angle

	return ArUco_marker_angles	## returning the angles of the ArUco markers in degrees as a dictionary

def mark_ArUco(img,Detected_ArUco_markers,ArUco_marker_angles):
	## function to mark ArUco in the test image as per the instructions given in problem_statement.pdf
	## arguments: img is the test image
	##			  Detected_ArUco_markers is the dictionary returned by function detect_ArUco(img)
	##			  ArUco_marker_angles is the return value of Calculate_orientation_in_degree(Detected_ArUco_markers)
	## return: image namely img after marking the aruco as per the instruction given in Problem_statement.pdf

    ## enter your code here ##

    #print Detected_ArUco_markers

    for ids in Detected_ArUco_markers:
    	#print ids
    	for corner in Detected_ArUco_markers[ids]:

    		topListx = []
    		topListy = []

    		for i in range(4):
    			cv2.circle(img,(corner[i][0],corner[i][1]),7,(10*i,50*i,30*i),-1)

    			xcenter = int((corner[0][0]+corner[2][0])/2)
    			ycenter = int((corner[0][1]+corner[2][1])/2)
    			cv2.circle(img,(xcenter,ycenter),7,(0,0,255),-1)

    			topListx.append(corner[i][0])
    			topListy.append(corner[i][1])

    		xtop1 = max(topListx)
    		ytop2 = min(topListy)

    		for i in range(4):
    			if corner[i][0] == xtop1:
    				ytop1 = corner[i][1]

    			if corner[i][1] == ytop2:
    				xtop2 = corner[i][0]

    		lcenterx = int( ( xtop2 + xtop1 ) / 2 )
    		lcentery = int( ( ytop2 + ytop1 ) / 2 )

    		cv2.circle(img,(lcenterx,lcentery),7,(100,100,100),-1)
    		cv2.line(img,(xcenter,ycenter),(lcenterx,lcentery),(255,0,0),5)

    		#cv2.circle(img,(corner[2][0],corner[2][1]),30,(0,0,255),4)
	    	x = int(corner[2][0]-15)
	    	y = int(corner[2][1]+15)
	    	k = ids
	    	ids = str(ids)
	    	ids = ids[1]
	    	cv2.putText(img,ids,(x,y),cv2.FONT_HERSHEY_COMPLEX,1.5,(0,0,255),2)

    		#cv2.circle(img,(corner[0][0],corner[0][1]),30,(0,255,0),4)
	    	x = int(corner[0][0]-25)
	    	y = int(corner[0][1]+15)
	    	angle = str(ArUco_marker_angles[k])
	    	#ids = ids[1]
	    	cv2.putText(img,angle,(x,y),cv2.FONT_HERSHEY_COMPLEX,1.3,(0,255,0),2)

	#return img


# coding: utf-8

import numpy as np
import pylab as plt
from scipy.integrate import quad
from scipy.integrate import odeint

def calc_derivative(ypos, time, counter_arr):
    counter_arr += 1
    return -2 * ypos

res, err = quad(np.sin, 0, np.pi / 2)

counter = np.zeros((1, ), dtype=np.uint16)
time_vec = np.linspace(0, 4, 40)
yvec, info = odeint(calc_derivative, 1, time_vec, args=(counter,), full_output=True)
plt.plot(time_vec, yvec)
plt.xlabel('Time [s]')
plt.ylabel('y position [m]')
plt.show()

def calc_deri(yvec, time, nu, om):
    return (yvec[1], -nu * yvec[1] - om * yvec[0])

mass = 0.5
kspring = 4
cviscous = 0.4
eps = cviscous / (2 * mass * np.sqrt(kspring / mass))
nu_coef = cviscous / mass
om_coef = kspring / mass

time_vec = np.linspace(0, 10, 100)
yinit = (1, 0)
yarr = odeint(calc_deri, yinit, time_vec, args=(nu_coef, om_coef))
plt.plot(time_vec, yarr[:, 0], label='y')
plt.plot(time_vec, yarr[:, 1], label="y'")
plt.legend()
plt.show()


# import packages: numpy, math (you might need pi for gaussian functions)
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy.signal import convolve2d as conv2

"""
Gaussian function taking as argument the standard deviation sigma
The filter should be defined for all integer values x in the range [-3sigma,3sigma]
The function should return the Gaussian values Gx computed at the indexes x
"""
def gauss(sigma):
    sigma = int(sigma)
    x = np.arange(-3 * sigma, 3 * sigma + 1)
    Gx = [1 / (math.sqrt(2 * math.pi) * sigma) * math.exp(- ((i**2) / (2*(sigma**2)))) for i in x]
    Gx = np.array(Gx)

    return Gx, x

"""
Implement a 2D Gaussian filter, leveraging the previous gauss.
Implement the filter from scratch or leverage the convolve2D method (scipy.signal)
Leverage the separability of Gaussian filtering
Input: image, sigma (standard deviation)
Output: smoothed image
"""
# def gaussianfilter_slow(img, sigma):
#     """Slower version of gaussianfilter"""
#     import time
#     start = time.time()
#     Gx, _ = gauss(sigma)
#     kernel = conv2(Gx, Gx.T)
#     smooth_img = conv2(img, kernel, "same")
#     print("Ho impiegato", time.time() - start, "sec")

#     return smooth_img

def gaussianfilter(img, sigma):
    # import time
    # start = time.time()
    Gx, _ = gauss(sigma)
    Gx = Gx.reshape((1, -1))
    smooth_img = conv2(img, Gx, "same")
    smooth_img = conv2(smooth_img, Gx.T, "same")
    # print("Ho impiegato", time.time() - start, "sec")

    return smooth_img

"""
Gaussian derivative function taking as argument the standard deviation sigma
The filter should be defined for all integer values x in the range [-3sigma,3sigma]
The function should return the Gaussian derivative values Dx computed at the indexes x
"""
def gaussdx(sigma):
    sigma = int(sigma)
    x = np.arange(-3 * sigma, 3 * sigma + 1)
    Dx = [- (1 / (math.sqrt(2 * math.pi) * (sigma**3))) * i * math.exp(- ((i**2) / (2*(sigma**2)))) for i in x]

    Dx = np.array(Dx)

    return Dx, x

def gaussderiv(img, sigma):
    Dx, _ = gaussdx(sigma)
    kernel = Dx.reshape((1, Dx.shape[0]))

    imgDx = conv2(img, kernel, "same")
    imgDy = conv2(img, kernel.T, "same")

    return imgDx, imgDy


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.7-x86_64/egg/airflow/contrib/operators/adls_list_operator.py
# Compiled at: 2019-09-11 03:47:34
# Size of source mod 2**32: 2598 bytes
from typing import Iterable
from airflow.contrib.hooks.azure_data_lake_hook import AzureDataLakeHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults

class AzureDataLakeStorageListOperator(BaseOperator):
    __doc__ = "\n    List all files from the specified path\n\n    This operator returns a python list with the names of files which can be used by\n     `xcom` in the downstream tasks.\n\n    :param path: The Azure Data Lake path to find the objects. Supports glob\n        strings (templated)\n    :type path: str\n    :param azure_data_lake_conn_id: The connection ID to use when\n        connecting to Azure Data Lake Storage.\n    :type azure_data_lake_conn_id: str\n\n    **Example**:\n        The following Operator would list all the Parquet files from ``folder/output/``\n        folder in the specified ADLS account ::\n\n            adls_files = AzureDataLakeStorageListOperator(\n                task_id='adls_files',\n                path='folder/output/*.parquet',\n                azure_data_lake_conn_id='azure_data_lake_default'\n            )\n    "
    template_fields = ('path', )
    ui_color = '#901dd2'

    @apply_defaults
    def __init__(self, path, azure_data_lake_conn_id='azure_data_lake_default', *args, **kwargs):
        (super(AzureDataLakeStorageListOperator, self).__init__)(*args, **kwargs)
        self.path = path
        self.azure_data_lake_conn_id = azure_data_lake_conn_id

    def execute(self, context):
        hook = AzureDataLakeHook(azure_data_lake_conn_id=(self.azure_data_lake_conn_id))
        self.log.info('Getting list of ADLS files in path: %s', self.path)
        return hook.list(path=(self.path))


import random

import requests

class QuizBrain:
    questions = list()
    score = 0

    def __init__(self, amount, question_factory):
        self.amount = amount
        self.question_factory = question_factory

    def play(self):
        self._init_questions()
        print("Get ready to play!")
        print()
        self._loop()
        print("========= FINAL =========")
        print(f"Your final score: {self.score}")
        self._reset()

    def _init_questions(self):
        response = requests.get(f"https://opentdb.com/api.php?amount={self.amount}")
        response.raise_for_status()
        response_json = response.json()
        questions = response_json["results"]
        for question in questions:
            self.questions.append(self.question_factory.create_from_json(question))

    def _loop(self):
        remained_questions = self.questions
        while len(remained_questions) > 0:
            next_question_index = random.randint(0, len(remained_questions) - 1)
            next_question = remained_questions[next_question_index]
            remained_questions.pop(next_question_index)
            print("========= NEXT QUESTION =========")
            print(next_question.description)
            answer = input("Answer: ").lower()
            if next_question.check_answer(answer):
                print("Correct!")
                self.score += 1
            else:
                print("Wrong :(")
            print(f"Your new score: {self.score}")
            print(f"Remained questions: {len(remained_questions)}")
            print()

    def _reset(self):
        self.questions = list()
        self.score = 0


# standard library
from pathlib import Path
from tempfile import TemporaryDirectory

# third-party packages
import numpy as np
import pandas as pd
import xarray as xr
from mao_merge_45m.weather import convert

# constants
CSV_COLS = "time", "wind_speed", "wind_direction"
TEST_CSV = Path("data") / "weather_20201123T004000Z.csv"
JST_HOURS = np.timedelta64(9, "h")

# test functions
def test_convert() -> None:
    """Test whether a CSV file is correctly parsed."""
    with TemporaryDirectory() as zarr:
        path_zarr = Path(zarr)
        convert(TEST_CSV, path_zarr, overwrite=True)

        ds = xr.open_zarr(path_zarr)
        ds = ds.assign_coords(time=ds.time + JST_HOURS)

        df = pd.read_csv(
            TEST_CSV,
            names=CSV_COLS,
            index_col=0,
            parse_dates=True,
        )

        assert (ds.wind_speed.to_series() == df[CSV_COLS[1]]).all()
        assert (ds.wind_direction.to_series() == df[CSV_COLS[2]]).all()


from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import orm
from sqlalchemy.sql import functions as sql_func

from airflow import configuration

class Connection:

    def __init__(self, db_url=None):
        if db_url is None:
            db_url = configuration.get('mri', 'SQL_ALCHEMY_CONN')

        self.Base = automap_base()
        self.engine = create_engine(db_url)
        self.Base.prepare(self.engine, reflect=True)

        self.ObservationFact = self.Base.classes.observation_fact
        self.PatientDimension = self.Base.classes.patient_dimension
        self.VisitDimension = self.Base.classes.visit_dimension
        self.ConceptDimension = self.Base.classes.concept_dimension
        self.ProviderDimension = self.Base.classes.provider_dimension
        self.ModifierDimension = self.Base.classes.modifier_dimension
        self.CodeLookup = self.Base.classes.code_lookup
        self.PatientMapping = self.Base.classes.patient_mapping
        self.EncounterMapping = self.Base.classes.encounter_mapping

        self.db_session = orm.Session(self.engine)

    def close(self):
        self.db_session.close()

    def new_patient_num(self):
        try:
            return self.db_session.query(sql_func.max(self.PatientMapping.patient_num).label('max')).one().max + 1
        except TypeError:
            return 0

    def new_encounter_num(self):
        try:
            return self.db_session.query(sql_func.max(self.EncounterMapping.encounter_num).label('max')).one().max + 1
        except TypeError:
            return 0