"""
Routines for preprocessing image stacks.

All functions in this module are designed to take an image stack and additional arguments as input.
"""
import numpy as np
from scipy.fft import fft2
from skimage.registration import phase_cross_correlation
from skimage.transform import AffineTransform, warp
from .image_functions import detect_changes_division, detect_changes_subtraction

def _stack_operation(stack, function, *args, **kwargs):
    """
    Perform an operation for all images of an image stack.

    This is just a wrapper for a function which can perform a procedure for one image to
    perform it for all images of a stack instead.

    Parameters
    ----------
    stack: ImageStack
        The image stack the function should be performed on
    function: function
        A function which takes ONE image as input and returns ONE image
    args:
        args are forwarded to the function.
    kwargs:
        kwargs are forwarded to the function.
    """
    for ind, img in enumerate(stack):
        stack[ind] = function(img, *args, **kwargs)
    return stack

def _rolling_stack_operation(stack, function, keep_first=False, *args, **kwargs):
    """
    Perform an rolling operation for all images of an image stack.

    :math:`I_{new} = func(I_{n-1}, I_n)`

    This is just a wrapper for a function which can perform a procedure for two subsequent images
    for a whole stack.

    Parameters
    ----------
    stack: ImageStack
        The image stack the function should be performed on
    function: function
        A function which takes TWO subsequent images as input and returns ONE image
    keep_first: bool
        If True, keep the first image of the stack.
        The function will not be performed on the first image alone!
    args:
        args are forwarded to the function.
    kwargs:
        kwargs are forwarded to the function.
    """
    img_minus1 = stack[0]
    for ind, img in enumerate(stack[1:]):
        stack[ind+1] = function(img_minus1, img, *args, **kwargs)
        img_minus1 = img
    if not keep_first:
        del stack[0]
    return stack

def region_of_interest(images, x0=0, x1=None, y0=0, y1=None):
    """
    Crop all images in a stack to the desired shape.

    This function changes the images in the stack.
    If the input images should be preserved copy the input to a separate object before!

    The coordinate system is the following: x0->x1 = width, y0->y1 = height from the top left corner of the image

    Parameters
    ----------
    images: list, ImageStack
    x0: int
    x1: int
    y0: int
    y1: int

    Returns
    -------
    out: list, ImageStack
        ImageStack or list with the cropped images
    """
    for ind, img in enumerate(images):
        images[ind] = img[y0:y1, x0:x1]
    return images

def image_shift(images):
    """
    Compute the shift of all images in a stack.

    The shift of the n+1st image relative to the n-th is computed. The commutative sum of these shifts
    is the shift relative to the 0th image in the stack.

    All input images must have the same width and height!
    Parameters
    ----------
    images: ImageStack, list

    Returns
    -------
    out: list
        [(0,0), (y1, x1), ...(yn, xn)] The shift in x and y direction relative to the first image in the stack.
    """
    n_minus_1 = fft2(images[0])
    shift = [(0, 0)]

    for img in images[1:]:
        fft_n = fft2(img)
        shift.append(phase_cross_correlation(n_minus_1, fft_n, space='fourier', upsample_factor=5)[0])
        n_minus_1 = fft_n

    return np.cumsum(shift, axis=0)

def biggest_common_sector(images):
    """
    Biggest common sector of the image stack

    This function computes the relative translation between the images with the first image in the stack as
    the reference image. Then the biggest common sector is cropped from the images. The cropping window
    moves with the relative translation of the images so that the translation is corrected.

    Warping of the images which could be a result of strain is not accounted for. If the warp cant be neglected
    do not use this method!!

    Parameters
    ----------
    images: list or ImageStack

    Returns
    -------
    out: list, ImageStack
        list or ImageStack with the corrected images.
    """
    # if all images are the same shape, no need to crop them
    shapes = np.array([img.shape for img in images])
    height, width = shapes.min(axis=0)
    if not (np.all(shapes[:, 0] == height) and np.all(shapes[:, 1] == width)):
        images = region_of_interest(images, 0, width, 0, height)

    # compute shift relative to the 0th image
    total_shift = (np.round(image_shift(images)) * -1).astype(int)

    # minimal and maximal boarders to cut after shift
    h_min, w_min = np.abs(np.min(total_shift, axis=0)).astype(int)
    h_max, w_max = np.abs(np.max(total_shift, axis=0)).astype(int)

    # cutting out the image
    for ind, (n, (t_h, t_w)) in enumerate(zip(images, total_shift)):
        images[ind] = n[t_h + h_min:height + t_h - h_max, t_w + w_min: width + t_w - w_max]

    return images

def shift_correction(images):
    """
    Shift correction of all images in a stack. This function is more precise than :func:`biggest_common_sector`
    but more time consuming. The memory footprint is the same.

    This function computes the relative translation between the images with the first image in the stack as
    the reference image. The images are translated into the coordinate system of the 0th image form the stack.

    Warping of the images which could be a result of strain is not accounted for. If the warp cant be neglected
    do not use this function!

    Parameters
    ----------
    images: list, ImageStack

    Returns
    -------
    out: list, ImageStack
        list or ImageStack with the corrected images.
    """

    shapes = np.array([img.shape for img in images])
    height, width = shapes.min(axis=0)
    if not (np.all(shapes[:, 0] == height) and np.all(shapes[:, 1] == width)):
        images = region_of_interest(images, 0, width, 0, height)

    # compute shift relative to the 0th image
    total_shift = np.round(image_shift(images)) * -1

    h_min, w_min = np.abs(np.min(total_shift, axis=0).astype(int))
    h_max, w_max = np.abs(np.max(total_shift, axis=0).astype(int))

    for ind, (img, t) in enumerate(zip(images, total_shift)):
        if not (t[0] == 0 and t[1] == 0):
            shift = AffineTransform(translation=t[::-1])
            temp = warp(img, shift, mode='constant', cval=0.5,
                        preserve_range=True)[h_min: height - h_max, w_min: width - w_max].astype(img.dtype.type)
        else:
            temp = img[h_min: height - h_max, w_min: width - w_max]
        images[ind] = temp
    return images

def change_detection_division(images, output_range=None):
    """
    Change detection for all images in an image stack.

    Change detection with image rationing is applied to an image stack.
    The new images are the result of the change between the n-th and the n-1st image.

    The first image will be deleted from the stack.

    Parameters
    ----------
    images: ImageStack, list
    output_range: tuple, optional
        The resulting images will be rescaled to the given range. E.g. (0,1).

    Returns
    -------
    out: ImageStack, list
    """
    return _rolling_stack_operation(images, detect_changes_division, output_range=output_range)

def change_detection_subtraction(images, output_range=None):
    """
    Change detection for all images in an image stack.

    Change detection with image differencing is applied to an image stack.
    The new images are the result of the change between the n-th and the n-1st image.

    The first image will be deleted from the stack.

    Parameters
    ----------
    images: ImageStack, list
    output_range: tuple, optional
        The resulting images will be rescaled to the given range. E.g. (0,1).

    Returns
    -------
    out: ImageStack, list
    """
    return _rolling_stack_operation(images, detect_changes_subtraction, output_range=output_range)

def overload_images(images):
    """
    Combines the nth image with the n-1st with logical_or.

    :math:`I_n^{new} = I_n | I_{n-1}`

    Parameters
    ----------
    images: ImageStack
        Image stack with image-dtype bool

    Returns
    -------
    out: ImageStack
    """
    if images._dtype != bool:
        raise TypeError('The stack must contain only bool images!')

    def fun(img1, img2):
        return np.logical_or(img1, img2)
    return _rolling_stack_operation(images, fun, True)


import unittest,os,sys,json

path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))))))
sys.path.append(path)
from config.config_test import Conf
from common.http_requests import HttpRequests
from common import logging_test
from common.mysql_data import Mysql_connet
from common.retry import Retry
from common.doc_value import doc_parameter
uri = '/device/getDeviceAlarms'
@Retry
class Test_Add_Task(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.url = Conf.TEST_APP_URL.value
        cls.http = HttpRequests(cls.url)
        cls.mysql = Mysql_connet('device')
    @doc_parameter(Conf.TEST_URL.value,uri)
    def test_add_task_success(self):
        '''获取设备某种报警类型下的报警信息用例：{}{}'''
        payload = {
          "alarmType": 0,
          "alarmTypes": [
            0
          ],
          "status": 0,
          "terminalNo": self.mysql.terminal_no
        }
        payload = json.dumps(payload)

        response = Test_Add_Task.http.post(
            uri, data=payload)
        self.assertEqual(200, response.status_code, '返回非200')
        self.assertEqual(str(0), str(response.json()['code']), '获取设备某种报警类型下的报警信息失败')
        logging_test.log_test()
        logging_test.logging.info(Conf.TEST_URL.value + uri + '-接口返回:' + response.text)

if __name__ == '__main__':
    unittest.main()


"""
test_vtluugwiki.py - tests for the VTLUUG wiki module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
import re
import unittest
from mock import MagicMock
from modules import vtluugwiki
from web import catch_timeout

class TestVtluugwiki(unittest.TestCase):

    def setUp(self):
        self.phenny = MagicMock()
        self.input = MagicMock()

    @catch_timeout
    def test_vtluug(self):
        self.input.groups.return_value = ['', "VT-Wireless"]
        vtluugwiki.vtluug(self.phenny, self.input)
        out = self.phenny.say.call_args[0][0]
        m = re.search(r'https://vtluug[.]org/wiki/VT-Wireless',
                out, flags=re.UNICODE)
        self.assertTrue(m)

    @catch_timeout
    def test_vtluug_invalid(self):
        term = "EAP-TLS#netcfg"
        self.input.groups.return_value = ['', term]
        vtluugwiki.vtluug(self.phenny, self.input)
        self.phenny.say.assert_called_once_with( "Can't find anything in "\
                "the VTLUUG Wiki for \"{0}\".".format(term))

    @catch_timeout
    def test_vtluug_none(self):
        term = "Ajgoajh"
        self.input.groups.return_value = ['', term]
        vtluugwiki.vtluug(self.phenny, self.input)
        self.phenny.say.assert_called_once_with( "Can't find anything in "\
                "the VTLUUG Wiki for \"{0}\".".format(term))


# -*- coding: utf-8 -*-
"""
Created on Mon Aug 28 20:46:08 2017

@author: Victor
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn import model_selection
from sklearn import linear_model
from sklearn import metrics
from sklearn import pipeline

def plot_learning_curve(estimator, X, y):
    plt.figure()
    plt.title('Learning curve')
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = model_selection .learning_curve(estimator, X, y, train_sizes=np.linspace(.1, 1.0, 5))
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g", label="Cross-validation score")

    plt.legend(loc="best")
    return plt

if __name__ == '__main__':
    # Read the training dataset
    trainDataSet = pd.read_table('train.csv', encoding='utf-8', sep=',', index_col=0, header=0)

    # Fills in missing values with average value
    trainDataSet['Embarked'].fillna('Unknwon', inplace = True)
    trainDataSet['Age'].fillna(30, inplace = True)

    # Generate dummy features from categorical features
    X = pd.get_dummies(trainDataSet[['Age','SibSp','Parch','Pclass','Sex','Embarked']], columns = ['Pclass','Sex','Embarked'])
    y = trainDataSet.Survived

    # Split the training set in training and test set
    X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3)

    #Generate a pipeline that scales and applies logistic regression
    pipe = pipeline.Pipeline(steps=[('scaler', preprocessing.StandardScaler()), ('logistic', linear_model.LogisticRegression())])

    # Find the best value for parameter C
    estimator = model_selection.GridSearchCV(pipe, dict(logistic__C=np.logspace(-4, 4, 9)))
    estimator.fit(X_train, y_train)
    print(estimator.best_params_)

    # Plot the learning curve
    plot_learning_curve(estimator.best_estimator_, X_train, y_train)

    # Estimate the prediction on the test dataset
    y_test_est = estimator.best_estimator_.predict(X_test)
    print(metrics.f1_score(y_test, y_test_est))
    print(metrics.accuracy_score(y_test, y_test_est))

    # Read the dataset to predict
    predictDataSet = pd.read_table('test.csv', encoding='utf-8', sep=',', index_col=0, header=0)
    predictDataSet['Embarked'].fillna('Unknwon', inplace = True)
    predictDataSet['Age'].fillna(30, inplace = True)

    # Generate features and predict output
    X_pred = pd.get_dummies(predictDataSet[['Age','SibSp','Parch','Pclass','Sex','Embarked']], columns = ['Pclass','Sex','Embarked'])
    X_pred = X_pred.reindex(columns = X.columns, fill_value=0)
    y_pred = estimator.best_estimator_.predict(X_pred)

    # Save output to format for kaggle submission
    np.savetxt('prediction.csv', np.column_stack((X_pred.index.values, y_pred)), delimiter=',', header = 'PassengerId,Survived', fmt = '%i', comments='')


