from math import atan2, cos, exp, sin, sqrt

import ngmix
import numpy as np
from numba import njit

@njit(fastmath=True)
def find_ellipmom1(pixels, x0, y0, Mxx, Mxy, Myy, res, conf, do_cov=False):
    F = res["F"]

    detM = Mxx * Myy - Mxy * Mxy
    res["wnorm"] = 1.0 / (2 * np.pi * sqrt(detM))

    Minv_xx = Myy / detM
    TwoMinv_xy = -Mxy / detM * 2.0
    Minv_yy = Mxx / detM

    n_pixels = pixels.size
    for i_pix in range(n_pixels):
        pixel = pixels[i_pix]

        umod = pixel["u"] - x0
        vmod = pixel["v"] - y0

        Minv_xx__x_x0__x_x0 = Minv_xx * umod * umod
        TwoMinv_xy__y_y0__x_x0 = TwoMinv_xy * vmod * umod
        Minv_yy__y_y0__y_y0 = Minv_yy * vmod * vmod

        rho2 = (
            Minv_yy__y_y0__y_y0 + TwoMinv_xy__y_y0__x_x0 + Minv_xx__x_x0__x_x0
        )

        res["npix"] += 1
        if rho2 < conf["max_moment_nsig2"]:
            weight = exp(-0.5 * rho2) * res["wnorm"] * pixel["area"]
            intensity = weight * pixel["val"]

            res["wsum"] += weight

            if not do_cov:
                res["sums"][0] += umod * intensity
                res["sums"][1] += vmod * intensity
                res["sums"][2] += umod * umod * intensity
                res["sums"][3] += umod * vmod * intensity
                res["sums"][4] += vmod * vmod * intensity
                res["sums"][5] += 1.0 * intensity
                res["sums"][6] += rho2 * rho2 * intensity

            else:
                w2 = weight * weight
                var = 1.0 / (pixel["ierr"] * pixel["ierr"])
                F[0] = umod
                F[1] = vmod
                F[2] = umod * umod
                F[3] = umod * vmod
                F[4] = vmod * vmod
                F[5] = 1.0
                F[6] = rho2 * rho2
                for i in range(7):
                    res["sums"][i] += intensity * F[i]
                    for j in range(7):
                        res["sums_cov"][i, j] += w2 * var * F[i] * F[j]

@njit(fastmath=True)
def find_ellipmom2(
    pixels,
    guess,
    resarray,
    confarray,
):
    """ """

    conf = confarray[0]
    res = resarray[0]

    convergence_factor = 1.0
    shiftscale0 = 0.0

    x0, y0, Mxx, Mxy, Myy, _ = guess
    x00 = x0
    y00 = y0
    do_cov = False
    for i in range(conf["maxiter"]):
        clear_result(res)
        find_ellipmom1(pixels, x0, y0, Mxx, Mxy, Myy, res, conf, do_cov)
        Bx, By, Cxx, Cxy, Cyy, Amp, rho4 = res["sums"]

        if Amp <= 0:
            res["flags"] = ngmix.flags.NONPOS_FLUX

        two_psi = atan2(2 * Mxy, Mxx - Myy)
        semi_a2 = 0.5 * ((Mxx + Myy) + (Mxx - Myy) * cos(two_psi)) + Mxy * sin(
            two_psi
        )
        semi_b2 = Mxx + Myy - semi_a2

        if semi_b2 <= 0:
            res["flags"] = ngmix.flags.NONPOS_SIZE

        shiftscale = sqrt(semi_b2)
        if res["numiter"] == 0:
            shiftscale0 = shiftscale

        dx = 2.0 * Bx / (Amp * shiftscale)
        dy = 2.0 * By / (Amp * shiftscale)
        dxx = 4 * (Cxx / Amp - 0.5 * Mxx) / semi_b2
        dxy = 4 * (Cxy / Amp - 0.5 * Mxy) / semi_b2
        dyy = 4 * (Cyy / Amp - 0.5 * Myy) / semi_b2

        if dx > conf["bound_correct_wt"]:
            dx = conf["bound_correct_wt"]
        if dx < -conf["bound_correct_wt"]:
            dx = -conf["bound_correct_wt"]
        if dy > conf["bound_correct_wt"]:
            dy = conf["bound_correct_wt"]
        if dy < -conf["bound_correct_wt"]:
            dy = -conf["bound_correct_wt"]
        if dxx > conf["bound_correct_wt"]:
            dxx = conf["bound_correct_wt"]
        if dxx < -conf["bound_correct_wt"]:
            dxx = -conf["bound_correct_wt"]
        if dxy > conf["bound_correct_wt"]:
            dxy = conf["bound_correct_wt"]
        if dxy < -conf["bound_correct_wt"]:
            dxy = -conf["bound_correct_wt"]
        if dyy > conf["bound_correct_wt"]:
            dyy = conf["bound_correct_wt"]
        if dyy < -conf["bound_correct_wt"]:
            dyy = -conf["bound_correct_wt"]

        if abs(dx) > abs(dy):
            convergence_factor = abs(dx)
        else:
            convergence_factor = abs(dy)
        convergence_factor = convergence_factor * convergence_factor

        if abs(dxx) > convergence_factor:
            convergence_factor = abs(dxx)
        if abs(dxy) > convergence_factor:
            convergence_factor = abs(dxy)
        if abs(dyy) > convergence_factor:
            convergence_factor = abs(dyy)

        convergence_factor = sqrt(convergence_factor)
        if shiftscale < shiftscale0:
            convergence_factor *= shiftscale0 / shiftscale

        x0 += dx * shiftscale
        y0 += dy * shiftscale
        Mxx += dxx * semi_b2
        Mxy += dxy * semi_b2
        Myy += dyy * semi_b2

        if (abs(x0 - x00) > conf["shiftmax"]) | (
            abs(y0 - y00) > conf["shiftmax"]
        ):
            res["flags"] = ngmix.flags.CEN_SHIFT

        res["numiter"] = i + 1

        if convergence_factor < conf["tol"]:
            if not do_cov:
                do_cov = True
                continue
            A = Amp
            rho4 /= Amp
            res["pars"][0] = x0
            res["pars"][1] = y0
            res["pars"][2] = Mxx
            res["pars"][3] = Mxy
            res["pars"][4] = Myy
            res["pars"][5] = A * rho4
            res["pars"][6] = rho4
            break

        if res["numiter"] == conf["maxiter"]:
            res["flags"] = ngmix.flags.MAXITER

@njit
def clear_result(res):
    """
    clear some fields in the result structure
    """
    res["npix"] = 0
    res["wsum"] = 0.0
    res["sums"][:] = 0.0
    res["sums_cov"][:, :] = 0.0
    res["pars"][:] = np.nan


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Aoc 2015 - 05

import sys
sys.path.append('..')
import utils

ISSUE=5

def word_is_nice(word:str) -> bool:

	has_three_vowels = False
	has_doubleletter = False
	has_restricted = False

	# search for restricted
	restricted = ['ab','cd','pq','xy']
	for rs in restricted:
		if word.find(rs) > -1:
			#~ print("Word `{}` contains restricted substring `{}`".format(word, rs))
			has_restricted = True
			break # shortcut

	if has_restricted:
		#~ print("Word `{}` contains restricted substring".format(word))
		return False # shortcut
		pass

	# search vowels
	vowel_count = 0
	vowels = "aeiou"
	for letter in word:
		if vowels.find(letter) > -1:
			#~ print("Word `{}` contains vowel `{}`".format(word, letter))
			vowel_count+=1
	if vowel_count>=3:
		has_three_vowels = True
	else:
		#~ print("Word `{}` does not contain 3 vowels".format(word))
		return False # shortcut
		pass

	# search doubles
	prev_letter = None
	for letter in word:
		if prev_letter is not None and letter == prev_letter:
			#~ print("Word `{}` contains double letter `{}`".format(word, letter))
			has_doubleletter = True
			break # shortcut
		prev_letter = letter

	if not has_doubleletter:
		#~ print("Word `{}` does not contain double letter".format(word))
		return False # shortcut
		pass

	# evaluate (if no shortcut is used)
	return has_three_vowels and has_doubleletter and not has_restricted
	return True

def solve_part_1(demo:bool) -> str:

	fn = utils.get_input_file(demo, ISSUE, True)
	print(fn)
	"""Do something here >>>"""

	words = utils.read_file_into_list(fn)
	print(words)

	count_nice = 0
	for word in words:
		nice = word_is_nice(word)
		print("Word `{}` is {}".format(word, 'nice' if nice else 'naughty'))
		if nice:
			count_nice+=1

	answer = count_nice

	"""<<< Do something here"""
	utils.print_answer(1, demo, answer)
	return answer

def word_is_nice2(word:str) -> bool:
	word = word.strip()
	wl = len(word)
	# ~ print("Word `{}`".format(word))
	hasDoubles = False
	hasCyclope = False

	# search for cyclopes (najit kyklopa jejednodussi - jeden linearni cyklus)
	i = 0
	while i < wl-2:
		cl = word[i]
		cr = word[i+2]
		# ~ print("compare cyclope `{}_{}`".format(cl,cr))
		if cl==cr:
			hasCyclope = True
			# ~ print("found cyclope `{}` on position {}".format(cl, i))
			break
		i+=1
	if not hasCyclope:
		# ~ print("Word `{}` does not contain cyclope".format(word))
		return False

	# search for doubles
	j = 0
	while j < wl-2:
		double = word[j:j+2]
		i = j+1 # ensure no overlap
		while i < wl-2:
			i+=1
			double2 = word[i:i+2]
			# ~ print("compare double `{}` to `{}`".format(double, double2))
			if double == double2:
				hasDoubles = True
				# ~ print("found double `{}` on position {} and {}".format(double, j, i))
				i=j=wl # cause break of both cycles
				break
		j+=1
	if not hasDoubles:
		# ~ print("Word `{}` does not contain doubles".format(word))
		return False

	return hasDoubles and hasCyclope

def solve_part_2(demo:bool) -> str:

	fn = utils.get_input_file(demo, ISSUE, True)
	print(fn)
	"""Do something here >>>"""

	words_are_nice = utils.read_file_into_list(fn, word_is_nice2)
	# ~ print(words_are_nice)

	answer = sum(words_are_nice)

	"""<<< Do something here"""
	utils.print_answer(2, demo, answer)
	return answer

def main(args):

	#solve_part_1(0)

	solve_part_2(0)

	return 0

if __name__ == '__main__':
	sys.exit(main(sys.argv))


import requests
import re
from bs4 import BeautifulSoup

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}
url = "https://finance.naver.com/sise/sise_market_sum.nhn?&page=1"
res = requests.get(url, headers = headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")

data_rows = soup.find("table", attrs={"class":"type_2"}).find("tbody").find_all("tr")

for data in data_rows:
    print(data)


import numpy as np

def py_cpu_nms(dets, thres=0.5):
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (y2 - y1 + 1) * (x2 - x1 + 1)
    keep = []

    index = (scores.argsort()[::-1]).astype(np.int32)

    while index.size > 0:
        i = index[0]
        keep.append(i)

        x11 = np.maximum(x1[i], x1[index[1:]])
        y11 = np.maximum(y1[i], y1[index[1:]])
        x22 = np.minimum(x2[i], x2[index[1:]])
        y22 = np.minimum(y2[i], y2[index[1:]])

        w = np.maximum(0, x22 - x11 + 1)
        h = np.maximum(0, y22 - y11 + 1)

        overlaps = w * h

        ious = overlaps / (areas[i] + areas[index[1:]] - overlaps)

        idx = np.where(ious <= thres)[0]

        index = index[idx+1] # index staty with 1

    return keep

import matplotlib.pyplot as plt

def plot_bbox(dets, c='k'):
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]

    plt.plot([x1, x2], [y1, y1], c)
    plt.plot([x1, x1], [y1, y2], c)
    plt.plot([x2, x2], [y1, y2], c)
    plt.plot([x1, x2], [y2, y2], c)

