from neo4j import GraphDatabase
import pymatgen.core as pmat

from poly_graphs_lib.database import PASSWORD,DBMS_NAME,LOCATION,DB_NAME,CIF_DIR
from poly_graphs_lib.database.populate.nodes import Node

def populate_element_nodes(elements):
    elements = dir(pmat.periodic_table.Element)[:-4]
    create_statements = []
    for element in elements[:]:
        # pymatgen object. Given element string, will have useful properties
        pmat_element = pmat.periodic_table.Element(element)

        node=Node(node_name=element,class_name='Element')
        node.add_property(name='atomic_number',value=pmat_element.Z)
        node.add_property(name='atomic_mass',value=float(pmat_element.atomic_mass))

        if str(pmat_element.X) != 'nan':
            node.add_property(name='X',value=pmat_element.X)
        else:
            node.add_property(name='X',value=None)

        if str(pmat_element.atomic_radius) != 'nan' and str(pmat_element.atomic_radius) != 'None':
            node.add_property(name='atomic_radius',value=float(pmat_element.atomic_radius))
        else:
            node.add_property(name='atomic_radius',value=None)

        if str(pmat_element.group) != 'nan':
            node.add_property(name='group',value=pmat_element.group)
        else:
            node.add_property(name='group',value=None)

        if str(pmat_element.row) != 'nan':
            node.add_property(name='row',value=pmat_element.row)
        else:
            node.add_property(name='row',value=None)

        execute_statement=node.final_execute_statement()
        create_statements.append(execute_statement)

    return create_statements

def main():
    # This statement Connects to the database server
    connection = GraphDatabase.driver(LOCATION, auth=(DBMS_NAME, PASSWORD))
    # To read and write to the data base you must open a session
    session = connection.session(database=DB_NAME)

    elements = dir(pmat.periodic_table.Element)[:-4]
    create_statements=populate_element_nodes(elements=elements)
    for execute_statment in create_statements:
        session.run(execute_statment)

    session.close()
    connection.close()

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  7 09:42:46 2022

Main code to estimate the committor using one of the methods described in the article.
You can choose either the AMOC or double-gyre model
You can choose of one the 5 methods: AMC, KNN, FFN, DGA or RC

