from ..fs.deimpl import MotorFS, AdditiveMotorFS
from ..fs.deimpl import SimpleMotivFS, MotivationFS, SecondaryFS
from ..fs import BaseFSNetwork

from . import de_equations as de_eqs
from . import lm_equations as lm_eqs
from ..fs.lmimpl import LMSecondary, OneActivationSecondary

from ..env import Environment

#-----action_fs----------------------------------
act_x0 = 0.25 #0.25
act_k = 10.0
act_edge = 1.5
act_tau = 1.0
act_alpha = 1.0#0.3
act_beta = 2.0
act_gamma = 2.0
act_x1 = 0.1
act_tauc0 = 1 #0.01
act_tauc1 = 25
act_sigma1 = 0.001 #0.001
act_sigma2 = 0.001 #0.001
#----/action_fs-----------------------------------
#-----secondary_fs--------------------------------
sec_x0 = 0.25
sec_k = 10
sec_edge = 1.0
sec_tau = 1.0
sec_alpha = 1.5 # 1.0
sec_beta = 2.0
sec_gamma = 2.0
sec_x1 = 0.1
sec_tauc0 = 1.0 #0.01
sec_tauc1 = 30.0
sec_sigma1 = 0.001#0.001
sec_sigma2 = 0.001#0.001
#----/secondary_fs--------------------------------
#-----motivational_fs--------------------------------
motiv_edge = 2.0
mot_k = 10
mot_x0 = 0.5
mot_sigma1 = 0.01
mot_sigma2 = 0.01
mot_alpha = 1.0 # 1.0
mot_beta = 2.0
mot_gamma = 0.0
mot_edge = 2.0

mot_tau = 1.0
mot_tau1 = 3 # 20
mot_tau2 = 30
mot_tau3 = 10
mot_tau4 = 10
#----/motivational_fs--------------------------------

def create_empty_network(name = "FSNetwork"):
    return BaseFSNetwork(act_edge, sec_edge, motiv_edge, name)

def create_network(env, create_motor, name = "FSNetwork"):
    assert isinstance(env, Environment)

    net = BaseFSNetwork(act_edge, sec_edge, motiv_edge, name)
    for i in xrange(env.get_dimension()):
        mot0 = create_motor(env, net, i, 0)
        mot1 = create_motor(env, net, i, 1)
        net.add_motor(mot0)
        net.add_motor(mot1)

    return net

# when motor_fs is connected to edge index instead of vertex coordinate
def create_edge_moving_network(env, create_motor, name = "FSNetwork"):
    assert isinstance(env, Environment)
    net = BaseFSNetwork(act_edge, sec_edge, motiv_edge, name)

    max_degree = max(len(v.get_outcoming()) for v in env.vertices())

    for i in xrange(max_degree):
        mot0 = create_motor(env, net, i)
        net.add_motor(mot0)

    return net

def simple_motiv(env, goal):
    return SimpleMotivFS(env, goal)

def motiv(env, goal):
    delta_si = de_eqs.delta_si_maker(mot_k, mot_x0, mot_sigma1)
    delta_ri = de_eqs.delta_ri_maker(mot_tau1, mot_sigma2)
    calc_I = de_eqs.ii_maker(mot_alpha, mot_beta, mot_gamma, 0)

    calc_IA = lambda x: 1.0
    calc_AR = de_eqs.mot_ar_maker( mot_tau1, mot_tau)
    numerical_integration = de_eqs.delta_RS_with_noise(de_eqs.delta_RS_rk4(1.0))
    m = MotivationFS(env, de_eqs.delta_RS_rk4(), delta_si, delta_ri, calc_IA, calc_AR, calc_I, goal)
    return m

def secondary(env, motiv_fs, prev, goal):
    delta_si = de_eqs.delta_si_maker(sec_k, sec_x0, sec_sigma1)
    delta_ri = de_eqs.delta_ri_maker(sec_tau, sec_sigma2)
    delta_ci = de_eqs.delta_ci_maker(sec_k, sec_x1, sec_tauc0, sec_tauc1)
    calc_IA = de_eqs.sec_ia_maker(sec_k,0.1)
    calc_AR = de_eqs.sec_ar_maker(sec_k,0.1)
    #calc_IA = eqs.foo_maker(21, 31)
    #calc_AR = eqs.foo_maker(81, 91)
    calc_ii = de_eqs.ii_maker(sec_alpha, sec_beta, sec_gamma, 0)
    sec =  SecondaryFS(env, delta_si, delta_ri, delta_ci, calc_IA, calc_AR, calc_ii, motiv_fs, prev, goal)
    #sec.active_threshold = 2.0
    return sec

def motor(env, net, index, coord_val):
    assert  isinstance(net, BaseFSNetwork)
    motiv_cn = net.get_cnet(net.MOTIV_NET)
    delta_si = de_eqs.delta_si_maker(act_k, act_x0, act_sigma1)
    delta_ri = de_eqs.delta_ri_maker(act_tau, act_sigma2)
    delta_ci = de_eqs.delta_ci_maker(act_k, act_x1, act_tauc0, act_tauc1)
    #calc_IA = de_eqs.motor_ia_maker(act_k, 0.1, coord_val) #eqs.foo_maker(0, 240)  #
    calc_IA = de_eqs.motor_ia_maker3(de_eqs.discrete_f_maker(0.5), index, coord_val)
    calc_AR = de_eqs.motor_ar_maker(act_k, 0.1, coord_val)
    calc_ii = de_eqs.ii_maker(act_alpha, act_beta, act_gamma, 1)
    return MotorFS(env, delta_si, delta_ri, delta_ci, calc_IA, calc_AR, calc_ii, motiv_cn, index, coord_val)

def edge_moving_motor(env, net, index, coord_val=None):
    assert isinstance(net, BaseFSNetwork)
    motiv_cn = net.get_cnet(net.MOTIV_NET)
    delta_si = de_eqs.delta_si_maker(act_k, act_x0, act_sigma1)
    delta_ri = de_eqs.delta_ri_maker(act_tau, act_sigma2)
    delta_ci = de_eqs.delta_ci_maker(act_k, act_x1, act_tauc0, act_tauc1)
    calc_IA = de_eqs.motor_ia_maker4(de_eqs.discrete_f_maker(0.5), index)
    calc_AR = lambda fs: 0
    calc_ii = de_eqs.ii_maker(act_alpha, act_beta, act_gamma, 1)
    return MotorFS(env, delta_si, delta_ri, delta_ci, calc_IA, calc_AR, calc_ii, motiv_cn, index, coord_val)

def base_fs(env, motiv_fs, index, coord_val):
    delta_si = de_eqs.delta_si_maker(act_k, act_x0, act_sigma1)
    delta_ri = de_eqs.delta_ri_maker(act_tau, act_sigma2)
    delta_ci = lambda si, ci: 0
    calc_IA = de_eqs.foo_maker(21, 161)
    calc_AR = de_eqs.foo_maker(150, 161)
    calc_ii = de_eqs.ii_maker(act_alpha, 1.6, act_gamma, 1) #act_beta
    m =  MotorFS(env, delta_si, delta_ri, delta_ci, calc_IA, calc_AR, calc_ii, motiv_fs, index, coord_val)
    m.deactivation_method = lambda: True
    return m

#deactivation in case of absence of the environment reaction
def lm_secondary(net, env, motiv_fs, prev, goal):
    calc_IA = lm_eqs.sec_IA_maker()
    calc_AR = lm_eqs.sec_AR_maker()
    calc_ii = lm_eqs.sec_ii_maker(0.5)
    return LMSecondary(net, env, motiv_fs, prev, goal, calc_IA, calc_AR, calc_ii, lm_eqs.delta_si)

#deactivation after one activity period
def lm_secondary2(net, env, motiv_fs, prev, goal, deactivation_delay=20):
    calc_IA = lm_eqs.weighted_IA_maker(1.0) #lm_eqs.sec_IA_maker()
    calc_AR = lm_eqs.sec_AR_maker()
    calc_ii = lm_eqs.sec_ii_maker(0.3)
    return OneActivationSecondary(net, env, motiv_fs, prev, goal,
                                  calc_IA, calc_AR, calc_ii, lm_eqs.delta_si_with_deactivation,
                                  activity_limit=deactivation_delay)

#deactivation_disabled
def lm_secondary3(net, env, motiv_fs, prev, goal):
    calc_IA = lm_eqs.sec_IA_maker()

    calc_IA = de_eqs.foo_maker(25, 65)
    calc_AR = lm_eqs.sec_AR_maker()
    calc_ii = lm_eqs.sec_ii_maker(0.5)

    lm=LMSecondary(net, env, motiv_fs, prev, goal, calc_IA, calc_AR, calc_ii, lm_eqs.delta_si)
    lm.deactivation_method = MotorFS.deactivation_method
    return lm


#!/usr/bin/env python3

import sys
import sqlite3
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error

def ape(y_true, y_pred):
    return np.abs(y_pred - y_true)/y_true

def se(y_true, y_pred):
    return (y_true - y_pred)**2

def ae(y_true, y_pred):
    return np.abs(y_true - y_pred)

if(len(sys.argv) != 2):
    print("Usage:\n\t" + sys.argv[0] + " <samples.db>")
    exit(-1)

print("Reading samples..")
conn = sqlite3.Connection(sys.argv[1])
samples = pd.read_sql_query(
    'select * from samples',
    conn, index_col=['bench','app','dataset','name'])

# Samples can be filtered by index
# This example get all entry in which the index 1 (app) is equal to 'kmeans'
# samples[samples.index.get_level_values(1) == 'kmeans']

X = samples[samples.columns[1:-1]]
y = samples['time']

print("Dividing into train and test set..")
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=17)

# Referr to https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor

###### Complete this section:

# regr =

##### End of Section

print("Train model..")
regr.fit(X_train, y_train)
y_predict = regr.predict(X_test)
print("Predicting:")
results = pd.DataFrame()
results['y_true'] = y_test
results['y_predict'] = regr.predict(X_test)
results['mse'] = se(y_test, results['y_predict'])
results['mae'] = ae(y_test, results['y_predict'])
results['mape'] = ape(y_test, results['y_predict'])
print(results)
print('Mean errors:')
print(results.mean())


'''
Created on Jun 27, 2016

@author: ubuntu
'''
import sqlite3 as database