if __name__ == '__main__':

    # fake data
    boxes = np.array([
        [100,100,210,210,0.72],
        [250,250,420,420,0.8],
        [220,220,320,330,0.92],
        [100,100,210,210,0.72],
        [230,240,325,330,0.81],
        [220,230,315,340,0.9]
    ])

    plot_bbox(boxes, 'k')
    plt.show()
    keep = py_cpu_nms(boxes, thres=0.7)
    plot_bbox(boxes[keep], 'r')
    plt.show()


"""
Django settings for taask project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# Get the environment (development/demonstration/production)
ENVIRONMENT= os.environ['TAASK_ENVIRONMENT']

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ['TAASK_SECRET_KEY']

# SECURITY WARNING: don't run with debug turned on in production!
if ENVIRONMENT == 'development':
    DEBUG = True
    TEMPLATE_DEBUG = True
    ALLOWED_HOSTS = []
elif ENVIRONMENT == 'demonstration':
    DEBUG = False
    TEMPLATE_DEBUG = False
    ALLOWED_HOSTS = [
        '.taask.org',
    ]
else:
    DEBUG = False
    TEMPLATE_DEBUG = False
    ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tasks',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'taask.urls'

WSGI_APPLICATION = 'taask.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

HOME = os.environ['HOME']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '%s/.taask/taask.sqlite3' % HOME,
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = None

USE_I18N = True

USE_L10N = False

USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    )

STATIC_ROOT = os.environ['TAASK_STATIC_ROOT']

# Dates
# https://docs.djangoproject.com/en/1.7/ref/settings/#date-format

DATE_FORMAT = 'j M Y'
DATE_INPUT_FORMATS = (
    '%d %b %Y',
    )

DATETIME_FORMAT = 'j M Y'
DATETIME_INPUT_FORMATS = (
    '%d %b %Y',
    )

# Fixtures
# https://docs.djangoproject.com/en/1.7/howto/initial-data/
FIXTURE_DIRS = (
    os.path.join(BASE_DIR, 'fixtures'),
)


import os
from flask import Flask,render_template,request
import jsonify
import requests
import pickle
import numpy as np
import sklearn
from sklearn.preprocessing import StandardScaler
app=Flask(__name__)
model=pickle.load(open('regression_heart1.pkl','rb'))
@app.route('/',methods=['GET'])
def Home():
    return render_template('frontend.html')
standard_to=StandardScaler()
@app.route("/predict",methods=['POST'])
def predict():
    if request.method=="POST":
        Age=int(request.form['Age'])
        sex_0=request.form['sex_0']
        if(sex_0=='Female'):
            sex_0=1
            sex_1=0
        else:
            sex_0=0
            sex_1=1
        trestbp=int(request.form['trestbps'])
        cholestrol=int(request.form['chol'])
        maxheartrate=int(request.form['thalach'])
        oldpeaks=float(request.form['oldpeak'])
        cp_0=request.form['cp_0']
        if(cp_0=='typical_angina'):
            cp_0=1
            cp_1=0
            cp_2=0
            cp_3=0
        elif(cp_0=='Atypica_angina'):
            cp_0=0
            cp_1=1
            cp_2=0
            cp_3=0
        elif(cp_0=='Non_anginal'):
            cp_0=0
            cp_1=0
            cp_2=1
            cp_3=0
        else:
            cp_0=0
            cp_1=0
            cp_2=0
            cp_3=1
        fbs_0=request.form['fbs_0']
        if(fbs_0=='True'):
            fbs_0=1
            fbs_1=0
        else:
            fbs_0=0
            fbs_1=1
        restecg_0=request.form['restecg_0']
        if(restecg_0=='normal'):
            restecg_0=1
            restecg_1=0
            restecg_2=0
        elif(restecg_0=='ST-T wave abnormality'):
            restecg_0=0
            restecg_1=1
            restecg_2=0
        else:
            restecg_0=0
            restecg_1=0
            restecg_2=1
        exang_0=request.form['exang_0']
        if(exang_0=='Yes'):
            exang_0=1
            exang_1=0
        else:
            exang_0=0
            exang_1=1
        slope_0=request.form['slope_0']
        if(slope_0=='upsloping'):
            slope_0=1
            slope_1=0
            slope_2=0
        elif(slope_0=='Flat'):
            slope_0=0
            slope_1=1
            slope_2=0
        else:
            slope_0=0
            slope_1=0
            slope_2=1
        ca_0=request.form['ca_0']
        if(ca_0=='0'):
            ca_0=1
            ca_1=0
            ca_2=0
            ca_3=0
            ca_4=0
        elif(ca_0=='1'):
            ca_0=0
            ca_1=1
            ca_2=0
            ca_3=0
            ca_4=0
        elif(ca_0=='2'):
            ca_0=0
            ca_1=0
            ca_2=1
            ca_3=0
            ca_4=0
        elif(ca_0=='3'):
            ca_0=0
            ca_1=0
            ca_2=0
            ca_3=1
            ca_4=0
        elif(ca_0=='4'):
            ca_0=0
            ca_1=0
            ca_2=0
            ca_3=0
            ca_4=1
        thal_0=request.form['thal_0']
        if(thal_0=='0'):
            thal_0=1
            thal_1=0
            thal_2=0
            thal_3=0
        elif(thal_0=='1'):
            thal_0=0
            thal_1=1
            thal_2=0
            thal_3=0
        elif(thal_0=='2'):
            thal_0=0
            thal_1=0
            thal_2=1
            thal_3=0
        else:
            thal_0=0
            thal_1=0
            thal_2=0
            thal_3=1
        prediction=model.predict([[Age,sex_0,sex_1,trestbp,cholestrol,maxheartrate,oldpeaks,cp_0,cp_1,cp_2,cp_3,fbs_0,fbs_1,restecg_0,restecg_1,restecg_2,exang_0,exang_1,slope_0,slope_1,slope_2,ca_0,ca_1,ca_2,ca_3,ca_4,thal_0,thal_1,thal_2,thal_3]])
        result=round(prediction[0],2)
        if(result<1):
            return render_template('positive.html',prediction_text="less chance of heart disease")
        else:
            return render_template('high.html' , prediction_text="heavy chances of getting heart disease")
    else:
        return render_template('frontend.html')
if __name__=="__main__":
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0',port=port,debug=True)


import socket

IP_ADDRS = '127.0.0.1'
PORT_NUM = 2121
FULL_ADDRS = (IP_ADDRS, PORT_NUM)
PACKET_SIZE = 1024

# Just initializing the socket object
data_sock = socket.socket()
cmd_sock = socket.socket()

def client_connected_handler():
    return '220 Service ready.\r\n'

def command_parser(message):

    message_parts = (message.strip()).split(' ')
    ftp_command = message_parts[0]
    arguments = ' '.join(message_parts[1:])
    return (ftp_command, arguments)

def opts_command_handler(args):
    return '202 Command not implemented.\r\n'

def user_command_handler(args):
    return '331 User name okay, need password.\r\n'

def pass_command_handler(args):
    return '230 Login Successful.\r\n'

def port_command_handler(args):

    global data_sock

    args_splited = args.split(',')
    data_ip_parts = args_splited[:4]
    data_port_parts = [int(part) for part in args_splited[4:]]

    data_ip = '.'.join(data_ip_parts)
    data_port =  (data_port_parts[0] * 256) + data_port_parts[1]

    print('Data IP:', data_ip)
    print('Data port:', data_port)

    data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    data_sock.connect((data_ip, data_port))

    print('Data connection successfully stablished!')

    return '200 Connected.\r\n'

def quit_command_handler(args):
    return '200 Goodbye.\r\n'

def retr_command_handler(args):

    file_data = b''
    try:
        with open(args, 'rb') as retr_file:
            file_data = retr_file.read()
    except FileNotFoundError:
        data_sock.close()
        return '426 Transfer aborted: Requested file is not present.\r\n'
    else:
        retr_file.close()

    cmd_sock.send('150 Starting data transfer\r\n'.encode())
    data_sock.send(file_data)
    data_sock.close()

    return '226 Operation successful\r\n'

def stor_command_handler(args):

    cmd_sock.send('150 Starting data transfer\r\n'.encode())
    file_data = b''
    received_data = data_sock.recv(PACKET_SIZE)

    while (received_data):
        file_data += received_data
        received_data = data_sock.recv(PACKET_SIZE)

    data_sock.close()

    with open(args, 'wb') as stor_file:
        stor_file.write(file_data)
        stor_file.close()

    return '226 Operation successful\r\n'

FTP_COMMANDS = {'OPTS': opts_command_handler,
                'USER': user_command_handler,
                'PASS': pass_command_handler,
                'PORT': port_command_handler,
                'QUIT': quit_command_handler,
                'RETR': retr_command_handler,
                'STOR': stor_command_handler,
                }

server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print('Starting up on {} port {}'.format(*FULL_ADDRS))

server_sock.bind(FULL_ADDRS)
server_sock.listen(1)

while True:
    print('Waiting for client...')

    cmd_sock, client_addr = server_sock.accept()

    print('Client connected from', client_addr)

    cmd_sock.send(client_connected_handler().encode())

    function_handler = None

    while function_handler != FTP_COMMANDS['QUIT']:

        message = (cmd_sock.recv(PACKET_SIZE)).decode()
        print('Received Message:', message.strip())
        (ftp_command, arguments) = command_parser(message)

        try:
            function_handler = FTP_COMMANDS[ftp_command]
        except KeyError:
            print("I don't know how to answer to this command!")
            response = '502 Command not implemented.\r\n'
        else:
            response = function_handler(arguments)

        cmd_sock.send(response.encode())

    print('Closing connection...')
    cmd_sock.close()
    print('Connection closed!')


import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

from utils.config import Config

def get_optimizer_and_scheduler(cfg: Config, model: nn.Module, dataloader: DataLoader = None):
    epochs = cfg.general.epochs
    learning_rate = cfg.training.learning_rate
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, betas=(0.5, 0.999))

    scheduler = cfg.training.scheduler
    scheduler_type = scheduler["type"].lower()
    if scheduler_type == "decay":
        gamma = float(scheduler["gamma"])
        return optimizer, optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=gamma)
    elif scheduler_type == "onecycle":
        scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, max_lr=learning_rate,
                                                  epochs=epochs, steps_per_epoch=len(dataloader))
        # Hack because OneCycleLR requires updating every step, not epoch
        return optimizer, {"scheduler": scheduler, "interval": "step"}
    else:
        raise ValueError(f"Unknown scheduler type: {scheduler_type}.")


from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
import detector
from PyQt5.QtGui import QIcon
import time
class MyMainGUI(QDialog):

    question_cnt = 0

    def __init__(self, parent=None):
        super().__init__(parent)

        self.detect_on=QPixmap('public/detect_on.png')
        self.detect_off=QPixmap('public/detect_off.png')
        # 이모티콘
        self.neutral=QPixmap('public/neutral.png').scaledToWidth(200)
        self.question=QPixmap('public/question.png').scaledToWidth(200)
        self.doubt = QPixmap('public/doubt.png').scaledToWidth(200)
        self.tired=QPixmap('public/sleep.png').scaledToWidth(200)
        self.left=QPixmap('public/left.png').scaledToWidth(200)
        self.yes=QPixmap('public/yes.png').scaledToWidth(200)
        self.no=QPixmap('public/no.png').scaledToWidth(200)

        # 얼굴인식 여부
        self.detectDot = QLabel()
        self.detectDot.resize(10,10)
        self.detectDot.setPixmap(self.detect_off)
        self.detectText = QLabel('인식안됨', self)

        hbox = QHBoxLayout()
        hbox.addWidget(self.detectDot)
        hbox.addWidget(self.detectText)

        # 사용자 이모티콘
        self.userEmoticon = QLabel()
        self.userEmoticon.setPixmap(self.neutral)

        # 질문 버튼
        self.questionBtn = QPushButton(self)
        self.questionBtn.setIcon(QIcon('public/question.png'))

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        vbox.addWidget(self.userEmoticon)
        vbox.addWidget(self.questionBtn)
        self.setLayout(vbox)
        self.setGeometry(100, 50, 200, 300)

class Test:
    def __init__(self):
        name = ""

class MyMain(MyMainGUI):
    add_sec_signal = pyqtSignal()
    send_instance_singal = pyqtSignal("PyQt_PyObject")

    def __init__(self, parent=None):
        super().__init__(parent)

        self.questionBtn.clicked.connect(self.btn_clicked)

        # video 스레드
        self.th = Worker(parent=self)
        self.th.detect_changed.connect(self.detect_update)  # custom signal from worker thread to main thread
        self.th.state_changed.connect(self.state_update)  # custom signal from worker thread to main thread
        self.th.start()
        self.th.working = True

        self.show()

    # 질문 버튼
    def btn_clicked(self):
        self.question_cnt = 30
        self.userEmoticon.setPixmap(self.question)

    @pyqtSlot(str)
    def detect_update(self, msg):
        # 검출 안됨
        if msg == "0":
            self.detectDot.setPixmap(self.detect_off)
            self.detectText.setText("인식 안됨")
        elif msg == "1":
            self.detectDot.setPixmap(self.detect_on)
            self.detectText.setText("인식 중")

    @pyqtSlot(str)
    def state_update(self, msg):
        self.question_cnt -= 1
        if self.question_cnt > 0:
            return

        if msg == "1":
            self.userEmoticon.setPixmap(self.neutral)
        elif msg == "2":
            self.userEmoticon.setPixmap(self.doubt)
        elif msg == "3":
            self.userEmoticon.setPixmap(self.tired)
        elif msg == "4":
            self.userEmoticon.setPixmap(self.left)
        elif msg == "5":
            self.userEmoticon.setPixmap(self.yes)
        elif msg == "6":
            self.userEmoticon.setPixmap(self.no)

class Worker(QThread):
    detect_changed = pyqtSignal(str)
    state_changed = pyqtSignal(str)

    def __init__(self, detect = 0,state=0, parent=None):
        super().__init__()
        self.main = parent
        self.working = True
        self.detect = detect
        self.state = state

        # self.main.add_sec_signal.connect(self.add_sec)   # 이것도 작동함. # custom signal from main thread to worker thread

    def __del__(self):
        print(".... end thread.....")
        self.wait()

    def run(self):
        md = detector.MyDetector()
        md.video(self.detect, self.detect_changed, self.state, self.state_changed)

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    form = MyMain()
    app.exec_()


def encode(text,s):
    result = ""
    for i in range(len(text)):
        char = text[i]

        if (char.isupper()):
            result += chr((ord(char) + s-65) % 26 + 65)
        elif (char == " "):
            result += " "
        else:
            result += chr((ord(char) + s-97) % 26 + 97)
    return result

text = input("enter message to be encoded ")
s = int(input("what is the key you want to use "))

print("your encoded message: " + encode(text,s))


"""
ExpandScript
Eric Diskin
Version is at bottom
2018
"""
import sys, os, json, time, re

logged = 0

def getLines(path,MODE):
	if not MODE == 'FUNCTION':

		try:
			# try to find file
			with open(path,'r') as file:
				lines = file.read().split('\n')

		except FileNotFoundError:
			# file not found
			error('File','File not found.',-1)

	else:
		lines = path.split('\n')

	return lines

#VARS
varz = {
	'0':'0',
	'1':'1',
	'2':'2',
	'3':'3',
	'4':'4',
	'5':'5',
	'6':'6',
	'7':'7',
	'8':'8',
	'9':'9'
}

class funs:
	"""a class for defining functions"""
	def __init__(self):
		self.funs = {}
		self.funs_args = {}

	def add(self,name,content,args):
		content = content.replace('%OP','(').replace('%CP',')')
		#log('USER function\n~~~',content,'\n~~~')
		self.funs[name] = content
		self.funs_args[name] = args

	def defined(self,name):
		return name in self.funs

	def fun(self,name,args,line):
		done = self.funs[name]

		for x in range(len(self.funs_args[name])):
			done = done.replace(self.funs_args[name][x],args[x])

		if 'return' in done:
			for li in done.split('\n'):

				if 'return' in li:
					print('returning from function')
					retval = li[(li.index('return') + len('return')):]
					print('returnvalue:',retval)

					#namedex = line.index(name) + len(name)
					line = line.replace(name + '(' + ','.join(args) + ')' ,retval,1)
					log('newline:',line)

		return (done,line)

funs = funs()

def join(arr,space=' '):
	return space.join(arr)

def error(etype='Unknown',content='An unknown error occured',linum=-1,path=''):
	sys.exit('\n\n[:ERROR:]\n%sError:\n%s\nline:%s' % (etype,content,linum))

def speacialChars(strr):
	strr = strr.replace('%OP','(').replace('%CP',')').replace('%WS','')
	return strr

def wordj(arr):
	return ' '.join(arr)

def EXEC(path,MODE='MAIN',funs=funs,varz=varz,itera=0):

	def log(*args):
		print('\t' * itera + ' '.join(args))

	class bytec:
		"""creates bytecode"""
		def __init__(self,path='',content=''):
			self.path = path

		_bytecode = ''

		def add(self,*txts):
			self._bytecode += '\n'
			for i in txts:
				if type(i) != str:
					i = str(i)
				self._bytecode += i + ' '

		def finish(self):
			directory = os.path.dirname(self.path) + '/ExpandScript_Output_/'
			if not os.path.exists(directory):
				os.mkdir(directory)

			file = open(directory + os.path.split(self.path)[1].split('.')[0] + '.exs_bytes','w')
			file.write(str(self._bytecode))
			file.close()

		def get(self):
			return self._bytecode

		def FUN(self,name,args,line):
			log('functioning')
			code = funs.fun(name,args,line)
			log('USER functio1n\n~~~',funs.fun(name,args,line)[0],'\n','~~~')
			self._bytecode += EXEC(code[0],'FUNCTION',funs,varz,itera + 1)
			return code[1]

		def finish_as_home(self):
			direct = (os.path.expanduser('$') + '/ExpandScript_Output_/').replace('\\','/')
			if not os.path.exists(direct):
				os.mkdir(direct)

			with open(direct + self.path.replace('\\','/').split('/')[-1],'w') as file:
				file.write(str(self._bytecode))

		def byte(self):
			self._bytecode = bytes(self._bytecode,'utf8')

	# var types:
	VAR_TYPES = [
	'int',
	'str',
	'bool',
	'arr',
	'obj',
	'void',
	'fun'
	]

	#lines to skip in a block:
	skipLines = -1
	byte = bytec(path)

	lines = getLines(path,MODE)

	linum = 0 # line number

	def hasChar(arr, word):
		for i in arr:
			for x in word:
				if i == x:
					return True
		return False

	inComment = False

	for line in lines:
		linum += 1

		log('reading line %s: %s' % (linum,line))
		line = line.strip()

		if line == '' or line == '\n' or line[0] == '^':
			continue

		if '::' in line:
			log('skipping comment')
			inComment = (inComment == False)
			if inComment:
				line = line.split('::')[1]

			else:
				line = line.split('::')[0]

		elif inComment:
			continue

		if skipLines > -1:
			skipLines -= 1
			continue

		if hasChar(line,'^'):
			log('skipping comment')
			line = line.split('^')[0].strip()

		words = line.split(' ')

		for i in range(len(words)):

			words[i] = words[i].strip()

		for i in varz:
			line = line.replace('$' + i,varz[i])

		if hasChar(line,'(') and hasChar(line,')'):
			# if line is a function
			_name = line.split('(')[0].strip().split()[-1]
			_args = line.split('(')[1].split(')')[len(line.split('(')[1].split(')')) - 2].split(',')
			log('function name:',_name)

			log('stripping args')
			for i in range(len(_args)):
				_args[i] = _args[i].strip()

			log('args stripped')

			line = line.replace(', ',',')
			if funs.defined(_name):
				line = byte.FUN(_name,_args,line)
				words = line.split()
				print('line returned from function:',line)

			elif _name == 'rawjs':
				log('adding raw js')
				byte.add('js',line.split('(\'')[1].split('\')')[len(line.split('(\'')[1].split('\')')) - 2])

			elif _name == 'rawhtml':
				log('adding raw html to body')
				byte.add('html',line.split('(\'')[1].split('\')')[len(line.split('(\'')[1].split('\')')) - 2])

			elif _name == 'rawcss':
				log('adding raw css')
				byte.add('css',line.split('(\'')[1].split('\')')[len(line.split('(\'')[1].split('\')')) - 2])

			elif _name == 'log':
				log('adding console statement')
				byte.add('log',join(_args))

			else:
				error('Define','An item on this line is not defined in file: %s, in word: %s' % (path, _name), linum)

		if words[0] in VAR_TYPES:
			log('Getting variable')
			_type = words[0]
			_val = 'undefined'
			if not '$' in words[1]:
				error('Name','Invalid variable name',linum)

			_name = words[1].split('$')[1]
			if not _type in VAR_TYPES:
				error('Type','Not a valid variable type',linum,time)
			elif words[2] != '=':
				error('Syntax','Invalid syntax.',linum)

			elif _type == 'int':
				log('type is int')
				try:
					_val = words[3]

				except ValueError:
					error('Type','Not of type: int',linum,time)

			elif _type == "str":
				log('type str')
				if hasChar(line,"'"):
					word = words[3]
					strStart = word.index("'")
					_val = ' '.join(words[(words.index(word) + strStart):])

				else:
					error('String','Invalid str.',linum)

			elif _type == 'bool':
				bool_types = ['true', 'false']
				if not words[3] in bool_types:
					error('Bool','invalid bool',linum)

			log('setting variable')
			log('variable data:','ANONY')
			varz[_name] = _val

		elif hasChar(line,'$') and line[0] == '$' and '[' in line and ']' in line:
			log('Creating function')
			i = linum
			_name = line.split('[')[0][1:]
			_args = line.split('[')[1].split(']')[0].split(',')

			for x in _args:
				if len(x) > 0:
					if x[0] != '$':
						error('Syntax','Invalid variable',linum)

			content = []
			skipLines = 0
			while True:
				if err(lines[i]):
					i += 1
					continue

				if lines[i][0] == '}':
					skipLines = i - linum
					break

				content.append(lines[i])
				i += 1

			fin = ''
			for x in content:
				fin = fin + '\n' + x

			linum = i + 1

			funs.add(_name,fin,_args)
			log('function created')

		if words[0] == '#inc':
			log('importing')
			try:
				if len(words) > 2:
					if not words[2] == 'from':
						error('Syntax','Invalid syntax.',linum,time)
					else:
						_path = words[3] + line.split('<')[1].split('>')[0] + '.expd'

				else:
					_path = './Lib/' + line.split('<')[1].split('>')[0] + '.expd'

				byte.add(EXEC(_path,'MODULE',funs,varz,itera + 1))

			except IndexError as e:
				raise e
				error('Syntax','Invalid syntax.l',linum,time)

	if MODE == 'MAIN':
		#byte.byte()

		byte.finish() # finish_as_home for non-dev purposes

		compileHTML(byte.get(),path)
	elif MODE == 'MODULE' or MODE == 'FUNCTION':
		print('returned script')
		return byte.get()

	else:
		error('Compile','Unable to compile at this time.')

	return 0

def err(line):
	if line == '' or line == '\n':
		return True

	return False

def strip(line):
	fin = line.strip()
	fin = fin.replace('${VERSION}$',VERSION)
	return fin

def replaceSP(line):
	return line.replace('${TABS}$','\t\t')

def wordsLen(words,num):
	if len(words) < num:
		error('Argument','Insufficint arguments',-1)

def compileHTML(code,path):

	#code = bytes.decode(code,'utf8')

	class compiled:
		"""this class is for making """
		def __init__(self):
			html = open('./html_compile/start_html.txt','r')
			self.html = html.read()
			html.close()

		js = """"""

		css = """"""

		htmlPlus = """"""

		def addHtml(self,*html):
			html = replaceSP(' '.join(html))
			self.htmlPlus += '\n\t' + html

		def addJs(self,*js):
			js = replaceSP(' '.join(js))
			self.js += '\n\t\t' + js

		def addCss(self,*css):
			css = replaceSP(' '.join(css))
			self.css += '\n\t\t' + css

		def bind(self):
			binded = self.html.replace('${HTML}$',self.htmlPlus).replace('${JS}$',self.js,1).replace('${CSS}$',self.css,1).replace('${VERSION}$',VERSION,1)
			return binded

	html = compiled()

	lines = code.split('\n')
	linum = 0

	for line in lines:
		linum += 1

		line = strip(line)
		words = line.split(' ')

		if err(line):
			continue

		if words[0] == 'log':
			# logs to the console
			wordsLen(words,2)
			html.addJs('console.log(' + ', '.join(words[1:]) + ');')

		elif words[0] == 'js':
			wordsLen(words,2)
			html.addJs(join(words[1:]))

		elif words[0] == 'html':
			wordsLen(words,2)
			html.addHtml(join(words[1:]))

		elif words[0] == 'css':
			wordsLen(words,2)
			html.addCss(join(words[1:]))

	with open(os.path.dirname(path) + '/ExpandScript_Output_/' + os.path.basename(path) + '.html','w') as output:
		output.write(html.bind())
		log('Succesfully compiled',path.replace('\\','/'))

VERSION = '1.0.5.0'

"""
name (current release {b:beta,a:alpha}) planned release version
Features to be added:
	return (b2) 1.1.0.0
	do-loop () 1.3.0.0
	for-loop () 1.3.0.0
	while-loop () 1.3.0.0
	if statement () 1.3.0.0
	else statement () 1.3.0.0

