import unittest
from http import server

import engine

class MyTestCase(unittest.TestCase):
    def test_something(self):
        self.assertEqual(True, False)  # add assertion here
        s = server.HTTPServer(("", 8000), server.SimpleHTTPRequestHandler)
        s.serve_forever()

def test_for_normal_routine():
    r = engine.Engine()
    r.add_route("/ping", lambda c: print(c), "GET")  # add a get route
    r.run_server()

if __name__ == '__main__':
    # unittest.main()
    test_for_normal_routine()


from turtle import Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time

screen = Screen()
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
scoreboard = Scoreboard()

def screen_setup():
    screen.setup(width=800, height=600)
    screen.bgcolor("black")
    screen.title("Pong Game!")
    screen.tracer(0)
    screen.listen()
    screen.onkey(fun=r_paddle.up, key="Up")
    screen.onkey(fun=r_paddle.down, key="Down")
    screen.onkey(fun=l_paddle.up, key="w")
    screen.onkey(fun=l_paddle.down, key="s")

def main():
    screen_setup()
    game_is_on = True
    while game_is_on:
        #for better ball visibility
        time.sleep(ball.move_speed)
        screen.update()
        ball.move()

        #detect ball collision with top and bottom wall
        if ball.ycor() > 280 or ball.ycor() < -280:
            #need to bounce
            ball.bounce_y()

        #detect ball collision with paddle
        if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
            #need to bounce
            ball.bounce_x()

        #detect right paddle misses the ball
        if ball.xcor() > 380:
            ball.reset_position()
            scoreboard.l_point()

        #detect left paddle misses the ball
        if ball.xcor() < -380:
            ball.reset_position()
            scoreboard.r_point()

        #first to 5 points wins
        if scoreboard.l_score == 5 or scoreboard.r_score == 5:
            game_is_on = False
            scoreboard.game_over()

    screen.exitonclick()

main()


from cmath import sqrt
import numpy as np
import matplotlib.pyplot as plt

def line(b):
    return (((b - 17.65)/12.37)**2)

mm_40 = np.loadtxt("wave_40_mm.txt", dtype=complex)
mm_80 = np.loadtxt("wave_80_mm.txt", dtype=complex)
mm_120 = np.loadtxt("wave_120_mm.txt", dtype=complex)
x_120 = np.arange(1, 975, 1)
x_80 = np.arange(1, 1041, 1)
x_40 = np.arange(1, 1130, 1)
x_40 = x_40 * 15/ 1131
x_80 = x_80 * 15/ 1041
x_120 = x_120*15/976

i = 0
while i < 1129:
    mm_40[i] =  line(mm_40[i])
    i = i+1

while i < 1040:
    mm_80[i] =  line(mm_80[i])
    i = i+1

while i < 974:
    mm_120[i] =  line(mm_120[i])
    i = i+1
plt.figure(figsize=(10, 5))
#plt.plot(x_40, mm_40)
#plt.plot(x_80, mm_80)
plt.plot(x_120, mm_120)
plt.xlabel(r'$Время, с$')
plt.ylabel(r'$Уровень воды, мм$')
#plt.savefig('120_mm.png')
plt.grid(True)

plt.show()


# p2p.py>

import random
from socket import *
from email import message
import sys
from threading import Thread
import socket
from time import sleep, time
from cv2 import cv2

BUFFER_SIZE = 1024

# Send data
def ClientSend(destination_IP, destination_port):
    # Create a UDP socket
    UDP_IP_ADDRESS = destination_IP
    UDP_PORT_NO = destination_port

    # Request message from user client
    # Message = str.encode("request photo")

    # Read image
    my_img = cv2.imread("image_black.png", cv2.IMREAD_GRAYSCALE)
    # print(my_img)
    # cv2.imshow('My image', my_img)
    # cv2.waitKey(0)

    # sleep(1000)

    # Create a socket with a 1s timeout
    clientSock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
    clientSock.settimeout(1.0)

    # Send data to client
    # print('Ping %d %s' % (1,Message.decode()))

    while True:
         # Send the message using the clientSock
        # clientSock.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT_NO))
        clientSock.sendto(my_img, (UDP_IP_ADDRESS, UDP_PORT_NO))

        # Receive response
        print('waiting to receive')

        # Get the response & extract data
        try:
            data = clientSock.recvfrom(BUFFER_SIZE)
        except TimeoutError:
            print("timeout")
            sleep(0.1)
            continue
        except ConnectionResetError:
            print("server not up")
            sleep(0.1)
            continue
        break

    img = data[0]
    address = data[1]
    print('client received: ' + str(img))

    # Close the socket
    print('closing socket')
    clientSock.close()

# Listen for incoming requests
def ClientReceive(username, source_IP, source_port):
    # Create a UDP socket with use of SOCK_DGRAM for UDP packets
    serverSocket = socket.socket(family=AF_INET, type=SOCK_DGRAM)

    # Assign IP address and port number to socket
    serverSocket.bind((source_IP, source_port))

    while True:
        # Receive the client packet along with the address it is coming from
        connection = serverSocket.recvfrom(BUFFER_SIZE)
        message = connection[0]
        address = connection[1]

        # Set message to the data to be sent back
        my_img = cv2.imread("image_white.png", cv2.IMREAD_GRAYSCALE)

        # The server responds
        print("                                      "+ str(username) + ": " + str(my_img))
        serverSocket.sendto(my_img, address)

def Header():

    return

def Packet(buffer, header, message):
    packet = [header, message]
    return packet

# The connection is defined by a tuple (source IP, source port, destination IP, destination port)
def OpenConnection(username, source_IP, source_port, destination_IP, destination_port):
    Thread(target = ClientSend, args=(destination_IP, destination_port)).start()
    Thread(target = ClientReceive, args=(username, source_IP, source_port)).start()


'''
DOCUMENTATION:

This is a script that requires the Makefile, and a C header and source file with potentiolly malicious OS syscalls to be present in the same folder where it is.

NOTE: This script will process all files in the watched directory (excluding alert files)

NOTE: if pip3 is not installed, install it to add the necesary modulesfor python3.6
NOTE: The scanner machine root is unable to see the watchdog & pyshark when starting the script as daemon so the USER has been specified
NOTE: watchdog package must be installed on machine already with 'sudo pip3 install watchdog'
NOTE: pyshark package must be installed on machine already with 'sudo pip3 install pyshark'

NOTE: This script was tested and can be run with Python 3.6
NOTE: This script will be added to the scanner machine
    - To run the script as a daemon (i.e. background service) make sure systemd works and the pyshark package is installed

    - Add the service name in system folder: sudo nano /etc/systemd/system/scanner_sysCallDetect.service
        - add the following inside:
            [Unit]
            Description=Python script service for processing new snort logs in scanner machine
            After=multi-user.target

            [Service]
            Type=simple
            Restart=always
            Environment=PYTHONUNBUFFERED=1
            ExecStart=/usr/bin/sudo /usr/bin/python3.6 /your/path/scanner_sysCallDetect.py

            [Install]
            WantedBy=multi-user.target

        - Then reload the daemon:
            - sudo systemctl daemon-reload

        - Then enable the service (it will not get disabled if server restarts):
            - sudo systemctl enable scanner_sysCallDetect.service

        - Start the service:
            - sudo systemctl start canner_sysCallDetect.service

        - You can end, restart, or check status of the service with:
            - sudo systemctl {stop | restart | status} scanner_sysCallDetect
NOTE: you may need to set the file permissions to include execute via: sudo chmod 755 scanner_sysCallDetect.service BEFORE you enable the service
'''

import os
import hashlib
import time
import pyshark
from pathlib import Path
import subprocess
import tempfile
from multiprocessing import Queue
import uuid

scanner_FOLDER = "/home/Documents/scan_fldr"
scanner_RECEIVED_LOGS_FLDR = "/home/Documents/Received_Logs"

executed_PCAP_pkt_binaries_hashes = set()
files_processed_or_skipped = set()
files_to_process_queue = Queue()

def checkIfNewLogFile(filePath:str):
    #check if the file is a log file or an alert file (which we will skip)
    if filePath in files_processed_or_skipped:
        return False
    elif "alert" in filePath.rsplit('/', 1)[1]:
        files_processed_or_skipped.add(filePath)
        return False
    else:
        files_processed_or_skipped.add(filePath) # adding this file also to not get the alert again
        print(f"ALERT - Adding new file to eventually process: {filePath}")
        return True

def scanForNewFilesToProcess():
    for fileName in os.listdir(scanner_RECEIVED_LOGS_FLDR):
        full_filePath = scanner_RECEIVED_LOGS_FLDR+"/"+fileName
        if os.path.isfile(full_filePath) and checkIfNewLogFile(full_filePath):
            files_to_process_queue.put(full_filePath)

def processFileForBinaries(filePath:str):
    print(f"ALERT - Extracting binaries from the file: {filePath}")

    full_pcap = pyshark.FileCapture(filePath)

    bin_pkt_num = 0 # FOR DEBUGGING ONLY
    bin_pkt_executed = 0 # FOR DEBUGGING ONLY

    full_pcap_ls = list(full_pcap)
    print(f"ALERT - Extracting a total of {len(full_pcap_ls)} binaries from the file: {filePath}\n")

    for pkt in full_pcap_ls:
        bin_pkt_num += 1
        try:
            bin_val = pkt.tcp.payload.binary_value
            pkt_hash = hashlib.md5(pkt.tcp.payload.binary_value).hexdigest()

            if pkt_hash not in executed_PCAP_pkt_binaries_hashes: # check if the hash of the packet binary has not already been seen
                executed_PCAP_pkt_binaries_hashes.add(pkt_hash)
                executeBinary(bin_val)

                bin_pkt_executed += 1
            else:
                print (f"WARNING - Packet number {bin_pkt_num} for the file {filePath} has already been seen and executed. Skipping")
        except:
            pass
    print(f"ALERT - All {bin_pkt_executed} new binaries from the file {filePath} have been executed and file is processed")

def executeBinary(bin_val):
    # creates a temporary file to store the binary conetn and then executes it
    # after running all neccessary commands and subsequently after execution deletes the file

    fd, path = tempfile.mkstemp()
    try:
        with os.fdopen(fd, 'wb') as tmp:
            tmp.write(bin_val)

        print(f"ALERT - Executing temporary binary file: {path}")

        subprocess.run("make clean", shell=True, stdout=subprocess.DEVNULL)
        subprocess.run("make -f Makefile", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        subprocess.run("dmesg --clear", shell=True, stdout=subprocess.DEVNULL)
        subprocess.run("insmod hooks.ko", shell=True, stdout=subprocess.DEVNULL)

        cmd = f"chmod a+x {path}"
        subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL)

        cmd = f"ls -l {path}"
        subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL)
        subprocess.run("rmmod hooks.ko", shell=True, stdout=subprocess.DEVNULL)

    except Exception as e:
        print(f"ERROR - Error during binary execution: {e}")
    finally:
        os.remove(path)

def main():
    os.chdir(scanner_FOLDER)
    os.makedirs(scanner_RECEIVED_LOGS_FLDR, exist_ok=True)  # silently checks if folder exsits otherwise creates it

    # the main daemon loop function
    while True:

        scanForNewFilesToProcess()

        while not files_to_process_queue.empty(): # process all new files
            file_fullpath = files_to_process_queue.get()
            processFileForBinaries(file_fullpath)

        time.sleep(1)

main()


from django.conf import settings
from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from projects.models import Project, ProjectInvitationToken
from accounts.api.serializers import HttpUserSerializer
from django.utils.http import urlsafe_base64_decode
from django.utils.encoding import force_text, DjangoUnicodeDecodeError
from django.utils import timezone
from rest_framework import status

User = get_user_model()

