from flask_cors import CORS
import cv2
from flask import Flask, request, render_template, send_from_directory ,jsonify
import os
import numpy as np
from keras.models import model_from_json
import time
import stat
flagg=False
app = Flask(__name__)
CORS(app)
json_file = open("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\model1.json", 'r')
file = os.stat ( "C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt" )
original_time=time.ctime ( file[ stat.ST_MTIME ] )
loaded_model_json = json_file.read()
json_file.close()
prevline=""
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\model1.h5")
loaded_model._make_predict_function()
print("Loaded model from disk")

@app.route("/modification",methods=["GET"])
def mod():
	global prevline,original_time,flagg
	dictt=dict()
	flagg=False
	fileStatsObj = os.stat ( "C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt" )
	modification_time = time.ctime ( fileStatsObj [ stat.ST_MTIME ] )
	print(modification_time,original_time)
	if(modification_time!=original_time):

		with open("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt", "r") as file:

			for line in file:
				print(line,flagg)
				line=line.strip("\n")
				if(flagg==True):
					val=line.split("|")
					dictt[val[0]]=val[1]

				if(prevline.strip("\n")==line.strip("\n")):
					flagg=True

			prevline=line
	print(dictt,prevline)
	original_time=modification_time
	return jsonify(dictt),200

@app.route("/original",methods=["GET"])
def orginal():
	global original_time ,prevline
	dictt=dict()
	fileStatsObj = os.stat ( "C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt" )
	original_time = time.ctime ( fileStatsObj [ stat.ST_MTIME ] )
	with open("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt", "r") as file:

		for line in file:
			val=line.split("|")
			dictt[val[0]]=val[1]
		prevline=line
	print(original_time,prevline)
	return jsonify(dictt),200

@app.route("/appy",methods=["POST"])
def apppy():
	sleep(60)
	return "yo"
@app.route("/rating",methods=["POST"])
def ratingg():
	print(request.data)
	#print(request)
	#for i in request.data:
	#	print(i)

	input=(request.data.decode("utf-8"))
	print(input)
	input=input.split("|")
	username=input[0]
	rating=input[1]
	def append_new_line(text_to_append):

		with open("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt", "a+") as file_object:

			file_object.seek(0)

			data = file_object.read(100)
			if len(data) > 0:
				file_object.write("\n")

			file_object.write(text_to_append)
	#username="bla"
	#file1 = open("C:\\Users\\anvitha poosarla\\Desktop\\webtech project\\myfile.txt", "a")  # append mode
	#file1.write(username+"|"+str(rating))
	#file1.close()
	append_new_line(username+"|"+str(rating))
	return username,200

@app.route("/app",methods=["POST"])
def bla():
    X=[]
    path = os.getcwd()

    print(path)
	#print("bla")
	#print(request.files)
    try:
     image = request.files["file"]
		#img = cv2.resize(image, (150,150))
    except:
     return "Please upload some image",400

    print(image)
	#print(img)
    target = "D:\\"

    # create image directory if not found

    # retrieve file from html file-picker

    upload = request.files.getlist("file")[0]

    print("File name: {}".format(upload.filename))
    filename = upload.filename

    # file support verification
    ext = os.path.splitext(filename)[1]
    if (ext == ".jpg") or (ext == ".jpg") or (ext == ".jpg"):
        print("File accepted")
    else:
        return "Please upload another file as only JPG is accepted",400
        return render_template("D:\\anvitha\\error.html", message="The selected file is not supported"), 400

    # save file
    destination = "/".join([target, filename])
    print("File saved to to:", destination)
    upload.save(destination)
    path='D:\\'+filename
    print(path)
	#path = os.path.join('D:',img)
    img = cv2.imread(path,cv2.IMREAD_COLOR)
    img = cv2.resize(img, (150,150))
    print(img)
    X.append(np.array(img))
    X=np.array(X)
    X=X/255
    print(X)
    print(len(X[0]))
    os.chdir('D:')
    print(os.getcwd())
    '''json_file = open("D:\\model1.json", 'r')

    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights("D:\\model1.h5")
    print("Loaded model from disk")
    # forward to processing page'''
    print("here")
    pred=loaded_model.predict(X)
    print("heree")
    pred_digits=np.argmax(pred,axis=1)
    print("hereee")
    print(pred_digits[0])
    #lol=dict()
    #lol["answer"]=pred_digits[0]
    return str(pred_digits[0]),200

    #return render_template("processing.html")

    return "postt"
@app.route("/app",methods=["GET"])
def helloWorld():
  return "Hello, cross-origin-world!"
app.debug = True
app.run(host='0.0.0.0',port=2166)


import matplotlib.pyplot as plt

from wattage.critical_power import CriticalPower
from wattage import conversion as cvt

def main():
    pbs = [
        # [meters, seconds]
        [1000, 4*60 + 30],  # 1K
        [1609, 7*60 + 31],  # 1mile
        [5000, 26*60 + 42],   # 5K this is probably an underestimate
    ]
    ms = [
        [s, m / s] for m, s in pbs
    ]

    critical_speed = CriticalPower(ms)

    times = list(range(4*60, 60*60 + 1, 60))
    speed = [critical_speed.estimated_power(t) for t in times]

    plt.plot(times, speed, label="CS Curve")
    plt.scatter([t for t, _ in ms], [s for _, s in ms], label="Samples")
    plt.xlabel("Time (seconds)")
    plt.ylabel("Speed (m/s)")
    plt.axhline(critical_speed._a, color="r", label="CS")
    mph_cs = (critical_speed._a  * 2.23694)
    pace_cs = 1 / mph_cs * 60.0
    print(mph_cs, pace_cs)
    plt.title(f"Critical Running Speed (Critical Pace: {pace_cs :.2f}min/mile)")
    plt.legend()
    plt.show()

if __name__ == "__main__":
    main()


from itertools import chain

WINGET_PACKAGES = (
    "git",
    "python",
    "notepad++",
    "Kitware.CMake",
    "KhronosGroup.VulkanSDK",
    "Microsoft.Powertoys",
    "Microsoft.WindowsTerminal",
    "Mozilla.Firefox",
)

VS_WORKLOADS = (
    'Microsoft.VisualStudio.Workload.NativeDesktop',
)

VS_COMPONENTS = (
    'Microsoft.VisualStudio.Component.VC.Llvm.Clang',
    'Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Llvm.Clang',
    'Component.GitHub.VisualStudio',
    'Microsoft.VisualStudio.Component.VC.14.20.ARM',
    'Microsoft.VisualStudio.Component.VC.14.20.ARM64'
)
VS_CHANNEL = "VisualStudio.16.Release"
VS_PRODUCT = "Microsoft.VisualStudio.Product.Community"
VS_INSTALLER = "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\vs_installer.exe"

VS_INSTALLER_BASE_CMD_ARGS = [
    "--channelId", VS_CHANNEL,
    "--productId", VS_PRODUCT,
    "--quiet",
    # "--installWhileDownloading",
    # "--noRestart"
]

VSCODE_EXTENSIONS = (
    'coenraads.bracket-pair-colorizer',
    'editorconfig.editorconfig',
    'kevinkyang.auto-comment-blocks',
    'marvhen.reflow-markdown',
    'ms-python.python',
    'ms-vscode-remote.remote-wsl',
    'ms-vscode.cmake-tools',
    'ms-vscode.cpptools',
    'ms-vscode.powershell',
    'twxs.cmake',
    'vscode-icons-team.vscode-icons',
    'yzhang.markdown-all-in-one',
    'ziyasal.vscode-open-in-github',
)

def makeStartProcess(cmd, args):
    """Return a powershell command to start and wait for a process with the given args."""
    ret = [
        "Start-Process",
        "-FilePath",

        '"{}"'.format(cmd),
        "-ArgumentList",
    ]
    quoted_arg_list = ','.join('"{}"'.format(x) for x in args)
    ret.append(quoted_arg_list)
    ret.append("-Wait")
    return ' '.join(ret)

def wrapPowershellForCmd(ps_command):
    """Wrap a powershell command for invocation from cmd."""
    # We're technically escaping for cmd here.
    return 'powershell -executionpolicy remotesigned -command "{}"'.format(ps_command.replace('"', r'\"'))

def getWingetCommands():
    lines = []
    for pkg in WINGET_PACKAGES:
        lines.append("winget install {}".format(pkg))
        lines.append(
            r"if %ERRORLEVEL% EQU 0 Echo {} installed successfully. ".format(pkg))
    return '\n'.join(lines)

ECHO_OFF = "@echo off\n"
WINGET_BATCH = 'winget-packages.cmd'
VSCODE_BATCH = 'vscode-extensions.cmd'
VS_PS = 'vs-update-modify.ps1'

OVERALL_BATCH = 'install-all.cmd'

if __name__ == "__main__":
    print("@echo off")

    calls = [
        ECHO_OFF,
        # Install scoop, set execution policy to remotesigned for this user.
        "call other-scripts\\enable-ps-and-install-scoop.cmd\n"
    ]

    # Install packages using winget
    print(getWingetCommands())
    with open(WINGET_BATCH, 'w') as fp:
        fp.writelines(ECHO_OFF)
        fp.writelines(getWingetCommands())
    calls.append('call ' + WINGET_BATCH)

    # Install vscode extensions
    print()
    cmd = 'code {}'.format(' '.join('--install-extension {}'.format(x)
                                    for x in VSCODE_EXTENSIONS))
    print(cmd)
    with open(VSCODE_BATCH, 'w') as fp:
        fp.writelines(ECHO_OFF)
        fp.writelines(cmd)
        fp.write('\n')
    calls.append('call ' + VSCODE_BATCH)

    with open(VS_PS, 'w') as fp:
        # Update visual studio
        print()
        fp.write('echo "Now updating Visual Studio"\n')
        args = ['update']
        args.extend(VS_INSTALLER_BASE_CMD_ARGS)
        fp.writelines(makeStartProcess(VS_INSTALLER, args))
        fp.write('\n')

        # Install additional components in visual studio
        print()
        fp.write(
            'echo "Now modifying Visual Studio to include all desired components"\n')
        args = ['modify']
        args.extend(VS_INSTALLER_BASE_CMD_ARGS)
        # The chain.from_iterable is so that we can add two items to the list
        # for each single item in the original list and thus the comprehension
        args.extend(chain.from_iterable(("--add", "{};includeRecommended".format(x))
                                        for x in VS_WORKLOADS))
        args.extend(chain.from_iterable(("--add", x) for x in VS_COMPONENTS))
        fp.writelines(makeStartProcess(VS_INSTALLER, args))
        fp.write('\n')
        fp.write('echo "Done messing with Visual Studio"\n')

    calls.append(wrapPowershellForCmd('& .\\' + VS_PS))

    # Configure git appropriately
    calls.append("call other-scripts\\do-gitconfig.cmd\n")
    with open(OVERALL_BATCH, 'w') as fp:
        fp.writelines('\n'.join(calls))


#!/usr/bin/env python3

import sys

rank = 'A23456789TJQK'
counter = {k: 0 for k in rank}

hand = sys.stdin.readline().strip().split()
for card in hand:
    counter[card[0]] += 1

print(max(counter.values()))


'''
猜字游戏
需求：
1、猜的数字是系统产生的，不是自己定义的
2、键盘输入的   操作完填入：input（“提示”）
3、判断			操作完填入：if判断条件 elif 判断条件。。。。。。Else
4、循环			操作完填入：while 条件循环
任务：你的初始资金为100 每猜一次减10  资金为0时或者猜成功游戏结束
    猜大 如果你输入的数字和随机数对比 大于随机数  打印一句话为  猜大了
    猜小 如果你输入的数字和随机数对比 小于随机数  打印一句话为  猜小了
'''
import random
num=random.randint(666,999)
capital = 100
while 1:
    c=int(capital)
    a=input("请输入一个数字")
    a=int(a)
    if a == num:
        print("你成功了！")
        break
    elif a > num:
        print("猜大了")
    elif a < num:
        print("猜小了")
    capital=c-10
    print("资金剩余",capital,"￥")
    if capital == 0:
        print("你失败了！")
        break


from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

from player.models import Podcast

# Create your views here.
def index(request):
    return HttpResponse('Provide podcast ID')

def player(request, pid):
    if not request.user.is_authenticated:
        return JsonResponse({'message': 'User unauthorised'}, status=401)

    pc = Podcast.objects.get(pid=pid)
    context = {
        'description': pc.description,
        'title': pc.title,
        'author': pc.author,
        'pid': pc.pid
    }
    return render(request, 'player.html', context)

def test(request):
    print(Podcast.objects.all())
    return HttpResponse(str(Podcast.objects.all()[0].media))


import argparse
import os.path
import random

import mlflow.keras
import numpy as np
import tensorflow as tf
import yaml
from numpy.random import seed
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split

from src.data.dataloader import load_data
from src.models.cnnbased.model import create_model
from src.models.cnnbased.preprocessing import scale, reshape, onehot_encoding

seed(1)
tf.random.set_seed(1)
tf.config.experimental.enable_op_determinism()
random.seed(2)

def eval_metrics(actual, pred):
    acc = round(accuracy_score(actual, pred, normalize=True) * 100, 2)
    precision = round(precision_score(actual, pred, average='macro') * 100, 2)
    recall = round(recall_score(actual, pred, average='macro') * 100, 2)
    f1 = round(f1_score(actual, pred, average='macro') * 100, 2)
    return acc, precision, recall, f1

