from keras_vggface.vggface import VGGFace
import numpy as np
import mediapipe as mp
from scipy import spatial
import cv2
import pickle
import time
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf

def load_stuff(filename):
    '''Loads the pickle file

    Args:
        filename: path of the pickle file (feature.pickle)

    Return:
        stuff: varible holding the loaded '''

    saved_stuff = open(filename, "rb")
    stuff = pickle.load(saved_stuff)
    saved_stuff.close()
    return stuff

class FaceIdentify(object):
    ''' This class collects the user data through the webcam(laptop) and resize it to
        224X224 and predictes the facial landmarks by VGGFace module and compares with
        features in the pickel file using euclidean distance method. The label of is
        accordance with the feature with min euclidean distance  '''

    def __new__(cls, precompute_features_file=None):
        if not hasattr(cls, 'instance'):
            cls.instance = super(FaceIdentify, cls).__new__(cls)
        return cls.instance

    def __init__(self, precompute_features_file=None):

        self.face_size = 224
        self.precompute_features_map = load_stuff(precompute_features_file)
        self.mpFaceDetection = mp.solutions.face_detection
        self.face_detection = self.mpFaceDetection.FaceDetection(0.50)
        print("[INFO] Loading the model")
        self.model = VGGFace(model='resnet50', include_top=False,
                             input_shape=(224, 224, 3), pooling='avg')  # pooling: None, avg or max
        print("[INFO] Completed...")

    @classmethod
    def draw_label(cls, image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
                   font_scale=1, t=2, l=20, color = (255, 255, 255)):

        # Normalizing the size
        h,w,_= image.shape
        x, y, w, h = int((point.xmin * w )), int((point.ymin * h )), int((point.width * w )), int((point.height * h ))
        x1, y1 = x+w, y+h

        #Top left
        cv2.line(image, (x, y), (x+l, y), color, t )
        cv2.line(image, (x, y), (x, y+l), color, t )

        #Top right
        cv2.line(image, (x1, y), (x1-l, y), color, t )
        cv2.line(image, (x1, y), (x1, y+l), color, t )

         #Bottom right
        cv2.line(image, (x, y1), (x+l, y1), color, t )
        cv2.line(image, (x, y1), (x, y1-l), color, t )

         #Bottom left
        cv2.line(image, (x1, y1), (x1-l, y1), color, t )
        cv2.line(image, (x1, y1), (x1, y1-l), color, t )

        cv2.putText(image, label, (x,y- 20 ), font, font_scale, color, t)

    def crop_face(self, imgarray, bound_box_c, margin=20, size=224):
        """
        Args:
            imgarray: full image
            bound_box_c: face detected area (xmin, ymin, widht, height)
            margin: add some margin to the face detected area to include a full head
            size: the result image resolution with be (size x size)
        Return:
         resized image in numpy array with shape (size x size x 3)
        """
        img_h, img_w, _ = imgarray.shape
        (x, y, w, h) = int(bound_box_c.xmin * img_w ), int(bound_box_c.ymin * img_h ), int(bound_box_c.width * img_w ), int(bound_box_c.height * img_h )

        #Boundaries for cropping the image
        x_a = x - margin
        y_a = y - margin
        x_b = x + w + margin
        y_b = y + h + margin
        if x_a < 0:
            x_b = min(x_b - x_a, img_w-1)
            x_a = 0
        if y_a < 0:
            y_b = min(y_b - y_a, img_h-1)
            y_a = 0
        if x_b > img_w:
            x_a = max(x_a - (x_b - img_w), 0)
            x_b = img_w
        if y_b > img_h:
            y_a = max(y_a - (y_b - img_h), 0)
            y_b = img_h

        #Cropping, Resizing and converting to 1D array
        cropped = imgarray[y_a: y_b, x_a: x_b]
        resized_img = cv2.resize(cropped, (224, 224), interpolation=cv2.INTER_AREA)
        resized_img = np.array(resized_img)

        return resized_img, (x_a, y_a, x_b - x_a, y_b - y_a)

    def identify_face(self, features, threshold=100):
        ''' Computes the eculidean distance between the predited and generated features(.pkl)
            and'return the label having minimum euclidean distance '''

        distances = []
        for person in self.precompute_features_map:
            person_features = person.get("features")
            distance = spatial.distance.euclidean(person_features, features)
            distances.append(distance)
        min_distance_value = min(distances)
        min_distance_index = distances.index(min_distance_value)
        if min_distance_value < threshold:
            return self.precompute_features_map[min_distance_index].get("name")
        else:
            return "Unkown"

    def detect_face(self, Instances=5):
        '''Detects 5(default) instance of the face and returns the most occured name'''

        # 0 means the default video capture device in OS
        video_capture = cv2.VideoCapture(0)
        ptime = 0
        person=[]

        # infinite loop, break by key ESC
        while True:
            if not video_capture.isOpened():
                time.sleep(5)

            # Capture frame-by-frame
            ret, frame = video_capture.read()
            faces = self.face_detection.process(frame)
            ctime = time.time()
            fps = (1/(ctime - ptime))
            ptime = ctime

            # placeholder for cropped
            if faces.detections:
                face_imgs = np.empty((len(faces.detections), self.face_size, self.face_size, 3))
                for id, detection in enumerate(faces.detections):
                    face_img, cropped = self.crop_face(frame, detection.location_data.relative_bounding_box, margin=10, size=self.face_size)
                    (x, y, w, h) = cropped
                    face_imgs[id, :, :, :] = face_img
                if len(face_imgs) > 0:
                    # generate features for each face
                    features_faces = self.model.predict(face_imgs)
                    predicted_names = [self.identify_face(features_face) for features_face in features_faces]

                # draw results
                for id, detection in enumerate(faces.detections):
                    label = "{}".format(predicted_names[id])
                    person.append(label)
                    self.draw_label(frame, (detection.location_data.relative_bounding_box) , label)

            cv2.putText(frame , f'FPS : {int(fps)}', (10,50), cv2.FONT_HERSHEY_SIMPLEX,1, (255,255,255), 1)
            cv2.imshow('Detecting', frame)

            # Keyboard Interrupt ('q' to quit)
            if cv2.waitKey(20) == ord('q') or len(person)>(Instances- 1):
                break

        # When everything is done, release the capture
        video_capture.release()
        cv2.destroyAllWindows()
        return person

def main():
    '''Returns a list of face detected (length = 5[default] ) '''

    face = FaceIdentify(precompute_features_file="./features.pickle")
    person = face.detect_face(Instances=5)
    return person

if __name__ == "__main__":

    detected_person = main()
    person = max(set(detected_person), key = detected_person.count)

1st Externship Batch/Face Recognition System/Recogniser.py


import sqlite3
from sqlite3 import Error

class DatabaseFiller:
    def __init__(self) -> None:
        self.db_filename = "game_stats.db"
        self.create_connection()
        self.create__map_name_table()
        self.create_table()
        pass

    def create_connection(self):
        """ create a database connection to a SQLite database """
        conn = None
        try:
            conn = sqlite3.connect(self.db_filename)
            print(sqlite3.version)
        except Error as e:
            print(e)
        finally:
            if conn:
                conn.close()

    def create__map_name_table(self):
        conn = sqlite3.connect(self.db_filename)
        c = conn.cursor()
        c.execute("""CREATE TABLE IF NOT EXISTS map_name (
                                                        id integer PRIMARY KEY,
                                                        map_name text NOT NULL,
                                                        original_console text NOT NULL
                                                        )""")
        # if the table map_name is empty, insert the data from the Mario_kart8deluxe_maps_with_dlcs_map.csv file
        if c.execute("""SELECT COUNT(*) FROM map_name""").fetchone()[0] == 0:
            # read a csv file and insert the data in the table map_name
            with open('Mario_kart8deluxe_maps_with_dlcs_map.csv', 'r') as file:
                for line in file:
                    line = line.split(',')
                    c.execute(
                        "INSERT INTO map_name (map_name,original_console) VALUES (?,?)", (line[0], line[1]))

        conn.commit()
        conn.close()

    def create_table(self):
        conn = sqlite3.connect(self.db_filename)
        c = conn.cursor()

        c.execute("""CREATE TABLE IF NOT EXISTS game (
                                                        id integer PRIMARY KEY,
                                                        date text NOT NULL
                                                        )""")
        c.execute("""CREATE TABLE IF NOT EXISTS leaderboard (
                                                id integer PRIMARY KEY,
                                                game_id integer NOT NULL,
                                                player_name text NOT NULL,
                                                player_score integer NOT NULL,
                                                FOREIGN KEY (game_id) REFERENCES game(id)
                                                )""")
        c.execute("""CREATE TABLE IF NOT EXISTS maps_played (
                                                id integer PRIMARY KEY,
                                                game_id integer NOT NULL,
                                                map_name text NOT NULL,
                                                FOREIGN KEY (game_id) REFERENCES game(id)
                                                )""")
        conn.commit()
        conn.close()

    def insert_data(self, map_name, players, scores, date):
        conn = sqlite3.connect(self.db_filename)
        c = conn.cursor()
        # insert the data in the table game
        c.execute("INSERT INTO game (date) VALUES (?)", (date,))
        game_id = c.lastrowid
        for player, score in zip(players, scores):
            c.execute("INSERT INTO leaderboard (game_id, player_name, player_score) VALUES (?,?,?)", (game_id, player, score))
        for map in map_name:
            c.execute("INSERT INTO maps_played (game_id, map_name) VALUES (?,?)", (game_id, map))
        conn.commit()
        conn.close()