class DBConnection(object):
    '''
    classdocs
    '''

    def __init__(self):
        self.__connection = None
        self.__cursor = None
        self.__database_name = "database.db"

        self.__createTables()

    def __createTables(self):

        query1 = '''CREATE TABLE IF NOT EXISTS devices
             (serial TEXT PRIMARY KEY NOT NULL)'''

        query2 = '''CREATE TABLE IF NOT EXISTS dev_values
             (serial TEXT PRIMARY KEY NOT NULL, dev_value TEXT NOT NULL)'''

        self._commit(query1)
        self._commit(query2)

    def __connect (self):
        self.__connection = database.connect(self.__database_name)
        self.__cursor = self.__connection.cursor()

        return self.__cursor

    def __disconnect(self):
        self.__cursor.close()
        self.__connection.close()

    def _commit(self,query):
        self.__cursor = self.__connect()
        self.__cursor.execute(query)
        self.__connection.commit()
        self.__disconnect()

        return self.__cursor

    def _query(self,query):
        self.__cursor = self.__connect()
        return self.__cursor.execute(query)


import pathlib
import platform
import shutil
import textwrap
import unittest

import onlinejudge_verify.languages.cplusplus_bundle as cplusplus_bundle
import tests.utils
from onlinejudge_verify.languages.cplusplus_bundle import BundleError

@unittest.skipIf(platform.system() == 'Darwin', 'We cannot use the fake g++ of macOS.')
class TestStringMethods(unittest.TestCase):
    def test_no_newline(self) -> None:
        # 末尾に改行がないコードをincludeした時に改行が足されていることの確認

        files = {
            'no_newline.cpp': b'void foo() {}',
            'example.test.cpp': textwrap.dedent("""\
                #include "no_newline.cpp"
                #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_B"
                """).encode(),
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir])
                bundler.update(path)
                self.assertIn(b'void foo() {}\n', bundler.get())

    def test_uncommenting(self) -> None:
        # prepare files
        files = {
            'foo.cpp': textwrap.dedent("""\
                void foo() {}
                """).encode(),
            'bar.cpp': textwrap.dedent("""\
                void bar() {}
                """).encode(),
            'example.test.cpp': textwrap.dedent("""\
                #include "foo.cpp"
                /*
                #include "bar.cpp"
                */
                // #include "bar.cpp"
                """).encode(),
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):

                # check the result
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir])
                bundler.update(path)
                bundled = bundler.get()
                self.assertIn(b'void foo() {}', bundled)
                self.assertNotIn(b'void bar() {}', bundled)

    @unittest.skipIf(shutil.which('clang++') is None, 'clang++ is required for this test')
    def test_reject_clang(self) -> None:
        files = {
            'example.test.cpp': b'',
        }
        path = pathlib.Path('example.test.cpp')
        with tests.utils.load_files(files) as tempdir:
            with tests.utils.chdir(tempdir):
                bundler = cplusplus_bundle.Bundler(iquotes=[tempdir], compiler='clang++')
                self.assertRaises(BundleError, lambda: bundler.update(path))


import pdb

import argparse

try:
    from solverRuns import run_solver, get_finite_witness
    from utils.SimpleTree import Formula
except:
    from encoding.solverRuns import run_solver, get_finite_witness
    from encoding.utils.SimpleTree import Formula

try:
    from utils.Traces import Trace, ExperimentTraces
except:
    from encoding.utils.Traces import Trace, ExperimentTraces

import logging

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log", dest="loglevel", default="INFO")
    parser.add_argument("--candidate_formula_1", default="F(a)")
    parser.add_argument("--candidate_formula_2", default="F(&(a,b))")
    parser.add_argument("--trace_length", default = 5)

    args, unknown = parser.parse_known_args()

    """
    traces is
     - list of different recorded values (traces)
     - each trace is a list of recordings at time units (time points)
     - each time point is a list of variable values (x1,..., xk)
    """

    numeric_level = args.loglevel.upper()
    logging.basicConfig(level=numeric_level)

    trace_length = int(args.trace_length)

    f_1 = Formula.convertTextToFormula(args.candidate_formula_1)
    f_2 = Formula.convertTextToFormula(args.candidate_formula_2)
    difference_formula = Formula([encodingConstants.LOR,
                                 Formula([encodingConstants.LAND, f_1, Formula([encodingConstants.LNOT, f_2])]),
                                 Formula([encodingConstants.LAND, Formula([encodingConstants.LNOT, f_1]), f_2])
                                  ])

    #difference_formula = Formula(["&", f_1, Formula([encodingConstants.LNOT, f_2])])
    #difference_formula = Formula(["&", f_2, Formula([encodingConstants.LNOT, f_1])])

    # it is weird trace_length = trace_length +1 --> a mistake in the encoding that was reasons about number of states, rather than number of actions
    counterexample_witness = get_finite_witness(f=difference_formula, trace_length=trace_length)

    if counterexample_witness == "unsat":
        logging.error("Formulas {} and {} are equivalent (at least for all traces of length {}".format(f_1, f_2, trace_length))
    else:

        if counterexample_witness.evaluateFormulaOnTrace(difference_formula) == False:
            raise RuntimeError("looking for witness of satisfiability, but got a trace {} that is not a model for {}".format(cex, difference_formula))

        logging.info("the distinguishing trace is {}".format(counterexample_witness))

if __name__ == "__main__":
    main()


"""
Master Node
This program runs on the master.
"""
from threading import Thread
from queue import Queue
from enum import Enum
import socket
import json
import random
import time
import sys

def Status(x):
    status = {
        1 : "SUCCESS",
        2 : "PENDING",
        3 : "FAILED",
        4 : "ONGOING"
    }
    return status[x]

class Master:
    def __init__(self, config_path, scheduler):
        with open(config_path, 'r') as cfg_file:
            config = json.load(cfg_file)
        # for now I will not use a separate worker class, as this is simpler
        self.workers = config['workers']
        for w in self.workers:
            w['free_slot_count'] = w['slots']

        #self.scheduler=Scheduler(sc)
        self.scheduler = scheduler
        self.threads = []
        self.connections = []
        #self.job_pool = list()
        self.task_pool = list()
        # threads and connections
        self.request_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.worker_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # For context manager, establishes the connections and launches them in a new thread
    def __enter__(self):
        # conn_info is just a tuple of (conn, address) that comes from sock.accept()
        #self.request_conn_info = self.establish_connection(self.request_sock,1)
        #self.worker_conn_info = self.establish_connection(self.worker_sock,0)

        #self.connections.extend([self.request_conn_info, self.worker_conn_info])
        self.request_thread = Thread(target=self.request_listener)
        self.request_thread.start()

        self.worker_thread = Thread(target=self.worker_listener)

        self.worker_thread.start()

        #self.threads.extend([request_listener_thread, worker_listener_thread])
        return self

    # cleanup of resources
    def __exit__(self, exc_type, exc_value, traceback):
        self.request_sock.close()
        self.worker_sock.close()
        for conn, _ in self.connections:
            conn.close()
        for thread in self.threads:
            thread.join()

    def random_scheduler(self):
        tasks=self.task_pool
        workers=self.workers
        total_free_slots=0
        num=0
        total=len(tasks)#number of tasks
        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']
        print("TASKS TO DO :\n",tasks,"\n")
        print("Total number of free slots = ",total_free_slots)
        while(total):#until task list in empty
            for task in tasks:
                #print("Total number of free slots = ",total_free_slots)
                #print(total)
                if task['depends'] == [] and task['status'] == "PENDING":#if is a task with no dependencies
                    print("Found task with 0 dependencies..")
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if(total_free_slots > 0):
                        x = random.randrange(0,num)
                        if workers[x]['slots'] > 0:
                            task['wid'] = workers[x]['worker_id']
                            workers[x]['free_slot_count']-=1#updating free slot of that worker
                            master.send_task(task)
                            task['status']=Status(4)
                            #total_free_slots -= 1
                            total-=1

    def round_robin_scheduler(self):
        tasks=self.task_pool
        workers=self.workers
        total_free_slots=0
        num=0
        total=len(tasks)#number of unassigned tasks

        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']
        #tj1m1,j1m2,j1m3,j1r1,j2m1,j2m2
        c = 0
        #mytasks=task[:]
        while(total):#until task list in empty
            for task in tasks:
                if task['depends'] == []:#if is a task with no dependencies
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if total_free_slots > 0 :
                        if workers[c]['slots'] > 0:
                            task['wid'] = workers[c]['worker_id']
                            workers[c]['free_slot_count']-=1
                                #total_free_slots -= 1
                            master.send_task(task)
                            c = (c+1)%num
                            total-=1
                else:
                    continue
                #mytasks.remove(task)

    def least_loaded_scheduler(self):
        tasks=self.task_pool
        workers=self.workers
        total_free_slots=0
        total=len(tasks)
        num=0
        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']
        #task_mapping = []

        while(total):#until task list in empty
            for task in tasks:
                if task['depends'] == []:#if is a task with no dependencies
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if(total_free_slots > 0):
                        if task['depends'] == []:
                            y=list(map(lambda x: x['free_slot_count'], workers))
                            x=y.index(max(y))
                            if workers[x]['slots'] > 0:
                                task['wid'] = workers[x]['worker_id']
                                workers[x]['free_slot_count']-=1
                                master.send_task(task)
                                total_free_slots -= 1

    def request_listener(self):
        c = 1
        port= 5000
        self.request_sock.bind(('localhost',port))
        self.request_sock.listen(5)
        while(1):
            connection,addr=self.request_sock.accept()

            while(1):
                message=connection.recv(1024)
                '''data = {'job_id': '0',
                'map_tasks': [{'task_id': '0_M0', 'duration': 3}, {'task_id': '0_M1', 'duration': 3}, {'task_id': '0_M2', 'duration': 2}],
                'reduce_tasks': [{'task_id': '0_R0', 'duration': 3}, {'task_id': '0_R1', 'duration': 3}]}'''
                if(len(message)!=0):
                    print("Adding tasks...\n")
                    data = json.loads(message)
                    job = {}
                    job['id'] = data['job_id']
                    job['tasks'] = list()
                    print("Initial task pool:\n",self.task_pool)
                    maptasks=list()#stores maptask ids
                    redtasks=list()#stores redtask ids

                    for red in data['reduce_tasks']:
                         redtasks.append(red['task_id'])

                    for mapper in data['map_tasks']:
                        print("IN MAPPER LOOP!!!!")
                        task={}
                        task['job_id'] = job['id']
                        task['task_id'] = mapper['task_id']
                        maptasks.append(mapper['task_id'])
                        task['duration'] = mapper['duration']
                        task['wid'] = []
                        task['depends'] = []
                        task['satisfies'] = redtasks
                        task['status'] = Status(2)
                        self.task_pool.append(task)
                        job['tasks'].append(task)
                    print("Task pool after the mapper loop :\n",self.task_pool)

                    for red in data['reduce_tasks']:
                        print("IN REDUCER LOOP!!!!")
                        task={}
                        task['job_id']=job['id']
                        task['task_id']=red['task_id']
                        task['duration']=red['duration']
                        task['wid'] = []
                        task['depends']=maptasks
                        task['satisfies'] = []
                        task['status']=Status(2)
                        self.task_pool.append(task)
                        job['tasks'].append(task)
                    print("Task pool after the reducer loop :\n",self.task_pool)

                #self.job_pool.put(job)#adding job to jobpool
                #print("Taskpool updated")

    def worker_listener(self):
        print("Listening to workers now...\n")
        s=self.worker_sock

        port= 5001
        s.bind(('localhost',port))
        s.listen(5)
        while(1):
            connection,addr=s.accept()
            while(1):
                message=connection.recv(1024)#dict task
                if(len(message)!=0):
                    data=message.decode()
                    task=json.loads(data)
                    #need to update task pool
                    #need to update satisfies if it is map task
                    self.update_worker_params(task)

    def update_worker_params(self,task):
        # If the update is from a mapper ->
        # find the tasks it satisfies and remove this from their depends lists
        # remove this map task from the task pool
        # If the update is from a reducer ->
        # remove this red task from the task pool
        wid=task['wid']
        t = task['task_id']
        print("TRYING TO UPDATE :")
        print("task_id : ",t)
        print("worker : ",wid)

        red_modify = []
        if "M" in t:
            red_modify = task['satisfies']
        print("red_modify : ", red_modify)
        for tp in self.task_pool:
            print()
            tp_id = tp['task_id']
            print("Current tp_id = ",tp_id)
            print(tp['depends'])
            if  tp_id == t:
                self.task_pool.remove(tp)
            elif tp_id in red_modify:
                tp['depends'].remove(t)

        for w in self.workers:
            if(w['worker_id']==wid):
                w['free_slot_count']+=1

    def run(self):
        print("In master.run()")
        while(1):
            if(len(self.task_pool)<4):
                time.sleep(1)

            elif(self.scheduler=='round robin'):
                print("Calling round robin on this")
                self.round_robin_scheduler()

            elif(self.scheduler=='random'):
                print("Calling random on this")
                self.random_scheduler()

            elif(self.scheduler=='least loaded'):
                self.least_loaded_scheduler()

            else:
                continue

    #def start_job(self):
        pass

    def send_task(self, task):
        print("Sending task :\n",task)
        wid=task['wid']
        port=[worker['port'] for worker in self.workers if worker['worker_id']==wid ]
        print("PORT = ",port)
        '''
        for worker in self.workers:
            if(worker['worker_id']==wid):
                port=worker['port']
                break'''

        #need to find which port number to send to
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect(("localhost", port[0]))#sending message to worker
            message=json.dumps(task)
            #send task
            s.send(message.encode())
            s.close()