bug (status {0:active,1:in progress}) (planned release fix OP)
bugs:
	functions inside calling function as argument (0)
	the error system outputs incorrect line number (1)
"""

#EXEC( os.path.dirname(os.path.realpath(__file__)) + '/game1.expd')

#EXEC canvas module
print(EXEC('./lib/canvas.expd','MODULE'))


import pandas as pd
import numpy as np
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import LocalOutlierFactor, KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import BaggingClassifier,AdaBoostClassifier
from joblib import dump, load
import os
import graphviz
import time

"""
Cuva model
"""
def save_model(clf, metod):
    dump(clf, os.path.join('models', metod + '.joblib'))

"""
Nalazi nula kolone i eliminise ih u zavisnosti od odabranog nacina - unija ili presek
"""
def zero_columns_to_remove(df1, df2, removal_type):
    first_zero_columns = df1.columns[(df1 == 0).all()]
    second_zero_columns = df2.columns[(df2 == 0).all()]

    if(removal_type == 'union'):
        removed_cols = np.union1d(first_zero_columns, second_zero_columns)
    elif (removal_type == 'intersection'):
        removed_cols = np.intersect1d(first_zero_columns, second_zero_columns)
    else:
        print("Bad type of removing zero values")

    return removed_cols

"""
Cuva trenutni CSV fajl
"""
def save_csv(df, filename):
    df.to_csv(filename + '.csv', sep=',')

"""
Razdvaja klasu od podataka i vraca oba
"""
def seperate_data_class(df):
    v_data = df.values[:, :-1]
    v_class = df.values[:, -1:]
    return v_data, v_class

"""
STABLO ODLUCIVANJA - klasifikacija
"""
def decisionTree(x_train, x_test, y_train, y_test, criteria, depth=None, generateGraph=[]):
    print("---------------------------------------------\n")
    print("Rezultati za stablo: [" + str(criteria) + " " + str(depth) + "] \n")
    beginTime = time.time()
    print("Kreiranje klasifikatora ... \n")
    clf = tree.DecisionTreeClassifier(criterion=criteria, max_depth=depth)
    clf.fit(x_train,y_train.ravel())
    save_model(clf, "tree_" + str(criteria) + "_" + str(depth))
    print('Trening tacnost: {}'.format(clf.score(x_train, y_train)))
    print('Test tacnost: {}'.format(clf.score(x_test, y_test)))
    y_predict_train = clf.predict(x_train)
    y_predict_test = clf.predict(x_test)
    print("Matrica kofuzije trening vrednosti: \n" + str(confusion_matrix(y_train, y_predict_train)))
    print("Matrica kofuzije test vrednosti: \n" + str(confusion_matrix(y_test, y_predict_test)))
    endTime = time.time()
    elapsedTime = endTime - beginTime
    print(f"Vreme potrebno za izvrsavanje: {elapsedTime:.4f} \n")
    if generateGraph:
        print("Kreiranje grafa")
        dot_data = tree.export_graphviz(clf,
                                    out_file=None,
                                    feature_names= generateGraph,
                                    class_names=['parental (BRAF inhibitor sensitive)', 'BRAF inhibitor resistant'])
        graph = graphviz.Source(dot_data)
        if(depth == None):
            depth = "full"
        graph.render("dtGraph/DecisionTree_" + criteria + "_" + str(depth))

"""
KNN - klasifikacija
"""
def knn(x_train, x_test, y_train, y_test, n_neigh, weights='uniform', algorithm = 'auto'):
    print("---------------------------------------------\n")
    print("Rezultati za knn: [" + str(n_neigh) + " " + str(weights) + " " + str(algorithm) + "] \n")
    beginTime = time.time()
    print("Kreiranje klasifikatora ... \n")
    clf = KNeighborsClassifier(n_neigh, weights, algorithm)
    clf.fit(x_train,y_train.ravel())
    save_model(clf, "knn_" + str(n_neigh) + "_" + str(weights) + "_" + str(algorithm))
    print('Trening tacnost: {}'.format(clf.score(x_train, y_train)))
    print('Test tacnost: {}'.format(clf.score(x_test, y_test)))
    y_predict_train = clf.predict(x_train)
    y_predict_test = clf.predict(x_test)
    print("Matrica kofuzije trening vrednosti: \n" + str(confusion_matrix(y_train, y_predict_train)))
    print("Matrica kofuzije test vrednosti: \n" + str(confusion_matrix(y_test, y_predict_test)))
    endTime = time.time()
    elapsedTime = endTime - beginTime
    print(f"Vreme potrebno za izvrsavanje: {elapsedTime:.4f} \n")

"""
SVM - klasifikacija
"""
def svm(x_train, x_test, y_train, y_test, kernel, gamma='auto'):
    print("---------------------------------------------\n")
    print("Rezultati za svm: [" + str(kernel) + " " + str(gamma) + "] \n")
    beginTime = time.time()
    print("Kreiranje klasifikatora ... \n")
    clf = SVC(kernel=kernel, gamma=gamma)
    clf.fit(x_train,y_train.ravel())
    save_model(clf, "svm_" + str(kernel) + "_" + str(gamma))
    print('Trening tacnost: {}'.format(clf.score(x_train, y_train)))
    print('Test tacnost: {}'.format(clf.score(x_test, y_test)))
    y_predict_train = clf.predict(x_train)
    y_predict_test = clf.predict(x_test)
    print("Matrica kofuzije trening vrednosti: \n" + str(confusion_matrix(y_train, y_predict_train)))
    print("Matrica kofuzije test vrednosti: \n" + str(confusion_matrix(y_test, y_predict_test)))
    endTime = time.time()
    elapsedTime = endTime - beginTime
    print(f"Vreme potrebno za izvrsavanje: {elapsedTime:.4f} \n")

"""
BAGGING - klasifikacija
"""
def bagging(x_train, x_test, y_train, y_test, n, classifier=None):
    print("---------------------------------------------\n")
    print("Rezultati za bagging: [" + str(classifier) + " " + str(n) + "] \n")
    beginTime = time.time()
    print("Kreiranje klasifikatora ... \n")
    if(classifier == "svc"):
        unit = SVC(kernel='poly', gamma='auto')
    elif(classifier == "tree"):
        unit = tree.DecisionTreeClassifier()
    elif(classifier == "knn"):
        unit = KNeighborsClassifier(3, algorithm="brute")
    else:
        unit = None

    clf = BaggingClassifier(unit, n_estimators=n)
    clf.fit(x_train,y_train.ravel())
    save_model(clf, "bagging_" + str(classifier) + "_" + str(n))
    print('Trening tacnost: {}'.format(clf.score(x_train, y_train)))
    print('Test tacnost: {}'.format(clf.score(x_test, y_test)))
    y_predict_train = clf.predict(x_train)
    y_predict_test = clf.predict(x_test)
    print("Matrica kofuzije trening vrednosti: \n" + str(confusion_matrix(y_train, y_predict_train)))
    print("Matrica kofuzije test vrednosti: \n" + str(confusion_matrix(y_test, y_predict_test)))
    endTime = time.time()
    elapsedTime = endTime - beginTime
    print(f"Vreme potrebno za izvrsavanje: {elapsedTime:.4f} \n")

"""
BOOSTING - klasifikacija
"""
def boosting(x_train, x_test, y_train, y_test, learning=1.0, classifier=None):
    print("---------------------------------------------\n")
    print("Rezultati za boosting: [" + str(classifier) + " " + str(learning) + "] \n")
    beginTime = time.time()
    print("Kreiranje klasifikatora ... \n")
    if(classifier == "svc"):
        unit = SVC(kernel='poly', gamma='auto')
        alg = "SAMME"
    elif(classifier == "tree"):
        unit = tree.DecisionTreeClassifier()
        alg = "SAMME.R"
    else:
        alg = "SAMME.R"
        unit = None

    clf = AdaBoostClassifier(unit, learning_rate=learning, algorithm = alg)
    clf.fit(x_train,y_train.ravel())
    save_model(clf, "boosting_" + str(classifier) + "_" + str(learning))
    print('Trening tacnost: {}'.format(clf.score(x_train, y_train)))
    print('Test tacnost: {}'.format(clf.score(x_test, y_test)))
    y_predict_train = clf.predict(x_train)
    y_predict_test = clf.predict(x_test)
    print("Matrica kofuzije trening vrednosti: \n" + str(confusion_matrix(y_train, y_predict_train)))
    print("Matrica kofuzije test vrednosti: \n" + str(confusion_matrix(y_test, y_predict_test)))
    endTime = time.time()
    elapsedTime = endTime - beginTime
    print(f"Vreme potrebno za izvrsavanje: {elapsedTime:.4f} \n")

"""
Nalazi kolone van granica
"""
def find_outlier_columns(df):
    outlier_res = (df.drop(columns="Class")).T
    outliers = outlier_res[(np.abs(stats.zscore(outlier_res)) >= 3).any(axis=1)]
    outlier_columns = list(outliers.T.columns)
    outlier_columns.append('Class')
    return df[outlier_columns]

"""
Proverava nedostajuce vrednosti
"""
def checkNaNvalues(df):
    if df.isnull().values.any():
        print("NaN vrednosti postoje\n")
    else:
        print("NaN vrednosti ne postoje\n")

def main():

    print("PRETRPOCESIRANJE")
    print("---------------------------------------------\n")
    print("Ucitavanje podataka ... \n")
    first_df = pd.read_csv('001_Melanoma_Cell_Line_csv.csv', index_col=0)
    second_df = pd.read_csv('002_Melanoma_Cell_Line_csv.csv', index_col=0)

    # Transponovanje matrice
    first_df = first_df.T
    second_df = second_df.T
    print("Pocetna dimenzija 1. matrice: " + str(first_df.shape))
    print("Pocetna dimenzija 2. matrice: " + str(second_df.shape) + '\n')

    # Dodavanje klase
    first_df.insert(len(first_df.columns), "Class", 'parental (BRAF inhibitor sensitive)')
    second_df.insert(len(second_df.columns), "Class", 'BRAF inhibitor resistant')
    print("Dimenzija 1. matrice posle dodavanja klase: " + str(first_df.shape))
    print("Dimenzija 2. matrice posle dodavanja klase: " + str(second_df.shape) + '\n')

    # Nalazenje kolona koje treba izbaciti na osnovu tipa (unija/presek)
    columns_to_remove = zero_columns_to_remove(first_df, second_df, removal_type="intersection")

    # Sjedinjavanje matrica
    result_df = pd.concat([first_df, second_df], ignore_index=True)
    print("Dimenzija spojene matrice: " + str(result_df.shape) + "\n")

    # Provera nedostajucih vrednosti
    checkNaNvalues(result_df)

    # Eliminacija nula
    result_df.drop(columns=columns_to_remove, inplace=True)
    print("Dimenzija posle izbacenih nula: " + str(result_df.shape) + "\n")

    # Pronalazenje autlajera
    outliers_df = find_outlier_columns(result_df)
    print("Dimenzija tabele autlajera: " + str(outliers_df.shape) + "\n")

    # Random promesati podatke
    result_df = result_df.sample(frac=1).reset_index(drop=True)
    outliers_df = outliers_df.sample(frac=1).reset_index(drop=True)

    # Uzimanje imena klasa za graf
    columns_without_class = result_df.loc[:, result_df.columns != 'Class'].columns.tolist()
    outlier_columns_without_class = outliers_df.loc[:, outliers_df.columns != 'Class'].columns.tolist()

    # Razdvajanje test i trening podataka
    x,y = seperate_data_class(result_df)
    out_x, out_y = seperate_data_class(outliers_df)

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4)
    ox_train, ox_test, oy_train, oy_test = train_test_split(out_x, out_y, test_size=0.4)

    # TESTIRANJE STABLA ODLUCIVANJA - ceo skup
    '''
    decisionTree(x_train, x_test, y_train, y_test, 'gini', None)
    decisionTree(x_train, x_test, y_train, y_test, 'gini', 6 )
    decisionTree(x_train, x_test, y_train, y_test, 'gini', 4 )
    decisionTree(x_train, x_test, y_train, y_test, 'gini', 3 , columns_without_class)
    decisionTree(x_train, x_test, y_train, y_test, 'entropy', None)
    decisionTree(x_train, x_test, y_train, y_test, 'gini', 6 )
    decisionTree(x_train, x_test, y_train, y_test, 'entropy', 6, columns_without_class)
    decisionTree(x_train, x_test, y_train, y_test, 'gini', 4, columns_without_class)
    decisionTree(x_train, x_test, y_train, y_test, 'entropy', 4, columns_without_class)
    '''

    # TESTIRANJE KNN - ceo skup
    '''
    knn(x_train, x_test, y_train, y_test, 5, 'uniform', 'kd_tree')
    knn(x_train, x_test, y_train, y_test, 5, 'uniform', 'ball_tree')
    knn(x_train, x_test, y_train, y_test, 5, 'uniform')
    knn(x_train, x_test, y_train, y_test, 5, 'uniform', 'brute')

    knn(x_train, x_test, y_train, y_test, 3, 'uniform', 'brute')
    knn(x_train, x_test, y_train, y_test, 5, 'uniform', 'brute')
    knn(x_train, x_test, y_train, y_test, 10, 'uniform', 'brute')
    knn(x_train, x_test, y_train, y_test, 15, 'uniform', 'brute')
    '''

    # TESTIRANJE SVM-a - ceo skup

    '''
    svm(x_train, x_test, y_train, y_test, 'rbf')
    svm(x_train, x_test, y_train, y_test, 'poly')
    svm(x_train, x_test, y_train, y_test, 'sigmoid')
    '''

    # TESTIRANJE BAGGING - ceo skup

    '''
    bagging(x_train, x_test, y_train, y_test, 3)
    bagging(x_train, x_test, y_train, y_test, 5)
    bagging(x_train, x_test, y_train, y_test, 7)
    bagging(x_train, x_test, y_train, y_test, 10)

    bagging(x_train, x_test, y_train, y_test, 5, 'svc')
    bagging(x_train, x_test, y_train, y_test, 5, 'tree')
    bagging(x_train, x_test, y_train, y_test, 5, 'knn')
    '''

    # TESTIRANJE BOOSTING - ceo skup

    '''
    boosting(x_train, x_test, y_train, y_test, 0.5)
    boosting(x_train, x_test, y_train, y_test, 0.7)
    boosting(x_train, x_test, y_train, y_test, 0.9)
    boosting(x_train, x_test, y_train, y_test, 1)
    boosting(x_train, x_test, y_train, y_test, 1.2)

    boosting(x_train, x_test, y_train, y_test, 1, 'svc')
    boosting(x_train, x_test, y_train, y_test, 1, 'tree')
    '''

    # TESTIRANJE redukovanog skupa

    decisionTree(ox_train, ox_test, oy_train, oy_test, 'gini', 4, outlier_columns_without_class)
    knn(ox_train, ox_test, oy_train, oy_test, 3, 'uniform', 'brute')
    svm(ox_train, ox_test, oy_train, oy_test, 'poly')
    bagging(ox_train, ox_test, oy_train, oy_test, 5, 'svc')
    boosting(ox_train, ox_test, oy_train, oy_test, 1)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
"""
Contains a class MRUCache that inherits from BaseCaching
"""

BaseCaching = __import__('base_caching').BaseCaching

class MRUCache(BaseCaching):
    """
    A caching system
    Implements the MRU algorithm
    """

    def __init__(self):
        super().__init__()
        self.mem_cache = {}
        self.counter = 0

    def put(self, key, item):
        """
        Adds an item in the cache
        If the number of items is higher that BaseCaching.MAX_ITEMS, discard
        the first element
        """
        if key and item:
            if key in self.cache_data:
                self.cache_data[key] = item
            else:
                if len(self.cache_data) >= BaseCaching.MAX_ITEMS:
                    max_key = max(self.mem_cache, key=self.mem_cache.get)
                    del self.cache_data[max_key]
                    del self.mem_cache[max_key]
                    print(f"DISCARD: {max_key}")

            self.mem_cache[key] = self.counter
            self.cache_data[key] = item
            self.counter += 1

        elif key is None or item is None:
            return

    def get(self, key):
        """
        Gets an item by key
        """
        if key in self.cache_data:
            self.mem_cache[key] = self.counter
            self.counter += 1
            return self.cache_data[key]


# Array que será formado de 0 e 1, o valor 1 será
# quando a linha = coluna, e o restante = 0

array = []
largura = 100
tamanho = 10

for altura in range(tamanho):
    for linha in range(largura):
        if linha >= 10 * altura:
            print(1, end="")
        else:
            print(0, end="")
    print("")


# This is for right side triangles and finding the
# size lengths of the hypotenuse, opposite, and adjacent.

from decimal import *

#Function to determine the opposite.
def determining_the_opposite(hypotenuse, adjacent):
	opposite = Decimal(((hypotenuse ** 2)-(adjacent ** 2)))
	print("The opposite is: {:.3f}".format(opposite.sqrt()))

#Function to determine the adjacent.
def determining_the_adjacent(hypotenuse, opposite):
	adjacent = Decimal(((hypotenuse ** 2)-(opposite ** 2)))
	print("The adjacent is: {:.3f}".format(adjacent.sqrt()))

#Function to determine the hypotenuse.
def determining_the_hypotenuse(adjacent, opposite):
	hypotenuse = Decimal(((adjacent ** 2) + (opposite ** 2)))
	print("The hypotenuse is: {:.3f}".format(hypotenuse.sqrt()))

#Function that provides options on calculating the opposite, adjacent, and hypotenuse.
def main():
	calculate = str(input("Enter (a) for opposite, (b) for adjacent, and (c) for hypotenuse: "))
	if calculate in "aA":
		hypotenuse = float(input("Enter the length of the hypotenuse: "))
		adjacent = float(input("Enter the length of the adjacent: "))
		determining_the_opposite(hypotenuse, adjacent)
	if calculate in "bB":
		hypotenuse = float(input("Enter the length of the hypotenuse: "))
		opposite = float(input("Enter the length of the opposite: "))
		determining_the_adjacent(hypotenuse, opposite)
	if calculate in "cC":
		adjacent = float(input("Enter the length of the adjacent: "))
		opposite = float(input("Enter the length of the opposite: "))
		determining_the_hypotenuse(adjacent, opposite)

main()


from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from fake_useragent import UserAgent
import requests
import json

ua = UserAgent()

fantasy_data = []
fantasy_team_data = []

BOT_TOKEN = 'YOUR_TOKEN'

bot = Bot(token=BOT_TOKEN, parse_mode=types.ParseMode.HTML)
dp = Dispatcher(bot, storage=MemoryStorage())

user_id = 0
current_event = 0

def fantasy_premier_league():
    response = requests.get(
        url="https://fantasy.premierleague.com/api/entry/your_id/",
        headers={
            "user-agent": f"{ua.random}"
        }
    )

    with open(f"{user_id}.json", "w", encoding="utf-8") as file:
        json.dump(response.json(), file, indent=4, ensure_ascii=False)

    data = response.json()

    fantasy_id = data["id"]
    joined_time = data["joined_time"]
    player_first_name = data["player_first_name"]
    player_last_name = data["player_last_name"]
    player_region_name = data["player_region_name"]
    summary_overall_points = data["summary_overall_points"]
    summary_overall_rank = data["summary_overall_rank"]
    summary_event_points = data["summary_event_points"]
    global current_event
    current_event = data["current_event"]
    name = data["name"]
    last_deadline_total_transfers = data["last_deadline_total_transfers"]

    fantasy_data.append(
        [
            fantasy_id,
            joined_time,
            player_first_name,
            player_last_name,
            player_region_name,
            summary_overall_points,
            summary_overall_rank,
            summary_event_points,
            current_event,
            name,
            last_deadline_total_transfers
        ]
    )

def fantasy_premier_league_team():
    response = requests.get(
        url=f"https://fantasy.premierleague.com/api/entry/your_id/event/{current_event}/picks/",
        headers={
            "user-agent": f"{ua.random}"
        }
    )

    with open(f"{user_id}.json", "w", encoding="utf-8") as file:
        json.dump(response.json(), file, indent=4, ensure_ascii=False)

    data = response.json()

    for i in data["picks"]:
        position = i["position"]
        multiplier = i["multiplier"]
        is_captain = i["is_captain"]
        is_vice_captain = i["is_vice_captain"]

        fantasy_team_data.append(
            [
                position,
                multiplier,
                is_captain,
                is_vice_captain
            ]
        )

@dp.message_handler(commands='start')
async def start(message: types.Message):
    global user_id
    user_id = message.from_user.id
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(types.KeyboardButton(text="Main datas"))
    keyboard.insert(types.KeyboardButton(text="Datas of team"))
    await message.answer("Choose the data", reply_markup=keyboard)

@dp.message_handler(content_types=types.ContentTypes.ANY)
async def main_datas(message: types.Message):
    fantasy_premier_league()
    if message.text == "Main datas":
        main_data = ''
        for i in fantasy_data:
            text_main_data = f"id: {i[0]}\njoined_time: {i[1]}\nplayer_first_name: {i[2]}\nplayer_last_name: {i[3]}\nplayer_region_name: {i[4]}\nsummary_overall_points: {i[5]}\nsummary_overall_rank: {i[6]}\nsummary_event_points: {i[7]}\ncurrent_event: {i[8]}\nname: {i[9]}\nlast_deadline_total_transfers: {i[10]}"
            main_data += text_main_data
        await message.answer(main_data)
    elif message.text == "Datas of team":
        fantasy_premier_league_team()
        data = ''
        for j in fantasy_team_data:
            text_data = f"position: {j[0]}\nmultiplier: {j[1]}\nis_captain: {j[2]}\nis_vice_captain: {j[3]}\n\n"
            data += text_data
        await message.answer(data)
    else:
        await message.answer("Please choose the type data below")
        return start

def main():
    executor.start_polling(dp)

if __name__ == '__main__':
    main()


#!/usr/bin/python3
# https://www.youtube.com/watch?v=CJMCoAsK-h0&index=10&list=PLQVvvaa0QuDdttJXlLtAJxJetJcqmqlQq

import cv2

# 1st webcam source
cap = cv2.VideoCapture(0)

while 1:
    _, frame = cap.read()

    laplacian_gradient = cv2.Laplacian(frame, cv2.CV_64F)

    # directional gradients
    sobelx_gradient = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=5)
    sobely_gradient = cv2.Sobel(frame, cv2.CV_64F, 0, 1, ksize=5)

    # edge detectors - canny - smaller numbers yield alot of noise - higher numbers for less edges
    edges = cv2.Canny(frame, 100, 200)

    cv2.imshow('Original', frame)
    cv2.imshow('Laplacian', laplacian_gradient)
    cv2.imshow('SobelX', sobelx_gradient)
    cv2.imshow('SobelY', sobely_gradient)
    cv2.imshow('Canny', edges)

    # Close all on 'q' press
    k = cv2.waitKey(5) & 0xFF
    if k == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()


# futval2.py
# A program to compute the compound interest
# Input : principal amount, interest rate and (period )no.of years.
# Output : Value of investement at the end of period.

def main():
    print("A program to compute the value of your investement:")
    principal, rate, period = eval(input("Enter Principal, Rate, Period (Comma separated values) : "))
    futval = principal * pow((1+rate/100), period)
    print("Future value of investment is : " + str(futval))

main()


import tensorflow as tf
import numpy as np

def spec2wav(amp, iters=1000):
    a = tf.placeholder(tf.float32, shape=amp.shape)
    Pi = tf.constant(np.pi, dtype=tf.float32)
    ang = 2 * Pi * tf.random_uniform(amp.shape, dtype=tf.float32)
    for i in range(iters):
        S = tf.matmul(a * tf.exp(1j * ang))
        W = tf


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 15 22:09:13 2020

@author: insauer
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

"""Script to reproduce main Figure 2!"""

fig3 = plt.figure(constrained_layout=True, figsize=(7., 9.9))
gs = fig3.add_gridspec(40, 15)
plt.subplots_adjust(wspace=0., hspace=0)

DATA_TSFull= pd.read_csv('../../data/reconstruction/attribution_TimeSeries_regions.csv')
DATA_TS= pd.read_csv('../../data/reconstruction/attribution_TimeSeries_subregions.csv')

DATA_FIT_Full= pd.read_csv('../../../data/reconstruction/vulnerability_adjustment_MetaData_regions.csv')
DATA_FIT= pd.read_csv('../../data/reconstruction/vulnerability_adjustment_MetaData_subregions.csv')

region_names={'GLB': 'Global (GLB)',
              'NAM':'North America (NAM)',
              'EAS':'Eastern Asia (EAS)',
              'OCE':'Oceania (OCE)',
              'LAM':'Latin America (LAM)',
              'EUR':'Europe (EUR)',
              'SSA':'South & Sub-Sahara Africa (SSA)',
              'SEA':'South & South-East Asia (SEA)',
              'CAS':'Central Asia & Russia (CAS)',
              'NAF':'North Africa & Middle East (NAF)'
              }

region_abs={'GLB': 'GLB',
            'NAM':'NAM',
            'EAS':'EAS',
            'LAM':'LAM',
            'EUR':'EUR',
            'OCE':'OCE',
            'CAS':'CAS',
            'SSA':'SSA',
            'SEA':'SEA',
            'NAF': 'NAF'}

regions = list(region_names)
r =0

for i in range(10):
    for j in range(4):

        DATA_regionFull = DATA_TSFull[(DATA_TSFull['Region']==regions[r]) &
                      (DATA_TSFull ['Year']<2011) & (DATA_TSFull ['Year']>1979)]

        DATA_region = DATA_TS[(DATA_TS['Region']==regions[r]) &
                      (DATA_TS['Year']<2011) & (DATA_TS['Year']>1979)]

        if j<3:

            f3_ax1 = fig3.add_subplot(gs[4*i:4*i+4,j*5:(j*5)+5])

            if j ==0:

                f3_ax1.plot(DATA_regionFull['Year'], np.log10(DATA_regionFull['natcat_flood_damages_2005_CPI']), label='D$_{Obs}$', color='black', linewidth = 1.)
                f3_ax1.scatter(DATA_regionFull['Year'], np.log10(DATA_regionFull['natcat_flood_damages_2005_CPI']), color='black', marker = '_', s = 3)
                f3_ax1.plot(DATA_regionFull['Year'], np.log10(DATA_regionFull['D_Full']), label='D$_{Full}$', color='#8856a7', linewidth = 1.)

                f3_ax1.plot(DATA_regionFull['Year'], np.log10(DATA_regionFull['D_CliExp']), label='D$_{CliExp}$', color='#ff7f00', linewidth = 1.)

                f3_ax1.plot(DATA_regionFull['Year'], np.log10(DATA_regionFull['D_1980']), label='D$_{1980}$', color='#4575b4', linewidth = 1.)

                f3_ax1.set_title(' '+ region_names[regions[r]], position = (0.5,0.75), fontsize = 6.5)

                if i ==0 and j ==0:

                    handles, labels = f3_ax1.get_legend_handles_labels()
                    leg =f3_ax1.legend(handles[:2], labels[:2], loc ='lower left', labelspacing = 0.1, frameon=True, fontsize = 6, handlelength = 1.1)
                    f3_ax1.legend(handles[2:], labels[2:], loc ='lower right', labelspacing = 0.1, frameon=True, fontsize = 6,  handlelength = 1.1)
                    f3_ax1.add_artist(leg)

                r_lin = DATA_FIT_Full.loc[DATA_FIT_Full['Region']==regions[r], 'R2_D_Full_D_Obs'].sum()

                #r2 = DATA_FIT_Full.loc[DATA_FIT_Full['Region']==regions[r], 'New_explained_variance'].sum()

            else:

                if j ==1:
                    dis = 'pos'
                    f3_ax1.set_title('{}'.format(region_abs[regions[r]])+'$_{+}$', position = (0.5,0.75), fontsize = 6.5)
                else:
                    dis = 'neg'
                    f3_ax1.set_title('{}'.format(region_abs[regions[r]])+'$_{-}$', position = (0.5,0.75), fontsize = 6.5)

                #f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['Impact_Pred_1thrd_{}'.format(dis)]), color='#8856a7', alpha = 0.5, linewidth = 1.)
                #f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['Impact_Pred_2thrd_{}'.format(dis)]), color='#8856a7', alpha = 0.5, linewidth = 1.)

                f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['natcat_damages_2005_CPI_{}'.format(dis)]), label='Observed Flood Losses (NatCat)', color='black', linewidth = 1.)
                f3_ax1.scatter(DATA_region['Year'], np.log10(DATA_region['natcat_damages_2005_CPI_{}'.format(dis)]), label='Observed Flood Losses (NatCat)', color='black', marker = '.', s = 3)

                if not (i==1 and dis=='pos'):

                    f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['D_CliExp_{}'.format(dis)]), label='$Loss_{HazExp}$', color='#ff7f00', linewidth = 1.)

                    f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['D_1980_{}'.format(dis)]), label='$Loss_{Haz}$', color='#4575b4', linewidth = 1.)

                    #f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['NormHaz_Imp2010_2y{}_offset'.format(dis)]), label='$Loss2010_{Haz}$', color='mediumseagreen', linewidth = 1.,linestyle ='--', alpha = 0.5)

                    f3_ax1.plot(DATA_region['Year'], np.log10(DATA_region['D_Full_{}'.format(dis)]), label='$Loss_{Full}$', color='#8856a7', linewidth = 1.)

                r_lin = DATA_FIT.loc[DATA_FIT['Region']==regions[r]+'_'+dis, 'R2_D_Full_D_Obs'].sum()

            #text_LOG = 'R²='+str(round(r_log*100,1))+ '% (LOG)'
            text_lin = '='+str(round(r_lin,1))+ '%'

            if r_lin> 0.2*100:

                f3_ax1.set_facecolor('gainsboro')

            f3_ax1.set_yticks([6, 8, 10])
            f3_ax1.set_yticklabels(['','',''])
            if j == 0 :
                f3_ax1.set_yticklabels(['6','8','10'], fontsize = 5.5)
            if i in [2]:
                f3_ax1.set_ylim((5, 11.5))

            elif i in [1]:
                f3_ax1.set_ylim((4.5, 11.5))

            elif i in [4]:
                f3_ax1.set_ylim((5, 11.5))

            elif i in [3]:
                f3_ax1.set_ylim((3., 10))
                f3_ax1.set_yticks([4, 6, 8])
                if j == 0:
                    f3_ax1.set_yticklabels(['4','6','8'])

            elif i in [6]:
                f3_ax1.set_ylim((3, 10))
                f3_ax1.set_yticks([4, 6, 8])
                if j == 0:
                    f3_ax1.set_yticklabels(['4','6','8'])

            elif i in [5,7]:
                f3_ax1.set_ylim((5.5, 11.5))

            elif i in [8]:
                f3_ax1.set_ylim((3.5, 10.5))
                f3_ax1.set_yticks([4, 6, 8])
                if j == 0:
                    f3_ax1.set_yticklabels(['4','6','8'])

            elif i in [0]:
                f3_ax1.set_ylim((7, 12))
                f3_ax1.set_yticks([8, 10])

                if j == 0:
                    f3_ax1.set_yticklabels(['8','10'])
            else:
                f3_ax1.set_ylim((3.65, 11))

            f3_ax1.set_xlim((1978 ,2013))
            if not (i==1 and j==1):
                f3_ax1.annotate( xy=(1991.8, f3_ax1.get_ylim()[0]+0.08*(f3_ax1.get_ylim()[1]-f3_ax1.get_ylim()[0]) ) ,s='R²', fontsize=6, fontstyle='italic')
                f3_ax1.annotate( xy=(1993.5, f3_ax1.get_ylim()[0]+0.08*(f3_ax1.get_ylim()[1]-f3_ax1.get_ylim()[0]) ) ,s=text_lin, fontsize=6)

            f3_ax1.set_xticks([1980,1990,2000,2010])
            f3_ax1.set_xticklabels(['','','',''])

            if i == 9:
                f3_ax1.set_xticklabels(['1980','1990','2000','2010'], fontsize =6)

            if i ==4 and j ==0:
                f3_ax1.set_ylabel('log$_{10}$(Damages in 2005 USD)', fontsize=6.5, labelpad=-0.)

            if i == 9 and j ==1:
                f3_ax1.set_xticklabels(['1980','1990','2000', '2010'],fontsize=6)
                f3_ax1.set_xlabel('Year', fontsize=7, labelpad=-2)

            f3_ax1.tick_params(axis="x", direction = 'in',length = 3)

            handles, labels = f3_ax1.get_legend_handles_labels()

    else:

        r_linPos = DATA_FIT.loc[DATA_FIT['Region']==regions[r]+'_pos', 'R2_D_Full_D_Obs'].sum()
        r_linNeg = DATA_FIT.loc[DATA_FIT['Region']==regions[r]+'_neg', 'R2_D_Full_D_Obs'].sum()
        r_lin = DATA_FIT_Full.loc[DATA_FIT_Full['Region']==regions[r], 'R2_D_Full_D_Obs'].sum()

    r+=1
plt.savefig('../../data/figures/Figure2.pdf',bbox_inches = 'tight')


# -*- coding: utf-8 -*-

from odoo import api, fields, models, tools, _
from odoo.osv import expression
import re

class ProductTemplate(models.Model):
    _inherit = "product.template"

    characteristics_ok = fields.Boolean('Characteristics OK')
    customer_ids = fields.One2many('product.customerinfo', 'product_tmpl_id', 'Customers')

class ProductProduct(models.Model):
    _inherit = "product.product"

    customer_code = fields.Char('Internal Reference', compute='_compute_customer_product_code')
    customer_ref = fields.Char('Customer Ref', compute='_compute_customer_ref')

    @api.one
    def _compute_customer_product_code(self):
        for cus_info in self.customer_ids:
            if cus_info.name.id == self._context.get('partner_id'):
                self.customer_code = cus_info.product_code or self.default_code
        else:
            self.customer_code = self.default_code

    @api.one
    def _compute_customer_ref(self):
        for cus_info in self.customer_ids:
            if cus_info.name.id == self._context.get('partner_id'):
                product_name = cus_info.product_name or self.default_code
        else:
            product_name = self.name
        self.customer_ref = '%s%s' % (self.customer_code and '[%s] ' % self.customer_code or '', product_name)

    @api.multi
    def name_get(self):
        # TDE: this could be cleaned a bit I think

        def _name_get(d):
            name = d.get('name', '')
            code = self._context.get('display_default_code', True) and d.get('default_code', False) or False
            if code:
                name = '[%s] %s' % (code,name)
            return (d['id'], name)

        partner_id = self._context.get('partner_id')
        if partner_id:
            partner_ids = [partner_id, self.env['res.partner'].browse(partner_id).commercial_partner_id.id]
        else:
            partner_ids = []

        # all user don't have access to seller and partner
        # check access and use superuser
        self.check_access_rights("read")
        self.check_access_rule("read")

        result = []
        for product in self.sudo():
            # display only the attributes with multiple possible values on the template
            variable_attributes = product.attribute_line_ids.filtered(lambda l: len(l.value_ids) > 1).mapped('attribute_id')
            variant = product.attribute_value_ids._variant_name(variable_attributes)

            name = variant and "%s (%s)" % (product.name, variant) or product.name
            sellers = []
            customers = []
            if partner_ids:
                sellers = [x for x in product.seller_ids if (x.name.id in partner_ids) and (x.product_id == product)]
                if not sellers:
                    sellers = [x for x in product.seller_ids if (x.name.id in partner_ids) and not x.product_id]
                customers = [x for x in product.customer_ids if (x.name.id in partner_ids) and (x.product_id == product)]
                if not customers:
                    customers = [x for x in product.customer_ids if (x.name.id in partner_ids) and not x.product_id]
            if customers:
                for c in customers:
                    seller_variant = c.product_name and (
                        variant and "%s (%s)" % (c.product_name, variant) or c.product_name
                        ) or False
                    mydict = {
                              'id': product.id,
                              'name': seller_variant or name,
                              'default_code': c.product_code or product.default_code,
                              }
                    temp = _name_get(mydict)
                    if temp not in result:
                        result.append(temp)

            elif sellers:
                for s in sellers:
                    seller_variant = s.product_name and (
                        variant and "%s (%s)" % (s.product_name, variant) or s.product_name
                        ) or False
                    mydict = {
                              'id': product.id,
                              'name': seller_variant or name,
                              'default_code': s.product_code or product.default_code,
                              }
                    temp = _name_get(mydict)
                    if temp not in result:
                        result.append(temp)

            else:
                mydict = {
                          'id': product.id,
                          'name': name,
                          'default_code': product.default_code,
                          }
                result.append(_name_get(mydict))
        return result

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if not args:
            args = []
        if name:
            positive_operators = ['=', 'ilike', '=ilike', 'like', '=like']
            products = self.env['product.product']
            if operator in positive_operators:
                products = self.search([('default_code', '=', name)] + args, limit=limit)
                if not products:
                    products = self.search([('barcode', '=', name)] + args, limit=limit)
            if not products and operator not in expression.NEGATIVE_TERM_OPERATORS:
                # Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
                # on a database with thousands of matching products, due to the huge merge+unique needed for the
                # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
                # Performing a quick memory merge of ids in Python will give much better performance
                products = self.search(args + [('default_code', operator, name)], limit=limit)
                if not limit or len(products) < limit:
                    # we may underrun the limit because of dupes in the results, that's fine
                    limit2 = (limit - len(products)) if limit else False
                    products += self.search(args + [('name', operator, name), ('id', 'not in', products.ids)], limit=limit2)
            elif not products and operator in expression.NEGATIVE_TERM_OPERATORS:
                products = self.search(args + ['&', ('default_code', operator, name), ('name', operator, name)], limit=limit)
            if not products and operator in positive_operators:
                ptrn = re.compile('(\[(.*?)\])')
                res = ptrn.search(name)
                if res:
                    products = self.search([('default_code', '=', res.group(2))] + args, limit=limit)
            # still no results, partner in context: search on supplier info as last hope to find something
            if not products and self._context.get('partner_id'):
                customers = self.env['product.customerinfo'].search([
                    ('name', '=', self._context.get('partner_id')),
                    '|',
                    ('product_code', operator, name),
                    ('product_name', operator, name)])
                if customers:
                    products = self.search([('product_tmpl_id.customer_ids', 'in', customers.ids)], limit=limit)

                else:
                    suppliers = self.env['product.supplierinfo'].search([
                    ('name', '=', self._context.get('partner_id')),
                    '|',
                    ('product_code', operator, name),
                    ('product_name', operator, name)])
                    if suppliers:
                        products = self.search([('product_tmpl_id.seller_ids', 'in', suppliers.ids)], limit=limit)
        else:
            products = self.search(args, limit=limit)
        return products.name_get()

class CustomerInfo(models.Model):
    _name = "product.customerinfo"
    _description = "Information about a product Customer"
    _order = 'sequence'

    name = fields.Many2one(
        'res.partner', 'Customer',
        domain=[('customer', '=', True)], ondelete='cascade', required=True,
        help="Customer of this product")
    product_name = fields.Char('Customer Product Name')
    product_code = fields.Char('Customer Product Code')
    sequence = fields.Integer('Sequence', default=1)
    product_id = fields.Many2one('product.product', 'Product Variant')
    product_tmpl_id = fields.Many2one('product.template', 'Product Template', index=True, ondelete='cascade')


import string
import random
def hangmanGame(words):
    # this filters words with - and a white space
    def filterWords():
        word = random.choice(words)
        while "-" in word and " " in word:
            word = random.choice(words)
        return word.lower()
    def game():
        gWord = list(filterWords())
        xWord = gWord.copy()
        alphabets = list(string.ascii_lowercase)
        usrRepeat = []
        print("type: IDK to give up")
        while len(gWord) != 0 :
            displayedWord = ['-' if item not in usrRepeat else item for item in xWord ]
            print("-------------------------------------------------------------------------")
            print("Guess Word:\n",' '.join(displayedWord), "\nGuessed Words:\n", " ".join(usrRepeat))
            print("-------------------------------------------------------------------------")
            usrInput = str(input("Enter a letter: ")).lower()
            if usrInput in alphabets:
                if usrInput in gWord:
                    usrRepeat.append(usrInput)
                    gWord.remove(usrInput)
                else:
                    print("\nPlease try again\n")
            elif usrInput == "idk":
                print("the word was: ", ''.join(gWord))
                break
            else:
                print("Invalid character")
                break
        print("#########################################################################")
        print("Congrats you guess the words: ", ''.join(xWord))
        print("#########################################################################")
    game()


'''
    May 30, 2020
    Script to add new models to our master data CSVs

    PREFACE:
    - In our web application, we use an external source to get the makes and models of vehicles manufactured in the US
    - This data source simply contains year, make, model for us to mine information from later
    - We need to be able to seamlessly add new vehicles to our 'master_data' from the external source,
    while maintaining the information we already have. This script helps accomplish that task
    - See source here : https://github.com/abhionlyone/us-car-models-data

    EXAMPLE:
    - Lets say within our master data set, we have all the vehicles produced by Chevrolet in 2020
    - We first mined for data on these vehicles in January 2020
    - At that point, Chevrolet produced 12 vehicle models, so we ran our other scripts on these 12 models
    and gathered information for them (trims, images, etc)

    - Fast forward to June 2020, now additional information has become available and Chevrolet added two models for 2020
    - Now we need to add these two models to our data set, and then run the other scripts later on the new cars we just added
    - Right now, we just want to add these two new models to our master data set. IMPORTANT! We're not doing anything else here.

    NOTE:
    - This script is only intended to merge the newly discovered models into our master data set
    - At the point of running this service, we don't have and trim information, or image sources

    USAGE:
    - Download the new CSVs from the above URL, and place the external source CSVs into their own folder called 'external_master'
    - Ensure that you have the 'master_data' available in the same directory

    The script will open the new CSVs, and the master CSVs, and compare each row.
    If a row (defined as year/make/model) exists in the new CSV and not in the master CSV,
    a new row will be created and then added into the master data set. The other colums such as trim_data and image_sources
    will remain empty.. To be filled in a later step

    The result is a new CSV with new models filled in amongst the old models we already have
'''
# ----------------------
import csv
# ----------------------
# Helper to get the external source filename given a year
def get_external_source_filepath(year):
    return 'external_master/'+str(year)+'.csv'
# ----------------------
# Helper to get the master source filename given a year
def get_master_source_filepath(year):
    return 'master_data/'+str(year)+'.csv'
# ----------------------
# Helper to get the target destination filepath, which will be the outer directory for now
def get_new_destination_filepath(year):
    return 'models_added/'+str(year)+'.csv'
# ----------------------
def external_data_to_list(year):
    # Get current filename
    curr_filename = get_external_source_filepath(year)

    # Open file and get a cursor to read the file
    with open(curr_filename) as file:
        next(csv.reader(file)) # Skip the header
        data_list = list(csv.reader(file))

    # Return the new list
    return data_list
# ----------------------
def master_data_to_list(year):
    # Get current filename
    curr_filename = get_master_source_filepath(year)

    # Open file and get a cursor to read the file
    with open(curr_filename) as file:
        next(csv.reader(file)) # Skip the header
        data_list = list(csv.reader(file))

    # Return the new list
    return data_list
# ----------------------
def clear_double_quotes(string):
    return str(string).replace('"',"'")
# ----------------------
def add_external_source_data_to_master_data(year):
    # Get new file path (destination)
    new_path = get_new_destination_filepath(year)

    # Open the new file
    with open(new_path, 'w') as new_file:
        # Create writer object
        writer_object = csv.writer(new_file)

        # Write the headers
        writer_object.writerow(['year', 'make', 'model', 'body_styles', 'trim_data', 'image_sources'])

        # Convert the old and new files to lists
        old_list = master_data_to_list(year)
        new_list = external_data_to_list(year)
        old_counter = 0
        new_counter = 0

        # Go through both new and old files and compare the rows to look for any additions
        while new_counter != len(new_list):

            # Get old row and new row, as well each rows models
            old_row = old_list[old_counter]
            old_model = old_row[2]
            new_row = new_list[new_counter]
            new_model = new_row[2]

            if new_model != old_model:
                # Write the new year, make, model, and bodystyle. We'll get trim_data and image_sources later
                print('Adding a new model to the data set: '+str(new_row))
                writer_object.writerow([new_row[0], new_row[1], new_row[2], clear_double_quotes(new_row[3]), '', ''])
                new_counter += 1 # Move the new file's cursor until it matches the old file's cursor again

            else:
                # Write the old row's info, its not a new addition and we've already got its info
                writer_object.writerow([old_row[0], old_row[1], old_row[2], old_row[3], old_row[4], old_row[5]])
                new_counter += 1
                old_counter += 1
    return None
# ----------------------
def create_updated_csv_directory():
    import os, pathlib
    curr_path = str(pathlib.Path().absolute())
    new_path = curr_path+'/models_added'
    try:
        os.mkdir(new_path)
    except OSError:
        print('Failed to create directory '+new_path)
# ----------------------
def add_models(year):
    add_external_source_data_to_master_data(year)
    return None
# ----------------------


import django_filters
from django_filters.widgets import RangeWidget
from django_filters import FilterSet, DateFromToRangeFilter

from clients.models import ClientProject

class ClientProjectsFilter(FilterSet):
    # testt = django_filters.CharFilter(lookup_expr='iexact')

    # price = django_filters.NumberFilter()
    # client = django_filters.ChoiceFilter(field_name='client_id', label='CLLL')
    # price__lt = django_filters.NumberFilter(field_name='price', lookup_expr='lt')
    #
    # release_year = django_filters.NumberFilter(field_name='release_date', lookup_expr='year')
    next_amc_date = DateFromToRangeFilter(label='AMC Date Renewal Date Between',
                                          widget=RangeWidget(attrs=
                                                             {
                                                                 'display': 'inline',
                                                                 'class' : 'datepicker',
                                                                 'data-date-format':"yyyy-mm-dd",
                                                             }))
    # release_year__lt = django_filters.NumberFilter(field_name='release_date', lookup_expr='year__lt')

    # manufacturer__name = django_filters.CharFilter(lookup_expr='icontains')

    class Meta:
        model = ClientProject
        fields = ['client', 'project']


import unittest
import os
import sys
import dolphindb as ddb
from datetime import datetime
sys.path.append('..')
from dolphindb import *
from xxdb_server import HOST, PORT
rs = lambda s: s.replace(' ', '')
s = ddb.session(HOST, PORT)
s = session()
s.connect(HOST,PORT,"admin","123456")
WORK_DIR = "C:/Tutorials_EN/data"

# print("test loadText")
# trade = s.loadText(filename=WORK_DIR+"/example.csv")
# print(trade.select("distinct TICKER").toDF())
# #print(trade.toDF())
# print(trade.select("min(date) as date").toDF())
# # 2
# print("test loadText parallel mode")
# trade = s.ploadText( WORK_DIR+"/example.csv")
# print(trade.rows)

# 3
print("test drop & create partitioned database; loadTextEx")
if s.existsDatabase(WORK_DIR+"/valuedb"  or os.path.exists(WORK_DIR+"/valuedb")):
    s.dropDatabase(WORK_DIR+"/valuedb")
s.database(dbName='db', partitionType=VALUE, partitions=["AMZN","NFLX", "NVDA"], dbPath=WORK_DIR+"/valuedb")
t = s.loadTextEx("db",  tableName='trade',partitionColumns=["TICKER"], filePath=WORK_DIR + "/example.csv")
print(t.top(5).toDF())
print(t.where('TICKER=`AMZN').where('date>2016.12.15').sort('date').selectAsVector('prc'))

trade = s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade", memoryMode=True)
print(trade.select(['ticker','date','bid','ask','prc','vol']).top(5).toDF())

# print(trade.select("ticker, date, vol").toDF())
# df= s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade").contextby('ticker').top(5).toDF()
# print(df)
# df= s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade").contextby('ticker').having(" sum(VOL)>40000000000").toDF()
# print(df)
# t= s.loadText(filename=WORK_DIR+"/example.csv")
# wjt = t.merge_window(t, -10, -5, aggFunctions='<sum(VOL)>', on=["TICKER","date"], prevailing=True).toDF()
# print(wjt)
#
# trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade")
# # c1 = trade.rows
# # print (c1)
# # top10 = trade.top(10).executeAs("top10")
# #
# # c2 = trade.append(top10).rows
# # print (c2)
# # trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade")
# # print(trade.rows)
#
# trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade")
# trade = trade.update(["VOL"],["999999"]).where("TICKER=`AMZN").where(["date=2015.12.16"]).execute()
# trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade")
# t1=trade.where("ticker=`AMZN").where("VOL=999999")
# print(t1.toDF())
#
#
# print("here")
#
# #4 To load only the "AMZN" partition:
# trade = s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade", partitions="AMZN")
# print(trade.rows)
#
# #5 To load "NFLX" and "NVDA" partitions as an in-memory partitioned table:
#
# trade = s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade", partitions=["NFLX","NVDA"], memoryMode=True)
# print(trade.rows)

# #6 We can use a list of field names in **select** method to select columns. We can also use **where** method to filter the selection.
#
# trade = s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade", memoryMode=True)
# print(trade.select(['date','bid','ask','prc','vol']).where('TICKER=`AMZN').where('bid!=NULL').where('ask!=NULL').where('vol>10000000').sort('vol desc').top(5).toDF())
# print(trade.select(['date','bid','ask','prc','vol']).where('TICKER=`AMZN').where('bid!=NULL').where('ask!=NULL').where('vol>10000000').sort('vol desc').showSQL())

#7

print(trade.select("ticker, date, vol").where("bid!=NULL, ask!=NULL, vol>50000000").top(5).toDF())

#8

print(trade.select('ticker').groupby(['ticker']).count().sort(bys=['ticker desc']).toDF())

#9

print(trade.select(['vol','prc']).groupby(['ticker']).sum().toDF())

#10

print(trade.select('count(ask)').groupby(['vol']).having('count(ask)>1').toDF())

#11

df= s.loadTable(dbPath=WORK_DIR+"/valuedb", tableName="trade").select(["vol","prc"]).contextby('ticker').sum().top(10).toDF()
print(df)

#12

trade = s.loadTable(dbPath=WORK_DIR + "/valuedb", tableName="trade", memoryMode=True)
z=trade.select(['bid','ask','prc']).ols('PRC', ['BID', 'ASK'])
print(z['ANOVA'])
print(z["RegressionStat"])
print(z["Coefficient"])
print(z["Coefficient"].beta[1])

#13

trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade")
t1 = s.table(data={'TICKER': ['AMZN', 'AMZN', 'AMZN'], 'date': ['2015.12.31', '2015.12.30', '2015.12.29'], 'open': [695, 685, 674]})
print(trade.merge(t1,on=["TICKER","date"]).select(["date","prc","open"]).toDF())
# print(trade.merge(t1,on=["ticker","date"]).toDF())

#14
from datetime import datetime
dates = [Date.from_date(x) for x in [datetime(1993,12,31), datetime(2015,12,30), datetime(2015,12,29)]]
t1 = s.table(data={'TICKER': ['AMZN', 'AMZN', 'AMZN'], 'date': dates, 'open': [695, 685, 674]})
print(trade.merge_asof(t1,on=["date"]).top(20).toDF())

#15

trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade", inMem=True)
trade = trade.update(["VOL"],["999999"]).where("TICKER=`AMZN").where(["date=2015.12.16"]).execute()
t1=trade.where("ticker=`AMZN").where("VOL=999999")
print(t1.count())

# 16

trade = s.table(dbPath=WORK_DIR+"/valuedb", data="trade", inMem=True)
trade.delete().where('date<2013.01.01').execute()
print(trade.rows)

#17
trade = s.table(dbPath=WORK_DIR + "/valuedb", data="trade", inMem=True)
t1=trade.drop(['ask', 'bid'])
print(t1.top(5).toDF())

#18
trade = s.table(dbPath=WORK_DIR + "/valuedb", data="trade", inMem=True)
t1=trade.drop('ask')
print(t1.top(5).toDF())

#19

s.dropTable(WORK_DIR + "/valuedb", "trade")

#20

trade=s.ploadText( WORK_DIR+"/example.csv")
print(trade.toDF())

#21
s.database('db',partitionType=VALUE, partitions=["AMZN","NFLX", "NVDA"], dbPath="")
trade=s.loadTextEx(dbPath="db", partitionColumns=["TICKER"], tableName='trade', filePath=WORK_DIR + "/example.csv")
print(trade.rows)

#
if s.existsDatabase(WORK_DIR+"/valuedb"  or os.path.exists(WORK_DIR+"/valuedb")):
    s.dropDatabase(WORK_DIR+"/valuedb")
s.database(dbName='db', partitionType=VALUE, partitions=["AMZN","NFLX", "NVDA"], dbPath=WORK_DIR+"/valuedb")
t = s.loadTextEx("db",  tableName='trade',partitionColumns=["TICKER"], filePath=WORK_DIR + "/example.csv")
trade = s.loadTableBySQL(dbPath=WORK_DIR + "/valuedb",tableName="trade", sql="select * from trade where date>2010.01.01")
print(trade.rows)