###########################################################################################
#																						 #
# This sample shows how to evaluate object detections applying the following metrics:	 #
#  * Precision x Recall curve	   ---->	   used by VOC PASCAL 2012)				  #
#  * Average Precision (AP)		 ---->	   used by VOC PASCAL 2012)				  #
#																						 #
# Developed by: Rafael Padilla (rafael.padilla@smt.ufrj.br)							   #
#		SMT - Signal Multimedia and Telecommunications Lab							   #
#		COPPE - Universidade Federal do Rio de Janeiro								   #
#		Last modification: Oct 9th 2018												 #
###########################################################################################

import argparse
import glob
import os
import shutil
import sys

import _init_paths
from BoundingBox import BoundingBox
from BoundingBoxes import BoundingBoxes
from Evaluator import *
from utils import BBFormat

import pandas as pd
from datetime import datetime

# Validate formats
def ValidateFormats(argFormat, argName, errors):
	if argFormat == 'xywh':
		return BBFormat.XYWH
	elif argFormat == 'xyrb':
		return BBFormat.XYX2Y2
	elif argFormat is None:
		return BBFormat.XYWH  # default when nothing is passed
	else:
		errors.append(
			'argument %s: invalid value. It must be either \'xywh\' or \'xyrb\'' % argName)

# Validate mandatory args
def ValidateMandatoryArgs(arg, argName, errors):
	if arg is None:
		errors.append('argument %s: required argument' % argName)
	else:
		return True

def ValidateImageSize(arg, argName, argInformed, errors):
	errorMsg = 'argument %s: required argument if %s is relative' % (argName, argInformed)
	ret = None
	if arg is None:
		errors.append(errorMsg)
	else:
		arg = arg.replace('(', '').replace(')', '')
		args = arg.split(',')
		if len(args) != 2:
			errors.append(
				'%s. It must be in the format \'width,height\' (e.g. \'600,400\')' % errorMsg)
		else:
			if not args[0].isdigit() or not args[1].isdigit():
				errors.append(
					'%s. It must be in INTEGER the format \'width,height\' (e.g. \'600,400\')' %
					errorMsg)
			else:
				ret = (int(args[0]), int(args[1]))
	return ret

# Validate coordinate types
def ValidateCoordinatesTypes(arg, argName, errors):
	if arg == 'abs':
		return CoordinatesType.Absolute
	elif arg == 'rel':
		return CoordinatesType.Relative
	elif arg is None:
		return CoordinatesType.Absolute  # default when nothing is passed
	errors.append('argument %s: invalid value. It must be either \'rel\' or \'abs\'' % argName)

def ValidatePaths(arg, nameArg, errors):
	if arg is None:
		errors.append('argument %s: invalid directory' % nameArg)
	elif os.path.isdir(arg) is False and os.path.isdir(os.path.join(currentPath, arg)) is False:
		errors.append('argument %s: directory does not exist \'%s\'' % (nameArg, arg))
	# elif os.path.isdir(os.path.join(currentPath, arg)) is True:
	#	 arg = os.path.join(currentPath, arg)
	else:
		arg = os.path.join(currentPath, arg)
	return arg

def getBoundingBoxes(directory,
					 isGT,
					 bbFormat,
					 coordType,
					 allBoundingBoxes=None,
					 allClasses=None,
					 imgSize=(0, 0)):
	"""Read txt files containing bounding boxes (ground truth and detections)."""
	if allBoundingBoxes is None:
		allBoundingBoxes = BoundingBoxes()
	if allClasses is None:
		allClasses = []
	# Read ground truths
	os.chdir(directory)
	files = glob.glob("*.txt")
	files.sort()
	# Read GT detections from txt file
	# Each line of the files in the groundtruths folder represents a ground truth bounding box
	# (bounding boxes that a detector should detect)
	# Each value of each line is  "class_id, x, y, width, height" respectively
	# Class_id represents the class of the bounding box
	# x, y represents the most top-left coordinates of the bounding box
	# x2, y2 represents the most bottom-right coordinates of the bounding box
	for f in files:
		nameOfImage = f.replace(".txt", "")
		fh1 = open(f, "r")
		for line in fh1:
			line = line.replace("\n", "")
			if line.replace(' ', '') == '':
				continue
			splitLine = line.split(" ")
			if isGT:
				# idClass = int(splitLine[0]) #class
				idClass = (splitLine[0])  # class
				x = float(splitLine[1])
				y = float(splitLine[2])
				w = float(splitLine[3])
				h = float(splitLine[4])
				bb = BoundingBox(
					nameOfImage,
					idClass,
					x,
					y,
					w,
					h,
					coordType,
					imgSize,
					BBType.GroundTruth,
					format=bbFormat)
			else:
				# idClass = int(splitLine[0]) #class
				idClass = (splitLine[0])  # class
				confidence = float(splitLine[1])
				x = float(splitLine[2])
				y = float(splitLine[3])
				w = float(splitLine[4])
				h = float(splitLine[5])
				bb = BoundingBox(
					nameOfImage,
					idClass,
					x,
					y,
					w,
					h,
					coordType,
					imgSize,
					BBType.Detected,
					confidence,
					format=bbFormat)
			allBoundingBoxes.addBoundingBox(bb)
			if idClass not in allClasses:
				allClasses.append(idClass)
		fh1.close()
	return allBoundingBoxes, allClasses

# Get current path to set default folders
currentPath = os.path.dirname(os.path.abspath(__file__))

VERSION = '0.1 (beta)'

parser = argparse.ArgumentParser(
	prog='Object Detection Metrics - Pascal VOC',
	description='This project applies the most popular metrics used to evaluate object detection '
	'algorithms.\nThe current implemention runs the Pascal VOC metrics.\nFor further references, '
	'please check:\nhttps://github.com/rafaelpadilla/Object-Detection-Metrics',
	epilog="Developed by: Rafael Padilla (rafael.padilla@smt.ufrj.br)")
# formatter_class=RawTextHelpFormatter)
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + VERSION)
# Positional arguments
# Mandatory
parser.add_argument(
	'-gt',
	'--gtfolder',
	dest='gtFolder',
	default=os.path.join(currentPath, 'groundtruths'),
	metavar='',
	help='folder containing your ground truth bounding boxes')
parser.add_argument(
	'-det',
	'--detfolder',
	dest='detFolder',
	default=os.path.join(currentPath, 'detections'),
	metavar='',
	help='folder containing your detected bounding boxes')
parser.add_argument(
	'-sp',
	'--savePath',
	dest='savePath',
	default=os.path.join(currentPath, 'results'),
	metavar='',
	help='folder where the plots are saved')

# Optional
parser.add_argument(
	'-t',
	'--threshold',
	dest='iouThreshold',
	type=float,
	default=0.5,
	metavar='',
	help='IOU threshold. Default 0.5')
parser.add_argument(
	'-gtformat',
	dest='gtFormat',
	metavar='',
	default='xywh',
	help='format of the coordinates of the ground truth bounding boxes: '
	'(\'xywh\': <left> <top> <width> <height>)'
	' or (\'xyrb\': <left> <top> <right> <bottom>)')
parser.add_argument(
	'-detformat',
	dest='detFormat',
	metavar='',
	default='xywh',
	help='format of the coordinates of the detected bounding boxes '
	'(\'xywh\': <left> <top> <width> <height>) '
	'or (\'xyrb\': <left> <top> <right> <bottom>)')
parser.add_argument(
	'-gtcoords',
	dest='gtCoordinates',
	default='abs',
	metavar='',
	help='reference of the ground truth bounding box coordinates: absolute '
	'values (\'abs\') or relative to its image size (\'rel\')')
parser.add_argument(
	'-detcoords',
	default='abs',
	dest='detCoordinates',
	metavar='',
	help='reference of the ground truth bounding box coordinates: '
	'absolute values (\'abs\') or relative to its image size (\'rel\')')
parser.add_argument(
	'-imgsize',
	dest='imgSize',
	metavar='',
	help='image size. Required if -gtcoords or -detcoords are \'rel\'')

parser.add_argument(
	'-np',
	'--noplot',
	dest='showPlot',
	action='store_false',
	help='no plot is shown during execution')

args = parser.parse_args()

iouThreshold = args.iouThreshold

# Arguments validation
errors = []
# Validate formats
gtFormat = ValidateFormats(args.gtFormat, '-gtformat', errors)
detFormat = ValidateFormats(args.detFormat, '-detformat', errors)
# Groundtruth folder
if ValidateMandatoryArgs(args.gtFolder, '-gt/--gtfolder', errors):
	gtFolder = ValidatePaths(args.gtFolder, '-gt/--gtfolder', errors)
else:
	# errors.pop()
	gtFolder = os.path.join(currentPath, 'groundtruths')
	if os.path.isdir(gtFolder) is False:
		errors.append('folder %s not found' % gtFolder)
# Coordinates types
gtCoordType = ValidateCoordinatesTypes(args.gtCoordinates, '-gtCoordinates', errors)
detCoordType = ValidateCoordinatesTypes(args.detCoordinates, '-detCoordinates', errors)
imgSize = (0, 0)
if gtCoordType == CoordinatesType.Relative:  # Image size is required
	imgSize = ValidateImageSize(args.imgSize, '-imgsize', '-gtCoordinates', errors)
if detCoordType == CoordinatesType.Relative:  # Image size is required
	imgSize = ValidateImageSize(args.imgSize, '-imgsize', '-detCoordinates', errors)

detFolder = ValidatePaths(args.detFolder, '-det/--detfolder', errors)
savePath = ValidatePaths(args.savePath, '--savePath', errors)

# Create directory to save results
os.makedirs(savePath,exist_ok=True)
# Show plot during execution
showPlot = args.showPlot

print('gtFolder = %s' % gtFolder)

startTime = datetime.now()
# Get groundtruth boxes
allBoundingBoxes, allClasses = getBoundingBoxes(
	gtFolder, True, gtFormat, gtCoordType, imgSize=imgSize)
# Get detected boxes
allBoundingBoxes, allClasses = getBoundingBoxes(
	detFolder, False, detFormat, detCoordType, allBoundingBoxes, allClasses, imgSize=imgSize)
allClasses.sort()

evaluator = Evaluator()
acc_AP = 0
validClasses = 0

# Plot Precision x Recall curve
detections = evaluator.PlotPrecisionRecallCurve(
	allBoundingBoxes,  # Object containing all bounding boxes (ground truths and detections)
	IOUThreshold=iouThreshold,  # IOU threshold
	method=MethodAveragePrecision.EveryPointInterpolation,
	showAP=True,  # Show Average Precision in the title of the plot
	showInterpolatedPrecision=False,  # Don't plot the interpolated precision curve
	savePath=savePath,
	showGraphic=showPlot)

f = open(os.path.join(savePath, 'results_IoU_{}.txt'.format(iouThreshold)), 'w')
f.write('Object Detection Metrics\n')
f.write('https://github.com/rafaelpadilla/Object-Detection-Metrics\n\n\n')
f.write('Average Precision (AP), Precision and Recall per class:')