if __name__ == '__main__':
    # Context management protocol, all threads and connections
    # are automatically closed after it's done

    '''config=sys.argv[1]
    scheduler=sys.argv[2]'''
    with Master("config.json","random") as master:
        master.run()

'''class Scheduler:

    def __init__(scheduler):
        self.scheduler = scheduler

    @staticmethod
    def random_scheduler(tasks, workers):
        total_free_slots=0
        num=0
        total=len(tasks)#number of tasks
        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']

        while(total):#until task list in empty
            for task in tasks:
                if task['depends'] == []:#if is a task with no dependencies
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if(total_free_slots > 0):
                        x = random.randrange(0,num)
                        if workers[x]['slots'] > 0:
                            task['wid'] = workers[x]['worker_id']
                            workers[x]['free_slot_count']-=1#updating free slot of that worker
                            master.send_task(task)
                            #total_free_slots -= 1
                            total-=1

    @staticmethod
    def round_robin_scheduler(tasks, workers):
        total_free_slots=0
        num=0
        total=len(tasks)#number of unassigned tasks

        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']
        #tj1m1,j1m2,j1m3,j1r1,j2m1,j2m2
        c = 0
        #mytasks=task[:]
        while(total):#until task list in empty
            for task in tasks:
                if task['depends'] == []:#if is a task with no dependencies
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if total_free_slots > 0 :
                        if workers[c]['slots'] > 0:
                            task['wid'] = workers[c]['worker_id']
                            workers[c]['free_slot_count']-=1
                                #total_free_slots -= 1
                            master.send_task(task)
                            c = (c+1)%num
                            total-=1
                else:
                    continue
                #mytasks.remove(task)

    @staticmethod
    def least_loaded_scheduler(tasks, workers):
        total_free_slots=0
        total=len(tasks)
        num=0
        for w in workers:
            num+=1
            total_free_slots+=w['free_slot_count']
        #task_mapping = []

        while(total):#until task list in empty
            for task in tasks:
                if task['depends'] == []:#if is a task with no dependencies
                    while(total_free_slots==0):#check for free slots
                        time.sleep(1)
                        num=0
                        for w in workers:#check for number of slots
                            num+=1
                            total_free_slots+=w['free_slot_count']

                    if(total_free_slots > 0):
                        if task['depends'] == []:
                            y=list(map(lambda x: x['free_slot_count'], workers))
                            x=y.index(max(y))
                            if workers[x]['slots'] > 0:
                                task['wid'] = workers[x]['worker_id']
                                workers[x]['free_slot_count']-=1
                                master.send_task(task)
                                total_free_slots -= 1'''
'''
def update_worker_params(self,task):
    key=self.task_pool.index(task)
    self.task_pool[key]['status']=Status(1)

    for i in self.task_pool:
        if task['task_id'] in i['depends']:
            i['depends'].remove(task['task_id'])

    wid=task['wid']
    for w in self.workers:
        if(w['worker_id']==wid):
            w['free_slot_count']+=1

'''


# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 12:23:30 2020