@author: Valerian Jacques-Dumas
"""

import Datasets
import numpy as np
import Machine_Learning, AMC
import matplotlib.pyplot as plt
from NewGen_Reservoir import NewGen_ResNet
import Model_Cimatoribus, Model_wind_driven
from Logarithm_score import LogaScore, diff_score
from Dynamical_Galerkin_Approximation import DynGalApp

# Define both models and needed info to load datasets
C = Model_Cimatoribus.CimatoribusModel()
idx, noise = 60, 0.4
p_c = {"idx":idx, "noise":noise}

sigma = 0.45
beta = 0.198
WD = Model_wind_driven.WindDrivenModel(1, sigma=sigma, beta=beta)
p_wd = {"sigma":sigma, "beta":beta}

Cima = {'model':C, 'params':p_c}
Wind = {'model':WD, 'params':p_wd}

models = {'C':Cima, 'WD':Wind}

Nt = [10, 20, 30, 50, 75, 100, 150, 200, 300, 400, 500]

nb_dataset = 1

# Choose the model to use and the method to use

to_use = "WD"
assert to_use in ["AMOC", "WD"]

method = "Reservoir"
assert method in ["AMC", "KNN", "Feedforward NN", "DGA", "Reservoir"]

#Whether to save the results or not
save = False
dir_data #Directory containing the data
base_dir #Directory containing the results
dirsave = base_dir+models[to_use]['model'].name

# We load the data corresponding to the chosen model
par = models[to_use]['params']
handler =  Datasets.DataHandler(models[to_use]['model'], par, np.max(Nt), nb_dataset, dir_data)
# Load the true committor of the largest dataset to define the climatology of the logarithm score
ref_comm = handler.open_data("Committor")
score = LogaScore(ref_comm)

# Load test dataset
test = handler.open_data("Trajectory", test=True)
true_comm = handler.open_data("Committor", test=True)
lab_test = handler.open_data("Labels", test=True)

# For reference and comparison, the logarithm score of the true committor
# We need its average, 5th and 95th percentile
true_scores = score.get_score(test,true_comm,lab_test)
m, e = np.mean(true_scores), np.percentile(true_scores, [5,95])

# Process of each method

if method == "AMC" or method == "KNN":

    var = ["Sdiff", "Sn", "Ss"] if to_use=="C" else ["A1", "A2", "A3", "A4"]
    K = 25 if to_use=="C" else 10
    norm = False if to_use=="C" else True

    name = "K_"+str(K)+"_"+"_".join(var)+"_"+"_".join([str(e).replace(".",",") for i in range(len(par)) for e in list(par.items())[i]])
    dirsave += "/AMC_KNN/"

    #Initialize method
    amc = AMC.AnaloguesMethod(models[to_use]['model'], K)

    if method == "AMC":
        # Compute committor with AMC only
        possible_analogues = np.arange(np.max(test.shape)-1)
        comm_amc = amc.process_AMC(test, possible_analogues, var, norm=norm)

        # Only retain the estimated committors having values in [0,1]
        idx = []
        for i in range(100):
            if np.all((comm_amc[i]>=0)&(comm_amc[i]<=1)):
                idx.append(i)
        print(len(idx)) #Number of retained trajectories

        # Logarithm score
        score_amc = score.get_score(test[idx], comm_amc[idx], lab_test[idx])
        print(np.mean(score_amc), np.std(score_amc))

        # Difference score
        diff_amc = diff_score(comm_amc[idx], true_comm[idx])
        print(np.mean(diff_amc), np.std(diff_amc))

        # Reshape the scores to make plotting easier (see Plot_results.py)
        score_amc = np.repeat(score_amc[np.newaxis], len(Nt), axis=0)
        diff_amc = np.repeat(diff_amc[np.newaxis], len(Nt), axis=0)
        all_times_amc = np.repeat(amc.all_times[idx][np.newaxis], len(Nt), axis=0)
        print(np.mean(all_times_amc), np.std(all_times_amc))

        if save:
            res_amc = {"logscore": np.round(score_amc,decimals=3),
                        "diffscore": np.round(diff_amc,decimals=3),
                        "time_test": np.round(all_times_amc,decimals=3),
                        "time_train": np.full((len(Nt),len(idx)), np.nan)}
            np.save(dirsave+"AMC"+name, res_amc)

    else:
        # Initialize files to save
        score_knn = np.zeros((len(Nt),100))
        diff_knn = np.zeros((len(Nt),100))
        all_times_knn_train = np.zeros((len(Nt),100))
        all_times_knn_test = np.zeros((len(Nt),100))

        # Estimation loop with KNN
        for i, nt in enumerate(Nt):
            print(nt)
            #Get data
            handler.Nt = nt
            train = handler.open_data("Trajectory")
            possible_analogues = np.arange(np.max(train.shape)-1)

            #Compute committor
            comm_knn = amc.process_KNN(train, test, possible_analogues, var, norm=norm)

            #Compute score
            score_knn[i] = score.get_score(test,comm_knn,lab_test)
            diff_knn[i] = diff_score(comm_knn, true_comm)
            print(np.mean(score_knn[i]), np.std(score_knn[i]))
            print(np.mean(diff_knn[i]), np.std(diff_knn[i]))

            #Save committor, time, score
            all_times_knn_train[i] = np.repeat(amc.time_amc, 100)
            all_times_knn_test[i] = amc.time_knn
            print(amc.time_amc)
            print(np.mean(all_times_knn_test[i]), np.std(all_times_knn_test[i]))

            if save:
                res_knn = {"logscore": np.round(score_knn,decimals=3),
                            "diffscore": np.round(diff_knn,decimals=3),
                            "time_train": np.round(all_times_knn_train,decimals=3),
                            "time_test": np.round(all_times_knn_test,decimals=3)}
                np.save(dirsave+"KNN"+name, res_knn)

elif method == "Feedforward NN":

    var = ["Sdiff", "D"] if to_use=="C" else ["A1", 'A2', "A3", "A4"]

    layers = [64,128,256]
    LR = 1e-4

    dirsave += "/ML/"

    name = "_".join([str(l) for l in layers])+"_LR_"+str(LR).replace(".",",")+"_"+"_".join(var)\
        +"_"+"_".join([str(e).replace(".",",") for i in range(len(par)) for e in list(par.items())[i]])

    #Initialize method
    nb_epochs, nb_runs = 30, 20
    # The directory where to save the best state of the NN after each epoch
    dir_ml = dir_data + "/save_best_state_ml/"
    nn = Machine_Learning.NeuralNetworks(nb_epochs, nb_runs, dir_ml, LR=LR)

    test = models[to_use]['model'].select_var(test, var)

    score_ml = np.zeros((len(Nt),nb_runs,100))
    diff_ml = np.zeros((len(Nt),nb_runs,100))
    all_times_train = np.zeros((len(Nt),nb_runs))
    all_times_test = np.zeros((len(Nt),nb_runs,100))

    # Estimation loop
    for i, nt in enumerate(Nt):
        print(nt)
        #Get data
        handler.Nt = nt
        train, lab_train = handler.open_data("Trajectory"), handler.open_data("Labels")
        # If applicable, reduce set of variables of train data
        train = models[to_use]['model'].select_var(train, var)

        #Compute committor
        comm = nn.process(train, lab_train, test, (len(var),layers))

        #Compute score
        for k in range(nb_runs):
            score_ml[i,k] = score.get_score(test,comm[k],lab_test)
            diff_ml[i,k] = diff_score(comm[k],true_comm)
        print(np.mean(score_ml[i]), np.std(score_ml[i]))
        print(np.mean(diff_ml[i]), np.std(diff_ml[i]))

        print(np.mean(diff_ml[i],axis=1))
        k_max = np.argmax(np.mean(diff_ml[i],axis=1))
        for j in range(10):
            plt.plot(true_comm[j],label="Truth")
            plt.plot(comm[k_max,j],label="Prediction")
            plt.ylabel("Committor", fontsize=13)
            plt.xlabel("Time (arbitrary unit)", fontsize=13)
            plt.legend()
            plt.title("Feedforward Network", fontsize=13)
            plt.show()

        #Save committor, time, score
        all_times_train[i] = nn.all_times_train
        all_times_test[i] = nn.all_times_test

        print(np.mean(nn.all_times_train), np.std(nn.all_times_train))
        print(np.mean(nn.all_times_test), np.std(nn.all_times_test))

        if save:
            res_ml = {"logscore": np.round(score_ml,decimals=3),
                      "diffscore": np.round(diff_ml,decimals=3),
                      "time_train": np.round(all_times_train,decimals=3),
                      "time_test": np.round(all_times_test,decimals=3)}
            np.save(dirsave+name, res_ml)

elif method == "DGA":

    dirsave += "/DGA/"

    var = ["Sdiff", "Sn", "Ss"] if to_use=="C" else ["A1", "A2", "A3", "A4"]

    process = "train_in_test"
    assert process in ["full_train", "no_train", "train_in_test"]
    give_params = True

    d, k_eps0, k_eps = None, None, None
    if give_params:
        if to_use == 'C':
            d, k_eps0, k_eps = 0.45, 15, -15
        else:
            d, k_eps0, k_eps = 0.75, 5, -5

    nb_modes = 10

    dga = DynGalApp(M=nb_modes, d=d, k_eps0=k_eps0, k_eps=k_eps)

    if process == "full_train":

        name = "Full_train_"+str(nb_modes)+"_modes_d_"+str(d).replace('.',',')+\
            "_k_eps0_"+str(k_eps0)+"_k_eps_"+str(k_eps)+"_"+"_".join(var)

        score_dga, diff_dga = np.zeros((len(Nt),test.shape[0])), np.zeros((len(Nt),test.shape[0]))
        time_train_dga, time_test_dga = np.zeros((len(Nt),test.shape[0])), np.zeros((len(Nt),test.shape[0]))

        for i, nt in enumerate(Nt[4:]):
            print(nt)
            handler.Nt = nt
            train = handler.open_data("Trajectory")

            comm_dga = dga.process_DGA(models[to_use]['model'], test, train_traj=train, process=process, var=var)

            score_dga[i] = score.get_score(test, comm_dga, lab_test)
            diff_dga[i] = diff_score(comm_dga, true_comm)
            time_train_dga[i] = np.repeat(dga.time_train, test.shape[0])
            time_test_dga[i] = dga.times_test

            print(np.mean(score_dga[i]), np.std(score_dga[i]))
            print(np.mean(diff_dga[i]), np.std(diff_dga[i]))
            print(dga.time_train)
            print(np.mean(time_test_dga), np.std(time_test_dga))
            print()

            if save:
                res_dga= {"logscore": np.round(score_dga,decimals=3),
                          "diffscore": np.round(diff_dga,decimals=3),
                          "time_train": np.round(time_train_dga,decimals=3),
                          "time_test": np.round(time_test_dga,decimals=3)}
                np.save(dirsave+name, res_dga)

    else:

        nb_transi = 3
        if process == "train_in_test":
            name = "Train_in_test_"+str(nb_transi)+"_transi_"+str(nb_modes)+"_modes_d_"+\
                str(d).replace('.',',')+"_k_eps0_"+str(k_eps0)+"_k_eps_"+str(k_eps)+"_"+"_".join(var)
        else:
            name = "No_train_"+str(nb_modes)+"_modes_d_"+str(d).replace('.',',')+\
                "_k_eps0_"+str(k_eps0)+"_k_eps_"+str(k_eps)+"_"+"_".join(var)

        comm_dga = dga.process_DGA(models[to_use]['model'], test, label=lab_test, N_transi=nt, process=process, var=var)

        score_dga = score.get_score(test, comm_dga, lab_test)
        diff_dga = diff_score(comm_dga, true_comm)

        score_dga = np.repeat(score_dga[np.newaxis], len(Nt), axis=0)
        diff_dga = np.repeat(diff_dga[np.newaxis], len(Nt), axis=0)
        all_times_test_dga = np.repeat(dga.times_test[np.newaxis], len(Nt), axis=0)

        print(np.mean(score_dga), np.std(score_dga))
        print(np.mean(diff_dga), np.std(diff_dga))
        print(np.mean(dga.times_test), np.std(dga.times_test))

        if process == "train_in_test":
            all_times_train_dga = np.repeat(dga.times_train[np.newaxis], len(Nt), axis=0)
            print(np.mean(dga.times_train), np.std(dga.times_train))
        else:
            all_times_train_dga = np.repeat(np.nan, len(Nt))

        if save:
            res_dga = {"logscore": np.round(score_dga,decimals=3),
                        "diffscore": np.round(diff_dga,decimals=3),
                        "time_test": np.round(all_times_test_dga,decimals=3),
                        "time_train": np.round(all_times_train_dga,decimals=3)}
            np.save(dirsave+name, res_dga)

elif method == "Reservoir":

    var = ["Sdiff", "Sn", "Ss", "D"] if to_use=="C" else ["A1", "A2", "A3", "A4"]
    if to_use == 'C':
        config = {'k':3, 's':1, 'p':4, 'alpha':1e-9}
    else:
        config = {'k':2, 's':1, 'p':6, 'alpha':1e-6}

    dirsave += "/Reservoir/"
    name = "New_generation_"+"_".join(var)+"_"+"_".join([str(e).replace(".",",") for i in range(len(config)) for e in list(config.items())[i]])

    test = models[to_use]['model'].select_var(test, var)

    score_res, diff_res = np.zeros((len(Nt),100)), np.zeros((len(Nt),test.shape[0]))
    time_train_res, time_test_res = np.zeros(len(Nt)), np.zeros((len(Nt),test.shape[0]))

    for i, nt in enumerate(Nt):
        print(nt)
        handler.Nt = nt
        train, train_comm = handler.open_data("Trajectory"), handler.open_data("Committor")
        train = models[to_use]['model'].select_var(train, var)

        esn = NewGen_ResNet(config['k'], config['s'], config['p'], config['alpha'])
        pred_test = esn.process(train, train_comm, test)
        pred_test[pred_test>1], pred_test[pred_test<0] = 1, 0

        score_res[i] = score.get_score(test, pred_test, lab_test)
        diff_res[i] = diff_score(pred_test, true_comm)
        time_train_res[i] = esn.time_train
        time_test_res[i] = esn.times_test

        for j in range(10):
            plt.plot(true_comm[j],label="Truth")
            plt.plot(pred_test[j], label="NGRN estimate")
            plt.ylabel("Committor", fontsize=13)
            plt.xlabel("Time (arbitrary unit)", fontsize=13)
            plt.legend()
            plt.title("New Generation Reservoir Computing", fontsize=13)
            plt.show()

        m_log, e_log = np.mean(score_res[i]), np.percentile(score_res[i], [5,95])
        m_diff, e_diff = np.mean(diff_res[i]), np.percentile(diff_res[i], [5,95])
        m_test, e_test = np.mean(time_test_res[i]), np.percentile(time_test_res[i], [5,95])
        print(m_log, e_log[0], e_log[1])
        print(m_diff, e_diff[0], e_diff[1])
        print(time_train_res[i])
        print(m_test, e_test[0], e_test[1])
        print()

    if save:
        res_rl= {"logscore": np.round(score_res,decimals=3),
                  "diffscore": np.round(diff_res,decimals=3),
                  "time_train": np.round(time_train_res,decimals=3),
                  "time_test": np.round(time_test_res,decimals=3)}
        np.save(dirsave+name, res_rl)


'''
Class Files for ImageProc Class
Captures Images through Raspberry Pi camera
Captures Command Signals
Classifies Images in Classes 1-7
Stores Images in Train/1-7 directories
Manish Mahajan
9 Dec 2019
'''
# import the necessary packages
from yaml import load, Loader, dump
from picamera.array import PiRGBArray
from picamera import PiCamera
import os
import io
import sys
import time
from datetime import datetime
import numpy as np
import argparse
import threading
from PIL import Image

#Testing Functions
def get_commands():
	return np.random.randn(2)
def is_driving():
	return True
def is_paused():
	return False
def get_category():
	return 'straight'
#####

class ImageProc(object):
	def __init__(self,args,params):
		try:
			camera = PiCamera(
				resolution = params['resolution'],
				framerate = args.framerate)
		except:
			sys.exit('could not set up camera')
		camera.rotation = params['rotation']
		camera.iso = params['iso'] #800 for indoors, 200 outdoors
		# allow the camera to warmup
		time.sleep(2)
		camera.exposure_mode = params['exposure_mode']
		camera.awb_mode = params['awb_mode']
		self.camera = camera
		self.args = args
		self.params = params
		self._lock=threading.RLock()
		self.image_array = PiRGBArray(camera, size=params['resolution'])
		self.frame_num = 0
		#create requisite Directories
		if args.selfdrive == 'True':
			self.dirName = os.path.join(params['self_drive_dirname'],\
				'Example{0}'.format(args.example))
		else:
			self.dirName = os.path.join(params['training_dirname'],\
				'Example{0}'.format(args.example))

		if args.labels == 'True':
			self.labels=[]
		else:
			for category_name in params['category_names']:
				try:
					os.makedirs(os.path.join(self.dirName,category_name))
				except FileExistsError:
					pass
		pass

	def collect_data(self,get_category=get_category,\
			get_commands=get_commands,is_driving=is_driving,is_paused=is_paused):
		'''
		collects example data and stores to file in separate directories
		Classifies the steer data into categories and stores them in relevant Directories
		'''
		start_time = time.time()
		frame_num = 0
		with io.BytesIO() as stream:
			for _ in self.camera.capture_continuous(stream,format="jpeg",use_video_port=True):

				while is_paused() and is_driving():
					time.sleep(0.5)
				if not is_driving():
					break
				category = get_category()
				if category == 'straight':
					self.camera.framerate = self.args.framerate/10
					#capture stright category at a sampling rate of 10%
				else:
					self.camera.framerate = min(50,self.args.framerate)
				timestring = datetime.now().strftime("%Y%m%d%H%M%S-%f")[:-3]

				if self.args.labels == 'True':
					filepath = self.dirName
					self.labels.append([category,get_commands()])
				else:
					filepath = os.path.join(self.dirName,category)

				filename = os.path.join(filepath,'img{0:d}-{1}-{2}.jpg'\
					.format(self.args.example,category,timestring))
				with open(filename,"wb") as imagefile:
					imagefile.write(stream.getbuffer())
				# clear the stream in preparation for the next frame
				stream.seek(0)
				stream.truncate(0)
				frame_num +=1
		print('Finished Collecting {0:d} frames in {1} mins'.\
			format(frame_num,(time.time()-start_time)/60.0))
		self.cleanup()
		pass

	def capture_image(self,get_category=get_category,\
			get_commands=get_commands,is_driving=is_driving,is_paused=is_paused):
			'''
			capture images for the self driver; sends data for prediction ;
			also stores them in files for later analysis
			'''
			frame_num = 0
			with PiRGBArray(self.camera) as stream:
				for _ in self.camera.capture_continuous(stream,format="rgb",use_video_port=True):
					#print(is_paused(), is_driving())
					while is_paused() and is_driving():
						time.sleep(0.05)
					if not is_driving():
						break
					category = get_category()
					timestring = datetime.now().strftime("%Y%m%d%H%M%S-%f")[:-3]
					with self._lock:
						self.image_array=stream
						self.frame_num+=1

					if self.args.labels == 'True':
						filepath = self.dirName
						self.labels.append([category,get_commands()])
					else:
						filepath = os.path.join(self.dirName,category)

					filename = os.path.join(filepath,'img{0:d}-{1}-{2}.jpg'\
						.format(self.args.example,category,timestring))
					#Write to File, using PIL Image class definition
					Image.fromarray(stream.array).save(filename)
					# clear the stream in preparation for the next frame
					stream.truncate(0)
					frame_num+=1
					#if frame_num == self.Max_Frames:
					#	break
			print('Finished Recording {0:d} Images'.format(frame_num))
			self.cleanup()
			pass

	def cleanup(self):
		self.camera.close()
		if self.args.labels=='True' :
			np.save(os.join(self.dirName,'labels_{0:d}.npy'\
				.format(self.args.example)),self.labels,allow_pickle=True)
		self.params['example']+=1
		with open('/home/pi/ChaturCar/ChaturCar.yaml','w') as outfile:
			dump(self.params,outfile)
		print('ImageProc Cleaned Up')
		pass

	def get_frame_num(self):
		with self._lock:
			return self.frame_num

	def set_frame_num(self,frame_num):
		with self._lock:
			self.frame_num=frame_num
		pass

	def set_PiRGBimage_array(self,image_array):#PIRGBArray):
		with self._lock:
			self.image_array=image_array

	def get_image_array(self):
		#returns array attribute
		with self._lock:
			return self.image_array.array

def main():
	params = load(open('ChaturCar.yaml').read(), Loader=Loader)
	parser = argparse.ArgumentParser(description='Capture Video and Content Frames')
	parser.add_argument('--example', default=params['example'])
	parser.add_argument('--framerate',default=params['framerate'])
	parser.add_argument('--selfdrive',default=params['selfdrive'])
	parser.add_argument('--collectdata',default=params['collectdata'])
	parser.add_argument('--labels', default=params['labels'])
	args = parser.parse_args()
	c = ImageProc(args,params)
	c.collect_data()
if __name__=="__main__":
        main()


# TODO - Fix expression command, and interaction with negative numbers.
# TODO - Add more commands!

import discord
from discord.ext import commands
import math
import re

class MathCog:
    """
        Used for math functions such as adding, subtracting, multiplying, and dividing.
        Uses regexes for more friendly interaction.
        I currently have an expression command utilizing Python's built in parser, but doesn't work too well currently.
        It has quite a few problems, like interacting with negative numbers will mess up sometimes. I'll have to look into that.
    """

    def __init__(self, client):

        self.client = client
        self.simpleRegex = re.compile(r'''
                                                    ([-]?   # Checks for negative number
                                                    [0-9]*  # Integer part of number
                                                    \.?[0-9]+)+ #Decimal part of number
                                                    ''', re.VERBOSE)

    @commands.command()
    async def expression(self, expression: str):
        """Uses a parser to solve more complicated equations. With the built in Python one, it's not that good."""

        # Will use parser for more complicated equations.
        compiledequation = compile(expression, "math.py", "eval")

        await self.client.say(eval(compiledequation))

    @commands.command()
    async def add(self, *args):
        """Adds numbers."""

        if len(list(args)) == 0:
            await self.client.say("0")
            return

        # String to be passed to regex, will include all the user parameters.
        regexstring = ""

        for x in list(args):
            regexstring += str(x) + " "

        mo = self.simpleRegex.findall(regexstring)

        for x in mo:
            x.strip()

        total = float(mo[0])

        for x in mo[1:]:
            total += float(x)

        await self.client.say(total)

    @commands.command()
    async def subtract(self, *args):
        """Subtracts numbers."""

        if len(list(args)) == 0:
            await self.client.say("0")
            return

        regexstring = ""
        for x in list(args):
            regexstring += str(x) + " "

        mo = self.simpleRegex.findall(regexstring)

        for x in mo:
            x.strip()

        total = float(mo[0])

        for x in mo[1:]:
            total -= float(x)

        await self.client.say(total)

    @commands.command()
    async def multiply(self, *args):
        """Multiplies numbers."""

        if len(list(args)) == 0:
            await self.client.say("0")
            return

        regexstring = ""
        for x in list(args):
            regexstring += str(x) + " "

        mo = self.simpleRegex.findall(regexstring)

        for x in mo:
            x.strip()

        total = float(mo[0])

        for x in mo[1:]:
            total *= float(x)

        await self.client.say(total)

    @commands.command()
    async def divide(self, *args):
        """Divides numbers."""

        if len(list(args)) == 0:
            await self.client.say("0")
            return

        regexstring = ""
        for x in list(args):
            regexstring += str(x) + " "

        mo = self.simpleRegex.findall(regexstring)

        for x in mo:
            x.strip()

        total = float(mo[0])

        for x in mo[1:]:
            total /= float(x)

        await self.client.say(total)