class GeneralProjectSerializer(serializers.ModelSerializer):
    """
    Serializer class for representing overall information about a project.
    """

    members_count = serializers.SerializerMethodField('get_members_count')

    @staticmethod
    def get_members_count(project):
        # total members count not including the user that makes the request
        return project.members.count() - 1

    class Meta:
        model = Project
        fields = ['id', 'name', 'budget_usd', 'description', 'members_count']

class DetailProjectSerializer(serializers.ModelSerializer):
    """
    Serializer class for representing detailed information about a project.
    """

    members = serializers.SerializerMethodField('get_member_list')

    @staticmethod
    def get_member_list(project):
        serializer = HttpUserSerializer(project.members.all(), many=True)
        return serializer.data

    def validate(self, attrs):
        if not self.instance:
            projects = self.context['projects']
            try:
                _ = projects.get(name__exact=attrs['name'])
                raise ValidationError({'name': 'You already have project with provided name.'})
            except Project.DoesNotExist:
                pass

        desc = attrs.get('description', Project.DEFAULT_DESCRIPTION)

        # verify if description is not an empty string
        attrs['description'] = desc if desc else Project.DEFAULT_DESCRIPTION

        return attrs

    def save(self):
        """
        Creates new or updates existing projects.
        """
        instance = self.instance
        if instance:
            instance.name = self.validated_data['name']
            instance.description = self.validated_data['description']
            instance.save()
        else:
            instance = Project.objects.create(**self.validated_data)
            instance.members.set([self.context['request'].user])

        return instance

    class Meta:
        model = Project
        fields = ['id', 'name', 'budget_usd', 'description', 'members']

class AddMembersSerializer(serializers.Serializer):
    """
    Serializer for adding new members to a project.
    """

    new_members = serializers.ListField(child=serializers.IntegerField(min_value=0), required=True)

    def __init__(self, instance, *args, **kwargs):
        self.instance = instance
        super().__init__(instance=instance, *args, **kwargs)

    def validate_new_members(self, ids):
        if len(ids) == 0:
            raise ValidationError('No members selected.')

        # get user objects associated with passed ids
        associated_id_user_dict = User.objects.in_bulk(ids)
        # check if all passed user ids are valid
        if len(ids) != len(associated_id_user_dict.keys()):
            raise ValidationError('Some of the passed members do not exist.')

        users = associated_id_user_dict.values()
        # check if some of the passed users are already members of the target project
        present_and_new_users_itersect = self.instance.members.filter(id__in=[u.id for u in users])
        # fetching users by len
        cnt = len(present_and_new_users_itersect)
        usernames = [u.username for u in present_and_new_users_itersect]
        if usernames:
            error = (f"User {usernames[0]} is already a member of the project."
                     if cnt == 1 else
                     f"Users {', '.join(usernames)} are already members of the project.")

            raise ValidationError(error)

        # all users exist, and they are not members of the project
        return users

    def save(self, **kwargs):
        """
        Sends invitations to join the project to all the new members.
        """
        manager = self.context['request'].user
        project = self.instance
        for member in self.validated_data['new_members']:
            # if an invitation for the particular user already exists, it's timestamp will be updated
            defaults = {
                'timestamp': timezone.now()
            }
            token, created = ProjectInvitationToken.objects.update_or_create(project=project,
                                                                             manager=manager,
                                                                             new_member=member,
                                                                             defaults=defaults)
            token.save()

        return self.instance

class RemoveMembersSerializer(serializers.Serializer):
    """
    Serializer class for removing members from a project.
    """
    delete_members = serializers.ListField(child=serializers.IntegerField(min_value=0), required=True)

    def __init__(self, instance, *args, **kwargs):
        self.instance = instance
        super().__init__(instance=instance, *args, **kwargs)

    def validate_delete_members(self, ids):
        if len(ids) == 0:
            raise ValidationError('No members selected.')

        if self.context['request'].user.id in ids:
            raise ValidationError('You cannot remove yourself from your own project.')

        return ids

    def save(self, **kwargs):
        self.instance.members.filter(id__in=self.validated_data['delete_members']).delete()
        return self.instance

class ProjectInvitationSerializer(serializers.Serializer):
    """
    Serializer class that represents invitations to join a project.
    """
    token = serializers.CharField(max_length=80, required=True)

    def get_invitation(self, raw_token):
        try:
            token = force_text(urlsafe_base64_decode(raw_token))
            return ProjectInvitationToken.objects.get(key=token, new_member=self.context['request'].user)
        except (ProjectInvitationToken.DoesNotExist, DjangoUnicodeDecodeError):
            raise ValidationError('Invalid token. Make sure you are logged in '
                                  'as the user mentioned in the invitation email.')

    @staticmethod
    def check_invitation_accepted(invitation):
        """
        Verifies the invitation is actual.
        """
        if invitation.accepted:
            raise ValidationError(f'You are already a member of project \'{invitation.project.name}\'',
                                  code=status.HTTP_200_OK)

    @staticmethod
    def check_invitation_expired(invitation):
        """
        Verifies the invitation hasn't expired.
        """
        range_end = timezone.now()
        range_start = range_end - timezone.timedelta(seconds=settings.PROJECT_INVITATION_AGE)
        valid_timestamp = range_start <= invitation.timestamp <= range_end
        if not valid_timestamp:
            raise ValidationError('This invitation has expired.')

    def validate_token(self, raw_token):
        invitation = self.get_invitation(raw_token)
        self.check_invitation_accepted(invitation)
        self.check_invitation_expired(invitation)
        return invitation

    def save(self, **kwargs):
        """
        Add new member to project and make the invitation resolved.
        """
        invitation = self.validated_data['token']

        invitation.project.members.add(invitation.new_member)
        invitation.accepted = True
        invitation.save()
        return invitation


import matplotlib.pyplot as plt
import numpy as np
import math
#设置常量
constant_dx=0.005
constant_c=300.0
constant_dt=constant_dx/constant_c
constant_r=constant_c*constant_dt/constant_dx
total_L=1.0
total_t=0.1
total_M=int(total_L/constant_dx)
total_N=int(total_t/constant_dt)
#初始化Cy
Cy=[[0.0 for i in range(total_M+1)] for n in range (total_N+1)]
for n in range(total_N+1):
    for i in range(total_M+1):
        Cy[n][i]=0.0
Cx0=0.3

#constant_k=1000
#for n in range(total_N+1):
#    for i in range(total_M+1):
#        Cy[n][i]=math.e**(-constant_k*((i*constant_dx-Cx0)**2))
#    Cy[n][0]=0.0
#    Cy[n][-1]=0.0

constant_k=1000
intCx0=int(Cx0/constant_dx)
for n in range(total_N+1):
    for i in range(0,intCx0):
        Cy[n][i]=1.0/0.3*i*constant_dx
    for i in range(intCx0,total_M+1):
        Cy[n][i]=-1.0/0.7*(i*constant_dx-Cx0)+1
    Cy[n][0]=0.0
    Cy[n][-1]=0.0

#计算过程
for n in range(2,total_N):
    for i in range(1,total_M):
        Cy[n+1][i]=2*(1-constant_r**2)*Cy[n][i]-Cy[n-1][i]+(constant_r**2)*(Cy[n][i+1]+Cy[n][i-1])
    Cy[n][0]=0.0
    Cy[n][-1]=0.0
#绘制图像
plt.figure(figsize=(15,15))
plot_cx=[]
plot_cy=[]
for i in range(total_M+1):
    plot_cx.append(i*constant_dx)
    plot_cy.append(Cy[0][i])
plt.subplot(12, 1, 1)
plt.title("problem 6.12(Gaussian profile)")
plt.xlim(0,1)
plt.ylim(-1,1)
plt.plot(plot_cx,plot_cy,color='black',label="0")
for nt in range(11):
    plot_cx=None
    plot_cy=None
    plot_cx=[]
    plot_cy=[]
    for i in range(total_M+1):
        plot_cx.append(i*constant_dx)
        plot_cy.append(Cy[(nt+1)*8][i])
    plt.subplot(12, 1, nt+2)
    plt.xlim(0,1)
    plt.ylim(-1,1)
    plt.plot(plot_cx,plot_cy,color='black',label=str(nt))
plt.show()
plot_ct=[]
plot_cyt=[]
Ctx=0.01
Nposition=int(Ctx/constant_dx)
for i in range(total_N+1):
    plot_ct.append(i*constant_dt)
    plot_cyt.append(Cy[i][Nposition])
plt.figure(figsize=(15,15))
plt.title("problem 6.12(Gaussian profile)")
plt.plot(plot_ct,plot_cyt,color='black')
plt.xlabel("Time(s)")
plt.ylabel("y(m)")
plt.show()


from rapidpro4rapidsms.outgoing import RapidProBackend

from mock import patch, Mock

from django.test import TestCase

from rapidsms.tests.harness import CreateDataMixin

from base import *

__author__ = 'Tomasz J. Kotarba <tomasz@kotarba.net>'
__copyright__ = 'Copyright (c) 2015, Tomasz J. Kotarba. All rights reserved.'
__maintainer__ = 'Tomasz J. Kotarba'
__email__ = 'tomasz@kotarba.net'

class RapidProSendTest(CreateDataMixin, TestCase):
    def setUp(self):
        self.mo_sms_valid_data = mo_sms_valid_data
        self.mt_sent_valid_data = mt_sent_valid_data
        self.mt_dlvd_valid_data = mt_dlvd_valid_data
        self.config = {
            'rapidpro_api_gateway_url': 'https://some.rapidpro.io/api/v1',
            'rapidpro_api_token': 'some-rapidpro-api-token'
        }

    def test_required_fields(self):
        """RapidPro backend requires the RapidPro API URL and API token in its
        configuration.
        """
        self.assertRaises(
            TypeError, RapidProBackend,
        )
        self.assertRaises(
            TypeError, RapidProBackend,
            router=None, name='rapidpro-backend',
            rapidpro_api_gateway_url='',
        )
        self.assertRaises(
            TypeError, RapidProBackend,
            router=None, name='rapidpro-backend',
            rapidpro_api_token=''
        )
        try:
            RapidProBackend(
                router=None, name='rapidpro-backend',
                rapidpro_api_gateway_url='',
                rapidpro_api_token=''
            )
        except TypeError:
            self.fail(
                'This test should not fail when all required keys are '
                'present!'
            )

    def test_rapidpro_api_token_and_gateway_url_passed_to_temba_client(self):
        """RapidPro requires all API calls to authenticate by including an
        Authorization header with a RapidPro API token. For security reasons
        all calls must be made using HTTPS.  We use temba.TembaClient (which is
        an external component created by the developer of RapidPro) to take
        care of that which means that its constructor should be called with two
        arguments - rapidpro_api_gateway_url and rapidpro_api_token - when
        method RapidProBackend._prepare_rapidpro_client() is executed.
        """
        backend = RapidProBackend(None, 'rapidpro-backend', **self.config)
        with patch(
                'temba_client.v1.TembaClient.__init__',
                Mock(return_value=None)
        ) as mock_temba_constructor:
            backend._prepare_rapidpro_client()
        mock_temba_constructor.assert_called_once_with(
            self.config['rapidpro_api_gateway_url'],
            self.config['rapidpro_api_token']
        )

    def test_outgoing_keys_or_keyword_arguments_for_temba(self):
        """RapidPro requires JSON to include 'text' and at least one of:
            - urns
            - contacts
            - groups
        The current implementation supports just urns and uses class
        temba.TembaClient to authenticate with RapidPro servers and send SMS.
        We use method RapidProBackend.prepare_request() to prepare arguments
        for both the constructor of TembaClient and its method create_broadcast.
        """
        message = self.create_outgoing_message()
        backend = RapidProBackend(None, 'rapidpro-backend', **self.config)
        kwargs = backend.prepare_request(
            message.id,
            message.text,
            [message.connections[0].identity],
            {}
        )
        self.assertEqual(
            kwargs['rapidpro_api_gateway_url'],
            self.config['rapidpro_api_gateway_url']
        )
        self.assertEqual(
            kwargs['rapidpro_api_token'],
            self.config['rapidpro_api_token']
        )
        self.assertEqual(
            kwargs['urns'],
            ['tel:' + message.connections[0].identity])
        self.assertEqual(kwargs['text'], message.text)

    def test_send(self):
        """Test successful send."""
        message = self.create_outgoing_message()
        backend = RapidProBackend(None, 'rapidpro-backend', **self.config)
        kwargs = backend.prepare_request(
            message.id,
            message.text,
            [message.connections[0].identity],
            {}
        )
        with patch(
                'temba_client.v1.TembaClient.create_broadcast'
        ) as mock_create_bcast:
            backend.send(
                message.id,
                message.text,
                [message.connections[0].identity],
                {}
            )
        mock_create_bcast.assert_called_once_with(
            text=kwargs['text'],
            urns=kwargs['urns']
        )


