# Copyright 2018 Dong-Hyun Lee, Kakao Brain.

""" Training Config & Helper Classes  """

import os
from typing import NamedTuple
from tqdm import tqdm

import torch
import torch.nn as nn

import checkpoint

class Config(NamedTuple):
    """ Hyperparameters for optimization """
    seed: int = 3431 # random seed
    batch_size: int = 32
    lr: int = 5e-5 # learning rate
    n_epochs: int = 10 # the number of epoch
    # `warm up` period = warmup(0.1)*total_steps
    # linearly increasing learning rate from zero to the specified value(5e-5)
    warmup: float = 0.1
    save_steps: int = 100 # interval for saving model
    total_steps: int = 100000 # total number of steps to train
    data_parallel: bool = False
    comments: str = "" # for comments in json file

class TrainLoop(object):
    """Training Helper Class"""
    def __init__(self, cfg, model, data_iter, optimizer, save_dir, device):
        self.cfg = cfg # config for training : see class Config
        self.model = model
        self.data_iter = data_iter # iterator to load data
        self.optimizer = optimizer
        self.save_dir = save_dir
        self.device = device # device name

    def train(self, get_loss, model_file=None, pretrain_file=None, data_parallel=True):
        """ Train Loop """
        self.model.train() # train mode
        self.load(model_file, pretrain_file)
        model = self.model.to(self.device)
        if data_parallel: # use Data Parallelism with Multi-GPU
            model = nn.DataParallel(model)

        global_step = 0 # global iteration steps regardless of epochs
        for e in range(self.cfg.n_epochs):
            loss_sum = 0. # the sum of iteration losses to get average loss in every epoch
            iter_bar = tqdm(self.data_iter, desc='Iter (loss=X.XXX)')
            for i, batch in enumerate(iter_bar):
                batch = [t.to(self.device) for t in batch]

                self.optimizer.zero_grad()
                loss = get_loss(model, batch, global_step).mean() # mean() for Data Parallelism
                loss.backward()
                self.optimizer.step()

                global_step += 1
                loss_sum += loss.item()
                iter_bar.set_description('Iter (loss=%5.3f)'%loss.item())

                if global_step % self.cfg.save_steps == 0: # save
                    self.save(global_step)

                if self.cfg.total_steps and self.cfg.total_steps < global_step:
                    print(f"Epoch {e+1}/{self.cfg.n_epochs} : Average Loss {loss_sum/(i+1)}")
                    print('The Total Steps have been reached.')
                    self.save(final=True) # save and finish when global_steps reach total_steps
                    return

            print(f"Epoch {e+1}/{self.cfg.n_epochs} : Average Loss {loss_sum/(i+1)}")
        self.save(final=True)

    def eval(self, evaluate, model_file):
        """ Evaluation Loop """
        self.model.eval() # evaluation mode
        self.load(model_file, None)
        model = self.model.to(self.device)
        if self.cfg.data_parallel: # use Data Parallelism with Multi-GPU
            model = nn.DataParallel(model)

        results = [] # prediction results
        iter_bar = tqdm(self.data_iter, desc='Iter (loss=X.XXX)')
        for batch in iter_bar:
            batch = [t.to(self.device) for t in batch]
            with torch.no_grad(): # evaluation without gradient calculation
                accuracy, result = evaluate(model, batch) # accuracy to print
            results.append(result)

            iter_bar.set_description('Iter(acc=%5.3f)'%accuracy)
        return results

    def load(self, model_file, pretrain_file):
        """ load saved model or pretrained transformer (a part of model) """
        if model_file:
            print('Loading the model from', model_file)
            self.model.load_state_dict(torch.load(model_file))

        elif pretrain_file: # use pretrained transformer
            print('Loading the pretrained model from', pretrain_file)
            if pretrain_file.endswith('.ckpt'): # checkpoint file in tensorflow
                checkpoint.load_model(self.model.transformer, pretrain_file)
            elif pretrain_file.endswith('.pt'): # pretrain model file in pytorch
                self.model.transformer.load_state_dict(
                    {key[12:]: value
                        for key, value in torch.load(pretrain_file).items()
                        if key.startswith('transformer')}
                ) # load only transformer parts

    def save(self, i=0, final=False):
        """ save current model """
        file = "model_final.pt" if final else f"model_steps_{i}.pt"
        # save model object before nn.DataParallel
        torch.save(self.model.state_dict(), os.path.join(self.save_dir, file))

if __name__ == '__main__':
    pass


import pickle
import datetime
import pandas.io.data as web
import sys
import pandas as pd
sys.path.append('../prod/')
from Portfolio import Portfolio, Position

def daily_data_update():
    df = pd.DataFrame.from_csv("returns_data.csv")
    tickers = df.columns
    weekno = datetime.datetime.today().weekday()
    if weekno == 6:
        date = datetime.date.today() - datetime.timedelta(2)
    elif weekno == 5:
        date = datetime.date.today() - datetime.timedelta(1)
    else:
        date = datetime.date.today()
    today_row = web.DataReader(tickers,'yahoo',date - datetime.timedelta(days=1),date)['Close']
    today_row = today_row.pct_change()[1:len(today_row)]
    df = df.append(today_row)
    df.to_csv("returns_data.csv")
    return df

def start_daily_update_process():
    latest_saved_portfolio = pull_latest_portfolio()
    df = daily_data_update()
    latest_portfolio = daily_portfolio_update(latest_saved_portfolio,df)
    update_last_portfolio(latest_portfolio)

def daily_portfolio_update(latest_saved_portfolio,return_grid):
    df = return_grid
    latest_portfolio = latest_saved_portfolio.uncompile_and_update(df)
    return latest_portfolio

def pull_latest_portfolio():
    olds = pull_old_portfolios()
    return olds[len(olds)-1]

def update_last_portfolio(latest_portfolio):
    all_portfolios = pull_old_portfolios()
    all_portfolios[len(all_portfolios)-1] = latest_portfolio.compile_portfolio()
    with open('portfolio.txt', 'wb') as f:
        pickle.dump(all_portfolios, f)

    #use this to plot the historical performance of the fund, for whatever asset allocation
def pull_old_portfolios():
    with open('portfolio.txt','rb') as f:
        var = pickle.load(f)
        return var

#Run everyday after market closes
start_daily_update_process()


import scrapy
class tpbSpider(scrapy.Spider):
	name = "TPB"

	start_urls = [
		'http://www.info.gov.hk/tpb/en/plan_application/application_comment_all.html'
	]

	def parse(self,response):
		for tr in response.css('tr'):
			yield{
			"application number" : tr.css("a[href^=java]::text").extract_first(),
			"date of meeting" : tr.css("td>div::text").re(r"^\d\d/\d\d/\d\d\d\d.*")
			}


__author__ = 'Titi'

import videodataset
from model.model_factory import Model
from utils.tools import init_meters
from utils.config import process_command

import numpy as np
import matplotlib.pyplot as plt
import argparse
import math
import random
import os
import cv2
from tqdm import tqdm

import torch
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

def main():
    args = process_command()
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

    # Loading LSTM model
    print("Loading LSTM model...", end='')
    LSTM = Model(args, device)
    LSTM.network.eval()
    print("...OK")

    # get video list from video_list_paths
    with open(args.valid_data_paths, 'r') as f:
        valid_video_list = [line.strip() for line in f.readlines()]

    # Loading model parameter
    if os.path.isfile(args.checkpoint_path):
        checkpoint = torch.load(args.checkpoint_path)
        model_state_dict = checkpoint['model_state_dict']
        optimizer_state_dict = checkpoint['optimizer_state_dict']
        pretrained_epoch = checkpoint['epoch']
        mask_probability = checkpoint['mask_probability']
        # model loading weight
        LSTM.load_checkpoint(model_state_dict, optimizer_state_dict)
    else:
        raise "[Error] No this checkpoint file"

    # Create validation dataset
    valid_dataset = videodataset.VideoDataset(valid_video_list, args.dataset_name,
                                              seq_length=args.seq_length,
                                              img_size=args.img_size,
                                              img_channel=args.img_channel,
                                              mode="valid",
                                              mask_probability=0)
    valid_loader = DataLoader(dataset=valid_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers)

    # validation
    print("Testing...")
    psnrs, ssims = [], []
    for _ in range(args.seq_length):
        init_psnr, init_ssim = init_meters()
        psnrs.append(init_psnr)
        ssims.append(init_ssim)

    with torch.no_grad():
        for vid_path, seq, seq_gt in tqdm(valid_loader):
            psnrs, ssims = LSTM.test(vid_path, args.gen_frm_dir, seq, seq_gt, 0, psnrs, ssims)

    for t in range(args.seq_length):
        print("{}\tPSNR: {:0.3f}, SSIM: {:0.3f}".format(t+1, psnrs[t].avg, ssims[t].avg))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.auth
from tornado.web import HTTPError

from tornado.escape import json_encode as dumps
from tornado.escape import json_decode as loads

import db
import settings

# Major TODO: Handle exceptions for all SQL operations

class MainHandler(tornado.web.RequestHandler):
    """Main Handler... list all databases"""

    def get(self):
        self.write(dumps(db.list_databases()))

class ListTableHandler(tornado.web.RequestHandler):
    """List tables in specified database"""

    def get(self,database):
        try:
            self.write(dumps(db.list_tables(database)))
        except db.NoSuchDatabase:
            raise HTTPError(404)

class DataHandler(tornado.web.RequestHandler):
    def get(self,database,table,rowid=None):
        """Dumps a single record or all records from a table"""
        try:
            if rowid:
                self.write(dumps(db.get_record(database,table,rowid)))
            else:
                self.write(dumps([row
                    for row in db.all_records(database,table)]))
        except db.NoSuchDatabase:
            raise HTTPError(404)

    def post(self,database,table,rowid=None):
        """INSERT or UPDATE records"""
        # Prepare request (POST) vars. We only have to do this because
        # we get a dict of lists and our funcs want a dict of single
        # values
        kwargs = {}
        for k,v in self.request.arguments.iteritems():
            kwargs[k] = v[0]

        try:
            if rowid:
                # Perform UPDATE
                json = dumps(db.update_record(database,table,rowid,**kwargs))
                self.write(json)

            else:
                # Perform INSERT
                id = db.insert_record(database,table,**kwargs)
                self.set_status = 201   # HTTP 201 Created
                loc = '/%s/%s/%d' % (database,table,id)
                self.set_header('Location', loc)
                self.write(loc)

        except db.NoSuchDatabase:
            raise HTTPError(404)

    def put(self,database,table,rowid=None):
        """REPLACE record"""

        if not rowid:
            raise HTTPError(405) # We need a rowid to use REPLACE

        obj = loads(self.request.body)
        try:
            db.replace_record(database,table,rowid,obj)
        except db.NoSuchDatabase:
            raise HTTPError(404)

    def delete(self,database,table,rowid=None):
        """DELETE record"""

        if not rowid:
            raise HTTPError(405) # Need ROWID

        try:
            db.delete_record(database,table,rowid)
        except db.NoSuchDatabase:
            raise HTTPError(404)

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/([\w_\-\.]+)/", ListTableHandler),
    (r"/([\w_\-\.]+)/([\w]+)/([\d]+)?", DataHandler),
],
    cookie_secret=settings.cookie_secret,
)

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(settings.port)
    tornado.ioloop.IOLoop.instance().start()


