import cv2
import os
import numpy as np
import pickle as pkl
import mediapipe as mp
from utils.mediapipe_utils import mediapipe_detection

def landmark_to_array(mp_landmark_list):
    """Inputs a list to return a np array of size (nb_keypoints x 3)"""
    keypoints = []
    for landmark in mp_landmark_list.landmark:
        keypoints.append([landmark.x, landmark.y, landmark.z])
    return np.nan_to_num(keypoints)

def extract_landmarks(results):
    """Extract the results of pose and convert into np array
    if a pose doesn't appear, return an array of zeros
    :param results: mediapipe object that contains the 3D position of all keypoints
    :return: Two np arrays of size (1, 33 * 3) = (1, nb_keypoints * nb_coordinates) corresponding to both hands
    """
    pose = landmark_to_array(results.pose_landmarks).reshape(99).tolist()
    return pose

def save_landmarks_from_video(video_name):

    landmark_list = {"pose": []}
    yoga_name = video_name.split("-")[0]
    # Set the Video stream
    cap = cv2.VideoCapture(
        os.path.join("data", "videos", yoga_name,video_name + ".mp4")
    )
    with mp.solutions.holistic.Holistic(
        min_detection_confidence=0.5, min_tracking_confidence=0.5
    ) as holistic:
        # print(cap.isOpened())#This is outputting false
        while cap.isOpened():
            ret, frame = cap.read()
            if ret:
                # Make detections
                image, results = mediapipe_detection(frame, holistic)
                # Store results
                pose = extract_landmarks(results)
                landmark_list["pose"].append(pose)
            else:
                break

        cap.release()
    # Create the folder of the pose if it doesn't exists
    path = os.path.join("data", "dataset", yoga_name)
    if not os.path.exists(path):
        os.mkdir(path)

    # Create the folder of the video data if it doesn't exists
    data_path = os.path.join(path, video_name)
    if not os.path.exists(data_path):
        os.mkdir(data_path)

    # Saving the landmark_list in the correct folder
    save_array(
        landmark_list["pose"], os.path.join(data_path, f"pose_{video_name}.pickle")
    )

def save_array(arr, path):
    file = open(path, "wb")
    pkl.dump(arr, file)
    file.close()

def load_array(path):
    file = open(path, "rb")
    arr = pkl.load(file)
    file.close()
    return np.array(arr)


import os
from PIL import Image

# Function to rename and convert .jpg files to .png
def rename_jpg_to_png(folder_path, folder_name, parent_directory):
    i = 0
    for filename in os.listdir(folder_path):
        if filename.endswith('.jpg'):

            png_image = Image.open(parent_directory + "\\" + folder_name + "\\" + filename)
            jpeg_image = png_image.convert("RGB")
            jpeg_image.save(parent_directory + "\\" + folder_name + "\\" + folder_name + "_" + str(i) + ".jpg", "JPEG")
            i += 1
            os.remove(parent_directory + "\\" + folder_name + "\\" + filename)

# List of folder names
folder_names = ['Ahri', 'Annie', 'Corki', 'Garen', 'Miss_fortune', 'Jinx', 'Nidalee', 'Poppy', 'Yasuo', 'Urgot']

# Specify the parent directory where the folders are located
parent_directory = "bloc 4 DL\Who's that Pkmn\Dataset champion"

# Iterate through each folder and rename .jpg files to .png
for folder_name in folder_names:
    folder_path = os.path.join(parent_directory, folder_name)

    # Check if the folder exists
    if os.path.exists(folder_path):
        rename_jpg_to_png(folder_path, folder_name, parent_directory)
    else:
        print(f"Folder not found: {folder_path}")


from functools import wraps
from flask import request
from hashlib import sha512
from Crypto.PublicKey import RSA
from model.error import Forbidden
from base64 import urlsafe_b64decode
from time import time
import json
import requests
import os

def require_login(func):
    @wraps(func)
    def decorated(*args, **kwargs):
        if verify_token():
            return func(*args, **kwargs)
        else:
            raise Forbidden("Invalid access token.")

    return decorated

def verify_token(token = None):
    access_token = retrive_token() if token is None else token
    if access_token is None:
        return False

    # get the hash value from header and payload
    header, payload, signature = access_token.split('.')
    header_payload = f"{header}.{payload}".encode("utf-8")
    hash = int.from_bytes(sha512(header_payload).digest(), byteorder='big')

    # get hash value from signature and public key
    header, payload, signature = decode_token(access_token)

    # get public key from authenticator well know endpoint
    resp = requests.get(f'http://{os.environ.get("AUTHENTICATOR_ENDPOINT")}:{os.environ.get("AUTHENTICATOR_PORT")}/auth/.well-known/configuration')
    jwks = resp.json()
    jwk = None
    for data in jwks:
        if header["kid"] == data["kid"]:
            jwk = data
            break
    # kid not found
    if jwk is None:
        return False
    e = int.from_bytes(urlsafe_b64decode(fill_b64_padding(jwk["e"])), 'big')
    n = int.from_bytes(urlsafe_b64decode(fill_b64_padding(jwk["n"])), 'big')
    hash_from_signature = pow(signature, e, n)
    # verify signature
    if hash != hash_from_signature:
        return False

    # expiration time verify
    if int(time()) > payload["exp"]:
        return False

    # valid token
    return True

def decode_token(token = None):
    access_token = retrive_token() if token is None else token
    header, payload, signature = access_token.split('.')
    header_decode = json.loads(urlsafe_b64decode(fill_b64_padding(header)))
    payload_decode = json.loads(urlsafe_b64decode(fill_b64_padding(payload)))
    signature_decode = int.from_bytes(urlsafe_b64decode(fill_b64_padding(signature)), 'big')

    return (header_decode, payload_decode, signature_decode)

def fill_b64_padding(b64msg):
    missing_padding = 4 - len(b64msg) % 4
    b64msg += '=' * missing_padding
    return b64msg

def retrive_token() -> str:
    # check field in header
    header = request.headers
    if "Authorization" not in header:
        return None

    # check schema
    if not header["Authorization"].lower().startswith("bearer "):
        return None
    else:
        _, token = header["Authorization"].split()
        return token


import numpy as np
import pandas as pd
import random
import tqdm
import os
import sys

def main():

	base_path = sys.argv[1]
	df = pd.read_csv(os.path.join(base_path, "trainLabels.csv"))
	df.loc[df["level"] > 0, "level"] = 1
	data = df.values
	N, _ = data.shape

	print(f"Creating split for kaggle dataset with {N} images.")

	train_ratio, val_ratio = 0.85, 0.15

	img_ids = data[:,0].tolist()
	person_ids = list(set(i.split('_')[0] for i in img_ids))
	person_ids_to_ind = {}
	for i, img_id in enumerate(img_ids):
		p_id = img_id.split('_')[0]
		if p_id in person_ids_to_ind: person_ids_to_ind[p_id].append(i)
		else: person_ids_to_ind[p_id] = [i]

	z0 = np.mean(data[:,1])
	min_score = np.inf
	best_train_ind = None

	for _ in tqdm.tqdm(range(2000)):

		train_person_ids = random.sample(person_ids, int(train_ratio*len(person_ids)))
		train_split_ind = [j for i in train_person_ids for j in person_ids_to_ind[i]]

		s = abs(z0 - np.mean(data[train_split_ind, 1]))

		if s < min_score:
			min_score = s
			best_train_ind = train_split_ind
			s = f"min score: {min_score:.7f}"
			tqdm.tqdm.write(s)

	best_train_ind = sorted(best_train_ind)
	best_val_ind = sorted(list(set(range(N)) - set(best_train_ind)))

	train_df = df.iloc[best_train_ind]
	val_df   = df.iloc[best_val_ind]

	train_df.to_csv(os.path.join(base_path, "train_ann.csv"), index=False)
	val_df.to_csv(os.path.join(base_path, "val_ann.csv"), index=False)

	print("Done!")

if __name__ == "__main__":
	main()


import os
import jsonpickle as json
import importlib
from . import log_manager

def get_files_in_folder(path):
    try:
        return os.listdir(path)
    except:
        log_manager.warning("Could not get a list of dirs")
        return []

def create_folder(path):
    if os.path.exists(path):
        log_manager.log("Directory already exists")
        return

    try:
        os.makedirs(path)
    except:
        log_manager.warning("Could not create dir")
    else:
        log_manager.log("Created folder structure: ", path)

def delete_folder(path):
    try:
        os.rmdir(path)
    except:
        log_manager.warning("Could not delete dir")
    else:
        log_manager.log("Deleted folder structure: ", path)

def encode_to_json(obj, forWeb=False):
    try:
        if(forWeb):
            return json.encode(obj, unpicklable=False)
        else:
            return json.encode(obj)
    except:
        log_manager.warning("Couldn't convert to JSON")
        return ""

def decode_json(text):
    try:
        return json.decode(text)
    except:
        log_manager.warning("Couldn't convert to python object")
        return None

def exists(path, name):
    s = os.path.join(path, name)
    return os.path.exists(s)

#open file and return text
def open_file(path, name):
    s = os.path.join(path, name)
    try:
        file = open(s, encoding='utf-8')
        text = file.read()
        file.close()
        log_manager.log("Opened file: ", s)
        return text
    except:
        log_manager.warning("Couldn't open file")
        return ""

#save text to the specified path, creates new if it doesn't exist
def write_file(path, name, text):
    s = os.path.join(path, name)
    try:
        file = open(s, mode='w', encoding='utf-8')
        file.write(text)
        file.flush()
        file.close()
        log_manager.log("written to/ created: ", s)
        return True
    except Exception as e:
        log_manager.warning(str(e))
        return False

def delete_file(path, name):
    try:
        s = os.path.join(path, name)
        if os.path.exists(s):
            os.remove(s)
            log_manager.log("deleted: ", s)
            return True
    except:
        log_manager.warning("Couldn't delete file")
        return False

"""
Imports a module and class on runtime
This function works only if the filname IS EXACTLY THE SAME
as the classname, or give a specific classname.
Returns an attribute that can be instantiated as a class

param modulename: name of the module
param classname: OPTIONAL, name of the class to import
param pkgname: name of the package the module is in
"""
def dynamic_import_module(modulename, *, classname = "", pkgname=""):
    try:
        path = "."+pkgname+"."+modulename
        module = importlib.import_module(path , package="cosplay_manager_server")
        dynClass = None
        if classname == "":
            dynClass = getattr(module, modulename)
        else:
            dynClass = getattr(module, classname)
        return dynClass

    except Exception as e:
        log_manager.error(str(e))
        return None