"""
vbinds - An interface for working with a playable specializations.
"""

# built-in
import os
from typing import Optional, Tuple, List

# internal
from .engine import Engine
from .talent import Talent

def level_to_index(level: int) -> int:
    """From a talen row's 'level', get the one-indexed translation."""

    level_data = {
        15: 1,
        25: 2,
        30: 3,
        35: 4,
        40: 5,
        45: 6,
        50: 7,
    }
    return level_data[level]

def is_talent_active(talent_data: dict) -> bool:
    """Determine if a talent is active or passive, based on the API data."""

    return talent_data["spell_tooltip"]["cast_time"].lower() != "passive"

def build_row_macro(
    row_idx: int, row_data: dict
) -> Optional[Tuple[str, List[str]]]:
    """
    For a given talent row, build a macro to arbitrate non-passive abilities
    if applicable.
    """

    val = None
    non_passives = []

    for idx, data in row_data.items():
        if is_talent_active(data["raw"]):
            non_passives.append((data["raw"]["talent"]["name"], idx))

    if len(non_passives) > 1:
        strval = "#showtooltip{}/cast ".format(os.linesep)
        for ability in non_passives:
            strval += "[talent:{}/{}] {}; ".format(
                row_idx, ability[1], ability[0]
            )
        strval = strval.rstrip()
        val = strval, strval.split(os.linesep)

    return val

# pylint: disable=too-many-locals
class Specialization:
    """An interface for working with spec data."""

    def __init__(self, engine: Engine, spec_id: int):
        """Initialize spec data from its numeric identity."""

        self.engine = engine
        self.data = engine.get_spec(spec_id)
        assert self.data is not None

        # build the talent rows
        self.talent_rows = {}
        self.levels = {}
        for tier_data in self.data["talent_tiers"]:
            # store talents as one-indexed
            talent_row = {}
            for talent_data in tier_data["talents"]:
                ttip = talent_data["spell_tooltip"]
                f_str = "({}, {}) {}".format(
                    talent_data["talent"]["name"],
                    ttip["cast_time"],
                    ttip["description"],
                )
                talent_row[talent_data["column_index"] + 1] = {
                    "text": f_str,
                    "raw": talent_data,
                }

            # save tiers as one-indexed
            index = level_to_index(tier_data["level"])
            self.talent_rows[index] = talent_row
            self.levels[index] = tier_data["level"]

        # store this spec's talent macros
        self.macros = {}
        for row_idx, row_data in self.talent_rows.items():
            macro = build_row_macro(row_idx, row_data)
            if macro is not None:
                self.macros[row_idx] = macro

        self.name = self.data["name"]

        # build a data structure for serialization
        media = engine.get_spec_media(spec_id)
        assert media is not None
        self.to_serialize = {
            "icon": media["assets"][0]["value"],
            "name": self.name,
            "slug": self.name.lower().replace(" ", "_"),
            "role": self.data["role"]["name"],
            "has_macros": bool(self.macros),
        }
        self.to_serialize["talent_rows"] = []
        for row, data in self.talent_rows.items():
            rdata: dict = {
                "index": row,
                "level": self.levels[row],
                "macro": None,
            }
            if row in self.macros:
                rdata["macro"] = self.macros[row][0]
                rdata["macro_lines"] = self.macros[row][1]
            rdata["talents"] = {}
            for talent_idx, talent_data in data.items():
                tdata = Talent(
                    self.engine, talent_data["raw"]["talent"]["id"]
                ).to_serialize
                tdata["active"] = is_talent_active(talent_data["raw"])
                rdata["talents"][talent_idx] = tdata
            self.to_serialize["talent_rows"].append(rdata)

    def role(self) -> str:
        """
        Determine the role (i.e. 'Damage', 'Tank', 'Healer') for this spec.
        """

        assert self.data is not None
        return self.data["role"]["name"]

    def __str__(self) -> str:
        """Turn the spec into a String for debugging."""

        assert self.data is not None
        title = "{} ({})".format(self.name, self.data["role"]["name"])
        name_border = "-" * len(title)
        lines = [name_border, title, name_border]

        # build the talent-tree "table"
        for row_idx, row_data in self.talent_rows.items():
            lines.append("{}:".format(row_idx))
            for talent_idx, talent_data in row_data.items():
                lines.append(
                    "    {}: {}".format(talent_idx, talent_data["text"])
                )
            macro = build_row_macro(row_idx, row_data)
            if macro is not None:
                lines.append("macro:")
                lines.append(macro[0])

        return os.linesep.join(lines)


'''
Very simple script to monitor the LiDAR temperature and status.
Can also toggle the LiDAR state to Run/Idle

Was a quick hack job... No guarantees are made about how well it all works

By Jasper Harvey - 17/10/2022
'''

import requests
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QWidget, QApplication, QComboBox
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QFont

OFFLINE_DEBUG = False # For development when I dont have access to the LiDAR

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.api_url = "http://192.168.9.10:8007/"

        # Link api call and scan name
        # Typical way to call the API is playtable=playtable-[name].txt
        self.scanOptions = (
            ("128 Point", self.api_url + "playtable/set?playtable=playtable-128_point.txt"),
            ("1000 Point", self.api_url + "playtable/set?playtable=playtable-1000_point.txt"),
            ("128 Point Uniform", self.api_url + "playtable/set?playtable=playtable-128_Uniform.txt"),
            ("96 Point Uniform", self.api_url + "playtable/set?playtable=playtable-96_Uniform.txt"),
        )

        self.toggleButton = QPushButton("Run/Idle")
        self.toggleButton.setFont(QFont("Arial", 20))
        self.toggleButton.pressed.connect(self.toggleMode)

        self.modeBox = QComboBox()
        for item in self.scanOptions:
            self.modeBox.addItem(item[0])

        self.modeOk = QPushButton("Change Mode")
        self.modeOk.clicked.connect(self.changeScanMode)
        self.currentModeLabel = QLabel("Current Mode: \n96 Point")
        self.currentModeLabel.setFont(QFont("Arial", 14))
        modeLabel = QLabel("Set scan mode:")
        modeLabel.setFont(QFont("Arial", 14))

        self.mode = QLabel()
        self.mode.setFont(QFont("Arial", 20))
        self.tx_temp = QLabel()
        self.tx_temp.setFont(QFont("Arial", 20))
        self.rx_temp = QLabel()
        self.rx_temp.setFont(QFont("Arial", 20))
        self.processor_temp = QLabel()
        self.processor_temp.setFont(QFont("Arial", 20))

        Vlayout = QVBoxLayout()
        Vlayout.setSpacing(15)
        Vlayout.addWidget(self.toggleButton, alignment=Qt.AlignTop)
        Vlayout.addWidget(self.mode)
        Vlayout.addWidget(self.tx_temp)
        Vlayout.addWidget(self.rx_temp)
        Vlayout.addWidget(self.processor_temp)

        leftPanel = QWidget()
        leftPanel.setLayout(Vlayout)

        Vlayout = QVBoxLayout()
        Vlayout.addWidget(modeLabel, alignment=Qt.AlignTop)
        Vlayout.addWidget(self.modeBox, alignment=Qt.AlignTop)
        Vlayout.addWidget(self.modeOk)
        Vlayout.addWidget(self.currentModeLabel)
        Vlayout.setSpacing(15)
        Vlayout.addStretch(1)
        rightPanel = QWidget()
        rightPanel.setLayout(Vlayout)

        Hlayout = QHBoxLayout()
        Hlayout.addWidget(leftPanel)
        Hlayout.addWidget(rightPanel)

        w = QWidget()
        w.setLayout(Hlayout)

        self.setCentralWidget(w)
        self.show()

        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.update_stats)
        self.timer.start()

    def update_stats(self):
        temps = self.get_temperature()
        mode = self.get_mode()

        tx_temp = temps["stat_tx_temp"]
        rx_temp = temps["stat_rx_temp"]
        proc_temp = temps["stat_proc_temp"]

        self.tx_temp.setText("TX Temp: " + str(tx_temp/100) + " C")
        self.rx_temp.setText("RX Temp: " + str(rx_temp/100) + " C")
        self.processor_temp.setText("Processor Temp: " + str(proc_temp/100) + " C")

        if proc_temp/100 > 70:
            self.processor_temp.setStyleSheet("background-color: red")
        if proc_temp/100 < 70:
            self.processor_temp.setStyleSheet("background-color: green")

        if mode["Error"] == 0:
            self.mode.setText("Mode: " + mode["Mode"])
        else:
            self.mode.setText("Mode: error" + mode["Error"])

        if mode["Mode"] == "Idle":
            self.mode.setStyleSheet("background-color: green")
        if mode["Mode"] == "Running":
            self.mode.setStyleSheet("background-color: red")

    def get_temperature(self):
        if OFFLINE_DEBUG:
            return {"stat_tx_temp": 2033,
                    "stat_rx_temp": 2213,
                    "stat_proc_temp" : 5535}

        reply = requests.get(self.api_url + "DeviceStatus/Get?stat_tx_temp&stat_rx_temp&stat_proc_temp")
        return reply.json()

    def get_mode(self):
        if OFFLINE_DEBUG:
            return {"Mode" : "Idle",
                    "Error" : 0}

        reply = requests.get(self.api_url + "Mode/Get")
        return reply.json()

    def toggleMode(self):
        current_mode = requests.get(self.api_url + "Mode/Get")

        if current_mode.json()["Mode"] == "Idle":
            change_mode = requests.get(self.api_url + "Mode/Set/Run")

        if current_mode.json()["Mode"] == "Running":
            change_mode = requests.get(self.api_url + "Mode/Set/Idle")

        # print(current_mode.json)

    def changeScanMode(self):
        currentIdx = self.modeBox.currentIndex()
        print(self.scanOptions[currentIdx])
        if OFFLINE_DEBUG:
            return

        try:
            option = self.scanOptions[currentIdx]
            change_mode = requests.post(option[1]).json()
            print(change_mode)

            for items in self.scanOptions:
                if change_mode["result"] in items[1]:
                    self.currentModeLabel.setText("Current Mode: \n" + items[0])

        except IndexError:
            print("Not implemented yet.")
            return

########### Main #############
if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    app.exec_()


from app.src.utils import get_encoded_img, get_image
import app.src.config as config
from flask import Flask, render_template, request, redirect, url_for,jsonify
from app.src.predict import get_prediction

app = Flask(__name__)

def allowed_file(filename):
    return (
        "." in filename
        and filename.rsplit(".", 1)[1].lower() in config.ALLOWED_EXTENSIONS
    )

@app.route('/')
def home():
    return render_template('home.html')