if __name__ == '__main__':
    db = DatabaseFiller()


from __future__ import print_function

import time

import numpy as np
import sympy as sy

from bokeh.objects import Plot, DataRange1d, LinearAxis, ColumnDataSource, Glyph, Grid, Legend
from bokeh.widgetobjects import Slider, TextInput, HBox, VBox, Dialog
from bokeh.glyphs import Patch, Line, Text
from bokeh.document import Document
from bokeh.session import Session

from requests.exceptions import ConnectionError

document = Document()
session = Session()
session.use_doc('taylor_server')
session.load_document(document)

xs = sy.Symbol('x')
expr = sy.exp(-xs)*sy.sin(xs)
order = 1

def taylor(fx, xs, order, x_range=(0, 1), n=200):
    x0, x1 = x_range
    x = np.linspace(float(x0), float(x1), n)

    fy = sy.lambdify(xs, fx, modules=['numpy'])(x)
    tx = fx.series(xs, n=order).removeO()

    if tx.is_Number:
        ty = np.zeros_like(x)
        ty.fill(float(tx))
    else:
        ty = sy.lambdify(xs, tx, modules=['numpy'])(x)

    return x, fy, ty

def update_data():
    x, fy, ty = taylor(expr, xs, order, (-2*sy.pi, 2*sy.pi), 200)

    plot.title = "%s vs. taylor(%s, n=%d)" % (expr, expr, order)
    legend.legends = {
        "%s"         % expr: [line_f_glyph],
        "taylor(%s)" % expr: [line_t_glyph],
    }
    source.data = dict(x=x, fy=fy, ty=ty)
    slider.value = order

    session.store_document(document)

source = ColumnDataSource(data=dict(
    x  = [],
    fy = [],
    ty = [],
))

xdr = DataRange1d(sources=[source.columns("x")])
ydr = DataRange1d(sources=[source.columns("fy")])

plot = Plot(data_sources=[source], x_range=xdr, y_range=ydr, plot_width=800, plot_height=400)

line_f = Line(x="x", y="fy", line_color="blue", line_width=2)
line_f_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line_f)
plot.renderers.append(line_f_glyph)

line_t = Line(x="x", y="ty", line_color="red", line_width=2)
line_t_glyph = Glyph(data_source=source, xdata_range=xdr, ydata_range=ydr, glyph=line_t)
plot.renderers.append(line_t_glyph)

xaxis = LinearAxis(plot=plot, location="bottom")
plot.below.append(xaxis)
yaxis = LinearAxis(plot=plot, location="left")
plot.left.append(yaxis)

xgrid = Grid(plot=plot, dimension=0, axis=xaxis)
ygrid = Grid(plot=plot, dimension=1, axis=yaxis)

legend = Legend(plot=plot, orientation="bottom_left")
plot.renderers.append(legend)

def on_slider_value_change(obj, attr, old, new):
    global order
    order = int(new)
    update_data()

def on_text_value_change(obj, attr, old, new):
    try:
        global expr
        expr = sy.sympify(new, dict(x=xs))
    except (sy.SympifyError, TypeError, ValueError) as exception:
        dialog.content = str(exception)
        dialog.visible = True
        session.store_objects(dialog)
    else:
        update_data()

dialog = Dialog(title="Invalid expression", buttons=["Close"])

slider = Slider(start=1, end=20, value=order, step=1, title="Order:")
slider.on_change('value', on_slider_value_change)

text = TextInput(value=str(expr), title="Expression:")
text.on_change('value', on_text_value_change)

inputs = HBox(children=[slider, text])
layout = VBox(children=[inputs, plot, dialog])

document.add(layout)
update_data()

if __name__ == "__main__":
    link = session.object_link(document._plotcontext)
    print("Please visit %s to see the plots" % link)

    try:
        while True:
            session.load_document(document)
            time.sleep(0.5)
    except KeyboardInterrupt:
        print()
    except ConnectionError:
        print("Connection to bokeh-server was terminated")


import numpy as np
import matplotlib.pyplot as plt
import imageio
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import scipy.constants as const
import scipy.sparse as sp
import scipy.sparse.linalg as la

def gaussian_pulse_1d(l, x0, sigma, dt, tf, kappa):
    n = 1000
    alpha = l / n
    t = np.arange(0, tf, dt)
    x = np.arange(0, l, alpha)
    hbar = const.physical_constants["Planck constant over 2 pi in eV s"][0]
    mass = const.physical_constants["electron mass energy equivalent in MeV"][0] * 1e6 / const.speed_of_light ** 2
    beta = -1j * hbar * dt / (4 * mass * alpha**2)

    # Initialize matrices A and B
    A = np.eye(n, k=-1, dtype=complex) * -beta + \
        np.eye(n, k=0, dtype=complex) * (1 + 2 * beta) + \
        np.eye(n, k=1, dtype=complex) * -beta
    B = np.eye(n, k=-1, dtype=complex) * beta + \
        np.eye(n, k=0, dtype=complex) * (1 - 2 * beta) + \
        np.eye(n, k=1, dtype=complex) * beta

    # Initialize psi and set initial pulse(s)
    psi = np.zeros((len(t), len(x)), dtype=complex)
    for i in range(len(x0)):
        psi[0, :] += np.exp(-1 * (x - x0[i])**2 / (2 * sigma**2)) * np.exp(1j * kappa[i] * x)

    for i in range(0, len(t)-1, 1):
        b = np.matmul(B, psi[i, :])
        psi[i + 1, :] = np.linalg.solve(A, b)

    return x, t, psi

def gaussian_pulse_2d_movie(l, x0, y0, sigma, dt, tf, kx, ky):
    # Initialize constants
    n = 100
    alpha = l / n
    t = np.arange(0, tf, dt)
    x = np.arange(0, l, alpha)
    y = np.arange(0, l, alpha)
    gx, gy = np.meshgrid(x, y)
    hbar = const.physical_constants["Planck constant over 2 pi in eV s"][0]
    mass = const.physical_constants["electron mass energy equivalent in MeV"][0] * 1e6 / const.speed_of_light ** 2
    beta = -1j * hbar * dt / (4 * mass * alpha ** 2)

    # Initialize matrices A and B
    A = sp.eye(n, k=-1, dtype=complex) * -beta + \
        sp.eye(n, k=0, dtype=complex) * (1 - 4 * beta) + \
        sp.eye(n, k=1, dtype=complex) * -beta
    A = sp.kron(sp.eye(n), A)
    A += sp.eye(A.shape[0], k=-n, dtype=complex) * -beta + \
         sp.eye(A.shape[0], k=n, dtype=complex) * -beta
    B = sp.eye(n, k=-1, dtype=complex) * beta + \
        sp.eye(n, k=0, dtype=complex) * (1 + 4 * beta) + \
        sp.eye(n, k=1, dtype=complex) * beta
    B = sp.kron(sp.eye(n), B)
    B += sp.eye(B.shape[0], k=-n, dtype=complex) * beta + \
         sp.eye(B.shape[0], k=n, dtype=complex) * beta

    # Initialize psi and set initial pulse(s)
    psi = np.zeros((len(x), len(y)), dtype=complex)
    for i in range(len(x0)):
        psi[:, :] += np.exp(-1 * ((gx - x0[i]) ** 2 / (2 * sigma ** 2) + (gy - y0[i]) ** 2 / (2 * sigma ** 2))) * \
                        np.exp(1j * (kx[i] * gx + ky[i] * gy))
    psi = psi.flatten()

    # Simulate and make .mp4
    with imageio.get_writer('pulse2db.mp4', fps=100, mode='I') as writer:
        for i in range(0, len(t) - 1, 1):
            b = B @ psi  # np.matmul(B, psi)
            psi = la.spsolve(A, b)

            fig = plt.figure(1)
            ax = plt.axes(projection='3d')
            ax.view_init(elev=45.0, azim=-45.0)
            ax.plot_surface(gx, gy, np.real(psi.reshape([n, n])), cmap='inferno', vmin=-1.0, vmax=1.0,
                            rstride=1, cstride=1, alpha=None, antialiased=True)
            ax.set_title(f'Gaussian Pulse\nTime = {t[i] * 1.0e15:.4f} [fs]')
            ax.set_xlabel("Distance [m]")
            ax.set_ylabel("Distance [m]")
            ax.set_zlabel("|Ψ(x)|")
            ax.set_xlim([0.0, l])
            ax.set_ylim([0.0, l])
            ax.set_zlim([0.0, 1.0])

            # plt.imshow(np.real(psi.reshape([n, n])), vmin=-1.0, vmax=1.0)
            # plt.xlabel('x')
            # plt.ylabel('y')

            canvas = FigureCanvas(fig)
            canvas.draw()
            writer.append_data(np.asarray(canvas.buffer_rgba()))
            plt.close(1)

            try:
                print(f'Step: {i}/{(len(t)-1)}, Time: {t[i] * 1.0e15:.4f}/{tf * 1.0e15:.4f} [fs]')
            except KeyboardInterrupt:
                print("user close")
                break

        writer.close()
        print("successful close")

    return