def setup(client):
    client.add_cog(MathCog(client))


import xml.etree.ElementTree as ET
import os

#Initializig file location
dir_path=os.path.dirname(os.path.realpath(__file__))
dir_path=dir_path.replace("\\",'/')
#XML file
file_name="person.xsd"
input_filename=dir_path+'/input_files/'+file_name

try:
    #Reading the file
    fhandler=open(input_filename,'r')
except:
    print("Provided file name",file_name,"is not available in the path",dir_path)
    quit()

tree = ET.fromstring(fhandler.read())
#creates an iterative list
data_list = tree.findall('people/user')

for i in data_list:
    print("******* MEMBER DETAILS *******")
    print('Membership ID: ', i.find('name').get('member_id'))
    print('Name: ' , i.find('name').text)
    print('phone: ' , i.find('phone').text)
    print('sex: ' , i.find('sex').text)


import cv2
from cv2 import aruco
import numpy as np

### --- aruco設定 --- ###
dict_aruco = aruco.Dictionary_get(aruco.DICT_4X4_50)
parameters = aruco.DetectorParameters_create()

cap = cv2.VideoCapture(0)

try:
    while True:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

        corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, dict_aruco, parameters=parameters)
        list_ids = np.ravel(ids)
        frame_markers = aruco.drawDetectedMarkers(frame.copy(), corners, ids)
        windowsize = (800, 600)
        frame_markers = cv2.resize(frame_markers, windowsize)
        print(list_ids)
        cv2.imshow('frame', frame_markers)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyWindow('frame')
    cap.release()
except KeyboardInterrupt:
    cv2.destroyWindow('frame')
    cap.release()


from flask import Flask
from flask import request
# from pymongo import MongoClient
app = Flask(__name__)

# client = MongoClient('localhost:27017')
# db=client.air_data

print("done establishing connection!")

@app.route('/sensor',methods = ['POST', 'GET'])
def sensor():
   if request.method == 'POST':
        # print (request.is_json)
        content = request.json
        print(content)
#         db.air_datas.insert_one(content)
#         fivestar = db.sensor_datas.find()
#         print(fivestar)
        return "done"
   else:
        return "error"
@app.route('/',methods = ['GET'])
def index():
   return "done"

@app.route('/val/<co>/<dust>',methods = ['GET'])
def val(co=None,dust=None):
	print(co+" "+dust)
	db.air_datas.insert_one({'co': co,'dust':dust})
	return co+" "+dust

@app.route('/<area>',methods = ['GET'])
def val(area=None):
	print(area)
	#db.air_datas.insert_one({'co': co,'dust':dust})

	return "{'co':'211','du':'211'}"

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


import numpy as np
import pytest

from pandas import DataFrame, date_range
import pandas._testing as tm

class TestTranspose:
    def test_transpose_tzaware_1col_single_tz(self):
        # GH#26825
        dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")

        df = DataFrame(dti)
        assert (df.dtypes == dti.dtype).all()
        res = df.T
        assert (res.dtypes == dti.dtype).all()

    def test_transpose_tzaware_2col_single_tz(self):
        # GH#26825
        dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")

        df3 = DataFrame({"A": dti, "B": dti})
        assert (df3.dtypes == dti.dtype).all()
        res3 = df3.T
        assert (res3.dtypes == dti.dtype).all()

    def test_transpose_tzaware_2col_mixed_tz(self):
        # GH#26825
        dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")
        dti2 = dti.tz_convert("US/Pacific")

        df4 = DataFrame({"A": dti, "B": dti2})
        assert (df4.dtypes == [dti.dtype, dti2.dtype]).all()
        assert (df4.T.dtypes == object).all()
        tm.assert_frame_equal(df4.T.T, df4)

    @pytest.mark.parametrize("tz", [None, "America/New_York"])
    def test_transpose_preserves_dtindex_equality_with_dst(self, tz):
        # GH#19970
        idx = date_range("20161101", "20161130", freq="4H", tz=tz)
        df = DataFrame({"a": range(len(idx)), "b": range(len(idx))}, index=idx)
        result = df.T == df.T
        expected = DataFrame(True, index=list("ab"), columns=idx)
        tm.assert_frame_equal(result, expected)

    def test_transpose_object_to_tzaware_mixed_tz(self):
        # GH#26825
        dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")
        dti2 = dti.tz_convert("US/Pacific")

        # mixed all-tzaware dtypes
        df2 = DataFrame([dti, dti2])
        assert (df2.dtypes == object).all()
        res2 = df2.T
        assert (res2.dtypes == [dti.dtype, dti2.dtype]).all()

    def test_transpose_uint64(self, uint64_frame):

        result = uint64_frame.T
        expected = DataFrame(uint64_frame.values.T)
        expected.index = ["A", "B"]
        tm.assert_frame_equal(result, expected)

    def test_transpose_float(self, float_frame):
        frame = float_frame
        dft = frame.T
        for idx, series in dft.items():
            for col, value in series.items():
                if np.isnan(value):
                    assert np.isnan(frame[col][idx])
                else:
                    assert value == frame[col][idx]

        # mixed type
        index, data = tm.getMixedTypeDict()
        mixed = DataFrame(data, index=index)

        mixed_T = mixed.T
        for col, s in mixed_T.items():
            assert s.dtype == np.object_

    def test_transpose_get_view(self, float_frame):
        dft = float_frame.T
        dft.values[:, 5:10] = 5

        assert (float_frame.values[5:10] == 5).all()


import json

from .oauth import OAuth2Test

class UniverseAuth2Test(OAuth2Test):
    backend_path = "social_core.backends.universe.UniverseOAuth2"
    user_data_url = "https://www.universe.com/api/v2/current_user"
    expected_username = "scott+awesome@universe.com"
    access_token_body = json.dumps({"access_token": "foobar", "token_type": "bearer"})
    user_data_body = json.dumps(
        {
            "current_user": {
                "id": "123456",
                "slug": "foo-bar",
                "first_name": "Scott",
                "last_name": "Vitale",
                "created_at": "2019-01-08T15:49:42.514Z",
                "updated_at": "2019-01-17T19:41:39.711Z",
                "email": "scott+awesome@universe.com",
            }
        }
    )

    def test_login(self):
        self.do_login()

    def test_partial_pipeline(self):
        self.do_partial_pipeline()


#!/bin/python
import os
import js2py
from pathlib import Path

from configStrategies import cS
from configIndicators import cI

class makeSettings():
    def __init__(self, entries):
        '''
        print(entries)
        def iterate(self, DATA):
            for W in DATA.keys():
                if type(DATA[W]) == dict:
                    iterate(self,DATA[W])
                else:
                    self.__dict__.update(DATA)
        iterate(self,entries)
        '''
        self.__dict__.update(entries)

    def getstrat(self, name):
        return self.strategies[name]

def getSettings(specific=None):
    HOME = str(Path.home())
    s = {
        'Global': {
            'gekkoPath': '/steve/gekko',
            'configFilename': 'config.js',
            'log_name': 'evolution_gen.csv',

            # Hosts list of remote machines running gekko, to distribute evaluation load;
            # option values: path to HOSTS file list OR False;
            'RemoteAWS': '../AmazonSetup/hosts',

            # Your gekko local URL - CHECK THIS!
            'GekkoURLs': ['http://localhost:3000','http://18.219.12.229:3000','http://18.221.183.250:3000'],
            'showFailedStrategies': False
        },
        # genetic algorithm settings
        'generations': {
            # show gekko verbose (strat info) - gekko must start with -d flag;
            'gekkoDebug': True,

            # Verbose single evaluation results;
            'showIndividualEvaluationInfo': True,

            # if parameter is set to value rather than tuple limits at settings, make the value
            # a tuple based on chosen spread value (percents); value: 10 --spread=50-->  value: (5,15)
            'parameter_spread' : 60,

            # Initial population size, per locale
            'POP_SIZE': 30,

            # number of epochs to run
            'NBEPOCH': 800,

            # number of locales on parallel GA;
            'NBLOCALE': 6,

            # show current best settings on every X epochs. (or False)
            'evaluateSettingsPeriodically': False,

            # time window size on days of candlesticks for each evaluation
            'deltaDays': 90,

            # Number of candlestick data loaded simultaneously in each locale;
            # slower EPOCHS, theoretical better evolution;
            # seems broken. values other than 1 makes evolution worse.
            'ParallelCandlestickDataset': 1,

            # -- Genetic Algorithm Parameters
            'cxpb': 0.8, # Probabilty of crossover
            'mutpb': 0.2,# Probability of mutation;
            '_lambda': 7,# size of offspring generated per epoch;

            # weight of PRoFIGA calculations on variability of population size
            'PRoFIGA_beta': 0.005,
            'ageBoundaries': (9, 19), # minimum age to die, age when everyone dies (on EPOCHS)

            'candleSize': 10, # candle size for gekko backtest, in minutes

            # number of evaluations on evaluation break. for each selected individue on locales;
            'proofSize': 12,
            # filter individuals for minimum trade count; [has heavy impact]
            'minTradeNumber': 6,
            'DRP': 70,# Date range persistence; Number of subsequent rounds
             # until another time range in dataset is selected;

            # number of parallel backtests running on gekko;
            'ParallelBacktests': 16,

            'finaltest': {
                # number of individues selected by score on each evaluation break for each locale;
                'NBBESTINDS': 2,
                # number of individues randomly selected on each evaluation break for each locale;
                'NBADDITIONALINDS': 4,
            },
            # chromosome settings are for -gc mode, which uses another GA internal representation mode
            # for parameter values of each individue;
            # check promoterz/representation/chromosome.py to see how it works.
            # both parameters interact with crossover probability AKA cxpb;
            # practical effects on evolution are really uknown;
            'chromosome': {
                # length of the representation for each parameter. largers sizes should mantain
                # evolutionary dynamics for parameters with proportional larger ranges;
                'GeneSize': 2,
                # number of parameters represented by each cromosome;
                # this should mantain evolutionary dynamics for strategies with proportional larger
                # parameter counts;
                'Density': 3,
            },
            # weights to score each individual, self explanatory;
            'weights': {
                'profit': 1.0,
                'sharpe': 0.1},

            # mode of profit interpretation: v1, v2 or v3.
            # please check the first functions at evaluation.gekko.backtest
            # to understand what is this. has big impact on evolutionary agenda.
            'interpreteBacktestProfit': 'v3',

            # after this age in epoches, locale surely explodes i.e. ends.
            'localeExpirationAge': 100,
            # chance, on each epoch, of a locale to finish [in percentage];
            'localeExplodeChance': 2
        },

        # bayesian optimization settings
        'bayesian': {
            # show gekko verbose (strat info) - gekko must start with -d flag;
            'gekkoDebug': False,

            # time window size on days of candlesticks for each evaluation
            'deltaDays': 120,

            # number of evaluation rounds
            'num_rounds': 10,

            # seed for randomziation of values
            'random_state': 2018,

            # number of iterations on each round
            'num_iter': 50,

            # number of random values to start bayes evaluation
            'init_points': 9,

            'parallel': False,
            'show_chart': False,
            'save': True,

            'parameter_spread': 100,

            # candleSize & historySize on Gekko, for all evals
            'candleSize': 10,
            'historySize': 1000,

            'watch':{
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'ETH'
            },

            'interpreteBacktestProfit': 'v3'

        },
        'dataset' : {
            # -- Gekko Dataset Settings
            # in order to enable dataset selection, turn off ignore&autoselect entry;
            'dataset_source': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'ETH',
            },
            'dataset_source2': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'BTC',
            },
            'dataset_source3': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'LTC',
            },
            'dataset_source4': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'XRP',
            },
            'dataset_source5': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'IOT',
            },
            'eval_dataset_source': {
                'ignore&autoselect': False,
                "exchange": "bitfinex",
                "currency": 'USD',
                "asset": 'ETH',
            },
            # span in days from the end of dataset to the beggining. Or zero.
            # (to restrain length);
            'dataset_span': 0,
            'eval_dataset_span': 0

            },
        'strategies': cS,
        'indicators': cI,
        'skeletons': {
            'ontrend': {
                "SMA_long": 1000,
                "SMA_short": 50
            }
        }
    }

    if specific != None:
        if not specific:
            return makeSettings(s)
        else:
            return makeSettings(s[specific])

    return s