def train(data_path, epochs, batch_size):
    # Read dataset:
    x_train, y_train = load_data(os.path.join(data_path, 'train.csv'))
    # Scaling:
    x_train = scale(x_train)
    # Reshape:
    x_train = reshape(x_train)
    # Encode the output:
    y_train = onehot_encoding(y_train)
    # Split dataset:
    x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.3, random_state=42)

    # Create model:
    model = create_model(x_train[0].shape)

    # Log parameters:
    loss = 'categorical_crossentropy'
    metric = 'accuracy'
    mlflow.log_param("loss", loss)
    mlflow.log_param("metric", metric)
    mlflow.log_param("epochs", epochs)
    mlflow.log_param("batch_size", batch_size)
    loss = 'categorical_crossentropy'
    metric = 'accuracy'

    # Train:
    class LogMetricsCallback(tf.keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs=None):
            mlflow.log_metric('training_loss', logs['loss'], epoch)
            mlflow.log_metric(f'training_{metric}', logs[metric], epoch)
            mlflow.log_metric('val_loss', logs['val_loss'], epoch)
            mlflow.log_metric(f'val_{metric}', logs[f'val_{metric}'], epoch)

    model.compile(loss=loss, optimizer='adam', metrics=[metric])
    history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1,
                        validation_data=(x_val, y_val), callbacks=[LogMetricsCallback()])

    # Evaluation
    x_test, y_test = load_data(os.path.join(data_path, 'test.csv'))
    # Reshape:
    x_test = reshape(x_test)
    # Scaling:
    x_test = scale(x_test)
    # Encode the output:
    y_test = onehot_encoding(y_test)

    # evaluate and log:
    test_loss, test_metric = model.evaluate(x_test, y_test, verbose=2)
    mlflow.log_metric('test_loss', test_loss)
    mlflow.log_metric('test_loss', test_loss)

    # Other metric evaluation:
    y_pred = np.argmax(model.predict(x_test), axis=1)

    acc, precision, recall, f1 = eval_metrics(np.argmax(y_test, axis=1), y_pred)
    mlflow.log_metric('test_acc', acc)
    mlflow.log_metric('test_precision', precision)
    mlflow.log_metric('test_recall', recall)
    mlflow.log_metric('test_f1', f1)

    # Log model:
    mlflow.tensorflow.log_model(model, 'models')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Train a Keras CNN-based model for MNIST classification")
    parser.add_argument("--config-file", "-c", type=str, default='configs/cnnbased.yaml')
    args = parser.parse_args()

    # Load the configuration file:
    with open(args.config_file, 'r') as file:
        config = yaml.safe_load(file)

    # Get parameters:
    data_path = config['data']['dataset_path']

    # MLflow setup
    # mlflow.set_tracking_uri(config['mlflow']['mlruns_path'])
    mlflow.set_experiment(config['mlflow']['experiment_name'])

    with mlflow.start_run():
        train(data_path, config['training']['num_epochs'],
              config['training']['batch_size'])


# Imports

import currency
import con_basic_token

# Setup Tokens

STAKING_TOKEN = currency
YIELD_TOKEN = con_basic_token

# State Variables

Owner = Variable()
DevRewardWallet = Variable()
EmissionRatePerHour = Variable()
DevRewardPct = Variable()
StartTime = Variable()
EndTime = Variable()
OpenForBusiness = Variable()  # If false, users will be unable to join the pool

# State

Users = Hash(default_value=False)
Deposits = Hash(default_value=False)
Withdrawals = Hash(default_value=0)
CurrentEpochIndex = Variable()
Epochs = Hash(default_value=False)
StakedBalance = Variable()  # The total amount of farming token in the vault.

@construct
def seed():
    Owner.set(ctx.caller)
    DevRewardWallet.set(ctx.caller)
    CurrentEpochIndex.set(0)
    StakedBalance.set(0)
    Epochs[0] = {
        "time": now,
        "staked": 0
    }

    EmissionRatePerHour.set(136.98) # 1200000 RSWP per year = 10% of supply
    DevRewardPct.set(0.1)

    StartTime.set(datetime.datetime(year=2018, month=1, day=1, hour=0)) # The datetime from which you want to allow staking.
    EndTime.set(datetime.datetime(year=2050, month=1, day=1, hour=0)) # The datetime at which you want staking to finish.

    OpenForBusiness.set(True)

@export
def addStakingTokens(amount: float):
    assert OpenForBusiness.get() == True, 'This staking pool is not open right now.'
    assert amount > 0, 'You cannot stake a negative balance.'

    user = ctx.caller

    # Take the staking tokens from the user's wallet
    STAKING_TOKEN.transfer_from(amount=amount, to=ctx.this, main_account=user)

    # Update the Staked amount
    staked = StakedBalance.get()
    new_staked_amount = staked + amount
    StakedBalance.set(new_staked_amount)

    # Update the Epoch
    new_epoch_idx = incrementEpoch(new_staked_amount=new_staked_amount)

    if Deposits[user] is False:
        Deposits[user] = []

    # Create a record of the user's deposit

    Deposits[user].append({
        'starting_epoch': new_epoch_idx,
        'time': now,
        'amount': amount
    })

@export
def withdrawYield(amount: float):
    assert amount > 0, 'You cannot harvest a negative balance'

    user = ctx.caller
    deposits = Deposits[user]

    assert deposits is not False, "You have no deposit to withdraw yield from."

    # Calculate how much yield is due per deposit account
    withdrawn_yield = Withdrawals[user]
    harvestable_yield = 0

    for d in deposits:
        harvestable_yield += calculateYield(
            starting_epoch_index=d['starting_epoch'], start_time=d['time'], amount=d['amount'])

    # Determine maximum amount of yield user can withdraw
    harvestable_yield -= withdrawn_yield

    yield_to_harvest = amount if amount < harvestable_yield else harvestable_yield

    assert yield_to_harvest > 0, 'There is no yield to harvest right now :('

    # Take % of Yield Tokens, send it to dev fund
    dev_share = yield_to_harvest * DevRewardPct.get()

    if dev_share > 0:
        YIELD_TOKEN.transfer(to=DevRewardWallet.get(), amount=dev_share)

    # Send remanding Yield Tokens to user
    user_share = yield_to_harvest-dev_share
    YIELD_TOKEN.transfer(to=user, amount=user_share)

    Withdrawals[user] = withdrawn_yield + yield_to_harvest

@export
def withdrawTokensAndYield():
    user = ctx.caller
    deposits = Deposits[user]

    assert deposits is not False, "You have no deposit to withdraw"

    # Calculate how much yield is due per deposit account
    withdrawn_yield = Withdrawals[user]
    stake_to_return = 0
    yield_to_harvest = 0

    for d in deposits:
        yield_to_harvest += calculateYield(
            starting_epoch_index=d['starting_epoch'], start_time=d['time'], amount=d['amount'])
        stake_to_return += d['amount']

    # Send Staking Tokens to user
    STAKING_TOKEN.transfer(to=user, amount=stake_to_return)

    # check that the user has yield left to harvest (this should never be negative, but let's check here just in case)
    yield_to_harvest -= withdrawn_yield
    if yield_to_harvest > 0:

        # Take % of Yield Tokens, send it to dev fund
        dev_share = yield_to_harvest * DevRewardPct.get()
        if dev_share > 0:
            YIELD_TOKEN.transfer(to=DevRewardWallet.get(), amount=dev_share)

        # Send remanding Yield Tokens to user
        user_share = yield_to_harvest-dev_share
        YIELD_TOKEN.transfer(to=user, amount=user_share)

    # Reset User's Deposits
    Deposits[user] = False

    # Reset User's Withdrawal
    Withdrawals[user] = 0

    # Remove token amount from Staked
    new_staked_amount = StakedBalance.get() - stake_to_return
    StakedBalance.set(new_staked_amount)

    # Increment Epoch
    incrementEpoch(new_staked_amount=new_staked_amount)

# This runs over each of the items in the user's Deposit
def calculateYield(starting_epoch_index: int, start_time, amount: float):
    current_epoch_index = getCurrentEpochIndex()
    this_epoch_index = starting_epoch_index
    y = 0
    while this_epoch_index <= current_epoch_index:
        this_epoch = Epochs[this_epoch_index]
        next_epoch = Epochs[this_epoch_index+1]

        delta = 0

        if starting_epoch_index == current_epoch_index:
            delta = fitTimeToRange(now) - fitTimeToRange(start_time)
        elif this_epoch_index == starting_epoch_index:
            delta = fitTimeToRange(next_epoch['time']) - fitTimeToRange(start_time)
        elif this_epoch_index == current_epoch_index:
            delta = fitTimeToRange(now) - fitTimeToRange(this_epoch['time'])
        else:
            delta = fitTimeToRange(next_epoch['time']) - fitTimeToRange(this_epoch['time'])

        pct_share_of_stake = amount / this_epoch['staked']
        # These two lines below were causing some problems, until I used the decimal method. get a python expert to review.
        global_yield_this_epoch = delta.seconds * getEmissionRatePerSecond()
        deposit_yield_this_epoch = decimal(
            global_yield_this_epoch) * pct_share_of_stake
        y += deposit_yield_this_epoch

        this_epoch_index += 1

    return y

def fitTimeToRange(time: Any):
    if time < StartTime.get():
        time = StartTime.get()
    elif time > EndTime.get():
        time = EndTime.get()
    return time

@export
def getCurrentEpochIndex():
    current_epoch_index = CurrentEpochIndex.get()
    return current_epoch_index

def incrementEpoch(new_staked_amount: float):
    current_epoch = CurrentEpochIndex.get()
    new_epoch_idx = current_epoch + 1
    CurrentEpochIndex.set(new_epoch_idx)
    Epochs[new_epoch_idx] = {
        "time": now,
        "staked": new_staked_amount
    }
    return new_epoch_idx

@export
def getEmissionRatePerSecond():
    emission_rate_per_hour = EmissionRatePerHour.get()
    emission_rate_per_minute = emission_rate_per_hour / 60
    emission_rate_per_second = emission_rate_per_minute / 60
    return emission_rate_per_second

@export
def setOwner(vk: str):
    assertOwner()
    Owner.set(vk)

@export
def setDevWallet(vk: str):
    assertOwner()
    DevRewardWallet.set(vk)

@export
def setDevRewardPct(amount: float):
    assertOwner()
    assert amount < 1 and amount >= 0, 'Amount must be a value between 0 and 1'
    DevRewardPct.set(amount)

@export
def setEmissionRatePerHour(amount: float):
    assertOwner()
    EmissionRatePerHour.set(amount)

@export
def recoverYieldToken(amount: float):
    assertOwner()
    YIELD_TOKEN.transfer(amount=amount, to=Owner.get())

@export
def allowStaking(is_open: bool):
    assertOwner()
    OpenForBusiness.set(is_open)

@export
def setStartTime(year: int, month: int, day: int, hour: int):
    assertOwner()
    time = datetime.datetime(year, month, day, hour)
    StartTime.set(time)

@export
def setEndTime(year: int, month: int, day: int, hour: int):
    assertOwner()
    time = datetime.datetime(year, month, day, hour)
    EndTime.set(time)

def assertOwner():
    assert Owner.get() == ctx.caller, 'You must be the owner to call this function.'


from sys import argv
import random
N = 10
Q = 100
PL = 400
PR = 600
TL = 4
TR = 6
filename = 'default.req'
random.seed(11)

if len(argv) > 1:
    filename = argv[1]
file = open(filename, 'w')
file.write('{}\n'.format(Q))
port = [0] * N
t = 0
for i in range(Q):
    h = random.randint(1, N - 1)
    port[h] = (port[h] + 1) % 100
    t = t + random.uniform(TL, TR)
    file.write('{} h1 h{} {} {}\n'.format(t, h + 1, port[h] + 5400, random.randint(PL, PR)))
file.close()


"""
-------------------------------
    I N T R O D U C T I O N
-------------------------------
Author:         Fuad Al Abir
Date:           December 26, 2018
File name:      trailing_zeroes_of_factorial.py
Objective:      is to find the trailing zeroes of factorial of numbers
Problem Source: To derive a formula of the trailing zeroes comes with the factorial of a number

-------------------------------------
    I M P O R T E D   M O D U L E
-------------------------------------
Header: math
Reason: to get factorial() values

-------------------------------------------------
    U S E R   D E F I N E D   F U N C T I O N
-------------------------------------------------

Function: _powered_by(a)
Reason: 5 based logarithm of a
Function: _trailing_zeroes(a)
Reason: returns the number of trailing zeroes of a
        by the recursive formula
        w/o calculating the factorial of a
Function: _trailing_zeroes_in_range(a, b)
Reason: prints the number of trailing zeroes in range a to b by two methods

---------------------------------
    S A M P L E   O U T P U T
---------------------------------
15620! has 3900 = 3900 zeroes.
15621! has 3900 = 3900 zeroes.
15622! has 3900 = 3900 zeroes.
15623! has 3900 = 3900 zeroes.
15624! has 3900 = 3900 zeroes.
15625! has 3906 = 3906 zeroes.
15626! has 3906 = 3906 zeroes.
15627! has 3906 = 3906 zeroes.
15628! has 3906 = 3906 zeroes.
15629! has 3906 = 3906 zeroes.

--------------------------
    Recursive Formula:
--------------------------
                        0                                   if p = 0
_tailing_zeroes(n) =    n//5                                if p = 1
                        n//5 + _tailing_zeroes(n//5)        if p > 1

                        here,   p       = 5 based log of n
                                n//5    = n's floor division by 5
"""