#!/usr/bin/python
# -*- coding: UTF-8 -*-
# -- coding: UTF-8
#coding=utf-8
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Routine for decoding the CIFAR-10 binary file format."""
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# -- coding: UTF-8
#coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

#from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
import numpy as np
from PIL import Image
import random
import re
import csv

# Process images of this size. Note that this differs from the original CIFAR
# image size of 32 x 32. If one alters this number, then the entire model
# architecture will change and any model would need to be retrained.
IMAGE_SIZE = 128
# Global constants describing the CIFAR-10 data set.
EYE_H = 40; EYE_W = 40;
NOSE_H = 32; NOSE_W = 40;
MOUTH_H = 32; MOUTH_W = 48;
re_pose = re.compile('_\d{3}_')
re_poseIllum = re.compile('_\d{3}_\d{2}_')

class MultiPIE():
    """Reads and parses examples from MultiPIE data filelist
    """
    def __init__(self, datasplit='train', Random=True, LOAD_60_LABEL=False, MIRROR_TO_ONE_SIDE=True, RANDOM_VERIFY=False,
                 GENERATE_MASK=False, testing = True):

        if not testing:
            self.dir = './data1/MultiPIE/totally'
        else:
            self.dir = './data1/MultiPIE/totally_01'

        self.csvpath  = './data1/MultiPIE/train.csv'     #train_15~90°
        self.test_dir = './data1/MultiPIE/test_01.csv'   #test_01_15~90°

        self.split = datasplit
        self.random = Random
        self.seed = None
        self.LOAD_60_LABEL = LOAD_60_LABEL
        self.MIRROR_TO_ONE_SIDE = MIRROR_TO_ONE_SIDE
        self.RANDOM_VERIFY = RANDOM_VERIFY
        self.GENERATE_MASK = GENERATE_MASK
        self.cameraPositions = {'24_0': (+90, '10'),'01_0' : (+75, '08'), '20_0' : (+60, '08'), '19_0' : (+45, '09'), '04_1' : (+30, '07'), '05_0' : (+15, '06'), #left 右脸
                    '05_1' : (0,'06'), #center
                    '14_0' : (-15,'06'), '13_0' : (-30, '05'), '08_0' : (-45, '15'),'09_0' : (-60, '15'),'12_0' : (-75, '15'),'11_0' : (-90, '15')} #right 左脸

        # if not testing: #训练阶段
        self.indices = open(self.csvpath, 'r').read().splitlines() #打开train.csv
        self.size = len(self.indices) #40*4=160

        # else:
        self.indices_test = open(self.test_dir, 'r').read().splitlines() #打开test.csv
        self.test_size = len(self.indices_test) #20*4=80

        self.idx = 0

    def test_batch(self, test_batch_size,time_size,idxx,jdxx):  #test_batch_size=100
        test_batch_size = min(test_batch_size, len(self.indices_test)) #min(150 ,80)
        images = np.empty([test_batch_size, IMAGE_SIZE, IMAGE_SIZE, 3]) #(80,128,128,3)
        # pose = np.empty([test_batch_size],dtype=np.int32)
        filenames = list()
        poses = list()

        if idxx==0 and jdxx==0:
            for i in range(len(self.indices_test)//12):
                a1 = self.indices_test[i*12 : (i+1)*12]
                random.shuffle(a1)
                self.indices_test[i*12 : (i+1)*12] = a1

        for i in range(test_batch_size):
            idx = idxx * (time_size * test_batch_size) + (jdxx * test_batch_size + i)
            # idx = idxx * (time_size * 6) + (jdxx * 6 + i)
            # idx = jdxx * test_batch_size + i
            images[i, ...] = self.load_image(self.indices_test[idx])
            filename = self.indices_test[idx]

            filenames.append(filename)
            pose = abs(self.findPose(filename))
            poses.append(pose)
        return images, filenames, None, None, poses, None

    def next_image_and_label_mask_batch(self, batch_size,time_size,idxx,jdxx):
        """Construct a batch of images and labels masks.
        Args:
        batch_size: Number of images per batch.
        shuffle: boolean indicating whether to use a shuffling queue.
        Returns:
        ndarray feed.
        images: Images. 4D of [batch_size, height, width, 6] size.
        labels: Labels. 4D of [batch_size, height, width, 3] size.
        masks: masks. 4D of [batch_size, height, width, 3] size.
        verifyImages: Images. 4D of [batch_size, height, width, 3] size.
        verifyLabels: 1D of [batch_size] 0 / 1 classification label
        """
        assert batch_size >= 1
        filenames = list()
        labelnames = list()
        poses = list()
        images = np.empty([batch_size, IMAGE_SIZE, IMAGE_SIZE, 3]) #[10, 128, 128, 3]
        labels = np.empty([batch_size, IMAGE_SIZE, IMAGE_SIZE, 3]) #[10, 128, 128, 3]
        # pose = np.empty([batch_size],dtype=np.int32)
        idenlabels = np.empty([batch_size],dtype=np.int32)
        if idxx==0 and jdxx==0:
            for i in range(len(self.indices)//12):
                a1 = self.indices[i*12 : (i+1)*12]
                random.shuffle(a1)
                self.indices[i*12: (i+1)*12] = a1

        for i in range(batch_size):
            idx = idxx * (time_size * batch_size) + (jdxx * batch_size + i)
            images[i, ...] = self.load_image(self.indices[idx])

            filename = self.indices[idx]
            filenames.append(filename)

            labels[i, ...], labelname = self.load_label_mask(filename)

            # labels[i, ...],labelname = self.load_label_mask(filename)
            labelnames.append(labelname)
            pose = abs(self.findPose(filename))
            poses.append(pose)
            identity = int(filename[0:3])
            idenlabels[i] = identity
        return images, labels, poses, idenlabels,filenames,labelnames, None

    def updateidx(self):
        if self.random:
            self.idx = random.randint(0, len(self.indices)-1)
        else:
            self.idx += 1
            if self.idx == len(self.indices):
                self.idx = 0
    def load_image(self, filename):
            """
            Load input image & codemap and preprocess:
            - cast to float
            - subtract mean divide stdadv
            - concatenate together
            """
            im = Image.open(self.dir + os.sep + filename)
            in_ = np.array(im, dtype=np.float32) # (128,128,3)
            in_ /= 256
            return in_
    def load_image2(self, filename):
            im = Image.open(self.dir + os.sep + filename)
            in_ = np.array(im, dtype=np.float32) # (128,128,3)
            in_ /= 256
            return in_, filename

    def load_label_mask(self, filename, labelnum=-1):
        _, labelname = self.findSameIllumCodeLabelpath(filename) # input:207_02_01_090_15_cropped.png output:207_02_01_051_15_code.png, 207_02_01_051_15_cropped.png
        im = Image.open(self.dir + os.sep + labelname)
        label = np.array(im, dtype=np.float32) #(128,128,3)
        label /= 256
        return label,labelname

    def findBestIllumCodeImagepath(self, fullpath):
        span = re_pose.search(fullpath).span()
        camPos = list(fullpath[span[0]+1:span[1]-1])
        camPos.insert(2,'_')
        camPos = ''.join(camPos)
        #get 01_0 like string
        bestIllum = self.cameraPositions[camPos][1]

        labelpath = list(fullpath)
        labelpath[span[1]:span[1]+2] = bestIllum[:]
        labelpath = ''.join(labelpath)
        codepath = str(labelpath).replace('cropped', 'code')
        return (codepath, labelpath)

    def findSameIllumCodeLabelpath(self, fullpath):
        span = re_poseIllum.search(fullpath).span() #re_poseIllum = re.compile('_\d{3}_\d{2}_')
        tempath = list(fullpath)
        tempath[span[0]+1:span[0]+7] = '051_06'
        labelpath = ''.join(tempath)
        codepath = str(labelpath).replace('cropped', 'code')
        return (codepath, labelpath)

    def findPose(self, fullpath):
        span = re_pose.search(fullpath).span()  #re_pose = re.compile('_\d{3}_') #\d 表示[0-9] span() 返回匹配的区间[9,14] 左闭右开
        camPos = list(fullpath[span[0]+1:span[1]-1]) #'201_01_01_010_08_cropped.png'
        camPos.insert(2,'_') #['0', '1', '_', '0']
        camPos = ''.join(camPos) #'01_0'
        #get 01_0 like string
        return self.cameraPositions[camPos][0] #+75


import numpy as np
import matplotlib.pyplot as plt
import pickle
import time

class Spline:
	"""
	Initiate a class variable spline that has one break at x = x_break starting at x_initial and has
	the equation y = a + b*(x-x_o) + c*(x-x_o)**2 + d*(x-x_o)**3.

	pp_func(X)
	~~~~~~~~~~~~~~~~~~~

	Takes in X array and outputs the piecewise polynomial associated with this spline.

	pp_deriv()
	~~~~~~~~~~~~~~~~~~~

	Takes in X array and outputs the piecewise polynomial associated with the spline's derivative.

	find_max_and_min()
	~~~~~~~~~~~~~~~~~~~

	Takes in the min and max values for both x and y and will find the maximum y values of the piecewise
	polynomial. To do this, first we find the extrema point (find_extrema) by inputing the x values that
	set the derivate of the piecewise polynomial equal to zero (quadratic formula). Next we ensure that
	the zero values are in fact real (is_real). We then filter out the zeros that are not in the
	appropriate domains (is_in_appropriate_domain). To see if these values are maximum or minimum, we
	plug them back into the second derivative of the appropriate piecewise polynomial (second_deriv_is_neg()
	and second_deriv_is_pos(), respectively). Finally we determine the y value of these extrema by using
	the class function self.pp_func().

	is_initial_slope_positive()
	~~~~~~~~~~~~~~~~~~~

	This takes in X and will check to make sure that for the first 2500 entries in X, that the derivative
	of the piecewise polynomial (pp_deriv()) will be positive. Make sure that X is at least 2500 in length.

	is_within_bounds()
	~~~~~~~~~~~~~~~~~~~

	This checks to see if the maximum maximum value and the minimum mininum value calculated above will
	fall between y_min and y_max. This makes use of self.find_max_and_min()

	"""
	def __init__(self,a,b,c,d,x_initial,x_break):
		self.a = a
		self.b = b
		self.c = c
		self.d = d
		self.x_initial = x_initial
		self.x_break = x_break
		#self.all_values = {'A': a, 'B' : b, 'C' : c, 'D' : d, 'init' : x_initial, 'break' : x_break}
	def pp_func(self,X):
		result = np.piecewise(X,[X <= self.x_break, X > self.x_break], \
									[lambda X: self.a[0] + self.b[0]*(X-self.x_initial) + self.c[0]*(X-self.x_initial)**2 + self.d[0]*(X-self.x_initial)**3, \
									lambda X: self.a[1] + self.b[1]*(X-self.x_break) + self.c[1]*(X-self.x_break)**2 + self.d[1]*(X-self.x_break)**3])
		return(result)
	def pp_deriv(self,X):
		result = np.piecewise(X,[X <= self.x_break, X > self.x_break], \
									[lambda X: self.b[0] + 2*self.c[0]*(X-self.x_initial) + 3*self.d[0]*(X-self.x_initial)**2, \
									lambda X: self.b[1] + 2*self.c[1]*(X-self.x_break) + 3*self.d[1]*(X-self.x_break)**2])
		return(result)
	def find_max_and_min(self,x_min,x_max,y_min,y_max):
		def find_extrema():
			extrema_1 = np.float(self.x_initial + (- 2*self.c[0] + (4*self.c[0]**2 - 12*self.b[0]*self.d[0])**.5)/(6*self.d[0]))
			extrema_2 = np.float(self.x_initial + (- 2*self.c[0] - (4*self.c[0]**2 - 12*self.b[0]*self.d[0])**.5)/(6*self.d[0]))
			extrema_3 = np.float(self.x_break + (- 2*self.c[1] + (4*self.c[1]**2 - 12*self.b[1]*self.d[1])**.5)/(6*self.d[1]))
			extrema_4 = np.float(self.x_break + (- 2*self.c[1] - (4*self.c[1]**2 - 12*self.b[1]*self.d[1])**.5)/(6*self.d[1]))
			return(extrema_1,extrema_2,extrema_3,extrema_4)
		def is_real(x_value):
			result = not isinstance(x_value,complex)
			return(result)
		def is_in_appropriate_domain(x_value,x_min,x_max,segment_number):
			if segment_number == 1:
				result = x_value >= x_min and x_value <= self.x_break
			elif segment_number == 2:
				result = x_value >= self.x_break and x_value <= x_max
			return(result)
		def second_deriv_is_neg(x_value,segment_number):
			if segment_number == 1:
				x_not = self.x_initial
			elif segment_number == 2:
				x_not = self.x_break
			second_deriv =  2*self.c[segment_number-1] + 6*self.d[segment_number-1]*(x_value-x_not)
			result = second_deriv<0
			return(result)
		def second_deriv_is_pos(x_value,segment_number):
			if segment_number == 1:
				x_not = self.x_initial
			elif segment_number == 2:
				x_not = self.x_break
			second_deriv =  2*self.c[segment_number-1] + 6*self.d[segment_number-1]*(x_value-x_not)
			result = second_deriv>0
			return(result)
		def determine_if_max_or_min(extrema_1,extrema_2,extrema_3,extrema_4,x_min,x_max):
			maxima = []
			minima = []
			if is_real(extrema_1) and is_in_appropriate_domain(extrema_1,x_min,x_max,1):
				if second_deriv_is_neg(extrema_1,1):
					maxima.append(np.float(self.pp_func(extrema_1)))
				elif second_deriv_is_pos(extrema_1,1):
					minima.append(np.float(self.pp_func(extrema_1)))
			if is_real(extrema_2) and is_in_appropriate_domain(extrema_2,x_min,x_max,1):
				if second_deriv_is_neg(extrema_2,1):
					maxima.append(np.float(self.pp_func(extrema_2)))
				elif second_deriv_is_pos(extrema_2,1):
					minima.append(np.float(self.pp_func(extrema_2)))
			if is_real(extrema_3) and is_in_appropriate_domain(extrema_3,x_min,x_max,2):
				if second_deriv_is_neg(extrema_3,2):
					maxima.append(np.float(self.pp_func(extrema_3)))
				elif second_deriv_is_pos(extrema_3,2):
					minima.append(np.float(self.pp_func(extrema_3)))
			if is_real(extrema_4) and is_in_appropriate_domain(extrema_4,x_min,x_max,2):
				if second_deriv_is_neg(extrema_4,2):
					maxima.append(np.float(self.pp_func(extrema_4)))
				elif second_deriv_is_pos(extrema_4,2):
					minima.append(np.float(self.pp_func(extrema_4)))
			return(maxima,minima)
		extrema_1,extrema_2,extrema_3,extrema_4 = find_extrema()
		maxima, minima = determine_if_max_or_min(extrema_1,extrema_2,extrema_3,extrema_4,x_min,x_max)
		return(maxima,minima)
	def is_initial_slope_positive(self,X):
		result = min(self.pp_deriv(X[:2501]))>=0
		return(result)
	def is_within_bounds(self,x_min,x_max,y_min,y_max):
		maxima,minima = self.find_max_and_min(x_min,x_max,y_min,y_max)
		if len(maxima) == 0:
			maxima = y_max
		if len(minima) == 0:
			minima = y_min
		result = np.max(maxima) <= y_max and np.min(minima) >= y_min
		return(result)
def normalized_muscle_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
	#R_tranpose Column 1
	r1DELTa = 19
	r1CB = 20
	r1DELTp = -8
	r1BIC = 15
	r1TRI = -15
	r1BRA = 0
	r1BRD = 0
	r1PRO = 0
	r1FCR = 0
	r1ECRB = 0
	r1ECRL = 0
	r1FCU = 0
	r1FDS = 0
	r1PL = 0
	r1ECU = 0
	r1EDM = 0
	r1EDC = 0
	r1APL = 0

	#R_tranpose Column 2
	r2DELTa = 0
	r2CB = 0
	r2DELTp = 0
	r2BIC = lambda Angle2: (24777*np.pi)/10000 + (1288265228720957*Angle2)/35184372088832 \
							- (2429*np.pi*Angle2)/125 + (68251*np.pi*(Angle2**2))/5000 \
							- (10427*np.pi*(Angle2**3))/5000 + (20571*(Angle2**2))/10000 \
							- (14043*(Angle2**3))/2500 + 84533/10000
	r2TRI = lambda Angle2: - (8759*(Angle2**3))/5000 + (93509*(Angle2**2))/10000 \
							- (88691*Angle2)/10000 - 863614486669217/35184372088832
	r2BRA = lambda Angle2: - (12667*(Angle2**3))/2000 + (30689*(Angle2**2))/1250 \
							- (4544779416463265*Angle2)/281474976710656 + 1139910323808397/70368744177664
	r2BRD = lambda Angle2: (28129*np.pi)/10000 - (23671*Angle2)/2000 - (57781*np.pi*Angle2)/10000 \
							+ (3629*np.pi*(Angle2**2))/1250 - (197*np.pi*(Angle2**3))/500 \
							+ (24636921970321*(Angle2**2))/549755813888 - (33739*(Angle2**3))/2500 + 38141/2500
	r2PRO = lambda Angle2: (3933*np.pi)/10000 - (10079*Angle2)/10000 - (13103*np.pi*Angle2)/1250 \
							+ (2597831076304493*np.pi*(Angle2**2))/70368744177664 + (2202*np.pi**2*Angle2)/625 \
							- (93111*np.pi*(Angle2**3))/2500 + (72987*np.pi*(Angle2**4))/5000 \
							- (20089*np.pi*(Angle2**5))/10000 - (4369*np.pi**2)/10000 \
							- (6847666938421497*(Angle2**2))/562949953421312 + (53151*(Angle2**3))/2500 \
							- (5503*(Angle2**4))/500 + (8763*(Angle2**5))/5000 \
							- (1466808324885735*np.pi**2*(Angle2**2))/140737488355328 + (51333*np.pi**2*(Angle2**3))/5000 \
							- (39919*np.pi**2*(Angle2**4))/10000 + (273*np.pi**2*(Angle2**5))/500 + 22081/2000
	r2FCR = 14
	r2ECRB = lambda Angle2: (8199*np.pi)/5000 + (44637*Angle2)/2500 - (5073*np.pi*Angle2)/10000 \
							- (471*np.pi*(Angle2**2))/5000 - (28827*(Angle2**2))/10000 - 1407/125
	r2ECRL = lambda Angle2: (74361*np.pi)/10000 + (72089699777459*Angle2)/4398046511104 \
							- (8783*np.pi*Angle2)/5000 + (371*np.pi**2*Angle2)/5000 - (1667*np.pi**2)/1250 \
							- 38517/5000
	r2FCU = 19
	r2FDS = 20
	r2PL = 25
	r2ECU = -23
	r2EDM = -10
	r2EDC = -20
	r2APL = 0

	#R_tranpose Column 3
	r3DELTa = 0
	r3CB = 0
	r3DELTp = 0
	r3BIC = 0
	r3TRI = 0
	r3BRA = 0
	r3BRD = 0
	r3PRO = 0
	r3FCR = lambda Angle3: (3199*Angle3)/2000 + 3301/250
	r3ECRB = lambda Angle3: (21411*Angle3)/10000 - 7562500789275879/562949953421312
	r3ECRL = lambda Angle3: (457*Angle3)/200 - 58583/5000
	r3FCU = lambda Angle3: (13307*(Angle3**2))/10000 + (1869*Angle3)/400 + 1578328710658497/140737488355328
	r3FDS = lambda Angle3: (2099*(Angle3**2))/2000 + (10641*Angle3)/10000 + 5824674283064289/562949953421312
	r3PL = lambda Angle3: (5011*(Angle3**2))/10000 + (13821*Angle3)/10000 + 3749/400
	r3ECU = lambda Angle3: (3883*Angle3)/1250 - 21289/2500
	r3EDM = lambda Angle3: (7603*Angle3)/2500 - 7791/625
	r3EDC = lambda Angle3: (693*Angle3)/400 - 35319/2500
	r3APL = lambda Angle3: 1171/2000 - (171*(Angle3**2))/2000 - (73*Angle3)/2500

	OptimalMuscleLength = np.array([(9.8),     (9.3),      (13.7),      (11.6),      (13.4),    (8.6),   \
	                  				(17.3),    (4.9),       (6.3),       (5.9),       (8.1),    (5.1),   \
	                   				(8.4),     (6.4),       (6.2),       (6.8),        (7.),    (7.1)] )
	def muscle_1_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1DELTa  									\
					- Angle2Spline.pp_deriv(Time)*r2DELTa  								\
					-Angle3Spline.pp_deriv(Time)*r3DELTa)  								\
         	        /(10*OptimalMuscleLength[0])
		return(velocity)
	def muscle_2_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1CB  								\
                    -Angle2Spline.pp_deriv(Time)*r2CB  									\
                    -Angle3Spline.pp_deriv(Time)*r3CB)  								\
                    /(10*OptimalMuscleLength[1])
		return(velocity)
	def muscle_3_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1DELTp  								\
                    -Angle2Spline.pp_deriv(Time)*r2DELTp  								\
                    -Angle3Spline.pp_deriv(Time)*r3DELTp)  								\
                    /(10*OptimalMuscleLength[2])
		return(velocity)
	def muscle_4_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1BIC  								\
                    -Angle2Spline.pp_deriv(Time)*r2BIC(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3BIC)  								\
                    /(10*OptimalMuscleLength[3])
		return(velocity)
	def muscle_5_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1TRI  								\
                    -Angle2Spline.pp_deriv(Time)*r2TRI(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3TRI)  								\
                    /(10*OptimalMuscleLength[4])
		return(velocity)
	def muscle_6_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1BRA  								\
                    -Angle2Spline.pp_deriv(Time)*r2BRA(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3BRA)  								\
                    /(10*OptimalMuscleLength[5])
		return(velocity)
	def muscle_7_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1BRD  								\
                    -Angle2Spline.pp_deriv(Time)*r2BRD(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3BRD)  								\
                    /(10*OptimalMuscleLength[6])
		return(velocity)
	def muscle_8_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1PRO  								\
                    -Angle2Spline.pp_deriv(Time)*r2PRO(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3PRO)  								\
                    /(10*OptimalMuscleLength[7])
		return(velocity)
	def muscle_9_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1FCR  								\
                    -Angle2Spline.pp_deriv(Time)*r2FCR  								\
                    -Angle3Spline.pp_deriv(Time)*r3FCR(Angle3Spline.pp_func(Time)))  	\
                    /(10*OptimalMuscleLength[8])
		return(velocity)
	def muscle_10_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1ECRB  								\
                    -Angle2Spline.pp_deriv(Time)*r2ECRB(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3ECRB(Angle3Spline.pp_func(Time)))  	\
                    /(10*OptimalMuscleLength[9])
		return(velocity)
	def muscle_11_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1ECRL  								\
                    -Angle2Spline.pp_deriv(Time)*r2ECRL(Angle2Spline.pp_func(Time))  	\
                    -Angle3Spline.pp_deriv(Time)*r3ECRL(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[10])
		return(velocity)
	def muscle_12_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1FCU  								\
                    -Angle2Spline.pp_deriv(Time)*r2FCU  								\
                    -Angle3Spline.pp_deriv(Time)*r3FCU(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[11])
		return(velocity)
	def muscle_13_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1FDS  								\
                    -Angle2Spline.pp_deriv(Time)*r2FDS  								\
                    -Angle3Spline.pp_deriv(Time)*r3FDS(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[12])
		return(velocity)
	def muscle_14_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1PL  									\
                    -Angle2Spline.pp_deriv(Time)*r2PL  									\
                    -Angle3Spline.pp_deriv(Time)*r3PL(Angle3Spline.pp_func(Time))) 		\
                    /(10*OptimalMuscleLength[13])
		return(velocity)
	def muscle_15_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1ECU  								\
                    -Angle2Spline.pp_deriv(Time)*r2ECU  								\
                    -Angle3Spline.pp_deriv(Time)*r3ECU(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[14])
		return(velocity)
	def muscle_16_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1EDM  								\
                    -Angle2Spline.pp_deriv(Time)*r2EDM  								\
                    -Angle3Spline.pp_deriv(Time)*r3EDM(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[15])
		return(velocity)
	def muscle_17_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1EDC  								\
                    -Angle2Spline.pp_deriv(Time)*r2EDC  								\
                    -Angle3Spline.pp_deriv(Time)*r3EDC(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[16])
		return(velocity)
	def muscle_18_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time):
		velocity = (-Angle1Spline.pp_deriv(Time)*r1APL  								\
                    -Angle2Spline.pp_deriv(Time)*r2APL  								\
                    -Angle3Spline.pp_deriv(Time)*r3APL(Angle3Spline.pp_func(Time))) 	\
                    /(10*OptimalMuscleLength[17])
		return(velocity)
	NormalizedMuscleVelocity = np.array([ 	muscle_1_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_2_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_3_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_4_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_5_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_6_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_7_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_8_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_9_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_10_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_11_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_12_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_13_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_14_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_15_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_16_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_17_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time), \
											muscle_18_velocity(Angle1Spline,Angle2Spline,Angle3Spline,Time) 	],	\
			                                float)
	return(NormalizedMuscleVelocity)
def sign_changed_here(X):
	import numpy as np
	sign_change_index = []
	if np.shape(X) == (len(X),):
		Xsign = np.sign(X[len(X):])
		signchange = ((np.roll(Xsign,1)-Xsign) != 0).astype(int)
		signchange[0] = 0
		if 1 in list(signchange):
			temp = []
			while 1 in list(signchange):
				temp.append(list(signchange).index(1))
				signchange[list(signchange).index(1)]=0
			sign_change_index.append(temp[-1])
		else:
			sign_change_index.append(5199)
	else:
		for i in range(np.shape(X)[0]):
			Xsign = np.sign(X[i,:])
			Xsign[0] = Xsign[1]
			signchange = ((np.roll(Xsign,1)-Xsign) != 0).astype(int)
			signchange[0] = 0
			if 1 in list(signchange):
				temp = []
				while 1 in list(signchange):
					temp.append(list(signchange).index(1))
					signchange[list(signchange).index(1)] = 0
				sign_change_index.append(temp[-1])
			else:
				sign_change_index.append(5199)
	return(sign_change_index)

def concentric_muscle_power(Angle1Splines,Angle2Splines,Angle3Splines,Time):
	dt = Time[1]-Time[0]
	NormalizedMuscleVelocity = normalized_muscle_velocity(Angle1Splines,Angle2Splines,Angle3Splines,Time)
	Zeros = [[0]*len(NormalizedMuscleVelocity[0])]*len(NormalizedMuscleVelocity)
	zero_velocity_index = sign_changed_here(NormalizedMuscleVelocity)
	ConcentricContractions = (NormalizedMuscleVelocity<Zeros)*NormalizedMuscleVelocity
	OptimalMuscleLength = np.array([[(9.8),     (9.3),      (13.7),      (11.6),      (13.4),    (8.6),   \
                  					(17.3),    (4.9),       (6.3),       (5.9),       (8.1),    (5.1),   \
                   					(8.4),     (6.4),       (6.2),       (6.8),        (7.),    (7.1)]] )
	ConcentricMuscleAcceleration = np.concatenate((np.array([[0]*18]).T,np.diff(ConcentricContractions*OptimalMuscleLength.T)/dt),axis=1)
	ConcentricMusclePower = ConcentricMuscleAcceleration*(ConcentricContractions*OptimalMuscleLength.T)
	Power = 0
	for i in range(18): Power += max(ConcentricMusclePower[i,:zero_velocity_index[i]])
	return(Power)
def statusbar(i,N,**kwargs):
	"""
	i is the current iteration (must be an int) and N is the length of
	the range (must be an int). i must also be in [0,N).

	~~~~~~~~~~~~~~
	**kwargs
	~~~~~~~~~~~~~~

	StartTime should equal time.time() and should be defined before your
	loop to ensure that you get an accurate representation of elapsed time.

	Title should be a str that will be displayed before the statusbar. Title
	should be no longer than 25 characters.

	~~~~~~~~~~~~~~

	NOTE: you should place a print('\n') after the loop to ensure you
	begin printing on the next line.

	"""
	import time
	StartTime = kwargs.get("StartTime",False)
	Title = kwargs.get("Title",'')

	assert type(i)==int, "i must be an int"
	assert type(N)==int, "N must be an int"
	assert N>i, "N must be greater than i"
	assert N>0, "N must be a positive integer"
	assert i>=0, "i must not be negative (can be zero)"
	assert type(Title) == str, "Title should be a string"
	assert len(Title) <= 25, "Title should be less than 25 characters"
	if Title != '': Title = ' '*(25-len(Title)) + Title + ': '
	statusbar = Title +'[' + '\u25a0'*int((i+1)/(N/50)) + '\u25a1'*(50-int((i+1)/(N/50))) + '] '
	if StartTime != False:
		print(statusbar + '{0:1.1f}'.format((i+1)/N*100) + '% complete, ' + '{0:1.1f}'.format(time.time() - StartTime) + 'sec        \r', end='')
	else:
		print(statusbar + '{0:1.1f}'.format((i+1)/N*100) + '% complete           \r',end = '')

EndTime = 0.55
ChangeInTime = 0.0001
Time = np.arange(0,EndTime+ChangeInTime,ChangeInTime,float)
AngleSplines = pickle.load(open('AllAngleSplines.pkl','rb'))
Angle1Splines = AngleSplines[0]
Angle2Splines = AngleSplines[1]
Angle3Splines = AngleSplines[2]
StartTime = time.time()
EccentricAccelerations = []
for i in range(len(Angle1Splines)):
	EccentricAccelerations.append(eccentric_muscle_acceleration(Angle1Splines[i],Angle2Splines[i],Angle3Splines[i],Time))
	statusbar(i,len(Angle1Splines),StartTime=StartTime,Title = "Ecc. Accel.")

plt.figure()
plt.hist(EccentricAccelerations,bins = 25)
plt.xlabel("Eccentric Accelerations")
plt.ylabel("Frequency")
plt.show()


import logging
import requests
from time import time

try:
  from google.appengine.ext import ndb
except e:
  logging.warn('appengine not found, NDBConfigSource will not work')

class NDBConfig(ndb.Model):
  props = ndb.JsonProperty()

class PropertySource(object):
  """A source of properties."""

  # time to live in seconds
  ttl = 60

  def fetch(self, existing_props={}):
    """Fetch properties.

    existing_props - all the properties that have been fetched so far
    return dictionary of properties
    """
    raise ValueError('should be implemented by subclass')

class NDBConfigSource(PropertySource):
  """Fetch/Store properties from appengine ndb"""

  def __init__(self, key):
    self.key = key

  def fetch(self, existing_props={}):
    """Fetch properties from ndb for a given key.

    return properties or empty {} if not found
    raise ValueError when not in appengine context
    """
    if not ndb:
      raise ValueError('not inside appengine context')

    config = NDBConfig.get_by_id(self.key)
    if not config:
      return {}

    return config.props

  def store(self, props):
    """Update ndb config entry"""
    config = NDBConfig(id = self.key, props = props).put()
    return props

class ConfigServiceSource(PropertySource):

  def __init__(self, namespace=None):
    """Define a config source from centralized config service.

    namespace - the config namespace
    """
    if not namespace:
      raise ValueError("url is required")

    self.namespace = namespace
    self.url = 'https://ragilconfig.appspot.com/config/%s' % self.namespace

  def fetch(self, existing_props={}):
    r = requests.get(self.url,
        auth=(existing_props['username'], existing_props['password']))

    if r.status_code == 200:
      return r.json()

    raise ValueError('Cannot fetch properties for namespace %s.\n%s' %
        (namespace, r.json()))

class Properties(object):

  def __init__(self, sources=[]):
    """Manages property lifecycle.

    sources - a list of PropertySource
    """
    self.sources = sources
    self.prop_cache = {}

  def fetch(self):
    """Return properties from all config sources"""
    props = {}
    for source in self.sources:

      prop = None

      # check if we still have valid props
      if source in self.prop_cache:
        if int(time()) - self.prop_cache[source]['ts_created'] < source.ttl:
          prop = self.prop_cache[source]['prop']

      if not prop:
        prop = source.fetch(props)
        self.prop_cache[source] = {
          'ts_created' : int(time()),
          'prop' : prop
        }

      props.update(prop)

    return props


import cv2
import operator

######################################################################
class ContourWithData ():
    # member variables
    npaContour = None  # contour
    boundingRect = None  # bounding rect for contour
    intRectX = 0  # bounding rect top left corner x location
    intRectY = 0  # bounding rect top left corner y location
    intRectWidth = 0  # bounding rect width
    intRectHeight = 0  # bounding rect height
    fltArea = 0.0  # area of contour
    aspectratio = 0  # aspect ratio of the contour formed
    avgwidth = 0
    avgheight =0

    def Calculate_Parameters(self):  # calculate bounding rect info
        [intX, intY, intWidth, intHeight] = self.boundingRect
        self.intRectX = intX
        self.intRectY = intY
        self.intRectWidth = intWidth
        self.intRectHeight = intHeight
        self.aspectratio = intWidth / intHeight;

def main(path,img):
    # Read the Image

    frame = cv2.imread(path + img)
    blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
    hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

    lower = {'red': (166, 84, 141),'orange': (0, 50, 80)}  # assign new item lower['blue'] = (93, 10, 0)
    upper = {'red': (186, 255, 255),'orange': (20, 255, 255)}

    mask = None

    for key, value in upper.items():
        mask = cv2.inRange(hsv, lower[key], upper[key])

    allContoursWithData = []
    mask1=mask.copy()
    contours, _ = cv2.findContours(mask1, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    ValidContour = []

    for i in contours:  # for each contour
        contourWithData = ContourWithData()  # instantiate a contour with data object
        contourWithData.npaContour = i  # assign contour to contour with data
        contourWithData.boundingRect = cv2.boundingRect ( contourWithData.npaContour )  # get the bounding rect
        contourWithData.Calculate_Parameters ()  # get bounding rect info
        contourWithData.fltArea = cv2.contourArea ( contourWithData.npaContour )  # calculate the contour area
        allContoursWithData.append ( contourWithData )  # add contour with data object to list of all contours with data

    for contour in allContoursWithData:
        if contour.fltArea > 0:
            ValidContour.append(contour)

    if len(ValidContour)==0:
        cv2.imwrite(path+"/Image_Processing/significant_img.jpg",frame)
        print("No Insignificant Numbers found.")
        return

    ValidContour.sort(key=operator.attrgetter("intRectX"))
    cv2.drawContours(frame, contours, -1, (54,255,255),5)
    h, w = frame.shape[:2]
    x=None

    if ValidContour[0].intRectX<(w/4):
        for i in range(len(ValidContour)):
            if ValidContour[i].intRectX>w/4:
                x=ValidContour[i].intRectX
    else:
        x=ValidContour[0].intRectX
    initial_y=ValidContour[0].intRectY

    y=initial_y+h

    #v = cv2.line(frame,(x,0),(x,y),(0,255,0),2)

    crop = frame[0:0 + y, 0:0 + x]

   #cv2.imshow ( "Significant_Digits Image", crop )
   # cv2.waitKey ( 0 )

    cv2.imwrite(path+"Image_Processing/significant_img.jpg",crop)
    print ( "Saving Significant Numbers." )
    return

if __name__ == '__main__':
    path= "C:/Users/Dushyant Sharma/Desktop/dataset/"
    main(path,"11.jpg")
    a=cv2.imread(path+"11.jpg")
    b=cv2.imread(path+"Image_Processing/significant_img.jpg")
    cv2.imshow("Original Image",a)
    cv2.imshow("Cropped Image",b)
    cv2.waitKey(0)


# Copyright 2013 Google Inc. All Rights Reserved.

"""A module to make it easy to set up and run CLIs in the Cloud SDK."""

import urllib
import urlparse
import uuid

from oauth2client import client

from googlecloudsdk.core import config
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.credentials import store as c_store
from googlecloudsdk.core.util import console_io

__all__ = ['Credentials', 'Http']

class Error(exceptions.Error):
  """Exceptions for the cli module."""

class CannotRefreshAuthTokenError(Error, client.AccessTokenRefreshError):
  """An exception raised when the auth tokens fail to refresh."""

  def __init__(self, msg):
    auth_command = '$ gcloud auth login'
    message = ('There was a problem refreshing your current auth tokens: '
               '{0}.  Please run\n  {1}.'.format(msg, auth_command))
    super(CannotRefreshAuthTokenError, self).__init__(message)

def Credentials():
  """Get the currently active credentials.

  This function loads account credentials via core.account property
  of core.properties module.

  These credentials will be refreshed before being returned, so it makes sense
  to cache the value returned for short-lived programs.

  Returns:
    An active, valid credentials object.

  Raises:
    c_store.Error: If an error loading the credentials occurs.
  """
  return c_store.Load()

def Http(cmd_path=None, trace_token=None,
         auth=True, creds=None, timeout=None, log_http=False):
  """Get an httplib2.Http object for working with the Google API.

  Args:
    cmd_path: str, Path of command that will use the httplib2.Http object.
    trace_token: str, Token to be used to route service request traces.
    auth: bool, True if the http object returned should be authorized.
    creds: oauth2client.client.Credentials, If auth is True and creds is not
        None, use those credentials to authorize the httplib2.Http object.
    timeout: double, The timeout in seconds to pass to httplib2.  This is the
        socket level timeout.  If timeout is None, timeout is infinite.
    log_http: bool, Enable/disable client side logging of service requests.

  Returns:
    An authorized httplib2.Http object, or a regular httplib2.Http object if no
    credentials are available.
  """

  # TODO(user): Have retry-once-if-denied logic, to allow client tools to not
  # worry about refreshing credentials.

  http = c_store._Http(timeout=timeout)  # pylint:disable=protected-access

  # Wrap first to dump any data added by other wrappers.
  if log_http:
    http = _WrapRequestForLogging(http)

  # Wrap the request method to put in our own user-agent, and trace reporting.
  gcloud_ua = 'gcloud/{0} command/{1} invocation-id/{2} interactive/{3}'.format(
      config.CLOUD_SDK_VERSION,
      cmd_path,
      uuid.uuid4().hex,
      console_io.IsInteractive())
  http = _WrapRequestForUserAgentAndTracing(http, trace_token,
                                            gcloud_ua)
  if auth:
    if not creds:
      creds = Credentials()
    http = creds.authorize(http)
    # Wrap the request method to put in our own error handling.
    http = _WrapRequestForAuthErrHandling(http)
  return http

def _WrapRequestForUserAgentAndTracing(http, trace_token,
                                       gcloud_ua):
  """Wrap request with user-agent, and trace reporting.

  Args:
    http: The original http object.
    trace_token: str, Token to be used to route service request traces.
    gcloud_ua: str, User agent string to be included in the request.

  Returns:
    http, The same http object but with the request method wrapped.
  """
  orig_request = http.request

  def RequestWithUserAgentAndTracing(*args, **kwargs):
    """Wrap request with user-agent, and trace reporting.

    Args:
      *args: Positional arguments.
      **kwargs: Keyword arguments.

    Returns:
      Wrapped request method with user-agent and trace reporting.
    """
    modified_args = list(args)

    # Use gcloud specific user-agent with command path and invocation-id.
    # Pass in the user-agent through kwargs or args.
    def UserAgent(current=''):
      user_agent = '{0} {1}'.format(current, gcloud_ua)
      return user_agent.strip()
    if 'headers' in kwargs:
      cur_ua = kwargs['headers'].get('user-agent', '')
      kwargs['headers']['user-agent'] = UserAgent(cur_ua)
    elif len(args) > 3:
      cur_ua = modified_args[3].get('user-agent', '')
      modified_args[3]['user-agent'] = UserAgent(cur_ua)
    else:
      kwargs['headers'] = {'user-agent': UserAgent()}

    # Modify request url to enable requested tracing.
    url_parts = urlparse.urlsplit(args[0])
    query_params = urlparse.parse_qs(url_parts.query)
    if trace_token:
      query_params['trace'] = 'token:{0}'.format(trace_token)

    # Replace the request url in the args
    modified_url_parts = list(url_parts)
    modified_url_parts[3] = urllib.urlencode(query_params, doseq=True)
    modified_args[0] = urlparse.urlunsplit(modified_url_parts)

    return orig_request(*modified_args, **kwargs)

  http.request = RequestWithUserAgentAndTracing

  # apitools needs this attribute to do credential refreshes during batch API
  # requests.
  if hasattr(orig_request, 'credentials'):
    setattr(http.request, 'credentials', orig_request.credentials)

  return http

def _WrapRequestForAuthErrHandling(http):
  """Wrap request with exception handling for auth.

  We need to wrap exception handling because oauth2client does similar wrapping
  when you authorize the http object.  Because of this, a credential refresh
  error can get raised wherever someone makes an http request.  With no common
  place to handle this exception, we do more wrapping here so we can convert it
  to one of our typed exceptions.

  Args:
    http: The original http object.

  Returns:
    http, The same http object but with the request method wrapped.
  """
  orig_request = http.request

  def RequestWithErrHandling(*args, **kwargs):
    try:
      return orig_request(*args, **kwargs)
    except client.AccessTokenRefreshError as e:
      log.debug('Exception caught during HTTP request: %s', e.message,
                exc_info=True)
      raise CannotRefreshAuthTokenError(e.message)

  http.request = RequestWithErrHandling

  # apitools needs this attribute to do credential refreshes during batch API
  # requests.
  if hasattr(orig_request, 'credentials'):
    setattr(http.request, 'credentials', orig_request.credentials)

  return http

def _WrapRequestForLogging(http):
  """Wrap request for capturing and logging of http request/response data.

  Args:
    http: httplib2.Http, The original http object.

  Returns:
    http, The same http object but with the request method wrapped.
  """

  orig_request = http.request

  def RequestWithLogging(*args, **kwargs):
    """Wrap request for request/response logging.

    Args:
      *args: Positional arguments.
      **kwargs: Keyword arguments.

    Returns:
      Original returned response of this wrapped request.
    """
    _LogRequest(*args, **kwargs)
    response = orig_request(*args, **kwargs)
    _LogResponse(response)
    return response

  http.request = RequestWithLogging

  # apitools needs this attribute to do credential refreshes during batch API
  # requests.
  if hasattr(orig_request, 'credentials'):
    setattr(http.request, 'credentials', orig_request.credentials)

  return http

def _LogRequest(*args, **kwargs):
  """Logs request arguments."""

  log.status.Print('--request-start--')
  log.status.Print('uri: {uri}'.format(uri=args[0]))
  log.status.Print('-headers-start-')
  for h, v in sorted(kwargs.get('headers', {}).iteritems()):
    log.status.Print('{0}: {1}'.format(h, v))
  log.status.Print('-headers-end-')
  log.status.Print('-body-start-')
  if 'body' in kwargs:
    log.status.Print(kwargs['body'])
  log.status.Print('-body-end-')
  log.status.Print('--request-end--')

def _LogResponse(response):
  """Logs response headers and content."""

  headers, content = response
  log.status.Print('--response-start--')
  log.status.Print('-headers-start-')
  for h, v in sorted(headers.iteritems()):
    log.status.Print('{0}: {1}'.format(h, v))
  log.status.Print('-headers-end-')
  log.status.Print('-body-start-')
  log.status.Print(content)
  log.status.Print('-body-end-')
  log.status.Print('--response-end--')


import datetime
import itertools
import time
import types

from selenium import webdriver

import vtk

def now():
    return datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")

def compare_images(test_img, baseline_img):
    # Create a vtkTesting object, and tell it to use the current directory as
    # the "baseline root directory", and specify the name of the baseline image
    # file itself.
    t = vtk.vtkTesting()
    t.AddArgument("-B")
    t.AddArgument(".")
    t.AddArgument("-V")
    t.AddArgument(baseline_img)

    # Perform the image comparison test and print out the result.
    t.RegressionTest(test_img, 0.0)

def wait_with_timeout(delay=None, limit=None, criterion=None):
    for i in itertools.count():
        if criterion():
            return True
        elif delay * i > limit:
            return False
        else:
            time.sleep(delay)

browsers = ["firefox", "chrome", "internet_explorer"]
Browsers = type("Enum", (), {k: i for i, k in enumerate(browsers)})

class WebTest(object):
    class Abort:
        pass

    def __init__(self, size=None, url=None, browser=None):
        self.size = size
        self.url = url
        self.browser = browser

        self.window = None

    def run_test(self):
        result = None
        try:
            self.initialize()
            self.setup()
            self.capture()
            result = self.postprocess()
        except WebTest.Abort:
            result = 1

        self.cleanup()
        return result

    def initialize(self):
        if self.browser is None or self.browser == Browsers.chrome:
            self.window = webdriver.Chrome()
        elif self.browser == Browsers.firefox:
            self.window = webdriver.Firefox()
        elif self.browser == Browsers.internet_explorer:
            self.window = webdriver.Ie()
        else:
            raise ValueError("self.browser argument has illegal value %r" %
                             (self.browser))

        if self.size is not None:
            self.window.set_window_size(self.size[0], self.size[1])

        if self.url is not None:
            self.window.get(self.url)

    def setup(self):
        pass

    def capture(self):
        self.filename = "%s-%s.png" % (self.__class__name, now())
        self.window.save_screenshot(filename)

    def postprocess(self):
        pass

    def cleanup(self):
        self.window.quit()

class CDashImageComparator(WebTest):
    def __init__(self, filepat=None, baseline=None, **kwargs):
        if filepat is None:
            raise TypeError("missing argument 'filepat'")
        if baseline is None:
            raise TypeError("missing argument 'baseline'")

        WebTest.__init__(self, **kwargs)
        self.filepat = filepat
        self.baseline = baseline

    def capture(self):
        self.filename = self.filepat % (now())
        self.window.save_screenshot(self.filename)

    def postprocess(self):
        compare_images(self.filename, self.baseline)


import os
import logging
from importlib import import_module

from django.conf import settings
from django.http import HttpResponse, HttpResponseServerError
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt

from djangae.utils import on_production
from djangae.core.signals import module_started, module_stopped

def warmup(request):
    """
        Provides default procedure for handling warmup requests on App
        Engine. Just add this view to your main urls.py.
    """
    for app in settings.INSTALLED_APPS:
        for name in ('urls', 'views', 'models'):
            try:
                import_module('%s.%s' % (app, name))
            except ImportError:
                pass
    return HttpResponse("Ok.")

def start(request):
    module_started.send(sender=__name__, request=request)
    return HttpResponse("Ok.")

def stop(request):
    module_stopped.send(sender=__name__, request=request)
    return HttpResponse("Ok.")

@csrf_exempt
def deferred(request):
    from google.appengine.ext.deferred.deferred import (
        run,
        SingularTaskFailure,
        PermanentTaskFailure
    )

    response = HttpResponse()

    if 'HTTP_X_APPENGINE_TASKEXECUTIONCOUNT' in request.META:
        logging.debug("[DEFERRED] Retry %s of deferred task", request.META['HTTP_X_APPENGINE_TASKEXECUTIONCOUNT'])

    if 'HTTP_X_APPENGINE_TASKNAME' not in request.META:
        logging.critical('Detected an attempted XSRF attack. The header "X-AppEngine-Taskname" was not set.')
        response.status_code = 403
        return response

    in_prod = on_production()

    if in_prod and os.environ.get("REMOTE_ADDR") != "0.1.0.2":
        logging.critical('Detected an attempted XSRF attack. This request did not originate from Task Queue.')
        response.status_code = 403
        return response

    try:
        run(request.body)
    except SingularTaskFailure:
        logging.debug("Failure executing task, task retry forced")
        response.status_code = 408
    except PermanentTaskFailure:
        logging.exception("Permanent failure attempting to execute task")

    return response

@csrf_exempt
@require_POST
def internalupload(request):
    try:
        return HttpResponse(str(request.FILES['file'].blobstore_info.key()))
    except Exception:
        logging.exception("DJANGAE UPLOAD FAILED: The internal upload handler couldn't retrieve the blob info key.")
        return HttpResponseServerError()


# import the necessary packages
import numpy as np
import imutils
import cv2

class Stitcher:
    def stitch(self, images, ratio=0.75, reprojThresh=4.0, showMatches=False):
        (image2, image1) = images
        (kps1, features1) = self.detectAndDescribe(image1)
        (kps2, features2) = self.detectAndDescribe(image2)

        M = self.matchKeypoints(kps1, kps2,
                                features1, features2, ratio, reprojThresh)
        if M is None:
            return None

        (matches, H, status) = M
        result = cv2.warpPerspective(image1, H,
                                     (image1.shape[1] + image2.shape[1], image1.shape[0]))
        result[0:image2.shape[0], 0:image2.shape[1]] = image2

        return result

    def detectAndDescribe(self, image):
        #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        descriptor = cv2.xfeatures2d.SIFT_create()
        (kps, features) = descriptor.detectAndCompute(image, None)

        kps = np.float32([kp.pt for kp in kps])

        return (kps, features)

    def matchKeypoints(self, kps1, kps2, features1, features2,
                       ratio, reprojThresh):

        matcher = cv2.DescriptorMatcher_create("BruteForce")
        rawMatches = matcher.knnMatch(features1, features2, 2)
        matches = []

        for m in rawMatches:
            if len(m) == 2 and m[0].distance < m[1].distance * ratio:
                matches.append((m[0].trainIdx, m[0].queryIdx))

        if len(matches) > 4:
            pts1 = np.float32([kps1[i] for (_, i) in matches])
            pts2 = np.float32([kps2[i] for (i, _) in matches])

            (H, status) = cv2.findHomography(pts1, pts2, cv2.RANSAC,
                                             reprojThresh)

            return (matches, H, status)

        return None

#image1 = cv2.imread("C:/Users/AlexGP/Desktop/hotel-01.png")
#image2 = cv2.imread("C:/Users/AlexGP/Desktop/hotel-00.png")

image1 = cv2.imread("C:/Users/AlexGP/Desktop/Boat/boat1.jpg")
image2 = cv2.imread("C:/Users/AlexGP/Desktop/Boat/boat2.jpg")
image1 = imutils.resize(image1, width=600)
image2 = imutils.resize(image2, width=600)

stitcher = Stitcher()
result = stitcher.stitch([image1, image2], showMatches=True)

cv2.imshow("Image 1", image1)
cv2.imshow("Image 2", image2)
cv2.imshow("Panorama", result)
cv2.waitKey(0)


# --- import ---
import common
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import sys
import json
import glob

ahost = common.ahost
l = common.blueprint()
token = l[0]
bp_id = l[1]
dir_probe = './iba_probe/'

# Post Probe
def post_probe():
  files = glob.glob(dir_probe + '*')
  for i in files:
    f = open(i)
    cont = f.read()
    f.close()
    ep = 'https://' + ahost + '/api/blueprints/{0}/probes'.format(bp_id)
    resp = requests.post(ep, headers={'AUTHTOKEN':token, 'Content-Type':'application/json'}, data=cont, verify=False)
    if resp.status_code == 201: print ('Install ' + i.lstrip(dir_probe))
    else:
      print ('Failed to install ' + i.lstrip(dir_probe))
      sys.exit()

post_probe()


#!/usr/bin/env python

"""
Takes a 1-D array of many giant pulses and computes the
correlation coefficient between them.