def get_configjs(filename="example-config.js"):
    with open(filename, "r") as f:
        text = f.read()
    text = text.replace("module.exports = config;","config;")
    return js2py.eval_js(text).to_dict()


import logging

def set_logger(name: str, file_path: str)-> logging.GetLogger:

    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s')
    stream_handler = logging.StreamHandler()
    file_handler = logging.FileHandler(file_path)

    stream_handler.setFormatter(formatter)
    file_handler.setFormatter(formatter)

    logger.addHandler(stream_handler)
    logger.addHandler(file_handler)

    return logger

if __name__ == '__main__':

    logger = set_logger('test_logger', 'test.log')
    logger.info('test')


from typing import Dict
from src.shared.mongo import get_collection_from_config
from src.dao.raw_tweet.getter.mongo_raw_tweet_getter import MongoRawTweetGetter
from src.dao.processed_tweet.setter.mongo_processed_tweet_setter import MongoProcessedTweetSetter
from src.process.raw_tweet_processing.tweet_processor import TweetProcessor
from src.dao.raw_tweet.raw_tweet_dao_factory import RawTweetDAOFactory
from src.dao.local_neighbourhood.local_neighbourhood_dao_factory import LocalNeighbourhoodDAOFactory
from src.dao.processed_tweet.processed_tweet_dao_factory import ProcessedTweetDAOFactory

class ProcessLocalNeighbourhoodTweetsActivity():
    def __init__(self, config: Dict):
        self.tweet_processor = None
        self.local_neighbourhood_getter = None

        self.configure(config)

    def configure(self, config: Dict):
        if config is not None:
            input_datastore = config["input-datastore"]

            raw_tweet = input_datastore['UserTweet']
            local_neighbourhood = input_datastore['LocalNeighbourhood']

            raw_tweet_getter = RawTweetDAOFactory.create_getter(raw_tweet)
            local_neighbourhood_getter = LocalNeighbourhoodDAOFactory.create_getter(local_neighbourhood)

            output_datastore = config["output-datastore"]

            processed_tweet = output_datastore['ProcessedTweet']

            processed_tweet_setter = ProcessedTweetDAOFactory.create_setter(processed_tweet)

            self.tweet_processor = TweetProcessor(raw_tweet_getter,
                processed_tweet_setter)

            self.local_neighbourhood_getter = local_neighbourhood_getter

    def process_local_neighbourhood_tweets(self, seed_id: str, params=None):
        local_neighbourhood = self.local_neighbourhood_getter.get_local_neighbourhood(seed_id, params)
        self.tweet_processor.process_tweets_by_local_neighbourhood(local_neighbourhood)


import acquire
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
import pandas_profiling
df = get_titanic_data()
profile = pandas_profiling.ProfileReport(df)
profile.get_rejected_variables(threshold=.9)
df.isnull().sum()
df.age.value_counts(bins=9, sort=True)
df.embarked.value_counts(dropna=False)
df.age.value_counts(dropna=False)
df.drop(columns=["deck"], inplace=True)
df.fillna(np.nan, inplace=True)
train, test = train_test_split(df, train_size=.8, random_state=123)
train.age.value_counts(dropna=False)
imp_mode = SimpleImputer(missing_values=np.nan, strategy="most_frequent")
imp_mode.fit(train[["embarked"]])
train["embarked"]=imp_mode.transform(train[["embarked"]])
test["embarked"]=imp_mode.transform(test[["embarked"]])
train.embarked.value_counts()
imp_median = SimpleImputer(missing_values=np.nan, strategy="median")
train['age'] = imp_median.fit_transform(train[['age']])
train.age.isnull().sum()
int_encoder = LabelEncoder()
int_encoder.fit(train.embarked)
train.embarked = int_encoder.transform(train.embarked)
train.embarked.value_counts()
embarked_array = np.array(train.embarked)
embarked_array[0:5]
embarked_array = embarked_array.reshape(len(embarked_array),1)
ohe = OneHotEncoder(sparse=False, categories="auto")
embarked_ohe = ohe.fit_transform(embarked_array)
embarked_test_ohe = ohe.transform(embarked_array)
embarked_test_ohe[0:5]


import itertools
import time
import typing
from enum import IntFlag
from pathlib import Path
from typing import *

from pros.common import ui
from pros.common.utils import retries, logger
from pros.conductor import Project
from pros.serial import bytes_to_str
from pros.serial.devices.vex import VEXCommError
from pros.serial.devices.vex.stm32_device import STM32Device
from pros.serial.ports import list_all_comports

from .vex_device import VEXDevice
from ..system_device import SystemDevice

def find_cortex_ports():
    return [p for p in list_all_comports() if p.vid is not None and p.vid in [0x4D8, 0x67B]]

class CortexDevice(VEXDevice, SystemDevice):
    class SystemStatus(object):
        def __init__(self, data: Tuple[bytes, ...]):
            self.joystick_firmware = data[0:2]
            self.robot_firmware = data[2:4]
            self.joystick_battery = float(data[4]) * .059
            self.robot_battery = float(data[5]) * .059
            self.backup_battery = float(data[6]) * .059
            self.flags = CortexDevice.SystemStatusFlags(data[7])

        def __str__(self):
            return f'  Tether: {str(self.flags)}\n' \
                   f'  Cortex: F/W {self.robot_firmware[0]}.{self.robot_firmware[1]} w/ {self.robot_battery:1.2f} V ' \
                   f'(Backup: {self.backup_battery:1.2f} V)\n' \
                   f'Joystick: F/W {self.joystick_firmware[0]}.{self.robot_firmware[1]} w/ ' \
                   f'{self.joystick_battery:1.2f} V'

    class SystemStatusFlags(IntFlag):
        DL_MODE = (1 << 0)
        TETH_VN2 = (1 << 2)
        FCS_CONNECT = (1 << 3)
        TETH_USB = (1 << 4)
        DIRECT_USB = (1 << 5)
        FCS_AUTON = (1 << 6)
        FCS_DISABLE = (1 << 7)

        TETH_BITS = DL_MODE | TETH_VN2 | TETH_USB

        def __str__(self):
            def andeq(a, b):
                return (a & b) == b

            if not self.value & self.TETH_BITS:
                s = 'Serial w/VEXnet 1.0 Keys'
            elif andeq(self.value, 0x01):
                s = 'Serial w/VEXnet 1.0 Keys (turbo)'
            elif andeq(self.value, 0x04):
                s = 'Serial w/VEXnet 2.0 Keys'
            elif andeq(self.value, 0x05):
                s = 'Serial w/VEXnet 2.0 Keys (download mode)'
            elif andeq(self.value, 0x10):
                s = 'Serial w/ a USB Cable'
            elif andeq(self.value, 0x20):
                s = 'Directly w/ a USB Cable'
            else:
                s = 'Unknown'

            if andeq(self.value, self.FCS_CONNECT):
                s += ' - FCS Connected'
            return s

    def get_connected_device(self) -> SystemDevice:
        logger(__name__).info('Interrogating Cortex...')
        stm32 = STM32Device(self.port, do_negoitate=False)
        try:
            stm32.get(n_retries=1)
            return stm32
        except VEXCommError:
            return self

    def upload_project(self, project: Project, **kwargs):
        assert project.target == 'cortex'
        output_path = project.path.joinpath(project.output)
        if not output_path.exists():
            raise ui.dont_send(Exception('No output files were found! Have you built your project?'))
        with output_path.open(mode='rb') as pf:
            return self.write_program(pf, **kwargs)

    def write_program(self, file: typing.BinaryIO, **kwargs):
        action_string = ''
        if hasattr(file, 'name'):
            action_string += f' {Path(file.name).name}'
        action_string += f' to Cortex on {self.port}'
        ui.echo(f'Uploading {action_string}')

        logger(__name__).info('Writing program to Cortex')
        status = self.query_system()
        logger(__name__).info(status)
        if not status.flags | self.SystemStatusFlags.TETH_USB and not status.flags | self.SystemStatusFlags.DL_MODE:
            self.send_to_download_channel()

        bootloader = self.expose_bootloader()
        rv = bootloader.write_program(file, **kwargs)

        ui.finalize('upload', f'Finished uploading {action_string}')
        return rv

    @retries
    def query_system(self) -> SystemStatus:
        logger(__name__).info('Querying system information')
        rx = self._txrx_simple_struct(0x21, "<8B2x")
        status = CortexDevice.SystemStatus(rx)
        ui.finalize('cortex-status', status)
        return status

    @retries
    def send_to_download_channel(self):
        logger(__name__).info('Sending to download channel')
        self._txrx_ack_packet(0x35, timeout=1.0)

    @retries
    def expose_bootloader(self):
        logger(__name__).info('Exposing bootloader')
        for _ in itertools.repeat(None, 5):
            self._tx_packet(0x25)
            time.sleep(0.1)
        self.port.read_all()
        time.sleep(0.3)
        return STM32Device(self.port, must_initialize=True)

    def _rx_ack(self, timeout: float = 0.01):
        # Optimized to read as quickly as possible w/o delay
        start_time = time.time()
        while time.time() - start_time < timeout:
            if self.port.read(1)[0] == self.ACK_BYTE:
                return
        raise IOError("Device never ACK'd")

    def _txrx_ack_packet(self, command: int, timeout=0.1):
        """
                Goes through a send/receive cycle with a VEX device.
                Transmits the command with the optional additional payload, then reads and parses the outer layer
                of the response
                :param command: Command to send the device
                :param retries: Number of retries to attempt to parse the output before giving up and raising an error
                :return: Returns a dictionary containing the received command field and the payload. Correctly computes
                the payload length even if the extended command (0x56) is used (only applies to the V5).
                """
        tx = self._tx_packet(command)
        self._rx_ack(timeout=timeout)
        logger(__name__).debug('TX: {}'.format(bytes_to_str(tx)))


from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from .models import *
# Create your views here.

#admin Dashboard Loading

class AdminDashboard(View):
    def get(self,request):
        try:
            adminposts = ImagePosts.objects.all()
            context = {'posts':adminposts}
            return render(request,'index.html',context)
        except Exception as e:
            print(e)
    def post(self,request):
        try:
            postname = request.POST.get('postname')
            images = request.FILES
            desc = request.POST

            lend = len(images.getlist('image'))

            AdminPost.objects.get_or_create(post_name=postname)

            adminpost = AdminPost.objects.get(post_name=postname)
            for i in range(0,lend) :

                post = ImagePosts.objects.create(image_add=adminpost,images=images.getlist('image')[i],description=desc.getlist('desc')[i],tag=desc.getlist('tag')[i])

            return redirect('admindashboard')
        except Exception as e:
            print(e)