import math

def trailing_zeros(longint):
    manipulandum = str(longint)
    return len(manipulandum)-len(manipulandum.rstrip('0'))

# 5 based logarithm
def _powered_by(a):
    c = 0
    while a >= 5:
        a /= 5
        c += 1
    return c

def _trailing_zeroes(longint):
    p = _powered_by(longint)
    if (p == 0): return 0
    elif (p == 1): return longint//5
    else: return longint//5 + _trailing_zeroes(longint//5)

def _trailing_zeroes_in_range(a, b):
    for x in range(a, b):
        fact = math.factorial(x)
        print(x, end="")
        print("! has", _trailing_zeroes(x), "=", trailing_zeros(fact), "zeroes.")

_trailing_zeroes_in_range(5 ** 6 - 5, 5 ** 6 + 5)


from django.shortcuts import render
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
from django.conf import settings
from django.http import (
    HttpResponse,
    JsonResponse,
    HttpResponseRedirect,
)

from .models import (
    Hat,
    HatUser,
    Like,
    Slap,
    HatTransfer,
    HatSerializer,
    HatUserSerializer,
    LikeSerializer,
    SlapSerializer,
    HatTransferSerializer,
)

class HomePage(View):
    """ Basic home page
    """
    def get(self, request):
        hats = Hat.objects.all()
        users = HatUser.objects.all()
        hat_transfers = HatTransfer.objects.order_by("-create_date")[:50]
        if settings.PARTY_IS_HAPPENING:
            dct = {
                'hats': hats,
                'users': users,
                'total_counter': sum(u.counter_value for u in users),
                'hat_transfers': hat_transfers,
            }
            return render(request, 'home.html', dct)
        else:
            dct = {
                'slap_count': Slap.objects.count(),
                'like_count': Like.objects.count(),
                'transfer_count': HatTransfer.objects.count(),
            }
            return render(request, 'after.html', dct)

class HatUserCreate(View):
    """ Un-auth'd page to create user
    """
    def get(self, request):
        return render(request, 'login.html', {})

    def post(self, request):
        name = request.POST.get('name')
        email = request.POST.get('email')
        matching_user = HatUser.objects.filter(name=name).first()
        if matching_user:
            user = matching_user
        else:
            user = HatUser()
            user.name = name
        if email:
            user.email = email
        user.save()
        request.session['user_auth_token'] = str(user.auth_token)
        return HttpResponseRedirect("/")

class CreateSlap(APIView):
    """ Slap!
    """
    def post(self, request, user_id=None):
        user = HatUser.objects.get(id=user_id)
        s = Slap(create_user=request.hat_user, target_user=user)
        s.save()
        return Response(SlapSerializer(s).data)

class ClaimHatRoute(APIView):
    """
    """
    def post(self, request, hat_id=None):
        hat = Hat.objects.get(id=hat_id)
        if hat:
            transfer = HatTransfer()
            if hat.current_wearer:
                transfer.source_user = hat.current_wearer
            transfer.target_user = request.hat_user
            transfer.hat = hat
            transfer.save()
            request.hat_user.current_hat = hat
            request.hat_user.save()
        return Response()

class DitchHatRoute(APIView):
    """
    """
    def post(self, request, hat_id=None):
        hat = Hat.objects.get(id=hat_id)
        if hat and hat.current_wearer:
            transfer = HatTransfer()
            transfer.source_user = request.hat_user
            transfer.target_user = None
            transfer.hat = hat
            transfer.save()
            request.hat_user.current_hat = None
            request.hat_user.save()
        return Response()

class LikeHatRoute(APIView):
    """
    """
    def post(self, request, hat_id=None):
        hat = Hat.objects.get(id=hat_id)
        if hat and hat.current_wearer:
            like = Like()
            like.create_user = request.hat_user
            like.target_user = hat.current_wearer
            like.hat = hat
            like.save()
        return Response()

class RefreshDataRoute(APIView):
    """ Returns a giant blob on JSON for loading async data on page
    """
    def get(self, request):
        likes = Like.objects.all()
        slaps = Slap.objects.all()
        transfers = HatTransfer.objects.all()
        hats = Hat.objects.all()
        users = HatUser.objects.all()

        dct = {
            'hats': HatSerializer(hats, many=True).data,
            'users': HatUserSerializer(users, many=True).data,
            'likes': LikeSerializer(likes, many=True).data,
            'slaps': SlapSerializer(slaps, many=True).data,
            'transfers': HatTransferSerializer(transfers, many=True).data,
            'total_counter': sum(u.counter_value for u in users),
        }

        return Response(dct)

class IncrementCounter(APIView):
    """ Hehe
    """
    def post(self, request):
        request.hat_user.counter_value += 1
        request.hat_user.save()
        return Response()


import numpy as np
import matplotlib.pyplot as pt

case = 1

mesh = np.loadtxt("OutputData/grid_case"+str(case)+".dat") # Holds the data of the mesh grid.
arrayh = np.loadtxt("OutputData/computed_case"+str(case)+".dat") # Holds the data of the computed solution
array2 = np.loadtxt("OutputData/computed_case"+str(case)+"_2.dat");

ts = arrayh[:, 0] # time steps are in first column of output.dat file
us_h = arrayh[:,1:] # computed solutions are on columns: from 1 onwards, of file output.dat
us2 = array2[:,1:]

_maph = {}
_maph2 = {}

for i in range(ts.size):
    t = ts[i]
    _maph[t] = us_h[i] # Map time step to its computed solution.
    _maph2[t] = us2[i]

ts_toplot = [0, 2.5, 5] # Will only plot for this time values.

for time in ts_toplot:
    fig = pt.figure(figsize=(9,7))
    ax = fig.add_subplot(1,1,1)
    pay_off = _maph2[0]
    approx_sol = _maph[time]
    approx_sol2 = _maph2[time]
    ax.plot(mesh, approx_sol, 'r-', label = 'American Put')
    ax.plot(mesh, approx_sol2, 'b-', label = 'European Put')
    ax.plot(mesh, pay_off, 'g-', label = 'Pay off')
    ax.set_title("Case "+str(case+4)+"\n Black-Scholes @ time: "+str(time))
    ax.legend(loc='upper right')
    ax.set_xlabel('x \n Asset Price')
    ax.set_ylabel('Value of Put Option \n v(x,'+str(time)+') ')
    fig.savefig("BS_Case"+str(case)+"_time_"+str(time)+".png")

pt.show()


#create a copy of a two dimensional array

import stdarray

# the array is square

#a = stdarray.create2D(5, 5, 0)
#print(a)
#b = a[:]
#print(b)
# the array is rectangular

#c = stdarray.create2D(5, 10, 0)
#print(c)
#d = c[:]
#print(d)
# the array is ragged
e = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
f = e[:]
print(f)

# the array is ragged
z = []
for i in range(len(e)):
    t = []
    for j in range(len(e[i])):
        t += [e[i][j]]
    z += [t]
print(z)

Chapter 1 - Elements of Programming/1.4 Arrays/1-4-9.py


from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from Crypto.PublicKey._slowmath import rsa_construct
import secrets
import os

def readPublicBallotKey(ballotNo):
    wrkd = os.getcwd()
    pathPublic = wrkd + "/Signatures/"+str(ballotNo)+"_ballotPublic.pem"
    PublicBallotKey = open(str(pathPublic), "rb").read()
    return PublicBallotKey

def readPrivateBallotKey(ballotNo):
    wrkd = os.getcwd()
    pathPrivate = wrkd + "/Signatures/"+str(ballotNo)+"_ballotPrivate.pem"
    PrivateBallotKey = open(str(pathPrivate), "rb").read()
    return PrivateBallotKey

def writePrivateBallotKey(ballotNo):
    wrkd = os.getcwd()
    pathPrivate = wrkd + "/Signatures/"+str(ballotNo)+"_ballotPrivate.pem"
    pathPublic = wrkd + "/Signatures/"+str(ballotNo)+"_ballotPublic.pem"
    if(os.path.isfile(str(pathPrivate)) == False):
        ballot = rsa.generate_private_key(65537, 512)
        ballot_private = ballot.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
        ballot_public = (ballot.public_key()).public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)
        open(str(pathPrivate), "wb").write(ballot_private)
        open(str(pathPublic), "wb").write(ballot_public)

def signedKey(blindKey, ballotNo):
    ballot = serialization.load_pem_private_key(readPrivateBallotKey(ballotNo), None,None)
    rsa_priv_key = rsa_construct(ballot.private_numbers()._public_numbers._n, ballot.private_numbers()._public_numbers._e, ballot.private_numbers()._d, ballot.private_numbers()._p, ballot.private_numbers()._q)
    signedToken = rsa_priv_key._sign(blindKey)
    return signedToken

def verifySignedKey(signedKey, blindKey, ballotNo):
    ballot = serialization.load_pem_public_key(readPublicBallotKey(ballotNo))
    ballot.verify(signedKey, blindKey, padding.PSS(padding.MGF1(hashes.SHA256()), padding.PSS.MAX_LENGTH), hashes.SHA256())

#writePrivateBallotKey(223) use this once to generate private key for ballot no 223
#bkey = secrets.token_bytes(64)
#skey = signedKey(bkey, 223)
#print(skey)


# Basically, defined as:
# {
#     'NODE A': set(['CONNECTING NODE 1', 'CONNECTING NODE 2', ...]),
#     'NODE B': set(['CONNECTING NODE 1', 'CONNECTING NODE 2', ...]),
#     ...
# }
graph = {'A': set(['B', 'C']),
         'B': set(['A', 'D', 'E']),
         'C': set(['A', 'F']),
         'D': set(['B']),
         'E': set(['B', 'F']),
         'F': set(['C', 'E'])}

def dfs(graph, start, goal, path=None):
    if path is None:
        path = [start]

    if start == goal:
        yield path

    for node in (graph[start] - set(path)):
        for res in dfs(graph, node, goal, path + [node]):
            yield res

def bfs(graph, start, goal):
    Q = [(start, [start])]

    while Q:
        v, path = Q.pop(0)

        for node in (graph[v] - set(path)):
            if node == goal:
                yield path + [node]
            else:
                Q.append((node, path + [node]))


# Matt Goggin
#
# the controller for the BnterProfiler App.
# Presents the two static pages, and also the simple
# wrapper API needed to support JSONP

# commands to start server and update
# dev_appserver.py /Users/mattgoggin/Desktop/CS/bnterprofiler
# appcfg.py update /Users/mattgoggin/Desktop/CS/bnterprofiler
# java -jar yuicompressor-2.4.2.jar -o bnterprofiler.min.js  bnterprofiler.js

import os
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from django.utils import simplejson as json

# This class wraps the actual Bnter API in order to provide support for
# JSONP requests.
# responds to requests of the form
# /bnter?user_screen_name=USERNAME
class APIWrapper(webapp.RequestHandler):
   def formatJSON(self, JSON, callback):
       if callback:
           JSON = '%s(%s)' % (callback, JSON)
       return JSON
   def get(self):
       username = self.request.get('user_screen_name')
       callback = self.request.get('callback')
       url = 'http://bnter.com/api/v1/user/conversations.json?&user_screen_name=' + username

       if not username:
          self.response.out.write('required username and callback')
          return

       JSON = memcache.get(url)
       if JSON:
           self.response.out.write(self.formatJSON(JSON, callback))
           return

       result = urlfetch.fetch(url)
       if result.status_code == 200:
           memcache.set(url, result.content, 60 * 30)
           self.response.out.write(self.formatJSON(result.content, callback))

# These two classes simply write out the static HTML
# for the two pages, using the helper WriteTemplate
def WriteTemplate(page, html, include_js):
    template_values = {'include_js':include_js}
    temp = os.path.join('..', 'html',  html)
    path = os.path.join(os.path.dirname(__file__), temp)
    page.response.out.write(template.render(path, template_values))

class WTF(webapp.RequestHandler):
   def get(self):
      WriteTemplate(self, 'wtf.html', False)

class Main(webapp.RequestHandler):
   def get(self):
      WriteTemplate(self, 'index.html', True)

# /_ah/warmup
# simply here to suppress 404's on our dashboard from GAE hitting this URL
class WarmupHandler(webapp.RequestHandler):
    def get(self):
        logging.info('Warmup Request')