from abc import ABC, abstractmethod
from enum import Enum
from typing import Union
from datetime import datetime, timedelta

EPOCH = datetime(1970, 1, 1)

class ModerationDecision(Enum):
    ABSTAIN = -1
    DELETE_MSG = 0
    TIMEOUT_USER = 1

class Moderator(ABC):
    message: str
    decision: ModerationDecision

    def __init__(self, message: str, decision: ModerationDecision, timeout_duration: Union[None, int]):
        self.message = message
        self.timeout_duration = timeout_duration
        self.decision = decision

    @abstractmethod
    def get_name(self) -> str:
        pass

    @abstractmethod
    def vote(self, msg: str, author: str) -> ModerationDecision:
        pass

class CapsLockModerator(Moderator):
    def __init__(
            self,
            message: str,
            decision: ModerationDecision,
            timeout_duration: Union[None, int],
            min_size: int,
            threshold: int
    ):
        super().__init__(message, decision, timeout_duration)

        self.min_size = min_size
        self.threshold = threshold / 100

    def get_name(self) -> str:
        return 'Caps Lock'

    def vote(self, msg: str, author: str) -> ModerationDecision:
        msg = ''.join(filter(str.isalpha, msg))

        if len(msg) < self.min_size:
            return ModerationDecision.ABSTAIN

        n = 0
        for char in msg:
            if char.strip() == '':
                continue
            if char == char.upper():
                n += 1

        if n / len(msg) >= self.threshold:
            return self.decision

        return ModerationDecision.ABSTAIN

class FloodModerator(Moderator):
    def __init__(
            self,
            message: str,
            decision: ModerationDecision,
            timeout_duration: Union[None, int],
            max_word_length: Union[None, int],
            raid_cooldown: Union[None, int],
            ignore_hashtags: bool,
            max_msg_occurrences: Union[None, int],
            min_time_between_occurrence: Union[None, int]
    ):
        super().__init__(message, decision, timeout_duration)
        self.max_word_length = max_word_length
        self.raid_cooldown = raid_cooldown
        self.last_raid = EPOCH
        self.ignore_hashtags = ignore_hashtags
        self.max_msg_occurrences = max_msg_occurrences
        self.min_time_between_occurrence = min_time_between_occurrence
        self.last_msgs = []

    def get_name(self) -> str:
        return 'Flood'

    def vote(self, msg: str, author: str) -> ModerationDecision:
        if self.raid_cooldown is not None and self.last_raid + timedelta(minutes=self.raid_cooldown) > datetime.now():
            return ModerationDecision.ABSTAIN

        if self.max_word_length is not None:
            for word in msg.split(' '):
                if word.startswith('#'):
                    continue
                if len(word) > self.max_word_length:
                    return ModerationDecision.TIMEOUT_USER

        if self.max_msg_occurrences is None or self.min_time_between_occurrence is None:
            return ModerationDecision.ABSTAIN

        clean_msg = None
        for last_msg in self.last_msgs:
            if last_msg['first-occurrence'] + timedelta(seconds=self.min_time_between_occurrence) <= datetime.now():
                clean_msg = last_msg
                break

            if author != last_msg['author'] or msg != last_msg['message']:
                break

            last_msg['occurrences'] += 1
            if last_msg['occurrences'] >= self.max_msg_occurrences:
                return ModerationDecision.TIMEOUT_USER

        if clean_msg is not None:
            self.last_msgs.remove(clean_msg)

        self.last_msgs.append({
            'first-occurrence': datetime.now(),
            'author': author,
            'message': msg,
            'occurrences': 1
        })

        return ModerationDecision.ABSTAIN

    def declare_raid(self):
        self.last_raid = datetime.now()


import cv2
import numpy as np

img = cv2.imread("../../../img/taekwonv1.jpg")

smaller = cv2.pyrDown(img)
bigger = cv2.pyrUp(smaller)

laplacian = cv2.subtract(img, bigger)
restored = bigger + laplacian
merged = np.hstack((img, laplacian, bigger, restored))

cv2.imshow("Laplacian Pyramid", merged)
cv2.waitKey(0)
cv2.destroyAllWindows()


# coding: utf-8

# In[89]:

from collections import Counter
import scipy.stats as stats
import pandas as pd
import os
import skimage.io as io
import matplotlib.pyplot as plt
import csv
import errno
import sys
import random

#reload(sys)
#sys.setdefaultencoding('utf8')

def chunks(listItems, groupSize):
    #Generator function to yield chuncks of 4 from the list
    for i in range(0, len(listItems), groupSize):
        yield listItems[i:i + groupSize]

def produceLabel(answerList):
    entropy = stats.entropy(createFreqDistribution(answerList))
    return entropy

def createFreqDistribution(answerList):
    #print "before>>>"
    #print answerList
    #print "<<<<<<<<after>>>>>>"
    answerList=[ans.lower() for ans in answerList]
    #print answerList
    uniqueAnswers = Counter(answerList).keys()
    answersFrequencies = Counter(answerList).values()
    frequencyVector = []
    numSamples = 10
    for count in answersFrequencies:
        frequency = float(count)/float(numSamples)
        frequencyVector.append(frequency)

    numZeros = numSamples - len(answersFrequencies)
    for i in range(numZeros):
        frequencyVector.append(0)
    return frequencyVector

# In[90]:

def search_dictionary(key, value, list_of_dictionaries):
    return [element for element in list_of_dictionaries if element[key] == value]

# User-configurable parameter
baseDir = "D:/Study !/CPU-Vision/project/test/VQACrowdSourcing/data/"
imgDirPath = "https://www.cs.utexas.edu/~dgurari/Projects/vqAnswerCollection/" + "Images/"
vqasPath = baseDir + "Answers/answers.xls"
destPath = baseDir + "Res-Test/"

df = pd.read_excel(open(vqasPath,'rb'), sheetname='answers')
allHITResults = df['hitResult'].values

questionList = []
answersList = {}
entropyMoreHalf=[]
entropyBetweenHalfandThreeHalf=[]
entropyMoreThanThreeHalf=[]
checkCount=0
with open(destPath+'../testInput.input', 'wb') as csvfile:
    VQAWriter = csv.writer(csvfile,delimiter='\t', quoting=csv.QUOTE_MINIMAL)
    VQAWriter.writerow(["i1", "i2", "i3","i4"])
    for hit in allHITResults:
        processedhit = hit.replace("[", "")
        processedhit = processedhit.replace("]", "")
        processedhit = processedhit.replace("\"", "")
        processedhit = processedhit.replace("{", "")
        vqas = processedhit.split('}')
        for vqa in vqas:  # ignore last entry which will be blank
            if (len(vqa) == 0):
                continue
            if (vqa[0] == ','):
                vqa = vqa[1:len(vqa)]
            imgIndex = vqa.find("imgID:")
            questionIndex = vqa.find("question:")
            answerIndex = vqa.find("answer:")
            answerConfIndex = vqa.find("ansConf:")
            imageName = vqa[imgIndex+6:questionIndex-1]
            question = vqa[questionIndex+9:answerIndex-1]
            answer = vqa[answerIndex+7:answerConfIndex-1]
            # answerConf = vqaPieces[3].replace("ansConf:","")
            dict = {'imageName': imageName, 'question': question}
            questionList.append(dict)
            answersList.setdefault(imageName, []).append(answer)
            imgPath = imgDirPath + imageName
            imgText = question + '\n'
            curAnswers = answersList.get(imageName)
            answers = '#'.join(map(str, curAnswers))
            if (len(curAnswers) == 10):
                entropyVal=produceLabel(curAnswers)
                if(entropyVal>1.5):
                    entropyMoreThanThreeHalf.append(imageName+"|"+question+"|"+answers+"|"+str(3)+"|"+"V")
                elif(entropyVal>1.0 and entropyVal<1.5):
                    entropyBetweenHalfandThreeHalf.append(imageName+"|"+question+"|"+answers+"|"+str(2)+"|"+"V")
                elif(entropyVal>0.5):
                    entropyMoreHalf.append(imageName+"|"+question+"|"+answers+"|"+str(1)+"|"+"V")
                else:
                    checkCount+=1

    entropyMoreThanThreeHalf=random.sample(entropyMoreThanThreeHalf,16)
    entropyBetweenHalfandThreeHalf=random.sample(entropyBetweenHalfandThreeHalf,16)
    entropyMoreHalf=random.sample(entropyMoreHalf,16)

    for element in chunks(entropyMoreThanThreeHalf,4):
        VQAWriter.writerow(element)
    for element in chunks(entropyBetweenHalfandThreeHalf,4):
        VQAWriter.writerow(element)
    for element in chunks(entropyMoreHalf,4):
        VQAWriter.writerow(element)

# In[60]:

# In[ ]:


import mwparserfromhell as parser
import re

ExternalLink = parser.nodes.external_link.ExternalLink
Argument = parser.nodes.argument.Argument
Template = parser.nodes.template.Template
Wikilink = parser.nodes.wikilink.Wikilink
Comment = parser.nodes.comment.Comment
Heading = parser.nodes.heading.Heading
Text = parser.nodes.text.Text
Tag = parser.nodes.tag.Tag
Node = parser.nodes.Node

def get_heading(heading):
  properties = normalize_section(heading.title.nodes)
  if properties[0]["type"] == "text":
    properties[0]["text"] = properties[0]["text"].lstrip()

  return {
    "properties": properties,
    "level": heading.level,
    "type": "heading",
  }

def get_template(template):
  params = template.params
  name = str(template.name)

  result = {
    "type": "template",
    "content": [],
    "params": {},
    "name": name,
  }

  for param in params:
    if param.showkey:
      result["params"][str(param.name)] = str(param.value)
    else:
      result["content"].append(normalize_section(param.value.nodes))

  return result

def get_tag(tag):
  result = {
    "closing_tag": str(tag.closing_tag),
    "attributes": str(tag.attributes),
    "self_closing": tag.self_closing,
    "implicit": tag.implicit,
    "type": "tag"
  }

  if tag.closing_wiki_markup:
    result["closing_wiki_markup"] = str(tag.closing_wiki_markup),

  if tag.contents:
    result["properties"] = normalize_section(tag.contents.nodes)
  else:
    result["properties"] = []
  return result