# each detection is a class
for metricsPerClass in detections:

	# Get metric values per each class
	cl = metricsPerClass['class']
	ap = metricsPerClass['AP']
	precision = metricsPerClass['precision']
	recall = metricsPerClass['recall']
	totalPositives = metricsPerClass['total positives']
	total_TP = metricsPerClass['total TP']
	total_FP = metricsPerClass['total FP']
	area_sq = metricsPerClass['area_sq']
	longest_side = metricsPerClass['longest_side']
	gt_counts = metricsPerClass['Count_gt_detections']
	dictionary = metricsPerClass['GT_dict']
	f.write("Class: {}, Total Positives: {}\n".format(cl,totalPositives))

	if totalPositives > 0:
		validClasses = validClasses + 1
		acc_AP = acc_AP + ap
		prec = ['%.2f' % p for p in precision]
		rec = ['%.2f' % r for r in recall]
		ap_str = "{0:.2f}%".format(ap * 100)
		# ap_str = "{0:.4f}%".format(ap * 100)
		print('AP: %s (%s)' % (ap_str, cl))
		f.write('\n\nClass: %s' % cl)
		f.write('\nAP: %s' % ap_str)
		f.write('\nPrecision: %s' % prec)
		f.write('\nRecall: %s' % rec)
		a_sq = ['%.2f' % a for a in area_sq]
		f.write('\nArea_sq: %s' % a_sq)
		l_side = ['%.1f' % l for l in longest_side]
		f.write('\n\nLongest_side: %s' % l_side)
		f.write('\nNumber of ground-truth boxes: {} should be equal to len(gt_counts)= {}\n'.format(totalPositives,len(gt_counts)))
		num_gt_not_found=sum(int(num) < 1 for num in gt_counts)+(totalPositives-len(gt_counts))
		f.write('Amount of gt boxes not found = {}\n'.format(num_gt_not_found))
		num_gt_found=sum(int(num) > 0 for num in gt_counts)
		f.write('Amount of gt boxes found = {}\n'.format(num_gt_found))
		double_boxes=sum(gt_counts)-num_gt_found
		f.write('Amount of double boxes = {}\n'.format(double_boxes))
		f.write('Amount of detections that did not have (enough) overlap with any gt box = {}\n'.format(total_FP-double_boxes))
		f.write('\nGT_counts: %s' % gt_counts)
		gt_detect=pd.DataFrame.from_dict(dictionary,orient='index').transpose()
		gt_detect.to_csv(os.path.join(savePath, 'detected_gt_analysis_{}_{}.csv'.format(iouThreshold,cl)),index=True,index_label='Index')

mAP = acc_AP / validClasses
mAP_str = "{0:.2f}%".format(mAP * 100)
print('mAP: %s' % mAP_str)
f.write('\n\n\nmAP: %s' % mAP_str)
f.close()
print(type(prec))
print(type(area_sq.tolist()))
print("gtFolder: {}\n".format(gtFolder))
print("detFolder: {}\n".format(detFolder))
print("savePath: {}\n".format(savePath))
print(datetime.now() - startTime)


from typing import Dict

import pandas as pd
import pytest
from biome.text import TrainerConfiguration, VocabularyConfiguration
from biome.text import Pipeline
from biome.text.data import DataSource

@pytest.fixture
def training_data_source(tmp_path) -> DataSource:
    data_file = tmp_path / "relations.json"
    df = pd.DataFrame(
        [
            {
                "text": "The most common audits were about waste and recycling.",
                "entities": [
                    {"start": 34, "end": 39, "label": "PN", "text": "waste"},
                    {"start": 16, "end": 22, "label": "QTY", "text": "audits"},
                ],
                "label": "Message-Topic(e1,e2)",
            },
            {
                "text": "The company fabricates plastic chairs.",
                "entities": [
                    {"start": 4, "end": 11, "label": "OBJECT", "text": "company"},
                    {"start": 31, "end": 37, "label": "SUBJECT", "text": "chairs"},
                ],
                "label": "Product-Producer(e2,e1)",
            },
        ]
    )
    df.to_json(data_file, lines=True, orient="records")

    return DataSource(
        source=str(data_file), flatten=False, lines=True, orient="records"
    )

@pytest.fixture
def pipeline_dict() -> Dict:
    pipeline_dict = {
        "name": "biome-rele",
        "features": {
            "word": {"embedding_dim": 2},
            "char": {
                "embedding_dim": 2,
                "dropout": 0.1,
                "encoder": {
                    "type": "gru",
                    "hidden_size": 2,
                },
            },
        },
        "head": {
            "type": "RelationClassification",
            "labels": ["Message-Topic(e1,e2)", "Product-Producer(e2,e1)"],
            "entities_embedder": {"num_embeddings": 12, "embedding_dim": 50},
            "feedforward": {
                "num_layers": 1,
                "hidden_dims": [4],
                "activations": ["relu"],
                "dropout": [0.1],
            },
        },
    }

    return pipeline_dict

@pytest.fixture
def trainer_dict() -> Dict:
    trainer_dict = {
        "num_epochs": 1,
        "optimizer": {"type": "adamw", "lr": 0.002},
    }

    return trainer_dict

def test_train(pipeline_dict, training_data_source, trainer_dict, tmp_path):
    pipeline = Pipeline.from_config(pipeline_dict)
    pipeline.predict(
        text="The most common audits were about waste and recycling",
        entities=[
            {"start": 34, "end": 39, "label": "OBJECT", "text": "waste"},
            {"start": 16, "end": 22, "label": "SUBJECT", "text": "audits"},
        ],
    )
    pipeline.create_vocabulary(VocabularyConfiguration(sources=[training_data_source]))

    pipeline.train(
        output=str(tmp_path / "relation_classifier"),
        trainer=TrainerConfiguration(**trainer_dict),
        training=training_data_source,
        validation=training_data_source,
    )

    pl_trained = Pipeline.from_pretrained(str(tmp_path / "relation_classifier"))
    pl_trained.predict(
        text="The most common audits were about waste and recycling",
        entities=[
            {"start": 34, "end": 39, "label": "OBJECT", "text": "waste"},
            {"start": 16, "end": 22, "label": "SUBJECT", "text": "audits"},
        ],
    )


import numpy as np
import matplotlib.pyplot as plt
import timeit
from scipy.stats import norm
from random import randint
from tabulate import tabulate
import seaborn as sns
from scipy import stats
import scipy.optimize as optimize
import pandas as pd

seed = 10
# seed = randint(0, 100000)                                                         # Create random seed
np.random.seed(seed)                                                                # Set seed

# T         = End  time
# NoOfSteps = Number of steps in the time grid
# NoOfRates = Number of rates
# insCorr   = Matrix containing instantaneous correlation
def GenerateBM(T, NoOfSteps, NoOfRates, insCorr):
    """"Generate Brownian motions."""
    dt = T / NoOfSteps                                                              # Discretization grid

    Z     = np.random.normal(0, 1, [NoOfRates, NoOfSteps])                              # Create standard normal variables
    Zanti = -Z                                                                      # Antithetic variables
    W     = np.zeros([NoOfRates, NoOfSteps+1])                                          # Initialize Brownian motion
    Wanti = np.zeros([NoOfRates, NoOfSteps+1])                                      # Initialize Brownian motion

    C = insCorr                                                                     # Obtain correlation structure for the Brownian motions
    L = np.linalg.cholesky(C)                                                       # Apply Cholesky decomposition

    Z     = L @ Z
    Zanti = L @ Zanti

    for i in range(NoOfSteps):
        # Calculate the BM for every forward rate per time step i
        W[:, i+1]     = W[:, i] + (np.power(dt, 0.5) * Z[:, i])
        Wanti[:, i+1] = Wanti[:, i] + (np.power(dt, 0.5) * Zanti[:, i])

    return W, Wanti                                                                 # Return the generated Brownian motion

# NoOfSteps = Number of steps in the time grid
# NoOfRates = Number of rates
# IV        = Given instantaneous volatilities from the market
def insVol(NoOfSteps, NoOfRates, IV):
    """Generate the instantaneous volatility matrix."""
    V = np.zeros([NoOfRates, NoOfSteps])

    tenor_steps = NoOfSteps / NoOfRates                                             # Steps in between tenor points

    for i in range(NoOfSteps):                                                      # Loop per time step
        for j in range(NoOfRates):                                                  # Loop per forward rate
            if i == 0:
                V[j,:] = IV[j]                                                      # Set every time step equal to the initialized IV
            if i >= tenor_steps*j:
                V[0:j,i:] = np.nan                                                  # If the forward rate is 'dead' reset the value to NaN

    return V                                                                        # Return the instantaneous volatility matrix

# NoOfRates = Number of rates
def insCorr(NoOfRates):
    """Generate the instantaneous correlation matrix."""
    N = NoOfRates                                                                   # N is the total number of rates
    C = np.ones((N, N))                                                             # Initialize the correlation matrix

    # Self-picked the value of 0.95
    np.fill_diagonal(C[:,1:], 0.95*np.ones(N-1))                                    # Set the value of the diagonal right from the true diagonal equal to 0.95

    for i in range(N):
        for j in range(N):
            if j - i == 1:                                                          # Make the matrix symmetric
                C[j,i] = C[i,j]
            if j - i > 1:                                                           # Fill in the rest of the matrix
                k = i
                while k < j:
                    C[i,j] = C[i,j] * C[k,k+1]
                    C[j,i] = C[i,j]
                    k = k + 1

    return C                                                                        # Return the correlation matrix

# NoOfRates      = Number of rates
# Initial_market = Initial market rates
def inFor(NoOfRates, initial_market):
    """Generate the initial forward rates."""
    L = initial_market[:NoOfRates].to_numpy()
    return L                                                                        # Return the forward rates

# NoOfSteps     = Number of time steps
# NoOfRat       = Number of rates
# T             = End time
# tau           = Difference between time steps
# V             = Instantaneous volatility matrix
# L0            = Initial forward rates
# theta         = Shift values
def Generate_BWR_Log_Eul(NoOfSteps, NoOfRat, T, tau, V, L0, theta):
    # start = timeit.default_timer()
    """Generate backward rates using the log-Euler method."""
    """You can actually see this as generating forward rates then in the next function we put the right values at the correct places"""
    # Obtain the initial values needed for the simulation
    IC = insCorr(NoOfRat)                                                            # Instantaneous correlation matrix
    BM, BManti = GenerateBM(T, NoOfSteps, NoOfRat, IC)                               # Generated Brownian motions

    L0_original = L0                                                                 # Initial negative rates
    L0_shifted  = L0 + theta                                                         # Shifted positive rates

    BWR_org        = np.zeros([NoOfRat, NoOfSteps + 1])                                     # Create matrices for the backward/forward rates
    BWR_org[:,0]   = np.transpose(L0_original)                                         # Set initial values
    BWR_shift      = np.zeros([NoOfRat, NoOfSteps + 1])                                   # Create matrices for the backward/forward rates
    BWR_shift[:,0] = np.transpose(L0_shifted)                                        # Set initial values

    BWR_org_anti        = np.zeros([NoOfRat, NoOfSteps + 1])                                # Do the same for the antithetic variables
    BWR_org_anti[:,0]   = np.transpose(L0_original)
    BWR_shift_anti      = np.zeros([NoOfRat, NoOfSteps + 1])
    BWR_shift_anti[:,0] = np.transpose(L0_shifted)

    # Calculate the constant over time C matrix. Only constant since IV constant
    fa = np.outer(V[:,0] , V[:,0]) * IC[:, :]
    fb = np.outer(V[:,0] , V[:,0]) * IC[:, :]
    Cii = np.diagonal(tau * ((fa + fb) / 2))
    C = tau * ((fa + fb) / 2)
    for time in range(NoOfSteps):
        '''Loop per time step'''
        Z     = V[:, 0] * (BM[: , time+1] - BM[: , time])                          # Note: I just use the first volatility value since it is constant over time
        Zanti = V[:, 0] * (BManti[: , time+1] - BManti[: , time])              # Note: I just use the first volatility value since it is constant over time

        drift_approx       = (tau * (BWR_org[:,time] + theta)) / (1 + tau * BWR_org[:,time]) * C            # Start drift approximation
        drift_approx_anti  = (tau * (BWR_org_anti[:,time] + theta)) / (1 + tau * BWR_org_anti[:,time]) * C  # Start drift approximation

        matrix1 = np.tril(drift_approx[:,time:],-time)                         # Help matrix
        matrix2 = np.tril(drift_approx_anti[:,time:],-time)                    # Help matrix

        X      = np.nansum(matrix1, axis=1)                                    # Drift approximation
        X_anti = np.nansum(matrix2, axis=1)                                    # Drift approximation

        BWR_shift[:,time+1]      = BWR_shift[:,time] * np.exp(X + (-0.5 * Cii) + Z)                       # Calculate the next step of the shifted lognormal rates
        BWR_org[:,time+1]        = BWR_shift[:,time+1] - theta                                            # Shift back to obtain the original rates, needed for the dynamics of the shifted rates
        BWR_shift_anti[:,time+1] = BWR_shift_anti[:,time] * np.exp(X_anti + (-0.5 * Cii) + Zanti)         # Calculate the next step of the shifted lognormal rates
        BWR_org_anti[:,time+1]   = BWR_shift_anti[:,time+1] - theta                                       # Shift back to obtain the original rates, needed for the dynamics of the shifted rates

        # Predictor-Corrector Method
        approxFRW          = 0.5 * ((BWR_org[:,time+1] + theta) + (BWR_org[:,time] + theta))
        approxFRW_org      = 0.5 * ((BWR_org[:,time+1]) + (BWR_org[:,time]))
        approxFRW_anti     = 0.5 * ((BWR_org_anti[:,time+1] + theta) + (BWR_org_anti[:,time] + theta))
        approxFRW_org_anti = 0.5 * ((BWR_org_anti[:,time+1]) + (BWR_org_anti[:,time]))

        drift_approx      = (tau * approxFRW) / (1 + tau * approxFRW_org) * C
        drift_approx_anti = (tau * approxFRW_anti) / (1 + tau * approxFRW_org_anti) * C

        matrix1 = np.tril(drift_approx[:,time:],-time)
        matrix2 = np.tril(drift_approx_anti[:,time:],-time)

        X      = np.nansum(matrix1, axis=1)
        X_anti = np.nansum(matrix2, axis=1)

        BWR_shift[:,time+1]      = BWR_shift[:,time] * np.exp(X + (-0.5 * Cii) + Z)                # Calculate the next step of the shifted lognormal rates
        BWR_org[:,time+1]        = BWR_shift[:,time+1] - theta                                     # Shift back to obtain the original rates, needed for the dynamics of the shifted rates
        BWR_shift_anti[:,time+1] = BWR_shift_anti[:,time] * np.exp(X_anti + (-0.5 * Cii) + Zanti)  # Calculate the next step of the shifted lognormal rates
        BWR_org_anti[:,time+1]   = BWR_shift_anti[:,time+1] - theta                                # Shift back to obtain the original rates, needed for the dynamics of the shifted rates

    return BWR_org, BWR_org_anti, BWR_shift, BWR_shift_anti, IC                                                                   # Return backward rates