@author: Lorenzo
"""
import os
import glob
import pandas as pd
import re
import datetime
import numpy as np
import pickle

nmin = 1
TICKER = 'SPY'

os.chdir(os.path.join('data', TICKER, TICKER+'_extracted_files'))

extension = 'csv'
dir_paths = os.listdir()
csv_file_list = []
output_path = os.path.join(os.path.pardir,TICKER +'_equidistant_log_returns')

#get a list of all subdirectories and search for csv files in these subdirectories

for dir_path in dir_paths:
    file_path = os.path.join(dir_path,'*.{}'.format(extension))
    csv_file_list_to_add = [i for i in glob.glob(file_path)]
    csv_file_list += csv_file_list_to_add

csv_orderbook = [name for name in csv_file_list if 'orderbook' in name]
csv_message   = [name for name in csv_file_list if 'message'   in name]

#check if exactly half of the files are orderbook and exactly half are messages
assert( len(csv_message) == len(csv_orderbook))
assert( len(csv_file_list) == len(csv_message) + len(csv_orderbook))

print('started loop')

orderbook_with_problems = []
messages_with_problems  = []
opening_closing_times = []
empty_time_intervals = []

for orderbook_name in csv_orderbook:

    print(orderbook_name)

    #read the orderbook. keep a record of problematic files
    try:
        df_orderbook = pd.read_csv(orderbook_name, header= None)
    except:
        orderbook_with_problems.append(orderbook_name)
        print('the following file has been skipped:  ' + orderbook_name)
        continue

    df_orderbook.columns = ("ASKp1" , "ASKs1" , "BIDp1",  "BIDs1")

    #get date first
    match = re.findall('\d{4}-\d{2}-\d{2}', orderbook_name)[-1]
    date = datetime.datetime.strptime(match, '%Y-%m-%d')

    #read times from message file. keep a record of problematic files
    message_name = orderbook_name.replace('orderbook','message')
    try:
        df_message  =  pd.read_csv(message_name, usecols = [0], header = None)
    except:
        messages_with_problems.append(message_name)
        print('the following file has been skipped:  ' + message_name)
        continue

    market_open = int(df_message.iloc[0]/60)/60 #open at minute before first transaction
    market_close = (int(df_message.iloc[-1]/60)+1)/60 #close at minute after last transaction

    #check that the two df have the same length
    assert (len(df_message) == len(df_orderbook))

    #convert df_message.index to seconds since midnight and add this to the current date
    seconds_since_midnight = pd.to_timedelta(df_message[0], unit = 'S', errors="coerce")
    timeindex_ = seconds_since_midnight.values + pd.Series(date).repeat(repeats = len(seconds_since_midnight))

    # find the index of the last order in every n-minute batch (resampling) and
    # select the appropriate Bid-Ask prices and sizes from the orderbook
    df_indicestokeep = pd.DataFrame(range(len(df_message[0])), timeindex_)
    df_indices = df_indicestokeep.resample(str(nmin)+'min', closed='right').max()
    # keep track of the number of empty time intervals (on days that have at least one)
    if int(df_indices.isna().sum()) > 0:
        empty_time_intervals.append(str(date) + ' : ' + str(int(df_indices.isna().sum())))
    #add opening prices and fill in empty time intervals with preceding price
    indices = [0] + list(df_indices.ffill()[0])
    # reduce orderbook to relevant equidistant data (convert to numeric)
    df_orderbook_nmin = df_orderbook.iloc[indices]
    df_orderbook_nmin = df_orderbook_nmin.apply(pd.to_numeric)

    # compute the n-min equidistant microprices
    df_microprices =  (df_orderbook_nmin['ASKp1'] * df_orderbook_nmin['ASKs1'] + df_orderbook_nmin['BIDp1'] * df_orderbook_nmin['BIDs1']) / (df_orderbook_nmin['ASKs1'] + df_orderbook_nmin['BIDs1'])

    # label microprices with the appropriate time index and drop values outside of market hours
    market_open_minutes = range(int(market_open*60), int(market_close*60) + nmin, nmin)
    df_microprices.index = pd.to_timedelta(market_open_minutes, unit='m') + pd.Series(date).repeat(repeats = len(market_open_minutes))
    df_microprices = df_microprices.between_time(datetime.time(9, 30), datetime.time(16))

    #keep track of market opening and closing time if its not 9:30 - 16:00
    if not(market_open == 9.5 and market_close == 16):
        opening_closing_times.append(str(df_microprices.index[0]) + ' - ' + str(df_microprices.index[-1]))

    #compute n-min log returns
    log_returns_nmin = (np.log(df_microprices)).diff(1).drop(df_microprices.index[0])
    log_returns_nmin.sort_index(inplace=True)

    #save
    output_name = os.path.join(output_path, str(nmin)+'min_' + str(date.date())+'.csv')
    log_returns_nmin.to_csv(output_name, header = False)

print('finished loop')

supplementary_path = os.path.join(os.path.pardir,TICKER +'_supplementary_files')
skipped_files_path = os.path.join(supplementary_path, str(nmin)+'min_skipped')
empty_files_path = os.path.join(supplementary_path, str(nmin)+'min_empty_time_intervals')
open_close_files_path = os.path.join(supplementary_path, str(nmin)+'min_opening_closing_times')

with open(skipped_files_path +'_orderbook.txt', 'wb') as fp:
      pickle.dump(orderbook_with_problems, fp)

with open(skipped_files_path + '_messages.txt', 'wb') as fp:
      pickle.dump(messages_with_problems, fp)

with open(empty_files_path + '.txt' , 'wb') as fp:
      pickle.dump(empty_time_intervals, fp)

with open(open_close_files_path + '.txt' , 'wb') as fp:
      pickle.dump(opening_closing_times, fp)

print('please check supplementary files before performing analysis')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def addInitialData(apps, schema_editor):
	AreaNews = apps.get_model('news', 'AreaNews')
	AreaNews.objects.bulk_create([
		AreaNews(
			title='Noticias',
			content= '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacus massa, scelerisque eget imperdiet at,
					accumsan tincidunt metus. Donec interdum sit amet risus vel dignissim. Donec ultrices cursus tortor, quis eleifend
					quam cursus in. Suspendisse dignissim tincidunt dui sed consequat. Aenean ultrices luctus laoreet. Nam egestas at
					leo sit amet malesuada.''',
			),
		])

class Migration(migrations.Migration):

    dependencies = [
        ('news', '0001_initial'),
    ]

    operations = [
    	migrations.RunPython(addInitialData)
    ]


"""
Display line segments. Can be useful e.g. for visializing vector fields.
"""

import numpy as np

import pygfx as gfx

from PySide6 import QtWidgets
from wgpu.gui.qt import WgpuCanvas

app = QtWidgets.QApplication([])

canvas = WgpuCanvas()
renderer = gfx.WgpuRenderer(canvas)

scene = gfx.Scene()

x = np.linspace(20, 620, 200, dtype=np.float32)
y = np.sin(x / 10) * 100 + 200

positions = np.column_stack([x, y, np.zeros_like(x)])
colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)
geometry = gfx.Geometry(positions=positions, colors=colors)

# Also see LineSegmentMaterial and LineThinSegmentMaterial
material = gfx.LineSegmentMaterial(
    thickness=6.0, color=(0.0, 0.7, 0.3, 0.5), vertex_colors=True
)
line = gfx.Line(geometry, material)
scene.add(line)

camera = gfx.ScreenCoordsCamera()

if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    app.exec()


# -*- coding: utf-8 -*-
###################################################################################
# Added to enable code completion in IDE's.
if 0:
    import gluon
    global auth; auth = gluon.tools.Auth()
    from gluon import T,request,response,URL,H2
    from applications.baadal.models import *  # @UnusedWildImport
###################################################################################
from helper import is_moderator, is_faculty, is_orgadmin

response.title = request.application
response.google_analytics_id = None

response.top_menu = [
    (T('About'), False, URL('default','index')),
    (T('Blog'), False, URL('default','page_under_construction')),
    (T('Photos'), False, URL('default','page_under_construction')),
    (T('Team Baadal'), False, URL('default','page_under_construction')),
    (T('Contact'), False, URL('default','page_under_construction'))
    ]
if auth.is_logged_in():
    response.user_menu = [
        (H2('USER MENU'),False, dict(_href='#', _id='menu_user')),
        (T('Home'), False, URL('default','index')),
        (T('Request VM'), False, URL('user','request_vm')),
        (T('My VMs'), False, URL('user','list_my_vm')),
        (T('My Tasks'), False, URL('user','list_my_task')),
        (T('Mail Admin'), False, URL('default','page_under_construction')),
        (T('Report Bug'), False, URL('default','page_under_construction'))
        ]

    if (is_moderator() | is_faculty()):
        response.faculty_menu = [
            (H2('FACULTY MENU'),False, dict(_href='#', _id='menu_faculty')),
            (T('Pending Requests {'+str(len(get_pending_requests()))+'} '), False, URL('faculty','pending_requests'))
            ]

        if (is_moderator() | is_orgadmin()):
            response.orgadmin_menu = [
                (H2('ORG-ADMIN MENU'),False, dict(_href='#', _id='menu_orgadmin')),
                (T('List All Org-Level VMs'), False, URL('orgadmin','list_all_orglevel_vm')),
                (T('Pending Org-Level VM Approvals {'+str(get_pending_approval_count())+'}'), False, URL('orgadmin','pending_approvals'))
                ]

            if is_moderator():
                response.admin_menu = [
                    (H2('ADMIN MENU'),False, dict(_href='#', _id='menu_admin')),
                    (T('All VM''s'), False, URL('admin','list_all_vm')),
                    (T('Host and VMs'), False, URL('admin','hosts_vms')),
                    (T('Tasks'), False, URL('admin','task_list')),
                    (T('Sanity Check'), False, URL('admin','sanity_check')),
                    (T('Configure System'), False,dict(_href='#', _id='configure'),[
                        (T('Configure Host'), False, URL('admin','host_details')),
                        (T('Configure Template'), False, URL('admin','add_template')),
                        (T('Configure Datastore'), False, URL('admin','add_datastore'))
                        ])
                    ]


import torch
import torch.nn as nn
import torch.nn.functional as F

class AlexNet(nn.Module):
    def __init__(self):
        super(AlexNet, self).__init__()
        # convolution part
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=96, kernel_size=11, stride=4, padding=0)
        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2)
        self.conv2 = nn.Conv2d(in_channels =96, out_channels=256, kernel_size=5, stride=1, padding=2)
        self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2)
        self.conv3 = nn.Conv2d(in_channels =256, out_channels=384, kernel_size=3, stride=1, padding=1)
        self.conv4 = nn.Conv2d(in_channels =384, out_channels=384, kernel_size=3, stride=1, padding=1)
        self.conv5 = nn.Conv2d(in_channels =384, out_channels=256, kernel_size=3, stride=1, padding=1)
        self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2)
        # dense part
        self.fc1 = nn.Linear(in_features=9216, out_features = 4096)
        self.dropout1 = nn.Dropout(0.5)
        self.fc2 = nn.Linear(in_features=4096, out_features = 4096)
        self.dropout2 = nn.Dropout(0.5)
        self.fc3 = nn.Linear(in_features=4096, out_features=1000)

    def forward(self, image):
        # get the batch size, channels, height and width
        # original size (bs, 3, 227, 227)
        bs, c, h, w = image.size()
        x = F.relu(self.conv1(image)) # size (bs, 96, 55, 55) ((w-f+2p)/s) + 1)
        x = self.pool1(x) # (bs, 96, 27, 27) ((w-f+2p)/s) + 1)
        x = F.relu(self.conv2(x)) # size (bs, 256, 27, 27
        x= self.pool2(x) # size (bs, 256, 13, 13)
        x = F.relu(self.conv3(image)) # size (bs, 384, 13, 13)
        x = F.relu(self.conv4(image)) # size (bs, 384, 13, 13)
        x = F.relu(self.conv5(image)) # size (bs, 256, 13, 13)
        x = self.pool3(x) # size (bs, 256, 6, 6)
        x = x.view(bs, -1) # size (bs, 9216)
        x = F.relu(self.fc1(x)) # size (bs, 4096)
        x = self.dropout1(x) # size (bs, 4096)
        x = self.fc2(x) # size (bs, 4096)
        x = self.dropout2(x) # size (bs, 4096)
        x = F.relu(self.fc3(x)) # size (bs, 1000)
        # 1000 is number of classes in ImageNet dataset
        # softmax is an activation function that converts linear output to praobabilities that add upto 1
        # for each sample in the batch
        x = torch.softmax(x, axis=1) # size (bs, 1000)
        return x


# import the necessary packages
from imutils.video import VideoStream
import datetime
import numpy, imutils, time, pygame, cv2, math, sys, os
from pygame.locals import *

def orig():
    # initialize the video stream and allow the cammera sensor to warmup
    vs = VideoStream(usePivs=False).start()
    time.sleep(2.0)

    # loop over the frames from the video stream
    while True:
            # grab the frame from the threaded video stream and resize it
            # to have a maximum width of 400 pixels
            frame = vs.read()
            frame = imutils.resize(frame, width=720)
            # draw the timestamp on the frame
            timestamp = datetime.datetime.now()
            ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
            cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    0.35, (0, 0, 255), 1)

            # show the frame
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF

            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                    break

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
 #----------------------------------------------------------

def init():
    global color, vs, camera_index, screen, screen_width, xres, yres
    global mWIDTH, mHEIGHT, monSize, resolution, label1
    pygame.init()
    variables()

    color=False
    # initialize the video stream and allow the camera sensor to warmup
    vs = VideoStream(usePiCamera=False).start() # usePiCamera = False for webcam
    time.sleep(2.0)
    resolution = 720
    label1 = "test"

    # set defaults
    getMonSize() # grab the resolution of the monitor

    screen = pygame.display.set_mode((mWIDTH,mHEIGHT), pygame.RESIZABLE)
    print("xres: %s" % xres)
    print("yres: %s" % yres)
    print("monSize: %s" % monSize)
    print('')
    print('')

    # set title of screen
    pygame.display.set_caption("Python Camera Scope")

def updateScreenSize():
    global screen, xres, yres
    screen_width, screen_height = xres, yres
    screen=pygame.display.set_mode((screen_width,screen_height), pygame.RESIZABLE)

# Define variables
def variables():
    global red, black, white, blue
    global yellow, magenta, cyan, green, width, height
    global count, myfont, myfont1, d, trk, myfont2, myfont3, myfont4, crossHairDes
    crossHairDes = 1
    #xres, yres = 1920, 1080 # monitor resolution
    password = "no"
    myfont = pygame.font.SysFont("Times New Roman", 35)
    myfont1 = pygame.font.SysFont("Times New Roman", 25)
    myfont2 = pygame.font.SysFont("Times New Roman", 20) # previewbox
    myfont3 = pygame.font.SysFont("Times New Roman", 18) # 720p measuremode
    myfont4 = pygame.font.SysFont("Times New Roman", 10) # 480p and 576p measuremode

    trk = 1
    red = (255,0,0)
    black = (0,0,0)
    white = (255,255,255)
    blue = (0,0,255)
    yellow = (255,255,0)
    magenta = (255,0,255)
    black = (0,0,0)
    cyan = (0,255,255)
    green = (0,255,0)

    width = 0
    height = 0
    channels= 0
    count = 0

def getMonSize():
    global mWIDTH, mHEIGHT, monSize, xres, yres
    # grab size of monitor
    infoObject = pygame.display.Info()
    mWIDTH = infoObject.current_w
    mHEIGHT = infoObject.current_h
    #mWIDTH = 720 # test different monitor resolutions (size)
    #mHEIGHT = 480
    print("Monitor width: %s" % mWIDTH)
    print("Monitor height: %s" % mHEIGHT)
    print('')
    print('')

    # check if monitor is proper resolution
    if(mWIDTH == 1920 and mHEIGHT == 1080): monSize = 1080
    elif(mWIDTH == 1280 and mHEIGHT == 720): monSize = 720
    elif(mWIDTH == 720 and mHEIGHT == 480): monSize = 480
    elif(mWIDTH == 720 and mHEIGHT == 576): monSize = 576
    elif(mWIDTH == 800 and mHEIGHT == 440): monSize = 800
    elif(mWIDTH == 3840 and mHEIGHT == 2160): monSize = 3840
    else:
        monSize = mHEIGHT # so program will have something to work with
    xres = mWIDTH
    yres = mHEIGHT
    updateScreenSize()

# shows rectangle and label with directions
def preview_box(val):
    global screen
    # create second surface for semi-transparent rectangle
    s = pygame.Surface(((xres/8)*7,90))  # the size of your rect
    s.set_alpha(128)                # alpha level
    s.fill(white)           # this fills the entire surface
    screen.blit(s, ((xres/16)*1,(yres/16)*14))    # (0,0) are the top-left coordinates

    # .rect(screen,color,(x,y,width,height),thickness)
    if(val==1):
        label = myfont2.render("Live Mode. Press '+' to save image.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*14.35))
    elif(val==2):
        stext=myfont2.render(imgText, 1,magenta)
        screen.blit(stext, (10, 10))
        label = myfont2.render("Live Mode.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*14.1))
        label = myfont2.render("Press number keys to view img or '+' to save image.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*14.6))
        label = myfont2.render("Press '-' to delete saved images.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*15.1))
    elif(val==3):
        s.fill(black)
        s.set_alpha(255)                # alpha level
        screen.blit(s, ((xres/16)*1,(yres/16)*14))    # (0,0) are the top-left coordinates
        label = myfont2.render("Please move any images you wish to keep out of the 'IMAGES' folder,", 1, white)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*7.5))
        label = myfont2.render("or else you will lose them.", 1, white)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*8))
        label = myfont2.render("Press 'ENTER' to continue.", 1, white)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*8.5))
    elif(val==4):
        label = myfont2.render("Image Viewer. Press '-' for Live Mode.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*14.35))
    else:
        label = myfont2.render("Oops, this is embarassing. Something went wrong.", 1, black)
        screen.blit(label, ((xres/16)*2.8, (yres/16)*14.35))

# shows rectangle and label with directions
def preview_box_img(val):
    global screen
    stext=myfont2.render(str(val), 1,magenta)
    screen.blit(stext, (10, 10))

# shows message box
def msgBox():
    global screen, count
    #clear old label by blacking out screen
    screen.fill(black)
    pygame.display.update()
    preview_box(3)
    pygame.display.update()
    x = True
    while x==True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                cleanQuit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    cleanQuit()
                if event.key == pygame.K_KP_ENTER:
                    remOld()
                    x=False
                if event.key == pygame.K_RETURN:
                    remOld()
                    x=False
    count = 1

def getCamFrame(color,vs):
    global height, width, channels, img, saveFrame
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=720)
    # adjust color
    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    #rotate image
    frame=numpy.rot90(frame)

    # save frame to use in measuremode and calibration
    saveFrame = frame
    # make into pygame surface
    frame=pygame.surfarray.make_surface(frame)
    frame = pygame.transform.flip(frame,True,False)
    return frame

def blitCamFrame(frame,screen):
    global width, height
    width = frame.get_width()
    height = frame.get_height()
    screen.blit(frame,((mWIDTH/2)-(width/2),(mHEIGHT/2)-(height/2)))
    return screen

# shows crosshair
def crossHair():
    global screen
    radius = 100
    width = 3
   # # create second surface for semi-transparent rectangle
   # s = pygame.Surface(((xres/8)*7,90))  # the size of your rect
   # s.set_alpha(128)                # alpha level
   # s.fill(white)           # this fills the entire surface
   # screen.blit(s, ((xres/16)*1,(yres/16)*14))    # (0,0) are the top-left coordinates

    # .rect(screen,color,(x,y,width,height),thickness)
    if(crossHairDes==1):
        pygame.draw.circle(screen, green, ((xres/2),(yres/2)),radius,width)
        pygame.draw.circle(screen, green, ((xres/2),(yres/2)),radius/2,width)
        pygame.draw.line(screen, green, ((xres/2)-radius,(yres/2)),((xres/2)+radius,(yres/2)),width)
        pygame.draw.line(screen, green, ((xres/2),(yres/2)-radius),((xres/2),(yres/2)+radius),width)
    elif(crossHairDes==2):
        pygame.draw.circle(screen, magenta, ((xres/2),(yres/2)),radius,width)
        pygame.draw.circle(screen, magenta, ((xres/2),(yres/2)),radius/2,width)
        pygame.draw.line(screen, magenta, ((xres/2)-radius,(yres/2)),((xres/2)+radius,(yres/2)),width)
        pygame.draw.line(screen, magenta, ((xres/2),(yres/2)-radius),((xres/2),(yres/2)+radius),width)
    elif(crossHairDes==3):
        pygame.draw.circle(screen, cyan, ((xres/2),(yres/2)),radius,width)
        pygame.draw.circle(screen, cyan, ((xres/2),(yres/2)),radius/2,width)
        pygame.draw.line(screen, cyan, ((xres/2)-radius,(yres/2)),((xres/2)+radius,(yres/2)),width)
        pygame.draw.line(screen, cyan, ((xres/2),(yres/2)-radius),((xres/2),(yres/2)+radius),width)

# shows message box
def msgBox():
    global screen, count
    #clear old label by blacking out screen
    screen.fill(black)
    pygame.display.update()
    preview_box(3)
    pygame.display.update()
    x = True
    while x==True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                cleanQuit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    cleanQuit()
                if event.key == pygame.K_KP_ENTER:
                    remOld()
                    x=False
                if event.key == pygame.K_RETURN:
                    remOld()
                    x=False
    count = 1

def adjustRes():
    global screen, resolution, xres, yres, myfont, myfont1, label1
    if(resolution==480): #480p -> 576p
        xres = 720
        yres = 576
        resolution = 576
        label1 = "576p"
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 10) # 'inches' label
        myfont1 = pygame.font.SysFont("Times New Roman", 10) # other stuff label
    elif(resolution==576): #576p -> 800x440
        xres = 800
        yres = 440
        resolution = 800
        label1 = "800x440"
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 12)
        myfont1 = pygame.font.SysFont("Times New Roman", 16)
    elif(resolution==800): #800x440 -> 720p
        xres = 1280
        yres = 720
        resolution = 720
        label1 = "720p"
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 35)
        myfont1 = pygame.font.SysFont("Times New Roman", 25)
    elif(resolution==720): #720p -> current screen resolution (camera may run higher than 720p)
        xres = mWIDTH
        yres = mHEIGHT
        resolution = 999
        label1 = str(mWIDTH) + "x" + str(mHEIGHT)
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 35)
        myfont1 = pygame.font.SysFont("Times New Roman", 25)
    elif(resolution==999):
        xres = 720
        yres = 480
        resolution = 480
        label1 = "480p"
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 10)
        myfont1 = pygame.font.SysFont("Times New Roman", 10)
    else:
        xres = 720
        yres = 480
        resolution = 480
        label1 = "480p"
        #adjust font for new resolution
        myfont = pygame.font.SysFont("Times New Roman", 10)
        myfont1 = pygame.font.SysFont("Times New Roman", 10)

    screen.fill(black)
    pygame.display.update()
    try:
        updateScreenSize()
    except:
        print("Resolution not available...")
    val = "Resolution is now: " + label1
    label = myfont2.render(val, 1, red)
    screen.blit(label, ((xres/16)*4, (yres/16)*7))
    val = "If the resolution has changed"
    label = myfont2.render(val, 1, yellow)
    screen.blit(label, ((xres/16)*4, (yres/16)*8))
    val = "recalibrate for this new resolution."
    label = myfont2.render(val, 1, yellow)
    screen.blit(label, ((xres/16)*4, (yres/16)*9))
    pygame.display.update()
    print("Resolution is now: %s" % label1)
    time.sleep(1)
    screen.fill(black)
    pygame.display.update()

def cleanQuit():
    global screen
    pygame.quit()
    cv2.destroyAllWindows()
    sys.exit()

def run():
    global screen, resolution, label1, password, calibfeature, xres, yres, myfont, myfont1, crossHairDes, vs
    running = True
    adjustRes()
    while running:
        frame=getCamFrame(color,vs)
        screen=blitCamFrame(frame,screen)
        crossHair()
        pygame.display.update()
        for event in pygame.event.get(): #process events since last loop cycle
            if event.type == pygame.QUIT:
                cleanQuit()
            if event.type == KEYDOWN:
                if event.key == pygame.K_SLASH or event.key == pygame.K_KP_DIVIDE: # '/'
                    print("'Divide' pressed...")
                    crossHairDes+=1
                    if(crossHairDes>3):crossHairDes=1
                elif event.key == pygame.K_ESCAPE:
                    running = False
                    print("'Escape' pressed...quitting now...")
                    cleanQuit()

init()
run()
cleanQuit()


from django.shortcuts import render, HttpResponse, redirect
from .models import *
from django.contrib import messages

def index(request):
    return render(request, 'index.html')

def register(request):
    if request.method == 'POST':
        post_data = request.POST
        response = User.objects.register(post_data)
        if response['valid']:
            request.session['uid'] = response['user'].id
            return redirect('/success')
        else:
            for error in response['errors']:
                messages.add_message(request, messages.ERROR, error)
            return redirect('/')

def login(request):
    if request.method == 'POST':
        post_data = request.POST
        response = User.objects.login(post_data)
        if response['valid']:
            request.session['uid'] = response['user'].id
            return redirect('/success')
        else:
            for error in response['errors']:
                messages.add_message(request, messages.ERROR, error)
            return redirect('/')

def success(request):
    if 'uid' in request.session:
        user = User.objects.get(id=request.session['uid'])
        return render(request, 'success.html', {'user': user})
    else:
        return redirect('/')

def logout(request):
    request.session.clear()
    return redirect('/')


"""
    api_main