def get_wikilink(link):
  result = {
    "title": str(link.title)
  }

  matching_file = re.compile("^(File|Файл):.*$")
  if matching_file.fullmatch(result["title"]):
    result["type"] = "filelink"
  else:
    result["type"] = "wikilink"
    if link.text:
      result["text"] = str(link.text)

  if (link.text):
    result["text"] = str(link.text)
  return result

def get_external_link(link):
  return {
    "title": str(link.title),
    "url": str(link.url),
    "type": "link",
  }

def normalize_section(nodes):
  result = []

  for index, node in enumerate(nodes):
    node_type = type(node)

    if node_type == Text:
      result.append({
        "text": node.value,
        "type": "text"
      })

    elif node_type == Heading:
      heading = get_heading(node)
      result.append(heading)

    elif node_type == Template:
      template = get_template(node)
      result.append(template)

    elif node_type == Tag:
      tag = get_tag(node)
      result.append(tag)

    elif node_type == Wikilink:
      wikilink = get_wikilink(node)
      result.append(wikilink)

    elif node_type == ExternalLink:
      external_link = get_external_link(node)
      result.append(external_link)

  return result

def parse(wikicode):
  sections = parser.parse(wikicode).get_sections()
  result_nodes = [ normalize_section(section.nodes) for section in sections ]
  return result_nodes


import torch

from diamond.image_caption.utils import load_images_from_numpy
from diamond.user_classes import ImagesData, ImageDescriptionSpec

import os
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer

class ImageDescription:
    def __init__(self,
                 images_data: ImagesData,
                 image_description_spec: ImageDescriptionSpec,
                 plot=True,
                 diamond_collector=None):
        self.images_data = images_data
        self.image_description_spec = image_description_spec
        self.plot = plot
        self.gen_kwargs = self.image_description_spec.gen_kwargs
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.label_dict = self.images_data.label_dict
        self.model, self.feature_extractor, self.tokenizer = None, None, None
        self.diamond_collector = {} if diamond_collector is None else diamond_collector
        self.images = self.images_data.raw_images
        self.load_model()

    def load_model(self):
        self.model, self.feature_extractor, self.tokenizer = get_image_captioning_models()

    def predict_caption(self):
        new_images = []
        model = self.model.to(self.device)
        feature_extractor = self.feature_extractor
        tokenizer = self.tokenizer
        gen_kwargs = self.gen_kwargs
        for image in self.images:
            if image.mode != "RGB":
                image = image.convert(mode="RGB")
            new_images.append(image)
        print("Predicting Image Description")
        pixel_values = feature_extractor(images=new_images, return_tensors="pt").pixel_values
        pixel_values = pixel_values.to(self.device)
        output_ids = model.generate(pixel_values, **gen_kwargs)
        preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
        preds = [pred.strip() for pred in preds]
        print("Predicting Image Description - Done!")
        return preds

    def run(self):
        self.diamond_collector["image description"] = self.predict_caption()
        if self.plot:
            pass

def get_image_captioning_models():
    # Check if models directory exists
    print("=> Loading Image Caption Models...")

    model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
    feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
    tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")

    print("=> Loading Models - Done!")
    return model, feature_extractor, tokenizer


import collections

class Solution:
    # @param {integer[]} nums
    # @param {integer} k
    # @param {integer} t
    # @return {boolean}
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        if k < 1 or t < 0:
            return False
        numDict = collections.OrderedDict()
        for x in range(len(nums)):
            key = nums[x] / max(1, t)
            for m in (key, key - 1, key + 1):
                if m in numDict and abs(nums[x] - numDict[m]) <= t:
                    return "true"
            numDict[key] = nums[x]
            if x >=  k:
                numDict.popitem(last=False)
        return "false"

t=Solution()
s=input()
T=int(s[len(s)-1])
num=[]
i=0
check=0
k=0
already=0
while (i<len(s)):
    if s[i]=='[':
        check=1
        while(s[i]!=']'):
            num.append(s[i])
            i+=1
    c=s[i]
    if check==1 and '0'<=s[i]<='9' and already==0:
        k=int(s[i])
        already+=1
    i+=1
nums="".join(num)+']'
print(t.containsNearbyAlmostDuplicate(eval(nums),k,T))


import customtkinter as ctk  # Enhanced tkinter module for improved UI elements
from datetime import date  # Date module for handling date operations

# Define the 'Penalties' class, inheriting from customtkinter's CTkFrame
class Penalties(ctk.CTkFrame):
    def __init__(self, parent, controller, db):
        super().__init__(parent)    # Initialize the parent class (CTkFrame)
        # Initialize attributes for storing various data and UI elements
        self.scrollable_frame = None
        self.lapsL = None
        self.trackL = None
        self.countryL = None
        self.penalties = None
        self.lapsT = None
        self.trackT = None
        self.countryT = None

        self.controller = controller  # Controller reference
        self.data = db  # Database connection
        self.load_data(self.controller.race_menu.get())  # Load data based on the selected race
        self.setup_ui()  # Setup the user interface

    def setup_ui(self):
        # Set grid weights for layout management and create UI elements
        # Configure rows and columns with appropriate weights for the layout

        # Set weights for rows and columns
        self.grid_rowconfigure(1, weight=0)  # Country label row
        self.grid_rowconfigure(2, weight=0)  # Track row
        self.grid_rowconfigure(3, weight=1)  # Scrollable Frame row

        self.grid_columnconfigure(1, weight=1)  # Country and track label column
        self.grid_columnconfigure(2, weight=1)  # This will be the container for the segmented button
        self.grid_columnconfigure(3, weight=1)  # Laps and Pts label column

        # UI Elements

        self.countryL = ctk.CTkLabel(self, text=self.countryT, font=("Lucidia Sans", 25))
        self.countryL.grid(row=1, column=1, sticky="nw", padx=20, pady=10)

        self.trackL = ctk.CTkLabel(self, text=self.trackT, font=("Lucidia Sans", 20))
        self.trackL.grid(row=2, column=1, sticky="nw", padx=20)

        self.lapsL = ctk.CTkLabel(self, text=f"Laps: {self.lapsT}", font=("Lucidia Sans", 20))
        self.lapsL.grid(row=1, column=2, sticky="ne", columnspan=3, padx=20, pady=10)

        self.scrollable_frame = ctk.CTkScrollableFrame(self, width=720, height=200)
        self.scrollable_frame.grid(row=3, column=1, columnspan=3, pady=10, sticky="nsew")

        self.fill_penalties()

    def fill_penalties(self):
        # Update label text with the current data and populate penalties
        # Clear existing content in the scrollable frame
        # Iterate over fetched penalty data, creating and positioning labels
        self.countryL.configure(text=self.countryT)
        self.trackL.configure(text=self.trackT)
        self.lapsL.configure(text=f"Laps: {self.lapsT}")

        for widget in self.scrollable_frame.winfo_children():
            widget.destroy()

        i = 0
        while self.penalties:
            nameT = self.penalties[0]
            sessionT = self.penalties[1]
            offenceT = self.penalties[2]
            decisionT = self.penalties[3]

            self.scrollable_frame.grid_columnconfigure(0, weight=1)
            self.scrollable_frame.grid_columnconfigure(1, weight=1)
            self.scrollable_frame.grid_columnconfigure(2, weight=1)

            nameL = ctk.CTkLabel(self.scrollable_frame, text=nameT, font=("Lucidia Sans", 17))
            nameL.grid(row=i, column=0, sticky="nw", padx=20, pady=10)

            sessionL = ctk.CTkLabel(self.scrollable_frame, text=sessionT, font=("Lucidia Sans", 17))
            sessionL.grid(row=i, column=1, sticky="nw", padx=20, pady=10)

            offenceL = ctk.CTkLabel(self.scrollable_frame, text=offenceT, font=("Lucidia Sans", 17))
            offenceL.grid(row=i, column=2, sticky="nw", padx=20, pady=10)

            decisionL = ctk.CTkLabel(self.scrollable_frame, text=decisionT, font=("Lucidia Sans", 17))
            decisionL.grid(row=i, column=3, sticky="nw", padx=20, pady=10)

            self.penalties = self.data.fetchone()
            i += 1

    def load_data(self, race, season=date.today().year):
        # Load penalty data based on the selected race and season
        # Execute SQL queries to fetch race details and penalty data
        # Update class attributes with fetched data
        query = (
            f"SELECT r.country, r.track, r.laps, r.season, r.round FROM races r WHERE r.season = {season} AND r.track ="
            "? ORDER BY date DESC")
        self.data.execute(query, (race,))
        fetched = self.data.fetchone()

        self.countryT = fetched[0]
        self.trackT = fetched[1]
        if fetched[2] is not None:
            self.lapsT = str(int(fetched[2]))
        else:
            self.lapsT = "Not Updated Yet"
        seasonT = fetched[3]
        roundT = fetched[4]

        query = ("SELECT d.name, p.session, p.offence, p.decision "
                 "FROM drivers d "
                 "JOIN penalties p ON d.number = p.number AND d.season = p.season "
                 f"WHERE p.round = {roundT} AND p.season = {seasonT} "
                 "ORDER BY p.incident")
        self.data.execute(query)

        self.penalties = self.data.fetchone()


import logging
import os

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logger = logging.getLogger(__name__)

def get_file_data(fn='input.txt'):
    with open(fn) as f:
        data = f.read()
    return data

def parse_data(text_data):
    inventory_blobs = text_data.strip('\n').strip().split('\n\n')
    inventories = [[int(x) for x in blob.strip('\n').strip().split('\n')] for blob in inventory_blobs]
    return inventories

def get_top3_inventories(inventories):
    totals = [sum(inventory) for inventory in inventories]
    totals.sort(reverse=True)
    return sum(totals[0:3])

def get_max_inventory(inventories):
    totals = [sum(inventory) for inventory in inventories]
    return max(totals)

def main():
    logger.setLevel(level=logging.INFO)
    data = get_file_data()
    inventories = parse_data(data)
    answer = get_max_inventory(inventories)
    logger.info(f"Puzzle1: Largest Elf Inventory: {answer}")
    answer = get_top3_inventories(inventories)
    logger.info(f"Puzzle2: Sum of Top 3 Elf Inventories: {answer}")