if __name__ == '__main__':
    from matplotlib import rc
    rc('font', **{'family': 'serif', 'serif': ['DejaVu Serif']})
    SMALL_SIZE = 10
    MEDIUM_SIZE = 12
    BIGGER_SIZE = 14
    plt.rc('font', size=MEDIUM_SIZE)       # controls default text sizes
    plt.rc('axes', titlesize=BIGGER_SIZE)  # fontsize of the axes title
    plt.rc('axes', labelsize=MEDIUM_SIZE)  # fontsize of the x and y labelsa
    plt.rc('xtick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
    plt.rc('ytick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
    plt.rc('legend', fontsize=SMALL_SIZE)  # legend fontsize
    plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

    # Question 5
    l = 1.0e-8              # [m]
    x0 = np.array([l/4, l/2, l*3/4])    # [m]
    sigma = 1.0e-10         # [m]
    dt = 1.0e-18            # [s]
    tf = 3.0e-15            # [s]
    kappa = np.array([5.0e10, 0.0, -5.0e10])          # [m^-1]

    x, t, p = gaussian_pulse_1d(l, x0, sigma, dt, tf, kappa)
    print("wave processed")

    with imageio.get_writer('three_pulse_1d_multi.mp4', fps=100, mode='I') as writer:
        for i in range(len(t)):
            fig = plt.figure(1, figsize=[12, 7])
            #plt.suptitle(f'Time = {t[i]*1.0e15:.4f} [fs]')

            plt.subplot(311)
            plt.plot(x, np.real(p[i, :]), 'k')
            plt.title(f'Time = {t[i] * 1.0e15:.4f} [fs]')
            #plt.title(f'Real')
            plt.xlabel("Distance [m]")
            plt.ylabel("Re[Ψ(x)]")
            plt.ylim([-1.0, 1.0])

            plt.subplot(312)
            plt.plot(x, np.imag(p[i, :]), 'k')
            #plt.title(f'Imaginary')
            plt.xlabel("Distance [m]")
            plt.ylabel("Im[Ψ(x)]")
            plt.ylim([-1.0, 1.0])

            plt.subplot(313)
            plt.plot(x, np.sqrt(np.real(p[i, :])**2 + np.imag(p[i, :])**2), 'k')
            #plt.title(f'Magnitude')
            plt.xlabel("Distance [m]")
            plt.ylabel("|Ψ(x)|")
            plt.ylim([-1.0, 1.0])

            plt.tight_layout()

            canvas = FigureCanvas(fig)
            canvas.draw()
            writer.append_data(np.asarray(canvas.buffer_rgba()))
            plt.close(1)

        writer.close()
    print("movie processed")

    # Bonus
    # l = 1.0e-8              # [m]
    # sigma = 1.0e-10         # [m]
    # dt = 1.0e-18            # [s]
    # tf = 10.0e-15            # [s]
    # speed = 5.0e10
    # x0 = np.array([l/4, l/4, l*3/4, l*3/4])    # [m]
    # y0 = np.array([l/4, l*3/4, l/4, l*3/4])    # [m]
    # kx = np.array([1.0, 0.0, 0.0, -1.0]) * speed  # [m^-1]
    # ky = np.array([0.0, -1.0, 1.0, 0.0]) * speed  # [m^-1]
    # x0 = np.array([l/2])
    # y0 = np.array([l/2])
    # kx = np.array([0.0])
    # ky = np.array([0.0])

    # gaussian_pulse_2d_movie(l, x0, y0, sigma, dt, tf, kx, ky)


import json
import subprocess
from contextlib import suppress

from bbot.modules.base import BaseModule

class masscan(BaseModule):
    flags = ["active", "portscan", "aggressive"]
    watched_events = ["SCAN"]
    produced_events = ["OPEN_TCP_PORT"]
    meta = {"description": "Port scan IP subnets with masscan"}
    # 600 packets/s ~= entire private IP space in 8 hours
    options = {"ports": "80,443", "rate": 600, "wait": 10, "ping_first": False, "use_cache": False}
    options_desc = {
        "ports": "Ports to scan",
        "rate": "Rate in packets per second",
        "wait": "Seconds to wait for replies after scan is complete",
        "ping_first": "Only portscan hosts that reply to pings",
        "use_cache": "Instead of scanning, use the results from the previous scan",
    }
    deps_ansible = [
        {
            "name": "install dev tools",
            "package": {"name": ["gcc", "git", "make"], "state": "present"},
            "become": True,
            "ignore_errors": True,
        },
        {
            "name": "Download masscan source code",
            "git": {
                "repo": "https://github.com/robertdavidgraham/masscan.git",
                "dest": "#{BBOT_TEMP}/masscan",
                "single_branch": True,
                "version": "master",
            },
        },
        {
            "name": "Build masscan",
            "command": {
                "chdir": "#{BBOT_TEMP}/masscan",
                "cmd": "make -j",
                "creates": "#{BBOT_TEMP}/masscan/bin/masscan",
            },
        },
        {
            "name": "Install masscan",
            "copy": {"src": "#{BBOT_TEMP}/masscan/bin/masscan", "dest": "#{BBOT_TOOLS}/", "mode": "u+x,g+x,o+x"},
        },
    ]
    _qsize = 100

    async def setup(self):
        self.ports = self.config.get("ports", "80,443")
        self.rate = self.config.get("rate", 600)
        self.wait = self.config.get("wait", 10)
        self.ping_first = self.config.get("ping_first", False)
        self.alive_hosts = dict()
        # make a quick dry run to validate ports etc.
        self._target_findkey = "9.8.7.6"
        if not self.helpers.in_tests:
            try:
                dry_run_command = self._build_masscan_command(self._target_findkey, dry_run=True)
                dry_run_result = await self.helpers.run(dry_run_command)
                self.masscan_config = dry_run_result.stdout
                self.masscan_config = "\n".join(l for l in self.masscan_config.splitlines() if "nocapture" not in l)
            except subprocess.CalledProcessError as e:
                self.warning(f"Error in masscan: {e.stderr}")
                return False

        self.run_time = self.helpers.make_date()
        self.use_cache = self.config.get("use_cache", False)
        self.ping_cache = self.scan.home / f"masscan_ping.txt"
        self.syn_cache = self.scan.home / f"masscan_syn.txt"
        if self.use_cache:
            files_exist = self.ping_cache.is_file() or self.syn_cache.is_file()
            files_empty = self.helpers.filesize(self.ping_cache) == 0 and self.helpers.filesize(self.syn_cache) == 0
            if not files_exist:
                return (
                    False,
                    f"use_cache is True but could not find cache file at {self.ping_cache} or {self.syn_cache}",
                )
            if files_empty:
                return (
                    False,
                    f"use_cache is True but could cached files {self.ping_cache} and {self.syn_cache} are empty",
                )
        else:
            self.helpers.depsinstaller.ensure_root(message="Masscan requires root privileges")
        self.ping_cache_fd = None
        self.syn_cache_fd = None
        return True

    async def handle_event(self, event):
        if self.use_cache:
            self.emit_from_cache()
        else:
            exclude, invalid_exclude = self._build_targets(self.scan.blacklist)
            targets, invalid_targets = self._build_targets(self.scan.whitelist)
            if invalid_exclude > 0:
                self.warning(
                    f"Masscan can only accept IP addresses or IP ranges for blacklist ({invalid_exclude:,} blacklisted were hostnames)"
                )
            if invalid_targets > 0:
                self.warning(
                    f"Masscan can only accept IP addresses or IP ranges as target ({invalid_targets:,} targets were hostnames)"
                )

            if not targets:
                self.warning("No targets specified")
                return

            # ping scan
            if self.ping_first:
                self.verbose("Starting masscan (ping scan)")

                await self.masscan(targets, result_callback=self.append_alive_host, exclude=exclude, ping=True)
                targets = ",".join(str(h) for h in self.alive_hosts)
                if not targets:
                    self.warning("No hosts responded to pings")
                    return

            # TCP SYN scan
            if self.ports:
                self.verbose("Starting masscan (TCP SYN scan)")
                await self.masscan(targets, result_callback=self.emit_open_tcp_port, exclude=exclude)
            else:
                self.verbose("No ports specified, skipping TCP SYN scan")
            # save memory
            self.alive_hosts.clear()

    async def masscan(self, targets, result_callback, exclude=None, ping=False):
        # config file
        masscan_config = self.masscan_config.replace(self._target_findkey, targets)
        self.debug("Masscan config:")
        for line in masscan_config.splitlines():
            self.debug(line)
        config_file = self.helpers.tempfile(masscan_config)
        # output file
        # process_output = functools.partial(self.process_output, result_callback=result_callback)
        # json_output_file = self.helpers.tempfile_tail(process_output)
        # command
        command = self._build_masscan_command(config=config_file, exclude=exclude, ping=ping)
        # execute
        stats_file = self.helpers.tempfile_tail(callback=self.verbose)
        try:
            with open(stats_file, "w") as stats_fh:
                async for line in self.helpers.run_live(command, sudo=True, stderr=stats_fh):
                    self.process_output(line, result_callback=result_callback)
        finally:
            stats_file.unlink()

    def _build_masscan_command(self, targets=None, config=None, exclude=None, dry_run=False, ping=False):
        command = ("masscan", "--rate", self.rate, "--wait", self.wait, "--open-only", "-oJ", "-")
        if targets is not None:
            command += (targets,)
        if config is not None:
            command += ("-c", config)
        if ping:
            command += ("--ping",)
        elif not dry_run:
            command += ("-p", self.ports)
        if exclude is not None:
            command += ("--exclude", exclude)
        if dry_run:
            command += ("--echo",)
        return command

    def process_output(self, line, result_callback):
        try:
            j = json.loads(line)
        except Exception:
            return
        ip = j.get("ip", "")
        if not ip:
            return
        ports = j.get("ports", [])
        if not ports:
            return
        for p in ports:
            proto = p.get("proto", "")
            port_number = p.get("port", "")
            if proto == "" or port_number == "":
                continue
            result = str(ip)
            source = None
            with suppress(KeyError):
                source = self.alive_hosts[ip]
            if proto != "icmp":
                result += f":{port_number}"
                if source is None:
                    source = self.make_event(ip, "IP_ADDRESS", source=self.get_source_event(ip))
                    self.emit_event(source)
            result_callback(result, source=source)

    def append_alive_host(self, host, source):
        host_event = self.make_event(host, "IP_ADDRESS", source=self.get_source_event(host))
        self.alive_hosts[host] = host_event
        self._write_ping_result(host)
        self.emit_event(host_event)

    def emit_open_tcp_port(self, data, source):
        self._write_syn_result(data)
        self.emit_event(data, "OPEN_TCP_PORT", source=source)

    def emit_from_cache(self):
        ip_events = {}
        # ping scan
        if self.ping_cache.is_file():
            cached_pings = list(self.helpers.read_file(self.ping_cache))
            if cached_pings:
                self.success(f"{len(cached_pings):,} hosts loaded from previous ping scan")
            else:
                self.verbose(f"No hosts cached from previous ping scan")
            for ip in cached_pings:
                if self.scan.stopping:
                    break
                ip_event = self.make_event(ip, "IP_ADDRESS", source=self.get_source_event(ip))
                ip_events[ip] = ip_event
                self.emit_event(ip_event)
        # syn scan
        if self.syn_cache.is_file():
            cached_syns = list(self.helpers.read_file(self.syn_cache))
            if cached_syns:
                self.success(f"{len(cached_syns):,} hosts loaded from previous SYN scan")
            else:
                self.warning(f"No hosts cached from previous SYN scan")
            for line in cached_syns:
                if self.scan.stopping:
                    break
                host, port = self.helpers.split_host_port(line)
                host = str(host)
                source_event = ip_events.get(host)
                if source_event is None:
                    self.verbose(f"Source event not found for {line}")
                    source_event = self.make_event(line, "IP_ADDRESS", source=self.get_source_event(line))
                    self.emit_event(source_event)
                self.emit_event(line, "OPEN_TCP_PORT", source=source_event)

    def get_source_event(self, host):
        source_event = self.scan.whitelist.get(host)
        if source_event is None:
            source_event = self.scan.root_event
        return source_event

    def _build_targets(self, target):
        invalid_targets = 0
        targets = []
        for t in target:
            t = self.helpers.make_ip_type(t.data)
            if isinstance(t, str):
                invalid_targets += 1
            else:
                targets.append(t)
        return ",".join(str(t) for t in targets), invalid_targets

    async def cleanup(self):
        if self.ping_first:
            with suppress(Exception):
                self.ping_cache_fd.close()
        with suppress(Exception):
            self.syn_cache_fd.close()

    def _write_ping_result(self, host):
        if self.ping_cache_fd is None:
            self.helpers.backup_file(self.ping_cache)
            self.ping_cache_fd = open(self.ping_cache, "w")
        self.ping_cache_fd.write(f"{host}\n")
        self.ping_cache_fd.flush()

    def _write_syn_result(self, data):
        if self.syn_cache_fd is None:
            self.helpers.backup_file(self.syn_cache)
            self.syn_cache_fd = open(self.syn_cache, "w")
        self.syn_cache_fd.write(f"{data}\n")
        self.syn_cache_fd.flush()


import nltk
import random
from nltk.corpus import movie_reviews
from nltk.tokenize import word_tokenize
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.naive_bayes import MultinomialNB

# nltk.download('movie_reviews')
# nltk.download('punkt')
def extract_features(words):
    return dict([(word, True) for word in words])

documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)

featuresets = [(extract_features(words), category) for (words, category) in documents]

train_set, test_set = featuresets[100:], featuresets[:100]
classifier = SklearnClassifier(MultinomialNB())
classifier.train(train_set)

print("Accuracy:", nltk.classify.accuracy(classifier, test_set))
input_sentence = input("Enter a sentence: ")
words = word_tokenize(input_sentence)
features =extract_features(words)
print("Predicted sentiment:", classifier.classify(features))


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from db_methods import DatabaseS
from json import dumps
from models import FilterData, FilterDataTT
# from starlette.status import HTTP_403_FORBIDDEN
# import json

app = FastAPI()

origins = ["*",]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return ""

@app.get('/filters_main/{typee}')
async def get_filters_main(typee: str):
    return dumps({'result': DatabaseS().get_filters_main(typee=typee)})

@app.put('/filter_data/')
async def get_filter_data(data: FilterData):
    return DatabaseS().filter_data(data)

@app.put('/filter_data_tt/')
async def get_filter_data_tt(data: FilterDataTT):
    return DatabaseS().filter_data_tt(data)

@app.delete('/remove_filter')
async def remove_filter(data: FilterData):
    DatabaseS().remove_filter(data)
    return {'removed': True}

@app.get('/get_additions')
async def get_additions():
    return DatabaseS().get_additions()

@app.get('/get_uniqe_typees')
async def get_uniqe_typees():
    return DatabaseS().get_uniqe_typees()

@app.get('/get_uniqe_targets')
async def get_uniqe_targets():
    return DatabaseS().get_uniqe_targets()


#! /usr/bin/env python
#-*- coding: utf-8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

"""
    Views for Microbe app
"""

__author__ = 'TROUVERIE Joachim'

import os.path as op
import shelve
import xml.etree.ElementTree as ET
from datetime import datetime
from werkzeug.contrib.atom import AtomFeed
from urlparse import urljoin

from microbe import app, contents, babel
from microbe.utils import render, render_list
from microbe.mods.search import search_query
from microbe.flatcontent.forms import CommentForm
from microbe.mods.links.models import Links
from microbe.mods.search.forms import SearchForm

from flask.ext.babel import format_datetime, lazy_gettext
from flask.ext.themes2 import static_file_url, get_theme
from flask import (g, request, abort, url_for, make_response,
                  render_template, redirect)

@app.errorhandler(404)
def page_not_found(e):
    """
        Page not found error handler
    """
    return render('404.html'), 404

@babel.localeselector
def get_locale() :
    """
        Get locale for page translations
    """
    return app.config.get('LANGUAGE')

@app.template_filter('date')
def date_filter(date, format = None) :
    """
        Date filter to use in Jinja2 templates

        :param date : datetime object to convert
        :param format: format to convert date object
        :type date: datetime
        :type format: str

        .. warnings:: Format is in flask.ext.babel format
    """
    if date :
        if format :
            return format_datetime(date, format)
        else :
            return format_datetime(date, 'dd MM yyyy')
    else :
        return ''

@app.before_request
def before_request() :
    """
        Refresh global vars before each requests
    """
    # update config
    path = app.config['SHELVE_FILENAME']
    db = shelve.open(path)
    app.config.update(db)
    # close db
    db.close()
    # posts list
    g.posts  = sorted(
                [c for c in contents if c.content_type == 'posts'
                and not c.draft],
                key = lambda x : x.published,
                reverse = True
                )
    # posts categories
    g.categories = set([c.category for c in g.posts if c.category])
    # static pages
    g.static_pages = [c for c in contents if c.content_type == 'pages'
                     and not c.draft]
    # links
    g.links = Links.get_all()
    # search form
    if not hasattr(g, 'search_form') :
        g.search_form = SearchForm()

@app.route('/')
def index():
    """
        Main page of the app

        List of blog posts summaries
    """
    return render_list('index.html', g.posts)

@app.route('/sitemaps.xml')
def sitemap() :
    """
        Site sitemap
    """
    # list all contents
    sitemap_contents = [c for c in contents if not c.draft]
    # root
    root = ET.Element('urlset')
    root.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
    for content in sitemap_contents :
        url = ET.SubElement(root, 'url')
        loc = ET.SubElement(url, 'loc')
        loc.text = urljoin(request.url_root, content.path)
        lastmod = ET.SubElement(url, 'lastmod')
        lastmod.text = format_datetime(content.published, 'yyyy-MM-dd')
    text = ET.tostring(root)
    response = make_response(text, 200)
    response.headers['Content-Type'] = 'application/xml'
    return response

@app.route('/favicon.ico')
def favicon() :
    """
        App favicon
    """
    default = app.config['DEFAULT_THEME']
    theme_id = app.config.get(u'THEME', default)
    theme = get_theme(theme_id)
    path = op.join(theme.static_path, 'img', 'favicon.png')
    if op.exists(path) :
        url = static_file_url(theme, 'img/favicon.png')
    else :
        url = url_for('static', filename='img/favicon.png')
    return redirect(url)

@app.route('/robots.txt')
def robots() :
    """
        App robots.txt
    """
    try :
        url = url_for('static', filename='media/robots.txt')
        return redirect(url)
    except :
        abort(404)

@app.route('/<path:path>/', methods = ['GET', 'POST'])
def page(path):
    """
        Get access for page from its path.
        If path is not valid it will return a 404 error page

        :param path: Valid content path
    """
    content = contents.get_or_404(path)
    form = None
    # enable comments for posts only
    if content.content_type == 'posts' :
        form = CommentForm()
    # form management
    if form and form.validate_on_submit() :
        author = form.name.data
        body = form.content.data
        content.add_comment(author, body)
    return render('page.html', page = content, form = form)

@app.route('/category/<category>')
def category(category) :
    """
        Filter posts by category

        :param category: Category to filter contents
        :type category: str
    """
    posts = [p for p in g.posts if p.category == category]
    return render_list('index.html', posts, title = category)

@app.route('/tag/<tag>')
def tag(tag) :
    """
        Filter posts by tag

        :param tag: Category to filter contents
        :type tag: str
    """
    posts = [p for p in g.posts if tag in p.tags.split(',')]
    return render_list('index.html', posts, title = tag)

@app.route('/archives')
def archives() :
    """
       List all contents order by reverse date
    """
    # sort pages by reverse date
    sorted_pages = sorted(
                        contents,
                        key = lambda x : x.published,
                        reverse = True
                    )
    return render_list('archive.html', sorted_pages, per_page=10)

@app.route('/search/', methods = ['POST'])
def search() :
    """
        Search in contents
    """
    if not g.search_form.validate_on_submit() :
        return redirect(url_for('index'))
    query = g.search_form.search.data
    contents = search_query(query)
    # sort not draft contents by reverse date
    sorted_contents = sorted(
                            [c for c in contents if not c.draft],
                            key = lambda x : x.published,
                            reverse = True
                       )
    return render_list('index.html', sorted_contents, per_page=10)

@app.route('/feed.atom')
def feed() :
    """
        Generate 20 last content atom feed
    """
    if app.config.get('RSS', 'NO') != 'YES' :
        abort(404)
    name = app.config['SITENAME']
    feed = AtomFeed(name,feed_url=request.url, url=request.url_root)
    # sort posts by reverse date
    for post in g.posts[:20] :
        feed.add( post.title,
                unicode(post.summary),
                content_type = 'html',
                author = post.meta.get('author', ''),
                url = urljoin(request.url_root, post.path),
                updated = post.published)
    return feed.get_response()


from PIL import Image
import sys

def main():
    infile = sys.argv[1]

    image = Image.open(infile)
    inPixels = image.load()

    width = image.size[0]
    height = image.size[1]

    for i in range(width):
        for j in range(height):
            if inPixels[i, j] == (255, 255, 255, 255):
                inPixels[i, j] = (255, 255, 255, 0)

    image.save(infile)

if __name__ == "__main__":
    main()


import pprint

__author__ = 'ronanbrady2'

# Connects to 'udwrangle' DB and returns DB object
def connectDB() :
    from pymongo import MongoClient
    client = MongoClient("mongodb://localhost:27017")
    db = client.udwrangle
    return db

if __name__ == "__main__":
    db = connectDB()

    mapping = { "Ave": "Avenue",
            "Avevnue": "Avenue",
            "Cente": "Centre",
            "Center": "Centre",
            "Center": "Centre",
            "Donghmede": "Donaghmede",
            "Nouth": "North",
            "Roafd": "Road",
            "Sreet": "Street",
            "St": "Street",
            "street": "Street",
            "heights": "Heights",
            "lane": "Lane",
            "park": "Park",
            "place": "Place",
            "road": "Road"
            }

    correctedcount = 0
    for misspelling, correction in mapping.items():
        pprint.pprint(misspelling)
        regex = "\w " + misspelling + "$"
        results = db.openmap.find({"address.street" : { "$regex" : regex}})
        for obj in results:
            street = obj["address"]["street"]
            print(street)

            correctedstreet = street.replace(misspelling, correction)
            obj["address"]["street"] = correctedstreet
            print(correctedstreet)
            correctedcount += 1
            db.openmap.save(obj)
    print("total corrected: {0}".format(correctedcount))


from pathlib import Path

class Solution():
    def __init__(self) -> None:
        self.find_top_n_elves = 3
        self.current_elf_number = 1
        self.current_calories_sum = 0
        self.list_of_elves_and_calories: list[dict[str, int]] = []

    def solve(self) -> None:
        with open(f'{Path(__file__).resolve().parent}/day_01_input.txt',
                  encoding='utf-8') as input_file:
            for line in input_file:
                if len(line.strip()) == 0:
                    self.process_next_elf()
                else:
                    self.current_calories_sum += int(line)
            self.process_next_elf()

        self.list_of_elves_and_calories.sort(key=lambda x: x['calories'],
                                             reverse=True)

        total_calories = 0
        for i in range(self.find_top_n_elves):
            print(f'Elf #{self.list_of_elves_and_calories[i]["elf"]} '
                  f'with {self.list_of_elves_and_calories[i]["calories"]} '
                  f'calories')
            total_calories += self.list_of_elves_and_calories[i]["calories"]

        print(f'Total calories carried by the top {self.find_top_n_elves} ' +
              f'elves: {total_calories}')

    def process_next_elf(self) -> None:
        self.list_of_elves_and_calories.append({
            'elf': self.current_elf_number,
            'calories': self.current_calories_sum
        })

        self.current_calories_sum = 0
        self.current_elf_number += 1

Solution().solve()


from selenium import webdriver
import os

options = webdriver.ChromeOptions()
options.add_argument("-headless")
drivePath = os.getenv("HOME") + "/Downloads/chromedriver"
browser = webdriver.Chrome(executable_path=drivePath, options=options)
url = "https://www.csie.ncnu.edu.tw/member/professor"

browser.get(url)
infos = browser.find_elements_by_class_name("personcard_wrap ")
for info in infos:
    tmp = info.text.split()
    print(tmp[0], tmp[3])
browser.close()


# forked from proj@bilke

import os
from conans import ConanFile, CMake
from conans.tools import download, unzip, patch

class ProjConan(ConanFile):
    name = "proj"
    version = "4.9.3"
    generators = "cmake"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = "shared=False"
    exports = ["CMakeLists.txt", "FindPROJ4.cmake"]
    license="https://github.com/OSGeo/proj.4"
    description = "The Proj library for projecting things"
    url = "https://github.com/barcharcraz/conan-packages"

    ZIP_FOLDER_NAME = "proj.4-%s" % version
    INSTALL_DIR = "_install"

    def source(self):
        zip_name = self.version + ".zip"
        download("https://github.com/OSGeo/proj.4/archive/%s" % zip_name , zip_name)
        unzip(zip_name)
        os.unlink(zip_name)

    def build(self):
        # produced with `diff -U 1 -p Proj4Config.cmake tmp.cmake`

        patch_content1 = '''--- cmake/Proj4Config.cmake	2016-04-25 09:27:06.000000000 +0200
+++ cmake/Proj4Config.cmake	2016-04-25 09:27:02.000000000 +0200
@@ -38,2 +38,2 @@ set(PACKAGE_VERSION "${${PROJECT_INTERN_

-configure_file(cmake/proj_config.cmake.in src/proj_config.h)
+configure_file(${PROJ4_SOURCE_DIR}/cmake/proj_config.cmake.in ${CMAKE_SOURCE_DIR}/_build/%s/src/proj_config.h)
''' % self.ZIP_FOLDER_NAME
        patch(patch_string=patch_content1, base_path=self.ZIP_FOLDER_NAME)
        patch_content2 = '''--- cmake/Proj4InstallPath.cmake	2016-04-25 09:27:06.000000000 +0200
+++ cmake/Proj4InstallPath.cmake	2016-04-25 09:28:02.000000000 +0200
@@ -24,3 +24,3 @@ ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_T

-if(WIN32)
+if(FALSE)
   set(DEFAULT_BIN_SUBDIR bin)
'''

        patch(patch_string=patch_content2, base_path=self.ZIP_FOLDER_NAME)
        cmake = CMake(self.settings)
        if self.settings.os == "Windows":
            self.run("IF not exist _build mkdir _build")
        else:
            self.run("mkdir _build")
        cd_build = "cd _build"
        CMAKE_OPTIONALS = "-DBUILD_CS2CS=ON -DBUILD_PROJ=ON -DBUILD_GEOD=ON -DBUILD_NAD2BIN=ON "
        if self.options.shared == False:
            CMAKE_OPTIONALS += "-DBUILD_LIBPROJ_SHARED=OFF "
        else:
            CMAKE_OPTIONALS += "-DBUILD_LIBPROJ_SHARED=ON "
        self.run("%s && cmake .. -DPROJ4_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../%s %s %s" % (cd_build, self.INSTALL_DIR, cmake.command_line, CMAKE_OPTIONALS))
        self.run("%s && cmake --build . %s" % (cd_build, cmake.build_config))
        self.run("%s && cmake --build . --target install %s" % (cd_build, cmake.build_config))

    def package(self):
        #self.copy("FindPROJ4.cmake", ".", ".")
        self.copy("*", dst=".", src=self.INSTALL_DIR)

    def package_info(self):
        if self.settings.os == "Windows":
            if self.settings.build_type == "Debug":
                self.cpp_info.libs = ["proj_4_9_d"]
            else:
                self.cpp_info.libs = ["proj_4_9"]
        else:
            self.cpp_info.libs = ["proj"]


from http.client import ImproperConnectionState
from operator import imod
from pkgutil import ImpImporter
import re
from django.shortcuts import render
from .models import Book, Borrower
from . import forms
from . import models
from .forms  import NewBook_form
from django.core import validators
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.views.generic import View,TemplateView,ListView,DetailView,FormView
# Create your views here.

def landing(request):
    mydect2 = {'name2':'This is a landing page !!  '}
    return render(request, "landing_page.html",context=mydect2)

class IndexView(TemplateView):
    template_name = "landing_page.html"

class BooksListView(ListView):
    paginate_by = 5
    model = Book
#    template_name = 'books/bookslist.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
#        context['book'] = Book.objects.all()
        return context

class BooksDetailView(DetailView):
    context_object_name='book_detail'
    model = Book
    template_name = 'books/book_detail.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['boook'] = Book.objects.all()
        return context

def books(request):

    books_view = Book.objects.all()
    books_dict= {'book_key': books_view}
    return render(request, "books/books_page.html",context=books_dict)

#def form_name_view(request):
#    form = forms.Register_a_book()
#    return render(request, "books/register_a_book.html",context={'form':form})

def form_name_view(request):
    form = forms.NewBook_form()
    if request.method == "POST":
        form = NewBook_form(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return books(request)
        else:

            print(form.errors)

    return render(request, "books/register_a_book.html",context={'form':form})


import importlib.util
import multiprocessing

from darcyai.perceptor.coral.coral_perceptor_base import CoralPerceptorBase
from darcyai.perceptor.processor import Processor
from darcyai.utils import validate, validate_not_none, validate_type

_supported_processors = None

def get_supported_processors() -> dict:
    """
    Gets the list of supported processors.

    # Returns
    `dict` - The list of supported processors.

    # Examples
    ```python
    >>> get_supported_processors()
    {<Processor.CORAL_EDGE_TPU: 1>: {'is_available': True, 'coral_tpus': [{'type': 'usb', 'path':\
    '/sys/bus/usb/devices/2-2'}]}, <Processor.CPU: 2>: {'is_available': True, 'cpu_count': 4}}
    ```
    """
    global _supported_processors
    if _supported_processors is None:
        coral_tpus = CoralPerceptorBase.list_edge_tpus()
        _supported_processors = {
            Processor.CORAL_EDGE_TPU: {
                "is_available": len(coral_tpus) > 0,
                "coral_tpus": coral_tpus,
            },
            Processor.CPU: {
                "is_available": importlib.util.find_spec("tensorflow") is not None \
                    or importlib.util.find_spec("tflite_runtime") is not None,
                "cpu_count": multiprocessing.cpu_count(),
            },
        }

    return _supported_processors

def get_perceptor_processor(processor_preference:list) -> Processor:
    """
    Gets the processor for the perceptor.

    # Arguments
    processor_preference (list): The list of preferred processors.

    # Returns
    `Processor` - The processor for the perceptor.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_preference = [Processor.CORAL_EDGE_TPU, Processor.CPU]
    >>> get_perceptor_processor(processor_preference)
    Processor.CORAL_EDGE_TPU
    ```
    """
    validate_processor_list(processor_preference)

    supported_processors = get_supported_processors()
    for processor in processor_preference:
        validate(processor in supported_processors,
                    f"processor {processor} is not valid")
        if supported_processors[processor]["is_available"]:
            return processor

    return None

def validate_processor_preference(processor_preference, model_path_required=True):
    """
    Validate the processor preference.

    # Arguments
    processor_preference: A dictionary of processor preference.
        The key is the processor. Values are dictionaries of model paths and labels.

    # Raises
    Exception: If the processor preference is invalid.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_preference = {
            Processor.CORAL_EDGE_TPU: {
                "model_path": "/path/to/model.tflite",
                "labels_file": "/path/to/labels.txt",
            },
            Processor.CPU: {
                "model_path": "/path/to/model.tflite",
                "labels_file": "/path/to/labels.txt",
            },
        }
    >>> validate_processor_preference(processor_preference)
    ```
    """
    validate_not_none(processor_preference, "processor_preference is required.")
    validate_type(processor_preference, dict, "processor_preference must be a dictionary.")
    validate(len(processor_preference) > 0, "processor_preference must not be empty.")

    for processor, processor_preference_dict in processor_preference.items():
        validate_type(processor, Processor, "processor must be a Processor.")
        validate_type(processor_preference_dict,
                      dict,
                      "processor_preference_dict must be a dictionary.")

        if model_path_required:
            if "model_path" not in processor_preference_dict:
                raise Exception("The model path is required for the processor preference")
            else:
                validate_type(
                    processor_preference_dict["model_path"], str, "model_path must be a string.")

        if "labels" in processor_preference_dict:
            validate_type(
                processor_preference_dict["labels"], dict, "labels must be a dictionary.")

        if "labels_file" in processor_preference_dict:
            validate_type(
                processor_preference_dict["labels_file"], str, "labels_file must be a string.")

def validate_processor_list(processor_list):
    """
    Validate the processor list.

    # Arguments
    processor_list: A list of processors.

    # Raises
    Exception: If the processor preference is invalid.

    # Examples
    ```python
    >>> from darcyai.perceptor.processor import Processor
    >>> processor_list = [Processor.CORAL_EDGE_TPU, Processor.CPU]
    >>> validate_processor_list(processor_list)
    ```
    """
    validate_not_none(processor_list, "processor_list is required.")
    validate_type(processor_list, list, "processor_list must be a list.")
    validate(len(processor_list) > 0, "processor_list must not be empty.")

    for processor in processor_list:
        validate_type(processor, Processor, "processor must be a Processor.")


import nltk
nltk.download('vader_lexicon')
from newspaper import Article
from nltk.sentiment.vader import SentimentIntensityAnalyzer

new_words = {
    'crushes': 10,
    'beats': 5,
    'misses': -5,
    'trouble': -10,
    'falls': -100,
    'bankrupt': -100,
    'fraud': -100,
    'crime': -100,
    'prison': -100,
    'embezzlement': -100,
    'money_trail':-100,
    'jail': -100,
    'sentenced':-100,
    'forfeiture': -100,
    'ill-gotten gain':-100,
    'laundering':-100
}

vader = SentimentIntensityAnalyzer()

vader.lexicon.update(new_words)
#does not work
#url2 = "https://www.bloomberg.com/news/articles/2020-08-01/apple-buys-startup-to-turn-iphones-into-payment-terminals?srnd=premium"

#works
#url2 = "https://hbswk.hbs.edu/item/white-collar-crime-enforcement"
#works
#url2 = 'https://www.straitstimes.com/singapore/courts-crime/doing-more-to-keep-child-sex-offenders-from-making-same-mistake'

najib_news = "https://www.theedgemarkets.com/article/najib-received-massive-rm2973-billion-says-prosecution-forfeiture-hearing"

article = Article(najib_news)
article.download()
article.parse()

news_text = article.text

ss = vader.polarity_scores(news_text)

print(ss)


import dash
from dash import dcc
from dash import html

import plotly.graph_objects as go

import numpy as np

from ClaseDash.graph import *

def create_scatter_plot_1():
    np.random.seed(42)
    random_x= np.random.randint(0,101,100)
    random_y= np.random.randint(0,101,100)

    data= [go.Scatter(x=random_x, y=random_y, mode='markers', name='Random Data')]
    layout=go.Layout(title='My graph on dash')

    return go.Figure(data=data, layout=layout)

app = dash.Dash()

app.layout = html.Div([
    html.H1("Hola Dash!"),
    html.Hr(),
    html.H3("Mi primera app en Dash"),
    html.P("Esta es la primera ocasión en que estoy modificando mi página web"),
    dcc.Graph(
        id = 'graph_1',
        figure=create_scatter_plot_1()
    ),
    dcc.Graph(
        id = 'graph_2',
        figure=create_line_plot()
    ),
    html.Table([
        html.Tr([
            html.Th("Company"),
            html.Th("Contact"),
            html.Th("Country")
        ]),
        html.Tr([
            html.Td("Alfreds Futterkiste"),
            html.Td("Maria Anders"),
            html.Td("Germany")
        ]),
        html.Tr([
            html.Td("Centro comercial Moctezuma"),
            html.Td("Francisco Chang"),
            html.Td("Mexico")
        ]),
    ]),
    html.H5("Lista sin numeros"),
    html.Ul([
        html.Li("Coffee"),
        html.Li("Tea"),
        html.Li("Milk"),
    ]),
    html.H5("Lista enumerada"),
    html.Ol([
        html.Li("Coffee"),
        html.Li("Tea"),
        html.Li("Milk"),
    ]),
    ])

if __name__ == '_main_':
    app.run_server()

    app.layout = html.Div([
    html.H1("Hola Dash!"),
    html.H3("Mi primera app de Dash"),
    html.P("Esta es la muestra de una aplicacion de dash, no hemos creado gráficos pero estamos poniendo información"),
    html.Table([
        html.Tr([
            html.Th("Company"),
            html.Th("Contact"),
            html.Th("Country")
        ]),
        html.Tr([
            html.Td("Alfreds Futterkiste"),
            html.Td("Maria Anders"),
            html.Td("Germany")
        ]),
        html.Tr([
            html.Td("Centro comercial Moctezuma"),
            html.Td("Francisco Chang"),
            html.Td("Mexico")
        ]),
    ])
    ])

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


#!/usr/bin/python
# -*- coding: utf8 -*-

import argparse
import os
import fnmatch
import re
import shutil

# PARSER

DEFAULT_MODE = 1
DEFAULT_WRAP = "__"

parser = argparse.ArgumentParser(
    description='Rename/remove files according to environment code before deploy'
)
parser.add_argument(
    '-e',
    '--environment',
    required=True,
    help='Selected environment'
)
parser.add_argument(
    '-p',
    '--path',
    required=True,
    help='Project root path'
)
parser.add_argument(
    '-w',
    '--wrap',
    type=str,
    nargs='?',
    const=DEFAULT_WRAP,
    default=DEFAULT_WRAP,
    help='Character wrapping environment name'
)
parser.add_argument(
    '-m',
    '--mode',
    type=int,
    nargs='?',
    const=DEFAULT_MODE,
    default=DEFAULT_MODE,
    help='Location of environment code in filename. 0 is before, 1 is just before the extension and 2 is at the end.'
)
parser.add_argument(
    '-v',
    '--verbose',
    action='store_true',
    help='Print what has been renamed or deleted'
)
args = parser.parse_args()

# Generate codes from wrap character and environment name
CODE_ENV = "{}{}{}".format(
    args.wrap,
    args.environment,
    args.wrap
)
CODE_WILDCARD_ENV = "{}{}{}".format(
    args.wrap,
    "*",
    args.wrap
)

# FUNCTIONS

def lremove(pattern, string):
    """
    Remove 'pattern' in 'string' if 'pattern' starts 'string'.
    """
    return re.sub('^%s' % pattern, '', string)

def rremove(pattern, string):
    """
    Remove 'pattern' in 'string' if 'pattern' ends 'string'.
    """
    return re.sub('%s$' % pattern, '', string)

def deletePath(path):
    """
    Same function to remove either file or folder
    """
    if os.path.exists(path):
        try:
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
        except:
            pass

def doBeginning(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is at the
    beginning of the file name
    """
    if fnmatch.fnmatch(file_name, CODE_ENV + "*"):
        new_file_name = lremove(CODE_ENV, file_name)
        new_path = file_path + "/" + new_file_name + file_extension
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name, CODE_WILDCARD_ENV + "*"):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def doBeforeExtension(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is before
    extension in file name
    """
    if fnmatch.fnmatch(file_name, "*" + CODE_ENV):
        new_file_name = rremove(CODE_ENV, file_name)
        new_path = file_path + "/" + new_file_name + file_extension
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name, "*" + CODE_WILDCARD_ENV):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def doEnd(path, file_path, file_name, file_extension):
    """
    Rename or delete files when environment code is at the
    end of file name
    """
    file_name_extension = file_name + file_extension
    if fnmatch.fnmatch(file_name_extension, "*" + CODE_ENV):
        new_file_name = rremove(CODE_ENV, file_name_extension)
        new_path = file_path + "/" + new_file_name
        if args.verbose:
            print("rename {} to {}".format(path, new_path))
        deletePath(new_path)
        os.rename(path, new_path)
    elif fnmatch.fnmatch(file_name_extension, "*" + CODE_WILDCARD_ENV):
        if args.verbose:
            print("delete {}".format(path))
        deletePath(path)

def processPath(path):
    """
    Process path and choose the good method
    """
    file_path = path.rpartition("/")[0]
    file_name = path.rpartition("/")[2]
    file_extension = ""
    if "." in file_name:
        file_extension = file_name.rpartition(".")[1] + file_name.rpartition(".")[2]
    file_name = rremove(file_extension, file_name)

    # Code at the beginning of the filename
    if args.mode == 0:
        doBeginning(path, file_path, file_name, file_extension)
    # Code just before extension
    elif args.mode == 1:
        doBeforeExtension(path, file_path, file_name, file_extension)
    # Code at the end of the filename
    elif args.mode == 2:
        doEnd(path, file_path, file_name, file_extension)

# PROCESS FILES

for root, dirs, files in os.walk(args.path, topdown=False):
    for name in files:
        processPath(os.path.join(root, name))
    for name in dirs:
        processPath(os.path.join(root, name))


from model.android import *
import numpy as np
import torch
from utils import IS_DEBUG
from torch.utils.data import DataLoader,Dataset
import torch.nn as nn
import torch.optim as optim
import os

my_device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")

def malware_loader(x, y):
    res = torch.cat((y.float(), x), dim = 1)
    data_loader = DataLoader(
        res, batch_size=2048,
        shuffle=True, collate_fn=my_collate_fn,
    )
    return data_loader

def my_collate_fn(batch):
    x = [data[1:] for data in batch]
    y = [data[:1].long() for data in batch]
    return torch.stack(x, dim=0), torch.stack(y, dim = 0)

def train_model():
    x = np.load('data/android_malware/x_train.npy')
    y = np.load('data/android_malware/y_train.npy')
    if IS_DEBUG:
        x = x[:1000, :500]
        y = y[:1000]
    data_num, feature_num = np.shape(x)
    print(data_num, feature_num)
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.long)
    train_loader = malware_loader(x, y)
    model = Android_Poor(feature_num).to(my_device)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=0.01)
    model.train()
    for epoch in range(5):
        sum_loss = 0
        for i, (x, y) in enumerate(train_loader):
            print(i)
            optimizer.zero_grad()
            x = x.to(my_device)
            y = y.to(my_device).view([-1])
            pred_y = model(x)
            loss = criterion(pred_y, y)
            loss.backward()
            optimizer.step()
            sum_loss += loss
        print('loss is ', sum_loss/data_num)

    model.eval()

    x = np.load('data/android_malware/x_test.npy')
    y = np.load('data/android_malware/y_test.npy')
    if IS_DEBUG:
        x = x[:1000, :500]
        y = y[:1000]
    data_num, feature_num = np.shape(x)
    print(data_num, feature_num)
    x = torch.tensor(x, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.long)
    train_loader = malware_loader(x, y)
    y_list, pred_list = [], []
    for (x, y) in train_loader:
        optimizer.zero_grad()
        x = x.to(my_device)
        y = y.to(my_device)
        pred_y = model(x)
        _, pred_y = torch.max(pred_y, dim=1)
        y_list.append(y)
        pred_list.append(pred_y)
    pred_list = torch.cat(pred_list, dim=0).detach().cpu().numpy().reshape([-1])
    y_list = torch.cat(y_list, dim=0).detach().cpu().numpy().reshape([-1])
    print(np.sum(pred_list == y_list)/data_num)
    path = './model_weight/android/'
    if not os.path.isdir(path):
        os.mkdir(path)
    torch.save(model.state_dict(), path + model.__class__.__name__ + '.h5')
    print('save model !', path + model.__class__.__name__ + '.h5')