Currently very inefficient - it may be best to only look
adjacent pairs, or perform a time cut
"""

import os
import glob
import numpy as np
import matplotlib.pyplot as plt

nfreq=1024 # Number of frequency bins, currently hardcoded.
files=glob.glob('*npy')

n=len(files)
t=np.zeros(n)
dt=(np.zeros(n**2))
GPs=np.zeros((n,nfreq))
corr=np.zeros(n**2)

i=0
for f in files:
    x=np.load(f)
    GP = x
    GPs[i]=GP/np.std(GP)
    t[i]=float(f.split('GP')[-1].split('.npy')[0])
    i+=1

for j in xrange(n):
    GP_temp=GPs[j]
    GP_temp=GP_temp[np.newaxis,:]
    norm = np.sqrt(np.sum(GP_temp*GP_temp) * (GPs*GPs).sum(1))
    corr[j*n:(j+1)*n] = (GP_temp*GPs).sum(1) / norm
    dt[j*n:(j+1)*n] = t-t[j]

dt=dt[dt>1E-4]
corr=corr[dt>1E-4]

#plt.plot(np.log10(dt),corr,'.')
plt.plot(GP[3])
plt.plot(GP[16])
plt.show()


# coding:utf-8
from __future__ import unicode_literals
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np
import time
from annoy import AnnoyIndex

from src.data_generator.data_loader import BaseBatch
from src.data_generator.vocabulary import Tokenizer
from src.utils import utils

def init_helper(hparam, question, answer):
    utils.raise_inexistence(question)
    utils.raise_inexistence(answer)
    return dict(
        input_x=utils.read_lines(question),
        input_y=utils.read_lines(answer),
        x_max_len=hparam.x_max_len,
        y_max_len=hparam.y_max_len)

def vector(hparam, model, sess):
    tokenizer = Tokenizer(hparam.vocab_file, segment=hparam.segment)
    batch = BaseBatch(
        tokenizer, init_helper(hparam, hparam.train_q, hparam.train_a))
    saver = tf.train.Saver()
    saver.restore(sess, hparam.model_path)
    return vector_space(hparam, model, sess, batch)

def vector_space(hparam, model, sess, batch):
    features = {'id': 0, 'vecs': []}
    total_size = batch.data_size
    starter = time.time()
    for i in range(total_size // hparam.batch_size + (
            1 if total_size % hparam.batch_size != 0 else 0)):
        fetches, feed_dict = model.infer_step(
            batch.next_batch(hparam.batch_size, features['id']))
        features['id'] += hparam.batch_size
        features['vecs'].append(sess.run(fetches, feed_dict=feed_dict))
        if i % hparam.show_steps == 0 and i:
            speed = hparam.show_steps / (time.time() - starter)
            utils.verbose('step : {:05d} | speed: {:.5f} it/s'.format(i, speed))
            starter = time.time()
    question_vecs = np.reshape(np.array(features['vecs']), [-1, hparam.hidden])[: total_size]
    vec_dim = question_vecs.shape[-1]
    ann = AnnoyIndex(vec_dim)
    for n, ii in enumerate(question_vecs):
        ann.add_item(n, ii)
    ann.build(10)
    return ann

def process(hparam, model, sess):
    ann = vector(hparam, model, sess)
    ann.save(hparam.ann_path)
    utils.verbose('dump annoy into {}'.format(hparam.ann_path))


# Copyright 2014 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.4+ and Openssl 1.0+
#

from __future__ import print_function

import copy
import glob
import json
import mock
import os
import platform
import random
import subprocess
import sys
import tempfile
import textwrap
import zipfile

from tests.protocol.mockwiredata import *
from tests.tools import *

import azurelinuxagent.common.conf as conf
import azurelinuxagent.common.logger as logger
import azurelinuxagent.common.utils.fileutil as fileutil
import azurelinuxagent.common.version as version

from azurelinuxagent.common.utils.flexible_version import FlexibleVersion
from azurelinuxagent.common.version import *

class TestCurrentAgentName(AgentTestCase):
    def setUp(self):
        AgentTestCase.setUp(self)
        return

    @patch("os.getcwd", return_value="/default/install/directory")
    def test_extract_name_finds_installed(self, mock_cwd):
        current_agent, current_version = set_current_agent()
        self.assertEqual(AGENT_LONG_VERSION, current_agent)
        self.assertEqual(AGENT_VERSION, str(current_version))
        return

    @patch("os.getcwd")
    def test_extract_name_finds_latest_agent(self, mock_cwd):
        path = os.path.join(conf.get_lib_dir(), "{0}-{1}".format(
            AGENT_NAME,
            "1.2.3"))
        mock_cwd.return_value = path
        agent = os.path.basename(path)
        version = AGENT_NAME_PATTERN.match(agent).group(1)
        current_agent, current_version = set_current_agent()
        self.assertEqual(agent, current_agent)
        self.assertEqual(version, str(current_version))
        return

class TestGetF5Platforms(AgentTestCase):
    def test_get_f5_platform_bigip_12_1_1(self):
        version_file = textwrap.dedent("""
        Product: BIG-IP
        Version: 12.1.1
        Build: 0.0.184
        Sequence: 12.1.1.0.0.184.0
        BaseBuild: 0.0.184
        Edition: Final
        Date: Thu Aug 11 17:09:01 PDT 2016
        Built: 160811170901
        Changelist: 1874858
        JobID: 705993""")

        mo = mock.mock_open(read_data=version_file)
        with patch(open_patch(), mo):
            platform = version.get_f5_platform()
            self.assertTrue(platform[0] == 'bigip')
            self.assertTrue(platform[1] == '12.1.1')
            self.assertTrue(platform[2] == 'bigip')
            self.assertTrue(platform[3] == 'BIG-IP')

    def test_get_f5_platform_bigip_12_1_0_hf1(self):
        version_file = textwrap.dedent("""
        Product: BIG-IP
        Version: 12.1.0
        Build: 1.0.1447
        Sequence: 12.1.0.1.0.1447.0
        BaseBuild: 0.0.1434
        Edition: Hotfix HF1
        Date: Wed Jun  8 13:41:59 PDT 2016
        Built: 160608134159
        Changelist: 1773831
        JobID: 673467""")

        mo = mock.mock_open(read_data=version_file)
        with patch(open_patch(), mo):
            platform = version.get_f5_platform()
            self.assertTrue(platform[0] == 'bigip')
            self.assertTrue(platform[1] == '12.1.0')
            self.assertTrue(platform[2] == 'bigip')
            self.assertTrue(platform[3] == 'BIG-IP')

    def test_get_f5_platform_bigip_12_0_0(self):
        version_file = textwrap.dedent("""
        Product: BIG-IP
        Version: 12.0.0
        Build: 0.0.606
        Sequence: 12.0.0.0.0.606.0
        BaseBuild: 0.0.606
        Edition: Final
        Date: Fri Aug 21 13:29:22 PDT 2015
        Built: 150821132922
        Changelist: 1486072
        JobID: 536212""")

        mo = mock.mock_open(read_data=version_file)
        with patch(open_patch(), mo):
            platform = version.get_f5_platform()
            self.assertTrue(platform[0] == 'bigip')
            self.assertTrue(platform[1] == '12.0.0')
            self.assertTrue(platform[2] == 'bigip')
            self.assertTrue(platform[3] == 'BIG-IP')


from flask import Flask, render_template
from flask import request

app = Flask(__name__)
locations = []

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

@app.route('/mapview', methods = ["GET","POST"])
def mapview():
    if request.method == "GET":
        return (render_template("mapview.html"))
    else:
        userLocation = (request.get_json(force= True)["ip"],
            request.get_json(force= True)["latitude"],
            request.get_json(force= True)["longitude"])
        locations.append(userLocation)
        print(locations)

@app.route('/result')
def result():
    return """