if __name__ == '__main__':
    main()


import numpy as np
from tqdm import tqdm
import torch
from poutyne import Model, SKLearnMetrics
from dataset import unnormalize, normalize, pad_borders
from utils import metric_flatten
from models.Baseline import Baseline
# from models.PerfectGraph import PerfectGraph
from sklearn.metrics import r2_score, explained_variance_score, mean_squared_error, median_absolute_error
from typing import Optional, List, Tuple
import matplotlib.pyplot as plt
import pandas as pd
from utils import to_device_eval

from matplotlib import cm
from matplotlib.colors import Normalize
from scipy.interpolate import interpn

def density_scatter( x , y, ax = None, sort = True, bins = 20, **kwargs )   :
    """
    Scatter plot colored by 2d histogram
    """

    yhat = x[time_step][mask_cnn]
    test_targets = y[time_step][mask_cnn]

    if ax is None :
        fig , ax = plt.subplots(figsize=(8,5))
    data , x_e, y_e = np.histogram2d( x, y, bins = bins, density = True )
    z = interpn( ( 0.5*(x_e[1:] + x_e[:-1]) , 0.5*(y_e[1:]+y_e[:-1]) ) , data , np.vstack([x,y]).T , method = "splinef2d", bounds_error = False)

    #To be sure to plot all data
    z[np.where(np.isnan(z))] = 0.0

    # Sort the points by density, so that the densest points are plotted last
    if sort :
        idx = z.argsort()
        x, y, z = x[idx], y[idx], z[idx]

    ax.scatter( x, y, c=z, **kwargs )
    plt.xlim([0, 6])
    plt.ylim([0, 6])

    plt.xlabel("Predicted water depth (m)",fontsize=18)
    plt.ylabel("Real water depth (m)", fontsize=18)
    plt.title("Scatter plot errors", fontsize=20)
#     plt.plot([0, 6], [0, 6], 'black', label= 'diagonal')

    norm = Normalize(vmin = np.min(z), vmax = np.max(z))
    cbar = fig.colorbar(cm.ScalarMappable(norm = norm), ax=ax)
    cbar.ax.set_ylabel('Density')

    return ax

def predict_event_bay(model, dataset, event_num, arch, start_ts=None, ar = True, T = None, ts_out = 0):
    """
    Predict a full event.

    This function will split the prediction if `dataset.sample_type == "single"`.
    Alternatively, it will use the full frame to predict if `dataset.sample_type == "full"`.

    ar: auto regressively
    start_ts: start at timestep start_ts (if None starts at 0.)
    """

    patch_dim = dataset.nx
    # inds = dataset.get_all_fix_indexes()
    b = dataset.border_size
    timestep = dataset.timestep
    tsh = dataset.predict_ahead

    if start_ts is None:
        start_ts = timestep-1
    assert start_ts >= timestep-1
    # assert dataset.normalize_output == False
    if T is None:
        T = len(dataset.rainfall_events[event_num]) - start_ts - 1 - tsh

    index_t = start_ts - timestep + 1
    xin = dataset.waterdepth[event_num][index_t : index_t +timestep].clone()
    rainfall = dataset.rainfall_events[event_num][index_t : ].clone()
    dem = dataset.dem
    diff_dem = dataset.diff_dem
    mask = dataset.dem_mask
    recons_pred_full = torch.zeros(T, dataset.px - 2*b, dataset.py - 2*b)
    recons_pred_full_sigma = torch.zeros(T, dataset.px - 2*b, dataset.py - 2*b)

    for t in tqdm(range(T)):
        recons_pred_full[t], recons_pred_full_sigma[t] = predict_next_ts_bay(dataset, model, xin, rainfall[t:t+timestep+tsh], mask, dem, diff_dem, ts_out)
        recons_pred_full[t] = recons_pred_full[t] * mask
        recons_pred_full_sigma[t] = recons_pred_full_sigma[t] * mask

        if ar:
            if timestep>1:
                xin[:-1] = xin[1:].clone()
            xin[-1] = dataset.pad_borders(recons_pred_full[t].clone(), 0)
        else:
            xin = dataset.waterdepth[event_num][index_t+t+1: index_t+t+timestep+1].clone()

    if b:
        recons_gt_full = dataset.waterdepth[event_num][start_ts+tsh +1:start_ts+tsh+T +1, b : -b, b: -b].clone()
        recons_mask_full = mask[b : -b, b : -b ].clone()
    else:
        recons_gt_full = dataset.waterdepth[event_num][start_ts+tsh +1:start_ts+tsh+T +1, : , :].clone()
        recons_mask_full = mask.clone()

    return recons_pred_full.numpy(), recons_gt_full.numpy(), recons_mask_full.numpy(), recons_pred_full_sigma.numpy()

def predict_next_ts_bay(dataset, model, xin, rainfall, mask, dem, diff_dem, ts_out):

    """Predict the next timestep for unet arch. C x H x W
    C = channels, H = height, W = width
    """

    b = dataset.border_size
    normalize_output = dataset.normalize_output

    def create_inputs(dataset, x_p, y_p, xin, rainfall, mask, dem, diff_dem):
        xin, mask, dem, diff_dem, _ = dataset.crop_to_patch(x_p, y_p, xin, mask, dem, diff_dem)
        inputs = dataset.build_inputs(xin, rainfall, mask, dem, diff_dem)
        return inputs

    nx = dataset.nx
    ny = dataset.ny

    inds = dataset.get_all_fix_indexes(non_full=True)
    recons_pred = torch.zeros(dataset.px - 2*b, dataset.py - 2*b)
    recons_pred_sigma = torch.zeros(dataset.px - 2*b, dataset.py - 2*b)
    for inds_count, (x_p, y_p) in enumerate(inds):

        inputs_ts = create_inputs(dataset, x_p, y_p, xin, rainfall, mask, dem, diff_dem)

        # stef
        (data, mask1) = inputs_ts
        data = data.unsqueeze(dim=0)
        data = to_device_eval(data)
        y_pred = model(data)

        sigma = y_pred['sigma'].detach().cpu()
        y_pred = y_pred['y_pred'].squeeze().detach().cpu()

        pred_cnn_ts = y_pred * mask1.squeeze().numpy()
        sigma = sigma * mask1.squeeze().numpy()

        x_p2 = x_p + (nx-b*2)
        y_p2 = y_p + (ny-b*2)
        if normalize_output:
            px, py = y_pred.shape
            y_pred = xin[-1, x_p+b: x_p+b+ px, y_p+b:y_p + b + py] + unnormalize(pred_cnn_ts)

        if ts_out:
            pred_cnn_ts = pred_cnn_ts.squeeze()[0]
            sigma = sigma.squeeze()[0]

        recons_pred[x_p:x_p2, y_p:y_p2] = pred_cnn_ts
        recons_pred_sigma[x_p:x_p2, y_p:y_p2] = sigma

    return recons_pred, recons_pred_sigma

def plot_answer_sample_bay(pred, pred_sigma, gt, mask, ts, zoom=None, show_diff=True, global_scale=False, save_folder = None, model_name = '_1ts_ahead_cnn'):

    '''
    Visualize a full or zoomed in reconstruction for 1 timestep, for a single model. This function is valid for both 1timestep ahead and autoregressive mode
    Inputs:
        - predictions
        - aground truth
        - mask
        - zoom: array of coordinates
    '''

    import matplotlib.colors as colors
    cmin = 0.01
    cmap = "hot_r"
    cmap = "gist_heat_r"
#     cmap='seismic'
    if zoom is not None:
        pred = pred[:, zoom[0]:zoom[1], zoom[2]:zoom[3]]
        pred_sigma = pred_sigma[:,zoom[0]:zoom[1], zoom[2]:zoom[3]]
        gt = gt[:, zoom[0]:zoom[1], zoom[2]:zoom[3]]
        mask = mask[zoom[0]:zoom[1], zoom[2]:zoom[3]]

    if show_diff:
        fig, axs = plt.subplots(2,3, figsize=(18,10))
        axs = [axs[0][0], axs[0][1], axs[0][2], axs[1][0], axs[1][1], axs[1][2]]
    else:
        fig, axs = plt.subplots(1,4, figsize=(18,5))

    diff = np.abs(gt[ts] - pred[ts])

    if global_scale:
        vmax_abs = max(np.max(gt), cmin)
        vmax_diff = max(np.max(np.abs(gt-pred)), cmin)
    else:
        vmax_abs = max(np.max(gt[ts]), cmin)
        vmax_diff = max(np.max(diff), cmin)

    pos0 = axs[0].imshow(pred[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_abs))
    pos1 = axs[1].imshow(gt[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_abs))
    pos2 = axs[2].imshow(pred_sigma[ts], cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_diff))
    pos3 = axs[3].imshow(diff, cmap=cmap, norm=colors.LogNorm(vmin =cmin, vmax = vmax_diff))
    if show_diff and ts>0:
        pos3 = axs[3].imshow(pred[ts] - gt[ts-1], cmap="seismic", vmin=-vmax_diff, vmax = vmax_diff)
        pos4 = axs[4].imshow(gt[ts] - gt[ts-1], cmap="seismic", vmin=-vmax_diff, vmax = vmax_diff)

    fig.suptitle("Visualization for model {} and timestep {} ".format(model_name.split('_')[-1], ts), fontsize = 14 ,fontweight="bold")

    axs[0].set_title('Reconstructed Output',fontweight="bold")
    axs[1].set_title('Reconstructed GT ',fontweight="bold")
    axs[2].set_title('Sigma',fontweight="bold")
    axs[3].set_title('Absolute Error',fontweight="bold")

    cbar0 = fig.colorbar(pos0, ax=axs[0], extend='both', fraction=0.136, pad=0.02)
    cbar1 = fig.colorbar(pos1, ax=axs[1], extend='both', fraction=0.136, pad=0.02)
    cbar2 = fig.colorbar(pos2, ax=axs[2], extend='both', fraction=0.136, pad=0.02)
    cbar3 = fig.colorbar(pos3, ax=axs[2], extend='both', fraction=0.136, pad=0.02)

    if show_diff and ts>0:
        axs[3].set_title('Diff pred',fontweight="bold")
        axs[4].set_title('Diff GT',fontweight="bold")
        cbar3 = fig.colorbar(pos3, ax=axs[3], extend='both', fraction=0.136, pad=0.02)
        cbar4 = fig.colorbar(pos4, ax=axs[4], extend='both', fraction=0.136, pad=0.02)

    if save_folder:
        if zoom:
            model_name = model_name + '_zoom'
        filename = save_folder + 'Reconstruction_' +  model_name + '.png'
        plt.savefig(filename, dpi=1200)
    plt.show()

    # Combine errors