# NoOfSteps     = Number of time steps
# NoOfRates     = Number of rates
# T             = End time
# tau           = Difference between time steps
# V             = Instantaneous volatility matrix
# L             = Initial forward rates
# eul_steps     = Number of euler discretization steps
# cap_shift     = Shifted to obtain positive rates
def Generate_Backward_Rates(NoOfSteps, NoOfRates, T, tau, V, L, eul_steps, cap_shift):
    """Generate backward-looking forward rates"""
    """Important to remember!! We have a different starting date / today date. Difference is exactly tau. Hence first backward rate is over [T_1,T_2]"""
    BWR_original, BWRanti_original, BWR_shifted, BWRanti_shifted, IC  = Generate_BWR_Log_Eul(NoOfSteps, NoOfRates, T, tau, V, L, cap_shift)                   # Obtain backward rates which were generated using the log_euler method

    # Initialize the backward rates
    BW_rate_original      = np.zeros([NoOfRates, NoOfSteps + eul_steps])
    BW_rate_anti_original = np.zeros([NoOfRates, NoOfSteps + eul_steps])

    BW_rate_shifted       = np.zeros([NoOfRates, NoOfSteps + eul_steps])
    BW_rate_anti_shifted  = np.zeros([NoOfRates, NoOfSteps + eul_steps])

    # Every rate needs it's own 'x-axis'
    x_axis = np.zeros([NoOfRates, NoOfSteps + eul_steps])

    # Set values up to time T_(j-1) to the same value as forward rates
    """For t <= T_j the backward rates are equal to the log-Euler rates this
    is the same as the generated forward rates for the LMM. So this creates the forward rates"""
    for i in range(NoOfRates):
        BW_rate_original[i, 0:i+1]      = BWR_original[i, 0:i+1]
        BW_rate_anti_original[i, 0:i+1] = BWRanti_original[i, 0:i+1]

        BW_rate_shifted[i, 0:i+1]      = BWR_shifted[i, 0:i+1]
        BW_rate_anti_shifted[i, 0:i+1] = BWRanti_shifted[i, 0:i+1]
        x_axis[i, 0:i+1]               = np.arange(0, i+1) * tau

    # Euler discretization size
    dt = tau / eul_steps                                                                  # time step
    BM_new, BManti_new = GenerateBM(tau, eul_steps, NoOfRates, IC)                      # Create new Brownian motions

    A_org = A = np.arange(NoOfRates).reshape((NoOfRates,1))                           # A is a filter which can be put over matrices obtaining the correct rows and columns we need to change
    for steps in range(eul_steps):                                                        # Loop per Euler step
        x_axis[np.arange(A.shape[0])[:,None], (A+1)] = (x_axis[np.arange(A.shape[0])[:,None], (A)] + dt)       # Next value for the x-axis
        Tj = x_axis[np.arange(A_org.shape[0])[:,None], (A_org)] + tau                                          # Tj per rate
        t = x_axis[np.arange(A.shape[0])[:,None], (A)]                                                         # t per rate, Note!! This does differ per rate due to mixed combination of methods

        # Apply gamma function
        z1 = Tj - t
        z1[z1 < 0] = 0
        gamma_rate = z1 / (Tj - (Tj-tau))
        gamma_rate[gamma_rate > 1] = 1
        X = Xanti = 0

        if steps == 0: # No need for for loop since only 1 time
            # No need for the gamma function since it will be 1! Only the case for our chosen gamma function
            # No need for IC since we have just the same of j till j so i always equal to 1.
            Y = (tau * V[:,0] * (BW_rate_original[np.arange(A.shape[0])[:,None], (A)] + cap_shift) * gamma_rate)
            Z = 1 + tau * BW_rate_original[np.arange(A.shape[0])[:,None], (A)]
            X += Y / Z

            Yanti = np.diagonal(tau * V[:,0] * (BW_rate_anti_original[np.arange(A.shape[0])[:,None], (A)] + cap_shift) * gamma_rate)
            Zanti = 1 + tau * BW_rate_anti_original[np.arange(A.shape[0])[:,None], (A)]
            Xanti += Yanti / Zanti

            # Values that are present in the summation of the formula
            X = np.diagonal(X)
            Xanti = np.diagonal(Xanti)

        BW_rate_shifted[np.arange((A+1).shape[0])[:,None], (A+1)] = np.reshape(np.diagonal(BW_rate_shifted[np.arange(A.shape[0])[:,None], (A)] + V[:,0] * gamma_rate * BW_rate_shifted[np.arange(A.shape[0])[:,None], (A)] * X * dt + \
            V[:,0] * BW_rate_shifted[np.arange(A.shape[0])[:,None], (A)] * gamma_rate * (BM_new[:,steps+1] - BM_new[:,steps])), (NoOfRates,1))                                      # Perform the Euler discretization
        BW_rate_original[np.arange((A+1).shape[0])[:,None], (A+1)] = BW_rate_shifted[np.arange(A.shape[0])[:,None], (A+1)] - cap_shift                  # Shift back the rates

        BW_rate_anti_shifted[np.arange((A+1).shape[0])[:,None], (A+1)] = np.reshape(np.diagonal(BW_rate_anti_shifted[np.arange(A.shape[0])[:,None], (A)] + V[:,0] * gamma_rate * BW_rate_anti_shifted[np.arange(A.shape[0])[:,None], (A)] * Xanti * dt + \
            V[:,0] * BW_rate_anti_shifted[np.arange(A.shape[0])[:,None], (A)] * gamma_rate * (BManti_new[:,steps+1] - BManti_new[:,steps])), (NoOfRates,1))                         # Perform the Euler discretization
        BW_rate_anti_original[np.arange((A+1).shape[0])[:,None], (A+1)] = BW_rate_anti_shifted[np.arange(A.shape[0])[:,None], (A+1)] - cap_shift       # Shift back the rates

        A = A+1

    # Define values after the accrual period
    BW_rate_original[BW_rate_original == 0] = BW_rate_anti_original[BW_rate_anti_original == 0] = np.nan
    BW_rate_shifted[BW_rate_shifted == 0]   = BW_rate_anti_shifted[BW_rate_anti_shifted == 0]   = np.nan

    return BW_rate_original, BW_rate_anti_original, BW_rate_shifted, BW_rate_anti_shifted, x_axis

# Notional   = Notional of the contract
# tau        = Difference between tenor points
# T          = Reset date of the caplet
# K          = Strike price of the caplet
# V          = Instantaneous volatility matrix
# L          = Initional forward rates
# cap_shift  = Shift given by the market
# DF         = Discount factor for period [T0, T1]
"""This is because for the log-Euler method the backward and forward rates are the same
hence we do not work with the spotrate yet which is equal to the start of the first backward rates
I just define a seperate shift for that. There are also other solutions."""
def cap_price_Black(Notional, tau, T, K, V, L, cap_shift, DF):
    """Calculate the price of a caplet using Black's formula"""

    num_of_caps = int(T/tau)                                                                    # Number of caplets we want to validate
    cap_price   = np.zeros(num_of_caps)                                                           # Save caplet prices
    for resetT in range(0, num_of_caps):                                                    # Apply Black's formula for every caplet
        helpval = (tau ** 3) / (tau ** 2)                                                       # We want the caplet price at time zero hence no maximum needed

        R = L[resetT] + cap_shift
        K_hat = K[resetT] + cap_shift
        vsqr = (resetT * tau + 1 / 3 * helpval) * V[resetT, 0] * V[resetT, 0]

        d1 = (np.log(R / K_hat) + (vsqr / 2)) / (np.sqrt(vsqr))
        d2 = d1 - (np.sqrt(vsqr))

        # P = DF[0]
        P = 1 / (1 + tau * (-0.0049754))
        for i in range(0,resetT):
            # Loop to calculate the value of P(0,T_n)
            P = P * (1 / (1 + tau * L[i]) )

        cap_price[resetT] = Notional * P * tau * (R * norm.cdf(d1) - K_hat * norm.cdf(d2) )
        if cap_price[resetT] / Notional < 10 ** -8:                                             # I take this as such a small value that we assume that the capprice is equal to zero
            cap_price[resetT] = 0

    return cap_price

# N          = Notional
# tau        = Difference between tenor points
# resetT     = Reset date of the caplet
# K          = Strike price of the caplet
# V          = Instantaneous volatility matrix of the large time step method
# L          = Initial forward rates
# NoOfSteps  = Number of steps used
# NoOfRates  = Number of rates that will be simulated
# T          = End date
# M          = Number of Monte Carlo simulations
# eul_steps  = Number of Euler discretization steps
# cap_shift  = SHift given by the market
# DF         = Discount factor for first period
def cap_price_MC(N, tau, strike, V, L, NoOfSteps, NoOfRates, T, M, eul_steps, cap_shift, DF):
    """Calculate the price of a caplet using Monte Carlo simulation"""
    cappriceBack = np.zeros([NoOfRates,M])
    A = np.arange(NoOfRates).reshape((NoOfRates,1)) + eul_steps               # Filter to get the correct rows and columns

    discount_rate = np.zeros([NoOfRates, M])                                               # Simulated ZCB
    discount_rate_anti = np.zeros([NoOfRates, M])                                               # Simulated ZCB

    K = strike + cap_shift

    for i in range(M):
        [BW_rate_original, BW_rate_anti_original, BW_rate_shifted, BW_rate_anti_shifted, x_axis] = Generate_Backward_Rates(NoOfSteps, NoOfRates, T, tau, V, L, eul_steps, cap_shift)                # Obtain backward rates using a large time step
        # Calculate payoffs and set negative values to zero (=max operator)

        X = BW_rate_shifted[np.arange(A.shape[0])[:,None], (A)]
        Xanti = BW_rate_anti_shifted[np.arange(A.shape[0])[:,None], (A)]

        payoffLS                         = (X.reshape(X.shape[0],) - K) * N * tau
        payoffLS[payoffLS < 0]           = 0
        payoffLS_anti                    = (Xanti.reshape(Xanti.shape[0],) - K) * N * tau
        payoffLS_anti[payoffLS_anti < 0] = 0

        for k in range(NoOfRates):                                                         # Loop to cover _valiple ZCB
            # Obtain the rate using Euler discretization
            pSim = pSimanti = (1 + tau * (-0.0049754))
            for j in range(0, k):
                pSim     = pSim * (1 + tau * BW_rate_original[j+1, j+1])
                pSimanti = pSimanti * (1 + tau * BW_rate_anti_original[j+1, j+1])

            discount_rate[k, i]      = pSim
            discount_rate_anti[k, i] = pSimanti

        cappriceBack[:,i] = (np.reshape(np.divide(payoffLS , np.transpose(discount_rate[:,i])),(NoOfRates,)) + np.reshape(np.divide(payoffLS_anti , np.transpose(discount_rate_anti[:,i])),(NoOfRates,))) * 0.5

    # Calculate standard errors
    se_Back  = np.sqrt(np.var(cappriceBack, axis=1, ddof=1)) / np.sqrt(M)

    return np.sum(cappriceBack, axis=1) / M, se_Back