"""

if __name__ == '__main__':
    app.run(host='0.0.0.0')


import numpy as np
import itertools
import statistics
import matplotlib.pyplot as plt

def plot_ellsworth_figs(data):

    emotions = ["sadness", "joy", "fear", "anger", "challenge", "boredom", "frustration"]
    vars = ["pleasantness", "attention", "control",
                          "certainty", "anticipated_effort", "responsibility"]

    em_plot = {
        "pleasantness":{x:[] for x in emotions},
        "attention":{x:[] for x in emotions},
        "control":{x:[] for x in emotions},
        "certainty":{x:[] for x in emotions},
        "anticipated_effort":{x:[] for x in emotions},
        "responsibility":{x:[] for x in emotions}
    }

    for x in data:
        for turn in ["turn1", "turn2", "turn3"]:
            emotion = x[turn]["emotion"]
            for var in vars:
                em_plot[var][emotion].append(x[turn]["appraisals"][var])

    for var in em_plot:
        for em in em_plot[var]:
            em_plot[var][em] = statistics.mode(em_plot[var][em])

    fig, ax = plt.subplots()
    ax.scatter(list(em_plot["pleasantness"].values()), list(em_plot["anticipated_effort"].values()))
    ax.set_xlabel("Pleasantness")
    ax.set_ylabel("Effort")
    for i, txt in enumerate(emotions):
        ax.annotate(txt, ( list(em_plot["pleasantness"].values())[i], list(em_plot["anticipated_effort"].values())[i]))
    plt.savefig("results/figs/(mode)effort_vs_pleasantness.png")

    fig, ax = plt.subplots()
    ax.scatter(list(em_plot["responsibility"].values()), list(em_plot["control"].values()))
    ax.set_xlabel("Responsibility")
    ax.set_ylabel("Control")
    for i, txt in enumerate(emotions):
        ax.annotate(txt, ( list(em_plot["responsibility"].values())[i], list(em_plot["control"].values())[i]))
    plt.savefig("results/figs/(mode)control_vs_res.png")

    fig, ax = plt.subplots()
    ax.scatter(list(em_plot["attention"].values()), list(em_plot["certainty"].values()))
    ax.set_xlabel("Attention")
    ax.set_ylabel("Certainty")
    for i, txt in enumerate(emotions):
        ax.annotate(txt, ( list(em_plot["attention"].values())[i], list(em_plot["certainty"].values())[i]))
    plt.savefig("results/figs/(mode)certainty_vs_attention.png")

def plot_confusion_matrix(cm, classes, title, normalize=False, cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.

    Courtesy of scikit-learn
    """
    if normalize:
        cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]

    plt.imshow(cm, interpolation="nearest", cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = ".2f" if normalize else "d"
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                    horizontalalignment="center",
                    color="white" if cm[i, j] > thresh else "black")

    plt.ylabel("True label")
    plt.xlabel("Predicted label")
    plt.tight_layout()
    plt.savefig("results/confusion_matrices/" + title + ".png")
    plt.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-08-03 20:07:19