if __name__ == '__main__':
    train_model()


#!/usr/bin/env python
# -*- coding:utf-8 -*-

import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
import math
import time
import re
import yaml

class CMTFollower(object):
    def __init__(self):
        f = open('/home/robot/ros_workspace/SC0_ws/src/ocean_vision/config/tracker.yaml', 'r')
        config = yaml.load(f.read())
        self.FRAME_WIDTH = config['Video']['FRAME_WIDTH']
        self.FRAME_HEIGHT = config['Video']['FRAME_HEIGHT']
        self.dist_default = config['SmartCar']['dist_default']

        self.center_delta = 0
        self.dist_delta = 0
        self.if_track = False
        self.do_job = True

        rospy.init_node("cmt_tracker")

        self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
        rospy.loginfo("wait for the action server to come up")
        self.move_base.wait_for_server(rospy.Duration(5))
        rospy.loginfo("waiting done")

        self.rate = rospy.get_param("~rate", 30)
        self.r = rospy.Rate(self.rate)

        rospy.on_shutdown(self.shutdown)
        self.run()

    def callback(self, data):
        # rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)
        cmd_message = re.findall(re.compile(r'\$\$.*?\&\&'), data.data)
        if len(cmd_message) > 0:
            cmd_para = re.findall(re.compile(r'[\d.]+'), cmd_message[-1])
            # print('LCH: the cmd para is ', cmd_para)
            if len(cmd_para)==4:
                centerx = float(cmd_para[0])
                dist = float(cmd_para[2])
                self.center_delta = (self.FRAME_WIDTH/2 - centerx)/float(self.FRAME_WIDTH)
                self.dist_delta = -(self.dist_default - dist)
                self.if_track = True
            else:
                rospy.loginfo("The target lost, need redetect ...")
                self.if_track = False

    def run(self):
        rospy.Subscriber('chatter', String, self.callback)
        while self.do_job:
            if self.if_track:
                goal = MoveBaseGoal()
                goal.target_pose.header.frame_id = 'base_link'
                goal.target_pose.header.stamp = rospy.Time.now()
                goal.target_pose.pose.position.x = self.dist_delta
                goal.target_pose.pose.position.y = self.center_delta
                goal.target_pose.pose.orientation.w = 1.0 #go forward
                print('LCH: the goal is ', goal)

                self.move_base.send_goal(goal)
                # success = self.move_base.wait_for_result(rospy.Duration(10))
                #
                # if not success:
                #     self.move_base.cancel_goal()
                #     rospy.loginfo("The base failed to move forward 3 meters for some reason")
                #     continue
                # else:
                #     # We made it!
                #     state = self.move_base.get_state()
                #     if state == GoalStatus.SUCCEEDED:
                #         rospy.loginfo("Hooray, the base moved 3 meters forward")

            else:
                rospy.loginfo("Holding")

            # self.r.sleep()
            time.sleep(2)

    def shutdown(self):
        rospy.loginfo("Stopping the robot...")
        self.do_job = False
        exit(0)