class LikedUsers(View):
    def get(self, request,id):
        post = ImagePosts.objects.get(id=id)

        statust = StatusTable.objects.filter(action=True,posts=post)

        return render(request,'likeduser.html',{'likedusers':statust})


import json
from typing import *

INPUT_FILE_NAME = "input"

def input_user_info() -> Union[Dict, bool]:
    try:
        with open(f"{INPUT_FILE_NAME}.json", "r", encoding="UTF-8") as f:
            input_data = json.load(f)

    except FileNotFoundError:
        # input.json 파일이 없음.
        with open(f"{INPUT_FILE_NAME}.json", "w", encoding="UTF-8") as f:
            f.write(
"""{
    "user_company" : "",
    "user_job" : "",
    "job_requirement" : "",
    "cover_letter" : "",
    "self_introduce" : ""
}"""
            )
        print("input.json을 열고, 정보를 작성하세요.")
        return False

    return input_data

def create_question_parser(question: str) -> List:
    question_list = question.replace("\n\n", "\n").split("\n")

    for quest_idx in range(len(question_list)):
        temp_str = question_list[quest_idx]
        for str_idx in range(len(temp_str)):
            if temp_str[str_idx] == ".":
                question_list[quest_idx] = temp_str[str_idx+1:].strip()
                break

    return question_list

def follow_up_question_parser(question: str) -> str:
    question.replace("\n", "")
    for str_idx in range(len(question)):
        if question[str_idx] == ":":
            return question[str_idx+1:].strip()
    return question

def remove_indent(string: str) -> str:
    return string.replace("    ", "").replace("\t", "")


import json
import numpy as np
from tqdm import tqdm
import argparse

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_file', type=str, required=True)
    parser.add_argument('--top_n', type=int, required=True)
    args = parser.parse_args()
    return args

def main():
    args = get_args()
    correct_lst = []
    M = args.top_n
    with open(args.data_file) as f:
        for line in tqdm(f):
            item = json.loads(line)
            gold_tables = item['table_id_lst']
            ctx_lst = item['ctxs']
            labels = [int(a['tag']['table_id'] in gold_tables) for a in ctx_lst]
            correct = max(labels[:M])
            correct_lst.append(correct)

    result = np.mean(correct_lst) * 100

    print('p@%s=%.2f' % (M, result))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
from typing import (
    Union,
    Set
)

from PyQt5.QtCore import (
    QPointF,
    QPoint,
    QRect,
    Qt
)
from PyQt5.QtGui import (
    QPainterPath,
    QKeyEvent
)
from PyQt5.QtWidgets import (
    QGraphicsItemGroup,
    QGraphicsPathItem,
    # QGraphicsRectItem,
    QGraphicsItem,
    QMenu,
    QAction,
    QGraphicsSceneMouseEvent
)
from cadnano.fileio import (
    v3encode,
    v3decode
)
from cadnano.views.sliceview.virtualhelixitem import SliceVirtualHelixItem
from cadnano.gui.palette import getPenObj
from cadnano.views.sliceview import slicestyles as styles
from .abstractslicetool import AbstractSliceTool
from cadnano.views.sliceview import (
    SliceToolManagerT,
    SliceNucleicAcidPartItemT
)
from cadnano.cntypes import (
    RectT,
    Vec3T
)

def normalizeRect(rect: RectT) -> RectT:
    """
    Args:
        rect: Description

    Returns:
        tuple of a rectangle corners
    """
    x1, y1, x2, y2 = rect
    if x1 > x2:
        # swap
        x1, x2 = x2, x1
    if y1 > y2:
        # swap
        y1, y2 = y2, y1
    return (x1, y1, x2, y2)

_SELECT_PEN_WIDTH = styles.SELECTED_PEN_WIDTH
_SELECT_COLOR = styles.SELECTED_COLOR  # "#ff0000"
_TEST_COLOR = "#00ff00"

class SelectSliceTool(AbstractSliceTool):
    """Handles SelectTool operations in the Slice view

    Attributes:
        clipboard (TYPE): Description
        group (TYPE): Description
        individual_pick (bool): Description
        is_selection_active (bool): Description
        last_rubberband_vals (tuple): Description
        part_item (TYPE): Description
        selection_set (TYPE): Description
        snap_origin_item (TYPE): Description
    """

    def __init__(self, manager: SliceToolManagerT):
        """Summary

        Args:
            manager (TYPE): Description
        """
        super(SelectSliceTool, self).__init__(manager)
        self.last_rubberband_vals = (None, None, None)
        self.selection_set = set()
        self.group = SliceSelectionGroup(self, parent=self)
        self.group.hide()
        self.is_selection_active = False
        self.individual_pick = False
        self.snap_origin_item = None
        self.clipboard = None
    # end def

    def __repr__(self) -> str:
        """
        Returns:
            tool name string
        """
        return "select_tool"  # first letter should be lowercase

    def methodPrefix(self) -> str:
        """
        Returns:
            prefix string
        """
        return "selectTool"  # first letter should be lowercase

    def isSelectionActive(self) -> bool:
        """
        Returns:
            is the selection active?
        """
        return self.is_selection_active
    # end def

    def resetSelections(self):
        """
        """
        # print("resetSelections")
        doc = self.manager.document
        doc.clearAllSelected()

    def modelClear(self):
        """Clear model selections
        """
        # print("modelClear")
        doc = self.manager.document
        doc.clearAllSelected()

    def setPartItem(self, part_item: SliceNucleicAcidPartItemT):
        """
        Args:
            part_item: Description
        """
        if part_item is not self.part_item:
            if self.sgv is not None:
                # attempt to enforce good housekeeping, not required
                try:
                    self.sgv.rubberBandChanged.disconnect(self.selectRubberband)
                except Exception:
                    # required for first call
                    pass
            if self.part_item is not None:
                # print("modelClear yikes")
                self.modelClear()
            self.part_item = part_item

            # In the event that the old part_item was deleted (and garbage
            # collected), self.group (whose parent was the old part_item) is
            # also garbage collected.  Therefore, create a new
            # SliceSelectionGroup when this is the case
            try:
                self.group.setParentItem(part_item)
            except RuntimeError:
                self.group = SliceSelectionGroup(self, parent=part_item)

            # required for whatever reason to renable QGraphicsView.RubberBandDrag
            # TODO[NF]:  Determine why self.slice_graphics_view is None when the dual-slice view is enabled
            # TODO[NF]:  Now that SVG is being set properly, can the conditional be removed?
            # TODO[NF]:  This might be better kept as an error condition logged to logger.error
            if self.sgv is not None:
                self.sgv.activateSelection(True)
                self.sgv.rubberBandChanged.connect(self.selectRubberband)
            else:
                # TODO[NF]:  Use logger.error here
                print("SGV is still none")
    # end def

    def selectRubberband(self, rect: QRect, from_pt: QPointF, to_point: QPointF):
        """
        Args:
            rect: Description
            from_pt: Description
            to_point: Description
        """
        fset = self.manager.document.filter_set
        if self.FILTER_NAME not in fset:
            return
        rbr_last, fp_last, tp_last = self.last_rubberband_vals
        if rect.isNull() and rbr_last.isValid():
            part_item = self.part_item
            part = part_item.part()

            # convert and normalize the drag rectangle
            from_pt_part_item = part_item.mapFromScene(fp_last)
            to_pt_part_item = part_item.mapFromScene(tp_last)

            # note QRectF.normalized doesn't seem to actually normalize a
            # rectangle near as I can tell no we have normaliz
            from_model_point: Vec3T =   part_item.getModelPos(from_pt_part_item)
            to_model_point: Vec3T =     part_item.getModelPos(to_pt_part_item)
            query_rect = (from_model_point[0], from_model_point[1],
                          to_model_point[0], to_model_point[1])
            query_rect = normalizeRect(query_rect)
            # print("Query rect", query_rect,
            #     query_rect[0] < query_rect[2], query_rect[1] < query_rect[3])
            res = part.getVirtualHelicesInArea(query_rect)

            doc = self.manager.document
            doc.clearAllSelected()
            doc.addVirtualHelicesToSelection(part, res)
        else:
            self.last_rubberband_vals = (rect, from_pt, to_point)
    # end def

    def getSelectionBoundingRect(self):
        """Show the bounding rect
        """
        part_item = self.part_item
        group = self.group
        # self.deselectItems()
        for id_num in self.selection_set:
            vhi = part_item.getVirtualHelixItem(id_num)
            group.addToGroup(vhi)

        self.is_selection_active = True
        if len(group.childItems()) > 0:
            group.setSelectionRect()
            # print("showing")
            group.show()
        else:
            print("Nothing in selection_set")
    # end def

    def deselectItems(self) -> bool:
        """
        Returns:
            ``True`` if selection cleared ``False`` if no selection active
        """
        # print("deselecting")
        group = self.group
        if self.snap_origin_item is not None:
            self.snap_origin_item.setSnapOrigin(False)
            self.snap_origin_item = None
        if self.is_selection_active:
            part_item = self.part_item
            for id_num in self.selection_set:
                vhi = part_item.getVirtualHelixItem(id_num)
                if vhi is not None:
                    group.removeFromGroup(vhi)
            self.selection_set.clear()
            group.clearSelectionRect()
            group.hide()
            self.is_selection_active = False
            return True
        if group is not None:
            group.clearSelectionRect()
        return False
    # end def

    def deselectSet(self, vh_set: Set[int]) -> bool:
        """
        Args:
            vh_set: Description

        Returns:
            ``False`` if selection is not active, ``True`` if active
        """
        group = self.group
        if self.snap_origin_item is not None:
            self.snap_origin_item.setSnapOrigin(False)
            self.snap_origin_item = None
        selection_set = self.selection_set
        if self.is_selection_active:
            part_item = self.part_item
            for id_num in vh_set:
                vhi = part_item.getVirtualHelixItem(id_num)
                if vhi is not None:
                    group.removeFromGroup(vhi)
                    selection_set.remove(id_num)
            group.clearSelectionRect()
            group.hide()
            if len(selection_set) > 0 and len(group.childItems()) > 0:
                group.setSelectionRect()
                group.show()
            return True
        group.clearSelectionRect()
        return False
    # end def

    def selectOrSnap(self,  part_item: SliceNucleicAcidPartItemT,
                            target_item: SliceVirtualHelixItem,
                            event: QGraphicsSceneMouseEvent):
        """
        Args:
            part_item:
            target_item: Description
            event: Description

        Deleted Parameters:
            snap_to_item (SliceVirtualHelixItem or GridEvent): Item to snap
                selection to
        """
        self.setPartItem(part_item)
        if (self.snap_origin_item is not None and event.modifiers() == Qt.AltModifier):
            self.doSnap(part_item, target_item)
            self.individual_pick = False
        else:  # just do a selection
            if event.modifiers() != Qt.ShiftModifier:
                self.modelClear()   # deselect if shift isn't held

            if isinstance(target_item, SliceVirtualHelixItem):
                # NOTE: individual_pick seems not needed.
                # it's supposed to allow for single item picking
                # self.individual_pick = True

                if self.snap_origin_item is not None:
                    self.snap_origin_item.setSnapOrigin(False)
                    self.snap_origin_item = None

                doc = self.manager.document
                part = part_item.part()
                doc.addVirtualHelicesToSelection(part, [target_item.idNum()])
    # end def

    def doSnap(self, part_item: SliceNucleicAcidPartItemT,
                    snap_to_item: SliceVirtualHelixItem):
        """
        Args:
            part_item:
            snap_to_item: Item to snap
                selection to
        """
        # print("snapping")
        origin = self.snap_origin_item.getCenterScenePos()

        # xy = part_item.mapFromScene(snap_to_item.scenePos())
        # xy2 = part_item.mapFromScene(self.snap_origin_item.scenePos())
        # print("snapping from:", xy2.x(), xy2.y())
        # print("snapped to:", xy.x(), xy.y())

        if isinstance(snap_to_item, SliceVirtualHelixItem):
            self.setVirtualHelixItem(snap_to_item)
            destination = self.findNearestPoint(part_item, origin)
        else:  # GridEvent
            destination = snap_to_item.pos()
            print("GridEvent", destination)

        origin = part_item.mapFromScene(origin)
        if destination is None:
            destination = origin
        if origin == destination:
            # snap clockwise
            destination = self.findNextPoint(part_item, origin)
        if origin is None or destination is None:
            print("o", origin, "d", destination)
        delta = destination - origin
        dx, dy = delta.x(), delta.y()
        group = self.group
        pos = group.pos() + delta
        self.group.setPos(pos)
        self.moveSelection(dx, dy, False, use_undostack=True)
        self.hideLineItem()
    # end def

    def copySelection(self):
        """Summary

        Returns:
            TYPE: Description
        """
        part_instance = self.part_item.partInstance()
        copy_dict = v3encode.encodePartList(part_instance, list(self.selection_set))
        self.clipboard = copy_dict