@app.route("/predict", methods=["GET", "POST"])
def predict():
    if request.method == 'POST':
        file = request.files.get('file')
        if file is None or file.filename == "":
            return jsonify({'error': 'no file'})
        if not allowed_file(file.filename):
            return jsonify({'error': 'format not supported'})

        img_byte = file.read()
        img = get_image(img_byte)
        encoded_img = get_encoded_img(img_byte)
        img_data = f"data:image/jpeg;base64,{encoded_img.decode('utf-8')}"
        pred = get_prediction(img)
        print(pred)
        data = {'prediction': pred, 'class_name': pred}
        return render_template('home.html',prediction=pred,img=img_data)

if __name__ == '__main__':
    #host='127.0.0.1'
    app.run(debug=True)
    #app.run(debug=True,host=host)


import serial
from time import sleep
import os.path
import datetime
import psutil
import RPi.GPIO as GPIO
import argparse
import socket

path = "/home/pi/data/power/"

def read_current_and_voltage():
    with serial.Serial('/dev/ttyS0', 9600, timeout=1) as ser:

        try:
            line = ser.readline()
            if len(line) > 0:
                decoded_line = line.decode('utf-8')
                decoded_line = decoded_line.replace('\r\n', '')

                return decoded_line
        except serial.serialutil.SerialException:
            return None

def file_len(name):
    count = 0
    with open(name) as f:
        count = len(f.readlines())
    print(count)
    return count

def save_to_file(file_name, timestamp, data):
    if not os.path.exists(path):
        os.makedirs(path)
    filename = path + file_name
    with open(filename, 'a') as out:
        out.write("{} | ".format(timestamp) + str(data) + '\n')

def get_time():
    time_object = datetime.datetime.now()
    date_time = time_object.strftime("%Y-%m-%d %H:%M:%S")
    date = time_object.strftime("%Y-%m-%d")
    timestamp = int(time_object.timestamp())

    return date_time, date, timestamp

if __name__ == "__main__":
    parser = argparse.ArgumentParser("Experiment")
    parser.add_argument('count', type=int, help='Number of the experiment')

    args = parser.parse_args()
    experiment_nr = args.count
    _, date, _ = get_time()
    hostname = socket.gethostname()
    file_name = "sleepypi2-{}-{}-{}".format(hostname, date, experiment_nr)

    try:
        while True:
            _, _, timestamp = get_time()

            values = read_current_and_voltage()
            if values is not None:
                save_to_file(file_name, timestamp, values)

    except KeyboardInterrupt:
        while values is not None:
            values = read_current_and_voltage()
            if values is not None:
                save_to_file(file_name, timestamp, values)
        # values = read_current_and_voltage()


import socket
import ssl
import core.decryptionScheme as decryptionScheme
import core.tools as tools
from cryptography.fernet import Fernet
import base64
import netifaces
import yaml

COLOR_RESET = '\033[0m'
COLOR_GREEN = '\033[32m'
COLOR_BLUE = '\033[34m'

with open('config.yaml','r') as config:
    data = yaml.safe_load(config)

AUTH_IP_ADDR = data['pkg']['ip']
AUTH_PORT = data['pkg']['port']

interfaces = netifaces.interfaces()

for interface in interfaces:
    addresses = netifaces.ifaddresses(interface)
    if netifaces.AF_INET in addresses:
        ip_addresses = [addr['addr'] for addr in addresses[netifaces.AF_INET]]
        if(ip_addresses[0][:9]=="10.10.100"):
            BOB_IP_ADDR = ip_addresses[0]

try:
    normal_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # print ("Socket successfully created")
except socket.error as err:
    print ("socket creation failed with error %s" %(err))

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_verify_locations("./certificates/ca_certificate.pem")

secure_socket = ssl_context.wrap_socket(normal_socket, server_side=False, server_hostname=data['pkg']['hostname'])
# secure_socket = ssl.wrap_socket(normal_socket, server_side=False)
secure_socket.connect((AUTH_IP_ADDR, AUTH_PORT))

print(secure_socket.recv(1024).decode())
print(secure_socket.recv(1024).decode())
print(secure_socket.recv(1024).decode())
print(secure_socket.recv(1024).decode())
print(secure_socket.recv(1024).decode())

authority_MPK = int(secure_socket.recv(1024).decode())
print(authority_MPK, "AUTH MPK")

identity =  input("Identity: ")
hashed_id = tools.hashIdentity(identity, authority_MPK)

secure_socket.send(identity.encode())

print(secure_socket.recv(1024).decode())
secret_key = secure_socket.recv(1024).decode()

secure_socket.close()

#  Now bob has received its secret key and alice can send any data to it now:

try:
    bob_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
    print ("socket creation failed with error %s" %(err))

bob_socket.bind(('0.0.0.0', 3002))
bob_socket.listen(1)
print("Bob is now open to encrypted Communication")

while True:
    listen = input("Do you want to continue listening?(Y/N)")
    if(listen=="N"):
        bob_socket.close()
        break

    print("To connect, use the following IP : ", BOB_IP_ADDR)
    conn, addr = bob_socket.accept()
    print("You are connected to", addr)

    #  now the key exchange will happen
    print("Sender is now sending encrypted connection key")
    encrypted_str = conn.recv(10240).decode()
    print("Encrypted Communication can start, enter 'baskar()' to exit chat")
    encrypted_list = eval(encrypted_str.split("|")[1])
    connection_key = decryptionScheme.decrypt_sequence(encrypted_list, int(secret_key), hashed_id, authority_MPK)

    # connection key is retrieved and now symmtric encryption can be used
    fernetKey = base64.urlsafe_b64encode(tools.bits_to_key(connection_key))
    fernetObject = Fernet(fernetKey)

    # fernet object created for encryption and decryption
    print("------------------")
    while(True):
        rec = conn.recv(1024).decode()
        if(fernetObject.decrypt(rec).decode().lower()=="baskar()"):
            break
        print(COLOR_GREEN + "SENDER : {}".format(fernetObject.decrypt(rec).decode())+ COLOR_RESET)
        BobIn = input(COLOR_BLUE + "ME : ")
        print(COLOR_RESET, end="")
        if(BobIn.lower()=="baskar()"):
            encryptedMessage = fernetObject.encrypt(BobIn.encode())
            conn.send(encryptedMessage)
            break
        encryptedMessage = fernetObject.encrypt(BobIn.encode())
        conn.send(encryptedMessage)


import urllib.parse
import boto3

index_start = '''

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

'''

index_end = '''

'''

def bucket_to_website(bucket_name):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)

    folder = 'Tapes/'
    ls = bucket.objects.filter(Prefix=folder)
    keys = []
    for item in ls:
        if folder == item.key:
            continue

        key = item.key.replace(folder, '')
        url = 'https://s3.amazonaws.com/%s/%s' % (bucket_name, urllib.parse.quote(item.key))

        keys += [(key, url)]
        print('%s  %s' % (item.key, url))

    pages = index_start
    for key in keys:
        pages += '%s

' % (key[1], key[0])
    pages += index_end

    with open('index.html', 'w', encoding='utf8') as f:
        f.write(pages)

if __name__ == "__main__":
    bucket_to_website('saba-tapes')


#!/usr/bin/env python3
'''
Given a tree T, a start time S, and an end time E, remove all leaves from T that
are outside of the time window [S,E]. Trees must be in the Newick format, and
leaf labels must be delimited by the '|' character, with the last field being
the time associated with the leaf.
'''
from dendropy import Node,Tree
from queue import Queue

# get the label of Dendropy node u
def label(u):
    return str(u.taxon).replace("'",'')

# my own version of extract_tree_with_taxa
def extract_tree_with_taxa(tree, taxa, suppress_unifurcations=True):
    taxon_to_leaf = {}
    for n in tree.preorder_node_iter():
        n.keep = False
        if n.is_leaf():
            taxon_to_leaf[n.taxon] = n
    for t in taxa:
        for n in taxon_to_leaf[t].ancestor_iter(inclusive=True):
            n.keep = True
    out = Tree()
    q_old = Queue(); q_old.put(tree.seed_node)
    q_new = Queue(); q_new.put(out.seed_node)
    while not q_old.empty():
        n_old = q_old.get(); n_new = q_new.get()
        for c_old in n_old.child_node_iter():
            if c_old.keep:
                c_new = Node(taxon=c_old.taxon, label=c_old.label, edge_length=c_old.edge_length); n_new.add_child(c_new)
                q_old.put(c_old); q_new.put(c_new)
    if suppress_unifurcations:
        out.suppress_unifurcations()
    return out

# run main program
if __name__ == "__main__":
    import argparse; from gzip import open as gopen
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-t', '--tree', required=False, type=str, default='stdin', help="Input Tree File")
    parser.add_argument('-s', '--start', required=False, type=float, default=float('-inf'), help="Window Start Time")
    parser.add_argument('-e', '--end', required=False, type=float, default=float('inf'), help="Window End Time")
    parser.add_argument('-o', '--output', required=False, type=str, default='stdout', help="Output File")
    args = parser.parse_args()
    assert args.start != float('-inf') or args.end != float('inf'), "Must specify either start time or end time (or both)"
    if args.tree.lower() == 'stdin':
        from sys import stdin; tree = Tree.get(data=stdin.read(),schema='newick')
    elif args.tree.lower().endswith('.gz'):
        tree = Tree.get(data=gopen(args.tree).read().decode(),schema='newick')
    else:
        tree = Tree.get(data=open(args.tree).read(),schema='newick')
    if args.output == 'stdout':
        from sys import stdout; output = stdout
    else:
        output = open(args.output,'w')
    taxa = [leaf.taxon for leaf in tree.leaf_node_iter() if args.start < float(label(leaf).split('|')[-1]) < args.end]
    #output.write((tree.extract_tree_with_taxa(taxa, suppress_unifurcations=False).as_string('newick')))
    output.write(extract_tree_with_taxa(tree,taxa).as_string('newick').replace("'",''))
    output.write('\n'); output.close()


import pandas as pd
import numpy as np
import math
print('loaded dependencies')
pathTrain = 'C:\\Users\\Ben\\Desktop\\Sandboc\\Untitled Folder\\chapter 3\\Problem 3\\train.csv'
pathTest = 'C:\\Users\\Ben\\Desktop\\Sandboc\\Untitled Folder\\chapter 3\\Problem 3\\test.csv'

trainData = pd.read_csv(pathTrain)
testData = pd.read_csv(pathTest)

def clean_Data(inDF):
	trainDataTrimmed = inDF.drop(['Name','Ticket', 'PassengerId','Cabin'], axis = 1) # Most Cabins are undefined, so it's not wroth convoluting the data
	trainDataTrimmed = trainDataTrimmed.dropna(subset = ['Fare'])
	# Make the sex of each passenger a number
	trainDataTrimmed['Sex'] = trainDataTrimmed['Sex'] .mask(trainDataTrimmed['Sex'] == 'male' , 1)
	trainDataTrimmed['Sex'] = trainDataTrimmed['Sex'] .mask(trainDataTrimmed['Sex'] == 'female' , 2)
	# Make the place they left from a value as well
	trainDataTrimmed['Embarked'] = trainDataTrimmed['Embarked'].mask(trainDataTrimmed['Embarked'] == 'S' , 0)
	trainDataTrimmed['Embarked'] = trainDataTrimmed['Embarked'].mask(trainDataTrimmed['Embarked'] == 'C' , 1)
	trainDataTrimmed['Embarked'] = trainDataTrimmed['Embarked'].mask(trainDataTrimmed['Embarked'] == 'G' , 2)
	trainDataTrimmed['Embarked'] = trainDataTrimmed['Embarked'].mask(trainDataTrimmed['Embarked'] == 'Q' , 3)
	trainDataTrimmed['Embarked'] = trainDataTrimmed['Embarked'].mask(trainDataTrimmed['Embarked'].isnull() , 4) #There are only two null values, so it isn't a big deal
	# Next we convert the cabin to a number so we can make predictions based on that

	# Next we make a prediciction of the age using a classifier. We make the
	trainDataAgeDefined = trainDataTrimmed[trainDataTrimmed['Age'].notnull()]
	trainDataAgeUnDefined = trainDataTrimmed[trainDataTrimmed['Age'].isnull()]

	from sklearn.neighbors import KNeighborsClassifier
	knn_clf_age = KNeighborsClassifier(weights = 'uniform', n_neighbors = 2 )
	knn_clf_age.fit((trainDataAgeDefined.drop('Age',axis = 1)), (trainDataAgeDefined['Age']).astype(int) ) #Labels must be either ints or strings
	trainDataAgeUnDefined['Age'] = knn_clf_age.predict(trainDataAgeUnDefined.drop('Age',axis = 1))
	return pd.concat([trainDataAgeUnDefined , trainDataAgeDefined  ])