if __name__ == '__main__':
    try:
        CMTFollower()
        rospy.spin()
    except rospy.ROSInterruptException:
        rospy.loginfo("CMT follower node terminated.")


"""Training script for CheXpert"""
import argparse
import numpy as np
import pandas as pd
import torch
import tqdm
import util

from constants        import *
from args             import CheXpertTrainArgParser
from dataset.chexpert import get_dataloader
from logger           import Logger
from models           import CheXpertModel

# Reproducibility
torch.manual_seed(6)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)

def train(args):

    # get transformations
    train_trainsforms = util.transformation_composer(args, "train", "CheXpert")
    valid_trainsforms = util.transformation_composer(args, "valid", "CheXpert")

    # get dataloader
    train_loader = get_dataloader(args, train_trainsforms, "train", "CheXpert")
    valid_loader = get_dataloader(args, valid_trainsforms, "valid", "CheXpert")

    # get model and put on device
    model = CheXpertModel(
        model_name=args.model_name,
        num_classes=14
    )
    if args.device == "cuda":
        model = torch.nn.DataParallel(model, args.gpu_ids)
    model = model.to(args.device)

    # optimizer
    optimizer = util.set_optimizer(opt=args, model=model)

    # loss function
    loss_fn = torch.nn.BCEWithLogitsLoss(reduction="mean")

    # define logger
    logger = Logger(
        log_dir=args.log_dir,
        metrics_name=args.eval_metrics,
        args=args
    )

    # iterate over epoch
    global_step = 0
    model.train()
    for epoch in range(args.num_epoch):

        # training loop
        for inputs, targets in tqdm.tqdm(train_loader, desc=f"[epoch {epoch}]"):

            # validate every N training iteration
            if global_step % args.iters_per_eval == 0:
                model.eval()
                probs, gt = [], []
                with torch.no_grad():

                    # validation loop
                    for val_inputs, val_targets in valid_loader:

                        batch_logits = model(val_inputs.to(args.device))
                        batch_probs = torch.sigmoid(batch_logits)

                        probs.append(batch_probs.cpu())
                        gt.append(val_targets.cpu())

                # evaluate results
                metrics = util.evaluate(probs, gt, args.threshold)
                avg_metric, metric_dict = util.aggregate_metrics(metrics)

                # log image
                logger.log_iteration(metric_dict, global_step, "val")
                logger.log_dict(avg_metric, global_step, "val")
                logger.log_image(inputs, global_step)

                # save checkpoint
                logger.save_checkpoint(model, metrics, global_step)

                model.to(args.device)

            model.train()
            with torch.set_grad_enabled(True):

                # Run the minibatch through the model.
                logits = model(inputs.to(args.device))

                # Compute the minibatch loss.
                loss = loss_fn(logits, targets.to(args.device))

                logger.log_dict({"train/loss": loss}, global_step, "train")

                # Perform a backward pass.
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

            global_step += 1