#        print('SliceSelectTool %s' % self.selection_set)
    # end def

    def pasteClipboard(self):
        """Summary

        Returns:
            TYPE: Description
        """
        doc = self.manager.document
#        part = self.part_item.part()
        part_instance = self.part_item.partInstance()
        offset = self.part_item.copypaste_origin_offset

        # Abort if there is no offset (e.g. if the location is invalid)
        if offset is None:
            return

        doc.undoStack().beginMacro("Paste VirtualHelices")
        new_vh_set = v3decode.importToPart(part_instance, self.clipboard, offset)
        doc.undoStack().endMacro()
        self.modelClear()
        self.clipboard = None
        # doc.addVirtualHelicesToSelection(part, new_vh_set)
        return new_vh_set
    # end def

    def moveSelection(self, dx: float, dy: float,
                            finalize: bool,
                            use_undostack: bool = True):
        """Y-axis is inverted in Qt +y === DOWN

        Args:
            dx: Description
            dy: Description
            finalize: Description
            use_undostack (bool, optional): Description
        """
        # print("moveSelection: {}, {}".format(dx, dy))
        part_item = self.part_item
        sf = part_item.scaleFactor()
        part = part_item.part()
        part.translateVirtualHelices(self.selection_set,
                                     dx / sf, -dy / sf, 0,
                                     finalize,
                                     use_undostack=use_undostack)
    # end def

    def deactivate(self):
        """Summary

        Returns:
            TYPE: Description
        """
        if self.sgv is not None:
            try:
                self.sgv.rubberBandChanged.disconnect(self.selectRubberband)
            except Exception:
                pass    # required for first call
        self.modelClear()
        if self.snap_origin_item is not None:
            self.snap_origin_item.setSnapOrigin(False)
            self.snap_origin_item = None
        AbstractSliceTool.deactivate(self)
    # end def

    def getCustomContextMenu(self, point: QPoint):
        """point

        Args:
            point: Description
        """
        if len(self.selection_set) > 0:
            sgv = self.sgv
            menu = QMenu(sgv)
            copy_act = QAction("Copy selection", sgv)
            copy_act.setStatusTip("Copy selection")
            copy_act.triggered.connect(self.copySelection)
            menu.addAction(copy_act)
            if self.clipboard and self.part_item.copypaste_origin_offset:
                copy_act = QAction("Paste", sgv)
                copy_act.setStatusTip("Paste from clipboard")
                copy_act.triggered.connect(self.pasteClipboard)
                menu.addAction(copy_act)
            menu.aboutToShow.connect(self.lockHints)
            menu.aboutToHide.connect(self.unlockHints)
            menu.exec_(sgv.mapToGlobal(point))
    # end def

    def lockHints(self):
        self.part_item.lock_hints = True
    # end def

    def unlockHints(self):
        self.part_item.lock_hints = False
    # end def
# end class

class SliceSelectionBox(QGraphicsPathItem):
    _RADIUS = styles.SLICE_HELIX_RADIUS

    def __init__(self, parent: QGraphicsItem = None):
        super(QGraphicsPathItem, self).__init__(parent)
        self._rect = None
    # end def

    def painterPath(self):
        rect = self._rect
        radius = self._RADIUS

        path = QPainterPath()
        path.addRoundedRect(rect, radius, radius)
        return path
    # end def

    def refreshPath(self):
        """Summary

        Returns:
            TYPE: Description
        """
        self.prepareGeometryChange()
        self.setPath(self.painterPath())
        self._pos0 = self.pos()
    # end def

    def setRect(self, rect):
        self._rect = rect
    # end def

class SliceSelectionGroup(QGraphicsItemGroup):
    """Summary

    Attributes:
        bounding_rect_item (TYPE): Description
        drag_last_position (TYPE): Description
        drag_start_position (TYPE): Description
        tool (TYPE): Description
    """

    def __init__(self, tool, parent: QGraphicsItem = None):
        """Summary

        Args:
            tool (TYPE): Description
            parent (None, optional): Description
        """
        super(SliceSelectionGroup, self).__init__(parent)
        self.tool = tool
        self.setFiltersChildEvents(True)
        self.setFlag(QGraphicsItem.ItemIsFocusable)  # for keyPressEvents
        self.setFlag(QGraphicsItem.ItemIsMovable)

        # self.bounding_rect_item = bri = QGraphicsRectItem(tool)
        self.bounding_rect_item = bri = SliceSelectionBox()
        bri.hide()
        bri.setPen(getPenObj(_SELECT_COLOR, _SELECT_PEN_WIDTH))

        self.setZValue(styles.ZSELECTION)

        self.drag_start_position = QPointF()
        self.drag_last_position = QPointF()
    # end def

    def setSelectionRect(self):
        """Summary

        Returns:
            TYPE: Description
        """
        bri = self.bounding_rect_item
        rect = self.childrenBoundingRect()
        point = rect.topLeft() + self.scenePos()
        bri.setPos(bri.mapFromScene(point))
        bri.setRect(bri.mapRectFromItem(self, rect))
        self.addToGroup(bri)
        bri.refreshPath()
        bri.show()
        self.setFocus(True)
    # end def

    """ reimplement boundingRect if you want to call resetGroupPos
    """

    def clearSelectionRect(self):
        """reset positions to zero to keep things in check
        """
        bri = self.bounding_rect_item
        if bri.group() == self:
            bri.hide()
            self.removeFromGroup(bri)
            self.setFocus(False)
            bri.setParentItem(self.tool)
    # end def

    def keyPressEvent(self, event: QKeyEvent):
        """event.key() seems to be capital only?

        Args:
            event (TYPE): Description
        """
        # print("press", ord('g'))
        if event.text() == 'g':
            print("hey het")
        return QGraphicsItem.keyPressEvent(self, event)
    # end def

    def mousePressEvent(self, event: QGraphicsSceneMouseEvent):
        """Handler for user mouse press.

        Args:
            event: Contains item, scene, and screen
            coordinates of the the event, and previous event.
        """
        return QGraphicsItemGroup.mousePressEvent(self, event)
    # end def

    def mouseMoveEvent(self, event):
        """because SliceSelectionGroup has the flag
        QGraphicsItem.ItemIsMovable
        we need only get the position of the item to figure
        out what to submit to the model

        Args:
            event (TYPE): Description
        """
        # 1. call this super class method first to get the item position updated
        return
        res = QGraphicsItemGroup.mouseMoveEvent(self, event)
        # watch out for bugs here?  everything seems OK for now, but
        # could be weird window switching edge cases
        if not self.tool.individual_pick and event.buttons() == Qt.LeftButton:
            new_pos = self.pos()
            delta = new_pos - self.drag_last_position
            self.drag_last_position = new_pos
            dx, dy = delta.x(), delta.y()
            self.tool.moveSelection(dx, dy, False, use_undostack=False)
        return res
    # end def

    def mouseReleaseEvent(self, event):
        """because SliceSelectionGroup has the flag
        QGraphicsItem.ItemIsMovable
        we need only get the position of the item to figure
        out what to submit to the model

        Args:
            event (TYPE): Description
        """
        MOVE_THRESHOLD = 0.01   # ignore small moves
        # print("mouse mouseReleaseEvent", self.tool.individual_pick)
        if not self.tool.individual_pick and event.button() == Qt.LeftButton:
            delta = self.pos() - self.drag_start_position
            dx, dy = delta.x(), delta.y()
            # print(abs(dx), abs(dy))
            if abs(dx) > MOVE_THRESHOLD or abs(dy) > MOVE_THRESHOLD:
                # print("finalizling", dx, dy)
                self.tool.moveSelection(dx, dy, True)
        self.tool.individual_pick = False
        return QGraphicsItemGroup.mouseReleaseEvent(self, event)
    # end def


# based on gridworld.py and tictactoe.py

import datetime
import os
import sys
import logging
from collections import deque

import numpy as np
import torch
from gym.utils import seeding
#from webdriverwrapper import Chrome
#from selenium import webdriver
from .webdriver_imitation import WebDriverImitaion
#from webdriver_selenium import SeleniumWebDriver

NUMBER_ACTIONS = 7
MAX_STEPS = 100

WIN_REWARD, POS_REWARD, NEG_REWARD = 100, 1, 0 # 100, 1, 0

def negative_reward():
    #print("-1")
    return NEG_REWARD

def positive_reward():
    #print("+1")
    return POS_REWARD