"""

import random
import argparse
import time

from utils import Pid, get_logger
from hardware import Dynamo
from network import ClientHandler

random.seed()

def random_range(floor: float, ceil: float, point: int) -> float:
    """
        Create a 'random' value between the given values
        args:
            floor: The minimum value
            ceil: The maximum value
            point: The number of decimals after the floating point
    """
    return round(random.uniform(floor, ceil), point)

def main():
    """
        main
    """
    my_pid = Pid("cf_api")
    assert not my_pid.is_running()
    my_pid.set_pidfile()
    logger = get_logger(__name__)
    parser = argparse.ArgumentParser(description="API communication experiment")
    parser.add_argument('--range', '-r',
                        help="Production range (in Watt)",
                        nargs=2,
                        metavar=('min', 'max'),
                        type=float,
                        action='store',
                        default=(1, 5))
    parser.add_argument('--point', '-p',
                        help="Number of digits after the floating point",
                        type=int,
                        action='store',
                        default=2)
    args = parser.parse_args()
    client = ClientHandler()
    modules = [
        Dynamo(address, uuid) for address, uuid in
        ((0x2, "001:001:001"), (0x3, "001:001:002"))
        ]
    logger.info("Program started")
    #id_dict = client.get_module_id(tuple(dynamo.uuid for dynamo in modules))
    #for dynamo in modules:
    #    if dynamo.uuid in id_dict:
    #        dynamo.session_id = id_dict[dynamo.uuid]
    #        print("uuid: {}, session_id {}".format(dynamo.uuid,
    #                                               dynamo.session_id))
    #    else:
    #        print("missing session_id for module {}".format(dynamo.uuid))
    id_list = client.get_module_id(tuple(dynamo.uuid for dynamo in modules))
    if not id_list:
        logger.critical("Empty module ids. Execution stop.")
        return 1
    for it, dynamo in enumerate(modules):
        dynamo.session_id = id_list[it]
    logger.info("Initialisation done. Send production NOW.")
    execution = True
    prod_d = dict()
    while execution:
        prod_d.clear()
        for dynamo in modules:
            dynamo.add_prod(
                random_range(args.range[0], args.range[1], args.point))
            prod_d[dynamo.uuid] = dynamo.prod_sum()
        logger.debug(prod_d)
        ret = client.module_send_production(prod_d)
        time.sleep(1)
        if ret is None:
            execution = False
        else:
            for command in ret:
                if command["cmd"] == "setModuleId":
                    for param in command["params"]:
                        for dynamo in modules:
                            if dynamo.uuid == param['UUID']:
                                dynamo.session_id = param["moduleID"]

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Banner',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(null=True, blank=True, max_length=50, help_text='Banner名称，通常作为标识')),
                ('description', models.CharField(null=True, blank=True, max_length=255, help_text='Banner描述')),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
            ],
            options={
                'verbose_name_plural': 'Banner',
                'db_table': 'banner',
                'verbose_name': 'Banner',
            },
        ),
        migrations.CreateModel(
            name='BannerItem',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('key_word', models.CharField(help_text='执行关键字，根据不同的type含义不同', max_length=100)),
                ('type', models.IntegerField(choices=[(0, '无导向'), (1, '导向商品'), (2, '导向专题')], help_text='0:无导向；1：导向商品;2:导向专题', default=1)),
                ('delete_time', models.IntegerField(null=True, blank=True)),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('banner', models.ForeignKey(related_name='items', db_constraint=False, to='api.Banner', help_text='外键，关联banner表')),
            ],
            options={
                'verbose_name_plural': 'Banner子项',
                'db_table': 'banner_item',
                'verbose_name': 'Banner子项',
            },
        ),
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(help_text='分类名称', max_length=50)),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('description', models.CharField(null=True, blank=True, max_length=100, help_text='描述')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
            ],
            options={
                'verbose_name_plural': '商品分类',
                'db_table': 'category',
                'verbose_name': '商品分类',
            },
        ),
        migrations.CreateModel(
            name='Image',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('url', models.CharField(help_text='图片路径', max_length=255)),
                ('img_from', models.IntegerField(choices=[(1, '本地'), (2, '公网')], help_text='图片来自 1 本地 ，2公网', default=1, db_column='from')),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
            ],
            options={
                'verbose_name_plural': '图片',
                'db_table': 'image',
                'verbose_name': '图片',
            },
        ),
        migrations.CreateModel(
            name='Order',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('order_no', models.CharField(help_text='订单号', unique=True, max_length=20)),
                ('user_id', models.IntegerField(help_text='外键，用户id，注意并不是openid')),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('create_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('total_price', models.DecimalField(help_text='总价', max_digits=6, decimal_places=2)),
                ('status', models.IntegerField(choices=[(1, '未支付'), (2, '已支付'), (3, '已发货'), (4, '已支付但库存不足')], help_text='1:未支付， 2：已支付，3：已发货 , 4: 已支付，但库存不足', default=1)),
                ('snap_img', models.CharField(null=True, blank=True, max_length=255, help_text='订单快照图片')),
                ('snap_name', models.CharField(null=True, blank=True, max_length=80, help_text='订单快照名称')),
                ('total_count', models.IntegerField(help_text='总数', default=0)),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('snap_items', models.TextField(null=True, blank=True, help_text='订单其他信息快照（json)')),
                ('snap_address', models.CharField(null=True, blank=True, max_length=500, help_text='地址快照')),
                ('prepay_id', models.CharField(null=True, blank=True, max_length=100, help_text='订单微信支付的预订单id（用于发送模板消息')),
            ],
            options={
                'verbose_name_plural': '订单',
                'db_table': 'order',
                'verbose_name': '订单',
            },
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(help_text='商品名称', max_length=80)),
                ('price', models.DecimalField(help_text='价格,单位：分', max_digits=6, decimal_places=2)),
                ('stock', models.IntegerField(help_text='库存量', default=0)),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('main_img_url', models.CharField(null=True, blank=True, max_length=255, help_text='主图ID号，这是一个反范式设计，有一定的冗余')),
                ('img_from', models.IntegerField(choices=[(1, '本地'), (2, '公网')], help_text='图片来自 1 本地 ，2公网', db_column='from')),
                ('create_time', models.IntegerField(null=True, blank=True, help_text='创建时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('summary', models.CharField(null=True, blank=True, max_length=50, help_text='摘要')),
            ],
            options={
                'verbose_name_plural': '商品',
                'db_table': 'product',
                'verbose_name': '商品',
            },
        ),
        migrations.CreateModel(
            name='ProductImage',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('order', models.IntegerField(help_text='图片排序序号')),
                ('img', models.OneToOneField(db_constraint=False, to='api.Image', help_text='图片外键')),
            ],
            options={
                'verbose_name_plural': '商品图片',
                'db_table': 'product_image',
                'verbose_name': '商品图片',
            },
        ),
        migrations.CreateModel(
            name='ProductProperty',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(null=True, blank=True, max_length=30, help_text='详情属性名称')),
                ('detail', models.CharField(help_text='详情属性', max_length=255)),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
            ],
            options={
                'verbose_name_plural': '商品属性',
                'db_table': 'product_property',
                'verbose_name': '商品属性',
            },
        ),
        migrations.CreateModel(
            name='Theme',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=50)),
                ('description', models.CharField(null=True, blank=True, max_length=255)),
                ('delete_time', models.IntegerField(null=True, blank=True)),
                ('update_time', models.IntegerField(null=True, blank=True)),
                ('head_img', models.OneToOneField(related_name='head', help_text='图片外键', to='api.Image', null=True, blank=True, db_constraint=False)),
            ],
            options={
                'db_table': 'theme',
            },
        ),
        migrations.CreateModel(
            name='ThirdApp',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('app_id', models.CharField(max_length=64)),
                ('app_secret', models.CharField(max_length=64)),
                ('app_description', models.CharField(null=True, blank=True, max_length=100)),
                ('scope', models.CharField(max_length=20)),
                ('scope_description', models.CharField(null=True, blank=True, max_length=100)),
                ('delete_time', models.IntegerField(null=True, blank=True)),
                ('update_time', models.IntegerField(null=True, blank=True)),
            ],
            options={
                'db_table': 'third_app',
            },
        ),
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('openid', models.CharField(help_text='wechat openid', unique=True, max_length=50)),
                ('nickname', models.CharField(null=True, blank=True, max_length=50, help_text='昵称')),
                ('extend', models.CharField(null=True, blank=True, max_length=255)),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('create_time', models.IntegerField(null=True, blank=True, help_text='创建时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
            ],
            options={
                'db_table': 'user',
            },
        ),
        migrations.CreateModel(
            name='UserAddress',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
                ('name', models.CharField(help_text='姓名', max_length=30)),
                ('mobile', models.CharField(help_text='手机号', max_length=20)),
                ('province', models.CharField(null=True, blank=True, max_length=20, help_text='省')),
                ('city', models.CharField(null=True, blank=True, max_length=20, help_text='城市')),
                ('country', models.CharField(null=True, blank=True, max_length=20, help_text='国家')),
                ('detail', models.CharField(null=True, blank=True, max_length=100, help_text='详情')),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('user', models.OneToOneField(related_name='address', help_text='用户', to='api.User', null=True, blank=True, db_constraint=False)),
            ],
            options={
                'db_table': 'user_address',
            },
        ),
        migrations.CreateModel(
            name='OrderProduct',
            fields=[
                ('product', models.OneToOneField(serialize=False, to='api.Product', help_text='联合主键，商品id', primary_key=True, db_constraint=False)),
                ('count', models.IntegerField(help_text='商品数量')),
                ('delete_time', models.IntegerField(null=True, blank=True, help_text='删除时间')),
                ('update_time', models.IntegerField(null=True, blank=True, help_text='更新时间')),
                ('order', models.ForeignKey(to='api.Order', help_text='联合主键，订单id', db_constraint=False)),
            ],
            options={
                'verbose_name_plural': '订单商品',
                'db_table': 'order_product',
                'verbose_name': '订单商品',
            },
        ),
        migrations.AddField(
            model_name='theme',
            name='product',
            field=models.ManyToManyField(to='api.Product', help_text='商品，多对多关系', related_name='products', db_constraint=False),
        ),
        migrations.AddField(
            model_name='theme',
            name='topic_img',
            field=models.OneToOneField(related_name='topic', help_text='图片外键', to='api.Image', null=True, blank=True, db_constraint=False),
        ),
        migrations.AddField(
            model_name='productproperty',
            name='product',
            field=models.ForeignKey(related_name='properties', db_constraint=False, to='api.Product', help_text='商品外键'),
        ),
        migrations.AddField(
            model_name='productimage',
            name='product',
            field=models.ForeignKey(related_name='images', db_constraint=False, to='api.Product', help_text='商品外键'),
        ),
        migrations.AddField(
            model_name='product',
            name='category',
            field=models.ForeignKey(help_text='商品分类外键', to='api.Category', null=True, blank=True, db_constraint=False),
        ),
        migrations.AddField(
            model_name='product',
            name='img',
            field=models.OneToOneField(help_text='图片外键', to='api.Image', null=True, blank=True, db_constraint=False),
        ),
        migrations.AddField(
            model_name='category',
            name='topic_img',
            field=models.OneToOneField(help_text='外键，关联image表', to='api.Image', null=True, blank=True, db_constraint=False),
        ),
        migrations.AddField(
            model_name='banneritem',
            name='img',
            field=models.OneToOneField(db_constraint=False, to='api.Image', help_text='外键，关联image表'),
        ),
        migrations.AlterUniqueTogether(
            name='orderproduct',
            unique_together=set([('product', 'order')]),
        ),
    ]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
"""
description
"""

import numpy as np
import Analysis.fitting as fit
from scipy.signal import hanning
from scipy import convolve
from datetime import timedelta

__author__ = "Fanger1.Yunis"
__copyright__ = "Copyright 2012, Fanger1.Yunis"
__credits__ = ["Fanger1.Yunis"]
__license__ = "GPL"
__version__ = "$Rev$"
__maintainer__ = "Fanger1.Yunis"
__email__ = "mail@ralfkelzenberg.de"
__status__ = "Alpha"

def treppenfunktion(x, y, xval, factor, reverse=False):
    """
    all y-values which's x-value is or comes after xval will be multiplied by factor
    """
    if not isinstance(xval, list):
        xval = [xval]
    for val in xval:
        xlist = list(x)
        ylist = list(y)

        xy = zip(xlist, ylist)
        xy = sorted(xy)

        xlist = [t[0] for t in xy]
        ylist = [t[1] for t in xy]

        if val in xlist:
            pos = xlist.index(val)
        else:
            if val < min(xlist):
                pos = 0
            elif val > max(xlist):
                pos = len(xlist)+1

        x1 = xlist[:pos]
        x2 = xlist[pos:]

        y1 = ylist[:pos]
        y2 = ylist[pos:]

        func1 = lambda yt: yt * 1
        func2 = lambda yt: yt * factor

        if reverse:
            func1 = lambda yt: yt * factor
            func2 = lambda yt: yt * 1

        y1 = map(func1, y1)
        y2 = map(func2, y2)

        x = np.concatenate([x1, x2])
        y = np.concatenate([y1, y2])

    return list(x), list(y)

def treppenoffset(x, y, xval, offset, reverse=False):
    """
    all y-values which's x-value is or comes after xval will be added an offset
    """
    if not isinstance(xval, list):
        xval = [xval]
    for val in xval:
        xlist = list(x)
        ylist = list(y)

        xy = zip(xlist, ylist)
        xy = sorted(xy)

        xlist = [t[0] for t in xy]
        ylist = [t[1] for t in xy]

        if val in xlist:
            pos = xlist.index(val)
        else:
            if val < min(xlist):
                pos = 0
            elif val > max(xlist):
                pos = len(xlist)+1

        x1 = xlist[:pos]
        x2 = xlist[pos:]

        y1 = ylist[:pos]
        y2 = ylist[pos:]

        func1 = lambda yt: yt
        func2 = lambda yt: yt + offset

        if reverse:
            func1 = lambda yt: yt + offset
            func2 = lambda yt: yt

        y1 = map(func1, y1)
        y2 = map(func2, y2)

        x = np.concatenate([x1, x2])
        y = np.concatenate([y1, y2])

    return list(x), list(y)

def punktwerte(x, y, xval, factor):
    """
    The y-value(s) whos x-value is xval will be multiplied by factor
    """
    if not isinstance(xval, list):
        xval = [xval]

    if isinstance(x, np.ndarray):
        xlist = list(x)
    else:
        xlist = x

    if isinstance(y, np.ndarray):
        ylist = list(y)
    else:
        ylist = y

    xy = zip(xlist, ylist)
    xy = sorted(xy)

    xlist = [t[0] for t in xy]
    ylist = [t[1] for t in xy]

    pos = [xlist.index(val)for val in xval if val in xlist]

    enum = zip(range(len(y)), ylist)
    func = lambda i, yt: yt * factor if i in pos else yt
    y = map(func, [i[0] for i in enum], [v[1] for v in enum])

    return xlist, y

def offset(x, y, xval, offset):
    """
    The y-value(s) whos x-value is xval an offset will be added
    """
    if not isinstance(xval, list):
        xval = [xval]

    if isinstance(x, np.ndarray):
        xlist = list(x)
    else:
        xlist = x

    if isinstance(y, np.ndarray):
        ylist = list(y)
    else:
        ylist = y

    xy = zip(xlist, ylist)
    xy = sorted(xy)

    xlist = [t[0] for t in xy]
    ylist = [t[1] for t in xy]

    pos = [xlist.index(val)for val in xval if val in xlist]

    enum = zip(range(len(y)), ylist)
    func = lambda i, yt: yt + offset if i in pos else yt
    y = map(func, [i[0] for i in enum], [v[1] for v in enum])

    return xlist, y

def exponentiel(x, y, xstart, xend, startheight, steepness=1):
    """
    subtracts a declining exponenential curve that starts at x=xtart and ends if given at x=xend
    with an offset of startheight and the exponent being multiplied by steepness
    """

    xlist = list(x)
    ylist = list(y)

    xy = zip(xlist, ylist)
    xy = sorted(xy, key=lambda x: x[0])

    if xend == None:
        xend = xy[-1][0]

    xlist = [t[0] for t in xy]
    ylist = [t[1] for t in xy]

    offset = 0
    while xstart < xlist[0]:
        offset = +1
        xstart += timedelta(7)
    pos = (xlist.index(xstart), xlist.index(xend))

    func = lambda i, yt, xs: yt - (startheight * (np.e ** (-steepness * xs))) if pos[0] <= i <= pos[1]  else yt
    y = map(func, range(len(y)), ylist,
            [0 for _ in xlist[:pos[0]]] + range(offset, len(xlist[pos[0]:pos[1] + 1]) + offset) + [0 for _ in xlist[pos[1] + 1:]])
    return list(xlist), list(y)

def cut_off_by_stdiv(xlist, ylist, func, pars=None, maxa=0.5, stdmult=3, maxnum=None):
    xy = zip(xlist, ylist)
    return _cut_off_by_stdiv_rec(xy, func, pars, len(xlist) * maxa, stdmult, count=len(xlist), maxnum=maxnum)

def cut_off_by_largest_div(xlist, ylist, func, pars=None, maxnum=None):
    xy = zip(xlist, ylist)
    return _cut_off_by_largest_div_rec(xy, func, pars, count=len(xlist), maxnum=maxnum)

def _cut_off_by_largest_div_rec(xy, func, pars, count, maxnum):
    pval = fit.fit_1D_data(func, [x[0] for x in xy], [y[1] for y in xy], pars, None)[0]
    d = []
    for el in xy:
        d.append(el[1] - func(el[0], *pval))

    largest = (-1, None)
    for i, el in enumerate(d):
        if np.abs(el) > largest[0]:
            largest = (np.abs(el), i)
    xy.pop(largest[1])

    if maxnum > 0:
        return _cut_off_by_largest_div_rec(xy, func, pars, count, maxnum - 1)
    else:
        return [x[0] for x in xy], [y[1] for y in xy]

def _cut_off_by_stdiv_rec(xy, func, pars, a, stdmult, count, maxnum):
    pval = fit.fit_1D_data(func, [x[0] for x in xy], [y[1] for y in xy], pars, None)[0]
    d = []
    clean = True
    for el in xy:
        d.append(el[1] - func(el[0], *pval))
    std = np.std(d)
    avg = 0
    newxy = []
    for i, el in enumerate(d):
        if np.abs(el - avg) > stdmult * std:
            clean = False
        else:
            newxy.append(xy[i])

    if maxnum != None:
        if maxnum < count - len(newxy):
            return [x[0] for x in xy], [y[1] for y in xy], count - len(xy)

    xy = newxy

    if len(xy) < a:
        raise Exception('too many values removed')
        return False
    elif not clean:
        return _cut_off_by_stdiv_rec(xy, func, pars, a, stdmult, count, maxnum)
    else:
        return [x[0] for x in xy], [y[1] for y in xy], count - len(xy)

def normalize_lifeline(data, mode="simple", **kwargs):
    if mode == "smooth":
        winsize = kwargs["winsize"]
        winhalf = int((winsize - 1) / 2.)
        s = [data[0] for _ in range(winhalf)] + data + [data[-1] for _ in range(winhalf)]
        smoothdata = convolve(hanning(winsize) / hanning(winsize).sum(), s, mode='valid')
        for i, v in enumerate(data):
            data[i] = v - smoothdata[i]
    average = np.mean(np.ma.masked_array(data, np.isfinite(data) == False))
    stddev = np.std(np.ma.masked_array(data, np.isfinite(data) == False))
    for i, v in enumerate(data):
        data[i] = (v / average - 1) / stddev * average

if __name__ == '__main__':
    import Graphics.diagram_layout_maker as dm
    dialay = dm.Diagramm_Layout_Maker()

    x1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    y1 = np.array([90, 100, 44, 115, 110, 100, 90, 85, 90, 40, 110, 115, 110, 100, 90, 85, 90, 100, 110, 115])

    x2 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    y2 = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31.5, 33, 34.5, 36, 37.5, 39, 40.5, 42, 43.5])

    x3 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    y3 = np.array([10, 10, 10, 10, 10, 10 + (100 * np.e ** -0), 10 + (100 * np.e ** -1), 10 + (100 * np.e ** -2),
                   10 + (100 * np.e ** -3), 10 + (100 * np.e ** -4), 10 + (100 * np.e ** -5), 10 + (100 * np.e ** -6),
                   10 + (100 * np.e ** -7), 10 + (100 * np.e ** -8), 10 + (100 * np.e ** -9), 10, 10, 10, 10, 10])
    func = lambda x: (np.sin(x) + (x + 1)) * 1.6 if 16 >= x >= 8  else np.sin(x) + (x + 1)

    x4 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
    y4 = [func(y) for y in range(len(x4))]

    for n in range(10):
        y4[5 + n] += 30 * np.e ** (-0.2 * n)

    y4[3] *= 1 / 2.5
    y4[13] *= 1 / 2.5
    y4[17] *= 1 / 2.5

    y4 = np.array(y4)

    x1 = list(x1)
    x2 = list(x2)
    x3 = list(x3)
    x4 = list(x4)

    y1 = list(y1)
    y2 = list(y2)
    y3 = list(y3)
    y4 = list(y4)

    dialay.set_lineplotdefaults(color='k')
    dialay.add_layout(columns=2, rows=2)
    dialay.add_lineplot(x=x1, y=y1)
    dialay.add_lineplot(x=x2, y=y2)
    dialay.add_lineplot(x=x3, y=y3)
    dialay.add_lineplot(x=x4, y=y4)

    x2, y2 = treppenfunktion(x2, y2, 10, 1 / 1.5)

    x1, y1 = punktwerte(x1, y1, [2, 9], 2.5)

    x3, y3 = exponentiel(x3, y3, xstart=5, xend=14, startheight=100, steepness=1)

    x1 = list(x1)
    x2 = list(x2)
    x3 = list(x3)

    y1 = list(y1)
    y2 = list(y2)
    y3 = list(y3)

    dialay.add_layout(columns=2, rows=2)

    dialay.add_lineplot(x=x1, y=y1)
    dialay.add_lineplot(x=x2, y=y2)
    dialay.add_lineplot(x=x3, y=y3)
    dialay.add_lineplot(x=x4, y=y4)

    dialay.add_layout(columns=2, rows=2)

    x4, y4 = punktwerte(x4, y4, [3, 13, 17], 2.5)
    y4 = list(y4)
    x4 = list(x4)
    dialay.add_lineplot(x=x4, y=y4)

    x4, y4 = exponentiel(x4, y4, xstart=5, xend=None, startheight=30, steepness=0.2)
    y4 = list(y4)
    x4 = list(x4)
    dialay.add_lineplot(x=x4, y=y4)

    x4, y4 = treppenfunktion(x4, y4, 8, 1 / 1.6)
    y4 = list(y4)
    x4 = list(x4)
    dialay.add_lineplot(x=x4, y=y4)

    x4, y4 = treppenfunktion(x4, y4, 17, 1.6)
    y4 = list(y4)
    x4 = list(x4)
    dialay.add_lineplot(x=x4, y=y4)

    #
    dialay.show()
    """xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    ylist = [-5, -5, -6, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, ]
    func = lambda xt:0
    print cut_off_by_stdiv(xlist, ylist, func, 3)"""


import datetime
from picamera import PiCamera
from time import sleep
from sense_hat import SenseHat
import json

camera = PiCamera()
sense = SenseHat()
sense.clear()
img_counter = 0
img_name_counter = 1
interval_secs = 10 #camera
max_history = 72 #12 hours
max_dump = 1 #push to feed
data_dump = {}
sleep(2)

while True:
    data = {}
    temp = sense.get_temperature()
    humidity = sense.get_humidity()
    if img_counter == max_history:
        img_counter = 0
        img_name_counter = 1
    if img_counter % max_dump == 0:
        with open("/var/www/html/data/feed.json", "w") as write_file:
            json.dump(data_dump, write_file)
    camera.capture('/var/www/html/images/' + str(img_name_counter) + '.jpg')
    data['temp'] = temp
    data['img'] = '/images/' + str(img_name_counter) + '.jpg'
    data['hum'] = humidity
    data['time'] = str(datetime.datetime.now())
    json_data = json.dumps(data)
    data_dump[img_counter] = json_data
    print(json_data)
    img_counter = img_counter + 1
    img_name_counter = img_name_counter + 1
    sleep(interval_secs)


import torch.nn as nn
import torch

def conv3x3(in_planes, out_planes, stride=1):
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

def conv1x1(in_planes, out_planes, stride=1):
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)

class SELayer(nn.Module):
    def __init__(self, in_planes, out_planes, reduction=16):
        super(SELayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(in_planes, out_planes // reduction),
            nn.ReLU(inplace=True),
            nn.Linear(out_planes // reduction, out_planes),
            nn.Sigmoid()
        )
        self.out_planes = out_planes

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y).view(b, self.out_planes, 1, 1)
        return y

class CAB(nn.Module):
    def __init__(self, in_planes, out_planes, reduction=16):
        super(CAB, self).__init__()
        self.channel_attention = SELayer(2*in_planes, out_planes, reduction)

    def forward(self, guidePath, mainPath):
        combined = torch.cat((guidePath, mainPath), 1)
        channel_attetion = self.channel_attention(combined)
        out = mainPath + channel_attetion * guidePath
        return out

class PAM(nn.Module):
    def __init__(self, in_planes, out_planes, reduction=16, bn_momentum=0.0003):
        self.init__ = super(PAM, self).__init__()
        self.in_planes = in_planes
        self.bn_momentum = bn_momentum

        self.cabRGB = CAB(in_planes, out_planes, reduction)
        self.cabHHA = CAB(in_planes, out_planes, reduction)

        self.getAttention1 = nn.Conv2d(in_planes*2, 1, kernel_size=1, bias=True)
        self.getAttention2 = nn.Conv2d(in_planes*2, 1, kernel_size=1, bias=True)

        self.relu1 = nn.ReLU()
        self.relu2 = nn.ReLU()

        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        rgb, hha = x
        b, c, h, w = rgb.size()

        guideRGB = self.cabRGB(hha, rgb)
        guideHHA = self.cabHHA(rgb, hha)

        cat_fea = torch.cat([guideRGB, guideHHA], dim=1)

        attention_vector_l = self.getAttention1(cat_fea)
        attention_vector_r = self.getAttention2(cat_fea)

        attention_vector = torch.cat([attention_vector_l, attention_vector_r], dim=1)
        attention_vector = self.softmax(attention_vector)

        attention_vector_l, attention_vector_r = attention_vector[:, 0:1, :, :], attention_vector[:, 1:2, :, :]

        merge_feature = rgb*attention_vector_l + hha*attention_vector_r

        rgb_out = (rgb + merge_feature) / 2
        hha_out = (hha + merge_feature) / 2

        rgb_out = self.relu1(rgb_out)
        hha_out = self.relu2(hha_out)

        return [rgb_out, hha_out], merge_feature


# -*- coding: utf-8 -*-
from __future__ import absolute_import

from celery import Celery

app = Celery('proj',
             broker='amqp://',
             backend='amqp://',
             include=['celery_test.tasks'])
# include 옵션으로 해당 모듈을 포함해야,
# worker 가 그 모듈을 찾을 수 있다.

# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)

if __name__ == '__main__':
    app.start()


from mrjob.job import MRJob
import re
import os

WORD_RE = re.compile(r"[\w']+")

class MRWordLength(MRJob):
    def mapper(self, _, line):
        filename = os.environ['map_input_file'].split("/")[-1]
        for word in WORD_RE.findall(line):
            yield filename, len(word)

    def combiner(self, filename, lengths):
        total = 0
        count = 0
        for word_len in lengths:
            total += word_len
            count += 1
        yield filename, (total, count)

    def reducer(self, filename, vals):
        total_total = 0
        total_count = 0
        for total, count in vals:
            total_total += total
            total_count += count
        yield filename, total_total / float(total_count)

if __name__ == '__main__':
    MRWordLength.run()


#yolov4를 tf로 변환한 것을 사용하기 위한 것
import time
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
#GPU 메모리 증가 허용
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import cv2
import numpy as np

#flask를 사용하기 위한 것
from flask import Flask, render_template, Response

framework = 'tf' #'tflite' # tf, tflite, trt
weights = './checkpoints/yolov4-416' #변환한 모델이 저장된 경로 적기
size = 416 # resize images to
tiny = False  # yolo-tiny인 경우 True 아니라면 False
model = 'yolov4' # yolov3 or yolov4
iou = 0.45 # iou threshold
score = 0.25 # score threshold (0.25 이상의 확률값이 나온 것만 탐지하기)

input_size = 416
webcam = cv2.VideoCapture(0) #webcam 사용
app = Flask(__name__)

#tf model load
saved_model_loaded = tf.saved_model.load(weights, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']

def gen_frames():
    frame_id = 0
    while True:
        # 특정 조건을 만족하는 경우 카메라를 중지하고자 하는 경우 (아래 예시는 frame_id가 10인 경우)
        if (frame_id==10):
            webcam.release()
            break

        return_value, frame = webcam.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            if frame_id == webcam.get(cv2.CAP_PROP_FRAME_COUNT):
                print("Video processing complete")
                break
            raise ValueError("No image! Try with another video format")

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        batch_data = tf.constant(image_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

        # 필요한 값들 계산
        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=iou,
            score_threshold=score
        )
        pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
        image = utils.draw_bbox(frame, pred_bbox)
        result = np.asarray(image)

        #각 물체가 몇%의 확률로 해당 물체라고 판별했는지 해당 물체를 판별한 시각을 출력
        object_num = -1
        flag = 0
        for i in scores.numpy()[0]:
            object_num += 1
            now_time = time.strftime('%Y'+'-'+'%m'+'-'+'%d'+'T'+'%H' + '-' + '%M' + '-' + '%S')
            if (i != 0):
                print(object_num, '번째 물체의 확률:', scores.numpy()[0][object_num], '시각:', now_time)
            else:
                if (object_num == 0):
                    flag = 1
                break

        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        #이미지 저장
        if (flag == 0):
            cv2.imwrite("C:/Users/USER/Desktop/capture/" + now_time + ".png", result)

        frame_id += 1

        #webcam에서 찍고 있는 화면을 web상에서 보여줌.
        ret, buffer = cv2.imencode('.jpg', result)
        frame1 = buffer.tobytes()
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame1 + b'\r\n')

@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

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

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


import datetime

import eyed3
from eyed3 import id3

def modify_mp3_tag(mp3file, title, album, artist):
    print(mp3file)
    audiofile = eyed3.load(mp3file)
    # If no tag is set, need to set Tag instance implicitly.
    if audiofile.tag is None:
        audiofile.tag = id3.Tag()
    audiofile.tag.artist = artist
    audiofile.tag.album = album
    audiofile.tag.title = title
    audiofile.tag.save()

def parse_start_date(yyyy_mm_dd_hh_mm_str):
    return datetime.datetime.strptime(yyyy_mm_dd_hh_mm_str, '%Y-%m-%d-%H-%M')