"""
# Here I was messing with some parameters to check how well the classifier predicted ages. Instead of scoring how often it predicted correctly, it's based on hoe close it was to the correct val
sizeOfTrain = math.ceil( len(trainDataAgeDefined) * 3/4 )
trainDataAgeDefined_train , trainDataAgeDefined_test = trainDataAgeDefined[:sizeOfTrain] , trainDataAgeDefined[sizeOfTrain:]

knn_clf_age.fit(trainDataAgeDefined_train.drop('Age',axis = 1), (trainDataAgeDefined_train['Age']).astype(int) ) #Labels must be either ints or strings

preds = knn_clf_age.predict(trainDataAgeDefined_test.drop('Age',axis = 1) )

def mean_absolute_percentage_error(y_true, y_pred):
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
print( mean_absolute_percentage_error (trainDataAgeDefined_test['Age'] , preds) )
"""
from sklearn.neighbors import KNeighborsClassifier

DataFrameMain_Train = clean_Data(trainData)

knn_clf_survival = KNeighborsClassifier()
knn_clf_survival.fit(DataFrameMain_Train.drop('Survived',axis = 1) ,  DataFrameMain_Train['Survived'])

DataFrameMain_Test = clean_Data(testData)
score = knn_clf_survival.predict( DataFrameMain_Test )
print(score)


# shared_resources.py
import time

from PyQt5.QtCore import pyqtSignal, QObject
import random
import threading

INITIAL_PLAYER_COUNT = 5
player_flags = [True] * INITIAL_PLAYER_COUNT
flags_lock = threading.Lock()  # Create a lock

class TestSignalEmitter(QObject):
    # Define a custom signal that takes a tuple as an argument
    payload_received = pyqtSignal(tuple)

def simulate_payload(p_emitter, client_ip, payload):
    # Simulate incoming payload by emitting the custom signal
    data = (f'TestController{client_ip}', payload)
    p_emitter.payload_received.emit(data)

def make_random_move(emitter, player):
    # Determine a random number of "left" or "right" actions (0 to 3 times)
    num_actions = random.randint(0, 3)
    action = random.choice([0b0001, 0b0011])  # "left" or "right"

    # Perform the "left" or "right" actions
    for _ in range(num_actions):
        simulate_payload(emitter, player + 1, action)
        time.sleep(0.2)  # Realistic delay for performing actions

    # Perform the "down" action to end the move
    simulate_payload(emitter, player + 1, 0b0010)
    print(f"TEST: PLAYER {player + 1} sent his action to the GAME!")
    time.sleep(0.2)  # Realistic delay for performing actions


import streamlit as st
import pandas as  pd
import numpy as np
import sklearn
from mapper_plus import mapper_plus
from download_button_file import download_button
run=0
st.write("""
# MapperPlus Ⓒ \n
2022 Esha Datta, Aditya Ballal, Javier Lopez & Leighton T. Izu
""")

#more_information = st.checkbox("More Information", False)

with st.expander("More Information"):
    st.write("""
    The Mapper Plus data pipeline is an extension of the algorithm Mapper which is based on topogical data analysis (TDA). Mapper Plus uses a community detection algorithm WLCF, first introduced by one is us, to get cluster assignments from a Mapper graph. We use the existing Mapper implementation provided by KeplerMapper in order to get a Mapper graph and then apply WLCF to get overlapping and disjoint clusters.
    \n
    More information on KeplerMapper can be found here: https://kepler-mapper.scikit-tda.org/en/latest/index.html
    \n
    More information on WLCF can be found here:
    \n
    - arxviv link: https://arxiv.org/abs/2202.11171
    \n
    - github code: https://github.com/lordareicgnon/Walk_likelihood

    """)

#with st.sidebar:
#    with st.expander("More information on parameters"):
#        st.write("This is more information on parameters")

#ranonce=0
#runmapperplus = st.checkbox("Run MapperPlus", False)
runmapperplus=1
#with st.expander("Run MapperPlus"):
if runmapperplus:
    st.markdown("## Upload data for Clustering")
    see_results=0
    uploaded_file='False'
    with st.expander("ℹ More information"):
        st.write("Upload data in CSV format with no headers.")
    #with st.sidebar:
    Wine_data = st.checkbox(
        "Use Sample Data", False, help="Wine Dataset")

    if not Wine_data:

        uploaded_file = st.file_uploader("Upload CSV", type=".csv")
        #st.write(uploaded_file)
        if uploaded_file:
            data = np.loadtxt(uploaded_file, delimiter=',')
            file_name=uploaded_file.name

    else:
        from sklearn.datasets import load_wine
        file_name='wine.csv'
        data = load_wine()['data']
    if uploaded_file or Wine_data:
        normalize = st.checkbox(
            "Normalize Data", True, help="Normalize Data using standard method")
        if normalize:
            X=(data-np.mean(data,axis=0))/np.std(data,axis=0)
        else:
            X=data
    #submit=False
    #with st.form("parameters"):
    if uploaded_file or Wine_data:
        st.markdown("## MapperPlus Parameters")

        st.markdown("### Lens")
        with st.expander('ℹ More information'):
            st.write("Lens is any function that projects the dataset into a lower dimension. We provide option of PCA, Isolation Forest and l2 norm for lens. Multiple lenses can be chosen.")

        Nl = st.number_input('Number of lenses',min_value=1, max_value=100,step=1,value=2)
        #submit = st.form_submit_button()#on_click=False)
        lens_input=[]
        #with st.form("Lenses"):
        colslens=[]
        for i in range(Nl):

            a=int(i/2)
            b=i-2*a
            if b==0:
                colslens.append(st.columns((1, 1)))
            lens_input.append(colslens[a][b].selectbox(
            "Lens "+str(i+1)+":", ["PCA", "IsolationForest", "L2 Norm"], index=0))
        lenses=set(lens_input)
        if "PCA" in lenses:
            n_comps_PCA=sum(np.array(lens_input)=="PCA")

        import sklearn.cluster
        import sklearn.mixture
        st.markdown("### Clusterer")
        with st.expander('ℹ More information'):
            st.write("Clusterer is the clustering algorithm that MapperPlus applies within each cover. We provide the choice of 9 clustering algorithms:")
            st.write("1. K-Means \n 2. Affinity Propagation \n 3. Mean-shift \n 4. Spectral Clustering \n 5. Agglomerative clustering \n 6. DBSCAN \n 7. OPTICS \n 8. Gaussian mixtures \n 9. BIRCH")
        #st.help('Hoho')
        cols2=st.columns((1, 1))
        clusterers={'K-Means':sklearn.cluster.KMeans,
        'Affinity Propagation':sklearn.cluster.AffinityPropagation,
        'Mean-shift':sklearn.cluster.MeanShift,
        'Spectral Clustering':sklearn.cluster.SpectralClustering,
        'Agglomerative clustering':sklearn.cluster.AgglomerativeClustering,
        'DBSCAN':sklearn.cluster.DBSCAN,
        'OPTICS':sklearn.cluster.OPTICS,
        'Gaussian mixtures':sklearn.mixture.GaussianMixture,
        'BIRCH':sklearn.cluster.Birch}#,
        #'Bisecting K-Means':sklearn.cluster.BisectingKMeans}
        clusterer_name=cols2[0].selectbox("Clustering Method :", list(clusterers.keys()), index=0)
        no_num_clusterer={'DBSCAN','Affinity Propagation','Mean-shift','OPTICS'}
        if clusterer_name not in no_num_clusterer:
            n_cluster=cols2[1].number_input('Number of clusters',min_value=1, max_value=100,step=1,value=2)
        if clusterer_name in no_num_clusterer:
            clusterer= clusterers[clusterer_name]()
        elif clusterer_name=='Gaussian mixtures':
            clusterer= clusterers[clusterer_name](n_components=n_cluster)
        else:
            clusterer= clusterers[clusterer_name](n_clusters=n_cluster)

        #st.write(clusterers[clusterer])
        st.markdown("### Resolution and Gain")
        #st.info('Information about Gain and Resolution', icon="ℹ")
        with st.expander("""ℹ More Information"""):
            st.write("MapperPlus takes two parameters as input: resolution and gain. The resolution determines the number of hypercubes used in the cover. The resolution can be viewd as “binning” the range of the lens. The gain determines the extent to which the hypercubes overlap , in turn determining the connectivity of the points in the dataset. The higher the gain, the more points will be shared by multiple pullback sets.")
        cols = st.columns((1, 1))
        resolution = cols[0].number_input('Resolution',min_value=1, max_value=100,step=1,value=8)
        gain = cols[1].number_input('Gain',min_value=0.0000001, max_value=0.9999999,value=0.6)
        #new_method=st.checkbox('Use new method',False)
        with st.form(key="my_form"):

            run=st.form_submit_button(label="Run Mapper Plus")
if run:
    #st.stop()
    N=X.shape[0]
    import kmapper as km
    mapper=km.KeplerMapper()
    lens=np.zeros((N,0))
    if "PCA" in lenses:
        from sklearn.decomposition import PCA
        #st.write(X)
        lens1 = mapper.fit_transform(X,projection=PCA(n_components=n_comps_PCA))
        lens=np.concatenate((lens,lens1),axis=1)
    if "L2 Norm" in lenses:
        lens2 = mapper.fit_transform(X,projection='l2norm')
        lens = np.concatenate((lens,lens2),axis=1)
    if "IsolationForest" in lenses:
        from sklearn import ensemble
        isomodel = ensemble.IsolationForest(random_state=1729)
        isomodel.fit(X)
        lens3 = isomodel.decision_function(X).reshape((X.shape[0], 1))
        lens = np.concatenate((lens,lens3),axis=1)

    ranonce=1
    cover = km.Cover(n_cubes = resolution, perc_overlap = gain)
    #model=mapper_plus(new_method=new_method)
    model=mapper_plus()

    st.write("....... Running")
    st.write("....... Finding Mapper Graph")
    model.get_mapper_graph(lens,X,cover=cover,clusterer=clusterer,)
    st.write("....... Finding overlapping clusters")
    model.get_overlapping_clusters()
    st.write("##### "+str(len(model.overlapping_clusters))+" overlapping clusters found")
    model.get_non_overlapping_clusters(new_method=1)
    overlap_str=''
    for comms in range(len(model.overlapping_clusters)):
        if comms>0:
            overlap_str+='\n'
        overlap_str+='Cluster '+str(comms)+','
        with st.expander("Cluster "+str(comms)):
            st.write(str(model.overlapping_clusters[comms])[1:-1])
        overlap_str+=str(model.overlapping_clusters[comms])[1:-1]
    #st.download_button('Download Overlapping Clusters', overlap_str,file_name='overlapping_clusters_'+file_name)
    csvbutton=download_button(overlap_str, 'overlapping_clusters_'+file_name, 'Download Overlapping Clusters')
    #st.write(csvbutton)
    st.write("....... Finding disjoint clusters")
    st.write("##### "+str(len(model.non_overlapping_clusters))+" disjoint clusters found")
    disjoint_str=''
    for comms in range(len(model.non_overlapping_clusters)):
        if comms>0:
            disjoint_str+='\n'
        disjoint_str+='Cluster '+str(comms)+','
        with st.expander("Cluster "+str(comms)):
            st.write(str(model.non_overlapping_clusters[comms])[1:-1])
        disjoint_str+=str(model.non_overlapping_clusters[comms])[1:-1]
    download_button( disjoint_str,'disjoint_clusters_'+file_name,'Download Disjoint Clusters')
    outliers = np.array(range(len(model.comm_id)))[model.comm_id==-1]
    if len(outliers)>0:
        if len(outliers)==1:
            st.write("We found 1 outlier")
        else:
            st.write("We found "+str(len(outliers))+" outliers")
        with st.expander("Outliers"):
            st.write(str(list(outliers))[1:-1])