class TestDriverEnviroment:
    """This is enviroment for a test driver
    """
    def __init__(self):

        self.solved_task = False

        self.env_size = (3, 5)
        self.step_count = 0
        self.total_steps = 0
        self.last_num_steps = 0
        self.history_steps = deque()
        self.seed()
        #self.board = numpy.zeros((3, 3)).astype(int)
        # Prepare the webdriver
        self.driver = WebDriverImitaion()
        #self.driver = SeleniumWebDriver(init_url_address="https://demo1.testgold.dev/login")

        # Init the state
        self.chosen_type = None     # 'clickables', 'selectables', 'enterables'
        self.chosen_number = None
        self.reset()

        self.possible_actions = [
            #"CLICK-N": "Click the N-th clickable element",
            #"ENTER-RND": "Enter random text",
            #"OPEN":     "Open the given website",
            (0, "NEXT",     "Go to the next active element"),
            (1, "CLICK",    "Click on the current element"),
            (2, "CHOOSE_FIRST_CLICK",  "Choose the firse clickable element"),
            (3, "ENTER",    "Enter DATA in the current element"),
            (4, "CHOOSE_FIRST_ENTER",  "Choose the firse enterable element"),
            (5, "SELECT",    "Select the current element"),
            (6, "CHOOSE_FIRST_SELECT", "Choose the firse selectable element"),
            #(7, "HIT",      "Hit the current element"),
            #(8, "VERIFY",   "Verify the current URL"),
            #(9, "CLOSE",    "Close the current page"),
            #(10, "WAIT",     "Wait 1 sec"),
        ]
        self.possible_actions = self.possible_actions[:NUMBER_ACTIONS]
        self.cmd_to_element_type = {
            "CHOOSE_FIRST_CLICK": "clickables",
            "CHOOSE_FIRST_SELECT": "selectables",
            "CHOOSE_FIRST_ENTER": "enterables",
            "CLICK": "clickables",
            "SELECT": "selectables",
            "ENTER": "enterables"
        }
        self.action_number_to_cmd = {i: x[1] for i, x in enumerate(self.possible_actions)}
        self.action_number_to_description = {i: x[2] for i, x in enumerate(self.possible_actions)}
        self.wins = 0
        self.losses = 0

    def seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    #def to_play(self):
    #    return 0 if self.player == 1 else 1

    def reset(self):
        #self.board = numpy.zeros((3, 3)).astype(int)
        #self.state = [0]
        self.last_num_steps = self.step_count
        self.step_count = 0
        self.driver.reset()
        self.prv_state = None
        self.step_history = {'actions': [], 'commands': []}

        return self.get_observation()

    def step(self, action):
        """
        action (int or str)
        """
        self.step_count += 1
        self.total_steps += 1

        # Call the webdriver and perform the action
        if type(action) is str:
            cmd = action
        else:
            action = int(action)
            cmd = self.action_number_to_cmd[action]
            #print("action type:", type(action))
            #raise Exception("Wrong the action type")

        print("wins={}, cmd={}".format(self.wins, cmd))
        #print(cmd)

        #print("{}: wins={} (in {}), obs={} cmd={}".format(
        #    self.step_count, self.wins, self.last_num_steps, self.get_observation()[:15], cmd))

        self.step_history['actions'].append(action)

        #cmd = self.action_number_to_cmd[action]
        reward = 0
        site_elements = self.driver.get_site_elements()
        current_element = None

        if cmd == "WAIT":
            pass

        elif cmd in {"CHOOSE_FIRST_CLICK", "CHOOSE_FIRST_SELECT", "CHOOSE_FIRST_ENTER"}:
            self.chosen_type = self.cmd_to_element_type[cmd]
            if len(site_elements[self.chosen_type]) > 0:
                #current_element = site_elements[self.chosen_type][0]
                self.chosen_number = 0

                self.step_history['commands'].append((cmd,))

            else:
                reward = negative_reward()

        elif cmd == "NEXT":
            if self.chosen_number is None:
                reward = negative_reward()
            elif self.chosen_type:
                if len(site_elements[self.chosen_type]) > self.chosen_number + 1:
                    self.chosen_number += 1
                    print("self.chosen_number:", self.chosen_number)

                    self.step_history['commands'].append((cmd,))

                    #current_element = site_elements[self.chosen_type][self.chosen_number]
                else:
                    reward = negative_reward()
            else:
                reward = negative_reward()

        elif cmd in {"CLICK", "ENTER", "SELECT"}:
            if self.chosen_number is None:
                reward = negative_reward()
            elif self.chosen_type != self.cmd_to_element_type[cmd]:
                reward = negative_reward()
            elif (self.chosen_type and self.chosen_number < len(site_elements[self.chosen_type])):
                #reward = positive_reward()
                current_element = site_elements[self.chosen_type][self.chosen_number]
                if current_element is None: # perhaps, the element has been already used
                    reward = negative_reward()  # prevent clicking the same element twice
                else:
                    reward = positive_reward()

                    #print("current_element:", current_element)

                    element_number = self.chosen_number
                    #element_number = current_element # wrong!

                    if cmd == "CLICK":
                        self.driver.action_on_element("CLICK", element_number)
                        #self.driver.click(current_element)
                    elif cmd == "ENTER":
                        self.driver.action_on_element("ENTER", element_number, data="Hello world")
                        #self.driver.enter(current_element, data="Hello world")
                    elif cmd == "SELECT":
                        pass
                    # it works only if I put current_element instead of self.chosen_number

                    self.step_history['commands'].append((cmd, element_number))

                    if self.solved_task:
                        print("{} {}".format(cmd, self.chosen_number))

            else:
                reward = negative_reward()

        done = self.have_winner() or len(self.legal_actions()) == 0

        #reward = 1 if self.have_winner() else 0
        if self.have_winner():
            reward = WIN_REWARD * (1 - 0.9*(self.step_count / MAX_STEPS))
            self.wins += 1

            self.history_steps.append(self.step_count)
            if len(self.history_steps) > 100:
                self.history_steps.popleft()
            avg_steps = np.mean(self.history_steps)

            print("{}-th win in {} steps [{:.1f}]; reward={:.4f}".format(
                self.wins, self.step_count, avg_steps, reward))
            self.last_num_steps = self.step_count
            self.step_count = 0

            if avg_steps < 19.0:
                self.solved_task = True

            print("actions:", self.step_history['actions'])
            print("commands:", self.step_history['commands'])

        return self.get_observation_float(), reward, done, {}

    def get_observation(self):
        # It should return the current state as a numpy array of the float32 type
        # i.e. whole necessary information from the current webpage
        # including the list of active elements and so on.
        # probably, some additional information.
        # It will be fed to neural network.

        #board_player1 = numpy.where(self.board == 1, 1.0, 0.0)
        #board_player2 = numpy.where(self.board == -1, 1.0, 0.0)
        #board_to_play = numpy.full((3, 3), self.player).astype(float)
        #return numpy.array([board_player1, board_player2, board_to_play])

        #site_elements = {'clickables': ['Sign', 'Currency', 'Skip'], 'selectables': [], 'enterables': ['Your email']}
        site_elements = self.driver.get_site_elements()
        de_type = {0: 'clickables', 1: 'selectables', 2: 'enterables'}
        lengths = [len(site_elements[k]) for k in site_elements]

        (hight, width) = self.env_size
        # env_state shows which elements have been touched
        env_state = [[1 if i<lengths[j] and site_elements[de_type[j]][i] else 0 for i in range(width)] for j in range(len(lengths))]
        env_state = np.array(env_state, dtype=np.int32)

        # int_state shows which element is chosen right now
        int_state = np.zeros((hight, width), dtype=np.int32)
        de_type_to_number = {y:x for x,y in de_type.items()}
        if self.chosen_type is not None and self.chosen_number is not None:
            de_type_number = de_type_to_number.get(self.chosen_type)
            int_state[de_type_number, self.chosen_number] = 1

        if self.prv_state is None:
            self.prv_state = env_state

        #state = np.vstack([env_state, self.prv_state, int_state])

        state = np.vstack([env_state, int_state])
        state = np.expand_dims(state, axis=0) # 3-dim. array is required

        #print(state)

        #state = state.flatten()
        self.prv_state = env_state

        return state

    def get_observation_float(self):
        return np.array(self.get_observation(), dtype=np.float32)

    def legal_actions(self):
        legal = list(range(len(self.possible_actions)))
        return legal

    def have_winner(self):
        #observation = self.get_observation()
        #sum_obs = np.sum(observation[:self.env_size[0]*self.env_size[1]])
        #return True if sum_obs < 0.01 else False # if all_active_elements_have_been_clicked
        return self.driver.is_target_achieved()

    def render(self):
        print("Display the game observation")
        print(self.get_observation())

    def obs_size(self):
        return 3 * self.env_size[0] * self.env_size[1]

    def number_of_actions(self):
        return NUMBER_ACTIONS

if __name__ == "__main__":

    #im = Webdriver_imitation()

    env = TestDriverEnviroment()
    print(env.get_observation())
    print(env.obs_size())
    print(env.legal_actions())
    env.step("CHOOSE_FIRST_CLICK")
    env.step("NEXT")
    env.step("CLICK")
    env.step("CLICK")
    print(env.get_observation())


from easydict import EasyDict as edict

__C = edict()
__C.DATA = edict()
cfg = __C

# data set config
__C.DATA.DATASET = "Grocery"
__C.DATA.MAP_FILE_PATH = "../../DataSets/Grocery"
__C.DATA.CLASS_MAP_FILE = "class_map.txt"
__C.DATA.TRAIN_MAP_FILE = "train_img_file.txt"
__C.DATA.TRAIN_ROI_FILE = "train_roi_file.txt"
__C.DATA.TEST_MAP_FILE = "test_img_file.txt"
__C.DATA.TEST_ROI_FILE = "test_roi_file.txt"
__C.DATA.NUM_TRAIN_IMAGES = 20
__C.DATA.NUM_TEST_IMAGES = 5
__C.DATA.PROPOSAL_LAYER_SCALES = [4, 8, 12]

# overwriting proposal parameters for Fast R-CNN
# minimum relative width/height of an ROI
__C.roi_min_side_rel = 0.04
# maximum relative width/height of an ROI
__C.roi_max_side_rel = 0.4
# minimum relative area of an ROI
__C.roi_min_area_rel = 2 * __C.roi_min_side_rel * __C.roi_min_side_rel
# maximum relative area of an ROI
__C.roi_max_area_rel = 0.33 * __C.roi_max_side_rel * __C.roi_max_side_rel
# maximum aspect ratio of an ROI vertically and horizontally
__C.roi_max_aspect_ratio = 4.0

# For this data set use the following lr factor for Fast R-CNN:
# __C.CNTK.LR_FACTOR = 10.0


from typing import Any
from collections import defaultdict

overlap_1 = defaultdict(int)
overlap_2 = defaultdict(int)

def solvePartOneTwo(x1: int, y1: int, dx: int, dy: int) -> None:
    for i in range(1 + max(abs(dx), abs(dy))):
        x = x1 + (1 if dx > 0 else (-1 if dx < 0 else 0)) * i
        y = y1 + (1 if dy > 0 else (-1 if dy < 0 else 0)) * i
        if dx == 0 or dy == 0:
            overlap_1[(x, y)] += 1
        overlap_2[(x, y)] += 1

def parseFileLine(line: Any) -> None:
    x1, y1, _, x2, y2 = line.replace(',', ' ').split()
    x1, x2 = int(x1), int(x2)
    y1, y2 = int(y1), int(y2)

    dx, dy = x2 - x1, y2 - y1
    solvePartOneTwo(x1, y1, dx, dy)

def partOne() -> int:
    print("Result <PartOne>: ", len([i for i in overlap_1 if overlap_1[i] > 1]))

def partTwo() -> int:
    print("Result <PartTwo>: ", len([i for i in overlap_2 if overlap_2[i] > 1]))

def main() -> None:
    for line in open("day_5/input.txt"):
        parseFileLine(line)
    partOne()
    partTwo()

if __name__ == "__main__":
    main()


"""
Cubic hermite splines in 1d, 2d, and 3d

Related to FYS-4096 Computational Physics
exercise 3 assignments.

By Ilkka Kylanpaa on January 2019
"""

import numpy as np
import matplotlib.pyplot as plt

def p1(t):
    return (1.+2.*t)*(t-1.)**2
def p2(t):
    return t**2*(3.-2.*t)
def q1(t):
    return t*(t-1.)**2
def q2(t):
    return t**2*(t-1.)

def init_1d_spline(x,f,h):
    # now using complete boundary conditions
    # - natural boundary conditions commented
    a=np.zeros((len(x),))
    b=np.zeros((len(x),))
    c=np.zeros((len(x),))
    d=np.zeros((len(x),))
    fx=np.zeros((len(x),))

    # a[0]=1.0 # not needed
    b[0]=1.0

    # natural boundary conditions
    #c[0]=0.5
    #d[0]=1.5*(f[1]-f[0])/(x[1]-x[0])

    # complete boundary conditions
    c[0]=0.0
    d[0]=(f[1]-f[0])/(x[1]-x[0])

    for i in range(1,len(x)-1):
        d[i]=6.0*(h[i]/h[i-1]-h[i-1]/h[i])*f[i]-6.0*h[i]/h[i-1]*f[i-1]+6.0*h[i-1]/h[i]*f[i+1]
        a[i]=2.0*h[i]
        b[i]=4.0*(h[i]+h[i-1])
        c[i]=2.0*h[i-1]
    #end for

    b[-1]=1.0
    #c[-1]=1.0 # not needed

    # natural boundary conditions
    #a[-1]=0.5
    #d[-1]=1.5*(f[-1]-f[-2])/(x[-1]-x[-2])

    # complete boundary conditions
    a[-1]=0.0
    d[-1]=(f[-1]-f[-2])/(x[-1]-x[-2])

    # solve tridiagonal eq. A*f=d
    c[0]=c[0]/b[0]
    d[0]=d[0]/b[0]
    for i in range(1,len(x)-1):
        temp=b[i]-c[i-1]*a[i]
        c[i]=c[i]/temp
        d[i]=(d[i]-d[i-1]*a[i])/temp
    #end for

    fx[-1]=d[-1]
    for i in range(len(x)-2,-1,-1):
        fx[i]=d[i]-c[i]*fx[i+1]
    #end for

    return fx

"""
Add smoothing functions

def smooth1d(x,f,factor=3):
    ...
    ...
    return ...

def smooth2d(x,y,f,factor=3):
    ...
    ...
    return ...

def smooth3d(x,y,z,f,factor=3):
    ...
    ...
    ...
    return ...
"""