# @Author  : LZR (sharp_l@163.com)
# @Link    : ${link}
# @Version : $Id$

import matplotlib.pyplot as plt
import numpy as np
from numpy import linalg as LA

C = 200  # 样本数
x = np.arange(C)
s1 = 2*np.sin(0.02*np.pi * x)  # 正弦信号

a = np.linspace(-2, 2, 25)
s2 = np.array([a, a, a, a, a, a, a, a]).reshape(200,)  # 锯齿信号
s3 = np.array(20*(5*[2]+5*[-2]))  # 方波信号
s4 = 4*(np.random.random([1, C])-0.5).reshape(200,)  # 随机信号

# drow origin signal
ax1 = plt.subplot(411)
ax2 = plt.subplot(412)
ax3 = plt.subplot(413)
ax4 = plt.subplot(414)
ax1.plot(x, s1)
ax1.set_title("origin signal")
ax2.plot(x, s2)
ax3.plot(x, s3)
ax4.plot(x, s4)
plt.show()

s = np.array([s1, s2, s3, s4])  # 合成信号
ran = 2*np.random.random([4, 4])  # 随机矩阵
mix = ran.dot(s)  # 混合信号
# drow mix signal
ax1 = plt.subplot(411)
ax2 = plt.subplot(412)
ax3 = plt.subplot(413)
ax4 = plt.subplot(414)
ax1.plot(x, mix.T[:, 0])
ax1.set_title("mix signal")
ax2.plot(x, mix.T[:, 1])
ax3.plot(x, mix.T[:, 2])
ax4.plot(x, mix.T[:, 3])
plt.show()