def boxplot_single(time_step, pred_cnn, gt_cnn, mask_cnn):
    yhat = pred_cnn[time_step][mask_cnn]
    test_targets = gt_cnn[time_step][mask_cnn]

    a = [yhat, test_targets]
    a = np.array(a).T
    a_df = pd.DataFrame(data=a, columns=['yhat', 'test_targets'])
    a_df_sorted = a_df.sort_values(by=['test_targets'])
    a_df_sorted.reset_index(drop=True, inplace=True)
    a_df_error = 100 * np.abs(a_df_sorted.yhat - a_df_sorted.test_targets)    # predicting in meter, so need to be multiplied by 100

    data = np.array([a_df_error[a_df_sorted.test_targets<0.1],
            a_df_error[(a_df_sorted.test_targets>0.10) & (a_df_sorted.test_targets<0.20)],
            a_df_error[(a_df_sorted.test_targets>0.20) & (a_df_sorted.test_targets<0.50)],
            a_df_error[(a_df_sorted.test_targets>0.50) & (a_df_sorted.test_targets<1)],
            a_df_error[a_df_sorted.test_targets>1]], dtype="object")
    fig, ax = plt.subplots(figsize = [8,5])
    ax.set_ylabel('Absolute Error (cm)')
    ax.set_xlabel('Water Depth')
    ax.boxplot(data, showfliers=False)                                #'Hide Outlier Points'
    ax.set_xticklabels(['0-10 cm', '10-20 cm', '20-50cm', '50-100cm','>100cm'])

    plt.show()


from collections.abc import (
    Callable,
    Coroutine,
)
from dast.aws.types import (
    Location,
)
from dast.aws.utils import (
    build_vulnerabilities,
    run_boto3_fun,
)
from model import (
    core_model,
)
from model.core_model import (
    AwsCredentials,
    Vulnerability,
)
from typing import (
    Any,
)

async def uses_insecure_security_policy(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    predifined_ssl_policy_values: list[str] = [
        "ELBSecurityPolicy-2015-05",
        "ELBSecurityPolicy-2016-08",
        "ELBSecurityPolicy-FS-2018-06",
        "ELBSecurityPolicy-FS-1-1-2019-08",
        "ELBSecurityPolicy-FS-1-2-2019-08",
        "ELBSecurityPolicy-FS-1-2-Res-2019-08",
        "ELBSecurityPolicy-FS-1-2-Res-2020-10",
        "ELBSecurityPolicy-TLS-1-0-2015-04",
        "ELBSecurityPolicy-TLS-1-1-2017-01",
        "ELBSecurityPolicy-TLS-1-2-2017-01",
        "ELBSecurityPolicy-TLS-1-2-Ext-2018-06",
        "ELBSecurityPolicy-TLS13-1-0-2021-06",
        "ELBSecurityPolicy-TLS13-1-1-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Ext1-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Ext2-2021-06",
        "ELBSecurityPolicy-TLS13-1-2-Res-2021-06",
        "ELBSecurityPolicy-TLS13-1-3-2021-06",
    ]

    safe_ssl_policy_values: list[str] = [
        "ELBSecurityPolicy-FS-1-2-Res-2020-10",
        "ELBSecurityPolicy-TLS13-1-2-Res-2021-06",
        "ELBSecurityPolicy-TLS13-1-3-2021-06",
    ]

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="elbv2", function="describe_load_balancers"
    )
    balancers = response.get("LoadBalancers", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if balancers:
        for balancer in balancers:
            locations: list[Location] = []
            load_balancer_arn = balancer["LoadBalancerArn"]

            listeners: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="elbv2",
                function="describe_listeners",
                parameters={
                    "LoadBalancerArn": str(load_balancer_arn),
                },
            )
            for index, listener in enumerate(listeners.get("Listeners", [])):
                ssl_policy = listener.get("SslPolicy", None)
                if (
                    ssl_policy
                    and ssl_policy in predifined_ssl_policy_values
                    and ssl_policy not in safe_ssl_policy_values
                ):
                    locations = [
                        *locations,
                        *[
                            Location(
                                access_patterns=(
                                    f"/Listeners/{index}/SslPolicy",
                                ),
                                arn=(f"{listener['ListenerArn']}"),
                                values=(ssl_policy,),
                                description=(
                                    "src.lib_path.f070."
                                    "elb2_uses_insecure_security_policy"
                                ),
                            )
                        ],
                    ]

            vulns = (
                *vulns,
                *build_vulnerabilities(
                    locations=locations,
                    method=(
                        core_model.MethodsEnum.AWS_INSECURE_SECURITY_POLICY
                    ),
                    aws_response=listeners,
                ),
            )

    return vulns

async def target_group_insecure_port(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="elbv2", function="describe_target_groups"
    )
    target_groups = response.get("TargetGroups", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if target_groups:
        for index, target_group in enumerate(target_groups):
            locations: list[Location] = []
            port = target_group.get("Port", "")
            target_type = target_group.get("TargetType", "")
            if target_type == "lambda":
                continue
            if not port:
                locations = [
                    Location(
                        access_patterns=(f"/TargetGroups/{index}/TargetType",),
                        arn=(f"{target_group['TargetGroupArn']}"),
                        values=(target_type,),
                        description=(
                            "src.lib_path.f070."
                            "elb2_target_group_insecure_port"
                        ),
                    )
                ]

            elif int(port) != 443:
                locations = [
                    Location(
                        access_patterns=(f"/TargetGroups/{index}/Port",),
                        arn=(f"{target_group['TargetGroupArn']}"),
                        values=(port,),
                        description=(
                            "src.lib_path.f070.elb2_target_group_insecure_port"
                        ),
                    )
                ]

            vulns = (
                *vulns,
                *build_vulnerabilities(
                    locations=locations,
                    method=(core_model.MethodsEnum.AWS_GROUP_INSECURE_PORT),
                    aws_response=response,
                ),
            )
    return vulns

async def eks_allows_insecure_inbound_traffic(
    credentials: AwsCredentials,
) -> core_model.Vulnerabilities:

    response: dict[str, Any] = await run_boto3_fun(
        credentials, service="eks", function="list_clusters"
    )
    model = core_model.MethodsEnum.AWS_EKS_INSECURE_INBOUND_TRAFFIC
    clusters = response.get("clusters", []) if response else []
    vulns: core_model.Vulnerabilities = ()
    if clusters:
        for cluster in clusters:
            locations: list[Location] = []
            describe_cluster: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="eks",
                function="describe_cluster",
                parameters={
                    "name": str(cluster),
                },
            )
            cluster_description = describe_cluster.get("cluster", [])

            security_groups_ids = cluster_description["resourcesVpcConfig"][
                "securityGroupIds"
            ]
            describe_security_groups: dict[str, Any] = await run_boto3_fun(
                credentials,
                service="ec2",
                function="describe_security_groups",
                parameters={
                    "GroupIds": security_groups_ids,
                },
            )
            security_groups = (
                describe_security_groups.get("SecurityGroups", [])
                if response
                else []
            )
            if security_groups:
                for group in security_groups:
                    locations = [
                        *[
                            Location(
                                access_patterns=(
                                    f"/IpPermissions/{index_ip}/FromPort",
                                    f"/IpPermissions/{index_ip}/ToPort",
                                ),
                                arn=(
                                    f"arn:aws:ec2::{group['OwnerId']}:"
                                    f"security-group/{group['GroupId']}"
                                ),
                                values=(
                                    ip_permission.get("FromPort"),
                                    ip_permission.get("ToPort"),
                                ),
                                description=(
                                    "lib_path.f070."
                                    "eks_allows_insecure_inbound_traffic"
                                ),
                            )
                            for index_ip, ip_permission in enumerate(
                                group["IpPermissions"]
                            )
                            if not (
                                ip_permission["FromPort"] == 443
                                and ip_permission["FromPort"]
                                == ip_permission["ToPort"]
                            )
                        ],
                    ]

                    vulns = (
                        *vulns,
                        *build_vulnerabilities(
                            locations=locations,
                            method=model,
                            aws_response=group,
                        ),
                    )
    return vulns

CHECKS: tuple[
    Callable[[AwsCredentials], Coroutine[Any, Any, tuple[Vulnerability, ...]]],
    ...,
] = (
    target_group_insecure_port,
    uses_insecure_security_policy,
    eks_allows_insecure_inbound_traffic,
)


#
# (c) 2020 Yoichi Tanibayashi
#
"""
Music Box Websocket Server

### Architecture (server side)

         --------------------------------------------
This -->|                  WsServer                  |
        |-------------------------------+------------|
        |            Player             |            |
        |-------------------------------|            |
        |           Movement            |            |
        |-------------------------------| websockets |
        |     Servo     | RotationMotor |            |
        |---------------+---------------|            |
        | ServoPCA9685  |  StepMtrTh    |            |
        |---------------+---------------|            |
        | pigpioPCA9685 |   StepMtr     |            |
         --------------------------------------------

"""
__author__ = 'Yoichi Tanibayashi'
__date__ = '2021/01'

import json
import asyncio
import websockets
from . import Player
from .my_logger import get_logger