class spline:

    def __init__(self,*args,**kwargs):
        self.dims=kwargs['dims']
        if (self.dims==1):
            self.x=kwargs['x']
            self.f=kwargs['f']
            self.hx=np.diff(self.x)
            self.fx=init_1d_spline(self.x,self.f,self.hx)
        elif (self.dims==2):
            self.x=kwargs['x']
            self.y=kwargs['y']
            self.f=kwargs['f']
            self.hx=np.diff(self.x)
            self.hy=np.diff(self.y)
            self.fx=np.zeros(np.shape(self.f))
            self.fy=np.zeros(np.shape(self.f))
            self.fxy=np.zeros(np.shape(self.f))
            for i in range(max([len(self.x),len(self.y)])):
                if (i<len(self.y)):
                    self.fx[:,i]=init_1d_spline(self.x,self.f[:,i],self.hx)
                if (i<len(self.x)):
                    self.fy[i,:]=init_1d_spline(self.y,self.f[i,:],self.hy)
            #end for
            for i in range(len(self.y)):
                self.fxy[:,i]=init_1d_spline(self.x,self.fy[:,i],self.hx)
            #end for
        elif (self.dims==3):
            self.x=kwargs['x']
            self.y=kwargs['y']
            self.z=kwargs['z']
            self.f=kwargs['f']
            self.hx=np.diff(self.x)
            self.hy=np.diff(self.y)
            self.hz=np.diff(self.z)
            self.fx=np.zeros(np.shape(self.f))
            self.fy=np.zeros(np.shape(self.f))
            self.fz=np.zeros(np.shape(self.f))
            self.fxy=np.zeros(np.shape(self.f))
            self.fxz=np.zeros(np.shape(self.f))
            self.fyz=np.zeros(np.shape(self.f))
            self.fxyz=np.zeros(np.shape(self.f))
            for i in range(max([len(self.x),len(self.y),len(self.z)])):
                for j in range(max([len(self.x),len(self.y),len(self.z)])):
                    if (i<len(self.y) and j<len(self.z)):
                        self.fx[:,i,j]=init_1d_spline(self.x,self.f[:,i,j],self.hx)
                    if (i<len(self.x) and j<len(self.z)):
                        self.fy[i,:,j]=init_1d_spline(self.y,self.f[i,:,j],self.hy)
                    if (i<len(self.x) and j<len(self.y)):
                        self.fz[i,j,:]=init_1d_spline(self.z,self.f[i,j,:],self.hz)
            #end for
            for i in range(max([len(self.x),len(self.y),len(self.z)])):
                for j in range(max([len(self.x),len(self.y),len(self.z)])):
                    if (i<len(self.y) and j<len(self.z)):
                        self.fxy[:,i,j]=init_1d_spline(self.x,self.fy[:,i,j],self.hx)
                    if (i<len(self.y) and j<len(self.z)):
                        self.fxz[:,i,j]=init_1d_spline(self.x,self.fz[:,i,j],self.hx)
                    if (i<len(self.x) and j<len(self.z)):
                        self.fyz[i,:,j]=init_1d_spline(self.y,self.fz[i,:,j],self.hy)
            #end for
            for i in range(len(self.y)):
                for j in range(len(self.z)):
                    self.fxyz[:,i,j]=init_1d_spline(self.x,self.fyz[:,i,j],self.hx)
            #end for
        else:
            print('Either dims is missing or specific dims is not available')
        #end if

    def eval1d(self,x):
        if np.isscalar(x):
            x=np.array([x])
        N=len(self.x)-1
        f=np.zeros((len(x),))
        ii=0
        for val in x:
            i=np.floor(np.where(self.x<=val)[0][-1]).astype(int)
            if i==N:
                f[ii]=self.f[i]
            else:
                t=(val-self.x[i])/self.hx[i]
                f[ii]=self.f[i]*p1(t)+self.f[i+1]*p2(t)+self.hx[i]*(self.fx[i]*q1(t)+self.fx[i+1]*q2(t))
            ii+=1

        return f
    #end eval1d

    def eval2d(self,x,y):
        if np.isscalar(x):
            x=np.array([x])
        if np.isscalar(y):
            y=np.array([y])
        Nx=len(self.x)-1
        Ny=len(self.y)-1
        f=np.zeros((len(x),len(y)))
        A=np.zeros((4,4))
        ii=0
        for valx in x:
            i=np.floor(np.where(self.x<=valx)[0][-1]).astype(int)
            if (i==Nx):
                i-=1
            jj=0
            for valy in y:
                j=np.floor(np.where(self.y<=valy)[0][-1]).astype(int)
                if (j==Ny):
                    j-=1
                u = (valx-self.x[i])/self.hx[i]
                v = (valy-self.y[j])/self.hy[j]
                pu = np.array([p1(u),p2(u),self.hx[i]*q1(u),self.hx[i]*q2(u)])
                pv = np.array([p1(v),p2(v),self.hy[j]*q1(v),self.hy[j]*q2(v)])
                A[0,:]=np.array([self.f[i,j],self.f[i,j+1],self.fy[i,j],self.fy[i,j+1]])
                A[1,:]=np.array([self.f[i+1,j],self.f[i+1,j+1],self.fy[i+1,j],self.fy[i+1,j+1]])
                A[2,:]=np.array([self.fx[i,j],self.fx[i,j+1],self.fxy[i,j],self.fxy[i,j+1]])
                A[3,:]=np.array([self.fx[i+1,j],self.fx[i+1,j+1],self.fxy[i+1,j],self.fxy[i+1,j+1]])

                f[ii,jj]=np.dot(pu,np.dot(A,pv))
                jj+=1
            ii+=1
        return f
    #end eval2d

    def eval3d(self,x,y,z):
        if np.isscalar(x):
            x=np.array([x])
        if np.isscalar(y):
            y=np.array([y])
        if np.isscalar(z):
            z=np.array([z])
        Nx=len(self.x)-1
        Ny=len(self.y)-1
        Nz=len(self.z)-1
        f=np.zeros((len(x),len(y),len(z)))
        A=np.zeros((4,4))
        B=np.zeros((4,4))
        ii=0
        for valx in x:
            i=np.floor(np.where(self.x<=valx)[0][-1]).astype(int)
            if (i==Nx):
                i-=1
            jj=0
            for valy in y:
                j=np.floor(np.where(self.y<=valy)[0][-1]).astype(int)
                if (j==Ny):
                    j-=1
                kk=0
                for valz in z:
                    k=np.floor(np.where(self.z<=valz)[0][-1]).astype(int)
                    if (k==Nz):
                        k-=1
                    u = (valx-self.x[i])/self.hx[i]
                    v = (valy-self.y[j])/self.hy[j]
                    t = (valz-self.z[k])/self.hz[k]
                    pu = np.array([p1(u),p2(u),self.hx[i]*q1(u),self.hx[i]*q2(u)])
                    pv = np.array([p1(v),p2(v),self.hy[j]*q1(v),self.hy[j]*q2(v)])
                    pt = np.array([p1(t),p2(t),self.hz[k]*q1(t),self.hz[k]*q2(t)])
                    B[0,:]=np.array([self.f[i,j,k],self.f[i,j,k+1],self.fz[i,j,k],self.fz[i,j,k+1]])
                    B[1,:]=np.array([self.f[i+1,j,k],self.f[i+1,j,k+1],self.fz[i+1,j,k],self.fz[i+1,j,k+1]])
                    B[2,:]=np.array([self.fx[i,j,k],self.fx[i,j,k+1],self.fxz[i,j,k],self.fxz[i,j,k+1]])
                    B[3,:]=np.array([self.fx[i+1,j,k],self.fx[i+1,j,k+1],self.fxz[i+1,j,k],self.fxz[i+1,j,k+1]])
                    A[:,0]=np.dot(B,pt)
                    B[0,:]=np.array([self.f[i,j+1,k],self.f[i,j+1,k+1],self.fz[i,j+1,k],self.fz[i,j+1,k+1]])
                    B[1,:]=np.array([self.f[i+1,j+1,k],self.f[i+1,j+1,k+1],self.fz[i+1,j+1,k],self.fz[i+1,j+1,k+1]])
                    B[2,:]=np.array([self.fx[i,j+1,k],self.fx[i,j+1,k+1],self.fxz[i,j+1,k],self.fxz[i,j+1,k+1]])
                    B[3,:]=np.array([self.fx[i+1,j+1,k],self.fx[i+1,j+1,k+1],self.fxz[i+1,j+1,k],self.fxz[i+1,j+1,k+1]])
                    A[:,1]=np.dot(B,pt)

                    B[0,:]=np.array([self.fy[i,j,k],self.fy[i,j,k+1],self.fyz[i,j,k],self.fyz[i,j,k+1]])
                    B[1,:]=np.array([self.fy[i+1,j,k],self.fy[i+1,j,k+1],self.fyz[i+1,j,k],self.fyz[i+1,j,k+1]])
                    B[2,:]=np.array([self.fxy[i,j,k],self.fxy[i,j,k+1],self.fxyz[i,j,k],self.fxyz[i,j,k+1]])
                    B[3,:]=np.array([self.fxy[i+1,j,k],self.fxy[i+1,j,k+1],self.fxyz[i+1,j,k],self.fxyz[i+1,j,k+1]])
                    A[:,2]=np.dot(B,pt)
                    B[0,:]=np.array([self.fy[i,j+1,k],self.fy[i,j+1,k+1],self.fyz[i,j+1,k],self.fyz[i,j+1,k+1]])
                    B[1,:]=np.array([self.fy[i+1,j+1,k],self.fy[i+1,j+1,k+1],self.fyz[i+1,j+1,k],self.fyz[i+1,j+1,k+1]])
                    B[2,:]=np.array([self.fxy[i,j+1,k],self.fxy[i,j+1,k+1],self.fxyz[i,j+1,k],self.fxyz[i,j+1,k+1]])
                    B[3,:]=np.array([self.fxy[i+1,j+1,k],self.fxy[i+1,j+1,k+1],self.fxyz[i+1,j+1,k],self.fxyz[i+1,j+1,k+1]])
                    A[:,3]=np.dot(B,pt)

                    f[ii,jj,kk]=np.dot(pu,np.dot(A,pv))
                    kk+=1
                jj+=1
            ii+=1
        return f
    #end eval3d
#end class spline

def main():

    # 1d example
    x=np.linspace(0.,2.*np.pi,20)
    y=np.sin(x)
    spl1d=spline(x=x,f=y,dims=1)
    xx=np.linspace(0.,2.*np.pi,100)
    plt.figure()
    # function
    plt.plot(xx,spl1d.eval1d(xx))
    plt.plot(x,y,'o',xx,np.sin(xx),'r--')
    plt.title('function')

    # 2d example
    fig=plt.figure()
    ax=fig.add_subplot(121)
    x=np.linspace(0.0,3.0,11)
    y=np.linspace(0.0,3.0,11)
    X,Y = np.meshgrid(x,y)
    Z = (X+Y)*np.exp(-1.0*(X*X+Y*Y))
    ax.pcolor(X,Y,Z)
    ax.set_title('original')

    spl2d=spline(x=x,y=y,f=Z,dims=2)
    #figure()
    ax2=fig.add_subplot(122)
    x=np.linspace(0.0,3.0,51)
    y=np.linspace(0.0,3.0,51)
    X,Y = np.meshgrid(x,y)
    Z = spl2d.eval2d(x,y)
    ax2.pcolor(X,Y,Z)
    ax2.set_title('interpolated')

    # 3d example
    x=np.linspace(0.0,3.0,10)
    y=np.linspace(0.0,3.0,10)
    z=np.linspace(0.0,3.0,10)
    X,Y,Z = np.meshgrid(x,y,z)
    F = (X+Y+Z)*np.exp(-1.0*(X*X+Y*Y+Z*Z))
    X,Y= np.meshgrid(x,y)
    fig3d=plt.figure()
    ax=fig3d.add_subplot(121)
    ax.pcolor(X,Y,F[...,int(len(z)/2)])
    ax.set_title('original')

    spl3d=spline(x=x,y=y,z=z,f=F,dims=3)
    x=np.linspace(0.0,3.0,50)
    y=np.linspace(0.0,3.0,50)
    z=np.linspace(0.0,3.0,50)
    X,Y= np.meshgrid(x,y)
    ax2=fig3d.add_subplot(122)
    F=spl3d.eval3d(x,y,z)
    ax2.pcolor(X,Y,F[...,int(len(z)/2)])
    ax2.set_title('interpolated')

    plt.show()
#end main

if __name__=="__main__":
    main()