import os
import sys

import gym
from griddly.util.rllib.callbacks import VideoCallbacks
from griddly.util.rllib.environment.core import RLlibEnv, RLlibMultiAgentWrapper
from ray.air.callbacks.wandb import WandbLoggerCallback
from ray.rllib.algorithms.ppo import PPOConfig
from ray.rllib.models import ModelCatalog
from ray.rllib.policy.policy import PolicySpec
from ray.tune import register_env, tune

from rllib_multi_agent_example.gap_agent import GAPAgent
from rllib_multi_agent_example.simple_conv_agent import SimpleConvAgent

# You have to put this here so that rllib can find griddly libraries when it starts new workers
sep = os.pathsep
os.environ["PYTHONPATH"] = sep.join(sys.path)

environment_name = "GridmanMultiAgent"
environment_yaml = "gridman/gridman_multiagent.yaml"
model_name = "SimpleConvAgent"

# Register the environment with RLlib
register_env(environment_name, lambda config: RLlibMultiAgentWrapper(RLlibEnv(config)))

model_class = None
if model_name == "SimpleConvAgent":
    model_class = SimpleConvAgent
elif model_name == "GlobalAveragePoolingAgent":
    model_class = GAPAgent

# Register the model with RLlib
ModelCatalog.register_custom_model(model_name, model_class)

test_dir = f"./results/{environment_name}"
video_dir = f"videos"

# multi-agent policies
policies = {
    # Use the PolicySpec namedtuple to specify an individual policy:
    "gridman_policy": PolicySpec(
        observation_space=gym.spaces.Box(0, 255, (9, 9, 11), float),
        config={"gamma": 0.95},
    ),
    "enemy_policy": PolicySpec(
        observation_space=gym.spaces.Box(0, 255, (7, 7, 11), float),
        config={"gamma": 0.95},
    ),
}

def policy_mapping_fn(agent_id, episode, worker, **kwargs):
    if agent_id == 1:
        return "gridman_policy"
    else:
        return "enemy_policy"

config = (
    PPOConfig()
    .rollouts(num_rollout_workers=8, num_envs_per_worker=16, rollout_fragment_length=256)
    .callbacks(VideoCallbacks)
    .multi_agent(policies=policies, policy_mapping_fn=policy_mapping_fn)
    .training(
        model={
            "custom_model": model_name
        },
        train_batch_size=32768,
        lr=1e-4,
        gamma=0.95,
        lambda_=0.9,
        use_gae=True,
        clip_param=0.4,
        grad_clip=None,
        entropy_coeff=0.1,
        vf_loss_coeff=0.5,
        sgd_minibatch_size=2048,
        num_sgd_iter=4,
    )
    .environment(
        env_config={
            # A video every 50 iterations
            'record_video_config': {
                'fps': 20,
                'frequency': 5000,
                'directory': video_dir,

                # Will record a video of the global observations
                'include_global': True,

                # Will record a video of the agent's perspective
                'include_agents': False,
            },
            'player_done_variable': "player_done",
            'random_level_on_reset': True,
            'yaml_file': environment_yaml,
            'global_observer_type': "GlobalSpriteObserver",
            'player_observer_type': ["VectorGridMan", "VectorEnemy", "VectorEnemy", "VectorEnemy"],
            'max_steps': 2000,
        },
        env=environment_name, clip_actions=True)
    .debugging(log_level="ERROR")
    .framework(framework="torch")
    .resources(num_gpus=int(os.environ.get("RLLIB_NUM_GPUS", "1")))
)

result = tune.run(
    "PPO",
    name="PPO",
    stop={"timesteps_total": 10000000},
    local_dir=test_dir,
    config=config.to_dict(),
    callbacks=[
        WandbLoggerCallback(project="RLLib Gridman MultiAgent", entity="griddlyai")
    ]
)


# coding=utf-8
"""Tests that exercise Pulp's repoview feature.

For more information, see:

* `Pulp #189 <https://pulp.plan.io/issues/189>`_: "Repoview-like functionality
  for browsing repositories via the web interface"
* Yum Plugins → Yum Distributor → `Optional Configuration Parameters
  <http://docs.pulpproject.org/plugins/pulp_rpm/tech-reference/yum-plugins.html#optional-configuration-parameters>`_
"""
import unittest
from urllib.parse import urljoin

from packaging.version import Version
from pulp_smash import api, config, selectors, utils
from pulp_smash.pulp2.constants import REPOSITORY_PATH
from pulp_smash.pulp2.utils import publish_repo, upload_import_unit

from pulp_2_tests.constants import RPM_UNSIGNED_URL
from pulp_2_tests.tests.rpm.utils import set_up_module as setUpModule  # pylint:disable=unused-import
from pulp_2_tests.tests.rpm.api_v2.utils import gen_distributor, gen_repo

class RepoviewTestCase(unittest.TestCase):
    """Publish a repository with the repoview feature on and off.

    Do the following:

    1. Create an RPM repository, and add some content to it.
    2. Publish the repository. Get ``/pulp/repos/{rel_url}/``, and verify that
       no redirects occur.
    3. Publish the repository with the ``repoview`` and ``generate_sqlite``
       options set to true. Get ``/pulp/repos/{rel_url}/``, and verify that a
       redirect to ``/pulp/repos/{rel_url}/repoview/index.html`` occurs.
    4. Repeat step 2.
    """

    def test_all(self):
        """Publish a repository with the repoview feature on and off."""
        cfg = config.get_config()
        if cfg.pulp_version < Version('2.9'):
            self.skipTest('https://pulp.plan.io/issues/189')
        if utils.fips_is_supported(cfg) and utils.fips_is_enabled(cfg):
            self.skipTest('https://pulp.plan.io/issues/3775')

        # Create a repo, and add content
        client = api.Client(cfg)
        body = gen_repo()
        body['distributors'] = [gen_distributor()]
        repo = client.post(REPOSITORY_PATH, body).json()
        self.addCleanup(client.delete, repo['_href'])
        rpm = utils.http_get(RPM_UNSIGNED_URL)
        upload_import_unit(cfg, rpm, {'unit_type_id': 'rpm'}, repo)

        # Get info about the repo distributor
        repo = client.get(repo['_href'], params={'details': True}).json()
        pub_path = urljoin(
            '/pulp/repos/',
            repo['distributors'][0]['config']['relative_url']
        )

        # Publish the repo
        publish_repo(cfg, repo)
        response = client.get(pub_path)
        with self.subTest(comment='first publish'):
            self.assertEqual(len(response.history), 0, response.history)

        # Publish the repo a second time
        publish_repo(cfg, repo, {
            'id': repo['distributors'][0]['id'],
            'override_config': {'generate_sqlite': True, 'repoview': True},
        })
        response = client.get(pub_path)
        with self.subTest(comment='second publish'):
            self.assertEqual(len(response.history), 1, response.history)
            self.assertEqual(
                response.request.url,
                urljoin(response.history[0].request.url, 'repoview/index.html')
            )

        # Publish the repo a third time
        if not selectors.bug_is_fixed(2349, cfg.pulp_version):
            self.skipTest('https://pulp.plan.io/issues/2349')
        publish_repo(cfg, repo)
        response = client.get(pub_path)
        with self.subTest(comment='third publish'):
            self.assertEqual(len(response.history), 0, response.history)


from PDP import PDP
from JinEnv import JinEnv
import numpy as np
import time
import scipy.io as sio

# --------------------------- load environment ----------------------------------------
pendulum = JinEnv.SinglePendulum()
pendulum.initDyn()
pendulum.initCost()

# --------------------------- create PDP SysID object ----------------------------------------
dt = 0.05
pendulumid = PDP.SysID()
pendulumid.setAuxvarVariable(pendulum.dyn_auxvar)
pendulumid.setStateVariable(pendulum.X)
pendulumid.setControlVariable(pendulum.U)
dyn = pendulum.X + dt * pendulum.f
pendulumid.setDyn(dyn)

# --------------------------- load the data ----------------------------------------
load_data = sio.loadmat('data/pendulum_iodata.mat')
data = load_data['pendulum_iodata'][0, 0]
true_parameter = data['true_parameter']
n_batch = len(data['batch_inputs'])
batch_inputs = []
batch_states = []
for i in range(n_batch):
    batch_inputs += [data['batch_inputs'][i]]
    batch_states += [data['batch_states'][i]]

# --------------------------- learn the dynamics ----------------------------------------
for j in range(10):
    start_time = time.time()
    # learning rate
    lr = 1e-5
    # initialize
    loss_trace, parameter_trace = [], []
    sigma = 0.5
    initial_parameter = np.array(true_parameter) + sigma * np.random.rand(len(true_parameter)) - sigma / 2
    current_parameter = initial_parameter
    for k in range(int(2e4)):
        # one iteration of PDP
        loss, dp = pendulumid.step(batch_inputs, batch_states, current_parameter)
        # update
        current_parameter -= lr * dp
        loss_trace += [loss]
        parameter_trace += [current_parameter]
        # print
        if k % 100 == 0:
            print('Trial:', j, 'Iter:', k, 'loss:', loss)

    # save
    save_data = {'trail_no': j,
                 'loss_trace': loss_trace,
                 'parameter_trace': parameter_trace,
                 'learning_rate': lr,
                 'time_passed': time.time() - start_time}
    sio.savemat('./data/PDP_SysID_results_trial_' + str(j) + '.mat', {'results': save_data})


import sys
from pprint import pformat
import fixtures
import testtools
from testtools import content
import httpretty
import json
import logging

sys.path.insert(0, '../../../../build/debug/api-lib/')

from vnc_api import vnc_api

def setup_flexmock():
    pass
# end setup_flexmock

cov_handle = None

class TestCase(testtools.TestCase, fixtures.TestWithFixtures):
    _HTTP_HEADERS = {
        'Content-type': 'application/json; charset="UTF-8"',
    }

    def _add_request_detail(self, op, url, headers=None, query_params=None,
                            body=None):
        request_str = ' URL: ' + pformat(url) + \
                      ' OPER: ' + pformat(op) + \
                      ' Headers: ' + pformat(headers) + \
                      ' Query Params: ' + pformat(query_params) + \
                      ' Body: ' + pformat(body)
        self.addDetail('Requesting: ', content.text_content(request_str))

    def setUp(self):
        super(TestCase, self).setUp()
        setup_flexmock()

        httpretty.enable()
        httpretty.register_uri(
                httpretty.GET, "http://127.0.0.1:8082/",
                body=json.dumps({'href': "http://127.0.0.1:8082",
                                 'links': []}))

        self._logger = logging.getLogger(__name__)
        logging.basicConfig()
        self._logger.setLevel(logging.DEBUG)
        # Use empty config file (does not exist) to be sure using default
        # parameter values
        self._vnc_lib = vnc_api.VncApi(conf_file='/tmp/fake-config-file')
    # end setUp

    def tearDown(self):
        httpretty.disable()
        httpretty.reset()
        super(TestCase, self).tearDown()
    # end tearDown
# end TestCommon