class WsServer:
    """ Music Box websocket server

    Message format (JSON)
    ---------------------
    {"cmd": "single_play", "ch": [0,2,4]}  # single play

    {"cmd": "music_load",                 # load music and play
     "music_data": [ {"ch": null,"delay": 500},.. ]

    {"cmd": "music_play"}                 # (re)start music
    {"cmd": "music_stop"}
    {"cmd": "music_pause"}
    {"cmd": "music_seek", "pos": 30.5}
    {"cmd": "music_rewind"}

    {"cmd": "calibrate",                # change servo param
     "ch": 5,
     "on": true,  # on:ture, off: false
     "pw_diff": -10,
     "tap": ture }

    Simple client example(1)
    ---------------------------------------
    ```python3
    from musicbox import WsClientHostPort

    msg = {'cmd': ...}

    client = WsClientHostPort('localhost', 8880)
    client.send(msg)
    ```

    [FYI]
    Simple websocket client example(2)
    ----------------------------------
    ```python3
    # [Important!] 'websocket' is not 'websockets'

    import json
    from websocket import create_connection

    msg = {'cmd': ...}
    msg_json = json.dumps(msg)

    ws = create_connection('ws://localhost:8880/')
    ws.send(msg_json)
    ws.close()
    ```

    """
    DEF_PORT = 8880

    def __init__(self,
                 wav_mode=Player.WAVMODE_NONE,
                 host="0.0.0.0", port=DEF_PORT,
                 wavdir='wav',
                 debug=False):
        """ Constructor

        Parameters
        ----------
        wav_mode: int
            wav file mode
        host: str
            host
        port: int
            port number
        wavdir: str
            wav file directory
        """
        self._dbg = debug
        self._log = get_logger(self.__class__.__name__, self._dbg)
        self._log.debug('wav_mode=%s', wav_mode)
        self._log.info('host:port=%s:%s', host, port)
        self._log.debug('wavdir=%s', wavdir)

        self._wav_mode = wav_mode
        self._port = port
        self._host = host
        self._wavdir = wavdir

        self._player = Player(wav_mode=self._wav_mode,
                              wavdir=self._wavdir, debug=self._dbg)

        self._start_svr = websockets.serve(self.handle, host, port)
        self._loop = asyncio.get_event_loop()

    def main(self):
        """ main
        """
        self._log.debug('')

        self._log.debug('start server ..')
        self._loop.run_until_complete(self._start_svr)

        self._log.info('run_forever() ..')
        self._loop.run_forever()

    def end(self):
        """ Call at the end of program
        """
        self._log.debug('doing ..')

        self._player.end()

        self._log.debug('done')

    async def handle(self, websock, path):
        """
        request handler

        Parameters
        ----------
        websock: dict
        path: str
        """
        self._log.debug('websock=%s:%s, path=%s',
                        websock.local_address, websock.host, path)

        msg = await websock.recv()
        self._log.info('msg=%s', msg)

        try:
            data = json.loads(msg)
            self._log.debug('data=%s', data)
        except json.decoder.JSONDecodeError as ex:
            self._log.error('%s: %s. msg=%s', type(ex), ex, msg)
            return

        self._log.debug('received command: %a', data['cmd'])
        self._log.debug('data=%s', data)

        try:
            cmd = data['cmd']
        except KeyError as ex:
            self._log.error('%s: %s. data=%s', type(ex), ex, data)
            return

        if cmd in ('single_play', 'single', 'play', 'P'):
            try:
                ch_list = data['ch']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)
                return

            self._player.single_play(ch_list)
            return

        if cmd in ('music_load', 'music', 'load', 'l'):
            try:
                music_data = data['music_data']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)
                return

            self._player.music_load(music_data)
            return

        if cmd in ('music_play', 'start', 's'):
            self._player.music_play()
            return

        if cmd in ('music_pause', 'pause', 'p'):
            self._player.music_pause()
            return

        if cmd in ('music_rewind', 'rewind', 'r'):
            self._player.music_rewind()
            return

        if cmd in ('music_seek', 'seek'):
            self._player.music_seek_percent(data['pos'])
            return

        if cmd in ('music_shift', 'shift'):
            self._player.music_shift_percent(data['pos'])
            print('AAA')
            return

        if cmd in ('music_stop', 'stop', 'S'):
            self._player.music_stop()
            return

        if cmd in ('music_wait', 'wait', 'w'):
            self._player.music_wait()
            return

        if cmd in ('calibrate',):
            try:
                ch = int(data['ch'])
                on = data['on']
                pw_diff = data['pw_diff']
                tap = data['tap']
            except KeyError as ex:
                self._log.error('%s: %s. data=%s', type(ex), ex, data)

            self._player.calibrate(ch, on, pw_diff, tap)
            return


#!/usr/bin/env python
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(0,os.path.dirname(os.path.dirname(currentdir)))

import numpy as np
import time

from Modelling.system_params import SystemParams
from Helpers.ros_manager import ros_manager
import Helpers.kinematics_helper as kh

from cvxopt import matrix, solvers
solvers.options['show_progress'] = False
solvers.options['reltol'] = 1e-6
solvers.options['abstol'] = 1e-6
solvers.options['feastol'] = 1e-6