# tau        = Difference between tenor points
# L          = Initial forward rates
# NoOfSteps  = Number of steps used
# NoOfRat    = Number of rates that will be simulated
# T          = End date
# M          = Number of Monte Carlo simulations
# eul_steps  = Number of Euler discretization steps
# shift      = Market shift
def zero_coupon_bond(tau, L_un, NoOfSteps, NoOfRat, T, M, eul_steps, IV, DF, shift):
    """Calculate the ZCB rate analytically and using Monte Carlo simulation"""
    discount_rate = np.zeros([NoOfRat, M])                                               # Simulated ZCB
    analytZCB = np.zeros(NoOfRat)                                                        # Analytical ZCB

    analytZCB[0] = 1 / (1 + tau * (-0.0049754))
    for i in range(NoOfRat-1):
        analytZCB[i+1] = analytZCB[i] * (1 / (1 + tau * L_un[i]))

    # Perform Monte Carlo Simulation
    for i in range(M):
        [BW, BW_anti, BW_shift, BW_anti_shift, x_axis] = Generate_Backward_Rates(NoOfSteps, NoOfRat, T, tau, IV, L_un, eul_steps, shift)                                 # Obtain backward rates using a large time step
        for k in range(NoOfRat):                                                         # Loop to cover _valiple ZCB
            # Obtain the rate using Euler discretization
            pSim = pSimanti = (1 + tau * (-0.0049754))                                                      # First discount rate uses spotrate
            for j in range(0, k):
                pSim = pSim * (1 + tau * BW[j, j])
                pSimanti = pSimanti * (1 + tau * BW_anti[j, j])
            discount_rate[k, i] = (1 / pSim + 1 / pSimanti) * 0.5

    # Calculate standard errors
    se_Sim = np.sqrt(np.var(discount_rate, axis=1, ddof=1)) / np.sqrt(M)

    # [Analytical rate, Rate using Euler, Rate using large steps]
    return analytZCB, np.sum(discount_rate, axis=1) / M, se_Sim

# NoOfSteps  = Number of steps used
# NoOfRates  = Number of rates that will be simulated
# T          = End date
# tau        = Difference between tenor points
# V          = Instantaneous volatility matrix of the large time step method
# L          = Initial forward rates
# eul_steps  = Number of Euler discretization steps
# cap_shift  = Shift given by the market
def density_plot(NoOfSteps, NoOfRat, T, tau, V, L, eul_steps, cap_shift):
    """This function makes a 3D plot of different paths for a rate and also density plots at different time intervals"""
    fig = plt.figure()
    ax = plt.axes(projection='3d')

    NoOfLines = 500                                                             # Number of different paths you want to plot

    # One time calculation to obtain some sizes
    BW_rate_original, BW_rate_anti_original, BW_rate_shifted, BW_rate_anti_shifted, x_axis = Generate_Backward_Rates(NoOfSteps, NoOfRat, T, tau, V, L, eul_steps, cap_shift)

    # Define the y-axis and z-axis for the plotting the rates
    zline = np.linspace(0, 0, len(x_axis[0,:]))
    yline = x_axis[NoOfRat-1,:]

    # Define limits for the x-axis and z-axis
    left_lim  = -0.015
    right_lim = 0.005
    ax.set_xlim(left_lim, right_lim)
    ax.set_zlim(0, 500)

    data = np.zeros([T,NoOfLines])                                              # Save data to make density plots

    for i in range(NoOfLines):
        # Plot different paths
        BW_rate_original, BW_rate_anti_original, BW_rate_shifted, BW_rate_anti_shifted, x_axis = Generate_Backward_Rates(NoOfSteps, NoOfRat, T, tau, V, L, eul_steps, cap_shift)
        xline = BW_rate_original[NoOfRat-1,:]

        if np.size(xline[xline>right_lim]) == 0 and i < 50: # Make sure lines don't cross the x limits
            ax.plot3D(xline, yline, zline, 'blue')

        # Obtain values for density plots
        for j in range(T):
            col = int((1/tau) * (j+1))
            if j == (T-1):
                col = -1
            data[j,i] = BW_rate_original[NoOfRat-1, col]

    for j in range(T):
        # Make density plots
        col = int((1/tau) * (j+1))
        if j == (T-1):
            col = -1
        [xdata, ydata] = get_hist_lines(data[j,:])

        # Distinguish positive and negative values
        idx_pos_1 = np.where((xdata >= 0) & (xdata < right_lim))  ; pos_x = xdata[idx_pos_1] ; pos_y = ydata[idx_pos_1]
        idx_neg_1 = np.where((xdata <= 0) & (xdata > left_lim))   ; neg_x = xdata[idx_neg_1] ; neg_y = ydata[idx_neg_1]

        xpos = x_axis[NoOfRat-1, col]

        ax.plot3D(pos_x,np.linspace(xpos,xpos,len(pos_x)),pos_y, 'red')
        ax.plot3D(neg_x,np.linspace(xpos,xpos,len(neg_x)),neg_y, color='black')

    ax.set_xlabel('rate')
    ax.set_ylabel('Time (years)')
    ax.set_zlabel('density')
    ax.set_title('R(t) paths and density plots in a negative rate environment')

    ax.view_init(35, -35)
    return

# data = Histogram data
def get_hist_lines(data):
    """This function gahters the correct data for the denisty plots"""
    plt.figure()
    xdata = sns.distplot(data).get_lines()[0].get_data()[0]
    ydata = sns.distplot(data).get_lines()[0].get_data()[1]
    plt.close()

    return xdata, ydata

def mainCalculation():
    """Run the program to generate backward rates."""
    tau         = 0.25                                                              # Difference between tenor points
    T           = 5                                                                 # Time horizon
    eul_steps   = 64                                                                # Euler discretization steps between two tenor points

    NoOfSteps   = int(T / tau)                                                      # Number of time-steps
    NoOfRat     = int(T / tau)                                                      # Number of backward rates we want to generate

    # Obtain the correct data (Initial rates, Instantaneous volatilities)
    df = pd.read_excel (r'C:\Users\hackt\Documents\Thesis Rabobank\Python\Caplets_3Months_Test.xlsx')

    help1 = df.iloc[16:,:]
    data = help1.iloc[:,[1,2,5,7]]
    data.reset_index(drop=True, inplace=True)
    data.columns = ['Date', 'CapletForward', 'DF', 'IV']

    V         = insVol(NoOfSteps, NoOfRat, data.IV)
    L         = inFor(NoOfRat, data.CapletForward)  / 100                                        # Initial rates
    cap_shift = 3 / 100
    # Obtain the forward-looking backward rates and their corresponding x-axis
    BW_rate_original, BW_rate_anti_original, BW_rate_shifted, BW_rate_anti_shifted, x_axis = Generate_Backward_Rates(NoOfSteps, NoOfRat, T, tau, V, L, eul_steps, cap_shift)

    "Plotting the rates"
    # Obtain labels and locations for the x-axis
    labels = np.zeros(NoOfRat+2) ; locs = np.zeros(NoOfRat+2)
    hv = 0
    for i in range(NoOfRat+1):
        labels[i] = hv
        locs[i] = i * tau
        hv += 0.25

    fig, ax = plt.subplots()
    for i in range(NoOfRat):
        ax.plot(x_axis[i,:], BW_rate_shifted[i,:])

    plt.title('Simulated shifted backward-looking forward rates')
    plt.ylabel('Rate (%)')
    plt.xlabel('Time (years)')
    plt.xticks(locs,labels, rotation='45')
    plt.grid()

    "Validating caplet prices"
    # Notional = 10000                                                                 # Notional
    # K = L                                                                          # Strike price
    # M = 50000                                                                        # Number of Monte Carlo simulations

    # [capLS, se_LS] = cap_price_MC(Notional, tau, K, V, L, NoOfSteps, NoOfRat, T, M, eul_steps, cap_shift, data.DF)         # Obtain the simulated caplet prices
    # capBlack       = cap_price_Black(Notional, tau, T, K, V, L, cap_shift, data.DF)

    # # Generate nice ouput
    # colname = list()
    # for i in range(NoOfRat):
    #     name = '{}{}'.format('Cap', '(T_{' + str(i) + '},' + 'T_{' + str(i+1) + '})')
    #     colname.append(name)

    # table = zip(colname, capBlack, capLS, 100 * abs(capLS - capBlack) /  capBlack, se_LS)
    # header = ['Analytical price', 'Simulated price', 'Error (%)', 'Standard\n error']

    # print(tabulate(table, headers = header, tablefmt="fancy_grid"))

    # # Generate the plots
    # M     = [500, 1000, 5000, 10000, 50000]
    # capLS = np.zeros([len(M), NoOfRat])
    # se_LS = np.zeros([len(M), NoOfRat])
    # rel   = np.zeros([len(M), NoOfRat])

    # labels = [] ; locs = np.zeros(NoOfRat+1) ; locsLS = np.zeros(NoOfRat+1)
    # for i in range(NoOfRat):
    #     labels.append(r'$T_{%s}$' %(i+1))
    #     locs[i] = i * eul_steps
    #     locsLS[i] = i

    # # Plot the different forward rates
    # plt.figure()
    # for i in range(len(M)):
    #     [prices, se]      = cap_price_MC(Notional, tau, K, V, L, NoOfSteps, NoOfRat, T, M[i], eul_steps, cap_shift, data.DF)         # Obtain the simulated caplet prices
    #     capLS[i, :]       = prices
    #     se_LS[i, :]       = se
    #     rel[i, :]         = 100 * abs(prices - capBlack) / prices

    # for i in range(len(M)):
    #     plt.plot(capLS[i, :], label='%d'%M[i])

    # plt.plot(capBlack, label='Blacks price')
    # plt.title('Simulated caplet prices')
    # plt.ylabel('Caplet price (bps)')
    # plt.xlabel('Reset time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # plt.figure()
    # for i in range(len(M)):
    #     plt.plot(se_LS[i, :], label='%d'%M[i])

    # plt.title('Standard errors')
    # plt.ylabel('Standard error')
    # plt.xlabel('Reset time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # plt.figure()
    # for i in range(len(M)):
    #     plt.plot(rel[i, :], label='%d'%M[i])

    # plt.title('Relative errors')
    # plt.ylabel('Relative error (%)')
    # plt.xlabel('Reset time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # "Validating zero-coupon bonds"
    # M = 2000

    # [anrate, sim_rate, se_Sim] = zero_coupon_bond(tau, L, NoOfSteps, NoOfRat, T, M, eul_steps, V, data.DF, cap_shift)      # Obtain the differenct calculatd zero-coupon rates

    # colname = list()
    # for i in range(NoOfRat):
    #     name = '{}{}'.format('P', '(T_{' + str(i+1) + '})')
    #     colname.append(name)

    # table = zip(colname, anrate, sim_rate, 100 * abs(sim_rate - anrate) / anrate, se_Sim)
    # header = ['Analytical Price', 'Simulated price', 'Error (%)', 'Standard error']

    # print(tabulate(table, headers = header, tablefmt="fancy_grid"))

    # # Plot the ZCB validation
    # M     = [500, 1000, 5000, 10000, 50000]
    # capLS = np.zeros([len(M), NoOfRat])
    # se_LS = np.zeros([len(M), NoOfRat])
    # rel   = np.zeros([len(M), NoOfRat])

    # labels = [] ; locs = np.zeros(NoOfRat) ; locsLS = np.zeros(NoOfRat)
    # for i in range(NoOfRat):
    #     labels.append(r'$T_{%s}$' %(i+1))
    #     locs[i] = i * tau
    #     locsLS[i] = i

    # plt.figure()
    # for i in range(len(M)):
    #     [anrate, sim_rate, se_Sim] = zero_coupon_bond(tau, L, NoOfSteps, NoOfRat, T, M[i], eul_steps, V, data.DF, cap_shift)      # Obtain the differenct calculatd zero-coupon rates

    #     capLS[i, :]       = sim_rate
    #     se_LS[i, :]       = se_Sim
    #     rel[i, :]         = 100 * abs(sim_rate - anrate) / sim_rate

    # for i in range(len(M)):
    #     plt.plot(capLS[i, :], label='%d'%M[i])

    # plt.plot(anrate, label='Market price')
    # plt.title('Simulated ZCB prices')
    # plt.ylabel('ZCB value')
    # plt.xlabel('Maturity time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # plt.figure()
    # for i in range(len(M)):
    #     plt.plot(se_LS[i, :], label='%d'%M[i])

    # plt.title('Standard errors')
    # plt.ylabel('Standard error')
    # plt.xlabel('Maturity time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # plt.figure()
    # for i in range(len(M)):
    #     plt.plot(rel[i, :], label='%d'%M[i])

    # plt.title('Relative errors')
    # plt.ylabel('Relative error (%)')
    # plt.xlabel('Maturity time')
    # plt.xticks(locsLS,labels, rotation='45')
    # plt.legend(title='Number of simulations')
    # plt.grid()
    # plt.show()

    # "Make density plot"
    # density_plot(NoOfSteps, NoOfRat, T, tau, V, L, eul_steps, cap_shift)