if __name__ == "__main__":

    # get argumetns
    parser = CheXpertTrainArgParser()
    args = parser.parse_args()
    train(args)


import os

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(context='notebook',
        style='whitegrid',
        palette='deep',
        font='sans-serif',
        font_scale=1)

def fitness_plot(dir):
    print(os.getcwd())
    with open(dir + "loss.txt") as f:
        lines = f.readlines()
    fitness = [float(line[:-2]) for line in lines]

    plt.plot(fitness)
    plt.xlabel("Generations")
    plt.ylabel("Fitness")
    plt.savefig(dir + "fitness.png")
    plt.close()


'''
Write a script that takes a string from the user and creates a dictionary of letter that exist
in the string and the number of times they occur. For example:

user_input = "hello"
result = {"h": 1, "e": 1, "l": 2, "o": 1}

'''
user_input = input("Type a word: ")
user_input_list = list(user_input)

from collections import Counter
cc = Counter(user_input_list)
dict1 = dict(cc)

print(dict1)


import torch
from torch import nn

from src.data.data import mnist

from models.model import MyAwesomeModel

def evaluate(model_checkpoint):
    print("Evaluating until hitting the ceiling")
    print(model_checkpoint)

    checkpoint = torch.load(model_checkpoint)
    model = MyAwesomeModel()
    model.load_state_dict(checkpoint["state_dict"])
    _, test_set = mnist()
    criterion = nn.NLLLoss()
    accuracy = 0
    loss = 0
    _, testloader = mnist()

    for images, labels in testloader:
        output = model.forward(images)

        labels = labels.type(torch.LongTensor)
        loss += criterion(output, labels).item()

        ps = torch.exp(output)

        temp = labels.data == ps.max(1)[1]

        accuracy += temp.type_as(torch.FloatTensor()).mean()

        print(
            "Test Loss: {:.3f}.. ".format(loss / len(testloader)),
            "Test Accuracy: {:.3f}".format(accuracy / len(testloader)),
        )