# mix=np.array([[1.1,2,3,4,1],
# [5,6,2,8,7],
#  [6,2,5,1,3],
# [7,8,3,3,2]])

Maxcount = 10000  # %最大迭代次数
Critical = 0.00001  # %判断是否收敛
R, C = mix.shape

average = np.mean(mix, axis=1)  # 计算行均值，axis=0，计算每一列的均值

for i in range(R):
    mix[i, :] = mix[i, :] - average[i]  # 数据标准化，均值为零
Cx = np.cov(mix)
value, eigvector = np.linalg.eig(Cx)  # 计算协方差阵的特征值
val = value**(-1/2)*np.eye(R, dtype=float)
White = np.dot(val, eigvector.T)  # 白化矩阵

Z = np.dot(White, mix)  # 混合矩阵的主成分Z，Z为正交阵

# W = np.random.random((R,R))# 4x4
W = 0.5*np.ones([4, 4])  # 初始化权重矩阵

for n in range(R):
    count = 0
    WP = W[:, n].reshape(R, 1)  # 初始化
    LastWP = np.zeros(R).reshape(R, 1)  # 列向量;LastWP=zeros(m,1);
    while LA.norm(WP-LastWP, 1) > Critical:
        #print(count," loop :",LA.norm(WP-LastWP,1))
        count = count+1
        LastWP = np.copy(WP)  # %上次迭代的值
        gx = np.tanh(LastWP.T.dot(Z))  # 行向量

        for i in range(R):
            tm1 = np.mean(Z[i, :]*gx)
            tm2 = np.mean(1-gx**2)*LastWP[i]  # 收敛快
            # tm2=np.mean(gx)*LastWP[i]     #收敛慢
            WP[i] = tm1 - tm2
        #print(" wp :", WP.T )
        WPP = np.zeros(R)  # 一维0向量
        for j in range(n):
            WPP = WPP + WP.T.dot(W[:, j]) * W[:, j]
        WP.shape = 1, R
        WP = WP-WPP
        WP.shape = R, 1
        WP = WP/(LA.norm(WP))
        if(count == Maxcount):
            print("reach Maxcount，exit loop", LA.norm(WP-LastWP, 1))
            break
    print("loop count:", count)
    W[:, n] = WP.reshape(R,)