# Time the program
start = timeit.default_timer()
print('Starting the function')
mainCalculation()
stop = timeit.default_timer()
print('Time: ', stop - start, '\n')


import os

import cv2 as cv
import torch
import tqdm
from torch.multiprocessing import Pool
from tqdm import tqdm

def detect_face(data):
    from utils import get_central_face_attributes, align_face
    src_path = data['src_path']
    dst_path = data['dst_path']
    with torch.no_grad():
        has_face, bboxes, landmarks = get_central_face_attributes(src_path)
        if has_face:
            img = align_face(src_path, landmarks)
            cv.imwrite(dst_path, img)

    return True

def megaface_align(src, dst):
    image_paths = []
    for dirName, subdirList, fileList in tqdm(os.walk(src)):
        for fname in fileList:
            if fname.lower().endswith('.jpg'):
                src_path = os.path.join(dirName, fname)
                dst_path = os.path.join(dirName.replace(src, dst), fname)
                image_paths.append({'src_path': src_path, 'dst_path': dst_path})

    # print(image_paths[:20])
    num_images = len(image_paths)
    print('num_images: ' + str(num_images))

    with Pool(1) as p:
        r = list(tqdm(p.imap(detect_face, image_paths), total=num_images))

    # for image_path in tqdm(image_paths):
    #     detect_face(image_path)

    print('Completed!')

if __name__ == '__main__':
    megaface_align('megaface/MegaFace', 'megaface/MegaFace_aligned')
    megaface_align('megaface/FaceScrub', 'megaface/FaceScrub_aligned')


import numpy as np
import cv2
from keras.models import load_model

label = ['circle', 'mark', 'erase', 'X']
filename = './../image_classification(CNN)/my_model.h5'
model = load_model(filename)

# load the two input images
img1 = cv2.imread("./survey1.jpg")
img2 = cv2.imread("./survey1_checked.jpg")

if img1.shape != img2.shape:
    print('Two images shape differ')
    width = img1.shape[0]
    height = img1.shape[1]
    # The position of width and height where exactly?
    img2 = cv2.resize(img2, (height, width), interpolation=cv2.INTER_AREA)

#convert the images to gray scale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

kernel = np.ones((3, 3), np.uint8)

diff = cv2.subtract(gray1, gray2)
diff = cv2.morphologyEx(diff, cv2.MORPH_OPEN, kernel)

# diff = cv2.morphologyEx(diff, cv2.MORPH_CLOSE, kernel)
dilation = cv2.dilate(diff, kernel, iterations=3)

diff = cv2.medianBlur(dilation, 5)
_, mask = cv2.threshold(diff, 100, 255, cv2.THRESH_BINARY)  # Binary threshold
_, region = cv2.threshold(dilation, 200, 255, cv2.THRESH_BINARY_INV)

image, contours, hierachy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
image = cv2.drawContours(img1, contours, -1, (0, 0, 255), -1)

for idx, cnt in enumerate(contours):
    x, y, w, h = cv2.boundingRect(cnt)
    roi = region[y:y+h, x:x+w]
    roi = cv2.copyMakeBorder(roi, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255, 255, 255])
    extend = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
    cv2.imshow('test', extend)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    X = np.array([extend])
    X = X.reshape(1, 28, 28, 1)
    Y = model.predict(X)
    lab = np.unravel_index(np.argmax(Y), Y.shape)[1]
    print(label[lab])
    if label[lab] == "mark":
        color = (0, 255, 0)
    elif label[lab] == "circle":
        color = (0, 255, 0)
    elif label[lab] == "X":
        color = (0, 0, 255)
    else:
        color = (0, 0, 255)

    print('cnt: {} len: {}, coordinate: ({}, {}), ({}, {})'.format(idx, len(cnt), x, y, x + w, y + h))
    img3 = cv2.rectangle(img2, (x, y), (x + w, y + h), color, 2)

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 600, 600)

cv2.imshow('image', img3)

cv2.waitKey(0)
cv2.destroyAllWindows()


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

db = pymongo.Connection()
pypair = db.pypair

collection_srnt = pypair.srnt
collection_d892 = pypair.d892

filename = '/home/roland/set_alpha/12400316-2009-11-05-00001-SRNT.txt'

lines = open(filename).readlines()
entries = []
entry = {}
for line in lines:
    if line.rfind(':') > -1:
        line = line.split(':')
        entry[line[0]] = ':'.join(line[1:]).strip('\n')
    else:
        if len(entry) > 0:
            entries.append(entry)
        entry = {}

collection_srnt.insert({
    '_id': filename.replace('-SRNT.txt', ''),
    'queries': entries
    }
)


# --------------
# Importing header files
import numpy as np

# Path of the file has been stored in variable called 'path'
data = np.genfromtxt(path,delimiter=',',skip_header=1)
#print(data)
#New record
new_record=[[50,  9,  4,  1,  0,  0, 40,  0]]
census = np.concatenate((new_record,data),axis=0)
print(census.shape)
#Code starts here###

# --------------
#Code starts here
age = np.array(census[:,0])
print(age)
max_age = age.max()
min_age = age.min()
age_mean = age.mean()
age_std = np.std(age)

# --------------
#Code starr
r=len(census[:,2])
race_0=census[census[:,2]==0]
race_1=census[census[:,2]==1]
race_2=census[census[:,2]==2]
race_3=census[census[:,2]==3]
race_4=census[census[:,2]==4]

len_0=len(race_0)
len_1=len(race_1)
len_2=len(race_2)
len_3=len(race_3)
len_4=len(race_4)
print(len_0,len_1,len_2,len_3,len_4)
minority_race=3
print(minority_race)

# --------------
#Code starts here
import numpy as np
working_hours_sum=0
senior_citizens = census[census[:,0]>60]

working_hours_sum=senior_citizens.sum(axis=0)[6]

senior_citizens_len=len(senior_citizens)
avg_working_hours=working_hours_sum/senior_citizens_len
print(avg_working_hours)
print(working_hours_sum,senior_citizens_len,avg_working_hours)

# --------------
#Code starts here
high = census[census[:,1]>10]
low = census[census[:,1]<=10]
avg_pay_high=high.mean(axis=0)[7]
avg_pay_low=low.mean(axis=0)[7]


# import libs, setup data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf

# setup data holders
x = tf.placeholder(tf.float32, [None, 784])

# setup weights and biases
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

# define a relationship between y and x so that TF understands it
y = tf.nn.softmax(tf.matmul(x,W) + b)

# actual data, so that we can train using a cost function
y_ = tf.placeholder(tf.float32, [None, 10])

# cost function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# train the network
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# initialize all of the variables
init = tf.initialize_all_variables()

# launch a session
sess = tf.Session();
sess.run(init);

# actually go throuugh and train the network
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

# test our network's performance
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


import sublime
import sublime_plugin

import sys
import subprocess
import threading
import os
import time

class ExecByondBuildCommand(sublime_plugin.WindowCommand):

    encoding = 'utf-8'
    killed = False
    proc = None
    panel = None
    panel_lock = threading.Lock()

    def is_enabled(self, kill = False, **kwargs):
        return self.proc is not None

    def run(self, kill = False, byond_launch = False, **kwargs):
        if kill:
            self.kill()
            return

        vars = self.window.extract_variables()
        working_dir = vars['project_path']

        # A lock is used to ensure only one thread is touching the output panel at a time
        with self.panel_lock:
            # Creating the panel implicitly clears any previous contents
            self.panel = self.window.create_output_panel('BYOND')

            # Enable result navigation. The result_base_dir sets the
            # path to resolve relative file names against.
            settings = self.panel.settings()
            settings.set(
                'result_file_regex',
                r'^((?:.*?)\.(?:.*?)):(.*?):()(.*?)$'
            )
            settings.set('result_base_dir', working_dir)
            settings.set('syntax', 'BYOND.tmLanguage')

            self.window.run_command('show_panel', {'panel': 'output.BYOND'})

        if self.proc is not None:
            self.kill()

        self.start_time = time.time()

        # Hide the console window on Windows
        startupInfo = None
        if os.name == 'nt':
            startupInfo = subprocess.STARTUPINFO()
            startupInfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        if byond_launch:
            cmd = [self.get_setting('installation_path') + self.get_setting(byond_launch + '_executable')] + [self.get_build_file(working_dir, '.dmb')] + ['-trusted']

            if byond_launch == 'seeker':
                self.queue_write('[Running project in Dream Seeker...]')
            elif byond_launch == 'daemon':
                self.queue_write('[Running project in Dream Daemon...]')

        else:
            cmd = [self.get_setting('installation_path') + self.get_setting('compiler_executable')] + [self.get_build_file(working_dir, '.dme')]

        self.proc = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            startupinfo=startupInfo,
            cwd=working_dir,
            shell=True
        )
        self.killed = False

        threading.Thread(
            target=self.read_handle,
            args=(self.proc.stdout,)
        ).start()

    def read_handle(self, handle):
        chunk_size = 2 ** 13
        out = b''
        while True:
            try:
                data = os.read(handle.fileno(), chunk_size)
                # If exactly the requested number of bytes was
                # read, there may be more data, and the current
                # data may contain part of a multibyte char
                out += data
                if len(data) == chunk_size:
                    continue
                if data == b'' and out == b'':
                    raise IOError('EOF')
                # We pass out to a function to ensure the
                # timeout gets the value of out right now,
                # rather than a future (mutated) version
                characters = out.decode(self.encoding)
                characters = characters.replace('\r\n', '\n').replace('\r', '\n')
                self.queue_write(characters)
                if data == b'':
                    raise IOError('EOF')
                out = b''
            except (UnicodeDecodeError) as e:
                msg = 'Error decoding output using %s - %s'
                self.queue_write(msg  % (self.encoding, str(e)))
                break
            except (IOError):
                if self.killed:
                    msg = 'Cancelled'
                else:
                    elapsed = time.time() - self.start_time
                    msg = 'Finished in %.1fs' % elapsed
                    sublime.status_message('Build finished')
                self.queue_write('\n[%s]' % msg)
                break

    def queue_write(self, text):
        sublime.set_timeout(lambda: self.do_write(text), 1)

    def do_write(self, text):
        with self.panel_lock:
            self.panel.run_command('append', {'characters': text, 'scroll_to_end': True})

    def get_setting(self, config):
        settings = sublime.load_settings('Preferences.sublime-settings')

        if settings.get('byond_' + config):
            return settings.get('byond_' + config)
        else:
            settings = sublime.load_settings('BYOND.sublime-settings')
            return settings.get('byond_' + config)

    def get_build_file(self, working_dir, ext):
        for root, dirs, files in os.walk(working_dir):
            # Ignore dotfiles/dotfolders
            files = [f for f in files if not f[0] == '.']
            dirs[:] = [d for d in dirs if not d[0] == '.']
            for file in files:
                if file.lower().endswith(ext):
                    filePath = os.path.join(root, file)
                    return filePath

    def kill(self):
        if not self.killed and self.proc is not None:
            self.killed = True
            if sys.platform == "win32":
                # terminate would not kill process opened by the shell cmd.exe,
                # it will only kill cmd.exe leaving the child running
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                subprocess.Popen(
                    "taskkill /T /F /PID " + str(self.proc.pid),
                    startupinfo=startupinfo)
            else:
                self.proc.terminate()
            self.proc = None