class feasible_motion_estimator(object):
    def __init__(self,pivot_params):
        self.pivot_params = pivot_params

        self.hand_normal_ee = np.array([1.0,0.0,0.0])
        self.hand_tangent_ee = np.array([0.0,1.0,0.0])
        self.hand_z_ee = np.array([0.0,0.0,1.0])

        return None

    def load_system_state(self,system_state):

        self.l_contact = system_state['l_contact']

        self.theta_hand = system_state['theta_hand']

        # R2C is the matrix that maps 2-D wrench vectors in the end-effector frame [F_n,F_t,Tau_z] (in EE frame)
        #to corresponding 2-D wrench vectors in the world_manipulation frame [F_n,F_t,Tau_z] (in world manipulation frame)
        #we make assumption that the Z axes of the EE frame and world manipulation frames are parrallel to one another
        #theta_hand is the counterclockwise rotation of the end-effector about its Z axis
        self.R2C = np.array([[-np.cos(self.theta_hand), np.sin(self.theta_hand), 0.],
                             [-np.sin(self.theta_hand),-np.cos(self.theta_hand), 0.],
                             [                      0.,                      0., 1.]])

        # C2R is the matrix that maps 2-D wrench vectors in the world_manipulation frame [F_n,F_t,Tau_z] (in world manipulation frame)
        #to corresponding 2-D wrench vectors in the end-effector frame [F_n,F_t,Tau_z] (in EE frame)
        #we make assumption that the Z axes of the EE frame and world manipulation frames are parrallel to one another
        #theta_hand is the counterclockwise rotation of the end-effector about its Z axis
        self.C2R = np.array([[-np.cos(self.theta_hand),-np.sin(self.theta_hand), 0.],
                             [ np.sin(self.theta_hand),-np.cos(self.theta_hand), 0.],
                             [                      0.,                      0., 1.]])

        self.ee_pose_homog = system_state['ee_pose_homog']
        self.hand_normal_world = self.R2C[:,0]
        self.hand_tangent_world = self.R2C[:,1]
        self.hand_z_world = self.R2C[:,2]

        self.vertex_array = system_state['vertex_array']
        self.contact_indices = system_state['contact_indices']
        self.mgl_cos_theta_array = system_state['mgl_cos_theta_array']
        self.mgl_sin_theta_array = system_state['mgl_sin_theta_array']

        self.measured_world_manipulation_wrench = system_state['measured_world_manipulation_wrench']
        self.measured_ee_wrench = system_state['measured_ee_wrench']

        self.friction_parameter_dict = system_state['friction_parameter_dict']

        self.single_pivot_contact_index = self.contact_indices [0]

    def check_motion_directions(self):

        test = self.test_slide_right_ground_feasible_single_contact()

        str_out = ''
        if test:
            # print('slide right ground')

            str_out+='rg '

        test = self.test_slide_left_ground_feasible_single_contact()

        if test:
            # print('slide left ground')

            str_out+='lg '

        test = self.test_slide_right_contact_feasible_single_contact()

        if test:
            # print('slide right contact')
            str_out+='rc '

        test = self.test_slide_left_contact_feasible_single_contact()

        if test:
            # print('slide left contact')
            str_out+='lc '

        lb,ub = self.compute_single_contact_pivot_range()

        lb*=180/np.pi
        ub*=180/np.pi

        lb = round(lb)
        ub = round(ub)

        print(str_out)
        print('min_theta (deg): ',lb,'max_theta (deg): ',ub)
        print()

    def test_slide_right_ground_feasible_single_contact(self):
        inequality_constraints_enforced =  [self.friction_right_contact_constraint,
                                            self.friction_left_contact_constraint,
                                            self.torque_right_contact_constraint,
                                            self.torque_left_contact_constraint,
                                            self.normal_force_max_contact_constraint,
                                            self.normal_force_min_contact_constraint,
                                            self.normal_force_min_external_constraint,
                                            self.friction_left_external_constraint]

        equality_constraints_enforced = [self.torque_single_pivot_constant_angle_constraint]
        inequality_constraints_violated = [self.friction_right_external_constraint]

        can_move = self.check_single_contact_sliding_motion_feasibility(inequality_constraints_enforced,equality_constraints_enforced,inequality_constraints_violated,
                                    enforcement_margin = 0., violation_margin = 0.)

        return can_move

    def test_slide_left_ground_feasible_single_contact(self):
        inequality_constraints_enforced =  [self.friction_right_contact_constraint,
                                            self.friction_left_contact_constraint,
                                            self.torque_right_contact_constraint,
                                            self.torque_left_contact_constraint,
                                            self.normal_force_max_contact_constraint,
                                            self.normal_force_min_contact_constraint,
                                            self.normal_force_min_external_constraint,
                                            self.friction_right_external_constraint]

        equality_constraints_enforced = [self.torque_single_pivot_constant_angle_constraint]
        inequality_constraints_violated = [self.friction_left_external_constraint]

        can_move = self.check_single_contact_sliding_motion_feasibility(inequality_constraints_enforced,equality_constraints_enforced,inequality_constraints_violated,
                                    enforcement_margin = 0., violation_margin = 0.)

        return can_move

    def test_slide_right_contact_feasible_single_contact(self):
        inequality_constraints_enforced =  [self.friction_left_contact_constraint,
                                            self.torque_right_contact_constraint,
                                            self.torque_left_contact_constraint,
                                            self.normal_force_max_contact_constraint,
                                            self.normal_force_min_contact_constraint,
                                            self.normal_force_min_external_constraint,
                                            self.friction_left_external_constraint,
                                            self.friction_right_external_constraint]

        equality_constraints_enforced = [self.torque_single_pivot_constant_angle_constraint]
        inequality_constraints_violated = [self.friction_right_contact_constraint]

        can_move = self.check_single_contact_sliding_motion_feasibility(inequality_constraints_enforced,equality_constraints_enforced,inequality_constraints_violated,
                                    enforcement_margin = 0., violation_margin = 0.)

        return can_move

    def test_slide_left_contact_feasible_single_contact(self):
        inequality_constraints_enforced =  [self.friction_right_contact_constraint,
                                            self.torque_right_contact_constraint,
                                            self.torque_left_contact_constraint,
                                            self.normal_force_max_contact_constraint,
                                            self.normal_force_min_contact_constraint,
                                            self.normal_force_min_external_constraint,
                                            self.friction_left_external_constraint,
                                            self.friction_right_external_constraint]

        equality_constraints_enforced = [self.torque_single_pivot_constant_angle_constraint]
        inequality_constraints_violated = [self.friction_left_contact_constraint]

        can_move = self.check_single_contact_sliding_motion_feasibility(inequality_constraints_enforced,equality_constraints_enforced,inequality_constraints_violated,
                                    enforcement_margin = 0., violation_margin = 0.)

        return can_move

    def compute_single_contact_pivot_range(self):
        theta_max = np.pi/2
        theta_min = self.theta_hand

        while theta_max-theta_min>.01:
            theta_test = (theta_max+theta_min)/2.0

            test_val = self.single_contact_pivot_check_theta(theta_test)

            if test_val:
                theta_min = theta_test
            else:
                theta_max = theta_test

        upper_bound = theta_test

        theta_max = self.theta_hand
        theta_min = -np.pi/2

        while theta_max-theta_min>.01:
            theta_test = (theta_max+theta_min)/2.0

            test_val = self.single_contact_pivot_check_theta(theta_test)

            if test_val:
                theta_max = theta_test
            else:
                theta_min = theta_test

        lower_bound = theta_test

        return lower_bound,upper_bound

    def single_contact_pivot_check_theta(self,theta_test):

        delta_theta = theta_test-self.theta_hand

        rot_mat = np.array([[ np.cos(delta_theta),-np.sin(delta_theta), 0.],
                            [ np.sin(delta_theta), np.cos(delta_theta), 0.],
                            [                   0.,                 0., 1.]])

        contact_constraints =  [self.friction_right_contact_constraint,
                                self.friction_left_contact_constraint,
                                self.torque_right_contact_constraint,
                                self.torque_left_contact_constraint,
                                self.normal_force_max_contact_constraint,
                                self.normal_force_min_contact_constraint]

        Aiq0, Biq0 = self.stack_constraints(contact_constraints,is_equality = False)

        equality_constraint = [self.torque_single_pivot_constant_angle_constraint]

        Aiq1, Biq1 = self.stack_constraints(equality_constraint,is_equality = True)

        #Aiq1[0] * delta_W + torque_term_increment == Biq1[0]
        #Aiq1[1] * delta_W - torque_term_increment == Biq1[1]
        #thus:
        #Aiq1[0] * delta_W == Biq1[0] - torque_term_increment
        #Aiq1[1] * delta_W == Biq1[1] + torque_term_increment

        torque_term_increment = self.mgl_cos_theta_array*(np.cos(theta_test)-np.cos(self.theta_hand)) + self.mgl_sin_theta_array*(np.sin(theta_test)-np.sin(self.theta_hand))

        Biq1[0]-=torque_term_increment
        Biq1[1]+=torque_term_increment

        external_constraints = [self.normal_force_min_external_constraint,
                                self.friction_left_external_constraint,
                                self.friction_right_external_constraint]

        Aiq2, Biq2 = self.stack_constraints(external_constraints,is_equality = False)

        Aiq2 = np.dot(Aiq2,rot_mat)

        A = np.vstack([Aiq0,Aiq1,Aiq2])
        B = np.hstack([Biq0-np.dot(Aiq0,self.measured_ee_wrench),Biq1,Biq2-np.dot(Aiq2,self.measured_ee_wrench)])

        C = np.array([1.0,0.0,0.0])

        sol = solvers.lp(matrix(C),matrix(A),matrix(B))

        if sol['x'] is not None:
            return True

        else:
            return False

    def check_single_contact_sliding_motion_feasibility(self,inequality_constraints_enforced,equality_constraints_enforced,inequality_constraints_violated,
                                    enforcement_margin = .5, violation_margin = 1.0):
        Aiq_enforced, Biq_enforced = self.stack_constraints(inequality_constraints_enforced,is_equality = False)
        Aeq_enforced, Beq_enforced = self.stack_constraints(equality_constraints_enforced,is_equality = True)
        Aiq_violated, Biq_violated = self.stack_constraints(inequality_constraints_violated,is_equality = False)

        A = np.vstack([np.array(Aiq_enforced),np.array(Aeq_enforced)])
        q = Biq_enforced-np.dot(Aiq_enforced,self.measured_ee_wrench)-enforcement_margin
        B = np.hstack([q, Beq_enforced])

        B_check = Biq_violated-np.dot(Aiq_violated,self.measured_ee_wrench)

        Amatrix = matrix(A)
        Bmatrix = matrix(B)

        for i in range(len(Aiq_violated)):
            sol = solvers.lp(matrix(-Aiq_violated[i]),Amatrix,Bmatrix)

            if sol['x'] is not None:

                if -sol['primal objective']>=B_check[i]+violation_margin:
                    return True

        return False

    def stack_constraints(self,constraints_list, is_equality = False):
        A, B = [], []
        for constraint in constraints_list:
            a, b = constraint()

            if a.ndim == 1:
                A.append(a)
                B.append(b)

                if is_equality:
                    A.append(-a)
                    B.append(-b)

            else:
                for i in range(len(b)):
                    A.append(a[i])
                    B.append(b[i])

                    if is_equality:
                        A.append(-a[i])
                        B.append(-b[i])

        return np.array(A), np.array(B)

    def friction_right_contact_constraint(self):
        ''' right (i.e., positive) boundary of friction cone '''

        aiq = np.array(self.friction_parameter_dict['acr'])

        biq = self.friction_parameter_dict['bcr']

        return aiq, biq

    def friction_left_contact_constraint(self):
        ''' right (i.e., positive) boundary of friction cone '''

        aiq = np.array(self.friction_parameter_dict['acl'])

        biq = self.friction_parameter_dict['bcl']

        return aiq, biq

    def torque_right_contact_constraint(self):
        ''' right (i.e., positive) boundary of torque cone '''

        aiq = np.array([-self.l_contact  / 2., 0., 1.])
        aiq/=np.abs(aiq[0])

        biq = 0.0

        return aiq, biq

    def torque_left_contact_constraint(self):
        ''' left (i.e., negative) boundary of torque cone '''

        aiq = np.array([-self.l_contact  / 2., 0., -1.])
        aiq/=np.abs(aiq[0])

        biq = 0.0

        return aiq, biq

    def normal_force_max_contact_constraint(self):
        ''' maximum applied normal force at contact constraint '''

        aiq = np.array([1., 0., 0.])

        biq = self.pivot_params['Nmax_contact']

        return aiq, biq

    def normal_force_min_contact_constraint(self):
        ''' minimum applied normal force at contact constraint '''

        aiq = np.array([-1., 0., 0.])

        biq = -self.pivot_params['Nmin_contact']

        return aiq, biq

    def normal_force_min_external_constraint(self):
        ''' normal force at external contact must be above 0 '''

        aiq = np.array([1., 0., 0.])
        aiq = np.dot(aiq,self.R2C)

        biq = 0.

        return aiq, biq

    def friction_right_external_constraint(self):
        ''' right (i.e., positive) boundary of friction cone '''

        aiq = self.friction_parameter_dict['aer']
        aiq = np.dot(aiq,self.R2C)

        biq = np.array(self.friction_parameter_dict['ber'])

        return aiq, biq

    def friction_left_external_constraint(self):
        ''' left (i.e., negative) boundary of friction cone '''

        aiq = self.friction_parameter_dict['ael']
        aiq = np.dot(aiq,self.R2C)

        biq = np.array(self.friction_parameter_dict['bel'])

        return aiq, biq

    def torque_single_pivot_constant_angle_constraint(self):
        ''' 0 net torque w/respect to single external contact '''
        contact_index = self.single_pivot_contact_index

        contact_vertex = self.vertex_array[:,contact_index]
        r_moment_arm = np.array([self.ee_pose_homog[0,3]-contact_vertex[0],self.ee_pose_homog[1,3]-contact_vertex[1]])

        aiq = np.array([ r_moment_arm[0],-r_moment_arm[1], 1.])
        aiq = np.dot(aiq,self.R2C)

        biq = 0.

        return aiq, biq