import os.path
from setuptools import setup, find_packages

package_dir = os.path.abspath(os.path.dirname(__file__))
version_file = os.path.join(package_dir, "version")
with open(version_file) as version_file_handle:
    version = version_file_handle.read()

setup(
    name = "message-api",
    version = version,
    description = "Message API",
    packages = ["message_api"],
    install_requires=["requests"],
    url = 'https://github.com/message-api/message-api-python',
    keywords = ['channel', 'api', 'message'],
    classifiers = []
)


import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.style.use('seaborn')

# Get the path
args = {}
args['cwd'] = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
args['simulation_data'] = os.path.join(args['cwd'], 'data', 'derived', 'GNAR_GARCH_simulation')
args['simulation_figure'] = os.path.join(args['cwd'], 'figures', 'GNAR_GARCH_simulation')

# Read the data
observations = pd.read_csv(os.path.join(args['simulation_data'], 'GG_one_simulation_data.csv'),index_col=0)
fitted_values1 = pd.read_csv(os.path.join(args['simulation_data'], 'GG_one_simulation_fittedvalues_gnar.csv'),index_col=0)
fitted_values2 = pd.read_csv(os.path.join(args['simulation_data'], 'GG_one_simulation_fittedvalues_gnar_garch.csv'),index_col=0)
residuals1 = pd.read_csv(os.path.join(args['simulation_data'], 'GG_one_simulation_residuals_gnar.csv'),index_col=0)
conditional_std_node1 = pd.read_csv(os.path.join(args['simulation_data'], 'GG_one_simulation_conditional_std_gnar_garch_node1.csv'),index_col=0)