from sources.functions import *
import time
text = {}
for f in os.listdir("."):
    if f.endswith("csv"):
        title = f.replace(".csv", "")
        text[title] = {}
        with open(f) as open_f:
            for row in csv.reader(open_f):
                ch = int(row[0].split(".")[1])
                if ch not in text[title]:
                    text[title][ch] = {}
                    text[title][ch][1] = []
                    pasuk = 1
                pasuk_re = re.search("^\[?(\d+)\. ([A-Z ]{1,}\.?)\]?", row[1])
                if pasuk_re:
                    pasuk_and_text = pasuk_re.group(0)
                    just_text = pasuk_re.group(2)
                    pasuk = int(pasuk_re.group(1))
                    row[1] = row[1].replace(pasuk_and_text, just_text)
                    if pasuk not in text[title][ch]:
                        text[title][ch][pasuk] = []
                is_dh = row[1].split(".")[0].isupper()
                if is_dh:
                    text[title][ch][pasuk].append(row[1])
                elif len(text[title][ch][pasuk]) > 0:
                    text[title][ch][pasuk][-1] += " "+row[1]
                else:
                    text[title][ch][pasuk] = [row[1]]

vsource = "https://www.nli.org.il/he/books/NNL_ALEPH001102376/NLI"
vtitle = "Ibn Ezra's commentary on the Pentateuch, tran. and annot. by H. Norman Strickman and Arthur M. Silver. Menorah Pub., 1988-2004"
ends = {"Genesis": 50, "Exodus": 40, "Deuteronomy": 34, "Numbers": 36, "Leviticus": 27}
for title in text:
    for ch in text[title]:
        text[title][ch] = convertDictToArray(text[title][ch])
    base = title.replace("Ibn Ezra on ", "")
    this_ending = ends[base]
    text[title] = convertDictToArray(text[title])
    text[title] = text[title][:this_ending]
    send_text = {
        "text": text[title],
        "language": "en",
        "versionTitle": vtitle,
        "versionSource": vsource
    }
    time.sleep(30)
    post_text(title, send_text)


import datetime
from twilio.rest import TwilioRestClient
from data import *
import sqlite3

account_sid = "" # Your Account SID from www.twilio.com/console
auth_token  = ""  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

def sendMessage(body , to) :
    message = client.messages.create(body=body,
        to= to, # Replace with your phone number
        from_="+14133845487") # Replace with your Twilio number
    print(message.sid)

def sendBaby(chat_id):
    conn = sqlite3.connect('example.db')
    c = conn.cursor()

    c.execute("SELECT * FROM subs WHERE chatid = ?" , [chat_id])
    temp  = c.fetchall()[0]
    babyweek = temp[5]
    mobile_no  = temp[1]
    for i in babydata :
        if i >= babyweek :
            write_baby( i, babyweek , mobile_no)
    sendMessage("You have registered for New Born Baby Vaccination Schedule." , '+91' + str(mobile_no))

def write_baby(week , babyweek , mobile_no):
    d = datetime.date.today()
    diff = week - babyweek
    t =datetime.timedelta(days=7*diff)
    a = t+d
    result_date = str(a.day) + '/' + str(a.month) + '/' + str(a.year)
    file = open('messages.csv' , 'a')
    file.write(result_date + ',' + str(mobile_no) + ',' + babydata[week] + '\n')
    file.close()

def sendPreg(chat_id):
    conn = sqlite3.connect('example.db')
    c = conn.cursor()

    c.execute("SELECT * FROM subs WHERE chatid = ?" , [chat_id])
    temp  = c.fetchall()[0]
    pregweek = temp[3]
    mobile_no  = temp[1]
    for i in pregdata :
        if i >= pregweek :
            write_preg( i, pregweek , mobile_no)
    sendMessage("You have registered for Pregnancy Checkup Schedule." , '+91' + str(mobile_no))

def write_preg(week , pregweek , mobile_no):
    d = datetime.date.today()
    diff = week - pregweek
    t =datetime.timedelta(days=7*diff)
    a = t+d
    result_date = str(a.day) + '/' + str(a.month) + '/' + str(a.year)
    file = open('messages.csv' , 'a')
    file.write(result_date + ',' + str(mobile_no) + ',' + pregdata[week] + '\n')
    file.close()

def write_monthly(chat_id) :
    conn = sqlite3.connect('example.db')
    c = conn.cursor()
    c.execute("SELECT * FROM subs WHERE chatid = ?" , [chat_id])
    temp  = c.fetchall()[0]
    mobile_no  = temp[1]
    d = datetime.date.today()
    for i in range(1,13):
        t = datetime.timedelta(days=30*i)
        a = t+d
        result_date = str(a.day) + '/' + str(a.month) + '/' + str(a.year)
        file = open('messages.csv' , 'a')
        file.write(result_date + ',' + str(mobile_no) + ',' + "Reminder For Due Monthly Checkup By MEDIBOT" + '\n')
        file.close()
    sendMessage("You have registered for Monthly Regular Checkup Schedule." , '+91' + str(mobile_no))


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

from __future__ import (division, absolute_import, print_function,
                        unicode_literals, annotations)

from urllib.parse import urlencode, quote_plus
import requests

def main():
    base_url = 'https://images-api.nasa.gov/search'

    search_term = 'apollo 11'
    desc = 'moon landing'
    media = 'image'
    query = {'q': search_term, 'description': desc, 'media_type': media}
    full_url = base_url + '?' + urlencode(query, quote_via=quote_plus)

    r = requests.get(full_url)
    data = r.json()
    item = data['collection']['items'][0]

    nasa_id = item['data'][0]['nasa_id']
    asset_url = 'https://images-api.nasa.gov/asset/' + nasa_id
    image_request = requests.get(asset_url)
    image_json = image_request.json()
    image_urls = [url['href'] for url in image_json['collection']['items']]
    print(image_urls)

if __name__ == '__main__':
    main()


from uuid import UUID

from pulsar.api import BadRequest, Http401

from lux.models import Schema, fields

def ensure_service_user(request, errorCls=None):
    # user must be anonymous
    if not request.cache.user.is_anonymous():
        raise (errorCls or BadRequest)
    return
    # the service user must be authenticated
    if not request.cache.user.is_authenticated():
        raise Http401('Token')

def id_or_field(field, id=None, **kwargs):
    if id:
        try:
            UUID(id)
        except ValueError:
            kwargs[field] = id
        else:
            kwargs['id'] = id
    return kwargs

class IdSchema(Schema):
    id = fields.UUID(required=True, description='unique id')


import matplotlib.pyplot as plt
import math
import random
# from TopoInfo_Savvis import *
from TopoInfo_Rnp import *

n = 100
T = 100
c = 5
s = 28

def FACP(T, n, c, s, Switches, Distances):
    x = np.ones((n, 2*c)).astype(int)
    # 坐标初始矩阵x
    O = np.ones((n, c)).astype(int)
    # 索引矩阵O
    D = np.ones((c, s)).astype(int)
    # 与现有离散的部署方案的距离
    LABEL = np.ones((n, s)).astype(int)
    # 交换机与控制器的映射关系
    delay = np.ones((n, s)).astype(int)
    for i in range(0, n):
        for j in range(0, 2 * c, 2):
            x[i, j] = random.uniform(-70, -30)
        for j in range(1, 2 * c + 1, 2):
            x[i, j] = random.uniform(-31, 5)
        # for j in range(0, 2 * c, 2):
        #     x[i, j] = random.uniform(-125, -70)
        # for j in range(1, 2 * c + 1, 2):
        #     x[i, j] = random.uniform(29, 45)
        # 随机生成100组解
        for k in range(0,2*c,2):
            Select_pos = [0 for i in range(0, s)]
            for m in range(0, s):
                controller = [x[i,k],x[i,k+1]]
                Select_pos[m] = np.linalg.norm(controller - Switches[m,:])
            O[i, int(k/2)] = int(np.argmin(Select_pos))
            # 生成离散的部署解
            o = O[i, int(k/2)]
            x[i, k] = Switches[o, 0]
            x[i, k+1] = Switches[o, 1]
            # 根据索引给出100中方案的控制器坐标

    for i in range(0, n):
        for j in range(0, c):
            D[j, :] = Distances[O[i,j],:]
        for k in range(0, s):
            LABEL[i, k] = np.argmin(D[:,k])
            delay[i ,k] = np.min(D[:,k])
    DELAY = (np.sum(delay,axis=1)/s)
    # 根据距离矩阵计算LABEL值和平均时延DELAY

    t = 1
    Des = np.ones((100,1)).astype(int)
    DesFill = np.ones((100,1)).astype(int)
    while t <= T:
        print(mmm, t)
        if (3*T/5 - t) > 0:
            Ri = 3*T/5 - t+10
        else:
            Ri = 10

        for i in range(0, n):
            B = [0 for k in range(0, n)]
            for j in range(0, n):
                B[j] = DELAY[j]*math.exp(Ri*(0.001)*np.linalg.norm(x[i,:]-x[j,:]))
            Des[i,:] = np.argmin(B)
            DesFill[i,:] = np.min(B)
        # print(Des)

        for i in range(0,n):
            rand_mov = [random.random() for j in range(0,2*c)]
            x[i,:] = (x[Des[i,:],:]-x[i,:])*(t/T)+x[i,:]+1.6*((T-0.9*t)/T)*np.array(rand_mov)

        for i in range(0, n):
            for k in range(0, 2 * c, 2):
                Select_pos = [0 for i in range(0, s)]
                for m in range(0, s):
                    controller = [x[i, k], x[i, k + 1]]
                    Select_pos[m] = np.linalg.norm(controller - Switches[m, :])
                O[i, int(k / 2)] = int(np.argmin(Select_pos))
                # 生成离散的部署解
                o = O[i, int(k / 2)]
                x[i, k] = Switches[o, 0]
                x[i, k + 1] = Switches[o, 1]
                # 根据索引给出100中方案的控制器坐标

        for i in range(0, n):
            for j in range(0, c):
                D[j, :] = Distances[O[i, j], :]
            for k in range(0, s):
                LABEL[i, k] = np.argmin(D[:, k])
                delay[i, k] = np.min(D[:, k])
        DELAY = (np.sum(delay, axis=1) / s)
        # 根据距离矩阵计算LABEL值和平均时延DELAY

        t = t+1

    # c1x=x[:,0]
    # c1y=x[:,1]
    # c2x=x[:,2]
    # c2y=x[:,3]
    # c3x=x[:,4]
    # c3y=x[:,5]
    # c4x=x[:,6]
    # c4y=x[:,7]
    # plt.scatter(c1x, c1y, c = 'red')
    # plt.scatter(c2x, c2y,c = 'blue')
    # plt.scatter(c3x, c3y,c = 'black')
    # plt.scatter(c4x, c4y,c = 'deeppink')
    # plt.show()
    # print(np.min(DELAY),x[np.argmin(DELAY),:],LABEL[np.argmin(DELAY),:])
    # 输出全局平均实验最低值,控制器坐标,以及控制器-交换机的映射关系
    # print(x)

    # LLL[mmm, :] = LABEL[np.argmin(DELAY),:]
    # for i in range(0, s):
    #     if LLL[mmm, i] == 0:
    #         PPP[mmm, 0] = PPP[mmm, 0] + 1
    #     elif LLL[mmm, i] == 1:
    #         PPP[mmm, 1] = PPP[mmm, 1] + 1
    #     elif LLL[mmm, i] == 2:
    #         PPP[mmm, 2] = PPP[mmm, 2] + 1
    #     else:
    #         PPP[mmm, 3] = PPP[mmm, 3] + 1

    DDD[mmm, :] = np.min(DELAY)/200
    for i in range(0, n):
        print(DELAY[i]/200)