application = webapp.WSGIApplication(
                                    [
                                     ('/', Main),
                                     ('/wtf', WTF),
                                     ('/bnter', APIWrapper)
                                    ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()


import hashlib
import random
import requests

from flask import current_app

class Piastrix:
    def __init__(self, amount, currency):
        self.amount = amount
        self.currency = currency

    def request_piastrix(self, url, fields):
        try:
            resp = requests.post(url, json=fields).json()
        except requests.exceptions.ConnectionError as e:
            current_app.logger.error(e)
            return None
        return resp

    def prepare_form_fields(self):
        if self.currency == '978':
            fields, keys_required = self._prepare_data_eur()
        elif self.currency == '840':
            fields, keys_required = self._prepare_data_usd()
        else:
            fields, keys_required = self._prepare_data_rur()

        sign = self._get_sign(fields, keys_required)
        fields['sign'] = sign
        return fields

    def _prepare_data_rur(self):
        keys_required = ['amount', 'currency', 'payway', 'shop_id', 'shop_order_id']
        fields = {
            'amount': self.amount,
            'currency': self.currency,
            'payway': 'payeer_rub',
            'shop_id': current_app.config['SHOP_ID'],
            'shop_order_id': random.randrange(10000)
        }
        return fields, keys_required

    def _prepare_data_usd(self):
        keys_required = ['shop_amount', 'shop_currency', 'shop_id', 'shop_order_id', 'payer_currency']
        fields = {
            'shop_amount': self.amount,
            'shop_currency': self.currency,
            'shop_id': current_app.config['SHOP_ID'],
            'shop_order_id': random.randrange(10000),
            'payer_currency': self.currency
        }
        return fields, keys_required

    def _prepare_data_eur(self):
        keys_required = ['amount', 'currency', 'shop_id', 'shop_order_id']
        fields = {
            'amount': self.amount,
            'currency': self.currency,
            'shop_id': current_app.config['SHOP_ID'],
            'shop_order_id': random.randrange(10000)
        }
        return fields, keys_required

    def _get_sign(self, fields, keys_required):
        keys_sorted = sorted(keys_required)
        values = [str(fields[k]) for k in keys_sorted]
        pre_hash = ':'.join(values)
        pre_hash += current_app.config['SECRET_KEY']
        sign = hashlib.sha256(pre_hash.encode()).hexdigest()
        return sign


import math
import struct

import speakeasy.windows.windows.windows as windef
from unicorn.unicorn_const import UC_ARCH_X86, UC_ERR_EXCEPTION
from uc_handler.api_handler import CALL_CONV as cv
from uc_handler.api_handler import ApiHandler
import common

EINVAL = 22
_TRUNCATE = 0xFFFFFFFF

TIME_BASE = 1576292568
RAND_BASE = 0
TICK_BASE = 86400000  # 1 day in millisecs

class Msvcrt(ApiHandler):
    """
    Implements functions from various versions of the C runtime on Windows
    """
    name = 'msvcrt'
    api_call = ApiHandler.api_call

    def __init__(self, win_emu):
        self.win_emu = win_emu
        self.stdin = 0
        self.stdout = 1
        self.stderr = 2

        self.rand_int = RAND_BASE

        self.funcs = {}
        self.data = {}
        self.wintypes = windef
        self.ptr_size = self.win_emu.get_ptr_size()
        self.tick_counter = TICK_BASE

        super().__set_api_attrs__(self) # initalize info about each apis

    # Reference: https://wiki.osdev.org/Visual_C%2B%2B_Runtime
    @api_call('_initterm', argc=2, conv=cv.CALL_CONV_CDECL)
    def _initterm(self, proc, argv, ctx={}):
        """
        static int _initterm_e(_PIFV * pfbegin,
                                    _PIFV * pfend)
        """

        pfbegin, pfend = argv

        rv = 0

        return rv

    @api_call('_initterm_e', argc=2, conv=cv.CALL_CONV_CDECL)
    def _initterm_e(self, proc, argv, ctx={}):
        """
        static int _initterm_e(_PIFV * pfbegin,
                                    _PIFV * pfend)
        """

        return self._initterm(proc, argv, ctx={})

    @api_call('__p___argv', argc=0, conv=cv.CALL_CONV_CDECL)
    def __p___argv(self, proc, argv, ctx={}):
        """char *** __p___argv ()"""

        ptr_size = proc.ptr_size
        _argv = proc.get_param()
        if not _argv:
            return 0
        argv = [(a + '\x00\x00\x00\x00').encode('utf-8') for a in _argv]

        array_size = (ptr_size * (len(argv) + 1))
        total = sum([len(a) for a in argv])
        total += array_size

        sptr = 0
        pptr = 0

        #pArgs = proc.default_heap_alloc(total+ptr_size)
        pArgs = self.win_emu.mem_manager.alloc_heap(proc.default_proc_heap, total+ptr_size)
        pptr = pArgs + ptr_size
        common.mem_write(proc.uc_eng, pArgs, pptr.to_bytes(ptr_size, 'little'))
        sptr = pptr + array_size

        for a in argv:
            proc.write_mem_self(pptr, sptr.to_bytes(ptr_size, 'little'))
            pptr += ptr_size
            proc.write_mem_self(sptr, a)
            sptr += len(a)

        proc.write_mem_self(pptr, pptr, b'\x00' * ptr_size)

        rv = pArgs

        return rv

    @api_call('__p___argc', argc=0, conv=cv.CALL_CONV_CDECL)
    def __p___argc(self, proc, argv, ctx={}):
        """int * __p___argc ()"""

        _argv = proc.get_param()
        if not _argv:
            return 0
        pMem = self.win_emu.mem_manager.alloc_heap(proc.default_proc_heap, self.ptr_size*2)
        proc.write_mem_self(pMem, len(_argv).to_bytes(4, 'little'))

        return pMem

    @api_call('_get_initial_narrow_environment', argc=0, conv=cv.CALL_CONV_CDECL)
    def _get_initial_narrow_environment(self, proc, argv, ctx={}):
        """char** _get_initial_narrow_environment ()"""

        ptr_size = self.win_emu.get_ptr_size()
        env = common.get_env(self.win_emu)
        total = ptr_size
        sptr = total
        pptr = 0
        fmt_env = []
        for k, v in env.items():
            envstr = '%s=%s\x00' % (k, v)
            envstr = envstr.encode('utf-8')
            total += len(envstr)
            fmt_env.append(envstr)
            total += ptr_size
            sptr += ptr_size

        pMem = self.win_emu.mem_manager.alloc_heap(proc.proc_default_heap, ptr_size*2)
        pptr = pMem
        sptr += pMem

        for v in fmt_env:
            proc.write_mem_self(pptr, sptr.to_bytes(ptr_size, 'little'))
            pptr += ptr_size
            proc.write_mem_self(sptr, v)
            sptr += len(v)

        return pMem

    @api_call('exit', argc=1, conv=cv.CALL_CONV_CDECL)
    def exit(self, proc, argv, ctx={}):
        """
        void exit(
           int const status
        );
        """
        proc.exit()
        return 0

    @api_call('_exit', argc=1, conv=cv.CALL_CONV_CDECL)
    def _exit(self, proc, argv, ctx={}):
        """
        void _exit(
           int const status
        );
        """
        self.exit(proc, argv, ctx)
        return 0

    @api_call('_cexit', argc=1, conv=cv.CALL_CONV_CDECL)
    def _cexit(self, proc, argv, ctx={}):
        """
        void _cexit(
           int const status
        );
        """
        self.exit(proc, argv, ctx)

        return 0

    @api_call('_CrtSetCheckCount', argc=1, conv=cv.CALL_CONV_CDECL)
    def _crtsetcheckcount(self, proc, argv, ctx={}):
        """int _CrtSetCheckCount( int chk_count );"""
        rv = 0
        return rv

    @api_call('__acrt_iob_func', argc=1, conv=cv.CALL_CONV_CDECL)
    def __acrt_iob_func(self, proc, argv, ctx={}):
        """FILE * __acrt_iob_func (fd)"""

        fd, = argv

        return fd

    @api_call('printf', argc=0, conv=cv.CALL_CONV_CDECL)
    def printf(self, proc, argv, ctx={}):

        arch = proc.get_arch()
        if arch == UC_ARCH_X86:
            fmt, va_list = ApiHandler.get_argv(proc, cv.CALL_CONV_CDECL, 2)[:2]
        else:
            raise Exception ("Unsupported architecture")

        rv = 0

        fmt_str = common.read_mem_string(proc.uc_eng, fmt, 1)
        fmt_cnt = self.get_va_arg_count(fmt_str)

        vargs = self.va_args2(proc, fmt_cnt)
        fin = common.make_fmt_str(proc, fmt_str, vargs)

        rv = len(fin)
        argv.append(fin)

        # print(fin)

        return rv

    @api_call('wprintf', argc=0, conv=cv.CALL_CONV_CDECL)
    def wprintf(self, proc, argv, ctx={}):

        arch = proc.get_arch()
        if arch == UC_ARCH_X86:
            fmt, va_list = ApiHandler.get_argv(proc, cv.CALL_CONV_CDECL, 2)[:2]
        else:
            raise Exception ("Unsupported architecture")

        rv = 0

        fmt_str = common.read_wide_string(proc.uc_eng, fmt)
        fmt_cnt = self.get_va_arg_count(fmt_str)

        vargs = self.va_args2(proc, fmt_cnt)
        fin = common.make_fmt_str(proc, fmt_str, vargs, True)

        rv = len(fin)
        argv.append(fin)

        # print(fin)

        return rv

    @api_call('__stdio_common_vfprintf', argc=0, conv=cv.CALL_CONV_CDECL)
    def __stdio_common_vfprintf(self, proc, argv, ctx={}):
        arch = proc.get_arch()
        if arch == UC_ARCH_X86:
            opts, opts2, stream, fmt, _, va_list = ApiHandler.get_argv(proc, cv.CALL_CONV_CDECL, 6)[:6]
        else:
            raise Exception ("Unsupported architecture")

        fmt_str = proc.read_string(fmt, 1)
        fmt_cnt = self.get_va_arg_count(fmt_str)
        vargs = self.va_args(proc, va_list, fmt_cnt)
        fin = common.make_fmt_str(proc, fmt_str, vargs)
        argv[:] = [opts, stream, fin]

        rv = len(fin)

        return rv

    @api_call('puts', argc=1, conv=cv.CALL_CONV_CDECL)
    def puts(self, proc, argv, ctx={}):
        """
        int puts(
           const char *str
        );
        """
        s, = argv
        string = proc.read_string(s, 1)
        argv[0] = string
        rv = len(string)

        return rv

    @api_call('_putws', argc=1, conv=cv.CALL_CONV_CDECL)
    def _putws(self, proc, argv, ctx={}):
        """
        int _putws(
           const wchar_t *str
        );
        """
        s, = argv
        string = common.read_string(s, 2, 20)
        argv[0] = string
        rv = len(string)

        # print(string)

        return rv

    @api_call('strlen', argc=1, conv=cv.CALL_CONV_CDECL)
    def strlen(self, proc, argv, ctx={}):
        """
        size_t strlen(
            const char *str
        );
        """
        s, = argv
        string = proc.read_string(s, 1)
        argv[0] = string
        rv = len(string)

        return rv

    @api_call('strcpy', argc=2, conv=cv.CALL_CONV_CDECL)
    def strcpy(self, proc, argv, ctx={}):
        """
        char *strcpy(
           char *strDestination,
           const char *strSource
        );
        """
        dest, src = argv
        s = proc.read_string(src, 1)
        bytz = s.encode("ascii")
        proc.write_mem_self(bytz, len(bytz))
        argv[1] = s
        return dest

    @api_call('wcscpy', argc=2, conv=cv.CALL_CONV_CDECL)
    def wcscpy(self, proc, argv, ctx={}):
        """
        wchar_t *wcscpy(
            wchar_t *strDestination,
            const wchar_t *strSource
        );
        """
        dest, src = argv
        ws = common.read_wide_string(proc.uc_eng, src)
        common.write_wide_string(proc.uc_eng, ws, dest)
        argv[1] = ws
        return dest

    @api_call('wcslen', argc=1, conv=cv.CALL_CONV_CDECL)
    def wcslen(self, proc, argv, ctx={}):
        """
        size_t wcslen(
          const wchar_t* wcs
        );
        """
        s, = argv
        string = proc.read_string(s, 2)
        argv[0] = string
        rv = len(string)

        return rv

    @api_call('wcscat', argc=2, conv=cv.CALL_CONV_CDECL)
    def wcscat(self, proc, argv, ctx={}):
        '''
        wchar_t *wcscat(
           wchar_t *strDestination,
           const wchar_t *strSource
        );
        '''
        _str1, _str2 = argv
        s1 = proc.read_string(_str1, 2)
        s2 = proc.read_string(_str2, 2)

        argv[0] = s1
        argv[1] = s2
        new = (s1 + s2).encode('utf-16le')
        proc.write_mem_self(_str1, new + b'\x00\x00')

        return _str1

    @api_call('_wtoi', argc=1, conv=cv.CALL_CONV_CDECL)
    def _wtoi(self, proc, argv, ctx={}):
        pStr, = argv
        _str = proc.read_string(pStr, 2)

        return int.from_bytes(_str.encode("utf-16le"), "little")

    @api_call('strncpy', argc=3, conv=cv.CALL_CONV_CDECL)
    def strncpy(self, proc, argv, ctx={}):
        """
        char * strncpy(
            char * destination,
            const char * source,
            size_t num
        );
        """
        dest, src, length = argv
        s = proc.read_string(src, 1, max_len=length)
        if len(s) < length:
            s += '\x00'*(length-len(s))
        proc.write_mem_self(s, dest)

        argv[1] = s
        return dest

    @api_call('memcpy', argc=3, conv=cv.CALL_CONV_CDECL)
    def memcpy(self, proc, argv, ctx={}):
        """
        void *memcpy(
            void *dest,
            const void *src,
            size_t count
            );
        """
        dest, src, count = argv
        data = proc.read_mem_self(src, count)
        if isinstance(data, bytearray):
            data = bytes(data)
        proc.write_mem_self(dest, data)

        return dest

    @api_call('memmove', argc=3, conv=cv.CALL_CONV_CDECL)
    def memmove(self, proc, argv, ctx={}):
        """
        void *memmove(
            void *dest,
            const void *src,
            size_t count
        );
        """

        return self.memcpy(proc, argv, ctx)

    @api_call('memset', argc=3, conv=cv.CALL_CONV_CDECL)
    def memset(self, proc, argv, ctx={}):
        """
        void *memset ( void * ptr,
                       int value,
                       size_t num );
        """

        ptr, value, num = argv

        data = value.to_bytes(1, 'little') * num
        proc.write_mem_self(ptr, data)

        return ptr

    @api_call('memcmp', argc=3, conv=cv.CALL_CONV_CDECL)
    def memcmp(self, proc, argv, ctx={}):
        """
        int memcmp(
           const void *buffer1,
           const void *buffer2,
           size_t count
        );
        """
        diff = 0
        buff1, buff2, cnt = argv
        for i in range(cnt):
            b1 = proc.read_mem_self(buff1, 1)
            b2 = proc.read_mem_self(buff2, 1)

            if b1 > b2:
                diff = 1
                break
            elif b1 < b2:
                diff = -1
                break

        return diff

    @api_call('malloc', argc=1, conv=cv.CALL_CONV_CDECL)
    def malloc(self, proc, argv, ctx={}):
        """
        void *malloc(
        size_t size
        );
        """
        size, = argv
        pMem = self.win_emu.mem_manager.alloc_heap(proc.proc_default_heap, size)

        return pMem

    @api_call('calloc', argc=2, conv=cv.CALL_CONV_CDECL)
    def calloc(self, proc, argv, ctx={}):
        """
        void *calloc(
        size_t num,
        size_t size
        );
        """
        num, size, = argv
        pMem = self.malloc(proc, num*size, ctx)
        self.memset(proc, (pMem, 0, num*size))
        return

    @api_call('free', argc=1, conv=cv.CALL_CONV_CDECL)
    def free(self, proc, argv, ctx={}):
        """
        void free(
        void *memblock
        );
        """
        mem, = argv
        self.win_emu.mem_manager.free_heap(proc.proc_default_heap.handle, mem)


import ray
from ray import tune
import numpy as np
from env.neobotixschunkGymEnv import NeobotixSchunkGymEnv

def env_creator(env_config):
    env = NeobotixSchunkGymEnv(**env_config)
    return env

tune.register_env("my_env",env_creator)

ray.init()
tune.run(
    "PPO",
    num_samples= 1,
    checkpoint_freq=10,
    config={
        "env":"my_env",

    }
)


# There is a forest with an unknown number of rabbits. We asked n rabbits "How
# many rabbits have the same color as you?" and collected the answers in an
# integer array answers where answers[i] is the answer of the iᵗʰ rabbit.
#
#  Given the array answers, return the minimum number of rabbits that could be
# in the forest.
#
#
#  Example 1:
#
#
# Input: answers = [1,1,2]
# Output: 5
# Explanation:
# The two rabbits that answered "1" could both be the same color, say red.
# The rabbit that answered "2" can't be red or the answers would be
# inconsistent.
# Say the rabbit that answered "2" was blue.
# Then there should be 2 other blue rabbits in the forest that didn't answer
# into the array.
# The smallest possible number of rabbits in the forest is therefore 5: 3 that
# answered plus 2 that didn't.
#
#
#  Example 2:
#
#
# Input: answers = [10,10,10]
# Output: 11
#
#
#
#  Constraints:
#
#
#  1 <= answers.length <= 1000
#  0 <= answers[i] < 1000
#
#  Related Topics
from collections import Counter
from typing import List

# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        d = Counter(answers)
        ans = 0
        for k, v in d.items():
            ans += v // (k + 1) * (k + 1)
            if v % (k + 1):
                ans += 1 + k
        return ans

# leetcode submit region end(Prohibit modification and deletion)

if __name__ == '__main__':
    answers = [[1, 1, 1, 2], [10, 10, 10], [4]]
    for answer in answers:
        print(Solution().numRabbits(answer))


import numpy as np
import pandas as pd
from .result_loader import NORMALIZE_COLS, NORMALIZED_COLS
import torch

TARGET_COLS=["left_victory", "scores_sum_diff_norm", "match_duration_norm"]

class SymmetricScaler:
    def __init__(self):
        pass

    def fit(self, X):
        mean = X.mean(axis=0)
        minmax = X.min(axis=0), X.max(axis=0)
        diff = [abs(m-mean) for m in minmax]
        max_diff = np.maximum(*diff)
        self.mean = mean
        self.max_diff = max_diff
        return self

    def transform(self, X):
        X_std = (X - self.mean) / self.max_diff
        return X_std

    def fit_transform(self, X):
        self.fit(X)
        return self.transform(X)

    def inverse_transform(self, X_std):
        X = X_std * self.max_diff + self.mean
        return X

def normalize(df, scaler=None):
    if not scaler:
        scaler = SymmetricScaler().fit(df[NORMALIZE_COLS])
    df[NORMALIZED_COLS] = scaler.transform(df[NORMALIZE_COLS])
    return scaler

def calc_objective(target):
    target["objective"] = target["left_victory"] + (target["scores_sum_diff_norm"] / (2 + target["match_duration_norm"]))
    return target["objective"]

def extract_target(df, target_cols=TARGET_COLS):
    return torch.Tensor(df[target_cols].to_numpy().astype(float))

def split_dataframe(df, points, rand=42):
    return np.split(
        df.sample(frac=1, random_state=rand),
        [int(x*len(df)) for x in points]
    )

def split_dataframe_kfold(df, ratio=0.2, rand=42, val=True, filter_i=None):
    result = []
    count = int(1.0/ratio)
    splits = [i*ratio for i in range(1, count)]
    splits = split_dataframe(df, splits, rand=rand)

    for i in range(count):
        if filter_i and i not in filter_i:
            continue
        j = count - 1 + i
        test_df = splits[j%count]
        val_df = None
        if val:
            val_df = splits[(j-1)%count]
        train_dfs = [s for s in splits if s is not test_df and s is not val_df]
        train_df = pd.concat(train_dfs)
        result.append((train_df, val_df, test_df))

    return result


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

import copy
import os
import re
import subprocess
import random
import tempfile

from utils import util

def execute(cmd, verbose=True):
    if verbose:
        print("CMD> %s" % cmd)
    p = subprocess.Popen([cmd],
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         stdin=subprocess.PIPE,
                         universal_newlines=True)

    if not verbose:
        # use p.communicate instead of p.wait to avoid such situation:
        # pipe is filled and the child process is blocked.
        out, err = p.communicate()
        if p.returncode != 0:
            raise Exception("errorcode: {}".format(p.returncode))
        return out

    buf = []

    while p.poll() is None:
        line = p.stdout.readline().strip()
        if verbose:
            print(line)
        buf.append(line)

    for li in p.stdout:
        line = li.strip()
        if verbose:
            print(line)
        buf.append(line)

    if p.returncode != 0:
        if verbose:
            print(line)
        raise Exception("errorcode: %s" % p.returncode)

    return "\n".join(buf)

class Device(object):
    def __init__(self, device_id, target_abi):
        self._device_id = device_id
        self._target_abi = target_abi

    def install(self, target, install_dir, install_deps=False):
        pass

    def run(self, target):
        pass

    def pull(self, target, out_dir):
        pass

    def mkdir(self, dirname):
        pass

    def info(self):
        pass

class HostDevice(Device):
    def __init__(self, device_id, target_abi):
        super(HostDevice, self).__init__(device_id, target_abi)

    @staticmethod
    def list_devices():
        return ["host"]

    def install(self, target, install_dir, install_deps=False):
        install_dir = os.path.abspath(install_dir)

        if install_dir.strip() and install_dir != os.path.dirname(target.path):
            execute("mkdir -p %s" % install_dir)
            if os.path.isdir(target.path):
                execute("cp -f %s/* %s" % (target.path, install_dir))
            else:
                execute("cp -f %s %s" % (target.path, install_dir))
            for lib in target.libs:
                execute("cp -f %s %s" % (lib, install_dir))

            target.path = "%s/%s" % (install_dir,
                                     os.path.basename(target.path))
            target.libs = ["%s/%s" % (install_dir, os.path.basename(lib))
                           for lib in target.libs]

        target.envs.append("LD_LIBRARY_PATH=%s" % install_dir)

        return target

    def run(self, target):
        execute(str(target))

    def pull(self, target, out_dir):
        out_dir = os.path.abspath(out_dir)

        if out_dir.strip() and out_dir != os.path.dirname(target.path):
            execute("cp -rp %s %s" % (target.path, out_dir))

    def mkdir(self, dirname):
        execute("mkdir -p %s" % dirname)

class AndroidDevice(Device):
    def __init__(self, device_id, target_abi):
        super(AndroidDevice, self).__init__(device_id, target_abi)

    @staticmethod
    def list_devices():
        out = execute("adb devices", False)
        serialno_list = out.strip().split('\n')[1:]
        serialno_list = [tuple(pair.split('\t')) for pair in serialno_list]
        devices = []
        for serialno in serialno_list:
            if (len(serialno) < 2 or
                serialno[1].startswith("no permissions") or
                    serialno[1].startswith("unauthorized")):
                continue
            devices.append(serialno[0])

        return devices

    def install(self, target, install_dir, install_deps=False):
        install_dir = os.path.abspath(install_dir)
        sn = self._device_id

        execute("adb -s %s shell mkdir -p %s" % (sn, install_dir))
        if os.path.isdir(target.path):
            execute("adb -s %s push %s/* %s" % (sn, target.path, install_dir),
                    False)
        else:
            execute("adb -s %s push %s %s" % (sn, target.path, install_dir),
                    False)

        for lib in target.libs:
            execute("adb -s %s push %s %s" % (sn, lib, install_dir), False)

        device_target = copy.deepcopy(target)
        device_target.path = "%s/%s" % (install_dir,
                                        os.path.basename(target.path))
        device_target.libs = ["%s/%s" % (install_dir, os.path.basename(lib))
                              for lib in target.libs]
        device_target.envs.append("LD_LIBRARY_PATH=%s" % install_dir)

        if install_deps:
            self.install_common_libs_for_target(target, install_dir)

        return device_target

    def install_common_libs_for_target(self, target, install_dir):
        sn = self._device_id
        abi = self._target_abi
        lib_file = "%s/sources/cxx-stl/llvm-libc++/libs/" \
                   "%s/libc++_shared.so" \
                   % (os.environ["ANDROID_NDK_HOME"], abi)
        ndk_depends_path = os.environ["ANDROID_NDK_HOME"] + "/ndk-depends"
        if os.path.exists(ndk_depends_path):
            dep_so_libs = execute(ndk_depends_path + " " + target.path)
            for dep in dep_so_libs.split("\n"):
                if dep == "libgnustl_shared.so":
                    lib_file = "%s/sources/cxx-stl/gnu-libstdc++/4.9/libs/" \
                               "%s/libgnustl_shared.so" \
                               % (os.environ["ANDROID_NDK_HOME"], abi)
        else:
            print("Find no ndk-depends, use default libc++_shared.so")

        execute("adb -s %s push %s %s" % (sn, lib_file, install_dir), False)

    def run(self, target):
        tmpdirname = tempfile.mkdtemp()
        cmd_file_path = tmpdirname + "/cmd.sh"
        with open(cmd_file_path, "w") as cmd_file:
            cmd_file.write(str(target))
        target_dir = os.path.dirname(target.path)
        execute("adb -s %s push %s %s" % (self._device_id,
                                          cmd_file_path,
                                          target_dir))

        out = execute("adb -s %s shell sh %s" % (self._device_id,
                                                 target_dir + "/cmd.sh"))
        # May have false positive using the following error word
        for line in out.split("\n")[:-10]:
            if ("Aborted" in line
                    or "FAILED" in line or "Segmentation fault" in line):
                raise Exception(line)

    def pull(self, target, out_dir):
        sn = self._device_id
        execute("adb -s %s pull %s %s" % (sn, target.path, out_dir), False)

    def mkdir(self, dirname):
        sn = self._device_id
        execute("adb -s %s shell mkdir -p %s" % (sn, dirname))

    def info(self):
        sn = self._device_id
        output = execute("adb -s %s shell getprop" % sn, False)
        raw_props = output.split("\n")
        props = {}
        p = re.compile(r'\[(.+)\]: \[(.+)\]')
        for raw_prop in raw_props:
            m = p.match(raw_prop)
            if m:
                props[m.group(1)] = m.group(2)
        return props

class ArmLinuxDevice(Device):
    devices = {}

    def __init__(self, device_id, target_abi):
        super(ArmLinuxDevice, self).__init__(device_id, target_abi)

    @staticmethod
    def list_devices():
        device_ids = []
        print("!!!", ArmLinuxDevice.devices)
        for dev_name, dev_info in ArmLinuxDevice.devices.items():
            address = dev_info["address"]
            username = dev_info["username"]
            device_ids.append("%s@%s" % (username, address))
        return device_ids

    @staticmethod
    def set_devices(devices):
        ArmLinuxDevice.devices = devices

    def install(self, target, install_dir, install_deps=False):
        install_dir = os.path.abspath(install_dir)
        ip = self._device_id

        execute("ssh %s mkdir -p %s" % (ip, install_dir))
        execute("scp -r %s %s:%s" % (target.path, ip, install_dir))
        for lib in target.libs:
            execute("scp -r %s:%s" % (lib, install_dir))

        target.path = "%s/%s" % (install_dir, os.path.basename(target.path))
        target.libs = ["%s/%s" % (install_dir, os.path.basename(lib))
                       for lib in target.libs]
        target.envs.append("LD_LIBRARY_PATH=%s" % install_dir)

        return target

    def run(self, target):
        execute("ssh %s %s" % (self._device_id, target))

    def pull(self, target, out_dir):
        sn = self._device_id
        execute("scp -r %s:%s %s" % (sn, target.path, out_dir))

    def mkdir(self, dirname):
        sn = self._device_id
        execute("ssh %s mkdir -p %s" % (sn, dirname))

def device_class(target_abi):
    device_dispatch = {
        "host": "HostDevice",
        "armeabi-v7a": "AndroidDevice",
        "arm64-v8a": "AndroidDevice",
        "arm-linux-gnueabihf": "ArmLinuxDevice",
        "aarch64-linux-gnu": "ArmLinuxDevice"
    }

    if target_abi not in device_dispatch:
        raise ValueError(
            "target_abi should be one of %s" % device_dispatch.keys())

    return globals()[device_dispatch[target_abi]]

def create_device(target_abi, device_id=None):
    return device_class(target_abi)(device_id, target_abi)

def choose_devices(target_abi, target_ids):
    device_clazz = device_class(target_abi)
    devices = device_clazz.list_devices()

    if target_ids == "all":
        run_devices = devices
    elif target_ids == "random":
        unlocked_devices = [dev for dev in devices if
                            not util.is_device_locked(dev)]
        if unlocked_devices:
            run_devices = [random.choice(unlocked_devices)]
        else:
            run_devices = [random.choice(devices)]
    else:
        device_id_list = [dev.strip() for dev in target_ids.split(",")]
        run_devices = [dev for dev in device_id_list if dev in devices]

    return run_devices


import torch
from torchsummary import summary  # torchsummary-1.5.1
from thop import profile #  thop-0.0.31
from ptflops import get_model_complexity_info # ptflops-0.6.4

from u2net import U2NETP
from u2net_groupconv import U2NET as U2NETGroupConv
from u2net_dsconv import U2NET as U2NETDSConv

to_device = 'cpu'
input_size = (3, 400, 400)
verbose = False

models = {
    "U2NETP": U2NETP(3, 1),
    "U2NETGroupConv": U2NETGroupConv(3, 1),
    "U2NETDSConv": U2NETDSConv(3, 1)
}

for key in models.keys():
    model = models[key].to(to_device)

    # thop
    input_tensor = torch.randn(1, *input_size, device=to_device)
    flops, params = profile(model, (input_tensor, ), verbose=verbose)
    print(f"{key} flops: {flops}, params: {params}")

    # ptflops
    macs, params = get_model_complexity_info(model, input_size, as_strings=True,
                                           print_per_layer_stat=False, verbose=verbose)
    print(key)
    print('{:<30}  {:<8}'.format('Computational complexity: ', macs))
    print('{:<30}  {:<8}'.format('Number of parameters: ', params))

    #  torchsummary
    # summary(model, input_size=input_size,  device=to_device) # (channels, h, w)

# U2NETP
# Input size (MB): 1.83 (1, 3, 400, 400)
# Forward/backward pass size (MB): 2270.41
# Params size (MB): 4.32
# Estimated Total Size (MB): 2276.56
# Computational complexity (GMac): 31.16
# Number of parameters (M): 1.13

# U2NETGroupConv
# Input size (MB): 1.83 (1, 3, 400, 400)
# Forward/backward pass size (MB): 3136.27
# Params size (MB): 84.19
# Estimated Total Size (MB): 3222.29
# Computational complexity (GMac):  52.36
# Number of parameters (M): 22.07

# U2NETDSConv
# Input size (MB): 1.83 (1, 3, 400, 400)
# Forward/backward pass size (MB): 4042.85
# Params size (MB): 19.61
# Estimated Total Size (MB): 4064.29
# Computational complexity (GMac): 11.9
# Number of parameters (M): 5.14


import ROOT as rt
import sys
rt.gROOT.SetBatch(True)
rt.gStyle.SetOptTitle(1)

def createInFileName(mZ, mD, rI, aD):
	return "PrivateSamples.SVJ_mZprime-"+str(mZ)+"_mDark-"+mD+"_rinv-"+rI+"_alpha-"+aD+"_n-1000_0_RA2AnalysisTree.root"

def createImageName(mZ, mD, rI, aD, plotName):
	return "PS_SVJ_JetMTTests_mZprime-"+str(mZ)+"_mDark-"+mD+"_rinv-"+rI+"_alpha-"+aD+"_"+plotName+".png"

def plotTwo2D(hist1, hist2,outputName):
	canv =rt.TCanvas()
	hist1.SetMarkerStyle(2)
	hist2.SetMarkerStyle(3)
	hist1.SetMarkerColorAlpha(rt.kBlue,0.5)
	hist2.SetMarkerColorAlpha(rt.kGreen,0.5)
	hist1.Draw()
	hist2.Draw("same")
	canv.SaveAs(outputName)

def makeCornerPlot3D(inputHisto, outputName):
	#make canvas, and split into 9 sections:
	canvas = rt.TCanvas("canvas","canvas", 900,900)
	#  x <>  <>   1 2 3
	# yx  y  <>   4 5 6
	# zx zy   z   7 8 9
	canvas.Divide(3,3,0,0)
	T = inputHisto.GetTitle()
	X = inputHisto.GetXaxis().GetTitle()
	Y = inputHisto.GetYaxis().GetTitle()
	Z = inputHisto.GetZaxis().GetTitle()

	hist_X = inputHisto.Project3D("x")
	hist_X.GetXaxis().SetLabelSize(0)
	hist_X.GetYaxis().CenterTitle()
	hist_X.SetTitle(";;"+X)

	#hist_XY = inputHisto.Project3D("xy")
	#hist_XY.GetYaxis().SetLabelSize(0)
	#hist_XY.GetXaxis().SetLabelSize(0)
	#hist_XY.SetTitle(";;")

	#hist_XZ = inputHisto.Project3D("xz")
	#hist_XZ.GetYaxis().SetLabelSize(0)
	#hist_XZ.GetXaxis().SetLabelSize(0)
	#hist_XZ.SetTitle(";;")

	hist_YX = inputHisto.Project3D("yx")
	hist_YX.GetXaxis().SetLabelSize(0)
	hist_YX.GetYaxis().CenterTitle()
	hist_YX.SetTitle(";"+X+";"+Y)

	hist_Y = inputHisto.Project3D("y")
	hist_Y.GetYaxis().SetLabelSize(0)
	hist_Y.GetXaxis().SetLabelSize(0)
	hist_Y.SetTitle(";;")

	#hist_YZ = inputHisto.Project3D("yz")
	#hist_YZ.GetXaxis().SetLabelSize(0)
	#hist_YZ.GetYaxis().SetLabelSize(0)
	#hist_YZ.SetTitle(";;")

	hist_ZX = inputHisto.Project3D("zx")
	hist_ZX.GetXaxis().CenterTitle()
	hist_ZX.GetYaxis().CenterTitle()
	hist_ZX.SetTitle(";"+X+";"+Z)

	hist_ZY = inputHisto.Project3D("zy")
	hist_ZY.GetYaxis().SetLabelSize(0)
	hist_ZY.GetXaxis().CenterTitle()
	hist_ZY.SetTitle(";"+Y+";")

	hist_Z = inputHisto.Project3D("z")
	hist_Z.GetYaxis().SetLabelSize(0)
	hist_Z.GetXaxis().CenterTitle()
	hist_Z.SetTitle(";"+Z+";")

	canvas.cd(1)
	hist_X.DrawCopy()
	#canvas.cd(2)
	#hist_XY.DrawCopy("cont")
	#canvas.cd(3)
	#hist_XZ.DrawCopy("cont")
	canvas.cd(4)
	hist_YX.DrawCopy("cont")
	canvas.cd(5)
	hist_Y.DrawCopy()
	#canvas.cd(6)
	#hist_YZ.DrawCopy("cont")
	canvas.cd(7)
	hist_ZX.DrawCopy("cont")
	canvas.cd(8)
	hist_ZY.DrawCopy("cont")
	canvas.cd(9)
	hist_Z.DrawCopy()
	canvas.SaveAs(outputName)

def makeCornerPlot2D(inputHisto, outputName):
	#make canvas, and split into 4 sections:
	canvas = rt.TCanvas("canvas","canvas", 900,900)
	#  x <>   1 2
	# yx  y   3 4

	canvas.Divide(2,2,0,0)

	T = inputHisto.GetTitle()
	X = inputHisto.GetXaxis().GetTitle()
	Y = inputHisto.GetYaxis().GetTitle()

	hist_YX = inputHisto
	hist_YX.SetTitle("")
	hist_YX.GetXaxis().CenterTitle()
	hist_YX.GetYaxis().CenterTitle()
	hist_X = inputHisto.ProjectionX()
	hist_X.GetXaxis().SetLabelSize(0)
	hist_X.GetYaxis().CenterTitle()
	hist_Y = inputHisto.ProjectionY()
	hist_Y.GetYaxis().SetLabelSize(0)
	hist_Y.GetXaxis().CenterTitle()

	canvas.cd(3)
	hist_YX.DrawCopy("cont")
	canvas.cd(1)
	hist_X.DrawCopy()

	canvas.cd(4)
	hist_Y.DrawCopy("hbar2")

	canvas.SaveAs(outputName)

def trans_mass_Njet(jets, met, metPhi):
	visible = rt.TLorentzVector()
	for jet in jets:
		visible += jet
	jetMass2 = visible.M2()
	term1 = rt.TMath.Sqrt(jetMass2 + visible.Pt()**2) * met
	term2 = rt.TMath.Cos(metPhi-visible.Phi())*visible.Pt()*met
	return rt.TMath.Sqrt(jetMass2 + 2*(term1 - term2))

def drawHistos(histo, plotName, logY = False, stack = False):
	canvas = rt.TCanvas()
	canvas.SetCanvasSize(900,600)
	histStack = rt.THStack()
	histStack.SetTitle(histo[0].GetTitle())
	for hist in histo:
		histStack.Add(hist)
	if logY == True:
		canvas.SetLogy()
	histStack.SetTitle(plotName)
	if not stack:
		histStack.Draw("nostack")
	else:
		histStack.Draw()
	histStack.GetXaxis().SetTitle(histo[0].GetXaxis().GetTitle())
	canvas.BuildLegend(0.75,0.75,0.9,0.9,"")
	canvas.SaveAs(createImageName(mZprime, mDark, rinv, alpha, plotName))

def isNear(val1, val2, delta):
	return (abs(val1-val2)<delta)

rt.gStyle.SetOptStat(0)

def absDphi(phi1, phi2):
	dphi = phi1-phi2
	if dphi > rt.TMath.Pi():
		dphi -= 2*rt.TMath.Pi()
	elif dphi < -rt.TMath.Pi():
		dphi += 2*rt.TMath.Pi()
	dphi = rt.TMath.Abs(dphi)
	return dphi

#step1, know what data set we're looking at

mZprime = 3000
mDark = '20'
rinv = '0p3'
alpha = '0p2'

inFile = rt.TFile("../"+createInFileName(mZprime, mDark,rinv,alpha),"read")
tree = inFile.Get("TreeMaker2/PreSelection")

nEvents = tree.GetEntries()
print("Total Events: " + str(nEvents))

# initilize histograms

hist_MT_All = rt.TH1F("All MT", "All;MT;a.u.",100,0,1.3*mZprime)
hist_MT_2jets = rt.TH1F("2jets MT", "nJets==2;MT;a.u.",100,0,1.3*mZprime)
hist_MT_3jets = rt.TH1F("3jets MT", "nJets==3;MT;a.u.",100,0,1.3*mZprime)
hist_MT_4jets = rt.TH1F("4jets MT", "nJets>=4;MT;a.u.",100,0,1.3*mZprime)

hist_MT3jet_3jets = rt.TH1F("3jets MT3jet", "3jet MT;MT",100,0,1.8*mZprime)
hist_MTvsMT3jet_3jets = rt.TH2F("3jets MTvsMT3jet", ";MT;3Jet MT",100,0,1.3*mZprime,100,0,1.8*mZprime)
prof_MTvsMT3jet_3jets = rt.TProfile("3jets MTvsMT3jet profile","MT;3jetMT;a.u.",100,0,1.3*mZprime,0,1.8*mZprime)

hist_MVsEtaVsPhi = rt.TH3F("","T;Mass;Eta;Phi",100,0,30*int(mDark),100,-3,3,100,-rt.TMath.Pi(),rt.TMath.Pi())
hist_MtMt3Jet_dPhiMin2Jet = rt.TH3F("","T;MT;dPhiMin2jet;MT3Jet",100,0,1.3*mZprime,100,0,rt.TMath.Pi(),100,0,1.8*mZprime)
hist_MtMt3Jet_dPhiMin3Jet = rt.TH3F("","T;MT;dPhiMin3jet;MT3Jet",100,0,1.3*mZprime,100,0,rt.TMath.Pi(),100,0,1.8*mZprime)
hist_MtMt3Jet_MET = rt.TH3F("","T;MT;MET;MT3Jet",100,0,1.3*mZprime,100,0,0.7*mZprime,100,0,1.8*mZprime)

histList = []
histList.append(hist_MT_All)
histList.append(hist_MT_2jets)
histList.append(hist_MT_3jets)
histList.append(hist_MT_4jets)

histList.append(hist_MT3jet_3jets)

nEventsPassedPreSelection = 0
nEventsWithOnly2Jets = 0
nEventsWithOnly3Jets = 0
nEventsWith4OrMoreJets = 0

for iEvent in range(nEvents):
	tree.GetEvent(iEvent)
	if iEvent%1000==0:
		print("EventNumber:"+str(iEvent))

	#jetCollection = tree.GenJetsAK8
	jetCollection = tree.JetsAK8
	nJets = len(jetCollection)

	#some calculations to determine which histos to fill
	# PreSelection Cuts
	if not (len(tree.JetsAK8)>=2): #moreThan2Jets
		continue
	if not ((tree.JetsAK8[0].Pt() > 170.0) and (tree.JetsAK8[1].Pt() > 170.0)): #leadingJetsPtOver170
		continue
	if not (tree.MET/tree.MT_AK8 > 0.15): #METoverMTgreaterThan0p15
		continue
	if not ((len(tree.Electrons) + len(tree.Muons)) == 0): #leptonNotPresent
		continue
	nEventsPassedPreSelection += 1

	#do stuff for all events
	hist_MT_All.Fill(tree.MT_AK8)
	for jet in jetCollection:
		hist_MVsEtaVsPhi.Fill(jet.M(),jet.Eta(),jet.Phi())
	if nJets == 2: #do stuff here for events with 2 jets
		nEventsWithOnly2Jets += 1
		hist_MT_2jets.Fill(tree.MT_AK8)
	elif nJets == 3: #do stuff here for events wth 3 jets
		nEventsWithOnly3Jets += 1
		DeltaPhi3_AK8 = rt.TMath.Abs(jetCollection[2].Phi()-tree.METPhi)
		DeltaPhiMin_3jet = min(DeltaPhi3_AK8, tree.DeltaPhi1_AK8, tree.DeltaPhi2_AK8)
		hist_MT_3jets.Fill(tree.MT_AK8)
		MT3jet = trans_mass_Njet(jetCollection[0:3],tree.MET,tree.METPhi)
		hist_MT3jet_3jets.Fill(MT3jet)
		hist_MTvsMT3jet_3jets.Fill(tree.MT_AK8,MT3jet)
		hist_MtMt3Jet_dPhiMin2Jet.Fill(tree.MT_AK8,tree.DeltaPhiMin_AK8,MT3jet)
		hist_MtMt3Jet_dPhiMin3Jet.Fill(tree.MT_AK8,DeltaPhiMin_3jet,MT3jet)
		hist_MtMt3Jet_MET.Fill(tree.MT_AK8,tree.MET,MT3jet)
		prof_MTvsMT3jet_3jets.Fill(tree.MT_AK8,MT3jet,1)
	elif nJets >= 4: # do stuff here for events with 4+ jets
		nEventsWith4OrMoreJets += 1
		DeltaPhi3_AK8 = rt.TMath.Abs(jetCollection[2].Phi()-tree.METPhi)
		DeltaPhiMin_3jet = min(DeltaPhi3_AK8, tree.DeltaPhi2_AK8, tree.DeltaPhi1_AK8)
		DeltaPhi4_AK8 = rt.TMath.Abs(jetCollection[3].Phi()-tree.METPhi)
		DeltaPhiMin_4jet = min(DeltaPhi4_AK8, DeltaPhi3_AK8, tree.DeltaPhi2_AK8, tree.DeltaPhi1_AK8)
"""

	#find the HV quarks
	nHVquark = 0
	nbarHVquark = 0
	for iPart in range(len(tree.GenParticles)):
		if tree.GenParticles_PdgId[iPart] == 4900101:
			hvQuark = tree.GenParticles[iPart]
			nHVquark += 1
		elif tree.GenParticles_PdgId[iPart] == -4900101:
			barhvQuark = tree.GenParticles[iPart]
			nbarHVquark += 1
	if (nHVquark != 1) or (nbarHVquark != 1): # skip events that don't have only 2 unstable HV quarks
		continue

	deltaRjetHVquark = [hvQuark.DeltaR(jetCollection[0]),hvQuark.DeltaR(jetCollection[1]),hvQuark.DeltaR(jetCollection[2])]
	deltaRjetbarHVquark = [barhvQuark.DeltaR(jetCollection[0]),barhvQuark.DeltaR(jetCollection[1]),barhvQuark.DeltaR(jetCollection[2])]

	deltaRjethvQuarkMinIndex = deltaRjetHVquark.index(min(deltaRjetHVquark))
	deltaRjetbarhvQuarkMinIndex = deltaRjetbarHVquark.index(min(deltaRjetbarHVquark))

	if not (deltaRjethvQuarkMinIndex == deltaRjetbarhvQuarkMinIndex):
		MT_HVQuarks_Jets = trans_mass_Njet([jetCollection[deltaRjethvQuarkMinIndex], jetCollection[deltaRjetbarhvQuarkMinIndex]],tree.MET,tree.METPhi)

	# fill 'All' histograms here
	if (tree.DeltaPhi1_AK8 == DeltaPhiMin_3jet):
	elif (tree.DeltaPhi2_AK8 == DeltaPhiMin_3jet):
	elif (DeltaPhi3_AK8 == DeltaPhiMin_3jet):
"""

print("Events with 2 jets: " + str(nEventsWithOnly2Jets+nEventsWithOnly3Jets+nEventsWith4OrMoreJets))
print("Events with only 2 jets: " + str(nEventsWithOnly2Jets))
print("Events with 3 jets: " + str(nEventsWithOnly3Jets+nEventsWith4OrMoreJets))
print("Events with only 3 jets: " + str(nEventsWithOnly3Jets))
print("Events with 4 jets: " + str(nEventsWith4OrMoreJets))

#step 4, normalize & color all histograms

for histo in histList:
	name = histo.GetName()
	if "All" in name:
		histo.SetLineColor(rt.kBlack)
	elif "2jets" in name:
		histo.SetLineColor(rt.kBlue)
	elif "3jets" in name:
		histo.SetLineColor(rt.kGreen)
	elif "4jets" in name:
		histo.SetLineColor(rt.kRed)
	else:
		print(name + " doesn't contain 'All', '2jets', '3jets', or '4jets'")
	area = histo.GetEntries()
	#area = 1
	try:
		histo.Scale(1/area)
	except ZeroDivisionError:
		print("Empty histogram: " + histo.GetName())

#5: save histogram plots
hist_MT3jet_3jets.SetLineColor(rt.kBlue)
#hist_MT4jet_4jets.SetLineColor(rt.kBlue)
drawHistos([hist_MT_All,hist_MT_2jets,hist_MT_3jets],"MT",True)
drawHistos([hist_MT_All,hist_MT3jet_3jets,hist_MT_3jets],"MT_3jets",True)

c1 = rt.TCanvas()
hist_MTvsMT3jet_3jets.Draw("colz")
c1.SaveAs(createImageName(mZprime, mDark, rinv, alpha, "Mtvs3jetMT_3jets"))
prof_MTvsMT3jet_3jets.Draw()
c1.SaveAs(createImageName(mZprime, mDark, rinv, alpha, "Mtvs3jetMT_3jets_profile"))

makeCornerPlot3D(hist_MVsEtaVsPhi,createImageName(mZprime, mDark, rinv, alpha, "3dcorner_example"))
makeCornerPlot2D(hist_MTvsMT3jet_3jets,createImageName(mZprime, mDark, rinv, alpha, "2dcorner_3jetsMTvsMT3Jet"))
makeCornerPlot3D(hist_MtMt3Jet_dPhiMin2Jet,createImageName(mZprime, mDark, rinv, alpha, "3dcorner_dPhiMin2jet"))
makeCornerPlot3D(hist_MtMt3Jet_dPhiMin3Jet,createImageName(mZprime, mDark, rinv, alpha, "3dcorner_dPhiMin3jet"))
makeCornerPlot3D(hist_MtMt3Jet_MET,createImageName(mZprime, mDark, rinv, alpha, "3dcorner_MET"))

plotTwo2D(hist_MtMt3Jet_MET.Project3D("xy"),hist_MtMt3Jet_MET.Project3D("zy"), createImageName(mZprime, mDark, rinv, alpha, "double2Dplot_MET"))
plotTwo2D(hist_MtMt3Jet_dPhiMin2Jet.Project3D("xy"),hist_MtMt3Jet_dPhiMin2Jet.Project3D("zy"), createImageName(mZprime, mDark, rinv, alpha, "double2Dplot_dPhiMin2Jet"))
plotTwo2D(hist_MtMt3Jet_dPhiMin3Jet.Project3D("xy"),hist_MtMt3Jet_dPhiMin3Jet.Project3D("zy"), createImageName(mZprime, mDark, rinv, alpha, "double2Dplot_dPhiMin3jet"))

inFile.Close()


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 24 02:29:57 2020

@author: gutia
"""

import pandas as pd
import plotly as py
from plotly import tools
import plotly.graph_objs as go
#https://plot.ly/python/
from ForestTrade.config import token
from ForestTrade.config import oanda_login as account
from ForestTrade.config import var_prod_1
from ForestTrade.file_directory import file_name
import oandapyV20.endpoints.positions as positions

import oandapyV20
import oandapyV20.endpoints.trades as trades
import pandas as pd
#import matplotlib.pyplot as plt
import numpy as np
import time
import oandapyV20.endpoints.forexlabs as labs
import oandapyV20.endpoints.instruments as instruments
import oandapyV20.definitions.instruments as definstruments
#import datetime module
import datetime
from sklearn.model_selection import train_test_split

# =============================================================================
# pd.set_option('display.max_rows', 1000)
# pd.set_option('display.width', None)
# pd.set_option('display.max_colwidth', 1000)
# =============================================================================

#initiating API connection and defining trade parameters
CandlestickGranularity = (definstruments.CandlestickGranularity().definitions.keys())

#initiating API connection and defining trade parameters
client = oandapyV20.API(token.token,environment="practice")
account_id = account.oanda_pratice_account_id

#Globle variable
TRADING_INSTRUMENT = var_prod_1.TRADING_INSTRUMENT
NUM_SHARES_PER_TRADE = var_prod_1.NUM_SHARES_PER_TRADE

def candles(instrument):
    params = {"count": 5000,"granularity": list(CandlestickGranularity)[13]} #granularity is in 'M15'; it can be in seconds S5 - S30, minutes M1 - M30, hours H1 - H12, days D[18], weeks W or months M
    candles = instruments.InstrumentsCandles(instrument=instrument,params=params)
    client.request(candles)
    ohlc_dict = candles.response["candles"]
    ohlc = pd.DataFrame(ohlc_dict)
    ohlc_df = ohlc.mid.dropna().apply(pd.Series)
    ohlc_df["volume"] = ohlc["volume"]
    ohlc_df.index = ohlc["time"]
    ohlc_df = ohlc_df.apply(pd.to_numeric)
    return ohlc_df

df = candles(TRADING_INSTRUMENT)
df = df.reset_index()
df.columns= ['date','open','high','low','close','volume']
df = df.set_index(df.date)
df = df[['open','high','low','close','volume']]
df = df.drop_duplicates(keep=False)

ma= df.close.rolling(center=False,window=30).mean()

trace0 = go.Ohlc(x=df.index, open=df.open, high=df.high, low=df.low,close=df.close,name="Currency Quote")
trace1 = go.Scatter(x=df.index, y=ma)
trace2 = go.Bar(x=df.index, y=df.volume)

data = [trace0]

fig = tools.make_subplots(rows=2,cols=1,shared_xaxes=True)
fig.append_trace(trace0,1,1)
fig.append_trace(trace1,1,1)
fig.append_trace(trace2,2,1)

#ignoring warnings
import warnings
warnings.simplefilter('ignore')

#importing neccesary modules
import sys
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import seaborn as sns

import sklearn
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.svm import SVR
from sklearn.ensemble import BaggingRegressor, RandomForestRegressor
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import GridSearchCV, train_test_split

import joblib

import xgboost
from xgboost import XGBRegressor, DMatrix

def generate_features(df):
    """ Generate features for a stock/index/currency/commodity based on historical price and performance
    Args:
        df (dataframe with columns "open", "close", "high", "low", "volume")
    Returns:
        dataframe, data set with new features
    """
    df_new = pd.DataFrame()

    # 6 original features
    df_new['open'] = df['open']
    df_new['open_1'] = df['open'].shift(1)
    df_new['close_1'] = df['close'].shift(1)
    df_new['high_1'] = df['high'].shift(1)
    df_new['low_1'] = df['low'].shift(1)
    df_new['volume_1'] = df['volume'].shift(1)

    # 50 original features
    # average price
    df_new['avg_price_5'] = df['close'].rolling(window=5).mean().shift(1)
    df_new['avg_price_30'] = df['close'].rolling(window=21).mean().shift(1)
    df_new['avg_price_90'] = df['close'].rolling(window=63).mean().shift(1)
    df_new['avg_price_365'] = df['close'].rolling(window=252).mean().shift(1)

    # average price ratio
    df_new['ratio_avg_price_5_30'] = df_new['avg_price_5'] / df_new['avg_price_30']
    df_new['ratio_avg_price_905_'] = df_new['avg_price_5'] / df_new['avg_price_90']
    df_new['ratio_avg_price_5_365'] = df_new['avg_price_5'] / df_new['avg_price_365']
    df_new['ratio_avg_price_30_90'] = df_new['avg_price_30'] / df_new['avg_price_90']
    df_new['ratio_avg_price_30_365'] = df_new['avg_price_30'] / df_new['avg_price_365']
    df_new['ratio_avg_price_90_365'] = df_new['avg_price_90'] / df_new['avg_price_365']

    # average volume
    df_new['avg_volume_5'] = df['volume'].rolling(window=5).mean().shift(1)
    df_new['avg_volume_30'] = df['volume'].rolling(window=21).mean().shift(1)
    df_new['avg_volume_90'] = df['volume'].rolling(window=63).mean().shift(1)
    df_new['avg_volume_365'] = df['volume'].rolling(window=252).mean().shift(1)

    #average volume ratio
    df_new['ratio_avg_volume_5_30'] = df_new['avg_volume_5'] / df_new['avg_volume_30']
    df_new['ratio_avg_volumee_5_90'] = df_new['avg_volume_5'] / df_new['avg_volume_90']
    df_new['ratio_avg_volume_5_365'] = df_new['avg_volume_5'] / df_new['avg_volume_365']
    df_new['ratio_avg_volume_30_90'] = df_new['avg_volume_30'] / df_new['avg_volume_90']
    df_new['ratio_avg_volume_30_365'] = df_new['avg_volume_30'] / df_new['avg_volume_365']
    df_new['ratio_avg_volume_90_365'] = df_new['avg_volume_90'] / df_new['avg_volume_365']

    # standard deviation of prices
    df_new['std_price_5'] = df['close'].rolling(window=5).std().shift(1)
    df_new['std_price_30'] = df['close'].rolling(window=21).std().shift(1)
    df_new['std_price_90'] = df['close'].rolling(window=63).std().shift(1)
    df_new['std_price_365'] = df['close'].rolling(window=252).std().shift(1)

    # standard deviation ratio of prices
    df_new['ratio_std_price_5_30'] = df_new['std_price_5'] / df_new['std_price_30']
    df_new['ratio_std_price_5_90'] = df_new['std_price_5'] / df_new['std_price_90']
    df_new['ratio_std_price_5_365'] = df_new['std_price_5'] / df_new['std_price_365']
    df_new['ratio_std_price_30_90'] = df_new['std_price_30'] / df_new['std_price_90']
    df_new['ratio_std_price_30_365'] = df_new['std_price_30'] / df_new['std_price_365']
    df_new['ratio_std_price_90_365'] = df_new['std_price_90'] / df_new['std_price_365']

    # standard deviation of volumes
    df_new['std_volume_5'] = df['volume'].rolling(window=5).std().shift(1)
    df_new['std_volume_30'] = df['volume'].rolling(window=21).std().shift(1)
    df_new['std_volume_90'] = df['volume'].rolling(window=63).std().shift(1)
    df_new['std_volume_365'] = df['volume'].rolling(window=252).std().shift(1)

    #standard deviation ratio of volumes
    df_new['ratio_std_volume_5_30'] = df_new['std_volume_5'] / df_new['std_volume_30']
    df_new['ratio_std_volume_5_90'] = df_new['std_volume_5'] / df_new['std_volume_90']
    df_new['ratio_std_volume_5_365'] = df_new['std_volume_5'] / df_new['std_volume_365']
    df_new['ratio_std_volume_30_90'] = df_new['std_volume_30'] / df_new['std_volume_90']
    df_new['ratio_std_volume_30_365'] = df_new['std_volume_30'] / df_new['std_volume_365']
    df_new['ratio_std_volume_90_365'] = df_new['std_volume_90'] / df_new['std_volume_365']

    # return
    df_new['return_1'] = ((df['close'] - df['close'].shift(1)) / df['close'].shift(1)).shift(1)
    df_new['return_5'] = ((df['close'] - df['close'].shift(5)) / df['close'].shift(5)).shift(1)
    df_new['return_30'] = ((df['close'] - df['close'].shift(21)) / df['close'].shift(21)).shift(1)
    df_new['return_90'] = ((df['close'] - df['close'].shift(63)) / df['close'].shift(63)).shift(1)
    df_new['return_365'] = ((df['close'] - df['close'].shift(252)) / df['close'].shift(252)).shift(1)

    #average of return
    df_new['moving_avg_5'] = df_new['return_1'].rolling(window=5).mean()
    df_new['moving_avg_30'] = df_new['return_1'].rolling(window=21).mean()
    df_new['moving_avg_90'] = df_new['return_1'].rolling(window=63).mean()
    df_new['moving_avg_365'] = df_new['return_1'].rolling(window=252).mean()

    # the target
    df_new['close'] = df['close']
    df_new = df_new.dropna(axis=0)
    return df_new

data = generate_features(df)
#data.head()

data_train, data_test  = train_test_split(data, test_size=0.2)
X_train = data_train.drop('close', axis='columns')
y_train = data_train.close

X_test = data_test.drop('close', axis='columns')
y_test = data_test.close
#X_train.shape, X_test.shape, y_train.shape, y_test.shape

#initiating standard scaler
scaler = StandardScaler()

#fit the scaler in training features
scaler.fit(X_train)

#Rescale both sets using the trained scaler
X_scaled_train = scaler.transform(X_train)
X_scaled_test = scaler.transform(X_test)

#from sklearn.linear_model import LinearRegression
lin = LinearRegression()

lin.fit(X_scaled_train, y_train)
predictions_lin = lin.predict(X_scaled_test)

print('RMSE: {0:.3f}'.format(mean_squared_error(y_test, predictions_lin)**0.5))
print('MAE: {0:.3f}'.format(mean_absolute_error(y_test, predictions_lin)))
print('R^2: {0:.3f}'.format(r2_score(y_test, predictions_lin)))

dates = data_test.index.values
plt.figure(figsize = (18,9))
plt.style.use('seaborn-whitegrid')
plot_truth, = plt.plot(dates, y_test)
plot_lin, = plt.plot(dates, predictions_lin)
plt.legend([plot_truth, plot_lin], ['Truth', 'Linear Regression'])
plt.title('Gold price : Prediction vs Truth - Linear Regression')
plt.show()

last_row = data.tail(1)
predictions_lin[-1]


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

# In[38]:

import matplotlib.pyplot as plt
import numpy as np
from sympy import *

# In[39]:

def f(x):
  if (x >= -2) & (x <= 0):
    return -x**2 + 2
  if (x >= 0) & (x <= 1):
    return 1 - 8*(x-0.5)**3
  if (x >= 1):
    return x**2 - 2*x + 1
  return None

def Pol(x, n):
  A = [[0]*(n+1) for i in range(n+1)]
  for i in range(n+1):
    A[i][0] = f(x[i])
    #print(x[i], ' ', A[i][0])
  for j in range(1,n+1):
    for i in range(n+1-j):
      A[i][j] = (A[i+1][j-1] - A[i][j-1])/(x[i+j]-x[i])
  print('P =',end=' ')
  z = Symbol('x')
  poly = 0
  res = Matrix.zeros(n+1,1)
  for i in range(n+1):
    res[i] = A[0][i]
    print(res[i].n(2) ,end='')
    term = res[i]
    for j in range(i):
      term *= z + x[j]
      if x[j] < 0:
        print('( x +',x[j].n(2)*(-1),')',end='')
      else:
        print('( x -',x[j].n(2),')',end='')
    if i != n:
      print(' +')
    poly += term
    print()
  return res

def p(x, x_0, coefs):
  res = 0
  for i in range(n+1):
    t = 1;
    for j in range(i):
      t *= (x - x_0[j])
    res += t*coefs[i]
  return res

# In[40]:

n = 11
a = -2
b = 3

#рівновіддалені вузли
x1 = Matrix.zeros(n+1, 1)
for i in range(n+1):
  x1[i] = a + i*(b-a)/n

#чебишовські вузли
x2 = Matrix.zeros(n+1, 1)
for i in range(n+1):
  x2[i] = (a+b)/2+(b-a)/2*cos((2*i + 1)*pi/(2*(n+1)))

print("Рівновіддалені вузли")
Matrix(x1).n(2)

# In[41]:

res1 = Pol(x1,n)

# In[42]:

print("Чебишовські вузли")
Matrix(x2).n(2)

# In[43]:

res2 = Pol(x2.n(14),n)

# In[44]:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

x = np.arange(a,b + 0.01 ,0.1)

ax.plot(x, p(x,x1,res1), 'r', label='P_n_E(x)')
ax.plot(x, p(x,x2,res2), 'b', label='P_n_T(x)')
ax.plot(x, [f(i) for i in x], 'g', label='f(x)')

plt.legend(loc='upper left')
plt.ylim([-5, 5])
ax.set_xlim([-5,5])

plt.show()

# In[45]:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

x = np.arange(a,b + 0.01 ,0.1)

ax.plot(x, [f(i) for i in x]-p(x,x1,res1), 'r', label='f(x) - P_n_E(x)')
ax.plot(x, [f(i) for i in x]-p(x,x2,res2), 'b', label='f(x) - P_n_T(x)')

plt.legend(loc='upper left')
plt.ylim([-5, 5])
ax.set_xlim([-5,5])

plt.show()

# In[46]:

def gauss(A, eq=0, var=0):
    n, m = len(A), len(A[0])-1

    if eq == n or var == m:
        if any(A[k][m] != 0 for k in range(eq, n)):
            return None

        if n >= m and A[m-1][m-1] != 0:
            return [A[k][m] for k in range(m)]

        return None

    for k in range(eq, n):
        if A[k][var] != 0:
            A[eq], A[k] = A[k], A[eq]
            break

    if A[eq][var] == 0:
        return gauss(A, eq, var+1)

    for i in range(var+1, m+1):
        A[eq][i] /= A[eq][var]
    A[eq][var] = 1

    for k in range(n):
        if k == eq or A[k][var] == 0: continue
        coef = A[k][var]
        for i in range(var, m+1):
            A[k][i] -= coef*A[eq][i]

    return gauss(A, eq+1, var+1)

# In[47]:

h = (b-a)/n
C = [[0] * n for i in range(n-1)]
for i in range(0,n-1):
    if i!=0: C[i][i-1] = h
    C[i][i] = 2*(h+h)
    if i!=n-2: C[i][i+1] = h
    C[i][n-1] = 6*((f(x1[i+2])-f(x1[i+1]))/h - (f(x1[i+1])-f(x1[i]))/h)

# In[48]:

c = [0, *gauss(C), 0]
_a = [f(i) for i in x1]
d = [0] + [(c[i]-c[i-1])/h for i in range(1, n+1)]
_b = [0] + [h*c[i]/2-h**2*d[i]/6+(f(x1[i])-f(x1[i-1]))/h for i in range(1,n+1)]

# In[53]:

def build_spline(a, b, c, d, x_i):
    def spline(x):
        a + b*(x-x_i) + c*(x-x_i)**2/2 + d*(x-x_i)**3/6
    return spline

#s = [build_spline(a[i], b[i], c[i], d[i], x1[i]) for i in range(1,n+1)]

#for spline in s:
#    print(spline(Symbol('x')))

def spline(a, b, c, d, x_i, x, draw = 0):
    if (draw == 1):
        print(a, " + ", b, "(x - ", x_i, ") + ", c, "(x - ", x_i, ")^2/2 + ", d,"(x - ", x_i, ")^3/6")
        print()
    return a + b*(x-x_i) + c*(x-x_i)**2/2 + d*(x-x_i)**3/6

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

x = np.arange(a,b + 0.01 ,0.05)
ax.plot(x, [f(i) for i in x], 'g', label='f(x)')

for i in range(1,n+1):
    x = np.arange(x1[i-1],x1[i] + 0.01 ,0.05)
    if i == 1: ax.plot(x, spline(_a[i], _b[i], c[i], d[i], x1[i], x, 1), 'r', label='s(x)')
    else : ax.plot(x, spline(_a[i], _b[i], c[i], d[i], x1[i], x, 1), 'r')

plt.legend(loc='upper left')
plt.ylim([-5, 5])
ax.set_xlim([-5,5])

plt.show()

# In[54]:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

for i in range(1,n+1):
    x = np.arange(x1[i-1],x1[i] + 0.01 ,0.05)
    f_part = [f(i) for i in x]
    if i == 1: ax.plot(x, f_part - spline(_a[i], _b[i], c[i], d[i], x1[i], x), 'r', label='f(x) - s(x)')
    else : ax.plot(x, f_part - spline(_a[i], _b[i], c[i], d[i], x1[i], x), 'r')

plt.legend(loc='upper left')
plt.ylim([-5, 5])
ax.set_xlim([-5,5])

plt.show()

# In[ ]:


# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="kluitel"
__date__ ="$Sep 19, 2013 3:00:56 PM$"

#if __name__ == "__main__":
#    print "Hello World"
import telnetlib
print("asdf")
tn = telnetlib.Telnet("10.193.183.222")

tn.write("admin")
#tn.write("nbv12345\n")
#print tn.read_all()