if __name__ == '__main__':
    global rospy

    # load params
    node_name = 'test_possible_direction_estimator'

    sys_params = SystemParams()

    l_contact = sys_params.object_params['L_CONTACT_MAX']

    controller_params = sys_params.controller_params
    initial_object_params = sys_params.object_params

    RATE = controller_params['RATE']

    use_load = False

    rm = None
    fname = None
    path = None
    if use_load:
        #use if playing from pickle file
        path = '/home/thecube/Documents/pbal_experiments/gtsam_test_data_fall_2022'
        # path = '/home/taylorott/Documents/experiment_data/gtsam_test_data_fall_2022'
        fname = '/test_data-experiment0024.pickle'
        rm = ros_manager(load_mode = True, path=path, fname=fname)

    else:
        #use if running live
        rm = ros_manager()

    if rm.load_mode:
        rm.setRate(RATE)
    else:
        import rospy
        rospy.init_node(node_name)
        rate = rospy.Rate(RATE)

    rm.subscribe_to_list(['/end_effector_sensor_in_end_effector_frame',
                          '/end_effector_sensor_in_world_manipulation_frame',
                          '/ee_pose_in_world_manipulation_from_franka_publisher',
                          '/friction_parameters',
                          '/polygon_contact_estimate',],True)

    # rm.subscribe_to_list([],False)

    rm.wait_for_necessary_data()

    motion_estimator = feasible_motion_estimator(sys_params.pivot_params)

    # hand_tangent = np.array([0.0, 1.0, 0.0, 0.0])
    # hand_normal = np.array([1.0, 0.0, 0.0, 0.0])
    # hand_front_center = np.array([0.0, 0.0, .041, 1.0])

    system_state = {}
    system_state['l_contact'] = l_contact

    while (rm.load_mode and rm.read_still_running()) or (not rm.load_mode and not rospy.is_shutdown()):
        rm.unpack_all()

        system_state['theta_hand'] = kh.quatlist_to_theta(rm.ee_pose_in_world_manipulation_list[3:])

        system_state['ee_pose_homog'] = rm.ee_pose_in_world_manipulation_homog

        # hand_front_center_world = np.dot(rm.ee_pose_in_world_manipulation_homog,hand_front_center)
        # hand_normal_world = np.dot(rm.ee_pose_in_world_manipulation_homog,hand_normal)
        # hand_tangent_world = np.dot(rm.ee_pose_in_world_manipulation_homog,hand_tangent)

        system_state['vertex_array'] = rm.polygon_contact_estimate_dict['vertex_array']

        system_state['contact_indices'] = rm.polygon_contact_estimate_dict['contact_indices']
        system_state['mgl_cos_theta_array'] = rm.polygon_contact_estimate_dict['mgl_cos_theta_array']
        system_state['mgl_sin_theta_array'] = rm.polygon_contact_estimate_dict['mgl_sin_theta_array']

        system_state['measured_world_manipulation_wrench'] = np.array(rm.measured_world_manipulation_wrench)
        system_state['measured_ee_wrench'] = np.array(rm.measured_contact_wrench)

        system_state['friction_parameter_dict'] = rm.friction_parameter_dict

        motion_estimator.load_system_state(system_state)
        motion_estimator.check_motion_directions()

        # if len(contact_indices)==1:
        #     contact_index = contact_indices[0]
        #     contact_vertex = vertex_array[:,contact_index]

        #     mgl_cos_theta = mgl_cos_theta_array[contact_index]
        #     mgl_sin_theta = mgl_sin_theta_array[contact_index]

        #     net_gravity_torque =  mgl_cos_theta*np.cos(theta_hand)+mgl_sin_theta*np.sin(theta_hand)

        #     r_moment_arm = np.array([hand_front_center_world[0]-contact_vertex[0],hand_front_center_world[1]-contact_vertex[1]])

        #     net_hand_torque = r_moment_arm[0]*measured_world_manipulation_wrench[1]-r_moment_arm[1]*measured_world_manipulation_wrench[0]+measured_world_manipulation_wrench[2]

        #     max_hand_torque = np.linalg.norm(r_moment_arm)*np.linalg.norm(measured_world_manipulation_wrench[0:2])+abs(measured_world_manipulation_wrench[2])

        #     net_torque = net_gravity_torque+net_hand_torque

        if rm.load_mode:
            rm.sleep()
        else:
            rate.sleep()


# Data Preprocessing Template

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
df=pd.read_csv("Social_Network_Ads.csv")
df.head()
X_data=df.iloc[:,:2].values
Y_data=df.iloc[:,-1:].values
print(X_data);
print(Y_data);
#Handling Missing Values
from sklearn.impute import SimpleImputer
SI=SimpleImputer(missing_values=np.nan,strategy="mean")
SI.fit_transform(X_data)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split

X_train,X_test,Y_train,Y_test=train_test_split(X_data,Y_data,test_size=0.25,random_state=None)
print(X_test)
print

#Standerdisation and Normalization
from sklearn.preprocessing import StandardScaler

#we will fit the standerd scaler object with Xtrain set because it can create information leakage if we
#Normelize  the data befoe scaling ; after fitting the scaler object we will use that object instant to transform
# Test set or in future for predicting the target lable out of a feature set
#we use Normelization when the data is Normally Distributed
#otherwise Standerization

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test= sc.transform(X_test)
print(X_train)
print(X_test)

#####################

# Building Logistic regression MOdel

#####################

from sklearn.linear_model import LogisticRegression
classifier=LogisticRegression(random_state=0)
classifier.fit(X_train,Y_train)

######
# Predicting the test result
######
Y_hat_test = classifier.predict(X_test)

print(np.concatenate((Y_hat_test.reshape(len(Y_hat_test),1),Y_test.reshape(len(Y_test),1)),1))

####
## CONFUSION METRIX
####

##confusion metrix will tel us about the fasle positive
## False Negetive ,True Positive and True negetive
from sklearn.metrics import confusion_matrix
CM=confusion_matrix(Y_test,Y_hat_test)
print(CM)
###
###CALCULATING THE ACCURACY OF OUR MODEL

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(Y_test,Y_hat_test)

print(accuracy)

##########
# Visualizing the TRaining Set Result
##########


import warnings

import numpy as np
import scipy.signal as signal
import torch
from scipy.ndimage.filters import gaussian_filter1d
import time

class GAUS1DFilter:
    """
    Args:
        x (np.ndarray): input pose
        window_size (int, optional): for median filters (must be odd).
        sigma (float, optional): Sigma for gaussian smoothing.

    Returns:
        np.ndarray: Smoothed poses
    """

    def __init__(self, cfg):
        super(GAUS1DFilter, self).__init__()

        self.window_size = cfg.EVALUATE.TRADITION_GAUS1D.WINDOW_SIZE
        self.sigma = cfg.EVALUATE.TRADITION_GAUS1D.SIGMA

    def __call__(self, x=None):
        if self.window_size % 2 == 0:
            window_size = self.window_size - 1
        else:
            window_size = self.window_size
        if window_size > x.shape[0]:
            window_size = x.shape[0]
        if len(x.shape) != 3:
            warnings.warn('x should be a tensor or numpy of [T*M,K,C]')
        assert len(x.shape) == 3
        x_type = x
        if isinstance(x, torch.Tensor):
            if x.is_cuda:
                x = x.cpu().numpy()
            else:
                x = x.numpy()

        T,K,C=x.shape
        x=x.reshape(T,K*C)

        smooth_poses=np.empty((0,K*C))
        start = time.time()

        for i in range(T//window_size):
            smooth_poses=np.concatenate((smooth_poses,gaussian_filter1d(input=x[i*window_size:(i+1)*window_size,:],
                                        sigma=self.sigma,
                                        axis=0)),0)
        smooth_poses=np.concatenate((smooth_poses,x[(T//window_size)*window_size:,:]),0)
        end = time.time()
        inference_time=end-start
        smooth_poses=smooth_poses.reshape(T,K,C)

        if isinstance(x_type, torch.Tensor):
            # we also return tensor by default
            if x_type.is_cuda:
                smooth_poses = torch.from_numpy(smooth_poses).cuda()
            else:
                smooth_poses = torch.from_numpy(smooth_poses)

        return smooth_poses,inference_time


import h5py
import sys
import os

# Simple tool to help you with getting a list of entries and channels
# you want to perform spike sorting on.

def main(filename):
    with h5py.File(filename) as f:
        entries = sorted(list(f.keys()))

        datasets = None
        for e in entries:
            try:
                entry = f[e]
                datasets = sorted(list(entry.keys()))
                if len(datasets) > 0:
                    break
            except:
                pass

        print('Entries in {0}:'.format(filename))
        print(entries)
        print('\n Datasets in {0}:'.format(entry))
        print(datasets)

if __name__ == '__main__':
    filename = sys.argv[1]
    if not os.path.isfile(filename):
        print("USAGE: listlist.py filename")
    else:
        main(filename)


"""Solution to day 01 of the Advent of Code.

Reading input:
    Each line in the input file can be read as an integer. Using Python's int
    function parses positive and negative numbers equally well, so we will use
    that. See _read_changes function.

Changing frequency:
    Applying a change to an existing frequency as simple as adding the two
    integers that represent the frequency and the frequency change. See
    _apply_change function.

Part 1:
    Getting the final frequency means summing up all the frequency changes in
    our input. We could do this in a single line of Python (yay for list
    comprehensions!) but we'll use our _apply_change function instead. See
    get_final_frequency function.

Part 2:
    To find out which frequency appears twice first, we keep track of all
    frequencies we've seen in a set (not a list, because that would be much
    slower!). See get_first_repetition function.
"""

from typing import List

import utils

def _read_changes(input_string: str) -> List[int]:
    """Reads frequency changes from a given input string.

    Args:
        input_string: A string containing the day's input.

    Returns:
        A list integers representing changes.
    """
    return [int(line) for line in input_string.split('\n')]

def _apply_change(frequency: int, change: int) -> int:
    """Applies a change to a frequency.

    Args:
        frequency: A integer representation of the frequency to change.
        change: An integer representing the change to apply.

    Returns:
        An integer representing the new frequency.
    """
    return frequency + change

def get_final_frequency(input_string: str) -> int:
    """Computes the final frequency given an input string.

    Args:
        input_string: The puzzle input.

    Returns:
        The final frequency after applying all changes.
    """
    changes = _read_changes(input_string)
    frequency = 0
    for change in changes:
        frequency = _apply_change(frequency, change)
    return frequency

def get_first_repetition(input_string: str) -> int:
    """Finds the first repetition given an input string.

    Args:
        input_string: The puzzle input.

    Returns:
        The first frequency to appear twice.
    """
    changes = _read_changes(input_string)
    frequency = 0
    seen_frequencies = {frequency}
    while True:
        for change in changes:
            frequency = _apply_change(frequency, change)
            if frequency in seen_frequencies:
                return frequency
            seen_frequencies.add(frequency)

def _run_tests() -> None:
    """Tests solution."""
    assert get_final_frequency('+1\n+1\n+1') == 3
    assert get_final_frequency('+1\n+1\n-2') == 0
    assert get_final_frequency('-1\n-2\n-3') == -6
    assert get_first_repetition('+1\n-1') == 0
    assert get_first_repetition('+3\n+3\n+4\n-2\n-4') == 10
    assert get_first_repetition('-6\n+3\n+8\n+5\n-6') == 5
    assert get_first_repetition('+7\n+7\n-2\n-7\n-4') == 14

def _print_answers(final_frequency: int = None,
                   first_repetition: int = None) -> None:
    """Prints answers.

    Args:
        final_frequency: The answer to the first part of the day's puzzle.
        first_repetition: The answer to the second part of the day's puzzle.
    """
    print('Answers for day 01:')
    print(f'  Final frequency: {final_frequency}')
    print(f'  First repetition: {first_repetition}')

def main() -> None:
    """Runs tests and prints answers to day's puzzle."""
    _run_tests()
    input_string = utils.read_input(1)
    final_frequency = get_final_frequency(input_string)
    first_repetition = get_first_repetition(input_string)
    _print_answers(final_frequency, first_repetition)

if __name__ == '__main__':
    main()