SZ = W.T.dot(Z)

# plot extract signal
x = np.arange(0, C)
ax1 = plt.subplot(411)
ax2 = plt.subplot(412)
ax3 = plt.subplot(413)
ax4 = plt.subplot(414)
ax1.plot(x, SZ.T[:, 0])
ax1.set_title("extract signal")
ax2.plot(x, SZ.T[:, 1])
ax3.plot(x, SZ.T[:, 2])
ax4.plot(x, SZ.T[:, 3])
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

import glob
import skimage.io
import skimage.measure
import skimage.filters

def bar_plot (data, n_slices, dx = 1, dy = 1, z_max = 1, x_label = 'x',
              y_label='y', z_label='z', elev_angle = 30, azim_angle = 115):
    """
    Makes a 3d bar plot of the data given as a 2d numpy array.

    Parameters
    ----------
    data: 2d-array
        Two-dimensional numpy array of z-values
    n_slices: int
        Number of 'slices' in y-directions to be used in the 3D plot
    dx: float
        Distance between neighboring x-positions
    dy: float
        Distance between neighboring y-positions
    x_label: str
        Label of the x-axis
    y_label: str
        Label of the y-axis
    z_lable: str
        Label of the z-axis
    elev_angle: int
        Alevation viewing angle
    azim_angle: int
        Azimuthal viewing angle
    z_max: float
        Default limit to the z-axis

    Returns
    -------
    fig: pyplot figure object
        Figure of the 3d-plot

    ax: pyplot axes object
        Axes object that contains the figure elements
    """

    # Initialize the figure object
    fig = plt.figure(figsize = [10, 8])
    ax = fig.add_subplot(111, projection='3d')

    # Colors to indicate variation in y-axis
    colors = sns.color_palette('YlGnBu_r', n_colors=n_slices+1)

    # Dimensions of the 2d-array
    x_length, y_length = data.shape

    # Initial index of the slice
    i_slice = 0

    # Iterate through each slice and add bar plots
    for y in np.arange(0, y_length, y_length//n_slices):

        # x-, y- and z-positions
        x_pos = np.arange(x_length)*dx
        y_pos = y*np.ones(x_length)*dy
        z_pos = np.zeros(x_length)

        # Horizontal dimensions of the bars
        delta_x = dx*np.ones(x_length)
        delta_y = 2*dy*np.ones(x_length)

        # Heights in the z-direction
        delta_z = data[:,y]

        ax.bar3d(x_pos, y_pos, z_pos, delta_x, delta_y, delta_z,
                 color = colors[i_slice])

        i_slice = i_slice + 1;

    # Add axis labels
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_zlabel(z_label)

    # Adjust the 3d viewing angle of the plot
    ax.view_init(elev_angle, azim_angle)

    # Set the z-limit of the plot
    z_max = np.min([z_max, np.max(data)])
    ax.set_zlim([0, z_max])

    return fig, ax

def create_mask (phase_image, gauss_radius = 50, threshold = -0.2, \
                 pixel_size = 0.16, area_low = 1, area_high = 5):

    # Normalize
    im_float = (phase_image - np.min(phase_image))/(np.max(phase_image)-np.min(phase_image))

    # Find the background
    im_bg = skimage.filters.gaussian(im_float, gauss_radius)

    # Subtract the background
    im_gauss = im_float - im_bg

    # Threshold the image
    im_thresh = im_gauss < threshold

    # Label the image
    im_label = skimage.measure.label(im_thresh)

    # Obtain the features of the objects
    props = skimage.measure.regionprops(im_label)

    # Remove small objects
    im_mask = np.zeros_like(im_label)
    for prop in props:
        area = prop.area * pixel_size**2
        if (area > area_low) and (area < area_high):
            im_mask = im_mask + (im_label == prop.label)

    # Label the mask
    im_mask_label = skimage.measure.label(im_mask)

    # Return the labeled mask
    return im_mask_label

def find_intensities (im_yfp, im_mask_label):

    # List to store the object intensity values
    intensities = []

    # Obtain the features of objects in the YFP channel
    props = skimage.measure.regionprops(im_mask_label, intensity_image=im_yfp)

    # Add the YFP intensity values
    for prop in props:
        intensities.append(prop.mean_intensity)

    return intensities

def find_intensities_all (operator, repressor):
    # Array to store intensity values from all the strains
    intensities_all = []

    # Filename structure
    file_structure_phase = 'data/lacI_titration/' + operator + '_' + repressor + '_' + 'phase*'
    file_structure_yfp = 'data/lacI_titration/' + operator + '_' + repressor + '_' + 'yfp*'

    # Name of all images for a given strain
    phase_names = glob.glob(file_structure_phase)
    yfp_names = glob.glob(file_structure_yfp)

    # Number of positions
    n_positions = len(phase_names)

    for i in range(n_positions):
        im_phase = skimage.io.imread(phase_names[i])
        im_yfp = skimage.io.imread(yfp_names[i])

        im_mask_label = create_mask(im_phase)
        intensities = find_intensities(im_yfp, im_mask_label)

        for intensity in intensities:
            intensities_all.append(intensity)

    return intensities_all