if __name__ == '__main__':

    # LLL = np.ones((5, s))
    # PPP = np.zeros((5, c))
    DDD = np.ones((1, 1))
    for mmm in range(0,1):
        FACP(T,n,c,s,Switches,Distances)
    print(DDD.mean())


# # Exercise 1.

# # Write a function that takes a list of numbers and iterates through each number on a list and counts all numbers that are equal to 1.

# a_list = [1,3,55,1,9,4,56,2,1,8]

a_list = [1,3,55,1,9,4,56,2,1,8]
x = 0
for a in a_list:
  if a == 1:
    x= x+1
print("You have {} numbers eqaul to 1" .format(x))


import asyncio

class LogicalPlanner:

    def __init__(self, operation, planning_svc, stopping_conditions=()):
        self.operation = operation
        self.planning_svc = planning_svc
        self.stopping_conditions = stopping_conditions
        self.stopping_condition_met = False
        self.state_machine = ['initial_access', 'defense_evasion', 'command_and_control', 'discovery', 'execution',
                              'credential_access', 'privilege_escalation', 'persistence', 'lateral_movement',
                              'collection', 'exfiltration', 'impact']
        self.next_bucket = 'initial_access'   # set first, bucket to execute
        self.current_length = 0

    async def execute(self):
        await self.planning_svc.execute_planner(self)

    async def do_bucket(self, bucket):
        await self.planning_svc.exhaust_bucket(self, bucket, self.operation)

    async def initial_access(self):
        await self.do_bucket('initial-access')
        self.next_bucket = await self.planning_svc.default_next_bucket('initial_access', self.state_machine)

    async def execution(self):
        await self.do_bucket('execution')
        self.next_bucket = await self.planning_svc.default_next_bucket('execution', self.state_machine)

    async def persistence(self):
        await self.do_bucket('persistence')
        self.next_bucket = await self.planning_svc.default_next_bucket('persistence', self.state_machine)

    async def privilege_escalation(self):
        await self.do_bucket('privilege-escalation')
        self.next_bucket = await self.planning_svc.default_next_bucket('privilege_escalation', self.state_machine)

    async def defense_evasion(self):
        await self.do_bucket('defense-evasion')
        self.next_bucket = await self.planning_svc.default_next_bucket('defense_evasion', self.state_machine)

    async def credential_access(self):
        await self.do_bucket('credential-access')
        self.next_bucket = await self.planning_svc.default_next_bucket('credential_access', self.state_machine)

    async def discovery(self):
        await self.do_bucket('discovery')
        self.next_bucket = await self.planning_svc.default_next_bucket('discovery', self.state_machine)

    async def lateral_movement(self):
        await self.do_bucket('lateral-movement')
        self.next_bucket = await self.planning_svc.default_next_bucket('lateral_movement', self.state_machine)

    async def collection(self):
        await self.do_bucket('collection')
        self.next_bucket = await self.planning_svc.default_next_bucket('collection', self.state_machine)

    async def command_and_control(self):
        await self.do_bucket('command-and-control')
        self.next_bucket = await self.planning_svc.default_next_bucket('command_and_control', self.state_machine)

    async def exfiltration(self):
        await self.do_bucket('exfiltration')
        self.next_bucket = await self.planning_svc.default_next_bucket('exfiltration', self.state_machine)

    async def impact(self):
        await self.do_bucket('impact')
        if len(self.operation.chain) == self.current_length:  # check to see if we've done anything new recently
            if self.operation.auto_close:  # we aren't making any further forward progress, and we should close
                self.next_bucket = None
            else:  # we aren't making any further forward progress, but let's wait a bit and see if that changes
                await asyncio.sleep(180)  # Sleep for a while before we enter the flow loop again
                self.planning_svc.log.debug('[buckets] Ran out of things to do for the moment. Sleeping for a bit.')
                self.next_bucket = 'initial_access'
        else:
            self.current_length = len(self.operation.chain)
            self.next_bucket = await self.planning_svc.default_next_bucket('impact', self.state_machine)


#!/usr/bin/python3.6
from flask import Flask, request, render_template, render_template_string
import requests

app = Flask(__name__)

def lfi_search(ip,port,param):
	file = open('lfi.txt','r')
	res = []
	for x in file:
		payload = str('http://'+ip+':'+port+'?'+param+'='+x).strip('\n')
		r = requests.get(payload)
		res.append(r.text)
	return(res)

@app.route("/")
def index():
	return render_template_string('Hello World!')

@app.route("/gun", methods=['GET', 'POST'])
def lfi():
	if request.method == 'POST':
		ip = str(request.form['ip'])
		port = str(request.form['port'])
		param = str(request.form['param'])
		result = lfi_search(ip,port,param)
		return render_template('gun.html', ip=ip,port=port, param=param,result=result)
	else:
		return render_template('gun.html')

@app.route("/hehehe")
def hehehe():
	return render_template_string('lalka')

if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8123,debug=True)


# -*- coding: utf-8 -*-
# index_page.models

from google.appengine.ext import db

class FeedBack(db.Model):
    question = db.TextProperty(u'Ваш вопрос, отзыв или пожелание')
    email = db.EmailProperty(u'Ваш email')
    telephone = db.StringProperty(
        u'или Ваш телефон (не забудьте указать код города)')


# Faça um Programa que peça 2 números inteiros e um número real. Calcule e mostre:
# o produto do dobro do primeiro com metade do segundo .
# a soma do triplo do primeiro com o terceiro.
# o terceiro elevado ao cubo.

int1 = int(input('Digite o 1º inteiro:  '))
int2 = int(input('Digite o 2º inteiro:  '))
float1 = float(input('Digite um decimal:  '))

resp1 = (int1 * 2) * (int2 / 2)
resp2 = (int1 * 3) + float1
resp3 = (float1 ** 3)

print(f'O produto do dobro do primeiro com a metade do segundo: {resp1}')
print(f'A soma do triplo do primeiro com o terceiro: {resp2}')
print(f'O terceiro elevado ao cubo: {resp3}')


import datetime
from back_test.model.base_account import BaseAccount
from back_test.model.base_future_coutinuous import BaseFutureCoutinuous
from back_test.model.base_instrument import BaseInstrument
from back_test.model.base_option_set import BaseOptionSet
from back_test.model.constant import Util, FrequentType, LongShort
import data_access.get_data as data

start_date = datetime.date(2017, 6, 1)
end_date = datetime.date(2017, 7, 1)
df_option_metrics = data.get_50option_mktdata(start_date, end_date)
df_index_metrics = data.get_index_mktdata(start_date, end_date, 'index_50etf')
df_cf = data.get_mktdata_future_c1_daily(start_date, end_date, 'ih')

optionset = BaseOptionSet(df_option_metrics)
optionset.init()
account = BaseAccount(Util.MILLION,rf=0)

def _next():
    optionset.next()

def value_equal(df,portfolio_value,cash,portfolio_margin_capital):
    if abs(df['portfolio_value'] - portfolio_value)>0.001:
        return False
    elif abs(df['portfolio_margin_capital'] - portfolio_margin_capital)>0.001:
        return False
    elif abs(df['cash'] - cash) > 0.001:
        return False
    else:
        return True

print('Option Trading Account Test')
maturity = optionset.select_maturity_date(nbr_maturity=0, min_holding=20)
list_atm_call, list_atm_put = optionset.get_options_list_by_moneyness_mthd1(
    moneyness_rank=0,
    maturity=maturity)
atm_call = optionset.select_higher_volume(list_atm_call)
atm_put = optionset.select_higher_volume(list_atm_put)

print('期权卖方开仓')
order = account.create_trade_order(atm_call, LongShort.SHORT, 10)
record = atm_call.execute_order(order)
account.add_record(record, atm_call)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],999950,971070,31440))
print('-'*50)

_next()

print('加仓')
order = account.create_trade_order(atm_call, LongShort.SHORT, 2)
record = atm_call.execute_order(order)
account.add_record(record, atm_call)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1000660.0,968692.0,34176.00000000002))
print('-'*50)
_next()

print('减仓')
order = account.create_trade_order(atm_call, LongShort.LONG, 2)
record = atm_call.execute_order(order)
account.add_record(record, atm_call)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1001766.0,978262.0,24414.00000000002))
print('-'*50)
_next()

print('平仓')
order = account.create_trade_order(atm_call, LongShort.LONG, 10)
record = atm_call.execute_order(order)
account.add_record(record, atm_call)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1001376.0,1001376.0,0.0))
print('-'*50)
_next()

account.daily_accounting(optionset.eval_date)
print('-'*50)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1001376.0,1001376.0,0.0))
print('-'*50)
_next()

print('期权买方开仓')
order = account.create_trade_order(atm_put, LongShort.LONG, 10)
record = atm_put.execute_order(order)
account.add_record(record, atm_put)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1001326.0,998286.0,0.0))
print('-'*50)
_next()
print('加仓')
order = account.create_trade_order(atm_put, LongShort.LONG, 2)
record = atm_put.execute_order(order)
account.add_record(record, atm_put)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1000916.0,997748.0,0.0))
print('-'*50)
_next()
print('减仓')

order = account.create_trade_order(atm_put, LongShort.SHORT, 8)
record = atm_put.execute_order(order)
account.add_record(record, atm_put)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1001104.0,999972.0,0.0))
print('-'*50)
_next()
print('平仓')
order = account.create_trade_order(atm_put, LongShort.SHORT, 4)
record = atm_put.execute_order(order)
account.add_record(record, atm_put)
account.daily_accounting(optionset.eval_date)
print('Test Equal: ',value_equal(account.account.loc[optionset.eval_date],1000976.0,1000976.0,0.0))
print('-'*50)


# pylint: disable=no-self-use,invalid-name
import numpy
from numpy.testing import assert_almost_equal

from allennlp.common import Params, constants
from allennlp.data import Vocabulary
from allennlp.data.dataset_readers import SnliReader
from allennlp.data.fields import TextField
from allennlp.data.token_indexers import SingleIdTokenIndexer
from allennlp.models import DecomposableAttention, Model
from allennlp.nn import InitializerApplicator
from allennlp.nn.util import arrays_to_variables
from allennlp.common.testing import AllenNlpTestCase

class TestDecomposableAttention(AllenNlpTestCase):
    def setUp(self):
        super(TestDecomposableAttention, self).setUp()

        constants.GLOVE_PATH = 'tests/fixtures/glove.6B.300d.sample.txt.gz'
        dataset = SnliReader().read('tests/fixtures/data/snli.jsonl')
        vocab = Vocabulary.from_dataset(dataset)
        self.vocab = vocab
        dataset.index_instances(vocab)
        self.dataset = dataset
        self.token_indexers = {'tokens': SingleIdTokenIndexer()}

        self.model = DecomposableAttention.from_params(self.vocab, Params({}))
        initializer = InitializerApplicator()
        initializer(self.model)

    def test_forward_pass_runs_correctly(self):
        training_arrays = arrays_to_variables(self.dataset.as_array_dict())
        _ = self.model.forward(**training_arrays)

    def test_model_can_train_save_and_load(self):
        self.ensure_model_can_train_save_and_load(self.model, self.dataset)

    def test_predict_entailment_gives_reasonable_outputs(self):
        premise = TextField(["A", "dog", "is", "a", "mammal"], token_indexers=self.token_indexers)
        hypothesis = TextField(["A", "dog", "is", "an", "animal"], token_indexers=self.token_indexers)
        output_dict = self.model.predict_entailment(premise, hypothesis)
        assert_almost_equal(numpy.sum(output_dict["label_probs"], -1), 1, decimal=6)

    def test_model_load(self):
        params = Params.from_file('tests/fixtures/decomposable_attention/experiment.json')
        model = Model.load(params)

        assert isinstance(model, DecomposableAttention)