# -------------------------
# Project: Deep Q-Learning on Flappy Bird
# Author: Flood Sung
# Date: 2016.3.21
# -------------------------

import cv2
import sys
sys.path.append("game/")
import wrapped_flappy_bird as game
from BrainDQN_Nature import BrainDQN
import numpy as np

# preprocess raw image to 80*80 gray image
def preprocess(observation):
	observation = cv2.cvtColor(cv2.resize(observation, (80, 80)), cv2.COLOR_BGR2GRAY)
	ret, observation = cv2.threshold(observation,1,255,cv2.THRESH_BINARY)
	return np.reshape(observation,(80,80,1))

def playFlappyBird():
	# Step 1: init BrainDQN
	actions = 2
	brain = BrainDQN(actions)
	# Step 2: init Flappy Bird Game
	flappyBird = game.GameState()
	# Step 3: play game
	# Step 3.1: obtain init state
	action0 = np.array([1,0])  # do nothing
	observation0, reward0, terminal = flappyBird.frame_step(action0)
	observation0 = cv2.cvtColor(cv2.resize(observation0, (80, 80)), cv2.COLOR_BGR2GRAY)
	ret, observation0 = cv2.threshold(observation0,1,255,cv2.THRESH_BINARY)
	brain.setInitState(observation0)

	# Step 3.2: run the game
	while 1!= 0:
		action = brain.getAction()
		nextObservation,reward,terminal = flappyBird.frame_step(action)
		nextObservation = preprocess(nextObservation)
		brain.setPerception(nextObservation,action,reward,terminal)

def main():
	playFlappyBird()

if __name__ == '__main__':
	main()


from flask import Flask, render_template, request, url_for, redirect
from dbloader import *
from resource_model import *
from volunteer_op_model import *

app = Flask(__name__)

@app.route('/')
def home():
    style = url_for('static', filename='css/flatly.css')
    return render_template('index.html', style=style)

@app.route('/resources/')
def resources():
    style = url_for('static', filename='css/flatly.css')
    results = get_resources_matching({})
    return render_template('list_view.html', results=results, style=style, title='Resources', is_res=True)

@app.route('/volunteer/')
def volunteers():
    style = url_for('static', filename='css/flatly.css')
    results = get_Volunteer_Ops_matching({})
    return render_template('list_view.html', results=results, style=style, title='Volunteer', is_res=False)

@app.route('/submit/', methods=['POST'])
def submit():
    result = request.form
    if result['sub_type'] == 'vol_op':
        x = Volunteer_Op(result['name'], result['loc'], result['descrip'], result['email'])
        store_Volunteer_Op(x)
        return redirect(url_for('volunteers'))
    else:
        x = Resource(result['name'], result['category'], result['loc'], result['descrip'], result['email'])
        store_resource(x)
        return redirect(url_for('/resources'))

# /view?id=abc
@app.route('/view-res/')
def view_res():
    style = url_for('static', filename='css/flatly.css')
    res_id = request.args.get('id')
    result = get_resources_matching({'_id': res_id})
    if result == None:
        return redirect(url_for('home'))
    else:
        return render_template('individual.html', style=style, info=result[0].get_json(), is_res=True)

@app.route('/view-vol/')
def view_vol():
    style = url_for('static', filename='css/flatly.css')
    vol_id = request.args.get('id')
    result = get_Volunteer_Ops_matching({'_id': vol_id})
    if result == None:
        return redirect(url_for('home'))
    else:
        return render_template('individual.html', style=style, info=result[0].get_json(), is_res=False)

@app.route('/about')
def about():
    style = url_for('static', filename='css/flatly.css')
    return render_template('about.html', style=style)


#%%
# IMPORT LIBRARIES

import glob
import os
import matplotlib.pyplot as plt
import sys
import json
import numpy as np
from RL_funcs import *
import Carla_funcs as cf
import gcg