# Plot the data
fig, ax = plt.subplots(2, 1, figsize=(12, 8))

ax[0].plot(range(1,1001), observations.iloc[:,0], color='black', label='Observations', linewidth=0.5)
ax[0].plot(range(3,1001), fitted_values1.iloc[:,0], color='red', label='Fitted values', linewidth=0.5)
ax[0].fill_between(range(3,1001), fitted_values1.iloc[:,0]+1.96*np.std(residuals1.values),
                   fitted_values1.iloc[:,0]-1.96*np.std(residuals1.iloc[:,0]), color='blue',
                   alpha=0.15, label='95% interval')
ax[0].set_ylabel('')
ax[0].set_xticks([])
ax[0].set_ylim([-8,8])
ax[0].set_title('GNAR(2,{2,1})', fontsize=15)
ax[0].legend(loc = 'lower right', fontsize=12)

ax[1].plot(range(1,1001), observations.iloc[:,0], color='black', label='Observations', linewidth=0.5)
ax[1].plot(range(3,1001), fitted_values2.iloc[:,0], color='red', label='Fitted values', linewidth=0.5)
ax[1].fill_between(range(3,1001), fitted_values1.iloc[:,0]+1.96*conditional_std_node1.iloc[:,0],
                   fitted_values1.iloc[:,0]-1.96*conditional_std_node1.iloc[:,0], color='blue',
                   alpha=0.15, label='95% interval')
ax[1].set_ylabel('')
ax[1].set_ylim([-8,8])
ax[1].set_title('GNAR(2,{2,1}) + GARCH(1,1)', fontsize=15)
ax[1].legend(loc = 'lower right', fontsize=12)

plt.tight_layout()
plt.savefig(os.path.join(args['simulation_figure'],'GG_one_simulation_fit_plot.pdf'))