try:
    sys.path.append(glob.glob('../PythonAPI/carla/dist/carla-*%d.%d-%s.egg' % (
        sys.version_info.major,
        sys.version_info.minor,
        'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
    pass

import carla

# %%
# SETUP RL VARIABLE

# K-shooting time horizon
H = 16

# Number of K-shoots
K = 5

# Steering actions as a percentage of ~70°
action_space = np.array([-0.3, 0, 0.3])

# Vehicle speed in m/s (this is decomposed into an orthogonal cartesian coordinate system and only approximates horizontal velocities)
vehicle_speed = 5

# Simulator time steps
delta_t = 0.1

# %%
# INITIALIZE THE TF NETWORK MODEL

# Use these two lines to build a TF model from scratch
model = gcg.computation_graph(H)
print('GCG Built!')

# Use these two lines to load a previously built TF model (used for outages and troubleshooting)
# model = gcg.load_old_model('../models/model.tf')
# print('GCG Loaded!')

# %%
#  CARLA SIMULATOR SETUP

# Initialize a connection with the CARLA server and setup all CARLA related experimental parameters and supports
client, world, vehicle, camera, collision, orig_settings, image_queue, collision_queue = cf.setup(time_step = delta_t, img_x = 128, img_y = 72, speed=vehicle_speed)

# %%
# ITERATE THROUGH A PREDIFIEND SET OF EPISODES

# Define the number of episodes
big_loop_counter = 4000
#big_loop_counter = 2

# Target a collision free runtime of 10 minutes
step_max = int((10 * 60) / delta_t)

# Setup an object to hold the stacked image state
img_stack = None

# Setup some metrics
cum_steps = 0
cum_steps_per_ep = []
steps_per_ep = []

# Iterate through the episodes
for i in range(big_loop_counter):

    # Initialize and episodic dataset
    dataset_I = []
    dataset_a = []
    y_labels = []
    y_buffer = np.zeros(H)

    episode_done = False

    # Retrieve data from the CARLA simulator (resulting from a "tick", see Carla_funcs.py)
    world.get_snapshot().frame

    # Retrieve and image from the image queue.
    img = image_queue.get(True, 1.0)

    # Navigate through the simulator (up to 10 minutes collision free)
    for step in range(step_max):

        # Setup objects for a single iteration
        img_stack = cf.preprocess_img(img, img_stack)
        rwd_list = []
        action_sets = []

        # Do K-shooting
        for k in range(K):

            # Initialize a new action vector
            action_input = generate_actions(action_space, H)

            # Predict the probabilities of collision
            y_hats = gcg.run(model, img_stack, action_input)

            # Calculate the reward
            rwd = reward_function(y_hats)
            rwd_list.append(rwd)
            action_sets.append(action_input)

        # Extract the highest reward and action from the K-shoot
        best_action_set = best_actions(action_sets, rwd_list)

        # Take a single action in CARLA Simulator and observe the new state and "reward" signal
        img, collided = cf.take_action(world, vehicle, image_queue, img, collision_queue, best_action_set[0])
        dataset_I.append(img_stack)
        dataset_a.append(best_action_set)

        # Populate actual collision labels
        if collided == 1:
            if step+1 >= H:

                for j in range(H):
                    y_buffer[H-1-j] = 1
                    y_labels.append(y_buffer.copy())
                break

            else:
                for j in range(step):
                    y_buffer[H-j-step:] = 1
                    y_labels.append(y_buffer.copy())
                break
        elif step+1 >= H:
            y_labels.append(y_buffer.copy())

    cum_steps += step
    cum_steps_per_ep.append(cum_steps)
    steps_per_ep.append(step)

    # Update/train the model at the end of every episode
    gcg.train(model, dataset_I, dataset_a, y_labels)

    # Outputs for observing training, saving the model and storing metrics
    if (i % 10 == 0) and (i > 0):
        print('Big Loop iteration - ', i)

    if (i % 50 == 0)and (i > 0):
        model.save('../models/model.tf')
        print('Model saved')

        with open("../images/cum_steps.json", 'w') as f:
            # indent=2 is not needed but makes the file human-readable
            # if the data is nested
            json.dump(cum_steps_per_ep, f, indent=2)

        with open("../images/steps.json", 'w') as f:
            # indent=2 is not needed but makes the file human-readable
            # if the data is nested
            json.dump(steps_per_ep, f, indent=2)

        fig1 = plt.figure()
        plt.plot(cum_steps_per_ep)
        plt.xlabel('Episodes')
        plt.ylabel('Cumulative Moves')
        plt.title('Figure 1')
        plt.savefig('../images/figure1.png')

        fig2 = plt.figure()
        plt.plot(steps_per_ep)
        plt.xlabel('Episodes')
        plt.ylabel('Moves')
        plt.title('Figure 2')
        plt.savefig('../images/figure2.png')

    # Clean-up the CARLA server and get it read for the next episode
    cf.close(world, camera, collision, vehicle, orig_settings)
    vehicle, camera, collision, image_queue, collision_queue = cf.spawn_car(world= world, img_x = 128, img_y = 72, speed=vehicle_speed)

# %%
# SHUT DOWN CARLA

# Leave the CARLA Server in a clean state
cf.close(world, camera, collision, vehicle, orig_settings)


from typing import List

import numpy as np

class NeuralNetwork:
    def __init__(self, hidden_sizes: List[int], output_size: int, input_size: int) -> None:

        self.weights = []
        self.layers = []
        self.output_size = output_size
        self.input_size = input_size

        self.weights.append(np.random.random((input_size, hidden_sizes[0])))

        for i in range(len(hidden_sizes) - 1):
            weight = np.random.random((hidden_sizes[i], self.output_size))
            self.weights.append(weight)

    def set_layers(self, data):
        """ Ustawianie wag poszczegolnych warstw. """
        self.layers.clear()
        for i in range(len(data)):
            self.layer0 = data[i:i+1]
            self.layers.append(self.layer0)
            self.layer1 = self.layer0.dot(self.weights[0])
            self.layers.append(self.layer1)
            self.layer2 = self.layer1.dot(self.weights[1])
            self.layers.append(self.layer2)

    def predict(self, input_: np.ndarray) -> np.ndarray:
        # zwracamy wartosci znajdujace sie w ostatniej warstwie sieci
        return self.layers[len(self.layers) - 1]

    def fit(self, data: np.ndarray, labels: np.ndarray, iterations: int, alpha: float) -> float:
        # zwracamy wartosc bledu treningu
        for _ in range(iterations):
            error = 0
            #correct_count = 0
            for i in range(len(data)):
                self.set_layers(data)

                #layer = self.predict(index)

                error += np.sum((labels[i] - self.layers[len(self.layers) - 1]) ** 2)
                #correct_count += int(self.layers[len(self.layers) - 1] == labels[i])

                delta2 = labels[i] - self.layers[len(self.layers) - 1]

                lenght = len(self.weights) - 1

                while lenght >= 0:
                    delta = delta2.dot(self.weights[lenght-1].T)
                    self.weights[lenght] += alpha * (self.layers[lenght].T.dot(delta))
                    lenght -= 1
        return print(error)

input_size = 2
hidden_size = [10,4,16,16]
output_size = 1
network = NeuralNetwork(hidden_size, output_size, input_size)
#network.__init__(hidden_size, output_size, input_size)

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

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

train_iterations = 500

train_alpha = 0.01

network = NeuralNetwork(hidden_size, output_size, input_size)
network.fit(train_inputs, train_outputs, train_iterations, train_alpha)


import argparse
import csv
import glob
import json
import os
import random
import re

from nltk.tokenize import sent_tokenize
import numpy as np
import torch
import torch.nn as nn
import rouge
from transformers import *

from tqdm import tqdm
from generate_utils  import generate_paragraph

def clear_dirs(gen_dir, tgt_dir):
    for f in glob.glob("{}/*".format(tgt_dir)):
        os.remove(f)
    for f in glob.glob("{}/*".format(gen_dir)):
        os.remove(f)
    os.makedirs(tgt_dir, exist_ok=True)
    os.makedirs(gen_dir, exist_ok=True)

def format_text(text, max_len, stop_words=[]):
    text = "\n".join(sent_tokenize(text)).replace("<", "&lt").replace(">", "&gt")
    for stop_word in stop_words:
        text = text.replace(" {} ".format(stop_word), " ")
    if max_len is not None:
        text = " ".join(text.split(" ")[:max_len])
    return text.encode('ascii','ignore').decode("ascii","ignore")

def get_average_scores(jsonfile, srcs,hyps, refs,maxlen=110, stop_words=[]):
    rouge_scorer = rouge.Rouge()
    averaged_scores = {'rouge-1': {'f': 0, 'p': 0, 'r': 0},
                       'rouge-2': {'f': 0, 'p': 0, 'r': 0},
                       'rouge-l': {'f': 0, 'p': 0, 'r': 0}}

    scores = rouge_scorer.get_scores(hyps, refs)
    for metric in averaged_scores.keys():
        for values in scores:
            for sub_metric in averaged_scores[metric]:
                averaged_scores[metric][sub_metric] += values[metric][sub_metric]
    for key in averaged_scores.keys():
        for sub_key in averaged_scores[key].keys():
            averaged_scores[key][sub_key] /= len(hyps)
    for i in range(len(srcs)):
        jsonfile.write(json.dumps({'r1': scores[i]['rouge-1'], 'r2': scores[i]['rouge-2'], 'rl': scores[i]['rouge-l'],'hyp':hyps[i], 'ref':refs[i],'src':srcs[i]})+"\n")
    return averaged_scores

def evaluate_doc_model(model, val_loader, text_encoder, device, beam, gen_len, k, p, decoding_strategy, save_file, gen_dir="gen", tgt_dir="tgt", max_len=110, stop_words=[], args=None):
    data = {"src": [], "gen": [], "tgt": []}
    srcs, hyps, refs = [], [], []
    model.eval()
    for batchargs in tqdm(val_loader):
        with torch.no_grad():
            # Generating outputs for evaluation
            src_strs, tgt_strs, gen_strs = generate_paragraph(model, batchargs, text_encoder, device, beam, gen_len, k, p, decoding_strategy, min_len=args.min_len)
            data["src"].extend(src_strs)
            data["gen"].extend(gen_strs)
            data["tgt"].extend(tgt_strs)

    jsf = open(save_file+".output.json","w")
    for i in range(min(len(data['src']),50)):
        print("*" * 50)
        try:
            print("Source: {}".format(data['src'][i]))
            print('Hypothesis: {}'.format(data['gen'][i]))
            print("Reference: {}".format(data['tgt'][i]))
        except:
            pass

    with open(save_file, "w") as f:
        json.dump(
            #get_rouge_scores(gen_dir, tgt_dir),
            get_average_scores(jsf,data['src'],data['gen'],data['tgt'],max_len,stop_words),
            f,
            indent=4,
            sort_keys=True
        )


import pandas as pd                 # Para la manipulación y análisis de los datos
import numpy as np                  # Para crear vectores y matrices n dimensionales
import matplotlib.pyplot as plt     # Para la generación de gráficas a partir de los datos
from scipy.spatial.distance import cdist
from scipy.spatial import distance

def get_matriz(file_name,metric_method):
    DataF = pd.read_csv(file_name)
    Distancias = cdist(DataF,DataF,metric=metric_method)
    MatrizDF = pd.DataFrame(Distancias)
    MatrizResult=MatrizDF.round(3)
    columns=list(MatrizResult.columns)
    data_list=MatrizResult.values.tolist()
    data_list.insert(0,columns)
    return data_list

def get_single_distance(file_name ,vec1,vec2,metrica):

    DataF = pd.read_csv(file_name)
    Objeto1 = DataF.iloc[vec1]
    Objeto2 = DataF.iloc[vec2]
    if metrica == "euclidean":
        distancia = distance.euclidean(Objeto1,Objeto2)
    elif metrica == "chebyshev":
        distancia = distance.chebyshev(Objeto1,Objeto2)
    elif metrica == "cityblock":
        distancia = distance.cityblock(Objeto1,Objeto2)
    elif metrica == "minkowski":
        distancia = distance.minkowski(Objeto1,Objeto2)
    else:
        return None
    return distancia


from ctim.constant import DISPLAY_NODE_THRESHOLD
from stix.common.structured_text import StructuredText

class AlchemyJsonData:
    def __init__(self):
        self._json_nodes = {}
        self._json_edges = []

    def add_json_node(self, n):
        id_ = n.get_id()
        if(id_ in self._json_nodes):
            return
        self._json_nodes[id_] = n

    def add_json_edge(self, e):
        self._json_edges.append(e)

    def set_json_node_exact(self, id_):
        try:
            an = self._json_nodes[id_]
            an.set_is_exact(True)
        except KeyError:
            pass

    def set_json_node_user_language(self, _user_language):
        for node in self._json_nodes.values():
            node.set_user_language(_user_language)

    def get_alchemy_json(self, ctirs, too_many_nodes='confirm'):
        is_redact = False
        if self.is_over_unlinked_end_nodes():
            if too_many_nodes == 'confirm':
                return None
            elif too_many_nodes == 'redact':
                is_redact = True
            elif too_many_nodes == 'all':
                is_redact = False
            else:
                return None

        json = {}

        json_nodes = []
        json_node_ids = []
        redact_nodes = []

        for node in self._json_nodes.values():
            if is_redact:
                if self.is_end_node(node):
                    if not self.is_include_in_link(node):
                        redact_nodes.append(node)
                        continue
            json_nodes.append(node.get_json(ctirs))
            json_node_ids.append(node._id_)
        json['nodes'] = json_nodes
        json_edges = []
        for edge in set(self._json_edges):
            redact_edge = False
            for redact_node in redact_nodes:
                if edge._source == redact_node._id_ or edge._target == redact_node._id_:
                    redact_edge = True
                    break
            if redact_edge:
                continue
            if edge._source not in json_node_ids:
                print('no edge. skip (%s)' % (edge._source))
                continue
            if edge._target not in json_node_ids:
                print('no edge. skip (%s)' % (edge._target))
                continue
            json_edges.append(edge.get_json(ctirs))
        json['edges'] = json_edges
        return json

    def is_over_unlinked_end_nodes(self):
        count = 0
        for node in self._json_nodes.values():
            if not self.is_end_node(node):
                continue
            else:
                if self.is_include_in_link(node):
                    continue
                else:
                    count += 1
                    if count > DISPLAY_NODE_THRESHOLD:
                        return True
        return False

    def is_include_in_link(self, node):
        return node._is_exact

    def is_end_node(self, node):
        not_end_node_types = [
            'Header',
            'Indicators',
            'Observables',
            'Campaigns',
            'TTPs',
            'Threat_Actors',
            'Incidents',
            'Exploit_Targets',
            'Courses_Of_Action',
            'Incidents']
        if node._type in not_end_node_types:
            return False
        else:
            return True

    def get_alchemy_node_by_id(self, id_):
        return self._json_nodes[id_]

class AlchemyJsonBase(object):
    def get_json(self, ctirs):
        raise NotImplementedError()

class AlchemyEdge(AlchemyJsonBase):
    _id_ = None
    _source = -1
    _target = -1
    _caption = ''
    _type = ''
    _reason = None
    _stix2_object = None

    def __init__(self, source, target, caption, type=None, object_=None, reason=None):
        if object_ is not None:
            self._id_ = object_['id']
            self._stix2_object = object_
        self._source = source
        self._target = target
        self._caption = caption
        self._reason = reason
        if type is None:
            self._type = caption
        else:
            self._type = type

    def __eq__(self, obj):
        return ((self._source == obj._source) and (self._target == obj._target))

    def __hash__(self):
        return hash((self._source, self._target))

    def get_json(self, ctirs):
        r = {}
        r['revoked'] = False
        r['is_updated'] = False
        if self._stix2_object is not None:
            r['id'] = self._id_
            r['stix2_object'] = self._stix2_object
            r['modified'] = self._stix2_object['modified']
            try:
                ret = ctirs.get_latest_object(
                    self._stix2_object['id'],
                    self._stix2_object['modified'])
                r['is_latest'] = ret['is_latest']
                r['latest_object'] = ret['object']
                r['versions'] = ret['versions']
                if 'revoked' in ret['object']:
                    r['revoked'] = ret['object']['revoked']
            except Exception:
                r['is_latest'] = True
                r['latest_object'] = self._stix2_object
                r['versions'] = [self._stix2_object['modified']]
                if 'revoked' in self._stix2_object:
                    r['revoked'] = self._stix2_object['revoked']
        r['source'] = str(self._source)
        r['target'] = str(self._target)
        r['caption'] = str(self._caption)
        r['type'] = str(self._type)
        r['reason'] = self._reason
        return r

class AlchemyNode(AlchemyJsonBase):
    _id_ = -1
    _type = ''
    _caption = ''
    _description = ''
    _cluster = -1
    _is_exact = False
    _value = ''
    _language_contents = None
    _stix2_object = None
    _user_language = None
    _revoked = False
    _is_modified = False

    def __init__(self, id_, type_, caption, description, object_=None, observable_=None, cluster=None):
        super(AlchemyNode, self).__init__()
        self._id_ = id_
        self._set_type(type_)
        self._set_caption(caption)
        if(description is None):
            self._description = ''
        else:
            v = ''
            if isinstance(description, str):
                v = description
            elif isinstance(description, StructuredText):
                v = description.value

            if len(v) == 0:
                self._description = ''
            else:
                self._description = v
        self._cluster = cluster

    def __eq__(self, obj):
        return obj._id_ == self._id_

    def _set_caption(self, d):
        if(d is None):
            self._caption = ''
        else:
            self._caption = d

    def _set_type(self, d):
        self._type = d

    def set_cluster(self, cluster):
        self._cluster = cluster

    def set_is_exact(self, is_exact):
        self._is_exact = is_exact

    def get_id(self):
        return self._id_

    def set_value(self, value):
        self._value = value

    def get_json(self, ctirs):
        r = {}
        r['id'] = self.sanitize(str(self._id_))
        r['type'] = self.sanitize(str(self._type))
        r['caption'] = self.sanitize(self._caption)
        description = self.sanitize(self._description)
        r['stix2_object'] = self._stix2_object
        r['language_contents'] = self._language_contents
        r['user_language'] = self._user_language
        r['description'] = self.sanitize(description)
        r['cluster'] = self.sanitize(str(self._cluster))
        r['value'] = self.sanitize(self._value)
        r['revoked'] = False
        r['is_latest'] = False
        if self._stix2_object is not None:
            if 'modified' in self._stix2_object:
                r['modified'] = self._stix2_object['modified']
                try:
                    ret = ctirs.get_latest_object(
                        self._stix2_object['id'],
                        self._stix2_object['modified'])
                    r['is_latest'] = ret['is_latest']
                    r['latest_object'] = ret['object']
                    r['versions'] = ret['versions']
                    if 'revoked' in ret['object']:
                        r['revoked'] = ret['object']['revoked']
                except Exception:
                    r['is_latest'] = True
                    r['latest_object'] = self._stix2_object
                    r['versions'] = [self._stix2_object['modified']]
                    if 'revoked' in self._stix2_object:
                        r['revoked'] = self._stix2_object['revoked']
            else:
                r['is_latest'] = True
                r['latest_object'] = self._stix2_object
                r['versions'] = []
                r['revoked'] = False
        return r

    def sanitize(self, s):
        return s

    def set_language_contents(self, language_contents):
        self._language_contents = language_contents

    def set_stix2_object(self, _stix2_object):
        self._stix2_object = _stix2_object

    def set_user_language(self, _user_language):
        self._user_language = _user_language

    class RelatedCampaign:
        _campaign = ''
        _related_num = -1

        def __init__(self, campaign, related_num):
            self._campaign = campaign
            self._related_num = related_num